blob: c8097ae2482fc83c71ecf8ecced5e5657406f890 [file] [log] [blame]
Sergey Lapin2c21d112009-06-08 12:18:49 +00001/*
2 * Netlink inteface for IEEE 802.15.4 stack
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 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Written by:
20 * Sergey Lapin <slapin@ossfans.org>
21 * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com>
Dmitry Baryshkov8e753dd2009-08-07 02:58:40 +000022 * Maxim Osipov <maxim.osipov@siemens.com>
Sergey Lapin2c21d112009-06-08 12:18:49 +000023 */
24
25#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/gfp.h>
Sergey Lapin2c21d112009-06-08 12:18:49 +000027#include <net/genetlink.h>
28#include <linux/nl802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040029
30#include "ieee802154.h"
Sergey Lapin2c21d112009-06-08 12:18:49 +000031
32static unsigned int ieee802154_seq_num;
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040033static DEFINE_SPINLOCK(ieee802154_seq_lock);
Sergey Lapin2c21d112009-06-08 12:18:49 +000034
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040035struct genl_family nl802154_family = {
Sergey Lapin2c21d112009-06-08 12:18:49 +000036 .id = GENL_ID_GENERATE,
37 .hdrsize = 0,
38 .name = IEEE802154_NL_NAME,
39 .version = 1,
40 .maxattr = IEEE802154_ATTR_MAX,
41};
42
Sergey Lapin2c21d112009-06-08 12:18:49 +000043/* Requests to userspace */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040044struct sk_buff *ieee802154_nl_create(int flags, u8 req)
Sergey Lapin2c21d112009-06-08 12:18:49 +000045{
46 void *hdr;
47 struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040048 unsigned long f;
Sergey Lapin2c21d112009-06-08 12:18:49 +000049
50 if (!msg)
51 return NULL;
52
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040053 spin_lock_irqsave(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000054 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040055 &nl802154_family, flags, req);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040056 spin_unlock_irqrestore(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000057 if (!hdr) {
58 nlmsg_free(msg);
59 return NULL;
60 }
61
62 return msg;
63}
64
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040065int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
Sergey Lapin2c21d112009-06-08 12:18:49 +000066{
67 /* XXX: nlh is right at the start of msg */
68 void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
69
Dmitry Baryshkov8e753dd2009-08-07 02:58:40 +000070 if (genlmsg_end(msg, hdr) < 0)
Sergey Lapin2c21d112009-06-08 12:18:49 +000071 goto out;
72
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040073 return genlmsg_multicast(msg, 0, group, GFP_ATOMIC);
Sergey Lapin2c21d112009-06-08 12:18:49 +000074out:
75 nlmsg_free(msg);
76 return -ENOBUFS;
77}
78
Dmitry Eremin-Solenikov339b4ca2009-11-04 18:05:38 +030079struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info,
80 int flags, u8 req)
81{
82 void *hdr;
83 struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
84
85 if (!msg)
86 return NULL;
87
88 hdr = genlmsg_put_reply(msg, info,
89 &nl802154_family, flags, req);
90 if (!hdr) {
91 nlmsg_free(msg);
92 return NULL;
93 }
94
95 return msg;
96}
97
98int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info)
99{
100 /* XXX: nlh is right at the start of msg */
101 void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
102
103 if (genlmsg_end(msg, hdr) < 0)
104 goto out;
105
106 return genlmsg_reply(msg, info);
107out:
108 nlmsg_free(msg);
109 return -ENOBUFS;
110}
111
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400112int __init ieee802154_nl_init(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +0000113{
114 int rc;
Sergey Lapin2c21d112009-06-08 12:18:49 +0000115
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400116 rc = genl_register_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000117 if (rc)
118 goto fail;
119
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400120 rc = nl802154_mac_register();
Sergey Lapin2c21d112009-06-08 12:18:49 +0000121 if (rc)
122 goto fail;
123
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +0400124 rc = nl802154_phy_register();
125 if (rc)
126 goto fail;
127
Sergey Lapin2c21d112009-06-08 12:18:49 +0000128 return 0;
129
130fail:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400131 genl_unregister_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000132 return rc;
133}
Sergey Lapin2c21d112009-06-08 12:18:49 +0000134
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400135void __exit ieee802154_nl_exit(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +0000136{
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400137 genl_unregister_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000138}
Dmitry Eremin-Solenikovdfd06fe2009-06-19 17:02:09 +0400139