| 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 |
#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 |
| 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 |
|
| 108 |
int fd; |
| 109 |
struct netif_fddata *fddata; |
| 110 |
}; |
| 111 |
|
| 112 |
|
| 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 |
|
| 134 |
|
| 135 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 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 |
|
| 212 |
|
| 213 |
bufptr = &buf[0]; |
| 214 |
|
| 215 |
for(q = p; q != NULL; q = q->next) { |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
memcpy(bufptr, q->payload, q->len); |
| 222 |
bufptr += q->len; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
if(write(tapif->fd, buf, p->tot_len) == -1) { |
| 227 |
perror("tapif: write"); |
| 228 |
} |
| 229 |
|
| 230 |
return ERR_OK; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 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 |
|
| 250 |
|
| 251 |
len = read(tapif->fd, buf, sizeof(buf)); |
| 252 |
if (!(ETH_RECEIVING_RULE(buf,tapif->ethaddr->addr,ifflags))) { |
| 253 |
return NULL; |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
p = pbuf_alloc(PBUF_RAW, len, PBUF_POOL); |
| 258 |
|
| 259 |
if(p != NULL) { |
| 260 |
|
| 261 |
|
| 262 |
bufptr = &buf[0]; |
| 263 |
for(q = p; q != NULL; q = q->next) { |
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
memcpy(q->payload, bufptr, q->len); |
| 269 |
bufptr += q->len; |
| 270 |
} |
| 271 |
|
| 272 |
} else { |
| 273 |
|
| 274 |
} |
| 275 |
|
| 276 |
return p; |
| 277 |
} |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 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 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 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 |
| 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 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 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; |
| 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 |
|
| 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 |
|