]>
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" |
d9a03c57 | 66 | #include "bgpd/bgp_trace.h" |
718e3744 | 67 | |
584470fb DL |
68 | DEFINE_HOOK(bgp_packet_dump, |
69 | (struct peer *peer, uint8_t type, bgp_size_t size, | |
70 | struct stream *s), | |
8451921b | 71 | (peer, type, size, s)); |
584470fb | 72 | |
6fd04594 DL |
73 | DEFINE_HOOK(bgp_packet_send, |
74 | (struct peer *peer, uint8_t type, bgp_size_t size, | |
75 | struct stream *s), | |
8451921b | 76 | (peer, type, size, s)); |
6fd04594 | 77 | |
d8151687 QY |
78 | /** |
79 | * Sets marker and type fields for a BGP message. | |
80 | * | |
81 | * @param s the stream containing the packet | |
82 | * @param type the packet type | |
83 | * @return the size of the stream | |
84 | */ | |
d7c0a89a | 85 | int bgp_packet_set_marker(struct stream *s, uint8_t type) |
718e3744 | 86 | { |
d62a17ae | 87 | int i; |
718e3744 | 88 | |
d62a17ae | 89 | /* Fill in marker. */ |
90 | for (i = 0; i < BGP_MARKER_SIZE; i++) | |
91 | stream_putc(s, 0xff); | |
718e3744 | 92 | |
d62a17ae | 93 | /* Dummy total length. This field is should be filled in later on. */ |
94 | stream_putw(s, 0); | |
718e3744 | 95 | |
d62a17ae | 96 | /* BGP packet type. */ |
97 | stream_putc(s, type); | |
718e3744 | 98 | |
d62a17ae | 99 | /* Return current stream size. */ |
100 | return stream_get_endp(s); | |
718e3744 | 101 | } |
102 | ||
d8151687 QY |
103 | /** |
104 | * Sets size field for a BGP message. | |
105 | * | |
106 | * Size field is set to the size of the stream passed. | |
107 | * | |
108 | * @param s the stream containing the packet | |
d8151687 | 109 | */ |
65baedca | 110 | void 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 | } |
118 | ||
d3ecc69e QY |
119 | /* |
120 | * Push a packet onto the beginning of the peer's output queue. | |
121 | * This function acquires the peer's write mutex before proceeding. | |
122 | */ | |
123 | static void bgp_packet_add(struct peer *peer, struct stream *s) | |
124 | { | |
bd9fb6f3 DL |
125 | intmax_t delta; |
126 | uint32_t holdtime; | |
127 | ||
cb1991af | 128 | frr_with_mutex (&peer->io_mtx) { |
bd9fb6f3 DL |
129 | /* if the queue is empty, reset the "last OK" timestamp to |
130 | * now, otherwise if we write another packet immediately | |
131 | * after it'll get confused | |
132 | */ | |
133 | if (!stream_fifo_count_safe(peer->obuf)) | |
083ec940 | 134 | peer->last_sendq_ok = monotime(NULL); |
bd9fb6f3 | 135 | |
00dffa8c | 136 | stream_fifo_push(peer->obuf, s); |
bd9fb6f3 | 137 | |
083ec940 | 138 | delta = monotime(NULL) - peer->last_sendq_ok; |
bd9fb6f3 DL |
139 | holdtime = atomic_load_explicit(&peer->holdtime, |
140 | memory_order_relaxed); | |
141 | ||
142 | /* Note that when we're here, we're adding some packet to the | |
143 | * OutQ. That includes keepalives when there is nothing to | |
144 | * do, so there's a guarantee we pass by here once in a while. | |
145 | * | |
146 | * That implies there is no need to go set up another separate | |
147 | * timer that ticks down SendHoldTime, as we'll be here sooner | |
148 | * or later anyway and will see the checks below failing. | |
149 | */ | |
382268f4 DL |
150 | if (!holdtime) { |
151 | /* no holdtime, do nothing. */ | |
152 | } else if (delta > 2 * (intmax_t)holdtime) { | |
bd9fb6f3 DL |
153 | flog_err( |
154 | EC_BGP_SENDQ_STUCK_PROPER, | |
155 | "%s has not made any SendQ progress for 2 holdtimes, terminating session", | |
156 | peer->host); | |
157 | BGP_EVENT_ADD(peer, TCP_fatal_error); | |
158 | } else if (delta > (intmax_t)holdtime && | |
083ec940 | 159 | monotime(NULL) - peer->last_sendq_warn > 5) { |
bd9fb6f3 DL |
160 | flog_warn( |
161 | EC_BGP_SENDQ_STUCK_WARN, | |
162 | "%s has not made any SendQ progress for 1 holdtime, peer overloaded?", | |
163 | peer->host); | |
083ec940 | 164 | peer->last_sendq_warn = monotime(NULL); |
bd9fb6f3 | 165 | } |
00dffa8c | 166 | } |
718e3744 | 167 | } |
168 | ||
d62a17ae | 169 | static struct stream *bgp_update_packet_eor(struct peer *peer, afi_t afi, |
170 | safi_t safi) | |
93406d87 | 171 | { |
d62a17ae | 172 | struct stream *s; |
617975d1 DS |
173 | iana_afi_t pkt_afi = IANA_AFI_IPV4; |
174 | iana_safi_t pkt_safi = IANA_SAFI_UNICAST; | |
d62a17ae | 175 | |
176 | if (DISABLE_BGP_ANNOUNCE) | |
177 | return NULL; | |
178 | ||
179 | if (bgp_debug_neighbor_events(peer)) | |
180 | zlog_debug("send End-of-RIB for %s to %s", | |
5cb5f4d0 | 181 | get_afi_safi_str(afi, safi, false), peer->host); |
d62a17ae | 182 | |
ef56aee4 | 183 | s = stream_new(peer->max_packet_size); |
d62a17ae | 184 | |
185 | /* Make BGP update packet. */ | |
186 | bgp_packet_set_marker(s, BGP_MSG_UPDATE); | |
187 | ||
188 | /* Unfeasible Routes Length */ | |
189 | stream_putw(s, 0); | |
190 | ||
191 | if (afi == AFI_IP && safi == SAFI_UNICAST) { | |
192 | /* Total Path Attribute Length */ | |
193 | stream_putw(s, 0); | |
194 | } else { | |
195 | /* Convert AFI, SAFI to values for packet. */ | |
196 | bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi); | |
197 | ||
198 | /* Total Path Attribute Length */ | |
199 | stream_putw(s, 6); | |
200 | stream_putc(s, BGP_ATTR_FLAG_OPTIONAL); | |
201 | stream_putc(s, BGP_ATTR_MP_UNREACH_NLRI); | |
202 | stream_putc(s, 3); | |
203 | stream_putw(s, pkt_afi); | |
204 | stream_putc(s, pkt_safi); | |
205 | } | |
206 | ||
207 | bgp_packet_set_size(s); | |
d62a17ae | 208 | return s; |
718e3744 | 209 | } |
210 | ||
d8151687 QY |
211 | /* Called when there is a change in the EOR(implicit or explicit) status of a |
212 | * peer. Ends the update-delay if all expected peers are done with EORs. */ | |
213 | void bgp_check_update_delay(struct bgp *bgp) | |
214 | { | |
215 | struct listnode *node, *nnode; | |
216 | struct peer *peer = NULL; | |
217 | ||
218 | if (bgp_debug_neighbor_events(peer)) | |
219 | zlog_debug("Checking update delay, T: %d R: %d I:%d E: %d", | |
220 | bgp->established, bgp->restarted_peers, | |
221 | bgp->implicit_eors, bgp->explicit_eors); | |
222 | ||
223 | if (bgp->established | |
224 | <= bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) { | |
becedef6 QY |
225 | /* |
226 | * This is an extra sanity check to make sure we wait for all | |
227 | * the eligible configured peers. This check is performed if | |
228 | * establish wait timer is on, or establish wait option is not | |
229 | * given with the update-delay command | |
230 | */ | |
d8151687 QY |
231 | if (bgp->t_establish_wait |
232 | || (bgp->v_establish_wait == bgp->v_update_delay)) | |
233 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) { | |
234 | if (CHECK_FLAG(peer->flags, | |
235 | PEER_FLAG_CONFIG_NODE) | |
236 | && !CHECK_FLAG(peer->flags, | |
237 | PEER_FLAG_SHUTDOWN) | |
cb9196e7 DS |
238 | && !CHECK_FLAG(peer->bgp->flags, |
239 | BGP_FLAG_SHUTDOWN) | |
d8151687 QY |
240 | && !peer->update_delay_over) { |
241 | if (bgp_debug_neighbor_events(peer)) | |
242 | zlog_debug( | |
243 | " Peer %s pending, continuing read-only mode", | |
244 | peer->host); | |
245 | return; | |
246 | } | |
247 | } | |
248 | ||
249 | zlog_info( | |
250 | "Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d", | |
251 | bgp->restarted_peers, bgp->implicit_eors, | |
252 | bgp->explicit_eors); | |
253 | bgp_update_delay_end(bgp); | |
254 | } | |
255 | } | |
256 | ||
becedef6 QY |
257 | /* |
258 | * Called if peer is known to have restarted. The restart-state bit in | |
259 | * Graceful-Restart capability is used for that | |
260 | */ | |
d8151687 QY |
261 | void bgp_update_restarted_peers(struct peer *peer) |
262 | { | |
263 | if (!bgp_update_delay_active(peer->bgp)) | |
264 | return; /* BGP update delay has ended */ | |
265 | if (peer->update_delay_over) | |
266 | return; /* This peer has already been considered */ | |
267 | ||
268 | if (bgp_debug_neighbor_events(peer)) | |
269 | zlog_debug("Peer %s: Checking restarted", peer->host); | |
270 | ||
feb17238 | 271 | if (peer_established(peer)) { |
d8151687 QY |
272 | peer->update_delay_over = 1; |
273 | peer->bgp->restarted_peers++; | |
274 | bgp_check_update_delay(peer->bgp); | |
275 | } | |
276 | } | |
277 | ||
becedef6 QY |
278 | /* |
279 | * Called as peer receives a keep-alive. Determines if this occurence can be | |
280 | * taken as an implicit EOR for this peer. | |
281 | * NOTE: The very first keep-alive after the Established state of a peer is | |
282 | * considered implicit EOR for the update-delay purposes | |
283 | */ | |
d8151687 QY |
284 | void bgp_update_implicit_eors(struct peer *peer) |
285 | { | |
286 | if (!bgp_update_delay_active(peer->bgp)) | |
287 | return; /* BGP update delay has ended */ | |
288 | if (peer->update_delay_over) | |
289 | return; /* This peer has already been considered */ | |
290 | ||
291 | if (bgp_debug_neighbor_events(peer)) | |
292 | zlog_debug("Peer %s: Checking implicit EORs", peer->host); | |
293 | ||
feb17238 | 294 | if (peer_established(peer)) { |
d8151687 QY |
295 | peer->update_delay_over = 1; |
296 | peer->bgp->implicit_eors++; | |
297 | bgp_check_update_delay(peer->bgp); | |
298 | } | |
299 | } | |
300 | ||
becedef6 QY |
301 | /* |
302 | * Should be called only when there is a change in the EOR_RECEIVED status | |
303 | * for any afi/safi on a peer. | |
304 | */ | |
d8151687 QY |
305 | static void bgp_update_explicit_eors(struct peer *peer) |
306 | { | |
307 | afi_t afi; | |
308 | safi_t safi; | |
309 | ||
310 | if (!bgp_update_delay_active(peer->bgp)) | |
311 | return; /* BGP update delay has ended */ | |
312 | if (peer->update_delay_over) | |
313 | return; /* This peer has already been considered */ | |
314 | ||
315 | if (bgp_debug_neighbor_events(peer)) | |
316 | zlog_debug("Peer %s: Checking explicit EORs", peer->host); | |
317 | ||
f18ba3cd DS |
318 | FOREACH_AFI_SAFI (afi, safi) { |
319 | if (peer->afc_nego[afi][safi] | |
320 | && !CHECK_FLAG(peer->af_sflags[afi][safi], | |
321 | PEER_STATUS_EOR_RECEIVED)) { | |
322 | if (bgp_debug_neighbor_events(peer)) | |
323 | zlog_debug( | |
324 | " afi %d safi %d didn't receive EOR", | |
325 | afi, safi); | |
326 | return; | |
d8151687 | 327 | } |
f18ba3cd | 328 | } |
d8151687 QY |
329 | |
330 | peer->update_delay_over = 1; | |
331 | peer->bgp->explicit_eors++; | |
332 | bgp_check_update_delay(peer->bgp); | |
333 | } | |
334 | ||
335 | /** | |
336 | * Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers. | |
337 | * | |
338 | * mp_withdraw, if set, is used to nullify attr structure on most of the | |
339 | * calling safi function and for evpn, passed as parameter | |
340 | */ | |
341 | int bgp_nlri_parse(struct peer *peer, struct attr *attr, | |
342 | struct bgp_nlri *packet, int mp_withdraw) | |
343 | { | |
344 | switch (packet->safi) { | |
345 | case SAFI_UNICAST: | |
346 | case SAFI_MULTICAST: | |
347 | return bgp_nlri_parse_ip(peer, mp_withdraw ? NULL : attr, | |
348 | packet); | |
349 | case SAFI_LABELED_UNICAST: | |
350 | return bgp_nlri_parse_label(peer, mp_withdraw ? NULL : attr, | |
351 | packet); | |
352 | case SAFI_MPLS_VPN: | |
353 | return bgp_nlri_parse_vpn(peer, mp_withdraw ? NULL : attr, | |
354 | packet); | |
355 | case SAFI_EVPN: | |
356 | return bgp_nlri_parse_evpn(peer, attr, packet, mp_withdraw); | |
7c40bf39 | 357 | case SAFI_FLOWSPEC: |
358 | return bgp_nlri_parse_flowspec(peer, attr, packet, mp_withdraw); | |
d8151687 | 359 | } |
513386b5 | 360 | return BGP_NLRI_PARSE_ERROR; |
d8151687 QY |
361 | } |
362 | ||
6ec98a2f QY |
363 | /* |
364 | * Checks a variety of conditions to determine whether the peer needs to be | |
365 | * rescheduled for packet generation again, and does so if necessary. | |
366 | * | |
367 | * @param peer to check for rescheduling | |
368 | */ | |
af1e1dc6 QY |
369 | static void bgp_write_proceed_actions(struct peer *peer) |
370 | { | |
371 | afi_t afi; | |
372 | safi_t safi; | |
373 | struct peer_af *paf; | |
374 | struct bpacket *next_pkt; | |
375 | struct update_subgroup *subgrp; | |
0e5cdd59 | 376 | enum bgp_af_index index; |
af1e1dc6 | 377 | |
0e5cdd59 DS |
378 | for (index = BGP_AF_START; index < BGP_AF_MAX; index++) { |
379 | paf = peer->peer_af_array[index]; | |
c58b0f46 QY |
380 | if (!paf) |
381 | continue; | |
0e5cdd59 | 382 | |
c58b0f46 QY |
383 | subgrp = paf->subgroup; |
384 | if (!subgrp) | |
385 | continue; | |
386 | ||
387 | next_pkt = paf->next_pkt_to_send; | |
388 | if (next_pkt && next_pkt->buffer) { | |
389 | BGP_TIMER_ON(peer->t_generate_updgrp_packets, | |
390 | bgp_generate_updgrp_packets, 0); | |
391 | return; | |
392 | } | |
af1e1dc6 | 393 | |
c58b0f46 QY |
394 | /* No packets readily available for AFI/SAFI, are there |
395 | * subgroup packets | |
396 | * that need to be generated? */ | |
397 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), | |
398 | SUBGRP_PKTQ(subgrp)) | |
399 | || subgroup_packets_to_build(subgrp)) { | |
400 | BGP_TIMER_ON(peer->t_generate_updgrp_packets, | |
401 | bgp_generate_updgrp_packets, 0); | |
402 | return; | |
403 | } | |
af1e1dc6 | 404 | |
0e5cdd59 DS |
405 | afi = paf->afi; |
406 | safi = paf->safi; | |
407 | ||
c58b0f46 QY |
408 | /* No packets to send, see if EOR is pending */ |
409 | if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV)) { | |
410 | if (!subgrp->t_coalesce && peer->afc_nego[afi][safi] | |
411 | && peer->synctime | |
412 | && !CHECK_FLAG(peer->af_sflags[afi][safi], | |
413 | PEER_STATUS_EOR_SEND) | |
414 | && safi != SAFI_MPLS_VPN) { | |
af1e1dc6 QY |
415 | BGP_TIMER_ON(peer->t_generate_updgrp_packets, |
416 | bgp_generate_updgrp_packets, 0); | |
417 | return; | |
418 | } | |
af1e1dc6 | 419 | } |
c58b0f46 | 420 | } |
af1e1dc6 QY |
421 | } |
422 | ||
6ec98a2f QY |
423 | /* |
424 | * Generate advertisement information (withdraws, updates, EOR) from each | |
425 | * update group a peer belongs to, encode this information into packets, and | |
426 | * enqueue the packets onto the peer's output buffer. | |
56257a44 | 427 | */ |
cc9f21da | 428 | void bgp_generate_updgrp_packets(struct thread *thread) |
718e3744 | 429 | { |
56257a44 | 430 | struct peer *peer = THREAD_ARG(thread); |
56257a44 QY |
431 | |
432 | struct stream *s; | |
d62a17ae | 433 | struct peer_af *paf; |
434 | struct bpacket *next_pkt; | |
d0ad6d8e QY |
435 | uint32_t wpq; |
436 | uint32_t generated = 0; | |
d62a17ae | 437 | afi_t afi; |
438 | safi_t safi; | |
439 | ||
d0ad6d8e QY |
440 | wpq = atomic_load_explicit(&peer->bgp->wpkt_quanta, |
441 | memory_order_relaxed); | |
442 | ||
d62a17ae | 443 | /* |
444 | * The code beyond this part deals with update packets, proceed only | |
445 | * if peer is Established and updates are not on hold (as part of | |
f4d2dd84 | 446 | * update-delay processing). |
3f9c7369 | 447 | */ |
feb17238 | 448 | if (!peer_established(peer)) |
cc9f21da | 449 | return; |
d62a17ae | 450 | |
f4d2dd84 DS |
451 | if ((peer->bgp->main_peers_update_hold) |
452 | || bgp_update_delay_active(peer->bgp)) | |
cc9f21da | 453 | return; |
d62a17ae | 454 | |
b10b6d52 | 455 | if (peer->t_routeadv) |
cc9f21da | 456 | return; |
b10b6d52 | 457 | |
56257a44 | 458 | do { |
0e5cdd59 DS |
459 | enum bgp_af_index index; |
460 | ||
56257a44 | 461 | s = NULL; |
0e5cdd59 DS |
462 | for (index = BGP_AF_START; index < BGP_AF_MAX; index++) { |
463 | paf = peer->peer_af_array[index]; | |
c58b0f46 QY |
464 | if (!paf || !PAF_SUBGRP(paf)) |
465 | continue; | |
0e5cdd59 DS |
466 | |
467 | afi = paf->afi; | |
468 | safi = paf->safi; | |
c58b0f46 | 469 | next_pkt = paf->next_pkt_to_send; |
80bd61c4 | 470 | |
c58b0f46 QY |
471 | /* |
472 | * Try to generate a packet for the peer if we are at | |
473 | * the end of the list. Always try to push out | |
474 | * WITHDRAWs first. | |
475 | */ | |
476 | if (!next_pkt || !next_pkt->buffer) { | |
477 | next_pkt = subgroup_withdraw_packet( | |
478 | PAF_SUBGRP(paf)); | |
479 | if (!next_pkt || !next_pkt->buffer) | |
480 | subgroup_update_packet(PAF_SUBGRP(paf)); | |
481 | next_pkt = paf->next_pkt_to_send; | |
482 | } | |
80bd61c4 | 483 | |
c58b0f46 QY |
484 | /* |
485 | * If we still don't have a packet to send to the peer, | |
486 | * then try to find out out if we have to send eor or | |
487 | * if not, skip to the next AFI, SAFI. Don't send the | |
488 | * EOR prematurely; if the subgroup's coalesce timer is | |
489 | * running, the adjacency-out structure is not created | |
490 | * yet. | |
491 | */ | |
492 | if (!next_pkt || !next_pkt->buffer) { | |
9af52ccf DA |
493 | if (!paf->t_announce_route) { |
494 | /* Make sure we supress BGP UPDATES | |
495 | * for normal processing later again. | |
496 | */ | |
2adac256 DA |
497 | UNSET_FLAG(paf->subgroup->sflags, |
498 | SUBGRP_STATUS_FORCE_UPDATES); | |
499 | ||
9af52ccf DA |
500 | /* If route-refresh BoRR message was |
501 | * already sent and we are done with | |
502 | * re-announcing tables for a decent | |
503 | * afi/safi, we ready to send | |
504 | * EoRR request. | |
505 | */ | |
506 | if (CHECK_FLAG( | |
507 | peer->af_sflags[afi][safi], | |
508 | PEER_STATUS_BORR_SEND)) { | |
509 | bgp_route_refresh_send( | |
510 | peer, afi, safi, 0, 0, | |
511 | 0, | |
512 | BGP_ROUTE_REFRESH_EORR); | |
513 | ||
514 | SET_FLAG(peer->af_sflags[afi] | |
515 | [safi], | |
516 | PEER_STATUS_EORR_SEND); | |
517 | UNSET_FLAG( | |
518 | peer->af_sflags[afi] | |
519 | [safi], | |
520 | PEER_STATUS_BORR_SEND); | |
521 | ||
522 | if (bgp_debug_neighbor_events( | |
523 | peer)) | |
524 | zlog_debug( | |
f70c91dc DA |
525 | "%pBP sending route-refresh (EoRR) for %s/%s", |
526 | peer, | |
9af52ccf DA |
527 | afi2str(afi), |
528 | safi2str(safi)); | |
529 | } | |
530 | } | |
531 | ||
c58b0f46 | 532 | if (CHECK_FLAG(peer->cap, |
36235319 | 533 | PEER_CAP_RESTART_RCV)) { |
c58b0f46 | 534 | if (!(PAF_SUBGRP(paf))->t_coalesce |
36235319 QY |
535 | && peer->afc_nego[afi][safi] |
536 | && peer->synctime | |
537 | && !CHECK_FLAG( | |
538 | peer->af_sflags[afi][safi], | |
539 | PEER_STATUS_EOR_SEND)) { | |
d6e3c15b | 540 | /* If EOR is disabled, |
541 | * the message is not sent | |
542 | */ | |
36235319 QY |
543 | if (BGP_SEND_EOR(peer->bgp, afi, |
544 | safi)) { | |
d6e3c15b | 545 | SET_FLAG( |
36235319 QY |
546 | peer->af_sflags |
547 | [afi] | |
548 | [safi], | |
549 | PEER_STATUS_EOR_SEND); | |
d6e3c15b | 550 | |
9e3b51a7 | 551 | /* Update EOR |
552 | * send time | |
553 | */ | |
36235319 QY |
554 | peer->eor_stime[afi] |
555 | [safi] = | |
556 | monotime(NULL); | |
9e3b51a7 | 557 | |
d6e3c15b | 558 | BGP_UPDATE_EOR_PKT( |
36235319 QY |
559 | peer, afi, safi, |
560 | s); | |
56257a44 | 561 | } |
80bd61c4 | 562 | } |
d62a17ae | 563 | } |
c58b0f46 QY |
564 | continue; |
565 | } | |
9e3b51a7 | 566 | |
567 | /* Update packet send time */ | |
568 | peer->pkt_stime[afi][safi] = monotime(NULL); | |
569 | ||
c58b0f46 QY |
570 | /* Found a packet template to send, overwrite |
571 | * packet with appropriate attributes from peer | |
572 | * and advance peer */ | |
573 | s = bpacket_reformat_for_peer(next_pkt, paf); | |
574 | bgp_packet_add(peer, s); | |
c58b0f46 QY |
575 | bpacket_queue_advance_peer(paf); |
576 | } | |
d0ad6d8e | 577 | } while (s && (++generated < wpq)); |
80bd61c4 | 578 | |
6ec98a2f QY |
579 | if (generated) |
580 | bgp_writes_on(peer); | |
581 | ||
af1e1dc6 | 582 | bgp_write_proceed_actions(peer); |
80bd61c4 QY |
583 | } |
584 | ||
d3ecc69e QY |
585 | /* |
586 | * Creates a BGP Keepalive packet and appends it to the peer's output queue. | |
587 | */ | |
d62a17ae | 588 | void bgp_keepalive_send(struct peer *peer) |
718e3744 | 589 | { |
d62a17ae | 590 | struct stream *s; |
591 | ||
556beacf | 592 | s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE); |
718e3744 | 593 | |
d62a17ae | 594 | /* Make keepalive packet. */ |
595 | bgp_packet_set_marker(s, BGP_MSG_KEEPALIVE); | |
718e3744 | 596 | |
d62a17ae | 597 | /* Set packet size. */ |
65baedca | 598 | bgp_packet_set_size(s); |
718e3744 | 599 | |
d62a17ae | 600 | /* Dump packet if debug option is set. */ |
601 | /* bgp_packet_dump (s); */ | |
718e3744 | 602 | |
d62a17ae | 603 | if (bgp_debug_keepalive(peer)) |
604 | zlog_debug("%s sending KEEPALIVE", peer->host); | |
718e3744 | 605 | |
d62a17ae | 606 | /* Add packet to the peer. */ |
607 | bgp_packet_add(peer, s); | |
424ab01d QY |
608 | |
609 | bgp_writes_on(peer); | |
718e3744 | 610 | } |
611 | ||
d3ecc69e QY |
612 | /* |
613 | * Creates a BGP Open packet and appends it to the peer's output queue. | |
614 | * Sets capabilities as necessary. | |
615 | */ | |
d62a17ae | 616 | void bgp_open_send(struct peer *peer) |
718e3744 | 617 | { |
d62a17ae | 618 | struct stream *s; |
d7c0a89a | 619 | uint16_t send_holdtime; |
d62a17ae | 620 | as_t local_as; |
718e3744 | 621 | |
b90a8e13 | 622 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) |
d62a17ae | 623 | send_holdtime = peer->holdtime; |
624 | else | |
625 | send_holdtime = peer->bgp->default_holdtime; | |
718e3744 | 626 | |
d62a17ae | 627 | /* local-as Change */ |
628 | if (peer->change_local_as) | |
629 | local_as = peer->change_local_as; | |
630 | else | |
631 | local_as = peer->local_as; | |
718e3744 | 632 | |
556beacf | 633 | s = stream_new(BGP_STANDARD_MESSAGE_MAX_PACKET_SIZE); |
718e3744 | 634 | |
d62a17ae | 635 | /* Make open packet. */ |
636 | bgp_packet_set_marker(s, BGP_MSG_OPEN); | |
718e3744 | 637 | |
d62a17ae | 638 | /* Set open packet values. */ |
639 | stream_putc(s, BGP_VERSION_4); /* BGP version */ | |
d7c0a89a QY |
640 | stream_putw(s, (local_as <= BGP_AS_MAX) ? (uint16_t)local_as |
641 | : BGP_AS_TRANS); | |
d62a17ae | 642 | stream_putw(s, send_holdtime); /* Hold Time */ |
643 | stream_put_in_addr(s, &peer->local_id); /* BGP Identifier */ | |
718e3744 | 644 | |
d08c0c80 DA |
645 | /* Set capabilities */ |
646 | if (CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) { | |
647 | (void)bgp_open_capability(s, peer, true); | |
648 | } else { | |
649 | struct stream *tmp = stream_new(STREAM_SIZE(s)); | |
650 | ||
651 | stream_copy(tmp, s); | |
652 | if (bgp_open_capability(tmp, peer, false) | |
653 | > BGP_OPEN_NON_EXT_OPT_LEN) { | |
654 | stream_free(tmp); | |
655 | (void)bgp_open_capability(s, peer, true); | |
656 | } else { | |
657 | stream_copy(s, tmp); | |
658 | stream_free(tmp); | |
659 | } | |
660 | } | |
718e3744 | 661 | |
d62a17ae | 662 | /* Set BGP packet length. */ |
65baedca | 663 | bgp_packet_set_size(s); |
718e3744 | 664 | |
d62a17ae | 665 | if (bgp_debug_neighbor_events(peer)) |
666 | zlog_debug( | |
23d0a753 | 667 | "%s sending OPEN, version %d, my as %u, holdtime %d, id %pI4", |
d62a17ae | 668 | peer->host, BGP_VERSION_4, local_as, send_holdtime, |
23d0a753 | 669 | &peer->local_id); |
718e3744 | 670 | |
d62a17ae | 671 | /* Dump packet if debug option is set. */ |
672 | /* bgp_packet_dump (s); */ | |
6fd04594 | 673 | hook_call(bgp_packet_send, peer, BGP_MSG_OPEN, stream_get_endp(s), s); |
718e3744 | 674 | |
d62a17ae | 675 | /* Add packet to the peer. */ |
676 | bgp_packet_add(peer, s); | |
424ab01d QY |
677 | |
678 | bgp_writes_on(peer); | |
679 | } | |
680 | ||
a127f33b QY |
681 | /* |
682 | * Writes NOTIFICATION message directly to a peer socket without waiting for | |
683 | * the I/O thread. | |
684 | * | |
685 | * There must be exactly one stream on the peer->obuf FIFO, and the data within | |
686 | * this stream must match the format of a BGP NOTIFICATION message. | |
687 | * Transmission is best-effort. | |
688 | * | |
689 | * @requires peer->io_mtx | |
690 | * @param peer | |
691 | * @return 0 | |
692 | */ | |
3dc339cd | 693 | static void bgp_write_notify(struct peer *peer) |
424ab01d QY |
694 | { |
695 | int ret, val; | |
d7c0a89a | 696 | uint8_t type; |
424ab01d QY |
697 | struct stream *s; |
698 | ||
a127f33b QY |
699 | /* There should be at least one packet. */ |
700 | s = stream_fifo_pop(peer->obuf); | |
424ab01d | 701 | |
8ec586b0 | 702 | if (!s) |
3dc339cd | 703 | return; |
8ec586b0 QY |
704 | |
705 | assert(stream_get_endp(s) >= BGP_HEADER_SIZE); | |
706 | ||
becedef6 QY |
707 | /* |
708 | * socket is in nonblocking mode, if we can't deliver the NOTIFY, well, | |
709 | * we only care about getting a clean shutdown at this point. | |
710 | */ | |
424ab01d QY |
711 | ret = write(peer->fd, STREAM_DATA(s), stream_get_endp(s)); |
712 | ||
becedef6 QY |
713 | /* |
714 | * only connection reset/close gets counted as TCP_fatal_error, failure | |
715 | * to write the entire NOTIFY doesn't get different FSM treatment | |
716 | */ | |
424ab01d | 717 | if (ret <= 0) { |
3735936b | 718 | stream_free(s); |
424ab01d | 719 | BGP_EVENT_ADD(peer, TCP_fatal_error); |
3dc339cd | 720 | return; |
424ab01d QY |
721 | } |
722 | ||
723 | /* Disable Nagle, make NOTIFY packet go out right away */ | |
724 | val = 1; | |
725 | (void)setsockopt(peer->fd, IPPROTO_TCP, TCP_NODELAY, (char *)&val, | |
726 | sizeof(val)); | |
727 | ||
728 | /* Retrieve BGP packet type. */ | |
729 | stream_set_getp(s, BGP_MARKER_SIZE + 2); | |
730 | type = stream_getc(s); | |
731 | ||
732 | assert(type == BGP_MSG_NOTIFY); | |
733 | ||
734 | /* Type should be notify. */ | |
0112e9e0 | 735 | atomic_fetch_add_explicit(&peer->notify_out, 1, memory_order_relaxed); |
424ab01d QY |
736 | |
737 | /* Double start timer. */ | |
738 | peer->v_start *= 2; | |
739 | ||
740 | /* Overflow check. */ | |
741 | if (peer->v_start >= (60 * 2)) | |
742 | peer->v_start = (60 * 2); | |
743 | ||
becedef6 QY |
744 | /* |
745 | * Handle Graceful Restart case where the state changes to | |
746 | * Connect instead of Idle | |
747 | */ | |
424ab01d QY |
748 | BGP_EVENT_ADD(peer, BGP_Stop); |
749 | ||
3735936b | 750 | stream_free(s); |
718e3744 | 751 | } |
752 | ||
eea685b6 DA |
753 | /* |
754 | * Encapsulate an original BGP CEASE Notification into Hard Reset | |
755 | */ | |
756 | static uint8_t *bgp_notify_encapsulate_hard_reset(uint8_t code, uint8_t subcode, | |
757 | uint8_t *data, size_t datalen) | |
758 | { | |
759 | uint8_t *message = XCALLOC(MTYPE_BGP_NOTIFICATION, datalen + 2); | |
760 | ||
761 | /* ErrCode */ | |
762 | message[0] = code; | |
763 | /* Subcode */ | |
764 | message[1] = subcode; | |
765 | /* Data */ | |
766 | if (datalen) | |
767 | memcpy(message + 2, data, datalen); | |
768 | ||
769 | return message; | |
770 | } | |
771 | ||
772 | /* | |
773 | * Decapsulate an original BGP CEASE Notification from Hard Reset | |
774 | */ | |
775 | struct bgp_notify bgp_notify_decapsulate_hard_reset(struct bgp_notify *notify) | |
776 | { | |
777 | struct bgp_notify bn = {}; | |
778 | ||
779 | bn.code = notify->raw_data[0]; | |
780 | bn.subcode = notify->raw_data[1]; | |
781 | bn.length = notify->length - 2; | |
782 | ||
c73d2363 | 783 | bn.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, bn.length); |
eea685b6 DA |
784 | memcpy(bn.raw_data, notify->raw_data + 2, bn.length); |
785 | ||
786 | return bn; | |
787 | } | |
788 | ||
20170775 DA |
789 | /* Check if Graceful-Restart N-bit is exchanged */ |
790 | bool bgp_has_graceful_restart_notification(struct peer *peer) | |
791 | { | |
792 | return CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_RCV) && | |
793 | CHECK_FLAG(peer->cap, PEER_CAP_GRACEFUL_RESTART_N_BIT_ADV); | |
794 | } | |
795 | ||
eea685b6 DA |
796 | /* |
797 | * Check if to send BGP CEASE Notification/Hard Reset? | |
798 | */ | |
1ae314be DA |
799 | bool bgp_notify_send_hard_reset(struct peer *peer, uint8_t code, |
800 | uint8_t subcode) | |
eea685b6 DA |
801 | { |
802 | /* When the "N" bit has been exchanged, a Hard Reset message is used to | |
803 | * indicate to the peer that the session is to be fully terminated. | |
804 | */ | |
20170775 | 805 | if (!bgp_has_graceful_restart_notification(peer)) |
eea685b6 DA |
806 | return false; |
807 | ||
808 | /* | |
809 | * https://datatracker.ietf.org/doc/html/rfc8538#section-5.1 | |
810 | */ | |
1ae314be | 811 | if (code == BGP_NOTIFY_CEASE) { |
eea685b6 DA |
812 | switch (subcode) { |
813 | case BGP_NOTIFY_CEASE_MAX_PREFIX: | |
814 | case BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN: | |
815 | case BGP_NOTIFY_CEASE_PEER_UNCONFIG: | |
816 | case BGP_NOTIFY_CEASE_HARD_RESET: | |
aebe2e37 | 817 | case BGP_NOTIFY_CEASE_BFD_DOWN: |
eea685b6 | 818 | return true; |
1ae314be DA |
819 | case BGP_NOTIFY_CEASE_ADMIN_RESET: |
820 | /* Provide user control: | |
821 | * `bgp hard-adminstrative-reset` | |
822 | */ | |
823 | if (CHECK_FLAG(peer->bgp->flags, | |
824 | BGP_FLAG_HARD_ADMIN_RESET)) | |
825 | return true; | |
826 | else | |
827 | return false; | |
eea685b6 DA |
828 | default: |
829 | break; | |
830 | } | |
831 | } | |
832 | ||
833 | return false; | |
834 | } | |
835 | ||
1ae314be DA |
836 | /* |
837 | * Check if received BGP CEASE Notification/Hard Reset? | |
838 | */ | |
839 | bool bgp_notify_received_hard_reset(struct peer *peer, uint8_t code, | |
840 | uint8_t subcode) | |
841 | { | |
842 | /* When the "N" bit has been exchanged, a Hard Reset message is used to | |
843 | * indicate to the peer that the session is to be fully terminated. | |
844 | */ | |
20170775 | 845 | if (!bgp_has_graceful_restart_notification(peer)) |
1ae314be DA |
846 | return false; |
847 | ||
848 | if (code == BGP_NOTIFY_CEASE && subcode == BGP_NOTIFY_CEASE_HARD_RESET) | |
849 | return true; | |
850 | ||
851 | return false; | |
852 | } | |
853 | ||
d3ecc69e QY |
854 | /* |
855 | * Creates a BGP Notify and appends it to the peer's output queue. | |
856 | * | |
becedef6 QY |
857 | * This function attempts to write the packet from the thread it is called |
858 | * from, to ensure the packet gets out ASAP. | |
d3ecc69e | 859 | * |
a127f33b QY |
860 | * This function may be called from multiple threads. Since the function |
861 | * modifies I/O buffer(s) in the peer, these are locked for the duration of the | |
862 | * call to prevent tampering from other threads. | |
863 | * | |
864 | * Delivery of the NOTIFICATION is attempted once and is best-effort. After | |
865 | * return, the peer structure *must* be reset; no assumptions about session | |
866 | * state are valid. | |
867 | * | |
d3ecc69e QY |
868 | * @param peer |
869 | * @param code BGP error code | |
870 | * @param sub_code BGP error subcode | |
871 | * @param data Data portion | |
872 | * @param datalen length of data portion | |
873 | */ | |
71ca5b09 MS |
874 | static void bgp_notify_send_internal(struct peer *peer, uint8_t code, |
875 | uint8_t sub_code, uint8_t *data, | |
876 | size_t datalen, bool use_curr) | |
718e3744 | 877 | { |
d62a17ae | 878 | struct stream *s; |
1ae314be | 879 | bool hard_reset = bgp_notify_send_hard_reset(peer, code, sub_code); |
d62a17ae | 880 | |
a127f33b | 881 | /* Lock I/O mutex to prevent other threads from pushing packets */ |
00dffa8c | 882 | frr_mutex_lock_autounlock(&peer->io_mtx); |
a127f33b QY |
883 | /* ============================================== */ |
884 | ||
d62a17ae | 885 | /* Allocate new stream. */ |
ef56aee4 | 886 | s = stream_new(peer->max_packet_size); |
d62a17ae | 887 | |
d3ecc69e | 888 | /* Make notify packet. */ |
d62a17ae | 889 | bgp_packet_set_marker(s, BGP_MSG_NOTIFY); |
890 | ||
eea685b6 DA |
891 | /* Check if we should send Hard Reset Notification or not */ |
892 | if (hard_reset) { | |
893 | uint8_t *hard_reset_message = bgp_notify_encapsulate_hard_reset( | |
894 | code, sub_code, data, datalen); | |
d62a17ae | 895 | |
eea685b6 DA |
896 | /* Hard Reset encapsulates another NOTIFICATION message |
897 | * in its data portion. | |
898 | */ | |
899 | stream_putc(s, BGP_NOTIFY_CEASE); | |
900 | stream_putc(s, BGP_NOTIFY_CEASE_HARD_RESET); | |
901 | stream_write(s, hard_reset_message, datalen + 2); | |
902 | ||
903 | XFREE(MTYPE_BGP_NOTIFICATION, hard_reset_message); | |
904 | } else { | |
905 | stream_putc(s, code); | |
906 | stream_putc(s, sub_code); | |
907 | if (data) | |
908 | stream_write(s, data, datalen); | |
909 | } | |
d62a17ae | 910 | |
911 | /* Set BGP packet length. */ | |
bd6b2706 | 912 | bgp_packet_set_size(s); |
d62a17ae | 913 | |
424ab01d | 914 | /* wipe output buffer */ |
a127f33b | 915 | stream_fifo_clean(peer->obuf); |
d62a17ae | 916 | |
becedef6 QY |
917 | /* |
918 | * If possible, store last packet for debugging purposes. This check is | |
919 | * in place because we are sometimes called with a doppelganger peer, | |
920 | * who tends to have a plethora of fields nulled out. | |
71ca5b09 MS |
921 | * |
922 | * Some callers should not attempt this - the io pthread for example | |
923 | * should not touch internals of the peer struct. | |
becedef6 | 924 | */ |
71ca5b09 | 925 | if (use_curr && peer->curr) { |
d8151687 | 926 | size_t packetsize = stream_get_endp(peer->curr); |
556beacf | 927 | assert(packetsize <= peer->max_packet_size); |
d8151687 QY |
928 | memcpy(peer->last_reset_cause, peer->curr->data, packetsize); |
929 | peer->last_reset_cause_size = packetsize; | |
930 | } | |
931 | ||
d62a17ae | 932 | /* For debug */ |
933 | { | |
934 | struct bgp_notify bgp_notify; | |
935 | int first = 0; | |
936 | int i; | |
937 | char c[4]; | |
938 | ||
939 | bgp_notify.code = code; | |
940 | bgp_notify.subcode = sub_code; | |
941 | bgp_notify.data = NULL; | |
e0981960 | 942 | bgp_notify.length = datalen; |
d62a17ae | 943 | bgp_notify.raw_data = data; |
944 | ||
945 | peer->notify.code = bgp_notify.code; | |
946 | peer->notify.subcode = bgp_notify.subcode; | |
947 | ||
e0981960 | 948 | if (bgp_notify.length && data) { |
d62a17ae | 949 | bgp_notify.data = |
950 | XMALLOC(MTYPE_TMP, bgp_notify.length * 3); | |
951 | for (i = 0; i < bgp_notify.length; i++) | |
952 | if (first) { | |
552d6491 QY |
953 | snprintf(c, sizeof(c), " %02x", |
954 | data[i]); | |
f009ff26 | 955 | |
552d6491 | 956 | strlcat(bgp_notify.data, c, |
2ba1fe69 | 957 | bgp_notify.length); |
f009ff26 | 958 | |
d62a17ae | 959 | } else { |
960 | first = 1; | |
552d6491 | 961 | snprintf(c, sizeof(c), "%02x", data[i]); |
f009ff26 | 962 | |
552d6491 | 963 | strlcpy(bgp_notify.data, c, |
2ba1fe69 | 964 | bgp_notify.length); |
d62a17ae | 965 | } |
966 | } | |
eea685b6 | 967 | bgp_notify_print(peer, &bgp_notify, "sending", hard_reset); |
d62a17ae | 968 | |
969 | if (bgp_notify.data) { | |
970 | XFREE(MTYPE_TMP, bgp_notify.data); | |
d62a17ae | 971 | bgp_notify.length = 0; |
972 | } | |
973 | } | |
974 | ||
975 | /* peer reset cause */ | |
976 | if (code == BGP_NOTIFY_CEASE) { | |
977 | if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET) | |
978 | peer->last_reset = PEER_DOWN_USER_RESET; | |
979 | else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN) | |
980 | peer->last_reset = PEER_DOWN_USER_SHUTDOWN; | |
981 | else | |
982 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; | |
983 | } else | |
984 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; | |
985 | ||
d3ecc69e | 986 | /* Add packet to peer's output queue */ |
a127f33b | 987 | stream_fifo_push(peer->obuf, s); |
424ab01d | 988 | |
5cce3f05 | 989 | bgp_peer_gr_flags_update(peer); |
36235319 QY |
990 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp, |
991 | peer->bgp->peer); | |
5cce3f05 | 992 | |
424ab01d | 993 | bgp_write_notify(peer); |
718e3744 | 994 | } |
995 | ||
d3ecc69e QY |
996 | /* |
997 | * Creates a BGP Notify and appends it to the peer's output queue. | |
998 | * | |
becedef6 QY |
999 | * This function attempts to write the packet from the thread it is called |
1000 | * from, to ensure the packet gets out ASAP. | |
d3ecc69e QY |
1001 | * |
1002 | * @param peer | |
1003 | * @param code BGP error code | |
1004 | * @param sub_code BGP error subcode | |
1005 | */ | |
d7c0a89a | 1006 | void bgp_notify_send(struct peer *peer, uint8_t code, uint8_t sub_code) |
718e3744 | 1007 | { |
71ca5b09 MS |
1008 | bgp_notify_send_internal(peer, code, sub_code, NULL, 0, true); |
1009 | } | |
1010 | ||
1011 | /* | |
1012 | * Enqueue notification; called from the main pthread, peer object access is ok. | |
1013 | */ | |
1014 | void bgp_notify_send_with_data(struct peer *peer, uint8_t code, | |
1015 | uint8_t sub_code, uint8_t *data, size_t datalen) | |
1016 | { | |
1017 | bgp_notify_send_internal(peer, code, sub_code, data, datalen, true); | |
1018 | } | |
1019 | ||
1020 | /* | |
1021 | * For use by the io pthread, queueing a notification but avoiding access to | |
1022 | * the peer object. | |
1023 | */ | |
1024 | void bgp_notify_io_invalid(struct peer *peer, uint8_t code, uint8_t sub_code, | |
1025 | uint8_t *data, size_t datalen) | |
1026 | { | |
1027 | /* Avoid touching the peer object */ | |
1028 | bgp_notify_send_internal(peer, code, sub_code, data, datalen, false); | |
718e3744 | 1029 | } |
1030 | ||
d3ecc69e QY |
1031 | /* |
1032 | * Creates BGP Route Refresh packet and appends it to the peer's output queue. | |
1033 | * | |
1034 | * @param peer | |
1035 | * @param afi Address Family Identifier | |
1036 | * @param safi Subsequent Address Family Identifier | |
1037 | * @param orf_type Outbound Route Filtering type | |
1038 | * @param when_to_refresh Whether to refresh immediately or defer | |
1039 | * @param remove Whether to remove ORF for specified AFI/SAFI | |
1040 | */ | |
d62a17ae | 1041 | void bgp_route_refresh_send(struct peer *peer, afi_t afi, safi_t safi, |
d7c0a89a | 1042 | uint8_t orf_type, uint8_t when_to_refresh, |
9af52ccf | 1043 | int remove, uint8_t subtype) |
718e3744 | 1044 | { |
d62a17ae | 1045 | struct stream *s; |
1046 | struct bgp_filter *filter; | |
1047 | int orf_refresh = 0; | |
617975d1 DS |
1048 | iana_afi_t pkt_afi = IANA_AFI_IPV4; |
1049 | iana_safi_t pkt_safi = IANA_SAFI_UNICAST; | |
d62a17ae | 1050 | |
1051 | if (DISABLE_BGP_ANNOUNCE) | |
1052 | return; | |
1053 | ||
1054 | filter = &peer->filter[afi][safi]; | |
1055 | ||
1056 | /* Convert AFI, SAFI to values for packet. */ | |
1057 | bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi); | |
1058 | ||
ef56aee4 | 1059 | s = stream_new(peer->max_packet_size); |
d62a17ae | 1060 | |
1061 | /* Make BGP update packet. */ | |
1062 | if (CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_NEW_RCV)) | |
1063 | bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_NEW); | |
718e3744 | 1064 | else |
d62a17ae | 1065 | bgp_packet_set_marker(s, BGP_MSG_ROUTE_REFRESH_OLD); |
1066 | ||
1067 | /* Encode Route Refresh message. */ | |
1068 | stream_putw(s, pkt_afi); | |
9af52ccf DA |
1069 | if (subtype) |
1070 | stream_putc(s, subtype); | |
1071 | else | |
1072 | stream_putc(s, 0); | |
d62a17ae | 1073 | stream_putc(s, pkt_safi); |
1074 | ||
1075 | if (orf_type == ORF_TYPE_PREFIX || orf_type == ORF_TYPE_PREFIX_OLD) | |
1076 | if (remove || filter->plist[FILTER_IN].plist) { | |
d7c0a89a | 1077 | uint16_t orf_len; |
d62a17ae | 1078 | unsigned long orfp; |
1079 | ||
1080 | orf_refresh = 1; | |
1081 | stream_putc(s, when_to_refresh); | |
1082 | stream_putc(s, orf_type); | |
1083 | orfp = stream_get_endp(s); | |
1084 | stream_putw(s, 0); | |
1085 | ||
1086 | if (remove) { | |
1087 | UNSET_FLAG(peer->af_sflags[afi][safi], | |
1088 | PEER_STATUS_ORF_PREFIX_SEND); | |
1089 | stream_putc(s, ORF_COMMON_PART_REMOVE_ALL); | |
1090 | if (bgp_debug_neighbor_events(peer)) | |
1091 | zlog_debug( | |
f70c91dc DA |
1092 | "%pBP sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %s/%s", |
1093 | peer, orf_type, | |
a7d91a8c DA |
1094 | (when_to_refresh == |
1095 | REFRESH_DEFER | |
d62a17ae | 1096 | ? "defer" |
1097 | : "immediate"), | |
748a041f DS |
1098 | iana_afi2str(pkt_afi), |
1099 | iana_safi2str(pkt_safi)); | |
d62a17ae | 1100 | } else { |
1101 | SET_FLAG(peer->af_sflags[afi][safi], | |
1102 | PEER_STATUS_ORF_PREFIX_SEND); | |
1103 | prefix_bgp_orf_entry( | |
1104 | s, filter->plist[FILTER_IN].plist, | |
1105 | ORF_COMMON_PART_ADD, | |
1106 | ORF_COMMON_PART_PERMIT, | |
1107 | ORF_COMMON_PART_DENY); | |
1108 | if (bgp_debug_neighbor_events(peer)) | |
1109 | zlog_debug( | |
f70c91dc DA |
1110 | "%pBP sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %s/%s", |
1111 | peer, orf_type, | |
a7d91a8c DA |
1112 | (when_to_refresh == |
1113 | REFRESH_DEFER | |
d62a17ae | 1114 | ? "defer" |
1115 | : "immediate"), | |
748a041f DS |
1116 | iana_afi2str(pkt_afi), |
1117 | iana_safi2str(pkt_safi)); | |
d62a17ae | 1118 | } |
1119 | ||
1120 | /* Total ORF Entry Len. */ | |
1121 | orf_len = stream_get_endp(s) - orfp - 2; | |
1122 | stream_putw_at(s, orfp, orf_len); | |
1123 | } | |
1124 | ||
1125 | /* Set packet size. */ | |
65baedca | 1126 | bgp_packet_set_size(s); |
d62a17ae | 1127 | |
1128 | if (bgp_debug_neighbor_events(peer)) { | |
1129 | if (!orf_refresh) | |
a7d91a8c | 1130 | zlog_debug( |
f70c91dc DA |
1131 | "%pBP sending REFRESH_REQ for afi/safi: %s/%s", |
1132 | peer, iana_afi2str(pkt_afi), | |
1133 | iana_safi2str(pkt_safi)); | |
d62a17ae | 1134 | } |
1135 | ||
1136 | /* Add packet to the peer. */ | |
1137 | bgp_packet_add(peer, s); | |
424ab01d QY |
1138 | |
1139 | bgp_writes_on(peer); | |
718e3744 | 1140 | } |
1141 | ||
d3ecc69e QY |
1142 | /* |
1143 | * Create a BGP Capability packet and append it to the peer's output queue. | |
1144 | * | |
1145 | * @param peer | |
1146 | * @param afi Address Family Identifier | |
1147 | * @param safi Subsequent Address Family Identifier | |
1148 | * @param capability_code BGP Capability Code | |
1149 | * @param action Set or Remove capability | |
1150 | */ | |
d62a17ae | 1151 | void bgp_capability_send(struct peer *peer, afi_t afi, safi_t safi, |
1152 | int capability_code, int action) | |
718e3744 | 1153 | { |
d62a17ae | 1154 | struct stream *s; |
617975d1 DS |
1155 | iana_afi_t pkt_afi = IANA_AFI_IPV4; |
1156 | iana_safi_t pkt_safi = IANA_SAFI_UNICAST; | |
d62a17ae | 1157 | |
1158 | /* Convert AFI, SAFI to values for packet. */ | |
1159 | bgp_map_afi_safi_int2iana(afi, safi, &pkt_afi, &pkt_safi); | |
1160 | ||
ef56aee4 | 1161 | s = stream_new(peer->max_packet_size); |
d62a17ae | 1162 | |
1163 | /* Make BGP update packet. */ | |
1164 | bgp_packet_set_marker(s, BGP_MSG_CAPABILITY); | |
1165 | ||
1166 | /* Encode MP_EXT capability. */ | |
1167 | if (capability_code == CAPABILITY_CODE_MP) { | |
1168 | stream_putc(s, action); | |
1169 | stream_putc(s, CAPABILITY_CODE_MP); | |
1170 | stream_putc(s, CAPABILITY_CODE_MP_LEN); | |
1171 | stream_putw(s, pkt_afi); | |
1172 | stream_putc(s, 0); | |
1173 | stream_putc(s, pkt_safi); | |
1174 | ||
1175 | if (bgp_debug_neighbor_events(peer)) | |
1176 | zlog_debug( | |
f70c91dc DA |
1177 | "%pBP sending CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s", |
1178 | peer, | |
d62a17ae | 1179 | action == CAPABILITY_ACTION_SET ? "Advertising" |
1180 | : "Removing", | |
748a041f | 1181 | iana_afi2str(pkt_afi), iana_safi2str(pkt_safi)); |
d62a17ae | 1182 | } |
1183 | ||
1184 | /* Set packet size. */ | |
65baedca | 1185 | bgp_packet_set_size(s); |
718e3744 | 1186 | |
d62a17ae | 1187 | /* Add packet to the peer. */ |
1188 | bgp_packet_add(peer, s); | |
424ab01d QY |
1189 | |
1190 | bgp_writes_on(peer); | |
d62a17ae | 1191 | } |
718e3744 | 1192 | |
d62a17ae | 1193 | /* RFC1771 6.8 Connection collision detection. */ |
1194 | static int bgp_collision_detect(struct peer *new, struct in_addr remote_id) | |
1195 | { | |
1196 | struct peer *peer; | |
1197 | ||
f88221f3 DS |
1198 | /* |
1199 | * Upon receipt of an OPEN message, the local system must examine | |
1200 | * all of its connections that are in the OpenConfirm state. A BGP | |
1201 | * speaker may also examine connections in an OpenSent state if it | |
1202 | * knows the BGP Identifier of the peer by means outside of the | |
1203 | * protocol. If among these connections there is a connection to a | |
1204 | * remote BGP speaker whose BGP Identifier equals the one in the | |
1205 | * OPEN message, then the local system performs the following | |
1206 | * collision resolution procedure: | |
1207 | */ | |
1208 | peer = new->doppelganger; | |
1209 | if (peer == NULL) | |
1210 | return 0; | |
1211 | ||
1212 | /* | |
1213 | * Do not accept the new connection in Established or Clearing | |
1214 | * states. Note that a peer GR is handled by closing the existing | |
1215 | * connection upon receipt of new one. | |
1216 | */ | |
feb17238 | 1217 | if (peer_established(peer) || peer->status == Clearing) { |
f88221f3 DS |
1218 | bgp_notify_send(new, BGP_NOTIFY_CEASE, |
1219 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1220 | return -1; | |
1221 | } | |
1222 | ||
1223 | if ((peer->status != OpenConfirm) && (peer->status != OpenSent)) | |
1224 | return 0; | |
1225 | ||
1226 | /* | |
1227 | * 1. The BGP Identifier of the local system is | |
1228 | * compared to the BGP Identifier of the remote | |
1229 | * system (as specified in the OPEN message). | |
1230 | * | |
1231 | * If the BGP Identifiers of the peers | |
1232 | * involved in the connection collision | |
1233 | * are identical, then the connection | |
1234 | * initiated by the BGP speaker with the | |
1235 | * larger AS number is preserved. | |
1236 | */ | |
1237 | if (ntohl(peer->local_id.s_addr) < ntohl(remote_id.s_addr) | |
1238 | || (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr) | |
1239 | && peer->local_as < peer->as)) | |
1240 | if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) { | |
1241 | /* | |
1242 | * 2. If the value of the local BGP | |
1243 | * Identifier is less than the remote one, | |
1244 | * the local system closes BGP connection | |
1245 | * that already exists (the one that is | |
1246 | * already in the OpenConfirm state), | |
1247 | * and accepts BGP connection initiated by | |
1248 | * the remote system. | |
1249 | */ | |
1250 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, | |
1251 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1252 | return 1; | |
1253 | } else { | |
1254 | bgp_notify_send(new, BGP_NOTIFY_CEASE, | |
1255 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1256 | return -1; | |
1257 | } | |
1258 | else { | |
1259 | if (ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr) | |
1260 | && peer->local_as == peer->as) | |
1261 | flog_err(EC_BGP_ROUTER_ID_SAME, | |
1262 | "Peer's router-id %pI4 is the same as ours", | |
1263 | &remote_id); | |
1264 | ||
1265 | /* | |
1266 | * 3. Otherwise, the local system closes newly | |
1267 | * created BGP connection (the one associated with the | |
1268 | * newly received OPEN message), and continues to use | |
1269 | * the existing one (the one that is already in the | |
1270 | * OpenConfirm state). | |
d62a17ae | 1271 | */ |
f88221f3 DS |
1272 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) { |
1273 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, | |
1274 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
1275 | return 1; | |
1276 | } else { | |
d62a17ae | 1277 | bgp_notify_send(new, BGP_NOTIFY_CEASE, |
1278 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
95f7965d | 1279 | return -1; |
d62a17ae | 1280 | } |
1281 | } | |
d62a17ae | 1282 | } |
718e3744 | 1283 | |
d8151687 QY |
1284 | /* Packet processing routines ---------------------------------------------- */ |
1285 | /* | |
1286 | * This is a family of functions designed to be called from | |
1287 | * bgp_process_packet(). These functions all share similar behavior and should | |
1288 | * adhere to the following invariants and restrictions: | |
1289 | * | |
1290 | * Return codes | |
1291 | * ------------ | |
1292 | * The return code of any one of those functions should be one of the FSM event | |
1293 | * codes specified in bgpd.h. If a NOTIFY was sent, this event code MUST be | |
1294 | * BGP_Stop. Otherwise, the code SHOULD correspond to the function's expected | |
1295 | * packet type. For example, bgp_open_receive() should return BGP_Stop upon | |
1296 | * error and Receive_OPEN_message otherwise. | |
1297 | * | |
1298 | * If no action is necessary, the correct return code is BGP_PACKET_NOOP as | |
1299 | * defined below. | |
1300 | * | |
1301 | * Side effects | |
1302 | * ------------ | |
1303 | * - May send NOTIFY messages | |
1304 | * - May not modify peer->status | |
1305 | * - May not call bgp_event_update() | |
1306 | */ | |
1307 | ||
1308 | #define BGP_PACKET_NOOP 0 | |
1309 | ||
1310 | /** | |
1311 | * Process BGP OPEN message for peer. | |
1312 | * | |
1313 | * If any errors are encountered in the OPEN message, immediately sends NOTIFY | |
1314 | * and returns BGP_Stop. | |
1315 | * | |
1316 | * @param peer | |
1317 | * @param size size of the packet | |
1318 | * @return as in summary | |
1319 | */ | |
d62a17ae | 1320 | static int bgp_open_receive(struct peer *peer, bgp_size_t size) |
1321 | { | |
1322 | int ret; | |
d7c0a89a | 1323 | uint8_t version; |
d08c0c80 | 1324 | uint16_t optlen; |
d7c0a89a QY |
1325 | uint16_t holdtime; |
1326 | uint16_t send_holdtime; | |
d62a17ae | 1327 | as_t remote_as; |
6dcef54c | 1328 | as_t as4 = 0, as4_be; |
d62a17ae | 1329 | struct in_addr remote_id; |
1330 | int mp_capability; | |
d7c0a89a QY |
1331 | uint8_t notify_data_remote_as[2]; |
1332 | uint8_t notify_data_remote_as4[4]; | |
1333 | uint8_t notify_data_remote_id[4]; | |
1334 | uint16_t *holdtime_ptr; | |
d62a17ae | 1335 | |
1336 | /* Parse open packet. */ | |
424ab01d QY |
1337 | version = stream_getc(peer->curr); |
1338 | memcpy(notify_data_remote_as, stream_pnt(peer->curr), 2); | |
1339 | remote_as = stream_getw(peer->curr); | |
d7c0a89a | 1340 | holdtime_ptr = (uint16_t *)stream_pnt(peer->curr); |
424ab01d QY |
1341 | holdtime = stream_getw(peer->curr); |
1342 | memcpy(notify_data_remote_id, stream_pnt(peer->curr), 4); | |
1343 | remote_id.s_addr = stream_get_ipv4(peer->curr); | |
d62a17ae | 1344 | |
d62a17ae | 1345 | /* BEGIN to read the capability here, but dont do it yet */ |
1346 | mp_capability = 0; | |
424ab01d | 1347 | optlen = stream_getc(peer->curr); |
d62a17ae | 1348 | |
d08c0c80 DA |
1349 | /* Extended Optional Parameters Length for BGP OPEN Message */ |
1350 | if (optlen == BGP_OPEN_NON_EXT_OPT_LEN | |
1351 | || CHECK_FLAG(peer->flags, PEER_FLAG_EXTENDED_OPT_PARAMS)) { | |
1352 | uint8_t opttype; | |
1353 | ||
1354 | opttype = stream_getc(peer->curr); | |
1355 | if (opttype == BGP_OPEN_NON_EXT_OPT_TYPE_EXTENDED_LENGTH) { | |
1356 | optlen = stream_getw(peer->curr); | |
1357 | SET_FLAG(peer->sflags, | |
1358 | PEER_STATUS_EXT_OPT_PARAMS_LENGTH); | |
1359 | } | |
1360 | } | |
1361 | ||
1362 | /* Receive OPEN message log */ | |
1363 | if (bgp_debug_neighbor_events(peer)) | |
1364 | zlog_debug( | |
1365 | "%s rcv OPEN%s, version %d, remote-as (in open) %u, holdtime %d, id %pI4", | |
1366 | peer->host, | |
1367 | CHECK_FLAG(peer->sflags, | |
1368 | PEER_STATUS_EXT_OPT_PARAMS_LENGTH) | |
1369 | ? " (Extended)" | |
1370 | : "", | |
1371 | version, remote_as, holdtime, &remote_id); | |
1372 | ||
d62a17ae | 1373 | if (optlen != 0) { |
1374 | /* If not enough bytes, it is an error. */ | |
424ab01d | 1375 | if (STREAM_READABLE(peer->curr) < optlen) { |
d08c0c80 DA |
1376 | flog_err(EC_BGP_PKT_OPEN, |
1377 | "%s: stream has not enough bytes (%u)", | |
1378 | peer->host, optlen); | |
d62a17ae | 1379 | bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR, |
1380 | BGP_NOTIFY_OPEN_MALFORMED_ATTR); | |
d8151687 | 1381 | return BGP_Stop; |
d62a17ae | 1382 | } |
718e3744 | 1383 | |
d62a17ae | 1384 | /* We need the as4 capability value *right now* because |
1385 | * if it is there, we have not got the remote_as yet, and | |
1386 | * without | |
1387 | * that we do not know which peer is connecting to us now. | |
1388 | */ | |
1389 | as4 = peek_for_as4_capability(peer, optlen); | |
d62a17ae | 1390 | } |
718e3744 | 1391 | |
6dcef54c DL |
1392 | as4_be = htonl(as4); |
1393 | memcpy(notify_data_remote_as4, &as4_be, 4); | |
1394 | ||
d62a17ae | 1395 | /* Just in case we have a silly peer who sends AS4 capability set to 0 |
1396 | */ | |
1397 | if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) && !as4) { | |
e50f7cfd | 1398 | flog_err(EC_BGP_PKT_OPEN, |
1c50c1c0 QY |
1399 | "%s bad OPEN, got AS4 capability, but AS4 set to 0", |
1400 | peer->host); | |
d62a17ae | 1401 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, |
1402 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1403 | notify_data_remote_as4, 4); | |
d8151687 | 1404 | return BGP_Stop; |
d62a17ae | 1405 | } |
718e3744 | 1406 | |
33d022bc DA |
1407 | /* Codification of AS 0 Processing */ |
1408 | if (remote_as == BGP_AS_ZERO) { | |
1409 | flog_err(EC_BGP_PKT_OPEN, "%s bad OPEN, got AS set to 0", | |
1410 | peer->host); | |
1411 | bgp_notify_send(peer, BGP_NOTIFY_OPEN_ERR, | |
1412 | BGP_NOTIFY_OPEN_BAD_PEER_AS); | |
1413 | return BGP_Stop; | |
1414 | } | |
1415 | ||
d62a17ae | 1416 | if (remote_as == BGP_AS_TRANS) { |
1417 | /* Take the AS4 from the capability. We must have received the | |
1418 | * capability now! Otherwise we have a asn16 peer who uses | |
1419 | * BGP_AS_TRANS, for some unknown reason. | |
1420 | */ | |
1421 | if (as4 == BGP_AS_TRANS) { | |
af4c2728 | 1422 | flog_err( |
e50f7cfd | 1423 | EC_BGP_PKT_OPEN, |
d62a17ae | 1424 | "%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed", |
1425 | peer->host); | |
1426 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1427 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1428 | notify_data_remote_as4, 4); | |
d8151687 | 1429 | return BGP_Stop; |
d62a17ae | 1430 | } |
718e3744 | 1431 | |
d62a17ae | 1432 | if (!as4 && BGP_DEBUG(as4, AS4)) |
1433 | zlog_debug( | |
3efd0893 | 1434 | "%s [AS4] OPEN remote_as is AS_TRANS, but no AS4. Odd, but proceeding.", |
d62a17ae | 1435 | peer->host); |
1436 | else if (as4 < BGP_AS_MAX && BGP_DEBUG(as4, AS4)) | |
1437 | zlog_debug( | |
3efd0893 | 1438 | "%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits in 2-bytes, very odd peer.", |
d62a17ae | 1439 | peer->host, as4); |
1440 | if (as4) | |
1441 | remote_as = as4; | |
1442 | } else { | |
1443 | /* We may have a partner with AS4 who has an asno < BGP_AS_MAX | |
1444 | */ | |
1445 | /* If we have got the capability, peer->as4cap must match | |
1446 | * remote_as */ | |
1447 | if (CHECK_FLAG(peer->cap, PEER_CAP_AS4_RCV) | |
1448 | && as4 != remote_as) { | |
1449 | /* raise error, log this, close session */ | |
af4c2728 | 1450 | flog_err( |
e50f7cfd | 1451 | EC_BGP_PKT_OPEN, |
3efd0893 | 1452 | "%s bad OPEN, got AS4 capability, but remote_as %u mismatch with 16bit 'myasn' %u in open", |
d62a17ae | 1453 | peer->host, as4, remote_as); |
1454 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1455 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1456 | notify_data_remote_as4, 4); | |
d8151687 | 1457 | return BGP_Stop; |
d62a17ae | 1458 | } |
1459 | } | |
6b0655a2 | 1460 | |
787c3020 DA |
1461 | /* rfc6286: |
1462 | * If the BGP Identifier field of the OPEN message | |
1463 | * is zero, or if it is the same as the BGP Identifier | |
1464 | * of the local BGP speaker and the message is from an | |
1465 | * internal peer, then the Error Subcode is set to | |
1466 | * "Bad BGP Identifier". | |
1467 | */ | |
975a328e | 1468 | if (remote_id.s_addr == INADDR_ANY |
787c3020 DA |
1469 | || (peer->sort == BGP_PEER_IBGP |
1470 | && ntohl(peer->local_id.s_addr) == ntohl(remote_id.s_addr))) { | |
d62a17ae | 1471 | if (bgp_debug_neighbor_events(peer)) |
23d0a753 DA |
1472 | zlog_debug("%s bad OPEN, wrong router identifier %pI4", |
1473 | peer->host, &remote_id); | |
d62a17ae | 1474 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, |
1475 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, | |
1476 | notify_data_remote_id, 4); | |
d8151687 | 1477 | return BGP_Stop; |
d62a17ae | 1478 | } |
1479 | ||
d62a17ae | 1480 | /* Peer BGP version check. */ |
1481 | if (version != BGP_VERSION_4) { | |
d7c0a89a | 1482 | uint16_t maxver = htons(BGP_VERSION_4); |
d62a17ae | 1483 | /* XXX this reply may not be correct if version < 4 XXX */ |
1484 | if (bgp_debug_neighbor_events(peer)) | |
1485 | zlog_debug( | |
1486 | "%s bad protocol version, remote requested %d, local request %d", | |
1487 | peer->host, version, BGP_VERSION_4); | |
1488 | /* Data must be in network byte order here */ | |
1489 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1490 | BGP_NOTIFY_OPEN_UNSUP_VERSION, | |
d7c0a89a | 1491 | (uint8_t *)&maxver, 2); |
d8151687 | 1492 | return BGP_Stop; |
d62a17ae | 1493 | } |
1494 | ||
1495 | /* Check neighbor as number. */ | |
1496 | if (peer->as_type == AS_UNSPECIFIED) { | |
1497 | if (bgp_debug_neighbor_events(peer)) | |
1498 | zlog_debug( | |
1499 | "%s bad OPEN, remote AS is unspecified currently", | |
1500 | peer->host); | |
1501 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1502 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1503 | notify_data_remote_as, 2); | |
d8151687 | 1504 | return BGP_Stop; |
d62a17ae | 1505 | } else if (peer->as_type == AS_INTERNAL) { |
1506 | if (remote_as != peer->bgp->as) { | |
1507 | if (bgp_debug_neighbor_events(peer)) | |
1508 | zlog_debug( | |
1509 | "%s bad OPEN, remote AS is %u, internal specified", | |
1510 | peer->host, remote_as); | |
1511 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1512 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1513 | notify_data_remote_as, 2); | |
d8151687 | 1514 | return BGP_Stop; |
1ff9a340 | 1515 | } |
d62a17ae | 1516 | peer->as = peer->local_as; |
1517 | } else if (peer->as_type == AS_EXTERNAL) { | |
1518 | if (remote_as == peer->bgp->as) { | |
1519 | if (bgp_debug_neighbor_events(peer)) | |
1520 | zlog_debug( | |
1521 | "%s bad OPEN, remote AS is %u, external specified", | |
1522 | peer->host, remote_as); | |
1523 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1524 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1525 | notify_data_remote_as, 2); | |
d8151687 | 1526 | return BGP_Stop; |
1ff9a340 | 1527 | } |
d62a17ae | 1528 | peer->as = remote_as; |
1529 | } else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) { | |
1530 | if (bgp_debug_neighbor_events(peer)) | |
1531 | zlog_debug("%s bad OPEN, remote AS is %u, expected %u", | |
1532 | peer->host, remote_as, peer->as); | |
1533 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, | |
1534 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1535 | notify_data_remote_as, 2); | |
d8151687 | 1536 | return BGP_Stop; |
eb821189 | 1537 | } |
718e3744 | 1538 | |
7a75470f DS |
1539 | /* |
1540 | * When collision is detected and this peer is closed. | |
1541 | * Return immediately. | |
1542 | */ | |
1543 | ret = bgp_collision_detect(peer, remote_id); | |
1544 | if (ret < 0) | |
1545 | return BGP_Stop; | |
1546 | ||
1547 | /* Get sockname. */ | |
1548 | if (bgp_getsockname(peer) < 0) { | |
1549 | flog_err_sys(EC_LIB_SOCKET, | |
1550 | "%s: bgp_getsockname() failed for peer: %s", | |
1551 | __func__, peer->host); | |
1552 | return BGP_Stop; | |
1553 | } | |
1554 | ||
1555 | /* Set remote router-id */ | |
1556 | peer->remote_id = remote_id; | |
1557 | ||
d62a17ae | 1558 | /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST |
1559 | calculate the value of the Hold Timer by using the smaller of its | |
1560 | configured Hold Time and the Hold Time received in the OPEN message. | |
1561 | The Hold Time MUST be either zero or at least three seconds. An | |
1562 | implementation may reject connections on the basis of the Hold Time. | |
0b2aa3a0 | 1563 | */ |
d62a17ae | 1564 | |
1565 | if (holdtime < 3 && holdtime != 0) { | |
b042667a TI |
1566 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, |
1567 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME, | |
1568 | (uint8_t *)holdtime_ptr, 2); | |
1569 | return BGP_Stop; | |
1570 | } | |
1571 | ||
1572 | /* Send notification message when Hold Time received in the OPEN message | |
1573 | * is smaller than configured minimum Hold Time. */ | |
1574 | if (holdtime < peer->bgp->default_min_holdtime | |
1575 | && peer->bgp->default_min_holdtime != 0) { | |
d62a17ae | 1576 | bgp_notify_send_with_data(peer, BGP_NOTIFY_OPEN_ERR, |
1577 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME, | |
d7c0a89a | 1578 | (uint8_t *)holdtime_ptr, 2); |
d8151687 | 1579 | return BGP_Stop; |
0b2aa3a0 | 1580 | } |
d62a17ae | 1581 | |
1582 | /* From the rfc: A reasonable maximum time between KEEPALIVE messages | |
1583 | would be one third of the Hold Time interval. KEEPALIVE messages | |
1584 | MUST NOT be sent more frequently than one per second. An | |
1585 | implementation MAY adjust the rate at which it sends KEEPALIVE | |
1586 | messages as a function of the Hold Time interval. */ | |
1587 | ||
b90a8e13 | 1588 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) |
d62a17ae | 1589 | send_holdtime = peer->holdtime; |
1590 | else | |
1591 | send_holdtime = peer->bgp->default_holdtime; | |
1592 | ||
1593 | if (holdtime < send_holdtime) | |
1594 | peer->v_holdtime = holdtime; | |
1595 | else | |
1596 | peer->v_holdtime = send_holdtime; | |
1597 | ||
7aa4fd5b TA |
1598 | /* Set effective keepalive to 1/3 the effective holdtime. |
1599 | * Use configured keeplive when < effective keepalive. | |
1600 | */ | |
1601 | peer->v_keepalive = peer->v_holdtime / 3; | |
1602 | if (CHECK_FLAG(peer->flags, PEER_FLAG_TIMER)) { | |
1603 | if (peer->keepalive && peer->keepalive < peer->v_keepalive) | |
1604 | peer->v_keepalive = peer->keepalive; | |
1605 | } else { | |
1606 | if (peer->bgp->default_keepalive | |
1607 | && peer->bgp->default_keepalive < peer->v_keepalive) | |
1608 | peer->v_keepalive = peer->bgp->default_keepalive; | |
1609 | } | |
d62a17ae | 1610 | |
1611 | /* Open option part parse. */ | |
1612 | if (optlen != 0) { | |
1bb379bf | 1613 | if (bgp_open_option_parse(peer, optlen, &mp_capability) < 0) |
d8151687 | 1614 | return BGP_Stop; |
d62a17ae | 1615 | } else { |
1616 | if (bgp_debug_neighbor_events(peer)) | |
1617 | zlog_debug("%s rcvd OPEN w/ OPTION parameter len: 0", | |
1618 | peer->host); | |
0299c004 | 1619 | } |
d62a17ae | 1620 | |
1621 | /* | |
1622 | * Assume that the peer supports the locally configured set of | |
1623 | * AFI/SAFIs if the peer did not send us any Mulitiprotocol | |
1624 | * capabilities, or if 'override-capability' is configured. | |
1625 | */ | |
1626 | if (!mp_capability | |
1627 | || CHECK_FLAG(peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) { | |
1628 | peer->afc_nego[AFI_IP][SAFI_UNICAST] = | |
1629 | peer->afc[AFI_IP][SAFI_UNICAST]; | |
1630 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] = | |
1631 | peer->afc[AFI_IP][SAFI_MULTICAST]; | |
1632 | peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] = | |
1633 | peer->afc[AFI_IP][SAFI_LABELED_UNICAST]; | |
7c40bf39 | 1634 | peer->afc_nego[AFI_IP][SAFI_FLOWSPEC] = |
1635 | peer->afc[AFI_IP][SAFI_FLOWSPEC]; | |
d62a17ae | 1636 | peer->afc_nego[AFI_IP6][SAFI_UNICAST] = |
1637 | peer->afc[AFI_IP6][SAFI_UNICAST]; | |
1638 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = | |
1639 | peer->afc[AFI_IP6][SAFI_MULTICAST]; | |
1640 | peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] = | |
1641 | peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]; | |
1642 | peer->afc_nego[AFI_L2VPN][SAFI_EVPN] = | |
1643 | peer->afc[AFI_L2VPN][SAFI_EVPN]; | |
7c40bf39 | 1644 | peer->afc_nego[AFI_IP6][SAFI_FLOWSPEC] = |
1645 | peer->afc[AFI_IP6][SAFI_FLOWSPEC]; | |
0299c004 | 1646 | } |
d62a17ae | 1647 | |
d62a17ae | 1648 | /* Verify valid local address present based on negotiated |
1649 | * address-families. */ | |
1650 | if (peer->afc_nego[AFI_IP][SAFI_UNICAST] | |
e5f22b30 | 1651 | || peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] |
d62a17ae | 1652 | || peer->afc_nego[AFI_IP][SAFI_MULTICAST] |
1653 | || peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] | |
1654 | || peer->afc_nego[AFI_IP][SAFI_ENCAP]) { | |
975a328e | 1655 | if (peer->nexthop.v4.s_addr == INADDR_ANY) { |
d62a17ae | 1656 | #if defined(HAVE_CUMULUS) |
50121ac0 DS |
1657 | zlog_warn("%s: No local IPv4 addr, BGP routing may not work", |
1658 | peer->host); | |
1d808091 | 1659 | #endif |
d62a17ae | 1660 | } |
1661 | } | |
1662 | if (peer->afc_nego[AFI_IP6][SAFI_UNICAST] | |
e5f22b30 | 1663 | || peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] |
d62a17ae | 1664 | || peer->afc_nego[AFI_IP6][SAFI_MULTICAST] |
1665 | || peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN] | |
1666 | || peer->afc_nego[AFI_IP6][SAFI_ENCAP]) { | |
1667 | if (IN6_IS_ADDR_UNSPECIFIED(&peer->nexthop.v6_global)) { | |
1668 | #if defined(HAVE_CUMULUS) | |
50121ac0 DS |
1669 | zlog_warn("%s: No local IPv6 address, BGP routing may not work", |
1670 | peer->host); | |
1d808091 | 1671 | #endif |
d62a17ae | 1672 | } |
1673 | } | |
1674 | peer->rtt = sockopt_tcp_rtt(peer->fd); | |
1675 | ||
d8151687 | 1676 | return Receive_OPEN_message; |
718e3744 | 1677 | } |
1678 | ||
d8151687 QY |
1679 | /** |
1680 | * Process BGP KEEPALIVE message for peer. | |
1681 | * | |
1682 | * @param peer | |
1683 | * @param size size of the packet | |
1684 | * @return as in summary | |
1685 | */ | |
1686 | static int bgp_keepalive_receive(struct peer *peer, bgp_size_t size) | |
f188f2c4 | 1687 | { |
d8151687 QY |
1688 | if (bgp_debug_keepalive(peer)) |
1689 | zlog_debug("%s KEEPALIVE rcvd", peer->host); | |
d62a17ae | 1690 | |
d8151687 | 1691 | bgp_update_implicit_eors(peer); |
d62a17ae | 1692 | |
e410d563 DA |
1693 | peer->rtt = sockopt_tcp_rtt(peer->fd); |
1694 | ||
8336c896 DA |
1695 | /* If the peer's RTT is higher than expected, shutdown |
1696 | * the peer automatically. | |
1697 | */ | |
1698 | if (CHECK_FLAG(peer->flags, PEER_FLAG_RTT_SHUTDOWN) | |
1699 | && peer->rtt > peer->rtt_expected) { | |
1700 | ||
1701 | peer->rtt_keepalive_rcv++; | |
1702 | ||
1703 | if (peer->rtt_keepalive_rcv > peer->rtt_keepalive_conf) { | |
1704 | zlog_warn( | |
1705 | "%s shutdown due to high round-trip-time (%dms > %dms)", | |
1706 | peer->host, peer->rtt, peer->rtt_expected); | |
1707 | peer_flag_set(peer, PEER_FLAG_SHUTDOWN); | |
1708 | } | |
1709 | } else { | |
1710 | if (peer->rtt_keepalive_rcv) | |
1711 | peer->rtt_keepalive_rcv--; | |
1712 | } | |
1713 | ||
d8151687 | 1714 | return Receive_KEEPALIVE_message; |
f188f2c4 DS |
1715 | } |
1716 | ||
cc9f21da | 1717 | static void bgp_refresh_stalepath_timer_expire(struct thread *thread) |
9af52ccf DA |
1718 | { |
1719 | struct peer_af *paf; | |
1720 | ||
1721 | paf = THREAD_ARG(thread); | |
1722 | ||
1723 | afi_t afi = paf->afi; | |
1724 | safi_t safi = paf->safi; | |
1725 | struct peer *peer = paf->peer; | |
1726 | ||
1727 | peer->t_refresh_stalepath = NULL; | |
1728 | ||
1729 | if (peer->nsf[afi][safi]) | |
1730 | bgp_clear_stale_route(peer, afi, safi); | |
1731 | ||
1732 | if (bgp_debug_neighbor_events(peer)) | |
a7d91a8c | 1733 | zlog_debug( |
f70c91dc DA |
1734 | "%pBP route-refresh (BoRR) timer expired for afi/safi: %d/%d", |
1735 | peer, afi, safi); | |
9af52ccf DA |
1736 | |
1737 | bgp_timer_set(peer); | |
9af52ccf | 1738 | } |
d62a17ae | 1739 | |
d8151687 QY |
1740 | /** |
1741 | * Process BGP UPDATE message for peer. | |
1742 | * | |
1743 | * Parses UPDATE and creates attribute object. | |
1744 | * | |
1745 | * @param peer | |
1746 | * @param size size of the packet | |
1747 | * @return as in summary | |
7ef5a232 | 1748 | */ |
d62a17ae | 1749 | static int bgp_update_receive(struct peer *peer, bgp_size_t size) |
718e3744 | 1750 | { |
d62a17ae | 1751 | int ret, nlri_ret; |
d7c0a89a | 1752 | uint8_t *end; |
d62a17ae | 1753 | struct stream *s; |
1754 | struct attr attr; | |
1755 | bgp_size_t attribute_len; | |
1756 | bgp_size_t update_len; | |
1757 | bgp_size_t withdraw_len; | |
f009ff26 | 1758 | bool restart = false; |
d62a17ae | 1759 | |
1760 | enum NLRI_TYPES { | |
1761 | NLRI_UPDATE, | |
1762 | NLRI_WITHDRAW, | |
1763 | NLRI_MP_UPDATE, | |
1764 | NLRI_MP_WITHDRAW, | |
1765 | NLRI_TYPE_MAX | |
1766 | }; | |
1767 | struct bgp_nlri nlris[NLRI_TYPE_MAX]; | |
1768 | ||
1769 | /* Status must be Established. */ | |
feb17238 | 1770 | if (!peer_established(peer)) { |
e50f7cfd | 1771 | flog_err(EC_BGP_INVALID_STATUS, |
1c50c1c0 QY |
1772 | "%s [FSM] Update packet received under status %s", |
1773 | peer->host, | |
1774 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
0e35025e | 1775 | bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, |
3893aeee | 1776 | bgp_fsm_error_subcode(peer->status)); |
d8151687 | 1777 | return BGP_Stop; |
d62a17ae | 1778 | } |
1779 | ||
1780 | /* Set initial values. */ | |
6006b807 | 1781 | memset(&attr, 0, sizeof(attr)); |
d62a17ae | 1782 | attr.label_index = BGP_INVALID_LABEL_INDEX; |
1783 | attr.label = MPLS_INVALID_LABEL; | |
1784 | memset(&nlris, 0, sizeof(nlris)); | |
1785 | memset(peer->rcvd_attr_str, 0, BUFSIZ); | |
1786 | peer->rcvd_attr_printed = 0; | |
1787 | ||
424ab01d | 1788 | s = peer->curr; |
d62a17ae | 1789 | end = stream_pnt(s) + size; |
1790 | ||
1791 | /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute | |
1792 | Length is too large (i.e., if Unfeasible Routes Length + Total | |
1793 | Attribute Length + 23 exceeds the message Length), then the Error | |
1794 | Subcode is set to Malformed Attribute List. */ | |
1795 | if (stream_pnt(s) + 2 > end) { | |
e50f7cfd | 1796 | flog_err(EC_BGP_UPDATE_RCV, |
3efd0893 | 1797 | "%s [Error] Update packet error (packet length is short for unfeasible length)", |
1c50c1c0 | 1798 | peer->host); |
d62a17ae | 1799 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1800 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1801 | return BGP_Stop; |
d62a17ae | 1802 | } |
1803 | ||
1804 | /* Unfeasible Route Length. */ | |
1805 | withdraw_len = stream_getw(s); | |
1806 | ||
1807 | /* Unfeasible Route Length check. */ | |
1808 | if (stream_pnt(s) + withdraw_len > end) { | |
e50f7cfd | 1809 | flog_err(EC_BGP_UPDATE_RCV, |
3efd0893 | 1810 | "%s [Error] Update packet error (packet unfeasible length overflow %d)", |
1c50c1c0 | 1811 | peer->host, withdraw_len); |
d62a17ae | 1812 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1813 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1814 | return BGP_Stop; |
d62a17ae | 1815 | } |
1816 | ||
1817 | /* Unfeasible Route packet format check. */ | |
1818 | if (withdraw_len > 0) { | |
1819 | nlris[NLRI_WITHDRAW].afi = AFI_IP; | |
1820 | nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST; | |
1821 | nlris[NLRI_WITHDRAW].nlri = stream_pnt(s); | |
1822 | nlris[NLRI_WITHDRAW].length = withdraw_len; | |
1823 | stream_forward_getp(s, withdraw_len); | |
1824 | } | |
1825 | ||
1826 | /* Attribute total length check. */ | |
1827 | if (stream_pnt(s) + 2 > end) { | |
ade6974d | 1828 | flog_warn( |
e50f7cfd | 1829 | EC_BGP_UPDATE_PACKET_SHORT, |
ade6974d QY |
1830 | "%s [Error] Packet Error (update packet is short for attribute length)", |
1831 | peer->host); | |
d62a17ae | 1832 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1833 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1834 | return BGP_Stop; |
d62a17ae | 1835 | } |
1836 | ||
1837 | /* Fetch attribute total length. */ | |
1838 | attribute_len = stream_getw(s); | |
1839 | ||
1840 | /* Attribute length check. */ | |
1841 | if (stream_pnt(s) + attribute_len > end) { | |
ade6974d | 1842 | flog_warn( |
e50f7cfd | 1843 | EC_BGP_UPDATE_PACKET_LONG, |
ade6974d QY |
1844 | "%s [Error] Packet Error (update packet attribute length overflow %d)", |
1845 | peer->host, attribute_len); | |
d62a17ae | 1846 | bgp_notify_send(peer, BGP_NOTIFY_UPDATE_ERR, |
1847 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
d8151687 | 1848 | return BGP_Stop; |
d62a17ae | 1849 | } |
1850 | ||
1851 | /* Certain attribute parsing errors should not be considered bad enough | |
1852 | * to reset the session for, most particularly any partial/optional | |
1853 | * attributes that have 'tunneled' over speakers that don't understand | |
1854 | * them. Instead we withdraw only the prefix concerned. | |
1855 | * | |
1856 | * Complicates the flow a little though.. | |
1857 | */ | |
79288e4c | 1858 | enum bgp_attr_parse_ret attr_parse_ret = BGP_ATTR_PARSE_PROCEED; |
d62a17ae | 1859 | /* This define morphs the update case into a withdraw when lower levels |
1860 | * have signalled an error condition where this is best. | |
1861 | */ | |
b881c707 | 1862 | #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL) |
718e3744 | 1863 | |
d62a17ae | 1864 | /* Parse attribute when it exists. */ |
1865 | if (attribute_len) { | |
1866 | attr_parse_ret = bgp_attr_parse(peer, &attr, attribute_len, | |
1867 | &nlris[NLRI_MP_UPDATE], | |
1868 | &nlris[NLRI_MP_WITHDRAW]); | |
1869 | if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) { | |
1870 | bgp_attr_unintern_sub(&attr); | |
d8151687 | 1871 | return BGP_Stop; |
d62a17ae | 1872 | } |
1873 | } | |
1874 | ||
1875 | /* Logging the attribute. */ | |
1876 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW | |
1877 | || BGP_DEBUG(update, UPDATE_IN) | |
1878 | || BGP_DEBUG(update, UPDATE_PREFIX)) { | |
5022c833 DA |
1879 | ret = bgp_dump_attr(&attr, peer->rcvd_attr_str, |
1880 | sizeof(peer->rcvd_attr_str)); | |
d62a17ae | 1881 | |
b4d46cc9 DL |
1882 | peer->stat_upd_7606++; |
1883 | ||
d62a17ae | 1884 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW) |
af4c2728 | 1885 | flog_err( |
e50f7cfd | 1886 | EC_BGP_UPDATE_RCV, |
f70c91dc DA |
1887 | "%pBP rcvd UPDATE with errors in attr(s)!! Withdrawing route.", |
1888 | peer); | |
d62a17ae | 1889 | |
1890 | if (ret && bgp_debug_update(peer, NULL, NULL, 1)) { | |
f70c91dc | 1891 | zlog_debug("%pBP rcvd UPDATE w/ attr: %s", peer, |
d62a17ae | 1892 | peer->rcvd_attr_str); |
1893 | peer->rcvd_attr_printed = 1; | |
1894 | } | |
1895 | } | |
1896 | ||
1897 | /* Network Layer Reachability Information. */ | |
1898 | update_len = end - stream_pnt(s); | |
1899 | ||
1900 | if (update_len) { | |
1901 | /* Set NLRI portion to structure. */ | |
1902 | nlris[NLRI_UPDATE].afi = AFI_IP; | |
1903 | nlris[NLRI_UPDATE].safi = SAFI_UNICAST; | |
1904 | nlris[NLRI_UPDATE].nlri = stream_pnt(s); | |
1905 | nlris[NLRI_UPDATE].length = update_len; | |
1906 | stream_forward_getp(s, update_len); | |
9738e9aa | 1907 | |
1908 | if (CHECK_FLAG(attr.flag, ATTR_FLAG_BIT(BGP_ATTR_MP_REACH_NLRI))) { | |
1909 | /* | |
1910 | * We skipped nexthop attribute validation earlier so | |
1911 | * validate the nexthop now. | |
1912 | */ | |
1913 | if (bgp_attr_nexthop_valid(peer, &attr) < 0) { | |
1914 | bgp_attr_unintern_sub(&attr); | |
1915 | return BGP_Stop; | |
1916 | } | |
1917 | } | |
d62a17ae | 1918 | } |
1919 | ||
1920 | if (BGP_DEBUG(update, UPDATE_IN)) | |
f70c91dc DA |
1921 | zlog_debug("%pBP rcvd UPDATE wlen %d attrlen %d alen %d", peer, |
1922 | withdraw_len, attribute_len, update_len); | |
d62a17ae | 1923 | |
1924 | /* Parse any given NLRIs */ | |
1925 | for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) { | |
1926 | if (!nlris[i].nlri) | |
1927 | continue; | |
1928 | ||
1929 | /* NLRI is processed iff the peer if configured for the specific | |
1930 | * afi/safi */ | |
1931 | if (!peer->afc[nlris[i].afi][nlris[i].safi]) { | |
1932 | zlog_info( | |
1933 | "%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u", | |
1934 | peer->host, nlris[i].afi, nlris[i].safi); | |
1935 | continue; | |
1936 | } | |
1937 | ||
1938 | /* EoR handled later */ | |
1939 | if (nlris[i].length == 0) | |
1940 | continue; | |
1941 | ||
1942 | switch (i) { | |
1943 | case NLRI_UPDATE: | |
1944 | case NLRI_MP_UPDATE: | |
1945 | nlri_ret = bgp_nlri_parse(peer, NLRI_ATTR_ARG, | |
1946 | &nlris[i], 0); | |
1947 | break; | |
1948 | case NLRI_WITHDRAW: | |
1949 | case NLRI_MP_WITHDRAW: | |
1950 | nlri_ret = bgp_nlri_parse(peer, &attr, &nlris[i], 1); | |
1951 | break; | |
1952 | default: | |
513386b5 | 1953 | nlri_ret = BGP_NLRI_PARSE_ERROR; |
d62a17ae | 1954 | } |
1955 | ||
513386b5 DA |
1956 | if (nlri_ret < BGP_NLRI_PARSE_OK |
1957 | && nlri_ret != BGP_NLRI_PARSE_ERROR_PREFIX_OVERFLOW) { | |
e50f7cfd | 1958 | flog_err(EC_BGP_UPDATE_RCV, |
1c50c1c0 | 1959 | "%s [Error] Error parsing NLRI", peer->host); |
feb17238 | 1960 | if (peer_established(peer)) |
d62a17ae | 1961 | bgp_notify_send( |
1962 | peer, BGP_NOTIFY_UPDATE_ERR, | |
1963 | i <= NLRI_WITHDRAW | |
1964 | ? BGP_NOTIFY_UPDATE_INVAL_NETWORK | |
1965 | : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR); | |
1966 | bgp_attr_unintern_sub(&attr); | |
d8151687 | 1967 | return BGP_Stop; |
d62a17ae | 1968 | } |
1969 | } | |
1970 | ||
1971 | /* EoR checks | |
1972 | * | |
1973 | * Non-MP IPv4/Unicast EoR is a completely empty UPDATE | |
1974 | * and MP EoR should have only an empty MP_UNREACH | |
1975 | */ | |
996c9314 LB |
1976 | if ((!update_len && !withdraw_len && nlris[NLRI_MP_UPDATE].length == 0) |
1977 | || (attr_parse_ret == BGP_ATTR_PARSE_EOR)) { | |
d62a17ae | 1978 | afi_t afi = 0; |
1979 | safi_t safi; | |
f009ff26 | 1980 | struct graceful_restart_info *gr_info; |
1981 | ||
1982 | /* Restarting router */ | |
36235319 QY |
1983 | if (BGP_PEER_GRACEFUL_RESTART_CAPABLE(peer) |
1984 | && BGP_PEER_RESTARTING_MODE(peer)) | |
f009ff26 | 1985 | restart = true; |
d62a17ae | 1986 | |
1987 | /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already | |
1988 | * checked | |
1989 | * update and withdraw NLRI lengths are 0. | |
1990 | */ | |
1991 | if (!attribute_len) { | |
1992 | afi = AFI_IP; | |
1993 | safi = SAFI_UNICAST; | |
1994 | } else if (attr.flag & ATTR_FLAG_BIT(BGP_ATTR_MP_UNREACH_NLRI) | |
1995 | && nlris[NLRI_MP_WITHDRAW].length == 0) { | |
1996 | afi = nlris[NLRI_MP_WITHDRAW].afi; | |
1997 | safi = nlris[NLRI_MP_WITHDRAW].safi; | |
9b9df989 DS |
1998 | } else if (attr_parse_ret == BGP_ATTR_PARSE_EOR) { |
1999 | afi = nlris[NLRI_MP_UPDATE].afi; | |
2000 | safi = nlris[NLRI_MP_UPDATE].safi; | |
d62a17ae | 2001 | } |
2002 | ||
2003 | if (afi && peer->afc[afi][safi]) { | |
e82d19a3 DS |
2004 | struct vrf *vrf = vrf_lookup_by_id(peer->bgp->vrf_id); |
2005 | ||
d62a17ae | 2006 | /* End-of-RIB received */ |
2007 | if (!CHECK_FLAG(peer->af_sflags[afi][safi], | |
2008 | PEER_STATUS_EOR_RECEIVED)) { | |
2009 | SET_FLAG(peer->af_sflags[afi][safi], | |
2010 | PEER_STATUS_EOR_RECEIVED); | |
2011 | bgp_update_explicit_eors(peer); | |
f009ff26 | 2012 | /* Update graceful restart information */ |
2013 | gr_info = &(peer->bgp->gr_info[afi][safi]); | |
2014 | if (restart) | |
2015 | gr_info->eor_received++; | |
2016 | /* If EOR received from all peers and selection | |
2017 | * deferral timer is running, cancel the timer | |
2018 | * and invoke the best path calculation | |
2019 | */ | |
36235319 QY |
2020 | if (gr_info->eor_required |
2021 | == gr_info->eor_received) { | |
2022 | if (bgp_debug_neighbor_events(peer)) | |
2023 | zlog_debug( | |
2024 | "%s %d, %s %d", | |
f009ff26 | 2025 | "EOR REQ", |
2026 | gr_info->eor_required, | |
2027 | "EOR RCV", | |
2028 | gr_info->eor_received); | |
fa5806c3 | 2029 | THREAD_OFF(gr_info->t_select_deferral); |
f009ff26 | 2030 | gr_info->eor_required = 0; |
2031 | gr_info->eor_received = 0; | |
2032 | /* Best path selection */ | |
42c93837 DA |
2033 | bgp_best_path_select_defer(peer->bgp, |
2034 | afi, safi); | |
f009ff26 | 2035 | } |
d62a17ae | 2036 | } |
2037 | ||
2038 | /* NSF delete stale route */ | |
2039 | if (peer->nsf[afi][safi]) | |
2040 | bgp_clear_stale_route(peer, afi, safi); | |
2041 | ||
1479ed2f DA |
2042 | zlog_info( |
2043 | "%s: rcvd End-of-RIB for %s from %s in vrf %s", | |
2044 | __func__, get_afi_safi_str(afi, safi, false), | |
2045 | peer->host, vrf ? vrf->name : VRF_DEFAULT_NAME); | |
2046 | } | |
f80f838b | 2047 | } |
d62a17ae | 2048 | |
2049 | /* Everything is done. We unintern temporary structures which | |
2050 | interned in bgp_attr_parse(). */ | |
2051 | bgp_attr_unintern_sub(&attr); | |
2052 | ||
083ec940 | 2053 | peer->update_time = monotime(NULL); |
d62a17ae | 2054 | |
c385f82a MK |
2055 | /* Notify BGP Conditional advertisement scanner process */ |
2056 | peer->advmap_table_change = true; | |
2057 | ||
d8151687 | 2058 | return Receive_UPDATE_message; |
718e3744 | 2059 | } |
2060 | ||
d8151687 QY |
2061 | /** |
2062 | * Process BGP NOTIFY message for peer. | |
2063 | * | |
2064 | * @param peer | |
2065 | * @param size size of the packet | |
2066 | * @return as in summary | |
2067 | */ | |
2068 | static int bgp_notify_receive(struct peer *peer, bgp_size_t size) | |
718e3744 | 2069 | { |
10d476d4 DA |
2070 | struct bgp_notify outer = {}; |
2071 | struct bgp_notify inner = {}; | |
eea685b6 | 2072 | bool hard_reset = false; |
d62a17ae | 2073 | |
2074 | if (peer->notify.data) { | |
eea685b6 | 2075 | XFREE(MTYPE_BGP_NOTIFICATION, peer->notify.data); |
d62a17ae | 2076 | peer->notify.length = 0; |
e7ce634f | 2077 | peer->notify.hard_reset = false; |
d62a17ae | 2078 | } |
2079 | ||
eea685b6 DA |
2080 | outer.code = stream_getc(peer->curr); |
2081 | outer.subcode = stream_getc(peer->curr); | |
2082 | outer.length = size - 2; | |
2083 | outer.data = NULL; | |
2084 | outer.raw_data = NULL; | |
2085 | if (outer.length) { | |
2086 | outer.raw_data = XMALLOC(MTYPE_BGP_NOTIFICATION, outer.length); | |
2087 | memcpy(outer.raw_data, stream_pnt(peer->curr), outer.length); | |
2088 | } | |
2089 | ||
1ae314be DA |
2090 | hard_reset = |
2091 | bgp_notify_received_hard_reset(peer, outer.code, outer.subcode); | |
eea685b6 DA |
2092 | if (hard_reset && outer.length) { |
2093 | inner = bgp_notify_decapsulate_hard_reset(&outer); | |
2094 | peer->notify.hard_reset = true; | |
2095 | } else { | |
2096 | inner = outer; | |
2097 | } | |
d62a17ae | 2098 | |
2099 | /* Preserv notify code and sub code. */ | |
eea685b6 DA |
2100 | peer->notify.code = inner.code; |
2101 | peer->notify.subcode = inner.subcode; | |
d62a17ae | 2102 | /* For further diagnostic record returned Data. */ |
eea685b6 DA |
2103 | if (inner.length) { |
2104 | peer->notify.length = inner.length; | |
2105 | peer->notify.data = | |
2106 | XMALLOC(MTYPE_BGP_NOTIFICATION, inner.length); | |
2107 | memcpy(peer->notify.data, inner.raw_data, inner.length); | |
d62a17ae | 2108 | } |
2109 | ||
2110 | /* For debug */ | |
2111 | { | |
2112 | int i; | |
2113 | int first = 0; | |
2114 | char c[4]; | |
2115 | ||
eea685b6 DA |
2116 | if (inner.length) { |
2117 | inner.data = XMALLOC(MTYPE_BGP_NOTIFICATION, | |
2118 | inner.length * 3); | |
2119 | for (i = 0; i < inner.length; i++) | |
d62a17ae | 2120 | if (first) { |
552d6491 | 2121 | snprintf(c, sizeof(c), " %02x", |
424ab01d | 2122 | stream_getc(peer->curr)); |
f009ff26 | 2123 | |
eea685b6 DA |
2124 | strlcat(inner.data, c, |
2125 | inner.length * 3); | |
f009ff26 | 2126 | |
d62a17ae | 2127 | } else { |
2128 | first = 1; | |
552d6491 QY |
2129 | snprintf(c, sizeof(c), "%02x", |
2130 | stream_getc(peer->curr)); | |
f009ff26 | 2131 | |
eea685b6 DA |
2132 | strlcpy(inner.data, c, |
2133 | inner.length * 3); | |
d62a17ae | 2134 | } |
d62a17ae | 2135 | } |
2136 | ||
eea685b6 | 2137 | bgp_notify_print(peer, &inner, "received", hard_reset); |
10d476d4 | 2138 | if (inner.length) { |
eea685b6 DA |
2139 | XFREE(MTYPE_BGP_NOTIFICATION, inner.data); |
2140 | inner.length = 0; | |
2141 | } | |
2142 | if (outer.length) { | |
2143 | XFREE(MTYPE_BGP_NOTIFICATION, outer.data); | |
10d476d4 | 2144 | XFREE(MTYPE_BGP_NOTIFICATION, outer.raw_data); |
c73d2363 DA |
2145 | |
2146 | /* If this is a Hard Reset notification, we MUST free | |
2147 | * the inner (encapsulated) notification too. | |
2148 | */ | |
2149 | if (hard_reset) | |
2150 | XFREE(MTYPE_BGP_NOTIFICATION, inner.raw_data); | |
eea685b6 | 2151 | outer.length = 0; |
d62a17ae | 2152 | } |
2153 | } | |
2154 | ||
2155 | /* peer count update */ | |
0112e9e0 | 2156 | atomic_fetch_add_explicit(&peer->notify_in, 1, memory_order_relaxed); |
d62a17ae | 2157 | |
2158 | peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED; | |
2159 | ||
2160 | /* We have to check for Notify with Unsupported Optional Parameter. | |
2161 | in that case we fallback to open without the capability option. | |
2162 | But this done in bgp_stop. We just mark it here to avoid changing | |
2163 | the fsm tables. */ | |
eea685b6 DA |
2164 | if (inner.code == BGP_NOTIFY_OPEN_ERR && |
2165 | inner.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM) | |
d62a17ae | 2166 | UNSET_FLAG(peer->sflags, PEER_STATUS_CAPABILITY_OPEN); |
2167 | ||
20170775 DA |
2168 | /* If Graceful-Restart N-bit (Notification) is exchanged, |
2169 | * and it's not a Hard Reset, let's retain the routes. | |
2170 | */ | |
2171 | if (bgp_has_graceful_restart_notification(peer) && !hard_reset && | |
2172 | CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) | |
2173 | SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT); | |
2174 | ||
5cce3f05 | 2175 | bgp_peer_gr_flags_update(peer); |
36235319 QY |
2176 | BGP_GR_ROUTER_DETECT_AND_SEND_CAPABILITY_TO_ZEBRA(peer->bgp, |
2177 | peer->bgp->peer); | |
5cce3f05 | 2178 | |
d8151687 | 2179 | return Receive_NOTIFICATION_message; |
718e3744 | 2180 | } |
2181 | ||
d8151687 QY |
2182 | /** |
2183 | * Process BGP ROUTEREFRESH message for peer. | |
2184 | * | |
2185 | * @param peer | |
2186 | * @param size size of the packet | |
2187 | * @return as in summary | |
2188 | */ | |
2189 | static int bgp_route_refresh_receive(struct peer *peer, bgp_size_t size) | |
718e3744 | 2190 | { |
d62a17ae | 2191 | iana_afi_t pkt_afi; |
2192 | afi_t afi; | |
5c525538 RW |
2193 | iana_safi_t pkt_safi; |
2194 | safi_t safi; | |
d62a17ae | 2195 | struct stream *s; |
2196 | struct peer_af *paf; | |
2197 | struct update_group *updgrp; | |
2198 | struct peer *updgrp_peer; | |
9af52ccf | 2199 | uint8_t subtype; |
e1a32ec1 | 2200 | bool force_update = false; |
9af52ccf DA |
2201 | bgp_size_t msg_length = |
2202 | size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE); | |
d62a17ae | 2203 | |
2204 | /* If peer does not have the capability, send notification. */ | |
2205 | if (!CHECK_FLAG(peer->cap, PEER_CAP_REFRESH_ADV)) { | |
e50f7cfd | 2206 | flog_err(EC_BGP_NO_CAP, |
1c50c1c0 QY |
2207 | "%s [Error] BGP route refresh is not enabled", |
2208 | peer->host); | |
d62a17ae | 2209 | bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR, |
2210 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
d8151687 | 2211 | return BGP_Stop; |
d62a17ae | 2212 | } |
2213 | ||
2214 | /* Status must be Established. */ | |
feb17238 | 2215 | if (!peer_established(peer)) { |
af4c2728 | 2216 | flog_err( |
e50f7cfd | 2217 | EC_BGP_INVALID_STATUS, |
d62a17ae | 2218 | "%s [Error] Route refresh packet received under status %s", |
2219 | peer->host, | |
2220 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
0e35025e | 2221 | bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, |
3893aeee | 2222 | bgp_fsm_error_subcode(peer->status)); |
d8151687 | 2223 | return BGP_Stop; |
d62a17ae | 2224 | } |
2225 | ||
424ab01d | 2226 | s = peer->curr; |
d62a17ae | 2227 | |
2228 | /* Parse packet. */ | |
2229 | pkt_afi = stream_getw(s); | |
9af52ccf | 2230 | subtype = stream_getc(s); |
d62a17ae | 2231 | pkt_safi = stream_getc(s); |
2232 | ||
d62a17ae | 2233 | /* Convert AFI, SAFI to internal values and check. */ |
2234 | if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, &safi)) { | |
2235 | zlog_info( | |
748a041f DS |
2236 | "%s REFRESH_REQ for unrecognized afi/safi: %s/%s - ignored", |
2237 | peer->host, iana_afi2str(pkt_afi), | |
2238 | iana_safi2str(pkt_safi)); | |
d8151687 | 2239 | return BGP_PACKET_NOOP; |
d62a17ae | 2240 | } |
2241 | ||
2242 | if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) { | |
d7c0a89a QY |
2243 | uint8_t *end; |
2244 | uint8_t when_to_refresh; | |
2245 | uint8_t orf_type; | |
2246 | uint16_t orf_len; | |
d62a17ae | 2247 | |
9af52ccf DA |
2248 | if (subtype) { |
2249 | /* If the length, excluding the fixed-size message | |
2250 | * header, of the received ROUTE-REFRESH message with | |
2251 | * Message Subtype 1 and 2 is not 4, then the BGP | |
2252 | * speaker MUST send a NOTIFICATION message with the | |
2253 | * Error Code of "ROUTE-REFRESH Message Error" and the | |
2254 | * subcode of "Invalid Message Length". | |
2255 | */ | |
2256 | if (msg_length != 4) { | |
2257 | zlog_err( | |
2258 | "%s Enhanced Route Refresh message length error", | |
2259 | peer->host); | |
2260 | bgp_notify_send( | |
2261 | peer, BGP_NOTIFY_ROUTE_REFRESH_ERR, | |
2262 | BGP_NOTIFY_ROUTE_REFRESH_INVALID_MSG_LEN); | |
2263 | } | |
2264 | ||
2265 | /* When the BGP speaker receives a ROUTE-REFRESH message | |
2266 | * with a "Message Subtype" field other than 0, 1, or 2, | |
2267 | * it MUST ignore the received ROUTE-REFRESH message. | |
2268 | */ | |
2269 | if (subtype > 2) | |
2270 | zlog_err( | |
2271 | "%s Enhanced Route Refresh invalid subtype", | |
2272 | peer->host); | |
2273 | } | |
2274 | ||
2275 | if (msg_length < 5) { | |
d62a17ae | 2276 | zlog_info("%s ORF route refresh length error", |
2277 | peer->host); | |
0e35025e DA |
2278 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
2279 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 2280 | return BGP_Stop; |
718e3744 | 2281 | } |
2282 | ||
d62a17ae | 2283 | when_to_refresh = stream_getc(s); |
2284 | end = stream_pnt(s) + (size - 5); | |
2285 | ||
2286 | while ((stream_pnt(s) + 2) < end) { | |
2287 | orf_type = stream_getc(s); | |
2288 | orf_len = stream_getw(s); | |
2289 | ||
2290 | /* orf_len in bounds? */ | |
2291 | if ((stream_pnt(s) + orf_len) > end) | |
2292 | break; /* XXX: Notify instead?? */ | |
2293 | if (orf_type == ORF_TYPE_PREFIX | |
2294 | || orf_type == ORF_TYPE_PREFIX_OLD) { | |
2295 | uint8_t *p_pnt = stream_pnt(s); | |
2296 | uint8_t *p_end = stream_pnt(s) + orf_len; | |
2297 | struct orf_prefix orfp; | |
d7c0a89a QY |
2298 | uint8_t common = 0; |
2299 | uint32_t seq; | |
d62a17ae | 2300 | int psize; |
2301 | char name[BUFSIZ]; | |
2302 | int ret = CMD_SUCCESS; | |
2303 | ||
2304 | if (bgp_debug_neighbor_events(peer)) { | |
2305 | zlog_debug( | |
f70c91dc DA |
2306 | "%pBP rcvd Prefixlist ORF(%d) length %d", |
2307 | peer, orf_type, orf_len); | |
d62a17ae | 2308 | } |
2309 | ||
f1aa4929 DA |
2310 | /* ORF prefix-list name */ |
2311 | snprintf(name, sizeof(name), "%s.%d.%d", | |
2312 | peer->host, afi, safi); | |
2313 | ||
d62a17ae | 2314 | /* we're going to read at least 1 byte of common |
2315 | * ORF header, | |
2316 | * and 7 bytes of ORF Address-filter entry from | |
2317 | * the stream | |
2318 | */ | |
f1aa4929 DA |
2319 | if (*p_pnt & ORF_COMMON_PART_REMOVE_ALL) { |
2320 | if (bgp_debug_neighbor_events(peer)) | |
2321 | zlog_debug( | |
2322 | "%pBP rcvd Remove-All pfxlist ORF request", | |
2323 | peer); | |
2324 | prefix_bgp_orf_remove_all(afi, name); | |
d62a17ae | 2325 | break; |
f1aa4929 | 2326 | } |
d62a17ae | 2327 | |
f1aa4929 DA |
2328 | if (orf_len < 7) |
2329 | break; | |
d62a17ae | 2330 | |
2331 | while (p_pnt < p_end) { | |
2332 | /* If the ORF entry is malformed, want | |
2333 | * to read as much of it | |
2334 | * as possible without going beyond the | |
2335 | * bounds of the entry, | |
2336 | * to maximise debug information. | |
2337 | */ | |
2338 | int ok; | |
6006b807 | 2339 | memset(&orfp, 0, sizeof(orfp)); |
d62a17ae | 2340 | common = *p_pnt++; |
2341 | /* after ++: p_pnt <= p_end */ | |
d7c0a89a QY |
2342 | ok = ((uint32_t)(p_end - p_pnt) |
2343 | >= sizeof(uint32_t)); | |
d62a17ae | 2344 | if (ok) { |
2345 | memcpy(&seq, p_pnt, | |
d7c0a89a QY |
2346 | sizeof(uint32_t)); |
2347 | p_pnt += sizeof(uint32_t); | |
d62a17ae | 2348 | orfp.seq = ntohl(seq); |
2349 | } else | |
2350 | p_pnt = p_end; | |
2351 | ||
5ca840a3 | 2352 | /* val checked in prefix_bgp_orf_set */ |
1bb379bf | 2353 | if (p_pnt < p_end) |
5ca840a3 DS |
2354 | orfp.ge = *p_pnt++; |
2355 | ||
2356 | /* val checked in prefix_bgp_orf_set */ | |
1bb379bf | 2357 | if (p_pnt < p_end) |
5ca840a3 DS |
2358 | orfp.le = *p_pnt++; |
2359 | ||
d62a17ae | 2360 | if ((ok = (p_pnt < p_end))) |
2361 | orfp.p.prefixlen = *p_pnt++; | |
5ca840a3 DS |
2362 | |
2363 | /* afi checked already */ | |
2364 | orfp.p.family = afi2family(afi); | |
2365 | ||
2366 | /* 0 if not ok */ | |
2367 | psize = PSIZE(orfp.p.prefixlen); | |
2368 | /* valid for family ? */ | |
2369 | if (psize > prefix_blen(&orfp.p)) { | |
d62a17ae | 2370 | ok = 0; |
2371 | psize = prefix_blen(&orfp.p); | |
2372 | } | |
5ca840a3 DS |
2373 | /* valid for packet ? */ |
2374 | if (psize > (p_end - p_pnt)) { | |
d62a17ae | 2375 | ok = 0; |
2376 | psize = p_end - p_pnt; | |
2377 | } | |
2378 | ||
2379 | if (psize > 0) | |
2380 | memcpy(&orfp.p.u.prefix, p_pnt, | |
2381 | psize); | |
2382 | p_pnt += psize; | |
2383 | ||
2384 | if (bgp_debug_neighbor_events(peer)) { | |
2385 | char buf[INET6_BUFSIZ]; | |
2386 | ||
2387 | zlog_debug( | |
f70c91dc DA |
2388 | "%pBP rcvd %s %s seq %u %s/%d ge %d le %d%s", |
2389 | peer, | |
d62a17ae | 2390 | (common & ORF_COMMON_PART_REMOVE |
2391 | ? "Remove" | |
2392 | : "Add"), | |
2393 | (common & ORF_COMMON_PART_DENY | |
2394 | ? "deny" | |
2395 | : "permit"), | |
2396 | orfp.seq, | |
2397 | inet_ntop( | |
2398 | orfp.p.family, | |
2399 | &orfp.p.u.prefix, | |
2400 | buf, | |
2401 | INET6_BUFSIZ), | |
2402 | orfp.p.prefixlen, | |
2403 | orfp.ge, orfp.le, | |
2404 | ok ? "" : " MALFORMED"); | |
2405 | } | |
2406 | ||
2407 | if (ok) | |
2408 | ret = prefix_bgp_orf_set( | |
2409 | name, afi, &orfp, | |
2410 | (common & ORF_COMMON_PART_DENY | |
2411 | ? 0 | |
2412 | : 1), | |
2413 | (common & ORF_COMMON_PART_REMOVE | |
2414 | ? 0 | |
2415 | : 1)); | |
2416 | ||
2417 | if (!ok || (ok && ret != CMD_SUCCESS)) { | |
2418 | zlog_info( | |
f70c91dc DA |
2419 | "%pBP Received misformatted prefixlist ORF. Remove All pfxlist", |
2420 | peer); | |
d62a17ae | 2421 | prefix_bgp_orf_remove_all(afi, |
2422 | name); | |
2423 | break; | |
2424 | } | |
2425 | } | |
2426 | ||
2427 | peer->orf_plist[afi][safi] = | |
2428 | prefix_bgp_orf_lookup(afi, name); | |
2429 | } | |
2430 | stream_forward_getp(s, orf_len); | |
718e3744 | 2431 | } |
d62a17ae | 2432 | if (bgp_debug_neighbor_events(peer)) |
f70c91dc | 2433 | zlog_debug("%pBP rcvd Refresh %s ORF request", peer, |
d62a17ae | 2434 | when_to_refresh == REFRESH_DEFER |
2435 | ? "Defer" | |
2436 | : "Immediate"); | |
2437 | if (when_to_refresh == REFRESH_DEFER) | |
d8151687 | 2438 | return BGP_PACKET_NOOP; |
d62a17ae | 2439 | } |
40d2700d | 2440 | |
d62a17ae | 2441 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ |
2442 | if (CHECK_FLAG(peer->af_sflags[afi][safi], | |
2443 | PEER_STATUS_ORF_WAIT_REFRESH)) | |
2444 | UNSET_FLAG(peer->af_sflags[afi][safi], | |
2445 | PEER_STATUS_ORF_WAIT_REFRESH); | |
2446 | ||
2447 | paf = peer_af_find(peer, afi, safi); | |
2448 | if (paf && paf->subgroup) { | |
2449 | if (peer->orf_plist[afi][safi]) { | |
2450 | updgrp = PAF_UPDGRP(paf); | |
2451 | updgrp_peer = UPDGRP_PEER(updgrp); | |
2452 | updgrp_peer->orf_plist[afi][safi] = | |
2453 | peer->orf_plist[afi][safi]; | |
2454 | } | |
2455 | ||
2adac256 DA |
2456 | /* Avoid supressing duplicate routes later |
2457 | * when processing in subgroup_announce_table(). | |
2458 | */ | |
e1a32ec1 | 2459 | force_update = true; |
2adac256 | 2460 | |
d62a17ae | 2461 | /* If the peer is configured for default-originate clear the |
2462 | * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will | |
2463 | * re-advertise the | |
2464 | * default | |
2465 | */ | |
2466 | if (CHECK_FLAG(paf->subgroup->sflags, | |
2467 | SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
2468 | UNSET_FLAG(paf->subgroup->sflags, | |
2469 | SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
718e3744 | 2470 | } |
d62a17ae | 2471 | |
9af52ccf DA |
2472 | if (subtype == BGP_ROUTE_REFRESH_BORR) { |
2473 | /* A BGP speaker that has received the Graceful Restart | |
2474 | * Capability from its neighbor MUST ignore any BoRRs for | |
2475 | * an <AFI, SAFI> from the neighbor before the speaker | |
2476 | * receives the EoR for the given <AFI, SAFI> from the | |
2477 | * neighbor. | |
2478 | */ | |
2479 | if (CHECK_FLAG(peer->cap, PEER_CAP_RESTART_RCV) | |
2480 | && !CHECK_FLAG(peer->af_sflags[afi][safi], | |
2481 | PEER_STATUS_EOR_RECEIVED)) { | |
2482 | if (bgp_debug_neighbor_events(peer)) | |
2483 | zlog_debug( | |
f70c91dc DA |
2484 | "%pBP rcvd route-refresh (BoRR) for %s/%s before EoR", |
2485 | peer, afi2str(afi), safi2str(safi)); | |
9af52ccf DA |
2486 | return BGP_PACKET_NOOP; |
2487 | } | |
2488 | ||
2489 | if (peer->t_refresh_stalepath) { | |
2490 | if (bgp_debug_neighbor_events(peer)) | |
2491 | zlog_debug( | |
f70c91dc DA |
2492 | "%pBP rcvd route-refresh (BoRR) for %s/%s, whereas BoRR already received", |
2493 | peer, afi2str(afi), safi2str(safi)); | |
9af52ccf DA |
2494 | return BGP_PACKET_NOOP; |
2495 | } | |
2496 | ||
2497 | SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_BORR_RECEIVED); | |
2498 | UNSET_FLAG(peer->af_sflags[afi][safi], | |
2499 | PEER_STATUS_EORR_RECEIVED); | |
2500 | ||
2501 | /* When a BGP speaker receives a BoRR message from | |
2502 | * a peer, it MUST mark all the routes with the given | |
2503 | * Address Family Identifier and Subsequent Address | |
2504 | * Family Identifier, <AFI, SAFI> [RFC2918], from | |
2505 | * that peer as stale. | |
2506 | */ | |
2507 | if (peer_active_nego(peer)) { | |
2508 | SET_FLAG(peer->af_sflags[afi][safi], | |
2509 | PEER_STATUS_ENHANCED_REFRESH); | |
2510 | bgp_set_stale_route(peer, afi, safi); | |
2511 | } | |
2512 | ||
feb17238 | 2513 | if (peer_established(peer)) |
9af52ccf DA |
2514 | thread_add_timer(bm->master, |
2515 | bgp_refresh_stalepath_timer_expire, | |
2516 | paf, peer->bgp->stalepath_time, | |
2517 | &peer->t_refresh_stalepath); | |
2518 | ||
2519 | if (bgp_debug_neighbor_events(peer)) | |
2520 | zlog_debug( | |
f70c91dc DA |
2521 | "%pBP rcvd route-refresh (BoRR) for %s/%s, triggering timer for %u seconds", |
2522 | peer, afi2str(afi), safi2str(safi), | |
9af52ccf DA |
2523 | peer->bgp->stalepath_time); |
2524 | } else if (subtype == BGP_ROUTE_REFRESH_EORR) { | |
2525 | if (!peer->t_refresh_stalepath) { | |
2526 | zlog_err( | |
f70c91dc DA |
2527 | "%pBP rcvd route-refresh (EoRR) for %s/%s, whereas no BoRR received", |
2528 | peer, afi2str(afi), safi2str(safi)); | |
9af52ccf DA |
2529 | return BGP_PACKET_NOOP; |
2530 | } | |
2531 | ||
fa5806c3 | 2532 | THREAD_OFF(peer->t_refresh_stalepath); |
9af52ccf DA |
2533 | |
2534 | SET_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EORR_RECEIVED); | |
2535 | UNSET_FLAG(peer->af_sflags[afi][safi], | |
2536 | PEER_STATUS_BORR_RECEIVED); | |
2537 | ||
2538 | if (bgp_debug_neighbor_events(peer)) | |
2539 | zlog_debug( | |
f70c91dc DA |
2540 | "%pBP rcvd route-refresh (EoRR) for %s/%s, stopping BoRR timer", |
2541 | peer, afi2str(afi), safi2str(safi)); | |
9af52ccf DA |
2542 | |
2543 | if (peer->nsf[afi][safi]) | |
2544 | bgp_clear_stale_route(peer, afi, safi); | |
2545 | } else { | |
bcbeb3f9 | 2546 | if (bgp_debug_neighbor_events(peer)) |
a7d91a8c | 2547 | zlog_debug( |
f70c91dc DA |
2548 | "%pBP rcvd route-refresh (REQUEST) for %s/%s", |
2549 | peer, afi2str(afi), safi2str(safi)); | |
bcbeb3f9 | 2550 | |
9af52ccf DA |
2551 | /* In response to a "normal route refresh request" from the |
2552 | * peer, the speaker MUST send a BoRR message. | |
2553 | */ | |
2554 | if (CHECK_FLAG(peer->cap, PEER_CAP_ENHANCED_RR_RCV)) { | |
2555 | /* For a BGP speaker that supports the BGP Graceful | |
2556 | * Restart, it MUST NOT send a BoRR for an <AFI, SAFI> | |
2557 | * to a neighbor before it sends the EoR for the | |
2558 | * <AFI, SAFI> to the neighbor. | |
2559 | */ | |
2560 | if (!CHECK_FLAG(peer->af_sflags[afi][safi], | |
2561 | PEER_STATUS_EOR_SEND)) { | |
2562 | if (bgp_debug_neighbor_events(peer)) | |
2563 | zlog_debug( | |
f70c91dc DA |
2564 | "%pBP rcvd route-refresh (REQUEST) for %s/%s before EoR", |
2565 | peer, afi2str(afi), | |
2566 | safi2str(safi)); | |
9af52ccf DA |
2567 | return BGP_PACKET_NOOP; |
2568 | } | |
2569 | ||
2570 | bgp_route_refresh_send(peer, afi, safi, 0, 0, 0, | |
2571 | BGP_ROUTE_REFRESH_BORR); | |
2572 | ||
2573 | if (bgp_debug_neighbor_events(peer)) | |
2574 | zlog_debug( | |
f70c91dc DA |
2575 | "%pBP sending route-refresh (BoRR) for %s/%s", |
2576 | peer, afi2str(afi), safi2str(safi)); | |
9af52ccf DA |
2577 | |
2578 | /* Set flag Ready-To-Send to know when we can send EoRR | |
2579 | * message. | |
2580 | */ | |
2581 | SET_FLAG(peer->af_sflags[afi][safi], | |
2582 | PEER_STATUS_BORR_SEND); | |
2583 | UNSET_FLAG(peer->af_sflags[afi][safi], | |
2584 | PEER_STATUS_EORR_SEND); | |
2585 | } | |
2586 | } | |
2587 | ||
d62a17ae | 2588 | /* Perform route refreshment to the peer */ |
e1a32ec1 | 2589 | bgp_announce_route(peer, afi, safi, force_update); |
d8151687 QY |
2590 | |
2591 | /* No FSM action necessary */ | |
2592 | return BGP_PACKET_NOOP; | |
718e3744 | 2593 | } |
2594 | ||
d8151687 QY |
2595 | /** |
2596 | * Parse BGP CAPABILITY message for peer. | |
2597 | * | |
2598 | * @param peer | |
2599 | * @param size size of the packet | |
2600 | * @return as in summary | |
2601 | */ | |
d7c0a89a | 2602 | static int bgp_capability_msg_parse(struct peer *peer, uint8_t *pnt, |
d62a17ae | 2603 | bgp_size_t length) |
718e3744 | 2604 | { |
d7c0a89a | 2605 | uint8_t *end; |
d62a17ae | 2606 | struct capability_mp_data mpc; |
2607 | struct capability_header *hdr; | |
d7c0a89a | 2608 | uint8_t action; |
d62a17ae | 2609 | iana_afi_t pkt_afi; |
2610 | afi_t afi; | |
5c525538 RW |
2611 | iana_safi_t pkt_safi; |
2612 | safi_t safi; | |
d62a17ae | 2613 | |
2614 | end = pnt + length; | |
2615 | ||
2616 | while (pnt < end) { | |
2617 | /* We need at least action, capability code and capability | |
2618 | * length. */ | |
2619 | if (pnt + 3 > end) { | |
2620 | zlog_info("%s Capability length error", peer->host); | |
0e35025e DA |
2621 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
2622 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 2623 | return BGP_Stop; |
d62a17ae | 2624 | } |
2625 | action = *pnt; | |
2626 | hdr = (struct capability_header *)(pnt + 1); | |
2627 | ||
2628 | /* Action value check. */ | |
2629 | if (action != CAPABILITY_ACTION_SET | |
2630 | && action != CAPABILITY_ACTION_UNSET) { | |
2631 | zlog_info("%s Capability Action Value error %d", | |
2632 | peer->host, action); | |
0e35025e DA |
2633 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
2634 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 2635 | return BGP_Stop; |
d62a17ae | 2636 | } |
2637 | ||
2638 | if (bgp_debug_neighbor_events(peer)) | |
2639 | zlog_debug( | |
2640 | "%s CAPABILITY has action: %d, code: %u, length %u", | |
2641 | peer->host, action, hdr->code, hdr->length); | |
2642 | ||
ff6db102 DS |
2643 | if (hdr->length < sizeof(struct capability_mp_data)) { |
2644 | zlog_info( | |
2645 | "%pBP Capability structure is not properly filled out, expected at least %zu bytes but header length specified is %d", | |
2646 | peer, sizeof(struct capability_mp_data), | |
2647 | hdr->length); | |
2648 | return BGP_Stop; | |
2649 | } | |
2650 | ||
d62a17ae | 2651 | /* Capability length check. */ |
2652 | if ((pnt + hdr->length + 3) > end) { | |
2653 | zlog_info("%s Capability length error", peer->host); | |
0e35025e DA |
2654 | bgp_notify_send(peer, BGP_NOTIFY_CEASE, |
2655 | BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
d8151687 | 2656 | return BGP_Stop; |
d62a17ae | 2657 | } |
2658 | ||
2659 | /* Fetch structure to the byte stream. */ | |
2660 | memcpy(&mpc, pnt + 3, sizeof(struct capability_mp_data)); | |
d2b6417b | 2661 | pnt += hdr->length + 3; |
d62a17ae | 2662 | |
2663 | /* We know MP Capability Code. */ | |
2664 | if (hdr->code == CAPABILITY_CODE_MP) { | |
2665 | pkt_afi = ntohs(mpc.afi); | |
2666 | pkt_safi = mpc.safi; | |
2667 | ||
2668 | /* Ignore capability when override-capability is set. */ | |
2669 | if (CHECK_FLAG(peer->flags, | |
2670 | PEER_FLAG_OVERRIDE_CAPABILITY)) | |
2671 | continue; | |
2672 | ||
2673 | /* Convert AFI, SAFI to internal values. */ | |
2674 | if (bgp_map_afi_safi_iana2int(pkt_afi, pkt_safi, &afi, | |
2675 | &safi)) { | |
2676 | if (bgp_debug_neighbor_events(peer)) | |
2677 | zlog_debug( | |
3efd0893 | 2678 | "%s Dynamic Capability MP_EXT afi/safi invalid (%s/%s)", |
748a041f DS |
2679 | peer->host, |
2680 | iana_afi2str(pkt_afi), | |
2681 | iana_safi2str(pkt_safi)); | |
d62a17ae | 2682 | continue; |
2683 | } | |
2684 | ||
2685 | /* Address family check. */ | |
2686 | if (bgp_debug_neighbor_events(peer)) | |
2687 | zlog_debug( | |
c386cdd8 | 2688 | "%s CAPABILITY has %s MP_EXT CAP for afi/safi: %s/%s", |
d62a17ae | 2689 | peer->host, |
2690 | action == CAPABILITY_ACTION_SET | |
2691 | ? "Advertising" | |
2692 | : "Removing", | |
c386cdd8 DA |
2693 | iana_afi2str(pkt_afi), |
2694 | iana_safi2str(pkt_safi)); | |
d62a17ae | 2695 | |
2696 | if (action == CAPABILITY_ACTION_SET) { | |
2697 | peer->afc_recv[afi][safi] = 1; | |
2698 | if (peer->afc[afi][safi]) { | |
2699 | peer->afc_nego[afi][safi] = 1; | |
e1a32ec1 DS |
2700 | bgp_announce_route(peer, afi, safi, |
2701 | false); | |
d62a17ae | 2702 | } |
2703 | } else { | |
2704 | peer->afc_recv[afi][safi] = 0; | |
2705 | peer->afc_nego[afi][safi] = 0; | |
2706 | ||
2707 | if (peer_active_nego(peer)) | |
2708 | bgp_clear_route(peer, afi, safi); | |
2709 | else | |
d8151687 | 2710 | return BGP_Stop; |
d62a17ae | 2711 | } |
2712 | } else { | |
ade6974d | 2713 | flog_warn( |
e50f7cfd | 2714 | EC_BGP_UNRECOGNIZED_CAPABILITY, |
ade6974d QY |
2715 | "%s unrecognized capability code: %d - ignored", |
2716 | peer->host, hdr->code); | |
d62a17ae | 2717 | } |
d62a17ae | 2718 | } |
d8151687 QY |
2719 | |
2720 | /* No FSM action necessary */ | |
2721 | return BGP_PACKET_NOOP; | |
718e3744 | 2722 | } |
2723 | ||
d8151687 QY |
2724 | /** |
2725 | * Parse BGP CAPABILITY message for peer. | |
01b7ce2d | 2726 | * |
d8151687 QY |
2727 | * Exported for unit testing. |
2728 | * | |
2729 | * @param peer | |
2730 | * @param size size of the packet | |
2731 | * @return as in summary | |
01b7ce2d | 2732 | */ |
d62a17ae | 2733 | int bgp_capability_receive(struct peer *peer, bgp_size_t size) |
718e3744 | 2734 | { |
d7c0a89a | 2735 | uint8_t *pnt; |
d62a17ae | 2736 | |
2737 | /* Fetch pointer. */ | |
424ab01d | 2738 | pnt = stream_pnt(peer->curr); |
d62a17ae | 2739 | |
2740 | if (bgp_debug_neighbor_events(peer)) | |
2741 | zlog_debug("%s rcv CAPABILITY", peer->host); | |
2742 | ||
2743 | /* If peer does not have the capability, send notification. */ | |
2744 | if (!CHECK_FLAG(peer->cap, PEER_CAP_DYNAMIC_ADV)) { | |
e50f7cfd | 2745 | flog_err(EC_BGP_NO_CAP, |
1c50c1c0 QY |
2746 | "%s [Error] BGP dynamic capability is not enabled", |
2747 | peer->host); | |
d62a17ae | 2748 | bgp_notify_send(peer, BGP_NOTIFY_HEADER_ERR, |
2749 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
d8151687 | 2750 | return BGP_Stop; |
d62a17ae | 2751 | } |
2752 | ||
2753 | /* Status must be Established. */ | |
feb17238 | 2754 | if (!peer_established(peer)) { |
af4c2728 | 2755 | flog_err( |
e50f7cfd | 2756 | EC_BGP_NO_CAP, |
d62a17ae | 2757 | "%s [Error] Dynamic capability packet received under status %s", |
2758 | peer->host, | |
2759 | lookup_msg(bgp_status_msg, peer->status, NULL)); | |
0e35025e | 2760 | bgp_notify_send(peer, BGP_NOTIFY_FSM_ERR, |
3893aeee | 2761 | bgp_fsm_error_subcode(peer->status)); |
d8151687 | 2762 | return BGP_Stop; |
d62a17ae | 2763 | } |
2764 | ||
2765 | /* Parse packet. */ | |
2766 | return bgp_capability_msg_parse(peer, pnt, size); | |
718e3744 | 2767 | } |
6b0655a2 | 2768 | |
d8151687 QY |
2769 | /** |
2770 | * Processes a peer's input buffer. | |
2771 | * | |
2772 | * This function sidesteps the event loop and directly calls bgp_event_update() | |
2773 | * after processing each BGP message. This is necessary to ensure proper | |
2774 | * ordering of FSM events and unifies the behavior that was present previously, | |
2775 | * whereby some of the packet handling functions would update the FSM and some | |
2776 | * would not, making event flow difficult to understand. Please think twice | |
2777 | * before hacking this. | |
2778 | * | |
2779 | * Thread type: THREAD_EVENT | |
2780 | * @param thread | |
2781 | * @return 0 | |
2782 | */ | |
cc9f21da | 2783 | void bgp_process_packet(struct thread *thread) |
718e3744 | 2784 | { |
424ab01d | 2785 | /* Yes first of all get peer pointer. */ |
d8151687 QY |
2786 | struct peer *peer; // peer |
2787 | uint32_t rpkt_quanta_old; // how many packets to read | |
2788 | int fsm_update_result; // return code of bgp_event_update() | |
2789 | int mprc; // message processing return code | |
555e09d4 | 2790 | |
424ab01d | 2791 | peer = THREAD_ARG(thread); |
555e09d4 QY |
2792 | rpkt_quanta_old = atomic_load_explicit(&peer->bgp->rpkt_quanta, |
2793 | memory_order_relaxed); | |
e3c7270d | 2794 | fsm_update_result = 0; |
555e09d4 | 2795 | |
424ab01d | 2796 | /* Guard against scheduled events that occur after peer deletion. */ |
9eb217ff | 2797 | if (peer->status == Deleted || peer->status == Clearing) |
cc9f21da | 2798 | return; |
718e3744 | 2799 | |
555e09d4 | 2800 | unsigned int processed = 0; |
d62a17ae | 2801 | |
555e09d4 | 2802 | while (processed < rpkt_quanta_old) { |
d7c0a89a | 2803 | uint8_t type = 0; |
9eb217ff QY |
2804 | bgp_size_t size; |
2805 | char notify_data_length[2]; | |
d62a17ae | 2806 | |
cb1991af | 2807 | frr_with_mutex (&peer->io_mtx) { |
9eb217ff QY |
2808 | peer->curr = stream_fifo_pop(peer->ibuf); |
2809 | } | |
d62a17ae | 2810 | |
9eb217ff | 2811 | if (peer->curr == NULL) // no packets to process, hmm... |
cc9f21da | 2812 | return; |
d62a17ae | 2813 | |
9eb217ff QY |
2814 | /* skip the marker and copy the packet length */ |
2815 | stream_forward_getp(peer->curr, BGP_MARKER_SIZE); | |
2816 | memcpy(notify_data_length, stream_pnt(peer->curr), 2); | |
2817 | ||
2818 | /* read in the packet length and type */ | |
2819 | size = stream_getw(peer->curr); | |
2820 | type = stream_getc(peer->curr); | |
2821 | ||
584470fb | 2822 | hook_call(bgp_packet_dump, peer, type, size, peer->curr); |
9eb217ff QY |
2823 | |
2824 | /* adjust size to exclude the marker + length + type */ | |
2825 | size -= BGP_HEADER_SIZE; | |
2826 | ||
2827 | /* Read rest of the packet and call each sort of packet routine | |
2828 | */ | |
2829 | switch (type) { | |
2830 | case BGP_MSG_OPEN: | |
c7bb4f00 | 2831 | frrtrace(2, frr_bgp, open_process, peer, size); |
0112e9e0 QY |
2832 | atomic_fetch_add_explicit(&peer->open_in, 1, |
2833 | memory_order_relaxed); | |
d8151687 QY |
2834 | mprc = bgp_open_receive(peer, size); |
2835 | if (mprc == BGP_Stop) | |
af4c2728 | 2836 | flog_err( |
e50f7cfd | 2837 | EC_BGP_PKT_OPEN, |
d8151687 | 2838 | "%s: BGP OPEN receipt failed for peer: %s", |
0767b4f3 | 2839 | __func__, peer->host); |
9eb217ff QY |
2840 | break; |
2841 | case BGP_MSG_UPDATE: | |
c7bb4f00 | 2842 | frrtrace(2, frr_bgp, update_process, peer, size); |
0112e9e0 QY |
2843 | atomic_fetch_add_explicit(&peer->update_in, 1, |
2844 | memory_order_relaxed); | |
9eb217ff | 2845 | peer->readtime = monotime(NULL); |
d8151687 QY |
2846 | mprc = bgp_update_receive(peer, size); |
2847 | if (mprc == BGP_Stop) | |
af4c2728 | 2848 | flog_err( |
e50f7cfd | 2849 | EC_BGP_UPDATE_RCV, |
d8151687 | 2850 | "%s: BGP UPDATE receipt failed for peer: %s", |
0767b4f3 | 2851 | __func__, peer->host); |
9eb217ff QY |
2852 | break; |
2853 | case BGP_MSG_NOTIFY: | |
c7bb4f00 | 2854 | frrtrace(2, frr_bgp, notification_process, peer, size); |
0112e9e0 QY |
2855 | atomic_fetch_add_explicit(&peer->notify_in, 1, |
2856 | memory_order_relaxed); | |
d8151687 QY |
2857 | mprc = bgp_notify_receive(peer, size); |
2858 | if (mprc == BGP_Stop) | |
af4c2728 | 2859 | flog_err( |
e50f7cfd | 2860 | EC_BGP_NOTIFY_RCV, |
d8151687 | 2861 | "%s: BGP NOTIFY receipt failed for peer: %s", |
0767b4f3 | 2862 | __func__, peer->host); |
9eb217ff QY |
2863 | break; |
2864 | case BGP_MSG_KEEPALIVE: | |
c7bb4f00 | 2865 | frrtrace(2, frr_bgp, keepalive_process, peer, size); |
9eb217ff | 2866 | peer->readtime = monotime(NULL); |
0112e9e0 QY |
2867 | atomic_fetch_add_explicit(&peer->keepalive_in, 1, |
2868 | memory_order_relaxed); | |
d8151687 QY |
2869 | mprc = bgp_keepalive_receive(peer, size); |
2870 | if (mprc == BGP_Stop) | |
af4c2728 | 2871 | flog_err( |
e50f7cfd | 2872 | EC_BGP_KEEP_RCV, |
d8151687 | 2873 | "%s: BGP KEEPALIVE receipt failed for peer: %s", |
0767b4f3 | 2874 | __func__, peer->host); |
9eb217ff QY |
2875 | break; |
2876 | case BGP_MSG_ROUTE_REFRESH_NEW: | |
2877 | case BGP_MSG_ROUTE_REFRESH_OLD: | |
c7bb4f00 | 2878 | frrtrace(2, frr_bgp, refresh_process, peer, size); |
0112e9e0 QY |
2879 | atomic_fetch_add_explicit(&peer->refresh_in, 1, |
2880 | memory_order_relaxed); | |
d8151687 QY |
2881 | mprc = bgp_route_refresh_receive(peer, size); |
2882 | if (mprc == BGP_Stop) | |
af4c2728 | 2883 | flog_err( |
e50f7cfd | 2884 | EC_BGP_RFSH_RCV, |
d8151687 | 2885 | "%s: BGP ROUTEREFRESH receipt failed for peer: %s", |
0767b4f3 | 2886 | __func__, peer->host); |
9eb217ff QY |
2887 | break; |
2888 | case BGP_MSG_CAPABILITY: | |
c7bb4f00 | 2889 | frrtrace(2, frr_bgp, capability_process, peer, size); |
0112e9e0 QY |
2890 | atomic_fetch_add_explicit(&peer->dynamic_cap_in, 1, |
2891 | memory_order_relaxed); | |
d8151687 QY |
2892 | mprc = bgp_capability_receive(peer, size); |
2893 | if (mprc == BGP_Stop) | |
af4c2728 | 2894 | flog_err( |
e50f7cfd | 2895 | EC_BGP_CAP_RCV, |
d8151687 | 2896 | "%s: BGP CAPABILITY receipt failed for peer: %s", |
0767b4f3 | 2897 | __func__, peer->host); |
9eb217ff | 2898 | break; |
e3c7270d | 2899 | default: |
db878db0 QY |
2900 | /* Suppress uninitialized variable warning */ |
2901 | mprc = 0; | |
5041dc4f | 2902 | (void)mprc; |
becedef6 QY |
2903 | /* |
2904 | * The message type should have been sanitized before | |
2905 | * we ever got here. Receipt of a message with an | |
2906 | * invalid header at this point is indicative of a | |
2907 | * security issue. | |
2908 | */ | |
e3c7270d | 2909 | assert (!"Message of invalid type received during input processing"); |
9eb217ff QY |
2910 | } |
2911 | ||
d8151687 QY |
2912 | /* delete processed packet */ |
2913 | stream_free(peer->curr); | |
2914 | peer->curr = NULL; | |
2915 | processed++; | |
9eb217ff | 2916 | |
d8151687 QY |
2917 | /* Update FSM */ |
2918 | if (mprc != BGP_PACKET_NOOP) | |
2919 | fsm_update_result = bgp_event_update(peer, mprc); | |
e3c7270d QY |
2920 | else |
2921 | continue; | |
d8151687 | 2922 | |
becedef6 QY |
2923 | /* |
2924 | * If peer was deleted, do not process any more packets. This | |
2925 | * is usually due to executing BGP_Stop or a stub deletion. | |
2926 | */ | |
d8151687 QY |
2927 | if (fsm_update_result == FSM_PEER_TRANSFERRED |
2928 | || fsm_update_result == FSM_PEER_STOPPED) | |
2929 | break; | |
d62a17ae | 2930 | } |
2931 | ||
d8151687 QY |
2932 | if (fsm_update_result != FSM_PEER_TRANSFERRED |
2933 | && fsm_update_result != FSM_PEER_STOPPED) { | |
cb1991af | 2934 | frr_with_mutex (&peer->io_mtx) { |
becedef6 QY |
2935 | // more work to do, come back later |
2936 | if (peer->ibuf->count > 0) | |
e0d550df | 2937 | thread_add_event( |
4af76660 QY |
2938 | bm->master, bgp_process_packet, peer, 0, |
2939 | &peer->t_process_packet); | |
d8151687 | 2940 | } |
718e3744 | 2941 | } |
718e3744 | 2942 | } |
9e3b51a7 | 2943 | |
2944 | /* Send EOR when routes are processed by selection deferral timer */ | |
2945 | void bgp_send_delayed_eor(struct bgp *bgp) | |
2946 | { | |
2947 | struct peer *peer; | |
2948 | struct listnode *node, *nnode; | |
2949 | ||
2950 | /* EOR message sent in bgp_write_proceed_actions */ | |
2951 | for (ALL_LIST_ELEMENTS(bgp->peer, node, nnode, peer)) | |
2952 | bgp_write_proceed_actions(peer); | |
2953 | } | |
6af96fa3 MS |
2954 | |
2955 | /* | |
2956 | * Task callback to handle socket error encountered in the io pthread. We avoid | |
2957 | * having the io pthread try to enqueue fsm events or mess with the peer | |
2958 | * struct. | |
2959 | */ | |
cc9f21da | 2960 | void bgp_packet_process_error(struct thread *thread) |
6af96fa3 MS |
2961 | { |
2962 | struct peer *peer; | |
2963 | int code; | |
2964 | ||
2965 | peer = THREAD_ARG(thread); | |
2966 | code = THREAD_VAL(thread); | |
2967 | ||
2968 | if (bgp_debug_neighbor_events(peer)) | |
2969 | zlog_debug("%s [Event] BGP error %d on fd %d", | |
046bb347 | 2970 | peer->host, code, peer->fd); |
6af96fa3 MS |
2971 | |
2972 | /* Closed connection or error on the socket */ | |
feb17238 | 2973 | if (peer_established(peer)) { |
6af96fa3 MS |
2974 | if ((CHECK_FLAG(peer->flags, PEER_FLAG_GRACEFUL_RESTART) |
2975 | || CHECK_FLAG(peer->flags, | |
2976 | PEER_FLAG_GRACEFUL_RESTART_HELPER)) | |
2977 | && CHECK_FLAG(peer->sflags, PEER_STATUS_NSF_MODE)) { | |
2978 | peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION; | |
2979 | SET_FLAG(peer->sflags, PEER_STATUS_NSF_WAIT); | |
2980 | } else | |
2981 | peer->last_reset = PEER_DOWN_CLOSE_SESSION; | |
2982 | } | |
2983 | ||
2984 | bgp_event_update(peer, code); | |
6af96fa3 | 2985 | } |