]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/iproute_lwtunnel.c
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/lwtunnel.h>
20 #include <linux/mpls_iptunnel.h>
25 #include "iproute_lwtunnel.h"
27 static int read_encap_type(const char *name
)
29 if (strcmp(name
, "mpls") == 0)
30 return LWTUNNEL_ENCAP_MPLS
;
31 else if (strcmp(name
, "ip") == 0)
32 return LWTUNNEL_ENCAP_IP
;
33 else if (strcmp(name
, "ip6") == 0)
34 return LWTUNNEL_ENCAP_IP6
;
36 return LWTUNNEL_ENCAP_NONE
;
39 static const char *format_encap_type(int type
)
42 case LWTUNNEL_ENCAP_MPLS
:
44 case LWTUNNEL_ENCAP_IP
:
46 case LWTUNNEL_ENCAP_IP6
:
53 static void print_encap_mpls(FILE *fp
, struct rtattr
*encap
)
55 struct rtattr
*tb
[MPLS_IPTUNNEL_MAX
+1];
58 parse_rtattr_nested(tb
, MPLS_IPTUNNEL_MAX
, encap
);
60 if (tb
[MPLS_IPTUNNEL_DST
])
61 fprintf(fp
, " %s ", format_host(AF_MPLS
,
62 RTA_PAYLOAD(tb
[MPLS_IPTUNNEL_DST
]),
63 RTA_DATA(tb
[MPLS_IPTUNNEL_DST
]),
67 static void print_encap_ip(FILE *fp
, struct rtattr
*encap
)
69 struct rtattr
*tb
[LWTUNNEL_IP_MAX
+1];
72 parse_rtattr_nested(tb
, LWTUNNEL_IP_MAX
, encap
);
74 if (tb
[LWTUNNEL_IP_ID
])
75 fprintf(fp
, "id %llu ", ntohll(rta_getattr_u64(tb
[LWTUNNEL_IP_ID
])));
77 if (tb
[LWTUNNEL_IP_SRC
])
78 fprintf(fp
, "src %s ",
80 RTA_PAYLOAD(tb
[LWTUNNEL_IP_SRC
]),
81 RTA_DATA(tb
[LWTUNNEL_IP_SRC
]),
84 if (tb
[LWTUNNEL_IP_DST
])
85 fprintf(fp
, "dst %s ",
87 RTA_PAYLOAD(tb
[LWTUNNEL_IP_DST
]),
88 RTA_DATA(tb
[LWTUNNEL_IP_DST
]),
91 if (tb
[LWTUNNEL_IP_TTL
])
92 fprintf(fp
, "ttl %d ", rta_getattr_u8(tb
[LWTUNNEL_IP_TTL
]));
94 if (tb
[LWTUNNEL_IP_TOS
])
95 fprintf(fp
, "tos %d ", rta_getattr_u8(tb
[LWTUNNEL_IP_TOS
]));
98 void lwt_print_encap(FILE *fp
, struct rtattr
*encap_type
,
106 et
= rta_getattr_u16(encap_type
);
108 fprintf(fp
, " encap %s", format_encap_type(et
));
111 case LWTUNNEL_ENCAP_MPLS
:
112 print_encap_mpls(fp
, encap
);
114 case LWTUNNEL_ENCAP_IP
:
115 print_encap_ip(fp
, encap
);
120 static int parse_encap_mpls(struct rtattr
*rta
, size_t len
, int *argcp
, char ***argvp
)
124 char **argv
= *argvp
;
126 if (get_addr(&addr
, *argv
, AF_MPLS
)) {
127 fprintf(stderr
, "Error: an inet address is expected rather than \"%s\".\n", *argv
);
131 rta_addattr_l(rta
, len
, MPLS_IPTUNNEL_DST
, &addr
.data
,
140 static int parse_encap_ip(struct rtattr
*rta
, size_t len
, int *argcp
, char ***argvp
)
142 int id_ok
= 0, dst_ok
= 0, tos_ok
= 0, ttl_ok
= 0;
143 char **argv
= *argvp
;
147 if (strcmp(*argv
, "id") == 0) {
151 duparg2("id", *argv
);
152 if (get_u64(&id
, *argv
, 0))
153 invarg("\"id\" value is invalid\n", *argv
);
154 rta_addattr64(rta
, len
, LWTUNNEL_IP_ID
, htonll(id
));
155 } else if (strcmp(*argv
, "dst") == 0) {
159 duparg2("dst", *argv
);
160 get_addr(&addr
, *argv
, AF_INET
);
161 rta_addattr_l(rta
, len
, LWTUNNEL_IP_DST
, &addr
.data
, addr
.bytelen
);
162 } else if (strcmp(*argv
, "tos") == 0) {
166 duparg2("tos", *argv
);
167 if (rtnl_dsfield_a2n(&tos
, *argv
))
168 invarg("\"tos\" value is invalid\n", *argv
);
169 rta_addattr8(rta
, len
, LWTUNNEL_IP_TOS
, tos
);
170 } else if (strcmp(*argv
, "ttl") == 0) {
174 duparg2("ttl", *argv
);
175 if (get_u8(&ttl
, *argv
, 0))
176 invarg("\"ttl\" value is invalid\n", *argv
);
177 rta_addattr8(rta
, len
, LWTUNNEL_IP_TTL
, ttl
);
190 int lwt_parse_encap(struct rtattr
*rta
, size_t len
, int *argcp
, char ***argvp
)
194 char **argv
= *argvp
;
198 type
= read_encap_type(*argv
);
200 invarg("\"encap type\" value is invalid\n", *argv
);
204 fprintf(stderr
, "Error: unexpected end of line after \"encap\"\n");
208 nest
= rta_nest(rta
, 1024, RTA_ENCAP
);
210 case LWTUNNEL_ENCAP_MPLS
:
211 parse_encap_mpls(rta
, len
, &argc
, &argv
);
213 case LWTUNNEL_ENCAP_IP
:
214 parse_encap_ip(rta
, len
, &argc
, &argv
);
217 fprintf(stderr
, "Error: unsupported encap type\n");
220 rta_nest_end(rta
, nest
);
222 rta_addattr16(rta
, 1024, RTA_ENCAP_TYPE
, type
);