Saturday, May 22, 2004
optarg offer
I send an e-mail to developer list and suggested to use optarg instead of argv[optind-1]. It may be seen like waste of time. Correcting will give no performance gain but maybe a better understanding of iptables.c code.
Thursday, May 20, 2004
Notes about getopt_long
Example command;
iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT
argc is 11
argv is "iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT"
-A is an option element
A is an option character
argv[2] is TEST
TEST is an optarg (option argument of -A)
10.1.1.1 is another optarg
optind is the index of an option
if optind == 3 then its value is argv[optind-1], argv[2] is TEST
argv[optind-1] is equal to optarg
getopt_long is used in a loop to parse all the command and find all the options. When it finds an option returns its option character. In every loop value of optarg and optind changes. getopt_long returns -1 when there is no option is left.
The difference between getopt and getopt_long is, getopt_long can take long parameters which are pointers to option names.
iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT
argc is 11
argv is "iptables -A TEST -s 10.1.1.1 -p tcp --sport 80 -j ACCEPT"
-A is an option element
A is an option character
argv[2] is TEST
TEST is an optarg (option argument of -A)
10.1.1.1 is another optarg
optind is the index of an option
if optind == 3 then its value is argv[optind-1], argv[2] is TEST
argv[optind-1] is equal to optarg
getopt_long is used in a loop to parse all the command and find all the options. When it finds an option returns its option character. In every loop value of optarg and optind changes. getopt_long returns -1 when there is no option is left.
The difference between getopt and getopt_long is, getopt_long can take long parameters which are pointers to option names.
Tuesday, May 18, 2004
iptables : Master of the rules
Today I feel very good. I swimed for half an hour at 07:30 am, drank a big glass of milk and ate well.
I have 3-4 hours to work on netfilter. So lets start by understanding how iptables command works because without seeing the full picture we have no chance to fix a small bug. Our small bug (as mentioned before) is: iptables lets same rule to be inserted twice one after another. Actually this has no big negative effect but it is useless and unmeaningful. ...So before a modification in iptables we have to check if the modification will cause a "same rule one after another" problem.
When we enter a rule by typing "iptables -A INPUT -s 10.1.1.1 -i eth0 -j REJECT", iptables parses the parameters (-A INPUT -s 10.1.1.1 -i eth0 -j REJECT) it gets from the command line interface (CLI). do_command function does well with all the string we entered. iptables.c has 2297 line of code (at the time of writing) and 647 lines of it is for do_command function. Guess how much work do_command handles.
In do_command with the help of (*)getopt_long funciton (described in getopt.h) -A and -s like parameters are easily read. In iptables capital letters (like -A) are for commands and small letters (like -s) are for options. There are 13 commands and 12 options available in iptables.
In a switch statement commands and options are parsed with add_command and set_option functions.
add_command takes four arguments, command (as variable) is one of them. command (as variable) holds the hex value of the command which is defined in the early parts of iptables.c. Some commands takes another command by default or an invert option may be set so a possible incompability problem is checked. If there is no problem add_command sets command (as variable) with a hex value.
set_option function takes four arugments. option (as variable) behaves like command (as variable) and holds the value of an option which are defined with #DEFINE.
After some checks in a switch statement of command, key functions (I call them key functions because they modify the rules) are executed. These are append_entry, insert_entry, delete_entry, replace_entry.
(*)getopt_long uses an array of "struct option" called original_opts[] to identify the long name and the value of the command also if command has an argument. has_arg the int value of option struct which identifies if the command has an option takes three values. 0 for no arguments, 1 for required argument, 2 for optional argument.For example append (-A) has a required arugment which must be given but list (-L) has an optional argument which my be null. Verbose (is an option and) has no arguments. These are defined in command_v_options which is a two dimensional array.
getopt_long`s another argument is an interesting one named optstring. optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so getopt (which is defined as extern char * in getopt.h) places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the current argv-element, it is returned in optarg, otherwise optarg is set to zero.
According to our example iptables command you can think optarg for A as INPUT. In iptables.c there are some checks like if command is "A" and chain (optarg) is "INPUT" then there can`t be an "o" option. But...
...INPUT chain has no output interface but you can create a custom chain, give it an output interface and jump a rule in INPUT to your custom chain. This is not correct but iptables can not decide if it is correct or not. For example;
I have 3-4 hours to work on netfilter. So lets start by understanding how iptables command works because without seeing the full picture we have no chance to fix a small bug. Our small bug (as mentioned before) is: iptables lets same rule to be inserted twice one after another. Actually this has no big negative effect but it is useless and unmeaningful. ...So before a modification in iptables we have to check if the modification will cause a "same rule one after another" problem.
When we enter a rule by typing "iptables -A INPUT -s 10.1.1.1 -i eth0 -j REJECT", iptables parses the parameters (-A INPUT -s 10.1.1.1 -i eth0 -j REJECT) it gets from the command line interface (CLI). do_command function does well with all the string we entered. iptables.c has 2297 line of code (at the time of writing) and 647 lines of it is for do_command function. Guess how much work do_command handles.
In do_command with the help of (*)getopt_long funciton (described in getopt.h) -A and -s like parameters are easily read. In iptables capital letters (like -A) are for commands and small letters (like -s) are for options. There are 13 commands and 12 options available in iptables.
In a switch statement commands and options are parsed with add_command and set_option functions.
add_command takes four arguments, command (as variable) is one of them. command (as variable) holds the hex value of the command which is defined in the early parts of iptables.c. Some commands takes another command by default or an invert option may be set so a possible incompability problem is checked. If there is no problem add_command sets command (as variable) with a hex value.
set_option function takes four arugments. option (as variable) behaves like command (as variable) and holds the value of an option which are defined with #DEFINE.
After some checks in a switch statement of command, key functions (I call them key functions because they modify the rules) are executed. These are append_entry, insert_entry, delete_entry, replace_entry.
(*)getopt_long uses an array of "struct option" called original_opts[] to identify the long name and the value of the command also if command has an argument. has_arg the int value of option struct which identifies if the command has an option takes three values. 0 for no arguments, 1 for required argument, 2 for optional argument.For example append (-A) has a required arugment which must be given but list (-L) has an optional argument which my be null. Verbose (is an option and) has no arguments. These are defined in command_v_options which is a two dimensional array.
getopt_long`s another argument is an interesting one named optstring. optstring is a string containing the legitimate option characters. If such a character is followed by a colon, the option requires an argument, so getopt (which is defined as extern char * in getopt.h) places a pointer to the following text in the same argv-element, or the text of the following argv-element, in optarg. Two colons mean an option takes an optional arg; if there is text in the current argv-element, it is returned in optarg, otherwise optarg is set to zero.
According to our example iptables command you can think optarg for A as INPUT. In iptables.c there are some checks like if command is "A" and chain (optarg) is "INPUT" then there can`t be an "o" option. But...
...INPUT chain has no output interface but you can create a custom chain, give it an output interface and jump a rule in INPUT to your custom chain. This is not correct but iptables can not decide if it is correct or not. For example;
iptables -N TEST
iptables -A TEST -o eth0 -j ACCEPT
iptables -A INPUT -j TEST
Monday, May 17, 2004
Killing me softly...
I walked for 10 hours to buy a wedding dress on saturday. Yesterday we cleaned home, washed all carpets... It took several hours. At 23:00 I sit down and watched NBA for an hour. After six hours of sleep I feel tired but there is a patch waiting for me to be written :) With one word : Motivation
Friday, May 14, 2004
insert_rules, it is all about memory
insert_rules function is used while appending or inserting a rule to a table. It takes 7 arguments as I mentioned yesterday.
If chain_cache`s end_offset is bigger or equal to the total size of the chains in the table, it returns EINVAL. Else;
There is a struct named ipt_getinfo in iptc_handle_t structure. It holds valuable information about hooks, size, entry addresses etc.
struct ipt_getinfo
{
char name[IPT_TABLE_MAXNAMELEN]; // table name, filled by caller
unsigned int valid_hooks; // validated hooks by kernel
unsigned int hook_entry[NF_IP_NUMHOOKS]; // entry point for each hook (chain)
unsigned int underflow[NF_IP_NUMHOOKS]; // end of each hook
unsigned int num_entries; // number of entries
unsigned int size; // size of entries
};
to be continued...
If chain_cache`s end_offset is bigger or equal to the total size of the chains in the table, it returns EINVAL. Else;
There is a struct named ipt_getinfo in iptc_handle_t structure. It holds valuable information about hooks, size, entry addresses etc.
struct ipt_getinfo
{
char name[IPT_TABLE_MAXNAMELEN]; // table name, filled by caller
unsigned int valid_hooks; // validated hooks by kernel
unsigned int hook_entry[NF_IP_NUMHOOKS]; // entry point for each hook (chain)
unsigned int underflow[NF_IP_NUMHOOKS]; // end of each hook
unsigned int num_entries; // number of entries
unsigned int size; // size of entries
};
to be continued...
Thursday, May 13, 2004
Woaow I am about to append a rule...
What do you need to append a rule? It seems to be a little bit complicated. insert_rules gives the clues so lets read it...
ret = insert_rules(1, e->next_offset, e, c->end_off, offset2index(*handle, c->end_off), 0, handle);
1 : I will insert one rule
e->next_offset : size of the entry(rule) that I will insert
e : the entry that I will insert
c->end_off : c is chain_cache and chain_cache->end_off is the end_offset of the chain_cache like e->next_offset, so I guess it gives the size of the chain.
offset2index(*handle, c->end_off) : returns the position of the rule, detailed below...
0 : no prepend, not the first rule
handle : address for the table
offset2index uses entry2index which uses offset2entry.
offset2entry returns an ipt_entry by adding c->end_off to handle->entries.entrytable.
entry2index returns the position number of the rule, by using ENTRY_ITERATE(in ip_tables.h).
Next I wish to write about insert_rules internals...
ret = insert_rules(1, e->next_offset, e, c->end_off, offset2index(*handle, c->end_off), 0, handle);
1 : I will insert one rule
e->next_offset : size of the entry(rule) that I will insert
e : the entry that I will insert
c->end_off : c is chain_cache and chain_cache->end_off is the end_offset of the chain_cache like e->next_offset, so I guess it gives the size of the chain.
offset2index(*handle, c->end_off) : returns the position of the rule, detailed below...
0 : no prepend, not the first rule
handle : address for the table
offset2index uses entry2index which uses offset2entry.
offset2entry returns an ipt_entry by adding c->end_off to handle->entries.entrytable.
entry2index returns the position number of the rule, by using ENTRY_ITERATE(in ip_tables.h).
Next I wish to write about insert_rules internals...
The big picture
STRUCT_TC_HANDLE (struct iptc_handle_t) holds all the information about a table, it`s chains and so. More technically it is a pointer to the structure of the rules. It maps a place in memory so we can get the meaningful parts of the memory we want. Actually we use other structures to get these data in order to understand them.
iptc_handle_t has an important structure called ipt_enrty which is hold as an array in ipt_get_entries structure`s entrytable array.
struct ipt_get_entries
{
/* Which table: user fills this in. */
char name[IPT_TABLE_MAXNAMELEN];
/* User fills this in: total entry size. */
unsigned int size;
/* The entries. */
struct ipt_entry entrytable[0];
};
ipt_entry entrytable`s initial size is zero because (as you guess) place for new rules are allocated when needed.
iptc_handle_t has an important structure called ipt_enrty which is hold as an array in ipt_get_entries structure`s entrytable array.
struct ipt_get_entries
{
/* Which table: user fills this in. */
char name[IPT_TABLE_MAXNAMELEN];
/* User fills this in: total entry size. */
unsigned int size;
/* The entries. */
struct ipt_entry entrytable[0];
};
ipt_entry entrytable`s initial size is zero because (as you guess) place for new rules are allocated when needed.
Useful information about a chain
I meet chain_cache struct in TC_APPEND and TC_INSERT (in libiptc.c) which holds the name, the first rule and the last rule of the chain.
struct chain_cahce {
char name [max...] : name of the chain
unsigned int start_off : offset of the first rule in the chain
unsigned int end_off : offset of the last rule in the chain (size of the rules)
}
struct chain_cahce {
char name [max...] : name of the chain
unsigned int start_off : offset of the first rule in the chain
unsigned int end_off : offset of the last rule in the chain (size of the rules)
}
Do not forget me!
Being a human, I forget everything... It has been 5 days since my last study on netfilter and I forget insert_rules function`s details. So today it will be a restart day. Because I am working on a patch for iptables.c which will prohibit a user to insert same rule one after another, I need to understand how a rule is inserted to memory, how two rules can be matched and how a rule with a rulenum can be found.
I am working on insert_rules which is used both by TC_INSERT_ENTRY and TC_APPEND_ENTRY. As you may already know append adds the rule as the last rule of the chain, insert adds the rule to the position which is mention in rulenum.
I am working on insert_rules which is used both by TC_INSERT_ENTRY and TC_APPEND_ENTRY. As you may already know append adds the rule as the last rule of the chain, insert adds the rule to the position which is mention in rulenum.
Wednesday, May 12, 2004
Netfilter?
I will write my notes about netfilter here. I don`have time now so see you tomarrow. By the way thanks to Faik Uygur who informed about blogger.