bgp_attr.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718
  1. /* BGP attributes management routines.
  2. Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
  3. This file is part of GNU Zebra.
  4. GNU Zebra is free software; you can redistribute it and/or modify it
  5. under the terms of the GNU General Public License as published by the
  6. Free Software Foundation; either version 2, or (at your option) any
  7. later version.
  8. GNU Zebra is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Zebra; see the file COPYING. If not, write to the Free
  14. Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  15. 02111-1307, USA. */
  16. #include <zebra.h>
  17. #include "linklist.h"
  18. #include "prefix.h"
  19. #include "memory.h"
  20. #include "vector.h"
  21. #include "vty.h"
  22. #include "stream.h"
  23. #include "log.h"
  24. #include "hash.h"
  25. #include "jhash.h"
  26. #include "bgpd/bgpd.h"
  27. #include "bgpd/bgp_attr.h"
  28. #include "bgpd/bgp_route.h"
  29. #include "bgpd/bgp_aspath.h"
  30. #include "bgpd/bgp_community.h"
  31. #include "bgpd/bgp_debug.h"
  32. #include "bgpd/bgp_packet.h"
  33. #include "bgpd/bgp_ecommunity.h"
  34. /* Attribute strings for logging. */
  35. static const struct message attr_str [] =
  36. {
  37. { BGP_ATTR_ORIGIN, "ORIGIN" },
  38. { BGP_ATTR_AS_PATH, "AS_PATH" },
  39. { BGP_ATTR_NEXT_HOP, "NEXT_HOP" },
  40. { BGP_ATTR_MULTI_EXIT_DISC, "MULTI_EXIT_DISC" },
  41. { BGP_ATTR_LOCAL_PREF, "LOCAL_PREF" },
  42. { BGP_ATTR_ATOMIC_AGGREGATE, "ATOMIC_AGGREGATE" },
  43. { BGP_ATTR_AGGREGATOR, "AGGREGATOR" },
  44. { BGP_ATTR_COMMUNITIES, "COMMUNITY" },
  45. { BGP_ATTR_ORIGINATOR_ID, "ORIGINATOR_ID" },
  46. { BGP_ATTR_CLUSTER_LIST, "CLUSTER_LIST" },
  47. { BGP_ATTR_DPA, "DPA" },
  48. { BGP_ATTR_ADVERTISER, "ADVERTISER"} ,
  49. { BGP_ATTR_RCID_PATH, "RCID_PATH" },
  50. { BGP_ATTR_MP_REACH_NLRI, "MP_REACH_NLRI" },
  51. { BGP_ATTR_MP_UNREACH_NLRI, "MP_UNREACH_NLRI" },
  52. { BGP_ATTR_EXT_COMMUNITIES, "EXT_COMMUNITIES" },
  53. { BGP_ATTR_AS4_PATH, "AS4_PATH" },
  54. { BGP_ATTR_AS4_AGGREGATOR, "AS4_AGGREGATOR" },
  55. { BGP_ATTR_AS_PATHLIMIT, "AS_PATHLIMIT" },
  56. };
  57. static const int attr_str_max = array_size(attr_str);
  58. static const struct message attr_flag_str[] =
  59. {
  60. { BGP_ATTR_FLAG_OPTIONAL, "Optional" },
  61. { BGP_ATTR_FLAG_TRANS, "Transitive" },
  62. { BGP_ATTR_FLAG_PARTIAL, "Partial" },
  63. /* bgp_attr_flags_diagnose() relies on this bit being last in this list */
  64. { BGP_ATTR_FLAG_EXTLEN, "Extended Length" },
  65. };
  66. static const size_t attr_flag_str_max = array_size(attr_flag_str);
  67. static struct hash *cluster_hash;
  68. static void *
  69. cluster_hash_alloc (void *p)
  70. {
  71. struct cluster_list * val = (struct cluster_list *) p;
  72. struct cluster_list *cluster;
  73. cluster = XMALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
  74. cluster->length = val->length;
  75. if (cluster->length)
  76. {
  77. cluster->list = XMALLOC (MTYPE_CLUSTER_VAL, val->length);
  78. memcpy (cluster->list, val->list, val->length);
  79. }
  80. else
  81. cluster->list = NULL;
  82. cluster->refcnt = 0;
  83. return cluster;
  84. }
  85. /* Cluster list related functions. */
  86. static struct cluster_list *
  87. cluster_parse (struct in_addr * pnt, int length)
  88. {
  89. struct cluster_list tmp;
  90. struct cluster_list *cluster;
  91. tmp.length = length;
  92. tmp.list = pnt;
  93. cluster = hash_get (cluster_hash, &tmp, cluster_hash_alloc);
  94. cluster->refcnt++;
  95. return cluster;
  96. }
  97. int
  98. cluster_loop_check (struct cluster_list *cluster, struct in_addr originator)
  99. {
  100. int i;
  101. for (i = 0; i < cluster->length / 4; i++)
  102. if (cluster->list[i].s_addr == originator.s_addr)
  103. return 1;
  104. return 0;
  105. }
  106. static unsigned int
  107. cluster_hash_key_make (void *p)
  108. {
  109. const struct cluster_list *cluster = p;
  110. return jhash(cluster->list, cluster->length, 0);
  111. }
  112. static int
  113. cluster_hash_cmp (const void *p1, const void *p2)
  114. {
  115. const struct cluster_list * cluster1 = p1;
  116. const struct cluster_list * cluster2 = p2;
  117. return (cluster1->length == cluster2->length &&
  118. memcmp (cluster1->list, cluster2->list, cluster1->length) == 0);
  119. }
  120. static void
  121. cluster_free (struct cluster_list *cluster)
  122. {
  123. if (cluster->list)
  124. XFREE (MTYPE_CLUSTER_VAL, cluster->list);
  125. XFREE (MTYPE_CLUSTER, cluster);
  126. }
  127. #if 0
  128. static struct cluster_list *
  129. cluster_dup (struct cluster_list *cluster)
  130. {
  131. struct cluster_list *new;
  132. new = XCALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
  133. new->length = cluster->length;
  134. if (cluster->length)
  135. {
  136. new->list = XMALLOC (MTYPE_CLUSTER_VAL, cluster->length);
  137. memcpy (new->list, cluster->list, cluster->length);
  138. }
  139. else
  140. new->list = NULL;
  141. return new;
  142. }
  143. #endif
  144. static struct cluster_list *
  145. cluster_intern (struct cluster_list *cluster)
  146. {
  147. struct cluster_list *find;
  148. find = hash_get (cluster_hash, cluster, cluster_hash_alloc);
  149. find->refcnt++;
  150. return find;
  151. }
  152. void
  153. cluster_unintern (struct cluster_list *cluster)
  154. {
  155. if (cluster->refcnt)
  156. cluster->refcnt--;
  157. if (cluster->refcnt == 0)
  158. {
  159. hash_release (cluster_hash, cluster);
  160. cluster_free (cluster);
  161. }
  162. }
  163. static void
  164. cluster_init (void)
  165. {
  166. cluster_hash = hash_create (cluster_hash_key_make, cluster_hash_cmp);
  167. }
  168. static void
  169. cluster_finish (void)
  170. {
  171. hash_free (cluster_hash);
  172. cluster_hash = NULL;
  173. }
  174. /* Unknown transit attribute. */
  175. static struct hash *transit_hash;
  176. static void
  177. transit_free (struct transit *transit)
  178. {
  179. if (transit->val)
  180. XFREE (MTYPE_TRANSIT_VAL, transit->val);
  181. XFREE (MTYPE_TRANSIT, transit);
  182. }
  183. static void *
  184. transit_hash_alloc (void *p)
  185. {
  186. /* Transit structure is already allocated. */
  187. return p;
  188. }
  189. static struct transit *
  190. transit_intern (struct transit *transit)
  191. {
  192. struct transit *find;
  193. find = hash_get (transit_hash, transit, transit_hash_alloc);
  194. if (find != transit)
  195. transit_free (transit);
  196. find->refcnt++;
  197. return find;
  198. }
  199. void
  200. transit_unintern (struct transit *transit)
  201. {
  202. if (transit->refcnt)
  203. transit->refcnt--;
  204. if (transit->refcnt == 0)
  205. {
  206. hash_release (transit_hash, transit);
  207. transit_free (transit);
  208. }
  209. }
  210. static unsigned int
  211. transit_hash_key_make (void *p)
  212. {
  213. const struct transit * transit = p;
  214. return jhash(transit->val, transit->length, 0);
  215. }
  216. static int
  217. transit_hash_cmp (const void *p1, const void *p2)
  218. {
  219. const struct transit * transit1 = p1;
  220. const struct transit * transit2 = p2;
  221. return (transit1->length == transit2->length &&
  222. memcmp (transit1->val, transit2->val, transit1->length) == 0);
  223. }
  224. static void
  225. transit_init (void)
  226. {
  227. transit_hash = hash_create (transit_hash_key_make, transit_hash_cmp);
  228. }
  229. static void
  230. transit_finish (void)
  231. {
  232. hash_free (transit_hash);
  233. transit_hash = NULL;
  234. }
  235. /* Attribute hash routines. */
  236. static struct hash *attrhash;
  237. static struct attr_extra *
  238. bgp_attr_extra_new (void)
  239. {
  240. return XCALLOC (MTYPE_ATTR_EXTRA, sizeof (struct attr_extra));
  241. }
  242. void
  243. bgp_attr_extra_free (struct attr *attr)
  244. {
  245. if (attr->extra)
  246. {
  247. XFREE (MTYPE_ATTR_EXTRA, attr->extra);
  248. attr->extra = NULL;
  249. }
  250. }
  251. struct attr_extra *
  252. bgp_attr_extra_get (struct attr *attr)
  253. {
  254. if (!attr->extra)
  255. attr->extra = bgp_attr_extra_new();
  256. return attr->extra;
  257. }
  258. /* Shallow copy of an attribute
  259. * Though, not so shallow that it doesn't copy the contents
  260. * of the attr_extra pointed to by 'extra'
  261. */
  262. void
  263. bgp_attr_dup (struct attr *new, struct attr *orig)
  264. {
  265. struct attr_extra *extra = new->extra;
  266. *new = *orig;
  267. /* if caller provided attr_extra space, use it in any case.
  268. *
  269. * This is neccesary even if orig->extra equals NULL, because otherwise
  270. * memory may be later allocated on the heap by bgp_attr_extra_get.
  271. *
  272. * That memory would eventually be leaked, because the caller must not
  273. * call bgp_attr_extra_free if he provided attr_extra on the stack.
  274. */
  275. if (extra)
  276. {
  277. new->extra = extra;
  278. memset(new->extra, 0, sizeof(struct attr_extra));
  279. if (orig->extra)
  280. *new->extra = *orig->extra;
  281. }
  282. else if (orig->extra)
  283. {
  284. new->extra = bgp_attr_extra_new();
  285. *new->extra = *orig->extra;
  286. }
  287. }
  288. unsigned long int
  289. attr_count (void)
  290. {
  291. return attrhash->count;
  292. }
  293. unsigned long int
  294. attr_unknown_count (void)
  295. {
  296. return transit_hash->count;
  297. }
  298. unsigned int
  299. attrhash_key_make (void *p)
  300. {
  301. const struct attr *attr = (struct attr *) p;
  302. const struct attr_extra *extra = attr->extra;
  303. uint32_t key = 0;
  304. #define MIX(val) key = jhash_1word(val, key)
  305. MIX(attr->origin);
  306. MIX(attr->nexthop.s_addr);
  307. MIX(attr->med);
  308. MIX(attr->local_pref);
  309. key += attr->origin;
  310. key += attr->nexthop.s_addr;
  311. key += attr->med;
  312. key += attr->local_pref;
  313. if (extra)
  314. {
  315. MIX(extra->aggregator_as);
  316. MIX(extra->aggregator_addr.s_addr);
  317. MIX(extra->weight);
  318. MIX(extra->mp_nexthop_global_in.s_addr);
  319. MIX(extra->originator_id.s_addr);
  320. }
  321. if (attr->aspath)
  322. MIX(aspath_key_make (attr->aspath));
  323. if (attr->community)
  324. MIX(community_hash_make (attr->community));
  325. if (extra)
  326. {
  327. if (extra->ecommunity)
  328. MIX(ecommunity_hash_make (extra->ecommunity));
  329. if (extra->cluster)
  330. MIX(cluster_hash_key_make (extra->cluster));
  331. if (extra->transit)
  332. MIX(transit_hash_key_make (extra->transit));
  333. #ifdef HAVE_IPV6
  334. MIX(extra->mp_nexthop_len);
  335. key = jhash(extra->mp_nexthop_global.s6_addr, 16, key);
  336. key = jhash(extra->mp_nexthop_local.s6_addr, 16, key);
  337. #endif /* HAVE_IPV6 */
  338. }
  339. return key;
  340. }
  341. int
  342. attrhash_cmp (const void *p1, const void *p2)
  343. {
  344. const struct attr * attr1 = p1;
  345. const struct attr * attr2 = p2;
  346. if (attr1->flag == attr2->flag
  347. && attr1->origin == attr2->origin
  348. && attr1->nexthop.s_addr == attr2->nexthop.s_addr
  349. && attr1->aspath == attr2->aspath
  350. && attr1->community == attr2->community
  351. && attr1->med == attr2->med
  352. && attr1->local_pref == attr2->local_pref)
  353. {
  354. const struct attr_extra *ae1 = attr1->extra;
  355. const struct attr_extra *ae2 = attr2->extra;
  356. if (ae1 && ae2
  357. && ae1->aggregator_as == ae2->aggregator_as
  358. && ae1->aggregator_addr.s_addr == ae2->aggregator_addr.s_addr
  359. && ae1->weight == ae2->weight
  360. #ifdef HAVE_IPV6
  361. && ae1->mp_nexthop_len == ae2->mp_nexthop_len
  362. && IPV6_ADDR_SAME (&ae1->mp_nexthop_global, &ae2->mp_nexthop_global)
  363. && IPV6_ADDR_SAME (&ae1->mp_nexthop_local, &ae2->mp_nexthop_local)
  364. #endif /* HAVE_IPV6 */
  365. && IPV4_ADDR_SAME (&ae1->mp_nexthop_global_in, &ae2->mp_nexthop_global_in)
  366. && ae1->ecommunity == ae2->ecommunity
  367. && ae1->cluster == ae2->cluster
  368. && ae1->transit == ae2->transit
  369. && IPV4_ADDR_SAME (&ae1->originator_id, &ae2->originator_id))
  370. return 1;
  371. else if (ae1 || ae2)
  372. return 0;
  373. /* neither attribute has extra attributes, so they're same */
  374. return 1;
  375. }
  376. else
  377. return 0;
  378. }
  379. static void
  380. attrhash_init (void)
  381. {
  382. attrhash = hash_create (attrhash_key_make, attrhash_cmp);
  383. }
  384. static void
  385. attrhash_finish (void)
  386. {
  387. hash_free (attrhash);
  388. attrhash = NULL;
  389. }
  390. static void
  391. attr_show_all_iterator (struct hash_backet *backet, struct vty *vty)
  392. {
  393. struct attr *attr = backet->data;
  394. vty_out (vty, "attr[%ld] nexthop %s%s", attr->refcnt,
  395. inet_ntoa (attr->nexthop), VTY_NEWLINE);
  396. }
  397. void
  398. attr_show_all (struct vty *vty)
  399. {
  400. hash_iterate (attrhash,
  401. (void (*)(struct hash_backet *, void *))
  402. attr_show_all_iterator,
  403. vty);
  404. }
  405. static void *
  406. bgp_attr_hash_alloc (void *p)
  407. {
  408. struct attr * val = (struct attr *) p;
  409. struct attr *attr;
  410. attr = XMALLOC (MTYPE_ATTR, sizeof (struct attr));
  411. *attr = *val;
  412. if (val->extra)
  413. {
  414. attr->extra = bgp_attr_extra_new ();
  415. *attr->extra = *val->extra;
  416. }
  417. attr->refcnt = 0;
  418. return attr;
  419. }
  420. /* Internet argument attribute. */
  421. struct attr *
  422. bgp_attr_intern (struct attr *attr)
  423. {
  424. struct attr *find;
  425. /* Intern referenced strucutre. */
  426. if (attr->aspath)
  427. {
  428. if (! attr->aspath->refcnt)
  429. attr->aspath = aspath_intern (attr->aspath);
  430. else
  431. attr->aspath->refcnt++;
  432. }
  433. if (attr->community)
  434. {
  435. if (! attr->community->refcnt)
  436. attr->community = community_intern (attr->community);
  437. else
  438. attr->community->refcnt++;
  439. }
  440. if (attr->extra)
  441. {
  442. struct attr_extra *attre = attr->extra;
  443. if (attre->ecommunity)
  444. {
  445. if (! attre->ecommunity->refcnt)
  446. attre->ecommunity = ecommunity_intern (attre->ecommunity);
  447. else
  448. attre->ecommunity->refcnt++;
  449. }
  450. if (attre->cluster)
  451. {
  452. if (! attre->cluster->refcnt)
  453. attre->cluster = cluster_intern (attre->cluster);
  454. else
  455. attre->cluster->refcnt++;
  456. }
  457. if (attre->transit)
  458. {
  459. if (! attre->transit->refcnt)
  460. attre->transit = transit_intern (attre->transit);
  461. else
  462. attre->transit->refcnt++;
  463. }
  464. }
  465. find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc);
  466. find->refcnt++;
  467. return find;
  468. }
  469. /* Make network statement's attribute. */
  470. struct attr *
  471. bgp_attr_default_set (struct attr *attr, u_char origin)
  472. {
  473. memset (attr, 0, sizeof (struct attr));
  474. bgp_attr_extra_get (attr);
  475. attr->origin = origin;
  476. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
  477. attr->aspath = aspath_empty ();
  478. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
  479. attr->extra->weight = BGP_ATTR_DEFAULT_WEIGHT;
  480. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
  481. #ifdef HAVE_IPV6
  482. attr->extra->mp_nexthop_len = IPV6_MAX_BYTELEN;
  483. #endif
  484. return attr;
  485. }
  486. /* Make network statement's attribute. */
  487. struct attr *
  488. bgp_attr_default_intern (u_char origin)
  489. {
  490. struct attr attr;
  491. struct attr *new;
  492. bgp_attr_default_set(&attr, origin);
  493. new = bgp_attr_intern (&attr);
  494. bgp_attr_extra_free (&attr);
  495. aspath_unintern (&new->aspath);
  496. return new;
  497. }
  498. struct attr *
  499. bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin,
  500. struct aspath *aspath,
  501. struct community *community, int as_set)
  502. {
  503. struct attr attr;
  504. struct attr *new;
  505. struct attr_extra attre;
  506. memset (&attr, 0, sizeof (struct attr));
  507. memset (&attre, 0, sizeof (struct attr_extra));
  508. attr.extra = &attre;
  509. /* Origin attribute. */
  510. attr.origin = origin;
  511. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
  512. /* AS path attribute. */
  513. if (aspath)
  514. attr.aspath = aspath_intern (aspath);
  515. else
  516. attr.aspath = aspath_empty ();
  517. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
  518. /* Next hop attribute. */
  519. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
  520. if (community)
  521. {
  522. attr.community = community;
  523. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
  524. }
  525. attre.weight = BGP_ATTR_DEFAULT_WEIGHT;
  526. #ifdef HAVE_IPV6
  527. attre.mp_nexthop_len = IPV6_MAX_BYTELEN;
  528. #endif
  529. if (! as_set)
  530. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
  531. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
  532. if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION))
  533. attre.aggregator_as = bgp->confed_id;
  534. else
  535. attre.aggregator_as = bgp->as;
  536. attre.aggregator_addr = bgp->router_id;
  537. new = bgp_attr_intern (&attr);
  538. aspath_unintern (&new->aspath);
  539. return new;
  540. }
  541. /* Unintern just the sub-components of the attr, but not the attr */
  542. void
  543. bgp_attr_unintern_sub (struct attr *attr)
  544. {
  545. /* aspath refcount shoud be decrement. */
  546. if (attr->aspath)
  547. aspath_unintern (&attr->aspath);
  548. UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH));
  549. if (attr->community)
  550. community_unintern (&attr->community);
  551. UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES));
  552. if (attr->extra)
  553. {
  554. if (attr->extra->ecommunity)
  555. ecommunity_unintern (&attr->extra->ecommunity);
  556. UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES));
  557. if (attr->extra->cluster)
  558. cluster_unintern (attr->extra->cluster);
  559. UNSET_FLAG(attr->flag, ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST));
  560. if (attr->extra->transit)
  561. transit_unintern (attr->extra->transit);
  562. }
  563. }
  564. /* Free bgp attribute and aspath. */
  565. void
  566. bgp_attr_unintern (struct attr **pattr)
  567. {
  568. struct attr *attr = *pattr;
  569. struct attr *ret;
  570. struct attr tmp;
  571. struct attr_extra tmp_extra;
  572. /* Decrement attribute reference. */
  573. attr->refcnt--;
  574. tmp = *attr;
  575. if (attr->extra)
  576. {
  577. tmp.extra = &tmp_extra;
  578. memcpy (tmp.extra, attr->extra, sizeof (struct attr_extra));
  579. }
  580. /* If reference becomes zero then free attribute object. */
  581. if (attr->refcnt == 0)
  582. {
  583. ret = hash_release (attrhash, attr);
  584. assert (ret != NULL);
  585. bgp_attr_extra_free (attr);
  586. XFREE (MTYPE_ATTR, attr);
  587. *pattr = NULL;
  588. }
  589. bgp_attr_unintern_sub (&tmp);
  590. }
  591. void
  592. bgp_attr_flush (struct attr *attr)
  593. {
  594. if (attr->aspath && ! attr->aspath->refcnt)
  595. aspath_free (attr->aspath);
  596. if (attr->community && ! attr->community->refcnt)
  597. community_free (attr->community);
  598. if (attr->extra)
  599. {
  600. struct attr_extra *attre = attr->extra;
  601. if (attre->ecommunity && ! attre->ecommunity->refcnt)
  602. ecommunity_free (&attre->ecommunity);
  603. if (attre->cluster && ! attre->cluster->refcnt)
  604. cluster_free (attre->cluster);
  605. if (attre->transit && ! attre->transit->refcnt)
  606. transit_free (attre->transit);
  607. }
  608. }
  609. /* Implement draft-scudder-idr-optional-transitive behaviour and
  610. * avoid resetting sessions for malformed attributes which are
  611. * are partial/optional and hence where the error likely was not
  612. * introduced by the sending neighbour.
  613. */
  614. static bgp_attr_parse_ret_t
  615. bgp_attr_malformed (struct bgp_attr_parser_args *args, u_char subcode,
  616. bgp_size_t length)
  617. {
  618. struct peer *const peer = args->peer;
  619. const u_int8_t flags = args->flags;
  620. /* startp and length must be special-cased, as whether or not to
  621. * send the attribute data with the NOTIFY depends on the error,
  622. * the caller therefore signals this with the seperate length argument
  623. */
  624. u_char *notify_datap = (length > 0 ? args->startp : NULL);
  625. /* Only relax error handling for eBGP peers */
  626. if (peer->sort != BGP_PEER_EBGP)
  627. {
  628. bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode,
  629. notify_datap, length);
  630. return BGP_ATTR_PARSE_ERROR;
  631. }
  632. /* Adjust the stream getp to the end of the attribute, in case we can
  633. * still proceed but the caller hasn't read all the attribute.
  634. */
  635. stream_set_getp (BGP_INPUT (peer),
  636. (args->startp - STREAM_DATA (BGP_INPUT (peer)))
  637. + args->total);
  638. switch (args->type) {
  639. /* where an attribute is relatively inconsequential, e.g. it does not
  640. * affect route selection, and can be safely ignored, then any such
  641. * attributes which are malformed should just be ignored and the route
  642. * processed as normal.
  643. */
  644. case BGP_ATTR_AS4_AGGREGATOR:
  645. case BGP_ATTR_AGGREGATOR:
  646. case BGP_ATTR_ATOMIC_AGGREGATE:
  647. return BGP_ATTR_PARSE_PROCEED;
  648. /* Core attributes, particularly ones which may influence route
  649. * selection, should always cause session resets
  650. */
  651. case BGP_ATTR_ORIGIN:
  652. case BGP_ATTR_AS_PATH:
  653. case BGP_ATTR_NEXT_HOP:
  654. case BGP_ATTR_MULTI_EXIT_DISC:
  655. case BGP_ATTR_LOCAL_PREF:
  656. case BGP_ATTR_COMMUNITIES:
  657. case BGP_ATTR_ORIGINATOR_ID:
  658. case BGP_ATTR_CLUSTER_LIST:
  659. case BGP_ATTR_MP_REACH_NLRI:
  660. case BGP_ATTR_MP_UNREACH_NLRI:
  661. case BGP_ATTR_EXT_COMMUNITIES:
  662. bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR, subcode,
  663. notify_datap, length);
  664. return BGP_ATTR_PARSE_ERROR;
  665. }
  666. /* Partial optional attributes that are malformed should not cause
  667. * the whole session to be reset. Instead treat it as a withdrawal
  668. * of the routes, if possible.
  669. */
  670. if (CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS)
  671. && CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
  672. && CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL))
  673. return BGP_ATTR_PARSE_WITHDRAW;
  674. /* default to reset */
  675. return BGP_ATTR_PARSE_ERROR;
  676. }
  677. /* Find out what is wrong with the path attribute flag bits and log the error.
  678. "Flag bits" here stand for Optional, Transitive and Partial, but not for
  679. Extended Length. Checking O/T/P bits at once implies, that the attribute
  680. being diagnosed is defined by RFC as either a "well-known" or an "optional,
  681. non-transitive" attribute. */
  682. static void
  683. bgp_attr_flags_diagnose (struct bgp_attr_parser_args *args,
  684. u_int8_t desired_flags /* how RFC says it must be */
  685. )
  686. {
  687. u_char seen = 0, i;
  688. u_char real_flags = args->flags;
  689. const u_int8_t attr_code = args->type;
  690. desired_flags &= ~BGP_ATTR_FLAG_EXTLEN;
  691. real_flags &= ~BGP_ATTR_FLAG_EXTLEN;
  692. for (i = 0; i <= 2; i++) /* O,T,P, but not E */
  693. if
  694. (
  695. CHECK_FLAG (desired_flags, attr_flag_str[i].key) !=
  696. CHECK_FLAG (real_flags, attr_flag_str[i].key)
  697. )
  698. {
  699. zlog (args->peer->log, LOG_ERR, "%s attribute must%s be flagged as \"%s\"",
  700. LOOKUP (attr_str, attr_code),
  701. CHECK_FLAG (desired_flags, attr_flag_str[i].key) ? "" : " not",
  702. attr_flag_str[i].str);
  703. seen = 1;
  704. }
  705. if (!seen)
  706. {
  707. zlog (args->peer->log, LOG_DEBUG,
  708. "Strange, %s called for attr %s, but no problem found with flags"
  709. " (real flags 0x%x, desired 0x%x)",
  710. __func__, LOOKUP (attr_str, attr_code),
  711. real_flags, desired_flags);
  712. }
  713. }
  714. /* Required flags for attributes. EXTLEN will be masked off when testing,
  715. * as will PARTIAL for optional+transitive attributes.
  716. */
  717. const u_int8_t attr_flags_values [] = {
  718. [BGP_ATTR_ORIGIN] = BGP_ATTR_FLAG_TRANS,
  719. [BGP_ATTR_AS_PATH] = BGP_ATTR_FLAG_TRANS,
  720. [BGP_ATTR_NEXT_HOP] = BGP_ATTR_FLAG_TRANS,
  721. [BGP_ATTR_MULTI_EXIT_DISC] = BGP_ATTR_FLAG_OPTIONAL,
  722. [BGP_ATTR_LOCAL_PREF] = BGP_ATTR_FLAG_TRANS,
  723. [BGP_ATTR_ATOMIC_AGGREGATE] = BGP_ATTR_FLAG_TRANS,
  724. [BGP_ATTR_AGGREGATOR] = BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
  725. [BGP_ATTR_COMMUNITIES] = BGP_ATTR_FLAG_TRANS | BGP_ATTR_FLAG_OPTIONAL,
  726. [BGP_ATTR_ORIGINATOR_ID] = BGP_ATTR_FLAG_OPTIONAL,
  727. [BGP_ATTR_CLUSTER_LIST] = BGP_ATTR_FLAG_OPTIONAL,
  728. [BGP_ATTR_MP_REACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL,
  729. [BGP_ATTR_MP_UNREACH_NLRI] = BGP_ATTR_FLAG_OPTIONAL,
  730. [BGP_ATTR_EXT_COMMUNITIES] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
  731. [BGP_ATTR_AS4_PATH] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
  732. [BGP_ATTR_AS4_AGGREGATOR] = BGP_ATTR_FLAG_OPTIONAL | BGP_ATTR_FLAG_TRANS,
  733. };
  734. static const size_t attr_flags_values_max =
  735. sizeof (attr_flags_values) / sizeof (attr_flags_values[0]);
  736. static int
  737. bgp_attr_flag_invalid (struct bgp_attr_parser_args *args)
  738. {
  739. u_int8_t mask = BGP_ATTR_FLAG_EXTLEN;
  740. const u_int8_t flags = args->flags;
  741. const u_int8_t attr_code = args->type;
  742. struct peer *const peer = args->peer;
  743. /* there may be attributes we don't know about */
  744. if (attr_code > attr_flags_values_max)
  745. return 0;
  746. if (attr_flags_values[attr_code] == 0)
  747. return 0;
  748. /* RFC4271, "For well-known attributes, the Transitive bit MUST be set to
  749. * 1."
  750. */
  751. if (!CHECK_FLAG (BGP_ATTR_FLAG_OPTIONAL, flags)
  752. && !CHECK_FLAG (BGP_ATTR_FLAG_TRANS, flags))
  753. {
  754. zlog (peer->log, LOG_ERR,
  755. "%s well-known attributes must have transitive flag set (%x)",
  756. LOOKUP (attr_str, attr_code), flags);
  757. return 1;
  758. }
  759. /* "For well-known attributes and for optional non-transitive attributes,
  760. * the Partial bit MUST be set to 0."
  761. */
  762. if (CHECK_FLAG (flags, BGP_ATTR_FLAG_PARTIAL))
  763. {
  764. if (!CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL))
  765. {
  766. zlog (peer->log, LOG_ERR,
  767. "%s well-known attribute "
  768. "must NOT have the partial flag set (%x)",
  769. LOOKUP (attr_str, attr_code), flags);
  770. return 1;
  771. }
  772. if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
  773. && !CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS))
  774. {
  775. zlog (peer->log, LOG_ERR,
  776. "%s optional + transitive attribute "
  777. "must NOT have the partial flag set (%x)",
  778. LOOKUP (attr_str, attr_code), flags);
  779. return 1;
  780. }
  781. }
  782. /* Optional transitive attributes may go through speakers that don't
  783. * reocgnise them and set the Partial bit.
  784. */
  785. if (CHECK_FLAG (flags, BGP_ATTR_FLAG_OPTIONAL)
  786. && CHECK_FLAG (flags, BGP_ATTR_FLAG_TRANS))
  787. SET_FLAG (mask, BGP_ATTR_FLAG_PARTIAL);
  788. if ((flags & ~mask)
  789. == attr_flags_values[attr_code])
  790. return 0;
  791. bgp_attr_flags_diagnose (args, attr_flags_values[attr_code]);
  792. return 1;
  793. }
  794. /* Get origin attribute of the update message. */
  795. static bgp_attr_parse_ret_t
  796. bgp_attr_origin (struct bgp_attr_parser_args *args)
  797. {
  798. struct peer *const peer = args->peer;
  799. struct attr *const attr = args->attr;
  800. const bgp_size_t length = args->length;
  801. /* If any recognized attribute has Attribute Length that conflicts
  802. with the expected length (based on the attribute type code), then
  803. the Error Subcode is set to Attribute Length Error. The Data
  804. field contains the erroneous attribute (type, length and
  805. value). */
  806. if (length != 1)
  807. {
  808. zlog (peer->log, LOG_ERR, "Origin attribute length is not one %d",
  809. length);
  810. return bgp_attr_malformed (args,
  811. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  812. args->total);
  813. }
  814. /* Fetch origin attribute. */
  815. attr->origin = stream_getc (BGP_INPUT (peer));
  816. /* If the ORIGIN attribute has an undefined value, then the Error
  817. Subcode is set to Invalid Origin Attribute. The Data field
  818. contains the unrecognized attribute (type, length and value). */
  819. if ((attr->origin != BGP_ORIGIN_IGP)
  820. && (attr->origin != BGP_ORIGIN_EGP)
  821. && (attr->origin != BGP_ORIGIN_INCOMPLETE))
  822. {
  823. zlog (peer->log, LOG_ERR, "Origin attribute value is invalid %d",
  824. attr->origin);
  825. return bgp_attr_malformed (args,
  826. BGP_NOTIFY_UPDATE_INVAL_ORIGIN,
  827. args->total);
  828. }
  829. /* Set oring attribute flag. */
  830. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
  831. return 0;
  832. }
  833. /* Parse AS path information. This function is wrapper of
  834. aspath_parse. */
  835. static int
  836. bgp_attr_aspath (struct bgp_attr_parser_args *args)
  837. {
  838. struct attr *const attr = args->attr;
  839. struct peer *const peer = args->peer;
  840. const bgp_size_t length = args->length;
  841. /*
  842. * peer with AS4 => will get 4Byte ASnums
  843. * otherwise, will get 16 Bit
  844. */
  845. attr->aspath = aspath_parse (peer->ibuf, length,
  846. CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV));
  847. /* In case of IBGP, length will be zero. */
  848. if (! attr->aspath)
  849. {
  850. zlog (peer->log, LOG_ERR,
  851. "Malformed AS path from %s, length is %d",
  852. peer->host, length);
  853. return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_MAL_AS_PATH, 0);
  854. }
  855. /* Set aspath attribute flag. */
  856. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
  857. return BGP_ATTR_PARSE_PROCEED;
  858. }
  859. static bgp_attr_parse_ret_t
  860. bgp_attr_aspath_check (struct peer *const peer, struct attr *const attr)
  861. {
  862. /* These checks were part of bgp_attr_aspath, but with
  863. * as4 we should to check aspath things when
  864. * aspath synthesizing with as4_path has already taken place.
  865. * Otherwise we check ASPATH and use the synthesized thing, and that is
  866. * not right.
  867. * So do the checks later, i.e. here
  868. */
  869. struct bgp *bgp = peer->bgp;
  870. struct aspath *aspath;
  871. /* Confederation sanity check. */
  872. if ((peer->sort == BGP_PEER_CONFED && ! aspath_left_confed_check (attr->aspath)) ||
  873. (peer->sort == BGP_PEER_EBGP && aspath_confed_check (attr->aspath)))
  874. {
  875. zlog (peer->log, LOG_ERR, "Malformed AS path from %s", peer->host);
  876. bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
  877. BGP_NOTIFY_UPDATE_MAL_AS_PATH);
  878. return BGP_ATTR_PARSE_ERROR;
  879. }
  880. /* First AS check for EBGP. */
  881. if (bgp != NULL && bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS))
  882. {
  883. if (peer->sort == BGP_PEER_EBGP
  884. && ! aspath_firstas_check (attr->aspath, peer->as))
  885. {
  886. zlog (peer->log, LOG_ERR,
  887. "%s incorrect first AS (must be %u)", peer->host, peer->as);
  888. bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
  889. BGP_NOTIFY_UPDATE_MAL_AS_PATH);
  890. return BGP_ATTR_PARSE_ERROR;
  891. }
  892. }
  893. /* local-as prepend */
  894. if (peer->change_local_as &&
  895. ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND))
  896. {
  897. aspath = aspath_dup (attr->aspath);
  898. aspath = aspath_add_seq (aspath, peer->change_local_as);
  899. aspath_unintern (&attr->aspath);
  900. attr->aspath = aspath_intern (aspath);
  901. }
  902. return BGP_ATTR_PARSE_PROCEED;
  903. }
  904. /* Parse AS4 path information. This function is another wrapper of
  905. aspath_parse. */
  906. static int
  907. bgp_attr_as4_path (struct bgp_attr_parser_args *args, struct aspath **as4_path)
  908. {
  909. struct peer *const peer = args->peer;
  910. struct attr *const attr = args->attr;
  911. const bgp_size_t length = args->length;
  912. *as4_path = aspath_parse (peer->ibuf, length, 1);
  913. /* In case of IBGP, length will be zero. */
  914. if (!*as4_path)
  915. {
  916. zlog (peer->log, LOG_ERR,
  917. "Malformed AS4 path from %s, length is %d",
  918. peer->host, length);
  919. return bgp_attr_malformed (args,
  920. BGP_NOTIFY_UPDATE_MAL_AS_PATH,
  921. 0);
  922. }
  923. /* Set aspath attribute flag. */
  924. if (as4_path)
  925. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_PATH);
  926. return BGP_ATTR_PARSE_PROCEED;
  927. }
  928. /* Nexthop attribute. */
  929. static bgp_attr_parse_ret_t
  930. bgp_attr_nexthop (struct bgp_attr_parser_args *args)
  931. {
  932. struct peer *const peer = args->peer;
  933. struct attr *const attr = args->attr;
  934. const bgp_size_t length = args->length;
  935. in_addr_t nexthop_h, nexthop_n;
  936. /* Check nexthop attribute length. */
  937. if (length != 4)
  938. {
  939. zlog (peer->log, LOG_ERR, "Nexthop attribute length isn't four [%d]",
  940. length);
  941. return bgp_attr_malformed (args,
  942. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  943. args->total);
  944. }
  945. /* According to section 6.3 of RFC4271, syntactically incorrect NEXT_HOP
  946. attribute must result in a NOTIFICATION message (this is implemented below).
  947. At the same time, semantically incorrect NEXT_HOP is more likely to be just
  948. logged locally (this is implemented somewhere else). The UPDATE message
  949. gets ignored in any of these cases. */
  950. nexthop_n = stream_get_ipv4 (peer->ibuf);
  951. nexthop_h = ntohl (nexthop_n);
  952. if (IPV4_NET0 (nexthop_h) || IPV4_NET127 (nexthop_h) || IPV4_CLASS_DE (nexthop_h))
  953. {
  954. char buf[INET_ADDRSTRLEN];
  955. inet_ntop (AF_INET, &nexthop_h, buf, INET_ADDRSTRLEN);
  956. zlog (peer->log, LOG_ERR, "Martian nexthop %s", buf);
  957. return bgp_attr_malformed (args,
  958. BGP_NOTIFY_UPDATE_INVAL_NEXT_HOP,
  959. args->total);
  960. }
  961. attr->nexthop.s_addr = nexthop_n;
  962. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
  963. return BGP_ATTR_PARSE_PROCEED;
  964. }
  965. /* MED atrribute. */
  966. static bgp_attr_parse_ret_t
  967. bgp_attr_med (struct bgp_attr_parser_args *args)
  968. {
  969. struct peer *const peer = args->peer;
  970. struct attr *const attr = args->attr;
  971. const bgp_size_t length = args->length;
  972. /* Length check. */
  973. if (length != 4)
  974. {
  975. zlog (peer->log, LOG_ERR,
  976. "MED attribute length isn't four [%d]", length);
  977. return bgp_attr_malformed (args,
  978. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  979. args->total);
  980. }
  981. attr->med = stream_getl (peer->ibuf);
  982. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
  983. return BGP_ATTR_PARSE_PROCEED;
  984. }
  985. /* Local preference attribute. */
  986. static bgp_attr_parse_ret_t
  987. bgp_attr_local_pref (struct bgp_attr_parser_args *args)
  988. {
  989. struct peer *const peer = args->peer;
  990. struct attr *const attr = args->attr;
  991. const bgp_size_t length = args->length;
  992. /* Length check. */
  993. if (length != 4)
  994. {
  995. zlog (peer->log, LOG_ERR, "LOCAL_PREF attribute length isn't 4 [%u]",
  996. length);
  997. return bgp_attr_malformed (args,
  998. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  999. args->total);
  1000. }
  1001. /* If it is contained in an UPDATE message that is received from an
  1002. external peer, then this attribute MUST be ignored by the
  1003. receiving speaker. */
  1004. if (peer->sort == BGP_PEER_EBGP)
  1005. {
  1006. stream_forward_getp (peer->ibuf, length);
  1007. return BGP_ATTR_PARSE_PROCEED;
  1008. }
  1009. attr->local_pref = stream_getl (peer->ibuf);
  1010. /* Set atomic aggregate flag. */
  1011. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF);
  1012. return BGP_ATTR_PARSE_PROCEED;
  1013. }
  1014. /* Atomic aggregate. */
  1015. static int
  1016. bgp_attr_atomic (struct bgp_attr_parser_args *args)
  1017. {
  1018. struct peer *const peer = args->peer;
  1019. struct attr *const attr = args->attr;
  1020. const bgp_size_t length = args->length;
  1021. /* Length check. */
  1022. if (length != 0)
  1023. {
  1024. zlog (peer->log, LOG_ERR, "ATOMIC_AGGREGATE attribute length isn't 0 [%u]",
  1025. length);
  1026. return bgp_attr_malformed (args,
  1027. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  1028. args->total);
  1029. }
  1030. /* Set atomic aggregate flag. */
  1031. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
  1032. return BGP_ATTR_PARSE_PROCEED;
  1033. }
  1034. /* Aggregator attribute */
  1035. static int
  1036. bgp_attr_aggregator (struct bgp_attr_parser_args *args)
  1037. {
  1038. struct peer *const peer = args->peer;
  1039. struct attr *const attr = args->attr;
  1040. const bgp_size_t length = args->length;
  1041. int wantedlen = 6;
  1042. struct attr_extra *attre = bgp_attr_extra_get (attr);
  1043. /* peer with AS4 will send 4 Byte AS, peer without will send 2 Byte */
  1044. if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
  1045. wantedlen = 8;
  1046. if (length != wantedlen)
  1047. {
  1048. zlog (peer->log, LOG_ERR, "AGGREGATOR attribute length isn't %u [%u]",
  1049. wantedlen, length);
  1050. return bgp_attr_malformed (args,
  1051. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  1052. args->total);
  1053. }
  1054. if ( CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV ) )
  1055. attre->aggregator_as = stream_getl (peer->ibuf);
  1056. else
  1057. attre->aggregator_as = stream_getw (peer->ibuf);
  1058. attre->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf);
  1059. /* Set atomic aggregate flag. */
  1060. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
  1061. return BGP_ATTR_PARSE_PROCEED;
  1062. }
  1063. /* New Aggregator attribute */
  1064. static bgp_attr_parse_ret_t
  1065. bgp_attr_as4_aggregator (struct bgp_attr_parser_args *args,
  1066. as_t *as4_aggregator_as,
  1067. struct in_addr *as4_aggregator_addr)
  1068. {
  1069. struct peer *const peer = args->peer;
  1070. struct attr *const attr = args->attr;
  1071. const bgp_size_t length = args->length;
  1072. if (length != 8)
  1073. {
  1074. zlog (peer->log, LOG_ERR, "New Aggregator length is not 8 [%d]",
  1075. length);
  1076. return bgp_attr_malformed (args,
  1077. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  1078. 0);
  1079. }
  1080. *as4_aggregator_as = stream_getl (peer->ibuf);
  1081. as4_aggregator_addr->s_addr = stream_get_ipv4 (peer->ibuf);
  1082. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR);
  1083. return BGP_ATTR_PARSE_PROCEED;
  1084. }
  1085. /* Munge Aggregator and New-Aggregator, AS_PATH and NEW_AS_PATH.
  1086. */
  1087. static bgp_attr_parse_ret_t
  1088. bgp_attr_munge_as4_attrs (struct peer *const peer,
  1089. struct attr *const attr,
  1090. struct aspath *as4_path, as_t as4_aggregator,
  1091. struct in_addr *as4_aggregator_addr)
  1092. {
  1093. int ignore_as4_path = 0;
  1094. struct aspath *newpath;
  1095. struct attr_extra *attre = attr->extra;
  1096. if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV))
  1097. {
  1098. /* peer can do AS4, so we ignore AS4_PATH and AS4_AGGREGATOR
  1099. * if given.
  1100. * It is worth a warning though, because the peer really
  1101. * should not send them
  1102. */
  1103. if (BGP_DEBUG(as4, AS4))
  1104. {
  1105. if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_PATH)))
  1106. zlog_debug ("[AS4] %s %s AS4_PATH",
  1107. peer->host, "AS4 capable peer, yet it sent");
  1108. if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS4_AGGREGATOR)))
  1109. zlog_debug ("[AS4] %s %s AS4_AGGREGATOR",
  1110. peer->host, "AS4 capable peer, yet it sent");
  1111. }
  1112. return BGP_ATTR_PARSE_PROCEED;
  1113. }
  1114. /* We have a asn16 peer. First, look for AS4_AGGREGATOR
  1115. * because that may override AS4_PATH
  1116. */
  1117. if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AS4_AGGREGATOR) ) )
  1118. {
  1119. if (attr->flag & (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR) ) )
  1120. {
  1121. assert (attre);
  1122. /* received both.
  1123. * if the as_number in aggregator is not AS_TRANS,
  1124. * then AS4_AGGREGATOR and AS4_PATH shall be ignored
  1125. * and the Aggregator shall be taken as
  1126. * info on the aggregating node, and the AS_PATH
  1127. * shall be taken as the AS_PATH
  1128. * otherwise
  1129. * the Aggregator shall be ignored and the
  1130. * AS4_AGGREGATOR shall be taken as the
  1131. * Aggregating node and the AS_PATH is to be
  1132. * constructed "as in all other cases"
  1133. */
  1134. if (attre->aggregator_as != BGP_AS_TRANS)
  1135. {
  1136. /* ignore */
  1137. if ( BGP_DEBUG(as4, AS4))
  1138. zlog_debug ("[AS4] %s BGP not AS4 capable peer"
  1139. " send AGGREGATOR != AS_TRANS and"
  1140. " AS4_AGGREGATOR, so ignore"
  1141. " AS4_AGGREGATOR and AS4_PATH", peer->host);
  1142. ignore_as4_path = 1;
  1143. }
  1144. else
  1145. {
  1146. /* "New_aggregator shall be taken as aggregator" */
  1147. attre->aggregator_as = as4_aggregator;
  1148. attre->aggregator_addr.s_addr = as4_aggregator_addr->s_addr;
  1149. }
  1150. }
  1151. else
  1152. {
  1153. /* We received a AS4_AGGREGATOR but no AGGREGATOR.
  1154. * That is bogus - but reading the conditions
  1155. * we have to handle AS4_AGGREGATOR as if it were
  1156. * AGGREGATOR in that case
  1157. */
  1158. if ( BGP_DEBUG(as4, AS4))
  1159. zlog_debug ("[AS4] %s BGP not AS4 capable peer send"
  1160. " AS4_AGGREGATOR but no AGGREGATOR, will take"
  1161. " it as if AGGREGATOR with AS_TRANS had been there", peer->host);
  1162. (attre = bgp_attr_extra_get (attr))->aggregator_as = as4_aggregator;
  1163. /* sweep it under the carpet and simulate a "good" AGGREGATOR */
  1164. attr->flag |= (ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR));
  1165. }
  1166. }
  1167. /* need to reconcile NEW_AS_PATH and AS_PATH */
  1168. if (!ignore_as4_path && (attr->flag & (ATTR_FLAG_BIT( BGP_ATTR_AS4_PATH))))
  1169. {
  1170. if (!attr->aspath)
  1171. return BGP_ATTR_PARSE_PROCEED;
  1172. newpath = aspath_reconcile_as4 (attr->aspath, as4_path);
  1173. aspath_unintern (&attr->aspath);
  1174. attr->aspath = aspath_intern (newpath);
  1175. }
  1176. return BGP_ATTR_PARSE_PROCEED;
  1177. }
  1178. /* Community attribute. */
  1179. static bgp_attr_parse_ret_t
  1180. bgp_attr_community (struct bgp_attr_parser_args *args)
  1181. {
  1182. struct peer *const peer = args->peer;
  1183. struct attr *const attr = args->attr;
  1184. const bgp_size_t length = args->length;
  1185. if (length == 0)
  1186. {
  1187. attr->community = NULL;
  1188. return BGP_ATTR_PARSE_PROCEED;
  1189. }
  1190. attr->community =
  1191. community_parse ((u_int32_t *)stream_pnt (peer->ibuf), length);
  1192. /* XXX: fix community_parse to use stream API and remove this */
  1193. stream_forward_getp (peer->ibuf, length);
  1194. if (!attr->community)
  1195. return bgp_attr_malformed (args,
  1196. BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
  1197. args->total);
  1198. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
  1199. return BGP_ATTR_PARSE_PROCEED;
  1200. }
  1201. /* Originator ID attribute. */
  1202. static bgp_attr_parse_ret_t
  1203. bgp_attr_originator_id (struct bgp_attr_parser_args *args)
  1204. {
  1205. struct peer *const peer = args->peer;
  1206. struct attr *const attr = args->attr;
  1207. const bgp_size_t length = args->length;
  1208. /* Length check. */
  1209. if (length != 4)
  1210. {
  1211. zlog (peer->log, LOG_ERR, "Bad originator ID length %d", length);
  1212. return bgp_attr_malformed (args,
  1213. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  1214. args->total);
  1215. }
  1216. (bgp_attr_extra_get (attr))->originator_id.s_addr
  1217. = stream_get_ipv4 (peer->ibuf);
  1218. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID);
  1219. return BGP_ATTR_PARSE_PROCEED;
  1220. }
  1221. /* Cluster list attribute. */
  1222. static bgp_attr_parse_ret_t
  1223. bgp_attr_cluster_list (struct bgp_attr_parser_args *args)
  1224. {
  1225. struct peer *const peer = args->peer;
  1226. struct attr *const attr = args->attr;
  1227. const bgp_size_t length = args->length;
  1228. /* Check length. */
  1229. if (length % 4)
  1230. {
  1231. zlog (peer->log, LOG_ERR, "Bad cluster list length %d", length);
  1232. return bgp_attr_malformed (args, BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  1233. args->total);
  1234. }
  1235. (bgp_attr_extra_get (attr))->cluster
  1236. = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf), length);
  1237. /* XXX: Fix cluster_parse to use stream API and then remove this */
  1238. stream_forward_getp (peer->ibuf, length);
  1239. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST);
  1240. return BGP_ATTR_PARSE_PROCEED;
  1241. }
  1242. /* Multiprotocol reachability information parse. */
  1243. int
  1244. bgp_mp_reach_parse (struct bgp_attr_parser_args *args,
  1245. struct bgp_nlri *mp_update)
  1246. {
  1247. afi_t afi;
  1248. safi_t safi;
  1249. bgp_size_t nlri_len;
  1250. size_t start;
  1251. int ret;
  1252. struct stream *s;
  1253. struct peer *const peer = args->peer;
  1254. struct attr *const attr = args->attr;
  1255. const bgp_size_t length = args->length;
  1256. struct attr_extra *attre = bgp_attr_extra_get(attr);
  1257. /* Set end of packet. */
  1258. s = BGP_INPUT(peer);
  1259. start = stream_get_getp(s);
  1260. /* safe to read statically sized header? */
  1261. #define BGP_MP_REACH_MIN_SIZE 5
  1262. #define LEN_LEFT (length - (stream_get_getp(s) - start))
  1263. if ((length > STREAM_READABLE(s)) || (length < BGP_MP_REACH_MIN_SIZE))
  1264. {
  1265. zlog_info ("%s: %s sent invalid length, %lu",
  1266. __func__, peer->host, (unsigned long)length);
  1267. return BGP_ATTR_PARSE_ERROR;
  1268. }
  1269. /* Load AFI, SAFI. */
  1270. afi = stream_getw (s);
  1271. safi = stream_getc (s);
  1272. /* Get nexthop length. */
  1273. attre->mp_nexthop_len = stream_getc (s);
  1274. if (LEN_LEFT < attre->mp_nexthop_len)
  1275. {
  1276. zlog_info ("%s: %s, MP nexthop length, %u, goes past end of attribute",
  1277. __func__, peer->host, attre->mp_nexthop_len);
  1278. return BGP_ATTR_PARSE_ERROR;
  1279. }
  1280. /* Nexthop length check. */
  1281. switch (attre->mp_nexthop_len)
  1282. {
  1283. case 4:
  1284. stream_get (&attre->mp_nexthop_global_in, s, 4);
  1285. /* Probably needed for RFC 2283 */
  1286. if (attr->nexthop.s_addr == 0)
  1287. memcpy(&attr->nexthop.s_addr, &attre->mp_nexthop_global_in, 4);
  1288. break;
  1289. case 12:
  1290. stream_getl (s); /* RD high */
  1291. stream_getl (s); /* RD low */
  1292. stream_get (&attre->mp_nexthop_global_in, s, 4);
  1293. break;
  1294. #ifdef HAVE_IPV6
  1295. case 16:
  1296. stream_get (&attre->mp_nexthop_global, s, 16);
  1297. break;
  1298. case 32:
  1299. stream_get (&attre->mp_nexthop_global, s, 16);
  1300. stream_get (&attre->mp_nexthop_local, s, 16);
  1301. if (! IN6_IS_ADDR_LINKLOCAL (&attre->mp_nexthop_local))
  1302. {
  1303. char buf1[INET6_ADDRSTRLEN];
  1304. char buf2[INET6_ADDRSTRLEN];
  1305. if (BGP_DEBUG (update, UPDATE_IN))
  1306. zlog_debug ("%s got two nexthop %s %s but second one is not a link-local nexthop", peer->host,
  1307. inet_ntop (AF_INET6, &attre->mp_nexthop_global,
  1308. buf1, INET6_ADDRSTRLEN),
  1309. inet_ntop (AF_INET6, &attre->mp_nexthop_local,
  1310. buf2, INET6_ADDRSTRLEN));
  1311. attre->mp_nexthop_len = 16;
  1312. }
  1313. break;
  1314. #endif /* HAVE_IPV6 */
  1315. default:
  1316. zlog_info ("%s: (%s) Wrong multiprotocol next hop length: %d",
  1317. __func__, peer->host, attre->mp_nexthop_len);
  1318. return BGP_ATTR_PARSE_ERROR;
  1319. }
  1320. if (!LEN_LEFT)
  1321. {
  1322. zlog_info ("%s: (%s) Failed to read SNPA and NLRI(s)",
  1323. __func__, peer->host);
  1324. return BGP_ATTR_PARSE_ERROR;
  1325. }
  1326. {
  1327. u_char val;
  1328. if ((val = stream_getc (s)))
  1329. zlog_warn ("%s sent non-zero value, %u, for defunct SNPA-length field",
  1330. peer->host, val);
  1331. }
  1332. /* must have nrli_len, what is left of the attribute */
  1333. nlri_len = LEN_LEFT;
  1334. if ((!nlri_len) || (nlri_len > STREAM_READABLE(s)))
  1335. {
  1336. zlog_info ("%s: (%s) Failed to read NLRI",
  1337. __func__, peer->host);
  1338. return BGP_ATTR_PARSE_ERROR;
  1339. }
  1340. if (safi != SAFI_MPLS_LABELED_VPN)
  1341. {
  1342. ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), nlri_len);
  1343. if (ret < 0)
  1344. {
  1345. zlog_info ("%s: (%s) NLRI doesn't pass sanity check",
  1346. __func__, peer->host);
  1347. return BGP_ATTR_PARSE_ERROR;
  1348. }
  1349. }
  1350. mp_update->afi = afi;
  1351. mp_update->safi = safi;
  1352. mp_update->nlri = stream_pnt (s);
  1353. mp_update->length = nlri_len;
  1354. stream_forward_getp (s, nlri_len);
  1355. return BGP_ATTR_PARSE_PROCEED;
  1356. #undef LEN_LEFT
  1357. }
  1358. /* Multiprotocol unreachable parse */
  1359. int
  1360. bgp_mp_unreach_parse (struct bgp_attr_parser_args *args,
  1361. struct bgp_nlri *mp_withdraw)
  1362. {
  1363. struct stream *s;
  1364. afi_t afi;
  1365. safi_t safi;
  1366. u_int16_t withdraw_len;
  1367. int ret;
  1368. struct peer *const peer = args->peer;
  1369. const bgp_size_t length = args->length;
  1370. s = peer->ibuf;
  1371. #define BGP_MP_UNREACH_MIN_SIZE 3
  1372. if ((length > STREAM_READABLE(s)) || (length < BGP_MP_UNREACH_MIN_SIZE))
  1373. return BGP_ATTR_PARSE_ERROR;
  1374. afi = stream_getw (s);
  1375. safi = stream_getc (s);
  1376. withdraw_len = length - BGP_MP_UNREACH_MIN_SIZE;
  1377. if (safi != SAFI_MPLS_LABELED_VPN)
  1378. {
  1379. ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), withdraw_len);
  1380. if (ret < 0)
  1381. return BGP_ATTR_PARSE_ERROR;
  1382. }
  1383. mp_withdraw->afi = afi;
  1384. mp_withdraw->safi = safi;
  1385. mp_withdraw->nlri = stream_pnt (s);
  1386. mp_withdraw->length = withdraw_len;
  1387. stream_forward_getp (s, withdraw_len);
  1388. return BGP_ATTR_PARSE_PROCEED;
  1389. }
  1390. /* Extended Community attribute. */
  1391. static bgp_attr_parse_ret_t
  1392. bgp_attr_ext_communities (struct bgp_attr_parser_args *args)
  1393. {
  1394. struct peer *const peer = args->peer;
  1395. struct attr *const attr = args->attr;
  1396. const bgp_size_t length = args->length;
  1397. if (length == 0)
  1398. {
  1399. if (attr->extra)
  1400. attr->extra->ecommunity = NULL;
  1401. /* Empty extcomm doesn't seem to be invalid per se */
  1402. return BGP_ATTR_PARSE_PROCEED;
  1403. }
  1404. (bgp_attr_extra_get (attr))->ecommunity =
  1405. ecommunity_parse ((u_int8_t *)stream_pnt (peer->ibuf), length);
  1406. /* XXX: fix ecommunity_parse to use stream API */
  1407. stream_forward_getp (peer->ibuf, length);
  1408. if (!attr->extra->ecommunity)
  1409. return bgp_attr_malformed (args,
  1410. BGP_NOTIFY_UPDATE_OPT_ATTR_ERR,
  1411. args->total);
  1412. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
  1413. return BGP_ATTR_PARSE_PROCEED;
  1414. }
  1415. /* BGP unknown attribute treatment. */
  1416. static bgp_attr_parse_ret_t
  1417. bgp_attr_unknown (struct bgp_attr_parser_args *args)
  1418. {
  1419. bgp_size_t total = args->total;
  1420. struct transit *transit;
  1421. struct attr_extra *attre;
  1422. struct peer *const peer = args->peer;
  1423. struct attr *const attr = args->attr;
  1424. u_char *const startp = args->startp;
  1425. const u_char type = args->type;
  1426. const u_char flag = args->flags;
  1427. const bgp_size_t length = args->length;
  1428. if (BGP_DEBUG (normal, NORMAL))
  1429. zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
  1430. peer->host, type, length);
  1431. if (BGP_DEBUG (events, EVENTS))
  1432. zlog (peer->log, LOG_DEBUG,
  1433. "Unknown attribute type %d length %d is received", type, length);
  1434. /* Forward read pointer of input stream. */
  1435. stream_forward_getp (peer->ibuf, length);
  1436. /* If any of the mandatory well-known attributes are not recognized,
  1437. then the Error Subcode is set to Unrecognized Well-known
  1438. Attribute. The Data field contains the unrecognized attribute
  1439. (type, length and value). */
  1440. if (!CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL))
  1441. {
  1442. return bgp_attr_malformed (args,
  1443. BGP_NOTIFY_UPDATE_UNREC_ATTR,
  1444. args->total);
  1445. }
  1446. /* Unrecognized non-transitive optional attributes must be quietly
  1447. ignored and not passed along to other BGP peers. */
  1448. if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
  1449. return BGP_ATTR_PARSE_PROCEED;
  1450. /* If a path with recognized transitive optional attribute is
  1451. accepted and passed along to other BGP peers and the Partial bit
  1452. in the Attribute Flags octet is set to 1 by some previous AS, it
  1453. is not set back to 0 by the current AS. */
  1454. SET_FLAG (*startp, BGP_ATTR_FLAG_PARTIAL);
  1455. /* Store transitive attribute to the end of attr->transit. */
  1456. if (! ((attre = bgp_attr_extra_get(attr))->transit) )
  1457. attre->transit = XCALLOC (MTYPE_TRANSIT, sizeof (struct transit));
  1458. transit = attre->transit;
  1459. if (transit->val)
  1460. transit->val = XREALLOC (MTYPE_TRANSIT_VAL, transit->val,
  1461. transit->length + total);
  1462. else
  1463. transit->val = XMALLOC (MTYPE_TRANSIT_VAL, total);
  1464. memcpy (transit->val + transit->length, startp, total);
  1465. transit->length += total;
  1466. return BGP_ATTR_PARSE_PROCEED;
  1467. }
  1468. /* Read attribute of update packet. This function is called from
  1469. bgp_update_receive() in bgp_packet.c. */
  1470. bgp_attr_parse_ret_t
  1471. bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
  1472. struct bgp_nlri *mp_update, struct bgp_nlri *mp_withdraw)
  1473. {
  1474. int ret;
  1475. u_char flag = 0;
  1476. u_char type = 0;
  1477. bgp_size_t length;
  1478. u_char *startp, *endp;
  1479. u_char *attr_endp;
  1480. u_char seen[BGP_ATTR_BITMAP_SIZE];
  1481. /* we need the as4_path only until we have synthesized the as_path with it */
  1482. /* same goes for as4_aggregator */
  1483. struct aspath *as4_path = NULL;
  1484. as_t as4_aggregator = 0;
  1485. struct in_addr as4_aggregator_addr = { 0 };
  1486. /* Initialize bitmap. */
  1487. memset (seen, 0, BGP_ATTR_BITMAP_SIZE);
  1488. /* End pointer of BGP attribute. */
  1489. endp = BGP_INPUT_PNT (peer) + size;
  1490. /* Get attributes to the end of attribute length. */
  1491. while (BGP_INPUT_PNT (peer) < endp)
  1492. {
  1493. /* Check remaining length check.*/
  1494. if (endp - BGP_INPUT_PNT (peer) < BGP_ATTR_MIN_LEN)
  1495. {
  1496. /* XXX warning: long int format, int arg (arg 5) */
  1497. zlog (peer->log, LOG_WARNING,
  1498. "%s: error BGP attribute length %lu is smaller than min len",
  1499. peer->host,
  1500. (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
  1501. bgp_notify_send (peer,
  1502. BGP_NOTIFY_UPDATE_ERR,
  1503. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1504. return BGP_ATTR_PARSE_ERROR;
  1505. }
  1506. /* Fetch attribute flag and type. */
  1507. startp = BGP_INPUT_PNT (peer);
  1508. /* "The lower-order four bits of the Attribute Flags octet are
  1509. unused. They MUST be zero when sent and MUST be ignored when
  1510. received." */
  1511. flag = 0xF0 & stream_getc (BGP_INPUT (peer));
  1512. type = stream_getc (BGP_INPUT (peer));
  1513. /* Check whether Extended-Length applies and is in bounds */
  1514. if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN)
  1515. && ((endp - startp) < (BGP_ATTR_MIN_LEN + 1)))
  1516. {
  1517. zlog (peer->log, LOG_WARNING,
  1518. "%s: Extended length set, but just %lu bytes of attr header",
  1519. peer->host,
  1520. (unsigned long) (endp - STREAM_PNT (BGP_INPUT (peer))));
  1521. bgp_notify_send (peer,
  1522. BGP_NOTIFY_UPDATE_ERR,
  1523. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1524. return BGP_ATTR_PARSE_ERROR;
  1525. }
  1526. /* Check extended attribue length bit. */
  1527. if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN))
  1528. length = stream_getw (BGP_INPUT (peer));
  1529. else
  1530. length = stream_getc (BGP_INPUT (peer));
  1531. /* If any attribute appears more than once in the UPDATE
  1532. message, then the Error Subcode is set to Malformed Attribute
  1533. List. */
  1534. if (CHECK_BITMAP (seen, type))
  1535. {
  1536. zlog (peer->log, LOG_WARNING,
  1537. "%s: error BGP attribute type %d appears twice in a message",
  1538. peer->host, type);
  1539. bgp_notify_send (peer,
  1540. BGP_NOTIFY_UPDATE_ERR,
  1541. BGP_NOTIFY_UPDATE_MAL_ATTR);
  1542. return BGP_ATTR_PARSE_ERROR;
  1543. }
  1544. /* Set type to bitmap to check duplicate attribute. `type' is
  1545. unsigned char so it never overflow bitmap range. */
  1546. SET_BITMAP (seen, type);
  1547. /* Overflow check. */
  1548. attr_endp = BGP_INPUT_PNT (peer) + length;
  1549. if (attr_endp > endp)
  1550. {
  1551. zlog (peer->log, LOG_WARNING,
  1552. "%s: BGP type %d length %d is too large, attribute total length is %d. attr_endp is %p. endp is %p", peer->host, type, length, size, attr_endp, endp);
  1553. bgp_notify_send (peer,
  1554. BGP_NOTIFY_UPDATE_ERR,
  1555. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1556. return BGP_ATTR_PARSE_ERROR;
  1557. }
  1558. struct bgp_attr_parser_args attr_args = {
  1559. .peer = peer,
  1560. .length = length,
  1561. .attr = attr,
  1562. .type = type,
  1563. .flags = flag,
  1564. .startp = startp,
  1565. .total = attr_endp - startp,
  1566. };
  1567. /* If any recognized attribute has Attribute Flags that conflict
  1568. with the Attribute Type Code, then the Error Subcode is set to
  1569. Attribute Flags Error. The Data field contains the erroneous
  1570. attribute (type, length and value). */
  1571. if (bgp_attr_flag_invalid (&attr_args))
  1572. {
  1573. bgp_attr_parse_ret_t ret;
  1574. ret = bgp_attr_malformed (&attr_args,
  1575. BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
  1576. attr_args.total);
  1577. if (ret == BGP_ATTR_PARSE_PROCEED)
  1578. continue;
  1579. return ret;
  1580. }
  1581. /* OK check attribute and store it's value. */
  1582. switch (type)
  1583. {
  1584. case BGP_ATTR_ORIGIN:
  1585. ret = bgp_attr_origin (&attr_args);
  1586. break;
  1587. case BGP_ATTR_AS_PATH:
  1588. ret = bgp_attr_aspath (&attr_args);
  1589. break;
  1590. case BGP_ATTR_AS4_PATH:
  1591. ret = bgp_attr_as4_path (&attr_args, &as4_path);
  1592. break;
  1593. case BGP_ATTR_NEXT_HOP:
  1594. ret = bgp_attr_nexthop (&attr_args);
  1595. break;
  1596. case BGP_ATTR_MULTI_EXIT_DISC:
  1597. ret = bgp_attr_med (&attr_args);
  1598. break;
  1599. case BGP_ATTR_LOCAL_PREF:
  1600. ret = bgp_attr_local_pref (&attr_args);
  1601. break;
  1602. case BGP_ATTR_ATOMIC_AGGREGATE:
  1603. ret = bgp_attr_atomic (&attr_args);
  1604. break;
  1605. case BGP_ATTR_AGGREGATOR:
  1606. ret = bgp_attr_aggregator (&attr_args);
  1607. break;
  1608. case BGP_ATTR_AS4_AGGREGATOR:
  1609. ret = bgp_attr_as4_aggregator (&attr_args,
  1610. &as4_aggregator,
  1611. &as4_aggregator_addr);
  1612. break;
  1613. case BGP_ATTR_COMMUNITIES:
  1614. ret = bgp_attr_community (&attr_args);
  1615. break;
  1616. case BGP_ATTR_ORIGINATOR_ID:
  1617. ret = bgp_attr_originator_id (&attr_args);
  1618. break;
  1619. case BGP_ATTR_CLUSTER_LIST:
  1620. ret = bgp_attr_cluster_list (&attr_args);
  1621. break;
  1622. case BGP_ATTR_MP_REACH_NLRI:
  1623. ret = bgp_mp_reach_parse (&attr_args, mp_update);
  1624. break;
  1625. case BGP_ATTR_MP_UNREACH_NLRI:
  1626. ret = bgp_mp_unreach_parse (&attr_args, mp_withdraw);
  1627. break;
  1628. case BGP_ATTR_EXT_COMMUNITIES:
  1629. ret = bgp_attr_ext_communities (&attr_args);
  1630. break;
  1631. default:
  1632. ret = bgp_attr_unknown (&attr_args);
  1633. break;
  1634. }
  1635. /* If hard error occured immediately return to the caller. */
  1636. if (ret == BGP_ATTR_PARSE_ERROR)
  1637. {
  1638. zlog (peer->log, LOG_WARNING,
  1639. "%s: Attribute %s, parse error",
  1640. peer->host,
  1641. LOOKUP (attr_str, type));
  1642. bgp_notify_send (peer,
  1643. BGP_NOTIFY_UPDATE_ERR,
  1644. BGP_NOTIFY_UPDATE_MAL_ATTR);
  1645. if (as4_path)
  1646. aspath_unintern (&as4_path);
  1647. return ret;
  1648. }
  1649. if (ret == BGP_ATTR_PARSE_WITHDRAW)
  1650. {
  1651. zlog (peer->log, LOG_WARNING,
  1652. "%s: Attribute %s, parse error - treating as withdrawal",
  1653. peer->host,
  1654. LOOKUP (attr_str, type));
  1655. if (as4_path)
  1656. aspath_unintern (&as4_path);
  1657. return ret;
  1658. }
  1659. /* Check the fetched length. */
  1660. if (BGP_INPUT_PNT (peer) != attr_endp)
  1661. {
  1662. zlog (peer->log, LOG_WARNING,
  1663. "%s: BGP attribute %s, fetch error",
  1664. peer->host, LOOKUP (attr_str, type));
  1665. bgp_notify_send (peer,
  1666. BGP_NOTIFY_UPDATE_ERR,
  1667. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1668. if (as4_path)
  1669. aspath_unintern (&as4_path);
  1670. return BGP_ATTR_PARSE_ERROR;
  1671. }
  1672. }
  1673. /* Check final read pointer is same as end pointer. */
  1674. if (BGP_INPUT_PNT (peer) != endp)
  1675. {
  1676. zlog (peer->log, LOG_WARNING,
  1677. "%s: BGP attribute %s, length mismatch",
  1678. peer->host, LOOKUP (attr_str, type));
  1679. bgp_notify_send (peer,
  1680. BGP_NOTIFY_UPDATE_ERR,
  1681. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1682. if (as4_path)
  1683. aspath_unintern (&as4_path);
  1684. return BGP_ATTR_PARSE_ERROR;
  1685. }
  1686. /*
  1687. * At this place we can see whether we got AS4_PATH and/or
  1688. * AS4_AGGREGATOR from a 16Bit peer and act accordingly.
  1689. * We can not do this before we've read all attributes because
  1690. * the as4 handling does not say whether AS4_PATH has to be sent
  1691. * after AS_PATH or not - and when AS4_AGGREGATOR will be send
  1692. * in relationship to AGGREGATOR.
  1693. * So, to be defensive, we are not relying on any order and read
  1694. * all attributes first, including these 32bit ones, and now,
  1695. * afterwards, we look what and if something is to be done for as4.
  1696. */
  1697. if (bgp_attr_munge_as4_attrs (peer, attr, as4_path,
  1698. as4_aggregator, &as4_aggregator_addr))
  1699. {
  1700. if (as4_path)
  1701. aspath_unintern (&as4_path);
  1702. return BGP_ATTR_PARSE_ERROR;
  1703. }
  1704. /* At this stage, we have done all fiddling with as4, and the
  1705. * resulting info is in attr->aggregator resp. attr->aspath
  1706. * so we can chuck as4_aggregator and as4_path alltogether in
  1707. * order to save memory
  1708. */
  1709. if (as4_path)
  1710. {
  1711. aspath_unintern (&as4_path); /* unintern - it is in the hash */
  1712. /* The flag that we got this is still there, but that does not
  1713. * do any trouble
  1714. */
  1715. }
  1716. /*
  1717. * The "rest" of the code does nothing with as4_aggregator.
  1718. * there is no memory attached specifically which is not part
  1719. * of the attr.
  1720. * so ignoring just means do nothing.
  1721. */
  1722. /*
  1723. * Finally do the checks on the aspath we did not do yet
  1724. * because we waited for a potentially synthesized aspath.
  1725. */
  1726. if (attr->flag & (ATTR_FLAG_BIT(BGP_ATTR_AS_PATH)))
  1727. {
  1728. ret = bgp_attr_aspath_check (peer, attr);
  1729. if (ret != BGP_ATTR_PARSE_PROCEED)
  1730. return ret;
  1731. }
  1732. /* Finally intern unknown attribute. */
  1733. if (attr->extra && attr->extra->transit)
  1734. attr->extra->transit = transit_intern (attr->extra->transit);
  1735. return BGP_ATTR_PARSE_PROCEED;
  1736. }
  1737. /* Well-known attribute check. */
  1738. int
  1739. bgp_attr_check (struct peer *peer, struct attr *attr)
  1740. {
  1741. u_char type = 0;
  1742. if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN)))
  1743. type = BGP_ATTR_ORIGIN;
  1744. if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH)))
  1745. type = BGP_ATTR_AS_PATH;
  1746. if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP)))
  1747. type = BGP_ATTR_NEXT_HOP;
  1748. if (peer->sort == BGP_PEER_IBGP
  1749. && ! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)))
  1750. type = BGP_ATTR_LOCAL_PREF;
  1751. if (type)
  1752. {
  1753. zlog (peer->log, LOG_WARNING,
  1754. "%s Missing well-known attribute %d.",
  1755. peer->host, type);
  1756. bgp_notify_send_with_data (peer,
  1757. BGP_NOTIFY_UPDATE_ERR,
  1758. BGP_NOTIFY_UPDATE_MISS_ATTR,
  1759. &type, 1);
  1760. return BGP_ATTR_PARSE_ERROR;
  1761. }
  1762. return BGP_ATTR_PARSE_PROCEED;
  1763. }
  1764. int stream_put_prefix (struct stream *, struct prefix *);
  1765. size_t
  1766. bgp_packet_mpattr_start (struct stream *s, afi_t afi, safi_t safi,
  1767. struct attr *attr)
  1768. {
  1769. size_t sizep;
  1770. /* Set extended bit always to encode the attribute length as 2 bytes */
  1771. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
  1772. stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
  1773. sizep = stream_get_endp (s);
  1774. stream_putw (s, 0); /* Marker: Attribute length. */
  1775. stream_putw (s, afi); /* AFI */
  1776. stream_putc (s, safi); /* SAFI */
  1777. /* Nexthop */
  1778. switch (afi)
  1779. {
  1780. case AFI_IP:
  1781. switch (safi)
  1782. {
  1783. case SAFI_UNICAST:
  1784. case SAFI_MULTICAST:
  1785. stream_putc (s, 4);
  1786. stream_put_ipv4 (s, attr->nexthop.s_addr);
  1787. break;
  1788. case SAFI_MPLS_VPN:
  1789. stream_putc (s, 12);
  1790. stream_putl (s, 0);
  1791. stream_putl (s, 0);
  1792. stream_put (s, &attr->extra->mp_nexthop_global_in, 4);
  1793. break;
  1794. default:
  1795. break;
  1796. }
  1797. break;
  1798. #ifdef HAVE_IPV6
  1799. case AFI_IP6:
  1800. switch (safi)
  1801. {
  1802. case SAFI_UNICAST:
  1803. case SAFI_MULTICAST:
  1804. {
  1805. unsigned long sizep;
  1806. struct attr_extra *attre = attr->extra;
  1807. assert (attr->extra);
  1808. stream_putc (s, attre->mp_nexthop_len);
  1809. stream_put (s, &attre->mp_nexthop_global, 16);
  1810. if (attre->mp_nexthop_len == 32)
  1811. stream_put (s, &attre->mp_nexthop_local, 16);
  1812. }
  1813. default:
  1814. break;
  1815. }
  1816. break;
  1817. #endif /*HAVE_IPV6*/
  1818. default:
  1819. break;
  1820. }
  1821. /* SNPA */
  1822. stream_putc (s, 0);
  1823. return sizep;
  1824. }
  1825. void
  1826. bgp_packet_mpattr_prefix (struct stream *s, afi_t afi, safi_t safi,
  1827. struct prefix *p, struct prefix_rd *prd,
  1828. u_char *tag)
  1829. {
  1830. switch (safi)
  1831. {
  1832. case SAFI_MPLS_VPN:
  1833. /* Tag, RD, Prefix write. */
  1834. stream_putc (s, p->prefixlen + 88);
  1835. stream_put (s, tag, 3);
  1836. stream_put (s, prd->val, 8);
  1837. stream_put (s, &p->u.prefix, PSIZE (p->prefixlen));
  1838. break;
  1839. default:
  1840. /* Prefix write. */
  1841. stream_put_prefix (s, p);
  1842. break;
  1843. }
  1844. }
  1845. void
  1846. bgp_packet_mpattr_end (struct stream *s, size_t sizep)
  1847. {
  1848. /* Set MP attribute length. Don't count the (2) bytes used to encode
  1849. the attr length */
  1850. stream_putw_at (s, sizep, (stream_get_endp (s) - sizep) - 2);
  1851. }
  1852. /* Make attribute packet. */
  1853. bgp_size_t
  1854. bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
  1855. struct stream *s, struct attr *attr,
  1856. struct prefix *p, afi_t afi, safi_t safi,
  1857. struct peer *from, struct prefix_rd *prd, u_char *tag)
  1858. {
  1859. size_t cp;
  1860. size_t aspath_sizep;
  1861. struct aspath *aspath;
  1862. int send_as4_path = 0;
  1863. int send_as4_aggregator = 0;
  1864. int use32bit = (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)) ? 1 : 0;
  1865. size_t mpattrlen_pos = 0;
  1866. if (! bgp)
  1867. bgp = bgp_get_default ();
  1868. /* Remember current pointer. */
  1869. cp = stream_get_endp (s);
  1870. if (p && !(afi == AFI_IP && safi == SAFI_UNICAST))
  1871. {
  1872. mpattrlen_pos = bgp_packet_mpattr_start(s, afi, safi, attr);
  1873. bgp_packet_mpattr_prefix(s, afi, safi, p, prd, tag);
  1874. bgp_packet_mpattr_end(s, mpattrlen_pos);
  1875. }
  1876. /* Origin attribute. */
  1877. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1878. stream_putc (s, BGP_ATTR_ORIGIN);
  1879. stream_putc (s, 1);
  1880. stream_putc (s, attr->origin);
  1881. /* AS path attribute. */
  1882. /* If remote-peer is EBGP */
  1883. if (peer->sort == BGP_PEER_EBGP
  1884. && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)
  1885. || attr->aspath->segments == NULL)
  1886. && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)))
  1887. {
  1888. aspath = aspath_dup (attr->aspath);
  1889. if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION))
  1890. {
  1891. /* Strip the confed info, and then stuff our path CONFED_ID
  1892. on the front */
  1893. aspath = aspath_delete_confed_seq (aspath);
  1894. aspath = aspath_add_seq (aspath, bgp->confed_id);
  1895. }
  1896. else
  1897. {
  1898. if (peer->change_local_as) {
  1899. /* If replace-as is specified, we only use the change_local_as when
  1900. advertising routes. */
  1901. if( ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_REPLACE_AS) ) {
  1902. aspath = aspath_add_seq (aspath, peer->local_as);
  1903. }
  1904. aspath = aspath_add_seq (aspath, peer->change_local_as);
  1905. } else {
  1906. aspath = aspath_add_seq (aspath, peer->local_as);
  1907. }
  1908. }
  1909. }
  1910. else if (peer->sort == BGP_PEER_CONFED)
  1911. {
  1912. /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */
  1913. aspath = aspath_dup (attr->aspath);
  1914. aspath = aspath_add_confed_seq (aspath, peer->local_as);
  1915. }
  1916. else
  1917. aspath = attr->aspath;
  1918. /* If peer is not AS4 capable, then:
  1919. * - send the created AS_PATH out as AS4_PATH (optional, transitive),
  1920. * but ensure that no AS_CONFED_SEQUENCE and AS_CONFED_SET path segment
  1921. * types are in it (i.e. exclude them if they are there)
  1922. * AND do this only if there is at least one asnum > 65535 in the path!
  1923. * - send an AS_PATH out, but put 16Bit ASnums in it, not 32bit, and change
  1924. * all ASnums > 65535 to BGP_AS_TRANS
  1925. */
  1926. stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  1927. stream_putc (s, BGP_ATTR_AS_PATH);
  1928. aspath_sizep = stream_get_endp (s);
  1929. stream_putw (s, 0);
  1930. stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, use32bit));
  1931. /* OLD session may need NEW_AS_PATH sent, if there are 4-byte ASNs
  1932. * in the path
  1933. */
  1934. if (!use32bit && aspath_has_as4 (aspath))
  1935. send_as4_path = 1; /* we'll do this later, at the correct place */
  1936. /* Nexthop attribute. */
  1937. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP) && afi == AFI_IP)
  1938. {
  1939. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1940. stream_putc (s, BGP_ATTR_NEXT_HOP);
  1941. stream_putc (s, 4);
  1942. if (safi == SAFI_MPLS_VPN)
  1943. {
  1944. if (attr->nexthop.s_addr == 0)
  1945. stream_put_ipv4 (s, peer->nexthop.v4.s_addr);
  1946. else
  1947. stream_put_ipv4 (s, attr->nexthop.s_addr);
  1948. }
  1949. else
  1950. stream_put_ipv4 (s, attr->nexthop.s_addr);
  1951. }
  1952. /* MED attribute. */
  1953. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  1954. {
  1955. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1956. stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
  1957. stream_putc (s, 4);
  1958. stream_putl (s, attr->med);
  1959. }
  1960. /* Local preference. */
  1961. if (peer->sort == BGP_PEER_IBGP ||
  1962. peer->sort == BGP_PEER_CONFED)
  1963. {
  1964. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1965. stream_putc (s, BGP_ATTR_LOCAL_PREF);
  1966. stream_putc (s, 4);
  1967. stream_putl (s, attr->local_pref);
  1968. }
  1969. /* Atomic aggregate. */
  1970. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
  1971. {
  1972. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1973. stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
  1974. stream_putc (s, 0);
  1975. }
  1976. /* Aggregator. */
  1977. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
  1978. {
  1979. assert (attr->extra);
  1980. /* Common to BGP_ATTR_AGGREGATOR, regardless of ASN size */
  1981. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  1982. stream_putc (s, BGP_ATTR_AGGREGATOR);
  1983. if (use32bit)
  1984. {
  1985. /* AS4 capable peer */
  1986. stream_putc (s, 8);
  1987. stream_putl (s, attr->extra->aggregator_as);
  1988. }
  1989. else
  1990. {
  1991. /* 2-byte AS peer */
  1992. stream_putc (s, 6);
  1993. /* Is ASN representable in 2-bytes? Or must AS_TRANS be used? */
  1994. if ( attr->extra->aggregator_as > 65535 )
  1995. {
  1996. stream_putw (s, BGP_AS_TRANS);
  1997. /* we have to send AS4_AGGREGATOR, too.
  1998. * we'll do that later in order to send attributes in ascending
  1999. * order.
  2000. */
  2001. send_as4_aggregator = 1;
  2002. }
  2003. else
  2004. stream_putw (s, (u_int16_t) attr->extra->aggregator_as);
  2005. }
  2006. stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
  2007. }
  2008. /* Community attribute. */
  2009. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY)
  2010. && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES)))
  2011. {
  2012. if (attr->community->size * 4 > 255)
  2013. {
  2014. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  2015. stream_putc (s, BGP_ATTR_COMMUNITIES);
  2016. stream_putw (s, attr->community->size * 4);
  2017. }
  2018. else
  2019. {
  2020. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  2021. stream_putc (s, BGP_ATTR_COMMUNITIES);
  2022. stream_putc (s, attr->community->size * 4);
  2023. }
  2024. stream_put (s, attr->community->val, attr->community->size * 4);
  2025. }
  2026. /* Route Reflector. */
  2027. if (peer->sort == BGP_PEER_IBGP
  2028. && from
  2029. && from->sort == BGP_PEER_IBGP)
  2030. {
  2031. /* Originator ID. */
  2032. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  2033. stream_putc (s, BGP_ATTR_ORIGINATOR_ID);
  2034. stream_putc (s, 4);
  2035. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
  2036. stream_put_in_addr (s, &attr->extra->originator_id);
  2037. else
  2038. stream_put_in_addr (s, &from->remote_id);
  2039. /* Cluster list. */
  2040. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  2041. stream_putc (s, BGP_ATTR_CLUSTER_LIST);
  2042. if (attr->extra && attr->extra->cluster)
  2043. {
  2044. stream_putc (s, attr->extra->cluster->length + 4);
  2045. /* If this peer configuration's parent BGP has cluster_id. */
  2046. if (bgp->config & BGP_CONFIG_CLUSTER_ID)
  2047. stream_put_in_addr (s, &bgp->cluster_id);
  2048. else
  2049. stream_put_in_addr (s, &bgp->router_id);
  2050. stream_put (s, attr->extra->cluster->list,
  2051. attr->extra->cluster->length);
  2052. }
  2053. else
  2054. {
  2055. stream_putc (s, 4);
  2056. /* If this peer configuration's parent BGP has cluster_id. */
  2057. if (bgp->config & BGP_CONFIG_CLUSTER_ID)
  2058. stream_put_in_addr (s, &bgp->cluster_id);
  2059. else
  2060. stream_put_in_addr (s, &bgp->router_id);
  2061. }
  2062. }
  2063. /* Extended Communities attribute. */
  2064. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY)
  2065. && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES)))
  2066. {
  2067. struct attr_extra *attre = attr->extra;
  2068. assert (attre);
  2069. if (peer->sort == BGP_PEER_IBGP
  2070. || peer->sort == BGP_PEER_CONFED)
  2071. {
  2072. if (attre->ecommunity->size * 8 > 255)
  2073. {
  2074. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  2075. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  2076. stream_putw (s, attre->ecommunity->size * 8);
  2077. }
  2078. else
  2079. {
  2080. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  2081. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  2082. stream_putc (s, attre->ecommunity->size * 8);
  2083. }
  2084. stream_put (s, attre->ecommunity->val, attre->ecommunity->size * 8);
  2085. }
  2086. else
  2087. {
  2088. u_int8_t *pnt;
  2089. int tbit;
  2090. int ecom_tr_size = 0;
  2091. int i;
  2092. for (i = 0; i < attre->ecommunity->size; i++)
  2093. {
  2094. pnt = attre->ecommunity->val + (i * 8);
  2095. tbit = *pnt;
  2096. if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
  2097. continue;
  2098. ecom_tr_size++;
  2099. }
  2100. if (ecom_tr_size)
  2101. {
  2102. if (ecom_tr_size * 8 > 255)
  2103. {
  2104. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  2105. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  2106. stream_putw (s, ecom_tr_size * 8);
  2107. }
  2108. else
  2109. {
  2110. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  2111. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  2112. stream_putc (s, ecom_tr_size * 8);
  2113. }
  2114. for (i = 0; i < attre->ecommunity->size; i++)
  2115. {
  2116. pnt = attre->ecommunity->val + (i * 8);
  2117. tbit = *pnt;
  2118. if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
  2119. continue;
  2120. stream_put (s, pnt, 8);
  2121. }
  2122. }
  2123. }
  2124. }
  2125. if ( send_as4_path )
  2126. {
  2127. /* If the peer is NOT As4 capable, AND */
  2128. /* there are ASnums > 65535 in path THEN
  2129. * give out AS4_PATH */
  2130. /* Get rid of all AS_CONFED_SEQUENCE and AS_CONFED_SET
  2131. * path segments!
  2132. * Hm, I wonder... confederation things *should* only be at
  2133. * the beginning of an aspath, right? Then we should use
  2134. * aspath_delete_confed_seq for this, because it is already
  2135. * there! (JK)
  2136. * Folks, talk to me: what is reasonable here!?
  2137. */
  2138. aspath = aspath_delete_confed_seq (aspath);
  2139. stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
  2140. stream_putc (s, BGP_ATTR_AS4_PATH);
  2141. aspath_sizep = stream_get_endp (s);
  2142. stream_putw (s, 0);
  2143. stream_putw_at (s, aspath_sizep, aspath_put (s, aspath, 1));
  2144. }
  2145. if (aspath != attr->aspath)
  2146. aspath_free (aspath);
  2147. if ( send_as4_aggregator )
  2148. {
  2149. assert (attr->extra);
  2150. /* send AS4_AGGREGATOR, at this place */
  2151. /* this section of code moved here in order to ensure the correct
  2152. * *ascending* order of attributes
  2153. */
  2154. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  2155. stream_putc (s, BGP_ATTR_AS4_AGGREGATOR);
  2156. stream_putc (s, 8);
  2157. stream_putl (s, attr->extra->aggregator_as);
  2158. stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
  2159. }
  2160. /* Unknown transit attribute. */
  2161. if (attr->extra && attr->extra->transit)
  2162. stream_put (s, attr->extra->transit->val, attr->extra->transit->length);
  2163. /* Return total size of attribute. */
  2164. return stream_get_endp (s) - cp;
  2165. }
  2166. size_t
  2167. bgp_packet_mpunreach_start (struct stream *s, afi_t afi, safi_t safi)
  2168. {
  2169. unsigned long attrlen_pnt;
  2170. /* Set extended bit always to encode the attribute length as 2 bytes */
  2171. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_EXTLEN);
  2172. stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
  2173. attrlen_pnt = stream_get_endp (s);
  2174. stream_putw (s, 0); /* Length of this attribute. */
  2175. stream_putw (s, afi);
  2176. safi = (safi == SAFI_MPLS_VPN) ? SAFI_MPLS_LABELED_VPN : safi;
  2177. stream_putc (s, safi);
  2178. return attrlen_pnt;
  2179. }
  2180. void
  2181. bgp_packet_mpunreach_prefix (struct stream *s, struct prefix *p,
  2182. afi_t afi, safi_t safi, struct prefix_rd *prd,
  2183. u_char *tag)
  2184. {
  2185. if (safi == SAFI_MPLS_VPN)
  2186. {
  2187. stream_putc (s, p->prefixlen + 88);
  2188. stream_put (s, tag, 3);
  2189. stream_put (s, prd->val, 8);
  2190. stream_put (s, &p->u.prefix, PSIZE (p->prefixlen));
  2191. }
  2192. else
  2193. stream_put_prefix (s, p);
  2194. }
  2195. void
  2196. bgp_packet_mpunreach_end (struct stream *s, size_t attrlen_pnt)
  2197. {
  2198. bgp_size_t size;
  2199. /* Set MP attribute length. Don't count the (2) bytes used to encode
  2200. the attr length */
  2201. size = stream_get_endp (s) - attrlen_pnt - 2;
  2202. stream_putw_at (s, attrlen_pnt, size);
  2203. }
  2204. /* Initialization of attribute. */
  2205. void
  2206. bgp_attr_init (void)
  2207. {
  2208. aspath_init ();
  2209. attrhash_init ();
  2210. community_init ();
  2211. ecommunity_init ();
  2212. cluster_init ();
  2213. transit_init ();
  2214. }
  2215. void
  2216. bgp_attr_finish (void)
  2217. {
  2218. aspath_finish ();
  2219. attrhash_finish ();
  2220. community_finish ();
  2221. ecommunity_finish ();
  2222. cluster_finish ();
  2223. transit_finish ();
  2224. }
  2225. /* Make attribute packet. */
  2226. void
  2227. bgp_dump_routes_attr (struct stream *s, struct attr *attr,
  2228. struct prefix *prefix)
  2229. {
  2230. unsigned long cp;
  2231. unsigned long len;
  2232. size_t aspath_lenp;
  2233. struct aspath *aspath;
  2234. /* Remember current pointer. */
  2235. cp = stream_get_endp (s);
  2236. /* Place holder of length. */
  2237. stream_putw (s, 0);
  2238. /* Origin attribute. */
  2239. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  2240. stream_putc (s, BGP_ATTR_ORIGIN);
  2241. stream_putc (s, 1);
  2242. stream_putc (s, attr->origin);
  2243. aspath = attr->aspath;
  2244. stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  2245. stream_putc (s, BGP_ATTR_AS_PATH);
  2246. aspath_lenp = stream_get_endp (s);
  2247. stream_putw (s, 0);
  2248. stream_putw_at (s, aspath_lenp, aspath_put (s, aspath, 1));
  2249. /* Nexthop attribute. */
  2250. /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
  2251. if(prefix != NULL
  2252. #ifdef HAVE_IPV6
  2253. && prefix->family != AF_INET6
  2254. #endif /* HAVE_IPV6 */
  2255. )
  2256. {
  2257. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  2258. stream_putc (s, BGP_ATTR_NEXT_HOP);
  2259. stream_putc (s, 4);
  2260. stream_put_ipv4 (s, attr->nexthop.s_addr);
  2261. }
  2262. /* MED attribute. */
  2263. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  2264. {
  2265. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  2266. stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
  2267. stream_putc (s, 4);
  2268. stream_putl (s, attr->med);
  2269. }
  2270. /* Local preference. */
  2271. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
  2272. {
  2273. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  2274. stream_putc (s, BGP_ATTR_LOCAL_PREF);
  2275. stream_putc (s, 4);
  2276. stream_putl (s, attr->local_pref);
  2277. }
  2278. /* Atomic aggregate. */
  2279. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
  2280. {
  2281. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  2282. stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
  2283. stream_putc (s, 0);
  2284. }
  2285. /* Aggregator. */
  2286. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
  2287. {
  2288. assert (attr->extra);
  2289. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  2290. stream_putc (s, BGP_ATTR_AGGREGATOR);
  2291. stream_putc (s, 8);
  2292. stream_putl (s, attr->extra->aggregator_as);
  2293. stream_put_ipv4 (s, attr->extra->aggregator_addr.s_addr);
  2294. }
  2295. /* Community attribute. */
  2296. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES))
  2297. {
  2298. if (attr->community->size * 4 > 255)
  2299. {
  2300. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  2301. stream_putc (s, BGP_ATTR_COMMUNITIES);
  2302. stream_putw (s, attr->community->size * 4);
  2303. }
  2304. else
  2305. {
  2306. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  2307. stream_putc (s, BGP_ATTR_COMMUNITIES);
  2308. stream_putc (s, attr->community->size * 4);
  2309. }
  2310. stream_put (s, attr->community->val, attr->community->size * 4);
  2311. }
  2312. #ifdef HAVE_IPV6
  2313. /* Add a MP_NLRI attribute to dump the IPv6 next hop */
  2314. if (prefix != NULL && prefix->family == AF_INET6 && attr->extra &&
  2315. (attr->extra->mp_nexthop_len == 16 || attr->extra->mp_nexthop_len == 32) )
  2316. {
  2317. int sizep;
  2318. struct attr_extra *attre = attr->extra;
  2319. stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
  2320. stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
  2321. sizep = stream_get_endp (s);
  2322. /* MP header */
  2323. stream_putc (s, 0); /* Marker: Attribute length. */
  2324. stream_putw(s, AFI_IP6); /* AFI */
  2325. stream_putc(s, SAFI_UNICAST); /* SAFI */
  2326. /* Next hop */
  2327. stream_putc(s, attre->mp_nexthop_len);
  2328. stream_put(s, &attre->mp_nexthop_global, 16);
  2329. if (attre->mp_nexthop_len == 32)
  2330. stream_put(s, &attre->mp_nexthop_local, 16);
  2331. /* SNPA */
  2332. stream_putc(s, 0);
  2333. /* Prefix */
  2334. stream_put_prefix(s, prefix);
  2335. /* Set MP attribute length. */
  2336. stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
  2337. }
  2338. #endif /* HAVE_IPV6 */
  2339. /* Return total size of attribute. */
  2340. len = stream_get_endp (s) - cp - 2;
  2341. stream_putw_at (s, cp, len);
  2342. }