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