File: net.c

package info (click to toggle)
oskit 0.97.20000202-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 58,008 kB
  • ctags: 172,612
  • sloc: ansic: 832,827; asm: 7,640; sh: 3,920; yacc: 3,664; perl: 1,457; lex: 427; makefile: 337; csh: 141; awk: 78
file content (515 lines) | stat: -rw-r--r-- 11,855 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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
/*
 * Copyright (c) 1997,1999,2000 University of Utah and the Flux Group.
 * All rights reserved.
 * 
 * This file is part of the Flux OSKit.  The OSKit is free software, also known
 * as "open source;" you can redistribute it and/or modify it under the terms
 * of the GNU General Public License (GPL), version 2, as published by the Free
 * Software Foundation (FSF).  To explore alternate licensing terms, contact
 * the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
 * 
 * The OSKit 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 GPL for more details.  You should have
 * received a copy of the GPL along with the OSKit; see the file COPYING.  If
 * not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
 */
#define __USE_BSD
#include <sys/types.h>

#include <oskit/dev/dev.h>
#include <oskit/dev/net.h>
#include <oskit/dev/ethernet.h>
#include <oskit/io/bufio.h>
#include <oskit/error.h>

#include "native.h"
#include "support.h"

#include <fcntl.h>
#include <string.h>
#include <net/if.h>
#include <netinet/if_ether.h>
#include <sys/ioctl.h>
#include <arpa/inet.h>

/**************************************************************************/


/**************************************************************************/

/*** Network send I/O interface ***/

typedef struct linuxnetdev {
	oskit_netio_t	nio;
	oskit_etherdev_t	eio;
	unsigned	count;
	int		fd;
	oskit_netio_t	*recv;
	unsigned char	ethaddr[OSKIT_ETHERDEV_ADDR_SIZE];
	char		name[6];
	unsigned char	*recvbuf;
	unsigned int	recvbuflen;
        int		offset;
} linuxnetdev_t;

/*
 * Query a net I/O object for its interfaces.
 */
static OSKIT_COMDECL
net_query(oskit_netio_t *io, const oskit_iid_t *iid, void **out_ihandle)
{
	linuxnetdev_t *dev = (linuxnetdev_t *)io;

	if (dev == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (dev->count == 0)
		panic("%s:%d: bad count", __FILE__, __LINE__);

	if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
	    memcmp(iid, &oskit_netio_iid, sizeof(*iid)) == 0) {
		*out_ihandle = &dev->nio;
		++dev->count;
		return 0;
	}

	*out_ihandle = NULL;
	return OSKIT_E_NOINTERFACE;
}

/*
 * Clone a reference to a device's block I/O interface.
 */
static OSKIT_COMDECL_U
net_addref(oskit_netio_t *io)
{
	linuxnetdev_t *dev = (linuxnetdev_t *)io;

	if (dev == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (dev->count == 0)
		panic("%s:%d: bad count", __FILE__, __LINE__);

	return ++dev->count;
}


/*
 * Close ("release") a device.
 */
static OSKIT_COMDECL_U
net_release(oskit_netio_t *io)
{
	linuxnetdev_t *dev = (linuxnetdev_t *)io;
	unsigned newcount;

	if (dev == NULL)
		panic("%s:%d: null peropen", __FILE__, __LINE__);
	if (dev->count == 0)
		panic("%s:%d: bad count", __FILE__, __LINE__);

	newcount = dev->count - 1;
	if (newcount == 0) {

		unregister_async_fd(dev->fd);
		NATIVEOS(close)(dev->fd);
		dev->fd = 0;

		/*
		 * Release our reference on the client's
		 * receive net_io interface.
		 */
		oskit_netio_release(dev->recv);
		dev->recv = NULL;
	}

	return dev->count = newcount;
}


/*
 * Queue a packet for transmission.
 */
static OSKIT_COMDECL
net_push(oskit_netio_t *io, oskit_bufio_t *b, oskit_size_t size)
{
        linuxnetdev_t *dev = (linuxnetdev_t *)io;
	int err, maperr;
	oskit_size_t actual;
	void *p;
	struct sockaddr to;
	int tolen;

	/*
	 * Try to map the packet into contiguous local memory.
	 * Since the map operation on net_io interfaces is optional,
	 * we have to use copyin instead if this doesn't work.
	 */
	maperr = oskit_bufio_map(b, &p, 0, size);

	if (maperr) {
		p = osenv_mem_alloc(size, OSENV_NONBLOCKING, 0);

		/* Couldn't map, try to copy it in */
	        err = oskit_bufio_read(b, p, 0, size, &actual);
		assert(actual == size);
	}
/*
 * note: this must reflect the properties of the system you'll run it on,
 * not the system you compile it on.
 */
	/*
	 * also, not that the source address is overwritten
	 * the BSD kernel doesn't allow to forge ethernet source addresses
	 * so make sure our code doesn't even attempt it:
	 */
	assert(!memcmp(p+6, dev->ethaddr, 6));
#if 0
	printf("write packet with %d bytes\n", size);
	hexdump(p, 48);
#endif

#if 0
	do {
		err = NATIVEOS(write)(dev->fd, p, size);
	} while (err == -1 && errno == EWOULDBLOCK);
#else
	memset(&to, 0, sizeof(to));
	to.sa_family = AF_INET;
	strcpy(to.sa_data, dev->name);
	tolen = sizeof(to);

	do {
	    	err = NATIVEOS(sendto)(dev->fd, p, size, 0, &to, tolen);
	} while (err == -1 && errno == EWOULDBLOCK);

#endif

	if (!maperr)
		oskit_bufio_unmap(b, p, 0, size);
	else
		osenv_mem_free(p, OSENV_NONBLOCKING, size);

	return err == size ? 0 : err;
}

/**************************************************************************/


/**************************************************************************/


/*** Network device node interface methods ***/

static OSKIT_COMDECL
netdev_query(oskit_etherdev_t *io, const oskit_iid_t *iid, void **out_ihandle)
{
        if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_device_iid, sizeof(*iid)) == 0 ||
            memcmp(iid, &oskit_etherdev_iid, sizeof(*iid)) == 0) {
                *out_ihandle = io;
                return 0;
        }

        *out_ihandle = NULL;
        return OSKIT_E_NOINTERFACE;
};

