23 #include <sys/types.h> 24 #include <linux/netfilter/nfnetlink_log.h> 26 #include <netlink-private/netlink.h> 27 #include <netlink/attr.h> 28 #include <netlink/netfilter/nfnl.h> 29 #include <netlink/netfilter/log_msg.h> 30 #include <netlink-private/utils.h> 32 static struct nla_policy log_msg_policy[NFULA_MAX+1] = {
33 [NFULA_PACKET_HDR] = {
34 .
minlen =
sizeof(
struct nfulnl_msg_packet_hdr)
36 [NFULA_MARK] = { .type =
NLA_U32 },
38 .minlen =
sizeof(
struct nfulnl_msg_packet_timestamp)
40 [NFULA_IFINDEX_INDEV] = { .type =
NLA_U32 },
41 [NFULA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
42 [NFULA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
43 [NFULA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
45 .minlen =
sizeof(
struct nfulnl_msg_packet_hw)
49 [NFULA_UID] = { .type =
NLA_U32 },
50 [NFULA_GID] = { .type =
NLA_U32 },
51 [NFULA_SEQ] = { .type =
NLA_U32 },
52 [NFULA_SEQ_GLOBAL] = { .type =
NLA_U32 },
55 int nfnlmsg_log_msg_parse(
struct nlmsghdr *nlh,
struct nfnl_log_msg **result)
57 struct nfnl_log_msg *msg;
58 struct nlattr *tb[NFULA_MAX+1];
62 msg = nfnl_log_msg_alloc();
66 msg->ce_msgtype = nlh->nlmsg_type;
68 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFULA_MAX,
75 attr = tb[NFULA_PACKET_HDR];
77 struct nfulnl_msg_packet_hdr *hdr =
nla_data(attr);
80 nfnl_log_msg_set_hwproto(msg, hdr->hw_protocol);
81 nfnl_log_msg_set_hook(msg, hdr->hook);
84 attr = tb[NFULA_MARK];
86 nfnl_log_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
88 attr = tb[NFULA_TIMESTAMP];
90 struct nfulnl_msg_packet_timestamp *timestamp =
nla_data(attr);
93 tv.tv_sec = ntohll(timestamp->sec);
94 tv.tv_usec = ntohll(timestamp->usec);
95 nfnl_log_msg_set_timestamp(msg, &tv);
98 attr = tb[NFULA_IFINDEX_INDEV];
100 nfnl_log_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
102 attr = tb[NFULA_IFINDEX_OUTDEV];
104 nfnl_log_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
106 attr = tb[NFULA_IFINDEX_PHYSINDEV];
108 nfnl_log_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
110 attr = tb[NFULA_IFINDEX_PHYSOUTDEV];
112 nfnl_log_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
114 attr = tb[NFULA_HWADDR];
116 struct nfulnl_msg_packet_hw *hw =
nla_data(attr);
118 nfnl_log_msg_set_hwaddr(msg, hw->hw_addr, ntohs(hw->hw_addrlen));
121 attr = tb[NFULA_PAYLOAD];
128 attr = tb[NFULA_PREFIX];
130 err = nfnl_log_msg_set_prefix(msg,
nla_data(attr));
135 attr = tb[NFULA_UID];
137 nfnl_log_msg_set_uid(msg, ntohl(
nla_get_u32(attr)));
139 attr = tb[NFULA_GID];
141 nfnl_log_msg_set_gid(msg, ntohl(
nla_get_u32(attr)));
143 attr = tb[NFULA_SEQ];
145 nfnl_log_msg_set_seq(msg, ntohl(
nla_get_u32(attr)));
147 attr = tb[NFULA_SEQ_GLOBAL];
149 nfnl_log_msg_set_seq_global(msg, ntohl(
nla_get_u32(attr)));
155 nfnl_log_msg_put(msg);
159 static int log_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
160 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
162 struct nfnl_log_msg *msg;
165 if ((err = nfnlmsg_log_msg_parse(nlh, &msg)) < 0)
168 err = pp->pp_cb((
struct nl_object *) msg, pp);
169 nfnl_log_msg_put(msg);
175 #define NFNLMSG_LOG_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_ULOG, (type)) 176 static struct nl_cache_ops nfnl_log_msg_ops = {
177 .co_name =
"netfilter/log_msg",
178 .co_hdrsize = NFNL_HDRLEN,
180 { NFNLMSG_LOG_TYPE(NFULNL_MSG_PACKET), NL_ACT_NEW,
"new" },
181 END_OF_MSGTYPES_LIST,
183 .co_protocol = NETLINK_NETFILTER,
184 .co_msg_parser = log_msg_parser,
185 .co_obj_ops = &log_msg_obj_ops,
188 static void __init log_msg_init(
void)
193 static void __exit log_msg_exit(
void)
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, const struct nla_policy *policy)
parse attributes of a netlink message
int nl_cache_mngt_unregister(struct nl_cache_ops *ops)
Unregister a set of cache operations.
Attribute validation policy.
uint32_t nla_get_u32(const struct nlattr *nla)
Return payload of 32 bit integer attribute.
NUL terminated character string.
int nl_cache_mngt_register(struct nl_cache_ops *ops)
Register a set of cache operations.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
int nla_len(const struct nlattr *nla)
Return length of the payload .
uint8_t nfnlmsg_family(struct nlmsghdr *nlh)
Get netfilter family from message.
uint16_t minlen
Minimal length of payload required.