blob: 572d87dc116ffa838d2f9f8838129156add7284e [file] [log] [blame]
Harald Weltef9e815b2005-08-09 19:30:24 -07001/* 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 <jschlst@samba.org>,
5 * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -07006 * (C) 2005,2007 by Pablo Neira Ayuso <pablo@netfilter.org>
Harald Weltef9e815b2005-08-09 19:30:24 -07007 *
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
Harald Weltef9e815b2005-08-09 19:30:24 -070017#include <linux/module.h>
18#include <linux/types.h>
19#include <linux/socket.h>
20#include <linux/kernel.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070021#include <linux/string.h>
22#include <linux/sockios.h>
23#include <linux/net.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070024#include <linux/skbuff.h>
25#include <asm/uaccess.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070026#include <net/sock.h>
27#include <linux/init.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070028
Hong zhi guo573ce262013-03-27 06:47:04 +000029#include <net/netlink.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070030#include <linux/netfilter/nfnetlink.h>
31
32MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -070033MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
34MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
Harald Weltef9e815b2005-08-09 19:30:24 -070035
36static char __initdata nfversion[] = "0.30";
37
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010038static struct {
39 struct mutex mutex;
40 const struct nfnetlink_subsystem __rcu *subsys;
41} table[NFNL_SUBSYS_COUNT];
Harald Weltef9e815b2005-08-09 19:30:24 -070042
Pablo Neira Ayuso03292742012-06-29 06:15:22 +000043static const int nfnl_group2type[NFNLGRP_MAX+1] = {
44 [NFNLGRP_CONNTRACK_NEW] = NFNL_SUBSYS_CTNETLINK,
45 [NFNLGRP_CONNTRACK_UPDATE] = NFNL_SUBSYS_CTNETLINK,
46 [NFNLGRP_CONNTRACK_DESTROY] = NFNL_SUBSYS_CTNETLINK,
47 [NFNLGRP_CONNTRACK_EXP_NEW] = NFNL_SUBSYS_CTNETLINK_EXP,
48 [NFNLGRP_CONNTRACK_EXP_UPDATE] = NFNL_SUBSYS_CTNETLINK_EXP,
49 [NFNLGRP_CONNTRACK_EXP_DESTROY] = NFNL_SUBSYS_CTNETLINK_EXP,
50};
51
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010052void nfnl_lock(__u8 subsys_id)
Harald Weltef9e815b2005-08-09 19:30:24 -070053{
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010054 mutex_lock(&table[subsys_id].mutex);
Harald Weltef9e815b2005-08-09 19:30:24 -070055}
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070056EXPORT_SYMBOL_GPL(nfnl_lock);
Harald Weltef9e815b2005-08-09 19:30:24 -070057
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010058void nfnl_unlock(__u8 subsys_id)
Patrick McHardya3c50292007-03-14 16:39:25 -070059{
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010060 mutex_unlock(&table[subsys_id].mutex);
Patrick McHardya3c50292007-03-14 16:39:25 -070061}
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070062EXPORT_SYMBOL_GPL(nfnl_unlock);
Patrick McHardya3c50292007-03-14 16:39:25 -070063
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070064int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
Harald Weltef9e815b2005-08-09 19:30:24 -070065{
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010066 nfnl_lock(n->subsys_id);
67 if (table[n->subsys_id].subsys) {
68 nfnl_unlock(n->subsys_id);
Harald Welte0ab43f82005-08-09 19:43:44 -070069 return -EBUSY;
70 }
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010071 rcu_assign_pointer(table[n->subsys_id].subsys, n);
72 nfnl_unlock(n->subsys_id);
Harald Weltef9e815b2005-08-09 19:30:24 -070073
74 return 0;
75}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -070076EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
Harald Weltef9e815b2005-08-09 19:30:24 -070077
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070078int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
Harald Weltef9e815b2005-08-09 19:30:24 -070079{
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010080 nfnl_lock(n->subsys_id);
81 table[n->subsys_id].subsys = NULL;
82 nfnl_unlock(n->subsys_id);
Eric Dumazet6b75e3e2011-07-18 16:08:07 +020083 synchronize_rcu();
Harald Weltef9e815b2005-08-09 19:30:24 -070084 return 0;
85}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -070086EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
Harald Weltef9e815b2005-08-09 19:30:24 -070087
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070088static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
Harald Weltef9e815b2005-08-09 19:30:24 -070089{
90 u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
91
Pablo Neira Ayusoac0f1d92007-03-14 16:41:28 -070092 if (subsys_id >= NFNL_SUBSYS_COUNT)
Harald Weltef9e815b2005-08-09 19:30:24 -070093 return NULL;
94
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +010095 return rcu_dereference(table[subsys_id].subsys);
Harald Weltef9e815b2005-08-09 19:30:24 -070096}
97
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070098static inline const struct nfnl_callback *
99nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss)
Harald Weltef9e815b2005-08-09 19:30:24 -0700100{
101 u_int8_t cb_id = NFNL_MSG_TYPE(type);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800102
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -0700103 if (cb_id >= ss->cb_count)
Harald Weltef9e815b2005-08-09 19:30:24 -0700104 return NULL;
Harald Weltef9e815b2005-08-09 19:30:24 -0700105
106 return &ss->cb[cb_id];
107}
108
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100109int nfnetlink_has_listeners(struct net *net, unsigned int group)
Patrick McHardya2427692006-03-20 18:03:59 -0800110{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100111 return netlink_has_listeners(net->nfnl, group);
Patrick McHardya2427692006-03-20 18:03:59 -0800112}
113EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
114
Patrick McHardy3ab1f682013-04-17 06:47:09 +0000115struct sk_buff *nfnetlink_alloc_skb(struct net *net, unsigned int size,
116 u32 dst_portid, gfp_t gfp_mask)
117{
118 return netlink_alloc_skb(net->nfnl, size, dst_portid, gfp_mask);
119}
120EXPORT_SYMBOL_GPL(nfnetlink_alloc_skb);
121
Patrick McHardyec464e52013-04-17 06:47:08 +0000122int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 portid,
Eric Dumazet95c96172012-04-15 05:58:06 +0000123 unsigned int group, int echo, gfp_t flags)
Harald Weltef9e815b2005-08-09 19:30:24 -0700124{
Patrick McHardyec464e52013-04-17 06:47:08 +0000125 return nlmsg_notify(net->nfnl, skb, portid, group, echo, flags);
Harald Weltef9e815b2005-08-09 19:30:24 -0700126}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700127EXPORT_SYMBOL_GPL(nfnetlink_send);
Harald Weltef9e815b2005-08-09 19:30:24 -0700128
Patrick McHardyec464e52013-04-17 06:47:08 +0000129int nfnetlink_set_err(struct net *net, u32 portid, u32 group, int error)
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +0100130{
Patrick McHardyec464e52013-04-17 06:47:08 +0000131 return netlink_set_err(net->nfnl, portid, group, error);
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +0100132}
133EXPORT_SYMBOL_GPL(nfnetlink_set_err);
134
Patrick McHardyec464e52013-04-17 06:47:08 +0000135int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u32 portid,
136 int flags)
Harald Weltef9e815b2005-08-09 19:30:24 -0700137{
Patrick McHardyec464e52013-04-17 06:47:08 +0000138 return netlink_unicast(net->nfnl, skb, portid, flags);
Harald Weltef9e815b2005-08-09 19:30:24 -0700139}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700140EXPORT_SYMBOL_GPL(nfnetlink_unicast);
Harald Weltef9e815b2005-08-09 19:30:24 -0700141
142/* Process one complete nfnetlink message. */
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700143static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Harald Weltef9e815b2005-08-09 19:30:24 -0700144{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100145 struct net *net = sock_net(skb->sk);
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700146 const struct nfnl_callback *nc;
147 const struct nfnetlink_subsystem *ss;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700148 int type, err;
Harald Weltef9e815b2005-08-09 19:30:24 -0700149
Eric W. Biedermandf008c92012-11-16 03:03:07 +0000150 if (!ns_capable(net->user_ns, CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700151 return -EPERM;
Harald Welte37d2e7a2005-11-14 15:24:59 -0800152
Harald Weltef9e815b2005-08-09 19:30:24 -0700153 /* All the messages must at least contain nfgenmsg */
Hong zhi guo573ce262013-03-27 06:47:04 +0000154 if (nlmsg_len(nlh) < sizeof(struct nfgenmsg))
Harald Weltef9e815b2005-08-09 19:30:24 -0700155 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700156
157 type = nlh->nlmsg_type;
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700158replay:
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200159 rcu_read_lock();
Harald Weltef9e815b2005-08-09 19:30:24 -0700160 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700161 if (!ss) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700162#ifdef CONFIG_MODULES
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200163 rcu_read_unlock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800164 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200165 rcu_read_lock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800166 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700167 if (!ss)
168#endif
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200169 {
170 rcu_read_unlock();
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700171 return -EINVAL;
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200172 }
Harald Welte0ab43f82005-08-09 19:43:44 -0700173 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700174
175 nc = nfnetlink_find_client(type, ss);
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200176 if (!nc) {
177 rcu_read_unlock();
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700178 return -EINVAL;
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200179 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700180
Harald Weltef9e815b2005-08-09 19:30:24 -0700181 {
Hong zhi guo573ce262013-03-27 06:47:04 +0000182 int min_len = nlmsg_total_size(sizeof(struct nfgenmsg));
Patrick McHardye3730572007-09-28 14:38:52 -0700183 u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
Pablo Neira Ayusof49c8572009-06-02 20:03:33 +0200184 struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
185 struct nlattr *attr = (void *)nlh + min_len;
186 int attrlen = nlh->nlmsg_len - min_len;
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +0100187 __u8 subsys_id = NFNL_SUBSYS_ID(type);
Harald Weltef9e815b2005-08-09 19:30:24 -0700188
Pablo Neira Ayusof49c8572009-06-02 20:03:33 +0200189 err = nla_parse(cda, ss->cb[cb_id].attr_count,
190 attr, attrlen, ss->cb[cb_id].policy);
Tomasz Bursztyka4009e182012-06-28 02:57:49 +0000191 if (err < 0) {
192 rcu_read_unlock();
Pablo Neira Ayusof49c8572009-06-02 20:03:33 +0200193 return err;
Tomasz Bursztyka4009e182012-06-28 02:57:49 +0000194 }
Patrick McHardye3730572007-09-28 14:38:52 -0700195
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200196 if (nc->call_rcu) {
197 err = nc->call_rcu(net->nfnl, skb, nlh,
198 (const struct nlattr **)cda);
199 rcu_read_unlock();
200 } else {
201 rcu_read_unlock();
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +0100202 nfnl_lock(subsys_id);
203 if (rcu_dereference_protected(table[subsys_id].subsys,
Paul Bolle9df9e7832013-03-04 02:45:41 +0000204 lockdep_is_held(&table[subsys_id].mutex)) != ss ||
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200205 nfnetlink_find_client(type, ss) != nc)
206 err = -EAGAIN;
Tomasz Bursztyka59560a32012-06-28 02:57:47 +0000207 else if (nc->call)
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200208 err = nc->call(net->nfnl, skb, nlh,
209 (const struct nlattr **)cda);
Tomasz Bursztyka59560a32012-06-28 02:57:47 +0000210 else
211 err = -EINVAL;
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +0100212 nfnl_unlock(subsys_id);
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200213 }
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700214 if (err == -EAGAIN)
215 goto replay;
216 return err;
Harald Weltef9e815b2005-08-09 19:30:24 -0700217 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700218}
219
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700220static void nfnetlink_rcv(struct sk_buff *skb)
Harald Weltef9e815b2005-08-09 19:30:24 -0700221{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700222 netlink_rcv_skb(skb, &nfnetlink_rcv_msg);
Harald Weltef9e815b2005-08-09 19:30:24 -0700223}
224
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000225#ifdef CONFIG_MODULES
226static void nfnetlink_bind(int group)
227{
228 const struct nfnetlink_subsystem *ss;
229 int type = nfnl_group2type[group];
230
231 rcu_read_lock();
232 ss = nfnetlink_get_subsys(type);
233 if (!ss) {
234 rcu_read_unlock();
235 request_module("nfnetlink-subsys-%d", type);
236 return;
237 }
238 rcu_read_unlock();
239}
240#endif
241
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100242static int __net_init nfnetlink_net_init(struct net *net)
Harald Weltef9e815b2005-08-09 19:30:24 -0700243{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100244 struct sock *nfnl;
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000245 struct netlink_kernel_cfg cfg = {
246 .groups = NFNLGRP_MAX,
247 .input = nfnetlink_rcv,
Pablo Neira Ayuso03292742012-06-29 06:15:22 +0000248#ifdef CONFIG_MODULES
249 .bind = nfnetlink_bind,
250#endif
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000251 };
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100252
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000253 nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, &cfg);
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100254 if (!nfnl)
255 return -ENOMEM;
256 net->nfnl_stash = nfnl;
Eric Dumazetcf778b02012-01-12 04:41:32 +0000257 rcu_assign_pointer(net->nfnl, nfnl);
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100258 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700259}
260
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100261static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
262{
263 struct net *net;
264
265 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000266 RCU_INIT_POINTER(net->nfnl, NULL);
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100267 synchronize_net();
268 list_for_each_entry(net, net_exit_list, exit_list)
269 netlink_kernel_release(net->nfnl_stash);
270}
271
272static struct pernet_operations nfnetlink_net_ops = {
273 .init = nfnetlink_net_init,
274 .exit_batch = nfnetlink_net_exit_batch,
275};
276
Adrian Bunk395dde22005-09-05 18:06:45 -0700277static int __init nfnetlink_init(void)
Harald Weltef9e815b2005-08-09 19:30:24 -0700278{
Pablo Neira Ayusoc14b78e2013-02-05 01:50:26 +0100279 int i;
280
281 for (i=0; i<NFNL_SUBSYS_COUNT; i++)
282 mutex_init(&table[i].mutex);
283
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200284 pr_info("Netfilter messages via NETLINK v%s.\n", nfversion);
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100285 return register_pernet_subsys(&nfnetlink_net_ops);
Harald Weltef9e815b2005-08-09 19:30:24 -0700286}
287
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100288static void __exit nfnetlink_exit(void)
289{
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200290 pr_info("Removing netfilter NETLINK layer.\n");
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100291 unregister_pernet_subsys(&nfnetlink_net_ops);
292}
Harald Weltef9e815b2005-08-09 19:30:24 -0700293module_init(nfnetlink_init);
294module_exit(nfnetlink_exit);