17 #include <sys/types.h>
18 #include <linux/netfilter/nfnetlink_conntrack.h>
20 #include <netlink-private/netlink.h>
21 #include <netlink/attr.h>
22 #include <netlink/netfilter/nfnl.h>
23 #include <netlink/netfilter/ct.h>
24 #include <netlink-private/utils.h>
26 static struct nl_cache_ops nfnl_ct_ops;
29 static struct nla_policy ct_policy[CTA_MAX+1] = {
32 [CTA_STATUS] = { .type =
NLA_U32 },
36 [CTA_TIMEOUT] = { .type =
NLA_U32 },
37 [CTA_MARK] = { .type =
NLA_U32 },
40 [CTA_USE] = { .type =
NLA_U32 },
42 [CTA_ZONE] = { .type =
NLA_U16 },
46 static struct nla_policy ct_tuple_policy[CTA_TUPLE_MAX+1] = {
51 static struct nla_policy ct_ip_policy[CTA_IP_MAX+1] = {
53 [CTA_IP_V4_DST] = { .type =
NLA_U32 },
54 [CTA_IP_V6_SRC] = { .minlen = 16 },
55 [CTA_IP_V6_DST] = { .minlen = 16 },
58 static struct nla_policy ct_proto_policy[CTA_PROTO_MAX+1] = {
60 [CTA_PROTO_SRC_PORT] = { .type =
NLA_U16 },
61 [CTA_PROTO_DST_PORT] = { .type =
NLA_U16 },
62 [CTA_PROTO_ICMP_ID] = { .type =
NLA_U16 },
63 [CTA_PROTO_ICMP_TYPE] = { .type =
NLA_U8 },
64 [CTA_PROTO_ICMP_CODE] = { .type =
NLA_U8 },
65 [CTA_PROTO_ICMPV6_ID] = { .type =
NLA_U16 },
66 [CTA_PROTO_ICMPV6_TYPE] = { .type =
NLA_U8 },
67 [CTA_PROTO_ICMPV6_CODE] = { .type =
NLA_U8 },
70 static struct nla_policy ct_protoinfo_policy[CTA_PROTOINFO_MAX+1] = {
74 static struct nla_policy ct_protoinfo_tcp_policy[CTA_PROTOINFO_TCP_MAX+1] = {
75 [CTA_PROTOINFO_TCP_STATE] = { .
type =
NLA_U8 },
76 [CTA_PROTOINFO_TCP_WSCALE_ORIGINAL] = { .type =
NLA_U8 },
77 [CTA_PROTOINFO_TCP_WSCALE_REPLY] = { .type =
NLA_U8 },
78 [CTA_PROTOINFO_TCP_FLAGS_ORIGINAL] = { .minlen = 2 },
79 [CTA_PROTOINFO_TCP_FLAGS_REPLY] = { .minlen = 2 },
83 static struct nla_policy ct_counters_policy[CTA_COUNTERS_MAX+1] = {
85 [CTA_COUNTERS_BYTES] = { .type =
NLA_U64 },
86 [CTA_COUNTERS32_PACKETS]= { .type =
NLA_U32 },
87 [CTA_COUNTERS32_BYTES] = { .type =
NLA_U32 },
90 static struct nla_policy ct_timestamp_policy[CTA_TIMESTAMP_MAX + 1] = {
92 [CTA_TIMESTAMP_STOP] = { .type =
NLA_U64 },
95 static int ct_parse_ip(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
97 struct nlattr *tb[CTA_IP_MAX+1];
105 if (tb[CTA_IP_V4_SRC]) {
109 err = nfnl_ct_set_src(ct, repl, addr);
114 if (tb[CTA_IP_V4_DST]) {
118 err = nfnl_ct_set_dst(ct, repl, addr);
123 if (tb[CTA_IP_V6_SRC]) {
127 err = nfnl_ct_set_src(ct, repl, addr);
132 if (tb[CTA_IP_V6_DST]) {
136 err = nfnl_ct_set_dst(ct, repl, addr);
150 static int ct_parse_proto(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
152 struct nlattr *tb[CTA_PROTO_MAX+1];
159 if (!repl && tb[CTA_PROTO_NUM])
160 nfnl_ct_set_proto(ct,
nla_get_u8(tb[CTA_PROTO_NUM]));
161 if (tb[CTA_PROTO_SRC_PORT])
162 nfnl_ct_set_src_port(ct, repl,
164 if (tb[CTA_PROTO_DST_PORT])
165 nfnl_ct_set_dst_port(ct, repl,
168 if (ct->ct_family == AF_INET) {
169 if (tb[CTA_PROTO_ICMP_ID])
170 nfnl_ct_set_icmp_id(ct, repl,
172 if (tb[CTA_PROTO_ICMP_TYPE])
173 nfnl_ct_set_icmp_type(ct, repl,
175 if (tb[CTA_PROTO_ICMP_CODE])
176 nfnl_ct_set_icmp_code(ct, repl,
178 }
else if (ct->ct_family == AF_INET6) {
179 if (tb[CTA_PROTO_ICMPV6_ID])
180 nfnl_ct_set_icmp_id(ct, repl,
182 if (tb[CTA_PROTO_ICMPV6_TYPE])
183 nfnl_ct_set_icmp_type(ct, repl,
185 if (tb[CTA_PROTO_ICMPV6_CODE])
186 nfnl_ct_set_icmp_code(ct, repl,
193 static int ct_parse_tuple(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
195 struct nlattr *tb[CTA_TUPLE_MAX+1];
202 if (tb[CTA_TUPLE_IP]) {
203 err = ct_parse_ip(ct, repl, tb[CTA_TUPLE_IP]);
208 if (tb[CTA_TUPLE_PROTO]) {
209 err = ct_parse_proto(ct, repl, tb[CTA_TUPLE_PROTO]);
217 static int ct_parse_protoinfo_tcp(
struct nfnl_ct *ct,
struct nlattr *attr)
219 struct nlattr *tb[CTA_PROTOINFO_TCP_MAX+1];
223 ct_protoinfo_tcp_policy);
227 if (tb[CTA_PROTOINFO_TCP_STATE])
228 nfnl_ct_set_tcp_state(ct,
234 static int ct_parse_protoinfo(
struct nfnl_ct *ct,
struct nlattr *attr)
236 struct nlattr *tb[CTA_PROTOINFO_MAX+1];
240 ct_protoinfo_policy);
244 if (tb[CTA_PROTOINFO_TCP]) {
245 err = ct_parse_protoinfo_tcp(ct, tb[CTA_PROTOINFO_TCP]);
253 static int ct_parse_counters(
struct nfnl_ct *ct,
int repl,
struct nlattr *attr)
255 struct nlattr *tb[CTA_COUNTERS_MAX+1];
262 if (tb[CTA_COUNTERS_PACKETS])
263 nfnl_ct_set_packets(ct, repl,
265 if (tb[CTA_COUNTERS32_PACKETS])
266 nfnl_ct_set_packets(ct, repl,
268 if (tb[CTA_COUNTERS_BYTES])
269 nfnl_ct_set_bytes(ct, repl,
271 if (tb[CTA_COUNTERS32_BYTES])
272 nfnl_ct_set_bytes(ct, repl,
278 int nfnlmsg_ct_group(
struct nlmsghdr *nlh)
281 case IPCTNL_MSG_CT_NEW:
282 if (nlh->nlmsg_flags & (NLM_F_CREATE|NLM_F_EXCL))
283 return NFNLGRP_CONNTRACK_NEW;
285 return NFNLGRP_CONNTRACK_UPDATE;
286 case IPCTNL_MSG_CT_DELETE:
287 return NFNLGRP_CONNTRACK_DESTROY;
293 static int ct_parse_timestamp(
struct nfnl_ct *ct,
struct nlattr *attr)
295 struct nlattr *tb[CTA_TIMESTAMP_MAX + 1];
299 ct_timestamp_policy);
303 if (tb[CTA_TIMESTAMP_START] && tb[CTA_TIMESTAMP_STOP])
304 nfnl_ct_set_timestamp(ct,
311 static int _nfnlmsg_ct_parse(
struct nlattr **tb,
struct nfnl_ct *ct)
315 if (tb[CTA_TUPLE_ORIG]) {
316 err = ct_parse_tuple(ct, 0, tb[CTA_TUPLE_ORIG]);
320 if (tb[CTA_TUPLE_REPLY]) {
321 err = ct_parse_tuple(ct, 1, tb[CTA_TUPLE_REPLY]);
326 if (tb[CTA_PROTOINFO]) {
327 err = ct_parse_protoinfo(ct, tb[CTA_PROTOINFO]);
333 nfnl_ct_set_status(ct, ntohl(
nla_get_u32(tb[CTA_STATUS])));
335 nfnl_ct_set_timeout(ct, ntohl(
nla_get_u32(tb[CTA_TIMEOUT])));
337 nfnl_ct_set_mark(ct, ntohl(
nla_get_u32(tb[CTA_MARK])));
339 nfnl_ct_set_use(ct, ntohl(
nla_get_u32(tb[CTA_USE])));
341 nfnl_ct_set_id(ct, ntohl(
nla_get_u32(tb[CTA_ID])));
343 nfnl_ct_set_zone(ct, ntohs(
nla_get_u16(tb[CTA_ZONE])));
345 if (tb[CTA_COUNTERS_ORIG]) {
346 err = ct_parse_counters(ct, 0, tb[CTA_COUNTERS_ORIG]);
351 if (tb[CTA_COUNTERS_REPLY]) {
352 err = ct_parse_counters(ct, 1, tb[CTA_COUNTERS_REPLY]);
357 if (tb[CTA_TIMESTAMP]) {
358 err = ct_parse_timestamp(ct, tb[CTA_TIMESTAMP]);
366 int nfnlmsg_ct_parse(
struct nlmsghdr *nlh,
struct nfnl_ct **result)
369 struct nlattr *tb[CTA_MAX+1];
372 ct = nfnl_ct_alloc();
376 ct->ce_msgtype = nlh->nlmsg_type;
378 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, CTA_MAX,
385 err = _nfnlmsg_ct_parse(tb, ct);
396 int nfnlmsg_ct_parse_nested(
struct nlattr *attr,
struct nfnl_ct **result)
399 struct nlattr *tb[CTA_MAX+1];
402 ct = nfnl_ct_alloc();
416 err = _nfnlmsg_ct_parse(tb, ct);
427 static int ct_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
428 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
433 if ((err = nfnlmsg_ct_parse(nlh, &ct)) < 0)
436 err = pp->pp_cb((
struct nl_object *) ct, pp);
451 NLM_F_DUMP, AF_UNSPEC, 0);
454 static int ct_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
459 static int nfnl_ct_build_tuple(
struct nl_msg *msg,
const struct nfnl_ct *ct,
462 struct nlattr *tuple, *ip, *proto;
463 struct nl_addr *addr;
466 family = nfnl_ct_get_family(ct);
468 tuple =
nla_nest_start(msg, repl ? CTA_TUPLE_REPLY : CTA_TUPLE_ORIG);
470 goto nla_put_failure;
474 goto nla_put_failure;
476 addr = nfnl_ct_get_src(ct, repl);
479 family == AF_INET ? CTA_IP_V4_SRC : CTA_IP_V6_SRC,
482 addr = nfnl_ct_get_dst(ct, repl);
485 family == AF_INET ? CTA_IP_V4_DST : CTA_IP_V6_DST,
492 goto nla_put_failure;
494 if (nfnl_ct_test_proto(ct))
495 NLA_PUT_U8(msg, CTA_PROTO_NUM, nfnl_ct_get_proto(ct));
497 if (nfnl_ct_test_src_port(ct, repl))
499 htons(nfnl_ct_get_src_port(ct, repl)));
501 if (nfnl_ct_test_dst_port(ct, repl))
503 htons(nfnl_ct_get_dst_port(ct, repl)));
505 if (family == AF_INET) {
506 if (nfnl_ct_test_icmp_id(ct, repl))
508 htons(nfnl_ct_get_icmp_id(ct, repl)));
510 if (nfnl_ct_test_icmp_type(ct, repl))
512 nfnl_ct_get_icmp_type(ct, repl));
514 if (nfnl_ct_test_icmp_code(ct, repl))
516 nfnl_ct_get_icmp_code(ct, repl));
517 }
else if (family == AF_INET6) {
518 if (nfnl_ct_test_icmp_id(ct, repl))
520 htons(nfnl_ct_get_icmp_id(ct, repl)));
522 if (nfnl_ct_test_icmp_type(ct, repl))
524 nfnl_ct_get_icmp_type(ct, repl));
526 if (nfnl_ct_test_icmp_code(ct, repl))
528 nfnl_ct_get_icmp_code(ct, repl));
540 static int nfnl_ct_build_message(
const struct nfnl_ct *ct,
int cmd,
int flags,
541 struct nl_msg **result)
548 nfnl_ct_get_family(ct), 0);
553 if (nfnl_ct_get_src(ct, 1) || nfnl_ct_get_dst(ct, 1)) {
555 if ((err = nfnl_ct_build_tuple(msg, ct, 1)) < 0)
559 if (!reply || nfnl_ct_get_src(ct, 0) || nfnl_ct_get_dst(ct, 0)) {
560 if ((err = nfnl_ct_build_tuple(msg, ct, 0)) < 0)
564 if (nfnl_ct_test_status(ct))
565 NLA_PUT_U32(msg, CTA_STATUS, htonl(nfnl_ct_get_status(ct)));
567 if (nfnl_ct_test_timeout(ct))
568 NLA_PUT_U32(msg, CTA_TIMEOUT, htonl(nfnl_ct_get_timeout(ct)));
570 if (nfnl_ct_test_mark(ct))
571 NLA_PUT_U32(msg, CTA_MARK, htonl(nfnl_ct_get_mark(ct)));
573 if (nfnl_ct_test_id(ct))
574 NLA_PUT_U32(msg, CTA_ID, htonl(nfnl_ct_get_id(ct)));
576 if (nfnl_ct_test_zone(ct))
577 NLA_PUT_U16(msg, CTA_ZONE, htons(nfnl_ct_get_zone(ct)));
588 int nfnl_ct_build_add_request(
const struct nfnl_ct *ct,
int flags,
589 struct nl_msg **result)
591 return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_NEW, flags, result);
594 int nfnl_ct_add(
struct nl_sock *sk,
const struct nfnl_ct *ct,
int flags)
599 if ((err = nfnl_ct_build_add_request(ct, flags, &msg)) < 0)
607 return wait_for_ack(sk);
610 int nfnl_ct_build_delete_request(
const struct nfnl_ct *ct,
int flags,
611 struct nl_msg **result)
613 return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_DELETE, flags, result);
616 int nfnl_ct_del(
struct nl_sock *sk,
const struct nfnl_ct *ct,
int flags)
621 if ((err = nfnl_ct_build_delete_request(ct, flags, &msg)) < 0)
629 return wait_for_ack(sk);
632 int nfnl_ct_build_query_request(
const struct nfnl_ct *ct,
int flags,
633 struct nl_msg **result)
635 return nfnl_ct_build_message(ct, IPCTNL_MSG_CT_GET, flags, result);
638 int nfnl_ct_query(
struct nl_sock *sk,
const struct nfnl_ct *ct,
int flags)
643 if ((err = nfnl_ct_build_query_request(ct, flags, &msg)) < 0)
651 return wait_for_ack(sk);
683 static struct nl_af_group ct_groups[] = {
684 { AF_UNSPEC, NFNLGRP_CONNTRACK_NEW },
685 { AF_UNSPEC, NFNLGRP_CONNTRACK_UPDATE },
686 { AF_UNSPEC, NFNLGRP_CONNTRACK_DESTROY },
687 { END_OF_GROUP_LIST },
690 #define NFNLMSG_CT_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_CTNETLINK, (type))
691 static struct nl_cache_ops nfnl_ct_ops = {
692 .co_name =
"netfilter/ct",
693 .co_hdrsize = NFNL_HDRLEN,
695 { NFNLMSG_CT_TYPE(IPCTNL_MSG_CT_NEW), NL_ACT_NEW,
"new" },
696 { NFNLMSG_CT_TYPE(IPCTNL_MSG_CT_GET), NL_ACT_GET,
"get" },
697 { NFNLMSG_CT_TYPE(IPCTNL_MSG_CT_DELETE), NL_ACT_DEL,
"del" },
698 END_OF_MSGTYPES_LIST,
700 .co_protocol = NETLINK_NETFILTER,
701 .co_groups = ct_groups,
702 .co_request_update = ct_request_update,
703 .co_msg_parser = ct_msg_parser,
704 .co_obj_ops = &ct_obj_ops,
707 static void __init ct_init(
void)
712 static void __exit ct_exit(
void)
struct nl_addr * nl_addr_alloc_attr(const struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
uint16_t nla_get_u16(const struct nlattr *nla)
Return payload of 16 bit integer attribute.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
uint64_t nla_get_u64(const struct nlattr *nla)
Return payload of u64 attribute.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
uint8_t nla_get_u8(const struct nlattr *nla)
Return value of 8 bit integer attribute.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, const struct nla_policy *policy)
Create attribute index based on nested attribute.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
@ NLA_NESTED
Nested attributes.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
int nl_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
int nfnl_ct_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Build a conntrack cache holding all conntrack currently in the kernel.
int nfnl_ct_dump_request(struct nl_sock *sk)
Send nfnl ct dump request.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
uint8_t nfnlmsg_family(struct nlmsghdr *nlh)
Get netfilter family from message.
int nfnl_send_simple(struct nl_sock *sk, uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
Send trivial netfilter netlink message.
uint8_t nfnlmsg_subtype(struct nlmsghdr *nlh)
Get netfilter message type from message.
struct nl_msg * nfnlmsg_alloc_simple(uint8_t subsys_id, uint8_t type, int flags, uint8_t family, uint16_t res_id)
Allocate a new netfilter netlink message.
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
Attribute validation policy.
uint16_t type
Type of attribute or NLA_UNSPEC.