Skip to content

Commit bbe5f5c

Browse files
Alexander Aringdavem330
authored andcommitted
6lowpan: introduce 6lowpan-nd
This patch introduce different 6lowpan handling for receive and transmit NS/NA messages for the ipv6 neighbour discovery. The first use-case is for supporting 802.15.4 short addresses inside the option fields and handling for RFC6775 6CO option field as userspace option. Cc: David S. Miller <davem@davemloft.net> Cc: Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> Cc: James Morris <jmorris@namei.org> Cc: Hideaki YOSHIFUJI <yoshfuji@linux-ipv6.org> Cc: Patrick McHardy <kaber@trash.net> Reviewed-by: Stefan Schmidt <stefan@osg.samsung.com> Acked-by: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> Signed-off-by: Alexander Aring <aar@pengutronix.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent cc84b3c commit bbe5f5c

5 files changed

Lines changed: 254 additions & 8 deletions

File tree

‎include/net/ndisc.h‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ enum {
3535
ND_OPT_ROUTE_INFO = 24, /* RFC4191 */
3636
ND_OPT_RDNSS = 25, /* RFC5006 */
3737
ND_OPT_DNSSL = 31, /* RFC6106 */
38+
ND_OPT_6CO = 34, /* RFC6775 */
3839
__ND_OPT_MAX
3940
};
4041

@@ -109,14 +110,19 @@ struct ndisc_options {
109110
#endif
110111
struct nd_opt_hdr *nd_useropts;
111112
struct nd_opt_hdr *nd_useropts_end;
113+
#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
114+
struct nd_opt_hdr *nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR + 1];
115+
#endif
112116
};
113117

114-
#define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
115-
#define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
116-
#define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
117-
#define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
118-
#define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
119-
#define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
118+
#define nd_opts_src_lladdr nd_opt_array[ND_OPT_SOURCE_LL_ADDR]
119+
#define nd_opts_tgt_lladdr nd_opt_array[ND_OPT_TARGET_LL_ADDR]
120+
#define nd_opts_pi nd_opt_array[ND_OPT_PREFIX_INFO]
121+
#define nd_opts_pi_end nd_opt_array[__ND_OPT_PREFIX_INFO_END]
122+
#define nd_opts_rh nd_opt_array[ND_OPT_REDIRECT_HDR]
123+
#define nd_opts_mtu nd_opt_array[ND_OPT_MTU]
124+
#define nd_802154_opts_src_lladdr nd_802154_opt_array[ND_OPT_SOURCE_LL_ADDR]
125+
#define nd_802154_opts_tgt_lladdr nd_802154_opt_array[ND_OPT_TARGET_LL_ADDR]
120126

121127
#define NDISC_OPT_SPACE(len) (((len)+2+7)&~7)
122128

‎net/6lowpan/6lowpan_i.h‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ static inline bool lowpan_is_ll(const struct net_device *dev,
1212
return lowpan_dev(dev)->lltype == lltype;
1313
}
1414

15+
extern const struct ndisc_ops lowpan_ndisc_ops;
16+
17+
int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev);
18+
1519
#ifdef CONFIG_6LOWPAN_DEBUGFS
1620
int lowpan_dev_debugfs_init(struct net_device *dev);
1721
void lowpan_dev_debugfs_exit(struct net_device *dev);

‎net/6lowpan/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
obj-$(CONFIG_6LOWPAN) += 6lowpan.o
22

3-
6lowpan-y := core.o iphc.o nhc.o
3+
6lowpan-y := core.o iphc.o nhc.o ndisc.o
44
6lowpan-$(CONFIG_6LOWPAN_DEBUGFS) += debugfs.o
55

66
#rfc6282 nhcs

‎net/6lowpan/core.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ int lowpan_register_netdevice(struct net_device *dev,
3434
for (i = 0; i < LOWPAN_IPHC_CTX_TABLE_SIZE; i++)
3535
lowpan_dev(dev)->ctx.table[i].id = i;
3636

37+
dev->ndisc_ops = &lowpan_ndisc_ops;
38+
3739
ret = register_netdevice(dev);
3840
if (ret < 0)
3941
return ret;
@@ -73,7 +75,7 @@ void lowpan_unregister_netdev(struct net_device *dev)
7375
}
7476
EXPORT_SYMBOL(lowpan_unregister_netdev);
7577

