]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
9afe19d6 | 7 | * Daniel Lezcano <daniel.lezcano at free.fr> |
0ad19a3f | 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 | |
250b1eec | 21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
0ad19a3f | 22 | */ |
bfcedc7e CB |
23 | |
24 | #include "config.h" | |
25 | ||
0ad19a3f | 26 | #include <sys/socket.h> |
27 | #include <string.h> | |
28 | #include <stdio.h> | |
29 | #include <time.h> | |
30 | #include <unistd.h> | |
31 | #include <errno.h> | |
32 | #include <stdlib.h> | |
33 | #include <linux/netlink.h> | |
34 | #include <linux/rtnetlink.h> | |
f549edcc | 35 | |
bfcedc7e | 36 | #include "log.h" |
f549edcc | 37 | #include "nl.h" |
0ad19a3f | 38 | |
bfcedc7e CB |
39 | lxc_log_define(nl, lxc); |
40 | ||
0ad19a3f | 41 | extern size_t nlmsg_len(const struct nlmsg *nlmsg) |
42 | { | |
06f976ca | 43 | return nlmsg->nlmsghdr->nlmsg_len - NLMSG_HDRLEN; |
0ad19a3f | 44 | } |
45 | ||
46 | extern void *nlmsg_data(struct nlmsg *nlmsg) | |
47 | { | |
06f976ca | 48 | char *data = ((char *)nlmsg) + NLMSG_HDRLEN; |
0ad19a3f | 49 | if (!nlmsg_len(nlmsg)) |
50 | return NULL; | |
51 | return data; | |
52 | } | |
53 | ||
f79d43bb | 54 | static int nla_put(struct nlmsg *nlmsg, int attr, |
0ad19a3f | 55 | const void *data, size_t len) |
56 | { | |
57 | struct rtattr *rta; | |
58 | size_t rtalen = RTA_LENGTH(len); | |
06f976ca | 59 | size_t tlen = NLMSG_ALIGN(nlmsg->nlmsghdr->nlmsg_len) + RTA_ALIGN(rtalen); |
84760c11 | 60 | |
06f976ca SZ |
61 | if (tlen > nlmsg->cap) |
62 | return -ENOMEM; | |
63 | ||
64 | rta = NLMSG_TAIL(nlmsg->nlmsghdr); | |
65 | rta->rta_type = attr; | |
66 | rta->rta_len = rtalen; | |
c8f05589 RM |
67 | if (data && len) |
68 | memcpy(RTA_DATA(rta), data, len); | |
06f976ca | 69 | nlmsg->nlmsghdr->nlmsg_len = tlen; |
0ad19a3f | 70 | return 0; |
71 | } | |
72 | ||
f79d43bb | 73 | extern int nla_put_buffer(struct nlmsg *nlmsg, int attr, |
0ad19a3f | 74 | const void *data, size_t size) |
75 | { | |
76 | return nla_put(nlmsg, attr, data, size); | |
77 | } | |
78 | ||
79 | extern int nla_put_string(struct nlmsg *nlmsg, int attr, const char *string) | |
80 | { | |
d028235d | 81 | return nla_put(nlmsg, attr, string, strlen(string) + 1); |
0ad19a3f | 82 | } |
83 | ||
84 | extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value) | |
85 | { | |
86 | return nla_put(nlmsg, attr, &value, sizeof(value)); | |
87 | } | |
88 | ||
7c11d57a | 89 | extern int nla_put_u16(struct nlmsg *nlmsg, int attr, unsigned short value) |
9ddaf3bf JHS |
90 | { |
91 | return nla_put(nlmsg, attr, &value, 2); | |
92 | } | |
93 | ||
0ad19a3f | 94 | extern int nla_put_attr(struct nlmsg *nlmsg, int attr) |
95 | { | |
96 | return nla_put(nlmsg, attr, NULL, 0); | |
97 | } | |
98 | ||
99 | struct rtattr *nla_begin_nested(struct nlmsg *nlmsg, int attr) | |
100 | { | |
06f976ca | 101 | struct rtattr *rtattr = NLMSG_TAIL(nlmsg->nlmsghdr); |
0ad19a3f | 102 | |
103 | if (nla_put_attr(nlmsg, attr)) | |
104 | return NULL; | |
105 | ||
106 | return rtattr; | |
107 | } | |
108 | ||
109 | void nla_end_nested(struct nlmsg *nlmsg, struct rtattr *attr) | |
110 | { | |
06f976ca | 111 | attr->rta_len = (void *)NLMSG_TAIL(nlmsg->nlmsghdr) - (void *)attr; |
0ad19a3f | 112 | } |
113 | ||
114 | extern struct nlmsg *nlmsg_alloc(size_t size) | |
115 | { | |
116 | struct nlmsg *nlmsg; | |
b5887164 | 117 | size_t len = NLMSG_HDRLEN + NLMSG_ALIGN(size); |
0ad19a3f | 118 | |
06f976ca | 119 | nlmsg = (struct nlmsg *)malloc(sizeof(struct nlmsg)); |
0ad19a3f | 120 | if (!nlmsg) |
121 | return NULL; | |
122 | ||
06f976ca SZ |
123 | nlmsg->nlmsghdr = (struct nlmsghdr *)malloc(len); |
124 | if (!nlmsg->nlmsghdr) | |
125 | goto errout; | |
126 | ||
127 | memset(nlmsg->nlmsghdr, 0, len); | |
128 | nlmsg->cap = len; | |
129 | nlmsg->nlmsghdr->nlmsg_len = NLMSG_HDRLEN; | |
0ad19a3f | 130 | |
06f976ca SZ |
131 | return nlmsg; |
132 | errout: | |
133 | free(nlmsg); | |
134 | return NULL; | |
135 | } | |
136 | ||
137 | extern void *nlmsg_reserve(struct nlmsg *nlmsg, size_t len) | |
138 | { | |
139 | void *buf; | |
140 | size_t nlmsg_len = nlmsg->nlmsghdr->nlmsg_len; | |
141 | size_t tlen = NLMSG_ALIGN(len); | |
142 | ||
143 | if (nlmsg_len + tlen > nlmsg->cap) | |
144 | return NULL; | |
145 | ||
146 | buf = ((char *)(nlmsg->nlmsghdr)) + nlmsg_len; | |
147 | nlmsg->nlmsghdr->nlmsg_len += tlen; | |
148 | ||
149 | if (tlen > len) | |
150 | memset(buf + len, 0, tlen - len); | |
151 | ||
152 | return buf; | |
153 | } | |
154 | ||
155 | extern struct nlmsg *nlmsg_alloc_reserve(size_t size) | |
156 | { | |
157 | struct nlmsg *nlmsg; | |
158 | ||
159 | nlmsg = nlmsg_alloc(size); | |
160 | if (!nlmsg) | |
161 | return NULL; | |
162 | ||
1a0e70ac | 163 | /* Just set message length to cap directly. */ |
06f976ca | 164 | nlmsg->nlmsghdr->nlmsg_len = nlmsg->cap; |
0ad19a3f | 165 | return nlmsg; |
166 | } | |
167 | ||
168 | extern void nlmsg_free(struct nlmsg *nlmsg) | |
169 | { | |
06f976ca SZ |
170 | if (!nlmsg) |
171 | return; | |
172 | ||
173 | free(nlmsg->nlmsghdr); | |
0ad19a3f | 174 | free(nlmsg); |
175 | } | |
176 | ||
9fbbc427 | 177 | extern int __netlink_recv(struct nl_handler *handler, struct nlmsghdr *nlmsghdr) |
0ad19a3f | 178 | { |
179 | int ret; | |
d028235d SG |
180 | struct sockaddr_nl nladdr; |
181 | struct iovec iov = { | |
9fbbc427 CB |
182 | .iov_base = nlmsghdr, |
183 | .iov_len = nlmsghdr->nlmsg_len, | |
d028235d | 184 | }; |
1a0e70ac | 185 | |
0ad19a3f | 186 | struct msghdr msg = { |
9fbbc427 CB |
187 | .msg_name = &nladdr, |
188 | .msg_namelen = sizeof(nladdr), | |
189 | .msg_iov = &iov, | |
190 | .msg_iovlen = 1, | |
d028235d | 191 | }; |
1a0e70ac | 192 | |
d028235d SG |
193 | memset(&nladdr, 0, sizeof(nladdr)); |
194 | nladdr.nl_family = AF_NETLINK; | |
195 | nladdr.nl_pid = 0; | |
196 | nladdr.nl_groups = 0; | |
0ad19a3f | 197 | |
198 | again: | |
199 | ret = recvmsg(handler->fd, &msg, 0); | |
200 | if (ret < 0) { | |
201 | if (errno == EINTR) | |
202 | goto again; | |
9fbbc427 CB |
203 | |
204 | return -1; | |
0ad19a3f | 205 | } |
206 | ||
207 | if (!ret) | |
208 | return 0; | |
209 | ||
bfcedc7e CB |
210 | if (msg.msg_flags & MSG_TRUNC && (ret == nlmsghdr->nlmsg_len)) { |
211 | errno = EMSGSIZE; | |
212 | ret = -1; | |
213 | } | |
0ad19a3f | 214 | |
215 | return ret; | |
216 | } | |
217 | ||
9fbbc427 CB |
218 | extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *answer) |
219 | { | |
220 | return __netlink_recv(handler, answer->nlmsghdr); | |
221 | } | |
222 | ||
223 | extern int __netlink_send(struct nl_handler *handler, struct nlmsghdr *nlmsghdr) | |
0ad19a3f | 224 | { |
9fbbc427 | 225 | int ret; |
d028235d SG |
226 | struct sockaddr_nl nladdr; |
227 | struct iovec iov = { | |
9fbbc427 CB |
228 | .iov_base = nlmsghdr, |
229 | .iov_len = nlmsghdr->nlmsg_len, | |
d028235d | 230 | }; |
0ad19a3f | 231 | struct msghdr msg = { |
9fbbc427 CB |
232 | .msg_name = &nladdr, |
233 | .msg_namelen = sizeof(nladdr), | |
234 | .msg_iov = &iov, | |
235 | .msg_iovlen = 1, | |
d028235d | 236 | }; |
1a0e70ac | 237 | |
d028235d SG |
238 | memset(&nladdr, 0, sizeof(nladdr)); |
239 | nladdr.nl_family = AF_NETLINK; | |
240 | nladdr.nl_pid = 0; | |
241 | nladdr.nl_groups = 0; | |
0ad19a3f | 242 | |
57b1ab60 | 243 | ret = sendmsg(handler->fd, &msg, MSG_NOSIGNAL); |
e239ff31 | 244 | if (ret < 0) |
9fbbc427 | 245 | return -1; |
0ad19a3f | 246 | |
247 | return ret; | |
248 | } | |
249 | ||
9fbbc427 CB |
250 | extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg) |
251 | { | |
252 | return __netlink_send(handler, nlmsg->nlmsghdr); | |
253 | } | |
254 | ||
255 | extern int __netlink_transaction(struct nl_handler *handler, | |
256 | struct nlmsghdr *request, | |
257 | struct nlmsghdr *answer) | |
0ad19a3f | 258 | { |
0ad19a3f | 259 | int ret; |
260 | ||
9fbbc427 | 261 | ret = __netlink_send(handler, request); |
0ad19a3f | 262 | if (ret < 0) |
bfcedc7e | 263 | return -1; |
0ad19a3f | 264 | |
9fbbc427 | 265 | ret = __netlink_recv(handler, answer); |
0ad19a3f | 266 | if (ret < 0) |
bfcedc7e | 267 | return -1; |
0ad19a3f | 268 | |
bfcedc7e | 269 | ret = 0; |
9fbbc427 CB |
270 | if (answer->nlmsg_type == NLMSG_ERROR) { |
271 | struct nlmsgerr *err = (struct nlmsgerr *)NLMSG_DATA(answer); | |
bfcedc7e CB |
272 | errno = -err->error; |
273 | if (err->error < 0) | |
274 | ret = -1; | |
0ad19a3f | 275 | } |
e239ff31 | 276 | |
bfcedc7e | 277 | return ret; |
0ad19a3f | 278 | } |
279 | ||
9fbbc427 CB |
280 | extern int netlink_transaction(struct nl_handler *handler, |
281 | struct nlmsg *request, struct nlmsg *answer) | |
282 | { | |
283 | return __netlink_transaction(handler, request->nlmsghdr, | |
284 | answer->nlmsghdr); | |
285 | } | |
286 | ||
0ad19a3f | 287 | extern int netlink_open(struct nl_handler *handler, int protocol) |
288 | { | |
289 | socklen_t socklen; | |
d028235d SG |
290 | int sndbuf = 32768; |
291 | int rcvbuf = 32768; | |
b73e5969 | 292 | int err; |
0ad19a3f | 293 | |
d028235d | 294 | memset(handler, 0, sizeof(*handler)); |
0ad19a3f | 295 | |
d028235d SG |
296 | handler->fd = socket(AF_NETLINK, SOCK_RAW, protocol); |
297 | if (handler->fd < 0) | |
298 | return -errno; | |
0ad19a3f | 299 | |
d028235d | 300 | if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF, |
0ad19a3f | 301 | &sndbuf, sizeof(sndbuf)) < 0) |
b73e5969 | 302 | goto err_with_errno; |
0ad19a3f | 303 | |
d028235d | 304 | if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF, |
0ad19a3f | 305 | &rcvbuf,sizeof(rcvbuf)) < 0) |
b73e5969 | 306 | goto err_with_errno; |
0ad19a3f | 307 | |
d028235d SG |
308 | memset(&handler->local, 0, sizeof(handler->local)); |
309 | handler->local.nl_family = AF_NETLINK; | |
310 | handler->local.nl_groups = 0; | |
0ad19a3f | 311 | |
d028235d | 312 | if (bind(handler->fd, (struct sockaddr*)&handler->local, |
0ad19a3f | 313 | sizeof(handler->local)) < 0) |
b73e5969 | 314 | goto err_with_errno; |
0ad19a3f | 315 | |
d028235d SG |
316 | socklen = sizeof(handler->local); |
317 | if (getsockname(handler->fd, (struct sockaddr*)&handler->local, | |
0ad19a3f | 318 | &socklen) < 0) |
b73e5969 | 319 | goto err_with_errno; |
0ad19a3f | 320 | |
b73e5969 WB |
321 | if (socklen != sizeof(handler->local)) { |
322 | err = -EINVAL; | |
323 | goto errclose; | |
324 | } | |
0ad19a3f | 325 | |
b73e5969 WB |
326 | if (handler->local.nl_family != AF_NETLINK) { |
327 | err = -EINVAL; | |
328 | goto errclose; | |
329 | } | |
0ad19a3f | 330 | |
331 | handler->seq = time(NULL); | |
332 | ||
d028235d | 333 | return 0; |
b73e5969 WB |
334 | err_with_errno: |
335 | err = -errno; | |
336 | errclose: | |
337 | close(handler->fd); | |
338 | return err; | |
0ad19a3f | 339 | } |
340 | ||
341 | extern int netlink_close(struct nl_handler *handler) | |
342 | { | |
343 | close(handler->fd); | |
344 | handler->fd = -1; | |
345 | return 0; | |
346 | } | |
347 |