Debian Codegraph @

lwip-contrib/ports/unix/netif/tapif.c

raw symbols SCIP
1 /* This is part of LWIPv6
2 *
3 * tap interface for ale4net
4 * Copyright 2005,2010,2011 Renzo Davoli University of Bologna - Italy
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20 /*
21 * Copyright (c) 2001-2003 Swedish Institute of Computer Science.
22 * All rights reserved.
23 *
24 * Redistribution and use in source and binary forms, with or without modification,
25 * are permitted provided that the following conditions are met:
26 *
27 * 1. Redistributions of source code must retain the above copyright notice,
28 * this list of conditions and the following disclaimer.
29 * 2. Redistributions in binary form must reproduce the above copyright notice,
30 * this list of conditions and the following disclaimer in the documentation
31 * and/or other materials provided with the distribution.
32 * 3. The name of the author may not be used to endorse or promote products
33 * derived from this software without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
37 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
38 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
39 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
40 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
41 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
42 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
43 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
44 * OF SUCH DAMAGE.
45 *
46 * This file is part of the lwIP TCP/IP stack.
47 *
48 * Author: Adam Dunkels <[email protected]>
49 *
50 */
51
52 #include <fcntl.h>
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <string.h>
56 #include <unistd.h>
57 #include <poll.h>
58 #include <sys/types.h>
59 #include <sys/uio.h>
60 #include <sys/socket.h>
61
62
63 #include "lwip/debug.h"
64
65 #include "lwip/opt.h"
66 #include "lwip/def.h"
67 #include "lwip/ip.h"
68 #include "lwip/mem.h"
69 #include "lwip/pbuf.h"
70 #include "lwip/sys.h"
71
72 #include "netif/etharp.h"
73
74 #if LWIP_NL
75 #include "lwip/arphdr.h"
76 #endif
77
78 #ifdef IPv6_AUTO_CONFIGURATION
79 #include "lwip/ip_autoconf.h"
80 #endif
81
82
83 #if defined(LWIP_DEBUG) && defined(LWIP_TCPDUMP)
84 #include "netif/tcpdump.h"
85 #endif /* LWIP_DEBUG && LWIP_TCPDUMP */
86
87 #include <sys/ioctl.h>
88 #include <linux/if.h>
89 #include <linux/if_tun.h>
90 #define DEVTAP "/dev/net/tun"
91
92 /*-----------------------------------------------------------------------------------*/
93
94 #ifndef TAPIF_DEBUG
95 #define TAPIF_DEBUG DBG_OFF
96 #endif
97
98 #define IFNAME0 't'
99 #define IFNAME1 'p'
100
101 /*-----------------------------------------------------------------------------------*/
102
103 static const struct eth_addr ethbroadcast = {{0xff,0xff,0xff,0xff,0xff,0xff}};
104
105 struct tapif {
106 struct eth_addr *ethaddr;
107 /* Add whatever per-interface state that is needed here. */
108 int fd;
109 struct netif_fddata *fddata;
110 };
111
112 /* Forward declarations. */
113 static void tapif_input(struct netif_fddata *fddata, short revents);
114 static err_t tapif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr);
115
116 /*-----------------------------------------------------------------------------------*/
117
118 static void
119 arp_timer(void *arg)
120 {
121 etharp_tmr((struct netif *) arg );
122 sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, arg);
123 }
124
125 /*-----------------------------------------------------------------------------------*/
126 static int
127 low_level_init(struct netif *netif, char *ifname)
128 {
129 struct tapif *tapif;
130
131 tapif = netif->state;
132
133 /* Obtain MAC address from network interface. */
134
135 /* (We just fake an address...) */
136 tapif->ethaddr->addr[0] = 0x2;
137 tapif->ethaddr->addr[1] = 0x2;
138 tapif->ethaddr->addr[2] = 0x3;
139 tapif->ethaddr->addr[3] = 0x4;
140 tapif->ethaddr->addr[4] = 0x5 + netif->num;
141 tapif->ethaddr->addr[5] = 0x6;
142
143 /* Do whatever else is needed to initialize interface. */
144
145 tapif->fd = open(DEVTAP, O_RDWR);
146 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_init: fd %d\n", tapif->fd));
147 if(tapif->fd == -1) {
148 perror("tapif_init: try running \"modprobe tun\" or rebuilding your kernel with CONFIG_TUN; cannot open "DEVTAP);
149 return ERR_IF;
150 }
151 {
152 struct ifreq ifr;
153 memset(&ifr, 0, sizeof(ifr));
154 ifr.ifr_flags = IFF_TAP|IFF_NO_PI;
155 if (ifname != NULL)
156 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
157 if (ioctl(tapif->fd, TUNSETIFF, (void *) &ifr) < 0) {
158 perror("tapif_init: DEVTAP ioctl TUNSETIFF");
159 return ERR_IF;
160 }
161 }
162 if ((tapif->fddata=netif_addfd(netif,
163 tapif->fd, tapif_input, NULL, 0, POLLIN)) == NULL)
164 return ERR_IF;
165 else
166 return ERR_OK;
167 }
168
169 /* cleanup: garbage collection */
170 static err_t tapif_ctl(struct netif *netif, int request, void *arg)
171 {
172 struct tapif *tapif = netif->state;
173
174 if (tapif) {
175
176 switch (request) {
177
178 case NETIFCTL_CLEANUP:
179 close(tapif->fd);
180
181 /* Unset ARP timeout on this interface */
182 sys_untimeout((sys_timeout_handler)arp_timer, netif);
183
184 mem_free(tapif);
185 }
186 }
187 return ERR_OK;
188 }
189
190
191 /*-----------------------------------------------------------------------------------*/
192 /*
193 * low_level_output():
194 *
195 * Should do the actual transmission of the packet. The packet is
196 * contained in the pbuf that is passed to the function. This pbuf
197 * might be chained.
198 *
199 */
200 /*-----------------------------------------------------------------------------------*/
201
202 static err_t
203 low_level_output(struct netif *netif, struct pbuf *p)
204 {
205 struct pbuf *q;
206 char buf[1514];
207 char *bufptr;
208 struct tapif *tapif;
209
210 tapif = netif->state;
211 /* initiate transfer(); */
212
213 bufptr = &buf[0];
214
215 for(q = p; q != NULL; q = q->next) {
216 //printf("%s: q->len=%d tot_len=%d\n", __func__, q->len, p->tot_len);
217 /* Send the data from the pbuf to the interface, one pbuf at a
218 time. The size of the data in each pbuf is kept in the ->len
219 variable. */
220 /* send data from(q->payload, q->len); */
221 memcpy(bufptr, q->payload, q->len);
222 bufptr += q->len;
223 }
224
225 /* signal that packet should be sent(); */
226 if(write(tapif->fd, buf, p->tot_len) == -1) {
227 perror("tapif: write");
228 }
229
230 return ERR_OK;
231 }
232 /*-----------------------------------------------------------------------------------*/
233 /*
234 * low_level_input():
235 *
236 * Should allocate a pbuf and transfer the bytes of the incoming
237 * packet from the interface into the pbuf.
238 *
239 */
240 /*-----------------------------------------------------------------------------------*/
241 static struct pbuf *
242 low_level_input(struct tapif *tapif,u16_t ifflags)
243 {
244 struct pbuf *p, *q;
245 u16_t len;
246 char buf[1514];
247 char *bufptr;
248
249 /* Obtain the size of the packet and put it into the "len"
250 variable. */
251 len = read(tapif->fd, buf, sizeof(buf));
252 if (!(ETH_RECEIVING_RULE(buf,tapif->ethaddr->addr,ifflags))) {
253 return NULL;
254 }
255
256 /* We allocate a pbuf chain of pbufs from the pool. */
257 p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL);
258
259 if(p != NULL) {
260 /* We iterate over the pbuf chain until we have read the entire
261 packet into the pbuf. */
262 bufptr = &buf[0];
263 for(q = p; q != NULL; q = q->next) {
264 /* Read enough bytes to fill this pbuf in the chain. The
265 available data in the pbuf is given by the q->len
266 variable. */
267 /* read data into(q->payload, q->len); */
268 memcpy(q->payload, bufptr, q->len);
269 bufptr += q->len;
270 }
271 /* acknowledge that packet has been read(); */
272 } else {
273 /* drop packet(); */
274 }
275
276 return p;
277 }
278
279 /*-----------------------------------------------------------------------------------*/
280 /*
281 * tapif_output():
282 *
283 * This function is called by the TCP/IP stack when an IP packet
284 * should be sent. It calls the function called low_level_output() to
285 * do the actuall transmission of the packet.
286 *
287 */
288 /*-----------------------------------------------------------------------------------*/
289 static err_t
290 tapif_output(struct netif *netif, struct pbuf *p, struct ip_addr *ipaddr)
291 {
292 if (! (netif->flags & NETIF_FLAG_UP)) {
293 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_output: interface DOWN, discarded\n"));
294 return ERR_OK;
295 } else
296 return etharp_output(netif, ipaddr, p);
297 }
298
299 /*-----------------------------------------------------------------------------------*/
300 /*
301 * tapif_input():
302 *
303 * This function should be called when a packet is ready to be read
304 * from the interface. It uses the function low_level_input() that
305 * should handle the actual reception of bytes from the network
306 * interface.
307 *
308 */
309 /*-----------------------------------------------------------------------------------*/
310 static void
311 tapif_input(struct netif_fddata *fddata, short revents)
312 {
313 struct netif *netif = fddata->netif;
314 struct tapif *tapif;
315 struct eth_hdr *ethhdr;
316 struct pbuf *p;
317
318 tapif = netif->state;
319
320 p = low_level_input(tapif,netif->flags);
321
322 if(p == NULL) {
323 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: low_level_input returned NULL\n"));
324 return;
325 }
326 ethhdr = p->payload;
327
328 #ifdef LWIP_PACKET
329 ETH_CHECK_PACKET_IN(netif,p);
330 #endif
331 switch(htons(ethhdr->type)) {
332 #ifdef IPv6
333 case ETHTYPE_IP6:
334 #endif
335 case ETHTYPE_IP:
336 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: IP packet\n"));
337 etharp_ip_input(netif, p);
338 pbuf_header(p, -14);
339 #if defined(LWIP_DEBUG) && defined(LWIP_TCPDUMP)
340 tcpdump(p);
341 #endif /* LWIP_DEBUG && LWIP_TCPDUMP */
342 netif->input(p, netif);
343 break;
344 case ETHTYPE_ARP:
345 LWIP_DEBUGF(TAPIF_DEBUG, ("tapif_input: ARP packet\n"));
346 etharp_arp_input(netif, tapif->ethaddr, p);
347 break;
348 default:
349 pbuf_free(p);
350 break;
351 }
352 }
353
354 /*-----------------------------------------------------------------------------------*/
355 /*
356 * tapif_init():
357 *
358 * Should be called at the beginning of the program to set up the
359 * network interface. It calls the function low_level_init() to do the
360 * actual setup of the hardware.
361 *
362 */
363 /*-----------------------------------------------------------------------------------*/
364 err_t
365 tapif_init(struct netif *netif)
366 {
367 struct tapif *tapif;
368 char *ifname;
369
370 tapif = mem_malloc(sizeof(struct tapif));
371 if (!tapif)
372 return ERR_MEM;
373 ifname = netif->state; /*state is temporarily used to store the if name */
374 netif->state = tapif;
375 netif->name[0] = IFNAME0;
376 netif->name[1] = IFNAME1;
377 netif->link_type = NETIF_TAPIF;
378 netif->num=netif_next_num(netif,NETIF_TAPIF);
379 netif->output = tapif_output;
380 netif->linkoutput = low_level_output;
381 netif->netifctl = tapif_ctl;
382 netif->mtu = 1500;
383 /* hardware address length */
384 netif->hwaddr_len = 6;
385 netif->flags|=NETIF_FLAG_BROADCAST;
386 #if LWIP_NL
387 netif->type = ARPHRD_ETHER;
388 #endif
389
390 tapif->ethaddr = (struct eth_addr *)&(netif->hwaddr[0]);
391 if (low_level_init(netif, ifname) < 0) {
392 mem_free(tapif);
393 return ERR_IF;
394 }
395
396 etharp_init();
397
398 sys_timeout(ARP_TMR_INTERVAL, (sys_timeout_handler)arp_timer, netif);
399
400 return ERR_OK;
401 }
402