]>
git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/confile_utils.c
3 * Copyright © 2017 Christian Brauner <christian.brauner@ubuntu.com>.
4 * Copyright © 2017 Canonical Ltd.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2, as
8 * published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <arpa/inet.h>
30 #include "confile_utils.h"
38 lxc_log_define(lxc_confile_utils
, lxc
);
40 int parse_idmaps(const char *idmap
, char *type
, unsigned long *nsid
,
41 unsigned long *hostid
, unsigned long *range
)
44 unsigned long tmp_hostid
, tmp_nsid
, tmp_range
;
49 /* Duplicate string. */
54 /* A prototypical idmap entry would be: "u 1000 1000000 65536" */
59 slide
+= strspn(slide
, " \t\r");
60 if (slide
!= window
&& *slide
== '\0')
64 if (*slide
!= 'u' && *slide
!= 'g')
69 /* move beyond type */
73 /* Validate that only whitespace follows. */
74 slide
+= strspn(slide
, " \t\r");
75 /* There must be whitespace. */
79 /* Mark beginning of nsuid. */
81 /* Validate that non-whitespace follows. */
82 slide
+= strcspn(slide
, " \t\r");
83 /* There must be non-whitespace. */
84 if (slide
== window
|| *slide
== '\0')
86 /* Mark end of nsuid. */
90 if (lxc_safe_ulong(window
, &tmp_nsid
) < 0)
95 /* Validate that only whitespace follows. */
96 slide
+= strspn(slide
, " \t\r");
97 /* If there was only one whitespace then we whiped it with our \0 above.
98 * So only ensure that we're not at the end of the string.
103 /* Mark beginning of hostid. */
105 /* Validate that non-whitespace follows. */
106 slide
+= strcspn(slide
, " \t\r");
107 /* There must be non-whitespace. */
108 if (slide
== window
|| *slide
== '\0')
110 /* Mark end of nsuid. */
114 if (lxc_safe_ulong(window
, &tmp_hostid
) < 0)
117 /* Move beyond \0. */
119 /* Validate that only whitespace follows. */
120 slide
+= strspn(slide
, " \t\r");
121 /* If there was only one whitespace then we whiped it with our \0 above.
122 * So only ensure that we're not at the end of the string.
127 /* Mark beginning of range. */
129 /* Validate that non-whitespace follows. */
130 slide
+= strcspn(slide
, " \t\r");
131 /* There must be non-whitespace. */
135 /* The range is the last valid entry we expect. So make sure that there
136 * is not trailing garbage and if there is, error out.
138 if (*(slide
+ strspn(slide
, " \t\r\n")) != '\0')
140 /* Mark end of range. */
144 if (lxc_safe_ulong(window
, &tmp_range
) < 0)
149 *hostid
= tmp_hostid
;
152 /* Yay, we survived. */
161 bool lxc_config_value_empty(const char *value
)
163 if (value
&& strlen(value
) > 0)
169 struct lxc_netdev
*lxc_network_add(struct lxc_list
*networks
, int idx
, bool tail
)
171 struct lxc_list
*newlist
;
172 struct lxc_netdev
*netdev
= NULL
;
174 /* network does not exist */
175 netdev
= malloc(sizeof(*netdev
));
179 memset(netdev
, 0, sizeof(*netdev
));
180 lxc_list_init(&netdev
->ipv4
);
181 lxc_list_init(&netdev
->ipv6
);
183 /* give network a unique index */
186 /* prepare new list */
187 newlist
= malloc(sizeof(*newlist
));
193 lxc_list_init(newlist
);
194 newlist
->elem
= netdev
;
197 lxc_list_add_tail(networks
, newlist
);
199 lxc_list_add(networks
, newlist
);
203 /* Takes care of finding the correct netdev struct in the networks list or
204 * allocates a new one if it couldn't be found.
206 struct lxc_netdev
*lxc_get_netdev_by_idx(struct lxc_conf
*conf
,
207 unsigned int idx
, bool allocate
)
209 struct lxc_netdev
*netdev
= NULL
;
210 struct lxc_list
*networks
= &conf
->network
;
211 struct lxc_list
*insert
= networks
;
214 if (!lxc_list_empty(networks
)) {
215 lxc_list_for_each(insert
, networks
) {
216 netdev
= insert
->elem
;
217 if (netdev
->idx
== idx
)
219 else if (netdev
->idx
> idx
)
227 return lxc_network_add(insert
, idx
, true);
230 void lxc_log_configured_netdevs(const struct lxc_conf
*conf
)
232 struct lxc_netdev
*netdev
;
233 struct lxc_list
*it
= (struct lxc_list
*)&conf
->network
;;
235 if ((conf
->loglevel
!= LXC_LOG_LEVEL_TRACE
) &&
236 (lxc_log_get_level() != LXC_LOG_LEVEL_TRACE
))
239 if (lxc_list_empty(it
)) {
240 TRACE("container has no networks configured");
244 lxc_list_for_each(it
, &conf
->network
) {
245 struct lxc_list
*cur
, *next
;
246 struct lxc_inetdev
*inet4dev
;
247 struct lxc_inet6dev
*inet6dev
;
248 char bufinet4
[INET_ADDRSTRLEN
], bufinet6
[INET6_ADDRSTRLEN
];
252 TRACE("index: %zd", netdev
->idx
);
253 TRACE("ifindex: %d", netdev
->ifindex
);
254 switch (netdev
->type
) {
257 if (netdev
->priv
.veth_attr
.pair
[0] != '\0')
258 TRACE("veth pair: %s",
259 netdev
->priv
.veth_attr
.pair
);
260 if (netdev
->priv
.veth_attr
.veth1
[0] != '\0')
262 netdev
->priv
.veth_attr
.veth1
);
263 if (netdev
->priv
.veth_attr
.ifindex
> 0)
264 TRACE("host side ifindex for veth device: %d",
265 netdev
->priv
.veth_attr
.ifindex
);
267 case LXC_NET_MACVLAN
:
268 TRACE("type: macvlan");
269 if (netdev
->priv
.macvlan_attr
.mode
> 0) {
271 macvlan_mode
= lxc_macvlan_flag_to_mode(
272 netdev
->priv
.macvlan_attr
.mode
);
273 TRACE("macvlan mode: %s",
274 macvlan_mode
? macvlan_mode
280 TRACE("vlan id: %d", netdev
->priv
.vlan_attr
.vid
);
284 if (netdev
->priv
.phys_attr
.ifindex
> 0) {
285 TRACE("host side ifindex for phys device: %d",
286 netdev
->priv
.phys_attr
.ifindex
);
290 TRACE("type: empty");
296 ERROR("invalid network type %d", netdev
->type
);
300 if (netdev
->type
!= LXC_NET_EMPTY
) {
302 netdev
->flags
== IFF_UP
? "up" : "none");
303 if (netdev
->link
[0] != '\0')
304 TRACE("link: %s", netdev
->link
);
305 if (netdev
->name
[0] != '\0')
306 TRACE("name: %s", netdev
->name
);
308 TRACE("hwaddr: %s", netdev
->hwaddr
);
310 TRACE("mtu: %s", netdev
->mtu
);
311 if (netdev
->upscript
)
312 TRACE("upscript: %s", netdev
->upscript
);
313 if (netdev
->downscript
)
314 TRACE("downscript: %s", netdev
->downscript
);
316 TRACE("ipv4 gateway auto: %s",
317 netdev
->ipv4_gateway_auto
? "true" : "false");
319 if (netdev
->ipv4_gateway
) {
320 inet_ntop(AF_INET
, netdev
->ipv4_gateway
,
321 bufinet4
, sizeof(bufinet4
));
322 TRACE("ipv4 gateway: %s", bufinet4
);
325 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
326 inet4dev
= cur
->elem
;
327 inet_ntop(AF_INET
, &inet4dev
->addr
, bufinet4
,
329 TRACE("ipv4 addr: %s", bufinet4
);
332 TRACE("ipv6 gateway auto: %s",
333 netdev
->ipv6_gateway_auto
? "true" : "false");
334 if (netdev
->ipv6_gateway
) {
335 inet_ntop(AF_INET6
, netdev
->ipv6_gateway
,
336 bufinet6
, sizeof(bufinet6
));
337 TRACE("ipv6 gateway: %s", bufinet6
);
339 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
340 inet6dev
= cur
->elem
;
341 inet_ntop(AF_INET6
, &inet6dev
->addr
, bufinet6
,
343 TRACE("ipv6 addr: %s", bufinet6
);
349 static void lxc_free_netdev(struct lxc_netdev
*netdev
)
351 struct lxc_list
*cur
, *next
;
353 free(netdev
->upscript
);
354 free(netdev
->downscript
);
355 free(netdev
->hwaddr
);
358 free(netdev
->ipv4_gateway
);
359 lxc_list_for_each_safe(cur
, &netdev
->ipv4
, next
) {
365 free(netdev
->ipv6_gateway
);
366 lxc_list_for_each_safe(cur
, &netdev
->ipv6
, next
) {
375 bool lxc_remove_nic_by_idx(struct lxc_conf
*conf
, unsigned int idx
)
377 struct lxc_list
*cur
, *next
;
378 struct lxc_netdev
*netdev
;
381 lxc_list_for_each_safe(cur
, &conf
->network
, next
) {
383 if (netdev
->idx
!= idx
)
394 lxc_free_netdev(netdev
);
400 void lxc_free_networks(struct lxc_list
*networks
)
402 struct lxc_list
*cur
, *next
;
403 struct lxc_netdev
*netdev
;
405 lxc_list_for_each_safe(cur
, networks
, next
) {
407 lxc_free_netdev(netdev
);
411 /* prevent segfaults */
412 lxc_list_init(networks
);
415 static struct macvlan_mode
{
419 { "private", MACVLAN_MODE_PRIVATE
},
420 { "vepa", MACVLAN_MODE_VEPA
},
421 { "bridge", MACVLAN_MODE_BRIDGE
},
422 { "passthru", MACVLAN_MODE_PASSTHRU
},
425 int lxc_macvlan_mode_to_flag(int *mode
, const char *value
)
429 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
430 if (strcmp(macvlan_mode
[i
].name
, value
))
433 *mode
= macvlan_mode
[i
].mode
;
440 char *lxc_macvlan_flag_to_mode(int mode
)
444 for (i
= 0; i
< sizeof(macvlan_mode
) / sizeof(macvlan_mode
[0]); i
++) {
445 if (macvlan_mode
[i
].mode
== mode
)
448 return macvlan_mode
[i
].name
;
454 int set_config_string_item(char **conf_item
, const char *value
)
458 if (lxc_config_value_empty(value
)) {
464 new_value
= strdup(value
);
466 SYSERROR("failed to duplicate string \"%s\"", value
);
471 *conf_item
= new_value
;
475 int set_config_string_item_max(char **conf_item
, const char *value
, size_t max
)
477 if (strlen(value
) >= max
) {
478 ERROR("%s is too long (>= %lu)", value
, (unsigned long)max
);
482 return set_config_string_item(conf_item
, value
);
485 int set_config_path_item(char **conf_item
, const char *value
)
487 return set_config_string_item_max(conf_item
, value
, PATH_MAX
);
490 int config_ip_prefix(struct in_addr
*addr
)
492 if (IN_CLASSA(addr
->s_addr
))
493 return 32 - IN_CLASSA_NSHIFT
;
494 if (IN_CLASSB(addr
->s_addr
))
495 return 32 - IN_CLASSB_NSHIFT
;
496 if (IN_CLASSC(addr
->s_addr
))
497 return 32 - IN_CLASSC_NSHIFT
;
502 int network_ifname(char *valuep
, const char *value
)
504 if (strlen(value
) >= IFNAMSIZ
) {
505 ERROR("Network devie name \"%s\" is too long (>= %zu)", value
,
509 strcpy(valuep
, value
);
513 int rand_complete_hwaddr(char *hwaddr
)
515 const char hex
[] = "0123456789abcdef";
523 seed
= randseed(false);
525 while (*curs
!= '\0' && *curs
!= '\n') {
526 if (*curs
== 'x' || *curs
== 'X') {
527 if (curs
- hwaddr
== 1) {
528 /* ensure address is unicast */
530 *curs
= hex
[rand_r(&seed
) & 0x0E];
532 *curs
= hex
[rand_r(&seed
) & 0x0F];
534 *curs
= hex
[rand() & 0x0E];
536 *curs
= hex
[rand() & 0x0F];
545 bool lxc_config_net_hwaddr(const char *line
)
549 if (strncmp(line
, "lxc.net", 7) != 0)
551 if (strncmp(line
, "lxc.network.hwaddr", 18) == 0)
554 /* We have to dup the line, if line is something like
555 * "lxc.net.[i].xxx = xxxxx ", we need to remove
556 * '[i]' and compare its key with 'lxc.net.hwaddr'*/
559 SYSERROR("failed to allocate memory");
562 if (*(copy
+ 8) >= '0' && *(copy
+ 8) <= '9') {
563 p
= strchr(copy
+ 8, '.');
568 /* strlen("hwaddr") = 6 */
569 strncpy(copy
+ 8, p
+ 1, 6);
572 if (strncmp(copy
, "lxc.net.hwaddr", 14) == 0) {
578 /* We have to dup the line second time, if line is something like
579 * "lxc.network.[i].xxx = xxxxx ", we need to remove
580 * '[i]' and compare its key with 'lxc.network.hwaddr'*/
583 SYSERROR("failed to allocate memory");
586 if (*(copy
+ 12) >= '0' && *(copy
+ 12) <= '9') {
587 p
= strchr(copy
+ 12, '.');
592 /* strlen("hwaddr") = 6 */
593 strncpy(copy
+ 12, p
+ 1, 6);
596 if (strncmp(copy
, "lxc.network.hwaddr", 18) == 0) {
606 * If we find a lxc.net.[i].hwaddr or lxc.network.hwaddr in the original config
607 * file, we expand it in the unexpanded_config, so that after a save_config we
608 * store the hwaddr for re-use.
609 * This is only called when reading the config file, not when executing a
611 * 'x' and 'X' are substituted in-place.
613 void update_hwaddr(const char *line
)
617 line
+= lxc_char_left_gc(line
, strlen(line
));
621 if (!lxc_config_net_hwaddr(line
))
624 /* Let config_net_hwaddr raise the error. */
625 p
= strchr(line
, '=');
636 rand_complete_hwaddr(p
);
639 bool new_hwaddr(char *hwaddr
)
643 (void)randseed(true);
645 ret
= snprintf(hwaddr
, 18, "00:16:3e:%02x:%02x:%02x", rand() % 255,
646 rand() % 255, rand() % 255);
647 if (ret
< 0 || ret
>= 18) {
648 SYSERROR("Failed to call snprintf().");
655 int lxc_get_conf_str(char *retv
, int inlen
, const char *value
)
659 if (retv
&& inlen
>= strlen(value
) + 1)
660 strncpy(retv
, value
, strlen(value
) + 1);
662 return strlen(value
);
665 int lxc_get_conf_int(struct lxc_conf
*c
, char *retv
, int inlen
, int v
)
670 memset(retv
, 0, inlen
);
672 return snprintf(retv
, inlen
, "%d", v
);
675 bool parse_limit_value(const char **value
, unsigned long *res
)
679 if (strncmp(*value
, "unlimited", sizeof("unlimited") - 1) == 0) {
680 *res
= RLIM_INFINITY
;
681 *value
+= sizeof("unlimited") - 1;
686 *res
= strtoul(*value
, &endptr
, 10);
687 if (errno
|| !endptr
)