1 /* $NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause-NetBSD
6 * Copyright (c) 2008 Iain Hibbert
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __RCSID("$NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
35 #include <sys/types.h>
36 #include <sys/param.h>
37 #include <sys/ioctl.h>
40 #include <net/if_tap.h>
48 #define L2CAP_SOCKET_CHECKED
51 static bool tap_send(channel_t *, packet_t *);
52 static bool tap_recv(packet_t *);
60 char pidfile[PATH_MAX];
62 fd = open(interface_name, O_RDWR);
64 log_err("Could not open \"%s\": %m", interface_name);
68 memset(&ifr, 0, sizeof(ifr));
69 if (ioctl(fd, TAPGIFNAME, &ifr) == -1) {
70 log_err("Could not get interface name: %m");
74 s = socket(AF_INET, SOCK_DGRAM, 0);
76 log_err("Could not open PF_LINK socket: %m");
80 ifr.ifr_addr.sa_family = AF_LINK;
81 ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
82 b2eaddr(ifr.ifr_addr.sa_data, &local_bdaddr);
84 if (ioctl(s, SIOCSIFLLADDR, &ifr) == -1) {
85 log_err("Could not set %s physical address: %m", ifr.ifr_name);
89 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
90 log_err("Could not get interface flags: %m");
94 if ((ifr.ifr_flags & IFF_UP) == 0) {
95 ifr.ifr_flags |= IFF_UP;
97 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) {
98 log_err("Could not set IFF_UP: %m");
105 log_info("Using interface %s with addr %s", ifr.ifr_name,
106 ether_ntoa((struct ether_addr *)&ifr.ifr_addr.sa_data));
108 chan = channel_alloc();
112 chan->send = tap_send;
113 chan->recv = tap_recv;
114 chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
115 memcpy(chan->raddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
116 memcpy(chan->laddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
117 chan->state = CHANNEL_OPEN;
118 if (!channel_open(chan, fd))
121 snprintf(pidfile, sizeof(pidfile), "%s/%s.pid",
122 _PATH_VARRUN, ifr.ifr_name);
123 chan->pfh = pidfile_open(pidfile, 0600, NULL);
124 if (chan->pfh == NULL)
125 log_err("can't create pidfile");
126 else if (pidfile_write(chan->pfh) < 0) {
127 log_err("can't write pidfile");
128 pidfile_remove(chan->pfh);
134 tap_send(channel_t *chan, packet_t *pkt)
139 iov[0].iov_base = pkt->dst;
140 iov[0].iov_len = ETHER_ADDR_LEN;
141 iov[1].iov_base = pkt->src;
142 iov[1].iov_len = ETHER_ADDR_LEN;
143 iov[2].iov_base = pkt->type;
144 iov[2].iov_len = ETHER_TYPE_LEN;
145 iov[3].iov_base = pkt->ptr;
146 iov[3].iov_len = pkt->len;
148 /* tap device write never fails */
149 nw = writev(chan->fd, iov, __arraycount(iov));
156 tap_recv(packet_t *pkt)
159 if (pkt->len < ETHER_HDR_LEN)
163 packet_adj(pkt, ETHER_ADDR_LEN);
165 packet_adj(pkt, ETHER_ADDR_LEN);
166 pkt->type = pkt->ptr;
167 packet_adj(pkt, ETHER_TYPE_LEN);