1*ce444a00SDaniel Borkmann // SPDX-License-Identifier: GPL-2.0-or-later
2*ce444a00SDaniel Borkmann /* Taken & modified from iproute2's libnetlink.c
3*ce444a00SDaniel Borkmann  * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
4*ce444a00SDaniel Borkmann  */
5*ce444a00SDaniel Borkmann #include <stdio.h>
6*ce444a00SDaniel Borkmann #include <stdlib.h>
7*ce444a00SDaniel Borkmann #include <unistd.h>
8*ce444a00SDaniel Borkmann #include <errno.h>
9*ce444a00SDaniel Borkmann #include <time.h>
10*ce444a00SDaniel Borkmann #include <sys/socket.h>
11*ce444a00SDaniel Borkmann 
12*ce444a00SDaniel Borkmann #include "netlink_helpers.h"
13*ce444a00SDaniel Borkmann 
14*ce444a00SDaniel Borkmann static int rcvbuf = 1024 * 1024;
15*ce444a00SDaniel Borkmann 
rtnl_close(struct rtnl_handle * rth)16*ce444a00SDaniel Borkmann void rtnl_close(struct rtnl_handle *rth)
17*ce444a00SDaniel Borkmann {
18*ce444a00SDaniel Borkmann 	if (rth->fd >= 0) {
19*ce444a00SDaniel Borkmann 		close(rth->fd);
20*ce444a00SDaniel Borkmann 		rth->fd = -1;
21*ce444a00SDaniel Borkmann 	}
22*ce444a00SDaniel Borkmann }
23*ce444a00SDaniel Borkmann 
rtnl_open_byproto(struct rtnl_handle * rth,unsigned int subscriptions,int protocol)24*ce444a00SDaniel Borkmann int rtnl_open_byproto(struct rtnl_handle *rth, unsigned int subscriptions,
25*ce444a00SDaniel Borkmann 		      int protocol)
26*ce444a00SDaniel Borkmann {
27*ce444a00SDaniel Borkmann 	socklen_t addr_len;
28*ce444a00SDaniel Borkmann 	int sndbuf = 32768;
29*ce444a00SDaniel Borkmann 	int one = 1;
30*ce444a00SDaniel Borkmann 
31*ce444a00SDaniel Borkmann 	memset(rth, 0, sizeof(*rth));
32*ce444a00SDaniel Borkmann 	rth->proto = protocol;
33*ce444a00SDaniel Borkmann 	rth->fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, protocol);
34*ce444a00SDaniel Borkmann 	if (rth->fd < 0) {
35*ce444a00SDaniel Borkmann 		perror("Cannot open netlink socket");
36*ce444a00SDaniel Borkmann 		return -1;
37*ce444a00SDaniel Borkmann 	}
38*ce444a00SDaniel Borkmann 	if (setsockopt(rth->fd, SOL_SOCKET, SO_SNDBUF,
39*ce444a00SDaniel Borkmann 		       &sndbuf, sizeof(sndbuf)) < 0) {
40*ce444a00SDaniel Borkmann 		perror("SO_SNDBUF");
41*ce444a00SDaniel Borkmann 		goto err;
42*ce444a00SDaniel Borkmann 	}
43*ce444a00SDaniel Borkmann 	if (setsockopt(rth->fd, SOL_SOCKET, SO_RCVBUF,
44*ce444a00SDaniel Borkmann 		       &rcvbuf, sizeof(rcvbuf)) < 0) {
45*ce444a00SDaniel Borkmann 		perror("SO_RCVBUF");
46*ce444a00SDaniel Borkmann 		goto err;
47*ce444a00SDaniel Borkmann 	}
48*ce444a00SDaniel Borkmann 
49*ce444a00SDaniel Borkmann 	/* Older kernels may no support extended ACK reporting */
50*ce444a00SDaniel Borkmann 	setsockopt(rth->fd, SOL_NETLINK, NETLINK_EXT_ACK,
51*ce444a00SDaniel Borkmann 		   &one, sizeof(one));
52*ce444a00SDaniel Borkmann 
53*ce444a00SDaniel Borkmann 	memset(&rth->local, 0, sizeof(rth->local));
54*ce444a00SDaniel Borkmann 	rth->local.nl_family = AF_NETLINK;
55*ce444a00SDaniel Borkmann 	rth->local.nl_groups = subscriptions;
56*ce444a00SDaniel Borkmann 
57*ce444a00SDaniel Borkmann 	if (bind(rth->fd, (struct sockaddr *)&rth->local,
58*ce444a00SDaniel Borkmann 		 sizeof(rth->local)) < 0) {
59*ce444a00SDaniel Borkmann 		perror("Cannot bind netlink socket");
60*ce444a00SDaniel Borkmann 		goto err;
61*ce444a00SDaniel Borkmann 	}
62*ce444a00SDaniel Borkmann 	addr_len = sizeof(rth->local);
63*ce444a00SDaniel Borkmann 	if (getsockname(rth->fd, (struct sockaddr *)&rth->local,
64*ce444a00SDaniel Borkmann 			&addr_len) < 0) {
65*ce444a00SDaniel Borkmann 		perror("Cannot getsockname");
66*ce444a00SDaniel Borkmann 		goto err;
67*ce444a00SDaniel Borkmann 	}
68*ce444a00SDaniel Borkmann 	if (addr_len != sizeof(rth->local)) {
69*ce444a00SDaniel Borkmann 		fprintf(stderr, "Wrong address length %d\n", addr_len);
70*ce444a00SDaniel Borkmann 		goto err;
71*ce444a00SDaniel Borkmann 	}
72*ce444a00SDaniel Borkmann 	if (rth->local.nl_family != AF_NETLINK) {
73*ce444a00SDaniel Borkmann 		fprintf(stderr, "Wrong address family %d\n",
74*ce444a00SDaniel Borkmann 			rth->local.nl_family);
75*ce444a00SDaniel Borkmann 		goto err;
76*ce444a00SDaniel Borkmann 	}
77*ce444a00SDaniel Borkmann 	rth->seq = time(NULL);
78*ce444a00SDaniel Borkmann 	return 0;
79*ce444a00SDaniel Borkmann err:
80*ce444a00SDaniel Borkmann 	rtnl_close(rth);
81*ce444a00SDaniel Borkmann 	return -1;
82*ce444a00SDaniel Borkmann }
83*ce444a00SDaniel Borkmann 
rtnl_open(struct rtnl_handle * rth,unsigned int subscriptions)84*ce444a00SDaniel Borkmann int rtnl_open(struct rtnl_handle *rth, unsigned int subscriptions)
85*ce444a00SDaniel Borkmann {
86*ce444a00SDaniel Borkmann 	return rtnl_open_byproto(rth, subscriptions, NETLINK_ROUTE);
87*ce444a00SDaniel Borkmann }
88*ce444a00SDaniel Borkmann 
__rtnl_recvmsg(int fd,struct msghdr * msg,int flags)89*ce444a00SDaniel Borkmann static int __rtnl_recvmsg(int fd, struct msghdr *msg, int flags)
90*ce444a00SDaniel Borkmann {
91*ce444a00SDaniel Borkmann 	int len;
92*ce444a00SDaniel Borkmann 
93*ce444a00SDaniel Borkmann 	do {
94*ce444a00SDaniel Borkmann 		len = recvmsg(fd, msg, flags);
95*ce444a00SDaniel Borkmann 	} while (len < 0 && (errno == EINTR || errno == EAGAIN));
96*ce444a00SDaniel Borkmann 	if (len < 0) {
97*ce444a00SDaniel Borkmann 		fprintf(stderr, "netlink receive error %s (%d)\n",
98*ce444a00SDaniel Borkmann 			strerror(errno), errno);
99*ce444a00SDaniel Borkmann 		return -errno;
100*ce444a00SDaniel Borkmann 	}
101*ce444a00SDaniel Borkmann 	if (len == 0) {
102*ce444a00SDaniel Borkmann 		fprintf(stderr, "EOF on netlink\n");
103*ce444a00SDaniel Borkmann 		return -ENODATA;
104*ce444a00SDaniel Borkmann 	}
105*ce444a00SDaniel Borkmann 	return len;
106*ce444a00SDaniel Borkmann }
107*ce444a00SDaniel Borkmann 
rtnl_recvmsg(int fd,struct msghdr * msg,char ** answer)108*ce444a00SDaniel Borkmann static int rtnl_recvmsg(int fd, struct msghdr *msg, char **answer)
109*ce444a00SDaniel Borkmann {
110*ce444a00SDaniel Borkmann 	struct iovec *iov = msg->msg_iov;
111*ce444a00SDaniel Borkmann 	char *buf;
112*ce444a00SDaniel Borkmann 	int len;
113*ce444a00SDaniel Borkmann 
114*ce444a00SDaniel Borkmann 	iov->iov_base = NULL;
115*ce444a00SDaniel Borkmann 	iov->iov_len = 0;
116*ce444a00SDaniel Borkmann 
117*ce444a00SDaniel Borkmann 	len = __rtnl_recvmsg(fd, msg, MSG_PEEK | MSG_TRUNC);
118*ce444a00SDaniel Borkmann 	if (len < 0)
119*ce444a00SDaniel Borkmann 		return len;
120*ce444a00SDaniel Borkmann 	if (len < 32768)
121*ce444a00SDaniel Borkmann 		len = 32768;
122*ce444a00SDaniel Borkmann 	buf = malloc(len);
123*ce444a00SDaniel Borkmann 	if (!buf) {
124*ce444a00SDaniel Borkmann 		fprintf(stderr, "malloc error: not enough buffer\n");
125*ce444a00SDaniel Borkmann 		return -ENOMEM;
126*ce444a00SDaniel Borkmann 	}
127*ce444a00SDaniel Borkmann 	iov->iov_base = buf;
128*ce444a00SDaniel Borkmann 	iov->iov_len = len;
129*ce444a00SDaniel Borkmann 	len = __rtnl_recvmsg(fd, msg, 0);
130*ce444a00SDaniel Borkmann 	if (len < 0) {
131*ce444a00SDaniel Borkmann 		free(buf);
132*ce444a00SDaniel Borkmann 		return len;
133*ce444a00SDaniel Borkmann 	}
134*ce444a00SDaniel Borkmann 	if (answer)
135*ce444a00SDaniel Borkmann 		*answer = buf;
136*ce444a00SDaniel Borkmann 	else
137*ce444a00SDaniel Borkmann 		free(buf);
138*ce444a00SDaniel Borkmann 	return len;
139*ce444a00SDaniel Borkmann }
140*ce444a00SDaniel Borkmann 
rtnl_talk_error(struct nlmsghdr * h,struct nlmsgerr * err,nl_ext_ack_fn_t errfn)141*ce444a00SDaniel Borkmann static void rtnl_talk_error(struct nlmsghdr *h, struct nlmsgerr *err,
142*ce444a00SDaniel Borkmann 			    nl_ext_ack_fn_t errfn)
143*ce444a00SDaniel Borkmann {
144*ce444a00SDaniel Borkmann 	fprintf(stderr, "RTNETLINK answers: %s\n",
145*ce444a00SDaniel Borkmann 		strerror(-err->error));
146*ce444a00SDaniel Borkmann }
147*ce444a00SDaniel Borkmann 
__rtnl_talk_iov(struct rtnl_handle * rtnl,struct iovec * iov,size_t iovlen,struct nlmsghdr ** answer,bool show_rtnl_err,nl_ext_ack_fn_t errfn)148*ce444a00SDaniel Borkmann static int __rtnl_talk_iov(struct rtnl_handle *rtnl, struct iovec *iov,
149*ce444a00SDaniel Borkmann 			   size_t iovlen, struct nlmsghdr **answer,
150*ce444a00SDaniel Borkmann 			   bool show_rtnl_err, nl_ext_ack_fn_t errfn)
151*ce444a00SDaniel Borkmann {
152*ce444a00SDaniel Borkmann 	struct sockaddr_nl nladdr = { .nl_family = AF_NETLINK };
153*ce444a00SDaniel Borkmann 	struct iovec riov;
154*ce444a00SDaniel Borkmann 	struct msghdr msg = {
155*ce444a00SDaniel Borkmann 		.msg_name	= &nladdr,
156*ce444a00SDaniel Borkmann 		.msg_namelen	= sizeof(nladdr),
157*ce444a00SDaniel Borkmann 		.msg_iov	= iov,
158*ce444a00SDaniel Borkmann 		.msg_iovlen	= iovlen,
159*ce444a00SDaniel Borkmann 	};
160*ce444a00SDaniel Borkmann 	unsigned int seq = 0;
161*ce444a00SDaniel Borkmann 	struct nlmsghdr *h;
162*ce444a00SDaniel Borkmann 	int i, status;
163*ce444a00SDaniel Borkmann 	char *buf;
164*ce444a00SDaniel Borkmann 
165*ce444a00SDaniel Borkmann 	for (i = 0; i < iovlen; i++) {
166*ce444a00SDaniel Borkmann 		h = iov[i].iov_base;
167*ce444a00SDaniel Borkmann 		h->nlmsg_seq = seq = ++rtnl->seq;
168*ce444a00SDaniel Borkmann 		if (answer == NULL)
169*ce444a00SDaniel Borkmann 			h->nlmsg_flags |= NLM_F_ACK;
170*ce444a00SDaniel Borkmann 	}
171*ce444a00SDaniel Borkmann 	status = sendmsg(rtnl->fd, &msg, 0);
172*ce444a00SDaniel Borkmann 	if (status < 0) {
173*ce444a00SDaniel Borkmann 		perror("Cannot talk to rtnetlink");
174*ce444a00SDaniel Borkmann 		return -1;
175*ce444a00SDaniel Borkmann 	}
176*ce444a00SDaniel Borkmann 	/* change msg to use the response iov */
177*ce444a00SDaniel Borkmann 	msg.msg_iov = &riov;
178*ce444a00SDaniel Borkmann 	msg.msg_iovlen = 1;
179*ce444a00SDaniel Borkmann 	i = 0;
180*ce444a00SDaniel Borkmann 	while (1) {
181*ce444a00SDaniel Borkmann next:
182*ce444a00SDaniel Borkmann 		status = rtnl_recvmsg(rtnl->fd, &msg, &buf);
183*ce444a00SDaniel Borkmann 		++i;
184*ce444a00SDaniel Borkmann 		if (status < 0)
185*ce444a00SDaniel Borkmann 			return status;
186*ce444a00SDaniel Borkmann 		if (msg.msg_namelen != sizeof(nladdr)) {
187*ce444a00SDaniel Borkmann 			fprintf(stderr,
188*ce444a00SDaniel Borkmann 				"Sender address length == %d!\n",
189*ce444a00SDaniel Borkmann 				msg.msg_namelen);
190*ce444a00SDaniel Borkmann 			exit(1);
191*ce444a00SDaniel Borkmann 		}
192*ce444a00SDaniel Borkmann 		for (h = (struct nlmsghdr *)buf; status >= sizeof(*h); ) {
193*ce444a00SDaniel Borkmann 			int len = h->nlmsg_len;
194*ce444a00SDaniel Borkmann 			int l = len - sizeof(*h);
195*ce444a00SDaniel Borkmann 
196*ce444a00SDaniel Borkmann 			if (l < 0 || len > status) {
197*ce444a00SDaniel Borkmann 				if (msg.msg_flags & MSG_TRUNC) {
198*ce444a00SDaniel Borkmann 					fprintf(stderr, "Truncated message!\n");
199*ce444a00SDaniel Borkmann 					free(buf);
200*ce444a00SDaniel Borkmann 					return -1;
201*ce444a00SDaniel Borkmann 				}
202*ce444a00SDaniel Borkmann 				fprintf(stderr,
203*ce444a00SDaniel Borkmann 					"Malformed message: len=%d!\n",
204*ce444a00SDaniel Borkmann 					len);
205*ce444a00SDaniel Borkmann 				exit(1);
206*ce444a00SDaniel Borkmann 			}
207*ce444a00SDaniel Borkmann 			if (nladdr.nl_pid != 0 ||
208*ce444a00SDaniel Borkmann 			    h->nlmsg_pid != rtnl->local.nl_pid ||
209*ce444a00SDaniel Borkmann 			    h->nlmsg_seq > seq || h->nlmsg_seq < seq - iovlen) {
210*ce444a00SDaniel Borkmann 				/* Don't forget to skip that message. */
211*ce444a00SDaniel Borkmann 				status -= NLMSG_ALIGN(len);
212*ce444a00SDaniel Borkmann 				h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
213*ce444a00SDaniel Borkmann 				continue;
214*ce444a00SDaniel Borkmann 			}
215*ce444a00SDaniel Borkmann 			if (h->nlmsg_type == NLMSG_ERROR) {
216*ce444a00SDaniel Borkmann 				struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(h);
217*ce444a00SDaniel Borkmann 				int error = err->error;
218*ce444a00SDaniel Borkmann 
219*ce444a00SDaniel Borkmann 				if (l < sizeof(struct nlmsgerr)) {
220*ce444a00SDaniel Borkmann 					fprintf(stderr, "ERROR truncated\n");
221*ce444a00SDaniel Borkmann 					free(buf);
222*ce444a00SDaniel Borkmann 					return -1;
223*ce444a00SDaniel Borkmann 				}
224*ce444a00SDaniel Borkmann 				if (error) {
225*ce444a00SDaniel Borkmann 					errno = -error;
226*ce444a00SDaniel Borkmann 					if (rtnl->proto != NETLINK_SOCK_DIAG &&
227*ce444a00SDaniel Borkmann 					    show_rtnl_err)
228*ce444a00SDaniel Borkmann 						rtnl_talk_error(h, err, errfn);
229*ce444a00SDaniel Borkmann 				}
230*ce444a00SDaniel Borkmann 				if (i < iovlen) {
231*ce444a00SDaniel Borkmann 					free(buf);
232*ce444a00SDaniel Borkmann 					goto next;
233*ce444a00SDaniel Borkmann 				}
234*ce444a00SDaniel Borkmann 				if (error) {
235*ce444a00SDaniel Borkmann 					free(buf);
236*ce444a00SDaniel Borkmann 					return -i;
237*ce444a00SDaniel Borkmann 				}
238*ce444a00SDaniel Borkmann 				if (answer)
239*ce444a00SDaniel Borkmann 					*answer = (struct nlmsghdr *)buf;
240*ce444a00SDaniel Borkmann 				else
241*ce444a00SDaniel Borkmann 					free(buf);
242*ce444a00SDaniel Borkmann 				return 0;
243*ce444a00SDaniel Borkmann 			}
244*ce444a00SDaniel Borkmann 			if (answer) {
245*ce444a00SDaniel Borkmann 				*answer = (struct nlmsghdr *)buf;
246*ce444a00SDaniel Borkmann 				return 0;
247*ce444a00SDaniel Borkmann 			}
248*ce444a00SDaniel Borkmann 			fprintf(stderr, "Unexpected reply!\n");
249*ce444a00SDaniel Borkmann 			status -= NLMSG_ALIGN(len);
250*ce444a00SDaniel Borkmann 			h = (struct nlmsghdr *)((char *)h + NLMSG_ALIGN(len));
251*ce444a00SDaniel Borkmann 		}
252*ce444a00SDaniel Borkmann 		free(buf);
253*ce444a00SDaniel Borkmann 		if (msg.msg_flags & MSG_TRUNC) {
254*ce444a00SDaniel Borkmann 			fprintf(stderr, "Message truncated!\n");
255*ce444a00SDaniel Borkmann 			continue;
256*ce444a00SDaniel Borkmann 		}
257*ce444a00SDaniel Borkmann 		if (status) {
258*ce444a00SDaniel Borkmann 			fprintf(stderr, "Remnant of size %d!\n", status);
259*ce444a00SDaniel Borkmann 			exit(1);
260*ce444a00SDaniel Borkmann 		}
261*ce444a00SDaniel Borkmann 	}
262*ce444a00SDaniel Borkmann }
263*ce444a00SDaniel Borkmann 
__rtnl_talk(struct rtnl_handle * rtnl,struct nlmsghdr * n,struct nlmsghdr ** answer,bool show_rtnl_err,nl_ext_ack_fn_t errfn)264*ce444a00SDaniel Borkmann static int __rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
265*ce444a00SDaniel Borkmann 		       struct nlmsghdr **answer, bool show_rtnl_err,
266*ce444a00SDaniel Borkmann 		       nl_ext_ack_fn_t errfn)
267*ce444a00SDaniel Borkmann {
268*ce444a00SDaniel Borkmann 	struct iovec iov = {
269*ce444a00SDaniel Borkmann 		.iov_base	= n,
270*ce444a00SDaniel Borkmann 		.iov_len	= n->nlmsg_len,
271*ce444a00SDaniel Borkmann 	};
272*ce444a00SDaniel Borkmann 
273*ce444a00SDaniel Borkmann 	return __rtnl_talk_iov(rtnl, &iov, 1, answer, show_rtnl_err, errfn);
274*ce444a00SDaniel Borkmann }
275*ce444a00SDaniel Borkmann 
rtnl_talk(struct rtnl_handle * rtnl,struct nlmsghdr * n,struct nlmsghdr ** answer)276*ce444a00SDaniel Borkmann int rtnl_talk(struct rtnl_handle *rtnl, struct nlmsghdr *n,
277*ce444a00SDaniel Borkmann 	      struct nlmsghdr **answer)
278*ce444a00SDaniel Borkmann {
279*ce444a00SDaniel Borkmann 	return __rtnl_talk(rtnl, n, answer, true, NULL);
280*ce444a00SDaniel Borkmann }
281*ce444a00SDaniel Borkmann 
addattr(struct nlmsghdr * n,int maxlen,int type)282*ce444a00SDaniel Borkmann int addattr(struct nlmsghdr *n, int maxlen, int type)
283*ce444a00SDaniel Borkmann {
284*ce444a00SDaniel Borkmann 	return addattr_l(n, maxlen, type, NULL, 0);
285*ce444a00SDaniel Borkmann }
286*ce444a00SDaniel Borkmann 
addattr8(struct nlmsghdr * n,int maxlen,int type,__u8 data)287*ce444a00SDaniel Borkmann int addattr8(struct nlmsghdr *n, int maxlen, int type, __u8 data)
288*ce444a00SDaniel Borkmann {
289*ce444a00SDaniel Borkmann 	return addattr_l(n, maxlen, type, &data, sizeof(__u8));
290*ce444a00SDaniel Borkmann }
291*ce444a00SDaniel Borkmann 
addattr16(struct nlmsghdr * n,int maxlen,int type,__u16 data)292*ce444a00SDaniel Borkmann int addattr16(struct nlmsghdr *n, int maxlen, int type, __u16 data)
293*ce444a00SDaniel Borkmann {
294*ce444a00SDaniel Borkmann 	return addattr_l(n, maxlen, type, &data, sizeof(__u16));
295*ce444a00SDaniel Borkmann }
296*ce444a00SDaniel Borkmann 
addattr32(struct nlmsghdr * n,int maxlen,int type,__u32 data)297*ce444a00SDaniel Borkmann int addattr32(struct nlmsghdr *n, int maxlen, int type, __u32 data)
298*ce444a00SDaniel Borkmann {
299*ce444a00SDaniel Borkmann 	return addattr_l(n, maxlen, type, &data, sizeof(__u32));
300*ce444a00SDaniel Borkmann }
301*ce444a00SDaniel Borkmann 
addattr64(struct nlmsghdr * n,int maxlen,int type,__u64 data)302*ce444a00SDaniel Borkmann int addattr64(struct nlmsghdr *n, int maxlen, int type, __u64 data)
303*ce444a00SDaniel Borkmann {
304*ce444a00SDaniel Borkmann 	return addattr_l(n, maxlen, type, &data, sizeof(__u64));
305*ce444a00SDaniel Borkmann }
306*ce444a00SDaniel Borkmann 
addattrstrz(struct nlmsghdr * n,int maxlen,int type,const char * str)307*ce444a00SDaniel Borkmann int addattrstrz(struct nlmsghdr *n, int maxlen, int type, const char *str)
308*ce444a00SDaniel Borkmann {
309*ce444a00SDaniel Borkmann 	return addattr_l(n, maxlen, type, str, strlen(str)+1);
310*ce444a00SDaniel Borkmann }
311*ce444a00SDaniel Borkmann 
addattr_l(struct nlmsghdr * n,int maxlen,int type,const void * data,int alen)312*ce444a00SDaniel Borkmann int addattr_l(struct nlmsghdr *n, int maxlen, int type, const void *data,
313*ce444a00SDaniel Borkmann 	      int alen)
314*ce444a00SDaniel Borkmann {
315*ce444a00SDaniel Borkmann 	int len = RTA_LENGTH(alen);
316*ce444a00SDaniel Borkmann 	struct rtattr *rta;
317*ce444a00SDaniel Borkmann 
318*ce444a00SDaniel Borkmann 	if (NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len) > maxlen) {
319*ce444a00SDaniel Borkmann 		fprintf(stderr, "%s: Message exceeded bound of %d\n",
320*ce444a00SDaniel Borkmann 			__func__, maxlen);
321*ce444a00SDaniel Borkmann 		return -1;
322*ce444a00SDaniel Borkmann 	}
323*ce444a00SDaniel Borkmann 	rta = NLMSG_TAIL(n);
324*ce444a00SDaniel Borkmann 	rta->rta_type = type;
325*ce444a00SDaniel Borkmann 	rta->rta_len = len;
326*ce444a00SDaniel Borkmann 	if (alen)
327*ce444a00SDaniel Borkmann 		memcpy(RTA_DATA(rta), data, alen);
328*ce444a00SDaniel Borkmann 	n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + RTA_ALIGN(len);
329*ce444a00SDaniel Borkmann 	return 0;
330*ce444a00SDaniel Borkmann }
331*ce444a00SDaniel Borkmann 
addraw_l(struct nlmsghdr * n,int maxlen,const void * data,int len)332*ce444a00SDaniel Borkmann int addraw_l(struct nlmsghdr *n, int maxlen, const void *data, int len)
333*ce444a00SDaniel Borkmann {
334*ce444a00SDaniel Borkmann 	if (NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len) > maxlen) {
335*ce444a00SDaniel Borkmann 		fprintf(stderr, "%s: Message exceeded bound of %d\n",
336*ce444a00SDaniel Borkmann 			__func__, maxlen);
337*ce444a00SDaniel Borkmann 		return -1;
338*ce444a00SDaniel Borkmann 	}
339*ce444a00SDaniel Borkmann 
340*ce444a00SDaniel Borkmann 	memcpy(NLMSG_TAIL(n), data, len);
341*ce444a00SDaniel Borkmann 	memset((void *) NLMSG_TAIL(n) + len, 0, NLMSG_ALIGN(len) - len);
342*ce444a00SDaniel Borkmann 	n->nlmsg_len = NLMSG_ALIGN(n->nlmsg_len) + NLMSG_ALIGN(len);
343*ce444a00SDaniel Borkmann 	return 0;
344*ce444a00SDaniel Borkmann }
345*ce444a00SDaniel Borkmann 
addattr_nest(struct nlmsghdr * n,int maxlen,int type)346*ce444a00SDaniel Borkmann struct rtattr *addattr_nest(struct nlmsghdr *n, int maxlen, int type)
347*ce444a00SDaniel Borkmann {
348*ce444a00SDaniel Borkmann 	struct rtattr *nest = NLMSG_TAIL(n);
349*ce444a00SDaniel Borkmann 
350*ce444a00SDaniel Borkmann 	addattr_l(n, maxlen, type, NULL, 0);
351*ce444a00SDaniel Borkmann 	return nest;
352*ce444a00SDaniel Borkmann }
353*ce444a00SDaniel Borkmann 
addattr_nest_end(struct nlmsghdr * n,struct rtattr * nest)354*ce444a00SDaniel Borkmann int addattr_nest_end(struct nlmsghdr *n, struct rtattr *nest)
355*ce444a00SDaniel Borkmann {
356*ce444a00SDaniel Borkmann 	nest->rta_len = (void *)NLMSG_TAIL(n) - (void *)nest;
357*ce444a00SDaniel Borkmann 	return n->nlmsg_len;
358*ce444a00SDaniel Borkmann }
359