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