bgp_packet.c 72 KB

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