18 #include <sys/types.h>
19 #include <linux/netfilter/nfnetlink_conntrack.h>
21 #include <netlink-private/netlink.h>
22 #include <netlink/attr.h>
23 #include <netlink/netfilter/nfnl.h>
24 #include <netlink/netfilter/exp.h>
26 static struct nl_cache_ops nfnl_exp_ops;
28 static struct nla_policy exp_policy[CTA_EXPECT_MAX+1] = {
32 [CTA_EXPECT_TIMEOUT] = { .type =
NLA_U32 },
33 [CTA_EXPECT_ID] = { .type =
NLA_U32 },
34 [CTA_EXPECT_HELP_NAME] = { .type =
NLA_STRING },
35 [CTA_EXPECT_ZONE] = { .type =
NLA_U16 },
36 [CTA_EXPECT_FLAGS] = { .type =
NLA_U32 },
37 [CTA_EXPECT_CLASS] = { .type =
NLA_U32 },
42 static struct nla_policy exp_tuple_policy[CTA_TUPLE_MAX+1] = {
47 static struct nla_policy exp_ip_policy[CTA_IP_MAX+1] = {
49 [CTA_IP_V4_DST] = { .type =
NLA_U32 },
50 [CTA_IP_V6_SRC] = { .minlen = 16 },
51 [CTA_IP_V6_DST] = { .minlen = 16 },
54 static struct nla_policy exp_proto_policy[CTA_PROTO_MAX+1] = {
56 [CTA_PROTO_SRC_PORT] = { .type =
NLA_U16 },
57 [CTA_PROTO_DST_PORT] = { .type =
NLA_U16 },
58 [CTA_PROTO_ICMP_ID] = { .type =
NLA_U16 },
59 [CTA_PROTO_ICMP_TYPE] = { .type =
NLA_U8 },
60 [CTA_PROTO_ICMP_CODE] = { .type =
NLA_U8 },
61 [CTA_PROTO_ICMPV6_ID] = { .type =
NLA_U16 },
62 [CTA_PROTO_ICMPV6_TYPE] = { .type =
NLA_U8 },
63 [CTA_PROTO_ICMPV6_CODE] = { .type =
NLA_U8 },
66 static struct nla_policy exp_nat_policy[CTA_EXPECT_NAT_MAX+1] = {
68 [CTA_EXPECT_NAT_TUPLE] = { .type =
NLA_NESTED },
71 static int exp_parse_ip(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
73 struct nlattr *tb[CTA_IP_MAX+1];
81 if (tb[CTA_IP_V4_SRC]) {
85 err = nfnl_exp_set_src(exp, tuple, addr);
90 if (tb[CTA_IP_V4_DST]) {
94 err = nfnl_exp_set_dst(exp, tuple, addr);
99 if (tb[CTA_IP_V6_SRC]) {
103 err = nfnl_exp_set_src(exp, tuple, addr);
108 if (tb[CTA_IP_V6_DST]) {
112 err = nfnl_exp_set_dst(exp, tuple, addr);
126 static int exp_parse_proto(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
128 struct nlattr *tb[CTA_PROTO_MAX+1];
130 uint16_t srcport = 0, dstport = 0, icmpid = 0;
131 uint8_t icmptype = 0, icmpcode = 0;
137 if (tb[CTA_PROTO_NUM])
138 nfnl_exp_set_l4protonum(exp, tuple,
nla_get_u8(tb[CTA_PROTO_NUM]));
140 if (tb[CTA_PROTO_SRC_PORT])
141 srcport = ntohs(
nla_get_u16(tb[CTA_PROTO_SRC_PORT]));
142 if (tb[CTA_PROTO_DST_PORT])
143 dstport = ntohs(
nla_get_u16(tb[CTA_PROTO_DST_PORT]));
144 if (tb[CTA_PROTO_SRC_PORT] || tb[CTA_PROTO_DST_PORT])
145 nfnl_exp_set_ports(exp, tuple, srcport, dstport);
147 if (tb[CTA_PROTO_ICMP_ID])
148 icmpid = ntohs(
nla_get_u16(tb[CTA_PROTO_ICMP_ID]));
149 if (tb[CTA_PROTO_ICMP_TYPE])
150 icmptype =
nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
151 if (tb[CTA_PROTO_ICMP_CODE])
152 icmpcode =
nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
153 if (tb[CTA_PROTO_ICMP_ID] || tb[CTA_PROTO_ICMP_TYPE] || tb[CTA_PROTO_ICMP_CODE])
154 nfnl_exp_set_icmp(exp, tuple, icmpid, icmptype, icmpcode);
158 static int exp_parse_tuple(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
160 struct nlattr *tb[CTA_TUPLE_MAX+1];
167 if (tb[CTA_TUPLE_IP]) {
168 err = exp_parse_ip(exp, tuple, tb[CTA_TUPLE_IP]);
173 if (tb[CTA_TUPLE_PROTO]) {
174 err = exp_parse_proto(exp, tuple, tb[CTA_TUPLE_PROTO]);
182 static int exp_parse_nat(
struct nfnl_exp *exp,
struct nlattr *attr)
184 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
191 if (tb[CTA_EXPECT_NAT_DIR])
192 nfnl_exp_set_nat_dir(exp,
nla_get_u32(tb[CTA_EXPECT_NAT_DIR]));
194 if (tb[CTA_EXPECT_NAT_TUPLE]) {
195 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_NAT, tb[CTA_EXPECT_NAT_TUPLE]);
203 int nfnlmsg_exp_group(
struct nlmsghdr *nlh)
206 case IPCTNL_MSG_EXP_NEW:
207 if (nlh->nlmsg_flags & (NLM_F_CREATE|NLM_F_EXCL))
208 return NFNLGRP_CONNTRACK_EXP_NEW;
210 return NFNLGRP_CONNTRACK_EXP_UPDATE;
211 case IPCTNL_MSG_EXP_DELETE:
212 return NFNLGRP_CONNTRACK_EXP_DESTROY;
218 int nfnlmsg_exp_parse(
struct nlmsghdr *nlh,
struct nfnl_exp **result)
220 struct nfnl_exp *exp;
221 struct nlattr *tb[CTA_MAX+1];
224 exp = nfnl_exp_alloc();
228 exp->ce_msgtype = nlh->nlmsg_type;
230 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, CTA_EXPECT_MAX,
237 if (tb[CTA_EXPECT_TUPLE]) {
238 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_EXPECT, tb[CTA_EXPECT_TUPLE]);
242 if (tb[CTA_EXPECT_MASTER]) {
243 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_MASTER, tb[CTA_EXPECT_MASTER]);
247 if (tb[CTA_EXPECT_MASK]) {
248 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_MASK, tb[CTA_EXPECT_MASK]);
253 if (tb[CTA_EXPECT_NAT]) {
254 err = exp_parse_nat(exp, tb[CTA_EXPECT_MASK]);
259 if (tb[CTA_EXPECT_CLASS])
260 nfnl_exp_set_class(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_CLASS])));
262 if (tb[CTA_EXPECT_FN])
263 nfnl_exp_set_fn(exp,
nla_data(tb[CTA_EXPECT_FN]));
265 if (tb[CTA_EXPECT_TIMEOUT])
266 nfnl_exp_set_timeout(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_TIMEOUT])));
268 if (tb[CTA_EXPECT_ID])
269 nfnl_exp_set_id(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_ID])));
271 if (tb[CTA_EXPECT_HELP_NAME])
272 nfnl_exp_set_helper_name(exp,
nla_data(tb[CTA_EXPECT_HELP_NAME]));
274 if (tb[CTA_EXPECT_ZONE])
275 nfnl_exp_set_zone(exp, ntohs(
nla_get_u16(tb[CTA_EXPECT_ZONE])));
277 if (tb[CTA_EXPECT_FLAGS])
278 nfnl_exp_set_flags(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_FLAGS])));
288 static int exp_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
289 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
291 struct nfnl_exp *exp;
294 if ((err = nfnlmsg_exp_parse(nlh, &exp)) < 0)
297 err = pp->pp_cb((
struct nl_object *) exp, pp);
312 NLM_F_DUMP, AF_UNSPEC, 0);
315 static int exp_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
320 static int exp_get_tuple_attr(
int tuple)
325 case CTA_EXPECT_MASTER:
326 attr = NFNL_EXP_TUPLE_MASTER;
328 case CTA_EXPECT_MASK:
329 attr = NFNL_EXP_TUPLE_MASK;
332 attr = NFNL_EXP_TUPLE_NAT;
334 case CTA_EXPECT_TUPLE:
336 attr = NFNL_EXP_TUPLE_EXPECT;
343 static int nfnl_exp_build_tuple(
struct nl_msg *msg,
const struct nfnl_exp *exp,
346 struct nlattr *tuple, *ip, *proto;
347 struct nl_addr *addr;
351 family = nfnl_exp_get_family(exp);
353 type = exp_get_tuple_attr(cta);
355 if (cta == CTA_EXPECT_NAT)
361 goto nla_put_failure;
365 goto nla_put_failure;
367 addr = nfnl_exp_get_src(exp, type);
370 family == AF_INET ? CTA_IP_V4_SRC : CTA_IP_V6_SRC,
373 addr = nfnl_exp_get_dst(exp, type);
376 family == AF_INET ? CTA_IP_V4_DST : CTA_IP_V6_DST,
383 goto nla_put_failure;
385 if (nfnl_exp_test_l4protonum(exp, type))
386 NLA_PUT_U8(msg, CTA_PROTO_NUM, nfnl_exp_get_l4protonum(exp, type));
388 if (nfnl_exp_test_ports(exp, type)) {
390 htons(nfnl_exp_get_src_port(exp, type)));
393 htons(nfnl_exp_get_dst_port(exp, type)));
396 if (nfnl_exp_test_icmp(exp, type)) {
398 htons(nfnl_exp_get_icmp_id(exp, type)));
401 nfnl_exp_get_icmp_type(exp, type));
404 nfnl_exp_get_icmp_code(exp, type));
416 static int nfnl_exp_build_nat(
struct nl_msg *msg,
const struct nfnl_exp *exp)
422 if (nfnl_exp_test_nat_dir(exp)) {
424 nfnl_exp_get_nat_dir(exp));
427 if (nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_NAT) < 0)
428 goto nla_put_failure;
437 static int nfnl_exp_build_message(
const struct nfnl_exp *exp,
int cmd,
int flags,
438 struct nl_msg **result)
444 nfnl_exp_get_family(exp), 0);
448 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_TUPLE)) < 0)
451 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_MASTER)) < 0)
454 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_MASK)) < 0)
457 if (nfnl_exp_test_src(exp, NFNL_EXP_TUPLE_NAT)) {
458 if ((err = nfnl_exp_build_nat(msg, exp)) < 0)
462 if (nfnl_exp_test_class(exp))
463 NLA_PUT_U32(msg, CTA_EXPECT_CLASS, htonl(nfnl_exp_get_class(exp)));
465 if (nfnl_exp_test_fn(exp))
468 if (nfnl_exp_test_id(exp))
469 NLA_PUT_U32(msg, CTA_EXPECT_ID, htonl(nfnl_exp_get_id(exp)));
471 if (nfnl_exp_test_timeout(exp))
472 NLA_PUT_U32(msg, CTA_EXPECT_TIMEOUT, htonl(nfnl_exp_get_timeout(exp)));
474 if (nfnl_exp_test_helper_name(exp))
475 NLA_PUT_STRING(msg, CTA_EXPECT_HELP_NAME, nfnl_exp_get_helper_name(exp));
477 if (nfnl_exp_test_zone(exp))
478 NLA_PUT_U16(msg, CTA_EXPECT_ZONE, htons(nfnl_exp_get_zone(exp)));
480 if (nfnl_exp_test_flags(exp))
481 NLA_PUT_U32(msg, CTA_EXPECT_FLAGS, htonl(nfnl_exp_get_flags(exp)));
494 int nfnl_exp_build_add_request(
const struct nfnl_exp *exp,
int flags,
495 struct nl_msg **result)
497 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_NEW, flags, result);
500 int nfnl_exp_add(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
505 if ((err = nfnl_exp_build_add_request(exp, flags, &msg)) < 0)
513 return wait_for_ack(sk);
516 int nfnl_exp_build_delete_request(
const struct nfnl_exp *exp,
int flags,
517 struct nl_msg **result)
519 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_DELETE, flags, result);
522 int nfnl_exp_del(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
527 if ((err = nfnl_exp_build_delete_request(exp, flags, &msg)) < 0)
535 return wait_for_ack(sk);
538 int nfnl_exp_build_query_request(
const struct nfnl_exp *exp,
int flags,
539 struct nl_msg **result)
541 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_GET, flags, result);
544 int nfnl_exp_query(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
549 if ((err = nfnl_exp_build_query_request(exp, flags, &msg)) < 0)
557 return wait_for_ack(sk);
589 static struct nl_af_group exp_groups[] = {
590 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_NEW },
591 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_UPDATE },
592 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_DESTROY },
593 { END_OF_GROUP_LIST },
596 #define NFNLMSG_EXP_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_CTNETLINK_EXP, (type))
597 static struct nl_cache_ops nfnl_exp_ops = {
598 .co_name =
"netfilter/exp",
599 .co_hdrsize = NFNL_HDRLEN,
601 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_NEW), NL_ACT_NEW,
"new" },
602 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_GET), NL_ACT_GET,
"get" },
603 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_DELETE), NL_ACT_DEL,
"del" },
604 END_OF_MSGTYPES_LIST,
606 .co_protocol = NETLINK_NETFILTER,
607 .co_groups = exp_groups,
608 .co_request_update = exp_request_update,
609 .co_msg_parser = exp_msg_parser,
610 .co_obj_ops = &exp_obj_ops,
613 static void __init exp_init(
void)
618 static void __exit exp_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.
#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.
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
@ NLA_STRING
NUL terminated character string.
@ 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_exp_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Build a expectation cache holding all expectations currently in the kernel.
int nfnl_exp_dump_request(struct nl_sock *sk)
Send nfnl exp 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.