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