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