Gentoo Archives: gentoo-user

From: Grant Taylor <gtaylor@×××××××××××××××××××××.net>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] Bind to 127.0.0.N for any N
Date: Fri, 29 Jan 2021 01:32:35
Message-Id: c0ccdb34-7d8d-c5c8-0f33-8737d0c63bc9@spamtrap.tnetconsulting.net
In Reply to: [gentoo-user] Bind to 127.0.0.N for any N by Grant Edwards
1 On 1/28/21 5:38 PM, Grant Edwards wrote:
2 > I've just recently realized something about the "lo" interface.
3
4 I don't think this is as much about the interface as it is the routes
5 that are created. (More below.)
6
7 > You can bind a socket to any 127.0.0.N address, even though only
8 > 127.0.0.1/8 is configured in /etc/config/net, and "ip addr" only shows
9 > 127.0.0.1/8 for that interface.
10
11 Yes. But for specific reasons. (...)
12
13 > In the past, when I wanted to use other 127.0.0.N address, I
14 > always added them to /etc/config/net. The last time, I forget to do
15 > that. Later, I realized it was working anyway. I've since removed
16 > all of the extra "lo" addresses from /etc/config/net, and everything
17 > still works.
18
19 Because of a very special route.
20
21 > Apparently "lo" is special.
22 >
23 > Perhaps I don't even need to have 127.0.0.1/8 listed in
24 > /etc/config/net...
25
26 I think that you still want 127.0.0.1 in /etc/config/net even if only to
27 bring the interface up (a la 'ip link set dev lo up', sans IP).
28
29 I believe the ""magic that is allowing this to work is one of the four
30 following routes:
31
32 # ip route show table local | grep 127.0.0 | nl
33 1 broadcast 127.0.0.0 dev lo proto kernel scope link src 127.0.0.1
34 2 local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
35 3 local 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1
36 4 broadcast 127.255.255.255 dev lo proto kernel scope link src
37 127.0.0.1
38
39 Lines 1, 3, and 4, are typical routes. You should have something
40 similar for other IPs and devices.
41
42 But line 2 is very special. Notice how it's assigning the entire 127/8
43 to the lo device.
44
45 Reformatting the route with some white space makes it somewhat more obvious.
46
47 2 local 127.0.0.0/8 dev lo proto kernel scope host src 127.0.0.1
48 3 local 127.0.0.1 dev lo proto kernel scope host src 127.0.0.1
49
50 #3 is a more typical /host/ route.
51 #2 is a less typical /net/ route.
52
53 #2 actually tells the kernel that anything and everything in the 127/8
54 destination network can be reached directly via the lo adapter.
55
56 This network route is more efficient than having multiple host routes to
57 cover some portion of the same IP space.
58
59 My understanding -- which may be wrong, and please correct me if you
60 think it is -- is that this special route (#2) is how the kernel sends
61 the entire 127/8 network to the lo adapter, even if the IP addresses
62 aren't bound to the adapter.
63
64 Now, as for things receiving the connections, I think it is highly
65 dependent on if the thing is listening to 0.0.0.0 or specific IP
66 addresses. Because if it's listening to 0.0.0.0, I think it will
67 happily serve connections to other addresses in 127/8. If it's
68 listening to explicitly 127.0.0.1, then it likely will not serve
69 connections to other addresses in 127/8.
70
71 I believe the same technique can be applied to other addresses outside
72 of the 127/8 network. Though it's much less often done. You'd most
73 likely see this with a service that wants to serve for an entire /24;
74 e.g. 192.0.2.0/24 while listening to 0.0.0.0.
75
76 Admittely it's been a while since I last delt with this, so I could be
77 mis-remembering. But I think the special route, #2, is at the root of
78 what you're asking about.
79
80 Again, I believe you do want the 127.0.0.1 in /etc/config/net to
81 actually bring the interface up. You probably don't even need to bind
82 an IP to it. I think the kernel does the 127/8 automatically /if/ the
83 interface is simply up, a la 'ip link set dev lo up'.
84
85
86
87 --
88 Grant. . . .
89 unix || die

Replies

Subject Author
[gentoo-user] Re: Bind to 127.0.0.N for any N Grant Edwards <grant.b.edwards@×××××.com>