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