blob: 3cfa76b89a20816e97b31bbbbd18f50c1aed48cd [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>
21#include <linux/major.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070022#include <linux/timer.h>
23#include <linux/string.h>
24#include <linux/sockios.h>
25#include <linux/net.h>
26#include <linux/fcntl.h>
27#include <linux/skbuff.h>
28#include <asm/uaccess.h>
29#include <asm/system.h>
30#include <net/sock.h>
Patrick McHardya3c50292007-03-14 16:39:25 -070031#include <net/netlink.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070032#include <linux/init.h>
Harald Weltef9e815b2005-08-09 19:30:24 -070033
Harald Weltef9e815b2005-08-09 19:30:24 -070034#include <linux/netlink.h>
35#include <linux/netfilter/nfnetlink.h>
36
37MODULE_LICENSE("GPL");
Harald Welte4fdb3bb2005-08-09 19:40:55 -070038MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
39MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
Harald Weltef9e815b2005-08-09 19:30:24 -070040
41static char __initdata nfversion[] = "0.30";
42
Harald Weltef9e815b2005-08-09 19:30:24 -070043static struct sock *nfnl = NULL;
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070044static const struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
Patrick McHardya3c50292007-03-14 16:39:25 -070045static DEFINE_MUTEX(nfnl_mutex);
Harald Weltef9e815b2005-08-09 19:30:24 -070046
Patrick McHardya3c50292007-03-14 16:39:25 -070047static void nfnl_lock(void)
Harald Weltef9e815b2005-08-09 19:30:24 -070048{
Patrick McHardya3c50292007-03-14 16:39:25 -070049 mutex_lock(&nfnl_mutex);
Harald Weltef9e815b2005-08-09 19:30:24 -070050}
51
Patrick McHardya3c50292007-03-14 16:39:25 -070052static int nfnl_trylock(void)
Harald Weltef9e815b2005-08-09 19:30:24 -070053{
Patrick McHardya3c50292007-03-14 16:39:25 -070054 return !mutex_trylock(&nfnl_mutex);
55}
56
57static void __nfnl_unlock(void)
58{
59 mutex_unlock(&nfnl_mutex);
60}
61
62static void nfnl_unlock(void)
63{
64 mutex_unlock(&nfnl_mutex);
65 if (nfnl->sk_receive_queue.qlen)
66 nfnl->sk_data_ready(nfnl, 0);
Harald Weltef9e815b2005-08-09 19:30:24 -070067}
68
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070069int nfnetlink_subsys_register(const struct nfnetlink_subsystem *n)
Harald Weltef9e815b2005-08-09 19:30:24 -070070{
Harald Weltef9e815b2005-08-09 19:30:24 -070071 nfnl_lock();
Harald Welte0ab43f82005-08-09 19:43:44 -070072 if (subsys_table[n->subsys_id]) {
73 nfnl_unlock();
74 return -EBUSY;
75 }
Harald Weltef9e815b2005-08-09 19:30:24 -070076 subsys_table[n->subsys_id] = n;
77 nfnl_unlock();
78
79 return 0;
80}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -070081EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
Harald Weltef9e815b2005-08-09 19:30:24 -070082
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070083int nfnetlink_subsys_unregister(const struct nfnetlink_subsystem *n)
Harald Weltef9e815b2005-08-09 19:30:24 -070084{
Harald Weltef9e815b2005-08-09 19:30:24 -070085 nfnl_lock();
86 subsys_table[n->subsys_id] = NULL;
87 nfnl_unlock();
88
89 return 0;
90}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -070091EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
Harald Weltef9e815b2005-08-09 19:30:24 -070092
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -070093static inline const struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
Harald Weltef9e815b2005-08-09 19:30:24 -070094{
95 u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
96
Pablo Neira Ayusoac0f1d92007-03-14 16:41:28 -070097 if (subsys_id >= NFNL_SUBSYS_COUNT)
Harald Weltef9e815b2005-08-09 19:30:24 -070098 return NULL;
99
100 return subsys_table[subsys_id];
101}
102
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700103static inline const struct nfnl_callback *
104nfnetlink_find_client(u_int16_t type, const struct nfnetlink_subsystem *ss)
Harald Weltef9e815b2005-08-09 19:30:24 -0700105{
106 u_int8_t cb_id = NFNL_MSG_TYPE(type);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800107
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -0700108 if (cb_id >= ss->cb_count)
Harald Weltef9e815b2005-08-09 19:30:24 -0700109 return NULL;
Harald Weltef9e815b2005-08-09 19:30:24 -0700110
111 return &ss->cb[cb_id];
112}
113
Harald Weltef9e815b2005-08-09 19:30:24 -0700114/**
115 * nfnetlink_check_attributes - check and parse nfnetlink attributes
116 *
117 * subsys: nfnl subsystem for which this message is to be parsed
118 * nlmsghdr: netlink message to be checked/parsed
Patrick McHardydf6fb862007-09-28 14:37:03 -0700119 * cda: array of pointers, needs to be at least subsys->attr_count+1 big
Harald Weltef9e815b2005-08-09 19:30:24 -0700120 *
121 */
122static int
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700123nfnetlink_check_attributes(const struct nfnetlink_subsystem *subsys,
Patrick McHardydf6fb862007-09-28 14:37:03 -0700124 struct nlmsghdr *nlh, struct nlattr *cda[])
Harald Weltef9e815b2005-08-09 19:30:24 -0700125{
Pablo Neira Ayusod9e6d022007-03-14 16:41:03 -0700126 int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
Harald Welte927ccbc2005-08-09 20:03:40 -0700127 u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
Pablo Neira Ayusoac6d1412007-03-14 16:45:39 -0700128 u_int16_t attr_count = subsys->cb[cb_id].attr_count;
Harald Weltef9e815b2005-08-09 19:30:24 -0700129
130 /* check attribute lengths. */
Harald Weltea42827b2005-08-09 20:03:54 -0700131 if (likely(nlh->nlmsg_len > min_len)) {
Patrick McHardydf6fb862007-09-28 14:37:03 -0700132 struct nlattr *attr = (void *)nlh + NLMSG_ALIGN(min_len);
Harald Weltef9e815b2005-08-09 19:30:24 -0700133 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
Patrick McHardydf6fb862007-09-28 14:37:03 -0700134 nla_parse(cda, attr_count, attr, attrlen, NULL);
Harald Weltea42827b2005-08-09 20:03:54 -0700135 }
136
137 /* implicit: if nlmsg_len == min_len, we return 0, and an empty
138 * (zeroed) cda[] array. The message is valid, but empty. */
Harald Weltef9e815b2005-08-09 19:30:24 -0700139
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800140 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700141}
142
Patrick McHardya2427692006-03-20 18:03:59 -0800143int nfnetlink_has_listeners(unsigned int group)
144{
145 return netlink_has_listeners(nfnl, group);
146}
147EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
148
Harald Weltef9e815b2005-08-09 19:30:24 -0700149int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
150{
Harald Weltef9e815b2005-08-09 19:30:24 -0700151 int err = 0;
152
Patrick McHardyac6d4392005-08-14 19:29:52 -0700153 NETLINK_CB(skb).dst_group = group;
Harald Weltef9e815b2005-08-09 19:30:24 -0700154 if (echo)
155 atomic_inc(&skb->users);
Patrick McHardy44981212007-02-27 09:56:42 -0800156 netlink_broadcast(nfnl, skb, pid, group, gfp_any());
Harald Weltef9e815b2005-08-09 19:30:24 -0700157 if (echo)
158 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
159
160 return err;
161}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700162EXPORT_SYMBOL_GPL(nfnetlink_send);
Harald Weltef9e815b2005-08-09 19:30:24 -0700163
164int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
165{
166 return netlink_unicast(nfnl, skb, pid, flags);
167}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700168EXPORT_SYMBOL_GPL(nfnetlink_unicast);
Harald Weltef9e815b2005-08-09 19:30:24 -0700169
170/* Process one complete nfnetlink message. */
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700171static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Harald Weltef9e815b2005-08-09 19:30:24 -0700172{
Patrick McHardy7c8d4cb2007-09-28 14:15:45 -0700173 const struct nfnl_callback *nc;
174 const struct nfnetlink_subsystem *ss;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700175 int type, err;
Harald Weltef9e815b2005-08-09 19:30:24 -0700176
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700177 if (security_netlink_recv(skb, CAP_NET_ADMIN))
178 return -EPERM;
Harald Welte37d2e7a2005-11-14 15:24:59 -0800179
Harald Weltef9e815b2005-08-09 19:30:24 -0700180 /* All the messages must at least contain nfgenmsg */
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -0700181 if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg)))
Harald Weltef9e815b2005-08-09 19:30:24 -0700182 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700183
184 type = nlh->nlmsg_type;
185 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700186 if (!ss) {
187#ifdef CONFIG_KMOD
Patrick McHardya3c50292007-03-14 16:39:25 -0700188 /* don't call nfnl_unlock, since it would reenter
Harald Welte37d2e7a2005-11-14 15:24:59 -0800189 * with further packet processing */
Patrick McHardya3c50292007-03-14 16:39:25 -0700190 __nfnl_unlock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800191 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
Patrick McHardya3c50292007-03-14 16:39:25 -0700192 nfnl_lock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800193 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700194 if (!ss)
195#endif
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700196 return -EINVAL;
Harald Welte0ab43f82005-08-09 19:43:44 -0700197 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700198
199 nc = nfnetlink_find_client(type, ss);
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -0700200 if (!nc)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700201 return -EINVAL;
Harald Weltef9e815b2005-08-09 19:30:24 -0700202
Harald Weltef9e815b2005-08-09 19:30:24 -0700203 {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800204 u_int16_t attr_count =
Harald Welte927ccbc2005-08-09 20:03:40 -0700205 ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
Patrick McHardydf6fb862007-09-28 14:37:03 -0700206 struct nlattr *cda[attr_count+1];
Harald Weltef9e815b2005-08-09 19:30:24 -0700207
Patrick McHardydf6fb862007-09-28 14:37:03 -0700208 memset(cda, 0, sizeof(struct nlattr *) * attr_count);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800209
Harald Weltef9e815b2005-08-09 19:30:24 -0700210 err = nfnetlink_check_attributes(ss, nlh, cda);
211 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700212 return err;
213 return nc->call(nfnl, skb, nlh, cda);
Harald Weltef9e815b2005-08-09 19:30:24 -0700214 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700215}
216
Harald Weltef9e815b2005-08-09 19:30:24 -0700217static void nfnetlink_rcv(struct sock *sk, int len)
218{
Patrick McHardy73c36182007-03-14 16:39:45 -0700219 unsigned int qlen = 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700220
Patrick McHardy73c36182007-03-14 16:39:45 -0700221 do {
Patrick McHardya3c50292007-03-14 16:39:25 -0700222 if (nfnl_trylock())
Harald Weltef9e815b2005-08-09 19:30:24 -0700223 return;
Herbert Xu0cfad072007-09-16 16:24:44 -0700224 qlen = netlink_run_queue(sk, qlen, nfnetlink_rcv_msg);
Patrick McHardya3c50292007-03-14 16:39:25 -0700225 __nfnl_unlock();
Patrick McHardy73c36182007-03-14 16:39:45 -0700226 } while (qlen);
Harald Weltef9e815b2005-08-09 19:30:24 -0700227}
228
Adrian Bunk395dde22005-09-05 18:06:45 -0700229static void __exit nfnetlink_exit(void)
Harald Weltef9e815b2005-08-09 19:30:24 -0700230{
231 printk("Removing netfilter NETLINK layer.\n");
232 sock_release(nfnl->sk_socket);
233 return;
234}
235
Adrian Bunk395dde22005-09-05 18:06:45 -0700236static int __init nfnetlink_init(void)
Harald Weltef9e815b2005-08-09 19:30:24 -0700237{
238 printk("Netfilter messages via NETLINK v%s.\n", nfversion);
239
Eric W. Biedermanb4b51022007-09-12 13:05:38 +0200240 nfnl = netlink_kernel_create(&init_net, NETLINK_NETFILTER, NFNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700241 nfnetlink_rcv, NULL, THIS_MODULE);
Harald Weltef9e815b2005-08-09 19:30:24 -0700242 if (!nfnl) {
243 printk(KERN_ERR "cannot initialize nfnetlink!\n");
244 return -1;
245 }
246
247 return 0;
248}
249
250module_init(nfnetlink_init);
251module_exit(nfnetlink_exit);