Debian Codegraph @

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

raw symbols SCIP
1 /* This is part of LWIPv6
2 *
3 * Copyright 2004,2008,2011 Renzo Davoli University of Bologna - Italy
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
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 #include "netif/etharp.h"
63
64 #include "lwip/debug.h"
65
66 #include "lwip/opt.h"
67 #include "lwip/def.h"
68 #include "lwip/ip.h"
69 #include "lwip/mem.h"
70 #include "lwip/netif.h"
71 #include "lwip/pbuf.h"
72 #include "lwip/sys.h"
73
74 #include <sys/ioctl.h>
75 #include <linux/if.h>
76 #include <linux/if_tun.h>
77 #define DEVTAP "/dev/net/tun"
78
79 /*-----------------------------------------------------------------------------------*/
80
81 #ifndef TUNIF_DEBUG
82 #define TUNIF_DEBUG DBG_OFF
83 #endif
84
85 #define IFNAME0 't'
86 #define IFNAME1 'n'
87
88 /*-----------------------------------------------------------------------------------*/
89
90 struct tunif {
91 /* Add whatever per-interface state that is needed here. */
92 int fd;
93 struct netif_fddata *fddata;
94 };
95
96 /* Forward declarations. */
97 static void tunif_input(struct netif_fddata *fddata, short revents);
98 static err_t tunif_output(struct netif *netif, struct pbuf *p,
99 struct ip_addr *ipaddr);
100
101 /*-----------------------------------------------------------------------------------*/
102 static int
103 low_level_init(struct netif *netif, char *ifname)
104 {
105 struct tunif *tunif;
106
107 tunif = netif->state;
108
109 /* Obtain MAC address from network interface. */
110
111 /* Do whatever else is needed to initialize interface. */
112
113 tunif->fd = open(DEVTAP, O_RDWR);
114 LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_init: fd %d\n", tunif->fd));
115 if (tunif->fd == -1) {
116 perror("tunif_init");
117 return ERR_IF;
118 }
119 {
120 struct ifreq ifr;
121 memset(&ifr, 0, sizeof(ifr));
122 ifr.ifr_flags = IFF_TUN|IFF_NO_PI;
123 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1);
124 if (ioctl(tunif->fd, TUNSETIFF, (void *) &ifr) < 0) {
125 perror("tunif_init: DEVTUN ioctl TUNSETIFF");
126 return ERR_IF;
127 }
128 }
129
130 if ((tunif->fddata=netif_addfd(netif,
131 tunif->fd, tunif_input, NULL, 0, POLLIN)) == NULL)
132 return ERR_IF;
133 else
134 return ERR_OK;
135 }
136 /*-----------------------------------------------------------------------------------*/
137 /*
138 * low_level_output():
139 *
140 * Should do the actual transmission of the packet. The packet is
141 * contained in the pbuf that is passed to the function. This pbuf
142 * might be chained.
143 *
144 */
145 /*-----------------------------------------------------------------------------------*/
146
147 static err_t
148 low_level_output(struct tunif *tunif, struct pbuf *p)
149 {
150 struct pbuf *q;
151 char buf[1500];
152 char *bufptr;
153
154 /* initiate transfer(); */
155
156
157 bufptr = &buf[0];
158
159 for(q = p; q != NULL; q = q->next) {
160 /* Send the data from the pbuf to the interface, one pbuf at a
161 time. The size of the data in each pbuf is kept in the ->len
162 variable. */
163 /* send data from(q->payload, q->len); */
164 bcopy(q->payload, bufptr, q->len);
165 bufptr += q->len;
166 }
167
168 /* signal that packet should be sent(); */
169 if (write(tunif->fd, buf, p->tot_len) == -1) {
170 perror("tunif: write");
171 }
172 return ERR_OK;
173 }
174 /*-----------------------------------------------------------------------------------*/
175 /*
176 * low_level_input():
177 *
178 * Should allocate a pbuf and transfer the bytes of the incoming
179 * packet from the interface into the pbuf.
180 *
181 */
182 /*-----------------------------------------------------------------------------------*/
183 static struct pbuf *
184 low_level_input(struct tunif *tunif,u16_t ifflags)
185 {
186 struct pbuf *p, *q;
187 u16_t len;
188 char buf[1500];
189 char *bufptr;
190
191 /* Obtain the size of the packet and put it into the "len"
192 variable. */
193 len = read(tunif->fd, buf, sizeof(buf));
194 if (! (ifflags & NETIF_FLAG_UP)) {
195 LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_output: interface DOWN, discarded\n"));
196 return NULL;
197 }
198
199 /* We allocate a pbuf chain of pbufs from the pool. */
200 p = pbuf_alloc(PBUF_LINK, len, PBUF_POOL);
201
202 if (p != NULL) {
203 /* We iterate over the pbuf chain until we have read the entire
204 packet into the pbuf. */
205 bufptr = &buf[0];
206 for(q = p; q != NULL; q = q->next) {
207 /* Read enough bytes to fill this pbuf in the chain. The
208 available data in the pbuf is given by the q->len
209 variable. */
210 /* read data into(q->payload, q->len); */
211 bcopy(bufptr, q->payload, q->len);
212 bufptr += q->len;
213 }
214 /* acknowledge that packet has been read(); */
215 } else {
216 /* drop packet(); */
217 }
218
219 return p;
220 }
221 /*-----------------------------------------------------------------------------------*/
222 /*
223 * tunif_output():
224 *
225 * This function is called by the TCP/IP stack when an IP packet
226 * should be sent. It calls the function called low_level_output() to
227 * do the actuall transmission of the packet.
228 *
229 */
230 /*-----------------------------------------------------------------------------------*/
231 static err_t
232 tunif_output(struct netif *netif, struct pbuf *p,
233 struct ip_addr *ipaddr)
234 {
235 struct tunif *tunif;
236
237 tunif = netif->state;
238 if (! (netif->flags & NETIF_FLAG_UP)) {
239 LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_output: interface DOWN, discarded\n"));
240 return ERR_OK;
241 } else
242 return low_level_output(tunif, p);
243 }
244 /*-----------------------------------------------------------------------------------*/
245 /*
246 * tunif_input():
247 *
248 * This function should be called when a packet is ready to be read
249 * from the interface. It uses the function low_level_input() that
250 * should handle the actual reception of bytes from the network
251 * interface.
252 *
253 */
254 /*-----------------------------------------------------------------------------------*/
255 static void
256 tunif_input(struct netif_fddata *fddata, short revents)
257 {
258 struct netif *netif = fddata->netif;
259 struct tunif *tunif;
260 struct pbuf *p;
261
262 tunif = netif->state;
263
264 p = low_level_input(tunif,netif->flags);
265
266 if (p == NULL) {
267 LWIP_DEBUGF(TUNIF_DEBUG, ("tunif_input: low_level_input returned NULL\n"));
268 return;
269 }
270
271 netif->input(p, netif);
272 }
273
274 /* cleanup: garbage collection */
275 static err_t tunif_ctl(struct netif *netif, int request, void *arg)
276 {
277 struct tunif *tunif = netif->state;
278
279 if (tunif) {
280
281 switch (request) {
282
283 case NETIFCTL_CLEANUP:
284 close(tunif->fd);
285
286 mem_free(tunif);
287 }
288 }
289 return ERR_OK;
290 }
291
292 /*-----------------------------------------------------------------------------------*/
293 /*
294 * tunif_init():
295 *
296 * Should be called at the beginning of the program to set up the
297 * network interface. It calls the function low_level_init() to do the
298 * actual setup of the hardware.
299 *
300 */
301 /*-----------------------------------------------------------------------------------*/
302 err_t
303 tunif_init(struct netif *netif)
304 {
305 struct tunif *tunif;
306 char *ifname;
307
308 tunif = mem_malloc(sizeof(struct tunif));
309 if (!tunif)
310 return ERR_MEM;
311 ifname = netif->state; /*state is temporarily used to store the if name */
312 netif->state = tunif;
313 netif->name[0] = IFNAME0;
314 netif->name[1] = IFNAME1;
315 netif->link_type = NETIF_TUNIF;
316 netif->num=netif_next_num(netif,NETIF_TUNIF);
317 netif->output = tunif_output;
318 netif->netifctl = tunif_ctl;
319 netif->mtu = 1500;
320 netif->hwaddr_len = 0;
321 #if LWIP_NL
322 netif->type = ARPHRD_NONE;
323 #endif
324
325 if (low_level_init(netif,ifname) < 0) {
326 mem_free(tunif);
327 return ERR_IF;
328 }
329
330 return ERR_OK;
331 }
332 /*-----------------------------------------------------------------------------------*/