Skip to contentSkip to conversation

iptables 1.3.6 not using /etc/networks

DISCUSSION Laurence J. Lane 9 participants 27 messages Nov 12, 2006 – Nov 29, 2006 netfilter

lore
Share & tools
lore
#0 lore raw

http://bugs.debian.org/398082

iptables 1.3.5 and 1.3.6 appear to read /etc/networks, but the information is lost somewhere with 1.3.6.

 # cat /etc/networks
 foonet 10.0.0.0
 # strace -s 255 -o /tmp/foo iptables -v -A INPUT -s foonet/8 -j
ACCEPT #1.3.5 [1]
 ACCEPT  all opt -- in * out *  10.0.0.0/8  -> 0.0.0.0/0
 # strace -s 255 -o /tmp/bar iptables -v -A INPUT -s foonet/8 -j
ACCEPT #1.3.6 [2]
 iptables v1.3.6: host/network `foonet.0.0.0' not found
 Try `iptables -h' or 'iptables --help' for more information.

1. http://people.debian.org/~ljlane/stuff/strace-iptables-1.3.5.txt
2. http://people.debian.org/~ljlane/stuff/strace-iptables-1.3.6.txt

#1 lore raw
Quoted 4 lines · Laurence J. Lane
  # strace -s 255 -o /tmp/bar iptables -v -A INPUT -s foonet/8 -j
ACCEPT #1.3.6 [2]
  iptables v1.3.6: host/network `foonet.0.0.0' not found
  Try `iptables -h' or 'iptables --help' for more information.

This looks like something with the pad_cidr() call in parse_hostnetworkmask(). ltrace shows the code calling getnetbyname("foonet.0.0.0") instead of getentbyname("foonet").

#2 lore raw
Quoted 10 lines · Laurence J. Lane
Quoted 4 lines · Laurence J. Lane
 # strace -s 255 -o /tmp/bar iptables -v -A INPUT -s foonet/8 -j
ACCEPT #1.3.6 [2]
 iptables v1.3.6: host/network `foonet.0.0.0' not found
 Try `iptables -h' or 'iptables --help' for more information.

This looks like something with the pad_cidr() call in parse_hostnetworkmask(). ltrace shows the code calling getnetbyname("foonet.0.0.0") instead of getentbyname("foonet").

Correct. This was added between 1.3.5 and 1.3.6 to more sanely handle CIDR notation. See the commit:

https://lists.netfilter.org/pipermail/netfilter-cvslog/2006-July/005122.html

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

Phil

#3 lore raw
Quoted 12 lines · Phil Oester
Quoted 10 lines · Laurence J. Lane
Quoted 4 lines · Laurence J. Lane
 # strace -s 255 -o /tmp/bar iptables -v -A INPUT -s foonet/8 -j
ACCEPT #1.3.6 [2]
 iptables v1.3.6: host/network `foonet.0.0.0' not found
 Try `iptables -h' or 'iptables --help' for more information.

This looks like something with the pad_cidr() call in parse_hostnetworkmask(). ltrace shows the code calling getnetbyname("foonet.0.0.0") instead of getentbyname("foonet").

Quoted 7 lines

Correct. This was added between 1.3.5 and 1.3.6 to more sanely handle CIDR notation. See the commit:

https://lists.netfilter.org/pipermail/netfilter-cvslog/2006-July/005122.html

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

Isn't it always true that foonet/y starts with a letter and x.x.x/y starts with a digit? In this case, checking that the string starts with a digit before searching it for '.' characters and appending '.0's should work.

Alexey

#4 lore raw
Quoted 6 lines · Alexey Toptygin
Quoted 2 lines

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

Isn't it always true that foonet/y starts with a letter and x.x.x/y starts with a digit? In this case, checking that the string starts with a digit before searching it for '.' characters and appending '.0's should work.

But if you use a FQDN such as www.domain.com/24, then shouldn't that then imply <ip of www.domain.com>/24? That's why I didn't make the exception for letters vs. digits, as it could be used either way.

/etc/networks certainly throws a wrench in the works.

Phil

#5 lore raw
Quoted 11 lines · Phil Oester
Quoted 6 lines · Alexey Toptygin
Quoted 2 lines

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

Isn't it always true that foonet/y starts with a letter and x.x.x/y starts with a digit? In this case, checking that the string starts with a digit before searching it for '.' characters and appending '.0's should work.

