Home | History | Annotate | Line # | Download | only in btpand
tap.c revision 1.1
      1  1.1  plunky /*	$NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
      2  1.1  plunky 
      3  1.1  plunky /*-
      4  1.1  plunky  * Copyright (c) 2008 Iain Hibbert
      5  1.1  plunky  * All rights reserved.
      6  1.1  plunky  *
      7  1.1  plunky  * Redistribution and use in source and binary forms, with or without
      8  1.1  plunky  * modification, are permitted provided that the following conditions
      9  1.1  plunky  * are met:
     10  1.1  plunky  * 1. Redistributions of source code must retain the above copyright
     11  1.1  plunky  *    notice, this list of conditions and the following disclaimer.
     12  1.1  plunky  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  plunky  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  plunky  *    documentation and/or other materials provided with the distribution.
     15  1.1  plunky  *
     16  1.1  plunky  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.1  plunky  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.1  plunky  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.1  plunky  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.1  plunky  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.1  plunky  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.1  plunky  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.1  plunky  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.1  plunky  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.1  plunky  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  1.1  plunky  */
     27  1.1  plunky 
     28  1.1  plunky #include <sys/cdefs.h>
     29  1.1  plunky __RCSID("$NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
     30  1.1  plunky 
     31  1.1  plunky #include <sys/ioctl.h>
     32  1.1  plunky #include <sys/uio.h>
     33  1.1  plunky 
     34  1.1  plunky #include <net/if_dl.h>
     35  1.1  plunky #include <net/if_tap.h>
     36  1.1  plunky 
     37  1.1  plunky #include <fcntl.h>
     38  1.1  plunky #include <unistd.h>
     39  1.1  plunky #include <util.h>
     40  1.1  plunky 
     41  1.1  plunky #include "btpand.h"
     42  1.1  plunky 
     43  1.1  plunky static bool tap_send(channel_t *, packet_t *);
     44  1.1  plunky static bool tap_recv(packet_t *);
     45  1.1  plunky 
     46  1.1  plunky void
     47  1.1  plunky tap_init(void)
     48  1.1  plunky {
     49  1.1  plunky 	channel_t *chan;
     50  1.1  plunky 	struct sockaddr_dl *sdl;
     51  1.1  plunky 	struct ifaliasreq ifra;
     52  1.1  plunky 	struct ifreq ifr;
     53  1.1  plunky 	int fd, s;
     54  1.1  plunky 
     55  1.1  plunky 	fd = open(interface_name, O_RDWR);
     56  1.1  plunky 	if (fd == -1) {
     57  1.1  plunky 		log_err("Could not open \"%s\": %m", interface_name);
     58  1.1  plunky 		exit(EXIT_FAILURE);
     59  1.1  plunky 	}
     60  1.1  plunky 
     61  1.1  plunky 	memset(&ifr, 0, sizeof(ifr));
     62  1.1  plunky 	if (ioctl(fd, TAPGIFNAME, &ifr) == -1) {
     63  1.1  plunky 		log_err("Could not get interface name: %m");
     64  1.1  plunky 		exit(EXIT_FAILURE);
     65  1.1  plunky 	}
     66  1.1  plunky 
     67  1.1  plunky 	s = socket(PF_LINK, SOCK_DGRAM, 0);
     68  1.1  plunky 	if (s == -1) {
     69  1.1  plunky 		log_err("Could not open PF_LINK socket: %m");
     70  1.1  plunky 		exit(EXIT_FAILURE);
     71  1.1  plunky 	}
     72  1.1  plunky 
     73  1.1  plunky 	memset(&ifra, 0, sizeof(ifra));
     74  1.1  plunky 	memcpy(ifra.ifra_name, ifr.ifr_name, IFNAMSIZ);
     75  1.1  plunky 
     76  1.1  plunky 	sdl = satosdl(&ifra.ifra_addr);
     77  1.1  plunky 	sdl->sdl_family = AF_LINK;
     78  1.1  plunky 	sdl->sdl_len = sizeof(struct sockaddr_dl);
     79  1.1  plunky 	sdl->sdl_alen = ETHER_ADDR_LEN;
     80  1.1  plunky 	b2eaddr(LLADDR(sdl), &local_bdaddr);
     81  1.1  plunky 
     82  1.1  plunky 	if (ioctl(s, SIOCSIFPHYADDR, &ifra) == -1) {
     83  1.1  plunky 		log_err("Could not set %s physical address: %m", ifra.ifra_name);
     84  1.1  plunky 		exit(EXIT_FAILURE);
     85  1.1  plunky 	}
     86  1.1  plunky 
     87  1.1  plunky 	if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
     88  1.1  plunky 		log_err("Could not get interface flags: %m");
     89  1.1  plunky 		exit(EXIT_FAILURE);
     90  1.1  plunky 	}
     91  1.1  plunky 
     92  1.1  plunky 	if ((ifr.ifr_flags & IFF_UP) == 0) {
     93  1.1  plunky 		ifr.ifr_flags |= IFF_UP;
     94  1.1  plunky 
     95  1.1  plunky 		if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) {
     96  1.1  plunky 			log_err("Could not set IFF_UP: %m");
     97  1.1  plunky 			exit(EXIT_FAILURE);
     98  1.1  plunky 		}
     99  1.1  plunky 	}
    100  1.1  plunky 
    101  1.1  plunky 	close(s);
    102  1.1  plunky 
    103  1.1  plunky 	log_info("Using interface %s with addr %s",
    104  1.1  plunky 	    ifr.ifr_name, ether_ntoa((struct ether_addr *)LLADDR(sdl)));
    105  1.1  plunky 
    106  1.1  plunky 	chan = channel_alloc();
    107  1.1  plunky 	if (chan == NULL)
    108  1.1  plunky 		exit(EXIT_FAILURE);
    109  1.1  plunky 
    110  1.1  plunky 	chan->send = tap_send;
    111  1.1  plunky 	chan->recv = tap_recv;
    112  1.1  plunky 	chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
    113  1.1  plunky 	memcpy(chan->raddr, LLADDR(sdl), ETHER_ADDR_LEN);
    114  1.1  plunky 	memcpy(chan->laddr, LLADDR(sdl), ETHER_ADDR_LEN);
    115  1.1  plunky 	chan->state = CHANNEL_OPEN;
    116  1.1  plunky 	if (!channel_open(chan, fd))
    117  1.1  plunky 		exit(EXIT_FAILURE);
    118  1.1  plunky 
    119  1.1  plunky 	if (pidfile(ifr.ifr_name) == -1)
    120  1.1  plunky 		log_err("pidfile not made");
    121  1.1  plunky }
    122  1.1  plunky 
    123  1.1  plunky static bool
    124  1.1  plunky tap_send(channel_t *chan, packet_t *pkt)
    125  1.1  plunky {
    126  1.1  plunky 	struct iovec iov[4];
    127  1.1  plunky 	ssize_t nw;
    128  1.1  plunky 
    129  1.1  plunky 	iov[0].iov_base = pkt->dst;
    130  1.1  plunky 	iov[0].iov_len = ETHER_ADDR_LEN;
    131  1.1  plunky 	iov[1].iov_base = pkt->src;
    132  1.1  plunky 	iov[1].iov_len = ETHER_ADDR_LEN;
    133  1.1  plunky 	iov[2].iov_base = pkt->type;
    134  1.1  plunky 	iov[2].iov_len = ETHER_TYPE_LEN;
    135  1.1  plunky 	iov[3].iov_base = pkt->ptr;
    136  1.1  plunky 	iov[3].iov_len = pkt->len;
    137  1.1  plunky 
    138  1.1  plunky 	/* tap device write never fails */
    139  1.1  plunky 	nw = writev(chan->fd, iov, __arraycount(iov));
    140  1.1  plunky 	_DIAGASSERT(nw > 0);
    141  1.1  plunky 
    142  1.1  plunky 	return true;
    143  1.1  plunky }
    144  1.1  plunky 
    145  1.1  plunky static bool
    146  1.1  plunky tap_recv(packet_t *pkt)
    147  1.1  plunky {
    148  1.1  plunky 
    149  1.1  plunky 	if (pkt->len < ETHER_HDR_LEN)
    150  1.1  plunky 		return false;
    151  1.1  plunky 
    152  1.1  plunky 	pkt->dst = pkt->ptr;
    153  1.1  plunky 	packet_adj(pkt, ETHER_ADDR_LEN);
    154  1.1  plunky 	pkt->src = pkt->ptr;
    155  1.1  plunky 	packet_adj(pkt, ETHER_ADDR_LEN);
    156  1.1  plunky 	pkt->type = pkt->ptr;
    157  1.1  plunky 	packet_adj(pkt, ETHER_TYPE_LEN);
    158  1.1  plunky 
    159  1.1  plunky 	return true;
    160  1.1  plunky }
    161