root/net/ieee802154/nl802154.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. __cfg802154_wpan_dev_from_attrs
  2. __cfg802154_rdev_from_attrs
  3. cfg802154_get_dev_from_info
  4. nl802154_prepare_wpan_dev_dump
  5. nl802154_finish_wpan_dev_dump
  6. nl802154hdr_put
  7. nl802154_put_flags
  8. nl802154_send_wpan_phy_channels
  9. nl802154_put_capabilities
  10. nl802154_send_wpan_phy
  11. nl802154_dump_wpan_phy_parse
  12. nl802154_dump_wpan_phy
  13. nl802154_dump_wpan_phy_done
  14. nl802154_get_wpan_phy
  15. wpan_dev_id
  16. ieee802154_llsec_send_key_id
  17. nl802154_get_llsec_params
  18. nl802154_send_iface
  19. nl802154_dump_interface
  20. nl802154_get_interface
  21. nl802154_new_interface
  22. nl802154_del_interface
  23. nl802154_set_channel
  24. nl802154_set_cca_mode
  25. nl802154_set_cca_ed_level
  26. nl802154_set_tx_power
  27. nl802154_set_pan_id
  28. nl802154_set_short_addr
  29. nl802154_set_backoff_exponent
  30. nl802154_set_max_csma_backoffs
  31. nl802154_set_max_frame_retries
  32. nl802154_set_lbt_mode
  33. nl802154_set_ackreq_default
  34. nl802154_wpan_phy_netns
  35. ieee802154_llsec_parse_dev_addr
  36. ieee802154_llsec_parse_key_id
  37. nl802154_set_llsec_params
  38. nl802154_send_key
  39. nl802154_dump_llsec_key
  40. nl802154_add_llsec_key
  41. nl802154_del_llsec_key
  42. nl802154_send_device
  43. nl802154_dump_llsec_dev
  44. ieee802154_llsec_parse_device
  45. nl802154_add_llsec_dev
  46. nl802154_del_llsec_dev
  47. nl802154_send_devkey
  48. nl802154_dump_llsec_devkey
  49. nl802154_add_llsec_devkey
  50. nl802154_del_llsec_devkey
  51. nl802154_send_seclevel
  52. nl802154_dump_llsec_seclevel
  53. llsec_parse_seclevel
  54. nl802154_add_llsec_seclevel
  55. nl802154_del_llsec_seclevel
  56. nl802154_pre_doit
  57. nl802154_post_doit
  58. nl802154_init
  59. nl802154_exit

   1 // SPDX-License-Identifier: GPL-2.0-only
   2 /*
   3  *
   4  * Authors:
   5  * Alexander Aring <aar@pengutronix.de>
   6  *
   7  * Based on: net/wireless/nl80211.c
   8  */
   9 
  10 #include <linux/rtnetlink.h>
  11 
  12 #include <net/cfg802154.h>
  13 #include <net/genetlink.h>
  14 #include <net/mac802154.h>
  15 #include <net/netlink.h>
  16 #include <net/nl802154.h>
  17 #include <net/sock.h>
  18 
  19 #include "nl802154.h"
  20 #include "rdev-ops.h"
  21 #include "core.h"
  22 
  23 /* the netlink family */
  24 static struct genl_family nl802154_fam;
  25 
  26 /* multicast groups */
  27 enum nl802154_multicast_groups {
  28         NL802154_MCGRP_CONFIG,
  29 };
  30 
  31 static const struct genl_multicast_group nl802154_mcgrps[] = {
  32         [NL802154_MCGRP_CONFIG] = { .name = "config", },
  33 };
  34 
  35 /* returns ERR_PTR values */
  36 static struct wpan_dev *
  37 __cfg802154_wpan_dev_from_attrs(struct net *netns, struct nlattr **attrs)
  38 {
  39         struct cfg802154_registered_device *rdev;
  40         struct wpan_dev *result = NULL;
  41         bool have_ifidx = attrs[NL802154_ATTR_IFINDEX];
  42         bool have_wpan_dev_id = attrs[NL802154_ATTR_WPAN_DEV];
  43         u64 wpan_dev_id;
  44         int wpan_phy_idx = -1;
  45         int ifidx = -1;
  46 
  47         ASSERT_RTNL();
  48 
  49         if (!have_ifidx && !have_wpan_dev_id)
  50                 return ERR_PTR(-EINVAL);
  51 
  52         if (have_ifidx)
  53                 ifidx = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
  54         if (have_wpan_dev_id) {
  55                 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
  56                 wpan_phy_idx = wpan_dev_id >> 32;
  57         }
  58 
  59         list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
  60                 struct wpan_dev *wpan_dev;
  61 
  62                 if (wpan_phy_net(&rdev->wpan_phy) != netns)
  63                         continue;
  64 
  65                 if (have_wpan_dev_id && rdev->wpan_phy_idx != wpan_phy_idx)
  66                         continue;
  67 
  68                 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
  69                         if (have_ifidx && wpan_dev->netdev &&
  70                             wpan_dev->netdev->ifindex == ifidx) {
  71                                 result = wpan_dev;
  72                                 break;
  73                         }
  74                         if (have_wpan_dev_id &&
  75                             wpan_dev->identifier == (u32)wpan_dev_id) {
  76                                 result = wpan_dev;
  77                                 break;
  78                         }
  79                 }
  80 
  81                 if (result)
  82                         break;
  83         }
  84 
  85         if (result)
  86                 return result;
  87 
  88         return ERR_PTR(-ENODEV);
  89 }
  90 
  91 static struct cfg802154_registered_device *
  92 __cfg802154_rdev_from_attrs(struct net *netns, struct nlattr **attrs)
  93 {
  94         struct cfg802154_registered_device *rdev = NULL, *tmp;
  95         struct net_device *netdev;
  96 
  97         ASSERT_RTNL();
  98 
  99         if (!attrs[NL802154_ATTR_WPAN_PHY] &&
 100             !attrs[NL802154_ATTR_IFINDEX] &&
 101             !attrs[NL802154_ATTR_WPAN_DEV])
 102                 return ERR_PTR(-EINVAL);
 103 
 104         if (attrs[NL802154_ATTR_WPAN_PHY])
 105                 rdev = cfg802154_rdev_by_wpan_phy_idx(
 106                                 nla_get_u32(attrs[NL802154_ATTR_WPAN_PHY]));
 107 
 108         if (attrs[NL802154_ATTR_WPAN_DEV]) {
 109                 u64 wpan_dev_id = nla_get_u64(attrs[NL802154_ATTR_WPAN_DEV]);
 110                 struct wpan_dev *wpan_dev;
 111                 bool found = false;
 112 
 113                 tmp = cfg802154_rdev_by_wpan_phy_idx(wpan_dev_id >> 32);
 114                 if (tmp) {
 115                         /* make sure wpan_dev exists */
 116                         list_for_each_entry(wpan_dev, &tmp->wpan_dev_list, list) {
 117                                 if (wpan_dev->identifier != (u32)wpan_dev_id)
 118                                         continue;
 119                                 found = true;
 120                                 break;
 121                         }
 122 
 123                         if (!found)
 124                                 tmp = NULL;
 125 
 126                         if (rdev && tmp != rdev)
 127                                 return ERR_PTR(-EINVAL);
 128                         rdev = tmp;
 129                 }
 130         }
 131 
 132         if (attrs[NL802154_ATTR_IFINDEX]) {
 133                 int ifindex = nla_get_u32(attrs[NL802154_ATTR_IFINDEX]);
 134 
 135                 netdev = __dev_get_by_index(netns, ifindex);
 136                 if (netdev) {
 137                         if (netdev->ieee802154_ptr)
 138                                 tmp = wpan_phy_to_rdev(
 139                                                 netdev->ieee802154_ptr->wpan_phy);
 140                         else
 141                                 tmp = NULL;
 142 
 143                         /* not wireless device -- return error */
 144                         if (!tmp)
 145                                 return ERR_PTR(-EINVAL);
 146 
 147                         /* mismatch -- return error */
 148                         if (rdev && tmp != rdev)
 149                                 return ERR_PTR(-EINVAL);
 150 
 151                         rdev = tmp;
 152                 }
 153         }
 154 
 155         if (!rdev)
 156                 return ERR_PTR(-ENODEV);
 157 
 158         if (netns != wpan_phy_net(&rdev->wpan_phy))
 159                 return ERR_PTR(-ENODEV);
 160 
 161         return rdev;
 162 }
 163 
 164 /* This function returns a pointer to the driver
 165  * that the genl_info item that is passed refers to.
 166  *
 167  * The result of this can be a PTR_ERR and hence must
 168  * be checked with IS_ERR() for errors.
 169  */
 170 static struct cfg802154_registered_device *
 171 cfg802154_get_dev_from_info(struct net *netns, struct genl_info *info)
 172 {
 173         return __cfg802154_rdev_from_attrs(netns, info->attrs);
 174 }
 175 
 176 /* policy for the attributes */
 177 static const struct nla_policy nl802154_policy[NL802154_ATTR_MAX+1] = {
 178         [NL802154_ATTR_WPAN_PHY] = { .type = NLA_U32 },
 179         [NL802154_ATTR_WPAN_PHY_NAME] = { .type = NLA_NUL_STRING,
 180                                           .len = 20-1 },
 181 
 182         [NL802154_ATTR_IFINDEX] = { .type = NLA_U32 },
 183         [NL802154_ATTR_IFTYPE] = { .type = NLA_U32 },
 184         [NL802154_ATTR_IFNAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ-1 },
 185 
 186         [NL802154_ATTR_WPAN_DEV] = { .type = NLA_U64 },
 187 
 188         [NL802154_ATTR_PAGE] = { .type = NLA_U8, },
 189         [NL802154_ATTR_CHANNEL] = { .type = NLA_U8, },
 190 
 191         [NL802154_ATTR_TX_POWER] = { .type = NLA_S32, },
 192 
 193         [NL802154_ATTR_CCA_MODE] = { .type = NLA_U32, },
 194         [NL802154_ATTR_CCA_OPT] = { .type = NLA_U32, },
 195         [NL802154_ATTR_CCA_ED_LEVEL] = { .type = NLA_S32, },
 196 
 197         [NL802154_ATTR_SUPPORTED_CHANNEL] = { .type = NLA_U32, },
 198 
 199         [NL802154_ATTR_PAN_ID] = { .type = NLA_U16, },
 200         [NL802154_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
 201         [NL802154_ATTR_SHORT_ADDR] = { .type = NLA_U16, },
 202 
 203         [NL802154_ATTR_MIN_BE] = { .type = NLA_U8, },
 204         [NL802154_ATTR_MAX_BE] = { .type = NLA_U8, },
 205         [NL802154_ATTR_MAX_CSMA_BACKOFFS] = { .type = NLA_U8, },
 206 
 207         [NL802154_ATTR_MAX_FRAME_RETRIES] = { .type = NLA_S8, },
 208 
 209         [NL802154_ATTR_LBT_MODE] = { .type = NLA_U8, },
 210 
 211         [NL802154_ATTR_WPAN_PHY_CAPS] = { .type = NLA_NESTED },
 212 
 213         [NL802154_ATTR_SUPPORTED_COMMANDS] = { .type = NLA_NESTED },
 214 
 215         [NL802154_ATTR_ACKREQ_DEFAULT] = { .type = NLA_U8 },
 216 
 217         [NL802154_ATTR_PID] = { .type = NLA_U32 },
 218         [NL802154_ATTR_NETNS_FD] = { .type = NLA_U32 },
 219 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 220         [NL802154_ATTR_SEC_ENABLED] = { .type = NLA_U8, },
 221         [NL802154_ATTR_SEC_OUT_LEVEL] = { .type = NLA_U32, },
 222         [NL802154_ATTR_SEC_OUT_KEY_ID] = { .type = NLA_NESTED, },
 223         [NL802154_ATTR_SEC_FRAME_COUNTER] = { .type = NLA_U32 },
 224 
 225         [NL802154_ATTR_SEC_LEVEL] = { .type = NLA_NESTED },
 226         [NL802154_ATTR_SEC_DEVICE] = { .type = NLA_NESTED },
 227         [NL802154_ATTR_SEC_DEVKEY] = { .type = NLA_NESTED },
 228         [NL802154_ATTR_SEC_KEY] = { .type = NLA_NESTED },
 229 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
 230 };
 231 
 232 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 233 static int
 234 nl802154_prepare_wpan_dev_dump(struct sk_buff *skb,
 235                                struct netlink_callback *cb,
 236                                struct cfg802154_registered_device **rdev,
 237                                struct wpan_dev **wpan_dev)
 238 {
 239         int err;
 240 
 241         rtnl_lock();
 242 
 243         if (!cb->args[0]) {
 244                 err = nlmsg_parse_deprecated(cb->nlh,
 245                                              GENL_HDRLEN + nl802154_fam.hdrsize,
 246                                              genl_family_attrbuf(&nl802154_fam),
 247                                              nl802154_fam.maxattr,
 248                                              nl802154_policy, NULL);
 249                 if (err)
 250                         goto out_unlock;
 251 
 252                 *wpan_dev = __cfg802154_wpan_dev_from_attrs(sock_net(skb->sk),
 253                                                             genl_family_attrbuf(&nl802154_fam));
 254                 if (IS_ERR(*wpan_dev)) {
 255                         err = PTR_ERR(*wpan_dev);
 256                         goto out_unlock;
 257                 }
 258                 *rdev = wpan_phy_to_rdev((*wpan_dev)->wpan_phy);
 259                 /* 0 is the first index - add 1 to parse only once */
 260                 cb->args[0] = (*rdev)->wpan_phy_idx + 1;
 261                 cb->args[1] = (*wpan_dev)->identifier;
 262         } else {
 263                 /* subtract the 1 again here */
 264                 struct wpan_phy *wpan_phy = wpan_phy_idx_to_wpan_phy(cb->args[0] - 1);
 265                 struct wpan_dev *tmp;
 266 
 267                 if (!wpan_phy) {
 268                         err = -ENODEV;
 269                         goto out_unlock;
 270                 }
 271                 *rdev = wpan_phy_to_rdev(wpan_phy);
 272                 *wpan_dev = NULL;
 273 
 274                 list_for_each_entry(tmp, &(*rdev)->wpan_dev_list, list) {
 275                         if (tmp->identifier == cb->args[1]) {
 276                                 *wpan_dev = tmp;
 277                                 break;
 278                         }
 279                 }
 280 
 281                 if (!*wpan_dev) {
 282                         err = -ENODEV;
 283                         goto out_unlock;
 284                 }
 285         }
 286 
 287         return 0;
 288  out_unlock:
 289         rtnl_unlock();
 290         return err;
 291 }
 292 
 293 static void
 294 nl802154_finish_wpan_dev_dump(struct cfg802154_registered_device *rdev)
 295 {
 296         rtnl_unlock();
 297 }
 298 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
 299 
 300 /* message building helper */
 301 static inline void *nl802154hdr_put(struct sk_buff *skb, u32 portid, u32 seq,
 302                                     int flags, u8 cmd)
 303 {
 304         /* since there is no private header just add the generic one */
 305         return genlmsg_put(skb, portid, seq, &nl802154_fam, flags, cmd);
 306 }
 307 
 308 static int
 309 nl802154_put_flags(struct sk_buff *msg, int attr, u32 mask)
 310 {
 311         struct nlattr *nl_flags = nla_nest_start_noflag(msg, attr);
 312         int i;
 313 
 314         if (!nl_flags)
 315                 return -ENOBUFS;
 316 
 317         i = 0;
 318         while (mask) {
 319                 if ((mask & 1) && nla_put_flag(msg, i))
 320                         return -ENOBUFS;
 321 
 322                 mask >>= 1;
 323                 i++;
 324         }
 325 
 326         nla_nest_end(msg, nl_flags);
 327         return 0;
 328 }
 329 
 330 static int
 331 nl802154_send_wpan_phy_channels(struct cfg802154_registered_device *rdev,
 332                                 struct sk_buff *msg)
 333 {
 334         struct nlattr *nl_page;
 335         unsigned long page;
 336 
 337         nl_page = nla_nest_start_noflag(msg, NL802154_ATTR_CHANNELS_SUPPORTED);
 338         if (!nl_page)
 339                 return -ENOBUFS;
 340 
 341         for (page = 0; page <= IEEE802154_MAX_PAGE; page++) {
 342                 if (nla_put_u32(msg, NL802154_ATTR_SUPPORTED_CHANNEL,
 343                                 rdev->wpan_phy.supported.channels[page]))
 344                         return -ENOBUFS;
 345         }
 346         nla_nest_end(msg, nl_page);
 347 
 348         return 0;
 349 }
 350 
 351 static int
 352 nl802154_put_capabilities(struct sk_buff *msg,
 353                           struct cfg802154_registered_device *rdev)
 354 {
 355         const struct wpan_phy_supported *caps = &rdev->wpan_phy.supported;
 356         struct nlattr *nl_caps, *nl_channels;
 357         int i;
 358 
 359         nl_caps = nla_nest_start_noflag(msg, NL802154_ATTR_WPAN_PHY_CAPS);
 360         if (!nl_caps)
 361                 return -ENOBUFS;
 362 
 363         nl_channels = nla_nest_start_noflag(msg, NL802154_CAP_ATTR_CHANNELS);
 364         if (!nl_channels)
 365                 return -ENOBUFS;
 366 
 367         for (i = 0; i <= IEEE802154_MAX_PAGE; i++) {
 368                 if (caps->channels[i]) {
 369                         if (nl802154_put_flags(msg, i, caps->channels[i]))
 370                                 return -ENOBUFS;
 371                 }
 372         }
 373 
 374         nla_nest_end(msg, nl_channels);
 375 
 376         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
 377                 struct nlattr *nl_ed_lvls;
 378 
 379                 nl_ed_lvls = nla_nest_start_noflag(msg,
 380                                                    NL802154_CAP_ATTR_CCA_ED_LEVELS);
 381                 if (!nl_ed_lvls)
 382                         return -ENOBUFS;
 383 
 384                 for (i = 0; i < caps->cca_ed_levels_size; i++) {
 385                         if (nla_put_s32(msg, i, caps->cca_ed_levels[i]))
 386                                 return -ENOBUFS;
 387                 }
 388 
 389                 nla_nest_end(msg, nl_ed_lvls);
 390         }
 391 
 392         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
 393                 struct nlattr *nl_tx_pwrs;
 394 
 395                 nl_tx_pwrs = nla_nest_start_noflag(msg,
 396                                                    NL802154_CAP_ATTR_TX_POWERS);
 397                 if (!nl_tx_pwrs)
 398                         return -ENOBUFS;
 399 
 400                 for (i = 0; i < caps->tx_powers_size; i++) {
 401                         if (nla_put_s32(msg, i, caps->tx_powers[i]))
 402                                 return -ENOBUFS;
 403                 }
 404 
 405                 nla_nest_end(msg, nl_tx_pwrs);
 406         }
 407 
 408         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
 409                 if (nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_MODES,
 410                                        caps->cca_modes) ||
 411                     nl802154_put_flags(msg, NL802154_CAP_ATTR_CCA_OPTS,
 412                                        caps->cca_opts))
 413                         return -ENOBUFS;
 414         }
 415 
 416         if (nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MINBE, caps->min_minbe) ||
 417             nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MINBE, caps->max_minbe) ||
 418             nla_put_u8(msg, NL802154_CAP_ATTR_MIN_MAXBE, caps->min_maxbe) ||
 419             nla_put_u8(msg, NL802154_CAP_ATTR_MAX_MAXBE, caps->max_maxbe) ||
 420             nla_put_u8(msg, NL802154_CAP_ATTR_MIN_CSMA_BACKOFFS,
 421                        caps->min_csma_backoffs) ||
 422             nla_put_u8(msg, NL802154_CAP_ATTR_MAX_CSMA_BACKOFFS,
 423                        caps->max_csma_backoffs) ||
 424             nla_put_s8(msg, NL802154_CAP_ATTR_MIN_FRAME_RETRIES,
 425                        caps->min_frame_retries) ||
 426             nla_put_s8(msg, NL802154_CAP_ATTR_MAX_FRAME_RETRIES,
 427                        caps->max_frame_retries) ||
 428             nl802154_put_flags(msg, NL802154_CAP_ATTR_IFTYPES,
 429                                caps->iftypes) ||
 430             nla_put_u32(msg, NL802154_CAP_ATTR_LBT, caps->lbt))
 431                 return -ENOBUFS;
 432 
 433         nla_nest_end(msg, nl_caps);
 434 
 435         return 0;
 436 }
 437 
 438 static int nl802154_send_wpan_phy(struct cfg802154_registered_device *rdev,
 439                                   enum nl802154_commands cmd,
 440                                   struct sk_buff *msg, u32 portid, u32 seq,
 441                                   int flags)
 442 {
 443         struct nlattr *nl_cmds;
 444         void *hdr;
 445         int i;
 446 
 447         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
 448         if (!hdr)
 449                 return -ENOBUFS;
 450 
 451         if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
 452             nla_put_string(msg, NL802154_ATTR_WPAN_PHY_NAME,
 453                            wpan_phy_name(&rdev->wpan_phy)) ||
 454             nla_put_u32(msg, NL802154_ATTR_GENERATION,
 455                         cfg802154_rdev_list_generation))
 456                 goto nla_put_failure;
 457 
 458         if (cmd != NL802154_CMD_NEW_WPAN_PHY)
 459                 goto finish;
 460 
 461         /* DUMP PHY PIB */
 462 
 463         /* current channel settings */
 464         if (nla_put_u8(msg, NL802154_ATTR_PAGE,
 465                        rdev->wpan_phy.current_page) ||
 466             nla_put_u8(msg, NL802154_ATTR_CHANNEL,
 467                        rdev->wpan_phy.current_channel))
 468                 goto nla_put_failure;
 469 
 470         /* TODO remove this behaviour, we still keep support it for a while
 471          * so users can change the behaviour to the new one.
 472          */
 473         if (nl802154_send_wpan_phy_channels(rdev, msg))
 474                 goto nla_put_failure;
 475 
 476         /* cca mode */
 477         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE) {
 478                 if (nla_put_u32(msg, NL802154_ATTR_CCA_MODE,
 479                                 rdev->wpan_phy.cca.mode))
 480                         goto nla_put_failure;
 481 
 482                 if (rdev->wpan_phy.cca.mode == NL802154_CCA_ENERGY_CARRIER) {
 483                         if (nla_put_u32(msg, NL802154_ATTR_CCA_OPT,
 484                                         rdev->wpan_phy.cca.opt))
 485                                 goto nla_put_failure;
 486                 }
 487         }
 488 
 489         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER) {
 490                 if (nla_put_s32(msg, NL802154_ATTR_TX_POWER,
 491                                 rdev->wpan_phy.transmit_power))
 492                         goto nla_put_failure;
 493         }
 494 
 495         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL) {
 496                 if (nla_put_s32(msg, NL802154_ATTR_CCA_ED_LEVEL,
 497                                 rdev->wpan_phy.cca_ed_level))
 498                         goto nla_put_failure;
 499         }
 500 
 501         if (nl802154_put_capabilities(msg, rdev))
 502                 goto nla_put_failure;
 503 
 504         nl_cmds = nla_nest_start_noflag(msg, NL802154_ATTR_SUPPORTED_COMMANDS);
 505         if (!nl_cmds)
 506                 goto nla_put_failure;
 507 
 508         i = 0;
 509 #define CMD(op, n)                                                      \
 510         do {                                                            \
 511                 if (rdev->ops->op) {                                    \
 512                         i++;                                            \
 513                         if (nla_put_u32(msg, i, NL802154_CMD_ ## n))    \
 514                                 goto nla_put_failure;                   \
 515                 }                                                       \
 516         } while (0)
 517 
 518         CMD(add_virtual_intf, NEW_INTERFACE);
 519         CMD(del_virtual_intf, DEL_INTERFACE);
 520         CMD(set_channel, SET_CHANNEL);
 521         CMD(set_pan_id, SET_PAN_ID);
 522         CMD(set_short_addr, SET_SHORT_ADDR);
 523         CMD(set_backoff_exponent, SET_BACKOFF_EXPONENT);
 524         CMD(set_max_csma_backoffs, SET_MAX_CSMA_BACKOFFS);
 525         CMD(set_max_frame_retries, SET_MAX_FRAME_RETRIES);
 526         CMD(set_lbt_mode, SET_LBT_MODE);
 527         CMD(set_ackreq_default, SET_ACKREQ_DEFAULT);
 528 
 529         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER)
 530                 CMD(set_tx_power, SET_TX_POWER);
 531 
 532         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL)
 533                 CMD(set_cca_ed_level, SET_CCA_ED_LEVEL);
 534 
 535         if (rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE)
 536                 CMD(set_cca_mode, SET_CCA_MODE);
 537 
 538 #undef CMD
 539         nla_nest_end(msg, nl_cmds);
 540 
 541 finish:
 542         genlmsg_end(msg, hdr);
 543         return 0;
 544 
 545 nla_put_failure:
 546         genlmsg_cancel(msg, hdr);
 547         return -EMSGSIZE;
 548 }
 549 
 550 struct nl802154_dump_wpan_phy_state {
 551         s64 filter_wpan_phy;
 552         long start;
 553 
 554 };
 555 
 556 static int nl802154_dump_wpan_phy_parse(struct sk_buff *skb,
 557                                         struct netlink_callback *cb,
 558                                         struct nl802154_dump_wpan_phy_state *state)
 559 {
 560         struct nlattr **tb = genl_family_attrbuf(&nl802154_fam);
 561         int ret = nlmsg_parse_deprecated(cb->nlh,
 562                                          GENL_HDRLEN + nl802154_fam.hdrsize,
 563                                          tb, nl802154_fam.maxattr,
 564                                          nl802154_policy, NULL);
 565 
 566         /* TODO check if we can handle error here,
 567          * we have no backward compatibility
 568          */
 569         if (ret)
 570                 return 0;
 571 
 572         if (tb[NL802154_ATTR_WPAN_PHY])
 573                 state->filter_wpan_phy = nla_get_u32(tb[NL802154_ATTR_WPAN_PHY]);
 574         if (tb[NL802154_ATTR_WPAN_DEV])
 575                 state->filter_wpan_phy = nla_get_u64(tb[NL802154_ATTR_WPAN_DEV]) >> 32;
 576         if (tb[NL802154_ATTR_IFINDEX]) {
 577                 struct net_device *netdev;
 578                 struct cfg802154_registered_device *rdev;
 579                 int ifidx = nla_get_u32(tb[NL802154_ATTR_IFINDEX]);
 580 
 581                 netdev = __dev_get_by_index(&init_net, ifidx);
 582                 if (!netdev)
 583                         return -ENODEV;
 584                 if (netdev->ieee802154_ptr) {
 585                         rdev = wpan_phy_to_rdev(
 586                                         netdev->ieee802154_ptr->wpan_phy);
 587                         state->filter_wpan_phy = rdev->wpan_phy_idx;
 588                 }
 589         }
 590 
 591         return 0;
 592 }
 593 
 594 static int
 595 nl802154_dump_wpan_phy(struct sk_buff *skb, struct netlink_callback *cb)
 596 {
 597         int idx = 0, ret;
 598         struct nl802154_dump_wpan_phy_state *state = (void *)cb->args[0];
 599         struct cfg802154_registered_device *rdev;
 600 
 601         rtnl_lock();
 602         if (!state) {
 603                 state = kzalloc(sizeof(*state), GFP_KERNEL);
 604                 if (!state) {
 605                         rtnl_unlock();
 606                         return -ENOMEM;
 607                 }
 608                 state->filter_wpan_phy = -1;
 609                 ret = nl802154_dump_wpan_phy_parse(skb, cb, state);
 610                 if (ret) {
 611                         kfree(state);
 612                         rtnl_unlock();
 613                         return ret;
 614                 }
 615                 cb->args[0] = (long)state;
 616         }
 617 
 618         list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
 619                 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
 620                         continue;
 621                 if (++idx <= state->start)
 622                         continue;
 623                 if (state->filter_wpan_phy != -1 &&
 624                     state->filter_wpan_phy != rdev->wpan_phy_idx)
 625                         continue;
 626                 /* attempt to fit multiple wpan_phy data chunks into the skb */
 627                 ret = nl802154_send_wpan_phy(rdev,
 628                                              NL802154_CMD_NEW_WPAN_PHY,
 629                                              skb,
 630                                              NETLINK_CB(cb->skb).portid,
 631                                              cb->nlh->nlmsg_seq, NLM_F_MULTI);
 632                 if (ret < 0) {
 633                         if ((ret == -ENOBUFS || ret == -EMSGSIZE) &&
 634                             !skb->len && cb->min_dump_alloc < 4096) {
 635                                 cb->min_dump_alloc = 4096;
 636                                 rtnl_unlock();
 637                                 return 1;
 638                         }
 639                         idx--;
 640                         break;
 641                 }
 642                 break;
 643         }
 644         rtnl_unlock();
 645 
 646         state->start = idx;
 647 
 648         return skb->len;
 649 }
 650 
 651 static int nl802154_dump_wpan_phy_done(struct netlink_callback *cb)
 652 {
 653         kfree((void *)cb->args[0]);
 654         return 0;
 655 }
 656 
 657 static int nl802154_get_wpan_phy(struct sk_buff *skb, struct genl_info *info)
 658 {
 659         struct sk_buff *msg;
 660         struct cfg802154_registered_device *rdev = info->user_ptr[0];
 661 
 662         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 663         if (!msg)
 664                 return -ENOMEM;
 665 
 666         if (nl802154_send_wpan_phy(rdev, NL802154_CMD_NEW_WPAN_PHY, msg,
 667                                    info->snd_portid, info->snd_seq, 0) < 0) {
 668                 nlmsg_free(msg);
 669                 return -ENOBUFS;
 670         }
 671 
 672         return genlmsg_reply(msg, info);
 673 }
 674 
 675 static inline u64 wpan_dev_id(struct wpan_dev *wpan_dev)
 676 {
 677         return (u64)wpan_dev->identifier |
 678                ((u64)wpan_phy_to_rdev(wpan_dev->wpan_phy)->wpan_phy_idx << 32);
 679 }
 680 
 681 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 682 #include <net/ieee802154_netdev.h>
 683 
 684 static int
 685 ieee802154_llsec_send_key_id(struct sk_buff *msg,
 686                              const struct ieee802154_llsec_key_id *desc)
 687 {
 688         struct nlattr *nl_dev_addr;
 689 
 690         if (nla_put_u32(msg, NL802154_KEY_ID_ATTR_MODE, desc->mode))
 691                 return -ENOBUFS;
 692 
 693         switch (desc->mode) {
 694         case NL802154_KEY_ID_MODE_IMPLICIT:
 695                 nl_dev_addr = nla_nest_start_noflag(msg,
 696                                                     NL802154_KEY_ID_ATTR_IMPLICIT);
 697                 if (!nl_dev_addr)
 698                         return -ENOBUFS;
 699 
 700                 if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_PAN_ID,
 701                                  desc->device_addr.pan_id) ||
 702                     nla_put_u32(msg,  NL802154_DEV_ADDR_ATTR_MODE,
 703                                 desc->device_addr.mode))
 704                         return -ENOBUFS;
 705 
 706                 switch (desc->device_addr.mode) {
 707                 case NL802154_DEV_ADDR_SHORT:
 708                         if (nla_put_le16(msg, NL802154_DEV_ADDR_ATTR_SHORT,
 709                                          desc->device_addr.short_addr))
 710                                 return -ENOBUFS;
 711                         break;
 712                 case NL802154_DEV_ADDR_EXTENDED:
 713                         if (nla_put_le64(msg, NL802154_DEV_ADDR_ATTR_EXTENDED,
 714                                          desc->device_addr.extended_addr,
 715                                          NL802154_DEV_ADDR_ATTR_PAD))
 716                                 return -ENOBUFS;
 717                         break;
 718                 default:
 719                         /* userspace should handle unknown */
 720                         break;
 721                 }
 722 
 723                 nla_nest_end(msg, nl_dev_addr);
 724                 break;
 725         case NL802154_KEY_ID_MODE_INDEX:
 726                 break;
 727         case NL802154_KEY_ID_MODE_INDEX_SHORT:
 728                 /* TODO renmae short_source? */
 729                 if (nla_put_le32(msg, NL802154_KEY_ID_ATTR_SOURCE_SHORT,
 730                                  desc->short_source))
 731                         return -ENOBUFS;
 732                 break;
 733         case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
 734                 if (nla_put_le64(msg, NL802154_KEY_ID_ATTR_SOURCE_EXTENDED,
 735                                  desc->extended_source,
 736                                  NL802154_KEY_ID_ATTR_PAD))
 737                         return -ENOBUFS;
 738                 break;
 739         default:
 740                 /* userspace should handle unknown */
 741                 break;
 742         }
 743 
 744         /* TODO key_id to key_idx ? Check naming */
 745         if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
 746                 if (nla_put_u8(msg, NL802154_KEY_ID_ATTR_INDEX, desc->id))
 747                         return -ENOBUFS;
 748         }
 749 
 750         return 0;
 751 }
 752 
 753 static int nl802154_get_llsec_params(struct sk_buff *msg,
 754                                      struct cfg802154_registered_device *rdev,
 755                                      struct wpan_dev *wpan_dev)
 756 {
 757         struct nlattr *nl_key_id;
 758         struct ieee802154_llsec_params params;
 759         int ret;
 760 
 761         ret = rdev_get_llsec_params(rdev, wpan_dev, &params);
 762         if (ret < 0)
 763                 return ret;
 764 
 765         if (nla_put_u8(msg, NL802154_ATTR_SEC_ENABLED, params.enabled) ||
 766             nla_put_u32(msg, NL802154_ATTR_SEC_OUT_LEVEL, params.out_level) ||
 767             nla_put_be32(msg, NL802154_ATTR_SEC_FRAME_COUNTER,
 768                          params.frame_counter))
 769                 return -ENOBUFS;
 770 
 771         nl_key_id = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_OUT_KEY_ID);
 772         if (!nl_key_id)
 773                 return -ENOBUFS;
 774 
 775         ret = ieee802154_llsec_send_key_id(msg, &params.out_key);
 776         if (ret < 0)
 777                 return ret;
 778 
 779         nla_nest_end(msg, nl_key_id);
 780 
 781         return 0;
 782 }
 783 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
 784 
 785 static int
 786 nl802154_send_iface(struct sk_buff *msg, u32 portid, u32 seq, int flags,
 787                     struct cfg802154_registered_device *rdev,
 788                     struct wpan_dev *wpan_dev)
 789 {
 790         struct net_device *dev = wpan_dev->netdev;
 791         void *hdr;
 792 
 793         hdr = nl802154hdr_put(msg, portid, seq, flags,
 794                               NL802154_CMD_NEW_INTERFACE);
 795         if (!hdr)
 796                 return -1;
 797 
 798         if (dev &&
 799             (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex) ||
 800              nla_put_string(msg, NL802154_ATTR_IFNAME, dev->name)))
 801                 goto nla_put_failure;
 802 
 803         if (nla_put_u32(msg, NL802154_ATTR_WPAN_PHY, rdev->wpan_phy_idx) ||
 804             nla_put_u32(msg, NL802154_ATTR_IFTYPE, wpan_dev->iftype) ||
 805             nla_put_u64_64bit(msg, NL802154_ATTR_WPAN_DEV,
 806                               wpan_dev_id(wpan_dev), NL802154_ATTR_PAD) ||
 807             nla_put_u32(msg, NL802154_ATTR_GENERATION,
 808                         rdev->devlist_generation ^
 809                         (cfg802154_rdev_list_generation << 2)))
 810                 goto nla_put_failure;
 811 
 812         /* address settings */
 813         if (nla_put_le64(msg, NL802154_ATTR_EXTENDED_ADDR,
 814                          wpan_dev->extended_addr,
 815                          NL802154_ATTR_PAD) ||
 816             nla_put_le16(msg, NL802154_ATTR_SHORT_ADDR,
 817                          wpan_dev->short_addr) ||
 818             nla_put_le16(msg, NL802154_ATTR_PAN_ID, wpan_dev->pan_id))
 819                 goto nla_put_failure;
 820 
 821         /* ARET handling */
 822         if (nla_put_s8(msg, NL802154_ATTR_MAX_FRAME_RETRIES,
 823                        wpan_dev->frame_retries) ||
 824             nla_put_u8(msg, NL802154_ATTR_MAX_BE, wpan_dev->max_be) ||
 825             nla_put_u8(msg, NL802154_ATTR_MAX_CSMA_BACKOFFS,
 826                        wpan_dev->csma_retries) ||
 827             nla_put_u8(msg, NL802154_ATTR_MIN_BE, wpan_dev->min_be))
 828                 goto nla_put_failure;
 829 
 830         /* listen before transmit */
 831         if (nla_put_u8(msg, NL802154_ATTR_LBT_MODE, wpan_dev->lbt))
 832                 goto nla_put_failure;
 833 
 834         /* ackreq default behaviour */
 835         if (nla_put_u8(msg, NL802154_ATTR_ACKREQ_DEFAULT, wpan_dev->ackreq))
 836                 goto nla_put_failure;
 837 
 838 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
 839         if (nl802154_get_llsec_params(msg, rdev, wpan_dev) < 0)
 840                 goto nla_put_failure;
 841 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
 842 
 843         genlmsg_end(msg, hdr);
 844         return 0;
 845 
 846 nla_put_failure:
 847         genlmsg_cancel(msg, hdr);
 848         return -EMSGSIZE;
 849 }
 850 
 851 static int
 852 nl802154_dump_interface(struct sk_buff *skb, struct netlink_callback *cb)
 853 {
 854         int wp_idx = 0;
 855         int if_idx = 0;
 856         int wp_start = cb->args[0];
 857         int if_start = cb->args[1];
 858         struct cfg802154_registered_device *rdev;
 859         struct wpan_dev *wpan_dev;
 860 
 861         rtnl_lock();
 862         list_for_each_entry(rdev, &cfg802154_rdev_list, list) {
 863                 if (!net_eq(wpan_phy_net(&rdev->wpan_phy), sock_net(skb->sk)))
 864                         continue;
 865                 if (wp_idx < wp_start) {
 866                         wp_idx++;
 867                         continue;
 868                 }
 869                 if_idx = 0;
 870 
 871                 list_for_each_entry(wpan_dev, &rdev->wpan_dev_list, list) {
 872                         if (if_idx < if_start) {
 873                                 if_idx++;
 874                                 continue;
 875                         }
 876                         if (nl802154_send_iface(skb, NETLINK_CB(cb->skb).portid,
 877                                                 cb->nlh->nlmsg_seq, NLM_F_MULTI,
 878                                                 rdev, wpan_dev) < 0) {
 879                                 goto out;
 880                         }
 881                         if_idx++;
 882                 }
 883 
 884                 wp_idx++;
 885         }
 886 out:
 887         rtnl_unlock();
 888 
 889         cb->args[0] = wp_idx;
 890         cb->args[1] = if_idx;
 891 
 892         return skb->len;
 893 }
 894 
 895 static int nl802154_get_interface(struct sk_buff *skb, struct genl_info *info)
 896 {
 897         struct sk_buff *msg;
 898         struct cfg802154_registered_device *rdev = info->user_ptr[0];
 899         struct wpan_dev *wdev = info->user_ptr[1];
 900 
 901         msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
 902         if (!msg)
 903                 return -ENOMEM;
 904 
 905         if (nl802154_send_iface(msg, info->snd_portid, info->snd_seq, 0,
 906                                 rdev, wdev) < 0) {
 907                 nlmsg_free(msg);
 908                 return -ENOBUFS;
 909         }
 910 
 911         return genlmsg_reply(msg, info);
 912 }
 913 
 914 static int nl802154_new_interface(struct sk_buff *skb, struct genl_info *info)
 915 {
 916         struct cfg802154_registered_device *rdev = info->user_ptr[0];
 917         enum nl802154_iftype type = NL802154_IFTYPE_UNSPEC;
 918         __le64 extended_addr = cpu_to_le64(0x0000000000000000ULL);
 919 
 920         /* TODO avoid failing a new interface
 921          * creation due to pending removal?
 922          */
 923 
 924         if (!info->attrs[NL802154_ATTR_IFNAME])
 925                 return -EINVAL;
 926 
 927         if (info->attrs[NL802154_ATTR_IFTYPE]) {
 928                 type = nla_get_u32(info->attrs[NL802154_ATTR_IFTYPE]);
 929                 if (type > NL802154_IFTYPE_MAX ||
 930                     !(rdev->wpan_phy.supported.iftypes & BIT(type)))
 931                         return -EINVAL;
 932         }
 933 
 934         if (info->attrs[NL802154_ATTR_EXTENDED_ADDR])
 935                 extended_addr = nla_get_le64(info->attrs[NL802154_ATTR_EXTENDED_ADDR]);
 936 
 937         if (!rdev->ops->add_virtual_intf)
 938                 return -EOPNOTSUPP;
 939 
 940         return rdev_add_virtual_intf(rdev,
 941                                      nla_data(info->attrs[NL802154_ATTR_IFNAME]),
 942                                      NET_NAME_USER, type, extended_addr);
 943 }
 944 
 945 static int nl802154_del_interface(struct sk_buff *skb, struct genl_info *info)
 946 {
 947         struct cfg802154_registered_device *rdev = info->user_ptr[0];
 948         struct wpan_dev *wpan_dev = info->user_ptr[1];
 949 
 950         if (!rdev->ops->del_virtual_intf)
 951                 return -EOPNOTSUPP;
 952 
 953         /* If we remove a wpan device without a netdev then clear
 954          * user_ptr[1] so that nl802154_post_doit won't dereference it
 955          * to check if it needs to do dev_put(). Otherwise it crashes
 956          * since the wpan_dev has been freed, unlike with a netdev where
 957          * we need the dev_put() for the netdev to really be freed.
 958          */
 959         if (!wpan_dev->netdev)
 960                 info->user_ptr[1] = NULL;
 961 
 962         return rdev_del_virtual_intf(rdev, wpan_dev);
 963 }
 964 
 965 static int nl802154_set_channel(struct sk_buff *skb, struct genl_info *info)
 966 {
 967         struct cfg802154_registered_device *rdev = info->user_ptr[0];
 968         u8 channel, page;
 969 
 970         if (!info->attrs[NL802154_ATTR_PAGE] ||
 971             !info->attrs[NL802154_ATTR_CHANNEL])
 972                 return -EINVAL;
 973 
 974         page = nla_get_u8(info->attrs[NL802154_ATTR_PAGE]);
 975         channel = nla_get_u8(info->attrs[NL802154_ATTR_CHANNEL]);
 976 
 977         /* check 802.15.4 constraints */
 978         if (page > IEEE802154_MAX_PAGE || channel > IEEE802154_MAX_CHANNEL ||
 979             !(rdev->wpan_phy.supported.channels[page] & BIT(channel)))
 980                 return -EINVAL;
 981 
 982         return rdev_set_channel(rdev, page, channel);
 983 }
 984 
 985 static int nl802154_set_cca_mode(struct sk_buff *skb, struct genl_info *info)
 986 {
 987         struct cfg802154_registered_device *rdev = info->user_ptr[0];
 988         struct wpan_phy_cca cca;
 989 
 990         if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_MODE))
 991                 return -EOPNOTSUPP;
 992 
 993         if (!info->attrs[NL802154_ATTR_CCA_MODE])
 994                 return -EINVAL;
 995 
 996         cca.mode = nla_get_u32(info->attrs[NL802154_ATTR_CCA_MODE]);
 997         /* checking 802.15.4 constraints */
 998         if (cca.mode < NL802154_CCA_ENERGY ||
 999             cca.mode > NL802154_CCA_ATTR_MAX ||
1000             !(rdev->wpan_phy.supported.cca_modes & BIT(cca.mode)))
1001                 return -EINVAL;
1002 
1003         if (cca.mode == NL802154_CCA_ENERGY_CARRIER) {
1004                 if (!info->attrs[NL802154_ATTR_CCA_OPT])
1005                         return -EINVAL;
1006 
1007                 cca.opt = nla_get_u32(info->attrs[NL802154_ATTR_CCA_OPT]);
1008                 if (cca.opt > NL802154_CCA_OPT_ATTR_MAX ||
1009                     !(rdev->wpan_phy.supported.cca_opts & BIT(cca.opt)))
1010                         return -EINVAL;
1011         }
1012 
1013         return rdev_set_cca_mode(rdev, &cca);
1014 }
1015 
1016 static int nl802154_set_cca_ed_level(struct sk_buff *skb, struct genl_info *info)
1017 {
1018         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1019         s32 ed_level;
1020         int i;
1021 
1022         if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_CCA_ED_LEVEL))
1023                 return -EOPNOTSUPP;
1024 
1025         if (!info->attrs[NL802154_ATTR_CCA_ED_LEVEL])
1026                 return -EINVAL;
1027 
1028         ed_level = nla_get_s32(info->attrs[NL802154_ATTR_CCA_ED_LEVEL]);
1029 
1030         for (i = 0; i < rdev->wpan_phy.supported.cca_ed_levels_size; i++) {
1031                 if (ed_level == rdev->wpan_phy.supported.cca_ed_levels[i])
1032                         return rdev_set_cca_ed_level(rdev, ed_level);
1033         }
1034 
1035         return -EINVAL;
1036 }
1037 
1038 static int nl802154_set_tx_power(struct sk_buff *skb, struct genl_info *info)
1039 {
1040         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1041         s32 power;
1042         int i;
1043 
1044         if (!(rdev->wpan_phy.flags & WPAN_PHY_FLAG_TXPOWER))
1045                 return -EOPNOTSUPP;
1046 
1047         if (!info->attrs[NL802154_ATTR_TX_POWER])
1048                 return -EINVAL;
1049 
1050         power = nla_get_s32(info->attrs[NL802154_ATTR_TX_POWER]);
1051 
1052         for (i = 0; i < rdev->wpan_phy.supported.tx_powers_size; i++) {
1053                 if (power == rdev->wpan_phy.supported.tx_powers[i])
1054                         return rdev_set_tx_power(rdev, power);
1055         }
1056 
1057         return -EINVAL;
1058 }
1059 
1060 static int nl802154_set_pan_id(struct sk_buff *skb, struct genl_info *info)
1061 {
1062         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1063         struct net_device *dev = info->user_ptr[1];
1064         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1065         __le16 pan_id;
1066 
1067         /* conflict here while tx/rx calls */
1068         if (netif_running(dev))
1069                 return -EBUSY;
1070 
1071         if (wpan_dev->lowpan_dev) {
1072                 if (netif_running(wpan_dev->lowpan_dev))
1073                         return -EBUSY;
1074         }
1075 
1076         /* don't change address fields on monitor */
1077         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1078             !info->attrs[NL802154_ATTR_PAN_ID])
1079                 return -EINVAL;
1080 
1081         pan_id = nla_get_le16(info->attrs[NL802154_ATTR_PAN_ID]);
1082 
1083         /* TODO
1084          * I am not sure about to check here on broadcast pan_id.
1085          * Broadcast is a valid setting, comment from 802.15.4:
1086          * If this value is 0xffff, the device is not associated.
1087          *
1088          * This could useful to simple deassociate an device.
1089          */
1090         if (pan_id == cpu_to_le16(IEEE802154_PAN_ID_BROADCAST))
1091                 return -EINVAL;
1092 
1093         return rdev_set_pan_id(rdev, wpan_dev, pan_id);
1094 }
1095 
1096 static int nl802154_set_short_addr(struct sk_buff *skb, struct genl_info *info)
1097 {
1098         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1099         struct net_device *dev = info->user_ptr[1];
1100         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1101         __le16 short_addr;
1102 
1103         /* conflict here while tx/rx calls */
1104         if (netif_running(dev))
1105                 return -EBUSY;
1106 
1107         if (wpan_dev->lowpan_dev) {
1108                 if (netif_running(wpan_dev->lowpan_dev))
1109                         return -EBUSY;
1110         }
1111 
1112         /* don't change address fields on monitor */
1113         if (wpan_dev->iftype == NL802154_IFTYPE_MONITOR ||
1114             !info->attrs[NL802154_ATTR_SHORT_ADDR])
1115                 return -EINVAL;
1116 
1117         short_addr = nla_get_le16(info->attrs[NL802154_ATTR_SHORT_ADDR]);
1118 
1119         /* TODO
1120          * I am not sure about to check here on broadcast short_addr.
1121          * Broadcast is a valid setting, comment from 802.15.4:
1122          * A value of 0xfffe indicates that the device has
1123          * associated but has not been allocated an address. A
1124          * value of 0xffff indicates that the device does not
1125          * have a short address.
1126          *
1127          * I think we should allow to set these settings but
1128          * don't allow to allow socket communication with it.
1129          */
1130         if (short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_UNSPEC) ||
1131             short_addr == cpu_to_le16(IEEE802154_ADDR_SHORT_BROADCAST))
1132                 return -EINVAL;
1133 
1134         return rdev_set_short_addr(rdev, wpan_dev, short_addr);
1135 }
1136 
1137 static int
1138 nl802154_set_backoff_exponent(struct sk_buff *skb, struct genl_info *info)
1139 {
1140         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1141         struct net_device *dev = info->user_ptr[1];
1142         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1143         u8 min_be, max_be;
1144 
1145         /* should be set on netif open inside phy settings */
1146         if (netif_running(dev))
1147                 return -EBUSY;
1148 
1149         if (!info->attrs[NL802154_ATTR_MIN_BE] ||
1150             !info->attrs[NL802154_ATTR_MAX_BE])
1151                 return -EINVAL;
1152 
1153         min_be = nla_get_u8(info->attrs[NL802154_ATTR_MIN_BE]);
1154         max_be = nla_get_u8(info->attrs[NL802154_ATTR_MAX_BE]);
1155 
1156         /* check 802.15.4 constraints */
1157         if (min_be < rdev->wpan_phy.supported.min_minbe ||
1158             min_be > rdev->wpan_phy.supported.max_minbe ||
1159             max_be < rdev->wpan_phy.supported.min_maxbe ||
1160             max_be > rdev->wpan_phy.supported.max_maxbe ||
1161             min_be > max_be)
1162                 return -EINVAL;
1163 
1164         return rdev_set_backoff_exponent(rdev, wpan_dev, min_be, max_be);
1165 }
1166 
1167 static int
1168 nl802154_set_max_csma_backoffs(struct sk_buff *skb, struct genl_info *info)
1169 {
1170         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1171         struct net_device *dev = info->user_ptr[1];
1172         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1173         u8 max_csma_backoffs;
1174 
1175         /* conflict here while other running iface settings */
1176         if (netif_running(dev))
1177                 return -EBUSY;
1178 
1179         if (!info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS])
1180                 return -EINVAL;
1181 
1182         max_csma_backoffs = nla_get_u8(
1183                         info->attrs[NL802154_ATTR_MAX_CSMA_BACKOFFS]);
1184 
1185         /* check 802.15.4 constraints */
1186         if (max_csma_backoffs < rdev->wpan_phy.supported.min_csma_backoffs ||
1187             max_csma_backoffs > rdev->wpan_phy.supported.max_csma_backoffs)
1188                 return -EINVAL;
1189 
1190         return rdev_set_max_csma_backoffs(rdev, wpan_dev, max_csma_backoffs);
1191 }
1192 
1193 static int
1194 nl802154_set_max_frame_retries(struct sk_buff *skb, struct genl_info *info)
1195 {
1196         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1197         struct net_device *dev = info->user_ptr[1];
1198         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1199         s8 max_frame_retries;
1200 
1201         if (netif_running(dev))
1202                 return -EBUSY;
1203 
1204         if (!info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES])
1205                 return -EINVAL;
1206 
1207         max_frame_retries = nla_get_s8(
1208                         info->attrs[NL802154_ATTR_MAX_FRAME_RETRIES]);
1209 
1210         /* check 802.15.4 constraints */
1211         if (max_frame_retries < rdev->wpan_phy.supported.min_frame_retries ||
1212             max_frame_retries > rdev->wpan_phy.supported.max_frame_retries)
1213                 return -EINVAL;
1214 
1215         return rdev_set_max_frame_retries(rdev, wpan_dev, max_frame_retries);
1216 }
1217 
1218 static int nl802154_set_lbt_mode(struct sk_buff *skb, struct genl_info *info)
1219 {
1220         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1221         struct net_device *dev = info->user_ptr[1];
1222         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1223         int mode;
1224 
1225         if (netif_running(dev))
1226                 return -EBUSY;
1227 
1228         if (!info->attrs[NL802154_ATTR_LBT_MODE])
1229                 return -EINVAL;
1230 
1231         mode = nla_get_u8(info->attrs[NL802154_ATTR_LBT_MODE]);
1232 
1233         if (mode != 0 && mode != 1)
1234                 return -EINVAL;
1235 
1236         if (!wpan_phy_supported_bool(mode, rdev->wpan_phy.supported.lbt))
1237                 return -EINVAL;
1238 
1239         return rdev_set_lbt_mode(rdev, wpan_dev, mode);
1240 }
1241 
1242 static int
1243 nl802154_set_ackreq_default(struct sk_buff *skb, struct genl_info *info)
1244 {
1245         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1246         struct net_device *dev = info->user_ptr[1];
1247         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1248         int ackreq;
1249 
1250         if (netif_running(dev))
1251                 return -EBUSY;
1252 
1253         if (!info->attrs[NL802154_ATTR_ACKREQ_DEFAULT])
1254                 return -EINVAL;
1255 
1256         ackreq = nla_get_u8(info->attrs[NL802154_ATTR_ACKREQ_DEFAULT]);
1257 
1258         if (ackreq != 0 && ackreq != 1)
1259                 return -EINVAL;
1260 
1261         return rdev_set_ackreq_default(rdev, wpan_dev, ackreq);
1262 }
1263 
1264 static int nl802154_wpan_phy_netns(struct sk_buff *skb, struct genl_info *info)
1265 {
1266         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1267         struct net *net;
1268         int err;
1269 
1270         if (info->attrs[NL802154_ATTR_PID]) {
1271                 u32 pid = nla_get_u32(info->attrs[NL802154_ATTR_PID]);
1272 
1273                 net = get_net_ns_by_pid(pid);
1274         } else if (info->attrs[NL802154_ATTR_NETNS_FD]) {
1275                 u32 fd = nla_get_u32(info->attrs[NL802154_ATTR_NETNS_FD]);
1276 
1277                 net = get_net_ns_by_fd(fd);
1278         } else {
1279                 return -EINVAL;
1280         }
1281 
1282         if (IS_ERR(net))
1283                 return PTR_ERR(net);
1284 
1285         err = 0;
1286 
1287         /* check if anything to do */
1288         if (!net_eq(wpan_phy_net(&rdev->wpan_phy), net))
1289                 err = cfg802154_switch_netns(rdev, net);
1290 
1291         put_net(net);
1292         return err;
1293 }
1294 
1295 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
1296 static const struct nla_policy nl802154_dev_addr_policy[NL802154_DEV_ADDR_ATTR_MAX + 1] = {
1297         [NL802154_DEV_ADDR_ATTR_PAN_ID] = { .type = NLA_U16 },
1298         [NL802154_DEV_ADDR_ATTR_MODE] = { .type = NLA_U32 },
1299         [NL802154_DEV_ADDR_ATTR_SHORT] = { .type = NLA_U16 },
1300         [NL802154_DEV_ADDR_ATTR_EXTENDED] = { .type = NLA_U64 },
1301 };
1302 
1303 static int
1304 ieee802154_llsec_parse_dev_addr(struct nlattr *nla,
1305                                 struct ieee802154_addr *addr)
1306 {
1307         struct nlattr *attrs[NL802154_DEV_ADDR_ATTR_MAX + 1];
1308 
1309         if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ADDR_ATTR_MAX, nla, nl802154_dev_addr_policy, NULL))
1310                 return -EINVAL;
1311 
1312         if (!attrs[NL802154_DEV_ADDR_ATTR_PAN_ID] ||
1313             !attrs[NL802154_DEV_ADDR_ATTR_MODE] ||
1314             !(attrs[NL802154_DEV_ADDR_ATTR_SHORT] ||
1315               attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]))
1316                 return -EINVAL;
1317 
1318         addr->pan_id = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_PAN_ID]);
1319         addr->mode = nla_get_u32(attrs[NL802154_DEV_ADDR_ATTR_MODE]);
1320         switch (addr->mode) {
1321         case NL802154_DEV_ADDR_SHORT:
1322                 addr->short_addr = nla_get_le16(attrs[NL802154_DEV_ADDR_ATTR_SHORT]);
1323                 break;
1324         case NL802154_DEV_ADDR_EXTENDED:
1325                 addr->extended_addr = nla_get_le64(attrs[NL802154_DEV_ADDR_ATTR_EXTENDED]);
1326                 break;
1327         default:
1328                 return -EINVAL;
1329         }
1330 
1331         return 0;
1332 }
1333 
1334 static const struct nla_policy nl802154_key_id_policy[NL802154_KEY_ID_ATTR_MAX + 1] = {
1335         [NL802154_KEY_ID_ATTR_MODE] = { .type = NLA_U32 },
1336         [NL802154_KEY_ID_ATTR_INDEX] = { .type = NLA_U8 },
1337         [NL802154_KEY_ID_ATTR_IMPLICIT] = { .type = NLA_NESTED },
1338         [NL802154_KEY_ID_ATTR_SOURCE_SHORT] = { .type = NLA_U32 },
1339         [NL802154_KEY_ID_ATTR_SOURCE_EXTENDED] = { .type = NLA_U64 },
1340 };
1341 
1342 static int
1343 ieee802154_llsec_parse_key_id(struct nlattr *nla,
1344                               struct ieee802154_llsec_key_id *desc)
1345 {
1346         struct nlattr *attrs[NL802154_KEY_ID_ATTR_MAX + 1];
1347 
1348         if (!nla || nla_parse_nested_deprecated(attrs, NL802154_KEY_ID_ATTR_MAX, nla, nl802154_key_id_policy, NULL))
1349                 return -EINVAL;
1350 
1351         if (!attrs[NL802154_KEY_ID_ATTR_MODE])
1352                 return -EINVAL;
1353 
1354         desc->mode = nla_get_u32(attrs[NL802154_KEY_ID_ATTR_MODE]);
1355         switch (desc->mode) {
1356         case NL802154_KEY_ID_MODE_IMPLICIT:
1357                 if (!attrs[NL802154_KEY_ID_ATTR_IMPLICIT])
1358                         return -EINVAL;
1359 
1360                 if (ieee802154_llsec_parse_dev_addr(attrs[NL802154_KEY_ID_ATTR_IMPLICIT],
1361                                                     &desc->device_addr) < 0)
1362                         return -EINVAL;
1363                 break;
1364         case NL802154_KEY_ID_MODE_INDEX:
1365                 break;
1366         case NL802154_KEY_ID_MODE_INDEX_SHORT:
1367                 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT])
1368                         return -EINVAL;
1369 
1370                 desc->short_source = nla_get_le32(attrs[NL802154_KEY_ID_ATTR_SOURCE_SHORT]);
1371                 break;
1372         case NL802154_KEY_ID_MODE_INDEX_EXTENDED:
1373                 if (!attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED])
1374                         return -EINVAL;
1375 
1376                 desc->extended_source = nla_get_le64(attrs[NL802154_KEY_ID_ATTR_SOURCE_EXTENDED]);
1377                 break;
1378         default:
1379                 return -EINVAL;
1380         }
1381 
1382         if (desc->mode != NL802154_KEY_ID_MODE_IMPLICIT) {
1383                 if (!attrs[NL802154_KEY_ID_ATTR_INDEX])
1384                         return -EINVAL;
1385 
1386                 /* TODO change id to idx */
1387                 desc->id = nla_get_u8(attrs[NL802154_KEY_ID_ATTR_INDEX]);
1388         }
1389 
1390         return 0;
1391 }
1392 
1393 static int nl802154_set_llsec_params(struct sk_buff *skb,
1394                                      struct genl_info *info)
1395 {
1396         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1397         struct net_device *dev = info->user_ptr[1];
1398         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1399         struct ieee802154_llsec_params params;
1400         u32 changed = 0;
1401         int ret;
1402 
1403         if (info->attrs[NL802154_ATTR_SEC_ENABLED]) {
1404                 u8 enabled;
1405 
1406                 enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1407                 if (enabled != 0 && enabled != 1)
1408                         return -EINVAL;
1409 
1410                 params.enabled = nla_get_u8(info->attrs[NL802154_ATTR_SEC_ENABLED]);
1411                 changed |= IEEE802154_LLSEC_PARAM_ENABLED;
1412         }
1413 
1414         if (info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID]) {
1415                 ret = ieee802154_llsec_parse_key_id(info->attrs[NL802154_ATTR_SEC_OUT_KEY_ID],
1416                                                     &params.out_key);
1417                 if (ret < 0)
1418                         return ret;
1419 
1420                 changed |= IEEE802154_LLSEC_PARAM_OUT_KEY;
1421         }
1422 
1423         if (info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]) {
1424                 params.out_level = nla_get_u32(info->attrs[NL802154_ATTR_SEC_OUT_LEVEL]);
1425                 if (params.out_level > NL802154_SECLEVEL_MAX)
1426                         return -EINVAL;
1427 
1428                 changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL;
1429         }
1430 
1431         if (info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]) {
1432                 params.frame_counter = nla_get_be32(info->attrs[NL802154_ATTR_SEC_FRAME_COUNTER]);
1433                 changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER;
1434         }
1435 
1436         return rdev_set_llsec_params(rdev, wpan_dev, &params, changed);
1437 }
1438 
1439 static int nl802154_send_key(struct sk_buff *msg, u32 cmd, u32 portid,
1440                              u32 seq, int flags,
1441                              struct cfg802154_registered_device *rdev,
1442                              struct net_device *dev,
1443                              const struct ieee802154_llsec_key_entry *key)
1444 {
1445         void *hdr;
1446         u32 commands[NL802154_CMD_FRAME_NR_IDS / 32];
1447         struct nlattr *nl_key, *nl_key_id;
1448 
1449         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1450         if (!hdr)
1451                 return -1;
1452 
1453         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1454                 goto nla_put_failure;
1455 
1456         nl_key = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_KEY);
1457         if (!nl_key)
1458                 goto nla_put_failure;
1459 
1460         nl_key_id = nla_nest_start_noflag(msg, NL802154_KEY_ATTR_ID);
1461         if (!nl_key_id)
1462                 goto nla_put_failure;
1463 
1464         if (ieee802154_llsec_send_key_id(msg, &key->id) < 0)
1465                 goto nla_put_failure;
1466 
1467         nla_nest_end(msg, nl_key_id);
1468 
1469         if (nla_put_u8(msg, NL802154_KEY_ATTR_USAGE_FRAMES,
1470                        key->key->frame_types))
1471                 goto nla_put_failure;
1472 
1473         if (key->key->frame_types & BIT(NL802154_FRAME_CMD)) {
1474                 /* TODO for each nested */
1475                 memset(commands, 0, sizeof(commands));
1476                 commands[7] = key->key->cmd_frame_ids;
1477                 if (nla_put(msg, NL802154_KEY_ATTR_USAGE_CMDS,
1478                             sizeof(commands), commands))
1479                         goto nla_put_failure;
1480         }
1481 
1482         if (nla_put(msg, NL802154_KEY_ATTR_BYTES, NL802154_KEY_SIZE,
1483                     key->key->key))
1484                 goto nla_put_failure;
1485 
1486         nla_nest_end(msg, nl_key);
1487         genlmsg_end(msg, hdr);
1488 
1489         return 0;
1490 
1491 nla_put_failure:
1492         genlmsg_cancel(msg, hdr);
1493         return -EMSGSIZE;
1494 }
1495 
1496 static int
1497 nl802154_dump_llsec_key(struct sk_buff *skb, struct netlink_callback *cb)
1498 {
1499         struct cfg802154_registered_device *rdev = NULL;
1500         struct ieee802154_llsec_key_entry *key;
1501         struct ieee802154_llsec_table *table;
1502         struct wpan_dev *wpan_dev;
1503         int err;
1504 
1505         err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1506         if (err)
1507                 return err;
1508 
1509         if (!wpan_dev->netdev) {
1510                 err = -EINVAL;
1511                 goto out_err;
1512         }
1513 
1514         rdev_lock_llsec_table(rdev, wpan_dev);
1515         rdev_get_llsec_table(rdev, wpan_dev, &table);
1516 
1517         /* TODO make it like station dump */
1518         if (cb->args[2])
1519                 goto out;
1520 
1521         list_for_each_entry(key, &table->keys, list) {
1522                 if (nl802154_send_key(skb, NL802154_CMD_NEW_SEC_KEY,
1523                                       NETLINK_CB(cb->skb).portid,
1524                                       cb->nlh->nlmsg_seq, NLM_F_MULTI,
1525                                       rdev, wpan_dev->netdev, key) < 0) {
1526                         /* TODO */
1527                         err = -EIO;
1528                         rdev_unlock_llsec_table(rdev, wpan_dev);
1529                         goto out_err;
1530                 }
1531         }
1532 
1533         cb->args[2] = 1;
1534 
1535 out:
1536         rdev_unlock_llsec_table(rdev, wpan_dev);
1537         err = skb->len;
1538 out_err:
1539         nl802154_finish_wpan_dev_dump(rdev);
1540 
1541         return err;
1542 }
1543 
1544 static const struct nla_policy nl802154_key_policy[NL802154_KEY_ATTR_MAX + 1] = {
1545         [NL802154_KEY_ATTR_ID] = { NLA_NESTED },
1546         /* TODO handle it as for_each_nested and NLA_FLAG? */
1547         [NL802154_KEY_ATTR_USAGE_FRAMES] = { NLA_U8 },
1548         /* TODO handle it as for_each_nested, not static array? */
1549         [NL802154_KEY_ATTR_USAGE_CMDS] = { .len = NL802154_CMD_FRAME_NR_IDS / 8 },
1550         [NL802154_KEY_ATTR_BYTES] = { .len = NL802154_KEY_SIZE },
1551 };
1552 
1553 static int nl802154_add_llsec_key(struct sk_buff *skb, struct genl_info *info)
1554 {
1555         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1556         struct net_device *dev = info->user_ptr[1];
1557         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1558         struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1559         struct ieee802154_llsec_key key = { };
1560         struct ieee802154_llsec_key_id id = { };
1561         u32 commands[NL802154_CMD_FRAME_NR_IDS / 32] = { };
1562 
1563         if (nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1564                 return -EINVAL;
1565 
1566         if (!attrs[NL802154_KEY_ATTR_USAGE_FRAMES] ||
1567             !attrs[NL802154_KEY_ATTR_BYTES])
1568                 return -EINVAL;
1569 
1570         if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1571                 return -ENOBUFS;
1572 
1573         key.frame_types = nla_get_u8(attrs[NL802154_KEY_ATTR_USAGE_FRAMES]);
1574         if (key.frame_types > BIT(NL802154_FRAME_MAX) ||
1575             ((key.frame_types & BIT(NL802154_FRAME_CMD)) &&
1576              !attrs[NL802154_KEY_ATTR_USAGE_CMDS]))
1577                 return -EINVAL;
1578 
1579         if (attrs[NL802154_KEY_ATTR_USAGE_CMDS]) {
1580                 /* TODO for each nested */
1581                 nla_memcpy(commands, attrs[NL802154_KEY_ATTR_USAGE_CMDS],
1582                            NL802154_CMD_FRAME_NR_IDS / 8);
1583 
1584                 /* TODO understand the -EINVAL logic here? last condition */
1585                 if (commands[0] || commands[1] || commands[2] || commands[3] ||
1586                     commands[4] || commands[5] || commands[6] ||
1587                     commands[7] > BIT(NL802154_CMD_FRAME_MAX))
1588                         return -EINVAL;
1589 
1590                 key.cmd_frame_ids = commands[7];
1591         } else {
1592                 key.cmd_frame_ids = 0;
1593         }
1594 
1595         nla_memcpy(key.key, attrs[NL802154_KEY_ATTR_BYTES], NL802154_KEY_SIZE);
1596 
1597         if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1598                 return -ENOBUFS;
1599 
1600         return rdev_add_llsec_key(rdev, wpan_dev, &id, &key);
1601 }
1602 
1603 static int nl802154_del_llsec_key(struct sk_buff *skb, struct genl_info *info)
1604 {
1605         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1606         struct net_device *dev = info->user_ptr[1];
1607         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1608         struct nlattr *attrs[NL802154_KEY_ATTR_MAX + 1];
1609         struct ieee802154_llsec_key_id id;
1610 
1611         if (nla_parse_nested_deprecated(attrs, NL802154_KEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_KEY], nl802154_key_policy, info->extack))
1612                 return -EINVAL;
1613 
1614         if (ieee802154_llsec_parse_key_id(attrs[NL802154_KEY_ATTR_ID], &id) < 0)
1615                 return -ENOBUFS;
1616 
1617         return rdev_del_llsec_key(rdev, wpan_dev, &id);
1618 }
1619 
1620 static int nl802154_send_device(struct sk_buff *msg, u32 cmd, u32 portid,
1621                                 u32 seq, int flags,
1622                                 struct cfg802154_registered_device *rdev,
1623                                 struct net_device *dev,
1624                                 const struct ieee802154_llsec_device *dev_desc)
1625 {
1626         void *hdr;
1627         struct nlattr *nl_device;
1628 
1629         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1630         if (!hdr)
1631                 return -1;
1632 
1633         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1634                 goto nla_put_failure;
1635 
1636         nl_device = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVICE);
1637         if (!nl_device)
1638                 goto nla_put_failure;
1639 
1640         if (nla_put_u32(msg, NL802154_DEV_ATTR_FRAME_COUNTER,
1641                         dev_desc->frame_counter) ||
1642             nla_put_le16(msg, NL802154_DEV_ATTR_PAN_ID, dev_desc->pan_id) ||
1643             nla_put_le16(msg, NL802154_DEV_ATTR_SHORT_ADDR,
1644                          dev_desc->short_addr) ||
1645             nla_put_le64(msg, NL802154_DEV_ATTR_EXTENDED_ADDR,
1646                          dev_desc->hwaddr, NL802154_DEV_ATTR_PAD) ||
1647             nla_put_u8(msg, NL802154_DEV_ATTR_SECLEVEL_EXEMPT,
1648                        dev_desc->seclevel_exempt) ||
1649             nla_put_u32(msg, NL802154_DEV_ATTR_KEY_MODE, dev_desc->key_mode))
1650                 goto nla_put_failure;
1651 
1652         nla_nest_end(msg, nl_device);
1653         genlmsg_end(msg, hdr);
1654 
1655         return 0;
1656 
1657 nla_put_failure:
1658         genlmsg_cancel(msg, hdr);
1659         return -EMSGSIZE;
1660 }
1661 
1662 static int
1663 nl802154_dump_llsec_dev(struct sk_buff *skb, struct netlink_callback *cb)
1664 {
1665         struct cfg802154_registered_device *rdev = NULL;
1666         struct ieee802154_llsec_device *dev;
1667         struct ieee802154_llsec_table *table;
1668         struct wpan_dev *wpan_dev;
1669         int err;
1670 
1671         err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1672         if (err)
1673                 return err;
1674 
1675         if (!wpan_dev->netdev) {
1676                 err = -EINVAL;
1677                 goto out_err;
1678         }
1679 
1680         rdev_lock_llsec_table(rdev, wpan_dev);
1681         rdev_get_llsec_table(rdev, wpan_dev, &table);
1682 
1683         /* TODO make it like station dump */
1684         if (cb->args[2])
1685                 goto out;
1686 
1687         list_for_each_entry(dev, &table->devices, list) {
1688                 if (nl802154_send_device(skb, NL802154_CMD_NEW_SEC_LEVEL,
1689                                          NETLINK_CB(cb->skb).portid,
1690                                          cb->nlh->nlmsg_seq, NLM_F_MULTI,
1691                                          rdev, wpan_dev->netdev, dev) < 0) {
1692                         /* TODO */
1693                         err = -EIO;
1694                         rdev_unlock_llsec_table(rdev, wpan_dev);
1695                         goto out_err;
1696                 }
1697         }
1698 
1699         cb->args[2] = 1;
1700 
1701 out:
1702         rdev_unlock_llsec_table(rdev, wpan_dev);
1703         err = skb->len;
1704 out_err:
1705         nl802154_finish_wpan_dev_dump(rdev);
1706 
1707         return err;
1708 }
1709 
1710 static const struct nla_policy nl802154_dev_policy[NL802154_DEV_ATTR_MAX + 1] = {
1711         [NL802154_DEV_ATTR_FRAME_COUNTER] = { NLA_U32 },
1712         [NL802154_DEV_ATTR_PAN_ID] = { .type = NLA_U16 },
1713         [NL802154_DEV_ATTR_SHORT_ADDR] = { .type = NLA_U16 },
1714         [NL802154_DEV_ATTR_EXTENDED_ADDR] = { .type = NLA_U64 },
1715         [NL802154_DEV_ATTR_SECLEVEL_EXEMPT] = { NLA_U8 },
1716         [NL802154_DEV_ATTR_KEY_MODE] = { NLA_U32 },
1717 };
1718 
1719 static int
1720 ieee802154_llsec_parse_device(struct nlattr *nla,
1721                               struct ieee802154_llsec_device *dev)
1722 {
1723         struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1724 
1725         if (!nla || nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, nla, nl802154_dev_policy, NULL))
1726                 return -EINVAL;
1727 
1728         memset(dev, 0, sizeof(*dev));
1729 
1730         if (!attrs[NL802154_DEV_ATTR_FRAME_COUNTER] ||
1731             !attrs[NL802154_DEV_ATTR_PAN_ID] ||
1732             !attrs[NL802154_DEV_ATTR_SHORT_ADDR] ||
1733             !attrs[NL802154_DEV_ATTR_EXTENDED_ADDR] ||
1734             !attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT] ||
1735             !attrs[NL802154_DEV_ATTR_KEY_MODE])
1736                 return -EINVAL;
1737 
1738         /* TODO be32 */
1739         dev->frame_counter = nla_get_u32(attrs[NL802154_DEV_ATTR_FRAME_COUNTER]);
1740         dev->pan_id = nla_get_le16(attrs[NL802154_DEV_ATTR_PAN_ID]);
1741         dev->short_addr = nla_get_le16(attrs[NL802154_DEV_ATTR_SHORT_ADDR]);
1742         /* TODO rename hwaddr to extended_addr */
1743         dev->hwaddr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1744         dev->seclevel_exempt = nla_get_u8(attrs[NL802154_DEV_ATTR_SECLEVEL_EXEMPT]);
1745         dev->key_mode = nla_get_u32(attrs[NL802154_DEV_ATTR_KEY_MODE]);
1746 
1747         if (dev->key_mode > NL802154_DEVKEY_MAX ||
1748             (dev->seclevel_exempt != 0 && dev->seclevel_exempt != 1))
1749                 return -EINVAL;
1750 
1751         return 0;
1752 }
1753 
1754 static int nl802154_add_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1755 {
1756         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1757         struct net_device *dev = info->user_ptr[1];
1758         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1759         struct ieee802154_llsec_device dev_desc;
1760 
1761         if (ieee802154_llsec_parse_device(info->attrs[NL802154_ATTR_SEC_DEVICE],
1762                                           &dev_desc) < 0)
1763                 return -EINVAL;
1764 
1765         return rdev_add_device(rdev, wpan_dev, &dev_desc);
1766 }
1767 
1768 static int nl802154_del_llsec_dev(struct sk_buff *skb, struct genl_info *info)
1769 {
1770         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1771         struct net_device *dev = info->user_ptr[1];
1772         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1773         struct nlattr *attrs[NL802154_DEV_ATTR_MAX + 1];
1774         __le64 extended_addr;
1775 
1776         if (nla_parse_nested_deprecated(attrs, NL802154_DEV_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVICE], nl802154_dev_policy, info->extack))
1777                 return -EINVAL;
1778 
1779         if (!attrs[NL802154_DEV_ATTR_EXTENDED_ADDR])
1780                 return -EINVAL;
1781 
1782         extended_addr = nla_get_le64(attrs[NL802154_DEV_ATTR_EXTENDED_ADDR]);
1783         return rdev_del_device(rdev, wpan_dev, extended_addr);
1784 }
1785 
1786 static int nl802154_send_devkey(struct sk_buff *msg, u32 cmd, u32 portid,
1787                                 u32 seq, int flags,
1788                                 struct cfg802154_registered_device *rdev,
1789                                 struct net_device *dev, __le64 extended_addr,
1790                                 const struct ieee802154_llsec_device_key *devkey)
1791 {
1792         void *hdr;
1793         struct nlattr *nl_devkey, *nl_key_id;
1794 
1795         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1796         if (!hdr)
1797                 return -1;
1798 
1799         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1800                 goto nla_put_failure;
1801 
1802         nl_devkey = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_DEVKEY);
1803         if (!nl_devkey)
1804                 goto nla_put_failure;
1805 
1806         if (nla_put_le64(msg, NL802154_DEVKEY_ATTR_EXTENDED_ADDR,
1807                          extended_addr, NL802154_DEVKEY_ATTR_PAD) ||
1808             nla_put_u32(msg, NL802154_DEVKEY_ATTR_FRAME_COUNTER,
1809                         devkey->frame_counter))
1810                 goto nla_put_failure;
1811 
1812         nl_key_id = nla_nest_start_noflag(msg, NL802154_DEVKEY_ATTR_ID);
1813         if (!nl_key_id)
1814                 goto nla_put_failure;
1815 
1816         if (ieee802154_llsec_send_key_id(msg, &devkey->key_id) < 0)
1817                 goto nla_put_failure;
1818 
1819         nla_nest_end(msg, nl_key_id);
1820         nla_nest_end(msg, nl_devkey);
1821         genlmsg_end(msg, hdr);
1822 
1823         return 0;
1824 
1825 nla_put_failure:
1826         genlmsg_cancel(msg, hdr);
1827         return -EMSGSIZE;
1828 }
1829 
1830 static int
1831 nl802154_dump_llsec_devkey(struct sk_buff *skb, struct netlink_callback *cb)
1832 {
1833         struct cfg802154_registered_device *rdev = NULL;
1834         struct ieee802154_llsec_device_key *kpos;
1835         struct ieee802154_llsec_device *dpos;
1836         struct ieee802154_llsec_table *table;
1837         struct wpan_dev *wpan_dev;
1838         int err;
1839 
1840         err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
1841         if (err)
1842                 return err;
1843 
1844         if (!wpan_dev->netdev) {
1845                 err = -EINVAL;
1846                 goto out_err;
1847         }
1848 
1849         rdev_lock_llsec_table(rdev, wpan_dev);
1850         rdev_get_llsec_table(rdev, wpan_dev, &table);
1851 
1852         /* TODO make it like station dump */
1853         if (cb->args[2])
1854                 goto out;
1855 
1856         /* TODO look if remove devkey and do some nested attribute */
1857         list_for_each_entry(dpos, &table->devices, list) {
1858                 list_for_each_entry(kpos, &dpos->keys, list) {
1859                         if (nl802154_send_devkey(skb,
1860                                                  NL802154_CMD_NEW_SEC_LEVEL,
1861                                                  NETLINK_CB(cb->skb).portid,
1862                                                  cb->nlh->nlmsg_seq,
1863                                                  NLM_F_MULTI, rdev,
1864                                                  wpan_dev->netdev,
1865                                                  dpos->hwaddr,
1866                                                  kpos) < 0) {
1867                                 /* TODO */
1868                                 err = -EIO;
1869                                 rdev_unlock_llsec_table(rdev, wpan_dev);
1870                                 goto out_err;
1871                         }
1872                 }
1873         }
1874 
1875         cb->args[2] = 1;
1876 
1877 out:
1878         rdev_unlock_llsec_table(rdev, wpan_dev);
1879         err = skb->len;
1880 out_err:
1881         nl802154_finish_wpan_dev_dump(rdev);
1882 
1883         return err;
1884 }
1885 
1886 static const struct nla_policy nl802154_devkey_policy[NL802154_DEVKEY_ATTR_MAX + 1] = {
1887         [NL802154_DEVKEY_ATTR_FRAME_COUNTER] = { NLA_U32 },
1888         [NL802154_DEVKEY_ATTR_EXTENDED_ADDR] = { NLA_U64 },
1889         [NL802154_DEVKEY_ATTR_ID] = { NLA_NESTED },
1890 };
1891 
1892 static int nl802154_add_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1893 {
1894         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1895         struct net_device *dev = info->user_ptr[1];
1896         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1897         struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1898         struct ieee802154_llsec_device_key key;
1899         __le64 extended_addr;
1900 
1901         if (!info->attrs[NL802154_ATTR_SEC_DEVKEY] ||
1902             nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack) < 0)
1903                 return -EINVAL;
1904 
1905         if (!attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER] ||
1906             !attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1907                 return -EINVAL;
1908 
1909         /* TODO change key.id ? */
1910         if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1911                                           &key.key_id) < 0)
1912                 return -ENOBUFS;
1913 
1914         /* TODO be32 */
1915         key.frame_counter = nla_get_u32(attrs[NL802154_DEVKEY_ATTR_FRAME_COUNTER]);
1916         /* TODO change naming hwaddr -> extended_addr
1917          * check unique identifier short+pan OR extended_addr
1918          */
1919         extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1920         return rdev_add_devkey(rdev, wpan_dev, extended_addr, &key);
1921 }
1922 
1923 static int nl802154_del_llsec_devkey(struct sk_buff *skb, struct genl_info *info)
1924 {
1925         struct cfg802154_registered_device *rdev = info->user_ptr[0];
1926         struct net_device *dev = info->user_ptr[1];
1927         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
1928         struct nlattr *attrs[NL802154_DEVKEY_ATTR_MAX + 1];
1929         struct ieee802154_llsec_device_key key;
1930         __le64 extended_addr;
1931 
1932         if (nla_parse_nested_deprecated(attrs, NL802154_DEVKEY_ATTR_MAX, info->attrs[NL802154_ATTR_SEC_DEVKEY], nl802154_devkey_policy, info->extack))
1933                 return -EINVAL;
1934 
1935         if (!attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR])
1936                 return -EINVAL;
1937 
1938         /* TODO change key.id ? */
1939         if (ieee802154_llsec_parse_key_id(attrs[NL802154_DEVKEY_ATTR_ID],
1940                                           &key.key_id) < 0)
1941                 return -ENOBUFS;
1942 
1943         /* TODO change naming hwaddr -> extended_addr
1944          * check unique identifier short+pan OR extended_addr
1945          */
1946         extended_addr = nla_get_le64(attrs[NL802154_DEVKEY_ATTR_EXTENDED_ADDR]);
1947         return rdev_del_devkey(rdev, wpan_dev, extended_addr, &key);
1948 }
1949 
1950 static int nl802154_send_seclevel(struct sk_buff *msg, u32 cmd, u32 portid,
1951                                   u32 seq, int flags,
1952                                   struct cfg802154_registered_device *rdev,
1953                                   struct net_device *dev,
1954                                   const struct ieee802154_llsec_seclevel *sl)
1955 {
1956         void *hdr;
1957         struct nlattr *nl_seclevel;
1958 
1959         hdr = nl802154hdr_put(msg, portid, seq, flags, cmd);
1960         if (!hdr)
1961                 return -1;
1962 
1963         if (nla_put_u32(msg, NL802154_ATTR_IFINDEX, dev->ifindex))
1964                 goto nla_put_failure;
1965 
1966         nl_seclevel = nla_nest_start_noflag(msg, NL802154_ATTR_SEC_LEVEL);
1967         if (!nl_seclevel)
1968                 goto nla_put_failure;
1969 
1970         if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_FRAME, sl->frame_type) ||
1971             nla_put_u32(msg, NL802154_SECLEVEL_ATTR_LEVELS, sl->sec_levels) ||
1972             nla_put_u8(msg, NL802154_SECLEVEL_ATTR_DEV_OVERRIDE,
1973                        sl->device_override))
1974                 goto nla_put_failure;
1975 
1976         if (sl->frame_type == NL802154_FRAME_CMD) {
1977                 if (nla_put_u32(msg, NL802154_SECLEVEL_ATTR_CMD_FRAME,
1978                                 sl->cmd_frame_id))
1979                         goto nla_put_failure;
1980         }
1981 
1982         nla_nest_end(msg, nl_seclevel);
1983         genlmsg_end(msg, hdr);
1984 
1985         return 0;
1986 
1987 nla_put_failure:
1988         genlmsg_cancel(msg, hdr);
1989         return -EMSGSIZE;
1990 }
1991 
1992 static int
1993 nl802154_dump_llsec_seclevel(struct sk_buff *skb, struct netlink_callback *cb)
1994 {
1995         struct cfg802154_registered_device *rdev = NULL;
1996         struct ieee802154_llsec_seclevel *sl;
1997         struct ieee802154_llsec_table *table;
1998         struct wpan_dev *wpan_dev;
1999         int err;
2000 
2001         err = nl802154_prepare_wpan_dev_dump(skb, cb, &rdev, &wpan_dev);
2002         if (err)
2003                 return err;
2004 
2005         if (!wpan_dev->netdev) {
2006                 err = -EINVAL;
2007                 goto out_err;
2008         }
2009 
2010         rdev_lock_llsec_table(rdev, wpan_dev);
2011         rdev_get_llsec_table(rdev, wpan_dev, &table);
2012 
2013         /* TODO make it like station dump */
2014         if (cb->args[2])
2015                 goto out;
2016 
2017         list_for_each_entry(sl, &table->security_levels, list) {
2018                 if (nl802154_send_seclevel(skb, NL802154_CMD_NEW_SEC_LEVEL,
2019                                            NETLINK_CB(cb->skb).portid,
2020                                            cb->nlh->nlmsg_seq, NLM_F_MULTI,
2021                                            rdev, wpan_dev->netdev, sl) < 0) {
2022                         /* TODO */
2023                         err = -EIO;
2024                         rdev_unlock_llsec_table(rdev, wpan_dev);
2025                         goto out_err;
2026                 }
2027         }
2028 
2029         cb->args[2] = 1;
2030 
2031 out:
2032         rdev_unlock_llsec_table(rdev, wpan_dev);
2033         err = skb->len;
2034 out_err:
2035         nl802154_finish_wpan_dev_dump(rdev);
2036 
2037         return err;
2038 }
2039 
2040 static const struct nla_policy nl802154_seclevel_policy[NL802154_SECLEVEL_ATTR_MAX + 1] = {
2041         [NL802154_SECLEVEL_ATTR_LEVELS] = { .type = NLA_U8 },
2042         [NL802154_SECLEVEL_ATTR_FRAME] = { .type = NLA_U32 },
2043         [NL802154_SECLEVEL_ATTR_CMD_FRAME] = { .type = NLA_U32 },
2044         [NL802154_SECLEVEL_ATTR_DEV_OVERRIDE] = { .type = NLA_U8 },
2045 };
2046 
2047 static int
2048 llsec_parse_seclevel(struct nlattr *nla, struct ieee802154_llsec_seclevel *sl)
2049 {
2050         struct nlattr *attrs[NL802154_SECLEVEL_ATTR_MAX + 1];
2051 
2052         if (!nla || nla_parse_nested_deprecated(attrs, NL802154_SECLEVEL_ATTR_MAX, nla, nl802154_seclevel_policy, NULL))
2053                 return -EINVAL;
2054 
2055         memset(sl, 0, sizeof(*sl));
2056 
2057         if (!attrs[NL802154_SECLEVEL_ATTR_LEVELS] ||
2058             !attrs[NL802154_SECLEVEL_ATTR_FRAME] ||
2059             !attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE])
2060                 return -EINVAL;
2061 
2062         sl->sec_levels = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_LEVELS]);
2063         sl->frame_type = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_FRAME]);
2064         sl->device_override = nla_get_u8(attrs[NL802154_SECLEVEL_ATTR_DEV_OVERRIDE]);
2065         if (sl->frame_type > NL802154_FRAME_MAX ||
2066             (sl->device_override != 0 && sl->device_override != 1))
2067                 return -EINVAL;
2068 
2069         if (sl->frame_type == NL802154_FRAME_CMD) {
2070                 if (!attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME])
2071                         return -EINVAL;
2072 
2073                 sl->cmd_frame_id = nla_get_u32(attrs[NL802154_SECLEVEL_ATTR_CMD_FRAME]);
2074                 if (sl->cmd_frame_id > NL802154_CMD_FRAME_MAX)
2075                         return -EINVAL;
2076         }
2077 
2078         return 0;
2079 }
2080 
2081 static int nl802154_add_llsec_seclevel(struct sk_buff *skb,
2082                                        struct genl_info *info)
2083 {
2084         struct cfg802154_registered_device *rdev = info->user_ptr[0];
2085         struct net_device *dev = info->user_ptr[1];
2086         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2087         struct ieee802154_llsec_seclevel sl;
2088 
2089         if (llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2090                                  &sl) < 0)
2091                 return -EINVAL;
2092 
2093         return rdev_add_seclevel(rdev, wpan_dev, &sl);
2094 }
2095 
2096 static int nl802154_del_llsec_seclevel(struct sk_buff *skb,
2097                                        struct genl_info *info)
2098 {
2099         struct cfg802154_registered_device *rdev = info->user_ptr[0];
2100         struct net_device *dev = info->user_ptr[1];
2101         struct wpan_dev *wpan_dev = dev->ieee802154_ptr;
2102         struct ieee802154_llsec_seclevel sl;
2103 
2104         if (!info->attrs[NL802154_ATTR_SEC_LEVEL] ||
2105             llsec_parse_seclevel(info->attrs[NL802154_ATTR_SEC_LEVEL],
2106                                  &sl) < 0)
2107                 return -EINVAL;
2108 
2109         return rdev_del_seclevel(rdev, wpan_dev, &sl);
2110 }
2111 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2112 
2113 #define NL802154_FLAG_NEED_WPAN_PHY     0x01
2114 #define NL802154_FLAG_NEED_NETDEV       0x02
2115 #define NL802154_FLAG_NEED_RTNL         0x04
2116 #define NL802154_FLAG_CHECK_NETDEV_UP   0x08
2117 #define NL802154_FLAG_NEED_NETDEV_UP    (NL802154_FLAG_NEED_NETDEV |\
2118                                          NL802154_FLAG_CHECK_NETDEV_UP)
2119 #define NL802154_FLAG_NEED_WPAN_DEV     0x10
2120 #define NL802154_FLAG_NEED_WPAN_DEV_UP  (NL802154_FLAG_NEED_WPAN_DEV |\
2121                                          NL802154_FLAG_CHECK_NETDEV_UP)
2122 
2123 static int nl802154_pre_doit(const struct genl_ops *ops, struct sk_buff *skb,
2124                              struct genl_info *info)
2125 {
2126         struct cfg802154_registered_device *rdev;
2127         struct wpan_dev *wpan_dev;
2128         struct net_device *dev;
2129         bool rtnl = ops->internal_flags & NL802154_FLAG_NEED_RTNL;
2130 
2131         if (rtnl)
2132                 rtnl_lock();
2133 
2134         if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_PHY) {
2135                 rdev = cfg802154_get_dev_from_info(genl_info_net(info), info);
2136                 if (IS_ERR(rdev)) {
2137                         if (rtnl)
2138                                 rtnl_unlock();
2139                         return PTR_ERR(rdev);
2140                 }
2141                 info->user_ptr[0] = rdev;
2142         } else if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV ||
2143                    ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2144                 ASSERT_RTNL();
2145                 wpan_dev = __cfg802154_wpan_dev_from_attrs(genl_info_net(info),
2146                                                            info->attrs);
2147                 if (IS_ERR(wpan_dev)) {
2148                         if (rtnl)
2149                                 rtnl_unlock();
2150                         return PTR_ERR(wpan_dev);
2151                 }
2152 
2153                 dev = wpan_dev->netdev;
2154                 rdev = wpan_phy_to_rdev(wpan_dev->wpan_phy);
2155 
2156                 if (ops->internal_flags & NL802154_FLAG_NEED_NETDEV) {
2157                         if (!dev) {
2158                                 if (rtnl)
2159                                         rtnl_unlock();
2160                                 return -EINVAL;
2161                         }
2162 
2163                         info->user_ptr[1] = dev;
2164                 } else {
2165                         info->user_ptr[1] = wpan_dev;
2166                 }
2167 
2168                 if (dev) {
2169                         if (ops->internal_flags & NL802154_FLAG_CHECK_NETDEV_UP &&
2170                             !netif_running(dev)) {
2171                                 if (rtnl)
2172                                         rtnl_unlock();
2173                                 return -ENETDOWN;
2174                         }
2175 
2176                         dev_hold(dev);
2177                 }
2178 
2179                 info->user_ptr[0] = rdev;
2180         }
2181 
2182         return 0;
2183 }
2184 
2185 static void nl802154_post_doit(const struct genl_ops *ops, struct sk_buff *skb,
2186                                struct genl_info *info)
2187 {
2188         if (info->user_ptr[1]) {
2189                 if (ops->internal_flags & NL802154_FLAG_NEED_WPAN_DEV) {
2190                         struct wpan_dev *wpan_dev = info->user_ptr[1];
2191 
2192                         if (wpan_dev->netdev)
2193                                 dev_put(wpan_dev->netdev);
2194                 } else {
2195                         dev_put(info->user_ptr[1]);
2196                 }
2197         }
2198 
2199         if (ops->internal_flags & NL802154_FLAG_NEED_RTNL)
2200                 rtnl_unlock();
2201 }
2202 
2203 static const struct genl_ops nl802154_ops[] = {
2204         {
2205                 .cmd = NL802154_CMD_GET_WPAN_PHY,
2206                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2207                 .doit = nl802154_get_wpan_phy,
2208                 .dumpit = nl802154_dump_wpan_phy,
2209                 .done = nl802154_dump_wpan_phy_done,
2210                 /* can be retrieved by unprivileged users */
2211                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2212                                   NL802154_FLAG_NEED_RTNL,
2213         },
2214         {
2215                 .cmd = NL802154_CMD_GET_INTERFACE,
2216                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2217                 .doit = nl802154_get_interface,
2218                 .dumpit = nl802154_dump_interface,
2219                 /* can be retrieved by unprivileged users */
2220                 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2221                                   NL802154_FLAG_NEED_RTNL,
2222         },
2223         {
2224                 .cmd = NL802154_CMD_NEW_INTERFACE,
2225                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2226                 .doit = nl802154_new_interface,
2227                 .flags = GENL_ADMIN_PERM,
2228                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2229                                   NL802154_FLAG_NEED_RTNL,
2230         },
2231         {
2232                 .cmd = NL802154_CMD_DEL_INTERFACE,
2233                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2234                 .doit = nl802154_del_interface,
2235                 .flags = GENL_ADMIN_PERM,
2236                 .internal_flags = NL802154_FLAG_NEED_WPAN_DEV |
2237                                   NL802154_FLAG_NEED_RTNL,
2238         },
2239         {
2240                 .cmd = NL802154_CMD_SET_CHANNEL,
2241                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2242                 .doit = nl802154_set_channel,
2243                 .flags = GENL_ADMIN_PERM,
2244                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2245                                   NL802154_FLAG_NEED_RTNL,
2246         },
2247         {
2248                 .cmd = NL802154_CMD_SET_CCA_MODE,
2249                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2250                 .doit = nl802154_set_cca_mode,
2251                 .flags = GENL_ADMIN_PERM,
2252                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2253                                   NL802154_FLAG_NEED_RTNL,
2254         },
2255         {
2256                 .cmd = NL802154_CMD_SET_CCA_ED_LEVEL,
2257                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2258                 .doit = nl802154_set_cca_ed_level,
2259                 .flags = GENL_ADMIN_PERM,
2260                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2261                                   NL802154_FLAG_NEED_RTNL,
2262         },
2263         {
2264                 .cmd = NL802154_CMD_SET_TX_POWER,
2265                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2266                 .doit = nl802154_set_tx_power,
2267                 .flags = GENL_ADMIN_PERM,
2268                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2269                                   NL802154_FLAG_NEED_RTNL,
2270         },
2271         {
2272                 .cmd = NL802154_CMD_SET_WPAN_PHY_NETNS,
2273                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2274                 .doit = nl802154_wpan_phy_netns,
2275                 .flags = GENL_ADMIN_PERM,
2276                 .internal_flags = NL802154_FLAG_NEED_WPAN_PHY |
2277                                   NL802154_FLAG_NEED_RTNL,
2278         },
2279         {
2280                 .cmd = NL802154_CMD_SET_PAN_ID,
2281                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2282                 .doit = nl802154_set_pan_id,
2283                 .flags = GENL_ADMIN_PERM,
2284                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2285                                   NL802154_FLAG_NEED_RTNL,
2286         },
2287         {
2288                 .cmd = NL802154_CMD_SET_SHORT_ADDR,
2289                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2290                 .doit = nl802154_set_short_addr,
2291                 .flags = GENL_ADMIN_PERM,
2292                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2293                                   NL802154_FLAG_NEED_RTNL,
2294         },
2295         {
2296                 .cmd = NL802154_CMD_SET_BACKOFF_EXPONENT,
2297                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2298                 .doit = nl802154_set_backoff_exponent,
2299                 .flags = GENL_ADMIN_PERM,
2300                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2301                                   NL802154_FLAG_NEED_RTNL,
2302         },
2303         {
2304                 .cmd = NL802154_CMD_SET_MAX_CSMA_BACKOFFS,
2305                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2306                 .doit = nl802154_set_max_csma_backoffs,
2307                 .flags = GENL_ADMIN_PERM,
2308                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2309                                   NL802154_FLAG_NEED_RTNL,
2310         },
2311         {
2312                 .cmd = NL802154_CMD_SET_MAX_FRAME_RETRIES,
2313                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2314                 .doit = nl802154_set_max_frame_retries,
2315                 .flags = GENL_ADMIN_PERM,
2316                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2317                                   NL802154_FLAG_NEED_RTNL,
2318         },
2319         {
2320                 .cmd = NL802154_CMD_SET_LBT_MODE,
2321                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2322                 .doit = nl802154_set_lbt_mode,
2323                 .flags = GENL_ADMIN_PERM,
2324                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2325                                   NL802154_FLAG_NEED_RTNL,
2326         },
2327         {
2328                 .cmd = NL802154_CMD_SET_ACKREQ_DEFAULT,
2329                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2330                 .doit = nl802154_set_ackreq_default,
2331                 .flags = GENL_ADMIN_PERM,
2332                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2333                                   NL802154_FLAG_NEED_RTNL,
2334         },
2335 #ifdef CONFIG_IEEE802154_NL802154_EXPERIMENTAL
2336         {
2337                 .cmd = NL802154_CMD_SET_SEC_PARAMS,
2338                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2339                 .doit = nl802154_set_llsec_params,
2340                 .flags = GENL_ADMIN_PERM,
2341                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2342                                   NL802154_FLAG_NEED_RTNL,
2343         },
2344         {
2345                 .cmd = NL802154_CMD_GET_SEC_KEY,
2346                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2347                 /* TODO .doit by matching key id? */
2348                 .dumpit = nl802154_dump_llsec_key,
2349                 .flags = GENL_ADMIN_PERM,
2350                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2351                                   NL802154_FLAG_NEED_RTNL,
2352         },
2353         {
2354                 .cmd = NL802154_CMD_NEW_SEC_KEY,
2355                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2356                 .doit = nl802154_add_llsec_key,
2357                 .flags = GENL_ADMIN_PERM,
2358                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2359                                   NL802154_FLAG_NEED_RTNL,
2360         },
2361         {
2362                 .cmd = NL802154_CMD_DEL_SEC_KEY,
2363                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2364                 .doit = nl802154_del_llsec_key,
2365                 .flags = GENL_ADMIN_PERM,
2366                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2367                                   NL802154_FLAG_NEED_RTNL,
2368         },
2369         /* TODO unique identifier must short+pan OR extended_addr */
2370         {
2371                 .cmd = NL802154_CMD_GET_SEC_DEV,
2372                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2373                 /* TODO .doit by matching extended_addr? */
2374                 .dumpit = nl802154_dump_llsec_dev,
2375                 .flags = GENL_ADMIN_PERM,
2376                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2377                                   NL802154_FLAG_NEED_RTNL,
2378         },
2379         {
2380                 .cmd = NL802154_CMD_NEW_SEC_DEV,
2381                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2382                 .doit = nl802154_add_llsec_dev,
2383                 .flags = GENL_ADMIN_PERM,
2384                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2385                                   NL802154_FLAG_NEED_RTNL,
2386         },
2387         {
2388                 .cmd = NL802154_CMD_DEL_SEC_DEV,
2389                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2390                 .doit = nl802154_del_llsec_dev,
2391                 .flags = GENL_ADMIN_PERM,
2392                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2393                                   NL802154_FLAG_NEED_RTNL,
2394         },
2395         /* TODO remove complete devkey, put it as nested? */
2396         {
2397                 .cmd = NL802154_CMD_GET_SEC_DEVKEY,
2398                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2399                 /* TODO doit by matching ??? */
2400                 .dumpit = nl802154_dump_llsec_devkey,
2401                 .flags = GENL_ADMIN_PERM,
2402                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2403                                   NL802154_FLAG_NEED_RTNL,
2404         },
2405         {
2406                 .cmd = NL802154_CMD_NEW_SEC_DEVKEY,
2407                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2408                 .doit = nl802154_add_llsec_devkey,
2409                 .flags = GENL_ADMIN_PERM,
2410                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2411                                   NL802154_FLAG_NEED_RTNL,
2412         },
2413         {
2414                 .cmd = NL802154_CMD_DEL_SEC_DEVKEY,
2415                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2416                 .doit = nl802154_del_llsec_devkey,
2417                 .flags = GENL_ADMIN_PERM,
2418                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2419                                   NL802154_FLAG_NEED_RTNL,
2420         },
2421         {
2422                 .cmd = NL802154_CMD_GET_SEC_LEVEL,
2423                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2424                 /* TODO .doit by matching frame_type? */
2425                 .dumpit = nl802154_dump_llsec_seclevel,
2426                 .flags = GENL_ADMIN_PERM,
2427                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2428                                   NL802154_FLAG_NEED_RTNL,
2429         },
2430         {
2431                 .cmd = NL802154_CMD_NEW_SEC_LEVEL,
2432                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2433                 .doit = nl802154_add_llsec_seclevel,
2434                 .flags = GENL_ADMIN_PERM,
2435                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2436                                   NL802154_FLAG_NEED_RTNL,
2437         },
2438         {
2439                 .cmd = NL802154_CMD_DEL_SEC_LEVEL,
2440                 .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
2441                 /* TODO match frame_type only? */
2442                 .doit = nl802154_del_llsec_seclevel,
2443                 .flags = GENL_ADMIN_PERM,
2444                 .internal_flags = NL802154_FLAG_NEED_NETDEV |
2445                                   NL802154_FLAG_NEED_RTNL,
2446         },
2447 #endif /* CONFIG_IEEE802154_NL802154_EXPERIMENTAL */
2448 };
2449 
2450 static struct genl_family nl802154_fam __ro_after_init = {
2451         .name = NL802154_GENL_NAME,     /* have users key off the name instead */
2452         .hdrsize = 0,                   /* no private header */
2453         .version = 1,                   /* no particular meaning now */
2454         .maxattr = NL802154_ATTR_MAX,
2455         .policy = nl802154_policy,
2456         .netnsok = true,
2457         .pre_doit = nl802154_pre_doit,
2458         .post_doit = nl802154_post_doit,
2459         .module = THIS_MODULE,
2460         .ops = nl802154_ops,
2461         .n_ops = ARRAY_SIZE(nl802154_ops),
2462         .mcgrps = nl802154_mcgrps,
2463         .n_mcgrps = ARRAY_SIZE(nl802154_mcgrps),
2464 };
2465 
2466 /* initialisation/exit functions */
2467 int __init nl802154_init(void)
2468 {
2469         return genl_register_family(&nl802154_fam);
2470 }
2471 
2472 void nl802154_exit(void)
2473 {
2474         genl_unregister_family(&nl802154_fam);
2475 }

/* [<][>][^][v][top][bottom][index][help] */