1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0-only
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * Netlink interface for IEEE 802.15.4 stack
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright 2007, 2008 Siemens AG
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Written by:
8*4882a593Smuzhiyun * Sergey Lapin <slapin@ossfans.org>
9*4882a593Smuzhiyun * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
10*4882a593Smuzhiyun * Maxim Osipov <maxim.osipov@siemens.com>
11*4882a593Smuzhiyun */
12*4882a593Smuzhiyun
13*4882a593Smuzhiyun #include <linux/kernel.h>
14*4882a593Smuzhiyun #include <linux/gfp.h>
15*4882a593Smuzhiyun #include <net/genetlink.h>
16*4882a593Smuzhiyun #include <linux/nl802154.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "ieee802154.h"
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun static unsigned int ieee802154_seq_num;
21*4882a593Smuzhiyun static DEFINE_SPINLOCK(ieee802154_seq_lock);
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /* Requests to userspace */
ieee802154_nl_create(int flags,u8 req)24*4882a593Smuzhiyun struct sk_buff *ieee802154_nl_create(int flags, u8 req)
25*4882a593Smuzhiyun {
26*4882a593Smuzhiyun void *hdr;
27*4882a593Smuzhiyun struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
28*4882a593Smuzhiyun unsigned long f;
29*4882a593Smuzhiyun
30*4882a593Smuzhiyun if (!msg)
31*4882a593Smuzhiyun return NULL;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun spin_lock_irqsave(&ieee802154_seq_lock, f);
34*4882a593Smuzhiyun hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
35*4882a593Smuzhiyun &nl802154_family, flags, req);
36*4882a593Smuzhiyun spin_unlock_irqrestore(&ieee802154_seq_lock, f);
37*4882a593Smuzhiyun if (!hdr) {
38*4882a593Smuzhiyun nlmsg_free(msg);
39*4882a593Smuzhiyun return NULL;
40*4882a593Smuzhiyun }
41*4882a593Smuzhiyun
42*4882a593Smuzhiyun return msg;
43*4882a593Smuzhiyun }
44*4882a593Smuzhiyun
ieee802154_nl_mcast(struct sk_buff * msg,unsigned int group)45*4882a593Smuzhiyun int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
46*4882a593Smuzhiyun {
47*4882a593Smuzhiyun struct nlmsghdr *nlh = nlmsg_hdr(msg);
48*4882a593Smuzhiyun void *hdr = genlmsg_data(nlmsg_data(nlh));
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun genlmsg_end(msg, hdr);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
ieee802154_nl_new_reply(struct genl_info * info,int flags,u8 req)55*4882a593Smuzhiyun struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
56*4882a593Smuzhiyun int flags, u8 req)
57*4882a593Smuzhiyun {
58*4882a593Smuzhiyun void *hdr;
59*4882a593Smuzhiyun struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
60*4882a593Smuzhiyun
61*4882a593Smuzhiyun if (!msg)
62*4882a593Smuzhiyun return NULL;
63*4882a593Smuzhiyun
64*4882a593Smuzhiyun hdr = genlmsg_put_reply(msg, info,
65*4882a593Smuzhiyun &nl802154_family, flags, req);
66*4882a593Smuzhiyun if (!hdr) {
67*4882a593Smuzhiyun nlmsg_free(msg);
68*4882a593Smuzhiyun return NULL;
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun
71*4882a593Smuzhiyun return msg;
72*4882a593Smuzhiyun }
73*4882a593Smuzhiyun
ieee802154_nl_reply(struct sk_buff * msg,struct genl_info * info)74*4882a593Smuzhiyun int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
75*4882a593Smuzhiyun {
76*4882a593Smuzhiyun struct nlmsghdr *nlh = nlmsg_hdr(msg);
77*4882a593Smuzhiyun void *hdr = genlmsg_data(nlmsg_data(nlh));
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun genlmsg_end(msg, hdr);
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun return genlmsg_reply(msg, info);
82*4882a593Smuzhiyun }
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun static const struct genl_small_ops ieee802154_ops[] = {
85*4882a593Smuzhiyun /* see nl-phy.c */
86*4882a593Smuzhiyun IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
87*4882a593Smuzhiyun ieee802154_dump_phy),
88*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
89*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
90*4882a593Smuzhiyun /* see nl-mac.c */
91*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
92*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
93*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
94*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
95*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
96*4882a593Smuzhiyun IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
97*4882a593Smuzhiyun ieee802154_dump_iface),
98*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
99*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams),
100*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams),
101*4882a593Smuzhiyun IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL,
102*4882a593Smuzhiyun ieee802154_llsec_dump_keys),
103*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key),
104*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key),
105*4882a593Smuzhiyun IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL,
106*4882a593Smuzhiyun ieee802154_llsec_dump_devs),
107*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev),
108*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev),
109*4882a593Smuzhiyun IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL,
110*4882a593Smuzhiyun ieee802154_llsec_dump_devkeys),
111*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey),
112*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey),
113*4882a593Smuzhiyun IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL,
114*4882a593Smuzhiyun ieee802154_llsec_dump_seclevels),
115*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL,
116*4882a593Smuzhiyun ieee802154_llsec_add_seclevel),
117*4882a593Smuzhiyun IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL,
118*4882a593Smuzhiyun ieee802154_llsec_del_seclevel),
119*4882a593Smuzhiyun };
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun static const struct genl_multicast_group ieee802154_mcgrps[] = {
122*4882a593Smuzhiyun [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
123*4882a593Smuzhiyun [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
124*4882a593Smuzhiyun };
125*4882a593Smuzhiyun
126*4882a593Smuzhiyun struct genl_family nl802154_family __ro_after_init = {
127*4882a593Smuzhiyun .hdrsize = 0,
128*4882a593Smuzhiyun .name = IEEE802154_NL_NAME,
129*4882a593Smuzhiyun .version = 1,
130*4882a593Smuzhiyun .maxattr = IEEE802154_ATTR_MAX,
131*4882a593Smuzhiyun .policy = ieee802154_policy,
132*4882a593Smuzhiyun .module = THIS_MODULE,
133*4882a593Smuzhiyun .small_ops = ieee802154_ops,
134*4882a593Smuzhiyun .n_small_ops = ARRAY_SIZE(ieee802154_ops),
135*4882a593Smuzhiyun .mcgrps = ieee802154_mcgrps,
136*4882a593Smuzhiyun .n_mcgrps = ARRAY_SIZE(ieee802154_mcgrps),
137*4882a593Smuzhiyun };
138*4882a593Smuzhiyun
ieee802154_nl_init(void)139*4882a593Smuzhiyun int __init ieee802154_nl_init(void)
140*4882a593Smuzhiyun {
141*4882a593Smuzhiyun return genl_register_family(&nl802154_family);
142*4882a593Smuzhiyun }
143*4882a593Smuzhiyun
ieee802154_nl_exit(void)144*4882a593Smuzhiyun void ieee802154_nl_exit(void)
145*4882a593Smuzhiyun {
146*4882a593Smuzhiyun genl_unregister_family(&nl802154_family);
147*4882a593Smuzhiyun }
148