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