xref: /linux/net/tipc/netlink.c (revision 2cf8aa19fe8bec578b707daa383ebff80e3f81a1)
1b97bf3fdSPer Liden /*
2b97bf3fdSPer Liden  * net/tipc/netlink.c: TIPC configuration handling
3b97bf3fdSPer Liden  *
4593a5f22SPer Liden  * Copyright (c) 2005-2006, Ericsson AB
559f0c452SAllan Stephens  * Copyright (c) 2005-2007, Wind River Systems
6b97bf3fdSPer Liden  * All rights reserved.
7b97bf3fdSPer Liden  *
8b97bf3fdSPer Liden  * Redistribution and use in source and binary forms, with or without
9b97bf3fdSPer Liden  * modification, are permitted provided that the following conditions are met:
10b97bf3fdSPer Liden  *
119ea1fd3cSPer Liden  * 1. Redistributions of source code must retain the above copyright
129ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer.
139ea1fd3cSPer Liden  * 2. Redistributions in binary form must reproduce the above copyright
149ea1fd3cSPer Liden  *    notice, this list of conditions and the following disclaimer in the
159ea1fd3cSPer Liden  *    documentation and/or other materials provided with the distribution.
169ea1fd3cSPer Liden  * 3. Neither the names of the copyright holders nor the names of its
179ea1fd3cSPer Liden  *    contributors may be used to endorse or promote products derived from
189ea1fd3cSPer Liden  *    this software without specific prior written permission.
199ea1fd3cSPer Liden  *
209ea1fd3cSPer Liden  * Alternatively, this software may be distributed under the terms of the
219ea1fd3cSPer Liden  * GNU General Public License ("GPL") version 2 as published by the Free
229ea1fd3cSPer Liden  * Software Foundation.
23b97bf3fdSPer Liden  *
24b97bf3fdSPer Liden  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25b97bf3fdSPer Liden  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26b97bf3fdSPer Liden  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27b97bf3fdSPer Liden  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28b97bf3fdSPer Liden  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29b97bf3fdSPer Liden  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30b97bf3fdSPer Liden  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31b97bf3fdSPer Liden  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32b97bf3fdSPer Liden  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33b97bf3fdSPer Liden  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34b97bf3fdSPer Liden  * POSSIBILITY OF SUCH DAMAGE.
35b97bf3fdSPer Liden  */
36b97bf3fdSPer Liden 
37b97bf3fdSPer Liden #include "core.h"
38b97bf3fdSPer Liden #include "config.h"
39b97bf3fdSPer Liden #include <net/genetlink.h>
40b97bf3fdSPer Liden 
41b97bf3fdSPer Liden static int handle_cmd(struct sk_buff *skb, struct genl_info *info)
42b97bf3fdSPer Liden {
43b97bf3fdSPer Liden 	struct sk_buff *rep_buf;
44b97bf3fdSPer Liden 	struct nlmsghdr *rep_nlh;
45b97bf3fdSPer Liden 	struct nlmsghdr *req_nlh = info->nlhdr;
46b97bf3fdSPer Liden 	struct tipc_genlmsghdr *req_userhdr = info->userhdr;
471dba9743SPer Liden 	int hdr_space = NLMSG_SPACE(GENL_HDRLEN + TIPC_GENL_HDRLEN);
4859f0c452SAllan Stephens 	u16 cmd;
49b97bf3fdSPer Liden 
50b97bf3fdSPer Liden 	if ((req_userhdr->cmd & 0xC000) && (!capable(CAP_NET_ADMIN)))
5159f0c452SAllan Stephens 		cmd = TIPC_CMD_NOT_NET_ADMIN;
52b97bf3fdSPer Liden 	else
5359f0c452SAllan Stephens 		cmd = req_userhdr->cmd;
5459f0c452SAllan Stephens 
5559f0c452SAllan Stephens 	rep_buf = tipc_cfg_do_cmd(req_userhdr->dest, cmd,
56b97bf3fdSPer Liden 			NLMSG_DATA(req_nlh) + GENL_HDRLEN + TIPC_GENL_HDRLEN,
57b97bf3fdSPer Liden 			NLMSG_PAYLOAD(req_nlh, GENL_HDRLEN + TIPC_GENL_HDRLEN),
58b97bf3fdSPer Liden 			hdr_space);
59b97bf3fdSPer Liden 
60b97bf3fdSPer Liden 	if (rep_buf) {
61b97bf3fdSPer Liden 		skb_push(rep_buf, hdr_space);
62b529ccf2SArnaldo Carvalho de Melo 		rep_nlh = nlmsg_hdr(rep_buf);
63b97bf3fdSPer Liden 		memcpy(rep_nlh, req_nlh, hdr_space);
64b97bf3fdSPer Liden 		rep_nlh->nlmsg_len = rep_buf->len;
65134e6375SJohannes Berg 		genlmsg_unicast(&init_net, rep_buf, NETLINK_CB(skb).pid);
66b97bf3fdSPer Liden 	}
67b97bf3fdSPer Liden 
68b97bf3fdSPer Liden 	return 0;
69b97bf3fdSPer Liden }
70b97bf3fdSPer Liden 
71acb0a200SMichał Mirosław static struct genl_family tipc_genl_family = {
721dba9743SPer Liden 	.id		= GENL_ID_GENERATE,
73b97bf3fdSPer Liden 	.name		= TIPC_GENL_NAME,
74b97bf3fdSPer Liden 	.version	= TIPC_GENL_VERSION,
75b97bf3fdSPer Liden 	.hdrsize	= TIPC_GENL_HDRLEN,
76b97bf3fdSPer Liden 	.maxattr	= 0,
77b97bf3fdSPer Liden };
78b97bf3fdSPer Liden 
79acb0a200SMichał Mirosław static struct genl_ops tipc_genl_ops = {
80b97bf3fdSPer Liden 	.cmd		= TIPC_GENL_CMD,
81b97bf3fdSPer Liden 	.doit		= handle_cmd,
82b97bf3fdSPer Liden };
83b97bf3fdSPer Liden 
84acb0a200SMichał Mirosław static int tipc_genl_family_registered;
85b97bf3fdSPer Liden 
864323add6SPer Liden int tipc_netlink_start(void)
87b97bf3fdSPer Liden {
88acb0a200SMichał Mirosław 	int res;
89b70e4f45SJon Maloy 
90acb0a200SMichał Mirosław 	res = genl_register_family_with_ops(&tipc_genl_family,
91acb0a200SMichał Mirosław 		&tipc_genl_ops, 1);
92acb0a200SMichał Mirosław 	if (res) {
93*2cf8aa19SErik Hugne 		pr_err("Failed to register netlink interface\n");
94acb0a200SMichał Mirosław 		return res;
95acb0a200SMichał Mirosław 	}
96acb0a200SMichał Mirosław 
97acb0a200SMichał Mirosław 	tipc_genl_family_registered = 1;
98acb0a200SMichał Mirosław 	return 0;
99b97bf3fdSPer Liden }
100b97bf3fdSPer Liden 
1014323add6SPer Liden void tipc_netlink_stop(void)
102b97bf3fdSPer Liden {
103acb0a200SMichał Mirosław 	if (!tipc_genl_family_registered)
104acb0a200SMichał Mirosław 		return;
105acb0a200SMichał Mirosław 
106acb0a200SMichał Mirosław 	genl_unregister_family(&tipc_genl_family);
107acb0a200SMichał Mirosław 	tipc_genl_family_registered = 0;
108b97bf3fdSPer Liden }
109