1 /* $NetBSD: btpand.h,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/types.h> 32 #include <sys/queue.h> 33 #include <sys/socket.h> 34 35 #include <net/if.h> 36 #include <net/ethernet.h> 37 38 #include <assert.h> 39 #include <bluetooth.h> 40 #include <stdbool.h> 41 #include <stdlib.h> 42 #include <string.h> 43 #include <syslog.h> 44 45 #include "event.h" 46 47 #ifndef __arraycount 48 #define __arraycount(__x) (int)(sizeof((__x)) / sizeof((__x)[0])) 49 #endif 50 51 #ifndef L2CAP_PSM_INVALID 52 #define L2CAP_PSM_INVALID(psm) (((psm) & 0x0101) != 0x0001) 53 #endif 54 55 #ifndef L2CAP_PSM_BNEP 56 #define L2CAP_PSM_BNEP 15 57 #endif 58 59 typedef struct channel channel_t; 60 typedef struct pfilter pfilter_t; 61 typedef struct mfilter mfilter_t; 62 typedef struct packet packet_t; 63 typedef struct pkthdr pkthdr_t; 64 typedef struct pktlist pktlist_t; 65 typedef struct exthdr exthdr_t; 66 typedef struct extlist extlist_t; 67 68 LIST_HEAD(chlist, channel); 69 STAILQ_HEAD(extlist, exthdr); 70 STAILQ_HEAD(pktlist, pkthdr); 71 72 enum channel_state { 73 CHANNEL_CLOSED, 74 CHANNEL_WAIT_CONNECT_REQ, 75 CHANNEL_WAIT_CONNECT_RSP, 76 CHANNEL_OPEN, 77 }; 78 79 #define CHANNEL_MAXQLEN 128 80 81 /* BNEP or tap channel */ 82 struct channel { 83 enum channel_state state; 84 bool oactive; 85 86 uint8_t laddr[ETHER_ADDR_LEN]; 87 uint8_t raddr[ETHER_ADDR_LEN]; 88 size_t mru; 89 size_t mtu; 90 91 int npfilter; 92 pfilter_t * pfilter; 93 94 int nmfilter; 95 mfilter_t * mfilter; 96 97 pktlist_t pktlist; 98 int qlen; 99 100 int fd; 101 struct event rd_ev; 102 struct event wr_ev; 103 uint8_t * sendbuf; 104 105 bool (*send)(channel_t *, packet_t *); 106 bool (*recv)(packet_t *); 107 108 int tick; 109 110 struct pidfh *pfh; 111 112 int refcnt; 113 LIST_ENTRY(channel) next; 114 }; 115 116 /* network protocol type filter */ 117 struct pfilter { 118 uint16_t start; 119 uint16_t end; 120 }; 121 122 /* multicast address filter */ 123 struct mfilter { 124 uint8_t start[ETHER_ADDR_LEN]; 125 uint8_t end[ETHER_ADDR_LEN]; 126 }; 127 128 /* packet data buffer */ 129 struct packet { 130 channel_t * chan; /* source channel */ 131 uint8_t * dst; /* dest address */ 132 uint8_t * src; /* source address */ 133 uint8_t * type; /* protocol type */ 134 uint8_t * ptr; /* data pointer */ 135 size_t len; /* data length */ 136 int refcnt; /* reference count */ 137 extlist_t extlist;/* extension headers */ 138 uint8_t buf[0]; /* data starts here */ 139 }; 140 141 /* extension header */ 142 struct exthdr { 143 STAILQ_ENTRY(exthdr) next; 144 uint8_t * ptr; 145 uint8_t len; 146 }; 147 148 /* packet header */ 149 struct pkthdr { 150 STAILQ_ENTRY(pkthdr) next; 151 packet_t * data; 152 }; 153 154 /* global variables */ 155 extern const char * control_path; 156 extern const char * service_name; 157 extern const char * interface_name; 158 extern bdaddr_t local_bdaddr; 159 extern bdaddr_t remote_bdaddr; 160 extern uint16_t l2cap_psm; 161 extern int l2cap_mode; 162 extern uint16_t service_class; 163 extern int server_limit; 164 165 /* 166 * Bluetooth addresses are stored the other way around than 167 * Ethernet addresses even though they are of the same family 168 */ 169 static inline void 170 b2eaddr(void *dst, bdaddr_t *src) 171 { 172 uint8_t *d = dst; 173 int i; 174 175 for (i = 0; i < ETHER_ADDR_LEN; i++) 176 d[i] = src->b[ETHER_ADDR_LEN - i - 1]; 177 } 178 179 #define log_err(fmt, args...) syslog(LOG_ERR, fmt , ##args) 180 #define log_info(fmt, args...) syslog(LOG_INFO, fmt , ##args) 181 #define log_notice(fmt, args...) syslog(LOG_NOTICE, fmt , ##args) 182 #define log_debug(fmt, args...) syslog(LOG_DEBUG, "%s: " fmt, __func__ , ##args) 183 184 /* bnep.c */ 185 bool bnep_send(channel_t *, packet_t *); 186 bool bnep_recv(packet_t *); 187 void bnep_send_control(channel_t *, unsigned, ...); 188 189 /* channel.c */ 190 void channel_init(void); 191 channel_t * channel_alloc(void); 192 bool channel_open(channel_t *, int); 193 void channel_close(channel_t *); 194 void channel_free(channel_t *); 195 void channel_timeout(channel_t *, int); 196 void channel_put(channel_t *, packet_t *); 197 198 /* client.c */ 199 void client_init(void); 200 201 /* packet.c */ 202 packet_t * packet_alloc(channel_t *); 203 void packet_free(packet_t *); 204 void packet_adj(packet_t *, size_t); 205 pkthdr_t * pkthdr_alloc(packet_t *); 206 void pkthdr_free(pkthdr_t *); 207 208 /* server.c */ 209 void server_init(void); 210 void server_update(int); 211 212 /* tap.c */ 213 void tap_init(void); 214