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