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