static OSKIT_COMDECL_U
netdev_addref(oskit_etherdev_t *intf)
{
	/* No reference counting */
	return 1;
}

static OSKIT_COMDECL_U
netdev_release(oskit_etherdev_t *intf)
{
	/* No reference counting */
	return 1;
}

static OSKIT_COMDECL
netdev_getinfo(oskit_etherdev_t *intf, oskit_devinfo_t *out_info)
{
	linuxnetdev_t *dev = (linuxnetdev_t *)(intf-1);
	out_info->name = dev->name;
	out_info->description = "linux networking device";
	out_info->vendor = "UofU";
	out_info->author = "Stephen Clawson";
	out_info->version =  "1.0";
	return 0;
}

static OSKIT_COMDECL
netdev_getdriver(oskit_etherdev_t *intf, oskit_driver_t **out_driver)
{
	return OSKIT_ENOTSUP;
}

/*
 * read all packets you can from this interface
 */
void
read_packets(void *_dev)
{
	linuxnetdev_t	*dev = _dev;
	oskit_error_t  	err;
	oskit_bufio_t 	*b;
	struct sockaddr from;
	int		r, fromlen;

	/* Keep trying until we get one that's for us. =) */
	/* XXX Need to check to see if this will ever matter. */

retry:
	do {
	    fromlen = sizeof(from);
	    r = NATIVEOS(recvfrom)(dev->fd, dev->recvbuf, dev->recvbuflen, 0,
				    &from, &fromlen);

	    if (r <= 0) {
		if (errno == EAGAIN)
			goto retry;
		if (r == 0 || errno == EWOULDBLOCK) {
		    osenv_log(0, "spurious wakeup (rc=%d `%s')\n", r,
			      r ? strerror(r):"");
		    goto done;
		} else {
		    perror("read_packets"),	exit(0);
		}
	    }
	} while (strcmp(dev->name, from.sa_data));

	{
	    	void *p;
		int  len, handup = 1;
#if 0
		struct ether_header *eh;
#endif

#if 0
		eh = (void *)dev->recvbuf + dev->offset;
#endif
		len = r;

#if 0
		printf("got a packet of %d length!\n", len);
#endif

		b = oskit_bufio_create(len);
		err = oskit_bufio_map(b, &p, 0, len);
		assert(!err);

#if 0
		for (i = 0; i < 14; i++) {
		    osenv_log(0, "%x ", dev->recvbuf[i]);
		}
		osenv_log(0, "\n");
#endif

		memcpy(p, dev->recvbuf, len);
#if 0
		/* Do we even get an ether header? */
		eh = p;
#endif
#if 0	/* be verbose */
		{
			int v = VERBOSE();
			if (v) {
				osenv_log(0, outgoing ? "OUT:" : "IN:");
				osenv_log(0, handup ? "HANDUP:" : "REJECT:");
				osenv_log(0, " type 0x%x, len=%d\n",
					  ntohs(eh->ether_type), fromlen);
				if (v > 3 && handup)
					dohexdump(0 /* base */,
						  p, fromlen, 0 /* bytes */);
			}
		}
#endif

		err = oskit_bufio_unmap(b, p, 0, len);
		assert(!err);

		if (handup)
			oskit_netio_push(dev->recv, b, len);

	}

done:
	/* please call read_packets(dev) when we can read */
	register_async_fd(dev->fd, IOTYPE_READ, read_packets, _dev);
	return;
}



