Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 | /* BNEP implementation for Linux Bluetooth stack (BlueZ). Copyright (C) 2001-2002 Inventel Systemes Written 2001-2002 by Clément Moreau <clement.moreau@inventel.fr> David Libault <david.libault@inventel.fr> Copyright (C) 2002 Maxim Krasnyanskiy <maxk@qualcomm.com> This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF THIRD PARTY RIGHTS. IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) AND AUTHOR(S) BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES, OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ALL LIABILITY, INCLUDING LIABILITY FOR INFRINGEMENT OF ANY PATENTS, COPYRIGHTS, TRADEMARKS OR OTHER RIGHTS, RELATING TO USE OF THIS SOFTWARE IS DISCLAIMED. */ /* * $Id: core.c,v 1.20 2002/08/04 21:23:58 maxk Exp $ */ #define __KERNEL_SYSCALLS__ #include <linux/config.h> #include <linux/module.h> #include <linux/kernel.h> #include <linux/sched.h> #include <linux/signal.h> #include <linux/init.h> #include <linux/wait.h> #include <linux/errno.h> #include <linux/smp_lock.h> #include <linux/net.h> #include <net/sock.h> #include <linux/socket.h> #include <linux/file.h> #include <linux/netdevice.h> #include <linux/etherdevice.h> #include <linux/skbuff.h> #include <asm/unaligned.h> #include <net/bluetooth/bluetooth.h> #include <net/bluetooth/l2cap.h> #include "bnep.h" #ifndef CONFIG_BT_BNEP_DEBUG #undef BT_DBG #define BT_DBG(D...) #endif #define VERSION "1.0" static LIST_HEAD(bnep_session_list); static DECLARE_RWSEM(bnep_session_sem); static struct bnep_session *__bnep_get_session(u8 *dst) { struct bnep_session *s; struct list_head *p; BT_DBG(""); list_for_each(p, &bnep_session_list) { s = list_entry(p, struct bnep_session, list); if (!memcmp(dst, s->eh.h_source, ETH_ALEN)) return s; } return NULL; } static void __bnep_link_session(struct bnep_session *s) { MOD_INC_USE_COUNT; list_add(&s->list, &bnep_session_list); } static void __bnep_unlink_session(struct bnep_session *s) { list_del(&s->list); MOD_DEC_USE_COUNT; } static int bnep_send(struct bnep_session *s, void *data, size_t len) { struct kiocb iocb; struct sock_iocb *si; struct socket *sock = s->sock; struct iovec iv = { data, len }; int err; s->msg.msg_iov = &iv; s->msg.msg_iovlen = 1; init_sync_kiocb(&iocb, NULL); si = kiocb_to_siocb(&iocb); si->scm = NULL; si->sock = sock; si->msg = &s->msg; si->size = len; err = sock->ops->sendmsg(&iocb, sock, &s->msg, len, NULL); if (-EIOCBQUEUED == err) err = wait_on_sync_kiocb(&iocb); return err; } static int bnep_send_rsp(struct bnep_session *s, u8 ctrl, u16 resp) { struct bnep_control_rsp rsp; rsp.type = BNEP_CONTROL; rsp.ctrl = ctrl; rsp.resp = htons(resp); return bnep_send(s, &rsp, sizeof(rsp)); } static int bnep_ctrl_set_netfilter(struct bnep_session *s, struct sk_buff *skb) { u16 *data; int n; data = (void *) skb->data; if (!skb_pull(skb, 2)) return -EILSEQ; n = ntohs(get_unaligned(data)); data = (void *) skb->data; if (!skb_pull(skb, n)) return -EILSEQ; BT_DBG("filter len %d", n); #ifdef CONFIG_BT_BNEP_PROTO_FILTER n /= 4; if (n <= BNEP_MAX_PROTO_FILTERS) { struct bnep_proto_filter *f = s->proto_filter; int i; for (i = 0; i < n; i++) { f[i].start = get_unaligned(data++); f[i].end = get_unaligned(data++); BT_DBG("proto filter start %d end %d", f[i].start, f[i].end); } if (i < BNEP_MAX_PROTO_FILTERS) memset(f + i, 0, sizeof(*f)); bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_SUCCESS); } else { bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_LIMIT_REACHED); } #else bnep_send_rsp(s, BNEP_FILTER_NET_TYPE_RSP, BNEP_FILTER_UNSUPPORTED_REQ); #endif return 0; } static int bnep_ctrl_set_mcfilter(struct bnep_session *s, struct sk_buff *skb) { u8 *data; int n; data = (void *) skb->data; if (!skb_pull(skb, 2)) return -EILSEQ; n = ntohs(get_unaligned((u16 *) data)); data = (void *) skb->data; if (!skb_pull(skb, n)) return -EILSEQ; BT_DBG("filter len %d", n); #ifdef CONFIG_BT_BNEP_MC_FILTER n /= (ETH_ALEN * 2); if (n > 0) { s->mc_filter = 0; /* Always send broadcast */ set_bit(bnep_mc_hash(s->dev.broadcast), (ulong *) &s->mc_filter); /* Add address ranges to the multicast hash */ for (; n > 0; n--) { u8 a1[6], *a2; memcpy(a1, data, ETH_ALEN); data += ETH_ALEN; a2 = data; data += ETH_ALEN; BT_DBG("mc filter %s -> %s", batostr((void *) a1), batostr((void *) a2)); #define INCA(a) { int i = 5; while (i >=0 && ++a[i--] == 0); } /* Iterate from a1 to a2 */ set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter); while (memcmp(a1, a2, 6) < 0 && s->mc_filter != ~0LL) { INCA(a1); set_bit(bnep_mc_hash(a1), (ulong *) &s->mc_filter); } } } BT_DBG("mc filter hash 0x%llx", s->mc_filter); bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_SUCCESS); #else bnep_send_rsp(s, BNEP_FILTER_MULTI_ADDR_RSP, BNEP_FILTER_UNSUPPORTED_REQ); #endif return 0; } static int bnep_rx_control(struct bnep_session *s, struct sk_buff *skb) { int err = 0; u8 cmd = *(u8 *) skb->data; skb_pull(skb, 1); switch (cmd) { case BNEP_CMD_NOT_UNDERSTOOD: case BNEP_SETUP_CONN_REQ: case BNEP_SETUP_CONN_RSP: case BNEP_FILTER_NET_TYPE_RSP: case BNEP_FILTER_MULTI_ADDR_RSP: /* Ignore these for now */ break; case BNEP_FILTER_NET_TYPE_SET: err = bnep_ctrl_set_netfilter(s, skb); break; case BNEP_FILTER_MULTI_ADDR_SET: err = bnep_ctrl_set_mcfilter(s, skb); break; default: { u8 pkt[3]; pkt[0] = BNEP_CONTROL; pkt[1] = BNEP_CMD_NOT_UNDERSTOOD; pkt[2] = cmd; bnep_send(s, pkt, sizeof(pkt)); } break; } return err; } static int bnep_rx_extension(struct bnep_session *s, struct sk_buff *skb) { struct bnep_ext_hdr *h; int err = 0; do { h = (void *) skb->data; if (!skb_pull(skb, sizeof(*h))) { err = -EILSEQ; break; } BT_DBG("type 0x%x len %d", h->type, h->len); switch (h->type & BNEP_TYPE_MASK) { case BNEP_EXT_CONTROL: err = bnep_rx_control(s, skb); break; default: /* Unknown extension */ if (!skb_pull(skb, h->len)) err = -EILSEQ; break; } } while (!err && (h->type & BNEP_EXT_HEADER)); return err; } static u8 __bnep_rx_hlen[] = { ETH_HLEN, /* BNEP_GENERAL */ 0, /* BNEP_CONTROL */ 2, /* BNEP_COMPRESSED */ ETH_ALEN + 2, /* BNEP_COMPRESSED_SRC_ONLY */ ETH_ALEN + 2 /* BNEP_COMPRESSED_DST_ONLY */ }; #define BNEP_RX_TYPES (sizeof(__bnep_rx_hlen) - 1) static inline int bnep_rx_frame(struct bnep_session *s, struct sk_buff *skb) { struct net_device *dev = &s->dev; struct sk_buff *nskb; u8 type; dev->last_rx = jiffies; s->stats.rx_bytes += skb->len; type = *(u8 *) skb->data; skb_pull(skb, 1); if ((type & BNEP_TYPE_MASK) > BNEP_RX_TYPES) goto badframe; if ((type & BNEP_TYPE_MASK) == BNEP_CONTROL) { bnep_rx_control(s, skb); kfree_skb(skb); return 0; } skb->mac.raw = skb->data; /* Verify and pull out header */ if (!skb_pull(skb, __bnep_rx_hlen[type & BNEP_TYPE_MASK])) goto badframe; s->eh.h_proto = get_unaligned((u16 *) (skb->data - 2)); if (type & BNEP_EXT_HEADER) { if (bnep_rx_extension(s, skb) < 0) goto badframe; } /* Strip 802.1p header */ if (ntohs(s->eh.h_proto) == 0x8100) { if (!skb_pull(skb, 4)) goto badframe; s->eh.h_proto = get_unaligned((u16 *) (skb->data - 2)); } /* We have to alloc new skb and copy data here :(. Because original skb * may not be modified and because of the alignment requirements. */ nskb = alloc_skb(2 + ETH_HLEN + skb->len, GFP_KERNEL); if (!nskb) { s->stats.rx_dropped++; kfree_skb(skb); return -ENOMEM; } skb_reserve(nskb, 2); /* Decompress header and construct ether frame */ switch (type & BNEP_TYPE_MASK) { case BNEP_COMPRESSED: memcpy(__skb_put(nskb, ETH_HLEN), &s->eh, ETH_HLEN); break; case BNEP_COMPRESSED_SRC_ONLY: memcpy(__skb_put(nskb, ETH_ALEN), s->eh.h_dest, ETH_ALEN); memcpy(__skb_put(nskb, ETH_ALEN), skb->mac.raw, ETH_ALEN); put_unaligned(s->eh.h_proto, (u16 *) __skb_put(nskb, 2)); break; case BNEP_COMPRESSED_DST_ONLY: memcpy(__skb_put(nskb, ETH_ALEN), skb->mac.raw, ETH_ALEN); memcpy(__skb_put(nskb, ETH_ALEN + 2), s->eh.h_source, ETH_ALEN + 2); break; case BNEP_GENERAL: memcpy(__skb_put(nskb, ETH_ALEN * 2), skb->mac.raw, ETH_ALEN * 2); put_unaligned(s->eh.h_proto, (u16 *) __skb_put(nskb, 2)); break; } memcpy(__skb_put(nskb, skb->len), skb->data, skb->len); kfree_skb(skb); s->stats.rx_packets++; nskb->dev = dev; nskb->ip_summed = CHECKSUM_UNNECESSARY; nskb->protocol = eth_type_trans(nskb, dev); netif_rx_ni(nskb); return 0; badframe: s->stats.rx_errors++; kfree_skb(skb); return 0; } static u8 __bnep_tx_types[] = { BNEP_GENERAL, BNEP_COMPRESSED_SRC_ONLY, BNEP_COMPRESSED_DST_ONLY, BNEP_COMPRESSED }; static inline int bnep_tx_frame(struct bnep_session *s, struct sk_buff *skb) { struct ethhdr *eh = (void *) skb->data; struct socket *sock = s->sock; struct iovec iv[3]; int len = 0, il = 0; u8 type = 0; BT_DBG("skb %p dev %p type %d", skb, skb->dev, skb->pkt_type); if (!skb->dev) { /* Control frame sent by us */ goto send; } iv[il++] = (struct iovec) { &type, 1 }; len++; if (!memcmp(eh->h_dest, s->eh.h_source, ETH_ALEN)) type |= 0x01; if (!memcmp(eh->h_source, s->eh.h_dest, ETH_ALEN)) type |= 0x02; if (type) skb_pull(skb, ETH_ALEN * 2); type = __bnep_tx_types[type]; switch (type) { case BNEP_COMPRESSED_SRC_ONLY: iv[il++] = (struct iovec) { eh->h_source, ETH_ALEN }; len += ETH_ALEN; break; case BNEP_COMPRESSED_DST_ONLY: iv[il++] = (struct iovec) { eh->h_dest, ETH_ALEN }; len += ETH_ALEN; break; } send: iv[il++] = (struct iovec) { skb->data, skb->len }; len += skb->len; /* FIXME: linearize skb */ { struct kiocb iocb; struct sock_iocb *si; s->msg.msg_iov = iv; s->msg.msg_iovlen = il; init_sync_kiocb(&iocb, NULL); si = kiocb_to_siocb(&iocb); si->scm = NULL; si->sock = sock; si->msg = &s->msg; si->size = len; len = sock->ops->sendmsg(&iocb, sock, &s->msg, len, NULL); if (-EIOCBQUEUED == len) len = wait_on_sync_kiocb(&iocb); } kfree_skb(skb); if (len > 0) { s->stats.tx_bytes += len; s->stats.tx_packets++; return 0; } return len; } static int bnep_session(void *arg) { struct bnep_session *s = arg; struct net_device *dev = &s->dev; struct sock *sk = s->sock->sk; struct sk_buff *skb; wait_queue_t wait; BT_DBG(""); daemonize(); set_user_nice(current, -15); current->flags |= PF_IOTHREAD; sigfillset(¤t->blocked); flush_signals(current); sprintf(current->comm, "kbnepd %s", dev->name); set_fs(KERNEL_DS); init_waitqueue_entry(&wait, current); add_wait_queue(sk->sleep, &wait); while (!atomic_read(&s->killed)) { set_current_state(TASK_INTERRUPTIBLE); // RX while ((skb = skb_dequeue(&sk->receive_queue))) { skb_orphan(skb); bnep_rx_frame(s, skb); } if (sk->state != BT_CONNECTED) break; // TX while ((skb = skb_dequeue(&sk->write_queue))) if (bnep_tx_frame(s, skb)) break; netif_wake_queue(dev); schedule(); } set_current_state(TASK_RUNNING); remove_wait_queue(sk->sleep, &wait); /* Cleanup session */ down_write(&bnep_session_sem); /* Delete network device */ unregister_netdev(dev); /* Release the socket */ fput(s->sock->file); __bnep_unlink_session(s); up_write(&bnep_session_sem); kfree(s); return 0; } int bnep_add_connection(struct bnep_conadd_req *req, struct socket *sock) { struct net_device *dev; struct bnep_session *s, *ss; u8 dst[ETH_ALEN], src[ETH_ALEN]; int err; BT_DBG(""); baswap((void *) dst, &bt_sk(sock->sk)->dst); baswap((void *) src, &bt_sk(sock->sk)->src); s = kmalloc(sizeof(struct bnep_session), GFP_KERNEL); if (!s) return -ENOMEM; memset(s, 0, sizeof(struct bnep_session)); down_write(&bnep_session_sem); ss = __bnep_get_session(dst); if (ss && ss->state == BT_CONNECTED) { err = -EEXIST; goto failed; } dev = &s->dev; if (*req->device) strcpy(dev->name, req->device); else strcpy(dev->name, "bnep%d"); memset(dev->broadcast, 0xff, ETH_ALEN); /* This is rx header therefor addresses are swaped. * ie eh.h_dest is our local address. */ memcpy(s->eh.h_dest, &src, ETH_ALEN); memcpy(s->eh.h_source, &dst, ETH_ALEN); s->sock = sock; s->role = req->role; s->state = BT_CONNECTED; s->msg.msg_flags = MSG_NOSIGNAL; #ifdef CONFIG_BT_BNEP_MC_FILTER /* Set default mc filter */ set_bit(bnep_mc_hash(dev->broadcast), (ulong *) &s->mc_filter); #endif #ifdef CONFIG_BT_BNEP_PROTO_FILTER /* Set default protocol filter */ /* (IPv4, ARP) */ s->proto_filter[0].start = htons(0x0800); s->proto_filter[0].end = htons(0x0806); /* (RARP, AppleTalk) */ s->proto_filter[1].start = htons(0x8035); s->proto_filter[1].end = htons(0x80F3); /* (IPX, IPv6) */ s->proto_filter[2].start = htons(0x8137); s->proto_filter[2].end = htons(0x86DD); #endif dev->init = bnep_net_init; dev->priv = s; err = register_netdev(dev); if (err) { goto failed; } __bnep_link_session(s); err = kernel_thread(bnep_session, s, CLONE_FS | CLONE_FILES | CLONE_SIGHAND); if (err < 0) { /* Session thread start failed, gotta cleanup. */ unregister_netdev(dev); __bnep_unlink_session(s); goto failed; } up_write(&bnep_session_sem); strcpy(req->device, dev->name); return 0; failed: up_write(&bnep_session_sem); kfree(s); return err; } int bnep_del_connection(struct bnep_condel_req *req) { struct bnep_session *s; int err = 0; BT_DBG(""); down_read(&bnep_session_sem); s = __bnep_get_session(req->dst); if (s) { /* Wakeup user-space which is polling for socket errors. * This is temporary hack untill we have shutdown in L2CAP */ s->sock->sk->err = EUNATCH; /* Kill session thread */ atomic_inc(&s->killed); wake_up_interruptible(s->sock->sk->sleep); } else err = -ENOENT; up_read(&bnep_session_sem); return err; } static void __bnep_copy_ci(struct bnep_coninfo *ci, struct bnep_session *s) { memcpy(ci->dst, s->eh.h_source, ETH_ALEN); strcpy(ci->device, s->dev.name); ci->flags = s->flags; ci->state = s->state; ci->role = s->role; } int bnep_get_conlist(struct bnep_conlist_req *req) { struct list_head *p; int err = 0, n = 0; down_read(&bnep_session_sem); list_for_each(p, &bnep_session_list) { struct bnep_session *s; struct bnep_coninfo ci; s = list_entry(p, struct bnep_session, list); __bnep_copy_ci(&ci, s); if (copy_to_user(req->ci, &ci, sizeof(ci))) { err = -EFAULT; break; } if (++n >= req->cnum) break; req->ci++; } req->cnum = n; up_read(&bnep_session_sem); return err; } int bnep_get_coninfo(struct bnep_coninfo *ci) { struct bnep_session *s; int err = 0; down_read(&bnep_session_sem); s = __bnep_get_session(ci->dst); if (s) __bnep_copy_ci(ci, s); else err = -ENOENT; up_read(&bnep_session_sem); return err; } static int __init bnep_init_module(void) { char flt[50] = ""; #ifdef CONFIG_BT_BNEP_PROTO_FILTER strcat(flt, "protocol "); #endif #ifdef CONFIG_BT_BNEP_MC_FILTER strcat(flt, "multicast"); #endif BT_INFO("BNEP (Ethernet Emulation) ver %s", VERSION); if (flt[0]) BT_INFO("BNEP filters: %s", flt); bnep_sock_init(); return 0; } static void __exit bnep_cleanup_module(void) { bnep_sock_cleanup(); } module_init(bnep_init_module); module_exit(bnep_cleanup_module); MODULE_DESCRIPTION("Bluetooth BNEP ver " VERSION); MODULE_AUTHOR("David Libault <david.libault@inventel.fr>, Maxim Krasnyanskiy <maxk@qualcomm.com>"); MODULE_LICENSE("GPL"); |