Gentoo Archives: gentoo-portage-dev

From: Mike Gilbert <floppym@g.o>
To: Zac Medico <zmedico@g.o>
Cc: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] Re: [PATCH] process: add _has_ipv6() function
Date: Sun, 04 Aug 2019 13:40:38
Message-Id: CAJ0EP436fD+ZuQJ66Lu1wgEsz_fR8we4FsbjB5h6S8nWWinAew@mail.gmail.com
In Reply to: [gentoo-portage-dev] Re: [PATCH] process: add _has_ipv6() function by Mike Gilbert
1 On Sun, Aug 4, 2019 at 9:34 AM Mike Gilbert <floppym@g.o> wrote:
2 >
3 > On Sat, Aug 3, 2019 at 7:37 PM Mike Gilbert <floppym@g.o> wrote:
4 > >
5 > > On Sat, Aug 3, 2019 at 3:30 PM Zac Medico <zmedico@g.o> wrote:
6 > > >
7 > > > Add _has_ipv6() function and use it in _configure_loopback_interface()
8 > > > to decide whether to add an IPv6 address.
9 > > >
10 > > > Bug: https://bugs.gentoo.org/691290
11 > > > ---
12 > > > lib/portage/process.py | 40 +++++++++++++++++++++++++++++++++++++---
13 > > > 1 file changed, 37 insertions(+), 3 deletions(-)
14 > > >
15 > > > diff --git a/lib/portage/process.py b/lib/portage/process.py
16 > > > index 690421815..ca8b0c172 100644
17 > > > --- a/lib/portage/process.py
18 > > > +++ b/lib/portage/process.py
19 > > > @@ -339,6 +339,9 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False,
20 > > > fd_pipes[1] = pw
21 > > > fd_pipes[2] = pw
22 > > >
23 > > > + # Cache _has_ipv6() result for use in child processes.
24 > > > + _has_ipv6()
25 > > > +
26 > > > # This caches the libc library lookup and _unshare_validator results
27 > > > # in the current process, so that results are cached for use in
28 > > > # child processes.
29 > > > @@ -446,6 +449,38 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False,
30 > > > # Everything succeeded
31 > > > return 0
32 > > >
33 > > > +__has_ipv6 = None
34 > > > +
35 > > > +def _has_ipv6():
36 > > > + """
37 > > > + Test that both userland and kernel support IPv6, by attempting
38 > > > + to create a socket and listen on any unused port of the IPv6
39 > > > + ::1 loopback address.
40 > > > +
41 > > > + @rtype: bool
42 > > > + @return: True if IPv6 is supported, False otherwise.
43 > > > + """
44 > > > + global __has_ipv6
45 > > > +
46 > > > + if __has_ipv6 is None:
47 > > > + if socket.has_ipv6:
48 > > > + sock = None
49 > > > + try:
50 > > > + # python2.7 sockets do not support context management protocol
51 > > > + sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM)
52 > > > + sock.bind(('::1', 0))
53 > > > + except EnvironmentError:
54 > > > + __has_ipv6 = False
55 > > > + else:
56 > > > + __has_ipv6 = True
57 > > > + finally:
58 > > > + if sock is not None:
59 > > > + sock.close()
60 > > > + else:
61 > > > + __has_ipv6 = False
62 > > > +
63 > > > + return __has_ipv6
64 > > > +
65 > > > def _configure_loopback_interface():
66 > > > """
67 > > > Configure the loopback interface.
68 > > > @@ -478,9 +513,8 @@ def _configure_loopback_interface():
69 > > >
70 > > > try:
71 > > > subprocess.call(['ip', 'address', 'add', '10.0.0.1/8', 'dev', 'lo'])
72 > > > - with open(os.devnull, 'wb', 0) as devnull:
73 > > > - subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo'],
74 > > > - stdout=devnull, stderr=devnull)
75 > > > + if _has_ipv6():
76 > > > + subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo'])
77 > > > except EnvironmentError as e:
78 > > > writemsg("Error calling 'ip': %s\n" % e.strerror, noiselevel=-1)
79 > > >
80 > > > --
81 > > > 2.21.0
82 > > >
83 > >
84 > > This seems reasonable, though I don't have an IPv6-less system to test it on.
85 >
86 > While chatting in #gentoo-desktop, we found that it is possible to
87 > have IPv6 enabled, but prohibit IPv6 addresses from being added to
88 > interfaces. This produces the following error from ip:
89 >
90 > RTNETLINK answers: Permission denied
91 >
92 > https://www.kernel.org/doc/Documentation/networking/ipv6.txt
93 >
94 > ipv6.disabled = 0
95 > ipv6.disable_ipv6 = 1
96 >
97 > I don't think your __has_ipv6 function will catch this.
98
99 Possibly the bind('::1', 0) call will fail if the loopback interface
100 doesn't have that address configured. This appears to be the case when
101 disable_ipv6 = 1.

Replies

Subject Author
[gentoo-portage-dev] Re: [PATCH] process: add _has_ipv6() function Mike Gilbert <floppym@g.o>