blob: e2b66f3b0a4958a066b4ac0d284c879d929efc6b [file] [log] [blame]
Jukka Rissanen18722c22013-12-11 17:05:37 +02001/*
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03002 Copyright (c) 2013-2014 Intel Corp.
Jukka Rissanen18722c22013-12-11 17:05:37 +02003
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License version 2 and
6 only version 2 as published by the Free Software Foundation.
7
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
12*/
13
Jukka Rissanen18722c22013-12-11 17:05:37 +020014#include <linux/if_arp.h>
15#include <linux/netdevice.h>
16#include <linux/etherdevice.h>
Jukka Rissanen5547e482014-06-18 16:37:09 +030017#include <linux/module.h>
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030018#include <linux/debugfs.h>
Jukka Rissanen18722c22013-12-11 17:05:37 +020019
20#include <net/ipv6.h>
21#include <net/ip6_route.h>
22#include <net/addrconf.h>
23
24#include <net/af_ieee802154.h> /* to get the address type */
25
26#include <net/bluetooth/bluetooth.h>
27#include <net/bluetooth/hci_core.h>
28#include <net/bluetooth/l2cap.h>
29
Alexander Aringcefc8c82014-03-05 14:29:05 +010030#include <net/6lowpan.h> /* for the compression support */
Jukka Rissanen18722c22013-12-11 17:05:37 +020031
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030032#define VERSION "0.1"
33
Jukka Rissanen7b2ed602015-01-08 17:00:55 +020034static struct dentry *lowpan_enable_debugfs;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030035static struct dentry *lowpan_control_debugfs;
36
Jukka Rissanen18722c22013-12-11 17:05:37 +020037#define IFACE_NAME_TEMPLATE "bt%d"
Jukka Rissanen18722c22013-12-11 17:05:37 +020038
39struct skb_cb {
40 struct in6_addr addr;
Jukka Rissanen39e90c72014-09-08 12:11:45 +030041 struct in6_addr gw;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030042 struct l2cap_chan *chan;
43 int status;
Jukka Rissanen18722c22013-12-11 17:05:37 +020044};
45#define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
46
47/* The devices list contains those devices that we are acting
48 * as a proxy. The BT 6LoWPAN device is a virtual device that
49 * connects to the Bluetooth LE device. The real connection to
50 * BT device is done via l2cap layer. There exists one
51 * virtual device / one BT 6LoWPAN network (=hciX device).
52 * The list contains struct lowpan_dev elements.
53 */
54static LIST_HEAD(bt_6lowpan_devices);
Jukka Rissanen90305822014-10-28 17:16:47 +020055static DEFINE_SPINLOCK(devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +020056
Jukka Rissanen7b2ed602015-01-08 17:00:55 +020057static bool enable_6lowpan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030058
59/* We are listening incoming connections via this channel
60 */
61static struct l2cap_chan *listen_chan;
62
Jukka Rissanen18722c22013-12-11 17:05:37 +020063struct lowpan_peer {
64 struct list_head list;
Jukka Rissanen90305822014-10-28 17:16:47 +020065 struct rcu_head rcu;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030066 struct l2cap_chan *chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +020067
68 /* peer addresses in various formats */
69 unsigned char eui64_addr[EUI64_ADDR_LEN];
70 struct in6_addr peer_addr;
71};
72
73struct lowpan_dev {
74 struct list_head list;
75
76 struct hci_dev *hdev;
77 struct net_device *netdev;
78 struct list_head peers;
79 atomic_t peer_count; /* number of items in peers list */
80
81 struct work_struct delete_netdev;
82 struct delayed_work notify_peers;
83};
84
85static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev)
86{
Alexander Aringb72f6f52015-08-11 21:44:08 +020087 return (struct lowpan_dev *)lowpan_priv(netdev)->priv;
Jukka Rissanen18722c22013-12-11 17:05:37 +020088}
89
90static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer)
91{
Jukka Rissanen90305822014-10-28 17:16:47 +020092 list_add_rcu(&peer->list, &dev->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +020093 atomic_inc(&dev->peer_count);
94}
95
96static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer)
97{
Jukka Rissanen90305822014-10-28 17:16:47 +020098 list_del_rcu(&peer->list);
Johan Hedberg4e790222014-11-11 14:16:29 +020099 kfree_rcu(peer, rcu);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200100
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300101 module_put(THIS_MODULE);
102
Jukka Rissanen18722c22013-12-11 17:05:37 +0200103 if (atomic_dec_and_test(&dev->peer_count)) {
104 BT_DBG("last peer");
105 return true;
106 }
107
108 return false;
109}
110
111static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev,
112 bdaddr_t *ba, __u8 type)
113{
Jukka Rissanen90305822014-10-28 17:16:47 +0200114 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200115
116 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
117 ba, type);
118
Jukka Rissanen90305822014-10-28 17:16:47 +0200119 rcu_read_lock();
120
121 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300122 BT_DBG("dst addr %pMR dst type %d",
123 &peer->chan->dst, peer->chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200124
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300125 if (bacmp(&peer->chan->dst, ba))
Jukka Rissanen18722c22013-12-11 17:05:37 +0200126 continue;
127
Jukka Rissanen90305822014-10-28 17:16:47 +0200128 if (type == peer->chan->dst_type) {
129 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300130 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200131 }
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300132 }
133
Jukka Rissanen90305822014-10-28 17:16:47 +0200134 rcu_read_unlock();
135
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300136 return NULL;
137}
138
Jukka Rissanen90305822014-10-28 17:16:47 +0200139static inline struct lowpan_peer *__peer_lookup_chan(struct lowpan_dev *dev,
140 struct l2cap_chan *chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300141{
Jukka Rissanen90305822014-10-28 17:16:47 +0200142 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300143
Jukka Rissanen90305822014-10-28 17:16:47 +0200144 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300145 if (peer->chan == chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200146 return peer;
147 }
148
149 return NULL;
150}
151
Jukka Rissanen90305822014-10-28 17:16:47 +0200152static inline struct lowpan_peer *__peer_lookup_conn(struct lowpan_dev *dev,
153 struct l2cap_conn *conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200154{
Jukka Rissanen90305822014-10-28 17:16:47 +0200155 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200156
Jukka Rissanen90305822014-10-28 17:16:47 +0200157 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300158 if (peer->chan->conn == conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200159 return peer;
160 }
161
162 return NULL;
163}
164
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300165static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_dev *dev,
166 struct in6_addr *daddr,
167 struct sk_buff *skb)
168{
Jukka Rissanen90305822014-10-28 17:16:47 +0200169 struct lowpan_peer *peer;
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300170 struct in6_addr *nexthop;
171 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
172 int count = atomic_read(&dev->peer_count);
173
174 BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
175
176 /* If we have multiple 6lowpan peers, then check where we should
177 * send the packet. If only one peer exists, then we can send the
178 * packet right away.
179 */
Jukka Rissanen90305822014-10-28 17:16:47 +0200180 if (count == 1) {
181 rcu_read_lock();
182 peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer,
183 list);
184 rcu_read_unlock();
185 return peer;
186 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300187
188 if (!rt) {
189 nexthop = &lowpan_cb(skb)->gw;
190
191 if (ipv6_addr_any(nexthop))
192 return NULL;
193 } else {
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700194 nexthop = rt6_nexthop(rt, daddr);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300195
196 /* We need to remember the address because it is needed
197 * by bt_xmit() when sending the packet. In bt_xmit(), the
198 * destination routing info is not set.
199 */
200 memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
201 }
202
203 BT_DBG("gw %pI6c", nexthop);
204
Jukka Rissanen90305822014-10-28 17:16:47 +0200205 rcu_read_lock();
206
207 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300208 BT_DBG("dst addr %pMR dst type %d ip %pI6c",
209 &peer->chan->dst, peer->chan->dst_type,
210 &peer->peer_addr);
211
Jukka Rissanen90305822014-10-28 17:16:47 +0200212 if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
213 rcu_read_unlock();
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300214 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200215 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300216 }
217
Jukka Rissanen90305822014-10-28 17:16:47 +0200218 rcu_read_unlock();
219
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300220 return NULL;
221}
222
Jukka Rissanen18722c22013-12-11 17:05:37 +0200223static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
224{
Jukka Rissanen90305822014-10-28 17:16:47 +0200225 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200226 struct lowpan_peer *peer = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200227
Jukka Rissanen90305822014-10-28 17:16:47 +0200228 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200229
Jukka Rissanen90305822014-10-28 17:16:47 +0200230 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
231 peer = __peer_lookup_conn(entry, conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200232 if (peer)
233 break;
234 }
235
Jukka Rissanen90305822014-10-28 17:16:47 +0200236 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200237
238 return peer;
239}
240
241static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn)
242{
Jukka Rissanen90305822014-10-28 17:16:47 +0200243 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200244 struct lowpan_dev *dev = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200245
Jukka Rissanen90305822014-10-28 17:16:47 +0200246 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200247
Jukka Rissanen90305822014-10-28 17:16:47 +0200248 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200249 if (conn->hcon->hdev == entry->hdev) {
250 dev = entry;
251 break;
252 }
253 }
254
Jukka Rissanen90305822014-10-28 17:16:47 +0200255 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200256
257 return dev;
258}
259
Jukka Rissanen18722c22013-12-11 17:05:37 +0200260static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
261{
262 struct sk_buff *skb_cp;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200263
264 skb_cp = skb_copy(skb, GFP_ATOMIC);
265 if (!skb_cp)
Martin Townsendf8b36172014-10-23 15:40:53 +0100266 return NET_RX_DROP;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200267
Li RongQing4456c502014-10-16 10:21:55 +0800268 return netif_rx(skb_cp);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200269}
270
Martin Townsend01141232014-10-23 15:40:56 +0100271static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
272 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200273{
274 const u8 *saddr, *daddr;
275 u8 iphc0, iphc1;
276 struct lowpan_dev *dev;
277 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200278
279 dev = lowpan_dev(netdev);
280
Jukka Rissanen90305822014-10-28 17:16:47 +0200281 rcu_read_lock();
282 peer = __peer_lookup_chan(dev, chan);
283 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200284 if (!peer)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000285 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200286
287 saddr = peer->eui64_addr;
288 daddr = dev->netdev->dev_addr;
289
290 /* at least two bytes will be used for the encoding */
291 if (skb->len < 2)
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000292 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200293
294 if (lowpan_fetch_skb_u8(skb, &iphc0))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000295 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200296
297 if (lowpan_fetch_skb_u8(skb, &iphc1))
Martin Townsend56b2c3e2014-11-06 19:15:13 +0000298 return -EINVAL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200299
Martin Townsend01141232014-10-23 15:40:56 +0100300 return lowpan_header_decompress(skb, netdev,
301 saddr, IEEE802154_ADDR_LONG,
302 EUI64_ADDR_LEN, daddr,
303 IEEE802154_ADDR_LONG, EUI64_ADDR_LEN,
304 iphc0, iphc1);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200305
Jukka Rissanen18722c22013-12-11 17:05:37 +0200306}
307
308static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300309 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200310{
311 struct sk_buff *local_skb;
312 int ret;
313
314 if (!netif_running(dev))
315 goto drop;
316
Alexander Aringcefdb802015-10-13 13:42:55 +0200317 if (dev->type != ARPHRD_6LOWPAN || !skb->len)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200318 goto drop;
319
Alexander Aringcefdb802015-10-13 13:42:55 +0200320 skb_reset_network_header(skb);
321
Martin Townsend11e3ff72014-10-13 11:00:56 +0100322 skb = skb_share_check(skb, GFP_ATOMIC);
323 if (!skb)
324 goto drop;
325
Jukka Rissanen18722c22013-12-11 17:05:37 +0200326 /* check that it's our buffer */
Alexander Aringcefdb802015-10-13 13:42:55 +0200327 if (lowpan_is_ipv6(*skb_network_header(skb))) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200328 /* Copy the packet so that the IPv6 header is
329 * properly aligned.
330 */
331 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
332 skb_tailroom(skb), GFP_ATOMIC);
333 if (!local_skb)
334 goto drop;
335
336 local_skb->protocol = htons(ETH_P_IPV6);
337 local_skb->pkt_type = PACKET_HOST;
338
Jukka Rissanen18722c22013-12-11 17:05:37 +0200339 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
340
341 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
342 kfree_skb(local_skb);
343 goto drop;
344 }
345
346 dev->stats.rx_bytes += skb->len;
347 dev->stats.rx_packets++;
348
Martin Townsend3c400b82014-10-23 15:40:55 +0100349 consume_skb(local_skb);
350 consume_skb(skb);
Alexander Aringcefdb802015-10-13 13:42:55 +0200351 } else if (lowpan_is_iphc(*skb_network_header(skb))) {
352 local_skb = skb_clone(skb, GFP_ATOMIC);
353 if (!local_skb)
354 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200355
Alexander Aringcefdb802015-10-13 13:42:55 +0200356 ret = iphc_decompress(local_skb, dev, chan);
357 if (ret < 0) {
358 kfree_skb(local_skb);
359 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200360 }
Alexander Aringcefdb802015-10-13 13:42:55 +0200361
362 local_skb->protocol = htons(ETH_P_IPV6);
363 local_skb->pkt_type = PACKET_HOST;
364 local_skb->dev = dev;
365
366 if (give_skb_to_upper(local_skb, dev)
367 != NET_RX_SUCCESS) {
368 kfree_skb(local_skb);
369 goto drop;
370 }
371
372 dev->stats.rx_bytes += skb->len;
373 dev->stats.rx_packets++;
374
375 consume_skb(local_skb);
376 consume_skb(skb);
377 } else {
378 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200379 }
380
381 return NET_RX_SUCCESS;
382
383drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300384 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200385 return NET_RX_DROP;
386}
387
388/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300389static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200390{
391 struct lowpan_dev *dev;
392 struct lowpan_peer *peer;
393 int err;
394
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300395 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200396 if (!peer)
397 return -ENOENT;
398
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300399 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200400 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200401 return -ENOENT;
402
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300403 err = recv_pkt(skb, dev->netdev, chan);
404 if (err) {
405 BT_DBG("recv pkt %d", err);
406 err = -EAGAIN;
407 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200408
409 return err;
410}
411
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300412static u8 get_addr_type_from_eui64(u8 byte)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200413{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300414 /* Is universal(0) or local(1) bit */
415 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300416}
417
418static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
419{
420 u8 *eui64 = ip6_daddr->s6_addr + 8;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200421
422 addr->b[0] = eui64[7];
423 addr->b[1] = eui64[6];
424 addr->b[2] = eui64[5];
425 addr->b[3] = eui64[2];
426 addr->b[4] = eui64[1];
427 addr->b[5] = eui64[0];
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300428}
Jukka Rissanen18722c22013-12-11 17:05:37 +0200429
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300430static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
431 bdaddr_t *addr, u8 *addr_type)
432{
433 copy_to_bdaddr(ip6_daddr, addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200434
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300435 /* We need to toggle the U/L bit that we got from IPv6 address
436 * so that we get the proper address and type of the BD address.
437 */
438 addr->b[5] ^= 0x02;
439
440 *addr_type = get_addr_type_from_eui64(addr->b[5]);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200441}
442
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300443static int setup_header(struct sk_buff *skb, struct net_device *netdev,
444 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200445{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300446 struct in6_addr ipv6_daddr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200447 struct lowpan_dev *dev;
448 struct lowpan_peer *peer;
449 bdaddr_t addr, *any = BDADDR_ANY;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300450 u8 *daddr = any->b;
451 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200452
453 dev = lowpan_dev(netdev);
454
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300455 memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr));
456
457 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300458 lowpan_cb(skb)->chan = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200459 } else {
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300460 u8 addr_type;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200461
462 /* Get destination BT device from skb.
463 * If there is no such peer then discard the packet.
464 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300465 convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200466
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300467 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300468 addr_type, &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200469
Jukka Rissanen18722c22013-12-11 17:05:37 +0200470 peer = peer_lookup_ba(dev, &addr, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200471 if (!peer) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300472 /* The packet might be sent to 6lowpan interface
473 * because of routing (either via default route
474 * or user set route) so get peer according to
475 * the destination address.
476 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300477 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300478 if (!peer) {
479 BT_DBG("no such peer %pMR found", &addr);
480 return -ENOENT;
481 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200482 }
483
484 daddr = peer->eui64_addr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300485 *peer_addr = addr;
486 *peer_addr_type = addr_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300487 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300488
489 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200490 }
491
Alexander Aringa6f77382015-10-13 13:42:57 +0200492 lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200493
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300494 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
495 if (err < 0)
496 return err;
497
498 return status;
499}
500
501static int header_create(struct sk_buff *skb, struct net_device *netdev,
502 unsigned short type, const void *_daddr,
503 const void *_saddr, unsigned int len)
504{
505 struct ipv6hdr *hdr;
506
507 if (type != ETH_P_IPV6)
508 return -EINVAL;
509
510 hdr = ipv6_hdr(skb);
511
512 memcpy(&lowpan_cb(skb)->addr, &hdr->daddr, sizeof(struct in6_addr));
513
514 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200515}
516
517/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300518static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300519 struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200520{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300521 struct msghdr msg;
522 struct kvec iv;
523 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200524
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300525 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300526 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300527 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300528 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300529
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300530 iv.iov_base = skb->data;
531 iv.iov_len = skb->len;
532
Al Viroc0371da2014-11-24 10:42:55 -0500533 memset(&msg, 0, sizeof(msg));
Al Viro17836392014-11-24 17:07:38 -0500534 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len);
Al Viroc0371da2014-11-24 10:42:55 -0500535
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300536 err = l2cap_chan_send(chan, &msg, skb->len);
537 if (err > 0) {
538 netdev->stats.tx_bytes += err;
539 netdev->stats.tx_packets++;
540 return 0;
541 }
542
543 if (!err)
544 err = lowpan_cb(skb)->status;
545
546 if (err < 0) {
547 if (err == -EAGAIN)
548 netdev->stats.tx_dropped++;
549 else
550 netdev->stats.tx_errors++;
551 }
552
553 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200554}
555
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300556static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200557{
558 struct sk_buff *local_skb;
Jukka Rissanen90305822014-10-28 17:16:47 +0200559 struct lowpan_dev *entry;
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300560 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200561
Jukka Rissanen90305822014-10-28 17:16:47 +0200562 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200563
Jukka Rissanen90305822014-10-28 17:16:47 +0200564 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
565 struct lowpan_peer *pentry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200566 struct lowpan_dev *dev;
567
568 if (entry->netdev != netdev)
569 continue;
570
571 dev = lowpan_dev(entry->netdev);
572
Jukka Rissanen90305822014-10-28 17:16:47 +0200573 list_for_each_entry_rcu(pentry, &dev->peers, list) {
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300574 int ret;
575
Jukka Rissanen18722c22013-12-11 17:05:37 +0200576 local_skb = skb_clone(skb, GFP_ATOMIC);
577
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300578 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
579 netdev->name,
580 &pentry->chan->dst, pentry->chan->dst_type,
581 &pentry->peer_addr, pentry->chan);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300582 ret = send_pkt(pentry->chan, local_skb, netdev);
583 if (ret < 0)
584 err = ret;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200585
586 kfree_skb(local_skb);
587 }
588 }
589
Jukka Rissanen90305822014-10-28 17:16:47 +0200590 rcu_read_unlock();
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300591
592 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200593}
594
595static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
596{
597 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200598 bdaddr_t addr;
599 u8 addr_type;
600
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300601 /* We must take a copy of the skb before we modify/replace the ipv6
602 * header as the header could be used elsewhere
603 */
Alexander Aringb0c42cd2014-10-08 10:24:53 +0200604 skb = skb_unshare(skb, GFP_ATOMIC);
605 if (!skb)
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300606 return NET_XMIT_DROP;
607
608 /* Return values from setup_header()
609 * <0 - error, packet is dropped
610 * 0 - this is a multicast packet
611 * 1 - this is unicast packet
612 */
613 err = setup_header(skb, netdev, &addr, &addr_type);
614 if (err < 0) {
615 kfree_skb(skb);
616 return NET_XMIT_DROP;
617 }
618
619 if (err) {
620 if (lowpan_cb(skb)->chan) {
621 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
622 netdev->name, &addr, addr_type,
623 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300624 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300625 } else {
626 err = -ENOENT;
627 }
628 } else {
629 /* We need to send the packet to every device behind this
630 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200631 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300632 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200633 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200634
Jukka Rissanenfc125182014-10-01 11:30:26 +0300635 dev_kfree_skb(skb);
636
Jukka Rissanen18722c22013-12-11 17:05:37 +0200637 if (err)
638 BT_DBG("ERROR: xmit failed (%d)", err);
639
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300640 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200641}
642
Jukka Rissanendf092302014-10-28 17:16:48 +0200643static struct lock_class_key bt_tx_busylock;
644static struct lock_class_key bt_netdev_xmit_lock_key;
645
646static void bt_set_lockdep_class_one(struct net_device *dev,
647 struct netdev_queue *txq,
648 void *_unused)
649{
650 lockdep_set_class(&txq->_xmit_lock, &bt_netdev_xmit_lock_key);
651}
652
653static int bt_dev_init(struct net_device *dev)
654{
655 netdev_for_each_tx_queue(dev, bt_set_lockdep_class_one, NULL);
656 dev->qdisc_tx_busylock = &bt_tx_busylock;
657
658 return 0;
659}
660
Jukka Rissanen18722c22013-12-11 17:05:37 +0200661static const struct net_device_ops netdev_ops = {
Jukka Rissanendf092302014-10-28 17:16:48 +0200662 .ndo_init = bt_dev_init,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200663 .ndo_start_xmit = bt_xmit,
664};
665
666static struct header_ops header_ops = {
667 .create = header_create,
668};
669
670static void netdev_setup(struct net_device *dev)
671{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200672 dev->hard_header_len = 0;
673 dev->needed_tailroom = 0;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300674 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
675 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200676 dev->watchdog_timeo = 0;
677
678 dev->netdev_ops = &netdev_ops;
679 dev->header_ops = &header_ops;
680 dev->destructor = free_netdev;
681}
682
683static struct device_type bt_type = {
684 .name = "bluetooth",
685};
686
687static void set_addr(u8 *eui, u8 *addr, u8 addr_type)
688{
689 /* addr is the BT address in little-endian format */
690 eui[0] = addr[5];
691 eui[1] = addr[4];
692 eui[2] = addr[3];
693 eui[3] = 0xFF;
694 eui[4] = 0xFE;
695 eui[5] = addr[2];
696 eui[6] = addr[1];
697 eui[7] = addr[0];
698
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300699 /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300700 if (addr_type == BDADDR_LE_PUBLIC)
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300701 eui[0] &= ~0x02;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200702 else
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300703 eui[0] |= 0x02;
704
705 BT_DBG("type %d addr %*phC", addr_type, 8, eui);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200706}
707
708static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr,
709 u8 addr_type)
710{
711 netdev->addr_assign_type = NET_ADDR_PERM;
712 set_addr(netdev->dev_addr, addr->b, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200713}
714
715static void ifup(struct net_device *netdev)
716{
717 int err;
718
719 rtnl_lock();
720 err = dev_open(netdev);
721 if (err < 0)
722 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
723 rtnl_unlock();
724}
725
Jukka Rissanen7f118252014-06-18 16:37:11 +0300726static void ifdown(struct net_device *netdev)
727{
728 int err;
729
730 rtnl_lock();
731 err = dev_close(netdev);
732 if (err < 0)
733 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
734 rtnl_unlock();
735}
736
Jukka Rissanen18722c22013-12-11 17:05:37 +0200737static void do_notify_peers(struct work_struct *work)
738{
739 struct lowpan_dev *dev = container_of(work, struct lowpan_dev,
740 notify_peers.work);
741
742 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
743}
744
745static bool is_bt_6lowpan(struct hci_conn *hcon)
746{
747 if (hcon->type != LE_LINK)
748 return false;
749
Jukka Rissanen7b2ed602015-01-08 17:00:55 +0200750 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300751 return false;
752
753 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200754}
755
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300756static struct l2cap_chan *chan_create(void)
757{
758 struct l2cap_chan *chan;
759
760 chan = l2cap_chan_create();
761 if (!chan)
762 return NULL;
763
764 l2cap_chan_set_defaults(chan);
765
766 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
767 chan->mode = L2CAP_MODE_LE_FLOWCTL;
Johan Hedberg301de2c2015-10-06 13:03:19 +0300768 chan->imtu = 1280;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300769
770 return chan;
771}
772
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300773static void set_ip_addr_bits(u8 addr_type, u8 *addr)
774{
775 if (addr_type == BDADDR_LE_PUBLIC)
776 *addr |= 0x02;
777 else
778 *addr &= ~0x02;
779}
780
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300781static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
782 struct lowpan_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200783{
784 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200785
786 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
787 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300788 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200789
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300790 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200791 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
792
793 /* RFC 2464 ch. 5 */
794 peer->peer_addr.s6_addr[0] = 0xFE;
795 peer->peer_addr.s6_addr[1] = 0x80;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300796 set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b,
797 chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200798
799 memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8,
800 EUI64_ADDR_LEN);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200801
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300802 /* IPv6 address needs to have the U/L bit set properly so toggle
803 * it back here.
804 */
805 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
806
Jukka Rissanen90305822014-10-28 17:16:47 +0200807 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200808 INIT_LIST_HEAD(&peer->list);
809 peer_add(dev, peer);
Jukka Rissanen90305822014-10-28 17:16:47 +0200810 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200811
812 /* Notifying peers about us needs to be done without locks held */
813 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
814 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
815
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300816 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200817}
818
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300819static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200820{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200821 struct net_device *netdev;
822 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200823
Alexander Aringb72f6f52015-08-11 21:44:08 +0200824 netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_dev)),
825 IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
826 netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200827 if (!netdev)
828 return -ENOMEM;
829
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300830 set_dev_addr(netdev, &chan->src, chan->src_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200831
832 netdev->netdev_ops = &netdev_ops;
Glenn Ruben Bakkefc842422015-06-17 07:32:25 -0700833 SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200834 SET_NETDEV_DEVTYPE(netdev, &bt_type);
835
Alexander Aringa42bbba2015-08-11 21:44:07 +0200836 *dev = lowpan_dev(netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300837 (*dev)->netdev = netdev;
838 (*dev)->hdev = chan->conn->hcon->hdev;
839 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200840
Jukka Rissanen90305822014-10-28 17:16:47 +0200841 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300842 INIT_LIST_HEAD(&(*dev)->list);
Jukka Rissanen90305822014-10-28 17:16:47 +0200843 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
844 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200845
Alexander Aringb72f6f52015-08-11 21:44:08 +0200846 lowpan_netdev_setup(netdev, LOWPAN_LLTYPE_BTLE);
847
Alexander Aring5857d1d2015-07-30 09:40:53 +0200848 err = register_netdev(netdev);
849 if (err < 0) {
850 BT_INFO("register_netdev failed %d", err);
851 spin_lock(&devices_lock);
852 list_del_rcu(&(*dev)->list);
853 spin_unlock(&devices_lock);
854 free_netdev(netdev);
855 goto out;
856 }
857
858 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
859 netdev->ifindex, &chan->dst, chan->dst_type,
860 &chan->src, chan->src_type);
861 set_bit(__LINK_STATE_PRESENT, &netdev->state);
862
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300863 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200864
865out:
866 return err;
867}
868
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300869static inline void chan_ready_cb(struct l2cap_chan *chan)
870{
871 struct lowpan_dev *dev;
872
873 dev = lookup_dev(chan->conn);
874
875 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
876
877 if (!dev) {
878 if (setup_netdev(chan, &dev) < 0) {
879 l2cap_chan_del(chan, -ENOENT);
880 return;
881 }
882 }
883
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300884 if (!try_module_get(THIS_MODULE))
885 return;
886
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300887 add_peer_chan(chan, dev);
888 ifup(dev->netdev);
889}
890
Johan Hedberg2b293492014-08-07 10:03:32 +0300891static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300892{
Johan Hedberg2b293492014-08-07 10:03:32 +0300893 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300894
Johan Hedberg630ef792015-10-06 13:03:22 +0300895 chan = chan_create();
896 if (!chan)
897 return NULL;
898
Johan Hedberg2b293492014-08-07 10:03:32 +0300899 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300900
901 BT_DBG("chan %p pchan %p", chan, pchan);
902
Johan Hedberg2b293492014-08-07 10:03:32 +0300903 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300904}
905
Jukka Rissanen18722c22013-12-11 17:05:37 +0200906static void delete_netdev(struct work_struct *work)
907{
908 struct lowpan_dev *entry = container_of(work, struct lowpan_dev,
909 delete_netdev);
910
911 unregister_netdev(entry->netdev);
912
Glenn Ruben Bakke2ad88fb2015-06-17 07:32:26 -0700913 /* The entry pointer is deleted by the netdev destructor. */
Jukka Rissanen18722c22013-12-11 17:05:37 +0200914}
915
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300916static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200917{
Jukka Rissanen90305822014-10-28 17:16:47 +0200918 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200919 struct lowpan_dev *dev = NULL;
920 struct lowpan_peer *peer;
921 int err = -ENOENT;
Glenn Ruben Bakkef63666d2015-06-17 07:32:24 -0700922 bool last = false, remove = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200923
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300924 BT_DBG("chan %p conn %p", chan, chan->conn);
925
926 if (chan->conn && chan->conn->hcon) {
927 if (!is_bt_6lowpan(chan->conn->hcon))
928 return;
929
930 /* If conn is set, then the netdev is also there and we should
931 * not remove it.
932 */
Glenn Ruben Bakkef63666d2015-06-17 07:32:24 -0700933 remove = false;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300934 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200935
Jukka Rissanen90305822014-10-28 17:16:47 +0200936 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200937
Jukka Rissanen90305822014-10-28 17:16:47 +0200938 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200939 dev = lowpan_dev(entry->netdev);
Jukka Rissanen90305822014-10-28 17:16:47 +0200940 peer = __peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200941 if (peer) {
942 last = peer_del(dev, peer);
943 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300944
945 BT_DBG("dev %p removing %speer %p", dev,
946 last ? "last " : "1 ", peer);
947 BT_DBG("chan %p orig refcnt %d", chan,
948 atomic_read(&chan->kref.refcount));
949
950 l2cap_chan_put(chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200951 break;
952 }
953 }
954
955 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
Jukka Rissanen90305822014-10-28 17:16:47 +0200956 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200957
958 cancel_delayed_work_sync(&dev->notify_peers);
959
Jukka Rissanen7f118252014-06-18 16:37:11 +0300960 ifdown(dev->netdev);
961
Glenn Ruben Bakkef63666d2015-06-17 07:32:24 -0700962 if (remove) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300963 INIT_WORK(&entry->delete_netdev, delete_netdev);
964 schedule_work(&entry->delete_netdev);
965 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200966 } else {
Jukka Rissanen90305822014-10-28 17:16:47 +0200967 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200968 }
969
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300970 return;
971}
972
973static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
974{
975 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
976 state_to_string(state), err);
977}
978
979static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
980 unsigned long hdr_len,
981 unsigned long len, int nb)
982{
983 /* Note that we must allocate using GFP_ATOMIC here as
984 * this function is called originally from netdev hard xmit
985 * function in atomic context.
986 */
987 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
988}
989
990static void chan_suspend_cb(struct l2cap_chan *chan)
991{
992 struct sk_buff *skb = chan->data;
993
994 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
995
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300996 if (!skb)
997 return;
998
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300999 lowpan_cb(skb)->status = -EAGAIN;
1000}
1001
1002static void chan_resume_cb(struct l2cap_chan *chan)
1003{
1004 struct sk_buff *skb = chan->data;
1005
1006 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
1007
Jukka Rissanen59790aa2014-09-29 10:55:46 +03001008 if (!skb)
1009 return;
1010
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001011 lowpan_cb(skb)->status = 0;
1012}
1013
1014static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
1015{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +03001016 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001017}
1018
1019static const struct l2cap_ops bt_6lowpan_chan_ops = {
1020 .name = "L2CAP 6LoWPAN channel",
1021 .new_connection = chan_new_conn_cb,
1022 .recv = chan_recv_cb,
1023 .close = chan_close_cb,
1024 .state_change = chan_state_change_cb,
1025 .ready = chan_ready_cb,
1026 .resume = chan_resume_cb,
1027 .suspend = chan_suspend_cb,
1028 .get_sndtimeo = chan_get_sndtimeo_cb,
1029 .alloc_skb = chan_alloc_skb_cb,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001030
1031 .teardown = l2cap_chan_no_teardown,
1032 .defer = l2cap_chan_no_defer,
1033 .set_shutdown = l2cap_chan_no_set_shutdown,
1034};
1035
1036static inline __u8 bdaddr_type(__u8 type)
1037{
1038 if (type == ADDR_LE_DEV_PUBLIC)
1039 return BDADDR_LE_PUBLIC;
1040 else
1041 return BDADDR_LE_RANDOM;
1042}
1043
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001044static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
1045{
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001046 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001047 int err;
1048
Johan Hedberg26d46df2015-10-06 13:03:24 +03001049 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001050 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001051 return -EINVAL;
1052
Johan Hedberg26d46df2015-10-06 13:03:24 +03001053 chan->ops = &bt_6lowpan_chan_ops;
1054
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001055 err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001056 addr, dst_type);
1057
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001058 BT_DBG("chan %p err %d", chan, err);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001059 if (err < 0)
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001060 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001061
Jukka Rissanen18722c22013-12-11 17:05:37 +02001062 return err;
1063}
1064
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001065static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1066{
1067 struct lowpan_peer *peer;
1068
1069 BT_DBG("conn %p dst type %d", conn, dst_type);
1070
1071 peer = lookup_peer(conn);
1072 if (!peer)
1073 return -ENOENT;
1074
1075 BT_DBG("peer %p chan %p", peer, peer->chan);
1076
1077 l2cap_chan_close(peer->chan, ENOENT);
1078
1079 return 0;
1080}
1081
1082static struct l2cap_chan *bt_6lowpan_listen(void)
1083{
1084 bdaddr_t *addr = BDADDR_ANY;
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001085 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001086 int err;
1087
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001088 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001089 return NULL;
1090
Johan Hedberg26d46df2015-10-06 13:03:24 +03001091 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001092 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001093 return NULL;
1094
Johan Hedberg26d46df2015-10-06 13:03:24 +03001095 chan->ops = &bt_6lowpan_chan_ops;
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001096 chan->state = BT_LISTEN;
1097 chan->src_type = BDADDR_LE_PUBLIC;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001098
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001099 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
Johan Hedberg2773b022014-11-13 09:46:05 +02001100
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001101 BT_DBG("chan %p src type %d", chan, chan->src_type);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001102
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001103 err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001104 if (err) {
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001105 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001106 BT_ERR("psm cannot be added err %d", err);
1107 return NULL;
1108 }
1109
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001110 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001111}
1112
1113static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1114 struct l2cap_conn **conn)
1115{
1116 struct hci_conn *hcon;
1117 struct hci_dev *hdev;
1118 bdaddr_t *src = BDADDR_ANY;
1119 int n;
1120
1121 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1122 &addr->b[5], &addr->b[4], &addr->b[3],
1123 &addr->b[2], &addr->b[1], &addr->b[0],
1124 addr_type);
1125
1126 if (n < 7)
1127 return -EINVAL;
1128
1129 hdev = hci_get_route(addr, src);
1130 if (!hdev)
1131 return -ENOENT;
1132
1133 hci_dev_lock(hdev);
1134 hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr);
1135 hci_dev_unlock(hdev);
1136
1137 if (!hcon)
1138 return -ENOENT;
1139
1140 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1141
1142 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1143
1144 return 0;
1145}
1146
1147static void disconnect_all_peers(void)
1148{
Jukka Rissanen90305822014-10-28 17:16:47 +02001149 struct lowpan_dev *entry;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001150 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1151 struct list_head peers;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001152
1153 INIT_LIST_HEAD(&peers);
1154
1155 /* We make a separate list of peers as the close_cb() will
1156 * modify the device peers list so it is better not to mess
1157 * with the same list at the same time.
1158 */
1159
Jukka Rissanen90305822014-10-28 17:16:47 +02001160 rcu_read_lock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001161
Jukka Rissanen90305822014-10-28 17:16:47 +02001162 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1163 list_for_each_entry_rcu(peer, &entry->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001164 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1165 if (!new_peer)
1166 break;
1167
1168 new_peer->chan = peer->chan;
1169 INIT_LIST_HEAD(&new_peer->list);
1170
1171 list_add(&new_peer->list, &peers);
1172 }
1173 }
1174
Jukka Rissanen90305822014-10-28 17:16:47 +02001175 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001176
Jukka Rissanen90305822014-10-28 17:16:47 +02001177 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001178 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1179 l2cap_chan_close(peer->chan, ENOENT);
Jukka Rissanen90305822014-10-28 17:16:47 +02001180
1181 list_del_rcu(&peer->list);
Johan Hedberg4e790222014-11-11 14:16:29 +02001182 kfree_rcu(peer, rcu);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001183 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001184 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001185}
1186
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001187struct set_enable {
Jukka Rissanen90305822014-10-28 17:16:47 +02001188 struct work_struct work;
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001189 bool flag;
Jukka Rissanen90305822014-10-28 17:16:47 +02001190};
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001191
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001192static void do_enable_set(struct work_struct *work)
Jukka Rissanen90305822014-10-28 17:16:47 +02001193{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001194 struct set_enable *set_enable = container_of(work,
1195 struct set_enable, work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001196
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001197 if (!set_enable->flag || enable_6lowpan != set_enable->flag)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001198 /* Disconnect existing connections if 6lowpan is
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001199 * disabled
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001200 */
1201 disconnect_all_peers();
1202
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001203 enable_6lowpan = set_enable->flag;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001204
1205 if (listen_chan) {
1206 l2cap_chan_close(listen_chan, 0);
1207 l2cap_chan_put(listen_chan);
1208 }
1209
1210 listen_chan = bt_6lowpan_listen();
1211
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001212 kfree(set_enable);
Jukka Rissanen90305822014-10-28 17:16:47 +02001213}
1214
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001215static int lowpan_enable_set(void *data, u64 val)
Jukka Rissanen90305822014-10-28 17:16:47 +02001216{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001217 struct set_enable *set_enable;
Jukka Rissanen90305822014-10-28 17:16:47 +02001218
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001219 set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
1220 if (!set_enable)
Jukka Rissanen90305822014-10-28 17:16:47 +02001221 return -ENOMEM;
1222
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001223 set_enable->flag = !!val;
1224 INIT_WORK(&set_enable->work, do_enable_set);
Jukka Rissanen90305822014-10-28 17:16:47 +02001225
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001226 schedule_work(&set_enable->work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001227
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001228 return 0;
1229}
1230
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001231static int lowpan_enable_get(void *data, u64 *val)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001232{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001233 *val = enable_6lowpan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001234 return 0;
1235}
1236
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001237DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
1238 lowpan_enable_set, "%llu\n");
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001239
1240static ssize_t lowpan_control_write(struct file *fp,
1241 const char __user *user_buffer,
1242 size_t count,
1243 loff_t *position)
1244{
1245 char buf[32];
1246 size_t buf_size = min(count, sizeof(buf) - 1);
1247 int ret;
1248 bdaddr_t addr;
1249 u8 addr_type;
1250 struct l2cap_conn *conn = NULL;
1251
1252 if (copy_from_user(buf, user_buffer, buf_size))
1253 return -EFAULT;
1254
1255 buf[buf_size] = '\0';
1256
1257 if (memcmp(buf, "connect ", 8) == 0) {
1258 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1259 if (ret == -EINVAL)
1260 return ret;
1261
1262 if (listen_chan) {
1263 l2cap_chan_close(listen_chan, 0);
1264 l2cap_chan_put(listen_chan);
1265 listen_chan = NULL;
1266 }
1267
1268 if (conn) {
1269 struct lowpan_peer *peer;
1270
1271 if (!is_bt_6lowpan(conn->hcon))
1272 return -EINVAL;
1273
1274 peer = lookup_peer(conn);
1275 if (peer) {
1276 BT_DBG("6LoWPAN connection already exists");
1277 return -EALREADY;
1278 }
1279
1280 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1281 &conn->hcon->dst, conn->hcon->dst_type,
1282 addr_type);
1283 }
1284
1285 ret = bt_6lowpan_connect(&addr, addr_type);
1286 if (ret < 0)
1287 return ret;
1288
1289 return count;
1290 }
1291
1292 if (memcmp(buf, "disconnect ", 11) == 0) {
1293 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1294 if (ret < 0)
1295 return ret;
1296
1297 ret = bt_6lowpan_disconnect(conn, addr_type);
1298 if (ret < 0)
1299 return ret;
1300
1301 return count;
1302 }
1303
1304 return count;
1305}
1306
1307static int lowpan_control_show(struct seq_file *f, void *ptr)
1308{
Jukka Rissanen90305822014-10-28 17:16:47 +02001309 struct lowpan_dev *entry;
1310 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001311
Jukka Rissanen90305822014-10-28 17:16:47 +02001312 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001313
Jukka Rissanen90305822014-10-28 17:16:47 +02001314 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1315 list_for_each_entry(peer, &entry->peers, list)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001316 seq_printf(f, "%pMR (type %u)\n",
1317 &peer->chan->dst, peer->chan->dst_type);
1318 }
1319
Jukka Rissanen90305822014-10-28 17:16:47 +02001320 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001321
1322 return 0;
1323}
1324
1325static int lowpan_control_open(struct inode *inode, struct file *file)
1326{
1327 return single_open(file, lowpan_control_show, inode->i_private);
1328}
1329
1330static const struct file_operations lowpan_control_fops = {
1331 .open = lowpan_control_open,
1332 .read = seq_read,
1333 .write = lowpan_control_write,
1334 .llseek = seq_lseek,
1335 .release = single_release,
1336};
1337
Jukka Rissanen7f118252014-06-18 16:37:11 +03001338static void disconnect_devices(void)
1339{
Dan Carpenterdaac1972014-10-29 19:10:57 +03001340 struct lowpan_dev *entry, *tmp, *new_dev;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001341 struct list_head devices;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001342
1343 INIT_LIST_HEAD(&devices);
1344
1345 /* We make a separate list of devices because the unregister_netdev()
1346 * will call device_event() which will also want to modify the same
1347 * devices list.
1348 */
1349
Jukka Rissanen90305822014-10-28 17:16:47 +02001350 rcu_read_lock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001351
Jukka Rissanen90305822014-10-28 17:16:47 +02001352 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001353 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1354 if (!new_dev)
1355 break;
1356
1357 new_dev->netdev = entry->netdev;
1358 INIT_LIST_HEAD(&new_dev->list);
1359
Jukka Rissanen90305822014-10-28 17:16:47 +02001360 list_add_rcu(&new_dev->list, &devices);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001361 }
1362
Jukka Rissanen90305822014-10-28 17:16:47 +02001363 rcu_read_unlock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001364
Dan Carpenterdaac1972014-10-29 19:10:57 +03001365 list_for_each_entry_safe(entry, tmp, &devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001366 ifdown(entry->netdev);
1367 BT_DBG("Unregistering netdev %s %p",
1368 entry->netdev->name, entry->netdev);
1369 unregister_netdev(entry->netdev);
1370 kfree(entry);
1371 }
1372}
1373
Jukka Rissanen18722c22013-12-11 17:05:37 +02001374static int device_event(struct notifier_block *unused,
1375 unsigned long event, void *ptr)
1376{
1377 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Jukka Rissanen90305822014-10-28 17:16:47 +02001378 struct lowpan_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +02001379
1380 if (netdev->type != ARPHRD_6LOWPAN)
1381 return NOTIFY_DONE;
1382
1383 switch (event) {
1384 case NETDEV_UNREGISTER:
Jukka Rissanen90305822014-10-28 17:16:47 +02001385 spin_lock(&devices_lock);
1386 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +02001387 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001388 BT_DBG("Unregistered netdev %s %p",
1389 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001390 list_del(&entry->list);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001391 break;
1392 }
1393 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001394 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001395 break;
1396 }
1397
1398 return NOTIFY_DONE;
1399}
1400
1401static struct notifier_block bt_6lowpan_dev_notifier = {
1402 .notifier_call = device_event,
1403};
1404
Jukka Rissanen5547e482014-06-18 16:37:09 +03001405static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001406{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001407 lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644,
1408 bt_debugfs, NULL,
1409 &lowpan_enable_fops);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001410 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1411 bt_debugfs, NULL,
1412 &lowpan_control_fops);
1413
Jukka Rissanen18722c22013-12-11 17:05:37 +02001414 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1415}
1416
Jukka Rissanen5547e482014-06-18 16:37:09 +03001417static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001418{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001419 debugfs_remove(lowpan_enable_debugfs);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001420 debugfs_remove(lowpan_control_debugfs);
1421
1422 if (listen_chan) {
1423 l2cap_chan_close(listen_chan, 0);
1424 l2cap_chan_put(listen_chan);
1425 }
1426
Jukka Rissanen7f118252014-06-18 16:37:11 +03001427 disconnect_devices();
1428
Jukka Rissanen18722c22013-12-11 17:05:37 +02001429 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1430}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001431
1432module_init(bt_6lowpan_init);
1433module_exit(bt_6lowpan_exit);
1434
1435MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1436MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1437MODULE_VERSION(VERSION);
1438MODULE_LICENSE("GPL");