Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 1 | /* |
Varka Bhadram | 69bb631 | 2014-11-25 10:04:57 +0530 | [diff] [blame] | 2 | * Netlink interface for IEEE 802.15.4 stack |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 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 | * |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 15 | * Written by: |
| 16 | * Sergey Lapin <slapin@ossfans.org> |
| 17 | * Dmitry Eremin-Solenikov <dbaryshkov@gmail.com> |
Dmitry Baryshkov | 8e753dd | 2009-08-07 02:58:40 +0000 | [diff] [blame] | 18 | * Maxim Osipov <maxim.osipov@siemens.com> |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include <linux/kernel.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 22 | #include <linux/gfp.h> |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 23 | #include <net/genetlink.h> |
| 24 | #include <linux/nl802154.h> |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 25 | |
| 26 | #include "ieee802154.h" |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 27 | |
| 28 | static unsigned int ieee802154_seq_num; |
Dmitry Eremin-Solenikov | c4835d8 | 2009-09-10 18:02:30 +0400 | [diff] [blame] | 29 | static DEFINE_SPINLOCK(ieee802154_seq_lock); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 30 | |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 31 | struct genl_family nl802154_family = { |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 32 | .id = GENL_ID_GENERATE, |
| 33 | .hdrsize = 0, |
| 34 | .name = IEEE802154_NL_NAME, |
| 35 | .version = 1, |
| 36 | .maxattr = IEEE802154_ATTR_MAX, |
| 37 | }; |
| 38 | |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 39 | /* Requests to userspace */ |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 40 | struct sk_buff *ieee802154_nl_create(int flags, u8 req) |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 41 | { |
| 42 | void *hdr; |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 43 | struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Dmitry Eremin-Solenikov | c4835d8 | 2009-09-10 18:02:30 +0400 | [diff] [blame] | 44 | unsigned long f; |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 45 | |
| 46 | if (!msg) |
| 47 | return NULL; |
| 48 | |
Dmitry Eremin-Solenikov | c4835d8 | 2009-09-10 18:02:30 +0400 | [diff] [blame] | 49 | spin_lock_irqsave(&ieee802154_seq_lock, f); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 50 | hdr = genlmsg_put(msg, 0, ieee802154_seq_num++, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 51 | &nl802154_family, flags, req); |
Dmitry Eremin-Solenikov | c4835d8 | 2009-09-10 18:02:30 +0400 | [diff] [blame] | 52 | spin_unlock_irqrestore(&ieee802154_seq_lock, f); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 53 | if (!hdr) { |
| 54 | nlmsg_free(msg); |
| 55 | return NULL; |
| 56 | } |
| 57 | |
| 58 | return msg; |
| 59 | } |
| 60 | |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 61 | int ieee802154_nl_mcast(struct sk_buff *msg, unsigned int group) |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 62 | { |
Hong zhi guo | 10c9cbb | 2013-03-29 05:09:35 +0000 | [diff] [blame] | 63 | struct nlmsghdr *nlh = nlmsg_hdr(msg); |
| 64 | void *hdr = genlmsg_data(nlmsg_data(nlh)); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 65 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 66 | genlmsg_end(msg, hdr); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 67 | |
Johannes Berg | 68eb550 | 2013-11-19 15:19:38 +0100 | [diff] [blame] | 68 | return genlmsg_multicast(&nl802154_family, msg, 0, group, GFP_ATOMIC); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 69 | } |
| 70 | |
Dmitry Eremin-Solenikov | 339b4ca | 2009-11-04 18:05:38 +0300 | [diff] [blame] | 71 | struct sk_buff *ieee802154_nl_new_reply(struct genl_info *info, |
Stefan Schmidt | cad865d | 2014-12-04 11:51:58 +0100 | [diff] [blame] | 72 | int flags, u8 req) |
Dmitry Eremin-Solenikov | 339b4ca | 2009-11-04 18:05:38 +0300 | [diff] [blame] | 73 | { |
| 74 | void *hdr; |
Thomas Graf | 58050fc | 2012-06-28 03:57:45 +0000 | [diff] [blame] | 75 | struct sk_buff *msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC); |
Dmitry Eremin-Solenikov | 339b4ca | 2009-11-04 18:05:38 +0300 | [diff] [blame] | 76 | |
| 77 | if (!msg) |
| 78 | return NULL; |
| 79 | |
| 80 | hdr = genlmsg_put_reply(msg, info, |
Varka Bhadram | 4710d80 | 2014-07-02 09:01:09 +0530 | [diff] [blame] | 81 | &nl802154_family, flags, req); |
Dmitry Eremin-Solenikov | 339b4ca | 2009-11-04 18:05:38 +0300 | [diff] [blame] | 82 | if (!hdr) { |
| 83 | nlmsg_free(msg); |
| 84 | return NULL; |
| 85 | } |
| 86 | |
| 87 | return msg; |
| 88 | } |
| 89 | |
| 90 | int ieee802154_nl_reply(struct sk_buff *msg, struct genl_info *info) |
| 91 | { |
Hong zhi guo | 10c9cbb | 2013-03-29 05:09:35 +0000 | [diff] [blame] | 92 | struct nlmsghdr *nlh = nlmsg_hdr(msg); |
| 93 | void *hdr = genlmsg_data(nlmsg_data(nlh)); |
Dmitry Eremin-Solenikov | 339b4ca | 2009-11-04 18:05:38 +0300 | [diff] [blame] | 94 | |
Johannes Berg | 053c095 | 2015-01-16 22:09:00 +0100 | [diff] [blame] | 95 | genlmsg_end(msg, hdr); |
Dmitry Eremin-Solenikov | 339b4ca | 2009-11-04 18:05:38 +0300 | [diff] [blame] | 96 | |
| 97 | return genlmsg_reply(msg, info); |
Dmitry Eremin-Solenikov | 339b4ca | 2009-11-04 18:05:38 +0300 | [diff] [blame] | 98 | } |
| 99 | |
Johannes Berg | 4534de8 | 2013-11-14 17:14:46 +0100 | [diff] [blame] | 100 | static const struct genl_ops ieee8021154_ops[] = { |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 101 | /* see nl-phy.c */ |
| 102 | IEEE802154_DUMP(IEEE802154_LIST_PHY, ieee802154_list_phy, |
| 103 | ieee802154_dump_phy), |
| 104 | IEEE802154_OP(IEEE802154_ADD_IFACE, ieee802154_add_iface), |
| 105 | IEEE802154_OP(IEEE802154_DEL_IFACE, ieee802154_del_iface), |
| 106 | /* see nl-mac.c */ |
| 107 | IEEE802154_OP(IEEE802154_ASSOCIATE_REQ, ieee802154_associate_req), |
| 108 | IEEE802154_OP(IEEE802154_ASSOCIATE_RESP, ieee802154_associate_resp), |
| 109 | IEEE802154_OP(IEEE802154_DISASSOCIATE_REQ, ieee802154_disassociate_req), |
| 110 | IEEE802154_OP(IEEE802154_SCAN_REQ, ieee802154_scan_req), |
| 111 | IEEE802154_OP(IEEE802154_START_REQ, ieee802154_start_req), |
| 112 | IEEE802154_DUMP(IEEE802154_LIST_IFACE, ieee802154_list_iface, |
| 113 | ieee802154_dump_iface), |
Phoebe Buckheister | e462ded | 2014-03-31 21:37:46 +0200 | [diff] [blame] | 114 | IEEE802154_OP(IEEE802154_SET_MACPARAMS, ieee802154_set_macparams), |
Phoebe Buckheister | 3e9c156 | 2014-05-16 17:46:44 +0200 | [diff] [blame] | 115 | IEEE802154_OP(IEEE802154_LLSEC_GETPARAMS, ieee802154_llsec_getparams), |
| 116 | IEEE802154_OP(IEEE802154_LLSEC_SETPARAMS, ieee802154_llsec_setparams), |
| 117 | IEEE802154_DUMP(IEEE802154_LLSEC_LIST_KEY, NULL, |
| 118 | ieee802154_llsec_dump_keys), |
| 119 | IEEE802154_OP(IEEE802154_LLSEC_ADD_KEY, ieee802154_llsec_add_key), |
| 120 | IEEE802154_OP(IEEE802154_LLSEC_DEL_KEY, ieee802154_llsec_del_key), |
| 121 | IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEV, NULL, |
| 122 | ieee802154_llsec_dump_devs), |
| 123 | IEEE802154_OP(IEEE802154_LLSEC_ADD_DEV, ieee802154_llsec_add_dev), |
| 124 | IEEE802154_OP(IEEE802154_LLSEC_DEL_DEV, ieee802154_llsec_del_dev), |
| 125 | IEEE802154_DUMP(IEEE802154_LLSEC_LIST_DEVKEY, NULL, |
| 126 | ieee802154_llsec_dump_devkeys), |
| 127 | IEEE802154_OP(IEEE802154_LLSEC_ADD_DEVKEY, ieee802154_llsec_add_devkey), |
| 128 | IEEE802154_OP(IEEE802154_LLSEC_DEL_DEVKEY, ieee802154_llsec_del_devkey), |
| 129 | IEEE802154_DUMP(IEEE802154_LLSEC_LIST_SECLEVEL, NULL, |
| 130 | ieee802154_llsec_dump_seclevels), |
| 131 | IEEE802154_OP(IEEE802154_LLSEC_ADD_SECLEVEL, |
| 132 | ieee802154_llsec_add_seclevel), |
| 133 | IEEE802154_OP(IEEE802154_LLSEC_DEL_SECLEVEL, |
| 134 | ieee802154_llsec_del_seclevel), |
Johannes Berg | 1c582d9 | 2013-11-14 17:14:41 +0100 | [diff] [blame] | 135 | }; |
| 136 | |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 137 | static const struct genl_multicast_group ieee802154_mcgrps[] = { |
| 138 | [IEEE802154_COORD_MCGRP] = { .name = IEEE802154_MCAST_COORD_NAME, }, |
| 139 | [IEEE802154_BEACON_MCGRP] = { .name = IEEE802154_MCAST_BEACON_NAME, }, |
| 140 | }; |
| 141 | |
Dmitry Eremin-Solenikov | cb6b376 | 2009-09-10 17:50:12 +0400 | [diff] [blame] | 142 | int __init ieee802154_nl_init(void) |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 143 | { |
Johannes Berg | 2a94fe4 | 2013-11-19 15:19:39 +0100 | [diff] [blame] | 144 | return genl_register_family_with_ops_groups(&nl802154_family, |
| 145 | ieee8021154_ops, |
| 146 | ieee802154_mcgrps); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 147 | } |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 148 | |
Alexander Aring | 79fe1a2 | 2014-11-09 08:36:53 +0100 | [diff] [blame] | 149 | void ieee802154_nl_exit(void) |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 150 | { |
Dmitry Eremin-Solenikov | 78fe738 | 2009-09-14 18:17:36 +0400 | [diff] [blame] | 151 | genl_unregister_family(&nl802154_family); |
Sergey Lapin | 2c21d11 | 2009-06-08 12:18:49 +0000 | [diff] [blame] | 152 | } |