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 | |
| 24 | #include <net/af_ieee802154.h> /* to get the address type */ |
| 25 | |
| 26 | #include <net/bluetooth/bluetooth.h> |
| 27 | #include <net/bluetooth/hci_core.h> |
| 28 | #include <net/bluetooth/l2cap.h> |
| 29 | |
Alexander Aring | cefc8c8 | 2014-03-05 14:29:05 +0100 | [diff] [blame] | 30 | #include <net/6lowpan.h> /* for the compression support */ |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 31 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 32 | #define VERSION "0.1" |
| 33 | |
| 34 | static struct dentry *lowpan_psm_debugfs; |
| 35 | static struct dentry *lowpan_control_debugfs; |
| 36 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 37 | #define IFACE_NAME_TEMPLATE "bt%d" |
| 38 | #define EUI64_ADDR_LEN 8 |
| 39 | |
| 40 | struct skb_cb { |
| 41 | struct in6_addr addr; |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 42 | struct in6_addr gw; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 43 | struct l2cap_chan *chan; |
| 44 | int status; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 45 | }; |
| 46 | #define lowpan_cb(skb) ((struct skb_cb *)((skb)->cb)) |
| 47 | |
| 48 | /* The devices list contains those devices that we are acting |
| 49 | * as a proxy. The BT 6LoWPAN device is a virtual device that |
| 50 | * connects to the Bluetooth LE device. The real connection to |
| 51 | * BT device is done via l2cap layer. There exists one |
| 52 | * virtual device / one BT 6LoWPAN network (=hciX device). |
| 53 | * The list contains struct lowpan_dev elements. |
| 54 | */ |
| 55 | static LIST_HEAD(bt_6lowpan_devices); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 56 | static DEFINE_SPINLOCK(devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 57 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 58 | /* If psm is set to 0 (default value), then 6lowpan is disabled. |
| 59 | * Other values are used to indicate a Protocol Service Multiplexer |
| 60 | * value for 6lowpan. |
| 61 | */ |
| 62 | static u16 psm_6lowpan; |
| 63 | |
| 64 | /* We are listening incoming connections via this channel |
| 65 | */ |
| 66 | static struct l2cap_chan *listen_chan; |
| 67 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 68 | struct lowpan_peer { |
| 69 | struct list_head list; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 70 | struct rcu_head rcu; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 71 | struct l2cap_chan *chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 72 | |
| 73 | /* peer addresses in various formats */ |
| 74 | unsigned char eui64_addr[EUI64_ADDR_LEN]; |
| 75 | struct in6_addr peer_addr; |
| 76 | }; |
| 77 | |
| 78 | struct lowpan_dev { |
| 79 | struct list_head list; |
| 80 | |
| 81 | struct hci_dev *hdev; |
| 82 | struct net_device *netdev; |
| 83 | struct list_head peers; |
| 84 | atomic_t peer_count; /* number of items in peers list */ |
| 85 | |
| 86 | struct work_struct delete_netdev; |
| 87 | struct delayed_work notify_peers; |
| 88 | }; |
| 89 | |
| 90 | static inline struct lowpan_dev *lowpan_dev(const struct net_device *netdev) |
| 91 | { |
| 92 | return netdev_priv(netdev); |
| 93 | } |
| 94 | |
| 95 | static inline void peer_add(struct lowpan_dev *dev, struct lowpan_peer *peer) |
| 96 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 97 | list_add_rcu(&peer->list, &dev->peers); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 98 | atomic_inc(&dev->peer_count); |
| 99 | } |
| 100 | |
| 101 | static inline bool peer_del(struct lowpan_dev *dev, struct lowpan_peer *peer) |
| 102 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 103 | list_del_rcu(&peer->list); |
Johan Hedberg | 4e79022 | 2014-11-11 14:16:29 +0200 | [diff] [blame] | 104 | kfree_rcu(peer, rcu); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 105 | |
Jukka Rissanen | 18d93c1 | 2014-06-18 16:37:10 +0300 | [diff] [blame] | 106 | module_put(THIS_MODULE); |
| 107 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 108 | if (atomic_dec_and_test(&dev->peer_count)) { |
| 109 | BT_DBG("last peer"); |
| 110 | return true; |
| 111 | } |
| 112 | |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | static inline struct lowpan_peer *peer_lookup_ba(struct lowpan_dev *dev, |
| 117 | bdaddr_t *ba, __u8 type) |
| 118 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 119 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 120 | |
| 121 | BT_DBG("peers %d addr %pMR type %d", atomic_read(&dev->peer_count), |
| 122 | ba, type); |
| 123 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 124 | rcu_read_lock(); |
| 125 | |
| 126 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 127 | BT_DBG("dst addr %pMR dst type %d", |
| 128 | &peer->chan->dst, peer->chan->dst_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 129 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 130 | if (bacmp(&peer->chan->dst, ba)) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 131 | continue; |
| 132 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 133 | if (type == peer->chan->dst_type) { |
| 134 | rcu_read_unlock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 135 | return peer; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 136 | } |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 137 | } |
| 138 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 139 | rcu_read_unlock(); |
| 140 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| 143 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 144 | static inline struct lowpan_peer *__peer_lookup_chan(struct lowpan_dev *dev, |
| 145 | struct l2cap_chan *chan) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 146 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 147 | struct lowpan_peer *peer; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 148 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 149 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 150 | if (peer->chan == chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 151 | return peer; |
| 152 | } |
| 153 | |
| 154 | return NULL; |
| 155 | } |
| 156 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 157 | static inline struct lowpan_peer *__peer_lookup_conn(struct lowpan_dev *dev, |
| 158 | struct l2cap_conn *conn) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 159 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 160 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 161 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 162 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 163 | if (peer->chan->conn == conn) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 164 | return peer; |
| 165 | } |
| 166 | |
| 167 | return NULL; |
| 168 | } |
| 169 | |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 170 | static inline struct lowpan_peer *peer_lookup_dst(struct lowpan_dev *dev, |
| 171 | struct in6_addr *daddr, |
| 172 | struct sk_buff *skb) |
| 173 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 174 | struct lowpan_peer *peer; |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 175 | struct in6_addr *nexthop; |
| 176 | struct rt6_info *rt = (struct rt6_info *)skb_dst(skb); |
| 177 | int count = atomic_read(&dev->peer_count); |
| 178 | |
| 179 | BT_DBG("peers %d addr %pI6c rt %p", count, daddr, rt); |
| 180 | |
| 181 | /* If we have multiple 6lowpan peers, then check where we should |
| 182 | * send the packet. If only one peer exists, then we can send the |
| 183 | * packet right away. |
| 184 | */ |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 185 | if (count == 1) { |
| 186 | rcu_read_lock(); |
| 187 | peer = list_first_or_null_rcu(&dev->peers, struct lowpan_peer, |
| 188 | list); |
| 189 | rcu_read_unlock(); |
| 190 | return peer; |
| 191 | } |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 192 | |
| 193 | if (!rt) { |
| 194 | nexthop = &lowpan_cb(skb)->gw; |
| 195 | |
| 196 | if (ipv6_addr_any(nexthop)) |
| 197 | return NULL; |
| 198 | } else { |
| 199 | nexthop = rt6_nexthop(rt); |
| 200 | |
| 201 | /* We need to remember the address because it is needed |
| 202 | * by bt_xmit() when sending the packet. In bt_xmit(), the |
| 203 | * destination routing info is not set. |
| 204 | */ |
| 205 | memcpy(&lowpan_cb(skb)->gw, nexthop, sizeof(struct in6_addr)); |
| 206 | } |
| 207 | |
| 208 | BT_DBG("gw %pI6c", nexthop); |
| 209 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 210 | rcu_read_lock(); |
| 211 | |
| 212 | list_for_each_entry_rcu(peer, &dev->peers, list) { |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 213 | BT_DBG("dst addr %pMR dst type %d ip %pI6c", |
| 214 | &peer->chan->dst, peer->chan->dst_type, |
| 215 | &peer->peer_addr); |
| 216 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 217 | if (!ipv6_addr_cmp(&peer->peer_addr, nexthop)) { |
| 218 | rcu_read_unlock(); |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 219 | return peer; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 220 | } |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 221 | } |
| 222 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 223 | rcu_read_unlock(); |
| 224 | |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 225 | return NULL; |
| 226 | } |
| 227 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 228 | static struct lowpan_peer *lookup_peer(struct l2cap_conn *conn) |
| 229 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 230 | struct lowpan_dev *entry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 231 | struct lowpan_peer *peer = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 232 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 233 | rcu_read_lock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 234 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 235 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 236 | peer = __peer_lookup_conn(entry, conn); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 237 | if (peer) |
| 238 | break; |
| 239 | } |
| 240 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 241 | rcu_read_unlock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 242 | |
| 243 | return peer; |
| 244 | } |
| 245 | |
| 246 | static struct lowpan_dev *lookup_dev(struct l2cap_conn *conn) |
| 247 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 248 | struct lowpan_dev *entry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 249 | struct lowpan_dev *dev = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 250 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 251 | rcu_read_lock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 252 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 253 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 254 | if (conn->hcon->hdev == entry->hdev) { |
| 255 | dev = entry; |
| 256 | break; |
| 257 | } |
| 258 | } |
| 259 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 260 | rcu_read_unlock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 261 | |
| 262 | return dev; |
| 263 | } |
| 264 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 265 | static int give_skb_to_upper(struct sk_buff *skb, struct net_device *dev) |
| 266 | { |
| 267 | struct sk_buff *skb_cp; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 268 | |
| 269 | skb_cp = skb_copy(skb, GFP_ATOMIC); |
| 270 | if (!skb_cp) |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 271 | return NET_RX_DROP; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 272 | |
Li RongQing | 4456c50 | 2014-10-16 10:21:55 +0800 | [diff] [blame] | 273 | return netif_rx(skb_cp); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 274 | } |
| 275 | |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 276 | static int iphc_decompress(struct sk_buff *skb, struct net_device *netdev, |
| 277 | struct l2cap_chan *chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 278 | { |
| 279 | const u8 *saddr, *daddr; |
| 280 | u8 iphc0, iphc1; |
| 281 | struct lowpan_dev *dev; |
| 282 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 283 | |
| 284 | dev = lowpan_dev(netdev); |
| 285 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 286 | rcu_read_lock(); |
| 287 | peer = __peer_lookup_chan(dev, chan); |
| 288 | rcu_read_unlock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 289 | if (!peer) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 290 | return -EINVAL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 291 | |
| 292 | saddr = peer->eui64_addr; |
| 293 | daddr = dev->netdev->dev_addr; |
| 294 | |
| 295 | /* at least two bytes will be used for the encoding */ |
| 296 | if (skb->len < 2) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 297 | return -EINVAL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 298 | |
| 299 | if (lowpan_fetch_skb_u8(skb, &iphc0)) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 300 | return -EINVAL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 301 | |
| 302 | if (lowpan_fetch_skb_u8(skb, &iphc1)) |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 303 | return -EINVAL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 304 | |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 305 | return lowpan_header_decompress(skb, netdev, |
| 306 | saddr, IEEE802154_ADDR_LONG, |
| 307 | EUI64_ADDR_LEN, daddr, |
| 308 | IEEE802154_ADDR_LONG, EUI64_ADDR_LEN, |
| 309 | iphc0, iphc1); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 310 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 311 | } |
| 312 | |
| 313 | static int recv_pkt(struct sk_buff *skb, struct net_device *dev, |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 314 | struct l2cap_chan *chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 315 | { |
| 316 | struct sk_buff *local_skb; |
| 317 | int ret; |
| 318 | |
| 319 | if (!netif_running(dev)) |
| 320 | goto drop; |
| 321 | |
| 322 | if (dev->type != ARPHRD_6LOWPAN) |
| 323 | goto drop; |
| 324 | |
Martin Townsend | 11e3ff7 | 2014-10-13 11:00:56 +0100 | [diff] [blame] | 325 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 326 | if (!skb) |
| 327 | goto drop; |
| 328 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 329 | /* check that it's our buffer */ |
| 330 | if (skb->data[0] == LOWPAN_DISPATCH_IPV6) { |
| 331 | /* Copy the packet so that the IPv6 header is |
| 332 | * properly aligned. |
| 333 | */ |
| 334 | local_skb = skb_copy_expand(skb, NET_SKB_PAD - 1, |
| 335 | skb_tailroom(skb), GFP_ATOMIC); |
| 336 | if (!local_skb) |
| 337 | goto drop; |
| 338 | |
| 339 | local_skb->protocol = htons(ETH_P_IPV6); |
| 340 | local_skb->pkt_type = PACKET_HOST; |
| 341 | |
| 342 | skb_reset_network_header(local_skb); |
| 343 | skb_set_transport_header(local_skb, sizeof(struct ipv6hdr)); |
| 344 | |
| 345 | if (give_skb_to_upper(local_skb, dev) != NET_RX_SUCCESS) { |
| 346 | kfree_skb(local_skb); |
| 347 | goto drop; |
| 348 | } |
| 349 | |
| 350 | dev->stats.rx_bytes += skb->len; |
| 351 | dev->stats.rx_packets++; |
| 352 | |
Martin Townsend | 3c400b8 | 2014-10-23 15:40:55 +0100 | [diff] [blame] | 353 | consume_skb(local_skb); |
| 354 | consume_skb(skb); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 355 | } else { |
| 356 | switch (skb->data[0] & 0xe0) { |
| 357 | case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */ |
| 358 | local_skb = skb_clone(skb, GFP_ATOMIC); |
| 359 | if (!local_skb) |
| 360 | goto drop; |
| 361 | |
Martin Townsend | 0114123 | 2014-10-23 15:40:56 +0100 | [diff] [blame] | 362 | ret = iphc_decompress(local_skb, dev, chan); |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 363 | if (ret < 0) { |
| 364 | kfree_skb(local_skb); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 365 | goto drop; |
Martin Townsend | 56b2c3e | 2014-11-06 19:15:13 +0000 | [diff] [blame] | 366 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 367 | |
Martin Townsend | f8b3617 | 2014-10-23 15:40:53 +0100 | [diff] [blame] | 368 | local_skb->protocol = htons(ETH_P_IPV6); |
| 369 | local_skb->pkt_type = PACKET_HOST; |
| 370 | local_skb->dev = dev; |
| 371 | |
| 372 | if (give_skb_to_upper(local_skb, dev) |
| 373 | != NET_RX_SUCCESS) { |
| 374 | kfree_skb(local_skb); |
| 375 | goto drop; |
| 376 | } |
| 377 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 378 | dev->stats.rx_bytes += skb->len; |
| 379 | dev->stats.rx_packets++; |
| 380 | |
Martin Townsend | 3c400b8 | 2014-10-23 15:40:55 +0100 | [diff] [blame] | 381 | consume_skb(local_skb); |
| 382 | consume_skb(skb); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 383 | break; |
| 384 | default: |
| 385 | break; |
| 386 | } |
| 387 | } |
| 388 | |
| 389 | return NET_RX_SUCCESS; |
| 390 | |
| 391 | drop: |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 392 | dev->stats.rx_dropped++; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 393 | kfree_skb(skb); |
| 394 | return NET_RX_DROP; |
| 395 | } |
| 396 | |
| 397 | /* Packet from BT LE device */ |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 398 | 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] | 399 | { |
| 400 | struct lowpan_dev *dev; |
| 401 | struct lowpan_peer *peer; |
| 402 | int err; |
| 403 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 404 | peer = lookup_peer(chan->conn); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 405 | if (!peer) |
| 406 | return -ENOENT; |
| 407 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 408 | dev = lookup_dev(chan->conn); |
Johan Hedberg | 30d3db4 | 2013-12-12 09:53:21 +0200 | [diff] [blame] | 409 | if (!dev || !dev->netdev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 410 | return -ENOENT; |
| 411 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 412 | err = recv_pkt(skb, dev->netdev, chan); |
| 413 | if (err) { |
| 414 | BT_DBG("recv pkt %d", err); |
| 415 | err = -EAGAIN; |
| 416 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 417 | |
| 418 | return err; |
| 419 | } |
| 420 | |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 421 | static u8 get_addr_type_from_eui64(u8 byte) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 422 | { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 423 | /* Is universal(0) or local(1) bit */ |
| 424 | return ((byte & 0x02) ? BDADDR_LE_RANDOM : BDADDR_LE_PUBLIC); |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | static void copy_to_bdaddr(struct in6_addr *ip6_daddr, bdaddr_t *addr) |
| 428 | { |
| 429 | u8 *eui64 = ip6_daddr->s6_addr + 8; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 430 | |
| 431 | addr->b[0] = eui64[7]; |
| 432 | addr->b[1] = eui64[6]; |
| 433 | addr->b[2] = eui64[5]; |
| 434 | addr->b[3] = eui64[2]; |
| 435 | addr->b[4] = eui64[1]; |
| 436 | addr->b[5] = eui64[0]; |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 437 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 438 | |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 439 | static void convert_dest_bdaddr(struct in6_addr *ip6_daddr, |
| 440 | bdaddr_t *addr, u8 *addr_type) |
| 441 | { |
| 442 | copy_to_bdaddr(ip6_daddr, addr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 443 | |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 444 | /* We need to toggle the U/L bit that we got from IPv6 address |
| 445 | * so that we get the proper address and type of the BD address. |
| 446 | */ |
| 447 | addr->b[5] ^= 0x02; |
| 448 | |
| 449 | *addr_type = get_addr_type_from_eui64(addr->b[5]); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 450 | } |
| 451 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 452 | static int setup_header(struct sk_buff *skb, struct net_device *netdev, |
| 453 | bdaddr_t *peer_addr, u8 *peer_addr_type) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 454 | { |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 455 | struct in6_addr ipv6_daddr; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 456 | struct lowpan_dev *dev; |
| 457 | struct lowpan_peer *peer; |
| 458 | bdaddr_t addr, *any = BDADDR_ANY; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 459 | u8 *daddr = any->b; |
| 460 | int err, status = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 461 | |
| 462 | dev = lowpan_dev(netdev); |
| 463 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 464 | memcpy(&ipv6_daddr, &lowpan_cb(skb)->addr, sizeof(ipv6_daddr)); |
| 465 | |
| 466 | if (ipv6_addr_is_multicast(&ipv6_daddr)) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 467 | lowpan_cb(skb)->chan = NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 468 | } else { |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 469 | u8 addr_type; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 470 | |
| 471 | /* Get destination BT device from skb. |
| 472 | * If there is no such peer then discard the packet. |
| 473 | */ |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 474 | convert_dest_bdaddr(&ipv6_daddr, &addr, &addr_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 475 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 476 | BT_DBG("dest addr %pMR type %d IP %pI6c", &addr, |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 477 | addr_type, &ipv6_daddr); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 478 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 479 | peer = peer_lookup_ba(dev, &addr, addr_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 480 | if (!peer) { |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 481 | /* The packet might be sent to 6lowpan interface |
| 482 | * because of routing (either via default route |
| 483 | * or user set route) so get peer according to |
| 484 | * the destination address. |
| 485 | */ |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 486 | peer = peer_lookup_dst(dev, &ipv6_daddr, skb); |
Jukka Rissanen | 39e90c7 | 2014-09-08 12:11:45 +0300 | [diff] [blame] | 487 | if (!peer) { |
| 488 | BT_DBG("no such peer %pMR found", &addr); |
| 489 | return -ENOENT; |
| 490 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | daddr = peer->eui64_addr; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 494 | *peer_addr = addr; |
| 495 | *peer_addr_type = addr_type; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 496 | lowpan_cb(skb)->chan = peer->chan; |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 497 | |
| 498 | status = 1; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 499 | } |
| 500 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 501 | lowpan_header_compress(skb, netdev, ETH_P_IPV6, daddr, |
| 502 | dev->netdev->dev_addr, skb->len); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 503 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 504 | err = dev_hard_header(skb, netdev, ETH_P_IPV6, NULL, NULL, 0); |
| 505 | if (err < 0) |
| 506 | return err; |
| 507 | |
| 508 | return status; |
| 509 | } |
| 510 | |
| 511 | static int header_create(struct sk_buff *skb, struct net_device *netdev, |
| 512 | unsigned short type, const void *_daddr, |
| 513 | const void *_saddr, unsigned int len) |
| 514 | { |
| 515 | struct ipv6hdr *hdr; |
| 516 | |
| 517 | if (type != ETH_P_IPV6) |
| 518 | return -EINVAL; |
| 519 | |
| 520 | hdr = ipv6_hdr(skb); |
| 521 | |
| 522 | memcpy(&lowpan_cb(skb)->addr, &hdr->daddr, sizeof(struct in6_addr)); |
| 523 | |
| 524 | return 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* Packet to BT LE device */ |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 528 | static int send_pkt(struct l2cap_chan *chan, struct sk_buff *skb, |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 529 | struct net_device *netdev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 530 | { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 531 | struct msghdr msg; |
| 532 | struct kvec iv; |
| 533 | int err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 534 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 535 | /* 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] | 536 | * we run out of credits. |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 537 | */ |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 538 | chan->data = skb; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 539 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 540 | iv.iov_base = skb->data; |
| 541 | iv.iov_len = skb->len; |
| 542 | |
Al Viro | c0371da | 2014-11-24 10:42:55 -0500 | [diff] [blame^] | 543 | memset(&msg, 0, sizeof(msg)); |
| 544 | iov_iter_init(&msg.msg_iter, WRITE, (struct iovec *) &iv, 1, skb->len); |
| 545 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 546 | err = l2cap_chan_send(chan, &msg, skb->len); |
| 547 | if (err > 0) { |
| 548 | netdev->stats.tx_bytes += err; |
| 549 | netdev->stats.tx_packets++; |
| 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | if (!err) |
| 554 | err = lowpan_cb(skb)->status; |
| 555 | |
| 556 | if (err < 0) { |
| 557 | if (err == -EAGAIN) |
| 558 | netdev->stats.tx_dropped++; |
| 559 | else |
| 560 | netdev->stats.tx_errors++; |
| 561 | } |
| 562 | |
| 563 | return err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 564 | } |
| 565 | |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 566 | 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] | 567 | { |
| 568 | struct sk_buff *local_skb; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 569 | struct lowpan_dev *entry; |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 570 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 571 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 572 | rcu_read_lock(); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 573 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 574 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 575 | struct lowpan_peer *pentry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 576 | struct lowpan_dev *dev; |
| 577 | |
| 578 | if (entry->netdev != netdev) |
| 579 | continue; |
| 580 | |
| 581 | dev = lowpan_dev(entry->netdev); |
| 582 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 583 | list_for_each_entry_rcu(pentry, &dev->peers, list) { |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 584 | int ret; |
| 585 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 586 | local_skb = skb_clone(skb, GFP_ATOMIC); |
| 587 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 588 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", |
| 589 | netdev->name, |
| 590 | &pentry->chan->dst, pentry->chan->dst_type, |
| 591 | &pentry->peer_addr, pentry->chan); |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 592 | ret = send_pkt(pentry->chan, local_skb, netdev); |
| 593 | if (ret < 0) |
| 594 | err = ret; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 595 | |
| 596 | kfree_skb(local_skb); |
| 597 | } |
| 598 | } |
| 599 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 600 | rcu_read_unlock(); |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 601 | |
| 602 | return err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 603 | } |
| 604 | |
| 605 | static netdev_tx_t bt_xmit(struct sk_buff *skb, struct net_device *netdev) |
| 606 | { |
| 607 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 608 | bdaddr_t addr; |
| 609 | u8 addr_type; |
| 610 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 611 | /* We must take a copy of the skb before we modify/replace the ipv6 |
| 612 | * header as the header could be used elsewhere |
| 613 | */ |
Alexander Aring | b0c42cd | 2014-10-08 10:24:53 +0200 | [diff] [blame] | 614 | skb = skb_unshare(skb, GFP_ATOMIC); |
| 615 | if (!skb) |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 616 | return NET_XMIT_DROP; |
| 617 | |
| 618 | /* Return values from setup_header() |
| 619 | * <0 - error, packet is dropped |
| 620 | * 0 - this is a multicast packet |
| 621 | * 1 - this is unicast packet |
| 622 | */ |
| 623 | err = setup_header(skb, netdev, &addr, &addr_type); |
| 624 | if (err < 0) { |
| 625 | kfree_skb(skb); |
| 626 | return NET_XMIT_DROP; |
| 627 | } |
| 628 | |
| 629 | if (err) { |
| 630 | if (lowpan_cb(skb)->chan) { |
| 631 | BT_DBG("xmit %s to %pMR type %d IP %pI6c chan %p", |
| 632 | netdev->name, &addr, addr_type, |
| 633 | &lowpan_cb(skb)->addr, lowpan_cb(skb)->chan); |
Jukka Rissanen | d7b6b0a | 2014-10-01 15:59:14 +0300 | [diff] [blame] | 634 | err = send_pkt(lowpan_cb(skb)->chan, skb, netdev); |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 635 | } else { |
| 636 | err = -ENOENT; |
| 637 | } |
| 638 | } else { |
| 639 | /* We need to send the packet to every device behind this |
| 640 | * interface. |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 641 | */ |
Jukka Rissanen | 9c238ca | 2014-10-01 15:59:15 +0300 | [diff] [blame] | 642 | err = send_mcast_pkt(skb, netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 643 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 644 | |
Jukka Rissanen | fc12518 | 2014-10-01 11:30:26 +0300 | [diff] [blame] | 645 | dev_kfree_skb(skb); |
| 646 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 647 | if (err) |
| 648 | BT_DBG("ERROR: xmit failed (%d)", err); |
| 649 | |
Jukka Rissanen | 36b3dd2 | 2014-09-29 16:37:25 +0300 | [diff] [blame] | 650 | return err < 0 ? NET_XMIT_DROP : err; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 651 | } |
| 652 | |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 653 | static struct lock_class_key bt_tx_busylock; |
| 654 | static struct lock_class_key bt_netdev_xmit_lock_key; |
| 655 | |
| 656 | static void bt_set_lockdep_class_one(struct net_device *dev, |
| 657 | struct netdev_queue *txq, |
| 658 | void *_unused) |
| 659 | { |
| 660 | lockdep_set_class(&txq->_xmit_lock, &bt_netdev_xmit_lock_key); |
| 661 | } |
| 662 | |
| 663 | static int bt_dev_init(struct net_device *dev) |
| 664 | { |
| 665 | netdev_for_each_tx_queue(dev, bt_set_lockdep_class_one, NULL); |
| 666 | dev->qdisc_tx_busylock = &bt_tx_busylock; |
| 667 | |
| 668 | return 0; |
| 669 | } |
| 670 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 671 | static const struct net_device_ops netdev_ops = { |
Jukka Rissanen | df09230 | 2014-10-28 17:16:48 +0200 | [diff] [blame] | 672 | .ndo_init = bt_dev_init, |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 673 | .ndo_start_xmit = bt_xmit, |
| 674 | }; |
| 675 | |
| 676 | static struct header_ops header_ops = { |
| 677 | .create = header_create, |
| 678 | }; |
| 679 | |
| 680 | static void netdev_setup(struct net_device *dev) |
| 681 | { |
| 682 | dev->addr_len = EUI64_ADDR_LEN; |
| 683 | dev->type = ARPHRD_6LOWPAN; |
| 684 | |
| 685 | dev->hard_header_len = 0; |
| 686 | dev->needed_tailroom = 0; |
| 687 | dev->mtu = IPV6_MIN_MTU; |
| 688 | dev->tx_queue_len = 0; |
Jukka Rissanen | 156395c | 2014-09-29 16:37:26 +0300 | [diff] [blame] | 689 | dev->flags = IFF_RUNNING | IFF_POINTOPOINT | |
| 690 | IFF_MULTICAST; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 691 | dev->watchdog_timeo = 0; |
| 692 | |
| 693 | dev->netdev_ops = &netdev_ops; |
| 694 | dev->header_ops = &header_ops; |
| 695 | dev->destructor = free_netdev; |
| 696 | } |
| 697 | |
| 698 | static struct device_type bt_type = { |
| 699 | .name = "bluetooth", |
| 700 | }; |
| 701 | |
| 702 | static void set_addr(u8 *eui, u8 *addr, u8 addr_type) |
| 703 | { |
| 704 | /* addr is the BT address in little-endian format */ |
| 705 | eui[0] = addr[5]; |
| 706 | eui[1] = addr[4]; |
| 707 | eui[2] = addr[3]; |
| 708 | eui[3] = 0xFF; |
| 709 | eui[4] = 0xFE; |
| 710 | eui[5] = addr[2]; |
| 711 | eui[6] = addr[1]; |
| 712 | eui[7] = addr[0]; |
| 713 | |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 714 | /* Universal/local bit set, BT 6lowpan draft ch. 3.2.1 */ |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 715 | if (addr_type == BDADDR_LE_PUBLIC) |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 716 | eui[0] &= ~0x02; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 717 | else |
Jukka Rissanen | 62bbd5b | 2014-05-27 11:33:22 +0300 | [diff] [blame] | 718 | eui[0] |= 0x02; |
| 719 | |
| 720 | BT_DBG("type %d addr %*phC", addr_type, 8, eui); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 721 | } |
| 722 | |
| 723 | static void set_dev_addr(struct net_device *netdev, bdaddr_t *addr, |
| 724 | u8 addr_type) |
| 725 | { |
| 726 | netdev->addr_assign_type = NET_ADDR_PERM; |
| 727 | set_addr(netdev->dev_addr, addr->b, addr_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 728 | } |
| 729 | |
| 730 | static void ifup(struct net_device *netdev) |
| 731 | { |
| 732 | int err; |
| 733 | |
| 734 | rtnl_lock(); |
| 735 | err = dev_open(netdev); |
| 736 | if (err < 0) |
| 737 | BT_INFO("iface %s cannot be opened (%d)", netdev->name, err); |
| 738 | rtnl_unlock(); |
| 739 | } |
| 740 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 741 | static void ifdown(struct net_device *netdev) |
| 742 | { |
| 743 | int err; |
| 744 | |
| 745 | rtnl_lock(); |
| 746 | err = dev_close(netdev); |
| 747 | if (err < 0) |
| 748 | BT_INFO("iface %s cannot be closed (%d)", netdev->name, err); |
| 749 | rtnl_unlock(); |
| 750 | } |
| 751 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 752 | static void do_notify_peers(struct work_struct *work) |
| 753 | { |
| 754 | struct lowpan_dev *dev = container_of(work, struct lowpan_dev, |
| 755 | notify_peers.work); |
| 756 | |
| 757 | netdev_notify_peers(dev->netdev); /* send neighbour adv at startup */ |
| 758 | } |
| 759 | |
| 760 | static bool is_bt_6lowpan(struct hci_conn *hcon) |
| 761 | { |
| 762 | if (hcon->type != LE_LINK) |
| 763 | return false; |
| 764 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 765 | if (!psm_6lowpan) |
| 766 | return false; |
| 767 | |
| 768 | return true; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 769 | } |
| 770 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 771 | static struct l2cap_chan *chan_create(void) |
| 772 | { |
| 773 | struct l2cap_chan *chan; |
| 774 | |
| 775 | chan = l2cap_chan_create(); |
| 776 | if (!chan) |
| 777 | return NULL; |
| 778 | |
| 779 | l2cap_chan_set_defaults(chan); |
| 780 | |
| 781 | chan->chan_type = L2CAP_CHAN_CONN_ORIENTED; |
| 782 | chan->mode = L2CAP_MODE_LE_FLOWCTL; |
| 783 | chan->omtu = 65535; |
| 784 | chan->imtu = chan->omtu; |
| 785 | |
| 786 | return chan; |
| 787 | } |
| 788 | |
| 789 | static struct l2cap_chan *chan_open(struct l2cap_chan *pchan) |
| 790 | { |
| 791 | struct l2cap_chan *chan; |
| 792 | |
| 793 | chan = chan_create(); |
| 794 | if (!chan) |
| 795 | return NULL; |
| 796 | |
| 797 | chan->remote_mps = chan->omtu; |
| 798 | chan->mps = chan->omtu; |
| 799 | |
| 800 | chan->state = BT_CONNECTED; |
| 801 | |
| 802 | return chan; |
| 803 | } |
| 804 | |
Jukka Rissanen | b2799ce | 2014-09-08 12:11:44 +0300 | [diff] [blame] | 805 | static void set_ip_addr_bits(u8 addr_type, u8 *addr) |
| 806 | { |
| 807 | if (addr_type == BDADDR_LE_PUBLIC) |
| 808 | *addr |= 0x02; |
| 809 | else |
| 810 | *addr &= ~0x02; |
| 811 | } |
| 812 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 813 | static struct l2cap_chan *add_peer_chan(struct l2cap_chan *chan, |
| 814 | struct lowpan_dev *dev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 815 | { |
| 816 | struct lowpan_peer *peer; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 817 | |
| 818 | peer = kzalloc(sizeof(*peer), GFP_ATOMIC); |
| 819 | if (!peer) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 820 | return NULL; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 821 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 822 | peer->chan = chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 823 | memset(&peer->peer_addr, 0, sizeof(struct in6_addr)); |
| 824 | |
| 825 | /* RFC 2464 ch. 5 */ |
| 826 | peer->peer_addr.s6_addr[0] = 0xFE; |
| 827 | peer->peer_addr.s6_addr[1] = 0x80; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 828 | set_addr((u8 *)&peer->peer_addr.s6_addr + 8, chan->dst.b, |
| 829 | chan->dst_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 830 | |
| 831 | memcpy(&peer->eui64_addr, (u8 *)&peer->peer_addr.s6_addr + 8, |
| 832 | EUI64_ADDR_LEN); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 833 | |
Jukka Rissanen | b2799ce | 2014-09-08 12:11:44 +0300 | [diff] [blame] | 834 | /* IPv6 address needs to have the U/L bit set properly so toggle |
| 835 | * it back here. |
| 836 | */ |
| 837 | set_ip_addr_bits(chan->dst_type, (u8 *)&peer->peer_addr.s6_addr + 8); |
| 838 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 839 | spin_lock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 840 | INIT_LIST_HEAD(&peer->list); |
| 841 | peer_add(dev, peer); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 842 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 843 | |
| 844 | /* Notifying peers about us needs to be done without locks held */ |
| 845 | INIT_DELAYED_WORK(&dev->notify_peers, do_notify_peers); |
| 846 | schedule_delayed_work(&dev->notify_peers, msecs_to_jiffies(100)); |
| 847 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 848 | return peer->chan; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 849 | } |
| 850 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 851 | static int setup_netdev(struct l2cap_chan *chan, struct lowpan_dev **dev) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 852 | { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 853 | struct net_device *netdev; |
| 854 | int err = 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 855 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 856 | netdev = alloc_netdev(sizeof(struct lowpan_dev), IFACE_NAME_TEMPLATE, |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 857 | NET_NAME_UNKNOWN, netdev_setup); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 858 | if (!netdev) |
| 859 | return -ENOMEM; |
| 860 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 861 | set_dev_addr(netdev, &chan->src, chan->src_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 862 | |
| 863 | netdev->netdev_ops = &netdev_ops; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 864 | SET_NETDEV_DEV(netdev, &chan->conn->hcon->dev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 865 | SET_NETDEV_DEVTYPE(netdev, &bt_type); |
| 866 | |
| 867 | err = register_netdev(netdev); |
| 868 | if (err < 0) { |
| 869 | BT_INFO("register_netdev failed %d", err); |
| 870 | free_netdev(netdev); |
| 871 | goto out; |
| 872 | } |
| 873 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 874 | BT_DBG("ifindex %d peer bdaddr %pMR type %d my addr %pMR type %d", |
| 875 | netdev->ifindex, &chan->dst, chan->dst_type, |
| 876 | &chan->src, chan->src_type); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 877 | set_bit(__LINK_STATE_PRESENT, &netdev->state); |
| 878 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 879 | *dev = netdev_priv(netdev); |
| 880 | (*dev)->netdev = netdev; |
| 881 | (*dev)->hdev = chan->conn->hcon->hdev; |
| 882 | INIT_LIST_HEAD(&(*dev)->peers); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 883 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 884 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 885 | INIT_LIST_HEAD(&(*dev)->list); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 886 | list_add_rcu(&(*dev)->list, &bt_6lowpan_devices); |
| 887 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 888 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 889 | return 0; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 890 | |
| 891 | out: |
| 892 | return err; |
| 893 | } |
| 894 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 895 | static inline void chan_ready_cb(struct l2cap_chan *chan) |
| 896 | { |
| 897 | struct lowpan_dev *dev; |
| 898 | |
| 899 | dev = lookup_dev(chan->conn); |
| 900 | |
| 901 | BT_DBG("chan %p conn %p dev %p", chan, chan->conn, dev); |
| 902 | |
| 903 | if (!dev) { |
| 904 | if (setup_netdev(chan, &dev) < 0) { |
| 905 | l2cap_chan_del(chan, -ENOENT); |
| 906 | return; |
| 907 | } |
| 908 | } |
| 909 | |
Jukka Rissanen | 18d93c1 | 2014-06-18 16:37:10 +0300 | [diff] [blame] | 910 | if (!try_module_get(THIS_MODULE)) |
| 911 | return; |
| 912 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 913 | add_peer_chan(chan, dev); |
| 914 | ifup(dev->netdev); |
| 915 | } |
| 916 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 917 | 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] | 918 | { |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 919 | struct l2cap_chan *chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 920 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 921 | chan = chan_open(pchan); |
| 922 | chan->ops = pchan->ops; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 923 | |
| 924 | BT_DBG("chan %p pchan %p", chan, pchan); |
| 925 | |
Johan Hedberg | 2b29349 | 2014-08-07 10:03:32 +0300 | [diff] [blame] | 926 | return chan; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 927 | } |
| 928 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 929 | static void delete_netdev(struct work_struct *work) |
| 930 | { |
| 931 | struct lowpan_dev *entry = container_of(work, struct lowpan_dev, |
| 932 | delete_netdev); |
| 933 | |
| 934 | unregister_netdev(entry->netdev); |
| 935 | |
| 936 | /* The entry pointer is deleted in device_event() */ |
| 937 | } |
| 938 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 939 | static void chan_close_cb(struct l2cap_chan *chan) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 940 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 941 | struct lowpan_dev *entry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 942 | struct lowpan_dev *dev = NULL; |
| 943 | struct lowpan_peer *peer; |
| 944 | int err = -ENOENT; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 945 | bool last = false, removed = true; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 946 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 947 | BT_DBG("chan %p conn %p", chan, chan->conn); |
| 948 | |
| 949 | if (chan->conn && chan->conn->hcon) { |
| 950 | if (!is_bt_6lowpan(chan->conn->hcon)) |
| 951 | return; |
| 952 | |
| 953 | /* If conn is set, then the netdev is also there and we should |
| 954 | * not remove it. |
| 955 | */ |
| 956 | removed = false; |
| 957 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 958 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 959 | spin_lock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 960 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 961 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 962 | dev = lowpan_dev(entry->netdev); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 963 | peer = __peer_lookup_chan(dev, chan); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 964 | if (peer) { |
| 965 | last = peer_del(dev, peer); |
| 966 | err = 0; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 967 | |
| 968 | BT_DBG("dev %p removing %speer %p", dev, |
| 969 | last ? "last " : "1 ", peer); |
| 970 | BT_DBG("chan %p orig refcnt %d", chan, |
| 971 | atomic_read(&chan->kref.refcount)); |
| 972 | |
| 973 | l2cap_chan_put(chan); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 974 | break; |
| 975 | } |
| 976 | } |
| 977 | |
| 978 | if (!err && last && dev && !atomic_read(&dev->peer_count)) { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 979 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 980 | |
| 981 | cancel_delayed_work_sync(&dev->notify_peers); |
| 982 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 983 | ifdown(dev->netdev); |
| 984 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 985 | if (!removed) { |
| 986 | INIT_WORK(&entry->delete_netdev, delete_netdev); |
| 987 | schedule_work(&entry->delete_netdev); |
| 988 | } |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 989 | } else { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 990 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 991 | } |
| 992 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 993 | return; |
| 994 | } |
| 995 | |
| 996 | static void chan_state_change_cb(struct l2cap_chan *chan, int state, int err) |
| 997 | { |
| 998 | BT_DBG("chan %p conn %p state %s err %d", chan, chan->conn, |
| 999 | state_to_string(state), err); |
| 1000 | } |
| 1001 | |
| 1002 | static struct sk_buff *chan_alloc_skb_cb(struct l2cap_chan *chan, |
| 1003 | unsigned long hdr_len, |
| 1004 | unsigned long len, int nb) |
| 1005 | { |
| 1006 | /* Note that we must allocate using GFP_ATOMIC here as |
| 1007 | * this function is called originally from netdev hard xmit |
| 1008 | * function in atomic context. |
| 1009 | */ |
| 1010 | return bt_skb_alloc(hdr_len + len, GFP_ATOMIC); |
| 1011 | } |
| 1012 | |
| 1013 | static void chan_suspend_cb(struct l2cap_chan *chan) |
| 1014 | { |
| 1015 | struct sk_buff *skb = chan->data; |
| 1016 | |
| 1017 | BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); |
| 1018 | |
Jukka Rissanen | 59790aa | 2014-09-29 10:55:46 +0300 | [diff] [blame] | 1019 | if (!skb) |
| 1020 | return; |
| 1021 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1022 | lowpan_cb(skb)->status = -EAGAIN; |
| 1023 | } |
| 1024 | |
| 1025 | static void chan_resume_cb(struct l2cap_chan *chan) |
| 1026 | { |
| 1027 | struct sk_buff *skb = chan->data; |
| 1028 | |
| 1029 | BT_DBG("chan %p conn %p skb %p", chan, chan->conn, skb); |
| 1030 | |
Jukka Rissanen | 59790aa | 2014-09-29 10:55:46 +0300 | [diff] [blame] | 1031 | if (!skb) |
| 1032 | return; |
| 1033 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1034 | lowpan_cb(skb)->status = 0; |
| 1035 | } |
| 1036 | |
| 1037 | static long chan_get_sndtimeo_cb(struct l2cap_chan *chan) |
| 1038 | { |
Jukka Rissanen | 2ae50d8 | 2014-09-08 12:11:43 +0300 | [diff] [blame] | 1039 | return L2CAP_CONN_TIMEOUT; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1040 | } |
| 1041 | |
| 1042 | static const struct l2cap_ops bt_6lowpan_chan_ops = { |
| 1043 | .name = "L2CAP 6LoWPAN channel", |
| 1044 | .new_connection = chan_new_conn_cb, |
| 1045 | .recv = chan_recv_cb, |
| 1046 | .close = chan_close_cb, |
| 1047 | .state_change = chan_state_change_cb, |
| 1048 | .ready = chan_ready_cb, |
| 1049 | .resume = chan_resume_cb, |
| 1050 | .suspend = chan_suspend_cb, |
| 1051 | .get_sndtimeo = chan_get_sndtimeo_cb, |
| 1052 | .alloc_skb = chan_alloc_skb_cb, |
| 1053 | .memcpy_fromiovec = l2cap_chan_no_memcpy_fromiovec, |
| 1054 | |
| 1055 | .teardown = l2cap_chan_no_teardown, |
| 1056 | .defer = l2cap_chan_no_defer, |
| 1057 | .set_shutdown = l2cap_chan_no_set_shutdown, |
| 1058 | }; |
| 1059 | |
| 1060 | static inline __u8 bdaddr_type(__u8 type) |
| 1061 | { |
| 1062 | if (type == ADDR_LE_DEV_PUBLIC) |
| 1063 | return BDADDR_LE_PUBLIC; |
| 1064 | else |
| 1065 | return BDADDR_LE_RANDOM; |
| 1066 | } |
| 1067 | |
| 1068 | static struct l2cap_chan *chan_get(void) |
| 1069 | { |
| 1070 | struct l2cap_chan *pchan; |
| 1071 | |
| 1072 | pchan = chan_create(); |
| 1073 | if (!pchan) |
| 1074 | return NULL; |
| 1075 | |
| 1076 | pchan->ops = &bt_6lowpan_chan_ops; |
| 1077 | |
| 1078 | return pchan; |
| 1079 | } |
| 1080 | |
| 1081 | static int bt_6lowpan_connect(bdaddr_t *addr, u8 dst_type) |
| 1082 | { |
| 1083 | struct l2cap_chan *pchan; |
| 1084 | int err; |
| 1085 | |
| 1086 | pchan = chan_get(); |
| 1087 | if (!pchan) |
| 1088 | return -EINVAL; |
| 1089 | |
| 1090 | err = l2cap_chan_connect(pchan, cpu_to_le16(psm_6lowpan), 0, |
| 1091 | addr, dst_type); |
| 1092 | |
| 1093 | BT_DBG("chan %p err %d", pchan, err); |
| 1094 | if (err < 0) |
| 1095 | l2cap_chan_put(pchan); |
| 1096 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1097 | return err; |
| 1098 | } |
| 1099 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1100 | static int bt_6lowpan_disconnect(struct l2cap_conn *conn, u8 dst_type) |
| 1101 | { |
| 1102 | struct lowpan_peer *peer; |
| 1103 | |
| 1104 | BT_DBG("conn %p dst type %d", conn, dst_type); |
| 1105 | |
| 1106 | peer = lookup_peer(conn); |
| 1107 | if (!peer) |
| 1108 | return -ENOENT; |
| 1109 | |
| 1110 | BT_DBG("peer %p chan %p", peer, peer->chan); |
| 1111 | |
| 1112 | l2cap_chan_close(peer->chan, ENOENT); |
| 1113 | |
| 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | static struct l2cap_chan *bt_6lowpan_listen(void) |
| 1118 | { |
| 1119 | bdaddr_t *addr = BDADDR_ANY; |
| 1120 | struct l2cap_chan *pchan; |
| 1121 | int err; |
| 1122 | |
| 1123 | if (psm_6lowpan == 0) |
| 1124 | return NULL; |
| 1125 | |
| 1126 | pchan = chan_get(); |
| 1127 | if (!pchan) |
| 1128 | return NULL; |
| 1129 | |
| 1130 | pchan->state = BT_LISTEN; |
| 1131 | pchan->src_type = BDADDR_LE_PUBLIC; |
| 1132 | |
Johan Hedberg | 2773b02 | 2014-11-13 09:46:05 +0200 | [diff] [blame] | 1133 | atomic_set(&pchan->nesting, L2CAP_NESTING_PARENT); |
| 1134 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1135 | BT_DBG("psm 0x%04x chan %p src type %d", psm_6lowpan, pchan, |
| 1136 | pchan->src_type); |
| 1137 | |
| 1138 | err = l2cap_add_psm(pchan, addr, cpu_to_le16(psm_6lowpan)); |
| 1139 | if (err) { |
| 1140 | l2cap_chan_put(pchan); |
| 1141 | BT_ERR("psm cannot be added err %d", err); |
| 1142 | return NULL; |
| 1143 | } |
| 1144 | |
| 1145 | return pchan; |
| 1146 | } |
| 1147 | |
| 1148 | static int get_l2cap_conn(char *buf, bdaddr_t *addr, u8 *addr_type, |
| 1149 | struct l2cap_conn **conn) |
| 1150 | { |
| 1151 | struct hci_conn *hcon; |
| 1152 | struct hci_dev *hdev; |
| 1153 | bdaddr_t *src = BDADDR_ANY; |
| 1154 | int n; |
| 1155 | |
| 1156 | n = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx %hhu", |
| 1157 | &addr->b[5], &addr->b[4], &addr->b[3], |
| 1158 | &addr->b[2], &addr->b[1], &addr->b[0], |
| 1159 | addr_type); |
| 1160 | |
| 1161 | if (n < 7) |
| 1162 | return -EINVAL; |
| 1163 | |
| 1164 | hdev = hci_get_route(addr, src); |
| 1165 | if (!hdev) |
| 1166 | return -ENOENT; |
| 1167 | |
| 1168 | hci_dev_lock(hdev); |
| 1169 | hcon = hci_conn_hash_lookup_ba(hdev, LE_LINK, addr); |
| 1170 | hci_dev_unlock(hdev); |
| 1171 | |
| 1172 | if (!hcon) |
| 1173 | return -ENOENT; |
| 1174 | |
| 1175 | *conn = (struct l2cap_conn *)hcon->l2cap_data; |
| 1176 | |
| 1177 | BT_DBG("conn %p dst %pMR type %d", *conn, &hcon->dst, hcon->dst_type); |
| 1178 | |
| 1179 | return 0; |
| 1180 | } |
| 1181 | |
| 1182 | static void disconnect_all_peers(void) |
| 1183 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1184 | struct lowpan_dev *entry; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1185 | struct lowpan_peer *peer, *tmp_peer, *new_peer; |
| 1186 | struct list_head peers; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1187 | |
| 1188 | INIT_LIST_HEAD(&peers); |
| 1189 | |
| 1190 | /* We make a separate list of peers as the close_cb() will |
| 1191 | * modify the device peers list so it is better not to mess |
| 1192 | * with the same list at the same time. |
| 1193 | */ |
| 1194 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1195 | rcu_read_lock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1196 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1197 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
| 1198 | list_for_each_entry_rcu(peer, &entry->peers, list) { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1199 | new_peer = kmalloc(sizeof(*new_peer), GFP_ATOMIC); |
| 1200 | if (!new_peer) |
| 1201 | break; |
| 1202 | |
| 1203 | new_peer->chan = peer->chan; |
| 1204 | INIT_LIST_HEAD(&new_peer->list); |
| 1205 | |
| 1206 | list_add(&new_peer->list, &peers); |
| 1207 | } |
| 1208 | } |
| 1209 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1210 | rcu_read_unlock(); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1211 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1212 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1213 | list_for_each_entry_safe(peer, tmp_peer, &peers, list) { |
| 1214 | l2cap_chan_close(peer->chan, ENOENT); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1215 | |
| 1216 | list_del_rcu(&peer->list); |
Johan Hedberg | 4e79022 | 2014-11-11 14:16:29 +0200 | [diff] [blame] | 1217 | kfree_rcu(peer, rcu); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1218 | |
| 1219 | module_put(THIS_MODULE); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1220 | } |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1221 | spin_unlock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1222 | } |
| 1223 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1224 | struct set_psm { |
| 1225 | struct work_struct work; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1226 | u16 psm; |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1227 | }; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1228 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1229 | static void do_psm_set(struct work_struct *work) |
| 1230 | { |
| 1231 | struct set_psm *set_psm = container_of(work, struct set_psm, work); |
| 1232 | |
| 1233 | if (set_psm->psm == 0 || psm_6lowpan != set_psm->psm) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1234 | /* Disconnect existing connections if 6lowpan is |
| 1235 | * disabled (psm = 0), or if psm changes. |
| 1236 | */ |
| 1237 | disconnect_all_peers(); |
| 1238 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1239 | psm_6lowpan = set_psm->psm; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1240 | |
| 1241 | if (listen_chan) { |
| 1242 | l2cap_chan_close(listen_chan, 0); |
| 1243 | l2cap_chan_put(listen_chan); |
| 1244 | } |
| 1245 | |
| 1246 | listen_chan = bt_6lowpan_listen(); |
| 1247 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1248 | kfree(set_psm); |
| 1249 | } |
| 1250 | |
| 1251 | static int lowpan_psm_set(void *data, u64 val) |
| 1252 | { |
| 1253 | struct set_psm *set_psm; |
| 1254 | |
| 1255 | set_psm = kzalloc(sizeof(*set_psm), GFP_KERNEL); |
| 1256 | if (!set_psm) |
| 1257 | return -ENOMEM; |
| 1258 | |
| 1259 | set_psm->psm = val; |
| 1260 | INIT_WORK(&set_psm->work, do_psm_set); |
| 1261 | |
| 1262 | schedule_work(&set_psm->work); |
| 1263 | |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | static int lowpan_psm_get(void *data, u64 *val) |
| 1268 | { |
| 1269 | *val = psm_6lowpan; |
| 1270 | return 0; |
| 1271 | } |
| 1272 | |
| 1273 | DEFINE_SIMPLE_ATTRIBUTE(lowpan_psm_fops, lowpan_psm_get, |
| 1274 | lowpan_psm_set, "%llu\n"); |
| 1275 | |
| 1276 | static ssize_t lowpan_control_write(struct file *fp, |
| 1277 | const char __user *user_buffer, |
| 1278 | size_t count, |
| 1279 | loff_t *position) |
| 1280 | { |
| 1281 | char buf[32]; |
| 1282 | size_t buf_size = min(count, sizeof(buf) - 1); |
| 1283 | int ret; |
| 1284 | bdaddr_t addr; |
| 1285 | u8 addr_type; |
| 1286 | struct l2cap_conn *conn = NULL; |
| 1287 | |
| 1288 | if (copy_from_user(buf, user_buffer, buf_size)) |
| 1289 | return -EFAULT; |
| 1290 | |
| 1291 | buf[buf_size] = '\0'; |
| 1292 | |
| 1293 | if (memcmp(buf, "connect ", 8) == 0) { |
| 1294 | ret = get_l2cap_conn(&buf[8], &addr, &addr_type, &conn); |
| 1295 | if (ret == -EINVAL) |
| 1296 | return ret; |
| 1297 | |
| 1298 | if (listen_chan) { |
| 1299 | l2cap_chan_close(listen_chan, 0); |
| 1300 | l2cap_chan_put(listen_chan); |
| 1301 | listen_chan = NULL; |
| 1302 | } |
| 1303 | |
| 1304 | if (conn) { |
| 1305 | struct lowpan_peer *peer; |
| 1306 | |
| 1307 | if (!is_bt_6lowpan(conn->hcon)) |
| 1308 | return -EINVAL; |
| 1309 | |
| 1310 | peer = lookup_peer(conn); |
| 1311 | if (peer) { |
| 1312 | BT_DBG("6LoWPAN connection already exists"); |
| 1313 | return -EALREADY; |
| 1314 | } |
| 1315 | |
| 1316 | BT_DBG("conn %p dst %pMR type %d user %d", conn, |
| 1317 | &conn->hcon->dst, conn->hcon->dst_type, |
| 1318 | addr_type); |
| 1319 | } |
| 1320 | |
| 1321 | ret = bt_6lowpan_connect(&addr, addr_type); |
| 1322 | if (ret < 0) |
| 1323 | return ret; |
| 1324 | |
| 1325 | return count; |
| 1326 | } |
| 1327 | |
| 1328 | if (memcmp(buf, "disconnect ", 11) == 0) { |
| 1329 | ret = get_l2cap_conn(&buf[11], &addr, &addr_type, &conn); |
| 1330 | if (ret < 0) |
| 1331 | return ret; |
| 1332 | |
| 1333 | ret = bt_6lowpan_disconnect(conn, addr_type); |
| 1334 | if (ret < 0) |
| 1335 | return ret; |
| 1336 | |
| 1337 | return count; |
| 1338 | } |
| 1339 | |
| 1340 | return count; |
| 1341 | } |
| 1342 | |
| 1343 | static int lowpan_control_show(struct seq_file *f, void *ptr) |
| 1344 | { |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1345 | struct lowpan_dev *entry; |
| 1346 | struct lowpan_peer *peer; |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1347 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1348 | spin_lock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1349 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1350 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
| 1351 | list_for_each_entry(peer, &entry->peers, list) |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1352 | seq_printf(f, "%pMR (type %u)\n", |
| 1353 | &peer->chan->dst, peer->chan->dst_type); |
| 1354 | } |
| 1355 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1356 | spin_unlock(&devices_lock); |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1357 | |
| 1358 | return 0; |
| 1359 | } |
| 1360 | |
| 1361 | static int lowpan_control_open(struct inode *inode, struct file *file) |
| 1362 | { |
| 1363 | return single_open(file, lowpan_control_show, inode->i_private); |
| 1364 | } |
| 1365 | |
| 1366 | static const struct file_operations lowpan_control_fops = { |
| 1367 | .open = lowpan_control_open, |
| 1368 | .read = seq_read, |
| 1369 | .write = lowpan_control_write, |
| 1370 | .llseek = seq_lseek, |
| 1371 | .release = single_release, |
| 1372 | }; |
| 1373 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1374 | static void disconnect_devices(void) |
| 1375 | { |
Dan Carpenter | daac197 | 2014-10-29 19:10:57 +0300 | [diff] [blame] | 1376 | struct lowpan_dev *entry, *tmp, *new_dev; |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1377 | struct list_head devices; |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1378 | |
| 1379 | INIT_LIST_HEAD(&devices); |
| 1380 | |
| 1381 | /* We make a separate list of devices because the unregister_netdev() |
| 1382 | * will call device_event() which will also want to modify the same |
| 1383 | * devices list. |
| 1384 | */ |
| 1385 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1386 | rcu_read_lock(); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1387 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1388 | list_for_each_entry_rcu(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1389 | new_dev = kmalloc(sizeof(*new_dev), GFP_ATOMIC); |
| 1390 | if (!new_dev) |
| 1391 | break; |
| 1392 | |
| 1393 | new_dev->netdev = entry->netdev; |
| 1394 | INIT_LIST_HEAD(&new_dev->list); |
| 1395 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1396 | list_add_rcu(&new_dev->list, &devices); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1397 | } |
| 1398 | |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1399 | rcu_read_unlock(); |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1400 | |
Dan Carpenter | daac197 | 2014-10-29 19:10:57 +0300 | [diff] [blame] | 1401 | list_for_each_entry_safe(entry, tmp, &devices, list) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1402 | ifdown(entry->netdev); |
| 1403 | BT_DBG("Unregistering netdev %s %p", |
| 1404 | entry->netdev->name, entry->netdev); |
| 1405 | unregister_netdev(entry->netdev); |
| 1406 | kfree(entry); |
| 1407 | } |
| 1408 | } |
| 1409 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1410 | static int device_event(struct notifier_block *unused, |
| 1411 | unsigned long event, void *ptr) |
| 1412 | { |
| 1413 | struct net_device *netdev = netdev_notifier_info_to_dev(ptr); |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1414 | struct lowpan_dev *entry; |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1415 | |
| 1416 | if (netdev->type != ARPHRD_6LOWPAN) |
| 1417 | return NOTIFY_DONE; |
| 1418 | |
| 1419 | switch (event) { |
| 1420 | case NETDEV_UNREGISTER: |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1421 | spin_lock(&devices_lock); |
| 1422 | list_for_each_entry(entry, &bt_6lowpan_devices, list) { |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1423 | if (entry->netdev == netdev) { |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1424 | BT_DBG("Unregistered netdev %s %p", |
| 1425 | netdev->name, netdev); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1426 | list_del(&entry->list); |
| 1427 | kfree(entry); |
| 1428 | break; |
| 1429 | } |
| 1430 | } |
Jukka Rissanen | 9030582 | 2014-10-28 17:16:47 +0200 | [diff] [blame] | 1431 | spin_unlock(&devices_lock); |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1432 | break; |
| 1433 | } |
| 1434 | |
| 1435 | return NOTIFY_DONE; |
| 1436 | } |
| 1437 | |
| 1438 | static struct notifier_block bt_6lowpan_dev_notifier = { |
| 1439 | .notifier_call = device_event, |
| 1440 | }; |
| 1441 | |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1442 | static int __init bt_6lowpan_init(void) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1443 | { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1444 | lowpan_psm_debugfs = debugfs_create_file("6lowpan_psm", 0644, |
| 1445 | bt_debugfs, NULL, |
| 1446 | &lowpan_psm_fops); |
| 1447 | lowpan_control_debugfs = debugfs_create_file("6lowpan_control", 0644, |
| 1448 | bt_debugfs, NULL, |
| 1449 | &lowpan_control_fops); |
| 1450 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1451 | return register_netdevice_notifier(&bt_6lowpan_dev_notifier); |
| 1452 | } |
| 1453 | |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1454 | static void __exit bt_6lowpan_exit(void) |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1455 | { |
Jukka Rissanen | 6b8d4a6 | 2014-06-18 16:37:08 +0300 | [diff] [blame] | 1456 | debugfs_remove(lowpan_psm_debugfs); |
| 1457 | debugfs_remove(lowpan_control_debugfs); |
| 1458 | |
| 1459 | if (listen_chan) { |
| 1460 | l2cap_chan_close(listen_chan, 0); |
| 1461 | l2cap_chan_put(listen_chan); |
| 1462 | } |
| 1463 | |
Jukka Rissanen | 7f11825 | 2014-06-18 16:37:11 +0300 | [diff] [blame] | 1464 | disconnect_devices(); |
| 1465 | |
Jukka Rissanen | 18722c2 | 2013-12-11 17:05:37 +0200 | [diff] [blame] | 1466 | unregister_netdevice_notifier(&bt_6lowpan_dev_notifier); |
| 1467 | } |
Jukka Rissanen | 5547e48 | 2014-06-18 16:37:09 +0300 | [diff] [blame] | 1468 | |
| 1469 | module_init(bt_6lowpan_init); |
| 1470 | module_exit(bt_6lowpan_exit); |
| 1471 | |
| 1472 | MODULE_AUTHOR("Jukka Rissanen <jukka.rissanen@linux.intel.com>"); |
| 1473 | MODULE_DESCRIPTION("Bluetooth 6LoWPAN"); |
| 1474 | MODULE_VERSION(VERSION); |
| 1475 | MODULE_LICENSE("GPL"); |