blob: d6d39e2413278b5b8fa9a60523be7a74ded14979 [file] [log] [blame]
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001/* Connection tracking 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>
Harald Weltedc808fe2006-03-20 17:56:32 -08005 * (C) 2002-2006 by Harald Welte <laforge@gnumonks.org>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08006 * (C) 2003 by Patrick Mchardy <kaber@trash.net>
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08007 * (C) 2005-2006 by Pablo Neira Ayuso <pablo@eurodev.net>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08008 *
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08009 * Initial connection tracking via netlink development funded and
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080010 * generally made possible by Network Robots, Inc. (www.networkrobots.com)
11 *
12 * Further development of this code funded by Astaro AG (http://www.astaro.com)
13 *
14 * This software may be used and distributed according to the terms
15 * of the GNU General Public License, incorporated herein by reference.
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080016 */
17
18#include <linux/init.h>
19#include <linux/module.h>
20#include <linux/kernel.h>
21#include <linux/types.h>
22#include <linux/timer.h>
23#include <linux/skbuff.h>
24#include <linux/errno.h>
25#include <linux/netlink.h>
26#include <linux/spinlock.h>
Yasuyuki Kozakai40a839f2006-06-27 03:00:35 -070027#include <linux/interrupt.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080028#include <linux/notifier.h>
29
30#include <linux/netfilter.h>
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -070031#include <net/netlink.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080032#include <net/netfilter/nf_conntrack.h>
33#include <net/netfilter/nf_conntrack_core.h>
Martin Josefsson77ab9cf2006-11-29 02:34:58 +010034#include <net/netfilter/nf_conntrack_expect.h>
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080035#include <net/netfilter/nf_conntrack_helper.h>
36#include <net/netfilter/nf_conntrack_l3proto.h>
Martin Josefsson605dcad2006-11-29 02:35:06 +010037#include <net/netfilter/nf_conntrack_l4proto.h>
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -080038#include <net/netfilter/nf_conntrack_tuple.h>
39#ifdef CONFIG_NF_NAT_NEEDED
40#include <net/netfilter/nf_nat_core.h>
41#include <net/netfilter/nf_nat_protocol.h>
42#endif
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080043
44#include <linux/netfilter/nfnetlink.h>
45#include <linux/netfilter/nfnetlink_conntrack.h>
46
47MODULE_LICENSE("GPL");
48
Harald Weltedc808fe2006-03-20 17:56:32 -080049static char __initdata version[] = "0.93";
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080050
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080051static inline int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080052ctnetlink_dump_tuples_proto(struct sk_buff *skb,
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080053 const struct nf_conntrack_tuple *tuple,
Martin Josefsson605dcad2006-11-29 02:35:06 +010054 struct nf_conntrack_l4proto *l4proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080055{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080056 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080057 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080058
59 NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum);
60
Martin Josefsson605dcad2006-11-29 02:35:06 +010061 if (likely(l4proto->tuple_to_nfattr))
62 ret = l4proto->tuple_to_nfattr(skb, tuple);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -080063
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080064 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080065
66 return ret;
67
68nfattr_failure:
69 return -1;
70}
71
72static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080073ctnetlink_dump_tuples_ip(struct sk_buff *skb,
74 const struct nf_conntrack_tuple *tuple,
75 struct nf_conntrack_l3proto *l3proto)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080076{
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080077 int ret = 0;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080078 struct nfattr *nest_parms = NFA_NEST(skb, CTA_TUPLE_IP);
79
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080080 if (likely(l3proto->tuple_to_nfattr))
81 ret = l3proto->tuple_to_nfattr(skb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080082
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -080083 NFA_NEST_END(skb, nest_parms);
84
85 return ret;
86
87nfattr_failure:
88 return -1;
89}
90
91static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080092ctnetlink_dump_tuples(struct sk_buff *skb,
93 const struct nf_conntrack_tuple *tuple)
94{
95 int ret;
96 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +010097 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -080098
99 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
100 ret = ctnetlink_dump_tuples_ip(skb, tuple, l3proto);
101 nf_ct_l3proto_put(l3proto);
102
103 if (unlikely(ret < 0))
104 return ret;
105
Martin Josefsson605dcad2006-11-29 02:35:06 +0100106 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
107 ret = ctnetlink_dump_tuples_proto(skb, tuple, l4proto);
108 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -0800109
110 return ret;
111}
112
113static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800114ctnetlink_dump_status(struct sk_buff *skb, const struct nf_conn *ct)
115{
Patrick McHardybff9a892006-12-02 22:05:08 -0800116 __be32 status = htonl((u_int32_t) ct->status);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800117 NFA_PUT(skb, CTA_STATUS, sizeof(status), &status);
118 return 0;
119
120nfattr_failure:
121 return -1;
122}
123
124static inline int
125ctnetlink_dump_timeout(struct sk_buff *skb, const struct nf_conn *ct)
126{
127 long timeout_l = ct->timeout.expires - jiffies;
Patrick McHardybff9a892006-12-02 22:05:08 -0800128 __be32 timeout;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800129
130 if (timeout_l < 0)
131 timeout = 0;
132 else
133 timeout = htonl(timeout_l / HZ);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800134
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800135 NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout);
136 return 0;
137
138nfattr_failure:
139 return -1;
140}
141
142static inline int
143ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct nf_conn *ct)
144{
Martin Josefsson605dcad2006-11-29 02:35:06 +0100145 struct nf_conntrack_l4proto *l4proto = nf_ct_l4proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num, ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800146 struct nfattr *nest_proto;
147 int ret;
148
Martin Josefsson605dcad2006-11-29 02:35:06 +0100149 if (!l4proto->to_nfattr) {
150 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800151 return 0;
152 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800153
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800154 nest_proto = NFA_NEST(skb, CTA_PROTOINFO);
155
Martin Josefsson605dcad2006-11-29 02:35:06 +0100156 ret = l4proto->to_nfattr(skb, nest_proto, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800157
Martin Josefsson605dcad2006-11-29 02:35:06 +0100158 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800159
160 NFA_NEST_END(skb, nest_proto);
161
162 return ret;
163
164nfattr_failure:
Martin Josefsson605dcad2006-11-29 02:35:06 +0100165 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800166 return -1;
167}
168
169static inline int
170ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct nf_conn *ct)
171{
172 struct nfattr *nest_helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800173 const struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800174
Harald Weltedc808fe2006-03-20 17:56:32 -0800175 if (!help || !help->helper)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800176 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800177
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800178 nest_helper = NFA_NEST(skb, CTA_HELP);
Harald Weltedc808fe2006-03-20 17:56:32 -0800179 NFA_PUT(skb, CTA_HELP_NAME, strlen(help->helper->name), help->helper->name);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800180
Harald Weltedc808fe2006-03-20 17:56:32 -0800181 if (help->helper->to_nfattr)
182 help->helper->to_nfattr(skb, ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800183
184 NFA_NEST_END(skb, nest_helper);
185
186 return 0;
187
188nfattr_failure:
189 return -1;
190}
191
192#ifdef CONFIG_NF_CT_ACCT
193static inline int
194ctnetlink_dump_counters(struct sk_buff *skb, const struct nf_conn *ct,
195 enum ip_conntrack_dir dir)
196{
197 enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG;
198 struct nfattr *nest_count = NFA_NEST(skb, type);
Patrick McHardybff9a892006-12-02 22:05:08 -0800199 __be32 tmp;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800200
201 tmp = htonl(ct->counters[dir].packets);
202 NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp);
203
204 tmp = htonl(ct->counters[dir].bytes);
205 NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp);
206
207 NFA_NEST_END(skb, nest_count);
208
209 return 0;
210
211nfattr_failure:
212 return -1;
213}
214#else
215#define ctnetlink_dump_counters(a, b, c) (0)
216#endif
217
218#ifdef CONFIG_NF_CONNTRACK_MARK
219static inline int
220ctnetlink_dump_mark(struct sk_buff *skb, const struct nf_conn *ct)
221{
Patrick McHardybff9a892006-12-02 22:05:08 -0800222 __be32 mark = htonl(ct->mark);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800223
224 NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark);
225 return 0;
226
227nfattr_failure:
228 return -1;
229}
230#else
231#define ctnetlink_dump_mark(a, b) (0)
232#endif
233
234static inline int
235ctnetlink_dump_id(struct sk_buff *skb, const struct nf_conn *ct)
236{
Patrick McHardybff9a892006-12-02 22:05:08 -0800237 __be32 id = htonl(ct->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800238 NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id);
239 return 0;
240
241nfattr_failure:
242 return -1;
243}
244
245static inline int
246ctnetlink_dump_use(struct sk_buff *skb, const struct nf_conn *ct)
247{
Patrick McHardybff9a892006-12-02 22:05:08 -0800248 __be32 use = htonl(atomic_read(&ct->ct_general.use));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800249
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800250 NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use);
251 return 0;
252
253nfattr_failure:
254 return -1;
255}
256
257#define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple)
258
259static int
260ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800261 int event, int nowait,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800262 const struct nf_conn *ct)
263{
264 struct nlmsghdr *nlh;
265 struct nfgenmsg *nfmsg;
266 struct nfattr *nest_parms;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700267 unsigned char *b = skb_tail_pointer(skb);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800268
269 event |= NFNL_SUBSYS_CTNETLINK << 8;
270 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
271 nfmsg = NLMSG_DATA(nlh);
272
273 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800274 nfmsg->nfgen_family =
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800275 ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
276 nfmsg->version = NFNETLINK_V0;
277 nfmsg->res_id = 0;
278
279 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
280 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
281 goto nfattr_failure;
282 NFA_NEST_END(skb, nest_parms);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800283
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800284 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
285 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
286 goto nfattr_failure;
287 NFA_NEST_END(skb, nest_parms);
288
289 if (ctnetlink_dump_status(skb, ct) < 0 ||
290 ctnetlink_dump_timeout(skb, ct) < 0 ||
291 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
292 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 ||
293 ctnetlink_dump_protoinfo(skb, ct) < 0 ||
294 ctnetlink_dump_helpinfo(skb, ct) < 0 ||
295 ctnetlink_dump_mark(skb, ct) < 0 ||
296 ctnetlink_dump_id(skb, ct) < 0 ||
297 ctnetlink_dump_use(skb, ct) < 0)
298 goto nfattr_failure;
299
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700300 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800301 return skb->len;
302
303nlmsg_failure:
304nfattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -0700305 nlmsg_trim(skb, b);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800306 return -1;
307}
308
309#ifdef CONFIG_NF_CONNTRACK_EVENTS
310static int ctnetlink_conntrack_event(struct notifier_block *this,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800311 unsigned long events, void *ptr)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800312{
313 struct nlmsghdr *nlh;
314 struct nfgenmsg *nfmsg;
315 struct nfattr *nest_parms;
316 struct nf_conn *ct = (struct nf_conn *)ptr;
317 struct sk_buff *skb;
318 unsigned int type;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700319 sk_buff_data_t b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800320 unsigned int flags = 0, group;
321
322 /* ignore our fake conntrack entry */
323 if (ct == &nf_conntrack_untracked)
324 return NOTIFY_DONE;
325
326 if (events & IPCT_DESTROY) {
327 type = IPCTNL_MSG_CT_DELETE;
328 group = NFNLGRP_CONNTRACK_DESTROY;
329 } else if (events & (IPCT_NEW | IPCT_RELATED)) {
330 type = IPCTNL_MSG_CT_NEW;
331 flags = NLM_F_CREATE|NLM_F_EXCL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800332 group = NFNLGRP_CONNTRACK_NEW;
Pablo Neira Ayuso1a315262006-08-22 00:32:23 -0700333 } else if (events & (IPCT_STATUS | IPCT_PROTOINFO)) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800334 type = IPCTNL_MSG_CT_NEW;
335 group = NFNLGRP_CONNTRACK_UPDATE;
336 } else
337 return NOTIFY_DONE;
Patrick McHardya2427692006-03-20 18:03:59 -0800338
339 if (!nfnetlink_has_listeners(group))
340 return NOTIFY_DONE;
341
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800342 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
343 if (!skb)
344 return NOTIFY_DONE;
345
346 b = skb->tail;
347
348 type |= NFNL_SUBSYS_CTNETLINK << 8;
349 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
350 nfmsg = NLMSG_DATA(nlh);
351
352 nlh->nlmsg_flags = flags;
353 nfmsg->nfgen_family = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
354 nfmsg->version = NFNETLINK_V0;
355 nfmsg->res_id = 0;
356
357 nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG);
358 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0)
359 goto nfattr_failure;
360 NFA_NEST_END(skb, nest_parms);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800361
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800362 nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY);
363 if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0)
364 goto nfattr_failure;
365 NFA_NEST_END(skb, nest_parms);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800366
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100367 if (events & IPCT_DESTROY) {
368 if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
369 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0)
370 goto nfattr_failure;
371 } else {
372 if (ctnetlink_dump_status(skb, ct) < 0)
373 goto nfattr_failure;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800374
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100375 if (ctnetlink_dump_timeout(skb, ct) < 0)
376 goto nfattr_failure;
377
378 if (events & IPCT_PROTOINFO
379 && ctnetlink_dump_protoinfo(skb, ct) < 0)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800380 goto nfattr_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100381
382 if ((events & IPCT_HELPER || nfct_help(ct))
383 && ctnetlink_dump_helpinfo(skb, ct) < 0)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800384 goto nfattr_failure;
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100385
Patrick McHardy40e0cb02007-02-02 19:33:11 -0800386#ifdef CONFIG_NF_CONNTRACK_MARK
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100387 if ((events & IPCT_MARK || ct->mark)
388 && ctnetlink_dump_mark(skb, ct) < 0)
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800389 goto nfattr_failure;
Patrick McHardy40e0cb02007-02-02 19:33:11 -0800390#endif
Pablo Neira Ayuso7b621c12006-11-29 02:35:32 +0100391
392 if (events & IPCT_COUNTER_FILLING &&
393 (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 ||
394 ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0))
395 goto nfattr_failure;
396 }
Pablo Neira Ayusob9a37e02006-08-22 00:31:49 -0700397
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800398 nlh->nlmsg_len = skb->tail - b;
399 nfnetlink_send(skb, 0, group, 0);
400 return NOTIFY_DONE;
401
402nlmsg_failure:
403nfattr_failure:
404 kfree_skb(skb);
405 return NOTIFY_DONE;
406}
407#endif /* CONFIG_NF_CONNTRACK_EVENTS */
408
409static int ctnetlink_done(struct netlink_callback *cb)
410{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700411 if (cb->args[1])
412 nf_ct_put((struct nf_conn *)cb->args[1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800413 return 0;
414}
415
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800416#define L3PROTO(ct) ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num
417
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800418static int
419ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
420{
Patrick McHardy89f2e212006-05-29 18:24:58 -0700421 struct nf_conn *ct, *last;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800422 struct nf_conntrack_tuple_hash *h;
423 struct list_head *i;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800424 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
425 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800426
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800427 read_lock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700428 last = (struct nf_conn *)cb->args[1];
Patrick McHardy89f2e212006-05-29 18:24:58 -0700429 for (; cb->args[0] < nf_conntrack_htable_size; cb->args[0]++) {
430restart:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800431 list_for_each_prev(i, &nf_conntrack_hash[cb->args[0]]) {
432 h = (struct nf_conntrack_tuple_hash *) i;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800433 if (NF_CT_DIRECTION(h) != IP_CT_DIR_ORIGINAL)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800434 continue;
435 ct = nf_ct_tuplehash_to_ctrack(h);
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -0800436 /* Dump entries of a given L3 protocol number.
437 * If it is not specified, ie. l3proto == 0,
438 * then dump everything. */
439 if (l3proto && L3PROTO(ct) != l3proto)
440 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700441 if (cb->args[1]) {
442 if (ct != last)
Patrick McHardy89f2e212006-05-29 18:24:58 -0700443 continue;
Patrick McHardyd205dc42006-08-17 18:12:38 -0700444 cb->args[1] = 0;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700445 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800446 if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800447 cb->nlh->nlmsg_seq,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800448 IPCTNL_MSG_CT_NEW,
Patrick McHardy89f2e212006-05-29 18:24:58 -0700449 1, ct) < 0) {
450 nf_conntrack_get(&ct->ct_general);
451 cb->args[1] = (unsigned long)ct;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800452 goto out;
Patrick McHardy89f2e212006-05-29 18:24:58 -0700453 }
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700454#ifdef CONFIG_NF_CT_ACCT
455 if (NFNL_MSG_TYPE(cb->nlh->nlmsg_type) ==
456 IPCTNL_MSG_CT_GET_CTRZERO)
457 memset(&ct->counters, 0, sizeof(ct->counters));
458#endif
Patrick McHardy89f2e212006-05-29 18:24:58 -0700459 }
Patrick McHardyd205dc42006-08-17 18:12:38 -0700460 if (cb->args[1]) {
Patrick McHardy89f2e212006-05-29 18:24:58 -0700461 cb->args[1] = 0;
462 goto restart;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800463 }
464 }
Patrick McHardy89f2e212006-05-29 18:24:58 -0700465out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800466 read_unlock_bh(&nf_conntrack_lock);
Patrick McHardyd205dc42006-08-17 18:12:38 -0700467 if (last)
468 nf_ct_put(last);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800469
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800470 return skb->len;
471}
472
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800473static inline int
474ctnetlink_parse_tuple_ip(struct nfattr *attr, struct nf_conntrack_tuple *tuple)
475{
476 struct nfattr *tb[CTA_IP_MAX];
477 struct nf_conntrack_l3proto *l3proto;
478 int ret = 0;
479
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800480 nfattr_parse_nested(tb, CTA_IP_MAX, attr);
481
482 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
483
484 if (likely(l3proto->nfattr_to_tuple))
485 ret = l3proto->nfattr_to_tuple(tb, tuple);
486
487 nf_ct_l3proto_put(l3proto);
488
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800489 return ret;
490}
491
492static const size_t cta_min_proto[CTA_PROTO_MAX] = {
493 [CTA_PROTO_NUM-1] = sizeof(u_int8_t),
494};
495
496static inline int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800497ctnetlink_parse_tuple_proto(struct nfattr *attr,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800498 struct nf_conntrack_tuple *tuple)
499{
500 struct nfattr *tb[CTA_PROTO_MAX];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100501 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800502 int ret = 0;
503
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800504 nfattr_parse_nested(tb, CTA_PROTO_MAX, attr);
505
506 if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto))
507 return -EINVAL;
508
509 if (!tb[CTA_PROTO_NUM-1])
510 return -EINVAL;
511 tuple->dst.protonum = *(u_int8_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]);
512
Martin Josefsson605dcad2006-11-29 02:35:06 +0100513 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800514
Martin Josefsson605dcad2006-11-29 02:35:06 +0100515 if (likely(l4proto->nfattr_to_tuple))
516 ret = l4proto->nfattr_to_tuple(tb, tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800517
Martin Josefsson605dcad2006-11-29 02:35:06 +0100518 nf_ct_l4proto_put(l4proto);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800519
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800520 return ret;
521}
522
523static inline int
524ctnetlink_parse_tuple(struct nfattr *cda[], struct nf_conntrack_tuple *tuple,
525 enum ctattr_tuple type, u_int8_t l3num)
526{
527 struct nfattr *tb[CTA_TUPLE_MAX];
528 int err;
529
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800530 memset(tuple, 0, sizeof(*tuple));
531
532 nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]);
533
534 if (!tb[CTA_TUPLE_IP-1])
535 return -EINVAL;
536
537 tuple->src.l3num = l3num;
538
539 err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple);
540 if (err < 0)
541 return err;
542
543 if (!tb[CTA_TUPLE_PROTO-1])
544 return -EINVAL;
545
546 err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple);
547 if (err < 0)
548 return err;
549
550 /* orig and expect tuples get DIR_ORIGINAL */
551 if (type == CTA_TUPLE_REPLY)
552 tuple->dst.dir = IP_CT_DIR_REPLY;
553 else
554 tuple->dst.dir = IP_CT_DIR_ORIGINAL;
555
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800556 return 0;
557}
558
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800559#ifdef CONFIG_NF_NAT_NEEDED
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800560static const size_t cta_min_protonat[CTA_PROTONAT_MAX] = {
561 [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t),
562 [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t),
563};
564
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800565static int nfnetlink_parse_nat_proto(struct nfattr *attr,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800566 const struct nf_conn *ct,
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800567 struct nf_nat_range *range)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800568{
569 struct nfattr *tb[CTA_PROTONAT_MAX];
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800570 struct nf_nat_protocol *npt;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800571
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800572 nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr);
573
574 if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat))
575 return -EINVAL;
576
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800577 npt = nf_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800578
579 if (!npt->nfattr_to_range) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800580 nf_nat_proto_put(npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800581 return 0;
582 }
583
584 /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */
585 if (npt->nfattr_to_range(tb, range) > 0)
586 range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED;
587
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800588 nf_nat_proto_put(npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800589
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800590 return 0;
591}
592
593static const size_t cta_min_nat[CTA_NAT_MAX] = {
594 [CTA_NAT_MINIP-1] = sizeof(u_int32_t),
595 [CTA_NAT_MAXIP-1] = sizeof(u_int32_t),
596};
597
598static inline int
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800599nfnetlink_parse_nat(struct nfattr *nat,
600 const struct nf_conn *ct, struct nf_nat_range *range)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800601{
602 struct nfattr *tb[CTA_NAT_MAX];
603 int err;
604
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800605 memset(range, 0, sizeof(*range));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800606
Patrick McHardy3726add2006-05-29 18:24:39 -0700607 nfattr_parse_nested(tb, CTA_NAT_MAX, nat);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800608
609 if (nfattr_bad_size(tb, CTA_NAT_MAX, cta_min_nat))
610 return -EINVAL;
611
612 if (tb[CTA_NAT_MINIP-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800613 range->min_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MINIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800614
615 if (!tb[CTA_NAT_MAXIP-1])
616 range->max_ip = range->min_ip;
617 else
Patrick McHardybff9a892006-12-02 22:05:08 -0800618 range->max_ip = *(__be32 *)NFA_DATA(tb[CTA_NAT_MAXIP-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800619
620 if (range->min_ip)
621 range->flags |= IP_NAT_RANGE_MAP_IPS;
622
623 if (!tb[CTA_NAT_PROTO-1])
624 return 0;
625
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800626 err = nfnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800627 if (err < 0)
628 return err;
629
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800630 return 0;
631}
632#endif
633
634static inline int
635ctnetlink_parse_help(struct nfattr *attr, char **helper_name)
636{
637 struct nfattr *tb[CTA_HELP_MAX];
638
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800639 nfattr_parse_nested(tb, CTA_HELP_MAX, attr);
640
641 if (!tb[CTA_HELP_NAME-1])
642 return -EINVAL;
643
644 *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]);
645
646 return 0;
647}
648
649static const size_t cta_min[CTA_MAX] = {
650 [CTA_STATUS-1] = sizeof(u_int32_t),
651 [CTA_TIMEOUT-1] = sizeof(u_int32_t),
652 [CTA_MARK-1] = sizeof(u_int32_t),
653 [CTA_USE-1] = sizeof(u_int32_t),
654 [CTA_ID-1] = sizeof(u_int32_t)
655};
656
657static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800658ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700659 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800660{
661 struct nf_conntrack_tuple_hash *h;
662 struct nf_conntrack_tuple tuple;
663 struct nf_conn *ct;
664 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
665 u_int8_t u3 = nfmsg->nfgen_family;
666 int err = 0;
667
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800668 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
669 return -EINVAL;
670
671 if (cda[CTA_TUPLE_ORIG-1])
672 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
673 else if (cda[CTA_TUPLE_REPLY-1])
674 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
675 else {
676 /* Flush the whole table */
677 nf_conntrack_flush();
678 return 0;
679 }
680
681 if (err < 0)
682 return err;
683
684 h = nf_conntrack_find_get(&tuple, NULL);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700685 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800686 return -ENOENT;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800687
688 ct = nf_ct_tuplehash_to_ctrack(h);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800689
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800690 if (cda[CTA_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -0800691 u_int32_t id = ntohl(*(__be32 *)NFA_DATA(cda[CTA_ID-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800692 if (ct->id != id) {
693 nf_ct_put(ct);
694 return -ENOENT;
695 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800696 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800697 if (del_timer(&ct->timeout))
698 ct->timeout.function((unsigned long)ct);
699
700 nf_ct_put(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800701
702 return 0;
703}
704
705static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800706ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -0700707 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800708{
709 struct nf_conntrack_tuple_hash *h;
710 struct nf_conntrack_tuple tuple;
711 struct nf_conn *ct;
712 struct sk_buff *skb2 = NULL;
713 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
714 u_int8_t u3 = nfmsg->nfgen_family;
715 int err = 0;
716
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800717 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Pablo Neira Ayuso01f34842006-09-20 12:00:45 -0700718#ifndef CONFIG_NF_CT_ACCT
719 if (NFNL_MSG_TYPE(nlh->nlmsg_type) == IPCTNL_MSG_CT_GET_CTRZERO)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800720 return -ENOTSUPP;
721#endif
Thomas Grafc702e802007-03-22 23:30:55 -0700722 return netlink_dump_start(ctnl, skb, nlh, ctnetlink_dump_table,
723 ctnetlink_done);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800724 }
725
726 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
727 return -EINVAL;
728
729 if (cda[CTA_TUPLE_ORIG-1])
730 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG, u3);
731 else if (cda[CTA_TUPLE_REPLY-1])
732 err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY, u3);
733 else
734 return -EINVAL;
735
736 if (err < 0)
737 return err;
738
739 h = nf_conntrack_find_get(&tuple, NULL);
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700740 if (!h)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800741 return -ENOENT;
Pablo Neira Ayuso9ea8cfd2006-10-12 14:09:16 -0700742
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800743 ct = nf_ct_tuplehash_to_ctrack(h);
744
745 err = -ENOMEM;
746 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
747 if (!skb2) {
748 nf_ct_put(ct);
749 return -ENOMEM;
750 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800751
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800752 err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800753 IPCTNL_MSG_CT_NEW, 1, ct);
754 nf_ct_put(ct);
755 if (err <= 0)
756 goto free;
757
758 err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
759 if (err < 0)
760 goto out;
761
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800762 return 0;
763
764free:
765 kfree_skb(skb2);
766out:
767 return err;
768}
769
770static inline int
771ctnetlink_change_status(struct nf_conn *ct, struct nfattr *cda[])
772{
773 unsigned long d;
Patrick McHardybff9a892006-12-02 22:05:08 -0800774 unsigned int status = ntohl(*(__be32 *)NFA_DATA(cda[CTA_STATUS-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800775 d = ct->status ^ status;
776
777 if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING))
778 /* unchangeable */
779 return -EINVAL;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800780
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800781 if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY))
782 /* SEEN_REPLY bit can only be set */
783 return -EINVAL;
784
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800785
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800786 if (d & IPS_ASSURED && !(status & IPS_ASSURED))
787 /* ASSURED bit can only be set */
788 return -EINVAL;
789
Patrick McHardy3726add2006-05-29 18:24:39 -0700790 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800791#ifndef CONFIG_NF_NAT_NEEDED
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800792 return -EINVAL;
793#else
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800794 struct nf_nat_range range;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800795
Patrick McHardy3726add2006-05-29 18:24:39 -0700796 if (cda[CTA_NAT_DST-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800797 if (nfnetlink_parse_nat(cda[CTA_NAT_DST-1], ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700798 &range) < 0)
799 return -EINVAL;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800800 if (nf_nat_initialized(ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700801 HOOK2MANIP(NF_IP_PRE_ROUTING)))
802 return -EEXIST;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800803 nf_nat_setup_info(ct, &range, NF_IP_PRE_ROUTING);
Patrick McHardy3726add2006-05-29 18:24:39 -0700804 }
805 if (cda[CTA_NAT_SRC-1]) {
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800806 if (nfnetlink_parse_nat(cda[CTA_NAT_SRC-1], ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700807 &range) < 0)
808 return -EINVAL;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800809 if (nf_nat_initialized(ct,
Patrick McHardy3726add2006-05-29 18:24:39 -0700810 HOOK2MANIP(NF_IP_POST_ROUTING)))
811 return -EEXIST;
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800812 nf_nat_setup_info(ct, &range, NF_IP_POST_ROUTING);
Patrick McHardy3726add2006-05-29 18:24:39 -0700813 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800814#endif
815 }
816
817 /* Be careful here, modifying NAT bits can screw up things,
818 * so don't let users modify them directly if they don't pass
Jozsef Kadlecsik5b1158e2006-12-02 22:07:13 -0800819 * nf_nat_range. */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800820 ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK);
821 return 0;
822}
823
824
825static inline int
826ctnetlink_change_helper(struct nf_conn *ct, struct nfattr *cda[])
827{
828 struct nf_conntrack_helper *helper;
Harald Weltedc808fe2006-03-20 17:56:32 -0800829 struct nf_conn_help *help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800830 char *helpname;
831 int err;
832
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800833 /* don't change helper of sibling connections */
834 if (ct->master)
835 return -EINVAL;
836
837 err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname);
838 if (err < 0)
839 return err;
840
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700841 if (!strcmp(helpname, "")) {
842 if (help && help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800843 /* we had a helper before ... */
844 nf_ct_remove_expectations(ct);
Harald Weltedc808fe2006-03-20 17:56:32 -0800845 help->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800846 }
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700847
848 return 0;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800849 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800850
Yasuyuki Kozakaidf293bb2007-05-10 14:15:58 -0700851 if (!help) {
852 /* FIXME: we need to reallocate and rehash */
853 return -EBUSY;
854 }
855
856 helper = __nf_conntrack_helper_find_byname(helpname);
857 if (helper == NULL)
858 return -EINVAL;
859
860 if (help->helper == helper)
861 return 0;
862
863 if (help->helper)
864 /* we had a helper before ... */
865 nf_ct_remove_expectations(ct);
866
867 /* need to zero data of old helper */
868 memset(&help->help, 0, sizeof(help->help));
Harald Weltedc808fe2006-03-20 17:56:32 -0800869 help->helper = helper;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800870
871 return 0;
872}
873
874static inline int
875ctnetlink_change_timeout(struct nf_conn *ct, struct nfattr *cda[])
876{
Patrick McHardybff9a892006-12-02 22:05:08 -0800877 u_int32_t timeout = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800878
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800879 if (!del_timer(&ct->timeout))
880 return -ETIME;
881
882 ct->timeout.expires = jiffies + timeout * HZ;
883 add_timer(&ct->timeout);
884
885 return 0;
886}
887
888static inline int
889ctnetlink_change_protoinfo(struct nf_conn *ct, struct nfattr *cda[])
890{
891 struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1];
Martin Josefsson605dcad2006-11-29 02:35:06 +0100892 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800893 u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum;
894 u_int16_t l3num = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.src.l3num;
895 int err = 0;
896
897 nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr);
898
Martin Josefsson605dcad2006-11-29 02:35:06 +0100899 l4proto = nf_ct_l4proto_find_get(l3num, npt);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800900
Martin Josefsson605dcad2006-11-29 02:35:06 +0100901 if (l4proto->from_nfattr)
902 err = l4proto->from_nfattr(tb, ct);
903 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800904
905 return err;
906}
907
908static int
909ctnetlink_change_conntrack(struct nf_conn *ct, struct nfattr *cda[])
910{
911 int err;
912
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800913 if (cda[CTA_HELP-1]) {
914 err = ctnetlink_change_helper(ct, cda);
915 if (err < 0)
916 return err;
917 }
918
919 if (cda[CTA_TIMEOUT-1]) {
920 err = ctnetlink_change_timeout(ct, cda);
921 if (err < 0)
922 return err;
923 }
924
925 if (cda[CTA_STATUS-1]) {
926 err = ctnetlink_change_status(ct, cda);
927 if (err < 0)
928 return err;
929 }
930
931 if (cda[CTA_PROTOINFO-1]) {
932 err = ctnetlink_change_protoinfo(ct, cda);
933 if (err < 0)
934 return err;
935 }
936
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800937#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800938 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800939 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800940#endif
941
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800942 return 0;
943}
944
945static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800946ctnetlink_create_conntrack(struct nfattr *cda[],
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800947 struct nf_conntrack_tuple *otuple,
948 struct nf_conntrack_tuple *rtuple)
949{
950 struct nf_conn *ct;
951 int err = -EINVAL;
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800952 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800953
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800954 ct = nf_conntrack_alloc(otuple, rtuple);
955 if (ct == NULL || IS_ERR(ct))
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800956 return -ENOMEM;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800957
958 if (!cda[CTA_TIMEOUT-1])
959 goto err;
Patrick McHardybff9a892006-12-02 22:05:08 -0800960 ct->timeout.expires = ntohl(*(__be32 *)NFA_DATA(cda[CTA_TIMEOUT-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800961
962 ct->timeout.expires = jiffies + ct->timeout.expires * HZ;
963 ct->status |= IPS_CONFIRMED;
964
Pablo Neira Ayusobbb33572006-11-29 02:35:31 +0100965 if (cda[CTA_STATUS-1]) {
966 err = ctnetlink_change_status(ct, cda);
967 if (err < 0)
968 goto err;
969 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800970
971 if (cda[CTA_PROTOINFO-1]) {
972 err = ctnetlink_change_protoinfo(ct, cda);
973 if (err < 0)
Patrick McHardyc54ea3b2007-01-15 17:16:03 -0800974 goto err;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800975 }
976
Martin Josefssonbcd1e832006-04-01 02:23:21 -0800977#if defined(CONFIG_NF_CONNTRACK_MARK)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800978 if (cda[CTA_MARK-1])
Patrick McHardybff9a892006-12-02 22:05:08 -0800979 ct->mark = ntohl(*(__be32 *)NFA_DATA(cda[CTA_MARK-1]));
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800980#endif
981
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800982 help = nfct_help(ct);
983 if (help)
984 help->helper = nf_ct_helper_find_get(rtuple);
985
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800986 add_timer(&ct->timeout);
987 nf_conntrack_hash_insert(ct);
988
Yasuyuki Kozakaidafc7412006-11-27 10:25:32 -0800989 if (help && help->helper)
990 nf_ct_helper_put(help->helper);
991
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800992 return 0;
993
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800994err:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -0800995 nf_conntrack_free(ct);
996 return err;
997}
998
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -0800999static int
1000ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001001 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001002{
1003 struct nf_conntrack_tuple otuple, rtuple;
1004 struct nf_conntrack_tuple_hash *h = NULL;
1005 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1006 u_int8_t u3 = nfmsg->nfgen_family;
1007 int err = 0;
1008
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001009 if (nfattr_bad_size(cda, CTA_MAX, cta_min))
1010 return -EINVAL;
1011
1012 if (cda[CTA_TUPLE_ORIG-1]) {
1013 err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG, u3);
1014 if (err < 0)
1015 return err;
1016 }
1017
1018 if (cda[CTA_TUPLE_REPLY-1]) {
1019 err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY, u3);
1020 if (err < 0)
1021 return err;
1022 }
1023
1024 write_lock_bh(&nf_conntrack_lock);
1025 if (cda[CTA_TUPLE_ORIG-1])
1026 h = __nf_conntrack_find(&otuple, NULL);
1027 else if (cda[CTA_TUPLE_REPLY-1])
1028 h = __nf_conntrack_find(&rtuple, NULL);
1029
1030 if (h == NULL) {
1031 write_unlock_bh(&nf_conntrack_lock);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001032 err = -ENOENT;
1033 if (nlh->nlmsg_flags & NLM_F_CREATE)
1034 err = ctnetlink_create_conntrack(cda, &otuple, &rtuple);
1035 return err;
1036 }
1037 /* implicit 'else' */
1038
1039 /* we only allow nat config for new conntracks */
Patrick McHardy3726add2006-05-29 18:24:39 -07001040 if (cda[CTA_NAT_SRC-1] || cda[CTA_NAT_DST-1]) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001041 err = -EINVAL;
1042 goto out_unlock;
1043 }
1044
1045 /* We manipulate the conntrack inside the global conntrack table lock,
1046 * so there's no need to increase the refcount */
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001047 err = -EEXIST;
1048 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1049 err = ctnetlink_change_conntrack(nf_ct_tuplehash_to_ctrack(h), cda);
1050
1051out_unlock:
1052 write_unlock_bh(&nf_conntrack_lock);
1053 return err;
1054}
1055
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001056/***********************************************************************
1057 * EXPECT
1058 ***********************************************************************/
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001059
1060static inline int
1061ctnetlink_exp_dump_tuple(struct sk_buff *skb,
1062 const struct nf_conntrack_tuple *tuple,
1063 enum ctattr_expect type)
1064{
1065 struct nfattr *nest_parms = NFA_NEST(skb, type);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001066
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001067 if (ctnetlink_dump_tuples(skb, tuple) < 0)
1068 goto nfattr_failure;
1069
1070 NFA_NEST_END(skb, nest_parms);
1071
1072 return 0;
1073
1074nfattr_failure:
1075 return -1;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001076}
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001077
1078static inline int
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001079ctnetlink_exp_dump_mask(struct sk_buff *skb,
1080 const struct nf_conntrack_tuple *tuple,
1081 const struct nf_conntrack_tuple *mask)
1082{
1083 int ret;
1084 struct nf_conntrack_l3proto *l3proto;
Martin Josefsson605dcad2006-11-29 02:35:06 +01001085 struct nf_conntrack_l4proto *l4proto;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001086 struct nfattr *nest_parms = NFA_NEST(skb, CTA_EXPECT_MASK);
1087
1088 l3proto = nf_ct_l3proto_find_get(tuple->src.l3num);
1089 ret = ctnetlink_dump_tuples_ip(skb, mask, l3proto);
1090 nf_ct_l3proto_put(l3proto);
1091
1092 if (unlikely(ret < 0))
1093 goto nfattr_failure;
1094
Martin Josefsson605dcad2006-11-29 02:35:06 +01001095 l4proto = nf_ct_l4proto_find_get(tuple->src.l3num, tuple->dst.protonum);
1096 ret = ctnetlink_dump_tuples_proto(skb, mask, l4proto);
1097 nf_ct_l4proto_put(l4proto);
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001098 if (unlikely(ret < 0))
1099 goto nfattr_failure;
1100
1101 NFA_NEST_END(skb, nest_parms);
1102
1103 return 0;
1104
1105nfattr_failure:
1106 return -1;
1107}
1108
1109static inline int
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001110ctnetlink_exp_dump_expect(struct sk_buff *skb,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001111 const struct nf_conntrack_expect *exp)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001112{
1113 struct nf_conn *master = exp->master;
Patrick McHardybff9a892006-12-02 22:05:08 -08001114 __be32 timeout = htonl((exp->timeout.expires - jiffies) / HZ);
1115 __be32 id = htonl(exp->id);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001116
1117 if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0)
1118 goto nfattr_failure;
Pablo Neira Ayuso1cde6432006-03-22 13:54:15 -08001119 if (ctnetlink_exp_dump_mask(skb, &exp->tuple, &exp->mask) < 0)
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001120 goto nfattr_failure;
1121 if (ctnetlink_exp_dump_tuple(skb,
1122 &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple,
1123 CTA_EXPECT_MASTER) < 0)
1124 goto nfattr_failure;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001125
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001126 NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout);
1127 NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id);
1128
1129 return 0;
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001130
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001131nfattr_failure:
1132 return -1;
1133}
1134
1135static int
1136ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq,
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001137 int event,
1138 int nowait,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001139 const struct nf_conntrack_expect *exp)
1140{
1141 struct nlmsghdr *nlh;
1142 struct nfgenmsg *nfmsg;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001143 unsigned char *b = skb_tail_pointer(skb);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001144
1145 event |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
1146 nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg));
1147 nfmsg = NLMSG_DATA(nlh);
1148
1149 nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0;
1150 nfmsg->nfgen_family = exp->tuple.src.l3num;
1151 nfmsg->version = NFNETLINK_V0;
1152 nfmsg->res_id = 0;
1153
1154 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1155 goto nfattr_failure;
1156
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001157 nlh->nlmsg_len = skb_tail_pointer(skb) - b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001158 return skb->len;
1159
1160nlmsg_failure:
1161nfattr_failure:
Arnaldo Carvalho de Melodc5fc572007-03-25 23:06:12 -07001162 nlmsg_trim(skb, b);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001163 return -1;
1164}
1165
1166#ifdef CONFIG_NF_CONNTRACK_EVENTS
1167static int ctnetlink_expect_event(struct notifier_block *this,
1168 unsigned long events, void *ptr)
1169{
1170 struct nlmsghdr *nlh;
1171 struct nfgenmsg *nfmsg;
1172 struct nf_conntrack_expect *exp = (struct nf_conntrack_expect *)ptr;
1173 struct sk_buff *skb;
1174 unsigned int type;
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -07001175 sk_buff_data_t b;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001176 int flags = 0;
1177
1178 if (events & IPEXP_NEW) {
1179 type = IPCTNL_MSG_EXP_NEW;
1180 flags = NLM_F_CREATE|NLM_F_EXCL;
1181 } else
1182 return NOTIFY_DONE;
1183
Pablo Neira Ayusob3a27bf2006-08-22 00:32:05 -07001184 if (!nfnetlink_has_listeners(NFNLGRP_CONNTRACK_EXP_NEW))
1185 return NOTIFY_DONE;
1186
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001187 skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC);
1188 if (!skb)
1189 return NOTIFY_DONE;
1190
1191 b = skb->tail;
1192
Marcus Sundbergb633ad52006-02-04 02:11:09 -08001193 type |= NFNL_SUBSYS_CTNETLINK_EXP << 8;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001194 nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg));
1195 nfmsg = NLMSG_DATA(nlh);
1196
1197 nlh->nlmsg_flags = flags;
1198 nfmsg->nfgen_family = exp->tuple.src.l3num;
1199 nfmsg->version = NFNETLINK_V0;
1200 nfmsg->res_id = 0;
1201
1202 if (ctnetlink_exp_dump_expect(skb, exp) < 0)
1203 goto nfattr_failure;
1204
1205 nlh->nlmsg_len = skb->tail - b;
1206 nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0);
1207 return NOTIFY_DONE;
1208
1209nlmsg_failure:
1210nfattr_failure:
1211 kfree_skb(skb);
1212 return NOTIFY_DONE;
1213}
1214#endif
1215
1216static int
1217ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb)
1218{
1219 struct nf_conntrack_expect *exp = NULL;
1220 struct list_head *i;
1221 u_int32_t *id = (u_int32_t *) &cb->args[0];
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001222 struct nfgenmsg *nfmsg = NLMSG_DATA(cb->nlh);
1223 u_int8_t l3proto = nfmsg->nfgen_family;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001224
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001225 read_lock_bh(&nf_conntrack_lock);
1226 list_for_each_prev(i, &nf_conntrack_expect_list) {
1227 exp = (struct nf_conntrack_expect *) i;
Pablo Neira Ayuso87711cb2006-01-05 12:19:23 -08001228 if (l3proto && exp->tuple.src.l3num != l3proto)
1229 continue;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001230 if (exp->id <= *id)
1231 continue;
1232 if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid,
1233 cb->nlh->nlmsg_seq,
1234 IPCTNL_MSG_EXP_NEW,
1235 1, exp) < 0)
1236 goto out;
1237 *id = exp->id;
1238 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001239out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001240 read_unlock_bh(&nf_conntrack_lock);
1241
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001242 return skb->len;
1243}
1244
1245static const size_t cta_min_exp[CTA_EXPECT_MAX] = {
1246 [CTA_EXPECT_TIMEOUT-1] = sizeof(u_int32_t),
1247 [CTA_EXPECT_ID-1] = sizeof(u_int32_t)
1248};
1249
1250static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001251ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001252 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001253{
1254 struct nf_conntrack_tuple tuple;
1255 struct nf_conntrack_expect *exp;
1256 struct sk_buff *skb2;
1257 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1258 u_int8_t u3 = nfmsg->nfgen_family;
1259 int err = 0;
1260
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001261 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1262 return -EINVAL;
1263
1264 if (nlh->nlmsg_flags & NLM_F_DUMP) {
Thomas Grafc702e802007-03-22 23:30:55 -07001265 return netlink_dump_start(ctnl, skb, nlh,
1266 ctnetlink_exp_dump_table,
1267 ctnetlink_done);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001268 }
1269
1270 if (cda[CTA_EXPECT_MASTER-1])
1271 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER, u3);
1272 else
1273 return -EINVAL;
1274
1275 if (err < 0)
1276 return err;
1277
Yasuyuki Kozakai468ec442006-11-29 02:35:23 +01001278 exp = nf_conntrack_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001279 if (!exp)
1280 return -ENOENT;
1281
1282 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001283 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001284 if (exp->id != ntohl(id)) {
1285 nf_conntrack_expect_put(exp);
1286 return -ENOENT;
1287 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001288 }
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001289
1290 err = -ENOMEM;
1291 skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL);
1292 if (!skb2)
1293 goto out;
Thomas Graf4e9b8262006-11-27 09:25:58 -08001294
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001295 err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid,
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001296 nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW,
1297 1, exp);
1298 if (err <= 0)
1299 goto free;
1300
1301 nf_conntrack_expect_put(exp);
1302
1303 return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT);
1304
1305free:
1306 kfree_skb(skb2);
1307out:
1308 nf_conntrack_expect_put(exp);
1309 return err;
1310}
1311
1312static int
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001313ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001314 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001315{
1316 struct nf_conntrack_expect *exp, *tmp;
1317 struct nf_conntrack_tuple tuple;
1318 struct nf_conntrack_helper *h;
1319 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1320 u_int8_t u3 = nfmsg->nfgen_family;
1321 int err;
1322
1323 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1324 return -EINVAL;
1325
1326 if (cda[CTA_EXPECT_TUPLE-1]) {
1327 /* delete a single expect by tuple */
1328 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1329 if (err < 0)
1330 return err;
1331
1332 /* bump usage count to 2 */
Yasuyuki Kozakai468ec442006-11-29 02:35:23 +01001333 exp = nf_conntrack_expect_find_get(&tuple);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001334 if (!exp)
1335 return -ENOENT;
1336
1337 if (cda[CTA_EXPECT_ID-1]) {
Patrick McHardybff9a892006-12-02 22:05:08 -08001338 __be32 id = *(__be32 *)NFA_DATA(cda[CTA_EXPECT_ID-1]);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001339 if (exp->id != ntohl(id)) {
1340 nf_conntrack_expect_put(exp);
1341 return -ENOENT;
1342 }
1343 }
1344
1345 /* after list removal, usage count == 1 */
1346 nf_conntrack_unexpect_related(exp);
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001347 /* have to put what we 'get' above.
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001348 * after this line usage count == 0 */
1349 nf_conntrack_expect_put(exp);
1350 } else if (cda[CTA_EXPECT_HELP_NAME-1]) {
1351 char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]);
1352
1353 /* delete all expectations for this helper */
1354 write_lock_bh(&nf_conntrack_lock);
1355 h = __nf_conntrack_helper_find_byname(name);
1356 if (!h) {
1357 write_unlock_bh(&nf_conntrack_lock);
1358 return -EINVAL;
1359 }
1360 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1361 list) {
Harald Weltedc808fe2006-03-20 17:56:32 -08001362 struct nf_conn_help *m_help = nfct_help(exp->master);
1363 if (m_help->helper == h
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001364 && del_timer(&exp->timeout)) {
1365 nf_ct_unlink_expect(exp);
1366 nf_conntrack_expect_put(exp);
1367 }
1368 }
1369 write_unlock_bh(&nf_conntrack_lock);
1370 } else {
1371 /* This basically means we have to flush everything*/
1372 write_lock_bh(&nf_conntrack_lock);
1373 list_for_each_entry_safe(exp, tmp, &nf_conntrack_expect_list,
1374 list) {
1375 if (del_timer(&exp->timeout)) {
1376 nf_ct_unlink_expect(exp);
1377 nf_conntrack_expect_put(exp);
1378 }
1379 }
1380 write_unlock_bh(&nf_conntrack_lock);
1381 }
1382
1383 return 0;
1384}
1385static int
1386ctnetlink_change_expect(struct nf_conntrack_expect *x, struct nfattr *cda[])
1387{
1388 return -EOPNOTSUPP;
1389}
1390
1391static int
1392ctnetlink_create_expect(struct nfattr *cda[], u_int8_t u3)
1393{
1394 struct nf_conntrack_tuple tuple, mask, master_tuple;
1395 struct nf_conntrack_tuple_hash *h = NULL;
1396 struct nf_conntrack_expect *exp;
1397 struct nf_conn *ct;
Harald Weltedc808fe2006-03-20 17:56:32 -08001398 struct nf_conn_help *help;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001399 int err = 0;
1400
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001401 /* caller guarantees that those three CTA_EXPECT_* exist */
1402 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1403 if (err < 0)
1404 return err;
1405 err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK, u3);
1406 if (err < 0)
1407 return err;
1408 err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER, u3);
1409 if (err < 0)
1410 return err;
1411
1412 /* Look for master conntrack of this expectation */
1413 h = nf_conntrack_find_get(&master_tuple, NULL);
1414 if (!h)
1415 return -ENOENT;
1416 ct = nf_ct_tuplehash_to_ctrack(h);
Harald Weltedc808fe2006-03-20 17:56:32 -08001417 help = nfct_help(ct);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001418
Harald Weltedc808fe2006-03-20 17:56:32 -08001419 if (!help || !help->helper) {
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001420 /* such conntrack hasn't got any helper, abort */
1421 err = -EINVAL;
1422 goto out;
1423 }
1424
1425 exp = nf_conntrack_expect_alloc(ct);
1426 if (!exp) {
1427 err = -ENOMEM;
1428 goto out;
1429 }
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001430
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001431 exp->expectfn = NULL;
1432 exp->flags = 0;
1433 exp->master = ct;
Patrick McHardy9457d852006-12-02 22:05:25 -08001434 exp->helper = NULL;
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001435 memcpy(&exp->tuple, &tuple, sizeof(struct nf_conntrack_tuple));
1436 memcpy(&exp->mask, &mask, sizeof(struct nf_conntrack_tuple));
1437
1438 err = nf_conntrack_expect_related(exp);
1439 nf_conntrack_expect_put(exp);
1440
YOSHIFUJI Hideaki601e68e2007-02-12 11:15:49 -08001441out:
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001442 nf_ct_put(nf_ct_tuplehash_to_ctrack(h));
1443 return err;
1444}
1445
1446static int
1447ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb,
Thomas Graf1d00a4e2007-03-22 23:30:12 -07001448 struct nlmsghdr *nlh, struct nfattr *cda[])
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001449{
1450 struct nf_conntrack_tuple tuple;
1451 struct nf_conntrack_expect *exp;
1452 struct nfgenmsg *nfmsg = NLMSG_DATA(nlh);
1453 u_int8_t u3 = nfmsg->nfgen_family;
1454 int err = 0;
1455
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001456 if (nfattr_bad_size(cda, CTA_EXPECT_MAX, cta_min_exp))
1457 return -EINVAL;
1458
1459 if (!cda[CTA_EXPECT_TUPLE-1]
1460 || !cda[CTA_EXPECT_MASK-1]
1461 || !cda[CTA_EXPECT_MASTER-1])
1462 return -EINVAL;
1463
1464 err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE, u3);
1465 if (err < 0)
1466 return err;
1467
1468 write_lock_bh(&nf_conntrack_lock);
1469 exp = __nf_conntrack_expect_find(&tuple);
1470
1471 if (!exp) {
1472 write_unlock_bh(&nf_conntrack_lock);
1473 err = -ENOENT;
1474 if (nlh->nlmsg_flags & NLM_F_CREATE)
1475 err = ctnetlink_create_expect(cda, u3);
1476 return err;
1477 }
1478
1479 err = -EEXIST;
1480 if (!(nlh->nlmsg_flags & NLM_F_EXCL))
1481 err = ctnetlink_change_expect(exp, cda);
1482 write_unlock_bh(&nf_conntrack_lock);
1483
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001484 return err;
1485}
1486
1487#ifdef CONFIG_NF_CONNTRACK_EVENTS
1488static struct notifier_block ctnl_notifier = {
1489 .notifier_call = ctnetlink_conntrack_event,
1490};
1491
1492static struct notifier_block ctnl_notifier_exp = {
1493 .notifier_call = ctnetlink_expect_event,
1494};
1495#endif
1496
1497static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = {
1498 [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack,
1499 .attr_count = CTA_MAX, },
1500 [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack,
1501 .attr_count = CTA_MAX, },
1502 [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack,
1503 .attr_count = CTA_MAX, },
1504 [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack,
1505 .attr_count = CTA_MAX, },
1506};
1507
1508static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = {
1509 [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect,
1510 .attr_count = CTA_EXPECT_MAX, },
1511 [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect,
1512 .attr_count = CTA_EXPECT_MAX, },
1513 [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect,
1514 .attr_count = CTA_EXPECT_MAX, },
1515};
1516
1517static struct nfnetlink_subsystem ctnl_subsys = {
1518 .name = "conntrack",
1519 .subsys_id = NFNL_SUBSYS_CTNETLINK,
1520 .cb_count = IPCTNL_MSG_MAX,
1521 .cb = ctnl_cb,
1522};
1523
1524static struct nfnetlink_subsystem ctnl_exp_subsys = {
1525 .name = "conntrack_expect",
1526 .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP,
1527 .cb_count = IPCTNL_MSG_EXP_MAX,
1528 .cb = ctnl_exp_cb,
1529};
1530
Patrick McHardyd2483dd2006-12-02 22:06:05 -08001531MODULE_ALIAS("ip_conntrack_netlink");
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001532MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK);
Pablo Neira Ayuso34f9a2e2006-02-04 02:11:41 -08001533MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK_EXP);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001534
1535static int __init ctnetlink_init(void)
1536{
1537 int ret;
1538
1539 printk("ctnetlink v%s: registering with nfnetlink.\n", version);
1540 ret = nfnetlink_subsys_register(&ctnl_subsys);
1541 if (ret < 0) {
1542 printk("ctnetlink_init: cannot register with nfnetlink.\n");
1543 goto err_out;
1544 }
1545
1546 ret = nfnetlink_subsys_register(&ctnl_exp_subsys);
1547 if (ret < 0) {
1548 printk("ctnetlink_init: cannot register exp with nfnetlink.\n");
1549 goto err_unreg_subsys;
1550 }
1551
1552#ifdef CONFIG_NF_CONNTRACK_EVENTS
1553 ret = nf_conntrack_register_notifier(&ctnl_notifier);
1554 if (ret < 0) {
1555 printk("ctnetlink_init: cannot register notifier.\n");
1556 goto err_unreg_exp_subsys;
1557 }
1558
1559 ret = nf_conntrack_expect_register_notifier(&ctnl_notifier_exp);
1560 if (ret < 0) {
1561 printk("ctnetlink_init: cannot expect register notifier.\n");
1562 goto err_unreg_notifier;
1563 }
1564#endif
1565
1566 return 0;
1567
1568#ifdef CONFIG_NF_CONNTRACK_EVENTS
1569err_unreg_notifier:
1570 nf_conntrack_unregister_notifier(&ctnl_notifier);
1571err_unreg_exp_subsys:
1572 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1573#endif
1574err_unreg_subsys:
1575 nfnetlink_subsys_unregister(&ctnl_subsys);
1576err_out:
1577 return ret;
1578}
1579
1580static void __exit ctnetlink_exit(void)
1581{
1582 printk("ctnetlink: unregistering from nfnetlink.\n");
1583
1584#ifdef CONFIG_NF_CONNTRACK_EVENTS
Martin Josefssone64a70b2006-04-01 02:24:48 -08001585 nf_conntrack_expect_unregister_notifier(&ctnl_notifier_exp);
Pablo Neira Ayusoc1d10ad2006-01-05 12:19:05 -08001586 nf_conntrack_unregister_notifier(&ctnl_notifier);
1587#endif
1588
1589 nfnetlink_subsys_unregister(&ctnl_exp_subsys);
1590 nfnetlink_subsys_unregister(&ctnl_subsys);
1591 return;
1592}
1593
1594module_init(ctnetlink_init);
1595module_exit(ctnetlink_exit);