[NETFILTER]: nfnetlink: remove unrequired check in nfnetlink_get_subsys
[deliverable/linux.git] / net / netfilter / nfnetlink.c
1 /* 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>
6 * (C) 2005,2007 by Pablo Neira Ayuso <pablo@netfilter.org>
7 *
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
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>
34
35 #include <linux/netfilter.h>
36 #include <linux/netlink.h>
37 #include <linux/netfilter/nfnetlink.h>
38
39 MODULE_LICENSE("GPL");
40 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
41 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
42
43 static char __initdata nfversion[] = "0.30";
44
45 static struct sock *nfnl = NULL;
46 static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
47 static DEFINE_MUTEX(nfnl_mutex);
48
49 static void nfnl_lock(void)
50 {
51 mutex_lock(&nfnl_mutex);
52 }
53
54 static int nfnl_trylock(void)
55 {
56 return !mutex_trylock(&nfnl_mutex);
57 }
58
59 static void __nfnl_unlock(void)
60 {
61 mutex_unlock(&nfnl_mutex);
62 }
63
64 static void nfnl_unlock(void)
65 {
66 mutex_unlock(&nfnl_mutex);
67 if (nfnl->sk_receive_queue.qlen)
68 nfnl->sk_data_ready(nfnl, 0);
69 }
70
71 int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
72 {
73 nfnl_lock();
74 if (subsys_table[n->subsys_id]) {
75 nfnl_unlock();
76 return -EBUSY;
77 }
78 subsys_table[n->subsys_id] = n;
79 nfnl_unlock();
80
81 return 0;
82 }
83
84 int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
85 {
86 nfnl_lock();
87 subsys_table[n->subsys_id] = NULL;
88 nfnl_unlock();
89
90 return 0;
91 }
92
93 static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
94 {
95 u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
96
97 if (subsys_id >= NFNL_SUBSYS_COUNT)
98 return NULL;
99
100 return subsys_table[subsys_id];
101 }
102
103 static inline struct nfnl_callback *
104 nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
105 {
106 u_int8_t cb_id = NFNL_MSG_TYPE(type);
107
108 if (cb_id >= ss->cb_count)
109 return NULL;
110
111 return &ss->cb[cb_id];
112 }
113
114 void __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);
124 memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
125 }
126
127 void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
128 {
129 memset(tb, 0, sizeof(struct nfattr *) * maxattr);
130
131 while (NFA_OK(nfa, len)) {
132 unsigned flavor = NFA_TYPE(nfa);
133 if (flavor && flavor <= maxattr)
134 tb[flavor-1] = nfa;
135 nfa = NFA_NEXT(nfa, len);
136 }
137 }
138
139 /**
140 * nfnetlink_check_attributes - check and parse nfnetlink attributes
141 *
142 * subsys: nfnl subsystem for which this message is to be parsed
143 * nlmsghdr: netlink message to be checked/parsed
144 * cda: array of pointers, needs to be at least subsys->attr_count big
145 *
146 */
147 static int
148 nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
149 struct nlmsghdr *nlh, struct nfattr *cda[])
150 {
151 int min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
152 u_int16_t attr_count;
153 u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
154
155 attr_count = subsys->cb[cb_id].attr_count;
156 memset(cda, 0, sizeof(struct nfattr *) * attr_count);
157
158 /* check attribute lengths. */
159 if (likely(nlh->nlmsg_len > min_len)) {
160 struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
161 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
162
163 while (NFA_OK(attr, attrlen)) {
164 unsigned flavor = NFA_TYPE(attr);
165 if (flavor) {
166 if (flavor > attr_count)
167 return -EINVAL;
168 cda[flavor - 1] = attr;
169 }
170 attr = NFA_NEXT(attr, attrlen);
171 }
172 }
173
174 /* implicit: if nlmsg_len == min_len, we return 0, and an empty
175 * (zeroed) cda[] array. The message is valid, but empty. */
176
177 return 0;
178 }
179
180 int nfnetlink_has_listeners(unsigned int group)
181 {
182 return netlink_has_listeners(nfnl, group);
183 }
184 EXPORT_SYMBOL_GPL(nfnetlink_has_listeners);
185
186 int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
187 {
188 int err = 0;
189
190 NETLINK_CB(skb).dst_group = group;
191 if (echo)
192 atomic_inc(&skb->users);
193 netlink_broadcast(nfnl, skb, pid, group, gfp_any());
194 if (echo)
195 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
196
197 return err;
198 }
199
200 int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
201 {
202 return netlink_unicast(nfnl, skb, pid, flags);
203 }
204
205 /* Process one complete nfnetlink message. */
206 static int nfnetlink_rcv_msg(struct sk_buff *skb,
207 struct nlmsghdr *nlh, int *errp)
208 {
209 struct nfnl_callback *nc;
210 struct nfnetlink_subsystem *ss;
211 int type, err = 0;
212
213 if (security_netlink_recv(skb, CAP_NET_ADMIN)) {
214 *errp = -EPERM;
215 return -1;
216 }
217
218 /* Only requests are handled by kernel now. */
219 if (!(nlh->nlmsg_flags & NLM_F_REQUEST))
220 return 0;
221
222 /* All the messages must at least contain nfgenmsg */
223 if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg)))
224 return 0;
225
226 type = nlh->nlmsg_type;
227 ss = nfnetlink_get_subsys(type);
228 if (!ss) {
229 #ifdef CONFIG_KMOD
230 /* don't call nfnl_unlock, since it would reenter
231 * with further packet processing */
232 __nfnl_unlock();
233 request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
234 nfnl_lock();
235 ss = nfnetlink_get_subsys(type);
236 if (!ss)
237 #endif
238 goto err_inval;
239 }
240
241 nc = nfnetlink_find_client(type, ss);
242 if (!nc)
243 goto err_inval;
244
245 {
246 u_int16_t attr_count =
247 ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
248 struct nfattr *cda[attr_count];
249
250 memset(cda, 0, sizeof(struct nfattr *) * attr_count);
251
252 err = nfnetlink_check_attributes(ss, nlh, cda);
253 if (err < 0)
254 goto err_inval;
255
256 err = nc->call(nfnl, skb, nlh, cda, errp);
257 *errp = err;
258 return err;
259 }
260
261 err_inval:
262 *errp = -EINVAL;
263 return -1;
264 }
265
266 static void nfnetlink_rcv(struct sock *sk, int len)
267 {
268 unsigned int qlen = 0;
269
270 do {
271 if (nfnl_trylock())
272 return;
273 netlink_run_queue(sk, &qlen, nfnetlink_rcv_msg);
274 __nfnl_unlock();
275 } while (qlen);
276 }
277
278 static void __exit nfnetlink_exit(void)
279 {
280 printk("Removing netfilter NETLINK layer.\n");
281 sock_release(nfnl->sk_socket);
282 return;
283 }
284
285 static int __init nfnetlink_init(void)
286 {
287 printk("Netfilter messages via NETLINK v%s.\n", nfversion);
288
289 nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
290 nfnetlink_rcv, THIS_MODULE);
291 if (!nfnl) {
292 printk(KERN_ERR "cannot initialize nfnetlink!\n");
293 return -1;
294 }
295
296 return 0;
297 }
298
299 module_init(nfnetlink_init);
300 module_exit(nfnetlink_exit);
301
302 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
303 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
304 EXPORT_SYMBOL_GPL(nfnetlink_send);
305 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
306 EXPORT_SYMBOL_GPL(nfattr_parse);
307 EXPORT_SYMBOL_GPL(__nfa_fill);
This page took 0.038325 seconds and 6 git commands to generate.