14 #include <sys/types.h>
15 #include <linux/netfilter/nfnetlink_conntrack.h>
16 #include <linux/netfilter/nf_conntrack_common.h>
17 #include <linux/netfilter/nf_conntrack_tcp.h>
19 #include <netlink-private/netlink.h>
20 #include <netlink/netfilter/nfnl.h>
21 #include <netlink/netfilter/ct.h>
24 #define CT_ATTR_FAMILY (1UL << 0)
25 #define CT_ATTR_PROTO (1UL << 1)
27 #define CT_ATTR_TCP_STATE (1UL << 2)
29 #define CT_ATTR_STATUS (1UL << 3)
30 #define CT_ATTR_TIMEOUT (1UL << 4)
31 #define CT_ATTR_MARK (1UL << 5)
32 #define CT_ATTR_USE (1UL << 6)
33 #define CT_ATTR_ID (1UL << 7)
35 #define CT_ATTR_ORIG_SRC (1UL << 8)
36 #define CT_ATTR_ORIG_DST (1UL << 9)
37 #define CT_ATTR_ORIG_SRC_PORT (1UL << 10)
38 #define CT_ATTR_ORIG_DST_PORT (1UL << 11)
39 #define CT_ATTR_ORIG_ICMP_ID (1UL << 12)
40 #define CT_ATTR_ORIG_ICMP_TYPE (1UL << 13)
41 #define CT_ATTR_ORIG_ICMP_CODE (1UL << 14)
42 #define CT_ATTR_ORIG_PACKETS (1UL << 15)
43 #define CT_ATTR_ORIG_BYTES (1UL << 16)
45 #define CT_ATTR_REPL_SRC (1UL << 17)
46 #define CT_ATTR_REPL_DST (1UL << 18)
47 #define CT_ATTR_REPL_SRC_PORT (1UL << 19)
48 #define CT_ATTR_REPL_DST_PORT (1UL << 20)
49 #define CT_ATTR_REPL_ICMP_ID (1UL << 21)
50 #define CT_ATTR_REPL_ICMP_TYPE (1UL << 22)
51 #define CT_ATTR_REPL_ICMP_CODE (1UL << 23)
52 #define CT_ATTR_REPL_PACKETS (1UL << 24)
53 #define CT_ATTR_REPL_BYTES (1UL << 25)
54 #define CT_ATTR_TIMESTAMP (1UL << 26)
55 #define CT_ATTR_ZONE (1UL << 27)
58 static void ct_free_data(
struct nl_object *c)
60 struct nfnl_ct *ct = (
struct nfnl_ct *) c;
71 static int ct_clone(
struct nl_object *_dst,
struct nl_object *_src)
73 struct nfnl_ct *dst = (
struct nfnl_ct *) _dst;
74 struct nfnl_ct *src = (
struct nfnl_ct *) _src;
77 if (src->ct_orig.src) {
81 dst->ct_orig.src = addr;
84 if (src->ct_orig.dst) {
88 dst->ct_orig.dst = addr;
91 if (src->ct_repl.src) {
95 dst->ct_repl.src = addr;
98 if (src->ct_repl.dst) {
102 dst->ct_repl.dst = addr;
108 static void dump_addr(
struct nl_dump_params *p,
struct nl_addr *addr,
int port)
121 static void dump_icmp(
struct nl_dump_params *p,
struct nfnl_ct *ct,
int reply)
123 if (nfnl_ct_test_icmp_type(ct, reply))
124 nl_dump(p,
"icmp type %d ", nfnl_ct_get_icmp_type(ct, reply));
126 if (nfnl_ct_test_icmp_code(ct, reply))
127 nl_dump(p,
"code %d ", nfnl_ct_get_icmp_code(ct, reply));
129 if (nfnl_ct_test_icmp_id(ct, reply))
130 nl_dump(p,
"id %d ", nfnl_ct_get_icmp_id(ct, reply));
133 static void ct_dump_tuples(
struct nfnl_ct *ct,
struct nl_dump_params *p)
135 struct nl_addr *orig_src, *orig_dst, *reply_src, *reply_dst;
136 int orig_sport = 0, orig_dport = 0, reply_sport = 0, reply_dport = 0;
139 orig_src = nfnl_ct_get_src(ct, 0);
140 orig_dst = nfnl_ct_get_dst(ct, 0);
141 reply_src = nfnl_ct_get_src(ct, 1);
142 reply_dst = nfnl_ct_get_dst(ct, 1);
144 if (nfnl_ct_test_src_port(ct, 0))
145 orig_sport = nfnl_ct_get_src_port(ct, 0);
147 if (nfnl_ct_test_dst_port(ct, 0))
148 orig_dport = nfnl_ct_get_dst_port(ct, 0);
150 if (nfnl_ct_test_src_port(ct, 1))
151 reply_sport = nfnl_ct_get_src_port(ct, 1);
153 if (nfnl_ct_test_dst_port(ct, 1))
154 reply_dport = nfnl_ct_get_dst_port(ct, 1);
156 if (orig_src && orig_dst && reply_src && reply_dst &&
157 orig_sport == reply_dport && orig_dport == reply_sport &&
162 dump_addr(p, orig_src, orig_sport);
163 nl_dump(p, sync ?
"<-> " :
"-> ");
164 dump_addr(p, orig_dst, orig_dport);
168 dump_addr(p, reply_src, reply_sport);
170 dump_addr(p, reply_dst, reply_dport);
176 static void ct_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
178 struct nfnl_ct *ct = (
struct nfnl_ct *) a;
183 if (nfnl_ct_test_proto(ct))
185 nl_ip_proto2str(nfnl_ct_get_proto(ct), buf,
sizeof(buf)));
187 if (nfnl_ct_test_tcp_state(ct))
189 nfnl_ct_tcp_state2str(nfnl_ct_get_tcp_state(ct),
192 ct_dump_tuples(ct, p);
194 if (nfnl_ct_test_mark(ct) && nfnl_ct_get_mark(ct))
195 nl_dump(p,
"mark %u ", nfnl_ct_get_mark(ct));
197 if (nfnl_ct_test_zone(ct))
198 nl_dump(p,
"zone %hu ", nfnl_ct_get_zone(ct));
200 if (nfnl_ct_test_timestamp(ct)) {
202 int64_t delta_time = tstamp->stop - tstamp->start;
205 delta_time /= NSEC_PER_SEC;
208 nl_dump(p,
"delta-time %llu ", delta_time);
214 static void ct_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
216 struct nfnl_ct *ct = (
struct nfnl_ct *) a;
222 nl_dump(p,
" id 0x%x ", ct->ct_id);
223 nl_dump_line(p,
"family %s ",
224 nl_af2str(ct->ct_family, buf,
sizeof(buf)));
226 if (nfnl_ct_test_use(ct))
227 nl_dump(p,
"refcnt %u ", nfnl_ct_get_use(ct));
229 if (nfnl_ct_test_timeout(ct)) {
230 uint64_t timeout_ms = nfnl_ct_get_timeout(ct) * 1000UL;
238 #define PRINT_FLAG(str) \
239 { nl_dump(p, "%s%s", fp++ ? "," : "", (str)); }
241 if (ct->ct_status & IPS_EXPECTED)
242 PRINT_FLAG(
"EXPECTED");
243 if (!(ct->ct_status & IPS_SEEN_REPLY))
244 PRINT_FLAG(
"NOREPLY");
245 if (ct->ct_status & IPS_ASSURED)
246 PRINT_FLAG(
"ASSURED");
247 if (!(ct->ct_status & IPS_CONFIRMED))
248 PRINT_FLAG(
"NOTSENT");
249 if (ct->ct_status & IPS_SRC_NAT)
251 if (ct->ct_status & IPS_DST_NAT)
253 if (ct->ct_status & IPS_SEQ_ADJUST)
254 PRINT_FLAG(
"SEQADJUST");
255 if (!(ct->ct_status & IPS_SRC_NAT_DONE))
256 PRINT_FLAG(
"SNAT_INIT");
257 if (!(ct->ct_status & IPS_DST_NAT_DONE))
258 PRINT_FLAG(
"DNAT_INIT");
259 if (ct->ct_status & IPS_DYING)
261 if (ct->ct_status & IPS_FIXED_TIMEOUT)
262 PRINT_FLAG(
"FIXED_TIMEOUT");
270 static void ct_dump_stats(
struct nl_object *a,
struct nl_dump_params *p)
272 struct nfnl_ct *ct = (
struct nfnl_ct *) a;
276 const char *
const names[] = {
"rx",
"tx"};
279 ct_dump_details(a, p);
281 if (!nfnl_ct_test_bytes(ct, 0) ||
282 !nfnl_ct_test_packets(ct, 0) ||
283 !nfnl_ct_test_bytes(ct, 1) ||
284 !nfnl_ct_test_packets(ct, 1))
286 nl_dump_line(p,
" Statistics are not available.\n");
287 nl_dump_line(p,
" Please set sysctl net.netfilter.nf_conntrack_acct=1\n");
288 nl_dump_line(p,
" (Require kernel 2.6.27)\n");
292 nl_dump_line(p,
" # packets volume\n");
293 for (i=0; i<=1; i++) {
295 packets = nfnl_ct_get_packets(ct, i);
296 nl_dump_line(p,
" %s %10" PRIu64
" %7.2f %s\n", names[i], packets, res, unit);
300 static int ct_compare(
struct nl_object *_a,
struct nl_object *_b,
301 uint32_t attrs,
int flags)
303 struct nfnl_ct *a = (
struct nfnl_ct *) _a;
304 struct nfnl_ct *b = (
struct nfnl_ct *) _b;
307 #define CT_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, CT_ATTR_##ATTR, a, b, EXPR)
308 #define CT_DIFF_VAL(ATTR, FIELD) CT_DIFF(ATTR, a->FIELD != b->FIELD)
309 #define CT_DIFF_ADDR(ATTR, FIELD) \
310 ((flags & LOOSE_COMPARISON) \
311 ? CT_DIFF(ATTR, nl_addr_cmp_prefix(a->FIELD, b->FIELD)) \
312 : CT_DIFF(ATTR, nl_addr_cmp(a->FIELD, b->FIELD)))
314 diff |= CT_DIFF_VAL(FAMILY, ct_family);
315 diff |= CT_DIFF_VAL(PROTO, ct_proto);
316 diff |= CT_DIFF_VAL(TCP_STATE, ct_protoinfo.tcp.state);
317 diff |= CT_DIFF_VAL(TIMEOUT, ct_timeout);
318 diff |= CT_DIFF_VAL(MARK, ct_mark);
319 diff |= CT_DIFF_VAL(USE, ct_use);
320 diff |= CT_DIFF_VAL(ID, ct_id);
321 diff |= CT_DIFF_ADDR(ORIG_SRC, ct_orig.src);
322 diff |= CT_DIFF_ADDR(ORIG_DST, ct_orig.dst);
323 diff |= CT_DIFF_VAL(ORIG_SRC_PORT, ct_orig.proto.port.src);
324 diff |= CT_DIFF_VAL(ORIG_DST_PORT, ct_orig.proto.port.dst);
325 diff |= CT_DIFF_VAL(ORIG_ICMP_ID, ct_orig.proto.icmp.id);
326 diff |= CT_DIFF_VAL(ORIG_ICMP_TYPE, ct_orig.proto.icmp.type);
327 diff |= CT_DIFF_VAL(ORIG_ICMP_CODE, ct_orig.proto.icmp.code);
328 diff |= CT_DIFF_VAL(ORIG_PACKETS, ct_orig.packets);
329 diff |= CT_DIFF_VAL(ORIG_BYTES, ct_orig.bytes);
330 diff |= CT_DIFF_ADDR(REPL_SRC, ct_repl.src);
331 diff |= CT_DIFF_ADDR(REPL_DST, ct_repl.dst);
332 diff |= CT_DIFF_VAL(REPL_SRC_PORT, ct_repl.proto.port.src);
333 diff |= CT_DIFF_VAL(REPL_DST_PORT, ct_repl.proto.port.dst);
334 diff |= CT_DIFF_VAL(REPL_ICMP_ID, ct_repl.proto.icmp.id);
335 diff |= CT_DIFF_VAL(REPL_ICMP_TYPE, ct_repl.proto.icmp.type);
336 diff |= CT_DIFF_VAL(REPL_ICMP_CODE, ct_repl.proto.icmp.code);
337 diff |= CT_DIFF_VAL(REPL_PACKETS, ct_repl.packets);
338 diff |= CT_DIFF_VAL(REPL_BYTES, ct_repl.bytes);
340 if (flags & LOOSE_COMPARISON)
341 diff |= CT_DIFF(STATUS, (a->ct_status ^ b->ct_status) &
344 diff |= CT_DIFF(STATUS, a->ct_status != b->ct_status);
353 static const struct trans_tbl ct_attrs[] = {
354 __ADD(CT_ATTR_FAMILY, family)
355 __ADD(CT_ATTR_PROTO, proto)
356 __ADD(CT_ATTR_TCP_STATE, tcpstate)
357 __ADD(CT_ATTR_STATUS, status)
358 __ADD(CT_ATTR_TIMEOUT, timeout)
359 __ADD(CT_ATTR_MARK, mark)
360 __ADD(CT_ATTR_USE, use)
361 __ADD(CT_ATTR_ID,
id)
362 __ADD(CT_ATTR_ORIG_SRC, origsrc)
363 __ADD(CT_ATTR_ORIG_DST, origdst)
364 __ADD(CT_ATTR_ORIG_SRC_PORT, origsrcport)
365 __ADD(CT_ATTR_ORIG_DST_PORT, origdstport)
366 __ADD(CT_ATTR_ORIG_ICMP_ID, origicmpid)
367 __ADD(CT_ATTR_ORIG_ICMP_TYPE, origicmptype)
368 __ADD(CT_ATTR_ORIG_ICMP_CODE, origicmpcode)
369 __ADD(CT_ATTR_ORIG_PACKETS, origpackets)
370 __ADD(CT_ATTR_ORIG_BYTES, origbytes)
371 __ADD(CT_ATTR_REPL_SRC, replysrc)
372 __ADD(CT_ATTR_REPL_DST, replydst)
373 __ADD(CT_ATTR_REPL_SRC_PORT, replysrcport)
374 __ADD(CT_ATTR_REPL_DST_PORT, replydstport)
375 __ADD(CT_ATTR_REPL_ICMP_ID, replyicmpid)
376 __ADD(CT_ATTR_REPL_ICMP_TYPE, replyicmptype)
377 __ADD(CT_ATTR_REPL_ICMP_CODE, replyicmpcode)
378 __ADD(CT_ATTR_REPL_PACKETS, replypackets)
379 __ADD(CT_ATTR_REPL_BYTES, replybytes)
382 static
char *ct_attrs2str(
int attrs,
char *buf,
size_t len)
384 return __flags2str(attrs, buf, len, ct_attrs, ARRAY_SIZE(ct_attrs));
392 struct nfnl_ct *nfnl_ct_alloc(
void)
397 void nfnl_ct_get(
struct nfnl_ct *ct)
402 void nfnl_ct_put(
struct nfnl_ct *ct)
414 void nfnl_ct_set_family(
struct nfnl_ct *ct, uint8_t family)
416 ct->ct_family = family;
417 ct->ce_mask |= CT_ATTR_FAMILY;
420 uint8_t nfnl_ct_get_family(
const struct nfnl_ct *ct)
422 if (ct->ce_mask & CT_ATTR_FAMILY)
423 return ct->ct_family;
428 void nfnl_ct_set_proto(
struct nfnl_ct *ct, uint8_t proto)
430 ct->ct_proto = proto;
431 ct->ce_mask |= CT_ATTR_PROTO;
434 int nfnl_ct_test_proto(
const struct nfnl_ct *ct)
436 return !!(ct->ce_mask & CT_ATTR_PROTO);
439 uint8_t nfnl_ct_get_proto(
const struct nfnl_ct *ct)
444 void nfnl_ct_set_tcp_state(
struct nfnl_ct *ct, uint8_t state)
446 ct->ct_protoinfo.tcp.state = state;
447 ct->ce_mask |= CT_ATTR_TCP_STATE;
450 int nfnl_ct_test_tcp_state(
const struct nfnl_ct *ct)
452 return !!(ct->ce_mask & CT_ATTR_TCP_STATE);
455 uint8_t nfnl_ct_get_tcp_state(
const struct nfnl_ct *ct)
457 return ct->ct_protoinfo.tcp.state;
460 static const struct trans_tbl tcp_states[] = {
461 __ADD(TCP_CONNTRACK_NONE,NONE)
462 __ADD(TCP_CONNTRACK_SYN_SENT,SYN_SENT)
463 __ADD(TCP_CONNTRACK_SYN_RECV,SYN_RECV)
464 __ADD(TCP_CONNTRACK_ESTABLISHED,ESTABLISHED)
465 __ADD(TCP_CONNTRACK_FIN_WAIT,FIN_WAIT)
466 __ADD(TCP_CONNTRACK_CLOSE_WAIT,CLOSE_WAIT)
467 __ADD(TCP_CONNTRACK_LAST_ACK,LAST_ACK)
468 __ADD(TCP_CONNTRACK_TIME_WAIT,TIME_WAIT)
469 __ADD(TCP_CONNTRACK_CLOSE,CLOSE)
470 __ADD(TCP_CONNTRACK_LISTEN,LISTEN)
473 char *nfnl_ct_tcp_state2str(uint8_t state,
char *buf,
size_t len)
475 return __type2str(state, buf, len, tcp_states, ARRAY_SIZE(tcp_states));
478 int nfnl_ct_str2tcp_state(
const char *name)
480 return __str2type(name, tcp_states, ARRAY_SIZE(tcp_states));
483 void nfnl_ct_set_status(
struct nfnl_ct *ct, uint32_t status)
485 ct->ct_status_mask |= status;
486 ct->ct_status |= status;
487 ct->ce_mask |= CT_ATTR_STATUS;
490 void nfnl_ct_unset_status(
struct nfnl_ct *ct, uint32_t status)
492 ct->ct_status_mask |= status;
493 ct->ct_status &= ~status;
494 ct->ce_mask |= CT_ATTR_STATUS;
497 uint32_t nfnl_ct_get_status(
const struct nfnl_ct *ct)
499 return ct->ct_status;
502 static const struct trans_tbl status_flags[] = {
503 __ADD(IPS_EXPECTED, expected)
504 __ADD(IPS_SEEN_REPLY, seen_reply)
505 __ADD(IPS_ASSURED, assured)
506 __ADD(IPS_CONFIRMED, confirmed)
507 __ADD(IPS_SRC_NAT, snat)
508 __ADD(IPS_DST_NAT, dnat)
509 __ADD(IPS_SEQ_ADJUST, seqadjust)
510 __ADD(IPS_SRC_NAT_DONE, snat_done)
511 __ADD(IPS_DST_NAT_DONE, dnat_done)
512 __ADD(IPS_DYING, dying)
513 __ADD(IPS_FIXED_TIMEOUT, fixed_timeout)
516 char * nfnl_ct_status2str(
int flags,
char *buf,
size_t len)
518 return __flags2str(flags, buf, len, status_flags,
519 ARRAY_SIZE(status_flags));
522 int nfnl_ct_str2status(
const char *name)
524 return __str2flags(name, status_flags, ARRAY_SIZE(status_flags));
527 void nfnl_ct_set_timeout(
struct nfnl_ct *ct, uint32_t timeout)
529 ct->ct_timeout = timeout;
530 ct->ce_mask |= CT_ATTR_TIMEOUT;
533 int nfnl_ct_test_timeout(
const struct nfnl_ct *ct)
535 return !!(ct->ce_mask & CT_ATTR_TIMEOUT);
538 uint32_t nfnl_ct_get_timeout(
const struct nfnl_ct *ct)
540 return ct->ct_timeout;
543 void nfnl_ct_set_mark(
struct nfnl_ct *ct, uint32_t mark)
546 ct->ce_mask |= CT_ATTR_MARK;
549 int nfnl_ct_test_mark(
const struct nfnl_ct *ct)
551 return !!(ct->ce_mask & CT_ATTR_MARK);
554 uint32_t nfnl_ct_get_mark(
const struct nfnl_ct *ct)
559 void nfnl_ct_set_use(
struct nfnl_ct *ct, uint32_t use)
562 ct->ce_mask |= CT_ATTR_USE;
565 int nfnl_ct_test_use(
const struct nfnl_ct *ct)
567 return !!(ct->ce_mask & CT_ATTR_USE);
570 uint32_t nfnl_ct_get_use(
const struct nfnl_ct *ct)
575 void nfnl_ct_set_id(
struct nfnl_ct *ct, uint32_t
id)
578 ct->ce_mask |= CT_ATTR_ID;
581 int nfnl_ct_test_id(
const struct nfnl_ct *ct)
583 return !!(ct->ce_mask & CT_ATTR_ID);
586 uint32_t nfnl_ct_get_id(
const struct nfnl_ct *ct)
591 void nfnl_ct_set_zone(
struct nfnl_ct *ct, uint16_t zone)
594 ct->ce_mask |= CT_ATTR_ZONE;
597 int nfnl_ct_test_zone(
const struct nfnl_ct *ct)
599 return !!(ct->ce_mask & CT_ATTR_ZONE);
602 uint16_t nfnl_ct_get_zone(
const struct nfnl_ct *ct)
607 static int ct_set_addr(
struct nfnl_ct *ct,
struct nl_addr *addr,
608 int attr,
struct nl_addr ** ct_addr)
610 if (ct->ce_mask & CT_ATTR_FAMILY) {
611 if (addr->a_family != ct->ct_family)
612 return -NLE_AF_MISMATCH;
614 nfnl_ct_set_family(ct, addr->a_family);
626 int nfnl_ct_set_src(
struct nfnl_ct *ct,
int repl,
struct nl_addr *addr)
628 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
629 int attr = repl ? CT_ATTR_REPL_SRC : CT_ATTR_ORIG_SRC;
630 return ct_set_addr(ct, addr, attr, &dir->src);
633 int nfnl_ct_set_dst(
struct nfnl_ct *ct,
int repl,
struct nl_addr *addr)
635 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
636 int attr = repl ? CT_ATTR_REPL_DST : CT_ATTR_ORIG_DST;
637 return ct_set_addr(ct, addr, attr, &dir->dst);
640 struct nl_addr *nfnl_ct_get_src(
const struct nfnl_ct *ct,
int repl)
642 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
643 int attr = repl ? CT_ATTR_REPL_SRC : CT_ATTR_ORIG_SRC;
644 if (!(ct->ce_mask & attr))
649 struct nl_addr *nfnl_ct_get_dst(
const struct nfnl_ct *ct,
int repl)
651 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
652 int attr = repl ? CT_ATTR_REPL_DST : CT_ATTR_ORIG_DST;
653 if (!(ct->ce_mask & attr))
658 void nfnl_ct_set_src_port(
struct nfnl_ct *ct,
int repl, uint16_t port)
660 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
661 int attr = repl ? CT_ATTR_REPL_SRC_PORT : CT_ATTR_ORIG_SRC_PORT;
663 dir->proto.port.src = port;
667 int nfnl_ct_test_src_port(
const struct nfnl_ct *ct,
int repl)
669 int attr = repl ? CT_ATTR_REPL_SRC_PORT : CT_ATTR_ORIG_SRC_PORT;
670 return !!(ct->ce_mask & attr);
673 uint16_t nfnl_ct_get_src_port(
const struct nfnl_ct *ct,
int repl)
675 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
677 return dir->proto.port.src;
680 void nfnl_ct_set_dst_port(
struct nfnl_ct *ct,
int repl, uint16_t port)
682 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
683 int attr = repl ? CT_ATTR_REPL_DST_PORT : CT_ATTR_ORIG_DST_PORT;
685 dir->proto.port.dst = port;
689 int nfnl_ct_test_dst_port(
const struct nfnl_ct *ct,
int repl)
691 int attr = repl ? CT_ATTR_REPL_DST_PORT : CT_ATTR_ORIG_DST_PORT;
692 return !!(ct->ce_mask & attr);
695 uint16_t nfnl_ct_get_dst_port(
const struct nfnl_ct *ct,
int repl)
697 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
699 return dir->proto.port.dst;
702 void nfnl_ct_set_icmp_id(
struct nfnl_ct *ct,
int repl, uint16_t
id)
704 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
705 int attr = repl ? CT_ATTR_REPL_ICMP_ID : CT_ATTR_ORIG_ICMP_ID;
707 dir->proto.icmp.id = id;
711 int nfnl_ct_test_icmp_id(
const struct nfnl_ct *ct,
int repl)
713 int attr = repl ? CT_ATTR_REPL_ICMP_ID : CT_ATTR_ORIG_ICMP_ID;
714 return !!(ct->ce_mask & attr);
717 uint16_t nfnl_ct_get_icmp_id(
const struct nfnl_ct *ct,
int repl)
719 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
721 return dir->proto.icmp.id;
724 void nfnl_ct_set_icmp_type(
struct nfnl_ct *ct,
int repl, uint8_t type)
726 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
727 int attr = repl ? CT_ATTR_REPL_ICMP_TYPE : CT_ATTR_ORIG_ICMP_TYPE;
729 dir->proto.icmp.type = type;
733 int nfnl_ct_test_icmp_type(
const struct nfnl_ct *ct,
int repl)
735 int attr = repl ? CT_ATTR_REPL_ICMP_TYPE : CT_ATTR_ORIG_ICMP_TYPE;
736 return !!(ct->ce_mask & attr);
739 uint8_t nfnl_ct_get_icmp_type(
const struct nfnl_ct *ct,
int repl)
741 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
743 return dir->proto.icmp.type;
746 void nfnl_ct_set_icmp_code(
struct nfnl_ct *ct,
int repl, uint8_t code)
748 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
749 int attr = repl ? CT_ATTR_REPL_ICMP_CODE : CT_ATTR_ORIG_ICMP_CODE;
751 dir->proto.icmp.code = code;
755 int nfnl_ct_test_icmp_code(
const struct nfnl_ct *ct,
int repl)
757 int attr = repl ? CT_ATTR_REPL_ICMP_CODE : CT_ATTR_ORIG_ICMP_CODE;
758 return !!(ct->ce_mask & attr);
761 uint8_t nfnl_ct_get_icmp_code(
const struct nfnl_ct *ct,
int repl)
763 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
765 return dir->proto.icmp.code;
768 void nfnl_ct_set_packets(
struct nfnl_ct *ct,
int repl, uint64_t packets)
770 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
771 int attr = repl ? CT_ATTR_REPL_PACKETS : CT_ATTR_ORIG_PACKETS;
773 dir->packets = packets;
777 int nfnl_ct_test_packets(
const struct nfnl_ct *ct,
int repl)
779 int attr = repl ? CT_ATTR_REPL_PACKETS : CT_ATTR_ORIG_PACKETS;
780 return !!(ct->ce_mask & attr);
783 uint64_t nfnl_ct_get_packets(
const struct nfnl_ct *ct,
int repl)
785 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
790 void nfnl_ct_set_bytes(
struct nfnl_ct *ct,
int repl, uint64_t bytes)
792 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
793 int attr = repl ? CT_ATTR_REPL_BYTES : CT_ATTR_ORIG_BYTES;
799 int nfnl_ct_test_bytes(
const struct nfnl_ct *ct,
int repl)
801 int attr = repl ? CT_ATTR_REPL_BYTES : CT_ATTR_ORIG_BYTES;
802 return !!(ct->ce_mask & attr);
805 uint64_t nfnl_ct_get_bytes(
const struct nfnl_ct *ct,
int repl)
807 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
812 void nfnl_ct_set_timestamp(
struct nfnl_ct *ct, uint64_t start, uint64_t stop)
814 ct->ct_tstamp.start = start;
815 ct->ct_tstamp.stop = stop;
816 ct->ce_mask |= CT_ATTR_TIMESTAMP;
819 int nfnl_ct_test_timestamp(
const struct nfnl_ct *ct)
821 return !!(ct->ce_mask & CT_ATTR_TIMESTAMP);
826 return &ct->ct_tstamp;
831 struct nl_object_ops ct_obj_ops = {
832 .oo_name =
"netfilter/ct",
833 .oo_size =
sizeof(
struct nfnl_ct),
834 .oo_free_data = ct_free_data,
835 .oo_clone = ct_clone,
841 .oo_compare = ct_compare,
842 .oo_attrs2str = ct_attrs2str,