2 * Copyright (c) 2007-2012 Nicira Networks.
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 <net/genetlink.h>
23 #include "genl_exec.h"
25 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,35)
27 static DEFINE_MUTEX(genl_exec_lock);
29 static genl_exec_func_t genl_exec_function;
30 static int genl_exec_function_ret;
31 static void *genl_exec_data;
32 static struct completion done;
34 static struct sk_buff *genlmsg_skb;
36 static int genl_exec_cmd(struct sk_buff *dummy, struct genl_info *dummy2)
38 genl_exec_function_ret = genl_exec_function(genl_exec_data);
48 static struct genl_family genl_exec_family = {
49 .id = GENL_ID_GENERATE,
50 .name = "ovs_genl_exec",
54 static struct genl_ops genl_exec_ops[] = {
57 .doit = genl_exec_cmd,
58 .flags = CAP_NET_ADMIN,
62 int genl_exec_init(void)
66 err = genl_register_family_with_ops(&genl_exec_family,
67 genl_exec_ops, ARRAY_SIZE(genl_exec_ops));
72 genlmsg_skb = genlmsg_new(0, GFP_KERNEL);
74 genl_unregister_family(&genl_exec_family);
80 void genl_exec_exit(void)
82 kfree_skb(genlmsg_skb);
83 genl_unregister_family(&genl_exec_family);
86 /* genl_lock() is not exported from older kernel.
87 * Following function allows any function to be executed with
90 int genl_exec(genl_exec_func_t func, void *data)
94 mutex_lock(&genl_exec_lock);
96 init_completion(&done);
98 genlmsg_put(genlmsg_skb, 0, 0, &genl_exec_family,
99 NLM_F_REQUEST, GENL_EXEC_RUN);
101 genl_exec_function = func;
102 genl_exec_data = data;
104 /* There is no need to send msg to current namespace. */
105 ret = genlmsg_unicast(&init_net, genlmsg_skb, 0);
108 wait_for_completion(&done);
109 ret = genl_exec_function_ret;
111 pr_err("genl_exec send error %d\n", ret);
114 /* Wait for genetlink to kfree skb. */
115 while (skb_shared(genlmsg_skb))
118 genlmsg_skb->data = genlmsg_skb->head;
119 skb_reset_tail_pointer(genlmsg_skb);
121 mutex_unlock(&genl_exec_lock);
128 int genl_exec(genl_exec_func_t func, void *data)
138 int genl_exec_init(void)
143 void genl_exec_exit(void)