usbmodeswitch: Updated to v.1.2.6 from shibby's branch.
[tomato.git] / release / src / router / zebra / bgpd / bgp_open.c
blob8047eeae39df5896cb72d76d6f0f643e398c1c0d
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
9 later version.
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
19 02111-1307, USA. */
21 #include <zebra.h>
23 #include "linklist.h"
24 #include "prefix.h"
25 #include "stream.h"
26 #include "thread.h"
27 #include "log.h"
28 #include "command.h"
30 #include "bgpd/bgpd.h"
31 #include "bgpd/bgp_attr.h"
32 #include "bgpd/bgp_debug.h"
33 #include "bgpd/bgp_fsm.h"
34 #include "bgpd/bgp_packet.h"
35 #include "bgpd/bgp_open.h"
36 #include "bgpd/bgp_vty.h"
38 /* BGP-4 Multiprotocol Extentions lead us to the complex world. We can
39 negotiate remote peer supports extentions or not. But if
40 remote-peer doesn't supports negotiation process itself. We would
41 like to do manual configuration.
43 So there is many configurable point. First of all we want set each
44 peer whether we send capability negotiation to the peer or not.
45 Next, if we send capability to the peer we want to set my capabilty
46 inforation at each peer. */
48 void
49 bgp_capability_vty_out (struct vty *vty, struct peer *peer)
51 u_char *pnt;
52 u_char *end;
53 struct capability cap;
55 pnt = peer->notify.data;
56 end = pnt + peer->notify.length;
58 while (pnt < end)
60 memcpy(&cap, pnt, sizeof(struct capability));
62 if (pnt + 2 > end)
63 return;
64 if (pnt + (cap.length + 2) > end)
65 return;
67 if (cap.code == CAPABILITY_CODE_MP)
69 vty_out (vty, " Capability error for: Multi protocol ");
71 switch (ntohs (cap.mpc.afi))
73 case AFI_IP:
74 vty_out (vty, "AFI IPv4, ");
75 break;
76 case AFI_IP6:
77 vty_out (vty, "AFI IPv6, ");
78 break;
79 default:
80 vty_out (vty, "AFI Unknown %d, ", ntohs (cap.mpc.afi));
81 break;
83 switch (cap.mpc.safi)
85 case SAFI_UNICAST:
86 vty_out (vty, "SAFI Unicast");
87 break;
88 case SAFI_MULTICAST:
89 vty_out (vty, "SAFI Multicast");
90 break;
91 case SAFI_UNICAST_MULTICAST:
92 vty_out (vty, "SAFI Unicast Multicast");
93 break;
94 case BGP_SAFI_VPNV4:
95 vty_out (vty, "SAFI MPLS-VPN");
96 break;
97 default:
98 vty_out (vty, "SAFI Unknown %d ", cap.mpc.safi);
99 break;
101 vty_out (vty, "%s", VTY_NEWLINE);
103 else if (cap.code >= 128)
104 vty_out (vty, " Capability error: vendor specific capability code %d",
105 cap.code);
106 else
107 vty_out (vty, " Capability error: unknown capability code %d",
108 cap.code);
110 pnt += cap.length + 2;
114 /* Set negotiated capability value. */
116 bgp_capability_mp (struct peer *peer, struct capability *cap)
118 if (ntohs (cap->mpc.afi) == AFI_IP)
120 if (cap->mpc.safi == SAFI_UNICAST)
122 peer->afc_recv[AFI_IP][SAFI_UNICAST] = 1;
124 if (peer->afc[AFI_IP][SAFI_UNICAST])
125 peer->afc_nego[AFI_IP][SAFI_UNICAST] = 1;
126 else
127 return -1;
129 else if (cap->mpc.safi == SAFI_MULTICAST)
131 peer->afc_recv[AFI_IP][SAFI_MULTICAST] = 1;
133 if (peer->afc[AFI_IP][SAFI_MULTICAST])
134 peer->afc_nego[AFI_IP][SAFI_MULTICAST] = 1;
135 else
136 return -1;
138 else if (cap->mpc.safi == BGP_SAFI_VPNV4)
140 peer->afc_recv[AFI_IP][SAFI_MPLS_VPN] = 1;
142 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
143 peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] = 1;
144 else
145 return -1;
147 else
148 return -1;
150 #ifdef HAVE_IPV6
151 else if (ntohs (cap->mpc.afi) == AFI_IP6)
153 if (cap->mpc.safi == SAFI_UNICAST)
155 peer->afc_recv[AFI_IP6][SAFI_UNICAST] = 1;
157 if (peer->afc[AFI_IP6][SAFI_UNICAST])
158 peer->afc_nego[AFI_IP6][SAFI_UNICAST] = 1;
159 else
160 return -1;
162 else if (cap->mpc.safi == SAFI_MULTICAST)
164 peer->afc_recv[AFI_IP6][SAFI_MULTICAST] = 1;
166 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
167 peer->afc_nego[AFI_IP6][SAFI_MULTICAST] = 1;
168 else
169 return -1;
171 else
172 return -1;
174 #endif /* HAVE_IPV6 */
175 else
177 /* Unknown Address Family. */
178 return -1;
181 return 0;
184 void
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_info ("%s Addr-family %d/%d has ORF type/mode %d/%d not supported",
190 peer->host, afi, safi, type, mode);
194 bgp_capability_orf (struct peer *peer, struct capability *cap,
195 u_char *pnt)
197 afi_t afi = ntohs(cap->mpc.afi);
198 safi_t safi = cap->mpc.safi;
199 u_char number_of_orfs;
200 u_char type;
201 u_char mode;
202 u_int16_t sm_cap = 0; /* capability send-mode receive */
203 u_int16_t rm_cap = 0; /* capability receive-mode receive */
204 int i;
206 /* Check length. */
207 if (cap->length < 7)
209 zlog_info ("%s ORF Capability length error %d",
210 peer->host, cap->length);
211 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
212 return -1;
215 if (BGP_DEBUG (normal, NORMAL))
216 zlog_info ("%s OPEN has ORF CAP(%s) for afi/safi: %u/%u",
217 peer->host, (cap->code == CAPABILITY_CODE_ORF ?
218 "new" : "old"), afi, safi);
220 /* Check AFI and SAFI. */
221 if ((afi != AFI_IP && afi != AFI_IP6)
222 || (safi != SAFI_UNICAST && safi != SAFI_MULTICAST
223 && safi != BGP_SAFI_VPNV4))
225 zlog_info ("%s Addr-family %d/%d not supported. Ignoring the ORF capability",
226 peer->host, afi, safi);
227 return -1;
230 number_of_orfs = *pnt++;
232 for (i = 0 ; i < number_of_orfs ; i++)
234 type = *pnt++;
235 mode = *pnt++;
237 /* ORF Mode error check */
238 if (mode != ORF_MODE_BOTH && mode != ORF_MODE_SEND
239 && mode != ORF_MODE_RECEIVE)
241 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
242 continue;
245 /* ORF Type and afi/safi error check */
246 if (cap->code == CAPABILITY_CODE_ORF)
248 if (type == ORF_TYPE_PREFIX &&
249 ((afi == AFI_IP && safi == SAFI_UNICAST)
250 || (afi == AFI_IP && safi == SAFI_MULTICAST)
251 || (afi == AFI_IP6 && safi == SAFI_UNICAST)))
253 sm_cap = PEER_CAP_ORF_PREFIX_SM_RCV;
254 rm_cap = PEER_CAP_ORF_PREFIX_RM_RCV;
255 if (BGP_DEBUG (normal, NORMAL))
256 zlog_info ("%s OPEN has Prefixlist ORF(%d) capability as %s for afi/safi: %d/%d",
257 peer->host, ORF_TYPE_PREFIX, (mode == ORF_MODE_SEND ? "SEND" :
258 mode == ORF_MODE_RECEIVE ? "RECEIVE" : "BOTH") , afi, safi);
260 else
262 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
263 continue;
266 else if (cap->code == CAPABILITY_CODE_ORF_OLD)
268 if (type == ORF_TYPE_PREFIX_OLD &&
269 ((afi == AFI_IP && safi == SAFI_UNICAST)
270 || (afi == AFI_IP && safi == SAFI_MULTICAST)
271 || (afi == AFI_IP6 && safi == SAFI_UNICAST)))
273 sm_cap = PEER_CAP_ORF_PREFIX_SM_OLD_RCV;
274 rm_cap = PEER_CAP_ORF_PREFIX_RM_OLD_RCV;
275 if (BGP_DEBUG (normal, NORMAL))
276 zlog_info ("%s OPEN has Prefixlist ORF(%d) capability as %s for afi/safi: %d/%d",
277 peer->host, ORF_TYPE_PREFIX_OLD, (mode == ORF_MODE_SEND ? "SEND" :
278 mode == ORF_MODE_RECEIVE ? "RECEIVE" : "BOTH") , afi, safi);
280 else
282 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
283 continue;
286 else
288 bgp_capability_orf_not_support (peer, afi, safi, type, mode);
289 continue;
292 switch (mode)
294 case ORF_MODE_BOTH:
295 SET_FLAG (peer->af_cap[afi][safi], sm_cap);
296 SET_FLAG (peer->af_cap[afi][safi], rm_cap);
297 break;
298 case ORF_MODE_SEND:
299 SET_FLAG (peer->af_cap[afi][safi], sm_cap);
300 break;
301 case ORF_MODE_RECEIVE:
302 SET_FLAG (peer->af_cap[afi][safi], rm_cap);
303 break;
306 return 0;
309 /* Parse given capability. */
311 bgp_capability_parse (struct peer *peer, u_char *pnt, u_char length,
312 u_char **error)
314 int ret;
315 u_char *end;
316 struct capability cap;
318 end = pnt + length;
320 while (pnt < end)
322 afi_t afi;
323 safi_t safi;
325 /* Fetch structure to the byte stream. */
326 memcpy (&cap, pnt, sizeof (struct capability));
328 afi = ntohs(cap.mpc.afi);
329 safi = cap.mpc.safi;
331 if (BGP_DEBUG (normal, NORMAL))
332 zlog_info ("%s OPEN has CAPABILITY code: %d, length %d",
333 peer->host, cap.code, cap.length);
335 /* We need at least capability code and capability length. */
336 if (pnt + 2 > end)
338 zlog_info ("%s Capability length error", peer->host);
339 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
340 return -1;
343 /* Capability length check. */
344 if (pnt + (cap.length + 2) > end)
346 zlog_info ("%s Capability length error", peer->host);
347 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
348 return -1;
351 /* We know MP Capability Code. */
352 if (cap.code == CAPABILITY_CODE_MP)
354 if (BGP_DEBUG (normal, NORMAL))
355 zlog_info ("%s OPEN has MP_EXT CAP for afi/safi: %u/%u",
356 peer->host, afi, safi);
358 /* Ignore capability when override-capability is set. */
359 if (! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
361 /* Set negotiated value. */
362 ret = bgp_capability_mp (peer, &cap);
364 /* Unsupported Capability. */
365 if (ret < 0)
367 /* Store return data. */
368 memcpy (*error, &cap, cap.length + 2);
369 *error += cap.length + 2;
373 else if (cap.code == CAPABILITY_CODE_REFRESH
374 || cap.code == CAPABILITY_CODE_REFRESH_OLD)
376 /* Check length. */
377 if (cap.length != CAPABILITY_CODE_REFRESH_LEN)
379 zlog_info ("%s Route Refresh Capability length error %d",
380 peer->host, cap.length);
381 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
382 return -1;
385 if (BGP_DEBUG (normal, NORMAL))
386 zlog_info ("%s OPEN has ROUTE-REFRESH capability(%s) for all address-families",
387 peer->host,
388 cap.code == CAPABILITY_CODE_REFRESH_OLD ? "old" : "new");
390 /* BGP refresh capability */
391 if (cap.code == CAPABILITY_CODE_REFRESH_OLD)
392 SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV);
393 else
394 SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV);
396 else if (cap.code == CAPABILITY_CODE_ORF
397 || cap.code == CAPABILITY_CODE_ORF_OLD)
398 bgp_capability_orf (peer, &cap, pnt + sizeof (struct capability));
399 else if (cap.code == CAPABILITY_CODE_RESTART)
401 struct graceful_restart_af graf;
402 u_int16_t restart_flag_time;
403 int restart_bit = 0;
404 u_char *restart_pnt;
405 u_char *restart_end;
407 /* Check length. */
408 if (cap.length < CAPABILITY_CODE_RESTART_LEN)
410 zlog_info ("%s Graceful Restart Capability length error %d",
411 peer->host, cap.length);
412 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
413 return -1;
416 SET_FLAG (peer->cap, PEER_CAP_RESTART_RCV);
417 restart_flag_time = ntohs(cap.mpc.afi);
418 if (CHECK_FLAG (restart_flag_time, RESTART_R_BIT))
419 restart_bit = 1;
420 UNSET_FLAG (restart_flag_time, 0xF000);
421 peer->v_gr_restart = restart_flag_time;
423 if (BGP_DEBUG (normal, NORMAL))
425 zlog_info ("%s OPEN has Graceful Restart capability", peer->host);
426 zlog_info ("%s Peer has%srestarted. Restart Time : %d",
427 peer->host, restart_bit ? " " : " not ",
428 peer->v_gr_restart);
431 restart_pnt = pnt + 4;
432 restart_end = pnt + cap.length + 2;
434 while (restart_pnt < restart_end)
436 memcpy (&graf, restart_pnt, sizeof (struct graceful_restart_af));
438 afi = ntohs(graf.afi);
439 safi = graf.safi;
441 if (CHECK_FLAG (graf.flag, RESTART_F_BIT))
442 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV);
444 if (strcmp (afi_safi_print (afi, safi), "Unknown") == 0)
446 if (BGP_DEBUG (normal, NORMAL))
447 zlog_info ("%s Addr-family %d/%d(afi/safi) not supported. Ignore the Graceful Restart capability",
448 peer->host, afi, safi);
450 else if (! peer->afc[afi][safi])
452 if (BGP_DEBUG (normal, NORMAL))
453 zlog_info ("%s Addr-family %d/%d(afi/safi) not enabled. Ignore the Graceful Restart capability",
454 peer->host, afi, safi);
456 else
458 if (BGP_DEBUG (normal, NORMAL))
459 zlog_info ("%s Address family %s is%spreserved", peer->host,
460 afi_safi_print (afi, safi),
461 CHECK_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV)
462 ? " " : " not ");
464 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV);
466 restart_pnt += 4;
469 else if (cap.code == CAPABILITY_CODE_DYNAMIC)
471 /* Check length. */
472 if (cap.length != CAPABILITY_CODE_DYNAMIC_LEN)
474 zlog_info ("%s Dynamic Capability length error %d",
475 peer->host, cap.length);
476 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
477 return -1;
480 if (BGP_DEBUG (normal, NORMAL))
481 zlog_info ("%s OPEN has DYNAMIC capability", peer->host);
483 SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV);
485 else if (cap.code > 128)
487 /* We don't send Notification for unknown vendor specific
488 capabilities. It seems reasonable for now... */
489 zlog_warn ("%s Vendor specific capability %d",
490 peer->host, cap.code);
492 else
494 zlog_warn ("%s unrecognized capability code: %d - ignored",
495 peer->host, cap.code);
496 memcpy (*error, &cap, cap.length + 2);
497 *error += cap.length + 2;
500 pnt += cap.length + 2;
502 return 0;
506 bgp_auth_parse (struct peer *peer, u_char *pnt, size_t length)
508 bgp_notify_send (peer,
509 BGP_NOTIFY_OPEN_ERR,
510 BGP_NOTIFY_OPEN_AUTH_FAILURE);
511 return -1;
515 strict_capability_same (struct peer *peer)
517 int i, j;
519 for (i = AFI_IP; i < AFI_MAX; i++)
520 for (j = SAFI_UNICAST; j < SAFI_MAX; j++)
521 if (peer->afc[i][j] != peer->afc_nego[i][j])
522 return 0;
523 return 1;
526 /* Parse open option */
528 bgp_open_option_parse (struct peer *peer, u_char length, int *capability)
530 int ret;
531 u_char *end;
532 u_char opt_type;
533 u_char opt_length;
534 u_char *pnt;
535 u_char *error;
536 u_char error_data[BGP_MAX_PACKET_SIZE];
538 /* Fetch pointer. */
539 pnt = stream_pnt (peer->ibuf);
541 ret = 0;
542 opt_type = 0;
543 opt_length = 0;
544 end = pnt + length;
545 error = error_data;
547 if (BGP_DEBUG (normal, NORMAL))
548 zlog_info ("%s rcv OPEN w/ OPTION parameter len: %u",
549 peer->host, length);
551 while (pnt < end)
553 /* Check the length. */
554 if (pnt + 2 > end)
556 zlog_info ("%s Option length error", peer->host);
557 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
558 return -1;
561 /* Fetch option type and length. */
562 opt_type = *pnt++;
563 opt_length = *pnt++;
565 /* Option length check. */
566 if (pnt + opt_length > end)
568 zlog_info ("%s Option length error", peer->host);
569 bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0);
570 return -1;
573 if (BGP_DEBUG (normal, NORMAL))
574 zlog_info ("%s rcvd OPEN w/ optional parameter type %u (%s) len %u",
575 peer->host, opt_type,
576 opt_type == BGP_OPEN_OPT_AUTH ? "Authentication" :
577 opt_type == BGP_OPEN_OPT_CAP ? "Capability" : "Unknown",
578 opt_length);
580 switch (opt_type)
582 case BGP_OPEN_OPT_AUTH:
583 ret = bgp_auth_parse (peer, pnt, opt_length);
584 break;
585 case BGP_OPEN_OPT_CAP:
586 ret = bgp_capability_parse (peer, pnt, opt_length, &error);
587 *capability = 1;
588 break;
589 default:
590 bgp_notify_send (peer,
591 BGP_NOTIFY_OPEN_ERR,
592 BGP_NOTIFY_OPEN_UNSUP_PARAM);
593 ret = -1;
594 break;
597 /* Parse error. To accumulate all unsupported capability codes,
598 bgp_capability_parse does not return -1 when encounter
599 unsupported capability code. To detect that, please check
600 error and erro_data pointer, like below. */
601 if (ret < 0)
602 return -1;
604 /* Forward pointer. */
605 pnt += opt_length;
608 /* All OPEN option is parsed. Check capability when strict compare
609 flag is enabled.*/
610 if (CHECK_FLAG (peer->flags, PEER_FLAG_STRICT_CAP_MATCH))
612 /* If Unsupported Capability exists. */
613 if (error != error_data)
615 bgp_notify_send_with_data (peer,
616 BGP_NOTIFY_OPEN_ERR,
617 BGP_NOTIFY_OPEN_UNSUP_CAPBL,
618 error_data, error - error_data);
619 return -1;
622 /* Check local capability does not negotiated with remote
623 peer. */
624 if (! strict_capability_same (peer))
626 bgp_notify_send (peer,
627 BGP_NOTIFY_OPEN_ERR,
628 BGP_NOTIFY_OPEN_UNSUP_CAPBL);
629 return -1;
633 /* Check there is no common capability send Unsupported Capability
634 error. */
635 if (*capability && ! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY))
637 if (! peer->afc_nego[AFI_IP][SAFI_UNICAST]
638 && ! peer->afc_nego[AFI_IP][SAFI_MULTICAST]
639 && ! peer->afc_nego[AFI_IP][SAFI_MPLS_VPN]
640 && ! peer->afc_nego[AFI_IP6][SAFI_UNICAST]
641 && ! peer->afc_nego[AFI_IP6][SAFI_MULTICAST])
643 plog_err (peer->log, "%s [Error] No common capability", peer->host);
645 if (error != error_data)
647 bgp_notify_send_with_data (peer,
648 BGP_NOTIFY_OPEN_ERR,
649 BGP_NOTIFY_OPEN_UNSUP_CAPBL,
650 error_data, error - error_data);
651 else
652 bgp_notify_send (peer,
653 BGP_NOTIFY_OPEN_ERR,
654 BGP_NOTIFY_OPEN_UNSUP_CAPBL);
655 return -1;
658 return 0;
661 void
662 bgp_open_capability_orf (struct stream *s, struct peer *peer,
663 afi_t afi, safi_t safi, u_char code)
665 u_char cap_len;
666 u_char orf_len;
667 unsigned long capp;
668 unsigned long orfp;
669 unsigned long numberp;
670 int number_of_orfs = 0;
672 if (safi == SAFI_MPLS_VPN)
673 safi = BGP_SAFI_VPNV4;
675 stream_putc (s, BGP_OPEN_OPT_CAP);
676 capp = stream_get_putp (s); /* Set Capability Len Pointer */
677 stream_putc (s, 0); /* Capability Length */
678 stream_putc (s, code); /* Capability Code */
679 orfp = stream_get_putp (s); /* Set ORF Len Pointer */
680 stream_putc (s, 0); /* ORF Length */
681 stream_putw (s, afi);
682 stream_putc (s, 0);
683 stream_putc (s, safi);
684 numberp = stream_get_putp (s); /* Set Number Pointer */
685 stream_putc (s, 0); /* Number of ORFs */
687 /* Address Prefix ORF */
688 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
689 || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
691 stream_putc (s, (code == CAPABILITY_CODE_ORF ?
692 ORF_TYPE_PREFIX : ORF_TYPE_PREFIX_OLD));
694 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
695 && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
697 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV);
698 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV);
699 stream_putc (s, ORF_MODE_BOTH);
701 else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM))
703 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV);
704 stream_putc (s, ORF_MODE_SEND);
706 else
708 SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV);
709 stream_putc (s, ORF_MODE_RECEIVE);
711 number_of_orfs++;
714 /* Total Number of ORFs. */
715 stream_putc_at (s, numberp, number_of_orfs);
717 /* Total ORF Len. */
718 orf_len = stream_get_putp (s) - orfp - 1;
719 stream_putc_at (s, orfp, orf_len);
721 /* Total Capability Len. */
722 cap_len = stream_get_putp (s) - capp - 1;
723 stream_putc_at (s, capp, cap_len);
726 /* Fill in capability open option to the packet. */
727 void
728 bgp_open_capability (struct stream *s, struct peer *peer)
730 u_char len;
731 unsigned long cp;
732 afi_t afi;
733 safi_t safi;
735 /* Remember current pointer for Opt Parm Len. */
736 cp = stream_get_putp (s);
738 /* Opt Parm Len. */
739 stream_putc (s, 0);
741 /* Do not send capability. */
742 if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN)
743 || CHECK_FLAG (peer->flags, PEER_FLAG_DONT_CAPABILITY))
744 return;
746 /* IPv4 unicast. */
747 if (peer->afc[AFI_IP][SAFI_UNICAST])
749 peer->afc_adv[AFI_IP][SAFI_UNICAST] = 1;
750 stream_putc (s, BGP_OPEN_OPT_CAP);
751 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
752 stream_putc (s, CAPABILITY_CODE_MP);
753 stream_putc (s, CAPABILITY_CODE_MP_LEN);
754 stream_putw (s, AFI_IP);
755 stream_putc (s, 0);
756 stream_putc (s, SAFI_UNICAST);
758 /* IPv4 multicast. */
759 if (peer->afc[AFI_IP][SAFI_MULTICAST])
761 peer->afc_adv[AFI_IP][SAFI_MULTICAST] = 1;
762 stream_putc (s, BGP_OPEN_OPT_CAP);
763 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
764 stream_putc (s, CAPABILITY_CODE_MP);
765 stream_putc (s, CAPABILITY_CODE_MP_LEN);
766 stream_putw (s, AFI_IP);
767 stream_putc (s, 0);
768 stream_putc (s, SAFI_MULTICAST);
770 /* IPv4 VPN */
771 if (peer->afc[AFI_IP][SAFI_MPLS_VPN])
773 peer->afc_adv[AFI_IP][SAFI_MPLS_VPN] = 1;
774 stream_putc (s, BGP_OPEN_OPT_CAP);
775 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
776 stream_putc (s, CAPABILITY_CODE_MP);
777 stream_putc (s, CAPABILITY_CODE_MP_LEN);
778 stream_putw (s, AFI_IP);
779 stream_putc (s, 0);
780 stream_putc (s, BGP_SAFI_VPNV4);
782 #ifdef HAVE_IPV6
783 /* IPv6 unicast. */
784 if (peer->afc[AFI_IP6][SAFI_UNICAST])
786 peer->afc_adv[AFI_IP6][SAFI_UNICAST] = 1;
787 stream_putc (s, BGP_OPEN_OPT_CAP);
788 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
789 stream_putc (s, CAPABILITY_CODE_MP);
790 stream_putc (s, CAPABILITY_CODE_MP_LEN);
791 stream_putw (s, AFI_IP6);
792 stream_putc (s, 0);
793 stream_putc (s, SAFI_UNICAST);
795 /* IPv6 multicast. */
796 if (peer->afc[AFI_IP6][SAFI_MULTICAST])
798 peer->afc_adv[AFI_IP6][SAFI_MULTICAST] = 1;
799 stream_putc (s, BGP_OPEN_OPT_CAP);
800 stream_putc (s, CAPABILITY_CODE_MP_LEN + 2);
801 stream_putc (s, CAPABILITY_CODE_MP);
802 stream_putc (s, CAPABILITY_CODE_MP_LEN);
803 stream_putw (s, AFI_IP6);
804 stream_putc (s, 0);
805 stream_putc (s, SAFI_MULTICAST);
807 #endif /* HAVE_IPV6 */
809 /* Route refresh. */
810 SET_FLAG (peer->cap, PEER_CAP_REFRESH_ADV);
811 stream_putc (s, BGP_OPEN_OPT_CAP);
812 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2);
813 stream_putc (s, CAPABILITY_CODE_REFRESH_OLD);
814 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
815 stream_putc (s, BGP_OPEN_OPT_CAP);
816 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2);
817 stream_putc (s, CAPABILITY_CODE_REFRESH);
818 stream_putc (s, CAPABILITY_CODE_REFRESH_LEN);
820 /* ORF capability. */
821 for (afi = AFI_IP ; afi < AFI_MAX ; afi++)
822 for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++)
823 if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)
824 || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM))
826 bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF_OLD);
827 bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF);
830 /* Dynamic capability. */
831 if (CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY))
833 SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV);
834 stream_putc (s, BGP_OPEN_OPT_CAP);
835 stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN + 2);
836 stream_putc (s, CAPABILITY_CODE_DYNAMIC);
837 stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN);
840 /* Graceful restart capability */
841 if (bgp_flag_check (peer->bgp, BGP_FLAG_GRACEFUL_RESTART))
843 SET_FLAG (peer->cap, PEER_CAP_RESTART_ADV);
844 stream_putc (s, BGP_OPEN_OPT_CAP);
845 stream_putc (s, CAPABILITY_CODE_RESTART_LEN + 2);
846 stream_putc (s, CAPABILITY_CODE_RESTART);
847 stream_putc (s, CAPABILITY_CODE_RESTART_LEN);
848 stream_putw (s, peer->bgp->restart_time);
851 /* Total Opt Parm Len. */
852 len = stream_get_putp (s) - cp - 1;
853 stream_putc_at (s, cp, len);