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