blob: 54bf4d765a7d4d3df76e70e639652aedc7c818fe [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
Jukka Rissanen18722c22013-12-11 17:05:37 +020024#include <net/bluetooth/bluetooth.h>
25#include <net/bluetooth/hci_core.h>
26#include <net/bluetooth/l2cap.h>
27
Alexander Aringcefc8c82014-03-05 14:29:05 +010028#include <net/6lowpan.h> /* for the compression support */
Jukka Rissanen18722c22013-12-11 17:05:37 +020029
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030030#define VERSION "0.1"
31
Jukka Rissanen7b2ed602015-01-08 17:00:55 +020032static struct dentry *lowpan_enable_debugfs;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030033static struct dentry *lowpan_control_debugfs;
34
Jukka Rissanen18722c22013-12-11 17:05:37 +020035#define IFACE_NAME_TEMPLATE "bt%d"
Jukka Rissanen18722c22013-12-11 17:05:37 +020036
37struct skb_cb {
38 struct in6_addr addr;
Jukka Rissanen39e90c72014-09-08 12:11:45 +030039 struct in6_addr gw;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030040 struct l2cap_chan *chan;
41 int status;
Jukka Rissanen18722c22013-12-11 17:05:37 +020042};
43#define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb))
44
45/* The devices list contains those devices that we are acting
46 * as a proxy. The BT 6LoWPAN device is a virtual device that
47 * connects to the Bluetooth LE device. The real connection to
48 * BT device is done via l2cap layer. There exists one
49 * virtual device / one BT 6LoWPAN network (=hciX device).
50 * The list contains struct lowpan_dev elements.
51 */
52static LIST_HEAD(bt_6lowpan_devices);
Jukka Rissanen90305822014-10-28 17:16:47 +020053static DEFINE_SPINLOCK(devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +020054
Jukka Rissanen7b2ed602015-01-08 17:00:55 +020055static bool enable_6lowpan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030056
57/* We are listening incoming connections via this channel
58 */
59static struct l2cap_chan *listen_chan;
60
Jukka Rissanen18722c22013-12-11 17:05:37 +020061struct lowpan_peer {
62 struct list_head list;
Jukka Rissanen90305822014-10-28 17:16:47 +020063 struct rcu_head rcu;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +030064 struct l2cap_chan *chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +020065
66 /* peer addresses in various formats */
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +020067 unsigned char lladdr[ETH_ALEN];
Jukka Rissanen18722c22013-12-11 17:05:37 +020068 struct in6_addr peer_addr;
69};
70
Alexander Aring2e4d60c2016-04-11 11:04:18 +020071struct lowpan_btle_dev {
Jukka Rissanen18722c22013-12-11 17:05:37 +020072 struct list_head list;
73
74 struct hci_dev *hdev;
75 struct net_device *netdev;
76 struct list_head peers;
77 atomic_t peer_count; /* number of items in peers list */
78
79 struct work_struct delete_netdev;
80 struct delayed_work notify_peers;
81};
82
Alexander Aring2e4d60c2016-04-11 11:04:18 +020083static inline struct lowpan_btle_dev *
84lowpan_btle_dev(const struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +020085{
Alexander Aring2e4d60c2016-04-11 11:04:18 +020086 return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv;
Jukka Rissanen18722c22013-12-11 17:05:37 +020087}
88
Alexander Aring2e4d60c2016-04-11 11:04:18 +020089static inline void peer_add(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_add_rcu(&peer->list, &dev->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +020093 atomic_inc(&dev->peer_count);
94}
95
Alexander Aring2e4d60c2016-04-11 11:04:18 +020096static inline bool peer_del(struct lowpan_btle_dev *dev,
97 struct lowpan_peer *peer)
Jukka Rissanen18722c22013-12-11 17:05:37 +020098{
Jukka Rissanen90305822014-10-28 17:16:47 +020099 list_del_rcu(&peer->list);
Johan Hedberg4e790222014-11-11 14:16:29 +0200100 kfree_rcu(peer, rcu);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200101
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300102 module_put(THIS_MODULE);
103
Jukka Rissanen18722c22013-12-11 17:05:37 +0200104 if (atomic_dec_and_test(&dev->peer_count)) {
105 BT_DBG("last peer");
106 return true;
107 }
108
109 return false;
110}
111
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200112static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_btle_dev *dev,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200113 bdaddr_t *ba, __u8 type)
114{
Jukka Rissanen90305822014-10-28 17:16:47 +0200115 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200116
117 BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count),
118 ba, type);
119
Jukka Rissanen90305822014-10-28 17:16:47 +0200120 rcu_read_lock();
121
122 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300123 BT_DBG("dst addr %pMR dst type %d",
124 &peer->chan->dst, peer->chan->dst_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200125
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300126 if (bacmp(&peer->chan->dst, ba))
Jukka Rissanen18722c22013-12-11 17:05:37 +0200127 continue;
128
Jukka Rissanen90305822014-10-28 17:16:47 +0200129 if (type == peer->chan->dst_type) {
130 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300131 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200132 }
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300133 }
134
Jukka Rissanen90305822014-10-28 17:16:47 +0200135 rcu_read_unlock();
136
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300137 return NULL;
138}
139
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200140static inline struct lowpan_peer *
141__peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300142{
Jukka Rissanen90305822014-10-28 17:16:47 +0200143 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300144
Jukka Rissanen90305822014-10-28 17:16:47 +0200145 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300146 if (peer->chan == chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200147 return peer;
148 }
149
150 return NULL;
151}
152
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200153static inline struct lowpan_peer *
154__peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200155{
Jukka Rissanen90305822014-10-28 17:16:47 +0200156 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200157
Jukka Rissanen90305822014-10-28 17:16:47 +0200158 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300159 if (peer->chan->conn == conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200160 return peer;
161 }
162
163 return NULL;
164}
165
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200166static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev,
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300167 struct in6_addr *daddr,
168 struct sk_buff *skb)
169{
Jukka Rissanen90305822014-10-28 17:16:47 +0200170 struct lowpan_peer *peer;
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300171 struct in6_addr *nexthop;
172 struct rt6_info *rt = (struct rt6_info *)skb_dst(skb);
173 int count = atomic_read(&dev->peer_count);
174
175 BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt);
176
177 /* If we have multiple 6lowpan peers, then check where we should
178 * send the packet. If only one peer exists, then we can send the
179 * packet right away.
180 */
Jukka Rissanen90305822014-10-28 17:16:47 +0200181 if (count == 1) {
182 rcu_read_lock();
183 peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer,
184 list);
185 rcu_read_unlock();
186 return peer;
187 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300188
189 if (!rt) {
190 nexthop = &lowpan_cb(skb)->gw;
191
192 if (ipv6_addr_any(nexthop))
193 return NULL;
194 } else {
Martin KaFai Lau2647a9b2015-05-22 20:55:58 -0700195 nexthop = rt6_nexthop(rt, daddr);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300196
197 /* We need to remember the address because it is needed
198 * by bt_xmit() when sending the packet. In bt_xmit(), the
199 * destination routing info is not set.
200 */
201 memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr));
202 }
203
204 BT_DBG("gw %pI6c", nexthop);
205
Jukka Rissanen90305822014-10-28 17:16:47 +0200206 rcu_read_lock();
207
208 list_for_each_entry_rcu(peer, &dev->peers, list) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300209 BT_DBG("dst addr %pMR dst type %d ip %pI6c",
210 &peer->chan->dst, peer->chan->dst_type,
211 &peer->peer_addr);
212
Jukka Rissanen90305822014-10-28 17:16:47 +0200213 if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) {
214 rcu_read_unlock();
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300215 return peer;
Jukka Rissanen90305822014-10-28 17:16:47 +0200216 }
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300217 }
218
Jukka Rissanen90305822014-10-28 17:16:47 +0200219 rcu_read_unlock();
220
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300221 return NULL;
222}
223
Jukka Rissanen18722c22013-12-11 17:05:37 +0200224static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn)
225{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200226 struct lowpan_btle_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200227 struct lowpan_peer *peer = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200228
Jukka Rissanen90305822014-10-28 17:16:47 +0200229 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200230
Jukka Rissanen90305822014-10-28 17:16:47 +0200231 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
232 peer = __peer_lookup_conn(entry, conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200233 if (peer)
234 break;
235 }
236
Jukka Rissanen90305822014-10-28 17:16:47 +0200237 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200238
239 return peer;
240}
241
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200242static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200243{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200244 struct lowpan_btle_dev *entry;
245 struct lowpan_btle_dev *dev = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200246
Jukka Rissanen90305822014-10-28 17:16:47 +0200247 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200248
Jukka Rissanen90305822014-10-28 17:16:47 +0200249 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +0200250 if (conn->hcon->hdev == entry->hdev) {
251 dev = entry;
252 break;
253 }
254 }
255
Jukka Rissanen90305822014-10-28 17:16:47 +0200256 rcu_read_unlock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200257
258 return dev;
259}
260
Jukka Rissanen18722c22013-12-11 17:05:37 +0200261static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev)
262{
263 struct sk_buff *skb_cp;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200264
265 skb_cp = skb_copy(skb, GFP_ATOMIC);
266 if (!skb_cp)
Martin Townsendf8b36172014-10-23 15:40:53 +0100267 return NET_RX_DROP;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200268
Alexander Aring324e7862015-10-27 08:35:24 +0100269 return netif_rx_ni(skb_cp);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200270}
271
Martin Townsend01141232014-10-23 15:40:56 +0100272static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev,
273 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200274{
Patrik Flyktc259d142017-03-12 10:19:33 +0200275 const u8 *saddr;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200276 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200277 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200278
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200279 dev = lowpan_btle_dev(netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200280
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
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200287 saddr = peer->lladdr;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200288
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200289 return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200290}
291
292static int recv_pkt(struct sk_buff *skb, struct net_device *dev,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300293 struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200294{
295 struct sk_buff *local_skb;
296 int ret;
297
298 if (!netif_running(dev))
299 goto drop;
300
Alexander Aringcefdb802015-10-13 13:42:55 +0200301 if (dev->type != ARPHRD_6LOWPAN || !skb->len)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200302 goto drop;
303
Alexander Aringcefdb802015-10-13 13:42:55 +0200304 skb_reset_network_header(skb);
305
Martin Townsend11e3ff702014-10-13 11:00:56 +0100306 skb = skb_share_check(skb, GFP_ATOMIC);
307 if (!skb)
308 goto drop;
309
Jukka Rissanen18722c22013-12-11 17:05:37 +0200310 /* check that it's our buffer */
Alexander Aringcefdb802015-10-13 13:42:55 +0200311 if (lowpan_is_ipv6(*skb_network_header(skb))) {
Lukasz Duda87f5fed2016-01-13 16:57:48 +0100312 /* Pull off the 1-byte of 6lowpan header. */
313 skb_pull(skb, 1);
314
Jukka Rissanen18722c22013-12-11 17:05:37 +0200315 /* Copy the packet so that the IPv6 header is
316 * properly aligned.
317 */
318 local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1,
319 skb_tailroom(skb), GFP_ATOMIC);
320 if (!local_skb)
321 goto drop;
322
323 local_skb->protocol = htons(ETH_P_IPV6);
324 local_skb->pkt_type = PACKET_HOST;
Glenn Ruben Bakke4c58f322016-01-13 16:41:42 +0100325 local_skb->dev = dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200326
Jukka Rissanen18722c22013-12-11 17:05:37 +0200327 skb_set_transport_header(local_skb, sizeof(struct ipv6hdr));
328
329 if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) {
330 kfree_skb(local_skb);
331 goto drop;
332 }
333
334 dev->stats.rx_bytes += skb->len;
335 dev->stats.rx_packets++;
336
Martin Townsend3c400b82014-10-23 15:40:55 +0100337 consume_skb(local_skb);
338 consume_skb(skb);
Alexander Aringcefdb802015-10-13 13:42:55 +0200339 } else if (lowpan_is_iphc(*skb_network_header(skb))) {
340 local_skb = skb_clone(skb, GFP_ATOMIC);
341 if (!local_skb)
342 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200343
Glenn Ruben Bakke4c58f322016-01-13 16:41:42 +0100344 local_skb->dev = dev;
345
Alexander Aringcefdb802015-10-13 13:42:55 +0200346 ret = iphc_decompress(local_skb, dev, chan);
347 if (ret < 0) {
348 kfree_skb(local_skb);
349 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200350 }
Alexander Aringcefdb802015-10-13 13:42:55 +0200351
352 local_skb->protocol = htons(ETH_P_IPV6);
353 local_skb->pkt_type = PACKET_HOST;
Alexander Aringcefdb802015-10-13 13:42:55 +0200354
355 if (give_skb_to_upper(local_skb, dev)
356 != NET_RX_SUCCESS) {
357 kfree_skb(local_skb);
358 goto drop;
359 }
360
361 dev->stats.rx_bytes += skb->len;
362 dev->stats.rx_packets++;
363
364 consume_skb(local_skb);
365 consume_skb(skb);
366 } else {
367 goto drop;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200368 }
369
370 return NET_RX_SUCCESS;
371
372drop:
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300373 dev->stats.rx_dropped++;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200374 return NET_RX_DROP;
375}
376
377/* Packet from BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300378static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200379{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200380 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200381 struct lowpan_peer *peer;
382 int err;
383
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300384 peer = lookup_peer(chan->conn);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200385 if (!peer)
386 return -ENOENT;
387
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300388 dev = lookup_dev(chan->conn);
Johan Hedberg30d3db42013-12-12 09:53:21 +0200389 if (!dev || !dev->netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200390 return -ENOENT;
391
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300392 err = recv_pkt(skb, dev->netdev, chan);
393 if (err) {
394 BT_DBG("recv pkt %d", err);
395 err = -EAGAIN;
396 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200397
398 return err;
399}
400
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300401static u8 get_addr_type_from_eui64(u8 byte)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200402{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300403 /* Is universal(0) or local(1) bit */
404 return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC);
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300405}
406
407static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr)
408{
409 u8 *eui64 = ip6_daddr->s6_addr + 8;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200410
411 addr->b[0] = eui64[7];
412 addr->b[1] = eui64[6];
413 addr->b[2] = eui64[5];
414 addr->b[3] = eui64[2];
415 addr->b[4] = eui64[1];
416 addr->b[5] = eui64[0];
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300417}
Jukka Rissanen18722c22013-12-11 17:05:37 +0200418
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300419static void convert_dest_bdaddr(struct in6_addr *ip6_daddr,
420 bdaddr_t *addr, u8 *addr_type)
421{
422 copy_to_bdaddr(ip6_daddr, addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200423
Jukka Rissanen62bbd5b2014-05-27 11:33:22 +0300424 /* We need to toggle the U/L bit that we got from IPv6 address
425 * so that we get the proper address and type of the BD address.
426 */
427 addr->b[5] ^= 0x02;
428
429 *addr_type = get_addr_type_from_eui64(addr->b[5]);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200430}
431
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300432static int setup_header(struct sk_buff *skb, struct net_device *netdev,
433 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200434{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300435 struct in6_addr ipv6_daddr;
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200436 struct ipv6hdr *hdr;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200437 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200438 struct lowpan_peer *peer;
439 bdaddr_t addr, *any = BDADDR_ANY;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300440 u8 *daddr = any->b;
441 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200442
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200443 hdr = ipv6_hdr(skb);
444
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200445 dev = lowpan_btle_dev(netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200446
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200447 memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr));
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300448
449 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300450 lowpan_cb(skb)->chan = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200451 } else {
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300452 u8 addr_type;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200453
454 /* Get destination BT device from skb.
455 * If there is no such peer then discard the packet.
456 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300457 convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200458
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300459 BT_DBG("dest addr %pMR type %d IP %pI6c", &addr,
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300460 addr_type, &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200461
Jukka Rissanen18722c22013-12-11 17:05:37 +0200462 peer = peer_lookup_ba(dev, &addr, addr_type);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200463 if (!peer) {
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300464 /* The packet might be sent to 6lowpan interface
465 * because of routing (either via default route
466 * or user set route) so get peer according to
467 * the destination address.
468 */
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300469 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen39e90c72014-09-08 12:11:45 +0300470 if (!peer) {
471 BT_DBG("no such peer %pMR found", &addr);
472 return -ENOENT;
473 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200474 }
475
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200476 daddr = peer->lladdr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300477 *peer_addr = addr;
478 *peer_addr_type = addr_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300479 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300480
481 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200482 }
483
Alexander Aringa6f77382015-10-13 13:42:57 +0200484 lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200485
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300486 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
487 if (err < 0)
488 return err;
489
490 return status;
491}
492
493static int header_create(struct sk_buff *skb, struct net_device *netdev,
494 unsigned short type, const void *_daddr,
495 const void *_saddr, unsigned int len)
496{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300497 if (type != ETH_P_IPV6)
498 return -EINVAL;
499
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300500 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200501}
502
503/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300504static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300505 struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200506{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300507 struct msghdr msg;
508 struct kvec iv;
509 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200510
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300511 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300512 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300513 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300514 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300515
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300516 iv.iov_base = skb->data;
517 iv.iov_len = skb->len;
518
Al Viroc0371da2014-11-24 10:42:55 -0500519 memset(&msg, 0, sizeof(msg));
Al Viro17836392014-11-24 17:07:38 -0500520 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len);
Al Viroc0371da2014-11-24 10:42:55 -0500521
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300522 err = l2cap_chan_send(chan, &msg, skb->len);
523 if (err > 0) {
524 netdev->stats.tx_bytes += err;
525 netdev->stats.tx_packets++;
526 return 0;
527 }
528
529 if (!err)
530 err = lowpan_cb(skb)->status;
531
532 if (err < 0) {
533 if (err == -EAGAIN)
534 netdev->stats.tx_dropped++;
535 else
536 netdev->stats.tx_errors++;
537 }
538
539 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200540}
541
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300542static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200543{
544 struct sk_buff *local_skb;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200545 struct lowpan_btle_dev *entry;
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300546 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200547
Jukka Rissanen90305822014-10-28 17:16:47 +0200548 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200549
Jukka Rissanen90305822014-10-28 17:16:47 +0200550 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
551 struct lowpan_peer *pentry;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200552 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200553
554 if (entry->netdev != netdev)
555 continue;
556
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200557 dev = lowpan_btle_dev(entry->netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200558
Jukka Rissanen90305822014-10-28 17:16:47 +0200559 list_for_each_entry_rcu(pentry, &dev->peers, list) {
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300560 int ret;
561
Jukka Rissanen18722c22013-12-11 17:05:37 +0200562 local_skb = skb_clone(skb, GFP_ATOMIC);
563
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300564 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
565 netdev->name,
566 &pentry->chan->dst, pentry->chan->dst_type,
567 &pentry->peer_addr, pentry->chan);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300568 ret = send_pkt(pentry->chan, local_skb, netdev);
569 if (ret < 0)
570 err = ret;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200571
572 kfree_skb(local_skb);
573 }
574 }
575
Jukka Rissanen90305822014-10-28 17:16:47 +0200576 rcu_read_unlock();
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300577
578 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200579}
580
581static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
582{
583 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200584 bdaddr_t addr;
585 u8 addr_type;
586
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300587 /* We must take a copy of the skb before we modify/replace the ipv6
588 * header as the header could be used elsewhere
589 */
Alexander Aringb0c42cd2014-10-08 10:24:53 +0200590 skb = skb_unshare(skb, GFP_ATOMIC);
591 if (!skb)
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300592 return NET_XMIT_DROP;
593
594 /* Return values from setup_header()
595 * <0 - error, packet is dropped
596 * 0 - this is a multicast packet
597 * 1 - this is unicast packet
598 */
599 err = setup_header(skb, netdev, &addr, &addr_type);
600 if (err < 0) {
601 kfree_skb(skb);
602 return NET_XMIT_DROP;
603 }
604
605 if (err) {
606 if (lowpan_cb(skb)->chan) {
607 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
608 netdev->name, &addr, addr_type,
609 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300610 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300611 } else {
612 err = -ENOENT;
613 }
614 } else {
615 /* We need to send the packet to every device behind this
616 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200617 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300618 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200619 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200620
Jukka Rissanenfc125182014-10-01 11:30:26 +0300621 dev_kfree_skb(skb);
622
Jukka Rissanen18722c22013-12-11 17:05:37 +0200623 if (err)
624 BT_DBG("ERROR: xmit failed (%d)", err);
625
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300626 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200627}
628
Jukka Rissanendf092302014-10-28 17:16:48 +0200629static int bt_dev_init(struct net_device *dev)
630{
Eric Dumazetd3fff6c2016-06-09 07:45:12 -0700631 netdev_lockdep_set_classes(dev);
Jukka Rissanendf092302014-10-28 17:16:48 +0200632
633 return 0;
634}
635
Jukka Rissanen18722c22013-12-11 17:05:37 +0200636static const struct net_device_ops netdev_ops = {
Jukka Rissanendf092302014-10-28 17:16:48 +0200637 .ndo_init = bt_dev_init,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200638 .ndo_start_xmit = bt_xmit,
639};
640
641static struct header_ops header_ops = {
642 .create = header_create,
643};
644
645static void netdev_setup(struct net_device *dev)
646{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200647 dev->hard_header_len = 0;
648 dev->needed_tailroom = 0;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300649 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
650 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200651 dev->watchdog_timeo = 0;
652
653 dev->netdev_ops = &netdev_ops;
654 dev->header_ops = &header_ops;
655 dev->destructor = free_netdev;
656}
657
658static struct device_type bt_type = {
659 .name = "bluetooth",
660};
661
Jukka Rissanen18722c22013-12-11 17:05:37 +0200662static void ifup(struct net_device *netdev)
663{
664 int err;
665
666 rtnl_lock();
667 err = dev_open(netdev);
668 if (err < 0)
669 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
670 rtnl_unlock();
671}
672
Jukka Rissanen7f118252014-06-18 16:37:11 +0300673static void ifdown(struct net_device *netdev)
674{
675 int err;
676
677 rtnl_lock();
678 err = dev_close(netdev);
679 if (err < 0)
680 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
681 rtnl_unlock();
682}
683
Jukka Rissanen18722c22013-12-11 17:05:37 +0200684static void do_notify_peers(struct work_struct *work)
685{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200686 struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev,
687 notify_peers.work);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200688
689 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
690}
691
692static bool is_bt_6lowpan(struct hci_conn *hcon)
693{
694 if (hcon->type != LE_LINK)
695 return false;
696
Jukka Rissanen7b2ed602015-01-08 17:00:55 +0200697 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300698 return false;
699
700 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200701}
702
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300703static struct l2cap_chan *chan_create(void)
704{
705 struct l2cap_chan *chan;
706
707 chan = l2cap_chan_create();
708 if (!chan)
709 return NULL;
710
711 l2cap_chan_set_defaults(chan);
712
713 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
714 chan->mode = L2CAP_MODE_LE_FLOWCTL;
Johan Hedberg301de2c2015-10-06 13:03:19 +0300715 chan->imtu = 1280;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300716
717 return chan;
718}
719
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300720static void set_ip_addr_bits(u8 addr_type, u8 *addr)
721{
722 if (addr_type == BDADDR_LE_PUBLIC)
723 *addr |= 0x02;
724 else
725 *addr &= ~0x02;
726}
727
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300728static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200729 struct lowpan_btle_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200730{
731 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200732
733 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
734 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300735 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200736
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300737 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200738 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
739
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200740 baswap((void *)peer->lladdr, &chan->dst);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200741
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200742 lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200743
Jukka Rissanenb2799ce2014-09-08 12:11:44 +0300744 /* IPv6 address needs to have the U/L bit set properly so toggle
745 * it back here.
746 */
747 set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8);
748
Jukka Rissanen90305822014-10-28 17:16:47 +0200749 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200750 INIT_LIST_HEAD(&peer->list);
751 peer_add(dev, peer);
Jukka Rissanen90305822014-10-28 17:16:47 +0200752 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200753
754 /* Notifying peers about us needs to be done without locks held */
755 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
756 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
757
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300758 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200759}
760
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200761static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200762{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200763 struct net_device *netdev;
764 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200765
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200766 netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)),
Alexander Aringb72f6f52015-08-11 21:44:08 +0200767 IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
768 netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200769 if (!netdev)
770 return -ENOMEM;
771
Patrik Flyktc259d142017-03-12 10:19:33 +0200772 netdev->addr_assign_type = NET_ADDR_PERM;
773 baswap((void *)netdev->dev_addr, &chan->src);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200774
775 netdev->netdev_ops = &netdev_ops;
Glenn Ruben Bakkefc842422015-06-17 07:32:25 -0700776 SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200777 SET_NETDEV_DEVTYPE(netdev, &bt_type);
778
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200779 *dev = lowpan_btle_dev(netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300780 (*dev)->netdev = netdev;
781 (*dev)->hdev = chan->conn->hcon->hdev;
782 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200783
Jukka Rissanen90305822014-10-28 17:16:47 +0200784 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300785 INIT_LIST_HEAD(&(*dev)->list);
Jukka Rissanen90305822014-10-28 17:16:47 +0200786 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
787 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200788
Alexander Aring00f59312015-12-09 22:46:29 +0100789 err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
Alexander Aring5857d1d2015-07-30 09:40:53 +0200790 if (err < 0) {
791 BT_INFO("register_netdev failed %d", err);
792 spin_lock(&devices_lock);
793 list_del_rcu(&(*dev)->list);
794 spin_unlock(&devices_lock);
795 free_netdev(netdev);
796 goto out;
797 }
798
799 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
800 netdev->ifindex, &chan->dst, chan->dst_type,
801 &chan->src, chan->src_type);
802 set_bit(__LINK_STATE_PRESENT, &netdev->state);
803
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300804 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200805
806out:
807 return err;
808}
809
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300810static inline void chan_ready_cb(struct l2cap_chan *chan)
811{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200812 struct lowpan_btle_dev *dev;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300813
814 dev = lookup_dev(chan->conn);
815
816 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
817
818 if (!dev) {
819 if (setup_netdev(chan, &dev) < 0) {
820 l2cap_chan_del(chan, -ENOENT);
821 return;
822 }
823 }
824
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300825 if (!try_module_get(THIS_MODULE))
826 return;
827
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300828 add_peer_chan(chan, dev);
829 ifup(dev->netdev);
830}
831
Johan Hedberg2b293492014-08-07 10:03:32 +0300832static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300833{
Johan Hedberg2b293492014-08-07 10:03:32 +0300834 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300835
Johan Hedberg630ef792015-10-06 13:03:22 +0300836 chan = chan_create();
837 if (!chan)
838 return NULL;
839
Johan Hedberg2b293492014-08-07 10:03:32 +0300840 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300841
842 BT_DBG("chan %p pchan %p", chan, pchan);
843
Johan Hedberg2b293492014-08-07 10:03:32 +0300844 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300845}
846
Jukka Rissanen18722c22013-12-11 17:05:37 +0200847static void delete_netdev(struct work_struct *work)
848{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200849 struct lowpan_btle_dev *entry = container_of(work,
850 struct lowpan_btle_dev,
851 delete_netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200852
Alexander Aring00f59312015-12-09 22:46:29 +0100853 lowpan_unregister_netdev(entry->netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200854
Glenn Ruben Bakke2ad88fb2015-06-17 07:32:26 -0700855 /* The entry pointer is deleted by the netdev destructor. */
Jukka Rissanen18722c22013-12-11 17:05:37 +0200856}
857
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300858static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200859{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200860 struct lowpan_btle_dev *entry;
861 struct lowpan_btle_dev *dev = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200862 struct lowpan_peer *peer;
863 int err = -ENOENT;
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700864 bool last = false, remove = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200865
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300866 BT_DBG("chan %p conn %p", chan, chan->conn);
867
868 if (chan->conn && chan->conn->hcon) {
869 if (!is_bt_6lowpan(chan->conn->hcon))
870 return;
871
872 /* If conn is set, then the netdev is also there and we should
873 * not remove it.
874 */
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700875 remove = false;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300876 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200877
Jukka Rissanen90305822014-10-28 17:16:47 +0200878 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200879
Jukka Rissanen90305822014-10-28 17:16:47 +0200880 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200881 dev = lowpan_btle_dev(entry->netdev);
Jukka Rissanen90305822014-10-28 17:16:47 +0200882 peer = __peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200883 if (peer) {
884 last = peer_del(dev, peer);
885 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300886
887 BT_DBG("dev %p removing %speer %p", dev,
888 last ? "last " : "1 ", peer);
889 BT_DBG("chan %p orig refcnt %d", chan,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100890 kref_read(&chan->kref));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300891
892 l2cap_chan_put(chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200893 break;
894 }
895 }
896
897 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
Jukka Rissanen90305822014-10-28 17:16:47 +0200898 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200899
900 cancel_delayed_work_sync(&dev->notify_peers);
901
Jukka Rissanen7f118252014-06-18 16:37:11 +0300902 ifdown(dev->netdev);
903
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700904 if (remove) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300905 INIT_WORK(&entry->delete_netdev, delete_netdev);
906 schedule_work(&entry->delete_netdev);
907 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200908 } else {
Jukka Rissanen90305822014-10-28 17:16:47 +0200909 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200910 }
911
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300912 return;
913}
914
915static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
916{
917 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
918 state_to_string(state), err);
919}
920
921static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
922 unsigned long hdr_len,
923 unsigned long len, int nb)
924{
925 /* Note that we must allocate using GFP_ATOMIC here as
926 * this function is called originally from netdev hard xmit
927 * function in atomic context.
928 */
929 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
930}
931
932static void chan_suspend_cb(struct l2cap_chan *chan)
933{
934 struct sk_buff *skb = chan->data;
935
936 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
937
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300938 if (!skb)
939 return;
940
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300941 lowpan_cb(skb)->status = -EAGAIN;
942}
943
944static void chan_resume_cb(struct l2cap_chan *chan)
945{
946 struct sk_buff *skb = chan->data;
947
948 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
949
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300950 if (!skb)
951 return;
952
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300953 lowpan_cb(skb)->status = 0;
954}
955
956static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
957{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +0300958 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300959}
960
961static const struct l2cap_ops bt_6lowpan_chan_ops = {
962 .name = "L2CAP 6LoWPAN channel",
963 .new_connection = chan_new_conn_cb,
964 .recv = chan_recv_cb,
965 .close = chan_close_cb,
966 .state_change = chan_state_change_cb,
967 .ready = chan_ready_cb,
968 .resume = chan_resume_cb,
969 .suspend = chan_suspend_cb,
970 .get_sndtimeo = chan_get_sndtimeo_cb,
971 .alloc_skb = chan_alloc_skb_cb,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300972
973 .teardown = l2cap_chan_no_teardown,
974 .defer = l2cap_chan_no_defer,
975 .set_shutdown = l2cap_chan_no_set_shutdown,
976};
977
978static inline __u8 bdaddr_type(__u8 type)
979{
980 if (type == ADDR_LE_DEV_PUBLIC)
981 return BDADDR_LE_PUBLIC;
982 else
983 return BDADDR_LE_RANDOM;
984}
985
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300986static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
987{
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300988 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300989 int err;
990
Johan Hedberg26d46df2015-10-06 13:03:24 +0300991 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300992 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300993 return -EINVAL;
994
Johan Hedberg26d46df2015-10-06 13:03:24 +0300995 chan->ops = &bt_6lowpan_chan_ops;
996
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300997 err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300998 addr, dst_type);
999
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001000 BT_DBG("chan %p err %d", chan, err);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001001 if (err < 0)
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001002 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001003
Jukka Rissanen18722c22013-12-11 17:05:37 +02001004 return err;
1005}
1006
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001007static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
1008{
1009 struct lowpan_peer *peer;
1010
1011 BT_DBG("conn %p dst type %d", conn, dst_type);
1012
1013 peer = lookup_peer(conn);
1014 if (!peer)
1015 return -ENOENT;
1016
1017 BT_DBG("peer %p chan %p", peer, peer->chan);
1018
1019 l2cap_chan_close(peer->chan, ENOENT);
1020
1021 return 0;
1022}
1023
1024static struct l2cap_chan *bt_6lowpan_listen(void)
1025{
1026 bdaddr_t *addr = BDADDR_ANY;
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001027 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001028 int err;
1029
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001030 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001031 return NULL;
1032
Johan Hedberg26d46df2015-10-06 13:03:24 +03001033 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001034 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001035 return NULL;
1036
Johan Hedberg26d46df2015-10-06 13:03:24 +03001037 chan->ops = &bt_6lowpan_chan_ops;
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001038 chan->state = BT_LISTEN;
1039 chan->src_type = BDADDR_LE_PUBLIC;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001040
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001041 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
Johan Hedberg2773b022014-11-13 09:46:05 +02001042
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001043 BT_DBG("chan %p src type %d", chan, chan->src_type);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001044
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001045 err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001046 if (err) {
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001047 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001048 BT_ERR("psm cannot be added err %d", err);
1049 return NULL;
1050 }
1051
Johan Hedberg0cd088f2015-10-06 13:03:23 +03001052 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001053}
1054
1055static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1056 struct l2cap_conn **conn)
1057{
1058 struct hci_conn *hcon;
1059 struct hci_dev *hdev;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001060 int n;
1061
1062 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1063 &addr->b[5], &addr->b[4], &addr->b[3],
1064 &addr->b[2], &addr->b[1], &addr->b[0],
1065 addr_type);
1066
1067 if (n < 7)
1068 return -EINVAL;
1069
Johan Hedberg39385cb2016-11-12 17:03:07 +02001070 /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */
1071 hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001072 if (!hdev)
1073 return -ENOENT;
1074
1075 hci_dev_lock(hdev);
Johan Hedbergf5ad4ff2015-10-21 18:03:02 +03001076 hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001077 hci_dev_unlock(hdev);
1078
1079 if (!hcon)
1080 return -ENOENT;
1081
1082 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1083
1084 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1085
1086 return 0;
1087}
1088
1089static void disconnect_all_peers(void)
1090{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001091 struct lowpan_btle_dev *entry;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001092 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1093 struct list_head peers;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001094
1095 INIT_LIST_HEAD(&peers);
1096
1097 /* We make a separate list of peers as the close_cb() will
1098 * modify the device peers list so it is better not to mess
1099 * with the same list at the same time.
1100 */
1101
Jukka Rissanen90305822014-10-28 17:16:47 +02001102 rcu_read_lock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001103
Jukka Rissanen90305822014-10-28 17:16:47 +02001104 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1105 list_for_each_entry_rcu(peer, &entry->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001106 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1107 if (!new_peer)
1108 break;
1109
1110 new_peer->chan = peer->chan;
1111 INIT_LIST_HEAD(&new_peer->list);
1112
1113 list_add(&new_peer->list, &peers);
1114 }
1115 }
1116
Jukka Rissanen90305822014-10-28 17:16:47 +02001117 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001118
Jukka Rissanen90305822014-10-28 17:16:47 +02001119 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001120 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1121 l2cap_chan_close(peer->chan, ENOENT);
Jukka Rissanen90305822014-10-28 17:16:47 +02001122
1123 list_del_rcu(&peer->list);
Johan Hedberg4e790222014-11-11 14:16:29 +02001124 kfree_rcu(peer, rcu);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001125 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001126 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001127}
1128
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001129struct set_enable {
Jukka Rissanen90305822014-10-28 17:16:47 +02001130 struct work_struct work;
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001131 bool flag;
Jukka Rissanen90305822014-10-28 17:16:47 +02001132};
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001133
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001134static void do_enable_set(struct work_struct *work)
Jukka Rissanen90305822014-10-28 17:16:47 +02001135{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001136 struct set_enable *set_enable = container_of(work,
1137 struct set_enable, work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001138
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001139 if (!set_enable->flag || enable_6lowpan != set_enable->flag)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001140 /* Disconnect existing connections if 6lowpan is
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001141 * disabled
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001142 */
1143 disconnect_all_peers();
1144
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001145 enable_6lowpan = set_enable->flag;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001146
1147 if (listen_chan) {
1148 l2cap_chan_close(listen_chan, 0);
1149 l2cap_chan_put(listen_chan);
1150 }
1151
1152 listen_chan = bt_6lowpan_listen();
1153
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001154 kfree(set_enable);
Jukka Rissanen90305822014-10-28 17:16:47 +02001155}
1156
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001157static int lowpan_enable_set(void *data, u64 val)
Jukka Rissanen90305822014-10-28 17:16:47 +02001158{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001159 struct set_enable *set_enable;
Jukka Rissanen90305822014-10-28 17:16:47 +02001160
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001161 set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
1162 if (!set_enable)
Jukka Rissanen90305822014-10-28 17:16:47 +02001163 return -ENOMEM;
1164
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001165 set_enable->flag = !!val;
1166 INIT_WORK(&set_enable->work, do_enable_set);
Jukka Rissanen90305822014-10-28 17:16:47 +02001167
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001168 schedule_work(&set_enable->work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001169
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001170 return 0;
1171}
1172
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001173static int lowpan_enable_get(void *data, u64 *val)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001174{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001175 *val = enable_6lowpan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001176 return 0;
1177}
1178
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001179DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
1180 lowpan_enable_set, "%llu\n");
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001181
1182static ssize_t lowpan_control_write(struct file *fp,
1183 const char __user *user_buffer,
1184 size_t count,
1185 loff_t *position)
1186{
1187 char buf[32];
1188 size_t buf_size = min(count, sizeof(buf) - 1);
1189 int ret;
1190 bdaddr_t addr;
1191 u8 addr_type;
1192 struct l2cap_conn *conn = NULL;
1193
1194 if (copy_from_user(buf, user_buffer, buf_size))
1195 return -EFAULT;
1196
1197 buf[buf_size] = '\0';
1198
1199 if (memcmp(buf, "connect ", 8) == 0) {
1200 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1201 if (ret == -EINVAL)
1202 return ret;
1203
1204 if (listen_chan) {
1205 l2cap_chan_close(listen_chan, 0);
1206 l2cap_chan_put(listen_chan);
1207 listen_chan = NULL;
1208 }
1209
1210 if (conn) {
1211 struct lowpan_peer *peer;
1212
1213 if (!is_bt_6lowpan(conn->hcon))
1214 return -EINVAL;
1215
1216 peer = lookup_peer(conn);
1217 if (peer) {
1218 BT_DBG("6LoWPAN connection already exists");
1219 return -EALREADY;
1220 }
1221
1222 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1223 &conn->hcon->dst, conn->hcon->dst_type,
1224 addr_type);
1225 }
1226
1227 ret = bt_6lowpan_connect(&addr, addr_type);
1228 if (ret < 0)
1229 return ret;
1230
1231 return count;
1232 }
1233
1234 if (memcmp(buf, "disconnect ", 11) == 0) {
1235 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1236 if (ret < 0)
1237 return ret;
1238
1239 ret = bt_6lowpan_disconnect(conn, addr_type);
1240 if (ret < 0)
1241 return ret;
1242
1243 return count;
1244 }
1245
1246 return count;
1247}
1248
1249static int lowpan_control_show(struct seq_file *f, void *ptr)
1250{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001251 struct lowpan_btle_dev *entry;
Jukka Rissanen90305822014-10-28 17:16:47 +02001252 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001253
Jukka Rissanen90305822014-10-28 17:16:47 +02001254 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001255
Jukka Rissanen90305822014-10-28 17:16:47 +02001256 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1257 list_for_each_entry(peer, &entry->peers, list)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001258 seq_printf(f, "%pMR (type %u)\n",
1259 &peer->chan->dst, peer->chan->dst_type);
1260 }
1261
Jukka Rissanen90305822014-10-28 17:16:47 +02001262 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001263
1264 return 0;
1265}
1266
1267static int lowpan_control_open(struct inode *inode, struct file *file)
1268{
1269 return single_open(file, lowpan_control_show, inode->i_private);
1270}
1271
1272static const struct file_operations lowpan_control_fops = {
1273 .open = lowpan_control_open,
1274 .read = seq_read,
1275 .write = lowpan_control_write,
1276 .llseek = seq_lseek,
1277 .release = single_release,
1278};
1279
Jukka Rissanen7f118252014-06-18 16:37:11 +03001280static void disconnect_devices(void)
1281{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001282 struct lowpan_btle_dev *entry, *tmp, *new_dev;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001283 struct list_head devices;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001284
1285 INIT_LIST_HEAD(&devices);
1286
1287 /* We make a separate list of devices because the unregister_netdev()
1288 * will call device_event() which will also want to modify the same
1289 * devices list.
1290 */
1291
Jukka Rissanen90305822014-10-28 17:16:47 +02001292 rcu_read_lock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001293
Jukka Rissanen90305822014-10-28 17:16:47 +02001294 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001295 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1296 if (!new_dev)
1297 break;
1298
1299 new_dev->netdev = entry->netdev;
1300 INIT_LIST_HEAD(&new_dev->list);
1301
Jukka Rissanen90305822014-10-28 17:16:47 +02001302 list_add_rcu(&new_dev->list, &devices);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001303 }
1304
Jukka Rissanen90305822014-10-28 17:16:47 +02001305 rcu_read_unlock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001306
Dan Carpenterdaac1972014-10-29 19:10:57 +03001307 list_for_each_entry_safe(entry, tmp, &devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001308 ifdown(entry->netdev);
1309 BT_DBG("Unregistering netdev %s %p",
1310 entry->netdev->name, entry->netdev);
Alexander Aring00f59312015-12-09 22:46:29 +01001311 lowpan_unregister_netdev(entry->netdev);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001312 kfree(entry);
1313 }
1314}
1315
Jukka Rissanen18722c22013-12-11 17:05:37 +02001316static int device_event(struct notifier_block *unused,
1317 unsigned long event, void *ptr)
1318{
1319 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001320 struct lowpan_btle_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +02001321
1322 if (netdev->type != ARPHRD_6LOWPAN)
1323 return NOTIFY_DONE;
1324
1325 switch (event) {
1326 case NETDEV_UNREGISTER:
Jukka Rissanen90305822014-10-28 17:16:47 +02001327 spin_lock(&devices_lock);
1328 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +02001329 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001330 BT_DBG("Unregistered netdev %s %p",
1331 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001332 list_del(&entry->list);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001333 break;
1334 }
1335 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001336 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001337 break;
1338 }
1339
1340 return NOTIFY_DONE;
1341}
1342
1343static struct notifier_block bt_6lowpan_dev_notifier = {
1344 .notifier_call = device_event,
1345};
1346
Jukka Rissanen5547e482014-06-18 16:37:09 +03001347static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001348{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001349 lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644,
1350 bt_debugfs, NULL,
1351 &lowpan_enable_fops);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001352 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1353 bt_debugfs, NULL,
1354 &lowpan_control_fops);
1355
Jukka Rissanen18722c22013-12-11 17:05:37 +02001356 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1357}
1358
Jukka Rissanen5547e482014-06-18 16:37:09 +03001359static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001360{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001361 debugfs_remove(lowpan_enable_debugfs);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001362 debugfs_remove(lowpan_control_debugfs);
1363
1364 if (listen_chan) {
1365 l2cap_chan_close(listen_chan, 0);
1366 l2cap_chan_put(listen_chan);
1367 }
1368
Jukka Rissanen7f118252014-06-18 16:37:11 +03001369 disconnect_devices();
1370
Jukka Rissanen18722c22013-12-11 17:05:37 +02001371 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1372}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001373
1374module_init(bt_6lowpan_init);
1375module_exit(bt_6lowpan_exit);
1376
1377MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1378MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1379MODULE_VERSION(VERSION);
1380MODULE_LICENSE("GPL");