Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1 | /* |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 2 | Copyright (c) 2013-2014 Intel Corp. |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 3 | |
| 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 Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 14 | #include <linux/if_arp.h> |
| 15 | #include <linux/netdevice.h> |
| 16 | #include <linux/etherdevice.h> |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 17 | #include <linux/module.h> |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 18 | #include <linux/debugfs.h> |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 19 | |
| 20 | #include <net/ipv6.h> |
| 21 | #include <net/ip6_route.h> |
| 22 | #include <net/addrconf.h> |
| 23 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 24 | #include <net/bluetooth/bluetooth.h> |
| 25 | #include <net/bluetooth/hci_core.h> |
| 26 | #include <net/bluetooth/l2cap.h> |
| 27 | |
Alexander Aring | cefc8c8 | 2014-03-05 14:29:05 +0100 | [diff] [blame] | 28 | #include <net/6lowpan.h> /* for the compression support */ |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 29 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 30 | #define VERSION "0.1" |
| 31 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 32 | static struct dentry *lowpan_enable_debugfs; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 33 | static struct dentry *lowpan_control_debugfs; |
| 34 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 35 | #define IFACE_NAME_TEMPLATE "bt%d" |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 36 | |
| 37 | struct skb_cb { |
| 38 | struct in6_addr addr; |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 39 | struct in6_addr gw; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 40 | struct l2cap_chan *chan; |
| 41 | int status; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 42 | }; |
| 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 | */ |
| 52 | static LIST_HEAD(bt_6lowpan_devices); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 53 | static DEFINE_SPINLOCK(devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 54 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 55 | static bool enable_6lowpan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 56 | |
| 57 | /* We are listening incoming connections via this channel |
| 58 | */ |
| 59 | static struct l2cap_chan *listen_chan; |
| 60 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 61 | struct lowpan_peer { |
| 62 | struct list_head list; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 63 | struct rcu_head rcu; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 64 | struct l2cap_chan *chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 65 | |
| 66 | /* peer addresses in various formats */ |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame] | 67 | unsigned char lladdr[ETH_ALEN]; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 68 | struct in6_addr peer_addr; |
| 69 | }; |
| 70 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 71 | struct lowpan_btle_dev { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 72 | 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 Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 83 | static inline struct lowpan_btle_dev * |
| 84 | lowpan_btle_dev(const struct net_device *netdev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 85 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 86 | return (struct lowpan_btle_dev *)lowpan_dev(netdev)->priv; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 89 | static inline void peer_add(struct lowpan_btle_dev *dev, |
| 90 | struct lowpan_peer *peer) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 91 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 92 | list_add_rcu(&peer->list, &dev->peers); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 93 | atomic_inc(&dev->peer_count); |
| 94 | } |
| 95 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 96 | static inline bool peer_del(struct lowpan_btle_dev *dev, |
| 97 | struct lowpan_peer *peer) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 98 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 99 | list_del_rcu(&peer->list); |
Johan Hedberg | 4e79022 | 2014-11-11 14:16:29 +0200 | [diff] [blame] | 100 | kfree_rcu(peer, rcu); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 101 | |
Jukka Rissanen | 18d93c1 | 2014-06-18 16:37:10 +0300 | [diff] [blame] | 102 | module_put(THIS_MODULE); |
| 103 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 104 | if (atomic_dec_and_test(&dev->peer_count)) { |
| 105 | BT_DBG("last peer"); |
| 106 | return true; |
| 107 | } |
| 108 | |
| 109 | return false; |
| 110 | } |
| 111 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 112 | static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_btle_dev *dev, |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 113 | bdaddr_t *ba, __u8 type) |
| 114 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 115 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 116 | |
| 117 | BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count), |
| 118 | ba, type); |
| 119 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 120 | rcu_read_lock(); |
| 121 | |
| 122 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 123 | BT_DBG("dst addr %pMR dst type %d", |
| 124 | &peer->chan->dst, peer->chan->dst_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 125 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 126 | if (bacmp(&peer->chan->dst, ba)) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 127 | continue; |
| 128 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 129 | if (type == peer->chan->dst_type) { |
| 130 | rcu_read_unlock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 131 | return peer; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 132 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 133 | } |
| 134 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 135 | rcu_read_unlock(); |
| 136 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 137 | return NULL; |
| 138 | } |
| 139 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 140 | static inline struct lowpan_peer * |
| 141 | __peer_lookup_chan(struct lowpan_btle_dev *dev, struct l2cap_chan *chan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 142 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 143 | struct lowpan_peer *peer; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 144 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 145 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 146 | if (peer->chan == chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 147 | return peer; |
| 148 | } |
| 149 | |
| 150 | return NULL; |
| 151 | } |
| 152 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 153 | static inline struct lowpan_peer * |
| 154 | __peer_lookup_conn(struct lowpan_btle_dev *dev, struct l2cap_conn *conn) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 155 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 156 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 157 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 158 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 159 | if (peer->chan->conn == conn) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 160 | return peer; |
| 161 | } |
| 162 | |
| 163 | return NULL; |
| 164 | } |
| 165 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 166 | static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_btle_dev *dev, |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 167 | struct in6_addr *daddr, |
| 168 | struct sk_buff *skb) |
| 169 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 170 | struct lowpan_peer *peer; |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 171 | 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 Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 181 | 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 Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 188 | |
| 189 | if (!rt) { |
| 190 | nexthop = &lowpan_cb(skb)->gw; |
| 191 | |
| 192 | if (ipv6_addr_any(nexthop)) |
| 193 | return NULL; |
| 194 | } else { |
Martin KaFai Lau | 2647a9b | 2015-05-22 20:55:58 -0700 | [diff] [blame] | 195 | nexthop = rt6_nexthop(rt, daddr); |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 196 | |
| 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 Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 206 | rcu_read_lock(); |
| 207 | |
| 208 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 209 | BT_DBG("dst addr %pMR dst type %d ip %pI6c", |
| 210 | &peer->chan->dst, peer->chan->dst_type, |
| 211 | &peer->peer_addr); |
| 212 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 213 | if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) { |
| 214 | rcu_read_unlock(); |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 215 | return peer; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 216 | } |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 217 | } |
| 218 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 219 | rcu_read_unlock(); |
| 220 | |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 221 | return NULL; |
| 222 | } |
| 223 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 224 | static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn) |
| 225 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 226 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 227 | struct lowpan_peer *peer = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 228 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 229 | rcu_read_lock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 230 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 231 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 232 | peer = __peer_lookup_conn(entry, conn); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 233 | if (peer) |
| 234 | break; |
| 235 | } |
| 236 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 237 | rcu_read_unlock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 238 | |
| 239 | return peer; |
| 240 | } |
| 241 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 242 | static struct lowpan_btle_dev *lookup_dev(struct l2cap_conn *conn) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 243 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 244 | struct lowpan_btle_dev *entry; |
| 245 | struct lowpan_btle_dev *dev = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 246 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 247 | rcu_read_lock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 248 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 249 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 250 | if (conn->hcon->hdev == entry->hdev) { |
| 251 | dev = entry; |
| 252 | break; |
| 253 | } |
| 254 | } |
| 255 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 256 | rcu_read_unlock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 257 | |
| 258 | return dev; |
| 259 | } |
| 260 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 261 | static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev) |
| 262 | { |
| 263 | struct sk_buff *skb_cp; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 264 | |
| 265 | skb_cp = skb_copy(skb, GFP_ATOMIC); |
| 266 | if (!skb_cp) |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 267 | return NET_RX_DROP; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 268 | |
Alexander Aring | 324e786 | 2015-10-27 08:35:24 +0100 | [diff] [blame] | 269 | return netif_rx_ni(skb_cp); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 270 | } |
| 271 | |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 272 | static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev, |
| 273 | struct l2cap_chan *chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 274 | { |
Patrik Flykt | c259d14 | 2017-03-12 10:19:33 +0200 | [diff] [blame] | 275 | const u8 *saddr; |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 276 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 277 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 278 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 279 | dev = lowpan_btle_dev(netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 280 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 281 | rcu_read_lock(); |
| 282 | peer = __peer_lookup_chan(dev, chan); |
| 283 | rcu_read_unlock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 284 | if (!peer) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 285 | return -EINVAL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 286 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame] | 287 | saddr = peer->lladdr; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 288 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame] | 289 | return lowpan_header_decompress(skb, netdev, netdev->dev_addr, saddr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | static int recv_pkt(struct sk_buff *skb, struct net_device *dev, |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 293 | struct l2cap_chan *chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 294 | { |
| 295 | struct sk_buff *local_skb; |
| 296 | int ret; |
| 297 | |
| 298 | if (!netif_running(dev)) |
| 299 | goto drop; |
| 300 | |
Alexander Aring | cefdb80 | 2015-10-13 13:42:55 +0200 | [diff] [blame] | 301 | if (dev->type != ARPHRD_6LOWPAN || !skb->len) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 302 | goto drop; |
| 303 | |
Alexander Aring | cefdb80 | 2015-10-13 13:42:55 +0200 | [diff] [blame] | 304 | skb_reset_network_header(skb); |
| 305 | |
Martin Townsend | 11e3ff7 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 306 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 307 | if (!skb) |
| 308 | goto drop; |
| 309 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 310 | /* check that it's our buffer */ |
Alexander Aring | cefdb80 | 2015-10-13 13:42:55 +0200 | [diff] [blame] | 311 | if (lowpan_is_ipv6(*skb_network_header(skb))) { |
Lukasz Duda | 87f5fed | 2016-01-13 16:57:48 +0100 | [diff] [blame] | 312 | /* Pull off the 1-byte of 6lowpan header. */ |
| 313 | skb_pull(skb, 1); |
| 314 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 315 | /* 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 Bakke | 4c58f32 | 2016-01-13 16:41:42 +0100 | [diff] [blame] | 325 | local_skb->dev = dev; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 326 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 327 | 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 Townsend | 3c400b8 | 2014-10-23 15:40:55 +0100 | [diff] [blame] | 337 | consume_skb(local_skb); |
| 338 | consume_skb(skb); |
Alexander Aring | cefdb80 | 2015-10-13 13:42:55 +0200 | [diff] [blame] | 339 | } 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 Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 343 | |
Glenn Ruben Bakke | 4c58f32 | 2016-01-13 16:41:42 +0100 | [diff] [blame] | 344 | local_skb->dev = dev; |
| 345 | |
Alexander Aring | cefdb80 | 2015-10-13 13:42:55 +0200 | [diff] [blame] | 346 | ret = iphc_decompress(local_skb, dev, chan); |
| 347 | if (ret < 0) { |
| 348 | kfree_skb(local_skb); |
| 349 | goto drop; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 350 | } |
Alexander Aring | cefdb80 | 2015-10-13 13:42:55 +0200 | [diff] [blame] | 351 | |
| 352 | local_skb->protocol = htons(ETH_P_IPV6); |
| 353 | local_skb->pkt_type = PACKET_HOST; |
Alexander Aring | cefdb80 | 2015-10-13 13:42:55 +0200 | [diff] [blame] | 354 | |
| 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 Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | return NET_RX_SUCCESS; |
| 371 | |
| 372 | drop: |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 373 | dev->stats.rx_dropped++; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 374 | return NET_RX_DROP; |
| 375 | } |
| 376 | |
| 377 | /* Packet from BT LE device */ |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 378 | static int chan_recv_cb(struct l2cap_chan *chan, struct sk_buff *skb) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 379 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 380 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 381 | struct lowpan_peer *peer; |
| 382 | int err; |
| 383 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 384 | peer = lookup_peer(chan->conn); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 385 | if (!peer) |
| 386 | return -ENOENT; |
| 387 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 388 | dev = lookup_dev(chan->conn); |
Johan Hedberg | 30d3db4 | 2013-12-12 09:53:21 +0200 | [diff] [blame] | 389 | if (!dev || !dev->netdev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 390 | return -ENOENT; |
| 391 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 392 | err = recv_pkt(skb, dev->netdev, chan); |
| 393 | if (err) { |
| 394 | BT_DBG("recv pkt %d", err); |
| 395 | err = -EAGAIN; |
| 396 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 397 | |
| 398 | return err; |
| 399 | } |
| 400 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 401 | static int setup_header(struct sk_buff *skb, struct net_device *netdev, |
| 402 | bdaddr_t *peer_addr, u8 *peer_addr_type) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 403 | { |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 404 | struct in6_addr ipv6_daddr; |
Glenn Ruben Bakke | 5544107 | 2016-04-22 18:06:11 +0200 | [diff] [blame] | 405 | struct ipv6hdr *hdr; |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 406 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 407 | struct lowpan_peer *peer; |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 408 | u8 *daddr; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 409 | int err, status = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 410 | |
Glenn Ruben Bakke | 5544107 | 2016-04-22 18:06:11 +0200 | [diff] [blame] | 411 | hdr = ipv6_hdr(skb); |
| 412 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 413 | dev = lowpan_btle_dev(netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 414 | |
Glenn Ruben Bakke | 5544107 | 2016-04-22 18:06:11 +0200 | [diff] [blame] | 415 | memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr)); |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 416 | |
| 417 | if (ipv6_addr_is_multicast(&ipv6_daddr)) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 418 | lowpan_cb(skb)->chan = NULL; |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 419 | daddr = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 420 | } else { |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 421 | BT_DBG("dest IP %pI6c", &ipv6_daddr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 422 | |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 423 | /* 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 Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 427 | */ |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 428 | peer = peer_lookup_dst(dev, &ipv6_daddr, skb); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 429 | if (!peer) { |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 430 | BT_DBG("no such peer"); |
| 431 | return -ENOENT; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 432 | } |
| 433 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame] | 434 | daddr = peer->lladdr; |
Luiz Augusto von Dentz | 9dae2e0 | 2017-03-12 10:19:38 +0200 | [diff] [blame] | 435 | peer_addr = &peer->chan->dst; |
| 436 | *peer_addr_type = peer->chan->dst_type; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 437 | lowpan_cb(skb)->chan = peer->chan; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 438 | |
| 439 | status = 1; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 440 | } |
| 441 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 442 | lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 443 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 444 | 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 | |
| 451 | static 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 Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 455 | if (type != ETH_P_IPV6) |
| 456 | return -EINVAL; |
| 457 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 458 | return 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | /* Packet to BT LE device */ |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 462 | static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb, |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 463 | struct net_device *netdev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 464 | { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 465 | struct msghdr msg; |
| 466 | struct kvec iv; |
| 467 | int err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 468 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 469 | /* Remember the skb so that we can send EAGAIN to the caller if |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 470 | * we run out of credits. |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 471 | */ |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 472 | chan->data = skb; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 473 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 474 | iv.iov_base = skb->data; |
| 475 | iv.iov_len = skb->len; |
| 476 | |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 477 | memset(&msg, 0, sizeof(msg)); |
Al Viro | 1783639 | 2014-11-24 17:07:38 -0500 | [diff] [blame] | 478 | iov_iter_kvec(&msg.msg_iter, WRITE | ITER_KVEC, &iv, 1, skb->len); |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 479 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 480 | 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 Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 498 | } |
| 499 | |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 500 | static int send_mcast_pkt(struct sk_buff *skb, struct net_device *netdev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 501 | { |
| 502 | struct sk_buff *local_skb; |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 503 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 504 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 505 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 506 | rcu_read_lock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 507 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 508 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 509 | struct lowpan_peer *pentry; |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 510 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 511 | |
| 512 | if (entry->netdev != netdev) |
| 513 | continue; |
| 514 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 515 | dev = lowpan_btle_dev(entry->netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 516 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 517 | list_for_each_entry_rcu(pentry, &dev->peers, list) { |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 518 | int ret; |
| 519 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 520 | local_skb = skb_clone(skb, GFP_ATOMIC); |
| 521 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 522 | 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 Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 526 | ret = send_pkt(pentry->chan, local_skb, netdev); |
| 527 | if (ret < 0) |
| 528 | err = ret; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 529 | |
| 530 | kfree_skb(local_skb); |
| 531 | } |
| 532 | } |
| 533 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 534 | rcu_read_unlock(); |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 535 | |
| 536 | return err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 540 | { |
| 541 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 542 | bdaddr_t addr; |
| 543 | u8 addr_type; |
| 544 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 545 | /* 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 Aring | b0c42cd | 2014-10-08 10:24:53 +0200 | [diff] [blame] | 548 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 549 | if (!skb) |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 550 | 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 Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 568 | err = send_pkt(lowpan_cb(skb)->chan, skb, netdev); |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 569 | } else { |
| 570 | err = -ENOENT; |
| 571 | } |
| 572 | } else { |
| 573 | /* We need to send the packet to every device behind this |
| 574 | * interface. |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 575 | */ |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 576 | err = send_mcast_pkt(skb, netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 577 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 578 | |
Jukka Rissanen | fc12518 | 2014-10-01 11:30:26 +0300 | [diff] [blame] | 579 | dev_kfree_skb(skb); |
| 580 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 581 | if (err) |
| 582 | BT_DBG("ERROR: xmit failed (%d)", err); |
| 583 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 584 | return err < 0 ? NET_XMIT_DROP : err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 585 | } |
| 586 | |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 587 | static int bt_dev_init(struct net_device *dev) |
| 588 | { |
Eric Dumazet | d3fff6c | 2016-06-09 07:45:12 -0700 | [diff] [blame] | 589 | netdev_lockdep_set_classes(dev); |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 590 | |
| 591 | return 0; |
| 592 | } |
| 593 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 594 | static const struct net_device_ops netdev_ops = { |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 595 | .ndo_init = bt_dev_init, |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 596 | .ndo_start_xmit = bt_xmit, |
| 597 | }; |
| 598 | |
| 599 | static struct header_ops header_ops = { |
| 600 | .create = header_create, |
| 601 | }; |
| 602 | |
| 603 | static void netdev_setup(struct net_device *dev) |
| 604 | { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 605 | dev->hard_header_len = 0; |
| 606 | dev->needed_tailroom = 0; |
Jukka Rissanen | 156395c | 2014-09-29 16:37:26 +0300 | [diff] [blame] | 607 | dev->flags = IFF_RUNNING | IFF_POINTOPOINT | |
| 608 | IFF_MULTICAST; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 609 | dev->watchdog_timeo = 0; |
| 610 | |
| 611 | dev->netdev_ops = &netdev_ops; |
| 612 | dev->header_ops = &header_ops; |
| 613 | dev->destructor = free_netdev; |
| 614 | } |
| 615 | |
| 616 | static struct device_type bt_type = { |
| 617 | .name = "bluetooth", |
| 618 | }; |
| 619 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 620 | static 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 Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 631 | static 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 Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 642 | static void do_notify_peers(struct work_struct *work) |
| 643 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 644 | struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev, |
| 645 | notify_peers.work); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 646 | |
| 647 | netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */ |
| 648 | } |
| 649 | |
| 650 | static bool is_bt_6lowpan(struct hci_conn *hcon) |
| 651 | { |
| 652 | if (hcon->type != LE_LINK) |
| 653 | return false; |
| 654 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 655 | if (!enable_6lowpan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 656 | return false; |
| 657 | |
| 658 | return true; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 659 | } |
| 660 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 661 | static 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 Hedberg | 301de2c | 2015-10-06 13:03:19 +0300 | [diff] [blame] | 673 | chan->imtu = 1280; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 674 | |
| 675 | return chan; |
| 676 | } |
| 677 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 678 | static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan, |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 679 | struct lowpan_btle_dev *dev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 680 | { |
| 681 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 682 | |
| 683 | peer = kzalloc(sizeof(*peer), GFP_ATOMIC); |
| 684 | if (!peer) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 685 | return NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 686 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 687 | peer->chan = chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 688 | memset(&peer->peer_addr, 0, sizeof(struct in6_addr)); |
| 689 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame] | 690 | baswap((void *)peer->lladdr, &chan->dst); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 691 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame] | 692 | lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 693 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 694 | spin_lock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 695 | INIT_LIST_HEAD(&peer->list); |
| 696 | peer_add(dev, peer); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 697 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 698 | |
| 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 Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 703 | return peer->chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 704 | } |
| 705 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 706 | static int setup_netdev(struct l2cap_chan *chan, struct lowpan_btle_dev **dev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 707 | { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 708 | struct net_device *netdev; |
| 709 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 710 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 711 | netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)), |
Alexander Aring | b72f6f51 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 712 | IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN, |
| 713 | netdev_setup); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 714 | if (!netdev) |
| 715 | return -ENOMEM; |
| 716 | |
Patrik Flykt | c259d14 | 2017-03-12 10:19:33 +0200 | [diff] [blame] | 717 | netdev->addr_assign_type = NET_ADDR_PERM; |
| 718 | baswap((void *)netdev->dev_addr, &chan->src); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 719 | |
| 720 | netdev->netdev_ops = &netdev_ops; |
Glenn Ruben Bakke | fc84242 | 2015-06-17 07:32:25 -0700 | [diff] [blame] | 721 | SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 722 | SET_NETDEV_DEVTYPE(netdev, &bt_type); |
| 723 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 724 | *dev = lowpan_btle_dev(netdev); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 725 | (*dev)->netdev = netdev; |
| 726 | (*dev)->hdev = chan->conn->hcon->hdev; |
| 727 | INIT_LIST_HEAD(&(*dev)->peers); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 728 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 729 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 730 | INIT_LIST_HEAD(&(*dev)->list); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 731 | list_add_rcu(&(*dev)->list, &bt_6lowpan_devices); |
| 732 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 733 | |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 734 | err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE); |
Alexander Aring | 5857d1d | 2015-07-30 09:40:53 +0200 | [diff] [blame] | 735 | 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 Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 749 | return 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 750 | |
| 751 | out: |
| 752 | return err; |
| 753 | } |
| 754 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 755 | static inline void chan_ready_cb(struct l2cap_chan *chan) |
| 756 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 757 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 758 | |
| 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 Rissanen | 18d93c1 | 2014-06-18 16:37:10 +0300 | [diff] [blame] | 770 | if (!try_module_get(THIS_MODULE)) |
| 771 | return; |
| 772 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 773 | add_peer_chan(chan, dev); |
| 774 | ifup(dev->netdev); |
| 775 | } |
| 776 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 777 | static inline struct l2cap_chan *chan_new_conn_cb(struct l2cap_chan *pchan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 778 | { |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 779 | struct l2cap_chan *chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 780 | |
Johan Hedberg | 630ef79 | 2015-10-06 13:03:22 +0300 | [diff] [blame] | 781 | chan = chan_create(); |
| 782 | if (!chan) |
| 783 | return NULL; |
| 784 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 785 | chan->ops = pchan->ops; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 786 | |
| 787 | BT_DBG("chan %p pchan %p", chan, pchan); |
| 788 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 789 | return chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 790 | } |
| 791 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 792 | static void delete_netdev(struct work_struct *work) |
| 793 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 794 | struct lowpan_btle_dev *entry = container_of(work, |
| 795 | struct lowpan_btle_dev, |
| 796 | delete_netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 797 | |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 798 | lowpan_unregister_netdev(entry->netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 799 | |
Glenn Ruben Bakke | 2ad88fb | 2015-06-17 07:32:26 -0700 | [diff] [blame] | 800 | /* The entry pointer is deleted by the netdev destructor. */ |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 801 | } |
| 802 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 803 | static void chan_close_cb(struct l2cap_chan *chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 804 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 805 | struct lowpan_btle_dev *entry; |
| 806 | struct lowpan_btle_dev *dev = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 807 | struct lowpan_peer *peer; |
| 808 | int err = -ENOENT; |
Glenn Ruben Bakke | f63666d2 | 2015-06-17 07:32:24 -0700 | [diff] [blame] | 809 | bool last = false, remove = true; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 810 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 811 | 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 Bakke | f63666d2 | 2015-06-17 07:32:24 -0700 | [diff] [blame] | 820 | remove = false; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 821 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 822 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 823 | spin_lock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 824 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 825 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 826 | dev = lowpan_btle_dev(entry->netdev); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 827 | peer = __peer_lookup_chan(dev, chan); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 828 | if (peer) { |
| 829 | last = peer_del(dev, peer); |
| 830 | err = 0; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 831 | |
| 832 | BT_DBG("dev %p removing %speer %p", dev, |
| 833 | last ? "last " : "1 ", peer); |
| 834 | BT_DBG("chan %p orig refcnt %d", chan, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 835 | kref_read(&chan->kref)); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 836 | |
| 837 | l2cap_chan_put(chan); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 838 | break; |
| 839 | } |
| 840 | } |
| 841 | |
| 842 | if (!err && last && dev && !atomic_read(&dev->peer_count)) { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 843 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 844 | |
| 845 | cancel_delayed_work_sync(&dev->notify_peers); |
| 846 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 847 | ifdown(dev->netdev); |
| 848 | |
Glenn Ruben Bakke | f63666d2 | 2015-06-17 07:32:24 -0700 | [diff] [blame] | 849 | if (remove) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 850 | INIT_WORK(&entry->delete_netdev, delete_netdev); |
| 851 | schedule_work(&entry->delete_netdev); |
| 852 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 853 | } else { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 854 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 855 | } |
| 856 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 857 | return; |
| 858 | } |
| 859 | |
| 860 | static 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 | |
| 866 | static 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 | |
| 877 | static 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 Rissanen | 59790aa | 2014-09-29 10:55:46 +0300 | [diff] [blame] | 883 | if (!skb) |
| 884 | return; |
| 885 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 886 | lowpan_cb(skb)->status = -EAGAIN; |
| 887 | } |
| 888 | |
| 889 | static 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 Rissanen | 59790aa | 2014-09-29 10:55:46 +0300 | [diff] [blame] | 895 | if (!skb) |
| 896 | return; |
| 897 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 898 | lowpan_cb(skb)->status = 0; |
| 899 | } |
| 900 | |
| 901 | static long chan_get_sndtimeo_cb(struct l2cap_chan *chan) |
| 902 | { |
Jukka Rissanen | 2ae50d8 | 2014-09-08 12:11:43 +0300 | [diff] [blame] | 903 | return L2CAP_CONN_TIMEOUT; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | static 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 Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 917 | |
| 918 | .teardown = l2cap_chan_no_teardown, |
| 919 | .defer = l2cap_chan_no_defer, |
| 920 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 921 | }; |
| 922 | |
| 923 | static 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 Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 931 | static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type) |
| 932 | { |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 933 | struct l2cap_chan *chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 934 | int err; |
| 935 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 936 | chan = chan_create(); |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 937 | if (!chan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 938 | return -EINVAL; |
| 939 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 940 | chan->ops = &bt_6lowpan_chan_ops; |
| 941 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 942 | err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0, |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 943 | addr, dst_type); |
| 944 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 945 | BT_DBG("chan %p err %d", chan, err); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 946 | if (err < 0) |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 947 | l2cap_chan_put(chan); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 948 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 949 | return err; |
| 950 | } |
| 951 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 952 | static 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 | |
| 969 | static struct l2cap_chan *bt_6lowpan_listen(void) |
| 970 | { |
| 971 | bdaddr_t *addr = BDADDR_ANY; |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 972 | struct l2cap_chan *chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 973 | int err; |
| 974 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 975 | if (!enable_6lowpan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 976 | return NULL; |
| 977 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 978 | chan = chan_create(); |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 979 | if (!chan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 980 | return NULL; |
| 981 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 982 | chan->ops = &bt_6lowpan_chan_ops; |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 983 | chan->state = BT_LISTEN; |
| 984 | chan->src_type = BDADDR_LE_PUBLIC; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 985 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 986 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); |
Johan Hedberg | 2773b02 | 2014-11-13 09:46:05 +0200 | [diff] [blame] | 987 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 988 | BT_DBG("chan %p src type %d", chan, chan->src_type); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 989 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 990 | err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP)); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 991 | if (err) { |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 992 | l2cap_chan_put(chan); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 993 | BT_ERR("psm cannot be added err %d", err); |
| 994 | return NULL; |
| 995 | } |
| 996 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 997 | return chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 998 | } |
| 999 | |
| 1000 | static 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 Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1005 | 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 Hedberg | 39385cb | 2016-11-12 17:03:07 +0200 | [diff] [blame] | 1015 | /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */ |
| 1016 | hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1017 | if (!hdev) |
| 1018 | return -ENOENT; |
| 1019 | |
| 1020 | hci_dev_lock(hdev); |
Johan Hedberg | f5ad4ff | 2015-10-21 18:03:02 +0300 | [diff] [blame] | 1021 | hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1022 | 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 | |
| 1034 | static void disconnect_all_peers(void) |
| 1035 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1036 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1037 | struct lowpan_peer *peer, *tmp_peer, *new_peer; |
| 1038 | struct list_head peers; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1039 | |
| 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 Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1047 | rcu_read_lock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1048 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1049 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 1050 | list_for_each_entry_rcu(peer, &entry->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1051 | 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 Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1062 | rcu_read_unlock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1063 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1064 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1065 | list_for_each_entry_safe(peer, tmp_peer, &peers, list) { |
| 1066 | l2cap_chan_close(peer->chan, ENOENT); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1067 | |
| 1068 | list_del_rcu(&peer->list); |
Johan Hedberg | 4e79022 | 2014-11-11 14:16:29 +0200 | [diff] [blame] | 1069 | kfree_rcu(peer, rcu); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1070 | } |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1071 | spin_unlock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1072 | } |
| 1073 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1074 | struct set_enable { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1075 | struct work_struct work; |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1076 | bool flag; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1077 | }; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1078 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1079 | static void do_enable_set(struct work_struct *work) |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1080 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1081 | struct set_enable *set_enable = container_of(work, |
| 1082 | struct set_enable, work); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1083 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1084 | if (!set_enable->flag || enable_6lowpan != set_enable->flag) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1085 | /* Disconnect existing connections if 6lowpan is |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1086 | * disabled |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1087 | */ |
| 1088 | disconnect_all_peers(); |
| 1089 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1090 | enable_6lowpan = set_enable->flag; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1091 | |
| 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 Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1099 | kfree(set_enable); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1100 | } |
| 1101 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1102 | static int lowpan_enable_set(void *data, u64 val) |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1103 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1104 | struct set_enable *set_enable; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1105 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1106 | set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL); |
| 1107 | if (!set_enable) |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1108 | return -ENOMEM; |
| 1109 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1110 | set_enable->flag = !!val; |
| 1111 | INIT_WORK(&set_enable->work, do_enable_set); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1112 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1113 | schedule_work(&set_enable->work); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1114 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1115 | return 0; |
| 1116 | } |
| 1117 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1118 | static int lowpan_enable_get(void *data, u64 *val) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1119 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1120 | *val = enable_6lowpan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1124 | DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get, |
| 1125 | lowpan_enable_set, "%llu\n"); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1126 | |
| 1127 | static 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 | |
| 1194 | static int lowpan_control_show(struct seq_file *f, void *ptr) |
| 1195 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1196 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1197 | struct lowpan_peer *peer; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1198 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1199 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1200 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1201 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
| 1202 | list_for_each_entry(peer, &entry->peers, list) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1203 | seq_printf(f, "%pMR (type %u)\n", |
| 1204 | &peer->chan->dst, peer->chan->dst_type); |
| 1205 | } |
| 1206 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1207 | spin_unlock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1208 | |
| 1209 | return 0; |
| 1210 | } |
| 1211 | |
| 1212 | static int lowpan_control_open(struct inode *inode, struct file *file) |
| 1213 | { |
| 1214 | return single_open(file, lowpan_control_show, inode->i_private); |
| 1215 | } |
| 1216 | |
| 1217 | static 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 Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1225 | static void disconnect_devices(void) |
| 1226 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1227 | struct lowpan_btle_dev *entry, *tmp, *new_dev; |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1228 | struct list_head devices; |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1229 | |
| 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 Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1237 | rcu_read_lock(); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1238 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1239 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1240 | 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 Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1247 | list_add_rcu(&new_dev->list, &devices); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1248 | } |
| 1249 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1250 | rcu_read_unlock(); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1251 | |
Dan Carpenter | daac197 | 2014-10-29 19:10:57 +0300 | [diff] [blame] | 1252 | list_for_each_entry_safe(entry, tmp, &devices, list) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1253 | ifdown(entry->netdev); |
| 1254 | BT_DBG("Unregistering netdev %s %p", |
| 1255 | entry->netdev->name, entry->netdev); |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 1256 | lowpan_unregister_netdev(entry->netdev); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1257 | kfree(entry); |
| 1258 | } |
| 1259 | } |
| 1260 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1261 | static 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 Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1265 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1266 | |
| 1267 | if (netdev->type != ARPHRD_6LOWPAN) |
| 1268 | return NOTIFY_DONE; |
| 1269 | |
| 1270 | switch (event) { |
| 1271 | case NETDEV_UNREGISTER: |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1272 | spin_lock(&devices_lock); |
| 1273 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1274 | if (entry->netdev == netdev) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1275 | BT_DBG("Unregistered netdev %s %p", |
| 1276 | netdev->name, netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1277 | list_del(&entry->list); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1278 | break; |
| 1279 | } |
| 1280 | } |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1281 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1282 | break; |
| 1283 | } |
| 1284 | |
| 1285 | return NOTIFY_DONE; |
| 1286 | } |
| 1287 | |
| 1288 | static struct notifier_block bt_6lowpan_dev_notifier = { |
| 1289 | .notifier_call = device_event, |
| 1290 | }; |
| 1291 | |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1292 | static int __init bt_6lowpan_init(void) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1293 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1294 | lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644, |
| 1295 | bt_debugfs, NULL, |
| 1296 | &lowpan_enable_fops); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1297 | lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644, |
| 1298 | bt_debugfs, NULL, |
| 1299 | &lowpan_control_fops); |
| 1300 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1301 | return register_netdevice_notifier(&bt_6lowpan_dev_notifier); |
| 1302 | } |
| 1303 | |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1304 | static void __exit bt_6lowpan_exit(void) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1305 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1306 | debugfs_remove(lowpan_enable_debugfs); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1307 | 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 Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1314 | disconnect_devices(); |
| 1315 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1316 | unregister_netdevice_notifier(&bt_6lowpan_dev_notifier); |
| 1317 | } |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1318 | |
| 1319 | module_init(bt_6lowpan_init); |
| 1320 | module_exit(bt_6lowpan_exit); |
| 1321 | |
| 1322 | MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>"); |
| 1323 | MODULE_DESCRIPTION("Bluetooth 6LoWPAN"); |
| 1324 | MODULE_VERSION(VERSION); |
| 1325 | MODULE_LICENSE("GPL"); |