]>
Commit | Line | Data |
---|---|---|
f9e815b3 HW |
1 | /* Netfilter messages via netlink socket. Allows for user space |
2 | * protocol helpers and general trouble making from userspace. | |
3 | * | |
4 | * (C) 2001 by Jay Schulist <[email protected]>, | |
5 | * (C) 2002-2005 by Harald Welte <[email protected]> | |
67ca3966 | 6 | * (C) 2005,2007 by Pablo Neira Ayuso <[email protected]> |
f9e815b3 HW |
7 | * |
8 | * Initial netfilter messages via netlink development funded and | |
9 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) | |
10 | * | |
11 | * Further development of this code funded by Astaro AG (http://www.astaro.com) | |
12 | * | |
13 | * This software may be used and distributed according to the terms | |
14 | * of the GNU General Public License, incorporated herein by reference. | |
15 | */ | |
16 | ||
f9e815b3 HW |
17 | #include <linux/module.h> |
18 | #include <linux/types.h> | |
19 | #include <linux/socket.h> | |
20 | #include <linux/kernel.h> | |
f9e815b3 HW |
21 | #include <linux/string.h> |
22 | #include <linux/sockios.h> | |
23 | #include <linux/net.h> | |
f9e815b3 HW |
24 | #include <linux/skbuff.h> |
25 | #include <asm/uaccess.h> | |
f9e815b3 | 26 | #include <net/sock.h> |
a3c5029c | 27 | #include <net/netlink.h> |
f9e815b3 | 28 | #include <linux/init.h> |
f9e815b3 | 29 | |
f9e815b3 HW |
30 | #include <linux/netlink.h> |
31 | #include <linux/netfilter/nfnetlink.h> | |
32 | ||
33 | MODULE_LICENSE("GPL"); | |
4fdb3bb7 HW |
34 | MODULE_AUTHOR("Harald Welte <[email protected]>"); |
35 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER); | |
f9e815b3 HW |
36 | |
37 | static char __initdata nfversion[] = "0.30"; | |
38 | ||
6b75e3e8 | 39 | static const struct nfnetlink_subsystem __rcu *subsys_table[NFNL_SUBSYS_COUNT]; |
a3c5029c | 40 | static DEFINE_MUTEX(nfnl_mutex); |
f9e815b3 | 41 | |
03292745 PNA |
42 | static const int nfnl_group2type[NFNLGRP_MAX+1] = { |
43 | [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK, | |
44 | [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK, | |
45 | [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK, | |
46 | [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP, | |
47 | [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP, | |
48 | [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP, | |
49 | }; | |
50 | ||
e6a7d3c0 | 51 | void nfnl_lock(void) |
f9e815b3 | 52 | { |
a3c5029c | 53 | mutex_lock(&nfnl_mutex); |
f9e815b3 | 54 | } |
e6a7d3c0 | 55 | EXPORT_SYMBOL_GPL(nfnl_lock); |
f9e815b3 | 56 | |
e6a7d3c0 | 57 | void nfnl_unlock(void) |
a3c5029c PM |
58 | { |
59 | mutex_unlock(&nfnl_mutex); | |
f9e815b3 | 60 | } |
e6a7d3c0 | 61 | EXPORT_SYMBOL_GPL(nfnl_unlock); |
f9e815b3 | 62 | |
7c8d4cb4 | 63 | int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n) |
f9e815b3 | 64 | { |
f9e815b3 | 65 | nfnl_lock(); |
0ab43f84 HW |
66 | if (subsys_table[n->subsys_id]) { |
67 | nfnl_unlock(); | |
68 | return -EBUSY; | |
69 | } | |
cf778b00 | 70 | rcu_assign_pointer(subsys_table[n->subsys_id], n); |
f9e815b3 HW |
71 | nfnl_unlock(); |
72 | ||
73 | return 0; | |
74 | } | |
f4bc177f | 75 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_register); |
f9e815b3 | 76 | |
7c8d4cb4 | 77 | int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n) |
f9e815b3 | 78 | { |
f9e815b3 HW |
79 | nfnl_lock(); |
80 | subsys_table[n->subsys_id] = NULL; | |
81 | nfnl_unlock(); | |
6b75e3e8 | 82 | synchronize_rcu(); |
f9e815b3 HW |
83 | return 0; |
84 | } | |
f4bc177f | 85 | EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister); |
f9e815b3 | 86 | |
7c8d4cb4 | 87 | static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type) |
f9e815b3 HW |
88 | { |
89 | u_int8_t subsys_id = NFNL_SUBSYS_ID(type); | |
90 | ||
ac0f1d98 | 91 | if (subsys_id >= NFNL_SUBSYS_COUNT) |
f9e815b3 HW |
92 | return NULL; |
93 | ||
6b75e3e8 | 94 | return rcu_dereference(subsys_table[subsys_id]); |
f9e815b3 HW |
95 | } |
96 | ||
7c8d4cb4 PM |
97 | static inline const struct nfnl_callback * |
98 | nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss) | |
f9e815b3 HW |
99 | { |
100 | u_int8_t cb_id = NFNL_MSG_TYPE(type); | |
601e68e1 | 101 | |
67ca3966 | 102 | if (cb_id >= ss->cb_count) |
f9e815b3 | 103 | return NULL; |
f9e815b3 HW |
104 | |
105 | return &ss->cb[cb_id]; | |
106 | } | |
107 | ||
cd8c20b6 | 108 | int nfnetlink_has_listeners(struct net *net, unsigned int group) |
a2427692 | 109 | { |
cd8c20b6 | 110 | return netlink_has_listeners(net->nfnl, group); |
a2427692 PM |
111 | } |
112 | EXPORT_SYMBOL_GPL(nfnetlink_has_listeners); | |
113 | ||
cd8c20b6 | 114 | int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 pid, |
95c96174 | 115 | unsigned int group, int echo, gfp_t flags) |
f9e815b3 | 116 | { |
cd8c20b6 | 117 | return nlmsg_notify(net->nfnl, skb, pid, group, echo, flags); |
f9e815b3 | 118 | } |
f4bc177f | 119 | EXPORT_SYMBOL_GPL(nfnetlink_send); |
f9e815b3 | 120 | |
37b7ef72 | 121 | int nfnetlink_set_err(struct net *net, u32 pid, u32 group, int error) |
dd5b6ce6 | 122 | { |
37b7ef72 | 123 | return netlink_set_err(net->nfnl, pid, group, error); |
dd5b6ce6 PNA |
124 | } |
125 | EXPORT_SYMBOL_GPL(nfnetlink_set_err); | |
126 | ||
cd8c20b6 | 127 | int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u_int32_t pid, int flags) |
f9e815b3 | 128 | { |
cd8c20b6 | 129 | return netlink_unicast(net->nfnl, skb, pid, flags); |
f9e815b3 | 130 | } |
f4bc177f | 131 | EXPORT_SYMBOL_GPL(nfnetlink_unicast); |
f9e815b3 HW |
132 | |
133 | /* Process one complete nfnetlink message. */ | |
1d00a4eb | 134 | static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
f9e815b3 | 135 | { |
cd8c20b6 | 136 | struct net *net = sock_net(skb->sk); |
7c8d4cb4 PM |
137 | const struct nfnl_callback *nc; |
138 | const struct nfnetlink_subsystem *ss; | |
1d00a4eb | 139 | int type, err; |
f9e815b3 | 140 | |
fd778461 | 141 | if (!capable(CAP_NET_ADMIN)) |
1d00a4eb | 142 | return -EPERM; |
37d2e7a2 | 143 | |
f9e815b3 | 144 | /* All the messages must at least contain nfgenmsg */ |
f49c857f | 145 | if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nfgenmsg))) |
f9e815b3 | 146 | return 0; |
f9e815b3 HW |
147 | |
148 | type = nlh->nlmsg_type; | |
e6a7d3c0 | 149 | replay: |
6b75e3e8 | 150 | rcu_read_lock(); |
f9e815b3 | 151 | ss = nfnetlink_get_subsys(type); |
0ab43f84 | 152 | if (!ss) { |
95a5afca | 153 | #ifdef CONFIG_MODULES |
6b75e3e8 | 154 | rcu_read_unlock(); |
37d2e7a2 | 155 | request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type)); |
6b75e3e8 | 156 | rcu_read_lock(); |
37d2e7a2 | 157 | ss = nfnetlink_get_subsys(type); |
0ab43f84 HW |
158 | if (!ss) |
159 | #endif | |
6b75e3e8 ED |
160 | { |
161 | rcu_read_unlock(); | |
1d00a4eb | 162 | return -EINVAL; |
6b75e3e8 | 163 | } |
0ab43f84 | 164 | } |
f9e815b3 HW |
165 | |
166 | nc = nfnetlink_find_client(type, ss); | |
6b75e3e8 ED |
167 | if (!nc) { |
168 | rcu_read_unlock(); | |
1d00a4eb | 169 | return -EINVAL; |
6b75e3e8 | 170 | } |
f9e815b3 | 171 | |
f9e815b3 | 172 | { |
e3730578 PM |
173 | int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg)); |
174 | u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type); | |
f49c857f PNA |
175 | struct nlattr *cda[ss->cb[cb_id].attr_count + 1]; |
176 | struct nlattr *attr = (void *)nlh + min_len; | |
177 | int attrlen = nlh->nlmsg_len - min_len; | |
178 | ||
179 | err = nla_parse(cda, ss->cb[cb_id].attr_count, | |
180 | attr, attrlen, ss->cb[cb_id].policy); | |
4009e188 TB |
181 | if (err < 0) { |
182 | rcu_read_unlock(); | |
f49c857f | 183 | return err; |
4009e188 | 184 | } |
601e68e1 | 185 | |
6b75e3e8 ED |
186 | if (nc->call_rcu) { |
187 | err = nc->call_rcu(net->nfnl, skb, nlh, | |
188 | (const struct nlattr **)cda); | |
189 | rcu_read_unlock(); | |
190 | } else { | |
191 | rcu_read_unlock(); | |
192 | nfnl_lock(); | |
193 | if (rcu_dereference_protected( | |
194 | subsys_table[NFNL_SUBSYS_ID(type)], | |
195 | lockdep_is_held(&nfnl_mutex)) != ss || | |
196 | nfnetlink_find_client(type, ss) != nc) | |
197 | err = -EAGAIN; | |
59560a38 | 198 | else if (nc->call) |
6b75e3e8 ED |
199 | err = nc->call(net->nfnl, skb, nlh, |
200 | (const struct nlattr **)cda); | |
59560a38 TB |
201 | else |
202 | err = -EINVAL; | |
6b75e3e8 ED |
203 | nfnl_unlock(); |
204 | } | |
e6a7d3c0 PNA |
205 | if (err == -EAGAIN) |
206 | goto replay; | |
207 | return err; | |
f9e815b3 | 208 | } |
f9e815b3 HW |
209 | } |
210 | ||
cd40b7d3 | 211 | static void nfnetlink_rcv(struct sk_buff *skb) |
f9e815b3 | 212 | { |
cd40b7d3 | 213 | netlink_rcv_skb(skb, &nfnetlink_rcv_msg); |
f9e815b3 HW |
214 | } |
215 | ||
03292745 PNA |
216 | #ifdef CONFIG_MODULES |
217 | static void nfnetlink_bind(int group) | |
218 | { | |
219 | const struct nfnetlink_subsystem *ss; | |
220 | int type = nfnl_group2type[group]; | |
221 | ||
222 | rcu_read_lock(); | |
223 | ss = nfnetlink_get_subsys(type); | |
224 | if (!ss) { | |
225 | rcu_read_unlock(); | |
226 | request_module("nfnetlink-subsys-%d", type); | |
227 | return; | |
228 | } | |
229 | rcu_read_unlock(); | |
230 | } | |
231 | #endif | |
232 | ||
cd8c20b6 | 233 | static int __net_init nfnetlink_net_init(struct net *net) |
f9e815b3 | 234 | { |
cd8c20b6 | 235 | struct sock *nfnl; |
a31f2d17 PNA |
236 | struct netlink_kernel_cfg cfg = { |
237 | .groups = NFNLGRP_MAX, | |
238 | .input = nfnetlink_rcv, | |
03292745 PNA |
239 | #ifdef CONFIG_MODULES |
240 | .bind = nfnetlink_bind, | |
241 | #endif | |
a31f2d17 | 242 | }; |
cd8c20b6 | 243 | |
a31f2d17 | 244 | nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, THIS_MODULE, &cfg); |
cd8c20b6 AD |
245 | if (!nfnl) |
246 | return -ENOMEM; | |
247 | net->nfnl_stash = nfnl; | |
cf778b00 | 248 | rcu_assign_pointer(net->nfnl, nfnl); |
cd8c20b6 | 249 | return 0; |
f9e815b3 HW |
250 | } |
251 | ||
cd8c20b6 | 252 | static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list) |
f9e815b3 | 253 | { |
cd8c20b6 | 254 | struct net *net; |
f9e815b3 | 255 | |
cd8c20b6 | 256 | list_for_each_entry(net, net_exit_list, exit_list) |
a9b3cd7f | 257 | RCU_INIT_POINTER(net->nfnl, NULL); |
cd8c20b6 AD |
258 | synchronize_net(); |
259 | list_for_each_entry(net, net_exit_list, exit_list) | |
260 | netlink_kernel_release(net->nfnl_stash); | |
261 | } | |
f9e815b3 | 262 | |
cd8c20b6 AD |
263 | static struct pernet_operations nfnetlink_net_ops = { |
264 | .init = nfnetlink_net_init, | |
265 | .exit_batch = nfnetlink_net_exit_batch, | |
266 | }; | |
267 | ||
268 | static int __init nfnetlink_init(void) | |
269 | { | |
654d0fbd | 270 | pr_info("Netfilter messages via NETLINK v%s.\n", nfversion); |
cd8c20b6 | 271 | return register_pernet_subsys(&nfnetlink_net_ops); |
f9e815b3 HW |
272 | } |
273 | ||
cd8c20b6 AD |
274 | static void __exit nfnetlink_exit(void) |
275 | { | |
654d0fbd | 276 | pr_info("Removing netfilter NETLINK layer.\n"); |
cd8c20b6 AD |
277 | unregister_pernet_subsys(&nfnetlink_net_ops); |
278 | } | |
f9e815b3 HW |
279 | module_init(nfnetlink_init); |
280 | module_exit(nfnetlink_exit); |