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