bgp_packet.c 72 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639
  1. /* BGP packet management routine.
  2. Copyright (C) 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 "thread.h"
  18. #include "stream.h"
  19. #include "network.h"
  20. #include "prefix.h"
  21. #include "command.h"
  22. #include "log.h"
  23. #include "memory.h"
  24. #include "sockunion.h" /* for inet_ntop () */
  25. #include "linklist.h"
  26. #include "plist.h"
  27. #include "bgpd/bgpd.h"
  28. #include "bgpd/bgp_table.h"
  29. #include "bgpd/bgp_dump.h"
  30. #include "bgpd/bgp_attr.h"
  31. #include "bgpd/bgp_debug.h"
  32. #include "bgpd/bgp_fsm.h"
  33. #include "bgpd/bgp_route.h"
  34. #include "bgpd/bgp_packet.h"
  35. #include "bgpd/bgp_open.h"
  36. #include "bgpd/bgp_aspath.h"
  37. #include "bgpd/bgp_community.h"
  38. #include "bgpd/bgp_ecommunity.h"
  39. #include "bgpd/bgp_network.h"
  40. #include "bgpd/bgp_mplsvpn.h"
  41. #include "bgpd/bgp_advertise.h"
  42. #include "bgpd/bgp_vty.h"
  43. int stream_put_prefix (struct stream *, struct prefix *);
  44. /* Set up BGP packet marker and packet type. */
  45. static int
  46. bgp_packet_set_marker (struct stream *s, u_char type)
  47. {
  48. int i;
  49. /* Fill in marker. */
  50. for (i = 0; i < BGP_MARKER_SIZE; i++)
  51. stream_putc (s, 0xff);
  52. /* Dummy total length. This field is should be filled in later on. */
  53. stream_putw (s, 0);
  54. /* BGP packet type. */
  55. stream_putc (s, type);
  56. /* Return current stream size. */
  57. return stream_get_endp (s);
  58. }
  59. /* Set BGP packet header size entry. If size is zero then use current
  60. stream size. */
  61. static int
  62. bgp_packet_set_size (struct stream *s)
  63. {
  64. int cp;
  65. /* Preserve current pointer. */
  66. cp = stream_get_endp (s);
  67. stream_putw_at (s, BGP_MARKER_SIZE, cp);
  68. return cp;
  69. }
  70. /* Add new packet to the peer. */
  71. static void
  72. bgp_packet_add (struct peer *peer, struct stream *s)
  73. {
  74. /* Add packet to the end of list. */
  75. stream_fifo_push (peer->obuf, s);
  76. }
  77. /* Free first packet. */
  78. static void
  79. bgp_packet_delete (struct peer *peer)
  80. {
  81. stream_free (stream_fifo_pop (peer->obuf));
  82. }
  83. /* Check file descriptor whether connect is established. */
  84. static void
  85. bgp_connect_check (struct peer *peer)
  86. {
  87. int status;
  88. socklen_t slen;
  89. int ret;
  90. /* Anyway I have to reset read and write thread. */
  91. BGP_READ_OFF (peer->t_read);
  92. BGP_WRITE_OFF (peer->t_write);
  93. /* Check file descriptor. */
  94. slen = sizeof (status);
  95. ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen);
  96. /* If getsockopt is fail, this is fatal error. */
  97. if (ret < 0)
  98. {
  99. zlog (peer->log, LOG_INFO, "can't get sockopt for nonblocking connect");
  100. BGP_EVENT_ADD (peer, TCP_fatal_error);
  101. return;
  102. }
  103. /* When status is 0 then TCP connection is established. */
  104. if (status == 0)
  105. {
  106. BGP_EVENT_ADD (peer, TCP_connection_open);
  107. }
  108. else
  109. {
  110. if (BGP_DEBUG (events, EVENTS))
  111. plog_debug (peer->log, "%s [Event] Connect failed (%s)",
  112. peer->host, safe_strerror (errno));
  113. BGP_EVENT_ADD (peer, TCP_connection_open_failed);
  114. }
  115. }
  116. /* Make BGP update packet. */
  117. static struct stream *
  118. bgp_update_packet (struct peer *peer, afi_t afi, safi_t safi)
  119. {
  120. struct stream *s;
  121. struct stream *snlri;
  122. struct bgp_adj_out *adj;
  123. struct bgp_advertise *adv;
  124. struct stream *packet;
  125. struct bgp_node *rn = NULL;
  126. struct bgp_info *binfo = NULL;
  127. bgp_size_t total_attr_len = 0;
  128. unsigned long attrlen_pos = 0;
  129. size_t mpattrlen_pos = 0;
  130. size_t mpattr_pos = 0;
  131. s = peer->work;
  132. stream_reset (s);
  133. snlri = peer->scratch;
  134. stream_reset (snlri);
  135. adv = FIFO_HEAD (&peer->sync[afi][safi]->update);
  136. while (adv)
  137. {
  138. assert (adv->rn);
  139. rn = adv->rn;
  140. adj = adv->adj;
  141. if (adv->binfo)
  142. binfo = adv->binfo;
  143. /* When remaining space can't include NLRI and it's length. */
  144. if (STREAM_CONCAT_REMAIN (s, snlri, STREAM_SIZE(s)) <=
  145. (BGP_NLRI_LENGTH + PSIZE (rn->p.prefixlen)))
  146. break;
  147. /* If packet is empty, set attribute. */
  148. if (stream_empty (s))
  149. {
  150. struct peer *from = NULL;
  151. if (binfo)
  152. from = binfo->peer;
  153. /* 1: Write the BGP message header - 16 bytes marker, 2 bytes length,
  154. * one byte message type.
  155. */
  156. bgp_packet_set_marker (s, BGP_MSG_UPDATE);
  157. /* 2: withdrawn routes length */
  158. stream_putw (s, 0);
  159. /* 3: total attributes length - attrlen_pos stores the position */
  160. attrlen_pos = stream_get_endp (s);
  161. stream_putw (s, 0);
  162. /* 4: if there is MP_REACH_NLRI attribute, that should be the first
  163. * attribute, according to draft-ietf-idr-error-handling. Save the
  164. * position.
  165. */
  166. mpattr_pos = stream_get_endp(s);
  167. /* 5: Encode all the attributes, except MP_REACH_NLRI attr. */
  168. total_attr_len = bgp_packet_attribute (NULL, peer, s,
  169. adv->baa->attr,
  170. NULL, afi, safi,
  171. from, NULL, NULL);
  172. }
  173. if (afi == AFI_IP && safi == SAFI_UNICAST)
  174. stream_put_prefix (s, &rn->p);
  175. else
  176. {
  177. /* Encode the prefix in MP_REACH_NLRI attribute */
  178. struct prefix_rd *prd = NULL;
  179. u_char *tag = NULL;
  180. if (rn->prn)
  181. prd = (struct prefix_rd *) &rn->prn->p;
  182. if (binfo && binfo->extra)
  183. tag = binfo->extra->tag;
  184. if (stream_empty(snlri))
  185. mpattrlen_pos = bgp_packet_mpattr_start(snlri, afi, safi,
  186. adv->baa->attr);
  187. bgp_packet_mpattr_prefix(snlri, afi, safi, &rn->p, prd, tag);
  188. }
  189. if (BGP_DEBUG (update, UPDATE_OUT))
  190. {
  191. char buf[INET6_BUFSIZ];
  192. zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d",
  193. peer->host,
  194. inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
  195. rn->p.prefixlen);
  196. }
  197. /* Synchnorize attribute. */
  198. if (adj->attr)
  199. bgp_attr_unintern (&adj->attr);
  200. else
  201. peer->scount[afi][safi]++;
  202. adj->attr = bgp_attr_intern (adv->baa->attr);
  203. adv = bgp_advertise_clean (peer, adj, afi, safi);
  204. }
  205. if (! stream_empty (s))
  206. {
  207. if (!stream_empty(snlri))
  208. {
  209. bgp_packet_mpattr_end(snlri, mpattrlen_pos);
  210. total_attr_len += stream_get_endp(snlri);
  211. }
  212. /* set the total attribute length correctly */
  213. stream_putw_at (s, attrlen_pos, total_attr_len);
  214. if (!stream_empty(snlri))
  215. packet = stream_dupcat(s, snlri, mpattr_pos);
  216. else
  217. packet = stream_dup (s);
  218. bgp_packet_set_size (packet);
  219. bgp_packet_add (peer, packet);
  220. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  221. stream_reset (s);
  222. stream_reset (snlri);
  223. return packet;
  224. }
  225. return NULL;
  226. }
  227. static struct stream *
  228. bgp_update_packet_eor (struct peer *peer, afi_t afi, safi_t safi)
  229. {
  230. struct stream *s;
  231. struct stream *packet;
  232. if (DISABLE_BGP_ANNOUNCE)
  233. return NULL;
  234. if (BGP_DEBUG (normal, NORMAL))
  235. zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi, safi), peer->host);
  236. s = stream_new (BGP_MAX_PACKET_SIZE);
  237. /* Make BGP update packet. */
  238. bgp_packet_set_marker (s, BGP_MSG_UPDATE);
  239. /* Unfeasible Routes Length */
  240. stream_putw (s, 0);
  241. if (afi == AFI_IP && safi == SAFI_UNICAST)
  242. {
  243. /* Total Path Attribute Length */
  244. stream_putw (s, 0);
  245. }
  246. else
  247. {
  248. /* Total Path Attribute Length */
  249. stream_putw (s, 6);
  250. stream_putc (s, BGP_ATTR_FLAG_OPTIONAL);
  251. stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI);
  252. stream_putc (s, 3);
  253. stream_putw (s, afi);
  254. stream_putc (s, safi);
  255. }
  256. bgp_packet_set_size (s);
  257. packet = stream_dup (s);
  258. bgp_packet_add (peer, packet);
  259. stream_free (s);
  260. return packet;
  261. }
  262. /* Make BGP withdraw packet. */
  263. /* For ipv4 unicast:
  264. 16-octet marker | 2-octet length | 1-octet type |
  265. 2-octet withdrawn route length | withdrawn prefixes | 2-octet attrlen (=0)
  266. */
  267. /* For other afi/safis:
  268. 16-octet marker | 2-octet length | 1-octet type |
  269. 2-octet withdrawn route length (=0) | 2-octet attrlen |
  270. mp_unreach attr type | attr len | afi | safi | withdrawn prefixes
  271. */
  272. static struct stream *
  273. bgp_withdraw_packet (struct peer *peer, afi_t afi, safi_t safi)
  274. {
  275. struct stream *s;
  276. struct stream *packet;
  277. struct bgp_adj_out *adj;
  278. struct bgp_advertise *adv;
  279. struct bgp_node *rn;
  280. unsigned long pos;
  281. bgp_size_t unfeasible_len;
  282. bgp_size_t total_attr_len;
  283. size_t mp_start = 0;
  284. size_t attrlen_pos = 0;
  285. size_t mplen_pos = 0;
  286. u_char first_time = 1;
  287. s = peer->work;
  288. stream_reset (s);
  289. while ((adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw)) != NULL)
  290. {
  291. assert (adv->rn);
  292. adj = adv->adj;
  293. rn = adv->rn;
  294. if (STREAM_REMAIN (s)
  295. < (BGP_NLRI_LENGTH + BGP_TOTAL_ATTR_LEN + PSIZE (rn->p.prefixlen)))
  296. break;
  297. if (stream_empty (s))
  298. {
  299. bgp_packet_set_marker (s, BGP_MSG_UPDATE);
  300. stream_putw (s, 0); /* unfeasible routes length */
  301. }
  302. else
  303. first_time = 0;
  304. if (afi == AFI_IP && safi == SAFI_UNICAST)
  305. stream_put_prefix (s, &rn->p);
  306. else
  307. {
  308. struct prefix_rd *prd = NULL;
  309. if (rn->prn)
  310. prd = (struct prefix_rd *) &rn->prn->p;
  311. /* If first time, format the MP_UNREACH header */
  312. if (first_time)
  313. {
  314. attrlen_pos = stream_get_endp (s);
  315. /* total attr length = 0 for now. reevaluate later */
  316. stream_putw (s, 0);
  317. mp_start = stream_get_endp (s);
  318. mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
  319. }
  320. bgp_packet_mpunreach_prefix(s, &rn->p, afi, safi, prd, NULL);
  321. }
  322. if (BGP_DEBUG (update, UPDATE_OUT))
  323. {
  324. char buf[INET6_BUFSIZ];
  325. zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
  326. peer->host,
  327. inet_ntop (rn->p.family, &(rn->p.u.prefix), buf, INET6_BUFSIZ),
  328. rn->p.prefixlen);
  329. }
  330. peer->scount[afi][safi]--;
  331. bgp_adj_out_remove (rn, adj, peer, afi, safi);
  332. bgp_unlock_node (rn);
  333. }
  334. if (! stream_empty (s))
  335. {
  336. if (afi == AFI_IP && safi == SAFI_UNICAST)
  337. {
  338. unfeasible_len
  339. = stream_get_endp (s) - BGP_HEADER_SIZE - BGP_UNFEASIBLE_LEN;
  340. stream_putw_at (s, BGP_HEADER_SIZE, unfeasible_len);
  341. stream_putw (s, 0);
  342. }
  343. else
  344. {
  345. /* Set the mp_unreach attr's length */
  346. bgp_packet_mpunreach_end(s, mplen_pos);
  347. /* Set total path attribute length. */
  348. total_attr_len = stream_get_endp(s) - mp_start;
  349. stream_putw_at (s, attrlen_pos, total_attr_len);
  350. }
  351. bgp_packet_set_size (s);
  352. packet = stream_dup (s);
  353. bgp_packet_add (peer, packet);
  354. stream_reset (s);
  355. return packet;
  356. }
  357. return NULL;
  358. }
  359. void
  360. bgp_default_update_send (struct peer *peer, struct attr *attr,
  361. afi_t afi, safi_t safi, struct peer *from)
  362. {
  363. struct stream *s;
  364. struct stream *packet;
  365. struct prefix p;
  366. unsigned long pos;
  367. bgp_size_t total_attr_len;
  368. if (DISABLE_BGP_ANNOUNCE)
  369. return;
  370. if (afi == AFI_IP)
  371. str2prefix ("0.0.0.0/0", &p);
  372. #ifdef HAVE_IPV6
  373. else
  374. str2prefix ("::/0", &p);
  375. #endif /* HAVE_IPV6 */
  376. /* Logging the attribute. */
  377. if (BGP_DEBUG (update, UPDATE_OUT))
  378. {
  379. char attrstr[BUFSIZ];
  380. char buf[INET6_BUFSIZ];
  381. attrstr[0] = '\0';
  382. bgp_dump_attr (peer, attr, attrstr, BUFSIZ);
  383. zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d %s",
  384. peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
  385. p.prefixlen, attrstr);
  386. }
  387. s = stream_new (BGP_MAX_PACKET_SIZE);
  388. /* Make BGP update packet. */
  389. bgp_packet_set_marker (s, BGP_MSG_UPDATE);
  390. /* Unfeasible Routes Length. */
  391. stream_putw (s, 0);
  392. /* Make place for total attribute length. */
  393. pos = stream_get_endp (s);
  394. stream_putw (s, 0);
  395. total_attr_len = bgp_packet_attribute (NULL, peer, s, attr, &p, afi, safi, from, NULL, NULL);
  396. /* Set Total Path Attribute Length. */
  397. stream_putw_at (s, pos, total_attr_len);
  398. /* NLRI set. */
  399. if (p.family == AF_INET && safi == SAFI_UNICAST)
  400. stream_put_prefix (s, &p);
  401. /* Set size. */
  402. bgp_packet_set_size (s);
  403. packet = stream_dup (s);
  404. stream_free (s);
  405. /* Dump packet if debug option is set. */
  406. #ifdef DEBUG
  407. /* bgp_packet_dump (packet); */
  408. #endif /* DEBUG */
  409. /* Add packet to the peer. */
  410. bgp_packet_add (peer, packet);
  411. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  412. }
  413. void
  414. bgp_default_withdraw_send (struct peer *peer, afi_t afi, safi_t safi)
  415. {
  416. struct stream *s;
  417. struct stream *packet;
  418. struct prefix p;
  419. unsigned long attrlen_pos = 0;
  420. unsigned long cp;
  421. bgp_size_t unfeasible_len;
  422. bgp_size_t total_attr_len;
  423. size_t mp_start = 0;
  424. size_t mplen_pos = 0;
  425. if (DISABLE_BGP_ANNOUNCE)
  426. return;
  427. if (afi == AFI_IP)
  428. str2prefix ("0.0.0.0/0", &p);
  429. #ifdef HAVE_IPV6
  430. else
  431. str2prefix ("::/0", &p);
  432. #endif /* HAVE_IPV6 */
  433. total_attr_len = 0;
  434. if (BGP_DEBUG (update, UPDATE_OUT))
  435. {
  436. char buf[INET6_BUFSIZ];
  437. zlog (peer->log, LOG_DEBUG, "%s send UPDATE %s/%d -- unreachable",
  438. peer->host, inet_ntop(p.family, &(p.u.prefix), buf, INET6_BUFSIZ),
  439. p.prefixlen);
  440. }
  441. s = stream_new (BGP_MAX_PACKET_SIZE);
  442. /* Make BGP update packet. */
  443. bgp_packet_set_marker (s, BGP_MSG_UPDATE);
  444. /* Unfeasible Routes Length. */;
  445. cp = stream_get_endp (s);
  446. stream_putw (s, 0);
  447. /* Withdrawn Routes. */
  448. if (p.family == AF_INET && safi == SAFI_UNICAST)
  449. {
  450. stream_put_prefix (s, &p);
  451. unfeasible_len = stream_get_endp (s) - cp - 2;
  452. /* Set unfeasible len. */
  453. stream_putw_at (s, cp, unfeasible_len);
  454. /* Set total path attribute length. */
  455. stream_putw (s, 0);
  456. }
  457. else
  458. {
  459. attrlen_pos = stream_get_endp (s);
  460. stream_putw (s, 0);
  461. mp_start = stream_get_endp (s);
  462. mplen_pos = bgp_packet_mpunreach_start(s, afi, safi);
  463. bgp_packet_mpunreach_prefix(s, &p, afi, safi, NULL, NULL);
  464. /* Set the mp_unreach attr's length */
  465. bgp_packet_mpunreach_end(s, mplen_pos);
  466. /* Set total path attribute length. */
  467. total_attr_len = stream_get_endp(s) - mp_start;
  468. stream_putw_at (s, attrlen_pos, total_attr_len);
  469. }
  470. bgp_packet_set_size (s);
  471. packet = stream_dup (s);
  472. stream_free (s);
  473. /* Add packet to the peer. */
  474. bgp_packet_add (peer, packet);
  475. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  476. }
  477. /* Get next packet to be written. */
  478. static struct stream *
  479. bgp_write_packet (struct peer *peer)
  480. {
  481. afi_t afi;
  482. safi_t safi;
  483. struct stream *s = NULL;
  484. struct bgp_advertise *adv;
  485. s = stream_fifo_head (peer->obuf);
  486. if (s)
  487. return s;
  488. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  489. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  490. {
  491. adv = FIFO_HEAD (&peer->sync[afi][safi]->withdraw);
  492. if (adv)
  493. {
  494. s = bgp_withdraw_packet (peer, afi, safi);
  495. if (s)
  496. return s;
  497. }
  498. }
  499. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  500. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  501. {
  502. adv = FIFO_HEAD (&peer->sync[afi][safi]->update);
  503. if (adv)
  504. {
  505. if (adv->binfo && adv->binfo->uptime < peer->synctime)
  506. {
  507. if (CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_RCV)
  508. && CHECK_FLAG (adv->binfo->peer->cap, PEER_CAP_RESTART_ADV)
  509. && ! CHECK_FLAG (adv->binfo->flags, BGP_INFO_STALE)
  510. && safi != SAFI_MPLS_VPN)
  511. {
  512. if (CHECK_FLAG (adv->binfo->peer->af_sflags[afi][safi],
  513. PEER_STATUS_EOR_RECEIVED))
  514. s = bgp_update_packet (peer, afi, safi);
  515. }
  516. else
  517. s = bgp_update_packet (peer, afi, safi);
  518. }
  519. if (s)
  520. return s;
  521. }
  522. if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV))
  523. {
  524. if (peer->afc_nego[afi][safi] && peer->synctime
  525. && ! CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND)
  526. && safi != SAFI_MPLS_VPN)
  527. {
  528. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_SEND);
  529. return bgp_update_packet_eor (peer, afi, safi);
  530. }
  531. }
  532. }
  533. return NULL;
  534. }
  535. /* Is there partially written packet or updates we can send right
  536. now. */
  537. static int
  538. bgp_write_proceed (struct peer *peer)
  539. {
  540. afi_t afi;
  541. safi_t safi;
  542. struct bgp_advertise *adv;
  543. if (stream_fifo_head (peer->obuf))
  544. return 1;
  545. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  546. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  547. if (FIFO_HEAD (&peer->sync[afi][safi]->withdraw))
  548. return 1;
  549. for (afi = AFI_IP; afi < AFI_MAX; afi++)
  550. for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++)
  551. if ((adv = FIFO_HEAD (&peer->sync[afi][safi]->update)) != NULL)
  552. if (adv->binfo->uptime < peer->synctime)
  553. return 1;
  554. return 0;
  555. }
  556. /* Write packet to the peer. */
  557. int
  558. bgp_write (struct thread *thread)
  559. {
  560. struct peer *peer;
  561. u_char type;
  562. struct stream *s;
  563. int num;
  564. unsigned int count = 0;
  565. /* Yes first of all get peer pointer. */
  566. peer = THREAD_ARG (thread);
  567. peer->t_write = NULL;
  568. /* For non-blocking IO check. */
  569. if (peer->status == Connect)
  570. {
  571. bgp_connect_check (peer);
  572. return 0;
  573. }
  574. s = bgp_write_packet (peer);
  575. if (!s)
  576. return 0; /* nothing to send */
  577. sockopt_cork (peer->fd, 1);
  578. /* Nonblocking write until TCP output buffer is full. */
  579. do
  580. {
  581. int writenum;
  582. /* Number of bytes to be sent. */
  583. writenum = stream_get_endp (s) - stream_get_getp (s);
  584. /* Call write() system call. */
  585. num = write (peer->fd, STREAM_PNT (s), writenum);
  586. if (num < 0)
  587. {
  588. /* write failed either retry needed or error */
  589. if (ERRNO_IO_RETRY(errno))
  590. break;
  591. BGP_EVENT_ADD (peer, TCP_fatal_error);
  592. return 0;
  593. }
  594. if (num != writenum)
  595. {
  596. /* Partial write */
  597. stream_forward_getp (s, num);
  598. break;
  599. }
  600. /* Retrieve BGP packet type. */
  601. stream_set_getp (s, BGP_MARKER_SIZE + 2);
  602. type = stream_getc (s);
  603. switch (type)
  604. {
  605. case BGP_MSG_OPEN:
  606. peer->open_out++;
  607. break;
  608. case BGP_MSG_UPDATE:
  609. peer->update_out++;
  610. break;
  611. case BGP_MSG_NOTIFY:
  612. peer->notify_out++;
  613. /* Double start timer. */
  614. peer->v_start *= 2;
  615. /* Overflow check. */
  616. if (peer->v_start >= (60 * 2))
  617. peer->v_start = (60 * 2);
  618. /* Flush any existing events */
  619. BGP_EVENT_ADD (peer, BGP_Stop);
  620. goto done;
  621. case BGP_MSG_KEEPALIVE:
  622. peer->keepalive_out++;
  623. break;
  624. case BGP_MSG_ROUTE_REFRESH_NEW:
  625. case BGP_MSG_ROUTE_REFRESH_OLD:
  626. peer->refresh_out++;
  627. break;
  628. case BGP_MSG_CAPABILITY:
  629. peer->dynamic_cap_out++;
  630. break;
  631. }
  632. /* OK we send packet so delete it. */
  633. bgp_packet_delete (peer);
  634. }
  635. while (++count < BGP_WRITE_PACKET_MAX &&
  636. (s = bgp_write_packet (peer)) != NULL);
  637. if (bgp_write_proceed (peer))
  638. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  639. done:
  640. sockopt_cork (peer->fd, 0);
  641. return 0;
  642. }
  643. /* This is only for sending NOTIFICATION message to neighbor. */
  644. static int
  645. bgp_write_notify (struct peer *peer)
  646. {
  647. int ret, val;
  648. u_char type;
  649. struct stream *s;
  650. /* There should be at least one packet. */
  651. s = stream_fifo_head (peer->obuf);
  652. if (!s)
  653. return 0;
  654. assert (stream_get_endp (s) >= BGP_HEADER_SIZE);
  655. /* Stop collecting data within the socket */
  656. sockopt_cork (peer->fd, 0);
  657. /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well,
  658. * we only care about getting a clean shutdown at this point. */
  659. ret = write (peer->fd, STREAM_DATA (s), stream_get_endp (s));
  660. /* only connection reset/close gets counted as TCP_fatal_error, failure
  661. * to write the entire NOTIFY doesn't get different FSM treatment */
  662. if (ret <= 0)
  663. {
  664. BGP_EVENT_ADD (peer, TCP_fatal_error);
  665. return 0;
  666. }
  667. /* Disable Nagle, make NOTIFY packet go out right away */
  668. val = 1;
  669. (void) setsockopt (peer->fd, IPPROTO_TCP, TCP_NODELAY,
  670. (char *) &val, sizeof (val));
  671. /* Retrieve BGP packet type. */
  672. stream_set_getp (s, BGP_MARKER_SIZE + 2);
  673. type = stream_getc (s);
  674. assert (type == BGP_MSG_NOTIFY);
  675. /* Type should be notify. */
  676. peer->notify_out++;
  677. /* Double start timer. */
  678. peer->v_start *= 2;
  679. /* Overflow check. */
  680. if (peer->v_start >= (60 * 2))
  681. peer->v_start = (60 * 2);
  682. BGP_EVENT_ADD (peer, BGP_Stop);
  683. return 0;
  684. }
  685. /* Make keepalive packet and send it to the peer. */
  686. void
  687. bgp_keepalive_send (struct peer *peer)
  688. {
  689. struct stream *s;
  690. int length;
  691. s = stream_new (BGP_MAX_PACKET_SIZE);
  692. /* Make keepalive packet. */
  693. bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE);
  694. /* Set packet size. */
  695. length = bgp_packet_set_size (s);
  696. /* Dump packet if debug option is set. */
  697. /* bgp_packet_dump (s); */
  698. if (BGP_DEBUG (keepalive, KEEPALIVE))
  699. zlog_debug ("%s sending KEEPALIVE", peer->host);
  700. if (BGP_DEBUG (normal, NORMAL))
  701. zlog_debug ("%s send message type %d, length (incl. header) %d",
  702. peer->host, BGP_MSG_KEEPALIVE, length);
  703. /* Add packet to the peer. */
  704. bgp_packet_add (peer, s);
  705. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  706. }
  707. /* Make open packet and send it to the peer. */
  708. void
  709. bgp_open_send (struct peer *peer)
  710. {
  711. struct stream *s;
  712. int length;
  713. u_int16_t send_holdtime;
  714. as_t local_as;
  715. if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
  716. send_holdtime = peer->holdtime;
  717. else
  718. send_holdtime = peer->bgp->default_holdtime;
  719. /* local-as Change */
  720. if (peer->change_local_as)
  721. local_as = peer->change_local_as;
  722. else
  723. local_as = peer->local_as;
  724. s = stream_new (BGP_MAX_PACKET_SIZE);
  725. /* Make open packet. */
  726. bgp_packet_set_marker (s, BGP_MSG_OPEN);
  727. /* Set open packet values. */
  728. stream_putc (s, BGP_VERSION_4); /* BGP version */
  729. stream_putw (s, (local_as <= BGP_AS_MAX) ? (u_int16_t) local_as
  730. : BGP_AS_TRANS);
  731. stream_putw (s, send_holdtime); /* Hold Time */
  732. stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */
  733. /* Set capability code. */
  734. bgp_open_capability (s, peer);
  735. /* Set BGP packet length. */
  736. length = bgp_packet_set_size (s);
  737. if (BGP_DEBUG (normal, NORMAL))
  738. zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s",
  739. peer->host, BGP_VERSION_4, local_as,
  740. send_holdtime, inet_ntoa (peer->local_id));
  741. if (BGP_DEBUG (normal, NORMAL))
  742. zlog_debug ("%s send message type %d, length (incl. header) %d",
  743. peer->host, BGP_MSG_OPEN, length);
  744. /* Dump packet if debug option is set. */
  745. /* bgp_packet_dump (s); */
  746. /* Add packet to the peer. */
  747. bgp_packet_add (peer, s);
  748. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  749. }
  750. /* Send BGP notify packet with data potion. */
  751. void
  752. bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code,
  753. u_char *data, size_t datalen)
  754. {
  755. struct stream *s;
  756. int length;
  757. /* Allocate new stream. */
  758. s = stream_new (BGP_MAX_PACKET_SIZE);
  759. /* Make nitify packet. */
  760. bgp_packet_set_marker (s, BGP_MSG_NOTIFY);
  761. /* Set notify packet values. */
  762. stream_putc (s, code); /* BGP notify code */
  763. stream_putc (s, sub_code); /* BGP notify sub_code */
  764. /* If notify data is present. */
  765. if (data)
  766. stream_write (s, data, datalen);
  767. /* Set BGP packet length. */
  768. length = bgp_packet_set_size (s);
  769. /* Add packet to the peer. */
  770. stream_fifo_clean (peer->obuf);
  771. bgp_packet_add (peer, s);
  772. /* For debug */
  773. {
  774. struct bgp_notify bgp_notify;
  775. int first = 0;
  776. int i;
  777. char c[4];
  778. bgp_notify.code = code;
  779. bgp_notify.subcode = sub_code;
  780. bgp_notify.data = NULL;
  781. bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE;
  782. if (bgp_notify.length)
  783. {
  784. bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
  785. for (i = 0; i < bgp_notify.length; i++)
  786. if (first)
  787. {
  788. sprintf (c, " %02x", data[i]);
  789. strcat (bgp_notify.data, c);
  790. }
  791. else
  792. {
  793. first = 1;
  794. sprintf (c, "%02x", data[i]);
  795. strcpy (bgp_notify.data, c);
  796. }
  797. }
  798. bgp_notify_print (peer, &bgp_notify, "sending");
  799. if (bgp_notify.data)
  800. XFREE (MTYPE_TMP, bgp_notify.data);
  801. }
  802. if (BGP_DEBUG (normal, NORMAL))
  803. zlog_debug ("%s send message type %d, length (incl. header) %d",
  804. peer->host, BGP_MSG_NOTIFY, length);
  805. /* peer reset cause */
  806. if (sub_code != BGP_NOTIFY_CEASE_CONFIG_CHANGE)
  807. {
  808. if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET)
  809. {
  810. peer->last_reset = PEER_DOWN_USER_RESET;
  811. zlog_info ("Notification sent to neighbor %s: User reset", peer->host);
  812. }
  813. else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN)
  814. {
  815. peer->last_reset = PEER_DOWN_USER_SHUTDOWN;
  816. zlog_info ("Notification sent to neighbor %s: shutdown", peer->host);
  817. }
  818. else
  819. {
  820. peer->last_reset = PEER_DOWN_NOTIFY_SEND;
  821. zlog_info ("Notification sent to neighbor %s: type %u/%u",
  822. peer->host, code, sub_code);
  823. }
  824. }
  825. else
  826. zlog_info ("Notification sent to neighbor %s: configuration change",
  827. peer->host);
  828. /* Call immediately. */
  829. BGP_WRITE_OFF (peer->t_write);
  830. bgp_write_notify (peer);
  831. }
  832. /* Send BGP notify packet. */
  833. void
  834. bgp_notify_send (struct peer *peer, u_char code, u_char sub_code)
  835. {
  836. bgp_notify_send_with_data (peer, code, sub_code, NULL, 0);
  837. }
  838. /* Send route refresh message to the peer. */
  839. void
  840. bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi,
  841. u_char orf_type, u_char when_to_refresh, int remove)
  842. {
  843. struct stream *s;
  844. struct stream *packet;
  845. int length;
  846. struct bgp_filter *filter;
  847. int orf_refresh = 0;
  848. if (DISABLE_BGP_ANNOUNCE)
  849. return;
  850. filter = &peer->filter[afi][safi];
  851. /* Adjust safi code. */
  852. if (safi == SAFI_MPLS_VPN)
  853. safi = SAFI_MPLS_LABELED_VPN;
  854. s = stream_new (BGP_MAX_PACKET_SIZE);
  855. /* Make BGP update packet. */
  856. if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV))
  857. bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW);
  858. else
  859. bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD);
  860. /* Encode Route Refresh message. */
  861. stream_putw (s, afi);
  862. stream_putc (s, 0);
  863. stream_putc (s, safi);
  864. if (orf_type == ORF_TYPE_PREFIX
  865. || orf_type == ORF_TYPE_PREFIX_OLD)
  866. if (remove || filter->plist[FILTER_IN].plist)
  867. {
  868. u_int16_t orf_len;
  869. unsigned long orfp;
  870. orf_refresh = 1;
  871. stream_putc (s, when_to_refresh);
  872. stream_putc (s, orf_type);
  873. orfp = stream_get_endp (s);
  874. stream_putw (s, 0);
  875. if (remove)
  876. {
  877. UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
  878. stream_putc (s, ORF_COMMON_PART_REMOVE_ALL);
  879. if (BGP_DEBUG (normal, NORMAL))
  880. zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d",
  881. peer->host, orf_type,
  882. (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
  883. afi, safi);
  884. }
  885. else
  886. {
  887. SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND);
  888. prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist,
  889. ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT,
  890. ORF_COMMON_PART_DENY);
  891. if (BGP_DEBUG (normal, NORMAL))
  892. zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d",
  893. peer->host, orf_type,
  894. (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"),
  895. afi, safi);
  896. }
  897. /* Total ORF Entry Len. */
  898. orf_len = stream_get_endp (s) - orfp - 2;
  899. stream_putw_at (s, orfp, orf_len);
  900. }
  901. /* Set packet size. */
  902. length = bgp_packet_set_size (s);
  903. if (BGP_DEBUG (normal, NORMAL))
  904. {
  905. if (! orf_refresh)
  906. zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d",
  907. peer->host, afi, safi);
  908. zlog_debug ("%s send message type %d, length (incl. header) %d",
  909. peer->host, CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV) ?
  910. BGP_MSG_ROUTE_REFRESH_NEW : BGP_MSG_ROUTE_REFRESH_OLD, length);
  911. }
  912. /* Make real packet. */
  913. packet = stream_dup (s);
  914. stream_free (s);
  915. /* Add packet to the peer. */
  916. bgp_packet_add (peer, packet);
  917. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  918. }
  919. /* Send capability message to the peer. */
  920. void
  921. bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi,
  922. int capability_code, int action)
  923. {
  924. struct stream *s;
  925. struct stream *packet;
  926. int length;
  927. /* Adjust safi code. */
  928. if (safi == SAFI_MPLS_VPN)
  929. safi = SAFI_MPLS_LABELED_VPN;
  930. s = stream_new (BGP_MAX_PACKET_SIZE);
  931. /* Make BGP update packet. */
  932. bgp_packet_set_marker (s, BGP_MSG_CAPABILITY);
  933. /* Encode MP_EXT capability. */
  934. if (capability_code == CAPABILITY_CODE_MP)
  935. {
  936. stream_putc (s, action);
  937. stream_putc (s, CAPABILITY_CODE_MP);
  938. stream_putc (s, CAPABILITY_CODE_MP_LEN);
  939. stream_putw (s, afi);
  940. stream_putc (s, 0);
  941. stream_putc (s, safi);
  942. if (BGP_DEBUG (normal, NORMAL))
  943. zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d",
  944. peer->host, action == CAPABILITY_ACTION_SET ?
  945. "Advertising" : "Removing", afi, safi);
  946. }
  947. /* Set packet size. */
  948. length = bgp_packet_set_size (s);
  949. /* Make real packet. */
  950. packet = stream_dup (s);
  951. stream_free (s);
  952. /* Add packet to the peer. */
  953. bgp_packet_add (peer, packet);
  954. if (BGP_DEBUG (normal, NORMAL))
  955. zlog_debug ("%s send message type %d, length (incl. header) %d",
  956. peer->host, BGP_MSG_CAPABILITY, length);
  957. BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd);
  958. }
  959. /* RFC1771 6.8 Connection collision detection. */
  960. static int
  961. bgp_collision_detect (struct peer *new, struct in_addr remote_id)
  962. {
  963. struct peer *peer;
  964. struct listnode *node, *nnode;
  965. struct bgp *bgp;
  966. bgp = bgp_get_default ();
  967. if (! bgp)
  968. return 0;
  969. /* Upon receipt of an OPEN message, the local system must examine
  970. all of its connections that are in the OpenConfirm state. A BGP
  971. speaker may also examine connections in an OpenSent state if it
  972. knows the BGP Identifier of the peer by means outside of the
  973. protocol. If among these connections there is a connection to a
  974. remote BGP speaker whose BGP Identifier equals the one in the
  975. OPEN message, then the local system performs the following
  976. collision resolution procedure: */
  977. for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer))
  978. {
  979. /* Under OpenConfirm status, local peer structure already hold
  980. remote router ID. */
  981. if (peer != new
  982. && (peer->status == OpenConfirm || peer->status == OpenSent)
  983. && sockunion_same (&peer->su, &new->su))
  984. {
  985. /* 1. The BGP Identifier of the local system is compared to
  986. the BGP Identifier of the remote system (as specified in
  987. the OPEN message). */
  988. if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr))
  989. {
  990. /* 2. If the value of the local BGP Identifier is less
  991. than the remote one, the local system closes BGP
  992. connection that already exists (the one that is
  993. already in the OpenConfirm state), and accepts BGP
  994. connection initiated by the remote system. */
  995. if (peer->fd >= 0)
  996. bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
  997. return 1;
  998. }
  999. else
  1000. {
  1001. /* 3. Otherwise, the local system closes newly created
  1002. BGP connection (the one associated with the newly
  1003. received OPEN message), and continues to use the
  1004. existing one (the one that is already in the
  1005. OpenConfirm state). */
  1006. if (new->fd >= 0)
  1007. bgp_notify_send (new, BGP_NOTIFY_CEASE,
  1008. BGP_NOTIFY_CEASE_COLLISION_RESOLUTION);
  1009. return -1;
  1010. }
  1011. }
  1012. }
  1013. return 0;
  1014. }
  1015. static int
  1016. bgp_open_receive (struct peer *peer, bgp_size_t size)
  1017. {
  1018. int ret;
  1019. u_char version;
  1020. u_char optlen;
  1021. u_int16_t holdtime;
  1022. u_int16_t send_holdtime;
  1023. as_t remote_as;
  1024. as_t as4 = 0;
  1025. struct peer *realpeer;
  1026. struct in_addr remote_id;
  1027. int mp_capability;
  1028. u_int8_t notify_data_remote_as[2];
  1029. u_int8_t notify_data_remote_id[4];
  1030. realpeer = NULL;
  1031. /* Parse open packet. */
  1032. version = stream_getc (peer->ibuf);
  1033. memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2);
  1034. remote_as = stream_getw (peer->ibuf);
  1035. holdtime = stream_getw (peer->ibuf);
  1036. memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4);
  1037. remote_id.s_addr = stream_get_ipv4 (peer->ibuf);
  1038. /* Receive OPEN message log */
  1039. if (BGP_DEBUG (normal, NORMAL))
  1040. zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u,"
  1041. " holdtime %d, id %s",
  1042. peer->host, version, remote_as, holdtime,
  1043. inet_ntoa (remote_id));
  1044. /* BEGIN to read the capability here, but dont do it yet */
  1045. mp_capability = 0;
  1046. optlen = stream_getc (peer->ibuf);
  1047. if (optlen != 0)
  1048. {
  1049. /* We need the as4 capability value *right now* because
  1050. * if it is there, we have not got the remote_as yet, and without
  1051. * that we do not know which peer is connecting to us now.
  1052. */
  1053. as4 = peek_for_as4_capability (peer, optlen);
  1054. }
  1055. /* Just in case we have a silly peer who sends AS4 capability set to 0 */
  1056. if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) && !as4)
  1057. {
  1058. zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0",
  1059. peer->host);
  1060. bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
  1061. BGP_NOTIFY_OPEN_BAD_PEER_AS);
  1062. return -1;
  1063. }
  1064. if (remote_as == BGP_AS_TRANS)
  1065. {
  1066. /* Take the AS4 from the capability. We must have received the
  1067. * capability now! Otherwise we have a asn16 peer who uses
  1068. * BGP_AS_TRANS, for some unknown reason.
  1069. */
  1070. if (as4 == BGP_AS_TRANS)
  1071. {
  1072. zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed",
  1073. peer->host);
  1074. bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
  1075. BGP_NOTIFY_OPEN_BAD_PEER_AS);
  1076. return -1;
  1077. }
  1078. if (!as4 && BGP_DEBUG (as4, AS4))
  1079. zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4."
  1080. " Odd, but proceeding.", peer->host);
  1081. else if (as4 < BGP_AS_MAX && BGP_DEBUG (as4, AS4))
  1082. zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits "
  1083. "in 2-bytes, very odd peer.", peer->host, as4);
  1084. if (as4)
  1085. remote_as = as4;
  1086. }
  1087. else
  1088. {
  1089. /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */
  1090. /* If we have got the capability, peer->as4cap must match remote_as */
  1091. if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV)
  1092. && as4 != remote_as)
  1093. {
  1094. /* raise error, log this, close session */
  1095. zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u"
  1096. " mismatch with 16bit 'myasn' %u in open",
  1097. peer->host, as4, remote_as);
  1098. bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
  1099. BGP_NOTIFY_OPEN_BAD_PEER_AS);
  1100. return -1;
  1101. }
  1102. }
  1103. /* Lookup peer from Open packet. */
  1104. if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  1105. {
  1106. int as = 0;
  1107. realpeer = peer_lookup_with_open (&peer->su, remote_as, &remote_id, &as);
  1108. if (! realpeer)
  1109. {
  1110. /* Peer's source IP address is check in bgp_accept(), so this
  1111. must be AS number mismatch or remote-id configuration
  1112. mismatch. */
  1113. if (as)
  1114. {
  1115. if (BGP_DEBUG (normal, NORMAL))
  1116. zlog_debug ("%s bad OPEN, wrong router identifier %s",
  1117. peer->host, inet_ntoa (remote_id));
  1118. bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
  1119. BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
  1120. notify_data_remote_id, 4);
  1121. }
  1122. else
  1123. {
  1124. if (BGP_DEBUG (normal, NORMAL))
  1125. zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
  1126. peer->host, remote_as, peer->as);
  1127. bgp_notify_send_with_data (peer, BGP_NOTIFY_OPEN_ERR,
  1128. BGP_NOTIFY_OPEN_BAD_PEER_AS,
  1129. notify_data_remote_as, 2);
  1130. }
  1131. return -1;
  1132. }
  1133. }
  1134. /* When collision is detected and this peer is closed. Retrun
  1135. immidiately. */
  1136. ret = bgp_collision_detect (peer, remote_id);
  1137. if (ret < 0)
  1138. return ret;
  1139. /* Hack part. */
  1140. if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  1141. {
  1142. if (realpeer->status == Established
  1143. && CHECK_FLAG (realpeer->sflags, PEER_STATUS_NSF_MODE))
  1144. {
  1145. realpeer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
  1146. SET_FLAG (realpeer->sflags, PEER_STATUS_NSF_WAIT);
  1147. }
  1148. else if (ret == 0 && realpeer->status != Active
  1149. && realpeer->status != OpenSent
  1150. && realpeer->status != OpenConfirm
  1151. && realpeer->status != Connect)
  1152. {
  1153. /* XXX: This is an awful problem..
  1154. *
  1155. * According to the RFC we should just let this connection (of the
  1156. * accepted 'peer') continue on to Established if the other
  1157. * connection (the 'realpeer' one) is in state Connect, and deal
  1158. * with the more larval FSM as/when it gets far enough to receive
  1159. * an Open. We don't do that though, we instead close the (more
  1160. * developed) accepted connection.
  1161. *
  1162. * This means there's a race, which if hit, can loop:
  1163. *
  1164. * FSM for A FSM for B
  1165. * realpeer accept-peer realpeer accept-peer
  1166. *
  1167. * Connect Connect
  1168. * Active
  1169. * OpenSent OpenSent
  1170. * <arrive here,
  1171. * Notify, delete>
  1172. * Idle Active
  1173. * OpenSent OpenSent
  1174. * <arrive here,
  1175. * Notify, delete>
  1176. * Idle
  1177. * <wait> <wait>
  1178. * Connect Connect
  1179. *
  1180. *
  1181. * If both sides are Quagga, they're almost certain to wait for
  1182. * the same amount of time of course (which doesn't preclude other
  1183. * implementations also waiting for same time). The race is
  1184. * exacerbated by high-latency (in bgpd and/or the network).
  1185. *
  1186. * The reason we do this is because our FSM is tied to our peer
  1187. * structure, which carries our configuration information, etc.
  1188. * I.e. we can't let the accepted-peer FSM continue on as it is,
  1189. * cause it's not associated with any actual peer configuration -
  1190. * it's just a dummy.
  1191. *
  1192. * It's possible we could hack-fix this by just bgp_stop'ing the
  1193. * realpeer and continueing on with the 'transfer FSM' below.
  1194. * Ideally, we need to seperate FSMs from struct peer.
  1195. *
  1196. * Setting one side to passive avoids the race, as a workaround.
  1197. */
  1198. if (BGP_DEBUG (events, EVENTS))
  1199. zlog_debug ("%s peer status is %s close connection",
  1200. realpeer->host, LOOKUP (bgp_status_msg,
  1201. realpeer->status));
  1202. bgp_notify_send (peer, BGP_NOTIFY_CEASE,
  1203. BGP_NOTIFY_CEASE_CONNECT_REJECT);
  1204. return -1;
  1205. }
  1206. if (BGP_DEBUG (events, EVENTS))
  1207. zlog_debug ("%s [Event] Transfer accept BGP peer to real (state %s)",
  1208. peer->host,
  1209. LOOKUP (bgp_status_msg, realpeer->status));
  1210. bgp_stop (realpeer);
  1211. /* Transfer file descriptor. */
  1212. realpeer->fd = peer->fd;
  1213. peer->fd = -1;
  1214. /* Transfer input buffer. */
  1215. stream_free (realpeer->ibuf);
  1216. realpeer->ibuf = peer->ibuf;
  1217. realpeer->packet_size = peer->packet_size;
  1218. peer->ibuf = NULL;
  1219. /* Transfer status. */
  1220. realpeer->status = peer->status;
  1221. bgp_stop (peer);
  1222. /* peer pointer change. Open packet send to neighbor. */
  1223. peer = realpeer;
  1224. bgp_open_send (peer);
  1225. if (peer->fd < 0)
  1226. {
  1227. zlog_err ("bgp_open_receive peer's fd is negative value %d",
  1228. peer->fd);
  1229. return -1;
  1230. }
  1231. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  1232. }
  1233. /* remote router-id check. */
  1234. if (remote_id.s_addr == 0
  1235. || IPV4_CLASS_DE (ntohl (remote_id.s_addr))
  1236. || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr))
  1237. {
  1238. if (BGP_DEBUG (normal, NORMAL))
  1239. zlog_debug ("%s bad OPEN, wrong router identifier %s",
  1240. peer->host, inet_ntoa (remote_id));
  1241. bgp_notify_send_with_data (peer,
  1242. BGP_NOTIFY_OPEN_ERR,
  1243. BGP_NOTIFY_OPEN_BAD_BGP_IDENT,
  1244. notify_data_remote_id, 4);
  1245. return -1;
  1246. }
  1247. /* Set remote router-id */
  1248. peer->remote_id = remote_id;
  1249. /* Peer BGP version check. */
  1250. if (version != BGP_VERSION_4)
  1251. {
  1252. u_int16_t maxver = htons(BGP_VERSION_4);
  1253. /* XXX this reply may not be correct if version < 4 XXX */
  1254. if (BGP_DEBUG (normal, NORMAL))
  1255. zlog_debug ("%s bad protocol version, remote requested %d, local request %d",
  1256. peer->host, version, BGP_VERSION_4);
  1257. /* Data must be in network byte order here */
  1258. bgp_notify_send_with_data (peer,
  1259. BGP_NOTIFY_OPEN_ERR,
  1260. BGP_NOTIFY_OPEN_UNSUP_VERSION,
  1261. (u_int8_t *) &maxver, 2);
  1262. return -1;
  1263. }
  1264. /* Check neighbor as number. */
  1265. if (remote_as != peer->as)
  1266. {
  1267. if (BGP_DEBUG (normal, NORMAL))
  1268. zlog_debug ("%s bad OPEN, remote AS is %u, expected %u",
  1269. peer->host, remote_as, peer->as);
  1270. bgp_notify_send_with_data (peer,
  1271. BGP_NOTIFY_OPEN_ERR,
  1272. BGP_NOTIFY_OPEN_BAD_PEER_AS,
  1273. notify_data_remote_as, 2);
  1274. return -1;
  1275. }
  1276. /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST
  1277. calculate the value of the Hold Timer by using the smaller of its
  1278. configured Hold Time and the Hold Time received in the OPEN message.
  1279. The Hold Time MUST be either zero or at least three seconds. An
  1280. implementation may reject connections on the basis of the Hold Time. */
  1281. if (holdtime < 3 && holdtime != 0)
  1282. {
  1283. bgp_notify_send (peer,
  1284. BGP_NOTIFY_OPEN_ERR,
  1285. BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
  1286. return -1;
  1287. }
  1288. /* From the rfc: A reasonable maximum time between KEEPALIVE messages
  1289. would be one third of the Hold Time interval. KEEPALIVE messages
  1290. MUST NOT be sent more frequently than one per second. An
  1291. implementation MAY adjust the rate at which it sends KEEPALIVE
  1292. messages as a function of the Hold Time interval. */
  1293. if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER))
  1294. send_holdtime = peer->holdtime;
  1295. else
  1296. send_holdtime = peer->bgp->default_holdtime;
  1297. if (holdtime < send_holdtime)
  1298. peer->v_holdtime = holdtime;
  1299. else
  1300. peer->v_holdtime = send_holdtime;
  1301. peer->v_keepalive = peer->v_holdtime / 3;
  1302. /* Open option part parse. */
  1303. if (optlen != 0)
  1304. {
  1305. if ((ret = bgp_open_option_parse (peer, optlen, &mp_capability)) < 0)
  1306. {
  1307. bgp_notify_send (peer,
  1308. BGP_NOTIFY_OPEN_ERR,
  1309. BGP_NOTIFY_OPEN_UNACEP_HOLDTIME);
  1310. return ret;
  1311. }
  1312. }
  1313. else
  1314. {
  1315. if (BGP_DEBUG (normal, NORMAL))
  1316. zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0",
  1317. peer->host);
  1318. }
  1319. /*
  1320. * Assume that the peer supports the locally configured set of
  1321. * AFI/SAFIs if the peer did not send us any Mulitiprotocol
  1322. * capabilities, or if 'override-capability' is configured.
  1323. */
  1324. if (! mp_capability ||
  1325. CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
  1326. {
  1327. peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST];
  1328. peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST];
  1329. peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST];
  1330. peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST];
  1331. }
  1332. /* Get sockname. */
  1333. bgp_getsockname (peer);
  1334. BGP_EVENT_ADD (peer, Receive_OPEN_message);
  1335. peer->packet_size = 0;
  1336. if (peer->ibuf)
  1337. stream_reset (peer->ibuf);
  1338. return 0;
  1339. }
  1340. /* Parse BGP Update packet and make attribute object. */
  1341. static int
  1342. bgp_update_receive (struct peer *peer, bgp_size_t size)
  1343. {
  1344. int ret;
  1345. u_char *end;
  1346. struct stream *s;
  1347. struct attr attr;
  1348. struct attr_extra extra;
  1349. bgp_size_t attribute_len;
  1350. bgp_size_t update_len;
  1351. bgp_size_t withdraw_len;
  1352. struct bgp_nlri update;
  1353. struct bgp_nlri withdraw;
  1354. struct bgp_nlri mp_update;
  1355. struct bgp_nlri mp_withdraw;
  1356. /* Status must be Established. */
  1357. if (peer->status != Established)
  1358. {
  1359. zlog_err ("%s [FSM] Update packet received under status %s",
  1360. peer->host, LOOKUP (bgp_status_msg, peer->status));
  1361. bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
  1362. return -1;
  1363. }
  1364. /* Set initial values. */
  1365. memset (&attr, 0, sizeof (struct attr));
  1366. memset (&extra, 0, sizeof (struct attr_extra));
  1367. memset (&update, 0, sizeof (struct bgp_nlri));
  1368. memset (&withdraw, 0, sizeof (struct bgp_nlri));
  1369. memset (&mp_update, 0, sizeof (struct bgp_nlri));
  1370. memset (&mp_withdraw, 0, sizeof (struct bgp_nlri));
  1371. attr.extra = &extra;
  1372. s = peer->ibuf;
  1373. end = stream_pnt (s) + size;
  1374. /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute
  1375. Length is too large (i.e., if Unfeasible Routes Length + Total
  1376. Attribute Length + 23 exceeds the message Length), then the Error
  1377. Subcode is set to Malformed Attribute List. */
  1378. if (stream_pnt (s) + 2 > end)
  1379. {
  1380. zlog_err ("%s [Error] Update packet error"
  1381. " (packet length is short for unfeasible length)",
  1382. peer->host);
  1383. bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
  1384. BGP_NOTIFY_UPDATE_MAL_ATTR);
  1385. return -1;
  1386. }
  1387. /* Unfeasible Route Length. */
  1388. withdraw_len = stream_getw (s);
  1389. /* Unfeasible Route Length check. */
  1390. if (stream_pnt (s) + withdraw_len > end)
  1391. {
  1392. zlog_err ("%s [Error] Update packet error"
  1393. " (packet unfeasible length overflow %d)",
  1394. peer->host, withdraw_len);
  1395. bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
  1396. BGP_NOTIFY_UPDATE_MAL_ATTR);
  1397. return -1;
  1398. }
  1399. /* Unfeasible Route packet format check. */
  1400. if (withdraw_len > 0)
  1401. {
  1402. ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), withdraw_len);
  1403. if (ret < 0)
  1404. return -1;
  1405. if (BGP_DEBUG (packet, PACKET_RECV))
  1406. zlog_debug ("%s [Update:RECV] Unfeasible NLRI received", peer->host);
  1407. withdraw.afi = AFI_IP;
  1408. withdraw.safi = SAFI_UNICAST;
  1409. withdraw.nlri = stream_pnt (s);
  1410. withdraw.length = withdraw_len;
  1411. stream_forward_getp (s, withdraw_len);
  1412. }
  1413. /* Attribute total length check. */
  1414. if (stream_pnt (s) + 2 > end)
  1415. {
  1416. zlog_warn ("%s [Error] Packet Error"
  1417. " (update packet is short for attribute length)",
  1418. peer->host);
  1419. bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
  1420. BGP_NOTIFY_UPDATE_MAL_ATTR);
  1421. return -1;
  1422. }
  1423. /* Fetch attribute total length. */
  1424. attribute_len = stream_getw (s);
  1425. /* Attribute length check. */
  1426. if (stream_pnt (s) + attribute_len > end)
  1427. {
  1428. zlog_warn ("%s [Error] Packet Error"
  1429. " (update packet attribute length overflow %d)",
  1430. peer->host, attribute_len);
  1431. bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR,
  1432. BGP_NOTIFY_UPDATE_MAL_ATTR);
  1433. return -1;
  1434. }
  1435. /* Certain attribute parsing errors should not be considered bad enough
  1436. * to reset the session for, most particularly any partial/optional
  1437. * attributes that have 'tunneled' over speakers that don't understand
  1438. * them. Instead we withdraw only the prefix concerned.
  1439. *
  1440. * Complicates the flow a little though..
  1441. */
  1442. bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED;
  1443. /* This define morphs the update case into a withdraw when lower levels
  1444. * have signalled an error condition where this is best.
  1445. */
  1446. #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL)
  1447. /* Parse attribute when it exists. */
  1448. if (attribute_len)
  1449. {
  1450. attr_parse_ret = bgp_attr_parse (peer, &attr, attribute_len,
  1451. &mp_update, &mp_withdraw);
  1452. if (attr_parse_ret == BGP_ATTR_PARSE_ERROR)
  1453. return -1;
  1454. }
  1455. /* Logging the attribute. */
  1456. if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW
  1457. || BGP_DEBUG (update, UPDATE_IN))
  1458. {
  1459. char attrstr[BUFSIZ];
  1460. attrstr[0] = '\0';
  1461. ret= bgp_dump_attr (peer, &attr, attrstr, BUFSIZ);
  1462. int lvl = (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
  1463. ? LOG_ERR : LOG_DEBUG;
  1464. if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW)
  1465. zlog (peer->log, LOG_ERR,
  1466. "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.",
  1467. peer->host);
  1468. if (ret)
  1469. zlog (peer->log, lvl, "%s rcvd UPDATE w/ attr: %s",
  1470. peer->host, attrstr);
  1471. }
  1472. /* Network Layer Reachability Information. */
  1473. update_len = end - stream_pnt (s);
  1474. if (update_len)
  1475. {
  1476. /* Check NLRI packet format and prefix length. */
  1477. ret = bgp_nlri_sanity_check (peer, AFI_IP, stream_pnt (s), update_len);
  1478. if (ret < 0)
  1479. {
  1480. bgp_attr_unintern_sub (&attr);
  1481. return -1;
  1482. }
  1483. /* Set NLRI portion to structure. */
  1484. update.afi = AFI_IP;
  1485. update.safi = SAFI_UNICAST;
  1486. update.nlri = stream_pnt (s);
  1487. update.length = update_len;
  1488. stream_forward_getp (s, update_len);
  1489. }
  1490. /* NLRI is processed only when the peer is configured specific
  1491. Address Family and Subsequent Address Family. */
  1492. if (peer->afc[AFI_IP][SAFI_UNICAST])
  1493. {
  1494. if (withdraw.length)
  1495. bgp_nlri_parse (peer, NULL, &withdraw);
  1496. if (update.length)
  1497. {
  1498. /* We check well-known attribute only for IPv4 unicast
  1499. update. */
  1500. ret = bgp_attr_check (peer, &attr);
  1501. if (ret < 0)
  1502. {
  1503. bgp_attr_unintern_sub (&attr);
  1504. return -1;
  1505. }
  1506. bgp_nlri_parse (peer, NLRI_ATTR_ARG, &update);
  1507. }
  1508. if (mp_update.length
  1509. && mp_update.afi == AFI_IP
  1510. && mp_update.safi == SAFI_UNICAST)
  1511. bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
  1512. if (mp_withdraw.length
  1513. && mp_withdraw.afi == AFI_IP
  1514. && mp_withdraw.safi == SAFI_UNICAST)
  1515. bgp_nlri_parse (peer, NULL, &mp_withdraw);
  1516. if (! attribute_len && ! withdraw_len)
  1517. {
  1518. /* End-of-RIB received */
  1519. SET_FLAG (peer->af_sflags[AFI_IP][SAFI_UNICAST],
  1520. PEER_STATUS_EOR_RECEIVED);
  1521. /* NSF delete stale route */
  1522. if (peer->nsf[AFI_IP][SAFI_UNICAST])
  1523. bgp_clear_stale_route (peer, AFI_IP, SAFI_UNICAST);
  1524. if (BGP_DEBUG (normal, NORMAL))
  1525. zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Unicast from %s",
  1526. peer->host);
  1527. }
  1528. }
  1529. if (peer->afc[AFI_IP][SAFI_MULTICAST])
  1530. {
  1531. if (mp_update.length
  1532. && mp_update.afi == AFI_IP
  1533. && mp_update.safi == SAFI_MULTICAST)
  1534. bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
  1535. if (mp_withdraw.length
  1536. && mp_withdraw.afi == AFI_IP
  1537. && mp_withdraw.safi == SAFI_MULTICAST)
  1538. bgp_nlri_parse (peer, NULL, &mp_withdraw);
  1539. if (! withdraw_len
  1540. && mp_withdraw.afi == AFI_IP
  1541. && mp_withdraw.safi == SAFI_MULTICAST
  1542. && mp_withdraw.length == 0)
  1543. {
  1544. /* End-of-RIB received */
  1545. SET_FLAG (peer->af_sflags[AFI_IP][SAFI_MULTICAST],
  1546. PEER_STATUS_EOR_RECEIVED);
  1547. /* NSF delete stale route */
  1548. if (peer->nsf[AFI_IP][SAFI_MULTICAST])
  1549. bgp_clear_stale_route (peer, AFI_IP, SAFI_MULTICAST);
  1550. if (BGP_DEBUG (normal, NORMAL))
  1551. zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv4 Multicast from %s",
  1552. peer->host);
  1553. }
  1554. }
  1555. if (peer->afc[AFI_IP6][SAFI_UNICAST])
  1556. {
  1557. if (mp_update.length
  1558. && mp_update.afi == AFI_IP6
  1559. && mp_update.safi == SAFI_UNICAST)
  1560. bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
  1561. if (mp_withdraw.length
  1562. && mp_withdraw.afi == AFI_IP6
  1563. && mp_withdraw.safi == SAFI_UNICAST)
  1564. bgp_nlri_parse (peer, NULL, &mp_withdraw);
  1565. if (! withdraw_len
  1566. && mp_withdraw.afi == AFI_IP6
  1567. && mp_withdraw.safi == SAFI_UNICAST
  1568. && mp_withdraw.length == 0)
  1569. {
  1570. /* End-of-RIB received */
  1571. SET_FLAG (peer->af_sflags[AFI_IP6][SAFI_UNICAST], PEER_STATUS_EOR_RECEIVED);
  1572. /* NSF delete stale route */
  1573. if (peer->nsf[AFI_IP6][SAFI_UNICAST])
  1574. bgp_clear_stale_route (peer, AFI_IP6, SAFI_UNICAST);
  1575. if (BGP_DEBUG (normal, NORMAL))
  1576. zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Unicast from %s",
  1577. peer->host);
  1578. }
  1579. }
  1580. if (peer->afc[AFI_IP6][SAFI_MULTICAST])
  1581. {
  1582. if (mp_update.length
  1583. && mp_update.afi == AFI_IP6
  1584. && mp_update.safi == SAFI_MULTICAST)
  1585. bgp_nlri_parse (peer, NLRI_ATTR_ARG, &mp_update);
  1586. if (mp_withdraw.length
  1587. && mp_withdraw.afi == AFI_IP6
  1588. && mp_withdraw.safi == SAFI_MULTICAST)
  1589. bgp_nlri_parse (peer, NULL, &mp_withdraw);
  1590. if (! withdraw_len
  1591. && mp_withdraw.afi == AFI_IP6
  1592. && mp_withdraw.safi == SAFI_MULTICAST
  1593. && mp_withdraw.length == 0)
  1594. {
  1595. /* End-of-RIB received */
  1596. /* NSF delete stale route */
  1597. if (peer->nsf[AFI_IP6][SAFI_MULTICAST])
  1598. bgp_clear_stale_route (peer, AFI_IP6, SAFI_MULTICAST);
  1599. if (BGP_DEBUG (update, UPDATE_IN))
  1600. zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for IPv6 Multicast from %s",
  1601. peer->host);
  1602. }
  1603. }
  1604. if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
  1605. {
  1606. if (mp_update.length
  1607. && mp_update.afi == AFI_IP
  1608. && mp_update.safi == SAFI_MPLS_LABELED_VPN)
  1609. bgp_nlri_parse_vpnv4 (peer, NLRI_ATTR_ARG, &mp_update);
  1610. if (mp_withdraw.length
  1611. && mp_withdraw.afi == AFI_IP
  1612. && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN)
  1613. bgp_nlri_parse_vpnv4 (peer, NULL, &mp_withdraw);
  1614. if (! withdraw_len
  1615. && mp_withdraw.afi == AFI_IP
  1616. && mp_withdraw.safi == SAFI_MPLS_LABELED_VPN
  1617. && mp_withdraw.length == 0)
  1618. {
  1619. /* End-of-RIB received */
  1620. if (BGP_DEBUG (update, UPDATE_IN))
  1621. zlog (peer->log, LOG_DEBUG, "rcvd End-of-RIB for VPNv4 Unicast from %s",
  1622. peer->host);
  1623. }
  1624. }
  1625. /* Everything is done. We unintern temporary structures which
  1626. interned in bgp_attr_parse(). */
  1627. bgp_attr_unintern_sub (&attr);
  1628. /* If peering is stopped due to some reason, do not generate BGP
  1629. event. */
  1630. if (peer->status != Established)
  1631. return 0;
  1632. /* Increment packet counter. */
  1633. peer->update_in++;
  1634. peer->update_time = bgp_clock ();
  1635. /* Rearm holdtime timer */
  1636. BGP_TIMER_OFF (peer->t_holdtime);
  1637. bgp_timer_set (peer);
  1638. return 0;
  1639. }
  1640. /* Notify message treatment function. */
  1641. static void
  1642. bgp_notify_receive (struct peer *peer, bgp_size_t size)
  1643. {
  1644. struct bgp_notify bgp_notify;
  1645. if (peer->notify.data)
  1646. {
  1647. XFREE (MTYPE_TMP, peer->notify.data);
  1648. peer->notify.data = NULL;
  1649. peer->notify.length = 0;
  1650. }
  1651. bgp_notify.code = stream_getc (peer->ibuf);
  1652. bgp_notify.subcode = stream_getc (peer->ibuf);
  1653. bgp_notify.length = size - 2;
  1654. bgp_notify.data = NULL;
  1655. /* Preserv notify code and sub code. */
  1656. peer->notify.code = bgp_notify.code;
  1657. peer->notify.subcode = bgp_notify.subcode;
  1658. /* For further diagnostic record returned Data. */
  1659. if (bgp_notify.length)
  1660. {
  1661. peer->notify.length = size - 2;
  1662. peer->notify.data = XMALLOC (MTYPE_TMP, size - 2);
  1663. memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2);
  1664. }
  1665. /* For debug */
  1666. {
  1667. int i;
  1668. int first = 0;
  1669. char c[4];
  1670. if (bgp_notify.length)
  1671. {
  1672. bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3);
  1673. for (i = 0; i < bgp_notify.length; i++)
  1674. if (first)
  1675. {
  1676. sprintf (c, " %02x", stream_getc (peer->ibuf));
  1677. strcat (bgp_notify.data, c);
  1678. }
  1679. else
  1680. {
  1681. first = 1;
  1682. sprintf (c, "%02x", stream_getc (peer->ibuf));
  1683. strcpy (bgp_notify.data, c);
  1684. }
  1685. }
  1686. bgp_notify_print(peer, &bgp_notify, "received");
  1687. if (bgp_notify.data)
  1688. XFREE (MTYPE_TMP, bgp_notify.data);
  1689. }
  1690. /* peer count update */
  1691. peer->notify_in++;
  1692. if (peer->status == Established)
  1693. peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED;
  1694. /* We have to check for Notify with Unsupported Optional Parameter.
  1695. in that case we fallback to open without the capability option.
  1696. But this done in bgp_stop. We just mark it here to avoid changing
  1697. the fsm tables. */
  1698. if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR &&
  1699. bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM )
  1700. UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN);
  1701. BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message);
  1702. }
  1703. /* Keepalive treatment function -- get keepalive send keepalive */
  1704. static void
  1705. bgp_keepalive_receive (struct peer *peer, bgp_size_t size)
  1706. {
  1707. if (BGP_DEBUG (keepalive, KEEPALIVE))
  1708. zlog_debug ("%s KEEPALIVE rcvd", peer->host);
  1709. BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message);
  1710. }
  1711. /* Route refresh message is received. */
  1712. static void
  1713. bgp_route_refresh_receive (struct peer *peer, bgp_size_t size)
  1714. {
  1715. afi_t afi;
  1716. safi_t safi;
  1717. u_char reserved;
  1718. struct stream *s;
  1719. /* If peer does not have the capability, send notification. */
  1720. if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV))
  1721. {
  1722. plog_err (peer->log, "%s [Error] BGP route refresh is not enabled",
  1723. peer->host);
  1724. bgp_notify_send (peer,
  1725. BGP_NOTIFY_HEADER_ERR,
  1726. BGP_NOTIFY_HEADER_BAD_MESTYPE);
  1727. return;
  1728. }
  1729. /* Status must be Established. */
  1730. if (peer->status != Established)
  1731. {
  1732. plog_err (peer->log,
  1733. "%s [Error] Route refresh packet received under status %s",
  1734. peer->host, LOOKUP (bgp_status_msg, peer->status));
  1735. bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
  1736. return;
  1737. }
  1738. s = peer->ibuf;
  1739. /* Parse packet. */
  1740. afi = stream_getw (s);
  1741. reserved = stream_getc (s);
  1742. safi = stream_getc (s);
  1743. if (BGP_DEBUG (normal, NORMAL))
  1744. zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d",
  1745. peer->host, afi, safi);
  1746. /* Check AFI and SAFI. */
  1747. if ((afi != AFI_IP && afi != AFI_IP6)
  1748. || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
  1749. && safi != SAFI_MPLS_LABELED_VPN))
  1750. {
  1751. if (BGP_DEBUG (normal, NORMAL))
  1752. {
  1753. zlog_debug ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored",
  1754. peer->host, afi, safi);
  1755. }
  1756. return;
  1757. }
  1758. /* Adjust safi code. */
  1759. if (safi == SAFI_MPLS_LABELED_VPN)
  1760. safi = SAFI_MPLS_VPN;
  1761. if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE)
  1762. {
  1763. u_char *end;
  1764. u_char when_to_refresh;
  1765. u_char orf_type;
  1766. u_int16_t orf_len;
  1767. if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5)
  1768. {
  1769. zlog_info ("%s ORF route refresh length error", peer->host);
  1770. bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
  1771. return;
  1772. }
  1773. when_to_refresh = stream_getc (s);
  1774. end = stream_pnt (s) + (size - 5);
  1775. while ((stream_pnt (s) + 2) < end)
  1776. {
  1777. orf_type = stream_getc (s);
  1778. orf_len = stream_getw (s);
  1779. /* orf_len in bounds? */
  1780. if ((stream_pnt (s) + orf_len) > end)
  1781. break; /* XXX: Notify instead?? */
  1782. if (orf_type == ORF_TYPE_PREFIX
  1783. || orf_type == ORF_TYPE_PREFIX_OLD)
  1784. {
  1785. u_char *p_pnt = stream_pnt (s);
  1786. u_char *p_end = stream_pnt (s) + orf_len;
  1787. struct orf_prefix orfp;
  1788. u_char common = 0;
  1789. u_int32_t seq;
  1790. int psize;
  1791. char name[BUFSIZ];
  1792. int ret;
  1793. if (BGP_DEBUG (normal, NORMAL))
  1794. {
  1795. zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d",
  1796. peer->host, orf_type, orf_len);
  1797. }
  1798. /* we're going to read at least 1 byte of common ORF header,
  1799. * and 7 bytes of ORF Address-filter entry from the stream
  1800. */
  1801. if (orf_len < 7)
  1802. break;
  1803. /* ORF prefix-list name */
  1804. sprintf (name, "%s.%d.%d", peer->host, afi, safi);
  1805. while (p_pnt < p_end)
  1806. {
  1807. /* If the ORF entry is malformed, want to read as much of it
  1808. * as possible without going beyond the bounds of the entry,
  1809. * to maximise debug information.
  1810. */
  1811. int ok;
  1812. memset (&orfp, 0, sizeof (struct orf_prefix));
  1813. common = *p_pnt++;
  1814. /* after ++: p_pnt <= p_end */
  1815. if (common & ORF_COMMON_PART_REMOVE_ALL)
  1816. {
  1817. if (BGP_DEBUG (normal, NORMAL))
  1818. zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host);
  1819. prefix_bgp_orf_remove_all (name);
  1820. break;
  1821. }
  1822. ok = ((p_end - p_pnt) >= sizeof(u_int32_t)) ;
  1823. if (ok)
  1824. {
  1825. memcpy (&seq, p_pnt, sizeof (u_int32_t));
  1826. p_pnt += sizeof (u_int32_t);
  1827. orfp.seq = ntohl (seq);
  1828. }
  1829. else
  1830. p_pnt = p_end ;
  1831. if ((ok = (p_pnt < p_end)))
  1832. orfp.ge = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */
  1833. if ((ok = (p_pnt < p_end)))
  1834. orfp.le = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */
  1835. if ((ok = (p_pnt < p_end)))
  1836. orfp.p.prefixlen = *p_pnt++ ;
  1837. orfp.p.family = afi2family (afi); /* afi checked already */
  1838. psize = PSIZE (orfp.p.prefixlen); /* 0 if not ok */
  1839. if (psize > prefix_blen(&orfp.p)) /* valid for family ? */
  1840. {
  1841. ok = 0 ;
  1842. psize = prefix_blen(&orfp.p) ;
  1843. }
  1844. if (psize > (p_end - p_pnt)) /* valid for packet ? */
  1845. {
  1846. ok = 0 ;
  1847. psize = p_end - p_pnt ;
  1848. }
  1849. if (psize > 0)
  1850. memcpy (&orfp.p.u.prefix, p_pnt, psize);
  1851. p_pnt += psize;
  1852. if (BGP_DEBUG (normal, NORMAL))
  1853. {
  1854. char buf[INET6_BUFSIZ];
  1855. zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s",
  1856. peer->host,
  1857. (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"),
  1858. (common & ORF_COMMON_PART_DENY ? "deny" : "permit"),
  1859. orfp.seq,
  1860. inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, INET6_BUFSIZ),
  1861. orfp.p.prefixlen, orfp.ge, orfp.le,
  1862. ok ? "" : " MALFORMED");
  1863. }
  1864. if (ok)
  1865. ret = prefix_bgp_orf_set (name, afi, &orfp,
  1866. (common & ORF_COMMON_PART_DENY ? 0 : 1 ),
  1867. (common & ORF_COMMON_PART_REMOVE ? 0 : 1));
  1868. if (!ok || (ret != CMD_SUCCESS))
  1869. {
  1870. if (BGP_DEBUG (normal, NORMAL))
  1871. zlog_debug ("%s Received misformatted prefixlist ORF."
  1872. " Remove All pfxlist", peer->host);
  1873. prefix_bgp_orf_remove_all (name);
  1874. break;
  1875. }
  1876. }
  1877. peer->orf_plist[afi][safi] =
  1878. prefix_list_lookup (AFI_ORF_PREFIX, name);
  1879. }
  1880. stream_forward_getp (s, orf_len);
  1881. }
  1882. if (BGP_DEBUG (normal, NORMAL))
  1883. zlog_debug ("%s rcvd Refresh %s ORF request", peer->host,
  1884. when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate");
  1885. if (when_to_refresh == REFRESH_DEFER)
  1886. return;
  1887. }
  1888. /* First update is deferred until ORF or ROUTE-REFRESH is received */
  1889. if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH))
  1890. UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH);
  1891. /* Perform route refreshment to the peer */
  1892. bgp_announce_route (peer, afi, safi);
  1893. }
  1894. static int
  1895. bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length)
  1896. {
  1897. u_char *end;
  1898. struct capability_mp_data mpc;
  1899. struct capability_header *hdr;
  1900. u_char action;
  1901. struct bgp *bgp;
  1902. afi_t afi;
  1903. safi_t safi;
  1904. bgp = peer->bgp;
  1905. end = pnt + length;
  1906. while (pnt < end)
  1907. {
  1908. /* We need at least action, capability code and capability length. */
  1909. if (pnt + 3 > end)
  1910. {
  1911. zlog_info ("%s Capability length error", peer->host);
  1912. bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
  1913. return -1;
  1914. }
  1915. action = *pnt;
  1916. hdr = (struct capability_header *)(pnt + 1);
  1917. /* Action value check. */
  1918. if (action != CAPABILITY_ACTION_SET
  1919. && action != CAPABILITY_ACTION_UNSET)
  1920. {
  1921. zlog_info ("%s Capability Action Value error %d",
  1922. peer->host, action);
  1923. bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
  1924. return -1;
  1925. }
  1926. if (BGP_DEBUG (normal, NORMAL))
  1927. zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u",
  1928. peer->host, action, hdr->code, hdr->length);
  1929. /* Capability length check. */
  1930. if ((pnt + hdr->length + 3) > end)
  1931. {
  1932. zlog_info ("%s Capability length error", peer->host);
  1933. bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
  1934. return -1;
  1935. }
  1936. /* Fetch structure to the byte stream. */
  1937. memcpy (&mpc, pnt + 3, sizeof (struct capability_mp_data));
  1938. /* We know MP Capability Code. */
  1939. if (hdr->code == CAPABILITY_CODE_MP)
  1940. {
  1941. afi = ntohs (mpc.afi);
  1942. safi = mpc.safi;
  1943. /* Ignore capability when override-capability is set. */
  1944. if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
  1945. continue;
  1946. if (!bgp_afi_safi_valid_indices (afi, &safi))
  1947. {
  1948. if (BGP_DEBUG (normal, NORMAL))
  1949. zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid "
  1950. "(%u/%u)", peer->host, afi, safi);
  1951. continue;
  1952. }
  1953. /* Address family check. */
  1954. if (BGP_DEBUG (normal, NORMAL))
  1955. zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u",
  1956. peer->host,
  1957. action == CAPABILITY_ACTION_SET
  1958. ? "Advertising" : "Removing",
  1959. ntohs(mpc.afi) , mpc.safi);
  1960. if (action == CAPABILITY_ACTION_SET)
  1961. {
  1962. peer->afc_recv[afi][safi] = 1;
  1963. if (peer->afc[afi][safi])
  1964. {
  1965. peer->afc_nego[afi][safi] = 1;
  1966. bgp_announce_route (peer, afi, safi);
  1967. }
  1968. }
  1969. else
  1970. {
  1971. peer->afc_recv[afi][safi] = 0;
  1972. peer->afc_nego[afi][safi] = 0;
  1973. if (peer_active_nego (peer))
  1974. bgp_clear_route (peer, afi, safi, BGP_CLEAR_ROUTE_NORMAL);
  1975. else
  1976. BGP_EVENT_ADD (peer, BGP_Stop);
  1977. }
  1978. }
  1979. else
  1980. {
  1981. zlog_warn ("%s unrecognized capability code: %d - ignored",
  1982. peer->host, hdr->code);
  1983. }
  1984. pnt += hdr->length + 3;
  1985. }
  1986. return 0;
  1987. }
  1988. /* Dynamic Capability is received.
  1989. *
  1990. * This is exported for unit-test purposes
  1991. */
  1992. int
  1993. bgp_capability_receive (struct peer *peer, bgp_size_t size)
  1994. {
  1995. u_char *pnt;
  1996. /* Fetch pointer. */
  1997. pnt = stream_pnt (peer->ibuf);
  1998. if (BGP_DEBUG (normal, NORMAL))
  1999. zlog_debug ("%s rcv CAPABILITY", peer->host);
  2000. /* If peer does not have the capability, send notification. */
  2001. if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV))
  2002. {
  2003. plog_err (peer->log, "%s [Error] BGP dynamic capability is not enabled",
  2004. peer->host);
  2005. bgp_notify_send (peer,
  2006. BGP_NOTIFY_HEADER_ERR,
  2007. BGP_NOTIFY_HEADER_BAD_MESTYPE);
  2008. return -1;
  2009. }
  2010. /* Status must be Established. */
  2011. if (peer->status != Established)
  2012. {
  2013. plog_err (peer->log,
  2014. "%s [Error] Dynamic capability packet received under status %s", peer->host, LOOKUP (bgp_status_msg, peer->status));
  2015. bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0);
  2016. return -1;
  2017. }
  2018. /* Parse packet. */
  2019. return bgp_capability_msg_parse (peer, pnt, size);
  2020. }
  2021. /* BGP read utility function. */
  2022. static int
  2023. bgp_read_packet (struct peer *peer)
  2024. {
  2025. int nbytes;
  2026. int readsize;
  2027. readsize = peer->packet_size - stream_get_endp (peer->ibuf);
  2028. /* If size is zero then return. */
  2029. if (! readsize)
  2030. return 0;
  2031. /* Read packet from fd. */
  2032. nbytes = stream_read_try (peer->ibuf, peer->fd, readsize);
  2033. /* If read byte is smaller than zero then error occured. */
  2034. if (nbytes < 0)
  2035. {
  2036. /* Transient error should retry */
  2037. if (nbytes == -2)
  2038. return -1;
  2039. plog_err (peer->log, "%s [Error] bgp_read_packet error: %s",
  2040. peer->host, safe_strerror (errno));
  2041. if (peer->status == Established)
  2042. {
  2043. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
  2044. {
  2045. peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
  2046. SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  2047. }
  2048. else
  2049. peer->last_reset = PEER_DOWN_CLOSE_SESSION;
  2050. }
  2051. BGP_EVENT_ADD (peer, TCP_fatal_error);
  2052. return -1;
  2053. }
  2054. /* When read byte is zero : clear bgp peer and return */
  2055. if (nbytes == 0)
  2056. {
  2057. if (BGP_DEBUG (events, EVENTS))
  2058. plog_debug (peer->log, "%s [Event] BGP connection closed fd %d",
  2059. peer->host, peer->fd);
  2060. if (peer->status == Established)
  2061. {
  2062. if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE))
  2063. {
  2064. peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION;
  2065. SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT);
  2066. }
  2067. else
  2068. peer->last_reset = PEER_DOWN_CLOSE_SESSION;
  2069. }
  2070. BGP_EVENT_ADD (peer, TCP_connection_closed);
  2071. return -1;
  2072. }
  2073. /* We read partial packet. */
  2074. if (stream_get_endp (peer->ibuf) != peer->packet_size)
  2075. return -1;
  2076. return 0;
  2077. }
  2078. /* Marker check. */
  2079. static int
  2080. bgp_marker_all_one (struct stream *s, int length)
  2081. {
  2082. int i;
  2083. for (i = 0; i < length; i++)
  2084. if (s->data[i] != 0xff)
  2085. return 0;
  2086. return 1;
  2087. }
  2088. /* Recent thread time.
  2089. On same clock base as bgp_clock (MONOTONIC)
  2090. but can be time of last context switch to bgp_read thread. */
  2091. static time_t
  2092. bgp_recent_clock (void)
  2093. {
  2094. return recent_relative_time().tv_sec;
  2095. }
  2096. /* Starting point of packet process function. */
  2097. int
  2098. bgp_read (struct thread *thread)
  2099. {
  2100. int ret;
  2101. u_char type = 0;
  2102. struct peer *peer;
  2103. bgp_size_t size;
  2104. char notify_data_length[2];
  2105. /* Yes first of all get peer pointer. */
  2106. peer = THREAD_ARG (thread);
  2107. peer->t_read = NULL;
  2108. /* For non-blocking IO check. */
  2109. if (peer->status == Connect)
  2110. {
  2111. bgp_connect_check (peer);
  2112. goto done;
  2113. }
  2114. else
  2115. {
  2116. if (peer->fd < 0)
  2117. {
  2118. zlog_err ("bgp_read peer's fd is negative value %d", peer->fd);
  2119. return -1;
  2120. }
  2121. BGP_READ_ON (peer->t_read, bgp_read, peer->fd);
  2122. }
  2123. /* Read packet header to determine type of the packet */
  2124. if (peer->packet_size == 0)
  2125. peer->packet_size = BGP_HEADER_SIZE;
  2126. if (stream_get_endp (peer->ibuf) < BGP_HEADER_SIZE)
  2127. {
  2128. ret = bgp_read_packet (peer);
  2129. /* Header read error or partial read packet. */
  2130. if (ret < 0)
  2131. goto done;
  2132. /* Get size and type. */
  2133. stream_forward_getp (peer->ibuf, BGP_MARKER_SIZE);
  2134. memcpy (notify_data_length, stream_pnt (peer->ibuf), 2);
  2135. size = stream_getw (peer->ibuf);
  2136. type = stream_getc (peer->ibuf);
  2137. if (BGP_DEBUG (normal, NORMAL) && type != 2 && type != 0)
  2138. zlog_debug ("%s rcv message type %d, length (excl. header) %d",
  2139. peer->host, type, size - BGP_HEADER_SIZE);
  2140. /* Marker check */
  2141. if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE))
  2142. && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE))
  2143. {
  2144. bgp_notify_send (peer,
  2145. BGP_NOTIFY_HEADER_ERR,
  2146. BGP_NOTIFY_HEADER_NOT_SYNC);
  2147. goto done;
  2148. }
  2149. /* BGP type check. */
  2150. if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE
  2151. && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE
  2152. && type != BGP_MSG_ROUTE_REFRESH_NEW
  2153. && type != BGP_MSG_ROUTE_REFRESH_OLD
  2154. && type != BGP_MSG_CAPABILITY)
  2155. {
  2156. if (BGP_DEBUG (normal, NORMAL))
  2157. plog_debug (peer->log,
  2158. "%s unknown message type 0x%02x",
  2159. peer->host, type);
  2160. bgp_notify_send_with_data (peer,
  2161. BGP_NOTIFY_HEADER_ERR,
  2162. BGP_NOTIFY_HEADER_BAD_MESTYPE,
  2163. &type, 1);
  2164. goto done;
  2165. }
  2166. /* Mimimum packet length check. */
  2167. if ((size < BGP_HEADER_SIZE)
  2168. || (size > BGP_MAX_PACKET_SIZE)
  2169. || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE)
  2170. || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE)
  2171. || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE)
  2172. || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE)
  2173. || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
  2174. || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE)
  2175. || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE))
  2176. {
  2177. if (BGP_DEBUG (normal, NORMAL))
  2178. plog_debug (peer->log,
  2179. "%s bad message length - %d for %s",
  2180. peer->host, size,
  2181. type == 128 ? "ROUTE-REFRESH" :
  2182. bgp_type_str[(int) type]);
  2183. bgp_notify_send_with_data (peer,
  2184. BGP_NOTIFY_HEADER_ERR,
  2185. BGP_NOTIFY_HEADER_BAD_MESLEN,
  2186. (u_char *) notify_data_length, 2);
  2187. goto done;
  2188. }
  2189. /* Adjust size to message length. */
  2190. peer->packet_size = size;
  2191. }
  2192. ret = bgp_read_packet (peer);
  2193. if (ret < 0)
  2194. goto done;
  2195. /* Get size and type again. */
  2196. size = stream_getw_from (peer->ibuf, BGP_MARKER_SIZE);
  2197. type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2);
  2198. /* BGP packet dump function. */
  2199. bgp_dump_packet (peer, type, peer->ibuf);
  2200. size = (peer->packet_size - BGP_HEADER_SIZE);
  2201. /* Read rest of the packet and call each sort of packet routine */
  2202. switch (type)
  2203. {
  2204. case BGP_MSG_OPEN:
  2205. peer->open_in++;
  2206. bgp_open_receive (peer, size); /* XXX return value ignored! */
  2207. break;
  2208. case BGP_MSG_UPDATE:
  2209. peer->readtime = bgp_recent_clock ();
  2210. bgp_update_receive (peer, size);
  2211. break;
  2212. case BGP_MSG_NOTIFY:
  2213. bgp_notify_receive (peer, size);
  2214. break;
  2215. case BGP_MSG_KEEPALIVE:
  2216. peer->readtime = bgp_recent_clock ();
  2217. bgp_keepalive_receive (peer, size);
  2218. break;
  2219. case BGP_MSG_ROUTE_REFRESH_NEW:
  2220. case BGP_MSG_ROUTE_REFRESH_OLD:
  2221. peer->refresh_in++;
  2222. bgp_route_refresh_receive (peer, size);
  2223. break;
  2224. case BGP_MSG_CAPABILITY:
  2225. peer->dynamic_cap_in++;
  2226. bgp_capability_receive (peer, size);
  2227. break;
  2228. }
  2229. /* Clear input buffer. */
  2230. peer->packet_size = 0;
  2231. if (peer->ibuf)
  2232. stream_reset (peer->ibuf);
  2233. done:
  2234. if (CHECK_FLAG (peer->sflags, PEER_STATUS_ACCEPT_PEER))
  2235. {
  2236. if (BGP_DEBUG (events, EVENTS))
  2237. zlog_debug ("%s [Event] Accepting BGP peer delete", peer->host);
  2238. peer_delete (peer);
  2239. }
  2240. return 0;
  2241. }