bgp_attr.c 66 KB

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