• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1diff -urN libnl-3.7.0/include/linux-private/linux/if_bridge.h libnl-3.7.0_new/include/linux-private/linux/if_bridge.h
2--- libnl-3.7.0/include/linux-private/linux/if_bridge.h	2022-05-04 00:50:34.000000000 +0800
3+++ libnl-3.7.0_new/include/linux-private/linux/if_bridge.h	2024-06-03 17:46:47.872030900 +0800
4@@ -16,7 +16,7 @@
5
6 #include <linux/types.h>
7 #include <linux/if_ether.h>
8-#include <linux/in6.h>
9+#include <netinet/in.h>
10
11 #define SYSFS_BRIDGE_ATTR	"bridge"
12 #define SYSFS_BRIDGE_FDB	"brforward"
13diff -urN libnl-3.7.0/include/netlink-private/nl-auto.h libnl-3.7.0_new/include/netlink-private/nl-auto.h
14--- libnl-3.7.0/include/netlink-private/nl-auto.h	2022-05-24 16:55:12.000000000 +0800
15+++ libnl-3.7.0_new/include/netlink-private/nl-auto.h	2024-06-03 17:37:51.785691800 +0800
16@@ -99,4 +99,11 @@
17 #define _nl_auto_nl_socket _nl_auto(_nl_auto_nl_socket_fcn)
18 _NL_AUTO_DEFINE_FCN_TYPED0(struct nl_sock *, _nl_auto_nl_socket_fcn, nl_socket_free);
19
20+struct xfrmnl_user_tmpl;
21+void xfrmnl_user_tmpl_free(struct xfrmnl_user_tmpl *utmpl);
22+#define _nl_auto_xfrmnl_user_tmpl _nl_auto(_nl_auto_xfrmnl_user_tmpl_fcn)
23+_NL_AUTO_DEFINE_FCN_TYPED0(struct xfrmnl_user_tmpl *,
24+			   _nl_auto_xfrmnl_user_tmpl_fcn,
25+			   xfrmnl_user_tmpl_free);
26+
27 #endif /* NETLINK_NL_AUTO_H_ */
28diff -urN libnl-3.7.0/include/netlink-private/utils.h libnl-3.7.0_new/include/netlink-private/utils.h
29--- libnl-3.7.0/include/netlink-private/utils.h	2022-05-24 16:55:12.000000000 +0800
30+++ libnl-3.7.0_new/include/netlink-private/utils.h	2024-06-03 17:36:49.655447000 +0800
31@@ -361,8 +361,7 @@
32 	struct in6_addr a6;
33 } _NLIPAddr;
34
35-static inline char *_nl_inet_ntop(int addr_family, const void *addr,
36-				  char buf[static INET_ADDRSTRLEN])
37+static inline char *_nl_inet_ntop(int addr_family, const void *addr, char *buf)
38 {
39 	char *r;
40
41diff -urN libnl-3.7.0/lib/attr.c libnl-3.7.0_new/lib/attr.c
42--- libnl-3.7.0/lib/attr.c	2022-07-06 23:01:59.000000000 +0800
43+++ libnl-3.7.0_new/lib/attr.c	2024-06-03 17:39:30.549111300 +0800
44@@ -349,10 +349,13 @@
45
46 	if (!src)
47 		return 0;
48-
49+
50 	minlen = min_t(int, count, nla_len(src));
51-	memcpy(dest, nla_data(src), minlen);
52
53+	if (minlen <= 0)
54+		return 0;
55+
56+	memcpy(dest, nla_data(src), minlen);
57 	return minlen;
58 }
59
60@@ -988,6 +991,15 @@
61 {
62 	ssize_t len;
63
64+	if (!attr) {
65+		/* For robustness, allow a NULL attr to do nothing. NULL is also
66+		 * what nla_nest_start() when out of buffer space.
67+		 *
68+		 * Warning, before libnl-3.8, the function did not accept NULL!
69+		 * If you care, catch NULL yourself. */
70+		return;
71+	}
72+
73 	len = (char *) nlmsg_tail(msg->nm_nlh) - (char *) attr;
74 	if (len < 0)
75 		BUG();
76diff -urN libnl-3.7.0/lib/object.c libnl-3.7.0_new/lib/object.c
77--- libnl-3.7.0/lib/object.c	2022-07-06 22:13:48.000000000 +0800
78+++ libnl-3.7.0_new/lib/object.c	2024-06-03 17:41:15.060021200 +0800
79@@ -392,7 +392,7 @@
80 	diff = nl_object_diff64(a, b);
81
82 	return (diff & ~((uint64_t) 0xFFFFFFFF))
83-		? (uint32_t) diff | (1 << 31)
84+		? (uint32_t) diff | (((uint32_t ) 1u) << 31)
85 		: (uint32_t) diff;
86 }
87
88diff -urN libnl-3.7.0/lib/route/cls/flower.c libnl-3.7.0_new/lib/route/cls/flower.c
89--- libnl-3.7.0/lib/route/cls/flower.c	2022-07-06 23:02:41.000000000 +0800
90+++ libnl-3.7.0_new/lib/route/cls/flower.c	2024-06-03 18:28:20.501852400 +0800
91@@ -787,6 +787,7 @@
92 int rtnl_flower_append_action(struct rtnl_cls *cls, struct rtnl_act *act)
93 {
94 	struct rtnl_flower *f;
95+	int err;
96
97 	if (!act)
98 		return 0;
99@@ -796,8 +797,11 @@
100
101 	f->cf_mask |= FLOWER_ATTR_ACTION;
102
103+	if ((err = rtnl_act_append(&f->cf_act, act)) < 0)
104+		return err;
105+
106 	rtnl_act_get(act);
107-	return rtnl_act_append(&f->cf_act, act);
108+	return 0;
109 }
110
111 /**
112diff -urN libnl-3.7.0/lib/route/link/bridge.c libnl-3.7.0_new/lib/route/link/bridge.c
113--- libnl-3.7.0/lib/route/link/bridge.c	2022-05-24 16:55:12.000000000 +0800
114+++ libnl-3.7.0_new/lib/route/link/bridge.c	2024-06-03 16:55:11.850716100 +0800
115@@ -189,6 +189,7 @@
116 		if (nla_type(attr) == IFLA_BRIDGE_MODE) {
117 			bd->b_hwmode = nla_get_u16(attr);
118 			bd->ce_mask |= BRIDGE_ATTR_HWMODE;
119+			continue;
120 		} else if (nla_type(attr) != IFLA_BRIDGE_VLAN_INFO)
121 			continue;
122
123diff -urN libnl-3.7.0/lib/route/link.c libnl-3.7.0_new/lib/route/link.c
124--- libnl-3.7.0/lib/route/link.c	2022-05-24 16:55:12.000000000 +0800
125+++ libnl-3.7.0_new/lib/route/link.c	2024-06-03 17:44:18.163118300 +0800
126@@ -115,7 +115,7 @@
127 	struct rtnl_link_af_ops *ops;
128
129 	ops = rtnl_link_af_ops_lookup(af_type);
130-	if (ops && ops->ao_override_rtm(changes))
131+	if (ops && ops->ao_override_rtm && ops->ao_override_rtm(changes))
132 		return RTM_SETLINK;
133
134 	return RTM_NEWLINK;
135diff -urN libnl-3.7.0/lib/route/tc.c libnl-3.7.0_new/lib/route/tc.c
136--- libnl-3.7.0/lib/route/tc.c	2022-05-24 16:55:12.000000000 +0800
137+++ libnl-3.7.0_new/lib/route/tc.c	2024-06-03 17:36:24.831904500 +0800
138@@ -666,14 +666,14 @@
139 /**
140  * Calculate the binary logarithm for a specific cell size
141  * @arg cell_size	Size of cell, must be a power of two.
142- * @return Binary logirhtm of cell size or a negative error code.
143+ * @return Binary logarithm of cell size or a negative error code.
144  */
145 int rtnl_tc_calc_cell_log(int cell_size)
146 {
147 	int i;
148
149 	for (i = 0; i < 32; i++)
150-		if ((1 << i) == cell_size)
151+		if ((((uint32_t)1u) << i) == cell_size)
152 			return i;
153
154 	return -NLE_INVAL;
155diff -urN libnl-3.7.0/lib/socket.c libnl-3.7.0_new/lib/socket.c
156--- libnl-3.7.0/lib/socket.c	2022-05-24 16:55:12.000000000 +0800
157+++ libnl-3.7.0_new/lib/socket.c	2024-06-03 17:45:33.823477800 +0800
158@@ -54,6 +54,24 @@
159 	}
160 }
161
162+static uint32_t _badrandom_from_time(void)
163+{
164+	uint32_t result;
165+	uint64_t v64;
166+	time_t t;
167+
168+	t = time(NULL);
169+	v64 = (uint64_t)t;
170+	result = (uint32_t)v64;
171+
172+	/* XOR with the upper bits. Otherwise, coverity warns about only
173+	 * considering 32 bit from time_t.  Use the inverse, so that for the
174+	 * most part the bits don't change.  */
175+	result ^= (~(v64 >> 32));
176+
177+	return result;
178+}
179+
180 static uint32_t used_ports_map[32];
181 static NL_RW_LOCK(port_map_lock);
182
183@@ -67,7 +85,7 @@
184 	nl_write_lock(&port_map_lock);
185
186 	if (idx_state == 0) {
187-		uint32_t t = time(NULL);
188+		uint32_t t = _badrandom_from_time();
189
190 		/* from time to time (on average each 2^15 calls), the idx_state will
191 		 * be zero again. No problem, just "seed" anew with time(). */
192@@ -184,7 +202,8 @@
193 	sk->s_cb = nl_cb_get(cb);
194 	sk->s_local.nl_family = AF_NETLINK;
195 	sk->s_peer.nl_family = AF_NETLINK;
196-	sk->s_seq_expect = sk->s_seq_next = time(NULL);
197+	sk->s_seq_next = _badrandom_from_time();
198+	sk->s_seq_expect = sk->s_seq_next;
199
200 	/* the port is 0 (unspecified), meaning NL_OWN_PORT */
201 	sk->s_flags = NL_OWN_PORT;
202diff -urN libnl-3.7.0/lib/utils.c libnl-3.7.0_new/lib/utils.c
203--- libnl-3.7.0/lib/utils.c	2022-07-06 23:21:11.000000000 +0800
204+++ libnl-3.7.0_new/lib/utils.c	2024-06-03 17:37:18.340704700 +0800
205@@ -880,7 +880,7 @@
206 		return p->p_proto;
207
208 	l = strtoul(name, &end, 0);
209-	if (l == ULONG_MAX || *end != '\0')
210+	if (name == end || *end != '\0' || l > (unsigned long)INT_MAX)
211 		return -NLE_OBJ_NOTFOUND;
212
213 	return (int) l;
214diff -urN libnl-3.7.0/lib/xfrm/ae.c libnl-3.7.0_new/lib/xfrm/ae.c
215--- libnl-3.7.0/lib/xfrm/ae.c	2022-05-24 16:55:12.000000000 +0800
216+++ libnl-3.7.0_new/lib/xfrm/ae.c	2024-06-03 17:42:21.017726700 +0800
217@@ -301,6 +301,7 @@
218 	char                flags[128], buf[128];
219 	time_t              add_time, use_time;
220 	struct tm           *add_time_tm, *use_time_tm;
221+	struct tm           tm_buf;
222
223 	nl_dump_line(p, "src %s dst %s \n", nl_addr2str(ae->saddr, src, sizeof(src)),
224 				nl_addr2str(ae->sa_id.daddr, dst, sizeof(dst)));
225@@ -320,7 +321,7 @@
226 	if (ae->lifetime_cur.add_time != 0)
227 	{
228 		add_time = ae->lifetime_cur.add_time;
229-		add_time_tm = gmtime (&add_time);
230+		add_time_tm = gmtime_r (&add_time, &tm_buf);
231 		strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm);
232 	}
233 	else
234@@ -331,7 +332,7 @@
235 	if (ae->lifetime_cur.use_time != 0)
236 	{
237 		use_time = ae->lifetime_cur.use_time;
238-		use_time_tm = gmtime (&use_time);
239+		use_time_tm = gmtime_r (&use_time, &tm_buf);
240 		strftime (buf, 128, "%Y-%m-%d %H-%M-%S", use_time_tm);
241 	}
242 	else
243@@ -505,11 +506,18 @@
244 	if (err < 0)
245 		goto errout;
246
247-	ae->sa_id.daddr = nl_addr_build(ae_id->sa_id.family, &ae_id->sa_id.daddr, sizeof (ae_id->sa_id.daddr));
248+	if (!(ae->sa_id.daddr = nl_addr_build(ae_id->sa_id.family, &ae_id->sa_id.daddr,
249+					       sizeof (ae_id->sa_id.daddr)))) {
250+		err = -NLE_NOMEM;
251+		goto errout;
252+	}
253 	ae->sa_id.family= ae_id->sa_id.family;
254 	ae->sa_id.spi   = ntohl(ae_id->sa_id.spi);
255 	ae->sa_id.proto = ae_id->sa_id.proto;
256-	ae->saddr       = nl_addr_build(ae_id->sa_id.family, &ae_id->saddr, sizeof (ae_id->saddr));
257+	if (!(ae->saddr = nl_addr_build(ae_id->sa_id.family, &ae_id->saddr, sizeof (ae_id->saddr)))) {
258+		err = -NLE_NOMEM;
259+		goto errout;
260+	}
261 	ae->reqid       = ae_id->reqid;
262 	ae->flags       = ae_id->flags;
263 	ae->ce_mask |= (XFRM_AE_ATTR_DADDR | XFRM_AE_ATTR_FAMILY | XFRM_AE_ATTR_SPI |
264diff -urN libnl-3.7.0/lib/xfrm/sa.c libnl-3.7.0_new/lib/xfrm/sa.c
265--- libnl-3.7.0/lib/xfrm/sa.c	2022-05-24 16:55:12.000000000 +0800
266+++ libnl-3.7.0_new/lib/xfrm/sa.c	2024-06-03 18:32:07.233173600 +0800
267@@ -415,6 +415,7 @@
268 	char                flags[128], mode[128];
269 	time_t              add_time, use_time;
270 	struct tm           *add_time_tm, *use_time_tm;
271+	struct tm           tm_buf;
272
273 	nl_dump_line(p, "src %s dst %s family: %s\n", nl_addr2str(sa->saddr, src, sizeof(src)),
274 	             nl_addr2str(sa->id.daddr, dst, sizeof(dst)),
275@@ -467,7 +468,7 @@
276 	if (sa->curlft.add_time != 0)
277 	{
278 		add_time = sa->curlft.add_time;
279-		add_time_tm = gmtime (&add_time);
280+		add_time_tm = gmtime_r (&add_time, &tm_buf);
281 		strftime (flags, 128, "%Y-%m-%d %H-%M-%S", add_time_tm);
282 	}
283 	else
284@@ -478,7 +479,7 @@
285 	if (sa->curlft.use_time != 0)
286 	{
287 		use_time = sa->curlft.use_time;
288-		use_time_tm = gmtime (&use_time);
289+		use_time_tm = gmtime_r (&use_time, &tm_buf);
290 		strftime (mode, 128, "%Y-%m-%d %H-%M-%S", use_time_tm);
291 	}
292 	else
293@@ -717,9 +718,19 @@
294 		goto errout;
295
296 	if (sa_info->sel.family == AF_INET)
297-		addr    = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a4, sizeof (sa_info->sel.daddr.a4));
298+	{
299+		if (!(addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a4, sizeof (sa_info->sel.daddr.a4)))) {
300+			err = -NLE_NOMEM;
301+			goto errout;
302+		}
303+	}
304 	else
305-		addr    = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a6, sizeof (sa_info->sel.daddr.a6));
306+	{
307+		if (!(addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.daddr.a6, sizeof (sa_info->sel.daddr.a6)))) {
308+			err = -NLE_NOMEM;
309+			goto errout;
310+		}
311+	}
312 	nl_addr_set_prefixlen (addr, sa_info->sel.prefixlen_d);
313 	xfrmnl_sel_set_daddr (sa->sel, addr);
314 	/* Drop the reference count from the above set operation */
315@@ -727,9 +738,19 @@
316 	xfrmnl_sel_set_prefixlen_d (sa->sel, sa_info->sel.prefixlen_d);
317
318 	if (sa_info->sel.family == AF_INET)
319-		addr    = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a4, sizeof (sa_info->sel.saddr.a4));
320+	{
321+		if (!(addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a4, sizeof (sa_info->sel.saddr.a4)))) {
322+			err = -NLE_NOMEM;
323+			goto errout;
324+		}
325+	}
326 	else
327-		addr    = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a6, sizeof (sa_info->sel.saddr.a6));
328+	{
329+		if (!(addr = nl_addr_build (sa_info->sel.family, &sa_info->sel.saddr.a6, sizeof (sa_info->sel.saddr.a6)))) {
330+			err = -NLE_NOMEM;
331+			goto errout;
332+		}
333+	}
334 	nl_addr_set_prefixlen (addr, sa_info->sel.prefixlen_s);
335 	xfrmnl_sel_set_saddr (sa->sel, addr);
336 	/* Drop the reference count from the above set operation */
337@@ -747,17 +768,37 @@
338 	sa->ce_mask             |= XFRM_SA_ATTR_SEL;
339
340 	if (sa_info->family == AF_INET)
341-		sa->id.daddr        = nl_addr_build (sa_info->family, &sa_info->id.daddr.a4, sizeof (sa_info->id.daddr.a4));
342+	{
343+		if (!(sa->id.daddr = nl_addr_build (sa_info->family, &sa_info->id.daddr.a4, sizeof (sa_info->id.daddr.a4)))) {
344+			err = -NLE_NOMEM;
345+			goto errout;
346+		}
347+	}
348 	else
349-		sa->id.daddr        = nl_addr_build (sa_info->family, &sa_info->id.daddr.a6, sizeof (sa_info->id.daddr.a6));
350+	{
351+		if (!(sa->id.daddr = nl_addr_build (sa_info->family, &sa_info->id.daddr.a6, sizeof (sa_info->id.daddr.a6)))) {
352+			err = -NLE_NOMEM;
353+			goto errout;
354+		}
355+	}
356 	sa->id.spi              = ntohl(sa_info->id.spi);
357 	sa->id.proto            = sa_info->id.proto;
358 	sa->ce_mask             |= (XFRM_SA_ATTR_DADDR | XFRM_SA_ATTR_SPI | XFRM_SA_ATTR_PROTO);
359
360 	if (sa_info->family == AF_INET)
361-		sa->saddr           = nl_addr_build (sa_info->family, &sa_info->saddr.a4, sizeof (sa_info->saddr.a4));
362+	{
363+		if (!(sa->saddr = nl_addr_build (sa_info->family, &sa_info->saddr.a4, sizeof (sa_info->saddr.a4)))) {
364+			err = -NLE_NOMEM;
365+			goto errout;
366+		}
367+	}
368 	else
369-		sa->saddr           = nl_addr_build (sa_info->family, &sa_info->saddr.a6, sizeof (sa_info->saddr.a6));
370+	{
371+		if (!(sa->saddr = nl_addr_build (sa_info->family, &sa_info->saddr.a6, sizeof (sa_info->saddr.a6)))) {
372+			err = -NLE_NOMEM;
373+			goto errout;
374+		}
375+	}
376 	sa->ce_mask             |= XFRM_SA_ATTR_SADDR;
377
378 	sa->lft->soft_byte_limit    =   sa_info->lft.soft_byte_limit;
379@@ -865,9 +906,19 @@
380 		sa->encap->encap_sport  =   ntohs(encap->encap_sport);
381 		sa->encap->encap_dport  =   ntohs(encap->encap_dport);
382 		if (sa_info->family == AF_INET)
383-			sa->encap->encap_oa =   nl_addr_build (sa_info->family, &encap->encap_oa.a4, sizeof (encap->encap_oa.a4));
384+		{
385+			if (!(sa->encap->encap_oa = nl_addr_build (sa_info->family, &encap->encap_oa.a4, sizeof (encap->encap_oa.a4)))) {
386+				err = -NLE_NOMEM;
387+				goto errout;
388+			}
389+		}
390 		else
391-			sa->encap->encap_oa =   nl_addr_build (sa_info->family, &encap->encap_oa.a6, sizeof (encap->encap_oa.a6));
392+		{
393+			if (!(sa->encap->encap_oa = nl_addr_build (sa_info->family, &encap->encap_oa.a6, sizeof (encap->encap_oa.a6)))) {
394+				err = -NLE_NOMEM;
395+				goto errout;
396+			}
397+		}
398 		sa->ce_mask     |= XFRM_SA_ATTR_ENCAP;
399 	}
400
401@@ -879,13 +930,19 @@
402 	if (tb[XFRMA_COADDR]) {
403 		if (sa_info->family == AF_INET)
404 		{
405-			sa->coaddr  = nl_addr_build(sa_info->family, nla_data(tb[XFRMA_COADDR]),
406-			                            sizeof (uint32_t));
407+			if (!(sa->coaddr = nl_addr_build(
408+					sa_info->family, nla_data(tb[XFRMA_COADDR]), sizeof (uint32_t)))) {
409+				err = -NLE_NOMEM;
410+				goto errout;
411+			}
412 		}
413 		else
414 		{
415-			sa->coaddr  = nl_addr_build(sa_info->family, nla_data(tb[XFRMA_COADDR]),
416-			                            sizeof (uint32_t) * 4);
417+			if (!(sa->coaddr = nl_addr_build(
418+					sa_info->family, nla_data(tb[XFRMA_COADDR]), sizeof (uint32_t) * 4))) {
419+				err = -NLE_NOMEM;
420+				goto errout;
421+			}
422 		}
423 		sa->ce_mask         |= XFRM_SA_ATTR_COADDR;
424 	}
425diff -urN libnl-3.7.0/lib/xfrm/sp.c libnl-3.7.0_new/lib/xfrm/sp.c
426--- libnl-3.7.0/lib/xfrm/sp.c	2022-05-24 16:55:12.000000000 +0800
427+++ libnl-3.7.0_new/lib/xfrm/sp.c	2024-06-03 17:43:31.464119900 +0800
428@@ -324,6 +324,7 @@
429 	char                dst[INET6_ADDRSTRLEN+5], src[INET6_ADDRSTRLEN+5];
430 	time_t              add_time, use_time;
431 	struct tm           *add_time_tm, *use_time_tm;
432+	struct tm           tm_buf;
433
434 	nl_addr2str(xfrmnl_sel_get_saddr (sp->sel), src, sizeof(src));
435 	nl_addr2str (xfrmnl_sel_get_daddr (sp->sel), dst, sizeof (dst));
436@@ -384,7 +385,7 @@
437 	if (sp->curlft.add_time != 0)
438 	{
439 		add_time = sp->curlft.add_time;
440-		add_time_tm = gmtime (&add_time);
441+		add_time_tm = gmtime_r (&add_time, &tm_buf);
442 		strftime (dst, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", add_time_tm);
443 	}
444 	else
445@@ -395,7 +396,7 @@
446 	if (sp->curlft.use_time != 0)
447 	{
448 		use_time = sp->curlft.use_time;
449-		use_time_tm = gmtime (&use_time);
450+		use_time_tm = gmtime_r (&use_time, &tm_buf);
451 		strftime (src, INET6_ADDRSTRLEN+5, "%Y-%m-%d %H-%M-%S", use_time_tm);
452 	}
453 	else
454@@ -557,19 +558,43 @@
455 	}
456
457 	if (sp_info->sel.family == AF_INET)
458-		addr    = nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a4, sizeof (sp_info->sel.daddr.a4));
459+	{
460+		if (!(addr = nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a4, sizeof (sp_info->sel.daddr.a4)))) {
461+			err = -NLE_NOMEM;
462+			goto errout;
463+		}
464+	}
465 	else
466-		addr    = nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a6, sizeof (sp_info->sel.daddr.a6));
467+	{
468+		if (!(addr = nl_addr_build (sp_info->sel.family, &sp_info->sel.daddr.a6, sizeof (sp_info->sel.daddr.a6)))) {
469+			err = -NLE_NOMEM;
470+			goto errout;
471+		}
472+	}
473 	nl_addr_set_prefixlen (addr, sp_info->sel.prefixlen_d);
474 	xfrmnl_sel_set_daddr (sp->sel, addr);
475+	/* Drop the reference count from the above set operation */
476+	nl_addr_put(addr);
477 	xfrmnl_sel_set_prefixlen_d (sp->sel, sp_info->sel.prefixlen_d);
478
479 	if (sp_info->sel.family == AF_INET)
480-		addr    = nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a4, sizeof (sp_info->sel.saddr.a4));
481+	{
482+		if (!(addr = nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a4, sizeof (sp_info->sel.saddr.a4)))) {
483+			err = -NLE_NOMEM;
484+			goto errout;
485+		}
486+	}
487 	else
488-		addr    = nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a6, sizeof (sp_info->sel.saddr.a6));
489+	{
490+		if (!(addr = nl_addr_build (sp_info->sel.family, &sp_info->sel.saddr.a6, sizeof (sp_info->sel.saddr.a6)))) {
491+			err = -NLE_NOMEM;
492+			goto errout;
493+		}
494+	}
495 	nl_addr_set_prefixlen (addr, sp_info->sel.prefixlen_s);
496 	xfrmnl_sel_set_saddr (sp->sel, addr);
497+	/* Drop the reference count from the above set operation */
498+	nl_addr_put(addr);
499 	xfrmnl_sel_set_prefixlen_s (sp->sel, sp_info->sel.prefixlen_s);
500
501 	xfrmnl_sel_set_dport (sp->sel, ntohs (sp_info->sel.dport));
502@@ -628,13 +653,13 @@
503
504 	if (tb[XFRMA_TMPL]) {
505 		struct xfrm_user_tmpl*      tmpl = nla_data(tb[XFRMA_TMPL]);
506-		struct xfrmnl_user_tmpl*    sputmpl;
507 		uint32_t                    i;
508 		uint32_t                    num_tmpls = nla_len(tb[XFRMA_TMPL]) / sizeof (*tmpl);
509 		struct  nl_addr*            addr;
510
511 		for (i = 0; (i < num_tmpls) && (tmpl); i ++, tmpl++)
512 		{
513+			_nl_auto_xfrmnl_user_tmpl struct xfrmnl_user_tmpl *sputmpl = NULL;
514 			if ((sputmpl = xfrmnl_user_tmpl_alloc ()) == NULL)
515 			{
516 				err = -NLE_NOMEM;
517@@ -642,19 +667,43 @@
518 			}
519
520 			if (tmpl->family == AF_INET)
521-				addr = nl_addr_build(tmpl->family, &tmpl->id.daddr.a4, sizeof (tmpl->id.daddr.a4));
522+			{
523+				if (!(addr = nl_addr_build(tmpl->family, &tmpl->id.daddr.a4, sizeof (tmpl->id.daddr.a4)))) {
524+					err = -NLE_NOMEM;
525+					goto errout;
526+				}
527+			}
528 			else
529-				addr = nl_addr_build(tmpl->family, &tmpl->id.daddr.a6, sizeof (tmpl->id.daddr.a6));
530+			{
531+				if (!(addr = nl_addr_build(tmpl->family, &tmpl->id.daddr.a6, sizeof (tmpl->id.daddr.a6)))) {
532+					err = -NLE_NOMEM;
533+					goto errout;
534+				}
535+			}
536 			xfrmnl_user_tmpl_set_daddr (sputmpl, addr);
537+			/* Drop the reference count from the above set operation */
538+			nl_addr_put(addr);
539 			xfrmnl_user_tmpl_set_spi (sputmpl, ntohl(tmpl->id.spi));
540 			xfrmnl_user_tmpl_set_proto (sputmpl, tmpl->id.proto);
541 			xfrmnl_user_tmpl_set_family (sputmpl, tmpl->family);
542
543 			if (tmpl->family == AF_INET)
544-				addr = nl_addr_build(tmpl->family, &tmpl->saddr.a4, sizeof (tmpl->saddr.a4));
545+			{
546+				if (!(addr = nl_addr_build(tmpl->family, &tmpl->saddr.a4, sizeof (tmpl->saddr.a4)))) {
547+					err = -NLE_NOMEM;
548+					goto errout;
549+				}
550+			}
551 			else
552-				addr = nl_addr_build(tmpl->family, &tmpl->saddr.a6, sizeof (tmpl->saddr.a6));
553+			{
554+				if (!(addr = nl_addr_build(tmpl->family, &tmpl->saddr.a6, sizeof (tmpl->saddr.a6)))) {
555+					err = -NLE_NOMEM;
556+					goto errout;
557+				}
558+			}
559 			xfrmnl_user_tmpl_set_saddr (sputmpl, addr);
560+			/* Drop the reference count from the above set operation */
561+			nl_addr_put(addr);
562
563 			xfrmnl_user_tmpl_set_reqid (sputmpl, tmpl->reqid);
564 			xfrmnl_user_tmpl_set_mode (sputmpl, tmpl->mode);
565@@ -663,7 +712,7 @@
566 			xfrmnl_user_tmpl_set_aalgos (sputmpl, tmpl->aalgos);
567 			xfrmnl_user_tmpl_set_ealgos (sputmpl, tmpl->ealgos);
568 			xfrmnl_user_tmpl_set_calgos (sputmpl, tmpl->calgos);
569-			xfrmnl_sp_add_usertemplate (sp, sputmpl);
570+			xfrmnl_sp_add_usertemplate (sp, _nl_steal_pointer(&sputmpl));
571
572 			sp->ce_mask     |=  XFRM_SP_ATTR_TMPL;
573 		}
574@@ -1316,6 +1365,8 @@
575 	if (sp->ce_mask & XFRM_SP_ATTR_TMPL) {
576 		sp->nr_user_tmpl--;
577 		nl_list_del(&utmpl->utmpl_list);
578+		if (sp->nr_user_tmpl == 0)
579+			sp->ce_mask &= ~XFRM_SP_ATTR_TMPL;
580 	}
581 }
582
583diff -urN libnl-3.7.0/tests/params.h libnl-3.7.0_new/tests/params.h
584--- libnl-3.7.0/tests/params.h	1970-01-01 08:00:00.000000000 +0800
585+++ libnl-3.7.0_new/tests/params.h	2024-06-03 16:23:13.861244200 +0800
586@@ -0,0 +1,5 @@
587+#define DST_ADDR "addr"
588+#define IP "ip"
589+#define NEXTHOP "dev=1,via=2"
590+#define DEV_NAME "dev_name"
591+
592diff -urN libnl-3.7.0/tests/test-add-delete-addr.c libnl-3.7.0_new/tests/test-add-delete-addr.c
593--- libnl-3.7.0/tests/test-add-delete-addr.c	1970-01-01 08:00:00.000000000 +0800
594+++ libnl-3.7.0_new/tests/test-add-delete-addr.c	2024-06-03 16:23:13.861244200 +0800
595@@ -0,0 +1,39 @@
596+#include <netlink/cli/utils.h>
597+#include <netlink/cli/addr.h>
598+#include <netlink/cli/link.h>
599+#include <linux/netlink.h>
600+#include <stdio.h>
601+#include <params.h>
602+
603+
604+int main(int argc, char *argv[])
605+{
606+    struct nl_sock *sock;
607+    struct rtnl_addr *addr;
608+    struct nl_cache *link_cache;
609+    int err = 0, nlflags = NLM_F_CREATE;
610+
611+    sock = nl_cli_alloc_socket();
612+    nl_cli_connect(sock, NETLINK_ROUTE);
613+    link_cache = nl_cli_link_alloc_cache(sock);
614+    addr = nl_cli_addr_alloc();
615+
616+    nl_cli_addr_parse_local(addr, IP);
617+    nl_cli_addr_parse_dev(addr, link_cache, DEV_NAME);
618+
619+    if ((err = rtnl_addr_add(sock, addr, nlflags)) < 0) {
620+        printf("Unable to add route: %s", nl_geterror(err));
621+        goto END;
622+    }
623+
624+    if ((err = rtnl_addr_delete(sock, addr, nlflags)) < 0) {
625+        printf("Unable to add route: %s", nl_geterror(err));
626+        goto END;
627+    }
628+
629+END:
630+    rtnl_addr_put(addr);
631+    nl_cache_put(link_cache);
632+    nl_socket_free(sock);
633+    return err;
634+}
635diff -urN libnl-3.7.0/tests/test-add-delete-class.c libnl-3.7.0_new/tests/test-add-delete-class.c
636--- libnl-3.7.0/tests/test-add-delete-class.c	1970-01-01 08:00:00.000000000 +0800
637+++ libnl-3.7.0_new/tests/test-add-delete-class.c	2024-06-03 16:23:13.876242300 +0800
638@@ -0,0 +1,142 @@
639+#include <netlink/cli/utils.h>
640+#include <netlink/cli/tc.h>
641+#include <netlink/cli/class.h>
642+#include <netlink/cli/link.h>
643+#include <netlink-private/route/tc-api.h>
644+#include <linux/netlink.h>
645+#include <stdio.h>
646+#include <params.h>
647+
648+static int default_yes = 0, deleted = 0, interactive = 0;
649+static struct nl_sock *sk;
650+
651+static int test_add_class()
652+{
653+    struct rtnl_class *class;
654+    struct rtnl_tc *tc;
655+    struct nl_cache *link_cache;
656+    struct nl_cli_tc_module *tm;
657+    struct rtnl_tc_ops *ops;
658+    int err = 0, flags = NLM_F_CREATE | NLM_F_EXCL;
659+    char kind[] = "htb";
660+    char *rate[] = {DEV_NAME, "root", "htb", "--rate=100mbit"};
661+
662+    sk = nl_cli_alloc_socket();
663+    nl_cli_connect(sk, NETLINK_ROUTE);
664+    link_cache = nl_cli_link_alloc_cache(sk);
665+    class = nl_cli_class_alloc();
666+    tc = (struct rtnl_tc *) class;
667+
668+    nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME);
669+    nl_cli_tc_parse_parent(tc, "root");
670+    if (!rtnl_tc_get_ifindex(tc)) {
671+        printf("You must specify a network device (--dev=XXX)\n");
672+        err = -1;
673+        goto END;
674+    }
675+    if (!rtnl_tc_get_parent(tc)) {
676+        printf("You must specify a parent (--parent=XXX)\n");
677+        err = -1;
678+        goto END;
679+    }
680+
681+    rtnl_tc_set_kind(tc, kind);
682+    if (!(ops = rtnl_tc_get_ops(tc))) {
683+        printf("Unknown class \"%s\"\n", kind);
684+        err = -1;
685+        goto END;
686+    }
687+    if (!(tm = nl_cli_tc_lookup(ops))) {
688+        printf("class type \"%s\" not supported.\n", kind);
689+        err = -1;
690+        goto END;
691+    }
692+    tm->tm_parse_argv(tc, 4, rate);
693+
694+    if ((err = rtnl_class_add(sk, class, flags)) < 0) {
695+        printf("Unable to add class: %s\n", nl_geterror(err));
696+        goto END;
697+    }
698+
699+END:
700+    nl_cache_mngt_unprovide(link_cache);
701+    nl_cache_put(link_cache);
702+    rtnl_class_put(class);
703+    nl_socket_free(sk);
704+    return err;
705+}
706+
707+
708+static void delete_cb(struct nl_object *obj, void *arg)
709+{
710+    struct rtnl_class *class = nl_object_priv(obj);
711+    struct nl_dump_params params = {
712+        .dp_type = NL_DUMP_LINE,
713+        .dp_fd = stdout,
714+	};
715+    int err;
716+
717+    if (interactive && !nl_cli_confirm(obj, &params, default_yes))
718+        return;
719+
720+    if ((err = rtnl_class_delete(sk, class)) < 0)
721+        nl_cli_fatal(err, "Unable to delete class: %s\n", nl_geterror(err));
722+
723+    deleted++;
724+}
725+
726+static int test_delete_class()
727+{
728+    struct rtnl_class *class;
729+    struct rtnl_tc *tc;
730+    struct nl_cache *link_cache, *class_cache;
731+    struct nl_cli_tc_module *tm;
732+    struct rtnl_tc_ops *ops;
733+    char kind[] = "htb";
734+    int err = 0;
735+
736+    sk = nl_cli_alloc_socket();
737+    nl_cli_connect(sk, NETLINK_ROUTE);
738+    link_cache = nl_cli_link_alloc_cache(sk);
739+    class = nl_cli_class_alloc();
740+    tc = (struct rtnl_tc *) class;
741+
742+    nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME);
743+    nl_cli_tc_parse_parent(tc, "root");
744+    if (!rtnl_tc_get_ifindex(tc)) {
745+        printf("You must specify a network device (--dev=XXX)\n");
746+        err = -1;
747+        goto END;
748+    }
749+    if (!rtnl_tc_get_parent(tc)) {
750+        printf("You must specify a parent (--parent=XXX)\n");
751+        err = -1;
752+        goto END;
753+    }
754+    rtnl_tc_set_kind(tc, kind);
755+    if (!(ops = rtnl_tc_get_ops(tc))) {
756+        printf("Unknown class \"%s\"\n", kind);
757+        err = -1;
758+        goto END;
759+    }
760+    class_cache = nl_cli_class_alloc_cache(sk, rtnl_tc_get_ifindex(tc));
761+    nl_cache_foreach_filter(class_cache, OBJ_CAST(class), delete_cb, NULL);
762+
763+END:
764+    nl_cache_put(link_cache);
765+    nl_socket_free(sk);
766+    rtnl_class_put(class);
767+    return err;
768+}
769+
770+int main(int argc, char *argv[])
771+{
772+    int err = 0;
773+    if ((err = test_add_class()) < 0) {
774+        printf("Unable to add class\n");
775+    }
776+    if ((err = test_delete_class()) < 0) {
777+        printf("Unable to delete class");
778+    }
779+    return err;
780+}
781diff -urN libnl-3.7.0/tests/test-add-delete-neigh.c libnl-3.7.0_new/tests/test-add-delete-neigh.c
782--- libnl-3.7.0/tests/test-add-delete-neigh.c	1970-01-01 08:00:00.000000000 +0800
783+++ libnl-3.7.0_new/tests/test-add-delete-neigh.c	2024-06-03 16:23:13.876242300 +0800
784@@ -0,0 +1,41 @@
785+#include <netlink/cli/utils.h>
786+#include <netlink/cli/neigh.h>
787+#include <netlink/cli/link.h>
788+#include <linux/netlink.h>
789+#include <stdio.h>
790+#include <params.h>
791+
792+
793+int main(int argc, char *argv[])
794+{
795+    struct nl_sock *sk;
796+    struct rtnl_neigh *neigh;
797+    struct nl_cache *link_cache;
798+    int err = 0, ok = 0, nlflags = NLM_F_REPLACE | NLM_F_CREATE;
799+    char lladdr[] = "AA:BB:CC:DD:EE:FF";
800+
801+    sk = nl_cli_alloc_socket();
802+    nl_cli_connect(sk, NETLINK_ROUTE);
803+    link_cache = nl_cli_link_alloc_cache(sk);
804+    neigh = nl_cli_neigh_alloc();
805+
806+    nl_cli_neigh_parse_dst(neigh, DST_ADDR);
807+    nl_cli_neigh_parse_lladdr(neigh, lladdr);
808+    nl_cli_neigh_parse_dev(neigh, link_cache, DEV_NAME);
809+
810+    if ((err = rtnl_neigh_add(sk, neigh, nlflags)) < 0){
811+        printf("Unable to add neighbour: %s\n",nl_geterror(err));
812+        goto END;
813+    }
814+
815+    if ((err = rtnl_neigh_delete(sk, neigh, nlflags)) < 0){
816+        printf("Unable to add neighbour: %s\n",nl_geterror(err));
817+        goto END;
818+    }
819+
820+END:
821+    nl_socket_free(sk);
822+    nl_cache_put(link_cache);
823+    rtnl_neigh_put(neigh);
824+    return err;
825+}
826diff -urN libnl-3.7.0/tests/test-add-delete-qdisc.c libnl-3.7.0_new/tests/test-add-delete-qdisc.c
827--- libnl-3.7.0/tests/test-add-delete-qdisc.c	1970-01-01 08:00:00.000000000 +0800
828+++ libnl-3.7.0_new/tests/test-add-delete-qdisc.c	2024-06-03 16:23:13.892190500 +0800
829@@ -0,0 +1,156 @@
830+#include <netlink/cli/utils.h>
831+#include <netlink/cli/tc.h>
832+#include <netlink/cli/qdisc.h>
833+#include <netlink/cli/link.h>
834+#include <netlink-private/route/tc-api.h>
835+#include <linux/netlink.h>
836+#include <stdio.h>
837+#include <params.h>
838+
839+
840+static int default_yes = 0, deleted = 0, interactive = 0;
841+static struct nl_sock *sk;
842+
843+static void delete_cb(struct nl_object *obj, void *arg)
844+{
845+    struct rtnl_qdisc *qdisc = nl_object_priv(obj);
846+    struct nl_dump_params params = {
847+        .dp_type = NL_DUMP_LINE,
848+        .dp_fd = stdout,
849+    };
850+    int err;
851+
852+        /* Ignore default qdiscs, unable to delete */
853+    if (rtnl_tc_get_handle((struct rtnl_tc *) qdisc) == 0)
854+         return;
855+
856+    if (interactive && !nl_cli_confirm(obj, &params, default_yes))
857+         return;
858+
859+    if ((err = rtnl_qdisc_delete(sk, qdisc)) < 0) {
860+         nl_cli_fatal(err, "Unable to delete qdisc: %s\n", nl_geterror(err));
861+    }
862+    deleted++;
863+}
864+
865+static int test_delete_qdisc()
866+{
867+    struct rtnl_qdisc *qdisc;
868+    struct rtnl_tc *tc;
869+    struct nl_cache *link_cache, *qdisc_cache;
870+    struct nl_cli_tc_module *tm;
871+    struct rtnl_tc_ops *ops;
872+    char kind[] = "htb";
873+    int err = 0;
874+
875+    sk = nl_cli_alloc_socket();
876+    nl_cli_connect(sk, NETLINK_ROUTE);
877+    link_cache = nl_cli_link_alloc_cache(sk);
878+    qdisc_cache = nl_cli_qdisc_alloc_cache(sk);
879+    qdisc = nl_cli_qdisc_alloc();
880+    tc = (struct rtnl_tc *) qdisc;
881+    nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME);
882+    nl_cli_tc_parse_parent(tc, "root");
883+
884+    if (!rtnl_tc_get_ifindex(tc)) {
885+        printf("You must specify a network device (--dev=XXX)");
886+        goto END;
887+    }
888+
889+    if (!rtnl_tc_get_parent(tc)) {
890+        printf("You must specify a parent");
891+        goto END;
892+    }
893+
894+    rtnl_tc_set_kind(tc, kind);
895+    if (!(ops = rtnl_tc_get_ops(tc))) {
896+        printf("Unknown qdisc \"%s\"", kind);
897+        goto END;
898+    }
899+
900+    if (!(tm = nl_cli_tc_lookup(ops))) {
901+        nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.", kind);
902+        goto END;
903+    }
904+
905+
906+    nl_cache_foreach_filter(qdisc_cache, OBJ_CAST(qdisc), delete_cb, NULL);
907+
908+END:
909+    nl_cache_put(link_cache);
910+    nl_cache_put(qdisc_cache);
911+    rtnl_qdisc_put(qdisc);
912+    nl_socket_free(sk);
913+    return err;
914+}
915+
916+static int test_add_qdisc()
917+{
918+    struct rtnl_qdisc *qdisc;
919+    struct rtnl_tc *tc;
920+    struct nl_cache *link_cache;
921+    struct nl_cli_tc_module *tm;
922+    struct rtnl_tc_ops *ops;
923+    char kind[] = "htb";
924+    int err = 0, flags = NLM_F_CREATE | NLM_F_EXCL;
925+
926+    if (!(sk = nl_socket_alloc())){
927+        printf("Unable to allocate netlink socket\n");
928+        return -1;
929+    }
930+    if ((err = nl_connect(sk, NETLINK_ROUTE)) < 0) {
931+        printf("Unable to connect netlink socket: %s\n", nl_geterror(err));
932+        nl_socket_free(sk);
933+        return -1;
934+    }
935+    link_cache = nl_cli_link_alloc_cache(sk);
936+    qdisc = nl_cli_qdisc_alloc();
937+    tc = (struct rtnl_tc *) qdisc;
938+
939+    nl_cli_tc_parse_dev(tc, link_cache, DEV_NAME);
940+    nl_cli_tc_parse_parent(tc, "root");
941+
942+    if (!rtnl_tc_get_ifindex(tc)){
943+        printf("You must specify a network device (--dev=XXX)\n");
944+        goto END;
945+    }
946+
947+    if (!rtnl_tc_get_parent(tc)){
948+        printf("You must specify a parent\n");
949+        goto END;
950+    }
951+
952+    rtnl_tc_set_kind(tc, kind);
953+    if (!(ops = rtnl_tc_get_ops(tc))){
954+        printf("Unknown qdisc \"%s\"\n", kind);
955+        goto END;
956+    }
957+    if (!(tm = nl_cli_tc_lookup(ops))){
958+        nl_cli_fatal(ENOTSUP, "Qdisc type \"%s\" not supported.\n", kind);
959+        goto END;
960+    }
961+
962+    if ((err = rtnl_qdisc_add(sk, qdisc, flags)) < 0){
963+        printf("Unable to add qdisc: %s\n", nl_geterror(err));
964+        goto END;
965+    }
966+
967+END:
968+    nl_cache_mngt_unprovide(link_cache);
969+    nl_cache_put(link_cache);
970+    rtnl_qdisc_put(qdisc);
971+    nl_socket_free(sk);
972+    return err;
973+}
974+
975+int main(int args, char *argv[])
976+{
977+    int err = 0;
978+    if ((err = test_add_qdisc()) < 0) {
979+        printf("Unable to add qdisc:%s", nl_geterror(err));
980+    }
981+    if ((err = test_delete_qdisc()) < 0) {
982+        printf("Unable to delete qdisc:%s", nl_geterror(err));
983+    }
984+    return err;
985+}
986diff -urN libnl-3.7.0/tests/test-add-delete-route.c libnl-3.7.0_new/tests/test-add-delete-route.c
987--- libnl-3.7.0/tests/test-add-delete-route.c	1970-01-01 08:00:00.000000000 +0800
988+++ libnl-3.7.0_new/tests/test-add-delete-route.c	2024-06-03 16:23:13.892190500 +0800
989@@ -0,0 +1,43 @@
990+#include <netlink/cli/utils.h>
991+#include <netlink/cli/route.h>
992+#include <netlink/cli/link.h>
993+#include <linux/netlink.h>
994+#include <stdio.h>
995+#include <params.h>
996+
997+
998+int main(int argc, char argv[])
999+{
1000+    struct nl_sock *sk;
1001+    struct rtnl_route *route;
1002+    struct nl_cache *link_cache, *route_cache;
1003+    char dst_addr[] = DST_ADDR;
1004+    char nexthop[] = NEXTHOP;
1005+    int err = 0;
1006+
1007+    sk = nl_cli_alloc_socket();
1008+    nl_cli_connect(sk, NETLINK_ROUTE);
1009+    link_cache = nl_cli_link_alloc_cache(sk);
1010+    route_cache = nl_cli_route_alloc_cache(sk, 0);
1011+    route = nl_cli_route_alloc();
1012+
1013+    nl_cli_route_parse_dst(route, dst_addr);
1014+    nl_cli_route_parse_nexthop(route, nexthop, link_cache);
1015+
1016+    if ((err = rtnl_route_add(sk, route, NLM_F_EXCL)) < 0) {
1017+        printf("Unable to add route: %s", nl_geterror(err));
1018+        goto END;
1019+    }
1020+
1021+    if ((err = rtnl_route_delete(sk, route, NLM_F_EXCL)) < 0) {
1022+        printf("Unable to delete route: %s", nl_geterror(err));
1023+        goto END;
1024+    }
1025+
1026+END:
1027+    rtnl_route_put(route);
1028+    nl_cache_put(link_cache);
1029+    nl_cache_put(route_cache);
1030+    nl_socket_free(sk);
1031+    return err;
1032+}
1033diff -urN libnl-3.7.0/tests/test-genl-connect.c libnl-3.7.0_new/tests/test-genl-connect.c
1034--- libnl-3.7.0/tests/test-genl-connect.c	1970-01-01 08:00:00.000000000 +0800
1035+++ libnl-3.7.0_new/tests/test-genl-connect.c	2024-06-03 16:23:13.892190500 +0800
1036@@ -0,0 +1,37 @@
1037+#include <linux/genetlink.h>
1038+#include <netlink/socket.h>
1039+#include <netlink/cli/utils.h>
1040+#include <stdio.h>
1041+
1042+
1043+int main(int argc, char *argv[])
1044+{
1045+    struct nl_sock *sk;
1046+    struct nl_cache *family_cache;
1047+    struct nl_dump_params params = {
1048+        .dp_type = NL_DUMP_LINE,
1049+        .dp_fd = stdout,
1050+	};
1051+    int err = 0;
1052+
1053+    sk = nl_socket_alloc();
1054+    if ((err = genl_connect(sk)) < 0) {
1055+        printf("Unable create socket: %s\n", nl_geterror(err));
1056+        goto END;
1057+    }
1058+    nl_socket_enable_auto_ack(sk);
1059+
1060+    if (nl_socket_get_fd(sk) < 0) {
1061+        printf("vaild socket\n");
1062+	err = -1;
1063+        goto END;
1064+    }
1065+    nl_socket_set_buffer_size(sk, 32655, 32655);
1066+    family_cache = nl_cli_alloc_cache(sk, "generic netlink family", genl_ctrl_alloc_cache);
1067+    nl_cache_dump(family_cache, &params);
1068+
1069+END:
1070+    nl_socket_free(sk);
1071+    nl_cache_put(family_cache);
1072+    return err;
1073+}
1074diff -urN libnl-3.7.0/tests/test-link.c libnl-3.7.0_new/tests/test-link.c
1075--- libnl-3.7.0/tests/test-link.c	1970-01-01 08:00:00.000000000 +0800
1076+++ libnl-3.7.0_new/tests/test-link.c	2024-06-03 18:34:22.314718700 +0800
1077@@ -0,0 +1,68 @@
1078+#include <netlink/socket.h>
1079+#include <netlink/netlink.h>
1080+#include <netlink/cli/utils.h>
1081+#include <netlink/cli/link.h>
1082+#include <netlink/route/link.h>
1083+#include <unistd.h>
1084+
1085+
1086+static int self_def_cb = NL_CB_DEBUG;
1087+
1088+
1089+int main(int argc, char *argv[])
1090+{
1091+    struct nl_sock *sk;
1092+    struct nl_cache *link_cache;
1093+    struct rtnl_link *link;
1094+    struct nl_addr *addr;
1095+    struct nl_cb *cb;
1096+    int err = 0, ifindex, pid;
1097+    char *buf;
1098+
1099+    cb = nl_cb_alloc(self_def_cb);
1100+    pid = getpid();
1101+
1102+    if (!(sk = nl_socket_alloc_cb(cb))) {
1103+        nl_cli_fatal(ENOBUFS, "Unable to allocate netlink socket\n");
1104+    }
1105+    nl_cli_connect(sk, NETLINK_ROUTE);
1106+    nl_socket_disable_seq_check(sk);
1107+    nl_socket_disable_auto_ack(sk);
1108+    nl_socket_set_local_port(sk, pid);
1109+    nl_join_groups(sk, pid);
1110+    nl_socket_drop_membership(sk, pid);
1111+    nl_socket_set_peer_port(sk, 0);
1112+
1113+    link_cache = nl_cli_link_alloc_cache(sk);
1114+    link = nl_cli_link_alloc();
1115+
1116+    if (err = nl_socket_get_peer_port(sk)){
1117+        printf("peer_port %d\n", err);
1118+        goto END;
1119+    }
1120+    if (err = nl_socket_use_seq(sk))
1121+        printf("sk->s_seq_next %d\n", err);
1122+
1123+    if ((ifindex = rtnl_link_get_ifindex(link)) == 0){
1124+        printf("ifindex is not set, %d\n", ifindex);
1125+        rtnl_link_set_ifindex(link, 1);
1126+    };
1127+
1128+    if (rtnl_link_get(link_cache, 1)){
1129+        printf("now link is cached\n");
1130+    }else{
1131+        nl_cache_add(link_cache, (struct nl_object *)link);
1132+    };
1133+
1134+    if ((err = rtnl_link_add(sk, link, AF_INET)) < 0){
1135+        printf("Unable to add link %s\n", nl_geterror(err));
1136+        goto END;
1137+    }
1138+
1139+END:
1140+    nl_cb_put(cb);
1141+    nl_socket_free(sk);
1142+    nl_cache_put(link_cache);
1143+    rtnl_link_put(link);
1144+    return err;
1145+}
1146