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