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 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
15 #define NDISC_802154_SHORT_ADDR_LENGTH 1
16 static int lowpan_ndisc_parse_802154_options(const struct net_device
*dev
,
17 struct nd_opt_hdr
*nd_opt
,
18 struct ndisc_options
*ndopts
)
20 switch (nd_opt
->nd_opt_len
) {
21 case NDISC_802154_SHORT_ADDR_LENGTH
:
22 if (ndopts
->nd_802154_opt_array
[nd_opt
->nd_opt_type
])
24 "%s: duplicated short addr ND6 option found: type=%d\n",
25 __func__
, nd_opt
->nd_opt_type
);
27 ndopts
->nd_802154_opt_array
[nd_opt
->nd_opt_type
] = nd_opt
;
30 /* all others will be handled by ndisc IPv6 option parsing */
35 static int lowpan_ndisc_parse_options(const struct net_device
*dev
,
36 struct nd_opt_hdr
*nd_opt
,
37 struct ndisc_options
*ndopts
)
39 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
42 switch (nd_opt
->nd_opt_type
) {
43 case ND_OPT_SOURCE_LL_ADDR
:
44 case ND_OPT_TARGET_LL_ADDR
:
45 return lowpan_ndisc_parse_802154_options(dev
, nd_opt
, ndopts
);
51 static void lowpan_ndisc_802154_update(struct neighbour
*n
, u32 flags
,
53 const struct ndisc_options
*ndopts
)
55 struct lowpan_802154_neigh
*neigh
= lowpan_802154_neigh(neighbour_priv(n
));
56 u8
*lladdr_short
= NULL
;
59 case NDISC_ROUTER_SOLICITATION
:
60 case NDISC_ROUTER_ADVERTISEMENT
:
61 case NDISC_NEIGHBOUR_SOLICITATION
:
62 if (ndopts
->nd_802154_opts_src_lladdr
) {
63 lladdr_short
= __ndisc_opt_addr_data(ndopts
->nd_802154_opts_src_lladdr
,
64 IEEE802154_SHORT_ADDR_LEN
, 0);
67 "NA: invalid short link-layer address length\n");
73 case NDISC_NEIGHBOUR_ADVERTISEMENT
:
74 if (ndopts
->nd_802154_opts_tgt_lladdr
) {
75 lladdr_short
= __ndisc_opt_addr_data(ndopts
->nd_802154_opts_tgt_lladdr
,
76 IEEE802154_SHORT_ADDR_LEN
, 0);
79 "NA: invalid short link-layer address length\n");
88 write_lock_bh(&n
->lock
);
90 ieee802154_be16_to_le16(&neigh
->short_addr
, lladdr_short
);
91 if (!lowpan_802154_is_valid_src_short_addr(neigh
->short_addr
))
92 neigh
->short_addr
= cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC
);
94 write_unlock_bh(&n
->lock
);
97 static void lowpan_ndisc_update(const struct net_device
*dev
,
98 struct neighbour
*n
, u32 flags
, u8 icmp6_type
,
99 const struct ndisc_options
*ndopts
)
101 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
104 /* react on overrides only. TODO check if this is really right. */
105 if (flags
& NEIGH_UPDATE_F_OVERRIDE
)
106 lowpan_ndisc_802154_update(n
, flags
, icmp6_type
, ndopts
);
109 static int lowpan_ndisc_opt_addr_space(const struct net_device
*dev
,
110 u8 icmp6_type
, struct neighbour
*neigh
,
113 struct lowpan_802154_neigh
*n
;
114 struct wpan_dev
*wpan_dev
;
117 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
120 switch (icmp6_type
) {
122 n
= lowpan_802154_neigh(neighbour_priv(neigh
));
124 read_lock_bh(&neigh
->lock
);
125 if (lowpan_802154_is_valid_src_short_addr(n
->short_addr
)) {
126 memcpy(ha_buf
, &n
->short_addr
,
127 IEEE802154_SHORT_ADDR_LEN
);
128 read_unlock_bh(&neigh
->lock
);
129 addr_space
+= __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN
, 0);
132 read_unlock_bh(&neigh
->lock
);
135 case NDISC_NEIGHBOUR_ADVERTISEMENT
:
136 case NDISC_NEIGHBOUR_SOLICITATION
:
137 case NDISC_ROUTER_SOLICITATION
:
138 wpan_dev
= lowpan_802154_dev(dev
)->wdev
->ieee802154_ptr
;
140 if (lowpan_802154_is_valid_src_short_addr(wpan_dev
->short_addr
))
141 addr_space
= __ndisc_opt_addr_space(IEEE802154_SHORT_ADDR_LEN
, 0);
150 static void lowpan_ndisc_fill_addr_option(const struct net_device
*dev
,
151 struct sk_buff
*skb
, u8 icmp6_type
,
154 struct wpan_dev
*wpan_dev
;
158 if (!lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
))
161 switch (icmp6_type
) {
164 ieee802154_le16_to_be16(&short_addr
, ha
);
165 __ndisc_fill_addr_option(skb
, ND_OPT_TARGET_LL_ADDR
,
167 IEEE802154_SHORT_ADDR_LEN
, 0);
170 case NDISC_NEIGHBOUR_ADVERTISEMENT
:
171 opt_type
= ND_OPT_TARGET_LL_ADDR
;
173 case NDISC_ROUTER_SOLICITATION
:
174 case NDISC_NEIGHBOUR_SOLICITATION
:
175 opt_type
= ND_OPT_SOURCE_LL_ADDR
;
181 wpan_dev
= lowpan_802154_dev(dev
)->wdev
->ieee802154_ptr
;
183 if (lowpan_802154_is_valid_src_short_addr(wpan_dev
->short_addr
)) {
184 ieee802154_le16_to_be16(&short_addr
,
185 &wpan_dev
->short_addr
);
186 __ndisc_fill_addr_option(skb
, opt_type
, &short_addr
,
187 IEEE802154_SHORT_ADDR_LEN
, 0);
191 static void lowpan_ndisc_prefix_rcv_add_addr(struct net
*net
,
192 struct net_device
*dev
,
193 const struct prefix_info
*pinfo
,
194 struct inet6_dev
*in6_dev
,
195 struct in6_addr
*addr
,
196 int addr_type
, u32 addr_flags
,
197 bool sllao
, bool tokenized
,
200 bool dev_addr_generated
)
204 /* generates short based address for RA PIO's */
205 if (lowpan_is_ll(dev
, LOWPAN_LLTYPE_IEEE802154
) && dev_addr_generated
&&
206 !addrconf_ifid_802154_6lowpan(addr
->s6_addr
+ 8, dev
)) {
207 err
= addrconf_prefix_rcv_add_addr(net
, dev
, pinfo
, in6_dev
,
208 addr
, addr_type
, addr_flags
,
209 sllao
, tokenized
, valid_lft
,
213 "RA: could not add a short address based address for prefix: %pI6c\n",
219 const struct ndisc_ops lowpan_ndisc_ops
= {
220 #if IS_ENABLED(CONFIG_IEEE802154_6LOWPAN)
221 .parse_options
= lowpan_ndisc_parse_options
,
222 .update
= lowpan_ndisc_update
,
223 .opt_addr_space
= lowpan_ndisc_opt_addr_space
,
224 .fill_addr_option
= lowpan_ndisc_fill_addr_option
,
225 .prefix_rcv_add_addr
= lowpan_ndisc_prefix_rcv_add_addr
,