bgp_packet.c 62 KB

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