1 // SPDX-License-Identifier: GPL-2.0-only
5 * (C) 2016 Pengutronix, Alexander Aring <aar@pengutronix.de>
8 #include <net/6lowpan.h>
9 #include <net/addrconf.h>
10 #include <net/ndisc.h>
12 #include "6lowpan_i.h"
14 static int lowpan_ndisc_is_useropt(u8 nd_opt_type
)
16 return nd_opt_type
== ND_OPT_6CO
;
19 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
20 #define NDISC_802154_SHORT_ADDR_LENGTH 1
21 static int lowpan_ndisc_parse_802154_options(const struct net_device
*dev
,
22 struct nd_opt_hdr
*nd_opt
,
23 struct ndisc_options
*ndopts
)
25 switch (nd_opt
->nd_opt_len
) {
26 case NDISC_802154_SHORT_ADDR_LENGTH
:
27 if (ndopts
->nd_802154_opt_array
[nd_opt
->nd_opt_type
])
29 "%s: duplicated short addr ND6 option found: type=%d\n",
30 __func__
, nd_opt
->nd_opt_type
);
32 ndopts
->nd_802154_opt_array
[nd_opt
->nd_opt_type
] = nd_opt
;
35 /* all others will be handled by ndisc IPv6 option parsing */
40 static int lowpan_ndisc_parse_options(const struct net_device
*dev
,
41 struct nd_opt_hdr
*nd_opt
,
42 struct ndisc_options
*ndopts
)
44 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
47 switch (nd_opt
->nd_opt_type
) {
48 case ND_OPT_SOURCE_LL_ADDR
:
49 case ND_OPT_TARGET_LL_ADDR
:
50 return lowpan_ndisc_parse_802154_options(dev
, nd_opt
, ndopts
);
56 static void lowpan_ndisc_802154_update(struct neighbour
*n
, u32 flags
,
58 const struct ndisc_options
*ndopts
)
60 struct lowpan_802154_neigh
*neigh
= lowpan_802154_neigh(neighbour_priv(n
));
61 u8
*lladdr_short
= NULL
;
64 case NDISC_ROUTER_SOLICITATION
:
65 case NDISC_ROUTER_ADVERTISEMENT
:
66 case NDISC_NEIGHBOUR_SOLICITATION
:
67 if (ndopts
->nd_802154_opts_src_lladdr
) {
68 lladdr_short
= __ndisc_opt_addr_data(ndopts
->nd_802154_opts_src_lladdr
,
69 IEEE802154_SHORT_ADDR_LEN
, 0);
72 "NA: invalid short link-layer address length\n");
78 case NDISC_NEIGHBOUR_ADVERTISEMENT
:
79 if (ndopts
->nd_802154_opts_tgt_lladdr
) {
80 lladdr_short
= __ndisc_opt_addr_data(ndopts
->nd_802154_opts_tgt_lladdr
,
81 IEEE802154_SHORT_ADDR_LEN
, 0);
84 "NA: invalid short link-layer address length\n");
93 write_lock_bh(&n
->lock
);
95 ieee802154_be16_to_le16(&neigh
->short_addr
, lladdr_short
);
96 if (!lowpan_802154_is_valid_src_short_addr(neigh
->short_addr
))
97 neigh
->short_addr
= cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC
);
99 write_unlock_bh(&n
->lock
);
102 static void lowpan_ndisc_update(const struct net_device
*dev
,
103 struct neighbour
*n
, u32 flags
, u8 icmp6_type
,
104 const struct ndisc_options
*ndopts
)
106 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
109 /* react on overrides only. TODO check if this is really right. */
110 if (flags
& NEIGH_UPDATE_F_OVERRIDE
)
111 lowpan_ndisc_802154_update(n
, flags
, icmp6_type
, ndopts
);
114 static int lowpan_ndisc_opt_addr_space(const struct net_device
*dev
,
115 u8 icmp6_type
, struct neighbour
*neigh
,
118 struct lowpan_802154_neigh
*n
;
119 struct wpan_dev
*wpan_dev
;
122 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
125 switch (icmp6_type
) {
127 n
= lowpan_802154_neigh(neighbour_priv(neigh
));
129 read_lock_bh(&neigh
->lock
);
130 if (lowpan_802154_is_valid_src_short_addr(n
->short_addr
)) {
131 memcpy(ha_buf
, &n
->short_addr
,
132 IEEE802154_SHORT_ADDR_LEN
);
133 read_unlock_bh(&neigh
->lock
);
134 addr_space
+= __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN
, 0);
137 read_unlock_bh(&neigh
->lock
);
140 case NDISC_NEIGHBOUR_ADVERTISEMENT
:
141 case NDISC_NEIGHBOUR_SOLICITATION
:
142 case NDISC_ROUTER_SOLICITATION
:
143 wpan_dev
= lowpan_802154_dev(dev
)->wdev
->ieee802154_ptr
;
145 if (lowpan_802154_is_valid_src_short_addr(wpan_dev
->short_addr
))
146 addr_space
= __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN
, 0);
155 static void lowpan_ndisc_fill_addr_option(const struct net_device
*dev
,
156 struct sk_buff
*skb
, u8 icmp6_type
,
159 struct wpan_dev
*wpan_dev
;
163 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
166 switch (icmp6_type
) {
169 ieee802154_le16_to_be16(&short_addr
, ha
);
170 __ndisc_fill_addr_option(skb
, ND_OPT_TARGET_LL_ADDR
,
172 IEEE802154_SHORT_ADDR_LEN
, 0);
175 case NDISC_NEIGHBOUR_ADVERTISEMENT
:
176 opt_type
= ND_OPT_TARGET_LL_ADDR
;
178 case NDISC_ROUTER_SOLICITATION
:
179 case NDISC_NEIGHBOUR_SOLICITATION
:
180 opt_type
= ND_OPT_SOURCE_LL_ADDR
;
186 wpan_dev
= lowpan_802154_dev(dev
)->wdev
->ieee802154_ptr
;
188 if (lowpan_802154_is_valid_src_short_addr(wpan_dev
->short_addr
)) {
189 ieee802154_le16_to_be16(&short_addr
,
190 &wpan_dev
->short_addr
);
191 __ndisc_fill_addr_option(skb
, opt_type
, &short_addr
,
192 IEEE802154_SHORT_ADDR_LEN
, 0);
196 static void lowpan_ndisc_prefix_rcv_add_addr(struct net
*net
,
197 struct net_device
*dev
,
198 const struct prefix_info
*pinfo
,
199 struct inet6_dev
*in6_dev
,
200 struct in6_addr
*addr
,
201 int addr_type
, u32 addr_flags
,
202 bool sllao
, bool tokenized
,
205 bool dev_addr_generated
)
209 /* generates short based address for RA PIO's */
210 if (lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
) && dev_addr_generated
&&
211 !addrconf_ifid_802154_6lowpan(addr
->s6_addr
+ 8, dev
)) {
212 err
= addrconf_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
,
213 addr
, addr_type
, addr_flags
,
214 sllao
, tokenized
, valid_lft
,
218 "RA: could not add a short address based address for prefix: %pI6c\n",
224 const struct ndisc_ops lowpan_ndisc_ops
= {
225 .is_useropt
= lowpan_ndisc_is_useropt
,
226 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
227 .parse_options
= lowpan_ndisc_parse_options
,
228 .update
= lowpan_ndisc_update
,
229 .opt_addr_space
= lowpan_ndisc_opt_addr_space
,
230 .fill_addr_option
= lowpan_ndisc_fill_addr_option
,
231 .prefix_rcv_add_addr
= lowpan_ndisc_prefix_rcv_add_addr
,