Skip to contentSkip to conversation

iptables u32 match patch-o-matic (attempt)

DISCUSSION Don Cohen 3 participants 6 messages Jan 29, 2003 – Feb 1, 2003 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 20030128234654.29362.28104.Mailman@kashyyyk

Don Cohen

author
#1 lore raw
Quoted 3 lines
Quoted 2 lines

Whereas NEWPATCHES seems to be meant to tell me what to do.

yup. but I think it's a bit out of date and never was completed.

I'd appreciate any ettempt to fix it to tell people like me what to do starting from square one.

Quoted 2 lines

no. I'm really surprised that it seems to be that difficult to explain in which format the patches are submitted in the ideal case.

Me too. Perhaps it would be worth while to store several of these ideal patches on the netfilter site as examples we could try to emulate.

Quoted 2 lines

And I have the feeling that I would spend more time explaining it in an email than it actually takes to package the patch in the correct format.

Surely you've already spent more time than that trying to explain what I should do.

Quoted 2 lines

So please just send me anything you have and I will bring it into the correct format as soon as I have some time left.

An offer I can't refuse. Thank you VERY much.

NEWPATCHES says:
1) Create a kernel patch which can be applied inside a kernel tree That's clearly one thing you need.

usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h renamed from usr/src/linux-2.4.18-clean/include/linux/netfilter_ipv4/ipt_u32.h +40 -0
diff -ur /usr/src/linux-2.4.18-clean/include/linux/netfilter_ipv4/ipt_u32.h /usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h
@@ -0,0 +1,40 @@
+#ifndef _IPT_U32_H
+#define _IPT_U32_H
+#include <linux/netfilter_ipv4/ip_tables.h>
+
+enum ipt_u32_ops
+{
+	IPT_U32_AND,
+	IPT_U32_LEFTSH,
+	IPT_U32_RIGHTSH,
+	IPT_U32_AT
+};
+
+struct ipt_u32_location_element
+{
+	u_int32_t number;
+	u_int8_t nextop;
+};
+struct ipt_u32_value_element
+{
+	u_int32_t min;
+	u_int32_t max;
+};
+/* *** any way to allow for an arbitrary number of elements?
+   for now I settle for a limit of 10 of each */
+#define U32MAXSIZE 10
+struct ipt_u32_test
+{
+	u_int8_t nnums;
+	struct ipt_u32_location_element location[U32MAXSIZE+1];
+	u_int8_t nvalues;
+	struct ipt_u32_value_element value[U32MAXSIZE+1];
+};
+
+struct ipt_u32
+{
+	u_int8_t ntests;
+	struct ipt_u32_test tests[U32MAXSIZE+1];
+};
+
+#endif /*_IPT_U32_H*/
usr/src/linux-2.4.18/net/ipv4/netfilter/ipt_u32.c renamed from usr/src/linux-2.4.18-clean/net/ipv4/netfilter/ipt_u32.c +201 -0

201 lines not on this page · raw message

