netdev.c 5.8 KB

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