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
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Roopa Prabhu, <roopa@cumulusnetworks.com>
10 * Thomas Graf <tgraf@suug.ch>
19 #include <linux/ila.h>
20 #include <linux/lwtunnel.h>
21 #include <linux/mpls_iptunnel.h>
27 #include "ip_common.h"
28 #include "ila_common.h"
30 #include <linux/seg6.h>
31 #include <linux/seg6_iptunnel.h>
32 #include <linux/seg6_hmac.h>
33 #include <linux/seg6_local.h>
36 static const char *format_encap_type(int type
)
39 case LWTUNNEL_ENCAP_MPLS
:
41 case LWTUNNEL_ENCAP_IP
:
43 case LWTUNNEL_ENCAP_IP6
:
45 case LWTUNNEL_ENCAP_ILA
:
47 case LWTUNNEL_ENCAP_BPF
:
49 case LWTUNNEL_ENCAP_SEG6
:
51 case LWTUNNEL_ENCAP_SEG6_LOCAL
:
58 static void encap_type_usage(void)
62 fprintf(stderr
, "Usage: ip route ... encap TYPE [ OPTIONS ] [...]\n");
64 for (i
= 1; i
<= LWTUNNEL_ENCAP_MAX
; i
++)
65 fprintf(stderr
, "%s %s\n", format_encap_type(i
),
66 i
== 1 ? "TYPE := " : " ");
71 static int read_encap_type(const char *name
)
73 if (strcmp(name
, "mpls") == 0)
74 return LWTUNNEL_ENCAP_MPLS
;
75 else if (strcmp(name
, "ip") == 0)
76 return LWTUNNEL_ENCAP_IP
;
77 else if (strcmp(name
, "ip6") == 0)
78 return LWTUNNEL_ENCAP_IP6
;
79 else if (strcmp(name
, "ila") == 0)
80 return LWTUNNEL_ENCAP_ILA
;
81 else if (strcmp(name
, "bpf") == 0)
82 return LWTUNNEL_ENCAP_BPF
;
83 else if (strcmp(name
, "seg6") == 0)
84 return LWTUNNEL_ENCAP_SEG6
;
85 else if (strcmp(name
, "seg6local") == 0)
86 return LWTUNNEL_ENCAP_SEG6_LOCAL
;
87 else if (strcmp(name
, "help") == 0)
90 return LWTUNNEL_ENCAP_NONE
;
93 static void print_srh(FILE *fp
, struct ipv6_sr_hdr
*srh
)
97 if (is_json_context())
98 open_json_array(PRINT_JSON
, "segs");
100 fprintf(fp
, "segs %d [ ", srh
->first_segment
+ 1);
102 for (i
= srh
->first_segment
; i
>= 0; i
--)
103 print_color_string(PRINT_ANY
, COLOR_INET6
,
105 rt_addr_n2a(AF_INET6
, 16, &srh
->segments
[i
]));
107 if (is_json_context())
108 close_json_array(PRINT_JSON
, NULL
);
112 if (sr_has_hmac(srh
)) {
113 unsigned int offset
= ((srh
->hdrlen
+ 1) << 3) - 40;
114 struct sr6_tlv_hmac
*tlv
;
116 tlv
= (struct sr6_tlv_hmac
*)((char *)srh
+ offset
);
117 print_0xhex(PRINT_ANY
, "hmac",
118 "hmac 0x%X ", ntohl(tlv
->hmackeyid
));
122 static const char *seg6_mode_types
[] = {
123 [SEG6_IPTUN_MODE_INLINE
] = "inline",
124 [SEG6_IPTUN_MODE_ENCAP
] = "encap",
125 [SEG6_IPTUN_MODE_L2ENCAP
] = "l2encap",
128 static const char *format_seg6mode_type(int mode
)
130 if (mode
< 0 || mode
> ARRAY_SIZE(seg6_mode_types
))
133 return seg6_mode_types
[mode
];
136 static int read_seg6mode_type(const char *mode
)
140 for (i
= 0; i
< ARRAY_SIZE(seg6_mode_types
); i
++) {
141 if (strcmp(mode
, seg6_mode_types
[i
]) == 0)
148 static void print_encap_seg6(FILE *fp
, struct rtattr
*encap
)
150 struct rtattr
*tb
[SEG6_IPTUNNEL_MAX
+1];
151 struct seg6_iptunnel_encap
*tuninfo
;
153 parse_rtattr_nested(tb
, SEG6_IPTUNNEL_MAX
, encap
);
155 if (!tb
[SEG6_IPTUNNEL_SRH
])
158 tuninfo
= RTA_DATA(tb
[SEG6_IPTUNNEL_SRH
]);
159 print_string(PRINT_ANY
, "mode",
160 "mode %s ", format_seg6mode_type(tuninfo
->mode
));
162 print_srh(fp
, tuninfo
->srh
);
165 static const char *seg6_action_names
[SEG6_LOCAL_ACTION_MAX
+ 1] = {
166 [SEG6_LOCAL_ACTION_END
] = "End",
167 [SEG6_LOCAL_ACTION_END_X
] = "End.X",
168 [SEG6_LOCAL_ACTION_END_T
] = "End.T",
169 [SEG6_LOCAL_ACTION_END_DX2
] = "End.DX2",
170 [SEG6_LOCAL_ACTION_END_DX6
] = "End.DX6",
171 [SEG6_LOCAL_ACTION_END_DX4
] = "End.DX4",
172 [SEG6_LOCAL_ACTION_END_DT6
] = "End.DT6",
173 [SEG6_LOCAL_ACTION_END_DT4
] = "End.DT4",
174 [SEG6_LOCAL_ACTION_END_B6
] = "End.B6",
175 [SEG6_LOCAL_ACTION_END_B6_ENCAP
] = "End.B6.Encaps",
176 [SEG6_LOCAL_ACTION_END_BM
] = "End.BM",
177 [SEG6_LOCAL_ACTION_END_S
] = "End.S",
178 [SEG6_LOCAL_ACTION_END_AS
] = "End.AS",
179 [SEG6_LOCAL_ACTION_END_AM
] = "End.AM",
180 [SEG6_LOCAL_ACTION_END_BPF
] = "End.BPF",
183 static const char *format_action_type(int action
)
185 if (action
< 0 || action
> SEG6_LOCAL_ACTION_MAX
)
188 return seg6_action_names
[action
] ?: "<unknown>";
191 static int read_action_type(const char *name
)
195 for (i
= 0; i
< SEG6_LOCAL_ACTION_MAX
+ 1; i
++) {
196 if (!seg6_action_names
[i
])
199 if (strcmp(seg6_action_names
[i
], name
) == 0)
203 return SEG6_LOCAL_ACTION_UNSPEC
;
206 static void print_encap_bpf_prog(FILE *fp
, struct rtattr
*encap
,
209 struct rtattr
*tb
[LWT_BPF_PROG_MAX
+1];
210 const char *progname
= NULL
;
212 parse_rtattr_nested(tb
, LWT_BPF_PROG_MAX
, encap
);
214 if (tb
[LWT_BPF_PROG_NAME
])
215 progname
= rta_getattr_str(tb
[LWT_BPF_PROG_NAME
]);
217 if (is_json_context())
218 print_string(PRINT_JSON
, str
, NULL
,
219 progname
? : "<unknown>");
221 fprintf(fp
, "%s ", str
);
223 fprintf(fp
, "%s ", progname
);
227 static void print_encap_seg6local(FILE *fp
, struct rtattr
*encap
)
229 struct rtattr
*tb
[SEG6_LOCAL_MAX
+ 1];
232 parse_rtattr_nested(tb
, SEG6_LOCAL_MAX
, encap
);
234 if (!tb
[SEG6_LOCAL_ACTION
])
237 action
= rta_getattr_u32(tb
[SEG6_LOCAL_ACTION
]);
239 print_string(PRINT_ANY
, "action",
240 "action %s ", format_action_type(action
));
242 if (tb
[SEG6_LOCAL_SRH
]) {
243 open_json_object("srh");
244 print_srh(fp
, RTA_DATA(tb
[SEG6_LOCAL_SRH
]));
248 if (tb
[SEG6_LOCAL_TABLE
])
249 print_uint(PRINT_ANY
, "table",
250 "table %u ", rta_getattr_u32(tb
[SEG6_LOCAL_TABLE
]));
252 if (tb
[SEG6_LOCAL_NH4
]) {
253 print_string(PRINT_ANY
, "nh4",
254 "nh4 %s ", rt_addr_n2a_rta(AF_INET
, tb
[SEG6_LOCAL_NH4
]));
257 if (tb
[SEG6_LOCAL_NH6
]) {
258 print_string(PRINT_ANY
, "nh6",
259 "nh6 %s ", rt_addr_n2a_rta(AF_INET6
, tb
[SEG6_LOCAL_NH6
]));
262 if (tb
[SEG6_LOCAL_IIF
]) {
263 int iif
= rta_getattr_u32(tb
[SEG6_LOCAL_IIF
]);
265 print_string(PRINT_ANY
, "iif",
266 "iif %s ", ll_index_to_name(iif
));
269 if (tb
[SEG6_LOCAL_OIF
]) {
270 int oif
= rta_getattr_u32(tb
[SEG6_LOCAL_OIF
]);
272 print_string(PRINT_ANY
, "oif",
273 "oif %s ", ll_index_to_name(oif
));
276 if (tb
[SEG6_LOCAL_BPF
])
277 print_encap_bpf_prog(fp
, tb
[SEG6_LOCAL_BPF
], "endpoint");
280 static void print_encap_mpls(FILE *fp
, struct rtattr
*encap
)
282 struct rtattr
*tb
[MPLS_IPTUNNEL_MAX
+1];
284 parse_rtattr_nested(tb
, MPLS_IPTUNNEL_MAX
, encap
);
286 if (tb
[MPLS_IPTUNNEL_DST
])
287 print_string(PRINT_ANY
, "dst", " %s ",
288 format_host_rta(AF_MPLS
, tb
[MPLS_IPTUNNEL_DST
]));
289 if (tb
[MPLS_IPTUNNEL_TTL
])
290 print_uint(PRINT_ANY
, "ttl", "ttl %u ",
291 rta_getattr_u8(tb
[MPLS_IPTUNNEL_TTL
]));
294 static void print_encap_ip(FILE *fp
, struct rtattr
*encap
)
296 struct rtattr
*tb
[LWTUNNEL_IP_MAX
+1];
298 parse_rtattr_nested(tb
, LWTUNNEL_IP_MAX
, encap
);
300 if (tb
[LWTUNNEL_IP_ID
])
301 print_u64(PRINT_ANY
, "id", "id %llu ",
302 ntohll(rta_getattr_u64(tb
[LWTUNNEL_IP_ID
])));
304 if (tb
[LWTUNNEL_IP_SRC
])
305 print_color_string(PRINT_ANY
, COLOR_INET
,
307 rt_addr_n2a_rta(AF_INET
, tb
[LWTUNNEL_IP_SRC
]));
309 if (tb
[LWTUNNEL_IP_DST
])
310 print_color_string(PRINT_ANY
, COLOR_INET
,
312 rt_addr_n2a_rta(AF_INET
, tb
[LWTUNNEL_IP_DST
]));
314 if (tb
[LWTUNNEL_IP_TTL
])
315 print_uint(PRINT_ANY
, "ttl",
316 "ttl %u ", rta_getattr_u8(tb
[LWTUNNEL_IP_TTL
]));
318 if (tb
[LWTUNNEL_IP_TOS
])
319 print_uint(PRINT_ANY
, "tos",
320 "tos %d ", rta_getattr_u8(tb
[LWTUNNEL_IP_TOS
]));
323 static void print_encap_ila(FILE *fp
, struct rtattr
*encap
)
325 struct rtattr
*tb
[ILA_ATTR_MAX
+1];
327 parse_rtattr_nested(tb
, ILA_ATTR_MAX
, encap
);
329 if (tb
[ILA_ATTR_LOCATOR
]) {
330 char abuf
[ADDR64_BUF_SIZE
];
332 addr64_n2a(rta_getattr_u64(tb
[ILA_ATTR_LOCATOR
]),
334 print_string(PRINT_ANY
, "locator",
338 if (tb
[ILA_ATTR_CSUM_MODE
])
339 print_string(PRINT_ANY
, "csum_mode",
341 ila_csum_mode2name(rta_getattr_u8(tb
[ILA_ATTR_CSUM_MODE
])));
343 if (tb
[ILA_ATTR_IDENT_TYPE
])
344 print_string(PRINT_ANY
, "ident_type",
346 ila_ident_type2name(rta_getattr_u8(tb
[ILA_ATTR_IDENT_TYPE
])));
348 if (tb
[ILA_ATTR_HOOK_TYPE
])
349 print_string(PRINT_ANY
, "hook_type",
351 ila_hook_type2name(rta_getattr_u8(tb
[ILA_ATTR_HOOK_TYPE
])));
354 static void print_encap_ip6(FILE *fp
, struct rtattr
*encap
)
356 struct rtattr
*tb
[LWTUNNEL_IP6_MAX
+1];
358 parse_rtattr_nested(tb
, LWTUNNEL_IP6_MAX
, encap
);
360 if (tb
[LWTUNNEL_IP6_ID
])
361 print_u64(PRINT_ANY
, "id", "id %llu ",
362 ntohll(rta_getattr_u64(tb
[LWTUNNEL_IP6_ID
])));
364 if (tb
[LWTUNNEL_IP6_SRC
])
365 print_color_string(PRINT_ANY
, COLOR_INET6
,
367 rt_addr_n2a_rta(AF_INET6
, tb
[LWTUNNEL_IP6_SRC
]));
369 if (tb
[LWTUNNEL_IP6_DST
])
370 print_color_string(PRINT_ANY
, COLOR_INET6
,
372 rt_addr_n2a_rta(AF_INET6
, tb
[LWTUNNEL_IP6_DST
]));
374 if (tb
[LWTUNNEL_IP6_HOPLIMIT
])
375 print_u64(PRINT_ANY
, "hoplimit",
377 rta_getattr_u8(tb
[LWTUNNEL_IP6_HOPLIMIT
]));
379 if (tb
[LWTUNNEL_IP6_TC
])
380 print_uint(PRINT_ANY
, "tc",
381 "tc %u ", rta_getattr_u8(tb
[LWTUNNEL_IP6_TC
]));
384 static void print_encap_bpf(FILE *fp
, struct rtattr
*encap
)
386 struct rtattr
*tb
[LWT_BPF_MAX
+1];
388 parse_rtattr_nested(tb
, LWT_BPF_MAX
, encap
);
391 print_encap_bpf_prog(fp
, tb
[LWT_BPF_IN
], "in");
393 print_encap_bpf_prog(fp
, tb
[LWT_BPF_OUT
], "out");
394 if (tb
[LWT_BPF_XMIT
])
395 print_encap_bpf_prog(fp
, tb
[LWT_BPF_XMIT
], "xmit");
396 if (tb
[LWT_BPF_XMIT_HEADROOM
])
397 print_uint(PRINT_ANY
, "headroom",
398 " %u ", rta_getattr_u32(tb
[LWT_BPF_XMIT_HEADROOM
]));
401 void lwt_print_encap(FILE *fp
, struct rtattr
*encap_type
,
402 struct rtattr
*encap
)
409 et
= rta_getattr_u16(encap_type
);
411 print_string(PRINT_ANY
, "encap", " encap %s ", format_encap_type(et
));
414 case LWTUNNEL_ENCAP_MPLS
:
415 print_encap_mpls(fp
, encap
);
417 case LWTUNNEL_ENCAP_IP
:
418 print_encap_ip(fp
, encap
);
420 case LWTUNNEL_ENCAP_ILA
:
421 print_encap_ila(fp
, encap
);
423 case LWTUNNEL_ENCAP_IP6
:
424 print_encap_ip6(fp
, encap
);
426 case LWTUNNEL_ENCAP_BPF
:
427 print_encap_bpf(fp
, encap
);
429 case LWTUNNEL_ENCAP_SEG6
:
430 print_encap_seg6(fp
, encap
);
432 case LWTUNNEL_ENCAP_SEG6_LOCAL
:
433 print_encap_seg6local(fp
, encap
);
438 static struct ipv6_sr_hdr
*parse_srh(char *segbuf
, int hmac
, bool encap
)
440 struct ipv6_sr_hdr
*srh
;
447 for (i
= 0; *s
; *s
++ == ',' ? i
++ : *s
);
453 srhlen
= 8 + 16*nsegs
;
458 srh
= malloc(srhlen
);
459 memset(srh
, 0, srhlen
);
461 srh
->hdrlen
= (srhlen
>> 3) - 1;
463 srh
->segments_left
= nsegs
- 1;
464 srh
->first_segment
= nsegs
- 1;
467 srh
->flags
|= SR6_FLAG1_HMAC
;
469 i
= srh
->first_segment
;
470 for (s
= strtok(segbuf
, ","); s
; s
= strtok(NULL
, ",")) {
473 get_addr(&addr
, s
, AF_INET6
);
474 memcpy(&srh
->segments
[i
], addr
.data
, sizeof(struct in6_addr
));
479 struct sr6_tlv_hmac
*tlv
;
481 tlv
= (struct sr6_tlv_hmac
*)((char *)srh
+ srhlen
- 40);
482 tlv
->tlvhdr
.type
= SR6_TLV_HMAC
;
483 tlv
->tlvhdr
.len
= 38;
484 tlv
->hmackeyid
= htonl(hmac
);
490 static int parse_encap_seg6(struct rtattr
*rta
, size_t len
, int *argcp
,
493 int mode_ok
= 0, segs_ok
= 0, hmac_ok
= 0;
494 struct seg6_iptunnel_encap
*tuninfo
;
495 struct ipv6_sr_hdr
*srh
;
496 char **argv
= *argvp
;
504 if (strcmp(*argv
, "mode") == 0) {
507 duparg2("mode", *argv
);
508 encap
= read_seg6mode_type(*argv
);
510 invarg("\"mode\" value is invalid\n", *argv
);
511 } else if (strcmp(*argv
, "segs") == 0) {
514 duparg2("segs", *argv
);
516 invarg("\"segs\" provided before \"mode\"\n",
519 strlcpy(segbuf
, *argv
, 1024);
520 } else if (strcmp(*argv
, "hmac") == 0) {
523 duparg2("hmac", *argv
);
524 get_u32(&hmac
, *argv
, 0);
531 srh
= parse_srh(segbuf
, hmac
, encap
);
532 srhlen
= (srh
->hdrlen
+ 1) << 3;
534 tuninfo
= malloc(sizeof(*tuninfo
) + srhlen
);
535 memset(tuninfo
, 0, sizeof(*tuninfo
) + srhlen
);
537 tuninfo
->mode
= encap
;
539 memcpy(tuninfo
->srh
, srh
, srhlen
);
541 if (rta_addattr_l(rta
, len
, SEG6_IPTUNNEL_SRH
, tuninfo
,
542 sizeof(*tuninfo
) + srhlen
))
559 static void bpf_lwt_cb(void *lwt_ptr
, int fd
, const char *annotation
)
561 struct lwt_x
*x
= lwt_ptr
;
563 rta_addattr32(x
->rta
, x
->len
, LWT_BPF_PROG_FD
, fd
);
564 rta_addattr_l(x
->rta
, x
->len
, LWT_BPF_PROG_NAME
, annotation
,
565 strlen(annotation
) + 1);
568 static const struct bpf_cfg_ops bpf_cb_ops
= {
569 .ebpf_cb
= bpf_lwt_cb
,
572 static int lwt_parse_bpf(struct rtattr
*rta
, size_t len
,
573 int *argcp
, char ***argvp
,
574 int attr
, const enum bpf_prog_type bpf_type
)
576 struct bpf_cfg_in cfg
= {
588 nest
= rta_nest(rta
, len
, attr
);
589 err
= bpf_parse_and_load_common(&cfg
, &bpf_cb_ops
, &x
);
591 fprintf(stderr
, "Failed to parse eBPF program: %s\n",
595 rta_nest_end(rta
, nest
);
603 static int parse_encap_seg6local(struct rtattr
*rta
, size_t len
, int *argcp
,
606 int segs_ok
= 0, hmac_ok
= 0, table_ok
= 0, nh4_ok
= 0, nh6_ok
= 0;
607 int iif_ok
= 0, oif_ok
= 0, action_ok
= 0, srh_ok
= 0, bpf_ok
= 0;
608 __u32 action
= 0, table
, iif
, oif
;
609 struct ipv6_sr_hdr
*srh
;
610 char **argv
= *argvp
;
618 if (strcmp(*argv
, "action") == 0) {
621 duparg2("action", *argv
);
622 action
= read_action_type(*argv
);
624 invarg("\"action\" value is invalid\n", *argv
);
625 ret
= rta_addattr32(rta
, len
, SEG6_LOCAL_ACTION
,
627 } else if (strcmp(*argv
, "table") == 0) {
630 duparg2("table", *argv
);
631 get_u32(&table
, *argv
, 0);
632 ret
= rta_addattr32(rta
, len
, SEG6_LOCAL_TABLE
, table
);
633 } else if (strcmp(*argv
, "nh4") == 0) {
636 duparg2("nh4", *argv
);
637 get_addr(&addr
, *argv
, AF_INET
);
638 ret
= rta_addattr_l(rta
, len
, SEG6_LOCAL_NH4
,
639 &addr
.data
, addr
.bytelen
);
640 } else if (strcmp(*argv
, "nh6") == 0) {
643 duparg2("nh6", *argv
);
644 get_addr(&addr
, *argv
, AF_INET6
);
645 ret
= rta_addattr_l(rta
, len
, SEG6_LOCAL_NH6
,
646 &addr
.data
, addr
.bytelen
);
647 } else if (strcmp(*argv
, "iif") == 0) {
650 duparg2("iif", *argv
);
651 iif
= ll_name_to_index(*argv
);
654 ret
= rta_addattr32(rta
, len
, SEG6_LOCAL_IIF
, iif
);
655 } else if (strcmp(*argv
, "oif") == 0) {
658 duparg2("oif", *argv
);
659 oif
= ll_name_to_index(*argv
);
662 ret
= rta_addattr32(rta
, len
, SEG6_LOCAL_OIF
, oif
);
663 } else if (strcmp(*argv
, "srh") == 0) {
666 duparg2("srh", *argv
);
667 if (strcmp(*argv
, "segs") != 0)
668 invarg("missing \"segs\" attribute for srh\n",
672 duparg2("segs", *argv
);
673 strncpy(segbuf
, *argv
, 1024);
678 if (strcmp(*argv
, "hmac") == 0) {
681 duparg2("hmac", *argv
);
682 get_u32(&hmac
, *argv
, 0);
686 } else if (strcmp(*argv
, "endpoint") == 0) {
689 duparg2("endpoint", *argv
);
691 if (lwt_parse_bpf(rta
, len
, &argc
, &argv
, SEG6_LOCAL_BPF
,
692 BPF_PROG_TYPE_LWT_SEG6LOCAL
) < 0)
703 fprintf(stderr
, "Missing action type\n");
710 srh
= parse_srh(segbuf
, hmac
,
711 action
== SEG6_LOCAL_ACTION_END_B6_ENCAP
);
712 srhlen
= (srh
->hdrlen
+ 1) << 3;
713 ret
= rta_addattr_l(rta
, len
, SEG6_LOCAL_SRH
, srh
, srhlen
);
723 static int parse_encap_mpls(struct rtattr
*rta
, size_t len
,
724 int *argcp
, char ***argvp
)
728 char **argv
= *argvp
;
731 if (get_addr(&addr
, *argv
, AF_MPLS
)) {
733 "Error: an inet address is expected rather than \"%s\".\n",
738 if (rta_addattr_l(rta
, len
, MPLS_IPTUNNEL_DST
,
739 &addr
.data
, addr
.bytelen
))
746 if (strcmp(*argv
, "ttl") == 0) {
751 duparg2("ttl", *argv
);
752 if (get_u8(&ttl
, *argv
, 0))
753 invarg("\"ttl\" value is invalid\n", *argv
);
754 if (rta_addattr8(rta
, len
, MPLS_IPTUNNEL_TTL
, ttl
))
762 /* argv is currently the first unparsed argument,
763 * but the lwt_parse_encap() caller will move to the next,
772 static int parse_encap_ip(struct rtattr
*rta
, size_t len
,
773 int *argcp
, char ***argvp
)
775 int id_ok
= 0, dst_ok
= 0, tos_ok
= 0, ttl_ok
= 0;
776 char **argv
= *argvp
;
781 if (strcmp(*argv
, "id") == 0) {
786 duparg2("id", *argv
);
787 if (get_be64(&id
, *argv
, 0))
788 invarg("\"id\" value is invalid\n", *argv
);
789 ret
= rta_addattr64(rta
, len
, LWTUNNEL_IP_ID
, id
);
790 } else if (strcmp(*argv
, "dst") == 0) {
795 duparg2("dst", *argv
);
796 get_addr(&addr
, *argv
, AF_INET
);
797 ret
= rta_addattr_l(rta
, len
, LWTUNNEL_IP_DST
,
798 &addr
.data
, addr
.bytelen
);
799 } else if (strcmp(*argv
, "tos") == 0) {
804 duparg2("tos", *argv
);
805 if (rtnl_dsfield_a2n(&tos
, *argv
))
806 invarg("\"tos\" value is invalid\n", *argv
);
807 ret
= rta_addattr8(rta
, len
, LWTUNNEL_IP_TOS
, tos
);
808 } else if (strcmp(*argv
, "ttl") == 0) {
813 duparg2("ttl", *argv
);
814 if (get_u8(&ttl
, *argv
, 0))
815 invarg("\"ttl\" value is invalid\n", *argv
);
816 ret
= rta_addattr8(rta
, len
, LWTUNNEL_IP_TTL
, ttl
);
825 /* argv is currently the first unparsed argument,
826 * but the lwt_parse_encap() caller will move to the next,
835 static int parse_encap_ila(struct rtattr
*rta
, size_t len
,
836 int *argcp
, char ***argvp
)
840 char **argv
= *argvp
;
843 if (get_addr64(&locator
, *argv
) < 0) {
844 fprintf(stderr
, "Bad locator: %s\n", *argv
);
850 if (rta_addattr64(rta
, 1024, ILA_ATTR_LOCATOR
, locator
))
854 if (strcmp(*argv
, "csum-mode") == 0) {
859 csum_mode
= ila_csum_name2mode(*argv
);
861 invarg("\"csum-mode\" value is invalid\n",
864 ret
= rta_addattr8(rta
, 1024, ILA_ATTR_CSUM_MODE
,
868 } else if (strcmp(*argv
, "ident-type") == 0) {
873 ident_type
= ila_ident_name2type(*argv
);
875 invarg("\"ident-type\" value is invalid\n",
878 ret
= rta_addattr8(rta
, 1024, ILA_ATTR_IDENT_TYPE
,
882 } else if (strcmp(*argv
, "hook-type") == 0) {
887 hook_type
= ila_hook_name2type(*argv
);
889 invarg("\"hook-type\" value is invalid\n",
892 ret
= rta_addattr8(rta
, 1024, ILA_ATTR_HOOK_TYPE
,
903 /* argv is currently the first unparsed argument,
904 * but the lwt_parse_encap() caller will move to the next,
913 static int parse_encap_ip6(struct rtattr
*rta
, size_t len
,
914 int *argcp
, char ***argvp
)
916 int id_ok
= 0, dst_ok
= 0, tos_ok
= 0, ttl_ok
= 0;
917 char **argv
= *argvp
;
922 if (strcmp(*argv
, "id") == 0) {
927 duparg2("id", *argv
);
928 if (get_be64(&id
, *argv
, 0))
929 invarg("\"id\" value is invalid\n", *argv
);
930 ret
= rta_addattr64(rta
, len
, LWTUNNEL_IP6_ID
, id
);
931 } else if (strcmp(*argv
, "dst") == 0) {
936 duparg2("dst", *argv
);
937 get_addr(&addr
, *argv
, AF_INET6
);
938 ret
= rta_addattr_l(rta
, len
, LWTUNNEL_IP6_DST
,
939 &addr
.data
, addr
.bytelen
);
940 } else if (strcmp(*argv
, "tc") == 0) {
945 duparg2("tc", *argv
);
946 if (rtnl_dsfield_a2n(&tc
, *argv
))
947 invarg("\"tc\" value is invalid\n", *argv
);
948 ret
= rta_addattr8(rta
, len
, LWTUNNEL_IP6_TC
, tc
);
949 } else if (strcmp(*argv
, "hoplimit") == 0) {
954 duparg2("hoplimit", *argv
);
955 if (get_u8(&hoplimit
, *argv
, 0))
956 invarg("\"hoplimit\" value is invalid\n",
958 ret
= rta_addattr8(rta
, len
, LWTUNNEL_IP6_HOPLIMIT
,
968 /* argv is currently the first unparsed argument,
969 * but the lwt_parse_encap() caller will move to the next,
978 static void lwt_bpf_usage(void)
980 fprintf(stderr
, "Usage: ip route ... encap bpf [ in BPF ] [ out BPF ] [ xmit BPF ] [...]\n");
981 fprintf(stderr
, "BPF := obj FILE [ section NAME ] [ verbose ]\n");
985 static int parse_encap_bpf(struct rtattr
*rta
, size_t len
, int *argcp
,
988 char **argv
= *argvp
;
990 int headroom_set
= 0;
993 if (strcmp(*argv
, "in") == 0) {
995 if (lwt_parse_bpf(rta
, len
, &argc
, &argv
, LWT_BPF_IN
,
996 BPF_PROG_TYPE_LWT_IN
) < 0)
998 } else if (strcmp(*argv
, "out") == 0) {
1000 if (lwt_parse_bpf(rta
, len
, &argc
, &argv
, LWT_BPF_OUT
,
1001 BPF_PROG_TYPE_LWT_OUT
) < 0)
1003 } else if (strcmp(*argv
, "xmit") == 0) {
1005 if (lwt_parse_bpf(rta
, len
, &argc
, &argv
, LWT_BPF_XMIT
,
1006 BPF_PROG_TYPE_LWT_XMIT
) < 0)
1008 } else if (strcmp(*argv
, "headroom") == 0) {
1009 unsigned int headroom
;
1012 if (get_unsigned(&headroom
, *argv
, 0) || headroom
== 0)
1013 invarg("headroom is invalid\n", *argv
);
1015 rta_addattr32(rta
, 1024, LWT_BPF_XMIT_HEADROOM
,
1018 } else if (strcmp(*argv
, "help") == 0) {
1026 /* argv is currently the first unparsed argument,
1027 * but the lwt_parse_encap() caller will move to the next,
1036 int lwt_parse_encap(struct rtattr
*rta
, size_t len
, int *argcp
, char ***argvp
)
1038 struct rtattr
*nest
;
1040 char **argv
= *argvp
;
1045 type
= read_encap_type(*argv
);
1047 invarg("\"encap type\" value is invalid\n", *argv
);
1052 "Error: unexpected end of line after \"encap\"\n");
1056 nest
= rta_nest(rta
, 1024, RTA_ENCAP
);
1058 case LWTUNNEL_ENCAP_MPLS
:
1059 ret
= parse_encap_mpls(rta
, len
, &argc
, &argv
);
1061 case LWTUNNEL_ENCAP_IP
:
1062 ret
= parse_encap_ip(rta
, len
, &argc
, &argv
);
1064 case LWTUNNEL_ENCAP_ILA
:
1065 ret
= parse_encap_ila(rta
, len
, &argc
, &argv
);
1067 case LWTUNNEL_ENCAP_IP6
:
1068 ret
= parse_encap_ip6(rta
, len
, &argc
, &argv
);
1070 case LWTUNNEL_ENCAP_BPF
:
1071 if (parse_encap_bpf(rta
, len
, &argc
, &argv
) < 0)
1074 case LWTUNNEL_ENCAP_SEG6
:
1075 ret
= parse_encap_seg6(rta
, len
, &argc
, &argv
);
1077 case LWTUNNEL_ENCAP_SEG6_LOCAL
:
1078 ret
= parse_encap_seg6local(rta
, len
, &argc
, &argv
);
1081 fprintf(stderr
, "Error: unsupported encap type\n");
1087 rta_nest_end(rta
, nest
);
1089 ret
= rta_addattr16(rta
, 1024, RTA_ENCAP_TYPE
, type
);