Skip to contentSkip to conversation

Questions about Iptable extensions

DISCUSSION Shannon J. Field 1 participant 1 message Jun 10, 2004 netfilter

lore
Share & tools
lore
#0 lore raw
	Thanks for your help Henrik!

	 

	I made all the changes to the code and it compiled fine. Once I got the library linked and in place, I tried this command:

	# iptables -A OUTPUT -d 64.185.64.13 -p tcp --dport 80 -m length --length 1000 -j ACCEPT
	iptables: match `length' has invalid size 2.
	

	I have been searching around and haven't quite found what this error means or how to correct it.

	 

	Thanks,

	Shannon

	** Revised code below **

	/* Shared library add-on to iptables to add packet length matching support. */
	#include <stdio.h>
	#include <netdb.h>
	#include <string.h>
	#include <stdlib.h>
	#include <getopt.h>
	#include <iptables.h>
	// #include<linux/netfilter_ipv4/ipt_length.h>

	#ifndef _IPT_LENGTH_H
	#define _IPT_LENGTH_H

	struct ipt_length_info {
	                u_int16_t length;
	};
	#endif /*_IPT_LENGTH_H*/

	/* Function which prints out usage message. */
	static void
	help(void)
	{
	        printf("length v%s options:\n"
	              "--length length    Match this packet length\n",
	              NETFILTER_VERSION);
	}

	static struct option opts[] = {
	        { "length", 1, 0, '1' },
	        {0}
	};

	/* Initialize the match. */
	static void
	init(struct ipt_entry_match *m, unsigned int *nfcache)
	{
	        *nfcache |= NFC_UNKNOWN;
	}

	/* 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_length_info *info = (struct ipt_length_info *)(*match)->data;
	       int len;

	        switch (c) {
	                case '1':
	                        if (*flags)
	                                exit_error(PARAMETER_PROBLEM,
	                                           "length: `--length' may only be "
	                                           "specified once");
	                       len = string_to_number(argv[optind-1], 0xFFFF,0, info->length);
	                       if (len == -1)
	                               exit_error(PARAMETER_PROBLEM,
	                                          "length invalid: `%s'\n", argv[optind-1]);
	                        *flags = 1;
	                        break;

	                default:
	                        return 0;
	        }
	        return 1;
	}

	/* Final check; must have specified --length. */
	static void
	final_check(unsigned int flags)
	{
	        if (!flags)
	                exit_error(PARAMETER_PROBLEM,
	                           "length: You must specify `--length'");
	}

	/* Common match printing code. */
	static void
	print_length(struct ipt_length_info *info)
	{
	        printf("%u ", info->length);
	}

	/* Prints out the matchinfo. */
	static void
	print(const struct ipt_ip *ip,
	      const struct ipt_entry_match *match,
	      int numeric)
	{
	        printf("length ");
	        print_length((struct ipt_length_info *)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("--length ");
	        print_length((struct ipt_length_info *)match->data);
	}

	struct iptables_match length
	= { NULL,
	    "length",
	    NETFILTER_VERSION,
	    sizeof(struct ipt_length_info),
	    sizeof(struct ipt_length_info),
	    &help,
	    &init,
	    &parse,
	    &final_check,
	    &print,
	    &save,
	    opts
	};

	void _init(void)
	{
	        register_match(&length);
	}