33 #include <sys/ioctl.h>
34 #include <netinet/in.h>
35 #include <linux/types.h>
36 #include <linux/netfilter.h>
37 #include <libnetfilter_conntrack/libnetfilter_conntrack.h>
38 #include <libnetfilter_conntrack/libnetfilter_conntrack_tcp.h>
41 #include "include/private.h"
49 struct nfct_handle *nfct;
54 static void close_nfct(
void *data) {
57 nfct_close(nfct->nfct);
60 static int nfct_cb(
enum nf_conntrack_msg_type type,
struct nf_conntrack *ct,
void *data) {
63 nfct_snprintf(buf,
sizeof(buf), ct, NFCT_T_UNKNOWN, NFCT_O_DEFAULT, NFCT_OF_SHOW_LAYER3 | NFCT_OF_TIME | NFCT_OF_TIMESTAMP);
66 return (NFCT_CB_CONTINUE);
69 static struct nfct_struct *nf_ctrack_alloc(uint8_t subsys_id,
unsigned subscriptions) {
72 if (!(nfct =
objalloc((
sizeof *ctrack), close_nfct))) {
77 if (!(nfct->nfct = nfct_open(subsys_id, subscriptions))) {
82 if ((nfct->fd = nfct_fd(nfct->nfct)) < 0) {
91 if (!ctrack && !(ctrack = nf_ctrack_alloc(CONNTRACK, 0))) {
98 struct nf_conntrack *ct;
99 struct iphdr *ip = (
struct iphdr *)pkt;
100 union l4hdr *l4 = (
union l4hdr *)(pkt + (ip->ihl * 4));
102 if (!(ct = nfct_new())) {
107 nfct_set_attr_u8(ct, ATTR_L3PROTO, PF_INET);
108 nfct_set_attr_u32(ct, ATTR_IPV4_SRC, ip->saddr);
109 nfct_set_attr_u32(ct, ATTR_IPV4_DST, ip->daddr);
110 nfct_set_attr_u8(ct, ATTR_L4PROTO, ip->protocol);
111 switch(ip->protocol) {
113 nfct_set_attr_u16(ct, ATTR_PORT_SRC, l4->tcp.source);
114 nfct_set_attr_u16(ct, ATTR_PORT_DST, l4->tcp.dest);
117 nfct_set_attr_u16(ct, ATTR_PORT_SRC, l4->udp.source);
118 nfct_set_attr_u16(ct, ATTR_PORT_DST, l4->udp.dest);
121 nfct_set_attr_u8(ct, ATTR_ICMP_TYPE, l4->icmp.type);
122 nfct_set_attr_u8(ct, ATTR_ICMP_CODE, l4->icmp.code);
123 nfct_set_attr_u16(ct, ATTR_ICMP_ID, l4->icmp.un.echo.id);
134 struct nf_conntrack *ct;
147 if (nfct_query(ctrack->nfct, NFCT_Q_DESTROY, ct) < 0) {
160 extern uint8_t
nf_ctrack_nat(uint8_t *pkt, uint32_t addr, uint16_t port, uint8_t dnat) {
161 struct iphdr *ip = (
struct iphdr *)pkt;
162 struct nf_conntrack *ct;
174 nfct_setobjopt(ct, NFCT_SOPT_SETUP_REPLY);
176 nfct_set_attr_u32(ct, ATTR_TIMEOUT, 120);
177 nfct_set_attr_u32(ct, (dnat) ? ATTR_DNAT_IPV4 : ATTR_SNAT_IPV4, addr);
179 switch(ip->protocol) {
181 nfct_set_attr_u8(ct, ATTR_TCP_STATE, TCP_CONNTRACK_ESTABLISHED);
185 nfct_set_attr_u16(ct, (dnat) ? ATTR_DNAT_PORT : ATTR_SNAT_PORT, port);
191 if (nfct_query(ctrack->nfct, NFCT_Q_CREATE_UPDATE, ct) < 0) {
205 uint32_t family = PF_INET;
216 nfct_callback_register(ctrack->nfct, NFCT_T_ALL, nfct_cb, NULL);
217 nfct_query(ctrack->nfct, NFCT_Q_DUMP, &family);
218 nfct_callback_unregister(ctrack->nfct);
226 static void *nf_ctrack_trace_th(
void *data) {
228 fd_set rd_set, act_set;
233 nfct_callback_register(nfct->nfct, NFCT_T_ALL, nfct_cb, NULL);
236 FD_SET(nfct->fd, &rd_set);
237 fcntl(nfct->fd, F_SETFD, O_NONBLOCK);
238 ioctl(nfct->fd, FIONBIO, &opt);
244 selfd = select(nfct->fd + 1, &act_set, NULL, NULL, &tv);
247 if ((selfd < 0 && errno == EINTR) || (!selfd)) {
254 if (FD_ISSET(nfct->fd, &act_set)) {
255 nfct_catch(nfct->nfct);
265 if (!(nfct = nf_ctrack_alloc(CONNTRACK, NFCT_ALL_CT_GROUPS))) {
uint8_t nf_ctrack_delete(uint8_t *pkt)
#define testflag(obj, flag)
Atomically test a flag in the flags field of a referenced object.
struct nf_conntrack * nf_ctrack_buildct(uint8_t *pkt)
uint8_t nf_ctrack_init(void)
int objlock(void *data)
Lock the reference.
uint8_t nf_ctrack_nat(uint8_t *pkt, uint32_t addr, uint16_t port, uint8_t dnat)
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
void nf_ctrack_endtrace(struct nfct_struct *nfct)
#define setflag(obj, flag)
Atomically set a flag in the flags field of a referenced object.
DTS Application library API Include file.
struct thread_pvt * framework_mkthread(threadfunc, threadcleanup, threadsighandler, void *data, int flags)
create a thread result must be unreferenced
Return reference to thread this must be unreferenced.
void nf_ctrack_close(void)
int objunlock(void *data)
Unlock a reference.
struct nfct_struct * nf_ctrack_trace(void)
struct nfct_struct nfct_struct
Forward decleration of structure.
int framework_threadok(void)
let threads check there status.
void nf_ctrack_dump(void)
NF_CTRACK_FLAGS
Netfilter Ctrack Flags.
int objunref(void *data)
Drop reference held.