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 | |
Patrik Flykt | be054fc | 2017-03-12 10:19:34 +0200 | [diff] [blame] | 26 | switch (lltype) { |
| 27 | case LOWPAN_LLTYPE_IEEE802154: |
| 28 | dev->addr_len = EUI64_ADDR_LEN; |
| 29 | break; |
| 30 | |
| 31 | case LOWPAN_LLTYPE_BTLE: |
| 32 | dev->addr_len = ETH_ALEN; |
| 33 | break; |
| 34 | } |
| 35 | |
Alexander Aring | 4d6a6ae | 2015-10-02 20:28:04 +0200 | [diff] [blame] | 36 | dev->type = ARPHRD_6LOWPAN; |
| 37 | dev->mtu = IPV6_MIN_MTU; |
Alexander Aring | 4d6a6ae | 2015-10-02 20:28:04 +0200 | [diff] [blame] | 38 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 39 | lowpan_dev(dev)->lltype = lltype; |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 40 | |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 41 | spin_lock_init(&lowpan_dev(dev)->ctx.lock); |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 42 | for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 43 | lowpan_dev(dev)->ctx.table[i].id = i; |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 44 | |
Alexander Aring | bbe5f5c | 2016-06-15 21:20:25 +0200 | [diff] [blame] | 45 | dev->ndisc_ops = &lowpan_ndisc_ops; |
| 46 | |
Alexander Aring | 92e17ee | 2015-12-15 12:25:35 +0100 | [diff] [blame] | 47 | ret = register_netdevice(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 48 | if (ret < 0) |
| 49 | return ret; |
| 50 | |
Alexander Aring | 92e17ee | 2015-12-15 12:25:35 +0100 | [diff] [blame] | 51 | ret = lowpan_dev_debugfs_init(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 52 | if (ret < 0) |
Alexander Aring | 92e17ee | 2015-12-15 12:25:35 +0100 | [diff] [blame] | 53 | unregister_netdevice(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 54 | |
| 55 | return ret; |
Alexander Aring | b72f6f5 | 2015-08-11 21:44:08 +0200 | [diff] [blame] | 56 | } |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 57 | EXPORT_SYMBOL(lowpan_register_netdevice); |
| 58 | |
| 59 | int lowpan_register_netdev(struct net_device *dev, |
| 60 | enum lowpan_lltypes lltype) |
| 61 | { |
| 62 | int ret; |
| 63 | |
| 64 | rtnl_lock(); |
| 65 | ret = lowpan_register_netdevice(dev, lltype); |
| 66 | rtnl_unlock(); |
| 67 | return ret; |
| 68 | } |
| 69 | EXPORT_SYMBOL(lowpan_register_netdev); |
| 70 | |
| 71 | void lowpan_unregister_netdevice(struct net_device *dev) |
| 72 | { |
| 73 | unregister_netdevice(dev); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 74 | lowpan_dev_debugfs_exit(dev); |
Alexander Aring | 00f5931 | 2015-12-09 22:46:29 +0100 | [diff] [blame] | 75 | } |
| 76 | EXPORT_SYMBOL(lowpan_unregister_netdevice); |
| 77 | |
| 78 | void lowpan_unregister_netdev(struct net_device *dev) |
| 79 | { |
| 80 | rtnl_lock(); |
| 81 | lowpan_unregister_netdevice(dev); |
| 82 | rtnl_unlock(); |
| 83 | } |
| 84 | EXPORT_SYMBOL(lowpan_unregister_netdev); |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 85 | |
Alexander Aring | bbe5f5c | 2016-06-15 21:20:25 +0200 | [diff] [blame] | 86 | int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev) |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame] | 87 | { |
| 88 | struct wpan_dev *wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr; |
| 89 | |
| 90 | /* Set short_addr autoconfiguration if short_addr is present only */ |
| 91 | if (!lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr)) |
| 92 | return -1; |
| 93 | |
| 94 | /* For either address format, all zero addresses MUST NOT be used */ |
| 95 | if (wpan_dev->pan_id == cpu_to_le16(0x0000) && |
| 96 | wpan_dev->short_addr == cpu_to_le16(0x0000)) |
| 97 | return -1; |
| 98 | |
| 99 | /* Alternatively, if no PAN ID is known, 16 zero bits may be used */ |
| 100 | if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST)) |
| 101 | memset(eui, 0, 2); |
| 102 | else |
| 103 | ieee802154_le16_to_be16(eui, &wpan_dev->pan_id); |
| 104 | |
| 105 | /* The "Universal/Local" (U/L) bit shall be set to zero */ |
| 106 | eui[0] &= ~2; |
| 107 | eui[2] = 0; |
| 108 | eui[3] = 0xFF; |
| 109 | eui[4] = 0xFE; |
| 110 | eui[5] = 0; |
| 111 | ieee802154_le16_to_be16(&eui[6], &wpan_dev->short_addr); |
| 112 | return 0; |
| 113 | } |
| 114 | |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 115 | static int lowpan_event(struct notifier_block *unused, |
| 116 | unsigned long event, void *ptr) |
| 117 | { |
| 118 | struct net_device *dev = netdev_notifier_info_to_dev(ptr); |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame] | 119 | struct inet6_dev *idev; |
| 120 | struct in6_addr addr; |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 121 | int i; |
| 122 | |
| 123 | if (dev->type != ARPHRD_6LOWPAN) |
| 124 | return NOTIFY_DONE; |
| 125 | |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame] | 126 | idev = __in6_dev_get(dev); |
| 127 | if (!idev) |
| 128 | return NOTIFY_DONE; |
| 129 | |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 130 | switch (event) { |
Alexander Aring | 2ad3ed5 | 2016-06-15 21:20:17 +0200 | [diff] [blame] | 131 | case NETDEV_UP: |
| 132 | case NETDEV_CHANGE: |
| 133 | /* (802.15.4 6LoWPAN short address slaac handling */ |
| 134 | if (lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154) && |
| 135 | addrconf_ifid_802154_6lowpan(addr.s6_addr + 8, dev) == 0) { |
| 136 | __ipv6_addr_set_half(&addr.s6_addr32[0], |
| 137 | htonl(0xFE800000), 0); |
| 138 | addrconf_add_linklocal(idev, &addr, 0); |
| 139 | } |
| 140 | break; |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 141 | case NETDEV_DOWN: |
| 142 | for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++) |
| 143 | clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE, |
Alexander Aring | 2e4d60c | 2016-04-11 11:04:18 +0200 | [diff] [blame] | 144 | &lowpan_dev(dev)->ctx.table[i].flags); |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 145 | break; |
| 146 | default: |
| 147 | return NOTIFY_DONE; |
| 148 | } |
| 149 | |
| 150 | return NOTIFY_OK; |
| 151 | } |
| 152 | |
| 153 | static struct notifier_block lowpan_notifier = { |
| 154 | .notifier_call = lowpan_event, |
| 155 | }; |
| 156 | |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 157 | static int __init lowpan_module_init(void) |
| 158 | { |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 159 | int ret; |
| 160 | |
| 161 | ret = lowpan_debugfs_init(); |
| 162 | if (ret < 0) |
| 163 | return ret; |
| 164 | |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 165 | ret = register_netdevice_notifier(&lowpan_notifier); |
| 166 | if (ret < 0) { |
| 167 | lowpan_debugfs_exit(); |
| 168 | return ret; |
| 169 | } |
| 170 | |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 171 | request_module_nowait("nhc_dest"); |
| 172 | request_module_nowait("nhc_fragment"); |
| 173 | request_module_nowait("nhc_hop"); |
| 174 | request_module_nowait("nhc_ipv6"); |
| 175 | request_module_nowait("nhc_mobility"); |
| 176 | request_module_nowait("nhc_routing"); |
| 177 | request_module_nowait("nhc_udp"); |
| 178 | |
| 179 | return 0; |
| 180 | } |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 181 | |
| 182 | static void __exit lowpan_module_exit(void) |
| 183 | { |
| 184 | lowpan_debugfs_exit(); |
Alexander Aring | 5609c18 | 2016-02-22 09:13:54 +0100 | [diff] [blame] | 185 | unregister_netdevice_notifier(&lowpan_notifier); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 186 | } |
| 187 | |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 188 | module_init(lowpan_module_init); |
Alexander Aring | b1815fd | 2015-12-09 22:46:30 +0100 | [diff] [blame] | 189 | module_exit(lowpan_module_exit); |
Alexander Aring | 4ae935c | 2015-08-11 21:44:09 +0200 | [diff] [blame] | 190 | |
| 191 | MODULE_LICENSE("GPL"); |