1187938Semax/*	$NetBSD: btpand.h,v 1.1 2008/08/17 13:20:57 plunky Exp $	*/
2187938Semax
3187938Semax/*-
4187938Semax * Copyright (c) 2008 Iain Hibbert
5187938Semax * All rights reserved.
6187938Semax *
7187938Semax * Redistribution and use in source and binary forms, with or without
8187938Semax * modification, are permitted provided that the following conditions
9187938Semax * are met:
10187938Semax * 1. Redistributions of source code must retain the above copyright
11187938Semax *    notice, this list of conditions and the following disclaimer.
12187938Semax * 2. Redistributions in binary form must reproduce the above copyright
13187938Semax *    notice, this list of conditions and the following disclaimer in the
14187938Semax *    documentation and/or other materials provided with the distribution.
15187938Semax *
16187938Semax * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17187938Semax * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18187938Semax * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19187938Semax * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20187938Semax * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21187938Semax * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22187938Semax * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23187938Semax * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24187938Semax * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25187938Semax * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26187938Semax */
27187938Semax
28187938Semax/* $FreeBSD$ */
29187938Semax
30187938Semax#include <sys/types.h>
31187938Semax#include <sys/queue.h>
32187938Semax#include <sys/socket.h>
33187938Semax
34187938Semax#include <net/if.h>
35187938Semax#include <net/ethernet.h>
36187938Semax
37187938Semax#include <assert.h>
38187938Semax#include <bluetooth.h>
39187938Semax#include <stdbool.h>
40187938Semax#include <stdlib.h>
41187938Semax#include <string.h>
42187938Semax#include <syslog.h>
43187938Semax
44187938Semax#include "event.h"
45187938Semax
46187938Semax#ifndef __arraycount
47187938Semax#define __arraycount(__x)	(int)(sizeof((__x)) / sizeof((__x)[0]))
48187938Semax#endif
49187938Semax
50187938Semax#ifndef	L2CAP_PSM_INVALID
51187938Semax#define	L2CAP_PSM_INVALID(psm)	(((psm) & 0x0101) != 0x0001)
52187938Semax#endif
53187938Semax
54188013Semax#ifndef	L2CAP_PSM_BNEP
55188013Semax#define	L2CAP_PSM_BNEP	15
56188013Semax#endif
57188013Semax
58187938Semaxtypedef struct channel	channel_t;
59187938Semaxtypedef struct pfilter	pfilter_t;
60187938Semaxtypedef struct mfilter	mfilter_t;
61187938Semaxtypedef struct packet	packet_t;
62187938Semaxtypedef struct pkthdr	pkthdr_t;
63187938Semaxtypedef struct pktlist	pktlist_t;
64187938Semaxtypedef struct exthdr	exthdr_t;
65187938Semaxtypedef struct extlist	extlist_t;
66187938Semax
67187938SemaxLIST_HEAD(chlist, channel);
68187938SemaxSTAILQ_HEAD(extlist, exthdr);
69187938SemaxSTAILQ_HEAD(pktlist, pkthdr);
70187938Semax
71187938Semaxenum channel_state {
72187938Semax	CHANNEL_CLOSED,
73187938Semax	CHANNEL_WAIT_CONNECT_REQ,
74187938Semax	CHANNEL_WAIT_CONNECT_RSP,
75187938Semax	CHANNEL_OPEN,
76187938Semax};
77187938Semax
78187938Semax#define CHANNEL_MAXQLEN		128
79187938Semax
80187938Semax/* BNEP or tap channel */
81187938Semaxstruct channel {
82187938Semax	enum channel_state	state;
83187938Semax	bool			oactive;
84187938Semax
85187938Semax	uint8_t			laddr[ETHER_ADDR_LEN];
86187938Semax	uint8_t			raddr[ETHER_ADDR_LEN];
87187938Semax	size_t			mru;
88187938Semax	size_t			mtu;
89187938Semax
90187938Semax	int			npfilter;
91187938Semax	pfilter_t *		pfilter;
92187938Semax
93187938Semax	int			nmfilter;
94187938Semax	mfilter_t *		mfilter;
95187938Semax
96187938Semax	pktlist_t		pktlist;
97187938Semax	int			qlen;
98187938Semax
99187938Semax	int			fd;
100187938Semax	struct event		rd_ev;
101187938Semax	struct event		wr_ev;
102187938Semax	uint8_t *		sendbuf;
103187938Semax
104187938Semax	bool			(*send)(channel_t *, packet_t *);
105187938Semax	bool			(*recv)(packet_t *);
106187938Semax
107187938Semax	int			tick;
108187938Semax
109187938Semax	struct pidfh		*pfh;
110187938Semax
111187938Semax	int			refcnt;
112187938Semax	LIST_ENTRY(channel)	next;
113187938Semax};
114187938Semax
115187938Semax/* network protocol type filter */
116187938Semaxstruct pfilter {
117187938Semax	uint16_t	start;
118187938Semax	uint16_t	end;
119187938Semax};
120187938Semax
121187938Semax/* multicast address filter */
122187938Semaxstruct mfilter {
123187938Semax	uint8_t		start[ETHER_ADDR_LEN];
124187938Semax	uint8_t		end[ETHER_ADDR_LEN];
125187938Semax};
126187938Semax
127187938Semax/* packet data buffer */
128187938Semaxstruct packet {
129187938Semax	channel_t *		chan;	/* source channel */
130187938Semax	uint8_t *		dst;	/* dest address */
131187938Semax	uint8_t *		src;	/* source address */
132187938Semax	uint8_t *		type;	/* protocol type */
133187938Semax	uint8_t *		ptr;	/* data pointer */
134187938Semax	size_t			len;	/* data length */
135187938Semax	int			refcnt;	/* reference count */
136187938Semax	extlist_t		extlist;/* extension headers */
137187938Semax	uint8_t			buf[0];	/* data starts here */
138187938Semax};
139187938Semax
140187938Semax/* extension header */
141187938Semaxstruct exthdr {
142187938Semax	STAILQ_ENTRY(exthdr)	next;
143187938Semax	uint8_t *		ptr;
144187938Semax	uint8_t			len;
145187938Semax};
146187938Semax
147187938Semax/* packet header */
148187938Semaxstruct pkthdr {
149187938Semax	STAILQ_ENTRY(pkthdr)	next;
150187938Semax	packet_t *		data;
151187938Semax};
152187938Semax
153187938Semax/* global variables */
154187938Semaxextern const char *	control_path;
155187938Semaxextern const char *	service_name;
156187938Semaxextern const char *	interface_name;
157187938Semaxextern bdaddr_t		local_bdaddr;
158187938Semaxextern bdaddr_t		remote_bdaddr;
159187938Semaxextern uint16_t		l2cap_psm;
160187938Semaxextern int		l2cap_mode;
161187938Semaxextern uint16_t		service_class;
162187938Semaxextern int		server_limit;
163187938Semax
164187938Semax/*
165187938Semax * Bluetooth addresses are stored the other way around than
166187938Semax * Ethernet addresses even though they are of the same family
167187938Semax */
168187938Semaxstatic inline void
169187938Semaxb2eaddr(void *dst, bdaddr_t *src)
170187938Semax{
171187938Semax	uint8_t *d = dst;
172187938Semax	int i;
173187938Semax
174187938Semax	for (i = 0; i < ETHER_ADDR_LEN; i++)
175187938Semax		d[i] = src->b[ETHER_ADDR_LEN - i - 1];
176187938Semax}
177187938Semax
178187938Semax#define log_err(fmt, args...)		syslog(LOG_ERR, fmt , ##args)
179187938Semax#define log_info(fmt, args...)		syslog(LOG_INFO, fmt , ##args)
180187938Semax#define log_notice(fmt, args...)	syslog(LOG_NOTICE, fmt , ##args)
181187938Semax#define log_debug(fmt, args...)		syslog(LOG_DEBUG, "%s: " fmt, __func__ , ##args)
182187938Semax
183187938Semax/* bnep.c */
184187938Semaxbool		bnep_send(channel_t *, packet_t *);
185187938Semaxbool		bnep_recv(packet_t *);
186305287Sdimvoid		bnep_send_control(channel_t *, unsigned, ...);
187187938Semax
188187938Semax/* channel.c */
189187938Semaxvoid		channel_init(void);
190187938Semaxchannel_t *	channel_alloc(void);
191187938Semaxbool		channel_open(channel_t *, int);
192187938Semaxvoid		channel_close(channel_t *);
193187938Semaxvoid		channel_free(channel_t *);
194187938Semaxvoid		channel_timeout(channel_t *, int);
195187938Semaxvoid		channel_put(channel_t *, packet_t *);
196187938Semax
197187938Semax/* client.c */
198187938Semaxvoid		client_init(void);
199187938Semax
200187938Semax/* packet.c */
201187938Semaxpacket_t *	packet_alloc(channel_t *);
202187938Semaxvoid		packet_free(packet_t *);
203187938Semaxvoid		packet_adj(packet_t *, size_t);
204187938Semaxpkthdr_t *	pkthdr_alloc(packet_t *);
205187938Semaxvoid		pkthdr_free(pkthdr_t *);
206187938Semax
207187938Semax/* server.c */
208187938Semaxvoid		server_init(void);
209187938Semaxvoid		server_update(int);
210187938Semax
211187938Semax/* tap.c */
212187938Semaxvoid		tap_init(void);
213