octeontx2-pf: Fix error return code in otx2_probe()
[linux/fpc-iii.git] / net / 6lowpan / ndisc.c
blob16be8f8b2f8ca8295956778cca9fe071e0048d8b
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
4 * Authors:
5 * (C) 2016 Pengutronix, Alexander Aring <aar@pengutronix.de>
6 */
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])
28 ND_PRINTK(2, warn,
29 "%s: duplicated short addr ND6 option found: type=%d\n",
30 __func__, nd_opt->nd_opt_type);
31 else
32 ndopts->nd_802154_opt_array[nd_opt->nd_opt_type] = nd_opt;
33 return 1;
34 default:
35 /* all others will be handled by ndisc IPv6 option parsing */
36 return 0;
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))
45 return 0;
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);
51 default:
52 return 0;
56 static void lowpan_ndisc_802154_update(struct neighbour *n, u32 flags,
57 u8 icmp6_type,
58 const struct ndisc_options *ndopts)
60 struct lowpan_802154_neigh *neigh = lowpan_802154_neigh(neighbour_priv(n));
61 u8 *lladdr_short = NULL;
63 switch (icmp6_type) {
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);
70 if (!lladdr_short) {
71 ND_PRINTK(2, warn,
72 "NA: invalid short link-layer address length\n");
73 return;
76 break;
77 case NDISC_REDIRECT:
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);
82 if (!lladdr_short) {
83 ND_PRINTK(2, warn,
84 "NA: invalid short link-layer address length\n");
85 return;
88 break;
89 default:
90 break;
93 write_lock_bh(&n->lock);
94 if (lladdr_short) {
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))
107 return;
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,
116 u8 *ha_buf, u8 **ha)
118 struct lowpan_802154_neigh *n;
119 struct wpan_dev *wpan_dev;
120 int addr_space = 0;
122 if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154))
123 return 0;
125 switch (icmp6_type) {
126 case NDISC_REDIRECT:
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);
135 *ha = ha_buf;
136 } else {
137 read_unlock_bh(&neigh->lock);
139 break;
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);
147 break;
148 default:
149 break;
152 return addr_space;
155 static void lowpan_ndisc_fill_addr_option(const struct net_device *dev,
156 struct sk_buff *skb, u8 icmp6_type,
157 const u8 *ha)
159 struct wpan_dev *wpan_dev;
160 __be16 short_addr;
161 u8 opt_type;
163 if (!lowpan_is_ll(dev, LOWPAN_LLTYPE_IEEE802154))
164 return;
166 switch (icmp6_type) {
167 case NDISC_REDIRECT:
168 if (ha) {
169 ieee802154_le16_to_be16(&short_addr, ha);
170 __ndisc_fill_addr_option(skb, ND_OPT_TARGET_LL_ADDR,
171 &short_addr,
172 IEEE802154_SHORT_ADDR_LEN, 0);
174 return;
175 case NDISC_NEIGHBOUR_ADVERTISEMENT:
176 opt_type = ND_OPT_TARGET_LL_ADDR;
177 break;
178 case NDISC_ROUTER_SOLICITATION:
179 case NDISC_NEIGHBOUR_SOLICITATION:
180 opt_type = ND_OPT_SOURCE_LL_ADDR;
181 break;
182 default:
183 return;
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,
203 __u32 valid_lft,
204 u32 prefered_lft,
205 bool dev_addr_generated)
207 int err;
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,
215 prefered_lft);
216 if (err)
217 ND_PRINTK(2, warn,
218 "RA: could not add a short address based address for prefix: %pI6c\n",
219 &pinfo->prefix);
222 #endif
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,
232 #endif