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