6 #include <libnetfilter_log/linux_nfnetlink_log.h>
8 #include <libmnl/libmnl.h>
9 #include <libnetfilter_log/libnetfilter_log.h>
12 #include <linux/netfilter/nf_conntrack_common.h>
13 #include <linux/netfilter/nf_conntrack_tuple_common.h>
14 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
18 static int print_ctinfo(
const struct nlattr *
const attr)
25 ctinfo = ntohl(mnl_attr_get_u32(attr));
26 printf(
" ip_conntrack_info:");
28 switch (CTINFO2DIR(ctinfo)) {
29 case IP_CT_DIR_ORIGINAL:
30 printf(
" ORIGINAL /");
36 printf(
" unknown dir: %d\n", CTINFO2DIR(ctinfo));
41 case IP_CT_ESTABLISHED:
42 case IP_CT_ESTABLISHED_REPLY:
43 printf(
" ESTABLISHED\n");
46 case IP_CT_RELATED_REPLY:
54 printf(
" unknown ctinfo: %d\n", ctinfo);
61 static int print_nfct(uint8_t family,
62 const struct nlattr *
const info_attr,
63 const struct nlattr *
const ct_attr)
66 struct nf_conntrack *ct = NULL;
68 if (info_attr != NULL)
69 print_ctinfo(info_attr);
80 if (nfct_payload_parse(mnl_attr_get_payload(ct_attr),
81 mnl_attr_get_payload_len(ct_attr),
83 perror(
"nfct_payload_parse");
88 nfct_snprintf(buf,
sizeof(buf), ct, 0, NFCT_O_DEFAULT, 0);
95 static int print_nfct(uint8_t family,
96 const struct nlattr *
const info_attr,
97 const struct nlattr *
const ct_attr)
103 static int log_cb(
const struct nlmsghdr *nlh,
void *data)
105 struct nlattr *attrs[NFULA_MAX + 1] = { NULL };
106 struct nfulnl_msg_packet_hdr *ph = NULL;
107 struct nfgenmsg *nfg;
108 const char *prefix = NULL;
114 if (ret != MNL_CB_OK)
117 nfg = mnl_nlmsg_get_payload(nlh);
119 if (attrs[NFULA_PACKET_HDR])
120 ph = mnl_attr_get_payload(attrs[NFULA_PACKET_HDR]);
121 if (attrs[NFULA_PREFIX])
122 prefix = mnl_attr_get_str(attrs[NFULA_PREFIX]);
123 if (attrs[NFULA_MARK])
124 mark = ntohl(mnl_attr_get_u32(attrs[NFULA_MARK]));
126 printf(
"log received (prefix=\"%s\" hw=0x%04x hook=%u mark=%u)\n",
127 prefix ? prefix :
"", ntohs(ph->hw_protocol), ph->hook,
131 NFLOG_OUTPUT_XML, NFLOG_XML_ALL);
134 printf(
"%s (ret=%d)\n", buf, ret);
136 print_nfct(nfg->nfgen_family, attrs[NFULA_CT_INFO], attrs[NFULA_CT]);
141 int main(
int argc,
char *argv[])
143 struct mnl_socket *nl;
144 char buf[MNL_SOCKET_BUFFER_SIZE];
145 struct nlmsghdr *nlh;
147 unsigned int portid, qnum;
150 printf(
"Usage: %s [queue_num]\n", argv[0]);
153 qnum = atoi(argv[1]);
155 nl = mnl_socket_open(NETLINK_NETFILTER);
157 perror(
"mnl_socket_open");
161 if (mnl_socket_bind(nl, 0, MNL_SOCKET_AUTOPID) < 0) {
162 perror(
"mnl_socket_bind");
165 portid = mnl_socket_get_portid(nl);
171 perror(
"nflog_attr_put_cfg_cmd");
175 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
176 perror(
"mnl_socket_sendto");
182 perror(
"nflog_attr_put_cfg_cmd");
186 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
187 perror(
"mnl_socket_sendto");
193 perror(
"nflog_attr_put_cfg_cmd");
197 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
198 perror(
"mnl_socket_sendto");
204 perror(
"nflog_attr_put_cfg_mode");
209 mnl_attr_put_u16(nlh, NFULA_CFG_FLAGS, htons(NFULNL_CFG_F_CONNTRACK));
212 if (mnl_socket_sendto(nl, nlh, nlh->nlmsg_len) < 0) {
213 perror(
"mnl_socket_sendto");
217 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
219 perror(
"mnl_socket_recvfrom");
223 ret = mnl_cb_run(buf, ret, 0, portid, log_cb, NULL);
225 perror(
"mnl_cb_run");
229 ret = mnl_socket_recvfrom(nl, buf,
sizeof(buf));
231 perror(
"mnl_socket_recvfrom");
236 mnl_socket_close(nl);
int nflog_nlmsg_snprintf(char *buf, size_t bufsiz, const struct nlmsghdr *nlh, struct nlattr **attr, enum nflog_output_type type, uint32_t flags)
int nflog_attr_put_cfg_cmd(struct nlmsghdr *nlh, uint8_t cmd)
int nflog_nlmsg_parse(const struct nlmsghdr *nlh, struct nlattr **attr)
struct nlmsghdr * nflog_nlmsg_put_header(char *buf, uint8_t type, uint8_t family, uint16_t qnum)
int nflog_attr_put_cfg_mode(struct nlmsghdr *nlh, uint8_t mode, uint32_t range)