1 /* Copyright 2011, Siemens AG 2 * written by Alexander Smirnov <alex.bluesman.smirnov@gmail.com> 3 */ 4 5 /* Based on patches from Jon Smirl <jonsmirl@gmail.com> 6 * Copyright (c) 2011 Jon Smirl <jonsmirl@gmail.com> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License version 2 10 * as published by the Free Software Foundation. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 */ 17 18 /* Jon's code is based on 6lowpan implementation for Contiki which is: 19 * Copyright (c) 2008, Swedish Institute of Computer Science. 20 * All rights reserved. 21 * 22 * Redistribution and use in source and binary forms, with or without 23 * modification, are permitted provided that the following conditions 24 * are met: 25 * 1. Redistributions of source code must retain the above copyright 26 * notice, this list of conditions and the following disclaimer. 27 * 2. Redistributions in binary form must reproduce the above copyright 28 * notice, this list of conditions and the following disclaimer in the 29 * documentation and/or other materials provided with the distribution. 30 * 3. Neither the name of the Institute nor the names of its contributors 31 * may be used to endorse or promote products derived from this software 32 * without specific prior written permission. 33 * 34 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND 35 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 36 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 37 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE 38 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 39 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 40 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 41 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 42 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 43 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 44 * SUCH DAMAGE. 45 */ 46 47 #include <linux/module.h> 48 #include <linux/netdevice.h> 49 #include <linux/ieee802154.h> 50 #include <linux/if_arp.h> 51 52 #include <net/ipv6.h> 53 #include <net/netdev_lock.h> 54 55 #include "6lowpan_i.h" 56 57 static int open_count; 58 59 static const struct header_ops lowpan_header_ops = { 60 .create = lowpan_header_create, 61 }; 62 63 static int lowpan_dev_init(struct net_device *ldev) 64 { 65 netdev_lockdep_set_classes(ldev); 66 67 return 0; 68 } 69 70 static int lowpan_open(struct net_device *dev) 71 { 72 if (!open_count) 73 lowpan_rx_init(); 74 open_count++; 75 return 0; 76 } 77 78 static int lowpan_stop(struct net_device *dev) 79 { 80 open_count--; 81 if (!open_count) 82 lowpan_rx_exit(); 83 return 0; 84 } 85 86 static int lowpan_neigh_construct(struct net_device *dev, struct neighbour *n) 87 { 88 struct lowpan_802154_neigh *neigh = lowpan_802154_neigh(neighbour_priv(n)); 89 90 /* default no short_addr is available for a neighbour */ 91 neigh->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC); 92 return 0; 93 } 94 95 static int lowpan_get_iflink(const struct net_device *dev) 96 { 97 return READ_ONCE(lowpan_802154_dev(dev)->wdev->ifindex); 98 } 99 100 static const struct net_device_ops lowpan_netdev_ops = { 101 .ndo_init = lowpan_dev_init, 102 .ndo_start_xmit = lowpan_xmit, 103 .ndo_open = lowpan_open, 104 .ndo_stop = lowpan_stop, 105 .ndo_neigh_construct = lowpan_neigh_construct, 106 .ndo_get_iflink = lowpan_get_iflink, 107 }; 108 109 static void lowpan_setup(struct net_device *ldev) 110 { 111 memset(ldev->broadcast, 0xff, IEEE802154_ADDR_LEN); 112 /* We need an ipv6hdr as minimum len when calling xmit */ 113 ldev->hard_header_len = sizeof(struct ipv6hdr); 114 ldev->flags = IFF_BROADCAST | IFF_MULTICAST; 115 ldev->priv_flags |= IFF_NO_QUEUE; 116 117 ldev->netdev_ops = &lowpan_netdev_ops; 118 ldev->header_ops = &lowpan_header_ops; 119 ldev->needs_free_netdev = true; 120 ldev->netns_immutable = true; 121 } 122 123 static int lowpan_validate(struct nlattr *tb[], struct nlattr *data[], 124 struct netlink_ext_ack *extack) 125 { 126 if (tb[IFLA_ADDRESS]) { 127 if (nla_len(tb[IFLA_ADDRESS]) != IEEE802154_ADDR_LEN) 128 return -EINVAL; 129 } 130 return 0; 131 } 132 133 static int lowpan_newlink(struct net_device *ldev, 134 struct rtnl_newlink_params *params, 135 struct netlink_ext_ack *extack) 136 { 137 struct nlattr **tb = params->tb; 138 struct net_device *wdev; 139 int ret; 140 141 ASSERT_RTNL(); 142 143 pr_debug("adding new link\n"); 144 145 if (!tb[IFLA_LINK]) 146 return -EINVAL; 147 if (params->link_net && !net_eq(params->link_net, dev_net(ldev))) 148 return -EINVAL; 149 /* find and hold wpan device */ 150 wdev = dev_get_by_index(dev_net(ldev), nla_get_u32(tb[IFLA_LINK])); 151 if (!wdev) 152 return -ENODEV; 153 if (wdev->type != ARPHRD_IEEE802154) { 154 dev_put(wdev); 155 return -EINVAL; 156 } 157 158 if (wdev->ieee802154_ptr->lowpan_dev) { 159 dev_put(wdev); 160 return -EBUSY; 161 } 162 163 lowpan_802154_dev(ldev)->wdev = wdev; 164 /* Set the lowpan hardware address to the wpan hardware address. */ 165 __dev_addr_set(ldev, wdev->dev_addr, IEEE802154_ADDR_LEN); 166 /* We need headroom for possible wpan_dev_hard_header call and tailroom 167 * for encryption/fcs handling. The lowpan interface will replace 168 * the IPv6 header with 6LoWPAN header. At worst case the 6LoWPAN 169 * header has LOWPAN_IPHC_MAX_HEADER_LEN more bytes than the IPv6 170 * header. 171 */ 172 ldev->needed_headroom = LOWPAN_IPHC_MAX_HEADER_LEN + 173 wdev->needed_headroom; 174 ldev->needed_tailroom = wdev->needed_tailroom; 175 176 ldev->neigh_priv_len = sizeof(struct lowpan_802154_neigh); 177 178 ret = lowpan_register_netdevice(ldev, LOWPAN_LLTYPE_IEEE802154); 179 if (ret < 0) { 180 dev_put(wdev); 181 return ret; 182 } 183 184 wdev->ieee802154_ptr->lowpan_dev = ldev; 185 return 0; 186 } 187 188 static void lowpan_dellink(struct net_device *ldev, struct list_head *head) 189 { 190 struct net_device *wdev = lowpan_802154_dev(ldev)->wdev; 191 192 ASSERT_RTNL(); 193 194 wdev->ieee802154_ptr->lowpan_dev = NULL; 195 lowpan_unregister_netdevice(ldev); 196 dev_put(wdev); 197 } 198 199 static struct rtnl_link_ops lowpan_link_ops __read_mostly = { 200 .kind = "lowpan", 201 .priv_size = LOWPAN_PRIV_SIZE(sizeof(struct lowpan_802154_dev)), 202 .setup = lowpan_setup, 203 .newlink = lowpan_newlink, 204 .dellink = lowpan_dellink, 205 .validate = lowpan_validate, 206 }; 207 208 static inline int __init lowpan_netlink_init(void) 209 { 210 return rtnl_link_register(&lowpan_link_ops); 211 } 212 213 static inline void lowpan_netlink_fini(void) 214 { 215 rtnl_link_unregister(&lowpan_link_ops); 216 } 217 218 static int lowpan_device_event(struct notifier_block *unused, 219 unsigned long event, void *ptr) 220 { 221 struct net_device *ndev = netdev_notifier_info_to_dev(ptr); 222 struct wpan_dev *wpan_dev; 223 224 if (ndev->type != ARPHRD_IEEE802154) 225 return NOTIFY_DONE; 226 wpan_dev = ndev->ieee802154_ptr; 227 if (!wpan_dev) 228 return NOTIFY_DONE; 229 230 switch (event) { 231 case NETDEV_UNREGISTER: 232 /* Check if wpan interface is unregistered that we 233 * also delete possible lowpan interfaces which belongs 234 * to the wpan interface. 235 */ 236 if (wpan_dev->lowpan_dev) 237 lowpan_dellink(wpan_dev->lowpan_dev, NULL); 238 break; 239 default: 240 return NOTIFY_DONE; 241 } 242 243 return NOTIFY_OK; 244 } 245 246 static struct notifier_block lowpan_dev_notifier = { 247 .notifier_call = lowpan_device_event, 248 }; 249 250 static int __init lowpan_init_module(void) 251 { 252 int err = 0; 253 254 err = lowpan_net_frag_init(); 255 if (err < 0) 256 goto out; 257 258 err = lowpan_netlink_init(); 259 if (err < 0) 260 goto out_frag; 261 262 err = register_netdevice_notifier(&lowpan_dev_notifier); 263 if (err < 0) 264 goto out_pack; 265 266 return 0; 267 268 out_pack: 269 lowpan_netlink_fini(); 270 out_frag: 271 lowpan_net_frag_exit(); 272 out: 273 return err; 274 } 275 276 static void __exit lowpan_cleanup_module(void) 277 { 278 lowpan_netlink_fini(); 279 280 lowpan_net_frag_exit(); 281 282 unregister_netdevice_notifier(&lowpan_dev_notifier); 283 } 284 285 module_init(lowpan_init_module); 286 module_exit(lowpan_cleanup_module); 287 MODULE_DESCRIPTION("IPv6 over Low power Wireless Personal Area Network IEEE 802.15.4 core"); 288 MODULE_LICENSE("GPL"); 289 MODULE_ALIAS_RTNL_LINK("lowpan"); 290