bgp_packet.c 72 KB

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