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