bgp_packet.c 65 KB

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