1 /* BGP open message handling
2 Copyright (C) 1998, 1999 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
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
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.
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
32 #include "bgpd/bgpd.h"
33 #include "bgpd/bgp_attr.h"
34 #include "bgpd/bgp_debug.h"
35 #include "bgpd/bgp_fsm.h"
36 #include "bgpd/bgp_packet.h"
37 #include "bgpd/bgp_open.h"
38 #include "bgpd/bgp_aspath.h"
39 #include "bgpd/bgp_vty.h"
41 /* BGP-4 Multiprotocol Extentions lead us to the complex world. We can
42 negotiate remote peer supports extentions or not. But if
43 remote-peer doesn't supports negotiation process itself. We would
44 like to do manual configuration.
46 So there is many configurable point. First of all we want set each
47 peer whether we send capability negotiation to the peer or not.
48 Next, if we send capability to the peer we want to set my capabilty
49 inforation at each peer. */
52 bgp_capability_vty_out (struct vty *vty, struct peer *peer)
56 struct capability_mp_data mpc;
57 struct capability_header *hdr;
59 pnt = peer->notify.data;
60 end = pnt + peer->notify.length;
64 if (pnt + sizeof (struct capability_mp_data) + 2 > end)
67 hdr = (struct capability_header *)pnt;
68 if (pnt + hdr->length + 2 > end)
71 memcpy (&mpc, pnt + 2, sizeof(struct capability_mp_data));
73 if (hdr->code == CAPABILITY_CODE_MP)
75 vty_out (vty, " Capability error for: Multi protocol ");
77 switch (ntohs (mpc.afi))
80 vty_out (vty, "AFI IPv4, ");
83 vty_out (vty, "AFI IPv6, ");
86 vty_out (vty, "AFI Unknown %d, ", ntohs (mpc.afi));
92 vty_out (vty, "SAFI Unicast");
95 vty_out (vty, "SAFI Multicast");
97 case SAFI_MPLS_LABELED_VPN:
98 vty_out (vty, "SAFI MPLS-labeled VPN");
101 vty_out (vty, "SAFI ENCAP");
104 vty_out (vty, "SAFI Unknown %d ", mpc.safi);
107 vty_out (vty, "%s", VTY_NEWLINE);
109 else if (hdr->code >= 128)
110 vty_out (vty, " Capability error: vendor specific capability code %d",
113 vty_out (vty, " Capability error: unknown capability code %d",
116 pnt += hdr->length + 2;
121 bgp_capability_mp_data (struct stream *s, struct capability_mp_data *mpc)
123 afi_t afi = stream_getw (s);
124 memcpy(&mpc->afi, &afi, sizeof(mpc->afi));
125 mpc->reserved = stream_getc (s);
126 mpc->safi = stream_getc (s);
130 bgp_afi_safi_valid_indices (afi_t afi, safi_t *safi)
138 /* BGP MPLS-labeled VPN SAFI isn't contigious with others, remap */
139 case SAFI_MPLS_LABELED_VPN:
140 *safi = SAFI_MPLS_VPN;
152 zlog_debug ("unknown afi/safi (%u/%u)", afi, *safi);
157 /* Set negotiated capability value. */
159 bgp_capability_mp (struct peer *peer, struct capability_header *hdr)
161 struct capability_mp_data mpc;
162 struct stream *s = BGP_INPUT (peer);
164 bgp_capability_mp_data (s, &mpc);
166 if (BGP_DEBUG (normal, NORMAL))
167 zlog_debug ("%s OPEN has MP_EXT CAP for afi/safi: %u/%u",
168 peer->host, mpc.afi, mpc.safi);
170 if (!bgp_afi_safi_valid_indices (mpc.afi, &mpc.safi))
173 /* Now safi remapped, and afi/safi are valid array indices */
174 peer->afc_recv[mpc.afi][mpc.safi] = 1;
176 if (peer->afc[mpc.afi][mpc.safi])
177 peer->afc_nego[mpc.afi][mpc.safi] = 1;
185 bgp_capability_orf_not_support (struct peer *peer, afi_t afi, safi_t safi,
186 u_char type, u_char mode)
188 if (BGP_DEBUG (normal, NORMAL))
189 zlog_debug ("%s Addr-family %d/%d has ORF type/mode %d/%d not supported",
190 peer->host, afi, safi, type, mode);
193 static const struct message orf_type_str[] =
195 { ORF_TYPE_PREFIX, "Prefixlist" },
196 { ORF_TYPE_PREFIX_OLD, "Prefixlist (old)" },
198 static const int orf_type_str_max = array_size(orf_type_str);
200 static const struct message orf_mode_str[] =
202 { ORF_MODE_RECEIVE, "Receive" },
203 { ORF_MODE_SEND, "Send" },
204 { ORF_MODE_BOTH, "Both" },
206 static const int orf_mode_str_max = array_size(orf_mode_str);
209 bgp_capability_orf_entry (struct peer *peer, struct capability_header *hdr)
211 struct stream *s = BGP_INPUT (peer);
212 struct capability_orf_entry entry;
217 u_int16_t sm_cap = 0; /* capability send-mode receive */
218 u_int16_t rm_cap = 0; /* capability receive-mode receive */
221 /* ORF Entry header */
222 bgp_capability_mp_data (s, &entry.mpc);
223 entry.num = stream_getc (s);
225 safi = entry.mpc.safi;
227 if (BGP_DEBUG (normal, NORMAL))
228 zlog_debug ("%s ORF Cap entry for afi/safi: %u/%u",
229 peer->host, entry.mpc.afi, entry.mpc.safi);
231 /* Check AFI and SAFI. */
232 if (!bgp_afi_safi_valid_indices (entry.mpc.afi, &safi))
234 zlog_info ("%s Addr-family %d/%d not supported."
235 " Ignoring the ORF capability",
236 peer->host, entry.mpc.afi, entry.mpc.safi);
240 /* validate number field */
241 if (CAPABILITY_CODE_ORF_LEN + (entry.num * 2) > hdr->length)
243 zlog_info ("%s ORF Capability entry length error,"
244 " Cap length %u, num %u",
245 peer->host, hdr->length, entry.num);
246 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, BGP_NOTIFY_OPEN_UNSPECIFIC);
250 for (i = 0 ; i < entry.num ; i++)
252 type = stream_getc(s);
253 mode = stream_getc(s);
255 /* ORF Mode error check */
260 case ORF_MODE_RECEIVE:
263 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
266 /* ORF Type and afi/safi error checks */
267 /* capcode versus type */
270 case CAPABILITY_CODE_ORF:
273 case ORF_TYPE_PREFIX:
276 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
280 case CAPABILITY_CODE_ORF_OLD:
283 case ORF_TYPE_PREFIX_OLD:
286 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
291 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
296 if (!((afi == AFI_IP && safi == SAFI_UNICAST)
297 || (afi == AFI_IP && safi == SAFI_MULTICAST)
298 || (afi == AFI_IP6 && safi == SAFI_UNICAST)))
300 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
304 if (BGP_DEBUG (normal, NORMAL))
305 zlog_debug ("%s OPEN has %s ORF capability"
306 " as %s for afi/safi: %d/%d",
307 peer->host, LOOKUP (orf_type_str, type),
308 LOOKUP (orf_mode_str, mode),
309 entry.mpc.afi, safi);
311 if (hdr->code == CAPABILITY_CODE_ORF)
313 sm_cap = PEER_CAP_ORF_PREFIX_SM_RCV;
314 rm_cap = PEER_CAP_ORF_PREFIX_RM_RCV;
316 else if (hdr->code == CAPABILITY_CODE_ORF_OLD)
318 sm_cap = PEER_CAP_ORF_PREFIX_SM_OLD_RCV;
319 rm_cap = PEER_CAP_ORF_PREFIX_RM_OLD_RCV;
323 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
330 SET_FLAG (peer->af_cap[afi][safi], sm_cap);
331 SET_FLAG (peer->af_cap[afi][safi], rm_cap);
334 SET_FLAG (peer->af_cap[afi][safi], sm_cap);
336 case ORF_MODE_RECEIVE:
337 SET_FLAG (peer->af_cap[afi][safi], rm_cap);
345 bgp_capability_restart (struct peer *peer, struct capability_header *caphdr)
347 struct stream *s = BGP_INPUT (peer);
348 u_int16_t restart_flag_time;
349 size_t end = stream_get_getp (s) + caphdr->length;
351 SET_FLAG (peer->cap, PEER_CAP_RESTART_RCV);
352 restart_flag_time = stream_getw(s);
353 if (CHECK_FLAG (restart_flag_time, RESTART_R_BIT))
354 SET_FLAG (peer->cap, PEER_CAP_RESTART_BIT_RCV);
356 UNSET_FLAG (restart_flag_time, 0xF000);
357 peer->v_gr_restart = restart_flag_time;
359 if (BGP_DEBUG (normal, NORMAL))
361 zlog_debug ("%s OPEN has Graceful Restart capability", peer->host);
362 zlog_debug ("%s Peer has%srestarted. Restart Time : %d",
364 CHECK_FLAG (peer->cap, PEER_CAP_RESTART_BIT_RCV) ? " "
369 while (stream_get_getp (s) + 4 <= end)
371 afi_t afi = stream_getw (s);
372 safi_t safi = stream_getc (s);
373 u_char flag = stream_getc (s);
375 if (!bgp_afi_safi_valid_indices (afi, &safi))
377 if (BGP_DEBUG (normal, NORMAL))
378 zlog_debug ("%s Addr-family %d/%d(afi/safi) not supported."
379 " Ignore the Graceful Restart capability",
380 peer->host, afi, safi);
382 else if (!peer->afc[afi][safi])
384 if (BGP_DEBUG (normal, NORMAL))
385 zlog_debug ("%s Addr-family %d/%d(afi/safi) not enabled."
386 " Ignore the Graceful Restart capability",
387 peer->host, afi, safi);
391 if (BGP_DEBUG (normal, NORMAL))
392 zlog_debug ("%s Address family %s is%spreserved", peer->host,
393 afi_safi_print (afi, safi),
394 CHECK_FLAG (peer->af_cap[afi][safi],
395 PEER_CAP_RESTART_AF_PRESERVE_RCV)
398 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV);
399 if (CHECK_FLAG (flag, RESTART_F_BIT))
400 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV);
408 bgp_capability_as4 (struct peer *peer, struct capability_header *hdr)
410 SET_FLAG (peer->cap, PEER_CAP_AS4_RCV);
412 if (hdr->length != CAPABILITY_CODE_AS4_LEN)
414 zlog_err ("%s AS4 capability has incorrect data length %d",
415 peer->host, hdr->length);
419 as_t as4 = stream_getl (BGP_INPUT(peer));
421 if (BGP_DEBUG (as4, AS4))
422 zlog_debug ("%s [AS4] about to set cap PEER_CAP_AS4_RCV, got as4 %u",
427 static const struct message capcode_str[] =
429 { CAPABILITY_CODE_MP, "MultiProtocol Extensions" },
430 { CAPABILITY_CODE_REFRESH, "Route Refresh" },
431 { CAPABILITY_CODE_ORF, "Cooperative Route Filtering" },
432 { CAPABILITY_CODE_RESTART, "Graceful Restart" },
433 { CAPABILITY_CODE_AS4, "4-octet AS number" },
434 { CAPABILITY_CODE_DYNAMIC, "Dynamic" },
435 { CAPABILITY_CODE_REFRESH_OLD, "Route Refresh (Old)" },
436 { CAPABILITY_CODE_ORF_OLD, "ORF (Old)" },
438 static const int capcode_str_max = array_size(capcode_str);
440 /* Minimum sizes for length field of each cap (so not inc. the header) */
441 static const size_t cap_minsizes[] =
443 [CAPABILITY_CODE_MP] = CAPABILITY_CODE_MP_LEN,
444 [CAPABILITY_CODE_REFRESH] = CAPABILITY_CODE_REFRESH_LEN,
445 [CAPABILITY_CODE_ORF] = CAPABILITY_CODE_ORF_LEN,
446 [CAPABILITY_CODE_RESTART] = CAPABILITY_CODE_RESTART_LEN,
447 [CAPABILITY_CODE_AS4] = CAPABILITY_CODE_AS4_LEN,
448 [CAPABILITY_CODE_DYNAMIC] = CAPABILITY_CODE_DYNAMIC_LEN,
449 [CAPABILITY_CODE_REFRESH_OLD] = CAPABILITY_CODE_REFRESH_LEN,
450 [CAPABILITY_CODE_ORF_OLD] = CAPABILITY_CODE_ORF_LEN,
453 /* value the capability must be a multiple of.
454 * 0-data capabilities won't be checked against this.
455 * Other capabilities whose data doesn't fall on convenient boundaries for this
456 * table should be set to 1.
458 static const size_t cap_modsizes[] =
460 [CAPABILITY_CODE_MP] = 4,
461 [CAPABILITY_CODE_REFRESH] = 1,
462 [CAPABILITY_CODE_ORF] = 1,
463 [CAPABILITY_CODE_RESTART] = 1,
464 [CAPABILITY_CODE_AS4] = 4,
465 [CAPABILITY_CODE_DYNAMIC] = 1,
466 [CAPABILITY_CODE_REFRESH_OLD] = 1,
467 [CAPABILITY_CODE_ORF_OLD] = 1,
471 * Parse given capability.
472 * XXX: This is reading into a stream, but not using stream API
474 * @param[out] mp_capability Set to 1 on return iff one or more Multiprotocol
475 * capabilities were encountered.
478 bgp_capability_parse (struct peer *peer, size_t length, int *mp_capability,
482 struct stream *s = BGP_INPUT (peer);
483 size_t end = stream_get_getp (s) + length;
485 assert (STREAM_READABLE (s) >= length);
487 while (stream_get_getp (s) < end)
490 u_char *sp = stream_pnt (s);
491 struct capability_header caphdr;
493 /* We need at least capability code and capability length. */
494 if (stream_get_getp(s) + 2 > end)
496 zlog_info ("%s Capability length error (< header)", peer->host);
497 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, BGP_NOTIFY_OPEN_UNSPECIFIC);
501 caphdr.code = stream_getc (s);
502 caphdr.length = stream_getc (s);
503 start = stream_get_getp (s);
505 /* Capability length check sanity check. */
506 if (start + caphdr.length > end)
508 zlog_info ("%s Capability length error (< length)", peer->host);
509 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, BGP_NOTIFY_OPEN_UNSPECIFIC);
513 if (BGP_DEBUG (normal, NORMAL))
514 zlog_debug ("%s OPEN has %s capability (%u), length %u",
516 LOOKUP (capcode_str, caphdr.code),
517 caphdr.code, caphdr.length);
519 /* Length sanity check, type-specific, for known capabilities */
522 case CAPABILITY_CODE_MP:
523 case CAPABILITY_CODE_REFRESH:
524 case CAPABILITY_CODE_REFRESH_OLD:
525 case CAPABILITY_CODE_ORF:
526 case CAPABILITY_CODE_ORF_OLD:
527 case CAPABILITY_CODE_RESTART:
528 case CAPABILITY_CODE_AS4:
529 case CAPABILITY_CODE_DYNAMIC:
531 if (caphdr.length < cap_minsizes[caphdr.code])
533 zlog_info ("%s %s Capability length error: got %u,"
534 " expected at least %u",
536 LOOKUP (capcode_str, caphdr.code),
538 (unsigned) cap_minsizes[caphdr.code]);
539 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
540 BGP_NOTIFY_OPEN_UNSPECIFIC);
544 && caphdr.length % cap_modsizes[caphdr.code] != 0)
546 zlog_info ("%s %s Capability length error: got %u,"
547 " expected a multiple of %u",
549 LOOKUP (capcode_str, caphdr.code),
551 (unsigned) cap_modsizes[caphdr.code]);
552 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
553 BGP_NOTIFY_OPEN_UNSPECIFIC);
556 /* we deliberately ignore unknown codes, see below */
563 case CAPABILITY_CODE_MP:
567 /* Ignore capability when override-capability is set. */
568 if (! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
570 /* Set negotiated value. */
571 ret = bgp_capability_mp (peer, &caphdr);
573 /* Unsupported Capability. */
576 /* Store return data. */
577 memcpy (*error, sp, caphdr.length + 2);
578 *error += caphdr.length + 2;
583 case CAPABILITY_CODE_REFRESH:
584 case CAPABILITY_CODE_REFRESH_OLD:
586 /* BGP refresh capability */
587 if (caphdr.code == CAPABILITY_CODE_REFRESH_OLD)
588 SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
590 SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
593 case CAPABILITY_CODE_ORF:
594 case CAPABILITY_CODE_ORF_OLD:
595 if (bgp_capability_orf_entry (peer, &caphdr))
598 case CAPABILITY_CODE_RESTART:
599 if (bgp_capability_restart (peer, &caphdr))
602 case CAPABILITY_CODE_DYNAMIC:
603 SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV);
605 case CAPABILITY_CODE_AS4:
606 /* Already handled as a special-case parsing of the capabilities
607 * at the beginning of OPEN processing. So we care not a jot
608 * for the value really, only error case.
610 if (!bgp_capability_as4 (peer, &caphdr))
614 if (caphdr.code > 128)
616 /* We don't send Notification for unknown vendor specific
617 capabilities. It seems reasonable for now... */
618 zlog_warn ("%s Vendor specific capability %d",
619 peer->host, caphdr.code);
623 zlog_warn ("%s unrecognized capability code: %d - ignored",
624 peer->host, caphdr.code);
625 memcpy (*error, sp, caphdr.length + 2);
626 *error += caphdr.length + 2;
629 if (stream_get_getp(s) != (start + caphdr.length))
631 if (stream_get_getp(s) > (start + caphdr.length))
632 zlog_warn ("%s Cap-parser for %s read past cap-length, %u!",
633 peer->host, LOOKUP (capcode_str, caphdr.code),
635 stream_set_getp (s, start + caphdr.length);
642 bgp_auth_parse (struct peer *peer, size_t length)
644 bgp_notify_send (peer,
646 BGP_NOTIFY_OPEN_AUTH_FAILURE);
651 strict_capability_same (struct peer *peer)
655 for (i = AFI_IP; i < AFI_MAX; i++)
656 for (j = SAFI_UNICAST; j < SAFI_MAX; j++)
657 if (peer->afc[i][j] != peer->afc_nego[i][j])
662 /* peek into option, stores ASN to *as4 if the AS4 capability was found.
663 * Returns 0 if no as4 found, as4cap value otherwise.
666 peek_for_as4_capability (struct peer *peer, u_char length)
668 struct stream *s = BGP_INPUT (peer);
669 size_t orig_getp = stream_get_getp (s);
670 size_t end = orig_getp + length;
673 /* The full capability parser will better flag the error.. */
674 if (STREAM_READABLE(s) < length)
677 if (BGP_DEBUG (as4, AS4))
678 zlog_info ("%s [AS4] rcv OPEN w/ OPTION parameter len: %u,"
681 /* the error cases we DONT handle, we ONLY try to read as4 out of
682 * correctly formatted options.
684 while (stream_get_getp(s) < end)
689 /* Check the length. */
690 if (stream_get_getp (s) + 2 > end)
693 /* Fetch option type and length. */
694 opt_type = stream_getc (s);
695 opt_length = stream_getc (s);
697 /* Option length check. */
698 if (stream_get_getp (s) + opt_length > end)
701 if (opt_type == BGP_OPEN_OPT_CAP)
703 unsigned long capd_start = stream_get_getp (s);
704 unsigned long capd_end = capd_start + opt_length;
706 assert (capd_end <= end);
708 while (stream_get_getp (s) < capd_end)
710 struct capability_header hdr;
712 if (stream_get_getp (s) + 2 > capd_end)
715 hdr.code = stream_getc (s);
716 hdr.length = stream_getc (s);
718 if ((stream_get_getp(s) + hdr.length) > capd_end)
721 if (hdr.code == CAPABILITY_CODE_AS4)
723 if (BGP_DEBUG (as4, AS4))
724 zlog_info ("[AS4] found AS4 capability, about to parse");
725 as4 = bgp_capability_as4 (peer, &hdr);
729 stream_forward_getp (s, hdr.length);
735 stream_set_getp (s, orig_getp);
742 * @param[out] mp_capability @see bgp_capability_parse() for semantics.
745 bgp_open_option_parse (struct peer *peer, u_char length, int *mp_capability)
749 u_char error_data[BGP_MAX_PACKET_SIZE];
750 struct stream *s = BGP_INPUT(peer);
751 size_t end = stream_get_getp (s) + length;
756 if (BGP_DEBUG (normal, NORMAL))
757 zlog_debug ("%s rcv OPEN w/ OPTION parameter len: %u",
760 while (stream_get_getp(s) < end)
765 /* Must have at least an OPEN option header */
766 if (STREAM_READABLE(s) < 2)
768 zlog_info ("%s Option length error", peer->host);
769 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
770 BGP_NOTIFY_OPEN_UNSPECIFIC);
774 /* Fetch option type and length. */
775 opt_type = stream_getc (s);
776 opt_length = stream_getc (s);
778 /* Option length check. */
779 if (STREAM_READABLE (s) < opt_length)
781 zlog_info ("%s Option length error", peer->host);
782 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
783 BGP_NOTIFY_OPEN_UNSPECIFIC);
787 if (BGP_DEBUG (normal, NORMAL))
788 zlog_debug ("%s rcvd OPEN w/ optional parameter type %u (%s) len %u",
789 peer->host, opt_type,
790 opt_type == BGP_OPEN_OPT_AUTH ? "Authentication" :
791 opt_type == BGP_OPEN_OPT_CAP ? "Capability" : "Unknown",
796 case BGP_OPEN_OPT_AUTH:
797 ret = bgp_auth_parse (peer, opt_length);
799 case BGP_OPEN_OPT_CAP:
800 ret = bgp_capability_parse (peer, opt_length, mp_capability, &error);
803 bgp_notify_send (peer,
805 BGP_NOTIFY_OPEN_UNSUP_PARAM);
810 /* Parse error. To accumulate all unsupported capability codes,
811 bgp_capability_parse does not return -1 when encounter
812 unsupported capability code. To detect that, please check
813 error and erro_data pointer, like below. */
818 /* All OPEN option is parsed. Check capability when strict compare
820 if (CHECK_FLAG (peer->flags, PEER_FLAG_STRICT_CAP_MATCH))
822 /* If Unsupported Capability exists. */
823 if (error != error_data)
825 bgp_notify_send_with_data (peer,
827 BGP_NOTIFY_OPEN_UNSUP_CAPBL,
828 error_data, error - error_data);
832 /* Check local capability does not negotiated with remote
834 if (! strict_capability_same (peer))
836 bgp_notify_send (peer,
838 BGP_NOTIFY_OPEN_UNSUP_CAPBL);
843 /* Check there are no common AFI/SAFIs and send Unsupported Capability
845 if (*mp_capability &&
846 ! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
848 if (! peer->afc_nego[AFI_IP][SAFI_UNICAST]
849 && ! peer->afc_nego[AFI_IP][SAFI_MULTICAST]
850 && ! peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
851 && ! peer->afc_nego[AFI_IP][SAFI_ENCAP]
852 && ! peer->afc_nego[AFI_IP6][SAFI_UNICAST]
853 && ! peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
854 && ! peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
855 && ! peer->afc_nego[AFI_IP6][SAFI_ENCAP])
857 plog_err (peer->log, "%s [Error] Configured AFI/SAFIs do not "
858 "overlap with received MP capabilities",
861 if (error != error_data)
863 bgp_notify_send_with_data (peer,
865 BGP_NOTIFY_OPEN_UNSUP_CAPBL,
866 error_data, error - error_data);
868 bgp_notify_send (peer,
870 BGP_NOTIFY_OPEN_UNSUP_CAPBL);
878 bgp_open_capability_orf (struct stream *s, struct peer *peer,
879 afi_t afi, safi_t safi, u_char code)
885 unsigned long numberp;
886 int number_of_orfs = 0;
888 if (safi == SAFI_MPLS_VPN)
889 safi = SAFI_MPLS_LABELED_VPN;
891 stream_putc (s, BGP_OPEN_OPT_CAP);
892 capp = stream_get_endp (s); /* Set Capability Len Pointer */
893 stream_putc (s, 0); /* Capability Length */
894 stream_putc (s, code); /* Capability Code */
895 orfp = stream_get_endp (s); /* Set ORF Len Pointer */
896 stream_putc (s, 0); /* ORF Length */
897 stream_putw (s, afi);
899 stream_putc (s, safi);
900 numberp = stream_get_endp (s); /* Set Number Pointer */
901 stream_putc (s, 0); /* Number of ORFs */
903 /* Address Prefix ORF */
904 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
905 || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
907 stream_putc (s, (code == CAPABILITY_CODE_ORF ?
908 ORF_TYPE_PREFIX : ORF_TYPE_PREFIX_OLD));
910 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
911 && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
913 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV);
914 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV);
915 stream_putc (s, ORF_MODE_BOTH);
917 else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM))
919 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV);
920 stream_putc (s, ORF_MODE_SEND);
924 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV);
925 stream_putc (s, ORF_MODE_RECEIVE);
930 /* Total Number of ORFs. */
931 stream_putc_at (s, numberp, number_of_orfs);
934 orf_len = stream_get_endp (s) - orfp - 1;
935 stream_putc_at (s, orfp, orf_len);
937 /* Total Capability Len. */
938 cap_len = stream_get_endp (s) - capp - 1;
939 stream_putc_at (s, capp, cap_len);
942 /* Fill in capability open option to the packet. */
944 bgp_open_capability (struct stream *s, struct peer *peer)
947 unsigned long cp, capp, rcapp;
951 u_int32_t restart_time;
953 /* Remember current pointer for Opt Parm Len. */
954 cp = stream_get_endp (s);
959 /* Do not send capability. */
960 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN)
961 || CHECK_FLAG (peer->flags, PEER_FLAG_DONT_CAPABILITY))
965 if (peer->afc[AFI_IP][SAFI_UNICAST])
967 peer->afc_adv[AFI_IP][SAFI_UNICAST] = 1;
968 stream_putc (s, BGP_OPEN_OPT_CAP);
969 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
970 stream_putc (s, CAPABILITY_CODE_MP);
971 stream_putc (s, CAPABILITY_CODE_MP_LEN);
972 stream_putw (s, AFI_IP);
974 stream_putc (s, SAFI_UNICAST);
976 /* IPv4 multicast. */
977 if (peer->afc[AFI_IP][SAFI_MULTICAST])
979 peer->afc_adv[AFI_IP][SAFI_MULTICAST] = 1;
980 stream_putc (s, BGP_OPEN_OPT_CAP);
981 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
982 stream_putc (s, CAPABILITY_CODE_MP);
983 stream_putc (s, CAPABILITY_CODE_MP_LEN);
984 stream_putw (s, AFI_IP);
986 stream_putc (s, SAFI_MULTICAST);
989 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
991 peer->afc_adv[AFI_IP][SAFI_MPLS_VPN] = 1;
992 stream_putc (s, BGP_OPEN_OPT_CAP);
993 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
994 stream_putc (s, CAPABILITY_CODE_MP);
995 stream_putc (s, CAPABILITY_CODE_MP_LEN);
996 stream_putw (s, AFI_IP);
998 stream_putc (s, SAFI_MPLS_LABELED_VPN);
1001 if (peer->afc[AFI_IP][SAFI_ENCAP])
1003 peer->afc_adv[AFI_IP][SAFI_ENCAP] = 1;
1004 stream_putc (s, BGP_OPEN_OPT_CAP);
1005 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1006 stream_putc (s, CAPABILITY_CODE_MP);
1007 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1008 stream_putw (s, AFI_IP);
1010 stream_putc (s, SAFI_ENCAP);
1013 if (peer->afc[AFI_IP6][SAFI_UNICAST])
1015 peer->afc_adv[AFI_IP6][SAFI_UNICAST] = 1;
1016 stream_putc (s, BGP_OPEN_OPT_CAP);
1017 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1018 stream_putc (s, CAPABILITY_CODE_MP);
1019 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1020 stream_putw (s, AFI_IP6);
1022 stream_putc (s, SAFI_UNICAST);
1024 /* IPv6 multicast. */
1025 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
1027 peer->afc_adv[AFI_IP6][SAFI_MULTICAST] = 1;
1028 stream_putc (s, BGP_OPEN_OPT_CAP);
1029 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1030 stream_putc (s, CAPABILITY_CODE_MP);
1031 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1032 stream_putw (s, AFI_IP6);
1034 stream_putc (s, SAFI_MULTICAST);
1037 if (peer->afc[AFI_IP6][SAFI_MPLS_VPN])
1039 peer->afc_adv[AFI_IP6][SAFI_MPLS_VPN] = 1;
1040 stream_putc (s, BGP_OPEN_OPT_CAP);
1041 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1042 stream_putc (s, CAPABILITY_CODE_MP);
1043 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1044 stream_putw (s, AFI_IP6);
1046 stream_putc (s, SAFI_MPLS_LABELED_VPN);
1049 if (peer->afc[AFI_IP6][SAFI_ENCAP])
1051 peer->afc_adv[AFI_IP6][SAFI_ENCAP] = 1;
1052 stream_putc (s, BGP_OPEN_OPT_CAP);
1053 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1054 stream_putc (s, CAPABILITY_CODE_MP);
1055 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1056 stream_putw (s, AFI_IP6);
1058 stream_putc (s, SAFI_ENCAP);
1061 /* Route refresh. */
1062 SET_FLAG (peer->cap, PEER_CAP_REFRESH_ADV);
1063 stream_putc (s, BGP_OPEN_OPT_CAP);
1064 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2);
1065 stream_putc (s, CAPABILITY_CODE_REFRESH_OLD);
1066 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1067 stream_putc (s, BGP_OPEN_OPT_CAP);
1068 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2);
1069 stream_putc (s, CAPABILITY_CODE_REFRESH);
1070 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1073 SET_FLAG (peer->cap, PEER_CAP_AS4_ADV);
1074 stream_putc (s, BGP_OPEN_OPT_CAP);
1075 stream_putc (s, CAPABILITY_CODE_AS4_LEN + 2);
1076 stream_putc (s, CAPABILITY_CODE_AS4);
1077 stream_putc (s, CAPABILITY_CODE_AS4_LEN);
1078 if ( peer->change_local_as )
1079 local_as = peer->change_local_as;
1081 local_as = peer->local_as;
1082 stream_putl (s, local_as );
1084 /* ORF capability. */
1085 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
1086 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
1087 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
1088 || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
1090 bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF_OLD);
1091 bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF);
1094 /* Dynamic capability. */
1095 if (CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY))
1097 SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV);
1098 stream_putc (s, BGP_OPEN_OPT_CAP);
1099 stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN + 2);
1100 stream_putc (s, CAPABILITY_CODE_DYNAMIC);
1101 stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN);
1104 /* Sending base graceful-restart capability irrespective of the config */
1105 SET_FLAG (peer->cap, PEER_CAP_RESTART_ADV);
1106 stream_putc (s, BGP_OPEN_OPT_CAP);
1107 capp = stream_get_endp (s); /* Set Capability Len Pointer */
1108 stream_putc (s, 0); /* Capability Length */
1109 stream_putc (s, CAPABILITY_CODE_RESTART);
1110 rcapp = stream_get_endp (s); /* Set Restart Capability Len Pointer */
1112 restart_time = peer->bgp->restart_time;
1113 if (peer->bgp->t_startup)
1115 SET_FLAG (restart_time, RESTART_R_BIT);
1116 SET_FLAG (peer->cap, PEER_CAP_RESTART_BIT_ADV);
1118 stream_putw (s, restart_time);
1120 /* Send address-family specific graceful-restart capability only when GR config
1122 if (bgp_flag_check (peer->bgp, BGP_FLAG_GRACEFUL_RESTART))
1124 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
1125 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
1126 if (peer->afc[afi][safi])
1128 stream_putw (s, afi);
1129 stream_putc (s, safi);
1130 stream_putc (s, 0); //Forwarding is not retained as of now.
1134 /* Total Graceful restart capability Len. */
1135 len = stream_get_endp (s) - rcapp - 1;
1136 stream_putc_at (s, rcapp, len);
1138 /* Total Capability Len. */
1139 len = stream_get_endp (s) - capp - 1;
1140 stream_putc_at (s, capp, len);
1142 /* Total Opt Parm Len. */
1143 len = stream_get_endp (s) - cp - 1;
1144 stream_putc_at (s, cp, len);