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