]>
Commit | Line | Data |
---|---|---|
d9bd1bd9 MN |
1 | /* |
2 | * Copyright (C)2006 USAGI/WIDE Project | |
ae665a52 | 3 | * |
d9bd1bd9 MN |
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. | |
ae665a52 | 8 | * |
d9bd1bd9 MN |
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. | |
ae665a52 | 13 | * |
d9bd1bd9 | 14 | * You should have received a copy of the GNU General Public License |
4d98ab00 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
d9bd1bd9 MN |
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> | |
3979ef91 | 28 | #include <errno.h> |
d9bd1bd9 MN |
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> | |
3e953938 | 36 | #include <linux/if_arp.h> |
d9bd1bd9 MN |
37 | |
38 | #include "utils.h" | |
39 | #include "tunnel.h" | |
bad76e6b | 40 | #include "json_print.h" |
d9bd1bd9 MN |
41 | |
42 | const char *tnl_strproto(__u8 proto) | |
43 | { | |
d9bd1bd9 MN |
44 | switch (proto) { |
45 | case IPPROTO_IPIP: | |
c9391f12 | 46 | return "ip"; |
d9bd1bd9 | 47 | case IPPROTO_GRE: |
c9391f12 | 48 | return "gre"; |
d9bd1bd9 | 49 | case IPPROTO_IPV6: |
c9391f12 | 50 | return "ipv6"; |
14e97673 | 51 | case IPPROTO_ESP: |
c9391f12 | 52 | return "esp"; |
f005b700 | 53 | case IPPROTO_MPLS: |
c9391f12 | 54 | return "mpls"; |
0b959b0f | 55 | case 0: |
c9391f12 | 56 | return "any"; |
d9bd1bd9 | 57 | default: |
c9391f12 | 58 | return "unknown"; |
d9bd1bd9 | 59 | } |
d9bd1bd9 MN |
60 | } |
61 | ||
d9bd1bd9 MN |
62 | int tnl_get_ioctl(const char *basedev, void *p) |
63 | { | |
64 | struct ifreq ifr; | |
65 | int fd; | |
66 | int err; | |
67 | ||
fcb18aa3 | 68 | strlcpy(ifr.ifr_name, basedev, IFNAMSIZ); |
56f5daac | 69 | ifr.ifr_ifru.ifru_data = (void *)p; |
cb89c7c7 | 70 | |
d9bd1bd9 | 71 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
72 | if (fd < 0) { |
73 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
74 | return -1; | |
75 | } | |
76 | ||
d9bd1bd9 MN |
77 | err = ioctl(fd, SIOCGETTUNNEL, &ifr); |
78 | if (err) | |
14645ec2 | 79 | fprintf(stderr, "get tunnel \"%s\" failed: %s\n", basedev, |
ea71beac SH |
80 | strerror(errno)); |
81 | ||
d9bd1bd9 MN |
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]) | |
fcb18aa3 | 93 | strlcpy(ifr.ifr_name, name, IFNAMSIZ); |
d9bd1bd9 | 94 | else |
fcb18aa3 | 95 | strlcpy(ifr.ifr_name, basedev, IFNAMSIZ); |
d9bd1bd9 | 96 | ifr.ifr_ifru.ifru_data = p; |
cb89c7c7 | 97 | |
d9bd1bd9 | 98 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
99 | if (fd < 0) { |
100 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
101 | return -1; | |
102 | } | |
103 | ||
d9bd1bd9 MN |
104 | err = ioctl(fd, cmd, &ifr); |
105 | if (err) | |
14645ec2 | 106 | fprintf(stderr, "add tunnel \"%s\" failed: %s\n", ifr.ifr_name, |
ea71beac | 107 | strerror(errno)); |
d9bd1bd9 MN |
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]) | |
fcb18aa3 | 119 | strlcpy(ifr.ifr_name, name, IFNAMSIZ); |
d9bd1bd9 | 120 | else |
fcb18aa3 | 121 | strlcpy(ifr.ifr_name, basedev, IFNAMSIZ); |
ea71beac | 122 | |
d9bd1bd9 | 123 | ifr.ifr_ifru.ifru_data = p; |
cb89c7c7 | 124 | |
d9bd1bd9 | 125 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
126 | if (fd < 0) { |
127 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
128 | return -1; | |
129 | } | |
130 | ||
d9bd1bd9 MN |
131 | err = ioctl(fd, SIOCDELTUNNEL, &ifr); |
132 | if (err) | |
14645ec2 | 133 | fprintf(stderr, "delete tunnel \"%s\" failed: %s\n", |
ea71beac | 134 | ifr.ifr_name, strerror(errno)); |
d9bd1bd9 MN |
135 | close(fd); |
136 | return err; | |
137 | } | |
a07e9912 | 138 | |
0612519e | 139 | static int tnl_gen_ioctl(int cmd, const char *name, |
ea71beac | 140 | void *p, int skiperr) |
a07e9912 SH |
141 | { |
142 | struct ifreq ifr; | |
143 | int fd; | |
144 | int err; | |
145 | ||
fcb18aa3 | 146 | strlcpy(ifr.ifr_name, name, IFNAMSIZ); |
a07e9912 | 147 | ifr.ifr_ifru.ifru_data = p; |
cb89c7c7 | 148 | |
a07e9912 | 149 | fd = socket(preferred_family, SOCK_DGRAM, 0); |
cb89c7c7 ZS |
150 | if (fd < 0) { |
151 | fprintf(stderr, "create socket failed: %s\n", strerror(errno)); | |
152 | return -1; | |
153 | } | |
154 | ||
a07e9912 | 155 | err = ioctl(fd, cmd, &ifr); |
3979ef91 | 156 | if (err && errno != skiperr) |
ea71beac SH |
157 | fprintf(stderr, "%s: ioctl %x failed: %s\n", name, |
158 | cmd, strerror(errno)); | |
a07e9912 SH |
159 | close(fd); |
160 | return err; | |
161 | } | |
b88215c4 AC |
162 | |
163 | int tnl_prl_ioctl(int cmd, const char *name, void *p) | |
164 | { | |
3979ef91 | 165 | return tnl_gen_ioctl(cmd, name, p, -1); |
b88215c4 AC |
166 | } |
167 | ||
168 | int tnl_6rd_ioctl(int cmd, const char *name, void *p) | |
169 | { | |
3979ef91 | 170 | return tnl_gen_ioctl(cmd, name, p, -1); |
b88215c4 AC |
171 | } |
172 | ||
173 | int tnl_ioctl_get_6rd(const char *name, void *p) | |
174 | { | |
3979ef91 | 175 | return tnl_gen_ioctl(SIOCGET6RD, name, p, EINVAL); |
b88215c4 | 176 | } |
a7ed1520 PS |
177 | |
178 | __be32 tnl_parse_key(const char *name, const char *key) | |
179 | { | |
56f5daac | 180 | unsigned int uval; |
a7ed1520 PS |
181 | |
182 | if (strchr(key, '.')) | |
183 | return get_addr32(key); | |
184 | ||
185 | if (get_unsigned(&uval, key, 0) < 0) { | |
1f44b937 SP |
186 | fprintf(stderr, |
187 | "invalid value for \"%s\": \"%s\"; it should be an unsigned integer\n", | |
188 | name, key); | |
a7ed1520 PS |
189 | exit(-1); |
190 | } | |
191 | return htonl(uval); | |
192 | } | |
7d6aadcd | 193 | |
bad76e6b SP |
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) { | |
b53835de | 205 | val = "auto "; |
bad76e6b SP |
206 | } else if (port < 0) { |
207 | val = ""; | |
208 | } else { | |
b53835de | 209 | snprintf(b1, sizeof(b1), "%u ", port - 1); |
bad76e6b SP |
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 | ||
b761fc41 SP |
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 | ||
ee713339 AC |
311 | void tnl_print_gre_flags(__u8 proto, |
312 | __be16 i_flags, __be16 o_flags, | |
313 | __be32 i_key, __be32 o_key) | |
314 | { | |
315 | if ((i_flags & GRE_KEY) && (o_flags & GRE_KEY) && | |
316 | o_key == i_key) { | |
a8360dd3 | 317 | print_uint(PRINT_ANY, "key", " key %u", ntohl(i_key)); |
ee713339 AC |
318 | } else { |
319 | if (i_flags & GRE_KEY) | |
a8360dd3 | 320 | print_uint(PRINT_ANY, "ikey", " ikey %u", ntohl(i_key)); |
ee713339 | 321 | if (o_flags & GRE_KEY) |
a8360dd3 | 322 | print_uint(PRINT_ANY, "okey", " okey %u", ntohl(o_key)); |
ee713339 AC |
323 | } |
324 | ||
a8360dd3 AC |
325 | if (proto != IPPROTO_GRE) |
326 | return; | |
327 | ||
328 | open_json_array(PRINT_JSON, "flags"); | |
329 | if (i_flags & GRE_SEQ) { | |
330 | if (is_json_context()) | |
331 | print_string(PRINT_JSON, NULL, "%s", "rx_drop_ooseq"); | |
332 | else | |
ee713339 | 333 | printf("%s Drop packets out of sequence.", _SL_); |
a8360dd3 AC |
334 | } |
335 | if (i_flags & GRE_CSUM) { | |
336 | if (is_json_context()) | |
337 | print_string(PRINT_JSON, NULL, "%s", "rx_csum"); | |
338 | else | |
ee713339 | 339 | printf("%s Checksum in received packet is required.", _SL_); |
a8360dd3 AC |
340 | } |
341 | if (o_flags & GRE_SEQ) { | |
342 | if (is_json_context()) | |
343 | print_string(PRINT_JSON, NULL, "%s", "tx_seq"); | |
344 | else | |
ee713339 | 345 | printf("%s Sequence packets on output.", _SL_); |
a8360dd3 AC |
346 | } |
347 | if (o_flags & GRE_CSUM) { | |
348 | if (is_json_context()) | |
349 | print_string(PRINT_JSON, NULL, "%s", "tx_csum"); | |
350 | else | |
ee713339 AC |
351 | printf("%s Checksum output packets.", _SL_); |
352 | } | |
a8360dd3 | 353 | close_json_array(PRINT_JSON, NULL); |
ee713339 AC |
354 | } |
355 | ||
3e953938 | 356 | static void tnl_print_stats(const struct rtnl_link_stats64 *s) |
affb3617 | 357 | { |
7d6aadcd PS |
358 | printf("%s", _SL_); |
359 | printf("RX: Packets Bytes Errors CsumErrs OutOfSeq Mcasts%s", _SL_); | |
affb3617 SP |
360 | printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-8lld%s", |
361 | s->rx_packets, s->rx_bytes, s->rx_errors, s->rx_frame_errors, | |
362 | s->rx_fifo_errors, s->multicast, _SL_); | |
7d6aadcd | 363 | printf("TX: Packets Bytes Errors DeadLoop NoRoute NoBufs%s", _SL_); |
affb3617 SP |
364 | printf(" %-10lld %-12lld %-6lld %-8lld %-8lld %-6lld", |
365 | s->tx_packets, s->tx_bytes, s->tx_errors, s->collisions, | |
366 | s->tx_carrier_errors, s->tx_dropped); | |
7d6aadcd | 367 | } |
3e953938 | 368 | |
cd554f2c | 369 | static int print_nlmsg_tunnel(struct nlmsghdr *n, void *arg) |
3e953938 SP |
370 | { |
371 | struct tnl_print_nlmsg_info *info = arg; | |
372 | struct ifinfomsg *ifi = NLMSG_DATA(n); | |
373 | struct rtattr *tb[IFLA_MAX+1]; | |
374 | const char *name, *n1; | |
375 | ||
376 | if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK) | |
377 | return 0; | |
378 | ||
379 | if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi))) | |
380 | return -1; | |
381 | ||
382 | if (preferred_family == AF_INET) { | |
383 | switch (ifi->ifi_type) { | |
384 | case ARPHRD_TUNNEL: | |
385 | case ARPHRD_IPGRE: | |
386 | case ARPHRD_SIT: | |
387 | break; | |
388 | default: | |
389 | return 0; | |
390 | } | |
391 | } else { | |
392 | switch (ifi->ifi_type) { | |
393 | case ARPHRD_TUNNEL6: | |
394 | case ARPHRD_IP6GRE: | |
395 | break; | |
396 | default: | |
397 | return 0; | |
398 | } | |
399 | } | |
400 | ||
401 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n)); | |
402 | ||
403 | if (!tb[IFLA_IFNAME]) | |
404 | return 0; | |
405 | ||
406 | name = rta_getattr_str(tb[IFLA_IFNAME]); | |
407 | ||
408 | /* Assume p1->name[IFNAMSIZ] is first field of structure */ | |
409 | n1 = info->p1; | |
410 | if (n1[0] && strcmp(n1, name)) | |
411 | return 0; | |
412 | ||
413 | info->ifi = ifi; | |
414 | info->init(info); | |
415 | ||
416 | /* TODO: parse netlink attributes */ | |
417 | if (tnl_get_ioctl(name, info->p2)) | |
418 | return 0; | |
419 | ||
420 | if (!info->match(info)) | |
421 | return 0; | |
422 | ||
423 | info->print(info->p2); | |
424 | if (show_stats) { | |
425 | struct rtnl_link_stats64 s; | |
426 | ||
427 | if (get_rtnl_link_stats_rta(&s, tb) <= 0) | |
428 | return -1; | |
429 | ||
430 | tnl_print_stats(&s); | |
431 | } | |
432 | fputc('\n', stdout); | |
433 | ||
434 | return 0; | |
435 | } | |
436 | ||
437 | int do_tunnels_list(struct tnl_print_nlmsg_info *info) | |
438 | { | |
a8360dd3 | 439 | new_json_obj(json); |
31ae2912 | 440 | if (rtnl_linkdump_req(&rth, preferred_family) < 0) { |
3e953938 SP |
441 | perror("Cannot send dump request\n"); |
442 | return -1; | |
443 | } | |
444 | ||
445 | if (rtnl_dump_filter(&rth, print_nlmsg_tunnel, info) < 0) { | |
446 | fprintf(stderr, "Dump terminated\n"); | |
447 | return -1; | |
448 | } | |
a8360dd3 | 449 | delete_json_obj(); |
3e953938 SP |
450 | |
451 | return 0; | |
452 | } |