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