]> git.proxmox.com Git - mirror_iproute2.git/blob - ip/iptuntap.c
lib: print_color_rate(): Fix formatting small rates in IEC mode
[mirror_iproute2.git] / ip / iptuntap.c
1 /*
2 * iptunnel.c "ip tuntap"
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: David Woodhouse <David.Woodhouse@intel.com>
10 *
11 */
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <unistd.h>
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <arpa/inet.h>
20 #include <sys/ioctl.h>
21 #include <linux/if.h>
22 #include <linux/if_tun.h>
23 #include <linux/if_arp.h>
24 #include <pwd.h>
25 #include <grp.h>
26 #include <fcntl.h>
27 #include <dirent.h>
28 #include <errno.h>
29 #include <glob.h>
30
31 #include "rt_names.h"
32 #include "utils.h"
33 #include "ip_common.h"
34
35 static const char drv_name[] = "tun";
36
37 #define TUNDEV "/dev/net/tun"
38
39 static void usage(void) __attribute__((noreturn));
40
41 static void usage(void)
42 {
43 fprintf(stderr,
44 "Usage: ip tuntap { add | del | show | list | lst | help } [ dev PHYS_DEV ]\n"
45 " [ mode { tun | tap } ] [ user USER ] [ group GROUP ]\n"
46 " [ one_queue ] [ pi ] [ vnet_hdr ] [ multi_queue ] [ name NAME ]\n"
47 "\n"
48 "Where: USER := { STRING | NUMBER }\n"
49 " GROUP := { STRING | NUMBER }\n");
50 exit(-1);
51 }
52
53 static int tap_add_ioctl(struct ifreq *ifr, uid_t uid, gid_t gid)
54 {
55 int fd;
56 int ret = -1;
57
58 #ifdef IFF_TUN_EXCL
59 ifr->ifr_flags |= IFF_TUN_EXCL;
60 #endif
61
62 fd = open(TUNDEV, O_RDWR);
63 if (fd < 0) {
64 perror("open");
65 return -1;
66 }
67 if (ioctl(fd, TUNSETIFF, ifr)) {
68 perror("ioctl(TUNSETIFF)");
69 goto out;
70 }
71 if (uid != -1 && ioctl(fd, TUNSETOWNER, uid)) {
72 perror("ioctl(TUNSETOWNER)");
73 goto out;
74 }
75 if (gid != -1 && ioctl(fd, TUNSETGROUP, gid)) {
76 perror("ioctl(TUNSETGROUP)");
77 goto out;
78 }
79 if (ioctl(fd, TUNSETPERSIST, 1)) {
80 perror("ioctl(TUNSETPERSIST)");
81 goto out;
82 }
83 ret = 0;
84 out:
85 close(fd);
86 return ret;
87 }
88
89 static int tap_del_ioctl(struct ifreq *ifr)
90 {
91 int fd = open(TUNDEV, O_RDWR);
92 int ret = -1;
93
94 if (fd < 0) {
95 perror("open");
96 return -1;
97 }
98 if (ioctl(fd, TUNSETIFF, ifr)) {
99 perror("ioctl(TUNSETIFF)");
100 goto out;
101 }
102 if (ioctl(fd, TUNSETPERSIST, 0)) {
103 perror("ioctl(TUNSETPERSIST)");
104 goto out;
105 }
106 ret = 0;
107 out:
108 close(fd);
109 return ret;
110
111 }
112 static int parse_args(int argc, char **argv,
113 struct ifreq *ifr, uid_t *uid, gid_t *gid)
114 {
115 int count = 0;
116
117 memset(ifr, 0, sizeof(*ifr));
118
119 ifr->ifr_flags |= IFF_NO_PI;
120
121 while (argc > 0) {
122 if (matches(*argv, "mode") == 0) {
123 NEXT_ARG();
124 if (matches(*argv, "tun") == 0) {
125 if (ifr->ifr_flags & IFF_TAP) {
126 fprintf(stderr, "You managed to ask for more than one tunnel mode.\n");
127 exit(-1);
128 }
129 ifr->ifr_flags |= IFF_TUN;
130 } else if (matches(*argv, "tap") == 0) {
131 if (ifr->ifr_flags & IFF_TUN) {
132 fprintf(stderr, "You managed to ask for more than one tunnel mode.\n");
133 exit(-1);
134 }
135 ifr->ifr_flags |= IFF_TAP;
136 } else {
137 fprintf(stderr, "Unknown tunnel mode \"%s\"\n", *argv);
138 exit(-1);
139 }
140 } else if (uid && matches(*argv, "user") == 0) {
141 char *end;
142 unsigned long user;
143
144 NEXT_ARG();
145 if (**argv && ((user = strtol(*argv, &end, 10)), !*end))
146 *uid = user;
147 else {
148 struct passwd *pw = getpwnam(*argv);
149
150 if (!pw) {
151 fprintf(stderr, "invalid user \"%s\"\n", *argv);
152 exit(-1);
153 }
154 *uid = pw->pw_uid;
155 }
156 } else if (gid && matches(*argv, "group") == 0) {
157 char *end;
158 unsigned long group;
159
160 NEXT_ARG();
161
162 if (**argv && ((group = strtol(*argv, &end, 10)), !*end))
163 *gid = group;
164 else {
165 struct group *gr = getgrnam(*argv);
166
167 if (!gr) {
168 fprintf(stderr, "invalid group \"%s\"\n", *argv);
169 exit(-1);
170 }
171 *gid = gr->gr_gid;
172 }
173 } else if (matches(*argv, "pi") == 0) {
174 ifr->ifr_flags &= ~IFF_NO_PI;
175 } else if (matches(*argv, "one_queue") == 0) {
176 ifr->ifr_flags |= IFF_ONE_QUEUE;
177 } else if (matches(*argv, "vnet_hdr") == 0) {
178 ifr->ifr_flags |= IFF_VNET_HDR;
179 } else if (matches(*argv, "multi_queue") == 0) {
180 ifr->ifr_flags |= IFF_MULTI_QUEUE;
181 } else if (matches(*argv, "dev") == 0) {
182 NEXT_ARG();
183 if (get_ifname(ifr->ifr_name, *argv))
184 invarg("\"dev\" not a valid ifname", *argv);
185 } else {
186 if (matches(*argv, "name") == 0) {
187 NEXT_ARG();
188 } else if (matches(*argv, "help") == 0)
189 usage();
190 if (ifr->ifr_name[0])
191 duparg2("name", *argv);
192 if (get_ifname(ifr->ifr_name, *argv))
193 invarg("\"name\" not a valid ifname", *argv);
194 }
195 count++;
196 argc--; argv++;
197 }
198
199 if (!(ifr->ifr_flags & TUN_TYPE_MASK)) {
200 fprintf(stderr, "You failed to specify a tunnel mode\n");
201 return -1;
202 }
203
204 return 0;
205 }
206
207
208 static int do_add(int argc, char **argv)
209 {
210 struct ifreq ifr;
211 uid_t uid = -1;
212 gid_t gid = -1;
213
214 if (parse_args(argc, argv, &ifr, &uid, &gid) < 0)
215 return -1;
216
217 return tap_add_ioctl(&ifr, uid, gid);
218 }
219
220 static int do_del(int argc, char **argv)
221 {
222 struct ifreq ifr;
223
224 if (parse_args(argc, argv, &ifr, NULL, NULL) < 0)
225 return -1;
226
227 return tap_del_ioctl(&ifr);
228 }
229
230 static void print_flags(long flags)
231 {
232 open_json_array(PRINT_JSON, "flags");
233
234 if (flags & IFF_TUN)
235 print_string(PRINT_ANY, NULL, " %s", "tun");
236
237 if (flags & IFF_TAP)
238 print_string(PRINT_ANY, NULL, " %s", "tap");
239
240 if (!(flags & IFF_NO_PI))
241 print_string(PRINT_ANY, NULL, " %s", "pi");
242
243 if (flags & IFF_ONE_QUEUE)
244 print_string(PRINT_ANY, NULL, " %s", "one_queue");
245
246 if (flags & IFF_VNET_HDR)
247 print_string(PRINT_ANY, NULL, " %s", "vnet_hdr");
248
249 if (flags & IFF_PERSIST)
250 print_string(PRINT_ANY, NULL, " %s", "persist");
251
252 if (!(flags & IFF_NOFILTER))
253 print_string(PRINT_ANY, NULL, " %s", "filter");
254
255 flags &= ~(IFF_TUN | IFF_TAP | IFF_NO_PI | IFF_ONE_QUEUE |
256 IFF_VNET_HDR | IFF_PERSIST | IFF_NOFILTER);
257 if (flags)
258 print_0xhex(PRINT_ANY, NULL, "%#llx", flags);
259
260 close_json_array(PRINT_JSON, NULL);
261 }
262
263 static char *pid_name(pid_t pid)
264 {
265 char *comm;
266 FILE *f;
267 int err;
268
269 err = asprintf(&comm, "/proc/%d/comm", pid);
270 if (err < 0)
271 return NULL;
272
273 f = fopen(comm, "r");
274 free(comm);
275 if (!f) {
276 perror("fopen");
277 return NULL;
278 }
279
280 if (fscanf(f, "%ms\n", &comm) != 1) {
281 perror("fscanf");
282 comm = NULL;
283 }
284
285
286 if (fclose(f))
287 perror("fclose");
288
289 return comm;
290 }
291
292 static void show_processes(const char *name)
293 {
294 glob_t globbuf = { };
295 char **fd_path;
296 int err;
297
298 err = glob("/proc/[0-9]*/fd/[0-9]*", GLOB_NOSORT,
299 NULL, &globbuf);
300 if (err)
301 return;
302
303 open_json_array(PRINT_JSON, "processes");
304
305 fd_path = globbuf.gl_pathv;
306 while (*fd_path) {
307 const char *dev_net_tun = "/dev/net/tun";
308 const size_t linkbuf_len = strlen(dev_net_tun) + 2;
309 char linkbuf[linkbuf_len], *fdinfo;
310 int pid, fd;
311 FILE *f;
312
313 if (sscanf(*fd_path, "/proc/%d/fd/%d", &pid, &fd) != 2)
314 goto next;
315
316 if (pid == getpid())
317 goto next;
318
319 err = readlink(*fd_path, linkbuf, linkbuf_len - 1);
320 if (err < 0) {
321 perror("readlink");
322 goto next;
323 }
324 linkbuf[err] = '\0';
325 if (strcmp(dev_net_tun, linkbuf))
326 goto next;
327
328 if (asprintf(&fdinfo, "/proc/%d/fdinfo/%d", pid, fd) < 0)
329 goto next;
330
331 f = fopen(fdinfo, "r");
332 free(fdinfo);
333 if (!f) {
334 perror("fopen");
335 goto next;
336 }
337
338 while (!feof(f)) {
339 char *key = NULL, *value = NULL;
340
341 err = fscanf(f, "%m[^:]: %ms\n", &key, &value);
342 if (err == EOF) {
343 if (ferror(f))
344 perror("fscanf");
345 break;
346 } else if (err == 2 &&
347 !strcmp("iff", key) &&
348 !strcmp(name, value)) {
349 char *pname = pid_name(pid);
350
351 print_string(PRINT_ANY, "name",
352 "%s", pname ? : "<NULL>");
353
354 print_uint(PRINT_ANY, "pid",
355 "(%d)", pid);
356 free(pname);
357 }
358
359 free(key);
360 free(value);
361 }
362 if (fclose(f))
363 perror("fclose");
364
365 next:
366 ++fd_path;
367 }
368 close_json_array(PRINT_JSON, NULL);
369
370 globfree(&globbuf);
371 }
372
373 static int tuntap_filter_req(struct nlmsghdr *nlh, int reqlen)
374 {
375 struct rtattr *linkinfo;
376 int err;
377
378 linkinfo = addattr_nest(nlh, reqlen, IFLA_LINKINFO);
379
380 err = addattr_l(nlh, reqlen, IFLA_INFO_KIND,
381 drv_name, sizeof(drv_name) - 1);
382 if (err)
383 return err;
384
385 addattr_nest_end(nlh, linkinfo);
386
387 return 0;
388 }
389
390 static int print_tuntap(struct nlmsghdr *n, void *arg)
391 {
392 struct ifinfomsg *ifi = NLMSG_DATA(n);
393 struct rtattr *tb[IFLA_MAX+1];
394 struct rtattr *linkinfo[IFLA_INFO_MAX+1];
395 const char *name, *kind;
396 long flags, owner = -1, group = -1;
397
398 if (n->nlmsg_type != RTM_NEWLINK && n->nlmsg_type != RTM_DELLINK)
399 return 0;
400
401 if (n->nlmsg_len < NLMSG_LENGTH(sizeof(*ifi)))
402 return -1;
403
404 switch (ifi->ifi_type) {
405 case ARPHRD_NONE:
406 case ARPHRD_ETHER:
407 break;
408 default:
409 return 0;
410 }
411
412 parse_rtattr(tb, IFLA_MAX, IFLA_RTA(ifi), IFLA_PAYLOAD(n));
413
414 if (!tb[IFLA_IFNAME])
415 return 0;
416
417 if (!tb[IFLA_LINKINFO])
418 return 0;
419
420 parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]);
421
422 if (!linkinfo[IFLA_INFO_KIND])
423 return 0;
424
425 kind = rta_getattr_str(linkinfo[IFLA_INFO_KIND]);
426 if (strcmp(kind, drv_name))
427 return 0;
428
429 name = rta_getattr_str(tb[IFLA_IFNAME]);
430
431 if (read_prop(name, "tun_flags", &flags))
432 return 0;
433 if (read_prop(name, "owner", &owner))
434 return 0;
435 if (read_prop(name, "group", &group))
436 return 0;
437
438 open_json_object(NULL);
439 print_color_string(PRINT_ANY, COLOR_IFNAME,
440 "ifname", "%s:", name);
441 print_flags(flags);
442 if (owner != -1)
443 print_u64(PRINT_ANY, "user",
444 " user %ld", owner);
445 if (group != -1)
446 print_u64(PRINT_ANY, "group",
447 " group %ld", group);
448
449 if (show_details) {
450 print_string(PRINT_FP, NULL,
451 "%s\tAttached to processes:", _SL_);
452 show_processes(name);
453 }
454 close_json_object();
455 print_string(PRINT_FP, NULL, "%s", "\n");
456
457 return 0;
458 }
459
460 static int do_show(int argc, char **argv)
461 {
462 if (rtnl_linkdump_req_filter_fn(&rth, AF_UNSPEC,
463 tuntap_filter_req) < 0) {
464 perror("Cannot send dump request\n");
465 return -1;
466 }
467
468 new_json_obj(json);
469
470 if (rtnl_dump_filter(&rth, print_tuntap, NULL) < 0) {
471 fprintf(stderr, "Dump terminated\n");
472 return -1;
473 }
474
475 delete_json_obj();
476 fflush(stdout);
477
478 return 0;
479 }
480
481 int do_iptuntap(int argc, char **argv)
482 {
483 if (argc > 0) {
484 if (matches(*argv, "add") == 0)
485 return do_add(argc-1, argv+1);
486 if (matches(*argv, "delete") == 0)
487 return do_del(argc-1, argv+1);
488 if (matches(*argv, "show") == 0 ||
489 matches(*argv, "lst") == 0 ||
490 matches(*argv, "list") == 0)
491 return do_show(argc-1, argv+1);
492 if (matches(*argv, "help") == 0)
493 usage();
494 } else
495 return do_show(0, NULL);
496
497 fprintf(stderr, "Command \"%s\" is unknown, try \"ip tuntap help\".\n",
498 *argv);
499 exit(-1);
500 }
501
502 static void print_owner(FILE *f, uid_t uid)
503 {
504 struct passwd *pw = getpwuid(uid);
505
506 if (pw)
507 print_string(PRINT_ANY, "user", "user %s ", pw->pw_name);
508 else
509 print_uint(PRINT_ANY, "user", "user %u ", uid);
510 }
511
512 static void print_group(FILE *f, gid_t gid)
513 {
514 struct group *group = getgrgid(gid);
515
516 if (group)
517 print_string(PRINT_ANY, "group", "group %s ", group->gr_name);
518 else
519 print_uint(PRINT_ANY, "group", "group %u ", gid);
520 }
521
522 static void print_mq(FILE *f, struct rtattr *tb[])
523 {
524 if (!tb[IFLA_TUN_MULTI_QUEUE] ||
525 !rta_getattr_u8(tb[IFLA_TUN_MULTI_QUEUE])) {
526 if (is_json_context())
527 print_bool(PRINT_JSON, "multi_queue", NULL, false);
528 return;
529 }
530
531 print_bool(PRINT_ANY, "multi_queue", "multi_queue ", true);
532
533 if (tb[IFLA_TUN_NUM_QUEUES]) {
534 print_uint(PRINT_ANY, "numqueues", "numqueues %u ",
535 rta_getattr_u32(tb[IFLA_TUN_NUM_QUEUES]));
536 }
537
538 if (tb[IFLA_TUN_NUM_DISABLED_QUEUES]) {
539 print_uint(PRINT_ANY, "numdisabled", "numdisabled %u ",
540 rta_getattr_u32(tb[IFLA_TUN_NUM_DISABLED_QUEUES]));
541 }
542 }
543
544 static void print_type(FILE *f, __u8 type)
545 {
546 SPRINT_BUF(buf);
547 const char *str = buf;
548
549 if (type == IFF_TUN)
550 str = "tun";
551 else if (type == IFF_TAP)
552 str = "tap";
553 else
554 snprintf(buf, sizeof(buf), "UNKNOWN:%hhu", type);
555
556 print_string(PRINT_ANY, "type", "type %s ", str);
557 }
558
559 static void tun_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
560 {
561 if (!tb)
562 return;
563
564 if (tb[IFLA_TUN_TYPE])
565 print_type(f, rta_getattr_u8(tb[IFLA_TUN_TYPE]));
566
567 if (tb[IFLA_TUN_PI])
568 print_on_off(PRINT_ANY, "pi", "pi %s ",
569 rta_getattr_u8(tb[IFLA_TUN_PI]));
570
571 if (tb[IFLA_TUN_VNET_HDR]) {
572 print_on_off(PRINT_ANY, "vnet_hdr", "vnet_hdr %s ",
573 rta_getattr_u8(tb[IFLA_TUN_VNET_HDR]));
574 }
575
576 print_mq(f, tb);
577
578 if (tb[IFLA_TUN_PERSIST])
579 print_on_off(PRINT_ANY, "persist", "persist %s ",
580 rta_getattr_u8(tb[IFLA_TUN_PERSIST]));
581
582 if (tb[IFLA_TUN_OWNER])
583 print_owner(f, rta_getattr_u32(tb[IFLA_TUN_OWNER]));
584
585 if (tb[IFLA_TUN_GROUP])
586 print_group(f, rta_getattr_u32(tb[IFLA_TUN_GROUP]));
587 }
588
589 struct link_util tun_link_util = {
590 .id = "tun",
591 .maxattr = IFLA_TUN_MAX,
592 .print_opt = tun_print_opt,
593 };