| Dmitry Eremin-Solenikov | 1eaa9d0 | 2009-09-15 17:04:44 +0400 | [diff] [blame] | 1 | /* | 
|  | 2 | * Netlink inteface for IEEE 802.15.4 stack | 
|  | 3 | * | 
|  | 4 | * Copyright 2007, 2008 Siemens AG | 
|  | 5 | * | 
|  | 6 | * This program is free software; you can redistribute it and/or modify | 
|  | 7 | * it under the terms of the GNU General Public License version 2 | 
|  | 8 | * as published by the Free Software Foundation. | 
|  | 9 | * | 
|  | 10 | * This program is distributed in the hope that it will be useful, | 
|  | 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|  | 13 | * GNU General Public License for more details. | 
|  | 14 | * | 
|  | 15 | * You should have received a copy of the GNU General Public License along | 
|  | 16 | * with this program; if not, write to the Free Software Foundation, Inc., | 
|  | 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | 
|  | 18 | * | 
|  | 19 | * Written by: | 
|  | 20 | * Sergey Lapin <slapin@ossfans.org> | 
|  | 21 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> | 
|  | 22 | * Maxim Osipov <maxim.osipov@siemens.com> | 
|  | 23 | */ | 
|  | 24 |  | 
|  | 25 | #include <linux/kernel.h> | 
|  | 26 | #include <net/netlink.h> | 
|  | 27 | #include <net/genetlink.h> | 
|  | 28 | #include <net/wpan-phy.h> | 
| Dmitry Eremin-Solenikov | bb1cafb | 2009-11-05 16:56:23 +0300 | [diff] [blame] | 29 | #include <net/af_ieee802154.h> | 
|  | 30 | #include <net/ieee802154_netdev.h> | 
|  | 31 | #include <net/rtnetlink.h> /* for rtnl_{un,}lock */ | 
| Dmitry Eremin-Solenikov | 1eaa9d0 | 2009-09-15 17:04:44 +0400 | [diff] [blame] | 32 | #include <linux/nl802154.h> | 
|  | 33 |  | 
|  | 34 | #include "ieee802154.h" | 
|  | 35 |  | 
|  | 36 | static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 pid, | 
|  | 37 | u32 seq, int flags, struct wpan_phy *phy) | 
|  | 38 | { | 
|  | 39 | void *hdr; | 
|  | 40 | int i, pages = 0; | 
|  | 41 | uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL); | 
|  | 42 |  | 
|  | 43 | pr_debug("%s\n", __func__); | 
|  | 44 |  | 
|  | 45 | if (!buf) | 
|  | 46 | goto out; | 
|  | 47 |  | 
|  | 48 | hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags, | 
|  | 49 | IEEE802154_LIST_PHY); | 
|  | 50 | if (!hdr) | 
|  | 51 | goto out; | 
|  | 52 |  | 
|  | 53 | mutex_lock(&phy->pib_lock); | 
|  | 54 | NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)); | 
|  | 55 |  | 
|  | 56 | NLA_PUT_U8(msg, IEEE802154_ATTR_PAGE, phy->current_page); | 
|  | 57 | NLA_PUT_U8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel); | 
|  | 58 | for (i = 0; i < 32; i++) { | 
|  | 59 | if (phy->channels_supported[i]) | 
|  | 60 | buf[pages++] = phy->channels_supported[i] | (i << 27); | 
|  | 61 | } | 
|  | 62 | if (pages) | 
|  | 63 | NLA_PUT(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST, | 
|  | 64 | pages * sizeof(uint32_t), buf); | 
|  | 65 |  | 
|  | 66 | mutex_unlock(&phy->pib_lock); | 
|  | 67 | return genlmsg_end(msg, hdr); | 
|  | 68 |  | 
|  | 69 | nla_put_failure: | 
|  | 70 | mutex_unlock(&phy->pib_lock); | 
|  | 71 | genlmsg_cancel(msg, hdr); | 
|  | 72 | out: | 
|  | 73 | kfree(buf); | 
|  | 74 | return -EMSGSIZE; | 
|  | 75 | } | 
|  | 76 |  | 
|  | 77 | static int ieee802154_list_phy(struct sk_buff *skb, | 
|  | 78 | struct genl_info *info) | 
|  | 79 | { | 
|  | 80 | /* Request for interface name, index, type, IEEE address, | 
|  | 81 | PAN Id, short address */ | 
|  | 82 | struct sk_buff *msg; | 
|  | 83 | struct wpan_phy *phy; | 
|  | 84 | const char *name; | 
|  | 85 | int rc = -ENOBUFS; | 
|  | 86 |  | 
|  | 87 | pr_debug("%s\n", __func__); | 
|  | 88 |  | 
|  | 89 | if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) | 
|  | 90 | return -EINVAL; | 
|  | 91 |  | 
|  | 92 | name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); | 
|  | 93 | if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') | 
|  | 94 | return -EINVAL; /* phy name should be null-terminated */ | 
|  | 95 |  | 
|  | 96 |  | 
|  | 97 | phy = wpan_phy_find(name); | 
|  | 98 | if (!phy) | 
|  | 99 | return -ENODEV; | 
|  | 100 |  | 
|  | 101 | msg = nlmsg_new(NLMSG_GOODSIZE, GFP_KERNEL); | 
|  | 102 | if (!msg) | 
|  | 103 | goto out_dev; | 
|  | 104 |  | 
|  | 105 | rc = ieee802154_nl_fill_phy(msg, info->snd_pid, info->snd_seq, | 
|  | 106 | 0, phy); | 
|  | 107 | if (rc < 0) | 
|  | 108 | goto out_free; | 
|  | 109 |  | 
|  | 110 | wpan_phy_put(phy); | 
|  | 111 |  | 
|  | 112 | return genlmsg_reply(msg, info); | 
|  | 113 | out_free: | 
|  | 114 | nlmsg_free(msg); | 
|  | 115 | out_dev: | 
|  | 116 | wpan_phy_put(phy); | 
|  | 117 | return rc; | 
|  | 118 |  | 
|  | 119 | } | 
|  | 120 |  | 
|  | 121 | struct dump_phy_data { | 
|  | 122 | struct sk_buff *skb; | 
|  | 123 | struct netlink_callback *cb; | 
|  | 124 | int idx, s_idx; | 
|  | 125 | }; | 
|  | 126 |  | 
|  | 127 | static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data) | 
|  | 128 | { | 
|  | 129 | int rc; | 
|  | 130 | struct dump_phy_data *data = _data; | 
|  | 131 |  | 
|  | 132 | pr_debug("%s\n", __func__); | 
|  | 133 |  | 
|  | 134 | if (data->idx++ < data->s_idx) | 
|  | 135 | return 0; | 
|  | 136 |  | 
|  | 137 | rc = ieee802154_nl_fill_phy(data->skb, | 
|  | 138 | NETLINK_CB(data->cb->skb).pid, | 
|  | 139 | data->cb->nlh->nlmsg_seq, | 
|  | 140 | NLM_F_MULTI, | 
|  | 141 | phy); | 
|  | 142 |  | 
|  | 143 | if (rc < 0) { | 
|  | 144 | data->idx--; | 
|  | 145 | return rc; | 
|  | 146 | } | 
|  | 147 |  | 
|  | 148 | return 0; | 
|  | 149 | } | 
|  | 150 |  | 
|  | 151 | static int ieee802154_dump_phy(struct sk_buff *skb, | 
|  | 152 | struct netlink_callback *cb) | 
|  | 153 | { | 
|  | 154 | struct dump_phy_data data = { | 
|  | 155 | .cb = cb, | 
|  | 156 | .skb = skb, | 
|  | 157 | .s_idx = cb->args[0], | 
|  | 158 | .idx = 0, | 
|  | 159 | }; | 
|  | 160 |  | 
|  | 161 | pr_debug("%s\n", __func__); | 
|  | 162 |  | 
|  | 163 | wpan_phy_for_each(ieee802154_dump_phy_iter, &data); | 
|  | 164 |  | 
|  | 165 | cb->args[0] = data.idx; | 
|  | 166 |  | 
|  | 167 | return skb->len; | 
|  | 168 | } | 
|  | 169 |  | 
| Dmitry Eremin-Solenikov | bb1cafb | 2009-11-05 16:56:23 +0300 | [diff] [blame] | 170 | static int ieee802154_add_iface(struct sk_buff *skb, | 
|  | 171 | struct genl_info *info) | 
|  | 172 | { | 
|  | 173 | struct sk_buff *msg; | 
|  | 174 | struct wpan_phy *phy; | 
|  | 175 | const char *name; | 
|  | 176 | const char *devname; | 
|  | 177 | int rc = -ENOBUFS; | 
|  | 178 | struct net_device *dev; | 
|  | 179 |  | 
|  | 180 | pr_debug("%s\n", __func__); | 
|  | 181 |  | 
|  | 182 | if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) | 
|  | 183 | return -EINVAL; | 
|  | 184 |  | 
|  | 185 | name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); | 
|  | 186 | if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') | 
|  | 187 | return -EINVAL; /* phy name should be null-terminated */ | 
|  | 188 |  | 
|  | 189 | if (info->attrs[IEEE802154_ATTR_DEV_NAME]) { | 
|  | 190 | devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]); | 
|  | 191 | if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] | 
|  | 192 | != '\0') | 
|  | 193 | return -EINVAL; /* phy name should be null-terminated */ | 
|  | 194 | } else  { | 
|  | 195 | devname = "wpan%d"; | 
|  | 196 | } | 
|  | 197 |  | 
|  | 198 | if (strlen(devname) >= IFNAMSIZ) | 
|  | 199 | return -ENAMETOOLONG; | 
|  | 200 |  | 
|  | 201 | phy = wpan_phy_find(name); | 
|  | 202 | if (!phy) | 
|  | 203 | return -ENODEV; | 
|  | 204 |  | 
|  | 205 | msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE); | 
|  | 206 | if (!msg) | 
|  | 207 | goto out_dev; | 
|  | 208 |  | 
|  | 209 | if (!phy->add_iface) { | 
|  | 210 | rc = -EINVAL; | 
|  | 211 | goto nla_put_failure; | 
|  | 212 | } | 
|  | 213 |  | 
|  | 214 | dev = phy->add_iface(phy, devname); | 
|  | 215 | if (IS_ERR(dev)) { | 
|  | 216 | rc = PTR_ERR(dev); | 
|  | 217 | goto nla_put_failure; | 
|  | 218 | } | 
|  | 219 |  | 
|  | 220 | NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)); | 
|  | 221 | NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, dev->name); | 
|  | 222 |  | 
|  | 223 | dev_put(dev); | 
|  | 224 |  | 
|  | 225 | wpan_phy_put(phy); | 
|  | 226 |  | 
|  | 227 | return ieee802154_nl_reply(msg, info); | 
|  | 228 |  | 
|  | 229 | nla_put_failure: | 
|  | 230 | nlmsg_free(msg); | 
|  | 231 | out_dev: | 
|  | 232 | wpan_phy_put(phy); | 
|  | 233 | return rc; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | static int ieee802154_del_iface(struct sk_buff *skb, | 
|  | 237 | struct genl_info *info) | 
|  | 238 | { | 
|  | 239 | struct sk_buff *msg; | 
|  | 240 | struct wpan_phy *phy; | 
|  | 241 | const char *name; | 
|  | 242 | int rc; | 
|  | 243 | struct net_device *dev; | 
|  | 244 |  | 
|  | 245 | pr_debug("%s\n", __func__); | 
|  | 246 |  | 
|  | 247 | if (!info->attrs[IEEE802154_ATTR_DEV_NAME]) | 
|  | 248 | return -EINVAL; | 
|  | 249 |  | 
|  | 250 | name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]); | 
|  | 251 | if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0') | 
|  | 252 | return -EINVAL; /* name should be null-terminated */ | 
|  | 253 |  | 
|  | 254 | dev = dev_get_by_name(genl_info_net(info), name); | 
|  | 255 | if (!dev) | 
|  | 256 | return -ENODEV; | 
|  | 257 |  | 
|  | 258 | phy = ieee802154_mlme_ops(dev)->get_phy(dev); | 
|  | 259 | BUG_ON(!phy); | 
|  | 260 |  | 
|  | 261 | rc = -EINVAL; | 
|  | 262 | /* phy name is optional, but should be checked if it's given */ | 
|  | 263 | if (info->attrs[IEEE802154_ATTR_PHY_NAME]) { | 
|  | 264 | struct wpan_phy *phy2; | 
|  | 265 |  | 
|  | 266 | const char *pname = | 
|  | 267 | nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); | 
|  | 268 | if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] | 
|  | 269 | != '\0') | 
|  | 270 | /* name should be null-terminated */ | 
|  | 271 | goto out_dev; | 
|  | 272 |  | 
|  | 273 | phy2 = wpan_phy_find(pname); | 
|  | 274 | if (!phy2) | 
|  | 275 | goto out_dev; | 
|  | 276 |  | 
|  | 277 | if (phy != phy2) { | 
|  | 278 | wpan_phy_put(phy2); | 
|  | 279 | goto out_dev; | 
|  | 280 | } | 
|  | 281 | } | 
|  | 282 |  | 
|  | 283 | rc = -ENOBUFS; | 
|  | 284 |  | 
|  | 285 | msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE); | 
|  | 286 | if (!msg) | 
|  | 287 | goto out_dev; | 
|  | 288 |  | 
|  | 289 | if (!phy->del_iface) { | 
|  | 290 | rc = -EINVAL; | 
|  | 291 | goto nla_put_failure; | 
|  | 292 | } | 
|  | 293 |  | 
|  | 294 | rtnl_lock(); | 
|  | 295 | phy->del_iface(phy, dev); | 
|  | 296 |  | 
|  | 297 | /* We don't have device anymore */ | 
|  | 298 | dev_put(dev); | 
|  | 299 | dev = NULL; | 
|  | 300 |  | 
|  | 301 | rtnl_unlock(); | 
|  | 302 |  | 
|  | 303 |  | 
|  | 304 | NLA_PUT_STRING(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)); | 
|  | 305 | NLA_PUT_STRING(msg, IEEE802154_ATTR_DEV_NAME, name); | 
|  | 306 |  | 
|  | 307 | wpan_phy_put(phy); | 
|  | 308 |  | 
|  | 309 | return ieee802154_nl_reply(msg, info); | 
|  | 310 |  | 
|  | 311 | nla_put_failure: | 
|  | 312 | nlmsg_free(msg); | 
|  | 313 | out_dev: | 
|  | 314 | wpan_phy_put(phy); | 
|  | 315 | if (dev) | 
|  | 316 | dev_put(dev); | 
|  | 317 |  | 
|  | 318 | return rc; | 
|  | 319 | } | 
|  | 320 |  | 
| Dmitry Eremin-Solenikov | 1eaa9d0 | 2009-09-15 17:04:44 +0400 | [diff] [blame] | 321 | static struct genl_ops ieee802154_phy_ops[] = { | 
|  | 322 | IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy, | 
|  | 323 | ieee802154_dump_phy), | 
| Dmitry Eremin-Solenikov | bb1cafb | 2009-11-05 16:56:23 +0300 | [diff] [blame] | 324 | IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface), | 
|  | 325 | IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface), | 
| Dmitry Eremin-Solenikov | 1eaa9d0 | 2009-09-15 17:04:44 +0400 | [diff] [blame] | 326 | }; | 
|  | 327 |  | 
|  | 328 | /* | 
|  | 329 | * No need to unregister as family unregistration will do it. | 
|  | 330 | */ | 
|  | 331 | int nl802154_phy_register(void) | 
|  | 332 | { | 
|  | 333 | int i; | 
|  | 334 | int rc; | 
|  | 335 |  | 
|  | 336 | for (i = 0; i < ARRAY_SIZE(ieee802154_phy_ops); i++) { | 
|  | 337 | rc = genl_register_ops(&nl802154_family, | 
|  | 338 | &ieee802154_phy_ops[i]); | 
|  | 339 | if (rc) | 
|  | 340 | return rc; | 
|  | 341 | } | 
|  | 342 |  | 
|  | 343 | return 0; | 
|  | 344 | } |