bgp_attr.c 48 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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. 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. { 0, NULL }
  52. };
  53. struct hash *cluster_hash;
  54. static void *
  55. cluster_hash_alloc (struct cluster_list *val)
  56. {
  57. struct cluster_list *cluster;
  58. cluster = XMALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
  59. cluster->length = val->length;
  60. if (cluster->length)
  61. {
  62. cluster->list = XMALLOC (MTYPE_CLUSTER_VAL, val->length);
  63. memcpy (cluster->list, val->list, val->length);
  64. }
  65. else
  66. cluster->list = NULL;
  67. cluster->refcnt = 0;
  68. return cluster;
  69. }
  70. /* Cluster list related functions. */
  71. static struct cluster_list *
  72. cluster_parse (struct in_addr * pnt, int length)
  73. {
  74. struct cluster_list tmp;
  75. struct cluster_list *cluster;
  76. tmp.length = length;
  77. tmp.list = pnt;
  78. cluster = hash_get (cluster_hash, &tmp, cluster_hash_alloc);
  79. cluster->refcnt++;
  80. return cluster;
  81. }
  82. int
  83. cluster_loop_check (struct cluster_list *cluster, struct in_addr originator)
  84. {
  85. int i;
  86. for (i = 0; i < cluster->length / 4; i++)
  87. if (cluster->list[i].s_addr == originator.s_addr)
  88. return 1;
  89. return 0;
  90. }
  91. static unsigned int
  92. cluster_hash_key_make (struct cluster_list *cluster)
  93. {
  94. unsigned int key = 0;
  95. int length;
  96. caddr_t pnt;
  97. length = cluster->length;
  98. pnt = (caddr_t) cluster->list;
  99. while (length)
  100. key += pnt[--length];
  101. return key;
  102. }
  103. static int
  104. cluster_hash_cmp (struct cluster_list *cluster1, struct cluster_list *cluster2)
  105. {
  106. if (cluster1->length == cluster2->length &&
  107. memcmp (cluster1->list, cluster2->list, cluster1->length) == 0)
  108. return 1;
  109. return 0;
  110. }
  111. static void
  112. cluster_free (struct cluster_list *cluster)
  113. {
  114. if (cluster->list)
  115. XFREE (MTYPE_CLUSTER_VAL, cluster->list);
  116. XFREE (MTYPE_CLUSTER, cluster);
  117. }
  118. static struct cluster_list *
  119. cluster_dup (struct cluster_list *cluster)
  120. {
  121. struct cluster_list *new;
  122. new = XMALLOC (MTYPE_CLUSTER, sizeof (struct cluster_list));
  123. memset (new, 0, sizeof (struct cluster_list));
  124. new->length = cluster->length;
  125. if (cluster->length)
  126. {
  127. new->list = XMALLOC (MTYPE_CLUSTER_VAL, cluster->length);
  128. memcpy (new->list, cluster->list, cluster->length);
  129. }
  130. else
  131. new->list = NULL;
  132. return new;
  133. }
  134. static struct cluster_list *
  135. cluster_intern (struct cluster_list *cluster)
  136. {
  137. struct cluster_list *find;
  138. find = hash_get (cluster_hash, cluster, cluster_hash_alloc);
  139. find->refcnt++;
  140. return find;
  141. }
  142. void
  143. cluster_unintern (struct cluster_list *cluster)
  144. {
  145. struct cluster_list *ret;
  146. if (cluster->refcnt)
  147. cluster->refcnt--;
  148. if (cluster->refcnt == 0)
  149. {
  150. ret = hash_release (cluster_hash, cluster);
  151. cluster_free (cluster);
  152. }
  153. }
  154. static void
  155. cluster_init (void)
  156. {
  157. cluster_hash = hash_create (cluster_hash_key_make, cluster_hash_cmp);
  158. }
  159. /* Unknown transit attribute. */
  160. struct hash *transit_hash;
  161. static void
  162. transit_free (struct transit *transit)
  163. {
  164. if (transit->val)
  165. XFREE (MTYPE_TRANSIT_VAL, transit->val);
  166. XFREE (MTYPE_TRANSIT, transit);
  167. }
  168. static void *
  169. transit_hash_alloc (struct transit *transit)
  170. {
  171. /* Transit structure is already allocated. */
  172. return transit;
  173. }
  174. static struct transit *
  175. transit_intern (struct transit *transit)
  176. {
  177. struct transit *find;
  178. find = hash_get (transit_hash, transit, transit_hash_alloc);
  179. if (find != transit)
  180. transit_free (transit);
  181. find->refcnt++;
  182. return find;
  183. }
  184. void
  185. transit_unintern (struct transit *transit)
  186. {
  187. struct transit *ret;
  188. if (transit->refcnt)
  189. transit->refcnt--;
  190. if (transit->refcnt == 0)
  191. {
  192. ret = hash_release (transit_hash, transit);
  193. transit_free (transit);
  194. }
  195. }
  196. static unsigned int
  197. transit_hash_key_make (struct transit *transit)
  198. {
  199. unsigned int key = 0;
  200. int length;
  201. caddr_t pnt;
  202. length = transit->length;
  203. pnt = (caddr_t) transit->val;
  204. while (length)
  205. key += pnt[--length];
  206. return key;
  207. }
  208. static int
  209. transit_hash_cmp (struct transit *transit1, struct transit *transit2)
  210. {
  211. if (transit1->length == transit2->length &&
  212. memcmp (transit1->val, transit2->val, transit1->length) == 0)
  213. return 1;
  214. return 0;
  215. }
  216. static void
  217. transit_init ()
  218. {
  219. transit_hash = hash_create (transit_hash_key_make, transit_hash_cmp);
  220. }
  221. /* Attribute hash routines. */
  222. struct hash *attrhash;
  223. unsigned int
  224. attrhash_key_make (struct attr *attr)
  225. {
  226. unsigned int key = 0;
  227. key += attr->origin;
  228. key += attr->nexthop.s_addr;
  229. key += attr->med;
  230. key += attr->local_pref;
  231. key += attr->aggregator_as;
  232. key += attr->aggregator_addr.s_addr;
  233. key += attr->weight;
  234. key += attr->mp_nexthop_global_in.s_addr;
  235. if (attr->aspath)
  236. key += aspath_key_make (attr->aspath);
  237. if (attr->community)
  238. key += community_hash_make (attr->community);
  239. if (attr->ecommunity)
  240. key += ecommunity_hash_make (attr->ecommunity);
  241. if (attr->cluster)
  242. key += cluster_hash_key_make (attr->cluster);
  243. if (attr->transit)
  244. key += transit_hash_key_make (attr->transit);
  245. #ifdef HAVE_IPV6
  246. {
  247. int i;
  248. key += attr->mp_nexthop_len;
  249. for (i = 0; i < 16; i++)
  250. key += attr->mp_nexthop_global.s6_addr[i];
  251. for (i = 0; i < 16; i++)
  252. key += attr->mp_nexthop_local.s6_addr[i];
  253. }
  254. #endif /* HAVE_IPV6 */
  255. return key;
  256. }
  257. int
  258. attrhash_cmp (struct attr *attr1, struct attr *attr2)
  259. {
  260. if (attr1->flag == attr2->flag
  261. && attr1->origin == attr2->origin
  262. && attr1->nexthop.s_addr == attr2->nexthop.s_addr
  263. && attr1->med == attr2->med
  264. && attr1->local_pref == attr2->local_pref
  265. && attr1->aggregator_as == attr2->aggregator_as
  266. && attr1->aggregator_addr.s_addr == attr2->aggregator_addr.s_addr
  267. && attr1->weight == attr2->weight
  268. #ifdef HAVE_IPV6
  269. && attr1->mp_nexthop_len == attr2->mp_nexthop_len
  270. && IPV6_ADDR_SAME (&attr1->mp_nexthop_global, &attr2->mp_nexthop_global)
  271. && IPV6_ADDR_SAME (&attr1->mp_nexthop_local, &attr2->mp_nexthop_local)
  272. #endif /* HAVE_IPV6 */
  273. && IPV4_ADDR_SAME (&attr1->mp_nexthop_global_in, &attr2->mp_nexthop_global_in)
  274. && attr1->aspath == attr2->aspath
  275. && attr1->community == attr2->community
  276. && attr1->ecommunity == attr2->ecommunity
  277. && attr1->cluster == attr2->cluster
  278. && attr1->transit == attr2->transit)
  279. return 1;
  280. else
  281. return 0;
  282. }
  283. static void
  284. attrhash_init ()
  285. {
  286. attrhash = hash_create (attrhash_key_make, attrhash_cmp);
  287. }
  288. static void
  289. attr_show_all_iterator (struct hash_backet *backet, struct vty *vty)
  290. {
  291. struct attr *attr = backet->data;
  292. vty_out (vty, "attr[%ld] nexthop %s%s", attr->refcnt,
  293. inet_ntoa (attr->nexthop), VTY_NEWLINE);
  294. }
  295. void
  296. attr_show_all (struct vty *vty)
  297. {
  298. hash_iterate (attrhash,
  299. (void (*)(struct hash_backet *, void *))
  300. attr_show_all_iterator,
  301. vty);
  302. }
  303. static void *
  304. bgp_attr_hash_alloc (struct attr *val)
  305. {
  306. struct attr *attr;
  307. attr = XMALLOC (MTYPE_ATTR, sizeof (struct attr));
  308. *attr = *val;
  309. attr->refcnt = 0;
  310. return attr;
  311. }
  312. /* Internet argument attribute. */
  313. struct attr *
  314. bgp_attr_intern (struct attr *attr)
  315. {
  316. struct attr *find;
  317. /* Intern referenced strucutre. */
  318. if (attr->aspath)
  319. {
  320. if (! attr->aspath->refcnt)
  321. attr->aspath = aspath_intern (attr->aspath);
  322. else
  323. attr->aspath->refcnt++;
  324. }
  325. if (attr->community)
  326. {
  327. if (! attr->community->refcnt)
  328. attr->community = community_intern (attr->community);
  329. else
  330. attr->community->refcnt++;
  331. }
  332. if (attr->ecommunity)
  333. {
  334. if (! attr->ecommunity->refcnt)
  335. attr->ecommunity = ecommunity_intern (attr->ecommunity);
  336. else
  337. attr->ecommunity->refcnt++;
  338. }
  339. if (attr->cluster)
  340. {
  341. if (! attr->cluster->refcnt)
  342. attr->cluster = cluster_intern (attr->cluster);
  343. else
  344. attr->cluster->refcnt++;
  345. }
  346. if (attr->transit)
  347. {
  348. if (! attr->transit->refcnt)
  349. attr->transit = transit_intern (attr->transit);
  350. else
  351. attr->transit->refcnt++;
  352. }
  353. find = (struct attr *) hash_get (attrhash, attr, bgp_attr_hash_alloc);
  354. find->refcnt++;
  355. return find;
  356. }
  357. /* Make network statement's attribute. */
  358. struct attr *
  359. bgp_attr_default_set (struct attr *attr, u_char origin)
  360. {
  361. memset (attr, 0, sizeof (struct attr));
  362. attr->origin = origin;
  363. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
  364. attr->aspath = aspath_empty ();
  365. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
  366. attr->weight = 32768;
  367. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
  368. #ifdef HAVE_IPV6
  369. attr->mp_nexthop_len = 16;
  370. #endif
  371. return attr;
  372. }
  373. /* Make network statement's attribute. */
  374. struct attr *
  375. bgp_attr_default_intern (u_char origin)
  376. {
  377. struct attr attr;
  378. struct attr *new;
  379. memset (&attr, 0, sizeof (struct attr));
  380. attr.origin = origin;
  381. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
  382. attr.aspath = aspath_empty ();
  383. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
  384. attr.weight = 32768;
  385. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
  386. #ifdef HAVE_IPV6
  387. attr.mp_nexthop_len = 16;
  388. #endif
  389. new = bgp_attr_intern (&attr);
  390. aspath_unintern (new->aspath);
  391. return new;
  392. }
  393. struct attr *
  394. bgp_attr_aggregate_intern (struct bgp *bgp, u_char origin,
  395. struct aspath *aspath,
  396. struct community *community, int as_set)
  397. {
  398. struct attr attr;
  399. struct attr *new;
  400. memset (&attr, 0, sizeof (struct attr));
  401. /* Origin attribute. */
  402. attr.origin = origin;
  403. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
  404. /* AS path attribute. */
  405. if (aspath)
  406. attr.aspath = aspath_intern (aspath);
  407. else
  408. attr.aspath = aspath_empty ();
  409. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
  410. /* Next hop attribute. */
  411. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
  412. if (community)
  413. {
  414. attr.community = community;
  415. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
  416. }
  417. attr.weight = 32768;
  418. #ifdef HAVE_IPV6
  419. attr.mp_nexthop_len = 16;
  420. #endif
  421. if (! as_set)
  422. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
  423. attr.flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
  424. if (CHECK_FLAG (bgp->config, BGP_CONFIG_CONFEDERATION))
  425. attr.aggregator_as = bgp->confed_id;
  426. else
  427. attr.aggregator_as = bgp->as;
  428. attr.aggregator_addr = bgp->router_id;
  429. new = bgp_attr_intern (&attr);
  430. aspath_unintern (new->aspath);
  431. return new;
  432. }
  433. /* Free bgp attribute and aspath. */
  434. void
  435. bgp_attr_unintern (struct attr *attr)
  436. {
  437. struct attr *ret;
  438. struct aspath *aspath;
  439. struct community *community;
  440. struct ecommunity *ecommunity;
  441. struct cluster_list *cluster;
  442. struct transit *transit;
  443. /* Decrement attribute reference. */
  444. attr->refcnt--;
  445. aspath = attr->aspath;
  446. community = attr->community;
  447. ecommunity = attr->ecommunity;
  448. cluster = attr->cluster;
  449. transit = attr->transit;
  450. /* If reference becomes zero then free attribute object. */
  451. if (attr->refcnt == 0)
  452. {
  453. ret = hash_release (attrhash, attr);
  454. assert (ret != NULL);
  455. XFREE (MTYPE_ATTR, attr);
  456. }
  457. /* aspath refcount shoud be decrement. */
  458. if (aspath)
  459. aspath_unintern (aspath);
  460. if (community)
  461. community_unintern (community);
  462. if (ecommunity)
  463. ecommunity_unintern (ecommunity);
  464. if (cluster)
  465. cluster_unintern (cluster);
  466. if (transit)
  467. transit_unintern (transit);
  468. }
  469. void
  470. bgp_attr_flush (struct attr *attr)
  471. {
  472. if (attr->aspath && ! attr->aspath->refcnt)
  473. aspath_free (attr->aspath);
  474. if (attr->community && ! attr->community->refcnt)
  475. community_free (attr->community);
  476. if (attr->ecommunity && ! attr->ecommunity->refcnt)
  477. ecommunity_free (attr->ecommunity);
  478. if (attr->cluster && ! attr->cluster->refcnt)
  479. cluster_free (attr->cluster);
  480. if (attr->transit && ! attr->transit->refcnt)
  481. transit_free (attr->transit);
  482. }
  483. /* Get origin attribute of the update message. */
  484. static int
  485. bgp_attr_origin (struct peer *peer, bgp_size_t length,
  486. struct attr *attr, u_char flag, u_char *startp)
  487. {
  488. bgp_size_t total;
  489. /* total is entire attribute length include Attribute Flags (1),
  490. Attribute Type code (1) and Attribute length (1 or 2). */
  491. total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
  492. /* If any recognized attribute has Attribute Flags that conflict
  493. with the Attribute Type Code, then the Error Subcode is set to
  494. Attribute Flags Error. The Data field contains the erroneous
  495. attribute (type, length and value). */
  496. if (flag != BGP_ATTR_FLAG_TRANS)
  497. {
  498. zlog (peer->log, LOG_ERR,
  499. "Origin attribute flag isn't transitive %d", flag);
  500. bgp_notify_send_with_data (peer,
  501. BGP_NOTIFY_UPDATE_ERR,
  502. BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
  503. startp, total);
  504. return -1;
  505. }
  506. /* If any recognized attribute has Attribute Length that conflicts
  507. with the expected length (based on the attribute type code), then
  508. the Error Subcode is set to Attribute Length Error. The Data
  509. field contains the erroneous attribute (type, length and
  510. value). */
  511. if (length != 1)
  512. {
  513. zlog (peer->log, LOG_ERR, "Origin attribute length is not one %d",
  514. length);
  515. bgp_notify_send_with_data (peer, BGP_NOTIFY_UPDATE_ERR,
  516. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  517. startp, total);
  518. return -1;
  519. }
  520. /* Fetch origin attribute. */
  521. attr->origin = stream_getc (BGP_INPUT (peer));
  522. /* If the ORIGIN attribute has an undefined value, then the Error
  523. Subcode is set to Invalid Origin Attribute. The Data field
  524. contains the unrecognized attribute (type, length and value). */
  525. if ((attr->origin != BGP_ORIGIN_IGP)
  526. && (attr->origin != BGP_ORIGIN_EGP)
  527. && (attr->origin != BGP_ORIGIN_INCOMPLETE))
  528. {
  529. zlog (peer->log, LOG_ERR, "Origin attribute value is invalid %d",
  530. attr->origin);
  531. bgp_notify_send_with_data (peer,
  532. BGP_NOTIFY_UPDATE_ERR,
  533. BGP_NOTIFY_UPDATE_INVAL_ORIGIN,
  534. startp, total);
  535. return -1;
  536. }
  537. /* Set oring attribute flag. */
  538. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGIN);
  539. return 0;
  540. }
  541. /* Parse AS path information. This function is wrapper of
  542. aspath_parse. */
  543. static int
  544. bgp_attr_aspath (struct peer *peer, bgp_size_t length,
  545. struct attr *attr, u_char flag, u_char *startp)
  546. {
  547. struct bgp *bgp;
  548. struct aspath *aspath;
  549. bgp_size_t total;
  550. total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
  551. /* Flag check. */
  552. if (CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL)
  553. || ! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
  554. {
  555. zlog (peer->log, LOG_ERR,
  556. "Origin attribute flag isn't transitive %d", flag);
  557. bgp_notify_send_with_data (peer,
  558. BGP_NOTIFY_UPDATE_ERR,
  559. BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
  560. startp, total);
  561. return -1;
  562. }
  563. /* In case of IBGP, length will be zero. */
  564. attr->aspath = aspath_parse (stream_pnt (peer->ibuf), length);
  565. if (! attr->aspath)
  566. {
  567. zlog (peer->log, LOG_ERR, "Malformed AS path length is %d", length);
  568. bgp_notify_send (peer,
  569. BGP_NOTIFY_UPDATE_ERR,
  570. BGP_NOTIFY_UPDATE_MAL_AS_PATH);
  571. return -1;
  572. }
  573. bgp = peer->bgp;
  574. /* First AS check for EBGP. */
  575. if (bgp != NULL && bgp_flag_check (bgp, BGP_FLAG_ENFORCE_FIRST_AS))
  576. {
  577. if (peer_sort (peer) == BGP_PEER_EBGP
  578. && ! aspath_firstas_check (attr->aspath, peer->as))
  579. {
  580. zlog (peer->log, LOG_ERR,
  581. "%s incorrect first AS (must be %d)", peer->host, peer->as);
  582. bgp_notify_send (peer,
  583. BGP_NOTIFY_UPDATE_ERR,
  584. BGP_NOTIFY_UPDATE_MAL_AS_PATH);
  585. return -1;
  586. }
  587. }
  588. /* local-as prepend */
  589. if (peer->change_local_as &&
  590. ! CHECK_FLAG (peer->flags, PEER_FLAG_LOCAL_AS_NO_PREPEND))
  591. {
  592. aspath = aspath_dup (attr->aspath);
  593. aspath = aspath_add_seq (aspath, peer->change_local_as);
  594. aspath_unintern (attr->aspath);
  595. attr->aspath = aspath_intern (aspath);
  596. }
  597. /* Forward pointer. */
  598. stream_forward_getp (peer->ibuf, length);
  599. /* Set aspath attribute flag. */
  600. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AS_PATH);
  601. return 0;
  602. }
  603. /* Nexthop attribute. */
  604. static int
  605. bgp_attr_nexthop (struct peer *peer, bgp_size_t length,
  606. struct attr *attr, u_char flag, u_char *startp)
  607. {
  608. bgp_size_t total;
  609. total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
  610. /* Flag check. */
  611. if (CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL)
  612. || ! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
  613. {
  614. zlog (peer->log, LOG_ERR,
  615. "Origin attribute flag isn't transitive %d", flag);
  616. bgp_notify_send_with_data (peer,
  617. BGP_NOTIFY_UPDATE_ERR,
  618. BGP_NOTIFY_UPDATE_ATTR_FLAG_ERR,
  619. startp, total);
  620. return -1;
  621. }
  622. /* Check nexthop attribute length. */
  623. if (length != 4)
  624. {
  625. zlog (peer->log, LOG_ERR, "Nexthop attribute length isn't four [%d]",
  626. length);
  627. bgp_notify_send_with_data (peer,
  628. BGP_NOTIFY_UPDATE_ERR,
  629. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  630. startp, total);
  631. return -1;
  632. }
  633. attr->nexthop.s_addr = stream_get_ipv4 (peer->ibuf);
  634. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP);
  635. return 0;
  636. }
  637. /* MED atrribute. */
  638. static int
  639. bgp_attr_med (struct peer *peer, bgp_size_t length,
  640. struct attr *attr, u_char flag, u_char *startp)
  641. {
  642. bgp_size_t total;
  643. total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
  644. /* Length check. */
  645. if (length != 4)
  646. {
  647. zlog (peer->log, LOG_ERR,
  648. "MED attribute length isn't four [%d]", length);
  649. bgp_notify_send_with_data (peer,
  650. BGP_NOTIFY_UPDATE_ERR,
  651. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR,
  652. startp, total);
  653. return -1;
  654. }
  655. attr->med = stream_getl (peer->ibuf);
  656. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC);
  657. return 0;
  658. }
  659. /* Local preference attribute. */
  660. static int
  661. bgp_attr_local_pref (struct peer *peer, bgp_size_t length,
  662. struct attr *attr, u_char flag)
  663. {
  664. /* If it is contained in an UPDATE message that is received from an
  665. external peer, then this attribute MUST be ignored by the
  666. receiving speaker. */
  667. if (peer_sort (peer) == BGP_PEER_EBGP)
  668. {
  669. stream_forward_getp (peer->ibuf, length);
  670. return 0;
  671. }
  672. if (length == 4)
  673. attr->local_pref = stream_getl (peer->ibuf);
  674. else
  675. attr->local_pref = 0;
  676. /* Set atomic aggregate flag. */
  677. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF);
  678. return 0;
  679. }
  680. /* Atomic aggregate. */
  681. static int
  682. bgp_attr_atomic (struct peer *peer, bgp_size_t length,
  683. struct attr *attr, u_char flag)
  684. {
  685. if (length != 0)
  686. {
  687. zlog (peer->log, LOG_ERR, "Bad atomic aggregate length %d", length);
  688. bgp_notify_send (peer,
  689. BGP_NOTIFY_UPDATE_ERR,
  690. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  691. return -1;
  692. }
  693. /* Set atomic aggregate flag. */
  694. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE);
  695. return 0;
  696. }
  697. /* Aggregator attribute */
  698. static int
  699. bgp_attr_aggregator (struct peer *peer, bgp_size_t length,
  700. struct attr *attr, u_char flag)
  701. {
  702. if (length != 6)
  703. {
  704. zlog (peer->log, LOG_ERR, "Aggregator length is not 6 [%d]", length);
  705. bgp_notify_send (peer,
  706. BGP_NOTIFY_UPDATE_ERR,
  707. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  708. return -1;
  709. }
  710. attr->aggregator_as = stream_getw (peer->ibuf);
  711. attr->aggregator_addr.s_addr = stream_get_ipv4 (peer->ibuf);
  712. /* Set atomic aggregate flag. */
  713. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR);
  714. return 0;
  715. }
  716. /* Community attribute. */
  717. static int
  718. bgp_attr_community (struct peer *peer, bgp_size_t length,
  719. struct attr *attr, u_char flag)
  720. {
  721. if (length == 0)
  722. attr->community = NULL;
  723. else
  724. {
  725. attr->community =
  726. community_parse ((u_int32_t *)stream_pnt (peer->ibuf), length);
  727. stream_forward_getp (peer->ibuf, length);
  728. }
  729. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES);
  730. return 0;
  731. }
  732. /* Originator ID attribute. */
  733. static int
  734. bgp_attr_originator_id (struct peer *peer, bgp_size_t length,
  735. struct attr *attr, u_char flag)
  736. {
  737. if (length != 4)
  738. {
  739. zlog (peer->log, LOG_ERR, "Bad originator ID length %d", length);
  740. bgp_notify_send (peer,
  741. BGP_NOTIFY_UPDATE_ERR,
  742. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  743. return -1;
  744. }
  745. attr->originator_id.s_addr = stream_get_ipv4 (peer->ibuf);
  746. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_ORIGINATOR_ID);
  747. return 0;
  748. }
  749. /* Cluster list attribute. */
  750. static int
  751. bgp_attr_cluster_list (struct peer *peer, bgp_size_t length,
  752. struct attr *attr, u_char flag)
  753. {
  754. /* Check length. */
  755. if (length % 4)
  756. {
  757. zlog (peer->log, LOG_ERR, "Bad cluster list length %d", length);
  758. bgp_notify_send (peer,
  759. BGP_NOTIFY_UPDATE_ERR,
  760. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  761. return -1;
  762. }
  763. attr->cluster = cluster_parse ((struct in_addr *)stream_pnt (peer->ibuf),
  764. length);
  765. stream_forward_getp (peer->ibuf, length);;
  766. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_CLUSTER_LIST);
  767. return 0;
  768. }
  769. /* Multiprotocol reachability information parse. */
  770. static int
  771. bgp_mp_reach_parse (struct peer *peer, bgp_size_t length, struct attr *attr,
  772. struct bgp_nlri *mp_update)
  773. {
  774. u_int16_t afi;
  775. u_char safi;
  776. u_char snpa_num;
  777. u_char snpa_len;
  778. u_char *lim;
  779. bgp_size_t nlri_len;
  780. int ret;
  781. struct stream *s;
  782. /* Set end of packet. */
  783. s = peer->ibuf;
  784. lim = stream_pnt (s) + length;
  785. /* Load AFI, SAFI. */
  786. afi = stream_getw (s);
  787. safi = stream_getc (s);
  788. /* Get nexthop length. */
  789. attr->mp_nexthop_len = stream_getc (s);
  790. /* Nexthop length check. */
  791. switch (attr->mp_nexthop_len)
  792. {
  793. case 4:
  794. stream_get (&attr->mp_nexthop_global_in, s, 4);
  795. break;
  796. case 12:
  797. {
  798. u_int32_t rd_high;
  799. u_int32_t rd_low;
  800. rd_high = stream_getl (s);
  801. rd_low = stream_getl (s);
  802. stream_get (&attr->mp_nexthop_global_in, s, 4);
  803. }
  804. break;
  805. #ifdef HAVE_IPV6
  806. case 16:
  807. stream_get (&attr->mp_nexthop_global, s, 16);
  808. break;
  809. case 32:
  810. stream_get (&attr->mp_nexthop_global, s, 16);
  811. stream_get (&attr->mp_nexthop_local, s, 16);
  812. if (! IN6_IS_ADDR_LINKLOCAL (&attr->mp_nexthop_local))
  813. {
  814. char buf1[INET6_ADDRSTRLEN];
  815. char buf2[INET6_ADDRSTRLEN];
  816. if (BGP_DEBUG (update, UPDATE_IN))
  817. zlog_debug ("%s got two nexthop %s %s but second one is not a link-local nexthop", peer->host,
  818. inet_ntop (AF_INET6, &attr->mp_nexthop_global,
  819. buf1, INET6_ADDRSTRLEN),
  820. inet_ntop (AF_INET6, &attr->mp_nexthop_local,
  821. buf2, INET6_ADDRSTRLEN));
  822. attr->mp_nexthop_len = 16;
  823. }
  824. break;
  825. #endif /* HAVE_IPV6 */
  826. default:
  827. zlog_info ("Wrong multiprotocol next hop length: %d",
  828. attr->mp_nexthop_len);
  829. return -1;
  830. break;
  831. }
  832. snpa_num = stream_getc (s);
  833. while (snpa_num--)
  834. {
  835. snpa_len = stream_getc (s);
  836. stream_forward_getp (s, (snpa_len + 1) >> 1);
  837. }
  838. nlri_len = lim - stream_pnt (s);
  839. if (safi != BGP_SAFI_VPNV4)
  840. {
  841. ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), nlri_len);
  842. if (ret < 0)
  843. return -1;
  844. }
  845. mp_update->afi = afi;
  846. mp_update->safi = safi;
  847. mp_update->nlri = stream_pnt (s);
  848. mp_update->length = nlri_len;
  849. stream_forward_getp (s, nlri_len);
  850. return 0;
  851. }
  852. /* Multiprotocol unreachable parse */
  853. static int
  854. bgp_mp_unreach_parse (struct peer *peer, int length,
  855. struct bgp_nlri *mp_withdraw)
  856. {
  857. struct stream *s;
  858. u_int16_t afi;
  859. u_char safi;
  860. u_char *lim;
  861. u_int16_t withdraw_len;
  862. int ret;
  863. s = peer->ibuf;
  864. lim = stream_pnt (s) + length;
  865. afi = stream_getw (s);
  866. safi = stream_getc (s);
  867. withdraw_len = lim - stream_pnt (s);
  868. if (safi != BGP_SAFI_VPNV4)
  869. {
  870. ret = bgp_nlri_sanity_check (peer, afi, stream_pnt (s), withdraw_len);
  871. if (ret < 0)
  872. return -1;
  873. }
  874. mp_withdraw->afi = afi;
  875. mp_withdraw->safi = safi;
  876. mp_withdraw->nlri = stream_pnt (s);
  877. mp_withdraw->length = withdraw_len;
  878. stream_forward_getp (s, withdraw_len);
  879. return 0;
  880. }
  881. /* Extended Community attribute. */
  882. static int
  883. bgp_attr_ext_communities (struct peer *peer, bgp_size_t length,
  884. struct attr *attr, u_char flag)
  885. {
  886. if (length == 0)
  887. attr->ecommunity = NULL;
  888. else
  889. {
  890. attr->ecommunity =
  891. ecommunity_parse ((u_int8_t *)stream_pnt (peer->ibuf), length);
  892. stream_forward_getp (peer->ibuf, length);
  893. }
  894. attr->flag |= ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES);
  895. return 0;
  896. }
  897. /* BGP unknown attribute treatment. */
  898. static int
  899. bgp_attr_unknown (struct peer *peer, struct attr *attr, u_char flag,
  900. u_char type, bgp_size_t length, u_char *startp)
  901. {
  902. bgp_size_t total;
  903. struct transit *transit;
  904. if (BGP_DEBUG (normal, NORMAL))
  905. zlog_debug ("%s Unknown attribute is received (type %d, length %d)",
  906. peer->host, type, length);
  907. if (BGP_DEBUG (events, EVENTS))
  908. zlog (peer->log, LOG_DEBUG,
  909. "Unknown attribute type %d length %d is received", type, length);
  910. /* Forward read pointer of input stream. */
  911. stream_forward_getp (peer->ibuf, length);
  912. /* Adjest total length to include type and length. */
  913. total = length + (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN) ? 4 : 3);
  914. /* If any of the mandatory well-known attributes are not recognized,
  915. then the Error Subcode is set to Unrecognized Well-known
  916. Attribute. The Data field contains the unrecognized attribute
  917. (type, length and value). */
  918. if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_OPTIONAL))
  919. {
  920. /* Adjust startp to do not include flag value. */
  921. bgp_notify_send_with_data (peer,
  922. BGP_NOTIFY_UPDATE_ERR,
  923. BGP_NOTIFY_UPDATE_UNREC_ATTR,
  924. startp, total);
  925. return -1;
  926. }
  927. /* Unrecognized non-transitive optional attributes must be quietly
  928. ignored and not passed along to other BGP peers. */
  929. if (! CHECK_FLAG (flag, BGP_ATTR_FLAG_TRANS))
  930. return 0;
  931. /* If a path with recognized transitive optional attribute is
  932. accepted and passed along to other BGP peers and the Partial bit
  933. in the Attribute Flags octet is set to 1 by some previous AS, it
  934. is not set back to 0 by the current AS. */
  935. SET_FLAG (*startp, BGP_ATTR_FLAG_PARTIAL);
  936. /* Store transitive attribute to the end of attr->transit. */
  937. if (! attr->transit)
  938. {
  939. attr->transit = XMALLOC (MTYPE_TRANSIT, sizeof (struct transit));
  940. memset (attr->transit, 0, sizeof (struct transit));
  941. }
  942. transit = attr->transit;
  943. if (transit->val)
  944. transit->val = XREALLOC (MTYPE_TRANSIT_VAL, transit->val,
  945. transit->length + total);
  946. else
  947. transit->val = XMALLOC (MTYPE_TRANSIT_VAL, total);
  948. memcpy (transit->val + transit->length, startp, total);
  949. transit->length += total;
  950. return 0;
  951. }
  952. /* Read attribute of update packet. This function is called from
  953. bgp_update() in bgpd.c. */
  954. int
  955. bgp_attr_parse (struct peer *peer, struct attr *attr, bgp_size_t size,
  956. struct bgp_nlri *mp_update, struct bgp_nlri *mp_withdraw)
  957. {
  958. int ret;
  959. u_char flag;
  960. u_char type;
  961. bgp_size_t length;
  962. u_char *startp, *endp;
  963. u_char *attr_endp;
  964. u_char seen[BGP_ATTR_BITMAP_SIZE];
  965. /* Initialize bitmap. */
  966. memset (seen, 0, BGP_ATTR_BITMAP_SIZE);
  967. /* End pointer of BGP attribute. */
  968. endp = BGP_INPUT_PNT (peer) + size;
  969. /* Get attributes to the end of attribute length. */
  970. while (BGP_INPUT_PNT (peer) < endp)
  971. {
  972. /* Check remaining length check.*/
  973. if (endp - BGP_INPUT_PNT (peer) < BGP_ATTR_MIN_LEN)
  974. {
  975. /* XXX warning: long int format, int arg (arg 5) */
  976. zlog (peer->log, LOG_WARNING,
  977. "%s error BGP attribute length %ld is smaller than min len",
  978. peer->host, endp - STREAM_PNT (BGP_INPUT (peer)));
  979. bgp_notify_send (peer,
  980. BGP_NOTIFY_UPDATE_ERR,
  981. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  982. return -1;
  983. }
  984. /* Fetch attribute flag and type. */
  985. startp = BGP_INPUT_PNT (peer);
  986. flag = stream_getc (BGP_INPUT (peer));
  987. type = stream_getc (BGP_INPUT (peer));
  988. /* Check extended attribue length bit. */
  989. if (CHECK_FLAG (flag, BGP_ATTR_FLAG_EXTLEN))
  990. length = stream_getw (BGP_INPUT (peer));
  991. else
  992. length = stream_getc (BGP_INPUT (peer));
  993. /* If any attribute appears more than once in the UPDATE
  994. message, then the Error Subcode is set to Malformed Attribute
  995. List. */
  996. if (CHECK_BITMAP (seen, type))
  997. {
  998. zlog (peer->log, LOG_WARNING,
  999. "%s error BGP attribute type %d appears twice in a message",
  1000. peer->host, type);
  1001. bgp_notify_send (peer,
  1002. BGP_NOTIFY_UPDATE_ERR,
  1003. BGP_NOTIFY_UPDATE_MAL_ATTR);
  1004. return -1;
  1005. }
  1006. /* Set type to bitmap to check duplicate attribute. `type' is
  1007. unsigned char so it never overflow bitmap range. */
  1008. SET_BITMAP (seen, type);
  1009. /* Overflow check. */
  1010. attr_endp = BGP_INPUT_PNT (peer) + length;
  1011. if (attr_endp > endp)
  1012. {
  1013. zlog (peer->log, LOG_WARNING,
  1014. "%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);
  1015. bgp_notify_send (peer,
  1016. BGP_NOTIFY_UPDATE_ERR,
  1017. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1018. return -1;
  1019. }
  1020. /* OK check attribute and store it's value. */
  1021. switch (type)
  1022. {
  1023. case BGP_ATTR_ORIGIN:
  1024. ret = bgp_attr_origin (peer, length, attr, flag, startp);
  1025. break;
  1026. case BGP_ATTR_AS_PATH:
  1027. ret = bgp_attr_aspath (peer, length, attr, flag, startp);
  1028. break;
  1029. case BGP_ATTR_NEXT_HOP:
  1030. ret = bgp_attr_nexthop (peer, length, attr, flag, startp);
  1031. break;
  1032. case BGP_ATTR_MULTI_EXIT_DISC:
  1033. ret = bgp_attr_med (peer, length, attr, flag, startp);
  1034. break;
  1035. case BGP_ATTR_LOCAL_PREF:
  1036. ret = bgp_attr_local_pref (peer, length, attr, flag);
  1037. break;
  1038. case BGP_ATTR_ATOMIC_AGGREGATE:
  1039. ret = bgp_attr_atomic (peer, length, attr, flag);
  1040. break;
  1041. case BGP_ATTR_AGGREGATOR:
  1042. ret = bgp_attr_aggregator (peer, length, attr, flag);
  1043. break;
  1044. case BGP_ATTR_COMMUNITIES:
  1045. ret = bgp_attr_community (peer, length, attr, flag);
  1046. break;
  1047. case BGP_ATTR_ORIGINATOR_ID:
  1048. ret = bgp_attr_originator_id (peer, length, attr, flag);
  1049. break;
  1050. case BGP_ATTR_CLUSTER_LIST:
  1051. ret = bgp_attr_cluster_list (peer, length, attr, flag);
  1052. break;
  1053. case BGP_ATTR_MP_REACH_NLRI:
  1054. ret = bgp_mp_reach_parse (peer, length, attr, mp_update);
  1055. break;
  1056. case BGP_ATTR_MP_UNREACH_NLRI:
  1057. ret = bgp_mp_unreach_parse (peer, length, mp_withdraw);
  1058. break;
  1059. case BGP_ATTR_EXT_COMMUNITIES:
  1060. ret = bgp_attr_ext_communities (peer, length, attr, flag);
  1061. break;
  1062. default:
  1063. ret = bgp_attr_unknown (peer, attr, flag, type, length, startp);
  1064. break;
  1065. }
  1066. /* If error occured immediately return to the caller. */
  1067. if (ret < 0)
  1068. return ret;
  1069. /* Check the fetched length. */
  1070. if (BGP_INPUT_PNT (peer) != attr_endp)
  1071. {
  1072. zlog (peer->log, LOG_WARNING,
  1073. "%s BGP attribute fetch error", peer->host);
  1074. bgp_notify_send (peer,
  1075. BGP_NOTIFY_UPDATE_ERR,
  1076. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1077. return -1;
  1078. }
  1079. }
  1080. /* Check final read pointer is same as end pointer. */
  1081. if (BGP_INPUT_PNT (peer) != endp)
  1082. {
  1083. zlog (peer->log, LOG_WARNING,
  1084. "%s BGP attribute length mismatch", peer->host);
  1085. bgp_notify_send (peer,
  1086. BGP_NOTIFY_UPDATE_ERR,
  1087. BGP_NOTIFY_UPDATE_ATTR_LENG_ERR);
  1088. return -1;
  1089. }
  1090. /* Finally intern unknown attribute. */
  1091. if (attr->transit)
  1092. attr->transit = transit_intern (attr->transit);
  1093. return 0;
  1094. }
  1095. /* Well-known attribute check. */
  1096. int
  1097. bgp_attr_check (struct peer *peer, struct attr *attr)
  1098. {
  1099. u_char type = 0;
  1100. if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_ORIGIN)))
  1101. type = BGP_ATTR_ORIGIN;
  1102. if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_AS_PATH)))
  1103. type = BGP_ATTR_AS_PATH;
  1104. if (! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP)))
  1105. type = BGP_ATTR_NEXT_HOP;
  1106. if (peer_sort (peer) == BGP_PEER_IBGP
  1107. && ! CHECK_FLAG (attr->flag, ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF)))
  1108. type = BGP_ATTR_LOCAL_PREF;
  1109. if (type)
  1110. {
  1111. zlog (peer->log, LOG_WARNING,
  1112. "%s Missing well-known attribute %d.",
  1113. peer->host, type);
  1114. bgp_notify_send_with_data (peer,
  1115. BGP_NOTIFY_UPDATE_ERR,
  1116. BGP_NOTIFY_UPDATE_MISS_ATTR,
  1117. &type, 1);
  1118. return -1;
  1119. }
  1120. return 0;
  1121. }
  1122. int stream_put_prefix (struct stream *, struct prefix *);
  1123. /* Make attribute packet. */
  1124. bgp_size_t
  1125. bgp_packet_attribute (struct bgp *bgp, struct peer *peer,
  1126. struct stream *s, struct attr *attr, struct prefix *p,
  1127. afi_t afi, safi_t safi, struct peer *from,
  1128. struct prefix_rd *prd, char *tag)
  1129. {
  1130. unsigned long cp;
  1131. struct aspath *aspath;
  1132. if (! bgp)
  1133. bgp = bgp_get_default ();
  1134. /* Remember current pointer. */
  1135. cp = stream_get_endp (s);
  1136. /* Origin attribute. */
  1137. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1138. stream_putc (s, BGP_ATTR_ORIGIN);
  1139. stream_putc (s, 1);
  1140. stream_putc (s, attr->origin);
  1141. /* AS path attribute. */
  1142. /* If remote-peer is EBGP */
  1143. if (peer_sort (peer) == BGP_PEER_EBGP
  1144. && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_AS_PATH_UNCHANGED)
  1145. || attr->aspath->length == 0)
  1146. && (! CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_RSERVER_CLIENT)))
  1147. {
  1148. aspath = aspath_dup (attr->aspath);
  1149. if (CHECK_FLAG(bgp->config, BGP_CONFIG_CONFEDERATION))
  1150. {
  1151. /* Strip the confed info, and then stuff our path CONFED_ID
  1152. on the front */
  1153. aspath = aspath_delete_confed_seq (aspath);
  1154. aspath = aspath_add_seq (aspath, bgp->confed_id);
  1155. }
  1156. else
  1157. {
  1158. aspath = aspath_add_seq (aspath, peer->local_as);
  1159. if (peer->change_local_as)
  1160. aspath = aspath_add_seq (aspath, peer->change_local_as);
  1161. }
  1162. }
  1163. else if (peer_sort (peer) == BGP_PEER_CONFED)
  1164. {
  1165. /* A confed member, so we need to do the AS_CONFED_SEQUENCE thing */
  1166. aspath = aspath_dup (attr->aspath);
  1167. aspath = aspath_add_confed_seq (aspath, peer->local_as);
  1168. }
  1169. else
  1170. aspath = attr->aspath;
  1171. /* AS path attribute extended length bit check. */
  1172. if (aspath->length > 255)
  1173. {
  1174. stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  1175. stream_putc (s, BGP_ATTR_AS_PATH);
  1176. stream_putw (s, aspath->length);
  1177. }
  1178. else
  1179. {
  1180. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1181. stream_putc(s, BGP_ATTR_AS_PATH);
  1182. stream_putc (s, aspath->length);
  1183. }
  1184. stream_put (s, aspath->data, aspath->length);
  1185. if (aspath != attr->aspath)
  1186. aspath_free (aspath);
  1187. /* Nexthop attribute. */
  1188. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_NEXT_HOP) && afi == AFI_IP)
  1189. {
  1190. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1191. stream_putc (s, BGP_ATTR_NEXT_HOP);
  1192. stream_putc (s, 4);
  1193. if (safi == SAFI_MPLS_VPN)
  1194. {
  1195. if (attr->nexthop.s_addr == 0)
  1196. stream_put_ipv4 (s, peer->nexthop.v4.s_addr);
  1197. else
  1198. stream_put_ipv4 (s, attr->nexthop.s_addr);
  1199. }
  1200. else
  1201. stream_put_ipv4 (s, attr->nexthop.s_addr);
  1202. }
  1203. /* MED attribute. */
  1204. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  1205. {
  1206. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1207. stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
  1208. stream_putc (s, 4);
  1209. stream_putl (s, attr->med);
  1210. }
  1211. /* Local preference. */
  1212. if (peer_sort (peer) == BGP_PEER_IBGP ||
  1213. peer_sort (peer) == BGP_PEER_CONFED)
  1214. {
  1215. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1216. stream_putc (s, BGP_ATTR_LOCAL_PREF);
  1217. stream_putc (s, 4);
  1218. stream_putl (s, attr->local_pref);
  1219. }
  1220. /* Atomic aggregate. */
  1221. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
  1222. {
  1223. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1224. stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
  1225. stream_putc (s, 0);
  1226. }
  1227. /* Aggregator. */
  1228. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
  1229. {
  1230. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  1231. stream_putc (s, BGP_ATTR_AGGREGATOR);
  1232. stream_putc (s, 6);
  1233. stream_putw (s, attr->aggregator_as);
  1234. stream_put_ipv4 (s, attr->aggregator_addr.s_addr);
  1235. }
  1236. /* Community attribute. */
  1237. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_COMMUNITY)
  1238. && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES)))
  1239. {
  1240. if (attr->community->size * 4 > 255)
  1241. {
  1242. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  1243. stream_putc (s, BGP_ATTR_COMMUNITIES);
  1244. stream_putw (s, attr->community->size * 4);
  1245. }
  1246. else
  1247. {
  1248. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  1249. stream_putc (s, BGP_ATTR_COMMUNITIES);
  1250. stream_putc (s, attr->community->size * 4);
  1251. }
  1252. stream_put (s, attr->community->val, attr->community->size * 4);
  1253. }
  1254. /* Route Reflector. */
  1255. if (peer_sort (peer) == BGP_PEER_IBGP
  1256. && from
  1257. && peer_sort (from) == BGP_PEER_IBGP)
  1258. {
  1259. /* Originator ID. */
  1260. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1261. stream_putc (s, BGP_ATTR_ORIGINATOR_ID);
  1262. stream_putc (s, 4);
  1263. if (attr->flag & ATTR_FLAG_BIT(BGP_ATTR_ORIGINATOR_ID))
  1264. stream_put_in_addr (s, &attr->originator_id);
  1265. else
  1266. {
  1267. if (from)
  1268. stream_put_in_addr (s, &from->remote_id);
  1269. else
  1270. stream_put_in_addr (s, &attr->originator_id);
  1271. }
  1272. /* Cluster list. */
  1273. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1274. stream_putc (s, BGP_ATTR_CLUSTER_LIST);
  1275. if (attr->cluster)
  1276. {
  1277. stream_putc (s, attr->cluster->length + 4);
  1278. /* If this peer configuration's parent BGP has cluster_id. */
  1279. if (bgp->config & BGP_CONFIG_CLUSTER_ID)
  1280. stream_put_in_addr (s, &bgp->cluster_id);
  1281. else
  1282. stream_put_in_addr (s, &bgp->router_id);
  1283. stream_put (s, attr->cluster->list, attr->cluster->length);
  1284. }
  1285. else
  1286. {
  1287. stream_putc (s, 4);
  1288. /* If this peer configuration's parent BGP has cluster_id. */
  1289. if (bgp->config & BGP_CONFIG_CLUSTER_ID)
  1290. stream_put_in_addr (s, &bgp->cluster_id);
  1291. else
  1292. stream_put_in_addr (s, &bgp->router_id);
  1293. }
  1294. }
  1295. #ifdef HAVE_IPV6
  1296. /* If p is IPv6 address put it into attribute. */
  1297. if (p->family == AF_INET6)
  1298. {
  1299. unsigned long sizep;
  1300. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1301. stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
  1302. sizep = stream_get_endp (s);
  1303. stream_putc (s, 0); /* Length of this attribute. */
  1304. stream_putw (s, AFI_IP6); /* AFI */
  1305. stream_putc (s, safi); /* SAFI */
  1306. stream_putc (s, attr->mp_nexthop_len);
  1307. if (attr->mp_nexthop_len == 16)
  1308. stream_put (s, &attr->mp_nexthop_global, 16);
  1309. else if (attr->mp_nexthop_len == 32)
  1310. {
  1311. stream_put (s, &attr->mp_nexthop_global, 16);
  1312. stream_put (s, &attr->mp_nexthop_local, 16);
  1313. }
  1314. /* SNPA */
  1315. stream_putc (s, 0);
  1316. /* Prefix write. */
  1317. stream_put_prefix (s, p);
  1318. /* Set MP attribute length. */
  1319. stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
  1320. }
  1321. #endif /* HAVE_IPV6 */
  1322. if (p->family == AF_INET && safi == SAFI_MULTICAST)
  1323. {
  1324. unsigned long sizep;
  1325. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1326. stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
  1327. sizep = stream_get_endp (s);
  1328. stream_putc (s, 0); /* Length of this attribute. */
  1329. stream_putw (s, AFI_IP); /* AFI */
  1330. stream_putc (s, SAFI_MULTICAST); /* SAFI */
  1331. stream_putc (s, 4);
  1332. stream_put_ipv4 (s, attr->nexthop.s_addr);
  1333. /* SNPA */
  1334. stream_putc (s, 0);
  1335. /* Prefix write. */
  1336. stream_put_prefix (s, p);
  1337. /* Set MP attribute length. */
  1338. stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
  1339. }
  1340. if (p->family == AF_INET && safi == SAFI_MPLS_VPN)
  1341. {
  1342. unsigned long sizep;
  1343. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1344. stream_putc (s, BGP_ATTR_MP_REACH_NLRI);
  1345. sizep = stream_get_endp (s);
  1346. stream_putc (s, 0); /* Length of this attribute. */
  1347. stream_putw (s, AFI_IP); /* AFI */
  1348. stream_putc (s, BGP_SAFI_VPNV4); /* SAFI */
  1349. stream_putc (s, 12);
  1350. stream_putl (s, 0);
  1351. stream_putl (s, 0);
  1352. stream_put (s, &attr->mp_nexthop_global_in, 4);
  1353. /* SNPA */
  1354. stream_putc (s, 0);
  1355. /* Tag, RD, Prefix write. */
  1356. stream_putc (s, p->prefixlen + 88);
  1357. stream_put (s, tag, 3);
  1358. stream_put (s, prd->val, 8);
  1359. stream_put (s, &p->u.prefix, PSIZE (p->prefixlen));
  1360. /* Set MP attribute length. */
  1361. stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
  1362. }
  1363. /* Extended Communities attribute. */
  1364. if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_SEND_EXT_COMMUNITY)
  1365. && (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_EXT_COMMUNITIES)))
  1366. {
  1367. if (peer_sort (peer) == BGP_PEER_IBGP || peer_sort (peer) == BGP_PEER_CONFED)
  1368. {
  1369. if (attr->ecommunity->size * 8 > 255)
  1370. {
  1371. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  1372. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  1373. stream_putw (s, attr->ecommunity->size * 8);
  1374. }
  1375. else
  1376. {
  1377. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  1378. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  1379. stream_putc (s, attr->ecommunity->size * 8);
  1380. }
  1381. stream_put (s, attr->ecommunity->val, attr->ecommunity->size * 8);
  1382. }
  1383. else
  1384. {
  1385. u_int8_t *pnt;
  1386. int tbit;
  1387. int ecom_tr_size = 0;
  1388. int i;
  1389. for (i = 0; i < attr->ecommunity->size; i++)
  1390. {
  1391. pnt = attr->ecommunity->val + (i * 8);
  1392. tbit = *pnt;
  1393. if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
  1394. continue;
  1395. ecom_tr_size++;
  1396. }
  1397. if (ecom_tr_size)
  1398. {
  1399. if (ecom_tr_size * 8 > 255)
  1400. {
  1401. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  1402. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  1403. stream_putw (s, ecom_tr_size * 8);
  1404. }
  1405. else
  1406. {
  1407. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  1408. stream_putc (s, BGP_ATTR_EXT_COMMUNITIES);
  1409. stream_putc (s, ecom_tr_size * 8);
  1410. }
  1411. for (i = 0; i < attr->ecommunity->size; i++)
  1412. {
  1413. pnt = attr->ecommunity->val + (i * 8);
  1414. tbit = *pnt;
  1415. if (CHECK_FLAG (tbit, ECOMMUNITY_FLAG_NON_TRANSITIVE))
  1416. continue;
  1417. stream_put (s, pnt, 8);
  1418. }
  1419. }
  1420. }
  1421. }
  1422. /* Unknown transit attribute. */
  1423. if (attr->transit)
  1424. stream_put (s, attr->transit->val, attr->transit->length);
  1425. /* Return total size of attribute. */
  1426. return stream_get_endp (s) - cp;
  1427. }
  1428. bgp_size_t
  1429. bgp_packet_withdraw (struct peer *peer, struct stream *s, struct prefix *p,
  1430. afi_t afi, safi_t safi, struct prefix_rd *prd,
  1431. char *tag)
  1432. {
  1433. unsigned long cp;
  1434. unsigned long attrlen_pnt;
  1435. bgp_size_t size;
  1436. cp = stream_get_endp (s);
  1437. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1438. stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
  1439. attrlen_pnt = stream_get_endp (s);
  1440. stream_putc (s, 0); /* Length of this attribute. */
  1441. stream_putw (s, family2afi (p->family));
  1442. if (safi == SAFI_MPLS_VPN)
  1443. {
  1444. /* SAFI */
  1445. stream_putc (s, BGP_SAFI_VPNV4);
  1446. /* prefix. */
  1447. stream_putc (s, p->prefixlen + 88);
  1448. stream_put (s, tag, 3);
  1449. stream_put (s, prd->val, 8);
  1450. stream_put (s, &p->u.prefix, PSIZE (p->prefixlen));
  1451. }
  1452. else
  1453. {
  1454. /* SAFI */
  1455. stream_putc (s, safi);
  1456. /* prefix */
  1457. stream_put_prefix (s, p);
  1458. }
  1459. /* Set MP attribute length. */
  1460. size = stream_get_endp (s) - attrlen_pnt - 1;
  1461. stream_putc_at (s, attrlen_pnt, size);
  1462. return stream_get_endp (s) - cp;
  1463. }
  1464. /* Initialization of attribute. */
  1465. void
  1466. bgp_attr_init ()
  1467. {
  1468. void attrhash_init ();
  1469. aspath_init ();
  1470. attrhash_init ();
  1471. community_init ();
  1472. ecommunity_init ();
  1473. cluster_init ();
  1474. transit_init ();
  1475. }
  1476. /* Make attribute packet. */
  1477. void
  1478. bgp_dump_routes_attr (struct stream *s, struct attr *attr,
  1479. struct prefix *prefix)
  1480. {
  1481. unsigned long cp;
  1482. unsigned long len;
  1483. struct aspath *aspath;
  1484. /* Remember current pointer. */
  1485. cp = stream_get_endp (s);
  1486. /* Place holder of length. */
  1487. stream_putw (s, 0);
  1488. /* Origin attribute. */
  1489. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1490. stream_putc (s, BGP_ATTR_ORIGIN);
  1491. stream_putc (s, 1);
  1492. stream_putc (s, attr->origin);
  1493. aspath = attr->aspath;
  1494. if (aspath->length > 255)
  1495. {
  1496. stream_putc (s, BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  1497. stream_putc (s, BGP_ATTR_AS_PATH);
  1498. stream_putw (s, aspath->length);
  1499. }
  1500. else
  1501. {
  1502. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1503. stream_putc (s, BGP_ATTR_AS_PATH);
  1504. stream_putc (s, aspath->length);
  1505. }
  1506. stream_put (s, aspath->data, aspath->length);
  1507. /* Nexthop attribute. */
  1508. /* If it's an IPv6 prefix, don't dump the IPv4 nexthop to save space */
  1509. if(prefix != NULL
  1510. #ifdef HAVE_IPV6
  1511. && prefix->family != AF_INET6
  1512. #endif /* HAVE_IPV6 */
  1513. )
  1514. {
  1515. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1516. stream_putc (s, BGP_ATTR_NEXT_HOP);
  1517. stream_putc (s, 4);
  1518. stream_put_ipv4 (s, attr->nexthop.s_addr);
  1519. }
  1520. /* MED attribute. */
  1521. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_MULTI_EXIT_DISC))
  1522. {
  1523. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  1524. stream_putc (s, BGP_ATTR_MULTI_EXIT_DISC);
  1525. stream_putc (s, 4);
  1526. stream_putl (s, attr->med);
  1527. }
  1528. /* Local preference. */
  1529. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_LOCAL_PREF))
  1530. {
  1531. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1532. stream_putc (s, BGP_ATTR_LOCAL_PREF);
  1533. stream_putc (s, 4);
  1534. stream_putl (s, attr->local_pref);
  1535. }
  1536. /* Atomic aggregate. */
  1537. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_ATOMIC_AGGREGATE))
  1538. {
  1539. stream_putc (s, BGP_ATTR_FLAG_TRANS);
  1540. stream_putc (s, BGP_ATTR_ATOMIC_AGGREGATE);
  1541. stream_putc (s, 0);
  1542. }
  1543. /* Aggregator. */
  1544. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_AGGREGATOR))
  1545. {
  1546. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  1547. stream_putc (s, BGP_ATTR_AGGREGATOR);
  1548. stream_putc (s, 6);
  1549. stream_putw (s, attr->aggregator_as);
  1550. stream_put_ipv4 (s, attr->aggregator_addr.s_addr);
  1551. }
  1552. /* Community attribute. */
  1553. if (attr->flag & ATTR_FLAG_BIT (BGP_ATTR_COMMUNITIES))
  1554. {
  1555. if (attr->community->size * 4 > 255)
  1556. {
  1557. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS|BGP_ATTR_FLAG_EXTLEN);
  1558. stream_putc (s, BGP_ATTR_COMMUNITIES);
  1559. stream_putw (s, attr->community->size * 4);
  1560. }
  1561. else
  1562. {
  1563. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL|BGP_ATTR_FLAG_TRANS);
  1564. stream_putc (s, BGP_ATTR_COMMUNITIES);
  1565. stream_putc (s, attr->community->size * 4);
  1566. }
  1567. stream_put (s, attr->community->val, attr->community->size * 4);
  1568. }
  1569. #ifdef HAVE_IPV6
  1570. /* Add a MP_NLRI attribute to dump the IPv6 next hop */
  1571. if(prefix != NULL && prefix->family == AF_INET6 &&
  1572. (attr->mp_nexthop_len == 16 || attr->mp_nexthop_len == 32) )
  1573. {
  1574. int sizep;
  1575. stream_putc(s, BGP_ATTR_FLAG_OPTIONAL);
  1576. stream_putc(s, BGP_ATTR_MP_REACH_NLRI);
  1577. sizep = stream_get_endp (s);
  1578. /* MP header */
  1579. stream_putc (s, 0); /* Length of this attribute. */
  1580. stream_putw(s, AFI_IP6); /* AFI */
  1581. stream_putc(s, SAFI_UNICAST); /* SAFI */
  1582. /* Next hop */
  1583. stream_putc(s, attr->mp_nexthop_len);
  1584. stream_put(s, &attr->mp_nexthop_global, 16);
  1585. if(attr->mp_nexthop_len == 32)
  1586. stream_put(s, &attr->mp_nexthop_local, 16);
  1587. /* SNPA */
  1588. stream_putc(s, 0);
  1589. /* Prefix */
  1590. stream_put_prefix(s, prefix);
  1591. /* Set MP attribute length. */
  1592. stream_putc_at (s, sizep, (stream_get_endp (s) - sizep) - 1);
  1593. }
  1594. #endif /* HAVE_IPV6 */
  1595. /* Return total size of attribute. */
  1596. len = stream_get_endp (s) - cp - 2;
  1597. stream_putw_at (s, cp, len);
  1598. }