Skip to contentSkip to conversation

libiptc/rules with MAC match

DISCUSSION DarKRaveR 2 participants 6 messages Feb 12, 2003 – Feb 13, 2003 netfilter

lore
Share & tools
lore

DarKRaveR

author
#0 lore raw

Hello netfilter-devel ppl,

I got some questions concerning libiptc and acquiring rules and setting rules with it. I read all documents I could find, as well as part of iptable sources and the header files etc. .
Yet there's some things I don't seem to get straight.

If I fetch the rules of a chain, I'll get a struct ipt_entry (well, actually a pointer, but this won'T matter for now). There's all kinda src,dst ips,netmasks etc. flags.

  Within this struct there an ipt_ip struct, with the properties:
  char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
  unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];

What's the difference between those two ?

The other thing is, assuming I have a rule, which matches a MAC Adress, how do I get the MAC Adress (and if such a matching is in the rule). From what I've understood, there's the target_offset counter, which helps me on that.
IT seems to describe the size of a memmory block, so basicly, if I sub the size of the ipt_entry structure, I get the size of the matches, or actually, behind the ipt_entry in the memory space there seems to be a flat array, with matches ?
The other thing is, if I want to set such a rule, with mac matching, even if I properly construct that memory block, what do I need to take care about, alignment stuff etc. ?

Even more intriguing is the following question to me:

I can see, there's a function to get the target.
Assuming I want to set a rule, how do I set the target properly, and how do I set a MAC matching ?

I couldn't find anything in particular on that and would apprechiate any help on that.

Signature · 2 lines
Best regards,
 DarKRaveR                          mailto:[email protected]
#1 lore raw

Hi,

Quoted 5 lines · DarKRaveR
  Within this struct there an ipt_ip struct, with the properties:
  char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
  unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];

What's the difference between those two ?

just have a look at parse_interface() in $IPT_SOURCE/iptables.c or ip_packet_match() in $KERNELDIR/net/ipv4/netfilter/ip_tables.c.

To make a long story short:

Iptables allows you to specify a device like this: "-i eth+", which means all devices that start with "eth".

In this example iniface would contain: "eth+", whereas iniface_mask contains something like { 0xFF,0xFF,0xFF,NULL }.

So when iptables comes to compare the actual device with the device specified in the rule it compares only the first 3 caracters.

Example:

eth1  actual device of packet
eth+  device in rule
1110  mask (see every "1" as a "0xFF" for a character comparator)

----
eth -> match

At least this is how I understood the sources.

Ciao!
Thomas

Signature · 2 lines
In theory, there is no difference between theory and practice.
In practice, there is.
author
#2 lore raw

Hello Thomas,

Kewl, thanx for that so far, it sounds really promising, now the term 'mask' makes sense and it opens a whole new arsenal of possibilities how to define rules. I really never came across this, but this might really help me, I could gather all vlan devices on on NIC, nice stuff, thnx.

Wednesday, February 12, 2003, 10:05:22 PM, you wrote:

TP> Hi,

Quoted 5 lines · DarKRaveR
Quoted 5 lines
  Within this struct there an ipt_ip struct, with the properties:
  char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
  unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];

What's the difference between those two ?

TP> just have a look at parse_interface() in $IPT_SOURCE/iptables.c or TP> ip_packet_match() in $KERNELDIR/net/ipv4/netfilter/ip_tables.c.

TP> To make a long story short:

TP> Iptables allows you to specify a device like this: "-i eth+", which TP> means all devices that start with "eth".

TP> In this example iniface would contain: "eth+", whereas iniface_mask TP> contains something like { 0xFF,0xFF,0xFF,NULL }.

TP> So when iptables comes to compare the actual device with the device TP> specified in the rule it compares only the first 3 caracters.

TP> Example:

TP> eth1  actual device of packet
TP> eth+  device in rule
TP> 1110  mask (see every "1" as a "0xFF" for a character comparator)

TP> ----
eth ->> match

TP> At least this is how I understood the sources.

TP> Ciao!
TP> Thomas

Signature · 2 lines
Best regards,
 DarKRaveR                            mailto:[email protected]
author
#3 lore raw
In reply to DarKRaveR

Hello list,

Okay, I kinda tried out some things.