76-
static int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev)
78+
int addrconf_ifid_802154_6lowpan(u8 *eui, struct net_device *dev)
7779
{
7880
struct wpan_dev *wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr;
7981

‎net/6lowpan/ndisc.c‎

Lines changed: 234 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,234 @@
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) 2016 Pengutronix, Alexander Aring <aar@pengutronix.de>
12+
*/
13+
14+
#include <net/6lowpan.h>
15+
#include <net/addrconf.h>
16+
#include <net/ndisc.h>
17+
18+
#include "6lowpan_i.h"
19+
20+
static int lowpan_ndisc_is_useropt(u8 nd_opt_type)
21+
{
22+
return nd_opt_type == ND_OPT_6CO;
23+
}
24+
25+
#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
26+
#define NDISC_802154_SHORT_ADDR_LENGTH 1
27+
static int lowpan_ndisc_parse_802154_options(const struct net_device *dev,
28+
struct nd_opt_hdr *nd_opt,
29+
struct ndisc_options *ndopts)
30+
{
31+
switch (nd_opt->nd_opt_len) {
32+
case NDISC_802154_SHORT_ADDR_LENGTH:
33+
if (ndopts->nd_802154_opt_array[nd_opt->nd_opt_type])
34+
ND_PRINTK(2, warn,
35+
"%s: duplicated short addr ND6 option found: type=%d\n",
36+
__func__, nd_opt->nd_opt_type);
37+
else
38+
ndopts->nd_802154_opt_array[nd_opt->nd_opt_type] = nd_opt;
39+
return 1;
40+
default:
41+
/* all others will be handled by ndisc IPv6 option parsing */
42+
return 0;
43+
}
44+
}
45+
46+
static int lowpan_ndisc_parse_options(const struct net_device *dev,
47+
struct nd_opt_hdr *nd_opt,
48+
struct ndisc_options *ndopts)
49+
{
50+
switch (nd_opt->nd_opt_type) {
51+
case ND_OPT_SOURCE_LL_ADDR:
52+
case ND_OPT_TARGET_LL_ADDR:
53+
return lowpan_ndisc_parse_802154_options(dev, nd_opt, ndopts);
54+
default:
55+
return 0;
56+
}
57+
}
58+
59+
static void lowpan_ndisc_802154_update(struct neighbour *n, u32 flags,
60+
u8 icmp6_type,
61+
const struct ndisc_options *ndopts)
62+
{
63+
struct lowpan_802154_neigh *neigh = lowpan_802154_neigh(neighbour_priv(n));
64+
u8 *lladdr_short = NULL;
65+
66+
switch (icmp6_type) {
67+
case NDISC_ROUTER_SOLICITATION:
68+
case NDISC_ROUTER_ADVERTISEMENT:
69+
case NDISC_NEIGHBOUR_SOLICITATION:
70+
if (ndopts->nd_802154_opts_src_lladdr) {
71+
lladdr_short = __ndisc_opt_addr_data(ndopts->nd_802154_opts_src_lladdr,
72+
IEEE802154_SHORT_ADDR_LEN, 0);
73+
if (!lladdr_short) {
74+
ND_PRINTK(2, warn,
75+
"NA: invalid short link-layer address length\n");
76+
return;
77+
}
78+
}
79+
break;
80+
case NDISC_REDIRECT:
81+
case NDISC_NEIGHBOUR_ADVERTISEMENT:
82+
if (ndopts->nd_802154_opts_tgt_lladdr) {
83+
lladdr_short = __ndisc_opt_addr_data(ndopts->nd_802154_opts_tgt_lladdr,
84+
IEEE802154_SHORT_ADDR_LEN, 0);
85+
if (!lladdr_short) {
86+
ND_PRINTK(2, warn,
87+
"NA: invalid short link-layer address length\n");
88+
return;
89+
}
90+
}
91+
break;
92+
default:
93+
break;
94+
}
95+
96+
write_lock_bh(&n->lock);
97+
if (lladdr_short)
98+
ieee802154_be16_to_le16(&neigh->short_addr, lladdr_short);
99+
else
100+
neigh->short_addr = cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC);
101+
write_unlock_bh(&n->lock);
102+
}
103+
104+
static void lowpan_ndisc_update(const struct net_device *dev,
105+
struct neighbour *n, u32 flags, u8 icmp6_type,
106+
const struct ndisc_options *ndopts)
107+
{
108+
if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154))
109+
return;
110+
111+
/* react on overrides only. TODO check if this is really right. */
112+
if (flags & NEIGH_UPDATE_F_OVERRIDE)
113+
lowpan_ndisc_802154_update(n, flags, icmp6_type, ndopts);
114+
}
115+
116+
static int lowpan_ndisc_opt_addr_space(const struct net_device *dev,
117+
u8 icmp6_type, struct neighbour *neigh,
118+
u8 *ha_buf, u8 **ha)
119+
{
120+
struct lowpan_802154_neigh *n;
121+
struct wpan_dev *wpan_dev;
122+
int addr_space = 0;
123+
124+
if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154))
125+
return 0;
126+
127+
switch (icmp6_type) {
128+
case NDISC_REDIRECT:
129+
n = lowpan_802154_neigh(neighbour_priv(neigh));
130+
131+
read_lock_bh(&neigh->lock);
132+
if (lowpan_802154_is_valid_src_short_addr(n->short_addr)) {
133+
memcpy(ha_buf, &n->short_addr,
134+
IEEE802154_SHORT_ADDR_LEN);
135+
read_unlock_bh(&neigh->lock);
136+
addr_space += __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN, 0);
137+
*ha = ha_buf;
138+
}
139+
read_unlock_bh(&neigh->lock);
140+
break;
141+
case NDISC_NEIGHBOUR_ADVERTISEMENT:
142+
case NDISC_NEIGHBOUR_SOLICITATION:
143+
case NDISC_ROUTER_SOLICITATION:
144+
wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr;
145+
146+
if (lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr))
147+
addr_space = __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN, 0);
148+
break;
149+
default:
150+
break;
151+
}
152+
153+
return addr_space;
154+
}
155+
156+
static void lowpan_ndisc_fill_addr_option(const struct net_device *dev,
157+
struct sk_buff *skb, u8 icmp6_type,
158+
const u8 *ha)
159+
{
160+
struct wpan_dev *wpan_dev;
161+
__be16 short_addr;
162+
u8 opt_type;
163+
164+
if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154))
165+
return;
166+
167+
switch (icmp6_type) {
168+
case NDISC_REDIRECT:
169+
if (ha) {
170+
ieee802154_le16_to_be16(&short_addr, ha);
171+
__ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
172+
&short_addr,
173+
IEEE802154_SHORT_ADDR_LEN, 0);
174+
}
175+
return;
176+
case NDISC_NEIGHBOUR_ADVERTISEMENT:
177+
opt_type = ND_OPT_TARGET_LL_ADDR;
178+
break;
179+
case NDISC_ROUTER_SOLICITATION:
180+
case NDISC_NEIGHBOUR_SOLICITATION:
181+
opt_type = ND_OPT_SOURCE_LL_ADDR;
182+
break;
183+
default:
184+
return;
185+
}
186+
187+
wpan_dev = lowpan_802154_dev(dev)->wdev->ieee802154_ptr;
188+
189+
if (lowpan_802154_is_valid_src_short_addr(wpan_dev->short_addr)) {
190+
ieee802154_le16_to_be16(&short_addr,
191+
&wpan_dev->short_addr);
192+
__ndisc_fill_addr_option(skb, opt_type, &short_addr,
193+
IEEE802154_SHORT_ADDR_LEN, 0);
194+
}
195+
}
196+
197+
static void lowpan_ndisc_prefix_rcv_add_addr(struct net *net,
198+
struct net_device *dev,
199+
const struct prefix_info *pinfo,
200+
struct inet6_dev *in6_dev,
201+
struct in6_addr *addr,
202+
int addr_type, u32 addr_flags,
203+
bool sllao, bool tokenized,
204+
__u32 valid_lft,
205+
u32 prefered_lft,
206+
bool dev_addr_generated)
207+
{
208+
int err;
209+
210+
/* generates short based address for RA PIO's */
211+
if (lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154) && dev_addr_generated &&
212+
!addrconf_ifid_802154_6lowpan(addr->s6_addr + 8, dev)) {
213+
err = addrconf_prefix_rcv_add_addr(net, dev, pinfo, in6_dev,
214+
addr, addr_type, addr_flags,
215+
sllao, tokenized, valid_lft,
216+
prefered_lft);
217+
if (err)
218+
ND_PRINTK(2, warn,
219+
"RA: could not add a short address based address for prefix: %pI6c\n",
220+
&pinfo->prefix);
221+
}
222+
}
223+
#endif
224+
225+
const struct ndisc_ops lowpan_ndisc_ops = {
226+
.is_useropt = lowpan_ndisc_is_useropt,
227+
#if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
228+
.parse_options = lowpan_ndisc_parse_options,
229+
.update = lowpan_ndisc_update,
230+
.opt_addr_space = lowpan_ndisc_opt_addr_space,
231+
.fill_addr_option = lowpan_ndisc_fill_addr_option,
232+
.prefix_rcv_add_addr = lowpan_ndisc_prefix_rcv_add_addr,
233+
#endif
234+
};

0 commit comments

Comments
 (0)