blob: 19d27bee285e7201a3518702e6c3aa743a6aa8cd [file] [log] [blame]
Thomas Gleixner97fb5e82019-05-29 07:17:58 -07001// SPDX-License-Identifier: GPL-2.0-only
Jukka Rissanen18722c22013-12-11 17:05:37 +02002/*
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03003 Copyright (c) 2013-2014 Intel Corp.
Jukka Rissanen18722c22013-12-11 17:05:37 +02004
Jukka Rissanen18722c22013-12-11 17:05:37 +02005*/
6
Jukka Rissanen18722c22013-12-11 17:05:37 +02007#include <linux/if_arp.h>
8#include <linux/netdevice.h>
9#include <linux/etherdevice.h>
Jukka Rissanen5547e482014-06-18 16:37:09 +030010#include <linux/module.h>
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030011#include <linux/debugfs.h>
Jukka Rissanen18722c22013-12-11 17:05:37 +020012
13#include <net/ipv6.h>
14#include <net/ip6_route.h>
15#include <net/addrconf.h>
Luiz Augusto von Dentz814f1b22017-04-11 22:21:02 +030016#include <net/pkt_sched.h>
Jukka Rissanen18722c22013-12-11 17:05:37 +020017
Jukka Rissanen18722c22013-12-11 17:05:37 +020018#include <net/bluetooth/bluetooth.h>
19#include <net/bluetooth/hci_core.h>
20#include <net/bluetooth/l2cap.h>
21
Alexander Aringcefc8c82014-03-05 14:29:05 +010022#include <net/6lowpan.h> /* for the compression support */
Jukka Rissanen18722c22013-12-11 17:05:37 +020023
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030024#define VERSION "0.1"
25
Jukka Rissanen7b2ed602015-01-08 17:00:55 +020026static struct dentry *lowpan_enable_debugfs;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030027static struct dentry *lowpan_control_debugfs;
28
Jukka Rissanen18722c22013-12-11 17:05:37 +020029#define IFACE_NAME_TEMPLATE "bt%d"
Jukka Rissanen18722c22013-12-11 17:05:37 +020030
31struct skb_cb {
32 struct in6_addr addr;
Jukka Rissanen39e90c72014-09-08 12:11:45 +030033 struct in6_addr gw;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030034 struct l2cap_chan *chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +020035};
36#define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
37
38/* The devices list contains those devices that we are acting
39 * as a proxy. The BT 6LoWPAN device is a virtual device that
40 * connects to the Bluetooth LE device. The real connection to
41 * BT device is done via l2cap layer. There exists one
42 * virtual device / one BT 6LoWPAN network (=hciX device).
43 * The list contains struct lowpan_dev elements.
44 */
45static LIST_HEAD(bt_6lowpan_devices);
Jukka Rissanen90305822014-10-28 17:16:47 +020046static DEFINE_SPINLOCK(devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +020047
Jukka Rissanen7b2ed602015-01-08 17:00:55 +020048static bool enable_6lowpan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030049
50/* We are listening incoming connections via this channel
51 */
52static struct l2cap_chan *listen_chan;
53
Jukka Rissanen18722c22013-12-11 17:05:37 +020054struct lowpan_peer {
55 struct list_head list;
Jukka Rissanen90305822014-10-28 17:16:47 +020056 struct rcu_head rcu;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030057 struct l2cap_chan *chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +020058
59 /* peer addresses in various formats */
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +020060 unsigned char lladdr[ETH_ALEN];
Jukka Rissanen18722c22013-12-11 17:05:37 +020061 struct in6_addr peer_addr;
62};
63
Alexander Aring2e4d60c2016-04-11 11:04:18 +020064struct lowpan_btle_dev {
Jukka Rissanen18722c22013-12-11 17:05:37 +020065 struct list_head list;
66
67 struct hci_dev *hdev;
68 struct net_device *netdev;
69 struct list_head peers;
70 atomic_t peer_count; /* number of items in peers list */
71
72 struct work_struct delete_netdev;
73 struct delayed_work notify_peers;
74};
75
Alexander Aring2e4d60c2016-04-11 11:04:18 +020076static inline struct lowpan_btle_dev *
77lowpan_btle_dev(const struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +020078{
Alexander Aring2e4d60c2016-04-11 11:04:18 +020079 return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv;
Jukka Rissanen18722c22013-12-11 17:05:37 +020080}
81
Alexander Aring2e4d60c2016-04-11 11:04:18 +020082static inline void peer_add(struct lowpan_btle_dev *dev,
83 struct lowpan_peer *peer)
Jukka Rissanen18722c22013-12-11 17:05:37 +020084{
Jukka Rissanen90305822014-10-28 17:16:47 +020085 list_add_rcu(&peer->list, &dev->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +020086 atomic_inc(&dev->peer_count);
87}
88
Alexander Aring2e4d60c2016-04-11 11:04:18 +020089static inline bool peer_del(struct lowpan_btle_dev *dev,
90 struct lowpan_peer *peer)
Jukka Rissanen18722c22013-12-11 17:05:37 +020091{
Jukka Rissanen90305822014-10-28 17:16:47 +020092 list_del_rcu(&peer->list);
Johan Hedberg4e790222014-11-11 14:16:29 +020093 kfree_rcu(peer, rcu);
Jukka Rissanen18722c22013-12-11 17:05:37 +020094
Jukka Rissanen18d93c12014-06-18 16:37:10 +030095 module_put(THIS_MODULE);
96
Jukka Rissanen18722c22013-12-11 17:05:37 +020097 if (atomic_dec_and_test(&dev->peer_count)) {
98 BT_DBG("last peer");
99 return true;
100 }
101
102 return false;
103}
104
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200105static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_btle_dev *dev,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200106 bdaddr_t *ba, __u8 type)
107{
Jukka Rissanen90305822014-10-28 17:16:47 +0200108 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200109
110 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
111 ba, type);
112
Jukka Rissanen90305822014-10-28 17:16:47 +0200113 rcu_read_lock();
114
115 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300116 BT_DBG("dst addr %pMR dst type %d",
117 &peer->chan->dst, peer->chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200118
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300119 if (bacmp(&peer->chan->dst, ba))
Jukka Rissanen18722c22013-12-11 17:05:37 +0200120 continue;
121
Jukka Rissanen90305822014-10-28 17:16:47 +0200122 if (type == peer->chan->dst_type) {
123 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300124 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200125 }
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300126 }
127
Jukka Rissanen90305822014-10-28 17:16:47 +0200128 rcu_read_unlock();
129
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300130 return NULL;
131}
132
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200133static inline struct lowpan_peer *
134__peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300135{
Jukka Rissanen90305822014-10-28 17:16:47 +0200136 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300137
Jukka Rissanen90305822014-10-28 17:16:47 +0200138 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300139 if (peer->chan == chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200140 return peer;
141 }
142
143 return NULL;
144}
145
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200146static inline struct lowpan_peer *
147__peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200148{
Jukka Rissanen90305822014-10-28 17:16:47 +0200149 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200150
Jukka Rissanen90305822014-10-28 17:16:47 +0200151 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300152 if (peer->chan->conn == conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200153 return peer;
154 }
155
156 return NULL;
157}
158
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200159static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev,
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300160 struct in6_addr *daddr,
161 struct sk_buff *skb)
162{
Jukka Rissanen90305822014-10-28 17:16:47 +0200163 struct lowpan_peer *peer;
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300164 struct in6_addr *nexthop;
165 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
166 int count = atomic_read(&dev->peer_count);
167
168 BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
169
170 /* If we have multiple 6lowpan peers, then check where we should
171 * send the packet. If only one peer exists, then we can send the
172 * packet right away.
173 */
Jukka Rissanen90305822014-10-28 17:16:47 +0200174 if (count == 1) {
175 rcu_read_lock();
176 peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer,
177 list);
178 rcu_read_unlock();
179 return peer;
180 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300181
182 if (!rt) {
183 nexthop = &lowpan_cb(skb)->gw;
184
185 if (ipv6_addr_any(nexthop))
186 return NULL;
187 } else {
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700188 nexthop = rt6_nexthop(rt, daddr);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300189
190 /* We need to remember the address because it is needed
191 * by bt_xmit() when sending the packet. In bt_xmit(), the
192 * destination routing info is not set.
193 */
194 memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
195 }
196
197 BT_DBG("gw %pI6c", nexthop);
198
Jukka Rissanen90305822014-10-28 17:16:47 +0200199 rcu_read_lock();
200
201 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300202 BT_DBG("dst addr %pMR dst type %d ip %pI6c",
203 &peer->chan->dst, peer->chan->dst_type,
204 &peer->peer_addr);
205
Jukka Rissanen90305822014-10-28 17:16:47 +0200206 if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
207 rcu_read_unlock();
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300208 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200209 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300210 }
211
Jukka Rissanen90305822014-10-28 17:16:47 +0200212 rcu_read_unlock();
213
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300214 return NULL;
215}
216
Jukka Rissanen18722c22013-12-11 17:05:37 +0200217static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
218{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200219 struct lowpan_btle_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200220 struct lowpan_peer *peer = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200221
Jukka Rissanen90305822014-10-28 17:16:47 +0200222 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200223
Jukka Rissanen90305822014-10-28 17:16:47 +0200224 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
225 peer = __peer_lookup_conn(entry, conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200226 if (peer)
227 break;
228 }
229
Jukka Rissanen90305822014-10-28 17:16:47 +0200230 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200231
232 return peer;
233}
234
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200235static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200236{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200237 struct lowpan_btle_dev *entry;
238 struct lowpan_btle_dev *dev = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200239
Jukka Rissanen90305822014-10-28 17:16:47 +0200240 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200241
Jukka Rissanen90305822014-10-28 17:16:47 +0200242 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200243 if (conn->hcon->hdev == entry->hdev) {
244 dev = entry;
245 break;
246 }
247 }
248
Jukka Rissanen90305822014-10-28 17:16:47 +0200249 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200250
251 return dev;
252}
253
Jukka Rissanen18722c22013-12-11 17:05:37 +0200254static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
255{
256 struct sk_buff *skb_cp;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200257
258 skb_cp = skb_copy(skb, GFP_ATOMIC);
259 if (!skb_cp)
Martin Townsendf8b36172014-10-23 15:40:53 +0100260 return NET_RX_DROP;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200261
Alexander Aring324e7862015-10-27 08:35:24 +0100262 return netif_rx_ni(skb_cp);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200263}
264
Martin Townsend01141232014-10-23 15:40:56 +0100265static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
Luiz Augusto von Dentz27ce68a2017-04-03 17:48:55 +0300266 struct lowpan_peer *peer)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200267{
Patrik Flyktc259d142017-03-12 10:19:33 +0200268 const u8 *saddr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200269
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200270 saddr = peer->lladdr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200271
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200272 return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200273}
274
275static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
Luiz Augusto von Dentz27ce68a2017-04-03 17:48:55 +0300276 struct lowpan_peer *peer)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200277{
278 struct sk_buff *local_skb;
279 int ret;
280
281 if (!netif_running(dev))
282 goto drop;
283
Alexander Aringcefdb802015-10-13 13:42:55 +0200284 if (dev->type != ARPHRD_6LOWPAN || !skb->len)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200285 goto drop;
286
Alexander Aringcefdb802015-10-13 13:42:55 +0200287 skb_reset_network_header(skb);
288
Martin Townsend11e3ff702014-10-13 11:00:56 +0100289 skb = skb_share_check(skb, GFP_ATOMIC);
290 if (!skb)
291 goto drop;
292
Jukka Rissanen18722c22013-12-11 17:05:37 +0200293 /* check that it's our buffer */
Alexander Aringcefdb802015-10-13 13:42:55 +0200294 if (lowpan_is_ipv6(*skb_network_header(skb))) {
Lukasz Duda87f5fed2016-01-13 16:57:48 +0100295 /* Pull off the 1-byte of 6lowpan header. */
296 skb_pull(skb, 1);
297
Jukka Rissanen18722c22013-12-11 17:05:37 +0200298 /* Copy the packet so that the IPv6 header is
299 * properly aligned.
300 */
301 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
302 skb_tailroom(skb), GFP_ATOMIC);
303 if (!local_skb)
304 goto drop;
305
306 local_skb->protocol = htons(ETH_P_IPV6);
307 local_skb->pkt_type = PACKET_HOST;
Glenn Ruben Bakke4c58f322016-01-13 16:41:42 +0100308 local_skb->dev = dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200309
Jukka Rissanen18722c22013-12-11 17:05:37 +0200310 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
311
312 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
313 kfree_skb(local_skb);
314 goto drop;
315 }
316
317 dev->stats.rx_bytes += skb->len;
318 dev->stats.rx_packets++;
319
Martin Townsend3c400b82014-10-23 15:40:55 +0100320 consume_skb(local_skb);
321 consume_skb(skb);
Alexander Aringcefdb802015-10-13 13:42:55 +0200322 } else if (lowpan_is_iphc(*skb_network_header(skb))) {
323 local_skb = skb_clone(skb, GFP_ATOMIC);
324 if (!local_skb)
325 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200326
Glenn Ruben Bakke4c58f322016-01-13 16:41:42 +0100327 local_skb->dev = dev;
328
Luiz Augusto von Dentz27ce68a2017-04-03 17:48:55 +0300329 ret = iphc_decompress(local_skb, dev, peer);
Alexander Aringcefdb802015-10-13 13:42:55 +0200330 if (ret < 0) {
Luiz Augusto von Dentzda75fdc2017-04-03 17:48:56 +0300331 BT_DBG("iphc_decompress failed: %d", ret);
Alexander Aringcefdb802015-10-13 13:42:55 +0200332 kfree_skb(local_skb);
333 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200334 }
Alexander Aringcefdb802015-10-13 13:42:55 +0200335
336 local_skb->protocol = htons(ETH_P_IPV6);
337 local_skb->pkt_type = PACKET_HOST;
Alexander Aringcefdb802015-10-13 13:42:55 +0200338
339 if (give_skb_to_upper(local_skb, dev)
340 != NET_RX_SUCCESS) {
341 kfree_skb(local_skb);
342 goto drop;
343 }
344
345 dev->stats.rx_bytes += skb->len;
346 dev->stats.rx_packets++;
347
348 consume_skb(local_skb);
349 consume_skb(skb);
350 } else {
Luiz Augusto von Dentzda75fdc2017-04-03 17:48:56 +0300351 BT_DBG("unknown packet type");
Alexander Aringcefdb802015-10-13 13:42:55 +0200352 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200353 }
354
355 return NET_RX_SUCCESS;
356
357drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300358 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200359 return NET_RX_DROP;
360}
361
362/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300363static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200364{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200365 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200366 struct lowpan_peer *peer;
367 int err;
368
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300369 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200370 if (!peer)
371 return -ENOENT;
372
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300373 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200374 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200375 return -ENOENT;
376
Luiz Augusto von Dentz27ce68a2017-04-03 17:48:55 +0300377 err = recv_pkt(skb, dev->netdev, peer);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300378 if (err) {
379 BT_DBG("recv pkt %d", err);
380 err = -EAGAIN;
381 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200382
383 return err;
384}
385
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300386static int setup_header(struct sk_buff *skb, struct net_device *netdev,
387 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200388{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300389 struct in6_addr ipv6_daddr;
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200390 struct ipv6hdr *hdr;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200391 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200392 struct lowpan_peer *peer;
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200393 u8 *daddr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300394 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200395
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200396 hdr = ipv6_hdr(skb);
397
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200398 dev = lowpan_btle_dev(netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200399
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200400 memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr));
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300401
402 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300403 lowpan_cb(skb)->chan = NULL;
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200404 daddr = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200405 } else {
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200406 BT_DBG("dest IP %pI6c", &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200407
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200408 /* The packet might be sent to 6lowpan interface
409 * because of routing (either via default route
410 * or user set route) so get peer according to
411 * the destination address.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200412 */
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200413 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200414 if (!peer) {
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200415 BT_DBG("no such peer");
416 return -ENOENT;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200417 }
418
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200419 daddr = peer->lladdr;
Colin Ian Kingfa0eaf82017-03-28 13:11:29 +0100420 *peer_addr = peer->chan->dst;
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200421 *peer_addr_type = peer->chan->dst_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300422 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300423
424 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200425 }
426
Alexander Aringa6f77382015-10-13 13:42:57 +0200427 lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200428
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300429 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
430 if (err < 0)
431 return err;
432
433 return status;
434}
435
436static int header_create(struct sk_buff *skb, struct net_device *netdev,
437 unsigned short type, const void *_daddr,
438 const void *_saddr, unsigned int len)
439{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300440 if (type != ETH_P_IPV6)
441 return -EINVAL;
442
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300443 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200444}
445
446/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300447static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300448 struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200449{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300450 struct msghdr msg;
451 struct kvec iv;
452 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200453
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300454 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300455 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300456 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300457 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300458
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300459 iv.iov_base = skb->data;
460 iv.iov_len = skb->len;
461
Al Viroc0371da2014-11-24 10:42:55 -0500462 memset(&msg, 0, sizeof(msg));
David Howellsaa563d72018-10-20 00:57:56 +0100463 iov_iter_kvec(&msg.msg_iter, WRITE, &iv, 1, skb->len);
Al Viroc0371da2014-11-24 10:42:55 -0500464
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300465 err = l2cap_chan_send(chan, &msg, skb->len);
466 if (err > 0) {
467 netdev->stats.tx_bytes += err;
468 netdev->stats.tx_packets++;
469 return 0;
470 }
471
Luiz Augusto von Dentze1008f92017-04-11 22:20:59 +0300472 if (err < 0)
473 netdev->stats.tx_errors++;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300474
475 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200476}
477
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300478static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200479{
480 struct sk_buff *local_skb;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200481 struct lowpan_btle_dev *entry;
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300482 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200483
Jukka Rissanen90305822014-10-28 17:16:47 +0200484 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200485
Jukka Rissanen90305822014-10-28 17:16:47 +0200486 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
487 struct lowpan_peer *pentry;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200488 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200489
490 if (entry->netdev != netdev)
491 continue;
492
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200493 dev = lowpan_btle_dev(entry->netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200494
Jukka Rissanen90305822014-10-28 17:16:47 +0200495 list_for_each_entry_rcu(pentry, &dev->peers, list) {
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300496 int ret;
497
Jukka Rissanen18722c22013-12-11 17:05:37 +0200498 local_skb = skb_clone(skb, GFP_ATOMIC);
499
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300500 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
501 netdev->name,
502 &pentry->chan->dst, pentry->chan->dst_type,
503 &pentry->peer_addr, pentry->chan);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300504 ret = send_pkt(pentry->chan, local_skb, netdev);
505 if (ret < 0)
506 err = ret;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200507
508 kfree_skb(local_skb);
509 }
510 }
511
Jukka Rissanen90305822014-10-28 17:16:47 +0200512 rcu_read_unlock();
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300513
514 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200515}
516
517static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
518{
519 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200520 bdaddr_t addr;
521 u8 addr_type;
522
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300523 /* We must take a copy of the skb before we modify/replace the ipv6
524 * header as the header could be used elsewhere
525 */
Alexander Aringb0c42cd2014-10-08 10:24:53 +0200526 skb = skb_unshare(skb, GFP_ATOMIC);
527 if (!skb)
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300528 return NET_XMIT_DROP;
529
530 /* Return values from setup_header()
531 * <0 - error, packet is dropped
532 * 0 - this is a multicast packet
533 * 1 - this is unicast packet
534 */
535 err = setup_header(skb, netdev, &addr, &addr_type);
536 if (err < 0) {
537 kfree_skb(skb);
538 return NET_XMIT_DROP;
539 }
540
541 if (err) {
542 if (lowpan_cb(skb)->chan) {
543 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
544 netdev->name, &addr, addr_type,
545 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300546 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300547 } else {
548 err = -ENOENT;
549 }
550 } else {
551 /* We need to send the packet to every device behind this
552 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200553 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300554 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200555 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200556
Jukka Rissanenfc125182014-10-01 11:30:26 +0300557 dev_kfree_skb(skb);
558
Jukka Rissanen18722c22013-12-11 17:05:37 +0200559 if (err)
560 BT_DBG("ERROR: xmit failed (%d)", err);
561
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300562 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200563}
564
Jukka Rissanendf092302014-10-28 17:16:48 +0200565static int bt_dev_init(struct net_device *dev)
566{
Eric Dumazetd3fff6c2016-06-09 07:45:12 -0700567 netdev_lockdep_set_classes(dev);
Jukka Rissanendf092302014-10-28 17:16:48 +0200568
569 return 0;
570}
571
Jukka Rissanen18722c22013-12-11 17:05:37 +0200572static const struct net_device_ops netdev_ops = {
Jukka Rissanendf092302014-10-28 17:16:48 +0200573 .ndo_init = bt_dev_init,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200574 .ndo_start_xmit = bt_xmit,
575};
576
577static struct header_ops header_ops = {
578 .create = header_create,
579};
580
581static void netdev_setup(struct net_device *dev)
582{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200583 dev->hard_header_len = 0;
584 dev->needed_tailroom = 0;
Patrik Flykt25869522017-04-11 22:21:03 +0300585 dev->flags = IFF_RUNNING | IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200586 dev->watchdog_timeo = 0;
Luiz Augusto von Dentz814f1b22017-04-11 22:21:02 +0300587 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200588
589 dev->netdev_ops = &netdev_ops;
590 dev->header_ops = &header_ops;
David S. Millercf124db2017-05-08 12:52:56 -0400591 dev->needs_free_netdev = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200592}
593
594static struct device_type bt_type = {
595 .name = "bluetooth",
596};
597
Jukka Rissanen18722c22013-12-11 17:05:37 +0200598static void ifup(struct net_device *netdev)
599{
600 int err;
601
602 rtnl_lock();
Petr Machata00f54e62018-12-06 17:05:36 +0000603 err = dev_open(netdev, NULL);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200604 if (err < 0)
605 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
606 rtnl_unlock();
607}
608
Jukka Rissanen7f118252014-06-18 16:37:11 +0300609static void ifdown(struct net_device *netdev)
610{
Jukka Rissanen7f118252014-06-18 16:37:11 +0300611 rtnl_lock();
stephen hemmingerddee3102017-07-18 15:59:25 -0700612 dev_close(netdev);
Jukka Rissanen7f118252014-06-18 16:37:11 +0300613 rtnl_unlock();
614}
615
Jukka Rissanen18722c22013-12-11 17:05:37 +0200616static void do_notify_peers(struct work_struct *work)
617{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200618 struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev,
619 notify_peers.work);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200620
621 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
622}
623
624static bool is_bt_6lowpan(struct hci_conn *hcon)
625{
626 if (hcon->type != LE_LINK)
627 return false;
628
Jukka Rissanen7b2ed602015-01-08 17:00:55 +0200629 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300630 return false;
631
632 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200633}
634
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300635static struct l2cap_chan *chan_create(void)
636{
637 struct l2cap_chan *chan;
638
639 chan = l2cap_chan_create();
640 if (!chan)
641 return NULL;
642
643 l2cap_chan_set_defaults(chan);
644
645 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
646 chan->mode = L2CAP_MODE_LE_FLOWCTL;
Johan Hedberg301de2c2015-10-06 13:03:19 +0300647 chan->imtu = 1280;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300648
649 return chan;
650}
651
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300652static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
Michael Scottd2891c42017-03-28 23:10:54 -0700653 struct lowpan_btle_dev *dev,
654 bool new_netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200655{
656 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200657
658 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
659 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300660 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200661
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300662 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200663 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
664
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200665 baswap((void *)peer->lladdr, &chan->dst);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200666
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200667 lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200668
Jukka Rissanen90305822014-10-28 17:16:47 +0200669 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200670 INIT_LIST_HEAD(&peer->list);
671 peer_add(dev, peer);
Jukka Rissanen90305822014-10-28 17:16:47 +0200672 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200673
674 /* Notifying peers about us needs to be done without locks held */
Michael Scottd2891c42017-03-28 23:10:54 -0700675 if (new_netdev)
676 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200677 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
678
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300679 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200680}
681
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200682static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200683{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200684 struct net_device *netdev;
685 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200686
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200687 netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)),
Alexander Aringb72f6f52015-08-11 21:44:08 +0200688 IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
689 netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200690 if (!netdev)
691 return -ENOMEM;
692
Patrik Flyktc259d142017-03-12 10:19:33 +0200693 netdev->addr_assign_type = NET_ADDR_PERM;
694 baswap((void *)netdev->dev_addr, &chan->src);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200695
696 netdev->netdev_ops = &netdev_ops;
Glenn Ruben Bakkefc842422015-06-17 07:32:25 -0700697 SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200698 SET_NETDEV_DEVTYPE(netdev, &bt_type);
699
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200700 *dev = lowpan_btle_dev(netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300701 (*dev)->netdev = netdev;
702 (*dev)->hdev = chan->conn->hcon->hdev;
703 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200704
Jukka Rissanen90305822014-10-28 17:16:47 +0200705 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300706 INIT_LIST_HEAD(&(*dev)->list);
Jukka Rissanen90305822014-10-28 17:16:47 +0200707 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
708 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200709
Alexander Aring00f59312015-12-09 22:46:29 +0100710 err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
Alexander Aring5857d1d2015-07-30 09:40:53 +0200711 if (err < 0) {
712 BT_INFO("register_netdev failed %d", err);
713 spin_lock(&devices_lock);
714 list_del_rcu(&(*dev)->list);
715 spin_unlock(&devices_lock);
716 free_netdev(netdev);
717 goto out;
718 }
719
720 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
721 netdev->ifindex, &chan->dst, chan->dst_type,
722 &chan->src, chan->src_type);
723 set_bit(__LINK_STATE_PRESENT, &netdev->state);
724
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300725 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200726
727out:
728 return err;
729}
730
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300731static inline void chan_ready_cb(struct l2cap_chan *chan)
732{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200733 struct lowpan_btle_dev *dev;
Michael Scottd2891c42017-03-28 23:10:54 -0700734 bool new_netdev = false;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300735
736 dev = lookup_dev(chan->conn);
737
738 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
739
740 if (!dev) {
741 if (setup_netdev(chan, &dev) < 0) {
742 l2cap_chan_del(chan, -ENOENT);
743 return;
744 }
Michael Scottd2891c42017-03-28 23:10:54 -0700745 new_netdev = true;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300746 }
747
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300748 if (!try_module_get(THIS_MODULE))
749 return;
750
Michael Scottd2891c42017-03-28 23:10:54 -0700751 add_peer_chan(chan, dev, new_netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300752 ifup(dev->netdev);
753}
754
Johan Hedberg2b293492014-08-07 10:03:32 +0300755static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300756{
Johan Hedberg2b293492014-08-07 10:03:32 +0300757 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300758
Johan Hedberg630ef792015-10-06 13:03:22 +0300759 chan = chan_create();
760 if (!chan)
761 return NULL;
762
Johan Hedberg2b293492014-08-07 10:03:32 +0300763 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300764
765 BT_DBG("chan %p pchan %p", chan, pchan);
766
Johan Hedberg2b293492014-08-07 10:03:32 +0300767 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300768}
769
Jukka Rissanen18722c22013-12-11 17:05:37 +0200770static void delete_netdev(struct work_struct *work)
771{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200772 struct lowpan_btle_dev *entry = container_of(work,
773 struct lowpan_btle_dev,
774 delete_netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200775
Alexander Aring00f59312015-12-09 22:46:29 +0100776 lowpan_unregister_netdev(entry->netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200777
Glenn Ruben Bakke2ad88fb2015-06-17 07:32:26 -0700778 /* The entry pointer is deleted by the netdev destructor. */
Jukka Rissanen18722c22013-12-11 17:05:37 +0200779}
780
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300781static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200782{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200783 struct lowpan_btle_dev *entry;
784 struct lowpan_btle_dev *dev = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200785 struct lowpan_peer *peer;
786 int err = -ENOENT;
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700787 bool last = false, remove = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200788
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300789 BT_DBG("chan %p conn %p", chan, chan->conn);
790
791 if (chan->conn && chan->conn->hcon) {
792 if (!is_bt_6lowpan(chan->conn->hcon))
793 return;
794
795 /* If conn is set, then the netdev is also there and we should
796 * not remove it.
797 */
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700798 remove = false;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300799 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200800
Jukka Rissanen90305822014-10-28 17:16:47 +0200801 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200802
Jukka Rissanen90305822014-10-28 17:16:47 +0200803 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200804 dev = lowpan_btle_dev(entry->netdev);
Jukka Rissanen90305822014-10-28 17:16:47 +0200805 peer = __peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200806 if (peer) {
807 last = peer_del(dev, peer);
808 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300809
810 BT_DBG("dev %p removing %speer %p", dev,
811 last ? "last " : "1 ", peer);
812 BT_DBG("chan %p orig refcnt %d", chan,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100813 kref_read(&chan->kref));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300814
815 l2cap_chan_put(chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200816 break;
817 }
818 }
819
820 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
Jukka Rissanen90305822014-10-28 17:16:47 +0200821 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200822
823 cancel_delayed_work_sync(&dev->notify_peers);
824
Jukka Rissanen7f118252014-06-18 16:37:11 +0300825 ifdown(dev->netdev);
826
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700827 if (remove) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300828 INIT_WORK(&entry->delete_netdev, delete_netdev);
829 schedule_work(&entry->delete_netdev);
830 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200831 } else {
Jukka Rissanen90305822014-10-28 17:16:47 +0200832 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200833 }
834
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300835 return;
836}
837
838static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
839{
840 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
841 state_to_string(state), err);
842}
843
844static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
845 unsigned long hdr_len,
846 unsigned long len, int nb)
847{
848 /* Note that we must allocate using GFP_ATOMIC here as
849 * this function is called originally from netdev hard xmit
850 * function in atomic context.
851 */
852 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
853}
854
855static void chan_suspend_cb(struct l2cap_chan *chan)
856{
Luiz Augusto von Dentzf183e522017-04-11 22:21:00 +0300857 struct lowpan_btle_dev *dev;
858
Michael Scott6dea44f2017-03-28 23:10:18 -0700859 BT_DBG("chan %p suspend", chan);
Luiz Augusto von Dentzf183e522017-04-11 22:21:00 +0300860
861 dev = lookup_dev(chan->conn);
862 if (!dev || !dev->netdev)
863 return;
864
865 netif_stop_queue(dev->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300866}
867
868static void chan_resume_cb(struct l2cap_chan *chan)
869{
Luiz Augusto von Dentzf183e522017-04-11 22:21:00 +0300870 struct lowpan_btle_dev *dev;
871
Michael Scott6dea44f2017-03-28 23:10:18 -0700872 BT_DBG("chan %p resume", chan);
Luiz Augusto von Dentzf183e522017-04-11 22:21:00 +0300873
874 dev = lookup_dev(chan->conn);
875 if (!dev || !dev->netdev)
876 return;
877
878 netif_wake_queue(dev->netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300879}
880
881static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
882{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +0300883 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300884}
885
886static const struct l2cap_ops bt_6lowpan_chan_ops = {
887 .name = "L2CAP 6LoWPAN channel",
888 .new_connection = chan_new_conn_cb,
889 .recv = chan_recv_cb,
890 .close = chan_close_cb,
891 .state_change = chan_state_change_cb,
892 .ready = chan_ready_cb,
893 .resume = chan_resume_cb,
894 .suspend = chan_suspend_cb,
895 .get_sndtimeo = chan_get_sndtimeo_cb,
896 .alloc_skb = chan_alloc_skb_cb,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300897
898 .teardown = l2cap_chan_no_teardown,
899 .defer = l2cap_chan_no_defer,
900 .set_shutdown = l2cap_chan_no_set_shutdown,
901};
902
903static inline __u8 bdaddr_type(__u8 type)
904{
905 if (type == ADDR_LE_DEV_PUBLIC)
906 return BDADDR_LE_PUBLIC;
907 else
908 return BDADDR_LE_RANDOM;
909}
910
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300911static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
912{
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300913 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300914 int err;
915
Johan Hedberg26d46df2015-10-06 13:03:24 +0300916 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300917 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300918 return -EINVAL;
919
Johan Hedberg26d46df2015-10-06 13:03:24 +0300920 chan->ops = &bt_6lowpan_chan_ops;
921
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300922 err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300923 addr, dst_type);
924
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300925 BT_DBG("chan %p err %d", chan, err);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300926 if (err < 0)
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300927 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300928
Jukka Rissanen18722c22013-12-11 17:05:37 +0200929 return err;
930}
931
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300932static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
933{
934 struct lowpan_peer *peer;
935
936 BT_DBG("conn %p dst type %d", conn, dst_type);
937
938 peer = lookup_peer(conn);
939 if (!peer)
940 return -ENOENT;
941
942 BT_DBG("peer %p chan %p", peer, peer->chan);
943
944 l2cap_chan_close(peer->chan, ENOENT);
945
946 return 0;
947}
948
949static struct l2cap_chan *bt_6lowpan_listen(void)
950{
951 bdaddr_t *addr = BDADDR_ANY;
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300952 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300953 int err;
954
Jukka Rissanen7b2ed602015-01-08 17:00:55 +0200955 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300956 return NULL;
957
Johan Hedberg26d46df2015-10-06 13:03:24 +0300958 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300959 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300960 return NULL;
961
Johan Hedberg26d46df2015-10-06 13:03:24 +0300962 chan->ops = &bt_6lowpan_chan_ops;
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300963 chan->state = BT_LISTEN;
964 chan->src_type = BDADDR_LE_PUBLIC;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300965
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300966 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
Johan Hedberg2773b022014-11-13 09:46:05 +0200967
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300968 BT_DBG("chan %p src type %d", chan, chan->src_type);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300969
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300970 err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300971 if (err) {
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300972 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300973 BT_ERR("psm cannot be added err %d", err);
974 return NULL;
975 }
976
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300977 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300978}
979
980static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
981 struct l2cap_conn **conn)
982{
983 struct hci_conn *hcon;
984 struct hci_dev *hdev;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300985 int n;
986
987 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
988 &addr->b[5], &addr->b[4], &addr->b[3],
989 &addr->b[2], &addr->b[1], &addr->b[0],
990 addr_type);
991
992 if (n < 7)
993 return -EINVAL;
994
Johan Hedberg39385cb2016-11-12 17:03:07 +0200995 /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */
996 hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300997 if (!hdev)
998 return -ENOENT;
999
1000 hci_dev_lock(hdev);
Johan Hedbergf5ad4ff2015-10-21 18:03:02 +03001001 hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001002 hci_dev_unlock(hdev);
1003
1004 if (!hcon)
1005 return -ENOENT;
1006
1007 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1008
1009 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1010
1011 return 0;
1012}
1013
1014static void disconnect_all_peers(void)
1015{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001016 struct lowpan_btle_dev *entry;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001017 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1018 struct list_head peers;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001019
1020 INIT_LIST_HEAD(&peers);
1021
1022 /* We make a separate list of peers as the close_cb() will
1023 * modify the device peers list so it is better not to mess
1024 * with the same list at the same time.
1025 */
1026
Jukka Rissanen90305822014-10-28 17:16:47 +02001027 rcu_read_lock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001028
Jukka Rissanen90305822014-10-28 17:16:47 +02001029 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1030 list_for_each_entry_rcu(peer, &entry->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001031 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1032 if (!new_peer)
1033 break;
1034
1035 new_peer->chan = peer->chan;
1036 INIT_LIST_HEAD(&new_peer->list);
1037
1038 list_add(&new_peer->list, &peers);
1039 }
1040 }
1041
Jukka Rissanen90305822014-10-28 17:16:47 +02001042 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001043
Jukka Rissanen90305822014-10-28 17:16:47 +02001044 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001045 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1046 l2cap_chan_close(peer->chan, ENOENT);
Jukka Rissanen90305822014-10-28 17:16:47 +02001047
1048 list_del_rcu(&peer->list);
Johan Hedberg4e790222014-11-11 14:16:29 +02001049 kfree_rcu(peer, rcu);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001050 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001051 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001052}
1053
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001054struct set_enable {
Jukka Rissanen90305822014-10-28 17:16:47 +02001055 struct work_struct work;
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001056 bool flag;
Jukka Rissanen90305822014-10-28 17:16:47 +02001057};
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001058
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001059static void do_enable_set(struct work_struct *work)
Jukka Rissanen90305822014-10-28 17:16:47 +02001060{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001061 struct set_enable *set_enable = container_of(work,
1062 struct set_enable, work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001063
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001064 if (!set_enable->flag || enable_6lowpan != set_enable->flag)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001065 /* Disconnect existing connections if 6lowpan is
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001066 * disabled
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001067 */
1068 disconnect_all_peers();
1069
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001070 enable_6lowpan = set_enable->flag;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001071
1072 if (listen_chan) {
1073 l2cap_chan_close(listen_chan, 0);
1074 l2cap_chan_put(listen_chan);
1075 }
1076
1077 listen_chan = bt_6lowpan_listen();
1078
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001079 kfree(set_enable);
Jukka Rissanen90305822014-10-28 17:16:47 +02001080}
1081
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001082static int lowpan_enable_set(void *data, u64 val)
Jukka Rissanen90305822014-10-28 17:16:47 +02001083{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001084 struct set_enable *set_enable;
Jukka Rissanen90305822014-10-28 17:16:47 +02001085
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001086 set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
1087 if (!set_enable)
Jukka Rissanen90305822014-10-28 17:16:47 +02001088 return -ENOMEM;
1089
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001090 set_enable->flag = !!val;
1091 INIT_WORK(&set_enable->work, do_enable_set);
Jukka Rissanen90305822014-10-28 17:16:47 +02001092
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001093 schedule_work(&set_enable->work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001094
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001095 return 0;
1096}
1097
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001098static int lowpan_enable_get(void *data, u64 *val)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001099{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001100 *val = enable_6lowpan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001101 return 0;
1102}
1103
YueHaibinge250fab2019-01-16 01:54:06 +00001104DEFINE_DEBUGFS_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
1105 lowpan_enable_set, "%llu\n");
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001106
1107static ssize_t lowpan_control_write(struct file *fp,
1108 const char __user *user_buffer,
1109 size_t count,
1110 loff_t *position)
1111{
1112 char buf[32];
1113 size_t buf_size = min(count, sizeof(buf) - 1);
1114 int ret;
1115 bdaddr_t addr;
1116 u8 addr_type;
1117 struct l2cap_conn *conn = NULL;
1118
1119 if (copy_from_user(buf, user_buffer, buf_size))
1120 return -EFAULT;
1121
1122 buf[buf_size] = '\0';
1123
1124 if (memcmp(buf, "connect ", 8) == 0) {
1125 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1126 if (ret == -EINVAL)
1127 return ret;
1128
1129 if (listen_chan) {
1130 l2cap_chan_close(listen_chan, 0);
1131 l2cap_chan_put(listen_chan);
1132 listen_chan = NULL;
1133 }
1134
1135 if (conn) {
1136 struct lowpan_peer *peer;
1137
1138 if (!is_bt_6lowpan(conn->hcon))
1139 return -EINVAL;
1140
1141 peer = lookup_peer(conn);
1142 if (peer) {
1143 BT_DBG("6LoWPAN connection already exists");
1144 return -EALREADY;
1145 }
1146
1147 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1148 &conn->hcon->dst, conn->hcon->dst_type,
1149 addr_type);
1150 }
1151
1152 ret = bt_6lowpan_connect(&addr, addr_type);
1153 if (ret < 0)
1154 return ret;
1155
1156 return count;
1157 }
1158
1159 if (memcmp(buf, "disconnect ", 11) == 0) {
1160 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1161 if (ret < 0)
1162 return ret;
1163
1164 ret = bt_6lowpan_disconnect(conn, addr_type);
1165 if (ret < 0)
1166 return ret;
1167
1168 return count;
1169 }
1170
1171 return count;
1172}
1173
1174static int lowpan_control_show(struct seq_file *f, void *ptr)
1175{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001176 struct lowpan_btle_dev *entry;
Jukka Rissanen90305822014-10-28 17:16:47 +02001177 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001178
Jukka Rissanen90305822014-10-28 17:16:47 +02001179 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001180
Jukka Rissanen90305822014-10-28 17:16:47 +02001181 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1182 list_for_each_entry(peer, &entry->peers, list)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001183 seq_printf(f, "%pMR (type %u)\n",
1184 &peer->chan->dst, peer->chan->dst_type);
1185 }
1186
Jukka Rissanen90305822014-10-28 17:16:47 +02001187 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001188
1189 return 0;
1190}
1191
1192static int lowpan_control_open(struct inode *inode, struct file *file)
1193{
1194 return single_open(file, lowpan_control_show, inode->i_private);
1195}
1196
1197static const struct file_operations lowpan_control_fops = {
1198 .open = lowpan_control_open,
1199 .read = seq_read,
1200 .write = lowpan_control_write,
1201 .llseek = seq_lseek,
1202 .release = single_release,
1203};
1204
Jukka Rissanen7f118252014-06-18 16:37:11 +03001205static void disconnect_devices(void)
1206{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001207 struct lowpan_btle_dev *entry, *tmp, *new_dev;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001208 struct list_head devices;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001209
1210 INIT_LIST_HEAD(&devices);
1211
1212 /* We make a separate list of devices because the unregister_netdev()
1213 * will call device_event() which will also want to modify the same
1214 * devices list.
1215 */
1216
Jukka Rissanen90305822014-10-28 17:16:47 +02001217 rcu_read_lock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001218
Jukka Rissanen90305822014-10-28 17:16:47 +02001219 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001220 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1221 if (!new_dev)
1222 break;
1223
1224 new_dev->netdev = entry->netdev;
1225 INIT_LIST_HEAD(&new_dev->list);
1226
Jukka Rissanen90305822014-10-28 17:16:47 +02001227 list_add_rcu(&new_dev->list, &devices);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001228 }
1229
Jukka Rissanen90305822014-10-28 17:16:47 +02001230 rcu_read_unlock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001231
Dan Carpenterdaac1972014-10-29 19:10:57 +03001232 list_for_each_entry_safe(entry, tmp, &devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001233 ifdown(entry->netdev);
1234 BT_DBG("Unregistering netdev %s %p",
1235 entry->netdev->name, entry->netdev);
Alexander Aring00f59312015-12-09 22:46:29 +01001236 lowpan_unregister_netdev(entry->netdev);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001237 kfree(entry);
1238 }
1239}
1240
Jukka Rissanen18722c22013-12-11 17:05:37 +02001241static int device_event(struct notifier_block *unused,
1242 unsigned long event, void *ptr)
1243{
1244 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001245 struct lowpan_btle_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +02001246
1247 if (netdev->type != ARPHRD_6LOWPAN)
1248 return NOTIFY_DONE;
1249
1250 switch (event) {
1251 case NETDEV_UNREGISTER:
Jukka Rissanen90305822014-10-28 17:16:47 +02001252 spin_lock(&devices_lock);
1253 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +02001254 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001255 BT_DBG("Unregistered netdev %s %p",
1256 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001257 list_del(&entry->list);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001258 break;
1259 }
1260 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001261 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001262 break;
1263 }
1264
1265 return NOTIFY_DONE;
1266}
1267
1268static struct notifier_block bt_6lowpan_dev_notifier = {
1269 .notifier_call = device_event,
1270};
1271
Jukka Rissanen5547e482014-06-18 16:37:09 +03001272static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001273{
YueHaibinge250fab2019-01-16 01:54:06 +00001274 lowpan_enable_debugfs = debugfs_create_file_unsafe("6lowpan_enable",
1275 0644, bt_debugfs,
1276 NULL,
1277 &lowpan_enable_fops);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001278 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1279 bt_debugfs, NULL,
1280 &lowpan_control_fops);
1281
Jukka Rissanen18722c22013-12-11 17:05:37 +02001282 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1283}
1284
Jukka Rissanen5547e482014-06-18 16:37:09 +03001285static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001286{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001287 debugfs_remove(lowpan_enable_debugfs);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001288 debugfs_remove(lowpan_control_debugfs);
1289
1290 if (listen_chan) {
1291 l2cap_chan_close(listen_chan, 0);
1292 l2cap_chan_put(listen_chan);
1293 }
1294
Jukka Rissanen7f118252014-06-18 16:37:11 +03001295 disconnect_devices();
1296
Jukka Rissanen18722c22013-12-11 17:05:37 +02001297 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1298}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001299
1300module_init(bt_6lowpan_init);
1301module_exit(bt_6lowpan_exit);
1302
1303MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1304MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1305MODULE_VERSION(VERSION);
1306MODULE_LICENSE("GPL");