]>
Commit | Line | Data |
---|---|---|
cc73685d | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
0b843d35 | 2 | |
d38dd64a CB |
3 | #ifndef _GNU_SOURCE |
4 | #define _GNU_SOURCE 1 | |
5 | #endif | |
6 | #include <arpa/inet.h> | |
f9373e40 | 7 | #include <ctype.h> |
0b843d35 | 8 | #include <stdio.h> |
ce2f5ae8 | 9 | #include <stdlib.h> |
0b843d35 CB |
10 | #include <string.h> |
11 | ||
663e9916 | 12 | #include "conf.h" |
d38dd64a | 13 | #include "config.h" |
ce2f5ae8 CB |
14 | #include "confile.h" |
15 | #include "confile_utils.h" | |
16 | #include "error.h" | |
ce2f5ae8 | 17 | #include "list.h" |
28d9e29e CB |
18 | #include "log.h" |
19 | #include "lxccontainer.h" | |
7b15813c | 20 | #include "macro.h" |
a4809e4e | 21 | #include "memory_utils.h" |
811ef482 | 22 | #include "network.h" |
f9373e40 | 23 | #include "parse.h" |
0b843d35 CB |
24 | #include "utils.h" |
25 | ||
18cd4b54 DJ |
26 | #ifndef HAVE_STRLCPY |
27 | #include "include/strlcpy.h" | |
28 | #endif | |
29 | ||
ac2cecc4 | 30 | lxc_log_define(confile_utils, lxc); |
ce2f5ae8 | 31 | |
0b843d35 CB |
32 | int parse_idmaps(const char *idmap, char *type, unsigned long *nsid, |
33 | unsigned long *hostid, unsigned long *range) | |
34 | { | |
a4809e4e | 35 | __do_free char *dup = NULL; |
0b843d35 CB |
36 | int ret = -1; |
37 | unsigned long tmp_hostid, tmp_nsid, tmp_range; | |
38 | char tmp_type; | |
39 | char *window, *slide; | |
0b843d35 CB |
40 | |
41 | /* Duplicate string. */ | |
42 | dup = strdup(idmap); | |
43 | if (!dup) | |
a4809e4e | 44 | return ret_errno(ENOMEM); |
0b843d35 CB |
45 | |
46 | /* A prototypical idmap entry would be: "u 1000 1000000 65536" */ | |
47 | ||
48 | /* align */ | |
49 | slide = window = dup; | |
50 | /* skip whitespace */ | |
51 | slide += strspn(slide, " \t\r"); | |
52 | if (slide != window && *slide == '\0') | |
a4809e4e | 53 | return ret_errno(EINVAL); |
0b843d35 CB |
54 | |
55 | /* Validate type. */ | |
a4809e4e CB |
56 | if (*slide != 'u' && *slide != 'g') |
57 | return log_error_errno(-EINVAL, EINVAL, "Invalid id mapping type: %c", *slide); | |
a8b1ac78 | 58 | |
0b843d35 CB |
59 | /* Assign type. */ |
60 | tmp_type = *slide; | |
61 | ||
62 | /* move beyond type */ | |
63 | slide++; | |
64 | /* align */ | |
65 | window = slide; | |
66 | /* Validate that only whitespace follows. */ | |
67 | slide += strspn(slide, " \t\r"); | |
68 | /* There must be whitespace. */ | |
69 | if (slide == window) | |
a4809e4e | 70 | return ret_errno(EINVAL); |
0b843d35 | 71 | |
f37d1c22 | 72 | /* Mark beginning of nsid. */ |
0b843d35 CB |
73 | window = slide; |
74 | /* Validate that non-whitespace follows. */ | |
75 | slide += strcspn(slide, " \t\r"); | |
76 | /* There must be non-whitespace. */ | |
77 | if (slide == window || *slide == '\0') | |
a4809e4e | 78 | return ret_errno(EINVAL); |
f37d1c22 | 79 | /* Mark end of nsid. */ |
0b843d35 CB |
80 | *slide = '\0'; |
81 | ||
f37d1c22 | 82 | /* Parse nsid. */ |
a4809e4e CB |
83 | ret = lxc_safe_ulong(window, &tmp_nsid); |
84 | if (ret < 0) | |
85 | return log_error_errno(ret, errno, "Failed to parse nsid: %s", window); | |
0b843d35 CB |
86 | |
87 | /* Move beyond \0. */ | |
88 | slide++; | |
0b843d35 CB |
89 | /* Validate that only whitespace follows. */ |
90 | slide += strspn(slide, " \t\r"); | |
91 | /* If there was only one whitespace then we whiped it with our \0 above. | |
92 | * So only ensure that we're not at the end of the string. | |
93 | */ | |
94 | if (*slide == '\0') | |
a4809e4e | 95 | return ret_errno(EINVAL); |
0b843d35 CB |
96 | |
97 | /* Mark beginning of hostid. */ | |
98 | window = slide; | |
99 | /* Validate that non-whitespace follows. */ | |
100 | slide += strcspn(slide, " \t\r"); | |
101 | /* There must be non-whitespace. */ | |
102 | if (slide == window || *slide == '\0') | |
a4809e4e | 103 | return ret_errno(EINVAL); |
f37d1c22 | 104 | /* Mark end of nsid. */ |
0b843d35 CB |
105 | *slide = '\0'; |
106 | ||
107 | /* Parse hostid. */ | |
a4809e4e CB |
108 | ret = lxc_safe_ulong(window, &tmp_hostid); |
109 | if (ret < 0) | |
110 | return log_error_errno(ret, errno, "Failed to parse hostid: %s", window); | |
0b843d35 CB |
111 | |
112 | /* Move beyond \0. */ | |
113 | slide++; | |
0b843d35 CB |
114 | /* Validate that only whitespace follows. */ |
115 | slide += strspn(slide, " \t\r"); | |
116 | /* If there was only one whitespace then we whiped it with our \0 above. | |
117 | * So only ensure that we're not at the end of the string. | |
118 | */ | |
119 | if (*slide == '\0') | |
a4809e4e | 120 | return ret_errno(EINVAL); |
0b843d35 CB |
121 | |
122 | /* Mark beginning of range. */ | |
123 | window = slide; | |
124 | /* Validate that non-whitespace follows. */ | |
125 | slide += strcspn(slide, " \t\r"); | |
126 | /* There must be non-whitespace. */ | |
127 | if (slide == window) | |
a4809e4e | 128 | return ret_errno(EINVAL); |
0b843d35 CB |
129 | |
130 | /* The range is the last valid entry we expect. So make sure that there | |
f37d1c22 | 131 | * is no trailing garbage and if there is, error out. |
0b843d35 CB |
132 | */ |
133 | if (*(slide + strspn(slide, " \t\r\n")) != '\0') | |
a4809e4e | 134 | return ret_errno(EINVAL); |
29c98ddd | 135 | |
0b843d35 CB |
136 | /* Mark end of range. */ |
137 | *slide = '\0'; | |
138 | ||
139 | /* Parse range. */ | |
a4809e4e CB |
140 | ret = lxc_safe_ulong(window, &tmp_range); |
141 | if (ret < 0) | |
142 | return log_error_errno(ret, errno, "Failed to parse id mapping range: %s", window); | |
0b843d35 | 143 | |
a4809e4e CB |
144 | *type = tmp_type; |
145 | *nsid = tmp_nsid; | |
0b843d35 | 146 | *hostid = tmp_hostid; |
a4809e4e | 147 | *range = tmp_range; |
0b843d35 CB |
148 | |
149 | /* Yay, we survived. */ | |
a4809e4e | 150 | return 0; |
0b843d35 | 151 | } |
663e9916 CB |
152 | |
153 | bool lxc_config_value_empty(const char *value) | |
154 | { | |
155 | if (value && strlen(value) > 0) | |
156 | return false; | |
157 | ||
158 | return true; | |
159 | } | |
ce2f5ae8 | 160 | |
c302b476 | 161 | struct lxc_netdev *lxc_network_add(struct lxc_list *networks, int idx, bool tail) |
ce2f5ae8 | 162 | { |
1e323af6 CB |
163 | __do_free struct lxc_list *newlist = NULL; |
164 | __do_free struct lxc_netdev *netdev = NULL; | |
ce2f5ae8 CB |
165 | |
166 | /* network does not exist */ | |
167 | netdev = malloc(sizeof(*netdev)); | |
168 | if (!netdev) | |
1e323af6 | 169 | return ret_set_errno(NULL, ENOMEM); |
ce2f5ae8 CB |
170 | |
171 | memset(netdev, 0, sizeof(*netdev)); | |
172 | lxc_list_init(&netdev->ipv4); | |
173 | lxc_list_init(&netdev->ipv6); | |
174 | ||
175 | /* give network a unique index */ | |
176 | netdev->idx = idx; | |
177 | ||
178 | /* prepare new list */ | |
179 | newlist = malloc(sizeof(*newlist)); | |
1e323af6 CB |
180 | if (!newlist) |
181 | return ret_set_errno(NULL, ENOMEM); | |
ce2f5ae8 CB |
182 | |
183 | lxc_list_init(newlist); | |
184 | newlist->elem = netdev; | |
185 | ||
c302b476 CB |
186 | if (tail) |
187 | lxc_list_add_tail(networks, newlist); | |
188 | else | |
189 | lxc_list_add(networks, newlist); | |
1e323af6 | 190 | move_ptr(newlist); |
29c98ddd | 191 | |
1e323af6 | 192 | return move_ptr(netdev); |
ce2f5ae8 | 193 | } |
1ed6ba91 | 194 | |
c302b476 CB |
195 | /* Takes care of finding the correct netdev struct in the networks list or |
196 | * allocates a new one if it couldn't be found. | |
197 | */ | |
198 | struct lxc_netdev *lxc_get_netdev_by_idx(struct lxc_conf *conf, | |
199 | unsigned int idx, bool allocate) | |
200 | { | |
201 | struct lxc_netdev *netdev = NULL; | |
202 | struct lxc_list *networks = &conf->network; | |
203 | struct lxc_list *insert = networks; | |
204 | ||
205 | /* lookup network */ | |
206 | if (!lxc_list_empty(networks)) { | |
207 | lxc_list_for_each(insert, networks) { | |
208 | netdev = insert->elem; | |
209 | if (netdev->idx == idx) | |
210 | return netdev; | |
211 | else if (netdev->idx > idx) | |
212 | break; | |
213 | } | |
214 | } | |
215 | ||
216 | if (!allocate) | |
2a655c0d | 217 | return ret_set_errno(NULL, EINVAL); |
c302b476 CB |
218 | |
219 | return lxc_network_add(insert, idx, true); | |
220 | } | |
221 | ||
1ed6ba91 CB |
222 | void lxc_log_configured_netdevs(const struct lxc_conf *conf) |
223 | { | |
224 | struct lxc_netdev *netdev; | |
225 | struct lxc_list *it = (struct lxc_list *)&conf->network;; | |
226 | ||
227 | if ((conf->loglevel != LXC_LOG_LEVEL_TRACE) && | |
228 | (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE)) | |
229 | return; | |
230 | ||
231 | if (lxc_list_empty(it)) { | |
232 | TRACE("container has no networks configured"); | |
233 | return; | |
234 | } | |
235 | ||
236 | lxc_list_for_each(it, &conf->network) { | |
9b0df30f CB |
237 | struct lxc_list *cur, *next; |
238 | struct lxc_inetdev *inet4dev; | |
239 | struct lxc_inet6dev *inet6dev; | |
240 | char bufinet4[INET_ADDRSTRLEN], bufinet6[INET6_ADDRSTRLEN]; | |
241 | ||
1ed6ba91 CB |
242 | netdev = it->elem; |
243 | ||
c302b476 | 244 | TRACE("index: %zd", netdev->idx); |
7a582518 | 245 | TRACE("ifindex: %d", netdev->ifindex); |
29c98ddd | 246 | |
1ed6ba91 CB |
247 | switch (netdev->type) { |
248 | case LXC_NET_VETH: | |
249 | TRACE("type: veth"); | |
134ded24 | 250 | TRACE("veth mode: %d", netdev->priv.veth_attr.mode); |
29c98ddd | 251 | |
de4855a8 | 252 | if (netdev->priv.veth_attr.pair[0] != '\0') |
9b0df30f CB |
253 | TRACE("veth pair: %s", |
254 | netdev->priv.veth_attr.pair); | |
29c98ddd | 255 | |
8ce727fc CB |
256 | if (netdev->priv.veth_attr.veth1[0] != '\0') |
257 | TRACE("veth1 : %s", | |
258 | netdev->priv.veth_attr.veth1); | |
29c98ddd | 259 | |
d952b351 CB |
260 | if (netdev->priv.veth_attr.ifindex > 0) |
261 | TRACE("host side ifindex for veth device: %d", | |
262 | netdev->priv.veth_attr.ifindex); | |
134ded24 TP |
263 | |
264 | if (netdev->priv.veth_attr.vlan_id_set) | |
265 | TRACE("veth vlan id: %d", netdev->priv.veth_attr.vlan_id); | |
266 | ||
1f92ddc1 TP |
267 | lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) { |
268 | unsigned short vlan_tagged_id = PTR_TO_USHORT(cur->elem); | |
269 | TRACE("veth vlan tagged id: %u", vlan_tagged_id); | |
270 | } | |
271 | ||
1ed6ba91 CB |
272 | break; |
273 | case LXC_NET_MACVLAN: | |
274 | TRACE("type: macvlan"); | |
29c98ddd | 275 | |
9b0df30f | 276 | if (netdev->priv.macvlan_attr.mode > 0) { |
7b15813c | 277 | char *mode; |
29c98ddd | 278 | |
7b15813c | 279 | mode = lxc_macvlan_flag_to_mode( |
9b0df30f CB |
280 | netdev->priv.macvlan_attr.mode); |
281 | TRACE("macvlan mode: %s", | |
7b15813c | 282 | mode ? mode : "(invalid mode)"); |
9b0df30f | 283 | } |
1ed6ba91 | 284 | break; |
c9f52382 | 285 | case LXC_NET_IPVLAN: |
286 | TRACE("type: ipvlan"); | |
287 | ||
288 | char *mode; | |
289 | mode = lxc_ipvlan_flag_to_mode(netdev->priv.ipvlan_attr.mode); | |
290 | TRACE("ipvlan mode: %s", mode ? mode : "(invalid mode)"); | |
291 | ||
292 | char *isolation; | |
293 | isolation = lxc_ipvlan_flag_to_isolation(netdev->priv.ipvlan_attr.isolation); | |
294 | TRACE("ipvlan isolation: %s", isolation ? isolation : "(invalid isolation)"); | |
295 | break; | |
1ed6ba91 CB |
296 | case LXC_NET_VLAN: |
297 | TRACE("type: vlan"); | |
9b0df30f | 298 | TRACE("vlan id: %d", netdev->priv.vlan_attr.vid); |
1ed6ba91 CB |
299 | break; |
300 | case LXC_NET_PHYS: | |
301 | TRACE("type: phys"); | |
29c98ddd | 302 | |
303 | if (netdev->priv.phys_attr.ifindex > 0) | |
b809f232 CB |
304 | TRACE("host side ifindex for phys device: %d", |
305 | netdev->priv.phys_attr.ifindex); | |
1ed6ba91 CB |
306 | break; |
307 | case LXC_NET_EMPTY: | |
308 | TRACE("type: empty"); | |
309 | break; | |
310 | case LXC_NET_NONE: | |
311 | TRACE("type: none"); | |
312 | break; | |
313 | default: | |
d4a7da46 | 314 | ERROR("Invalid network type %d", netdev->type); |
1ed6ba91 CB |
315 | return; |
316 | } | |
317 | ||
9b0df30f CB |
318 | if (netdev->type != LXC_NET_EMPTY) { |
319 | TRACE("flags: %s", | |
320 | netdev->flags == IFF_UP ? "up" : "none"); | |
29c98ddd | 321 | |
de4855a8 | 322 | if (netdev->link[0] != '\0') |
9b0df30f | 323 | TRACE("link: %s", netdev->link); |
29c98ddd | 324 | |
6509154d | 325 | /* l2proxy only used when link is specified */ |
326 | if (netdev->link[0] != '\0') | |
327 | TRACE("l2proxy: %s", netdev->l2proxy ? "true" : "false"); | |
328 | ||
de4855a8 | 329 | if (netdev->name[0] != '\0') |
9b0df30f | 330 | TRACE("name: %s", netdev->name); |
29c98ddd | 331 | |
9b0df30f CB |
332 | if (netdev->hwaddr) |
333 | TRACE("hwaddr: %s", netdev->hwaddr); | |
29c98ddd | 334 | |
9b0df30f CB |
335 | if (netdev->mtu) |
336 | TRACE("mtu: %s", netdev->mtu); | |
29c98ddd | 337 | |
9b0df30f CB |
338 | if (netdev->upscript) |
339 | TRACE("upscript: %s", netdev->upscript); | |
29c98ddd | 340 | |
9b0df30f CB |
341 | if (netdev->downscript) |
342 | TRACE("downscript: %s", netdev->downscript); | |
343 | ||
344 | TRACE("ipv4 gateway auto: %s", | |
345 | netdev->ipv4_gateway_auto ? "true" : "false"); | |
346 | ||
a2f9a670 | 347 | TRACE("ipv4 gateway dev: %s", |
348 | netdev->ipv4_gateway_dev ? "true" : "false"); | |
349 | ||
9b0df30f CB |
350 | if (netdev->ipv4_gateway) { |
351 | inet_ntop(AF_INET, netdev->ipv4_gateway, | |
352 | bufinet4, sizeof(bufinet4)); | |
353 | TRACE("ipv4 gateway: %s", bufinet4); | |
354 | } | |
355 | ||
356 | lxc_list_for_each_safe(cur, &netdev->ipv4, next) { | |
357 | inet4dev = cur->elem; | |
358 | inet_ntop(AF_INET, &inet4dev->addr, bufinet4, | |
359 | sizeof(bufinet4)); | |
360 | TRACE("ipv4 addr: %s", bufinet4); | |
361 | } | |
362 | ||
363 | TRACE("ipv6 gateway auto: %s", | |
364 | netdev->ipv6_gateway_auto ? "true" : "false"); | |
29c98ddd | 365 | |
a2f9a670 | 366 | TRACE("ipv6 gateway dev: %s", |
367 | netdev->ipv6_gateway_dev ? "true" : "false"); | |
368 | ||
9b0df30f CB |
369 | if (netdev->ipv6_gateway) { |
370 | inet_ntop(AF_INET6, netdev->ipv6_gateway, | |
371 | bufinet6, sizeof(bufinet6)); | |
372 | TRACE("ipv6 gateway: %s", bufinet6); | |
373 | } | |
29c98ddd | 374 | |
9b0df30f CB |
375 | lxc_list_for_each_safe(cur, &netdev->ipv6, next) { |
376 | inet6dev = cur->elem; | |
377 | inet_ntop(AF_INET6, &inet6dev->addr, bufinet6, | |
378 | sizeof(bufinet6)); | |
379 | TRACE("ipv6 addr: %s", bufinet6); | |
380 | } | |
d4a7da46 | 381 | |
382 | if (netdev->type == LXC_NET_VETH) { | |
383 | lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) { | |
384 | inet4dev = cur->elem; | |
385 | if (!inet_ntop(AF_INET, &inet4dev->addr, bufinet4, sizeof(bufinet4))) { | |
386 | ERROR("Invalid ipv4 veth route"); | |
387 | return; | |
388 | } | |
389 | ||
390 | TRACE("ipv4 veth route: %s/%u", bufinet4, inet4dev->prefix); | |
391 | } | |
392 | ||
393 | lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) { | |
394 | inet6dev = cur->elem; | |
395 | if (!inet_ntop(AF_INET6, &inet6dev->addr, bufinet6, sizeof(bufinet6))) { | |
396 | ERROR("Invalid ipv6 veth route"); | |
397 | return; | |
398 | } | |
399 | ||
400 | TRACE("ipv6 veth route: %s/%u", bufinet6, inet6dev->prefix); | |
401 | } | |
402 | } | |
9b0df30f | 403 | } |
1ed6ba91 CB |
404 | } |
405 | } | |
519df1c1 | 406 | |
e5d2fd7c CB |
407 | static void lxc_free_netdev(struct lxc_netdev *netdev) |
408 | { | |
409 | struct lxc_list *cur, *next; | |
410 | ||
06db6101 CB |
411 | if (!netdev) |
412 | return; | |
413 | ||
e5d2fd7c CB |
414 | free(netdev->upscript); |
415 | free(netdev->downscript); | |
416 | free(netdev->hwaddr); | |
417 | free(netdev->mtu); | |
418 | ||
419 | free(netdev->ipv4_gateway); | |
420 | lxc_list_for_each_safe(cur, &netdev->ipv4, next) { | |
421 | lxc_list_del(cur); | |
422 | free(cur->elem); | |
423 | free(cur); | |
424 | } | |
425 | ||
426 | free(netdev->ipv6_gateway); | |
427 | lxc_list_for_each_safe(cur, &netdev->ipv6, next) { | |
428 | lxc_list_del(cur); | |
429 | free(cur->elem); | |
430 | free(cur); | |
431 | } | |
432 | ||
d4a7da46 | 433 | if (netdev->type == LXC_NET_VETH) { |
434 | lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv4_routes, next) { | |
435 | lxc_list_del(cur); | |
436 | free(cur->elem); | |
437 | free(cur); | |
438 | } | |
439 | ||
440 | lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.ipv6_routes, next) { | |
441 | lxc_list_del(cur); | |
442 | free(cur->elem); | |
443 | free(cur); | |
444 | } | |
3a0049f3 TP |
445 | |
446 | lxc_list_for_each_safe(cur, &netdev->priv.veth_attr.vlan_tagged_ids, next) { | |
447 | lxc_list_del(cur); | |
448 | free(cur); | |
449 | } | |
d4a7da46 | 450 | } |
451 | ||
e5d2fd7c CB |
452 | free(netdev); |
453 | } | |
454 | ||
06db6101 CB |
455 | define_cleanup_function(struct lxc_netdev *, lxc_free_netdev); |
456 | ||
519df1c1 CB |
457 | bool lxc_remove_nic_by_idx(struct lxc_conf *conf, unsigned int idx) |
458 | { | |
06db6101 | 459 | call_cleaner(lxc_free_netdev) struct lxc_netdev *netdev = NULL; |
e5d2fd7c | 460 | struct lxc_list *cur, *next; |
519df1c1 CB |
461 | |
462 | lxc_list_for_each_safe(cur, &conf->network, next) { | |
463 | netdev = cur->elem; | |
464 | if (netdev->idx != idx) | |
465 | continue; | |
466 | ||
467 | lxc_list_del(cur); | |
06db6101 CB |
468 | free(cur); |
469 | return true; | |
519df1c1 CB |
470 | } |
471 | ||
06db6101 | 472 | return false; |
519df1c1 | 473 | } |
e5d2fd7c | 474 | |
c302b476 | 475 | void lxc_free_networks(struct lxc_list *networks) |
e5d2fd7c CB |
476 | { |
477 | struct lxc_list *cur, *next; | |
478 | struct lxc_netdev *netdev; | |
479 | ||
c302b476 | 480 | lxc_list_for_each_safe(cur, networks, next) { |
e5d2fd7c CB |
481 | netdev = cur->elem; |
482 | lxc_free_netdev(netdev); | |
483 | free(cur); | |
484 | } | |
485 | ||
486 | /* prevent segfaults */ | |
c302b476 | 487 | lxc_list_init(networks); |
e5d2fd7c | 488 | } |
9b0df30f | 489 | |
3f0ed090 TP |
490 | |
491 | static struct lxc_veth_mode { | |
492 | char *name; | |
493 | int mode; | |
494 | } veth_mode[] = { | |
495 | { "bridge", VETH_MODE_BRIDGE }, | |
496 | { "router", VETH_MODE_ROUTER }, | |
497 | }; | |
498 | ||
499 | int lxc_veth_mode_to_flag(int *mode, const char *value) | |
500 | { | |
501 | for (size_t i = 0; i < sizeof(veth_mode) / sizeof(veth_mode[0]); i++) { | |
502 | if (strcmp(veth_mode[i].name, value) != 0) | |
503 | continue; | |
504 | ||
505 | *mode = veth_mode[i].mode; | |
506 | return 0; | |
507 | } | |
508 | ||
bf39128d | 509 | return ret_set_errno(-1, EINVAL); |
3f0ed090 TP |
510 | } |
511 | ||
756cadb6 CB |
512 | char *lxc_veth_flag_to_mode(int mode) |
513 | { | |
514 | for (size_t i = 0; i < sizeof(veth_mode) / sizeof(veth_mode[0]); i++) { | |
515 | if (veth_mode[i].mode != mode) | |
516 | continue; | |
517 | ||
518 | return veth_mode[i].name; | |
519 | } | |
520 | ||
521 | return NULL; | |
522 | } | |
523 | ||
7b15813c | 524 | static struct lxc_macvlan_mode { |
9b0df30f CB |
525 | char *name; |
526 | int mode; | |
527 | } macvlan_mode[] = { | |
528 | { "private", MACVLAN_MODE_PRIVATE }, | |
529 | { "vepa", MACVLAN_MODE_VEPA }, | |
530 | { "bridge", MACVLAN_MODE_BRIDGE }, | |
531 | { "passthru", MACVLAN_MODE_PASSTHRU }, | |
532 | }; | |
533 | ||
534 | int lxc_macvlan_mode_to_flag(int *mode, const char *value) | |
535 | { | |
536 | size_t i; | |
537 | ||
538 | for (i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) { | |
539 | if (strcmp(macvlan_mode[i].name, value)) | |
540 | continue; | |
541 | ||
542 | *mode = macvlan_mode[i].mode; | |
543 | return 0; | |
544 | } | |
545 | ||
546 | return -1; | |
547 | } | |
548 | ||
549 | char *lxc_macvlan_flag_to_mode(int mode) | |
550 | { | |
551 | size_t i; | |
552 | ||
553 | for (i = 0; i < sizeof(macvlan_mode) / sizeof(macvlan_mode[0]); i++) { | |
b56680fd | 554 | if (macvlan_mode[i].mode != mode) |
9b0df30f CB |
555 | continue; |
556 | ||
557 | return macvlan_mode[i].name; | |
558 | } | |
559 | ||
560 | return NULL; | |
561 | } | |
f9373e40 | 562 | |
c9f52382 | 563 | static struct lxc_ipvlan_mode { |
564 | char *name; | |
565 | int mode; | |
566 | } ipvlan_mode[] = { | |
567 | { "l3", IPVLAN_MODE_L3 }, | |
568 | { "l3s", IPVLAN_MODE_L3S }, | |
569 | { "l2", IPVLAN_MODE_L2 }, | |
570 | }; | |
571 | ||
572 | int lxc_ipvlan_mode_to_flag(int *mode, const char *value) | |
573 | { | |
574 | for (size_t i = 0; i < sizeof(ipvlan_mode) / sizeof(ipvlan_mode[0]); i++) { | |
575 | if (strcmp(ipvlan_mode[i].name, value) != 0) | |
576 | continue; | |
577 | ||
578 | *mode = ipvlan_mode[i].mode; | |
579 | return 0; | |
580 | } | |
581 | ||
582 | return -1; | |
583 | } | |
584 | ||
585 | char *lxc_ipvlan_flag_to_mode(int mode) | |
586 | { | |
587 | for (size_t i = 0; i < sizeof(ipvlan_mode) / sizeof(ipvlan_mode[0]); i++) { | |
588 | if (ipvlan_mode[i].mode != mode) | |
589 | continue; | |
590 | ||
591 | return ipvlan_mode[i].name; | |
592 | } | |
593 | ||
594 | return NULL; | |
595 | } | |
596 | ||
597 | static struct lxc_ipvlan_isolation { | |
598 | char *name; | |
599 | int flag; | |
600 | } ipvlan_isolation[] = { | |
601 | { "bridge", IPVLAN_ISOLATION_BRIDGE }, | |
602 | { "private", IPVLAN_ISOLATION_PRIVATE }, | |
603 | { "vepa", IPVLAN_ISOLATION_VEPA }, | |
604 | }; | |
605 | ||
606 | int lxc_ipvlan_isolation_to_flag(int *flag, const char *value) | |
607 | { | |
608 | for (size_t i = 0; i < sizeof(ipvlan_isolation) / sizeof(ipvlan_isolation[0]); i++) { | |
609 | if (strcmp(ipvlan_isolation[i].name, value) != 0) | |
610 | continue; | |
611 | ||
612 | *flag = ipvlan_isolation[i].flag; | |
613 | return 0; | |
614 | } | |
615 | ||
616 | return -1; | |
617 | } | |
618 | ||
619 | char *lxc_ipvlan_flag_to_isolation(int flag) | |
620 | { | |
621 | for (size_t i = 0; i < sizeof(ipvlan_isolation) / sizeof(ipvlan_isolation[0]); i++) { | |
622 | if (ipvlan_isolation[i].flag != flag) | |
623 | continue; | |
624 | ||
625 | return ipvlan_isolation[i].name; | |
626 | } | |
627 | ||
628 | return NULL; | |
629 | } | |
630 | ||
f9373e40 CB |
631 | int set_config_string_item(char **conf_item, const char *value) |
632 | { | |
633 | char *new_value; | |
634 | ||
635 | if (lxc_config_value_empty(value)) { | |
636 | free(*conf_item); | |
637 | *conf_item = NULL; | |
638 | return 0; | |
639 | } | |
640 | ||
641 | new_value = strdup(value); | |
642 | if (!new_value) { | |
29c98ddd | 643 | SYSERROR("Failed to duplicate string \"%s\"", value); |
f9373e40 CB |
644 | return -1; |
645 | } | |
646 | ||
647 | free(*conf_item); | |
648 | *conf_item = new_value; | |
649 | return 0; | |
650 | } | |
651 | ||
652 | int set_config_string_item_max(char **conf_item, const char *value, size_t max) | |
653 | { | |
654 | if (strlen(value) >= max) { | |
655 | ERROR("%s is too long (>= %lu)", value, (unsigned long)max); | |
656 | return -1; | |
657 | } | |
658 | ||
659 | return set_config_string_item(conf_item, value); | |
660 | } | |
661 | ||
662 | int set_config_path_item(char **conf_item, const char *value) | |
663 | { | |
664 | return set_config_string_item_max(conf_item, value, PATH_MAX); | |
665 | } | |
666 | ||
8f818a84 MB |
667 | int set_config_bool_item(bool *conf_item, const char *value, bool empty_conf_action) |
668 | { | |
669 | unsigned int val = 0; | |
670 | ||
671 | if (lxc_config_value_empty(value)) { | |
672 | *conf_item = empty_conf_action; | |
673 | return 0; | |
674 | } | |
675 | ||
676 | if (lxc_safe_uint(value, &val) < 0) | |
677 | return -EINVAL; | |
678 | ||
679 | switch (val) { | |
680 | case 0: | |
681 | *conf_item = false; | |
682 | return 0; | |
683 | case 1: | |
684 | *conf_item = true; | |
685 | return 0; | |
686 | } | |
687 | ||
688 | return -EINVAL; | |
689 | } | |
690 | ||
f9373e40 CB |
691 | int config_ip_prefix(struct in_addr *addr) |
692 | { | |
693 | if (IN_CLASSA(addr->s_addr)) | |
694 | return 32 - IN_CLASSA_NSHIFT; | |
29c98ddd | 695 | |
f9373e40 CB |
696 | if (IN_CLASSB(addr->s_addr)) |
697 | return 32 - IN_CLASSB_NSHIFT; | |
29c98ddd | 698 | |
f9373e40 CB |
699 | if (IN_CLASSC(addr->s_addr)) |
700 | return 32 - IN_CLASSC_NSHIFT; | |
701 | ||
702 | return 0; | |
703 | } | |
704 | ||
18cd4b54 | 705 | int network_ifname(char *valuep, const char *value, size_t size) |
f9373e40 | 706 | { |
18cd4b54 DJ |
707 | size_t retlen; |
708 | ||
709 | if (!valuep || !value) | |
710 | return -1; | |
711 | ||
712 | retlen = strlcpy(valuep, value, size); | |
29c98ddd | 713 | if (retlen >= size) |
9005a3ff | 714 | ERROR("Network device name \"%s\" is too long (>= %zu)", value, |
18cd4b54 | 715 | size); |
de4855a8 | 716 | |
de4855a8 | 717 | return 0; |
f9373e40 CB |
718 | } |
719 | ||
3db41a6c CB |
720 | bool lxc_config_net_is_hwaddr(const char *line) |
721 | { | |
722 | unsigned index; | |
723 | char tmp[7]; | |
724 | ||
725 | if (strncmp(line, "lxc.net", 7) != 0) | |
726 | return false; | |
727 | ||
728 | if (strncmp(line, "lxc.net.hwaddr", 14) == 0) | |
729 | return true; | |
730 | ||
731 | if (strncmp(line, "lxc.network.hwaddr", 18) == 0) | |
732 | return true; | |
733 | ||
734 | if (sscanf(line, "lxc.net.%u.%6s", &index, tmp) == 2 || | |
735 | sscanf(line, "lxc.network.%u.%6s", &index, tmp) == 2) | |
736 | return strncmp(tmp, "hwaddr", 6) == 0; | |
737 | ||
738 | return false; | |
739 | } | |
740 | ||
29c98ddd | 741 | void rand_complete_hwaddr(char *hwaddr) |
f9373e40 CB |
742 | { |
743 | const char hex[] = "0123456789abcdef"; | |
744 | char *curs = hwaddr; | |
280cc35f | 745 | #ifdef HAVE_RAND_R |
f9373e40 CB |
746 | unsigned int seed; |
747 | ||
748 | seed = randseed(false); | |
280cc35f | 749 | #else |
750 | ||
751 | (void)randseed(true); | |
f9373e40 | 752 | #endif |
280cc35f | 753 | |
f9373e40 CB |
754 | while (*curs != '\0' && *curs != '\n') { |
755 | if (*curs == 'x' || *curs == 'X') { | |
756 | if (curs - hwaddr == 1) { | |
757 | /* ensure address is unicast */ | |
758 | #ifdef HAVE_RAND_R | |
759 | *curs = hex[rand_r(&seed) & 0x0E]; | |
760 | } else { | |
761 | *curs = hex[rand_r(&seed) & 0x0F]; | |
762 | #else | |
763 | *curs = hex[rand() & 0x0E]; | |
764 | } else { | |
765 | *curs = hex[rand() & 0x0F]; | |
766 | #endif | |
767 | } | |
768 | } | |
769 | curs++; | |
770 | } | |
f9373e40 CB |
771 | } |
772 | ||
f9373e40 CB |
773 | bool new_hwaddr(char *hwaddr) |
774 | { | |
775 | int ret; | |
280cc35f | 776 | #ifdef HAVE_RAND_R |
777 | unsigned int seed; | |
778 | ||
779 | seed = randseed(false); | |
780 | ||
781 | ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand_r(&seed) % 255, | |
782 | rand_r(&seed) % 255, rand_r(&seed) % 255); | |
783 | #else | |
f9373e40 CB |
784 | |
785 | (void)randseed(true); | |
786 | ||
787 | ret = snprintf(hwaddr, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255, | |
788 | rand() % 255, rand() % 255); | |
280cc35f | 789 | #endif |
f9373e40 | 790 | if (ret < 0 || ret >= 18) { |
280cc35f | 791 | SYSERROR("Failed to call snprintf()"); |
f9373e40 CB |
792 | return false; |
793 | } | |
794 | ||
795 | return true; | |
796 | } | |
953fe44f CB |
797 | |
798 | int lxc_get_conf_str(char *retv, int inlen, const char *value) | |
799 | { | |
d3bdf12c CB |
800 | size_t value_len; |
801 | ||
953fe44f CB |
802 | if (!value) |
803 | return 0; | |
d3bdf12c CB |
804 | |
805 | value_len = strlen(value); | |
806 | if (retv && inlen >= value_len + 1) | |
807 | memcpy(retv, value, value_len + 1); | |
953fe44f | 808 | |
29c98ddd | 809 | return value_len; |
953fe44f CB |
810 | } |
811 | ||
6e54330c CB |
812 | int lxc_get_conf_bool(struct lxc_conf *c, char *retv, int inlen, bool v) |
813 | { | |
814 | int len; | |
815 | int fulllen = 0; | |
816 | ||
817 | if (!retv) | |
818 | inlen = 0; | |
819 | else | |
820 | memset(retv, 0, inlen); | |
821 | ||
822 | strprint(retv, inlen, "%d", v); | |
823 | ||
824 | return fulllen; | |
825 | } | |
826 | ||
953fe44f CB |
827 | int lxc_get_conf_int(struct lxc_conf *c, char *retv, int inlen, int v) |
828 | { | |
1396b610 DJ |
829 | int len; |
830 | int fulllen = 0; | |
831 | ||
953fe44f CB |
832 | if (!retv) |
833 | inlen = 0; | |
834 | else | |
835 | memset(retv, 0, inlen); | |
836 | ||
1396b610 DJ |
837 | strprint(retv, inlen, "%d", v); |
838 | ||
839 | return fulllen; | |
953fe44f | 840 | } |
f7662514 | 841 | |
885766f5 CB |
842 | int lxc_get_conf_size_t(struct lxc_conf *c, char *retv, int inlen, size_t v) |
843 | { | |
1396b610 DJ |
844 | int len; |
845 | int fulllen = 0; | |
846 | ||
885766f5 CB |
847 | if (!retv) |
848 | inlen = 0; | |
849 | else | |
850 | memset(retv, 0, inlen); | |
851 | ||
1396b610 DJ |
852 | strprint(retv, inlen, "%zu", v); |
853 | ||
854 | return fulllen; | |
885766f5 CB |
855 | } |
856 | ||
2ea479c9 CB |
857 | int lxc_get_conf_uint64(struct lxc_conf *c, char *retv, int inlen, uint64_t v) |
858 | { | |
1396b610 DJ |
859 | int len; |
860 | int fulllen = 0; | |
861 | ||
2ea479c9 CB |
862 | if (!retv) |
863 | inlen = 0; | |
864 | else | |
865 | memset(retv, 0, inlen); | |
866 | ||
1396b610 DJ |
867 | strprint(retv, inlen, "%"PRIu64, v); |
868 | ||
869 | return fulllen; | |
2ea479c9 CB |
870 | } |
871 | ||
28d9e29e CB |
872 | static int lxc_container_name_to_pid(const char *lxcname_or_pid, |
873 | const char *lxcpath) | |
874 | { | |
875 | int ret; | |
876 | signed long int pid; | |
877 | char *err = NULL; | |
878 | ||
879 | pid = strtol(lxcname_or_pid, &err, 10); | |
880 | if (*err != '\0' || pid < 1) { | |
881 | struct lxc_container *c; | |
882 | ||
883 | c = lxc_container_new(lxcname_or_pid, lxcpath); | |
884 | if (!c) { | |
885 | ERROR("\"%s\" is not a valid pid nor a container name", | |
886 | lxcname_or_pid); | |
887 | return -1; | |
888 | } | |
889 | ||
890 | if (!c->may_control(c)) { | |
891 | ERROR("Insufficient privileges to control container " | |
892 | "\"%s\"", c->name); | |
893 | lxc_container_put(c); | |
894 | return -1; | |
895 | } | |
896 | ||
897 | pid = c->init_pid(c); | |
898 | if (pid < 1) { | |
899 | ERROR("Container \"%s\" is not running", c->name); | |
900 | lxc_container_put(c); | |
901 | return -1; | |
902 | } | |
903 | ||
904 | lxc_container_put(c); | |
905 | } | |
906 | ||
907 | ret = kill(pid, 0); | |
908 | if (ret < 0) { | |
6d1400b5 | 909 | SYSERROR("Failed to send signal to pid %d", (int)pid); |
29c98ddd | 910 | return -1; |
28d9e29e CB |
911 | } |
912 | ||
913 | return pid; | |
914 | } | |
915 | ||
39e6fd36 | 916 | int lxc_inherit_namespace(const char *nsfd_path, const char *lxcpath, |
28d9e29e CB |
917 | const char *namespace) |
918 | { | |
919 | int fd, pid; | |
920 | char *dup, *lastslash; | |
921 | ||
39e6fd36 SH |
922 | if (nsfd_path[0] == '/') { |
923 | return open(nsfd_path, O_RDONLY | O_CLOEXEC); | |
924 | } | |
925 | ||
926 | lastslash = strrchr(nsfd_path, '/'); | |
28d9e29e | 927 | if (lastslash) { |
39e6fd36 | 928 | dup = strdup(nsfd_path); |
28d9e29e | 929 | if (!dup) |
29c98ddd | 930 | return -1; |
28d9e29e | 931 | |
39e6fd36 | 932 | dup[lastslash - nsfd_path] = '\0'; |
71649566 | 933 | pid = lxc_container_name_to_pid(lastslash + 1, dup); |
28d9e29e CB |
934 | free(dup); |
935 | } else { | |
39e6fd36 | 936 | pid = lxc_container_name_to_pid(nsfd_path, lxcpath); |
28d9e29e CB |
937 | } |
938 | ||
939 | if (pid < 0) | |
29c98ddd | 940 | return -1; |
28d9e29e CB |
941 | |
942 | fd = lxc_preserve_ns(pid, namespace); | |
943 | if (fd < 0) | |
29c98ddd | 944 | return -1; |
28d9e29e CB |
945 | |
946 | return fd; | |
947 | } | |
f6e32eb0 CB |
948 | |
949 | struct signame { | |
950 | int num; | |
951 | const char *name; | |
952 | }; | |
953 | ||
954 | static const struct signame signames[] = { | |
955 | { SIGHUP, "HUP" }, | |
956 | { SIGINT, "INT" }, | |
957 | { SIGQUIT, "QUIT" }, | |
958 | { SIGILL, "ILL" }, | |
959 | { SIGABRT, "ABRT" }, | |
960 | { SIGFPE, "FPE" }, | |
961 | { SIGKILL, "KILL" }, | |
962 | { SIGSEGV, "SEGV" }, | |
963 | { SIGPIPE, "PIPE" }, | |
964 | { SIGALRM, "ALRM" }, | |
965 | { SIGTERM, "TERM" }, | |
966 | { SIGUSR1, "USR1" }, | |
967 | { SIGUSR2, "USR2" }, | |
968 | { SIGCHLD, "CHLD" }, | |
969 | { SIGCONT, "CONT" }, | |
970 | { SIGSTOP, "STOP" }, | |
971 | { SIGTSTP, "TSTP" }, | |
972 | { SIGTTIN, "TTIN" }, | |
973 | { SIGTTOU, "TTOU" }, | |
974 | #ifdef SIGTRAP | |
975 | { SIGTRAP, "TRAP" }, | |
976 | #endif | |
977 | #ifdef SIGIOT | |
978 | { SIGIOT, "IOT" }, | |
979 | #endif | |
980 | #ifdef SIGEMT | |
981 | { SIGEMT, "EMT" }, | |
982 | #endif | |
983 | #ifdef SIGBUS | |
984 | { SIGBUS, "BUS" }, | |
985 | #endif | |
986 | #ifdef SIGSTKFLT | |
987 | { SIGSTKFLT, "STKFLT" }, | |
988 | #endif | |
989 | #ifdef SIGCLD | |
990 | { SIGCLD, "CLD" }, | |
991 | #endif | |
992 | #ifdef SIGURG | |
993 | { SIGURG, "URG" }, | |
994 | #endif | |
995 | #ifdef SIGXCPU | |
996 | { SIGXCPU, "XCPU" }, | |
997 | #endif | |
998 | #ifdef SIGXFSZ | |
999 | { SIGXFSZ, "XFSZ" }, | |
1000 | #endif | |
1001 | #ifdef SIGVTALRM | |
1002 | { SIGVTALRM, "VTALRM" }, | |
1003 | #endif | |
1004 | #ifdef SIGPROF | |
1005 | { SIGPROF, "PROF" }, | |
1006 | #endif | |
1007 | #ifdef SIGWINCH | |
1008 | { SIGWINCH, "WINCH" }, | |
1009 | #endif | |
1010 | #ifdef SIGIO | |
1011 | { SIGIO, "IO" }, | |
1012 | #endif | |
1013 | #ifdef SIGPOLL | |
1014 | { SIGPOLL, "POLL" }, | |
1015 | #endif | |
1016 | #ifdef SIGINFO | |
1017 | { SIGINFO, "INFO" }, | |
1018 | #endif | |
1019 | #ifdef SIGLOST | |
1020 | { SIGLOST, "LOST" }, | |
1021 | #endif | |
1022 | #ifdef SIGPWR | |
1023 | { SIGPWR, "PWR" }, | |
1024 | #endif | |
1025 | #ifdef SIGUNUSED | |
1026 | { SIGUNUSED, "UNUSED" }, | |
1027 | #endif | |
1028 | #ifdef SIGSYS | |
1029 | { SIGSYS, "SYS" }, | |
1030 | #endif | |
1031 | }; | |
1032 | ||
1033 | static int sig_num(const char *sig) | |
1034 | { | |
1035 | unsigned int signum; | |
1036 | ||
1037 | if (lxc_safe_uint(sig, &signum) < 0) | |
1038 | return -1; | |
1039 | ||
1040 | return signum; | |
1041 | } | |
1042 | ||
1043 | static int rt_sig_num(const char *signame) | |
1044 | { | |
1045 | int rtmax = 0, sig_n = 0; | |
1046 | ||
29c98ddd | 1047 | if (strncasecmp(signame, "max-", 4) == 0) |
f6e32eb0 | 1048 | rtmax = 1; |
f6e32eb0 CB |
1049 | |
1050 | signame += 4; | |
1051 | if (!isdigit(*signame)) | |
1052 | return -1; | |
1053 | ||
1054 | sig_n = sig_num(signame); | |
1055 | sig_n = rtmax ? SIGRTMAX - sig_n : SIGRTMIN + sig_n; | |
1056 | if (sig_n > SIGRTMAX || sig_n < SIGRTMIN) | |
1057 | return -1; | |
1058 | ||
1059 | return sig_n; | |
1060 | } | |
1061 | ||
1062 | int sig_parse(const char *signame) | |
1063 | { | |
1064 | size_t n; | |
1065 | ||
1066 | if (isdigit(*signame)) { | |
1067 | return sig_num(signame); | |
1068 | } else if (strncasecmp(signame, "sig", 3) == 0) { | |
1069 | signame += 3; | |
1070 | if (strncasecmp(signame, "rt", 2) == 0) | |
1071 | return rt_sig_num(signame + 2); | |
29c98ddd | 1072 | |
1073 | for (n = 0; n < sizeof(signames) / sizeof((signames)[0]); n++) | |
f6e32eb0 CB |
1074 | if (strcasecmp(signames[n].name, signame) == 0) |
1075 | return signames[n].num; | |
f6e32eb0 CB |
1076 | } |
1077 | ||
1078 | return -1; | |
1079 | } |