But if you use a FQDN such as www.domain.com/24, then shouldn't that then imply <ip of www.domain.com>/24? That's why I didn't make the exception for letters vs. digits, as it could be used either way.

I don't understand what you mean. I think if it starts with a digit, it must be an IP (or part of an IP with 0's dropped), else it is a network name or a domain name (since neither of those can start with digits). If it's an IP by the above logic, then pad it with '.0's as necessary (or translate directly into a number without padding first). If it's not an IP, first call getnetbyname on it and if that returns NULL call gethostbyname. I think this algorithm works in all cases, unless I'm missing something.

Alexey

#6 lore raw
Quoted 2 lines · Alexey Toptygin

I don't understand what you mean. I think if it starts with a digit, it must be an IP (or part of an IP with 0's dropped), else it is a network name or a domain name (since neither of those can start with digits). If it's an IP by the above logic, then pad it with '.0's as necessary (or translate directly into a number without padding first). If it's not an IP, first call getnetbyname on it and if that returns NULL call gethostbyname. I think this algorithm works in all cases, unless I'm missing something.

Domains can legitimately start with digits. F.i. 9292ov.nl. However, checking for a valid IP address (in all forms) should be trivial.

M4

#7 lore raw
Quoted 5 lines · Martijn Lievaart
Quoted 1 line · Alexey Toptygin

I don't understand what you mean. I think if it starts with a digit, it must be an IP (or part of an IP with 0's dropped), else it is a network name or a domain name (since neither of those can start with digits). If it's an IP by the above logic, then pad it with '.0's as necessary (or translate directly into a number without padding first). If it's not an IP, first call getnetbyname on it and if that returns NULL call gethostbyname. I think this algorithm works in all cases, unless I'm missing something.

Domains can legitimately start with digits. F.i. 9292ov.nl. However, checking for a valid IP address (in all forms) should be trivial.

No, domains can't start with a digit. See RFC 1034:

ftp://ftp.rfc-editor.org/in-notes/rfc1034.txt

Quoted 10 lines

From which I quote:

<domain> ::= <subdomain> | " "

<subdomain> ::= <label> | <subdomain> "." <label>

<label> ::= <letter> [ [ <ldh-str> ] <let-dig> ]

<ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str>

Quoted 8 lines

<let-dig-hyp> ::= <let-dig> | "-"

<let-dig> ::= <letter> | <digit>

<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case

<digit> ::= any one of the ten digits 0 through 9

Alexey

#8 lore raw
Quoted 7 lines · Alexey Toptygin
Quoted 5 lines · Martijn Lievaart
Quoted 1 line · Alexey Toptygin

I don't understand what you mean. I think if it starts with a digit, it must be an IP (or part of an IP with 0's dropped), else it is a network name or a domain name (since neither of those can start with digits). If it's an IP by the above logic, then pad it with '.0's as necessary (or translate directly into a number without padding first). If it's not an IP, first call getnetbyname on it and if that returns NULL call gethostbyname. I think this algorithm works in all cases, unless I'm missing something.

Quoted 7 lines
Quoted 1 line

Domains can legitimately start with digits. F.i. 9292ov.nl. However, checking for a valid IP address (in all forms) should be trivial.

No, domains can't start with a digit. See RFC 1034:

ftp://ftp.rfc-editor.org/in-notes/rfc1034.txt

Domains can and do start with a digit, see RFC1912 section 2.1. Think 3com.com.

M4

#9 lore raw
Quoted 5 lines · Martijn Lievaart
Quoted 4 lines

No, domains can't start with a digit. See RFC 1034:

ftp://ftp.rfc-editor.org/in-notes/rfc1034.txt

Domains can and do start with a digit, see RFC1912 section 2.1. Think 3com.com.

This is news to me, and isn't reflected in the current standard (which is still RFC 1034), but you are right that it's widely implemented. It seems wierd that the Host Requirements standard can override the DNS standard without the DNS standard being updated...

Alexey

#10 lore raw

offtopic Re: iptables 1.3.6 not using /etc/networks

* Alexey Toptygin wrote, On 13/11/06 21:23:

Quoted 6 lines · Martijn Lievaart
Quoted 5 lines
Quoted 5 lines

No, domains can't start with a digit. See RFC 1034:

ftp://ftp.rfc-editor.org/in-notes/rfc1034.txt

