152d84ffcSAlexander Aring /* Copyright 2011, Siemens AG
252d84ffcSAlexander Aring * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
352d84ffcSAlexander Aring */
452d84ffcSAlexander Aring
552d84ffcSAlexander Aring /* Based on patches from Jon Smirl <jonsmirl@gmail.com>
652d84ffcSAlexander Aring * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com>
752d84ffcSAlexander Aring *
852d84ffcSAlexander Aring * This program is free software; you can redistribute it and/or modify
952d84ffcSAlexander Aring * it under the terms of the GNU General Public License version 2
1052d84ffcSAlexander Aring * as published by the Free Software Foundation.
1152d84ffcSAlexander Aring *
1252d84ffcSAlexander Aring * This program is distributed in the hope that it will be useful,
1352d84ffcSAlexander Aring * but WITHOUT ANY WARRANTY; without even the implied warranty of
1452d84ffcSAlexander Aring * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1552d84ffcSAlexander Aring * GNU General Public License for more details.
1652d84ffcSAlexander Aring */
1752d84ffcSAlexander Aring
1852d84ffcSAlexander Aring /* Jon's code is based on 6lowpan implementation for Contiki which is:
1952d84ffcSAlexander Aring * Copyright (c) 2008, Swedish Institute of Computer Science.
2052d84ffcSAlexander Aring * All rights reserved.
2152d84ffcSAlexander Aring *
2252d84ffcSAlexander Aring * Redistribution and use in source and binary forms, with or without
2352d84ffcSAlexander Aring * modification, are permitted provided that the following conditions
2452d84ffcSAlexander Aring * are met:
2552d84ffcSAlexander Aring * 1. Redistributions of source code must retain the above copyright
2652d84ffcSAlexander Aring * notice, this list of conditions and the following disclaimer.
2752d84ffcSAlexander Aring * 2. Redistributions in binary form must reproduce the above copyright
2852d84ffcSAlexander Aring * notice, this list of conditions and the following disclaimer in the
2952d84ffcSAlexander Aring * documentation and/or other materials provided with the distribution.
3052d84ffcSAlexander Aring * 3. Neither the name of the Institute nor the names of its contributors
3152d84ffcSAlexander Aring * may be used to endorse or promote products derived from this software
3252d84ffcSAlexander Aring * without specific prior written permission.
3352d84ffcSAlexander Aring *
3452d84ffcSAlexander Aring * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
3552d84ffcSAlexander Aring * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3652d84ffcSAlexander Aring * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3752d84ffcSAlexander Aring * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
3852d84ffcSAlexander Aring * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
3952d84ffcSAlexander Aring * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
4052d84ffcSAlexander Aring * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
4152d84ffcSAlexander Aring * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
4252d84ffcSAlexander Aring * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
4352d84ffcSAlexander Aring * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
4452d84ffcSAlexander Aring * SUCH DAMAGE.
4552d84ffcSAlexander Aring */
4652d84ffcSAlexander Aring
4752d84ffcSAlexander Aring #include <linux/module.h>
4852d84ffcSAlexander Aring #include <linux/netdevice.h>
4952d84ffcSAlexander Aring #include <linux/ieee802154.h>
50*c78b8b20SJakub Kicinski #include <linux/if_arp.h>
5152d84ffcSAlexander Aring
5252d84ffcSAlexander Aring #include <net/ipv6.h>
5352d84ffcSAlexander Aring
5452d84ffcSAlexander Aring #include "6lowpan_i.h"
5552d84ffcSAlexander Aring
5607512728SAlexander Aring static int open_count;
5707512728SAlexander Aring
5896a1c173SBhumika Goyal static const struct header_ops lowpan_header_ops = {
5952d84ffcSAlexander Aring .create = lowpan_header_create,
6052d84ffcSAlexander Aring };
6152d84ffcSAlexander Aring
lowpan_dev_init(struct net_device * ldev)621a33e10eSCong Wang static int lowpan_dev_init(struct net_device *ldev)
631a33e10eSCong Wang {
641a33e10eSCong Wang netdev_lockdep_set_classes(ldev);
651a33e10eSCong Wang
661a33e10eSCong Wang return 0;
671a33e10eSCong Wang }
681a33e10eSCong Wang
lowpan_open(struct net_device * dev)6990997af7SAlexander Aring static int lowpan_open(struct net_device *dev)
7090997af7SAlexander Aring {
7190997af7SAlexander Aring if (!open_count)
7290997af7SAlexander Aring lowpan_rx_init();
7390997af7SAlexander Aring open_count++;
7490997af7SAlexander Aring return 0;
7590997af7SAlexander Aring }
7690997af7SAlexander Aring
lowpan_stop(struct net_device * dev)7790997af7SAlexander Aring static int lowpan_stop(struct net_device *dev)
7890997af7SAlexander Aring {
7990997af7SAlexander Aring open_count--;
8090997af7SAlexander Aring if (!open_count)
8190997af7SAlexander Aring lowpan_rx_exit();
8290997af7SAlexander Aring return 0;
8390997af7SAlexander Aring }
8490997af7SAlexander Aring
lowpan_neigh_construct(struct net_device * dev,struct neighbour * n)85503eebc2SJiri Pirko static int lowpan_neigh_construct(struct net_device *dev, struct neighbour *n)
868626a0c8SAlexander Aring {
878626a0c8SAlexander Aring struct lowpan_802154_neigh *neigh = lowpan_802154_neigh(neighbour_priv(n));
888626a0c8SAlexander Aring
898626a0c8SAlexander Aring /* default no short_addr is available for a neighbour */
908626a0c8SAlexander Aring neigh->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC);
918626a0c8SAlexander Aring return 0;
928626a0c8SAlexander Aring }
938626a0c8SAlexander Aring
lowpan_get_iflink(const struct net_device * dev)94b30c122cSLubomir Rintel static int lowpan_get_iflink(const struct net_device *dev)
95b30c122cSLubomir Rintel {
96b30c122cSLubomir Rintel return lowpan_802154_dev(dev)->wdev->ifindex;
97b30c122cSLubomir Rintel }
98b30c122cSLubomir Rintel
9952d84ffcSAlexander Aring static const struct net_device_ops lowpan_netdev_ops = {
1001a33e10eSCong Wang .ndo_init = lowpan_dev_init,
10152d84ffcSAlexander Aring .ndo_start_xmit = lowpan_xmit,
10290997af7SAlexander Aring .ndo_open = lowpan_open,
10390997af7SAlexander Aring .ndo_stop = lowpan_stop,
1048626a0c8SAlexander Aring .ndo_neigh_construct = lowpan_neigh_construct,
105b30c122cSLubomir Rintel .ndo_get_iflink = lowpan_get_iflink,
10652d84ffcSAlexander Aring };
10752d84ffcSAlexander Aring
lowpan_setup(struct net_device * ldev)108f4606583SAlexander Aring static void lowpan_setup(struct net_device *ldev)
10952d84ffcSAlexander Aring {
110f4606583SAlexander Aring memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN);
11187a93e4eSAlexander Aring /* We need an ipv6hdr as minimum len when calling xmit */
11287a93e4eSAlexander Aring ldev->hard_header_len = sizeof(struct ipv6hdr);
113f4606583SAlexander Aring ldev->flags = IFF_BROADCAST | IFF_MULTICAST;
114ee6f4a5aSAlexander Aring ldev->priv_flags |= IFF_NO_QUEUE;
11552d84ffcSAlexander Aring
116f4606583SAlexander Aring ldev->netdev_ops = &lowpan_netdev_ops;
117f4606583SAlexander Aring ldev->header_ops = &lowpan_header_ops;
118cf124db5SDavid S. Miller ldev->needs_free_netdev = true;
119f4606583SAlexander Aring ldev->features |= NETIF_F_NETNS_LOCAL;
12052d84ffcSAlexander Aring }
12152d84ffcSAlexander Aring
lowpan_validate(struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)122a8b8a889SMatthias Schiffer static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[],
123a8b8a889SMatthias Schiffer struct netlink_ext_ack *extack)
12452d84ffcSAlexander Aring {
12552d84ffcSAlexander Aring if (tb[IFLA_ADDRESS]) {
12652d84ffcSAlexander Aring if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN)
12752d84ffcSAlexander Aring return -EINVAL;
12852d84ffcSAlexander Aring }
12952d84ffcSAlexander Aring return 0;
13052d84ffcSAlexander Aring }
13152d84ffcSAlexander Aring
lowpan_newlink(struct net * src_net,struct net_device * ldev,struct nlattr * tb[],struct nlattr * data[],struct netlink_ext_ack * extack)132f4606583SAlexander Aring static int lowpan_newlink(struct net *src_net, struct net_device *ldev,
1337a3f4a18SMatthias Schiffer struct nlattr *tb[], struct nlattr *data[],
1347a3f4a18SMatthias Schiffer struct netlink_ext_ack *extack)
13552d84ffcSAlexander Aring {
136f4606583SAlexander Aring struct net_device *wdev;
13752d84ffcSAlexander Aring int ret;
13852d84ffcSAlexander Aring
13952d84ffcSAlexander Aring ASSERT_RTNL();
14052d84ffcSAlexander Aring
14152d84ffcSAlexander Aring pr_debug("adding new link\n");
14252d84ffcSAlexander Aring
1431c5bf998SAlexander Aring if (!tb[IFLA_LINK])
14452d84ffcSAlexander Aring return -EINVAL;
145f4606583SAlexander Aring /* find and hold wpan device */
146f4606583SAlexander Aring wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK]));
147f4606583SAlexander Aring if (!wdev)
14852d84ffcSAlexander Aring return -ENODEV;
149f4606583SAlexander Aring if (wdev->type != ARPHRD_IEEE802154) {
150f4606583SAlexander Aring dev_put(wdev);
15152d84ffcSAlexander Aring return -EINVAL;
15252d84ffcSAlexander Aring }
15352d84ffcSAlexander Aring
154f4606583SAlexander Aring if (wdev->ieee802154_ptr->lowpan_dev) {
155f4606583SAlexander Aring dev_put(wdev);
15651e0e5d8SAlexander Aring return -EBUSY;
15752d84ffcSAlexander Aring }
15852d84ffcSAlexander Aring
1592e4d60cbSAlexander Aring lowpan_802154_dev(ldev)->wdev = wdev;
16052d84ffcSAlexander Aring /* Set the lowpan hardware address to the wpan hardware address. */
16108bb7516SJakub Kicinski __dev_addr_set(ldev, wdev->dev_addr, IEEE802154_ADDR_LEN);
16287a93e4eSAlexander Aring /* We need headroom for possible wpan_dev_hard_header call and tailroom
16387a93e4eSAlexander Aring * for encryption/fcs handling. The lowpan interface will replace
16487a93e4eSAlexander Aring * the IPv6 header with 6LoWPAN header. At worst case the 6LoWPAN
16587a93e4eSAlexander Aring * header has LOWPAN_IPHC_MAX_HEADER_LEN more bytes than the IPv6
16687a93e4eSAlexander Aring * header.
16787a93e4eSAlexander Aring */
16887a93e4eSAlexander Aring ldev->needed_headroom = LOWPAN_IPHC_MAX_HEADER_LEN +
16987a93e4eSAlexander Aring wdev->needed_headroom;
17087a93e4eSAlexander Aring ldev->needed_tailroom = wdev->needed_tailroom;
17152d84ffcSAlexander Aring
1728626a0c8SAlexander Aring ldev->neigh_priv_len = sizeof(struct lowpan_802154_neigh);
1738626a0c8SAlexander Aring
17400f59314SAlexander Aring ret = lowpan_register_netdevice(ldev, LOWPAN_LLTYPE_IEEE802154);
17507512728SAlexander Aring if (ret < 0) {
176f4606583SAlexander Aring dev_put(wdev);
17707512728SAlexander Aring return ret;
17852d84ffcSAlexander Aring }
17952d84ffcSAlexander Aring
180f4606583SAlexander Aring wdev->ieee802154_ptr->lowpan_dev = ldev;
18107512728SAlexander Aring return 0;
18252d84ffcSAlexander Aring }
18352d84ffcSAlexander Aring
lowpan_dellink(struct net_device * ldev,struct list_head * head)184f4606583SAlexander Aring static void lowpan_dellink(struct net_device *ldev, struct list_head *head)
18552d84ffcSAlexander Aring {
1862e4d60cbSAlexander Aring struct net_device *wdev = lowpan_802154_dev(ldev)->wdev;
18752d84ffcSAlexander Aring
18852d84ffcSAlexander Aring ASSERT_RTNL();
18952d84ffcSAlexander Aring
190f4606583SAlexander Aring wdev->ieee802154_ptr->lowpan_dev = NULL;
19100f59314SAlexander Aring lowpan_unregister_netdevice(ldev);
192f4606583SAlexander Aring dev_put(wdev);
19352d84ffcSAlexander Aring }
19452d84ffcSAlexander Aring
19552d84ffcSAlexander Aring static struct rtnl_link_ops lowpan_link_ops __read_mostly = {
19652d84ffcSAlexander Aring .kind = "lowpan",
1972e4d60cbSAlexander Aring .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_802154_dev)),
19852d84ffcSAlexander Aring .setup = lowpan_setup,
19952d84ffcSAlexander Aring .newlink = lowpan_newlink,
20052d84ffcSAlexander Aring .dellink = lowpan_dellink,
20152d84ffcSAlexander Aring .validate = lowpan_validate,
20252d84ffcSAlexander Aring };
20352d84ffcSAlexander Aring
lowpan_netlink_init(void)20452d84ffcSAlexander Aring static inline int __init lowpan_netlink_init(void)
20552d84ffcSAlexander Aring {
20652d84ffcSAlexander Aring return rtnl_link_register(&lowpan_link_ops);
20752d84ffcSAlexander Aring }
20852d84ffcSAlexander Aring
lowpan_netlink_fini(void)20952d84ffcSAlexander Aring static inline void lowpan_netlink_fini(void)
21052d84ffcSAlexander Aring {
21152d84ffcSAlexander Aring rtnl_link_unregister(&lowpan_link_ops);
21252d84ffcSAlexander Aring }
21352d84ffcSAlexander Aring
lowpan_device_event(struct notifier_block * unused,unsigned long event,void * ptr)21452d84ffcSAlexander Aring static int lowpan_device_event(struct notifier_block *unused,
21552d84ffcSAlexander Aring unsigned long event, void *ptr)
21652d84ffcSAlexander Aring {
217ca0edb13SEric Dumazet struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
218ca0edb13SEric Dumazet struct wpan_dev *wpan_dev;
21952d84ffcSAlexander Aring
220ca0edb13SEric Dumazet if (ndev->type != ARPHRD_IEEE802154)
221ca0edb13SEric Dumazet return NOTIFY_DONE;
222ca0edb13SEric Dumazet wpan_dev = ndev->ieee802154_ptr;
223ca0edb13SEric Dumazet if (!wpan_dev)
224ebba380cSAlexander Aring return NOTIFY_DONE;
22552d84ffcSAlexander Aring
22651e0e5d8SAlexander Aring switch (event) {
22751e0e5d8SAlexander Aring case NETDEV_UNREGISTER:
22851e0e5d8SAlexander Aring /* Check if wpan interface is unregistered that we
22951e0e5d8SAlexander Aring * also delete possible lowpan interfaces which belongs
23051e0e5d8SAlexander Aring * to the wpan interface.
23151e0e5d8SAlexander Aring */
232ca0edb13SEric Dumazet if (wpan_dev->lowpan_dev)
233ca0edb13SEric Dumazet lowpan_dellink(wpan_dev->lowpan_dev, NULL);
23451e0e5d8SAlexander Aring break;
23551e0e5d8SAlexander Aring default:
236ebba380cSAlexander Aring return NOTIFY_DONE;
23752d84ffcSAlexander Aring }
23852d84ffcSAlexander Aring
239ebba380cSAlexander Aring return NOTIFY_OK;
24052d84ffcSAlexander Aring }
24152d84ffcSAlexander Aring
24252d84ffcSAlexander Aring static struct notifier_block lowpan_dev_notifier = {
24352d84ffcSAlexander Aring .notifier_call = lowpan_device_event,
24452d84ffcSAlexander Aring };
24552d84ffcSAlexander Aring
lowpan_init_module(void)24652d84ffcSAlexander Aring static int __init lowpan_init_module(void)
24752d84ffcSAlexander Aring {
24852d84ffcSAlexander Aring int err = 0;
24952d84ffcSAlexander Aring
25052d84ffcSAlexander Aring err = lowpan_net_frag_init();
25152d84ffcSAlexander Aring if (err < 0)
25252d84ffcSAlexander Aring goto out;
25352d84ffcSAlexander Aring
25452d84ffcSAlexander Aring err = lowpan_netlink_init();
25552d84ffcSAlexander Aring if (err < 0)
25652d84ffcSAlexander Aring goto out_frag;
25752d84ffcSAlexander Aring
25852d84ffcSAlexander Aring err = register_netdevice_notifier(&lowpan_dev_notifier);
25952d84ffcSAlexander Aring if (err < 0)
26052d84ffcSAlexander Aring goto out_pack;
26152d84ffcSAlexander Aring
26252d84ffcSAlexander Aring return 0;
26352d84ffcSAlexander Aring
26452d84ffcSAlexander Aring out_pack:
26552d84ffcSAlexander Aring lowpan_netlink_fini();
26652d84ffcSAlexander Aring out_frag:
26752d84ffcSAlexander Aring lowpan_net_frag_exit();
26852d84ffcSAlexander Aring out:
26952d84ffcSAlexander Aring return err;
27052d84ffcSAlexander Aring }
27152d84ffcSAlexander Aring
lowpan_cleanup_module(void)27252d84ffcSAlexander Aring static void __exit lowpan_cleanup_module(void)
27352d84ffcSAlexander Aring {
27452d84ffcSAlexander Aring lowpan_netlink_fini();
27552d84ffcSAlexander Aring
27652d84ffcSAlexander Aring lowpan_net_frag_exit();
27752d84ffcSAlexander Aring
27852d84ffcSAlexander Aring unregister_netdevice_notifier(&lowpan_dev_notifier);
27952d84ffcSAlexander Aring }
28052d84ffcSAlexander Aring
28152d84ffcSAlexander Aring module_init(lowpan_init_module);
28252d84ffcSAlexander Aring module_exit(lowpan_cleanup_module);
28352d84ffcSAlexander Aring MODULE_LICENSE("GPL");
28452d84ffcSAlexander Aring MODULE_ALIAS_RTNL_LINK("lowpan");
285