Commit | Line | Data |
---|---|---|
1802d0be | 1 | // SPDX-License-Identifier: GPL-2.0-only |
78fe738d | 2 | /* |
69bb631e | 3 | * Netlink interface for IEEE 802.15.4 stack |
78fe738d DES |
4 | * |
5 | * Copyright 2007, 2008 Siemens AG | |
6 | * | |
78fe738d DES |
7 | * Written by: |
8 | * Sergey Lapin <slapin@ossfans.org> | |
9 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> | |
10 | * Maxim Osipov <maxim.osipov@siemens.com> | |
11 | */ | |
12 | ||
5a0e3ad6 | 13 | #include <linux/gfp.h> |
78fe738d DES |
14 | #include <linux/kernel.h> |
15 | #include <linux/if_arp.h> | |
16 | #include <linux/netdevice.h> | |
4ca24aca | 17 | #include <linux/ieee802154.h> |
78fe738d DES |
18 | #include <net/netlink.h> |
19 | #include <net/genetlink.h> | |
20 | #include <net/sock.h> | |
21 | #include <linux/nl802154.h> | |
bc3b2d7f | 22 | #include <linux/export.h> |
78fe738d | 23 | #include <net/af_ieee802154.h> |
78fe738d | 24 | #include <net/ieee802154_netdev.h> |
5ad60d36 | 25 | #include <net/cfg802154.h> |
78fe738d DES |
26 | |
27 | #include "ieee802154.h" | |
28 | ||
a558da09 ND |
29 | static int nla_put_hwaddr(struct sk_buff *msg, int type, __le64 hwaddr, |
30 | int padattr) | |
ae531b94 | 31 | { |
a558da09 ND |
32 | return nla_put_u64_64bit(msg, type, swab64((__force u64)hwaddr), |
33 | padattr); | |
ae531b94 PB |
34 | } |
35 | ||
36 | static __le64 nla_get_hwaddr(const struct nlattr *nla) | |
37 | { | |
38 | return ieee802154_devaddr_from_raw(nla_data(nla)); | |
39 | } | |
40 | ||
41 | static int nla_put_shortaddr(struct sk_buff *msg, int type, __le16 addr) | |
42 | { | |
43 | return nla_put_u16(msg, type, le16_to_cpu(addr)); | |
44 | } | |
45 | ||
46 | static __le16 nla_get_shortaddr(const struct nlattr *nla) | |
47 | { | |
48 | return cpu_to_le16(nla_get_u16(nla)); | |
49 | } | |
50 | ||
9f3295b9 | 51 | static int ieee802154_nl_start_confirm(struct net_device *dev, u8 status) |
78fe738d DES |
52 | { |
53 | struct sk_buff *msg; | |
54 | ||
55 | pr_debug("%s\n", __func__); | |
56 | ||
57 | msg = ieee802154_nl_create(0, IEEE802154_START_CONF); | |
58 | if (!msg) | |
59 | return -ENOBUFS; | |
60 | ||
be51da0f DM |
61 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
62 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || | |
63 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, | |
64 | dev->dev_addr) || | |
65 | nla_put_u8(msg, IEEE802154_ATTR_STATUS, status)) | |
66 | goto nla_put_failure; | |
2a94fe48 | 67 | return ieee802154_nl_mcast(msg, IEEE802154_COORD_MCGRP); |
78fe738d DES |
68 | |
69 | nla_put_failure: | |
70 | nlmsg_free(msg); | |
71 | return -ENOBUFS; | |
72 | } | |
78fe738d | 73 | |
15e47304 | 74 | static int ieee802154_nl_fill_iface(struct sk_buff *msg, u32 portid, |
4710d806 | 75 | u32 seq, int flags, struct net_device *dev) |
78fe738d DES |
76 | { |
77 | void *hdr; | |
0a868b26 | 78 | struct wpan_phy *phy; |
e462ded6 | 79 | struct ieee802154_mlme_ops *ops; |
ae531b94 | 80 | __le16 short_addr, pan_id; |
78fe738d DES |
81 | |
82 | pr_debug("%s\n", __func__); | |
83 | ||
84 | hdr = genlmsg_put(msg, 0, seq, &nl802154_family, flags, | |
4710d806 | 85 | IEEE802154_LIST_IFACE); |
78fe738d DES |
86 | if (!hdr) |
87 | goto out; | |
88 | ||
e462ded6 | 89 | ops = ieee802154_mlme_ops(dev); |
bd28a11f | 90 | phy = dev->ieee802154_ptr->wpan_phy; |
0a868b26 | 91 | BUG_ON(!phy); |
bd28a11f | 92 | get_device(&phy->dev); |
0a868b26 | 93 | |
c947f7e1 AA |
94 | rtnl_lock(); |
95 | short_addr = dev->ieee802154_ptr->short_addr; | |
96 | pan_id = dev->ieee802154_ptr->pan_id; | |
97 | rtnl_unlock(); | |
b70ab2e8 | 98 | |
be51da0f DM |
99 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || |
100 | nla_put_string(msg, IEEE802154_ATTR_PHY_NAME, wpan_phy_name(phy)) || | |
101 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || | |
102 | nla_put(msg, IEEE802154_ATTR_HW_ADDR, IEEE802154_ADDR_LEN, | |
103 | dev->dev_addr) || | |
ae531b94 PB |
104 | nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, short_addr) || |
105 | nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, pan_id)) | |
be51da0f | 106 | goto nla_put_failure; |
e462ded6 PB |
107 | |
108 | if (ops->get_mac_params) { | |
109 | struct ieee802154_mac_params params; | |
110 | ||
7bea1ea7 | 111 | rtnl_lock(); |
e462ded6 | 112 | ops->get_mac_params(dev, ¶ms); |
7bea1ea7 | 113 | rtnl_unlock(); |
e462ded6 PB |
114 | |
115 | if (nla_put_s8(msg, IEEE802154_ATTR_TXPOWER, | |
e2eb173a | 116 | params.transmit_power / 100) || |
e462ded6 PB |
117 | nla_put_u8(msg, IEEE802154_ATTR_LBT_ENABLED, params.lbt) || |
118 | nla_put_u8(msg, IEEE802154_ATTR_CCA_MODE, | |
7fe9a388 | 119 | params.cca.mode) || |
e462ded6 | 120 | nla_put_s32(msg, IEEE802154_ATTR_CCA_ED_LEVEL, |
32b23550 | 121 | params.cca_ed_level / 100) || |
e462ded6 PB |
122 | nla_put_u8(msg, IEEE802154_ATTR_CSMA_RETRIES, |
123 | params.csma_retries) || | |
124 | nla_put_u8(msg, IEEE802154_ATTR_CSMA_MIN_BE, | |
125 | params.min_be) || | |
126 | nla_put_u8(msg, IEEE802154_ATTR_CSMA_MAX_BE, | |
127 | params.max_be) || | |
128 | nla_put_s8(msg, IEEE802154_ATTR_FRAME_RETRIES, | |
129 | params.frame_retries)) | |
130 | goto nla_put_failure; | |
131 | } | |
132 | ||
0a868b26 | 133 | wpan_phy_put(phy); |
053c095a JB |
134 | genlmsg_end(msg, hdr); |
135 | return 0; | |
78fe738d DES |
136 | |
137 | nla_put_failure: | |
0a868b26 | 138 | wpan_phy_put(phy); |
78fe738d DES |
139 | genlmsg_cancel(msg, hdr); |
140 | out: | |
141 | return -EMSGSIZE; | |
142 | } | |
143 | ||
144 | /* Requests from userspace */ | |
145 | static struct net_device *ieee802154_nl_get_dev(struct genl_info *info) | |
146 | { | |
147 | struct net_device *dev; | |
148 | ||
149 | if (info->attrs[IEEE802154_ATTR_DEV_NAME]) { | |
150 | char name[IFNAMSIZ + 1]; | |
4710d806 | 151 | |
872f6903 | 152 | nla_strscpy(name, info->attrs[IEEE802154_ATTR_DEV_NAME], |
4710d806 | 153 | sizeof(name)); |
78fe738d | 154 | dev = dev_get_by_name(&init_net, name); |
4710d806 | 155 | } else if (info->attrs[IEEE802154_ATTR_DEV_INDEX]) { |
78fe738d DES |
156 | dev = dev_get_by_index(&init_net, |
157 | nla_get_u32(info->attrs[IEEE802154_ATTR_DEV_INDEX])); | |
4710d806 | 158 | } else { |
78fe738d | 159 | return NULL; |
4710d806 | 160 | } |
78fe738d DES |
161 | |
162 | if (!dev) | |
163 | return NULL; | |
164 | ||
01c8d2bb | 165 | if (dev->type != ARPHRD_IEEE802154) { |
78fe738d DES |
166 | dev_put(dev); |
167 | return NULL; | |
168 | } | |
169 | ||
170 | return dev; | |
171 | } | |
172 | ||
1c582d91 | 173 | int ieee802154_associate_req(struct sk_buff *skb, struct genl_info *info) |
78fe738d DES |
174 | { |
175 | struct net_device *dev; | |
ae531b94 | 176 | struct ieee802154_addr addr; |
78fe738d | 177 | u8 page; |
56aa091d | 178 | int ret = -EOPNOTSUPP; |
78fe738d DES |
179 | |
180 | if (!info->attrs[IEEE802154_ATTR_CHANNEL] || | |
181 | !info->attrs[IEEE802154_ATTR_COORD_PAN_ID] || | |
182 | (!info->attrs[IEEE802154_ATTR_COORD_HW_ADDR] && | |
183 | !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]) || | |
184 | !info->attrs[IEEE802154_ATTR_CAPABILITY]) | |
185 | return -EINVAL; | |
186 | ||
187 | dev = ieee802154_nl_get_dev(info); | |
188 | if (!dev) | |
189 | return -ENODEV; | |
56aa091d WA |
190 | if (!ieee802154_mlme_ops(dev)->assoc_req) |
191 | goto out; | |
78fe738d DES |
192 | |
193 | if (info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]) { | |
ae531b94 PB |
194 | addr.mode = IEEE802154_ADDR_LONG; |
195 | addr.extended_addr = nla_get_hwaddr( | |
196 | info->attrs[IEEE802154_ATTR_COORD_HW_ADDR]); | |
78fe738d | 197 | } else { |
ae531b94 PB |
198 | addr.mode = IEEE802154_ADDR_SHORT; |
199 | addr.short_addr = nla_get_shortaddr( | |
78fe738d DES |
200 | info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]); |
201 | } | |
ae531b94 PB |
202 | addr.pan_id = nla_get_shortaddr( |
203 | info->attrs[IEEE802154_ATTR_COORD_PAN_ID]); | |
78fe738d DES |
204 | |
205 | if (info->attrs[IEEE802154_ATTR_PAGE]) | |
206 | page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]); | |
207 | else | |
208 | page = 0; | |
209 | ||
210 | ret = ieee802154_mlme_ops(dev)->assoc_req(dev, &addr, | |
211 | nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]), | |
212 | page, | |
213 | nla_get_u8(info->attrs[IEEE802154_ATTR_CAPABILITY])); | |
214 | ||
56aa091d | 215 | out: |
78fe738d DES |
216 | dev_put(dev); |
217 | return ret; | |
218 | } | |
219 | ||
1c582d91 | 220 | int ieee802154_associate_resp(struct sk_buff *skb, struct genl_info *info) |
78fe738d DES |
221 | { |
222 | struct net_device *dev; | |
ae531b94 | 223 | struct ieee802154_addr addr; |
56aa091d | 224 | int ret = -EOPNOTSUPP; |
78fe738d DES |
225 | |
226 | if (!info->attrs[IEEE802154_ATTR_STATUS] || | |
227 | !info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] || | |
228 | !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) | |
229 | return -EINVAL; | |
230 | ||
231 | dev = ieee802154_nl_get_dev(info); | |
232 | if (!dev) | |
233 | return -ENODEV; | |
56aa091d WA |
234 | if (!ieee802154_mlme_ops(dev)->assoc_resp) |
235 | goto out; | |
78fe738d | 236 | |
ae531b94 PB |
237 | addr.mode = IEEE802154_ADDR_LONG; |
238 | addr.extended_addr = nla_get_hwaddr( | |
239 | info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]); | |
c947f7e1 AA |
240 | rtnl_lock(); |
241 | addr.pan_id = dev->ieee802154_ptr->pan_id; | |
242 | rtnl_unlock(); | |
78fe738d DES |
243 | |
244 | ret = ieee802154_mlme_ops(dev)->assoc_resp(dev, &addr, | |
ae531b94 | 245 | nla_get_shortaddr(info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]), |
78fe738d DES |
246 | nla_get_u8(info->attrs[IEEE802154_ATTR_STATUS])); |
247 | ||
56aa091d | 248 | out: |
78fe738d DES |
249 | dev_put(dev); |
250 | return ret; | |
251 | } | |
252 | ||
1c582d91 | 253 | int ieee802154_disassociate_req(struct sk_buff *skb, struct genl_info *info) |
78fe738d DES |
254 | { |
255 | struct net_device *dev; | |
ae531b94 | 256 | struct ieee802154_addr addr; |
56aa091d | 257 | int ret = -EOPNOTSUPP; |
78fe738d DES |
258 | |
259 | if ((!info->attrs[IEEE802154_ATTR_DEST_HW_ADDR] && | |
4710d806 | 260 | !info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]) || |
78fe738d DES |
261 | !info->attrs[IEEE802154_ATTR_REASON]) |
262 | return -EINVAL; | |
263 | ||
264 | dev = ieee802154_nl_get_dev(info); | |
265 | if (!dev) | |
266 | return -ENODEV; | |
56aa091d WA |
267 | if (!ieee802154_mlme_ops(dev)->disassoc_req) |
268 | goto out; | |
78fe738d DES |
269 | |
270 | if (info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]) { | |
ae531b94 PB |
271 | addr.mode = IEEE802154_ADDR_LONG; |
272 | addr.extended_addr = nla_get_hwaddr( | |
273 | info->attrs[IEEE802154_ATTR_DEST_HW_ADDR]); | |
78fe738d | 274 | } else { |
ae531b94 PB |
275 | addr.mode = IEEE802154_ADDR_SHORT; |
276 | addr.short_addr = nla_get_shortaddr( | |
78fe738d DES |
277 | info->attrs[IEEE802154_ATTR_DEST_SHORT_ADDR]); |
278 | } | |
c947f7e1 AA |
279 | rtnl_lock(); |
280 | addr.pan_id = dev->ieee802154_ptr->pan_id; | |
281 | rtnl_unlock(); | |
78fe738d DES |
282 | |
283 | ret = ieee802154_mlme_ops(dev)->disassoc_req(dev, &addr, | |
284 | nla_get_u8(info->attrs[IEEE802154_ATTR_REASON])); | |
285 | ||
56aa091d | 286 | out: |
78fe738d DES |
287 | dev_put(dev); |
288 | return ret; | |
289 | } | |
290 | ||
4710d806 | 291 | /* PANid, channel, beacon_order = 15, superframe_order = 15, |
78fe738d DES |
292 | * PAN_coordinator, battery_life_extension = 0, |
293 | * coord_realignment = 0, security_enable = 0 | |
294 | */ | |
1c582d91 | 295 | int ieee802154_start_req(struct sk_buff *skb, struct genl_info *info) |
78fe738d DES |
296 | { |
297 | struct net_device *dev; | |
ae531b94 | 298 | struct ieee802154_addr addr; |
78fe738d DES |
299 | |
300 | u8 channel, bcn_ord, sf_ord; | |
301 | u8 page; | |
302 | int pan_coord, blx, coord_realign; | |
8f499f99 | 303 | int ret = -EBUSY; |
78fe738d DES |
304 | |
305 | if (!info->attrs[IEEE802154_ATTR_COORD_PAN_ID] || | |
306 | !info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR] || | |
307 | !info->attrs[IEEE802154_ATTR_CHANNEL] || | |
308 | !info->attrs[IEEE802154_ATTR_BCN_ORD] || | |
309 | !info->attrs[IEEE802154_ATTR_SF_ORD] || | |
310 | !info->attrs[IEEE802154_ATTR_PAN_COORD] || | |
311 | !info->attrs[IEEE802154_ATTR_BAT_EXT] || | |
312 | !info->attrs[IEEE802154_ATTR_COORD_REALIGN] | |
313 | ) | |
314 | return -EINVAL; | |
315 | ||
316 | dev = ieee802154_nl_get_dev(info); | |
317 | if (!dev) | |
318 | return -ENODEV; | |
8f499f99 AA |
319 | |
320 | if (netif_running(dev)) | |
56aa091d | 321 | goto out; |
78fe738d | 322 | |
8f499f99 AA |
323 | if (!ieee802154_mlme_ops(dev)->start_req) { |
324 | ret = -EOPNOTSUPP; | |
325 | goto out; | |
326 | } | |
327 | ||
ae531b94 PB |
328 | addr.mode = IEEE802154_ADDR_SHORT; |
329 | addr.short_addr = nla_get_shortaddr( | |
78fe738d | 330 | info->attrs[IEEE802154_ATTR_COORD_SHORT_ADDR]); |
ae531b94 PB |
331 | addr.pan_id = nla_get_shortaddr( |
332 | info->attrs[IEEE802154_ATTR_COORD_PAN_ID]); | |
78fe738d DES |
333 | |
334 | channel = nla_get_u8(info->attrs[IEEE802154_ATTR_CHANNEL]); | |
335 | bcn_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_BCN_ORD]); | |
336 | sf_ord = nla_get_u8(info->attrs[IEEE802154_ATTR_SF_ORD]); | |
337 | pan_coord = nla_get_u8(info->attrs[IEEE802154_ATTR_PAN_COORD]); | |
338 | blx = nla_get_u8(info->attrs[IEEE802154_ATTR_BAT_EXT]); | |
339 | coord_realign = nla_get_u8(info->attrs[IEEE802154_ATTR_COORD_REALIGN]); | |
340 | ||
341 | if (info->attrs[IEEE802154_ATTR_PAGE]) | |
342 | page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]); | |
343 | else | |
344 | page = 0; | |
345 | ||
ae531b94 | 346 | if (addr.short_addr == cpu_to_le16(IEEE802154_ADDR_BROADCAST)) { |
78fe738d DES |
347 | ieee802154_nl_start_confirm(dev, IEEE802154_NO_SHORT_ADDRESS); |
348 | dev_put(dev); | |
349 | return -EINVAL; | |
350 | } | |
351 | ||
7bea1ea7 | 352 | rtnl_lock(); |
78fe738d DES |
353 | ret = ieee802154_mlme_ops(dev)->start_req(dev, &addr, channel, page, |
354 | bcn_ord, sf_ord, pan_coord, blx, coord_realign); | |
7bea1ea7 | 355 | rtnl_unlock(); |
78fe738d | 356 | |
9f3295b9 AA |
357 | /* FIXME: add validation for unused parameters to be sane |
358 | * for SoftMAC | |
359 | */ | |
360 | ieee802154_nl_start_confirm(dev, IEEE802154_SUCCESS); | |
361 | ||
56aa091d | 362 | out: |
78fe738d DES |
363 | dev_put(dev); |
364 | return ret; | |
365 | } | |
366 | ||
1c582d91 | 367 | int ieee802154_scan_req(struct sk_buff *skb, struct genl_info *info) |
78fe738d DES |
368 | { |
369 | struct net_device *dev; | |
56aa091d | 370 | int ret = -EOPNOTSUPP; |
78fe738d DES |
371 | u8 type; |
372 | u32 channels; | |
373 | u8 duration; | |
374 | u8 page; | |
375 | ||
376 | if (!info->attrs[IEEE802154_ATTR_SCAN_TYPE] || | |
377 | !info->attrs[IEEE802154_ATTR_CHANNELS] || | |
378 | !info->attrs[IEEE802154_ATTR_DURATION]) | |
379 | return -EINVAL; | |
380 | ||
381 | dev = ieee802154_nl_get_dev(info); | |
382 | if (!dev) | |
383 | return -ENODEV; | |
56aa091d WA |
384 | if (!ieee802154_mlme_ops(dev)->scan_req) |
385 | goto out; | |
78fe738d DES |
386 | |
387 | type = nla_get_u8(info->attrs[IEEE802154_ATTR_SCAN_TYPE]); | |
388 | channels = nla_get_u32(info->attrs[IEEE802154_ATTR_CHANNELS]); | |
389 | duration = nla_get_u8(info->attrs[IEEE802154_ATTR_DURATION]); | |
390 | ||
391 | if (info->attrs[IEEE802154_ATTR_PAGE]) | |
392 | page = nla_get_u8(info->attrs[IEEE802154_ATTR_PAGE]); | |
393 | else | |
394 | page = 0; | |
395 | ||
4710d806 VB |
396 | ret = ieee802154_mlme_ops(dev)->scan_req(dev, type, channels, |
397 | page, duration); | |
78fe738d | 398 | |
56aa091d | 399 | out: |
78fe738d DES |
400 | dev_put(dev); |
401 | return ret; | |
402 | } | |
403 | ||
1c582d91 | 404 | int ieee802154_list_iface(struct sk_buff *skb, struct genl_info *info) |
78fe738d DES |
405 | { |
406 | /* Request for interface name, index, type, IEEE address, | |
4710d806 VB |
407 | * PAN Id, short address |
408 | */ | |
78fe738d DES |
409 | struct sk_buff *msg; |
410 | struct net_device *dev = NULL; | |
411 | int rc = -ENOBUFS; | |
412 | ||
413 | pr_debug("%s\n", __func__); | |
414 | ||
415 | dev = ieee802154_nl_get_dev(info); | |
416 | if (!dev) | |
417 | return -ENODEV; | |
418 | ||
58050fce | 419 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); |
78fe738d DES |
420 | if (!msg) |
421 | goto out_dev; | |
422 | ||
15e47304 | 423 | rc = ieee802154_nl_fill_iface(msg, info->snd_portid, info->snd_seq, |
4710d806 | 424 | 0, dev); |
78fe738d DES |
425 | if (rc < 0) |
426 | goto out_free; | |
427 | ||
428 | dev_put(dev); | |
429 | ||
430 | return genlmsg_reply(msg, info); | |
431 | out_free: | |
432 | nlmsg_free(msg); | |
433 | out_dev: | |
434 | dev_put(dev); | |
435 | return rc; | |
78fe738d DES |
436 | } |
437 | ||
1c582d91 | 438 | int ieee802154_dump_iface(struct sk_buff *skb, struct netlink_callback *cb) |
78fe738d DES |
439 | { |
440 | struct net *net = sock_net(skb->sk); | |
441 | struct net_device *dev; | |
442 | int idx; | |
443 | int s_idx = cb->args[0]; | |
444 | ||
445 | pr_debug("%s\n", __func__); | |
446 | ||
447 | idx = 0; | |
448 | for_each_netdev(net, dev) { | |
01c8d2bb | 449 | if (idx < s_idx || dev->type != ARPHRD_IEEE802154) |
78fe738d DES |
450 | goto cont; |
451 | ||
15e47304 | 452 | if (ieee802154_nl_fill_iface(skb, NETLINK_CB(cb->skb).portid, |
4710d806 VB |
453 | cb->nlh->nlmsg_seq, |
454 | NLM_F_MULTI, dev) < 0) | |
78fe738d DES |
455 | break; |
456 | cont: | |
457 | idx++; | |
458 | } | |
459 | cb->args[0] = idx; | |
460 | ||
461 | return skb->len; | |
462 | } | |
e462ded6 PB |
463 | |
464 | int ieee802154_set_macparams(struct sk_buff *skb, struct genl_info *info) | |
465 | { | |
466 | struct net_device *dev = NULL; | |
467 | struct ieee802154_mlme_ops *ops; | |
468 | struct ieee802154_mac_params params; | |
469 | struct wpan_phy *phy; | |
470 | int rc = -EINVAL; | |
471 | ||
472 | pr_debug("%s\n", __func__); | |
473 | ||
474 | dev = ieee802154_nl_get_dev(info); | |
475 | if (!dev) | |
476 | return -ENODEV; | |
477 | ||
478 | ops = ieee802154_mlme_ops(dev); | |
479 | ||
480 | if (!ops->get_mac_params || !ops->set_mac_params) { | |
481 | rc = -EOPNOTSUPP; | |
482 | goto out; | |
483 | } | |
484 | ||
485 | if (netif_running(dev)) { | |
486 | rc = -EBUSY; | |
487 | goto out; | |
488 | } | |
489 | ||
490 | if (!info->attrs[IEEE802154_ATTR_LBT_ENABLED] && | |
491 | !info->attrs[IEEE802154_ATTR_CCA_MODE] && | |
492 | !info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL] && | |
493 | !info->attrs[IEEE802154_ATTR_CSMA_RETRIES] && | |
494 | !info->attrs[IEEE802154_ATTR_CSMA_MIN_BE] && | |
495 | !info->attrs[IEEE802154_ATTR_CSMA_MAX_BE] && | |
496 | !info->attrs[IEEE802154_ATTR_FRAME_RETRIES]) | |
497 | goto out; | |
498 | ||
bd28a11f AA |
499 | phy = dev->ieee802154_ptr->wpan_phy; |
500 | get_device(&phy->dev); | |
e462ded6 | 501 | |
7bea1ea7 | 502 | rtnl_lock(); |
e462ded6 PB |
503 | ops->get_mac_params(dev, ¶ms); |
504 | ||
505 | if (info->attrs[IEEE802154_ATTR_TXPOWER]) | |
e2eb173a | 506 | params.transmit_power = nla_get_s8(info->attrs[IEEE802154_ATTR_TXPOWER]) * 100; |
e462ded6 PB |
507 | |
508 | if (info->attrs[IEEE802154_ATTR_LBT_ENABLED]) | |
509 | params.lbt = nla_get_u8(info->attrs[IEEE802154_ATTR_LBT_ENABLED]); | |
510 | ||
511 | if (info->attrs[IEEE802154_ATTR_CCA_MODE]) | |
7fe9a388 | 512 | params.cca.mode = nla_get_u8(info->attrs[IEEE802154_ATTR_CCA_MODE]); |
e462ded6 PB |
513 | |
514 | if (info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) | |
32b23550 | 515 | params.cca_ed_level = nla_get_s32(info->attrs[IEEE802154_ATTR_CCA_ED_LEVEL]) * 100; |
e462ded6 PB |
516 | |
517 | if (info->attrs[IEEE802154_ATTR_CSMA_RETRIES]) | |
518 | params.csma_retries = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_RETRIES]); | |
519 | ||
520 | if (info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]) | |
521 | params.min_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MIN_BE]); | |
522 | ||
523 | if (info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]) | |
524 | params.max_be = nla_get_u8(info->attrs[IEEE802154_ATTR_CSMA_MAX_BE]); | |
525 | ||
526 | if (info->attrs[IEEE802154_ATTR_FRAME_RETRIES]) | |
527 | params.frame_retries = nla_get_s8(info->attrs[IEEE802154_ATTR_FRAME_RETRIES]); | |
528 | ||
529 | rc = ops->set_mac_params(dev, ¶ms); | |
7bea1ea7 | 530 | rtnl_unlock(); |
e462ded6 PB |
531 | |
532 | wpan_phy_put(phy); | |
533 | dev_put(dev); | |
e462ded6 | 534 | |
a543c598 AA |
535 | return 0; |
536 | ||
e462ded6 PB |
537 | out: |
538 | dev_put(dev); | |
539 | return rc; | |
540 | } | |
3e9c156e | 541 | |
3e9c156e PB |
542 | static int |
543 | ieee802154_llsec_parse_key_id(struct genl_info *info, | |
544 | struct ieee802154_llsec_key_id *desc) | |
545 | { | |
546 | memset(desc, 0, sizeof(*desc)); | |
547 | ||
548 | if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) | |
549 | return -EINVAL; | |
550 | ||
551 | desc->mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]); | |
552 | ||
553 | if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) { | |
6f7f657f | 554 | if (!info->attrs[IEEE802154_ATTR_PAN_ID]) |
3e9c156e PB |
555 | return -EINVAL; |
556 | ||
557 | desc->device_addr.pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]); | |
558 | ||
559 | if (info->attrs[IEEE802154_ATTR_SHORT_ADDR]) { | |
560 | desc->device_addr.mode = IEEE802154_ADDR_SHORT; | |
561 | desc->device_addr.short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]); | |
562 | } else { | |
6f7f657f AA |
563 | if (!info->attrs[IEEE802154_ATTR_HW_ADDR]) |
564 | return -EINVAL; | |
565 | ||
3e9c156e PB |
566 | desc->device_addr.mode = IEEE802154_ADDR_LONG; |
567 | desc->device_addr.extended_addr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]); | |
568 | } | |
569 | } | |
570 | ||
571 | if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT && | |
572 | !info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]) | |
573 | return -EINVAL; | |
574 | ||
575 | if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX && | |
576 | !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]) | |
577 | return -EINVAL; | |
578 | ||
579 | if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX && | |
580 | !info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]) | |
581 | return -EINVAL; | |
582 | ||
583 | if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT) | |
584 | desc->id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_ID]); | |
585 | ||
586 | switch (desc->mode) { | |
587 | case IEEE802154_SCF_KEY_SHORT_INDEX: | |
588 | { | |
589 | u32 source = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT]); | |
4710d806 | 590 | |
3e9c156e PB |
591 | desc->short_source = cpu_to_le32(source); |
592 | break; | |
593 | } | |
594 | case IEEE802154_SCF_KEY_HW_INDEX: | |
595 | desc->extended_source = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED]); | |
596 | break; | |
597 | } | |
598 | ||
599 | return 0; | |
600 | } | |
601 | ||
602 | static int | |
603 | ieee802154_llsec_fill_key_id(struct sk_buff *msg, | |
604 | const struct ieee802154_llsec_key_id *desc) | |
605 | { | |
606 | if (nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_MODE, desc->mode)) | |
607 | return -EMSGSIZE; | |
608 | ||
609 | if (desc->mode == IEEE802154_SCF_KEY_IMPLICIT) { | |
610 | if (nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, | |
611 | desc->device_addr.pan_id)) | |
612 | return -EMSGSIZE; | |
613 | ||
614 | if (desc->device_addr.mode == IEEE802154_ADDR_SHORT && | |
615 | nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, | |
616 | desc->device_addr.short_addr)) | |
617 | return -EMSGSIZE; | |
618 | ||
619 | if (desc->device_addr.mode == IEEE802154_ADDR_LONG && | |
620 | nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, | |
a558da09 ND |
621 | desc->device_addr.extended_addr, |
622 | IEEE802154_ATTR_PAD)) | |
3e9c156e PB |
623 | return -EMSGSIZE; |
624 | } | |
625 | ||
626 | if (desc->mode != IEEE802154_SCF_KEY_IMPLICIT && | |
627 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_ID, desc->id)) | |
628 | return -EMSGSIZE; | |
629 | ||
630 | if (desc->mode == IEEE802154_SCF_KEY_SHORT_INDEX && | |
631 | nla_put_u32(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT, | |
632 | le32_to_cpu(desc->short_source))) | |
633 | return -EMSGSIZE; | |
634 | ||
635 | if (desc->mode == IEEE802154_SCF_KEY_HW_INDEX && | |
636 | nla_put_hwaddr(msg, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED, | |
a558da09 | 637 | desc->extended_source, IEEE802154_ATTR_PAD)) |
3e9c156e PB |
638 | return -EMSGSIZE; |
639 | ||
640 | return 0; | |
641 | } | |
642 | ||
643 | int ieee802154_llsec_getparams(struct sk_buff *skb, struct genl_info *info) | |
644 | { | |
645 | struct sk_buff *msg; | |
646 | struct net_device *dev = NULL; | |
647 | int rc = -ENOBUFS; | |
648 | struct ieee802154_mlme_ops *ops; | |
649 | void *hdr; | |
650 | struct ieee802154_llsec_params params; | |
651 | ||
652 | pr_debug("%s\n", __func__); | |
653 | ||
654 | dev = ieee802154_nl_get_dev(info); | |
655 | if (!dev) | |
656 | return -ENODEV; | |
657 | ||
658 | ops = ieee802154_mlme_ops(dev); | |
b3f7a7b4 DC |
659 | if (!ops->llsec) { |
660 | rc = -EOPNOTSUPP; | |
661 | goto out_dev; | |
662 | } | |
3e9c156e PB |
663 | |
664 | msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL); | |
665 | if (!msg) | |
666 | goto out_dev; | |
667 | ||
668 | hdr = genlmsg_put(msg, 0, info->snd_seq, &nl802154_family, 0, | |
4710d806 | 669 | IEEE802154_LLSEC_GETPARAMS); |
3e9c156e PB |
670 | if (!hdr) |
671 | goto out_free; | |
672 | ||
673 | rc = ops->llsec->get_params(dev, ¶ms); | |
674 | if (rc < 0) | |
675 | goto out_free; | |
676 | ||
677 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || | |
678 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || | |
679 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_ENABLED, params.enabled) || | |
680 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVEL, params.out_level) || | |
681 | nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER, | |
682 | be32_to_cpu(params.frame_counter)) || | |
373e864c WY |
683 | ieee802154_llsec_fill_key_id(msg, ¶ms.out_key)) { |
684 | rc = -ENOBUFS; | |
3e9c156e | 685 | goto out_free; |
373e864c | 686 | } |
3e9c156e PB |
687 | |
688 | dev_put(dev); | |
689 | ||
690 | return ieee802154_nl_reply(msg, info); | |
691 | out_free: | |
692 | nlmsg_free(msg); | |
693 | out_dev: | |
694 | dev_put(dev); | |
695 | return rc; | |
696 | } | |
697 | ||
698 | int ieee802154_llsec_setparams(struct sk_buff *skb, struct genl_info *info) | |
699 | { | |
700 | struct net_device *dev = NULL; | |
701 | int rc = -EINVAL; | |
702 | struct ieee802154_mlme_ops *ops; | |
703 | struct ieee802154_llsec_params params; | |
704 | int changed = 0; | |
705 | ||
706 | pr_debug("%s\n", __func__); | |
707 | ||
708 | dev = ieee802154_nl_get_dev(info); | |
709 | if (!dev) | |
710 | return -ENODEV; | |
711 | ||
712 | if (!info->attrs[IEEE802154_ATTR_LLSEC_ENABLED] && | |
713 | !info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE] && | |
714 | !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) | |
715 | goto out; | |
716 | ||
717 | ops = ieee802154_mlme_ops(dev); | |
718 | if (!ops->llsec) { | |
719 | rc = -EOPNOTSUPP; | |
720 | goto out; | |
721 | } | |
722 | ||
723 | if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL] && | |
724 | nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) > 7) | |
725 | goto out; | |
726 | ||
727 | if (info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]) { | |
728 | params.enabled = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_ENABLED]); | |
729 | changed |= IEEE802154_LLSEC_PARAM_ENABLED; | |
730 | } | |
731 | ||
732 | if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_MODE]) { | |
733 | if (ieee802154_llsec_parse_key_id(info, ¶ms.out_key)) | |
734 | goto out; | |
735 | ||
736 | changed |= IEEE802154_LLSEC_PARAM_OUT_KEY; | |
737 | } | |
738 | ||
739 | if (info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]) { | |
740 | params.out_level = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVEL]); | |
741 | changed |= IEEE802154_LLSEC_PARAM_OUT_LEVEL; | |
742 | } | |
743 | ||
744 | if (info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]) { | |
745 | u32 fc = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]); | |
746 | ||
747 | params.frame_counter = cpu_to_be32(fc); | |
748 | changed |= IEEE802154_LLSEC_PARAM_FRAME_COUNTER; | |
749 | } | |
750 | ||
751 | rc = ops->llsec->set_params(dev, ¶ms, changed); | |
752 | ||
753 | dev_put(dev); | |
754 | ||
755 | return rc; | |
756 | out: | |
757 | dev_put(dev); | |
758 | return rc; | |
759 | } | |
760 | ||
3e9c156e PB |
761 | struct llsec_dump_data { |
762 | struct sk_buff *skb; | |
763 | int s_idx, s_idx2; | |
764 | int portid; | |
765 | int nlmsg_seq; | |
766 | struct net_device *dev; | |
767 | struct ieee802154_mlme_ops *ops; | |
768 | struct ieee802154_llsec_table *table; | |
769 | }; | |
770 | ||
771 | static int | |
772 | ieee802154_llsec_dump_table(struct sk_buff *skb, struct netlink_callback *cb, | |
4710d806 | 773 | int (*step)(struct llsec_dump_data *)) |
3e9c156e PB |
774 | { |
775 | struct net *net = sock_net(skb->sk); | |
776 | struct net_device *dev; | |
777 | struct llsec_dump_data data; | |
778 | int idx = 0; | |
779 | int first_dev = cb->args[0]; | |
780 | int rc; | |
781 | ||
782 | for_each_netdev(net, dev) { | |
01c8d2bb | 783 | if (idx < first_dev || dev->type != ARPHRD_IEEE802154) |
3e9c156e PB |
784 | goto skip; |
785 | ||
786 | data.ops = ieee802154_mlme_ops(dev); | |
787 | if (!data.ops->llsec) | |
788 | goto skip; | |
789 | ||
790 | data.skb = skb; | |
791 | data.s_idx = cb->args[1]; | |
792 | data.s_idx2 = cb->args[2]; | |
793 | data.dev = dev; | |
794 | data.portid = NETLINK_CB(cb->skb).portid; | |
795 | data.nlmsg_seq = cb->nlh->nlmsg_seq; | |
796 | ||
797 | data.ops->llsec->lock_table(dev); | |
798 | data.ops->llsec->get_table(data.dev, &data.table); | |
799 | rc = step(&data); | |
800 | data.ops->llsec->unlock_table(dev); | |
801 | ||
802 | if (rc < 0) | |
803 | break; | |
804 | ||
805 | skip: | |
806 | idx++; | |
807 | } | |
808 | cb->args[0] = idx; | |
809 | ||
810 | return skb->len; | |
811 | } | |
812 | ||
813 | static int | |
814 | ieee802154_nl_llsec_change(struct sk_buff *skb, struct genl_info *info, | |
815 | int (*fn)(struct net_device*, struct genl_info*)) | |
816 | { | |
817 | struct net_device *dev = NULL; | |
818 | int rc = -EINVAL; | |
819 | ||
820 | dev = ieee802154_nl_get_dev(info); | |
821 | if (!dev) | |
822 | return -ENODEV; | |
823 | ||
824 | if (!ieee802154_mlme_ops(dev)->llsec) | |
825 | rc = -EOPNOTSUPP; | |
826 | else | |
827 | rc = fn(dev, info); | |
828 | ||
829 | dev_put(dev); | |
830 | return rc; | |
831 | } | |
832 | ||
3e9c156e PB |
833 | static int |
834 | ieee802154_llsec_parse_key(struct genl_info *info, | |
835 | struct ieee802154_llsec_key *key) | |
836 | { | |
837 | u8 frames; | |
838 | u32 commands[256 / 32]; | |
839 | ||
840 | memset(key, 0, sizeof(*key)); | |
841 | ||
842 | if (!info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES] || | |
843 | !info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES]) | |
844 | return -EINVAL; | |
845 | ||
846 | frames = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES]); | |
847 | if ((frames & BIT(IEEE802154_FC_TYPE_MAC_CMD)) && | |
848 | !info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) | |
849 | return -EINVAL; | |
850 | ||
851 | if (info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS]) { | |
852 | nla_memcpy(commands, | |
853 | info->attrs[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS], | |
854 | 256 / 8); | |
855 | ||
856 | if (commands[0] || commands[1] || commands[2] || commands[3] || | |
857 | commands[4] || commands[5] || commands[6] || | |
858 | commands[7] >= BIT(IEEE802154_CMD_GTS_REQ + 1)) | |
859 | return -EINVAL; | |
860 | ||
861 | key->cmd_frame_ids = commands[7]; | |
862 | } | |
863 | ||
864 | key->frame_types = frames; | |
865 | ||
866 | nla_memcpy(key->key, info->attrs[IEEE802154_ATTR_LLSEC_KEY_BYTES], | |
867 | IEEE802154_LLSEC_KEY_SIZE); | |
868 | ||
869 | return 0; | |
870 | } | |
871 | ||
872 | static int llsec_add_key(struct net_device *dev, struct genl_info *info) | |
873 | { | |
874 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
875 | struct ieee802154_llsec_key key; | |
876 | struct ieee802154_llsec_key_id id; | |
877 | ||
878 | if (ieee802154_llsec_parse_key(info, &key) || | |
879 | ieee802154_llsec_parse_key_id(info, &id)) | |
880 | return -EINVAL; | |
881 | ||
882 | return ops->llsec->add_key(dev, &id, &key); | |
883 | } | |
884 | ||
885 | int ieee802154_llsec_add_key(struct sk_buff *skb, struct genl_info *info) | |
886 | { | |
887 | if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) != | |
888 | (NLM_F_CREATE | NLM_F_EXCL)) | |
889 | return -EINVAL; | |
890 | ||
891 | return ieee802154_nl_llsec_change(skb, info, llsec_add_key); | |
892 | } | |
893 | ||
894 | static int llsec_remove_key(struct net_device *dev, struct genl_info *info) | |
895 | { | |
896 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
897 | struct ieee802154_llsec_key_id id; | |
898 | ||
899 | if (ieee802154_llsec_parse_key_id(info, &id)) | |
900 | return -EINVAL; | |
901 | ||
902 | return ops->llsec->del_key(dev, &id); | |
903 | } | |
904 | ||
905 | int ieee802154_llsec_del_key(struct sk_buff *skb, struct genl_info *info) | |
906 | { | |
907 | return ieee802154_nl_llsec_change(skb, info, llsec_remove_key); | |
908 | } | |
909 | ||
910 | static int | |
911 | ieee802154_nl_fill_key(struct sk_buff *msg, u32 portid, u32 seq, | |
912 | const struct ieee802154_llsec_key_entry *key, | |
913 | const struct net_device *dev) | |
914 | { | |
915 | void *hdr; | |
916 | u32 commands[256 / 32]; | |
917 | ||
918 | hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI, | |
919 | IEEE802154_LLSEC_LIST_KEY); | |
920 | if (!hdr) | |
921 | goto out; | |
922 | ||
923 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || | |
924 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || | |
925 | ieee802154_llsec_fill_key_id(msg, &key->id) || | |
926 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES, | |
927 | key->key->frame_types)) | |
928 | goto nla_put_failure; | |
929 | ||
930 | if (key->key->frame_types & BIT(IEEE802154_FC_TYPE_MAC_CMD)) { | |
931 | memset(commands, 0, sizeof(commands)); | |
932 | commands[7] = key->key->cmd_frame_ids; | |
933 | if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS, | |
934 | sizeof(commands), commands)) | |
935 | goto nla_put_failure; | |
936 | } | |
937 | ||
938 | if (nla_put(msg, IEEE802154_ATTR_LLSEC_KEY_BYTES, | |
939 | IEEE802154_LLSEC_KEY_SIZE, key->key->key)) | |
940 | goto nla_put_failure; | |
941 | ||
942 | genlmsg_end(msg, hdr); | |
943 | return 0; | |
944 | ||
945 | nla_put_failure: | |
946 | genlmsg_cancel(msg, hdr); | |
947 | out: | |
948 | return -EMSGSIZE; | |
949 | } | |
950 | ||
951 | static int llsec_iter_keys(struct llsec_dump_data *data) | |
952 | { | |
953 | struct ieee802154_llsec_key_entry *pos; | |
954 | int rc = 0, idx = 0; | |
955 | ||
956 | list_for_each_entry(pos, &data->table->keys, list) { | |
957 | if (idx++ < data->s_idx) | |
958 | continue; | |
959 | ||
960 | if (ieee802154_nl_fill_key(data->skb, data->portid, | |
961 | data->nlmsg_seq, pos, data->dev)) { | |
962 | rc = -EMSGSIZE; | |
963 | break; | |
964 | } | |
965 | ||
966 | data->s_idx++; | |
967 | } | |
968 | ||
969 | return rc; | |
970 | } | |
971 | ||
972 | int ieee802154_llsec_dump_keys(struct sk_buff *skb, struct netlink_callback *cb) | |
973 | { | |
974 | return ieee802154_llsec_dump_table(skb, cb, llsec_iter_keys); | |
975 | } | |
976 | ||
3e9c156e PB |
977 | static int |
978 | llsec_parse_dev(struct genl_info *info, | |
979 | struct ieee802154_llsec_device *dev) | |
980 | { | |
981 | memset(dev, 0, sizeof(*dev)); | |
982 | ||
983 | if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] || | |
984 | !info->attrs[IEEE802154_ATTR_HW_ADDR] || | |
985 | !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE] || | |
986 | !info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE] || | |
987 | (!!info->attrs[IEEE802154_ATTR_PAN_ID] != | |
988 | !!info->attrs[IEEE802154_ATTR_SHORT_ADDR])) | |
989 | return -EINVAL; | |
990 | ||
991 | if (info->attrs[IEEE802154_ATTR_PAN_ID]) { | |
992 | dev->pan_id = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_PAN_ID]); | |
993 | dev->short_addr = nla_get_shortaddr(info->attrs[IEEE802154_ATTR_SHORT_ADDR]); | |
994 | } else { | |
995 | dev->short_addr = cpu_to_le16(IEEE802154_ADDR_UNDEF); | |
996 | } | |
997 | ||
998 | dev->hwaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]); | |
999 | dev->frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]); | |
1000 | dev->seclevel_exempt = !!nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]); | |
1001 | dev->key_mode = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE]); | |
1002 | ||
1003 | if (dev->key_mode >= __IEEE802154_LLSEC_DEVKEY_MAX) | |
1004 | return -EINVAL; | |
1005 | ||
1006 | return 0; | |
1007 | } | |
1008 | ||
1009 | static int llsec_add_dev(struct net_device *dev, struct genl_info *info) | |
1010 | { | |
1011 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
1012 | struct ieee802154_llsec_device desc; | |
1013 | ||
1014 | if (llsec_parse_dev(info, &desc)) | |
1015 | return -EINVAL; | |
1016 | ||
1017 | return ops->llsec->add_dev(dev, &desc); | |
1018 | } | |
1019 | ||
1020 | int ieee802154_llsec_add_dev(struct sk_buff *skb, struct genl_info *info) | |
1021 | { | |
1022 | if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) != | |
1023 | (NLM_F_CREATE | NLM_F_EXCL)) | |
1024 | return -EINVAL; | |
1025 | ||
1026 | return ieee802154_nl_llsec_change(skb, info, llsec_add_dev); | |
1027 | } | |
1028 | ||
1029 | static int llsec_del_dev(struct net_device *dev, struct genl_info *info) | |
1030 | { | |
1031 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
1032 | __le64 devaddr; | |
1033 | ||
1034 | if (!info->attrs[IEEE802154_ATTR_HW_ADDR]) | |
1035 | return -EINVAL; | |
1036 | ||
1037 | devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]); | |
1038 | ||
1039 | return ops->llsec->del_dev(dev, devaddr); | |
1040 | } | |
1041 | ||
1042 | int ieee802154_llsec_del_dev(struct sk_buff *skb, struct genl_info *info) | |
1043 | { | |
1044 | return ieee802154_nl_llsec_change(skb, info, llsec_del_dev); | |
1045 | } | |
1046 | ||
1047 | static int | |
1048 | ieee802154_nl_fill_dev(struct sk_buff *msg, u32 portid, u32 seq, | |
1049 | const struct ieee802154_llsec_device *desc, | |
1050 | const struct net_device *dev) | |
1051 | { | |
1052 | void *hdr; | |
1053 | ||
1054 | hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI, | |
1055 | IEEE802154_LLSEC_LIST_DEV); | |
1056 | if (!hdr) | |
1057 | goto out; | |
1058 | ||
1059 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || | |
1060 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || | |
1061 | nla_put_shortaddr(msg, IEEE802154_ATTR_PAN_ID, desc->pan_id) || | |
1062 | nla_put_shortaddr(msg, IEEE802154_ATTR_SHORT_ADDR, | |
1063 | desc->short_addr) || | |
a558da09 ND |
1064 | nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, desc->hwaddr, |
1065 | IEEE802154_ATTR_PAD) || | |
3e9c156e PB |
1066 | nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER, |
1067 | desc->frame_counter) || | |
1068 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE, | |
1069 | desc->seclevel_exempt) || | |
1070 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE, desc->key_mode)) | |
1071 | goto nla_put_failure; | |
1072 | ||
1073 | genlmsg_end(msg, hdr); | |
1074 | return 0; | |
1075 | ||
1076 | nla_put_failure: | |
1077 | genlmsg_cancel(msg, hdr); | |
1078 | out: | |
1079 | return -EMSGSIZE; | |
1080 | } | |
1081 | ||
1082 | static int llsec_iter_devs(struct llsec_dump_data *data) | |
1083 | { | |
1084 | struct ieee802154_llsec_device *pos; | |
1085 | int rc = 0, idx = 0; | |
1086 | ||
1087 | list_for_each_entry(pos, &data->table->devices, list) { | |
1088 | if (idx++ < data->s_idx) | |
1089 | continue; | |
1090 | ||
1091 | if (ieee802154_nl_fill_dev(data->skb, data->portid, | |
1092 | data->nlmsg_seq, pos, data->dev)) { | |
1093 | rc = -EMSGSIZE; | |
1094 | break; | |
1095 | } | |
1096 | ||
1097 | data->s_idx++; | |
1098 | } | |
1099 | ||
1100 | return rc; | |
1101 | } | |
1102 | ||
1103 | int ieee802154_llsec_dump_devs(struct sk_buff *skb, struct netlink_callback *cb) | |
1104 | { | |
1105 | return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devs); | |
1106 | } | |
1107 | ||
3e9c156e PB |
1108 | static int llsec_add_devkey(struct net_device *dev, struct genl_info *info) |
1109 | { | |
1110 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
1111 | struct ieee802154_llsec_device_key key; | |
1112 | __le64 devaddr; | |
1113 | ||
1114 | if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER] || | |
1115 | !info->attrs[IEEE802154_ATTR_HW_ADDR] || | |
1116 | ieee802154_llsec_parse_key_id(info, &key.key_id)) | |
1117 | return -EINVAL; | |
1118 | ||
1119 | devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]); | |
1120 | key.frame_counter = nla_get_u32(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_COUNTER]); | |
1121 | ||
1122 | return ops->llsec->add_devkey(dev, devaddr, &key); | |
1123 | } | |
1124 | ||
1125 | int ieee802154_llsec_add_devkey(struct sk_buff *skb, struct genl_info *info) | |
1126 | { | |
1127 | if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) != | |
1128 | (NLM_F_CREATE | NLM_F_EXCL)) | |
1129 | return -EINVAL; | |
1130 | ||
1131 | return ieee802154_nl_llsec_change(skb, info, llsec_add_devkey); | |
1132 | } | |
1133 | ||
1134 | static int llsec_del_devkey(struct net_device *dev, struct genl_info *info) | |
1135 | { | |
1136 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
1137 | struct ieee802154_llsec_device_key key; | |
1138 | __le64 devaddr; | |
1139 | ||
1140 | if (!info->attrs[IEEE802154_ATTR_HW_ADDR] || | |
1141 | ieee802154_llsec_parse_key_id(info, &key.key_id)) | |
1142 | return -EINVAL; | |
1143 | ||
1144 | devaddr = nla_get_hwaddr(info->attrs[IEEE802154_ATTR_HW_ADDR]); | |
1145 | ||
1146 | return ops->llsec->del_devkey(dev, devaddr, &key); | |
1147 | } | |
1148 | ||
1149 | int ieee802154_llsec_del_devkey(struct sk_buff *skb, struct genl_info *info) | |
1150 | { | |
1151 | return ieee802154_nl_llsec_change(skb, info, llsec_del_devkey); | |
1152 | } | |
1153 | ||
1154 | static int | |
1155 | ieee802154_nl_fill_devkey(struct sk_buff *msg, u32 portid, u32 seq, | |
1156 | __le64 devaddr, | |
1157 | const struct ieee802154_llsec_device_key *devkey, | |
1158 | const struct net_device *dev) | |
1159 | { | |
1160 | void *hdr; | |
1161 | ||
1162 | hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI, | |
1163 | IEEE802154_LLSEC_LIST_DEVKEY); | |
1164 | if (!hdr) | |
1165 | goto out; | |
1166 | ||
1167 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || | |
1168 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || | |
a558da09 ND |
1169 | nla_put_hwaddr(msg, IEEE802154_ATTR_HW_ADDR, devaddr, |
1170 | IEEE802154_ATTR_PAD) || | |
3e9c156e PB |
1171 | nla_put_u32(msg, IEEE802154_ATTR_LLSEC_FRAME_COUNTER, |
1172 | devkey->frame_counter) || | |
1173 | ieee802154_llsec_fill_key_id(msg, &devkey->key_id)) | |
1174 | goto nla_put_failure; | |
1175 | ||
1176 | genlmsg_end(msg, hdr); | |
1177 | return 0; | |
1178 | ||
1179 | nla_put_failure: | |
1180 | genlmsg_cancel(msg, hdr); | |
1181 | out: | |
1182 | return -EMSGSIZE; | |
1183 | } | |
1184 | ||
1185 | static int llsec_iter_devkeys(struct llsec_dump_data *data) | |
1186 | { | |
1187 | struct ieee802154_llsec_device *dpos; | |
1188 | struct ieee802154_llsec_device_key *kpos; | |
ad6f5cc5 | 1189 | int idx = 0, idx2; |
3e9c156e PB |
1190 | |
1191 | list_for_each_entry(dpos, &data->table->devices, list) { | |
1192 | if (idx++ < data->s_idx) | |
1193 | continue; | |
1194 | ||
1195 | idx2 = 0; | |
1196 | ||
1197 | list_for_each_entry(kpos, &dpos->keys, list) { | |
1198 | if (idx2++ < data->s_idx2) | |
1199 | continue; | |
1200 | ||
1201 | if (ieee802154_nl_fill_devkey(data->skb, data->portid, | |
1202 | data->nlmsg_seq, | |
1203 | dpos->hwaddr, kpos, | |
1204 | data->dev)) { | |
ad6f5cc5 | 1205 | return -EMSGSIZE; |
3e9c156e PB |
1206 | } |
1207 | ||
1208 | data->s_idx2++; | |
1209 | } | |
1210 | ||
1211 | data->s_idx++; | |
1212 | } | |
1213 | ||
ad6f5cc5 | 1214 | return 0; |
3e9c156e PB |
1215 | } |
1216 | ||
1217 | int ieee802154_llsec_dump_devkeys(struct sk_buff *skb, | |
1218 | struct netlink_callback *cb) | |
1219 | { | |
1220 | return ieee802154_llsec_dump_table(skb, cb, llsec_iter_devkeys); | |
1221 | } | |
1222 | ||
3e9c156e PB |
1223 | static int |
1224 | llsec_parse_seclevel(struct genl_info *info, | |
1225 | struct ieee802154_llsec_seclevel *sl) | |
1226 | { | |
1227 | memset(sl, 0, sizeof(*sl)); | |
1228 | ||
1229 | if (!info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE] || | |
1230 | !info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS] || | |
1231 | !info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]) | |
1232 | return -EINVAL; | |
1233 | ||
1234 | sl->frame_type = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_FRAME_TYPE]); | |
1235 | if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD) { | |
1236 | if (!info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]) | |
1237 | return -EINVAL; | |
1238 | ||
1239 | sl->cmd_frame_id = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID]); | |
1240 | } | |
1241 | ||
1242 | sl->sec_levels = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_SECLEVELS]); | |
1243 | sl->device_override = nla_get_u8(info->attrs[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE]); | |
1244 | ||
1245 | return 0; | |
1246 | } | |
1247 | ||
1248 | static int llsec_add_seclevel(struct net_device *dev, struct genl_info *info) | |
1249 | { | |
1250 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
1251 | struct ieee802154_llsec_seclevel sl; | |
1252 | ||
1253 | if (llsec_parse_seclevel(info, &sl)) | |
1254 | return -EINVAL; | |
1255 | ||
1256 | return ops->llsec->add_seclevel(dev, &sl); | |
1257 | } | |
1258 | ||
1259 | int ieee802154_llsec_add_seclevel(struct sk_buff *skb, struct genl_info *info) | |
1260 | { | |
1261 | if ((info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL)) != | |
1262 | (NLM_F_CREATE | NLM_F_EXCL)) | |
1263 | return -EINVAL; | |
1264 | ||
1265 | return ieee802154_nl_llsec_change(skb, info, llsec_add_seclevel); | |
1266 | } | |
1267 | ||
1268 | static int llsec_del_seclevel(struct net_device *dev, struct genl_info *info) | |
1269 | { | |
1270 | struct ieee802154_mlme_ops *ops = ieee802154_mlme_ops(dev); | |
1271 | struct ieee802154_llsec_seclevel sl; | |
1272 | ||
1273 | if (llsec_parse_seclevel(info, &sl)) | |
1274 | return -EINVAL; | |
1275 | ||
1276 | return ops->llsec->del_seclevel(dev, &sl); | |
1277 | } | |
1278 | ||
1279 | int ieee802154_llsec_del_seclevel(struct sk_buff *skb, struct genl_info *info) | |
1280 | { | |
1281 | return ieee802154_nl_llsec_change(skb, info, llsec_del_seclevel); | |
1282 | } | |
1283 | ||
1284 | static int | |
1285 | ieee802154_nl_fill_seclevel(struct sk_buff *msg, u32 portid, u32 seq, | |
1286 | const struct ieee802154_llsec_seclevel *sl, | |
1287 | const struct net_device *dev) | |
1288 | { | |
1289 | void *hdr; | |
1290 | ||
1291 | hdr = genlmsg_put(msg, 0, seq, &nl802154_family, NLM_F_MULTI, | |
1292 | IEEE802154_LLSEC_LIST_SECLEVEL); | |
1293 | if (!hdr) | |
1294 | goto out; | |
1295 | ||
1296 | if (nla_put_string(msg, IEEE802154_ATTR_DEV_NAME, dev->name) || | |
1297 | nla_put_u32(msg, IEEE802154_ATTR_DEV_INDEX, dev->ifindex) || | |
1298 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_FRAME_TYPE, sl->frame_type) || | |
1299 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_SECLEVELS, sl->sec_levels) || | |
1300 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE, | |
1301 | sl->device_override)) | |
1302 | goto nla_put_failure; | |
1303 | ||
1304 | if (sl->frame_type == IEEE802154_FC_TYPE_MAC_CMD && | |
1305 | nla_put_u8(msg, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID, | |
1306 | sl->cmd_frame_id)) | |
1307 | goto nla_put_failure; | |
1308 | ||
1309 | genlmsg_end(msg, hdr); | |
1310 | return 0; | |
1311 | ||
1312 | nla_put_failure: | |
1313 | genlmsg_cancel(msg, hdr); | |
1314 | out: | |
1315 | return -EMSGSIZE; | |
1316 | } | |
1317 | ||
1318 | static int llsec_iter_seclevels(struct llsec_dump_data *data) | |
1319 | { | |
1320 | struct ieee802154_llsec_seclevel *pos; | |
1321 | int rc = 0, idx = 0; | |
1322 | ||
1323 | list_for_each_entry(pos, &data->table->security_levels, list) { | |
1324 | if (idx++ < data->s_idx) | |
1325 | continue; | |
1326 | ||
1327 | if (ieee802154_nl_fill_seclevel(data->skb, data->portid, | |
1328 | data->nlmsg_seq, pos, | |
1329 | data->dev)) { | |
1330 | rc = -EMSGSIZE; | |
1331 | break; | |
1332 | } | |
1333 | ||
1334 | data->s_idx++; | |
1335 | } | |
1336 | ||
1337 | return rc; | |
1338 | } | |
1339 | ||
1340 | int ieee802154_llsec_dump_seclevels(struct sk_buff *skb, | |
1341 | struct netlink_callback *cb) | |
1342 | { | |
1343 | return ieee802154_llsec_dump_table(skb, cb, llsec_iter_seclevels); | |
1344 | } |