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