Quoted 7 lines
Quoted 2 lines

Domains can and do start with a digit, see RFC1912 section 2.1. Think 3com.com.

This is news to me, and isn't reflected in the current standard (which is still RFC 1034), but you are right that it's widely implemented. It seems wierd that the Host Requirements standard can override the DNS standard without the DNS standard being updated...

It's the same with SMTP, emails contain . in the left-hand-side despite it being counter-RFC for years.

Perhaps the self depracating title of the standard has something to do with it... anyway...

Sam

#11 lore raw
In reply to Alexey Toptygin
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

"AT" == Alexey Toptygin <[email protected]> writes:

AT> No, domains can't start with a digit. See RFC 1034:

AT> ftp://ftp.rfc-editor.org/in-notes/rfc1034.txt

Quoted 4 lines
Quoted 4 lines

From which I quote:

<domain> ::= <subdomain> | " "

You forgot to quote the section heading:

Quoted 1 line
Quoted 1 line

3.5. Preferred name syntax

You also forgot this bit:

Quoted 2 lines
Quoted 2 lines

The following syntax will result in fewer problems with many applications that use domain names (e.g., mail, TELNET).

as well as numerous other places in that section, where it is made very clear that the syntax is merely a recommendation.

RFC 1034 is probably the most misread RFC in existence. Please do not contribute further to this problem.

/Benny

#12 lore raw
In reply to Alexey Toptygin
Quoted 4 lines · Alexey Toptygin
Quoted 3 lines

But if you use a FQDN such as www.domain.com/24, then shouldn't that then imply <ip of www.domain.com>/24? That's why I didn't make the exception for letters vs. digits, as it could be used either way.

Quoted 8 lines

I don't understand what you mean. I think if it starts with a digit, it must be an IP (or part of an IP with 0's dropped), else it is a network name or a domain name (since neither of those can start with digits). If it's an IP by the above logic, then pad it with '.0's as necessary (or translate directly into a number without padding first). If it's not an IP, first call getnetbyname on it and if that returns NULL call gethostbyname. I think this algorithm works in all cases, unless I'm missing something.

What I meant was some people might want to include the /24 a host sits on, and use something like "mydomain.com/24". When the name gets translated to 1.2.3.4, the cidr would make it 1.2.3.0/24.

Also, as Martijn points out, just starting with digit doesn't imply an IP, as hosts can start with digits also.

The difficulty here is we can't easily have /etc/networks be processed and have shorthand CIDR notation (such as 10.10.10/24) work.

Phil

#13 lore raw
Quoted 14 lines · Phil Oester
Quoted 4 lines · Alexey Toptygin
Quoted 3 lines

But if you use a FQDN such as www.domain.com/24, then shouldn't that then imply <ip of www.domain.com>/24? That's why I didn't make the exception for letters vs. digits, as it could be used either way.

Quoted 8 lines

I don't understand what you mean. I think if it starts with a digit, it must be an IP (or part of an IP with 0's dropped), else it is a network name or a domain name (since neither of those can start with digits). If it's an IP by the above logic, then pad it with '.0's as necessary (or translate directly into a number without padding first). If it's not an IP, first call getnetbyname on it and if that returns NULL call gethostbyname. I think this algorithm works in all cases, unless I'm missing something.

Quoted 6 lines

What I meant was some people might want to include the /24 a host sits on, and use something like "mydomain.com/24". When the name gets translated to 1.2.3.4, the cidr would make it 1.2.3.0/24.

Also, as Martijn points out, just starting with digit doesn't imply an IP, as hosts can start with digits also.

I think my mail server ate my replies to this, so here it is a third time. Sorry if this is a duplicate; if it is, please let me know and I'll shut up (my incoming mail seems to be working fine). DNS domain names are not allowed to start with digits; I quote RFC 1034:

Quoted 9 lines

<domain> ::= <subdomain> | " "
<subdomain> ::= <label> | <subdomain> "." <label> <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ] <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str> <let-dig-hyp> ::= <let-dig> | "-"
<let-dig> ::= <letter> | <digit>
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
<digit> ::= any one of the ten digits 0 through 9

Thus, if the first character is a digit, the string cannot be a domain name.
I guess it could still be a network name in /etc/networks, but that would be a bit pathological. If you still don't like this approach, then how about:

