blob: 6bde9e5a55031c9a184c418588a78790405b1b83 [file] [log] [blame]
Sergey Lapin2c21d112009-06-08 12:18:49 +00001/*
Varka Bhadram69bb6312014-11-25 10:04:57 +05302 * Netlink interface for IEEE 802.15.4 stack
Sergey Lapin2c21d112009-06-08 12:18:49 +00003 *
4 * Copyright 2007, 2008 Siemens AG
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2
8 * as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
Sergey Lapin2c21d112009-06-08 12:18:49 +000015 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Dmitry Baryshkov8e753dd2009-08-07 02:58:40 +000018 * Maxim Osipov <maxim.osipov@siemens.com>
Sergey Lapin2c21d112009-06-08 12:18:49 +000019 */
20
21#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090022#include <linux/gfp.h>
Sergey Lapin2c21d112009-06-08 12:18:49 +000023#include <net/genetlink.h>
24#include <linux/nl802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040025
26#include "ieee802154.h"
Sergey Lapin2c21d112009-06-08 12:18:49 +000027
28static unsigned int ieee802154_seq_num;
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040029static DEFINE_SPINLOCK(ieee802154_seq_lock);
Sergey Lapin2c21d112009-06-08 12:18:49 +000030
Sergey Lapin2c21d112009-06-08 12:18:49 +000031/* Requests to userspace */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040032struct sk_buff *ieee802154_nl_create(int flags, u8 req)
Sergey Lapin2c21d112009-06-08 12:18:49 +000033{
34 void *hdr;
Thomas Graf58050fc2012-06-28 03:57:45 +000035 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040036 unsigned long f;
Sergey Lapin2c21d112009-06-08 12:18:49 +000037
38 if (!msg)
39 return NULL;
40
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040041 spin_lock_irqsave(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000042 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
Varka Bhadram4710d802014-07-02 09:01:09 +053043 &nl802154_family, flags, req);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040044 spin_unlock_irqrestore(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000045 if (!hdr) {
46 nlmsg_free(msg);
47 return NULL;
48 }
49
50 return msg;
51}
52
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040053int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
Sergey Lapin2c21d112009-06-08 12:18:49 +000054{
Hong zhi guo10c9cbb2013-03-29 05:09:35 +000055 struct nlmsghdr *nlh = nlmsg_hdr(msg);
56 void *hdr = genlmsg_data(nlmsg_data(nlh));
Sergey Lapin2c21d112009-06-08 12:18:49 +000057
Johannes Berg053c0952015-01-16 22:09:00 +010058 genlmsg_end(msg, hdr);
Sergey Lapin2c21d112009-06-08 12:18:49 +000059
Johannes Berg68eb5502013-11-19 15:19:38 +010060 return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
Sergey Lapin2c21d112009-06-08 12:18:49 +000061}
62
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030063struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
Stefan Schmidtcad865d2014-12-04 11:51:58 +010064 int flags, u8 req)
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030065{
66 void *hdr;
Thomas Graf58050fc2012-06-28 03:57:45 +000067 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030068
69 if (!msg)
70 return NULL;
71
72 hdr = genlmsg_put_reply(msg, info,
Varka Bhadram4710d802014-07-02 09:01:09 +053073 &nl802154_family, flags, req);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030074 if (!hdr) {
75 nlmsg_free(msg);
76 return NULL;
77 }
78
79 return msg;
80}
81
82int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
83{
Hong zhi guo10c9cbb2013-03-29 05:09:35 +000084 struct nlmsghdr *nlh = nlmsg_hdr(msg);
85 void *hdr = genlmsg_data(nlmsg_data(nlh));
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030086
Johannes Berg053c0952015-01-16 22:09:00 +010087 genlmsg_end(msg, hdr);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030088
89 return genlmsg_reply(msg, info);
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030090}
91
Johannes Berg4534de82013-11-14 17:14:46 +010092static const struct genl_ops ieee8021154_ops[] = {
Johannes Berg1c582d92013-11-14 17:14:41 +010093 /* see nl-phy.c */
94 IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy,
95 ieee802154_dump_phy),
96 IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface),
97 IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface),
98 /* see nl-mac.c */
99 IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req),
100 IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp),
101 IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req),
102 IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req),
103 IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req),
104 IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface,
105 ieee802154_dump_iface),
Phoebe Buckheistere462ded2014-03-31 21:37:46 +0200106 IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
Phoebe Buckheister3e9c1562014-05-16 17:46:44 +0200107 IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams),
108 IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams),
109 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL,
110 ieee802154_llsec_dump_keys),
111 IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key),
112 IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key),
113 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL,
114 ieee802154_llsec_dump_devs),
115 IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev),
116 IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev),
117 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL,
118 ieee802154_llsec_dump_devkeys),
119 IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey),
120 IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey),
121 IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL,
122 ieee802154_llsec_dump_seclevels),
123 IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL,
124 ieee802154_llsec_add_seclevel),
125 IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL,
126 ieee802154_llsec_del_seclevel),
Johannes Berg1c582d92013-11-14 17:14:41 +0100127};
128
Johannes Berg2a94fe42013-11-19 15:19:39 +0100129static const struct genl_multicast_group ieee802154_mcgrps[] = {
130 [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, },
131 [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, },
132};
133
Johannes Berg56989f62016-10-24 14:40:05 +0200134struct genl_family nl802154_family __ro_after_init = {
Johannes Berg489111e2016-10-24 14:40:03 +0200135 .hdrsize = 0,
136 .name = IEEE802154_NL_NAME,
137 .version = 1,
138 .maxattr = IEEE802154_ATTR_MAX,
139 .module = THIS_MODULE,
140 .ops = ieee8021154_ops,
141 .n_ops = ARRAY_SIZE(ieee8021154_ops),
142 .mcgrps = ieee802154_mcgrps,
143 .n_mcgrps = ARRAY_SIZE(ieee802154_mcgrps),
144};
145
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400146int __init ieee802154_nl_init(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +0000147{
Johannes Berg489111e2016-10-24 14:40:03 +0200148 return genl_register_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000149}
Sergey Lapin2c21d112009-06-08 12:18:49 +0000150
Alexander Aring79fe1a22014-11-09 08:36:53 +0100151void ieee802154_nl_exit(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +0000152{
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400153 genl_unregister_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000154}