summaryrefslogtreecommitdiffstats
path: root/meta/recipes-core/systemd/systemd/network-fix-Link-reference-counter-issue.patch
diff options
context:
space:
mode:
Diffstat (limited to 'meta/recipes-core/systemd/systemd/network-fix-Link-reference-counter-issue.patch')
-rw-r--r--meta/recipes-core/systemd/systemd/network-fix-Link-reference-counter-issue.patch278
1 files changed, 278 insertions, 0 deletions
diff --git a/meta/recipes-core/systemd/systemd/network-fix-Link-reference-counter-issue.patch b/meta/recipes-core/systemd/systemd/network-fix-Link-reference-counter-issue.patch
new file mode 100644
index 0000000000..a186bb4095
--- /dev/null
+++ b/meta/recipes-core/systemd/systemd/network-fix-Link-reference-counter-issue.patch
@@ -0,0 +1,278 @@
1From cc2d7efc5ca09a7de4bec55e80476986839a655c Mon Sep 17 00:00:00 2001
2From: Yu Watanabe <watanabe.yu+github@gmail.com>
3Date: Fri, 14 May 2021 15:58:15 +0900
4Subject: [PATCH] network: fix Link reference counter issue
5
6Previously, when link_new() fails, `link_unref()` was called, so,
7`Manager::links` may become dirty.
8This introduces `link_drop_or_unref()` and it will be called on
9failure.
10
11Upstream-Status: Backport [https://github.com/systemd/systemd-stable/commit/cc2d7efc5ca09a7de4bec55e80476986839a655c]
12Signed-off-by: Ranjitsinh Rathod <ranjitsinh.rathod@kpit.com>
13
14---
15 src/network/networkd-link.c | 240 ++++++++++++++++++------------------
16 1 file changed, 122 insertions(+), 118 deletions(-)
17
18diff --git a/src/network/networkd-link.c b/src/network/networkd-link.c
19index b56c232eca..d493afda4c 100644
20--- a/src/network/networkd-link.c
21+++ b/src/network/networkd-link.c
22@@ -540,109 +540,6 @@ static int link_update_flags(Link *link,
23 return 0;
24 }
25
26-static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
27- _cleanup_(link_unrefp) Link *link = NULL;
28- uint16_t type;
29- const char *ifname, *kind = NULL;
30- int r, ifindex;
31- unsigned short iftype;
32-
33- assert(manager);
34- assert(message);
35- assert(ret);
36-
37- /* check for link kind */
38- r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
39- if (r == 0) {
40- (void) sd_netlink_message_read_string(message, IFLA_INFO_KIND, &kind);
41- r = sd_netlink_message_exit_container(message);
42- if (r < 0)
43- return r;
44- }
45-
46- r = sd_netlink_message_get_type(message, &type);
47- if (r < 0)
48- return r;
49- else if (type != RTM_NEWLINK)
50- return -EINVAL;
51-
52- r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
53- if (r < 0)
54- return r;
55- else if (ifindex <= 0)
56- return -EINVAL;
57-
58- r = sd_rtnl_message_link_get_type(message, &iftype);
59- if (r < 0)
60- return r;
61-
62- r = sd_netlink_message_read_string(message, IFLA_IFNAME, &ifname);
63- if (r < 0)
64- return r;
65-
66- link = new(Link, 1);
67- if (!link)
68- return -ENOMEM;
69-
70- *link = (Link) {
71- .n_ref = 1,
72- .manager = manager,
73- .state = LINK_STATE_PENDING,
74- .ifindex = ifindex,
75- .iftype = iftype,
76-
77- .n_dns = (unsigned) -1,
78- .dns_default_route = -1,
79- .llmnr = _RESOLVE_SUPPORT_INVALID,
80- .mdns = _RESOLVE_SUPPORT_INVALID,
81- .dnssec_mode = _DNSSEC_MODE_INVALID,
82- .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
83- };
84-
85- link->ifname = strdup(ifname);
86- if (!link->ifname)
87- return -ENOMEM;
88-
89- if (kind) {
90- link->kind = strdup(kind);
91- if (!link->kind)
92- return -ENOMEM;
93- }
94-
95- r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t *)&link->master_ifindex);
96- if (r < 0)
97- log_link_debug_errno(link, r, "New device has no master, continuing without");
98-
99- r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac);
100- if (r < 0)
101- log_link_debug_errno(link, r, "MAC address not found for new device, continuing without");
102-
103- if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0)
104- return -ENOMEM;
105-
106- if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0)
107- return -ENOMEM;
108-
109- if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0)
110- return -ENOMEM;
111-
112- r = hashmap_ensure_allocated(&manager->links, NULL);
113- if (r < 0)
114- return r;
115-
116- r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
117- if (r < 0)
118- return r;
119-
120- r = link_update_flags(link, message, false);
121- if (r < 0)
122- return r;
123-
124- *ret = TAKE_PTR(link);
125-
126- return 0;
127-}
128-
129 void link_ntp_settings_clear(Link *link) {
130 link->ntp = strv_free(link->ntp);
131 }
132@@ -2030,9 +1927,9 @@ static void link_drop_requests(Link *lin
133 request_drop(req);
134 }
135
136-void link_drop(Link *link) {
137+Link *link_drop(Link *link) {
138 if (!link)
139- return;
140+ return NULL;
141
142 assert(link->manager);
143
144@@ -2057,7 +1954,7 @@ void link_drop(Link *link) {
145
146 /* The following must be called at last. */
147 assert_se(hashmap_remove(link->manager->links, INT_TO_PTR(link->ifindex)) == link);
148- link_unref(link);
149+ return link_unref(link);
150 }
151
152 static int link_joined(Link *link) {
153@@ -3295,6 +3192,112 @@ ipv4ll_address_fail:
154
155 return 0;
156 }
157+
158+static Link *link_drop_or_unref(Link *link) {
159+ if (!link)
160+ return NULL;
161+ if (!link->manager)
162+ return link_unref(link);
163+ return link_drop(link);
164+}
165+
166+DEFINE_TRIVIAL_CLEANUP_FUNC(Link*, link_drop_or_unref);
167+
168+static int link_new(Manager *manager, sd_netlink_message *message, Link **ret) {
169+ _cleanup_(link_drop_or_unrefp) Link *link = NULL;
170+ uint16_t type;
171+ _cleanup_free_ char *ifname = NULL, *kind = NULL;
172+ int r, ifindex;
173+ unsigned short iftype;
174+
175+ assert(manager);
176+ assert(message);
177+ assert(ret);
178+
179+ r = sd_netlink_message_get_type(message, &type);
180+ if (r < 0)
181+ return r;
182+ else if (type != RTM_NEWLINK)
183+ return -EINVAL;
184+
185+ r = sd_rtnl_message_link_get_ifindex(message, &ifindex);
186+ if (r < 0)
187+ return r;
188+ else if (ifindex <= 0)
189+ return -EINVAL;
190+
191+ r = sd_rtnl_message_link_get_type(message, &iftype);
192+ if (r < 0)
193+ return r;
194+
195+ r = sd_netlink_message_read_string_strdup(message, IFLA_IFNAME, &ifname);
196+ if (r < 0)
197+ return r;
198+
199+ /* check for link kind */
200+ r = sd_netlink_message_enter_container(message, IFLA_LINKINFO);
201+ if (r >= 0) {
202+ (void) sd_netlink_message_read_string_strdup(message, IFLA_INFO_KIND, &kind);
203+ r = sd_netlink_message_exit_container(message);
204+ if (r < 0)
205+ return r;
206+ }
207+
208+ link = new(Link, 1);
209+ if (!link)
210+ return -ENOMEM;
211+
212+ *link = (Link) {
213+ .n_ref = 1,
214+ .state = LINK_STATE_PENDING,
215+ .ifindex = ifindex,
216+ .iftype = iftype,
217+ .ifname = TAKE_PTR(ifname),
218+ .kind = TAKE_PTR(kind),
219+
220+ .n_dns = (unsigned) -1,
221+ .dns_default_route = -1,
222+ .llmnr = _RESOLVE_SUPPORT_INVALID,
223+ .mdns = _RESOLVE_SUPPORT_INVALID,
224+ .dnssec_mode = _DNSSEC_MODE_INVALID,
225+ .dns_over_tls_mode = _DNS_OVER_TLS_MODE_INVALID,
226+ };
227+
228+ r = hashmap_ensure_allocated(&manager->links, NULL);
229+ if (r < 0)
230+ return r;
231+
232+ r = hashmap_put(manager->links, INT_TO_PTR(link->ifindex), link);
233+ if (r < 0)
234+ return r;
235+
236+ link->manager = manager;
237+
238+ r = sd_netlink_message_read_u32(message, IFLA_MASTER, (uint32_t*) &link->master_ifindex);
239+ if (r < 0)
240+ log_link_debug_errno(link, r, "New device has no master, continuing without");
241+
242+ r = sd_netlink_message_read_ether_addr(message, IFLA_ADDRESS, &link->mac);
243+ if (r < 0)
244+ log_link_debug_errno(link, r, "MAC address not found for new device, continuing without");
245+
246+ if (asprintf(&link->state_file, "/run/systemd/netif/links/%d", link->ifindex) < 0)
247+ return -ENOMEM;
248+
249+ if (asprintf(&link->lease_file, "/run/systemd/netif/leases/%d", link->ifindex) < 0)
250+ return -ENOMEM;
251+
252+ if (asprintf(&link->lldp_file, "/run/systemd/netif/lldp/%d", link->ifindex) < 0)
253+ return -ENOMEM;
254+
255+ r = link_update_flags(link, message, false);
256+ if (r < 0)
257+ return r;
258+
259+ *ret = TAKE_PTR(link);
260+
261+ return 0;
262+}
263
264 int link_add(Manager *m, sd_netlink_message *message, Link **ret) {
265 _cleanup_(sd_device_unrefp) sd_device *device = NULL;
266
267--- a/src/network/networkd-link.h 2021-09-02 18:04:16.900542857 +0530
268+++ b/src/network/networkd-link.h 2021-09-02 18:18:56.776571563 +0530
269@@ -175,7 +175,7 @@ DEFINE_TRIVIAL_DESTRUCTOR(link_netlink_d
270
271 int link_get(Manager *m, int ifindex, Link **ret);
272 int link_add(Manager *manager, sd_netlink_message *message, Link **ret);
273-void link_drop(Link *link);
274+Link *link_drop(Link *link);
275
276 int link_down(Link *link, link_netlink_message_handler_t callback);
277
278