1) parse and remove any trailing /x
2) try to parse string you get from step 1 as a (partial) IP address. If it parses OK, it's an IP, otherwise
3) look the string you get from step 1 (with no added .0s from step 2, if any) up via getnetbyname. If that returns non-NULL, use this result, otherwise
4) look the string you get from step 1 (with no added .0s from step 2, if any) up via gethostbyname. If that returns non-NULL, use that, otherwise
5) fail.
Alexey

#14 lore raw
Quoted 16 lines · Alexey Toptygin
Quoted 14 lines · Phil Oester
Quoted 12 lines · Alexey Toptygin
Quoted 3 lines

But if you use a FQDN such as www.domain.com/24, then shouldn't that then imply <ip of www.domain.com>/24? That's why I didn't make the exception for letters vs. digits, as it could be used either way.

I don't understand what you mean. I think if it starts with a digit, it must be an IP (or part of an IP with 0's dropped), else it is a network name or a domain name (since neither of those can start with digits). If it's an IP by the above logic, then pad it with '.0's as necessary (or translate directly into a number without padding first). If it's not an IP, first call getnetbyname on it and if that returns NULL call gethostbyname. I think this algorithm works in all cases, unless I'm missing something.

Quoted 19 lines
Quoted 6 lines

What I meant was some people might want to include the /24 a host sits on, and use something like "mydomain.com/24". When the name gets translated to 1.2.3.4, the cidr would make it 1.2.3.0/24.

Also, as Martijn points out, just starting with digit doesn't imply an IP, as hosts can start with digits also.

I think my mail server ate my replies to this, so here it is a third time. Sorry if this is a duplicate; if it is, please let me know and I'll shut up (my incoming mail seems to be working fine). DNS domain names are not allowed to start with digits; I quote RFC 1034:

Quoted 9 lines

<domain> ::= <subdomain> | " "
<subdomain> ::= <label> | <subdomain> "." <label> <label> ::= <letter> [ [ <ldh-str> ] <let-dig> ] <ldh-str> ::= <let-dig-hyp> | <let-dig-hyp> <ldh-str> <let-dig-hyp> ::= <let-dig> | "-"
<let-dig> ::= <letter> | <digit>
<letter> ::= any one of the 52 alphabetic characters A through Z in upper case and a through z in lower case
<digit> ::= any one of the ten digits 0 through 9

the above is obsolete by rfc 1123, section 2.1, Host Names and Numbers. only a complete parse of the name will show that you have the ipv4 address instead (#.#.#.#):

"..., then a full syntactic check must be made, because a segment of a host domain name is now allowed to begin with a digit and could legally be entirely numeric (see Section 6.1.2.4). However, a valid host name can never have the dotted-decimal form #.#.#.#, since at least the highest-level component label will be alphabetic."

Quoted 14 lines

Thus, if the first character is a digit, the string cannot be a domain name.
I guess it could still be a network name in /etc/networks, but that would be a bit pathological. If you still don't like this approach, then how about:

1) parse and remove any trailing /x
2) try to parse string you get from step 1 as a (partial) IP address. If it parses OK, it's an IP, otherwise
3) look the string you get from step 1 (with no added .0s from step 2, if any) up via getnetbyname. If that returns non-NULL, use this result, otherwise
4) look the string you get from step 1 (with no added .0s from step 2, if any) up via gethostbyname. If that returns non-NULL, use that,
otherwise
5) fail.
Alexey

#15 lore raw
In reply to Phil Oester
Quoted 2 lines · Phil Oester

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

I have another bug report saying it breaks stuff from /etc/hosts too.

How about something like this? I assume valid IP characters are in the range of 0-9 and a dot. This will skip pad_cidr() if any characters outside of that range are encountered. Plain bad IP addresses are apparently validated elsewhere. Of course, I could be wrong about all of this.

iptables.c 2006-11-18 16:38:31.549789934 -0500 renamed from iptables.c.ORIG 2006-11-18 16:40:30.902915398 -0500 +3 -1
@@ -696,13 +696,15 @@
       char buf[256];
       char *p;
       int i, j, k, n;
+       char *vipchars = "0123456789.\0";
 
       strncpy(buf, name, sizeof(buf) - 1);
       buf[sizeof(buf) - 1] = '\0';
       if ((p = strrchr(buf, '/')) != NULL) {
               *p = '\0';
               addrp = parse_mask(p + 1);
-               if (strrchr(p + 1, '.') == NULL)
+               if (strrchr(p + 1, '.') == NULL
+                 && strspn(buf,vipchars) == strlen(buf))
                       pad_cidr(buf);
       } else
               addrp = parse_mask(NULL);

