paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* BGP open message handling |
| 2 | Copyright (C) 1998, 1999 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "linklist.h" |
| 24 | #include "prefix.h" |
| 25 | #include "stream.h" |
| 26 | #include "thread.h" |
| 27 | #include "log.h" |
| 28 | #include "command.h" |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 29 | #include "memory.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 30 | |
| 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" |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 37 | #include "bgpd/bgp_aspath.h" |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 38 | #include "bgpd/bgp_vty.h" |
| 39 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 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. |
| 44 | |
| 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. */ |
| 49 | |
| 50 | void |
| 51 | bgp_capability_vty_out (struct vty *vty, struct peer *peer) |
| 52 | { |
paul | 5228ad2 | 2004-06-04 17:58:18 +0000 | [diff] [blame] | 53 | char *pnt; |
| 54 | char *end; |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 55 | struct capability_mp_data mpc; |
| 56 | struct capability_header *hdr; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 57 | |
| 58 | pnt = peer->notify.data; |
| 59 | end = pnt + peer->notify.length; |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 60 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 61 | while (pnt < end) |
| 62 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 63 | if (pnt + sizeof (struct capability_mp_data) + 2 > end) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 64 | return; |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 65 | |
| 66 | hdr = (struct capability_header *)pnt; |
| 67 | if (pnt + hdr->length + 2 > end) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 68 | return; |
| 69 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 70 | memcpy (&mpc, pnt + 2, sizeof(struct capability_mp_data)); |
| 71 | |
| 72 | if (hdr->code == CAPABILITY_CODE_MP) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 73 | { |
| 74 | vty_out (vty, " Capability error for: Multi protocol "); |
| 75 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 76 | switch (ntohs (mpc.afi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 77 | { |
| 78 | case AFI_IP: |
| 79 | vty_out (vty, "AFI IPv4, "); |
| 80 | break; |
| 81 | case AFI_IP6: |
| 82 | vty_out (vty, "AFI IPv6, "); |
| 83 | break; |
| 84 | default: |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 85 | vty_out (vty, "AFI Unknown %d, ", ntohs (mpc.afi)); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 86 | break; |
| 87 | } |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 88 | switch (mpc.safi) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 89 | { |
| 90 | case SAFI_UNICAST: |
| 91 | vty_out (vty, "SAFI Unicast"); |
| 92 | break; |
| 93 | case SAFI_MULTICAST: |
| 94 | vty_out (vty, "SAFI Multicast"); |
| 95 | break; |
Denis Ovsienko | 42e6d74 | 2011-07-14 12:36:19 +0400 | [diff] [blame] | 96 | case SAFI_MPLS_LABELED_VPN: |
| 97 | vty_out (vty, "SAFI MPLS-labeled VPN"); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 98 | break; |
| 99 | default: |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 100 | vty_out (vty, "SAFI Unknown %d ", mpc.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 101 | break; |
| 102 | } |
| 103 | vty_out (vty, "%s", VTY_NEWLINE); |
| 104 | } |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 105 | else if (hdr->code >= 128) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 106 | vty_out (vty, " Capability error: vendor specific capability code %d", |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 107 | hdr->code); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 108 | else |
| 109 | vty_out (vty, " Capability error: unknown capability code %d", |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 110 | hdr->code); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 111 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 112 | pnt += hdr->length + 2; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 113 | } |
| 114 | } |
| 115 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 116 | static void |
| 117 | bgp_capability_mp_data (struct stream *s, struct capability_mp_data *mpc) |
| 118 | { |
| 119 | mpc->afi = stream_getw (s); |
| 120 | mpc->reserved = stream_getc (s); |
| 121 | mpc->safi = stream_getc (s); |
| 122 | } |
| 123 | |
| 124 | int |
| 125 | bgp_afi_safi_valid_indices (afi_t afi, safi_t *safi) |
| 126 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 127 | switch (afi) |
| 128 | { |
| 129 | case AFI_IP: |
| 130 | #ifdef HAVE_IPV6 |
| 131 | case AFI_IP6: |
| 132 | #endif |
| 133 | switch (*safi) |
| 134 | { |
Denis Ovsienko | 42e6d74 | 2011-07-14 12:36:19 +0400 | [diff] [blame] | 135 | /* BGP MPLS-labeled VPN SAFI isn't contigious with others, remap */ |
| 136 | case SAFI_MPLS_LABELED_VPN: |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 137 | *safi = SAFI_MPLS_VPN; |
| 138 | case SAFI_UNICAST: |
| 139 | case SAFI_MULTICAST: |
| 140 | case SAFI_MPLS_VPN: |
| 141 | return 1; |
| 142 | } |
| 143 | } |
| 144 | zlog_debug ("unknown afi/safi (%u/%u)", afi, *safi); |
| 145 | |
| 146 | return 0; |
| 147 | } |
| 148 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 149 | /* Set negotiated capability value. */ |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 150 | static int |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 151 | bgp_capability_mp (struct peer *peer, struct capability_header *hdr) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 153 | struct capability_mp_data mpc; |
| 154 | struct stream *s = BGP_INPUT (peer); |
| 155 | |
| 156 | bgp_capability_mp_data (s, &mpc); |
| 157 | |
| 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); |
| 161 | |
| 162 | if (!bgp_afi_safi_valid_indices (mpc.afi, &mpc.safi)) |
| 163 | return -1; |
| 164 | |
| 165 | /* Now safi remapped, and afi/safi are valid array indices */ |
| 166 | peer->afc_recv[mpc.afi][mpc.safi] = 1; |
| 167 | |
| 168 | if (peer->afc[mpc.afi][mpc.safi]) |
Paul Jakma | e08286b | 2007-09-18 12:11:26 +0000 | [diff] [blame] | 169 | peer->afc_nego[mpc.afi][mpc.safi] = 1; |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 170 | else |
| 171 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | |
| 173 | return 0; |
| 174 | } |
| 175 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 176 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 177 | bgp_capability_orf_not_support (struct peer *peer, afi_t afi, safi_t safi, |
| 178 | u_char type, u_char mode) |
| 179 | { |
| 180 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 8325cd7 | 2004-12-08 20:47:40 +0000 | [diff] [blame] | 181 | zlog_debug ("%s Addr-family %d/%d has ORF type/mode %d/%d not supported", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 182 | peer->host, afi, safi, type, mode); |
| 183 | } |
| 184 | |
Stephen Hemminger | 8f5abac | 2009-05-15 10:16:34 -0700 | [diff] [blame] | 185 | static const struct message orf_type_str[] = |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 186 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 187 | { ORF_TYPE_PREFIX, "Prefixlist" }, |
| 188 | { ORF_TYPE_PREFIX_OLD, "Prefixlist (old)" }, |
| 189 | }; |
Stephen Hemminger | 8f5abac | 2009-05-15 10:16:34 -0700 | [diff] [blame] | 190 | static const int orf_type_str_max |
| 191 | = sizeof(orf_type_str)/sizeof(orf_type_str[0]); |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 192 | |
Stephen Hemminger | 8f5abac | 2009-05-15 10:16:34 -0700 | [diff] [blame] | 193 | static const struct message orf_mode_str[] = |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 194 | { |
| 195 | { ORF_MODE_RECEIVE, "Receive" }, |
| 196 | { ORF_MODE_SEND, "Send" }, |
| 197 | { ORF_MODE_BOTH, "Both" }, |
| 198 | }; |
Stephen Hemminger | 8f5abac | 2009-05-15 10:16:34 -0700 | [diff] [blame] | 199 | static const int orf_mode_str_max |
| 200 | = sizeof(orf_mode_str)/sizeof(orf_mode_str[0]); |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 201 | |
| 202 | static int |
| 203 | bgp_capability_orf_entry (struct peer *peer, struct capability_header *hdr) |
| 204 | { |
| 205 | struct stream *s = BGP_INPUT (peer); |
| 206 | struct capability_orf_entry entry; |
| 207 | afi_t afi; |
| 208 | safi_t safi; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 209 | u_char type; |
| 210 | u_char mode; |
| 211 | u_int16_t sm_cap = 0; /* capability send-mode receive */ |
| 212 | u_int16_t rm_cap = 0; /* capability receive-mode receive */ |
| 213 | int i; |
| 214 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 215 | /* ORF Entry header */ |
| 216 | bgp_capability_mp_data (s, &entry.mpc); |
| 217 | entry.num = stream_getc (s); |
| 218 | afi = entry.mpc.afi; |
| 219 | safi = entry.mpc.safi; |
| 220 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 221 | if (BGP_DEBUG (normal, NORMAL)) |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 222 | zlog_debug ("%s ORF Cap entry for afi/safi: %u/%u", |
| 223 | peer->host, entry.mpc.afi, entry.mpc.safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 224 | |
| 225 | /* Check AFI and SAFI. */ |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 226 | if (!bgp_afi_safi_valid_indices (entry.mpc.afi, &safi)) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 227 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 228 | zlog_info ("%s Addr-family %d/%d not supported." |
| 229 | " Ignoring the ORF capability", |
| 230 | peer->host, entry.mpc.afi, entry.mpc.safi); |
| 231 | return 0; |
| 232 | } |
| 233 | |
| 234 | /* validate number field */ |
| 235 | if (sizeof (struct capability_orf_entry) + (entry.num * 2) > hdr->length) |
| 236 | { |
| 237 | zlog_info ("%s ORF Capability entry length error," |
| 238 | " Cap length %u, num %u", |
| 239 | peer->host, hdr->length, entry.num); |
| 240 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 244 | for (i = 0 ; i < entry.num ; i++) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 245 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 246 | type = stream_getc(s); |
| 247 | mode = stream_getc(s); |
| 248 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 249 | /* ORF Mode error check */ |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 250 | switch (mode) |
| 251 | { |
| 252 | case ORF_MODE_BOTH: |
| 253 | case ORF_MODE_SEND: |
| 254 | case ORF_MODE_RECEIVE: |
| 255 | break; |
| 256 | default: |
| 257 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 258 | continue; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 259 | } |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 260 | /* ORF Type and afi/safi error checks */ |
| 261 | /* capcode versus type */ |
| 262 | switch (hdr->code) |
| 263 | { |
| 264 | case CAPABILITY_CODE_ORF: |
| 265 | switch (type) |
| 266 | { |
| 267 | case ORF_TYPE_PREFIX: |
| 268 | break; |
| 269 | default: |
| 270 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 271 | continue; |
| 272 | } |
| 273 | break; |
| 274 | case CAPABILITY_CODE_ORF_OLD: |
| 275 | switch (type) |
| 276 | { |
| 277 | case ORF_TYPE_PREFIX_OLD: |
| 278 | break; |
| 279 | default: |
| 280 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 281 | continue; |
| 282 | } |
| 283 | break; |
| 284 | default: |
| 285 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 286 | continue; |
| 287 | } |
| 288 | |
| 289 | /* AFI vs SAFI */ |
| 290 | if (!((afi == AFI_IP && safi == SAFI_UNICAST) |
| 291 | || (afi == AFI_IP && safi == SAFI_MULTICAST) |
| 292 | || (afi == AFI_IP6 && safi == SAFI_UNICAST))) |
| 293 | { |
| 294 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 295 | continue; |
| 296 | } |
| 297 | |
| 298 | if (BGP_DEBUG (normal, NORMAL)) |
| 299 | zlog_debug ("%s OPEN has %s ORF capability" |
| 300 | " as %s for afi/safi: %d/%d", |
| 301 | peer->host, LOOKUP (orf_type_str, type), |
| 302 | LOOKUP (orf_mode_str, mode), |
| 303 | entry.mpc.afi, safi); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 304 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 305 | if (hdr->code == CAPABILITY_CODE_ORF) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 306 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 307 | sm_cap = PEER_CAP_ORF_PREFIX_SM_RCV; |
| 308 | rm_cap = PEER_CAP_ORF_PREFIX_RM_RCV; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 309 | } |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 310 | else if (hdr->code == CAPABILITY_CODE_ORF_OLD) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 311 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 312 | sm_cap = PEER_CAP_ORF_PREFIX_SM_OLD_RCV; |
| 313 | rm_cap = PEER_CAP_ORF_PREFIX_RM_OLD_RCV; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 314 | } |
| 315 | else |
| 316 | { |
| 317 | bgp_capability_orf_not_support (peer, afi, safi, type, mode); |
| 318 | continue; |
| 319 | } |
| 320 | |
| 321 | switch (mode) |
| 322 | { |
| 323 | case ORF_MODE_BOTH: |
| 324 | SET_FLAG (peer->af_cap[afi][safi], sm_cap); |
| 325 | SET_FLAG (peer->af_cap[afi][safi], rm_cap); |
| 326 | break; |
| 327 | case ORF_MODE_SEND: |
| 328 | SET_FLAG (peer->af_cap[afi][safi], sm_cap); |
| 329 | break; |
| 330 | case ORF_MODE_RECEIVE: |
| 331 | SET_FLAG (peer->af_cap[afi][safi], rm_cap); |
| 332 | break; |
| 333 | } |
| 334 | } |
| 335 | return 0; |
| 336 | } |
| 337 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 338 | static int |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 339 | bgp_capability_orf (struct peer *peer, struct capability_header *hdr) |
| 340 | { |
| 341 | struct stream *s = BGP_INPUT (peer); |
| 342 | size_t end = stream_get_getp (s) + hdr->length; |
| 343 | |
| 344 | assert (stream_get_getp(s) + sizeof(struct capability_orf_entry) <= end); |
| 345 | |
| 346 | /* We must have at least one ORF entry, as the caller has already done |
| 347 | * minimum length validation for the capability code - for ORF there must |
| 348 | * at least one ORF entry (header and unknown number of pairs of bytes). |
| 349 | */ |
| 350 | do |
| 351 | { |
| 352 | if (bgp_capability_orf_entry (peer, hdr) == -1) |
| 353 | return -1; |
| 354 | } |
| 355 | while (stream_get_getp(s) + sizeof(struct capability_orf_entry) < end); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static int |
| 361 | bgp_capability_restart (struct peer *peer, struct capability_header *caphdr) |
| 362 | { |
| 363 | struct stream *s = BGP_INPUT (peer); |
| 364 | u_int16_t restart_flag_time; |
| 365 | int restart_bit = 0; |
| 366 | size_t end = stream_get_getp (s) + caphdr->length; |
| 367 | |
| 368 | SET_FLAG (peer->cap, PEER_CAP_RESTART_RCV); |
| 369 | restart_flag_time = stream_getw(s); |
| 370 | if (CHECK_FLAG (restart_flag_time, RESTART_R_BIT)) |
| 371 | restart_bit = 1; |
| 372 | UNSET_FLAG (restart_flag_time, 0xF000); |
| 373 | peer->v_gr_restart = restart_flag_time; |
| 374 | |
| 375 | if (BGP_DEBUG (normal, NORMAL)) |
| 376 | { |
| 377 | zlog_debug ("%s OPEN has Graceful Restart capability", peer->host); |
| 378 | zlog_debug ("%s Peer has%srestarted. Restart Time : %d", |
| 379 | peer->host, restart_bit ? " " : " not ", |
| 380 | peer->v_gr_restart); |
| 381 | } |
| 382 | |
Peter Pentchev | 21cc769 | 2011-09-12 16:30:31 +0400 | [diff] [blame] | 383 | while (stream_get_getp (s) + 4 <= end) |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 384 | { |
| 385 | afi_t afi = stream_getw (s); |
| 386 | safi_t safi = stream_getc (s); |
| 387 | u_char flag = stream_getc (s); |
| 388 | |
| 389 | if (!bgp_afi_safi_valid_indices (afi, &safi)) |
| 390 | { |
| 391 | if (BGP_DEBUG (normal, NORMAL)) |
| 392 | zlog_debug ("%s Addr-family %d/%d(afi/safi) not supported." |
| 393 | " Ignore the Graceful Restart capability", |
| 394 | peer->host, afi, safi); |
| 395 | } |
| 396 | else if (!peer->afc[afi][safi]) |
| 397 | { |
| 398 | if (BGP_DEBUG (normal, NORMAL)) |
| 399 | zlog_debug ("%s Addr-family %d/%d(afi/safi) not enabled." |
| 400 | " Ignore the Graceful Restart capability", |
| 401 | peer->host, afi, safi); |
| 402 | } |
| 403 | else |
| 404 | { |
| 405 | if (BGP_DEBUG (normal, NORMAL)) |
| 406 | zlog_debug ("%s Address family %s is%spreserved", peer->host, |
| 407 | afi_safi_print (afi, safi), |
| 408 | CHECK_FLAG (peer->af_cap[afi][safi], |
| 409 | PEER_CAP_RESTART_AF_PRESERVE_RCV) |
| 410 | ? " " : " not "); |
| 411 | |
| 412 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_RCV); |
| 413 | if (CHECK_FLAG (flag, RESTART_F_BIT)) |
| 414 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_RESTART_AF_PRESERVE_RCV); |
| 415 | |
| 416 | } |
| 417 | } |
| 418 | return 0; |
| 419 | } |
| 420 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 421 | static as_t |
| 422 | bgp_capability_as4 (struct peer *peer, struct capability_header *hdr) |
| 423 | { |
Paul Jakma | 5861739 | 2012-01-09 20:59:26 +0000 | [diff] [blame] | 424 | SET_FLAG (peer->cap, PEER_CAP_AS4_RCV); |
| 425 | |
| 426 | if (hdr->length != CAPABILITY_CODE_AS4_LEN) |
| 427 | { |
| 428 | zlog_err ("%s AS4 capability has incorrect data length %d", |
| 429 | peer->host, hdr->length); |
| 430 | return 0; |
| 431 | } |
| 432 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 433 | as_t as4 = stream_getl (BGP_INPUT(peer)); |
| 434 | |
| 435 | if (BGP_DEBUG (as4, AS4)) |
| 436 | zlog_debug ("%s [AS4] about to set cap PEER_CAP_AS4_RCV, got as4 %u", |
| 437 | peer->host, as4); |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 438 | return as4; |
| 439 | } |
| 440 | |
Stephen Hemminger | fc52f95 | 2009-05-15 09:48:55 -0700 | [diff] [blame] | 441 | static const struct message capcode_str[] = |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 442 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 443 | { CAPABILITY_CODE_MP, "MultiProtocol Extensions" }, |
| 444 | { CAPABILITY_CODE_REFRESH, "Route Refresh" }, |
| 445 | { CAPABILITY_CODE_ORF, "Cooperative Route Filtering" }, |
| 446 | { CAPABILITY_CODE_RESTART, "Graceful Restart" }, |
| 447 | { CAPABILITY_CODE_AS4, "4-octet AS number" }, |
| 448 | { CAPABILITY_CODE_DYNAMIC, "Dynamic" }, |
| 449 | { CAPABILITY_CODE_REFRESH_OLD, "Route Refresh (Old)" }, |
| 450 | { CAPABILITY_CODE_ORF_OLD, "ORF (Old)" }, |
| 451 | }; |
Stephen Hemminger | fc52f95 | 2009-05-15 09:48:55 -0700 | [diff] [blame] | 452 | static const int capcode_str_max = sizeof(capcode_str)/sizeof(capcode_str[0]); |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 453 | |
| 454 | /* Minimum sizes for length field of each cap (so not inc. the header) */ |
Stephen Hemminger | fc52f95 | 2009-05-15 09:48:55 -0700 | [diff] [blame] | 455 | static const size_t cap_minsizes[] = |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 456 | { |
| 457 | [CAPABILITY_CODE_MP] = sizeof (struct capability_mp_data), |
| 458 | [CAPABILITY_CODE_REFRESH] = CAPABILITY_CODE_REFRESH_LEN, |
| 459 | [CAPABILITY_CODE_ORF] = sizeof (struct capability_orf_entry), |
Paul Jakma | 370b64a | 2007-12-22 16:49:52 +0000 | [diff] [blame] | 460 | [CAPABILITY_CODE_RESTART] = sizeof (struct capability_gr), |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 461 | [CAPABILITY_CODE_AS4] = CAPABILITY_CODE_AS4_LEN, |
| 462 | [CAPABILITY_CODE_DYNAMIC] = CAPABILITY_CODE_DYNAMIC_LEN, |
| 463 | [CAPABILITY_CODE_REFRESH_OLD] = CAPABILITY_CODE_REFRESH_LEN, |
| 464 | [CAPABILITY_CODE_ORF_OLD] = sizeof (struct capability_orf_entry), |
| 465 | }; |
| 466 | |
| 467 | /* Parse given capability. |
| 468 | * XXX: This is reading into a stream, but not using stream API |
| 469 | */ |
| 470 | static int |
| 471 | bgp_capability_parse (struct peer *peer, size_t length, u_char **error) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 472 | { |
| 473 | int ret; |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 474 | struct stream *s = BGP_INPUT (peer); |
| 475 | size_t end = stream_get_getp (s) + length; |
| 476 | |
| 477 | assert (STREAM_READABLE (s) >= length); |
| 478 | |
| 479 | while (stream_get_getp (s) < end) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 480 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 481 | size_t start; |
| 482 | u_char *sp = stream_pnt (s); |
| 483 | struct capability_header caphdr; |
| 484 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 485 | /* We need at least capability code and capability length. */ |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 486 | if (stream_get_getp(s) + 2 > end) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 487 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 488 | zlog_info ("%s Capability length error (< header)", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 489 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 490 | return -1; |
| 491 | } |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 492 | |
| 493 | caphdr.code = stream_getc (s); |
| 494 | caphdr.length = stream_getc (s); |
| 495 | start = stream_get_getp (s); |
| 496 | |
| 497 | /* Capability length check sanity check. */ |
| 498 | if (start + caphdr.length > end) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 499 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 500 | zlog_info ("%s Capability length error (< length)", peer->host); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 501 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 502 | return -1; |
| 503 | } |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 504 | |
| 505 | if (BGP_DEBUG (normal, NORMAL)) |
| 506 | zlog_debug ("%s OPEN has %s capability (%u), length %u", |
| 507 | peer->host, |
| 508 | LOOKUP (capcode_str, caphdr.code), |
| 509 | caphdr.code, caphdr.length); |
| 510 | |
| 511 | /* Length sanity check, type-specific, for known capabilities */ |
| 512 | switch (caphdr.code) |
| 513 | { |
| 514 | case CAPABILITY_CODE_MP: |
| 515 | case CAPABILITY_CODE_REFRESH: |
| 516 | case CAPABILITY_CODE_REFRESH_OLD: |
| 517 | case CAPABILITY_CODE_ORF: |
| 518 | case CAPABILITY_CODE_ORF_OLD: |
| 519 | case CAPABILITY_CODE_RESTART: |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 520 | case CAPABILITY_CODE_AS4: |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 521 | case CAPABILITY_CODE_DYNAMIC: |
| 522 | /* Check length. */ |
| 523 | if (caphdr.length < cap_minsizes[caphdr.code]) |
| 524 | { |
| 525 | zlog_info ("%s %s Capability length error: got %u," |
| 526 | " expected at least %u", |
| 527 | peer->host, |
| 528 | LOOKUP (capcode_str, caphdr.code), |
Stephen Hemminger | fc52f95 | 2009-05-15 09:48:55 -0700 | [diff] [blame] | 529 | caphdr.length, |
| 530 | (unsigned) cap_minsizes[caphdr.code]); |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 531 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 532 | return -1; |
| 533 | } |
| 534 | /* we deliberately ignore unknown codes, see below */ |
| 535 | default: |
| 536 | break; |
| 537 | } |
| 538 | |
| 539 | switch (caphdr.code) |
| 540 | { |
| 541 | case CAPABILITY_CODE_MP: |
| 542 | { |
| 543 | /* Ignore capability when override-capability is set. */ |
| 544 | if (! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 545 | { |
| 546 | /* Set negotiated value. */ |
| 547 | ret = bgp_capability_mp (peer, &caphdr); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 548 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 549 | /* Unsupported Capability. */ |
| 550 | if (ret < 0) |
| 551 | { |
| 552 | /* Store return data. */ |
| 553 | memcpy (*error, sp, caphdr.length + 2); |
| 554 | *error += caphdr.length + 2; |
| 555 | } |
| 556 | } |
| 557 | } |
| 558 | break; |
| 559 | case CAPABILITY_CODE_REFRESH: |
| 560 | case CAPABILITY_CODE_REFRESH_OLD: |
| 561 | { |
| 562 | /* BGP refresh capability */ |
| 563 | if (caphdr.code == CAPABILITY_CODE_REFRESH_OLD) |
| 564 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_OLD_RCV); |
| 565 | else |
| 566 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_NEW_RCV); |
| 567 | } |
| 568 | break; |
| 569 | case CAPABILITY_CODE_ORF: |
| 570 | case CAPABILITY_CODE_ORF_OLD: |
| 571 | if (bgp_capability_orf (peer, &caphdr)) |
| 572 | return -1; |
| 573 | break; |
| 574 | case CAPABILITY_CODE_RESTART: |
| 575 | if (bgp_capability_restart (peer, &caphdr)) |
| 576 | return -1; |
| 577 | break; |
| 578 | case CAPABILITY_CODE_DYNAMIC: |
| 579 | SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_RCV); |
| 580 | break; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 581 | case CAPABILITY_CODE_AS4: |
| 582 | /* Already handled as a special-case parsing of the capabilities |
| 583 | * at the beginning of OPEN processing. So we care not a jot |
| 584 | * for the value really, only error case. |
| 585 | */ |
| 586 | if (!bgp_capability_as4 (peer, &caphdr)) |
| 587 | return -1; |
| 588 | break; |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 589 | default: |
| 590 | if (caphdr.code > 128) |
| 591 | { |
| 592 | /* We don't send Notification for unknown vendor specific |
| 593 | capabilities. It seems reasonable for now... */ |
| 594 | zlog_warn ("%s Vendor specific capability %d", |
| 595 | peer->host, caphdr.code); |
| 596 | } |
| 597 | else |
| 598 | { |
| 599 | zlog_warn ("%s unrecognized capability code: %d - ignored", |
| 600 | peer->host, caphdr.code); |
| 601 | memcpy (*error, sp, caphdr.length + 2); |
| 602 | *error += caphdr.length + 2; |
| 603 | } |
| 604 | } |
| 605 | if (stream_get_getp(s) != (start + caphdr.length)) |
| 606 | { |
| 607 | if (stream_get_getp(s) > (start + caphdr.length)) |
| 608 | zlog_warn ("%s Cap-parser for %s read past cap-length, %u!", |
| 609 | peer->host, LOOKUP (capcode_str, caphdr.code), |
| 610 | caphdr.length); |
| 611 | stream_set_getp (s, start + caphdr.length); |
| 612 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 613 | } |
| 614 | return 0; |
| 615 | } |
| 616 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 617 | static int |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 618 | bgp_auth_parse (struct peer *peer, size_t length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 619 | { |
| 620 | bgp_notify_send (peer, |
| 621 | BGP_NOTIFY_OPEN_ERR, |
| 622 | BGP_NOTIFY_OPEN_AUTH_FAILURE); |
| 623 | return -1; |
| 624 | } |
| 625 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 626 | static int |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 627 | strict_capability_same (struct peer *peer) |
| 628 | { |
| 629 | int i, j; |
| 630 | |
| 631 | for (i = AFI_IP; i < AFI_MAX; i++) |
| 632 | for (j = SAFI_UNICAST; j < SAFI_MAX; j++) |
| 633 | if (peer->afc[i][j] != peer->afc_nego[i][j]) |
| 634 | return 0; |
| 635 | return 1; |
| 636 | } |
| 637 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 638 | /* peek into option, stores ASN to *as4 if the AS4 capability was found. |
| 639 | * Returns 0 if no as4 found, as4cap value otherwise. |
| 640 | */ |
| 641 | as_t |
| 642 | peek_for_as4_capability (struct peer *peer, u_char length) |
| 643 | { |
| 644 | struct stream *s = BGP_INPUT (peer); |
| 645 | size_t orig_getp = stream_get_getp (s); |
| 646 | size_t end = orig_getp + length; |
| 647 | as_t as4 = 0; |
| 648 | |
| 649 | /* The full capability parser will better flag the error.. */ |
| 650 | if (STREAM_READABLE(s) < length) |
| 651 | return 0; |
| 652 | |
| 653 | if (BGP_DEBUG (as4, AS4)) |
| 654 | zlog_info ("%s [AS4] rcv OPEN w/ OPTION parameter len: %u," |
| 655 | " peeking for as4", |
| 656 | peer->host, length); |
| 657 | /* the error cases we DONT handle, we ONLY try to read as4 out of |
| 658 | * correctly formatted options. |
| 659 | */ |
| 660 | while (stream_get_getp(s) < end) |
| 661 | { |
| 662 | u_char opt_type; |
| 663 | u_char opt_length; |
| 664 | |
| 665 | /* Check the length. */ |
| 666 | if (stream_get_getp (s) + 2 > end) |
| 667 | goto end; |
| 668 | |
| 669 | /* Fetch option type and length. */ |
| 670 | opt_type = stream_getc (s); |
| 671 | opt_length = stream_getc (s); |
| 672 | |
| 673 | /* Option length check. */ |
| 674 | if (stream_get_getp (s) + opt_length > end) |
| 675 | goto end; |
| 676 | |
| 677 | if (opt_type == BGP_OPEN_OPT_CAP) |
| 678 | { |
| 679 | unsigned long capd_start = stream_get_getp (s); |
| 680 | unsigned long capd_end = capd_start + opt_length; |
| 681 | |
| 682 | assert (capd_end <= end); |
| 683 | |
| 684 | while (stream_get_getp (s) < capd_end) |
| 685 | { |
| 686 | struct capability_header hdr; |
| 687 | |
| 688 | if (stream_get_getp (s) + 2 > capd_end) |
| 689 | goto end; |
| 690 | |
| 691 | hdr.code = stream_getc (s); |
| 692 | hdr.length = stream_getc (s); |
| 693 | |
| 694 | if ((stream_get_getp(s) + hdr.length) > capd_end) |
| 695 | goto end; |
| 696 | |
| 697 | if (hdr.code == CAPABILITY_CODE_AS4) |
| 698 | { |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 699 | if (BGP_DEBUG (as4, AS4)) |
| 700 | zlog_info ("[AS4] found AS4 capability, about to parse"); |
| 701 | as4 = bgp_capability_as4 (peer, &hdr); |
| 702 | |
| 703 | goto end; |
| 704 | } |
| 705 | stream_forward_getp (s, hdr.length); |
| 706 | } |
| 707 | } |
| 708 | } |
| 709 | |
| 710 | end: |
| 711 | stream_set_getp (s, orig_getp); |
| 712 | return as4; |
| 713 | } |
| 714 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 715 | /* Parse open option */ |
| 716 | int |
| 717 | bgp_open_option_parse (struct peer *peer, u_char length, int *capability) |
| 718 | { |
| 719 | int ret; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 720 | u_char *error; |
| 721 | u_char error_data[BGP_MAX_PACKET_SIZE]; |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 722 | struct stream *s = BGP_INPUT(peer); |
| 723 | size_t end = stream_get_getp (s) + length; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 724 | |
| 725 | ret = 0; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 726 | error = error_data; |
| 727 | |
| 728 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 8325cd7 | 2004-12-08 20:47:40 +0000 | [diff] [blame] | 729 | zlog_debug ("%s rcv OPEN w/ OPTION parameter len: %u", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 730 | peer->host, length); |
| 731 | |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 732 | while (stream_get_getp(s) < end) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 733 | { |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 734 | u_char opt_type; |
| 735 | u_char opt_length; |
| 736 | |
| 737 | /* Must have at least an OPEN option header */ |
| 738 | if (STREAM_READABLE(s) < 2) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 739 | { |
| 740 | zlog_info ("%s Option length error", peer->host); |
| 741 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 742 | return -1; |
| 743 | } |
| 744 | |
| 745 | /* Fetch option type and length. */ |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 746 | opt_type = stream_getc (s); |
| 747 | opt_length = stream_getc (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 748 | |
| 749 | /* Option length check. */ |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 750 | if (STREAM_READABLE (s) < opt_length) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 751 | { |
| 752 | zlog_info ("%s Option length error", peer->host); |
| 753 | bgp_notify_send (peer, BGP_NOTIFY_CEASE, 0); |
| 754 | return -1; |
| 755 | } |
| 756 | |
| 757 | if (BGP_DEBUG (normal, NORMAL)) |
ajs | 8325cd7 | 2004-12-08 20:47:40 +0000 | [diff] [blame] | 758 | zlog_debug ("%s rcvd OPEN w/ optional parameter type %u (%s) len %u", |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 759 | peer->host, opt_type, |
| 760 | opt_type == BGP_OPEN_OPT_AUTH ? "Authentication" : |
| 761 | opt_type == BGP_OPEN_OPT_CAP ? "Capability" : "Unknown", |
| 762 | opt_length); |
| 763 | |
| 764 | switch (opt_type) |
| 765 | { |
| 766 | case BGP_OPEN_OPT_AUTH: |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 767 | ret = bgp_auth_parse (peer, opt_length); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 768 | break; |
| 769 | case BGP_OPEN_OPT_CAP: |
Paul Jakma | 6d58272 | 2007-08-06 15:21:45 +0000 | [diff] [blame] | 770 | ret = bgp_capability_parse (peer, opt_length, &error); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 771 | *capability = 1; |
| 772 | break; |
| 773 | default: |
| 774 | bgp_notify_send (peer, |
| 775 | BGP_NOTIFY_OPEN_ERR, |
| 776 | BGP_NOTIFY_OPEN_UNSUP_PARAM); |
| 777 | ret = -1; |
| 778 | break; |
| 779 | } |
| 780 | |
| 781 | /* Parse error. To accumulate all unsupported capability codes, |
| 782 | bgp_capability_parse does not return -1 when encounter |
| 783 | unsupported capability code. To detect that, please check |
| 784 | error and erro_data pointer, like below. */ |
| 785 | if (ret < 0) |
| 786 | return -1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 787 | } |
| 788 | |
| 789 | /* All OPEN option is parsed. Check capability when strict compare |
| 790 | flag is enabled.*/ |
| 791 | if (CHECK_FLAG (peer->flags, PEER_FLAG_STRICT_CAP_MATCH)) |
| 792 | { |
| 793 | /* If Unsupported Capability exists. */ |
| 794 | if (error != error_data) |
| 795 | { |
| 796 | bgp_notify_send_with_data (peer, |
| 797 | BGP_NOTIFY_OPEN_ERR, |
| 798 | BGP_NOTIFY_OPEN_UNSUP_CAPBL, |
| 799 | error_data, error - error_data); |
| 800 | return -1; |
| 801 | } |
| 802 | |
| 803 | /* Check local capability does not negotiated with remote |
| 804 | peer. */ |
| 805 | if (! strict_capability_same (peer)) |
| 806 | { |
| 807 | bgp_notify_send (peer, |
| 808 | BGP_NOTIFY_OPEN_ERR, |
| 809 | BGP_NOTIFY_OPEN_UNSUP_CAPBL); |
| 810 | return -1; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* Check there is no common capability send Unsupported Capability |
| 815 | error. */ |
| 816 | if (*capability && ! CHECK_FLAG (peer->flags, PEER_FLAG_OVERRIDE_CAPABILITY)) |
| 817 | { |
| 818 | if (! peer->afc_nego[AFI_IP][SAFI_UNICAST] |
| 819 | && ! peer->afc_nego[AFI_IP][SAFI_MULTICAST] |
| 820 | && ! peer->afc_nego[AFI_IP][SAFI_MPLS_VPN] |
| 821 | && ! peer->afc_nego[AFI_IP6][SAFI_UNICAST] |
| 822 | && ! peer->afc_nego[AFI_IP6][SAFI_MULTICAST]) |
| 823 | { |
| 824 | plog_err (peer->log, "%s [Error] No common capability", peer->host); |
| 825 | |
| 826 | if (error != error_data) |
| 827 | |
| 828 | bgp_notify_send_with_data (peer, |
| 829 | BGP_NOTIFY_OPEN_ERR, |
| 830 | BGP_NOTIFY_OPEN_UNSUP_CAPBL, |
| 831 | error_data, error - error_data); |
| 832 | else |
| 833 | bgp_notify_send (peer, |
| 834 | BGP_NOTIFY_OPEN_ERR, |
| 835 | BGP_NOTIFY_OPEN_UNSUP_CAPBL); |
| 836 | return -1; |
| 837 | } |
| 838 | } |
| 839 | return 0; |
| 840 | } |
| 841 | |
paul | 94f2b39 | 2005-06-28 12:44:16 +0000 | [diff] [blame] | 842 | static void |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 843 | bgp_open_capability_orf (struct stream *s, struct peer *peer, |
| 844 | afi_t afi, safi_t safi, u_char code) |
| 845 | { |
| 846 | u_char cap_len; |
| 847 | u_char orf_len; |
| 848 | unsigned long capp; |
| 849 | unsigned long orfp; |
| 850 | unsigned long numberp; |
| 851 | int number_of_orfs = 0; |
| 852 | |
| 853 | if (safi == SAFI_MPLS_VPN) |
Denis Ovsienko | 42e6d74 | 2011-07-14 12:36:19 +0400 | [diff] [blame] | 854 | safi = SAFI_MPLS_LABELED_VPN; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 855 | |
| 856 | stream_putc (s, BGP_OPEN_OPT_CAP); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 857 | capp = stream_get_endp (s); /* Set Capability Len Pointer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 858 | stream_putc (s, 0); /* Capability Length */ |
| 859 | stream_putc (s, code); /* Capability Code */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 860 | orfp = stream_get_endp (s); /* Set ORF Len Pointer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 861 | stream_putc (s, 0); /* ORF Length */ |
| 862 | stream_putw (s, afi); |
| 863 | stream_putc (s, 0); |
| 864 | stream_putc (s, safi); |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 865 | numberp = stream_get_endp (s); /* Set Number Pointer */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 866 | stream_putc (s, 0); /* Number of ORFs */ |
| 867 | |
| 868 | /* Address Prefix ORF */ |
| 869 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 870 | || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 871 | { |
| 872 | stream_putc (s, (code == CAPABILITY_CODE_ORF ? |
| 873 | ORF_TYPE_PREFIX : ORF_TYPE_PREFIX_OLD)); |
| 874 | |
| 875 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 876 | && CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 877 | { |
| 878 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV); |
| 879 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV); |
| 880 | stream_putc (s, ORF_MODE_BOTH); |
| 881 | } |
| 882 | else if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM)) |
| 883 | { |
| 884 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_SM_ADV); |
| 885 | stream_putc (s, ORF_MODE_SEND); |
| 886 | } |
| 887 | else |
| 888 | { |
| 889 | SET_FLAG (peer->af_cap[afi][safi], PEER_CAP_ORF_PREFIX_RM_ADV); |
| 890 | stream_putc (s, ORF_MODE_RECEIVE); |
| 891 | } |
| 892 | number_of_orfs++; |
| 893 | } |
| 894 | |
| 895 | /* Total Number of ORFs. */ |
| 896 | stream_putc_at (s, numberp, number_of_orfs); |
| 897 | |
| 898 | /* Total ORF Len. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 899 | orf_len = stream_get_endp (s) - orfp - 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 900 | stream_putc_at (s, orfp, orf_len); |
| 901 | |
| 902 | /* Total Capability Len. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 903 | cap_len = stream_get_endp (s) - capp - 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 904 | stream_putc_at (s, capp, cap_len); |
| 905 | } |
| 906 | |
| 907 | /* Fill in capability open option to the packet. */ |
| 908 | void |
| 909 | bgp_open_capability (struct stream *s, struct peer *peer) |
| 910 | { |
| 911 | u_char len; |
| 912 | unsigned long cp; |
| 913 | afi_t afi; |
| 914 | safi_t safi; |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 915 | as_t local_as; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 916 | |
| 917 | /* Remember current pointer for Opt Parm Len. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 918 | cp = stream_get_endp (s); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 919 | |
| 920 | /* Opt Parm Len. */ |
| 921 | stream_putc (s, 0); |
| 922 | |
| 923 | /* Do not send capability. */ |
| 924 | if (! CHECK_FLAG (peer->sflags, PEER_STATUS_CAPABILITY_OPEN) |
| 925 | || CHECK_FLAG (peer->flags, PEER_FLAG_DONT_CAPABILITY)) |
| 926 | return; |
| 927 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 928 | /* IPv4 unicast. */ |
| 929 | if (peer->afc[AFI_IP][SAFI_UNICAST]) |
| 930 | { |
| 931 | peer->afc_adv[AFI_IP][SAFI_UNICAST] = 1; |
| 932 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 933 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 934 | stream_putc (s, CAPABILITY_CODE_MP); |
| 935 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 936 | stream_putw (s, AFI_IP); |
| 937 | stream_putc (s, 0); |
| 938 | stream_putc (s, SAFI_UNICAST); |
| 939 | } |
| 940 | /* IPv4 multicast. */ |
| 941 | if (peer->afc[AFI_IP][SAFI_MULTICAST]) |
| 942 | { |
| 943 | peer->afc_adv[AFI_IP][SAFI_MULTICAST] = 1; |
| 944 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 945 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 946 | stream_putc (s, CAPABILITY_CODE_MP); |
| 947 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 948 | stream_putw (s, AFI_IP); |
| 949 | stream_putc (s, 0); |
| 950 | stream_putc (s, SAFI_MULTICAST); |
| 951 | } |
| 952 | /* IPv4 VPN */ |
| 953 | if (peer->afc[AFI_IP][SAFI_MPLS_VPN]) |
| 954 | { |
| 955 | peer->afc_adv[AFI_IP][SAFI_MPLS_VPN] = 1; |
| 956 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 957 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 958 | stream_putc (s, CAPABILITY_CODE_MP); |
| 959 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 960 | stream_putw (s, AFI_IP); |
| 961 | stream_putc (s, 0); |
Denis Ovsienko | 42e6d74 | 2011-07-14 12:36:19 +0400 | [diff] [blame] | 962 | stream_putc (s, SAFI_MPLS_LABELED_VPN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 963 | } |
| 964 | #ifdef HAVE_IPV6 |
| 965 | /* IPv6 unicast. */ |
| 966 | if (peer->afc[AFI_IP6][SAFI_UNICAST]) |
| 967 | { |
| 968 | peer->afc_adv[AFI_IP6][SAFI_UNICAST] = 1; |
| 969 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 970 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 971 | stream_putc (s, CAPABILITY_CODE_MP); |
| 972 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 973 | stream_putw (s, AFI_IP6); |
| 974 | stream_putc (s, 0); |
| 975 | stream_putc (s, SAFI_UNICAST); |
| 976 | } |
| 977 | /* IPv6 multicast. */ |
| 978 | if (peer->afc[AFI_IP6][SAFI_MULTICAST]) |
| 979 | { |
| 980 | peer->afc_adv[AFI_IP6][SAFI_MULTICAST] = 1; |
| 981 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 982 | stream_putc (s, CAPABILITY_CODE_MP_LEN + 2); |
| 983 | stream_putc (s, CAPABILITY_CODE_MP); |
| 984 | stream_putc (s, CAPABILITY_CODE_MP_LEN); |
| 985 | stream_putw (s, AFI_IP6); |
| 986 | stream_putc (s, 0); |
| 987 | stream_putc (s, SAFI_MULTICAST); |
| 988 | } |
| 989 | #endif /* HAVE_IPV6 */ |
| 990 | |
| 991 | /* Route refresh. */ |
hasso | c950243 | 2005-02-01 22:01:48 +0000 | [diff] [blame] | 992 | SET_FLAG (peer->cap, PEER_CAP_REFRESH_ADV); |
| 993 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 994 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2); |
| 995 | stream_putc (s, CAPABILITY_CODE_REFRESH_OLD); |
| 996 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
| 997 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 998 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN + 2); |
| 999 | stream_putc (s, CAPABILITY_CODE_REFRESH); |
| 1000 | stream_putc (s, CAPABILITY_CODE_REFRESH_LEN); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1001 | |
Paul Jakma | 0b2aa3a | 2007-10-14 22:32:21 +0000 | [diff] [blame] | 1002 | /* AS4 */ |
| 1003 | SET_FLAG (peer->cap, PEER_CAP_AS4_ADV); |
| 1004 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 1005 | stream_putc (s, CAPABILITY_CODE_AS4_LEN + 2); |
| 1006 | stream_putc (s, CAPABILITY_CODE_AS4); |
| 1007 | stream_putc (s, CAPABILITY_CODE_AS4_LEN); |
| 1008 | if ( peer->change_local_as ) |
| 1009 | local_as = peer->change_local_as; |
| 1010 | else |
| 1011 | local_as = peer->local_as; |
| 1012 | stream_putl (s, local_as ); |
| 1013 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1014 | /* ORF capability. */ |
| 1015 | for (afi = AFI_IP ; afi < AFI_MAX ; afi++) |
| 1016 | for (safi = SAFI_UNICAST ; safi < SAFI_MAX ; safi++) |
| 1017 | if (CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_SM) |
| 1018 | || CHECK_FLAG (peer->af_flags[afi][safi], PEER_FLAG_ORF_PREFIX_RM)) |
| 1019 | { |
| 1020 | bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF_OLD); |
| 1021 | bgp_open_capability_orf (s, peer, afi, safi, CAPABILITY_CODE_ORF); |
| 1022 | } |
| 1023 | |
| 1024 | /* Dynamic capability. */ |
| 1025 | if (CHECK_FLAG (peer->flags, PEER_FLAG_DYNAMIC_CAPABILITY)) |
| 1026 | { |
| 1027 | SET_FLAG (peer->cap, PEER_CAP_DYNAMIC_ADV); |
| 1028 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 1029 | stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN + 2); |
| 1030 | stream_putc (s, CAPABILITY_CODE_DYNAMIC); |
| 1031 | stream_putc (s, CAPABILITY_CODE_DYNAMIC_LEN); |
| 1032 | } |
| 1033 | |
hasso | 538621f | 2004-05-21 09:31:30 +0000 | [diff] [blame] | 1034 | /* Graceful restart capability */ |
| 1035 | if (bgp_flag_check (peer->bgp, BGP_FLAG_GRACEFUL_RESTART)) |
| 1036 | { |
| 1037 | SET_FLAG (peer->cap, PEER_CAP_RESTART_ADV); |
| 1038 | stream_putc (s, BGP_OPEN_OPT_CAP); |
| 1039 | stream_putc (s, CAPABILITY_CODE_RESTART_LEN + 2); |
| 1040 | stream_putc (s, CAPABILITY_CODE_RESTART); |
| 1041 | stream_putc (s, CAPABILITY_CODE_RESTART_LEN); |
| 1042 | stream_putw (s, peer->bgp->restart_time); |
| 1043 | } |
| 1044 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1045 | /* Total Opt Parm Len. */ |
paul | 9985f83 | 2005-02-09 15:51:56 +0000 | [diff] [blame] | 1046 | len = stream_get_endp (s) - cp - 1; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1047 | stream_putc_at (s, cp, len); |
| 1048 | } |