2 * Copyright (c) 2007-2012 Nicira, Inc.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of version 2 of the GNU General Public
6 * License as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21 #include <linux/version.h>
22 #include <linux/completion.h>
23 #include <net/genetlink.h>
24 #include "genl_exec.h"
26 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
28 static DEFINE_MUTEX(genl_exec_lock);
30 static genl_exec_func_t genl_exec_function;
31 static int genl_exec_function_ret;
32 static void *genl_exec_data;
33 static struct completion done;
35 static struct sk_buff *genlmsg_skb;
37 static int genl_exec_cmd(struct sk_buff *dummy, struct genl_info *dummy2)
39 genl_exec_function_ret = genl_exec_function(genl_exec_data);
49 static struct genl_family genl_exec_family = {
50 .id = GENL_ID_GENERATE,
51 .name = "ovs_genl_exec",
55 static struct genl_ops genl_exec_ops[] = {
58 .doit = genl_exec_cmd,
59 .flags = CAP_NET_ADMIN,
63 int genl_exec_init(void)
67 err = genl_register_family_with_ops(&genl_exec_family,
68 genl_exec_ops, ARRAY_SIZE(genl_exec_ops));
73 genlmsg_skb = genlmsg_new(0, GFP_KERNEL);
75 genl_unregister_family(&genl_exec_family);
81 void genl_exec_exit(void)
83 kfree_skb(genlmsg_skb);
84 genl_unregister_family(&genl_exec_family);
87 /* genl_lock() is not exported from older kernel.
88 * Following function allows any function to be executed with
91 int genl_exec(genl_exec_func_t func, void *data)
95 mutex_lock(&genl_exec_lock);
97 init_completion(&done);
99 genlmsg_put(genlmsg_skb, 0, 0, &genl_exec_family,
100 NLM_F_REQUEST, GENL_EXEC_RUN);
102 genl_exec_function = func;
103 genl_exec_data = data;
105 /* There is no need to send msg to current namespace. */
106 ret = genlmsg_unicast(&init_net, genlmsg_skb, 0);
109 wait_for_completion(&done);
110 ret = genl_exec_function_ret;
112 pr_err("genl_exec send error %d\n", ret);
115 /* Wait for genetlink to kfree skb. */
116 while (skb_shared(genlmsg_skb))
119 genlmsg_skb->data = genlmsg_skb->head;
120 skb_reset_tail_pointer(genlmsg_skb);
122 mutex_unlock(&genl_exec_lock);
129 int genl_exec(genl_exec_func_t func, void *data)
139 int genl_exec_init(void)
144 void genl_exec_exit(void)