9#include <libnfnetlink/libnfnetlink.h>
10#include <libnetfilter_log/libnetfilter_log.h>
11#include <libnetfilter_log/libipulog.h>
14#define PAYLOAD_SIZE 0xffff
19 struct nflog_handle *nfulh;
20 struct nflog_g_handle *nful_gh;
21 struct nlmsghdr *last_nlh;
25 struct sockaddr_nl local;
26 struct sockaddr_nl peer;
28 struct ulog_packet_msg upmsg;
31static const struct ipulog_errmap_t
37 { IPULOG_ERR_NONE,
"No error" },
38 { IPULOG_ERR_IMPL,
"Not implemented yet" },
39 { IPULOG_ERR_HANDLE,
"Unable to create netlink handle" },
40 { IPULOG_ERR_SOCKET,
"Unable to create netlink socket" },
41 { IPULOG_ERR_BIND,
"Unable to bind netlink socket" },
42 { IPULOG_ERR_RECVBUF,
"Receive buffer size invalid" },
43 { IPULOG_ERR_RECV,
"Error during netlink receive" },
44 { IPULOG_ERR_NLEOF,
"Received EOF on netlink socket" },
45 { IPULOG_ERR_TRUNC,
"Receive message truncated" },
46 { IPULOG_ERR_INVGR,
"Invalid group specified" },
47 { IPULOG_ERR_INVNL,
"Invalid netlink message" },
51static unsigned int gmask2group(
unsigned int gmask)
55 for (bit =
sizeof(gmask)*4 -1; bit >= 0; bit--) {
56 if (gmask & (1 << bit))
66int ipulog_errno = IPULOG_ERR_NONE;
68const char *ipulog_strerror(
int errcode)
70 if (errcode < 0 || errcode > IPULOG_MAXERR)
71 errcode = IPULOG_ERR_IMPL;
72 return ipulog_errmap[errcode].message;
76uint32_t ipulog_group2gmask(uint32_t group)
78 if (group < 1 || group > 32)
80 ipulog_errno = IPULOG_ERR_INVGR;
83 return (1 << (group - 1));
87struct ipulog_handle *ipulog_create_handle(uint32_t gmask,
91 struct ipulog_handle *h;
92 unsigned int group = gmask2group(gmask);
94 h = calloc(1,
sizeof(*h)+PAYLOAD_SIZE);
96 ipulog_errno = IPULOG_ERR_HANDLE;
105 if (rv < 0 && rv != -EEXIST)
115 ipulog_errno = IPULOG_ERR_HANDLE;
120void ipulog_destroy_handle(
struct ipulog_handle *h)
127ulog_packet_msg_t *ipulog_get_packet(
struct ipulog_handle *h,
128 const unsigned char *buf,
131 struct nlmsghdr *nlh;
132 struct nfattr *tb[NFULA_MAX];
133 struct nfulnl_msg_packet_hdr *hdr;
137 nlh = nfnl_get_msg_first(nflog_nfnlh(h->nfulh), buf, len);
139next_msg: printf(
"next\n");
140 nlh = nfnl_get_msg_next(nflog_nfnlh(h->nfulh), buf, len);
147 nfnl_parse_attr(tb, NFULA_MAX, NFM_NFA(NLMSG_DATA(nlh)),
150 if (!tb[NFULA_PACKET_HDR-1])
154 hdr = NFA_DATA(tb[NFULA_PACKET_HDR-1]);
155 h->upmsg.hook = hdr->hook;
157 if (tb[NFULA_MARK-1])
158 h->upmsg.mark = ntohl(*(uint32_t *)NFA_DATA(tb[NFULA_MARK-1]));
162 if (tb[NFULA_TIMESTAMP-1]) {
163 struct nfulnl_msg_packet_timestamp *ts;
164 ts = NFA_DATA(tb[NFULA_TIMESTAMP-1]);
166 h->upmsg.timestamp_sec = __be64_to_cpu(ts->sec);
167 h->upmsg.timestamp_usec = __be64_to_cpu(ts->usec);
169 h->upmsg.timestamp_sec = h->upmsg.timestamp_usec = 0;
171 if (tb[NFULA_IFINDEX_INDEV-1]) {
172 void *indev_ptr = NFA_DATA(tb[NFULA_IFINDEX_INDEV-1]);
173 uint32_t indev_idx = ntohl(*(uint32_t *)indev_ptr);
175 if (!if_indextoname(indev_idx, h->upmsg.indev_name))
176 h->upmsg.indev_name[0] =
'\0';
178 h->upmsg.indev_name[0] =
'\0';
180 if (tb[NFULA_IFINDEX_OUTDEV-1]) {
181 void *outdev_ptr = NFA_DATA(tb[NFULA_IFINDEX_OUTDEV-1]);
182 uint32_t outdev_idx = ntohl(*(uint32_t *)outdev_ptr);
184 if (!if_indextoname(outdev_idx, h->upmsg.outdev_name))
185 h->upmsg.outdev_name[0] =
'\0';
187 h->upmsg.outdev_name[0] =
'\0';
189 if (tb[NFULA_HWADDR-1]) {
190 struct nfulnl_msg_packet_hw *phw = NFA_DATA(tb[NFULA_HWADDR-1]);
191 h->upmsg.mac_len = ntohs(phw->hw_addrlen);
192 memcpy(h->upmsg.mac, phw->hw_addr, 8);
194 h->upmsg.mac_len = 0;
196 if (tb[NFULA_PREFIX-1]) {
197 int plen = NFA_PAYLOAD(tb[NFULA_PREFIX-1]);
198 if (ULOG_PREFIX_LEN < plen)
199 plen = ULOG_PREFIX_LEN;
200 memcpy(h->upmsg.prefix, NFA_DATA(tb[NFULA_PREFIX-1]), plen);
201 h->upmsg.prefix[ULOG_PREFIX_LEN-1] =
'\0';
204 if (tb[NFULA_PAYLOAD-1]) {
205 memcpy(h->upmsg.payload, NFA_DATA(tb[NFULA_PAYLOAD-1]),
206 NFA_PAYLOAD(tb[NFULA_PAYLOAD-1]));
207 h->upmsg.data_len = NFA_PAYLOAD(tb[NFULA_PAYLOAD-1]);
209 h->upmsg.data_len = 0;
214ssize_t ipulog_read(
struct ipulog_handle *h,
unsigned char *buf,
215 size_t len,
int timeout)
219 return nfnl_recv(nflog_nfnlh(h->nfulh), buf, len);
223void ipulog_perror(
const char *s)
228 fputs(
"ERROR", stderr);
230 fprintf(stderr,
": %s", ipulog_strerror(ipulog_errno));
232 fprintf(stderr,
": %s", strerror(errno));
struct nflog_handle * nflog_open(void)
int nflog_bind_pf(struct nflog_handle *h, uint16_t pf)
int nflog_close(struct nflog_handle *h)
struct nflog_g_handle * nflog_bind_group(struct nflog_handle *h, uint16_t num)
int nflog_unbind_group(struct nflog_g_handle *gh)