1 /*
2 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
19 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
21 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
24 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
25 * OF SUCH DAMAGE.
26 *
27 * This file is part of the lwIP TCP/IP stack.
28 *
29 * Author: Adam Dunkels <adam@sics.se>
30 *
31 */
32
33 #include <fcntl.h>
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <sys/ioctl.h>
39 #include <sys/socket.h>
40 #include <sys/types.h>
41 #include <sys/time.h>
42 #include <sys/uio.h>
43 #include <sys/socket.h>
44
45 #include "lwip/opt.h"
46
47 #include "lwip/debug.h"
48 #include "lwip/def.h"
49 #include "lwip/ip.h"
50 #include "lwip/mem.h"
51 #include "lwip/stats.h"
52 #include "lwip/snmp.h"
53 #include "lwip/pbuf.h"
54 #include "lwip/sys.h"
55 #include "lwip/timeouts.h"
56 #include "netif/etharp.h"
57 #include "lwip/ethip6.h"
58
59 #include "netif/tapif.h"
60
61 #define IFCONFIG_BIN "/sbin/ifconfig "
62
63 #if defined(LWIP_UNIX_LINUX)
64 #include <sys/ioctl.h>
65 #include <linux/if.h>
66 #include <linux/if_tun.h>
67 /*
68 * Creating a tap interface requires special privileges. If the interfaces
69 * is created in advance with `tunctl -u <user>` it can be opened as a regular
70 * user. The network must already be configured. If DEVTAP_IF is defined it
71 * will be opened instead of creating a new tap device.
72 *
73 * You can also use PRECONFIGURED_TAPIF environment variable to do so.
74 */
75 #ifndef DEVTAP_DEFAULT_IF
76 #define DEVTAP_DEFAULT_IF "tap0"
77 #endif
78 #ifndef DEVTAP
79 #define DEVTAP "/dev/net/tun"
80 #endif
81 #define NETMASK_ARGS "netmask %d.%d.%d.%d"
82 #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS
83 #elif defined(LWIP_UNIX_OPENBSD)
84 #define DEVTAP "/dev/tun0"
85 #define NETMASK_ARGS "netmask %d.%d.%d.%d"
86 #define IFCONFIG_ARGS "tun0 inet %d.%d.%d.%d " NETMASK_ARGS " link0"
87 #else /* others */
88 #define DEVTAP "/dev/tap0"
89 #define NETMASK_ARGS "netmask %d.%d.%d.%d"
90 #define IFCONFIG_ARGS "tap0 inet %d.%d.%d.%d " NETMASK_ARGS
91 #endif
92
93 /* Define those to better describe your network interface. */
94 #define IFNAME0 't'
95 #define IFNAME1 'p'
96
97 #ifndef TAPIF_DEBUG
98 #define TAPIF_DEBUG LWIP_DBG_OFF
99 #endif
100
101 struct tapif {
102 /* Add whatever per-interface state that is needed here. */
103 int fd;
104 };
105
106 /* Forward declarations. */
107 static void tapif_input(struct netif *netif);
108 #if !NO_SYS
109 static void tapif_thread(void *arg);
110 #endif /* !NO_SYS */
111
112 /*-----------------------------------------------------------------------------------*/
113 static void
low_level_init(struct netif * netif)114 low_level_init(struct netif *netif)
115 {
116 struct tapif *tapif;
117 #if LWIP_IPV4
118 int ret;
119 char buf[1024];
120 #endif /* LWIP_IPV4 */
121 char *preconfigured_tapif = getenv("PRECONFIGURED_TAPIF");
122
123 tapif = (struct tapif *)netif->state;
124
125 /* Obtain MAC address from network interface. */
126
127 /* (We just fake an address...) */
128 netif->hwaddr[0] = 0x02;
129 netif->hwaddr[1] = 0x12;
130 netif->hwaddr[2] = 0x34;
131 netif->hwaddr[3] = 0x56;
132 netif->hwaddr[4] = 0x78;
133 netif->hwaddr[5] = 0xab;
134 netif->hwaddr_len = 6;
135
136 /* device capabilities */
137 netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_IGMP;
138
139 tapif->fd = open(DEVTAP, O_RDWR);
140 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: fd %d\n", tapif->fd));
141 if (tapif->fd == -1) {
142 #ifdef LWIP_UNIX_LINUX
143 perror("tapif_init: try running \"modprobe tun\" or rebuilding your kernel with CONFIG_TUN; cannot open "DEVTAP);
144 #else /* LWIP_UNIX_LINUX */
145 perror("tapif_init: cannot open "DEVTAP);
146 #endif /* LWIP_UNIX_LINUX */
147 exit(1);
148 }
149
150 #ifdef LWIP_UNIX_LINUX
151 {
152 struct ifreq ifr;
153 memset(&ifr, 0, sizeof(ifr));
154
155 if (preconfigured_tapif) {
156 strncpy(ifr.ifr_name, preconfigured_tapif, sizeof(ifr.ifr_name) - 1);
157 } else {
158 strncpy(ifr.ifr_name, DEVTAP_DEFAULT_IF, sizeof(ifr.ifr_name) - 1);
159 }
160 ifr.ifr_name[sizeof(ifr.ifr_name)-1] = 0; /* ensure \0 termination */
161
162 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
163 if (ioctl(tapif->fd, TUNSETIFF, (void *) &ifr) < 0) {
164 perror("tapif_init: "DEVTAP" ioctl TUNSETIFF");
165 exit(1);
166 }
167 }
168 #endif /* LWIP_UNIX_LINUX */
169
170 netif_set_link_up(netif);
171
172 if (preconfigured_tapif == NULL) {
173 #if LWIP_IPV4
174 snprintf(buf, 1024, IFCONFIG_BIN IFCONFIG_ARGS,
175 ip4_addr1(netif_ip4_gw(netif)),
176 ip4_addr2(netif_ip4_gw(netif)),
177 ip4_addr3(netif_ip4_gw(netif)),
178 ip4_addr4(netif_ip4_gw(netif))
179 #ifdef NETMASK_ARGS
180 ,
181 ip4_addr1(netif_ip4_netmask(netif)),
182 ip4_addr2(netif_ip4_netmask(netif)),
183 ip4_addr3(netif_ip4_netmask(netif)),
184 ip4_addr4(netif_ip4_netmask(netif))
185 #endif /* NETMASK_ARGS */
186 );
187
188 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: system(\"%s\");\n", buf));
189 ret = system(buf);
190 if (ret < 0) {
191 perror("ifconfig failed");
192 exit(1);
193 }
194 if (ret != 0) {
195 printf("ifconfig returned %d\n", ret);
196 }
197 #else /* LWIP_IPV4 */
198 perror("todo: support IPv6 support for non-preconfigured tapif");
199 exit(1);
200 #endif /* LWIP_IPV4 */
201 }
202
203 #if !NO_SYS
204 sys_thread_new("tapif_thread", tapif_thread, netif, DEFAULT_THREAD_STACKSIZE, DEFAULT_THREAD_PRIO);
205 #endif /* !NO_SYS */
206 }
207 /*-----------------------------------------------------------------------------------*/
208 /*
209 * low_level_output():
210 *
211 * Should do the actual transmission of the packet. The packet is
212 * contained in the pbuf that is passed to the function. This pbuf
213 * might be chained.
214 *
215 */
216 /*-----------------------------------------------------------------------------------*/
217
218 static err_t
low_level_output(struct netif * netif,struct pbuf * p)219 low_level_output(struct netif *netif, struct pbuf *p)
220 {
221 struct tapif *tapif = (struct tapif *)netif->state;
222 char buf[1518]; /* max packet size including VLAN excluding CRC */
223 ssize_t written;
224
225 #if 0
226 if (((double)rand()/(double)RAND_MAX) < 0.2) {
227 printf("drop output\n");
228 return ERR_OK; /* ERR_OK because we simulate packet loss on cable */
229 }
230 #endif
231
232 if (p->tot_len > sizeof(buf)) {
233 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
234 perror("tapif: packet too large");
235 return ERR_IF;
236 }
237
238 /* initiate transfer(); */
239 pbuf_copy_partial(p, buf, p->tot_len, 0);
240
241 /* signal that packet should be sent(); */
242 written = write(tapif->fd, buf, p->tot_len);
243 if (written < p->tot_len) {
244 MIB2_STATS_NETIF_INC(netif, ifoutdiscards);
245 perror("tapif: write");
246 return ERR_IF;
247 } else {
248 MIB2_STATS_NETIF_ADD(netif, ifoutoctets, (u32_t)written);
249 return ERR_OK;
250 }
251 }
252 /*-----------------------------------------------------------------------------------*/
253 /*
254 * low_level_input():
255 *
256 * Should allocate a pbuf and transfer the bytes of the incoming
257 * packet from the interface into the pbuf.
258 *
259 */
260 /*-----------------------------------------------------------------------------------*/
261 static struct pbuf *
low_level_input(struct netif * netif)262 low_level_input(struct netif *netif)
263 {
264 struct pbuf *p;
265 u16_t len;
266 ssize_t readlen;
267 char buf[1518]; /* max packet size including VLAN excluding CRC */
268 struct tapif *tapif = (struct tapif *)netif->state;
269
270 /* Obtain the size of the packet and put it into the "len"
271 variable. */
272 readlen = read(tapif->fd, buf, sizeof(buf));
273 if (readlen < 0) {
274 perror("read returned -1");
275 exit(1);
276 }
277 len = (u16_t)readlen;
278
279 MIB2_STATS_NETIF_ADD(netif, ifinoctets, len);
280
281 #if 0
282 if (((double)rand()/(double)RAND_MAX) < 0.2) {
283 printf("drop\n");
284 return NULL;
285 }
286 #endif
287
288 /* We allocate a pbuf chain of pbufs from the pool. */
289 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
290 if (p != NULL) {
291 pbuf_take(p, buf, len);
292 /* acknowledge that packet has been read(); */
293 } else {
294 /* drop packet(); */
295 MIB2_STATS_NETIF_INC(netif, ifindiscards);
296 LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: could not allocate pbuf\n"));
297 }
298
299 return p;
300 }
301
302 /*-----------------------------------------------------------------------------------*/
303 /*
304 * tapif_input():
305 *
306 * This function should be called when a packet is ready to be read
307 * from the interface. It uses the function low_level_input() that
308 * should handle the actual reception of bytes from the network
309 * interface.
310 *
311 */
312 /*-----------------------------------------------------------------------------------*/
313 static void
tapif_input(struct netif * netif)314 tapif_input(struct netif *netif)
315 {
316 struct pbuf *p = low_level_input(netif);
317
318 if (p == NULL) {
319 #if LINK_STATS
320 LINK_STATS_INC(link.recv);
321 #endif /* LINK_STATS */
322 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: low_level_input returned NULL\n"));
323 return;
324 }
325
326 if (netif->input(p, netif) != ERR_OK) {
327 LWIP_DEBUGF(NETIF_DEBUG, ("tapif_input: netif input error\n"));
328 pbuf_free(p);
329 }
330 }
331 /*-----------------------------------------------------------------------------------*/
332 /*
333 * tapif_init():
334 *
335 * Should be called at the beginning of the program to set up the
336 * network interface. It calls the function low_level_init() to do the
337 * actual setup of the hardware.
338 *
339 */
340 /*-----------------------------------------------------------------------------------*/
341 err_t
tapif_init(struct netif * netif)342 tapif_init(struct netif *netif)
343 {
344 struct tapif *tapif = (struct tapif *)mem_malloc(sizeof(struct tapif));
345
346 if (tapif == NULL) {
347 LWIP_DEBUGF(NETIF_DEBUG, ("tapif_init: out of memory for tapif\n"));
348 return ERR_MEM;
349 }
350 netif->state = tapif;
351 MIB2_INIT_NETIF(netif, snmp_ifType_other, 100000000);
352
353 netif->name[0] = IFNAME0;
354 netif->name[1] = IFNAME1;
355 #if LWIP_IPV4
356 netif->output = etharp_output;
357 #endif /* LWIP_IPV4 */
358 #if LWIP_IPV6
359 netif->output_ip6 = ethip6_output;
360 #endif /* LWIP_IPV6 */
361 netif->linkoutput = low_level_output;
362 netif->mtu = 1500;
363
364 low_level_init(netif);
365
366 return ERR_OK;
367 }
368
369
370 /*-----------------------------------------------------------------------------------*/
371 void
tapif_poll(struct netif * netif)372 tapif_poll(struct netif *netif)
373 {
374 tapif_input(netif);
375 }
376
377 #if NO_SYS
378
379 int
tapif_select(struct netif * netif)380 tapif_select(struct netif *netif)
381 {
382 fd_set fdset;
383 int ret;
384 struct timeval tv;
385 struct tapif *tapif;
386 u32_t msecs = sys_timeouts_sleeptime();
387
388 tapif = (struct tapif *)netif->state;
389
390 tv.tv_sec = msecs / 1000;
391 tv.tv_usec = (msecs % 1000) * 1000;
392
393 FD_ZERO(&fdset);
394 FD_SET(tapif->fd, &fdset);
395
396 ret = select(tapif->fd + 1, &fdset, NULL, NULL, &tv);
397 if (ret > 0) {
398 tapif_input(netif);
399 }
400 return ret;
401 }
402
403 #else /* NO_SYS */
404
405 static void
tapif_thread(void * arg)406 tapif_thread(void *arg)
407 {
408 struct netif *netif;
409 struct tapif *tapif;
410 fd_set fdset;
411 int ret;
412
413 netif = (struct netif *)arg;
414 tapif = (struct tapif *)netif->state;
415
416 while(1) {
417 FD_ZERO(&fdset);
418 FD_SET(tapif->fd, &fdset);
419
420 /* Wait for a packet to arrive. */
421 ret = select(tapif->fd + 1, &fdset, NULL, NULL, NULL);
422
423 if(ret == 1) {
424 /* Handle incoming packet. */
425 tapif_input(netif);
426 } else if(ret == -1) {
427 perror("tapif_thread: select");
428 }
429 }
430 }
431
432 #endif /* NO_SYS */
433