blob: 791d56bbd74a7613e4b1d87ba74bcf2a8a135e46 [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>
Patrick McHardya3c50292007-03-14 16:39:25 -070027#include <net/netlink.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070028#include <linux/init.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070029
Harald Weltef9e815b2005-08-09 19:30:24 -070030#include <linux/netlink.h>
31#include <linux/netfilter/nfnetlink.h>
32
33MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -070034MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
35MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
Harald Weltef9e815b2005-08-09 19:30:24 -070036
37static char __initdata nfversion[] = "0.30";
38
Eric Dumazet6b75e3e2011-07-18 16:08:07 +020039static const struct nfnetlink_subsystem __rcu *subsys_table[NFNL_SUBSYS_COUNT];
Patrick McHardya3c50292007-03-14 16:39:25 -070040static DEFINE_MUTEX(nfnl_mutex);
Harald Weltef9e815b2005-08-09 19:30:24 -070041
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070042void nfnl_lock(void)
Harald Weltef9e815b2005-08-09 19:30:24 -070043{
Patrick McHardya3c50292007-03-14 16:39:25 -070044 mutex_lock(&nfnl_mutex);
Harald Weltef9e815b2005-08-09 19:30:24 -070045}
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070046EXPORT_SYMBOL_GPL(nfnl_lock);
Harald Weltef9e815b2005-08-09 19:30:24 -070047
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070048void nfnl_unlock(void)
Patrick McHardya3c50292007-03-14 16:39:25 -070049{
50 mutex_unlock(&nfnl_mutex);
51}
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -070052EXPORT_SYMBOL_GPL(nfnl_unlock);
Patrick McHardya3c50292007-03-14 16:39:25 -070053
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070054int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
Harald Weltef9e815b2005-08-09 19:30:24 -070055{
Harald Weltef9e815b2005-08-09 19:30:24 -070056 nfnl_lock();
Harald Welte0ab43f82005-08-09 19:43:44 -070057 if (subsys_table[n->subsys_id]) {
58 nfnl_unlock();
59 return -EBUSY;
60 }
Eric Dumazetcf778b02012-01-12 04:41:32 +000061 rcu_assign_pointer(subsys_table[n->subsys_id], n);
Harald Weltef9e815b2005-08-09 19:30:24 -070062 nfnl_unlock();
63
64 return 0;
65}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -070066EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
Harald Weltef9e815b2005-08-09 19:30:24 -070067
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070068int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
Harald Weltef9e815b2005-08-09 19:30:24 -070069{
Harald Weltef9e815b2005-08-09 19:30:24 -070070 nfnl_lock();
71 subsys_table[n->subsys_id] = NULL;
72 nfnl_unlock();
Eric Dumazet6b75e3e2011-07-18 16:08:07 +020073 synchronize_rcu();
Harald Weltef9e815b2005-08-09 19:30:24 -070074 return 0;
75}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -070076EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
Harald Weltef9e815b2005-08-09 19:30:24 -070077
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070078static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
Harald Weltef9e815b2005-08-09 19:30:24 -070079{
80 u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
81
Pablo Neira Ayusoac0f1d92007-03-14 16:41:28 -070082 if (subsys_id >= NFNL_SUBSYS_COUNT)
Harald Weltef9e815b2005-08-09 19:30:24 -070083 return NULL;
84
Eric Dumazet6b75e3e2011-07-18 16:08:07 +020085 return rcu_dereference(subsys_table[subsys_id]);
Harald Weltef9e815b2005-08-09 19:30:24 -070086}
87
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070088static inline const struct nfnl_callback *
89nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss)
Harald Weltef9e815b2005-08-09 19:30:24 -070090{
91 u_int8_t cb_id = NFNL_MSG_TYPE(type);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080092
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -070093 if (cb_id >= ss->cb_count)
Harald Weltef9e815b2005-08-09 19:30:24 -070094 return NULL;
Harald Weltef9e815b2005-08-09 19:30:24 -070095
96 return &ss->cb[cb_id];
97}
98
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +010099int nfnetlink_has_listeners(struct net *net, unsigned int group)
Patrick McHardya2427692006-03-20 18:03:59 -0800100{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100101 return netlink_has_listeners(net->nfnl, group);
Patrick McHardya2427692006-03-20 18:03:59 -0800102}
103EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
104
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100105int nfnetlink_send(struct sk_buff *skb, struct net *net, u32 pid,
Eric Dumazet95c96172012-04-15 05:58:06 +0000106 unsigned int group, int echo, gfp_t flags)
Harald Weltef9e815b2005-08-09 19:30:24 -0700107{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100108 return nlmsg_notify(net->nfnl, skb, pid, group, echo, flags);
Harald Weltef9e815b2005-08-09 19:30:24 -0700109}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700110EXPORT_SYMBOL_GPL(nfnetlink_send);
Harald Weltef9e815b2005-08-09 19:30:24 -0700111
Pablo Neira Ayuso37b7ef72010-03-16 13:30:21 +0000112int nfnetlink_set_err(struct net *net, u32 pid, u32 group, int error)
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +0100113{
Pablo Neira Ayuso37b7ef72010-03-16 13:30:21 +0000114 return netlink_set_err(net->nfnl, pid, group, error);
Pablo Neira Ayusodd5b6ce2009-03-23 13:21:06 +0100115}
116EXPORT_SYMBOL_GPL(nfnetlink_set_err);
117
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100118int nfnetlink_unicast(struct sk_buff *skb, struct net *net, u_int32_t pid, int flags)
Harald Weltef9e815b2005-08-09 19:30:24 -0700119{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100120 return netlink_unicast(net->nfnl, skb, pid, flags);
Harald Weltef9e815b2005-08-09 19:30:24 -0700121}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700122EXPORT_SYMBOL_GPL(nfnetlink_unicast);
Harald Weltef9e815b2005-08-09 19:30:24 -0700123
124/* Process one complete nfnetlink message. */
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700125static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Harald Weltef9e815b2005-08-09 19:30:24 -0700126{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100127 struct net *net = sock_net(skb->sk);
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700128 const struct nfnl_callback *nc;
129 const struct nfnetlink_subsystem *ss;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700130 int type, err;
Harald Weltef9e815b2005-08-09 19:30:24 -0700131
Eric Parisfd778462012-01-03 12:25:16 -0500132 if (!capable(CAP_NET_ADMIN))
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700133 return -EPERM;
Harald Welte37d2e7a2005-11-14 15:24:59 -0800134
Harald Weltef9e815b2005-08-09 19:30:24 -0700135 /* All the messages must at least contain nfgenmsg */
Pablo Neira Ayusof49c8572009-06-02 20:03:33 +0200136 if (nlh->nlmsg_len < NLMSG_LENGTH(sizeof(struct nfgenmsg)))
Harald Weltef9e815b2005-08-09 19:30:24 -0700137 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700138
139 type = nlh->nlmsg_type;
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700140replay:
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200141 rcu_read_lock();
Harald Weltef9e815b2005-08-09 19:30:24 -0700142 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700143 if (!ss) {
Johannes Berg95a5afc2008-10-16 15:24:51 -0700144#ifdef CONFIG_MODULES
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200145 rcu_read_unlock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800146 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200147 rcu_read_lock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800148 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700149 if (!ss)
150#endif
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200151 {
152 rcu_read_unlock();
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700153 return -EINVAL;
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200154 }
Harald Welte0ab43f82005-08-09 19:43:44 -0700155 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700156
157 nc = nfnetlink_find_client(type, ss);
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200158 if (!nc) {
159 rcu_read_unlock();
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700160 return -EINVAL;
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200161 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700162
Harald Weltef9e815b2005-08-09 19:30:24 -0700163 {
Patrick McHardye3730572007-09-28 14:38:52 -0700164 int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
165 u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
Pablo Neira Ayusof49c8572009-06-02 20:03:33 +0200166 struct nlattr *cda[ss->cb[cb_id].attr_count + 1];
167 struct nlattr *attr = (void *)nlh + min_len;
168 int attrlen = nlh->nlmsg_len - min_len;
Harald Weltef9e815b2005-08-09 19:30:24 -0700169
Pablo Neira Ayusof49c8572009-06-02 20:03:33 +0200170 err = nla_parse(cda, ss->cb[cb_id].attr_count,
171 attr, attrlen, ss->cb[cb_id].policy);
Tomasz Bursztyka4009e182012-06-28 02:57:49 +0000172 if (err < 0) {
173 rcu_read_unlock();
Pablo Neira Ayusof49c8572009-06-02 20:03:33 +0200174 return err;
Tomasz Bursztyka4009e182012-06-28 02:57:49 +0000175 }
Patrick McHardye3730572007-09-28 14:38:52 -0700176
Eric Dumazet6b75e3e2011-07-18 16:08:07 +0200177 if (nc->call_rcu) {
178 err = nc->call_rcu(net->nfnl, skb, nlh,
179 (const struct nlattr **)cda);
180 rcu_read_unlock();
181 } else {
182 rcu_read_unlock();
183 nfnl_lock();
184 if (rcu_dereference_protected(
185 subsys_table[NFNL_SUBSYS_ID(type)],
186 lockdep_is_held(&nfnl_mutex)) != ss ||
187 nfnetlink_find_client(type, ss) != nc)
188 err = -EAGAIN;
189 else
190 err = nc->call(net->nfnl, skb, nlh,
191 (const struct nlattr **)cda);
192 nfnl_unlock();
193 }
Pablo Neira Ayusoe6a7d3c2008-10-14 11:58:31 -0700194 if (err == -EAGAIN)
195 goto replay;
196 return err;
Harald Weltef9e815b2005-08-09 19:30:24 -0700197 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700198}
199
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700200static void nfnetlink_rcv(struct sk_buff *skb)
Harald Weltef9e815b2005-08-09 19:30:24 -0700201{
Denis V. Lunevcd40b7d2007-10-10 21:15:29 -0700202 netlink_rcv_skb(skb, &nfnetlink_rcv_msg);
Harald Weltef9e815b2005-08-09 19:30:24 -0700203}
204
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100205static int __net_init nfnetlink_net_init(struct net *net)
Harald Weltef9e815b2005-08-09 19:30:24 -0700206{
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100207 struct sock *nfnl;
208
209 nfnl = netlink_kernel_create(net, NETLINK_NETFILTER, NFNLGRP_MAX,
210 nfnetlink_rcv, NULL, THIS_MODULE);
211 if (!nfnl)
212 return -ENOMEM;
213 net->nfnl_stash = nfnl;
Eric Dumazetcf778b02012-01-12 04:41:32 +0000214 rcu_assign_pointer(net->nfnl, nfnl);
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100215 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700216}
217
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100218static void __net_exit nfnetlink_net_exit_batch(struct list_head *net_exit_list)
219{
220 struct net *net;
221
222 list_for_each_entry(net, net_exit_list, exit_list)
Stephen Hemmingera9b3cd72011-08-01 16:19:00 +0000223 RCU_INIT_POINTER(net->nfnl, NULL);
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100224 synchronize_net();
225 list_for_each_entry(net, net_exit_list, exit_list)
226 netlink_kernel_release(net->nfnl_stash);
227}
228
229static struct pernet_operations nfnetlink_net_ops = {
230 .init = nfnetlink_net_init,
231 .exit_batch = nfnetlink_net_exit_batch,
232};
233
Adrian Bunk395dde22005-09-05 18:06:45 -0700234static int __init nfnetlink_init(void)
Harald Weltef9e815b2005-08-09 19:30:24 -0700235{
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200236 pr_info("Netfilter messages via NETLINK v%s.\n", nfversion);
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100237 return register_pernet_subsys(&nfnetlink_net_ops);
Harald Weltef9e815b2005-08-09 19:30:24 -0700238}
239
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100240static void __exit nfnetlink_exit(void)
241{
Stephen Hemminger654d0fb2010-05-13 15:02:08 +0200242 pr_info("Removing netfilter NETLINK layer.\n");
Alexey Dobriyancd8c20b2010-01-13 16:02:14 +0100243 unregister_pernet_subsys(&nfnetlink_net_ops);
244}
Harald Weltef9e815b2005-08-09 19:30:24 -0700245module_init(nfnetlink_init);
246module_exit(nfnetlink_exit);