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