File: netif.c

package info (click to toggle)
ppxp 0.99120923-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 2,812 kB
  • ctags: 3,704
  • sloc: ansic: 24,532; tcl: 3,992; makefile: 517; sh: 80
file content (328 lines) | stat: -rw-r--r-- 6,885 bytes parent folder | download
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <net/if.h>
#include <netinet/in.h>

#include <config.h>
#include <support.h>

/*
#include "option.h"
#include "log.h"
*/
#include <log.h>
#include <env.h>
#include <dev/device.h>
#include <frame.h>

#define	_PATH_PROC_DEVICES	"/proc/devices"
#define	_PATH_PROC_NET_DEV	"/proc/net/dev"
#define	_PATH_PROC_OSRELEASE	"/proc/sys/kernel/osrelease"
#define	LEN_PROCLINE	256

#define	IFBUFSIZ	4096

static char ifBuf[IFBUFSIZ];
static int ifFd=-1;
int sockFd=-1;
unsigned int kernelVersion;

static struct {
    int hdrlen;
    char *hdrbody;
} ifHdr;

/*
#define DEBUG_IF
*/

#ifdef DEBUG_IF
static FILE *debugFp;

static void
dumpbuf(char *buf, int len)
{
    int i, j;

    for (i = 0; i < len;) {
	for (j = 0; j < 16 && i < len; j++, i++)
	    fprintf(debugFp, " %02x", (unsigned char)buf[i]);
	fprintf(debugFp, "\n");
    }
    fflush(debugFp);
}
#endif

static int
SearchMajor(FILE *fp, char *driver_name)
{
    char line[LEN_PROCLINE], name[IFNAMSIZ];
    int major = -1;

    fseek(fp, 0L, SEEK_SET);

    while (fgets(line, LEN_PROCLINE, fp)) {
	if (sscanf(line, "%d %s", &major, name) == 2)
	    if (strcmp(name, driver_name) == 0)
		break;
	major = -1;
    }
#ifdef DEBUG_IF
    fprintf(debugFp, "SearchMajor(%s):major=%d\n", driver_name, major);
    fflush(debugFp);
#endif
    return(major);
}

/* When driver is not loaded, call dummy ioctl
   to invoke kerneld daemon */
static void
DummyIoctl(char *if_name)
{
    struct sockaddr_in *sin;
    struct ifreq ifr;

#ifdef DEBUG_IF
    fprintf(debugFp, "DummyIoctl(%s)\n", if_name);
    fflush(debugFp);
#endif
    bzero(&ifr, sizeof(ifr));
    sin = (struct sockaddr_in *)&(ifr.ifr_addr);
    sin->sin_family = AF_INET;
    strcpy(ifr.ifr_name, if_name);
    ioctl(sockFd, SIOCGIFFLAGS, &ifr);
}

static u_int32_t
SearchUsedIf(FILE *fp, char *if_name)
{
    int n, namelen;
    char *p;
    char line[LEN_PROCLINE];
    u_int32_t bits;

    fseek(fp, 0L, SEEK_SET);

    namelen = strlen(if_name);
    bits = 0;
    n = 0;
    while(fgets(line, LEN_PROCLINE, fp)) {
	p = line;
	while (*p && *p == ' ') p ++;
	if (*p && !strncmp(p, if_name, namelen)) {
	    n = atoi(p + namelen);
	    if (n < sizeof(bits)) bits |= 1 << n;
	}
    }
#ifdef DEBUG_IF
    fprintf(debugFp, "SearchUsedIf(%s):bits=%d\n", if_name, bits);
    fflush(debugFp);
#endif
    return(bits);
}

static int
UserlinkOpen(int major, u_int32_t bits, char *ifname, int *ifnp)
{
    int n, fd = -1;
    char name[IFNAMSIZ];

    strcpy(name, "/tmp/ulXXXXXX"); /* special file name of char dev */
    if (mktemp(name) == NULL) return(-1);

    for (n = 0; n < sizeof(bits); n ++) {
	if (bits & (1 << n)) continue;
	if (mknod(name, (S_IFCHR|S_IREAD|S_IWRITE), major << 8 | n)) {
	    LogError("mknod");
	    return(-1);
	}
	errno = 0;
	fd = open(name, O_RDWR);
	unlink(name);
	if (fd >= 0)
	    break;
    }
    if  (fd >= 0) {
	sprintf(ifname, "ul%d", n);
	*ifnp = n;
	ifHdr.hdrlen = 0;
	ifHdr.hdrbody = NULL;
    }
    return(fd);
}

