linux/include/net/cfg802154.h
<<
>>
Prefs
   1/*
   2 * Copyright (C) 2007, 2008, 2009 Siemens AG
   3 *
   4 * This program is free software; you can redistribute it and/or modify
   5 * it under the terms of the GNU General Public License version 2
   6 * as published by the Free Software Foundation.
   7 *
   8 * This program is distributed in the hope that it will be useful,
   9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11 * GNU General Public License for more details.
  12 *
  13 * Written by:
  14 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
  15 */
  16
  17#ifndef __NET_CFG802154_H
  18#define __NET_CFG802154_H
  19
  20#include <linux/ieee802154.h>
  21#include <linux/netdevice.h>
  22#include <linux/mutex.h>
  23#include <linux/bug.h>
  24
  25#include <net/nl802154.h>
  26
  27struct wpan_phy;
  28struct wpan_phy_cca;
  29
  30#ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  31struct ieee802154_llsec_device_key;
  32struct ieee802154_llsec_seclevel;
  33struct ieee802154_llsec_params;
  34struct ieee802154_llsec_device;
  35struct ieee802154_llsec_table;
  36struct ieee802154_llsec_key_id;
  37struct ieee802154_llsec_key;
  38#endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
  39
  40struct cfg802154_ops {
  41        struct net_device * (*add_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  42                                                           const char *name,
  43                                                           int type);
  44        void    (*del_virtual_intf_deprecated)(struct wpan_phy *wpan_phy,
  45                                               struct net_device *dev);
  46        int     (*suspend)(struct wpan_phy *wpan_phy);
  47        int     (*resume)(struct wpan_phy *wpan_phy);
  48        int     (*add_virtual_intf)(struct wpan_phy *wpan_phy,
  49                                    const char *name,
  50                                    enum nl802154_iftype type,
  51                                    __le64 extended_addr);
  52        int     (*del_virtual_intf)(struct wpan_phy *wpan_phy,
  53                                    struct wpan_dev *wpan_dev);
  54        int     (*set_channel)(struct wpan_phy *wpan_phy, u8 page, u8 channel);
  55        int     (*set_cca_mode)(struct wpan_phy *wpan_phy,
  56                                const struct wpan_phy_cca *cca);
  57        int     (*set_cca_ed_level)(struct wpan_phy *wpan_phy, s32 ed_level);
  58        int     (*set_tx_power)(struct wpan_phy *wpan_phy, s32 power);
  59        int     (*set_pan_id)(struct wpan_phy *wpan_phy,
  60                              struct wpan_dev *wpan_dev, __le16 pan_id);
  61        int     (*set_short_addr)(struct wpan_phy *wpan_phy,
  62                                  struct wpan_dev *wpan_dev, __le16 short_addr);
  63        int     (*set_backoff_exponent)(struct wpan_phy *wpan_phy,
  64                                        struct wpan_dev *wpan_dev, u8 min_be,
  65                                        u8 max_be);
  66        int     (*set_max_csma_backoffs)(struct wpan_phy *wpan_phy,
  67                                         struct wpan_dev *wpan_dev,
  68                                         u8 max_csma_backoffs);
  69        int     (*set_max_frame_retries)(struct wpan_phy *wpan_phy,
  70                                         struct wpan_dev *wpan_dev,
  71                                         s8 max_frame_retries);
  72        int     (*set_lbt_mode)(struct wpan_phy *wpan_phy,
  73                                struct wpan_dev *wpan_dev, bool mode);
  74        int     (*set_ackreq_default)(struct wpan_phy *wpan_phy,
  75                                      struct wpan_dev *wpan_dev, bool ackreq);
  76#ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
  77        void    (*get_llsec_table)(struct wpan_phy *wpan_phy,
  78                                   struct wpan_dev *wpan_dev,
  79                                   struct ieee802154_llsec_table **table);
  80        void    (*lock_llsec_table)(struct wpan_phy *wpan_phy,
  81                                    struct wpan_dev *wpan_dev);
  82        void    (*unlock_llsec_table)(struct wpan_phy *wpan_phy,
  83                                      struct wpan_dev *wpan_dev);
  84        /* TODO remove locking/get table callbacks, this is part of the
  85         * nl802154 interface and should be accessible from ieee802154 layer.
  86         */
  87        int     (*get_llsec_params)(struct wpan_phy *wpan_phy,
  88                                    struct wpan_dev *wpan_dev,
  89                                    struct ieee802154_llsec_params *params);
  90        int     (*set_llsec_params)(struct wpan_phy *wpan_phy,
  91                                    struct wpan_dev *wpan_dev,
  92                                    const struct ieee802154_llsec_params *params,
  93                                    int changed);
  94        int     (*add_llsec_key)(struct wpan_phy *wpan_phy,
  95                                 struct wpan_dev *wpan_dev,
  96                                 const struct ieee802154_llsec_key_id *id,
  97                                 const struct ieee802154_llsec_key *key);
  98        int     (*del_llsec_key)(struct wpan_phy *wpan_phy,
  99                                 struct wpan_dev *wpan_dev,
 100                                 const struct ieee802154_llsec_key_id *id);
 101        int     (*add_seclevel)(struct wpan_phy *wpan_phy,
 102                                 struct wpan_dev *wpan_dev,
 103                                 const struct ieee802154_llsec_seclevel *sl);
 104        int     (*del_seclevel)(struct wpan_phy *wpan_phy,
 105                                 struct wpan_dev *wpan_dev,
 106                                 const struct ieee802154_llsec_seclevel *sl);
 107        int     (*add_device)(struct wpan_phy *wpan_phy,
 108                              struct wpan_dev *wpan_dev,
 109                              const struct ieee802154_llsec_device *dev);
 110        int     (*del_device)(struct wpan_phy *wpan_phy,
 111                              struct wpan_dev *wpan_dev, __le64 extended_addr);
 112        int     (*add_devkey)(struct wpan_phy *wpan_phy,
 113                              struct wpan_dev *wpan_dev,
 114                              __le64 extended_addr,
 115                              const struct ieee802154_llsec_device_key *key);
 116        int     (*del_devkey)(struct wpan_phy *wpan_phy,
 117                              struct wpan_dev *wpan_dev,
 118                              __le64 extended_addr,
 119                              const struct ieee802154_llsec_device_key *key);
 120#endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
 121};
 122
 123static inline bool
 124wpan_phy_supported_bool(bool b, enum nl802154_supported_bool_states st)
 125{
 126        switch (st) {
 127        case NL802154_SUPPORTED_BOOL_TRUE:
 128                return b;
 129        case NL802154_SUPPORTED_BOOL_FALSE:
 130                return !b;
 131        case NL802154_SUPPORTED_BOOL_BOTH:
 132                return true;
 133        default:
 134                WARN_ON(1);
 135        }
 136
 137        return false;
 138}
 139
 140struct wpan_phy_supported {
 141        u32 channels[IEEE802154_MAX_PAGE + 1],
 142            cca_modes, cca_opts, iftypes;
 143        enum nl802154_supported_bool_states lbt;
 144        u8 min_minbe, max_minbe, min_maxbe, max_maxbe,
 145           min_csma_backoffs, max_csma_backoffs;
 146        s8 min_frame_retries, max_frame_retries;
 147        size_t tx_powers_size, cca_ed_levels_size;
 148        const s32 *tx_powers, *cca_ed_levels;
 149};
 150
 151struct wpan_phy_cca {
 152        enum nl802154_cca_modes mode;
 153        enum nl802154_cca_opts opt;
 154};
 155
 156static inline bool
 157wpan_phy_cca_cmp(const struct wpan_phy_cca *a, const struct wpan_phy_cca *b)
 158{
 159        if (a->mode != b->mode)
 160                return false;
 161
 162        if (a->mode == NL802154_CCA_ENERGY_CARRIER)
 163                return a->opt == b->opt;
 164
 165        return true;
 166}
 167
 168/**
 169 * @WPAN_PHY_FLAG_TRANSMIT_POWER: Indicates that transceiver will support
 170 *      transmit power setting.
 171 * @WPAN_PHY_FLAG_CCA_ED_LEVEL: Indicates that transceiver will support cca ed
 172 *      level setting.
 173 * @WPAN_PHY_FLAG_CCA_MODE: Indicates that transceiver will support cca mode
 174 *      setting.
 175 */
 176enum wpan_phy_flags {
 177        WPAN_PHY_FLAG_TXPOWER           = BIT(1),
 178        WPAN_PHY_FLAG_CCA_ED_LEVEL      = BIT(2),
 179        WPAN_PHY_FLAG_CCA_MODE          = BIT(3),
 180};
 181
 182struct wpan_phy {
 183        /* If multiple wpan_phys are registered and you're handed e.g.
 184         * a regular netdev with assigned ieee802154_ptr, you won't
 185         * know whether it points to a wpan_phy your driver has registered
 186         * or not. Assign this to something global to your driver to
 187         * help determine whether you own this wpan_phy or not.
 188         */
 189        const void *privid;
 190
 191        u32 flags;
 192
 193        /*
 194         * This is a PIB according to 802.15.4-2011.
 195         * We do not provide timing-related variables, as they
 196         * aren't used outside of driver
 197         */
 198        u8 current_channel;
 199        u8 current_page;
 200        struct wpan_phy_supported supported;
 201        /* current transmit_power in mBm */
 202        s32 transmit_power;
 203        struct wpan_phy_cca cca;
 204
 205        __le64 perm_extended_addr;
 206
 207        /* current cca ed threshold in mBm */
 208        s32 cca_ed_level;
 209
 210        /* PHY depended MAC PIB values */
 211
 212        /* 802.15.4 acronym: Tdsym in usec */
 213        u8 symbol_duration;
 214        /* lifs and sifs periods timing */
 215        u16 lifs_period;
 216        u16 sifs_period;
 217
 218        struct device dev;
 219
 220        /* the network namespace this phy lives in currently */
 221        possible_net_t _net;
 222
 223        char priv[0] __aligned(NETDEV_ALIGN);
 224};
 225
 226static inline struct net *wpan_phy_net(struct wpan_phy *wpan_phy)
 227{
 228        return read_pnet(&wpan_phy->_net);
 229}
 230
 231static inline void wpan_phy_net_set(struct wpan_phy *wpan_phy, struct net *net)
 232{
 233        write_pnet(&wpan_phy->_net, net);
 234}
 235
 236struct ieee802154_addr {
 237        u8 mode;
 238        __le16 pan_id;
 239        union {
 240                __le16 short_addr;
 241                __le64 extended_addr;
 242        };
 243};
 244
 245struct ieee802154_llsec_key_id {
 246        u8 mode;
 247        u8 id;
 248        union {
 249                struct ieee802154_addr device_addr;
 250                __le32 short_source;
 251                __le64 extended_source;
 252        };
 253};
 254
 255#define IEEE802154_LLSEC_KEY_SIZE 16
 256
 257struct ieee802154_llsec_key {
 258        u8 frame_types;
 259        u32 cmd_frame_ids;
 260        /* TODO replace with NL802154_KEY_SIZE */
 261        u8 key[IEEE802154_LLSEC_KEY_SIZE];
 262};
 263
 264struct ieee802154_llsec_key_entry {
 265        struct list_head list;
 266
 267        struct ieee802154_llsec_key_id id;
 268        struct ieee802154_llsec_key *key;
 269};
 270
 271struct ieee802154_llsec_params {
 272        bool enabled;
 273
 274        __be32 frame_counter;
 275        u8 out_level;
 276        struct ieee802154_llsec_key_id out_key;
 277
 278        __le64 default_key_source;
 279
 280        __le16 pan_id;
 281        __le64 hwaddr;
 282        __le64 coord_hwaddr;
 283        __le16 coord_shortaddr;
 284};
 285
 286struct ieee802154_llsec_table {
 287        struct list_head keys;
 288        struct list_head devices;
 289        struct list_head security_levels;
 290};
 291
 292struct ieee802154_llsec_seclevel {
 293        struct list_head list;
 294
 295        u8 frame_type;
 296        u8 cmd_frame_id;
 297        bool device_override;
 298        u32 sec_levels;
 299};
 300
 301struct ieee802154_llsec_device {
 302        struct list_head list;
 303
 304        __le16 pan_id;
 305        __le16 short_addr;
 306        __le64 hwaddr;
 307        u32 frame_counter;
 308        bool seclevel_exempt;
 309
 310        u8 key_mode;
 311        struct list_head keys;
 312};
 313
 314struct ieee802154_llsec_device_key {
 315        struct list_head list;
 316
 317        struct ieee802154_llsec_key_id key_id;
 318        u32 frame_counter;
 319};
 320
 321struct wpan_dev_header_ops {
 322        /* TODO create callback currently assumes ieee802154_mac_cb inside
 323         * skb->cb. This should be changed to give these information as
 324         * parameter.
 325         */
 326        int     (*create)(struct sk_buff *skb, struct net_device *dev,
 327                          const struct ieee802154_addr *daddr,
 328                          const struct ieee802154_addr *saddr,
 329                          unsigned int len);
 330};
 331
 332struct wpan_dev {
 333        struct wpan_phy *wpan_phy;
 334        int iftype;
 335
 336        /* the remainder of this struct should be private to cfg802154 */
 337        struct list_head list;
 338        struct net_device *netdev;
 339
 340        const struct wpan_dev_header_ops *header_ops;
 341
 342        /* lowpan interface, set when the wpan_dev belongs to one lowpan_dev */
 343        struct net_device *lowpan_dev;
 344
 345        u32 identifier;
 346
 347        /* MAC PIB */
 348        __le16 pan_id;
 349        __le16 short_addr;
 350        __le64 extended_addr;
 351
 352        /* MAC BSN field */
 353        atomic_t bsn;
 354        /* MAC DSN field */
 355        atomic_t dsn;
 356
 357        u8 min_be;
 358        u8 max_be;
 359        u8 csma_retries;
 360        s8 frame_retries;
 361
 362        bool lbt;
 363
 364        bool promiscuous_mode;
 365
 366        /* fallback for acknowledgment bit setting */
 367        bool ackreq;
 368};
 369
 370#define to_phy(_dev)    container_of(_dev, struct wpan_phy, dev)
 371
 372static inline int
 373wpan_dev_hard_header(struct sk_buff *skb, struct net_device *dev,
 374                     const struct ieee802154_addr *daddr,
 375                     const struct ieee802154_addr *saddr,
 376                     unsigned int len)
 377{
 378        struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
 379
 380        return wpan_dev->header_ops->create(skb, dev, daddr, saddr, len);
 381}
 382
 383struct wpan_phy *
 384wpan_phy_new(const struct cfg802154_ops *ops, size_t priv_size);
 385static inline void wpan_phy_set_dev(struct wpan_phy *phy, struct device *dev)
 386{
 387        phy->dev.parent = dev;
 388}
 389
 390int wpan_phy_register(struct wpan_phy *phy);
 391void wpan_phy_unregister(struct wpan_phy *phy);
 392void wpan_phy_free(struct wpan_phy *phy);
 393/* Same semantics as for class_for_each_device */
 394int wpan_phy_for_each(int (*fn)(struct wpan_phy *phy, void *data), void *data);
 395
 396static inline void *wpan_phy_priv(struct wpan_phy *phy)
 397{
 398        BUG_ON(!phy);
 399        return &phy->priv;
 400}
 401
 402struct wpan_phy *wpan_phy_find(const char *str);
 403
 404static inline void wpan_phy_put(struct wpan_phy *phy)
 405{
 406        put_device(&phy->dev);
 407}
 408
 409static inline const char *wpan_phy_name(struct wpan_phy *phy)
 410{
 411        return dev_name(&phy->dev);
 412}
 413
 414#endif /* __NET_CFG802154_H */
 415