15 #include <sys/types.h> 16 #include <linux/netfilter/nfnetlink_conntrack.h> 17 #include <linux/netfilter/nf_conntrack_common.h> 18 #include <linux/netfilter/nf_conntrack_tcp.h> 20 #include <netlink-private/netlink.h> 21 #include <netlink/netfilter/nfnl.h> 22 #include <netlink/netfilter/ct.h> 25 #define CT_ATTR_FAMILY (1UL << 0) 26 #define CT_ATTR_PROTO (1UL << 1) 28 #define CT_ATTR_TCP_STATE (1UL << 2) 30 #define CT_ATTR_STATUS (1UL << 3) 31 #define CT_ATTR_TIMEOUT (1UL << 4) 32 #define CT_ATTR_MARK (1UL << 5) 33 #define CT_ATTR_USE (1UL << 6) 34 #define CT_ATTR_ID (1UL << 7) 36 #define CT_ATTR_ORIG_SRC (1UL << 8) 37 #define CT_ATTR_ORIG_DST (1UL << 9) 38 #define CT_ATTR_ORIG_SRC_PORT (1UL << 10) 39 #define CT_ATTR_ORIG_DST_PORT (1UL << 11) 40 #define CT_ATTR_ORIG_ICMP_ID (1UL << 12) 41 #define CT_ATTR_ORIG_ICMP_TYPE (1UL << 13) 42 #define CT_ATTR_ORIG_ICMP_CODE (1UL << 14) 43 #define CT_ATTR_ORIG_PACKETS (1UL << 15) 44 #define CT_ATTR_ORIG_BYTES (1UL << 16) 46 #define CT_ATTR_REPL_SRC (1UL << 17) 47 #define CT_ATTR_REPL_DST (1UL << 18) 48 #define CT_ATTR_REPL_SRC_PORT (1UL << 19) 49 #define CT_ATTR_REPL_DST_PORT (1UL << 20) 50 #define CT_ATTR_REPL_ICMP_ID (1UL << 21) 51 #define CT_ATTR_REPL_ICMP_TYPE (1UL << 22) 52 #define CT_ATTR_REPL_ICMP_CODE (1UL << 23) 53 #define CT_ATTR_REPL_PACKETS (1UL << 24) 54 #define CT_ATTR_REPL_BYTES (1UL << 25) 55 #define CT_ATTR_TIMESTAMP (1UL << 26) 56 #define CT_ATTR_ZONE (1UL << 27) 59 static void ct_free_data(
struct nl_object *c)
61 struct nfnl_ct *ct = (
struct nfnl_ct *) c;
72 static int ct_clone(
struct nl_object *_dst,
struct nl_object *_src)
74 struct nfnl_ct *dst = (
struct nfnl_ct *) _dst;
75 struct nfnl_ct *src = (
struct nfnl_ct *) _src;
78 if (src->ct_orig.src) {
82 dst->ct_orig.src = addr;
85 if (src->ct_orig.dst) {
89 dst->ct_orig.dst = addr;
92 if (src->ct_repl.src) {
96 dst->ct_repl.src = addr;
99 if (src->ct_repl.dst) {
103 dst->ct_repl.dst = addr;
109 static void dump_addr(
struct nl_dump_params *p,
struct nl_addr *addr,
int port)
122 static void dump_icmp(
struct nl_dump_params *p,
struct nfnl_ct *ct,
int reply)
124 if (nfnl_ct_test_icmp_type(ct, reply))
125 nl_dump(p,
"icmp type %d ", nfnl_ct_get_icmp_type(ct, reply));
127 if (nfnl_ct_test_icmp_code(ct, reply))
128 nl_dump(p,
"code %d ", nfnl_ct_get_icmp_code(ct, reply));
130 if (nfnl_ct_test_icmp_id(ct, reply))
131 nl_dump(p,
"id %d ", nfnl_ct_get_icmp_id(ct, reply));
134 static void ct_dump_tuples(
struct nfnl_ct *ct,
struct nl_dump_params *p)
136 struct nl_addr *orig_src, *orig_dst, *reply_src, *reply_dst;
137 int orig_sport = 0, orig_dport = 0, reply_sport = 0, reply_dport = 0;
140 orig_src = nfnl_ct_get_src(ct, 0);
141 orig_dst = nfnl_ct_get_dst(ct, 0);
142 reply_src = nfnl_ct_get_src(ct, 1);
143 reply_dst = nfnl_ct_get_dst(ct, 1);
145 if (nfnl_ct_test_src_port(ct, 0))
146 orig_sport = nfnl_ct_get_src_port(ct, 0);
148 if (nfnl_ct_test_dst_port(ct, 0))
149 orig_dport = nfnl_ct_get_dst_port(ct, 0);
151 if (nfnl_ct_test_src_port(ct, 1))
152 reply_sport = nfnl_ct_get_src_port(ct, 1);
154 if (nfnl_ct_test_dst_port(ct, 1))
155 reply_dport = nfnl_ct_get_dst_port(ct, 1);
157 if (orig_src && orig_dst && reply_src && reply_dst &&
158 orig_sport == reply_dport && orig_dport == reply_sport &&
163 dump_addr(p, orig_src, orig_sport);
164 nl_dump(p, sync ?
"<-> " :
"-> ");
165 dump_addr(p, orig_dst, orig_dport);
169 dump_addr(p, reply_src, reply_sport);
171 dump_addr(p, reply_dst, reply_dport);
177 static void ct_dump_line(
struct nl_object *a,
struct nl_dump_params *p)
179 struct nfnl_ct *ct = (
struct nfnl_ct *) a;
184 if (nfnl_ct_test_proto(ct))
186 nl_ip_proto2str(nfnl_ct_get_proto(ct), buf,
sizeof(buf)));
188 if (nfnl_ct_test_tcp_state(ct))
190 nfnl_ct_tcp_state2str(nfnl_ct_get_tcp_state(ct),
193 ct_dump_tuples(ct, p);
195 if (nfnl_ct_test_mark(ct) && nfnl_ct_get_mark(ct))
196 nl_dump(p,
"mark %u ", nfnl_ct_get_mark(ct));
198 if (nfnl_ct_test_zone(ct))
199 nl_dump(p,
"zone %hu ", nfnl_ct_get_zone(ct));
201 if (nfnl_ct_test_timestamp(ct)) {
203 int64_t delta_time = tstamp->stop - tstamp->start;
206 delta_time /= NSEC_PER_SEC;
209 nl_dump(p,
"delta-time %llu ", delta_time);
215 static void ct_dump_details(
struct nl_object *a,
struct nl_dump_params *p)
217 struct nfnl_ct *ct = (
struct nfnl_ct *) a;
223 nl_dump(p,
" id 0x%x ", ct->ct_id);
224 nl_dump_line(p,
"family %s ",
225 nl_af2str(ct->ct_family, buf,
sizeof(buf)));
227 if (nfnl_ct_test_use(ct))
228 nl_dump(p,
"refcnt %u ", nfnl_ct_get_use(ct));
230 if (nfnl_ct_test_timeout(ct)) {
231 uint64_t timeout_ms = nfnl_ct_get_timeout(ct) * 1000UL;
239 #define PRINT_FLAG(str) \ 240 { nl_dump(p, "%s%s", fp++ ? "," : "", (str)); } 242 if (ct->ct_status & IPS_EXPECTED)
243 PRINT_FLAG(
"EXPECTED");
244 if (!(ct->ct_status & IPS_SEEN_REPLY))
245 PRINT_FLAG(
"NOREPLY");
246 if (ct->ct_status & IPS_ASSURED)
247 PRINT_FLAG(
"ASSURED");
248 if (!(ct->ct_status & IPS_CONFIRMED))
249 PRINT_FLAG(
"NOTSENT");
250 if (ct->ct_status & IPS_SRC_NAT)
252 if (ct->ct_status & IPS_DST_NAT)
254 if (ct->ct_status & IPS_SEQ_ADJUST)
255 PRINT_FLAG(
"SEQADJUST");
256 if (!(ct->ct_status & IPS_SRC_NAT_DONE))
257 PRINT_FLAG(
"SNAT_INIT");
258 if (!(ct->ct_status & IPS_DST_NAT_DONE))
259 PRINT_FLAG(
"DNAT_INIT");
260 if (ct->ct_status & IPS_DYING)
262 if (ct->ct_status & IPS_FIXED_TIMEOUT)
263 PRINT_FLAG(
"FIXED_TIMEOUT");
271 static void ct_dump_stats(
struct nl_object *a,
struct nl_dump_params *p)
273 struct nfnl_ct *ct = (
struct nfnl_ct *) a;
277 const char *
const names[] = {
"rx",
"tx"};
280 ct_dump_details(a, p);
282 if (!nfnl_ct_test_bytes(ct, 0) ||
283 !nfnl_ct_test_packets(ct, 0) ||
284 !nfnl_ct_test_bytes(ct, 1) ||
285 !nfnl_ct_test_packets(ct, 1))
287 nl_dump_line(p,
" Statistics are not available.\n");
288 nl_dump_line(p,
" Please set sysctl net.netfilter.nf_conntrack_acct=1\n");
289 nl_dump_line(p,
" (Require kernel 2.6.27)\n");
293 nl_dump_line(p,
" # packets volume\n");
294 for (i=0; i<=1; i++) {
296 packets = nfnl_ct_get_packets(ct, i);
297 nl_dump_line(p,
" %s %10" PRIu64
" %7.2f %s\n", names[i], packets, res, unit);
301 static uint64_t ct_compare(
struct nl_object *_a,
struct nl_object *_b,
302 uint64_t attrs,
int flags)
304 struct nfnl_ct *a = (
struct nfnl_ct *) _a;
305 struct nfnl_ct *b = (
struct nfnl_ct *) _b;
308 #define CT_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, CT_ATTR_##ATTR, a, b, EXPR) 309 #define CT_DIFF_VAL(ATTR, FIELD) CT_DIFF(ATTR, a->FIELD != b->FIELD) 310 #define CT_DIFF_ADDR(ATTR, FIELD) \ 311 ((flags & LOOSE_COMPARISON) \ 312 ? CT_DIFF(ATTR, nl_addr_cmp_prefix(a->FIELD, b->FIELD)) \ 313 : CT_DIFF(ATTR, nl_addr_cmp(a->FIELD, b->FIELD))) 315 diff |= CT_DIFF_VAL(FAMILY, ct_family);
316 diff |= CT_DIFF_VAL(PROTO, ct_proto);
317 diff |= CT_DIFF_VAL(TCP_STATE, ct_protoinfo.tcp.state);
318 diff |= CT_DIFF_VAL(TIMEOUT, ct_timeout);
319 diff |= CT_DIFF_VAL(MARK, ct_mark);
320 diff |= CT_DIFF_VAL(USE, ct_use);
321 diff |= CT_DIFF_VAL(ID, ct_id);
322 diff |= CT_DIFF_ADDR(ORIG_SRC, ct_orig.src);
323 diff |= CT_DIFF_ADDR(ORIG_DST, ct_orig.dst);
324 diff |= CT_DIFF_VAL(ORIG_SRC_PORT, ct_orig.proto.port.src);
325 diff |= CT_DIFF_VAL(ORIG_DST_PORT, ct_orig.proto.port.dst);
326 diff |= CT_DIFF_VAL(ORIG_ICMP_ID, ct_orig.proto.icmp.id);
327 diff |= CT_DIFF_VAL(ORIG_ICMP_TYPE, ct_orig.proto.icmp.type);
328 diff |= CT_DIFF_VAL(ORIG_ICMP_CODE, ct_orig.proto.icmp.code);
329 diff |= CT_DIFF_VAL(ORIG_PACKETS, ct_orig.packets);
330 diff |= CT_DIFF_VAL(ORIG_BYTES, ct_orig.bytes);
331 diff |= CT_DIFF_ADDR(REPL_SRC, ct_repl.src);
332 diff |= CT_DIFF_ADDR(REPL_DST, ct_repl.dst);
333 diff |= CT_DIFF_VAL(REPL_SRC_PORT, ct_repl.proto.port.src);
334 diff |= CT_DIFF_VAL(REPL_DST_PORT, ct_repl.proto.port.dst);
335 diff |= CT_DIFF_VAL(REPL_ICMP_ID, ct_repl.proto.icmp.id);
336 diff |= CT_DIFF_VAL(REPL_ICMP_TYPE, ct_repl.proto.icmp.type);
337 diff |= CT_DIFF_VAL(REPL_ICMP_CODE, ct_repl.proto.icmp.code);
338 diff |= CT_DIFF_VAL(REPL_PACKETS, ct_repl.packets);
339 diff |= CT_DIFF_VAL(REPL_BYTES, ct_repl.bytes);
341 if (flags & LOOSE_COMPARISON)
342 diff |= CT_DIFF(STATUS, (a->ct_status ^ b->ct_status) &
345 diff |= CT_DIFF(STATUS, a->ct_status != b->ct_status);
354 static const struct trans_tbl ct_attrs[] = {
355 __ADD(CT_ATTR_FAMILY, family),
356 __ADD(CT_ATTR_PROTO, proto),
357 __ADD(CT_ATTR_TCP_STATE, tcpstate),
358 __ADD(CT_ATTR_STATUS, status),
359 __ADD(CT_ATTR_TIMEOUT, timeout),
360 __ADD(CT_ATTR_MARK, mark),
361 __ADD(CT_ATTR_USE, use),
362 __ADD(CT_ATTR_ID,
id),
363 __ADD(CT_ATTR_ORIG_SRC, origsrc),
364 __ADD(CT_ATTR_ORIG_DST, origdst),
365 __ADD(CT_ATTR_ORIG_SRC_PORT, origsrcport),
366 __ADD(CT_ATTR_ORIG_DST_PORT, origdstport),
367 __ADD(CT_ATTR_ORIG_ICMP_ID, origicmpid),
368 __ADD(CT_ATTR_ORIG_ICMP_TYPE, origicmptype),
369 __ADD(CT_ATTR_ORIG_ICMP_CODE, origicmpcode),
370 __ADD(CT_ATTR_ORIG_PACKETS, origpackets),
371 __ADD(CT_ATTR_ORIG_BYTES, origbytes),
372 __ADD(CT_ATTR_REPL_SRC, replysrc),
373 __ADD(CT_ATTR_REPL_DST, replydst),
374 __ADD(CT_ATTR_REPL_SRC_PORT, replysrcport),
375 __ADD(CT_ATTR_REPL_DST_PORT, replydstport),
376 __ADD(CT_ATTR_REPL_ICMP_ID, replyicmpid),
377 __ADD(CT_ATTR_REPL_ICMP_TYPE, replyicmptype),
378 __ADD(CT_ATTR_REPL_ICMP_CODE, replyicmpcode),
379 __ADD(CT_ATTR_REPL_PACKETS, replypackets),
380 __ADD(CT_ATTR_REPL_BYTES, replybytes),
383 static char *ct_attrs2str(
int attrs,
char *buf,
size_t len)
385 return __flags2str(attrs, buf, len, ct_attrs, ARRAY_SIZE(ct_attrs));
393 struct nfnl_ct *nfnl_ct_alloc(
void)
398 void nfnl_ct_get(
struct nfnl_ct *ct)
403 void nfnl_ct_put(
struct nfnl_ct *ct)
415 void nfnl_ct_set_family(
struct nfnl_ct *ct, uint8_t family)
417 ct->ct_family = family;
418 ct->ce_mask |= CT_ATTR_FAMILY;
421 uint8_t nfnl_ct_get_family(
const struct nfnl_ct *ct)
423 if (ct->ce_mask & CT_ATTR_FAMILY)
424 return ct->ct_family;
429 void nfnl_ct_set_proto(
struct nfnl_ct *ct, uint8_t proto)
431 ct->ct_proto = proto;
432 ct->ce_mask |= CT_ATTR_PROTO;
435 int nfnl_ct_test_proto(
const struct nfnl_ct *ct)
437 return !!(ct->ce_mask & CT_ATTR_PROTO);
440 uint8_t nfnl_ct_get_proto(
const struct nfnl_ct *ct)
445 void nfnl_ct_set_tcp_state(
struct nfnl_ct *ct, uint8_t state)
447 ct->ct_protoinfo.tcp.state = state;
448 ct->ce_mask |= CT_ATTR_TCP_STATE;
451 int nfnl_ct_test_tcp_state(
const struct nfnl_ct *ct)
453 return !!(ct->ce_mask & CT_ATTR_TCP_STATE);
456 uint8_t nfnl_ct_get_tcp_state(
const struct nfnl_ct *ct)
458 return ct->ct_protoinfo.tcp.state;
461 static const struct trans_tbl tcp_states[] = {
462 __ADD(TCP_CONNTRACK_NONE,NONE),
463 __ADD(TCP_CONNTRACK_SYN_SENT,SYN_SENT),
464 __ADD(TCP_CONNTRACK_SYN_RECV,SYN_RECV),
465 __ADD(TCP_CONNTRACK_ESTABLISHED,ESTABLISHED),
466 __ADD(TCP_CONNTRACK_FIN_WAIT,FIN_WAIT),
467 __ADD(TCP_CONNTRACK_CLOSE_WAIT,CLOSE_WAIT),
468 __ADD(TCP_CONNTRACK_LAST_ACK,LAST_ACK),
469 __ADD(TCP_CONNTRACK_TIME_WAIT,TIME_WAIT),
470 __ADD(TCP_CONNTRACK_CLOSE,CLOSE),
471 __ADD(TCP_CONNTRACK_LISTEN,LISTEN),
474 char *nfnl_ct_tcp_state2str(uint8_t state,
char *buf,
size_t len)
476 return __type2str(state, buf, len, tcp_states, ARRAY_SIZE(tcp_states));
479 int nfnl_ct_str2tcp_state(
const char *name)
481 return __str2type(name, tcp_states, ARRAY_SIZE(tcp_states));
484 void nfnl_ct_set_status(
struct nfnl_ct *ct, uint32_t status)
486 ct->ct_status_mask |= status;
487 ct->ct_status |= status;
488 ct->ce_mask |= CT_ATTR_STATUS;
491 void nfnl_ct_unset_status(
struct nfnl_ct *ct, uint32_t status)
493 ct->ct_status_mask |= status;
494 ct->ct_status &= ~status;
495 ct->ce_mask |= CT_ATTR_STATUS;
498 int nfnl_ct_test_status(
const struct nfnl_ct *ct)
500 return !!(ct->ce_mask & CT_ATTR_STATUS);
503 uint32_t nfnl_ct_get_status(
const struct nfnl_ct *ct)
505 return ct->ct_status;
508 static const struct trans_tbl status_flags[] = {
509 __ADD(IPS_EXPECTED, expected),
510 __ADD(IPS_SEEN_REPLY, seen_reply),
511 __ADD(IPS_ASSURED, assured),
512 __ADD(IPS_CONFIRMED, confirmed),
513 __ADD(IPS_SRC_NAT, snat),
514 __ADD(IPS_DST_NAT, dnat),
515 __ADD(IPS_SEQ_ADJUST, seqadjust),
516 __ADD(IPS_SRC_NAT_DONE, snat_done),
517 __ADD(IPS_DST_NAT_DONE, dnat_done),
518 __ADD(IPS_DYING, dying),
519 __ADD(IPS_FIXED_TIMEOUT, fixed_timeout),
522 char * nfnl_ct_status2str(
int flags,
char *buf,
size_t len)
524 return __flags2str(flags, buf, len, status_flags,
525 ARRAY_SIZE(status_flags));
528 int nfnl_ct_str2status(
const char *name)
530 return __str2flags(name, status_flags, ARRAY_SIZE(status_flags));
533 void nfnl_ct_set_timeout(
struct nfnl_ct *ct, uint32_t timeout)
535 ct->ct_timeout = timeout;
536 ct->ce_mask |= CT_ATTR_TIMEOUT;
539 int nfnl_ct_test_timeout(
const struct nfnl_ct *ct)
541 return !!(ct->ce_mask & CT_ATTR_TIMEOUT);
544 uint32_t nfnl_ct_get_timeout(
const struct nfnl_ct *ct)
546 return ct->ct_timeout;
549 void nfnl_ct_set_mark(
struct nfnl_ct *ct, uint32_t mark)
552 ct->ce_mask |= CT_ATTR_MARK;
555 int nfnl_ct_test_mark(
const struct nfnl_ct *ct)
557 return !!(ct->ce_mask & CT_ATTR_MARK);
560 uint32_t nfnl_ct_get_mark(
const struct nfnl_ct *ct)
565 void nfnl_ct_set_use(
struct nfnl_ct *ct, uint32_t use)
568 ct->ce_mask |= CT_ATTR_USE;
571 int nfnl_ct_test_use(
const struct nfnl_ct *ct)
573 return !!(ct->ce_mask & CT_ATTR_USE);
576 uint32_t nfnl_ct_get_use(
const struct nfnl_ct *ct)
581 void nfnl_ct_set_id(
struct nfnl_ct *ct, uint32_t
id)
584 ct->ce_mask |= CT_ATTR_ID;
587 int nfnl_ct_test_id(
const struct nfnl_ct *ct)
589 return !!(ct->ce_mask & CT_ATTR_ID);
592 uint32_t nfnl_ct_get_id(
const struct nfnl_ct *ct)
597 void nfnl_ct_set_zone(
struct nfnl_ct *ct, uint16_t zone)
600 ct->ce_mask |= CT_ATTR_ZONE;
603 int nfnl_ct_test_zone(
const struct nfnl_ct *ct)
605 return !!(ct->ce_mask & CT_ATTR_ZONE);
608 uint16_t nfnl_ct_get_zone(
const struct nfnl_ct *ct)
613 static int ct_set_addr(
struct nfnl_ct *ct,
struct nl_addr *addr,
614 int attr,
struct nl_addr ** ct_addr)
616 if (ct->ce_mask & CT_ATTR_FAMILY) {
617 if (addr->a_family != ct->ct_family)
618 return -NLE_AF_MISMATCH;
620 nfnl_ct_set_family(ct, addr->a_family);
632 int nfnl_ct_set_src(
struct nfnl_ct *ct,
int repl,
struct nl_addr *addr)
634 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
635 int attr = repl ? CT_ATTR_REPL_SRC : CT_ATTR_ORIG_SRC;
636 return ct_set_addr(ct, addr, attr, &dir->src);
639 int nfnl_ct_set_dst(
struct nfnl_ct *ct,
int repl,
struct nl_addr *addr)
641 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
642 int attr = repl ? CT_ATTR_REPL_DST : CT_ATTR_ORIG_DST;
643 return ct_set_addr(ct, addr, attr, &dir->dst);
646 struct nl_addr *nfnl_ct_get_src(
const struct nfnl_ct *ct,
int repl)
648 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
649 int attr = repl ? CT_ATTR_REPL_SRC : CT_ATTR_ORIG_SRC;
650 if (!(ct->ce_mask & attr))
655 struct nl_addr *nfnl_ct_get_dst(
const struct nfnl_ct *ct,
int repl)
657 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
658 int attr = repl ? CT_ATTR_REPL_DST : CT_ATTR_ORIG_DST;
659 if (!(ct->ce_mask & attr))
664 void nfnl_ct_set_src_port(
struct nfnl_ct *ct,
int repl, uint16_t port)
666 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
667 int attr = repl ? CT_ATTR_REPL_SRC_PORT : CT_ATTR_ORIG_SRC_PORT;
669 dir->proto.port.src = port;
673 int nfnl_ct_test_src_port(
const struct nfnl_ct *ct,
int repl)
675 int attr = repl ? CT_ATTR_REPL_SRC_PORT : CT_ATTR_ORIG_SRC_PORT;
676 return !!(ct->ce_mask & attr);
679 uint16_t nfnl_ct_get_src_port(
const struct nfnl_ct *ct,
int repl)
681 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
683 return dir->proto.port.src;
686 void nfnl_ct_set_dst_port(
struct nfnl_ct *ct,
int repl, uint16_t port)
688 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
689 int attr = repl ? CT_ATTR_REPL_DST_PORT : CT_ATTR_ORIG_DST_PORT;
691 dir->proto.port.dst = port;
695 int nfnl_ct_test_dst_port(
const struct nfnl_ct *ct,
int repl)
697 int attr = repl ? CT_ATTR_REPL_DST_PORT : CT_ATTR_ORIG_DST_PORT;
698 return !!(ct->ce_mask & attr);
701 uint16_t nfnl_ct_get_dst_port(
const struct nfnl_ct *ct,
int repl)
703 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
705 return dir->proto.port.dst;
708 void nfnl_ct_set_icmp_id(
struct nfnl_ct *ct,
int repl, uint16_t
id)
710 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
711 int attr = repl ? CT_ATTR_REPL_ICMP_ID : CT_ATTR_ORIG_ICMP_ID;
713 dir->proto.icmp.id = id;
717 int nfnl_ct_test_icmp_id(
const struct nfnl_ct *ct,
int repl)
719 int attr = repl ? CT_ATTR_REPL_ICMP_ID : CT_ATTR_ORIG_ICMP_ID;
720 return !!(ct->ce_mask & attr);
723 uint16_t nfnl_ct_get_icmp_id(
const struct nfnl_ct *ct,
int repl)
725 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
727 return dir->proto.icmp.id;
730 void nfnl_ct_set_icmp_type(
struct nfnl_ct *ct,
int repl, uint8_t type)
732 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
733 int attr = repl ? CT_ATTR_REPL_ICMP_TYPE : CT_ATTR_ORIG_ICMP_TYPE;
735 dir->proto.icmp.type = type;
739 int nfnl_ct_test_icmp_type(
const struct nfnl_ct *ct,
int repl)
741 int attr = repl ? CT_ATTR_REPL_ICMP_TYPE : CT_ATTR_ORIG_ICMP_TYPE;
742 return !!(ct->ce_mask & attr);
745 uint8_t nfnl_ct_get_icmp_type(
const struct nfnl_ct *ct,
int repl)
747 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
749 return dir->proto.icmp.type;
752 void nfnl_ct_set_icmp_code(
struct nfnl_ct *ct,
int repl, uint8_t code)
754 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
755 int attr = repl ? CT_ATTR_REPL_ICMP_CODE : CT_ATTR_ORIG_ICMP_CODE;
757 dir->proto.icmp.code = code;
761 int nfnl_ct_test_icmp_code(
const struct nfnl_ct *ct,
int repl)
763 int attr = repl ? CT_ATTR_REPL_ICMP_CODE : CT_ATTR_ORIG_ICMP_CODE;
764 return !!(ct->ce_mask & attr);
767 uint8_t nfnl_ct_get_icmp_code(
const struct nfnl_ct *ct,
int repl)
769 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
771 return dir->proto.icmp.code;
774 void nfnl_ct_set_packets(
struct nfnl_ct *ct,
int repl, uint64_t packets)
776 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
777 int attr = repl ? CT_ATTR_REPL_PACKETS : CT_ATTR_ORIG_PACKETS;
779 dir->packets = packets;
783 int nfnl_ct_test_packets(
const struct nfnl_ct *ct,
int repl)
785 int attr = repl ? CT_ATTR_REPL_PACKETS : CT_ATTR_ORIG_PACKETS;
786 return !!(ct->ce_mask & attr);
789 uint64_t nfnl_ct_get_packets(
const struct nfnl_ct *ct,
int repl)
791 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
796 void nfnl_ct_set_bytes(
struct nfnl_ct *ct,
int repl, uint64_t bytes)
798 struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
799 int attr = repl ? CT_ATTR_REPL_BYTES : CT_ATTR_ORIG_BYTES;
805 int nfnl_ct_test_bytes(
const struct nfnl_ct *ct,
int repl)
807 int attr = repl ? CT_ATTR_REPL_BYTES : CT_ATTR_ORIG_BYTES;
808 return !!(ct->ce_mask & attr);
811 uint64_t nfnl_ct_get_bytes(
const struct nfnl_ct *ct,
int repl)
813 const struct nfnl_ct_dir *dir = repl ? &ct->ct_repl : &ct->ct_orig;
818 void nfnl_ct_set_timestamp(
struct nfnl_ct *ct, uint64_t start, uint64_t stop)
820 ct->ct_tstamp.start = start;
821 ct->ct_tstamp.stop = stop;
822 ct->ce_mask |= CT_ATTR_TIMESTAMP;
825 int nfnl_ct_test_timestamp(
const struct nfnl_ct *ct)
827 return !!(ct->ce_mask & CT_ATTR_TIMESTAMP);
832 return &ct->ct_tstamp;
837 struct nl_object_ops ct_obj_ops = {
838 .oo_name =
"netfilter/ct",
839 .oo_size =
sizeof(
struct nfnl_ct),
840 .oo_free_data = ct_free_data,
841 .oo_clone = ct_clone,
847 .oo_compare = ct_compare,
848 .oo_attrs2str = ct_attrs2str,
struct nl_addr * nl_addr_clone(const struct nl_addr *addr)
Clone existing abstract address object.
Dump object briefly on one line.
void nl_new_line(struct nl_dump_params *params)
Handle a new line while dumping.
int nl_addr_cmp(const struct nl_addr *a, const struct nl_addr *b)
Compare abstract addresses.
struct nl_object * nl_object_alloc(struct nl_object_ops *ops)
Allocate a new object of kind specified by the operations handle.
void nl_object_get(struct nl_object *obj)
Acquire a reference on a object.
char * nl_msec2str(uint64_t msec, char *buf, size_t len)
Convert milliseconds to a character string.
struct nl_addr * nl_addr_get(struct nl_addr *addr)
Increase the reference counter of an abstract address.
Dump all attributes but no statistics.
double nl_cancel_down_bytes(unsigned long long l, char **unit)
Cancel down a byte counter.
void nl_object_put(struct nl_object *obj)
Release a reference from an object.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
void nl_dump(struct nl_dump_params *params, const char *fmt,...)
Dump a formatted character string.
Dump all attributes including statistics.
char * nl_addr2str(const struct nl_addr *addr, char *buf, size_t size)
Convert abstract address object to character string.