]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* BGP packet management routine. |
56257a44 QY |
2 | * Contains utility functions for constructing and consuming BGP messages. |
3 | * Copyright (C) 2017 Cumulus Networks | |
896014f4 DL |
4 | * Copyright (C) 1999 Kunihiro Ishiguro |
5 | * | |
6 | * This file is part of GNU Zebra. | |
7 | * | |
8 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
9 | * under the terms of the GNU General Public License as published by the | |
10 | * Free Software Foundation; either version 2, or (at your option) any | |
11 | * later version. | |
12 | * | |
13 | * GNU Zebra is distributed in the hope that it will be useful, but | |
14 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | * General Public License for more details. | |
17 | * | |
18 | * You should have received a copy of the GNU General Public License along | |
19 | * with this program; see the file COPYING; if not, write to the Free Software | |
20 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
21 | */ | |
718e3744 | 22 | |
23 | #include <zebra.h> | |
d3ecc69e | 24 | #include <sys/time.h> |
718e3744 | 25 | |
26 | #include "thread.h" | |
27 | #include "stream.h" | |
28 | #include "network.h" | |
29 | #include "prefix.h" | |
30 | #include "command.h" | |
31 | #include "log.h" | |
32 | #include "memory.h" | |
d62a17ae | 33 | #include "sockunion.h" /* for inet_ntop () */ |
baa376fc | 34 | #include "sockopt.h" |
718e3744 | 35 | #include "linklist.h" |
36 | #include "plist.h" | |
3f9c7369 | 37 | #include "queue.h" |
039f3a34 | 38 | #include "filter.h" |
02705213 | 39 | #include "lib_errors.h" |
718e3744 | 40 | |
41 | #include "bgpd/bgpd.h" | |
42 | #include "bgpd/bgp_table.h" | |
43 | #include "bgpd/bgp_dump.h" | |
6c29258c | 44 | #include "bgpd/bgp_bmp.h" |
718e3744 | 45 | #include "bgpd/bgp_attr.h" |
46 | #include "bgpd/bgp_debug.h" | |
14454c9f | 47 | #include "bgpd/bgp_errors.h" |
718e3744 | 48 | #include "bgpd/bgp_fsm.h" |
49 | #include "bgpd/bgp_route.h" | |
50 | #include "bgpd/bgp_packet.h" | |
51 | #include "bgpd/bgp_open.h" | |
52 | #include "bgpd/bgp_aspath.h" | |
53 | #include "bgpd/bgp_community.h" | |
54 | #include "bgpd/bgp_ecommunity.h" | |
57d187bc | 55 | #include "bgpd/bgp_lcommunity.h" |
718e3744 | 56 | #include "bgpd/bgp_network.h" |
57 | #include "bgpd/bgp_mplsvpn.h" | |
7ef5a232 | 58 | #include "bgpd/bgp_evpn.h" |
718e3744 | 59 | #include "bgpd/bgp_advertise.h" |
93406d87 | 60 | #include "bgpd/bgp_vty.h" |
3f9c7369 | 61 | #include "bgpd/bgp_updgrp.h" |
cd1964ff | 62 | #include "bgpd/bgp_label.h" |
56257a44 | 63 | #include "bgpd/bgp_io.h" |
934af458 | 64 | #include "bgpd/bgp_keepalives.h" |
7c40bf39 | 65 | #include "bgpd/bgp_flowspec.h" |
718e3744 | 66 | |
584470fb DL |
67 | DEFINE_HOOK(bgp_packet_dump, |
68 | (struct peer *peer, uint8_t type, bgp_size_t size, | |
69 | struct stream *s), | |
70 | (peer, type, size, s)) | |
71 | ||
6fd04594 DL |
72 | DEFINE_HOOK(bgp_packet_send, |
73 | (struct peer *peer, uint8_t type, bgp_size_t size, | |
74 | struct stream *s), | |
75 | (peer, type, size, s)) | |
76 | ||
d8151687 QY |
77 | /** |
78 | * Sets marker and type fields for a BGP message. | |
79 | * | |
80 | * @param s the stream containing the packet | |
81 | * @param type the packet type | |
82 | * @return the size of the stream | |
83 | */ | |
d7c0a89a | 84 | int bgp_packet_set_marker(struct stream *s, uint8_t type) |
718e3744 | 85 | { |
d62a17ae | 86 | int i; |
718e3744 | 87 | |
d62a17ae | 88 | /* Fill in marker. */ |
89 | for (i = 0; i < BGP_MARKER_SIZE; i++) | |
90 | stream_putc(s, 0xff); | |
718e3744 | 91 | |
d62a17ae | 92 | /* Dummy total length. This field is should be filled in later on. */ |
93 | stream_putw(s, 0); | |
718e3744 | 94 | |
d62a17ae | 95 | /* BGP packet type. */ |
96 | stream_putc(s, type); | |
718e3744 | 97 | |
d62a17ae | 98 | /* Return current stream size. */ |
99 | return stream_get_endp(s); | |
718e3744 | 100 | } |
101 | ||
d8151687 QY |
102 | /** |
103 | * Sets size field for a BGP message. | |
104 | * | |
105 | * Size field is set to the size of the stream passed. | |
106 | * | |
107 | * @param s the stream containing the packet | |
108 | * @return the size of the stream | |
109 | */ | |
d62a17ae | 110 | int bgp_packet_set_size(struct stream *s) |
718e3744 | 111 | { |
d62a17ae | 112 | int cp; |
718e3744 | 113 | |
d62a17ae | 114 | /* Preserve current pointer. */ |
115 | cp = stream_get_endp(s); | |
116 | stream_putw_at(s, BGP_MARKER_SIZE, cp); | |
718e3744 | 117 | |
d62a17ae | 118 | return cp; |
718e3744 | 119 | } |
120 | ||
d3ecc69e QY |
121 | /* |
122 | * Push a packet onto the beginning of the peer's output queue. | |
123 | * This function acquires the peer's write mutex before proceeding. | |
124 | */ | |
125 | static void bgp_packet_add(struct peer *peer, struct stream *s) | |
126 | { | |
00dffa8c DL |
127 | frr_with_mutex(&peer->io_mtx) { |
128 | stream_fifo_push(peer->obuf, s); | |
129 | } | |
718e3744 | 130 | } |
131 | ||
d62a17ae | 132 | static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi, |
133 | safi_t safi) | |
93406d87 | 134 | { |
d62a17ae | 135 | struct stream *s; |
136 | iana_afi_t pkt_afi; | |
5c525538 | 137 | iana_safi_t pkt_safi; |
d62a17ae | 138 | |
139 | if (DISABLE_BGP_ANNOUNCE) | |
140 | return NULL; | |
141 | ||
142 | if (bgp_debug_neighbor_events(peer)) | |
143 | zlog_debug("send End-of-RIB for %s to %s", | |
5cb5f4d0 | 144 | get_afi_safi_str(afi, safi, false), peer->host); |
d62a17ae | 145 | |
146 | s = stream_new(BGP_MAX_PACKET_SIZE); | |
147 | ||
148 | /* Make BGP update packet. */ | |
149 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
150 | ||
151 | /* Unfeasible Routes Length */ | |
152 | stream_putw(s, 0); | |
153 | ||
154 | if (afi == AFI_IP && safi == SAFI_UNICAST) { | |
155 | /* Total Path Attribute Length */ | |
156 | stream_putw(s, 0); | |
157 | } else { | |
158 | /* Convert AFI, SAFI to values for packet. */ | |
159 | bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi); | |
160 | ||
161 | /* Total Path Attribute Length */ | |
162 | stream_putw(s, 6); | |
163 | stream_putc(s, BGP_ATTR_FLAG_OPTIONAL); | |
164 | stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI); | |
165 | stream_putc(s, 3); | |
166 | stream_putw(s, pkt_afi); | |
167 | stream_putc(s, pkt_safi); | |
168 | } | |
169 | ||
170 | bgp_packet_set_size(s); | |
d62a17ae | 171 | return s; |
718e3744 | 172 | } |
173 | ||
d8151687 QY |
174 | /* Called when there is a change in the EOR(implicit or explicit) status of a |
175 | * peer. Ends the update-delay if all expected peers are done with EORs. */ | |
176 | void bgp_check_update_delay(struct bgp *bgp) | |
177 | { | |
178 | struct listnode *node, *nnode; | |
179 | struct peer *peer = NULL; | |
180 | ||
181 | if (bgp_debug_neighbor_events(peer)) | |
182 | zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d", | |
183 | bgp->established, bgp->restarted_peers, | |
184 | bgp->implicit_eors, bgp->explicit_eors); | |
185 | ||
186 | if (bgp->established | |
187 | <= bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) { | |
becedef6 QY |
188 | /* |
189 | * This is an extra sanity check to make sure we wait for all | |
190 | * the eligible configured peers. This check is performed if | |
191 | * establish wait timer is on, or establish wait option is not | |
192 | * given with the update-delay command | |
193 | */ | |
d8151687 QY |
194 | if (bgp->t_establish_wait |
195 | || (bgp->v_establish_wait == bgp->v_update_delay)) | |
196 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
197 | if (CHECK_FLAG(peer->flags, | |
198 | PEER_FLAG_CONFIG_NODE) | |
199 | && !CHECK_FLAG(peer->flags, | |
200 | PEER_FLAG_SHUTDOWN) | |
201 | && !peer->update_delay_over) { | |
202 | if (bgp_debug_neighbor_events(peer)) | |
203 | zlog_debug( | |
204 | " Peer %s pending, continuing read-only mode", | |
205 | peer->host); | |
206 | return; | |
207 | } | |
208 | } | |
209 | ||
210 | zlog_info( | |
211 | "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d", | |
212 | bgp->restarted_peers, bgp->implicit_eors, | |
213 | bgp->explicit_eors); | |
214 | bgp_update_delay_end(bgp); | |
215 | } | |
216 | } | |
217 | ||
becedef6 QY |
218 | /* |
219 | * Called if peer is known to have restarted. The restart-state bit in | |
220 | * Graceful-Restart capability is used for that | |
221 | */ | |
d8151687 QY |
222 | void bgp_update_restarted_peers(struct peer *peer) |
223 | { | |
224 | if (!bgp_update_delay_active(peer->bgp)) | |
225 | return; /* BGP update delay has ended */ | |
226 | if (peer->update_delay_over) | |
227 | return; /* This peer has already been considered */ | |
228 | ||
229 | if (bgp_debug_neighbor_events(peer)) | |
230 | zlog_debug("Peer %s: Checking restarted", peer->host); | |
231 | ||
232 | if (peer->status == Established) { | |
233 | peer->update_delay_over = 1; | |
234 | peer->bgp->restarted_peers++; | |
235 | bgp_check_update_delay(peer->bgp); | |
236 | } | |
237 | } | |
238 | ||
becedef6 QY |
239 | /* |
240 | * Called as peer receives a keep-alive. Determines if this occurence can be | |
241 | * taken as an implicit EOR for this peer. | |
242 | * NOTE: The very first keep-alive after the Established state of a peer is | |
243 | * considered implicit EOR for the update-delay purposes | |
244 | */ | |
d8151687 QY |
245 | void bgp_update_implicit_eors(struct peer *peer) |
246 | { | |
247 | if (!bgp_update_delay_active(peer->bgp)) | |
248 | return; /* BGP update delay has ended */ | |
249 | if (peer->update_delay_over) | |
250 | return; /* This peer has already been considered */ | |
251 | ||
252 | if (bgp_debug_neighbor_events(peer)) | |
253 | zlog_debug("Peer %s: Checking implicit EORs", peer->host); | |
254 | ||
255 | if (peer->status == Established) { | |
256 | peer->update_delay_over = 1; | |
257 | peer->bgp->implicit_eors++; | |
258 | bgp_check_update_delay(peer->bgp); | |
259 | } | |
260 | } | |
261 | ||
becedef6 QY |
262 | /* |
263 | * Should be called only when there is a change in the EOR_RECEIVED status | |
264 | * for any afi/safi on a peer. | |
265 | */ | |
d8151687 QY |
266 | static void bgp_update_explicit_eors(struct peer *peer) |
267 | { | |
268 | afi_t afi; | |
269 | safi_t safi; | |
270 | ||
271 | if (!bgp_update_delay_active(peer->bgp)) | |
272 | return; /* BGP update delay has ended */ | |
273 | if (peer->update_delay_over) | |
274 | return; /* This peer has already been considered */ | |
275 | ||
276 | if (bgp_debug_neighbor_events(peer)) | |
277 | zlog_debug("Peer %s: Checking explicit EORs", peer->host); | |
278 | ||
279 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
280 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) { | |
281 | if (peer->afc_nego[afi][safi] | |
282 | && !CHECK_FLAG(peer->af_sflags[afi][safi], | |
283 | PEER_STATUS_EOR_RECEIVED)) { | |
284 | if (bgp_debug_neighbor_events(peer)) | |
285 | zlog_debug( | |
0437e105 | 286 | " afi %d safi %d didn't receive EOR", |
d8151687 QY |
287 | afi, safi); |
288 | return; | |
289 | } | |
290 | } | |
291 | ||
292 | peer->update_delay_over = 1; | |
293 | peer->bgp->explicit_eors++; | |
294 | bgp_check_update_delay(peer->bgp); | |
295 | } | |
296 | ||
297 | /** | |
298 | * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers. | |
299 | * | |
300 | * mp_withdraw, if set, is used to nullify attr structure on most of the | |
301 | * calling safi function and for evpn, passed as parameter | |
302 | */ | |
303 | int bgp_nlri_parse(struct peer *peer, struct attr *attr, | |
304 | struct bgp_nlri *packet, int mp_withdraw) | |
305 | { | |
306 | switch (packet->safi) { | |
307 | case SAFI_UNICAST: | |
308 | case SAFI_MULTICAST: | |
309 | return bgp_nlri_parse_ip(peer, mp_withdraw ? NULL : attr, | |
310 | packet); | |
311 | case SAFI_LABELED_UNICAST: | |
312 | return bgp_nlri_parse_label(peer, mp_withdraw ? NULL : attr, | |
313 | packet); | |
314 | case SAFI_MPLS_VPN: | |
315 | return bgp_nlri_parse_vpn(peer, mp_withdraw ? NULL : attr, | |
316 | packet); | |
317 | case SAFI_EVPN: | |
318 | return bgp_nlri_parse_evpn(peer, attr, packet, mp_withdraw); | |
7c40bf39 | 319 | case SAFI_FLOWSPEC: |
320 | return bgp_nlri_parse_flowspec(peer, attr, packet, mp_withdraw); | |
d8151687 | 321 | } |
513386b5 | 322 | return BGP_NLRI_PARSE_ERROR; |
d8151687 QY |
323 | } |
324 | ||
6ec98a2f QY |
325 | /* |
326 | * Checks a variety of conditions to determine whether the peer needs to be | |
327 | * rescheduled for packet generation again, and does so if necessary. | |
328 | * | |
329 | * @param peer to check for rescheduling | |
330 | */ | |
af1e1dc6 QY |
331 | static void bgp_write_proceed_actions(struct peer *peer) |
332 | { | |
333 | afi_t afi; | |
334 | safi_t safi; | |
335 | struct peer_af *paf; | |
336 | struct bpacket *next_pkt; | |
337 | struct update_subgroup *subgrp; | |
338 | ||
c58b0f46 QY |
339 | FOREACH_AFI_SAFI (afi, safi) { |
340 | paf = peer_af_find(peer, afi, safi); | |
341 | if (!paf) | |
342 | continue; | |
343 | subgrp = paf->subgroup; | |
344 | if (!subgrp) | |
345 | continue; | |
346 | ||
347 | next_pkt = paf->next_pkt_to_send; | |
348 | if (next_pkt && next_pkt->buffer) { | |
349 | BGP_TIMER_ON(peer->t_generate_updgrp_packets, | |
350 | bgp_generate_updgrp_packets, 0); | |
351 | return; | |
352 | } | |
af1e1dc6 | 353 | |
c58b0f46 QY |
354 | /* No packets readily available for AFI/SAFI, are there |
355 | * subgroup packets | |
356 | * that need to be generated? */ | |
357 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), | |
358 | SUBGRP_PKTQ(subgrp)) | |
359 | || subgroup_packets_to_build(subgrp)) { | |
360 | BGP_TIMER_ON(peer->t_generate_updgrp_packets, | |
361 | bgp_generate_updgrp_packets, 0); | |
362 | return; | |
363 | } | |
af1e1dc6 | 364 | |
c58b0f46 QY |
365 | /* No packets to send, see if EOR is pending */ |
366 | if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)) { | |
367 | if (!subgrp->t_coalesce && peer->afc_nego[afi][safi] | |
368 | && peer->synctime | |
369 | && !CHECK_FLAG(peer->af_sflags[afi][safi], | |
370 | PEER_STATUS_EOR_SEND) | |
371 | && safi != SAFI_MPLS_VPN) { | |
af1e1dc6 QY |
372 | BGP_TIMER_ON(peer->t_generate_updgrp_packets, |
373 | bgp_generate_updgrp_packets, 0); | |
374 | return; | |
375 | } | |
af1e1dc6 | 376 | } |
c58b0f46 | 377 | } |
af1e1dc6 QY |
378 | } |
379 | ||
6ec98a2f QY |
380 | /* |
381 | * Generate advertisement information (withdraws, updates, EOR) from each | |
382 | * update group a peer belongs to, encode this information into packets, and | |
383 | * enqueue the packets onto the peer's output buffer. | |
56257a44 QY |
384 | */ |
385 | int bgp_generate_updgrp_packets(struct thread *thread) | |
718e3744 | 386 | { |
56257a44 | 387 | struct peer *peer = THREAD_ARG(thread); |
56257a44 QY |
388 | |
389 | struct stream *s; | |
d62a17ae | 390 | struct peer_af *paf; |
391 | struct bpacket *next_pkt; | |
d0ad6d8e QY |
392 | uint32_t wpq; |
393 | uint32_t generated = 0; | |
d62a17ae | 394 | afi_t afi; |
395 | safi_t safi; | |
396 | ||
d0ad6d8e QY |
397 | wpq = atomic_load_explicit(&peer->bgp->wpkt_quanta, |
398 | memory_order_relaxed); | |
399 | ||
d62a17ae | 400 | /* |
401 | * The code beyond this part deals with update packets, proceed only | |
402 | * if peer is Established and updates are not on hold (as part of | |
403 | * update-delay post processing). | |
3f9c7369 | 404 | */ |
d62a17ae | 405 | if (peer->status != Established) |
56257a44 | 406 | return 0; |
d62a17ae | 407 | |
5410015a | 408 | if (peer->bgp->main_peers_update_hold) |
56257a44 | 409 | return 0; |
d62a17ae | 410 | |
b10b6d52 RW |
411 | if (peer->t_routeadv) |
412 | return 0; | |
413 | ||
56257a44 QY |
414 | do { |
415 | s = NULL; | |
c58b0f46 QY |
416 | FOREACH_AFI_SAFI (afi, safi) { |
417 | paf = peer_af_find(peer, afi, safi); | |
418 | if (!paf || !PAF_SUBGRP(paf)) | |
419 | continue; | |
420 | next_pkt = paf->next_pkt_to_send; | |
80bd61c4 | 421 | |
c58b0f46 QY |
422 | /* |
423 | * Try to generate a packet for the peer if we are at | |
424 | * the end of the list. Always try to push out | |
425 | * WITHDRAWs first. | |
426 | */ | |
427 | if (!next_pkt || !next_pkt->buffer) { | |
428 | next_pkt = subgroup_withdraw_packet( | |
429 | PAF_SUBGRP(paf)); | |
430 | if (!next_pkt || !next_pkt->buffer) | |
431 | subgroup_update_packet(PAF_SUBGRP(paf)); | |
432 | next_pkt = paf->next_pkt_to_send; | |
433 | } | |
80bd61c4 | 434 | |
c58b0f46 QY |
435 | /* |
436 | * If we still don't have a packet to send to the peer, | |
437 | * then try to find out out if we have to send eor or | |
438 | * if not, skip to the next AFI, SAFI. Don't send the | |
439 | * EOR prematurely; if the subgroup's coalesce timer is | |
440 | * running, the adjacency-out structure is not created | |
441 | * yet. | |
442 | */ | |
443 | if (!next_pkt || !next_pkt->buffer) { | |
444 | if (CHECK_FLAG(peer->cap, | |
36235319 | 445 | PEER_CAP_RESTART_RCV)) { |
c58b0f46 | 446 | if (!(PAF_SUBGRP(paf))->t_coalesce |
36235319 QY |
447 | && peer->afc_nego[afi][safi] |
448 | && peer->synctime | |
449 | && !CHECK_FLAG( | |
450 | peer->af_sflags[afi][safi], | |
451 | PEER_STATUS_EOR_SEND)) { | |
d6e3c15b | 452 | /* If EOR is disabled, |
453 | * the message is not sent | |
454 | */ | |
36235319 QY |
455 | if (BGP_SEND_EOR(peer->bgp, afi, |
456 | safi)) { | |
d6e3c15b | 457 | SET_FLAG( |
36235319 QY |
458 | peer->af_sflags |
459 | [afi] | |
460 | [safi], | |
461 | PEER_STATUS_EOR_SEND); | |
d6e3c15b | 462 | |
9e3b51a7 | 463 | /* Update EOR |
464 | * send time | |
465 | */ | |
36235319 QY |
466 | peer->eor_stime[afi] |
467 | [safi] = | |
468 | monotime(NULL); | |
9e3b51a7 | 469 | |
d6e3c15b | 470 | BGP_UPDATE_EOR_PKT( |
36235319 QY |
471 | peer, afi, safi, |
472 | s); | |
56257a44 | 473 | } |
80bd61c4 | 474 | } |
d62a17ae | 475 | } |
c58b0f46 QY |
476 | continue; |
477 | } | |
9e3b51a7 | 478 | |
479 | /* Update packet send time */ | |
480 | peer->pkt_stime[afi][safi] = monotime(NULL); | |
481 | ||
c58b0f46 QY |
482 | /* Found a packet template to send, overwrite |
483 | * packet with appropriate attributes from peer | |
484 | * and advance peer */ | |
485 | s = bpacket_reformat_for_peer(next_pkt, paf); | |
486 | bgp_packet_add(peer, s); | |
c58b0f46 QY |
487 | bpacket_queue_advance_peer(paf); |
488 | } | |
d0ad6d8e | 489 | } while (s && (++generated < wpq)); |
80bd61c4 | 490 | |
6ec98a2f QY |
491 | if (generated) |
492 | bgp_writes_on(peer); | |
493 | ||
af1e1dc6 QY |
494 | bgp_write_proceed_actions(peer); |
495 | ||
80bd61c4 QY |
496 | return 0; |
497 | } | |
498 | ||
d3ecc69e QY |
499 | /* |
500 | * Creates a BGP Keepalive packet and appends it to the peer's output queue. | |
501 | */ | |
d62a17ae | 502 | void bgp_keepalive_send(struct peer *peer) |
718e3744 | 503 | { |
d62a17ae | 504 | struct stream *s; |
505 | ||
506 | s = stream_new(BGP_MAX_PACKET_SIZE); | |
718e3744 | 507 | |
d62a17ae | 508 | /* Make keepalive packet. */ |
509 | bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE); | |
718e3744 | 510 | |
d62a17ae | 511 | /* Set packet size. */ |
512 | (void)bgp_packet_set_size(s); | |
718e3744 | 513 | |
d62a17ae | 514 | /* Dump packet if debug option is set. */ |
515 | /* bgp_packet_dump (s); */ | |
718e3744 | 516 | |
d62a17ae | 517 | if (bgp_debug_keepalive(peer)) |
518 | zlog_debug("%s sending KEEPALIVE", peer->host); | |
718e3744 | 519 | |
d62a17ae | 520 | /* Add packet to the peer. */ |
521 | bgp_packet_add(peer, s); | |
424ab01d QY |
522 | |
523 | bgp_writes_on(peer); | |
718e3744 | 524 | } |
525 | ||
d3ecc69e QY |
526 | /* |
527 | * Creates a BGP Open packet and appends it to the peer's output queue. | |
528 | * Sets capabilities as necessary. | |
529 | */ | |
d62a17ae | 530 | void bgp_open_send(struct peer *peer) |
718e3744 | 531 | { |
d62a17ae | 532 | struct stream *s; |
d7c0a89a | 533 | uint16_t send_holdtime; |
d62a17ae | 534 | as_t local_as; |
718e3744 | 535 | |
b90a8e13 | 536 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) |
d62a17ae | 537 | send_holdtime = peer->holdtime; |
538 | else | |
539 | send_holdtime = peer->bgp->default_holdtime; | |
718e3744 | 540 | |
d62a17ae | 541 | /* local-as Change */ |
542 | if (peer->change_local_as) | |
543 | local_as = peer->change_local_as; | |
544 | else | |
545 | local_as = peer->local_as; | |
718e3744 | 546 | |
d62a17ae | 547 | s = stream_new(BGP_MAX_PACKET_SIZE); |
718e3744 | 548 | |
d62a17ae | 549 | /* Make open packet. */ |
550 | bgp_packet_set_marker(s, BGP_MSG_OPEN); | |
718e3744 | 551 | |
d62a17ae | 552 | /* Set open packet values. */ |
553 | stream_putc(s, BGP_VERSION_4); /* BGP version */ | |
d7c0a89a QY |
554 | stream_putw(s, (local_as <= BGP_AS_MAX) ? (uint16_t)local_as |
555 | : BGP_AS_TRANS); | |
d62a17ae | 556 | stream_putw(s, send_holdtime); /* Hold Time */ |
557 | stream_put_in_addr(s, &peer->local_id); /* BGP Identifier */ | |
718e3744 | 558 | |
d62a17ae | 559 | /* Set capability code. */ |
560 | bgp_open_capability(s, peer); | |
718e3744 | 561 | |
d62a17ae | 562 | /* Set BGP packet length. */ |
563 | (void)bgp_packet_set_size(s); | |
718e3744 | 564 | |
d62a17ae | 565 | if (bgp_debug_neighbor_events(peer)) |
566 | zlog_debug( | |
567 | "%s sending OPEN, version %d, my as %u, holdtime %d, id %s", | |
568 | peer->host, BGP_VERSION_4, local_as, send_holdtime, | |
569 | inet_ntoa(peer->local_id)); | |
718e3744 | 570 | |
d62a17ae | 571 | /* Dump packet if debug option is set. */ |
572 | /* bgp_packet_dump (s); */ | |
6fd04594 | 573 | hook_call(bgp_packet_send, peer, BGP_MSG_OPEN, stream_get_endp(s), s); |
718e3744 | 574 | |
d62a17ae | 575 | /* Add packet to the peer. */ |
576 | bgp_packet_add(peer, s); | |
424ab01d QY |
577 | |
578 | bgp_writes_on(peer); | |
579 | } | |
580 | ||
a127f33b QY |
581 | /* |
582 | * Writes NOTIFICATION message directly to a peer socket without waiting for | |
583 | * the I/O thread. | |
584 | * | |
585 | * There must be exactly one stream on the peer->obuf FIFO, and the data within | |
586 | * this stream must match the format of a BGP NOTIFICATION message. | |
587 | * Transmission is best-effort. | |
588 | * | |
589 | * @requires peer->io_mtx | |
590 | * @param peer | |
591 | * @return 0 | |
592 | */ | |
3dc339cd | 593 | static void bgp_write_notify(struct peer *peer) |
424ab01d QY |
594 | { |
595 | int ret, val; | |
d7c0a89a | 596 | uint8_t type; |
424ab01d QY |
597 | struct stream *s; |
598 | ||
a127f33b QY |
599 | /* There should be at least one packet. */ |
600 | s = stream_fifo_pop(peer->obuf); | |
424ab01d | 601 | |
8ec586b0 | 602 | if (!s) |
3dc339cd | 603 | return; |
8ec586b0 QY |
604 | |
605 | assert(stream_get_endp(s) >= BGP_HEADER_SIZE); | |
606 | ||
424ab01d QY |
607 | /* Stop collecting data within the socket */ |
608 | sockopt_cork(peer->fd, 0); | |
609 | ||
becedef6 QY |
610 | /* |
611 | * socket is in nonblocking mode, if we can't deliver the NOTIFY, well, | |
612 | * we only care about getting a clean shutdown at this point. | |
613 | */ | |
424ab01d QY |
614 | ret = write(peer->fd, STREAM_DATA(s), stream_get_endp(s)); |
615 | ||
becedef6 QY |
616 | /* |
617 | * only connection reset/close gets counted as TCP_fatal_error, failure | |
618 | * to write the entire NOTIFY doesn't get different FSM treatment | |
619 | */ | |
424ab01d | 620 | if (ret <= 0) { |
3735936b | 621 | stream_free(s); |
424ab01d | 622 | BGP_EVENT_ADD(peer, TCP_fatal_error); |
3dc339cd | 623 | return; |
424ab01d QY |
624 | } |
625 | ||
626 | /* Disable Nagle, make NOTIFY packet go out right away */ | |
627 | val = 1; | |
628 | (void)setsockopt(peer->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, | |
629 | sizeof(val)); | |
630 | ||
631 | /* Retrieve BGP packet type. */ | |
632 | stream_set_getp(s, BGP_MARKER_SIZE + 2); | |
633 | type = stream_getc(s); | |
634 | ||
635 | assert(type == BGP_MSG_NOTIFY); | |
636 | ||
637 | /* Type should be notify. */ | |
0112e9e0 | 638 | atomic_fetch_add_explicit(&peer->notify_out, 1, memory_order_relaxed); |
424ab01d QY |
639 | peer->notify_out++; |
640 | ||
641 | /* Double start timer. */ | |
642 | peer->v_start *= 2; | |
643 | ||
644 | /* Overflow check. */ | |
645 | if (peer->v_start >= (60 * 2)) | |
646 | peer->v_start = (60 * 2); | |
647 | ||
becedef6 QY |
648 | /* |
649 | * Handle Graceful Restart case where the state changes to | |
650 | * Connect instead of Idle | |
651 | */ | |
424ab01d QY |
652 | BGP_EVENT_ADD(peer, BGP_Stop); |
653 | ||
3735936b | 654 | stream_free(s); |
718e3744 | 655 | } |
656 | ||
d3ecc69e QY |
657 | /* |
658 | * Creates a BGP Notify and appends it to the peer's output queue. | |
659 | * | |
becedef6 QY |
660 | * This function attempts to write the packet from the thread it is called |
661 | * from, to ensure the packet gets out ASAP. | |
d3ecc69e | 662 | * |
a127f33b QY |
663 | * This function may be called from multiple threads. Since the function |
664 | * modifies I/O buffer(s) in the peer, these are locked for the duration of the | |
665 | * call to prevent tampering from other threads. | |
666 | * | |
667 | * Delivery of the NOTIFICATION is attempted once and is best-effort. After | |
668 | * return, the peer structure *must* be reset; no assumptions about session | |
669 | * state are valid. | |
670 | * | |
d3ecc69e QY |
671 | * @param peer |
672 | * @param code BGP error code | |
673 | * @param sub_code BGP error subcode | |
674 | * @param data Data portion | |
675 | * @param datalen length of data portion | |
676 | */ | |
d7c0a89a QY |
677 | void bgp_notify_send_with_data(struct peer *peer, uint8_t code, |
678 | uint8_t sub_code, uint8_t *data, size_t datalen) | |
718e3744 | 679 | { |
d62a17ae | 680 | struct stream *s; |
d62a17ae | 681 | |
a127f33b | 682 | /* Lock I/O mutex to prevent other threads from pushing packets */ |
00dffa8c | 683 | frr_mutex_lock_autounlock(&peer->io_mtx); |
a127f33b QY |
684 | /* ============================================== */ |
685 | ||
d62a17ae | 686 | /* Allocate new stream. */ |
687 | s = stream_new(BGP_MAX_PACKET_SIZE); | |
688 | ||
d3ecc69e | 689 | /* Make notify packet. */ |
d62a17ae | 690 | bgp_packet_set_marker(s, BGP_MSG_NOTIFY); |
691 | ||
692 | /* Set notify packet values. */ | |
693 | stream_putc(s, code); /* BGP notify code */ | |
694 | stream_putc(s, sub_code); /* BGP notify sub_code */ | |
695 | ||
696 | /* If notify data is present. */ | |
697 | if (data) | |
698 | stream_write(s, data, datalen); | |
699 | ||
700 | /* Set BGP packet length. */ | |
bd6b2706 | 701 | bgp_packet_set_size(s); |
d62a17ae | 702 | |
424ab01d | 703 | /* wipe output buffer */ |
a127f33b | 704 | stream_fifo_clean(peer->obuf); |
d62a17ae | 705 | |
becedef6 QY |
706 | /* |
707 | * If possible, store last packet for debugging purposes. This check is | |
708 | * in place because we are sometimes called with a doppelganger peer, | |
709 | * who tends to have a plethora of fields nulled out. | |
710 | */ | |
1a1f4534 | 711 | if (peer->curr) { |
d8151687 | 712 | size_t packetsize = stream_get_endp(peer->curr); |
1a1f4534 | 713 | assert(packetsize <= sizeof(peer->last_reset_cause)); |
d8151687 QY |
714 | memcpy(peer->last_reset_cause, peer->curr->data, packetsize); |
715 | peer->last_reset_cause_size = packetsize; | |
716 | } | |
717 | ||
d62a17ae | 718 | /* For debug */ |
719 | { | |
720 | struct bgp_notify bgp_notify; | |
721 | int first = 0; | |
722 | int i; | |
723 | char c[4]; | |
724 | ||
725 | bgp_notify.code = code; | |
726 | bgp_notify.subcode = sub_code; | |
727 | bgp_notify.data = NULL; | |
e0981960 | 728 | bgp_notify.length = datalen; |
d62a17ae | 729 | bgp_notify.raw_data = data; |
730 | ||
731 | peer->notify.code = bgp_notify.code; | |
732 | peer->notify.subcode = bgp_notify.subcode; | |
733 | ||
e0981960 | 734 | if (bgp_notify.length && data) { |
d62a17ae | 735 | bgp_notify.data = |
736 | XMALLOC(MTYPE_TMP, bgp_notify.length * 3); | |
737 | for (i = 0; i < bgp_notify.length; i++) | |
738 | if (first) { | |
552d6491 QY |
739 | snprintf(c, sizeof(c), " %02x", |
740 | data[i]); | |
f009ff26 | 741 | |
552d6491 | 742 | strlcat(bgp_notify.data, c, |
2ba1fe69 | 743 | bgp_notify.length); |
f009ff26 | 744 | |
d62a17ae | 745 | } else { |
746 | first = 1; | |
552d6491 | 747 | snprintf(c, sizeof(c), "%02x", data[i]); |
f009ff26 | 748 | |
552d6491 | 749 | strlcpy(bgp_notify.data, c, |
2ba1fe69 | 750 | bgp_notify.length); |
d62a17ae | 751 | } |
752 | } | |
753 | bgp_notify_print(peer, &bgp_notify, "sending"); | |
754 | ||
755 | if (bgp_notify.data) { | |
756 | XFREE(MTYPE_TMP, bgp_notify.data); | |
d62a17ae | 757 | bgp_notify.length = 0; |
758 | } | |
759 | } | |
760 | ||
761 | /* peer reset cause */ | |
762 | if (code == BGP_NOTIFY_CEASE) { | |
763 | if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET) | |
764 | peer->last_reset = PEER_DOWN_USER_RESET; | |
765 | else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN) | |
766 | peer->last_reset = PEER_DOWN_USER_SHUTDOWN; | |
767 | else | |
768 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; | |
769 | } else | |
770 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; | |
771 | ||
d3ecc69e | 772 | /* Add packet to peer's output queue */ |
a127f33b | 773 | stream_fifo_push(peer->obuf, s); |
424ab01d | 774 | |
5cce3f05 | 775 | bgp_peer_gr_flags_update(peer); |
36235319 QY |
776 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp, |
777 | peer->bgp->peer); | |
5cce3f05 | 778 | |
424ab01d | 779 | bgp_write_notify(peer); |
718e3744 | 780 | } |
781 | ||
d3ecc69e QY |
782 | /* |
783 | * Creates a BGP Notify and appends it to the peer's output queue. | |
784 | * | |
becedef6 QY |
785 | * This function attempts to write the packet from the thread it is called |
786 | * from, to ensure the packet gets out ASAP. | |
d3ecc69e QY |
787 | * |
788 | * @param peer | |
789 | * @param code BGP error code | |
790 | * @param sub_code BGP error subcode | |
791 | */ | |
d7c0a89a | 792 | void bgp_notify_send(struct peer *peer, uint8_t code, uint8_t sub_code) |
718e3744 | 793 | { |
d62a17ae | 794 | bgp_notify_send_with_data(peer, code, sub_code, NULL, 0); |
718e3744 | 795 | } |
796 | ||
d3ecc69e QY |
797 | /* |
798 | * Creates BGP Route Refresh packet and appends it to the peer's output queue. | |
799 | * | |
800 | * @param peer | |
801 | * @param afi Address Family Identifier | |
802 | * @param safi Subsequent Address Family Identifier | |
803 | * @param orf_type Outbound Route Filtering type | |
804 | * @param when_to_refresh Whether to refresh immediately or defer | |
805 | * @param remove Whether to remove ORF for specified AFI/SAFI | |
806 | */ | |
d62a17ae | 807 | void bgp_route_refresh_send(struct peer *peer, afi_t afi, safi_t safi, |
d7c0a89a QY |
808 | uint8_t orf_type, uint8_t when_to_refresh, |
809 | int remove) | |
718e3744 | 810 | { |
d62a17ae | 811 | struct stream *s; |
812 | struct bgp_filter *filter; | |
813 | int orf_refresh = 0; | |
814 | iana_afi_t pkt_afi; | |
5c525538 | 815 | iana_safi_t pkt_safi; |
d62a17ae | 816 | |
817 | if (DISABLE_BGP_ANNOUNCE) | |
818 | return; | |
819 | ||
820 | filter = &peer->filter[afi][safi]; | |
821 | ||
822 | /* Convert AFI, SAFI to values for packet. */ | |
823 | bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi); | |
824 | ||
825 | s = stream_new(BGP_MAX_PACKET_SIZE); | |
826 | ||
827 | /* Make BGP update packet. */ | |
828 | if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV)) | |
829 | bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_NEW); | |
718e3744 | 830 | else |
d62a17ae | 831 | bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_OLD); |
832 | ||
833 | /* Encode Route Refresh message. */ | |
834 | stream_putw(s, pkt_afi); | |
835 | stream_putc(s, 0); | |
836 | stream_putc(s, pkt_safi); | |
837 | ||
838 | if (orf_type == ORF_TYPE_PREFIX || orf_type == ORF_TYPE_PREFIX_OLD) | |
839 | if (remove || filter->plist[FILTER_IN].plist) { | |
d7c0a89a | 840 | uint16_t orf_len; |
d62a17ae | 841 | unsigned long orfp; |
842 | ||
843 | orf_refresh = 1; | |
844 | stream_putc(s, when_to_refresh); | |
845 | stream_putc(s, orf_type); | |
846 | orfp = stream_get_endp(s); | |
847 | stream_putw(s, 0); | |
848 | ||
849 | if (remove) { | |
850 | UNSET_FLAG(peer->af_sflags[afi][safi], | |
851 | PEER_STATUS_ORF_PREFIX_SEND); | |
852 | stream_putc(s, ORF_COMMON_PART_REMOVE_ALL); | |
853 | if (bgp_debug_neighbor_events(peer)) | |
854 | zlog_debug( | |
748a041f | 855 | "%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s", |
d62a17ae | 856 | peer->host, orf_type, |
857 | (when_to_refresh == REFRESH_DEFER | |
858 | ? "defer" | |
859 | : "immediate"), | |
748a041f DS |
860 | iana_afi2str(pkt_afi), |
861 | iana_safi2str(pkt_safi)); | |
d62a17ae | 862 | } else { |
863 | SET_FLAG(peer->af_sflags[afi][safi], | |
864 | PEER_STATUS_ORF_PREFIX_SEND); | |
865 | prefix_bgp_orf_entry( | |
866 | s, filter->plist[FILTER_IN].plist, | |
867 | ORF_COMMON_PART_ADD, | |
868 | ORF_COMMON_PART_PERMIT, | |
869 | ORF_COMMON_PART_DENY); | |
870 | if (bgp_debug_neighbor_events(peer)) | |
871 | zlog_debug( | |
748a041f | 872 | "%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s", |
d62a17ae | 873 | peer->host, orf_type, |
874 | (when_to_refresh == REFRESH_DEFER | |
875 | ? "defer" | |
876 | : "immediate"), | |
748a041f DS |
877 | iana_afi2str(pkt_afi), |
878 | iana_safi2str(pkt_safi)); | |
d62a17ae | 879 | } |
880 | ||
881 | /* Total ORF Entry Len. */ | |
882 | orf_len = stream_get_endp(s) - orfp - 2; | |
883 | stream_putw_at(s, orfp, orf_len); | |
884 | } | |
885 | ||
886 | /* Set packet size. */ | |
887 | (void)bgp_packet_set_size(s); | |
888 | ||
889 | if (bgp_debug_neighbor_events(peer)) { | |
890 | if (!orf_refresh) | |
748a041f DS |
891 | zlog_debug("%s sending REFRESH_REQ for afi/safi: %s/%s", |
892 | peer->host, iana_afi2str(pkt_afi), | |
893 | iana_safi2str(pkt_safi)); | |
d62a17ae | 894 | } |
895 | ||
896 | /* Add packet to the peer. */ | |
897 | bgp_packet_add(peer, s); | |
424ab01d QY |
898 | |
899 | bgp_writes_on(peer); | |
718e3744 | 900 | } |
901 | ||
d3ecc69e QY |
902 | /* |
903 | * Create a BGP Capability packet and append it to the peer's output queue. | |
904 | * | |
905 | * @param peer | |
906 | * @param afi Address Family Identifier | |
907 | * @param safi Subsequent Address Family Identifier | |
908 | * @param capability_code BGP Capability Code | |
909 | * @param action Set or Remove capability | |
910 | */ | |
d62a17ae | 911 | void bgp_capability_send(struct peer *peer, afi_t afi, safi_t safi, |
912 | int capability_code, int action) | |
718e3744 | 913 | { |
d62a17ae | 914 | struct stream *s; |
915 | iana_afi_t pkt_afi; | |
5c525538 | 916 | iana_safi_t pkt_safi; |
d62a17ae | 917 | |
918 | /* Convert AFI, SAFI to values for packet. */ | |
919 | bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi); | |
920 | ||
921 | s = stream_new(BGP_MAX_PACKET_SIZE); | |
922 | ||
923 | /* Make BGP update packet. */ | |
924 | bgp_packet_set_marker(s, BGP_MSG_CAPABILITY); | |
925 | ||
926 | /* Encode MP_EXT capability. */ | |
927 | if (capability_code == CAPABILITY_CODE_MP) { | |
928 | stream_putc(s, action); | |
929 | stream_putc(s, CAPABILITY_CODE_MP); | |
930 | stream_putc(s, CAPABILITY_CODE_MP_LEN); | |
931 | stream_putw(s, pkt_afi); | |
932 | stream_putc(s, 0); | |
933 | stream_putc(s, pkt_safi); | |
934 | ||
935 | if (bgp_debug_neighbor_events(peer)) | |
936 | zlog_debug( | |
748a041f | 937 | "%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s", |
d62a17ae | 938 | peer->host, |
939 | action == CAPABILITY_ACTION_SET ? "Advertising" | |
940 | : "Removing", | |
748a041f | 941 | iana_afi2str(pkt_afi), iana_safi2str(pkt_safi)); |
d62a17ae | 942 | } |
943 | ||
944 | /* Set packet size. */ | |
945 | (void)bgp_packet_set_size(s); | |
718e3744 | 946 | |
d62a17ae | 947 | /* Add packet to the peer. */ |
948 | bgp_packet_add(peer, s); | |
424ab01d QY |
949 | |
950 | bgp_writes_on(peer); | |
d62a17ae | 951 | } |
718e3744 | 952 | |
d62a17ae | 953 | /* RFC1771 6.8 Connection collision detection. */ |
954 | static int bgp_collision_detect(struct peer *new, struct in_addr remote_id) | |
955 | { | |
956 | struct peer *peer; | |
957 | ||
958 | /* Upon receipt of an OPEN message, the local system must examine | |
959 | all of its connections that are in the OpenConfirm state. A BGP | |
960 | speaker may also examine connections in an OpenSent state if it | |
961 | knows the BGP Identifier of the peer by means outside of the | |
962 | protocol. If among these connections there is a connection to a | |
963 | remote BGP speaker whose BGP Identifier equals the one in the | |
964 | OPEN message, then the local system performs the following | |
965 | collision resolution procedure: */ | |
966 | ||
967 | if ((peer = new->doppelganger) != NULL) { | |
968 | /* Do not accept the new connection in Established or Clearing | |
969 | * states. | |
970 | * Note that a peer GR is handled by closing the existing | |
971 | * connection | |
972 | * upon receipt of new one. | |
973 | */ | |
974 | if (peer->status == Established || peer->status == Clearing) { | |
975 | bgp_notify_send(new, BGP_NOTIFY_CEASE, | |
976 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
95f7965d | 977 | return -1; |
d62a17ae | 978 | } else if ((peer->status == OpenConfirm) |
979 | || (peer->status == OpenSent)) { | |
787c3020 DA |
980 | /* 1. The BGP Identifier of the local system is |
981 | * compared to the BGP Identifier of the remote | |
982 | * system (as specified in the OPEN message). | |
983 | * | |
984 | * If the BGP Identifiers of the peers | |
985 | * involved in the connection collision | |
986 | * are identical, then the connection | |
987 | * initiated by the BGP speaker with the | |
988 | * larger AS number is preserved. | |
989 | */ | |
d62a17ae | 990 | if (ntohl(peer->local_id.s_addr) |
787c3020 DA |
991 | < ntohl(remote_id.s_addr) |
992 | || (ntohl(peer->local_id.s_addr) | |
993 | == ntohl(remote_id.s_addr) | |
994 | && peer->local_as < peer->as)) | |
d62a17ae | 995 | if (!CHECK_FLAG(peer->sflags, |
996 | PEER_STATUS_ACCEPT_PEER)) { | |
997 | /* 2. If the value of the local BGP | |
998 | Identifier is less | |
999 | than the remote one, the local system | |
1000 | closes BGP | |
1001 | connection that already exists (the | |
1002 | one that is | |
1003 | already in the OpenConfirm state), | |
1004 | and accepts BGP | |
1005 | connection initiated by the remote | |
1006 | system. */ | |
1007 | bgp_notify_send( | |
1008 | peer, BGP_NOTIFY_CEASE, | |
1009 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1010 | return 1; | |
1011 | } else { | |
1012 | bgp_notify_send( | |
1013 | new, BGP_NOTIFY_CEASE, | |
1014 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1015 | return -1; | |
1016 | } | |
1017 | else { | |
787c3020 DA |
1018 | if (ntohl(peer->local_id.s_addr) |
1019 | == ntohl(remote_id.s_addr) | |
1020 | && peer->local_as == peer->as) | |
1021 | flog_err( | |
1022 | EC_BGP_ROUTER_ID_SAME, | |
1023 | "Peer's router-id %s is the same as ours", | |
1024 | inet_ntoa(remote_id)); | |
3dbe2b60 | 1025 | |
d62a17ae | 1026 | /* 3. Otherwise, the local system closes newly |
1027 | created | |
1028 | BGP connection (the one associated with the | |
1029 | newly | |
1030 | received OPEN message), and continues to use | |
1031 | the | |
1032 | existing one (the one that is already in the | |
1033 | OpenConfirm state). */ | |
1034 | if (CHECK_FLAG(peer->sflags, | |
1035 | PEER_STATUS_ACCEPT_PEER)) { | |
1036 | bgp_notify_send( | |
1037 | peer, BGP_NOTIFY_CEASE, | |
1038 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1039 | return 1; | |
1040 | } else { | |
1041 | bgp_notify_send( | |
1042 | new, BGP_NOTIFY_CEASE, | |
1043 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1044 | return -1; | |
1045 | } | |
1046 | } | |
1047 | } | |
1048 | } | |
1049 | return 0; | |
1050 | } | |
718e3744 | 1051 | |
d8151687 QY |
1052 | /* Packet processing routines ---------------------------------------------- */ |
1053 | /* | |
1054 | * This is a family of functions designed to be called from | |
1055 | * bgp_process_packet(). These functions all share similar behavior and should | |
1056 | * adhere to the following invariants and restrictions: | |
1057 | * | |
1058 | * Return codes | |
1059 | * ------------ | |
1060 | * The return code of any one of those functions should be one of the FSM event | |
1061 | * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be | |
1062 | * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected | |
1063 | * packet type. For example, bgp_open_receive() should return BGP_Stop upon | |
1064 | * error and Receive_OPEN_message otherwise. | |
1065 | * | |
1066 | * If no action is necessary, the correct return code is BGP_PACKET_NOOP as | |
1067 | * defined below. | |
1068 | * | |
1069 | * Side effects | |
1070 | * ------------ | |
1071 | * - May send NOTIFY messages | |
1072 | * - May not modify peer->status | |
1073 | * - May not call bgp_event_update() | |
1074 | */ | |
1075 | ||
1076 | #define BGP_PACKET_NOOP 0 | |
1077 | ||
1078 | /** | |
1079 | * Process BGP OPEN message for peer. | |
1080 | * | |
1081 | * If any errors are encountered in the OPEN message, immediately sends NOTIFY | |
1082 | * and returns BGP_Stop. | |
1083 | * | |
1084 | * @param peer | |
1085 | * @param size size of the packet | |
1086 | * @return as in summary | |
1087 | */ | |
d62a17ae | 1088 | static int bgp_open_receive(struct peer *peer, bgp_size_t size) |
1089 | { | |
1090 | int ret; | |
d7c0a89a QY |
1091 | uint8_t version; |
1092 | uint8_t optlen; | |
1093 | uint16_t holdtime; | |
1094 | uint16_t send_holdtime; | |
d62a17ae | 1095 | as_t remote_as; |
6dcef54c | 1096 | as_t as4 = 0, as4_be; |
d62a17ae | 1097 | struct in_addr remote_id; |
1098 | int mp_capability; | |
d7c0a89a QY |
1099 | uint8_t notify_data_remote_as[2]; |
1100 | uint8_t notify_data_remote_as4[4]; | |
1101 | uint8_t notify_data_remote_id[4]; | |
1102 | uint16_t *holdtime_ptr; | |
d62a17ae | 1103 | |
1104 | /* Parse open packet. */ | |
424ab01d QY |
1105 | version = stream_getc(peer->curr); |
1106 | memcpy(notify_data_remote_as, stream_pnt(peer->curr), 2); | |
1107 | remote_as = stream_getw(peer->curr); | |
d7c0a89a | 1108 | holdtime_ptr = (uint16_t *)stream_pnt(peer->curr); |
424ab01d QY |
1109 | holdtime = stream_getw(peer->curr); |
1110 | memcpy(notify_data_remote_id, stream_pnt(peer->curr), 4); | |
1111 | remote_id.s_addr = stream_get_ipv4(peer->curr); | |
d62a17ae | 1112 | |
1113 | /* Receive OPEN message log */ | |
1114 | if (bgp_debug_neighbor_events(peer)) | |
1115 | zlog_debug( | |
3efd0893 | 1116 | "%s rcv OPEN, version %d, remote-as (in open) %u, holdtime %d, id %s", |
d62a17ae | 1117 | peer->host, version, remote_as, holdtime, |
1118 | inet_ntoa(remote_id)); | |
1119 | ||
1120 | /* BEGIN to read the capability here, but dont do it yet */ | |
1121 | mp_capability = 0; | |
424ab01d | 1122 | optlen = stream_getc(peer->curr); |
d62a17ae | 1123 | |
1124 | if (optlen != 0) { | |
1125 | /* If not enough bytes, it is an error. */ | |
424ab01d | 1126 | if (STREAM_READABLE(peer->curr) < optlen) { |
d62a17ae | 1127 | bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR, |
1128 | BGP_NOTIFY_OPEN_MALFORMED_ATTR); | |
d8151687 | 1129 | return BGP_Stop; |
d62a17ae | 1130 | } |
718e3744 | 1131 | |
d62a17ae | 1132 | /* We need the as4 capability value *right now* because |
1133 | * if it is there, we have not got the remote_as yet, and | |
1134 | * without | |
1135 | * that we do not know which peer is connecting to us now. | |
1136 | */ | |
1137 | as4 = peek_for_as4_capability(peer, optlen); | |
d62a17ae | 1138 | } |
718e3744 | 1139 | |
6dcef54c DL |
1140 | as4_be = htonl(as4); |
1141 | memcpy(notify_data_remote_as4, &as4_be, 4); | |
1142 | ||
d62a17ae | 1143 | /* Just in case we have a silly peer who sends AS4 capability set to 0 |
1144 | */ | |
1145 | if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) && !as4) { | |
e50f7cfd | 1146 | flog_err(EC_BGP_PKT_OPEN, |
1c50c1c0 QY |
1147 | "%s bad OPEN, got AS4 capability, but AS4 set to 0", |
1148 | peer->host); | |
d62a17ae | 1149 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, |
1150 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1151 | notify_data_remote_as4, 4); | |
d8151687 | 1152 | return BGP_Stop; |
d62a17ae | 1153 | } |
718e3744 | 1154 | |
33d022bc DA |
1155 | /* Codification of AS 0 Processing */ |
1156 | if (remote_as == BGP_AS_ZERO) { | |
1157 | flog_err(EC_BGP_PKT_OPEN, "%s bad OPEN, got AS set to 0", | |
1158 | peer->host); | |
1159 | bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR, | |
1160 | BGP_NOTIFY_OPEN_BAD_PEER_AS); | |
1161 | return BGP_Stop; | |
1162 | } | |
1163 | ||
d62a17ae | 1164 | if (remote_as == BGP_AS_TRANS) { |
1165 | /* Take the AS4 from the capability. We must have received the | |
1166 | * capability now! Otherwise we have a asn16 peer who uses | |
1167 | * BGP_AS_TRANS, for some unknown reason. | |
1168 | */ | |
1169 | if (as4 == BGP_AS_TRANS) { | |
af4c2728 | 1170 | flog_err( |
e50f7cfd | 1171 | EC_BGP_PKT_OPEN, |
d62a17ae | 1172 | "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed", |
1173 | peer->host); | |
1174 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1175 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1176 | notify_data_remote_as4, 4); | |
d8151687 | 1177 | return BGP_Stop; |
d62a17ae | 1178 | } |
718e3744 | 1179 | |
d62a17ae | 1180 | if (!as4 && BGP_DEBUG(as4, AS4)) |
1181 | zlog_debug( | |
3efd0893 | 1182 | "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.", |
d62a17ae | 1183 | peer->host); |
1184 | else if (as4 < BGP_AS_MAX && BGP_DEBUG(as4, AS4)) | |
1185 | zlog_debug( | |
3efd0893 | 1186 | "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.", |
d62a17ae | 1187 | peer->host, as4); |
1188 | if (as4) | |
1189 | remote_as = as4; | |
1190 | } else { | |
1191 | /* We may have a partner with AS4 who has an asno < BGP_AS_MAX | |
1192 | */ | |
1193 | /* If we have got the capability, peer->as4cap must match | |
1194 | * remote_as */ | |
1195 | if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) | |
1196 | && as4 != remote_as) { | |
1197 | /* raise error, log this, close session */ | |
af4c2728 | 1198 | flog_err( |
e50f7cfd | 1199 | EC_BGP_PKT_OPEN, |
3efd0893 | 1200 | "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open", |
d62a17ae | 1201 | peer->host, as4, remote_as); |
1202 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1203 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1204 | notify_data_remote_as4, 4); | |
d8151687 | 1205 | return BGP_Stop; |
d62a17ae | 1206 | } |
1207 | } | |
6b0655a2 | 1208 | |
787c3020 DA |
1209 | /* rfc6286: |
1210 | * If the BGP Identifier field of the OPEN message | |
1211 | * is zero, or if it is the same as the BGP Identifier | |
1212 | * of the local BGP speaker and the message is from an | |
1213 | * internal peer, then the Error Subcode is set to | |
1214 | * "Bad BGP Identifier". | |
1215 | */ | |
975a328e DA |
1216 | if (remote_id.s_addr == INADDR_ANY |
1217 | || IPV4_CLASS_DE(ntohl(remote_id.s_addr)) | |
787c3020 DA |
1218 | || (peer->sort == BGP_PEER_IBGP |
1219 | && ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr))) { | |
d62a17ae | 1220 | if (bgp_debug_neighbor_events(peer)) |
1221 | zlog_debug("%s bad OPEN, wrong router identifier %s", | |
1222 | peer->host, inet_ntoa(remote_id)); | |
1223 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1224 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, | |
1225 | notify_data_remote_id, 4); | |
d8151687 | 1226 | return BGP_Stop; |
d62a17ae | 1227 | } |
1228 | ||
1229 | /* Set remote router-id */ | |
1230 | peer->remote_id = remote_id; | |
1231 | ||
1232 | /* Peer BGP version check. */ | |
1233 | if (version != BGP_VERSION_4) { | |
d7c0a89a | 1234 | uint16_t maxver = htons(BGP_VERSION_4); |
d62a17ae | 1235 | /* XXX this reply may not be correct if version < 4 XXX */ |
1236 | if (bgp_debug_neighbor_events(peer)) | |
1237 | zlog_debug( | |
1238 | "%s bad protocol version, remote requested %d, local request %d", | |
1239 | peer->host, version, BGP_VERSION_4); | |
1240 | /* Data must be in network byte order here */ | |
1241 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1242 | BGP_NOTIFY_OPEN_UNSUP_VERSION, | |
d7c0a89a | 1243 | (uint8_t *)&maxver, 2); |
d8151687 | 1244 | return BGP_Stop; |
d62a17ae | 1245 | } |
1246 | ||
1247 | /* Check neighbor as number. */ | |
1248 | if (peer->as_type == AS_UNSPECIFIED) { | |
1249 | if (bgp_debug_neighbor_events(peer)) | |
1250 | zlog_debug( | |
1251 | "%s bad OPEN, remote AS is unspecified currently", | |
1252 | peer->host); | |
1253 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1254 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1255 | notify_data_remote_as, 2); | |
d8151687 | 1256 | return BGP_Stop; |
d62a17ae | 1257 | } else if (peer->as_type == AS_INTERNAL) { |
1258 | if (remote_as != peer->bgp->as) { | |
1259 | if (bgp_debug_neighbor_events(peer)) | |
1260 | zlog_debug( | |
1261 | "%s bad OPEN, remote AS is %u, internal specified", | |
1262 | peer->host, remote_as); | |
1263 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1264 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1265 | notify_data_remote_as, 2); | |
d8151687 | 1266 | return BGP_Stop; |
1ff9a340 | 1267 | } |
d62a17ae | 1268 | peer->as = peer->local_as; |
1269 | } else if (peer->as_type == AS_EXTERNAL) { | |
1270 | if (remote_as == peer->bgp->as) { | |
1271 | if (bgp_debug_neighbor_events(peer)) | |
1272 | zlog_debug( | |
1273 | "%s bad OPEN, remote AS is %u, external specified", | |
1274 | peer->host, remote_as); | |
1275 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1276 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1277 | notify_data_remote_as, 2); | |
d8151687 | 1278 | return BGP_Stop; |
1ff9a340 | 1279 | } |
d62a17ae | 1280 | peer->as = remote_as; |
1281 | } else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) { | |
1282 | if (bgp_debug_neighbor_events(peer)) | |
1283 | zlog_debug("%s bad OPEN, remote AS is %u, expected %u", | |
1284 | peer->host, remote_as, peer->as); | |
1285 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1286 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1287 | notify_data_remote_as, 2); | |
d8151687 | 1288 | return BGP_Stop; |
eb821189 | 1289 | } |
718e3744 | 1290 | |
d62a17ae | 1291 | /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST |
1292 | calculate the value of the Hold Timer by using the smaller of its | |
1293 | configured Hold Time and the Hold Time received in the OPEN message. | |
1294 | The Hold Time MUST be either zero or at least three seconds. An | |
1295 | implementation may reject connections on the basis of the Hold Time. | |
0b2aa3a0 | 1296 | */ |
d62a17ae | 1297 | |
1298 | if (holdtime < 3 && holdtime != 0) { | |
1299 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1300 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME, | |
d7c0a89a | 1301 | (uint8_t *)holdtime_ptr, 2); |
d8151687 | 1302 | return BGP_Stop; |
0b2aa3a0 | 1303 | } |
d62a17ae | 1304 | |
1305 | /* From the rfc: A reasonable maximum time between KEEPALIVE messages | |
1306 | would be one third of the Hold Time interval. KEEPALIVE messages | |
1307 | MUST NOT be sent more frequently than one per second. An | |
1308 | implementation MAY adjust the rate at which it sends KEEPALIVE | |
1309 | messages as a function of the Hold Time interval. */ | |
1310 | ||
b90a8e13 | 1311 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) |
d62a17ae | 1312 | send_holdtime = peer->holdtime; |
1313 | else | |
1314 | send_holdtime = peer->bgp->default_holdtime; | |
1315 | ||
1316 | if (holdtime < send_holdtime) | |
1317 | peer->v_holdtime = holdtime; | |
1318 | else | |
1319 | peer->v_holdtime = send_holdtime; | |
1320 | ||
b90a8e13 | 1321 | if ((CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) |
d25e4efc | 1322 | && (peer->keepalive < peer->v_holdtime / 3)) |
d62a17ae | 1323 | peer->v_keepalive = peer->keepalive; |
1324 | else | |
1325 | peer->v_keepalive = peer->v_holdtime / 3; | |
1326 | ||
1327 | /* Open option part parse. */ | |
1328 | if (optlen != 0) { | |
1bb379bf | 1329 | if (bgp_open_option_parse(peer, optlen, &mp_capability) < 0) |
d8151687 | 1330 | return BGP_Stop; |
d62a17ae | 1331 | } else { |
1332 | if (bgp_debug_neighbor_events(peer)) | |
1333 | zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0", | |
1334 | peer->host); | |
0299c004 | 1335 | } |
d62a17ae | 1336 | |
1337 | /* | |
1338 | * Assume that the peer supports the locally configured set of | |
1339 | * AFI/SAFIs if the peer did not send us any Mulitiprotocol | |
1340 | * capabilities, or if 'override-capability' is configured. | |
1341 | */ | |
1342 | if (!mp_capability | |
1343 | || CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) { | |
1344 | peer->afc_nego[AFI_IP][SAFI_UNICAST] = | |
1345 | peer->afc[AFI_IP][SAFI_UNICAST]; | |
1346 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] = | |
1347 | peer->afc[AFI_IP][SAFI_MULTICAST]; | |
1348 | peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] = | |
1349 | peer->afc[AFI_IP][SAFI_LABELED_UNICAST]; | |
7c40bf39 | 1350 | peer->afc_nego[AFI_IP][SAFI_FLOWSPEC] = |
1351 | peer->afc[AFI_IP][SAFI_FLOWSPEC]; | |
d62a17ae | 1352 | peer->afc_nego[AFI_IP6][SAFI_UNICAST] = |
1353 | peer->afc[AFI_IP6][SAFI_UNICAST]; | |
1354 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = | |
1355 | peer->afc[AFI_IP6][SAFI_MULTICAST]; | |
1356 | peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] = | |
1357 | peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]; | |
1358 | peer->afc_nego[AFI_L2VPN][SAFI_EVPN] = | |
1359 | peer->afc[AFI_L2VPN][SAFI_EVPN]; | |
7c40bf39 | 1360 | peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC] = |
1361 | peer->afc[AFI_IP6][SAFI_FLOWSPEC]; | |
0299c004 | 1362 | } |
d62a17ae | 1363 | |
036937f0 DA |
1364 | /* When collision is detected and this peer is closed. |
1365 | * Return immediately. | |
1366 | */ | |
d62a17ae | 1367 | ret = bgp_collision_detect(peer, remote_id); |
1368 | if (ret < 0) | |
d8151687 | 1369 | return BGP_Stop; |
d62a17ae | 1370 | |
1371 | /* Get sockname. */ | |
1bb379bf | 1372 | if (bgp_getsockname(peer) < 0) { |
450971aa | 1373 | flog_err_sys(EC_LIB_SOCKET, |
09c866e3 | 1374 | "%s: bgp_getsockname() failed for peer: %s", |
15569c58 | 1375 | __func__, peer->host); |
d8151687 | 1376 | return BGP_Stop; |
d62a17ae | 1377 | } |
1378 | ||
1379 | /* Verify valid local address present based on negotiated | |
1380 | * address-families. */ | |
1381 | if (peer->afc_nego[AFI_IP][SAFI_UNICAST] | |
e5f22b30 | 1382 | || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] |
d62a17ae | 1383 | || peer->afc_nego[AFI_IP][SAFI_MULTICAST] |
1384 | || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] | |
1385 | || peer->afc_nego[AFI_IP][SAFI_ENCAP]) { | |
975a328e | 1386 | if (peer->nexthop.v4.s_addr == INADDR_ANY) { |
d62a17ae | 1387 | #if defined(HAVE_CUMULUS) |
af4c2728 | 1388 | flog_err( |
e50f7cfd | 1389 | EC_BGP_SND_FAIL, |
d62a17ae | 1390 | "%s: No local IPv4 addr resetting connection, fd %d", |
1391 | peer->host, peer->fd); | |
1392 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, | |
1393 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 1394 | return BGP_Stop; |
1d808091 | 1395 | #endif |
d62a17ae | 1396 | } |
1397 | } | |
1398 | if (peer->afc_nego[AFI_IP6][SAFI_UNICAST] | |
e5f22b30 | 1399 | || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] |
d62a17ae | 1400 | || peer->afc_nego[AFI_IP6][SAFI_MULTICAST] |
1401 | || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN] | |
1402 | || peer->afc_nego[AFI_IP6][SAFI_ENCAP]) { | |
1403 | if (IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_global)) { | |
1404 | #if defined(HAVE_CUMULUS) | |
af4c2728 | 1405 | flog_err( |
e50f7cfd | 1406 | EC_BGP_SND_FAIL, |
d62a17ae | 1407 | "%s: No local IPv6 addr resetting connection, fd %d", |
1408 | peer->host, peer->fd); | |
1409 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, | |
1410 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 1411 | return BGP_Stop; |
1d808091 | 1412 | #endif |
d62a17ae | 1413 | } |
1414 | } | |
1415 | peer->rtt = sockopt_tcp_rtt(peer->fd); | |
1416 | ||
d8151687 | 1417 | return Receive_OPEN_message; |
718e3744 | 1418 | } |
1419 | ||
d8151687 QY |
1420 | /** |
1421 | * Process BGP KEEPALIVE message for peer. | |
1422 | * | |
1423 | * @param peer | |
1424 | * @param size size of the packet | |
1425 | * @return as in summary | |
1426 | */ | |
1427 | static int bgp_keepalive_receive(struct peer *peer, bgp_size_t size) | |
f188f2c4 | 1428 | { |
d8151687 QY |
1429 | if (bgp_debug_keepalive(peer)) |
1430 | zlog_debug("%s KEEPALIVE rcvd", peer->host); | |
d62a17ae | 1431 | |
d8151687 | 1432 | bgp_update_implicit_eors(peer); |
d62a17ae | 1433 | |
d8151687 | 1434 | return Receive_KEEPALIVE_message; |
f188f2c4 DS |
1435 | } |
1436 | ||
d62a17ae | 1437 | |
d8151687 QY |
1438 | /** |
1439 | * Process BGP UPDATE message for peer. | |
1440 | * | |
1441 | * Parses UPDATE and creates attribute object. | |
1442 | * | |
1443 | * @param peer | |
1444 | * @param size size of the packet | |
1445 | * @return as in summary | |
7ef5a232 | 1446 | */ |
d62a17ae | 1447 | static int bgp_update_receive(struct peer *peer, bgp_size_t size) |
718e3744 | 1448 | { |
d62a17ae | 1449 | int ret, nlri_ret; |
d7c0a89a | 1450 | uint8_t *end; |
d62a17ae | 1451 | struct stream *s; |
1452 | struct attr attr; | |
1453 | bgp_size_t attribute_len; | |
1454 | bgp_size_t update_len; | |
1455 | bgp_size_t withdraw_len; | |
f009ff26 | 1456 | bool restart = false; |
d62a17ae | 1457 | |
1458 | enum NLRI_TYPES { | |
1459 | NLRI_UPDATE, | |
1460 | NLRI_WITHDRAW, | |
1461 | NLRI_MP_UPDATE, | |
1462 | NLRI_MP_WITHDRAW, | |
1463 | NLRI_TYPE_MAX | |
1464 | }; | |
1465 | struct bgp_nlri nlris[NLRI_TYPE_MAX]; | |
1466 | ||
1467 | /* Status must be Established. */ | |
1468 | if (peer->status != Established) { | |
e50f7cfd | 1469 | flog_err(EC_BGP_INVALID_STATUS, |
1c50c1c0 QY |
1470 | "%s [FSM] Update packet received under status %s", |
1471 | peer->host, | |
1472 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
0e35025e | 1473 | bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, |
3893aeee | 1474 | bgp_fsm_error_subcode(peer->status)); |
d8151687 | 1475 | return BGP_Stop; |
d62a17ae | 1476 | } |
1477 | ||
1478 | /* Set initial values. */ | |
1479 | memset(&attr, 0, sizeof(struct attr)); | |
1480 | attr.label_index = BGP_INVALID_LABEL_INDEX; | |
1481 | attr.label = MPLS_INVALID_LABEL; | |
1482 | memset(&nlris, 0, sizeof(nlris)); | |
1483 | memset(peer->rcvd_attr_str, 0, BUFSIZ); | |
1484 | peer->rcvd_attr_printed = 0; | |
1485 | ||
424ab01d | 1486 | s = peer->curr; |
d62a17ae | 1487 | end = stream_pnt(s) + size; |
1488 | ||
1489 | /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute | |
1490 | Length is too large (i.e., if Unfeasible Routes Length + Total | |
1491 | Attribute Length + 23 exceeds the message Length), then the Error | |
1492 | Subcode is set to Malformed Attribute List. */ | |
1493 | if (stream_pnt(s) + 2 > end) { | |
e50f7cfd | 1494 | flog_err(EC_BGP_UPDATE_RCV, |
3efd0893 | 1495 | "%s [Error] Update packet error (packet length is short for unfeasible length)", |
1c50c1c0 | 1496 | peer->host); |
d62a17ae | 1497 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1498 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1499 | return BGP_Stop; |
d62a17ae | 1500 | } |
1501 | ||
1502 | /* Unfeasible Route Length. */ | |
1503 | withdraw_len = stream_getw(s); | |
1504 | ||
1505 | /* Unfeasible Route Length check. */ | |
1506 | if (stream_pnt(s) + withdraw_len > end) { | |
e50f7cfd | 1507 | flog_err(EC_BGP_UPDATE_RCV, |
3efd0893 | 1508 | "%s [Error] Update packet error (packet unfeasible length overflow %d)", |
1c50c1c0 | 1509 | peer->host, withdraw_len); |
d62a17ae | 1510 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1511 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1512 | return BGP_Stop; |
d62a17ae | 1513 | } |
1514 | ||
1515 | /* Unfeasible Route packet format check. */ | |
1516 | if (withdraw_len > 0) { | |
1517 | nlris[NLRI_WITHDRAW].afi = AFI_IP; | |
1518 | nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST; | |
1519 | nlris[NLRI_WITHDRAW].nlri = stream_pnt(s); | |
1520 | nlris[NLRI_WITHDRAW].length = withdraw_len; | |
1521 | stream_forward_getp(s, withdraw_len); | |
1522 | } | |
1523 | ||
1524 | /* Attribute total length check. */ | |
1525 | if (stream_pnt(s) + 2 > end) { | |
ade6974d | 1526 | flog_warn( |
e50f7cfd | 1527 | EC_BGP_UPDATE_PACKET_SHORT, |
ade6974d QY |
1528 | "%s [Error] Packet Error (update packet is short for attribute length)", |
1529 | peer->host); | |
d62a17ae | 1530 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1531 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1532 | return BGP_Stop; |
d62a17ae | 1533 | } |
1534 | ||
1535 | /* Fetch attribute total length. */ | |
1536 | attribute_len = stream_getw(s); | |
1537 | ||
1538 | /* Attribute length check. */ | |
1539 | if (stream_pnt(s) + attribute_len > end) { | |
ade6974d | 1540 | flog_warn( |
e50f7cfd | 1541 | EC_BGP_UPDATE_PACKET_LONG, |
ade6974d QY |
1542 | "%s [Error] Packet Error (update packet attribute length overflow %d)", |
1543 | peer->host, attribute_len); | |
d62a17ae | 1544 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1545 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1546 | return BGP_Stop; |
d62a17ae | 1547 | } |
1548 | ||
1549 | /* Certain attribute parsing errors should not be considered bad enough | |
1550 | * to reset the session for, most particularly any partial/optional | |
1551 | * attributes that have 'tunneled' over speakers that don't understand | |
1552 | * them. Instead we withdraw only the prefix concerned. | |
1553 | * | |
1554 | * Complicates the flow a little though.. | |
1555 | */ | |
1556 | bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED; | |
1557 | /* This define morphs the update case into a withdraw when lower levels | |
1558 | * have signalled an error condition where this is best. | |
1559 | */ | |
b881c707 | 1560 | #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL) |
718e3744 | 1561 | |
d62a17ae | 1562 | /* Parse attribute when it exists. */ |
1563 | if (attribute_len) { | |
1564 | attr_parse_ret = bgp_attr_parse(peer, &attr, attribute_len, | |
1565 | &nlris[NLRI_MP_UPDATE], | |
1566 | &nlris[NLRI_MP_WITHDRAW]); | |
1567 | if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) { | |
1568 | bgp_attr_unintern_sub(&attr); | |
d8151687 | 1569 | return BGP_Stop; |
d62a17ae | 1570 | } |
1571 | } | |
1572 | ||
1573 | /* Logging the attribute. */ | |
1574 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW | |
1575 | || BGP_DEBUG(update, UPDATE_IN) | |
1576 | || BGP_DEBUG(update, UPDATE_PREFIX)) { | |
1577 | ret = bgp_dump_attr(&attr, peer->rcvd_attr_str, BUFSIZ); | |
1578 | ||
b4d46cc9 DL |
1579 | peer->stat_upd_7606++; |
1580 | ||
d62a17ae | 1581 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW) |
af4c2728 | 1582 | flog_err( |
e50f7cfd | 1583 | EC_BGP_UPDATE_RCV, |
d62a17ae | 1584 | "%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.", |
1585 | peer->host); | |
1586 | ||
1587 | if (ret && bgp_debug_update(peer, NULL, NULL, 1)) { | |
1588 | zlog_debug("%s rcvd UPDATE w/ attr: %s", peer->host, | |
1589 | peer->rcvd_attr_str); | |
1590 | peer->rcvd_attr_printed = 1; | |
1591 | } | |
1592 | } | |
1593 | ||
1594 | /* Network Layer Reachability Information. */ | |
1595 | update_len = end - stream_pnt(s); | |
1596 | ||
1597 | if (update_len) { | |
1598 | /* Set NLRI portion to structure. */ | |
1599 | nlris[NLRI_UPDATE].afi = AFI_IP; | |
1600 | nlris[NLRI_UPDATE].safi = SAFI_UNICAST; | |
1601 | nlris[NLRI_UPDATE].nlri = stream_pnt(s); | |
1602 | nlris[NLRI_UPDATE].length = update_len; | |
1603 | stream_forward_getp(s, update_len); | |
9738e9aa | 1604 | |
1605 | if (CHECK_FLAG(attr.flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI))) { | |
1606 | /* | |
1607 | * We skipped nexthop attribute validation earlier so | |
1608 | * validate the nexthop now. | |
1609 | */ | |
1610 | if (bgp_attr_nexthop_valid(peer, &attr) < 0) { | |
1611 | bgp_attr_unintern_sub(&attr); | |
1612 | return BGP_Stop; | |
1613 | } | |
1614 | } | |
d62a17ae | 1615 | } |
1616 | ||
1617 | if (BGP_DEBUG(update, UPDATE_IN)) | |
1618 | zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d", | |
1619 | peer->host, withdraw_len, attribute_len, update_len); | |
1620 | ||
1621 | /* Parse any given NLRIs */ | |
1622 | for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) { | |
1623 | if (!nlris[i].nlri) | |
1624 | continue; | |
1625 | ||
1626 | /* NLRI is processed iff the peer if configured for the specific | |
1627 | * afi/safi */ | |
1628 | if (!peer->afc[nlris[i].afi][nlris[i].safi]) { | |
1629 | zlog_info( | |
1630 | "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u", | |
1631 | peer->host, nlris[i].afi, nlris[i].safi); | |
1632 | continue; | |
1633 | } | |
1634 | ||
1635 | /* EoR handled later */ | |
1636 | if (nlris[i].length == 0) | |
1637 | continue; | |
1638 | ||
1639 | switch (i) { | |
1640 | case NLRI_UPDATE: | |
1641 | case NLRI_MP_UPDATE: | |
1642 | nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG, | |
1643 | &nlris[i], 0); | |
1644 | break; | |
1645 | case NLRI_WITHDRAW: | |
1646 | case NLRI_MP_WITHDRAW: | |
1647 | nlri_ret = bgp_nlri_parse(peer, &attr, &nlris[i], 1); | |
1648 | break; | |
1649 | default: | |
513386b5 | 1650 | nlri_ret = BGP_NLRI_PARSE_ERROR; |
d62a17ae | 1651 | } |
1652 | ||
513386b5 DA |
1653 | if (nlri_ret < BGP_NLRI_PARSE_OK |
1654 | && nlri_ret != BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW) { | |
e50f7cfd | 1655 | flog_err(EC_BGP_UPDATE_RCV, |
1c50c1c0 | 1656 | "%s [Error] Error parsing NLRI", peer->host); |
d62a17ae | 1657 | if (peer->status == Established) |
1658 | bgp_notify_send( | |
1659 | peer, BGP_NOTIFY_UPDATE_ERR, | |
1660 | i <= NLRI_WITHDRAW | |
1661 | ? BGP_NOTIFY_UPDATE_INVAL_NETWORK | |
1662 | : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR); | |
1663 | bgp_attr_unintern_sub(&attr); | |
d8151687 | 1664 | return BGP_Stop; |
d62a17ae | 1665 | } |
1666 | } | |
1667 | ||
1668 | /* EoR checks | |
1669 | * | |
1670 | * Non-MP IPv4/Unicast EoR is a completely empty UPDATE | |
1671 | * and MP EoR should have only an empty MP_UNREACH | |
1672 | */ | |
996c9314 LB |
1673 | if ((!update_len && !withdraw_len && nlris[NLRI_MP_UPDATE].length == 0) |
1674 | || (attr_parse_ret == BGP_ATTR_PARSE_EOR)) { | |
d62a17ae | 1675 | afi_t afi = 0; |
1676 | safi_t safi; | |
f009ff26 | 1677 | struct graceful_restart_info *gr_info; |
1678 | ||
1679 | /* Restarting router */ | |
36235319 QY |
1680 | if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer) |
1681 | && BGP_PEER_RESTARTING_MODE(peer)) | |
f009ff26 | 1682 | restart = true; |
d62a17ae | 1683 | |
1684 | /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already | |
1685 | * checked | |
1686 | * update and withdraw NLRI lengths are 0. | |
1687 | */ | |
1688 | if (!attribute_len) { | |
1689 | afi = AFI_IP; | |
1690 | safi = SAFI_UNICAST; | |
1691 | } else if (attr.flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI) | |
1692 | && nlris[NLRI_MP_WITHDRAW].length == 0) { | |
1693 | afi = nlris[NLRI_MP_WITHDRAW].afi; | |
1694 | safi = nlris[NLRI_MP_WITHDRAW].safi; | |
9b9df989 DS |
1695 | } else if (attr_parse_ret == BGP_ATTR_PARSE_EOR) { |
1696 | afi = nlris[NLRI_MP_UPDATE].afi; | |
1697 | safi = nlris[NLRI_MP_UPDATE].safi; | |
d62a17ae | 1698 | } |
1699 | ||
1700 | if (afi && peer->afc[afi][safi]) { | |
e82d19a3 DS |
1701 | struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id); |
1702 | ||
d62a17ae | 1703 | /* End-of-RIB received */ |
1704 | if (!CHECK_FLAG(peer->af_sflags[afi][safi], | |
1705 | PEER_STATUS_EOR_RECEIVED)) { | |
1706 | SET_FLAG(peer->af_sflags[afi][safi], | |
1707 | PEER_STATUS_EOR_RECEIVED); | |
1708 | bgp_update_explicit_eors(peer); | |
f009ff26 | 1709 | /* Update graceful restart information */ |
1710 | gr_info = &(peer->bgp->gr_info[afi][safi]); | |
1711 | if (restart) | |
1712 | gr_info->eor_received++; | |
1713 | /* If EOR received from all peers and selection | |
1714 | * deferral timer is running, cancel the timer | |
1715 | * and invoke the best path calculation | |
1716 | */ | |
36235319 QY |
1717 | if (gr_info->eor_required |
1718 | == gr_info->eor_received) { | |
1719 | if (bgp_debug_neighbor_events(peer)) | |
1720 | zlog_debug( | |
1721 | "%s %d, %s %d", | |
f009ff26 | 1722 | "EOR REQ", |
1723 | gr_info->eor_required, | |
1724 | "EOR RCV", | |
1725 | gr_info->eor_received); | |
1726 | BGP_TIMER_OFF( | |
1727 | gr_info->t_select_deferral); | |
1728 | gr_info->eor_required = 0; | |
1729 | gr_info->eor_received = 0; | |
1730 | /* Best path selection */ | |
1731 | if (bgp_best_path_select_defer( | |
36235319 QY |
1732 | peer->bgp, afi, safi) |
1733 | < 0) | |
f009ff26 | 1734 | return BGP_Stop; |
1735 | } | |
d62a17ae | 1736 | } |
1737 | ||
1738 | /* NSF delete stale route */ | |
1739 | if (peer->nsf[afi][safi]) | |
1740 | bgp_clear_stale_route(peer, afi, safi); | |
1741 | ||
a0e89d54 TA |
1742 | zlog_info( |
1743 | "%s: rcvd End-of-RIB for %s from %s in vrf %s", | |
1744 | __func__, get_afi_safi_str(afi, safi, false), | |
1745 | peer->host, vrf ? vrf->name : VRF_DEFAULT_NAME); | |
1746 | } | |
f80f838b | 1747 | } |
d62a17ae | 1748 | |
1749 | /* Everything is done. We unintern temporary structures which | |
1750 | interned in bgp_attr_parse(). */ | |
1751 | bgp_attr_unintern_sub(&attr); | |
1752 | ||
d62a17ae | 1753 | peer->update_time = bgp_clock(); |
1754 | ||
1755 | /* Rearm holdtime timer */ | |
1756 | BGP_TIMER_OFF(peer->t_holdtime); | |
1757 | bgp_timer_set(peer); | |
1758 | ||
d8151687 | 1759 | return Receive_UPDATE_message; |
718e3744 | 1760 | } |
1761 | ||
d8151687 QY |
1762 | /** |
1763 | * Process BGP NOTIFY message for peer. | |
1764 | * | |
1765 | * @param peer | |
1766 | * @param size size of the packet | |
1767 | * @return as in summary | |
1768 | */ | |
1769 | static int bgp_notify_receive(struct peer *peer, bgp_size_t size) | |
718e3744 | 1770 | { |
d62a17ae | 1771 | struct bgp_notify bgp_notify; |
1772 | ||
1773 | if (peer->notify.data) { | |
1774 | XFREE(MTYPE_TMP, peer->notify.data); | |
d62a17ae | 1775 | peer->notify.length = 0; |
1776 | } | |
1777 | ||
424ab01d QY |
1778 | bgp_notify.code = stream_getc(peer->curr); |
1779 | bgp_notify.subcode = stream_getc(peer->curr); | |
d62a17ae | 1780 | bgp_notify.length = size - 2; |
1781 | bgp_notify.data = NULL; | |
1782 | ||
1783 | /* Preserv notify code and sub code. */ | |
1784 | peer->notify.code = bgp_notify.code; | |
1785 | peer->notify.subcode = bgp_notify.subcode; | |
1786 | /* For further diagnostic record returned Data. */ | |
1787 | if (bgp_notify.length) { | |
1788 | peer->notify.length = size - 2; | |
1789 | peer->notify.data = XMALLOC(MTYPE_TMP, size - 2); | |
424ab01d | 1790 | memcpy(peer->notify.data, stream_pnt(peer->curr), size - 2); |
d62a17ae | 1791 | } |
1792 | ||
1793 | /* For debug */ | |
1794 | { | |
1795 | int i; | |
1796 | int first = 0; | |
1797 | char c[4]; | |
1798 | ||
1799 | if (bgp_notify.length) { | |
1800 | bgp_notify.data = | |
1801 | XMALLOC(MTYPE_TMP, bgp_notify.length * 3); | |
1802 | for (i = 0; i < bgp_notify.length; i++) | |
1803 | if (first) { | |
552d6491 | 1804 | snprintf(c, sizeof(c), " %02x", |
424ab01d | 1805 | stream_getc(peer->curr)); |
f009ff26 | 1806 | |
552d6491 | 1807 | strlcat(bgp_notify.data, c, |
f009ff26 | 1808 | bgp_notify.length * 3); |
1809 | ||
d62a17ae | 1810 | } else { |
1811 | first = 1; | |
552d6491 QY |
1812 | snprintf(c, sizeof(c), "%02x", |
1813 | stream_getc(peer->curr)); | |
f009ff26 | 1814 | |
552d6491 | 1815 | strlcpy(bgp_notify.data, c, |
f009ff26 | 1816 | bgp_notify.length * 3); |
d62a17ae | 1817 | } |
d7c0a89a | 1818 | bgp_notify.raw_data = (uint8_t *)peer->notify.data; |
d62a17ae | 1819 | } |
1820 | ||
1821 | bgp_notify_print(peer, &bgp_notify, "received"); | |
1822 | if (bgp_notify.data) { | |
1823 | XFREE(MTYPE_TMP, bgp_notify.data); | |
d62a17ae | 1824 | bgp_notify.length = 0; |
1825 | } | |
1826 | } | |
1827 | ||
1828 | /* peer count update */ | |
0112e9e0 | 1829 | atomic_fetch_add_explicit(&peer->notify_in, 1, memory_order_relaxed); |
d62a17ae | 1830 | |
1831 | peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED; | |
1832 | ||
1833 | /* We have to check for Notify with Unsupported Optional Parameter. | |
1834 | in that case we fallback to open without the capability option. | |
1835 | But this done in bgp_stop. We just mark it here to avoid changing | |
1836 | the fsm tables. */ | |
1837 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR | |
1838 | && bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM) | |
1839 | UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN); | |
1840 | ||
5cce3f05 | 1841 | bgp_peer_gr_flags_update(peer); |
36235319 QY |
1842 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp, |
1843 | peer->bgp->peer); | |
5cce3f05 | 1844 | |
d8151687 | 1845 | return Receive_NOTIFICATION_message; |
718e3744 | 1846 | } |
1847 | ||
d8151687 QY |
1848 | /** |
1849 | * Process BGP ROUTEREFRESH message for peer. | |
1850 | * | |
1851 | * @param peer | |
1852 | * @param size size of the packet | |
1853 | * @return as in summary | |
1854 | */ | |
1855 | static int bgp_route_refresh_receive(struct peer *peer, bgp_size_t size) | |
718e3744 | 1856 | { |
d62a17ae | 1857 | iana_afi_t pkt_afi; |
1858 | afi_t afi; | |
5c525538 RW |
1859 | iana_safi_t pkt_safi; |
1860 | safi_t safi; | |
d62a17ae | 1861 | struct stream *s; |
1862 | struct peer_af *paf; | |
1863 | struct update_group *updgrp; | |
1864 | struct peer *updgrp_peer; | |
1865 | ||
1866 | /* If peer does not have the capability, send notification. */ | |
1867 | if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) { | |
e50f7cfd | 1868 | flog_err(EC_BGP_NO_CAP, |
1c50c1c0 QY |
1869 | "%s [Error] BGP route refresh is not enabled", |
1870 | peer->host); | |
d62a17ae | 1871 | bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR, |
1872 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
d8151687 | 1873 | return BGP_Stop; |
d62a17ae | 1874 | } |
1875 | ||
1876 | /* Status must be Established. */ | |
1877 | if (peer->status != Established) { | |
af4c2728 | 1878 | flog_err( |
e50f7cfd | 1879 | EC_BGP_INVALID_STATUS, |
d62a17ae | 1880 | "%s [Error] Route refresh packet received under status %s", |
1881 | peer->host, | |
1882 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
0e35025e | 1883 | bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, |
3893aeee | 1884 | bgp_fsm_error_subcode(peer->status)); |
d8151687 | 1885 | return BGP_Stop; |
d62a17ae | 1886 | } |
1887 | ||
424ab01d | 1888 | s = peer->curr; |
d62a17ae | 1889 | |
1890 | /* Parse packet. */ | |
1891 | pkt_afi = stream_getw(s); | |
1892 | (void)stream_getc(s); | |
1893 | pkt_safi = stream_getc(s); | |
1894 | ||
1895 | if (bgp_debug_update(peer, NULL, NULL, 0)) | |
748a041f DS |
1896 | zlog_debug("%s rcvd REFRESH_REQ for afi/safi: %s/%s", |
1897 | peer->host, iana_afi2str(pkt_afi), | |
1898 | iana_safi2str(pkt_safi)); | |
d62a17ae | 1899 | |
1900 | /* Convert AFI, SAFI to internal values and check. */ | |
1901 | if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) { | |
1902 | zlog_info( | |
748a041f DS |
1903 | "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored", |
1904 | peer->host, iana_afi2str(pkt_afi), | |
1905 | iana_safi2str(pkt_safi)); | |
d8151687 | 1906 | return BGP_PACKET_NOOP; |
d62a17ae | 1907 | } |
1908 | ||
1909 | if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) { | |
d7c0a89a QY |
1910 | uint8_t *end; |
1911 | uint8_t when_to_refresh; | |
1912 | uint8_t orf_type; | |
1913 | uint16_t orf_len; | |
d62a17ae | 1914 | |
1915 | if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) | |
1916 | < 5) { | |
1917 | zlog_info("%s ORF route refresh length error", | |
1918 | peer->host); | |
0e35025e DA |
1919 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
1920 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 1921 | return BGP_Stop; |
718e3744 | 1922 | } |
1923 | ||
d62a17ae | 1924 | when_to_refresh = stream_getc(s); |
1925 | end = stream_pnt(s) + (size - 5); | |
1926 | ||
1927 | while ((stream_pnt(s) + 2) < end) { | |
1928 | orf_type = stream_getc(s); | |
1929 | orf_len = stream_getw(s); | |
1930 | ||
1931 | /* orf_len in bounds? */ | |
1932 | if ((stream_pnt(s) + orf_len) > end) | |
1933 | break; /* XXX: Notify instead?? */ | |
1934 | if (orf_type == ORF_TYPE_PREFIX | |
1935 | || orf_type == ORF_TYPE_PREFIX_OLD) { | |
1936 | uint8_t *p_pnt = stream_pnt(s); | |
1937 | uint8_t *p_end = stream_pnt(s) + orf_len; | |
1938 | struct orf_prefix orfp; | |
d7c0a89a QY |
1939 | uint8_t common = 0; |
1940 | uint32_t seq; | |
d62a17ae | 1941 | int psize; |
1942 | char name[BUFSIZ]; | |
1943 | int ret = CMD_SUCCESS; | |
1944 | ||
1945 | if (bgp_debug_neighbor_events(peer)) { | |
1946 | zlog_debug( | |
1947 | "%s rcvd Prefixlist ORF(%d) length %d", | |
1948 | peer->host, orf_type, orf_len); | |
1949 | } | |
1950 | ||
1951 | /* we're going to read at least 1 byte of common | |
1952 | * ORF header, | |
1953 | * and 7 bytes of ORF Address-filter entry from | |
1954 | * the stream | |
1955 | */ | |
1956 | if (orf_len < 7) | |
1957 | break; | |
1958 | ||
1959 | /* ORF prefix-list name */ | |
772270f3 QY |
1960 | snprintf(name, sizeof(name), "%s.%d.%d", |
1961 | peer->host, afi, safi); | |
d62a17ae | 1962 | |
1963 | while (p_pnt < p_end) { | |
1964 | /* If the ORF entry is malformed, want | |
1965 | * to read as much of it | |
1966 | * as possible without going beyond the | |
1967 | * bounds of the entry, | |
1968 | * to maximise debug information. | |
1969 | */ | |
1970 | int ok; | |
1971 | memset(&orfp, 0, | |
1972 | sizeof(struct orf_prefix)); | |
1973 | common = *p_pnt++; | |
1974 | /* after ++: p_pnt <= p_end */ | |
1975 | if (common | |
1976 | & ORF_COMMON_PART_REMOVE_ALL) { | |
1977 | if (bgp_debug_neighbor_events( | |
1978 | peer)) | |
1979 | zlog_debug( | |
1980 | "%s rcvd Remove-All pfxlist ORF request", | |
1981 | peer->host); | |
1982 | prefix_bgp_orf_remove_all(afi, | |
1983 | name); | |
1984 | break; | |
1985 | } | |
d7c0a89a QY |
1986 | ok = ((uint32_t)(p_end - p_pnt) |
1987 | >= sizeof(uint32_t)); | |
d62a17ae | 1988 | if (ok) { |
1989 | memcpy(&seq, p_pnt, | |
d7c0a89a QY |
1990 | sizeof(uint32_t)); |
1991 | p_pnt += sizeof(uint32_t); | |
d62a17ae | 1992 | orfp.seq = ntohl(seq); |
1993 | } else | |
1994 | p_pnt = p_end; | |
1995 | ||
5ca840a3 | 1996 | /* val checked in prefix_bgp_orf_set */ |
1bb379bf | 1997 | if (p_pnt < p_end) |
5ca840a3 DS |
1998 | orfp.ge = *p_pnt++; |
1999 | ||
2000 | /* val checked in prefix_bgp_orf_set */ | |
1bb379bf | 2001 | if (p_pnt < p_end) |
5ca840a3 DS |
2002 | orfp.le = *p_pnt++; |
2003 | ||
d62a17ae | 2004 | if ((ok = (p_pnt < p_end))) |
2005 | orfp.p.prefixlen = *p_pnt++; | |
5ca840a3 DS |
2006 | |
2007 | /* afi checked already */ | |
2008 | orfp.p.family = afi2family(afi); | |
2009 | ||
2010 | /* 0 if not ok */ | |
2011 | psize = PSIZE(orfp.p.prefixlen); | |
2012 | /* valid for family ? */ | |
2013 | if (psize > prefix_blen(&orfp.p)) { | |
d62a17ae | 2014 | ok = 0; |
2015 | psize = prefix_blen(&orfp.p); | |
2016 | } | |
5ca840a3 DS |
2017 | /* valid for packet ? */ |
2018 | if (psize > (p_end - p_pnt)) { | |
d62a17ae | 2019 | ok = 0; |
2020 | psize = p_end - p_pnt; | |
2021 | } | |
2022 | ||
2023 | if (psize > 0) | |
2024 | memcpy(&orfp.p.u.prefix, p_pnt, | |
2025 | psize); | |
2026 | p_pnt += psize; | |
2027 | ||
2028 | if (bgp_debug_neighbor_events(peer)) { | |
2029 | char buf[INET6_BUFSIZ]; | |
2030 | ||
2031 | zlog_debug( | |
2032 | "%s rcvd %s %s seq %u %s/%d ge %d le %d%s", | |
2033 | peer->host, | |
2034 | (common & ORF_COMMON_PART_REMOVE | |
2035 | ? "Remove" | |
2036 | : "Add"), | |
2037 | (common & ORF_COMMON_PART_DENY | |
2038 | ? "deny" | |
2039 | : "permit"), | |
2040 | orfp.seq, | |
2041 | inet_ntop( | |
2042 | orfp.p.family, | |
2043 | &orfp.p.u.prefix, | |
2044 | buf, | |
2045 | INET6_BUFSIZ), | |
2046 | orfp.p.prefixlen, | |
2047 | orfp.ge, orfp.le, | |
2048 | ok ? "" : " MALFORMED"); | |
2049 | } | |
2050 | ||
2051 | if (ok) | |
2052 | ret = prefix_bgp_orf_set( | |
2053 | name, afi, &orfp, | |
2054 | (common & ORF_COMMON_PART_DENY | |
2055 | ? 0 | |
2056 | : 1), | |
2057 | (common & ORF_COMMON_PART_REMOVE | |
2058 | ? 0 | |
2059 | : 1)); | |
2060 | ||
2061 | if (!ok || (ok && ret != CMD_SUCCESS)) { | |
2062 | zlog_info( | |
3efd0893 | 2063 | "%s Received misformatted prefixlist ORF. Remove All pfxlist", |
d62a17ae | 2064 | peer->host); |
2065 | prefix_bgp_orf_remove_all(afi, | |
2066 | name); | |
2067 | break; | |
2068 | } | |
2069 | } | |
2070 | ||
2071 | peer->orf_plist[afi][safi] = | |
2072 | prefix_bgp_orf_lookup(afi, name); | |
2073 | } | |
2074 | stream_forward_getp(s, orf_len); | |
718e3744 | 2075 | } |
d62a17ae | 2076 | if (bgp_debug_neighbor_events(peer)) |
2077 | zlog_debug("%s rcvd Refresh %s ORF request", peer->host, | |
2078 | when_to_refresh == REFRESH_DEFER | |
2079 | ? "Defer" | |
2080 | : "Immediate"); | |
2081 | if (when_to_refresh == REFRESH_DEFER) | |
d8151687 | 2082 | return BGP_PACKET_NOOP; |
d62a17ae | 2083 | } |
40d2700d | 2084 | |
d62a17ae | 2085 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ |
2086 | if (CHECK_FLAG(peer->af_sflags[afi][safi], | |
2087 | PEER_STATUS_ORF_WAIT_REFRESH)) | |
2088 | UNSET_FLAG(peer->af_sflags[afi][safi], | |
2089 | PEER_STATUS_ORF_WAIT_REFRESH); | |
2090 | ||
2091 | paf = peer_af_find(peer, afi, safi); | |
2092 | if (paf && paf->subgroup) { | |
2093 | if (peer->orf_plist[afi][safi]) { | |
2094 | updgrp = PAF_UPDGRP(paf); | |
2095 | updgrp_peer = UPDGRP_PEER(updgrp); | |
2096 | updgrp_peer->orf_plist[afi][safi] = | |
2097 | peer->orf_plist[afi][safi]; | |
2098 | } | |
2099 | ||
2100 | /* If the peer is configured for default-originate clear the | |
2101 | * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will | |
2102 | * re-advertise the | |
2103 | * default | |
2104 | */ | |
2105 | if (CHECK_FLAG(paf->subgroup->sflags, | |
2106 | SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
2107 | UNSET_FLAG(paf->subgroup->sflags, | |
2108 | SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
718e3744 | 2109 | } |
d62a17ae | 2110 | |
2111 | /* Perform route refreshment to the peer */ | |
2112 | bgp_announce_route(peer, afi, safi); | |
d8151687 QY |
2113 | |
2114 | /* No FSM action necessary */ | |
2115 | return BGP_PACKET_NOOP; | |
718e3744 | 2116 | } |
2117 | ||
d8151687 QY |
2118 | /** |
2119 | * Parse BGP CAPABILITY message for peer. | |
2120 | * | |
2121 | * @param peer | |
2122 | * @param size size of the packet | |
2123 | * @return as in summary | |
2124 | */ | |
d7c0a89a | 2125 | static int bgp_capability_msg_parse(struct peer *peer, uint8_t *pnt, |
d62a17ae | 2126 | bgp_size_t length) |
718e3744 | 2127 | { |
d7c0a89a | 2128 | uint8_t *end; |
d62a17ae | 2129 | struct capability_mp_data mpc; |
2130 | struct capability_header *hdr; | |
d7c0a89a | 2131 | uint8_t action; |
d62a17ae | 2132 | iana_afi_t pkt_afi; |
2133 | afi_t afi; | |
5c525538 RW |
2134 | iana_safi_t pkt_safi; |
2135 | safi_t safi; | |
d62a17ae | 2136 | |
2137 | end = pnt + length; | |
2138 | ||
2139 | while (pnt < end) { | |
2140 | /* We need at least action, capability code and capability | |
2141 | * length. */ | |
2142 | if (pnt + 3 > end) { | |
2143 | zlog_info("%s Capability length error", peer->host); | |
0e35025e DA |
2144 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
2145 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 2146 | return BGP_Stop; |
d62a17ae | 2147 | } |
2148 | action = *pnt; | |
2149 | hdr = (struct capability_header *)(pnt + 1); | |
2150 | ||
2151 | /* Action value check. */ | |
2152 | if (action != CAPABILITY_ACTION_SET | |
2153 | && action != CAPABILITY_ACTION_UNSET) { | |
2154 | zlog_info("%s Capability Action Value error %d", | |
2155 | peer->host, action); | |
0e35025e DA |
2156 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
2157 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 2158 | return BGP_Stop; |
d62a17ae | 2159 | } |
2160 | ||
2161 | if (bgp_debug_neighbor_events(peer)) | |
2162 | zlog_debug( | |
2163 | "%s CAPABILITY has action: %d, code: %u, length %u", | |
2164 | peer->host, action, hdr->code, hdr->length); | |
2165 | ||
2166 | /* Capability length check. */ | |
2167 | if ((pnt + hdr->length + 3) > end) { | |
2168 | zlog_info("%s Capability length error", peer->host); | |
0e35025e DA |
2169 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
2170 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 2171 | return BGP_Stop; |
d62a17ae | 2172 | } |
2173 | ||
2174 | /* Fetch structure to the byte stream. */ | |
2175 | memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data)); | |
d2b6417b | 2176 | pnt += hdr->length + 3; |
d62a17ae | 2177 | |
2178 | /* We know MP Capability Code. */ | |
2179 | if (hdr->code == CAPABILITY_CODE_MP) { | |
2180 | pkt_afi = ntohs(mpc.afi); | |
2181 | pkt_safi = mpc.safi; | |
2182 | ||
2183 | /* Ignore capability when override-capability is set. */ | |
2184 | if (CHECK_FLAG(peer->flags, | |
2185 | PEER_FLAG_OVERRIDE_CAPABILITY)) | |
2186 | continue; | |
2187 | ||
2188 | /* Convert AFI, SAFI to internal values. */ | |
2189 | if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, | |
2190 | &safi)) { | |
2191 | if (bgp_debug_neighbor_events(peer)) | |
2192 | zlog_debug( | |
3efd0893 | 2193 | "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)", |
748a041f DS |
2194 | peer->host, |
2195 | iana_afi2str(pkt_afi), | |
2196 | iana_safi2str(pkt_safi)); | |
d62a17ae | 2197 | continue; |
2198 | } | |
2199 | ||
2200 | /* Address family check. */ | |
2201 | if (bgp_debug_neighbor_events(peer)) | |
2202 | zlog_debug( | |
2203 | "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u", | |
2204 | peer->host, | |
2205 | action == CAPABILITY_ACTION_SET | |
2206 | ? "Advertising" | |
2207 | : "Removing", | |
2208 | pkt_afi, pkt_safi); | |
2209 | ||
2210 | if (action == CAPABILITY_ACTION_SET) { | |
2211 | peer->afc_recv[afi][safi] = 1; | |
2212 | if (peer->afc[afi][safi]) { | |
2213 | peer->afc_nego[afi][safi] = 1; | |
2214 | bgp_announce_route(peer, afi, safi); | |
2215 | } | |
2216 | } else { | |
2217 | peer->afc_recv[afi][safi] = 0; | |
2218 | peer->afc_nego[afi][safi] = 0; | |
2219 | ||
2220 | if (peer_active_nego(peer)) | |
2221 | bgp_clear_route(peer, afi, safi); | |
2222 | else | |
d8151687 | 2223 | return BGP_Stop; |
d62a17ae | 2224 | } |
2225 | } else { | |
ade6974d | 2226 | flog_warn( |
e50f7cfd | 2227 | EC_BGP_UNRECOGNIZED_CAPABILITY, |
ade6974d QY |
2228 | "%s unrecognized capability code: %d - ignored", |
2229 | peer->host, hdr->code); | |
d62a17ae | 2230 | } |
d62a17ae | 2231 | } |
d8151687 QY |
2232 | |
2233 | /* No FSM action necessary */ | |
2234 | return BGP_PACKET_NOOP; | |
718e3744 | 2235 | } |
2236 | ||
d8151687 QY |
2237 | /** |
2238 | * Parse BGP CAPABILITY message for peer. | |
01b7ce2d | 2239 | * |
d8151687 QY |
2240 | * Exported for unit testing. |
2241 | * | |
2242 | * @param peer | |
2243 | * @param size size of the packet | |
2244 | * @return as in summary | |
01b7ce2d | 2245 | */ |
d62a17ae | 2246 | int bgp_capability_receive(struct peer *peer, bgp_size_t size) |
718e3744 | 2247 | { |
d7c0a89a | 2248 | uint8_t *pnt; |
d62a17ae | 2249 | |
2250 | /* Fetch pointer. */ | |
424ab01d | 2251 | pnt = stream_pnt(peer->curr); |
d62a17ae | 2252 | |
2253 | if (bgp_debug_neighbor_events(peer)) | |
2254 | zlog_debug("%s rcv CAPABILITY", peer->host); | |
2255 | ||
2256 | /* If peer does not have the capability, send notification. */ | |
2257 | if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) { | |
e50f7cfd | 2258 | flog_err(EC_BGP_NO_CAP, |
1c50c1c0 QY |
2259 | "%s [Error] BGP dynamic capability is not enabled", |
2260 | peer->host); | |
d62a17ae | 2261 | bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR, |
2262 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
d8151687 | 2263 | return BGP_Stop; |
d62a17ae | 2264 | } |
2265 | ||
2266 | /* Status must be Established. */ | |
2267 | if (peer->status != Established) { | |
af4c2728 | 2268 | flog_err( |
e50f7cfd | 2269 | EC_BGP_NO_CAP, |
d62a17ae | 2270 | "%s [Error] Dynamic capability packet received under status %s", |
2271 | peer->host, | |
2272 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
0e35025e | 2273 | bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, |
3893aeee | 2274 | bgp_fsm_error_subcode(peer->status)); |
d8151687 | 2275 | return BGP_Stop; |
d62a17ae | 2276 | } |
2277 | ||
2278 | /* Parse packet. */ | |
2279 | return bgp_capability_msg_parse(peer, pnt, size); | |
718e3744 | 2280 | } |
6b0655a2 | 2281 | |
d8151687 QY |
2282 | /** |
2283 | * Processes a peer's input buffer. | |
2284 | * | |
2285 | * This function sidesteps the event loop and directly calls bgp_event_update() | |
2286 | * after processing each BGP message. This is necessary to ensure proper | |
2287 | * ordering of FSM events and unifies the behavior that was present previously, | |
2288 | * whereby some of the packet handling functions would update the FSM and some | |
2289 | * would not, making event flow difficult to understand. Please think twice | |
2290 | * before hacking this. | |
2291 | * | |
2292 | * Thread type: THREAD_EVENT | |
2293 | * @param thread | |
2294 | * @return 0 | |
2295 | */ | |
424ab01d | 2296 | int bgp_process_packet(struct thread *thread) |
718e3744 | 2297 | { |
424ab01d | 2298 | /* Yes first of all get peer pointer. */ |
d8151687 QY |
2299 | struct peer *peer; // peer |
2300 | uint32_t rpkt_quanta_old; // how many packets to read | |
2301 | int fsm_update_result; // return code of bgp_event_update() | |
2302 | int mprc; // message processing return code | |
555e09d4 | 2303 | |
424ab01d | 2304 | peer = THREAD_ARG(thread); |
555e09d4 QY |
2305 | rpkt_quanta_old = atomic_load_explicit(&peer->bgp->rpkt_quanta, |
2306 | memory_order_relaxed); | |
e3c7270d | 2307 | fsm_update_result = 0; |
555e09d4 | 2308 | |
424ab01d | 2309 | /* Guard against scheduled events that occur after peer deletion. */ |
9eb217ff | 2310 | if (peer->status == Deleted || peer->status == Clearing) |
d62a17ae | 2311 | return 0; |
718e3744 | 2312 | |
555e09d4 | 2313 | unsigned int processed = 0; |
d62a17ae | 2314 | |
555e09d4 | 2315 | while (processed < rpkt_quanta_old) { |
d7c0a89a | 2316 | uint8_t type = 0; |
9eb217ff QY |
2317 | bgp_size_t size; |
2318 | char notify_data_length[2]; | |
d62a17ae | 2319 | |
00dffa8c | 2320 | frr_with_mutex(&peer->io_mtx) { |
9eb217ff QY |
2321 | peer->curr = stream_fifo_pop(peer->ibuf); |
2322 | } | |
d62a17ae | 2323 | |
9eb217ff QY |
2324 | if (peer->curr == NULL) // no packets to process, hmm... |
2325 | return 0; | |
d62a17ae | 2326 | |
9eb217ff QY |
2327 | /* skip the marker and copy the packet length */ |
2328 | stream_forward_getp(peer->curr, BGP_MARKER_SIZE); | |
2329 | memcpy(notify_data_length, stream_pnt(peer->curr), 2); | |
2330 | ||
2331 | /* read in the packet length and type */ | |
2332 | size = stream_getw(peer->curr); | |
2333 | type = stream_getc(peer->curr); | |
2334 | ||
584470fb | 2335 | hook_call(bgp_packet_dump, peer, type, size, peer->curr); |
9eb217ff QY |
2336 | |
2337 | /* adjust size to exclude the marker + length + type */ | |
2338 | size -= BGP_HEADER_SIZE; | |
2339 | ||
2340 | /* Read rest of the packet and call each sort of packet routine | |
2341 | */ | |
2342 | switch (type) { | |
2343 | case BGP_MSG_OPEN: | |
0112e9e0 QY |
2344 | atomic_fetch_add_explicit(&peer->open_in, 1, |
2345 | memory_order_relaxed); | |
d8151687 QY |
2346 | mprc = bgp_open_receive(peer, size); |
2347 | if (mprc == BGP_Stop) | |
af4c2728 | 2348 | flog_err( |
e50f7cfd | 2349 | EC_BGP_PKT_OPEN, |
d8151687 | 2350 | "%s: BGP OPEN receipt failed for peer: %s", |
0767b4f3 | 2351 | __func__, peer->host); |
9eb217ff QY |
2352 | break; |
2353 | case BGP_MSG_UPDATE: | |
0112e9e0 QY |
2354 | atomic_fetch_add_explicit(&peer->update_in, 1, |
2355 | memory_order_relaxed); | |
9eb217ff | 2356 | peer->readtime = monotime(NULL); |
d8151687 QY |
2357 | mprc = bgp_update_receive(peer, size); |
2358 | if (mprc == BGP_Stop) | |
af4c2728 | 2359 | flog_err( |
e50f7cfd | 2360 | EC_BGP_UPDATE_RCV, |
d8151687 | 2361 | "%s: BGP UPDATE receipt failed for peer: %s", |
0767b4f3 | 2362 | __func__, peer->host); |
9eb217ff QY |
2363 | break; |
2364 | case BGP_MSG_NOTIFY: | |
0112e9e0 QY |
2365 | atomic_fetch_add_explicit(&peer->notify_in, 1, |
2366 | memory_order_relaxed); | |
d8151687 QY |
2367 | mprc = bgp_notify_receive(peer, size); |
2368 | if (mprc == BGP_Stop) | |
af4c2728 | 2369 | flog_err( |
e50f7cfd | 2370 | EC_BGP_NOTIFY_RCV, |
d8151687 | 2371 | "%s: BGP NOTIFY receipt failed for peer: %s", |
0767b4f3 | 2372 | __func__, peer->host); |
9eb217ff QY |
2373 | break; |
2374 | case BGP_MSG_KEEPALIVE: | |
2375 | peer->readtime = monotime(NULL); | |
0112e9e0 QY |
2376 | atomic_fetch_add_explicit(&peer->keepalive_in, 1, |
2377 | memory_order_relaxed); | |
d8151687 QY |
2378 | mprc = bgp_keepalive_receive(peer, size); |
2379 | if (mprc == BGP_Stop) | |
af4c2728 | 2380 | flog_err( |
e50f7cfd | 2381 | EC_BGP_KEEP_RCV, |
d8151687 | 2382 | "%s: BGP KEEPALIVE receipt failed for peer: %s", |
0767b4f3 | 2383 | __func__, peer->host); |
9eb217ff QY |
2384 | break; |
2385 | case BGP_MSG_ROUTE_REFRESH_NEW: | |
2386 | case BGP_MSG_ROUTE_REFRESH_OLD: | |
0112e9e0 QY |
2387 | atomic_fetch_add_explicit(&peer->refresh_in, 1, |
2388 | memory_order_relaxed); | |
d8151687 QY |
2389 | mprc = bgp_route_refresh_receive(peer, size); |
2390 | if (mprc == BGP_Stop) | |
af4c2728 | 2391 | flog_err( |
e50f7cfd | 2392 | EC_BGP_RFSH_RCV, |
d8151687 | 2393 | "%s: BGP ROUTEREFRESH receipt failed for peer: %s", |
0767b4f3 | 2394 | __func__, peer->host); |
9eb217ff QY |
2395 | break; |
2396 | case BGP_MSG_CAPABILITY: | |
0112e9e0 QY |
2397 | atomic_fetch_add_explicit(&peer->dynamic_cap_in, 1, |
2398 | memory_order_relaxed); | |
d8151687 QY |
2399 | mprc = bgp_capability_receive(peer, size); |
2400 | if (mprc == BGP_Stop) | |
af4c2728 | 2401 | flog_err( |
e50f7cfd | 2402 | EC_BGP_CAP_RCV, |
d8151687 | 2403 | "%s: BGP CAPABILITY receipt failed for peer: %s", |
0767b4f3 | 2404 | __func__, peer->host); |
9eb217ff | 2405 | break; |
e3c7270d | 2406 | default: |
db878db0 QY |
2407 | /* Suppress uninitialized variable warning */ |
2408 | mprc = 0; | |
5041dc4f | 2409 | (void)mprc; |
becedef6 QY |
2410 | /* |
2411 | * The message type should have been sanitized before | |
2412 | * we ever got here. Receipt of a message with an | |
2413 | * invalid header at this point is indicative of a | |
2414 | * security issue. | |
2415 | */ | |
e3c7270d | 2416 | assert (!"Message of invalid type received during input processing"); |
9eb217ff QY |
2417 | } |
2418 | ||
d8151687 QY |
2419 | /* delete processed packet */ |
2420 | stream_free(peer->curr); | |
2421 | peer->curr = NULL; | |
2422 | processed++; | |
9eb217ff | 2423 | |
d8151687 QY |
2424 | /* Update FSM */ |
2425 | if (mprc != BGP_PACKET_NOOP) | |
2426 | fsm_update_result = bgp_event_update(peer, mprc); | |
e3c7270d QY |
2427 | else |
2428 | continue; | |
d8151687 | 2429 | |
becedef6 QY |
2430 | /* |
2431 | * If peer was deleted, do not process any more packets. This | |
2432 | * is usually due to executing BGP_Stop or a stub deletion. | |
2433 | */ | |
d8151687 QY |
2434 | if (fsm_update_result == FSM_PEER_TRANSFERRED |
2435 | || fsm_update_result == FSM_PEER_STOPPED) | |
2436 | break; | |
d62a17ae | 2437 | } |
2438 | ||
d8151687 QY |
2439 | if (fsm_update_result != FSM_PEER_TRANSFERRED |
2440 | && fsm_update_result != FSM_PEER_STOPPED) { | |
00dffa8c | 2441 | frr_with_mutex(&peer->io_mtx) { |
becedef6 QY |
2442 | // more work to do, come back later |
2443 | if (peer->ibuf->count > 0) | |
4af76660 QY |
2444 | thread_add_timer_msec( |
2445 | bm->master, bgp_process_packet, peer, 0, | |
2446 | &peer->t_process_packet); | |
d8151687 | 2447 | } |
718e3744 | 2448 | } |
2449 | ||
d62a17ae | 2450 | return 0; |
718e3744 | 2451 | } |
9e3b51a7 | 2452 | |
2453 | /* Send EOR when routes are processed by selection deferral timer */ | |
2454 | void bgp_send_delayed_eor(struct bgp *bgp) | |
2455 | { | |
2456 | struct peer *peer; | |
2457 | struct listnode *node, *nnode; | |
2458 | ||
2459 | /* EOR message sent in bgp_write_proceed_actions */ | |
2460 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) | |
2461 | bgp_write_proceed_actions(peer); | |
2462 | } |