2) Create a description file called `foo.patch.help'. Perhaps the doc from the source would have been better, but here's what I sent before.

Author: Don Cohen <[email protected]>
Status: Tested locally, seems to work.

U32 allows you to extract quantities of up to 4 bytes from a packet, AND them with specified masks, shift them by specified amounts and test whether the results are in any of a set of specified ranges. The specification of what to extract is general enough to skip over headers with lengths stored in the packet, as in IP or TCP header lengths.
Details and examples are in the kernel module source.

3) If your patch creates a new CONFIG option, eliminate that from the nope

4) If you want to edit the Makefile, eliminate that from the patch You might have told me not to include this, but

PF_EXT_SLIB+=TTL iplimit
PF_EXT_SLIB+=u32

5) If you want to edit ip{6}_conntrack.h in
include/linux/netfilter_ipv{4|6},
nope

So the one that's still missing is libipt_u32.c

/* Shared library add-on to iptables to add u32 matching,
generalized matching on values found at packet offsets

Detailed doc is in the kernel module source
net/ipv4/netfilter/ipt_u32.c
*/
#include <stdio.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <getopt.h>
#include <iptables.h>
#include <linux/netfilter_ipv4/ipt_u32.h>
#include <errno.h>
#include <ctype.h>

/* Function which prints out usage message. */
static void
help(void)

{
	printf(
		"u32 v%s options:\n"
		" --u32 tests\n"
		" tests := location = value | tests && location = value\n"
		" value := range | value , range\n"
		" range := number | number : number\n"
		" location := number | location operator number\n"
		" operator := & | << | >> | @\n"
		,IPTABLES_VERSION);
}

/* defined in /usr/include/getopt.h maybe in man getopt */ static struct option opts[] = {
{ "u32", 1, 0, '1' },
{0}
};

/* Initialize the match. */
static void
init(struct ipt_entry_match *m, unsigned int *nfcache)

{
	*nfcache |= NFC_UNKNOWN;
}

/* shared printing code */
static void print_u32(struct ipt_u32 *data)

{
	unsigned int testind;
	for (testind=0; testind < data->ntests; testind++) {
		if (testind) printf("&&");
		{
			unsigned int i;
			printf("0x%x", data->tests[testind].location[0].number);
			for (i=1; i < data->tests[testind].nnums; i++) {
				switch (data->tests[testind].location[i].nextop) {
				case IPT_U32_AND: printf("&"); break;
				case IPT_U32_LEFTSH: printf("<<"); break;
				case IPT_U32_RIGHTSH: printf(">>"); break;
				case IPT_U32_AT: printf("@"); break;
				}
				printf("0x%x", data->tests[testind].location[i].number);
			}
			printf("=");
			for (i=0; i < data->tests[testind].nvalues; i++) {
				if (i) printf(",");
				if (data->tests[testind].value[i].min
				    == data->tests[testind].value[i].max)
					printf("0x%x", data->tests[testind].value[i].min);
				else printf("0x%x:0x%x", data->tests[testind].value[i].min,
					    data->tests[testind].value[i].max);
			}
		}
	}
	printf(" ");
}

/* string_to_number is not quite what we need here ... */ u_int32_t parse_number(char **s, int pos)

{
	u_int32_t number;
	char *end;
	errno = 0;
	number = strtol(*s, &end, 0);
	if (end == *s)
		exit_error(PARAMETER_PROBLEM, "u32: at char %d expected number", pos);
	if (errno)
		exit_error(PARAMETER_PROBLEM, "u32: at char %d error reading number", pos);
	*s=end;
	return number;
}

/* Function which parses command options; returns true if it ate an option */ static int
parse(int c, char **argv, int invert, unsigned int *flags,
const struct ipt_entry *entry,
unsigned int *nfcache,
struct ipt_entry_match **match)

{
	struct ipt_u32 *data = (struct ipt_u32 *)(*match)->data;
	char *arg = argv[optind-1]; /* the argument string */
	char *start = arg;
	int state=0, testind=0, locind=0, valind=0;
	if (c != '1') return 0;
	/* states: 0 = looking for numbers and operations, 1 = looking for ranges */
	while (1) { /* read next operand/number or range */
		while (isspace(*arg)) arg++;  /* skip white space */
		if (! *arg) { /* end of argument found */
			if (state == 0)
				exit_error(PARAMETER_PROBLEM, "u32: input ended in location spec");
			if (valind == 0)
				exit_error(PARAMETER_PROBLEM, "u32: test ended with no value spec");
			data->tests[testind].nnums = locind;
			data->tests[testind].nvalues = valind;
			testind++;
			data->ntests=testind;
			if (testind > U32MAXSIZE)
				exit_error(PARAMETER_PROBLEM, "u32: at char %d too many &&'s",
					   arg-start);
			/* debugging 
			   print_u32(data);printf("\n");
			   exit_error(PARAMETER_PROBLEM, "debugging output done"); */
			return 1;
		}
		if (state==0) {
			/* reading location: read a number if nothing read yet,
			   otherwise either op number or = to end location spec */	 
			if (*arg == '=') {
				if (locind == 0)
					exit_error(PARAMETER_PROBLEM,
						   "u32: at char %d location spec missing", arg-start);
				else {arg++; state=1;
				}
			}
			else {
				if (locind) { /* need op before number */
					if (*arg == '&') {
						data->tests[testind].location[locind].nextop = IPT_U32_AND;
					}
					else if (*arg == '<') {
						arg++;
						if (*arg != '<')
							exit_error(PARAMETER_PROBLEM,
								   "u32: at char %d a second < expected", arg-start);
						data->tests[testind].location[locind].nextop = IPT_U32_LEFTSH;
					}
					else if (*arg == '>') {
						arg++;
						if (*arg != '>')
							exit_error(PARAMETER_PROBLEM,
								   "u32: at char %d a second > expected", arg-start);
						data->tests[testind].location[locind].nextop = IPT_U32_RIGHTSH;
					}
					else if (*arg == '@') {
						data->tests[testind].location[locind].nextop = IPT_U32_AT;
					}
					else exit_error(PARAMETER_PROBLEM,
							"u32: at char %d operator expected", arg-start);
					arg++;
				}
				/* now a number; string_to_number skips white space? */
				data->tests[testind].location[locind].number =
					parse_number(&arg, arg-start);
				locind++;
				if (locind > U32MAXSIZE)
					exit_error(PARAMETER_PROBLEM,
						   "u32: at char %d too many operators", arg-start);
			}
		}
		else {
			/* state 1 - reading values: read a range if nothing read yet,
			   otherwise either ,range or && to end test spec */
			if (*arg == '&') {
				arg++;
				if (*arg != '&')
					exit_error(PARAMETER_PROBLEM,
						   "u32: at char %d a second & expected", arg-start);
				if (valind == 0)
					exit_error(PARAMETER_PROBLEM,
						   "u32: at char %d value spec missing", arg-start);
				else {
					data->tests[testind].nnums = locind;
					data->tests[testind].nvalues = valind;
					testind++;
					if (testind > U32MAXSIZE)
						exit_error(PARAMETER_PROBLEM,
							   "u32: at char %d too many &&'s", arg-start);
					arg++; state=0; locind=0; valind=0;
				}
			}
			else { /* read value range */
				if (valind) { /* need , before number */
					if (*arg != ',')
						exit_error(PARAMETER_PROBLEM,
							   "u32: at char %d expected , or &&", arg-start);
					arg++;
				}
				data->tests[testind].value[valind].min = parse_number(&arg, arg-start);
				while (isspace(*arg)) arg++;  /* another place white space could be */
				if (*arg==':') {
					arg++;
					data->tests[testind].value[valind].max
						= parse_number(&arg, arg-start);
				}
				else data->tests[testind].value[valind].max
					     = data->tests[testind].value[valind].min;
				valind++;
				if (valind > U32MAXSIZE)
					exit_error(PARAMETER_PROBLEM,
						   "u32: at char %d too many ,'s", arg-start);
			}
		}
	}
}

/* Final check; must specify something. */
static void
final_check(unsigned int flags)
{
}

/* Prints out the matchinfo. */
static void
print(const struct ipt_ip *ip,
const struct ipt_entry_match *match,
int numeric)

{
	printf("u32 ");
	print_u32((struct ipt_u32 *)match->data);
}

/* Saves the union ipt_matchinfo in parsable form to stdout. */ static void save(const struct ipt_ip *ip, const struct ipt_entry_match *match)

{
	printf("--u32 ");
	print_u32((struct ipt_u32 *)match->data);
}

struct iptables_match u32
= { NULL,

    "u32",
    IPTABLES_VERSION,
    IPT_ALIGN(sizeof(struct ipt_u32)),
    IPT_ALIGN(sizeof(struct ipt_u32)),
    &help,
    &init,
    &parse,
    &final_check,
    &print,
    &save,
    opts
};

void
_init(void)

{
	register_match(&u32);
}
#2 lore raw

Hi Don!

May I ask you to change those u_int8_t's to u_int32_t in struct ipt_u32_location_element, struct ipt_u32_value_element, struct ipt_u32_test, struct ipt_u32?

There's really no point in making those integers 1 byte long when every second field is (a multiple of) 4 byte long and thus will eventually get padded during compilation. It just complicates matters.

Thanks,
Laszlo

Quoted 26 lines · Don Cohen
usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h renamed from usr/src/linux-2.4.18-clean/include/linux/netfilter_ipv4/ipt_u32.h +26 -0
+struct ipt_u32_location_element
+{
+	u_int32_t number;
+	u_int8_t nextop;
+};
+struct ipt_u32_value_element
+{
+	u_int32_t min;
+	u_int32_t max;
+};
+/* *** any way to allow for an arbitrary number of elements?
+   for now I settle for a limit of 10 of each */
+#define U32MAXSIZE 10
+struct ipt_u32_test
+{
+	u_int8_t nnums;
+	struct ipt_u32_location_element location[U32MAXSIZE+1];
+	u_int8_t nvalues;
+	struct ipt_u32_value_element value[U32MAXSIZE+1];
+};
+
+struct ipt_u32
+{
+	u_int8_t ntests;
+	struct ipt_u32_test tests[U32MAXSIZE+1];
+};
author
#3 lore raw
Quoted 9 lines · Laszlo Valko

Hi Don!

May I ask you to change those u_int8_t's to u_int32_t in struct ipt_u32_location_element, struct ipt_u32_value_element, struct ipt_u32_test, struct ipt_u32?

There's really no point in making those integers 1 byte long when every second field is (a multiple of) 4 byte long and thus will eventually get padded during compilation. It just complicates matters.

Now that I've dumped it in Harald's lap I guess it's up to him. But I don't see how it complicates matters.

Quoted 7 lines

usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h line 32

Quoted 7 lines
usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h renamed from usr/src/linux-2.4.18-clean/include/linux/netfilter_ipv4/ipt_u32.h +7 -0
+struct ipt_u32_test
+{
+	u_int8_t nnums;
+	struct ipt_u32_location_element location[U32MAXSIZE+1];
+	u_int8_t nvalues;
+	struct ipt_u32_value_element value[U32MAXSIZE+1];
+};

In this one case at least it might help to put the two 8's together, right?

#4 lore raw
Quoted 7 lines · Don Cohen
Quoted 6 lines · Laszlo Valko

Hi Don!

May I ask you to change those u_int8_t's to u_int32_t in struct ipt_u32_location_element, struct ipt_u32_value_element, struct ipt_u32_test, struct ipt_u32?

Quoted 6 lines
Quoted 3 lines

There's really no point in making those integers 1 byte long when every second field is (a multiple of) 4 byte long and thus will eventually get padded during compilation. It just complicates matters.

Now that I've dumped it in Harald's lap I guess it's up to him. But I don't see how it complicates matters.

I also disagree with Laszlo. We don't need cross-architecture compatibility of those structures anyway.

Quoted 10 lines
Quoted 7 lines

usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h line 32

Quoted 7 lines
usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h renamed from usr/src/linux-2.4.18-clean/include/linux/netfilter_ipv4/ipt_u32.h +7 -0
+struct ipt_u32_test
+{
+	u_int8_t nnums;
+	struct ipt_u32_location_element location[U32MAXSIZE+1];
+	u_int8_t nvalues;
+	struct ipt_u32_value_element value[U32MAXSIZE+1];
+};

In this one case at least it might help to put the two 8's together, right?

yes, it should. depending on compiler and archtiecture, though.

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
GCS/E/IT d- s-: a-- C+++ UL++++$ P+++ L++++$ E--- W- N++ o? K- w--- O- M- 
V-- PS+ PE-- Y+ PGP++ t++ 5-- !X !R tv-- b+++ DI? !D G+ e* h+ r% y+(*)
#5 lore raw
Quoted 12 lines · Harald Welte

On Wed, Jan 29, 2003 at 07:28:57AM -0800, Don Cohen wrote:
I also disagree with Laszlo. We don't need cross-architecture compatibility of those structures anyway.

Quoted 8 lines
Quoted 7 lines
Quoted 7 lines
usr/src/linux-2.4.18/include/linux/netfilter_ipv4/ipt_u32.h renamed from usr/src/linux-2.4.18-clean/include/linux/netfilter_ipv4/ipt_u32.h +7 -0
+struct ipt_u32_test
+{
+	u_int8_t nnums;
+	struct ipt_u32_location_element location[U32MAXSIZE+1];
+	u_int8_t nvalues;
+	struct ipt_u32_value_element value[U32MAXSIZE+1];
+};
Quoted 4 lines
Quoted 2 lines

In this one case at least it might help to put the two 8's together, right?

yes, it should. depending on compiler and archtiecture, though.

Ok, but please understand that gcc will not do any packing on x86 or any RISC platforms by default. And since this is part of the platform ABI, the default behaviour is a fairly stable thing.
This boils down to saying that whatever you use (u_int8_t or u_int32_t), it will not matter, the compiler will generate the same structure for all major platforms of Linux (x86, alpha, sparc for sure).

Of course, this whole debate is akin to splitting a hair into two... :)

Regards,
Laszlo

Quoted 6 lines
Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
GCS/E/IT d- s-: a-- C+++ UL++++$ P+++ L++++$ E--- W- N++ o? K- w--- O- M- 
V-- PS+ PE-- Y+ PGP++ t++ 5-- !X !R tv-- b+++ DI? !D G+ e* h+ r% y+(*)
#6 lore raw
In reply to Don Cohen

On Tue, Jan 28, 2003 at 08:18:36PM -0800, Don Cohen wrote:
Content-Description: message body text

Quoted 5 lines
Quoted 2 lines

no. I'm really surprised that it seems to be that difficult to explain in which format the patches are submitted in the ideal case.

Me too. Perhaps it would be worth while to store several of these ideal patches on the netfilter site as examples we could try to emulate.

mh. that would be an idea, though there are quite some in the mailinglist archives. Maybe this is something for the FAQ (which needs lots of updates anyway *sigh*).

Quoted 3 lines
Quoted 2 lines

So please just send me anything you have and I will bring it into the correct format as soon as I have some time left.

An offer I can't refuse. Thank you VERY much.

;) It will appear in CVS soon.

Signature · 5 lines
Live long and prosper
- Harald Welte / [email protected]               http://www.gnumonks.org/
============================================================================
GCS/E/IT d- s-: a-- C+++ UL++++$ P+++ L++++$ E--- W- N++ o? K- w--- O- M- 
V-- PS+ PE-- Y+ PGP++ t++ 5-- !X !R tv-- b+++ DI? !D G+ e* h+ r% y+(*)