17#include "nl-default.h"
21#include <linux/netfilter/nfnetlink_conntrack.h>
23#include <netlink/attr.h>
24#include <netlink/netfilter/nfnl.h>
25#include <netlink/netfilter/exp.h>
27#include "nl-netfilter.h"
28#include "nl-priv-dynamic-core/nl-core.h"
29#include "nl-priv-dynamic-core/cache-api.h"
31static struct nl_cache_ops nfnl_exp_ops;
78 struct nlattr *tb[CTA_IP_MAX+1];
86 if (tb[CTA_IP_V4_SRC]) {
90 err = nfnl_exp_set_src(exp, tuple, addr);
95 if (tb[CTA_IP_V4_DST]) {
99 err = nfnl_exp_set_dst(exp, tuple, addr);
104 if (tb[CTA_IP_V6_SRC]) {
108 err = nfnl_exp_set_src(exp, tuple, addr);
113 if (tb[CTA_IP_V6_DST]) {
117 err = nfnl_exp_set_dst(exp, tuple, addr);
131static int exp_parse_proto(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
133 struct nlattr *tb[CTA_PROTO_MAX+1];
135 uint16_t srcport = 0, dstport = 0, icmpid = 0;
136 uint8_t icmptype = 0, icmpcode = 0;
142 if (tb[CTA_PROTO_NUM])
143 nfnl_exp_set_l4protonum(exp, tuple,
nla_get_u8(tb[CTA_PROTO_NUM]));
145 if (tb[CTA_PROTO_SRC_PORT])
146 srcport = ntohs(
nla_get_u16(tb[CTA_PROTO_SRC_PORT]));
147 if (tb[CTA_PROTO_DST_PORT])
148 dstport = ntohs(
nla_get_u16(tb[CTA_PROTO_DST_PORT]));
149 if (tb[CTA_PROTO_SRC_PORT] || tb[CTA_PROTO_DST_PORT])
150 nfnl_exp_set_ports(exp, tuple, srcport, dstport);
152 if (tb[CTA_PROTO_ICMP_ID])
153 icmpid = ntohs(
nla_get_u16(tb[CTA_PROTO_ICMP_ID]));
154 if (tb[CTA_PROTO_ICMP_TYPE])
155 icmptype =
nla_get_u8(tb[CTA_PROTO_ICMP_TYPE]);
156 if (tb[CTA_PROTO_ICMP_CODE])
157 icmpcode =
nla_get_u8(tb[CTA_PROTO_ICMP_CODE]);
158 if (tb[CTA_PROTO_ICMP_ID] || tb[CTA_PROTO_ICMP_TYPE] || tb[CTA_PROTO_ICMP_CODE])
159 nfnl_exp_set_icmp(exp, tuple, icmpid, icmptype, icmpcode);
163static int exp_parse_tuple(
struct nfnl_exp *exp,
int tuple,
struct nlattr *attr)
165 struct nlattr *tb[CTA_TUPLE_MAX+1];
172 if (tb[CTA_TUPLE_IP]) {
173 err = exp_parse_ip(exp, tuple, tb[CTA_TUPLE_IP]);
178 if (tb[CTA_TUPLE_PROTO]) {
179 err = exp_parse_proto(exp, tuple, tb[CTA_TUPLE_PROTO]);
187static int exp_parse_nat(
struct nfnl_exp *exp,
struct nlattr *attr)
189 struct nlattr *tb[CTA_EXPECT_NAT_MAX+1];
196 if (tb[CTA_EXPECT_NAT_DIR])
197 nfnl_exp_set_nat_dir(exp,
nla_get_u32(tb[CTA_EXPECT_NAT_DIR]));
199 if (tb[CTA_EXPECT_NAT_TUPLE]) {
200 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_NAT, tb[CTA_EXPECT_NAT_TUPLE]);
208int nfnlmsg_exp_group(
struct nlmsghdr *nlh)
211 case IPCTNL_MSG_EXP_NEW:
212 if (nlh->nlmsg_flags & (NLM_F_CREATE|NLM_F_EXCL))
213 return NFNLGRP_CONNTRACK_EXP_NEW;
215 return NFNLGRP_CONNTRACK_EXP_UPDATE;
216 case IPCTNL_MSG_EXP_DELETE:
217 return NFNLGRP_CONNTRACK_EXP_DESTROY;
223int nfnlmsg_exp_parse(
struct nlmsghdr *nlh,
struct nfnl_exp **result)
226 struct nlattr *tb[CTA_MAX+1];
229 exp = nfnl_exp_alloc();
233 exp->ce_msgtype = nlh->nlmsg_type;
235 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, CTA_EXPECT_MAX,
242 if (tb[CTA_EXPECT_TUPLE]) {
243 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_EXPECT, tb[CTA_EXPECT_TUPLE]);
247 if (tb[CTA_EXPECT_MASTER]) {
248 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_MASTER, tb[CTA_EXPECT_MASTER]);
252 if (tb[CTA_EXPECT_MASK]) {
253 err = exp_parse_tuple(exp, NFNL_EXP_TUPLE_MASK, tb[CTA_EXPECT_MASK]);
258 if (tb[CTA_EXPECT_NAT]) {
259 err = exp_parse_nat(exp, tb[CTA_EXPECT_MASK]);
264 if (tb[CTA_EXPECT_CLASS])
265 nfnl_exp_set_class(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_CLASS])));
267 if (tb[CTA_EXPECT_FN])
268 nfnl_exp_set_fn(exp,
nla_data(tb[CTA_EXPECT_FN]));
270 if (tb[CTA_EXPECT_TIMEOUT])
271 nfnl_exp_set_timeout(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_TIMEOUT])));
273 if (tb[CTA_EXPECT_ID])
274 nfnl_exp_set_id(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_ID])));
276 if (tb[CTA_EXPECT_HELP_NAME])
277 nfnl_exp_set_helper_name(exp,
nla_data(tb[CTA_EXPECT_HELP_NAME]));
279 if (tb[CTA_EXPECT_ZONE])
280 nfnl_exp_set_zone(exp, ntohs(
nla_get_u16(tb[CTA_EXPECT_ZONE])));
282 if (tb[CTA_EXPECT_FLAGS])
283 nfnl_exp_set_flags(exp, ntohl(
nla_get_u32(tb[CTA_EXPECT_FLAGS])));
293static int exp_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
294 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
299 if ((err = nfnlmsg_exp_parse(nlh, &exp)) < 0)
302 err = pp->pp_cb((
struct nl_object *) exp, pp);
317 NLM_F_DUMP, AF_UNSPEC, 0);
320static int exp_request_update(
struct nl_cache *cache,
struct nl_sock *sk)
325static int exp_get_tuple_attr(
int tuple)
330 case CTA_EXPECT_MASTER:
331 attr = NFNL_EXP_TUPLE_MASTER;
333 case CTA_EXPECT_MASK:
334 attr = NFNL_EXP_TUPLE_MASK;
337 attr = NFNL_EXP_TUPLE_NAT;
339 case CTA_EXPECT_TUPLE:
341 attr = NFNL_EXP_TUPLE_EXPECT;
348static int nfnl_exp_build_tuple(
struct nl_msg *msg,
const struct nfnl_exp *exp,
351 struct nlattr *tuple, *ip, *proto;
352 struct nl_addr *addr;
356 family = nfnl_exp_get_family(exp);
358 type = exp_get_tuple_attr(cta);
360 if (cta == CTA_EXPECT_NAT)
366 goto nla_put_failure;
370 goto nla_put_failure;
372 addr = nfnl_exp_get_src(exp, type);
375 family == AF_INET ? CTA_IP_V4_SRC : CTA_IP_V6_SRC,
378 addr = nfnl_exp_get_dst(exp, type);
381 family == AF_INET ? CTA_IP_V4_DST : CTA_IP_V6_DST,
388 goto nla_put_failure;
390 if (nfnl_exp_test_l4protonum(exp, type))
391 NLA_PUT_U8(msg, CTA_PROTO_NUM, nfnl_exp_get_l4protonum(exp, type));
393 if (nfnl_exp_test_ports(exp, type)) {
395 htons(nfnl_exp_get_src_port(exp, type)));
398 htons(nfnl_exp_get_dst_port(exp, type)));
401 if (nfnl_exp_test_icmp(exp, type)) {
403 htons(nfnl_exp_get_icmp_id(exp, type)));
406 nfnl_exp_get_icmp_type(exp, type));
409 nfnl_exp_get_icmp_code(exp, type));
421static int nfnl_exp_build_nat(
struct nl_msg *msg,
const struct nfnl_exp *exp)
427 if (nfnl_exp_test_nat_dir(exp)) {
429 nfnl_exp_get_nat_dir(exp));
432 if (nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_NAT) < 0)
433 goto nla_put_failure;
442static int nfnl_exp_build_message(
const struct nfnl_exp *exp,
int cmd,
int flags,
443 struct nl_msg **result)
449 nfnl_exp_get_family(exp), 0);
453 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_TUPLE)) < 0)
456 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_MASTER)) < 0)
459 if ((err = nfnl_exp_build_tuple(msg, exp, CTA_EXPECT_MASK)) < 0)
462 if (nfnl_exp_test_src(exp, NFNL_EXP_TUPLE_NAT)) {
463 if ((err = nfnl_exp_build_nat(msg, exp)) < 0)
467 if (nfnl_exp_test_class(exp))
468 NLA_PUT_U32(msg, CTA_EXPECT_CLASS, htonl(nfnl_exp_get_class(exp)));
470 if (nfnl_exp_test_fn(exp))
473 if (nfnl_exp_test_id(exp))
474 NLA_PUT_U32(msg, CTA_EXPECT_ID, htonl(nfnl_exp_get_id(exp)));
476 if (nfnl_exp_test_timeout(exp))
477 NLA_PUT_U32(msg, CTA_EXPECT_TIMEOUT, htonl(nfnl_exp_get_timeout(exp)));
479 if (nfnl_exp_test_helper_name(exp))
480 NLA_PUT_STRING(msg, CTA_EXPECT_HELP_NAME, nfnl_exp_get_helper_name(exp));
482 if (nfnl_exp_test_zone(exp))
483 NLA_PUT_U16(msg, CTA_EXPECT_ZONE, htons(nfnl_exp_get_zone(exp)));
485 if (nfnl_exp_test_flags(exp))
486 NLA_PUT_U32(msg, CTA_EXPECT_FLAGS, htonl(nfnl_exp_get_flags(exp)));
499int nfnl_exp_build_add_request(
const struct nfnl_exp *exp,
int flags,
500 struct nl_msg **result)
502 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_NEW, flags, result);
505int nfnl_exp_add(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
510 if ((err = nfnl_exp_build_add_request(exp, flags, &msg)) < 0)
518 return wait_for_ack(sk);
521int nfnl_exp_build_delete_request(
const struct nfnl_exp *exp,
int flags,
522 struct nl_msg **result)
524 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_DELETE, flags, result);
527int nfnl_exp_del(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
532 if ((err = nfnl_exp_build_delete_request(exp, flags, &msg)) < 0)
540 return wait_for_ack(sk);
543int nfnl_exp_build_query_request(
const struct nfnl_exp *exp,
int flags,
544 struct nl_msg **result)
546 return nfnl_exp_build_message(exp, IPCTNL_MSG_EXP_GET, flags, result);
549int nfnl_exp_query(
struct nl_sock *sk,
const struct nfnl_exp *exp,
int flags)
554 if ((err = nfnl_exp_build_query_request(exp, flags, &msg)) < 0)
562 return wait_for_ack(sk);
594static struct nl_af_group exp_groups[] = {
595 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_NEW },
596 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_UPDATE },
597 { AF_UNSPEC, NFNLGRP_CONNTRACK_EXP_DESTROY },
598 { END_OF_GROUP_LIST },
601#define NFNLMSG_EXP_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_CTNETLINK_EXP, (type))
602static struct nl_cache_ops nfnl_exp_ops = {
603 .co_name =
"netfilter/exp",
604 .co_hdrsize = NFNL_HDRLEN,
606 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_NEW), NL_ACT_NEW,
"new" },
607 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_GET), NL_ACT_GET,
"get" },
608 { NFNLMSG_EXP_TYPE(IPCTNL_MSG_EXP_DELETE), NL_ACT_DEL,
"del" },
609 END_OF_MSGTYPES_LIST,
611 .co_protocol = NETLINK_NETFILTER,
612 .co_groups = exp_groups,
613 .co_request_update = exp_request_update,
614 .co_msg_parser = exp_msg_parser,
615 .co_obj_ops = &exp_obj_ops,
618static void _nl_init exp_init(
void)
623static void _nl_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.
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 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.
@ 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.