2 * Netlink inteface for IEEE 802.15.4 stack
4 * Copyright 2007, 2008 Siemens AG
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.
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.
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.
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
22 * Maxim Osipov <maxim.osipov@siemens.com>
25 #include <linux/kernel.h>
26 #include <net/netlink.h>
27 #include <net/genetlink.h>
28 #include <net/wpan-phy.h>
29 #include <net/af_ieee802154.h>
30 #include <net/ieee802154_netdev.h>
31 #include <net/rtnetlink.h> /* for rtnl_{un,}lock */
32 #include <linux/nl802154.h>
34 #include "ieee802154.h"
36 static int ieee802154_nl_fill_phy(struct sk_buff
*msg
, u32 pid
,
37 u32 seq
, int flags
, struct wpan_phy
*phy
)
41 uint32_t *buf
= kzalloc(32 * sizeof(uint32_t), GFP_KERNEL
);
43 pr_debug("%s\n", __func__
);
48 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, flags
,
53 mutex_lock(&phy
->pib_lock
);
54 NLA_PUT_STRING(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
));
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);
63 NLA_PUT(msg
, IEEE802154_ATTR_CHANNEL_PAGE_LIST
,
64 pages
* sizeof(uint32_t), buf
);
66 mutex_unlock(&phy
->pib_lock
);
67 return genlmsg_end(msg
, hdr
);
70 mutex_unlock(&phy
->pib_lock
);
71 genlmsg_cancel(msg
, hdr
);
77 static int ieee802154_list_phy(struct sk_buff
*skb
,
78 struct genl_info
*info
)
80 /* Request for interface name, index, type, IEEE address,
81 PAN Id, short address */
87 pr_debug("%s\n", __func__
);
89 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
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 */
97 phy
= wpan_phy_find(name
);
101 msg
= nlmsg_new(NLMSG_GOODSIZE
, GFP_KERNEL
);
105 rc
= ieee802154_nl_fill_phy(msg
, info
->snd_pid
, info
->snd_seq
,
112 return genlmsg_reply(msg
, info
);
121 struct dump_phy_data
{
123 struct netlink_callback
*cb
;
127 static int ieee802154_dump_phy_iter(struct wpan_phy
*phy
, void *_data
)
130 struct dump_phy_data
*data
= _data
;
132 pr_debug("%s\n", __func__
);
134 if (data
->idx
++ < data
->s_idx
)
137 rc
= ieee802154_nl_fill_phy(data
->skb
,
138 NETLINK_CB(data
->cb
->skb
).pid
,
139 data
->cb
->nlh
->nlmsg_seq
,
151 static int ieee802154_dump_phy(struct sk_buff
*skb
,
152 struct netlink_callback
*cb
)
154 struct dump_phy_data data
= {
157 .s_idx
= cb
->args
[0],
161 pr_debug("%s\n", __func__
);
163 wpan_phy_for_each(ieee802154_dump_phy_iter
, &data
);
165 cb
->args
[0] = data
.idx
;
170 static int ieee802154_add_iface(struct sk_buff
*skb
,
171 struct genl_info
*info
)
174 struct wpan_phy
*phy
;
178 struct net_device
*dev
;
180 pr_debug("%s\n", __func__
);
182 if (!info
->attrs
[IEEE802154_ATTR_PHY_NAME
])
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 */
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]
193 return -EINVAL
; /* phy name should be null-terminated */
198 if (strlen(devname
) >= IFNAMSIZ
)
199 return -ENAMETOOLONG
;
201 phy
= wpan_phy_find(name
);
205 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_ADD_IFACE
);
209 if (!phy
->add_iface
) {
211 goto nla_put_failure
;
214 dev
= phy
->add_iface(phy
, devname
);
217 goto nla_put_failure
;
220 NLA_PUT_STRING(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
));
221 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
);
227 return ieee802154_nl_reply(msg
, info
);
236 static int ieee802154_del_iface(struct sk_buff
*skb
,
237 struct genl_info
*info
)
240 struct wpan_phy
*phy
;
243 struct net_device
*dev
;
245 pr_debug("%s\n", __func__
);
247 if (!info
->attrs
[IEEE802154_ATTR_DEV_NAME
])
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 */
254 dev
= dev_get_by_name(genl_info_net(info
), name
);
258 phy
= ieee802154_mlme_ops(dev
)->get_phy(dev
);
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
;
267 nla_data(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]);
268 if (pname
[nla_len(info
->attrs
[IEEE802154_ATTR_PHY_NAME
]) - 1]
270 /* name should be null-terminated */
273 phy2
= wpan_phy_find(pname
);
285 msg
= ieee802154_nl_new_reply(info
, 0, IEEE802154_DEL_IFACE
);
289 if (!phy
->del_iface
) {
291 goto nla_put_failure
;
295 phy
->del_iface(phy
, dev
);
297 /* We don't have device anymore */
304 NLA_PUT_STRING(msg
, IEEE802154_ATTR_PHY_NAME
, wpan_phy_name(phy
));
305 NLA_PUT_STRING(msg
, IEEE802154_ATTR_DEV_NAME
, name
);
309 return ieee802154_nl_reply(msg
, info
);
321 static struct genl_ops ieee802154_phy_ops
[] = {
322 IEEE802154_DUMP(IEEE802154_LIST_PHY
, ieee802154_list_phy
,
323 ieee802154_dump_phy
),
324 IEEE802154_OP(IEEE802154_ADD_IFACE
, ieee802154_add_iface
),
325 IEEE802154_OP(IEEE802154_DEL_IFACE
, ieee802154_del_iface
),
329 * No need to unregister as family unregistration will do it.
331 int nl802154_phy_register(void)
336 for (i
= 0; i
< ARRAY_SIZE(ieee802154_phy_ops
); i
++) {
337 rc
= genl_register_ops(&nl802154_family
,
338 &ieee802154_phy_ops
[i
]);