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