core.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752
  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. Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
  8. This program is free software; you can redistribute it and/or modify
  9. it under the terms of the GNU General Public License version 2 as
  10. published by the Free Software Foundation;
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  12. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
  14. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
  15. CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
  16. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
  20. COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
  21. SOFTWARE IS DISCLAIMED.
  22. */
  23. #include <linux/module.h>
  24. #include <linux/kernel.h>
  25. #include <linux/sched.h>
  26. #include <linux/signal.h>
  27. #include <linux/init.h>
  28. #include <linux/wait.h>
  29. #include <linux/freezer.h>
  30. #include <linux/errno.h>
  31. #include <linux/net.h>
  32. #include <linux/slab.h>
  33. #include <linux/kthread.h>
  34. #include <net/sock.h>
  35. #include <linux/socket.h>
  36. #include <linux/file.h>
  37. #include <linux/netdevice.h>
  38. #include <linux/etherdevice.h>
  39. #include <linux/skbuff.h>
  40. #include <asm/unaligned.h>
  41. #include <net/bluetooth/bluetooth.h>
  42. #include <net/bluetooth/hci_core.h>
  43. #include <net/bluetooth/l2cap.h>
  44. #include "bnep.h"
  45. #define VERSION "1.3"
  46. static int compress_src = 1;
  47. static int compress_dst = 1;
  48. static LIST_HEAD(bnep_session_list);
  49. static DECLARE_RWSEM(bnep_session_sem);
  50. static struct bnep_session *__bnep_get_session(u8 *dst)
  51. {
  52. struct bnep_session *s;
  53. struct list_head *p;
  54. BT_DBG("");
  55. list_for_each(p, &bnep_session_list) {
  56. s = list_entry(p, struct bnep_session, list);
  57. if (!compare_ether_addr(dst, s->eh.h_source))
  58. return s;
  59. }
  60. return NULL;
  61. }
  62. static void __bnep_link_session(struct bnep_session *s)
  63. {
  64. /* It's safe to call __module_get() here because sessions are added
  65. by the socket layer which has to hold the reference to this module.
  66. */
  67. __module_get(THIS_MODULE);
  68. list_add(&s->list, &bnep_session_list);
  69. }
  70. static void __bnep_unlink_session(struct bnep_session *s)
  71. {
  72. list_del(&s->list);
  73. module_put(THIS_MODULE);
  74. }
  75. static int bnep_send(struct bnep_session *s, void *data, size_t len)
  76. {
  77. struct socket *sock = s->sock;
  78. struct kvec iv = { data, len };
  79. return kernel_sendmsg(sock, &s->msg, &iv, 1, len);
  80. }
  81. static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp)
  82. {
  83. struct bnep_control_rsp rsp;
  84. rsp.type = BNEP_CONTROL;
  85. rsp.ctrl = ctrl;
  86. rsp.resp = htons(resp);
  87. return bnep_send(s, &rsp, sizeof(rsp));
  88. }
  89. #ifdef CONFIG_BT_BNEP_PROTO_FILTER
  90. static inline void bnep_set_default_proto_filter(struct bnep_session *s)
  91. {
  92. /* (IPv4, ARP) */
  93. s->proto_filter[0].start = ETH_P_IP;
  94. s->proto_filter[0].end = ETH_P_ARP;
  95. /* (RARP, AppleTalk) */
  96. s->proto_filter[1].start = ETH_P_RARP;
  97. s->proto_filter[1].end = ETH_P_AARP;
  98. /* (IPX, IPv6) */
  99. s->proto_filter[2].start = ETH_P_IPX;
  100. s->proto_filter[2].end = ETH_P_IPV6;
  101. }
  102. #endif
  103. static int bnep_ctrl_set_netfilter(struct bnep_session *s, __be16 *data, int len)
  104. {
  105. int n;
  106. if (len < 2)
  107. return -EILSEQ;
  108. n = get_unaligned_be16(data);
  109. data++;
  110. len -= 2;
  111. if (len < n)
  112. return -EILSEQ;
  113. BT_DBG("filter len %d", n);
  114. #ifdef CONFIG_BT_BNEP_PROTO_FILTER
  115. n /= 4;
  116. if (n <= BNEP_MAX_PROTO_FILTERS) {
  117. struct bnep_proto_filter *f = s->proto_filter;
  118. int i;
  119. for (i = 0; i < n; i++) {
  120. f[i].start = get_unaligned_be16(data++);
  121. f[i].end = get_unaligned_be16(data++);
  122. BT_DBG("proto filter start %d end %d",
  123. f[i].start, f[i].end);
  124. }
  125. if (i < BNEP_MAX_PROTO_FILTERS)
  126. memset(f + i, 0, sizeof(*f));
  127. if (n == 0)
  128. bnep_set_default_proto_filter(s);
  129. bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_SUCCESS);
  130. } else {
  131. bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_LIMIT_REACHED);
  132. }
  133. #else
  134. bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
  135. #endif
  136. return 0;
  137. }
  138. static int bnep_ctrl_set_mcfilter(struct bnep_session *s, u8 *data, int len)
  139. {
  140. int n;
  141. if (len < 2)
  142. return -EILSEQ;
  143. n = get_unaligned_be16(data);
  144. data += 2;
  145. len -= 2;
  146. if (len < n)
  147. return -EILSEQ;
  148. BT_DBG("filter len %d", n);
  149. #ifdef CONFIG_BT_BNEP_MC_FILTER
  150. n /= (ETH_ALEN * 2);
  151. if (n > 0) {
  152. int i;
  153. s->mc_filter = 0;
  154. /* Always send broadcast */
  155. set_bit(bnep_mc_hash(s->dev->broadcast), (ulong *) &s->mc_filter);
  156. /* Add address ranges to the multicast hash */
  157. for (; n > 0; n--) {
  158. u8 a1[6], *a2;
  159. memcpy(a1, data, ETH_ALEN);
  160. data += ETH_ALEN;
  161. a2 = data;
  162. data += ETH_ALEN;
  163. BT_DBG("mc filter %s -> %s",
  164. batostr((void *) a1), batostr((void *) a2));
  165. /* Iterate from a1 to a2 */
  166. set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
  167. while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) {
  168. /* Increment a1 */
  169. i = 5;
  170. while (i >= 0 && ++a1[i--] == 0)
  171. ;
  172. set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter);
  173. }
  174. }
  175. }
  176. BT_DBG("mc filter hash 0x%llx", s->mc_filter);
  177. bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS);
  178. #else
  179. bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ);
  180. #endif
  181. return 0;
  182. }
  183. static int bnep_rx_control(struct bnep_session *s, void *data, int len)
  184. {
  185. u8 cmd = *(u8 *)data;
  186. int err = 0;
  187. data++;
  188. len--;
  189. switch (cmd) {
  190. case BNEP_CMD_NOT_UNDERSTOOD:
  191. case BNEP_SETUP_CONN_RSP:
  192. case BNEP_FILTER_NET_TYPE_RSP:
  193. case BNEP_FILTER_MULTI_ADDR_RSP:
  194. /* Ignore these for now */
  195. break;
  196. case BNEP_FILTER_NET_TYPE_SET:
  197. err = bnep_ctrl_set_netfilter(s, data, len);
  198. break;
  199. case BNEP_FILTER_MULTI_ADDR_SET:
  200. err = bnep_ctrl_set_mcfilter(s, data, len);
  201. break;
  202. case BNEP_SETUP_CONN_REQ:
  203. err = bnep_send_rsp(s, BNEP_SETUP_CONN_RSP, BNEP_CONN_NOT_ALLOWED);
  204. break;
  205. default: {
  206. u8 pkt[3];
  207. pkt[0] = BNEP_CONTROL;
  208. pkt[1] = BNEP_CMD_NOT_UNDERSTOOD;
  209. pkt[2] = cmd;
  210. bnep_send(s, pkt, sizeof(pkt));
  211. }
  212. break;
  213. }
  214. return err;
  215. }
  216. static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb)
  217. {
  218. struct bnep_ext_hdr *h;
  219. int err = 0;
  220. do {
  221. h = (void *) skb->data;
  222. if (!skb_pull(skb, sizeof(*h))) {
  223. err = -EILSEQ;
  224. break;
  225. }
  226. BT_DBG("type 0x%x len %d", h->type, h->len);
  227. switch (h->type & BNEP_TYPE_MASK) {
  228. case BNEP_EXT_CONTROL:
  229. bnep_rx_control(s, skb->data, skb->len);
  230. break;
  231. default:
  232. /* Unknown extension, skip it. */
  233. break;
  234. }
  235. if (!skb_pull(skb, h->len)) {
  236. err = -EILSEQ;
  237. break;
  238. }
  239. } while (!err && (h->type & BNEP_EXT_HEADER));
  240. return err;
  241. }
  242. static u8 __bnep_rx_hlen[] = {
  243. ETH_HLEN, /* BNEP_GENERAL */
  244. 0, /* BNEP_CONTROL */
  245. 2, /* BNEP_COMPRESSED */
  246. ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */
  247. ETH_ALEN + 2 /* BNEP_COMPRESSED_DST_ONLY */
  248. };
  249. static inline int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb)
  250. {
  251. struct net_device *dev = s->dev;
  252. struct sk_buff *nskb;
  253. u8 type;
  254. dev->stats.rx_bytes += skb->len;
  255. type = *(u8 *) skb->data;
  256. skb_pull(skb, 1);
  257. if ((type & BNEP_TYPE_MASK) >= sizeof(__bnep_rx_hlen))
  258. goto badframe;
  259. if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) {
  260. bnep_rx_control(s, skb->data, skb->len);
  261. kfree_skb(skb);
  262. return 0;
  263. }
  264. skb_reset_mac_header(skb);
  265. /* Verify and pull out header */
  266. if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK]))
  267. goto badframe;
  268. s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
  269. if (type & BNEP_EXT_HEADER) {
  270. if (bnep_rx_extension(s, skb) < 0)
  271. goto badframe;
  272. }
  273. /* Strip 802.1p header */
  274. if (ntohs(s->eh.h_proto) == 0x8100) {
  275. if (!skb_pull(skb, 4))
  276. goto badframe;
  277. s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2));
  278. }
  279. /* We have to alloc new skb and copy data here :(. Because original skb
  280. * may not be modified and because of the alignment requirements. */
  281. nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL);
  282. if (!nskb) {
  283. dev->stats.rx_dropped++;
  284. kfree_skb(skb);
  285. return -ENOMEM;
  286. }
  287. skb_reserve(nskb, 2);
  288. /* Decompress header and construct ether frame */
  289. switch (type & BNEP_TYPE_MASK) {
  290. case BNEP_COMPRESSED:
  291. memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN);
  292. break;
  293. case BNEP_COMPRESSED_SRC_ONLY:
  294. memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN);
  295. memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb), ETH_ALEN);
  296. put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
  297. break;
  298. case BNEP_COMPRESSED_DST_ONLY:
  299. memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb),
  300. ETH_ALEN);
  301. memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source,
  302. ETH_ALEN + 2);
  303. break;
  304. case BNEP_GENERAL:
  305. memcpy(__skb_put(nskb, ETH_ALEN * 2), skb_mac_header(skb),
  306. ETH_ALEN * 2);
  307. put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2));
  308. break;
  309. }
  310. skb_copy_from_linear_data(skb, __skb_put(nskb, skb->len), skb->len);
  311. kfree_skb(skb);
  312. dev->stats.rx_packets++;
  313. nskb->ip_summed = CHECKSUM_NONE;
  314. nskb->protocol = eth_type_trans(nskb, dev);
  315. netif_rx_ni(nskb);
  316. return 0;
  317. badframe:
  318. dev->stats.rx_errors++;
  319. kfree_skb(skb);
  320. return 0;
  321. }
  322. static u8 __bnep_tx_types[] = {
  323. BNEP_GENERAL,
  324. BNEP_COMPRESSED_SRC_ONLY,
  325. BNEP_COMPRESSED_DST_ONLY,
  326. BNEP_COMPRESSED
  327. };
  328. static inline int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb)
  329. {
  330. struct ethhdr *eh = (void *) skb->data;
  331. struct socket *sock = s->sock;
  332. struct kvec iv[3];
  333. int len = 0, il = 0;
  334. u8 type = 0;
  335. BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type);
  336. if (!skb->dev) {
  337. /* Control frame sent by us */
  338. goto send;
  339. }
  340. iv[il++] = (struct kvec) { &type, 1 };
  341. len++;
  342. if (compress_src && !compare_ether_addr(eh->h_dest, s->eh.h_source))
  343. type |= 0x01;
  344. if (compress_dst && !compare_ether_addr(eh->h_source, s->eh.h_dest))
  345. type |= 0x02;
  346. if (type)
  347. skb_pull(skb, ETH_ALEN * 2);
  348. type = __bnep_tx_types[type];
  349. switch (type) {
  350. case BNEP_COMPRESSED_SRC_ONLY:
  351. iv[il++] = (struct kvec) { eh->h_source, ETH_ALEN };
  352. len += ETH_ALEN;
  353. break;
  354. case BNEP_COMPRESSED_DST_ONLY:
  355. iv[il++] = (struct kvec) { eh->h_dest, ETH_ALEN };
  356. len += ETH_ALEN;
  357. break;
  358. }
  359. send:
  360. iv[il++] = (struct kvec) { skb->data, skb->len };
  361. len += skb->len;
  362. /* FIXME: linearize skb */
  363. {
  364. len = kernel_sendmsg(sock, &s->msg, iv, il, len);
  365. }
  366. kfree_skb(skb);
  367. if (len > 0) {
  368. s->dev->stats.tx_bytes += len;
  369. s->dev->stats.tx_packets++;
  370. return 0;
  371. }
  372. return len;
  373. }
  374. static int bnep_session(void *arg)
  375. {
  376. struct bnep_session *s = arg;
  377. struct net_device *dev = s->dev;
  378. struct sock *sk = s->sock->sk;
  379. struct sk_buff *skb;
  380. wait_queue_t wait;
  381. BT_DBG("");
  382. set_user_nice(current, -15);
  383. init_waitqueue_entry(&wait, current);
  384. add_wait_queue(sk_sleep(sk), &wait);
  385. while (1) {
  386. set_current_state(TASK_INTERRUPTIBLE);
  387. if (atomic_read(&s->terminate))
  388. break;
  389. /* RX */
  390. while ((skb = skb_dequeue(&sk->sk_receive_queue))) {
  391. skb_orphan(skb);
  392. bnep_rx_frame(s, skb);
  393. }
  394. if (sk->sk_state != BT_CONNECTED)
  395. break;
  396. /* TX */
  397. while ((skb = skb_dequeue(&sk->sk_write_queue)))
  398. if (bnep_tx_frame(s, skb))
  399. break;
  400. netif_wake_queue(dev);
  401. schedule();
  402. }
  403. __set_current_state(TASK_RUNNING);
  404. remove_wait_queue(sk_sleep(sk), &wait);
  405. /* Cleanup session */
  406. down_write(&bnep_session_sem);
  407. /* Delete network device */
  408. unregister_netdev(dev);
  409. /* Wakeup user-space polling for socket errors */
  410. s->sock->sk->sk_err = EUNATCH;
  411. wake_up_interruptible(sk_sleep(s->sock->sk));
  412. /* Release the socket */
  413. fput(s->sock->file);
  414. __bnep_unlink_session(s);
  415. up_write(&bnep_session_sem);
  416. free_netdev(dev);
  417. return 0;
  418. }
  419. static struct device *bnep_get_device(struct bnep_session *session)
  420. {
  421. bdaddr_t *src = &bt_sk(session->sock->sk)->src;
  422. bdaddr_t *dst = &bt_sk(session->sock->sk)->dst;
  423. struct hci_dev *hdev;
  424. struct hci_conn *conn;
  425. hdev = hci_get_route(dst, src);
  426. if (!hdev)
  427. return NULL;
  428. conn = hci_conn_hash_lookup_ba(hdev, ACL_LINK, dst);
  429. hci_dev_put(hdev);
  430. return conn ? &conn->dev : NULL;
  431. }
  432. static struct device_type bnep_type = {
  433. .name = "bluetooth",
  434. };
  435. int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock)
  436. {
  437. struct net_device *dev;
  438. struct bnep_session *s, *ss;
  439. u8 dst[ETH_ALEN], src[ETH_ALEN];
  440. int err;
  441. BT_DBG("");
  442. baswap((void *) dst, &bt_sk(sock->sk)->dst);
  443. baswap((void *) src, &bt_sk(sock->sk)->src);
  444. /* session struct allocated as private part of net_device */
  445. dev = alloc_netdev(sizeof(struct bnep_session),
  446. (*req->device) ? req->device : "bnep%d",
  447. bnep_net_setup);
  448. if (!dev)
  449. return -ENOMEM;
  450. down_write(&bnep_session_sem);
  451. ss = __bnep_get_session(dst);
  452. if (ss && ss->state == BT_CONNECTED) {
  453. err = -EEXIST;
  454. goto failed;
  455. }
  456. s = netdev_priv(dev);
  457. /* This is rx header therefore addresses are swapped.
  458. * ie. eh.h_dest is our local address. */
  459. memcpy(s->eh.h_dest, &src, ETH_ALEN);
  460. memcpy(s->eh.h_source, &dst, ETH_ALEN);
  461. memcpy(dev->dev_addr, s->eh.h_dest, ETH_ALEN);
  462. s->dev = dev;
  463. s->sock = sock;
  464. s->role = req->role;
  465. s->state = BT_CONNECTED;
  466. s->msg.msg_flags = MSG_NOSIGNAL;
  467. #ifdef CONFIG_BT_BNEP_MC_FILTER
  468. /* Set default mc filter */
  469. set_bit(bnep_mc_hash(dev->broadcast), (ulong *) &s->mc_filter);
  470. #endif
  471. #ifdef CONFIG_BT_BNEP_PROTO_FILTER
  472. /* Set default protocol filter */
  473. bnep_set_default_proto_filter(s);
  474. #endif
  475. SET_NETDEV_DEV(dev, bnep_get_device(s));
  476. SET_NETDEV_DEVTYPE(dev, &bnep_type);
  477. err = register_netdev(dev);
  478. if (err)
  479. goto failed;
  480. __bnep_link_session(s);
  481. s->task = kthread_run(bnep_session, s, "kbnepd %s", dev->name);
  482. if (IS_ERR(s->task)) {
  483. /* Session thread start failed, gotta cleanup. */
  484. unregister_netdev(dev);
  485. __bnep_unlink_session(s);
  486. err = PTR_ERR(s->task);
  487. goto failed;
  488. }
  489. up_write(&bnep_session_sem);
  490. strcpy(req->device, dev->name);
  491. return 0;
  492. failed:
  493. up_write(&bnep_session_sem);
  494. free_netdev(dev);
  495. return err;
  496. }
  497. int bnep_del_connection(struct bnep_conndel_req *req)
  498. {
  499. struct bnep_session *s;
  500. int err = 0;
  501. BT_DBG("");
  502. down_read(&bnep_session_sem);
  503. s = __bnep_get_session(req->dst);
  504. if (s) {
  505. atomic_inc(&s->terminate);
  506. wake_up_process(s->task);
  507. } else
  508. err = -ENOENT;
  509. up_read(&bnep_session_sem);
  510. return err;
  511. }
  512. static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s)
  513. {
  514. memset(ci, 0, sizeof(*ci));
  515. memcpy(ci->dst, s->eh.h_source, ETH_ALEN);
  516. strcpy(ci->device, s->dev->name);
  517. ci->flags = s->flags;
  518. ci->state = s->state;
  519. ci->role = s->role;
  520. }
  521. int bnep_get_connlist(struct bnep_connlist_req *req)
  522. {
  523. struct list_head *p;
  524. int err = 0, n = 0;
  525. down_read(&bnep_session_sem);
  526. list_for_each(p, &bnep_session_list) {
  527. struct bnep_session *s;
  528. struct bnep_conninfo ci;
  529. s = list_entry(p, struct bnep_session, list);
  530. __bnep_copy_ci(&ci, s);
  531. if (copy_to_user(req->ci, &ci, sizeof(ci))) {
  532. err = -EFAULT;
  533. break;
  534. }
  535. if (++n >= req->cnum)
  536. break;
  537. req->ci++;
  538. }
  539. req->cnum = n;
  540. up_read(&bnep_session_sem);
  541. return err;
  542. }
  543. int bnep_get_conninfo(struct bnep_conninfo *ci)
  544. {
  545. struct bnep_session *s;
  546. int err = 0;
  547. down_read(&bnep_session_sem);
  548. s = __bnep_get_session(ci->dst);
  549. if (s)
  550. __bnep_copy_ci(ci, s);
  551. else
  552. err = -ENOENT;
  553. up_read(&bnep_session_sem);
  554. return err;
  555. }
  556. static int __init bnep_init(void)
  557. {
  558. char flt[50] = "";
  559. #ifdef CONFIG_BT_BNEP_PROTO_FILTER
  560. strcat(flt, "protocol ");
  561. #endif
  562. #ifdef CONFIG_BT_BNEP_MC_FILTER
  563. strcat(flt, "multicast");
  564. #endif
  565. BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION);
  566. if (flt[0])
  567. BT_INFO("BNEP filters: %s", flt);
  568. bnep_sock_init();
  569. return 0;
  570. }
  571. static void __exit bnep_exit(void)
  572. {
  573. bnep_sock_cleanup();
  574. }
  575. module_init(bnep_init);
  576. module_exit(bnep_exit);
  577. module_param(compress_src, bool, 0644);
  578. MODULE_PARM_DESC(compress_src, "Compress sources headers");
  579. module_param(compress_dst, bool, 0644);
  580. MODULE_PARM_DESC(compress_dst, "Compress destination headers");
  581. MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>");
  582. MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION);
  583. MODULE_VERSION(VERSION);
  584. MODULE_LICENSE("GPL");
  585. MODULE_ALIAS("bt-proto-4");