Debian Codegraph @

src/nspawn/nspawn-expose-ports.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "sd-netlink.h"
4
5 #include "af-list.h"
6 #include "alloc-util.h"
7 #include "fd-util.h"
8 #include "firewall-util.h"
9 #include "in-addr-util.h"
10 #include "local-addresses.h"
11 #include "log.h"
12 #include "nspawn-expose-ports.h"
13 #include "parse-util.h"
14 #include "socket-util.h"
15 #include "string-util.h"
16
17 int expose_port_parse(ExposePort **l, const char *s) {
18 const char *split, *e;
19 uint16_t container_port, host_port;
20 ExposePort *port;
21 int protocol;
22 int r;
23
24 assert(l);
25 assert(s);
26
27 if ((e = startswith(s, "tcp:")))
28 protocol = IPPROTO_TCP;
29 else if ((e = startswith(s, "udp:")))
30 protocol = IPPROTO_UDP;
31 else {
32 e = s;
33 protocol = IPPROTO_TCP;
34 }
35
36 split = strchr(e, ':');
37 if (split) {
38 char v[split - e + 1];
39
40 memcpy(v, e, split - e);
41 v[split - e] = 0;
42
43 r = parse_ip_port(v, &host_port);
44 if (r < 0)
45 return -EINVAL;
46
47 r = parse_ip_port(split + 1, &container_port);
48 } else {
49 r = parse_ip_port(e, &container_port);
50 host_port = container_port;
51 }
52
53 if (r < 0)
54 return r;
55
56 LIST_FOREACH(ports, p, *l)
57 if (p->protocol == protocol && p->host_port == host_port)
58 return -EEXIST;
59
60 port = new(ExposePort, 1);
61 if (!port)
62 return -ENOMEM;
63
64 *port = (ExposePort) {
65 .protocol = protocol,
66 .host_port = host_port,
67 .container_port = container_port,
68 };
69
70 LIST_PREPEND(ports, *l, port);
71
72 return 0;
73 }
74
75 void expose_port_free_all(ExposePort *p) {
76 LIST_CLEAR(ports, p, free);
77 }
78
79 int expose_port_flush(sd_netlink *nfnl, ExposePort* l, int af, union in_addr_union *exposed) {
80 int r;
81
82 assert(IN_SET(af, AF_INET, AF_INET6));
83 assert(exposed);
84
85 if (!nfnl || !l)
86 return 0;
87
88 if (!in_addr_is_set(af, exposed))
89 return 0;
90
91 log_debug("Lost IP address.");
92
93 LIST_FOREACH(ports, p, l) {
94 r = fw_nftables_add_local_dnat(
95 nfnl,
96 /* add= */ false,
97 af,
98 p->protocol,
99 p->host_port,
100 exposed,
101 p->container_port,
102 /* previous_remote= */ NULL);
103 if (r < 0)
104 log_warning_errno(r, "Failed to modify %s firewall: %m", af_to_name(af));
105 }
106
107 *exposed = IN_ADDR_NULL;
108 return 0;
109 }
110
111 int expose_port_execute(sd_netlink *rtnl, sd_netlink *nfnl, ExposePort *l, int af, union in_addr_union *exposed) {
112 _cleanup_free_ struct local_address *addresses = NULL;
113 union in_addr_union new_exposed;
114 bool add;
115 int r;
116
117 assert(rtnl);
118 assert(nfnl);
119 assert(IN_SET(af, AF_INET, AF_INET6));
120 assert(exposed);
121
122 /* Invoked each time an address is added or removed inside the
123 * container */
124
125 if (!l)
126 return 0;
127
128 r = local_addresses(rtnl, 0, af, &addresses);
129 if (r < 0)
130 return log_error_errno(r, "Failed to enumerate local addresses: %m");
131
132 add = r > 0 &&
133 addresses[0].family == af &&
134 addresses[0].scope < RT_SCOPE_LINK;
135
136 if (!add)
137 return expose_port_flush(nfnl, l, af, exposed);
138
139 new_exposed = addresses[0].address;
140 if (in_addr_equal(af, exposed, &new_exposed))
141 return 0;
142
143 log_debug("New container IP is %s.", IN_ADDR_TO_STRING(af, &new_exposed));
144
145 LIST_FOREACH(ports, p, l) {
146 r = fw_nftables_add_local_dnat(
147 nfnl,
148 /* add= */ true,
149 af,
150 p->protocol,
151 p->host_port,
152 &new_exposed,
153 p->container_port,
154 in_addr_is_set(af, exposed) ? exposed : NULL);
155 if (r < 0)
156 log_warning_errno(r, "Failed to modify %s firewall: %m", af_to_name(af));
157 }
158
159 *exposed = new_exposed;
160 return 0;
161 }
162
163 int expose_port_send_rtnl(int send_fd) {
164 _cleanup_close_ int fd = -EBADF;
165 int r;
166
167 assert(send_fd >= 0);
168
169 fd = socket(AF_NETLINK, SOCK_RAW|SOCK_CLOEXEC|SOCK_NONBLOCK, NETLINK_ROUTE);
170 if (fd < 0)
171 return log_error_errno(errno, "Failed to allocate container netlink: %m");
172
173 /* Store away the fd in the socket, so that it stays open as
174 * long as we run the child */
175 r = send_one_fd(send_fd, fd, 0);
176 if (r < 0)
177 return log_error_errno(r, "Failed to send netlink fd: %m");
178
179 return 0;
180 }
181
182 int expose_port_watch_rtnl(
183 sd_event *event,
184 int recv_fd,
185 sd_netlink_message_handler_t handler,
186 void *userdata,
187 sd_netlink **ret) {
188 _cleanup_(sd_netlink_unrefp) sd_netlink *rtnl = NULL;
189 int fd, r;
190
191 assert(event);
192 assert(recv_fd >= 0);
193 assert(ret);
194
195 fd = receive_one_fd(recv_fd, 0);
196 if (fd < 0)
197 return log_error_errno(fd, "Failed to recv netlink fd: %m");
198
199 r = sd_netlink_open_fd(&rtnl, fd);
200 if (r < 0) {
201 safe_close(fd);
202 return log_error_errno(r, "Failed to create rtnl object: %m");
203 }
204
205 r = sd_netlink_add_match(rtnl, NULL, RTM_NEWADDR, handler, NULL, userdata, "nspawn-NEWADDR");
206 if (r < 0)
207 return log_error_errno(r, "Failed to subscribe to RTM_NEWADDR messages: %m");
208
209 r = sd_netlink_add_match(rtnl, NULL, RTM_DELADDR, handler, NULL, userdata, "nspawn-DELADDR");
210 if (r < 0)
211 return log_error_errno(r, "Failed to subscribe to RTM_DELADDR messages: %m");
212
213 r = sd_netlink_attach_event(rtnl, event, 0);
214 if (r < 0)
215 return log_error_errno(r, "Failed to add to event loop: %m");
216
217 *ret = TAKE_PTR(rtnl);
218
219 return 0;
220 }