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