Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)    Copyright (c) 2013-2014 Intel Corp.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/if_arp.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/netdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include <linux/etherdevice.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include <linux/debugfs.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) #include <net/ipv6.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) #include <net/ip6_route.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) #include <net/addrconf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) #include <net/pkt_sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) #include <net/bluetooth/bluetooth.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <net/bluetooth/hci_core.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <net/bluetooth/l2cap.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <net/6lowpan.h> /* for the compression support */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #define VERSION "0.1"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) static struct dentry *lowpan_enable_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) static struct dentry *lowpan_control_debugfs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) #define IFACE_NAME_TEMPLATE "bt%d"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) struct skb_cb {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) 	struct in6_addr addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 	struct in6_addr gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 	struct l2cap_chan *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) /* The devices list contains those devices that we are acting
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39)  * as a proxy. The BT 6LoWPAN device is a virtual device that
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40)  * connects to the Bluetooth LE device. The real connection to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41)  * BT device is done via l2cap layer. There exists one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42)  * virtual device / one BT 6LoWPAN network (=hciX device).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43)  * The list contains struct lowpan_dev elements.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static LIST_HEAD(bt_6lowpan_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) static DEFINE_SPINLOCK(devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) static bool enable_6lowpan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) /* We are listening incoming connections via this channel
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) static struct l2cap_chan *listen_chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) static DEFINE_MUTEX(set_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) struct lowpan_peer {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	struct rcu_head rcu;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	struct l2cap_chan *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	/* peer addresses in various formats */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	unsigned char lladdr[ETH_ALEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	struct in6_addr peer_addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) struct lowpan_btle_dev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 	struct list_head list;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	struct net_device *netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	struct list_head peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) 	atomic_t peer_count; /* number of items in peers list */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) 	struct work_struct delete_netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	struct delayed_work notify_peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) static inline struct lowpan_btle_dev *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) lowpan_btle_dev(const struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) static inline void peer_add(struct lowpan_btle_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 			    struct lowpan_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 	list_add_rcu(&peer->list, &dev->peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) 	atomic_inc(&dev->peer_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) static inline bool peer_del(struct lowpan_btle_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 			    struct lowpan_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	list_del_rcu(&peer->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	kfree_rcu(peer, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 	module_put(THIS_MODULE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) 	if (atomic_dec_and_test(&dev->peer_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99) 		BT_DBG("last peer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 		return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_btle_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 						 bdaddr_t *ba, __u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) 	BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) 	       ba, type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) 	list_for_each_entry_rcu(peer, &dev->peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) 		BT_DBG("dst addr %pMR dst type %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) 		       &peer->chan->dst, peer->chan->dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) 		if (bacmp(&peer->chan->dst, ba))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 		if (type == peer->chan->dst_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 			rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 			return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) static inline struct lowpan_peer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) __peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	list_for_each_entry_rcu(peer, &dev->peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		if (peer->chan == chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 			return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) static inline struct lowpan_peer *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) __peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	list_for_each_entry_rcu(peer, &dev->peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		if (peer->chan->conn == conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 			return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 						  struct in6_addr *daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 						  struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 	struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 	int count = atomic_read(&dev->peer_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	const struct in6_addr *nexthop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	struct neighbour *neigh;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) 	BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) 	if (!rt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 		if (ipv6_addr_any(&lowpan_cb(skb)->gw)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 			/* There is neither route nor gateway,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 			 * probably the destination is a direct peer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 			nexthop = daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 			/* There is a known gateway
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 			nexthop = &lowpan_cb(skb)->gw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 		nexthop = rt6_nexthop(rt, daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		/* We need to remember the address because it is needed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		 * by bt_xmit() when sending the packet. In bt_xmit(), the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		 * destination routing info is not set.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 		memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 	BT_DBG("gw %pI6c", nexthop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 	list_for_each_entry_rcu(peer, &dev->peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		BT_DBG("dst addr %pMR dst type %d ip %pI6c",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 		       &peer->chan->dst, peer->chan->dst_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 		       &peer->peer_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 			rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 			return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	/* use the neighbour cache for matching addresses assigned by SLAAC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	neigh = __ipv6_neigh_lookup(dev->netdev, nexthop);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 	if (neigh) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		list_for_each_entry_rcu(peer, &dev->peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 			if (!memcmp(neigh->ha, peer->lladdr, ETH_ALEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 				neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 				rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 				return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		neigh_release(neigh);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	struct lowpan_btle_dev *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 	struct lowpan_peer *peer = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 		peer = __peer_lookup_conn(entry, conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 		if (peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	return peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	struct lowpan_btle_dev *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 	struct lowpan_btle_dev *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 		if (conn->hcon->hdev == entry->hdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 			dev = entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 	return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 	struct sk_buff *skb_cp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	skb_cp = skb_copy(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	if (!skb_cp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	return netif_rx_ni(skb_cp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 			   struct lowpan_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 	const u8 *saddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 	saddr = peer->lladdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 	return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 		    struct lowpan_peer *peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 	struct sk_buff *local_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 	if (!netif_running(dev))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	if (dev->type != ARPHRD_6LOWPAN || !skb->len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	skb_reset_network_header(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 	skb = skb_share_check(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	/* check that it's our buffer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 	if (lowpan_is_ipv6(*skb_network_header(skb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 		/* Pull off the 1-byte of 6lowpan header. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 		skb_pull(skb, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		/* Copy the packet so that the IPv6 header is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 		 * properly aligned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) 					    skb_tailroom(skb), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 		if (!local_skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 		local_skb->protocol = htons(ETH_P_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		local_skb->pkt_type = PACKET_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 		local_skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 		skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 		if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 			kfree_skb(local_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 		dev->stats.rx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 		dev->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		consume_skb(local_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	} else if (lowpan_is_iphc(*skb_network_header(skb))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 		local_skb = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 		if (!local_skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		local_skb->dev = dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 		ret = iphc_decompress(local_skb, dev, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 		if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 			BT_DBG("iphc_decompress failed: %d", ret);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 			kfree_skb(local_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		local_skb->protocol = htons(ETH_P_IPV6);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 		local_skb->pkt_type = PACKET_HOST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 		if (give_skb_to_upper(local_skb, dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 				!= NET_RX_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 			kfree_skb(local_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 		dev->stats.rx_bytes += skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 		dev->stats.rx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 		consume_skb(local_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 		consume_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 		BT_DBG("unknown packet type");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		goto drop;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 	return NET_RX_SUCCESS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) drop:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 	dev->stats.rx_dropped++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	return NET_RX_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) /* Packet from BT LE device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	struct lowpan_btle_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 	peer = lookup_peer(chan->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 	if (!peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	dev = lookup_dev(chan->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	if (!dev || !dev->netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 	err = recv_pkt(skb, dev->netdev, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 		BT_DBG("recv pkt %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		err = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) static int setup_header(struct sk_buff *skb, struct net_device *netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 			bdaddr_t *peer_addr, u8 *peer_addr_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 	struct in6_addr ipv6_daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 	struct ipv6hdr *hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 	struct lowpan_btle_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 	u8 *daddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 	int err, status = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 	hdr = ipv6_hdr(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 	dev = lowpan_btle_dev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 	memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 	if (ipv6_addr_is_multicast(&ipv6_daddr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 		lowpan_cb(skb)->chan = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 		daddr = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 		BT_DBG("dest IP %pI6c", &ipv6_daddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 		/* The packet might be sent to 6lowpan interface
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 		 * because of routing (either via default route
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 		 * or user set route) so get peer according to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 		 * the destination address.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 		peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 		if (!peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 			BT_DBG("no such peer");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 			return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 		daddr = peer->lladdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 		*peer_addr = peer->chan->dst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 		*peer_addr_type = peer->chan->dst_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		lowpan_cb(skb)->chan = peer->chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		status = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 	lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 	err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 	return status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) static int header_create(struct sk_buff *skb, struct net_device *netdev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 			 unsigned short type, const void *_daddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 			 const void *_saddr, unsigned int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	if (type != ETH_P_IPV6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) /* Packet to BT LE device */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		    struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 	struct msghdr msg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	struct kvec iv;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	/* Remember the skb so that we can send EAGAIN to the caller if
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 	 * we run out of credits.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	chan->data = skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	iv.iov_base = skb->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 	iv.iov_len = skb->len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	memset(&msg, 0, sizeof(msg));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	iov_iter_kvec(&msg.msg_iter, WRITE, &iv, 1, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	err = l2cap_chan_send(chan, &msg, skb->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	if (err > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 		netdev->stats.tx_bytes += err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 		netdev->stats.tx_packets++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		netdev->stats.tx_errors++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 	struct sk_buff *local_skb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 	struct lowpan_btle_dev *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 		struct lowpan_peer *pentry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 		struct lowpan_btle_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 		if (entry->netdev != netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 		dev = lowpan_btle_dev(entry->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		list_for_each_entry_rcu(pentry, &dev->peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 			int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 			local_skb = skb_clone(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 			BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 			       netdev->name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 			       &pentry->chan->dst, pentry->chan->dst_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			       &pentry->peer_addr, pentry->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 			ret = send_pkt(pentry->chan, local_skb, netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 				err = ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 			kfree_skb(local_skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 	bdaddr_t addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 	u8 addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	/* We must take a copy of the skb before we modify/replace the ipv6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 	 * header as the header could be used elsewhere
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	skb = skb_unshare(skb, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	if (!skb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) 		return NET_XMIT_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	/* Return values from setup_header()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	 *  <0 - error, packet is dropped
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) 	 *   0 - this is a multicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 	 *   1 - this is unicast packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) 	err = setup_header(skb, netdev, &addr, &addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 		kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 		return NET_XMIT_DROP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 		if (lowpan_cb(skb)->chan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 			BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 			       netdev->name, &addr, addr_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 			       &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 			err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 			err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		/* We need to send the packet to every device behind this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		 * interface.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		err = send_mcast_pkt(skb, netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 	dev_kfree_skb(skb);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 		BT_DBG("ERROR: xmit failed (%d)", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 	return err < 0 ? NET_XMIT_DROP : err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) static int bt_dev_init(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	netdev_lockdep_set_classes(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) static const struct net_device_ops netdev_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	.ndo_init		= bt_dev_init,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	.ndo_start_xmit		= bt_xmit,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) static const struct header_ops header_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	.create	= header_create,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) static void netdev_setup(struct net_device *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	dev->hard_header_len	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	dev->needed_tailroom	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 	dev->flags		= IFF_RUNNING | IFF_MULTICAST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	dev->watchdog_timeo	= 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 	dev->tx_queue_len	= DEFAULT_TX_QUEUE_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	dev->netdev_ops		= &netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	dev->header_ops		= &header_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 	dev->needs_free_netdev	= true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) static struct device_type bt_type = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 	.name	= "bluetooth",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) static void ifup(struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	err = dev_open(netdev, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) static void ifdown(struct net_device *netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	rtnl_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	dev_close(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	rtnl_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) static void do_notify_peers(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 	struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 						   notify_peers.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 	netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) static bool is_bt_6lowpan(struct hci_conn *hcon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	if (hcon->type != LE_LINK)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 	if (!enable_6lowpan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) static struct l2cap_chan *chan_create(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 	struct l2cap_chan *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	chan = l2cap_chan_create();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 	if (!chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 	l2cap_chan_set_defaults(chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 	chan->mode = L2CAP_MODE_LE_FLOWCTL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 	chan->imtu = 1280;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 	return chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 					struct lowpan_btle_dev *dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 					bool new_netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 	peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	if (!peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	peer->chan = chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 	memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	baswap((void *)peer->lladdr, &chan->dst);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 	lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 	spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	INIT_LIST_HEAD(&peer->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	peer_add(dev, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	/* Notifying peers about us needs to be done without locks held */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	if (new_netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 		INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 	return peer->chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	struct net_device *netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 	int err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 			      IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 			      netdev_setup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	if (!netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 	netdev->addr_assign_type = NET_ADDR_PERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 	baswap((void *)netdev->dev_addr, &chan->src);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	netdev->netdev_ops = &netdev_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	SET_NETDEV_DEVTYPE(netdev, &bt_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	*dev = lowpan_btle_dev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	(*dev)->netdev = netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	(*dev)->hdev = chan->conn->hcon->hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	INIT_LIST_HEAD(&(*dev)->peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 	INIT_LIST_HEAD(&(*dev)->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 	spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (err < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		BT_INFO("register_netdev failed %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 		spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 		list_del_rcu(&(*dev)->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 		spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 		free_netdev(netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 	       netdev->ifindex, &chan->dst, chan->dst_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	       &chan->src, chan->src_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	set_bit(__LINK_STATE_PRESENT, &netdev->state);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) static inline void chan_ready_cb(struct l2cap_chan *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743) 	struct lowpan_btle_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 	bool new_netdev = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	dev = lookup_dev(chan->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 	BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 		if (setup_netdev(chan, &dev) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 			l2cap_chan_del(chan, -ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 		new_netdev = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) 	if (!try_module_get(THIS_MODULE))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 	add_peer_chan(chan, dev, new_netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	ifup(dev->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 	struct l2cap_chan *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 	chan = chan_create();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	if (!chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	chan->ops = pchan->ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 	BT_DBG("chan %p pchan %p", chan, pchan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	return chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) static void delete_netdev(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	struct lowpan_btle_dev *entry = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 						     struct lowpan_btle_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 						     delete_netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 	lowpan_unregister_netdev(entry->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 	/* The entry pointer is deleted by the netdev destructor. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) static void chan_close_cb(struct l2cap_chan *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	struct lowpan_btle_dev *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 	struct lowpan_btle_dev *dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	int err = -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	bool last = false, remove = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	BT_DBG("chan %p conn %p", chan, chan->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 	if (chan->conn && chan->conn->hcon) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 		if (!is_bt_6lowpan(chan->conn->hcon))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 			return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		/* If conn is set, then the netdev is also there and we should
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		 * not remove it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		remove = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 	spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		dev = lowpan_btle_dev(entry->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		peer = __peer_lookup_chan(dev, chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 		if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 			last = peer_del(dev, peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 			err = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 			BT_DBG("dev %p removing %speer %p", dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 			       last ? "last " : "1 ", peer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 			BT_DBG("chan %p orig refcnt %d", chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 			       kref_read(&chan->kref));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 			l2cap_chan_put(chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	if (!err && last && dev && !atomic_read(&dev->peer_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 		spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 		cancel_delayed_work_sync(&dev->notify_peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		ifdown(dev->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		if (remove) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 			INIT_WORK(&entry->delete_netdev, delete_netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			schedule_work(&entry->delete_netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 		spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 	BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 	       state_to_string(state), err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 					 unsigned long hdr_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 					 unsigned long len, int nb)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 	/* Note that we must allocate using GFP_ATOMIC here as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	 * this function is called originally from netdev hard xmit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 	 * function in atomic context.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) static void chan_suspend_cb(struct l2cap_chan *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	struct lowpan_btle_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	BT_DBG("chan %p suspend", chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	dev = lookup_dev(chan->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	if (!dev || !dev->netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	netif_stop_queue(dev->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) static void chan_resume_cb(struct l2cap_chan *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	struct lowpan_btle_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	BT_DBG("chan %p resume", chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	dev = lookup_dev(chan->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 	if (!dev || !dev->netdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	netif_wake_queue(dev->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 	return L2CAP_CONN_TIMEOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) static const struct l2cap_ops bt_6lowpan_chan_ops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 	.name			= "L2CAP 6LoWPAN channel",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 	.new_connection		= chan_new_conn_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	.recv			= chan_recv_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	.close			= chan_close_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	.state_change		= chan_state_change_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	.ready			= chan_ready_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 	.resume			= chan_resume_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 	.suspend		= chan_suspend_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	.get_sndtimeo		= chan_get_sndtimeo_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	.alloc_skb		= chan_alloc_skb_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	.teardown		= l2cap_chan_no_teardown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 	.defer			= l2cap_chan_no_defer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	.set_shutdown		= l2cap_chan_no_set_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) static inline __u8 bdaddr_type(__u8 type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 	if (type == ADDR_LE_DEV_PUBLIC)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 		return BDADDR_LE_PUBLIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 		return BDADDR_LE_RANDOM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 	struct l2cap_chan *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	chan = chan_create();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	if (!chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 	chan->ops = &bt_6lowpan_chan_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 	err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 				 addr, dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 	BT_DBG("chan %p err %d", chan, err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	if (err < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 		l2cap_chan_put(chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	BT_DBG("conn %p dst type %d", conn, dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 	peer = lookup_peer(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	if (!peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	BT_DBG("peer %p chan %p", peer, peer->chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	l2cap_chan_close(peer->chan, ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) static struct l2cap_chan *bt_6lowpan_listen(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	bdaddr_t *addr = BDADDR_ANY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	struct l2cap_chan *chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	if (!enable_6lowpan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 	chan = chan_create();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 	if (!chan)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 	chan->ops = &bt_6lowpan_chan_ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 	chan->state = BT_LISTEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 	chan->src_type = BDADDR_LE_PUBLIC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 	atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 	BT_DBG("chan %p src type %d", chan, chan->src_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 		l2cap_chan_put(chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 		BT_ERR("psm cannot be added err %d", err);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 	return chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) 			  struct l2cap_conn **conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	struct hci_conn *hcon;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 	struct hci_dev *hdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 	int n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 		   &addr->b[5], &addr->b[4], &addr->b[3],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 		   &addr->b[2], &addr->b[1], &addr->b[0],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 		   addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 	if (n < 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	/* The LE_PUBLIC address type is ignored because of BDADDR_ANY */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 	hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	if (!hdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 	hci_dev_lock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 	hci_dev_unlock(hdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	if (!hcon)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 		return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	*conn = (struct l2cap_conn *)hcon->l2cap_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) static void disconnect_all_peers(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) 	struct lowpan_btle_dev *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	struct lowpan_peer *peer, *tmp_peer, *new_peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	struct list_head peers;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	INIT_LIST_HEAD(&peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 	/* We make a separate list of peers as the close_cb() will
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	 * modify the device peers list so it is better not to mess
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	 * with the same list at the same time.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		list_for_each_entry_rcu(peer, &entry->peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 			new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 			if (!new_peer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 			new_peer->chan = peer->chan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 			INIT_LIST_HEAD(&new_peer->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 			list_add(&new_peer->list, &peers);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 	spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 	list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		l2cap_chan_close(peer->chan, ENOENT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 		list_del_rcu(&peer->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 		kfree_rcu(peer, rcu);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) 	spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) struct set_enable {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) 	struct work_struct work;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	bool flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) static void do_enable_set(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	struct set_enable *set_enable = container_of(work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 						     struct set_enable, work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (!set_enable->flag || enable_6lowpan != set_enable->flag)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		/* Disconnect existing connections if 6lowpan is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 		 * disabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 		disconnect_all_peers();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	enable_6lowpan = set_enable->flag;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 	mutex_lock(&set_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	if (listen_chan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		l2cap_chan_close(listen_chan, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 		l2cap_chan_put(listen_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 	listen_chan = bt_6lowpan_listen();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 	mutex_unlock(&set_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 	kfree(set_enable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) static int lowpan_enable_set(void *data, u64 val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 	struct set_enable *set_enable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) 	set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 	if (!set_enable)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) 	set_enable->flag = !!val;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	INIT_WORK(&set_enable->work, do_enable_set);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	schedule_work(&set_enable->work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) static int lowpan_enable_get(void *data, u64 *val)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112) 	*val = enable_6lowpan;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116) DEFINE_DEBUGFS_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117) 			 lowpan_enable_set, "%llu\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119) static ssize_t lowpan_control_write(struct file *fp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120) 				    const char __user *user_buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) 				    size_t count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 				    loff_t *position)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	char buf[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) 	size_t buf_size = min(count, sizeof(buf) - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	bdaddr_t addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	u8 addr_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	struct l2cap_conn *conn = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	if (copy_from_user(buf, user_buffer, buf_size))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 		return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	buf[buf_size] = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	if (memcmp(buf, "connect ", 8) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 		ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 		if (ret == -EINVAL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 		mutex_lock(&set_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) 		if (listen_chan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143) 			l2cap_chan_close(listen_chan, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144) 			l2cap_chan_put(listen_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145) 			listen_chan = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) 		mutex_unlock(&set_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 		if (conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 			struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 			if (!is_bt_6lowpan(conn->hcon))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 				return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 			peer = lookup_peer(conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 			if (peer) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) 				BT_DBG("6LoWPAN connection already exists");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 				return -EALREADY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) 			BT_DBG("conn %p dst %pMR type %d user %d", conn,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 			       &conn->hcon->dst, conn->hcon->dst_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 			       addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 		ret = bt_6lowpan_connect(&addr, addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	if (memcmp(buf, "disconnect ", 11) == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 		ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) 		ret = bt_6lowpan_disconnect(conn, addr_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179) 		if (ret < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182) 		return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) 	return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) static int lowpan_control_show(struct seq_file *f, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	struct lowpan_btle_dev *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 	struct lowpan_peer *peer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 	spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 	list_for_each_entry(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		list_for_each_entry(peer, &entry->peers, list)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 			seq_printf(f, "%pMR (type %u)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 				   &peer->chan->dst, peer->chan->dst_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) 	spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206) static int lowpan_control_open(struct inode *inode, struct file *file)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208) 	return single_open(file, lowpan_control_show, inode->i_private);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) static const struct file_operations lowpan_control_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	.open		= lowpan_control_open,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 	.read		= seq_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	.write		= lowpan_control_write,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 	.llseek		= seq_lseek,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 	.release	= single_release,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) static void disconnect_devices(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	struct lowpan_btle_dev *entry, *tmp, *new_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 	struct list_head devices;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) 	INIT_LIST_HEAD(&devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) 	/* We make a separate list of devices because the unregister_netdev()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) 	 * will call device_event() which will also want to modify the same
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) 	 * devices list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 	rcu_read_lock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 		new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 		if (!new_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 		new_dev->netdev = entry->netdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 		INIT_LIST_HEAD(&new_dev->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 		list_add_rcu(&new_dev->list, &devices);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 	rcu_read_unlock();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 	list_for_each_entry_safe(entry, tmp, &devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 		ifdown(entry->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		BT_DBG("Unregistering netdev %s %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		       entry->netdev->name, entry->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 		lowpan_unregister_netdev(entry->netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 		kfree(entry);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) static int device_event(struct notifier_block *unused,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 			unsigned long event, void *ptr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) 	struct lowpan_btle_dev *entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 	if (netdev->type != ARPHRD_6LOWPAN)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 		return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 	switch (event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 	case NETDEV_UNREGISTER:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 		spin_lock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		list_for_each_entry(entry, &bt_6lowpan_devices, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 			if (entry->netdev == netdev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 				BT_DBG("Unregistered netdev %s %p",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) 				       netdev->name, netdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 				list_del(&entry->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 		spin_unlock(&devices_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	return NOTIFY_DONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) static struct notifier_block bt_6lowpan_dev_notifier = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) 	.notifier_call = device_event,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) static int __init bt_6lowpan_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 	lowpan_enable_debugfs = debugfs_create_file_unsafe("6lowpan_enable",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 							   0644, bt_debugfs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 							   NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 							   &lowpan_enable_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 	lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 						     bt_debugfs, NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 						     &lowpan_control_fops);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) static void __exit bt_6lowpan_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	debugfs_remove(lowpan_enable_debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	debugfs_remove(lowpan_control_debugfs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) 	if (listen_chan) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 		l2cap_chan_close(listen_chan, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) 		l2cap_chan_put(listen_chan);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309) 	disconnect_devices();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) 	unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) module_init(bt_6lowpan_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) module_exit(bt_6lowpan_exit);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) MODULE_VERSION(VERSION);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) MODULE_LICENSE("GPL");