Simple tests:

 # echo "foonet0 3.2.1.0" > /etc/networks
 # for i in 8 16 24 32; do iptables -v -A INPUT -s foonet0/$i -j ACCEPT; done
 ACCEPT  0 opt -- in * out *  3.2.1.0  -> 0.0.0.0/0
 ACCEPT  0 opt -- in * out *  3.2.1.0/24  -> 0.0.0.0/0
 ACCEPT  0 opt -- in * out *  3.2.0.0/16  -> 0.0.0.0/0
 ACCEPT  0 opt -- in * out *  3.0.0.0/8  -> 0.0.0.0/0
 # echo 1.2.3.4 foo.bar.baz.quuz >> /etc/hosts
 # for i in 8 16 24 32; do iptables -v -A INPUT -s
foo.bar.baz.quuz/$i -j ACCEPT; done
 ACCEPT  0 opt -- in * out *  1.0.0.0/8  -> 0.0.0.0/0
 ACCEPT  0 opt -- in * out *  1.2.0.0/16  -> 0.0.0.0/0
 ACCEPT  0 opt -- in * out *  1.2.3.0/24  -> 0.0.0.0/0
 ACCEPT  0 opt -- in * out *  1.2.3.4  -> 0.0.0.0/0
#16 lore raw
Quoted 2 lines · Laurence J. Lane
  # echo "foonet0 3.2.1.0" > /etc/networks
  # for i in 8 16 24 32; do iptables -v -A INPUT -s foonet0/$i -j ACCEPT; done

I pasted the wrong line there. The list is clearly reversed.

#17 lore raw
In reply to Laurence J. Lane
Quoted 5 lines · Laurence J. Lane
Quoted 2 lines · Phil Oester

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

Quoted 7 lines

I have another bug report saying it breaks stuff from /etc/hosts too.

How about something like this? I assume valid IP characters are in the range of 0-9 and a dot. This will skip pad_cidr() if any characters outside of that range are encountered. Plain bad IP addresses are apparently validated elsewhere. Of course, I could be wrong about all of this.

Apparently /etc/host accepts entries composed of dots, e.g.

foo.machine 192.168.100.100

So this assumption can be OK as soon as nobody is using such notation.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
#18 lore raw
Quoted 17 lines · Pablo Neira Ayuso
Quoted 6 lines · Laurence J. Lane
Quoted 2 lines · Phil Oester

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

Quoted 7 lines

I have another bug report saying it breaks stuff from /etc/hosts too.

How about something like this? I assume valid IP characters are in the range of 0-9 and a dot. This will skip pad_cidr() if any characters outside of that range are encountered. Plain bad IP addresses are apparently validated elsewhere. Of course, I could be wrong about all of this.

Quoted 5 lines

Apparently /etc/host accepts entries composed of dots, e.g.

foo.machine 192.168.100.100

So this assumption can be OK as soon as nobody is using such notation.

I haven't followed this fully, but seen a lot of patches floating around. Is there already some consensus on which patch to use?

#19 lore raw
Quoted 2 lines · Patrick McHardy

I haven't followed this fully, but seen a lot of patches floating around. Is there already some consensus on which patch to use?

I posted a patch that fixes the problem although I didn't get any feedback from people.

Signature · 1 line
The dawn of the fourth age of Linux firewalling is coming; a time of great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
#20 lore raw
Quoted 6 lines · Pablo Neira Ayuso
Quoted 2 lines · Patrick McHardy

I haven't followed this fully, but seen a lot of patches floating around. Is there already some consensus on which patch to use?

I posted a patch that fixes the problem although I didn't get any feedback from people.

Sorry Pablo, I haven't had time to review your patch yet. I'll take a closer look at it this weekend.

Phil

#21 lore raw
Quoted 8 lines · Phil Oester
Quoted 2 lines · Pablo Neira Ayuso

I posted a patch that fixes the problem although I didn't get any feedback from people.

Sorry Pablo, I haven't had time to review your patch yet. I'll take a closer look at it this weekend.

Thanks guys, please let me know when you want me to apply it. I want to make another iptables release soon because of the compilation errors with the endian annotations in 2.6.19.

