Merge tag 'topic/drm-misc-2016-08-12' of git://anongit.freedesktop.org/drm-intel...
[linux-2.6-block.git] / net / 6lowpan / core.c
CommitLineData
b72f6f51
AA
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
4ae935c1
AA
14#include <linux/module.h>
15
b72f6f51 16#include <net/6lowpan.h>
2ad3ed59 17#include <net/addrconf.h>
b72f6f51 18
b1815fd9
AA
19#include "6lowpan_i.h"
20
00f59314
AA
21int lowpan_register_netdevice(struct net_device *dev,
22 enum lowpan_lltypes lltype)
b72f6f51 23{
5609c185 24 int i, ret;
b1815fd9 25
4d6a6aed
AA
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
2e4d60cb 31 lowpan_dev(dev)->lltype = lltype;
00f59314 32
2e4d60cb 33 spin_lock_init(&lowpan_dev(dev)->ctx.lock);
5609c185 34 for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
2e4d60cb 35 lowpan_dev(dev)->ctx.table[i].id = i;
5609c185 36
bbe5f5ce
AA
37 dev->ndisc_ops = &lowpan_ndisc_ops;
38
92e17ee7 39 ret = register_netdevice(dev);
b1815fd9
AA
40 if (ret < 0)
41 return ret;
42
92e17ee7 43 ret = lowpan_dev_debugfs_init(dev);
b1815fd9 44 if (ret < 0)
92e17ee7 45 unregister_netdevice(dev);
b1815fd9
AA
46
47 return ret;
00f59314
AA
48}
49EXPORT_SYMBOL(lowpan_register_netdevice);
50
51int lowpan_register_netdev(struct net_device *dev,
52 enum lowpan_lltypes lltype)
53{
54 int ret;
55
56 rtnl_lock();
57 ret = lowpan_register_netdevice(dev, lltype);
58 rtnl_unlock();
59 return ret;
60}
61EXPORT_SYMBOL(lowpan_register_netdev);
62
63void lowpan_unregister_netdevice(struct net_device *dev)
64{
65 unregister_netdevice(dev);
b1815fd9 66 lowpan_dev_debugfs_exit(dev);
00f59314
AA
67}
68EXPORT_SYMBOL(lowpan_unregister_netdevice);
69
70void lowpan_unregister_netdev(struct net_device *dev)
71{
72 rtnl_lock();
73 lowpan_unregister_netdevice(dev);
74 rtnl_unlock();
b72f6f51 75}
00f59314 76EXPORT_SYMBOL(lowpan_unregister_netdev);
4ae935c1 77
bbe5f5ce 78int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev)
2ad3ed59
AA
79{
80 struct wpan_dev *wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr;
81
82 /* Set short_addr autoconfiguration if short_addr is present only */
83 if (!lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr))
84 return -1;
85
86 /* For either address format, all zero addresses MUST NOT be used */
87 if (wpan_dev->pan_id == cpu_to_le16(0x0000) &&
88 wpan_dev->short_addr == cpu_to_le16(0x0000))
89 return -1;
90
91 /* Alternatively, if no PAN ID is known, 16 zero bits may be used */
92 if (wpan_dev->pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
93 memset(eui, 0, 2);
94 else
95 ieee802154_le16_to_be16(eui, &wpan_dev->pan_id);
96
97 /* The "Universal/Local" (U/L) bit shall be set to zero */
98 eui[0] &= ~2;
99 eui[2] = 0;
100 eui[3] = 0xFF;
101 eui[4] = 0xFE;
102 eui[5] = 0;
103 ieee802154_le16_to_be16(&eui[6], &wpan_dev->short_addr);
104 return 0;
105}
106
5609c185
AA
107static int lowpan_event(struct notifier_block *unused,
108 unsigned long event, void *ptr)
109{
110 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2ad3ed59
AA
111 struct inet6_dev *idev;
112 struct in6_addr addr;
5609c185
AA
113 int i;
114
115 if (dev->type != ARPHRD_6LOWPAN)
116 return NOTIFY_DONE;
117
2ad3ed59
AA
118 idev = __in6_dev_get(dev);
119 if (!idev)
120 return NOTIFY_DONE;
121
5609c185 122 switch (event) {
2ad3ed59
AA
123 case NETDEV_UP:
124 case NETDEV_CHANGE:
125 /* (802.15.4 6LoWPAN short address slaac handling */
126 if (lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154) &&
127 addrconf_ifid_802154_6lowpan(addr.s6_addr + 8, dev) == 0) {
128 __ipv6_addr_set_half(&addr.s6_addr32[0],
129 htonl(0xFE800000), 0);
130 addrconf_add_linklocal(idev, &addr, 0);
131 }
132 break;
5609c185
AA
133 case NETDEV_DOWN:
134 for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
135 clear_bit(LOWPAN_IPHC_CTX_FLAG_ACTIVE,
2e4d60cb 136 &lowpan_dev(dev)->ctx.table[i].flags);
5609c185
AA
137 break;
138 default:
139 return NOTIFY_DONE;
140 }
141
142 return NOTIFY_OK;
143}
144
145static struct notifier_block lowpan_notifier = {
146 .notifier_call = lowpan_event,
147};
148
4ae935c1
AA
149static int __init lowpan_module_init(void)
150{
b1815fd9
AA
151 int ret;
152
153 ret = lowpan_debugfs_init();
154 if (ret < 0)
155 return ret;
156
5609c185
AA
157 ret = register_netdevice_notifier(&lowpan_notifier);
158 if (ret < 0) {
159 lowpan_debugfs_exit();
160 return ret;
161 }
162
4ae935c1
AA
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}
b1815fd9
AA
173
174static void __exit lowpan_module_exit(void)
175{
176 lowpan_debugfs_exit();
5609c185 177 unregister_netdevice_notifier(&lowpan_notifier);
b1815fd9
AA
178}
179
4ae935c1 180module_init(lowpan_module_init);
b1815fd9 181module_exit(lowpan_module_exit);
4ae935c1
AA
182
183MODULE_LICENSE("GPL");