bgp_packet.c 74 KB

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