blob: 24348c8579dd7247586fd0782ef803bc0e972f62 [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 Townsend11e3ff72014-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 Rissanen36b3dd22014-09-29 16:37:25 +0300401static int setup_header(struct sk_buff *skb, struct net_device *netdev,
402 bdaddr_t *peer_addr, u8 *peer_addr_type)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200403{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300404 struct in6_addr ipv6_daddr;
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200405 struct ipv6hdr *hdr;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200406 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200407 struct lowpan_peer *peer;
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200408 u8 *daddr;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300409 int err, status = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200410
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200411 hdr = ipv6_hdr(skb);
412
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200413 dev = lowpan_btle_dev(netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200414
Glenn Ruben Bakke55441072016-04-22 18:06:11 +0200415 memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr));
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300416
417 if (ipv6_addr_is_multicast(&ipv6_daddr)) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300418 lowpan_cb(skb)->chan = NULL;
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200419 daddr = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200420 } else {
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200421 BT_DBG("dest IP %pI6c", &ipv6_daddr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200422
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200423 /* The packet might be sent to 6lowpan interface
424 * because of routing (either via default route
425 * or user set route) so get peer according to
426 * the destination address.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200427 */
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200428 peer = peer_lookup_dst(dev, &ipv6_daddr, skb);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200429 if (!peer) {
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200430 BT_DBG("no such peer");
431 return -ENOENT;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200432 }
433
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200434 daddr = peer->lladdr;
Luiz Augusto von Dentz9dae2e02017-03-12 10:19:38 +0200435 peer_addr = &peer->chan->dst;
436 *peer_addr_type = peer->chan->dst_type;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300437 lowpan_cb(skb)->chan = peer->chan;
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300438
439 status = 1;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200440 }
441
Alexander Aringa6f77382015-10-13 13:42:57 +0200442 lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200443
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300444 err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0);
445 if (err < 0)
446 return err;
447
448 return status;
449}
450
451static int header_create(struct sk_buff *skb, struct net_device *netdev,
452 unsigned short type, const void *_daddr,
453 const void *_saddr, unsigned int len)
454{
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300455 if (type != ETH_P_IPV6)
456 return -EINVAL;
457
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300458 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200459}
460
461/* Packet to BT LE device */
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300462static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb,
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300463 struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200464{
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300465 struct msghdr msg;
466 struct kvec iv;
467 int err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200468
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300469 /* Remember the skb so that we can send EAGAIN to the caller if
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300470 * we run out of credits.
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300471 */
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300472 chan->data = skb;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300473
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300474 iv.iov_base = skb->data;
475 iv.iov_len = skb->len;
476
Al Viroc0371da2014-11-24 10:42:55 -0500477 memset(&msg, 0, sizeof(msg));
Al Viro17836392014-11-24 17:07:38 -0500478 iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len);
Al Viroc0371da2014-11-24 10:42:55 -0500479
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300480 err = l2cap_chan_send(chan, &msg, skb->len);
481 if (err > 0) {
482 netdev->stats.tx_bytes += err;
483 netdev->stats.tx_packets++;
484 return 0;
485 }
486
487 if (!err)
488 err = lowpan_cb(skb)->status;
489
490 if (err < 0) {
491 if (err == -EAGAIN)
492 netdev->stats.tx_dropped++;
493 else
494 netdev->stats.tx_errors++;
495 }
496
497 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200498}
499
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300500static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200501{
502 struct sk_buff *local_skb;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200503 struct lowpan_btle_dev *entry;
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300504 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200505
Jukka Rissanen90305822014-10-28 17:16:47 +0200506 rcu_read_lock();
Jukka Rissanen18722c22013-12-11 17:05:37 +0200507
Jukka Rissanen90305822014-10-28 17:16:47 +0200508 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
509 struct lowpan_peer *pentry;
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200510 struct lowpan_btle_dev *dev;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200511
512 if (entry->netdev != netdev)
513 continue;
514
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200515 dev = lowpan_btle_dev(entry->netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200516
Jukka Rissanen90305822014-10-28 17:16:47 +0200517 list_for_each_entry_rcu(pentry, &dev->peers, list) {
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300518 int ret;
519
Jukka Rissanen18722c22013-12-11 17:05:37 +0200520 local_skb = skb_clone(skb, GFP_ATOMIC);
521
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300522 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
523 netdev->name,
524 &pentry->chan->dst, pentry->chan->dst_type,
525 &pentry->peer_addr, pentry->chan);
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300526 ret = send_pkt(pentry->chan, local_skb, netdev);
527 if (ret < 0)
528 err = ret;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200529
530 kfree_skb(local_skb);
531 }
532 }
533
Jukka Rissanen90305822014-10-28 17:16:47 +0200534 rcu_read_unlock();
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300535
536 return err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200537}
538
539static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev)
540{
541 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200542 bdaddr_t addr;
543 u8 addr_type;
544
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300545 /* We must take a copy of the skb before we modify/replace the ipv6
546 * header as the header could be used elsewhere
547 */
Alexander Aringb0c42cd2014-10-08 10:24:53 +0200548 skb = skb_unshare(skb, GFP_ATOMIC);
549 if (!skb)
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300550 return NET_XMIT_DROP;
551
552 /* Return values from setup_header()
553 * <0 - error, packet is dropped
554 * 0 - this is a multicast packet
555 * 1 - this is unicast packet
556 */
557 err = setup_header(skb, netdev, &addr, &addr_type);
558 if (err < 0) {
559 kfree_skb(skb);
560 return NET_XMIT_DROP;
561 }
562
563 if (err) {
564 if (lowpan_cb(skb)->chan) {
565 BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p",
566 netdev->name, &addr, addr_type,
567 &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan);
Jukka Rissanend7b6b0a2014-10-01 15:59:14 +0300568 err = send_pkt(lowpan_cb(skb)->chan, skb, netdev);
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300569 } else {
570 err = -ENOENT;
571 }
572 } else {
573 /* We need to send the packet to every device behind this
574 * interface.
Jukka Rissanen18722c22013-12-11 17:05:37 +0200575 */
Jukka Rissanen9c238ca2014-10-01 15:59:15 +0300576 err = send_mcast_pkt(skb, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200577 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200578
Jukka Rissanenfc125182014-10-01 11:30:26 +0300579 dev_kfree_skb(skb);
580
Jukka Rissanen18722c22013-12-11 17:05:37 +0200581 if (err)
582 BT_DBG("ERROR: xmit failed (%d)", err);
583
Jukka Rissanen36b3dd22014-09-29 16:37:25 +0300584 return err < 0 ? NET_XMIT_DROP : err;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200585}
586
Jukka Rissanendf092302014-10-28 17:16:48 +0200587static int bt_dev_init(struct net_device *dev)
588{
Eric Dumazetd3fff6c2016-06-09 07:45:12 -0700589 netdev_lockdep_set_classes(dev);
Jukka Rissanendf092302014-10-28 17:16:48 +0200590
591 return 0;
592}
593
Jukka Rissanen18722c22013-12-11 17:05:37 +0200594static const struct net_device_ops netdev_ops = {
Jukka Rissanendf092302014-10-28 17:16:48 +0200595 .ndo_init = bt_dev_init,
Jukka Rissanen18722c22013-12-11 17:05:37 +0200596 .ndo_start_xmit = bt_xmit,
597};
598
599static struct header_ops header_ops = {
600 .create = header_create,
601};
602
603static void netdev_setup(struct net_device *dev)
604{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200605 dev->hard_header_len = 0;
606 dev->needed_tailroom = 0;
Jukka Rissanen156395c2014-09-29 16:37:26 +0300607 dev->flags = IFF_RUNNING | IFF_POINTOPOINT |
608 IFF_MULTICAST;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200609 dev->watchdog_timeo = 0;
610
611 dev->netdev_ops = &netdev_ops;
612 dev->header_ops = &header_ops;
613 dev->destructor = free_netdev;
614}
615
616static struct device_type bt_type = {
617 .name = "bluetooth",
618};
619
Jukka Rissanen18722c22013-12-11 17:05:37 +0200620static void ifup(struct net_device *netdev)
621{
622 int err;
623
624 rtnl_lock();
625 err = dev_open(netdev);
626 if (err < 0)
627 BT_INFO("iface %s cannot be opened (%d)", netdev->name, err);
628 rtnl_unlock();
629}
630
Jukka Rissanen7f118252014-06-18 16:37:11 +0300631static void ifdown(struct net_device *netdev)
632{
633 int err;
634
635 rtnl_lock();
636 err = dev_close(netdev);
637 if (err < 0)
638 BT_INFO("iface %s cannot be closed (%d)", netdev->name, err);
639 rtnl_unlock();
640}
641
Jukka Rissanen18722c22013-12-11 17:05:37 +0200642static void do_notify_peers(struct work_struct *work)
643{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200644 struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev,
645 notify_peers.work);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200646
647 netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */
648}
649
650static bool is_bt_6lowpan(struct hci_conn *hcon)
651{
652 if (hcon->type != LE_LINK)
653 return false;
654
Jukka Rissanen7b2ed602015-01-08 17:00:55 +0200655 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300656 return false;
657
658 return true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200659}
660
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300661static struct l2cap_chan *chan_create(void)
662{
663 struct l2cap_chan *chan;
664
665 chan = l2cap_chan_create();
666 if (!chan)
667 return NULL;
668
669 l2cap_chan_set_defaults(chan);
670
671 chan->chan_type = L2CAP_CHAN_CONN_ORIENTED;
672 chan->mode = L2CAP_MODE_LE_FLOWCTL;
Johan Hedberg301de2c2015-10-06 13:03:19 +0300673 chan->imtu = 1280;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300674
675 return chan;
676}
677
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300678static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan,
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200679 struct lowpan_btle_dev *dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200680{
681 struct lowpan_peer *peer;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200682
683 peer = kzalloc(sizeof(*peer), GFP_ATOMIC);
684 if (!peer)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300685 return NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200686
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300687 peer->chan = chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200688 memset(&peer->peer_addr, 0, sizeof(struct in6_addr));
689
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200690 baswap((void *)peer->lladdr, &chan->dst);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200691
Luiz Augusto von Dentzfa09ae62017-03-12 10:19:37 +0200692 lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200693
Jukka Rissanen90305822014-10-28 17:16:47 +0200694 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200695 INIT_LIST_HEAD(&peer->list);
696 peer_add(dev, peer);
Jukka Rissanen90305822014-10-28 17:16:47 +0200697 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200698
699 /* Notifying peers about us needs to be done without locks held */
700 INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers);
701 schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100));
702
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300703 return peer->chan;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200704}
705
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200706static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200707{
Jukka Rissanen18722c22013-12-11 17:05:37 +0200708 struct net_device *netdev;
709 int err = 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200710
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200711 netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)),
Alexander Aringb72f6f512015-08-11 21:44:08 +0200712 IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN,
713 netdev_setup);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200714 if (!netdev)
715 return -ENOMEM;
716
Patrik Flyktc259d142017-03-12 10:19:33 +0200717 netdev->addr_assign_type = NET_ADDR_PERM;
718 baswap((void *)netdev->dev_addr, &chan->src);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200719
720 netdev->netdev_ops = &netdev_ops;
Glenn Ruben Bakkefc842422015-06-17 07:32:25 -0700721 SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200722 SET_NETDEV_DEVTYPE(netdev, &bt_type);
723
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200724 *dev = lowpan_btle_dev(netdev);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300725 (*dev)->netdev = netdev;
726 (*dev)->hdev = chan->conn->hcon->hdev;
727 INIT_LIST_HEAD(&(*dev)->peers);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200728
Jukka Rissanen90305822014-10-28 17:16:47 +0200729 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300730 INIT_LIST_HEAD(&(*dev)->list);
Jukka Rissanen90305822014-10-28 17:16:47 +0200731 list_add_rcu(&(*dev)->list, &bt_6lowpan_devices);
732 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200733
Alexander Aring00f59312015-12-09 22:46:29 +0100734 err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE);
Alexander Aring5857d1d2015-07-30 09:40:53 +0200735 if (err < 0) {
736 BT_INFO("register_netdev failed %d", err);
737 spin_lock(&devices_lock);
738 list_del_rcu(&(*dev)->list);
739 spin_unlock(&devices_lock);
740 free_netdev(netdev);
741 goto out;
742 }
743
744 BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d",
745 netdev->ifindex, &chan->dst, chan->dst_type,
746 &chan->src, chan->src_type);
747 set_bit(__LINK_STATE_PRESENT, &netdev->state);
748
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300749 return 0;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200750
751out:
752 return err;
753}
754
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300755static inline void chan_ready_cb(struct l2cap_chan *chan)
756{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200757 struct lowpan_btle_dev *dev;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300758
759 dev = lookup_dev(chan->conn);
760
761 BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev);
762
763 if (!dev) {
764 if (setup_netdev(chan, &dev) < 0) {
765 l2cap_chan_del(chan, -ENOENT);
766 return;
767 }
768 }
769
Jukka Rissanen18d93c12014-06-18 16:37:10 +0300770 if (!try_module_get(THIS_MODULE))
771 return;
772
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300773 add_peer_chan(chan, dev);
774 ifup(dev->netdev);
775}
776
Johan Hedberg2b293492014-08-07 10:03:32 +0300777static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300778{
Johan Hedberg2b293492014-08-07 10:03:32 +0300779 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300780
Johan Hedberg630ef792015-10-06 13:03:22 +0300781 chan = chan_create();
782 if (!chan)
783 return NULL;
784
Johan Hedberg2b293492014-08-07 10:03:32 +0300785 chan->ops = pchan->ops;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300786
787 BT_DBG("chan %p pchan %p", chan, pchan);
788
Johan Hedberg2b293492014-08-07 10:03:32 +0300789 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300790}
791
Jukka Rissanen18722c22013-12-11 17:05:37 +0200792static void delete_netdev(struct work_struct *work)
793{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200794 struct lowpan_btle_dev *entry = container_of(work,
795 struct lowpan_btle_dev,
796 delete_netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200797
Alexander Aring00f59312015-12-09 22:46:29 +0100798 lowpan_unregister_netdev(entry->netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200799
Glenn Ruben Bakke2ad88fb2015-06-17 07:32:26 -0700800 /* The entry pointer is deleted by the netdev destructor. */
Jukka Rissanen18722c22013-12-11 17:05:37 +0200801}
802
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300803static void chan_close_cb(struct l2cap_chan *chan)
Jukka Rissanen18722c22013-12-11 17:05:37 +0200804{
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200805 struct lowpan_btle_dev *entry;
806 struct lowpan_btle_dev *dev = NULL;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200807 struct lowpan_peer *peer;
808 int err = -ENOENT;
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700809 bool last = false, remove = true;
Jukka Rissanen18722c22013-12-11 17:05:37 +0200810
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300811 BT_DBG("chan %p conn %p", chan, chan->conn);
812
813 if (chan->conn && chan->conn->hcon) {
814 if (!is_bt_6lowpan(chan->conn->hcon))
815 return;
816
817 /* If conn is set, then the netdev is also there and we should
818 * not remove it.
819 */
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700820 remove = false;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300821 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200822
Jukka Rissanen90305822014-10-28 17:16:47 +0200823 spin_lock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200824
Jukka Rissanen90305822014-10-28 17:16:47 +0200825 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Alexander Aring2e4d60c2016-04-11 11:04:18 +0200826 dev = lowpan_btle_dev(entry->netdev);
Jukka Rissanen90305822014-10-28 17:16:47 +0200827 peer = __peer_lookup_chan(dev, chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200828 if (peer) {
829 last = peer_del(dev, peer);
830 err = 0;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300831
832 BT_DBG("dev %p removing %speer %p", dev,
833 last ? "last " : "1 ", peer);
834 BT_DBG("chan %p orig refcnt %d", chan,
Peter Zijlstra2c935bc2016-11-14 17:29:48 +0100835 kref_read(&chan->kref));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300836
837 l2cap_chan_put(chan);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200838 break;
839 }
840 }
841
842 if (!err && last && dev && !atomic_read(&dev->peer_count)) {
Jukka Rissanen90305822014-10-28 17:16:47 +0200843 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200844
845 cancel_delayed_work_sync(&dev->notify_peers);
846
Jukka Rissanen7f118252014-06-18 16:37:11 +0300847 ifdown(dev->netdev);
848
Glenn Ruben Bakkef63666d22015-06-17 07:32:24 -0700849 if (remove) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300850 INIT_WORK(&entry->delete_netdev, delete_netdev);
851 schedule_work(&entry->delete_netdev);
852 }
Jukka Rissanen18722c22013-12-11 17:05:37 +0200853 } else {
Jukka Rissanen90305822014-10-28 17:16:47 +0200854 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +0200855 }
856
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300857 return;
858}
859
860static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err)
861{
862 BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn,
863 state_to_string(state), err);
864}
865
866static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan,
867 unsigned long hdr_len,
868 unsigned long len, int nb)
869{
870 /* Note that we must allocate using GFP_ATOMIC here as
871 * this function is called originally from netdev hard xmit
872 * function in atomic context.
873 */
874 return bt_skb_alloc(hdr_len + len, GFP_ATOMIC);
875}
876
877static void chan_suspend_cb(struct l2cap_chan *chan)
878{
879 struct sk_buff *skb = chan->data;
880
881 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
882
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300883 if (!skb)
884 return;
885
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300886 lowpan_cb(skb)->status = -EAGAIN;
887}
888
889static void chan_resume_cb(struct l2cap_chan *chan)
890{
891 struct sk_buff *skb = chan->data;
892
893 BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb);
894
Jukka Rissanen59790aa2014-09-29 10:55:46 +0300895 if (!skb)
896 return;
897
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300898 lowpan_cb(skb)->status = 0;
899}
900
901static long chan_get_sndtimeo_cb(struct l2cap_chan *chan)
902{
Jukka Rissanen2ae50d82014-09-08 12:11:43 +0300903 return L2CAP_CONN_TIMEOUT;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300904}
905
906static const struct l2cap_ops bt_6lowpan_chan_ops = {
907 .name = "L2CAP 6LoWPAN channel",
908 .new_connection = chan_new_conn_cb,
909 .recv = chan_recv_cb,
910 .close = chan_close_cb,
911 .state_change = chan_state_change_cb,
912 .ready = chan_ready_cb,
913 .resume = chan_resume_cb,
914 .suspend = chan_suspend_cb,
915 .get_sndtimeo = chan_get_sndtimeo_cb,
916 .alloc_skb = chan_alloc_skb_cb,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300917
918 .teardown = l2cap_chan_no_teardown,
919 .defer = l2cap_chan_no_defer,
920 .set_shutdown = l2cap_chan_no_set_shutdown,
921};
922
923static inline __u8 bdaddr_type(__u8 type)
924{
925 if (type == ADDR_LE_DEV_PUBLIC)
926 return BDADDR_LE_PUBLIC;
927 else
928 return BDADDR_LE_RANDOM;
929}
930
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300931static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type)
932{
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300933 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300934 int err;
935
Johan Hedberg26d46df2015-10-06 13:03:24 +0300936 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300937 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300938 return -EINVAL;
939
Johan Hedberg26d46df2015-10-06 13:03:24 +0300940 chan->ops = &bt_6lowpan_chan_ops;
941
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300942 err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0,
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300943 addr, dst_type);
944
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300945 BT_DBG("chan %p err %d", chan, err);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300946 if (err < 0)
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300947 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300948
Jukka Rissanen18722c22013-12-11 17:05:37 +0200949 return err;
950}
951
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300952static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type)
953{
954 struct lowpan_peer *peer;
955
956 BT_DBG("conn %p dst type %d", conn, dst_type);
957
958 peer = lookup_peer(conn);
959 if (!peer)
960 return -ENOENT;
961
962 BT_DBG("peer %p chan %p", peer, peer->chan);
963
964 l2cap_chan_close(peer->chan, ENOENT);
965
966 return 0;
967}
968
969static struct l2cap_chan *bt_6lowpan_listen(void)
970{
971 bdaddr_t *addr = BDADDR_ANY;
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300972 struct l2cap_chan *chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300973 int err;
974
Jukka Rissanen7b2ed602015-01-08 17:00:55 +0200975 if (!enable_6lowpan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300976 return NULL;
977
Johan Hedberg26d46df2015-10-06 13:03:24 +0300978 chan = chan_create();
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300979 if (!chan)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300980 return NULL;
981
Johan Hedberg26d46df2015-10-06 13:03:24 +0300982 chan->ops = &bt_6lowpan_chan_ops;
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300983 chan->state = BT_LISTEN;
984 chan->src_type = BDADDR_LE_PUBLIC;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300985
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300986 atomic_set(&chan->nesting, L2CAP_NESTING_PARENT);
Johan Hedberg2773b022014-11-13 09:46:05 +0200987
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300988 BT_DBG("chan %p src type %d", chan, chan->src_type);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300989
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300990 err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP));
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300991 if (err) {
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300992 l2cap_chan_put(chan);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300993 BT_ERR("psm cannot be added err %d", err);
994 return NULL;
995 }
996
Johan Hedberg0cd088f2015-10-06 13:03:23 +0300997 return chan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +0300998}
999
1000static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type,
1001 struct l2cap_conn **conn)
1002{
1003 struct hci_conn *hcon;
1004 struct hci_dev *hdev;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001005 int n;
1006
1007 n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu",
1008 &addr->b[5], &addr->b[4], &addr->b[3],
1009 &addr->b[2], &addr->b[1], &addr->b[0],
1010 addr_type);
1011
1012 if (n < 7)
1013 return -EINVAL;
1014
Johan Hedberg39385cb2016-11-12 17:03:07 +02001015 /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */
1016 hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001017 if (!hdev)
1018 return -ENOENT;
1019
1020 hci_dev_lock(hdev);
Johan Hedbergf5ad4ff2015-10-21 18:03:02 +03001021 hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001022 hci_dev_unlock(hdev);
1023
1024 if (!hcon)
1025 return -ENOENT;
1026
1027 *conn = (struct l2cap_conn *)hcon->l2cap_data;
1028
1029 BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type);
1030
1031 return 0;
1032}
1033
1034static void disconnect_all_peers(void)
1035{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001036 struct lowpan_btle_dev *entry;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001037 struct lowpan_peer *peer, *tmp_peer, *new_peer;
1038 struct list_head peers;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001039
1040 INIT_LIST_HEAD(&peers);
1041
1042 /* We make a separate list of peers as the close_cb() will
1043 * modify the device peers list so it is better not to mess
1044 * with the same list at the same time.
1045 */
1046
Jukka Rissanen90305822014-10-28 17:16:47 +02001047 rcu_read_lock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001048
Jukka Rissanen90305822014-10-28 17:16:47 +02001049 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
1050 list_for_each_entry_rcu(peer, &entry->peers, list) {
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001051 new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC);
1052 if (!new_peer)
1053 break;
1054
1055 new_peer->chan = peer->chan;
1056 INIT_LIST_HEAD(&new_peer->list);
1057
1058 list_add(&new_peer->list, &peers);
1059 }
1060 }
1061
Jukka Rissanen90305822014-10-28 17:16:47 +02001062 rcu_read_unlock();
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001063
Jukka Rissanen90305822014-10-28 17:16:47 +02001064 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001065 list_for_each_entry_safe(peer, tmp_peer, &peers, list) {
1066 l2cap_chan_close(peer->chan, ENOENT);
Jukka Rissanen90305822014-10-28 17:16:47 +02001067
1068 list_del_rcu(&peer->list);
Johan Hedberg4e790222014-11-11 14:16:29 +02001069 kfree_rcu(peer, rcu);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001070 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001071 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001072}
1073
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001074struct set_enable {
Jukka Rissanen90305822014-10-28 17:16:47 +02001075 struct work_struct work;
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001076 bool flag;
Jukka Rissanen90305822014-10-28 17:16:47 +02001077};
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001078
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001079static void do_enable_set(struct work_struct *work)
Jukka Rissanen90305822014-10-28 17:16:47 +02001080{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001081 struct set_enable *set_enable = container_of(work,
1082 struct set_enable, work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001083
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001084 if (!set_enable->flag || enable_6lowpan != set_enable->flag)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001085 /* Disconnect existing connections if 6lowpan is
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001086 * disabled
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001087 */
1088 disconnect_all_peers();
1089
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001090 enable_6lowpan = set_enable->flag;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001091
1092 if (listen_chan) {
1093 l2cap_chan_close(listen_chan, 0);
1094 l2cap_chan_put(listen_chan);
1095 }
1096
1097 listen_chan = bt_6lowpan_listen();
1098
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001099 kfree(set_enable);
Jukka Rissanen90305822014-10-28 17:16:47 +02001100}
1101
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001102static int lowpan_enable_set(void *data, u64 val)
Jukka Rissanen90305822014-10-28 17:16:47 +02001103{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001104 struct set_enable *set_enable;
Jukka Rissanen90305822014-10-28 17:16:47 +02001105
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001106 set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL);
1107 if (!set_enable)
Jukka Rissanen90305822014-10-28 17:16:47 +02001108 return -ENOMEM;
1109
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001110 set_enable->flag = !!val;
1111 INIT_WORK(&set_enable->work, do_enable_set);
Jukka Rissanen90305822014-10-28 17:16:47 +02001112
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001113 schedule_work(&set_enable->work);
Jukka Rissanen90305822014-10-28 17:16:47 +02001114
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001115 return 0;
1116}
1117
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001118static int lowpan_enable_get(void *data, u64 *val)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001119{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001120 *val = enable_6lowpan;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001121 return 0;
1122}
1123
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001124DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get,
1125 lowpan_enable_set, "%llu\n");
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001126
1127static ssize_t lowpan_control_write(struct file *fp,
1128 const char __user *user_buffer,
1129 size_t count,
1130 loff_t *position)
1131{
1132 char buf[32];
1133 size_t buf_size = min(count, sizeof(buf) - 1);
1134 int ret;
1135 bdaddr_t addr;
1136 u8 addr_type;
1137 struct l2cap_conn *conn = NULL;
1138
1139 if (copy_from_user(buf, user_buffer, buf_size))
1140 return -EFAULT;
1141
1142 buf[buf_size] = '\0';
1143
1144 if (memcmp(buf, "connect ", 8) == 0) {
1145 ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn);
1146 if (ret == -EINVAL)
1147 return ret;
1148
1149 if (listen_chan) {
1150 l2cap_chan_close(listen_chan, 0);
1151 l2cap_chan_put(listen_chan);
1152 listen_chan = NULL;
1153 }
1154
1155 if (conn) {
1156 struct lowpan_peer *peer;
1157
1158 if (!is_bt_6lowpan(conn->hcon))
1159 return -EINVAL;
1160
1161 peer = lookup_peer(conn);
1162 if (peer) {
1163 BT_DBG("6LoWPAN connection already exists");
1164 return -EALREADY;
1165 }
1166
1167 BT_DBG("conn %p dst %pMR type %d user %d", conn,
1168 &conn->hcon->dst, conn->hcon->dst_type,
1169 addr_type);
1170 }
1171
1172 ret = bt_6lowpan_connect(&addr, addr_type);
1173 if (ret < 0)
1174 return ret;
1175
1176 return count;
1177 }
1178
1179 if (memcmp(buf, "disconnect ", 11) == 0) {
1180 ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn);
1181 if (ret < 0)
1182 return ret;
1183
1184 ret = bt_6lowpan_disconnect(conn, addr_type);
1185 if (ret < 0)
1186 return ret;
1187
1188 return count;
1189 }
1190
1191 return count;
1192}
1193
1194static int lowpan_control_show(struct seq_file *f, void *ptr)
1195{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001196 struct lowpan_btle_dev *entry;
Jukka Rissanen90305822014-10-28 17:16:47 +02001197 struct lowpan_peer *peer;
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001198
Jukka Rissanen90305822014-10-28 17:16:47 +02001199 spin_lock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001200
Jukka Rissanen90305822014-10-28 17:16:47 +02001201 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
1202 list_for_each_entry(peer, &entry->peers, list)
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001203 seq_printf(f, "%pMR (type %u)\n",
1204 &peer->chan->dst, peer->chan->dst_type);
1205 }
1206
Jukka Rissanen90305822014-10-28 17:16:47 +02001207 spin_unlock(&devices_lock);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001208
1209 return 0;
1210}
1211
1212static int lowpan_control_open(struct inode *inode, struct file *file)
1213{
1214 return single_open(file, lowpan_control_show, inode->i_private);
1215}
1216
1217static const struct file_operations lowpan_control_fops = {
1218 .open = lowpan_control_open,
1219 .read = seq_read,
1220 .write = lowpan_control_write,
1221 .llseek = seq_lseek,
1222 .release = single_release,
1223};
1224
Jukka Rissanen7f118252014-06-18 16:37:11 +03001225static void disconnect_devices(void)
1226{
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001227 struct lowpan_btle_dev *entry, *tmp, *new_dev;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001228 struct list_head devices;
Jukka Rissanen7f118252014-06-18 16:37:11 +03001229
1230 INIT_LIST_HEAD(&devices);
1231
1232 /* We make a separate list of devices because the unregister_netdev()
1233 * will call device_event() which will also want to modify the same
1234 * devices list.
1235 */
1236
Jukka Rissanen90305822014-10-28 17:16:47 +02001237 rcu_read_lock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001238
Jukka Rissanen90305822014-10-28 17:16:47 +02001239 list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001240 new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC);
1241 if (!new_dev)
1242 break;
1243
1244 new_dev->netdev = entry->netdev;
1245 INIT_LIST_HEAD(&new_dev->list);
1246
Jukka Rissanen90305822014-10-28 17:16:47 +02001247 list_add_rcu(&new_dev->list, &devices);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001248 }
1249
Jukka Rissanen90305822014-10-28 17:16:47 +02001250 rcu_read_unlock();
Jukka Rissanen7f118252014-06-18 16:37:11 +03001251
Dan Carpenterdaac1972014-10-29 19:10:57 +03001252 list_for_each_entry_safe(entry, tmp, &devices, list) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001253 ifdown(entry->netdev);
1254 BT_DBG("Unregistering netdev %s %p",
1255 entry->netdev->name, entry->netdev);
Alexander Aring00f59312015-12-09 22:46:29 +01001256 lowpan_unregister_netdev(entry->netdev);
Jukka Rissanen7f118252014-06-18 16:37:11 +03001257 kfree(entry);
1258 }
1259}
1260
Jukka Rissanen18722c22013-12-11 17:05:37 +02001261static int device_event(struct notifier_block *unused,
1262 unsigned long event, void *ptr)
1263{
1264 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Alexander Aring2e4d60c2016-04-11 11:04:18 +02001265 struct lowpan_btle_dev *entry;
Jukka Rissanen18722c22013-12-11 17:05:37 +02001266
1267 if (netdev->type != ARPHRD_6LOWPAN)
1268 return NOTIFY_DONE;
1269
1270 switch (event) {
1271 case NETDEV_UNREGISTER:
Jukka Rissanen90305822014-10-28 17:16:47 +02001272 spin_lock(&devices_lock);
1273 list_for_each_entry(entry, &bt_6lowpan_devices, list) {
Jukka Rissanen18722c22013-12-11 17:05:37 +02001274 if (entry->netdev == netdev) {
Jukka Rissanen7f118252014-06-18 16:37:11 +03001275 BT_DBG("Unregistered netdev %s %p",
1276 netdev->name, netdev);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001277 list_del(&entry->list);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001278 break;
1279 }
1280 }
Jukka Rissanen90305822014-10-28 17:16:47 +02001281 spin_unlock(&devices_lock);
Jukka Rissanen18722c22013-12-11 17:05:37 +02001282 break;
1283 }
1284
1285 return NOTIFY_DONE;
1286}
1287
1288static struct notifier_block bt_6lowpan_dev_notifier = {
1289 .notifier_call = device_event,
1290};
1291
Jukka Rissanen5547e482014-06-18 16:37:09 +03001292static int __init bt_6lowpan_init(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001293{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001294 lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644,
1295 bt_debugfs, NULL,
1296 &lowpan_enable_fops);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001297 lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644,
1298 bt_debugfs, NULL,
1299 &lowpan_control_fops);
1300
Jukka Rissanen18722c22013-12-11 17:05:37 +02001301 return register_netdevice_notifier(&bt_6lowpan_dev_notifier);
1302}
1303
Jukka Rissanen5547e482014-06-18 16:37:09 +03001304static void __exit bt_6lowpan_exit(void)
Jukka Rissanen18722c22013-12-11 17:05:37 +02001305{
Jukka Rissanen7b2ed602015-01-08 17:00:55 +02001306 debugfs_remove(lowpan_enable_debugfs);
Jukka Rissanen6b8d4a62014-06-18 16:37:08 +03001307 debugfs_remove(lowpan_control_debugfs);
1308
1309 if (listen_chan) {
1310 l2cap_chan_close(listen_chan, 0);
1311 l2cap_chan_put(listen_chan);
1312 }
1313
Jukka Rissanen7f118252014-06-18 16:37:11 +03001314 disconnect_devices();
1315
Jukka Rissanen18722c22013-12-11 17:05:37 +02001316 unregister_netdevice_notifier(&bt_6lowpan_dev_notifier);
1317}
Jukka Rissanen5547e482014-06-18 16:37:09 +03001318
1319module_init(bt_6lowpan_init);
1320module_exit(bt_6lowpan_exit);
1321
1322MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>");
1323MODULE_DESCRIPTION("Bluetooth 6LoWPAN");
1324MODULE_VERSION(VERSION);
1325MODULE_LICENSE("GPL");