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