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