bgp_packet.c 67 KB

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