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