#22 lore raw
Quoted 3 lines · Patrick McHardy

Thanks guys, please let me know when you want me to apply it. I want to make another iptables release soon because of the compilation errors with the endian annotations in 2.6.19.

I've reviewed Pablo's patch and it looks good to me. It handles the CIDR case nicely, as well as fixing up the breakage I introduced in /etc/{networks,hosts} handling.

Thanks Pablo!

Phil

#23 lore raw
Quoted 10 lines · Phil Oester
Quoted 3 lines · Patrick McHardy

Thanks guys, please let me know when you want me to apply it. I want to make another iptables release soon because of the compilation errors with the endian annotations in 2.6.19.

I've reviewed Pablo's patch and it looks good to me. It handles the CIDR case nicely, as well as fixing up the breakage I introduced in /etc/{networks,hosts} handling.

Thanks everyone, I've applied Pablo's patch.

#24 lore raw
In reply to Pablo Neira Ayuso

* Pablo Neira Ayuso wrote, On 20/11/06 00:00:

Quoted 13 lines
Quoted 11 lines · Laurence J. Lane
Quoted 2 lines · Phil Oester

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

I have another bug report saying it breaks stuff from /etc/hosts too.

How about something like this? I assume valid IP characters are in the range of 0-9 and a dot. This will skip pad_cidr() if any characters outside of that range are encountered. Plain bad IP addresses are apparently validated elsewhere. Of course, I could be wrong about all of this.

Quoted 5 lines

Apparently /etc/host accepts entries composed of dots, e.g.

foo.machine 192.168.100.100

So this assumption can be OK as soon as nobody is using such notation.

I do, so do lots of web developers I know; it helps them test websites under the correct domain.

Does the patch you suggested depend on this not being the case?

Sam

#25 lore raw
Quoted 15 lines · Amin Azez

* Pablo Neira Ayuso wrote, On 20/11/06 00:00:

Quoted 14 lines
Quoted 11 lines · Laurence J. Lane
Quoted 2 lines · Phil Oester

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

I have another bug report saying it breaks stuff from /etc/hosts too.

How about something like this? I assume valid IP characters are in the range of 0-9 and a dot. This will skip pad_cidr() if any characters outside of that range are encountered. Plain bad IP addresses are apparently validated elsewhere. Of course, I could be wrong about all of this.

Apparently /etc/host accepts entries composed of dots, e.g.

Quoted 8 lines
Quoted 3 lines

foo.machine 192.168.100.100

So this assumption can be OK as soon as nobody is using such notation.

I do, so do lots of web developers I know; it helps them test websites under the correct domain.

Does the patch you suggested depend on this not being the case?

No, because I considered that such assumption is wrong.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
#26 lore raw
In reply to Phil Oester
Quoted 11 lines · Phil Oester
Quoted 9 lines · Laurence J. Lane
Quoted 4 lines · Laurence J. Lane
 # strace -s 255 -o /tmp/bar iptables -v -A INPUT -s foonet/8 -j
ACCEPT #1.3.6 [2]
 iptables v1.3.6: host/network `foonet.0.0.0' not found
 Try `iptables -h' or 'iptables --help' for more information.

This looks like something with the pad_cidr() call in parse_hostnetworkmask(). ltrace shows the code calling getnetbyname("foonet.0.0.0") instead of getentbyname("foonet").

Quoted 7 lines

Correct. This was added between 1.3.5 and 1.3.6 to more sanely handle CIDR notation. See the commit:

https://lists.netfilter.org/pipermail/netfilter-cvslog/2006-July/005122.html

Not sure offhand how we can satisfy both cases here, but I'd posit that more people use x.x.x/24 than use foonet/x notation.

I think that we have to fix this because it also breaks hostnames defined in /etc/hosts, I have attached a patch to adress this issue.

Signature · 2 lines
The dawn of the fourth age of Linux firewalling is coming; a time of
great struggle and heroic deeds -- J.Kadlecsik got inspired by J.Morris
iptables.c +34 -35
Index: iptables.c
===================================================================
@@ -273,8 +273,13 @@
 		   "invalid port/service `%s' specified", port);
 }
 
