1 // SPDX-License-Identifier: GPL-2.0-only
3 * Netlink interface for IEEE 802.15.4 stack
5 * Copyright 2007, 2008 Siemens AG
8 * Sergey Lapin <slapin@ossfans.org>
9 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
10 * Maxim Osipov <maxim.osipov@siemens.com>
13 #include <linux/gfp.h>
14 #include <linux/kernel.h>
15 #include <linux/if_arp.h>
16 #include <linux/netdevice.h>
17 #include <linux/ieee802154.h>
18 #include <net/netlink.h>
19 #include <net/genetlink.h>
21 #include <linux/nl802154.h>
22 #include <linux/export.h>
23 #include <net/af_ieee802154.h>
24 #include <net/ieee802154_netdev.h>
25 #include <net/cfg802154.h>
27 #include "ieee802154.h"
29 static int nla_put_hwaddr(struct sk_buff
*msg
, int type
, __le64 hwaddr
,
32 return nla_put_u64_64bit(msg
, type
, swab64((__force u64
)hwaddr
),
36 static __le64
nla_get_hwaddr(const struct nlattr
*nla
)
38 return ieee802154_devaddr_from_raw(nla_data(nla
));
41 static int nla_put_shortaddr(struct sk_buff
*msg
, int type
, __le16 addr
)
43 return nla_put_u16(msg
, type
, le16_to_cpu(addr
));
46 static __le16
nla_get_shortaddr(const struct nlattr
*nla
)
48 return cpu_to_le16(nla_get_u16(nla
));
51 static int ieee802154_nl_start_confirm(struct net_device
*dev
, u8 status
)
55 pr_debug("%s\n", __func__
);
57 msg
= ieee802154_nl_create(0, IEEE802154_START_CONF
);
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
,
65 nla_put_u8(msg
, IEEE802154_ATTR_STATUS
, status
))
67 return ieee802154_nl_mcast(msg
, IEEE802154_COORD_MCGRP
);
74 static int ieee802154_nl_fill_iface(struct sk_buff
*msg
, u32 portid
,
75 u32 seq
, int flags
, struct net_device
*dev
)
79 struct ieee802154_mlme_ops
*ops
;
80 __le16 short_addr
, pan_id
;
82 pr_debug("%s\n", __func__
);
84 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, flags
,
85 IEEE802154_LIST_IFACE
);
89 ops
= ieee802154_mlme_ops(dev
);
90 phy
= dev
->ieee802154_ptr
->wpan_phy
;
92 get_device(&phy
->dev
);
95 short_addr
= dev
->ieee802154_ptr
->short_addr
;
96 pan_id
= dev
->ieee802154_ptr
->pan_id
;
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
,
104 nla_put_shortaddr(msg
, IEEE802154_ATTR_SHORT_ADDR
, short_addr
) ||
105 nla_put_shortaddr(msg
, IEEE802154_ATTR_PAN_ID
, pan_id
))
106 goto nla_put_failure
;
108 if (ops
->get_mac_params
) {
109 struct ieee802154_mac_params params
;
112 ops
->get_mac_params(dev
, ¶ms
);
115 if (nla_put_s8(msg
, IEEE802154_ATTR_TXPOWER
,
116 params
.transmit_power
/ 100) ||
117 nla_put_u8(msg
, IEEE802154_ATTR_LBT_ENABLED
, params
.lbt
) ||
118 nla_put_u8(msg
, IEEE802154_ATTR_CCA_MODE
,
120 nla_put_s32(msg
, IEEE802154_ATTR_CCA_ED_LEVEL
,
121 params
.cca_ed_level
/ 100) ||
122 nla_put_u8(msg
, IEEE802154_ATTR_CSMA_RETRIES
,
123 params
.csma_retries
) ||
124 nla_put_u8(msg
, IEEE802154_ATTR_CSMA_MIN_BE
,
126 nla_put_u8(msg
, IEEE802154_ATTR_CSMA_MAX_BE
,
128 nla_put_s8(msg
, IEEE802154_ATTR_FRAME_RETRIES
,
129 params
.frame_retries
))
130 goto nla_put_failure
;
134 genlmsg_end(msg
, hdr
);
139 genlmsg_cancel(msg
, hdr
);
144 /* Requests from userspace */
145 static struct net_device
*ieee802154_nl_get_dev(struct genl_info
*info
)
147 struct net_device
*dev
;
149 if (info
->attrs
[IEEE802154_ATTR_DEV_NAME
]) {
150 char name
[IFNAMSIZ
+ 1];
152 nla_strscpy(name
, info
->attrs
[IEEE802154_ATTR_DEV_NAME
],
154 dev
= dev_get_by_name(&init_net
, name
);
155 } else if (info
->attrs
[IEEE802154_ATTR_DEV_INDEX
]) {
156 dev
= dev_get_by_index(&init_net
,
157 nla_get_u32(info
->attrs
[IEEE802154_ATTR_DEV_INDEX
]));
165 if (dev
->type
!= ARPHRD_IEEE802154
) {
173 int ieee802154_associate_req(struct sk_buff
*skb
, struct genl_info
*info
)
175 struct net_device
*dev
;
176 struct ieee802154_addr addr
;
178 int ret
= -EOPNOTSUPP
;
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
])
187 dev
= ieee802154_nl_get_dev(info
);
190 if (!ieee802154_mlme_ops(dev
)->assoc_req
)
193 if (info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
]) {
194 addr
.mode
= IEEE802154_ADDR_LONG
;
195 addr
.extended_addr
= nla_get_hwaddr(
196 info
->attrs
[IEEE802154_ATTR_COORD_HW_ADDR
]);
198 addr
.mode
= IEEE802154_ADDR_SHORT
;
199 addr
.short_addr
= nla_get_shortaddr(
200 info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]);
202 addr
.pan_id
= nla_get_shortaddr(
203 info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
]);
205 page
= nla_get_u8_default(info
->attrs
[IEEE802154_ATTR_PAGE
], 0);
207 ret
= ieee802154_mlme_ops(dev
)->assoc_req(dev
, &addr
,
208 nla_get_u8(info
->attrs
[IEEE802154_ATTR_CHANNEL
]),
210 nla_get_u8(info
->attrs
[IEEE802154_ATTR_CAPABILITY
]));
217 int ieee802154_associate_resp(struct sk_buff
*skb
, struct genl_info
*info
)
219 struct net_device
*dev
;
220 struct ieee802154_addr addr
;
221 int ret
= -EOPNOTSUPP
;
223 if (!info
->attrs
[IEEE802154_ATTR_STATUS
] ||
224 !info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
] ||
225 !info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
])
228 dev
= ieee802154_nl_get_dev(info
);
231 if (!ieee802154_mlme_ops(dev
)->assoc_resp
)
234 addr
.mode
= IEEE802154_ADDR_LONG
;
235 addr
.extended_addr
= nla_get_hwaddr(
236 info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
]);
238 addr
.pan_id
= dev
->ieee802154_ptr
->pan_id
;
241 ret
= ieee802154_mlme_ops(dev
)->assoc_resp(dev
, &addr
,
242 nla_get_shortaddr(info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]),
243 nla_get_u8(info
->attrs
[IEEE802154_ATTR_STATUS
]));
250 int ieee802154_disassociate_req(struct sk_buff
*skb
, struct genl_info
*info
)
252 struct net_device
*dev
;
253 struct ieee802154_addr addr
;
254 int ret
= -EOPNOTSUPP
;
256 if ((!info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
] &&
257 !info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]) ||
258 !info
->attrs
[IEEE802154_ATTR_REASON
])
261 dev
= ieee802154_nl_get_dev(info
);
264 if (!ieee802154_mlme_ops(dev
)->disassoc_req
)
267 if (info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
]) {
268 addr
.mode
= IEEE802154_ADDR_LONG
;
269 addr
.extended_addr
= nla_get_hwaddr(
270 info
->attrs
[IEEE802154_ATTR_DEST_HW_ADDR
]);
272 addr
.mode
= IEEE802154_ADDR_SHORT
;
273 addr
.short_addr
= nla_get_shortaddr(
274 info
->attrs
[IEEE802154_ATTR_DEST_SHORT_ADDR
]);
277 addr
.pan_id
= dev
->ieee802154_ptr
->pan_id
;
280 ret
= ieee802154_mlme_ops(dev
)->disassoc_req(dev
, &addr
,
281 nla_get_u8(info
->attrs
[IEEE802154_ATTR_REASON
]));
288 /* PANid, channel, beacon_order = 15, superframe_order = 15,
289 * PAN_coordinator, battery_life_extension = 0,
290 * coord_realignment = 0, security_enable = 0
292 int ieee802154_start_req(struct sk_buff
*skb
, struct genl_info
*info
)
294 struct net_device
*dev
;
295 struct ieee802154_addr addr
;
297 u8 channel
, bcn_ord
, sf_ord
;
299 int pan_coord
, blx
, coord_realign
;
302 if (!info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
] ||
303 !info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
] ||
304 !info
->attrs
[IEEE802154_ATTR_CHANNEL
] ||
305 !info
->attrs
[IEEE802154_ATTR_BCN_ORD
] ||
306 !info
->attrs
[IEEE802154_ATTR_SF_ORD
] ||
307 !info
->attrs
[IEEE802154_ATTR_PAN_COORD
] ||
308 !info
->attrs
[IEEE802154_ATTR_BAT_EXT
] ||
309 !info
->attrs
[IEEE802154_ATTR_COORD_REALIGN
]
313 dev
= ieee802154_nl_get_dev(info
);
317 if (netif_running(dev
))
320 if (!ieee802154_mlme_ops(dev
)->start_req
) {
325 addr
.mode
= IEEE802154_ADDR_SHORT
;
326 addr
.short_addr
= nla_get_shortaddr(
327 info
->attrs
[IEEE802154_ATTR_COORD_SHORT_ADDR
]);
328 addr
.pan_id
= nla_get_shortaddr(
329 info
->attrs
[IEEE802154_ATTR_COORD_PAN_ID
]);
331 channel
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CHANNEL
]);
332 bcn_ord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_BCN_ORD
]);
333 sf_ord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_SF_ORD
]);
334 pan_coord
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_PAN_COORD
]);
335 blx
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_BAT_EXT
]);
336 coord_realign
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_COORD_REALIGN
]);
338 page
= nla_get_u8_default(info
->attrs
[IEEE802154_ATTR_PAGE
], 0);
340 if (addr
.short_addr
== cpu_to_le16(IEEE802154_ADDR_BROADCAST
)) {
341 ieee802154_nl_start_confirm(dev
, IEEE802154_NO_SHORT_ADDRESS
);
347 ret
= ieee802154_mlme_ops(dev
)->start_req(dev
, &addr
, channel
, page
,
348 bcn_ord
, sf_ord
, pan_coord
, blx
, coord_realign
);
351 /* FIXME: add validation for unused parameters to be sane
354 ieee802154_nl_start_confirm(dev
, IEEE802154_SUCCESS
);
361 int ieee802154_scan_req(struct sk_buff
*skb
, struct genl_info
*info
)
363 struct net_device
*dev
;
364 int ret
= -EOPNOTSUPP
;
370 if (!info
->attrs
[IEEE802154_ATTR_SCAN_TYPE
] ||
371 !info
->attrs
[IEEE802154_ATTR_CHANNELS
] ||
372 !info
->attrs
[IEEE802154_ATTR_DURATION
])
375 dev
= ieee802154_nl_get_dev(info
);
378 if (!ieee802154_mlme_ops(dev
)->scan_req
)
381 type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_SCAN_TYPE
]);
382 channels
= nla_get_u32(info
->attrs
[IEEE802154_ATTR_CHANNELS
]);
383 duration
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_DURATION
]);
385 page
= nla_get_u8_default(info
->attrs
[IEEE802154_ATTR_PAGE
], 0);
387 ret
= ieee802154_mlme_ops(dev
)->scan_req(dev
, type
, channels
,
395 int ieee802154_list_iface(struct sk_buff
*skb
, struct genl_info
*info
)
397 /* Request for interface name, index, type, IEEE address,
398 * PAN Id, short address
401 struct net_device
*dev
= NULL
;
404 pr_debug("%s\n", __func__
);
406 dev
= ieee802154_nl_get_dev(info
);
410 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
414 rc
= ieee802154_nl_fill_iface(msg
, info
->snd_portid
, info
->snd_seq
,
421 return genlmsg_reply(msg
, info
);
429 int ieee802154_dump_iface(struct sk_buff
*skb
, struct netlink_callback
*cb
)
431 struct net
*net
= sock_net(skb
->sk
);
432 struct net_device
*dev
;
434 int s_idx
= cb
->args
[0];
436 pr_debug("%s\n", __func__
);
439 for_each_netdev(net
, dev
) {
440 if (idx
< s_idx
|| dev
->type
!= ARPHRD_IEEE802154
)
443 if (ieee802154_nl_fill_iface(skb
, NETLINK_CB(cb
->skb
).portid
,
445 NLM_F_MULTI
, dev
) < 0)
455 int ieee802154_set_macparams(struct sk_buff
*skb
, struct genl_info
*info
)
457 struct net_device
*dev
= NULL
;
458 struct ieee802154_mlme_ops
*ops
;
459 struct ieee802154_mac_params params
;
460 struct wpan_phy
*phy
;
463 pr_debug("%s\n", __func__
);
465 dev
= ieee802154_nl_get_dev(info
);
469 ops
= ieee802154_mlme_ops(dev
);
471 if (!ops
->get_mac_params
|| !ops
->set_mac_params
) {
476 if (netif_running(dev
)) {
481 if (!info
->attrs
[IEEE802154_ATTR_LBT_ENABLED
] &&
482 !info
->attrs
[IEEE802154_ATTR_CCA_MODE
] &&
483 !info
->attrs
[IEEE802154_ATTR_CCA_ED_LEVEL
] &&
484 !info
->attrs
[IEEE802154_ATTR_CSMA_RETRIES
] &&
485 !info
->attrs
[IEEE802154_ATTR_CSMA_MIN_BE
] &&
486 !info
->attrs
[IEEE802154_ATTR_CSMA_MAX_BE
] &&
487 !info
->attrs
[IEEE802154_ATTR_FRAME_RETRIES
])
490 phy
= dev
->ieee802154_ptr
->wpan_phy
;
491 get_device(&phy
->dev
);
494 ops
->get_mac_params(dev
, ¶ms
);
496 if (info
->attrs
[IEEE802154_ATTR_TXPOWER
])
497 params
.transmit_power
= nla_get_s8(info
->attrs
[IEEE802154_ATTR_TXPOWER
]) * 100;
499 if (info
->attrs
[IEEE802154_ATTR_LBT_ENABLED
])
500 params
.lbt
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LBT_ENABLED
]);
502 if (info
->attrs
[IEEE802154_ATTR_CCA_MODE
])
503 params
.cca
.mode
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CCA_MODE
]);
505 if (info
->attrs
[IEEE802154_ATTR_CCA_ED_LEVEL
])
506 params
.cca_ed_level
= nla_get_s32(info
->attrs
[IEEE802154_ATTR_CCA_ED_LEVEL
]) * 100;
508 if (info
->attrs
[IEEE802154_ATTR_CSMA_RETRIES
])
509 params
.csma_retries
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CSMA_RETRIES
]);
511 if (info
->attrs
[IEEE802154_ATTR_CSMA_MIN_BE
])
512 params
.min_be
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CSMA_MIN_BE
]);
514 if (info
->attrs
[IEEE802154_ATTR_CSMA_MAX_BE
])
515 params
.max_be
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_CSMA_MAX_BE
]);
517 if (info
->attrs
[IEEE802154_ATTR_FRAME_RETRIES
])
518 params
.frame_retries
= nla_get_s8(info
->attrs
[IEEE802154_ATTR_FRAME_RETRIES
]);
520 rc
= ops
->set_mac_params(dev
, ¶ms
);
534 ieee802154_llsec_parse_key_id(struct genl_info
*info
,
535 struct ieee802154_llsec_key_id
*desc
)
537 memset(desc
, 0, sizeof(*desc
));
539 if (!info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_MODE
])
542 desc
->mode
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_MODE
]);
544 if (desc
->mode
== IEEE802154_SCF_KEY_IMPLICIT
) {
545 if (!info
->attrs
[IEEE802154_ATTR_PAN_ID
])
548 desc
->device_addr
.pan_id
= nla_get_shortaddr(info
->attrs
[IEEE802154_ATTR_PAN_ID
]);
550 if (info
->attrs
[IEEE802154_ATTR_SHORT_ADDR
]) {
551 desc
->device_addr
.mode
= IEEE802154_ADDR_SHORT
;
552 desc
->device_addr
.short_addr
= nla_get_shortaddr(info
->attrs
[IEEE802154_ATTR_SHORT_ADDR
]);
554 if (!info
->attrs
[IEEE802154_ATTR_HW_ADDR
])
557 desc
->device_addr
.mode
= IEEE802154_ADDR_LONG
;
558 desc
->device_addr
.extended_addr
= nla_get_hwaddr(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]);
562 if (desc
->mode
!= IEEE802154_SCF_KEY_IMPLICIT
&&
563 !info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_ID
])
566 if (desc
->mode
== IEEE802154_SCF_KEY_SHORT_INDEX
&&
567 !info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT
])
570 if (desc
->mode
== IEEE802154_SCF_KEY_HW_INDEX
&&
571 !info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED
])
574 if (desc
->mode
!= IEEE802154_SCF_KEY_IMPLICIT
)
575 desc
->id
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_ID
]);
577 switch (desc
->mode
) {
578 case IEEE802154_SCF_KEY_SHORT_INDEX
:
580 u32 source
= nla_get_u32(info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT
]);
582 desc
->short_source
= cpu_to_le32(source
);
585 case IEEE802154_SCF_KEY_HW_INDEX
:
586 desc
->extended_source
= nla_get_hwaddr(info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED
]);
594 ieee802154_llsec_fill_key_id(struct sk_buff
*msg
,
595 const struct ieee802154_llsec_key_id
*desc
)
597 if (nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_KEY_MODE
, desc
->mode
))
600 if (desc
->mode
== IEEE802154_SCF_KEY_IMPLICIT
) {
601 if (nla_put_shortaddr(msg
, IEEE802154_ATTR_PAN_ID
,
602 desc
->device_addr
.pan_id
))
605 if (desc
->device_addr
.mode
== IEEE802154_ADDR_SHORT
&&
606 nla_put_shortaddr(msg
, IEEE802154_ATTR_SHORT_ADDR
,
607 desc
->device_addr
.short_addr
))
610 if (desc
->device_addr
.mode
== IEEE802154_ADDR_LONG
&&
611 nla_put_hwaddr(msg
, IEEE802154_ATTR_HW_ADDR
,
612 desc
->device_addr
.extended_addr
,
613 IEEE802154_ATTR_PAD
))
617 if (desc
->mode
!= IEEE802154_SCF_KEY_IMPLICIT
&&
618 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_KEY_ID
, desc
->id
))
621 if (desc
->mode
== IEEE802154_SCF_KEY_SHORT_INDEX
&&
622 nla_put_u32(msg
, IEEE802154_ATTR_LLSEC_KEY_SOURCE_SHORT
,
623 le32_to_cpu(desc
->short_source
)))
626 if (desc
->mode
== IEEE802154_SCF_KEY_HW_INDEX
&&
627 nla_put_hwaddr(msg
, IEEE802154_ATTR_LLSEC_KEY_SOURCE_EXTENDED
,
628 desc
->extended_source
, IEEE802154_ATTR_PAD
))
634 int ieee802154_llsec_getparams(struct sk_buff
*skb
, struct genl_info
*info
)
637 struct net_device
*dev
= NULL
;
639 struct ieee802154_mlme_ops
*ops
;
641 struct ieee802154_llsec_params params
;
643 pr_debug("%s\n", __func__
);
645 dev
= ieee802154_nl_get_dev(info
);
649 ops
= ieee802154_mlme_ops(dev
);
655 msg
= nlmsg_new(NLMSG_DEFAULT_SIZE
, GFP_KERNEL
);
659 hdr
= genlmsg_put(msg
, 0, info
->snd_seq
, &nl802154_family
, 0,
660 IEEE802154_LLSEC_GETPARAMS
);
664 rc
= ops
->llsec
->get_params(dev
, ¶ms
);
668 if (nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
) ||
669 nla_put_u32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
) ||
670 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_ENABLED
, params
.enabled
) ||
671 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_SECLEVEL
, params
.out_level
) ||
672 nla_put_u32(msg
, IEEE802154_ATTR_LLSEC_FRAME_COUNTER
,
673 be32_to_cpu(params
.frame_counter
)) ||
674 ieee802154_llsec_fill_key_id(msg
, ¶ms
.out_key
)) {
681 return ieee802154_nl_reply(msg
, info
);
689 int ieee802154_llsec_setparams(struct sk_buff
*skb
, struct genl_info
*info
)
691 struct net_device
*dev
= NULL
;
693 struct ieee802154_mlme_ops
*ops
;
694 struct ieee802154_llsec_params params
;
697 pr_debug("%s\n", __func__
);
699 dev
= ieee802154_nl_get_dev(info
);
703 if (!info
->attrs
[IEEE802154_ATTR_LLSEC_ENABLED
] &&
704 !info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_MODE
] &&
705 !info
->attrs
[IEEE802154_ATTR_LLSEC_SECLEVEL
])
708 ops
= ieee802154_mlme_ops(dev
);
714 if (info
->attrs
[IEEE802154_ATTR_LLSEC_SECLEVEL
] &&
715 nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_SECLEVEL
]) > 7)
718 if (info
->attrs
[IEEE802154_ATTR_LLSEC_ENABLED
]) {
719 params
.enabled
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_ENABLED
]);
720 changed
|= IEEE802154_LLSEC_PARAM_ENABLED
;
723 if (info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_MODE
]) {
724 if (ieee802154_llsec_parse_key_id(info
, ¶ms
.out_key
))
727 changed
|= IEEE802154_LLSEC_PARAM_OUT_KEY
;
730 if (info
->attrs
[IEEE802154_ATTR_LLSEC_SECLEVEL
]) {
731 params
.out_level
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_SECLEVEL
]);
732 changed
|= IEEE802154_LLSEC_PARAM_OUT_LEVEL
;
735 if (info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_COUNTER
]) {
736 u32 fc
= nla_get_u32(info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_COUNTER
]);
738 params
.frame_counter
= cpu_to_be32(fc
);
739 changed
|= IEEE802154_LLSEC_PARAM_FRAME_COUNTER
;
742 rc
= ops
->llsec
->set_params(dev
, ¶ms
, changed
);
752 struct llsec_dump_data
{
757 struct net_device
*dev
;
758 struct ieee802154_mlme_ops
*ops
;
759 struct ieee802154_llsec_table
*table
;
763 ieee802154_llsec_dump_table(struct sk_buff
*skb
, struct netlink_callback
*cb
,
764 int (*step
)(struct llsec_dump_data
*))
766 struct net
*net
= sock_net(skb
->sk
);
767 struct net_device
*dev
;
768 struct llsec_dump_data data
;
770 int first_dev
= cb
->args
[0];
773 for_each_netdev(net
, dev
) {
774 if (idx
< first_dev
|| dev
->type
!= ARPHRD_IEEE802154
)
777 data
.ops
= ieee802154_mlme_ops(dev
);
778 if (!data
.ops
->llsec
)
782 data
.s_idx
= cb
->args
[1];
783 data
.s_idx2
= cb
->args
[2];
785 data
.portid
= NETLINK_CB(cb
->skb
).portid
;
786 data
.nlmsg_seq
= cb
->nlh
->nlmsg_seq
;
788 data
.ops
->llsec
->lock_table(dev
);
789 data
.ops
->llsec
->get_table(data
.dev
, &data
.table
);
791 data
.ops
->llsec
->unlock_table(dev
);
805 ieee802154_nl_llsec_change(struct sk_buff
*skb
, struct genl_info
*info
,
806 int (*fn
)(struct net_device
*, struct genl_info
*))
808 struct net_device
*dev
= NULL
;
811 dev
= ieee802154_nl_get_dev(info
);
815 if (!ieee802154_mlme_ops(dev
)->llsec
)
825 ieee802154_llsec_parse_key(struct genl_info
*info
,
826 struct ieee802154_llsec_key
*key
)
829 u32 commands
[256 / 32];
831 memset(key
, 0, sizeof(*key
));
833 if (!info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES
] ||
834 !info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_BYTES
])
837 frames
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES
]);
838 if ((frames
& BIT(IEEE802154_FC_TYPE_MAC_CMD
)) &&
839 !info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS
])
842 if (info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS
]) {
844 info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS
],
847 if (commands
[0] || commands
[1] || commands
[2] || commands
[3] ||
848 commands
[4] || commands
[5] || commands
[6] ||
849 commands
[7] >= BIT(IEEE802154_CMD_GTS_REQ
+ 1))
852 key
->cmd_frame_ids
= commands
[7];
855 key
->frame_types
= frames
;
857 nla_memcpy(key
->key
, info
->attrs
[IEEE802154_ATTR_LLSEC_KEY_BYTES
],
858 IEEE802154_LLSEC_KEY_SIZE
);
863 static int llsec_add_key(struct net_device
*dev
, struct genl_info
*info
)
865 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
866 struct ieee802154_llsec_key key
;
867 struct ieee802154_llsec_key_id id
;
869 if (ieee802154_llsec_parse_key(info
, &key
) ||
870 ieee802154_llsec_parse_key_id(info
, &id
))
873 return ops
->llsec
->add_key(dev
, &id
, &key
);
876 int ieee802154_llsec_add_key(struct sk_buff
*skb
, struct genl_info
*info
)
878 if ((info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
| NLM_F_EXCL
)) !=
879 (NLM_F_CREATE
| NLM_F_EXCL
))
882 return ieee802154_nl_llsec_change(skb
, info
, llsec_add_key
);
885 static int llsec_remove_key(struct net_device
*dev
, struct genl_info
*info
)
887 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
888 struct ieee802154_llsec_key_id id
;
890 if (ieee802154_llsec_parse_key_id(info
, &id
))
893 return ops
->llsec
->del_key(dev
, &id
);
896 int ieee802154_llsec_del_key(struct sk_buff
*skb
, struct genl_info
*info
)
898 return ieee802154_nl_llsec_change(skb
, info
, llsec_remove_key
);
902 ieee802154_nl_fill_key(struct sk_buff
*msg
, u32 portid
, u32 seq
,
903 const struct ieee802154_llsec_key_entry
*key
,
904 const struct net_device
*dev
)
907 u32 commands
[256 / 32];
909 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, NLM_F_MULTI
,
910 IEEE802154_LLSEC_LIST_KEY
);
914 if (nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
) ||
915 nla_put_u32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
) ||
916 ieee802154_llsec_fill_key_id(msg
, &key
->id
) ||
917 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_KEY_USAGE_FRAME_TYPES
,
918 key
->key
->frame_types
))
919 goto nla_put_failure
;
921 if (key
->key
->frame_types
& BIT(IEEE802154_FC_TYPE_MAC_CMD
)) {
922 memset(commands
, 0, sizeof(commands
));
923 commands
[7] = key
->key
->cmd_frame_ids
;
924 if (nla_put(msg
, IEEE802154_ATTR_LLSEC_KEY_USAGE_COMMANDS
,
925 sizeof(commands
), commands
))
926 goto nla_put_failure
;
929 if (nla_put(msg
, IEEE802154_ATTR_LLSEC_KEY_BYTES
,
930 IEEE802154_LLSEC_KEY_SIZE
, key
->key
->key
))
931 goto nla_put_failure
;
933 genlmsg_end(msg
, hdr
);
937 genlmsg_cancel(msg
, hdr
);
942 static int llsec_iter_keys(struct llsec_dump_data
*data
)
944 struct ieee802154_llsec_key_entry
*pos
;
947 list_for_each_entry(pos
, &data
->table
->keys
, list
) {
948 if (idx
++ < data
->s_idx
)
951 if (ieee802154_nl_fill_key(data
->skb
, data
->portid
,
952 data
->nlmsg_seq
, pos
, data
->dev
)) {
963 int ieee802154_llsec_dump_keys(struct sk_buff
*skb
, struct netlink_callback
*cb
)
965 return ieee802154_llsec_dump_table(skb
, cb
, llsec_iter_keys
);
969 llsec_parse_dev(struct genl_info
*info
,
970 struct ieee802154_llsec_device
*dev
)
972 memset(dev
, 0, sizeof(*dev
));
974 if (!info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_COUNTER
] ||
975 !info
->attrs
[IEEE802154_ATTR_HW_ADDR
] ||
976 !info
->attrs
[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE
] ||
977 !info
->attrs
[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE
] ||
978 (!!info
->attrs
[IEEE802154_ATTR_PAN_ID
] !=
979 !!info
->attrs
[IEEE802154_ATTR_SHORT_ADDR
]))
982 if (info
->attrs
[IEEE802154_ATTR_PAN_ID
]) {
983 dev
->pan_id
= nla_get_shortaddr(info
->attrs
[IEEE802154_ATTR_PAN_ID
]);
984 dev
->short_addr
= nla_get_shortaddr(info
->attrs
[IEEE802154_ATTR_SHORT_ADDR
]);
986 dev
->short_addr
= cpu_to_le16(IEEE802154_ADDR_UNDEF
);
989 dev
->hwaddr
= nla_get_hwaddr(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]);
990 dev
->frame_counter
= nla_get_u32(info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_COUNTER
]);
991 dev
->seclevel_exempt
= !!nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE
]);
992 dev
->key_mode
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_DEV_KEY_MODE
]);
994 if (dev
->key_mode
>= __IEEE802154_LLSEC_DEVKEY_MAX
)
1000 static int llsec_add_dev(struct net_device
*dev
, struct genl_info
*info
)
1002 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
1003 struct ieee802154_llsec_device desc
;
1005 if (llsec_parse_dev(info
, &desc
))
1008 return ops
->llsec
->add_dev(dev
, &desc
);
1011 int ieee802154_llsec_add_dev(struct sk_buff
*skb
, struct genl_info
*info
)
1013 if ((info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
| NLM_F_EXCL
)) !=
1014 (NLM_F_CREATE
| NLM_F_EXCL
))
1017 return ieee802154_nl_llsec_change(skb
, info
, llsec_add_dev
);
1020 static int llsec_del_dev(struct net_device
*dev
, struct genl_info
*info
)
1022 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
1025 if (!info
->attrs
[IEEE802154_ATTR_HW_ADDR
])
1028 devaddr
= nla_get_hwaddr(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]);
1030 return ops
->llsec
->del_dev(dev
, devaddr
);
1033 int ieee802154_llsec_del_dev(struct sk_buff
*skb
, struct genl_info
*info
)
1035 return ieee802154_nl_llsec_change(skb
, info
, llsec_del_dev
);
1039 ieee802154_nl_fill_dev(struct sk_buff
*msg
, u32 portid
, u32 seq
,
1040 const struct ieee802154_llsec_device
*desc
,
1041 const struct net_device
*dev
)
1045 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, NLM_F_MULTI
,
1046 IEEE802154_LLSEC_LIST_DEV
);
1050 if (nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
) ||
1051 nla_put_u32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
) ||
1052 nla_put_shortaddr(msg
, IEEE802154_ATTR_PAN_ID
, desc
->pan_id
) ||
1053 nla_put_shortaddr(msg
, IEEE802154_ATTR_SHORT_ADDR
,
1054 desc
->short_addr
) ||
1055 nla_put_hwaddr(msg
, IEEE802154_ATTR_HW_ADDR
, desc
->hwaddr
,
1056 IEEE802154_ATTR_PAD
) ||
1057 nla_put_u32(msg
, IEEE802154_ATTR_LLSEC_FRAME_COUNTER
,
1058 desc
->frame_counter
) ||
1059 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE
,
1060 desc
->seclevel_exempt
) ||
1061 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_DEV_KEY_MODE
, desc
->key_mode
))
1062 goto nla_put_failure
;
1064 genlmsg_end(msg
, hdr
);
1068 genlmsg_cancel(msg
, hdr
);
1073 static int llsec_iter_devs(struct llsec_dump_data
*data
)
1075 struct ieee802154_llsec_device
*pos
;
1076 int rc
= 0, idx
= 0;
1078 list_for_each_entry(pos
, &data
->table
->devices
, list
) {
1079 if (idx
++ < data
->s_idx
)
1082 if (ieee802154_nl_fill_dev(data
->skb
, data
->portid
,
1083 data
->nlmsg_seq
, pos
, data
->dev
)) {
1094 int ieee802154_llsec_dump_devs(struct sk_buff
*skb
, struct netlink_callback
*cb
)
1096 return ieee802154_llsec_dump_table(skb
, cb
, llsec_iter_devs
);
1099 static int llsec_add_devkey(struct net_device
*dev
, struct genl_info
*info
)
1101 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
1102 struct ieee802154_llsec_device_key key
;
1105 if (!info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_COUNTER
] ||
1106 !info
->attrs
[IEEE802154_ATTR_HW_ADDR
] ||
1107 ieee802154_llsec_parse_key_id(info
, &key
.key_id
))
1110 devaddr
= nla_get_hwaddr(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]);
1111 key
.frame_counter
= nla_get_u32(info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_COUNTER
]);
1113 return ops
->llsec
->add_devkey(dev
, devaddr
, &key
);
1116 int ieee802154_llsec_add_devkey(struct sk_buff
*skb
, struct genl_info
*info
)
1118 if ((info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
| NLM_F_EXCL
)) !=
1119 (NLM_F_CREATE
| NLM_F_EXCL
))
1122 return ieee802154_nl_llsec_change(skb
, info
, llsec_add_devkey
);
1125 static int llsec_del_devkey(struct net_device
*dev
, struct genl_info
*info
)
1127 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
1128 struct ieee802154_llsec_device_key key
;
1131 if (!info
->attrs
[IEEE802154_ATTR_HW_ADDR
] ||
1132 ieee802154_llsec_parse_key_id(info
, &key
.key_id
))
1135 devaddr
= nla_get_hwaddr(info
->attrs
[IEEE802154_ATTR_HW_ADDR
]);
1137 return ops
->llsec
->del_devkey(dev
, devaddr
, &key
);
1140 int ieee802154_llsec_del_devkey(struct sk_buff
*skb
, struct genl_info
*info
)
1142 return ieee802154_nl_llsec_change(skb
, info
, llsec_del_devkey
);
1146 ieee802154_nl_fill_devkey(struct sk_buff
*msg
, u32 portid
, u32 seq
,
1148 const struct ieee802154_llsec_device_key
*devkey
,
1149 const struct net_device
*dev
)
1153 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, NLM_F_MULTI
,
1154 IEEE802154_LLSEC_LIST_DEVKEY
);
1158 if (nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
) ||
1159 nla_put_u32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
) ||
1160 nla_put_hwaddr(msg
, IEEE802154_ATTR_HW_ADDR
, devaddr
,
1161 IEEE802154_ATTR_PAD
) ||
1162 nla_put_u32(msg
, IEEE802154_ATTR_LLSEC_FRAME_COUNTER
,
1163 devkey
->frame_counter
) ||
1164 ieee802154_llsec_fill_key_id(msg
, &devkey
->key_id
))
1165 goto nla_put_failure
;
1167 genlmsg_end(msg
, hdr
);
1171 genlmsg_cancel(msg
, hdr
);
1176 static int llsec_iter_devkeys(struct llsec_dump_data
*data
)
1178 struct ieee802154_llsec_device
*dpos
;
1179 struct ieee802154_llsec_device_key
*kpos
;
1182 list_for_each_entry(dpos
, &data
->table
->devices
, list
) {
1183 if (idx
++ < data
->s_idx
)
1188 list_for_each_entry(kpos
, &dpos
->keys
, list
) {
1189 if (idx2
++ < data
->s_idx2
)
1192 if (ieee802154_nl_fill_devkey(data
->skb
, data
->portid
,
1208 int ieee802154_llsec_dump_devkeys(struct sk_buff
*skb
,
1209 struct netlink_callback
*cb
)
1211 return ieee802154_llsec_dump_table(skb
, cb
, llsec_iter_devkeys
);
1215 llsec_parse_seclevel(struct genl_info
*info
,
1216 struct ieee802154_llsec_seclevel
*sl
)
1218 memset(sl
, 0, sizeof(*sl
));
1220 if (!info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_TYPE
] ||
1221 !info
->attrs
[IEEE802154_ATTR_LLSEC_SECLEVELS
] ||
1222 !info
->attrs
[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE
])
1225 sl
->frame_type
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_FRAME_TYPE
]);
1226 if (sl
->frame_type
== IEEE802154_FC_TYPE_MAC_CMD
) {
1227 if (!info
->attrs
[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID
])
1230 sl
->cmd_frame_id
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_CMD_FRAME_ID
]);
1233 sl
->sec_levels
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_SECLEVELS
]);
1234 sl
->device_override
= nla_get_u8(info
->attrs
[IEEE802154_ATTR_LLSEC_DEV_OVERRIDE
]);
1239 static int llsec_add_seclevel(struct net_device
*dev
, struct genl_info
*info
)
1241 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
1242 struct ieee802154_llsec_seclevel sl
;
1244 if (llsec_parse_seclevel(info
, &sl
))
1247 return ops
->llsec
->add_seclevel(dev
, &sl
);
1250 int ieee802154_llsec_add_seclevel(struct sk_buff
*skb
, struct genl_info
*info
)
1252 if ((info
->nlhdr
->nlmsg_flags
& (NLM_F_CREATE
| NLM_F_EXCL
)) !=
1253 (NLM_F_CREATE
| NLM_F_EXCL
))
1256 return ieee802154_nl_llsec_change(skb
, info
, llsec_add_seclevel
);
1259 static int llsec_del_seclevel(struct net_device
*dev
, struct genl_info
*info
)
1261 struct ieee802154_mlme_ops
*ops
= ieee802154_mlme_ops(dev
);
1262 struct ieee802154_llsec_seclevel sl
;
1264 if (llsec_parse_seclevel(info
, &sl
))
1267 return ops
->llsec
->del_seclevel(dev
, &sl
);
1270 int ieee802154_llsec_del_seclevel(struct sk_buff
*skb
, struct genl_info
*info
)
1272 return ieee802154_nl_llsec_change(skb
, info
, llsec_del_seclevel
);
1276 ieee802154_nl_fill_seclevel(struct sk_buff
*msg
, u32 portid
, u32 seq
,
1277 const struct ieee802154_llsec_seclevel
*sl
,
1278 const struct net_device
*dev
)
1282 hdr
= genlmsg_put(msg
, 0, seq
, &nl802154_family
, NLM_F_MULTI
,
1283 IEEE802154_LLSEC_LIST_SECLEVEL
);
1287 if (nla_put_string(msg
, IEEE802154_ATTR_DEV_NAME
, dev
->name
) ||
1288 nla_put_u32(msg
, IEEE802154_ATTR_DEV_INDEX
, dev
->ifindex
) ||
1289 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_FRAME_TYPE
, sl
->frame_type
) ||
1290 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_SECLEVELS
, sl
->sec_levels
) ||
1291 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_DEV_OVERRIDE
,
1292 sl
->device_override
))
1293 goto nla_put_failure
;
1295 if (sl
->frame_type
== IEEE802154_FC_TYPE_MAC_CMD
&&
1296 nla_put_u8(msg
, IEEE802154_ATTR_LLSEC_CMD_FRAME_ID
,
1298 goto nla_put_failure
;
1300 genlmsg_end(msg
, hdr
);
1304 genlmsg_cancel(msg
, hdr
);
1309 static int llsec_iter_seclevels(struct llsec_dump_data
*data
)
1311 struct ieee802154_llsec_seclevel
*pos
;
1312 int rc
= 0, idx
= 0;
1314 list_for_each_entry(pos
, &data
->table
->security_levels
, list
) {
1315 if (idx
++ < data
->s_idx
)
1318 if (ieee802154_nl_fill_seclevel(data
->skb
, data
->portid
,
1319 data
->nlmsg_seq
, pos
,
1331 int ieee802154_llsec_dump_seclevels(struct sk_buff
*skb
,
1332 struct netlink_callback
*cb
)
1334 return ieee802154_llsec_dump_table(skb
, cb
, llsec_iter_seclevels
);