1 /* $NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */
4 * Copyright (c) 2008 Iain Hibbert
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
38 #include <net/if_tap.h>
48 static bool tap_send(channel_t *, packet_t *);
49 static bool tap_recv(packet_t *);
57 char pidfile[PATH_MAX];
59 fd = open(interface_name, O_RDWR);
61 log_err("Could not open \"%s\": %m", interface_name);
65 memset(&ifr, 0, sizeof(ifr));
66 if (ioctl(fd, TAPGIFNAME, &ifr) == -1) {
67 log_err("Could not get interface name: %m");
71 s = socket(AF_INET, SOCK_DGRAM, 0);
73 log_err("Could not open PF_LINK socket: %m");
77 ifr.ifr_addr.sa_family = AF_LINK;
78 ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
79 b2eaddr(ifr.ifr_addr.sa_data, &local_bdaddr);
81 if (ioctl(s, SIOCSIFLLADDR, &ifr) == -1) {
82 log_err("Could not set %s physical address: %m", ifr.ifr_name);
86 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
87 log_err("Could not get interface flags: %m");
91 if ((ifr.ifr_flags & IFF_UP) == 0) {
92 ifr.ifr_flags |= IFF_UP;
94 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) {
95 log_err("Could not set IFF_UP: %m");
102 log_info("Using interface %s with addr %s", ifr.ifr_name,
103 ether_ntoa((struct ether_addr *)&ifr.ifr_addr.sa_data));
105 chan = channel_alloc();
109 chan->send = tap_send;
110 chan->recv = tap_recv;
111 chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
112 memcpy(chan->raddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
113 memcpy(chan->laddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
114 chan->state = CHANNEL_OPEN;
115 if (!channel_open(chan, fd))
118 snprintf(pidfile, sizeof(pidfile), "%s/%s.pid",
119 _PATH_VARRUN, ifr.ifr_name);
120 chan->pfh = pidfile_open(pidfile, 0600, NULL);
121 if (chan->pfh == NULL)
122 log_err("can't create pidfile");
123 else if (pidfile_write(chan->pfh) < 0) {
124 log_err("can't write pidfile");
125 pidfile_remove(chan->pfh);
131 tap_send(channel_t *chan, packet_t *pkt)
136 iov[0].iov_base = pkt->dst;
137 iov[0].iov_len = ETHER_ADDR_LEN;
138 iov[1].iov_base = pkt->src;
139 iov[1].iov_len = ETHER_ADDR_LEN;
140 iov[2].iov_base = pkt->type;
141 iov[2].iov_len = ETHER_TYPE_LEN;
142 iov[3].iov_base = pkt->ptr;
143 iov[3].iov_len = pkt->len;
145 /* tap device write never fails */
146 nw = writev(chan->fd, iov, __arraycount(iov));
153 tap_recv(packet_t *pkt)
156 if (pkt->len < ETHER_HDR_LEN)
160 packet_adj(pkt, ETHER_ADDR_LEN);
162 packet_adj(pkt, ETHER_ADDR_LEN);
163 pkt->type = pkt->ptr;
164 packet_adj(pkt, ETHER_TYPE_LEN);