11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds BNEP implementation for Linux Bluetooth stack (BlueZ).
31da177e4SLinus Torvalds Copyright (C) 2001-2002 Inventel Systemes
41da177e4SLinus Torvalds Written 2001-2002 by
596de0e25SJan Engelhardt Clément Moreau <clement.moreau@inventel.fr>
61da177e4SLinus Torvalds David Libault <david.libault@inventel.fr>
71da177e4SLinus Torvalds
81da177e4SLinus Torvalds Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com>
91da177e4SLinus Torvalds
101da177e4SLinus Torvalds This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds it under the terms of the GNU General Public License version 2 as
121da177e4SLinus Torvalds published by the Free Software Foundation;
131da177e4SLinus Torvalds
141da177e4SLinus Torvalds THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
151da177e4SLinus Torvalds OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
161da177e4SLinus Torvalds FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS.
171da177e4SLinus Torvalds IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY
181da177e4SLinus Torvalds CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES
191da177e4SLinus Torvalds WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
201da177e4SLinus Torvalds ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
211da177e4SLinus Torvalds OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
221da177e4SLinus Torvalds
231da177e4SLinus Torvalds ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS,
241da177e4SLinus Torvalds COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS
251da177e4SLinus Torvalds SOFTWARE IS DISCLAIMED.
261da177e4SLinus Torvalds */
271da177e4SLinus Torvalds
281da177e4SLinus Torvalds #include <linux/etherdevice.h>
291da177e4SLinus Torvalds
301da177e4SLinus Torvalds #include <net/bluetooth/bluetooth.h>
311da177e4SLinus Torvalds #include <net/bluetooth/hci_core.h>
321da177e4SLinus Torvalds #include <net/bluetooth/l2cap.h>
331da177e4SLinus Torvalds
341da177e4SLinus Torvalds #include "bnep.h"
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds #define BNEP_TX_QUEUE_LEN 20
371da177e4SLinus Torvalds
bnep_net_open(struct net_device * dev)381da177e4SLinus Torvalds static int bnep_net_open(struct net_device *dev)
391da177e4SLinus Torvalds {
401da177e4SLinus Torvalds netif_start_queue(dev);
411da177e4SLinus Torvalds return 0;
421da177e4SLinus Torvalds }
431da177e4SLinus Torvalds
bnep_net_close(struct net_device * dev)441da177e4SLinus Torvalds static int bnep_net_close(struct net_device *dev)
451da177e4SLinus Torvalds {
461da177e4SLinus Torvalds netif_stop_queue(dev);
471da177e4SLinus Torvalds return 0;
481da177e4SLinus Torvalds }
491da177e4SLinus Torvalds
bnep_net_set_mc_list(struct net_device * dev)501da177e4SLinus Torvalds static void bnep_net_set_mc_list(struct net_device *dev)
511da177e4SLinus Torvalds {
521da177e4SLinus Torvalds #ifdef CONFIG_BT_BNEP_MC_FILTER
53524ad0a7SWang Chen struct bnep_session *s = netdev_priv(dev);
541da177e4SLinus Torvalds struct sock *sk = s->sock->sk;
551da177e4SLinus Torvalds struct bnep_set_filter_req *r;
561da177e4SLinus Torvalds struct sk_buff *skb;
571da177e4SLinus Torvalds int size;
581da177e4SLinus Torvalds
594cd24eafSJiri Pirko BT_DBG("%s mc_count %d", dev->name, netdev_mc_count(dev));
601da177e4SLinus Torvalds
611da177e4SLinus Torvalds size = sizeof(*r) + (BNEP_MAX_MULTICAST_FILTERS + 1) * ETH_ALEN * 2;
621da177e4SLinus Torvalds skb = alloc_skb(size, GFP_ATOMIC);
631da177e4SLinus Torvalds if (!skb) {
641da177e4SLinus Torvalds BT_ERR("%s Multicast list allocation failed", dev->name);
651da177e4SLinus Torvalds return;
661da177e4SLinus Torvalds }
671da177e4SLinus Torvalds
681da177e4SLinus Torvalds r = (void *) skb->data;
691da177e4SLinus Torvalds __skb_put(skb, sizeof(*r));
701da177e4SLinus Torvalds
711da177e4SLinus Torvalds r->type = BNEP_CONTROL;
721da177e4SLinus Torvalds r->ctrl = BNEP_FILTER_MULTI_ADDR_SET;
731da177e4SLinus Torvalds
741da177e4SLinus Torvalds if (dev->flags & (IFF_PROMISC | IFF_ALLMULTI)) {
751da177e4SLinus Torvalds u8 start[ETH_ALEN] = { 0x01 };
761da177e4SLinus Torvalds
771da177e4SLinus Torvalds /* Request all addresses */
78de77b966Syuan linyu __skb_put_data(skb, start, ETH_ALEN);
79de77b966Syuan linyu __skb_put_data(skb, dev->broadcast, ETH_ALEN);
801da177e4SLinus Torvalds r->len = htons(ETH_ALEN * 2);
811da177e4SLinus Torvalds } else {
8222bedad3SJiri Pirko struct netdev_hw_addr *ha;
831da177e4SLinus Torvalds int i, len = skb->len;
841da177e4SLinus Torvalds
851da177e4SLinus Torvalds if (dev->flags & IFF_BROADCAST) {
86de77b966Syuan linyu __skb_put_data(skb, dev->broadcast, ETH_ALEN);
87de77b966Syuan linyu __skb_put_data(skb, dev->broadcast, ETH_ALEN);
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds
901da177e4SLinus Torvalds /* FIXME: We should group addresses here. */
911da177e4SLinus Torvalds
92ff6e2163SJiri Pirko i = 0;
9322bedad3SJiri Pirko netdev_for_each_mc_addr(ha, dev) {
94ff6e2163SJiri Pirko if (i == BNEP_MAX_MULTICAST_FILTERS)
95ff6e2163SJiri Pirko break;
96de77b966Syuan linyu __skb_put_data(skb, ha->addr, ETH_ALEN);
97de77b966Syuan linyu __skb_put_data(skb, ha->addr, ETH_ALEN);
981a61a83fSGustavo F. Padovan
991a61a83fSGustavo F. Padovan i++;
1001da177e4SLinus Torvalds }
1011da177e4SLinus Torvalds r->len = htons(skb->len - len);
1021da177e4SLinus Torvalds }
1031da177e4SLinus Torvalds
1041da177e4SLinus Torvalds skb_queue_tail(&sk->sk_write_queue, skb);
105aa395145SEric Dumazet wake_up_interruptible(sk_sleep(sk));
1061da177e4SLinus Torvalds #endif
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds
bnep_net_set_mac_addr(struct net_device * dev,void * arg)1091da177e4SLinus Torvalds static int bnep_net_set_mac_addr(struct net_device *dev, void *arg)
1101da177e4SLinus Torvalds {
1111da177e4SLinus Torvalds BT_DBG("%s", dev->name);
1121da177e4SLinus Torvalds return 0;
1131da177e4SLinus Torvalds }
1141da177e4SLinus Torvalds
bnep_net_timeout(struct net_device * dev,unsigned int txqueue)115*0290bd29SMichael S. Tsirkin static void bnep_net_timeout(struct net_device *dev, unsigned int txqueue)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds BT_DBG("net_timeout");
1181da177e4SLinus Torvalds netif_wake_queue(dev);
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds
1211da177e4SLinus Torvalds #ifdef CONFIG_BT_BNEP_MC_FILTER
bnep_net_mc_filter(struct sk_buff * skb,struct bnep_session * s)1226039aa73SGustavo Padovan static int bnep_net_mc_filter(struct sk_buff *skb, struct bnep_session *s)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds struct ethhdr *eh = (void *) skb->data;
1251da177e4SLinus Torvalds
1261da177e4SLinus Torvalds if ((eh->h_dest[0] & 1) && !test_bit(bnep_mc_hash(eh->h_dest), (ulong *) &s->mc_filter))
1271da177e4SLinus Torvalds return 1;
1281da177e4SLinus Torvalds return 0;
1291da177e4SLinus Torvalds }
1301da177e4SLinus Torvalds #endif
1311da177e4SLinus Torvalds
1321da177e4SLinus Torvalds #ifdef CONFIG_BT_BNEP_PROTO_FILTER
1331da177e4SLinus Torvalds /* Determine ether protocol. Based on eth_type_trans. */
bnep_net_eth_proto(struct sk_buff * skb)1346039aa73SGustavo Padovan static u16 bnep_net_eth_proto(struct sk_buff *skb)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds struct ethhdr *eh = (void *) skb->data;
137e41d2169SAl Viro u16 proto = ntohs(eh->h_proto);
1381da177e4SLinus Torvalds
139e5c5d22eSSimon Horman if (proto >= ETH_P_802_3_MIN)
140e41d2169SAl Viro return proto;
1411da177e4SLinus Torvalds
142e41d2169SAl Viro if (get_unaligned((__be16 *) skb->data) == htons(0xFFFF))
143e41d2169SAl Viro return ETH_P_802_3;
1441da177e4SLinus Torvalds
145e41d2169SAl Viro return ETH_P_802_2;
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds
bnep_net_proto_filter(struct sk_buff * skb,struct bnep_session * s)1486039aa73SGustavo Padovan static int bnep_net_proto_filter(struct sk_buff *skb, struct bnep_session *s)
1491da177e4SLinus Torvalds {
1501da177e4SLinus Torvalds u16 proto = bnep_net_eth_proto(skb);
1511da177e4SLinus Torvalds struct bnep_proto_filter *f = s->proto_filter;
1521da177e4SLinus Torvalds int i;
1531da177e4SLinus Torvalds
1541da177e4SLinus Torvalds for (i = 0; i < BNEP_MAX_PROTO_FILTERS && f[i].end; i++) {
1551da177e4SLinus Torvalds if (proto >= f[i].start && proto <= f[i].end)
1561da177e4SLinus Torvalds return 0;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds
1591da177e4SLinus Torvalds BT_DBG("BNEP: filtered skb %p, proto 0x%.4x", skb, proto);
1601da177e4SLinus Torvalds return 1;
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds #endif
1631da177e4SLinus Torvalds
bnep_net_xmit(struct sk_buff * skb,struct net_device * dev)1646fef4c0cSStephen Hemminger static netdev_tx_t bnep_net_xmit(struct sk_buff *skb,
1656fef4c0cSStephen Hemminger struct net_device *dev)
1661da177e4SLinus Torvalds {
167524ad0a7SWang Chen struct bnep_session *s = netdev_priv(dev);
1681da177e4SLinus Torvalds struct sock *sk = s->sock->sk;
1691da177e4SLinus Torvalds
1701da177e4SLinus Torvalds BT_DBG("skb %p, dev %p", skb, dev);
1711da177e4SLinus Torvalds
1721da177e4SLinus Torvalds #ifdef CONFIG_BT_BNEP_MC_FILTER
1731da177e4SLinus Torvalds if (bnep_net_mc_filter(skb, s)) {
1741da177e4SLinus Torvalds kfree_skb(skb);
1756ed10654SPatrick McHardy return NETDEV_TX_OK;
1761da177e4SLinus Torvalds }
1771da177e4SLinus Torvalds #endif
1781da177e4SLinus Torvalds
1791da177e4SLinus Torvalds #ifdef CONFIG_BT_BNEP_PROTO_FILTER
1801da177e4SLinus Torvalds if (bnep_net_proto_filter(skb, s)) {
1811da177e4SLinus Torvalds kfree_skb(skb);
1826ed10654SPatrick McHardy return NETDEV_TX_OK;
1831da177e4SLinus Torvalds }
1841da177e4SLinus Torvalds #endif
1851da177e4SLinus Torvalds
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds * We cannot send L2CAP packets from here as we are potentially in a bh.
1881da177e4SLinus Torvalds * So we have to queue them and wake up session thread which is sleeping
189aa395145SEric Dumazet * on the sk_sleep(sk).
1901da177e4SLinus Torvalds */
191860e9538SFlorian Westphal netif_trans_update(dev);
1921da177e4SLinus Torvalds skb_queue_tail(&sk->sk_write_queue, skb);
193aa395145SEric Dumazet wake_up_interruptible(sk_sleep(sk));
1941da177e4SLinus Torvalds
1951da177e4SLinus Torvalds if (skb_queue_len(&sk->sk_write_queue) >= BNEP_TX_QUEUE_LEN) {
1961da177e4SLinus Torvalds BT_DBG("tx queue is full");
1971da177e4SLinus Torvalds
1981da177e4SLinus Torvalds /* Stop queuing.
1991da177e4SLinus Torvalds * Session thread will do netif_wake_queue() */
2001da177e4SLinus Torvalds netif_stop_queue(dev);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds
2036ed10654SPatrick McHardy return NETDEV_TX_OK;
2041da177e4SLinus Torvalds }
2051da177e4SLinus Torvalds
206b4d7f0a4SStephen Hemminger static const struct net_device_ops bnep_netdev_ops = {
207b4d7f0a4SStephen Hemminger .ndo_open = bnep_net_open,
208b4d7f0a4SStephen Hemminger .ndo_stop = bnep_net_close,
209b4d7f0a4SStephen Hemminger .ndo_start_xmit = bnep_net_xmit,
210b4d7f0a4SStephen Hemminger .ndo_validate_addr = eth_validate_addr,
211afc4b13dSJiri Pirko .ndo_set_rx_mode = bnep_net_set_mc_list,
212b4d7f0a4SStephen Hemminger .ndo_set_mac_address = bnep_net_set_mac_addr,
213b4d7f0a4SStephen Hemminger .ndo_tx_timeout = bnep_net_timeout,
214b4d7f0a4SStephen Hemminger
215b4d7f0a4SStephen Hemminger };
216b4d7f0a4SStephen Hemminger
bnep_net_setup(struct net_device * dev)2171da177e4SLinus Torvalds void bnep_net_setup(struct net_device *dev)
2181da177e4SLinus Torvalds {
2191da177e4SLinus Torvalds
220211b8534SJoe Perches eth_broadcast_addr(dev->broadcast);
2211da177e4SLinus Torvalds dev->addr_len = ETH_ALEN;
2221da177e4SLinus Torvalds
2231da177e4SLinus Torvalds ether_setup(dev);
2248b1efc0fSJarod Wilson dev->min_mtu = 0;
2258b1efc0fSJarod Wilson dev->max_mtu = ETH_MAX_MTU;
226550fd08cSNeil Horman dev->priv_flags &= ~IFF_TX_SKB_SHARING;
227b4d7f0a4SStephen Hemminger dev->netdev_ops = &bnep_netdev_ops;
2281da177e4SLinus Torvalds
2291da177e4SLinus Torvalds dev->watchdog_timeo = HZ * 2;
2301da177e4SLinus Torvalds }
231