aboutsummaryrefslogtreecommitdiffstats
path: root/net/bluetooth/bnep
diff options
context:
space:
mode:
authorLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
committerLinus Torvalds <torvalds@ppc970.osdl.org>2005-04-16 18:20:36 -0400
commit1da177e4c3f41524e886b7f1b8a0c1fc7321cac2 (patch)
tree0bba044c4ce775e45a88a51686b5d9f90697ea9d /net/bluetooth/bnep
Linux-2.6.12-rc2v2.6.12-rc2
Initial git repository build. I'm not bothering with the full history, even though we have it. We can create a separate "historical" git archive of that later if we want to, and in the meantime it's about 3.2GB when imported into git - space that would just make the early git days unnecessarily complicated, when we don't have a lot of good infrastructure for it. Let it rip!
Diffstat (limited to 'net/bluetooth/bnep')
-rw-r--r--net/bluetooth/bnep/Kconfig24
-rw-r--r--net/bluetooth/bnep/Makefile7
-rw-r--r--net/bluetooth/bnep/bnep.h184
-rw-r--r--net/bluetooth/bnep/core.c713
-rw-r--r--net/bluetooth/bnep/netdev.c247
-rw-r--r--net/bluetooth/bnep/sock.c237
6 files changed, 1412 insertions, 0 deletions
diff --git a/net/bluetooth/bnep/Kconfig b/net/bluetooth/bnep/Kconfig
new file mode 100644
index 000000000000..35158b036d54
--- /dev/null
+++ b/net/bluetooth/bnep/Kconfig
@@ -0,0 +1,24 @@
1config BT_BNEP
2 tristate "BNEP protocol support"
3 depends on BT && BT_L2CAP
4 select CRC32
5 help
6 BNEP (Bluetooth Network Encapsulation Protocol) is Ethernet
7 emulation layer on top of Bluetooth. BNEP is required for
8 Bluetooth PAN (Personal Area Network).
9
10 Say Y here to compile BNEP support into the kernel or say M to
11 compile it as module (bnep).
12
13config BT_BNEP_MC_FILTER
14 bool "Multicast filter support"
15 depends on BT_BNEP
16 help
17 This option enables the multicast filter support for BNEP.
18
19config BT_BNEP_PROTO_FILTER
20 bool "Protocol filter support"
21 depends on BT_BNEP
22 help
23 This option enables the protocol filter support for BNEP.
24
diff --git a/net/bluetooth/bnep/Makefile b/net/bluetooth/bnep/Makefile
new file mode 100644
index 000000000000..c7821e76ca56
--- /dev/null
+++ b/net/bluetooth/bnep/Makefile
@@ -0,0 +1,7 @@
1#
2# Makefile for the Linux Bluetooth BNEP layer.
3#
4
5obj-$(CONFIG_BT_BNEP) += bnep.o
6
7bnep-objs := core.o sock.o netdev.o
diff --git a/net/bluetooth/bnep/bnep.h b/net/bluetooth/bnep/bnep.h
new file mode 100644
index 000000000000..bbb1ed7097a9
--- /dev/null
+++ b/net/bluetooth/bnep/bnep.h
@@ -0,0 +1,184 @@
1/*
2 BNEP protocol definition for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License, version 2, as
7 published by the Free Software Foundation.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*/
18
19/*
20 * $Id: bnep.h,v 1.5 2002/08/04 21:23:58 maxk Exp $
21 */
22
23#ifndef _BNEP_H
24#define _BNEP_H
25
26#include <linux/types.h>
27#include <linux/crc32.h>
28#include <net/bluetooth/bluetooth.h>
29
30// Limits
31#define BNEP_MAX_PROTO_FILTERS 5
32#define BNEP_MAX_MULTICAST_FILTERS 20
33
34// UUIDs
35#define BNEP_BASE_UUID 0x0000000000001000800000805F9B34FB
36#define BNEP_UUID16 0x02
37#define BNEP_UUID32 0x04
38#define BNEP_UUID128 0x16
39
40#define BNEP_SVC_PANU 0x1115
41#define BNEP_SVC_NAP 0x1116
42#define BNEP_SVC_GN 0x1117
43
44// Packet types
45#define BNEP_GENERAL 0x00
46#define BNEP_CONTROL 0x01
47#define BNEP_COMPRESSED 0x02
48#define BNEP_COMPRESSED_SRC_ONLY 0x03
49#define BNEP_COMPRESSED_DST_ONLY 0x04
50
51// Control types
52#define BNEP_CMD_NOT_UNDERSTOOD 0x00
53#define BNEP_SETUP_CONN_REQ 0x01
54#define BNEP_SETUP_CONN_RSP 0x02
55#define BNEP_FILTER_NET_TYPE_SET 0x03
56#define BNEP_FILTER_NET_TYPE_RSP 0x04
57#define BNEP_FILTER_MULTI_ADDR_SET 0x05
58#define BNEP_FILTER_MULTI_ADDR_RSP 0x06
59
60// Extension types
61#define BNEP_EXT_CONTROL 0x00
62
63// Response messages
64#define BNEP_SUCCESS 0x00
65
66#define BNEP_CONN_INVALID_DST 0x01
67#define BNEP_CONN_INVALID_SRC 0x02
68#define BNEP_CONN_INVALID_SVC 0x03
69#define BNEP_CONN_NOT_ALLOWED 0x04
70
71#define BNEP_FILTER_UNSUPPORTED_REQ 0x01
72#define BNEP_FILTER_INVALID_RANGE 0x02
73#define BNEP_FILTER_INVALID_MCADDR 0x02
74#define BNEP_FILTER_LIMIT_REACHED 0x03
75#define BNEP_FILTER_DENIED_SECURITY 0x04
76
77// L2CAP settings
78#define BNEP_MTU 1691
79#define BNEP_PSM 0x0f
80#define BNEP_FLUSH_TO 0xffff
81#define BNEP_CONNECT_TO 15
82#define BNEP_FILTER_TO 15
83
84// Headers
85#define BNEP_TYPE_MASK 0x7f
86#define BNEP_EXT_HEADER 0x80
87
88struct bnep_setup_conn_req {
89 __u8 type;
90 __u8 ctrl;
91 __u8 uuid_size;
92 __u8 service[0];
93} __attribute__((packed));
94
95struct bnep_set_filter_req {
96 __u8 type;
97 __u8 ctrl;
98 __u16 len;
99 __u8 list[0];
100} __attribute__((packed));
101
102struct bnep_control_rsp {
103 __u8 type;
104 __u8 ctrl;
105 __u16 resp;
106} __attribute__((packed));
107
108struct bnep_ext_hdr {
109 __u8 type;
110 __u8 len;
111 __u8 data[0];
112} __attribute__((packed));
113
114/* BNEP ioctl defines */
115#define BNEPCONNADD _IOW('B', 200, int)
116#define BNEPCONNDEL _IOW('B', 201, int)
117#define BNEPGETCONNLIST _IOR('B', 210, int)
118#define BNEPGETCONNINFO _IOR('B', 211, int)
119
120struct bnep_connadd_req {
121 int sock; // Connected socket
122 __u32 flags;
123 __u16 role;
124 char device[16]; // Name of the Ethernet device
125};
126
127struct bnep_conndel_req {
128 __u32 flags;
129 __u8 dst[ETH_ALEN];
130};
131
132struct bnep_conninfo {
133 __u32 flags;
134 __u16 role;
135 __u16 state;
136 __u8 dst[ETH_ALEN];
137 char device[16];
138};
139
140struct bnep_connlist_req {
141 __u32 cnum;
142 struct bnep_conninfo __user *ci;
143};
144
145struct bnep_proto_filter {
146 __u16 start;
147 __u16 end;
148};
149
150int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock);
151int bnep_del_connection(struct bnep_conndel_req *req);
152int bnep_get_connlist(struct bnep_connlist_req *req);
153int bnep_get_conninfo(struct bnep_conninfo *ci);
154
155// BNEP sessions
156struct bnep_session {
157 struct list_head list;
158
159 unsigned int role;
160 unsigned long state;
161 unsigned long flags;
162 atomic_t killed;
163
164 struct ethhdr eh;
165 struct msghdr msg;
166
167 struct bnep_proto_filter proto_filter[BNEP_MAX_PROTO_FILTERS];
168 u64 mc_filter;
169
170 struct socket *sock;
171 struct net_device *dev;
172 struct net_device_stats stats;
173};
174
175void bnep_net_setup(struct net_device *dev);
176int bnep_sock_init(void);
177int bnep_sock_cleanup(void);
178
179static inline int bnep_mc_hash(__u8 *addr)
180{
181 return (crc32_be(~0, addr, ETH_ALEN) >> 26);
182}
183
184#endif
diff --git a/net/bluetooth/bnep/core.c b/net/bluetooth/bnep/core.c
new file mode 100644
index 000000000000..682bf20af52d
--- /dev/null
+++ b/net/bluetooth/bnep/core.c
@@ -0,0 +1,713 @@
1/*
2 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
4 Written 2001-2002 by
5 Cl�ment Moreau <clement.moreau@inventel.fr>
6 David Libault <david.libault@inventel.fr>
7
8 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation;
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
26*/
27
28/*
29 * $Id: core.c,v 1.20 2002/08/04 21:23:58 maxk Exp $
30 */
31
32#include <linux/config.h>
33#include <linux/module.h>
34
35#include <linux/kernel.h>
36#include <linux/sched.h>
37#include <linux/signal.h>
38#include <linux/init.h>
39#include <linux/wait.h>
40#include <linux/errno.h>
41#include <linux/smp_lock.h>
42#include <linux/net.h>
43#include <net/sock.h>
44
45#include <linux/socket.h>
46#include <linux/file.h>
47
48#include <linux/netdevice.h>
49#include <linux/etherdevice.h>
50#include <linux/skbuff.h>
51
52#include <asm/unaligned.h>
53
54#include <net/bluetooth/bluetooth.h>
55#include <net/bluetooth/l2cap.h>
56
57#include "bnep.h"
58
59#ifndef CONFIG_BT_BNEP_DEBUG
60#undef BT_DBG
61#define BT_DBG(D...)
62#endif
63
64#define VERSION "1.2"
65
66static LIST_HEAD(bnep_session_list);
67static DECLARE_RWSEM(bnep_session_sem);
68
69static struct bnep_session *__bnep_get_session(u8 *dst)
70{
71 struct bnep_session *s;
72 struct list_head *p;
73
74 BT_DBG("");
75
76 list_for_each(p, &bnep_session_list) {
77 s = list_entry(p, struct bnep_session, list);
78 if (!memcmp(dst, s->eh.h_source, ETH_ALEN))
79 return s;
80 }
81 return NULL;
82}
83
84static void __bnep_link_session(struct bnep_session *s)
85{
86 /* It's safe to call __module_get() here because sessions are added
87 by the socket layer which has to hold the refference to this module.
88 */
89 __module_get(THIS_MODULE);
90 list_add(&s->list, &bnep_session_list);
91}
92
93static void __bnep_unlink_session(struct bnep_session *s)
94{
95 list_del(&s->list);
96 module_put(THIS_MODULE);
97}
98
99static int bnep_send(struct bnep_session *s, void *data, size_t len)
100{
101 struct socket *sock = s->sock;
102 struct kvec iv = { data, len };
103
104 return kernel_sendmsg(sock, &s->msg, &iv, 1, len);
105}
106
107static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp)
108{
109 struct bnep_control_rsp rsp;
110 rsp.type = BNEP_CONTROL;
111 rsp.ctrl = ctrl;
112 rsp.resp = htons(resp);
113 return bnep_send(s, &rsp, sizeof(rsp));
114}
115
116#ifdef CONFIG_BT_BNEP_PROTO_FILTER
117static inline void bnep_set_default_proto_filter(struct bnep_session *s)
118{
119 /* (IPv4, ARP) */
120 s->proto_filter[0].start = htons(0x0800);
121 s->proto_filter[0].end = htons(0x0806);
122 /* (RARP, AppleTalk) */
123 s->proto_filter[1].start = htons(0x8035);
124 s->proto_filter[1].end = htons(0x80F3);
125 /* (IPX, IPv6) */
126 s->proto_filter[2].start = htons(0x8137);
127 s->proto_filter[2].end = htons(0x86DD);
128}
129#endif
130
131static int bnep_ctrl_set_netfilter(struct bnep_session *s, u16 *data, int len)
132{
133 int n;
134
135 if (len < 2)
136 return -EILSEQ;
137
138 n = ntohs(get_unaligned(data));
139 data++; len -= 2;
140
141 if (len < n)
142 return -EILSEQ;
143
144 BT_DBG("filter len %d", n);
145
146#ifdef CONFIG_BT_BNEP_PROTO_FILTER
147 n /= 4;
148 if (n <= BNEP_MAX_PROTO_FILTERS) {
149 struct bnep_proto_filter *f = s->proto_filter;
150 int i;
151
152 for (i = 0; i < n; i++) {
153 f[i].start = get_unaligned(data++);
154 f[i].end = get_unaligned(data++);
155
156 BT_DBG("proto filter start %d end %d",
157 f[i].start, f[i].end);
158 }
159
160 if (i < BNEP_MAX_PROTO_FILTERS)
161 memset(f + i, 0, sizeof(*f));
162
163 if (n == 0)
164 bnep_set_default_proto_filter(s);
165
166 bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_SUCCESS);
167 } else {
168 bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_LIMIT_REACHED);
169 }
170#else
171 bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
172#endif
173 return 0;
174}
175
176static int bnep_ctrl_set_mcfilter(struct bnep_session *s, u8 *data, int len)
177{
178 int n;
179
180 if (len < 2)
181 return -EILSEQ;
182
183 n = ntohs(get_unaligned((u16 *) data));
184 data += 2; len -= 2;
185
186 if (len < n)
187 return -EILSEQ;
188
189 BT_DBG("filter len %d", n);
190
191#ifdef CONFIG_BT_BNEP_MC_FILTER
192 n /= (ETH_ALEN * 2);
193
194 if (n > 0) {
195 s->mc_filter = 0;
196
197 /* Always send broadcast */
198 set_bit(bnep_mc_hash(s->dev->broadcast), (ulong *) &s->mc_filter);
199
200 /* Add address ranges to the multicast hash */
201 for (; n > 0; n--) {
202 u8 a1[6], *a2;
203
204 memcpy(a1, data, ETH_ALEN); data += ETH_ALEN;
205 a2 = data; data += ETH_ALEN;
206
207 BT_DBG("mc filter %s -> %s",
208 batostr((void *) a1), batostr((void *) a2));
209
210 #define INCA(a) { int i = 5; while (i >=0 && ++a[i--] == 0); }
211
212 /* Iterate from a1 to a2 */
213 set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
214 while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) {
215 INCA(a1);
216 set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
217 }
218 }
219 }
220
221 BT_DBG("mc filter hash 0x%llx", s->mc_filter);
222
223 bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS);
224#else
225 bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
226#endif
227 return 0;
228}
229
230static int bnep_rx_control(struct bnep_session *s, void *data, int len)
231{
232 u8 cmd = *(u8 *)data;
233 int err = 0;
234
235 data++; len--;
236
237 switch (cmd) {
238 case BNEP_CMD_NOT_UNDERSTOOD:
239 case BNEP_SETUP_CONN_REQ:
240 case BNEP_SETUP_CONN_RSP:
241 case BNEP_FILTER_NET_TYPE_RSP:
242 case BNEP_FILTER_MULTI_ADDR_RSP:
243 /* Ignore these for now */
244 break;
245
246 case BNEP_FILTER_NET_TYPE_SET:
247 err = bnep_ctrl_set_netfilter(s, data, len);
248 break;
249
250 case BNEP_FILTER_MULTI_ADDR_SET:
251 err = bnep_ctrl_set_mcfilter(s, data, len);
252 break;
253
254 default: {
255 u8 pkt[3];
256 pkt[0] = BNEP_CONTROL;
257 pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
258 pkt[2] = cmd;
259 bnep_send(s, pkt, sizeof(pkt));
260 }
261 break;
262 }
263
264 return err;
265}
266
267static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb)
268{
269 struct bnep_ext_hdr *h;
270 int err = 0;
271
272 do {
273 h = (void *) skb->data;
274 if (!skb_pull(skb, sizeof(*h))) {
275 err = -EILSEQ;
276 break;
277 }
278
279 BT_DBG("type 0x%x len %d", h->type, h->len);
280
281 switch (h->type & BNEP_TYPE_MASK) {
282 case BNEP_EXT_CONTROL:
283 bnep_rx_control(s, skb->data, skb->len);
284 break;
285
286 default:
287 /* Unknown extension, skip it. */
288 break;
289 }
290
291 if (!skb_pull(skb, h->len)) {
292 err = -EILSEQ;
293 break;
294 }
295 } while (!err && (h->type & BNEP_EXT_HEADER));
296
297 return err;
298}
299
300static u8 __bnep_rx_hlen[] = {
301 ETH_HLEN, /* BNEP_GENERAL */
302 0, /* BNEP_CONTROL */
303 2, /* BNEP_COMPRESSED */
304 ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */
305 ETH_ALEN + 2 /* BNEP_COMPRESSED_DST_ONLY */
306};
307#define BNEP_RX_TYPES (sizeof(__bnep_rx_hlen) - 1)
308
309static inline int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb)
310{
311 struct net_device *dev = s->dev;
312 struct sk_buff *nskb;
313 u8 type;
314
315 dev->last_rx = jiffies;
316 s->stats.rx_bytes += skb->len;
317
318 type = *(u8 *) skb->data; skb_pull(skb, 1);
319
320 if ((type & BNEP_TYPE_MASK) > BNEP_RX_TYPES)
321 goto badframe;
322
323 if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) {
324 bnep_rx_control(s, skb->data, skb->len);
325 kfree_skb(skb);
326 return 0;
327 }
328
329 skb->mac.raw = skb->data;
330
331 /* Verify and pull out header */
332 if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK]))
333 goto badframe;
334
335 s->eh.h_proto = get_unaligned((u16 *) (skb->data - 2));
336
337 if (type & BNEP_EXT_HEADER) {
338 if (bnep_rx_extension(s, skb) < 0)
339 goto badframe;
340 }
341
342 /* Strip 802.1p header */
343 if (ntohs(s->eh.h_proto) == 0x8100) {
344 if (!skb_pull(skb, 4))
345 goto badframe;
346 s->eh.h_proto = get_unaligned((u16 *) (skb->data - 2));
347 }
348
349 /* We have to alloc new skb and copy data here :(. Because original skb
350 * may not be modified and because of the alignment requirements. */
351 nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL);
352 if (!nskb) {
353 s->stats.rx_dropped++;
354 kfree_skb(skb);
355 return -ENOMEM;
356 }
357 skb_reserve(nskb, 2);
358
359 /* Decompress header and construct ether frame */
360 switch (type & BNEP_TYPE_MASK) {
361 case BNEP_COMPRESSED:
362 memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN);
363 break;
364
365 case BNEP_COMPRESSED_SRC_ONLY:
366 memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN);
367 memcpy(__skb_put(nskb, ETH_ALEN), skb->mac.raw, ETH_ALEN);
368 put_unaligned(s->eh.h_proto, (u16 *) __skb_put(nskb, 2));
369 break;
370
371 case BNEP_COMPRESSED_DST_ONLY:
372 memcpy(__skb_put(nskb, ETH_ALEN), skb->mac.raw, ETH_ALEN);
373 memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source, ETH_ALEN + 2);
374 break;
375
376 case BNEP_GENERAL:
377 memcpy(__skb_put(nskb, ETH_ALEN * 2), skb->mac.raw, ETH_ALEN * 2);
378 put_unaligned(s->eh.h_proto, (u16 *) __skb_put(nskb, 2));
379 break;
380 }
381
382 memcpy(__skb_put(nskb, skb->len), skb->data, skb->len);
383 kfree_skb(skb);
384
385 s->stats.rx_packets++;
386 nskb->dev = dev;
387 nskb->ip_summed = CHECKSUM_NONE;
388 nskb->protocol = eth_type_trans(nskb, dev);
389 netif_rx_ni(nskb);
390 return 0;
391
392badframe:
393 s->stats.rx_errors++;
394 kfree_skb(skb);
395 return 0;
396}
397
398static u8 __bnep_tx_types[] = {
399 BNEP_GENERAL,
400 BNEP_COMPRESSED_SRC_ONLY,
401 BNEP_COMPRESSED_DST_ONLY,
402 BNEP_COMPRESSED
403};
404
405static inline int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb)
406{
407 struct ethhdr *eh = (void *) skb->data;
408 struct socket *sock = s->sock;
409 struct kvec iv[3];
410 int len = 0, il = 0;
411 u8 type = 0;
412
413 BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type);
414
415 if (!skb->dev) {
416 /* Control frame sent by us */
417 goto send;
418 }
419
420 iv[il++] = (struct kvec) { &type, 1 };
421 len++;
422
423 if (!memcmp(eh->h_dest, s->eh.h_source, ETH_ALEN))
424 type |= 0x01;
425
426 if (!memcmp(eh->h_source, s->eh.h_dest, ETH_ALEN))
427 type |= 0x02;
428
429 if (type)
430 skb_pull(skb, ETH_ALEN * 2);
431
432 type = __bnep_tx_types[type];
433 switch (type) {
434 case BNEP_COMPRESSED_SRC_ONLY:
435 iv[il++] = (struct kvec) { eh->h_source, ETH_ALEN };
436 len += ETH_ALEN;
437 break;
438
439 case BNEP_COMPRESSED_DST_ONLY:
440 iv[il++] = (struct kvec) { eh->h_dest, ETH_ALEN };
441 len += ETH_ALEN;
442 break;
443 }
444
445send:
446 iv[il++] = (struct kvec) { skb->data, skb->len };
447 len += skb->len;
448
449 /* FIXME: linearize skb */
450 {
451 len = kernel_sendmsg(sock, &s->msg, iv, il, len);
452 }
453 kfree_skb(skb);
454
455 if (len > 0) {
456 s->stats.tx_bytes += len;
457 s->stats.tx_packets++;
458 return 0;
459 }
460
461 return len;
462}
463
464static int bnep_session(void *arg)
465{
466 struct bnep_session *s = arg;
467 struct net_device *dev = s->dev;
468 struct sock *sk = s->sock->sk;
469 struct sk_buff *skb;
470 wait_queue_t wait;
471
472 BT_DBG("");
473
474 daemonize("kbnepd %s", dev->name);
475 set_user_nice(current, -15);
476 current->flags |= PF_NOFREEZE;
477
478 init_waitqueue_entry(&wait, current);
479 add_wait_queue(sk->sk_sleep, &wait);
480 while (!atomic_read(&s->killed)) {
481 set_current_state(TASK_INTERRUPTIBLE);
482
483 // RX
484 while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
485 skb_orphan(skb);
486 bnep_rx_frame(s, skb);
487 }
488
489 if (sk->sk_state != BT_CONNECTED)
490 break;
491
492 // TX
493 while ((skb = skb_dequeue(&sk->sk_write_queue)))
494 if (bnep_tx_frame(s, skb))
495 break;
496 netif_wake_queue(dev);
497
498 schedule();
499 }
500 set_current_state(TASK_RUNNING);
501 remove_wait_queue(sk->sk_sleep, &wait);
502
503 /* Cleanup session */
504 down_write(&bnep_session_sem);
505
506 /* Delete network device */
507 unregister_netdev(dev);
508
509 /* Release the socket */
510 fput(s->sock->file);
511
512 __bnep_unlink_session(s);
513
514 up_write(&bnep_session_sem);
515 free_netdev(dev);
516 return 0;
517}
518
519int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
520{
521 struct net_device *dev;
522 struct bnep_session *s, *ss;
523 u8 dst[ETH_ALEN], src[ETH_ALEN];
524 int err;
525
526 BT_DBG("");
527
528 baswap((void *) dst, &bt_sk(sock->sk)->dst);
529 baswap((void *) src, &bt_sk(sock->sk)->src);
530
531 /* session struct allocated as private part of net_device */
532 dev = alloc_netdev(sizeof(struct bnep_session),
533 (*req->device) ? req->device : "bnep%d",
534 bnep_net_setup);
535 if (!dev)
536 return ENOMEM;
537
538
539 down_write(&bnep_session_sem);
540
541 ss = __bnep_get_session(dst);
542 if (ss && ss->state == BT_CONNECTED) {
543 err = -EEXIST;
544 goto failed;
545 }
546
547 s = dev->priv;
548
549 /* This is rx header therefore addresses are swapped.
550 * ie eh.h_dest is our local address. */
551 memcpy(s->eh.h_dest, &src, ETH_ALEN);
552 memcpy(s->eh.h_source, &dst, ETH_ALEN);
553 memcpy(dev->dev_addr, s->eh.h_dest, ETH_ALEN);
554
555 s->dev = dev;
556 s->sock = sock;
557 s->role = req->role;
558 s->state = BT_CONNECTED;
559
560 s->msg.msg_flags = MSG_NOSIGNAL;
561
562#ifdef CONFIG_BT_BNEP_MC_FILTER
563 /* Set default mc filter */
564 set_bit(bnep_mc_hash(dev->broadcast), (ulong *) &s->mc_filter);
565#endif
566
567#ifdef CONFIG_BT_BNEP_PROTO_FILTER
568 /* Set default protocol filter */
569 bnep_set_default_proto_filter(s);
570#endif
571
572 err = register_netdev(dev);
573 if (err) {
574 goto failed;
575 }
576
577 __bnep_link_session(s);
578
579 err = kernel_thread(bnep_session, s, CLONE_KERNEL);
580 if (err < 0) {
581 /* Session thread start failed, gotta cleanup. */
582 unregister_netdev(dev);
583 __bnep_unlink_session(s);
584 goto failed;
585 }
586
587 up_write(&bnep_session_sem);
588 strcpy(req->device, dev->name);
589 return 0;
590
591failed:
592 up_write(&bnep_session_sem);
593 free_netdev(dev);
594 return err;
595}
596
597int bnep_del_connection(struct bnep_conndel_req *req)
598{
599 struct bnep_session *s;
600 int err = 0;
601
602 BT_DBG("");
603
604 down_read(&bnep_session_sem);
605
606 s = __bnep_get_session(req->dst);
607 if (s) {
608 /* Wakeup user-space which is polling for socket errors.
609 * This is temporary hack untill we have shutdown in L2CAP */
610 s->sock->sk->sk_err = EUNATCH;
611
612 /* Kill session thread */
613 atomic_inc(&s->killed);
614 wake_up_interruptible(s->sock->sk->sk_sleep);
615 } else
616 err = -ENOENT;
617
618 up_read(&bnep_session_sem);
619 return err;
620}
621
622static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s)
623{
624 memcpy(ci->dst, s->eh.h_source, ETH_ALEN);
625 strcpy(ci->device, s->dev->name);
626 ci->flags = s->flags;
627 ci->state = s->state;
628 ci->role = s->role;
629}
630
631int bnep_get_connlist(struct bnep_connlist_req *req)
632{
633 struct list_head *p;
634 int err = 0, n = 0;
635
636 down_read(&bnep_session_sem);
637
638 list_for_each(p, &bnep_session_list) {
639 struct bnep_session *s;
640 struct bnep_conninfo ci;
641
642 s = list_entry(p, struct bnep_session, list);
643
644 __bnep_copy_ci(&ci, s);
645
646 if (copy_to_user(req->ci, &ci, sizeof(ci))) {
647 err = -EFAULT;
648 break;
649 }
650
651 if (++n >= req->cnum)
652 break;
653
654 req->ci++;
655 }
656 req->cnum = n;
657
658 up_read(&bnep_session_sem);
659 return err;
660}
661
662int bnep_get_conninfo(struct bnep_conninfo *ci)
663{
664 struct bnep_session *s;
665 int err = 0;
666
667 down_read(&bnep_session_sem);
668
669 s = __bnep_get_session(ci->dst);
670 if (s)
671 __bnep_copy_ci(ci, s);
672 else
673 err = -ENOENT;
674
675 up_read(&bnep_session_sem);
676 return err;
677}
678
679static int __init bnep_init(void)
680{
681 char flt[50] = "";
682
683 l2cap_load();
684
685#ifdef CONFIG_BT_BNEP_PROTO_FILTER
686 strcat(flt, "protocol ");
687#endif
688
689#ifdef CONFIG_BT_BNEP_MC_FILTER
690 strcat(flt, "multicast");
691#endif
692
693 BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION);
694 if (flt[0])
695 BT_INFO("BNEP filters: %s", flt);
696
697 bnep_sock_init();
698 return 0;
699}
700
701static void __exit bnep_exit(void)
702{
703 bnep_sock_cleanup();
704}
705
706module_init(bnep_init);
707module_exit(bnep_exit);
708
709MODULE_AUTHOR("David Libault <david.libault@inventel.fr>, Maxim Krasnyansky <maxk@qualcomm.com>");
710MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION);
711MODULE_VERSION(VERSION);
712MODULE_LICENSE("GPL");
713MODULE_ALIAS("bt-proto-4");
diff --git a/net/bluetooth/bnep/netdev.c b/net/bluetooth/bnep/netdev.c
new file mode 100644
index 000000000000..921204f95f4a
--- /dev/null
+++ b/net/bluetooth/bnep/netdev.c
@@ -0,0 +1,247 @@
1/*
2 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
4 Written 2001-2002 by
5 Cl�ment Moreau <clement.moreau@inventel.fr>
6 David Libault <david.libault@inventel.fr>
7
8 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
9
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License version 2 as
12 published by the Free Software Foundation;
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
17 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
18 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
21 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
24 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
25 SOFTWARE IS DISCLAIMED.
26*/
27
28/*
29 * $Id: netdev.c,v 1.8 2002/08/04 21:23:58 maxk Exp $
30 */
31
32#include <linux/config.h>
33#include <linux/module.h>
34
35#include <linux/socket.h>
36#include <linux/netdevice.h>
37#include <linux/etherdevice.h>
38#include <linux/skbuff.h>
39#include <linux/wait.h>
40
41#include <asm/unaligned.h>
42
43#include <net/bluetooth/bluetooth.h>
44#include <net/bluetooth/hci_core.h>
45#include <net/bluetooth/l2cap.h>
46
47#include "bnep.h"
48
49#ifndef CONFIG_BT_BNEP_DEBUG
50#undef BT_DBG
51#define BT_DBG( A... )
52#endif
53
54#define BNEP_TX_QUEUE_LEN 20
55
56static int bnep_net_open(struct net_device *dev)
57{
58 netif_start_queue(dev);
59 return 0;
60}
61
62static int bnep_net_close(struct net_device *dev)
63{
64 netif_stop_queue(dev);
65 return 0;
66}
67
68static struct net_device_stats *bnep_net_get_stats(struct net_device *dev)
69{
70 struct bnep_session *s = dev->priv;
71 return &s->stats;
72}
73
74static void bnep_net_set_mc_list(struct net_device *dev)
75{
76#ifdef CONFIG_BT_BNEP_MC_FILTER
77 struct bnep_session *s = dev->priv;
78 struct sock *sk = s->sock->sk;
79 struct bnep_set_filter_req *r;
80 struct sk_buff *skb;
81 int size;
82
83 BT_DBG("%s mc_count %d", dev->name, dev->mc_count);
84
85 size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
86 skb = alloc_skb(size, GFP_ATOMIC);
87 if (!skb) {
88 BT_ERR("%s Multicast list allocation failed", dev->name);
89 return;
90 }
91
92 r = (void *) skb->data;
93 __skb_put(skb, sizeof(*r));
94
95 r->type = BNEP_CONTROL;
96 r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
97
98 if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
99 u8 start[ETH_ALEN] = { 0x01 };
100
101 /* Request all addresses */
102 memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN);
103 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
104 r->len = htons(ETH_ALEN * 2);
105 } else {
106 struct dev_mc_list *dmi = dev->mc_list;
107 int i, len = skb->len;
108
109 if (dev->flags & IFF_BROADCAST) {
110 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
111 memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
112 }
113
114 /* FIXME: We should group addresses here. */
115
116 for (i = 0; i < dev->mc_count && i < BNEP_MAX_MULTICAST_FILTERS; i++) {
117 memcpy(__skb_put(skb, ETH_ALEN), dmi->dmi_addr, ETH_ALEN);
118 memcpy(__skb_put(skb, ETH_ALEN), dmi->dmi_addr, ETH_ALEN);
119 dmi = dmi->next;
120 }
121 r->len = htons(skb->len - len);
122 }
123
124 skb_queue_tail(&sk->sk_write_queue, skb);
125 wake_up_interruptible(sk->sk_sleep);
126#endif
127}
128
129static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
130{
131 BT_DBG("%s", dev->name);
132 return 0;
133}
134
135static void bnep_net_timeout(struct net_device *dev)
136{
137 BT_DBG("net_timeout");
138 netif_wake_queue(dev);
139}
140
141static int bnep_net_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
142{
143 return -EINVAL;
144}
145
146#ifdef CONFIG_BT_BNEP_MC_FILTER
147static inline int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
148{
149 struct ethhdr *eh = (void *) skb->data;
150
151 if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
152 return 1;
153 return 0;
154}
155#endif
156
157#ifdef CONFIG_BT_BNEP_PROTO_FILTER
158/* Determine ether protocol. Based on eth_type_trans. */
159static inline u16 bnep_net_eth_proto(struct sk_buff *skb)
160{
161 struct ethhdr *eh = (void *) skb->data;
162
163 if (ntohs(eh->h_proto) >= 1536)
164 return eh->h_proto;
165
166 if (get_unaligned((u16 *) skb->data) == 0xFFFF)
167 return htons(ETH_P_802_3);
168
169 return htons(ETH_P_802_2);
170}
171
172static inline int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
173{
174 u16 proto = bnep_net_eth_proto(skb);
175 struct bnep_proto_filter *f = s->proto_filter;
176 int i;
177
178 for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
179 if (proto >= f[i].start && proto <= f[i].end)
180 return 0;
181 }
182
183 BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
184 return 1;
185}
186#endif
187
188static int bnep_net_xmit(struct sk_buff *skb, struct net_device *dev)
189{
190 struct bnep_session *s = dev->priv;
191 struct sock *sk = s->sock->sk;
192
193 BT_DBG("skb %p, dev %p", skb, dev);
194
195#ifdef CONFIG_BT_BNEP_MC_FILTER
196 if (bnep_net_mc_filter(skb, s)) {
197 kfree_skb(skb);
198 return 0;
199 }
200#endif
201
202#ifdef CONFIG_BT_BNEP_PROTO_FILTER
203 if (bnep_net_proto_filter(skb, s)) {
204 kfree_skb(skb);
205 return 0;
206 }
207#endif
208
209 /*
210 * We cannot send L2CAP packets from here as we are potentially in a bh.
211 * So we have to queue them and wake up session thread which is sleeping
212 * on the sk->sk_sleep.
213 */
214 dev->trans_start = jiffies;
215 skb_queue_tail(&sk->sk_write_queue, skb);
216 wake_up_interruptible(sk->sk_sleep);
217
218 if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
219 BT_DBG("tx queue is full");
220
221 /* Stop queuing.
222 * Session thread will do netif_wake_queue() */
223 netif_stop_queue(dev);
224 }
225
226 return 0;
227}
228
229void bnep_net_setup(struct net_device *dev)
230{
231
232 memset(dev->broadcast, 0xff, ETH_ALEN);
233 dev->addr_len = ETH_ALEN;
234
235 ether_setup(dev);
236
237 dev->open = bnep_net_open;
238 dev->stop = bnep_net_close;
239 dev->hard_start_xmit = bnep_net_xmit;
240 dev->get_stats = bnep_net_get_stats;
241 dev->do_ioctl = bnep_net_ioctl;
242 dev->set_mac_address = bnep_net_set_mac_addr;
243 dev->set_multicast_list = bnep_net_set_mc_list;
244
245 dev->watchdog_timeo = HZ * 2;
246 dev->tx_timeout = bnep_net_timeout;
247}
diff --git a/net/bluetooth/bnep/sock.c b/net/bluetooth/bnep/sock.c
new file mode 100644
index 000000000000..9a8d99a39b6d
--- /dev/null
+++ b/net/bluetooth/bnep/sock.c
@@ -0,0 +1,237 @@
1/*
2 BNEP implementation for Linux Bluetooth stack (BlueZ).
3 Copyright (C) 2001-2002 Inventel Systemes
4 Written 2001-2002 by
5 David Libault <david.libault@inventel.fr>
6
7 Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
8
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License version 2 as
11 published by the Free Software Foundation;
12
13 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
14 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
16 IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
17 CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
18 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
19 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
20 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21
22 ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
23 COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
24 SOFTWARE IS DISCLAIMED.
25*/
26
27/*
28 * $Id: sock.c,v 1.4 2002/08/04 21:23:58 maxk Exp $
29 */
30
31#include <linux/config.h>
32#include <linux/module.h>
33
34#include <linux/types.h>
35#include <linux/errno.h>
36#include <linux/kernel.h>
37#include <linux/major.h>
38#include <linux/sched.h>
39#include <linux/slab.h>
40#include <linux/poll.h>
41#include <linux/fcntl.h>
42#include <linux/skbuff.h>
43#include <linux/socket.h>
44#include <linux/ioctl.h>
45#include <linux/file.h>
46#include <linux/init.h>
47#include <net/sock.h>
48
49#include <asm/system.h>
50#include <asm/uaccess.h>
51
52#include "bnep.h"
53
54#ifndef CONFIG_BT_BNEP_DEBUG
55#undef BT_DBG
56#define BT_DBG( A... )
57#endif
58
59static int bnep_sock_release(struct socket *sock)
60{
61 struct sock *sk = sock->sk;
62
63 BT_DBG("sock %p sk %p", sock, sk);
64
65 if (!sk)
66 return 0;
67
68 sock_orphan(sk);
69 sock_put(sk);
70 return 0;
71}
72
73static int bnep_sock_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
74{
75 struct bnep_connlist_req cl;
76 struct bnep_connadd_req ca;
77 struct bnep_conndel_req cd;
78 struct bnep_conninfo ci;
79 struct socket *nsock;
80 void __user *argp = (void __user *)arg;
81 int err;
82
83 BT_DBG("cmd %x arg %lx", cmd, arg);
84
85 switch (cmd) {
86 case BNEPCONNADD:
87 if (!capable(CAP_NET_ADMIN))
88 return -EACCES;
89
90 if (copy_from_user(&ca, argp, sizeof(ca)))
91 return -EFAULT;
92
93 nsock = sockfd_lookup(ca.sock, &err);
94 if (!nsock)
95 return err;
96
97 if (nsock->sk->sk_state != BT_CONNECTED) {
98 fput(nsock->file);
99 return -EBADFD;
100 }
101
102 err = bnep_add_connection(&ca, nsock);
103 if (!err) {
104 if (copy_to_user(argp, &ca, sizeof(ca)))
105 err = -EFAULT;
106 } else
107 fput(nsock->file);
108
109 return err;
110
111 case BNEPCONNDEL:
112 if (!capable(CAP_NET_ADMIN))
113 return -EACCES;
114
115 if (copy_from_user(&cd, argp, sizeof(cd)))
116 return -EFAULT;
117
118 return bnep_del_connection(&cd);
119
120 case BNEPGETCONNLIST:
121 if (copy_from_user(&cl, argp, sizeof(cl)))
122 return -EFAULT;
123
124 if (cl.cnum <= 0)
125 return -EINVAL;
126
127 err = bnep_get_connlist(&cl);
128 if (!err && copy_to_user(argp, &cl, sizeof(cl)))
129 return -EFAULT;
130
131 return err;
132
133 case BNEPGETCONNINFO:
134 if (copy_from_user(&ci, argp, sizeof(ci)))
135 return -EFAULT;
136
137 err = bnep_get_conninfo(&ci);
138 if (!err && copy_to_user(argp, &ci, sizeof(ci)))
139 return -EFAULT;
140
141 return err;
142
143 default:
144 return -EINVAL;
145 }
146
147 return 0;
148}
149
150static struct proto_ops bnep_sock_ops = {
151 .family = PF_BLUETOOTH,
152 .owner = THIS_MODULE,
153 .release = bnep_sock_release,
154 .ioctl = bnep_sock_ioctl,
155 .bind = sock_no_bind,
156 .getname = sock_no_getname,
157 .sendmsg = sock_no_sendmsg,
158 .recvmsg = sock_no_recvmsg,
159 .poll = sock_no_poll,
160 .listen = sock_no_listen,
161 .shutdown = sock_no_shutdown,
162 .setsockopt = sock_no_setsockopt,
163 .getsockopt = sock_no_getsockopt,
164 .connect = sock_no_connect,
165 .socketpair = sock_no_socketpair,
166 .accept = sock_no_accept,
167 .mmap = sock_no_mmap
168};
169
170static struct proto bnep_proto = {
171 .name = "BNEP",
172 .owner = THIS_MODULE,
173 .obj_size = sizeof(struct bt_sock)
174};
175
176static int bnep_sock_create(struct socket *sock, int protocol)
177{
178 struct sock *sk;
179
180 BT_DBG("sock %p", sock);
181
182 if (sock->type != SOCK_RAW)
183 return -ESOCKTNOSUPPORT;
184
185 sk = sk_alloc(PF_BLUETOOTH, GFP_KERNEL, &bnep_proto, 1);
186 if (!sk)
187 return -ENOMEM;
188
189 sock_init_data(sock, sk);
190
191 sock->ops = &bnep_sock_ops;
192
193 sock->state = SS_UNCONNECTED;
194
195 sock_reset_flag(sk, SOCK_ZAPPED);
196
197 sk->sk_protocol = protocol;
198 sk->sk_state = BT_OPEN;
199
200 return 0;
201}
202
203static struct net_proto_family bnep_sock_family_ops = {
204 .family = PF_BLUETOOTH,
205 .owner = THIS_MODULE,
206 .create = bnep_sock_create
207};
208
209int __init bnep_sock_init(void)
210{
211 int err;
212
213 err = proto_register(&bnep_proto, 0);
214 if (err < 0)
215 return err;
216
217 err = bt_sock_register(BTPROTO_BNEP, &bnep_sock_family_ops);
218 if (err < 0)
219 goto error;
220
221 return 0;
222
223error:
224 BT_ERR("Can't register BNEP socket");
225 proto_unregister(&bnep_proto);
226 return err;
227}
228
229int __exit bnep_sock_cleanup(void)
230{
231 if (bt_sock_unregister(BTPROTO_BNEP) < 0)
232 BT_ERR("Can't unregister BNEP socket");
233
234 proto_unregister(&bnep_proto);
235
236 return 0;
237}