xref: /freebsd/usr.sbin/bluetooth/btpand/tap.c (revision 193d9e768ba63fcfb187cfd17f461f7d41345048)
1 /*	$NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 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 /* $FreeBSD$ */
29 
30 #include <sys/cdefs.h>
31 __RCSID("$NetBSD: tap.c,v 1.1 2008/08/17 13:20:57 plunky Exp $");
32 
33 #include <sys/types.h>
34 #include <sys/param.h>
35 #include <sys/ioctl.h>
36 #include <sys/uio.h>
37 
38 #include <net/if_tap.h>
39 
40 #include <fcntl.h>
41 #include <libutil.h>
42 #include <paths.h>
43 #include <stdio.h>
44 #include <unistd.h>
45 
46 #define L2CAP_SOCKET_CHECKED
47 #include "btpand.h"
48 
49 static bool tap_send(channel_t *, packet_t *);
50 static bool tap_recv(packet_t *);
51 
52 void
53 tap_init(void)
54 {
55 	channel_t *chan;
56 	struct ifreq ifr;
57 	int fd, s;
58 	char pidfile[PATH_MAX];
59 
60 	fd = open(interface_name, O_RDWR);
61 	if (fd == -1) {
62 		log_err("Could not open \"%s\": %m", interface_name);
63 		exit(EXIT_FAILURE);
64 	}
65 
66 	memset(&ifr, 0, sizeof(ifr));
67 	if (ioctl(fd, TAPGIFNAME, &ifr) == -1) {
68 		log_err("Could not get interface name: %m");
69 		exit(EXIT_FAILURE);
70 	}
71 
72 	s = socket(AF_INET, SOCK_DGRAM, 0);
73 	if (s == -1) {
74 		log_err("Could not open PF_LINK socket: %m");
75 		exit(EXIT_FAILURE);
76 	}
77 
78 	ifr.ifr_addr.sa_family = AF_LINK;
79 	ifr.ifr_addr.sa_len = ETHER_ADDR_LEN;
80 	b2eaddr(ifr.ifr_addr.sa_data, &local_bdaddr);
81 
82 	if (ioctl(s, SIOCSIFLLADDR, &ifr) == -1) {
83 		log_err("Could not set %s physical address: %m", ifr.ifr_name);
84 		exit(EXIT_FAILURE);
85 	}
86 
87 	if (ioctl(s, SIOCGIFFLAGS, &ifr) == -1) {
88 		log_err("Could not get interface flags: %m");
89 		exit(EXIT_FAILURE);
90 	}
91 
92 	if ((ifr.ifr_flags & IFF_UP) == 0) {
93 		ifr.ifr_flags |= IFF_UP;
94 
95 		if (ioctl(s, SIOCSIFFLAGS, &ifr) == -1) {
96 			log_err("Could not set IFF_UP: %m");
97 			exit(EXIT_FAILURE);
98 		}
99 	}
100 
101 	close(s);
102 
103 	log_info("Using interface %s with addr %s", ifr.ifr_name,
104 		ether_ntoa((struct ether_addr *)&ifr.ifr_addr.sa_data));
105 
106 	chan = channel_alloc();
107 	if (chan == NULL)
108 		exit(EXIT_FAILURE);
109 
110 	chan->send = tap_send;
111 	chan->recv = tap_recv;
112 	chan->mru = ETHER_HDR_LEN + ETHER_MAX_LEN;
113 	memcpy(chan->raddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
114 	memcpy(chan->laddr, ifr.ifr_addr.sa_data, ETHER_ADDR_LEN);
115 	chan->state = CHANNEL_OPEN;
116 	if (!channel_open(chan, fd))
117 		exit(EXIT_FAILURE);
118 
119 	snprintf(pidfile, sizeof(pidfile), "%s/%s.pid",
120 		_PATH_VARRUN, ifr.ifr_name);
121 	chan->pfh = pidfile_open(pidfile, 0600, NULL);
122 	if (chan->pfh == NULL)
123 		log_err("can't create pidfile");
124 	else if (pidfile_write(chan->pfh) < 0) {
125 		log_err("can't write pidfile");
126 		pidfile_remove(chan->pfh);
127 		chan->pfh = NULL;
128 	}
129 }
130 
131 static bool
132 tap_send(channel_t *chan, packet_t *pkt)
133 {
134 	struct iovec iov[4];
135 	ssize_t nw;
136 
137 	iov[0].iov_base = pkt->dst;
138 	iov[0].iov_len = ETHER_ADDR_LEN;
139 	iov[1].iov_base = pkt->src;
140 	iov[1].iov_len = ETHER_ADDR_LEN;
141 	iov[2].iov_base = pkt->type;
142 	iov[2].iov_len = ETHER_TYPE_LEN;
143 	iov[3].iov_base = pkt->ptr;
144 	iov[3].iov_len = pkt->len;
145 
146 	/* tap device write never fails */
147 	nw = writev(chan->fd, iov, __arraycount(iov));
148 	assert(nw > 0);
149 
150 	return true;
151 }
152 
153 static bool
154 tap_recv(packet_t *pkt)
155 {
156 
157 	if (pkt->len < ETHER_HDR_LEN)
158 		return false;
159 
160 	pkt->dst = pkt->ptr;
161 	packet_adj(pkt, ETHER_ADDR_LEN);
162 	pkt->src = pkt->ptr;
163 	packet_adj(pkt, ETHER_ADDR_LEN);
164 	pkt->type = pkt->ptr;
165 	packet_adj(pkt, ETHER_TYPE_LEN);
166 
167 	return true;
168 }
169