Alexander Aring | b72f6f5 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 1 | /* This program is free software; you can redistribute it and/or modify |
| 2 | * it under the terms of the GNU General Public License version 2 |
| 3 | * as published by the Free Software Foundation. |
| 4 | * |
| 5 | * This program is distributed in the hope that it will be useful, |
| 6 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 7 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 8 | * GNU General Public License for more details. |
| 9 | * |
| 10 | * Authors: |
| 11 | * (C) 2015 Pengutronix, Alexander Aring <aar@pengutronix.de> |
| 12 | */ |
| 13 | |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | |
Alexander Aring | b72f6f5 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 16 | #include <net/6lowpan.h> |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame^] | 17 | #include <net/addrconf.h> |
Alexander Aring | b72f6f5 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 18 | |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 19 | #include "6lowpan_i.h" |
| 20 | |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 21 | int lowpan_register_netdevice(struct net_device *dev, |
| 22 | enum lowpan_lltypes lltype) |
Alexander Aring | b72f6f5 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 23 | { |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 24 | int i, ret; |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 25 | |
Alexander Aring | 4d6a6ae | 2015-10-02 20:28:04 +0200 | [diff] [blame] | 26 | dev->addr_len = EUI64_ADDR_LEN; |
| 27 | dev->type = ARPHRD_6LOWPAN; |
| 28 | dev->mtu = IPV6_MIN_MTU; |
| 29 | dev->priv_flags |= IFF_NO_QUEUE; |
| 30 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 31 | lowpan_dev(dev)->lltype = lltype; |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 32 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 33 | spin_lock_init(&lowpan_dev(dev)->ctx.lock); |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 34 | for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 35 | lowpan_dev(dev)->ctx.table[i].id = i; |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 36 | |
Alexander Aring | 92e17ee | 2015-12-15 12:25:35 +0100 | [diff] [blame] | 37 | ret = register_netdevice(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 38 | if (ret < 0) |
| 39 | return ret; |
| 40 | |
Alexander Aring | 92e17ee | 2015-12-15 12:25:35 +0100 | [diff] [blame] | 41 | ret = lowpan_dev_debugfs_init(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 42 | if (ret < 0) |
Alexander Aring | 92e17ee | 2015-12-15 12:25:35 +0100 | [diff] [blame] | 43 | unregister_netdevice(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 44 | |
| 45 | return ret; |
Alexander Aring | b72f6f5 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 46 | } |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 47 | EXPORT_SYMBOL(lowpan_register_netdevice); |
| 48 | |
| 49 | int lowpan_register_netdev(struct net_device *dev, |
| 50 | enum lowpan_lltypes lltype) |
| 51 | { |
| 52 | int ret; |
| 53 | |
| 54 | rtnl_lock(); |
| 55 | ret = lowpan_register_netdevice(dev, lltype); |
| 56 | rtnl_unlock(); |
| 57 | return ret; |
| 58 | } |
| 59 | EXPORT_SYMBOL(lowpan_register_netdev); |
| 60 | |
| 61 | void lowpan_unregister_netdevice(struct net_device *dev) |
| 62 | { |
| 63 | unregister_netdevice(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 64 | lowpan_dev_debugfs_exit(dev); |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 65 | } |
| 66 | EXPORT_SYMBOL(lowpan_unregister_netdevice); |
| 67 | |
| 68 | void lowpan_unregister_netdev(struct net_device *dev) |
| 69 | { |
| 70 | rtnl_lock(); |
| 71 | lowpan_unregister_netdevice(dev); |
| 72 | rtnl_unlock(); |
| 73 | } |
| 74 | EXPORT_SYMBOL(lowpan_unregister_netdev); |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 75 | |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame^] | 76 | static int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev) |
| 77 | { |
| 78 | struct wpan_dev *wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr; |
| 79 | |
| 80 | /* Set short_addr autoconfiguration if short_addr is present only */ |
| 81 | if (!lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr)) |
| 82 | return -1; |
| 83 | |
| 84 | /* For either address format, all zero addresses MUST NOT be used */ |
| 85 | if (wpan_dev->pan_id == cpu_to_le16(0x0000) && |
| 86 | wpan_dev->short_addr == cpu_to_le16(0x0000)) |
| 87 | return -1; |
| 88 | |
| 89 | /* Alternatively, if no PAN ID is known, 16 zero bits may be used */ |
| 90 | if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST)) |
| 91 | memset(eui, 0, 2); |
| 92 | else |
| 93 | ieee802154_le16_to_be16(eui, &wpan_dev->pan_id); |
| 94 | |
| 95 | /* The "Universal/Local" (U/L) bit shall be set to zero */ |
| 96 | eui[0] &= ~2; |
| 97 | eui[2] = 0; |
| 98 | eui[3] = 0xFF; |
| 99 | eui[4] = 0xFE; |
| 100 | eui[5] = 0; |
| 101 | ieee802154_le16_to_be16(&eui[6], &wpan_dev->short_addr); |
| 102 | return 0; |
| 103 | } |
| 104 | |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 105 | static int lowpan_event(struct notifier_block *unused, |
| 106 | unsigned long event, void *ptr) |
| 107 | { |
| 108 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame^] | 109 | struct inet6_dev *idev; |
| 110 | struct in6_addr addr; |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 111 | int i; |
| 112 | |
| 113 | if (dev->type != ARPHRD_6LOWPAN) |
| 114 | return NOTIFY_DONE; |
| 115 | |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame^] | 116 | idev = __in6_dev_get(dev); |
| 117 | if (!idev) |
| 118 | return NOTIFY_DONE; |
| 119 | |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 120 | switch (event) { |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame^] | 121 | case NETDEV_UP: |
| 122 | case NETDEV_CHANGE: |
| 123 | /* (802.15.4 6LoWPAN short address slaac handling */ |
| 124 | if (lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154) && |
| 125 | addrconf_ifid_802154_6lowpan(addr.s6_addr + 8, dev) == 0) { |
| 126 | __ipv6_addr_set_half(&addr.s6_addr32[0], |
| 127 | htonl(0xFE800000), 0); |
| 128 | addrconf_add_linklocal(idev, &addr, 0); |
| 129 | } |
| 130 | break; |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 131 | case NETDEV_DOWN: |
| 132 | for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) |
| 133 | clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 134 | &lowpan_dev(dev)->ctx.table[i].flags); |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 135 | break; |
| 136 | default: |
| 137 | return NOTIFY_DONE; |
| 138 | } |
| 139 | |
| 140 | return NOTIFY_OK; |
| 141 | } |
| 142 | |
| 143 | static struct notifier_block lowpan_notifier = { |
| 144 | .notifier_call = lowpan_event, |
| 145 | }; |
| 146 | |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 147 | static int __init lowpan_module_init(void) |
| 148 | { |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 149 | int ret; |
| 150 | |
| 151 | ret = lowpan_debugfs_init(); |
| 152 | if (ret < 0) |
| 153 | return ret; |
| 154 | |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 155 | ret = register_netdevice_notifier(&lowpan_notifier); |
| 156 | if (ret < 0) { |
| 157 | lowpan_debugfs_exit(); |
| 158 | return ret; |
| 159 | } |
| 160 | |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 161 | request_module_nowait("ipv6"); |
| 162 | |
| 163 | request_module_nowait("nhc_dest"); |
| 164 | request_module_nowait("nhc_fragment"); |
| 165 | request_module_nowait("nhc_hop"); |
| 166 | request_module_nowait("nhc_ipv6"); |
| 167 | request_module_nowait("nhc_mobility"); |
| 168 | request_module_nowait("nhc_routing"); |
| 169 | request_module_nowait("nhc_udp"); |
| 170 | |
| 171 | return 0; |
| 172 | } |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 173 | |
| 174 | static void __exit lowpan_module_exit(void) |
| 175 | { |
| 176 | lowpan_debugfs_exit(); |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 177 | unregister_netdevice_notifier(&lowpan_notifier); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 178 | } |
| 179 | |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 180 | module_init(lowpan_module_init); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 181 | module_exit(lowpan_module_exit); |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 182 | |
| 183 | MODULE_LICENSE("GPL"); |