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