Path: blob/main/usr.sbin/bluetooth/btpand/btpand.h
107609 views
/* $NetBSD: btpand.h,v 1.1 2008/08/17 13:20:57 plunky Exp $ */12/*-3* SPDX-License-Identifier: BSD-2-Clause4*5* Copyright (c) 2008 Iain Hibbert6* All rights reserved.7*8* Redistribution and use in source and binary forms, with or without9* modification, are permitted provided that the following conditions10* are met:11* 1. Redistributions of source code must retain the above copyright12* notice, this list of conditions and the following disclaimer.13* 2. Redistributions in binary form must reproduce the above copyright14* notice, this list of conditions and the following disclaimer in the15* documentation and/or other materials provided with the distribution.16*17* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR18* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES19* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.20* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,21* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT22* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF26* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.27*/282930#include <sys/types.h>31#include <sys/queue.h>32#include <sys/socket.h>3334#include <net/if.h>35#include <net/ethernet.h>3637#include <assert.h>38#include <bluetooth.h>39#include <stdbool.h>40#include <stdlib.h>41#include <string.h>42#include <syslog.h>4344#include "event.h"4546#ifndef __arraycount47#define __arraycount(__x) (int)(sizeof((__x)) / sizeof((__x)[0]))48#endif4950#ifndef L2CAP_PSM_INVALID51#define L2CAP_PSM_INVALID(psm) (((psm) & 0x0101) != 0x0001)52#endif5354#ifndef L2CAP_PSM_BNEP55#define L2CAP_PSM_BNEP 1556#endif5758typedef struct channel channel_t;59typedef struct pfilter pfilter_t;60typedef struct mfilter mfilter_t;61typedef struct packet packet_t;62typedef struct pkthdr pkthdr_t;63typedef struct pktlist pktlist_t;64typedef struct exthdr exthdr_t;65typedef struct extlist extlist_t;6667LIST_HEAD(chlist, channel);68STAILQ_HEAD(extlist, exthdr);69STAILQ_HEAD(pktlist, pkthdr);7071enum channel_state {72CHANNEL_CLOSED,73CHANNEL_WAIT_CONNECT_REQ,74CHANNEL_WAIT_CONNECT_RSP,75CHANNEL_OPEN,76};7778#define CHANNEL_MAXQLEN 1287980/* BNEP or tap channel */81struct channel {82enum channel_state state;83bool oactive;8485uint8_t laddr[ETHER_ADDR_LEN];86uint8_t raddr[ETHER_ADDR_LEN];87size_t mru;88size_t mtu;8990int npfilter;91pfilter_t * pfilter;9293int nmfilter;94mfilter_t * mfilter;9596pktlist_t pktlist;97int qlen;9899int fd;100struct event rd_ev;101struct event wr_ev;102uint8_t * sendbuf;103104bool (*send)(channel_t *, packet_t *);105bool (*recv)(packet_t *);106107int tick;108109struct pidfh *pfh;110111int refcnt;112LIST_ENTRY(channel) next;113};114115/* network protocol type filter */116struct pfilter {117uint16_t start;118uint16_t end;119};120121/* multicast address filter */122struct mfilter {123uint8_t start[ETHER_ADDR_LEN];124uint8_t end[ETHER_ADDR_LEN];125};126127/* packet data buffer */128struct packet {129channel_t * chan; /* source channel */130uint8_t * dst; /* dest address */131uint8_t * src; /* source address */132uint8_t * type; /* protocol type */133uint8_t * ptr; /* data pointer */134size_t len; /* data length */135int refcnt; /* reference count */136extlist_t extlist;/* extension headers */137uint8_t buf[0]; /* data starts here */138};139140/* extension header */141struct exthdr {142STAILQ_ENTRY(exthdr) next;143uint8_t * ptr;144uint8_t len;145};146147/* packet header */148struct pkthdr {149STAILQ_ENTRY(pkthdr) next;150packet_t * data;151};152153/* global variables */154extern const char * control_path;155extern const char * service_name;156extern const char * interface_name;157extern bdaddr_t local_bdaddr;158extern bdaddr_t remote_bdaddr;159extern uint16_t l2cap_psm;160extern int l2cap_mode;161extern uint16_t service_class;162extern int server_limit;163164/*165* Bluetooth addresses are stored the other way around than166* Ethernet addresses even though they are of the same family167*/168static inline void169b2eaddr(void *dst, bdaddr_t *src)170{171uint8_t *d = dst;172int i;173174for (i = 0; i < ETHER_ADDR_LEN; i++)175d[i] = src->b[ETHER_ADDR_LEN - i - 1];176}177178#define log_err(fmt, args...) syslog(LOG_ERR, fmt , ##args)179#define log_info(fmt, args...) syslog(LOG_INFO, fmt , ##args)180#define log_notice(fmt, args...) syslog(LOG_NOTICE, fmt , ##args)181#define log_debug(fmt, args...) syslog(LOG_DEBUG, "%s: " fmt, __func__ , ##args)182183/* bnep.c */184bool bnep_send(channel_t *, packet_t *);185bool bnep_recv(packet_t *);186void bnep_send_control(channel_t *, unsigned, ...);187188/* channel.c */189void channel_init(void);190channel_t * channel_alloc(void);191bool channel_open(channel_t *, int);192void channel_close(channel_t *);193void channel_free(channel_t *);194void channel_timeout(channel_t *, int);195void channel_put(channel_t *, packet_t *);196197/* client.c */198void client_init(void);199200/* packet.c */201packet_t * packet_alloc(channel_t *);202void packet_free(packet_t *);203void packet_adj(packet_t *, size_t);204pkthdr_t * pkthdr_alloc(packet_t *);205void pkthdr_free(pkthdr_t *);206207/* server.c */208void server_init(void);209void server_update(int);210211/* tap.c */212void tap_init(void);213214215