blob: 8797e6953ef25cd7ca98548bf8b3c0355ac57e98 [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;
44static 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
69int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
70{
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
83int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
84{
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
93static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
94{
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
103static inline struct nfnl_callback *
104nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
105{
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
114void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
115 const void *data)
116{
117 struct nfattr *nfa;
118 int size = NFA_LENGTH(attrlen);
119
120 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
121 nfa->nfa_type = attrtype;
122 nfa->nfa_len = size;
123 memcpy(NFA_DATA(nfa), data, attrlen);
Harald Welte080774a2005-08-09 19:32:58 -0700124 memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
Harald Weltef9e815b2005-08-09 19:30:24 -0700125}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700126EXPORT_SYMBOL_GPL(__nfa_fill);
Harald Weltef9e815b2005-08-09 19:30:24 -0700127
Harald Weltea2506c02005-11-09 12:59:13 -0800128void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
Harald Weltef9e815b2005-08-09 19:30:24 -0700129{
130 memset(tb, 0, sizeof(struct nfattr *) * maxattr);
131
132 while (NFA_OK(nfa, len)) {
Harald Welteebe0bbf2005-10-10 20:52:19 -0700133 unsigned flavor = NFA_TYPE(nfa);
Harald Weltef9e815b2005-08-09 19:30:24 -0700134 if (flavor && flavor <= maxattr)
135 tb[flavor-1] = nfa;
136 nfa = NFA_NEXT(nfa, len);
137 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700138}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700139EXPORT_SYMBOL_GPL(nfattr_parse);
Harald Weltef9e815b2005-08-09 19:30:24 -0700140
141/**
142 * nfnetlink_check_attributes - check and parse nfnetlink attributes
143 *
144 * subsys: nfnl subsystem for which this message is to be parsed
145 * nlmsghdr: netlink message to be checked/parsed
146 * cda: array of pointers, needs to be at least subsys->attr_count big
147 *
148 */
149static int
150nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
151 struct nlmsghdr *nlh, struct nfattr *cda[])
152{
Pablo Neira Ayusod9e6d022007-03-14 16:41:03 -0700153 int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
Harald Welte927ccbc2005-08-09 20:03:40 -0700154 u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
Pablo Neira Ayusoac6d1412007-03-14 16:45:39 -0700155 u_int16_t attr_count = subsys->cb[cb_id].attr_count;
Harald Weltef9e815b2005-08-09 19:30:24 -0700156
157 /* check attribute lengths. */
Harald Weltea42827b2005-08-09 20:03:54 -0700158 if (likely(nlh->nlmsg_len > min_len)) {
Harald Weltef9e815b2005-08-09 19:30:24 -0700159 struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
160 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
Pablo Neira Ayusoac6d1412007-03-14 16:45:39 -0700161 nfattr_parse(cda, attr_count, attr, attrlen);
Harald Weltea42827b2005-08-09 20:03:54 -0700162 }
163
164 /* implicit: if nlmsg_len == min_len, we return 0, and an empty
165 * (zeroed) cda[] array. The message is valid, but empty. */
Harald Weltef9e815b2005-08-09 19:30:24 -0700166
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800167 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700168}
169
Patrick McHardya2427692006-03-20 18:03:59 -0800170int nfnetlink_has_listeners(unsigned int group)
171{
172 return netlink_has_listeners(nfnl, group);
173}
174EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
175
Harald Weltef9e815b2005-08-09 19:30:24 -0700176int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
177{
Harald Weltef9e815b2005-08-09 19:30:24 -0700178 int err = 0;
179
Patrick McHardyac6d4392005-08-14 19:29:52 -0700180 NETLINK_CB(skb).dst_group = group;
Harald Weltef9e815b2005-08-09 19:30:24 -0700181 if (echo)
182 atomic_inc(&skb->users);
Patrick McHardy44981212007-02-27 09:56:42 -0800183 netlink_broadcast(nfnl, skb, pid, group, gfp_any());
Harald Weltef9e815b2005-08-09 19:30:24 -0700184 if (echo)
185 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
186
187 return err;
188}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700189EXPORT_SYMBOL_GPL(nfnetlink_send);
Harald Weltef9e815b2005-08-09 19:30:24 -0700190
191int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
192{
193 return netlink_unicast(nfnl, skb, pid, flags);
194}
Pablo Neira Ayusof4bc1772007-03-14 16:42:11 -0700195EXPORT_SYMBOL_GPL(nfnetlink_unicast);
Harald Weltef9e815b2005-08-09 19:30:24 -0700196
197/* Process one complete nfnetlink message. */
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700198static int nfnetlink_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
Harald Weltef9e815b2005-08-09 19:30:24 -0700199{
200 struct nfnl_callback *nc;
201 struct nfnetlink_subsystem *ss;
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700202 int type, err;
Harald Weltef9e815b2005-08-09 19:30:24 -0700203
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700204 if (security_netlink_recv(skb, CAP_NET_ADMIN))
205 return -EPERM;
Harald Welte37d2e7a2005-11-14 15:24:59 -0800206
Harald Weltef9e815b2005-08-09 19:30:24 -0700207 /* All the messages must at least contain nfgenmsg */
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -0700208 if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg)))
Harald Weltef9e815b2005-08-09 19:30:24 -0700209 return 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700210
211 type = nlh->nlmsg_type;
212 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700213 if (!ss) {
214#ifdef CONFIG_KMOD
Patrick McHardya3c50292007-03-14 16:39:25 -0700215 /* don't call nfnl_unlock, since it would reenter
Harald Welte37d2e7a2005-11-14 15:24:59 -0800216 * with further packet processing */
Patrick McHardya3c50292007-03-14 16:39:25 -0700217 __nfnl_unlock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800218 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
Patrick McHardya3c50292007-03-14 16:39:25 -0700219 nfnl_lock();
Harald Welte37d2e7a2005-11-14 15:24:59 -0800220 ss = nfnetlink_get_subsys(type);
Harald Welte0ab43f82005-08-09 19:43:44 -0700221 if (!ss)
222#endif
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700223 return -EINVAL;
Harald Welte0ab43f82005-08-09 19:43:44 -0700224 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700225
226 nc = nfnetlink_find_client(type, ss);
Pablo Neira Ayuso67ca3962007-03-14 16:40:38 -0700227 if (!nc)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700228 return -EINVAL;
Harald Weltef9e815b2005-08-09 19:30:24 -0700229
Harald Weltef9e815b2005-08-09 19:30:24 -0700230 {
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800231 u_int16_t attr_count =
Harald Welte927ccbc2005-08-09 20:03:40 -0700232 ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
233 struct nfattr *cda[attr_count];
Harald Weltef9e815b2005-08-09 19:30:24 -0700234
Harald Welte927ccbc2005-08-09 20:03:40 -0700235 memset(cda, 0, sizeof(struct nfattr *) * attr_count);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800236
Harald Weltef9e815b2005-08-09 19:30:24 -0700237 err = nfnetlink_check_attributes(ss, nlh, cda);
238 if (err < 0)
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700239 return err;
240 return nc->call(nfnl, skb, nlh, cda);
Harald Weltef9e815b2005-08-09 19:30:24 -0700241 }
Harald Weltef9e815b2005-08-09 19:30:24 -0700242}
243
Harald Weltef9e815b2005-08-09 19:30:24 -0700244static void nfnetlink_rcv(struct sock *sk, int len)
245{
Patrick McHardy73c36182007-03-14 16:39:45 -0700246 unsigned int qlen = 0;
Harald Weltef9e815b2005-08-09 19:30:24 -0700247
Patrick McHardy73c36182007-03-14 16:39:45 -0700248 do {
Patrick McHardya3c50292007-03-14 16:39:25 -0700249 if (nfnl_trylock())
Harald Weltef9e815b2005-08-09 19:30:24 -0700250 return;
Patrick McHardy73c36182007-03-14 16:39:45 -0700251 netlink_run_queue(sk, &qlen, nfnetlink_rcv_msg);
Patrick McHardya3c50292007-03-14 16:39:25 -0700252 __nfnl_unlock();
Patrick McHardy73c36182007-03-14 16:39:45 -0700253 } while (qlen);
Harald Weltef9e815b2005-08-09 19:30:24 -0700254}
255
Adrian Bunk395dde22005-09-05 18:06:45 -0700256static void __exit nfnetlink_exit(void)
Harald Weltef9e815b2005-08-09 19:30:24 -0700257{
258 printk("Removing netfilter NETLINK layer.\n");
259 sock_release(nfnl->sk_socket);
260 return;
261}
262
Adrian Bunk395dde22005-09-05 18:06:45 -0700263static int __init nfnetlink_init(void)
Harald Weltef9e815b2005-08-09 19:30:24 -0700264{
265 printk("Netfilter messages via NETLINK v%s.\n", nfversion);
266
Patrick McHardy06628602005-08-15 12:33:26 -0700267 nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
Patrick McHardyaf65bdf2007-04-20 14:14:21 -0700268 nfnetlink_rcv, NULL, THIS_MODULE);
Harald Weltef9e815b2005-08-09 19:30:24 -0700269 if (!nfnl) {
270 printk(KERN_ERR "cannot initialize nfnetlink!\n");
271 return -1;
272 }
273
274 return 0;
275}
276
277module_init(nfnetlink_init);
278module_exit(nfnetlink_exit);