YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | BNEP implementation for Linux Bluetooth stack (BlueZ). |
| 3 | Copyright (C) 2001-2002 Inventel Systemes |
| 4 | Written 2001-2002 by |
Jan Engelhardt | 96de0e2 | 2007-10-19 23:21:04 +0200 | [diff] [blame] | 5 | Clément Moreau <clement.moreau@inventel.fr> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | David Libault <david.libault@inventel.fr> |
| 7 | |
| 8 | Copyright (C) 2002 Maxim Krasnyansky <maxk@qualcomm.com> |
| 9 | |
| 10 | This program is free software; you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License version 2 as |
| 12 | published by the Free Software Foundation; |
| 13 | |
| 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS |
| 15 | OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. |
| 17 | IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 18 | CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 23 | ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, |
| 24 | COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | SOFTWARE IS DISCLAIMED. |
| 26 | */ |
| 27 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | #include <linux/module.h> |
Szymon Janc | f4d7cd4 | 2011-03-21 14:20:00 +0100 | [diff] [blame] | 29 | #include <linux/kthread.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | #include <linux/file.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | #include <linux/etherdevice.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include <asm/unaligned.h> |
| 33 | |
| 34 | #include <net/bluetooth/bluetooth.h> |
Marcel Holtmann | 65f53e9 | 2013-10-13 09:49:57 -0700 | [diff] [blame] | 35 | #include <net/bluetooth/l2cap.h> |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 36 | #include <net/bluetooth/hci_core.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | #include "bnep.h" |
| 39 | |
Marcel Holtmann | 28111eb | 2008-08-07 22:26:54 +0200 | [diff] [blame] | 40 | #define VERSION "1.3" |
| 41 | |
Rusty Russell | eb93992 | 2011-12-19 14:08:01 +0000 | [diff] [blame] | 42 | static bool compress_src = true; |
| 43 | static bool compress_dst = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | |
| 45 | static LIST_HEAD(bnep_session_list); |
| 46 | static DECLARE_RWSEM(bnep_session_sem); |
| 47 | |
| 48 | static struct bnep_session *__bnep_get_session(u8 *dst) |
| 49 | { |
| 50 | struct bnep_session *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
| 52 | BT_DBG(""); |
| 53 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 54 | list_for_each_entry(s, &bnep_session_list, list) |
Joe Perches | c47fc98 | 2012-05-08 18:56:51 +0000 | [diff] [blame] | 55 | if (ether_addr_equal(dst, s->eh.h_source)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | return s; |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 57 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | return NULL; |
| 59 | } |
| 60 | |
| 61 | static void __bnep_link_session(struct bnep_session *s) |
| 62 | { |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 63 | list_add(&s->list, &bnep_session_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | static void __bnep_unlink_session(struct bnep_session *s) |
| 67 | { |
| 68 | list_del(&s->list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | static int bnep_send(struct bnep_session *s, void *data, size_t len) |
| 72 | { |
| 73 | struct socket *sock = s->sock; |
| 74 | struct kvec iv = { data, len }; |
| 75 | |
| 76 | return kernel_sendmsg(sock, &s->msg, &iv, 1, len); |
| 77 | } |
| 78 | |
| 79 | static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp) |
| 80 | { |
| 81 | struct bnep_control_rsp rsp; |
| 82 | rsp.type = BNEP_CONTROL; |
| 83 | rsp.ctrl = ctrl; |
| 84 | rsp.resp = htons(resp); |
| 85 | return bnep_send(s, &rsp, sizeof(rsp)); |
| 86 | } |
| 87 | |
| 88 | #ifdef CONFIG_BT_BNEP_PROTO_FILTER |
| 89 | static inline void bnep_set_default_proto_filter(struct bnep_session *s) |
| 90 | { |
| 91 | /* (IPv4, ARP) */ |
Al Viro | e41d216 | 2006-11-08 00:27:36 -0800 | [diff] [blame] | 92 | s->proto_filter[0].start = ETH_P_IP; |
| 93 | s->proto_filter[0].end = ETH_P_ARP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | /* (RARP, AppleTalk) */ |
Al Viro | e41d216 | 2006-11-08 00:27:36 -0800 | [diff] [blame] | 95 | s->proto_filter[1].start = ETH_P_RARP; |
| 96 | s->proto_filter[1].end = ETH_P_AARP; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | /* (IPX, IPv6) */ |
Al Viro | e41d216 | 2006-11-08 00:27:36 -0800 | [diff] [blame] | 98 | s->proto_filter[2].start = ETH_P_IPX; |
| 99 | s->proto_filter[2].end = ETH_P_IPV6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | } |
| 101 | #endif |
| 102 | |
Al Viro | 1bc5d44 | 2006-11-08 00:27:57 -0800 | [diff] [blame] | 103 | static int bnep_ctrl_set_netfilter(struct bnep_session *s, __be16 *data, int len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | { |
| 105 | int n; |
| 106 | |
| 107 | if (len < 2) |
| 108 | return -EILSEQ; |
| 109 | |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 110 | n = get_unaligned_be16(data); |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 111 | data++; |
| 112 | len -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | |
| 114 | if (len < n) |
| 115 | return -EILSEQ; |
| 116 | |
| 117 | BT_DBG("filter len %d", n); |
| 118 | |
| 119 | #ifdef CONFIG_BT_BNEP_PROTO_FILTER |
| 120 | n /= 4; |
| 121 | if (n <= BNEP_MAX_PROTO_FILTERS) { |
| 122 | struct bnep_proto_filter *f = s->proto_filter; |
| 123 | int i; |
| 124 | |
| 125 | for (i = 0; i < n; i++) { |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 126 | f[i].start = get_unaligned_be16(data++); |
| 127 | f[i].end = get_unaligned_be16(data++); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
| 129 | BT_DBG("proto filter start %d end %d", |
| 130 | f[i].start, f[i].end); |
| 131 | } |
| 132 | |
| 133 | if (i < BNEP_MAX_PROTO_FILTERS) |
| 134 | memset(f + i, 0, sizeof(*f)); |
| 135 | |
| 136 | if (n == 0) |
| 137 | bnep_set_default_proto_filter(s); |
| 138 | |
| 139 | bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_SUCCESS); |
| 140 | } else { |
| 141 | bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_LIMIT_REACHED); |
| 142 | } |
| 143 | #else |
| 144 | bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_UNSUPPORTED_REQ); |
| 145 | #endif |
| 146 | return 0; |
| 147 | } |
| 148 | |
| 149 | static int bnep_ctrl_set_mcfilter(struct bnep_session *s, u8 *data, int len) |
| 150 | { |
| 151 | int n; |
| 152 | |
| 153 | if (len < 2) |
| 154 | return -EILSEQ; |
| 155 | |
Harvey Harrison | 8398531 | 2008-05-02 16:25:46 -0700 | [diff] [blame] | 156 | n = get_unaligned_be16(data); |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 157 | data += 2; |
| 158 | len -= 2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | if (len < n) |
| 161 | return -EILSEQ; |
| 162 | |
| 163 | BT_DBG("filter len %d", n); |
| 164 | |
| 165 | #ifdef CONFIG_BT_BNEP_MC_FILTER |
| 166 | n /= (ETH_ALEN * 2); |
| 167 | |
| 168 | if (n > 0) { |
Szymon Janc | a3d9bd4 | 2011-03-21 14:19:57 +0100 | [diff] [blame] | 169 | int i; |
| 170 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | s->mc_filter = 0; |
| 172 | |
| 173 | /* Always send broadcast */ |
| 174 | set_bit(bnep_mc_hash(s->dev->broadcast), (ulong *) &s->mc_filter); |
| 175 | |
| 176 | /* Add address ranges to the multicast hash */ |
| 177 | for (; n > 0; n--) { |
| 178 | u8 a1[6], *a2; |
| 179 | |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 180 | memcpy(a1, data, ETH_ALEN); |
| 181 | data += ETH_ALEN; |
| 182 | a2 = data; |
| 183 | data += ETH_ALEN; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 184 | |
Andrei Emeltchenko | 6ed93dc | 2012-09-25 12:49:43 +0300 | [diff] [blame] | 185 | BT_DBG("mc filter %pMR -> %pMR", a1, a2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | /* Iterate from a1 to a2 */ |
| 188 | set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter); |
| 189 | while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) { |
Szymon Janc | a3d9bd4 | 2011-03-21 14:19:57 +0100 | [diff] [blame] | 190 | /* Increment a1 */ |
| 191 | i = 5; |
| 192 | while (i >= 0 && ++a1[i--] == 0) |
| 193 | ; |
| 194 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter); |
| 196 | } |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | BT_DBG("mc filter hash 0x%llx", s->mc_filter); |
| 201 | |
| 202 | bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS); |
| 203 | #else |
| 204 | bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ); |
| 205 | #endif |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int bnep_rx_control(struct bnep_session *s, void *data, int len) |
| 210 | { |
| 211 | u8 cmd = *(u8 *)data; |
| 212 | int err = 0; |
| 213 | |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 214 | data++; |
| 215 | len--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
| 217 | switch (cmd) { |
| 218 | case BNEP_CMD_NOT_UNDERSTOOD: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | case BNEP_SETUP_CONN_RSP: |
| 220 | case BNEP_FILTER_NET_TYPE_RSP: |
| 221 | case BNEP_FILTER_MULTI_ADDR_RSP: |
| 222 | /* Ignore these for now */ |
| 223 | break; |
| 224 | |
| 225 | case BNEP_FILTER_NET_TYPE_SET: |
| 226 | err = bnep_ctrl_set_netfilter(s, data, len); |
| 227 | break; |
| 228 | |
| 229 | case BNEP_FILTER_MULTI_ADDR_SET: |
| 230 | err = bnep_ctrl_set_mcfilter(s, data, len); |
| 231 | break; |
| 232 | |
Vikram Kandukuri | cde9f80 | 2009-12-03 15:12:51 +0530 | [diff] [blame] | 233 | case BNEP_SETUP_CONN_REQ: |
Grzegorz Kolodziejczyk | 836a061 | 2015-04-03 12:14:55 +0200 | [diff] [blame] | 234 | /* Successful response should be sent only once */ |
| 235 | if (test_bit(BNEP_SETUP_RESPONSE, &s->flags) && |
| 236 | !test_and_set_bit(BNEP_SETUP_RSP_SENT, &s->flags)) |
| 237 | err = bnep_send_rsp(s, BNEP_SETUP_CONN_RSP, |
| 238 | BNEP_SUCCESS); |
| 239 | else |
| 240 | err = bnep_send_rsp(s, BNEP_SETUP_CONN_RSP, |
| 241 | BNEP_CONN_NOT_ALLOWED); |
Vikram Kandukuri | cde9f80 | 2009-12-03 15:12:51 +0530 | [diff] [blame] | 242 | break; |
| 243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | default: { |
| 245 | u8 pkt[3]; |
| 246 | pkt[0] = BNEP_CONTROL; |
| 247 | pkt[1] = BNEP_CMD_NOT_UNDERSTOOD; |
| 248 | pkt[2] = cmd; |
Grzegorz Kolodziejczyk | e0fdbab | 2015-04-03 12:14:52 +0200 | [diff] [blame] | 249 | err = bnep_send(s, pkt, sizeof(pkt)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 250 | } |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | return err; |
| 255 | } |
| 256 | |
| 257 | static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb) |
| 258 | { |
| 259 | struct bnep_ext_hdr *h; |
| 260 | int err = 0; |
| 261 | |
| 262 | do { |
| 263 | h = (void *) skb->data; |
| 264 | if (!skb_pull(skb, sizeof(*h))) { |
| 265 | err = -EILSEQ; |
| 266 | break; |
| 267 | } |
| 268 | |
| 269 | BT_DBG("type 0x%x len %d", h->type, h->len); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 270 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | switch (h->type & BNEP_TYPE_MASK) { |
| 272 | case BNEP_EXT_CONTROL: |
| 273 | bnep_rx_control(s, skb->data, skb->len); |
| 274 | break; |
| 275 | |
| 276 | default: |
| 277 | /* Unknown extension, skip it. */ |
| 278 | break; |
| 279 | } |
| 280 | |
| 281 | if (!skb_pull(skb, h->len)) { |
| 282 | err = -EILSEQ; |
| 283 | break; |
| 284 | } |
| 285 | } while (!err && (h->type & BNEP_EXT_HEADER)); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 286 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | return err; |
| 288 | } |
| 289 | |
| 290 | static u8 __bnep_rx_hlen[] = { |
| 291 | ETH_HLEN, /* BNEP_GENERAL */ |
| 292 | 0, /* BNEP_CONTROL */ |
| 293 | 2, /* BNEP_COMPRESSED */ |
| 294 | ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */ |
| 295 | ETH_ALEN + 2 /* BNEP_COMPRESSED_DST_ONLY */ |
| 296 | }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 298 | static int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | { |
| 300 | struct net_device *dev = s->dev; |
| 301 | struct sk_buff *nskb; |
Grzegorz Kolodziejczyk | bf8b9a9 | 2015-04-03 12:14:54 +0200 | [diff] [blame] | 302 | u8 type, ctrl_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 304 | dev->stats.rx_bytes += skb->len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 306 | type = *(u8 *) skb->data; |
| 307 | skb_pull(skb, 1); |
Grzegorz Kolodziejczyk | bf8b9a9 | 2015-04-03 12:14:54 +0200 | [diff] [blame] | 308 | ctrl_type = *(u8 *)skb->data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | |
Szymon Janc | a3d9bd4 | 2011-03-21 14:19:57 +0100 | [diff] [blame] | 310 | if ((type & BNEP_TYPE_MASK) >= sizeof(__bnep_rx_hlen)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | goto badframe; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 312 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) { |
Grzegorz Kolodziejczyk | bf8b9a9 | 2015-04-03 12:14:54 +0200 | [diff] [blame] | 314 | if (bnep_rx_control(s, skb->data, skb->len) < 0) { |
| 315 | dev->stats.tx_errors++; |
| 316 | kfree_skb(skb); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | if (!(type & BNEP_EXT_HEADER)) { |
| 321 | kfree_skb(skb); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | /* Verify and pull ctrl message since it's already processed */ |
| 326 | switch (ctrl_type) { |
| 327 | case BNEP_SETUP_CONN_REQ: |
| 328 | /* Pull: ctrl type (1 b), len (1 b), data (len bytes) */ |
| 329 | if (!skb_pull(skb, 2 + *(u8 *)(skb->data + 1) * 2)) |
| 330 | goto badframe; |
| 331 | break; |
| 332 | case BNEP_FILTER_MULTI_ADDR_SET: |
| 333 | case BNEP_FILTER_NET_TYPE_SET: |
| 334 | /* Pull: ctrl type (1 b), len (2 b), data (len bytes) */ |
| 335 | if (!skb_pull(skb, 3 + *(u16 *)(skb->data + 1) * 2)) |
| 336 | goto badframe; |
| 337 | break; |
| 338 | default: |
| 339 | kfree_skb(skb); |
| 340 | return 0; |
| 341 | } |
| 342 | } else { |
| 343 | skb_reset_mac_header(skb); |
| 344 | |
| 345 | /* Verify and pull out header */ |
| 346 | if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK])) |
| 347 | goto badframe; |
| 348 | |
| 349 | s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 352 | if (type & BNEP_EXT_HEADER) { |
| 353 | if (bnep_rx_extension(s, skb) < 0) |
| 354 | goto badframe; |
| 355 | } |
| 356 | |
| 357 | /* Strip 802.1p header */ |
Eldad Zack | 000092b | 2012-05-08 00:09:35 +0200 | [diff] [blame] | 358 | if (ntohs(s->eh.h_proto) == ETH_P_8021Q) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 359 | if (!skb_pull(skb, 4)) |
| 360 | goto badframe; |
Al Viro | 1bc5d44 | 2006-11-08 00:27:57 -0800 | [diff] [blame] | 361 | s->eh.h_proto = get_unaligned((__be16 *) (skb->data - 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 362 | } |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 363 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | /* We have to alloc new skb and copy data here :(. Because original skb |
| 365 | * may not be modified and because of the alignment requirements. */ |
| 366 | nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL); |
| 367 | if (!nskb) { |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 368 | dev->stats.rx_dropped++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | kfree_skb(skb); |
| 370 | return -ENOMEM; |
| 371 | } |
| 372 | skb_reserve(nskb, 2); |
| 373 | |
| 374 | /* Decompress header and construct ether frame */ |
| 375 | switch (type & BNEP_TYPE_MASK) { |
| 376 | case BNEP_COMPRESSED: |
| 377 | memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN); |
| 378 | break; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 380 | case BNEP_COMPRESSED_SRC_ONLY: |
| 381 | memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN); |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 382 | memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb), ETH_ALEN); |
Al Viro | 1bc5d44 | 2006-11-08 00:27:57 -0800 | [diff] [blame] | 383 | put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | break; |
| 385 | |
| 386 | case BNEP_COMPRESSED_DST_ONLY: |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 387 | memcpy(__skb_put(nskb, ETH_ALEN), skb_mac_header(skb), |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 388 | ETH_ALEN); |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 389 | memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source, |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 390 | ETH_ALEN + 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 391 | break; |
| 392 | |
| 393 | case BNEP_GENERAL: |
Arnaldo Carvalho de Melo | 98e399f | 2007-03-19 15:33:04 -0700 | [diff] [blame] | 394 | memcpy(__skb_put(nskb, ETH_ALEN * 2), skb_mac_header(skb), |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 395 | ETH_ALEN * 2); |
Al Viro | 1bc5d44 | 2006-11-08 00:27:57 -0800 | [diff] [blame] | 396 | put_unaligned(s->eh.h_proto, (__be16 *) __skb_put(nskb, 2)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 397 | break; |
| 398 | } |
| 399 | |
Arnaldo Carvalho de Melo | d626f62 | 2007-03-27 18:55:52 -0300 | [diff] [blame] | 400 | skb_copy_from_linear_data(skb, __skb_put(nskb, skb->len), skb->len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | kfree_skb(skb); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 402 | |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 403 | dev->stats.rx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | nskb->ip_summed = CHECKSUM_NONE; |
| 405 | nskb->protocol = eth_type_trans(nskb, dev); |
| 406 | netif_rx_ni(nskb); |
| 407 | return 0; |
| 408 | |
| 409 | badframe: |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 410 | dev->stats.rx_errors++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | kfree_skb(skb); |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | static u8 __bnep_tx_types[] = { |
| 416 | BNEP_GENERAL, |
| 417 | BNEP_COMPRESSED_SRC_ONLY, |
| 418 | BNEP_COMPRESSED_DST_ONLY, |
| 419 | BNEP_COMPRESSED |
| 420 | }; |
| 421 | |
Gustavo Padovan | 6039aa7 | 2012-05-23 04:04:18 -0300 | [diff] [blame] | 422 | static int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
| 424 | struct ethhdr *eh = (void *) skb->data; |
| 425 | struct socket *sock = s->sock; |
| 426 | struct kvec iv[3]; |
| 427 | int len = 0, il = 0; |
| 428 | u8 type = 0; |
| 429 | |
| 430 | BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type); |
| 431 | |
| 432 | if (!skb->dev) { |
| 433 | /* Control frame sent by us */ |
| 434 | goto send; |
| 435 | } |
| 436 | |
| 437 | iv[il++] = (struct kvec) { &type, 1 }; |
| 438 | len++; |
| 439 | |
Joe Perches | c47fc98 | 2012-05-08 18:56:51 +0000 | [diff] [blame] | 440 | if (compress_src && ether_addr_equal(eh->h_dest, s->eh.h_source)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | type |= 0x01; |
| 442 | |
Joe Perches | c47fc98 | 2012-05-08 18:56:51 +0000 | [diff] [blame] | 443 | if (compress_dst && ether_addr_equal(eh->h_source, s->eh.h_dest)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 444 | type |= 0x02; |
| 445 | |
| 446 | if (type) |
| 447 | skb_pull(skb, ETH_ALEN * 2); |
| 448 | |
| 449 | type = __bnep_tx_types[type]; |
| 450 | switch (type) { |
| 451 | case BNEP_COMPRESSED_SRC_ONLY: |
| 452 | iv[il++] = (struct kvec) { eh->h_source, ETH_ALEN }; |
| 453 | len += ETH_ALEN; |
| 454 | break; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 455 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | case BNEP_COMPRESSED_DST_ONLY: |
| 457 | iv[il++] = (struct kvec) { eh->h_dest, ETH_ALEN }; |
| 458 | len += ETH_ALEN; |
| 459 | break; |
| 460 | } |
| 461 | |
| 462 | send: |
| 463 | iv[il++] = (struct kvec) { skb->data, skb->len }; |
| 464 | len += skb->len; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 465 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 466 | /* FIXME: linearize skb */ |
| 467 | { |
| 468 | len = kernel_sendmsg(sock, &s->msg, iv, il, len); |
| 469 | } |
| 470 | kfree_skb(skb); |
| 471 | |
| 472 | if (len > 0) { |
Stephen Hemminger | b4d7f0a | 2009-01-07 17:23:17 -0800 | [diff] [blame] | 473 | s->dev->stats.tx_bytes += len; |
| 474 | s->dev->stats.tx_packets++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | return 0; |
| 476 | } |
| 477 | |
| 478 | return len; |
| 479 | } |
| 480 | |
| 481 | static int bnep_session(void *arg) |
| 482 | { |
| 483 | struct bnep_session *s = arg; |
| 484 | struct net_device *dev = s->dev; |
| 485 | struct sock *sk = s->sock->sk; |
| 486 | struct sk_buff *skb; |
| 487 | wait_queue_t wait; |
| 488 | |
| 489 | BT_DBG(""); |
| 490 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | set_user_nice(current, -15); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
| 493 | init_waitqueue_entry(&wait, current); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 494 | add_wait_queue(sk_sleep(sk), &wait); |
Peter Hurley | 38d5755 | 2011-07-24 00:11:07 -0400 | [diff] [blame] | 495 | while (1) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | set_current_state(TASK_INTERRUPTIBLE); |
| 497 | |
Peter Hurley | 751c10a | 2011-08-05 10:41:35 -0400 | [diff] [blame] | 498 | if (atomic_read(&s->terminate)) |
Peter Hurley | 38d5755 | 2011-07-24 00:11:07 -0400 | [diff] [blame] | 499 | break; |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 500 | /* RX */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 501 | while ((skb = skb_dequeue(&sk->sk_receive_queue))) { |
| 502 | skb_orphan(skb); |
Mat Martineau | 4493572 | 2011-07-22 14:53:58 -0700 | [diff] [blame] | 503 | if (!skb_linearize(skb)) |
| 504 | bnep_rx_frame(s, skb); |
| 505 | else |
| 506 | kfree_skb(skb); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | } |
| 508 | |
| 509 | if (sk->sk_state != BT_CONNECTED) |
| 510 | break; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 511 | |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 512 | /* TX */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | while ((skb = skb_dequeue(&sk->sk_write_queue))) |
| 514 | if (bnep_tx_frame(s, skb)) |
| 515 | break; |
| 516 | netif_wake_queue(dev); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 517 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 518 | schedule(); |
| 519 | } |
Peter Hurley | 38d5755 | 2011-07-24 00:11:07 -0400 | [diff] [blame] | 520 | __set_current_state(TASK_RUNNING); |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 521 | remove_wait_queue(sk_sleep(sk), &wait); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | |
| 523 | /* Cleanup session */ |
| 524 | down_write(&bnep_session_sem); |
| 525 | |
| 526 | /* Delete network device */ |
| 527 | unregister_netdev(dev); |
| 528 | |
Marcel Holtmann | ec8dab3 | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 529 | /* Wakeup user-space polling for socket errors */ |
| 530 | s->sock->sk->sk_err = EUNATCH; |
| 531 | |
Eric Dumazet | aa39514 | 2010-04-20 13:03:51 +0000 | [diff] [blame] | 532 | wake_up_interruptible(sk_sleep(s->sock->sk)); |
Marcel Holtmann | ec8dab3 | 2008-07-14 20:13:53 +0200 | [diff] [blame] | 533 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | /* Release the socket */ |
| 535 | fput(s->sock->file); |
| 536 | |
| 537 | __bnep_unlink_session(s); |
| 538 | |
| 539 | up_write(&bnep_session_sem); |
| 540 | free_netdev(dev); |
David Herrmann | 9b338c3 | 2011-11-19 13:23:33 +0100 | [diff] [blame] | 541 | module_put_and_exit(0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | return 0; |
| 543 | } |
| 544 | |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 545 | static struct device *bnep_get_device(struct bnep_session *session) |
| 546 | { |
Johan Hedberg | 88d9077 | 2015-02-03 10:01:13 +0200 | [diff] [blame] | 547 | struct l2cap_conn *conn = l2cap_pi(session->sock->sk)->chan->conn; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 548 | |
Johan Hedberg | 88d9077 | 2015-02-03 10:01:13 +0200 | [diff] [blame] | 549 | if (!conn || !conn->hcon) |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 550 | return NULL; |
| 551 | |
Johan Hedberg | 88d9077 | 2015-02-03 10:01:13 +0200 | [diff] [blame] | 552 | return &conn->hcon->dev; |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 553 | } |
| 554 | |
Marcel Holtmann | 384912e | 2009-08-31 21:08:19 +0000 | [diff] [blame] | 555 | static struct device_type bnep_type = { |
| 556 | .name = "bluetooth", |
| 557 | }; |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | int bnep_add_connection(struct bnep_connadd_req *req, struct socket *sock) |
| 560 | { |
Grzegorz Kolodziejczyk | 836a061 | 2015-04-03 12:14:55 +0200 | [diff] [blame] | 561 | u32 valid_flags = BIT(BNEP_SETUP_RESPONSE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | struct net_device *dev; |
| 563 | struct bnep_session *s, *ss; |
| 564 | u8 dst[ETH_ALEN], src[ETH_ALEN]; |
| 565 | int err; |
| 566 | |
| 567 | BT_DBG(""); |
| 568 | |
Al Viro | 71bb99a | 2014-12-19 06:20:59 +0000 | [diff] [blame] | 569 | if (!l2cap_is_socket(sock)) |
| 570 | return -EBADFD; |
| 571 | |
Marcel Holtmann | 0151e42 | 2015-04-01 13:51:54 -0700 | [diff] [blame] | 572 | if (req->flags & ~valid_flags) |
| 573 | return -EINVAL; |
| 574 | |
Marcel Holtmann | 65f53e9 | 2013-10-13 09:49:57 -0700 | [diff] [blame] | 575 | baswap((void *) dst, &l2cap_pi(sock->sk)->chan->dst); |
| 576 | baswap((void *) src, &l2cap_pi(sock->sk)->chan->src); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | |
| 578 | /* session struct allocated as private part of net_device */ |
| 579 | dev = alloc_netdev(sizeof(struct bnep_session), |
Tom Gundersen | c835a67 | 2014-07-14 16:37:24 +0200 | [diff] [blame] | 580 | (*req->device) ? req->device : "bnep%d", |
| 581 | NET_NAME_UNKNOWN, |
| 582 | bnep_net_setup); |
Tobias Klauser | 67b52e5 | 2006-03-21 23:53:16 -0800 | [diff] [blame] | 583 | if (!dev) |
| 584 | return -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 585 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | down_write(&bnep_session_sem); |
| 587 | |
| 588 | ss = __bnep_get_session(dst); |
| 589 | if (ss && ss->state == BT_CONNECTED) { |
| 590 | err = -EEXIST; |
| 591 | goto failed; |
| 592 | } |
| 593 | |
Wang Chen | 524ad0a | 2008-11-12 23:39:10 -0800 | [diff] [blame] | 594 | s = netdev_priv(dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | |
| 596 | /* This is rx header therefore addresses are swapped. |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 597 | * ie. eh.h_dest is our local address. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | memcpy(s->eh.h_dest, &src, ETH_ALEN); |
| 599 | memcpy(s->eh.h_source, &dst, ETH_ALEN); |
| 600 | memcpy(dev->dev_addr, s->eh.h_dest, ETH_ALEN); |
| 601 | |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 602 | s->dev = dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | s->sock = sock; |
| 604 | s->role = req->role; |
| 605 | s->state = BT_CONNECTED; |
Grzegorz Kolodziejczyk | 836a061 | 2015-04-03 12:14:55 +0200 | [diff] [blame] | 606 | s->flags = req->flags; |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 607 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | s->msg.msg_flags = MSG_NOSIGNAL; |
| 609 | |
| 610 | #ifdef CONFIG_BT_BNEP_MC_FILTER |
Danny Schweizer | 4ada128 | 2015-12-11 10:04:54 +0100 | [diff] [blame] | 611 | /* Set default mc filter to not filter out any mc addresses |
| 612 | * as defined in the BNEP specification (revision 0.95a) |
| 613 | * http://grouper.ieee.org/groups/802/15/Bluetooth/BNEP.pdf |
| 614 | */ |
| 615 | s->mc_filter = ~0LL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 616 | #endif |
| 617 | |
| 618 | #ifdef CONFIG_BT_BNEP_PROTO_FILTER |
| 619 | /* Set default protocol filter */ |
| 620 | bnep_set_default_proto_filter(s); |
| 621 | #endif |
| 622 | |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 623 | SET_NETDEV_DEV(dev, bnep_get_device(s)); |
Marcel Holtmann | 384912e | 2009-08-31 21:08:19 +0000 | [diff] [blame] | 624 | SET_NETDEV_DEVTYPE(dev, &bnep_type); |
Marcel Holtmann | 0a85b96 | 2006-07-06 13:09:02 +0200 | [diff] [blame] | 625 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 626 | err = register_netdev(dev); |
Szymon Janc | 3aad75a | 2011-03-21 14:19:58 +0100 | [diff] [blame] | 627 | if (err) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | goto failed; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | |
| 630 | __bnep_link_session(s); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 631 | |
David Herrmann | 9b338c3 | 2011-11-19 13:23:33 +0100 | [diff] [blame] | 632 | __module_get(THIS_MODULE); |
Szymon Janc | f4d7cd4 | 2011-03-21 14:20:00 +0100 | [diff] [blame] | 633 | s->task = kthread_run(bnep_session, s, "kbnepd %s", dev->name); |
| 634 | if (IS_ERR(s->task)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | /* Session thread start failed, gotta cleanup. */ |
David Herrmann | 9b338c3 | 2011-11-19 13:23:33 +0100 | [diff] [blame] | 636 | module_put(THIS_MODULE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | unregister_netdev(dev); |
| 638 | __bnep_unlink_session(s); |
Szymon Janc | f4d7cd4 | 2011-03-21 14:20:00 +0100 | [diff] [blame] | 639 | err = PTR_ERR(s->task); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 640 | goto failed; |
| 641 | } |
| 642 | |
| 643 | up_write(&bnep_session_sem); |
| 644 | strcpy(req->device, dev->name); |
| 645 | return 0; |
| 646 | |
| 647 | failed: |
| 648 | up_write(&bnep_session_sem); |
| 649 | free_netdev(dev); |
| 650 | return err; |
| 651 | } |
| 652 | |
| 653 | int bnep_del_connection(struct bnep_conndel_req *req) |
| 654 | { |
Marcel Holtmann | 0151e42 | 2015-04-01 13:51:54 -0700 | [diff] [blame] | 655 | u32 valid_flags = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 656 | struct bnep_session *s; |
| 657 | int err = 0; |
| 658 | |
| 659 | BT_DBG(""); |
| 660 | |
Marcel Holtmann | 0151e42 | 2015-04-01 13:51:54 -0700 | [diff] [blame] | 661 | if (req->flags & ~valid_flags) |
| 662 | return -EINVAL; |
| 663 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | down_read(&bnep_session_sem); |
| 665 | |
| 666 | s = __bnep_get_session(req->dst); |
Peter Hurley | 751c10a | 2011-08-05 10:41:35 -0400 | [diff] [blame] | 667 | if (s) { |
| 668 | atomic_inc(&s->terminate); |
| 669 | wake_up_process(s->task); |
| 670 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | err = -ENOENT; |
| 672 | |
| 673 | up_read(&bnep_session_sem); |
| 674 | return err; |
| 675 | } |
| 676 | |
| 677 | static void __bnep_copy_ci(struct bnep_conninfo *ci, struct bnep_session *s) |
| 678 | { |
Grzegorz Kolodziejczyk | 836a061 | 2015-04-03 12:14:55 +0200 | [diff] [blame] | 679 | u32 valid_flags = BIT(BNEP_SETUP_RESPONSE); |
Marcel Holtmann | 0151e42 | 2015-04-01 13:51:54 -0700 | [diff] [blame] | 680 | |
Vasiliy Kulikov | 5520d20 | 2010-10-30 18:26:21 +0400 | [diff] [blame] | 681 | memset(ci, 0, sizeof(*ci)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 682 | memcpy(ci->dst, s->eh.h_source, ETH_ALEN); |
| 683 | strcpy(ci->device, s->dev->name); |
Marcel Holtmann | 0151e42 | 2015-04-01 13:51:54 -0700 | [diff] [blame] | 684 | ci->flags = s->flags & valid_flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 685 | ci->state = s->state; |
| 686 | ci->role = s->role; |
| 687 | } |
| 688 | |
| 689 | int bnep_get_connlist(struct bnep_connlist_req *req) |
| 690 | { |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 691 | struct bnep_session *s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 692 | int err = 0, n = 0; |
| 693 | |
| 694 | down_read(&bnep_session_sem); |
| 695 | |
Luiz Augusto von Dentz | 8035ded | 2011-11-01 10:58:56 +0200 | [diff] [blame] | 696 | list_for_each_entry(s, &bnep_session_list, list) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | struct bnep_conninfo ci; |
| 698 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | __bnep_copy_ci(&ci, s); |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 701 | if (copy_to_user(req->ci, &ci, sizeof(ci))) { |
| 702 | err = -EFAULT; |
| 703 | break; |
| 704 | } |
| 705 | |
| 706 | if (++n >= req->cnum) |
| 707 | break; |
| 708 | |
| 709 | req->ci++; |
| 710 | } |
| 711 | req->cnum = n; |
| 712 | |
| 713 | up_read(&bnep_session_sem); |
| 714 | return err; |
| 715 | } |
| 716 | |
| 717 | int bnep_get_conninfo(struct bnep_conninfo *ci) |
| 718 | { |
| 719 | struct bnep_session *s; |
| 720 | int err = 0; |
| 721 | |
| 722 | down_read(&bnep_session_sem); |
| 723 | |
| 724 | s = __bnep_get_session(ci->dst); |
| 725 | if (s) |
| 726 | __bnep_copy_ci(ci, s); |
| 727 | else |
| 728 | err = -ENOENT; |
| 729 | |
| 730 | up_read(&bnep_session_sem); |
| 731 | return err; |
| 732 | } |
| 733 | |
| 734 | static int __init bnep_init(void) |
YOSHIFUJI Hideaki | 8e87d14 | 2007-02-09 23:24:33 +0900 | [diff] [blame] | 735 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 736 | char flt[50] = ""; |
| 737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 738 | #ifdef CONFIG_BT_BNEP_PROTO_FILTER |
| 739 | strcat(flt, "protocol "); |
| 740 | #endif |
| 741 | |
| 742 | #ifdef CONFIG_BT_BNEP_MC_FILTER |
| 743 | strcat(flt, "multicast"); |
| 744 | #endif |
| 745 | |
| 746 | BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION); |
| 747 | if (flt[0]) |
| 748 | BT_INFO("BNEP filters: %s", flt); |
| 749 | |
| 750 | bnep_sock_init(); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | static void __exit bnep_exit(void) |
| 755 | { |
| 756 | bnep_sock_cleanup(); |
| 757 | } |
| 758 | |
| 759 | module_init(bnep_init); |
| 760 | module_exit(bnep_exit); |
| 761 | |
Marcel Holtmann | 28111eb | 2008-08-07 22:26:54 +0200 | [diff] [blame] | 762 | module_param(compress_src, bool, 0644); |
| 763 | MODULE_PARM_DESC(compress_src, "Compress sources headers"); |
| 764 | |
| 765 | module_param(compress_dst, bool, 0644); |
| 766 | MODULE_PARM_DESC(compress_dst, "Compress destination headers"); |
| 767 | |
Marcel Holtmann | 63fbd24 | 2008-08-18 13:23:53 +0200 | [diff] [blame] | 768 | MODULE_AUTHOR("Marcel Holtmann <marcel@holtmann.org>"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION); |
| 770 | MODULE_VERSION(VERSION); |
| 771 | MODULE_LICENSE("GPL"); |
| 772 | MODULE_ALIAS("bt-proto-4"); |