2 * An interface between IEEE802.15.4 device and rest of the kernel.
4 * Copyright (C) 2007-2012 Siemens AG
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
16 * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
17 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
18 * Maxim Osipov <maxim.osipov@siemens.com>
19 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
20 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
23 #ifndef IEEE802154_NETDEVICE_H
24 #define IEEE802154_NETDEVICE_H
26 #include <net/af_ieee802154.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/ieee802154.h>
31 #include <net/cfg802154.h>
33 struct ieee802154_sechdr {
34 #if defined(__LITTLE_ENDIAN_BITFIELD)
38 #elif defined(__BIG_ENDIAN_BITFIELD)
43 #error "Please fix <asm/byteorder.h>"
53 struct ieee802154_hdr_fc {
54 #if defined(__LITTLE_ENDIAN_BITFIELD)
64 #elif defined(__BIG_ENDIAN_BITFIELD)
76 #error "Please fix <asm/byteorder.h>"
80 struct ieee802154_hdr {
81 struct ieee802154_hdr_fc fc;
83 struct ieee802154_addr source;
84 struct ieee802154_addr dest;
85 struct ieee802154_sechdr sec;
88 /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
89 * the contents of hdr will be, and the actual value of those bits in
90 * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
91 * version, if SECEN is set.
93 int ieee802154_hdr_push(struct sk_buff *skb, struct ieee802154_hdr *hdr);
95 /* pulls the entire 802.15.4 header off of the skb, including the security
96 * header, and performs pan id decompression
98 int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
100 /* parses the frame control, sequence number of address fields in a given skb
101 * and stores them into hdr, performing pan id decompression and length checks
102 * to be suitable for use in header_ops.parse
104 int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
105 struct ieee802154_hdr *hdr);
107 /* parses the full 802.15.4 header a given skb and stores them into hdr,
108 * performing pan id decompression and length checks to be suitable for use in
111 int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr);
113 int ieee802154_max_payload(const struct ieee802154_hdr *hdr);
116 ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec)
118 switch (sec->level) {
119 case IEEE802154_SCF_SECLEVEL_MIC32:
120 case IEEE802154_SCF_SECLEVEL_ENC_MIC32:
122 case IEEE802154_SCF_SECLEVEL_MIC64:
123 case IEEE802154_SCF_SECLEVEL_ENC_MIC64:
125 case IEEE802154_SCF_SECLEVEL_MIC128:
126 case IEEE802154_SCF_SECLEVEL_ENC_MIC128:
128 case IEEE802154_SCF_SECLEVEL_NONE:
129 case IEEE802154_SCF_SECLEVEL_ENC:
135 static inline int ieee802154_hdr_length(struct sk_buff *skb)
137 struct ieee802154_hdr hdr;
138 int len = ieee802154_hdr_pull(skb, &hdr);
146 static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
147 const struct ieee802154_addr *a2)
149 if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
152 if ((a1->mode == IEEE802154_ADDR_LONG &&
153 a1->extended_addr != a2->extended_addr) ||
154 (a1->mode == IEEE802154_ADDR_SHORT &&
155 a1->short_addr != a2->short_addr))
161 static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
165 memcpy(&temp, raw, IEEE802154_ADDR_LEN);
166 return (__force __le64)swab64(temp);
169 static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
171 u64 temp = swab64((__force u64)addr);
173 memcpy(raw, &temp, IEEE802154_ADDR_LEN);
176 static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
177 const struct ieee802154_addr_sa *sa)
179 a->mode = sa->addr_type;
180 a->pan_id = cpu_to_le16(sa->pan_id);
183 case IEEE802154_ADDR_SHORT:
184 a->short_addr = cpu_to_le16(sa->short_addr);
186 case IEEE802154_ADDR_LONG:
187 a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
192 static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
193 const struct ieee802154_addr *a)
195 sa->addr_type = a->mode;
196 sa->pan_id = le16_to_cpu(a->pan_id);
199 case IEEE802154_ADDR_SHORT:
200 sa->short_addr = le16_to_cpu(a->short_addr);
202 case IEEE802154_ADDR_LONG:
203 ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
209 * A control block of skb passed between the ARPHRD_IEEE802154 device
210 * and other stack parts.
212 struct ieee802154_mac_cb {
219 bool seclevel_override;
220 struct ieee802154_addr source;
221 struct ieee802154_addr dest;
224 static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
226 return (struct ieee802154_mac_cb *)skb->cb;
229 static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb)
231 BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
233 memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb));
237 #define IEEE802154_LLSEC_KEY_SIZE 16
239 struct ieee802154_llsec_key_id {
243 struct ieee802154_addr device_addr;
245 __le64 extended_source;
249 struct ieee802154_llsec_key {
252 u8 key[IEEE802154_LLSEC_KEY_SIZE];
255 struct ieee802154_llsec_key_entry {
256 struct list_head list;
258 struct ieee802154_llsec_key_id id;
259 struct ieee802154_llsec_key *key;
262 struct ieee802154_llsec_device_key {
263 struct list_head list;
265 struct ieee802154_llsec_key_id key_id;
270 IEEE802154_LLSEC_DEVKEY_IGNORE,
271 IEEE802154_LLSEC_DEVKEY_RESTRICT,
272 IEEE802154_LLSEC_DEVKEY_RECORD,
274 __IEEE802154_LLSEC_DEVKEY_MAX,
277 struct ieee802154_llsec_device {
278 struct list_head list;
284 bool seclevel_exempt;
287 struct list_head keys;
290 struct ieee802154_llsec_seclevel {
291 struct list_head list;
295 bool device_override;
299 struct ieee802154_llsec_params {
302 __be32 frame_counter;
304 struct ieee802154_llsec_key_id out_key;
306 __le64 default_key_source;
311 __le16 coord_shortaddr;
314 struct ieee802154_llsec_table {
315 struct list_head keys;
316 struct list_head devices;
317 struct list_head security_levels;
320 #define IEEE802154_MAC_SCAN_ED 0
321 #define IEEE802154_MAC_SCAN_ACTIVE 1
322 #define IEEE802154_MAC_SCAN_PASSIVE 2
323 #define IEEE802154_MAC_SCAN_ORPHAN 3
325 struct ieee802154_mac_params {
333 struct wpan_phy_cca cca;
340 IEEE802154_LLSEC_PARAM_ENABLED = BIT(0),
341 IEEE802154_LLSEC_PARAM_FRAME_COUNTER = BIT(1),
342 IEEE802154_LLSEC_PARAM_OUT_LEVEL = BIT(2),
343 IEEE802154_LLSEC_PARAM_OUT_KEY = BIT(3),
344 IEEE802154_LLSEC_PARAM_KEY_SOURCE = BIT(4),
345 IEEE802154_LLSEC_PARAM_PAN_ID = BIT(5),
346 IEEE802154_LLSEC_PARAM_HWADDR = BIT(6),
347 IEEE802154_LLSEC_PARAM_COORD_HWADDR = BIT(7),
348 IEEE802154_LLSEC_PARAM_COORD_SHORTADDR = BIT(8),
351 struct ieee802154_llsec_ops {
352 int (*get_params)(struct net_device *dev,
353 struct ieee802154_llsec_params *params);
354 int (*set_params)(struct net_device *dev,
355 const struct ieee802154_llsec_params *params,
358 int (*add_key)(struct net_device *dev,
359 const struct ieee802154_llsec_key_id *id,
360 const struct ieee802154_llsec_key *key);
361 int (*del_key)(struct net_device *dev,
362 const struct ieee802154_llsec_key_id *id);
364 int (*add_dev)(struct net_device *dev,
365 const struct ieee802154_llsec_device *llsec_dev);
366 int (*del_dev)(struct net_device *dev, __le64 dev_addr);
368 int (*add_devkey)(struct net_device *dev,
370 const struct ieee802154_llsec_device_key *key);
371 int (*del_devkey)(struct net_device *dev,
373 const struct ieee802154_llsec_device_key *key);
375 int (*add_seclevel)(struct net_device *dev,
376 const struct ieee802154_llsec_seclevel *sl);
377 int (*del_seclevel)(struct net_device *dev,
378 const struct ieee802154_llsec_seclevel *sl);
380 void (*lock_table)(struct net_device *dev);
381 void (*get_table)(struct net_device *dev,
382 struct ieee802154_llsec_table **t);
383 void (*unlock_table)(struct net_device *dev);
386 * This should be located at net_device->ml_priv
388 * get_phy should increment the reference counting on returned phy.
389 * Use wpan_wpy_put to put that reference.
391 struct ieee802154_mlme_ops {
392 /* The following fields are optional (can be NULL). */
394 int (*assoc_req)(struct net_device *dev,
395 struct ieee802154_addr *addr,
396 u8 channel, u8 page, u8 cap);
397 int (*assoc_resp)(struct net_device *dev,
398 struct ieee802154_addr *addr,
399 __le16 short_addr, u8 status);
400 int (*disassoc_req)(struct net_device *dev,
401 struct ieee802154_addr *addr,
403 int (*start_req)(struct net_device *dev,
404 struct ieee802154_addr *addr,
405 u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
406 u8 pan_coord, u8 blx, u8 coord_realign);
407 int (*scan_req)(struct net_device *dev,
408 u8 type, u32 channels, u8 page, u8 duration);
410 int (*set_mac_params)(struct net_device *dev,
411 const struct ieee802154_mac_params *params);
412 void (*get_mac_params)(struct net_device *dev,
413 struct ieee802154_mac_params *params);
415 struct ieee802154_llsec_ops *llsec;
418 static inline struct ieee802154_mlme_ops *
419 ieee802154_mlme_ops(const struct net_device *dev)