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 * Written by: 16 * Sergey Lapin <slapin@ossfans.org> 17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> 18 * Maxim Osipov <maxim.osipov@siemens.com> 19 */ 20 21 #include <linux/kernel.h> 22 #include <linux/slab.h> 23 #include <linux/if_arp.h> 24 #include <net/netlink.h> 25 #include <net/genetlink.h> 26 #include <net/cfg802154.h> 27 #include <net/af_ieee802154.h> 28 #include <net/ieee802154_netdev.h> 29 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */ 30 #include <linux/nl802154.h> 31 32 #include "ieee802154.h" 33 34 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid, 35 u32 seq, int flags, struct wpan_phy *phy) 36 { 37 void *hdr; 38 int i, pages = 0; 39 uint32_t *buf = kzalloc(32 * sizeof(uint32_t), GFP_KERNEL); 40 41 pr_debug("%s\n", __func__); 42 43 if (!buf) 44 return -EMSGSIZE; 45 46 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags, 47 IEEE802154_LIST_PHY); 48 if (!hdr) 49 goto out; 50 51 mutex_lock(&phy->pib_lock); 52 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || 53 nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) || 54 nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel)) 55 goto nla_put_failure; 56 for (i = 0; i < 32; i++) { 57 if (phy->channels_supported[i]) 58 buf[pages++] = phy->channels_supported[i] | (i << 27); 59 } 60 if (pages && 61 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST, 62 pages * sizeof(uint32_t), buf)) 63 goto nla_put_failure; 64 mutex_unlock(&phy->pib_lock); 65 kfree(buf); 66 return genlmsg_end(msg, hdr); 67 68 nla_put_failure: 69 mutex_unlock(&phy->pib_lock); 70 genlmsg_cancel(msg, hdr); 71 out: 72 kfree(buf); 73 return -EMSGSIZE; 74 } 75 76 int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info) 77 { 78 /* Request for interface name, index, type, IEEE address, 79 * PAN Id, short address 80 */ 81 struct sk_buff *msg; 82 struct wpan_phy *phy; 83 const char *name; 84 int rc = -ENOBUFS; 85 86 pr_debug("%s\n", __func__); 87 88 if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) 89 return -EINVAL; 90 91 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); 92 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') 93 return -EINVAL; /* phy name should be null-terminated */ 94 95 96 phy = wpan_phy_find(name); 97 if (!phy) 98 return -ENODEV; 99 100 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); 101 if (!msg) 102 goto out_dev; 103 104 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq, 105 0, phy); 106 if (rc < 0) 107 goto out_free; 108 109 wpan_phy_put(phy); 110 111 return genlmsg_reply(msg, info); 112 out_free: 113 nlmsg_free(msg); 114 out_dev: 115 wpan_phy_put(phy); 116 return rc; 117 } 118 119 struct dump_phy_data { 120 struct sk_buff *skb; 121 struct netlink_callback *cb; 122 int idx, s_idx; 123 }; 124 125 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data) 126 { 127 int rc; 128 struct dump_phy_data *data = _data; 129 130 pr_debug("%s\n", __func__); 131 132 if (data->idx++ < data->s_idx) 133 return 0; 134 135 rc = ieee802154_nl_fill_phy(data->skb, 136 NETLINK_CB(data->cb->skb).portid, 137 data->cb->nlh->nlmsg_seq, 138 NLM_F_MULTI, 139 phy); 140 141 if (rc < 0) { 142 data->idx--; 143 return rc; 144 } 145 146 return 0; 147 } 148 149 int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb) 150 { 151 struct dump_phy_data data = { 152 .cb = cb, 153 .skb = skb, 154 .s_idx = cb->args[0], 155 .idx = 0, 156 }; 157 158 pr_debug("%s\n", __func__); 159 160 wpan_phy_for_each(ieee802154_dump_phy_iter, &data); 161 162 cb->args[0] = data.idx; 163 164 return skb->len; 165 } 166 167 int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info) 168 { 169 struct sk_buff *msg; 170 struct wpan_phy *phy; 171 const char *name; 172 const char *devname; 173 int rc = -ENOBUFS; 174 struct net_device *dev; 175 int type = __IEEE802154_DEV_INVALID; 176 177 pr_debug("%s\n", __func__); 178 179 if (!info->attrs[IEEE802154_ATTR_PHY_NAME]) 180 return -EINVAL; 181 182 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); 183 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0') 184 return -EINVAL; /* phy name should be null-terminated */ 185 186 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) { 187 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]); 188 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] 189 != '\0') 190 return -EINVAL; /* phy name should be null-terminated */ 191 } else { 192 devname = "wpan%d"; 193 } 194 195 if (strlen(devname) >= IFNAMSIZ) 196 return -ENAMETOOLONG; 197 198 phy = wpan_phy_find(name); 199 if (!phy) 200 return -ENODEV; 201 202 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE); 203 if (!msg) 204 goto out_dev; 205 206 if (!phy->add_iface) { 207 rc = -EINVAL; 208 goto nla_put_failure; 209 } 210 211 if (info->attrs[IEEE802154_ATTR_HW_ADDR] && 212 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) != 213 IEEE802154_ADDR_LEN) { 214 rc = -EINVAL; 215 goto nla_put_failure; 216 } 217 218 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) { 219 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]); 220 if (type >= __IEEE802154_DEV_MAX) { 221 rc = -EINVAL; 222 goto nla_put_failure; 223 } 224 } 225 226 dev = phy->add_iface(phy, devname, type); 227 if (IS_ERR(dev)) { 228 rc = PTR_ERR(dev); 229 goto nla_put_failure; 230 } 231 232 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) { 233 struct sockaddr addr; 234 235 addr.sa_family = ARPHRD_IEEE802154; 236 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR], 237 IEEE802154_ADDR_LEN); 238 239 /* strangely enough, some callbacks (inetdev_event) from 240 * dev_set_mac_address require RTNL_LOCK 241 */ 242 rtnl_lock(); 243 rc = dev_set_mac_address(dev, &addr); 244 rtnl_unlock(); 245 if (rc) 246 goto dev_unregister; 247 } 248 249 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || 250 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name)) 251 goto nla_put_failure; 252 dev_put(dev); 253 254 wpan_phy_put(phy); 255 256 return ieee802154_nl_reply(msg, info); 257 258 dev_unregister: 259 rtnl_lock(); /* del_iface must be called with RTNL lock */ 260 phy->del_iface(phy, dev); 261 dev_put(dev); 262 rtnl_unlock(); 263 nla_put_failure: 264 nlmsg_free(msg); 265 out_dev: 266 wpan_phy_put(phy); 267 return rc; 268 } 269 270 int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info) 271 { 272 struct sk_buff *msg; 273 struct wpan_phy *phy; 274 const char *name; 275 int rc; 276 struct net_device *dev; 277 278 pr_debug("%s\n", __func__); 279 280 if (!info->attrs[IEEE802154_ATTR_DEV_NAME]) 281 return -EINVAL; 282 283 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]); 284 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0') 285 return -EINVAL; /* name should be null-terminated */ 286 287 dev = dev_get_by_name(genl_info_net(info), name); 288 if (!dev) 289 return -ENODEV; 290 291 phy = ieee802154_mlme_ops(dev)->get_phy(dev); 292 BUG_ON(!phy); 293 294 rc = -EINVAL; 295 /* phy name is optional, but should be checked if it's given */ 296 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) { 297 struct wpan_phy *phy2; 298 299 const char *pname = 300 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]); 301 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] 302 != '\0') 303 /* name should be null-terminated */ 304 goto out_dev; 305 306 phy2 = wpan_phy_find(pname); 307 if (!phy2) 308 goto out_dev; 309 310 if (phy != phy2) { 311 wpan_phy_put(phy2); 312 goto out_dev; 313 } 314 } 315 316 rc = -ENOBUFS; 317 318 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE); 319 if (!msg) 320 goto out_dev; 321 322 if (!phy->del_iface) { 323 rc = -EINVAL; 324 goto nla_put_failure; 325 } 326 327 rtnl_lock(); 328 phy->del_iface(phy, dev); 329 330 /* We don't have device anymore */ 331 dev_put(dev); 332 dev = NULL; 333 334 rtnl_unlock(); 335 336 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || 337 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name)) 338 goto nla_put_failure; 339 wpan_phy_put(phy); 340 341 return ieee802154_nl_reply(msg, info); 342 343 nla_put_failure: 344 nlmsg_free(msg); 345 out_dev: 346 wpan_phy_put(phy); 347 if (dev) 348 dev_put(dev); 349 350 return rc; 351 } 352