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