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