UNIXSOCK plugin (v3)
Hi,
Here is an updated version of the UNIXSOCK plugin.
Changes since last version:
- if the bufsize value (from conf) is 0, try to guess the buffer size using getsockopt(SOL_SOCKET, SO_RCVBUF ..)
- set default path to /var/run/ulogd/ulogd2.sock with mode 0600
- add new configuration options perms, owner and group to be able to set these parameters on the created socket
- change the input algorithm to make it more robust against junk data (which can happen if the client is desynchronized)
- fixed a few bugs
Description:
[1] add new input plugin UNISOCK, using a unix socket. This allows userspace applications to send packets to ulogd, for example to send packets offline. It uses a key-length-value protocol to handle optional fields or extensions.
[2] is a Perl script to load a PCAP file and send it to ulogd using the UNIXSOCK plugin.
Pierre
PATCH 1/2 Add new input plugin UNIXSOCK
This input plugins creates a unix socket which can be used to log packets. Scripts or applications can connect to the socket (only one client allowed per socket) and send data in a Key-Length-Value format (including the payload).
--- input/packet/Makefile.am | 5 +- input/packet/ulogd_inppkt_UNIXSOCK.c | 792 ++++++++++++++++++++++++++++++++++ ulogd.conf.in | 7 + 3 files changed, 803 insertions(+), 1 deletions(-) create mode 100644 input/packet/ulogd_inppkt_UNIXSOCK.c
input/packet/Makefile.am +4 -1
@@ -3,7 +3,7 @@ AM_CPPFLAGS = $(all_includes) -I$(top_srcdir)/include AM_CFLAGS=-fPIC -Wall LIBS= -pkglib_LTLIBRARIES = ulogd_inppkt_NFLOG.la ulogd_inppkt_ULOG.la +pkglib_LTLIBRARIES = ulogd_inppkt_NFLOG.la ulogd_inppkt_ULOG.la ulogd_inppkt_UNIXSOCK.la ulogd_inppkt_NFLOG_la_SOURCES = ulogd_inppkt_NFLOG.c ulogd_inppkt_NFLOG_la_LDFLAGS = -avoid-version -module $(LIBNETFILTER_LOG_LIBS)
@@ -12,3 +12,6 @@ ulogd_inppkt_NFLOG_la_CFLAGS = $(AM_CFLAGS) $(LIBNETFILTER_LOG_CFLAGS) ulogd_inppkt_ULOG_la_SOURCES = ulogd_inppkt_ULOG.c ulogd_inppkt_ULOG_la_LDFLAGS = -avoid-version -module ulogd_inppkt_ULOG_la_LIBADD = ../../libipulog/libipulog.la + +ulogd_inppkt_UNIXSOCK_la_SOURCES = ulogd_inppkt_UNIXSOCK.c +ulogd_inppkt_UNIXSOCK_la_LDFLAGS = -avoid-version -module
input/packet/ulogd_inppkt_UNIXSOCK.c new file +792 -0
792 lines not on this page · raw message
ulogd.conf.in +7 -0
@@ -27,6 +27,7 @@ loglevel=1 plugin="@libdir@/ulogd/ulogd_inppkt_NFLOG.so" #plugin="@libdir@/ulogd/ulogd_inppkt_ULOG.so" +#plugin="@libdir@/ulogd/ulogd_inppkt_UNIXSOCK.so" plugin="@libdir@/ulogd/ulogd_inpflow_NFCT.so" plugin="@libdir@/ulogd/ulogd_filter_IFINDEX.so" plugin="@libdir@/ulogd/ulogd_filter_IP2STR.so"
@@ -75,6 +76,9 @@ plugin="@libdir@/ulogd/ulogd_raw2packet_BASE.so" # this is a stack for logging packets to syslog after a collect via NFLOG #stack=log3:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,sys1:SYSLOG +# this is a stack for logging packets to syslog after a collect via NuFW +#stack=nuauth1:UNIXSOCK,base1:BASE,ip2str1:IP2STR,print1:PRINTPKT,sys1:SYSLOG + # this is a stack for flow-based logging to MySQL #stack=ct1:NFCT,ip2bin1:IP2BIN,mysql2:MYSQL
@@ -137,6 +141,9 @@ numeric_label=1 # you can label the log info based on the packet verdict nlgroup=1 #numeric_label=0 # optional argument +[nuauth1] +socket_path="/tmp/nuauth_ulogd2.sock" + [emu1] file="/var/log/ulogd_syslogemu.log" sync=1
Signature · 1 line
1.6.5.2
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
Quoted 6 lines · Pierre Chifflier
This input plugins creates a unix socket which can be used to log packets. Scripts or applications can connect to the socket (only one client allowed per socket) and send data in a Key-Length-Value format (including the payload).
+#define
UNIX_PATH_MAX108
This is nowhere used and can be removed. (Also since 108 may be wrong for a given arch.)
input/packet/ulogd_inppkt_UNIXSOCK.c line 316
Quoted 5 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +5
-0+#define unixpath_ce (x) (x->ces[0]) +#define bufsize_ce (x) (x->ces[1]) +#define perms_ce (x) (x->ces[2]) +#define owner_ce (x) (x->ces[3]) +#define group_ce (x) (x->ces[4])
x should be guarded with ((x)->ces[0])
Quoted 11 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +11
-0+static int handle_packet (struct ulogd_pluginstance *upi,char *packet_buf,u_int16_t total_len) +{ +char *data; +struct iphdr *ip; +struct ulogd_key *ret = upi->output.keys; +u_int8_t oob_family; +u_int16_t payload_len; +u_int16_t option_number; +u_int16_t option_length; +char *buf; +
input/packet/ulogd_inppkt_UNIXSOCK.c line 355
Quoted 10 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +10
-0+ulogd_log (ULOGD_DEBUG , +"ulogd2: handling packet\n" ); + + data = packet_buf +sizeof (u_int16_t); + + payload_len =ntohs (*(u_int16_t *)data); + data +=sizeof (u_int16_t); + + + ip = (struct iphdr *) data;
If data is not aligned, later dereferencing can fail :(
Quoted 9 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +9
-0+if (ip->version ==4 ) + oob_family =AF_INET ; +else if (ip->version ==6 ) + oob_family =AF_INET6 ; +else oob_family =0 ; + +okey_set_u8 (&ret[UNIXSOCK_KEY_OOB_FAMILY ], oob_family); + +okey_set_ptr (&ret[UNIXSOCK_KEY_RAW_PCKT ], ip);
input/packet/ulogd_inppkt_UNIXSOCK.c line 375
Quoted 10 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +10
-0+okey_set_u32 (&ret[UNIXSOCK_KEY_RAW_PCKTLEN ], payload_len); + +/* options */ +if (total_len > payload_len +sizeof (u_int16_t)) { + data = packet_buf + payload_len +2 *sizeof (u_int16_t); + total_len -=2 *sizeof (u_int16_t); + +while ( (data - packet_buf) < total_len) { + + option_number =ntohs (*(u_int16_t *)(data));
This can fail even if packet_buf _is_ aligned, all it takes is that payload_len is an odd number. Unfortunately, I see no way to easily address this without lame bitshifting :(
input/packet/ulogd_inppkt_UNIXSOCK.c line 480
Quoted 5 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +5
-0+static int _unix_socket_set_permissions (struct ulogd_pluginstance *upi) +{ +const char *socket_path; +char *owner =owner_ce (upi->config_kset).u.string; +char *group =group_ce (upi->config_kset).u.string;
const char *, I guess
input/packet/ulogd_inppkt_UNIXSOCK.c line 482
Quoted 2 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +2
-0+int uid =-1 ; +int gid =-1 ;
uit_t, gid_t.
input/packet/ulogd_inppkt_UNIXSOCK.c line 513
Quoted 1 line
input/packet/ulogd_inppkt_UNIXSOCK.c new file +1
-0+if (uid >=0 || gid >=0 ) {
Subsequently, uid != -1 || gid != -1. However, you could also ignore this check entirely since chown checks for -1 already.
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
Quoted 4 lines · Jan Engelhardt
Quoted 1 line
+#define
UNIX_PATH_MAX108This is nowhere used and can be removed. (Also since 108 may be wrong for a given arch.)
Yep, removed
Quoted 8 lines
input/packet/ulogd_inppkt_UNIXSOCK.c line 316
Quoted 6 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +5
-0+#define unixpath_ce (x) (x->ces[0]) +#define bufsize_ce (x) (x->ces[1]) +#define perms_ce (x) (x->ces[2]) +#define owner_ce (x) (x->ces[3]) +#define group_ce (x) (x->ces[4])x should be guarded with ((x)->ces[0])
True (in general case). A quick grep shows that all plugins are using the same defines and should be updated (45 matches) .. it would need a required patch.
Quoted 4 lines
Review
Quoted 2 lines
(quoted diff) +2
-0+ + ip = (struct iphdr *) data;If data is not aligned, later dereferencing can fail :(
I remember some arches (sparc, arm ?) will send a signal (SIGBUS I think) in this case.
How can I prevent data from being unaligned ?
Adding some __attribute__((aligned(4)) will be enough ?
Quoted 9 lines
input/packet/ulogd_inppkt_UNIXSOCK.c line 375
Quoted 4 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +4
-0+ +while ( (data - packet_buf) < total_len) { + + option_number =ntohs (*(u_int16_t *)(data));This can fail even if
packet_buf_is_ aligned, all it takes is thatpayload_lenis an odd number. Unfortunately, I see no way to easily address this without lame bitshifting :(
Yes, since this is a Key-Length-Value protocol it's hard to ensure there is no weird value. I tried to add checks (marker on start, etc.), but I see no easy way to address this;
Quoted 14 lines
input/packet/ulogd_inppkt_UNIXSOCK.c line 480
Quoted 6 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +5
-0+static int _unix_socket_set_permissions (struct ulogd_pluginstance *upi) +{ +const char *socket_path; +char *owner =owner_ce (upi->config_kset).u.string; +char *group =group_ce (upi->config_kset).u.string;const char *, I guess
input/packet/ulogd_inppkt_UNIXSOCK.c line 482
Quoted 2 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +2
-0+int uid =-1 ; +int gid =-1 ;
uit_t,gid_t.
Quoted 4 lines
input/packet/ulogd_inppkt_UNIXSOCK.c line 513
Quoted 1 line
input/packet/ulogd_inppkt_UNIXSOCK.c new file +1
-0+if (uid >=0 || gid >=0 ) {Subsequently, uid != -1 || gid != -1. However, you could also ignore this check entirely since chown checks for -1 already.
The check was here only to avoid a call if both are -1, but I agree it's useless.
Thanks,
Pierre
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
Quoted 8 lines · Pierre Chifflier
Quoted 3 lines
input/packet/ulogd_inppkt_UNIXSOCK.c line 355
Quoted 1 line
input/packet/ulogd_inppkt_UNIXSOCK.c new file +1
-0+ ip = (struct iphdr *) data;If data is not aligned, later dereferencing can fail :(
I remember some arches (sparc, arm ?) will send a signal (SIGBUS I think) in this case.
How can I prevent data from being unaligned ?
Adding some __attribute__((aligned(4)) will be enough ?
__attribute__(aligned) only specifies the minimum requirement.
What one would need is __attribute__((packed)), and it needs to be on struct iphdr - in /usr/include/... - and that is the problem because you cannot change it.
And gcc does not allow saying (ignored the attribute)
struct iphdr __attribute__((packed)) *iph = data;
So.. tough luck.
Quoted 10 lines
Quoted 9 lines
input/packet/ulogd_inppkt_UNIXSOCK.c line 375
Quoted 4 lines
input/packet/ulogd_inppkt_UNIXSOCK.c new file +4
-0+ +while ( (data - packet_buf) < total_len) { + + option_number =ntohs (*(u_int16_t *)(data));This can fail even if
packet_buf_is_ aligned, all it takes is thatpayload_lenis an odd number. Unfortunately, I see no way to easily address this without lame bitshifting :(
Quoted 3 lines
Yes, since this is a Key-Length-Value protocol it's hard to ensure there is no weird value. I tried to add checks (marker on start, etc.), but I see no easy way to address this;
+static inline uint32_t deref_get_32(const void *ptr) +{
+ uint32_t ret; + memcpy(&ret, ptr, sizeof(ret)); + return ret;
+}
Is what I use in one project; however, trying to minimize the unalignedness - iptables is a "perfect" example, and netlink may do the same tho I am not sure - seems to be the best approach in serialized streams.
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
Quoted 14 lines · Jan Engelhardt
Quoted 8 lines · Pierre Chifflier
Quoted 3 lines
input/packet/ulogd_inppkt_UNIXSOCK.c line 355
Quoted 1 line
input/packet/ulogd_inppkt_UNIXSOCK.c new file +1
-0+ ip = (struct iphdr *) data;If data is not aligned, later dereferencing can fail :(
I remember some arches (sparc, arm ?) will send a signal (SIGBUS I think) in this case.
How can I prevent data from being unaligned ?
Adding some __attribute__((aligned(4)) will be enough ?
__attribute__(aligned)only specifies the minimum requirement.
Quoted 8 lines
What one would need is __attribute__((packed)), and it needs to be on struct iphdr - in
/usr/include/... - and that is the problem because you cannot change it.
And gcc does not allow saying (ignored the attribute)struct iphdr __attribute__((packed)) *iph = data;So.. tough luck.
It seems gcc does not appreciate:
ulogd_inppkt_UNIXSOCK.c:352: warning: ‘packed’ attribute ignored
I can see the problem, but I can't see any solution without memcpy'ing the data several times :(
Generally speaking, what is the best way to deal with these kind of Key-Kength-Value protocols and alignment ?
Since I do not not the size of the elements and that most of them are strings, it's very unlikely the result is going to be aligned. Should I memcpy each element before using it ? Or does a better (in terms of performance) solution ?
Thanks,
Pierre
--
To unsubscribe from this list: send the line "unsubscribe netfilter-devel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
In reply to Jan EngelhardtFrom: Jan Engelhardt <[email protected]>
Date: Sun, 1 Nov 2009 14:41:21 +0100 (CET)
Quoted 11 lines
(quoted diff) +6
-0+static inline uint32_t deref_get_32(const void *ptr) +{ + uint32_t ret; + memcpy(&ret, ptr, sizeof(ret)); + return ret; +}Is what I use in one project; however, trying to minimize the unalignedness - iptables is a "perfect" example, and netlink may do the same tho I am not sure - seems to be the best approach in serialized streams.
This will not work all the time.
If GCC can see at the inlining point that 'ptr' is of a type that should be properly aligned it can inline the memcpy and still emit the very unaligned load or store that you're trying to avoid.
Using a void pointer doesn't create a sort of "barrier" for typing visibility like you think it does.
So I would argue against using this construct, because it only gives you a false sense of security.
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
Quoted 14 lines · David Miller
Quoted 11 lines
(quoted diff) +6
-0+static inline uint32_t deref_get_32(const void *ptr) +{ + uint32_t ret; + memcpy(&ret, ptr, sizeof(ret)); + return ret; +}Is what I use in one project; however, trying to minimize the unalignedness - iptables is a "perfect" example, and netlink may do the same tho I am not sure - seems to be the best approach in serialized streams.
This will not work all the time.
Quoted 3 lines
If GCC can see at the inlining point that 'ptr' is of a type that should be properly aligned it can inline the memcpy and still emit the very unaligned load or store that you're trying to avoid.
Hi Dave,
Indeed it seems to be this way. Hm, bad.
What codes are you using on your alignment-sensitive SPARCs - the standard bitshifting, "char *p; p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3]"?
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
Quoted 11 lines · Jan Engelhardt
Quoted 4 lines
If GCC can see at the inlining point that 'ptr' is of a type that should be properly aligned it can inline the memcpy and still emit the very unaligned load or store that you're trying to avoid.
Hi Dave,
Indeed it seems to be this way. Hm, bad.
What codes are you using on your alignment-sensitive SPARCs - the standard bitshifting, "char *p; p[0]<<24 | p[1]<<16 | p[2]<<8 | p[3]"?
Hi all,
I am not very familiar with these kind of problems (alignment). If I understand correctly, what I need to avoid is playing with pointers on a buffer, especially if trying to read a structure.
Since what I am trying to read is formatted as following:
marker (uint32)total_size (uint16)payload_length (uint16)
payload (struct iphdr)
Followed by a sequence ofoption_id (uint16)option_length (uint16)option_value (variable, equal to option_length)
Am I right to assume that:
- I can define a (packed) structure for the first part, and that I can set the pointer on the data buffer which was allocated by malloc without problems ? [*]
- For the second part, there are 2 cases:
+ integers must be read byte per byte
+ strings can be used directly ?
[*] none of the iphdr, ip or similar structures are defined as packed in netinet/ip.h
Thanks,
Pierre
Re: [PATCH 1/2] Add new input plugin UNIXSOCK
Quoted 10 lines · Pierre Chifflier
I am not very familiar with these kind of problems (alignment). If I understand correctly, what I need to avoid is playing with pointers on a buffer, especially if trying to read a structure.
Since what I am trying to read is formatted as following:
marker (uint32)
total_size(uint16)payload_length(uint16)
payload (struct iphdr)
This looks fine. Your input buffer should be suitably aligned by the allocator and these members are all well-aligned themselves.
Quoted 9 lines
Followed by a sequence of
option_id(uint16)option_length(uint16)option_value(variable, equal tooption_length)Am I right to assume that:
- I can define a (packed) structure for the first part, and that I can set the pointer on the data buffer which was allocated by malloc without problems ? [*]
You don't even need packing.
Quoted 3 lines
- For the second part, there are 2 cases:
+ integers must be read byte per byte
+ strings can be used directly ?
Assuming integers do not exceed 32 bit, just make sure the entire sequence is aligned to a multiple of four (which it is for the first entry, further ones need to use padding).
PATCH 2/2 Add helper script pcap2ulog
In reply to Pierre ChifflierThis script uses the Net::Pcap Perl library to parse an pcap file and send packets to ulogd2 throught the UNIXSOCK input module.
--- contrib/pcap2ulog | 101 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 101 insertions(+), 0 deletions(-) create mode 100755 contrib/pcap2ulog
contrib/pcap2ulog new file +101 -0
@@ -0,0 +1,101 @@ +#!/usr/bin/perl -w + +# Copyright (C) 2009 Pierre Chifflier <[email protected]> +# +# This program is free software; you can redistribute it and/or modify +# it under the same terms as Perl itself, either Perl version 5.8.4 or, +# at your option, any later version of Perl 5 you may have available. + +use strict; + +use IO::Socket; +use Net::Pcap; + +my $ULOGD_SOCKET_MARK = 0x41c90fd4; + +my $dumpfile = shift or die "Unable to open pcap file"; +my($pcap_t, $err); +my($ulogd_client); +my $socketfile = "/var/run/ulogd2.sock"; + +sub connect_ulogd2 { + (-S $socketfile) or die "ulogd2 socket $socketfile does not exist - is ulogd running ?"; + + $ulogd_client = IO::Socket::UNIX->new(Peer => $socketfile, + Type => SOCK_STREAM ) or die $!; +} + +sub process_pkt { + my($user, $hdr, $pkt) = @_; + + if (($user ne "xyz") or !defined($hdr) or !defined($pkt)) { + print("Bad args passed to callback\n"); + print("Bad user data\n"), if ($user ne "xyz"); + print("Bad pkthdr\n"), if (!defined($hdr)); + print("Bad pkt data\n"), if (!defined($pkt)); + print("not ok\n"); + exit; + } + + #print "Header: len $hdr->{len}\n"; + #my $len = length $pkt; + #print "Packet length: $len\n"; + + # decode packet + # packet type (sent by us: 4) + # link layer address type: 1 + # link layer address length: 6 + # src dst + # protocol (IP, ARP, PPP, SNMP ...) + # data + my $ignored = substr ($pkt, 0, 6); + my $srcmac = substr ($pkt, 6, 6); + my $protocol = unpack ("n", substr ($pkt, 12, 2)); + my $size = length($pkt) - 16; + + (my $hex_src = unpack("H*", $srcmac)) =~ s/(..)/$1:/g; + chop $hex_src; + #printf "source mac: $hex_src\n"; + + my $hex_dst = "\0"; + + # format data + my $data; + + # ulogd packet signature + $data = pack ('N', $ULOGD_SOCKET_MARK); + print $ulogd_client $data; + + my $options_num=2; + my $options_len=length($hex_src) + length($hex_dst); + # total length + $data = pack ('n', $size + 4 + (4*$options_num) + ($options_len)); + print $ulogd_client $data; + # payload length + payload + $data = pack ('na*', $size, substr($pkt,16,$size)); + print $ulogd_client $data; + # options + my $OOB_IN = 2; + $data = pack ('nna*', $OOB_IN, length($hex_src), $hex_src); + print $ulogd_client $data; + my $OOB_OUT = 3; + $data = pack ('nna*', $OOB_OUT, length($hex_dst), $hex_dst); + print $ulogd_client $data; + + #exit; +} + + +connect_ulogd2 or die $!; + +$pcap_t = Net::Pcap::open_offline($dumpfile, \$err); +if (!defined($pcap_t)) { + print("Net::Pcap::dump_open failed: ", Net::Pcap::geterr($pcap_t), "\n"); + exit; +} + +Net::Pcap::loop($pcap_t, 10, \&process_pkt, "xyz"); +Net::Pcap::close($pcap_t); + + +
Signature · 1 line
1.6.5.2