Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * An interface between IEEE802.15.4 device and rest of the kernel. |
| 3 | * |
alex.bluesman.smirnov@gmail.com | 74a02fc | 2012-05-15 20:50:23 +0000 | [diff] [blame] | 4 | * Copyright (C) 2007-2012 Siemens AG |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 5 | * |
| 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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. |
| 18 | * |
| 19 | * Written by: |
| 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> |
alex.bluesman.smirnov@gmail.com | 74a02fc | 2012-05-15 20:50:23 +0000 | [diff] [blame] | 24 | * Alexander Smirnov <alex.bluesman.smirnov@gmail.com> |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 25 | */ |
| 26 | |
| 27 | #ifndef IEEE802154_NETDEVICE_H |
| 28 | #define IEEE802154_NETDEVICE_H |
| 29 | |
Phoebe Buckheister | c3a6114 | 2014-05-14 17:43:06 +0200 | [diff] [blame] | 30 | #include <net/ieee802154.h> |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 31 | #include <net/af_ieee802154.h> |
Phoebe Buckheister | 94b4f6c | 2014-03-14 21:24:00 +0100 | [diff] [blame] | 32 | #include <linux/netdevice.h> |
| 33 | #include <linux/skbuff.h> |
| 34 | |
| 35 | struct ieee802154_sechdr { |
| 36 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 37 | u8 level:3, |
| 38 | key_id_mode:2, |
| 39 | reserved:3; |
| 40 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 41 | u8 reserved:3, |
| 42 | key_id_mode:2, |
| 43 | level:3; |
| 44 | #else |
| 45 | #error "Please fix <asm/byteorder.h>" |
| 46 | #endif |
| 47 | u8 key_id; |
| 48 | __le32 frame_counter; |
| 49 | union { |
| 50 | __le32 short_src; |
| 51 | __le64 extended_src; |
| 52 | }; |
| 53 | }; |
alex.bluesman.smirnov@gmail.com | 1cd829c | 2012-05-15 20:50:21 +0000 | [diff] [blame] | 54 | |
Phoebe Buckheister | 46ef0eb | 2014-03-14 21:23:58 +0100 | [diff] [blame] | 55 | struct ieee802154_addr { |
| 56 | u8 mode; |
| 57 | __le16 pan_id; |
| 58 | union { |
| 59 | __le16 short_addr; |
| 60 | __le64 extended_addr; |
| 61 | }; |
| 62 | }; |
| 63 | |
Phoebe Buckheister | 94b4f6c | 2014-03-14 21:24:00 +0100 | [diff] [blame] | 64 | struct ieee802154_hdr_fc { |
| 65 | #if defined(__LITTLE_ENDIAN_BITFIELD) |
| 66 | u16 type:3, |
| 67 | security_enabled:1, |
| 68 | frame_pending:1, |
| 69 | ack_request:1, |
| 70 | intra_pan:1, |
| 71 | reserved:3, |
| 72 | dest_addr_mode:2, |
| 73 | version:2, |
| 74 | source_addr_mode:2; |
| 75 | #elif defined(__BIG_ENDIAN_BITFIELD) |
| 76 | u16 reserved:1, |
| 77 | intra_pan:1, |
| 78 | ack_request:1, |
| 79 | frame_pending:1, |
| 80 | security_enabled:1, |
| 81 | type:3, |
| 82 | source_addr_mode:2, |
| 83 | version:2, |
| 84 | dest_addr_mode:2, |
| 85 | reserved2:2; |
| 86 | #else |
| 87 | #error "Please fix <asm/byteorder.h>" |
| 88 | #endif |
| 89 | }; |
| 90 | |
| 91 | struct ieee802154_hdr { |
| 92 | struct ieee802154_hdr_fc fc; |
| 93 | u8 seq; |
| 94 | struct ieee802154_addr source; |
| 95 | struct ieee802154_addr dest; |
| 96 | struct ieee802154_sechdr sec; |
| 97 | }; |
| 98 | |
| 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. |
| 103 | */ |
| 104 | int ieee802154_hdr_push(struct sk_buff *skb, const struct ieee802154_hdr *hdr); |
| 105 | |
| 106 | /* pulls the entire 802.15.4 header off of the skb, including the security |
| 107 | * header, and performs pan id decompression |
| 108 | */ |
| 109 | int ieee802154_hdr_pull(struct sk_buff *skb, struct ieee802154_hdr *hdr); |
| 110 | |
| 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 |
| 114 | */ |
| 115 | int ieee802154_hdr_peek_addrs(const struct sk_buff *skb, |
| 116 | struct ieee802154_hdr *hdr); |
| 117 | |
Phoebe Buckheister | c3a6114 | 2014-05-14 17:43:06 +0200 | [diff] [blame] | 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 |
| 120 | * header_ops.parse |
| 121 | */ |
| 122 | int ieee802154_hdr_peek(const struct sk_buff *skb, struct ieee802154_hdr *hdr); |
| 123 | |
| 124 | int ieee802154_max_payload(const struct ieee802154_hdr *hdr); |
| 125 | |
| 126 | static inline int |
| 127 | ieee802154_sechdr_authtag_len(const struct ieee802154_sechdr *sec) |
| 128 | { |
| 129 | switch (sec->level) { |
| 130 | case IEEE802154_SCF_SECLEVEL_MIC32: |
| 131 | case IEEE802154_SCF_SECLEVEL_ENC_MIC32: |
| 132 | return 4; |
| 133 | case IEEE802154_SCF_SECLEVEL_MIC64: |
| 134 | case IEEE802154_SCF_SECLEVEL_ENC_MIC64: |
| 135 | return 8; |
| 136 | case IEEE802154_SCF_SECLEVEL_MIC128: |
| 137 | case IEEE802154_SCF_SECLEVEL_ENC_MIC128: |
| 138 | return 16; |
| 139 | case IEEE802154_SCF_SECLEVEL_NONE: |
| 140 | case IEEE802154_SCF_SECLEVEL_ENC: |
| 141 | default: |
| 142 | return 0; |
| 143 | } |
| 144 | } |
| 145 | |
Phoebe Buckheister | 94b4f6c | 2014-03-14 21:24:00 +0100 | [diff] [blame] | 146 | static inline int ieee802154_hdr_length(struct sk_buff *skb) |
| 147 | { |
| 148 | struct ieee802154_hdr hdr; |
| 149 | int len = ieee802154_hdr_pull(skb, &hdr); |
| 150 | |
| 151 | if (len > 0) |
| 152 | skb_push(skb, len); |
| 153 | |
| 154 | return len; |
| 155 | } |
| 156 | |
Phoebe Buckheister | 46ef0eb | 2014-03-14 21:23:58 +0100 | [diff] [blame] | 157 | static inline bool ieee802154_addr_equal(const struct ieee802154_addr *a1, |
| 158 | const struct ieee802154_addr *a2) |
| 159 | { |
| 160 | if (a1->pan_id != a2->pan_id || a1->mode != a2->mode) |
| 161 | return false; |
| 162 | |
| 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)) |
| 167 | return false; |
| 168 | |
| 169 | return true; |
| 170 | } |
| 171 | |
| 172 | static inline __le64 ieee802154_devaddr_from_raw(const void *raw) |
| 173 | { |
| 174 | u64 temp; |
| 175 | |
| 176 | memcpy(&temp, raw, IEEE802154_ADDR_LEN); |
| 177 | return (__force __le64)swab64(temp); |
| 178 | } |
| 179 | |
| 180 | static inline void ieee802154_devaddr_to_raw(void *raw, __le64 addr) |
| 181 | { |
| 182 | u64 temp = swab64((__force u64)addr); |
| 183 | |
| 184 | memcpy(raw, &temp, IEEE802154_ADDR_LEN); |
| 185 | } |
| 186 | |
| 187 | static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a, |
| 188 | const struct ieee802154_addr_sa *sa) |
| 189 | { |
| 190 | a->mode = sa->addr_type; |
| 191 | a->pan_id = cpu_to_le16(sa->pan_id); |
| 192 | |
| 193 | switch (a->mode) { |
| 194 | case IEEE802154_ADDR_SHORT: |
| 195 | a->short_addr = cpu_to_le16(sa->short_addr); |
| 196 | break; |
| 197 | case IEEE802154_ADDR_LONG: |
| 198 | a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr); |
| 199 | break; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa, |
| 204 | const struct ieee802154_addr *a) |
| 205 | { |
| 206 | sa->addr_type = a->mode; |
| 207 | sa->pan_id = le16_to_cpu(a->pan_id); |
| 208 | |
| 209 | switch (a->mode) { |
| 210 | case IEEE802154_ADDR_SHORT: |
| 211 | sa->short_addr = le16_to_cpu(a->short_addr); |
| 212 | break; |
| 213 | case IEEE802154_ADDR_LONG: |
| 214 | ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr); |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 219 | /* |
| 220 | * A control block of skb passed between the ARPHRD_IEEE802154 device |
| 221 | * and other stack parts. |
| 222 | */ |
| 223 | struct ieee802154_mac_cb { |
| 224 | u8 lqi; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 225 | u8 flags; |
| 226 | u8 seq; |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 227 | struct ieee802154_addr source; |
| 228 | struct ieee802154_addr dest; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb) |
| 232 | { |
| 233 | return (struct ieee802154_mac_cb *)skb->cb; |
| 234 | } |
| 235 | |
| 236 | #define MAC_CB_FLAG_TYPEMASK ((1 << 3) - 1) |
| 237 | |
| 238 | #define MAC_CB_FLAG_ACKREQ (1 << 3) |
| 239 | #define MAC_CB_FLAG_SECEN (1 << 4) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 240 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 241 | static inline bool mac_cb_is_ackreq(struct sk_buff *skb) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 242 | { |
| 243 | return mac_cb(skb)->flags & MAC_CB_FLAG_ACKREQ; |
| 244 | } |
| 245 | |
Phoebe Buckheister | e6278d9 | 2014-03-14 21:24:01 +0100 | [diff] [blame] | 246 | static inline bool mac_cb_is_secen(struct sk_buff *skb) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 247 | { |
| 248 | return mac_cb(skb)->flags & MAC_CB_FLAG_SECEN; |
| 249 | } |
| 250 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 251 | static inline int mac_cb_type(struct sk_buff *skb) |
| 252 | { |
| 253 | return mac_cb(skb)->flags & MAC_CB_FLAG_TYPEMASK; |
| 254 | } |
| 255 | |
| 256 | #define IEEE802154_MAC_SCAN_ED 0 |
| 257 | #define IEEE802154_MAC_SCAN_ACTIVE 1 |
| 258 | #define IEEE802154_MAC_SCAN_PASSIVE 2 |
| 259 | #define IEEE802154_MAC_SCAN_ORPHAN 3 |
| 260 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 261 | struct ieee802154_mac_params { |
| 262 | s8 transmit_power; |
| 263 | u8 min_be; |
| 264 | u8 max_be; |
| 265 | u8 csma_retries; |
| 266 | s8 frame_retries; |
| 267 | |
| 268 | bool lbt; |
| 269 | u8 cca_mode; |
| 270 | s32 cca_ed_level; |
| 271 | }; |
| 272 | |
Dmitry Eremin-Solenikov | 4272344 | 2009-11-04 17:53:23 +0300 | [diff] [blame] | 273 | struct wpan_phy; |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 274 | /* |
| 275 | * This should be located at net_device->ml_priv |
Dmitry Eremin-Solenikov | 4272344 | 2009-11-04 17:53:23 +0300 | [diff] [blame] | 276 | * |
| 277 | * get_phy should increment the reference counting on returned phy. |
| 278 | * Use wpan_wpy_put to put that reference. |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 279 | */ |
| 280 | struct ieee802154_mlme_ops { |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 281 | /* The following fields are optional (can be NULL). */ |
| 282 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 283 | int (*assoc_req)(struct net_device *dev, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 284 | struct ieee802154_addr *addr, |
Dmitry Eremin-Solenikov | 16eea49 | 2009-08-19 19:32:24 +0400 | [diff] [blame] | 285 | u8 channel, u8 page, u8 cap); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 286 | int (*assoc_resp)(struct net_device *dev, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 287 | struct ieee802154_addr *addr, |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 288 | __le16 short_addr, u8 status); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 289 | int (*disassoc_req)(struct net_device *dev, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 290 | struct ieee802154_addr *addr, |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 291 | u8 reason); |
| 292 | int (*start_req)(struct net_device *dev, |
Phoebe Buckheister | ae531b9 | 2014-03-14 21:24:02 +0100 | [diff] [blame] | 293 | struct ieee802154_addr *addr, |
Dmitry Eremin-Solenikov | 16eea49 | 2009-08-19 19:32:24 +0400 | [diff] [blame] | 294 | u8 channel, u8 page, u8 bcn_ord, u8 sf_ord, |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 295 | u8 pan_coord, u8 blx, u8 coord_realign); |
| 296 | int (*scan_req)(struct net_device *dev, |
Dmitry Eremin-Solenikov | 16eea49 | 2009-08-19 19:32:24 +0400 | [diff] [blame] | 297 | u8 type, u32 channels, u8 page, u8 duration); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 298 | |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 299 | int (*set_mac_params)(struct net_device *dev, |
| 300 | const struct ieee802154_mac_params *params); |
| 301 | void (*get_mac_params)(struct net_device *dev, |
| 302 | struct ieee802154_mac_params *params); |
| 303 | |
Werner Almesberger | 56aa091 | 2013-04-04 06:32:35 +0000 | [diff] [blame] | 304 | /* The fields below are required. */ |
| 305 | |
Dmitry Eremin-Solenikov | 4272344 | 2009-11-04 17:53:23 +0300 | [diff] [blame] | 306 | struct wpan_phy *(*get_phy)(const struct net_device *dev); |
| 307 | |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 308 | /* |
| 309 | * FIXME: these should become the part of PIB/MIB interface. |
| 310 | * However we still don't have IB interface of any kind |
| 311 | */ |
Phoebe Buckheister | b70ab2e | 2014-03-14 21:23:59 +0100 | [diff] [blame] | 312 | __le16 (*get_pan_id)(const struct net_device *dev); |
| 313 | __le16 (*get_short_addr)(const struct net_device *dev); |
Dmitry Eremin-Solenikov | a9966b5 | 2009-10-02 19:05:00 +0400 | [diff] [blame] | 314 | u8 (*get_dsn)(const struct net_device *dev); |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 315 | }; |
| 316 | |
alex.bluesman.smirnov@gmail.com | 74a02fc | 2012-05-15 20:50:23 +0000 | [diff] [blame] | 317 | /* The IEEE 802.15.4 standard defines 2 type of the devices: |
| 318 | * - FFD - full functionality device |
| 319 | * - RFD - reduce functionality device |
| 320 | * |
| 321 | * So 2 sets of mlme operations are needed |
| 322 | */ |
| 323 | struct ieee802154_reduced_mlme_ops { |
| 324 | struct wpan_phy *(*get_phy)(const struct net_device *dev); |
| 325 | }; |
| 326 | |
| 327 | static inline struct ieee802154_mlme_ops * |
| 328 | ieee802154_mlme_ops(const struct net_device *dev) |
| 329 | { |
| 330 | return dev->ml_priv; |
| 331 | } |
| 332 | |
| 333 | static inline struct ieee802154_reduced_mlme_ops * |
| 334 | ieee802154_reduced_mlme_ops(const struct net_device *dev) |
Sergey Lapin | 9ec7671 | 2009-06-08 12:18:48 +0000 | [diff] [blame] | 335 | { |
| 336 | return dev->ml_priv; |
| 337 | } |
| 338 | |
| 339 | #endif |