File: netbsd_tap.c

package info (click to toggle)
aircrack-ng 1%3A1.6%2Bgit20210130.91820bc-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 19,056 kB
  • sloc: ansic: 67,045; cs: 5,392; sh: 3,773; python: 2,565; pascal: 1,074; asm: 570; makefile: 253; cpp: 46
file content (221 lines) | stat: -rw-r--r-- 4,812 bytes parent folder | download | duplicates (3)
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
  *  Copyright (c) 2007, 2008, Andrea Bittau <a.bittau@cs.ucl.ac.uk>
  *
  *  OS dependent API for NetBSD. TAP routines
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *  the Free Software Foundation; either version 2 of the License, or
  *  (at your option) any later version.
  *
  *  This program is distributed in the hope that it will be useful,
  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  *  GNU General Public License for more details.
  *
  *  You should have received a copy of the GNU General Public License
  *  along with this program; if not, write to the Free Software
  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/time.h>
#include <sys/socket.h>
#include <net/if.h>
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

#include "osdep.h"

struct tip_nbsd
{
	int tn_fd;
	int tn_ioctls;
	struct ifreq tn_ifr;
	char tn_name[IFNAMSIZ];
	int tn_destroy;
};

static int ti_do_open_nbsd(struct tif * ti, char * name)
{
	int fd;
	char * iface = "/dev/tap";
	struct stat st;
	struct tip_nbsd * priv = ti_priv(ti);
	int s;
	unsigned int flags;
	struct ifreq * ifr;

	/* open tap */
	if (name)
		iface = name;
	else
		priv->tn_destroy = 1; /* we create, we destroy */

	fd = open(iface, O_RDWR);
	if (fd == -1) return -1;

	/* get name */
	if (fstat(fd, &st) == -1) goto err;
	snprintf(priv->tn_name,
			 sizeof(priv->tn_name) - 1,
			 "%s",
			 devname(st.st_rdev, S_IFCHR));

	/* bring iface up */
	s = socket(PF_INET, SOCK_DGRAM, 0);
	if (s == -1) goto err;
	priv->tn_ioctls = s;

	/* get flags */
	ifr = &priv->tn_ifr;
	memset(ifr, 0, sizeof(*ifr));
	snprintf(ifr->ifr_name, sizeof(ifr->ifr_name), "%s", priv->tn_name);
	if (ioctl(s, SIOCGIFFLAGS, ifr) == -1) goto err2;
	flags = ifr->ifr_flags;

	/* set flags */
	flags |= IFF_UP;
	ifr->ifr_flags = flags & 0xffff;
	if (ioctl(s, SIOCSIFFLAGS, ifr) == -1) goto err2;

	return fd;
err:
	/* XXX destroy */
	close(fd);
	return -1;
err2:
	close(s);
	goto err;
}

static void ti_do_free(struct tif * ti)
{
	struct tip_nbsd * priv = ti_priv(ti);

	free(priv);
	free(ti);
}

static void ti_destroy(struct tip_nbsd * priv)
{
	ioctl(priv->tn_ioctls, SIOCIFDESTROY, &priv->tn_ifr);
}

static void ti_close_nbsd(struct tif * ti)
{
	struct tip_nbsd * priv = ti_priv(ti);

	if (priv->tn_destroy) ti_destroy(priv);

	close(priv->tn_fd);
	close(priv->tn_ioctls);
	ti_do_free(ti);
}

static char * ti_name_nbsd(struct tif * ti)
{
	struct tip_nbsd * priv = ti_priv(ti);

	return priv->tn_name;
}

static int ti_set_mtu_nbsd(struct tif * ti, int mtu)
{
	struct tip_nbsd * priv = ti_priv(ti);

	priv->tn_ifr.ifr_mtu = mtu;

	return ioctl(priv->tn_ioctls, SIOCSIFMTU, &priv->tn_ifr);
}

static int ti_set_mac_nbsd(struct tif * ti, unsigned char * mac)
{
	struct tip_nbsd * priv = ti_priv(ti);
	struct ifreq * ifr = &priv->tn_ifr;

	ifr->ifr_addr.sa_family = AF_LINK;
	ifr->ifr_addr.sa_len = 6;
	memcpy(ifr->ifr_addr.sa_data, mac, 6);

	return ioctl(priv->tn_ioctls, SIOCSIFADDR, ifr);
}

static int ti_set_ip_nbsd(struct tif * ti, struct in_addr * ip)
{
	struct tip_nbsd * priv = ti_priv(ti);
	struct ifaliasreq ifra;
	struct sockaddr_in * s_in;

	/* assume same size */
	memset(&ifra, 0, sizeof(ifra));
	strncpy(ifra.ifra_name, priv->tn_ifr.ifr_name, IFNAMSIZ);

	s_in = (struct sockaddr_in *) &ifra.ifra_addr;
	s_in->sin_family = PF_INET;
	s_in->sin_addr = *ip;
	s_in->sin_len = sizeof(*s_in);

	return ioctl(priv->tn_ioctls, SIOCAIFADDR, &ifra);
}

static int ti_fd_nbsd(struct tif * ti)
{
	struct tip_nbsd * priv = ti_priv(ti);

	return priv->tn_fd;
}

static int ti_read_nbsd(struct tif * ti, void * buf, int len)
{
	return read(ti_fd(ti), buf, len);
}

static int ti_write_nbsd(struct tif * ti, void * buf, int len)
{
	return write(ti_fd(ti), buf, len);
}

static struct tif * ti_open_nbsd(char * iface)
{
	struct tif * ti;
	struct tip_nbsd * priv;
	int fd;

	/* setup ti struct */
	ti = ti_alloc(sizeof(*priv));
	if (!ti) return NULL;
	ti->ti_name = ti_name_nbsd;
	ti->ti_set_mtu = ti_set_mtu_nbsd;
	ti->ti_close = ti_close_nbsd;
	ti->ti_fd = ti_fd_nbsd;
	ti->ti_read = ti_read_nbsd;
	ti->ti_write = ti_write_nbsd;
	ti->ti_set_mac = ti_set_mac_nbsd;
	ti->ti_set_ip = ti_set_ip_nbsd;

	/* setup iface */
	fd = ti_do_open_nbsd(ti, iface);
	if (fd == -1)
	{
		ti_do_free(ti);
		return NULL;
	}

	/* setup private state */
	priv = ti_priv(ti);
	priv->tn_fd = fd;

	return ti;
}

EXPORT struct tif * ti_open(char * iface) { return ti_open_nbsd(iface); }