]>
Commit | Line | Data |
---|---|---|
0ad19a3f | 1 | /* |
2 | * lxc: linux Container library | |
3 | * | |
4 | * (C) Copyright IBM Corp. 2007, 2008 | |
5 | * | |
6 | * Authors: | |
7 | * Daniel Lezcano <dlezcano at fr.ibm.com> | |
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 | #include <nl.h> | |
33 | ||
34 | #define NLMSG_TAIL(nmsg) \ | |
35 | ((struct rtattr *) (((void *) (nmsg)) + NLMSG_ALIGN((nmsg)->nlmsg_len))) | |
36 | ||
37 | extern size_t nlmsg_len(const struct nlmsg *nlmsg) | |
38 | { | |
39 | return nlmsg->nlmsghdr.nlmsg_len - NLMSG_HDRLEN; | |
40 | } | |
41 | ||
42 | extern void *nlmsg_data(struct nlmsg *nlmsg) | |
43 | { | |
44 | char *data = ((char *)nlmsg) + NLMSG_ALIGN(sizeof(struct nlmsghdr)); | |
45 | if (!nlmsg_len(nlmsg)) | |
46 | return NULL; | |
47 | return data; | |
48 | } | |
49 | ||
50 | static int nla_put(struct nlmsg *nlmsg, int attr, | |
51 | const void *data, size_t len) | |
52 | { | |
53 | struct rtattr *rta; | |
54 | size_t rtalen = RTA_LENGTH(len); | |
55 | ||
56 | rta = NLMSG_TAIL(&nlmsg->nlmsghdr); | |
57 | rta->rta_type = attr; | |
58 | rta->rta_len = rtalen; | |
59 | memcpy(RTA_DATA(rta), data, len); | |
60 | nlmsg->nlmsghdr.nlmsg_len = | |
61 | NLMSG_ALIGN(nlmsg->nlmsghdr.nlmsg_len) + RTA_ALIGN(rtalen); | |
62 | return 0; | |
63 | } | |
64 | ||
65 | extern int nla_put_buffer(struct nlmsg *nlmsg, int attr, | |
66 | const void *data, size_t size) | |
67 | { | |
68 | return nla_put(nlmsg, attr, data, size); | |
69 | } | |
70 | ||
71 | extern int nla_put_string(struct nlmsg *nlmsg, int attr, const char *string) | |
72 | { | |
73 | return nla_put(nlmsg, attr, string, strlen(string) + 1); | |
74 | } | |
75 | ||
76 | extern int nla_put_u32(struct nlmsg *nlmsg, int attr, int value) | |
77 | { | |
78 | return nla_put(nlmsg, attr, &value, sizeof(value)); | |
79 | } | |
80 | ||
9ddaf3bf JHS |
81 | extern int nla_put_u16(struct nlmsg *nlmsg, int attr, ushort value) |
82 | { | |
83 | return nla_put(nlmsg, attr, &value, 2); | |
84 | } | |
85 | ||
0ad19a3f | 86 | extern int nla_put_attr(struct nlmsg *nlmsg, int attr) |
87 | { | |
88 | return nla_put(nlmsg, attr, NULL, 0); | |
89 | } | |
90 | ||
91 | struct rtattr *nla_begin_nested(struct nlmsg *nlmsg, int attr) | |
92 | { | |
93 | struct rtattr *rtattr = NLMSG_TAIL(&nlmsg->nlmsghdr); | |
94 | ||
95 | if (nla_put_attr(nlmsg, attr)) | |
96 | return NULL; | |
97 | ||
98 | return rtattr; | |
99 | } | |
100 | ||
101 | void nla_end_nested(struct nlmsg *nlmsg, struct rtattr *attr) | |
102 | { | |
103 | attr->rta_len = (void *)NLMSG_TAIL(&nlmsg->nlmsghdr) - (void *)attr; | |
104 | } | |
105 | ||
106 | extern struct nlmsg *nlmsg_alloc(size_t size) | |
107 | { | |
108 | struct nlmsg *nlmsg; | |
109 | size_t len = NLMSG_ALIGN(size) + NLMSG_ALIGN(sizeof(struct nlmsghdr *)); | |
110 | ||
111 | nlmsg = (struct nlmsg *)malloc(len); | |
112 | if (!nlmsg) | |
113 | return NULL; | |
114 | ||
115 | memset(nlmsg, 0, len); | |
116 | nlmsg->nlmsghdr.nlmsg_len = NLMSG_ALIGN(size); | |
117 | ||
118 | return nlmsg; | |
119 | } | |
120 | ||
121 | extern void nlmsg_free(struct nlmsg *nlmsg) | |
122 | { | |
123 | free(nlmsg); | |
124 | } | |
125 | ||
126 | extern int netlink_rcv(struct nl_handler *handler, struct nlmsg *answer) | |
127 | { | |
128 | int ret; | |
129 | struct sockaddr_nl nladdr; | |
130 | struct iovec iov = { | |
131 | .iov_base = answer, | |
132 | .iov_len = answer->nlmsghdr.nlmsg_len, | |
133 | }; | |
134 | ||
135 | struct msghdr msg = { | |
136 | .msg_name = &nladdr, | |
137 | .msg_namelen = sizeof(nladdr), | |
138 | .msg_iov = &iov, | |
139 | .msg_iovlen = 1, | |
140 | }; | |
141 | ||
142 | memset(&nladdr, 0, sizeof(nladdr)); | |
143 | nladdr.nl_family = AF_NETLINK; | |
144 | nladdr.nl_pid = 0; | |
145 | nladdr.nl_groups = 0; | |
146 | ||
147 | again: | |
148 | ret = recvmsg(handler->fd, &msg, 0); | |
149 | if (ret < 0) { | |
150 | if (errno == EINTR) | |
151 | goto again; | |
152 | return -errno; | |
153 | } | |
154 | ||
155 | if (!ret) | |
156 | return 0; | |
157 | ||
e239ff31 DL |
158 | if (msg.msg_flags & MSG_TRUNC && |
159 | ret == answer->nlmsghdr.nlmsg_len) | |
0ad19a3f | 160 | return -EMSGSIZE; |
161 | ||
162 | return ret; | |
163 | } | |
164 | ||
165 | extern int netlink_send(struct nl_handler *handler, struct nlmsg *nlmsg) | |
166 | { | |
167 | struct sockaddr_nl nladdr; | |
168 | struct iovec iov = { | |
169 | .iov_base = (void*)nlmsg, | |
170 | .iov_len = nlmsg->nlmsghdr.nlmsg_len, | |
171 | }; | |
172 | struct msghdr msg = { | |
173 | .msg_name = &nladdr, | |
174 | .msg_namelen = sizeof(nladdr), | |
175 | .msg_iov = &iov, | |
176 | .msg_iovlen = 1, | |
177 | }; | |
178 | int ret; | |
179 | ||
180 | memset(&nladdr, 0, sizeof(nladdr)); | |
181 | nladdr.nl_family = AF_NETLINK; | |
182 | nladdr.nl_pid = 0; | |
183 | nladdr.nl_groups = 0; | |
184 | ||
185 | ret = sendmsg(handler->fd, &msg, 0); | |
e239ff31 | 186 | if (ret < 0) |
0ad19a3f | 187 | return -errno; |
0ad19a3f | 188 | |
189 | return ret; | |
190 | } | |
191 | ||
9ddaf3bf JHS |
192 | #ifndef NLMSG_ERROR |
193 | #define NLMSG_ERROR 0x2 | |
194 | #endif | |
0ad19a3f | 195 | extern int netlink_transaction(struct nl_handler *handler, |
196 | struct nlmsg *request, struct nlmsg *answer) | |
197 | { | |
0ad19a3f | 198 | int ret; |
199 | ||
200 | ret = netlink_send(handler, request); | |
201 | if (ret < 0) | |
202 | return ret; | |
203 | ||
204 | ret = netlink_rcv(handler, answer); | |
205 | if (ret < 0) | |
206 | return ret; | |
207 | ||
208 | if (answer->nlmsghdr.nlmsg_type == NLMSG_ERROR) { | |
209 | struct nlmsgerr *err = (struct nlmsgerr*)NLMSG_DATA(answer); | |
3cfc0f3a | 210 | return err->error; |
0ad19a3f | 211 | } |
e239ff31 | 212 | |
0ad19a3f | 213 | return 0; |
214 | } | |
215 | ||
216 | extern int netlink_open(struct nl_handler *handler, int protocol) | |
217 | { | |
218 | socklen_t socklen; | |
219 | int sndbuf = 32768; | |
220 | int rcvbuf = 32768; | |
221 | ||
222 | memset(handler, 0, sizeof(*handler)); | |
223 | ||
224 | handler->fd = socket(AF_NETLINK, SOCK_RAW, protocol); | |
225 | if (handler->fd < 0) | |
226 | return -errno; | |
227 | ||
228 | if (setsockopt(handler->fd, SOL_SOCKET, SO_SNDBUF, | |
229 | &sndbuf, sizeof(sndbuf)) < 0) | |
230 | return -errno; | |
231 | ||
232 | if (setsockopt(handler->fd, SOL_SOCKET, SO_RCVBUF, | |
233 | &rcvbuf,sizeof(rcvbuf)) < 0) | |
234 | return -errno; | |
235 | ||
236 | memset(&handler->local, 0, sizeof(handler->local)); | |
237 | handler->local.nl_family = AF_NETLINK; | |
238 | handler->local.nl_groups = 0; | |
239 | ||
240 | if (bind(handler->fd, (struct sockaddr*)&handler->local, | |
241 | sizeof(handler->local)) < 0) | |
242 | return -errno; | |
243 | ||
244 | socklen = sizeof(handler->local); | |
245 | if (getsockname(handler->fd, (struct sockaddr*)&handler->local, | |
246 | &socklen) < 0) | |
247 | return -errno; | |
248 | ||
249 | if (socklen != sizeof(handler->local)) | |
250 | return -EINVAL; | |
251 | ||
252 | if (handler->local.nl_family != AF_NETLINK) | |
253 | return -EINVAL; | |
254 | ||
255 | handler->seq = time(NULL); | |
256 | ||
257 | return 0; | |
258 | } | |
259 | ||
260 | extern int netlink_close(struct nl_handler *handler) | |
261 | { | |
262 | close(handler->fd); | |
263 | handler->fd = -1; | |
264 | return 0; | |
265 | } | |
266 |