Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
005f246
formatting changes, removed some py2 compatibility, added support for…
DOWRIGHTTV Aug 18, 2021
a34cf60
fixed function args.
DOWRIGHTTV Aug 18, 2021
27c1dbb
adding derivative works license.
DOWRIGHTTV Aug 19, 2021
05be56c
type adjustments and bug fixes.
DOWRIGHTTV Aug 19, 2021
5af79a5
type adjustments and bug fixes.
DOWRIGHTTV Aug 19, 2021
7fe1d0c
work on new packet
DOWRIGHTTV Aug 23, 2021
4d89c30
work on new packet
DOWRIGHTTV Aug 23, 2021
5d874fe
work on new packet
DOWRIGHTTV Aug 24, 2021
88268f4
work on new packet
DOWRIGHTTV Aug 24, 2021
3e37a8a
work on new packet
DOWRIGHTTV Aug 24, 2021
505f6da
work on new packet
DOWRIGHTTV Aug 24, 2021
5240903
work on new packet
DOWRIGHTTV Aug 24, 2021
35f6e27
work on new packet
DOWRIGHTTV Aug 24, 2021
7c9a98e
work on new packet
DOWRIGHTTV Aug 24, 2021
5a15325
work on new packet
DOWRIGHTTV Aug 24, 2021
d155639
work on new packet
DOWRIGHTTV Aug 24, 2021
f8aee62
work on new packet
DOWRIGHTTV Aug 24, 2021
027547b
work on new packet
DOWRIGHTTV Aug 24, 2021
3674db7
work on new packet
DOWRIGHTTV Aug 24, 2021
58b08eb
work on new packet
DOWRIGHTTV Aug 24, 2021
f0f68af
work on new packet
DOWRIGHTTV Aug 24, 2021
532f2a4
work on new packet
DOWRIGHTTV Aug 24, 2021
6e3815f
work on new packet
DOWRIGHTTV Aug 24, 2021
963067c
work on new packet
DOWRIGHTTV Aug 24, 2021
8628615
work on new packet
DOWRIGHTTV Aug 24, 2021
7a5c2dd
work on new packet
DOWRIGHTTV Aug 24, 2021
3d75c07
work on new packet
DOWRIGHTTV Aug 24, 2021
3db7673
work on new packet
DOWRIGHTTV Aug 24, 2021
658aea3
work on new packet
DOWRIGHTTV Aug 24, 2021
4a475e5
work on new packet
DOWRIGHTTV Aug 24, 2021
901b232
work on new packet
DOWRIGHTTV Aug 24, 2021
9f9107c
work on new packet
DOWRIGHTTV Aug 24, 2021
027c3f4
work on new packet
DOWRIGHTTV Aug 24, 2021
1956702
work on new packet
DOWRIGHTTV Aug 24, 2021
250a12c
work on new packet
DOWRIGHTTV Aug 24, 2021
b2c28ae
work on new packet
DOWRIGHTTV Aug 24, 2021
f4c9d84
work on new packet
DOWRIGHTTV Aug 24, 2021
291015f
work on new packet
DOWRIGHTTV Aug 24, 2021
0ac7dd5
work on new packet
DOWRIGHTTV Aug 24, 2021
4fb6eda
work on new packet
DOWRIGHTTV Aug 25, 2021
9f2a2a9
work on new packet
DOWRIGHTTV Aug 25, 2021
5d7cdd6
work on new packet
DOWRIGHTTV Aug 25, 2021
d210710
work on new packet
DOWRIGHTTV Aug 25, 2021
cb0a699
work on new packet
DOWRIGHTTV Aug 25, 2021
9cd786e
work on new packet
DOWRIGHTTV Aug 25, 2021
b11b341
work on new packet
DOWRIGHTTV Aug 25, 2021
fe96181
work on new packet
DOWRIGHTTV Aug 25, 2021
6d031c4
work on new packet
DOWRIGHTTV Aug 25, 2021
31ec7b4
work on new packet
DOWRIGHTTV Aug 25, 2021
f93cddc
work on new packet
DOWRIGHTTV Aug 25, 2021
6ce2f3f
work on new packet
DOWRIGHTTV Aug 25, 2021
4760a91
work on new packet
DOWRIGHTTV Aug 25, 2021
ba4ba98
work on new packet
DOWRIGHTTV Aug 25, 2021
292855d
work on new packet
DOWRIGHTTV Aug 25, 2021
e0b22a8
work on new packet
DOWRIGHTTV Aug 25, 2021
a10c49a
work on new packet
DOWRIGHTTV Aug 25, 2021
61f5373
work on new packet
DOWRIGHTTV Aug 25, 2021
53c7abe
work on new packet
DOWRIGHTTV Aug 25, 2021
892a6bc
work on new packet
DOWRIGHTTV Aug 25, 2021
75b70ed
work on new packet
DOWRIGHTTV Aug 25, 2021
57097b3
work on new packet
DOWRIGHTTV Aug 25, 2021
1c60298
work on new packet
DOWRIGHTTV Aug 25, 2021
5969c08
work on new packet
DOWRIGHTTV Aug 26, 2021
710d457
work on new packet
DOWRIGHTTV Aug 26, 2021
b250f70
work on new packet
DOWRIGHTTV Aug 27, 2021
38f494d
proto header work
DOWRIGHTTV Aug 29, 2021
ac7ce15
restructure and cleanup for potential release build.
DOWRIGHTTV Aug 29, 2021
cf87074
implementing ctuples.
DOWRIGHTTV Aug 31, 2021
8ce7aad
implementing ctuples.
DOWRIGHTTV Aug 31, 2021
6916cb9
implementing ctuples.
DOWRIGHTTV Aug 31, 2021
89321dc
implementing ctuples.
DOWRIGHTTV Aug 31, 2021
c811d16
implementing ctuples.
DOWRIGHTTV Aug 31, 2021
d10b154
module info update.
DOWRIGHTTV Apr 16, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
661 changes: 661 additions & 0 deletions DERIVATIVE-LICENSE.txt

