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