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 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 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 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 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 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 185 ieee802154_sockaddr_check_size(struct sockaddr_ieee802154 *daddr, int len) 186 { 187 struct ieee802154_addr_sa *sa; 188 189 sa = &daddr->addr; 190 if (len < IEEE802154_MIN_NAMELEN) 191 return -EINVAL; 192 switch (sa->addr_type) { 193 case IEEE802154_ADDR_SHORT: 194 if (len < IEEE802154_NAMELEN_SHORT) 195 return -EINVAL; 196 break; 197 case IEEE802154_ADDR_LONG: 198 if (len < IEEE802154_NAMELEN_LONG) 199 return -EINVAL; 200 break; 201 } 202 return 0; 203 } 204 205 static inline void ieee802154_addr_from_sa(struct ieee802154_addr *a, 206 const struct ieee802154_addr_sa *sa) 207 { 208 a->mode = sa->addr_type; 209 a->pan_id = cpu_to_le16(sa->pan_id); 210 211 switch (a->mode) { 212 case IEEE802154_ADDR_SHORT: 213 a->short_addr = cpu_to_le16(sa->short_addr); 214 break; 215 case IEEE802154_ADDR_LONG: 216 a->extended_addr = ieee802154_devaddr_from_raw(sa->hwaddr); 217 break; 218 } 219 } 220 221 static inline void ieee802154_addr_to_sa(struct ieee802154_addr_sa *sa, 222 const struct ieee802154_addr *a) 223 { 224 sa->addr_type = a->mode; 225 sa->pan_id = le16_to_cpu(a->pan_id); 226 227 switch (a->mode) { 228 case IEEE802154_ADDR_SHORT: 229 sa->short_addr = le16_to_cpu(a->short_addr); 230 break; 231 case IEEE802154_ADDR_LONG: 232 ieee802154_devaddr_to_raw(sa->hwaddr, a->extended_addr); 233 break; 234 } 235 } 236 237 /* 238 * A control block of skb passed between the ARPHRD_IEEE802154 device 239 * and other stack parts. 240 */ 241 struct ieee802154_mac_cb { 242 u8 lqi; 243 u8 type; 244 bool ackreq; 245 bool secen; 246 bool secen_override; 247 u8 seclevel; 248 bool seclevel_override; 249 struct ieee802154_addr source; 250 struct ieee802154_addr dest; 251 }; 252 253 static inline struct ieee802154_mac_cb *mac_cb(struct sk_buff *skb) 254 { 255 return (struct ieee802154_mac_cb *)skb->cb; 256 } 257 258 static inline struct ieee802154_mac_cb *mac_cb_init(struct sk_buff *skb) 259 { 260 BUILD_BUG_ON(sizeof(struct ieee802154_mac_cb) > sizeof(skb->cb)); 261 262 memset(skb->cb, 0, sizeof(struct ieee802154_mac_cb)); 263 return mac_cb(skb); 264 } 265 266 enum { 267 IEEE802154_LLSEC_DEVKEY_IGNORE, 268 IEEE802154_LLSEC_DEVKEY_RESTRICT, 269 IEEE802154_LLSEC_DEVKEY_RECORD, 270 271 __IEEE802154_LLSEC_DEVKEY_MAX, 272 }; 273 274 #define IEEE802154_MAC_SCAN_ED 0 275 #define IEEE802154_MAC_SCAN_ACTIVE 1 276 #define IEEE802154_MAC_SCAN_PASSIVE 2 277 #define IEEE802154_MAC_SCAN_ORPHAN 3 278 279 struct ieee802154_mac_params { 280 s8 transmit_power; 281 u8 min_be; 282 u8 max_be; 283 u8 csma_retries; 284 s8 frame_retries; 285 286 bool lbt; 287 struct wpan_phy_cca cca; 288 s32 cca_ed_level; 289 }; 290 291 struct wpan_phy; 292 293 enum { 294 IEEE802154_LLSEC_PARAM_ENABLED = BIT(0), 295 IEEE802154_LLSEC_PARAM_FRAME_COUNTER = BIT(1), 296 IEEE802154_LLSEC_PARAM_OUT_LEVEL = BIT(2), 297 IEEE802154_LLSEC_PARAM_OUT_KEY = BIT(3), 298 IEEE802154_LLSEC_PARAM_KEY_SOURCE = BIT(4), 299 IEEE802154_LLSEC_PARAM_PAN_ID = BIT(5), 300 IEEE802154_LLSEC_PARAM_HWADDR = BIT(6), 301 IEEE802154_LLSEC_PARAM_COORD_HWADDR = BIT(7), 302 IEEE802154_LLSEC_PARAM_COORD_SHORTADDR = BIT(8), 303 }; 304 305 struct ieee802154_llsec_ops { 306 int (*get_params)(struct net_device *dev, 307 struct ieee802154_llsec_params *params); 308 int (*set_params)(struct net_device *dev, 309 const struct ieee802154_llsec_params *params, 310 int changed); 311 312 int (*add_key)(struct net_device *dev, 313 const struct ieee802154_llsec_key_id *id, 314 const struct ieee802154_llsec_key *key); 315 int (*del_key)(struct net_device *dev, 316 const struct ieee802154_llsec_key_id *id); 317 318 int (*add_dev)(struct net_device *dev, 319 const struct ieee802154_llsec_device *llsec_dev); 320 int (*del_dev)(struct net_device *dev, __le64 dev_addr); 321 322 int (*add_devkey)(struct net_device *dev, 323 __le64 device_addr, 324 const struct ieee802154_llsec_device_key *key); 325 int (*del_devkey)(struct net_device *dev, 326 __le64 device_addr, 327 const struct ieee802154_llsec_device_key *key); 328 329 int (*add_seclevel)(struct net_device *dev, 330 const struct ieee802154_llsec_seclevel *sl); 331 int (*del_seclevel)(struct net_device *dev, 332 const struct ieee802154_llsec_seclevel *sl); 333 334 void (*lock_table)(struct net_device *dev); 335 void (*get_table)(struct net_device *dev, 336 struct ieee802154_llsec_table **t); 337 void (*unlock_table)(struct net_device *dev); 338 }; 339 /* 340 * This should be located at net_device->ml_priv 341 * 342 * get_phy should increment the reference counting on returned phy. 343 * Use wpan_wpy_put to put that reference. 344 */ 345 struct ieee802154_mlme_ops { 346 /* The following fields are optional (can be NULL). */ 347 348 int (*assoc_req)(struct net_device *dev, 349 struct ieee802154_addr *addr, 350 u8 channel, u8 page, u8 cap); 351 int (*assoc_resp)(struct net_device *dev, 352 struct ieee802154_addr *addr, 353 __le16 short_addr, u8 status); 354 int (*disassoc_req)(struct net_device *dev, 355 struct ieee802154_addr *addr, 356 u8 reason); 357 int (*start_req)(struct net_device *dev, 358 struct ieee802154_addr *addr, 359 u8 channel, u8 page, u8 bcn_ord, u8 sf_ord, 360 u8 pan_coord, u8 blx, u8 coord_realign); 361 int (*scan_req)(struct net_device *dev, 362 u8 type, u32 channels, u8 page, u8 duration); 363 364 int (*set_mac_params)(struct net_device *dev, 365 const struct ieee802154_mac_params *params); 366 void (*get_mac_params)(struct net_device *dev, 367 struct ieee802154_mac_params *params); 368 369 const struct ieee802154_llsec_ops *llsec; 370 }; 371 372 static inline struct ieee802154_mlme_ops * 373 ieee802154_mlme_ops(const struct net_device *dev) 374 { 375 return dev->ml_priv; 376 } 377 378 #endif 379