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");
41 MODULE_AUTHOR("Harald Welte <laforge@netfilter.org>");
42 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_NETFILTER);
44 static char __initdata nfversion[] = "0.30";
49 #define DEBUGP(format, args...)
52 static struct sock *nfnl = NULL;
53 static struct nfnetlink_subsystem *subsys_table[NFNL_SUBSYS_COUNT];
54 DECLARE_MUTEX(nfnl_sem);
61 void nfnl_unlock(void)
66 int nfnetlink_subsys_register(struct nfnetlink_subsystem *n)
68 DEBUGP("registering subsystem ID %u\n", n->subsys_id);
70 /* If the netlink socket wasn't created, then fail */
75 subsys_table[n->subsys_id] = n;
81 int nfnetlink_subsys_unregister(struct nfnetlink_subsystem *n)
83 DEBUGP("unregistering subsystem ID %u\n", n->subsys_id);
86 subsys_table[n->subsys_id] = NULL;
92 static inline struct nfnetlink_subsystem *nfnetlink_get_subsys(u_int16_t type)
94 u_int8_t subsys_id = NFNL_SUBSYS_ID(type);
96 if (subsys_id >= NFNL_SUBSYS_COUNT
97 || subsys_table[subsys_id] == NULL)
100 return subsys_table[subsys_id];
103 static inline struct nfnl_callback *
104 nfnetlink_find_client(u_int16_t type, struct nfnetlink_subsystem *ss)
106 u_int8_t cb_id = NFNL_MSG_TYPE(type);
108 if (cb_id >= ss->cb_count) {
109 DEBUGP("msgtype %u >= %u, returning\n", type, ss->cb_count);
113 return &ss->cb[cb_id];
116 void __nfa_fill(struct sk_buff *skb, int attrtype, int attrlen,
120 int size = NFA_LENGTH(attrlen);
122 nfa = (struct nfattr *)skb_put(skb, NFA_ALIGN(size));
123 nfa->nfa_type = attrtype;
125 memcpy(NFA_DATA(nfa), data, attrlen);
126 memset(NFA_DATA(nfa) + attrlen, 0, NFA_ALIGN(size) - size);
129 int nfattr_parse(struct nfattr *tb[], int maxattr, struct nfattr *nfa, int len)
131 memset(tb, 0, sizeof(struct nfattr *) * maxattr);
133 while (NFA_OK(nfa, len)) {
134 unsigned flavor = nfa->nfa_type;
135 if (flavor && flavor <= maxattr)
137 nfa = NFA_NEXT(nfa, len);
144 * nfnetlink_check_attributes - check and parse nfnetlink attributes
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
152 nfnetlink_check_attributes(struct nfnetlink_subsystem *subsys,
153 struct nlmsghdr *nlh, struct nfattr *cda[])
157 memset(cda, 0, sizeof(struct nfattr *) * subsys->attr_count);
159 /* check attribute lengths. */
160 min_len = NLMSG_ALIGN(sizeof(struct nfgenmsg));
161 if (nlh->nlmsg_len < min_len)
164 if (nlh->nlmsg_len > min_len) {
165 struct nfattr *attr = NFM_NFA(NLMSG_DATA(nlh));
166 int attrlen = nlh->nlmsg_len - NLMSG_ALIGN(min_len);
168 while (NFA_OK(attr, attrlen)) {
169 unsigned flavor = attr->nfa_type;
171 if (flavor > subsys->attr_count)
173 cda[flavor - 1] = attr;
175 attr = NFA_NEXT(attr, attrlen);
183 int nfnetlink_send(struct sk_buff *skb, u32 pid, unsigned group, int echo)
185 int allocation = in_interrupt() ? GFP_ATOMIC : GFP_KERNEL;
188 NETLINK_CB(skb).dst_groups = group;
190 atomic_inc(&skb->users);
191 netlink_broadcast(nfnl, skb, pid, group, allocation);
193 err = netlink_unicast(nfnl, skb, pid, MSG_DONTWAIT);
198 int nfnetlink_unicast(struct sk_buff *skb, u_int32_t pid, int flags)
200 return netlink_unicast(nfnl, skb, pid, flags);
203 /* Process one complete nfnetlink message. */
204 static inline int nfnetlink_rcv_msg(struct sk_buff *skb,
205 struct nlmsghdr *nlh, int *errp)
207 struct nfnl_callback *nc;
208 struct nfnetlink_subsystem *ss;
211 DEBUGP("entered; subsys=%u, msgtype=%u\n",
212 NFNL_SUBSYS_ID(nlh->nlmsg_type),
213 NFNL_MSG_TYPE(nlh->nlmsg_type));
215 /* Only requests are handled by kernel now. */
216 if (!(nlh->nlmsg_flags & NLM_F_REQUEST)) {
217 DEBUGP("received non-request message\n");
221 /* All the messages must at least contain nfgenmsg */
223 NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg)))) {
224 DEBUGP("received message was too short\n");
228 type = nlh->nlmsg_type;
229 ss = nfnetlink_get_subsys(type);
233 nc = nfnetlink_find_client(type, ss);
235 DEBUGP("unable to find client for type %d\n", type);
239 if (nc->cap_required &&
240 !cap_raised(NETLINK_CB(skb).eff_cap, nc->cap_required)) {
241 DEBUGP("permission denied for type %d\n", type);
247 struct nfattr *cda[ss->attr_count];
249 memset(cda, 0, ss->attr_count*sizeof(struct nfattr *));
251 err = nfnetlink_check_attributes(ss, nlh, cda);
255 err = nc->call(nfnl, skb, nlh, cda, errp);
265 /* Process one packet of messages. */
266 static inline int nfnetlink_rcv_skb(struct sk_buff *skb)
269 struct nlmsghdr *nlh;
271 while (skb->len >= NLMSG_SPACE(0)) {
274 nlh = (struct nlmsghdr *)skb->data;
275 if (nlh->nlmsg_len < sizeof(struct nlmsghdr)
276 || skb->len < nlh->nlmsg_len)
278 rlen = NLMSG_ALIGN(nlh->nlmsg_len);
281 if (nfnetlink_rcv_msg(skb, nlh, &err)) {
284 netlink_ack(skb, nlh, err);
286 if (nlh->nlmsg_flags & NLM_F_ACK)
287 netlink_ack(skb, nlh, 0);
294 static void nfnetlink_rcv(struct sock *sk, int len)
299 if (nfnl_shlock_nowait())
302 while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
303 if (nfnetlink_rcv_skb(skb)) {
305 skb_queue_head(&sk->sk_receive_queue,
315 } while(nfnl && nfnl->sk_receive_queue.qlen);
318 void __exit nfnetlink_exit(void)
320 printk("Removing netfilter NETLINK layer.\n");
321 sock_release(nfnl->sk_socket);
325 int __init nfnetlink_init(void)
327 printk("Netfilter messages via NETLINK v%s.\n", nfversion);
329 nfnl = netlink_kernel_create(NETLINK_NETFILTER, nfnetlink_rcv,
332 printk(KERN_ERR "cannot initialize nfnetlink!\n");
339 module_init(nfnetlink_init);
340 module_exit(nfnetlink_exit);
342 EXPORT_SYMBOL_GPL(nfnetlink_subsys_register);
343 EXPORT_SYMBOL_GPL(nfnetlink_subsys_unregister);
344 EXPORT_SYMBOL_GPL(nfnetlink_send);
345 EXPORT_SYMBOL_GPL(nfnetlink_unicast);
346 EXPORT_SYMBOL_GPL(nfattr_parse);
347 EXPORT_SYMBOL_GPL(__nfa_fill);