bgp_attr.c 76 KB

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