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 | 11e3ff70 | 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 | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 401 | static u8 get_addr_type_from_eui64(u8 byte) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 402 | { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 403 | /* Is universal(0) or local(1) bit */ |
| 404 | return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC); |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr) |
| 408 | { |
| 409 | u8 *eui64 = ip6_daddr->s6_addr + 8; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 410 | |
| 411 | addr->b[0] = eui64[7]; |
| 412 | addr->b[1] = eui64[6]; |
| 413 | addr->b[2] = eui64[5]; |
| 414 | addr->b[3] = eui64[2]; |
| 415 | addr->b[4] = eui64[1]; |
| 416 | addr->b[5] = eui64[0]; |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 417 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 418 | |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 419 | static void convert_dest_bdaddr(struct in6_addr *ip6_daddr, |
| 420 | bdaddr_t *addr, u8 *addr_type) |
| 421 | { |
| 422 | copy_to_bdaddr(ip6_daddr, addr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 423 | |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 424 | /* We need to toggle the U/L bit that we got from IPv6 address |
| 425 | * so that we get the proper address and type of the BD address. |
| 426 | */ |
| 427 | addr->b[5] ^= 0x02; |
| 428 | |
| 429 | *addr_type = get_addr_type_from_eui64(addr->b[5]); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 430 | } |
| 431 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 432 | static int setup_header(struct sk_buff *skb, struct net_device *netdev, |
| 433 | bdaddr_t *peer_addr, u8 *peer_addr_type) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 434 | { |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 435 | struct in6_addr ipv6_daddr; |
Glenn Ruben Bakke | 5544107 | 2016-04-22 18:06:11 +0200 | [diff] [blame] | 436 | struct ipv6hdr *hdr; |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 437 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 438 | struct lowpan_peer *peer; |
| 439 | bdaddr_t addr, *any = BDADDR_ANY; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 440 | u8 *daddr = any->b; |
| 441 | int err, status = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 442 | |
Glenn Ruben Bakke | 5544107 | 2016-04-22 18:06:11 +0200 | [diff] [blame] | 443 | hdr = ipv6_hdr(skb); |
| 444 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 445 | dev = lowpan_btle_dev(netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 446 | |
Glenn Ruben Bakke | 5544107 | 2016-04-22 18:06:11 +0200 | [diff] [blame] | 447 | memcpy(&ipv6_daddr, &hdr->daddr, sizeof(ipv6_daddr)); |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 448 | |
| 449 | if (ipv6_addr_is_multicast(&ipv6_daddr)) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 450 | lowpan_cb(skb)->chan = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 451 | } else { |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 452 | u8 addr_type; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 453 | |
| 454 | /* Get destination BT device from skb. |
| 455 | * If there is no such peer then discard the packet. |
| 456 | */ |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 457 | convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 458 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 459 | BT_DBG("dest addr %pMR type %d IP %pI6c", &addr, |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 460 | addr_type, &ipv6_daddr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 461 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 462 | peer = peer_lookup_ba(dev, &addr, addr_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 463 | if (!peer) { |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 464 | /* The packet might be sent to 6lowpan interface |
| 465 | * because of routing (either via default route |
| 466 | * or user set route) so get peer according to |
| 467 | * the destination address. |
| 468 | */ |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 469 | peer = peer_lookup_dst(dev, &ipv6_daddr, skb); |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 470 | if (!peer) { |
| 471 | BT_DBG("no such peer %pMR found", &addr); |
| 472 | return -ENOENT; |
| 473 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 474 | } |
| 475 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame^] | 476 | daddr = peer->lladdr; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 477 | *peer_addr = addr; |
| 478 | *peer_addr_type = addr_type; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 479 | lowpan_cb(skb)->chan = peer->chan; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 480 | |
| 481 | status = 1; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 482 | } |
| 483 | |
Alexander Aring | a6f7738 | 2015-10-13 13:42:57 +0200 | [diff] [blame] | 484 | lowpan_header_compress(skb, netdev, daddr, dev->netdev->dev_addr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 485 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 486 | err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0); |
| 487 | if (err < 0) |
| 488 | return err; |
| 489 | |
| 490 | return status; |
| 491 | } |
| 492 | |
| 493 | static int header_create(struct sk_buff *skb, struct net_device *netdev, |
| 494 | unsigned short type, const void *_daddr, |
| 495 | const void *_saddr, unsigned int len) |
| 496 | { |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 497 | if (type != ETH_P_IPV6) |
| 498 | return -EINVAL; |
| 499 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 500 | return 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | /* Packet to BT LE device */ |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 504 | static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb, |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 505 | struct net_device *netdev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 506 | { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 507 | struct msghdr msg; |
| 508 | struct kvec iv; |
| 509 | int err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 510 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 511 | /* 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] | 512 | * we run out of credits. |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 513 | */ |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 514 | chan->data = skb; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 515 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 516 | iv.iov_base = skb->data; |
| 517 | iv.iov_len = skb->len; |
| 518 | |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame] | 519 | memset(&msg, 0, sizeof(msg)); |
Al Viro | 1783639 | 2014-11-24 17:07:38 -0500 | [diff] [blame] | 520 | 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] | 521 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 522 | err = l2cap_chan_send(chan, &msg, skb->len); |
| 523 | if (err > 0) { |
| 524 | netdev->stats.tx_bytes += err; |
| 525 | netdev->stats.tx_packets++; |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | if (!err) |
| 530 | err = lowpan_cb(skb)->status; |
| 531 | |
| 532 | if (err < 0) { |
| 533 | if (err == -EAGAIN) |
| 534 | netdev->stats.tx_dropped++; |
| 535 | else |
| 536 | netdev->stats.tx_errors++; |
| 537 | } |
| 538 | |
| 539 | return err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 540 | } |
| 541 | |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 542 | 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] | 543 | { |
| 544 | struct sk_buff *local_skb; |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 545 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 546 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 547 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 548 | rcu_read_lock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 549 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 550 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 551 | struct lowpan_peer *pentry; |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 552 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 553 | |
| 554 | if (entry->netdev != netdev) |
| 555 | continue; |
| 556 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 557 | dev = lowpan_btle_dev(entry->netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 558 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 559 | list_for_each_entry_rcu(pentry, &dev->peers, list) { |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 560 | int ret; |
| 561 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 562 | local_skb = skb_clone(skb, GFP_ATOMIC); |
| 563 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 564 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", |
| 565 | netdev->name, |
| 566 | &pentry->chan->dst, pentry->chan->dst_type, |
| 567 | &pentry->peer_addr, pentry->chan); |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 568 | ret = send_pkt(pentry->chan, local_skb, netdev); |
| 569 | if (ret < 0) |
| 570 | err = ret; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 571 | |
| 572 | kfree_skb(local_skb); |
| 573 | } |
| 574 | } |
| 575 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 576 | rcu_read_unlock(); |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 577 | |
| 578 | return err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 579 | } |
| 580 | |
| 581 | static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 582 | { |
| 583 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 584 | bdaddr_t addr; |
| 585 | u8 addr_type; |
| 586 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 587 | /* We must take a copy of the skb before we modify/replace the ipv6 |
| 588 | * header as the header could be used elsewhere |
| 589 | */ |
Alexander Aring | b0c42cd | 2014-10-08 10:24:53 +0200 | [diff] [blame] | 590 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 591 | if (!skb) |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 592 | return NET_XMIT_DROP; |
| 593 | |
| 594 | /* Return values from setup_header() |
| 595 | * <0 - error, packet is dropped |
| 596 | * 0 - this is a multicast packet |
| 597 | * 1 - this is unicast packet |
| 598 | */ |
| 599 | err = setup_header(skb, netdev, &addr, &addr_type); |
| 600 | if (err < 0) { |
| 601 | kfree_skb(skb); |
| 602 | return NET_XMIT_DROP; |
| 603 | } |
| 604 | |
| 605 | if (err) { |
| 606 | if (lowpan_cb(skb)->chan) { |
| 607 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", |
| 608 | netdev->name, &addr, addr_type, |
| 609 | &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan); |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 610 | err = send_pkt(lowpan_cb(skb)->chan, skb, netdev); |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 611 | } else { |
| 612 | err = -ENOENT; |
| 613 | } |
| 614 | } else { |
| 615 | /* We need to send the packet to every device behind this |
| 616 | * interface. |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 617 | */ |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 618 | err = send_mcast_pkt(skb, netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 619 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 620 | |
Jukka Rissanen | fc12518 | 2014-10-01 11:30:26 +0300 | [diff] [blame] | 621 | dev_kfree_skb(skb); |
| 622 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 623 | if (err) |
| 624 | BT_DBG("ERROR: xmit failed (%d)", err); |
| 625 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 626 | return err < 0 ? NET_XMIT_DROP : err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 627 | } |
| 628 | |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 629 | static int bt_dev_init(struct net_device *dev) |
| 630 | { |
Eric Dumazet | d3fff6c | 2016-06-09 07:45:12 -0700 | [diff] [blame] | 631 | netdev_lockdep_set_classes(dev); |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 632 | |
| 633 | return 0; |
| 634 | } |
| 635 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 636 | static const struct net_device_ops netdev_ops = { |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 637 | .ndo_init = bt_dev_init, |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 638 | .ndo_start_xmit = bt_xmit, |
| 639 | }; |
| 640 | |
| 641 | static struct header_ops header_ops = { |
| 642 | .create = header_create, |
| 643 | }; |
| 644 | |
| 645 | static void netdev_setup(struct net_device *dev) |
| 646 | { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 647 | dev->hard_header_len = 0; |
| 648 | dev->needed_tailroom = 0; |
Jukka Rissanen | 156395c | 2014-09-29 16:37:26 +0300 | [diff] [blame] | 649 | dev->flags = IFF_RUNNING | IFF_POINTOPOINT | |
| 650 | IFF_MULTICAST; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 651 | dev->watchdog_timeo = 0; |
| 652 | |
| 653 | dev->netdev_ops = &netdev_ops; |
| 654 | dev->header_ops = &header_ops; |
| 655 | dev->destructor = free_netdev; |
| 656 | } |
| 657 | |
| 658 | static struct device_type bt_type = { |
| 659 | .name = "bluetooth", |
| 660 | }; |
| 661 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 662 | static void ifup(struct net_device *netdev) |
| 663 | { |
| 664 | int err; |
| 665 | |
| 666 | rtnl_lock(); |
| 667 | err = dev_open(netdev); |
| 668 | if (err < 0) |
| 669 | BT_INFO("iface %s cannot be opened (%d)", netdev->name, err); |
| 670 | rtnl_unlock(); |
| 671 | } |
| 672 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 673 | static void ifdown(struct net_device *netdev) |
| 674 | { |
| 675 | int err; |
| 676 | |
| 677 | rtnl_lock(); |
| 678 | err = dev_close(netdev); |
| 679 | if (err < 0) |
| 680 | BT_INFO("iface %s cannot be closed (%d)", netdev->name, err); |
| 681 | rtnl_unlock(); |
| 682 | } |
| 683 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 684 | static void do_notify_peers(struct work_struct *work) |
| 685 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 686 | struct lowpan_btle_dev *dev = container_of(work, struct lowpan_btle_dev, |
| 687 | notify_peers.work); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 688 | |
| 689 | netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */ |
| 690 | } |
| 691 | |
| 692 | static bool is_bt_6lowpan(struct hci_conn *hcon) |
| 693 | { |
| 694 | if (hcon->type != LE_LINK) |
| 695 | return false; |
| 696 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 697 | if (!enable_6lowpan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 698 | return false; |
| 699 | |
| 700 | return true; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 701 | } |
| 702 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 703 | static struct l2cap_chan *chan_create(void) |
| 704 | { |
| 705 | struct l2cap_chan *chan; |
| 706 | |
| 707 | chan = l2cap_chan_create(); |
| 708 | if (!chan) |
| 709 | return NULL; |
| 710 | |
| 711 | l2cap_chan_set_defaults(chan); |
| 712 | |
| 713 | chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; |
| 714 | chan->mode = L2CAP_MODE_LE_FLOWCTL; |
Johan Hedberg | 301de2c | 2015-10-06 13:03:19 +0300 | [diff] [blame] | 715 | chan->imtu = 1280; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 716 | |
| 717 | return chan; |
| 718 | } |
| 719 | |
Jukka Rissanen | b2799ce | 2014-09-08 12:11:44 +0300 | [diff] [blame] | 720 | static void set_ip_addr_bits(u8 addr_type, u8 *addr) |
| 721 | { |
| 722 | if (addr_type == BDADDR_LE_PUBLIC) |
| 723 | *addr |= 0x02; |
| 724 | else |
| 725 | *addr &= ~0x02; |
| 726 | } |
| 727 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 728 | static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan, |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 729 | struct lowpan_btle_dev *dev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 730 | { |
| 731 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 732 | |
| 733 | peer = kzalloc(sizeof(*peer), GFP_ATOMIC); |
| 734 | if (!peer) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 735 | return NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 736 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 737 | peer->chan = chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 738 | memset(&peer->peer_addr, 0, sizeof(struct in6_addr)); |
| 739 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame^] | 740 | baswap((void *)peer->lladdr, &chan->dst); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 741 | |
Luiz Augusto von Dentz | fa09ae6 | 2017-03-12 10:19:37 +0200 | [diff] [blame^] | 742 | lowpan_iphc_uncompress_eui48_lladdr(&peer->peer_addr, peer->lladdr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 743 | |
Jukka Rissanen | b2799ce | 2014-09-08 12:11:44 +0300 | [diff] [blame] | 744 | /* IPv6 address needs to have the U/L bit set properly so toggle |
| 745 | * it back here. |
| 746 | */ |
| 747 | set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8); |
| 748 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 749 | spin_lock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 750 | INIT_LIST_HEAD(&peer->list); |
| 751 | peer_add(dev, peer); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 752 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 753 | |
| 754 | /* Notifying peers about us needs to be done without locks held */ |
| 755 | INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers); |
| 756 | schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100)); |
| 757 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 758 | return peer->chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 759 | } |
| 760 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 761 | 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] | 762 | { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 763 | struct net_device *netdev; |
| 764 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 765 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 766 | netdev = alloc_netdev(LOWPAN_PRIV_SIZE(sizeof(struct lowpan_btle_dev)), |
Alexander Aring | b72f6f5 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 767 | IFACE_NAME_TEMPLATE, NET_NAME_UNKNOWN, |
| 768 | netdev_setup); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 769 | if (!netdev) |
| 770 | return -ENOMEM; |
| 771 | |
Patrik Flykt | c259d14 | 2017-03-12 10:19:33 +0200 | [diff] [blame] | 772 | netdev->addr_assign_type = NET_ADDR_PERM; |
| 773 | baswap((void *)netdev->dev_addr, &chan->src); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 774 | |
| 775 | netdev->netdev_ops = &netdev_ops; |
Glenn Ruben Bakke | fc84242 | 2015-06-17 07:32:25 -0700 | [diff] [blame] | 776 | SET_NETDEV_DEV(netdev, &chan->conn->hcon->hdev->dev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 777 | SET_NETDEV_DEVTYPE(netdev, &bt_type); |
| 778 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 779 | *dev = lowpan_btle_dev(netdev); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 780 | (*dev)->netdev = netdev; |
| 781 | (*dev)->hdev = chan->conn->hcon->hdev; |
| 782 | INIT_LIST_HEAD(&(*dev)->peers); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 783 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 784 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 785 | INIT_LIST_HEAD(&(*dev)->list); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 786 | list_add_rcu(&(*dev)->list, &bt_6lowpan_devices); |
| 787 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 788 | |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 789 | err = lowpan_register_netdev(netdev, LOWPAN_LLTYPE_BTLE); |
Alexander Aring | 5857d1d | 2015-07-30 09:40:53 +0200 | [diff] [blame] | 790 | if (err < 0) { |
| 791 | BT_INFO("register_netdev failed %d", err); |
| 792 | spin_lock(&devices_lock); |
| 793 | list_del_rcu(&(*dev)->list); |
| 794 | spin_unlock(&devices_lock); |
| 795 | free_netdev(netdev); |
| 796 | goto out; |
| 797 | } |
| 798 | |
| 799 | BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d", |
| 800 | netdev->ifindex, &chan->dst, chan->dst_type, |
| 801 | &chan->src, chan->src_type); |
| 802 | set_bit(__LINK_STATE_PRESENT, &netdev->state); |
| 803 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 804 | return 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 805 | |
| 806 | out: |
| 807 | return err; |
| 808 | } |
| 809 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 810 | static inline void chan_ready_cb(struct l2cap_chan *chan) |
| 811 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 812 | struct lowpan_btle_dev *dev; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 813 | |
| 814 | dev = lookup_dev(chan->conn); |
| 815 | |
| 816 | BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev); |
| 817 | |
| 818 | if (!dev) { |
| 819 | if (setup_netdev(chan, &dev) < 0) { |
| 820 | l2cap_chan_del(chan, -ENOENT); |
| 821 | return; |
| 822 | } |
| 823 | } |
| 824 | |
Jukka Rissanen | 18d93c1 | 2014-06-18 16:37:10 +0300 | [diff] [blame] | 825 | if (!try_module_get(THIS_MODULE)) |
| 826 | return; |
| 827 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 828 | add_peer_chan(chan, dev); |
| 829 | ifup(dev->netdev); |
| 830 | } |
| 831 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 832 | 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] | 833 | { |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 834 | struct l2cap_chan *chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 835 | |
Johan Hedberg | 630ef79 | 2015-10-06 13:03:22 +0300 | [diff] [blame] | 836 | chan = chan_create(); |
| 837 | if (!chan) |
| 838 | return NULL; |
| 839 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 840 | chan->ops = pchan->ops; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 841 | |
| 842 | BT_DBG("chan %p pchan %p", chan, pchan); |
| 843 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 844 | return chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 845 | } |
| 846 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 847 | static void delete_netdev(struct work_struct *work) |
| 848 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 849 | struct lowpan_btle_dev *entry = container_of(work, |
| 850 | struct lowpan_btle_dev, |
| 851 | delete_netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 852 | |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 853 | lowpan_unregister_netdev(entry->netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 854 | |
Glenn Ruben Bakke | 2ad88fb | 2015-06-17 07:32:26 -0700 | [diff] [blame] | 855 | /* The entry pointer is deleted by the netdev destructor. */ |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 856 | } |
| 857 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 858 | static void chan_close_cb(struct l2cap_chan *chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 859 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 860 | struct lowpan_btle_dev *entry; |
| 861 | struct lowpan_btle_dev *dev = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 862 | struct lowpan_peer *peer; |
| 863 | int err = -ENOENT; |
Glenn Ruben Bakke | f63666d2 | 2015-06-17 07:32:24 -0700 | [diff] [blame] | 864 | bool last = false, remove = true; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 865 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 866 | BT_DBG("chan %p conn %p", chan, chan->conn); |
| 867 | |
| 868 | if (chan->conn && chan->conn->hcon) { |
| 869 | if (!is_bt_6lowpan(chan->conn->hcon)) |
| 870 | return; |
| 871 | |
| 872 | /* If conn is set, then the netdev is also there and we should |
| 873 | * not remove it. |
| 874 | */ |
Glenn Ruben Bakke | f63666d2 | 2015-06-17 07:32:24 -0700 | [diff] [blame] | 875 | remove = false; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 876 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 877 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 878 | spin_lock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 879 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 880 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 881 | dev = lowpan_btle_dev(entry->netdev); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 882 | peer = __peer_lookup_chan(dev, chan); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 883 | if (peer) { |
| 884 | last = peer_del(dev, peer); |
| 885 | err = 0; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 886 | |
| 887 | BT_DBG("dev %p removing %speer %p", dev, |
| 888 | last ? "last " : "1 ", peer); |
| 889 | BT_DBG("chan %p orig refcnt %d", chan, |
Peter Zijlstra | 2c935bc | 2016-11-14 17:29:48 +0100 | [diff] [blame] | 890 | kref_read(&chan->kref)); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 891 | |
| 892 | l2cap_chan_put(chan); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 893 | break; |
| 894 | } |
| 895 | } |
| 896 | |
| 897 | if (!err && last && dev && !atomic_read(&dev->peer_count)) { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 898 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 899 | |
| 900 | cancel_delayed_work_sync(&dev->notify_peers); |
| 901 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 902 | ifdown(dev->netdev); |
| 903 | |
Glenn Ruben Bakke | f63666d2 | 2015-06-17 07:32:24 -0700 | [diff] [blame] | 904 | if (remove) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 905 | INIT_WORK(&entry->delete_netdev, delete_netdev); |
| 906 | schedule_work(&entry->delete_netdev); |
| 907 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 908 | } else { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 909 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 910 | } |
| 911 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 912 | return; |
| 913 | } |
| 914 | |
| 915 | static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err) |
| 916 | { |
| 917 | BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn, |
| 918 | state_to_string(state), err); |
| 919 | } |
| 920 | |
| 921 | static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan, |
| 922 | unsigned long hdr_len, |
| 923 | unsigned long len, int nb) |
| 924 | { |
| 925 | /* Note that we must allocate using GFP_ATOMIC here as |
| 926 | * this function is called originally from netdev hard xmit |
| 927 | * function in atomic context. |
| 928 | */ |
| 929 | return bt_skb_alloc(hdr_len + len, GFP_ATOMIC); |
| 930 | } |
| 931 | |
| 932 | static void chan_suspend_cb(struct l2cap_chan *chan) |
| 933 | { |
| 934 | struct sk_buff *skb = chan->data; |
| 935 | |
| 936 | BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); |
| 937 | |
Jukka Rissanen | 59790aa | 2014-09-29 10:55:46 +0300 | [diff] [blame] | 938 | if (!skb) |
| 939 | return; |
| 940 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 941 | lowpan_cb(skb)->status = -EAGAIN; |
| 942 | } |
| 943 | |
| 944 | static void chan_resume_cb(struct l2cap_chan *chan) |
| 945 | { |
| 946 | struct sk_buff *skb = chan->data; |
| 947 | |
| 948 | BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); |
| 949 | |
Jukka Rissanen | 59790aa | 2014-09-29 10:55:46 +0300 | [diff] [blame] | 950 | if (!skb) |
| 951 | return; |
| 952 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 953 | lowpan_cb(skb)->status = 0; |
| 954 | } |
| 955 | |
| 956 | static long chan_get_sndtimeo_cb(struct l2cap_chan *chan) |
| 957 | { |
Jukka Rissanen | 2ae50d8 | 2014-09-08 12:11:43 +0300 | [diff] [blame] | 958 | return L2CAP_CONN_TIMEOUT; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 959 | } |
| 960 | |
| 961 | static const struct l2cap_ops bt_6lowpan_chan_ops = { |
| 962 | .name = "L2CAP 6LoWPAN channel", |
| 963 | .new_connection = chan_new_conn_cb, |
| 964 | .recv = chan_recv_cb, |
| 965 | .close = chan_close_cb, |
| 966 | .state_change = chan_state_change_cb, |
| 967 | .ready = chan_ready_cb, |
| 968 | .resume = chan_resume_cb, |
| 969 | .suspend = chan_suspend_cb, |
| 970 | .get_sndtimeo = chan_get_sndtimeo_cb, |
| 971 | .alloc_skb = chan_alloc_skb_cb, |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 972 | |
| 973 | .teardown = l2cap_chan_no_teardown, |
| 974 | .defer = l2cap_chan_no_defer, |
| 975 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 976 | }; |
| 977 | |
| 978 | static inline __u8 bdaddr_type(__u8 type) |
| 979 | { |
| 980 | if (type == ADDR_LE_DEV_PUBLIC) |
| 981 | return BDADDR_LE_PUBLIC; |
| 982 | else |
| 983 | return BDADDR_LE_RANDOM; |
| 984 | } |
| 985 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 986 | static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type) |
| 987 | { |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 988 | struct l2cap_chan *chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 989 | int err; |
| 990 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 991 | chan = chan_create(); |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 992 | if (!chan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 993 | return -EINVAL; |
| 994 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 995 | chan->ops = &bt_6lowpan_chan_ops; |
| 996 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 997 | err = l2cap_chan_connect(chan, cpu_to_le16(L2CAP_PSM_IPSP), 0, |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 998 | addr, dst_type); |
| 999 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1000 | BT_DBG("chan %p err %d", chan, err); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1001 | if (err < 0) |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1002 | l2cap_chan_put(chan); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1003 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1004 | return err; |
| 1005 | } |
| 1006 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1007 | static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type) |
| 1008 | { |
| 1009 | struct lowpan_peer *peer; |
| 1010 | |
| 1011 | BT_DBG("conn %p dst type %d", conn, dst_type); |
| 1012 | |
| 1013 | peer = lookup_peer(conn); |
| 1014 | if (!peer) |
| 1015 | return -ENOENT; |
| 1016 | |
| 1017 | BT_DBG("peer %p chan %p", peer, peer->chan); |
| 1018 | |
| 1019 | l2cap_chan_close(peer->chan, ENOENT); |
| 1020 | |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | static struct l2cap_chan *bt_6lowpan_listen(void) |
| 1025 | { |
| 1026 | bdaddr_t *addr = BDADDR_ANY; |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1027 | struct l2cap_chan *chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1028 | int err; |
| 1029 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1030 | if (!enable_6lowpan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1031 | return NULL; |
| 1032 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 1033 | chan = chan_create(); |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1034 | if (!chan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1035 | return NULL; |
| 1036 | |
Johan Hedberg | 26d46df | 2015-10-06 13:03:24 +0300 | [diff] [blame] | 1037 | chan->ops = &bt_6lowpan_chan_ops; |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1038 | chan->state = BT_LISTEN; |
| 1039 | chan->src_type = BDADDR_LE_PUBLIC; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1040 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1041 | atomic_set(&chan->nesting, L2CAP_NESTING_PARENT); |
Johan Hedberg | 2773b02 | 2014-11-13 09:46:05 +0200 | [diff] [blame] | 1042 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1043 | BT_DBG("chan %p src type %d", chan, chan->src_type); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1044 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1045 | err = l2cap_add_psm(chan, addr, cpu_to_le16(L2CAP_PSM_IPSP)); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1046 | if (err) { |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1047 | l2cap_chan_put(chan); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1048 | BT_ERR("psm cannot be added err %d", err); |
| 1049 | return NULL; |
| 1050 | } |
| 1051 | |
Johan Hedberg | 0cd088f | 2015-10-06 13:03:23 +0300 | [diff] [blame] | 1052 | return chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1053 | } |
| 1054 | |
| 1055 | static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type, |
| 1056 | struct l2cap_conn **conn) |
| 1057 | { |
| 1058 | struct hci_conn *hcon; |
| 1059 | struct hci_dev *hdev; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1060 | int n; |
| 1061 | |
| 1062 | n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu", |
| 1063 | &addr->b[5], &addr->b[4], &addr->b[3], |
| 1064 | &addr->b[2], &addr->b[1], &addr->b[0], |
| 1065 | addr_type); |
| 1066 | |
| 1067 | if (n < 7) |
| 1068 | return -EINVAL; |
| 1069 | |
Johan Hedberg | 39385cb | 2016-11-12 17:03:07 +0200 | [diff] [blame] | 1070 | /* The LE_PUBLIC address type is ignored because of BDADDR_ANY */ |
| 1071 | hdev = hci_get_route(addr, BDADDR_ANY, BDADDR_LE_PUBLIC); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1072 | if (!hdev) |
| 1073 | return -ENOENT; |
| 1074 | |
| 1075 | hci_dev_lock(hdev); |
Johan Hedberg | f5ad4ff | 2015-10-21 18:03:02 +0300 | [diff] [blame] | 1076 | hcon = hci_conn_hash_lookup_le(hdev, addr, *addr_type); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1077 | hci_dev_unlock(hdev); |
| 1078 | |
| 1079 | if (!hcon) |
| 1080 | return -ENOENT; |
| 1081 | |
| 1082 | *conn = (struct l2cap_conn *)hcon->l2cap_data; |
| 1083 | |
| 1084 | BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type); |
| 1085 | |
| 1086 | return 0; |
| 1087 | } |
| 1088 | |
| 1089 | static void disconnect_all_peers(void) |
| 1090 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1091 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1092 | struct lowpan_peer *peer, *tmp_peer, *new_peer; |
| 1093 | struct list_head peers; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1094 | |
| 1095 | INIT_LIST_HEAD(&peers); |
| 1096 | |
| 1097 | /* We make a separate list of peers as the close_cb() will |
| 1098 | * modify the device peers list so it is better not to mess |
| 1099 | * with the same list at the same time. |
| 1100 | */ |
| 1101 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1102 | rcu_read_lock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1103 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1104 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 1105 | list_for_each_entry_rcu(peer, &entry->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1106 | new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC); |
| 1107 | if (!new_peer) |
| 1108 | break; |
| 1109 | |
| 1110 | new_peer->chan = peer->chan; |
| 1111 | INIT_LIST_HEAD(&new_peer->list); |
| 1112 | |
| 1113 | list_add(&new_peer->list, &peers); |
| 1114 | } |
| 1115 | } |
| 1116 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1117 | rcu_read_unlock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1118 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1119 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1120 | list_for_each_entry_safe(peer, tmp_peer, &peers, list) { |
| 1121 | l2cap_chan_close(peer->chan, ENOENT); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1122 | |
| 1123 | list_del_rcu(&peer->list); |
Johan Hedberg | 4e79022 | 2014-11-11 14:16:29 +0200 | [diff] [blame] | 1124 | kfree_rcu(peer, rcu); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1125 | } |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1126 | spin_unlock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1127 | } |
| 1128 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1129 | struct set_enable { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1130 | struct work_struct work; |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1131 | bool flag; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1132 | }; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1133 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1134 | static void do_enable_set(struct work_struct *work) |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1135 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1136 | struct set_enable *set_enable = container_of(work, |
| 1137 | struct set_enable, work); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1138 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1139 | if (!set_enable->flag || enable_6lowpan != set_enable->flag) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1140 | /* Disconnect existing connections if 6lowpan is |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1141 | * disabled |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1142 | */ |
| 1143 | disconnect_all_peers(); |
| 1144 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1145 | enable_6lowpan = set_enable->flag; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1146 | |
| 1147 | if (listen_chan) { |
| 1148 | l2cap_chan_close(listen_chan, 0); |
| 1149 | l2cap_chan_put(listen_chan); |
| 1150 | } |
| 1151 | |
| 1152 | listen_chan = bt_6lowpan_listen(); |
| 1153 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1154 | kfree(set_enable); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1155 | } |
| 1156 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1157 | static int lowpan_enable_set(void *data, u64 val) |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1158 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1159 | struct set_enable *set_enable; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1160 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1161 | set_enable = kzalloc(sizeof(*set_enable), GFP_KERNEL); |
| 1162 | if (!set_enable) |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1163 | return -ENOMEM; |
| 1164 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1165 | set_enable->flag = !!val; |
| 1166 | INIT_WORK(&set_enable->work, do_enable_set); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1167 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1168 | schedule_work(&set_enable->work); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1169 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1170 | return 0; |
| 1171 | } |
| 1172 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1173 | static int lowpan_enable_get(void *data, u64 *val) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1174 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1175 | *val = enable_6lowpan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1176 | return 0; |
| 1177 | } |
| 1178 | |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1179 | DEFINE_SIMPLE_ATTRIBUTE(lowpan_enable_fops, lowpan_enable_get, |
| 1180 | lowpan_enable_set, "%llu\n"); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1181 | |
| 1182 | static ssize_t lowpan_control_write(struct file *fp, |
| 1183 | const char __user *user_buffer, |
| 1184 | size_t count, |
| 1185 | loff_t *position) |
| 1186 | { |
| 1187 | char buf[32]; |
| 1188 | size_t buf_size = min(count, sizeof(buf) - 1); |
| 1189 | int ret; |
| 1190 | bdaddr_t addr; |
| 1191 | u8 addr_type; |
| 1192 | struct l2cap_conn *conn = NULL; |
| 1193 | |
| 1194 | if (copy_from_user(buf, user_buffer, buf_size)) |
| 1195 | return -EFAULT; |
| 1196 | |
| 1197 | buf[buf_size] = '\0'; |
| 1198 | |
| 1199 | if (memcmp(buf, "connect ", 8) == 0) { |
| 1200 | ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn); |
| 1201 | if (ret == -EINVAL) |
| 1202 | return ret; |
| 1203 | |
| 1204 | if (listen_chan) { |
| 1205 | l2cap_chan_close(listen_chan, 0); |
| 1206 | l2cap_chan_put(listen_chan); |
| 1207 | listen_chan = NULL; |
| 1208 | } |
| 1209 | |
| 1210 | if (conn) { |
| 1211 | struct lowpan_peer *peer; |
| 1212 | |
| 1213 | if (!is_bt_6lowpan(conn->hcon)) |
| 1214 | return -EINVAL; |
| 1215 | |
| 1216 | peer = lookup_peer(conn); |
| 1217 | if (peer) { |
| 1218 | BT_DBG("6LoWPAN connection already exists"); |
| 1219 | return -EALREADY; |
| 1220 | } |
| 1221 | |
| 1222 | BT_DBG("conn %p dst %pMR type %d user %d", conn, |
| 1223 | &conn->hcon->dst, conn->hcon->dst_type, |
| 1224 | addr_type); |
| 1225 | } |
| 1226 | |
| 1227 | ret = bt_6lowpan_connect(&addr, addr_type); |
| 1228 | if (ret < 0) |
| 1229 | return ret; |
| 1230 | |
| 1231 | return count; |
| 1232 | } |
| 1233 | |
| 1234 | if (memcmp(buf, "disconnect ", 11) == 0) { |
| 1235 | ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn); |
| 1236 | if (ret < 0) |
| 1237 | return ret; |
| 1238 | |
| 1239 | ret = bt_6lowpan_disconnect(conn, addr_type); |
| 1240 | if (ret < 0) |
| 1241 | return ret; |
| 1242 | |
| 1243 | return count; |
| 1244 | } |
| 1245 | |
| 1246 | return count; |
| 1247 | } |
| 1248 | |
| 1249 | static int lowpan_control_show(struct seq_file *f, void *ptr) |
| 1250 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1251 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1252 | struct lowpan_peer *peer; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1253 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1254 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1255 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1256 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
| 1257 | list_for_each_entry(peer, &entry->peers, list) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1258 | seq_printf(f, "%pMR (type %u)\n", |
| 1259 | &peer->chan->dst, peer->chan->dst_type); |
| 1260 | } |
| 1261 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1262 | spin_unlock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1263 | |
| 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static int lowpan_control_open(struct inode *inode, struct file *file) |
| 1268 | { |
| 1269 | return single_open(file, lowpan_control_show, inode->i_private); |
| 1270 | } |
| 1271 | |
| 1272 | static const struct file_operations lowpan_control_fops = { |
| 1273 | .open = lowpan_control_open, |
| 1274 | .read = seq_read, |
| 1275 | .write = lowpan_control_write, |
| 1276 | .llseek = seq_lseek, |
| 1277 | .release = single_release, |
| 1278 | }; |
| 1279 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1280 | static void disconnect_devices(void) |
| 1281 | { |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1282 | struct lowpan_btle_dev *entry, *tmp, *new_dev; |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1283 | struct list_head devices; |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1284 | |
| 1285 | INIT_LIST_HEAD(&devices); |
| 1286 | |
| 1287 | /* We make a separate list of devices because the unregister_netdev() |
| 1288 | * will call device_event() which will also want to modify the same |
| 1289 | * devices list. |
| 1290 | */ |
| 1291 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1292 | rcu_read_lock(); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1293 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1294 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1295 | new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC); |
| 1296 | if (!new_dev) |
| 1297 | break; |
| 1298 | |
| 1299 | new_dev->netdev = entry->netdev; |
| 1300 | INIT_LIST_HEAD(&new_dev->list); |
| 1301 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1302 | list_add_rcu(&new_dev->list, &devices); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1303 | } |
| 1304 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1305 | rcu_read_unlock(); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1306 | |
Dan Carpenter | daac197 | 2014-10-29 19:10:57 +0300 | [diff] [blame] | 1307 | list_for_each_entry_safe(entry, tmp, &devices, list) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1308 | ifdown(entry->netdev); |
| 1309 | BT_DBG("Unregistering netdev %s %p", |
| 1310 | entry->netdev->name, entry->netdev); |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 1311 | lowpan_unregister_netdev(entry->netdev); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1312 | kfree(entry); |
| 1313 | } |
| 1314 | } |
| 1315 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1316 | static int device_event(struct notifier_block *unused, |
| 1317 | unsigned long event, void *ptr) |
| 1318 | { |
| 1319 | struct net_device *netdev = netdev_notifier_info_to_dev(ptr); |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 1320 | struct lowpan_btle_dev *entry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1321 | |
| 1322 | if (netdev->type != ARPHRD_6LOWPAN) |
| 1323 | return NOTIFY_DONE; |
| 1324 | |
| 1325 | switch (event) { |
| 1326 | case NETDEV_UNREGISTER: |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1327 | spin_lock(&devices_lock); |
| 1328 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1329 | if (entry->netdev == netdev) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1330 | BT_DBG("Unregistered netdev %s %p", |
| 1331 | netdev->name, netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1332 | list_del(&entry->list); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1333 | break; |
| 1334 | } |
| 1335 | } |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1336 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1337 | break; |
| 1338 | } |
| 1339 | |
| 1340 | return NOTIFY_DONE; |
| 1341 | } |
| 1342 | |
| 1343 | static struct notifier_block bt_6lowpan_dev_notifier = { |
| 1344 | .notifier_call = device_event, |
| 1345 | }; |
| 1346 | |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1347 | static int __init bt_6lowpan_init(void) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1348 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1349 | lowpan_enable_debugfs = debugfs_create_file("6lowpan_enable", 0644, |
| 1350 | bt_debugfs, NULL, |
| 1351 | &lowpan_enable_fops); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1352 | lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644, |
| 1353 | bt_debugfs, NULL, |
| 1354 | &lowpan_control_fops); |
| 1355 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1356 | return register_netdevice_notifier(&bt_6lowpan_dev_notifier); |
| 1357 | } |
| 1358 | |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1359 | static void __exit bt_6lowpan_exit(void) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1360 | { |
Jukka Rissanen | 7b2ed60 | 2015-01-08 17:00:55 +0200 | [diff] [blame] | 1361 | debugfs_remove(lowpan_enable_debugfs); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1362 | debugfs_remove(lowpan_control_debugfs); |
| 1363 | |
| 1364 | if (listen_chan) { |
| 1365 | l2cap_chan_close(listen_chan, 0); |
| 1366 | l2cap_chan_put(listen_chan); |
| 1367 | } |
| 1368 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1369 | disconnect_devices(); |
| 1370 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1371 | unregister_netdevice_notifier(&bt_6lowpan_dev_notifier); |
| 1372 | } |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1373 | |
| 1374 | module_init(bt_6lowpan_init); |
| 1375 | module_exit(bt_6lowpan_exit); |
| 1376 | |
| 1377 | MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>"); |
| 1378 | MODULE_DESCRIPTION("Bluetooth 6LoWPAN"); |
| 1379 | MODULE_VERSION(VERSION); |
| 1380 | MODULE_LICENSE("GPL"); |