blob: 8a221737f75c5c874e6caf13817c6917ff478ba7 [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>
Sergey Lapin2c21d112009-06-08 12:18:49 +000026#include <net/genetlink.h>
27#include <linux/nl802154.h>
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040028
29#include "ieee802154.h"
Sergey Lapin2c21d112009-06-08 12:18:49 +000030
31static unsigned int ieee802154_seq_num;
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040032static DEFINE_SPINLOCK(ieee802154_seq_lock);
Sergey Lapin2c21d112009-06-08 12:18:49 +000033
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040034struct genl_family nl802154_family = {
Sergey Lapin2c21d112009-06-08 12:18:49 +000035 .id = GENL_ID_GENERATE,
36 .hdrsize = 0,
37 .name = IEEE802154_NL_NAME,
38 .version = 1,
39 .maxattr = IEEE802154_ATTR_MAX,
40};
41
Sergey Lapin2c21d112009-06-08 12:18:49 +000042/* Requests to userspace */
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040043struct sk_buff *ieee802154_nl_create(int flags, u8 req)
Sergey Lapin2c21d112009-06-08 12:18:49 +000044{
45 void *hdr;
46 struct sk_buff *msg = nlmsg_new(NLMSG_GOODSIZE, GFP_ATOMIC);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040047 unsigned long f;
Sergey Lapin2c21d112009-06-08 12:18:49 +000048
49 if (!msg)
50 return NULL;
51
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040052 spin_lock_irqsave(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000053 hdr = genlmsg_put(msg, 0, ieee802154_seq_num++,
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040054 &nl802154_family, flags, req);
Dmitry Eremin-Solenikovc4835d82009-09-10 18:02:30 +040055 spin_unlock_irqrestore(&ieee802154_seq_lock, f);
Sergey Lapin2c21d112009-06-08 12:18:49 +000056 if (!hdr) {
57 nlmsg_free(msg);
58 return NULL;
59 }
60
61 return msg;
62}
63
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040064int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group)
Sergey Lapin2c21d112009-06-08 12:18:49 +000065{
66 /* XXX: nlh is right at the start of msg */
67 void *hdr = genlmsg_data(NLMSG_DATA(msg->data));
68
Dmitry Baryshkov8e753dd2009-08-07 02:58:40 +000069 if (genlmsg_end(msg, hdr) < 0)
Sergey Lapin2c21d112009-06-08 12:18:49 +000070 goto out;
71
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040072 return genlmsg_multicast(msg, 0, group, GFP_ATOMIC);
Sergey Lapin2c21d112009-06-08 12:18:49 +000073out:
74 nlmsg_free(msg);
75 return -ENOBUFS;
76}
77
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +040078int __init ieee802154_nl_init(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +000079{
80 int rc;
Sergey Lapin2c21d112009-06-08 12:18:49 +000081
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040082 rc = genl_register_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +000083 if (rc)
84 goto fail;
85
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040086 rc = nl802154_mac_register();
Sergey Lapin2c21d112009-06-08 12:18:49 +000087 if (rc)
88 goto fail;
89
Dmitry Eremin-Solenikov1eaa9d02009-09-15 17:04:44 +040090 rc = nl802154_phy_register();
91 if (rc)
92 goto fail;
93
Sergey Lapin2c21d112009-06-08 12:18:49 +000094 return 0;
95
96fail:
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +040097 genl_unregister_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +000098 return rc;
99}
Sergey Lapin2c21d112009-06-08 12:18:49 +0000100
Dmitry Eremin-Solenikovcb6b3762009-09-10 17:50:12 +0400101void __exit ieee802154_nl_exit(void)
Sergey Lapin2c21d112009-06-08 12:18:49 +0000102{
Dmitry Eremin-Solenikov78fe7382009-09-14 18:17:36 +0400103 genl_unregister_family(&nl802154_family);
Sergey Lapin2c21d112009-06-08 12:18:49 +0000104}
Dmitry Eremin-Solenikovdfd06fe2009-06-19 17:02:09 +0400105