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