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