Debian Codegraph @

contrib/ports/unix/proj/minimal/mintapif.c

raw
1 /*-----------------------------------------------------------------------------------*/
2 /*
3 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without modification,
7 * are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
18 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
20 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
22 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
25 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
26 * OF SUCH DAMAGE.
27 *
28 * This file is part of the lwIP TCP/IP stack.
29 *
30 * Author: Adam Dunkels <[email protected]>
31 *
32 */
33
34 #include <fcntl.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <sys/ioctl.h>
40 #include <sys/socket.h>
41 #include <sys/types.h>
42 #include <sys/time.h>
43 #include <sys/uio.h>
44 #include <sys/socket.h>
45
46 #if defined(linux)
47 #include <sys/ioctl.h>
48 #include <linux/if.h>
49 #include <linux/if_tun.h>
50 #define DEVTAP "/dev/net/tun"
51 #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d"
52
53 #elif defined(openbsd)
54 #define DEVTAP "/dev/tun0"
55 #define IFCONFIG_ARGS "tun0 inet %d.%d.%d.%d link0"
56
57 #else /* freebsd, cygwin? */
58 #define DEVTAP "/dev/tap0"
59 #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d"
60 #endif
61
62 #include "lwip/stats.h"
63 #include "lwip/snmp.h"
64 #include "lwip/mem.h"
65 #include "netif/etharp.h"
66
67 #include "mintapif.h"
68
69 /* Define those to better describe your network interface. */
70 #define IFNAME0 'e'
71 #define IFNAME1 't'
72
73 struct mintapif {
74 struct eth_addr *ethaddr;
75 /* Add whatever per-interface state that is needed here. */
76 u32_t lasttime;
77 int fd;
78 };
79
80 /* Forward declarations. */
81 static void mintapif_input(struct netif *netif);
82
83 /*-----------------------------------------------------------------------------------*/
84 static void
85 low_level_init(struct netif *netif)
86 {
87 struct mintapif *mintapif;
88 char buf[1024];
89 int ret;
90
91 mintapif = (struct mintapif *)netif->state;
92
93 /* Obtain MAC address from network interface. */
94 mintapif->ethaddr->addr[0] = 1;
95 mintapif->ethaddr->addr[1] = 2;
96 mintapif->ethaddr->addr[2] = 3;
97 mintapif->ethaddr->addr[3] = 4;
98 mintapif->ethaddr->addr[4] = 5;
99 mintapif->ethaddr->addr[5] = 6;
100
101 /* device capabilities */
102 /* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
103 netif->flags |= NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
104
105 /* Do whatever else is needed to initialize interface. */
106
107 mintapif->fd = open(DEVTAP, O_RDWR);
108 if (mintapif->fd == -1) {
109 perror("tapif: tapif_init: open");
110 exit(1);
111 }
112
113 #ifdef linux
114 {
115 struct ifreq ifr;
116 memset(&ifr, 0, sizeof(ifr));
117 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
118 if (ioctl(mintapif->fd, TUNSETIFF, (void *) &ifr) < 0) {
119 perror(buf);
120 exit(1);
121 }
122 }
123 #endif /* Linux */
124
125 snprintf(buf, sizeof(buf), "/sbin/ifconfig " IFCONFIG_ARGS,
126 ip4_addr1(&(netif->gw)),
127 ip4_addr2(&(netif->gw)),
128 ip4_addr3(&(netif->gw)),
129 ip4_addr4(&(netif->gw)));
130
131 ret = system(buf);
132 if (ret < 0) {
133 perror("ifconfig failed");
134 exit(1);
135 }
136 if (ret != 0) {
137 printf("ifconfig returned %d\n", ret);
138 }
139
140 mintapif->lasttime = 0;
141
142 }
143 /*-----------------------------------------------------------------------------------*/
144 /*
145 * low_level_output():
146 *
147 * Should do the actual transmission of the packet. The packet is
148 * contained in the pbuf that is passed to the function. This pbuf
149 * might be chained.
150 *
151 */
152 /*-----------------------------------------------------------------------------------*/
153
154 static err_t
155 low_level_output(struct netif *netif, struct pbuf *p)
156 {
157 struct mintapif *mintapif;
158 struct pbuf *q;
159 char buf[1514];
160 char *bufptr;
161 int written;
162
163 mintapif = (struct mintapif *)netif->state;
164
165 /* initiate transfer(); */
166
167 bufptr = &buf[0];
168
169 for(q = p; q != NULL; q = q->next) {
170 /* Send the data from the pbuf to the interface, one pbuf at a
171 time. The size of the data in each pbuf is kept in the ->len
172 variable. */
173 /* send data from(q->payload, q->len); */
174 memcpy(bufptr, q->payload, q->len);
175 bufptr += q->len;
176 }
177
178 /* signal that packet should be sent(); */
179 written = write(mintapif->fd, buf, p->tot_len);
180 if (written == -1) {
181 snmp_inc_ifoutdiscards(netif);
182 perror("tapif: write");
183 }
184 else {
185 snmp_add_ifoutoctets(netif, written);
186 }
187 return ERR_OK;
188 }
189 /*-----------------------------------------------------------------------------------*/
190 /*
191 * low_level_input():
192 *
193 * Should allocate a pbuf and transfer the bytes of the incoming
194 * packet from the interface into the pbuf.
195 *
196 */
197 /*-----------------------------------------------------------------------------------*/
198 static struct pbuf *
199 low_level_input(struct netif *netif)
200 {
201 struct pbuf *p, *q;
202 u16_t len;
203 char buf[1514];
204 char *bufptr;
205 struct mintapif *mintapif;
206
207 mintapif = (struct mintapif *)netif->state;
208
209 /* Obtain the size of the packet and put it into the "len"
210 variable. */
211 len = read(mintapif->fd, buf, sizeof(buf));
212 snmp_add_ifinoctets(netif,len);
213
214 /* if (((double)rand()/(double)RAND_MAX) < 0.1) {
215 printf("drop\n");
216 return NULL;
217 }*/
218
219 /* We allocate a pbuf chain of pbufs from the pool. */
220 p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL);
221
222 if (p != NULL) {
223 /* We iterate over the pbuf chain until we have read the entire
224 packet into the pbuf. */
225 bufptr = &buf[0];
226 for(q = p; q != NULL; q = q->next) {
227 /* Read enough bytes to fill this pbuf in the chain. The
228 available data in the pbuf is given by the q->len
229 variable. */
230 /* read data into(q->payload, q->len); */
231 memcpy(q->payload, bufptr, q->len);
232 bufptr += q->len;
233 }
234 /* acknowledge that packet has been read(); */
235 } else {
236 /* drop packet(); */
237 snmp_inc_ifindiscards(netif);
238 printf("Could not allocate pbufs\n");
239 }
240
241 return p;
242 }
243 /*-----------------------------------------------------------------------------------*/
244 /*
245 * mintapif_input():
246 *
247 * This function should be called when a packet is ready to be read
248 * from the interface. It uses the function low_level_input() that
249 * should handle the actual reception of bytes from the network
250 * interface.
251 *
252 */
253 /*-----------------------------------------------------------------------------------*/
254 static void
255 mintapif_input(struct netif *netif)
256 {
257 struct pbuf *p;
258
259 p = low_level_input(netif);
260 if (p != NULL) {
261 #if LINK_STATS
262 lwip_stats.link.recv++;
263 #endif /* LINK_STATS */
264
265 netif->input(p, netif);
266 }
267 }
268 /*-----------------------------------------------------------------------------------*/
269 /*
270 * mintapif_init():
271 *
272 * Should be called at the beginning of the program to set up the
273 * network interface. It calls the function low_level_init() to do the
274 * actual setup of the hardware.
275 *
276 */
277 /*-----------------------------------------------------------------------------------*/
278 err_t
279 mintapif_init(struct netif *netif)
280 {
281 struct mintapif *mintapif;
282
283 mintapif = (struct mintapif *)mem_malloc(sizeof(struct mintapif));
284 if (mintapif == NULL)
285 {
286 LWIP_DEBUGF(NETIF_DEBUG, ("cs8900_init: out of memory for mintapif\n"));
287 return ERR_MEM;
288 }
289 netif->state = mintapif;
290 #if LWIP_SNMP
291 /* ifType is other(1), there doesn't seem
292 to be a proper type for the tunnel if */
293 netif->link_type = 1;
294 /* @todo get this from struct tunif? */
295 netif->link_speed = 0;
296 netif->ts = 0;
297 netif->ifinoctets = 0;
298 netif->ifinucastpkts = 0;
299 netif->ifinnucastpkts = 0;
300 netif->ifindiscards = 0;
301 netif->ifoutoctets = 0;
302 netif->ifoutucastpkts = 0;
303 netif->ifoutnucastpkts = 0;
304 netif->ifoutdiscards = 0;
305 #endif
306
307 netif->hwaddr_len = 6;
308 netif->name[0] = IFNAME0;
309 netif->name[1] = IFNAME1;
310 netif->output = etharp_output;
311 netif->linkoutput = low_level_output;
312 netif->mtu = 1500;
313
314 mintapif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
315
316 low_level_init(netif);
317
318 return ERR_OK;
319 }
320 /*-----------------------------------------------------------------------------------*/
321 enum mintapif_signal
322 mintapif_wait(struct netif *netif, u16_t time)
323 {
324 fd_set fdset;
325 struct timeval tv, now;
326 struct timezone tz;
327 int ret;
328 struct mintapif *mintapif;
329
330 mintapif = (struct mintapif *)netif->state;
331
332 while (1) {
333
334 if (mintapif->lasttime >= (u32_t)time * 1000) {
335 mintapif->lasttime = 0;
336 return MINTAPIF_TIMEOUT;
337 }
338
339 tv.tv_sec = 0;
340 tv.tv_usec = (u32_t)time * 1000 - mintapif->lasttime;
341
342
343 FD_ZERO(&fdset);
344 FD_SET(mintapif->fd, &fdset);
345
346 gettimeofday(&now, &tz);
347 ret = select(mintapif->fd + 1, &fdset, NULL, NULL, &tv);
348 if (ret == 0) {
349 mintapif->lasttime = 0;
350 return MINTAPIF_TIMEOUT;
351 }
352 gettimeofday(&tv, &tz);
353 mintapif->lasttime += (tv.tv_sec - now.tv_sec) * 1000000 + (tv.tv_usec - now.tv_usec);
354
355 mintapif_input(netif);
356 }
357
358 return MINTAPIF_PACKET;
359 }
360
361 int
362 mintapif_select(struct netif *netif)
363 {
364 fd_set fdset;
365 int ret;
366 struct timeval tv;
367 struct mintapif *mintapif;
368
369 mintapif = (struct mintapif *)netif->state;
370
371 tv.tv_sec = 0;
372 tv.tv_usec = 0; /* usec_to; */
373
374 FD_ZERO(&fdset);
375 FD_SET(mintapif->fd, &fdset);
376
377 ret = select(mintapif->fd + 1, &fdset, NULL, NULL, &tv);
378 if (ret > 0) {
379 mintapif_input(netif);
380 }
381 return ret;
382 }