]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * link_vti6.c VTI driver module | |
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 | |
7 | * 2 of the License, or (at your option) any later version. | |
8 | * | |
9 | * Authors: Herbert Xu <herbert@gondor.apana.org.au> | |
10 | * Saurabh Mohan <saurabh.mohan@vyatta.com> Modified link_gre.c for VTI | |
11 | * Steffen Klassert <steffen.klassert@secunet.com> Modified link_vti.c for IPv6 | |
12 | */ | |
13 | ||
14 | #include <string.h> | |
15 | #include <net/if.h> | |
16 | #include <sys/types.h> | |
17 | #include <sys/socket.h> | |
18 | #include <arpa/inet.h> | |
19 | ||
20 | #include <linux/ip.h> | |
21 | #include <linux/if_tunnel.h> | |
22 | #include "rt_names.h" | |
23 | #include "utils.h" | |
24 | #include "ip_common.h" | |
25 | #include "tunnel.h" | |
26 | ||
27 | ||
28 | static void usage(void) __attribute__((noreturn)); | |
29 | static void usage(void) | |
30 | { | |
31 | fprintf(stderr, "Usage: ip link { add | set | change | replace | del } NAME\n"); | |
32 | fprintf(stderr, " type { vti6 } [ remote ADDR ] [ local ADDR ]\n"); | |
33 | fprintf(stderr, " [ [i|o]key KEY ]\n"); | |
34 | fprintf(stderr, " [ dev PHYS_DEV ]\n"); | |
35 | fprintf(stderr, "\n"); | |
36 | fprintf(stderr, "Where: NAME := STRING\n"); | |
37 | fprintf(stderr, " ADDR := { IPV6_ADDRESS }\n"); | |
38 | fprintf(stderr, " KEY := { DOTTED_QUAD | NUMBER }\n"); | |
39 | exit(-1); | |
40 | } | |
41 | ||
42 | static int vti6_parse_opt(struct link_util *lu, int argc, char **argv, | |
43 | struct nlmsghdr *n) | |
44 | { | |
45 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); | |
46 | struct { | |
47 | struct nlmsghdr n; | |
48 | struct ifinfomsg i; | |
49 | char buf[1024]; | |
50 | } req = { | |
51 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), | |
52 | .n.nlmsg_flags = NLM_F_REQUEST, | |
53 | .n.nlmsg_type = RTM_GETLINK, | |
54 | .i.ifi_family = preferred_family, | |
55 | .i.ifi_index = ifi->ifi_index, | |
56 | }; | |
57 | struct rtattr *tb[IFLA_MAX + 1]; | |
58 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; | |
59 | struct rtattr *vtiinfo[IFLA_VTI_MAX + 1]; | |
60 | struct in6_addr saddr; | |
61 | struct in6_addr daddr; | |
62 | unsigned int ikey = 0; | |
63 | unsigned int okey = 0; | |
64 | unsigned int link = 0; | |
65 | int len; | |
66 | ||
67 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { | |
68 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) { | |
69 | get_failed: | |
70 | fprintf(stderr, | |
71 | "Failed to get existing tunnel info.\n"); | |
72 | return -1; | |
73 | } | |
74 | ||
75 | len = req.n.nlmsg_len; | |
76 | len -= NLMSG_LENGTH(sizeof(*ifi)); | |
77 | if (len < 0) | |
78 | goto get_failed; | |
79 | ||
80 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); | |
81 | ||
82 | if (!tb[IFLA_LINKINFO]) | |
83 | goto get_failed; | |
84 | ||
85 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); | |
86 | ||
87 | if (!linkinfo[IFLA_INFO_DATA]) | |
88 | goto get_failed; | |
89 | ||
90 | parse_rtattr_nested(vtiinfo, IFLA_VTI_MAX, | |
91 | linkinfo[IFLA_INFO_DATA]); | |
92 | ||
93 | if (vtiinfo[IFLA_VTI_IKEY]) | |
94 | ikey = rta_getattr_u32(vtiinfo[IFLA_VTI_IKEY]); | |
95 | ||
96 | if (vtiinfo[IFLA_VTI_OKEY]) | |
97 | okey = rta_getattr_u32(vtiinfo[IFLA_VTI_OKEY]); | |
98 | ||
99 | if (vtiinfo[IFLA_VTI_LOCAL]) | |
100 | memcpy(&saddr, RTA_DATA(vtiinfo[IFLA_VTI_LOCAL]), sizeof(saddr)); | |
101 | ||
102 | if (vtiinfo[IFLA_VTI_REMOTE]) | |
103 | memcpy(&daddr, RTA_DATA(vtiinfo[IFLA_VTI_REMOTE]), sizeof(daddr)); | |
104 | ||
105 | if (vtiinfo[IFLA_VTI_LINK]) | |
106 | link = rta_getattr_u8(vtiinfo[IFLA_VTI_LINK]); | |
107 | } | |
108 | ||
109 | while (argc > 0) { | |
110 | if (!matches(*argv, "key")) { | |
111 | unsigned int uval; | |
112 | ||
113 | NEXT_ARG(); | |
114 | if (strchr(*argv, '.')) | |
115 | uval = get_addr32(*argv); | |
116 | else { | |
117 | if (get_unsigned(&uval, *argv, 0) < 0) { | |
118 | fprintf(stderr, | |
119 | "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv); | |
120 | exit(-1); | |
121 | } | |
122 | uval = htonl(uval); | |
123 | } | |
124 | ||
125 | ikey = okey = uval; | |
126 | } else if (!matches(*argv, "ikey")) { | |
127 | unsigned int uval; | |
128 | ||
129 | NEXT_ARG(); | |
130 | if (strchr(*argv, '.')) | |
131 | uval = get_addr32(*argv); | |
132 | else { | |
133 | if (get_unsigned(&uval, *argv, 0) < 0) { | |
134 | fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv); | |
135 | exit(-1); | |
136 | } | |
137 | uval = htonl(uval); | |
138 | } | |
139 | ikey = uval; | |
140 | } else if (!matches(*argv, "okey")) { | |
141 | unsigned int uval; | |
142 | ||
143 | NEXT_ARG(); | |
144 | if (strchr(*argv, '.')) | |
145 | uval = get_addr32(*argv); | |
146 | else { | |
147 | if (get_unsigned(&uval, *argv, 0) < 0) { | |
148 | fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv); | |
149 | exit(-1); | |
150 | } | |
151 | uval = htonl(uval); | |
152 | } | |
153 | okey = uval; | |
154 | } else if (!matches(*argv, "remote")) { | |
155 | NEXT_ARG(); | |
156 | if (!strcmp(*argv, "any")) { | |
157 | fprintf(stderr, "invalid value for \"remote\": \"%s\"\n", *argv); | |
158 | exit(-1); | |
159 | } else { | |
160 | inet_prefix addr; | |
161 | ||
162 | get_prefix(&addr, *argv, AF_INET6); | |
163 | memcpy(&daddr, addr.data, addr.bytelen); | |
164 | } | |
165 | } else if (!matches(*argv, "local")) { | |
166 | NEXT_ARG(); | |
167 | if (!strcmp(*argv, "any")) { | |
168 | fprintf(stderr, "invalid value for \"local\": \"%s\"\n", *argv); | |
169 | exit(-1); | |
170 | } else { | |
171 | inet_prefix addr; | |
172 | ||
173 | get_prefix(&addr, *argv, AF_INET6); | |
174 | memcpy(&saddr, addr.data, addr.bytelen); | |
175 | } | |
176 | } else if (!matches(*argv, "dev")) { | |
177 | NEXT_ARG(); | |
178 | link = if_nametoindex(*argv); | |
179 | if (link == 0) | |
180 | exit(-1); | |
181 | } else | |
182 | usage(); | |
183 | argc--; argv++; | |
184 | } | |
185 | ||
186 | addattr32(n, 1024, IFLA_VTI_IKEY, ikey); | |
187 | addattr32(n, 1024, IFLA_VTI_OKEY, okey); | |
188 | addattr_l(n, 1024, IFLA_VTI_LOCAL, &saddr, sizeof(saddr)); | |
189 | addattr_l(n, 1024, IFLA_VTI_REMOTE, &daddr, sizeof(daddr)); | |
190 | if (link) | |
191 | addattr32(n, 1024, IFLA_VTI_LINK, link); | |
192 | ||
193 | return 0; | |
194 | } | |
195 | ||
196 | static void vti6_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) | |
197 | { | |
198 | char s2[64]; | |
199 | const char *local = "any"; | |
200 | const char *remote = "any"; | |
201 | struct in6_addr saddr; | |
202 | struct in6_addr daddr; | |
203 | ||
204 | if (!tb) | |
205 | return; | |
206 | ||
207 | if (tb[IFLA_VTI_REMOTE]) { | |
208 | memcpy(&daddr, RTA_DATA(tb[IFLA_VTI_REMOTE]), sizeof(daddr)); | |
209 | ||
210 | remote = format_host(AF_INET6, 16, &daddr); | |
211 | } | |
212 | ||
213 | fprintf(f, "remote %s ", remote); | |
214 | ||
215 | if (tb[IFLA_VTI_LOCAL]) { | |
216 | memcpy(&saddr, RTA_DATA(tb[IFLA_VTI_LOCAL]), sizeof(saddr)); | |
217 | ||
218 | local = format_host(AF_INET6, 16, &saddr); | |
219 | } | |
220 | ||
221 | fprintf(f, "local %s ", local); | |
222 | ||
223 | if (tb[IFLA_VTI_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK])) { | |
224 | unsigned int link = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK]); | |
225 | const char *n = if_indextoname(link, s2); | |
226 | ||
227 | if (n) | |
228 | fprintf(f, "dev %s ", n); | |
229 | else | |
230 | fprintf(f, "dev %u ", link); | |
231 | } | |
232 | ||
233 | if (tb[IFLA_VTI_IKEY]) { | |
234 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_IKEY]), s2, sizeof(s2)); | |
235 | fprintf(f, "ikey %s ", s2); | |
236 | } | |
237 | ||
238 | if (tb[IFLA_VTI_OKEY]) { | |
239 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_OKEY]), s2, sizeof(s2)); | |
240 | fprintf(f, "okey %s ", s2); | |
241 | } | |
242 | } | |
243 | ||
244 | struct link_util vti6_link_util = { | |
245 | .id = "vti6", | |
246 | .maxattr = IFLA_VTI_MAX, | |
247 | .parse_opt = vti6_parse_opt, | |
248 | .print_opt = vti6_print_opt, | |
249 | }; |