xref: /openbmc/linux/net/netfilter/nfnetlink.c (revision 87c2ce3b)
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 by Pablo Neira Ayuso <pablo@eurodev.net>
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/config.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/socket.h>
21 #include <linux/kernel.h>
22 #include <linux/major.h>
23 #include <linux/sched.h>
24 #include <linux/timer.h>
25 #include <linux/string.h>
26 #include <linux/sockios.h>
27 #include <linux/net.h>
28 #include <linux/fcntl.h>
29 #include <linux/skbuff.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <net/sock.h>
33 #include <linux/init.h>
34 #include <linux/spinlock.h>
35 
36 #include <linux/netfilter.h>
37 #include <linux/netlink.h>
38 #include <linux/netfilter/nfnetlink.h>
39 
40 MODULE_LICENSE("GPL");
41 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
42 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
43 
44 static char __initdata nfversion[] = "0.30";
45 
46 #if 0
47 #define DEBUGP(format, args...)	\
48 		printk(KERN_DEBUG "%s(%d):%s(): " format, __FILE__, \
49 			__LINE__, __FUNCTION__, ## args)
50 #else
51 #define DEBUGP(format, args...)
52 #endif
53 
54 static struct sock *nfnl = NULL;
55 static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
56 DECLARE_MUTEX(nfnl_sem);
57 
58 void nfnl_lock(void)
59 {
60 	nfnl_shlock();
61 }
62 
63 void nfnl_unlock(void)
64 {
65 	nfnl_shunlock();
66 }
67 
68 int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
69 {
70 	DEBUGP("registering subsystem ID %u\n", n->subsys_id);
71 
72 	nfnl_lock();
73 	if (subsys_table[n->subsys_id]) {
74 		nfnl_unlock();
75 		return -EBUSY;
76 	}
77 	subsys_table[n->subsys_id] = n;
78 	nfnl_unlock();
79 
80 	return 0;
81 }
82 
83 int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
84 {
85 	DEBUGP("unregistering subsystem ID %u\n", n->subsys_id);
86 
87 	nfnl_lock();
88 	subsys_table[n->subsys_id] = NULL;
89 	nfnl_unlock();
90 
91 	return 0;
92 }
93 
94 static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
95 {
96 	u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
97 
98 	if (subsys_id >= NFNL_SUBSYS_COUNT
99 	    || subsys_table[subsys_id] == NULL)
100 		return NULL;
101 
102 	return subsys_table[subsys_id];
103 }
104 
105 static inline struct nfnl_callback *
106 nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
107 {
108 	u_int8_t cb_id = NFNL_MSG_TYPE(type);
109 
110 	if (cb_id >= ss->cb_count) {
111 		DEBUGP("msgtype %u >= %u, returning\n", type, ss->cb_count);
112 		return NULL;
113 	}
114 
115 	return &ss->cb[cb_id];
116 }
117 
118 void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
119 		const void *data)
120 {
121 	struct nfattr *nfa;
122 	int size = NFA_LENGTH(attrlen);
123 
124 	nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
125 	nfa->nfa_type = attrtype;
126 	nfa->nfa_len  = size;
127 	memcpy(NFA_DATA(nfa), data, attrlen);
128 	memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
129 }
130 
131 void nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
132 {
133 	memset(tb, 0, sizeof(struct nfattr *) * maxattr);
134 
135 	while (NFA_OK(nfa, len)) {
136 		unsigned flavor = NFA_TYPE(nfa);
137 		if (flavor && flavor <= maxattr)
138 			tb[flavor-1] = nfa;
139 		nfa = NFA_NEXT(nfa, len);
140 	}
141 }
142 
143 /**
144  * nfnetlink_check_attributes - check and parse nfnetlink attributes
145  *
146  * subsys: nfnl subsystem for which this message is to be parsed
147  * nlmsghdr: netlink message to be checked/parsed
148  * cda: array of pointers, needs to be at least subsys->attr_count big
149  *
150  */
151 static int
152 nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
153 			   struct nlmsghdr *nlh, struct nfattr *cda[])
154 {
155 	int min_len;
156 	u_int16_t attr_count;
157 	u_int8_t cb_id = NFNL_MSG_TYPE(nlh->nlmsg_type);
158 
159 	if (unlikely(cb_id >= subsys->cb_count)) {
160 		DEBUGP("msgtype %u >= %u, returning\n",
161 			cb_id, subsys->cb_count);
162 		return -EINVAL;
163 	}
164 
165 	min_len = NLMSG_SPACE(sizeof(struct nfgenmsg));
166 	if (unlikely(nlh->nlmsg_len < min_len))
167 		return -EINVAL;
168 
169 	attr_count = subsys->cb[cb_id].attr_count;
170 	memset(cda, 0, sizeof(struct nfattr *) * attr_count);
171 
172 	/* check attribute lengths. */
173 	if (likely(nlh->nlmsg_len > min_len)) {
174 		struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
175 		int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
176 
177 		while (NFA_OK(attr, attrlen)) {
178 			unsigned flavor = NFA_TYPE(attr);
179 			if (flavor) {
180 				if (flavor > attr_count)
181 					return -EINVAL;
182 				cda[flavor - 1] = attr;
183 			}
184 			attr = NFA_NEXT(attr, attrlen);
185 		}
186 	}
187 
188 	/* implicit: if nlmsg_len == min_len, we return 0, and an empty
189 	 * (zeroed) cda[] array. The message is valid, but empty. */
190 
191         return 0;
192 }
193 
194 int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
195 {
196 	gfp_t allocation = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
197 	int err = 0;
198 
199 	NETLINK_CB(skb).dst_group = group;
200 	if (echo)
201 		atomic_inc(&skb->users);
202 	netlink_broadcast(nfnl, skb, pid, group, allocation);
203 	if (echo)
204 		err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
205 
206 	return err;
207 }
208 
209 int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
210 {
211 	return netlink_unicast(nfnl, skb, pid, flags);
212 }
213 
214 /* Process one complete nfnetlink message. */
215 static inline int nfnetlink_rcv_msg(struct sk_buff *skb,
216 				    struct nlmsghdr *nlh, int *errp)
217 {
218 	struct nfnl_callback *nc;
219 	struct nfnetlink_subsystem *ss;
220 	int type, err = 0;
221 
222 	DEBUGP("entered; subsys=%u, msgtype=%u\n",
223 		 NFNL_SUBSYS_ID(nlh->nlmsg_type),
224 		 NFNL_MSG_TYPE(nlh->nlmsg_type));
225 
226 	if (!cap_raised(NETLINK_CB(skb).eff_cap, CAP_NET_ADMIN)) {
227 		DEBUGP("missing CAP_NET_ADMIN\n");
228 		*errp = -EPERM;
229 		return -1;
230 	}
231 
232 	/* Only requests are handled by kernel now. */
233 	if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
234 		DEBUGP("received non-request message\n");
235 		return 0;
236 	}
237 
238 	/* All the messages must at least contain nfgenmsg */
239 	if (nlh->nlmsg_len < NLMSG_SPACE(sizeof(struct nfgenmsg))) {
240 		DEBUGP("received message was too short\n");
241 		return 0;
242 	}
243 
244 	type = nlh->nlmsg_type;
245 	ss = nfnetlink_get_subsys(type);
246 	if (!ss) {
247 #ifdef CONFIG_KMOD
248 		/* don't call nfnl_shunlock, since it would reenter
249 		 * with further packet processing */
250 		up(&nfnl_sem);
251 		request_module("nfnetlink-subsys-%d", NFNL_SUBSYS_ID(type));
252 		nfnl_shlock();
253 		ss = nfnetlink_get_subsys(type);
254 		if (!ss)
255 #endif
256 			goto err_inval;
257 	}
258 
259 	nc = nfnetlink_find_client(type, ss);
260 	if (!nc) {
261 		DEBUGP("unable to find client for type %d\n", type);
262 		goto err_inval;
263 	}
264 
265 	{
266 		u_int16_t attr_count =
267 			ss->cb[NFNL_MSG_TYPE(nlh->nlmsg_type)].attr_count;
268 		struct nfattr *cda[attr_count];
269 
270 		memset(cda, 0, sizeof(struct nfattr *) * attr_count);
271 
272 		err = nfnetlink_check_attributes(ss, nlh, cda);
273 		if (err < 0)
274 			goto err_inval;
275 
276 		DEBUGP("calling handler\n");
277 		err = nc->call(nfnl, skb, nlh, cda, errp);
278 		*errp = err;
279 		return err;
280 	}
281 
282 err_inval:
283 	DEBUGP("returning -EINVAL\n");
284 	*errp = -EINVAL;
285 	return -1;
286 }
287 
288 /* Process one packet of messages. */
289 static inline int nfnetlink_rcv_skb(struct sk_buff *skb)
290 {
291 	int err;
292 	struct nlmsghdr *nlh;
293 
294 	while (skb->len >= NLMSG_SPACE(0)) {
295 		u32 rlen;
296 
297 		nlh = (struct nlmsghdr *)skb->data;
298 		if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
299 		    || skb->len < nlh->nlmsg_len)
300 			return 0;
301 		rlen = NLMSG_ALIGN(nlh->nlmsg_len);
302 		if (rlen > skb->len)
303 			rlen = skb->len;
304 		if (nfnetlink_rcv_msg(skb, nlh, &err)) {
305 			if (!err)
306 				return -1;
307 			netlink_ack(skb, nlh, err);
308 		} else
309 			if (nlh->nlmsg_flags & NLM_F_ACK)
310 				netlink_ack(skb, nlh, 0);
311 		skb_pull(skb, rlen);
312 	}
313 
314 	return 0;
315 }
316 
317 static void nfnetlink_rcv(struct sock *sk, int len)
318 {
319 	do {
320 		struct sk_buff *skb;
321 
322 		if (nfnl_shlock_nowait())
323 			return;
324 
325 		while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
326 			if (nfnetlink_rcv_skb(skb)) {
327 				if (skb->len)
328 					skb_queue_head(&sk->sk_receive_queue,
329 						       skb);
330 				else
331 					kfree_skb(skb);
332 				break;
333 			}
334 			kfree_skb(skb);
335 		}
336 
337 		/* don't call nfnl_shunlock, since it would reenter
338 		 * with further packet processing */
339 		up(&nfnl_sem);
340 	} while(nfnl && nfnl->sk_receive_queue.qlen);
341 }
342 
343 static void __exit nfnetlink_exit(void)
344 {
345 	printk("Removing netfilter NETLINK layer.\n");
346 	sock_release(nfnl->sk_socket);
347 	return;
348 }
349 
350 static int __init nfnetlink_init(void)
351 {
352 	printk("Netfilter messages via NETLINK v%s.\n", nfversion);
353 
354 	nfnl = netlink_kernel_create(NETLINK_NETFILTER, NFNLGRP_MAX,
355 	                             nfnetlink_rcv, THIS_MODULE);
356 	if (!nfnl) {
357 		printk(KERN_ERR "cannot initialize nfnetlink!\n");
358 		return -1;
359 	}
360 
361 	return 0;
362 }
363 
364 module_init(nfnetlink_init);
365 module_exit(nfnetlink_exit);
366 
367 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
368 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
369 EXPORT_SYMBOL_GPL(nfnetlink_send);
370 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
371 EXPORT_SYMBOL_GPL(nfattr_parse);
372 EXPORT_SYMBOL_GPL(__nfa_fill);
373