aboutsummaryrefslogtreecommitdiff
path: root/toys/net/tunctl.c
blob: 6a2cf1ee76f57130ec3a923dd021ef6c6426a004 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/* tunctl.c - Control tap/tun network devices.
 *
 * Copyright 2016 Rob Landley <rob@landley.net>
 *
 * See http://kernel.org/doc/Documentation/networking/tuntap.txt
 *
 * This is useful for things like "kvm -netdev tap" and containers.
 * See https://landley.net/lxc/02-networking.html for example usage.
 *
 * todo: bridge mode 
 *  -b	bridge daemon (forwards packets between NAME and NAME2 interfaces)


USE_TUNCTL(NEWTOY(tunctl, "<1>1t|d|u:T[!td]", TOYFLAG_USR|TOYFLAG_BIN))

config TUNCTL
  bool "tunctl"
  default y
  help
    usage: tunctl [-dtT] [-u USER] NAME

    Create and delete tun/tap virtual ethernet devices.

    -T	Use tap (ethernet frames) instead of tun (ip packets)
    -d	Delete tun/tap device
    -t	Create tun/tap device
    -u	Set owner (user who can read/write device without root access)
*/

#define FOR_tunctl
#include "toys.h"
#include <linux/if_tun.h>

GLOBALS(
  char *u;
)

void tunctl_main(void)
{
  struct ifreq *ifr = (void *)toybuf;
  uid_t u = TT.u ?  xgetuid(TT.u) : 0;
  int fd = xopen("/dev/net/tun", O_RDWR);

  // Associate filehandle with device
  ifr->ifr_flags = ((toys.optflags&FLAG_T) ? IFF_TUN : IFF_TAP)|IFF_NO_PI;
  strncpy(ifr->ifr_name, *toys.optargs, sizeof(ifr->ifr_name));
  xioctl(fd, TUNSETIFF, toybuf);

  if (toys.optflags&FLAG_t) {
    xioctl(fd, TUNSETPERSIST, (void *)1);
    xioctl(fd, TUNSETOWNER, (void *)(long)u);
  } else xioctl(fd, TUNSETPERSIST, (void *)0);
}