]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - usr.sbin/bluetooth/btpand/btpand.h
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / usr.sbin / bluetooth / btpand / btpand.h
1 /*      $NetBSD: btpand.h,v 1.1 2008/08/17 13:20:57 plunky Exp $        */
2
3 /*-
4  * Copyright (c) 2008 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 /* $FreeBSD$ */
29
30 #include <sys/types.h>
31 #include <sys/queue.h>
32 #include <sys/socket.h>
33
34 #include <net/if.h>
35 #include <net/ethernet.h>
36
37 #include <assert.h>
38 #include <bluetooth.h>
39 #include <stdbool.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <syslog.h>
43
44 #include "event.h"
45
46 #ifndef __arraycount
47 #define __arraycount(__x)       (int)(sizeof((__x)) / sizeof((__x)[0]))
48 #endif
49
50 #ifndef L2CAP_PSM_INVALID
51 #define L2CAP_PSM_INVALID(psm)  (((psm) & 0x0101) != 0x0001)
52 #endif
53
54 #ifndef L2CAP_PSM_BNEP
55 #define L2CAP_PSM_BNEP  15
56 #endif
57
58 typedef struct channel  channel_t;
59 typedef struct pfilter  pfilter_t;
60 typedef struct mfilter  mfilter_t;
61 typedef struct packet   packet_t;
62 typedef struct pkthdr   pkthdr_t;
63 typedef struct pktlist  pktlist_t;
64 typedef struct exthdr   exthdr_t;
65 typedef struct extlist  extlist_t;
66
67 LIST_HEAD(chlist, channel);
68 STAILQ_HEAD(extlist, exthdr);
69 STAILQ_HEAD(pktlist, pkthdr);
70
71 enum channel_state {
72         CHANNEL_CLOSED,
73         CHANNEL_WAIT_CONNECT_REQ,
74         CHANNEL_WAIT_CONNECT_RSP,
75         CHANNEL_OPEN,
76 };
77
78 #define CHANNEL_MAXQLEN         128
79
80 /* BNEP or tap channel */
81 struct channel {
82         enum channel_state      state;
83         bool                    oactive;
84
85         uint8_t                 laddr[ETHER_ADDR_LEN];
86         uint8_t                 raddr[ETHER_ADDR_LEN];
87         size_t                  mru;
88         size_t                  mtu;
89
90         int                     npfilter;
91         pfilter_t *             pfilter;
92
93         int                     nmfilter;
94         mfilter_t *             mfilter;
95
96         pktlist_t               pktlist;
97         int                     qlen;
98
99         int                     fd;
100         struct event            rd_ev;
101         struct event            wr_ev;
102         uint8_t *               sendbuf;
103
104         bool                    (*send)(channel_t *, packet_t *);
105         bool                    (*recv)(packet_t *);
106
107         int                     tick;
108
109         struct pidfh            *pfh;
110
111         int                     refcnt;
112         LIST_ENTRY(channel)     next;
113 };
114
115 /* network protocol type filter */
116 struct pfilter {
117         uint16_t        start;
118         uint16_t        end;
119 };
120
121 /* multicast address filter */
122 struct mfilter {
123         uint8_t         start[ETHER_ADDR_LEN];
124         uint8_t         end[ETHER_ADDR_LEN];
125 };
126
127 /* packet data buffer */
128 struct packet {
129         channel_t *             chan;   /* source channel */
130         uint8_t *               dst;    /* dest address */
131         uint8_t *               src;    /* source address */
132         uint8_t *               type;   /* protocol type */
133         uint8_t *               ptr;    /* data pointer */
134         size_t                  len;    /* data length */
135         int                     refcnt; /* reference count */
136         extlist_t               extlist;/* extension headers */
137         uint8_t                 buf[0]; /* data starts here */
138 };
139
140 /* extension header */
141 struct exthdr {
142         STAILQ_ENTRY(exthdr)    next;
143         uint8_t *               ptr;
144         uint8_t                 len;
145 };
146
147 /* packet header */
148 struct pkthdr {
149         STAILQ_ENTRY(pkthdr)    next;
150         packet_t *              data;
151 };
152
153 /* global variables */
154 extern const char *     control_path;
155 extern const char *     service_name;
156 extern const char *     interface_name;
157 extern bdaddr_t         local_bdaddr;
158 extern bdaddr_t         remote_bdaddr;
159 extern uint16_t         l2cap_psm;
160 extern int              l2cap_mode;
161 extern uint16_t         service_class;
162 extern int              server_limit;
163
164 /*
165  * Bluetooth addresses are stored the other way around than
166  * Ethernet addresses even though they are of the same family
167  */
168 static inline void
169 b2eaddr(void *dst, bdaddr_t *src)
170 {
171         uint8_t *d = dst;
172         int i;
173
174         for (i = 0; i < ETHER_ADDR_LEN; i++)
175                 d[i] = src->b[ETHER_ADDR_LEN - i - 1];
176 }
177
178 #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)
182
183 /* bnep.c */
184 bool            bnep_send(channel_t *, packet_t *);
185 bool            bnep_recv(packet_t *);
186 void            bnep_send_control(channel_t *, uint8_t, ...);
187
188 /* channel.c */
189 void            channel_init(void);
190 channel_t *     channel_alloc(void);
191 bool            channel_open(channel_t *, int);
192 void            channel_close(channel_t *);
193 void            channel_free(channel_t *);
194 void            channel_timeout(channel_t *, int);
195 void            channel_put(channel_t *, packet_t *);
196
197 /* client.c */
198 void            client_init(void);
199
200 /* packet.c */
201 packet_t *      packet_alloc(channel_t *);
202 void            packet_free(packet_t *);
203 void            packet_adj(packet_t *, size_t);
204 pkthdr_t *      pkthdr_alloc(packet_t *);
205 void            pkthdr_free(pkthdr_t *);
206
207 /* server.c */
208 void            server_init(void);
209 void            server_update(int);
210
211 /* tap.c */
212 void            tap_init(void);