netdev.c 5.9 KB

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