]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_encap_tlv.c
eigrpd: eigrp usage of uint32_t to struct in_addr for router_id data
[mirror_frr.git] / bgpd / bgp_encap_tlv.c
CommitLineData
f4c89855
LB
1/*
2 * Copyright 2015, LabN Consulting, L.L.C.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
896014f4
DL
14 * You should have received a copy of the GNU General Public License along
15 * with this program; see the file COPYING; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
f4c89855
LB
17 */
18
19#include <zebra.h>
20
5bf15956 21#include "command.h"
f4c89855
LB
22#include "memory.h"
23#include "prefix.h"
f4c89855 24#include "filter.h"
937652c6 25#include "stream.h"
f4c89855
LB
26
27#include "bgpd.h"
28#include "bgp_attr.h"
29
30#include "bgp_encap_types.h"
31#include "bgp_encap_tlv.h"
32
33/***********************************************************************
34 * SUBTLV ENCODE
35 ***********************************************************************/
36
37/* rfc5512 4.1 */
d62a17ae 38static struct bgp_attr_encap_subtlv *subtlv_encode_encap_l2tpv3_over_ip(
39 struct bgp_tea_subtlv_encap_l2tpv3_over_ip *st)
f4c89855 40{
d62a17ae 41 struct bgp_attr_encap_subtlv *new;
42 uint8_t *p;
43 int total = 4 + st->cookie_length;
f4c89855 44
d62a17ae 45 /* sanity check */
46 assert(st->cookie_length <= sizeof(st->cookie));
47 assert(total <= 0xff);
f4c89855 48
d62a17ae 49 new = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 50 sizeof(struct bgp_attr_encap_subtlv) + total);
d62a17ae 51 assert(new);
52 new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
53 new->length = total;
54 p = new->value;
f4c89855 55
d62a17ae 56 *p++ = (st->sessionid & 0xff000000) >> 24;
57 *p++ = (st->sessionid & 0xff0000) >> 16;
58 *p++ = (st->sessionid & 0xff00) >> 8;
59 *p++ = (st->sessionid & 0xff);
60 memcpy(p, st->cookie, st->cookie_length);
61 return new;
f4c89855
LB
62}
63
64/* rfc5512 4.1 */
65static struct bgp_attr_encap_subtlv *
d62a17ae 66subtlv_encode_encap_gre(struct bgp_tea_subtlv_encap_gre_key *st)
f4c89855 67{
d62a17ae 68 struct bgp_attr_encap_subtlv *new;
69 uint8_t *p;
70 int total = 4;
f4c89855 71
d62a17ae 72 assert(total <= 0xff);
f4c89855 73
d62a17ae 74 new = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 75 sizeof(struct bgp_attr_encap_subtlv) + total);
d62a17ae 76 assert(new);
77 new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
78 new->length = total;
79 p = new->value;
f4c89855 80
d62a17ae 81 *p++ = (st->gre_key & 0xff000000) >> 24;
82 *p++ = (st->gre_key & 0xff0000) >> 16;
83 *p++ = (st->gre_key & 0xff00) >> 8;
84 *p++ = (st->gre_key & 0xff);
85 return new;
f4c89855
LB
86}
87
88static struct bgp_attr_encap_subtlv *
d62a17ae 89subtlv_encode_encap_pbb(struct bgp_tea_subtlv_encap_pbb *st)
90{
91 struct bgp_attr_encap_subtlv *new;
92 uint8_t *p;
93 int total = 1 + 3 + 6 + 2; /* flags + isid + madaddr + vid */
94
95 assert(total <= 0xff);
96
97 new = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 98 sizeof(struct bgp_attr_encap_subtlv) + total);
d62a17ae 99 assert(new);
100 new->type = BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION;
101 new->length = total;
102 p = new->value;
103
104 *p++ = (st->flag_isid ? 0x80 : 0) | (st->flag_vid ? 0x40 : 0) | 0;
105 if (st->flag_isid) {
106 *p = (st->isid & 0xff0000) >> 16;
107 *(p + 1) = (st->isid & 0xff00) >> 8;
108 *(p + 2) = (st->isid & 0xff);
109 }
110 p += 3;
111 memcpy(p, st->macaddr, 6);
112 p += 6;
113 if (st->flag_vid) {
114 *p++ = (st->vid & 0xf00) >> 8;
115 *p++ = st->vid & 0xff;
116 }
117 return new;
f4c89855
LB
118}
119
120/* rfc5512 4.2 */
121static struct bgp_attr_encap_subtlv *
d62a17ae 122subtlv_encode_proto_type(struct bgp_tea_subtlv_proto_type *st)
f4c89855 123{
d62a17ae 124 struct bgp_attr_encap_subtlv *new;
125 uint8_t *p;
126 int total = 2;
f4c89855 127
d62a17ae 128 assert(total <= 0xff);
f4c89855 129
d62a17ae 130 new = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 131 sizeof(struct bgp_attr_encap_subtlv) + total);
d62a17ae 132 assert(new);
133 new->type = BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE;
134 new->length = total;
135 p = new->value;
f4c89855 136
d62a17ae 137 *p++ = (st->proto & 0xff00) >> 8;
138 *p++ = (st->proto & 0xff);
139 return new;
f4c89855
LB
140}
141
142/* rfc5512 4.3 */
143static struct bgp_attr_encap_subtlv *
d62a17ae 144subtlv_encode_color(struct bgp_tea_subtlv_color *st)
f4c89855 145{
d62a17ae 146 struct bgp_attr_encap_subtlv *new;
147 uint8_t *p;
148 int total = 8;
f4c89855 149
d62a17ae 150 assert(total <= 0xff);
f4c89855 151
d62a17ae 152 new = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 153 sizeof(struct bgp_attr_encap_subtlv) + total);
d62a17ae 154 assert(new);
155 new->type = BGP_ENCAP_SUBTLV_TYPE_COLOR;
156 new->length = total;
157 p = new->value;
f4c89855 158
d62a17ae 159 *p++ = 0x03; /* transitive*/
160 *p++ = 0x0b;
161 *p++ = 0; /* reserved */
162 *p++ = 0; /* reserved */
f4c89855 163
d62a17ae 164 *p++ = (st->color & 0xff000000) >> 24;
165 *p++ = (st->color & 0xff0000) >> 16;
166 *p++ = (st->color & 0xff00) >> 8;
167 *p++ = (st->color & 0xff);
f4c89855 168
d62a17ae 169 return new;
f4c89855
LB
170}
171
172/* rfc 5566 4. */
173static struct bgp_attr_encap_subtlv *
d62a17ae 174subtlv_encode_ipsec_ta(struct bgp_tea_subtlv_ipsec_ta *st)
f4c89855 175{
d62a17ae 176 struct bgp_attr_encap_subtlv *new;
177 uint8_t *p;
178 int total = 2 + st->authenticator_length;
f4c89855 179
d62a17ae 180 /* sanity check */
181 assert(st->authenticator_length <= sizeof(st->value));
182 assert(total <= 0xff);
f4c89855 183
d62a17ae 184 new = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 185 sizeof(struct bgp_attr_encap_subtlv) + total);
d62a17ae 186 assert(new);
187 new->type = BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA;
188 new->length = total;
189 p = new->value;
f4c89855 190
d62a17ae 191 *p++ = (st->authenticator_type & 0xff00) >> 8;
192 *p++ = st->authenticator_type & 0xff;
193 memcpy(p, st->value, st->authenticator_length);
194 return new;
f4c89855
LB
195}
196
587ff0fd
LB
197/* draft-rosen-idr-tunnel-encaps 2.1 */
198static struct bgp_attr_encap_subtlv *
d62a17ae 199subtlv_encode_remote_endpoint(struct bgp_tea_subtlv_remote_endpoint *st)
200{
201 struct bgp_attr_encap_subtlv *new;
202 uint8_t *p;
203
204 int total = (st->family == AF_INET ? 8 : 20);
205
206 assert(total <= 0xff);
207
208 new = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 209 sizeof(struct bgp_attr_encap_subtlv) + total);
d62a17ae 210 assert(new);
211 new->type = BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT;
212 new->length = total;
213 p = new->value;
214 if (st->family == AF_INET) {
215 memcpy(p, &(st->ip_address.v4.s_addr), 4);
216 p += 4;
217 } else {
218 assert(st->family == AF_INET6);
219 memcpy(p, &(st->ip_address.v6.s6_addr), 16);
220 p += 16;
221 }
222 memcpy(p, &(st->as4), 4);
223 return new;
587ff0fd 224}
f4c89855
LB
225
226/***********************************************************************
227 * TUNNEL TYPE-SPECIFIC TLV ENCODE
228 ***********************************************************************/
229
230/*
231 * requires "extra" and "last" to be defined in caller
232 */
d62a17ae 233#define ENC_SUBTLV(flag, function, field) \
234 do { \
235 struct bgp_attr_encap_subtlv *new; \
236 if (CHECK_FLAG(bet->valid_subtlvs, (flag))) { \
237 new = function(&bet->field); \
238 if (last) { \
239 last->next = new; \
240 } else { \
241 attr->encap_subtlvs = new; \
242 } \
243 last = new; \
244 } \
245 } while (0)
f4c89855 246
d62a17ae 247void bgp_encap_type_l2tpv3overip_to_tlv(
248 struct bgp_encap_type_l2tpv3_over_ip *bet, /* input structure */
249 struct attr *attr)
f4c89855 250{
d62a17ae 251 struct bgp_attr_encap_subtlv *last;
f4c89855 252
d62a17ae 253 /* advance to last subtlv */
254 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
255 ;
f4c89855 256
d62a17ae 257 attr->encap_tunneltype = BGP_ENCAP_TYPE_L2TPV3_OVER_IP;
f4c89855 258
d62a17ae 259 assert(CHECK_FLAG(bet->valid_subtlvs, BGP_TEA_SUBTLV_ENCAP));
f4c89855 260
d62a17ae 261 ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_l2tpv3_over_ip,
262 st_encap);
263 ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type,
264 st_proto);
265 ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
266 ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT,
267 subtlv_encode_remote_endpoint, st_endpoint);
f4c89855
LB
268}
269
d62a17ae 270void bgp_encap_type_gre_to_tlv(
271 struct bgp_encap_type_gre *bet, /* input structure */
272 struct attr *attr)
f4c89855 273{
d62a17ae 274 struct bgp_attr_encap_subtlv *last;
f4c89855 275
d62a17ae 276 /* advance to last subtlv */
277 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
278 ;
f4c89855 279
d62a17ae 280 attr->encap_tunneltype = BGP_ENCAP_TYPE_GRE;
f4c89855 281
d62a17ae 282 ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_gre, st_encap);
283 ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type,
284 st_proto);
285 ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
286 ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT,
287 subtlv_encode_remote_endpoint, st_endpoint);
f4c89855
LB
288}
289
d62a17ae 290void bgp_encap_type_ip_in_ip_to_tlv(
291 struct bgp_encap_type_ip_in_ip *bet, /* input structure */
292 struct attr *attr)
f4c89855 293{
d62a17ae 294 struct bgp_attr_encap_subtlv *last;
f4c89855 295
d62a17ae 296 /* advance to last subtlv */
297 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
298 ;
f4c89855 299
d62a17ae 300 attr->encap_tunneltype = BGP_ENCAP_TYPE_IP_IN_IP;
f4c89855 301
d62a17ae 302 ENC_SUBTLV(BGP_TEA_SUBTLV_PROTO_TYPE, subtlv_encode_proto_type,
303 st_proto);
304 ENC_SUBTLV(BGP_TEA_SUBTLV_COLOR, subtlv_encode_color, st_color);
305 ENC_SUBTLV(BGP_TEA_SUBTLV_REMOTE_ENDPOINT,
306 subtlv_encode_remote_endpoint, st_endpoint);
f4c89855
LB
307}
308
d62a17ae 309void bgp_encap_type_transmit_tunnel_endpoint(
310 struct bgp_encap_type_transmit_tunnel_endpoint
311 *bet, /* input structure */
312 struct attr *attr)
f4c89855 313{
d62a17ae 314 struct bgp_attr_encap_subtlv *last;
f4c89855 315
d62a17ae 316 /* advance to last subtlv */
317 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
318 ;
f4c89855 319
d62a17ae 320 attr->encap_tunneltype = BGP_ENCAP_TYPE_TRANSMIT_TUNNEL_ENDPOINT;
f4c89855 321
d62a17ae 322 /* no subtlvs for this type */
f4c89855
LB
323}
324
d62a17ae 325void bgp_encap_type_ipsec_in_tunnel_mode_to_tlv(
326 struct bgp_encap_type_ipsec_in_tunnel_mode *bet, /* input structure */
327 struct attr *attr)
f4c89855 328{
d62a17ae 329 struct bgp_attr_encap_subtlv *last;
f4c89855 330
d62a17ae 331 /* advance to last subtlv */
332 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
333 ;
f4c89855 334
d62a17ae 335 attr->encap_tunneltype = BGP_ENCAP_TYPE_IPSEC_IN_TUNNEL_MODE;
f4c89855 336
d62a17ae 337 ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta,
338 st_ipsec_ta);
f4c89855
LB
339}
340
d62a17ae 341void bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode_to_tlv(
342 struct bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode
343 *bet, /* input structure */
344 struct attr *attr)
f4c89855 345{
d62a17ae 346 struct bgp_attr_encap_subtlv *last;
f4c89855 347
d62a17ae 348 /* advance to last subtlv */
349 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
350 ;
f4c89855 351
d62a17ae 352 attr->encap_tunneltype =
353 BGP_ENCAP_TYPE_IP_IN_IP_TUNNEL_WITH_IPSEC_TRANSPORT_MODE;
f4c89855 354
d62a17ae 355 ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta,
356 st_ipsec_ta);
f4c89855
LB
357}
358
d62a17ae 359void bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode_to_tlv(
360 struct bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode
361 *bet, /* input structure */
362 struct attr *attr)
f4c89855 363{
d62a17ae 364 struct bgp_attr_encap_subtlv *last;
f4c89855 365
d62a17ae 366 /* advance to last subtlv */
367 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
368 ;
f4c89855 369
d62a17ae 370 attr->encap_tunneltype =
371 BGP_ENCAP_TYPE_MPLS_IN_IP_TUNNEL_WITH_IPSEC_TRANSPORT_MODE;
f4c89855 372
d62a17ae 373 ENC_SUBTLV(BGP_TEA_SUBTLV_IPSEC_TA, subtlv_encode_ipsec_ta,
374 st_ipsec_ta);
f4c89855
LB
375}
376
d62a17ae 377void bgp_encap_type_pbb_to_tlv(
378 struct bgp_encap_type_pbb *bet, /* input structure */
379 struct attr *attr)
f4c89855 380{
d62a17ae 381 struct bgp_attr_encap_subtlv *last;
f4c89855 382
d62a17ae 383 /* advance to last subtlv */
384 for (last = attr->encap_subtlvs; last && last->next; last = last->next)
385 ;
f4c89855 386
d62a17ae 387 attr->encap_tunneltype = BGP_ENCAP_TYPE_PBB;
f4c89855 388
d62a17ae 389 assert(CHECK_FLAG(bet->valid_subtlvs, BGP_TEA_SUBTLV_ENCAP));
390 ENC_SUBTLV(BGP_TEA_SUBTLV_ENCAP, subtlv_encode_encap_pbb, st_encap);
f4c89855
LB
391}
392
d62a17ae 393void bgp_encap_type_vxlan_to_tlv(
394 struct bgp_encap_type_vxlan *bet, /* input structure */
395 struct attr *attr)
f4c89855 396{
d62a17ae 397 struct bgp_attr_encap_subtlv *tlv;
398 uint32_t vnid;
f4c89855 399
d62a17ae 400 attr->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN;
aee875b5 401
d62a17ae 402 if (bet == NULL || !bet->vnid)
403 return;
404 if (attr->encap_subtlvs)
405 XFREE(MTYPE_ENCAP_TLV, attr->encap_subtlvs);
406 tlv = XCALLOC(MTYPE_ENCAP_TLV,
12f70478 407 sizeof(struct bgp_attr_encap_subtlv) + 12);
d62a17ae 408 tlv->type = 1; /* encapsulation type */
409 tlv->length = 12;
410 if (bet->vnid) {
411 vnid = htonl(bet->vnid | VXLAN_ENCAP_MASK_VNID_VALID);
412 memcpy(&tlv->value, &vnid, 4);
413 }
414 if (bet->mac_address) {
415 char *ptr = (char *)&tlv->value + 4;
416 memcpy(ptr, bet->mac_address, 6);
417 }
418 attr->encap_subtlvs = tlv;
419 return;
f4c89855
LB
420}
421
d62a17ae 422void bgp_encap_type_nvgre_to_tlv(
423 struct bgp_encap_type_nvgre *bet, /* input structure */
424 struct attr *attr)
f4c89855 425{
d62a17ae 426 attr->encap_tunneltype = BGP_ENCAP_TYPE_NVGRE;
f4c89855
LB
427}
428
d62a17ae 429void bgp_encap_type_mpls_to_tlv(
430 struct bgp_encap_type_mpls *bet, /* input structure */
431 struct attr *attr)
f4c89855 432{
d62a17ae 433 return; /* no encap attribute for MPLS */
f4c89855
LB
434}
435
d62a17ae 436void bgp_encap_type_mpls_in_gre_to_tlv(
437 struct bgp_encap_type_mpls_in_gre *bet, /* input structure */
438 struct attr *attr)
f4c89855 439{
d62a17ae 440 attr->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_GRE;
f4c89855
LB
441}
442
d62a17ae 443void bgp_encap_type_vxlan_gpe_to_tlv(
444 struct bgp_encap_type_vxlan_gpe *bet, /* input structure */
445 struct attr *attr)
f4c89855 446{
f4c89855 447
d62a17ae 448 attr->encap_tunneltype = BGP_ENCAP_TYPE_VXLAN_GPE;
f4c89855
LB
449}
450
d62a17ae 451void bgp_encap_type_mpls_in_udp_to_tlv(
452 struct bgp_encap_type_mpls_in_udp *bet, /* input structure */
453 struct attr *attr)
f4c89855 454{
f4c89855 455
d62a17ae 456 attr->encap_tunneltype = BGP_ENCAP_TYPE_MPLS_IN_UDP;
f4c89855
LB
457}
458
459
460/***********************************************************************
461 * SUBTLV DECODE
462 ***********************************************************************/
463/* rfc5512 4.1 */
d62a17ae 464static int subtlv_decode_encap_l2tpv3_over_ip(
465 struct bgp_attr_encap_subtlv *subtlv,
466 struct bgp_tea_subtlv_encap_l2tpv3_over_ip *st)
467{
468 if (subtlv->length < 4) {
469 zlog_debug("%s, subtlv length %d is less than 4", __func__,
470 subtlv->length);
471 return -1;
472 }
473
937652c6 474 ptr_get_be32(subtlv->value, &st->sessionid);
d62a17ae 475 st->cookie_length = subtlv->length - 4;
476 if (st->cookie_length > sizeof(st->cookie)) {
477 zlog_debug("%s, subtlv length %d is greater than %d", __func__,
478 st->cookie_length, (int)sizeof(st->cookie));
479 return -1;
480 }
481 memcpy(st->cookie, subtlv->value + 4, st->cookie_length);
482 return 0;
f4c89855
LB
483}
484
485/* rfc5512 4.1 */
d62a17ae 486static int subtlv_decode_encap_gre(struct bgp_attr_encap_subtlv *subtlv,
487 struct bgp_tea_subtlv_encap_gre_key *st)
488{
489 if (subtlv->length != 4) {
490 zlog_debug("%s, subtlv length %d does not equal 4", __func__,
491 subtlv->length);
492 return -1;
493 }
937652c6 494 ptr_get_be32(subtlv->value, &st->gre_key);
d62a17ae 495 return 0;
f4c89855
LB
496}
497
d62a17ae 498static int subtlv_decode_encap_pbb(struct bgp_attr_encap_subtlv *subtlv,
499 struct bgp_tea_subtlv_encap_pbb *st)
500{
501 if (subtlv->length != 1 + 3 + 6 + 2) {
502 zlog_debug("%s, subtlv length %d does not equal %d", __func__,
503 subtlv->length, 1 + 3 + 6 + 2);
504 return -1;
505 }
506 if (subtlv->value[0] & 0x80) {
507 st->flag_isid = 1;
508 st->isid = (subtlv->value[1] << 16) | (subtlv->value[2] << 8)
509 | subtlv->value[3];
510 }
511 if (subtlv->value[0] & 0x40) {
512 st->flag_vid = 1;
513 st->vid = ((subtlv->value[10] & 0x0f) << 8) | subtlv->value[11];
514 }
515 memcpy(st->macaddr, subtlv->value + 4, 6);
516 return 0;
f4c89855
LB
517}
518
519/* rfc5512 4.2 */
d62a17ae 520static int subtlv_decode_proto_type(struct bgp_attr_encap_subtlv *subtlv,
521 struct bgp_tea_subtlv_proto_type *st)
f4c89855 522{
d62a17ae 523 if (subtlv->length != 2) {
524 zlog_debug("%s, subtlv length %d does not equal 2", __func__,
525 subtlv->length);
526 return -1;
527 }
528 st->proto = (subtlv->value[0] << 8) | subtlv->value[1];
529 return 0;
f4c89855
LB
530}
531
532/* rfc5512 4.3 */
d62a17ae 533static int subtlv_decode_color(struct bgp_attr_encap_subtlv *subtlv,
534 struct bgp_tea_subtlv_color *st)
535{
536 if (subtlv->length != 8) {
537 zlog_debug("%s, subtlv length %d does not equal 8", __func__,
538 subtlv->length);
539 return -1;
540 }
541 if ((subtlv->value[0] != 0x03) || (subtlv->value[1] != 0x0b)
542 || (subtlv->value[2] != 0) || (subtlv->value[3] != 0)) {
543 zlog_debug("%s, subtlv value 1st 4 bytes are not 0x030b0000",
544 __func__);
545 return -1;
546 }
937652c6 547 ptr_get_be32(subtlv->value + 4, &st->color);
d62a17ae 548 return 0;
f4c89855
LB
549}
550
551/* rfc 5566 4. */
d62a17ae 552static int subtlv_decode_ipsec_ta(struct bgp_attr_encap_subtlv *subtlv,
553 struct bgp_tea_subtlv_ipsec_ta *st)
554{
555 st->authenticator_length = subtlv->length - 2;
556 if (st->authenticator_length > sizeof(st->value)) {
557 zlog_debug(
558 "%s, authenticator length %d exceeds storage maximum %d",
559 __func__, st->authenticator_length,
560 (int)sizeof(st->value));
561 return -1;
562 }
563 st->authenticator_type = (subtlv->value[0] << 8) | subtlv->value[1];
564 memcpy(st->value, subtlv->value + 2, st->authenticator_length);
565 return 0;
f4c89855
LB
566}
567
587ff0fd
LB
568/* draft-rosen-idr-tunnel-encaps 2.1 */
569static int
d62a17ae 570subtlv_decode_remote_endpoint(struct bgp_attr_encap_subtlv *subtlv,
571 struct bgp_tea_subtlv_remote_endpoint *st)
572{
573 int i;
574 if (subtlv->length != 8 && subtlv->length != 20) {
575 zlog_debug("%s, subtlv length %d does not equal 8 or 20",
576 __func__, subtlv->length);
577 return -1;
578 }
579 if (subtlv->length == 8) {
580 st->family = AF_INET;
937652c6 581 memcpy(&st->ip_address.v4.s_addr, subtlv->value, 4);
d62a17ae 582 } else {
583 st->family = AF_INET6;
584 memcpy(&(st->ip_address.v6.s6_addr), subtlv->value, 16);
585 }
586 i = subtlv->length - 4;
937652c6 587 ptr_get_be32(subtlv->value + i, &st->as4);
d62a17ae 588 return 0;
587ff0fd
LB
589}
590
f4c89855
LB
591/***********************************************************************
592 * TUNNEL TYPE-SPECIFIC TLV DECODE
593 ***********************************************************************/
594
d62a17ae 595int tlv_to_bgp_encap_type_l2tpv3overip(
596 struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
597 struct bgp_encap_type_l2tpv3_over_ip *bet) /* caller-allocated */
598{
599 struct bgp_attr_encap_subtlv *st;
600 int rc = 0;
601
602 for (st = stlv; st; st = st->next) {
603 switch (st->type) {
604 case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
605 rc |= subtlv_decode_encap_l2tpv3_over_ip(
606 st, &bet->st_encap);
607 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
608 break;
609
610 case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
611 rc |= subtlv_decode_proto_type(st, &bet->st_proto);
612 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
613 break;
614
615 case BGP_ENCAP_SUBTLV_TYPE_COLOR:
616 rc |= subtlv_decode_color(st, &bet->st_color);
617 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
618 break;
619
620 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
621 rc |= subtlv_decode_remote_endpoint(st,
622 &bet->st_endpoint);
623 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
624 break;
625
626 default:
627 zlog_debug("%s: unexpected subtlv type %d", __func__,
628 st->type);
629 rc |= -1;
630 break;
631 }
f4c89855 632 }
d62a17ae 633 return rc;
634}
635
636int tlv_to_bgp_encap_type_gre(
637 struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
638 struct bgp_encap_type_gre *bet) /* caller-allocated */
639{
640 struct bgp_attr_encap_subtlv *st;
641 int rc = 0;
642
643 for (st = stlv; st; st = st->next) {
644 switch (st->type) {
645 case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
646 rc |= subtlv_decode_encap_gre(st, &bet->st_encap);
647 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
648 break;
649
650 case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
651 rc |= subtlv_decode_proto_type(st, &bet->st_proto);
652 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
653 break;
654
655 case BGP_ENCAP_SUBTLV_TYPE_COLOR:
656 rc |= subtlv_decode_color(st, &bet->st_color);
657 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
658 break;
659
660 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
661 rc |= subtlv_decode_remote_endpoint(st,
662 &bet->st_endpoint);
663 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
664 break;
665
666 default:
667 zlog_debug("%s: unexpected subtlv type %d", __func__,
668 st->type);
669 rc |= -1;
670 break;
671 }
f4c89855 672 }
d62a17ae 673 return rc;
674}
675
676int tlv_to_bgp_encap_type_ip_in_ip(
677 struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
678 struct bgp_encap_type_ip_in_ip *bet) /* caller-allocated */
679{
680 struct bgp_attr_encap_subtlv *st;
681 int rc = 0;
682
683 for (st = stlv; st; st = st->next) {
684 switch (st->type) {
685 case BGP_ENCAP_SUBTLV_TYPE_PROTO_TYPE:
686 rc |= subtlv_decode_proto_type(st, &bet->st_proto);
687 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_PROTO_TYPE);
688 break;
689
690 case BGP_ENCAP_SUBTLV_TYPE_COLOR:
691 rc |= subtlv_decode_color(st, &bet->st_color);
692 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_COLOR);
693 break;
694
695 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
696 rc |= subtlv_decode_remote_endpoint(st,
697 &bet->st_endpoint);
698 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
699 break;
700
701 default:
702 zlog_debug("%s: unexpected subtlv type %d", __func__,
703 st->type);
704 rc |= -1;
705 break;
706 }
f4c89855 707 }
d62a17ae 708 return rc;
f4c89855
LB
709}
710
d62a17ae 711int tlv_to_bgp_encap_type_transmit_tunnel_endpoint(
712 struct bgp_attr_encap_subtlv *stlv,
713 struct bgp_encap_type_transmit_tunnel_endpoint *bet)
f4c89855 714{
d62a17ae 715 struct bgp_attr_encap_subtlv *st;
716 int rc = 0;
f4c89855 717
d62a17ae 718 for (st = stlv; st; st = st->next) {
719 switch (st->type) {
587ff0fd 720
d62a17ae 721 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
722 rc |= subtlv_decode_remote_endpoint(st,
723 &bet->st_endpoint);
724 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
725 break;
587ff0fd 726
d62a17ae 727 default:
728 zlog_debug("%s: unexpected subtlv type %d", __func__,
729 st->type);
730 rc |= -1;
731 break;
732 }
f4c89855 733 }
d62a17ae 734 return rc;
735}
736
737int tlv_to_bgp_encap_type_ipsec_in_tunnel_mode(
738 struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
739 struct bgp_encap_type_ipsec_in_tunnel_mode *bet) /* caller-allocated */
740{
741 struct bgp_attr_encap_subtlv *st;
742 int rc = 0;
743
744 for (st = stlv; st; st = st->next) {
745 switch (st->type) {
746 case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
747 rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
748 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
749 break;
750
751 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
752 rc |= subtlv_decode_remote_endpoint(st,
753 &bet->st_endpoint);
754 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
755 break;
756
757 default:
758 zlog_debug("%s: unexpected subtlv type %d", __func__,
759 st->type);
760 rc |= -1;
761 break;
762 }
f4c89855 763 }
d62a17ae 764 return rc;
765}
766
767int tlv_to_bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode(
768 struct bgp_attr_encap_subtlv *stlv,
769 struct bgp_encap_type_ip_in_ip_tunnel_with_ipsec_transport_mode *bet)
770{
771 struct bgp_attr_encap_subtlv *st;
772 int rc = 0;
773
774 for (st = stlv; st; st = st->next) {
775 switch (st->type) {
776 case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
777 rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
778 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
779 break;
780
781 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
782 rc |= subtlv_decode_remote_endpoint(st,
783 &bet->st_endpoint);
784 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
785 break;
786
787 default:
788 zlog_debug("%s: unexpected subtlv type %d", __func__,
789 st->type);
790 rc |= -1;
791 break;
792 }
f4c89855 793 }
d62a17ae 794 return rc;
795}
796
797int tlv_to_bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode(
798 struct bgp_attr_encap_subtlv *stlv,
799 struct bgp_encap_type_mpls_in_ip_tunnel_with_ipsec_transport_mode *bet)
800{
801 struct bgp_attr_encap_subtlv *st;
802 int rc = 0;
803
804 for (st = stlv; st; st = st->next) {
805 switch (st->type) {
806 case BGP_ENCAP_SUBTLV_TYPE_IPSEC_TA:
807 rc |= subtlv_decode_ipsec_ta(st, &bet->st_ipsec_ta);
808 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_IPSEC_TA);
809 break;
810
811 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
812 rc |= subtlv_decode_remote_endpoint(st,
813 &bet->st_endpoint);
814 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
815 break;
816
817 default:
818 zlog_debug("%s: unexpected subtlv type %d", __func__,
819 st->type);
820 rc |= -1;
821 break;
822 }
f4c89855 823 }
d62a17ae 824 return rc;
f4c89855
LB
825}
826
d62a17ae 827int tlv_to_bgp_encap_type_vxlan(struct bgp_attr_encap_subtlv *stlv,
828 struct bgp_encap_type_vxlan *bet)
f4c89855 829{
d62a17ae 830 struct bgp_attr_encap_subtlv *st;
831 int rc = 0;
f4c89855 832
d62a17ae 833 for (st = stlv; st; st = st->next) {
834 switch (st->type) {
587ff0fd 835
d62a17ae 836 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
837 rc |= subtlv_decode_remote_endpoint(st,
838 &bet->st_endpoint);
839 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
840 break;
587ff0fd 841
d62a17ae 842 default:
843 zlog_debug("%s: unexpected subtlv type %d", __func__,
844 st->type);
845 rc |= -1;
846 break;
847 }
f4c89855 848 }
d62a17ae 849 return rc;
f4c89855
LB
850}
851
d62a17ae 852int tlv_to_bgp_encap_type_nvgre(struct bgp_attr_encap_subtlv *stlv,
853 struct bgp_encap_type_nvgre *bet)
f4c89855 854{
d62a17ae 855 struct bgp_attr_encap_subtlv *st;
856 int rc = 0;
f4c89855 857
d62a17ae 858 for (st = stlv; st; st = st->next) {
859 switch (st->type) {
587ff0fd 860
d62a17ae 861 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
862 rc |= subtlv_decode_remote_endpoint(st,
863 &bet->st_endpoint);
864 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
865 break;
587ff0fd 866
d62a17ae 867 default:
868 zlog_debug("%s: unexpected subtlv type %d", __func__,
869 st->type);
870 rc |= -1;
871 break;
872 }
f4c89855 873 }
d62a17ae 874 return rc;
f4c89855
LB
875}
876
d62a17ae 877int tlv_to_bgp_encap_type_mpls(struct bgp_attr_encap_subtlv *stlv,
878 struct bgp_encap_type_mpls *bet)
f4c89855 879{
d62a17ae 880 struct bgp_attr_encap_subtlv *st;
881 int rc = 0;
f4c89855 882
d62a17ae 883 for (st = stlv; st; st = st->next) {
884 switch (st->type) {
587ff0fd 885
d62a17ae 886 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
887 rc |= subtlv_decode_remote_endpoint(st,
888 &bet->st_endpoint);
889 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
890 break;
587ff0fd 891
d62a17ae 892 default:
893 zlog_debug("%s: unexpected subtlv type %d", __func__,
894 st->type);
895 rc |= -1;
896 break;
897 }
f4c89855 898 }
d62a17ae 899 return rc;
f4c89855
LB
900}
901
d62a17ae 902int tlv_to_bgp_encap_type_mpls_in_gre(struct bgp_attr_encap_subtlv *stlv,
903 struct bgp_encap_type_mpls_in_gre *bet)
f4c89855 904{
d62a17ae 905 struct bgp_attr_encap_subtlv *st;
906 int rc = 0;
f4c89855 907
d62a17ae 908 for (st = stlv; st; st = st->next) {
909 switch (st->type) {
587ff0fd 910
d62a17ae 911 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
912 rc |= subtlv_decode_remote_endpoint(st,
913 &bet->st_endpoint);
914 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
915 break;
587ff0fd 916
d62a17ae 917 default:
918 zlog_debug("%s: unexpected subtlv type %d", __func__,
919 st->type);
920 rc |= -1;
921 break;
922 }
f4c89855 923 }
d62a17ae 924 return rc;
f4c89855
LB
925}
926
d62a17ae 927int tlv_to_bgp_encap_type_vxlan_gpe(struct bgp_attr_encap_subtlv *stlv,
928 struct bgp_encap_type_vxlan_gpe *bet)
f4c89855 929{
d62a17ae 930 struct bgp_attr_encap_subtlv *st;
931 int rc = 0;
f4c89855 932
d62a17ae 933 for (st = stlv; st; st = st->next) {
934 switch (st->type) {
587ff0fd 935
d62a17ae 936 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
937 rc |= subtlv_decode_remote_endpoint(st,
938 &bet->st_endpoint);
939 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
940 break;
587ff0fd 941
d62a17ae 942 default:
943 zlog_debug("%s: unexpected subtlv type %d", __func__,
944 st->type);
945 rc |= -1;
946 break;
947 }
f4c89855 948 }
d62a17ae 949 return rc;
f4c89855
LB
950}
951
d62a17ae 952int tlv_to_bgp_encap_type_mpls_in_udp(struct bgp_attr_encap_subtlv *stlv,
953 struct bgp_encap_type_mpls_in_udp *bet)
f4c89855 954{
d62a17ae 955 struct bgp_attr_encap_subtlv *st;
956 int rc = 0;
f4c89855 957
d62a17ae 958 for (st = stlv; st; st = st->next) {
959 switch (st->type) {
587ff0fd 960
d62a17ae 961 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
962 rc |= subtlv_decode_remote_endpoint(st,
963 &bet->st_endpoint);
964 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
965 break;
587ff0fd 966
d62a17ae 967 default:
968 zlog_debug("%s: unexpected subtlv type %d", __func__,
969 st->type);
970 rc |= -1;
971 break;
972 }
f4c89855 973 }
d62a17ae 974 return rc;
975}
976
977int tlv_to_bgp_encap_type_pbb(
978 struct bgp_attr_encap_subtlv *stlv, /* subtlv chain */
979 struct bgp_encap_type_pbb *bet) /* caller-allocated */
980{
981 struct bgp_attr_encap_subtlv *st;
982 int rc = 0;
983
984 for (st = stlv; st; st = st->next) {
985 switch (st->type) {
986 case BGP_ENCAP_SUBTLV_TYPE_ENCAPSULATION:
987 rc |= subtlv_decode_encap_pbb(st, &bet->st_encap);
988 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_ENCAP);
989 break;
990
991 case BGP_ENCAP_SUBTLV_TYPE_REMOTE_ENDPOINT:
992 rc |= subtlv_decode_remote_endpoint(st,
993 &bet->st_endpoint);
994 SET_SUBTLV_FLAG(bet, BGP_TEA_SUBTLV_REMOTE_ENDPOINT);
995 break;
996
997 default:
998 zlog_debug("%s: unexpected subtlv type %d", __func__,
999 st->type);
1000 rc |= -1;
1001 break;
1002 }
f4c89855 1003 }
d62a17ae 1004 return rc;
f4c89855 1005}