Gentoo Archives: gentoo-portage-dev

From: Mike Gilbert <floppym@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] [PATCH] Use RTNETLINK to configure the loopback interface
Date: Tue, 27 Aug 2019 03:03:07
Message-Id: 20190827030301.1184071-1-floppym@gentoo.org
1 This eliminates the dependency on iproute2 on Linux.
2
3 Signed-off-by: Mike Gilbert <floppym@g.o>
4 ---
5 lib/portage/process.py | 25 ++++------
6 lib/portage/util/netlink.py | 91 +++++++++++++++++++++++++++++++++++++
7 2 files changed, 100 insertions(+), 16 deletions(-)
8 create mode 100644 lib/portage/util/netlink.py
9
10 diff --git a/lib/portage/process.py b/lib/portage/process.py
11 index 2a2cbd972..bb4462c7f 100644
12 --- a/lib/portage/process.py
13 +++ b/lib/portage/process.py
14 @@ -10,7 +10,6 @@ import multiprocessing
15 import platform
16 import signal
17 import socket
18 -import struct
19 import subprocess
20 import sys
21 import traceback
22 @@ -489,17 +488,6 @@ def _configure_loopback_interface():
23 Configure the loopback interface.
24 """
25
26 - IFF_UP = 0x1
27 - ifreq = struct.pack('16sh', b'lo', IFF_UP)
28 - SIOCSIFFLAGS = 0x8914
29 -
30 - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
31 - try:
32 - fcntl.ioctl(sock, SIOCSIFFLAGS, ifreq)
33 - except IOError as e:
34 - writemsg("Unable to enable loopback interface: %s\n" % e.strerror, noiselevel=-1)
35 - sock.close()
36 -
37 # We add some additional addresses to work around odd behavior in glibc's
38 # getaddrinfo() implementation when the AI_ADDRCONFIG flag is set.
39 #
40 @@ -514,12 +502,17 @@ def _configure_loopback_interface():
41 # Bug: https://bugs.gentoo.org/690758
42 # Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=12377#c13
43
44 + from portage.util.netlink import RtNetlink
45 +
46 try:
47 - subprocess.call(['ip', 'address', 'add', '10.0.0.1/8', 'dev', 'lo'])
48 - if _has_ipv6():
49 - subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo'])
50 + with RtNetlink() as rtnl:
51 + ifindex = rtnl.get_link_ifindex(b'lo')
52 + rtnl.set_link_up(ifindex)
53 + rtnl.add_address(ifindex, socket.AF_INET, '10.0.0.1', 8)
54 + if _has_ipv6():
55 + rtnl.add_address(ifindex, socket.AF_INET6, 'fd::1', 8)
56 except EnvironmentError as e:
57 - writemsg("Error calling 'ip': %s\n" % e.strerror, noiselevel=-1)
58 + writemsg("Unable to configure loopback interface: %s\n" % e.strerror, noiselevel=-1)
59
60 def _exec(binary, mycommand, opt_name, fd_pipes,
61 env, gid, groups, uid, umask, cwd,
62 diff --git a/lib/portage/util/netlink.py b/lib/portage/util/netlink.py
63 new file mode 100644
64 index 000000000..5b18b8f95
65 --- /dev/null
66 +++ b/lib/portage/util/netlink.py
67 @@ -0,0 +1,91 @@
68 +# Copyright 2019 Gentoo Authors
69 +# Distributed under the terms of the GNU General Public License v2
70 +
71 +from io import BytesIO
72 +from os import strerror
73 +from struct import Struct
74 +
75 +from socket import (
76 + AF_NETLINK, AF_UNSPEC,
77 + MSG_PEEK, MSG_TRUNC,
78 + NETLINK_ROUTE,
79 + SOCK_DGRAM,
80 + inet_pton, socket,
81 +)
82 +
83 +IFA_LOCAL = 2
84 +IFF_UP = 0x1
85 +IFLA_IFNAME = 3
86 +NLMSG_ERROR = 2
87 +RTM_NEWLINK = 16
88 +RTM_GETLINK = 18
89 +RTM_NEWADDR = 20
90 +NLM_F_REQUEST = 0x1
91 +NLM_F_ACK = 0x4
92 +NLM_F_EXCL = 0x200
93 +NLM_F_CREATE = 0x400
94 +
95 +nlmsghdr = Struct('=IHHII')
96 +nlmsgerr = Struct('i')
97 +rtattr = Struct('HH')
98 +ifinfomsg = Struct('BHiII')
99 +ifaddrmsg = Struct('BBBBi')
100 +
101 +def create_nlmsg(nlmsg_type, nlmsg_flags, nlmsg_seq, nlmsg_pid, data):
102 + nlmsg_len = nlmsghdr.size + len(data)
103 + return nlmsghdr.pack(nlmsg_len, nlmsg_type, nlmsg_flags, nlmsg_seq, nlmsg_pid) + data
104 +
105 +def create_rtattr(rta_type, data):
106 + rta_len = rtattr.size + len(data)
107 + return rtattr.pack(rta_len, rta_type) + data
108 +
109 +def parse_message(msg):
110 + buf = BytesIO(msg)
111 + hdr = nlmsghdr.unpack(buf.read(nlmsghdr.size))
112 + if hdr[1] == NLMSG_ERROR:
113 + err = nlmsgerr.unpack(buf.read(nlmsgerr.size))
114 + error = -err[0]
115 + if error != 0:
116 + raise OSError(error, strerror(error))
117 + elif hdr[1] == RTM_NEWLINK:
118 + return ifinfomsg.unpack(buf.read(ifinfomsg.size))
119 +
120 +class RtNetlink:
121 + def __init__(self):
122 + self.sock = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)
123 + self.addr = (0, 0)
124 + self.sock.bind(self.addr)
125 +
126 + def __enter__(self):
127 + return self
128 +
129 + def __exit__(self, exc_type, exc_value, traceback):
130 + self.sock.close()
131 +
132 + def send_message(self, msg):
133 + self.sock.sendto(msg, self.addr)
134 + # Messages are variable length, but 128 is enough for the the ones we care about.
135 + resp = self.sock.recv(128)
136 + return parse_message(resp)
137 +
138 + def get_link_ifindex(self, ifname):
139 + body = ifinfomsg.pack(AF_UNSPEC, 0, 0, 0, 0)
140 + body += create_rtattr(IFLA_IFNAME, ifname)
141 + flags = NLM_F_REQUEST
142 + msg = create_nlmsg(RTM_GETLINK, flags, 1, 0, body)
143 + resp = self.send_message(msg)
144 + return resp[2]
145 +
146 + def set_link_up(self, ifindex):
147 + body = ifinfomsg.pack(AF_UNSPEC, 0, ifindex, IFF_UP, IFF_UP)
148 + flags = NLM_F_REQUEST|NLM_F_ACK
149 + msg = create_nlmsg(RTM_NEWLINK, flags, 1, 0, body)
150 + self.send_message(msg)
151 +
152 + def add_address(self, ifindex, family, address, prefixlen):
153 + body = ifaddrmsg.pack(family, prefixlen, 0, 0, ifindex)
154 + addr = inet_pton(family, address)
155 + body += create_rtattr(IFA_LOCAL, addr)
156 + flags = NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE
157 + msg = create_nlmsg(RTM_NEWADDR, flags, 1, 0, body)
158 + self.send_message(msg)
159 --
160 2.23.0

Replies