libnl  3.2.24-rc1
ct.c
1 /*
2  * lib/netfilter/ct.c Conntrack
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation version 2.1
7  * of the License.
8  *
9  * Copyright (c) 2003-2008 Thomas Graf <tgraf@suug.ch>
10  * Copyright (c) 2007 Philip Craig <philipc@snapgear.com>
11  * Copyright (c) 2007 Secure Computing Corporation
12  * Copyright (c= 2008 Patrick McHardy <kaber@trash.net>
13  */
14 
15 /**
16  * @ingroup nfnl
17  * @defgroup ct Conntrack
18  * @brief
19  * @{
20  */
21 
22 #include <byteswap.h>
23 #include <sys/types.h>
24 #include <linux/netfilter/nfnetlink_conntrack.h>
25 
26 #include <netlink-private/netlink.h>
27 #include <netlink/attr.h>
28 #include <netlink/netfilter/nfnl.h>
29 #include <netlink/netfilter/ct.h>
30 
31 static struct nl_cache_ops nfnl_ct_ops;
32 
33 #if __BYTE_ORDER == __BIG_ENDIAN
34 static uint64_t ntohll(uint64_t x)
35 {
36  return x;
37 }
38 #elif __BYTE_ORDER == __LITTLE_ENDIAN
39 static uint64_t ntohll(uint64_t x)
40 {
41  return bswap_64(x);
42 }
43 #endif
44 
45 static struct nla_policy ct_policy[CTA_MAX+1] = {
46  [CTA_TUPLE_ORIG] = { .type = NLA_NESTED },
47  [CTA_TUPLE_REPLY] = { .type = NLA_NESTED },
48  [CTA_STATUS] = { .type = NLA_U32 },
49  [CTA_PROTOINFO] = { .type = NLA_NESTED },
50  //[CTA_HELP]
51  //[CTA_NAT_SRC]
52  [CTA_TIMEOUT] = { .type = NLA_U32 },
53  [CTA_MARK] = { .type = NLA_U32 },
54  [CTA_COUNTERS_ORIG] = { .type = NLA_NESTED },
55  [CTA_COUNTERS_REPLY] = { .type = NLA_NESTED },
56  [CTA_USE] = { .type = NLA_U32 },
57  [CTA_ID] = { .type = NLA_U32 },
58  [CTA_ZONE] = { .type = NLA_U16 },
59  //[CTA_NAT_DST]
60 };
61 
62 static struct nla_policy ct_tuple_policy[CTA_TUPLE_MAX+1] = {
63  [CTA_TUPLE_IP] = { .type = NLA_NESTED },
64  [CTA_TUPLE_PROTO] = { .type = NLA_NESTED },
65 };
66 
67 static struct nla_policy ct_ip_policy[CTA_IP_MAX+1] = {
68  [CTA_IP_V4_SRC] = { .type = NLA_U32 },
69  [CTA_IP_V4_DST] = { .type = NLA_U32 },
70  [CTA_IP_V6_SRC] = { .minlen = 16 },
71  [CTA_IP_V6_DST] = { .minlen = 16 },
72 };
73 
74 static struct nla_policy ct_proto_policy[CTA_PROTO_MAX+1] = {
75  [CTA_PROTO_NUM] = { .type = NLA_U8 },
76  [CTA_PROTO_SRC_PORT] = { .type = NLA_U16 },
77  [CTA_PROTO_DST_PORT] = { .type = NLA_U16 },
78  [CTA_PROTO_ICMP_ID] = { .type = NLA_U16 },
79  [CTA_PROTO_ICMP_TYPE] = { .type = NLA_U8 },
80  [CTA_PROTO_ICMP_CODE] = { .type = NLA_U8 },
81  [CTA_PROTO_ICMPV6_ID] = { .type = NLA_U16 },
82  [CTA_PROTO_ICMPV6_TYPE] = { .type = NLA_U8 },
83  [CTA_PROTO_ICMPV6_CODE] = { .type = NLA_U8 },
84 };
85 
86 static struct nla_policy ct_protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
87  [CTA_PROTOINFO_TCP] = { .type = NLA_NESTED },
88 };
89 
90 static struct nla_policy ct_protoinfo_tcp_policy[CTA_PROTOINFO_TCP_MAX+1] = {
91  [CTA_PROTOINFO_TCP_STATE] = { .type = NLA_U8 },
92  [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type = NLA_U8 },
93  [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type = NLA_U8 },
94  [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .minlen = 2 },
95  [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .minlen = 2 },
96 
97 };
98 
99 static struct nla_policy ct_counters_policy[CTA_COUNTERS_MAX+1] = {
100  [CTA_COUNTERS_PACKETS] = { .type = NLA_U64 },
101  [CTA_COUNTERS_BYTES] = { .type = NLA_U64 },
102  [CTA_COUNTERS32_PACKETS]= { .type = NLA_U32 },
103  [CTA_COUNTERS32_BYTES] = { .type = NLA_U32 },
104 };
105 
106 static struct nla_policy ct_timestamp_policy[CTA_TIMESTAMP_MAX + 1] = {
107  [CTA_TIMESTAMP_START] = { .type = NLA_U64 },
108  [CTA_TIMESTAMP_STOP] = { .type = NLA_U64 },
109 };
110 
111 static int ct_parse_ip(struct nfnl_ct *ct, int repl, struct nlattr *attr)
112 {
113  struct nlattr *tb[CTA_IP_MAX+1];
114  struct nl_addr *addr;
115  int err;
116 
117  err = nla_parse_nested(tb, CTA_IP_MAX, attr, ct_ip_policy);
118  if (err < 0)
119  goto errout;
120 
121  if (tb[CTA_IP_V4_SRC]) {
122  addr = nl_addr_alloc_attr(tb[CTA_IP_V4_SRC], AF_INET);
123  if (addr == NULL)
124  goto errout_enomem;
125  err = nfnl_ct_set_src(ct, repl, addr);
126  nl_addr_put(addr);
127  if (err < 0)
128  goto errout;
129  }
130  if (tb[CTA_IP_V4_DST]) {
131  addr = nl_addr_alloc_attr(tb[CTA_IP_V4_DST], AF_INET);
132  if (addr == NULL)
133  goto errout_enomem;
134  err = nfnl_ct_set_dst(ct, repl, addr);
135  nl_addr_put(addr);
136  if (err < 0)
137  goto errout;
138  }
139  if (tb[CTA_IP_V6_SRC]) {
140  addr = nl_addr_alloc_attr(tb[CTA_IP_V6_SRC], AF_INET6);
141  if (addr == NULL)
142  goto errout_enomem;
143  err = nfnl_ct_set_src(ct, repl, addr);
144  nl_addr_put(addr);
145  if (err < 0)
146  goto errout;
147  }
148  if (tb[CTA_IP_V6_DST]) {
149  addr = nl_addr_alloc_attr(tb[CTA_IP_V6_DST], AF_INET6);
150  if (addr == NULL)
151  goto errout_enomem;
152  err = nfnl_ct_set_dst(ct, repl, addr);
153  nl_addr_put(addr);
154  if (err < 0)
155  goto errout;
156  }
157 
158  return 0;
159 
160 errout_enomem:
161  err = -NLE_NOMEM;
162 errout:
163  return err;
164 }
165 
166 static int ct_parse_proto(struct nfnl_ct *ct, int repl, struct nlattr *attr)
167 {
168  struct nlattr *tb[CTA_PROTO_MAX+1];
169  int err;
170 
171  err = nla_parse_nested(tb, CTA_PROTO_MAX, attr, ct_proto_policy);
172  if (err < 0)
173  return err;
174 
175  if (!repl && tb[CTA_PROTO_NUM])
176  nfnl_ct_set_proto(ct, nla_get_u8(tb[CTA_PROTO_NUM]));
177  if (tb[CTA_PROTO_SRC_PORT])
178  nfnl_ct_set_src_port(ct, repl,
179  ntohs(nla_get_u16(tb[CTA_PROTO_SRC_PORT])));
180  if (tb[CTA_PROTO_DST_PORT])
181  nfnl_ct_set_dst_port(ct, repl,
182  ntohs(nla_get_u16(tb[CTA_PROTO_DST_PORT])));
183 
184  if (ct->ct_family == AF_INET) {
185  if (tb[CTA_PROTO_ICMP_ID])
186  nfnl_ct_set_icmp_id(ct, repl,
187  ntohs(nla_get_u16(tb[CTA_PROTO_ICMP_ID])));
188  if (tb[CTA_PROTO_ICMP_TYPE])
189  nfnl_ct_set_icmp_type(ct, repl,
190  nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]));
191  if (tb[CTA_PROTO_ICMP_CODE])
192  nfnl_ct_set_icmp_code(ct, repl,
193  nla_get_u8(tb[CTA_PROTO_ICMP_CODE]));
194  } else if (ct->ct_family == AF_INET6) {
195  if (tb[CTA_PROTO_ICMPV6_ID])
196  nfnl_ct_set_icmp_id(ct, repl,
197  ntohs(nla_get_u16(tb[CTA_PROTO_ICMPV6_ID])));
198  if (tb[CTA_PROTO_ICMPV6_TYPE])
199  nfnl_ct_set_icmp_type(ct, repl,
200  nla_get_u8(tb[CTA_PROTO_ICMPV6_TYPE]));
201  if (tb[CTA_PROTO_ICMPV6_CODE])
202  nfnl_ct_set_icmp_code(ct, repl,
203  nla_get_u8(tb[CTA_PROTO_ICMPV6_CODE]));
204  }
205 
206  return 0;
207 }
208 
209 static int ct_parse_tuple(struct nfnl_ct *ct, int repl, struct nlattr *attr)
210 {
211  struct nlattr *tb[CTA_TUPLE_MAX+1];
212  int err;
213 
214  err = nla_parse_nested(tb, CTA_TUPLE_MAX, attr, ct_tuple_policy);
215  if (err < 0)
216  return err;
217 
218  if (tb[CTA_TUPLE_IP]) {
219  err = ct_parse_ip(ct, repl, tb[CTA_TUPLE_IP]);
220  if (err < 0)
221  return err;
222  }
223 
224  if (tb[CTA_TUPLE_PROTO]) {
225  err = ct_parse_proto(ct, repl, tb[CTA_TUPLE_PROTO]);
226  if (err < 0)
227  return err;
228  }
229 
230  return 0;
231 }
232 
233 static int ct_parse_protoinfo_tcp(struct nfnl_ct *ct, struct nlattr *attr)
234 {
235  struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
236  int err;
237 
238  err = nla_parse_nested(tb, CTA_PROTOINFO_TCP_MAX, attr,
239  ct_protoinfo_tcp_policy);
240  if (err < 0)
241  return err;
242 
243  if (tb[CTA_PROTOINFO_TCP_STATE])
244  nfnl_ct_set_tcp_state(ct,
245  nla_get_u8(tb[CTA_PROTOINFO_TCP_STATE]));
246 
247  return 0;
248 }
249 
250 static int ct_parse_protoinfo(struct nfnl_ct *ct, struct nlattr *attr)
251 {
252  struct nlattr *tb[CTA_PROTOINFO_MAX+1];
253  int err;
254 
255  err = nla_parse_nested(tb, CTA_PROTOINFO_MAX, attr,
256  ct_protoinfo_policy);
257  if (err < 0)
258  return err;
259 
260  if (tb[CTA_PROTOINFO_TCP]) {
261  err = ct_parse_protoinfo_tcp(ct, tb[CTA_PROTOINFO_TCP]);
262  if (err < 0)
263  return err;
264  }
265 
266  return 0;
267 }
268 
269 static int ct_parse_counters(struct nfnl_ct *ct, int repl, struct nlattr *attr)
270 {
271  struct nlattr *tb[CTA_COUNTERS_MAX+1];
272  int err;
273 
274  err = nla_parse_nested(tb, CTA_COUNTERS_MAX, attr, ct_counters_policy);
275  if (err < 0)
276  return err;
277 
278  if (tb[CTA_COUNTERS_PACKETS])
279  nfnl_ct_set_packets(ct, repl,
280  ntohll(nla_get_u64(tb[CTA_COUNTERS_PACKETS])));
281  if (tb[CTA_COUNTERS32_PACKETS])
282  nfnl_ct_set_packets(ct, repl,
283  ntohl(nla_get_u32(tb[CTA_COUNTERS32_PACKETS])));
284  if (tb[CTA_COUNTERS_BYTES])
285  nfnl_ct_set_bytes(ct, repl,
286  ntohll(nla_get_u64(tb[CTA_COUNTERS_BYTES])));
287  if (tb[CTA_COUNTERS32_BYTES])
288  nfnl_ct_set_bytes(ct, repl,
289  ntohl(nla_get_u32(tb[CTA_COUNTERS32_BYTES])));
290 
291  return 0;
292 }
293 
294 int nfnlmsg_ct_group(struct nlmsghdr *nlh)
295 {
296  switch (nfnlmsg_subtype(nlh)) {
297  case IPCTNL_MSG_CT_NEW:
298  if (nlh->nlmsg_flags & (NLM_F_CREATE|NLM_F_EXCL))
299  return NFNLGRP_CONNTRACK_NEW;
300  else
301  return NFNLGRP_CONNTRACK_UPDATE;
302  case IPCTNL_MSG_CT_DELETE:
303  return NFNLGRP_CONNTRACK_DESTROY;
304  default:
305  return NFNLGRP_NONE;
306  }
307 }
308 
309 static int ct_parse_timestamp(struct nfnl_ct *ct, struct nlattr *attr)
310 {
311  struct nlattr *tb[CTA_TIMESTAMP_MAX + 1];
312  int err;
313 
314  err = nla_parse_nested(tb, CTA_TIMESTAMP_MAX, attr,
315  ct_timestamp_policy);
316  if (err < 0)
317  return err;
318 
319  if (tb[CTA_TIMESTAMP_START] && tb[CTA_TIMESTAMP_STOP])
320  nfnl_ct_set_timestamp(ct,
321  ntohll(nla_get_u64(tb[CTA_TIMESTAMP_START])),
322  ntohll(nla_get_u64(tb[CTA_TIMESTAMP_STOP])));
323 
324  return 0;
325 }
326 
327 int nfnlmsg_ct_parse(struct nlmsghdr *nlh, struct nfnl_ct **result)
328 {
329  struct nfnl_ct *ct;
330  struct nlattr *tb[CTA_MAX+1];
331  int err;
332 
333  ct = nfnl_ct_alloc();
334  if (!ct)
335  return -NLE_NOMEM;
336 
337  ct->ce_msgtype = nlh->nlmsg_type;
338 
339  err = nlmsg_parse(nlh, sizeof(struct nfgenmsg), tb, CTA_MAX,
340  ct_policy);
341  if (err < 0)
342  goto errout;
343 
344  nfnl_ct_set_family(ct, nfnlmsg_family(nlh));
345 
346  if (tb[CTA_TUPLE_ORIG]) {
347  err = ct_parse_tuple(ct, 0, tb[CTA_TUPLE_ORIG]);
348  if (err < 0)
349  goto errout;
350  }
351  if (tb[CTA_TUPLE_REPLY]) {
352  err = ct_parse_tuple(ct, 1, tb[CTA_TUPLE_REPLY]);
353  if (err < 0)
354  goto errout;
355  }
356 
357  if (tb[CTA_PROTOINFO]) {
358  err = ct_parse_protoinfo(ct, tb[CTA_PROTOINFO]);
359  if (err < 0)
360  goto errout;
361  }
362 
363  if (tb[CTA_STATUS])
364  nfnl_ct_set_status(ct, ntohl(nla_get_u32(tb[CTA_STATUS])));
365  if (tb[CTA_TIMEOUT])
366  nfnl_ct_set_timeout(ct, ntohl(nla_get_u32(tb[CTA_TIMEOUT])));
367  if (tb[CTA_MARK])
368  nfnl_ct_set_mark(ct, ntohl(nla_get_u32(tb[CTA_MARK])));
369  if (tb[CTA_USE])
370  nfnl_ct_set_use(ct, ntohl(nla_get_u32(tb[CTA_USE])));
371  if (tb[CTA_ID])
372  nfnl_ct_set_id(ct, ntohl(nla_get_u32(tb[CTA_ID])));
373  if (tb[CTA_ZONE])
374  nfnl_ct_set_zone(ct, ntohs(nla_get_u16(tb[CTA_ZONE])));
375 
376  if (tb[CTA_COUNTERS_ORIG]) {
377  err = ct_parse_counters(ct, 0, tb[CTA_COUNTERS_ORIG]);
378  if (err < 0)
379  goto errout;
380  }
381 
382  if (tb[CTA_COUNTERS_REPLY]) {
383  err = ct_parse_counters(ct, 1, tb[CTA_COUNTERS_REPLY]);
384  if (err < 0)
385  goto errout;
386  }
387 
388  if (tb[CTA_TIMESTAMP]) {
389  err = ct_parse_timestamp(ct, tb[CTA_TIMESTAMP]);
390  if (err < 0)
391  goto errout;
392  }
393 
394  *result = ct;
395  return 0;
396 
397 errout:
398  nfnl_ct_put(ct);
399  return err;
400 }
401 
402 static int ct_msg_parser(struct nl_cache_ops *ops, struct sockaddr_nl *who,
403  struct nlmsghdr *nlh, struct nl_parser_param *pp)
404 {
405  struct nfnl_ct *ct;
406  int err;
407 
408  if ((err = nfnlmsg_ct_parse(nlh, &ct)) < 0)
409  goto errout;
410 
411  err = pp->pp_cb((struct nl_object *) ct, pp);
412 errout:
413  nfnl_ct_put(ct);
414  return err;
415 }
416 
417 int nfnl_ct_dump_request(struct nl_sock *sk)
418 {
419  return nfnl_send_simple(sk, NFNL_SUBSYS_CTNETLINK, IPCTNL_MSG_CT_GET,
420  NLM_F_DUMP, AF_UNSPEC, 0);
421 }
422 
423 static int ct_request_update(struct nl_cache *cache, struct nl_sock *sk)
424 {
425  return nfnl_ct_dump_request(sk);
426 }
427 
428 static int nfnl_ct_build_tuple(struct nl_msg *msg, const struct nfnl_ct *ct,
429  int repl)
430 {
431  struct nlattr *tuple, *ip, *proto;
432  struct nl_addr *addr;
433  int family;
434 
435  family = nfnl_ct_get_family(ct);
436 
437  tuple = nla_nest_start(msg, repl ? CTA_TUPLE_REPLY : CTA_TUPLE_ORIG);
438  if (!tuple)
439  goto nla_put_failure;
440 
441  ip = nla_nest_start(msg, CTA_TUPLE_IP);
442  if (!ip)
443  goto nla_put_failure;
444 
445  addr = nfnl_ct_get_src(ct, repl);
446  if (addr)
447  NLA_PUT_ADDR(msg,
448  family == AF_INET ? CTA_IP_V4_SRC : CTA_IP_V6_SRC,
449  addr);
450 
451  addr = nfnl_ct_get_dst(ct, repl);
452  if (addr)
453  NLA_PUT_ADDR(msg,
454  family == AF_INET ? CTA_IP_V4_DST : CTA_IP_V6_DST,
455  addr);
456 
457  nla_nest_end(msg, ip);
458 
459  proto = nla_nest_start(msg, CTA_TUPLE_PROTO);
460  if (!proto)
461  goto nla_put_failure;
462 
463  if (nfnl_ct_test_proto(ct))
464  NLA_PUT_U8(msg, CTA_PROTO_NUM, nfnl_ct_get_proto(ct));
465 
466  if (nfnl_ct_test_src_port(ct, repl))
467  NLA_PUT_U16(msg, CTA_PROTO_SRC_PORT,
468  htons(nfnl_ct_get_src_port(ct, repl)));
469 
470  if (nfnl_ct_test_dst_port(ct, repl))
471  NLA_PUT_U16(msg, CTA_PROTO_DST_PORT,
472  htons(nfnl_ct_get_dst_port(ct, repl)));
473 
474  if (family == AF_INET) {
475  if (nfnl_ct_test_icmp_id(ct, repl))
476  NLA_PUT_U16(msg, CTA_PROTO_ICMP_ID,
477  htons(nfnl_ct_get_icmp_id(ct, repl)));
478 
479  if (nfnl_ct_test_icmp_type(ct, repl))
480  NLA_PUT_U8(msg, CTA_PROTO_ICMP_TYPE,
481  nfnl_ct_get_icmp_type(ct, repl));
482 
483  if (nfnl_ct_test_icmp_code(ct, repl))
484  NLA_PUT_U8(msg, CTA_PROTO_ICMP_CODE,
485  nfnl_ct_get_icmp_code(ct, repl));
486  } else if (family == AF_INET6) {
487  if (nfnl_ct_test_icmp_id(ct, repl))
488  NLA_PUT_U16(msg, CTA_PROTO_ICMPV6_ID,
489  htons(nfnl_ct_get_icmp_id(ct, repl)));
490 
491  if (nfnl_ct_test_icmp_type(ct, repl))
492  NLA_PUT_U8(msg, CTA_PROTO_ICMPV6_TYPE,
493  nfnl_ct_get_icmp_type(ct, repl));
494 
495  if (nfnl_ct_test_icmp_code(ct, repl))
496  NLA_PUT_U8(msg, CTA_PROTO_ICMPV6_CODE,
497  nfnl_ct_get_icmp_code(ct, repl));
498  }
499 
500  nla_nest_end(msg, proto);
501 
502  nla_nest_end(msg, tuple);
503  return 0;
504 
505 nla_put_failure:
506  return -NLE_MSGSIZE;
507 }
508 
509 static int nfnl_ct_build_message(const struct nfnl_ct *ct, int cmd, int flags,
510  struct nl_msg **result)
511 {
512  struct nl_msg *msg;
513  int err;
514 
515  msg = nfnlmsg_alloc_simple(NFNL_SUBSYS_CTNETLINK, cmd, flags,
516  nfnl_ct_get_family(ct), 0);
517  if (msg == NULL)
518  return -NLE_NOMEM;
519 
520  if ((err = nfnl_ct_build_tuple(msg, ct, 0)) < 0)
521  goto err_out;
522 
523  *result = msg;
524  return 0;
525 
526 err_out:
527  nlmsg_free(msg);
528  return err;
529 }
530 
531 int nfnl_ct_build_add_request(const struct nfnl_ct *ct, int flags,
532  struct nl_msg **result)
533 {
534  return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_NEW, flags, result);
535 }
536 
537 int nfnl_ct_add(struct nl_sock *sk, const struct nfnl_ct *ct, int flags)
538 {
539  struct nl_msg *msg;
540  int err;
541 
542  if ((err = nfnl_ct_build_add_request(ct, flags, &msg)) < 0)
543  return err;
544 
545  err = nl_send_auto_complete(sk, msg);
546  nlmsg_free(msg);
547  if (err < 0)
548  return err;
549 
550  return wait_for_ack(sk);
551 }
552 
553 int nfnl_ct_build_delete_request(const struct nfnl_ct *ct, int flags,
554  struct nl_msg **result)
555 {
556  return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_DELETE, flags, result);
557 }
558 
559 int nfnl_ct_del(struct nl_sock *sk, const struct nfnl_ct *ct, int flags)
560 {
561  struct nl_msg *msg;
562  int err;
563 
564  if ((err = nfnl_ct_build_delete_request(ct, flags, &msg)) < 0)
565  return err;
566 
567  err = nl_send_auto_complete(sk, msg);
568  nlmsg_free(msg);
569  if (err < 0)
570  return err;
571 
572  return wait_for_ack(sk);
573 }
574 
575 int nfnl_ct_build_query_request(const struct nfnl_ct *ct, int flags,
576  struct nl_msg **result)
577 {
578  return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_GET, flags, result);
579 }
580 
581 int nfnl_ct_query(struct nl_sock *sk, const struct nfnl_ct *ct, int flags)
582 {
583  struct nl_msg *msg;
584  int err;
585 
586  if ((err = nfnl_ct_build_query_request(ct, flags, &msg)) < 0)
587  return err;
588 
589  err = nl_send_auto_complete(sk, msg);
590  nlmsg_free(msg);
591  if (err < 0)
592  return err;
593 
594  return wait_for_ack(sk);
595 }
596 
597 /**
598  * @name Cache Management
599  * @{
600  */
601 
602 /**
603  * Build a conntrack cache holding all conntrack currently in the kernel
604  * @arg sk Netlink socket.
605  * @arg result Pointer to store resulting cache.
606  *
607  * Allocates a new cache, initializes it properly and updates it to
608  * contain all conntracks currently in the kernel.
609  *
610  * @return 0 on success or a negative error code.
611  */
612 int nfnl_ct_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
613 {
614  return nl_cache_alloc_and_fill(&nfnl_ct_ops, sk, result);
615 }
616 
617 /** @} */
618 
619 /**
620  * @name Conntrack Addition
621  * @{
622  */
623 
624 /** @} */
625 
626 static struct nl_af_group ct_groups[] = {
627  { AF_UNSPEC, NFNLGRP_CONNTRACK_NEW },
628  { AF_UNSPEC, NFNLGRP_CONNTRACK_UPDATE },
629  { AF_UNSPEC, NFNLGRP_CONNTRACK_DESTROY },
630  { END_OF_GROUP_LIST },
631 };
632 
633 #define NFNLMSG_CT_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_CTNETLINK, (type))
634 static struct nl_cache_ops nfnl_ct_ops = {
635  .co_name = "netfilter/ct",
636  .co_hdrsize = NFNL_HDRLEN,
637  .co_msgtypes = {
638  { NFNLMSG_CT_TYPE(IPCTNL_MSG_CT_NEW), NL_ACT_NEW, "new" },
639  { NFNLMSG_CT_TYPE(IPCTNL_MSG_CT_GET), NL_ACT_GET, "get" },
640  { NFNLMSG_CT_TYPE(IPCTNL_MSG_CT_DELETE), NL_ACT_DEL, "del" },
641  END_OF_MSGTYPES_LIST,
642  },
643  .co_protocol = NETLINK_NETFILTER,
644  .co_groups = ct_groups,
645  .co_request_update = ct_request_update,
646  .co_msg_parser = ct_msg_parser,
647  .co_obj_ops = &ct_obj_ops,
648 };
649 
650 static void __init ct_init(void)
651 {
652  nl_cache_mngt_register(&nfnl_ct_ops);
653 }
654 
655 static void __exit ct_exit(void)
656 {
657  nl_cache_mngt_unregister(&nfnl_ct_ops);
658 }
659 
660 /** @} */