23 #include <sys/types.h>
24 #include <linux/netfilter/nfnetlink_conntrack.h>
26 #include <netlink-private/netlink.h>
27 #include <netlink/attr.h>
28 #include <netlink/netfilter/nfnl.h>
29 #include <netlink/netfilter/ct.h>
31 static struct nl_cache_ops nfnl_ct_ops;
33 #if __BYTE_ORDER == __BIG_ENDIAN
34 static uint64_t ntohll(uint64_t x)
38 #elif __BYTE_ORDER == __LITTLE_ENDIAN
39 static uint64_t ntohll(uint64_t x)
45 static struct nla_policy ct_policy[CTA_MAX+1] = {
48 [CTA_STATUS] = { .type =
NLA_U32 },
52 [CTA_TIMEOUT] = { .type =
NLA_U32 },
53 [CTA_MARK] = { .type =
NLA_U32 },
56 [CTA_USE] = { .type =
NLA_U32 },
58 [CTA_ZONE] = { .type =
NLA_U16 },
62 static struct nla_policy ct_tuple_policy[CTA_TUPLE_MAX+1] = {
67 static struct nla_policy ct_ip_policy[CTA_IP_MAX+1] = {
69 [CTA_IP_V4_DST] = { .type =
NLA_U32 },
70 [CTA_IP_V6_SRC] = { .minlen = 16 },
71 [CTA_IP_V6_DST] = { .minlen = 16 },
74 static struct nla_policy ct_proto_policy[CTA_PROTO_MAX+1] = {
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 },
86 static struct nla_policy ct_protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
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 },
99 static struct nla_policy ct_counters_policy[CTA_COUNTERS_MAX+1] = {
101 [CTA_COUNTERS_BYTES] = { .type =
NLA_U64 },
102 [CTA_COUNTERS32_PACKETS]= { .type =
NLA_U32 },
103 [CTA_COUNTERS32_BYTES] = { .type =
NLA_U32 },
106 static struct nla_policy ct_timestamp_policy[CTA_TIMESTAMP_MAX + 1] = {
108 [CTA_TIMESTAMP_STOP] = { .type =
NLA_U64 },
111 static int ct_parse_ip(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
113 struct nlattr *tb[CTA_IP_MAX+1];
114 struct nl_addr *addr;
121 if (tb[CTA_IP_V4_SRC]) {
125 err = nfnl_ct_set_src(ct, repl, addr);
130 if (tb[CTA_IP_V4_DST]) {
134 err = nfnl_ct_set_dst(ct, repl, addr);
139 if (tb[CTA_IP_V6_SRC]) {
143 err = nfnl_ct_set_src(ct, repl, addr);
148 if (tb[CTA_IP_V6_DST]) {
152 err = nfnl_ct_set_dst(ct, repl, addr);
166 static int ct_parse_proto(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
168 struct nlattr *tb[CTA_PROTO_MAX+1];
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,
180 if (tb[CTA_PROTO_DST_PORT])
181 nfnl_ct_set_dst_port(ct, repl,
184 if (ct->ct_family == AF_INET) {
185 if (tb[CTA_PROTO_ICMP_ID])
186 nfnl_ct_set_icmp_id(ct, repl,
188 if (tb[CTA_PROTO_ICMP_TYPE])
189 nfnl_ct_set_icmp_type(ct, repl,
191 if (tb[CTA_PROTO_ICMP_CODE])
192 nfnl_ct_set_icmp_code(ct, repl,
194 }
else if (ct->ct_family == AF_INET6) {
195 if (tb[CTA_PROTO_ICMPV6_ID])
196 nfnl_ct_set_icmp_id(ct, repl,
198 if (tb[CTA_PROTO_ICMPV6_TYPE])
199 nfnl_ct_set_icmp_type(ct, repl,
201 if (tb[CTA_PROTO_ICMPV6_CODE])
202 nfnl_ct_set_icmp_code(ct, repl,
209 static int ct_parse_tuple(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
211 struct nlattr *tb[CTA_TUPLE_MAX+1];
218 if (tb[CTA_TUPLE_IP]) {
219 err = ct_parse_ip(ct, repl, tb[CTA_TUPLE_IP]);
224 if (tb[CTA_TUPLE_PROTO]) {
225 err = ct_parse_proto(ct, repl, tb[CTA_TUPLE_PROTO]);
233 static int ct_parse_protoinfo_tcp(
struct nfnl_ct *ct,
struct nlattr *attr)
235 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
239 ct_protoinfo_tcp_policy);
243 if (tb[CTA_PROTOINFO_TCP_STATE])
244 nfnl_ct_set_tcp_state(ct,
250 static int ct_parse_protoinfo(
struct nfnl_ct *ct,
struct nlattr *attr)
252 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
256 ct_protoinfo_policy);
260 if (tb[CTA_PROTOINFO_TCP]) {
261 err = ct_parse_protoinfo_tcp(ct, tb[CTA_PROTOINFO_TCP]);
269 static int ct_parse_counters(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
271 struct nlattr *tb[CTA_COUNTERS_MAX+1];
278 if (tb[CTA_COUNTERS_PACKETS])
279 nfnl_ct_set_packets(ct, repl,
281 if (tb[CTA_COUNTERS32_PACKETS])
282 nfnl_ct_set_packets(ct, repl,
284 if (tb[CTA_COUNTERS_BYTES])
285 nfnl_ct_set_bytes(ct, repl,
287 if (tb[CTA_COUNTERS32_BYTES])
288 nfnl_ct_set_bytes(ct, repl,
294 int nfnlmsg_ct_group(
struct nlmsghdr *nlh)
297 case IPCTNL_MSG_CT_NEW:
298 if (nlh->nlmsg_flags & (NLM_F_CREATE|NLM_F_EXCL))
299 return NFNLGRP_CONNTRACK_NEW;
301 return NFNLGRP_CONNTRACK_UPDATE;
302 case IPCTNL_MSG_CT_DELETE:
303 return NFNLGRP_CONNTRACK_DESTROY;
309 static int ct_parse_timestamp(
struct nfnl_ct *ct,
struct nlattr *attr)
311 struct nlattr *tb[CTA_TIMESTAMP_MAX + 1];
315 ct_timestamp_policy);
319 if (tb[CTA_TIMESTAMP_START] && tb[CTA_TIMESTAMP_STOP])
320 nfnl_ct_set_timestamp(ct,
327 int nfnlmsg_ct_parse(
struct nlmsghdr *nlh,
struct nfnl_ct **result)
330 struct nlattr *tb[CTA_MAX+1];
333 ct = nfnl_ct_alloc();
337 ct->ce_msgtype = nlh->nlmsg_type;
339 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, CTA_MAX,
346 if (tb[CTA_TUPLE_ORIG]) {
347 err = ct_parse_tuple(ct, 0, tb[CTA_TUPLE_ORIG]);
351 if (tb[CTA_TUPLE_REPLY]) {
352 err = ct_parse_tuple(ct, 1, tb[CTA_TUPLE_REPLY]);
357 if (tb[CTA_PROTOINFO]) {
358 err = ct_parse_protoinfo(ct, tb[CTA_PROTOINFO]);
364 nfnl_ct_set_status(ct, ntohl(
nla_get_u32(tb[CTA_STATUS])));
366 nfnl_ct_set_timeout(ct, ntohl(
nla_get_u32(tb[CTA_TIMEOUT])));
368 nfnl_ct_set_mark(ct, ntohl(
nla_get_u32(tb[CTA_MARK])));
370 nfnl_ct_set_use(ct, ntohl(
nla_get_u32(tb[CTA_USE])));
372 nfnl_ct_set_id(ct, ntohl(
nla_get_u32(tb[CTA_ID])));
374 nfnl_ct_set_zone(ct, ntohs(
nla_get_u16(tb[CTA_ZONE])));
376 if (tb[CTA_COUNTERS_ORIG]) {
377 err = ct_parse_counters(ct, 0, tb[CTA_COUNTERS_ORIG]);
382 if (tb[CTA_COUNTERS_REPLY]) {
383 err = ct_parse_counters(ct, 1, tb[CTA_COUNTERS_REPLY]);
388 if (tb[CTA_TIMESTAMP]) {
389 err = ct_parse_timestamp(ct, tb[CTA_TIMESTAMP]);
402 static int ct_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
403 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
408 if ((err = nfnlmsg_ct_parse(nlh, &ct)) < 0)
411 err = pp->pp_cb((
struct nl_object *) ct, pp);
417 int nfnl_ct_dump_request(
struct nl_sock *sk)
420 NLM_F_DUMP, AF_UNSPEC, 0);
423 static int ct_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
425 return nfnl_ct_dump_request(sk);
428 static int nfnl_ct_build_tuple(
struct nl_msg *msg,
const struct nfnl_ct *ct,
431 struct nlattr *tuple, *ip, *proto;
432 struct nl_addr *addr;
435 family = nfnl_ct_get_family(ct);
437 tuple =
nla_nest_start(msg, repl ? CTA_TUPLE_REPLY : CTA_TUPLE_ORIG);
439 goto nla_put_failure;
443 goto nla_put_failure;
445 addr = nfnl_ct_get_src(ct, repl);
448 family == AF_INET ? CTA_IP_V4_SRC : CTA_IP_V6_SRC,
451 addr = nfnl_ct_get_dst(ct, repl);
454 family == AF_INET ? CTA_IP_V4_DST : CTA_IP_V6_DST,
461 goto nla_put_failure;
463 if (nfnl_ct_test_proto(ct))
464 NLA_PUT_U8(msg, CTA_PROTO_NUM, nfnl_ct_get_proto(ct));
466 if (nfnl_ct_test_src_port(ct, repl))
468 htons(nfnl_ct_get_src_port(ct, repl)));
470 if (nfnl_ct_test_dst_port(ct, repl))
472 htons(nfnl_ct_get_dst_port(ct, repl)));
474 if (family == AF_INET) {
475 if (nfnl_ct_test_icmp_id(ct, repl))
477 htons(nfnl_ct_get_icmp_id(ct, repl)));
479 if (nfnl_ct_test_icmp_type(ct, repl))
481 nfnl_ct_get_icmp_type(ct, repl));
483 if (nfnl_ct_test_icmp_code(ct, repl))
485 nfnl_ct_get_icmp_code(ct, repl));
486 }
else if (family == AF_INET6) {
487 if (nfnl_ct_test_icmp_id(ct, repl))
489 htons(nfnl_ct_get_icmp_id(ct, repl)));
491 if (nfnl_ct_test_icmp_type(ct, repl))
493 nfnl_ct_get_icmp_type(ct, repl));
495 if (nfnl_ct_test_icmp_code(ct, repl))
497 nfnl_ct_get_icmp_code(ct, repl));
509 static int nfnl_ct_build_message(
const struct nfnl_ct *ct,
int cmd,
int flags,
510 struct nl_msg **result)
516 nfnl_ct_get_family(ct), 0);
520 if ((err = nfnl_ct_build_tuple(msg, ct, 0)) < 0)
531 int nfnl_ct_build_add_request(
const struct nfnl_ct *ct,
int flags,
532 struct nl_msg **result)
534 return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_NEW, flags, result);
537 int nfnl_ct_add(
struct nl_sock *sk,
const struct nfnl_ct *ct,
int flags)
542 if ((err = nfnl_ct_build_add_request(ct, flags, &msg)) < 0)
550 return wait_for_ack(sk);
553 int nfnl_ct_build_delete_request(
const struct nfnl_ct *ct,
int flags,
554 struct nl_msg **result)
556 return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_DELETE, flags, result);
559 int nfnl_ct_del(
struct nl_sock *sk,
const struct nfnl_ct *ct,
int flags)
564 if ((err = nfnl_ct_build_delete_request(ct, flags, &msg)) < 0)
572 return wait_for_ack(sk);
575 int nfnl_ct_build_query_request(
const struct nfnl_ct *ct,
int flags,
576 struct nl_msg **result)
578 return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_GET, flags, result);
581 int nfnl_ct_query(
struct nl_sock *sk,
const struct nfnl_ct *ct,
int flags)
586 if ((err = nfnl_ct_build_query_request(ct, flags, &msg)) < 0)
594 return wait_for_ack(sk);
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 },
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,
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,
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,
650 static void __init ct_init(
void)
655 static void __exit ct_exit(
void)