2011-10-14 23:14:25 +02:00
|
|
|
"""
|
2015-05-16 09:26:41 +02:00
|
|
|
Bind to a Linux netfilter queue. Send packets to a user-specified callback
|
2011-10-14 23:14:25 +02:00
|
|
|
function.
|
|
|
|
|
|
|
|
Copyright: (c) 2011, Kerkhoff Technologies Inc.
|
|
|
|
License: MIT; see LICENSE.txt
|
|
|
|
"""
|
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
# Constants for module users
|
2016-06-28 05:52:13 +02:00
|
|
|
COPY_NONE = 0
|
|
|
|
COPY_META = 1
|
|
|
|
COPY_PACKET = 2
|
2011-05-12 22:45:14 +02:00
|
|
|
|
|
|
|
# Packet copying defaults
|
2015-08-06 22:26:40 +02:00
|
|
|
DEF DEFAULT_MAX_QUEUELEN = 1024
|
2011-05-12 22:45:14 +02:00
|
|
|
DEF MaxPacketSize = 0xFFFF
|
|
|
|
DEF BufferSize = 4096
|
|
|
|
DEF MetadataSize = 80
|
|
|
|
DEF MaxCopySize = BufferSize - MetadataSize
|
2015-08-06 22:26:40 +02:00
|
|
|
# Experimentally determined overhead
|
|
|
|
DEF SockOverhead = 760+20
|
|
|
|
DEF SockCopySize = MaxCopySize + SockOverhead
|
|
|
|
# Socket queue should hold max number of packets of copysize bytes
|
2022-01-12 05:28:21 +01:00
|
|
|
DEF SockRcvSize = DEFAULT_MAX_QUEUELEN * SockCopySize // 2
|
|
|
|
|
2022-01-13 11:14:43 +01:00
|
|
|
from cpython.exc cimport PyErr_CheckSignals
|
|
|
|
|
2022-01-14 20:58:56 +01:00
|
|
|
cdef extern from "Python.h":
|
|
|
|
ctypedef struct PyTypeObject:
|
|
|
|
const char* tp_name
|
|
|
|
|
2022-01-13 11:14:43 +01:00
|
|
|
# A negative return value from this callback will stop processing and
|
|
|
|
# make nfq_handle_packet return -1, so we use that as the error flag.
|
2015-05-16 09:26:41 +02:00
|
|
|
cdef int global_callback(nfq_q_handle *qh, nfgenmsg *nfmsg,
|
2022-01-13 11:14:43 +01:00
|
|
|
nfq_data *nfa, void *data) except -1 with gil:
|
2011-10-14 23:14:25 +02:00
|
|
|
"""Create a Packet and pass it to appropriate callback."""
|
|
|
|
cdef NetfilterQueue nfqueue = <NetfilterQueue>data
|
|
|
|
cdef object user_callback = <object>nfqueue.user_callback
|
2022-01-14 01:09:51 +01:00
|
|
|
if user_callback is None:
|
|
|
|
# Queue is being unbound; we can't send a verdict at this point
|
|
|
|
# so just ignore the packet. The kernel will drop it once we
|
|
|
|
# unbind.
|
|
|
|
return 1
|
2011-05-13 17:42:05 +02:00
|
|
|
packet = Packet()
|
2022-01-13 11:14:43 +01:00
|
|
|
packet.set_nfq_data(nfqueue, nfa)
|
|
|
|
try:
|
|
|
|
user_callback(packet)
|
|
|
|
finally:
|
|
|
|
packet.drop_refs()
|
2011-05-12 22:45:14 +02:00
|
|
|
return 1
|
|
|
|
|
2011-05-13 17:42:05 +02:00
|
|
|
cdef class Packet:
|
2011-10-14 23:14:25 +02:00
|
|
|
"""A packet received from NetfilterQueue."""
|
2011-05-12 22:45:14 +02:00
|
|
|
def __cinit__(self):
|
|
|
|
self._verdict_is_set = False
|
|
|
|
self._mark_is_set = False
|
2012-12-24 04:17:54 +01:00
|
|
|
self._given_payload = None
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
def __str__(self):
|
2022-04-19 17:01:36 +02:00
|
|
|
cdef unsigned char *payload = NULL
|
|
|
|
if self._owned_payload:
|
|
|
|
payload = self._owned_payload
|
|
|
|
elif self.payload != NULL:
|
|
|
|
payload = self.payload
|
|
|
|
else:
|
|
|
|
return "%d byte packet, contents unretained" % (self.payload_len,)
|
|
|
|
|
|
|
|
cdef iphdr *hdr = <iphdr*>payload
|
2011-05-13 17:42:05 +02:00
|
|
|
protocol = PROTOCOLS.get(hdr.protocol, "Unknown protocol")
|
2011-05-12 22:45:14 +02:00
|
|
|
return "%s packet, %s bytes" % (protocol, self.payload_len)
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2022-01-13 11:14:43 +01:00
|
|
|
cdef set_nfq_data(self, NetfilterQueue queue, nfq_data *nfa):
|
2011-05-12 22:45:14 +02:00
|
|
|
"""
|
2015-05-16 09:26:41 +02:00
|
|
|
Assign a packet from NFQ to this object. Parse the header and load
|
2011-05-12 22:45:14 +02:00
|
|
|
local values.
|
|
|
|
"""
|
2022-01-12 05:28:21 +01:00
|
|
|
cdef nfqnl_msg_packet_hw *hw
|
|
|
|
cdef nfqnl_msg_packet_hdr *hdr
|
|
|
|
|
|
|
|
hdr = nfq_get_msg_packet_hdr(nfa)
|
2022-01-13 11:14:43 +01:00
|
|
|
self._queue = queue
|
2022-01-12 05:28:21 +01:00
|
|
|
self.id = ntohl(hdr.packet_id)
|
|
|
|
self.hw_protocol = ntohs(hdr.hw_protocol)
|
|
|
|
self.hook = hdr.hook
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2022-01-12 05:28:21 +01:00
|
|
|
hw = nfq_get_packet_hw(nfa)
|
|
|
|
if hw == NULL:
|
|
|
|
# nfq_get_packet_hw doesn't work on OUTPUT and PREROUTING chains
|
|
|
|
self._hwaddr_is_set = False
|
|
|
|
else:
|
|
|
|
self.hw_addr = hw.hw_addr
|
|
|
|
self._hwaddr_is_set = True
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2022-01-12 05:28:21 +01:00
|
|
|
self.payload_len = nfq_get_payload(nfa, &self.payload)
|
2011-05-12 22:45:14 +02:00
|
|
|
if self.payload_len < 0:
|
2022-01-14 01:19:28 +01:00
|
|
|
# Probably using a mode that doesn't provide the payload
|
|
|
|
self.payload = NULL
|
|
|
|
self.payload_len = 0
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2022-01-12 05:28:21 +01:00
|
|
|
nfq_get_timestamp(nfa, &self.timestamp)
|
2013-04-15 05:30:35 +02:00
|
|
|
self.mark = nfq_get_nfmark(nfa)
|
2023-03-01 08:42:20 +01:00
|
|
|
self.indev = nfq_get_indev(nfa)
|
|
|
|
self.outdev = nfq_get_outdev(nfa)
|
|
|
|
self.physindev = nfq_get_physindev(nfa)
|
|
|
|
self.physoutdev = nfq_get_physoutdev(nfa)
|
2011-05-12 22:45:14 +02:00
|
|
|
|
2022-01-12 05:28:21 +01:00
|
|
|
cdef drop_refs(self):
|
|
|
|
"""
|
|
|
|
Called at the end of the user_callback, when the storage passed to
|
2022-01-14 01:09:51 +01:00
|
|
|
set_nfq_data() is about to be reused.
|
2022-01-12 05:28:21 +01:00
|
|
|
"""
|
|
|
|
self.payload = NULL
|
|
|
|
|
2022-01-13 11:14:43 +01:00
|
|
|
cdef int verdict(self, u_int8_t verdict) except -1:
|
2011-05-12 22:45:14 +02:00
|
|
|
"""Call appropriate set_verdict... function on packet."""
|
2011-05-13 17:42:05 +02:00
|
|
|
if self._verdict_is_set:
|
2022-01-13 11:14:43 +01:00
|
|
|
raise RuntimeError("Verdict already given for this packet")
|
|
|
|
if self._queue.qh == NULL:
|
|
|
|
raise RuntimeError("Parent queue has already been unbound")
|
2012-12-24 04:17:54 +01:00
|
|
|
|
|
|
|
cdef u_int32_t modified_payload_len = 0
|
2015-05-16 09:26:41 +02:00
|
|
|
cdef unsigned char *modified_payload = NULL
|
2012-12-24 04:17:54 +01:00
|
|
|
if self._given_payload:
|
|
|
|
modified_payload_len = len(self._given_payload)
|
|
|
|
modified_payload = self._given_payload
|
2011-05-12 22:45:14 +02:00
|
|
|
if self._mark_is_set:
|
2016-06-28 06:27:12 +02:00
|
|
|
nfq_set_verdict2(
|
2022-01-13 11:14:43 +01:00
|
|
|
self._queue.qh,
|
2011-05-12 22:45:14 +02:00
|
|
|
self.id,
|
|
|
|
verdict,
|
2016-10-29 17:57:26 +02:00
|
|
|
self._given_mark,
|
2012-12-24 04:17:54 +01:00
|
|
|
modified_payload_len,
|
|
|
|
modified_payload)
|
2011-05-12 22:45:14 +02:00
|
|
|
else:
|
|
|
|
nfq_set_verdict(
|
2022-01-13 11:14:43 +01:00
|
|
|
self._queue.qh,
|
2011-05-12 22:45:14 +02:00
|
|
|
self.id,
|
|
|
|
verdict,
|
2012-12-24 04:17:54 +01:00
|
|
|
modified_payload_len,
|
|
|
|
modified_payload)
|
2011-05-12 22:45:14 +02:00
|
|
|
|
2011-05-13 17:42:05 +02:00
|
|
|
self._verdict_is_set = True
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2016-10-28 23:22:52 +02:00
|
|
|
def get_hw(self):
|
2022-01-14 01:09:51 +01:00
|
|
|
"""Return the packet's source MAC address as a Python bytestring, or
|
|
|
|
None if it's not available.
|
|
|
|
"""
|
|
|
|
if not self._hwaddr_is_set:
|
|
|
|
return None
|
2016-10-28 23:22:52 +02:00
|
|
|
cdef object py_string
|
2022-01-12 05:28:21 +01:00
|
|
|
py_string = PyBytes_FromStringAndSize(<char*>self.hw_addr, 8)
|
2016-10-28 23:22:52 +02:00
|
|
|
return py_string
|
|
|
|
|
2022-01-12 05:28:21 +01:00
|
|
|
cpdef bytes get_payload(self):
|
2011-05-13 17:42:05 +02:00
|
|
|
"""Return payload as Python string."""
|
2022-01-13 11:14:43 +01:00
|
|
|
if self._given_payload:
|
|
|
|
return self._given_payload
|
|
|
|
elif self._owned_payload:
|
2022-01-12 05:28:21 +01:00
|
|
|
return self._owned_payload
|
|
|
|
elif self.payload != NULL:
|
|
|
|
return self.payload[:self.payload_len]
|
2022-01-14 01:19:28 +01:00
|
|
|
elif self.payload_len == 0:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Packet has no payload -- perhaps you're using COPY_META mode?"
|
|
|
|
)
|
2022-01-12 05:28:21 +01:00
|
|
|
else:
|
|
|
|
raise RuntimeError(
|
|
|
|
"Payload data is no longer available. You must call "
|
|
|
|
"retain() within the user_callback in order to copy "
|
|
|
|
"the payload if you need to expect it after your "
|
|
|
|
"callback has returned."
|
|
|
|
)
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
cpdef Py_ssize_t get_payload_len(self):
|
|
|
|
return self.payload_len
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
cpdef double get_timestamp(self):
|
|
|
|
return self.timestamp.tv_sec + (self.timestamp.tv_usec/1000000.0)
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2012-12-24 04:17:54 +01:00
|
|
|
cpdef set_payload(self, bytes payload):
|
|
|
|
"""Set the new payload of this packet."""
|
|
|
|
self._given_payload = payload
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
cpdef set_mark(self, u_int32_t mark):
|
|
|
|
self._given_mark = mark
|
|
|
|
self._mark_is_set = True
|
2013-04-15 05:30:35 +02:00
|
|
|
|
|
|
|
cpdef get_mark(self):
|
|
|
|
if self._mark_is_set:
|
|
|
|
return self._given_mark
|
|
|
|
return self.mark
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2022-01-12 05:28:21 +01:00
|
|
|
cpdef retain(self):
|
|
|
|
self._owned_payload = self.get_payload()
|
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
cpdef accept(self):
|
|
|
|
"""Accept the packet."""
|
|
|
|
self.verdict(NF_ACCEPT)
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
cpdef drop(self):
|
|
|
|
"""Drop the packet."""
|
|
|
|
self.verdict(NF_DROP)
|
|
|
|
|
2013-04-03 13:55:51 +02:00
|
|
|
cpdef repeat(self):
|
|
|
|
"""Repeat the packet."""
|
|
|
|
self.verdict(NF_REPEAT)
|
|
|
|
|
2022-01-13 11:14:43 +01:00
|
|
|
|
2011-10-14 23:14:25 +02:00
|
|
|
cdef class NetfilterQueue:
|
2011-05-12 22:45:14 +02:00
|
|
|
"""Handle a single numbered queue."""
|
2022-01-14 03:06:48 +01:00
|
|
|
def __cinit__(self, *, u_int16_t af = PF_INET, int sockfd = -1):
|
|
|
|
cdef nfnl_handle *nlh = NULL
|
|
|
|
try:
|
|
|
|
if sockfd >= 0:
|
|
|
|
# This is a hack to use the given Netlink socket instead
|
|
|
|
# of the one allocated by nfq_open(). Intended use case:
|
|
|
|
# the given socket was opened in a different network
|
|
|
|
# namespace, and you want to monitor traffic in that
|
|
|
|
# namespace from this process running outside of it.
|
|
|
|
# Call socket(AF_NETLINK, SOCK_RAW, /*NETLINK_NETFILTER*/ 12)
|
|
|
|
# in the other namespace and pass that fd here (via Unix
|
|
|
|
# domain socket or similar).
|
|
|
|
nlh = nfnl_open()
|
|
|
|
if nlh == NULL:
|
|
|
|
raise OSError(errno, "Failed to open nfnetlink handle")
|
|
|
|
|
|
|
|
# At this point nfnl_get_fd(nlh) is a new netlink socket
|
|
|
|
# and has been bound to an automatically chosen port id.
|
|
|
|
# This dup2 will close it, freeing up that address.
|
|
|
|
if dup2(sockfd, nfnl_fd(nlh)) < 0:
|
|
|
|
raise OSError(errno, "dup2 failed")
|
|
|
|
|
|
|
|
# Opening the netfilterqueue subsystem will rebind
|
|
|
|
# the socket, using the same portid from the old socket,
|
|
|
|
# which is hopefully now free. An alternative approach,
|
|
|
|
# theoretically more robust against concurrent binds,
|
|
|
|
# would be to autobind the new socket and write the chosen
|
|
|
|
# address to nlh->local. nlh is an opaque type so this
|
|
|
|
# would need to be done using memcpy (local starts
|
|
|
|
# 4 bytes into the structure); let's avoid that unless
|
|
|
|
# we really need it.
|
|
|
|
self.h = nfq_open_nfnl(nlh)
|
|
|
|
else:
|
|
|
|
self.h = nfq_open()
|
|
|
|
if self.h == NULL:
|
|
|
|
raise OSError(errno, "Failed to open NFQueue.")
|
|
|
|
except:
|
|
|
|
if nlh != NULL:
|
|
|
|
nfnl_close(nlh)
|
|
|
|
raise
|
2011-05-12 22:45:14 +02:00
|
|
|
|
2022-01-13 11:14:43 +01:00
|
|
|
nfq_unbind_pf(self.h, af) # This does NOT kick out previous queues
|
|
|
|
if nfq_bind_pf(self.h, af) < 0:
|
|
|
|
raise OSError("Failed to bind family %s. Are you root?" % af)
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2022-01-14 01:09:51 +01:00
|
|
|
def __del__(self):
|
|
|
|
# unbind() can result in invocations of global_callback, so we
|
|
|
|
# must do it in __del__ (when this is still a valid
|
|
|
|
# NetfilterQueue object) rather than __dealloc__
|
2022-01-13 11:14:43 +01:00
|
|
|
self.unbind()
|
2022-01-14 01:09:51 +01:00
|
|
|
|
|
|
|
def __dealloc__(self):
|
2015-05-16 09:26:41 +02:00
|
|
|
# Don't call nfq_unbind_pf unless you want to disconnect any other
|
2011-05-12 22:45:14 +02:00
|
|
|
# processes using this libnetfilter_queue on this protocol family!
|
2022-01-14 01:09:51 +01:00
|
|
|
if self.h != NULL:
|
|
|
|
nfq_close(self.h)
|
2011-05-12 22:45:14 +02:00
|
|
|
|
2011-10-14 23:14:25 +02:00
|
|
|
def bind(self, int queue_num, object user_callback,
|
2015-05-16 09:26:41 +02:00
|
|
|
u_int32_t max_len=DEFAULT_MAX_QUEUELEN,
|
2011-10-14 23:14:25 +02:00
|
|
|
u_int8_t mode=NFQNL_COPY_PACKET,
|
2015-08-06 22:26:40 +02:00
|
|
|
u_int32_t range=MaxPacketSize,
|
|
|
|
u_int32_t sock_len=SockRcvSize):
|
2011-05-13 17:42:05 +02:00
|
|
|
"""Create and bind to a new queue."""
|
2022-01-12 06:01:53 +01:00
|
|
|
if self.qh != NULL:
|
|
|
|
raise RuntimeError("A queue is already bound; use unbind() first")
|
|
|
|
|
2015-08-06 22:26:40 +02:00
|
|
|
cdef unsigned int newsiz
|
2011-10-14 23:14:25 +02:00
|
|
|
self.user_callback = user_callback
|
|
|
|
self.qh = nfq_create_queue(self.h, queue_num,
|
|
|
|
<nfq_callback*>global_callback, <void*>self)
|
2011-05-12 22:45:14 +02:00
|
|
|
if self.qh == NULL:
|
|
|
|
raise OSError("Failed to create queue %s." % queue_num)
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
if range > MaxCopySize:
|
|
|
|
range = MaxCopySize
|
|
|
|
if nfq_set_mode(self.qh, mode, range) < 0:
|
2022-01-12 06:01:53 +01:00
|
|
|
self.unbind()
|
2011-05-12 22:45:14 +02:00
|
|
|
raise OSError("Failed to set packet copy mode.")
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2011-05-13 18:23:17 +02:00
|
|
|
nfq_set_queue_maxlen(self.qh, max_len)
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2022-01-12 05:45:58 +01:00
|
|
|
newsiz = nfnl_rcvbufsiz(nfq_nfnlh(self.h), sock_len)
|
|
|
|
if newsiz != sock_len * 2:
|
2022-01-12 06:01:53 +01:00
|
|
|
try:
|
2022-01-13 04:15:53 +01:00
|
|
|
import warnings
|
|
|
|
|
|
|
|
warnings.warn(
|
2022-01-12 06:01:53 +01:00
|
|
|
"Socket rcvbuf limit is now %d, requested %d." % (newsiz, sock_len),
|
|
|
|
category=RuntimeWarning,
|
|
|
|
)
|
|
|
|
except: # if warnings are being treated as errors
|
|
|
|
self.unbind()
|
|
|
|
raise
|
2022-01-12 05:28:21 +01:00
|
|
|
|
2011-05-12 22:45:14 +02:00
|
|
|
def unbind(self):
|
|
|
|
"""Destroy the queue."""
|
2022-01-14 01:09:51 +01:00
|
|
|
self.user_callback = None
|
2011-05-12 22:45:14 +02:00
|
|
|
if self.qh != NULL:
|
2022-01-14 01:09:51 +01:00
|
|
|
nfq_destroy_queue(self.qh)
|
2015-07-16 23:35:23 +02:00
|
|
|
self.qh = NULL
|
2011-05-12 22:45:14 +02:00
|
|
|
# See warning about nfq_unbind_pf in __dealloc__ above.
|
2015-05-16 09:26:41 +02:00
|
|
|
|
2015-11-17 23:07:50 +01:00
|
|
|
def get_fd(self):
|
|
|
|
"""Get the file descriptor of the queue handler."""
|
|
|
|
return nfq_fd(self.h)
|
|
|
|
|
|
|
|
def run(self, block=True):
|
2016-06-28 08:48:53 +02:00
|
|
|
"""Accept packets using recv."""
|
|
|
|
cdef int fd = self.get_fd()
|
2011-05-12 22:45:14 +02:00
|
|
|
cdef char buf[BufferSize]
|
|
|
|
cdef int rv
|
2015-11-17 23:07:50 +01:00
|
|
|
cdef int recv_flags
|
|
|
|
recv_flags = 0 if block else MSG_DONTWAIT
|
2016-06-28 05:46:50 +02:00
|
|
|
|
2015-08-06 22:26:40 +02:00
|
|
|
while True:
|
2011-05-12 22:45:14 +02:00
|
|
|
with nogil:
|
2015-11-17 23:07:50 +01:00
|
|
|
rv = recv(fd, buf, sizeof(buf), recv_flags)
|
2022-01-13 11:14:43 +01:00
|
|
|
if rv < 0:
|
|
|
|
if errno == EAGAIN:
|
2015-08-06 22:26:40 +02:00
|
|
|
break
|
2022-01-13 11:14:43 +01:00
|
|
|
if errno == ENOBUFS:
|
|
|
|
# Kernel is letting us know we dropped a packet
|
|
|
|
continue
|
|
|
|
if errno == EINTR:
|
|
|
|
PyErr_CheckSignals()
|
|
|
|
continue
|
|
|
|
raise OSError(errno, "recv failed")
|
2022-01-14 01:09:51 +01:00
|
|
|
nfq_handle_packet(self.h, buf, rv)
|
2011-05-12 22:45:14 +02:00
|
|
|
|
2016-06-28 08:48:53 +02:00
|
|
|
def run_socket(self, s):
|
|
|
|
"""Accept packets using socket.recv so that, for example, gevent can monkeypatch it."""
|
2022-01-13 04:15:53 +01:00
|
|
|
import socket
|
|
|
|
|
2016-06-28 09:13:40 +02:00
|
|
|
while True:
|
|
|
|
try:
|
2016-06-28 08:48:53 +02:00
|
|
|
buf = s.recv(BufferSize)
|
2016-06-28 09:13:40 +02:00
|
|
|
except socket.error as e:
|
|
|
|
err = e.args[0]
|
|
|
|
if err == ENOBUFS:
|
|
|
|
continue
|
|
|
|
elif err == EAGAIN or err == EWOULDBLOCK:
|
|
|
|
# This should only happen with a non-blocking socket, and the
|
|
|
|
# app should call run_socket again when more data is available.
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
# This is bad. Let the caller handle it.
|
|
|
|
raise e
|
2022-01-14 01:09:51 +01:00
|
|
|
else:
|
|
|
|
nfq_handle_packet(self.h, buf, len(buf))
|
2016-06-28 08:48:53 +02:00
|
|
|
|
2022-01-14 20:58:56 +01:00
|
|
|
cdef void _fix_names():
|
|
|
|
# Avoid ._impl showing up in reprs. This doesn't work on PyPy; there we would
|
|
|
|
# need to modify the name before PyType_Ready(), but I can't find any way to
|
|
|
|
# write Cython code that would execute at that time.
|
|
|
|
cdef PyTypeObject* tp = <PyTypeObject*>Packet
|
|
|
|
tp.tp_name = "netfilterqueue.Packet"
|
|
|
|
tp = <PyTypeObject*>NetfilterQueue
|
|
|
|
tp.tp_name = "netfilterqueue.NetfilterQueue"
|
|
|
|
|
|
|
|
_fix_names()
|
|
|
|
|
2011-05-13 17:42:05 +02:00
|
|
|
PROTOCOLS = {
|
|
|
|
0: "HOPOPT",
|
|
|
|
1: "ICMP",
|
|
|
|
2: "IGMP",
|
|
|
|
3: "GGP",
|
|
|
|
4: "IP",
|
|
|
|
5: "ST",
|
|
|
|
6: "TCP",
|
|
|
|
7: "CBT",
|
|
|
|
8: "EGP",
|
|
|
|
9: "IGP",
|
|
|
|
10: "BBN-RCC-MON",
|
|
|
|
11: "NVP-II",
|
|
|
|
12: "PUP",
|
|
|
|
13: "ARGUS",
|
|
|
|
14: "EMCON",
|
|
|
|
15: "XNET",
|
|
|
|
16: "CHAOS",
|
|
|
|
17: "UDP",
|
|
|
|
18: "MUX",
|
|
|
|
19: "DCN-MEAS",
|
|
|
|
20: "HMP",
|
|
|
|
21: "PRM",
|
|
|
|
22: "XNS-IDP",
|
|
|
|
23: "TRUNK-1",
|
|
|
|
24: "TRUNK-2",
|
|
|
|
25: "LEAF-1",
|
|
|
|
26: "LEAF-2",
|
|
|
|
27: "RDP",
|
|
|
|
28: "IRTP",
|
|
|
|
29: "ISO-TP4",
|
|
|
|
30: "NETBLT",
|
|
|
|
31: "MFE-NSP",
|
|
|
|
32: "MERIT-INP",
|
|
|
|
33: "DCCP",
|
|
|
|
34: "3PC",
|
|
|
|
35: "IDPR",
|
|
|
|
36: "XTP",
|
|
|
|
37: "DDP",
|
|
|
|
38: "IDPR-CMTP",
|
|
|
|
39: "TP++",
|
|
|
|
40: "IL",
|
|
|
|
41: "IPv6",
|
|
|
|
42: "SDRP",
|
|
|
|
43: "IPv6-Route",
|
|
|
|
44: "IPv6-Frag",
|
|
|
|
45: "IDRP",
|
|
|
|
46: "RSVP",
|
|
|
|
47: "GRE",
|
|
|
|
48: "DSR",
|
|
|
|
49: "BNA",
|
|
|
|
50: "ESP",
|
|
|
|
51: "AH",
|
|
|
|
52: "I-NLSP",
|
|
|
|
53: "SWIPE",
|
|
|
|
54: "NARP",
|
|
|
|
55: "MOBILE",
|
|
|
|
56: "TLSP",
|
|
|
|
57: "SKIP",
|
|
|
|
58: "IPv6-ICMP",
|
|
|
|
59: "IPv6-NoNxt",
|
|
|
|
60: "IPv6-Opts",
|
|
|
|
61: "any host internal protocol",
|
|
|
|
62: "CFTP",
|
|
|
|
63: "any local network",
|
|
|
|
64: "SAT-EXPAK",
|
|
|
|
65: "KRYPTOLAN",
|
|
|
|
66: "RVD",
|
|
|
|
67: "IPPC",
|
|
|
|
68: "any distributed file system",
|
|
|
|
69: "SAT-MON",
|
|
|
|
70: "VISA",
|
|
|
|
71: "IPCV",
|
|
|
|
72: "CPNX",
|
|
|
|
73: "CPHB",
|
|
|
|
74: "WSN",
|
|
|
|
75: "PVP",
|
|
|
|
76: "BR-SAT-MON",
|
|
|
|
77: "SUN-ND",
|
|
|
|
78: "WB-MON",
|
|
|
|
79: "WB-EXPAK",
|
|
|
|
80: "ISO-IP",
|
|
|
|
81: "VMTP",
|
|
|
|
82: "SECURE-VMTP",
|
|
|
|
83: "VINES",
|
|
|
|
84: "TTP",
|
|
|
|
85: "NSFNET-IGP",
|
|
|
|
86: "DGP",
|
|
|
|
87: "TCF",
|
|
|
|
88: "EIGRP",
|
|
|
|
89: "OSPFIGP",
|
|
|
|
90: "Sprite-RPC",
|
|
|
|
91: "LARP",
|
|
|
|
92: "MTP",
|
|
|
|
93: "AX.25",
|
|
|
|
94: "IPIP",
|
|
|
|
95: "MICP",
|
|
|
|
96: "SCC-SP",
|
|
|
|
97: "ETHERIP",
|
|
|
|
98: "ENCAP",
|
|
|
|
99: "any private encryption scheme",
|
|
|
|
100: "GMTP",
|
|
|
|
101: "IFMP",
|
|
|
|
102: "PNNI",
|
|
|
|
103: "PIM",
|
|
|
|
104: "ARIS",
|
|
|
|
105: "SCPS",
|
|
|
|
106: "QNX",
|
|
|
|
107: "A/N",
|
|
|
|
108: "IPComp",
|
|
|
|
109: "SNP",
|
|
|
|
110: "Compaq-Peer",
|
|
|
|
111: "IPX-in-IP",
|
|
|
|
112: "VRRP",
|
|
|
|
113: "PGM",
|
|
|
|
114: "any 0-hop protocol",
|
|
|
|
115: "L2TP",
|
|
|
|
116: "DDX",
|
|
|
|
117: "IATP",
|
|
|
|
118: "STP",
|
|
|
|
119: "SRP",
|
|
|
|
120: "UTI",
|
|
|
|
121: "SMP",
|
|
|
|
122: "SM",
|
|
|
|
123: "PTP",
|
|
|
|
124: "ISIS",
|
|
|
|
125: "FIRE",
|
|
|
|
126: "CRTP",
|
|
|
|
127: "CRUDP",
|
|
|
|
128: "SSCOPMCE",
|
|
|
|
129: "IPLT",
|
|
|
|
130: "SPS",
|
|
|
|
131: "PIPE",
|
|
|
|
132: "SCTP",
|
|
|
|
133: "FC",
|
|
|
|
134: "RSVP-E2E-IGNORE",
|
|
|
|
135: "Mobility",
|
|
|
|
136: "UDPLite",
|
|
|
|
137: "MPLS-in-IP",
|
|
|
|
138: "manet",
|
|
|
|
139: "HIP",
|
|
|
|
140: "Shim6",
|
|
|
|
255: "Reserved",
|
2011-10-14 23:14:25 +02:00
|
|
|
}
|