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 mpc->afi = stream_getw (s);
124 mpc->reserved = stream_getc (s);
125 mpc->safi = stream_getc (s);
129 bgp_afi_safi_valid_indices (afi_t afi, safi_t *safi)
137 /* BGP MPLS-labeled VPN SAFI isn't contigious with others, remap */
138 case SAFI_MPLS_LABELED_VPN:
139 *safi = SAFI_MPLS_VPN;
151 zlog_debug ("unknown afi/safi (%u/%u)", afi, *safi);
156 /* Set negotiated capability value. */
158 bgp_capability_mp (struct peer *peer, struct capability_header *hdr)
160 struct capability_mp_data mpc;
161 struct stream *s = BGP_INPUT (peer);
163 bgp_capability_mp_data (s, &mpc);
165 if (BGP_DEBUG (normal, NORMAL))
166 zlog_debug ("%s OPEN has MP_EXT CAP for afi/safi: %u/%u",
167 peer->host, mpc.afi, mpc.safi);
169 if (!bgp_afi_safi_valid_indices (mpc.afi, &mpc.safi))
172 /* Now safi remapped, and afi/safi are valid array indices */
173 peer->afc_recv[mpc.afi][mpc.safi] = 1;
175 if (peer->afc[mpc.afi][mpc.safi])
176 peer->afc_nego[mpc.afi][mpc.safi] = 1;
184 bgp_capability_orf_not_support (struct peer *peer, afi_t afi, safi_t safi,
185 u_char type, u_char mode)
187 if (BGP_DEBUG (normal, NORMAL))
188 zlog_debug ("%s Addr-family %d/%d has ORF type/mode %d/%d not supported",
189 peer->host, afi, safi, type, mode);
192 static const struct message orf_type_str[] =
194 { ORF_TYPE_PREFIX, "Prefixlist" },
195 { ORF_TYPE_PREFIX_OLD, "Prefixlist (old)" },
197 static const int orf_type_str_max = array_size(orf_type_str);
199 static const struct message orf_mode_str[] =
201 { ORF_MODE_RECEIVE, "Receive" },
202 { ORF_MODE_SEND, "Send" },
203 { ORF_MODE_BOTH, "Both" },
205 static const int orf_mode_str_max = array_size(orf_mode_str);
208 bgp_capability_orf_entry (struct peer *peer, struct capability_header *hdr)
210 struct stream *s = BGP_INPUT (peer);
211 struct capability_orf_entry entry;
216 u_int16_t sm_cap = 0; /* capability send-mode receive */
217 u_int16_t rm_cap = 0; /* capability receive-mode receive */
220 /* ORF Entry header */
221 bgp_capability_mp_data (s, &entry.mpc);
222 entry.num = stream_getc (s);
224 safi = entry.mpc.safi;
226 if (BGP_DEBUG (normal, NORMAL))
227 zlog_debug ("%s ORF Cap entry for afi/safi: %u/%u",
228 peer->host, entry.mpc.afi, entry.mpc.safi);
230 /* Check AFI and SAFI. */
231 if (!bgp_afi_safi_valid_indices (entry.mpc.afi, &safi))
233 zlog_info ("%s Addr-family %d/%d not supported."
234 " Ignoring the ORF capability",
235 peer->host, entry.mpc.afi, entry.mpc.safi);
239 /* validate number field */
240 if (CAPABILITY_CODE_ORF_LEN + (entry.num * 2) > hdr->length)
242 zlog_info ("%s ORF Capability entry length error,"
243 " Cap length %u, num %u",
244 peer->host, hdr->length, entry.num);
245 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, BGP_NOTIFY_OPEN_UNSPECIFIC);
249 for (i = 0 ; i < entry.num ; i++)
251 type = stream_getc(s);
252 mode = stream_getc(s);
254 /* ORF Mode error check */
259 case ORF_MODE_RECEIVE:
262 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
265 /* ORF Type and afi/safi error checks */
266 /* capcode versus type */
269 case CAPABILITY_CODE_ORF:
272 case ORF_TYPE_PREFIX:
275 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
279 case CAPABILITY_CODE_ORF_OLD:
282 case ORF_TYPE_PREFIX_OLD:
285 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
290 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
295 if (!((afi == AFI_IP && safi == SAFI_UNICAST)
296 || (afi == AFI_IP && safi == SAFI_MULTICAST)
297 || (afi == AFI_IP6 && safi == SAFI_UNICAST)))
299 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
303 if (BGP_DEBUG (normal, NORMAL))
304 zlog_debug ("%s OPEN has %s ORF capability"
305 " as %s for afi/safi: %d/%d",
306 peer->host, LOOKUP (orf_type_str, type),
307 LOOKUP (orf_mode_str, mode),
308 entry.mpc.afi, safi);
310 if (hdr->code == CAPABILITY_CODE_ORF)
312 sm_cap = PEER_CAP_ORF_PREFIX_SM_RCV;
313 rm_cap = PEER_CAP_ORF_PREFIX_RM_RCV;
315 else if (hdr->code == CAPABILITY_CODE_ORF_OLD)
317 sm_cap = PEER_CAP_ORF_PREFIX_SM_OLD_RCV;
318 rm_cap = PEER_CAP_ORF_PREFIX_RM_OLD_RCV;
322 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
329 SET_FLAG (peer->af_cap[afi][safi], sm_cap);
330 SET_FLAG (peer->af_cap[afi][safi], rm_cap);
333 SET_FLAG (peer->af_cap[afi][safi], sm_cap);
335 case ORF_MODE_RECEIVE:
336 SET_FLAG (peer->af_cap[afi][safi], rm_cap);
344 bgp_capability_restart (struct peer *peer, struct capability_header *caphdr)
346 struct stream *s = BGP_INPUT (peer);
347 u_int16_t restart_flag_time;
348 size_t end = stream_get_getp (s) + caphdr->length;
350 SET_FLAG (peer->cap, PEER_CAP_RESTART_RCV);
351 restart_flag_time = stream_getw(s);
352 if (CHECK_FLAG (restart_flag_time, RESTART_R_BIT))
353 SET_FLAG (peer->cap, PEER_CAP_RESTART_BIT_RCV);
355 UNSET_FLAG (restart_flag_time, 0xF000);
356 peer->v_gr_restart = restart_flag_time;
358 if (BGP_DEBUG (normal, NORMAL))
360 zlog_debug ("%s OPEN has Graceful Restart capability", peer->host);
361 zlog_debug ("%s Peer has%srestarted. Restart Time : %d",
363 CHECK_FLAG (peer->cap, PEER_CAP_RESTART_BIT_RCV) ? " "
368 while (stream_get_getp (s) + 4 <= end)
370 afi_t afi = stream_getw (s);
371 safi_t safi = stream_getc (s);
372 u_char flag = stream_getc (s);
374 if (!bgp_afi_safi_valid_indices (afi, &safi))
376 if (BGP_DEBUG (normal, NORMAL))
377 zlog_debug ("%s Addr-family %d/%d(afi/safi) not supported."
378 " Ignore the Graceful Restart capability",
379 peer->host, afi, safi);
381 else if (!peer->afc[afi][safi])
383 if (BGP_DEBUG (normal, NORMAL))
384 zlog_debug ("%s Addr-family %d/%d(afi/safi) not enabled."
385 " Ignore the Graceful Restart capability",
386 peer->host, afi, safi);
390 if (BGP_DEBUG (normal, NORMAL))
391 zlog_debug ("%s Address family %s is%spreserved", peer->host,
392 afi_safi_print (afi, safi),
393 CHECK_FLAG (peer->af_cap[afi][safi],
394 PEER_CAP_RESTART_AF_PRESERVE_RCV)
397 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV);
398 if (CHECK_FLAG (flag, RESTART_F_BIT))
399 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV);
407 bgp_capability_as4 (struct peer *peer, struct capability_header *hdr)
409 SET_FLAG (peer->cap, PEER_CAP_AS4_RCV);
411 if (hdr->length != CAPABILITY_CODE_AS4_LEN)
413 zlog_err ("%s AS4 capability has incorrect data length %d",
414 peer->host, hdr->length);
418 as_t as4 = stream_getl (BGP_INPUT(peer));
420 if (BGP_DEBUG (as4, AS4))
421 zlog_debug ("%s [AS4] about to set cap PEER_CAP_AS4_RCV, got as4 %u",
426 static const struct message capcode_str[] =
428 { CAPABILITY_CODE_MP, "MultiProtocol Extensions" },
429 { CAPABILITY_CODE_REFRESH, "Route Refresh" },
430 { CAPABILITY_CODE_ORF, "Cooperative Route Filtering" },
431 { CAPABILITY_CODE_RESTART, "Graceful Restart" },
432 { CAPABILITY_CODE_AS4, "4-octet AS number" },
433 { CAPABILITY_CODE_DYNAMIC, "Dynamic" },
434 { CAPABILITY_CODE_REFRESH_OLD, "Route Refresh (Old)" },
435 { CAPABILITY_CODE_ORF_OLD, "ORF (Old)" },
437 static const int capcode_str_max = array_size(capcode_str);
439 /* Minimum sizes for length field of each cap (so not inc. the header) */
440 static const size_t cap_minsizes[] =
442 [CAPABILITY_CODE_MP] = CAPABILITY_CODE_MP_LEN,
443 [CAPABILITY_CODE_REFRESH] = CAPABILITY_CODE_REFRESH_LEN,
444 [CAPABILITY_CODE_ORF] = CAPABILITY_CODE_ORF_LEN,
445 [CAPABILITY_CODE_RESTART] = CAPABILITY_CODE_RESTART_LEN,
446 [CAPABILITY_CODE_AS4] = CAPABILITY_CODE_AS4_LEN,
447 [CAPABILITY_CODE_DYNAMIC] = CAPABILITY_CODE_DYNAMIC_LEN,
448 [CAPABILITY_CODE_REFRESH_OLD] = CAPABILITY_CODE_REFRESH_LEN,
449 [CAPABILITY_CODE_ORF_OLD] = CAPABILITY_CODE_ORF_LEN,
452 /* value the capability must be a multiple of.
453 * 0-data capabilities won't be checked against this.
454 * Other capabilities whose data doesn't fall on convenient boundaries for this
455 * table should be set to 1.
457 static const size_t cap_modsizes[] =
459 [CAPABILITY_CODE_MP] = 4,
460 [CAPABILITY_CODE_REFRESH] = 1,
461 [CAPABILITY_CODE_ORF] = 1,
462 [CAPABILITY_CODE_RESTART] = 1,
463 [CAPABILITY_CODE_AS4] = 4,
464 [CAPABILITY_CODE_DYNAMIC] = 1,
465 [CAPABILITY_CODE_REFRESH_OLD] = 1,
466 [CAPABILITY_CODE_ORF_OLD] = 1,
470 * Parse given capability.
471 * XXX: This is reading into a stream, but not using stream API
473 * @param[out] mp_capability Set to 1 on return iff one or more Multiprotocol
474 * capabilities were encountered.
477 bgp_capability_parse (struct peer *peer, size_t length, int *mp_capability,
481 struct stream *s = BGP_INPUT (peer);
482 size_t end = stream_get_getp (s) + length;
484 assert (STREAM_READABLE (s) >= length);
486 while (stream_get_getp (s) < end)
489 u_char *sp = stream_pnt (s);
490 struct capability_header caphdr;
492 /* We need at least capability code and capability length. */
493 if (stream_get_getp(s) + 2 > end)
495 zlog_info ("%s Capability length error (< header)", peer->host);
496 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, BGP_NOTIFY_OPEN_UNSPECIFIC);
500 caphdr.code = stream_getc (s);
501 caphdr.length = stream_getc (s);
502 start = stream_get_getp (s);
504 /* Capability length check sanity check. */
505 if (start + caphdr.length > end)
507 zlog_info ("%s Capability length error (< length)", peer->host);
508 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR, BGP_NOTIFY_OPEN_UNSPECIFIC);
512 if (BGP_DEBUG (normal, NORMAL))
513 zlog_debug ("%s OPEN has %s capability (%u), length %u",
515 LOOKUP (capcode_str, caphdr.code),
516 caphdr.code, caphdr.length);
518 /* Length sanity check, type-specific, for known capabilities */
521 case CAPABILITY_CODE_MP:
522 case CAPABILITY_CODE_REFRESH:
523 case CAPABILITY_CODE_REFRESH_OLD:
524 case CAPABILITY_CODE_ORF:
525 case CAPABILITY_CODE_ORF_OLD:
526 case CAPABILITY_CODE_RESTART:
527 case CAPABILITY_CODE_AS4:
528 case CAPABILITY_CODE_DYNAMIC:
530 if (caphdr.length < cap_minsizes[caphdr.code])
532 zlog_info ("%s %s Capability length error: got %u,"
533 " expected at least %u",
535 LOOKUP (capcode_str, caphdr.code),
537 (unsigned) cap_minsizes[caphdr.code]);
538 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
539 BGP_NOTIFY_OPEN_UNSPECIFIC);
543 && caphdr.length % cap_modsizes[caphdr.code] != 0)
545 zlog_info ("%s %s Capability length error: got %u,"
546 " expected a multiple of %u",
548 LOOKUP (capcode_str, caphdr.code),
550 (unsigned) cap_modsizes[caphdr.code]);
551 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
552 BGP_NOTIFY_OPEN_UNSPECIFIC);
555 /* we deliberately ignore unknown codes, see below */
562 case CAPABILITY_CODE_MP:
566 /* Ignore capability when override-capability is set. */
567 if (! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
569 /* Set negotiated value. */
570 ret = bgp_capability_mp (peer, &caphdr);
572 /* Unsupported Capability. */
575 /* Store return data. */
576 memcpy (*error, sp, caphdr.length + 2);
577 *error += caphdr.length + 2;
582 case CAPABILITY_CODE_REFRESH:
583 case CAPABILITY_CODE_REFRESH_OLD:
585 /* BGP refresh capability */
586 if (caphdr.code == CAPABILITY_CODE_REFRESH_OLD)
587 SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
589 SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
592 case CAPABILITY_CODE_ORF:
593 case CAPABILITY_CODE_ORF_OLD:
594 if (bgp_capability_orf_entry (peer, &caphdr))
597 case CAPABILITY_CODE_RESTART:
598 if (bgp_capability_restart (peer, &caphdr))
601 case CAPABILITY_CODE_DYNAMIC:
602 SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV);
604 case CAPABILITY_CODE_AS4:
605 /* Already handled as a special-case parsing of the capabilities
606 * at the beginning of OPEN processing. So we care not a jot
607 * for the value really, only error case.
609 if (!bgp_capability_as4 (peer, &caphdr))
613 if (caphdr.code > 128)
615 /* We don't send Notification for unknown vendor specific
616 capabilities. It seems reasonable for now... */
617 zlog_warn ("%s Vendor specific capability %d",
618 peer->host, caphdr.code);
622 zlog_warn ("%s unrecognized capability code: %d - ignored",
623 peer->host, caphdr.code);
624 memcpy (*error, sp, caphdr.length + 2);
625 *error += caphdr.length + 2;
628 if (stream_get_getp(s) != (start + caphdr.length))
630 if (stream_get_getp(s) > (start + caphdr.length))
631 zlog_warn ("%s Cap-parser for %s read past cap-length, %u!",
632 peer->host, LOOKUP (capcode_str, caphdr.code),
634 stream_set_getp (s, start + caphdr.length);
641 bgp_auth_parse (struct peer *peer, size_t length)
643 bgp_notify_send (peer,
645 BGP_NOTIFY_OPEN_AUTH_FAILURE);
650 strict_capability_same (struct peer *peer)
654 for (i = AFI_IP; i < AFI_MAX; i++)
655 for (j = SAFI_UNICAST; j < SAFI_MAX; j++)
656 if (peer->afc[i][j] != peer->afc_nego[i][j])
661 /* peek into option, stores ASN to *as4 if the AS4 capability was found.
662 * Returns 0 if no as4 found, as4cap value otherwise.
665 peek_for_as4_capability (struct peer *peer, u_char length)
667 struct stream *s = BGP_INPUT (peer);
668 size_t orig_getp = stream_get_getp (s);
669 size_t end = orig_getp + length;
672 /* The full capability parser will better flag the error.. */
673 if (STREAM_READABLE(s) < length)
676 if (BGP_DEBUG (as4, AS4))
677 zlog_info ("%s [AS4] rcv OPEN w/ OPTION parameter len: %u,"
680 /* the error cases we DONT handle, we ONLY try to read as4 out of
681 * correctly formatted options.
683 while (stream_get_getp(s) < end)
688 /* Check the length. */
689 if (stream_get_getp (s) + 2 > end)
692 /* Fetch option type and length. */
693 opt_type = stream_getc (s);
694 opt_length = stream_getc (s);
696 /* Option length check. */
697 if (stream_get_getp (s) + opt_length > end)
700 if (opt_type == BGP_OPEN_OPT_CAP)
702 unsigned long capd_start = stream_get_getp (s);
703 unsigned long capd_end = capd_start + opt_length;
705 assert (capd_end <= end);
707 while (stream_get_getp (s) < capd_end)
709 struct capability_header hdr;
711 if (stream_get_getp (s) + 2 > capd_end)
714 hdr.code = stream_getc (s);
715 hdr.length = stream_getc (s);
717 if ((stream_get_getp(s) + hdr.length) > capd_end)
720 if (hdr.code == CAPABILITY_CODE_AS4)
722 if (BGP_DEBUG (as4, AS4))
723 zlog_info ("[AS4] found AS4 capability, about to parse");
724 as4 = bgp_capability_as4 (peer, &hdr);
728 stream_forward_getp (s, hdr.length);
734 stream_set_getp (s, orig_getp);
741 * @param[out] mp_capability @see bgp_capability_parse() for semantics.
744 bgp_open_option_parse (struct peer *peer, u_char length, int *mp_capability)
748 u_char error_data[BGP_MAX_PACKET_SIZE];
749 struct stream *s = BGP_INPUT(peer);
750 size_t end = stream_get_getp (s) + length;
755 if (BGP_DEBUG (normal, NORMAL))
756 zlog_debug ("%s rcv OPEN w/ OPTION parameter len: %u",
759 while (stream_get_getp(s) < end)
764 /* Must have at least an OPEN option header */
765 if (STREAM_READABLE(s) < 2)
767 zlog_info ("%s Option length error", peer->host);
768 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
769 BGP_NOTIFY_OPEN_UNSPECIFIC);
773 /* Fetch option type and length. */
774 opt_type = stream_getc (s);
775 opt_length = stream_getc (s);
777 /* Option length check. */
778 if (STREAM_READABLE (s) < opt_length)
780 zlog_info ("%s Option length error", peer->host);
781 bgp_notify_send (peer, BGP_NOTIFY_OPEN_ERR,
782 BGP_NOTIFY_OPEN_UNSPECIFIC);
786 if (BGP_DEBUG (normal, NORMAL))
787 zlog_debug ("%s rcvd OPEN w/ optional parameter type %u (%s) len %u",
788 peer->host, opt_type,
789 opt_type == BGP_OPEN_OPT_AUTH ? "Authentication" :
790 opt_type == BGP_OPEN_OPT_CAP ? "Capability" : "Unknown",
795 case BGP_OPEN_OPT_AUTH:
796 ret = bgp_auth_parse (peer, opt_length);
798 case BGP_OPEN_OPT_CAP:
799 ret = bgp_capability_parse (peer, opt_length, mp_capability, &error);
802 bgp_notify_send (peer,
804 BGP_NOTIFY_OPEN_UNSUP_PARAM);
809 /* Parse error. To accumulate all unsupported capability codes,
810 bgp_capability_parse does not return -1 when encounter
811 unsupported capability code. To detect that, please check
812 error and erro_data pointer, like below. */
817 /* All OPEN option is parsed. Check capability when strict compare
819 if (CHECK_FLAG (peer->flags, PEER_FLAG_STRICT_CAP_MATCH))
821 /* If Unsupported Capability exists. */
822 if (error != error_data)
824 bgp_notify_send_with_data (peer,
826 BGP_NOTIFY_OPEN_UNSUP_CAPBL,
827 error_data, error - error_data);
831 /* Check local capability does not negotiated with remote
833 if (! strict_capability_same (peer))
835 bgp_notify_send (peer,
837 BGP_NOTIFY_OPEN_UNSUP_CAPBL);
842 /* Check there are no common AFI/SAFIs and send Unsupported Capability
844 if (*mp_capability &&
845 ! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
847 if (! peer->afc_nego[AFI_IP][SAFI_UNICAST]
848 && ! peer->afc_nego[AFI_IP][SAFI_MULTICAST]
849 && ! peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
850 && ! peer->afc_nego[AFI_IP][SAFI_ENCAP]
851 && ! peer->afc_nego[AFI_IP6][SAFI_UNICAST]
852 && ! peer->afc_nego[AFI_IP6][SAFI_MULTICAST]
853 && ! peer->afc_nego[AFI_IP6][SAFI_MPLS_VPN]
854 && ! peer->afc_nego[AFI_IP6][SAFI_ENCAP])
856 plog_err (peer->log, "%s [Error] Configured AFI/SAFIs do not "
857 "overlap with received MP capabilities",
860 if (error != error_data)
862 bgp_notify_send_with_data (peer,
864 BGP_NOTIFY_OPEN_UNSUP_CAPBL,
865 error_data, error - error_data);
867 bgp_notify_send (peer,
869 BGP_NOTIFY_OPEN_UNSUP_CAPBL);
877 bgp_open_capability_orf (struct stream *s, struct peer *peer,
878 afi_t afi, safi_t safi, u_char code)
884 unsigned long numberp;
885 int number_of_orfs = 0;
887 if (safi == SAFI_MPLS_VPN)
888 safi = SAFI_MPLS_LABELED_VPN;
890 stream_putc (s, BGP_OPEN_OPT_CAP);
891 capp = stream_get_endp (s); /* Set Capability Len Pointer */
892 stream_putc (s, 0); /* Capability Length */
893 stream_putc (s, code); /* Capability Code */
894 orfp = stream_get_endp (s); /* Set ORF Len Pointer */
895 stream_putc (s, 0); /* ORF Length */
896 stream_putw (s, afi);
898 stream_putc (s, safi);
899 numberp = stream_get_endp (s); /* Set Number Pointer */
900 stream_putc (s, 0); /* Number of ORFs */
902 /* Address Prefix ORF */
903 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
904 || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
906 stream_putc (s, (code == CAPABILITY_CODE_ORF ?
907 ORF_TYPE_PREFIX : ORF_TYPE_PREFIX_OLD));
909 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
910 && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
912 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV);
913 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV);
914 stream_putc (s, ORF_MODE_BOTH);
916 else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM))
918 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV);
919 stream_putc (s, ORF_MODE_SEND);
923 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV);
924 stream_putc (s, ORF_MODE_RECEIVE);
929 /* Total Number of ORFs. */
930 stream_putc_at (s, numberp, number_of_orfs);
933 orf_len = stream_get_endp (s) - orfp - 1;
934 stream_putc_at (s, orfp, orf_len);
936 /* Total Capability Len. */
937 cap_len = stream_get_endp (s) - capp - 1;
938 stream_putc_at (s, capp, cap_len);
941 /* Fill in capability open option to the packet. */
943 bgp_open_capability (struct stream *s, struct peer *peer)
946 unsigned long cp, capp, rcapp;
950 u_int32_t restart_time;
952 /* Remember current pointer for Opt Parm Len. */
953 cp = stream_get_endp (s);
958 /* Do not send capability. */
959 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN)
960 || CHECK_FLAG (peer->flags, PEER_FLAG_DONT_CAPABILITY))
964 if (peer->afc[AFI_IP][SAFI_UNICAST])
966 peer->afc_adv[AFI_IP][SAFI_UNICAST] = 1;
967 stream_putc (s, BGP_OPEN_OPT_CAP);
968 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
969 stream_putc (s, CAPABILITY_CODE_MP);
970 stream_putc (s, CAPABILITY_CODE_MP_LEN);
971 stream_putw (s, AFI_IP);
973 stream_putc (s, SAFI_UNICAST);
975 /* IPv4 multicast. */
976 if (peer->afc[AFI_IP][SAFI_MULTICAST])
978 peer->afc_adv[AFI_IP][SAFI_MULTICAST] = 1;
979 stream_putc (s, BGP_OPEN_OPT_CAP);
980 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
981 stream_putc (s, CAPABILITY_CODE_MP);
982 stream_putc (s, CAPABILITY_CODE_MP_LEN);
983 stream_putw (s, AFI_IP);
985 stream_putc (s, SAFI_MULTICAST);
988 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
990 peer->afc_adv[AFI_IP][SAFI_MPLS_VPN] = 1;
991 stream_putc (s, BGP_OPEN_OPT_CAP);
992 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
993 stream_putc (s, CAPABILITY_CODE_MP);
994 stream_putc (s, CAPABILITY_CODE_MP_LEN);
995 stream_putw (s, AFI_IP);
997 stream_putc (s, SAFI_MPLS_LABELED_VPN);
1000 if (peer->afc[AFI_IP][SAFI_ENCAP])
1002 peer->afc_adv[AFI_IP][SAFI_ENCAP] = 1;
1003 stream_putc (s, BGP_OPEN_OPT_CAP);
1004 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1005 stream_putc (s, CAPABILITY_CODE_MP);
1006 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1007 stream_putw (s, AFI_IP);
1009 stream_putc (s, SAFI_ENCAP);
1012 if (peer->afc[AFI_IP6][SAFI_UNICAST])
1014 peer->afc_adv[AFI_IP6][SAFI_UNICAST] = 1;
1015 stream_putc (s, BGP_OPEN_OPT_CAP);
1016 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1017 stream_putc (s, CAPABILITY_CODE_MP);
1018 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1019 stream_putw (s, AFI_IP6);
1021 stream_putc (s, SAFI_UNICAST);
1023 /* IPv6 multicast. */
1024 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
1026 peer->afc_adv[AFI_IP6][SAFI_MULTICAST] = 1;
1027 stream_putc (s, BGP_OPEN_OPT_CAP);
1028 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1029 stream_putc (s, CAPABILITY_CODE_MP);
1030 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1031 stream_putw (s, AFI_IP6);
1033 stream_putc (s, SAFI_MULTICAST);
1036 if (peer->afc[AFI_IP6][SAFI_MPLS_VPN])
1038 peer->afc_adv[AFI_IP6][SAFI_MPLS_VPN] = 1;
1039 stream_putc (s, BGP_OPEN_OPT_CAP);
1040 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1041 stream_putc (s, CAPABILITY_CODE_MP);
1042 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1043 stream_putw (s, AFI_IP6);
1045 stream_putc (s, SAFI_MPLS_LABELED_VPN);
1048 if (peer->afc[AFI_IP6][SAFI_ENCAP])
1050 peer->afc_adv[AFI_IP6][SAFI_ENCAP] = 1;
1051 stream_putc (s, BGP_OPEN_OPT_CAP);
1052 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
1053 stream_putc (s, CAPABILITY_CODE_MP);
1054 stream_putc (s, CAPABILITY_CODE_MP_LEN);
1055 stream_putw (s, AFI_IP6);
1057 stream_putc (s, SAFI_ENCAP);
1060 /* Route refresh. */
1061 SET_FLAG (peer->cap, PEER_CAP_REFRESH_ADV);
1062 stream_putc (s, BGP_OPEN_OPT_CAP);
1063 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2);
1064 stream_putc (s, CAPABILITY_CODE_REFRESH_OLD);
1065 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1066 stream_putc (s, BGP_OPEN_OPT_CAP);
1067 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2);
1068 stream_putc (s, CAPABILITY_CODE_REFRESH);
1069 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
1072 SET_FLAG (peer->cap, PEER_CAP_AS4_ADV);
1073 stream_putc (s, BGP_OPEN_OPT_CAP);
1074 stream_putc (s, CAPABILITY_CODE_AS4_LEN + 2);
1075 stream_putc (s, CAPABILITY_CODE_AS4);
1076 stream_putc (s, CAPABILITY_CODE_AS4_LEN);
1077 if ( peer->change_local_as )
1078 local_as = peer->change_local_as;
1080 local_as = peer->local_as;
1081 stream_putl (s, local_as );
1083 /* ORF capability. */
1084 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
1085 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
1086 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
1087 || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
1089 bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF_OLD);
1090 bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF);
1093 /* Dynamic capability. */
1094 if (CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY))
1096 SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV);
1097 stream_putc (s, BGP_OPEN_OPT_CAP);
1098 stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN + 2);
1099 stream_putc (s, CAPABILITY_CODE_DYNAMIC);
1100 stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN);
1103 /* Sending base graceful-restart capability irrespective of the config */
1104 SET_FLAG (peer->cap, PEER_CAP_RESTART_ADV);
1105 stream_putc (s, BGP_OPEN_OPT_CAP);
1106 capp = stream_get_endp (s); /* Set Capability Len Pointer */
1107 stream_putc (s, 0); /* Capability Length */
1108 stream_putc (s, CAPABILITY_CODE_RESTART);
1109 rcapp = stream_get_endp (s); /* Set Restart Capability Len Pointer */
1111 restart_time = peer->bgp->restart_time;
1112 if (peer->bgp->t_startup)
1114 SET_FLAG (restart_time, RESTART_R_BIT);
1115 SET_FLAG (peer->cap, PEER_CAP_RESTART_BIT_ADV);
1117 stream_putw (s, restart_time);
1119 /* Send address-family specific graceful-restart capability only when GR config
1121 if (bgp_flag_check (peer->bgp, BGP_FLAG_GRACEFUL_RESTART))
1123 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
1124 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
1125 if (peer->afc[afi][safi])
1127 stream_putw (s, afi);
1128 stream_putc (s, safi);
1129 stream_putc (s, 0); //Forwarding is not retained as of now.
1133 /* Total Graceful restart capability Len. */
1134 len = stream_get_endp (s) - rcapp - 1;
1135 stream_putc_at (s, rcapp, len);
1137 /* Total Capability Len. */
1138 len = stream_get_endp (s) - capp - 1;
1139 stream_putc_at (s, capp, len);
1141 /* Total Opt Parm Len. */
1142 len = stream_get_endp (s) - cp - 1;
1143 stream_putc_at (s, cp, len);