Large diffs are not rendered by default.

219 changes: 219 additions & 0 deletions dnx-netfilterqueue.pxd
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
cdef extern from "sys/types.h":
ctypedef unsigned char u_int8_t
ctypedef unsigned short int u_int16_t
ctypedef unsigned int u_int32_t

cdef extern from "<errno.h>":
int errno

# dummy defines from asm-generic/errno.h:
cdef enum:
EAGAIN = 11 # Try again
EWOULDBLOCK = EAGAIN
ENOBUFS = 105 # No buffer space available

cdef extern from "netinet/ip.h":
struct ip_header:
u_int8_t tos
u_int16_t tot_len
u_int16_t id
u_int16_t frag_off
u_int8_t ttl
u_int8_t protocol
u_int16_t check
u_int32_t saddr
u_int32_t daddr

# Dummy defines from netinet/in.h:
cdef enum:
IPPROTO_IP = 0 # Dummy protocol for TCP.
IPPROTO_HOPOPTS = 0 # IPv6 Hop-by-Hop options.
IPPROTO_ICMP = 1 # Internet Control Message Protocol.
IPPROTO_IGMP = 2 # Internet Group Management Protocol. */
IPPROTO_IPIP = 4 # IPIP tunnels (older KA9Q tunnels use 94).
IPPROTO_TCP = 6 # Transmission Control Protocol.
IPPROTO_EGP = 8 # Exterior Gateway Protocol.
IPPROTO_PUP = 12 # PUP protocol.
IPPROTO_UDP = 17 # User Datagram Protocol.
IPPROTO_IDP = 22 # XNS IDP protocol.
IPPROTO_TP = 29 # SO Transport Protocol Class 4.
IPPROTO_IPV6 = 41 # IPv6 header.
IPPROTO_ROUTING = 43 # IPv6 routing header.
IPPROTO_FRAGMENT = 44 # IPv6 fragmentation header.
IPPROTO_RSVP = 46 # Reservation Protocol.
IPPROTO_GRE = 47 # General Routing Encapsulation.
IPPROTO_ESP = 50 # encapsulating security payload.
IPPROTO_AH = 51 # authentication header.
IPPROTO_ICMPV6 = 58 # ICMPv6.
IPPROTO_NONE = 59 # IPv6 no next header.
IPPROTO_DSTOPTS = 60 # IPv6 destination options.
IPPROTO_MTP = 92 # Multicast Transport Protocol.
IPPROTO_ENCAP = 98 # Encapsulation Header.
IPPROTO_PIM = 103 # Protocol Independent Multicast.
IPPROTO_COMP = 108 # Compression Header Protocol.
IPPROTO_SCTP = 132 # Stream Control Transmission Protocol.
IPPROTO_RAW = 255 # Raw IP packets.
IPPROTO_MAX

cdef extern from "Python.h":
object PyBytes_FromStringAndSize(char *s, Py_ssize_t len)
object PyString_FromStringAndSize(char *s, Py_ssize_t len)

cdef extern from "sys/time.h":
ctypedef long time_t
struct timeval:
time_t tv_sec
time_t tv_usec
struct timezone:
pass

cdef extern from "netinet/in.h":
u_int32_t ntohl (u_int32_t __netlong) nogil
u_int16_t ntohs (u_int16_t __netshort) nogil
u_int32_t htonl (u_int32_t __hostlong) nogil
u_int16_t htons (u_int16_t __hostshort) nogil

cdef extern from "libnfnetlink/linux_nfnetlink.h":
struct nfgenmsg:
u_int8_t nfgen_family
u_int8_t version
u_int16_t res_id

cdef extern from "libnfnetlink/libnfnetlink.h":
struct nfnl_handle:
pass
unsigned int nfnl_rcvbufsiz(nfnl_handle *h, unsigned int size)

