]>
Commit | Line | Data |
---|---|---|
718e3744 | 1 | /* BGP packet management routine. |
896014f4 DL |
2 | * Copyright (C) 1999 Kunihiro Ishiguro |
3 | * | |
4 | * This file is part of GNU Zebra. | |
5 | * | |
6 | * GNU Zebra is free software; you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation; either version 2, or (at your option) any | |
9 | * later version. | |
10 | * | |
11 | * GNU Zebra is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program; see the file COPYING; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
718e3744 | 20 | |
21 | #include <zebra.h> | |
22 | ||
23 | #include "thread.h" | |
24 | #include "stream.h" | |
25 | #include "network.h" | |
26 | #include "prefix.h" | |
27 | #include "command.h" | |
28 | #include "log.h" | |
29 | #include "memory.h" | |
30 | #include "sockunion.h" /* for inet_ntop () */ | |
baa376fc | 31 | #include "sockopt.h" |
718e3744 | 32 | #include "linklist.h" |
33 | #include "plist.h" | |
3f9c7369 | 34 | #include "queue.h" |
039f3a34 | 35 | #include "filter.h" |
718e3744 | 36 | |
37 | #include "bgpd/bgpd.h" | |
38 | #include "bgpd/bgp_table.h" | |
39 | #include "bgpd/bgp_dump.h" | |
40 | #include "bgpd/bgp_attr.h" | |
41 | #include "bgpd/bgp_debug.h" | |
42 | #include "bgpd/bgp_fsm.h" | |
43 | #include "bgpd/bgp_route.h" | |
44 | #include "bgpd/bgp_packet.h" | |
45 | #include "bgpd/bgp_open.h" | |
46 | #include "bgpd/bgp_aspath.h" | |
47 | #include "bgpd/bgp_community.h" | |
48 | #include "bgpd/bgp_ecommunity.h" | |
57d187bc | 49 | #include "bgpd/bgp_lcommunity.h" |
718e3744 | 50 | #include "bgpd/bgp_network.h" |
51 | #include "bgpd/bgp_mplsvpn.h" | |
7ef5a232 | 52 | #include "bgpd/bgp_evpn.h" |
718e3744 | 53 | #include "bgpd/bgp_advertise.h" |
93406d87 | 54 | #include "bgpd/bgp_vty.h" |
3f9c7369 | 55 | #include "bgpd/bgp_updgrp.h" |
cd1964ff | 56 | #include "bgpd/bgp_label.h" |
718e3744 | 57 | |
718e3744 | 58 | /* Set up BGP packet marker and packet type. */ |
3f9c7369 | 59 | int |
718e3744 | 60 | bgp_packet_set_marker (struct stream *s, u_char type) |
61 | { | |
62 | int i; | |
63 | ||
64 | /* Fill in marker. */ | |
65 | for (i = 0; i < BGP_MARKER_SIZE; i++) | |
66 | stream_putc (s, 0xff); | |
67 | ||
68 | /* Dummy total length. This field is should be filled in later on. */ | |
69 | stream_putw (s, 0); | |
70 | ||
71 | /* BGP packet type. */ | |
72 | stream_putc (s, type); | |
73 | ||
74 | /* Return current stream size. */ | |
9985f83c | 75 | return stream_get_endp (s); |
718e3744 | 76 | } |
77 | ||
78 | /* Set BGP packet header size entry. If size is zero then use current | |
79 | stream size. */ | |
3f9c7369 | 80 | int |
718e3744 | 81 | bgp_packet_set_size (struct stream *s) |
82 | { | |
83 | int cp; | |
84 | ||
85 | /* Preserve current pointer. */ | |
9985f83c | 86 | cp = stream_get_endp (s); |
87 | stream_putw_at (s, BGP_MARKER_SIZE, cp); | |
718e3744 | 88 | |
89 | return cp; | |
90 | } | |
91 | ||
92 | /* Add new packet to the peer. */ | |
3f9c7369 | 93 | void |
718e3744 | 94 | bgp_packet_add (struct peer *peer, struct stream *s) |
95 | { | |
96 | /* Add packet to the end of list. */ | |
97 | stream_fifo_push (peer->obuf, s); | |
98 | } | |
99 | ||
100 | /* Free first packet. */ | |
94f2b392 | 101 | static void |
718e3744 | 102 | bgp_packet_delete (struct peer *peer) |
103 | { | |
104 | stream_free (stream_fifo_pop (peer->obuf)); | |
105 | } | |
106 | ||
718e3744 | 107 | /* Check file descriptor whether connect is established. */ |
fc9a856f DS |
108 | int |
109 | bgp_connect_check (struct peer *peer, int change_state) | |
718e3744 | 110 | { |
111 | int status; | |
5228ad27 | 112 | socklen_t slen; |
718e3744 | 113 | int ret; |
114 | ||
115 | /* Anyway I have to reset read and write thread. */ | |
116 | BGP_READ_OFF (peer->t_read); | |
117 | BGP_WRITE_OFF (peer->t_write); | |
118 | ||
119 | /* Check file descriptor. */ | |
120 | slen = sizeof (status); | |
eb821189 | 121 | ret = getsockopt(peer->fd, SOL_SOCKET, SO_ERROR, (void *) &status, &slen); |
718e3744 | 122 | |
123 | /* If getsockopt is fail, this is fatal error. */ | |
124 | if (ret < 0) | |
125 | { | |
16286195 | 126 | zlog_info ("can't get sockopt for nonblocking connect"); |
718e3744 | 127 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
fc9a856f | 128 | return -1; |
718e3744 | 129 | } |
130 | ||
131 | /* When status is 0 then TCP connection is established. */ | |
132 | if (status == 0) | |
133 | { | |
134 | BGP_EVENT_ADD (peer, TCP_connection_open); | |
fc9a856f | 135 | return 1; |
718e3744 | 136 | } |
137 | else | |
138 | { | |
16286195 DS |
139 | if (bgp_debug_neighbor_events(peer)) |
140 | zlog_debug ("%s [Event] Connect failed (%s)", | |
141 | peer->host, safe_strerror (errno)); | |
fc9a856f DS |
142 | if (change_state) |
143 | BGP_EVENT_ADD (peer, TCP_connection_open_failed); | |
144 | return 0; | |
718e3744 | 145 | } |
146 | } | |
147 | ||
94f2b392 | 148 | static struct stream * |
93406d87 | 149 | bgp_update_packet_eor (struct peer *peer, afi_t afi, safi_t safi) |
150 | { | |
151 | struct stream *s; | |
b58ed1f8 | 152 | iana_afi_t pkt_afi; |
9cabb64b | 153 | safi_t pkt_safi; |
93406d87 | 154 | |
750e8146 PJ |
155 | if (DISABLE_BGP_ANNOUNCE) |
156 | return NULL; | |
93406d87 | 157 | |
16286195 | 158 | if (bgp_debug_neighbor_events(peer)) |
93406d87 | 159 | zlog_debug ("send End-of-RIB for %s to %s", afi_safi_print (afi, safi), peer->host); |
160 | ||
161 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
162 | ||
163 | /* Make BGP update packet. */ | |
164 | bgp_packet_set_marker (s, BGP_MSG_UPDATE); | |
165 | ||
166 | /* Unfeasible Routes Length */ | |
167 | stream_putw (s, 0); | |
718e3744 | 168 | |
93406d87 | 169 | if (afi == AFI_IP && safi == SAFI_UNICAST) |
170 | { | |
171 | /* Total Path Attribute Length */ | |
172 | stream_putw (s, 0); | |
173 | } | |
174 | else | |
175 | { | |
9cabb64b | 176 | /* Convert AFI, SAFI to values for packet. */ |
177 | bgp_map_afi_safi_int2iana (afi, safi, &pkt_afi, &pkt_safi); | |
178 | ||
93406d87 | 179 | /* Total Path Attribute Length */ |
180 | stream_putw (s, 6); | |
181 | stream_putc (s, BGP_ATTR_FLAG_OPTIONAL); | |
182 | stream_putc (s, BGP_ATTR_MP_UNREACH_NLRI); | |
183 | stream_putc (s, 3); | |
9cabb64b | 184 | stream_putw (s, pkt_afi); |
185 | stream_putc (s, pkt_safi); | |
93406d87 | 186 | } |
187 | ||
188 | bgp_packet_set_size (s); | |
af4b20d2 DS |
189 | bgp_packet_add (peer, s); |
190 | return s; | |
718e3744 | 191 | } |
192 | ||
718e3744 | 193 | /* Get next packet to be written. */ |
94f2b392 | 194 | static struct stream * |
718e3744 | 195 | bgp_write_packet (struct peer *peer) |
196 | { | |
3f9c7369 DS |
197 | struct stream *s = NULL; |
198 | struct peer_af *paf; | |
199 | struct bpacket *next_pkt; | |
718e3744 | 200 | afi_t afi; |
201 | safi_t safi; | |
718e3744 | 202 | |
203 | s = stream_fifo_head (peer->obuf); | |
204 | if (s) | |
205 | return s; | |
206 | ||
3f9c7369 DS |
207 | /* |
208 | * The code beyond this part deals with update packets, proceed only | |
209 | * if peer is Established and updates are not on hold (as part of | |
210 | * update-delay post processing). | |
211 | */ | |
212 | if (peer->status != Established) | |
213 | return NULL; | |
214 | ||
2a3d5731 | 215 | if (peer->bgp && peer->bgp->main_peers_update_hold) |
4a16ae86 DS |
216 | return NULL; |
217 | ||
718e3744 | 218 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
219 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
220 | { | |
3f9c7369 DS |
221 | paf = peer_af_find (peer, afi, safi); |
222 | if (!paf || !PAF_SUBGRP(paf)) | |
223 | continue; | |
224 | next_pkt = paf->next_pkt_to_send; | |
225 | ||
226 | /* Try to generate a packet for the peer if we are at the end of | |
227 | * the list. Always try to push out WITHDRAWs first. */ | |
228 | if (!next_pkt || !next_pkt->buffer) | |
718e3744 | 229 | { |
3f9c7369 DS |
230 | next_pkt = subgroup_withdraw_packet(PAF_SUBGRP(paf)); |
231 | if (!next_pkt || !next_pkt->buffer) | |
232 | subgroup_update_packet (PAF_SUBGRP(paf)); | |
233 | next_pkt = paf->next_pkt_to_send; | |
718e3744 | 234 | } |
3f9c7369 DS |
235 | |
236 | /* If we still don't have a packet to send to the peer, then | |
237 | * try to find out out if we have to send eor or if not, skip to | |
238 | * the next AFI, SAFI. | |
239 | * Don't send the EOR prematurely... if the subgroup's coalesce | |
240 | * timer is running, the adjacency-out structure is not created | |
241 | * yet. | |
242 | */ | |
243 | if (!next_pkt || !next_pkt->buffer) | |
244 | { | |
245 | if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV)) | |
93406d87 | 246 | { |
3f9c7369 DS |
247 | if (!(PAF_SUBGRP(paf))->t_coalesce && |
248 | peer->afc_nego[afi][safi] && peer->synctime | |
249 | && ! CHECK_FLAG (peer->af_sflags[afi][safi], | |
3d22338f | 250 | PEER_STATUS_EOR_SEND)) |
93406d87 | 251 | { |
3f9c7369 DS |
252 | SET_FLAG (peer->af_sflags[afi][safi], |
253 | PEER_STATUS_EOR_SEND); | |
254 | return bgp_update_packet_eor (peer, afi, safi); | |
93406d87 | 255 | } |
93406d87 | 256 | } |
3f9c7369 | 257 | continue; |
93406d87 | 258 | } |
718e3744 | 259 | |
718e3744 | 260 | |
3f9c7369 DS |
261 | /* |
262 | * Found a packet template to send, overwrite packet with appropriate | |
263 | * attributes from peer and advance peer | |
264 | */ | |
265 | s = bpacket_reformat_for_peer (next_pkt, paf); | |
266 | bpacket_queue_advance_peer (paf); | |
3f9c7369 DS |
267 | return s; |
268 | } | |
718e3744 | 269 | |
3f9c7369 | 270 | return NULL; |
d889623f DS |
271 | } |
272 | ||
3f9c7369 DS |
273 | /* The next action for the peer from a write perspective */ |
274 | static void | |
275 | bgp_write_proceed_actions (struct peer *peer) | |
d889623f DS |
276 | { |
277 | afi_t afi; | |
278 | safi_t safi; | |
3f9c7369 DS |
279 | struct peer_af *paf; |
280 | struct bpacket *next_pkt; | |
281 | int fullq_found = 0; | |
638dc828 | 282 | struct update_subgroup *subgrp; |
d889623f | 283 | |
3f9c7369 | 284 | if (stream_fifo_head (peer->obuf)) |
d889623f | 285 | { |
d889623f | 286 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
3f9c7369 | 287 | return; |
d889623f DS |
288 | } |
289 | ||
3f9c7369 DS |
290 | for (afi = AFI_IP; afi < AFI_MAX; afi++) |
291 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
292 | { | |
293 | paf = peer_af_find (peer, afi, safi); | |
294 | if (!paf) | |
295 | continue; | |
638dc828 DS |
296 | subgrp = paf->subgroup; |
297 | if (!subgrp) | |
298 | continue; | |
299 | ||
3f9c7369 DS |
300 | next_pkt = paf->next_pkt_to_send; |
301 | if (next_pkt && next_pkt->buffer) | |
302 | { | |
303 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); | |
304 | return; | |
305 | } | |
638dc828 | 306 | |
3f9c7369 DS |
307 | /* No packets readily available for AFI/SAFI, are there subgroup packets |
308 | * that need to be generated? */ | |
638dc828 DS |
309 | if (bpacket_queue_is_full(SUBGRP_INST(subgrp), |
310 | SUBGRP_PKTQ(subgrp))) | |
3f9c7369 | 311 | fullq_found = 1; |
638dc828 | 312 | else if (subgroup_packets_to_build (subgrp)) |
3f9c7369 DS |
313 | { |
314 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); | |
315 | return; | |
316 | } | |
638dc828 DS |
317 | |
318 | /* No packets to send, see if EOR is pending */ | |
319 | if (CHECK_FLAG (peer->cap, PEER_CAP_RESTART_RCV)) | |
320 | { | |
321 | if (!subgrp->t_coalesce && | |
322 | peer->afc_nego[afi][safi] && | |
323 | peer->synctime && | |
324 | !CHECK_FLAG(peer->af_sflags[afi][safi], | |
325 | PEER_STATUS_EOR_SEND) && | |
326 | safi != SAFI_MPLS_VPN) | |
327 | { | |
328 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); | |
329 | return; | |
330 | } | |
331 | ||
332 | } | |
3f9c7369 DS |
333 | } |
334 | if (fullq_found) | |
d889623f | 335 | { |
3f9c7369 DS |
336 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
337 | return; | |
d889623f DS |
338 | } |
339 | } | |
340 | ||
718e3744 | 341 | /* Write packet to the peer. */ |
342 | int | |
343 | bgp_write (struct thread *thread) | |
344 | { | |
345 | struct peer *peer; | |
346 | u_char type; | |
3f9c7369 | 347 | struct stream *s; |
718e3744 | 348 | int num; |
1ba2a97a | 349 | int update_last_write = 0; |
fd79ac91 | 350 | unsigned int count = 0; |
ffd0c037 | 351 | unsigned int oc = 0; |
718e3744 | 352 | |
353 | /* Yes first of all get peer pointer. */ | |
354 | peer = THREAD_ARG (thread); | |
355 | peer->t_write = NULL; | |
356 | ||
357 | /* For non-blocking IO check. */ | |
358 | if (peer->status == Connect) | |
359 | { | |
fc9a856f | 360 | bgp_connect_check (peer, 1); |
718e3744 | 361 | return 0; |
362 | } | |
363 | ||
eac5702d SH |
364 | s = bgp_write_packet (peer); |
365 | if (!s) | |
3f9c7369 DS |
366 | { |
367 | bgp_write_proceed_actions (peer); | |
368 | return 0; | |
369 | } | |
eac5702d SH |
370 | |
371 | sockopt_cork (peer->fd, 1); | |
372 | ||
cb1faec9 DS |
373 | oc = peer->update_out; |
374 | ||
eac5702d SH |
375 | /* Nonblocking write until TCP output buffer is full. */ |
376 | do | |
718e3744 | 377 | { |
378 | int writenum; | |
718e3744 | 379 | |
380 | /* Number of bytes to be sent. */ | |
381 | writenum = stream_get_endp (s) - stream_get_getp (s); | |
382 | ||
383 | /* Call write() system call. */ | |
eb821189 | 384 | num = write (peer->fd, STREAM_PNT (s), writenum); |
35398589 | 385 | if (num < 0) |
718e3744 | 386 | { |
eac5702d SH |
387 | /* write failed either retry needed or error */ |
388 | if (ERRNO_IO_RETRY(errno)) | |
389 | break; | |
390 | ||
391 | BGP_EVENT_ADD (peer, TCP_fatal_error); | |
718e3744 | 392 | return 0; |
393 | } | |
35398589 | 394 | |
718e3744 | 395 | if (num != writenum) |
396 | { | |
35398589 | 397 | /* Partial write */ |
9985f83c | 398 | stream_forward_getp (s, num); |
eac5702d | 399 | break; |
718e3744 | 400 | } |
401 | ||
402 | /* Retrieve BGP packet type. */ | |
403 | stream_set_getp (s, BGP_MARKER_SIZE + 2); | |
404 | type = stream_getc (s); | |
405 | ||
406 | switch (type) | |
407 | { | |
408 | case BGP_MSG_OPEN: | |
409 | peer->open_out++; | |
410 | break; | |
411 | case BGP_MSG_UPDATE: | |
412 | peer->update_out++; | |
413 | break; | |
414 | case BGP_MSG_NOTIFY: | |
415 | peer->notify_out++; | |
416 | /* Double start timer. */ | |
417 | peer->v_start *= 2; | |
418 | ||
419 | /* Overflow check. */ | |
420 | if (peer->v_start >= (60 * 2)) | |
421 | peer->v_start = (60 * 2); | |
422 | ||
ca058a30 | 423 | /* Flush any existing events */ |
dcdf399f | 424 | BGP_EVENT_ADD (peer, BGP_Stop); |
3a69f74a SH |
425 | goto done; |
426 | ||
718e3744 | 427 | case BGP_MSG_KEEPALIVE: |
428 | peer->keepalive_out++; | |
429 | break; | |
430 | case BGP_MSG_ROUTE_REFRESH_NEW: | |
431 | case BGP_MSG_ROUTE_REFRESH_OLD: | |
432 | peer->refresh_out++; | |
433 | break; | |
434 | case BGP_MSG_CAPABILITY: | |
435 | peer->dynamic_cap_out++; | |
436 | break; | |
437 | } | |
438 | ||
439 | /* OK we send packet so delete it. */ | |
440 | bgp_packet_delete (peer); | |
1ba2a97a | 441 | update_last_write = 1; |
718e3744 | 442 | } |
cb1faec9 | 443 | while (++count < peer->bgp->wpkt_quanta && |
eac5702d | 444 | (s = bgp_write_packet (peer)) != NULL); |
cb1faec9 | 445 | |
3f9c7369 | 446 | bgp_write_proceed_actions (peer); |
3a69f74a SH |
447 | |
448 | done: | |
1ba2a97a | 449 | /* Update last_update if UPDATEs were written. */ |
cb1faec9 | 450 | if (peer->update_out > oc) |
1ba2a97a DW |
451 | peer->last_update = bgp_clock (); |
452 | ||
453 | /* If we TXed any flavor of packet update last_write */ | |
454 | if (update_last_write) | |
cb1faec9 DS |
455 | peer->last_write = bgp_clock (); |
456 | ||
3a69f74a | 457 | sockopt_cork (peer->fd, 0); |
718e3744 | 458 | return 0; |
459 | } | |
460 | ||
461 | /* This is only for sending NOTIFICATION message to neighbor. */ | |
94f2b392 | 462 | static int |
718e3744 | 463 | bgp_write_notify (struct peer *peer) |
464 | { | |
35398589 | 465 | int ret, val; |
718e3744 | 466 | u_char type; |
3f9c7369 | 467 | struct stream *s; |
718e3744 | 468 | |
469 | /* There should be at least one packet. */ | |
470 | s = stream_fifo_head (peer->obuf); | |
471 | if (!s) | |
472 | return 0; | |
473 | assert (stream_get_endp (s) >= BGP_HEADER_SIZE); | |
474 | ||
86998bc2 LR |
475 | /* Stop collecting data within the socket */ |
476 | sockopt_cork (peer->fd, 0); | |
477 | ||
8ff202e2 DL |
478 | /* socket is in nonblocking mode, if we can't deliver the NOTIFY, well, |
479 | * we only care about getting a clean shutdown at this point. */ | |
86998bc2 | 480 | ret = write (peer->fd, STREAM_DATA (s), stream_get_endp (s)); |
8ff202e2 DL |
481 | |
482 | /* only connection reset/close gets counted as TCP_fatal_error, failure | |
483 | * to write the entire NOTIFY doesn't get different FSM treatment */ | |
718e3744 | 484 | if (ret <= 0) |
485 | { | |
dcdf399f | 486 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
718e3744 | 487 | return 0; |
488 | } | |
489 | ||
86998bc2 LR |
490 | /* Disable Nagle, make NOTIFY packet go out right away */ |
491 | val = 1; | |
492 | (void) setsockopt (peer->fd, IPPROTO_TCP, TCP_NODELAY, | |
493 | (char *) &val, sizeof (val)); | |
494 | ||
718e3744 | 495 | /* Retrieve BGP packet type. */ |
496 | stream_set_getp (s, BGP_MARKER_SIZE + 2); | |
497 | type = stream_getc (s); | |
498 | ||
499 | assert (type == BGP_MSG_NOTIFY); | |
500 | ||
501 | /* Type should be notify. */ | |
502 | peer->notify_out++; | |
503 | ||
504 | /* Double start timer. */ | |
505 | peer->v_start *= 2; | |
506 | ||
507 | /* Overflow check. */ | |
508 | if (peer->v_start >= (60 * 2)) | |
509 | peer->v_start = (60 * 2); | |
510 | ||
1ff9a340 DS |
511 | /* Handle Graceful Restart case where the state changes to |
512 | Connect instead of Idle */ | |
dcdf399f | 513 | BGP_EVENT_ADD (peer, BGP_Stop); |
718e3744 | 514 | |
515 | return 0; | |
516 | } | |
517 | ||
518 | /* Make keepalive packet and send it to the peer. */ | |
519 | void | |
520 | bgp_keepalive_send (struct peer *peer) | |
521 | { | |
522 | struct stream *s; | |
718e3744 | 523 | |
524 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
525 | ||
526 | /* Make keepalive packet. */ | |
527 | bgp_packet_set_marker (s, BGP_MSG_KEEPALIVE); | |
528 | ||
529 | /* Set packet size. */ | |
ffd0c037 | 530 | (void)bgp_packet_set_size (s); |
718e3744 | 531 | |
532 | /* Dump packet if debug option is set. */ | |
533 | /* bgp_packet_dump (s); */ | |
534 | ||
16286195 | 535 | if (bgp_debug_keepalive(peer)) |
6b51474d | 536 | zlog_debug ("%s sending KEEPALIVE", peer->host); |
718e3744 | 537 | |
538 | /* Add packet to the peer. */ | |
539 | bgp_packet_add (peer, s); | |
540 | ||
eb821189 | 541 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 542 | } |
543 | ||
544 | /* Make open packet and send it to the peer. */ | |
545 | void | |
546 | bgp_open_send (struct peer *peer) | |
547 | { | |
548 | struct stream *s; | |
718e3744 | 549 | u_int16_t send_holdtime; |
550 | as_t local_as; | |
551 | ||
552 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) | |
553 | send_holdtime = peer->holdtime; | |
554 | else | |
555 | send_holdtime = peer->bgp->default_holdtime; | |
556 | ||
557 | /* local-as Change */ | |
558 | if (peer->change_local_as) | |
559 | local_as = peer->change_local_as; | |
560 | else | |
561 | local_as = peer->local_as; | |
562 | ||
563 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
564 | ||
565 | /* Make open packet. */ | |
566 | bgp_packet_set_marker (s, BGP_MSG_OPEN); | |
567 | ||
568 | /* Set open packet values. */ | |
569 | stream_putc (s, BGP_VERSION_4); /* BGP version */ | |
0b2aa3a0 PJ |
570 | stream_putw (s, (local_as <= BGP_AS_MAX) ? (u_int16_t) local_as |
571 | : BGP_AS_TRANS); | |
718e3744 | 572 | stream_putw (s, send_holdtime); /* Hold Time */ |
573 | stream_put_in_addr (s, &peer->local_id); /* BGP Identifier */ | |
574 | ||
575 | /* Set capability code. */ | |
576 | bgp_open_capability (s, peer); | |
577 | ||
578 | /* Set BGP packet length. */ | |
ffd0c037 | 579 | (void)bgp_packet_set_size (s); |
718e3744 | 580 | |
16286195 | 581 | if (bgp_debug_neighbor_events(peer)) |
aea339f7 | 582 | zlog_debug ("%s sending OPEN, version %d, my as %u, holdtime %d, id %s", |
718e3744 | 583 | peer->host, BGP_VERSION_4, local_as, |
584 | send_holdtime, inet_ntoa (peer->local_id)); | |
585 | ||
718e3744 | 586 | /* Dump packet if debug option is set. */ |
587 | /* bgp_packet_dump (s); */ | |
588 | ||
589 | /* Add packet to the peer. */ | |
590 | bgp_packet_add (peer, s); | |
591 | ||
eb821189 | 592 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 593 | } |
594 | ||
595 | /* Send BGP notify packet with data potion. */ | |
596 | void | |
597 | bgp_notify_send_with_data (struct peer *peer, u_char code, u_char sub_code, | |
598 | u_char *data, size_t datalen) | |
599 | { | |
600 | struct stream *s; | |
601 | int length; | |
602 | ||
603 | /* Allocate new stream. */ | |
604 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
605 | ||
606 | /* Make nitify packet. */ | |
607 | bgp_packet_set_marker (s, BGP_MSG_NOTIFY); | |
608 | ||
609 | /* Set notify packet values. */ | |
610 | stream_putc (s, code); /* BGP notify code */ | |
611 | stream_putc (s, sub_code); /* BGP notify sub_code */ | |
612 | ||
613 | /* If notify data is present. */ | |
614 | if (data) | |
615 | stream_write (s, data, datalen); | |
616 | ||
617 | /* Set BGP packet length. */ | |
618 | length = bgp_packet_set_size (s); | |
619 | ||
620 | /* Add packet to the peer. */ | |
621 | stream_fifo_clean (peer->obuf); | |
622 | bgp_packet_add (peer, s); | |
623 | ||
624 | /* For debug */ | |
625 | { | |
626 | struct bgp_notify bgp_notify; | |
627 | int first = 0; | |
628 | int i; | |
629 | char c[4]; | |
630 | ||
631 | bgp_notify.code = code; | |
632 | bgp_notify.subcode = sub_code; | |
633 | bgp_notify.data = NULL; | |
634 | bgp_notify.length = length - BGP_MSG_NOTIFY_MIN_SIZE; | |
38de8d02 | 635 | bgp_notify.raw_data = data; |
3a8c7ba1 DW |
636 | |
637 | peer->notify.code = bgp_notify.code; | |
638 | peer->notify.subcode = bgp_notify.subcode; | |
718e3744 | 639 | |
640 | if (bgp_notify.length) | |
641 | { | |
642 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); | |
643 | for (i = 0; i < bgp_notify.length; i++) | |
644 | if (first) | |
645 | { | |
646 | sprintf (c, " %02x", data[i]); | |
647 | strcat (bgp_notify.data, c); | |
648 | } | |
649 | else | |
650 | { | |
651 | first = 1; | |
652 | sprintf (c, "%02x", data[i]); | |
653 | strcpy (bgp_notify.data, c); | |
654 | } | |
655 | } | |
656 | bgp_notify_print (peer, &bgp_notify, "sending"); | |
9e08021c | 657 | |
718e3744 | 658 | if (bgp_notify.data) |
9e08021c DW |
659 | { |
660 | XFREE (MTYPE_TMP, bgp_notify.data); | |
661 | bgp_notify.data = NULL; | |
662 | bgp_notify.length = 0; | |
663 | } | |
718e3744 | 664 | } |
665 | ||
e0701b79 | 666 | /* peer reset cause */ |
3a8c7ba1 | 667 | if (code == BGP_NOTIFY_CEASE) |
e0701b79 | 668 | { |
669 | if (sub_code == BGP_NOTIFY_CEASE_ADMIN_RESET) | |
1212dc19 | 670 | peer->last_reset = PEER_DOWN_USER_RESET; |
e0701b79 | 671 | else if (sub_code == BGP_NOTIFY_CEASE_ADMIN_SHUTDOWN) |
1212dc19 | 672 | peer->last_reset = PEER_DOWN_USER_SHUTDOWN; |
e0701b79 | 673 | else |
1212dc19 | 674 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; |
e0701b79 | 675 | } |
3a8c7ba1 DW |
676 | else |
677 | peer->last_reset = PEER_DOWN_NOTIFY_SEND; | |
e0701b79 | 678 | |
7ccf5e59 | 679 | /* Call immediately. */ |
718e3744 | 680 | BGP_WRITE_OFF (peer->t_write); |
681 | ||
682 | bgp_write_notify (peer); | |
683 | } | |
684 | ||
685 | /* Send BGP notify packet. */ | |
686 | void | |
687 | bgp_notify_send (struct peer *peer, u_char code, u_char sub_code) | |
688 | { | |
689 | bgp_notify_send_with_data (peer, code, sub_code, NULL, 0); | |
690 | } | |
691 | ||
718e3744 | 692 | /* Send route refresh message to the peer. */ |
693 | void | |
694 | bgp_route_refresh_send (struct peer *peer, afi_t afi, safi_t safi, | |
695 | u_char orf_type, u_char when_to_refresh, int remove) | |
696 | { | |
697 | struct stream *s; | |
718e3744 | 698 | struct bgp_filter *filter; |
699 | int orf_refresh = 0; | |
b58ed1f8 | 700 | iana_afi_t pkt_afi; |
9cabb64b | 701 | safi_t pkt_safi; |
718e3744 | 702 | |
750e8146 PJ |
703 | if (DISABLE_BGP_ANNOUNCE) |
704 | return; | |
718e3744 | 705 | |
706 | filter = &peer->filter[afi][safi]; | |
707 | ||
9cabb64b | 708 | /* Convert AFI, SAFI to values for packet. */ |
709 | bgp_map_afi_safi_int2iana (afi, safi, &pkt_afi, &pkt_safi); | |
718e3744 | 710 | |
711 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
712 | ||
713 | /* Make BGP update packet. */ | |
714 | if (CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV)) | |
715 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_NEW); | |
716 | else | |
717 | bgp_packet_set_marker (s, BGP_MSG_ROUTE_REFRESH_OLD); | |
718 | ||
719 | /* Encode Route Refresh message. */ | |
9cabb64b | 720 | stream_putw (s, pkt_afi); |
718e3744 | 721 | stream_putc (s, 0); |
9cabb64b | 722 | stream_putc (s, pkt_safi); |
718e3744 | 723 | |
724 | if (orf_type == ORF_TYPE_PREFIX | |
725 | || orf_type == ORF_TYPE_PREFIX_OLD) | |
726 | if (remove || filter->plist[FILTER_IN].plist) | |
727 | { | |
728 | u_int16_t orf_len; | |
729 | unsigned long orfp; | |
730 | ||
731 | orf_refresh = 1; | |
732 | stream_putc (s, when_to_refresh); | |
733 | stream_putc (s, orf_type); | |
9985f83c | 734 | orfp = stream_get_endp (s); |
718e3744 | 735 | stream_putw (s, 0); |
736 | ||
737 | if (remove) | |
738 | { | |
739 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); | |
740 | stream_putc (s, ORF_COMMON_PART_REMOVE_ALL); | |
16286195 | 741 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 742 | zlog_debug ("%s sending REFRESH_REQ to remove ORF(%d) (%s) for afi/safi: %d/%d", |
718e3744 | 743 | peer->host, orf_type, |
744 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), | |
9cabb64b | 745 | pkt_afi, pkt_safi); |
718e3744 | 746 | } |
747 | else | |
748 | { | |
749 | SET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_PREFIX_SEND); | |
750 | prefix_bgp_orf_entry (s, filter->plist[FILTER_IN].plist, | |
751 | ORF_COMMON_PART_ADD, ORF_COMMON_PART_PERMIT, | |
752 | ORF_COMMON_PART_DENY); | |
16286195 | 753 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 754 | zlog_debug ("%s sending REFRESH_REQ with pfxlist ORF(%d) (%s) for afi/safi: %d/%d", |
718e3744 | 755 | peer->host, orf_type, |
756 | (when_to_refresh == REFRESH_DEFER ? "defer" : "immediate"), | |
9cabb64b | 757 | pkt_afi, pkt_safi); |
718e3744 | 758 | } |
759 | ||
760 | /* Total ORF Entry Len. */ | |
9985f83c | 761 | orf_len = stream_get_endp (s) - orfp - 2; |
718e3744 | 762 | stream_putw_at (s, orfp, orf_len); |
763 | } | |
764 | ||
765 | /* Set packet size. */ | |
ffd0c037 | 766 | (void)bgp_packet_set_size (s); |
718e3744 | 767 | |
16286195 | 768 | if (bgp_debug_neighbor_events(peer)) |
718e3744 | 769 | { |
770 | if (! orf_refresh) | |
6b51474d | 771 | zlog_debug ("%s sending REFRESH_REQ for afi/safi: %d/%d", |
9cabb64b | 772 | peer->host, pkt_afi, pkt_safi); |
718e3744 | 773 | } |
774 | ||
718e3744 | 775 | /* Add packet to the peer. */ |
af4b20d2 | 776 | bgp_packet_add (peer, s); |
718e3744 | 777 | |
eb821189 | 778 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 779 | } |
780 | ||
781 | /* Send capability message to the peer. */ | |
782 | void | |
783 | bgp_capability_send (struct peer *peer, afi_t afi, safi_t safi, | |
784 | int capability_code, int action) | |
785 | { | |
786 | struct stream *s; | |
b58ed1f8 | 787 | iana_afi_t pkt_afi; |
9cabb64b | 788 | safi_t pkt_safi; |
718e3744 | 789 | |
9cabb64b | 790 | /* Convert AFI, SAFI to values for packet. */ |
791 | bgp_map_afi_safi_int2iana (afi, safi, &pkt_afi, &pkt_safi); | |
718e3744 | 792 | |
793 | s = stream_new (BGP_MAX_PACKET_SIZE); | |
794 | ||
795 | /* Make BGP update packet. */ | |
796 | bgp_packet_set_marker (s, BGP_MSG_CAPABILITY); | |
797 | ||
798 | /* Encode MP_EXT capability. */ | |
799 | if (capability_code == CAPABILITY_CODE_MP) | |
800 | { | |
801 | stream_putc (s, action); | |
802 | stream_putc (s, CAPABILITY_CODE_MP); | |
803 | stream_putc (s, CAPABILITY_CODE_MP_LEN); | |
9cabb64b | 804 | stream_putw (s, pkt_afi); |
718e3744 | 805 | stream_putc (s, 0); |
9cabb64b | 806 | stream_putc (s, pkt_safi); |
718e3744 | 807 | |
16286195 | 808 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 809 | zlog_debug ("%s sending CAPABILITY has %s MP_EXT CAP for afi/safi: %d/%d", |
718e3744 | 810 | peer->host, action == CAPABILITY_ACTION_SET ? |
9cabb64b | 811 | "Advertising" : "Removing", pkt_afi, pkt_safi); |
718e3744 | 812 | } |
813 | ||
718e3744 | 814 | /* Set packet size. */ |
ffd0c037 | 815 | (void)bgp_packet_set_size (s); |
718e3744 | 816 | |
718e3744 | 817 | /* Add packet to the peer. */ |
af4b20d2 | 818 | bgp_packet_add (peer, s); |
718e3744 | 819 | |
eb821189 | 820 | BGP_WRITE_ON (peer->t_write, bgp_write, peer->fd); |
718e3744 | 821 | } |
6b0655a2 | 822 | |
718e3744 | 823 | /* RFC1771 6.8 Connection collision detection. */ |
94f2b392 | 824 | static int |
eb821189 | 825 | bgp_collision_detect (struct peer *new, struct in_addr remote_id) |
718e3744 | 826 | { |
eb821189 | 827 | struct peer *peer; |
718e3744 | 828 | |
718e3744 | 829 | /* Upon receipt of an OPEN message, the local system must examine |
830 | all of its connections that are in the OpenConfirm state. A BGP | |
831 | speaker may also examine connections in an OpenSent state if it | |
832 | knows the BGP Identifier of the peer by means outside of the | |
833 | protocol. If among these connections there is a connection to a | |
834 | remote BGP speaker whose BGP Identifier equals the one in the | |
835 | OPEN message, then the local system performs the following | |
836 | collision resolution procedure: */ | |
837 | ||
1ff9a340 | 838 | if ((peer = new->doppelganger) != NULL) |
718e3744 | 839 | { |
1ff9a340 DS |
840 | /* Do not accept the new connection in Established or Clearing states. |
841 | * Note that a peer GR is handled by closing the existing connection | |
842 | * upon receipt of new one. | |
843 | */ | |
844 | if (peer->status == Established || peer->status == Clearing) | |
845 | { | |
846 | bgp_notify_send (new, BGP_NOTIFY_CEASE, | |
847 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
848 | return (-1); | |
849 | } | |
850 | else if ((peer->status == OpenConfirm) || (peer->status == OpenSent)) | |
eb821189 | 851 | { |
718e3744 | 852 | /* 1. The BGP Identifier of the local system is compared to |
853 | the BGP Identifier of the remote system (as specified in | |
854 | the OPEN message). */ | |
855 | ||
856 | if (ntohl (peer->local_id.s_addr) < ntohl (remote_id.s_addr)) | |
1ff9a340 DS |
857 | if (!CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
858 | { | |
859 | /* 2. If the value of the local BGP Identifier is less | |
860 | than the remote one, the local system closes BGP | |
861 | connection that already exists (the one that is | |
862 | already in the OpenConfirm state), and accepts BGP | |
863 | connection initiated by the remote system. */ | |
864 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, | |
865 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
866 | return 1; | |
867 | } | |
868 | else | |
869 | { | |
870 | bgp_notify_send (new, BGP_NOTIFY_CEASE, | |
871 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
872 | return -1; | |
873 | } | |
718e3744 | 874 | else |
875 | { | |
876 | /* 3. Otherwise, the local system closes newly created | |
877 | BGP connection (the one associated with the newly | |
878 | received OPEN message), and continues to use the | |
879 | existing one (the one that is already in the | |
880 | OpenConfirm state). */ | |
1ff9a340 DS |
881 | if (CHECK_FLAG(peer->sflags, PEER_STATUS_ACCEPT_PEER)) |
882 | { | |
883 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, | |
884 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
885 | return 1; | |
886 | } | |
887 | else | |
888 | { | |
889 | bgp_notify_send (new, BGP_NOTIFY_CEASE, | |
890 | BGP_NOTIFY_CEASE_COLLISION_RESOLUTION); | |
891 | return -1; | |
892 | } | |
718e3744 | 893 | } |
eb821189 | 894 | } |
895 | } | |
718e3744 | 896 | return 0; |
897 | } | |
898 | ||
94f2b392 | 899 | static int |
718e3744 | 900 | bgp_open_receive (struct peer *peer, bgp_size_t size) |
901 | { | |
902 | int ret; | |
903 | u_char version; | |
904 | u_char optlen; | |
905 | u_int16_t holdtime; | |
906 | u_int16_t send_holdtime; | |
907 | as_t remote_as; | |
0b2aa3a0 | 908 | as_t as4 = 0; |
718e3744 | 909 | struct in_addr remote_id; |
3b381c32 | 910 | int mp_capability; |
5228ad27 | 911 | u_int8_t notify_data_remote_as[2]; |
c9e3a6f4 | 912 | u_int8_t notify_data_remote_as4[4]; |
5228ad27 | 913 | u_int8_t notify_data_remote_id[4]; |
b184ccd8 | 914 | u_int16_t *holdtime_ptr; |
718e3744 | 915 | |
718e3744 | 916 | /* Parse open packet. */ |
917 | version = stream_getc (peer->ibuf); | |
918 | memcpy (notify_data_remote_as, stream_pnt (peer->ibuf), 2); | |
919 | remote_as = stream_getw (peer->ibuf); | |
ffd0c037 | 920 | holdtime_ptr = (u_int16_t *)stream_pnt (peer->ibuf); |
718e3744 | 921 | holdtime = stream_getw (peer->ibuf); |
922 | memcpy (notify_data_remote_id, stream_pnt (peer->ibuf), 4); | |
923 | remote_id.s_addr = stream_get_ipv4 (peer->ibuf); | |
924 | ||
925 | /* Receive OPEN message log */ | |
16286195 | 926 | if (bgp_debug_neighbor_events(peer)) |
aea339f7 | 927 | zlog_debug ("%s rcv OPEN, version %d, remote-as (in open) %u," |
0b2aa3a0 PJ |
928 | " holdtime %d, id %s", |
929 | peer->host, version, remote_as, holdtime, | |
930 | inet_ntoa (remote_id)); | |
931 | ||
932 | /* BEGIN to read the capability here, but dont do it yet */ | |
3b381c32 | 933 | mp_capability = 0; |
0b2aa3a0 PJ |
934 | optlen = stream_getc (peer->ibuf); |
935 | ||
936 | if (optlen != 0) | |
937 | { | |
09825025 | 938 | /* If not enough bytes, it is an error. */ |
939 | if (STREAM_READABLE(peer->ibuf) < optlen) | |
940 | { | |
941 | bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, | |
942 | BGP_NOTIFY_OPEN_MALFORMED_ATTR); | |
943 | return -1; | |
944 | } | |
945 | ||
0b2aa3a0 PJ |
946 | /* We need the as4 capability value *right now* because |
947 | * if it is there, we have not got the remote_as yet, and without | |
948 | * that we do not know which peer is connecting to us now. | |
949 | */ | |
950 | as4 = peek_for_as4_capability (peer, optlen); | |
c9e3a6f4 | 951 | memcpy (notify_data_remote_as4, &as4, 4); |
0b2aa3a0 PJ |
952 | } |
953 | ||
954 | /* Just in case we have a silly peer who sends AS4 capability set to 0 */ | |
955 | if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) && !as4) | |
956 | { | |
957 | zlog_err ("%s bad OPEN, got AS4 capability, but AS4 set to 0", | |
958 | peer->host); | |
c9e3a6f4 DS |
959 | bgp_notify_send_with_data (peer, |
960 | BGP_NOTIFY_OPEN_ERR, | |
961 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
962 | notify_data_remote_as4, 4); | |
0b2aa3a0 PJ |
963 | return -1; |
964 | } | |
965 | ||
966 | if (remote_as == BGP_AS_TRANS) | |
967 | { | |
968 | /* Take the AS4 from the capability. We must have received the | |
969 | * capability now! Otherwise we have a asn16 peer who uses | |
970 | * BGP_AS_TRANS, for some unknown reason. | |
971 | */ | |
972 | if (as4 == BGP_AS_TRANS) | |
973 | { | |
974 | zlog_err ("%s [AS4] NEW speaker using AS_TRANS for AS4, not allowed", | |
975 | peer->host); | |
c9e3a6f4 DS |
976 | bgp_notify_send_with_data (peer, |
977 | BGP_NOTIFY_OPEN_ERR, | |
978 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
979 | notify_data_remote_as4, 4); | |
0b2aa3a0 PJ |
980 | return -1; |
981 | } | |
982 | ||
983 | if (!as4 && BGP_DEBUG (as4, AS4)) | |
984 | zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but no AS4." | |
985 | " Odd, but proceeding.", peer->host); | |
986 | else if (as4 < BGP_AS_MAX && BGP_DEBUG (as4, AS4)) | |
0df7c91f | 987 | zlog_debug ("%s [AS4] OPEN remote_as is AS_TRANS, but AS4 (%u) fits " |
0b2aa3a0 PJ |
988 | "in 2-bytes, very odd peer.", peer->host, as4); |
989 | if (as4) | |
990 | remote_as = as4; | |
991 | } | |
992 | else | |
993 | { | |
994 | /* We may have a partner with AS4 who has an asno < BGP_AS_MAX */ | |
995 | /* If we have got the capability, peer->as4cap must match remote_as */ | |
996 | if (CHECK_FLAG (peer->cap, PEER_CAP_AS4_RCV) | |
997 | && as4 != remote_as) | |
998 | { | |
999 | /* raise error, log this, close session */ | |
1000 | zlog_err ("%s bad OPEN, got AS4 capability, but remote_as %u" | |
1001 | " mismatch with 16bit 'myasn' %u in open", | |
1002 | peer->host, as4, remote_as); | |
c9e3a6f4 DS |
1003 | bgp_notify_send_with_data (peer, |
1004 | BGP_NOTIFY_OPEN_ERR, | |
1005 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1006 | notify_data_remote_as4, 4); | |
0b2aa3a0 PJ |
1007 | return -1; |
1008 | } | |
1009 | } | |
1010 | ||
718e3744 | 1011 | /* remote router-id check. */ |
58298ccd DS |
1012 | if (remote_id.s_addr == 0 |
1013 | || IPV4_CLASS_DE (ntohl (remote_id.s_addr)) | |
1014 | || ntohl (peer->local_id.s_addr) == ntohl (remote_id.s_addr)) | |
718e3744 | 1015 | { |
16286195 | 1016 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 1017 | zlog_debug ("%s bad OPEN, wrong router identifier %s", |
718e3744 | 1018 | peer->host, inet_ntoa (remote_id)); |
1019 | bgp_notify_send_with_data (peer, | |
1020 | BGP_NOTIFY_OPEN_ERR, | |
1021 | BGP_NOTIFY_OPEN_BAD_BGP_IDENT, | |
1022 | notify_data_remote_id, 4); | |
1023 | return -1; | |
1024 | } | |
1025 | ||
1026 | /* Set remote router-id */ | |
1027 | peer->remote_id = remote_id; | |
1028 | ||
1029 | /* Peer BGP version check. */ | |
1030 | if (version != BGP_VERSION_4) | |
1031 | { | |
a689e6a9 LR |
1032 | u_int16_t maxver = htons(BGP_VERSION_4); |
1033 | /* XXX this reply may not be correct if version < 4 XXX */ | |
16286195 | 1034 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 1035 | zlog_debug ("%s bad protocol version, remote requested %d, local request %d", |
718e3744 | 1036 | peer->host, version, BGP_VERSION_4); |
a689e6a9 | 1037 | /* Data must be in network byte order here */ |
718e3744 | 1038 | bgp_notify_send_with_data (peer, |
1039 | BGP_NOTIFY_OPEN_ERR, | |
1040 | BGP_NOTIFY_OPEN_UNSUP_VERSION, | |
a689e6a9 | 1041 | (u_int8_t *) &maxver, 2); |
718e3744 | 1042 | return -1; |
1043 | } | |
1044 | ||
1045 | /* Check neighbor as number. */ | |
f8cfafda DS |
1046 | if (peer->as_type == AS_UNSPECIFIED) |
1047 | { | |
1048 | if (bgp_debug_neighbor_events(peer)) | |
1049 | zlog_debug("%s bad OPEN, remote AS is unspecified currently", peer->host); | |
1050 | bgp_notify_send_with_data(peer, | |
1051 | BGP_NOTIFY_OPEN_ERR, | |
1052 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1053 | notify_data_remote_as, 2); | |
1054 | return -1; | |
1055 | } | |
1056 | else if (peer->as_type == AS_INTERNAL) | |
0299c004 DS |
1057 | { |
1058 | if (remote_as != peer->bgp->as) | |
1059 | { | |
1060 | if (bgp_debug_neighbor_events(peer)) | |
1061 | zlog_debug ("%s bad OPEN, remote AS is %u, internal specified", | |
1062 | peer->host, remote_as); | |
1063 | bgp_notify_send_with_data (peer, | |
1064 | BGP_NOTIFY_OPEN_ERR, | |
1065 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1066 | notify_data_remote_as, 2); | |
1067 | return -1; | |
1068 | } | |
1069 | peer->as = peer->local_as; | |
1070 | } | |
1071 | else if (peer->as_type == AS_EXTERNAL) | |
1072 | { | |
1073 | if (remote_as == peer->bgp->as) | |
1074 | { | |
1075 | if (bgp_debug_neighbor_events(peer)) | |
1076 | zlog_debug ("%s bad OPEN, remote AS is %u, external specified", | |
1077 | peer->host, remote_as); | |
1078 | bgp_notify_send_with_data (peer, | |
1079 | BGP_NOTIFY_OPEN_ERR, | |
1080 | BGP_NOTIFY_OPEN_BAD_PEER_AS, | |
1081 | notify_data_remote_as, 2); | |
1082 | return -1; | |
1083 | } | |
1084 | peer->as = remote_as; | |
1085 | } | |
1086 | else if ((peer->as_type == AS_SPECIFIED) && (remote_as != peer->as)) | |
718e3744 | 1087 | { |
16286195 | 1088 | if (bgp_debug_neighbor_events(peer)) |
aea339f7 | 1089 | zlog_debug ("%s bad OPEN, remote AS is %u, expected %u", |
0299c004 DS |
1090 | peer->host, remote_as, peer->as); |
1091 | bgp_notify_send_with_data (peer, | |
1092 | BGP_NOTIFY_OPEN_ERR, | |
718e3744 | 1093 | BGP_NOTIFY_OPEN_BAD_PEER_AS, |
1094 | notify_data_remote_as, 2); | |
1095 | return -1; | |
1096 | } | |
1097 | ||
1098 | /* From the rfc: Upon receipt of an OPEN message, a BGP speaker MUST | |
1099 | calculate the value of the Hold Timer by using the smaller of its | |
1100 | configured Hold Time and the Hold Time received in the OPEN message. | |
1101 | The Hold Time MUST be either zero or at least three seconds. An | |
1102 | implementation may reject connections on the basis of the Hold Time. */ | |
1103 | ||
1104 | if (holdtime < 3 && holdtime != 0) | |
1105 | { | |
b184ccd8 DS |
1106 | bgp_notify_send_with_data (peer, |
1107 | BGP_NOTIFY_OPEN_ERR, | |
1108 | BGP_NOTIFY_OPEN_UNACEP_HOLDTIME, | |
ffd0c037 | 1109 | (u_char *)holdtime_ptr, 2); |
718e3744 | 1110 | return -1; |
1111 | } | |
1112 | ||
1113 | /* From the rfc: A reasonable maximum time between KEEPALIVE messages | |
1114 | would be one third of the Hold Time interval. KEEPALIVE messages | |
1115 | MUST NOT be sent more frequently than one per second. An | |
1116 | implementation MAY adjust the rate at which it sends KEEPALIVE | |
1117 | messages as a function of the Hold Time interval. */ | |
1118 | ||
1119 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) | |
1120 | send_holdtime = peer->holdtime; | |
1121 | else | |
1122 | send_holdtime = peer->bgp->default_holdtime; | |
1123 | ||
1124 | if (holdtime < send_holdtime) | |
1125 | peer->v_holdtime = holdtime; | |
1126 | else | |
1127 | peer->v_holdtime = send_holdtime; | |
1128 | ||
10a6a98b DW |
1129 | if (CHECK_FLAG (peer->config, PEER_CONFIG_TIMER)) |
1130 | peer->v_keepalive = peer->keepalive; | |
1131 | else | |
1132 | peer->v_keepalive = peer->v_holdtime / 3; | |
718e3744 | 1133 | |
1134 | /* Open option part parse. */ | |
718e3744 | 1135 | if (optlen != 0) |
1136 | { | |
3b381c32 | 1137 | if ((ret = bgp_open_option_parse (peer, optlen, &mp_capability)) < 0) |
14051b36 | 1138 | return ret; |
718e3744 | 1139 | } |
1140 | else | |
1141 | { | |
16286195 | 1142 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 1143 | zlog_debug ("%s rcvd OPEN w/ OPTION parameter len: 0", |
718e3744 | 1144 | peer->host); |
1145 | } | |
1146 | ||
3b381c32 AS |
1147 | /* |
1148 | * Assume that the peer supports the locally configured set of | |
1149 | * AFI/SAFIs if the peer did not send us any Mulitiprotocol | |
1150 | * capabilities, or if 'override-capability' is configured. | |
1151 | */ | |
1152 | if (! mp_capability || | |
1153 | CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) | |
718e3744 | 1154 | { |
1155 | peer->afc_nego[AFI_IP][SAFI_UNICAST] = peer->afc[AFI_IP][SAFI_UNICAST]; | |
1156 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] = peer->afc[AFI_IP][SAFI_MULTICAST]; | |
cd1964ff | 1157 | peer->afc_nego[AFI_IP][SAFI_LABELED_UNICAST] = peer->afc[AFI_IP][SAFI_LABELED_UNICAST]; |
718e3744 | 1158 | peer->afc_nego[AFI_IP6][SAFI_UNICAST] = peer->afc[AFI_IP6][SAFI_UNICAST]; |
1159 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = peer->afc[AFI_IP6][SAFI_MULTICAST]; | |
cd1964ff | 1160 | peer->afc_nego[AFI_IP6][SAFI_LABELED_UNICAST] = peer->afc[AFI_IP6][SAFI_LABELED_UNICAST]; |
3d22338f | 1161 | peer->afc_nego[AFI_L2VPN][SAFI_EVPN] = peer->afc[AFI_L2VPN][SAFI_EVPN]; |
718e3744 | 1162 | } |
1163 | ||
1ff9a340 DS |
1164 | /* When collision is detected and this peer is closed. Retrun |
1165 | immidiately. */ | |
1166 | ret = bgp_collision_detect (peer, remote_id); | |
1167 | if (ret < 0) | |
1168 | return ret; | |
1169 | ||
718e3744 | 1170 | /* Get sockname. */ |
1ff9a340 DS |
1171 | if ((ret = bgp_getsockname (peer)) < 0) |
1172 | { | |
1173 | zlog_err("%s: bgp_getsockname() failed for peer: %s", __FUNCTION__, | |
1174 | peer->host); | |
1175 | return (ret); | |
1176 | } | |
718e3744 | 1177 | |
e33a4880 | 1178 | /* Verify valid local address present based on negotiated address-families. */ |
1179 | if (peer->afc_nego[AFI_IP][SAFI_UNICAST] || | |
1180 | peer->afc_nego[AFI_IP][SAFI_MULTICAST] || | |
587ff0fd LB |
1181 | peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] || |
1182 | peer->afc_nego[AFI_IP][SAFI_ENCAP]) | |
e33a4880 | 1183 | { |
1184 | if (!peer->nexthop.v4.s_addr) | |
1185 | { | |
4d41dd8b | 1186 | #if defined (HAVE_CUMULUS) |
e33a4880 | 1187 | zlog_err ("%s: No local IPv4 addr resetting connection, fd %d", |
1188 | peer->host, peer->fd); | |
1189 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
1190 | return -1; | |
1d808091 | 1191 | #endif |
e33a4880 | 1192 | } |
1193 | } | |
1194 | if (peer->afc_nego[AFI_IP6][SAFI_UNICAST] || | |
6407da5a | 1195 | peer->afc_nego[AFI_IP6][SAFI_MULTICAST] || |
587ff0fd LB |
1196 | peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN] || |
1197 | peer->afc_nego[AFI_IP6][SAFI_ENCAP]) | |
e33a4880 | 1198 | { |
1199 | if (IN6_IS_ADDR_UNSPECIFIED (&peer->nexthop.v6_global)) | |
1200 | { | |
4d41dd8b | 1201 | #if defined (HAVE_CUMULUS) |
e33a4880 | 1202 | zlog_err ("%s: No local IPv6 addr resetting connection, fd %d", |
1203 | peer->host, peer->fd); | |
1204 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, BGP_NOTIFY_SUBCODE_UNSPECIFIC); | |
1205 | return -1; | |
1d808091 | 1206 | #endif |
e33a4880 | 1207 | } |
1208 | } | |
0cd0d979 | 1209 | peer->rtt = sockopt_tcp_rtt (peer->fd); |
e33a4880 | 1210 | |
1ff9a340 DS |
1211 | if ((ret = bgp_event_update(peer, Receive_OPEN_message)) < 0) |
1212 | { | |
1213 | zlog_err("%s: BGP event update failed for peer: %s", __FUNCTION__, | |
1214 | peer->host); | |
1215 | /* DD: bgp send notify and reset state */ | |
1216 | return (ret); | |
1217 | } | |
718e3744 | 1218 | |
1219 | peer->packet_size = 0; | |
1220 | if (peer->ibuf) | |
1221 | stream_reset (peer->ibuf); | |
1222 | ||
1223 | return 0; | |
1224 | } | |
1225 | ||
f188f2c4 DS |
1226 | /* Called when there is a change in the EOR(implicit or explicit) status of a peer. |
1227 | Ends the update-delay if all expected peers are done with EORs. */ | |
1228 | void | |
1229 | bgp_check_update_delay(struct bgp *bgp) | |
1230 | { | |
1231 | struct listnode *node, *nnode; | |
ffd0c037 | 1232 | struct peer *peer = NULL; |
f188f2c4 | 1233 | |
16286195 | 1234 | if (bgp_debug_neighbor_events(peer)) |
f188f2c4 DS |
1235 | zlog_debug ("Checking update delay, T: %d R: %d I:%d E: %d", bgp->established, |
1236 | bgp->restarted_peers, bgp->implicit_eors, bgp->explicit_eors); | |
1237 | ||
1238 | if (bgp->established <= | |
1239 | bgp->restarted_peers + bgp->implicit_eors + bgp->explicit_eors) | |
1240 | { | |
1241 | /* This is an extra sanity check to make sure we wait for all the | |
1242 | eligible configured peers. This check is performed if establish wait | |
1243 | timer is on, or establish wait option is not given with the | |
1244 | update-delay command */ | |
1245 | if (bgp->t_establish_wait || | |
1246 | (bgp->v_establish_wait == bgp->v_update_delay)) | |
1247 | for (ALL_LIST_ELEMENTS (bgp->peer, node, nnode, peer)) | |
1248 | { | |
1ff9a340 DS |
1249 | if (CHECK_FLAG(peer->flags, PEER_FLAG_CONFIG_NODE) |
1250 | && !CHECK_FLAG (peer->flags, PEER_FLAG_SHUTDOWN) | |
f188f2c4 DS |
1251 | && !peer->update_delay_over) |
1252 | { | |
16286195 | 1253 | if (bgp_debug_neighbor_events(peer)) |
f188f2c4 DS |
1254 | zlog_debug (" Peer %s pending, continuing read-only mode", |
1255 | peer->host); | |
1256 | return; | |
1257 | } | |
1258 | } | |
1259 | ||
1260 | zlog_info ("Update delay ended, restarted: %d, EORs implicit: %d, explicit: %d", | |
1261 | bgp->restarted_peers, bgp->implicit_eors, bgp->explicit_eors); | |
1262 | bgp_update_delay_end(bgp); | |
1263 | } | |
1264 | } | |
1265 | ||
1266 | /* Called if peer is known to have restarted. The restart-state bit in | |
1267 | Graceful-Restart capability is used for that */ | |
1268 | void | |
1269 | bgp_update_restarted_peers (struct peer *peer) | |
1270 | { | |
1271 | if (!bgp_update_delay_active(peer->bgp)) return; /* BGP update delay has ended */ | |
1272 | if (peer->update_delay_over) return; /* This peer has already been considered */ | |
1273 | ||
16286195 | 1274 | if (bgp_debug_neighbor_events(peer)) |
f188f2c4 DS |
1275 | zlog_debug ("Peer %s: Checking restarted", peer->host); |
1276 | ||
1277 | if (peer->status == Established) | |
1278 | { | |
1279 | peer->update_delay_over = 1; | |
1280 | peer->bgp->restarted_peers++; | |
1281 | bgp_check_update_delay(peer->bgp); | |
1282 | } | |
1283 | } | |
1284 | ||
1285 | /* Called as peer receives a keep-alive. Determines if this occurence can be | |
1286 | taken as an implicit EOR for this peer. | |
1287 | NOTE: The very first keep-alive after the Established state of a peer is | |
1288 | considered implicit EOR for the update-delay purposes */ | |
1289 | void | |
1290 | bgp_update_implicit_eors (struct peer *peer) | |
1291 | { | |
1292 | if (!bgp_update_delay_active(peer->bgp)) return; /* BGP update delay has ended */ | |
1293 | if (peer->update_delay_over) return; /* This peer has already been considered */ | |
1294 | ||
16286195 | 1295 | if (bgp_debug_neighbor_events(peer)) |
f188f2c4 DS |
1296 | zlog_debug ("Peer %s: Checking implicit EORs", peer->host); |
1297 | ||
1298 | if (peer->status == Established) | |
1299 | { | |
1300 | peer->update_delay_over = 1; | |
1301 | peer->bgp->implicit_eors++; | |
1302 | bgp_check_update_delay(peer->bgp); | |
1303 | } | |
1304 | } | |
1305 | ||
1306 | /* Should be called only when there is a change in the EOR_RECEIVED status | |
1307 | for any afi/safi on a peer */ | |
1308 | static void | |
1309 | bgp_update_explicit_eors (struct peer *peer) | |
1310 | { | |
1311 | afi_t afi; | |
1312 | safi_t safi; | |
1313 | ||
1314 | if (!bgp_update_delay_active(peer->bgp)) return; /* BGP update delay has ended */ | |
1315 | if (peer->update_delay_over) return; /* This peer has already been considered */ | |
1316 | ||
16286195 | 1317 | if (bgp_debug_neighbor_events(peer)) |
f188f2c4 DS |
1318 | zlog_debug ("Peer %s: Checking explicit EORs", peer->host); |
1319 | ||
1320 | for (afi = AFI_IP; afi < AFI_MAX; afi++) | |
1321 | for (safi = SAFI_UNICAST; safi < SAFI_MAX; safi++) | |
1322 | { | |
1323 | if (peer->afc_nego[afi][safi] && | |
1324 | !CHECK_FLAG(peer->af_sflags[afi][safi], PEER_STATUS_EOR_RECEIVED)) | |
1325 | { | |
16286195 | 1326 | if (bgp_debug_neighbor_events(peer)) |
f188f2c4 DS |
1327 | zlog_debug (" afi %d safi %d didnt receive EOR", afi, safi); |
1328 | return; | |
1329 | } | |
1330 | } | |
1331 | ||
1332 | peer->update_delay_over = 1; | |
1333 | peer->bgp->explicit_eors++; | |
1334 | bgp_check_update_delay(peer->bgp); | |
1335 | } | |
1336 | ||
7ef5a232 PG |
1337 | /* Frontend for NLRI parsing, to fan-out to AFI/SAFI specific parsers |
1338 | * mp_withdraw, if set, is used to nullify attr structure on most of the calling safi function | |
1339 | * and for evpn, passed as parameter | |
1340 | */ | |
96e52474 | 1341 | int |
7ef5a232 | 1342 | bgp_nlri_parse (struct peer *peer, struct attr *attr, struct bgp_nlri *packet, int mp_withdraw) |
96e52474 PJ |
1343 | { |
1344 | switch (packet->safi) | |
1345 | { | |
1346 | case SAFI_UNICAST: | |
1347 | case SAFI_MULTICAST: | |
7ef5a232 | 1348 | return bgp_nlri_parse_ip (peer, mp_withdraw?NULL:attr, packet); |
cd1964ff | 1349 | case SAFI_LABELED_UNICAST: |
ea47f948 | 1350 | return bgp_nlri_parse_label (peer, mp_withdraw?NULL:attr, packet); |
96e52474 | 1351 | case SAFI_MPLS_VPN: |
7ef5a232 | 1352 | return bgp_nlri_parse_vpn (peer, mp_withdraw?NULL:attr, packet); |
7ef5a232 PG |
1353 | case SAFI_EVPN: |
1354 | return bgp_nlri_parse_evpn (peer, attr, packet, mp_withdraw); | |
96e52474 PJ |
1355 | } |
1356 | return -1; | |
1357 | } | |
1358 | ||
718e3744 | 1359 | /* Parse BGP Update packet and make attribute object. */ |
94f2b392 | 1360 | static int |
718e3744 | 1361 | bgp_update_receive (struct peer *peer, bgp_size_t size) |
1362 | { | |
96e52474 | 1363 | int ret, nlri_ret; |
718e3744 | 1364 | u_char *end; |
1365 | struct stream *s; | |
1366 | struct attr attr; | |
489d005a | 1367 | struct attr_extra extra; |
718e3744 | 1368 | bgp_size_t attribute_len; |
1369 | bgp_size_t update_len; | |
1370 | bgp_size_t withdraw_len; | |
718e3744 | 1371 | |
96e52474 PJ |
1372 | enum NLRI_TYPES { |
1373 | NLRI_UPDATE, | |
1374 | NLRI_WITHDRAW, | |
1375 | NLRI_MP_UPDATE, | |
1376 | NLRI_MP_WITHDRAW, | |
1377 | NLRI_TYPE_MAX | |
1378 | }; | |
1379 | struct bgp_nlri nlris[NLRI_TYPE_MAX]; | |
1380 | ||
718e3744 | 1381 | /* Status must be Established. */ |
1382 | if (peer->status != Established) | |
1383 | { | |
1384 | zlog_err ("%s [FSM] Update packet received under status %s", | |
1385 | peer->host, LOOKUP (bgp_status_msg, peer->status)); | |
1386 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); | |
1387 | return -1; | |
1388 | } | |
1389 | ||
1390 | /* Set initial values. */ | |
1391 | memset (&attr, 0, sizeof (struct attr)); | |
489d005a | 1392 | memset (&extra, 0, sizeof (struct attr_extra)); |
0ce5d20e | 1393 | extra.label_index = BGP_INVALID_LABEL_INDEX; |
96e52474 | 1394 | memset (&nlris, 0, sizeof (nlris)); |
489d005a | 1395 | attr.extra = &extra; |
16286195 DS |
1396 | memset (peer->rcvd_attr_str, 0, BUFSIZ); |
1397 | peer->rcvd_attr_printed = 0; | |
718e3744 | 1398 | |
1399 | s = peer->ibuf; | |
1400 | end = stream_pnt (s) + size; | |
1401 | ||
1402 | /* RFC1771 6.3 If the Unfeasible Routes Length or Total Attribute | |
1403 | Length is too large (i.e., if Unfeasible Routes Length + Total | |
1404 | Attribute Length + 23 exceeds the message Length), then the Error | |
1405 | Subcode is set to Malformed Attribute List. */ | |
1406 | if (stream_pnt (s) + 2 > end) | |
1407 | { | |
1408 | zlog_err ("%s [Error] Update packet error" | |
1409 | " (packet length is short for unfeasible length)", | |
1410 | peer->host); | |
1411 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1412 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1413 | return -1; | |
1414 | } | |
1415 | ||
1416 | /* Unfeasible Route Length. */ | |
1417 | withdraw_len = stream_getw (s); | |
1418 | ||
1419 | /* Unfeasible Route Length check. */ | |
1420 | if (stream_pnt (s) + withdraw_len > end) | |
1421 | { | |
1422 | zlog_err ("%s [Error] Update packet error" | |
1423 | " (packet unfeasible length overflow %d)", | |
1424 | peer->host, withdraw_len); | |
1425 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1426 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1427 | return -1; | |
1428 | } | |
1429 | ||
1430 | /* Unfeasible Route packet format check. */ | |
1431 | if (withdraw_len > 0) | |
1432 | { | |
96e52474 PJ |
1433 | nlris[NLRI_WITHDRAW].afi = AFI_IP; |
1434 | nlris[NLRI_WITHDRAW].safi = SAFI_UNICAST; | |
1435 | nlris[NLRI_WITHDRAW].nlri = stream_pnt (s); | |
1436 | nlris[NLRI_WITHDRAW].length = withdraw_len; | |
9985f83c | 1437 | stream_forward_getp (s, withdraw_len); |
718e3744 | 1438 | } |
1439 | ||
1440 | /* Attribute total length check. */ | |
1441 | if (stream_pnt (s) + 2 > end) | |
1442 | { | |
1443 | zlog_warn ("%s [Error] Packet Error" | |
1444 | " (update packet is short for attribute length)", | |
1445 | peer->host); | |
1446 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1447 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1448 | return -1; | |
1449 | } | |
1450 | ||
1451 | /* Fetch attribute total length. */ | |
1452 | attribute_len = stream_getw (s); | |
1453 | ||
1454 | /* Attribute length check. */ | |
1455 | if (stream_pnt (s) + attribute_len > end) | |
1456 | { | |
1457 | zlog_warn ("%s [Error] Packet Error" | |
1458 | " (update packet attribute length overflow %d)", | |
1459 | peer->host, attribute_len); | |
1460 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1461 | BGP_NOTIFY_UPDATE_MAL_ATTR); | |
1462 | return -1; | |
1463 | } | |
b881c707 PJ |
1464 | |
1465 | /* Certain attribute parsing errors should not be considered bad enough | |
1466 | * to reset the session for, most particularly any partial/optional | |
1467 | * attributes that have 'tunneled' over speakers that don't understand | |
1468 | * them. Instead we withdraw only the prefix concerned. | |
1469 | * | |
1470 | * Complicates the flow a little though.. | |
1471 | */ | |
1472 | bgp_attr_parse_ret_t attr_parse_ret = BGP_ATTR_PARSE_PROCEED; | |
1473 | /* This define morphs the update case into a withdraw when lower levels | |
1474 | * have signalled an error condition where this is best. | |
1475 | */ | |
1476 | #define NLRI_ATTR_ARG (attr_parse_ret != BGP_ATTR_PARSE_WITHDRAW ? &attr : NULL) | |
718e3744 | 1477 | |
1478 | /* Parse attribute when it exists. */ | |
1479 | if (attribute_len) | |
1480 | { | |
b881c707 | 1481 | attr_parse_ret = bgp_attr_parse (peer, &attr, attribute_len, |
96e52474 | 1482 | &nlris[NLRI_MP_UPDATE], &nlris[NLRI_MP_WITHDRAW]); |
b881c707 | 1483 | if (attr_parse_ret == BGP_ATTR_PARSE_ERROR) |
f80f838b DL |
1484 | { |
1485 | bgp_attr_unintern_sub (&attr); | |
1486 | return -1; | |
1487 | } | |
718e3744 | 1488 | } |
b881c707 | 1489 | |
718e3744 | 1490 | /* Logging the attribute. */ |
16286195 DS |
1491 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW || |
1492 | BGP_DEBUG (update, UPDATE_IN) || | |
1493 | BGP_DEBUG (update, UPDATE_PREFIX)) | |
718e3744 | 1494 | { |
9ff63e6f | 1495 | ret = bgp_dump_attr (&attr, peer->rcvd_attr_str, BUFSIZ); |
b881c707 PJ |
1496 | |
1497 | if (attr_parse_ret == BGP_ATTR_PARSE_WITHDRAW) | |
16286195 DS |
1498 | zlog_err ("%s rcvd UPDATE with errors in attr(s)!! Withdrawing route.", |
1499 | peer->host); | |
e01f9cbb | 1500 | |
3f9c7369 | 1501 | if (ret && bgp_debug_update(peer, NULL, NULL, 1)) |
16286195 DS |
1502 | { |
1503 | zlog_debug ("%s rcvd UPDATE w/ attr: %s", peer->host, peer->rcvd_attr_str); | |
1504 | peer->rcvd_attr_printed = 1; | |
1505 | } | |
718e3744 | 1506 | } |
b881c707 | 1507 | |
718e3744 | 1508 | /* Network Layer Reachability Information. */ |
1509 | update_len = end - stream_pnt (s); | |
1510 | ||
1511 | if (update_len) | |
1512 | { | |
48a5452b | 1513 | /* Set NLRI portion to structure. */ |
96e52474 PJ |
1514 | nlris[NLRI_UPDATE].afi = AFI_IP; |
1515 | nlris[NLRI_UPDATE].safi = SAFI_UNICAST; | |
1516 | nlris[NLRI_UPDATE].nlri = stream_pnt (s); | |
1517 | nlris[NLRI_UPDATE].length = update_len; | |
9985f83c | 1518 | stream_forward_getp (s, update_len); |
718e3744 | 1519 | } |
1520 | ||
d889623f | 1521 | if (BGP_DEBUG (update, UPDATE_IN)) |
ebd12e62 PJ |
1522 | zlog_debug("%s rcvd UPDATE wlen %d attrlen %d alen %d", |
1523 | peer->host, withdraw_len, attribute_len, update_len); | |
d889623f | 1524 | |
96e52474 PJ |
1525 | /* Parse any given NLRIs */ |
1526 | for (int i = NLRI_UPDATE; i < NLRI_TYPE_MAX; i++) | |
718e3744 | 1527 | { |
8628876f PJ |
1528 | if (!nlris[i].nlri) |
1529 | continue; | |
1530 | ||
96e52474 PJ |
1531 | /* NLRI is processed iff the peer if configured for the specific afi/safi */ |
1532 | if (!peer->afc[nlris[i].afi][nlris[i].safi]) | |
1533 | { | |
1534 | zlog_info ("%s [Info] UPDATE for non-enabled AFI/SAFI %u/%u", | |
1535 | peer->host, nlris[i].afi, nlris[i].safi); | |
1536 | continue; | |
1537 | } | |
93406d87 | 1538 | |
96e52474 PJ |
1539 | /* EoR handled later */ |
1540 | if (nlris[i].length == 0) | |
1541 | continue; | |
e01f9cbb | 1542 | |
96e52474 PJ |
1543 | switch (i) |
1544 | { | |
1545 | case NLRI_UPDATE: | |
1546 | case NLRI_MP_UPDATE: | |
7ef5a232 | 1547 | nlri_ret = bgp_nlri_parse (peer, NLRI_ATTR_ARG, &nlris[i], 0); |
96e52474 PJ |
1548 | break; |
1549 | case NLRI_WITHDRAW: | |
1550 | case NLRI_MP_WITHDRAW: | |
7ef5a232 | 1551 | nlri_ret = bgp_nlri_parse (peer, &attr, &nlris[i], 1); |
96e52474 PJ |
1552 | break; |
1553 | default: | |
1554 | nlri_ret = -1; | |
1555 | } | |
93406d87 | 1556 | |
96e52474 PJ |
1557 | if (nlri_ret < 0) |
1558 | { | |
1559 | zlog_err("%s [Error] Error parsing NLRI", peer->host); | |
1560 | if (peer->status == Established) | |
1561 | bgp_notify_send (peer, BGP_NOTIFY_UPDATE_ERR, | |
1562 | i <= NLRI_WITHDRAW ? BGP_NOTIFY_UPDATE_INVAL_NETWORK | |
1563 | : BGP_NOTIFY_UPDATE_OPT_ATTR_ERR); | |
1564 | bgp_attr_unintern_sub (&attr); | |
1565 | return -1; | |
1566 | } | |
718e3744 | 1567 | } |
e01f9cbb | 1568 | |
96e52474 PJ |
1569 | /* EoR checks |
1570 | * | |
1571 | * Non-MP IPv4/Unicast EoR is a completely empty UPDATE | |
1572 | * and MP EoR should have only an empty MP_UNREACH | |
1573 | */ | |
1574 | if (!update_len && !withdraw_len | |
1575 | && nlris[NLRI_MP_UPDATE].length == 0) | |
718e3744 | 1576 | { |
96e52474 PJ |
1577 | afi_t afi = 0; |
1578 | safi_t safi; | |
16286195 | 1579 | |
96e52474 PJ |
1580 | /* Non-MP IPv4/Unicast is a completely emtpy UPDATE - already checked |
1581 | * update and withdraw NLRI lengths are 0. | |
1582 | */ | |
1583 | if (!attribute_len) | |
1584 | { | |
1585 | afi = AFI_IP; | |
1586 | safi = SAFI_UNICAST; | |
1587 | } | |
18ef625f | 1588 | else if (attr.flag & ATTR_FLAG_BIT (BGP_ATTR_MP_UNREACH_NLRI) |
9cabb64b | 1589 | && nlris[NLRI_MP_WITHDRAW].length == 0) |
96e52474 PJ |
1590 | { |
1591 | afi = nlris[NLRI_MP_WITHDRAW].afi; | |
1592 | safi = nlris[NLRI_MP_WITHDRAW].safi; | |
1593 | } | |
e01f9cbb | 1594 | |
96e52474 PJ |
1595 | if (afi && peer->afc[afi][safi]) |
1596 | { | |
945c8fe9 | 1597 | /* End-of-RIB received */ |
96e52474 | 1598 | if (!CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_EOR_RECEIVED)) |
945c8fe9 | 1599 | { |
96e52474 PJ |
1600 | SET_FLAG (peer->af_sflags[afi][safi], |
1601 | PEER_STATUS_EOR_RECEIVED); | |
945c8fe9 LB |
1602 | bgp_update_explicit_eors(peer); |
1603 | } | |
1604 | ||
96e52474 PJ |
1605 | /* NSF delete stale route */ |
1606 | if (peer->nsf[afi][safi]) | |
1607 | bgp_clear_stale_route (peer, afi, safi); | |
587ff0fd LB |
1608 | |
1609 | if (bgp_debug_neighbor_events(peer)) | |
587ff0fd | 1610 | { |
96e52474 PJ |
1611 | zlog_debug ("rcvd End-of-RIB for %s from %s", |
1612 | afi_safi_print (afi, safi), peer->host); | |
587ff0fd | 1613 | } |
96e52474 | 1614 | } |
587ff0fd | 1615 | } |
718e3744 | 1616 | |
1617 | /* Everything is done. We unintern temporary structures which | |
1618 | interned in bgp_attr_parse(). */ | |
b881c707 | 1619 | bgp_attr_unintern_sub (&attr); |
489d005a | 1620 | |
718e3744 | 1621 | /* If peering is stopped due to some reason, do not generate BGP |
1622 | event. */ | |
1623 | if (peer->status != Established) | |
1624 | return 0; | |
1625 | ||
1626 | /* Increment packet counter. */ | |
1627 | peer->update_in++; | |
65957886 | 1628 | peer->update_time = bgp_clock (); |
718e3744 | 1629 | |
e2c38e6c | 1630 | /* Rearm holdtime timer */ |
6a4677b7 | 1631 | BGP_TIMER_OFF (peer->t_holdtime); |
e2c38e6c | 1632 | bgp_timer_set (peer); |
718e3744 | 1633 | |
1634 | return 0; | |
1635 | } | |
1636 | ||
1637 | /* Notify message treatment function. */ | |
94f2b392 | 1638 | static void |
718e3744 | 1639 | bgp_notify_receive (struct peer *peer, bgp_size_t size) |
1640 | { | |
1641 | struct bgp_notify bgp_notify; | |
1642 | ||
1643 | if (peer->notify.data) | |
1644 | { | |
1645 | XFREE (MTYPE_TMP, peer->notify.data); | |
1646 | peer->notify.data = NULL; | |
1647 | peer->notify.length = 0; | |
1648 | } | |
1649 | ||
1650 | bgp_notify.code = stream_getc (peer->ibuf); | |
1651 | bgp_notify.subcode = stream_getc (peer->ibuf); | |
1652 | bgp_notify.length = size - 2; | |
1653 | bgp_notify.data = NULL; | |
1654 | ||
1655 | /* Preserv notify code and sub code. */ | |
1656 | peer->notify.code = bgp_notify.code; | |
1657 | peer->notify.subcode = bgp_notify.subcode; | |
1658 | /* For further diagnostic record returned Data. */ | |
1659 | if (bgp_notify.length) | |
1660 | { | |
1661 | peer->notify.length = size - 2; | |
1662 | peer->notify.data = XMALLOC (MTYPE_TMP, size - 2); | |
1663 | memcpy (peer->notify.data, stream_pnt (peer->ibuf), size - 2); | |
1664 | } | |
1665 | ||
1666 | /* For debug */ | |
1667 | { | |
1668 | int i; | |
1669 | int first = 0; | |
1670 | char c[4]; | |
1671 | ||
1672 | if (bgp_notify.length) | |
1673 | { | |
1674 | bgp_notify.data = XMALLOC (MTYPE_TMP, bgp_notify.length * 3); | |
1675 | for (i = 0; i < bgp_notify.length; i++) | |
1676 | if (first) | |
1677 | { | |
1678 | sprintf (c, " %02x", stream_getc (peer->ibuf)); | |
1679 | strcat (bgp_notify.data, c); | |
1680 | } | |
1681 | else | |
1682 | { | |
1683 | first = 1; | |
1684 | sprintf (c, "%02x", stream_getc (peer->ibuf)); | |
1685 | strcpy (bgp_notify.data, c); | |
1686 | } | |
38de8d02 | 1687 | bgp_notify.raw_data = (u_char*)peer->notify.data; |
718e3744 | 1688 | } |
1689 | ||
1690 | bgp_notify_print(peer, &bgp_notify, "received"); | |
1691 | if (bgp_notify.data) | |
9e08021c DW |
1692 | { |
1693 | XFREE (MTYPE_TMP, bgp_notify.data); | |
1694 | bgp_notify.data = NULL; | |
1695 | bgp_notify.length = 0; | |
1696 | } | |
718e3744 | 1697 | } |
1698 | ||
1699 | /* peer count update */ | |
1700 | peer->notify_in++; | |
1701 | ||
3a8c7ba1 | 1702 | peer->last_reset = PEER_DOWN_NOTIFY_RECEIVED; |
e0701b79 | 1703 | |
718e3744 | 1704 | /* We have to check for Notify with Unsupported Optional Parameter. |
1705 | in that case we fallback to open without the capability option. | |
1706 | But this done in bgp_stop. We just mark it here to avoid changing | |
1707 | the fsm tables. */ | |
1708 | if (bgp_notify.code == BGP_NOTIFY_OPEN_ERR && | |
1709 | bgp_notify.subcode == BGP_NOTIFY_OPEN_UNSUP_PARAM ) | |
1710 | UNSET_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN); | |
1711 | ||
718e3744 | 1712 | BGP_EVENT_ADD (peer, Receive_NOTIFICATION_message); |
1713 | } | |
1714 | ||
1715 | /* Keepalive treatment function -- get keepalive send keepalive */ | |
94f2b392 | 1716 | static void |
718e3744 | 1717 | bgp_keepalive_receive (struct peer *peer, bgp_size_t size) |
1718 | { | |
16286195 | 1719 | if (bgp_debug_keepalive(peer)) |
6b51474d | 1720 | zlog_debug ("%s KEEPALIVE rcvd", peer->host); |
718e3744 | 1721 | |
1722 | BGP_EVENT_ADD (peer, Receive_KEEPALIVE_message); | |
1723 | } | |
1724 | ||
1725 | /* Route refresh message is received. */ | |
94f2b392 | 1726 | static void |
718e3744 | 1727 | bgp_route_refresh_receive (struct peer *peer, bgp_size_t size) |
1728 | { | |
b58ed1f8 | 1729 | iana_afi_t pkt_afi; |
718e3744 | 1730 | afi_t afi; |
9cabb64b | 1731 | safi_t pkt_safi, safi; |
718e3744 | 1732 | struct stream *s; |
b3657166 | 1733 | struct peer_af *paf; |
40d2700d DW |
1734 | struct update_group *updgrp; |
1735 | struct peer *updgrp_peer; | |
718e3744 | 1736 | |
1737 | /* If peer does not have the capability, send notification. */ | |
1738 | if (! CHECK_FLAG (peer->cap, PEER_CAP_REFRESH_ADV)) | |
1739 | { | |
16286195 | 1740 | zlog_err ("%s [Error] BGP route refresh is not enabled", |
718e3744 | 1741 | peer->host); |
1742 | bgp_notify_send (peer, | |
1743 | BGP_NOTIFY_HEADER_ERR, | |
1744 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
1745 | return; | |
1746 | } | |
1747 | ||
1748 | /* Status must be Established. */ | |
1749 | if (peer->status != Established) | |
1750 | { | |
16286195 | 1751 | zlog_err ("%s [Error] Route refresh packet received under status %s", |
718e3744 | 1752 | peer->host, LOOKUP (bgp_status_msg, peer->status)); |
1753 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); | |
1754 | return; | |
1755 | } | |
1756 | ||
1757 | s = peer->ibuf; | |
1758 | ||
1759 | /* Parse packet. */ | |
9cabb64b | 1760 | pkt_afi = stream_getw (s); |
ffd0c037 | 1761 | (void)stream_getc (s); |
9cabb64b | 1762 | pkt_safi = stream_getc (s); |
718e3744 | 1763 | |
3f9c7369 | 1764 | if (bgp_debug_update(peer, NULL, NULL, 0)) |
6b51474d | 1765 | zlog_debug ("%s rcvd REFRESH_REQ for afi/safi: %d/%d", |
9cabb64b | 1766 | peer->host, pkt_afi, pkt_safi); |
718e3744 | 1767 | |
9cabb64b | 1768 | /* Convert AFI, SAFI to internal values and check. */ |
1769 | if (bgp_map_afi_safi_iana2int (pkt_afi, pkt_safi, &afi, &safi)) | |
718e3744 | 1770 | { |
16286195 | 1771 | zlog_info ("%s REFRESH_REQ for unrecognized afi/safi: %d/%d - ignored", |
9cabb64b | 1772 | peer->host, pkt_afi, pkt_safi); |
718e3744 | 1773 | return; |
1774 | } | |
1775 | ||
718e3744 | 1776 | if (size != BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) |
1777 | { | |
1778 | u_char *end; | |
1779 | u_char when_to_refresh; | |
1780 | u_char orf_type; | |
1781 | u_int16_t orf_len; | |
1782 | ||
1783 | if (size - (BGP_MSG_ROUTE_REFRESH_MIN_SIZE - BGP_HEADER_SIZE) < 5) | |
1784 | { | |
1785 | zlog_info ("%s ORF route refresh length error", peer->host); | |
1786 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
1787 | return; | |
1788 | } | |
1789 | ||
1790 | when_to_refresh = stream_getc (s); | |
1791 | end = stream_pnt (s) + (size - 5); | |
1792 | ||
370b64a2 | 1793 | while ((stream_pnt (s) + 2) < end) |
718e3744 | 1794 | { |
1795 | orf_type = stream_getc (s); | |
1796 | orf_len = stream_getw (s); | |
370b64a2 PJ |
1797 | |
1798 | /* orf_len in bounds? */ | |
1799 | if ((stream_pnt (s) + orf_len) > end) | |
1800 | break; /* XXX: Notify instead?? */ | |
718e3744 | 1801 | if (orf_type == ORF_TYPE_PREFIX |
1802 | || orf_type == ORF_TYPE_PREFIX_OLD) | |
1803 | { | |
1f9a9fff PJ |
1804 | uint8_t *p_pnt = stream_pnt (s); |
1805 | uint8_t *p_end = stream_pnt (s) + orf_len; | |
718e3744 | 1806 | struct orf_prefix orfp; |
1807 | u_char common = 0; | |
1808 | u_int32_t seq; | |
1809 | int psize; | |
1810 | char name[BUFSIZ]; | |
ffd0c037 | 1811 | int ret = CMD_SUCCESS; |
718e3744 | 1812 | |
16286195 | 1813 | if (bgp_debug_neighbor_events(peer)) |
718e3744 | 1814 | { |
6b51474d | 1815 | zlog_debug ("%s rcvd Prefixlist ORF(%d) length %d", |
718e3744 | 1816 | peer->host, orf_type, orf_len); |
1817 | } | |
1818 | ||
370b64a2 PJ |
1819 | /* we're going to read at least 1 byte of common ORF header, |
1820 | * and 7 bytes of ORF Address-filter entry from the stream | |
1821 | */ | |
1822 | if (orf_len < 7) | |
1823 | break; | |
1824 | ||
718e3744 | 1825 | /* ORF prefix-list name */ |
1826 | sprintf (name, "%s.%d.%d", peer->host, afi, safi); | |
1827 | ||
1828 | while (p_pnt < p_end) | |
1829 | { | |
d64379e8 CH |
1830 | /* If the ORF entry is malformed, want to read as much of it |
1831 | * as possible without going beyond the bounds of the entry, | |
1832 | * to maximise debug information. | |
1833 | */ | |
fdbc8e77 | 1834 | int ok; |
718e3744 | 1835 | memset (&orfp, 0, sizeof (struct orf_prefix)); |
1836 | common = *p_pnt++; | |
d64379e8 | 1837 | /* after ++: p_pnt <= p_end */ |
718e3744 | 1838 | if (common & ORF_COMMON_PART_REMOVE_ALL) |
1839 | { | |
16286195 | 1840 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 1841 | zlog_debug ("%s rcvd Remove-All pfxlist ORF request", peer->host); |
c7da3d50 | 1842 | prefix_bgp_orf_remove_all (afi, name); |
718e3744 | 1843 | break; |
1844 | } | |
ffd0c037 | 1845 | ok = ((u_int32_t)(p_end - p_pnt) >= sizeof(u_int32_t)) ; |
bb915f5f | 1846 | if (ok) |
d64379e8 | 1847 | { |
fdbc8e77 PJ |
1848 | memcpy (&seq, p_pnt, sizeof (u_int32_t)); |
1849 | p_pnt += sizeof (u_int32_t); | |
1850 | orfp.seq = ntohl (seq); | |
d64379e8 CH |
1851 | } |
1852 | else | |
1853 | p_pnt = p_end ; | |
1854 | ||
1855 | if ((ok = (p_pnt < p_end))) | |
1856 | orfp.ge = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */ | |
1857 | if ((ok = (p_pnt < p_end))) | |
1858 | orfp.le = *p_pnt++ ; /* value checked in prefix_bgp_orf_set() */ | |
1859 | if ((ok = (p_pnt < p_end))) | |
1860 | orfp.p.prefixlen = *p_pnt++ ; | |
1861 | orfp.p.family = afi2family (afi); /* afi checked already */ | |
1862 | ||
1863 | psize = PSIZE (orfp.p.prefixlen); /* 0 if not ok */ | |
1864 | if (psize > prefix_blen(&orfp.p)) /* valid for family ? */ | |
1865 | { | |
1866 | ok = 0 ; | |
1867 | psize = prefix_blen(&orfp.p) ; | |
1868 | } | |
1869 | if (psize > (p_end - p_pnt)) /* valid for packet ? */ | |
1870 | { | |
1871 | ok = 0 ; | |
1872 | psize = p_end - p_pnt ; | |
1873 | } | |
1874 | ||
1875 | if (psize > 0) | |
1876 | memcpy (&orfp.p.u.prefix, p_pnt, psize); | |
718e3744 | 1877 | p_pnt += psize; |
1878 | ||
16286195 | 1879 | if (bgp_debug_neighbor_events(peer)) |
14542f3e JBD |
1880 | { |
1881 | char buf[INET6_BUFSIZ]; | |
1882 | ||
1883 | zlog_debug ("%s rcvd %s %s seq %u %s/%d ge %d le %d%s", | |
1884 | peer->host, | |
1885 | (common & ORF_COMMON_PART_REMOVE ? "Remove" : "Add"), | |
1886 | (common & ORF_COMMON_PART_DENY ? "deny" : "permit"), | |
1887 | orfp.seq, | |
1888 | inet_ntop (orfp.p.family, &orfp.p.u.prefix, buf, INET6_BUFSIZ), | |
1889 | orfp.p.prefixlen, orfp.ge, orfp.le, | |
1890 | ok ? "" : " MALFORMED"); | |
1891 | } | |
1892 | ||
d64379e8 | 1893 | if (ok) |
fdbc8e77 PJ |
1894 | ret = prefix_bgp_orf_set (name, afi, &orfp, |
1895 | (common & ORF_COMMON_PART_DENY ? 0 : 1 ), | |
1896 | (common & ORF_COMMON_PART_REMOVE ? 0 : 1)); | |
718e3744 | 1897 | |
1f9a9fff | 1898 | if (!ok || (ok && ret != CMD_SUCCESS)) |
718e3744 | 1899 | { |
16286195 DS |
1900 | zlog_info ("%s Received misformatted prefixlist ORF." |
1901 | " Remove All pfxlist", peer->host); | |
c7da3d50 | 1902 | prefix_bgp_orf_remove_all (afi, name); |
718e3744 | 1903 | break; |
1904 | } | |
1905 | } | |
40d2700d DW |
1906 | |
1907 | peer->orf_plist[afi][safi] = prefix_bgp_orf_lookup (afi, name); | |
718e3744 | 1908 | } |
9985f83c | 1909 | stream_forward_getp (s, orf_len); |
718e3744 | 1910 | } |
16286195 | 1911 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 1912 | zlog_debug ("%s rcvd Refresh %s ORF request", peer->host, |
718e3744 | 1913 | when_to_refresh == REFRESH_DEFER ? "Defer" : "Immediate"); |
1914 | if (when_to_refresh == REFRESH_DEFER) | |
1915 | return; | |
1916 | } | |
1917 | ||
1918 | /* First update is deferred until ORF or ROUTE-REFRESH is received */ | |
1919 | if (CHECK_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH)) | |
1920 | UNSET_FLAG (peer->af_sflags[afi][safi], PEER_STATUS_ORF_WAIT_REFRESH); | |
1921 | ||
b3657166 | 1922 | paf = peer_af_find (peer, afi, safi); |
40d2700d DW |
1923 | if (paf && paf->subgroup) |
1924 | { | |
1925 | if (peer->orf_plist[afi][safi]) | |
1926 | { | |
1927 | updgrp = PAF_UPDGRP(paf); | |
1928 | updgrp_peer = UPDGRP_PEER(updgrp); | |
1929 | updgrp_peer->orf_plist[afi][safi] = peer->orf_plist[afi][safi]; | |
1930 | } | |
1931 | ||
1932 | /* If the peer is configured for default-originate clear the | |
1933 | * SUBGRP_STATUS_DEFAULT_ORIGINATE flag so that we will re-advertise the | |
1934 | * default | |
1935 | */ | |
1936 | if (CHECK_FLAG (paf->subgroup->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE)) | |
1937 | UNSET_FLAG (paf->subgroup->sflags, SUBGRP_STATUS_DEFAULT_ORIGINATE); | |
1938 | } | |
b3657166 | 1939 | |
718e3744 | 1940 | /* Perform route refreshment to the peer */ |
1941 | bgp_announce_route (peer, afi, safi); | |
1942 | } | |
1943 | ||
94f2b392 | 1944 | static int |
718e3744 | 1945 | bgp_capability_msg_parse (struct peer *peer, u_char *pnt, bgp_size_t length) |
1946 | { | |
1947 | u_char *end; | |
6d58272b PJ |
1948 | struct capability_mp_data mpc; |
1949 | struct capability_header *hdr; | |
718e3744 | 1950 | u_char action; |
b58ed1f8 | 1951 | iana_afi_t pkt_afi; |
718e3744 | 1952 | afi_t afi; |
9cabb64b | 1953 | safi_t pkt_safi, safi; |
718e3744 | 1954 | |
718e3744 | 1955 | end = pnt + length; |
1956 | ||
1957 | while (pnt < end) | |
6d58272b | 1958 | { |
718e3744 | 1959 | /* We need at least action, capability code and capability length. */ |
1960 | if (pnt + 3 > end) | |
1961 | { | |
1962 | zlog_info ("%s Capability length error", peer->host); | |
1963 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
1964 | return -1; | |
1965 | } | |
718e3744 | 1966 | action = *pnt; |
6d58272b PJ |
1967 | hdr = (struct capability_header *)(pnt + 1); |
1968 | ||
718e3744 | 1969 | /* Action value check. */ |
1970 | if (action != CAPABILITY_ACTION_SET | |
1971 | && action != CAPABILITY_ACTION_UNSET) | |
1972 | { | |
1973 | zlog_info ("%s Capability Action Value error %d", | |
1974 | peer->host, action); | |
1975 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
1976 | return -1; | |
1977 | } | |
1978 | ||
16286195 | 1979 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 1980 | zlog_debug ("%s CAPABILITY has action: %d, code: %u, length %u", |
6d58272b | 1981 | peer->host, action, hdr->code, hdr->length); |
718e3744 | 1982 | |
1983 | /* Capability length check. */ | |
6d58272b | 1984 | if ((pnt + hdr->length + 3) > end) |
718e3744 | 1985 | { |
1986 | zlog_info ("%s Capability length error", peer->host); | |
1987 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); | |
1988 | return -1; | |
1989 | } | |
1990 | ||
6d58272b PJ |
1991 | /* Fetch structure to the byte stream. */ |
1992 | memcpy (&mpc, pnt + 3, sizeof (struct capability_mp_data)); | |
1993 | ||
718e3744 | 1994 | /* We know MP Capability Code. */ |
6d58272b | 1995 | if (hdr->code == CAPABILITY_CODE_MP) |
718e3744 | 1996 | { |
9cabb64b | 1997 | pkt_afi = ntohs (mpc.afi); |
1998 | pkt_safi = mpc.safi; | |
718e3744 | 1999 | |
2000 | /* Ignore capability when override-capability is set. */ | |
2001 | if (CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) | |
2002 | continue; | |
6d58272b | 2003 | |
9cabb64b | 2004 | /* Convert AFI, SAFI to internal values. */ |
2005 | if (bgp_map_afi_safi_iana2int (pkt_afi, pkt_safi, &afi, &safi)) | |
6d58272b | 2006 | { |
16286195 | 2007 | if (bgp_debug_neighbor_events(peer)) |
0b2aa3a0 | 2008 | zlog_debug ("%s Dynamic Capability MP_EXT afi/safi invalid " |
9cabb64b | 2009 | "(%u/%u)", peer->host, pkt_afi, pkt_safi); |
6d58272b PJ |
2010 | continue; |
2011 | } | |
2012 | ||
718e3744 | 2013 | /* Address family check. */ |
16286195 | 2014 | if (bgp_debug_neighbor_events(peer)) |
6d58272b PJ |
2015 | zlog_debug ("%s CAPABILITY has %s MP_EXT CAP for afi/safi: %u/%u", |
2016 | peer->host, | |
2017 | action == CAPABILITY_ACTION_SET | |
2018 | ? "Advertising" : "Removing", | |
9cabb64b | 2019 | pkt_afi, pkt_safi); |
6d58272b PJ |
2020 | |
2021 | if (action == CAPABILITY_ACTION_SET) | |
2022 | { | |
2023 | peer->afc_recv[afi][safi] = 1; | |
2024 | if (peer->afc[afi][safi]) | |
2025 | { | |
2026 | peer->afc_nego[afi][safi] = 1; | |
2027 | bgp_announce_route (peer, afi, safi); | |
2028 | } | |
2029 | } | |
2030 | else | |
2031 | { | |
2032 | peer->afc_recv[afi][safi] = 0; | |
2033 | peer->afc_nego[afi][safi] = 0; | |
2034 | ||
2035 | if (peer_active_nego (peer)) | |
2a3d5731 | 2036 | bgp_clear_route (peer, afi, safi); |
6d58272b PJ |
2037 | else |
2038 | BGP_EVENT_ADD (peer, BGP_Stop); | |
2039 | } | |
718e3744 | 2040 | } |
718e3744 | 2041 | else |
2042 | { | |
2043 | zlog_warn ("%s unrecognized capability code: %d - ignored", | |
6d58272b | 2044 | peer->host, hdr->code); |
718e3744 | 2045 | } |
6d58272b | 2046 | pnt += hdr->length + 3; |
718e3744 | 2047 | } |
2048 | return 0; | |
2049 | } | |
2050 | ||
01b7ce2d PJ |
2051 | /* Dynamic Capability is received. |
2052 | * | |
2053 | * This is exported for unit-test purposes | |
2054 | */ | |
6d58272b | 2055 | int |
718e3744 | 2056 | bgp_capability_receive (struct peer *peer, bgp_size_t size) |
2057 | { | |
2058 | u_char *pnt; | |
718e3744 | 2059 | |
2060 | /* Fetch pointer. */ | |
2061 | pnt = stream_pnt (peer->ibuf); | |
2062 | ||
16286195 | 2063 | if (bgp_debug_neighbor_events(peer)) |
6b51474d | 2064 | zlog_debug ("%s rcv CAPABILITY", peer->host); |
718e3744 | 2065 | |
2066 | /* If peer does not have the capability, send notification. */ | |
2067 | if (! CHECK_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV)) | |
2068 | { | |
16286195 | 2069 | zlog_err ("%s [Error] BGP dynamic capability is not enabled", |
718e3744 | 2070 | peer->host); |
2071 | bgp_notify_send (peer, | |
2072 | BGP_NOTIFY_HEADER_ERR, | |
2073 | BGP_NOTIFY_HEADER_BAD_MESTYPE); | |
0b2aa3a0 | 2074 | return -1; |
718e3744 | 2075 | } |
2076 | ||
2077 | /* Status must be Established. */ | |
2078 | if (peer->status != Established) | |
2079 | { | |
16286195 DS |
2080 | zlog_err ("%s [Error] Dynamic capability packet received under status %s", |
2081 | peer->host, LOOKUP (bgp_status_msg, peer->status)); | |
718e3744 | 2082 | bgp_notify_send (peer, BGP_NOTIFY_FSM_ERR, 0); |
0b2aa3a0 | 2083 | return -1; |
718e3744 | 2084 | } |
2085 | ||
2086 | /* Parse packet. */ | |
6d58272b | 2087 | return bgp_capability_msg_parse (peer, pnt, size); |
718e3744 | 2088 | } |
6b0655a2 | 2089 | |
718e3744 | 2090 | /* BGP read utility function. */ |
94f2b392 | 2091 | static int |
718e3744 | 2092 | bgp_read_packet (struct peer *peer) |
2093 | { | |
2094 | int nbytes; | |
2095 | int readsize; | |
2096 | ||
9985f83c | 2097 | readsize = peer->packet_size - stream_get_endp (peer->ibuf); |
718e3744 | 2098 | |
2099 | /* If size is zero then return. */ | |
2100 | if (! readsize) | |
2101 | return 0; | |
2102 | ||
2103 | /* Read packet from fd. */ | |
35398589 | 2104 | nbytes = stream_read_try (peer->ibuf, peer->fd, readsize); |
718e3744 | 2105 | |
2106 | /* If read byte is smaller than zero then error occured. */ | |
2107 | if (nbytes < 0) | |
2108 | { | |
35398589 SH |
2109 | /* Transient error should retry */ |
2110 | if (nbytes == -2) | |
718e3744 | 2111 | return -1; |
2112 | ||
16286195 DS |
2113 | zlog_err ("%s [Error] bgp_read_packet error: %s", |
2114 | peer->host, safe_strerror (errno)); | |
93406d87 | 2115 | |
2116 | if (peer->status == Established) | |
2117 | { | |
2118 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE)) | |
2119 | { | |
2120 | peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION; | |
2121 | SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT); | |
2122 | } | |
2123 | else | |
2124 | peer->last_reset = PEER_DOWN_CLOSE_SESSION; | |
2125 | } | |
2126 | ||
718e3744 | 2127 | BGP_EVENT_ADD (peer, TCP_fatal_error); |
2128 | return -1; | |
2129 | } | |
2130 | ||
2131 | /* When read byte is zero : clear bgp peer and return */ | |
2132 | if (nbytes == 0) | |
2133 | { | |
16286195 DS |
2134 | if (bgp_debug_neighbor_events(peer)) |
2135 | zlog_debug ("%s [Event] BGP connection closed fd %d", | |
2136 | peer->host, peer->fd); | |
e0701b79 | 2137 | |
2138 | if (peer->status == Established) | |
93406d87 | 2139 | { |
2140 | if (CHECK_FLAG (peer->sflags, PEER_STATUS_NSF_MODE)) | |
2141 | { | |
2142 | peer->last_reset = PEER_DOWN_NSF_CLOSE_SESSION; | |
2143 | SET_FLAG (peer->sflags, PEER_STATUS_NSF_WAIT); | |
2144 | } | |
2145 | else | |
2146 | peer->last_reset = PEER_DOWN_CLOSE_SESSION; | |
2147 | } | |
e0701b79 | 2148 | |
718e3744 | 2149 | BGP_EVENT_ADD (peer, TCP_connection_closed); |
2150 | return -1; | |
2151 | } | |
2152 | ||
2153 | /* We read partial packet. */ | |
9985f83c | 2154 | if (stream_get_endp (peer->ibuf) != peer->packet_size) |
718e3744 | 2155 | return -1; |
2156 | ||
2157 | return 0; | |
2158 | } | |
2159 | ||
2160 | /* Marker check. */ | |
94f2b392 | 2161 | static int |
718e3744 | 2162 | bgp_marker_all_one (struct stream *s, int length) |
2163 | { | |
2164 | int i; | |
2165 | ||
2166 | for (i = 0; i < length; i++) | |
2167 | if (s->data[i] != 0xff) | |
2168 | return 0; | |
2169 | ||
2170 | return 1; | |
2171 | } | |
2172 | ||
2173 | /* Starting point of packet process function. */ | |
2174 | int | |
2175 | bgp_read (struct thread *thread) | |
2176 | { | |
2177 | int ret; | |
2178 | u_char type = 0; | |
2179 | struct peer *peer; | |
2180 | bgp_size_t size; | |
2181 | char notify_data_length[2]; | |
d6661008 | 2182 | u_int32_t notify_out; |
718e3744 | 2183 | |
2184 | /* Yes first of all get peer pointer. */ | |
2185 | peer = THREAD_ARG (thread); | |
2186 | peer->t_read = NULL; | |
2187 | ||
d6661008 DS |
2188 | /* Note notify_out so we can check later to see if we sent another one */ |
2189 | notify_out = peer->notify_out; | |
2190 | ||
718e3744 | 2191 | /* For non-blocking IO check. */ |
2192 | if (peer->status == Connect) | |
2193 | { | |
fc9a856f | 2194 | bgp_connect_check (peer, 1); |
718e3744 | 2195 | goto done; |
2196 | } | |
2197 | else | |
2198 | { | |
eb821189 | 2199 | if (peer->fd < 0) |
718e3744 | 2200 | { |
eb821189 | 2201 | zlog_err ("bgp_read peer's fd is negative value %d", peer->fd); |
718e3744 | 2202 | return -1; |
2203 | } | |
eb821189 | 2204 | BGP_READ_ON (peer->t_read, bgp_read, peer->fd); |
718e3744 | 2205 | } |
2206 | ||
2207 | /* Read packet header to determine type of the packet */ | |
2208 | if (peer->packet_size == 0) | |
2209 | peer->packet_size = BGP_HEADER_SIZE; | |
2210 | ||
9985f83c | 2211 | if (stream_get_endp (peer->ibuf) < BGP_HEADER_SIZE) |
718e3744 | 2212 | { |
2213 | ret = bgp_read_packet (peer); | |
2214 | ||
2215 | /* Header read error or partial read packet. */ | |
2216 | if (ret < 0) | |
2217 | goto done; | |
2218 | ||
2219 | /* Get size and type. */ | |
9985f83c | 2220 | stream_forward_getp (peer->ibuf, BGP_MARKER_SIZE); |
718e3744 | 2221 | memcpy (notify_data_length, stream_pnt (peer->ibuf), 2); |
2222 | size = stream_getw (peer->ibuf); | |
2223 | type = stream_getc (peer->ibuf); | |
2224 | ||
718e3744 | 2225 | /* Marker check */ |
f5ba3874 | 2226 | if (((type == BGP_MSG_OPEN) || (type == BGP_MSG_KEEPALIVE)) |
718e3744 | 2227 | && ! bgp_marker_all_one (peer->ibuf, BGP_MARKER_SIZE)) |
2228 | { | |
2229 | bgp_notify_send (peer, | |
2230 | BGP_NOTIFY_HEADER_ERR, | |
2231 | BGP_NOTIFY_HEADER_NOT_SYNC); | |
2232 | goto done; | |
2233 | } | |
2234 | ||
2235 | /* BGP type check. */ | |
2236 | if (type != BGP_MSG_OPEN && type != BGP_MSG_UPDATE | |
2237 | && type != BGP_MSG_NOTIFY && type != BGP_MSG_KEEPALIVE | |
2238 | && type != BGP_MSG_ROUTE_REFRESH_NEW | |
2239 | && type != BGP_MSG_ROUTE_REFRESH_OLD | |
2240 | && type != BGP_MSG_CAPABILITY) | |
2241 | { | |
16286195 DS |
2242 | if (bgp_debug_neighbor_events(peer)) |
2243 | zlog_debug ("%s unknown message type 0x%02x", | |
2244 | peer->host, type); | |
718e3744 | 2245 | bgp_notify_send_with_data (peer, |
2246 | BGP_NOTIFY_HEADER_ERR, | |
2247 | BGP_NOTIFY_HEADER_BAD_MESTYPE, | |
2248 | &type, 1); | |
2249 | goto done; | |
2250 | } | |
2251 | /* Mimimum packet length check. */ | |
2252 | if ((size < BGP_HEADER_SIZE) | |
2253 | || (size > BGP_MAX_PACKET_SIZE) | |
2254 | || (type == BGP_MSG_OPEN && size < BGP_MSG_OPEN_MIN_SIZE) | |
2255 | || (type == BGP_MSG_UPDATE && size < BGP_MSG_UPDATE_MIN_SIZE) | |
2256 | || (type == BGP_MSG_NOTIFY && size < BGP_MSG_NOTIFY_MIN_SIZE) | |
2257 | || (type == BGP_MSG_KEEPALIVE && size != BGP_MSG_KEEPALIVE_MIN_SIZE) | |
2258 | || (type == BGP_MSG_ROUTE_REFRESH_NEW && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) | |
2259 | || (type == BGP_MSG_ROUTE_REFRESH_OLD && size < BGP_MSG_ROUTE_REFRESH_MIN_SIZE) | |
2260 | || (type == BGP_MSG_CAPABILITY && size < BGP_MSG_CAPABILITY_MIN_SIZE)) | |
2261 | { | |
16286195 DS |
2262 | if (bgp_debug_neighbor_events(peer)) |
2263 | zlog_debug ("%s bad message length - %d for %s", | |
2264 | peer->host, size, | |
2265 | type == 128 ? "ROUTE-REFRESH" : | |
2266 | bgp_type_str[(int) type]); | |
718e3744 | 2267 | bgp_notify_send_with_data (peer, |
2268 | BGP_NOTIFY_HEADER_ERR, | |
2269 | BGP_NOTIFY_HEADER_BAD_MESLEN, | |
c9e52be3 | 2270 | (u_char *) notify_data_length, 2); |
718e3744 | 2271 | goto done; |
2272 | } | |
2273 | ||
2274 | /* Adjust size to message length. */ | |
2275 | peer->packet_size = size; | |
2276 | } | |
2277 | ||
2278 | ret = bgp_read_packet (peer); | |
2279 | if (ret < 0) | |
2280 | goto done; | |
2281 | ||
2282 | /* Get size and type again. */ | |
18f1dc06 | 2283 | (void)stream_getw_from (peer->ibuf, BGP_MARKER_SIZE); |
718e3744 | 2284 | type = stream_getc_from (peer->ibuf, BGP_MARKER_SIZE + 2); |
2285 | ||
2286 | /* BGP packet dump function. */ | |
2287 | bgp_dump_packet (peer, type, peer->ibuf); | |
2288 | ||
2289 | size = (peer->packet_size - BGP_HEADER_SIZE); | |
2290 | ||
2291 | /* Read rest of the packet and call each sort of packet routine */ | |
2292 | switch (type) | |
2293 | { | |
2294 | case BGP_MSG_OPEN: | |
2295 | peer->open_in++; | |
f5ba3874 | 2296 | bgp_open_receive (peer, size); /* XXX return value ignored! */ |
718e3744 | 2297 | break; |
2298 | case BGP_MSG_UPDATE: | |
6ced0e7f | 2299 | peer->readtime = monotime (NULL); |
718e3744 | 2300 | bgp_update_receive (peer, size); |
2301 | break; | |
2302 | case BGP_MSG_NOTIFY: | |
2303 | bgp_notify_receive (peer, size); | |
2304 | break; | |
2305 | case BGP_MSG_KEEPALIVE: | |
6ced0e7f | 2306 | peer->readtime = monotime (NULL); |
718e3744 | 2307 | bgp_keepalive_receive (peer, size); |
2308 | break; | |
2309 | case BGP_MSG_ROUTE_REFRESH_NEW: | |
2310 | case BGP_MSG_ROUTE_REFRESH_OLD: | |
2311 | peer->refresh_in++; | |
2312 | bgp_route_refresh_receive (peer, size); | |
2313 | break; | |
2314 | case BGP_MSG_CAPABILITY: | |
2315 | peer->dynamic_cap_in++; | |
2316 | bgp_capability_receive (peer, size); | |
2317 | break; | |
2318 | } | |
2319 | ||
d6661008 DS |
2320 | /* If reading this packet caused us to send a NOTIFICATION then store a copy |
2321 | * of the packet for troubleshooting purposes | |
2322 | */ | |
2323 | if (notify_out < peer->notify_out) | |
2324 | { | |
2325 | memcpy(peer->last_reset_cause, peer->ibuf->data, peer->packet_size); | |
2326 | peer->last_reset_cause_size = peer->packet_size; | |
2327 | notify_out = peer->notify_out; | |
2328 | } | |
2329 | ||
718e3744 | 2330 | /* Clear input buffer. */ |
2331 | peer->packet_size = 0; | |
2332 | if (peer->ibuf) | |
2333 | stream_reset (peer->ibuf); | |
2334 | ||
2335 | done: | |
d6661008 DS |
2336 | /* If reading this packet caused us to send a NOTIFICATION then store a copy |
2337 | * of the packet for troubleshooting purposes | |
2338 | */ | |
2339 | if (notify_out < peer->notify_out) | |
2340 | { | |
2341 | memcpy(peer->last_reset_cause, peer->ibuf->data, peer->packet_size); | |
2342 | peer->last_reset_cause_size = peer->packet_size; | |
2343 | } | |
2344 | ||
718e3744 | 2345 | return 0; |
2346 | } |