int
open_eth(char *ifname, char *myaddr, unsigned int *buflen, int no_ieee802_3)
{
	struct ifreq ifr;
	struct sockaddr sa;
	int fd, rc;
	char *bp;

	/* Open a socket to grab all packets. */
	fd = NATIVEOS(socket)(PF_INET, SOCK_PACKET, htons(ETH_P_ALL));
	if (fd < 0) {
                panic("can't open socket!");
	}

	/* Bind our socket to a specific interface. */
	memset(&sa, 0, sizeof(sa));
	sa.sa_family = AF_INET;
	(void)strncpy(sa.sa_data, ifname, sizeof(sa.sa_data));
	rc = NATIVEOS(bind)(fd, &sa, sizeof(sa));
	if (rc) {
		panic("can't bind to interface!  ETHERIF may not be set!");
	}


	/* Get the MAC addr of our interface. */
	memset(&ifr, 0, sizeof(ifr));
	(void)strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	rc = NATIVEOS(ioctl)(fd, SIOCGIFHWADDR, &ifr);

	if (rc == 0) {
	    memcpy(myaddr, ifr.ifr_hwaddr.sa_data, IFHWADDRLEN);
	} else {
		int x0, x1, x2, x3, x4, x5;

		if ((bp = (char *) getenv("ETHERADDR")) == NULL) {
		    printf("open_eth: ETHERADDR not set\n");
		    NATIVEOS(_exit)(1);
		}

		sscanf(bp, "%x:%x:%x:%x:%x:%x", &x0, &x1, &x2, &x3, &x4, &x5);

		myaddr[0] = x0;
		myaddr[1] = x1;
		myaddr[2] = x2;
		myaddr[3] = x3;
		myaddr[4] = x4;
		myaddr[5] = x5;
	}

	memset(&ifr, 0, sizeof(ifr));
	(void)strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name));
	rc = NATIVEOS(ioctl)(fd, SIOCGIFMTU, &ifr);
	if (rc < 0)
	    	panic("can't get mtu of interface!");

	*buflen = ifr.ifr_mtu + 64;

	return fd;
}


static OSKIT_COMDECL
netdev_open(oskit_etherdev_t *intf, unsigned flags,
	   oskit_netio_t *recv_net_io,
	   oskit_netio_t **out_send_net_io)
{
	linuxnetdev_t *dev = (linuxnetdev_t *)(intf-1);
	int rc, mypid = NATIVEOS(getpid)();

	/* Only allow one opener at a time */
	if (dev->count > 0)
		return OSKIT_EBUSY;

	/* Stash a reference to the supplied recv_net_io interface */
	dev->recv = recv_net_io;
	oskit_netio_addref(recv_net_io);

	/* Caller starts out with one reference to the send_net_io interface */
	dev->count = 1;
	*out_send_net_io = &dev->nio;

	if (!dev->fd) {
		dev->fd = open_eth(dev->name, dev->ethaddr, &dev->recvbuflen,
			/* don't filter out IEEE 802.3 */ 0);
	}

	register_async_fd(dev->fd, IOTYPE_READ, read_packets, (void *)dev);

#if 0
	mypid = -mypid;
        rc = NATIVEOS(ioctl)(dev->fd, TIOCSPGRP, &mypid);
        if (rc == -1) {
	        printf("errno is %d\n", errno);
                perror("TIOCSPGRP"), exit(0);
	}
#else
        rc = NATIVEOS(fcntl)(dev->fd, F_SETOWN, mypid);
        if (rc == -1) {
	        printf("errno is %d\n", errno);
                perror("TIOCSPGRP"), exit(0);
	}
#endif

	/* set fd to async mode */
        rc = NATIVEOS(fcntl)(dev->fd, F_SETFL, O_ASYNC | O_NONBLOCK);
        if (rc == -1)
                perror("fcntl"), exit(0);

	return 0;
}

OSKIT_COMDECL_V
netdev_get_addr(oskit_etherdev_t *fdev,
	unsigned char out_addr[OSKIT_ETHERDEV_ADDR_SIZE])
{
	linuxnetdev_t *dev = (linuxnetdev_t *)(fdev-1);
	memcpy(out_addr, dev->ethaddr, OSKIT_ETHERDEV_ADDR_SIZE);
}

static struct oskit_netio_ops netio_ops = {
	net_query, net_addref, net_release,
	net_push
};

static struct oskit_etherdev_ops eth_ops = {
	netdev_query,
	netdev_addref,
	netdev_release,
	netdev_getinfo,
	netdev_getdriver,
	netdev_open,
	netdev_get_addr,
};

static	linuxnetdev_t	feth;

static int
add_ethernet_device(char *name)
{
	feth.nio.ops = &netio_ops;
	feth.eio.ops = &eth_ops;

	feth.fd = open_eth(name, feth.ethaddr, &feth.recvbuflen, 0);
	feth.recvbuf = (unsigned char *)malloc(feth.recvbuflen);
	strncpy(feth.name, name, sizeof feth.name);

	return osenv_device_register((oskit_device_t *)&feth.eio,
			&oskit_etherdev_iid, 1);
}

/* Yeah, right */
void
oskit_linux_init_net()
{
	char	*dev;
	static int init;
	if (init)
		return;
	init++;
	if (!(dev = getenv("ETHERIF")))
		dev = "eth1";
	assert(add_ethernet_device(dev) == 0);
}

#if 0
/* debugging */
int rv = 0;
VERBOSE() { return rv; }
#endif