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 60typedef struct channel channel_t; 61typedef struct pfilter pfilter_t; 62typedef struct mfilter mfilter_t; 63typedef struct packet packet_t; 64typedef struct pkthdr pkthdr_t; 65typedef struct pktlist pktlist_t; 66typedef struct exthdr exthdr_t; 67typedef struct extlist extlist_t; 68 69LIST_HEAD(chlist, channel); 70STAILQ_HEAD(extlist, exthdr); 71STAILQ_HEAD(pktlist, pkthdr); 72 73enum 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 */ 83struct 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 */ 118struct pfilter { 119 uint16_t start; 120 uint16_t end; 121}; 122 123/* multicast address filter */ 124struct mfilter { 125 uint8_t start[ETHER_ADDR_LEN]; 126 uint8_t end[ETHER_ADDR_LEN]; 127}; 128 129/* packet data buffer */ 130struct 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 */ 143struct exthdr { 144 STAILQ_ENTRY(exthdr) next; 145 uint8_t * ptr; 146 uint8_t len; 147}; 148 149/* packet header */ 150struct pkthdr { 151 STAILQ_ENTRY(pkthdr) next; 152 packet_t * data; 153}; 154 155/* global variables */ 156extern const char * control_path; 157extern const char * service_name; 158extern const char * interface_name; 159extern bdaddr_t local_bdaddr; 160extern bdaddr_t remote_bdaddr; 161extern uint16_t l2cap_psm; 162extern int l2cap_mode; 163extern uint16_t service_class; 164extern 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 */ 170static inline void 171b2eaddr(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 */ 186bool bnep_send(channel_t *, packet_t *); 187bool bnep_recv(packet_t *); 188void bnep_send_control(channel_t *, unsigned, ...); 189 190/* channel.c */ 191void channel_init(void); 192channel_t * channel_alloc(void); 193bool channel_open(channel_t *, int); 194void channel_close(channel_t *); 195void channel_free(channel_t *); 196void channel_timeout(channel_t *, int); 197void channel_put(channel_t *, packet_t *); 198 199/* client.c */ 200void client_init(void); 201 202/* packet.c */ 203packet_t * packet_alloc(channel_t *); 204void packet_free(packet_t *); 205void packet_adj(packet_t *, size_t); 206pkthdr_t * pkthdr_alloc(packet_t *); 207void pkthdr_free(pkthdr_t *); 208 209/* server.c */ 210void server_init(void); 211void server_update(int); 212 213/* tap.c */ 214void tap_init(void); 215