cdef extern from "libnetfilter_queue/linux_nfnetlink_queue.h":
enum nfqnl_config_mode:
NFQNL_COPY_NONE
NFQNL_COPY_META
NFQNL_COPY_PACKET
struct nfqnl_msg_packet_hdr:
u_int32_t packet_id
u_int16_t hw_protocol
u_int8_t hook

cdef extern from "libnetfilter_queue/libnetfilter_queue.h":
struct nfq_handle:
pass
struct nfq_q_handle:
pass
struct nfq_data:
pass
struct nfqnl_msg_packet_hw:
u_int8_t hw_addr[8]

nfq_handle *nfq_open()
int nfq_close(nfq_handle *h)

int nfq_bind_pf(nfq_handle *h, u_int16_t pf)
int nfq_unbind_pf(nfq_handle *h, u_int16_t pf)
ctypedef int *nfq_callback(nfq_q_handle *gh, nfgenmsg *nfmsg,
nfq_data *nfad, void *data)
nfq_q_handle *nfq_create_queue(nfq_handle *h,
u_int16_t num,
nfq_callback *cb,
void *data)
int nfq_destroy_queue(nfq_q_handle *qh)

int nfq_handle_packet(nfq_handle *h, char *buf, int len)

int nfq_set_mode(nfq_q_handle *qh,
u_int8_t mode, unsigned int len)

q_set_queue_maxlen(nfq_q_handle *qh,
u_int32_t queuelen)

int nfq_set_verdict(nfq_q_handle *qh,
u_int32_t id,
u_int32_t verdict,
u_int32_t data_len,
unsigned char *buf) nogil

int nfq_set_verdict2(nfq_q_handle *qh,
u_int32_t id,
u_int32_t verdict,
u_int32_t mark,
u_int32_t datalen,
unsigned char *buf) nogil
int nfq_set_queue_maxlen(nfq_q_handle *qh, u_int32_t queuelen)

int nfq_fd(nfq_handle *h)
nfqnl_msg_packet_hdr *nfq_get_msg_packet_hdr(nfq_data *nfad)
int nfq_get_payload(nfq_data *nfad, unsigned char **data)
int nfq_get_timestamp(nfq_data *nfad, timeval *tv)
nfqnl_msg_packet_hw *nfq_get_packet_hw(nfq_data *nfad)
int nfq_get_nfmark (nfq_data *nfad)
u_int8_t nfq_get_indev(nfq_data *nfad)
u_int8_t nfq_get_outdev(nfq_data *nfad)
nfnl_handle *nfq_nfnlh(nfq_handle *h)

# Dummy defines from linux/socket.h:
cdef enum: # Protocol families, same as address families.
PF_INET = 2
PF_INET6 = 10

cdef extern from "sys/socket.h":
ssize_t recv(int __fd, void *__buf, size_t __n, int __flags) nogil
int MSG_DONTWAIT

# Dummy defines from linux/netfilter.h
cdef enum:
NF_DROP
NF_ACCEPT
NF_STOLEN
NF_QUEUE
NF_REPEAT
NF_STOP
NF_MAX_VERDICT = NF_STOP

cdef class Packet:
cdef nfq_q_handle *_qh
cdef nfq_data *_nfa
cdef nfqnl_msg_packet_hdr *_hdr
cdef nfqnl_msg_packet_hw *_hw
cdef bint _verdict_is_set # True if verdict has been issued, otherwise false
cdef u_int32_t _mark # Mark given to packet
cdef bytes _given_payload # New payload of packet, or null

# From NFQ packet header:
cdef readonly u_int32_t id
cdef readonly u_int16_t hw_protocol
cdef readonly u_int8_t hook
cdef readonly u_int32_t mark

# Packet details:
cdef Py_ssize_t payload_len
cdef readonly unsigned char *payload
cdef timeval timestamp
cdef u_int8_t hw_addr[8]

# TODO: implement these | likely not using in this manner.
#cdef u_int8_t hw_addr[8] # A eui64-formatted address?
#cdef readonly u_int32_t nfmark
#cdef readonly u_int32_t indev
#cdef readonly u_int32_t physindev
#cdef readonly u_int32_t outdev
#cdef readonly u_int32_t physoutdev

cdef set_nfq_data(self, nfq_q_handle *qh, nfq_data *nfa)
cdef void verdict(self, u_int32_t verdict)
cpdef get_inint(self, bint name=*)
cpdef get_outint(self, bint name=*)
cpdef update_mark(self, u_int32_t mark)
cpdef Py_ssize_t get_payload_len(self)
cpdef double get_timestamp(self)
cpdef set_payload(self, bytes payload)
cpdef accept(self)
cpdef drop(self)
cpdef forward(self, u_int16_t queue_num)
cpdef repeat(self)

cdef class NetfilterQueue:
cdef object user_callback # User callback
cdef nfq_handle *h # Handle to NFQueue library
cdef nfq_q_handle *qh # A handle to the queue
cdef u_int16_t af # Address family
cdef packet_copy_size # Amount of packet metadata + data copied to buffer

Loading