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