1f0166e5eSDmitry Eremin-Solenikov /* 2f0166e5eSDmitry Eremin-Solenikov * An interface between IEEE802.15.4 device and rest of the kernel. 3f0166e5eSDmitry Eremin-Solenikov * 474a02fcfSalex.bluesman.smirnov@gmail.com * Copyright (C) 2007-2012 Siemens AG 5f0166e5eSDmitry Eremin-Solenikov * 6f0166e5eSDmitry Eremin-Solenikov * This program is free software; you can redistribute it and/or modify 7f0166e5eSDmitry Eremin-Solenikov * it under the terms of the GNU General Public License version 2 8f0166e5eSDmitry Eremin-Solenikov * as published by the Free Software Foundation. 9f0166e5eSDmitry Eremin-Solenikov * 10f0166e5eSDmitry Eremin-Solenikov * This program is distributed in the hope that it will be useful, 11f0166e5eSDmitry Eremin-Solenikov * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f0166e5eSDmitry Eremin-Solenikov * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13f0166e5eSDmitry Eremin-Solenikov * GNU General Public License for more details. 14f0166e5eSDmitry Eremin-Solenikov * 15f0166e5eSDmitry Eremin-Solenikov * You should have received a copy of the GNU General Public License along 16f0166e5eSDmitry Eremin-Solenikov * with this program; if not, write to the Free Software Foundation, Inc., 17f0166e5eSDmitry Eremin-Solenikov * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18f0166e5eSDmitry Eremin-Solenikov * 19f0166e5eSDmitry Eremin-Solenikov * Written by: 20f0166e5eSDmitry Eremin-Solenikov * Pavel Smolenskiy <pavel.smolenskiy@gmail.com> 21f0166e5eSDmitry Eremin-Solenikov * Maxim Gorbachyov <maxim.gorbachev@siemens.com> 22f0166e5eSDmitry Eremin-Solenikov * Maxim Osipov <maxim.osipov@siemens.com> 23f0166e5eSDmitry Eremin-Solenikov * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> 2474a02fcfSalex.bluesman.smirnov@gmail.com * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> 25f0166e5eSDmitry Eremin-Solenikov */ 26f0166e5eSDmitry Eremin-Solenikov 27f0166e5eSDmitry Eremin-Solenikov #ifndef IEEE802154_NETDEVICE_H 28f0166e5eSDmitry Eremin-Solenikov #define IEEE802154_NETDEVICE_H 29f0166e5eSDmitry Eremin-Solenikov 301cd829c8Salex.bluesman.smirnov@gmail.com #include <net/af_ieee802154.h> 3194b4f6c2SPhoebe Buckheister #include <linux/netdevice.h> 3294b4f6c2SPhoebe Buckheister #include <linux/skbuff.h> 3394b4f6c2SPhoebe Buckheister 3494b4f6c2SPhoebe Buckheister struct ieee802154_sechdr { 3594b4f6c2SPhoebe Buckheister #if defined(__LITTLE_ENDIAN_BITFIELD) 3694b4f6c2SPhoebe Buckheister u8 level:3, 3794b4f6c2SPhoebe Buckheister key_id_mode:2, 3894b4f6c2SPhoebe Buckheister reserved:3; 3994b4f6c2SPhoebe Buckheister #elif defined(__BIG_ENDIAN_BITFIELD) 4094b4f6c2SPhoebe Buckheister u8 reserved:3, 4194b4f6c2SPhoebe Buckheister key_id_mode:2, 4294b4f6c2SPhoebe Buckheister level:3; 4394b4f6c2SPhoebe Buckheister #else 4494b4f6c2SPhoebe Buckheister #error "Please fix <asm/byteorder.h>" 4594b4f6c2SPhoebe Buckheister #endif 4694b4f6c2SPhoebe Buckheister u8 key_id; 4794b4f6c2SPhoebe Buckheister __le32 frame_counter; 4894b4f6c2SPhoebe Buckheister union { 4994b4f6c2SPhoebe Buckheister __le32 short_src; 5094b4f6c2SPhoebe Buckheister __le64 extended_src; 5194b4f6c2SPhoebe Buckheister }; 5294b4f6c2SPhoebe Buckheister }; 531cd829c8Salex.bluesman.smirnov@gmail.com 5446ef0eb3SPhoebe Buckheister struct ieee802154_addr { 5546ef0eb3SPhoebe Buckheister u8 mode; 5646ef0eb3SPhoebe Buckheister __le16 pan_id; 5746ef0eb3SPhoebe Buckheister union { 5846ef0eb3SPhoebe Buckheister __le16 short_addr; 5946ef0eb3SPhoebe Buckheister __le64 extended_addr; 6046ef0eb3SPhoebe Buckheister }; 6146ef0eb3SPhoebe Buckheister }; 6246ef0eb3SPhoebe Buckheister 6394b4f6c2SPhoebe Buckheister struct ieee802154_hdr_fc { 6494b4f6c2SPhoebe Buckheister #if defined(__LITTLE_ENDIAN_BITFIELD) 6594b4f6c2SPhoebe Buckheister u16 type:3, 6694b4f6c2SPhoebe Buckheister security_enabled:1, 6794b4f6c2SPhoebe Buckheister frame_pending:1, 6894b4f6c2SPhoebe Buckheister ack_request:1, 6994b4f6c2SPhoebe Buckheister intra_pan:1, 7094b4f6c2SPhoebe Buckheister reserved:3, 7194b4f6c2SPhoebe Buckheister dest_addr_mode:2, 7294b4f6c2SPhoebe Buckheister version:2, 7394b4f6c2SPhoebe Buckheister source_addr_mode:2; 7494b4f6c2SPhoebe Buckheister #elif defined(__BIG_ENDIAN_BITFIELD) 7594b4f6c2SPhoebe Buckheister u16 reserved:1, 7694b4f6c2SPhoebe Buckheister intra_pan:1, 7794b4f6c2SPhoebe Buckheister ack_request:1, 7894b4f6c2SPhoebe Buckheister frame_pending:1, 7994b4f6c2SPhoebe Buckheister security_enabled:1, 8094b4f6c2SPhoebe Buckheister type:3, 8194b4f6c2SPhoebe Buckheister source_addr_mode:2, 8294b4f6c2SPhoebe Buckheister version:2, 8394b4f6c2SPhoebe Buckheister dest_addr_mode:2, 8494b4f6c2SPhoebe Buckheister reserved2:2; 8594b4f6c2SPhoebe Buckheister #else 8694b4f6c2SPhoebe Buckheister #error "Please fix <asm/byteorder.h>" 8794b4f6c2SPhoebe Buckheister #endif 8894b4f6c2SPhoebe Buckheister }; 8994b4f6c2SPhoebe Buckheister 9094b4f6c2SPhoebe Buckheister struct ieee802154_hdr { 9194b4f6c2SPhoebe Buckheister struct ieee802154_hdr_fc fc; 9294b4f6c2SPhoebe Buckheister u8 seq; 9394b4f6c2SPhoebe Buckheister struct ieee802154_addr source; 9494b4f6c2SPhoebe Buckheister struct ieee802154_addr dest; 9594b4f6c2SPhoebe Buckheister struct ieee802154_sechdr sec; 9694b4f6c2SPhoebe Buckheister }; 9794b4f6c2SPhoebe Buckheister 9894b4f6c2SPhoebe Buckheister /* pushes hdr onto the skb. fields of hdr->fc that can be calculated from 9994b4f6c2SPhoebe Buckheister * the contents of hdr will be, and the actual value of those bits in 10094b4f6c2SPhoebe Buckheister * hdr->fc will be ignored. this includes the INTRA_PAN bit and the frame 10194b4f6c2SPhoebe Buckheister * version, if SECEN is set. 10294b4f6c2SPhoebe Buckheister */ 10394b4f6c2SPhoebe Buckheister int ieee802154_hdr_push(struct sk_buff *skb, const struct ieee802154_hdr *hdr); 10494b4f6c2SPhoebe Buckheister 10594b4f6c2SPhoebe Buckheister /* pulls the entire 802.15.4 header off of the skb, including the security 10694b4f6c2SPhoebe Buckheister * header, and performs pan id decompression 10794b4f6c2SPhoebe Buckheister */ 10894b4f6c2SPhoebe Buckheister int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr); 10994b4f6c2SPhoebe Buckheister 11094b4f6c2SPhoebe Buckheister /* parses the frame control, sequence number of address fields in a given skb 11194b4f6c2SPhoebe Buckheister * and stores them into hdr, performing pan id decompression and length checks 11294b4f6c2SPhoebe Buckheister * to be suitable for use in header_ops.parse 11394b4f6c2SPhoebe Buckheister */ 11494b4f6c2SPhoebe Buckheister int ieee802154_hdr_peek_addrs(const struct sk_buff *skb, 11594b4f6c2SPhoebe Buckheister struct ieee802154_hdr *hdr); 11694b4f6c2SPhoebe Buckheister 11794b4f6c2SPhoebe Buckheister static inline int ieee802154_hdr_length(struct sk_buff *skb) 11894b4f6c2SPhoebe Buckheister { 11994b4f6c2SPhoebe Buckheister struct ieee802154_hdr hdr; 12094b4f6c2SPhoebe Buckheister int len = ieee802154_hdr_pull(skb, &hdr); 12194b4f6c2SPhoebe Buckheister 12294b4f6c2SPhoebe Buckheister if (len > 0) 12394b4f6c2SPhoebe Buckheister skb_push(skb, len); 12494b4f6c2SPhoebe Buckheister 12594b4f6c2SPhoebe Buckheister return len; 12694b4f6c2SPhoebe Buckheister } 12794b4f6c2SPhoebe Buckheister 12846ef0eb3SPhoebe Buckheister static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1, 12946ef0eb3SPhoebe Buckheister const struct ieee802154_addr *a2) 13046ef0eb3SPhoebe Buckheister { 13146ef0eb3SPhoebe Buckheister if (a1->pan_id != a2->pan_id || a1->mode != a2->mode) 13246ef0eb3SPhoebe Buckheister return false; 13346ef0eb3SPhoebe Buckheister 13446ef0eb3SPhoebe Buckheister if ((a1->mode == IEEE802154_ADDR_LONG && 13546ef0eb3SPhoebe Buckheister a1->extended_addr != a2->extended_addr) || 13646ef0eb3SPhoebe Buckheister (a1->mode == IEEE802154_ADDR_SHORT && 13746ef0eb3SPhoebe Buckheister a1->short_addr != a2->short_addr)) 13846ef0eb3SPhoebe Buckheister return false; 13946ef0eb3SPhoebe Buckheister 14046ef0eb3SPhoebe Buckheister return true; 14146ef0eb3SPhoebe Buckheister } 14246ef0eb3SPhoebe Buckheister 14346ef0eb3SPhoebe Buckheister static inline __le64 ieee802154_devaddr_from_raw(const void *raw) 14446ef0eb3SPhoebe Buckheister { 14546ef0eb3SPhoebe Buckheister u64 temp; 14646ef0eb3SPhoebe Buckheister 14746ef0eb3SPhoebe Buckheister memcpy(&temp, raw, IEEE802154_ADDR_LEN); 14846ef0eb3SPhoebe Buckheister return (__force __le64)swab64(temp); 14946ef0eb3SPhoebe Buckheister } 15046ef0eb3SPhoebe Buckheister 15146ef0eb3SPhoebe Buckheister static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr) 15246ef0eb3SPhoebe Buckheister { 15346ef0eb3SPhoebe Buckheister u64 temp = swab64((__force u64)addr); 15446ef0eb3SPhoebe Buckheister 15546ef0eb3SPhoebe Buckheister memcpy(raw, &temp, IEEE802154_ADDR_LEN); 15646ef0eb3SPhoebe Buckheister } 15746ef0eb3SPhoebe Buckheister 15846ef0eb3SPhoebe Buckheister static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a, 15946ef0eb3SPhoebe Buckheister const struct ieee802154_addr_sa *sa) 16046ef0eb3SPhoebe Buckheister { 16146ef0eb3SPhoebe Buckheister a->mode = sa->addr_type; 16246ef0eb3SPhoebe Buckheister a->pan_id = cpu_to_le16(sa->pan_id); 16346ef0eb3SPhoebe Buckheister 16446ef0eb3SPhoebe Buckheister switch (a->mode) { 16546ef0eb3SPhoebe Buckheister case IEEE802154_ADDR_SHORT: 16646ef0eb3SPhoebe Buckheister a->short_addr = cpu_to_le16(sa->short_addr); 16746ef0eb3SPhoebe Buckheister break; 16846ef0eb3SPhoebe Buckheister case IEEE802154_ADDR_LONG: 16946ef0eb3SPhoebe Buckheister a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr); 17046ef0eb3SPhoebe Buckheister break; 17146ef0eb3SPhoebe Buckheister } 17246ef0eb3SPhoebe Buckheister } 17346ef0eb3SPhoebe Buckheister 17446ef0eb3SPhoebe Buckheister static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa, 17546ef0eb3SPhoebe Buckheister const struct ieee802154_addr *a) 17646ef0eb3SPhoebe Buckheister { 17746ef0eb3SPhoebe Buckheister sa->addr_type = a->mode; 17846ef0eb3SPhoebe Buckheister sa->pan_id = le16_to_cpu(a->pan_id); 17946ef0eb3SPhoebe Buckheister 18046ef0eb3SPhoebe Buckheister switch (a->mode) { 18146ef0eb3SPhoebe Buckheister case IEEE802154_ADDR_SHORT: 18246ef0eb3SPhoebe Buckheister sa->short_addr = le16_to_cpu(a->short_addr); 18346ef0eb3SPhoebe Buckheister break; 18446ef0eb3SPhoebe Buckheister case IEEE802154_ADDR_LONG: 18546ef0eb3SPhoebe Buckheister ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr); 18646ef0eb3SPhoebe Buckheister break; 18746ef0eb3SPhoebe Buckheister } 18846ef0eb3SPhoebe Buckheister } 18946ef0eb3SPhoebe Buckheister 190f0166e5eSDmitry Eremin-Solenikov /* 191f0166e5eSDmitry Eremin-Solenikov * A control block of skb passed between the ARPHRD_IEEE802154 device 192f0166e5eSDmitry Eremin-Solenikov * and other stack parts. 193f0166e5eSDmitry Eremin-Solenikov */ 194f0166e5eSDmitry Eremin-Solenikov struct ieee802154_mac_cb { 195f0166e5eSDmitry Eremin-Solenikov u8 lqi; 196f0166e5eSDmitry Eremin-Solenikov u8 flags; 197f0166e5eSDmitry Eremin-Solenikov u8 seq; 198ae531b94SPhoebe Buckheister struct ieee802154_addr source; 199ae531b94SPhoebe Buckheister struct ieee802154_addr dest; 200f0166e5eSDmitry Eremin-Solenikov }; 201f0166e5eSDmitry Eremin-Solenikov 202f0166e5eSDmitry Eremin-Solenikov static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb) 203f0166e5eSDmitry Eremin-Solenikov { 204f0166e5eSDmitry Eremin-Solenikov return (struct ieee802154_mac_cb *)skb->cb; 205f0166e5eSDmitry Eremin-Solenikov } 206f0166e5eSDmitry Eremin-Solenikov 207f0166e5eSDmitry Eremin-Solenikov #define MAC_CB_FLAG_TYPEMASK ((1 << 3) - 1) 208f0166e5eSDmitry Eremin-Solenikov 209f0166e5eSDmitry Eremin-Solenikov #define MAC_CB_FLAG_ACKREQ (1 << 3) 210f0166e5eSDmitry Eremin-Solenikov #define MAC_CB_FLAG_SECEN (1 << 4) 211f0166e5eSDmitry Eremin-Solenikov 212e6278d92SPhoebe Buckheister static inline bool mac_cb_is_ackreq(struct sk_buff *skb) 213f0166e5eSDmitry Eremin-Solenikov { 214f0166e5eSDmitry Eremin-Solenikov return mac_cb(skb)->flags & MAC_CB_FLAG_ACKREQ; 215f0166e5eSDmitry Eremin-Solenikov } 216f0166e5eSDmitry Eremin-Solenikov 217e6278d92SPhoebe Buckheister static inline bool mac_cb_is_secen(struct sk_buff *skb) 218f0166e5eSDmitry Eremin-Solenikov { 219f0166e5eSDmitry Eremin-Solenikov return mac_cb(skb)->flags & MAC_CB_FLAG_SECEN; 220f0166e5eSDmitry Eremin-Solenikov } 221f0166e5eSDmitry Eremin-Solenikov 222f0166e5eSDmitry Eremin-Solenikov static inline int mac_cb_type(struct sk_buff *skb) 223f0166e5eSDmitry Eremin-Solenikov { 224f0166e5eSDmitry Eremin-Solenikov return mac_cb(skb)->flags & MAC_CB_FLAG_TYPEMASK; 225f0166e5eSDmitry Eremin-Solenikov } 226f0166e5eSDmitry Eremin-Solenikov 227f0166e5eSDmitry Eremin-Solenikov #define IEEE802154_MAC_SCAN_ED 0 228f0166e5eSDmitry Eremin-Solenikov #define IEEE802154_MAC_SCAN_ACTIVE 1 229f0166e5eSDmitry Eremin-Solenikov #define IEEE802154_MAC_SCAN_PASSIVE 2 230f0166e5eSDmitry Eremin-Solenikov #define IEEE802154_MAC_SCAN_ORPHAN 3 231f0166e5eSDmitry Eremin-Solenikov 232*e462ded6SPhoebe Buckheister struct ieee802154_mac_params { 233*e462ded6SPhoebe Buckheister s8 transmit_power; 234*e462ded6SPhoebe Buckheister u8 min_be; 235*e462ded6SPhoebe Buckheister u8 max_be; 236*e462ded6SPhoebe Buckheister u8 csma_retries; 237*e462ded6SPhoebe Buckheister s8 frame_retries; 238*e462ded6SPhoebe Buckheister 239*e462ded6SPhoebe Buckheister bool lbt; 240*e462ded6SPhoebe Buckheister u8 cca_mode; 241*e462ded6SPhoebe Buckheister s32 cca_ed_level; 242*e462ded6SPhoebe Buckheister }; 243*e462ded6SPhoebe Buckheister 24442723448SDmitry Eremin-Solenikov struct wpan_phy; 245f0166e5eSDmitry Eremin-Solenikov /* 246f0166e5eSDmitry Eremin-Solenikov * This should be located at net_device->ml_priv 24742723448SDmitry Eremin-Solenikov * 24842723448SDmitry Eremin-Solenikov * get_phy should increment the reference counting on returned phy. 24942723448SDmitry Eremin-Solenikov * Use wpan_wpy_put to put that reference. 250f0166e5eSDmitry Eremin-Solenikov */ 251f0166e5eSDmitry Eremin-Solenikov struct ieee802154_mlme_ops { 25256aa091dSWerner Almesberger /* The following fields are optional (can be NULL). */ 25356aa091dSWerner Almesberger 254f0166e5eSDmitry Eremin-Solenikov int (*assoc_req)(struct net_device *dev, 255ae531b94SPhoebe Buckheister struct ieee802154_addr *addr, 25616eea493SDmitry Eremin-Solenikov u8 channel, u8 page, u8 cap); 257f0166e5eSDmitry Eremin-Solenikov int (*assoc_resp)(struct net_device *dev, 258ae531b94SPhoebe Buckheister struct ieee802154_addr *addr, 259b70ab2e8SPhoebe Buckheister __le16 short_addr, u8 status); 260f0166e5eSDmitry Eremin-Solenikov int (*disassoc_req)(struct net_device *dev, 261ae531b94SPhoebe Buckheister struct ieee802154_addr *addr, 262f0166e5eSDmitry Eremin-Solenikov u8 reason); 263f0166e5eSDmitry Eremin-Solenikov int (*start_req)(struct net_device *dev, 264ae531b94SPhoebe Buckheister struct ieee802154_addr *addr, 26516eea493SDmitry Eremin-Solenikov u8 channel, u8 page, u8 bcn_ord, u8 sf_ord, 266f0166e5eSDmitry Eremin-Solenikov u8 pan_coord, u8 blx, u8 coord_realign); 267f0166e5eSDmitry Eremin-Solenikov int (*scan_req)(struct net_device *dev, 26816eea493SDmitry Eremin-Solenikov u8 type, u32 channels, u8 page, u8 duration); 269f0166e5eSDmitry Eremin-Solenikov 270*e462ded6SPhoebe Buckheister int (*set_mac_params)(struct net_device *dev, 271*e462ded6SPhoebe Buckheister const struct ieee802154_mac_params *params); 272*e462ded6SPhoebe Buckheister void (*get_mac_params)(struct net_device *dev, 273*e462ded6SPhoebe Buckheister struct ieee802154_mac_params *params); 274*e462ded6SPhoebe Buckheister 27556aa091dSWerner Almesberger /* The fields below are required. */ 27656aa091dSWerner Almesberger 27742723448SDmitry Eremin-Solenikov struct wpan_phy *(*get_phy)(const struct net_device *dev); 27842723448SDmitry Eremin-Solenikov 279f0166e5eSDmitry Eremin-Solenikov /* 280f0166e5eSDmitry Eremin-Solenikov * FIXME: these should become the part of PIB/MIB interface. 281f0166e5eSDmitry Eremin-Solenikov * However we still don't have IB interface of any kind 282f0166e5eSDmitry Eremin-Solenikov */ 283b70ab2e8SPhoebe Buckheister __le16 (*get_pan_id)(const struct net_device *dev); 284b70ab2e8SPhoebe Buckheister __le16 (*get_short_addr)(const struct net_device *dev); 285a9966b58SDmitry Eremin-Solenikov u8 (*get_dsn)(const struct net_device *dev); 286f0166e5eSDmitry Eremin-Solenikov }; 287f0166e5eSDmitry Eremin-Solenikov 28874a02fcfSalex.bluesman.smirnov@gmail.com /* The IEEE 802.15.4 standard defines 2 type of the devices: 28974a02fcfSalex.bluesman.smirnov@gmail.com * - FFD - full functionality device 29074a02fcfSalex.bluesman.smirnov@gmail.com * - RFD - reduce functionality device 29174a02fcfSalex.bluesman.smirnov@gmail.com * 29274a02fcfSalex.bluesman.smirnov@gmail.com * So 2 sets of mlme operations are needed 29374a02fcfSalex.bluesman.smirnov@gmail.com */ 29474a02fcfSalex.bluesman.smirnov@gmail.com struct ieee802154_reduced_mlme_ops { 29574a02fcfSalex.bluesman.smirnov@gmail.com struct wpan_phy *(*get_phy)(const struct net_device *dev); 29674a02fcfSalex.bluesman.smirnov@gmail.com }; 29774a02fcfSalex.bluesman.smirnov@gmail.com 29874a02fcfSalex.bluesman.smirnov@gmail.com static inline struct ieee802154_mlme_ops * 29974a02fcfSalex.bluesman.smirnov@gmail.com ieee802154_mlme_ops(const struct net_device *dev) 30074a02fcfSalex.bluesman.smirnov@gmail.com { 30174a02fcfSalex.bluesman.smirnov@gmail.com return dev->ml_priv; 30274a02fcfSalex.bluesman.smirnov@gmail.com } 30374a02fcfSalex.bluesman.smirnov@gmail.com 30474a02fcfSalex.bluesman.smirnov@gmail.com static inline struct ieee802154_reduced_mlme_ops * 30574a02fcfSalex.bluesman.smirnov@gmail.com ieee802154_reduced_mlme_ops(const struct net_device *dev) 306f0166e5eSDmitry Eremin-Solenikov { 307f0166e5eSDmitry Eremin-Solenikov return dev->ml_priv; 308f0166e5eSDmitry Eremin-Solenikov } 309f0166e5eSDmitry Eremin-Solenikov 310f0166e5eSDmitry Eremin-Solenikov #endif 311