-struct in_addr *
-dotted_to_addr(const char *dotted)
+enum {
+	IPT_DOTTED_ADDR = 0,
+	IPT_DOTTED_MASK
+};
+
+static struct in_addr *
+__dotted_to_addr(const char *dotted, int type)
 {
 	static struct in_addr addr;
 	unsigned char *addrp;
@@ -290,9 +295,21 @@
 
 	p = buf;
 	for (i = 0; i < 3; i++) {
-		if ((q = strchr(p, '.')) == NULL)
-			return (struct in_addr *) NULL;
+		if ((q = strchr(p, '.')) == NULL) {
+			if (type == IPT_DOTTED_ADDR) {
+				/* autocomplete, this is a network address */
+				if (string_to_number(p, 0, 255, &onebyte) == -1)
+					return (struct in_addr *) NULL;
 
+				addrp[i] = (unsigned char) onebyte;
+				while (i < 3)
+					addrp[++i] = 0;
+
+				return &addr;
+			} else
+				return (struct in_addr *) NULL;
+		}
+
 		*q = '\0';
 		if (string_to_number(p, 0, 255, &onebyte) == -1)
 			return (struct in_addr *) NULL;
@@ -310,6 +327,18 @@
 	return &addr;
 }
 
+struct in_addr *
+dotted_to_addr(const char *dotted)
+{
+	return __dotted_to_addr(dotted, IPT_DOTTED_ADDR);
+}
+
+struct in_addr *
+dotted_to_mask(const char *dotted)
+{
+	return __dotted_to_addr(dotted, IPT_DOTTED_MASK);
+}
+
 static struct in_addr *
 network_to_addr(const char *name)
 {
@@ -607,34 +636,6 @@
 	return (char *) NULL;
 }
 
-static void 
-pad_cidr(char *cidr)
-{
-	char *p, *q;
-	unsigned int onebyte;
-	int i, j;
-	char buf[20];
-
-	/* copy dotted string, because we need to modify it */
-	strncpy(buf, cidr, sizeof(buf) - 1);
-	buf[sizeof(buf) - 1] = '\0';
-
-	p = buf;
-	for (i = 0; i <= 3; i++) {
-		if ((q = strchr(p, '.')) == NULL)
-			break;
-		*q = '\0';
-		if (string_to_number(p, 0, 255, &onebyte) == -1)
-			return;
-		p = q + 1;
-	}
-
-	/* pad remaining octets with zeros */
-	for (j = i; j < 3; j++) {
-		strcat(cidr, ".0");
-	}
-}
-
 /*
  *	All functions starting with "parse" should succeed, otherwise
  *	the program fails.
@@ -674,7 +675,7 @@
 		maskaddr.s_addr = 0xFFFFFFFF;
 		return &maskaddr;
 	}
-	if ((addrp = dotted_to_addr(mask)) != NULL)
+	if ((addrp = dotted_to_mask(mask)) != NULL)
 		/* dotted_to_addr already returns a network byte order addr */
 		return addrp;
 	if (string_to_number(mask, 0, 32, &bits) == -1)
@@ -703,8 +704,6 @@
 	if ((p = strrchr(buf, '/')) != NULL) {
 		*p = '\0';
 		addrp = parse_mask(p + 1);
-		if (strrchr(p + 1, '.') == NULL)
-			pad_cidr(buf);
 	} else
 		addrp = parse_mask(NULL);
 	inaddrcpy(maskp, addrp);
libipt_NETMAP.c +1 -1
Index: extensions/libipt_NETMAP.c
===================================================================
@@ -86,7 +86,7 @@
 	range->min_ip = ip->s_addr;
 	if (slash) {
 		if (strchr(slash+1, '.')) {
-			ip = dotted_to_addr(slash+1);
+			ip = dotted_to_mask(slash+1);
 			if (!ip)
 				exit_error(PARAMETER_PROBLEM, "Bad netmask `%s'\n",
 					   slash+1);
iptables.h +1 -0
Index: include/iptables.h
===================================================================
@@ -154,6 +154,7 @@
 extern int service_to_port(const char *name, const char *proto);
 extern u_int16_t parse_port(const char *port, const char *proto);
 extern struct in_addr *dotted_to_addr(const char *dotted);
+extern struct in_addr *dotted_to_mask(const char *dotted);
 extern char *addr_to_dotted(const struct in_addr *addrp);
 extern char *addr_to_anyname(const struct in_addr *addr);
 extern char *mask_to_dotted(const struct in_addr *mask);