[NETFILTER]: nfnetlink: remove duplicate checks in nfnetlink_check_attributes
[linux-2.6/mini2440.git] / net / netfilter / nfnetlink.c
blob9d33807ec16de06980362c151fb01e1f2b0c9796
1 /* Netfilter messages via netlink socket. Allows for user space
2 * protocol helpers and general trouble making from userspace.
4 * (C) 2001 by Jay Schulist <jschlst@samba.org>,
5 * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org>
6 * (C) 2005,2007 by Pablo Neira Ayuso <pablo@netfilter.org>
8 * Initial netfilter messages via netlink development funded and
9 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11 * Further development of this code funded by Astaro AG (http://www.astaro.com)
13 * This software may be used and distributed according to the terms
14 * of the GNU General Public License, incorporated herein by reference.
17 #include <linux/module.h>
18 #include <linux/types.h>
19 #include <linux/socket.h>
20 #include <linux/kernel.h>
21 #include <linux/major.h>
22 #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>
31 #include <net/netlink.h>
32 #include <linux/init.h>
33 #include <linux/spinlock.h>
35 #include <linux/netfilter.h>
36 #include <linux/netlink.h>
37 #include <linux/netfilter/nfnetlink.h>
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
41 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
43 static char __initdata nfversion[] = "0.30";
45 static struct sock *nfnl = NULL;
46 static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
47 static DEFINE_MUTEX(nfnl_mutex);
49 static void nfnl_lock(void)
51 mutex_lock(&nfnl_mutex);
54 static int nfnl_trylock(void)
56 return !mutex_trylock(&nfnl_mutex);
59 static void __nfnl_unlock(void)
61 mutex_unlock(&nfnl_mutex);
64 static void nfnl_unlock(void)
66 mutex_unlock(&nfnl_mutex);
67 if (nfnl->sk_receive_queue.qlen)
68 nfnl->sk_data_ready(nfnl, 0);
71 int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
73 nfnl_lock();
74 if (subsys_table[n->subsys_id]) {
75 nfnl_unlock();
76 return -EBUSY;
78 subsys_table[n->subsys_id] = n;
79 nfnl_unlock();
81 return 0;
84 int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
86 nfnl_lock();
87 subsys_table[n->subsys_id] = NULL;
88 nfnl_unlock();
90 return 0;
93 static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
95 u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
97 if (subsys_id >= NFNL_SUBSYS_COUNT
98 || subsys_table[subsys_id] == NULL)
99 return NULL;
101 return subsys_table[subsys_id];
104 static inline struct nfnl_callback *
105 nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
107 u_int8_t cb_id = NFNL_MSG_TYPE(type);
109 if (cb_id >= ss->cb_count)
110 return NULL;
112 return &ss->cb[cb_id];
115 void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
116 const void *data)
118 struct nfattr *nfa;
119 int size = NFA_LENGTH(attrlen);
121 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
122 nfa->nfa_type = attrtype;
123 nfa->nfa_len = size;
124 memcpy(NFA_DATA(nfa), data, attrlen);
125 memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
128 void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
130 memset(tb, 0, sizeof(struct nfattr *) * maxattr);
132 while (NFA_OK(nfa, len)) {
133 unsigned flavor = NFA_TYPE(nfa);
134 if (flavor && flavor <= maxattr)
135 tb[flavor-1] = nfa;
136 nfa = NFA_NEXT(nfa, len);
141 * nfnetlink_check_attributes - check and parse nfnetlink attributes
143 * subsys: nfnl subsystem for which this message is to be parsed
144 * nlmsghdr: netlink message to be checked/parsed
145 * cda: array of pointers, needs to be at least subsys->attr_count big
148 static int
149 nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
150 struct nlmsghdr *nlh, struct nfattr *cda[])
152 int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
153 u_int16_t attr_count;
154 u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
156 attr_count = subsys->cb[cb_id].attr_count;
157 memset(cda, 0, sizeof(struct nfattr *) * attr_count);
159 /* check attribute lengths. */
160 if (likely(nlh->nlmsg_len > min_len)) {
161 struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
162 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
164 while (NFA_OK(attr, attrlen)) {
165 unsigned flavor = NFA_TYPE(attr);
166 if (flavor) {
167 if (flavor > attr_count)
168 return -EINVAL;
169 cda[flavor - 1] = attr;
171 attr = NFA_NEXT(attr, attrlen);
175 /* implicit: if nlmsg_len == min_len, we return 0, and an empty
176 * (zeroed) cda[] array. The message is valid, but empty. */
178 return 0;
181 int nfnetlink_has_listeners(unsigned int group)
183 return netlink_has_listeners(nfnl, group);
185 EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
187 int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
189 int err = 0;
191 NETLINK_CB(skb).dst_group = group;
192 if (echo)
193 atomic_inc(&skb->users);
194 netlink_broadcast(nfnl, skb, pid, group, gfp_any());
195 if (echo)
196 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
198 return err;
201 int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
203 return netlink_unicast(nfnl, skb, pid, flags);
206 /* Process one complete nfnetlink message. */
207 static int nfnetlink_rcv_msg(struct sk_buff *skb,
208 struct nlmsghdr *nlh, int *errp)
210 struct nfnl_callback *nc;
211 struct nfnetlink_subsystem *ss;
212 int type, err = 0;
214 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
215 *errp = -EPERM;
216 return -1;
219 /* Only requests are handled by kernel now. */
220 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
221 return 0;
223 /* All the messages must at least contain nfgenmsg */
224 if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg)))
225 return 0;
227 type = nlh->nlmsg_type;
228 ss = nfnetlink_get_subsys(type);
229 if (!ss) {
230 #ifdef CONFIG_KMOD
231 /* don't call nfnl_unlock, since it would reenter
232 * with further packet processing */
233 __nfnl_unlock();
234 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
235 nfnl_lock();
236 ss = nfnetlink_get_subsys(type);
237 if (!ss)
238 #endif
239 goto err_inval;
242 nc = nfnetlink_find_client(type, ss);
243 if (!nc)
244 goto err_inval;
247 u_int16_t attr_count =
248 ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
249 struct nfattr *cda[attr_count];
251 memset(cda, 0, sizeof(struct nfattr *) * attr_count);
253 err = nfnetlink_check_attributes(ss, nlh, cda);
254 if (err < 0)
255 goto err_inval;
257 err = nc->call(nfnl, skb, nlh, cda, errp);
258 *errp = err;
259 return err;
262 err_inval:
263 *errp = -EINVAL;
264 return -1;
267 static void nfnetlink_rcv(struct sock *sk, int len)
269 unsigned int qlen = 0;
271 do {
272 if (nfnl_trylock())
273 return;
274 netlink_run_queue(sk, &qlen, nfnetlink_rcv_msg);
275 __nfnl_unlock();
276 } while (qlen);
279 static void __exit nfnetlink_exit(void)
281 printk("Removing netfilter NETLINK layer.\n");
282 sock_release(nfnl->sk_socket);
283 return;
286 static int __init nfnetlink_init(void)
288 printk("Netfilter messages via NETLINK v%s.\n", nfversion);
290 nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
291 nfnetlink_rcv, THIS_MODULE);
292 if (!nfnl) {
293 printk(KERN_ERR "cannot initialize nfnetlink!\n");
294 return -1;
297 return 0;
300 module_init(nfnetlink_init);
301 module_exit(nfnetlink_exit);
303 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
304 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
305 EXPORT_SYMBOL_GPL(nfnetlink_send);
306 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
307 EXPORT_SYMBOL_GPL(nfattr_parse);
308 EXPORT_SYMBOL_GPL(__nfa_fill);