bgp_attr.c 66 KB

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