16#include "nl-default.h"
20#include <linux/netfilter/nfnetlink_log.h>
22#include <netlink/attr.h>
23#include <netlink/netfilter/nfnl.h>
24#include <netlink/netfilter/log_msg.h>
26#include "nl-netfilter.h"
27#include "nl-priv-dynamic-core/cache-api.h"
31 .
minlen =
sizeof(
struct nfulnl_msg_packet_hdr)
33 [NFULA_MARK] = { .type =
NLA_U32 },
35 .minlen =
sizeof(
struct nfulnl_msg_packet_timestamp)
37 [NFULA_IFINDEX_INDEV] = { .type =
NLA_U32 },
38 [NFULA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
39 [NFULA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
40 [NFULA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
42 .minlen =
sizeof(
struct nfulnl_msg_packet_hw)
46 [NFULA_UID] = { .type =
NLA_U32 },
47 [NFULA_GID] = { .type =
NLA_U32 },
48 [NFULA_SEQ] = { .type =
NLA_U32 },
49 [NFULA_SEQ_GLOBAL] = { .type =
NLA_U32 },
50 [NFULA_HWTYPE] = { .type =
NLA_U16 },
51 [NFULA_HWLEN] = { .type =
NLA_U16 },
54 [NFULA_CT_INFO] = { .type =
NLA_U32 },
65 struct nlattr *tb[NFULA_VLAN_MAX+1];
74 attr = tb[NFULA_VLAN_PROTO];
76 nfnl_log_msg_set_vlan_proto(msg,
nla_get_u16(attr));
78 attr = tb[NFULA_VLAN_TCI];
80 nfnl_log_msg_set_vlan_tag(msg, ntohs(
nla_get_u16(attr)));
85int nfnlmsg_log_msg_parse(
struct nlmsghdr *nlh,
struct nfnl_log_msg **result)
88 struct nlattr *tb[NFULA_MAX+1];
92 msg = nfnl_log_msg_alloc();
96 msg->ce_msgtype = nlh->nlmsg_type;
98 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFULA_MAX,
105 attr = tb[NFULA_PACKET_HDR];
107 struct nfulnl_msg_packet_hdr *hdr =
nla_data(attr);
109 if (hdr->hw_protocol)
110 nfnl_log_msg_set_hwproto(msg, hdr->hw_protocol);
111 nfnl_log_msg_set_hook(msg, hdr->hook);
114 attr = tb[NFULA_MARK];
116 nfnl_log_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
118 attr = tb[NFULA_TIMESTAMP];
120 struct nfulnl_msg_packet_timestamp *timestamp =
nla_data(attr);
123 tv.tv_sec = ntohll(timestamp->sec);
124 tv.tv_usec = ntohll(timestamp->usec);
125 nfnl_log_msg_set_timestamp(msg, &tv);
128 attr = tb[NFULA_IFINDEX_INDEV];
130 nfnl_log_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
132 attr = tb[NFULA_IFINDEX_OUTDEV];
134 nfnl_log_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
136 attr = tb[NFULA_IFINDEX_PHYSINDEV];
138 nfnl_log_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
140 attr = tb[NFULA_IFINDEX_PHYSOUTDEV];
142 nfnl_log_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
144 attr = tb[NFULA_HWADDR];
146 struct nfulnl_msg_packet_hw *hw =
nla_data(attr);
148 nfnl_log_msg_set_hwaddr(msg, hw->hw_addr, ntohs(hw->hw_addrlen));
151 attr = tb[NFULA_PAYLOAD];
158 attr = tb[NFULA_PREFIX];
160 err = nfnl_log_msg_set_prefix(msg,
nla_data(attr));
165 attr = tb[NFULA_UID];
167 nfnl_log_msg_set_uid(msg, ntohl(
nla_get_u32(attr)));
169 attr = tb[NFULA_GID];
171 nfnl_log_msg_set_gid(msg, ntohl(
nla_get_u32(attr)));
173 attr = tb[NFULA_SEQ];
175 nfnl_log_msg_set_seq(msg, ntohl(
nla_get_u32(attr)));
177 attr = tb[NFULA_SEQ_GLOBAL];
179 nfnl_log_msg_set_seq_global(msg, ntohl(
nla_get_u32(attr)));
181 attr = tb[NFULA_HWTYPE];
183 nfnl_log_msg_set_hwtype(msg, ntohs(
nla_get_u16(attr)));
185 attr = tb[NFULA_HWLEN];
187 nfnl_log_msg_set_hwlen(msg, ntohs(
nla_get_u16(attr)));
189 attr = tb[NFULA_HWHEADER];
193 attr = tb[NFULA_VLAN];
195 err = nfnlmsg_log_msg_parse_vlan(attr, msg);
203 err = nfnlmsg_ct_parse_nested(attr, &ct);
206 nfnl_log_msg_set_ct(msg, ct);
210 attr = tb[NFULA_CT_INFO];
212 nfnl_log_msg_set_ct_info(msg, ntohl(
nla_get_u32(attr)));
218 nfnl_log_msg_put(msg);
222static int log_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
223 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
228 if ((err = nfnlmsg_log_msg_parse(nlh, &msg)) < 0)
231 err = pp->pp_cb((
struct nl_object *) msg, pp);
232 nfnl_log_msg_put(msg);
238#define NFNLMSG_LOG_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_ULOG, (type))
239static struct nl_cache_ops nfnl_log_msg_ops = {
240 .co_name =
"netfilter/log_msg",
241 .co_hdrsize = NFNL_HDRLEN,
243 { NFNLMSG_LOG_TYPE(NFULNL_MSG_PACKET), NL_ACT_NEW,
"new" },
244 END_OF_MSGTYPES_LIST,
246 .co_protocol = NETLINK_NETFILTER,
247 .co_msg_parser = log_msg_parser,
248 .co_obj_ops = &log_msg_obj_ops,
251static void _nl_init log_msg_init(
void)
256static void _nl_exit log_msg_exit(
void)
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.
void * nla_data(const struct nlattr *nla)
Return pointer to the payload section.
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_len(const struct nlattr *nla)
Return length of the payload .
@ 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 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.
Attribute validation policy.
uint16_t minlen
Minimal length of payload required.
uint16_t type
Type of attribute or NLA_UNSPEC.