bgp_attr.c 65 KB

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