37 #ifdef NEED_STRERROR_H
45 #include <sys/socket.h>
46 #include <arpa/inet.h>
50 #include <netinet/in.h>
51 #include <linux/types.h>
53 #include <linux/netlink.h>
54 #include <linux/netfilter.h>
55 #include <linux/netfilter/nfnetlink.h>
56 #include <linux/netfilter/nfnetlink_log.h>
57 #include <linux/netfilter/nfnetlink_queue.h>
70 #define HDR_LENGTH (NLMSG_LENGTH(NLMSG_ALIGN(sizeof(struct nfgenmsg))))
72 #define NFLOG_IFACE "nflog"
73 #define NFQUEUE_IFACE "nfqueue"
92 register u_char *bp, *ep;
125 if (
errno == ENOBUFS)
127 }
while ((len == -1) && (
errno == EINTR ||
errno == ENOBUFS));
135 bp = (
unsigned char *)handle->
buffer;
140 const struct nlmsghdr *nlh = (
const struct nlmsghdr *) bp;
155 handle->
cc = (int)(ep - bp);
173 if ((
size_t)(ep - bp) < (
size_t)NLMSG_SPACE(0)) {
181 if (nlh->nlmsg_len <
sizeof(
struct nlmsghdr) || (u_int)len < nlh->nlmsg_len) {
186 if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_ULOG &&
187 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFULNL_MSG_PACKET)
189 else if (NFNL_SUBSYS_ID(nlh->nlmsg_type) == NFNL_SUBSYS_QUEUE &&
190 NFNL_MSG_TYPE(nlh->nlmsg_type) == NFQNL_MSG_PACKET)
194 const unsigned char *payload = NULL;
197 const struct nfgenmsg *nfg = NULL;
201 const struct nfattr *payload_attr = NULL;
208 nfg = NLMSG_DATA(nlh);
210 struct nfattr *attr = NFM_NFA(nfg);
211 int attr_len = nlh->nlmsg_len - NLMSG_ALIGN(
HDR_LENGTH);
213 while (NFA_OK(attr, attr_len)) {
215 switch (NFA_TYPE(attr)) {
216 case NFQA_PACKET_HDR:
218 const struct nfqnl_msg_packet_hdr *pkt_hdr = (
const struct nfqnl_msg_packet_hdr *) NFA_DATA(attr);
220 id = ntohl(pkt_hdr->packet_id);
228 }
else if (type ==
NFLOG) {
229 switch (NFA_TYPE(attr)) {
235 attr = NFA_NEXT(attr, attr_len);
240 payload = NFA_DATA(payload_attr);
241 pkth.
len = pkth.
caplen = NFA_PAYLOAD(payload_attr);
245 payload = NLMSG_DATA(nlh);
246 pkth.
caplen = pkth.
len = nlh->nlmsg_len-NLMSG_ALIGN(
sizeof(
struct nlmsghdr));
257 callback(user, &pkth, payload);
271 msg_len = NLMSG_ALIGN(nlh->nlmsg_len);
284 if (msg_len > (uint32_t)(ep - bp))
285 msg_len = (uint32_t)(ep - bp);
290 handle->
cc = (int)(ep - bp);
323 "Packet injection is not supported on netfilter devices");
336 char buf[1024] __attribute__ ((aligned));
337 memset(buf, 0,
sizeof(buf));
339 struct nlmsghdr *nlh = (
struct nlmsghdr *) buf;
340 struct nfgenmsg *nfg = (
struct nfgenmsg *) (buf +
sizeof(
struct nlmsghdr));
342 struct sockaddr_nl snl;
343 static unsigned int seq_id;
349 nlh->nlmsg_len = NLMSG_LENGTH(
sizeof(
struct nfgenmsg));
350 nlh->nlmsg_type = msg_type;
351 nlh->nlmsg_flags = NLM_F_REQUEST | (ack ? NLM_F_ACK : 0);
353 nlh->nlmsg_seq = seq_id;
355 nfg->nfgen_family = family;
356 nfg->version = NFNETLINK_V0;
357 nfg->res_id = htons(res_id);
360 struct nfattr *nfa = (
struct nfattr *) (buf + NLMSG_ALIGN(nlh->nlmsg_len));
363 nfa->nfa_len = NFA_LENGTH(mynfa->
nfa_len);
364 memcpy(NFA_DATA(nfa), mynfa->
data, mynfa->
nfa_len);
365 nlh->nlmsg_len = NLMSG_ALIGN(nlh->nlmsg_len) + NFA_ALIGN(nfa->nfa_len);
368 memset(&snl, 0,
sizeof(snl));
369 snl.nl_family = AF_NETLINK;
371 if (
sendto(handle->
fd, nlh, nlh->nlmsg_len, 0, (
struct sockaddr *) &snl,
sizeof(snl)) == -1)
388 len = (int)
recvfrom(handle->
fd, buf,
sizeof(buf), 0, (
struct sockaddr *) &snl, &addrlen);
389 }
while ((len == -1) && (
errno == EINTR));
394 if (addrlen !=
sizeof(snl) || snl.nl_family != AF_NETLINK) {
399 nlh = (
struct nlmsghdr *) buf;
400 if (snl.nl_pid != 0 || seq_id != nlh->nlmsg_seq)
403 while ((u_int)len >= NLMSG_SPACE(0) && NLMSG_OK(nlh, (u_int)len)) {
404 if (nlh->nlmsg_type == NLMSG_ERROR || (nlh->nlmsg_type == NLMSG_DONE && nlh->nlmsg_flags & NLM_F_MULTI)) {
405 if (nlh->nlmsg_len < NLMSG_ALIGN(
sizeof(
struct nlmsgerr))) {
409 errno = -(*((
int *)NLMSG_DATA(nlh)));
410 return (
errno == 0) ? 0 : -1;
412 nlh = NLMSG_NEXT(nlh, len);
428 struct nfulnl_msg_config_cmd msg;
443 struct nfulnl_msg_config_mode msg;
446 msg.copy_range = htonl(copy_range);
447 msg.copy_mode = copy_mode;
459 struct nfqnl_msg_verdict_hdr msg;
463 msg.verdict = htonl(verdict);
481 struct nfqnl_msg_config_cmd msg;
497 struct nfqnl_msg_config_params msg;
500 msg.copy_range = htonl(copy_range);
501 msg.copy_mode = copy_mode;
514 unsigned short groups[32];
528 if (type !=
OTHER && *dev ==
':') {
534 if (group_count == 32) {
536 "Maximum 32 netfilter groups! dev: %s",
541 group_id =
strtol(dev, &end_dev, 0);
542 if (end_dev != dev) {
543 if (group_id < 0 || group_id > 65535) {
545 "Netfilter group range from 0 to 65535 (got %ld)",
550 groups[group_count++] = (
unsigned short) group_id;
559 if (type ==
OTHER || *dev) {
561 "Can't get netfilter group(s) index from %s",
596 handle->
fd =
socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
597 if (handle->
fd < 0) {
599 errno,
"Can't create raw socket");
605 handle->
dlt_list = (u_int *) malloc(
sizeof(u_int) * 2);
618 errno,
"Can't allocate dump buffer");
626 "NFULNL_CFG_CMD_PF_UNBIND");
637 for (i = 0; i < group_count; i++) {
641 "Can't listen on group index");
648 "NFULNL_COPY_PACKET");
667 for (i = 0; i < group_count; i++) {
671 "Can't listen on group index");
678 "NFQNL_COPY_PACKET");
718 cp = strrchr(
device,
'/');
737 if (*cp !=
':' && *cp !=
'\0') {
759 sock =
socket(AF_NETLINK, SOCK_RAW, NETLINK_NETFILTER);
762 if (
errno == EAFNOSUPPORT ||
errno == EPROTONOSUPPORT)
765 errno,
"Can't open netlink socket");
776 "Linux netfilter log (NFLOG) interface", err_str) == NULL)
780 "Linux netfilter queue (NFQUEUE) interface", err_str) == NULL)
u_int pcap_filter(const struct bpf_insn *pc, const u_char *p, u_int wirelen, u_int buflen)
void pcap_fmt_errmsg_for_errno(char *errbuf, size_t errbuflen, int errnum, const char *fmt,...)
int install_bpf_program(pcap_t *p, struct bpf_program *fp)
int snprintf(char *, size_t, const char *,...)
int recvfrom(int, char *, u_int, int, struct sockaddr *, int *)
int gettimeofday(struct timeval *, struct timezone *)
long strtol(const char *, char **, int)
int recv(int, char *, u_int, int)
int setsockopt(int, int, int, char *, int)
int sendto(int, char *, u_int, int, struct sockaddr *, int)
int socket(int, int, int)
#define PACKET_COUNT_IS_UNLIMITED(count)
pcap_if_t * add_dev(pcap_if_list_t *, const char *, bpf_u_int32, const char *, char *)
void pcap_cleanup_live_common(pcap_t *)
int pcap_getnonblock_fd(pcap_t *)
int pcap_setnonblock_fd(pcap_t *p, int)
#define PCAP_CREATE_COMMON(ebuf, type)
static int nfqueue_send_verdict(const pcap_t *handle, uint16_t group_id, u_int32_t id, u_int32_t verdict)
static int netfilter_read_linux(pcap_t *handle, int max_packets, pcap_handler callback, u_char *user)
int netfilter_findalldevs(pcap_if_list_t *devlistp, char *err_str)
static int netfilter_stats_linux(pcap_t *handle, struct pcap_stat *stats)
static int nfqueue_send_config_mode(const pcap_t *handle, uint16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
static int netfilter_inject_linux(pcap_t *handle, const void *buf, int size)
static int nflog_send_config_msg(const pcap_t *handle, uint8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
static int nfqueue_send_config_cmd(const pcap_t *handle, uint16_t group_id, u_int8_t cmd, u_int16_t pf)
static int netfilter_set_datalink(pcap_t *handle, int dlt)
static int netfilter_send_config_msg(const pcap_t *handle, uint16_t msg_type, int ack, u_int8_t family, u_int16_t res_id, const struct my_nfattr *mynfa)
static int nflog_send_config_mode(const pcap_t *handle, uint16_t group_id, u_int8_t copy_mode, u_int32_t copy_range)
static int nflog_send_config_cmd(const pcap_t *handle, uint16_t group_id, u_int8_t cmd, u_int8_t family)
static int netfilter_activate(pcap_t *handle)
pcap_t * netfilter_create(const char *device, char *ebuf, int *is_ours)
static int nfqueue_send_config_msg(const pcap_t *handle, uint8_t family, u_int16_t group_id, const struct my_nfattr *mynfa)
#define PCAP_ERROR_RFMON_NOTSUP
#define PCAP_IF_CONNECTION_STATUS_NOT_APPLICABLE
void(* pcap_handler)(u_char *, const struct pcap_pkthdr *, const u_char *)
struct bpf_insn * bf_insns
activate_op_t activate_op
setnonblock_op_t setnonblock_op
setfilter_op_t setfilter_op
getnonblock_op_t getnonblock_op
setdirection_op_t setdirection_op
set_datalink_op_t set_datalink_op