]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <daniel.lezcano at free.fr> | |
8 | * | |
9 | * This library is free software; you can redistribute it and/or | |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; either | |
12 | * version 2.1 of the License, or (at your option) any later version. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
22 | */ | |
23 | #include <sys/socket.h> | |
24 | #include <string.h> | |
25 | #include <stdio.h> | |
26 | #include <time.h> | |
27 | #include <unistd.h> | |
28 | #include <errno.h> | |
29 | #include <stdlib.h> | |
30 | #include <linux/netlink.h> | |
31 | #include <linux/rtnetlink.h> | |
32 | ||
33 | #include "nl.h" | |
34 | ||
35 | #define NLMSG_TAIL(nmsg) \ | |
36 | ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) | |
37 | ||
38 | extern size_t nlmsg_len(const struct nlmsg *nlmsg) | |
39 | { | |
40 | return nlmsg->nlmsghdr->nlmsg_len - NLMSG_HDRLEN; | |
41 | } | |
42 | ||
43 | extern void *nlmsg_data(struct nlmsg *nlmsg) | |
44 | { | |
45 | char *data = ((char *)nlmsg) + NLMSG_HDRLEN; | |
46 | if (!nlmsg_len(nlmsg)) | |
47 | return NULL; | |
48 | return data; | |
49 | } | |
50 | ||
51 | static int nla_put(struct nlmsg *nlmsg, int attr, | |
52 | const void *data, size_t len) | |
53 | { | |
54 | struct rtattr *rta; | |
55 | size_t rtalen = RTA_LENGTH(len); | |
56 | size_t tlen = NLMSG_ALIGN(nlmsg->nlmsghdr->nlmsg_len) + RTA_ALIGN(rtalen); | |
57 | ||
58 | if (tlen > nlmsg->cap) | |
59 | return -ENOMEM; | |
60 | ||
61 | rta = NLMSG_TAIL(nlmsg->nlmsghdr); | |
62 | rta->rta_type = attr; | |
63 | rta->rta_len = rtalen; | |
64 | memcpy(RTA_DATA(rta), data, len); | |
65 | nlmsg->nlmsghdr->nlmsg_len = tlen; | |
66 | return 0; | |
67 | } | |
68 | ||
69 | extern int nla_put_buffer(struct nlmsg *nlmsg, int attr, | |
70 | const void *data, size_t size) | |
71 | { | |
72 | return nla_put(nlmsg, attr, data, size); | |
73 | } | |
74 | ||
75 | extern int nla_put_string(struct nlmsg *nlmsg, int attr, const char *string) | |
76 | { | |
77 | return nla_put(nlmsg, attr, string, strlen(string) + 1); | |
78 | } | |
79 | ||
80 | extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value) | |
81 | { | |
82 | return nla_put(nlmsg, attr, &value, sizeof(value)); | |
83 | } | |
84 | ||
85 | extern int nla_put_u16(struct nlmsg *nlmsg, int attr, unsigned short value) | |
86 | { | |
87 | return nla_put(nlmsg, attr, &value, 2); | |
88 | } | |
89 | ||
90 | extern int nla_put_attr(struct nlmsg *nlmsg, int attr) | |
91 | { | |
92 | return nla_put(nlmsg, attr, NULL, 0); | |
93 | } | |
94 | ||
95 | struct rtattr *nla_begin_nested(struct nlmsg *nlmsg, int attr) | |
96 | { | |
97 | struct rtattr *rtattr = NLMSG_TAIL(nlmsg->nlmsghdr); | |
98 | ||
99 | if (nla_put_attr(nlmsg, attr)) | |
100 | return NULL; | |
101 | ||
102 | return rtattr; | |
103 | } | |
104 | ||
105 | void nla_end_nested(struct nlmsg *nlmsg, struct rtattr *attr) | |
106 | { | |
107 | attr->rta_len = (void *)NLMSG_TAIL(nlmsg->nlmsghdr) - (void *)attr; | |
108 | } | |
109 | ||
110 | extern struct nlmsg *nlmsg_alloc(size_t size) | |
111 | { | |
112 | struct nlmsg *nlmsg; | |
113 | size_t len = NLMSG_HDRLEN + NLMSG_ALIGN(size); | |
114 | ||
115 | nlmsg = (struct nlmsg *)malloc(sizeof(struct nlmsg)); | |
116 | if (!nlmsg) | |
117 | return NULL; | |
118 | ||
119 | nlmsg->nlmsghdr = (struct nlmsghdr *)malloc(len); | |
120 | if (!nlmsg->nlmsghdr) | |
121 | goto errout; | |
122 | ||
123 | memset(nlmsg->nlmsghdr, 0, len); | |
124 | nlmsg->cap = len; | |
125 | nlmsg->nlmsghdr->nlmsg_len = NLMSG_HDRLEN; | |
126 | ||
127 | return nlmsg; | |
128 | errout: | |
129 | free(nlmsg); | |
130 | return NULL; | |
131 | } | |
132 | ||
133 | extern void *nlmsg_reserve(struct nlmsg *nlmsg, size_t len) | |
134 | { | |
135 | void *buf; | |
136 | size_t nlmsg_len = nlmsg->nlmsghdr->nlmsg_len; | |
137 | size_t tlen = NLMSG_ALIGN(len); | |
138 | ||
139 | if (nlmsg_len + tlen > nlmsg->cap) | |
140 | return NULL; | |
141 | ||
142 | buf = ((char *)(nlmsg->nlmsghdr)) + nlmsg_len; | |
143 | nlmsg->nlmsghdr->nlmsg_len += tlen; | |
144 | ||
145 | if (tlen > len) | |
146 | memset(buf + len, 0, tlen - len); | |
147 | ||
148 | return buf; | |
149 | } | |
150 | ||
151 | extern struct nlmsg *nlmsg_alloc_reserve(size_t size) | |
152 | { | |
153 | struct nlmsg *nlmsg; | |
154 | ||
155 | nlmsg = nlmsg_alloc(size); | |
156 | if (!nlmsg) | |
157 | return NULL; | |
158 | ||
159 | // just set message length to cap directly | |
160 | nlmsg->nlmsghdr->nlmsg_len = nlmsg->cap; | |
161 | return nlmsg; | |
162 | } | |
163 | ||
164 | extern void nlmsg_free(struct nlmsg *nlmsg) | |
165 | { | |
166 | if (!nlmsg) | |
167 | return; | |
168 | ||
169 | free(nlmsg->nlmsghdr); | |
170 | free(nlmsg); | |
171 | } | |
172 | ||
173 | extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *answer) | |
174 | { | |
175 | int ret; | |
176 | struct sockaddr_nl nladdr; | |
177 | struct iovec iov = { | |
178 | .iov_base = answer->nlmsghdr, | |
179 | .iov_len = answer->nlmsghdr->nlmsg_len, | |
180 | }; | |
181 | ||
182 | struct msghdr msg = { | |
183 | .msg_name = &nladdr, | |
184 | .msg_namelen = sizeof(nladdr), | |
185 | .msg_iov = &iov, | |
186 | .msg_iovlen = 1, | |
187 | }; | |
188 | ||
189 | memset(&nladdr, 0, sizeof(nladdr)); | |
190 | nladdr.nl_family = AF_NETLINK; | |
191 | nladdr.nl_pid = 0; | |
192 | nladdr.nl_groups = 0; | |
193 | ||
194 | again: | |
195 | ret = recvmsg(handler->fd, &msg, 0); | |
196 | if (ret < 0) { | |
197 | if (errno == EINTR) | |
198 | goto again; | |
199 | return -errno; | |
200 | } | |
201 | ||
202 | if (!ret) | |
203 | return 0; | |
204 | ||
205 | if (msg.msg_flags & MSG_TRUNC && | |
206 | ret == answer->nlmsghdr->nlmsg_len) | |
207 | return -EMSGSIZE; | |
208 | ||
209 | return ret; | |
210 | } | |
211 | ||
212 | extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg) | |
213 | { | |
214 | struct sockaddr_nl nladdr; | |
215 | struct iovec iov = { | |
216 | .iov_base = nlmsg->nlmsghdr, | |
217 | .iov_len = nlmsg->nlmsghdr->nlmsg_len, | |
218 | }; | |
219 | struct msghdr msg = { | |
220 | .msg_name = &nladdr, | |
221 | .msg_namelen = sizeof(nladdr), | |
222 | .msg_iov = &iov, | |
223 | .msg_iovlen = 1, | |
224 | }; | |
225 | int ret; | |
226 | ||
227 | memset(&nladdr, 0, sizeof(nladdr)); | |
228 | nladdr.nl_family = AF_NETLINK; | |
229 | nladdr.nl_pid = 0; | |
230 | nladdr.nl_groups = 0; | |
231 | ||
232 | ret = sendmsg(handler->fd, &msg, 0); | |
233 | if (ret < 0) | |
234 | return -errno; | |
235 | ||
236 | return ret; | |
237 | } | |
238 | ||
239 | #ifndef NLMSG_ERROR | |
240 | #define NLMSG_ERROR 0x2 | |
241 | #endif | |
242 | extern int netlink_transaction(struct nl_handler *handler, | |
243 | struct nlmsg *request, struct nlmsg *answer) | |
244 | { | |
245 | int ret; | |
246 | ||
247 | ret = netlink_send(handler, request); | |
248 | if (ret < 0) | |
249 | return ret; | |
250 | ||
251 | ret = netlink_rcv(handler, answer); | |
252 | if (ret < 0) | |
253 | return ret; | |
254 | ||
255 | if (answer->nlmsghdr->nlmsg_type == NLMSG_ERROR) { | |
256 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer->nlmsghdr); | |
257 | return err->error; | |
258 | } | |
259 | ||
260 | return 0; | |
261 | } | |
262 | ||
263 | extern int netlink_open(struct nl_handler *handler, int protocol) | |
264 | { | |
265 | socklen_t socklen; | |
266 | int sndbuf = 32768; | |
267 | int rcvbuf = 32768; | |
268 | int err; | |
269 | ||
270 | memset(handler, 0, sizeof(*handler)); | |
271 | ||
272 | handler->fd = socket(AF_NETLINK, SOCK_RAW, protocol); | |
273 | if (handler->fd < 0) | |
274 | return -errno; | |
275 | ||
276 | if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF, | |
277 | &sndbuf, sizeof(sndbuf)) < 0) | |
278 | goto err_with_errno; | |
279 | ||
280 | if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF, | |
281 | &rcvbuf,sizeof(rcvbuf)) < 0) | |
282 | goto err_with_errno; | |
283 | ||
284 | memset(&handler->local, 0, sizeof(handler->local)); | |
285 | handler->local.nl_family = AF_NETLINK; | |
286 | handler->local.nl_groups = 0; | |
287 | ||
288 | if (bind(handler->fd, (struct sockaddr*)&handler->local, | |
289 | sizeof(handler->local)) < 0) | |
290 | goto err_with_errno; | |
291 | ||
292 | socklen = sizeof(handler->local); | |
293 | if (getsockname(handler->fd, (struct sockaddr*)&handler->local, | |
294 | &socklen) < 0) | |
295 | goto err_with_errno; | |
296 | ||
297 | if (socklen != sizeof(handler->local)) { | |
298 | err = -EINVAL; | |
299 | goto errclose; | |
300 | } | |
301 | ||
302 | if (handler->local.nl_family != AF_NETLINK) { | |
303 | err = -EINVAL; | |
304 | goto errclose; | |
305 | } | |
306 | ||
307 | handler->seq = time(NULL); | |
308 | ||
309 | return 0; | |
310 | err_with_errno: | |
311 | err = -errno; | |
312 | errclose: | |
313 | close(handler->fd); | |
314 | return err; | |
315 | } | |
316 | ||
317 | extern int netlink_close(struct nl_handler *handler) | |
318 | { | |
319 | close(handler->fd); | |
320 | handler->fd = -1; | |
321 | return 0; | |
322 | } | |
323 |