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