PageRenderTime 51ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/net/bluetooth/bnep/netdev.c

https://bitbucket.org/Xmister/endeavoru-jb-crc-3.1.10
C | 239 lines | 158 code | 43 blank | 38 comment | 18 complexity | 6f032756eab5570467fa99539a0d2631 MD5 | raw file
  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/slab.h>
  25. #include <linux/socket.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/skbuff.h>
  29. #include <linux/wait.h>
  30. #include <asm/unaligned.h>
  31. #include <net/bluetooth/bluetooth.h>
  32. #include <net/bluetooth/hci_core.h>
  33. #include <net/bluetooth/l2cap.h>
  34. #include "bnep.h"
  35. #define BNEP_TX_QUEUE_LEN 20
  36. #define IFF_TX_SKB_SHARING 0x10000 /* The interface supports sharing
  37. * skbs on transmit */
  38. static int bnep_net_open(struct net_device *dev)
  39. {
  40. netif_start_queue(dev);
  41. return 0;
  42. }
  43. static int bnep_net_close(struct net_device *dev)
  44. {
  45. netif_stop_queue(dev);
  46. return 0;
  47. }
  48. static void bnep_net_set_mc_list(struct net_device *dev)
  49. {
  50. #ifdef CONFIG_BT_BNEP_MC_FILTER
  51. struct bnep_session *s = netdev_priv(dev);
  52. struct sock *sk = s->sock->sk;
  53. struct bnep_set_filter_req *r;
  54. struct sk_buff *skb;
  55. int size;
  56. BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev));
  57. size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
  58. skb = alloc_skb(size, GFP_ATOMIC);
  59. if (!skb) {
  60. BT_ERR("%s Multicast list allocation failed", dev->name);
  61. return;
  62. }
  63. r = (void *) skb->data;
  64. __skb_put(skb, sizeof(*r));
  65. r->type = BNEP_CONTROL;
  66. r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
  67. if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
  68. u8 start[ETH_ALEN] = { 0x01 };
  69. /* Request all addresses */
  70. memcpy(__skb_put(skb, ETH_ALEN), start, ETH_ALEN);
  71. memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
  72. r->len = htons(ETH_ALEN * 2);
  73. } else {
  74. struct netdev_hw_addr *ha;
  75. int i, len = skb->len;
  76. if (dev->flags & IFF_BROADCAST) {
  77. memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
  78. memcpy(__skb_put(skb, ETH_ALEN), dev->broadcast, ETH_ALEN);
  79. }
  80. /* FIXME: We should group addresses here. */
  81. i = 0;
  82. netdev_for_each_mc_addr(ha, dev) {
  83. if (i == BNEP_MAX_MULTICAST_FILTERS)
  84. break;
  85. memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
  86. memcpy(__skb_put(skb, ETH_ALEN), ha->addr, ETH_ALEN);
  87. i++;
  88. }
  89. r->len = htons(skb->len - len);
  90. }
  91. skb_queue_tail(&sk->sk_write_queue, skb);
  92. wake_up_interruptible(sk_sleep(sk));
  93. #endif
  94. }
  95. static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
  96. {
  97. BT_DBG("%s", dev->name);
  98. return 0;
  99. }
  100. static void bnep_net_timeout(struct net_device *dev)
  101. {
  102. BT_DBG("net_timeout");
  103. netif_wake_queue(dev);
  104. }
  105. #ifdef CONFIG_BT_BNEP_MC_FILTER
  106. static inline int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
  107. {
  108. struct ethhdr *eh = (void *) skb->data;
  109. if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
  110. return 1;
  111. return 0;
  112. }
  113. #endif
  114. #ifdef CONFIG_BT_BNEP_PROTO_FILTER
  115. /* Determine ether protocol. Based on eth_type_trans. */
  116. static inline u16 bnep_net_eth_proto(struct sk_buff *skb)
  117. {
  118. struct ethhdr *eh = (void *) skb->data;
  119. u16 proto = ntohs(eh->h_proto);
  120. if (proto >= 1536)
  121. return proto;
  122. if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF))
  123. return ETH_P_802_3;
  124. return ETH_P_802_2;
  125. }
  126. static inline int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
  127. {
  128. u16 proto = bnep_net_eth_proto(skb);
  129. struct bnep_proto_filter *f = s->proto_filter;
  130. int i;
  131. for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
  132. if (proto >= f[i].start && proto <= f[i].end)
  133. return 0;
  134. }
  135. BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
  136. return 1;
  137. }
  138. #endif
  139. static netdev_tx_t bnep_net_xmit(struct sk_buff *skb,
  140. struct net_device *dev)
  141. {
  142. struct bnep_session *s = netdev_priv(dev);
  143. struct sock *sk = s->sock->sk;
  144. BT_DBG("skb %p, dev %p", skb, dev);
  145. #ifdef CONFIG_BT_BNEP_MC_FILTER
  146. if (bnep_net_mc_filter(skb, s)) {
  147. kfree_skb(skb);
  148. return NETDEV_TX_OK;
  149. }
  150. #endif
  151. #ifdef CONFIG_BT_BNEP_PROTO_FILTER
  152. if (bnep_net_proto_filter(skb, s)) {
  153. kfree_skb(skb);
  154. return NETDEV_TX_OK;
  155. }
  156. #endif
  157. /*
  158. * We cannot send L2CAP packets from here as we are potentially in a bh.
  159. * So we have to queue them and wake up session thread which is sleeping
  160. * on the sk_sleep(sk).
  161. */
  162. dev->trans_start = jiffies;
  163. skb_queue_tail(&sk->sk_write_queue, skb);
  164. wake_up_interruptible(sk_sleep(sk));
  165. if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
  166. BT_DBG("tx queue is full");
  167. /* Stop queuing.
  168. * Session thread will do netif_wake_queue() */
  169. netif_stop_queue(dev);
  170. }
  171. return NETDEV_TX_OK;
  172. }
  173. static const struct net_device_ops bnep_netdev_ops = {
  174. .ndo_open = bnep_net_open,
  175. .ndo_stop = bnep_net_close,
  176. .ndo_start_xmit = bnep_net_xmit,
  177. .ndo_validate_addr = eth_validate_addr,
  178. .ndo_set_rx_mode = bnep_net_set_mc_list,
  179. .ndo_set_mac_address = bnep_net_set_mac_addr,
  180. .ndo_tx_timeout = bnep_net_timeout,
  181. .ndo_change_mtu = eth_change_mtu,
  182. };
  183. void bnep_net_setup(struct net_device *dev)
  184. {
  185. memset(dev->broadcast, 0xff, ETH_ALEN);
  186. dev->addr_len = ETH_ALEN;
  187. ether_setup(dev);
  188. dev->priv_flags &= ~IFF_TX_SKB_SHARING;
  189. //netdev_attach_ops(dev, &bnep_netdev_ops);
  190. dev->netdev_ops = &bnep_netdev_ops;
  191. dev->watchdog_timeo = HZ * 2;
  192. }