]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (C)2006 USAGI/WIDE Project | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (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 | |
15 | * along with this program; if not, see <http://www.gnu.org/licenses>. | |
16 | */ | |
17 | /* | |
18 | * split from ip_tunnel.c | |
19 | */ | |
20 | /* | |
21 | * Author: | |
22 | * Masahide NAKAMURA @USAGI | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <string.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
29 | #include <sys/types.h> | |
30 | #include <sys/socket.h> | |
31 | #include <sys/ioctl.h> | |
32 | #include <netinet/in.h> | |
33 | #include <linux/if.h> | |
34 | #include <linux/ip.h> | |
35 | #include <linux/if_tunnel.h> | |
36 | #include <linux/if_arp.h> | |
37 | ||
38 | #include "utils.h" | |
39 | #include "tunnel.h" | |
40 | #include "json_print.h" | |
41 | ||
42 | const char *tnl_strproto(__u8 proto) | |
43 | { | |
44 | switch (proto) { | |
45 | case IPPROTO_IPIP: | |
46 | return "ip"; | |
47 | case IPPROTO_GRE: | |
48 | return "gre"; | |
49 | case IPPROTO_IPV6: | |
50 | return "ipv6"; | |
51 | case IPPROTO_ESP: | |
52 | return "esp"; | |
53 | case IPPROTO_MPLS: | |
54 | return "mpls"; | |
55 | case 0: | |
56 | return "any"; | |
57 | default: | |
58 | return "unknown"; | |
59 | } | |
60 | } | |
61 | ||
62 | int tnl_get_ioctl(const char *basedev, void *p) | |
63 | { | |
64 | struct ifreq ifr; | |
65 | int fd; | |
66 | int err; | |
67 | ||
68 | strlcpy(ifr.ifr_name, basedev, IFNAMSIZ); | |
69 | ifr.ifr_ifru.ifru_data = (void *)p; | |
70 | ||
71 | fd = socket(preferred_family, SOCK_DGRAM, 0); | |
72 | if (fd < 0) { | |
73 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
74 | return -1; | |
75 | } | |
76 | ||
77 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); | |
78 | if (err) | |
79 | fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev, | |
80 | strerror(errno)); | |
81 | ||
82 | close(fd); | |
83 | return err; | |
84 | } | |
85 | ||
86 | int tnl_add_ioctl(int cmd, const char *basedev, const char *name, void *p) | |
87 | { | |
88 | struct ifreq ifr; | |
89 | int fd; | |
90 | int err; | |
91 | ||
92 | if (cmd == SIOCCHGTUNNEL && name[0]) | |
93 | strlcpy(ifr.ifr_name, name, IFNAMSIZ); | |
94 | else | |
95 | strlcpy(ifr.ifr_name, basedev, IFNAMSIZ); | |
96 | ifr.ifr_ifru.ifru_data = p; | |
97 | ||
98 | fd = socket(preferred_family, SOCK_DGRAM, 0); | |
99 | if (fd < 0) { | |
100 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
101 | return -1; | |
102 | } | |
103 | ||
104 | err = ioctl(fd, cmd, &ifr); | |
105 | if (err) | |
106 | fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name, | |
107 | strerror(errno)); | |
108 | close(fd); | |
109 | return err; | |
110 | } | |
111 | ||
112 | int tnl_del_ioctl(const char *basedev, const char *name, void *p) | |
113 | { | |
114 | struct ifreq ifr; | |
115 | int fd; | |
116 | int err; | |
117 | ||
118 | if (name[0]) | |
119 | strlcpy(ifr.ifr_name, name, IFNAMSIZ); | |
120 | else | |
121 | strlcpy(ifr.ifr_name, basedev, IFNAMSIZ); | |
122 | ||
123 | ifr.ifr_ifru.ifru_data = p; | |
124 | ||
125 | fd = socket(preferred_family, SOCK_DGRAM, 0); | |
126 | if (fd < 0) { | |
127 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
128 | return -1; | |
129 | } | |
130 | ||
131 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); | |
132 | if (err) | |
133 | fprintf(stderr, "delete tunnel \"%s\" failed: %s\n", | |
134 | ifr.ifr_name, strerror(errno)); | |
135 | close(fd); | |
136 | return err; | |
137 | } | |
138 | ||
139 | static int tnl_gen_ioctl(int cmd, const char *name, | |
140 | void *p, int skiperr) | |
141 | { | |
142 | struct ifreq ifr; | |
143 | int fd; | |
144 | int err; | |
145 | ||
146 | strlcpy(ifr.ifr_name, name, IFNAMSIZ); | |
147 | ifr.ifr_ifru.ifru_data = p; | |
148 | ||
149 | fd = socket(preferred_family, SOCK_DGRAM, 0); | |
150 | if (fd < 0) { | |
151 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
152 | return -1; | |
153 | } | |
154 | ||
155 | err = ioctl(fd, cmd, &ifr); | |
156 | if (err && errno != skiperr) | |
157 | fprintf(stderr, "%s: ioctl %x failed: %s\n", name, | |
158 | cmd, strerror(errno)); | |
159 | close(fd); | |
160 | return err; | |
161 | } | |
162 | ||
163 | int tnl_prl_ioctl(int cmd, const char *name, void *p) | |
164 | { | |
165 | return tnl_gen_ioctl(cmd, name, p, -1); | |
166 | } | |
167 | ||
168 | int tnl_6rd_ioctl(int cmd, const char *name, void *p) | |
169 | { | |
170 | return tnl_gen_ioctl(cmd, name, p, -1); | |
171 | } | |
172 | ||
173 | int tnl_ioctl_get_6rd(const char *name, void *p) | |
174 | { | |
175 | return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL); | |
176 | } | |
177 | ||
178 | __be32 tnl_parse_key(const char *name, const char *key) | |
179 | { | |
180 | unsigned int uval; | |
181 | ||
182 | if (strchr(key, '.')) | |
183 | return get_addr32(key); | |
184 | ||
185 | if (get_unsigned(&uval, key, 0) < 0) { | |
186 | fprintf(stderr, | |
187 | "invalid value for \"%s\": \"%s\"; it should be an unsigned integer\n", | |
188 | name, key); | |
189 | exit(-1); | |
190 | } | |
191 | return htonl(uval); | |
192 | } | |
193 | ||
194 | static const char *tnl_encap_str(const char *name, int enabled, int port) | |
195 | { | |
196 | static const char ne[][sizeof("no")] = { | |
197 | [0] = "no", | |
198 | [1] = "", | |
199 | }; | |
200 | static char buf[32]; | |
201 | char b1[16]; | |
202 | const char *val; | |
203 | ||
204 | if (!port) { | |
205 | val = "auto "; | |
206 | } else if (port < 0) { | |
207 | val = ""; | |
208 | } else { | |
209 | snprintf(b1, sizeof(b1), "%u ", port - 1); | |
210 | val = b1; | |
211 | } | |
212 | ||
213 | snprintf(buf, sizeof(buf), "%sencap-%s %s", ne[!!enabled], name, val); | |
214 | return buf; | |
215 | } | |
216 | ||
217 | void tnl_print_encap(struct rtattr *tb[], | |
218 | int encap_type, int encap_flags, | |
219 | int encap_sport, int encap_dport) | |
220 | { | |
221 | __u16 type, flags, sport, dport; | |
222 | ||
223 | if (!tb[encap_type]) | |
224 | return; | |
225 | ||
226 | type = rta_getattr_u16(tb[encap_type]); | |
227 | if (type == TUNNEL_ENCAP_NONE) | |
228 | return; | |
229 | ||
230 | flags = rta_getattr_u16(tb[encap_flags]); | |
231 | sport = rta_getattr_u16(tb[encap_sport]); | |
232 | dport = rta_getattr_u16(tb[encap_dport]); | |
233 | ||
234 | open_json_object("encap"); | |
235 | print_string(PRINT_FP, NULL, "encap ", NULL); | |
236 | ||
237 | switch (type) { | |
238 | case TUNNEL_ENCAP_FOU: | |
239 | print_string(PRINT_ANY, "type", "%s ", "fou"); | |
240 | break; | |
241 | case TUNNEL_ENCAP_GUE: | |
242 | print_string(PRINT_ANY, "type", "%s ", "gue"); | |
243 | break; | |
244 | default: | |
245 | print_null(PRINT_ANY, "type", "%s ", "unknown"); | |
246 | break; | |
247 | } | |
248 | ||
249 | if (is_json_context()) { | |
250 | print_uint(PRINT_JSON, "sport", NULL, ntohs(sport)); | |
251 | print_uint(PRINT_JSON, "dport", NULL, ntohs(dport)); | |
252 | print_bool(PRINT_JSON, "csum", NULL, | |
253 | flags & TUNNEL_ENCAP_FLAG_CSUM); | |
254 | print_bool(PRINT_JSON, "csum6", NULL, | |
255 | flags & TUNNEL_ENCAP_FLAG_CSUM6); | |
256 | print_bool(PRINT_JSON, "remcsum", NULL, | |
257 | flags & TUNNEL_ENCAP_FLAG_REMCSUM); | |
258 | close_json_object(); | |
259 | } else { | |
260 | int t; | |
261 | ||
262 | t = sport ? ntohs(sport) + 1 : 0; | |
263 | print_string(PRINT_FP, NULL, "%s", | |
264 | tnl_encap_str("sport", 1, t)); | |
265 | ||
266 | t = ntohs(dport) + 1; | |
267 | print_string(PRINT_FP, NULL, "%s", | |
268 | tnl_encap_str("dport", 1, t)); | |
269 | ||
270 | t = flags & TUNNEL_ENCAP_FLAG_CSUM; | |
271 | print_string(PRINT_FP, NULL, "%s", | |
272 | tnl_encap_str("csum", t, -1)); | |
273 | ||
274 | t = flags & TUNNEL_ENCAP_FLAG_CSUM6; | |
275 | print_string(PRINT_FP, NULL, "%s", | |
276 | tnl_encap_str("csum6", t, -1)); | |
277 | ||
278 | t = flags & TUNNEL_ENCAP_FLAG_REMCSUM; | |
279 | print_string(PRINT_FP, NULL, "%s", | |
280 | tnl_encap_str("remcsum", t, -1)); | |
281 | } | |
282 | } | |
283 | ||
284 | void tnl_print_endpoint(const char *name, const struct rtattr *rta, int family) | |
285 | { | |
286 | const char *value; | |
287 | inet_prefix dst; | |
288 | ||
289 | if (!rta) { | |
290 | value = "any"; | |
291 | } else if (get_addr_rta(&dst, rta, family)) { | |
292 | value = "unknown"; | |
293 | } else if (dst.flags & ADDRTYPE_UNSPEC) { | |
294 | value = "any"; | |
295 | } else { | |
296 | value = format_host(family, dst.bytelen, dst.data); | |
297 | if (!value) | |
298 | value = "unknown"; | |
299 | } | |
300 | ||
301 | if (is_json_context()) { | |
302 | print_string(PRINT_JSON, name, NULL, value); | |
303 | } else { | |
304 | SPRINT_BUF(b1); | |
305 | ||
306 | snprintf(b1, sizeof(b1), "%s %%s ", name); | |
307 | print_string(PRINT_FP, NULL, b1, value); | |
308 | } | |
309 | } | |
310 | ||
311 | static void tnl_print_stats(const struct rtnl_link_stats64 *s) | |
312 | { | |
313 | printf("%s", _SL_); | |
314 | printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); | |
315 | printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-8lld%s", | |
316 | s->rx_packets, s->rx_bytes, s->rx_errors, s->rx_frame_errors, | |
317 | s->rx_fifo_errors, s->multicast, _SL_); | |
318 | printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); | |
319 | printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-6lld", | |
320 | s->tx_packets, s->tx_bytes, s->tx_errors, s->collisions, | |
321 | s->tx_carrier_errors, s->tx_dropped); | |
322 | } | |
323 | ||
324 | static int print_nlmsg_tunnel(struct nlmsghdr *n, void *arg) | |
325 | { | |
326 | struct tnl_print_nlmsg_info *info = arg; | |
327 | struct ifinfomsg *ifi = NLMSG_DATA(n); | |
328 | struct rtattr *tb[IFLA_MAX+1]; | |
329 | const char *name, *n1; | |
330 | ||
331 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) | |
332 | return 0; | |
333 | ||
334 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi))) | |
335 | return -1; | |
336 | ||
337 | if (preferred_family == AF_INET) { | |
338 | switch (ifi->ifi_type) { | |
339 | case ARPHRD_TUNNEL: | |
340 | case ARPHRD_IPGRE: | |
341 | case ARPHRD_SIT: | |
342 | break; | |
343 | default: | |
344 | return 0; | |
345 | } | |
346 | } else { | |
347 | switch (ifi->ifi_type) { | |
348 | case ARPHRD_TUNNEL6: | |
349 | case ARPHRD_IP6GRE: | |
350 | break; | |
351 | default: | |
352 | return 0; | |
353 | } | |
354 | } | |
355 | ||
356 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); | |
357 | ||
358 | if (!tb[IFLA_IFNAME]) | |
359 | return 0; | |
360 | ||
361 | name = rta_getattr_str(tb[IFLA_IFNAME]); | |
362 | ||
363 | /* Assume p1->name[IFNAMSIZ] is first field of structure */ | |
364 | n1 = info->p1; | |
365 | if (n1[0] && strcmp(n1, name)) | |
366 | return 0; | |
367 | ||
368 | info->ifi = ifi; | |
369 | info->init(info); | |
370 | ||
371 | /* TODO: parse netlink attributes */ | |
372 | if (tnl_get_ioctl(name, info->p2)) | |
373 | return 0; | |
374 | ||
375 | if (!info->match(info)) | |
376 | return 0; | |
377 | ||
378 | info->print(info->p2); | |
379 | if (show_stats) { | |
380 | struct rtnl_link_stats64 s; | |
381 | ||
382 | if (get_rtnl_link_stats_rta(&s, tb) <= 0) | |
383 | return -1; | |
384 | ||
385 | tnl_print_stats(&s); | |
386 | } | |
387 | fputc('\n', stdout); | |
388 | ||
389 | return 0; | |
390 | } | |
391 | ||
392 | int do_tunnels_list(struct tnl_print_nlmsg_info *info) | |
393 | { | |
394 | if (rtnl_linkdump_req(&rth, preferred_family) < 0) { | |
395 | perror("Cannot send dump request\n"); | |
396 | return -1; | |
397 | } | |
398 | ||
399 | if (rtnl_dump_filter(&rth, print_nlmsg_tunnel, info) < 0) { | |
400 | fprintf(stderr, "Dump terminated\n"); | |
401 | return -1; | |
402 | } | |
403 | ||
404 | return 0; | |
405 | } |