Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/rzhang/linux
[linux-2.6-block.git] / net / ieee802154 / netlink.c
CommitLineData
2c21d115 1/*
69bb631e 2 * Netlink interface for IEEE 802.15.4 stack
2c21d115
SL
3 *
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 *
2c21d115
SL
15 * Written by:
16 * Sergey Lapin <slapin@ossfans.org>
17 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
8e753dd0 18 * Maxim Osipov <maxim.osipov@siemens.com>
2c21d115
SL
19 */
20
21#include <linux/kernel.h>
5a0e3ad6 22#include <linux/gfp.h>
2c21d115
SL
23#include <net/genetlink.h>
24#include <linux/nl802154.h>
78fe738d
DES
25
26#include "ieee802154.h"
2c21d115
SL
27
28static unsigned int ieee802154_seq_num;
c4835d81 29static DEFINE_SPINLOCK(ieee802154_seq_lock);
2c21d115 30
2c21d115 31/* Requests to userspace */
78fe738d 32struct sk_buff *ieee802154_nl_create(int flags, u8 req)
2c21d115
SL
33{
34 void *hdr;
58050fce 35 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
c4835d81 36 unsigned long f;
2c21d115
SL
37
38 if (!msg)
39 return NULL;
40
c4835d81 41 spin_lock_irqsave(&ieee802154_seq_lock, f);
2c21d115 42 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
4710d806 43 &nl802154_family, flags, req);
c4835d81 44 spin_unlock_irqrestore(&ieee802154_seq_lock, f);
2c21d115
SL
45 if (!hdr) {
46 nlmsg_free(msg);
47 return NULL;
48 }
49
50 return msg;
51}
52
78fe738d 53int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
2c21d115 54{
10c9cbb1
H
55 struct nlmsghdr *nlh = nlmsg_hdr(msg);
56 void *hdr = genlmsg_data(nlmsg_data(nlh));
2c21d115 57
053c095a 58 genlmsg_end(msg, hdr);
2c21d115 59
68eb5503 60 return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC);
2c21d115
SL
61}
62
339b4ca5 63struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
cad865dc 64 int flags, u8 req)
339b4ca5
DES
65{
66 void *hdr;
58050fce 67 struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
339b4ca5
DES
68
69 if (!msg)
70 return NULL;
71
72 hdr = genlmsg_put_reply(msg, info,
4710d806 73 &nl802154_family, flags, req);
339b4ca5
DES
74 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{
10c9cbb1
H
84 struct nlmsghdr *nlh = nlmsg_hdr(msg);
85 void *hdr = genlmsg_data(nlmsg_data(nlh));
339b4ca5 86
053c095a 87 genlmsg_end(msg, hdr);
339b4ca5
DES
88
89 return genlmsg_reply(msg, info);
339b4ca5
DES
90}
91
d01174fc 92static const struct genl_ops ieee802154_ops[] = {
1c582d91
JB
93 /* 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),
e462ded6 106 IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams),
3e9c156e
PB
107 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),
1c582d91
JB
127};
128
2a94fe48
JB
129static 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};
2c21d115 133
56989f6d 134struct genl_family nl802154_family __ro_after_init = {
489111e5
JB
135 .hdrsize = 0,
136 .name = IEEE802154_NL_NAME,
137 .version = 1,
138 .maxattr = IEEE802154_ATTR_MAX,
139 .module = THIS_MODULE,
d01174fc
XL
140 .ops = ieee802154_ops,
141 .n_ops = ARRAY_SIZE(ieee802154_ops),
489111e5
JB
142 .mcgrps = ieee802154_mcgrps,
143 .n_mcgrps = ARRAY_SIZE(ieee802154_mcgrps),
144};
145
2a94fe48
JB
146int __init ieee802154_nl_init(void)
147{
489111e5 148 return genl_register_family(&nl802154_family);
2c21d115 149}
2c21d115 150
79fe1a2a 151void ieee802154_nl_exit(void)
2c21d115 152{
78fe738d 153 genl_unregister_family(&nl802154_family);
2c21d115 154}