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
31 #include "bgpd/bgpd.h"
32 #include "bgpd/bgp_attr.h"
33 #include "bgpd/bgp_debug.h"
34 #include "bgpd/bgp_fsm.h"
35 #include "bgpd/bgp_packet.h"
36 #include "bgpd/bgp_open.h"
37 #include "bgpd/bgp_aspath.h"
38 #include "bgpd/bgp_vty.h"
40 /* BGP-4 Multiprotocol Extentions lead us to the complex world. We can
41 negotiate remote peer supports extentions or not. But if
42 remote-peer doesn't supports negotiation process itself. We would
43 like to do manual configuration.
45 So there is many configurable point. First of all we want set each
46 peer whether we send capability negotiation to the peer or not.
47 Next, if we send capability to the peer we want to set my capabilty
48 inforation at each peer. */
51 bgp_capability_vty_out (struct vty
*vty
, struct peer
*peer
)
55 struct capability_mp_data mpc
;
56 struct capability_header
*hdr
;
58 pnt
= peer
->notify
.data
;
59 end
= pnt
+ peer
->notify
.length
;
63 if (pnt
+ sizeof (struct capability_mp_data
) + 2 > end
)
66 hdr
= (struct capability_header
*)pnt
;
67 if (pnt
+ hdr
->length
+ 2 > end
)
70 memcpy (&mpc
, pnt
+ 2, sizeof(struct capability_mp_data
));
72 if (hdr
->code
== CAPABILITY_CODE_MP
)
74 vty_out (vty
, " Capability error for: Multi protocol ");
76 switch (ntohs (mpc
.afi
))
79 vty_out (vty
, "AFI IPv4, ");
82 vty_out (vty
, "AFI IPv6, ");
85 vty_out (vty
, "AFI Unknown %d, ", ntohs (mpc
.afi
));
91 vty_out (vty
, "SAFI Unicast");
94 vty_out (vty
, "SAFI Multicast");
96 case SAFI_MPLS_LABELED_VPN
:
97 vty_out (vty
, "SAFI MPLS-labeled VPN");
100 vty_out (vty
, "SAFI Unknown %d ", mpc
.safi
);
103 vty_out (vty
, "%s", VTY_NEWLINE
);
105 else if (hdr
->code
>= 128)
106 vty_out (vty
, " Capability error: vendor specific capability code %d",
109 vty_out (vty
, " Capability error: unknown capability code %d",
112 pnt
+= hdr
->length
+ 2;
117 bgp_capability_mp_data (struct stream
*s
, struct capability_mp_data
*mpc
)
119 mpc
->afi
= stream_getw (s
);
120 mpc
->reserved
= stream_getc (s
);
121 mpc
->safi
= stream_getc (s
);
125 bgp_afi_safi_valid_indices (afi_t afi
, safi_t
*safi
)
135 /* BGP MPLS-labeled VPN SAFI isn't contigious with others, remap */
136 case SAFI_MPLS_LABELED_VPN
:
137 *safi
= SAFI_MPLS_VPN
;
144 zlog_debug ("unknown afi/safi (%u/%u)", afi
, *safi
);
149 /* Set negotiated capability value. */
151 bgp_capability_mp (struct peer
*peer
, struct capability_header
*hdr
)
153 struct capability_mp_data mpc
;
154 struct stream
*s
= BGP_INPUT (peer
);
156 bgp_capability_mp_data (s
, &mpc
);
158 if (BGP_DEBUG (normal
, NORMAL
))
159 zlog_debug ("%s OPEN has MP_EXT CAP for afi/safi: %u/%u",
160 peer
->host
, mpc
.afi
, mpc
.safi
);
162 if (!bgp_afi_safi_valid_indices (mpc
.afi
, &mpc
.safi
))
165 /* Now safi remapped, and afi/safi are valid array indices */
166 peer
->afc_recv
[mpc
.afi
][mpc
.safi
] = 1;
168 if (peer
->afc
[mpc
.afi
][mpc
.safi
])
169 peer
->afc_nego
[mpc
.afi
][mpc
.safi
] = 1;
177 bgp_capability_orf_not_support (struct peer
*peer
, afi_t afi
, safi_t safi
,
178 u_char type
, u_char mode
)
180 if (BGP_DEBUG (normal
, NORMAL
))
181 zlog_debug ("%s Addr-family %d/%d has ORF type/mode %d/%d not supported",
182 peer
->host
, afi
, safi
, type
, mode
);
185 static const struct message orf_type_str
[] =
187 { ORF_TYPE_PREFIX
, "Prefixlist" },
188 { ORF_TYPE_PREFIX_OLD
, "Prefixlist (old)" },
190 static const int orf_type_str_max
= array_size(orf_type_str
);
192 static const struct message orf_mode_str
[] =
194 { ORF_MODE_RECEIVE
, "Receive" },
195 { ORF_MODE_SEND
, "Send" },
196 { ORF_MODE_BOTH
, "Both" },
198 static const int orf_mode_str_max
= array_size(orf_mode_str
);
201 bgp_capability_orf_entry (struct peer
*peer
, struct capability_header
*hdr
)
203 struct stream
*s
= BGP_INPUT (peer
);
204 struct capability_orf_entry entry
;
209 u_int16_t sm_cap
= 0; /* capability send-mode receive */
210 u_int16_t rm_cap
= 0; /* capability receive-mode receive */
213 /* ORF Entry header */
214 bgp_capability_mp_data (s
, &entry
.mpc
);
215 entry
.num
= stream_getc (s
);
217 safi
= entry
.mpc
.safi
;
219 if (BGP_DEBUG (normal
, NORMAL
))
220 zlog_debug ("%s ORF Cap entry for afi/safi: %u/%u",
221 peer
->host
, entry
.mpc
.afi
, entry
.mpc
.safi
);
223 /* Check AFI and SAFI. */
224 if (!bgp_afi_safi_valid_indices (entry
.mpc
.afi
, &safi
))
226 zlog_info ("%s Addr-family %d/%d not supported."
227 " Ignoring the ORF capability",
228 peer
->host
, entry
.mpc
.afi
, entry
.mpc
.safi
);
232 /* validate number field */
233 if (sizeof (struct capability_orf_entry
) + (entry
.num
* 2) > hdr
->length
)
235 zlog_info ("%s ORF Capability entry length error,"
236 " Cap length %u, num %u",
237 peer
->host
, hdr
->length
, entry
.num
);
238 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
242 for (i
= 0 ; i
< entry
.num
; i
++)
244 type
= stream_getc(s
);
245 mode
= stream_getc(s
);
247 /* ORF Mode error check */
252 case ORF_MODE_RECEIVE
:
255 bgp_capability_orf_not_support (peer
, afi
, safi
, type
, mode
);
258 /* ORF Type and afi/safi error checks */
259 /* capcode versus type */
262 case CAPABILITY_CODE_ORF
:
265 case ORF_TYPE_PREFIX
:
268 bgp_capability_orf_not_support (peer
, afi
, safi
, type
, mode
);
272 case CAPABILITY_CODE_ORF_OLD
:
275 case ORF_TYPE_PREFIX_OLD
:
278 bgp_capability_orf_not_support (peer
, afi
, safi
, type
, mode
);
283 bgp_capability_orf_not_support (peer
, afi
, safi
, type
, mode
);
288 if (!((afi
== AFI_IP
&& safi
== SAFI_UNICAST
)
289 || (afi
== AFI_IP
&& safi
== SAFI_MULTICAST
)
290 || (afi
== AFI_IP6
&& safi
== SAFI_UNICAST
)))
292 bgp_capability_orf_not_support (peer
, afi
, safi
, type
, mode
);
296 if (BGP_DEBUG (normal
, NORMAL
))
297 zlog_debug ("%s OPEN has %s ORF capability"
298 " as %s for afi/safi: %d/%d",
299 peer
->host
, LOOKUP (orf_type_str
, type
),
300 LOOKUP (orf_mode_str
, mode
),
301 entry
.mpc
.afi
, safi
);
303 if (hdr
->code
== CAPABILITY_CODE_ORF
)
305 sm_cap
= PEER_CAP_ORF_PREFIX_SM_RCV
;
306 rm_cap
= PEER_CAP_ORF_PREFIX_RM_RCV
;
308 else if (hdr
->code
== CAPABILITY_CODE_ORF_OLD
)
310 sm_cap
= PEER_CAP_ORF_PREFIX_SM_OLD_RCV
;
311 rm_cap
= PEER_CAP_ORF_PREFIX_RM_OLD_RCV
;
315 bgp_capability_orf_not_support (peer
, afi
, safi
, type
, mode
);
322 SET_FLAG (peer
->af_cap
[afi
][safi
], sm_cap
);
323 SET_FLAG (peer
->af_cap
[afi
][safi
], rm_cap
);
326 SET_FLAG (peer
->af_cap
[afi
][safi
], sm_cap
);
328 case ORF_MODE_RECEIVE
:
329 SET_FLAG (peer
->af_cap
[afi
][safi
], rm_cap
);
337 bgp_capability_restart (struct peer
*peer
, struct capability_header
*caphdr
)
339 struct stream
*s
= BGP_INPUT (peer
);
340 u_int16_t restart_flag_time
;
342 size_t end
= stream_get_getp (s
) + caphdr
->length
;
344 SET_FLAG (peer
->cap
, PEER_CAP_RESTART_RCV
);
345 restart_flag_time
= stream_getw(s
);
346 if (CHECK_FLAG (restart_flag_time
, RESTART_R_BIT
))
348 SET_FLAG (peer
->cap
, PEER_CAP_RESTART_BIT_RCV
);
351 UNSET_FLAG (restart_flag_time
, 0xF000);
352 peer
->v_gr_restart
= restart_flag_time
;
354 if (BGP_DEBUG (normal
, NORMAL
))
356 zlog_debug ("%s OPEN has Graceful Restart capability", peer
->host
);
357 zlog_debug ("%s Peer has%srestarted. Restart Time : %d",
358 peer
->host
, restart_bit
? " " : " not ",
362 while (stream_get_getp (s
) + 4 <= end
)
364 afi_t afi
= stream_getw (s
);
365 safi_t safi
= stream_getc (s
);
366 u_char flag
= stream_getc (s
);
368 if (!bgp_afi_safi_valid_indices (afi
, &safi
))
370 if (BGP_DEBUG (normal
, NORMAL
))
371 zlog_debug ("%s Addr-family %d/%d(afi/safi) not supported."
372 " Ignore the Graceful Restart capability",
373 peer
->host
, afi
, safi
);
375 else if (!peer
->afc
[afi
][safi
])
377 if (BGP_DEBUG (normal
, NORMAL
))
378 zlog_debug ("%s Addr-family %d/%d(afi/safi) not enabled."
379 " Ignore the Graceful Restart capability",
380 peer
->host
, afi
, safi
);
384 if (BGP_DEBUG (normal
, NORMAL
))
385 zlog_debug ("%s Address family %s is%spreserved", peer
->host
,
386 afi_safi_print (afi
, safi
),
387 CHECK_FLAG (peer
->af_cap
[afi
][safi
],
388 PEER_CAP_RESTART_AF_PRESERVE_RCV
)
391 SET_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_RCV
);
392 if (CHECK_FLAG (flag
, RESTART_F_BIT
))
393 SET_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_RESTART_AF_PRESERVE_RCV
);
401 bgp_capability_as4 (struct peer
*peer
, struct capability_header
*hdr
)
403 SET_FLAG (peer
->cap
, PEER_CAP_AS4_RCV
);
405 if (hdr
->length
!= CAPABILITY_CODE_AS4_LEN
)
407 zlog_err ("%s AS4 capability has incorrect data length %d",
408 peer
->host
, hdr
->length
);
412 as_t as4
= stream_getl (BGP_INPUT(peer
));
414 if (BGP_DEBUG (as4
, AS4
))
415 zlog_debug ("%s [AS4] about to set cap PEER_CAP_AS4_RCV, got as4 %u",
420 static const struct message capcode_str
[] =
422 { CAPABILITY_CODE_MP
, "MultiProtocol Extensions" },
423 { CAPABILITY_CODE_REFRESH
, "Route Refresh" },
424 { CAPABILITY_CODE_ORF
, "Cooperative Route Filtering" },
425 { CAPABILITY_CODE_RESTART
, "Graceful Restart" },
426 { CAPABILITY_CODE_AS4
, "4-octet AS number" },
427 { CAPABILITY_CODE_DYNAMIC
, "Dynamic" },
428 { CAPABILITY_CODE_REFRESH_OLD
, "Route Refresh (Old)" },
429 { CAPABILITY_CODE_ORF_OLD
, "ORF (Old)" },
431 static const int capcode_str_max
= array_size(capcode_str
);
433 /* Minimum sizes for length field of each cap (so not inc. the header) */
434 static const size_t cap_minsizes
[] =
436 [CAPABILITY_CODE_MP
] = sizeof (struct capability_mp_data
),
437 [CAPABILITY_CODE_REFRESH
] = CAPABILITY_CODE_REFRESH_LEN
,
438 [CAPABILITY_CODE_ORF
] = sizeof (struct capability_orf_entry
),
439 [CAPABILITY_CODE_RESTART
] = sizeof (struct capability_gr
),
440 [CAPABILITY_CODE_AS4
] = CAPABILITY_CODE_AS4_LEN
,
441 [CAPABILITY_CODE_DYNAMIC
] = CAPABILITY_CODE_DYNAMIC_LEN
,
442 [CAPABILITY_CODE_REFRESH_OLD
] = CAPABILITY_CODE_REFRESH_LEN
,
443 [CAPABILITY_CODE_ORF_OLD
] = sizeof (struct capability_orf_entry
),
447 * Parse given capability.
448 * XXX: This is reading into a stream, but not using stream API
450 * @param[out] mp_capability Set to 1 on return iff one or more Multiprotocol
451 * capabilities were encountered.
454 bgp_capability_parse (struct peer
*peer
, size_t length
, int *mp_capability
,
458 struct stream
*s
= BGP_INPUT (peer
);
459 size_t end
= stream_get_getp (s
) + length
;
461 assert (STREAM_READABLE (s
) >= length
);
463 while (stream_get_getp (s
) < end
)
466 u_char
*sp
= stream_pnt (s
);
467 struct capability_header caphdr
;
469 /* We need at least capability code and capability length. */
470 if (stream_get_getp(s
) + 2 > end
)
472 zlog_info ("%s Capability length error (< header)", peer
->host
);
473 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
477 caphdr
.code
= stream_getc (s
);
478 caphdr
.length
= stream_getc (s
);
479 start
= stream_get_getp (s
);
481 /* Capability length check sanity check. */
482 if (start
+ caphdr
.length
> end
)
484 zlog_info ("%s Capability length error (< length)", peer
->host
);
485 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
489 if (BGP_DEBUG (normal
, NORMAL
))
490 zlog_debug ("%s OPEN has %s capability (%u), length %u",
492 LOOKUP (capcode_str
, caphdr
.code
),
493 caphdr
.code
, caphdr
.length
);
495 /* Length sanity check, type-specific, for known capabilities */
498 case CAPABILITY_CODE_MP
:
499 case CAPABILITY_CODE_REFRESH
:
500 case CAPABILITY_CODE_REFRESH_OLD
:
501 case CAPABILITY_CODE_ORF
:
502 case CAPABILITY_CODE_ORF_OLD
:
503 case CAPABILITY_CODE_RESTART
:
504 case CAPABILITY_CODE_AS4
:
505 case CAPABILITY_CODE_DYNAMIC
:
507 if (caphdr
.length
< cap_minsizes
[caphdr
.code
])
509 zlog_info ("%s %s Capability length error: got %u,"
510 " expected at least %u",
512 LOOKUP (capcode_str
, caphdr
.code
),
514 (unsigned) cap_minsizes
[caphdr
.code
]);
515 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
518 /* we deliberately ignore unknown codes, see below */
525 case CAPABILITY_CODE_MP
:
529 /* Ignore capability when override-capability is set. */
530 if (! CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
532 /* Set negotiated value. */
533 ret
= bgp_capability_mp (peer
, &caphdr
);
535 /* Unsupported Capability. */
538 /* Store return data. */
539 memcpy (*error
, sp
, caphdr
.length
+ 2);
540 *error
+= caphdr
.length
+ 2;
545 case CAPABILITY_CODE_REFRESH
:
546 case CAPABILITY_CODE_REFRESH_OLD
:
548 /* BGP refresh capability */
549 if (caphdr
.code
== CAPABILITY_CODE_REFRESH_OLD
)
550 SET_FLAG (peer
->cap
, PEER_CAP_REFRESH_OLD_RCV
);
552 SET_FLAG (peer
->cap
, PEER_CAP_REFRESH_NEW_RCV
);
555 case CAPABILITY_CODE_ORF
:
556 case CAPABILITY_CODE_ORF_OLD
:
557 if (bgp_capability_orf_entry (peer
, &caphdr
))
560 case CAPABILITY_CODE_RESTART
:
561 if (bgp_capability_restart (peer
, &caphdr
))
564 case CAPABILITY_CODE_DYNAMIC
:
565 SET_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_RCV
);
567 case CAPABILITY_CODE_AS4
:
568 /* Already handled as a special-case parsing of the capabilities
569 * at the beginning of OPEN processing. So we care not a jot
570 * for the value really, only error case.
572 if (!bgp_capability_as4 (peer
, &caphdr
))
576 if (caphdr
.code
> 128)
578 /* We don't send Notification for unknown vendor specific
579 capabilities. It seems reasonable for now... */
580 zlog_warn ("%s Vendor specific capability %d",
581 peer
->host
, caphdr
.code
);
585 zlog_warn ("%s unrecognized capability code: %d - ignored",
586 peer
->host
, caphdr
.code
);
587 memcpy (*error
, sp
, caphdr
.length
+ 2);
588 *error
+= caphdr
.length
+ 2;
591 if (stream_get_getp(s
) != (start
+ caphdr
.length
))
593 if (stream_get_getp(s
) > (start
+ caphdr
.length
))
594 zlog_warn ("%s Cap-parser for %s read past cap-length, %u!",
595 peer
->host
, LOOKUP (capcode_str
, caphdr
.code
),
597 stream_set_getp (s
, start
+ caphdr
.length
);
604 bgp_auth_parse (struct peer
*peer
, size_t length
)
606 bgp_notify_send (peer
,
608 BGP_NOTIFY_OPEN_AUTH_FAILURE
);
613 strict_capability_same (struct peer
*peer
)
617 for (i
= AFI_IP
; i
< AFI_MAX
; i
++)
618 for (j
= SAFI_UNICAST
; j
< SAFI_MAX
; j
++)
619 if (peer
->afc
[i
][j
] != peer
->afc_nego
[i
][j
])
624 /* peek into option, stores ASN to *as4 if the AS4 capability was found.
625 * Returns 0 if no as4 found, as4cap value otherwise.
628 peek_for_as4_capability (struct peer
*peer
, u_char length
)
630 struct stream
*s
= BGP_INPUT (peer
);
631 size_t orig_getp
= stream_get_getp (s
);
632 size_t end
= orig_getp
+ length
;
635 /* The full capability parser will better flag the error.. */
636 if (STREAM_READABLE(s
) < length
)
639 if (BGP_DEBUG (as4
, AS4
))
640 zlog_info ("%s [AS4] rcv OPEN w/ OPTION parameter len: %u,"
643 /* the error cases we DONT handle, we ONLY try to read as4 out of
644 * correctly formatted options.
646 while (stream_get_getp(s
) < end
)
651 /* Check the length. */
652 if (stream_get_getp (s
) + 2 > end
)
655 /* Fetch option type and length. */
656 opt_type
= stream_getc (s
);
657 opt_length
= stream_getc (s
);
659 /* Option length check. */
660 if (stream_get_getp (s
) + opt_length
> end
)
663 if (opt_type
== BGP_OPEN_OPT_CAP
)
665 unsigned long capd_start
= stream_get_getp (s
);
666 unsigned long capd_end
= capd_start
+ opt_length
;
668 assert (capd_end
<= end
);
670 while (stream_get_getp (s
) < capd_end
)
672 struct capability_header hdr
;
674 if (stream_get_getp (s
) + 2 > capd_end
)
677 hdr
.code
= stream_getc (s
);
678 hdr
.length
= stream_getc (s
);
680 if ((stream_get_getp(s
) + hdr
.length
) > capd_end
)
683 if (hdr
.code
== CAPABILITY_CODE_AS4
)
685 if (BGP_DEBUG (as4
, AS4
))
686 zlog_info ("[AS4] found AS4 capability, about to parse");
687 as4
= bgp_capability_as4 (peer
, &hdr
);
691 stream_forward_getp (s
, hdr
.length
);
697 stream_set_getp (s
, orig_getp
);
704 * @param[out] mp_capability @see bgp_capability_parse() for semantics.
707 bgp_open_option_parse (struct peer
*peer
, u_char length
, int *mp_capability
)
711 u_char error_data
[BGP_MAX_PACKET_SIZE
];
712 struct stream
*s
= BGP_INPUT(peer
);
713 size_t end
= stream_get_getp (s
) + length
;
718 if (BGP_DEBUG (normal
, NORMAL
))
719 zlog_debug ("%s rcv OPEN w/ OPTION parameter len: %u",
722 while (stream_get_getp(s
) < end
)
727 /* Must have at least an OPEN option header */
728 if (STREAM_READABLE(s
) < 2)
730 zlog_info ("%s Option length error", peer
->host
);
731 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
735 /* Fetch option type and length. */
736 opt_type
= stream_getc (s
);
737 opt_length
= stream_getc (s
);
739 /* Option length check. */
740 if (STREAM_READABLE (s
) < opt_length
)
742 zlog_info ("%s Option length error", peer
->host
);
743 bgp_notify_send (peer
, BGP_NOTIFY_CEASE
, 0);
747 if (BGP_DEBUG (normal
, NORMAL
))
748 zlog_debug ("%s rcvd OPEN w/ optional parameter type %u (%s) len %u",
749 peer
->host
, opt_type
,
750 opt_type
== BGP_OPEN_OPT_AUTH
? "Authentication" :
751 opt_type
== BGP_OPEN_OPT_CAP
? "Capability" : "Unknown",
756 case BGP_OPEN_OPT_AUTH
:
757 ret
= bgp_auth_parse (peer
, opt_length
);
759 case BGP_OPEN_OPT_CAP
:
760 ret
= bgp_capability_parse (peer
, opt_length
, mp_capability
, &error
);
763 bgp_notify_send (peer
,
765 BGP_NOTIFY_OPEN_UNSUP_PARAM
);
770 /* Parse error. To accumulate all unsupported capability codes,
771 bgp_capability_parse does not return -1 when encounter
772 unsupported capability code. To detect that, please check
773 error and erro_data pointer, like below. */
778 /* All OPEN option is parsed. Check capability when strict compare
780 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_STRICT_CAP_MATCH
))
782 /* If Unsupported Capability exists. */
783 if (error
!= error_data
)
785 bgp_notify_send_with_data (peer
,
787 BGP_NOTIFY_OPEN_UNSUP_CAPBL
,
788 error_data
, error
- error_data
);
792 /* Check local capability does not negotiated with remote
794 if (! strict_capability_same (peer
))
796 bgp_notify_send (peer
,
798 BGP_NOTIFY_OPEN_UNSUP_CAPBL
);
803 /* Check there are no common AFI/SAFIs and send Unsupported Capability
805 if (*mp_capability
&&
806 ! CHECK_FLAG (peer
->flags
, PEER_FLAG_OVERRIDE_CAPABILITY
))
808 if (! peer
->afc_nego
[AFI_IP
][SAFI_UNICAST
]
809 && ! peer
->afc_nego
[AFI_IP
][SAFI_MULTICAST
]
810 && ! peer
->afc_nego
[AFI_IP
][SAFI_MPLS_VPN
]
811 && ! peer
->afc_nego
[AFI_IP6
][SAFI_UNICAST
]
812 && ! peer
->afc_nego
[AFI_IP6
][SAFI_MULTICAST
])
814 plog_err (peer
->log
, "%s [Error] Configured AFI/SAFIs do not "
815 "overlap with received MP capabilities",
818 if (error
!= error_data
)
819 bgp_notify_send_with_data (peer
,
821 BGP_NOTIFY_OPEN_UNSUP_CAPBL
,
822 error_data
, error
- error_data
);
824 bgp_notify_send (peer
,
826 BGP_NOTIFY_OPEN_UNSUP_CAPBL
);
834 bgp_open_capability_orf (struct stream
*s
, struct peer
*peer
,
835 afi_t afi
, safi_t safi
, u_char code
)
841 unsigned long numberp
;
842 int number_of_orfs
= 0;
844 if (safi
== SAFI_MPLS_VPN
)
845 safi
= SAFI_MPLS_LABELED_VPN
;
847 stream_putc (s
, BGP_OPEN_OPT_CAP
);
848 capp
= stream_get_endp (s
); /* Set Capability Len Pointer */
849 stream_putc (s
, 0); /* Capability Length */
850 stream_putc (s
, code
); /* Capability Code */
851 orfp
= stream_get_endp (s
); /* Set ORF Len Pointer */
852 stream_putc (s
, 0); /* ORF Length */
853 stream_putw (s
, afi
);
855 stream_putc (s
, safi
);
856 numberp
= stream_get_endp (s
); /* Set Number Pointer */
857 stream_putc (s
, 0); /* Number of ORFs */
859 /* Address Prefix ORF */
860 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ORF_PREFIX_SM
)
861 || CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ORF_PREFIX_RM
))
863 stream_putc (s
, (code
== CAPABILITY_CODE_ORF
?
864 ORF_TYPE_PREFIX
: ORF_TYPE_PREFIX_OLD
));
866 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ORF_PREFIX_SM
)
867 && CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ORF_PREFIX_RM
))
869 SET_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_ADV
);
870 SET_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
);
871 stream_putc (s
, ORF_MODE_BOTH
);
873 else if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ORF_PREFIX_SM
))
875 SET_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_SM_ADV
);
876 stream_putc (s
, ORF_MODE_SEND
);
880 SET_FLAG (peer
->af_cap
[afi
][safi
], PEER_CAP_ORF_PREFIX_RM_ADV
);
881 stream_putc (s
, ORF_MODE_RECEIVE
);
886 /* Total Number of ORFs. */
887 stream_putc_at (s
, numberp
, number_of_orfs
);
890 orf_len
= stream_get_endp (s
) - orfp
- 1;
891 stream_putc_at (s
, orfp
, orf_len
);
893 /* Total Capability Len. */
894 cap_len
= stream_get_endp (s
) - capp
- 1;
895 stream_putc_at (s
, capp
, cap_len
);
898 /* Fill in capability open option to the packet. */
900 bgp_open_capability (struct stream
*s
, struct peer
*peer
)
903 unsigned long cp
, capp
, rcapp
;
907 u_int32_t restart_time
;
909 /* Remember current pointer for Opt Parm Len. */
910 cp
= stream_get_endp (s
);
915 /* Do not send capability. */
916 if (! CHECK_FLAG (peer
->sflags
, PEER_STATUS_CAPABILITY_OPEN
)
917 || CHECK_FLAG (peer
->flags
, PEER_FLAG_DONT_CAPABILITY
))
921 if (peer
->afc
[AFI_IP
][SAFI_UNICAST
])
923 peer
->afc_adv
[AFI_IP
][SAFI_UNICAST
] = 1;
924 stream_putc (s
, BGP_OPEN_OPT_CAP
);
925 stream_putc (s
, CAPABILITY_CODE_MP_LEN
+ 2);
926 stream_putc (s
, CAPABILITY_CODE_MP
);
927 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
928 stream_putw (s
, AFI_IP
);
930 stream_putc (s
, SAFI_UNICAST
);
932 /* IPv4 multicast. */
933 if (peer
->afc
[AFI_IP
][SAFI_MULTICAST
])
935 peer
->afc_adv
[AFI_IP
][SAFI_MULTICAST
] = 1;
936 stream_putc (s
, BGP_OPEN_OPT_CAP
);
937 stream_putc (s
, CAPABILITY_CODE_MP_LEN
+ 2);
938 stream_putc (s
, CAPABILITY_CODE_MP
);
939 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
940 stream_putw (s
, AFI_IP
);
942 stream_putc (s
, SAFI_MULTICAST
);
945 if (peer
->afc
[AFI_IP
][SAFI_MPLS_VPN
])
947 peer
->afc_adv
[AFI_IP
][SAFI_MPLS_VPN
] = 1;
948 stream_putc (s
, BGP_OPEN_OPT_CAP
);
949 stream_putc (s
, CAPABILITY_CODE_MP_LEN
+ 2);
950 stream_putc (s
, CAPABILITY_CODE_MP
);
951 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
952 stream_putw (s
, AFI_IP
);
954 stream_putc (s
, SAFI_MPLS_LABELED_VPN
);
958 if (peer
->afc
[AFI_IP6
][SAFI_UNICAST
])
960 peer
->afc_adv
[AFI_IP6
][SAFI_UNICAST
] = 1;
961 stream_putc (s
, BGP_OPEN_OPT_CAP
);
962 stream_putc (s
, CAPABILITY_CODE_MP_LEN
+ 2);
963 stream_putc (s
, CAPABILITY_CODE_MP
);
964 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
965 stream_putw (s
, AFI_IP6
);
967 stream_putc (s
, SAFI_UNICAST
);
969 /* IPv6 multicast. */
970 if (peer
->afc
[AFI_IP6
][SAFI_MULTICAST
])
972 peer
->afc_adv
[AFI_IP6
][SAFI_MULTICAST
] = 1;
973 stream_putc (s
, BGP_OPEN_OPT_CAP
);
974 stream_putc (s
, CAPABILITY_CODE_MP_LEN
+ 2);
975 stream_putc (s
, CAPABILITY_CODE_MP
);
976 stream_putc (s
, CAPABILITY_CODE_MP_LEN
);
977 stream_putw (s
, AFI_IP6
);
979 stream_putc (s
, SAFI_MULTICAST
);
981 #endif /* HAVE_IPV6 */
984 SET_FLAG (peer
->cap
, PEER_CAP_REFRESH_ADV
);
985 stream_putc (s
, BGP_OPEN_OPT_CAP
);
986 stream_putc (s
, CAPABILITY_CODE_REFRESH_LEN
+ 2);
987 stream_putc (s
, CAPABILITY_CODE_REFRESH_OLD
);
988 stream_putc (s
, CAPABILITY_CODE_REFRESH_LEN
);
989 stream_putc (s
, BGP_OPEN_OPT_CAP
);
990 stream_putc (s
, CAPABILITY_CODE_REFRESH_LEN
+ 2);
991 stream_putc (s
, CAPABILITY_CODE_REFRESH
);
992 stream_putc (s
, CAPABILITY_CODE_REFRESH_LEN
);
995 SET_FLAG (peer
->cap
, PEER_CAP_AS4_ADV
);
996 stream_putc (s
, BGP_OPEN_OPT_CAP
);
997 stream_putc (s
, CAPABILITY_CODE_AS4_LEN
+ 2);
998 stream_putc (s
, CAPABILITY_CODE_AS4
);
999 stream_putc (s
, CAPABILITY_CODE_AS4_LEN
);
1000 if ( peer
->change_local_as
)
1001 local_as
= peer
->change_local_as
;
1003 local_as
= peer
->local_as
;
1004 stream_putl (s
, local_as
);
1006 /* ORF capability. */
1007 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1008 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1009 if (CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ORF_PREFIX_SM
)
1010 || CHECK_FLAG (peer
->af_flags
[afi
][safi
], PEER_FLAG_ORF_PREFIX_RM
))
1012 bgp_open_capability_orf (s
, peer
, afi
, safi
, CAPABILITY_CODE_ORF_OLD
);
1013 bgp_open_capability_orf (s
, peer
, afi
, safi
, CAPABILITY_CODE_ORF
);
1016 /* Dynamic capability. */
1017 if (CHECK_FLAG (peer
->flags
, PEER_FLAG_DYNAMIC_CAPABILITY
))
1019 SET_FLAG (peer
->cap
, PEER_CAP_DYNAMIC_ADV
);
1020 stream_putc (s
, BGP_OPEN_OPT_CAP
);
1021 stream_putc (s
, CAPABILITY_CODE_DYNAMIC_LEN
+ 2);
1022 stream_putc (s
, CAPABILITY_CODE_DYNAMIC
);
1023 stream_putc (s
, CAPABILITY_CODE_DYNAMIC_LEN
);
1026 /* Sending base graceful-restart capability irrespective of the config */
1027 SET_FLAG (peer
->cap
, PEER_CAP_RESTART_ADV
);
1028 stream_putc (s
, BGP_OPEN_OPT_CAP
);
1029 capp
= stream_get_endp (s
); /* Set Capability Len Pointer */
1030 stream_putc (s
, 0); /* Capability Length */
1031 stream_putc (s
, CAPABILITY_CODE_RESTART
);
1032 rcapp
= stream_get_endp (s
); /* Set Restart Capability Len Pointer */
1034 restart_time
= peer
->bgp
->restart_time
;
1035 if (peer
->bgp
->t_startup
)
1037 SET_FLAG (restart_time
, RESTART_R_BIT
);
1038 SET_FLAG (peer
->cap
, PEER_CAP_RESTART_BIT_ADV
);
1040 stream_putw (s
, restart_time
);
1042 /* Send address-family specific graceful-restart capability only when GR config
1044 if (bgp_flag_check (peer
->bgp
, BGP_FLAG_GRACEFUL_RESTART
))
1046 for (afi
= AFI_IP
; afi
< AFI_MAX
; afi
++)
1047 for (safi
= SAFI_UNICAST
; safi
< SAFI_MAX
; safi
++)
1048 if (peer
->afc
[afi
][safi
])
1050 stream_putw (s
, afi
);
1051 stream_putc (s
, safi
);
1052 stream_putc (s
, 0); //Forwarding is not retained as of now.
1056 /* Total Graceful restart capability Len. */
1057 len
= stream_get_endp (s
) - rcapp
- 1;
1058 stream_putc_at (s
, rcapp
, len
);
1060 /* Total Capability Len. */
1061 len
= stream_get_endp (s
) - capp
- 1;
1062 stream_putc_at (s
, capp
, len
);
1064 /* Total Opt Parm Len. */
1065 len
= stream_get_endp (s
) - cp
- 1;
1066 stream_putc_at (s
, cp
, len
);