24 #include <sys/types.h>
25 #include <linux/netfilter/nfnetlink_conntrack.h>
27 #include <netlink-private/netlink.h>
28 #include <netlink/attr.h>
29 #include <netlink/netfilter/nfnl.h>
30 #include <netlink/netfilter/exp.h>
32 static struct nl_cache_ops nfnl_exp_ops;
34 static struct nla_policy exp_policy[CTA_EXPECT_MAX+1] = {
38 [CTA_EXPECT_TIMEOUT] = { .type =
NLA_U32 },
39 [CTA_EXPECT_ID] = { .type =
NLA_U32 },
40 [CTA_EXPECT_HELP_NAME] = { .type =
NLA_STRING },
41 [CTA_EXPECT_ZONE] = { .type =
NLA_U16 },
42 [CTA_EXPECT_FLAGS] = { .type =
NLA_U32 },
43 [CTA_EXPECT_CLASS] = { .type =
NLA_U32 },
48 static struct nla_policy exp_tuple_policy[CTA_TUPLE_MAX+1] = {
53 static struct nla_policy exp_ip_policy[CTA_IP_MAX+1] = {
55 [CTA_IP_V4_DST] = { .type =
NLA_U32 },
56 [CTA_IP_V6_SRC] = { .minlen = 16 },
57 [CTA_IP_V6_DST] = { .minlen = 16 },
60 static struct nla_policy exp_proto_policy[CTA_PROTO_MAX+1] = {
62 [CTA_PROTO_SRC_PORT] = { .type =
NLA_U16 },
63 [CTA_PROTO_DST_PORT] = { .type =
NLA_U16 },
64 [CTA_PROTO_ICMP_ID] = { .type =
NLA_U16 },
65 [CTA_PROTO_ICMP_TYPE] = { .type =
NLA_U8 },
66 [CTA_PROTO_ICMP_CODE] = { .type =
NLA_U8 },
67 [CTA_PROTO_ICMPV6_ID] = { .type =
NLA_U16 },
68 [CTA_PROTO_ICMPV6_TYPE] = { .type =
NLA_U8 },
69 [CTA_PROTO_ICMPV6_CODE] = { .type =
NLA_U8 },
72 static struct nla_policy exp_nat_policy[CTA_EXPECT_NAT_MAX+1] = {
74 [CTA_EXPECT_NAT_TUPLE] = { .type =
NLA_NESTED },
77 static int exp_parse_ip(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
79 struct nlattr *tb[CTA_IP_MAX+1];
87 if (tb[CTA_IP_V4_SRC]) {
91 err = nfnl_exp_set_src(exp, tuple, addr);
96 if (tb[CTA_IP_V4_DST]) {
100 err = nfnl_exp_set_dst(exp, tuple, addr);
105 if (tb[CTA_IP_V6_SRC]) {
109 err = nfnl_exp_set_src(exp, tuple, addr);
114 if (tb[CTA_IP_V6_DST]) {
118 err = nfnl_exp_set_dst(exp, tuple, addr);
132 static int exp_parse_proto(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
134 struct nlattr *tb[CTA_PROTO_MAX+1];
136 uint16_t srcport = 0, dstport = 0, icmpid = 0;
137 uint8_t icmptype = 0, icmpcode = 0;
143 if (tb[CTA_PROTO_NUM])
144 nfnl_exp_set_l4protonum(exp, tuple,
nla_get_u8(tb[CTA_PROTO_NUM]));
146 if (tb[CTA_PROTO_SRC_PORT])
147 srcport = ntohs(
nla_get_u16(tb[CTA_PROTO_SRC_PORT]));
148 if (tb[CTA_PROTO_DST_PORT])
149 dstport = ntohs(
nla_get_u16(tb[CTA_PROTO_DST_PORT]));
150 if (tb[CTA_PROTO_SRC_PORT] || tb[CTA_PROTO_DST_PORT])
151 nfnl_exp_set_ports(exp, tuple, srcport, dstport);
153 if (tb[CTA_PROTO_ICMP_ID])
154 icmpid = ntohs(
nla_get_u16(tb[CTA_PROTO_ICMP_ID]));
155 if (tb[CTA_PROTO_ICMP_TYPE])
156 icmptype =
nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
157 if (tb[CTA_PROTO_ICMP_CODE])
158 icmpcode =
nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
159 if (tb[CTA_PROTO_ICMP_ID] || tb[CTA_PROTO_ICMP_TYPE] || tb[CTA_PROTO_ICMP_CODE])
160 nfnl_exp_set_icmp(exp, tuple, icmpid, icmptype, icmpcode);
164 static int exp_parse_tuple(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
166 struct nlattr *tb[CTA_TUPLE_MAX+1];
173 if (tb[CTA_TUPLE_IP]) {
174 err = exp_parse_ip(exp, tuple, tb[CTA_TUPLE_IP]);
179 if (tb[CTA_TUPLE_PROTO]) {
180 err = exp_parse_proto(exp, tuple, tb[CTA_TUPLE_PROTO]);
188 static int exp_parse_nat(
struct nfnl_exp *exp,
struct nlattr *attr)
190 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
197 if (tb[CTA_EXPECT_NAT_DIR])
198 nfnl_exp_set_nat_dir(exp,
nla_get_u32(tb[CTA_EXPECT_NAT_DIR]));
200 if (tb[CTA_EXPECT_NAT_TUPLE]) {
201 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_NAT, tb[CTA_EXPECT_NAT_TUPLE]);
209 int nfnlmsg_exp_group(
struct nlmsghdr *nlh)
212 case IPCTNL_MSG_EXP_NEW:
213 if (nlh->nlmsg_flags & (NLM_F_CREATE|NLM_F_EXCL))
214 return NFNLGRP_CONNTRACK_EXP_NEW;
216 return NFNLGRP_CONNTRACK_EXP_UPDATE;
217 case IPCTNL_MSG_EXP_DELETE:
218 return NFNLGRP_CONNTRACK_EXP_DESTROY;
224 int nfnlmsg_exp_parse(
struct nlmsghdr *nlh,
struct nfnl_exp **result)
226 struct nfnl_exp *exp;
227 struct nlattr *tb[CTA_MAX+1];
230 exp = nfnl_exp_alloc();
234 exp->ce_msgtype = nlh->nlmsg_type;
236 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, CTA_EXPECT_MAX,
243 if (tb[CTA_EXPECT_TUPLE]) {
244 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_EXPECT, tb[CTA_EXPECT_TUPLE]);
248 if (tb[CTA_EXPECT_MASTER]) {
249 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_MASTER, tb[CTA_EXPECT_MASTER]);
253 if (tb[CTA_EXPECT_MASK]) {
254 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_MASK, tb[CTA_EXPECT_MASK]);
259 if (tb[CTA_EXPECT_NAT]) {
260 err = exp_parse_nat(exp, tb[CTA_EXPECT_MASK]);
265 if (tb[CTA_EXPECT_CLASS])
266 nfnl_exp_set_class(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_CLASS])));
268 if (tb[CTA_EXPECT_FN])
269 nfnl_exp_set_fn(exp,
nla_data(tb[CTA_EXPECT_FN]));
271 if (tb[CTA_EXPECT_TIMEOUT])
272 nfnl_exp_set_timeout(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_TIMEOUT])));
274 if (tb[CTA_EXPECT_ID])
275 nfnl_exp_set_id(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_ID])));
277 if (tb[CTA_EXPECT_HELP_NAME])
278 nfnl_exp_set_helper_name(exp,
nla_data(tb[CTA_EXPECT_HELP_NAME]));
280 if (tb[CTA_EXPECT_ZONE])
281 nfnl_exp_set_zone(exp, ntohs(
nla_get_u16(tb[CTA_EXPECT_ZONE])));
283 if (tb[CTA_EXPECT_FLAGS])
284 nfnl_exp_set_flags(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_FLAGS])));
294 static int exp_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
295 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
297 struct nfnl_exp *exp;
300 if ((err = nfnlmsg_exp_parse(nlh, &exp)) < 0)
303 err = pp->pp_cb((
struct nl_object *) exp, pp);
308 int nfnl_exp_dump_request(
struct nl_sock *sk)
311 NLM_F_DUMP, AF_UNSPEC, 0);
314 static int exp_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
316 return nfnl_exp_dump_request(sk);
319 static int exp_get_tuple_attr(
int tuple)
324 case CTA_EXPECT_MASTER:
325 attr = NFNL_EXP_TUPLE_MASTER;
327 case CTA_EXPECT_MASK:
328 attr = NFNL_EXP_TUPLE_MASK;
331 attr = NFNL_EXP_TUPLE_NAT;
333 case CTA_EXPECT_TUPLE:
335 attr = NFNL_EXP_TUPLE_EXPECT;
342 static int nfnl_exp_build_tuple(
struct nl_msg *msg,
const struct nfnl_exp *exp,
345 struct nlattr *tuple, *ip, *proto;
346 struct nl_addr *addr;
349 family = nfnl_exp_get_family(exp);
351 int type = exp_get_tuple_attr(cta);
353 if (cta == CTA_EXPECT_NAT)
359 goto nla_put_failure;
363 goto nla_put_failure;
365 addr = nfnl_exp_get_src(exp, type);
368 family == AF_INET ? CTA_IP_V4_SRC : CTA_IP_V6_SRC,
371 addr = nfnl_exp_get_dst(exp, type);
374 family == AF_INET ? CTA_IP_V4_DST : CTA_IP_V6_DST,
381 goto nla_put_failure;
383 if (nfnl_exp_test_l4protonum(exp, type))
384 NLA_PUT_U8(msg, CTA_PROTO_NUM, nfnl_exp_get_l4protonum(exp, type));
386 if (nfnl_exp_test_ports(exp, type)) {
388 htons(nfnl_exp_get_src_port(exp, type)));
391 htons(nfnl_exp_get_dst_port(exp, type)));
394 if (nfnl_exp_test_icmp(exp, type)) {
396 htons(nfnl_exp_get_icmp_id(exp, type)));
399 nfnl_exp_get_icmp_type(exp, type));
402 nfnl_exp_get_icmp_code(exp, type));
414 static int nfnl_exp_build_nat(
struct nl_msg *msg,
const struct nfnl_exp *exp)
421 if (nfnl_exp_test_nat_dir(exp)) {
423 nfnl_exp_get_nat_dir(exp));
426 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_NAT)) < 0)
427 goto nla_put_failure;
436 static int nfnl_exp_build_message(
const struct nfnl_exp *exp,
int cmd,
int flags,
437 struct nl_msg **result)
443 nfnl_exp_get_family(exp), 0);
447 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_TUPLE)) < 0)
450 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_MASTER)) < 0)
453 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_MASK)) < 0)
456 if (nfnl_exp_test_src(exp, NFNL_EXP_TUPLE_NAT)) {
457 if ((err = nfnl_exp_build_nat(msg, exp)) < 0)
461 if (nfnl_exp_test_class(exp))
462 NLA_PUT_U32(msg, CTA_EXPECT_CLASS, htonl(nfnl_exp_get_class(exp)));
464 if (nfnl_exp_test_fn(exp))
467 if (nfnl_exp_test_id(exp))
468 NLA_PUT_U32(msg, CTA_EXPECT_ID, htonl(nfnl_exp_get_id(exp)));
470 if (nfnl_exp_test_timeout(exp))
471 NLA_PUT_U32(msg, CTA_EXPECT_TIMEOUT, htonl(nfnl_exp_get_timeout(exp)));
473 if (nfnl_exp_test_helper_name(exp))
474 NLA_PUT_STRING(msg, CTA_EXPECT_HELP_NAME, nfnl_exp_get_helper_name(exp));
476 if (nfnl_exp_test_zone(exp))
477 NLA_PUT_U16(msg, CTA_EXPECT_ZONE, htons(nfnl_exp_get_zone(exp)));
479 if (nfnl_exp_test_flags(exp))
480 NLA_PUT_U32(msg, CTA_EXPECT_FLAGS, htonl(nfnl_exp_get_flags(exp)));
491 int nfnl_exp_build_add_request(
const struct nfnl_exp *exp,
int flags,
492 struct nl_msg **result)
494 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_NEW, flags, result);
497 int nfnl_exp_add(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
502 if ((err = nfnl_exp_build_add_request(exp, flags, &msg)) < 0)
510 return wait_for_ack(sk);
513 int nfnl_exp_build_delete_request(
const struct nfnl_exp *exp,
int flags,
514 struct nl_msg **result)
516 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_DELETE, flags, result);
519 int nfnl_exp_del(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
524 if ((err = nfnl_exp_build_delete_request(exp, flags, &msg)) < 0)
532 return wait_for_ack(sk);
535 int nfnl_exp_build_query_request(
const struct nfnl_exp *exp,
int flags,
536 struct nl_msg **result)
538 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_GET, flags, result);
541 int nfnl_exp_query(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
546 if ((err = nfnl_exp_build_query_request(exp, flags, &msg)) < 0)
554 return wait_for_ack(sk);
586 static struct nl_af_group exp_groups[] = {
587 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_NEW },
588 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_UPDATE },
589 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_DESTROY },
590 { END_OF_GROUP_LIST },
593 #define NFNLMSG_EXP_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_CTNETLINK_EXP, (type))
594 static struct nl_cache_ops nfnl_exp_ops = {
595 .co_name =
"netfilter/exp",
596 .co_hdrsize = NFNL_HDRLEN,
598 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_NEW), NL_ACT_NEW,
"new" },
599 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_GET), NL_ACT_GET,
"get" },
600 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_DELETE), NL_ACT_DEL,
"del" },
601 END_OF_MSGTYPES_LIST,
603 .co_protocol = NETLINK_NETFILTER,
604 .co_groups = exp_groups,
605 .co_request_update = exp_request_update,
606 .co_msg_parser = exp_msg_parser,
607 .co_obj_ops = &exp_obj_ops,
610 static void __init exp_init(
void)
615 static void __exit exp_exit(
void)
int nl_send_auto_complete(struct nl_sock *sk, struct nl_msg *msg)
uint16_t nla_get_u16(struct nlattr *nla)
Return payload of 16 bit integer attribute.
void nlmsg_free(struct nl_msg *msg)
Release a reference from an netlink message.
#define NLA_PUT_ADDR(msg, attrtype, addr)
Add address attribute to netlink message.
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Attribute validation policy.
struct nl_addr * nl_addr_alloc_attr(struct nlattr *nla, int family)
Allocate abstract address based on Netlink attribute.
int nfnl_exp_alloc_cache(struct nl_sock *sk, struct nl_cache **result)
Build a expectation cache holding all expectations currently in the kernel.
uint8_t nfnlmsg_subtype(struct nlmsghdr *nlh)
Get netfilter message type from message.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
#define NLA_PUT_U8(msg, attrtype, value)
Add 8 bit integer attribute to netlink message.
NUL terminated character string.
int nla_nest_end(struct nl_msg *msg, struct nlattr *start)
Finalize nesting of attributes.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
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.
int nla_parse_nested(struct nlattr *tb[], int maxtype, struct nlattr *nla, struct nla_policy *policy)
Create attribute index based on nested attribute.
uint8_t nla_get_u8(struct nlattr *nla)
Return value of 8 bit integer attribute.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
#define NLA_PUT_U32(msg, attrtype, value)
Add 32 bit integer attribute to netlink message.
uint8_t nfnlmsg_family(struct nlmsghdr *nlh)
Get netfilter family from message.
#define NLA_PUT_STRING(msg, attrtype, value)
Add string attribute to netlink message.
void nl_addr_put(struct nl_addr *addr)
Decrease the reference counter of an abstract address.
uint16_t type
Type of attribute or NLA_UNSPEC.
#define NLA_PUT_U16(msg, attrtype, value)
Add 16 bit integer attribute to netlink message.
uint32_t nla_get_u32(struct nlattr *nla)
Return payload of 32 bit integer attribute.
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_cache_alloc_and_fill(struct nl_cache_ops *ops, struct nl_sock *sock, struct nl_cache **result)
Allocate new cache and fill it.
struct nlattr * nla_nest_start(struct nl_msg *msg, int attrtype)
Start a new level of nested attributes.