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>
31 #if __BYTE_ORDER == __BIG_ENDIAN
32 static uint64_t ntohll(uint64_t x)
36 #elif __BYTE_ORDER == __LITTLE_ENDIAN
37 static uint64_t ntohll(uint64_t x)
43 static struct nla_policy log_msg_policy[NFULA_MAX+1] = {
44 [NFULA_PACKET_HDR] = {
45 .
minlen =
sizeof(
struct nfulnl_msg_packet_hdr)
47 [NFULA_MARK] = { .type =
NLA_U32 },
49 .minlen =
sizeof(
struct nfulnl_msg_packet_timestamp)
51 [NFULA_IFINDEX_INDEV] = { .type =
NLA_U32 },
52 [NFULA_IFINDEX_OUTDEV] = { .type =
NLA_U32 },
53 [NFULA_IFINDEX_PHYSINDEV] = { .type =
NLA_U32 },
54 [NFULA_IFINDEX_PHYSOUTDEV] = { .type =
NLA_U32 },
56 .minlen =
sizeof(
struct nfulnl_msg_packet_hw)
60 [NFULA_UID] = { .type =
NLA_U32 },
61 [NFULA_GID] = { .type =
NLA_U32 },
62 [NFULA_SEQ] = { .type =
NLA_U32 },
63 [NFULA_SEQ_GLOBAL] = { .type =
NLA_U32 },
66 int nfnlmsg_log_msg_parse(
struct nlmsghdr *nlh,
struct nfnl_log_msg **result)
68 struct nfnl_log_msg *msg;
69 struct nlattr *tb[NFULA_MAX+1];
73 msg = nfnl_log_msg_alloc();
77 msg->ce_msgtype = nlh->nlmsg_type;
79 err =
nlmsg_parse(nlh,
sizeof(
struct nfgenmsg), tb, NFULA_MAX,
86 attr = tb[NFULA_PACKET_HDR];
88 struct nfulnl_msg_packet_hdr *hdr =
nla_data(attr);
91 nfnl_log_msg_set_hwproto(msg, hdr->hw_protocol);
92 nfnl_log_msg_set_hook(msg, hdr->hook);
95 attr = tb[NFULA_MARK];
97 nfnl_log_msg_set_mark(msg, ntohl(
nla_get_u32(attr)));
99 attr = tb[NFULA_TIMESTAMP];
101 struct nfulnl_msg_packet_timestamp *timestamp =
nla_data(attr);
104 tv.tv_sec = ntohll(timestamp->sec);
105 tv.tv_usec = ntohll(timestamp->usec);
106 nfnl_log_msg_set_timestamp(msg, &tv);
109 attr = tb[NFULA_IFINDEX_INDEV];
111 nfnl_log_msg_set_indev(msg, ntohl(
nla_get_u32(attr)));
113 attr = tb[NFULA_IFINDEX_OUTDEV];
115 nfnl_log_msg_set_outdev(msg, ntohl(
nla_get_u32(attr)));
117 attr = tb[NFULA_IFINDEX_PHYSINDEV];
119 nfnl_log_msg_set_physindev(msg, ntohl(
nla_get_u32(attr)));
121 attr = tb[NFULA_IFINDEX_PHYSOUTDEV];
123 nfnl_log_msg_set_physoutdev(msg, ntohl(
nla_get_u32(attr)));
125 attr = tb[NFULA_HWADDR];
127 struct nfulnl_msg_packet_hw *hw =
nla_data(attr);
129 nfnl_log_msg_set_hwaddr(msg, hw->hw_addr, ntohs(hw->hw_addrlen));
132 attr = tb[NFULA_PAYLOAD];
139 attr = tb[NFULA_PREFIX];
141 err = nfnl_log_msg_set_prefix(msg,
nla_data(attr));
146 attr = tb[NFULA_UID];
148 nfnl_log_msg_set_uid(msg, ntohl(
nla_get_u32(attr)));
150 attr = tb[NFULA_GID];
152 nfnl_log_msg_set_gid(msg, ntohl(
nla_get_u32(attr)));
154 attr = tb[NFULA_SEQ];
156 nfnl_log_msg_set_seq(msg, ntohl(
nla_get_u32(attr)));
158 attr = tb[NFULA_SEQ_GLOBAL];
160 nfnl_log_msg_set_seq_global(msg, ntohl(
nla_get_u32(attr)));
166 nfnl_log_msg_put(msg);
170 static int log_msg_parser(
struct nl_cache_ops *ops,
struct sockaddr_nl *who,
171 struct nlmsghdr *nlh,
struct nl_parser_param *pp)
173 struct nfnl_log_msg *msg;
176 if ((err = nfnlmsg_log_msg_parse(nlh, &msg)) < 0)
179 err = pp->pp_cb((
struct nl_object *) msg, pp);
181 nfnl_log_msg_put(msg);
187 #define NFNLMSG_LOG_TYPE(type) NFNLMSG_TYPE(NFNL_SUBSYS_ULOG, (type))
188 static struct nl_cache_ops nfnl_log_msg_ops = {
189 .co_name =
"netfilter/log_msg",
190 .co_hdrsize = NFNL_HDRLEN,
192 { NFNLMSG_LOG_TYPE(NFULNL_MSG_PACKET), NL_ACT_NEW,
"new" },
193 END_OF_MSGTYPES_LIST,
195 .co_protocol = NETLINK_NETFILTER,
196 .co_msg_parser = log_msg_parser,
197 .co_obj_ops = &log_msg_obj_ops,
200 static void __init log_msg_init(
void)
205 static void __exit log_msg_exit(
void)