bgp_attr.c 68 KB

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