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