1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * An interface between IEEE802.15.4 device and rest of the kernel.
4  *
5  * Copyright (C) 2007-2012 Siemens AG
6  *
7  * Written by:
8  * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
9  * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
10  * Maxim Osipov <maxim.osipov@siemens.com>
11  * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
12  * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
13  */
14 
15 #ifndef IEEE802154_NETDEVICE_H
16 #define IEEE802154_NETDEVICE_H
17 
18 #define IEEE802154_REQUIRED_SIZE(struct_type, member) \
19 	(offsetof(typeof(struct_type), member) + \
20 	sizeof(((typeof(struct_type) *)(NULL))->member))
21 
22 #define IEEE802154_ADDR_OFFSET \
23 	offsetof(typeof(struct sockaddr_ieee802154), addr)
24 
25 #define IEEE802154_MIN_NAMELEN (IEEE802154_ADDR_OFFSET + \
26 	IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, addr_type))
27 
28 #define IEEE802154_NAMELEN_SHORT (IEEE802154_ADDR_OFFSET + \
29 	IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, short_addr))
30 
31 #define IEEE802154_NAMELEN_LONG (IEEE802154_ADDR_OFFSET + \
32 	IEEE802154_REQUIRED_SIZE(struct ieee802154_addr_sa, hwaddr))
33 
34 #include <net/af_ieee802154.h>
35 #include <linux/netdevice.h>
36 #include <linux/skbuff.h>
37 #include <linux/ieee802154.h>
38 
39 #include <net/cfg802154.h>
40 
41 struct ieee802154_sechdr {
42 #if defined(__LITTLE_ENDIAN_BITFIELD)
43 	u8 level:3,
44 	   key_id_mode:2,
45 	   reserved:3;
46 #elif defined(__BIG_ENDIAN_BITFIELD)
47 	u8 reserved:3,
48 	   key_id_mode:2,
49 	   level:3;
50 #else
51 #error	"Please fix <asm/byteorder.h>"
52 #endif
53 	u8 key_id;
54 	__le32 frame_counter;
55 	union {
56 		__le32 short_src;
57 		__le64 extended_src;
58 	};
59 };
60 
61 struct ieee802154_hdr_fc {
62 #if defined(__LITTLE_ENDIAN_BITFIELD)
63 	u16 type:3,
64 	    security_enabled:1,
65 	    frame_pending:1,
66 	    ack_request:1,
67 	    intra_pan:1,
68 	    reserved:3,
69 	    dest_addr_mode:2,
70 	    version:2,
71 	    source_addr_mode:2;
72 #elif defined(__BIG_ENDIAN_BITFIELD)
73 	u16 reserved:1,
74 	    intra_pan:1,
75 	    ack_request:1,
76 	    frame_pending:1,
77 	    security_enabled:1,
78 	    type:3,
79 	    source_addr_mode:2,
80 	    version:2,
81 	    dest_addr_mode:2,
82 	    reserved2:2;
83 #else
84 #error	"Please fix <asm/byteorder.h>"
85 #endif
86 };
87 
88 struct ieee802154_hdr {
89 	struct ieee802154_hdr_fc fc;
90 	u8 seq;
91 	struct ieee802154_addr source;
92 	struct ieee802154_addr dest;
93 	struct ieee802154_sechdr sec;
94 };
95 
96 /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
97  * the contents of hdr will be, and the actual value of those bits in
98  * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
99  * version, if SECEN is set.
100  */
101 int ieee802154_hdr_push(struct sk_buff *skb, struct ieee802154_hdr *hdr);
102 
103 /* pulls the entire 802.15.4 header off of the skb, including the security
104  * header, and performs pan id decompression
105  */
106 int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
107 
108 /* parses the frame control, sequence number of address fields in a given skb
109  * and stores them into hdr, performing pan id decompression and length checks
110  * to be suitable for use in header_ops.parse
111  */
112 int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
113 			      struct ieee802154_hdr *hdr);
114 
115 /* parses the full 802.15.4 header a given skb and stores them into hdr,
116  * performing pan id decompression and length checks to be suitable for use in
117  * header_ops.parse
118  */
119 int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr);
120 
121 int ieee802154_max_payload(const struct ieee802154_hdr *hdr);
122 
123 static inline int
ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr * sec)124 ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec)
125 {
126 	switch (sec->level) {
127 	case IEEE802154_SCF_SECLEVEL_MIC32:
128 	case IEEE802154_SCF_SECLEVEL_ENC_MIC32:
129 		return 4;
130 	case IEEE802154_SCF_SECLEVEL_MIC64:
131 	case IEEE802154_SCF_SECLEVEL_ENC_MIC64:
132 		return 8;
133 	case IEEE802154_SCF_SECLEVEL_MIC128:
134 	case IEEE802154_SCF_SECLEVEL_ENC_MIC128:
135 		return 16;
136 	case IEEE802154_SCF_SECLEVEL_NONE:
137 	case IEEE802154_SCF_SECLEVEL_ENC:
138 	default:
139 		return 0;
140 	}
141 }
142 
ieee802154_hdr_length(struct sk_buff * skb)143 static inline int ieee802154_hdr_length(struct sk_buff *skb)
144 {
145 	struct ieee802154_hdr hdr;
146 	int len = ieee802154_hdr_pull(skb, &hdr);
147 
148 	if (len > 0)
149 		skb_push(skb, len);
150 
151 	return len;
152 }
153 
ieee802154_addr_equal(const struct ieee802154_addr * a1,const struct ieee802154_addr * a2)154 static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
155 					 const struct ieee802154_addr *a2)
156 {
157 	if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
158 		return false;
159 
160 	if ((a1->mode == IEEE802154_ADDR_LONG &&
161 	     a1->extended_addr != a2->extended_addr) ||
162 	    (a1->mode == IEEE802154_ADDR_SHORT &&
163 	     a1->short_addr != a2->short_addr))
164 		return false;
165 
166 	return true;
167 }
168 
ieee802154_devaddr_from_raw(const void * raw)169 static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
170 {
171 	u64 temp;
172 
173 	memcpy(&temp, raw, IEEE802154_ADDR_LEN);
174 	return (__force __le64)swab64(temp);
175 }
176 
ieee802154_devaddr_to_raw(void * raw,__le64 addr)177 static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
178 {
179 	u64 temp = swab64((__force u64)addr);
180 
181 	memcpy(raw, &temp, IEEE802154_ADDR_LEN);
182 }
183 
184 static inline int
ieee802154_sockaddr_check_size(struct sockaddr_ieee802154 * daddr,int len)185 ieee802154_sockaddr_check_size(struct sockaddr_ieee802154 *daddr, int len)
186 {
187 	struct ieee802154_addr_sa *sa;
188 	int ret = 0;
189 
190 	sa = &daddr->addr;
191 	if (len < IEEE802154_MIN_NAMELEN)
192 		return -EINVAL;
193 	switch (sa->addr_type) {
194 	case IEEE802154_ADDR_NONE:
195 		break;
196 	case IEEE802154_ADDR_SHORT:
197 		if (len < IEEE802154_NAMELEN_SHORT)
198 			ret = -EINVAL;
199 		break;
200 	case IEEE802154_ADDR_LONG:
201 		if (len < IEEE802154_NAMELEN_LONG)
202 			ret = -EINVAL;
203 		break;
204 	default:
205 		ret = -EINVAL;
206 		break;
207 	}
208 	return ret;
209 }
210 
ieee802154_addr_from_sa(struct ieee802154_addr * a,const struct ieee802154_addr_sa * sa)211 static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
212 					   const struct ieee802154_addr_sa *sa)
213 {
214 	a->mode = sa->addr_type;
215 	a->pan_id = cpu_to_le16(sa->pan_id);
216 
217 	switch (a->mode) {
218 	case IEEE802154_ADDR_SHORT:
219 		a->short_addr = cpu_to_le16(sa->short_addr);
220 		break;
221 	case IEEE802154_ADDR_LONG:
222 		a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
223 		break;
224 	}
225 }
226 
ieee802154_addr_to_sa(struct ieee802154_addr_sa * sa,const struct ieee802154_addr * a)227 static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
228 					 const struct ieee802154_addr *a)
229 {
230 	sa->addr_type = a->mode;
231 	sa->pan_id = le16_to_cpu(a->pan_id);
232 
233 	switch (a->mode) {
234 	case IEEE802154_ADDR_SHORT:
235 		sa->short_addr = le16_to_cpu(a->short_addr);
236 		break;
237 	case IEEE802154_ADDR_LONG:
238 		ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
239 		break;
240 	}
241 }
242 
243 /*
244  * A control block of skb passed between the ARPHRD_IEEE802154 device
245  * and other stack parts.
246  */
247 struct ieee802154_mac_cb {
248 	u8 lqi;
249 	u8 type;
250 	bool ackreq;
251 	bool secen;
252 	bool secen_override;
253 	u8 seclevel;
254 	bool seclevel_override;
255 	struct ieee802154_addr source;
256 	struct ieee802154_addr dest;
257 };
258 
mac_cb(struct sk_buff * skb)259 static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
260 {
261 	return (struct ieee802154_mac_cb *)skb->cb;
262 }
263 
mac_cb_init(struct sk_buff * skb)264 static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb)
265 {
266 	BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
267 
268 	memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb));
269 	return mac_cb(skb);
270 }
271 
272 enum {
273 	IEEE802154_LLSEC_DEVKEY_IGNORE,
274 	IEEE802154_LLSEC_DEVKEY_RESTRICT,
275 	IEEE802154_LLSEC_DEVKEY_RECORD,
276 
277 	__IEEE802154_LLSEC_DEVKEY_MAX,
278 };
279 
280 #define IEEE802154_MAC_SCAN_ED		0
281 #define IEEE802154_MAC_SCAN_ACTIVE	1
282 #define IEEE802154_MAC_SCAN_PASSIVE	2
283 #define IEEE802154_MAC_SCAN_ORPHAN	3
284 
285 struct ieee802154_mac_params {
286 	s8 transmit_power;
287 	u8 min_be;
288 	u8 max_be;
289 	u8 csma_retries;
290 	s8 frame_retries;
291 
292 	bool lbt;
293 	struct wpan_phy_cca cca;
294 	s32 cca_ed_level;
295 };
296 
297 struct wpan_phy;
298 
299 enum {
300 	IEEE802154_LLSEC_PARAM_ENABLED		= BIT(0),
301 	IEEE802154_LLSEC_PARAM_FRAME_COUNTER	= BIT(1),
302 	IEEE802154_LLSEC_PARAM_OUT_LEVEL	= BIT(2),
303 	IEEE802154_LLSEC_PARAM_OUT_KEY		= BIT(3),
304 	IEEE802154_LLSEC_PARAM_KEY_SOURCE	= BIT(4),
305 	IEEE802154_LLSEC_PARAM_PAN_ID		= BIT(5),
306 	IEEE802154_LLSEC_PARAM_HWADDR		= BIT(6),
307 	IEEE802154_LLSEC_PARAM_COORD_HWADDR	= BIT(7),
308 	IEEE802154_LLSEC_PARAM_COORD_SHORTADDR	= BIT(8),
309 };
310 
311 struct ieee802154_llsec_ops {
312 	int (*get_params)(struct net_device *dev,
313 			  struct ieee802154_llsec_params *params);
314 	int (*set_params)(struct net_device *dev,
315 			  const struct ieee802154_llsec_params *params,
316 			  int changed);
317 
318 	int (*add_key)(struct net_device *dev,
319 		       const struct ieee802154_llsec_key_id *id,
320 		       const struct ieee802154_llsec_key *key);
321 	int (*del_key)(struct net_device *dev,
322 		       const struct ieee802154_llsec_key_id *id);
323 
324 	int (*add_dev)(struct net_device *dev,
325 		       const struct ieee802154_llsec_device *llsec_dev);
326 	int (*del_dev)(struct net_device *dev, __le64 dev_addr);
327 
328 	int (*add_devkey)(struct net_device *dev,
329 			  __le64 device_addr,
330 			  const struct ieee802154_llsec_device_key *key);
331 	int (*del_devkey)(struct net_device *dev,
332 			  __le64 device_addr,
333 			  const struct ieee802154_llsec_device_key *key);
334 
335 	int (*add_seclevel)(struct net_device *dev,
336 			    const struct ieee802154_llsec_seclevel *sl);
337 	int (*del_seclevel)(struct net_device *dev,
338 			    const struct ieee802154_llsec_seclevel *sl);
339 
340 	void (*lock_table)(struct net_device *dev);
341 	void (*get_table)(struct net_device *dev,
342 			  struct ieee802154_llsec_table **t);
343 	void (*unlock_table)(struct net_device *dev);
344 };
345 /*
346  * This should be located at net_device->ml_priv
347  *
348  * get_phy should increment the reference counting on returned phy.
349  * Use wpan_wpy_put to put that reference.
350  */
351 struct ieee802154_mlme_ops {
352 	/* The following fields are optional (can be NULL). */
353 
354 	int (*assoc_req)(struct net_device *dev,
355 			struct ieee802154_addr *addr,
356 			u8 channel, u8 page, u8 cap);
357 	int (*assoc_resp)(struct net_device *dev,
358 			struct ieee802154_addr *addr,
359 			__le16 short_addr, u8 status);
360 	int (*disassoc_req)(struct net_device *dev,
361 			struct ieee802154_addr *addr,
362 			u8 reason);
363 	int (*start_req)(struct net_device *dev,
364 			struct ieee802154_addr *addr,
365 			u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
366 			u8 pan_coord, u8 blx, u8 coord_realign);
367 	int (*scan_req)(struct net_device *dev,
368 			u8 type, u32 channels, u8 page, u8 duration);
369 
370 	int (*set_mac_params)(struct net_device *dev,
371 			      const struct ieee802154_mac_params *params);
372 	void (*get_mac_params)(struct net_device *dev,
373 			       struct ieee802154_mac_params *params);
374 
375 	const struct ieee802154_llsec_ops *llsec;
376 };
377 
378 static inline struct ieee802154_mlme_ops *
ieee802154_mlme_ops(const struct net_device * dev)379 ieee802154_mlme_ops(const struct net_device *dev)
380 {
381 	return dev->ml_priv;
382 }
383 
384 #endif
385