1 /* $NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $ */
4 * SPDX-License-Identifier: BSD-2-Clause
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/ioctl.h>
39 #include <net/if_tap.h>
47 #define L2CAP_SOCKET_CHECKED
50 static bool tap_send(channel_t *, packet_t *);
51 static bool tap_recv(packet_t *);
59 char pidfile[PATH_MAX];
61 fd = open(interface_name, O_RDWR);
63 log_err("Could not open \"%s\": %m", interface_name);
67 memset(&ifr, 0, sizeof(ifr));
68 if (ioctl(fd, TAPGIFNAME, &ifr) == -1) {
69 log_err("Could not get interface name: %m");
73 s = socket(AF_INET, SOCK_DGRAM, 0);
75 log_err("Could not open PF_LINK socket: %m");
79 ifr.ifr_addr.sa_family = AF_LINK;
80 ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
81 b2eaddr(ifr.ifr_addr.sa_data, &local_bdaddr);
83 if (ioctl(s, SIOCSIFLLADDR, &ifr) == -1) {
84 log_err("Could not set %s physical address: %m", ifr.ifr_name);
88 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
89 log_err("Could not get interface flags: %m");
93 if ((ifr.ifr_flags & IFF_UP) == 0) {
94 ifr.ifr_flags |= IFF_UP;
96 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) {
97 log_err("Could not set IFF_UP: %m");
104 log_info("Using interface %s with addr %s", ifr.ifr_name,
105 ether_ntoa((struct ether_addr *)&ifr.ifr_addr.sa_data));
107 chan = channel_alloc();
111 chan->send = tap_send;
112 chan->recv = tap_recv;
113 chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
114 memcpy(chan->raddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
115 memcpy(chan->laddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
116 chan->state = CHANNEL_OPEN;
117 if (!channel_open(chan, fd))
120 snprintf(pidfile, sizeof(pidfile), "%s/%s.pid",
121 _PATH_VARRUN, ifr.ifr_name);
122 chan->pfh = pidfile_open(pidfile, 0600, NULL);
123 if (chan->pfh == NULL)
124 log_err("can't create pidfile");
125 else if (pidfile_write(chan->pfh) < 0) {
126 log_err("can't write pidfile");
127 pidfile_remove(chan->pfh);
133 tap_send(channel_t *chan, packet_t *pkt)
138 iov[0].iov_base = pkt->dst;
139 iov[0].iov_len = ETHER_ADDR_LEN;
140 iov[1].iov_base = pkt->src;
141 iov[1].iov_len = ETHER_ADDR_LEN;
142 iov[2].iov_base = pkt->type;
143 iov[2].iov_len = ETHER_TYPE_LEN;
144 iov[3].iov_base = pkt->ptr;
145 iov[3].iov_len = pkt->len;
147 /* tap device write never fails */
148 nw = writev(chan->fd, iov, __arraycount(iov));
155 tap_recv(packet_t *pkt)
158 if (pkt->len < ETHER_HDR_LEN)
162 packet_adj(pkt, ETHER_ADDR_LEN);
164 packet_adj(pkt, ETHER_ADDR_LEN);
165 pkt->type = pkt->ptr;
166 packet_adj(pkt, ETHER_TYPE_LEN);