patches and low-level development discussion
 help / color / mirror / code / Atom feed
a9457c608bbbbf1b0409b0976ca08db532c991ad blob 3518 bytes (raw)

  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
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
 
// SPDX-FileCopyrightText: 2019 Alyssa Ross <hi@alyssa.is>
// SPDX-License-Identifier: GPL-2.0-only

#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>
#include <sys/ioctl.h>

#include <linux/if.h>
#include <linux/if_tun.h>

/*
 * ifr_flags, ifr_name, etc. are macros, so throughout this program
 * variable names are suffixed with underscores where not doing so
 * would conflict with a macro.
 */

// Adapted 2019-12-14 from Documentation/networking/tuntuntap.txt in Linux.
int tuntap_alloc(char *dev, int open_flags, short ifr_flags_)
{
	struct ifreq ifr;
	int fd, err;

	if ((fd = open("/dev/net/tun", O_RDWR | open_flags)) < 0)
		return -1;

	memset(&ifr, 0, sizeof(ifr));

	ifr.ifr_flags = ifr_flags_;
	if (*dev)
		strncpy(ifr.ifr_name, dev, IFNAMSIZ);

	if ((err = ioctl(fd, TUNSETIFF, (void *) &ifr)) < 0) {
		close(fd);
		return err;
	}
	strcpy(dev, ifr.ifr_name);
	return fd;
}

void ex_usage()
{
	fputs("mktuntap: usage: mktuntap (-n | -p) [-v] [-B] [-P] [-E] [-i ifr_name] fd prog...\n", stderr);
	exit(EX_USAGE);
}

int main(int argc, char **argv)
{
	char *ifr_name_in = NULL;
	bool name_env = true;
	bool tap = false, tun = false;
	bool vnet_hdr = false, non_block = false, no_pi = true;

	int c;
	while ((c = getopt(argc, argv, "+npvBPEi:")) != -1) {
		switch (c) {
		case 'n':
			tun = true;
			break;
		case 'p':
			tap = true;
			break;
		case 'v':
			vnet_hdr = true;
			break;
		case 'B':
			non_block = true;
			break;
		case 'P':
			no_pi = false;
			break;
		case 'E':
			name_env = false;
			break;
		case 'i':
			ifr_name_in = optarg;
			break;
		default:
			ex_usage();
		}
	}

	if (argc - optind < 2 || !(tap ^ tun))
		ex_usage();

	char *ifr_name_ = calloc(IFNAMSIZ, sizeof(char));
	if (ifr_name_in) {
		if (strlen(ifr_name_in) > IFNAMSIZ - 1) {
			fprintf(stderr, "mktuntap: ifr_name too long (max %i)\n", IFNAMSIZ - 1);
			return EX_USAGE;
		}
		strcpy(ifr_name_, ifr_name_in);
	}

	errno = 0;
	char *target_fd_end;
	long int target_fd_l = strtol(argv[optind], &target_fd_end, 10);
	if (errno) {
		fprintf(stderr, "mktuntap: %s\n", strerror(errno));
		return EX_USAGE;
	}
	if (argv[optind][0] == '\0' || target_fd_end[0] != '\0') {
		fprintf(stderr, "mktuntap: invalid integer: `%s'\n", argv[optind]);
		return EX_USAGE;
	}
	if (target_fd_l < INT_MIN || target_fd_l > INT_MAX) {
		fprintf(stderr, "mktuntap: out of range for file descriptor: `%li'\n", target_fd_l);
		return EX_USAGE;
	}
	int target_fd = (int)target_fd_l;

	short open_flags = 0;
	if (non_block)
		open_flags |= O_NONBLOCK;
	short ifr_flags_ = tap ? IFF_TAP : IFF_TUN;
	if (no_pi)
		ifr_flags_ |= IFF_NO_PI;
	if (vnet_hdr)
		ifr_flags_ |= IFF_VNET_HDR;
	int tuntap_fd = tuntap_alloc(ifr_name_, open_flags, ifr_flags_);
	if (tuntap_fd < 0) {
		fprintf(stderr, "mktuntap: failed to open tuntap device: %s\n", strerror(errno));
		return EX_IOERR;
	}

	if (tuntap_fd != target_fd) {
		if (dup2(tuntap_fd, target_fd) == -1) {
			fprintf(stderr, "mktuntap: %s\n", strerror(errno));
			return EX_IOERR;
		}
		if (close(tuntap_fd) == -1) {
			fprintf(stderr, "mktuntap: %s\n", strerror(errno));
			return EX_IOERR;
		}
	}

	if (name_env) {
		if (setenv("TUNTAP_NAME", ifr_name_, 1) == -1) {
			fprintf(stderr, "mktuntap: %s\n", strerror(errno));
			return EX_OSERR;
		}
	}

	execvp(argv[optind + 1], &argv[optind + 1]);

	fprintf(stderr, "failed to exec: %s\n", strerror(errno));
	return EX_OSERR;
}
debug log:

solving a9457c6 ...
found a9457c6 in https://spectrum-os.org/git/mktuntap

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).