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