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.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 * Pavel Smolenskiy <pavel.smolenskiy@gmail.com>
21 * Maxim Gorbachyov <maxim.gorbachev@siemens.com>
22 * Maxim Osipov <maxim.osipov@siemens.com>
23 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
24 * Alexander Smirnov <alex.bluesman.smirnov@gmail.com>
27 #ifndef IEEE802154_NETDEVICE_H
28 #define IEEE802154_NETDEVICE_H
30 #include <net/ieee802154.h>
31 #include <net/af_ieee802154.h>
32 #include <linux/netdevice.h>
33 #include <linux/skbuff.h>
35 struct ieee802154_sechdr {
36 #if defined(__LITTLE_ENDIAN_BITFIELD)
40 #elif defined(__BIG_ENDIAN_BITFIELD)
45 #error "Please fix <asm/byteorder.h>"
55 struct ieee802154_addr {
64 struct ieee802154_hdr_fc {
65 #if defined(__LITTLE_ENDIAN_BITFIELD)
75 #elif defined(__BIG_ENDIAN_BITFIELD)
87 #error "Please fix <asm/byteorder.h>"
91 struct ieee802154_hdr {
92 struct ieee802154_hdr_fc fc;
94 struct ieee802154_addr source;
95 struct ieee802154_addr dest;
96 struct ieee802154_sechdr sec;
99 /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from
100 * the contents of hdr will be, and the actual value of those bits in
101 * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame
102 * version, if SECEN is set.
104 int ieee802154_hdr_push(struct sk_buff *skb, const struct ieee802154_hdr *hdr);
106 /* pulls the entire 802.15.4 header off of the skb, including the security
107 * header, and performs pan id decompression
109 int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr);
111 /* parses the frame control, sequence number of address fields in a given skb
112 * and stores them into hdr, performing pan id decompression and length checks
113 * to be suitable for use in header_ops.parse
115 int ieee802154_hdr_peek_addrs(const struct sk_buff *skb,
116 struct ieee802154_hdr *hdr);
118 /* parses the full 802.15.4 header a given skb and stores them into hdr,
119 * performing pan id decompression and length checks to be suitable for use in
122 int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr);
124 int ieee802154_max_payload(const struct ieee802154_hdr *hdr);
127 ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec)
129 switch (sec->level) {
130 case IEEE802154_SCF_SECLEVEL_MIC32:
131 case IEEE802154_SCF_SECLEVEL_ENC_MIC32:
133 case IEEE802154_SCF_SECLEVEL_MIC64:
134 case IEEE802154_SCF_SECLEVEL_ENC_MIC64:
136 case IEEE802154_SCF_SECLEVEL_MIC128:
137 case IEEE802154_SCF_SECLEVEL_ENC_MIC128:
139 case IEEE802154_SCF_SECLEVEL_NONE:
140 case IEEE802154_SCF_SECLEVEL_ENC:
146 static inline int ieee802154_hdr_length(struct sk_buff *skb)
148 struct ieee802154_hdr hdr;
149 int len = ieee802154_hdr_pull(skb, &hdr);
157 static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1,
158 const struct ieee802154_addr *a2)
160 if (a1->pan_id != a2->pan_id || a1->mode != a2->mode)
163 if ((a1->mode == IEEE802154_ADDR_LONG &&
164 a1->extended_addr != a2->extended_addr) ||
165 (a1->mode == IEEE802154_ADDR_SHORT &&
166 a1->short_addr != a2->short_addr))
172 static inline __le64 ieee802154_devaddr_from_raw(const void *raw)
176 memcpy(&temp, raw, IEEE802154_ADDR_LEN);
177 return (__force __le64)swab64(temp);
180 static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr)
182 u64 temp = swab64((__force u64)addr);
184 memcpy(raw, &temp, IEEE802154_ADDR_LEN);
187 static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a,
188 const struct ieee802154_addr_sa *sa)
190 a->mode = sa->addr_type;
191 a->pan_id = cpu_to_le16(sa->pan_id);
194 case IEEE802154_ADDR_SHORT:
195 a->short_addr = cpu_to_le16(sa->short_addr);
197 case IEEE802154_ADDR_LONG:
198 a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr);
203 static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa,
204 const struct ieee802154_addr *a)
206 sa->addr_type = a->mode;
207 sa->pan_id = le16_to_cpu(a->pan_id);
210 case IEEE802154_ADDR_SHORT:
211 sa->short_addr = le16_to_cpu(a->short_addr);
213 case IEEE802154_ADDR_LONG:
214 ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr);
220 * A control block of skb passed between the ARPHRD_IEEE802154 device
221 * and other stack parts.
223 struct ieee802154_mac_cb {
228 struct ieee802154_addr source;
229 struct ieee802154_addr dest;
232 static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb)
234 return (struct ieee802154_mac_cb *)skb->cb;
237 static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb)
239 BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb));
241 memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb));
245 #define IEEE802154_MAC_SCAN_ED 0
246 #define IEEE802154_MAC_SCAN_ACTIVE 1
247 #define IEEE802154_MAC_SCAN_PASSIVE 2
248 #define IEEE802154_MAC_SCAN_ORPHAN 3
250 struct ieee802154_mac_params {
264 * This should be located at net_device->ml_priv
266 * get_phy should increment the reference counting on returned phy.
267 * Use wpan_wpy_put to put that reference.
269 struct ieee802154_mlme_ops {
270 /* The following fields are optional (can be NULL). */
272 int (*assoc_req)(struct net_device *dev,
273 struct ieee802154_addr *addr,
274 u8 channel, u8 page, u8 cap);
275 int (*assoc_resp)(struct net_device *dev,
276 struct ieee802154_addr *addr,
277 __le16 short_addr, u8 status);
278 int (*disassoc_req)(struct net_device *dev,
279 struct ieee802154_addr *addr,
281 int (*start_req)(struct net_device *dev,
282 struct ieee802154_addr *addr,
283 u8 channel, u8 page, u8 bcn_ord, u8 sf_ord,
284 u8 pan_coord, u8 blx, u8 coord_realign);
285 int (*scan_req)(struct net_device *dev,
286 u8 type, u32 channels, u8 page, u8 duration);
288 int (*set_mac_params)(struct net_device *dev,
289 const struct ieee802154_mac_params *params);
290 void (*get_mac_params)(struct net_device *dev,
291 struct ieee802154_mac_params *params);
293 /* The fields below are required. */
295 struct wpan_phy *(*get_phy)(const struct net_device *dev);
298 * FIXME: these should become the part of PIB/MIB interface.
299 * However we still don't have IB interface of any kind
301 __le16 (*get_pan_id)(const struct net_device *dev);
302 __le16 (*get_short_addr)(const struct net_device *dev);
303 u8 (*get_dsn)(const struct net_device *dev);
306 /* The IEEE 802.15.4 standard defines 2 type of the devices:
307 * - FFD - full functionality device
308 * - RFD - reduce functionality device
310 * So 2 sets of mlme operations are needed
312 struct ieee802154_reduced_mlme_ops {
313 struct wpan_phy *(*get_phy)(const struct net_device *dev);
316 static inline struct ieee802154_mlme_ops *
317 ieee802154_mlme_ops(const struct net_device *dev)
322 static inline struct ieee802154_reduced_mlme_ops *
323 ieee802154_reduced_mlme_ops(const struct net_device *dev)