22 #include <sys/types.h> 23 #include <linux/netfilter/nfnetlink_log.h> 25 #include <netlink-private/netlink.h> 26 #include <netlink/attr.h> 27 #include <netlink/netfilter/nfnl.h> 28 #include <netlink/netfilter/log_msg.h> 29 #include <netlink-private/utils.h> 31 static struct nla_policy log_msg_policy[NFULA_MAX+1] = {
32 [NFULA_PACKET_HDR] = {
33 .
minlen =
sizeof(
struct nfulnl_msg_packet_hdr)
35 [NFULA_MARK] = { .type =
NLA_U32 },
37 .minlen =
sizeof(
struct nfulnl_msg_packet_timestamp)
39 [NFULA_IFINDEX_INDEV] = { .type =
NLA_U32 },
40 [NFULA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
41 [NFULA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
42 [NFULA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
44 .minlen =
sizeof(
struct nfulnl_msg_packet_hw)
48 [NFULA_UID] = { .type =
NLA_U32 },
49 [NFULA_GID] = { .type =
NLA_U32 },
50 [NFULA_SEQ] = { .type =
NLA_U32 },
51 [NFULA_SEQ_GLOBAL] = { .type =
NLA_U32 },
54 int nfnlmsg_log_msg_parse(
struct nlmsghdr *nlh,
struct nfnl_log_msg **result)
56 struct nfnl_log_msg *msg;
57 struct nlattr *tb[NFULA_MAX+1];
61 msg = nfnl_log_msg_alloc();
65 msg->ce_msgtype = nlh->nlmsg_type;
67 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFULA_MAX,
74 attr = tb[NFULA_PACKET_HDR];
76 struct nfulnl_msg_packet_hdr *hdr =
nla_data(attr);
79 nfnl_log_msg_set_hwproto(msg, hdr->hw_protocol);
80 nfnl_log_msg_set_hook(msg, hdr->hook);
83 attr = tb[NFULA_MARK];
85 nfnl_log_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
87 attr = tb[NFULA_TIMESTAMP];
89 struct nfulnl_msg_packet_timestamp *timestamp =
nla_data(attr);
92 tv.tv_sec = ntohll(timestamp->sec);
93 tv.tv_usec = ntohll(timestamp->usec);
94 nfnl_log_msg_set_timestamp(msg, &tv);
97 attr = tb[NFULA_IFINDEX_INDEV];
99 nfnl_log_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
101 attr = tb[NFULA_IFINDEX_OUTDEV];
103 nfnl_log_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
105 attr = tb[NFULA_IFINDEX_PHYSINDEV];
107 nfnl_log_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
109 attr = tb[NFULA_IFINDEX_PHYSOUTDEV];
111 nfnl_log_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
113 attr = tb[NFULA_HWADDR];
115 struct nfulnl_msg_packet_hw *hw =
nla_data(attr);
117 nfnl_log_msg_set_hwaddr(msg, hw->hw_addr, ntohs(hw->hw_addrlen));
120 attr = tb[NFULA_PAYLOAD];
127 attr = tb[NFULA_PREFIX];
129 err = nfnl_log_msg_set_prefix(msg,
nla_data(attr));
134 attr = tb[NFULA_UID];
136 nfnl_log_msg_set_uid(msg, ntohl(
nla_get_u32(attr)));
138 attr = tb[NFULA_GID];
140 nfnl_log_msg_set_gid(msg, ntohl(
nla_get_u32(attr)));
142 attr = tb[NFULA_SEQ];
144 nfnl_log_msg_set_seq(msg, ntohl(
nla_get_u32(attr)));
146 attr = tb[NFULA_SEQ_GLOBAL];
148 nfnl_log_msg_set_seq_global(msg, ntohl(
nla_get_u32(attr)));
154 nfnl_log_msg_put(msg);
158 static int log_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
159 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
161 struct nfnl_log_msg *msg;
164 if ((err = nfnlmsg_log_msg_parse(nlh, &msg)) < 0)
167 err = pp->pp_cb((
struct nl_object *) msg, pp);
168 nfnl_log_msg_put(msg);
174 #define NFNLMSG_LOG_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_ULOG, (type)) 175 static struct nl_cache_ops nfnl_log_msg_ops = {
176 .co_name =
"netfilter/log_msg",
177 .co_hdrsize = NFNL_HDRLEN,
179 { NFNLMSG_LOG_TYPE(NFULNL_MSG_PACKET), NL_ACT_NEW,
"new" },
180 END_OF_MSGTYPES_LIST,
182 .co_protocol = NETLINK_NETFILTER,
183 .co_msg_parser = log_msg_parser,
184 .co_obj_ops = &log_msg_obj_ops,
187 static void __init log_msg_init(
void)
192 static void __exit log_msg_exit(
void)
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.
int nlmsg_parse(struct nlmsghdr *nlh, int hdrlen, struct nlattr *tb[], int maxtype, struct nla_policy *policy)
parse attributes of a netlink message
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.