I use libiptc to get a rule ...
there's destinations source, etc.
if target_offset is bigger than sizeof(struct ipt_entry), there's a match present.
elems points to the first match ...
If I cast it to ipt_entry_match, and then check the vars in that structure, I can see various sizes etc.
u.user.name gives me 'mac'.

but, what I am wondering:
where can I find the actual mac address ?
there ipt_entry_match.data, but the data in there didn't make too much sense ...

Can anyone help me out on that ?

And how can I construct a rule myself, that does mac matching ? I am kinda stuck !

Wednesday, February 12, 2003, 9:13:03 PM, you wrote:

D> Hello netfilter-devel ppl,

D>   I got some questions concerning libiptc and acquiring rules and setting
D>   rules with it. I read all documents I could find, as well as part of
D>   iptable sources and the header files etc. .
D>   Yet there's some things I don't seem to get straight.
D>   If I fetch the rules of a chain, I'll get a struct ipt_entry (well,
D>   actually a pointer, but this won'T matter for now). There's all
D>   kinda src,dst ips,netmasks etc. flags.
D>   Within this struct there an ipt_ip struct, with the properties:
D>   char iniface[IFNAMSIZ], outiface[IFNAMSIZ];
D>   unsigned char iniface_mask[IFNAMSIZ], outiface_mask[IFNAMSIZ];

D> What's the difference between those two ?

D>   The other thing is, assuming I have a rule, which matches a MAC
D>   Adress, how do I get the MAC Adress (and if such a matching is in
D>   the rule). From what I've understood, there's the target_offset
D>   counter, which helps me on that.
D>   IT seems to describe the size of a memmory block, so basicly, if I
D>   sub the size of the ipt_entry structure, I get the size of the
D>   matches, or actually, behind the ipt_entry in the memory space there
D>   seems to be a flat array, with matches ?
D>   The other thing is, if I want to set such a rule, with mac matching,
D>   even if I properly construct that memory block, what do I need to
D>   take care about, alignment stuff etc. ?

D> Even more intriguing is the following question to me:

D>   I can see, there's a function to get the target.
D>   Assuming I want to set a rule, how do I set the target properly, and
D>   how do I set a MAC matching ?

D> I couldn't find anything in particular on that and would apprechiate
D> any help on that.

Signature · 2 lines
Best regards,
 DarKRaveR                            mailto:[email protected]
#4 lore raw

Hi,

Quoted 1 line · DarKRaveR

I use libiptc to get a rule ...

ok so far.

Quoted 1 line

there's destinations source, etc.

yep

Quoted 2 lines

if target_offset is bigger than sizeof(struct ipt_entry), there's a match present.

yep

Quoted 1 line

elems points to the first match ...

sorry, you lost me.
I'll just describe what I would do, from the point where you had ipt_entry, ok? If this is what you did, just skip this part.

struct ipt_entry *e;
struct ipt_entry_match *em;
unsigned int offset;

for(offset = sizeof(struct ipt_entry);
    offset < e->target_offset;
    offset += em->u.match_size) 
{ 
    em = (void* )e + offset;
}
Quoted 3 lines

If I cast it to ipt_entry_match, and then check the vars in that structure, I can see various sizes etc.
u.user.name gives me 'mac'.

this looks promising

Quoted 4 lines

but, what I am wondering:
where can I find the actual mac address ?
there ipt_entry_match.data, but the data in there didn't make too much sense ...

Have you had a look at $IPTABLES_SRC/extensions/libipt_mac.c ? It is just right there, at your fingertips. Its the first ETH_ALEN unsigned chars in match->data. Just look at the function print() in the above mentioned file and try to parse the output as they do it. Works for them and works for me, why shouldn't it work for you? ;-)

Quoted 1 line

Can anyone help me out on that ?

Hope I did.

Quoted 1 line

And how can I construct a rule myself, that does mac matching ?

iptables -I $chain -m mac --mac-source ...
or have a look at parse_mac() in libipt_mac.c

Happy hacking,
Thomas

Signature · 4 lines
"If my son wants to be a pimp when he grows up, that's fine with me.  I
hope he's a good one and enjoys it and doesn't get caught. I'll support
him in this. But if he wants to be a network administrator, he's out of
the house and not part of my family."                  -- Steve Wozniak
author
#5 lore raw

