tap.c revision 1.2 1 /* $NetBSD: tap.c,v 1.2 2009/03/10 22:12:17 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.2 2009/03/10 22:12:17 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 if_laddrreq iflr;
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(&iflr, 0, sizeof(iflr));
74 memcpy(iflr.iflr_name, ifr.ifr_name, IFNAMSIZ);
75 iflr.flags = IFLR_ACTIVE;
76
77 sdl = satosdl(sstosa(&iflr.addr));
78 sdl->sdl_family = AF_LINK;
79 sdl->sdl_len = sizeof(struct sockaddr_dl);
80 sdl->sdl_alen = ETHER_ADDR_LEN;
81 b2eaddr(LLADDR(sdl), &local_bdaddr);
82
83 if (ioctl(s, SIOCALIFADDR, &iflr) == -1) {
84 log_err("Could not add %s link address: %m", iflr.iflr_name);
85 exit(EXIT_FAILURE);
86 }
87
88 if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
89 log_err("Could not get interface flags: %m");
90 exit(EXIT_FAILURE);
91 }
92
93 if ((ifr.ifr_flags & IFF_UP) == 0) {
94 ifr.ifr_flags |= IFF_UP;
95
96 if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) {
97 log_err("Could not set IFF_UP: %m");
98 exit(EXIT_FAILURE);
99 }
100 }
101
102 close(s);
103
104 log_info("Using interface %s with addr %s",
105 ifr.ifr_name, ether_ntoa((struct ether_addr *)LLADDR(sdl)));
106
107 chan = channel_alloc();
108 if (chan == NULL)
109 exit(EXIT_FAILURE);
110
111 chan->send = tap_send;
112 chan->recv = tap_recv;
113 chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
114 memcpy(chan->raddr, LLADDR(sdl), ETHER_ADDR_LEN);
115 memcpy(chan->laddr, LLADDR(sdl), ETHER_ADDR_LEN);
116 chan->state = CHANNEL_OPEN;
117 if (!channel_open(chan, fd))
118 exit(EXIT_FAILURE);
119
120 if (pidfile(ifr.ifr_name) == -1)
121 log_err("pidfile not made");
122 }
123
124 static bool
125 tap_send(channel_t *chan, packet_t *pkt)
126 {
127 struct iovec iov[4];
128 ssize_t nw;
129
130 iov[0].iov_base = pkt->dst;
131 iov[0].iov_len = ETHER_ADDR_LEN;
132 iov[1].iov_base = pkt->src;
133 iov[1].iov_len = ETHER_ADDR_LEN;
134 iov[2].iov_base = pkt->type;
135 iov[2].iov_len = ETHER_TYPE_LEN;
136 iov[3].iov_base = pkt->ptr;
137 iov[3].iov_len = pkt->len;
138
139 /* tap device write never fails */
140 nw = writev(chan->fd, iov, __arraycount(iov));
141 _DIAGASSERT(nw > 0);
142
143 return true;
144 }
145
146 static bool
147 tap_recv(packet_t *pkt)
148 {
149
150 if (pkt->len < ETHER_HDR_LEN)
151 return false;
152
153 pkt->dst = pkt->ptr;
154 packet_adj(pkt, ETHER_ADDR_LEN);
155 pkt->src = pkt->ptr;
156 packet_adj(pkt, ETHER_ADDR_LEN);
157 pkt->type = pkt->ptr;
158 packet_adj(pkt, ETHER_TYPE_LEN);
159
160 return true;
161 }
162