1 // SPDX-License-Identifier: GPL-2.0-only
3 * Netlink interface for IEEE 802.15.4 stack
5 * Copyright 2007, 2008 Siemens AG
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/if_arp.h>
16 #include <net/netlink.h>
17 #include <net/genetlink.h>
18 #include <net/cfg802154.h>
19 #include <net/af_ieee802154.h>
20 #include <net/ieee802154_netdev.h>
21 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
22 #include <linux/nl802154.h>
24 #include "ieee802154.h"
28 static int ieee802154_nl_fill_phy(struct sk_buff *msg, u32 portid,
29 u32 seq, int flags, struct wpan_phy *phy)
33 uint32_t *buf = kcalloc(32, sizeof(uint32_t), GFP_KERNEL);
35 pr_debug("%s\n", __func__);
40 hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags,
46 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
47 nla_put_u8(msg, IEEE802154_ATTR_PAGE, phy->current_page) ||
48 nla_put_u8(msg, IEEE802154_ATTR_CHANNEL, phy->current_channel))
50 for (i = 0; i < 32; i++) {
51 if (phy->supported.channels[i])
52 buf[pages++] = phy->supported.channels[i] | (i << 27);
55 nla_put(msg, IEEE802154_ATTR_CHANNEL_PAGE_LIST,
56 pages * sizeof(uint32_t), buf))
60 genlmsg_end(msg, hdr);
65 genlmsg_cancel(msg, hdr);
71 int ieee802154_list_phy(struct sk_buff *skb, struct genl_info *info)
73 /* Request for interface name, index, type, IEEE address,
74 * PAN Id, short address
81 pr_debug("%s\n", __func__);
83 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
86 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
87 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
88 return -EINVAL; /* phy name should be null-terminated */
90 phy = wpan_phy_find(name);
94 msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
98 rc = ieee802154_nl_fill_phy(msg, info->snd_portid, info->snd_seq,
105 return genlmsg_reply(msg, info);
113 struct dump_phy_data {
115 struct netlink_callback *cb;
119 static int ieee802154_dump_phy_iter(struct wpan_phy *phy, void *_data)
122 struct dump_phy_data *data = _data;
124 pr_debug("%s\n", __func__);
126 if (data->idx++ < data->s_idx)
129 rc = ieee802154_nl_fill_phy(data->skb,
130 NETLINK_CB(data->cb->skb).portid,
131 data->cb->nlh->nlmsg_seq,
143 int ieee802154_dump_phy(struct sk_buff *skb, struct netlink_callback *cb)
145 struct dump_phy_data data = {
148 .s_idx = cb->args[0],
152 pr_debug("%s\n", __func__);
154 wpan_phy_for_each(ieee802154_dump_phy_iter, &data);
156 cb->args[0] = data.idx;
161 int ieee802154_add_iface(struct sk_buff *skb, struct genl_info *info)
164 struct wpan_phy *phy;
168 struct net_device *dev;
169 int type = __IEEE802154_DEV_INVALID;
170 unsigned char name_assign_type;
172 pr_debug("%s\n", __func__);
174 if (!info->attrs[IEEE802154_ATTR_PHY_NAME])
177 name = nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
178 if (name[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1] != '\0')
179 return -EINVAL; /* phy name should be null-terminated */
181 if (info->attrs[IEEE802154_ATTR_DEV_NAME]) {
182 devname = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
183 if (devname[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1]
185 return -EINVAL; /* phy name should be null-terminated */
186 name_assign_type = NET_NAME_USER;
189 name_assign_type = NET_NAME_ENUM;
192 if (strlen(devname) >= IFNAMSIZ)
193 return -ENAMETOOLONG;
195 phy = wpan_phy_find(name);
199 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_ADD_IFACE);
203 if (info->attrs[IEEE802154_ATTR_HW_ADDR] &&
204 nla_len(info->attrs[IEEE802154_ATTR_HW_ADDR]) !=
205 IEEE802154_ADDR_LEN) {
207 goto nla_put_failure;
210 if (info->attrs[IEEE802154_ATTR_DEV_TYPE]) {
211 type = nla_get_u8(info->attrs[IEEE802154_ATTR_DEV_TYPE]);
212 if (type >= __IEEE802154_DEV_MAX) {
214 goto nla_put_failure;
218 dev = rdev_add_virtual_intf_deprecated(wpan_phy_to_rdev(phy), devname,
219 name_assign_type, type);
222 goto nla_put_failure;
226 if (info->attrs[IEEE802154_ATTR_HW_ADDR]) {
227 struct sockaddr addr;
229 addr.sa_family = ARPHRD_IEEE802154;
230 nla_memcpy(&addr.sa_data, info->attrs[IEEE802154_ATTR_HW_ADDR],
231 IEEE802154_ADDR_LEN);
233 /* strangely enough, some callbacks (inetdev_event) from
234 * dev_set_mac_address require RTNL_LOCK
237 rc = dev_set_mac_address(dev, &addr, NULL);
243 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
244 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name))
245 goto nla_put_failure;
250 return ieee802154_nl_reply(msg, info);
253 rtnl_lock(); /* del_iface must be called with RTNL lock */
254 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
264 int ieee802154_del_iface(struct sk_buff *skb, struct genl_info *info)
267 struct wpan_phy *phy;
270 struct net_device *dev;
272 pr_debug("%s\n", __func__);
274 if (!info->attrs[IEEE802154_ATTR_DEV_NAME])
277 name = nla_data(info->attrs[IEEE802154_ATTR_DEV_NAME]);
278 if (name[nla_len(info->attrs[IEEE802154_ATTR_DEV_NAME]) - 1] != '\0')
279 return -EINVAL; /* name should be null-terminated */
282 dev = dev_get_by_name(genl_info_net(info), name);
285 if (dev->type != ARPHRD_IEEE802154)
288 phy = dev->ieee802154_ptr->wpan_phy;
290 get_device(&phy->dev);
293 /* phy name is optional, but should be checked if it's given */
294 if (info->attrs[IEEE802154_ATTR_PHY_NAME]) {
295 struct wpan_phy *phy2;
298 nla_data(info->attrs[IEEE802154_ATTR_PHY_NAME]);
299 if (pname[nla_len(info->attrs[IEEE802154_ATTR_PHY_NAME]) - 1]
301 /* name should be null-terminated */
304 phy2 = wpan_phy_find(pname);
316 msg = ieee802154_nl_new_reply(info, 0, IEEE802154_DEL_IFACE);
321 rdev_del_virtual_intf_deprecated(wpan_phy_to_rdev(phy), dev);
323 /* We don't have device anymore */
329 if (nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) ||
330 nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, name))
331 goto nla_put_failure;
334 return ieee802154_nl_reply(msg, info);