Commit | Line | Data |
---|---|---|
080774a2 HW |
1 | /* 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> | |
5 | * (C) 2002-2005 by Harald Welte <laforge@gnumonks.org> | |
6 | * (C) 2003 by Patrick Mchardy <kaber@trash.net> | |
7 | * (C) 2005 by Pablo Neira Ayuso <pablo@eurodev.net> | |
8 | * | |
9 | * I've reworked this stuff to use attributes instead of conntrack | |
10 | * structures. 5.44 am. I need more tea. --pablo 05/07/11. | |
11 | * | |
12 | * Initial connection tracking via netlink development funded and | |
13 | * generally made possible by Network Robots, Inc. (www.networkrobots.com) | |
14 | * | |
15 | * Further development of this code funded by Astaro AG (http://www.astaro.com) | |
16 | * | |
17 | * This software may be used and distributed according to the terms | |
18 | * of the GNU General Public License, incorporated herein by reference. | |
19 | */ | |
20 | ||
21 | #include <linux/init.h> | |
22 | #include <linux/module.h> | |
23 | #include <linux/kernel.h> | |
24 | #include <linux/types.h> | |
25 | #include <linux/timer.h> | |
26 | #include <linux/skbuff.h> | |
27 | #include <linux/errno.h> | |
28 | #include <linux/netlink.h> | |
29 | #include <linux/spinlock.h> | |
30 | #include <linux/notifier.h> | |
080774a2 HW |
31 | |
32 | #include <linux/netfilter.h> | |
080774a2 HW |
33 | #include <linux/netfilter_ipv4/ip_conntrack.h> |
34 | #include <linux/netfilter_ipv4/ip_conntrack_core.h> | |
35 | #include <linux/netfilter_ipv4/ip_conntrack_helper.h> | |
36 | #include <linux/netfilter_ipv4/ip_conntrack_protocol.h> | |
37 | #include <linux/netfilter_ipv4/ip_nat_protocol.h> | |
38 | ||
39 | #include <linux/netfilter/nfnetlink.h> | |
40 | #include <linux/netfilter/nfnetlink_conntrack.h> | |
41 | ||
42 | MODULE_LICENSE("GPL"); | |
43 | ||
44 | static char __initdata version[] = "0.90"; | |
45 | ||
46 | #if 0 | |
47 | #define DEBUGP printk | |
48 | #else | |
49 | #define DEBUGP(format, args...) | |
50 | #endif | |
51 | ||
52 | ||
53 | static inline int | |
54 | ctnetlink_dump_tuples_proto(struct sk_buff *skb, | |
55 | const struct ip_conntrack_tuple *tuple) | |
56 | { | |
57 | struct ip_conntrack_protocol *proto; | |
eaae4fa4 | 58 | int ret = 0; |
080774a2 HW |
59 | |
60 | NFA_PUT(skb, CTA_PROTO_NUM, sizeof(u_int8_t), &tuple->dst.protonum); | |
61 | ||
62 | proto = ip_conntrack_proto_find_get(tuple->dst.protonum); | |
eaae4fa4 YK |
63 | if (likely(proto && proto->tuple_to_nfattr)) { |
64 | ret = proto->tuple_to_nfattr(skb, tuple); | |
65 | ip_conntrack_proto_put(proto); | |
66 | } | |
080774a2 | 67 | |
eaae4fa4 | 68 | return ret; |
080774a2 HW |
69 | |
70 | nfattr_failure: | |
71 | return -1; | |
72 | } | |
73 | ||
74 | static inline int | |
75 | ctnetlink_dump_tuples(struct sk_buff *skb, | |
76 | const struct ip_conntrack_tuple *tuple) | |
77 | { | |
78 | struct nfattr *nest_parms; | |
79 | ||
80 | nest_parms = NFA_NEST(skb, CTA_TUPLE_IP); | |
81 | NFA_PUT(skb, CTA_IP_V4_SRC, sizeof(u_int32_t), &tuple->src.ip); | |
82 | NFA_PUT(skb, CTA_IP_V4_DST, sizeof(u_int32_t), &tuple->dst.ip); | |
83 | NFA_NEST_END(skb, nest_parms); | |
84 | ||
85 | nest_parms = NFA_NEST(skb, CTA_TUPLE_PROTO); | |
86 | ctnetlink_dump_tuples_proto(skb, tuple); | |
87 | NFA_NEST_END(skb, nest_parms); | |
88 | ||
89 | return 0; | |
90 | ||
91 | nfattr_failure: | |
92 | return -1; | |
93 | } | |
94 | ||
95 | static inline int | |
96 | ctnetlink_dump_status(struct sk_buff *skb, const struct ip_conntrack *ct) | |
97 | { | |
98 | u_int32_t status = htonl((u_int32_t) ct->status); | |
99 | NFA_PUT(skb, CTA_STATUS, sizeof(status), &status); | |
100 | return 0; | |
101 | ||
102 | nfattr_failure: | |
103 | return -1; | |
104 | } | |
105 | ||
106 | static inline int | |
107 | ctnetlink_dump_timeout(struct sk_buff *skb, const struct ip_conntrack *ct) | |
108 | { | |
109 | long timeout_l = ct->timeout.expires - jiffies; | |
110 | u_int32_t timeout; | |
111 | ||
112 | if (timeout_l < 0) | |
113 | timeout = 0; | |
114 | else | |
115 | timeout = htonl(timeout_l / HZ); | |
116 | ||
117 | NFA_PUT(skb, CTA_TIMEOUT, sizeof(timeout), &timeout); | |
118 | return 0; | |
119 | ||
120 | nfattr_failure: | |
121 | return -1; | |
122 | } | |
123 | ||
124 | static inline int | |
125 | ctnetlink_dump_protoinfo(struct sk_buff *skb, const struct ip_conntrack *ct) | |
126 | { | |
127 | struct ip_conntrack_protocol *proto = ip_conntrack_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); | |
128 | ||
129 | struct nfattr *nest_proto; | |
130 | int ret; | |
131 | ||
132 | if (!proto || !proto->to_nfattr) | |
133 | return 0; | |
134 | ||
135 | nest_proto = NFA_NEST(skb, CTA_PROTOINFO); | |
136 | ||
137 | ret = proto->to_nfattr(skb, nest_proto, ct); | |
138 | ||
139 | ip_conntrack_proto_put(proto); | |
140 | ||
141 | NFA_NEST_END(skb, nest_proto); | |
142 | ||
143 | return ret; | |
144 | ||
145 | nfattr_failure: | |
146 | return -1; | |
147 | } | |
148 | ||
149 | static inline int | |
150 | ctnetlink_dump_helpinfo(struct sk_buff *skb, const struct ip_conntrack *ct) | |
151 | { | |
152 | struct nfattr *nest_helper; | |
153 | ||
154 | if (!ct->helper) | |
155 | return 0; | |
156 | ||
157 | nest_helper = NFA_NEST(skb, CTA_HELP); | |
158 | NFA_PUT(skb, CTA_HELP_NAME, CTA_HELP_MAXNAMESIZE, &ct->helper->name); | |
159 | ||
160 | if (ct->helper->to_nfattr) | |
161 | ct->helper->to_nfattr(skb, ct); | |
162 | ||
163 | NFA_NEST_END(skb, nest_helper); | |
164 | ||
165 | return 0; | |
166 | ||
167 | nfattr_failure: | |
168 | return -1; | |
169 | } | |
170 | ||
171 | #ifdef CONFIG_IP_NF_CT_ACCT | |
172 | static inline int | |
173 | ctnetlink_dump_counters(struct sk_buff *skb, const struct ip_conntrack *ct, | |
174 | enum ip_conntrack_dir dir) | |
175 | { | |
176 | enum ctattr_type type = dir ? CTA_COUNTERS_REPLY: CTA_COUNTERS_ORIG; | |
177 | struct nfattr *nest_count = NFA_NEST(skb, type); | |
46998f59 | 178 | u_int32_t tmp; |
080774a2 | 179 | |
a051a8f7 HW |
180 | tmp = htonl(ct->counters[dir].packets); |
181 | NFA_PUT(skb, CTA_COUNTERS32_PACKETS, sizeof(u_int32_t), &tmp); | |
080774a2 | 182 | |
a051a8f7 HW |
183 | tmp = htonl(ct->counters[dir].bytes); |
184 | NFA_PUT(skb, CTA_COUNTERS32_BYTES, sizeof(u_int32_t), &tmp); | |
080774a2 HW |
185 | |
186 | NFA_NEST_END(skb, nest_count); | |
187 | ||
188 | return 0; | |
189 | ||
190 | nfattr_failure: | |
191 | return -1; | |
192 | } | |
193 | #else | |
194 | #define ctnetlink_dump_counters(a, b, c) (0) | |
195 | #endif | |
196 | ||
197 | #ifdef CONFIG_IP_NF_CONNTRACK_MARK | |
198 | static inline int | |
199 | ctnetlink_dump_mark(struct sk_buff *skb, const struct ip_conntrack *ct) | |
200 | { | |
201 | u_int32_t mark = htonl(ct->mark); | |
202 | ||
203 | NFA_PUT(skb, CTA_MARK, sizeof(u_int32_t), &mark); | |
204 | return 0; | |
205 | ||
206 | nfattr_failure: | |
207 | return -1; | |
208 | } | |
209 | #else | |
210 | #define ctnetlink_dump_mark(a, b) (0) | |
211 | #endif | |
212 | ||
213 | static inline int | |
214 | ctnetlink_dump_id(struct sk_buff *skb, const struct ip_conntrack *ct) | |
215 | { | |
216 | u_int32_t id = htonl(ct->id); | |
217 | NFA_PUT(skb, CTA_ID, sizeof(u_int32_t), &id); | |
218 | return 0; | |
219 | ||
220 | nfattr_failure: | |
221 | return -1; | |
222 | } | |
223 | ||
224 | static inline int | |
225 | ctnetlink_dump_use(struct sk_buff *skb, const struct ip_conntrack *ct) | |
226 | { | |
227 | unsigned int use = htonl(atomic_read(&ct->ct_general.use)); | |
228 | ||
229 | NFA_PUT(skb, CTA_USE, sizeof(u_int32_t), &use); | |
230 | return 0; | |
231 | ||
232 | nfattr_failure: | |
233 | return -1; | |
234 | } | |
235 | ||
236 | #define tuple(ct, dir) (&(ct)->tuplehash[dir].tuple) | |
237 | ||
238 | static int | |
239 | ctnetlink_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
240 | int event, int nowait, | |
241 | const struct ip_conntrack *ct) | |
242 | { | |
243 | struct nlmsghdr *nlh; | |
244 | struct nfgenmsg *nfmsg; | |
245 | struct nfattr *nest_parms; | |
246 | unsigned char *b; | |
247 | ||
248 | b = skb->tail; | |
249 | ||
250 | event |= NFNL_SUBSYS_CTNETLINK << 8; | |
251 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); | |
252 | nfmsg = NLMSG_DATA(nlh); | |
253 | ||
254 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; | |
255 | nfmsg->nfgen_family = AF_INET; | |
256 | nfmsg->version = NFNETLINK_V0; | |
257 | nfmsg->res_id = 0; | |
258 | ||
259 | nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG); | |
260 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) | |
261 | goto nfattr_failure; | |
262 | NFA_NEST_END(skb, nest_parms); | |
263 | ||
264 | nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY); | |
265 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) | |
266 | goto nfattr_failure; | |
267 | NFA_NEST_END(skb, nest_parms); | |
268 | ||
269 | if (ctnetlink_dump_status(skb, ct) < 0 || | |
270 | ctnetlink_dump_timeout(skb, ct) < 0 || | |
271 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
272 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0 || | |
273 | ctnetlink_dump_protoinfo(skb, ct) < 0 || | |
274 | ctnetlink_dump_helpinfo(skb, ct) < 0 || | |
275 | ctnetlink_dump_mark(skb, ct) < 0 || | |
276 | ctnetlink_dump_id(skb, ct) < 0 || | |
277 | ctnetlink_dump_use(skb, ct) < 0) | |
278 | goto nfattr_failure; | |
279 | ||
280 | nlh->nlmsg_len = skb->tail - b; | |
281 | return skb->len; | |
282 | ||
283 | nlmsg_failure: | |
284 | nfattr_failure: | |
285 | skb_trim(skb, b - skb->data); | |
286 | return -1; | |
287 | } | |
288 | ||
289 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS | |
290 | static int ctnetlink_conntrack_event(struct notifier_block *this, | |
291 | unsigned long events, void *ptr) | |
292 | { | |
293 | struct nlmsghdr *nlh; | |
294 | struct nfgenmsg *nfmsg; | |
295 | struct nfattr *nest_parms; | |
296 | struct ip_conntrack *ct = (struct ip_conntrack *)ptr; | |
297 | struct sk_buff *skb; | |
298 | unsigned int type; | |
299 | unsigned char *b; | |
ac6d439d | 300 | unsigned int flags = 0, group; |
080774a2 HW |
301 | |
302 | /* ignore our fake conntrack entry */ | |
303 | if (ct == &ip_conntrack_untracked) | |
304 | return NOTIFY_DONE; | |
305 | ||
306 | if (events & IPCT_DESTROY) { | |
307 | type = IPCTNL_MSG_CT_DELETE; | |
ac6d439d | 308 | group = NFNLGRP_CONNTRACK_DESTROY; |
080774a2 HW |
309 | goto alloc_skb; |
310 | } | |
311 | if (events & (IPCT_NEW | IPCT_RELATED)) { | |
312 | type = IPCTNL_MSG_CT_NEW; | |
313 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
314 | /* dump everything */ | |
315 | events = ~0UL; | |
ac6d439d | 316 | group = NFNLGRP_CONNTRACK_NEW; |
080774a2 HW |
317 | goto alloc_skb; |
318 | } | |
319 | if (events & (IPCT_STATUS | | |
320 | IPCT_PROTOINFO | | |
321 | IPCT_HELPER | | |
322 | IPCT_HELPINFO | | |
323 | IPCT_NATINFO)) { | |
324 | type = IPCTNL_MSG_CT_NEW; | |
ac6d439d | 325 | group = NFNLGRP_CONNTRACK_UPDATE; |
080774a2 HW |
326 | goto alloc_skb; |
327 | } | |
328 | ||
329 | return NOTIFY_DONE; | |
330 | ||
331 | alloc_skb: | |
332 | /* FIXME: Check if there are any listeners before, don't hurt performance */ | |
333 | ||
334 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); | |
335 | if (!skb) | |
336 | return NOTIFY_DONE; | |
337 | ||
338 | b = skb->tail; | |
339 | ||
340 | type |= NFNL_SUBSYS_CTNETLINK << 8; | |
341 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); | |
342 | nfmsg = NLMSG_DATA(nlh); | |
343 | ||
344 | nlh->nlmsg_flags = flags; | |
345 | nfmsg->nfgen_family = AF_INET; | |
346 | nfmsg->version = NFNETLINK_V0; | |
347 | nfmsg->res_id = 0; | |
348 | ||
349 | nest_parms = NFA_NEST(skb, CTA_TUPLE_ORIG); | |
350 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_ORIGINAL)) < 0) | |
351 | goto nfattr_failure; | |
352 | NFA_NEST_END(skb, nest_parms); | |
353 | ||
354 | nest_parms = NFA_NEST(skb, CTA_TUPLE_REPLY); | |
355 | if (ctnetlink_dump_tuples(skb, tuple(ct, IP_CT_DIR_REPLY)) < 0) | |
356 | goto nfattr_failure; | |
357 | NFA_NEST_END(skb, nest_parms); | |
358 | ||
359 | /* NAT stuff is now a status flag */ | |
360 | if ((events & IPCT_STATUS || events & IPCT_NATINFO) | |
361 | && ctnetlink_dump_status(skb, ct) < 0) | |
362 | goto nfattr_failure; | |
363 | if (events & IPCT_REFRESH | |
364 | && ctnetlink_dump_timeout(skb, ct) < 0) | |
365 | goto nfattr_failure; | |
366 | if (events & IPCT_PROTOINFO | |
367 | && ctnetlink_dump_protoinfo(skb, ct) < 0) | |
368 | goto nfattr_failure; | |
369 | if (events & IPCT_HELPINFO | |
370 | && ctnetlink_dump_helpinfo(skb, ct) < 0) | |
371 | goto nfattr_failure; | |
372 | ||
373 | if (ctnetlink_dump_counters(skb, ct, IP_CT_DIR_ORIGINAL) < 0 || | |
374 | ctnetlink_dump_counters(skb, ct, IP_CT_DIR_REPLY) < 0) | |
375 | goto nfattr_failure; | |
376 | ||
377 | nlh->nlmsg_len = skb->tail - b; | |
ac6d439d | 378 | nfnetlink_send(skb, 0, group, 0); |
080774a2 HW |
379 | return NOTIFY_DONE; |
380 | ||
381 | nlmsg_failure: | |
382 | nfattr_failure: | |
383 | kfree_skb(skb); | |
384 | return NOTIFY_DONE; | |
385 | } | |
386 | #endif /* CONFIG_IP_NF_CONNTRACK_EVENTS */ | |
387 | ||
388 | static int ctnetlink_done(struct netlink_callback *cb) | |
389 | { | |
390 | DEBUGP("entered %s\n", __FUNCTION__); | |
391 | return 0; | |
392 | } | |
393 | ||
394 | static int | |
395 | ctnetlink_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
396 | { | |
397 | struct ip_conntrack *ct = NULL; | |
398 | struct ip_conntrack_tuple_hash *h; | |
399 | struct list_head *i; | |
400 | u_int32_t *id = (u_int32_t *) &cb->args[1]; | |
401 | ||
402 | DEBUGP("entered %s, last bucket=%lu id=%u\n", __FUNCTION__, | |
403 | cb->args[0], *id); | |
404 | ||
405 | read_lock_bh(&ip_conntrack_lock); | |
406 | for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) { | |
ff21d577 | 407 | list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) { |
080774a2 HW |
408 | h = (struct ip_conntrack_tuple_hash *) i; |
409 | if (DIRECTION(h) != IP_CT_DIR_ORIGINAL) | |
410 | continue; | |
411 | ct = tuplehash_to_ctrack(h); | |
412 | if (ct->id <= *id) | |
413 | continue; | |
414 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, | |
415 | cb->nlh->nlmsg_seq, | |
416 | IPCTNL_MSG_CT_NEW, | |
417 | 1, ct) < 0) | |
418 | goto out; | |
419 | *id = ct->id; | |
420 | } | |
421 | } | |
422 | out: | |
423 | read_unlock_bh(&ip_conntrack_lock); | |
424 | ||
425 | DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id); | |
426 | ||
427 | return skb->len; | |
428 | } | |
429 | ||
430 | #ifdef CONFIG_IP_NF_CT_ACCT | |
431 | static int | |
432 | ctnetlink_dump_table_w(struct sk_buff *skb, struct netlink_callback *cb) | |
433 | { | |
434 | struct ip_conntrack *ct = NULL; | |
435 | struct ip_conntrack_tuple_hash *h; | |
436 | struct list_head *i; | |
437 | u_int32_t *id = (u_int32_t *) &cb->args[1]; | |
438 | ||
439 | DEBUGP("entered %s, last bucket=%u id=%u\n", __FUNCTION__, | |
440 | cb->args[0], *id); | |
441 | ||
442 | write_lock_bh(&ip_conntrack_lock); | |
443 | for (; cb->args[0] < ip_conntrack_htable_size; cb->args[0]++, *id = 0) { | |
ff21d577 | 444 | list_for_each_prev(i, &ip_conntrack_hash[cb->args[0]]) { |
080774a2 HW |
445 | h = (struct ip_conntrack_tuple_hash *) i; |
446 | if (DIRECTION(h) != IP_CT_DIR_ORIGINAL) | |
447 | continue; | |
448 | ct = tuplehash_to_ctrack(h); | |
449 | if (ct->id <= *id) | |
450 | continue; | |
451 | if (ctnetlink_fill_info(skb, NETLINK_CB(cb->skb).pid, | |
452 | cb->nlh->nlmsg_seq, | |
453 | IPCTNL_MSG_CT_NEW, | |
454 | 1, ct) < 0) | |
455 | goto out; | |
456 | *id = ct->id; | |
457 | ||
458 | memset(&ct->counters, 0, sizeof(ct->counters)); | |
459 | } | |
460 | } | |
461 | out: | |
462 | write_unlock_bh(&ip_conntrack_lock); | |
463 | ||
464 | DEBUGP("leaving, last bucket=%lu id=%u\n", cb->args[0], *id); | |
465 | ||
466 | return skb->len; | |
467 | } | |
468 | #endif | |
469 | ||
470 | static const int cta_min_ip[CTA_IP_MAX] = { | |
471 | [CTA_IP_V4_SRC-1] = sizeof(u_int32_t), | |
472 | [CTA_IP_V4_DST-1] = sizeof(u_int32_t), | |
473 | }; | |
474 | ||
475 | static inline int | |
476 | ctnetlink_parse_tuple_ip(struct nfattr *attr, struct ip_conntrack_tuple *tuple) | |
477 | { | |
478 | struct nfattr *tb[CTA_IP_MAX]; | |
479 | ||
480 | DEBUGP("entered %s\n", __FUNCTION__); | |
481 | ||
a2506c04 | 482 | nfattr_parse_nested(tb, CTA_IP_MAX, attr); |
080774a2 HW |
483 | |
484 | if (nfattr_bad_size(tb, CTA_IP_MAX, cta_min_ip)) | |
485 | return -EINVAL; | |
486 | ||
487 | if (!tb[CTA_IP_V4_SRC-1]) | |
488 | return -EINVAL; | |
489 | tuple->src.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_SRC-1]); | |
490 | ||
491 | if (!tb[CTA_IP_V4_DST-1]) | |
492 | return -EINVAL; | |
493 | tuple->dst.ip = *(u_int32_t *)NFA_DATA(tb[CTA_IP_V4_DST-1]); | |
494 | ||
495 | DEBUGP("leaving\n"); | |
496 | ||
497 | return 0; | |
080774a2 HW |
498 | } |
499 | ||
500 | static const int cta_min_proto[CTA_PROTO_MAX] = { | |
501 | [CTA_PROTO_NUM-1] = sizeof(u_int16_t), | |
502 | [CTA_PROTO_SRC_PORT-1] = sizeof(u_int16_t), | |
503 | [CTA_PROTO_DST_PORT-1] = sizeof(u_int16_t), | |
504 | [CTA_PROTO_ICMP_TYPE-1] = sizeof(u_int8_t), | |
505 | [CTA_PROTO_ICMP_CODE-1] = sizeof(u_int8_t), | |
506 | [CTA_PROTO_ICMP_ID-1] = sizeof(u_int16_t), | |
507 | }; | |
508 | ||
509 | static inline int | |
510 | ctnetlink_parse_tuple_proto(struct nfattr *attr, | |
511 | struct ip_conntrack_tuple *tuple) | |
512 | { | |
513 | struct nfattr *tb[CTA_PROTO_MAX]; | |
514 | struct ip_conntrack_protocol *proto; | |
515 | int ret = 0; | |
516 | ||
517 | DEBUGP("entered %s\n", __FUNCTION__); | |
518 | ||
a2506c04 | 519 | nfattr_parse_nested(tb, CTA_PROTO_MAX, attr); |
080774a2 HW |
520 | |
521 | if (nfattr_bad_size(tb, CTA_PROTO_MAX, cta_min_proto)) | |
522 | return -EINVAL; | |
523 | ||
524 | if (!tb[CTA_PROTO_NUM-1]) | |
525 | return -EINVAL; | |
526 | tuple->dst.protonum = *(u_int16_t *)NFA_DATA(tb[CTA_PROTO_NUM-1]); | |
527 | ||
528 | proto = ip_conntrack_proto_find_get(tuple->dst.protonum); | |
529 | ||
530 | if (likely(proto && proto->nfattr_to_tuple)) { | |
531 | ret = proto->nfattr_to_tuple(tb, tuple); | |
532 | ip_conntrack_proto_put(proto); | |
533 | } | |
534 | ||
535 | return ret; | |
080774a2 HW |
536 | } |
537 | ||
538 | static inline int | |
539 | ctnetlink_parse_tuple(struct nfattr *cda[], struct ip_conntrack_tuple *tuple, | |
540 | enum ctattr_tuple type) | |
541 | { | |
542 | struct nfattr *tb[CTA_TUPLE_MAX]; | |
543 | int err; | |
544 | ||
545 | DEBUGP("entered %s\n", __FUNCTION__); | |
546 | ||
080774a2 HW |
547 | memset(tuple, 0, sizeof(*tuple)); |
548 | ||
a2506c04 | 549 | nfattr_parse_nested(tb, CTA_TUPLE_MAX, cda[type-1]); |
080774a2 HW |
550 | |
551 | if (!tb[CTA_TUPLE_IP-1]) | |
552 | return -EINVAL; | |
553 | ||
554 | err = ctnetlink_parse_tuple_ip(tb[CTA_TUPLE_IP-1], tuple); | |
555 | if (err < 0) | |
556 | return err; | |
557 | ||
558 | if (!tb[CTA_TUPLE_PROTO-1]) | |
559 | return -EINVAL; | |
560 | ||
561 | err = ctnetlink_parse_tuple_proto(tb[CTA_TUPLE_PROTO-1], tuple); | |
562 | if (err < 0) | |
563 | return err; | |
564 | ||
565 | /* orig and expect tuples get DIR_ORIGINAL */ | |
566 | if (type == CTA_TUPLE_REPLY) | |
567 | tuple->dst.dir = IP_CT_DIR_REPLY; | |
568 | else | |
569 | tuple->dst.dir = IP_CT_DIR_ORIGINAL; | |
570 | ||
571 | DUMP_TUPLE(tuple); | |
572 | ||
573 | DEBUGP("leaving\n"); | |
574 | ||
575 | return 0; | |
080774a2 HW |
576 | } |
577 | ||
578 | #ifdef CONFIG_IP_NF_NAT_NEEDED | |
579 | static const int cta_min_protonat[CTA_PROTONAT_MAX] = { | |
580 | [CTA_PROTONAT_PORT_MIN-1] = sizeof(u_int16_t), | |
581 | [CTA_PROTONAT_PORT_MAX-1] = sizeof(u_int16_t), | |
582 | }; | |
583 | ||
584 | static int ctnetlink_parse_nat_proto(struct nfattr *attr, | |
585 | const struct ip_conntrack *ct, | |
586 | struct ip_nat_range *range) | |
587 | { | |
588 | struct nfattr *tb[CTA_PROTONAT_MAX]; | |
589 | struct ip_nat_protocol *npt; | |
590 | ||
591 | DEBUGP("entered %s\n", __FUNCTION__); | |
592 | ||
a2506c04 | 593 | nfattr_parse_nested(tb, CTA_PROTONAT_MAX, attr); |
080774a2 HW |
594 | |
595 | if (nfattr_bad_size(tb, CTA_PROTONAT_MAX, cta_min_protonat)) | |
fe902a91 | 596 | return -EINVAL; |
080774a2 HW |
597 | |
598 | npt = ip_nat_proto_find_get(ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum); | |
599 | if (!npt) | |
600 | return 0; | |
601 | ||
602 | if (!npt->nfattr_to_range) { | |
603 | ip_nat_proto_put(npt); | |
604 | return 0; | |
605 | } | |
606 | ||
607 | /* nfattr_to_range returns 1 if it parsed, 0 if not, neg. on error */ | |
608 | if (npt->nfattr_to_range(tb, range) > 0) | |
609 | range->flags |= IP_NAT_RANGE_PROTO_SPECIFIED; | |
610 | ||
611 | ip_nat_proto_put(npt); | |
612 | ||
613 | DEBUGP("leaving\n"); | |
614 | return 0; | |
080774a2 HW |
615 | } |
616 | ||
617 | static inline int | |
618 | ctnetlink_parse_nat(struct nfattr *cda[], | |
619 | const struct ip_conntrack *ct, struct ip_nat_range *range) | |
620 | { | |
621 | struct nfattr *tb[CTA_NAT_MAX]; | |
622 | int err; | |
623 | ||
624 | DEBUGP("entered %s\n", __FUNCTION__); | |
625 | ||
080774a2 HW |
626 | memset(range, 0, sizeof(*range)); |
627 | ||
a2506c04 | 628 | nfattr_parse_nested(tb, CTA_NAT_MAX, cda[CTA_NAT-1]); |
080774a2 HW |
629 | |
630 | if (tb[CTA_NAT_MINIP-1]) | |
631 | range->min_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MINIP-1]); | |
632 | ||
633 | if (!tb[CTA_NAT_MAXIP-1]) | |
634 | range->max_ip = range->min_ip; | |
635 | else | |
636 | range->max_ip = *(u_int32_t *)NFA_DATA(tb[CTA_NAT_MAXIP-1]); | |
637 | ||
638 | if (range->min_ip) | |
639 | range->flags |= IP_NAT_RANGE_MAP_IPS; | |
640 | ||
641 | if (!tb[CTA_NAT_PROTO-1]) | |
642 | return 0; | |
643 | ||
644 | err = ctnetlink_parse_nat_proto(tb[CTA_NAT_PROTO-1], ct, range); | |
645 | if (err < 0) | |
646 | return err; | |
647 | ||
648 | DEBUGP("leaving\n"); | |
649 | return 0; | |
080774a2 HW |
650 | } |
651 | #endif | |
652 | ||
653 | static inline int | |
654 | ctnetlink_parse_help(struct nfattr *attr, char **helper_name) | |
655 | { | |
656 | struct nfattr *tb[CTA_HELP_MAX]; | |
657 | ||
658 | DEBUGP("entered %s\n", __FUNCTION__); | |
080774a2 | 659 | |
a2506c04 | 660 | nfattr_parse_nested(tb, CTA_HELP_MAX, attr); |
080774a2 HW |
661 | |
662 | if (!tb[CTA_HELP_NAME-1]) | |
663 | return -EINVAL; | |
664 | ||
665 | *helper_name = NFA_DATA(tb[CTA_HELP_NAME-1]); | |
666 | ||
667 | return 0; | |
080774a2 HW |
668 | } |
669 | ||
670 | static int | |
671 | ctnetlink_del_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
672 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) | |
673 | { | |
674 | struct ip_conntrack_tuple_hash *h; | |
675 | struct ip_conntrack_tuple tuple; | |
676 | struct ip_conntrack *ct; | |
677 | int err = 0; | |
678 | ||
679 | DEBUGP("entered %s\n", __FUNCTION__); | |
680 | ||
681 | if (cda[CTA_TUPLE_ORIG-1]) | |
682 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG); | |
683 | else if (cda[CTA_TUPLE_REPLY-1]) | |
684 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY); | |
685 | else { | |
686 | /* Flush the whole table */ | |
687 | ip_conntrack_flush(); | |
688 | return 0; | |
689 | } | |
690 | ||
691 | if (err < 0) | |
692 | return err; | |
693 | ||
694 | h = ip_conntrack_find_get(&tuple, NULL); | |
695 | if (!h) { | |
696 | DEBUGP("tuple not found in conntrack hash\n"); | |
697 | return -ENOENT; | |
698 | } | |
699 | ||
700 | ct = tuplehash_to_ctrack(h); | |
701 | ||
702 | if (cda[CTA_ID-1]) { | |
703 | u_int32_t id = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_ID-1])); | |
704 | if (ct->id != id) { | |
705 | ip_conntrack_put(ct); | |
706 | return -ENOENT; | |
707 | } | |
708 | } | |
709 | if (del_timer(&ct->timeout)) { | |
710 | ip_conntrack_put(ct); | |
711 | ct->timeout.function((unsigned long)ct); | |
712 | return 0; | |
713 | } | |
714 | ip_conntrack_put(ct); | |
715 | DEBUGP("leaving\n"); | |
716 | ||
717 | return 0; | |
718 | } | |
719 | ||
720 | static int | |
721 | ctnetlink_get_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
722 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) | |
723 | { | |
724 | struct ip_conntrack_tuple_hash *h; | |
725 | struct ip_conntrack_tuple tuple; | |
726 | struct ip_conntrack *ct; | |
727 | struct sk_buff *skb2 = NULL; | |
728 | int err = 0; | |
729 | ||
730 | DEBUGP("entered %s\n", __FUNCTION__); | |
731 | ||
732 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
733 | struct nfgenmsg *msg = NLMSG_DATA(nlh); | |
734 | u32 rlen; | |
735 | ||
736 | if (msg->nfgen_family != AF_INET) | |
737 | return -EAFNOSUPPORT; | |
738 | ||
739 | if (NFNL_MSG_TYPE(nlh->nlmsg_type) == | |
740 | IPCTNL_MSG_CT_GET_CTRZERO) { | |
741 | #ifdef CONFIG_IP_NF_CT_ACCT | |
742 | if ((*errp = netlink_dump_start(ctnl, skb, nlh, | |
743 | ctnetlink_dump_table_w, | |
744 | ctnetlink_done)) != 0) | |
745 | return -EINVAL; | |
746 | #else | |
747 | return -ENOTSUPP; | |
748 | #endif | |
749 | } else { | |
750 | if ((*errp = netlink_dump_start(ctnl, skb, nlh, | |
751 | ctnetlink_dump_table, | |
752 | ctnetlink_done)) != 0) | |
753 | return -EINVAL; | |
754 | } | |
755 | ||
756 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); | |
757 | if (rlen > skb->len) | |
758 | rlen = skb->len; | |
759 | skb_pull(skb, rlen); | |
760 | return 0; | |
761 | } | |
762 | ||
763 | if (cda[CTA_TUPLE_ORIG-1]) | |
764 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_ORIG); | |
765 | else if (cda[CTA_TUPLE_REPLY-1]) | |
766 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_TUPLE_REPLY); | |
767 | else | |
768 | return -EINVAL; | |
769 | ||
770 | if (err < 0) | |
771 | return err; | |
772 | ||
773 | h = ip_conntrack_find_get(&tuple, NULL); | |
774 | if (!h) { | |
775 | DEBUGP("tuple not found in conntrack hash"); | |
776 | return -ENOENT; | |
777 | } | |
778 | DEBUGP("tuple found\n"); | |
779 | ct = tuplehash_to_ctrack(h); | |
780 | ||
781 | err = -ENOMEM; | |
81e5c27d | 782 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); |
080774a2 HW |
783 | if (!skb2) { |
784 | ip_conntrack_put(ct); | |
785 | return -ENOMEM; | |
786 | } | |
787 | NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid; | |
788 | ||
789 | err = ctnetlink_fill_info(skb2, NETLINK_CB(skb).pid, nlh->nlmsg_seq, | |
790 | IPCTNL_MSG_CT_NEW, 1, ct); | |
791 | ip_conntrack_put(ct); | |
792 | if (err <= 0) | |
0f81eb4d | 793 | goto free; |
080774a2 HW |
794 | |
795 | err = netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); | |
796 | if (err < 0) | |
797 | goto out; | |
798 | ||
799 | DEBUGP("leaving\n"); | |
800 | return 0; | |
801 | ||
0f81eb4d HW |
802 | free: |
803 | kfree_skb(skb2); | |
080774a2 | 804 | out: |
fcda4612 | 805 | return err; |
080774a2 HW |
806 | } |
807 | ||
808 | static inline int | |
809 | ctnetlink_change_status(struct ip_conntrack *ct, struct nfattr *cda[]) | |
810 | { | |
d000eaf7 HW |
811 | unsigned long d; |
812 | unsigned status = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_STATUS-1])); | |
080774a2 HW |
813 | d = ct->status ^ status; |
814 | ||
815 | if (d & (IPS_EXPECTED|IPS_CONFIRMED|IPS_DYING)) | |
816 | /* unchangeable */ | |
817 | return -EINVAL; | |
818 | ||
819 | if (d & IPS_SEEN_REPLY && !(status & IPS_SEEN_REPLY)) | |
820 | /* SEEN_REPLY bit can only be set */ | |
821 | return -EINVAL; | |
822 | ||
823 | ||
824 | if (d & IPS_ASSURED && !(status & IPS_ASSURED)) | |
825 | /* ASSURED bit can only be set */ | |
826 | return -EINVAL; | |
827 | ||
828 | if (cda[CTA_NAT-1]) { | |
829 | #ifndef CONFIG_IP_NF_NAT_NEEDED | |
830 | return -EINVAL; | |
831 | #else | |
832 | unsigned int hooknum; | |
833 | struct ip_nat_range range; | |
834 | ||
835 | if (ctnetlink_parse_nat(cda, ct, &range) < 0) | |
836 | return -EINVAL; | |
837 | ||
838 | DEBUGP("NAT: %u.%u.%u.%u-%u.%u.%u.%u:%u-%u\n", | |
839 | NIPQUAD(range.min_ip), NIPQUAD(range.max_ip), | |
840 | htons(range.min.all), htons(range.max.all)); | |
841 | ||
842 | /* This is tricky but it works. ip_nat_setup_info needs the | |
843 | * hook number as parameter, so let's do the correct | |
844 | * conversion and run away */ | |
845 | if (status & IPS_SRC_NAT_DONE) | |
846 | hooknum = NF_IP_POST_ROUTING; /* IP_NAT_MANIP_SRC */ | |
847 | else if (status & IPS_DST_NAT_DONE) | |
848 | hooknum = NF_IP_PRE_ROUTING; /* IP_NAT_MANIP_DST */ | |
849 | else | |
850 | return -EINVAL; /* Missing NAT flags */ | |
851 | ||
852 | DEBUGP("NAT status: %lu\n", | |
853 | status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK)); | |
854 | ||
855 | if (ip_nat_initialized(ct, hooknum)) | |
856 | return -EEXIST; | |
857 | ip_nat_setup_info(ct, &range, hooknum); | |
858 | ||
859 | DEBUGP("NAT status after setup_info: %lu\n", | |
860 | ct->status & (IPS_NAT_MASK | IPS_NAT_DONE_MASK)); | |
861 | #endif | |
862 | } | |
863 | ||
864 | /* Be careful here, modifying NAT bits can screw up things, | |
865 | * so don't let users modify them directly if they don't pass | |
866 | * ip_nat_range. */ | |
867 | ct->status |= status & ~(IPS_NAT_DONE_MASK | IPS_NAT_MASK); | |
868 | return 0; | |
869 | } | |
870 | ||
871 | ||
872 | static inline int | |
873 | ctnetlink_change_helper(struct ip_conntrack *ct, struct nfattr *cda[]) | |
874 | { | |
875 | struct ip_conntrack_helper *helper; | |
876 | char *helpname; | |
877 | int err; | |
878 | ||
879 | DEBUGP("entered %s\n", __FUNCTION__); | |
880 | ||
881 | /* don't change helper of sibling connections */ | |
882 | if (ct->master) | |
883 | return -EINVAL; | |
884 | ||
885 | err = ctnetlink_parse_help(cda[CTA_HELP-1], &helpname); | |
886 | if (err < 0) | |
887 | return err; | |
888 | ||
889 | helper = __ip_conntrack_helper_find_byname(helpname); | |
890 | if (!helper) { | |
891 | if (!strcmp(helpname, "")) | |
892 | helper = NULL; | |
893 | else | |
894 | return -EINVAL; | |
895 | } | |
896 | ||
897 | if (ct->helper) { | |
898 | if (!helper) { | |
899 | /* we had a helper before ... */ | |
900 | ip_ct_remove_expectations(ct); | |
901 | ct->helper = NULL; | |
902 | } else { | |
903 | /* need to zero data of old helper */ | |
904 | memset(&ct->help, 0, sizeof(ct->help)); | |
905 | } | |
906 | } | |
907 | ||
908 | ct->helper = helper; | |
909 | ||
910 | return 0; | |
911 | } | |
912 | ||
913 | static inline int | |
914 | ctnetlink_change_timeout(struct ip_conntrack *ct, struct nfattr *cda[]) | |
915 | { | |
916 | u_int32_t timeout = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1])); | |
917 | ||
918 | if (!del_timer(&ct->timeout)) | |
919 | return -ETIME; | |
920 | ||
921 | ct->timeout.expires = jiffies + timeout * HZ; | |
922 | add_timer(&ct->timeout); | |
923 | ||
924 | return 0; | |
925 | } | |
926 | ||
061cb4a0 PNA |
927 | static inline int |
928 | ctnetlink_change_protoinfo(struct ip_conntrack *ct, struct nfattr *cda[]) | |
929 | { | |
930 | struct nfattr *tb[CTA_PROTOINFO_MAX], *attr = cda[CTA_PROTOINFO-1]; | |
931 | struct ip_conntrack_protocol *proto; | |
932 | u_int16_t npt = ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum; | |
933 | int err = 0; | |
934 | ||
a2506c04 | 935 | nfattr_parse_nested(tb, CTA_PROTOINFO_MAX, attr); |
061cb4a0 PNA |
936 | |
937 | proto = ip_conntrack_proto_find_get(npt); | |
938 | if (!proto) | |
939 | return -EINVAL; | |
940 | ||
941 | if (proto->from_nfattr) | |
942 | err = proto->from_nfattr(tb, ct); | |
943 | ip_conntrack_proto_put(proto); | |
944 | ||
945 | return err; | |
061cb4a0 PNA |
946 | } |
947 | ||
080774a2 HW |
948 | static int |
949 | ctnetlink_change_conntrack(struct ip_conntrack *ct, struct nfattr *cda[]) | |
950 | { | |
951 | int err; | |
952 | ||
953 | DEBUGP("entered %s\n", __FUNCTION__); | |
954 | ||
955 | if (cda[CTA_HELP-1]) { | |
956 | err = ctnetlink_change_helper(ct, cda); | |
957 | if (err < 0) | |
958 | return err; | |
959 | } | |
960 | ||
961 | if (cda[CTA_TIMEOUT-1]) { | |
962 | err = ctnetlink_change_timeout(ct, cda); | |
963 | if (err < 0) | |
964 | return err; | |
965 | } | |
966 | ||
967 | if (cda[CTA_STATUS-1]) { | |
968 | err = ctnetlink_change_status(ct, cda); | |
969 | if (err < 0) | |
970 | return err; | |
971 | } | |
972 | ||
061cb4a0 PNA |
973 | if (cda[CTA_PROTOINFO-1]) { |
974 | err = ctnetlink_change_protoinfo(ct, cda); | |
975 | if (err < 0) | |
976 | return err; | |
977 | } | |
978 | ||
02a78cdf PNA |
979 | #if defined(CONFIG_IP_NF_CONNTRACK_MARK) |
980 | if (cda[CTA_MARK-1]) | |
981 | ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1])); | |
982 | #endif | |
983 | ||
080774a2 HW |
984 | DEBUGP("all done\n"); |
985 | return 0; | |
986 | } | |
987 | ||
988 | static int | |
989 | ctnetlink_create_conntrack(struct nfattr *cda[], | |
990 | struct ip_conntrack_tuple *otuple, | |
991 | struct ip_conntrack_tuple *rtuple) | |
992 | { | |
993 | struct ip_conntrack *ct; | |
994 | int err = -EINVAL; | |
995 | ||
996 | DEBUGP("entered %s\n", __FUNCTION__); | |
997 | ||
998 | ct = ip_conntrack_alloc(otuple, rtuple); | |
999 | if (ct == NULL || IS_ERR(ct)) | |
1000 | return -ENOMEM; | |
1001 | ||
1002 | if (!cda[CTA_TIMEOUT-1]) | |
1003 | goto err; | |
1004 | ct->timeout.expires = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_TIMEOUT-1])); | |
1005 | ||
1006 | ct->timeout.expires = jiffies + ct->timeout.expires * HZ; | |
1007 | ct->status |= IPS_CONFIRMED; | |
1008 | ||
1009 | err = ctnetlink_change_status(ct, cda); | |
1010 | if (err < 0) | |
1011 | goto err; | |
1012 | ||
061cb4a0 PNA |
1013 | if (cda[CTA_PROTOINFO-1]) { |
1014 | err = ctnetlink_change_protoinfo(ct, cda); | |
1015 | if (err < 0) | |
1016 | return err; | |
1017 | } | |
1018 | ||
080774a2 HW |
1019 | ct->helper = ip_conntrack_helper_find_get(rtuple); |
1020 | ||
1021 | add_timer(&ct->timeout); | |
1022 | ip_conntrack_hash_insert(ct); | |
1023 | ||
1024 | if (ct->helper) | |
1025 | ip_conntrack_helper_put(ct->helper); | |
1026 | ||
02a78cdf PNA |
1027 | #if defined(CONFIG_IP_NF_CONNTRACK_MARK) |
1028 | if (cda[CTA_MARK-1]) | |
1029 | ct->mark = ntohl(*(u_int32_t *)NFA_DATA(cda[CTA_MARK-1])); | |
1030 | #endif | |
1031 | ||
080774a2 HW |
1032 | DEBUGP("conntrack with id %u inserted\n", ct->id); |
1033 | return 0; | |
1034 | ||
1035 | err: | |
1036 | ip_conntrack_free(ct); | |
1037 | return err; | |
1038 | } | |
1039 | ||
1040 | static int | |
1041 | ctnetlink_new_conntrack(struct sock *ctnl, struct sk_buff *skb, | |
1042 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) | |
1043 | { | |
1044 | struct ip_conntrack_tuple otuple, rtuple; | |
1045 | struct ip_conntrack_tuple_hash *h = NULL; | |
1046 | int err = 0; | |
1047 | ||
1048 | DEBUGP("entered %s\n", __FUNCTION__); | |
1049 | ||
1050 | if (cda[CTA_TUPLE_ORIG-1]) { | |
1051 | err = ctnetlink_parse_tuple(cda, &otuple, CTA_TUPLE_ORIG); | |
1052 | if (err < 0) | |
1053 | return err; | |
1054 | } | |
1055 | ||
1056 | if (cda[CTA_TUPLE_REPLY-1]) { | |
1057 | err = ctnetlink_parse_tuple(cda, &rtuple, CTA_TUPLE_REPLY); | |
1058 | if (err < 0) | |
1059 | return err; | |
1060 | } | |
1061 | ||
1062 | write_lock_bh(&ip_conntrack_lock); | |
1063 | if (cda[CTA_TUPLE_ORIG-1]) | |
1064 | h = __ip_conntrack_find(&otuple, NULL); | |
1065 | else if (cda[CTA_TUPLE_REPLY-1]) | |
1066 | h = __ip_conntrack_find(&rtuple, NULL); | |
1067 | ||
1068 | if (h == NULL) { | |
1069 | write_unlock_bh(&ip_conntrack_lock); | |
1070 | DEBUGP("no such conntrack, create new\n"); | |
1071 | err = -ENOENT; | |
1072 | if (nlh->nlmsg_flags & NLM_F_CREATE) | |
1073 | err = ctnetlink_create_conntrack(cda, &otuple, &rtuple); | |
88aa0429 PN |
1074 | return err; |
1075 | } | |
1076 | /* implicit 'else' */ | |
1077 | ||
1078 | /* we only allow nat config for new conntracks */ | |
1079 | if (cda[CTA_NAT-1]) { | |
1080 | err = -EINVAL; | |
080774a2 | 1081 | goto out_unlock; |
080774a2 HW |
1082 | } |
1083 | ||
1084 | /* We manipulate the conntrack inside the global conntrack table lock, | |
1085 | * so there's no need to increase the refcount */ | |
1086 | DEBUGP("conntrack found\n"); | |
1087 | err = -EEXIST; | |
1088 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
1089 | err = ctnetlink_change_conntrack(tuplehash_to_ctrack(h), cda); | |
1090 | ||
1091 | out_unlock: | |
1092 | write_unlock_bh(&ip_conntrack_lock); | |
1093 | return err; | |
1094 | } | |
1095 | ||
1096 | /*********************************************************************** | |
1097 | * EXPECT | |
1098 | ***********************************************************************/ | |
1099 | ||
1100 | static inline int | |
1101 | ctnetlink_exp_dump_tuple(struct sk_buff *skb, | |
1102 | const struct ip_conntrack_tuple *tuple, | |
1103 | enum ctattr_expect type) | |
1104 | { | |
1105 | struct nfattr *nest_parms = NFA_NEST(skb, type); | |
1106 | ||
1107 | if (ctnetlink_dump_tuples(skb, tuple) < 0) | |
1108 | goto nfattr_failure; | |
1109 | ||
1110 | NFA_NEST_END(skb, nest_parms); | |
1111 | ||
1112 | return 0; | |
1113 | ||
1114 | nfattr_failure: | |
1115 | return -1; | |
1116 | } | |
1117 | ||
1118 | static inline int | |
1119 | ctnetlink_exp_dump_expect(struct sk_buff *skb, | |
1120 | const struct ip_conntrack_expect *exp) | |
1121 | { | |
1444fc55 | 1122 | struct ip_conntrack *master = exp->master; |
080774a2 HW |
1123 | u_int32_t timeout = htonl((exp->timeout.expires - jiffies) / HZ); |
1124 | u_int32_t id = htonl(exp->id); | |
080774a2 HW |
1125 | |
1126 | if (ctnetlink_exp_dump_tuple(skb, &exp->tuple, CTA_EXPECT_TUPLE) < 0) | |
1127 | goto nfattr_failure; | |
1128 | if (ctnetlink_exp_dump_tuple(skb, &exp->mask, CTA_EXPECT_MASK) < 0) | |
1129 | goto nfattr_failure; | |
1444fc55 HW |
1130 | if (ctnetlink_exp_dump_tuple(skb, |
1131 | &master->tuplehash[IP_CT_DIR_ORIGINAL].tuple, | |
1132 | CTA_EXPECT_MASTER) < 0) | |
1133 | goto nfattr_failure; | |
080774a2 HW |
1134 | |
1135 | NFA_PUT(skb, CTA_EXPECT_TIMEOUT, sizeof(timeout), &timeout); | |
1136 | NFA_PUT(skb, CTA_EXPECT_ID, sizeof(u_int32_t), &id); | |
080774a2 HW |
1137 | |
1138 | return 0; | |
1139 | ||
1140 | nfattr_failure: | |
1141 | return -1; | |
1142 | } | |
1143 | ||
1144 | static int | |
1145 | ctnetlink_exp_fill_info(struct sk_buff *skb, u32 pid, u32 seq, | |
1146 | int event, | |
1147 | int nowait, | |
1148 | const struct ip_conntrack_expect *exp) | |
1149 | { | |
1150 | struct nlmsghdr *nlh; | |
1151 | struct nfgenmsg *nfmsg; | |
1152 | unsigned char *b; | |
1153 | ||
1154 | b = skb->tail; | |
1155 | ||
1156 | event |= NFNL_SUBSYS_CTNETLINK_EXP << 8; | |
1157 | nlh = NLMSG_PUT(skb, pid, seq, event, sizeof(struct nfgenmsg)); | |
1158 | nfmsg = NLMSG_DATA(nlh); | |
1159 | ||
1160 | nlh->nlmsg_flags = (nowait && pid) ? NLM_F_MULTI : 0; | |
1161 | nfmsg->nfgen_family = AF_INET; | |
1162 | nfmsg->version = NFNETLINK_V0; | |
1163 | nfmsg->res_id = 0; | |
1164 | ||
1165 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
1166 | goto nfattr_failure; | |
1167 | ||
1168 | nlh->nlmsg_len = skb->tail - b; | |
1169 | return skb->len; | |
1170 | ||
1171 | nlmsg_failure: | |
1172 | nfattr_failure: | |
1173 | skb_trim(skb, b - skb->data); | |
1174 | return -1; | |
1175 | } | |
1176 | ||
1177 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS | |
1178 | static int ctnetlink_expect_event(struct notifier_block *this, | |
1179 | unsigned long events, void *ptr) | |
1180 | { | |
1181 | struct nlmsghdr *nlh; | |
1182 | struct nfgenmsg *nfmsg; | |
1183 | struct ip_conntrack_expect *exp = (struct ip_conntrack_expect *)ptr; | |
1184 | struct sk_buff *skb; | |
1185 | unsigned int type; | |
1186 | unsigned char *b; | |
1187 | int flags = 0; | |
1188 | u16 proto; | |
1189 | ||
1190 | if (events & IPEXP_NEW) { | |
1191 | type = IPCTNL_MSG_EXP_NEW; | |
1192 | flags = NLM_F_CREATE|NLM_F_EXCL; | |
1193 | } else | |
1194 | return NOTIFY_DONE; | |
1195 | ||
1196 | skb = alloc_skb(NLMSG_GOODSIZE, GFP_ATOMIC); | |
1197 | if (!skb) | |
1198 | return NOTIFY_DONE; | |
1199 | ||
1200 | b = skb->tail; | |
1201 | ||
1202 | type |= NFNL_SUBSYS_CTNETLINK << 8; | |
1203 | nlh = NLMSG_PUT(skb, 0, 0, type, sizeof(struct nfgenmsg)); | |
1204 | nfmsg = NLMSG_DATA(nlh); | |
1205 | ||
1206 | nlh->nlmsg_flags = flags; | |
1207 | nfmsg->nfgen_family = AF_INET; | |
1208 | nfmsg->version = NFNETLINK_V0; | |
1209 | nfmsg->res_id = 0; | |
1210 | ||
1211 | if (ctnetlink_exp_dump_expect(skb, exp) < 0) | |
1212 | goto nfattr_failure; | |
1213 | ||
1214 | nlh->nlmsg_len = skb->tail - b; | |
1215 | proto = exp->tuple.dst.protonum; | |
ac6d439d | 1216 | nfnetlink_send(skb, 0, NFNLGRP_CONNTRACK_EXP_NEW, 0); |
080774a2 HW |
1217 | return NOTIFY_DONE; |
1218 | ||
1219 | nlmsg_failure: | |
1220 | nfattr_failure: | |
1221 | kfree_skb(skb); | |
1222 | return NOTIFY_DONE; | |
1223 | } | |
1224 | #endif | |
1225 | ||
1226 | static int | |
1227 | ctnetlink_exp_dump_table(struct sk_buff *skb, struct netlink_callback *cb) | |
1228 | { | |
1229 | struct ip_conntrack_expect *exp = NULL; | |
1230 | struct list_head *i; | |
1231 | u_int32_t *id = (u_int32_t *) &cb->args[0]; | |
1232 | ||
1233 | DEBUGP("entered %s, last id=%llu\n", __FUNCTION__, *id); | |
1234 | ||
1235 | read_lock_bh(&ip_conntrack_lock); | |
ff21d577 | 1236 | list_for_each_prev(i, &ip_conntrack_expect_list) { |
080774a2 HW |
1237 | exp = (struct ip_conntrack_expect *) i; |
1238 | if (exp->id <= *id) | |
1239 | continue; | |
1240 | if (ctnetlink_exp_fill_info(skb, NETLINK_CB(cb->skb).pid, | |
1241 | cb->nlh->nlmsg_seq, | |
1242 | IPCTNL_MSG_EXP_NEW, | |
1243 | 1, exp) < 0) | |
1244 | goto out; | |
1245 | *id = exp->id; | |
1246 | } | |
1247 | out: | |
1248 | read_unlock_bh(&ip_conntrack_lock); | |
1249 | ||
1250 | DEBUGP("leaving, last id=%llu\n", *id); | |
1251 | ||
1252 | return skb->len; | |
1253 | } | |
1254 | ||
1255 | static int | |
1256 | ctnetlink_get_expect(struct sock *ctnl, struct sk_buff *skb, | |
1257 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) | |
1258 | { | |
1259 | struct ip_conntrack_tuple tuple; | |
1260 | struct ip_conntrack_expect *exp; | |
1261 | struct sk_buff *skb2; | |
1262 | int err = 0; | |
1263 | ||
1264 | DEBUGP("entered %s\n", __FUNCTION__); | |
1265 | ||
1266 | if (nlh->nlmsg_flags & NLM_F_DUMP) { | |
1267 | struct nfgenmsg *msg = NLMSG_DATA(nlh); | |
1268 | u32 rlen; | |
1269 | ||
1270 | if (msg->nfgen_family != AF_INET) | |
1271 | return -EAFNOSUPPORT; | |
1272 | ||
1273 | if ((*errp = netlink_dump_start(ctnl, skb, nlh, | |
1274 | ctnetlink_exp_dump_table, | |
1275 | ctnetlink_done)) != 0) | |
1276 | return -EINVAL; | |
1277 | rlen = NLMSG_ALIGN(nlh->nlmsg_len); | |
1278 | if (rlen > skb->len) | |
1279 | rlen = skb->len; | |
1280 | skb_pull(skb, rlen); | |
1281 | return 0; | |
1282 | } | |
1283 | ||
1444fc55 HW |
1284 | if (cda[CTA_EXPECT_MASTER-1]) |
1285 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_MASTER); | |
080774a2 HW |
1286 | else |
1287 | return -EINVAL; | |
1288 | ||
1289 | if (err < 0) | |
1290 | return err; | |
1291 | ||
a41bc002 | 1292 | exp = ip_conntrack_expect_find(&tuple); |
080774a2 HW |
1293 | if (!exp) |
1294 | return -ENOENT; | |
1295 | ||
1296 | err = -ENOMEM; | |
1297 | skb2 = alloc_skb(NLMSG_GOODSIZE, GFP_KERNEL); | |
1298 | if (!skb2) | |
1299 | goto out; | |
1300 | NETLINK_CB(skb2).dst_pid = NETLINK_CB(skb).pid; | |
1301 | ||
1302 | err = ctnetlink_exp_fill_info(skb2, NETLINK_CB(skb).pid, | |
1303 | nlh->nlmsg_seq, IPCTNL_MSG_EXP_NEW, | |
1304 | 1, exp); | |
1305 | if (err <= 0) | |
0f81eb4d | 1306 | goto free; |
080774a2 HW |
1307 | |
1308 | ip_conntrack_expect_put(exp); | |
1309 | ||
0f81eb4d | 1310 | return netlink_unicast(ctnl, skb2, NETLINK_CB(skb).pid, MSG_DONTWAIT); |
080774a2 | 1311 | |
0f81eb4d HW |
1312 | free: |
1313 | kfree_skb(skb2); | |
080774a2 HW |
1314 | out: |
1315 | ip_conntrack_expect_put(exp); | |
080774a2 HW |
1316 | return err; |
1317 | } | |
1318 | ||
1319 | static int | |
1320 | ctnetlink_del_expect(struct sock *ctnl, struct sk_buff *skb, | |
1321 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) | |
1322 | { | |
1323 | struct ip_conntrack_expect *exp, *tmp; | |
1324 | struct ip_conntrack_tuple tuple; | |
1325 | struct ip_conntrack_helper *h; | |
1326 | int err; | |
1327 | ||
1444fc55 HW |
1328 | if (cda[CTA_EXPECT_TUPLE-1]) { |
1329 | /* delete a single expect by tuple */ | |
1330 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE); | |
1331 | if (err < 0) | |
1332 | return err; | |
1333 | ||
1334 | /* bump usage count to 2 */ | |
a41bc002 | 1335 | exp = ip_conntrack_expect_find(&tuple); |
1444fc55 HW |
1336 | if (!exp) |
1337 | return -ENOENT; | |
1338 | ||
1339 | if (cda[CTA_EXPECT_ID-1]) { | |
1340 | u_int32_t id = | |
1341 | *(u_int32_t *)NFA_DATA(cda[CTA_EXPECT_ID-1]); | |
1342 | if (exp->id != ntohl(id)) { | |
1343 | ip_conntrack_expect_put(exp); | |
1344 | return -ENOENT; | |
1345 | } | |
1346 | } | |
1347 | ||
1348 | /* after list removal, usage count == 1 */ | |
1349 | ip_conntrack_unexpect_related(exp); | |
1350 | /* have to put what we 'get' above. | |
1351 | * after this line usage count == 0 */ | |
1352 | ip_conntrack_expect_put(exp); | |
1353 | } else if (cda[CTA_EXPECT_HELP_NAME-1]) { | |
1354 | char *name = NFA_DATA(cda[CTA_EXPECT_HELP_NAME-1]); | |
080774a2 HW |
1355 | |
1356 | /* delete all expectations for this helper */ | |
1357 | write_lock_bh(&ip_conntrack_lock); | |
1358 | h = __ip_conntrack_helper_find_byname(name); | |
1359 | if (!h) { | |
1360 | write_unlock_bh(&ip_conntrack_lock); | |
1361 | return -EINVAL; | |
1362 | } | |
1363 | list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list, | |
1364 | list) { | |
1365 | if (exp->master->helper == h | |
49719eb3 PNA |
1366 | && del_timer(&exp->timeout)) { |
1367 | ip_ct_unlink_expect(exp); | |
1368 | ip_conntrack_expect_put(exp); | |
1369 | } | |
080774a2 HW |
1370 | } |
1371 | write_unlock(&ip_conntrack_lock); | |
080774a2 HW |
1372 | } else { |
1373 | /* This basically means we have to flush everything*/ | |
1374 | write_lock_bh(&ip_conntrack_lock); | |
1375 | list_for_each_entry_safe(exp, tmp, &ip_conntrack_expect_list, | |
1376 | list) { | |
49719eb3 PNA |
1377 | if (del_timer(&exp->timeout)) { |
1378 | ip_ct_unlink_expect(exp); | |
1379 | ip_conntrack_expect_put(exp); | |
1380 | } | |
080774a2 HW |
1381 | } |
1382 | write_unlock_bh(&ip_conntrack_lock); | |
080774a2 HW |
1383 | } |
1384 | ||
080774a2 HW |
1385 | return 0; |
1386 | } | |
1387 | static int | |
1388 | ctnetlink_change_expect(struct ip_conntrack_expect *x, struct nfattr *cda[]) | |
1389 | { | |
1390 | return -EOPNOTSUPP; | |
1391 | } | |
1392 | ||
1393 | static int | |
1394 | ctnetlink_create_expect(struct nfattr *cda[]) | |
1395 | { | |
1396 | struct ip_conntrack_tuple tuple, mask, master_tuple; | |
1397 | struct ip_conntrack_tuple_hash *h = NULL; | |
1398 | struct ip_conntrack_expect *exp; | |
1399 | struct ip_conntrack *ct; | |
1400 | int err = 0; | |
1401 | ||
1402 | DEBUGP("entered %s\n", __FUNCTION__); | |
1403 | ||
1444fc55 | 1404 | /* caller guarantees that those three CTA_EXPECT_* exist */ |
080774a2 HW |
1405 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE); |
1406 | if (err < 0) | |
1407 | return err; | |
bd9a26b7 | 1408 | err = ctnetlink_parse_tuple(cda, &mask, CTA_EXPECT_MASK); |
080774a2 HW |
1409 | if (err < 0) |
1410 | return err; | |
1444fc55 | 1411 | err = ctnetlink_parse_tuple(cda, &master_tuple, CTA_EXPECT_MASTER); |
080774a2 HW |
1412 | if (err < 0) |
1413 | return err; | |
1414 | ||
1415 | /* Look for master conntrack of this expectation */ | |
1416 | h = ip_conntrack_find_get(&master_tuple, NULL); | |
1417 | if (!h) | |
1418 | return -ENOENT; | |
1419 | ct = tuplehash_to_ctrack(h); | |
1420 | ||
1421 | if (!ct->helper) { | |
1422 | /* such conntrack hasn't got any helper, abort */ | |
1423 | err = -EINVAL; | |
1424 | goto out; | |
1425 | } | |
1426 | ||
1427 | exp = ip_conntrack_expect_alloc(ct); | |
1428 | if (!exp) { | |
1429 | err = -ENOMEM; | |
1430 | goto out; | |
1431 | } | |
1432 | ||
1433 | exp->expectfn = NULL; | |
2248bcfc | 1434 | exp->flags = 0; |
080774a2 HW |
1435 | exp->master = ct; |
1436 | memcpy(&exp->tuple, &tuple, sizeof(struct ip_conntrack_tuple)); | |
1437 | memcpy(&exp->mask, &mask, sizeof(struct ip_conntrack_tuple)); | |
1438 | ||
1439 | err = ip_conntrack_expect_related(exp); | |
1440 | ip_conntrack_expect_put(exp); | |
1441 | ||
1442 | out: | |
1443 | ip_conntrack_put(tuplehash_to_ctrack(h)); | |
1444 | return err; | |
1445 | } | |
1446 | ||
1447 | static int | |
1448 | ctnetlink_new_expect(struct sock *ctnl, struct sk_buff *skb, | |
1449 | struct nlmsghdr *nlh, struct nfattr *cda[], int *errp) | |
1450 | { | |
1451 | struct ip_conntrack_tuple tuple; | |
1452 | struct ip_conntrack_expect *exp; | |
1453 | int err = 0; | |
1454 | ||
1455 | DEBUGP("entered %s\n", __FUNCTION__); | |
1456 | ||
1444fc55 HW |
1457 | if (!cda[CTA_EXPECT_TUPLE-1] |
1458 | || !cda[CTA_EXPECT_MASK-1] | |
1459 | || !cda[CTA_EXPECT_MASTER-1]) | |
080774a2 HW |
1460 | return -EINVAL; |
1461 | ||
1462 | err = ctnetlink_parse_tuple(cda, &tuple, CTA_EXPECT_TUPLE); | |
1463 | if (err < 0) | |
1464 | return err; | |
1465 | ||
1466 | write_lock_bh(&ip_conntrack_lock); | |
1467 | exp = __ip_conntrack_expect_find(&tuple); | |
1468 | ||
1469 | if (!exp) { | |
1470 | write_unlock_bh(&ip_conntrack_lock); | |
1471 | err = -ENOENT; | |
1472 | if (nlh->nlmsg_flags & NLM_F_CREATE) | |
1473 | err = ctnetlink_create_expect(cda); | |
1474 | return err; | |
1475 | } | |
1476 | ||
1477 | err = -EEXIST; | |
1478 | if (!(nlh->nlmsg_flags & NLM_F_EXCL)) | |
1479 | err = ctnetlink_change_expect(exp, cda); | |
1480 | write_unlock_bh(&ip_conntrack_lock); | |
1481 | ||
1482 | DEBUGP("leaving\n"); | |
1483 | ||
1484 | return err; | |
1485 | } | |
1486 | ||
1487 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS | |
1488 | static struct notifier_block ctnl_notifier = { | |
1489 | .notifier_call = ctnetlink_conntrack_event, | |
1490 | }; | |
1491 | ||
1492 | static struct notifier_block ctnl_notifier_exp = { | |
1493 | .notifier_call = ctnetlink_expect_event, | |
1494 | }; | |
1495 | #endif | |
1496 | ||
1497 | static struct nfnl_callback ctnl_cb[IPCTNL_MSG_MAX] = { | |
1498 | [IPCTNL_MSG_CT_NEW] = { .call = ctnetlink_new_conntrack, | |
927ccbcc | 1499 | .attr_count = CTA_MAX, |
080774a2 HW |
1500 | .cap_required = CAP_NET_ADMIN }, |
1501 | [IPCTNL_MSG_CT_GET] = { .call = ctnetlink_get_conntrack, | |
927ccbcc | 1502 | .attr_count = CTA_MAX, |
080774a2 HW |
1503 | .cap_required = CAP_NET_ADMIN }, |
1504 | [IPCTNL_MSG_CT_DELETE] = { .call = ctnetlink_del_conntrack, | |
927ccbcc | 1505 | .attr_count = CTA_MAX, |
080774a2 HW |
1506 | .cap_required = CAP_NET_ADMIN }, |
1507 | [IPCTNL_MSG_CT_GET_CTRZERO] = { .call = ctnetlink_get_conntrack, | |
927ccbcc | 1508 | .attr_count = CTA_MAX, |
080774a2 HW |
1509 | .cap_required = CAP_NET_ADMIN }, |
1510 | }; | |
1511 | ||
28b19d99 | 1512 | static struct nfnl_callback ctnl_exp_cb[IPCTNL_MSG_EXP_MAX] = { |
080774a2 | 1513 | [IPCTNL_MSG_EXP_GET] = { .call = ctnetlink_get_expect, |
927ccbcc | 1514 | .attr_count = CTA_EXPECT_MAX, |
080774a2 HW |
1515 | .cap_required = CAP_NET_ADMIN }, |
1516 | [IPCTNL_MSG_EXP_NEW] = { .call = ctnetlink_new_expect, | |
927ccbcc | 1517 | .attr_count = CTA_EXPECT_MAX, |
080774a2 HW |
1518 | .cap_required = CAP_NET_ADMIN }, |
1519 | [IPCTNL_MSG_EXP_DELETE] = { .call = ctnetlink_del_expect, | |
927ccbcc | 1520 | .attr_count = CTA_EXPECT_MAX, |
080774a2 HW |
1521 | .cap_required = CAP_NET_ADMIN }, |
1522 | }; | |
1523 | ||
1524 | static struct nfnetlink_subsystem ctnl_subsys = { | |
1525 | .name = "conntrack", | |
1526 | .subsys_id = NFNL_SUBSYS_CTNETLINK, | |
1527 | .cb_count = IPCTNL_MSG_MAX, | |
080774a2 HW |
1528 | .cb = ctnl_cb, |
1529 | }; | |
1530 | ||
1531 | static struct nfnetlink_subsystem ctnl_exp_subsys = { | |
1532 | .name = "conntrack_expect", | |
1533 | .subsys_id = NFNL_SUBSYS_CTNETLINK_EXP, | |
1534 | .cb_count = IPCTNL_MSG_EXP_MAX, | |
080774a2 HW |
1535 | .cb = ctnl_exp_cb, |
1536 | }; | |
1537 | ||
119a3184 PNA |
1538 | MODULE_ALIAS_NFNL_SUBSYS(NFNL_SUBSYS_CTNETLINK); |
1539 | ||
080774a2 HW |
1540 | static int __init ctnetlink_init(void) |
1541 | { | |
1542 | int ret; | |
1543 | ||
1544 | printk("ctnetlink v%s: registering with nfnetlink.\n", version); | |
1545 | ret = nfnetlink_subsys_register(&ctnl_subsys); | |
1546 | if (ret < 0) { | |
1547 | printk("ctnetlink_init: cannot register with nfnetlink.\n"); | |
1548 | goto err_out; | |
1549 | } | |
1550 | ||
1551 | ret = nfnetlink_subsys_register(&ctnl_exp_subsys); | |
1552 | if (ret < 0) { | |
1553 | printk("ctnetlink_init: cannot register exp with nfnetlink.\n"); | |
1554 | goto err_unreg_subsys; | |
1555 | } | |
1556 | ||
1557 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS | |
1558 | ret = ip_conntrack_register_notifier(&ctnl_notifier); | |
1559 | if (ret < 0) { | |
1560 | printk("ctnetlink_init: cannot register notifier.\n"); | |
1561 | goto err_unreg_exp_subsys; | |
1562 | } | |
1563 | ||
1564 | ret = ip_conntrack_expect_register_notifier(&ctnl_notifier_exp); | |
1565 | if (ret < 0) { | |
1566 | printk("ctnetlink_init: cannot expect register notifier.\n"); | |
1567 | goto err_unreg_notifier; | |
1568 | } | |
1569 | #endif | |
1570 | ||
1571 | return 0; | |
1572 | ||
1573 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS | |
1574 | err_unreg_notifier: | |
1575 | ip_conntrack_unregister_notifier(&ctnl_notifier); | |
1576 | err_unreg_exp_subsys: | |
1577 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
1578 | #endif | |
1579 | err_unreg_subsys: | |
1580 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
1581 | err_out: | |
1582 | return ret; | |
1583 | } | |
1584 | ||
1585 | static void __exit ctnetlink_exit(void) | |
1586 | { | |
1587 | printk("ctnetlink: unregistering from nfnetlink.\n"); | |
1588 | ||
1589 | #ifdef CONFIG_IP_NF_CONNTRACK_EVENTS | |
1590 | ip_conntrack_unregister_notifier(&ctnl_notifier_exp); | |
1591 | ip_conntrack_unregister_notifier(&ctnl_notifier); | |
1592 | #endif | |
1593 | ||
1594 | nfnetlink_subsys_unregister(&ctnl_exp_subsys); | |
1595 | nfnetlink_subsys_unregister(&ctnl_subsys); | |
1596 | return; | |
1597 | } | |
1598 | ||
1599 | module_init(ctnetlink_init); | |
1600 | module_exit(ctnetlink_exit); |