Hello Thomas,

Thanx for you email, this looks really promising, I think ... Okay, further down are some more remarks ...

Thursday, February 13, 2003, 11:55:03 AM, you wrote:

TP> Hi,

Quoted 1 line · DarKRaveR
Quoted 1 line

I use libiptc to get a rule ...

TP> ok so far.

Quoted 1 line
Quoted 1 line

there's destinations source, etc.

TP> yep

Quoted 2 lines
Quoted 2 lines

if target_offset is bigger than sizeof(struct ipt_entry), there's a match present.

TP> yep

Quoted 1 line
Quoted 1 line

elems points to the first match ...

TP> sorry, you lost me.
TP> I'll just describe what I would do, from the point where you had TP> ipt_entry, ok? If this is what you did, just skip this part.

Okay, I see, the way you do it, is pretty much the way it is done in IPT_ITERATE_MATCH function? in one of the header files ... at least it looks familiar.

anyway, I came to the conclusion that ...

(void *)e+offset .. is the same as (void *)e+sizeof(struct ipt_entry) and that this address is the same stored in unsigned char elems[0] (within ipt_entry, which is a one char array or basicly a char pointer) so (void *)e+sizeof(struct ipt_entry) is the same as (void *)elems.

But from there, you have to work with the u.match_size ... Just a thought, using a pointer to create some sort of 'linked list' like in the case of ipt_entry, and the pointer, pointing to the matches and targets, might be a cleaner design ? you wouldn't have to calculate memory offsets and use so much pointer arithmetic in your code, and maybe move the dirty stuff where it belongs, in iptc somewhere under the hood :-)

But that's just a general thought ...

TP> struct ipt_entry *e;
TP> struct ipt_entry_match *em;
TP> unsigned int offset;

TP> for(offset = sizeof(struct ipt_entry);
TP> offset < e->target_offset;
TP> offset += em->u.match_size)
TP> {
TP> em = (void* )e + offset;
TP> }

Quoted 3 lines
Quoted 3 lines

If I cast it to ipt_entry_match, and then check the vars in that structure, I can see various sizes etc.
u.user.name gives me 'mac'.

TP> this looks promising

That's what I thought myself ;-)

Quoted 4 lines
Quoted 4 lines

but, what I am wondering:
where can I find the actual mac address ?
there ipt_entry_match.data, but the data in there didn't make too much sense ...

TP> Have you had a look at $IPTABLES_SRC/extensions/libipt_mac.c ? It is

No unfortunately not, reading all sources was a little bit too much, at the current situation, but I guess, I'll have to do that :-)

TP> just right there, at your fingertips. Its the first ETH_ALEN unsigned chars in match->>data. Just look at the function print() in the above TP> mentioned file and try to parse the output as they do it. Works for them TP> and works for me, why shouldn't it work for you? ;-)

Okay, I will check back on that, I guess, my own routine ... aight *DAMN* thats the point, THANX ! I was runnign through the data searching for a \0 just because I read unsigned char pointer, what a fool I am, esp. when my MAC starts with 00: ... okay, good point, that HELPED alot, I think, I was/am just too tired ;-) maybe I should sleep some time ...

Quoted 1 line
Quoted 1 line

Can anyone help me out on that ?

TP> Hope I did.

Sure did, many thanx, sometimes things are so obvious and just won'T pop up to your eyes ...

Quoted 1 line
Quoted 1 line

And how can I construct a rule myself, that does mac matching ?

TP> iptables -I $chain -m mac --mac-source ...
TP> or have a look at parse_mac() in libipt_mac.c

I'll stick to the latter I fear, spawning iptables is not an option, if you want to update a pile of rules (delete/add) in a set of a dozen chains with approx 200 rules each ;-) that would not be too much fun I assume :-)

TP> Happy hacking,
TP> Thomas

Yeah, thanx, I'll try that tomorrow I think, tonight I will be too tired, after being up for 30 hrs ;-)

I really appreciate your help ..

Signature · 5 lines
Best regards,
 DarKRaveR (Sven)                           mailto:[email protected]

P.S.: In Deutsch wärs einfacher ;-) und wenn etwas unverständlich ist,
nochmal fragen, ich schlafe schon halb :-O