blob: 7fe3b6b6c4959ca7687024d8d0e988f3daf0f03e [file] [log] [blame]
Thomas Gleixner1802d0b2019-05-27 08:55:21 +02001// SPDX-License-Identifier: GPL-2.0-only
Sergey Lapin2c21d112009-06-08 12:18:49 +00002/*
Varka Bhadram69bb6312014-11-25 10:04:57 +05303 * Netlink interface for IEEE 802.15.4 stack
Sergey Lapin2c21d112009-06-08 12:18:49 +00004 *
5 * Copyright 2007, 2008 Siemens AG
6 *
Sergey Lapin2c21d112009-06-08 12:18:49 +00007 * Written by:
8 * Sergey Lapin <slapin@ossfans.org>
9 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Dmitry Baryshkov8e753dd2009-08-07 02:58:40 +000010 * Maxim Osipov <maxim.osipov@siemens.com>
Sergey Lapin2c21d112009-06-08 12:18:49 +000011 */
12
13#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090014#include <linux/gfp.h>
Sergey Lapin2c21d112009-06-08 12:18:49 +000015#include <net/genetlink.h>
16#include <linux/nl802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040017
18#include "ieee802154.h"
Sergey Lapin2c21d112009-06-08 12:18:49 +000019
20static unsigned int ieee802154_seq_num;
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040021static DEFINE_SPINLOCK(ieee802154_seq_lock);
Sergey Lapin2c21d112009-06-08 12:18:49 +000022
Sergey Lapin2c21d112009-06-08 12:18:49 +000023/* Requests to userspace */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040024struct sk_buff *ieee802154_nl_create(int flags, u8 req)
Sergey Lapin2c21d112009-06-08 12:18:49 +000025{
26 void *hdr;
Thomas Graf58050fc2012-06-28 03:57:45 +000027 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040028 unsigned long f;
Sergey Lapin2c21d112009-06-08 12:18:49 +000029
30 if (!msg)
31 return NULL;
32
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040033 spin_lock_irqsave(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000034 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
Varka Bhadram4710d802014-07-02 09:01:09 +053035 &nl802154_family, flags, req);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040036 spin_unlock_irqrestore(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000037 if (!hdr) {
38 nlmsg_free(msg);
39 return NULL;
40 }
41
42 return msg;
43}
44
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040045int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
Sergey Lapin2c21d112009-06-08 12:18:49 +000046{
Hong zhi guo10c9cbb2013-03-29 05:09:35 +000047 struct nlmsghdr *nlh = nlmsg_hdr(msg);
48 void *hdr = genlmsg_data(nlmsg_data(nlh));
Sergey Lapin2c21d112009-06-08 12:18:49 +000049
Johannes Berg053c0952015-01-16 22:09:00 +010050 genlmsg_end(msg, hdr);
Sergey Lapin2c21d112009-06-08 12:18:49 +000051
Johannes Berg68eb5502013-11-19 15:19:38 +010052 return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
Sergey Lapin2c21d112009-06-08 12:18:49 +000053}
54
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030055struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
Stefan Schmidtcad865d2014-12-04 11:51:58 +010056 int flags, u8 req)
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030057{
58 void *hdr;
Thomas Graf58050fc2012-06-28 03:57:45 +000059 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030060
61 if (!msg)
62 return NULL;
63
64 hdr = genlmsg_put_reply(msg, info,
Varka Bhadram4710d802014-07-02 09:01:09 +053065 &nl802154_family, flags, req);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030066 if (!hdr) {
67 nlmsg_free(msg);
68 return NULL;
69 }
70
71 return msg;
72}
73
74int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
75{
Hong zhi guo10c9cbb2013-03-29 05:09:35 +000076 struct nlmsghdr *nlh = nlmsg_hdr(msg);
77 void *hdr = genlmsg_data(nlmsg_data(nlh));
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030078
Johannes Berg053c0952015-01-16 22:09:00 +010079 genlmsg_end(msg, hdr);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030080
81 return genlmsg_reply(msg, info);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030082}
83
Xue Liud01174f2017-10-10 17:17:45 +020084static const struct genl_ops ieee802154_ops[] = {
Johannes Berg1c582d92013-11-14 17:14:41 +010085 /* see nl-phy.c */
86 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
87 ieee802154_dump_phy),
88 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
89 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
90 /* see nl-mac.c */
91 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
92 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
93 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
94 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
95 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
96 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
97 ieee802154_dump_iface),
Phoebe Buckheistere462ded2014-03-31 21:37:46 +020098 IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +020099 IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams),
100 IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams),
101 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL,
102 ieee802154_llsec_dump_keys),
103 IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key),
104 IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key),
105 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL,
106 ieee802154_llsec_dump_devs),
107 IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev),
108 IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev),
109 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL,
110 ieee802154_llsec_dump_devkeys),
111 IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey),
112 IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey),
113 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL,
114 ieee802154_llsec_dump_seclevels),
115 IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL,
116 ieee802154_llsec_add_seclevel),
117 IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL,
118 ieee802154_llsec_del_seclevel),
Johannes Berg1c582d92013-11-14 17:14:41 +0100119};
120
Johannes Berg2a94fe42013-11-19 15:19:39 +0100121static const struct genl_multicast_group ieee802154_mcgrps[] = {
122 [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
123 [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
124};
125
Johannes Berg56989f62016-10-24 14:40:05 +0200126struct genl_family nl802154_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200127 .hdrsize = 0,
128 .name = IEEE802154_NL_NAME,
129 .version = 1,
130 .maxattr = IEEE802154_ATTR_MAX,
Johannes Berg3b0f31f2019-03-21 22:51:02 +0100131 .policy = ieee802154_policy,
Johannes Berg489111e2016-10-24 14:40:03 +0200132 .module = THIS_MODULE,
Xue Liud01174f2017-10-10 17:17:45 +0200133 .ops = ieee802154_ops,
134 .n_ops = ARRAY_SIZE(ieee802154_ops),
Johannes Berg489111e2016-10-24 14:40:03 +0200135 .mcgrps = ieee802154_mcgrps,
136 .n_mcgrps = ARRAY_SIZE(ieee802154_mcgrps),
137};
138
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400139int __init ieee802154_nl_init(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +0000140{
Johannes Berg489111e2016-10-24 14:40:03 +0200141 return genl_register_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000142}
Sergey Lapin2c21d112009-06-08 12:18:49 +0000143
Alexander Aring79fe1a22014-11-09 08:36:53 +0100144void ieee802154_nl_exit(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +0000145{
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400146 genl_unregister_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000147}