static int
EthertapOpen(int major, u_int32_t bits, char *ifname, int *ifnp)
{
    int n, fd;
    char tmpname[IFNAMSIZ], devname[IFNAMSIZ];
    struct stat st;

    strcpy(tmpname, "/tmp/tapXXXXXX");
    if (mktemp(tmpname) == NULL) return(-1);

    fd = -1;
    for (n = 0; n < sizeof(bits); n++) {
	if (!(bits & (1 << n))) continue;

	sprintf(devname, "/dev/tap%d", n);
	if (stat(devname, &st) == 0) {
	    fd = open(devname, O_RDWR);
	} else if (errno == ENOENT) {
	    dev_t dev = major << 8 | n + NETLINK_TAPBASE;
	    if(mknod(tmpname, S_IFCHR|S_IREAD|S_IWRITE, dev))
		LogError("mknod");
	    else {
		fd = open(tmpname, O_RDWR);
		unlink(tmpname);
	    }
	}
	if (fd >= 0)
	    break;
    }
    if (fd >= 0) {
	uid_t euid;
	struct ethhdr hdr;
	struct ifreq ifr;

	sprintf(ifname, "tap%d", n);
	*ifnp = n;

	memset(&ifr, 0, sizeof(ifr));
	strcpy(ifr.ifr_name, ifname);
	if (ioctl(sockFd, SIOCGIFHWADDR, &ifr) < 0) {
	    LogError("EthertapOpen SIOCGIFHWADDR");
	    memcpy(hdr.h_dest, "\xFE\xFD\x00\x00\x00\x00", ETH_ALEN);
	} else memcpy(hdr.h_dest, ifr.ifr_hwaddr.sa_data, ETH_ALEN);
	memcpy(hdr.h_source, hdr.h_dest, ETH_ALEN);
	hdr.h_proto = htons(ETH_P_IP);

	ifHdr.hdrlen = ETH_HLEN + 2;
	ifHdr.hdrbody = Malloc(ifHdr.hdrlen);
	memset(ifHdr.hdrbody, 0, ifHdr.hdrlen);
	memcpy(ifHdr.hdrbody + 2, &hdr, ETH_HLEN);

	/* writing to ethertap requires real uid == 0 */
	euid = geteuid();
	setuid(0);
	seteuid(euid);
    }
    return(fd);
}

int
SysIfOpen(char *ifname, int *ifnp)
{
    int major;
    u_int32_t bits=0;
    FILE *osrFp, *devsFp, *netdevFp;

#ifdef DEBUG_IF
    debugFp = fopen("/tmp/debug.if", "w");
#endif
    if ((osrFp = fopen(_PATH_PROC_OSRELEASE, "r")) != NULL) {
	int v0=0, v1=0, v2=0;

	fscanf(osrFp, "%d.%d.%d\n", &v0, &v1, &v2);
	fclose(osrFp);
	kernelVersion = (v0 << 16) + (v1 << 8) + v2;
    }
    sockFd = socket(AF_INET, SOCK_DGRAM, 0);

    if ((devsFp = fopen(_PATH_PROC_DEVICES, "r")) == NULL) return(-1);
    if ((netdevFp = fopen(_PATH_PROC_NET_DEV, "r")) == NULL) {
	fclose(devsFp);
	return(-1);
    }
    /* search userlink */
    major = SearchMajor(devsFp, "userlink");
    if (major < 0) {
	DummyIoctl("userlink");
	major = SearchMajor(devsFp, "userlink");
    }
    if (major >= 0) {
	bits = SearchUsedIf(netdevFp, "ul");
	ifFd = UserlinkOpen(major, bits, ifname, ifnp);
	if (ifFd >= 0)
	    goto End;
    }
    /* if userlink is not found, search ethertap */
    major = SearchMajor(devsFp, "netlink");
    if (major < 0) {
	DummyIoctl("ethertap");
	major = SearchMajor(devsFp, "netlink");
    }
    if (major >= 0) {
	bits = SearchUsedIf(netdevFp, "tap");
	if (!bits) {
	    DummyIoctl("ethertap");
	    bits = SearchUsedIf(netdevFp, "tap");
	}
	ifFd = EthertapOpen(major, bits, ifname, ifnp);
    }
 End:
    fclose(devsFp);
    fclose(netdevFp);
    return(ifFd);
}

void
SysIfClose()
{
    if (ifFd >= 0) close(ifFd);
    if (sockFd >= 0) close(sockFd);
    ifFd = sockFd = -1;

    if (ifHdr.hdrbody)
	free(ifHdr.hdrbody);

#ifdef DEBUG_IF
    fclose(debugFp);
#endif
}

int
SysIfWrite(u_char *buf, int len, long hbo_proto)
{
    int n;

    if (ifHdr.hdrlen > 0) {
	buf -= ifHdr.hdrlen;
	len += ifHdr.hdrlen;
	memcpy(buf, ifHdr.hdrbody, ifHdr.hdrlen);
    }
    n = write(ifFd, buf, len);
#ifdef DEBUG_IF
    fprintf(debugFp, "SysIfWrite:size=%d,return=%d\n", ifFd, len, n);
    dumpbuf(buf, n);
#endif
    return(n);
}

int
SysIfRead(int fd, char **bp, u_int16_t *proto)
{
    int n;

    if ((n = read(fd, ifBuf, IFBUFSIZ)) > 0) {
#ifdef DEBUG_IF
	fprintf(debugFp, "SysIfRead:size=%d\n", n);
	dumpbuf(ifBuf, n);
#endif
	if (ifHdr.hdrlen > 0) {
	    n -= ifHdr.hdrlen;
	    *bp = &ifBuf[ifHdr.hdrlen];
	} else {
	    *bp = ifBuf;
	}
    }
    *proto = NBO_PROTO_IP;
    return(n);
}