1 1.5 joerg /* $NetBSD: btpand.h,v 1.5 2016/10/04 21:40:31 joerg Exp $ */ 2 1.1 plunky 3 1.1 plunky /*- 4 1.3 plunky * Copyright (c) 2008-2009 Iain Hibbert 5 1.1 plunky * All rights reserved. 6 1.1 plunky * 7 1.1 plunky * Redistribution and use in source and binary forms, with or without 8 1.1 plunky * modification, are permitted provided that the following conditions 9 1.1 plunky * are met: 10 1.1 plunky * 1. Redistributions of source code must retain the above copyright 11 1.1 plunky * notice, this list of conditions and the following disclaimer. 12 1.1 plunky * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 plunky * notice, this list of conditions and the following disclaimer in the 14 1.1 plunky * documentation and/or other materials provided with the distribution. 15 1.1 plunky * 16 1.1 plunky * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.1 plunky * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.1 plunky * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.1 plunky * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.1 plunky * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.1 plunky * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.1 plunky * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.1 plunky * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.1 plunky * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.1 plunky * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 plunky */ 27 1.1 plunky 28 1.1 plunky #include <sys/types.h> 29 1.1 plunky #include <sys/queue.h> 30 1.1 plunky 31 1.1 plunky #include <net/if.h> 32 1.1 plunky #include <net/if_ether.h> 33 1.1 plunky 34 1.1 plunky #include <assert.h> 35 1.2 plunky #include <bluetooth.h> 36 1.1 plunky #include <event.h> 37 1.1 plunky #include <stdbool.h> 38 1.1 plunky #include <stdlib.h> 39 1.1 plunky #include <string.h> 40 1.1 plunky #include <syslog.h> 41 1.1 plunky 42 1.1 plunky typedef struct channel channel_t; 43 1.1 plunky typedef struct pfilter pfilter_t; 44 1.1 plunky typedef struct mfilter mfilter_t; 45 1.1 plunky typedef struct packet packet_t; 46 1.1 plunky typedef struct pkthdr pkthdr_t; 47 1.1 plunky typedef struct pktlist pktlist_t; 48 1.1 plunky typedef struct exthdr exthdr_t; 49 1.1 plunky typedef struct extlist extlist_t; 50 1.1 plunky 51 1.1 plunky LIST_HEAD(chlist, channel); 52 1.1 plunky STAILQ_HEAD(extlist, exthdr); 53 1.1 plunky STAILQ_HEAD(pktlist, pkthdr); 54 1.1 plunky 55 1.1 plunky enum channel_state { 56 1.1 plunky CHANNEL_CLOSED, 57 1.1 plunky CHANNEL_WAIT_CONNECT_REQ, 58 1.1 plunky CHANNEL_WAIT_CONNECT_RSP, 59 1.4 plunky CHANNEL_OPEN 60 1.1 plunky }; 61 1.1 plunky 62 1.1 plunky #define CHANNEL_MAXQLEN 128 63 1.1 plunky 64 1.1 plunky /* BNEP or tap channel */ 65 1.1 plunky struct channel { 66 1.1 plunky enum channel_state state; 67 1.1 plunky bool oactive; 68 1.1 plunky 69 1.1 plunky uint8_t laddr[ETHER_ADDR_LEN]; 70 1.1 plunky uint8_t raddr[ETHER_ADDR_LEN]; 71 1.1 plunky size_t mru; 72 1.1 plunky size_t mtu; 73 1.1 plunky 74 1.1 plunky int npfilter; 75 1.1 plunky pfilter_t * pfilter; 76 1.1 plunky 77 1.1 plunky int nmfilter; 78 1.1 plunky mfilter_t * mfilter; 79 1.1 plunky 80 1.1 plunky pktlist_t pktlist; 81 1.1 plunky int qlen; 82 1.1 plunky 83 1.1 plunky int fd; 84 1.1 plunky struct event rd_ev; 85 1.1 plunky struct event wr_ev; 86 1.1 plunky uint8_t * sendbuf; 87 1.1 plunky 88 1.1 plunky bool (*send)(channel_t *, packet_t *); 89 1.1 plunky bool (*recv)(packet_t *); 90 1.2 plunky void (*down)(channel_t *); 91 1.1 plunky 92 1.1 plunky int tick; 93 1.1 plunky 94 1.1 plunky int refcnt; 95 1.1 plunky LIST_ENTRY(channel) next; 96 1.1 plunky }; 97 1.1 plunky 98 1.1 plunky /* network protocol type filter */ 99 1.1 plunky struct pfilter { 100 1.1 plunky uint16_t start; 101 1.1 plunky uint16_t end; 102 1.1 plunky }; 103 1.1 plunky 104 1.1 plunky /* multicast address filter */ 105 1.1 plunky struct mfilter { 106 1.1 plunky uint8_t start[ETHER_ADDR_LEN]; 107 1.1 plunky uint8_t end[ETHER_ADDR_LEN]; 108 1.1 plunky }; 109 1.1 plunky 110 1.1 plunky /* packet data buffer */ 111 1.1 plunky struct packet { 112 1.1 plunky channel_t * chan; /* source channel */ 113 1.1 plunky uint8_t * dst; /* dest address */ 114 1.1 plunky uint8_t * src; /* source address */ 115 1.1 plunky uint8_t * type; /* protocol type */ 116 1.1 plunky uint8_t * ptr; /* data pointer */ 117 1.1 plunky size_t len; /* data length */ 118 1.1 plunky int refcnt; /* reference count */ 119 1.1 plunky extlist_t extlist;/* extension headers */ 120 1.1 plunky uint8_t buf[0]; /* data starts here */ 121 1.1 plunky }; 122 1.1 plunky 123 1.1 plunky /* extension header */ 124 1.1 plunky struct exthdr { 125 1.1 plunky STAILQ_ENTRY(exthdr) next; 126 1.1 plunky uint8_t * ptr; 127 1.1 plunky uint8_t len; 128 1.1 plunky }; 129 1.1 plunky 130 1.1 plunky /* packet header */ 131 1.1 plunky struct pkthdr { 132 1.1 plunky STAILQ_ENTRY(pkthdr) next; 133 1.1 plunky packet_t * data; 134 1.1 plunky }; 135 1.1 plunky 136 1.1 plunky /* global variables */ 137 1.1 plunky extern const char * control_path; 138 1.3 plunky extern const char * service_type; 139 1.1 plunky extern const char * service_name; 140 1.3 plunky extern const char * service_desc; 141 1.1 plunky extern const char * interface_name; 142 1.1 plunky extern bdaddr_t local_bdaddr; 143 1.1 plunky extern bdaddr_t remote_bdaddr; 144 1.1 plunky extern uint16_t l2cap_psm; 145 1.1 plunky extern int l2cap_mode; 146 1.1 plunky extern uint16_t service_class; 147 1.1 plunky extern int server_limit; 148 1.1 plunky 149 1.1 plunky /* 150 1.1 plunky * Bluetooth addresses are stored the other way around than 151 1.1 plunky * Ethernet addresses even though they are of the same family 152 1.1 plunky */ 153 1.1 plunky static inline void 154 1.1 plunky b2eaddr(void *dst, bdaddr_t *src) 155 1.1 plunky { 156 1.1 plunky uint8_t *d = dst; 157 1.1 plunky int i; 158 1.1 plunky 159 1.1 plunky for (i = 0; i < ETHER_ADDR_LEN; i++) 160 1.1 plunky d[i] = src->b[ETHER_ADDR_LEN - i - 1]; 161 1.1 plunky } 162 1.1 plunky 163 1.1 plunky #define log_err(fmt, args...) syslog(LOG_ERR, fmt , ##args) 164 1.1 plunky #define log_info(fmt, args...) syslog(LOG_INFO, fmt , ##args) 165 1.1 plunky #define log_notice(fmt, args...) syslog(LOG_NOTICE, fmt , ##args) 166 1.1 plunky #define log_debug(fmt, args...) syslog(LOG_DEBUG, "%s: " fmt, __func__ , ##args) 167 1.1 plunky 168 1.1 plunky /* bnep.c */ 169 1.1 plunky bool bnep_send(channel_t *, packet_t *); 170 1.1 plunky bool bnep_recv(packet_t *); 171 1.5 joerg void bnep_send_control(channel_t *, int, ...); 172 1.1 plunky 173 1.1 plunky /* channel.c */ 174 1.1 plunky void channel_init(void); 175 1.1 plunky channel_t * channel_alloc(void); 176 1.1 plunky bool channel_open(channel_t *, int); 177 1.1 plunky void channel_close(channel_t *); 178 1.1 plunky void channel_free(channel_t *); 179 1.1 plunky void channel_timeout(channel_t *, int); 180 1.1 plunky void channel_put(channel_t *, packet_t *); 181 1.1 plunky 182 1.1 plunky /* client.c */ 183 1.1 plunky void client_init(void); 184 1.1 plunky 185 1.1 plunky /* packet.c */ 186 1.1 plunky packet_t * packet_alloc(channel_t *); 187 1.1 plunky void packet_free(packet_t *); 188 1.1 plunky void packet_adj(packet_t *, size_t); 189 1.1 plunky pkthdr_t * pkthdr_alloc(packet_t *); 190 1.1 plunky void pkthdr_free(pkthdr_t *); 191 1.1 plunky 192 1.1 plunky /* server.c */ 193 1.1 plunky void server_init(void); 194 1.1 plunky 195 1.1 plunky /* tap.c */ 196 1.1 plunky void tap_init(void); 197