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 by Pablo Neira Ayuso <pablo@eurodev.net>
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/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>
33 #include <linux/init.h>
34 #include <linux/spinlock.h>
36 #include <linux/netfilter.h>
37 #include <linux/netlink.h>
38 #include <linux/netfilter/nfnetlink.h>
40 MODULE_LICENSE("GPL");
42 static char __initdata nfversion[] = "0.30";
47 #define DEBUGP(format, args...)
50 static struct sock *nfnl = NULL;
51 static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
52 DECLARE_MUTEX(nfnl_sem);
59 void nfnl_unlock(void)
64 int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
66 DEBUGP("registering subsystem ID %u\n", n->subsys_id);
68 /* If the netlink socket wasn't created, then fail */
73 subsys_table[n->subsys_id] = n;
79 int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
81 DEBUGP("unregistering subsystem ID %u\n", n->subsys_id);
84 subsys_table[n->subsys_id] = NULL;
90 static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
92 u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
94 if (subsys_id >= NFNL_SUBSYS_COUNT
95 || subsys_table[subsys_id] == NULL)
98 return subsys_table[subsys_id];
101 static inline struct nfnl_callback *
102 nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
104 u_int8_t cb_id = NFNL_MSG_TYPE(type);
106 if (cb_id >= ss->cb_count) {
107 DEBUGP("msgtype %u >= %u, returning\n", type, ss->cb_count);
111 return &ss->cb[cb_id];
114 void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
118 int size = NFA_LENGTH(attrlen);
120 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
121 nfa->nfa_type = attrtype;
123 memcpy(NFA_DATA(nfa), data, attrlen);
124 memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
127 int nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
129 memset(tb, 0, sizeof(struct nfattr *) * maxattr);
131 while (NFA_OK(nfa, len)) {
132 unsigned flavor = nfa->nfa_type;
133 if (flavor && flavor <= maxattr)
135 nfa = NFA_NEXT(nfa, len);
142 * nfnetlink_check_attributes - check and parse nfnetlink attributes
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
150 nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
151 struct nlmsghdr *nlh, struct nfattr *cda[])
155 memset(cda, 0, sizeof(struct nfattr *) * subsys->attr_count);
157 /* check attribute lengths. */
158 min_len = NLMSG_ALIGN(sizeof(struct nfgenmsg));
159 if (nlh->nlmsg_len < min_len)
162 if (nlh->nlmsg_len > min_len) {
163 struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
164 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
166 while (NFA_OK(attr, attrlen)) {
167 unsigned flavor = attr->nfa_type;
169 if (flavor > subsys->attr_count)
171 cda[flavor - 1] = attr;
173 attr = NFA_NEXT(attr, attrlen);
181 int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
183 int allocation = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
186 NETLINK_CB(skb).dst_groups = group;
188 atomic_inc(&skb->users);
189 netlink_broadcast(nfnl, skb, pid, group, allocation);
191 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
196 int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
198 return netlink_unicast(nfnl, skb, pid, flags);
201 /* Process one complete nfnetlink message. */
202 static inline int nfnetlink_rcv_msg(struct sk_buff *skb,
203 struct nlmsghdr *nlh, int *errp)
205 struct nfnl_callback *nc;
206 struct nfnetlink_subsystem *ss;
209 DEBUGP("entered; subsys=%u, msgtype=%u\n",
210 NFNL_SUBSYS_ID(nlh->nlmsg_type),
211 NFNL_MSG_TYPE(nlh->nlmsg_type));
213 /* Only requests are handled by kernel now. */
214 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
215 DEBUGP("received non-request message\n");
219 /* All the messages must at least contain nfgenmsg */
221 NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg)))) {
222 DEBUGP("received message was too short\n");
226 type = nlh->nlmsg_type;
227 ss = nfnetlink_get_subsys(type);
231 nc = nfnetlink_find_client(type, ss);
233 DEBUGP("unable to find client for type %d\n", type);
237 if (nc->cap_required &&
238 !cap_raised(NETLINK_CB(skb).eff_cap, nc->cap_required)) {
239 DEBUGP("permission denied for type %d\n", type);
245 struct nfattr *cda[ss->attr_count];
247 memset(cda, 0, ss->attr_count*sizeof(struct nfattr *));
249 err = nfnetlink_check_attributes(ss, nlh, cda);
253 err = nc->call(nfnl, skb, nlh, cda, errp);
263 /* Process one packet of messages. */
264 static inline int nfnetlink_rcv_skb(struct sk_buff *skb)
267 struct nlmsghdr *nlh;
269 while (skb->len >= NLMSG_SPACE(0)) {
272 nlh = (struct nlmsghdr *)skb->data;
273 if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
274 || skb->len < nlh->nlmsg_len)
276 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
279 if (nfnetlink_rcv_msg(skb, nlh, &err)) {
282 netlink_ack(skb, nlh, err);
284 if (nlh->nlmsg_flags & NLM_F_ACK)
285 netlink_ack(skb, nlh, 0);
292 static void nfnetlink_rcv(struct sock *sk, int len)
297 if (nfnl_shlock_nowait())
300 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
301 if (nfnetlink_rcv_skb(skb)) {
303 skb_queue_head(&sk->sk_receive_queue,
313 } while(nfnl && nfnl->sk_receive_queue.qlen);
316 void __exit nfnetlink_exit(void)
318 printk("Removing netfilter NETLINK layer.\n");
319 sock_release(nfnl->sk_socket);
323 int __init nfnetlink_init(void)
325 printk("Netfilter messages via NETLINK v%s.\n", nfversion);
327 nfnl = netlink_kernel_create(NETLINK_NETFILTER, nfnetlink_rcv);
329 printk(KERN_ERR "cannot initialize nfnetlink!\n");
336 module_init(nfnetlink_init);
337 module_exit(nfnetlink_exit);
339 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
340 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
341 EXPORT_SYMBOL_GPL(nfnetlink_send);
342 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
343 EXPORT_SYMBOL_GPL(nfattr_parse);
344 EXPORT_SYMBOL_GPL(__nfa_fill);