-
Notifications
You must be signed in to change notification settings - Fork 620
/
Copy pathnet.c
3929 lines (3213 loc) · 92.1 KB
/
net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/netfilter/nfnetlink.h>
#include <linux/netfilter/nfnetlink_conntrack.h>
#include <linux/netfilter/nf_conntrack_tcp.h>
#include <string.h>
#include <net/if_arp.h>
#include <sys/wait.h>
#include <sched.h>
#include <sys/mount.h>
#include <sys/types.h>
#include <net/if.h>
#include <linux/sockios.h>
#include <libnl3/netlink/attr.h>
#include <libnl3/netlink/msg.h>
#include <libnl3/netlink/netlink.h>
#if defined(CONFIG_HAS_NFTABLES_LIB_API_0) || defined(CONFIG_HAS_NFTABLES_LIB_API_1)
#include <nftables/libnftables.h>
#endif
#ifdef CONFIG_HAS_SELINUX
#include <selinux/selinux.h>
#endif
#include "../soccr/soccr.h"
#include "imgset.h"
#include "namespaces.h"
#include "net.h"
#include "libnetlink.h"
#include "cr_options.h"
#include "sk-inet.h"
#include "tun.h"
#include "util-pie.h"
#include "plugin.h"
#include "action-scripts.h"
#include "sockets.h"
#include "pstree.h"
#include "string.h"
#include "sysctl.h"
#include "kerndat.h"
#include "util.h"
#include "external.h"
#include "fdstore.h"
#include "netfilter.h"
#include "protobuf.h"
#include "images/netdev.pb-c.h"
#include "images/inventory.pb-c.h"
#undef LOG_PREFIX
#define LOG_PREFIX "net: "
#ifndef IFLA_NEW_IFINDEX
#define IFLA_NEW_IFINDEX 49
#endif
#ifndef IFLA_LINK_NETNSID
#define IFLA_LINK_NETNSID 37
#undef IFLA_MAX
#define IFLA_MAX IFLA_LINK_NETNSID
#endif
#ifndef RTM_NEWNSID
#define RTM_NEWNSID 88
#endif
#ifndef IFLA_MACVLAN_FLAGS
#define IFLA_MACVLAN_FLAGS 2
#endif
enum {
IFLA_IPTUN_UNSPEC,
IFLA_IPTUN_LINK,
IFLA_IPTUN_LOCAL,
IFLA_IPTUN_REMOTE,
IFLA_IPTUN_TTL,
IFLA_IPTUN_TOS,
IFLA_IPTUN_ENCAP_LIMIT,
IFLA_IPTUN_FLOWINFO,
IFLA_IPTUN_FLAGS,
IFLA_IPTUN_PROTO,
IFLA_IPTUN_PMTUDISC,
IFLA_IPTUN_6RD_PREFIX,
IFLA_IPTUN_6RD_RELAY_PREFIX,
IFLA_IPTUN_6RD_PREFIXLEN,
IFLA_IPTUN_6RD_RELAY_PREFIXLEN,
IFLA_IPTUN_ENCAP_TYPE,
IFLA_IPTUN_ENCAP_FLAGS,
IFLA_IPTUN_ENCAP_SPORT,
IFLA_IPTUN_ENCAP_DPORT,
__IFLA_IPTUN_MAX,
};
#define IFLA_IPTUN_MAX (__IFLA_IPTUN_MAX - 1)
static int ns_sysfs_fd = -1;
int read_ns_sys_file(char *path, char *buf, int len)
{
int fd, rlen;
BUG_ON(ns_sysfs_fd == -1);
fd = openat(ns_sysfs_fd, path, O_RDONLY, 0);
if (fd < 0) {
pr_perror("Can't open ns' %s", path);
return -1;
}
rlen = read(fd, buf, len);
if (rlen == -1)
pr_perror("Can't read ns' %s", path);
close(fd);
if (rlen == len) {
buf[0] = '\0';
pr_err("Too small buffer to read ns sys file %s\n", path);
return -1;
}
if (rlen >= 0)
buf[rlen] = '\0';
return rlen;
}
static bool sysctl_entries_equal(SysctlEntry *a, SysctlEntry *b)
{
if (a->type != b->type)
return false;
switch (a->type) {
case SYSCTL_TYPE__CTL_32:
return a->has_iarg && b->has_iarg && a->iarg == b->iarg;
case SYSCTL_TYPE__CTL_STR:
return a->sarg && b->sarg && !strcmp(a->sarg, b->sarg);
default:;
}
return false;
}
static char *devconfs4[] = {
"accept_local",
"accept_redirects",
"accept_source_route",
"arp_accept",
"arp_announce",
"arp_filter",
"arp_ignore",
"arp_notify",
"bootp_relay",
"disable_policy",
"disable_xfrm",
"force_igmp_version",
"forwarding",
"igmpv2_unsolicited_report_interval",
"igmpv3_unsolicited_report_interval",
"log_martians",
"medium_id",
"promote_secondaries",
"proxy_arp",
"proxy_arp_pvlan",
"route_localnet",
"rp_filter",
"secure_redirects",
"send_redirects",
"shared_media",
"src_valid_mark",
"tag",
"ignore_routes_with_linkdown",
"drop_gratuitous_arp",
"drop_unicast_in_l2_multicast",
};
char *devconfs6[] = {
"accept_dad",
"accept_ra",
"accept_ra_defrtr",
"accept_ra_from_local",
"accept_ra_min_hop_limit",
"accept_ra_mtu",
"accept_ra_pinfo",
"accept_ra_rt_info_max_plen",
"accept_ra_rtr_pref",
"accept_redirects",
"accept_source_route",
"autoconf",
"dad_transmits",
"disable_ipv6",
"drop_unicast_in_l2_multicast",
"drop_unsolicited_na",
"force_mld_version",
"force_tllao",
"forwarding",
"hop_limit",
"ignore_routes_with_linkdown",
"keep_addr_on_down",
"max_addresses",
"max_desync_factor",
"mldv1_unsolicited_report_interval",
"mldv2_unsolicited_report_interval",
"mtu",
"ndisc_notify",
"optimistic_dad",
"proxy_ndp",
"regen_max_retry",
"router_probe_interval",
"router_solicitation_delay",
"router_solicitation_interval",
"router_solicitations",
"stable_secret",
"suppress_frag_ndisc",
"temp_prefered_lft",
"temp_valid_lft",
"use_oif_addrs_only",
"use_optimistic",
"use_tempaddr",
};
#define CONF_OPT_PATH "net/%s/conf/%s/%s"
#define MAX_CONF_OPT_PATH IFNAMSIZ + 60
#define MAX_STR_CONF_LEN 200
static const char *unix_conf_entries[] = {
"max_dgram_qlen",
};
/*
* MAX_CONF_UNIX_PATH = (sizeof(CONF_UNIX_FMT) - strlen("%s"))
* + MAX_CONF_UNIX_OPT_PATH
*/
#define CONF_UNIX_BASE "net/unix"
#define CONF_UNIX_FMT CONF_UNIX_BASE "/%s"
#define MAX_CONF_UNIX_OPT_PATH 32
#define MAX_CONF_UNIX_PATH (sizeof(CONF_UNIX_FMT) + MAX_CONF_UNIX_OPT_PATH - 2)
static int net_conf_op(char *tgt, SysctlEntry **conf, int n, int op, char *proto, struct sysctl_req *req,
char (*path)[MAX_CONF_OPT_PATH], int size, char **devconfs, SysctlEntry **def_conf)
{
int i, ri, ar = -1;
int ret, flags = op == CTL_READ ? CTL_FLAGS_OPTIONAL : 0;
SysctlEntry **rconf;
if (n > size)
pr_warn("The image contains unknown sysctl-s\n");
if (opts.weak_sysctls)
flags = CTL_FLAGS_OPTIONAL;
rconf = xmalloc(sizeof(SysctlEntry *) * size);
if (!rconf)
return -1;
for (i = 0, ri = 0; i < size; i++) {
if (i >= n) {
pr_warn("Skip %s/%s\n", tgt, devconfs[i]);
continue;
}
/*
* If dev conf value is the same as default skip restoring it,
* mtu may be changed by disable_ipv6 so we can not skip
* it's restore
*/
if (def_conf && sysctl_entries_equal(conf[i], def_conf[i]) && strcmp(devconfs[i], "mtu")) {
pr_debug("Skip %s/%s, coincides with default\n", tgt, devconfs[i]);
continue;
}
/*
* Make "accept_redirects" go last on write(it should
* restore after forwarding to be correct)
*/
if (op == CTL_WRITE && !strcmp(devconfs[i], "accept_redirects")) {
ar = i;
continue;
}
snprintf(path[i], MAX_CONF_OPT_PATH, CONF_OPT_PATH, proto, tgt, devconfs[i]);
req[ri].name = path[i];
req[ri].flags = flags;
switch (conf[i]->type) {
case SYSCTL_TYPE__CTL_32:
req[ri].type = CTL_32;
/* skip non-existing sysctl */
if (op == CTL_WRITE && !conf[i]->has_iarg)
continue;
req[ri].arg = &conf[i]->iarg;
break;
case SYSCTL_TYPE__CTL_STR:
req[ri].type = CTL_STR(MAX_STR_CONF_LEN);
req[ri].flags |=
op == CTL_READ && !strcmp(devconfs[i], "stable_secret") ? CTL_FLAGS_READ_EIO_SKIP : 0;
/* skip non-existing sysctl */
if (op == CTL_WRITE && !conf[i]->sarg)
continue;
req[ri].arg = conf[i]->sarg;
break;
default:
continue;
}
rconf[ri] = conf[i];
ri++;
}
if (ar != -1 && conf[ar]->type == SYSCTL_TYPE__CTL_32 && conf[ar]->has_iarg) {
snprintf(path[ar], MAX_CONF_OPT_PATH, CONF_OPT_PATH, proto, tgt, devconfs[ar]);
req[ri].name = path[ar];
req[ri].type = CTL_32;
req[ri].arg = &conf[ar]->iarg;
req[ri].flags = flags;
rconf[ri] = conf[ar];
ri++;
}
ret = sysctl_op(req, ri, op, CLONE_NEWNET);
if (ret < 0) {
pr_err("Failed to %s %s/<confs>\n", (op == CTL_READ) ? "read" : "write", tgt);
goto err_free;
}
if (op == CTL_READ) {
/* (un)mark (non-)existing sysctls in image */
for (i = 0; i < ri; i++)
if (req[i].flags & CTL_FLAGS_HAS) {
if (rconf[i]->type == SYSCTL_TYPE__CTL_32)
rconf[i]->has_iarg = true;
} else {
if (rconf[i]->type == SYSCTL_TYPE__CTL_STR)
rconf[i]->sarg = NULL;
}
}
err_free:
xfree(rconf);
return ret;
}
static int ipv4_conf_op(char *tgt, SysctlEntry **conf, int n, int op, SysctlEntry **def_conf)
{
struct sysctl_req req[ARRAY_SIZE(devconfs4)];
char path[ARRAY_SIZE(devconfs4)][MAX_CONF_OPT_PATH];
return net_conf_op(tgt, conf, n, op, "ipv4", req, path, ARRAY_SIZE(devconfs4), devconfs4, def_conf);
}
static int ipv6_conf_op(char *tgt, SysctlEntry **conf, int n, int op, SysctlEntry **def_conf)
{
struct sysctl_req req[ARRAY_SIZE(devconfs6)];
char path[ARRAY_SIZE(devconfs6)][MAX_CONF_OPT_PATH];
return net_conf_op(tgt, conf, n, op, "ipv6", req, path, ARRAY_SIZE(devconfs6), devconfs6, def_conf);
}
static int unix_conf_op(SysctlEntry ***rconf, size_t *pn, int op)
{
int i, ret = -1, flags = 0;
char path[ARRAY_SIZE(unix_conf_entries)][MAX_CONF_UNIX_PATH] = {};
struct sysctl_req req[ARRAY_SIZE(unix_conf_entries)] = {};
SysctlEntry **conf = *rconf;
size_t n = *pn;
if (n != ARRAY_SIZE(unix_conf_entries)) {
pr_err("unix: Unexpected entries in config (%zu %zu)\n", n, ARRAY_SIZE(unix_conf_entries));
return -EINVAL;
}
if (opts.weak_sysctls || op == CTL_READ)
flags = CTL_FLAGS_OPTIONAL;
for (i = 0; i < n; i++) {
snprintf(path[i], MAX_CONF_UNIX_PATH, CONF_UNIX_FMT, unix_conf_entries[i]);
req[i].name = path[i];
req[i].flags = flags;
switch (conf[i]->type) {
case SYSCTL_TYPE__CTL_32:
req[i].type = CTL_32;
req[i].arg = &conf[i]->iarg;
break;
default:
pr_err("unix: Unknown config type %d\n", conf[i]->type);
return -1;
}
}
ret = sysctl_op(req, n, op, CLONE_NEWNET);
if (ret < 0) {
pr_err("unix: Failed to %s %s/<confs>\n", (op == CTL_READ) ? "read" : "write", CONF_UNIX_BASE);
return -1;
}
if (op == CTL_READ) {
bool has_entries = false;
for (i = 0; i < n; i++) {
if (req[i].flags & CTL_FLAGS_HAS) {
conf[i]->has_iarg = true;
if (!has_entries)
has_entries = true;
}
}
/*
* Zap the whole section of data.
* Unix conf is optional.
*/
if (!has_entries) {
*pn = 0;
*rconf = NULL;
}
}
return 0;
}
/*
* I case if some entry is missing in
* the kernel, simply write DEVCONFS_UNUSED
* into the image so we would skip it.
*/
#define DEVCONFS_UNUSED (-1u)
static int ipv4_conf_op_old(char *tgt, int *conf, int n, int op, int *def_conf)
{
int i, ri;
int ret, flags = op == CTL_READ ? CTL_FLAGS_OPTIONAL : 0;
struct sysctl_req req[ARRAY_SIZE(devconfs4)];
char path[ARRAY_SIZE(devconfs4)][MAX_CONF_OPT_PATH];
if (n > ARRAY_SIZE(devconfs4))
pr_warn("The image contains unknown sysctl-s\n");
for (i = 0, ri = 0; i < ARRAY_SIZE(devconfs4); i++) {
if (i >= n) {
pr_warn("Skip %s/%s\n", tgt, devconfs4[i]);
continue;
}
/*
* If dev conf value is the same as default skip restoring it
*/
if (def_conf && conf[i] == def_conf[i]) {
pr_debug("DEBUG Skip %s/%s, val =%d\n", tgt, devconfs4[i], conf[i]);
continue;
}
if (op == CTL_WRITE && conf[i] == DEVCONFS_UNUSED)
continue;
else if (op == CTL_READ)
conf[i] = DEVCONFS_UNUSED;
snprintf(path[i], MAX_CONF_OPT_PATH, CONF_OPT_PATH, "ipv4", tgt, devconfs4[i]);
req[ri].name = path[i];
req[ri].arg = &conf[i];
req[ri].type = CTL_32;
req[ri].flags = flags;
ri++;
}
ret = sysctl_op(req, ri, op, CLONE_NEWNET);
if (ret < 0) {
pr_err("Failed to %s %s/<confs>\n", (op == CTL_READ) ? "read" : "write", tgt);
return -1;
}
return 0;
}
int write_netdev_img(NetDeviceEntry *nde, struct cr_imgset *fds, struct nlattr **info)
{
return pb_write_one(img_from_set(fds, CR_FD_NETDEV), nde, PB_NETDEV);
}
static int lookup_net_by_netid(struct ns_id *ns, int net_id)
{
struct netns_id *p;
list_for_each_entry(p, &ns->net.ids, node)
if (p->netnsid_value == net_id)
return p->target_ns_id;
return -1;
}
static int dump_one_netdev(int type, struct ifinfomsg *ifi, struct nlattr **tb, struct ns_id *ns, struct cr_imgset *fds,
int (*dump)(NetDeviceEntry *, struct cr_imgset *, struct nlattr **info))
{
int ret = -1, i, peer_ifindex;
NetDeviceEntry netdev = NET_DEVICE_ENTRY__INIT;
SysctlEntry *confs4 = NULL;
int size4 = ARRAY_SIZE(devconfs4);
SysctlEntry *confs6 = NULL;
int size6 = ARRAY_SIZE(devconfs6);
char stable_secret[MAX_STR_CONF_LEN + 1] = {};
struct nlattr *info[IFLA_INFO_MAX + 1], **arg = NULL;
if (!tb[IFLA_IFNAME]) {
pr_err("No name for link %d\n", ifi->ifi_index);
return -1;
}
netdev.type = type;
netdev.ifindex = ifi->ifi_index;
netdev.mtu = *(int *)RTA_DATA(tb[IFLA_MTU]);
netdev.flags = ifi->ifi_flags;
netdev.name = RTA_DATA(tb[IFLA_IFNAME]);
if (kdat.has_nsid) {
s32 nsid = -1;
peer_ifindex = ifi->ifi_index;
if (tb[IFLA_LINK])
peer_ifindex = nla_get_u32(tb[IFLA_LINK]);
netdev.has_peer_ifindex = true;
netdev.peer_ifindex = peer_ifindex;
if (tb[IFLA_LINK_NETNSID])
nsid = nla_get_s32(tb[IFLA_LINK_NETNSID]);
pr_debug("The peer link is in the %d netns with the %u index\n", nsid, netdev.peer_ifindex);
if (nsid == -1)
nsid = ns->id;
else
nsid = lookup_net_by_netid(ns, nsid);
if (nsid < 0) {
pr_warn("The %s veth is in an external netns\n", netdev.name);
} else {
netdev.has_peer_nsid = true;
netdev.peer_nsid = nsid;
}
}
/*
* If kdat.has_nsid is false, a multiple network namespaces are not dumped,
* so if we are here, this means only one netns is dumped.
*/
if (tb[IFLA_ADDRESS] && (type != ND_TYPE__LOOPBACK)) {
netdev.has_address = true;
netdev.address.data = nla_data(tb[IFLA_ADDRESS]);
netdev.address.len = nla_len(tb[IFLA_ADDRESS]);
pr_info("Found ll addr (%02x:../%d) for %s\n", (int)netdev.address.data[0], (int)netdev.address.len,
netdev.name);
}
if (tb[IFLA_MASTER]) {
netdev.has_master = true;
netdev.master = nla_get_u32(tb[IFLA_MASTER]);
}
netdev.n_conf4 = size4;
netdev.conf4 = xmalloc(sizeof(SysctlEntry *) * size4);
if (!netdev.conf4)
goto err_free;
confs4 = xmalloc(sizeof(SysctlEntry) * size4);
if (!confs4)
goto err_free;
for (i = 0; i < size4; i++) {
sysctl_entry__init(&confs4[i]);
netdev.conf4[i] = &confs4[i];
netdev.conf4[i]->type = CTL_32;
}
netdev.n_conf6 = size6;
netdev.conf6 = xmalloc(sizeof(SysctlEntry *) * size6);
if (!netdev.conf6)
goto err_free;
confs6 = xmalloc(sizeof(SysctlEntry) * size6);
if (!confs6)
goto err_free;
for (i = 0; i < size6; i++) {
sysctl_entry__init(&confs6[i]);
netdev.conf6[i] = &confs6[i];
if (strcmp(devconfs6[i], "stable_secret")) {
netdev.conf6[i]->type = SYSCTL_TYPE__CTL_32;
} else {
netdev.conf6[i]->type = SYSCTL_TYPE__CTL_STR;
netdev.conf6[i]->sarg = stable_secret;
}
}
ret = ipv4_conf_op(netdev.name, netdev.conf4, size4, CTL_READ, NULL);
if (ret < 0)
goto err_free;
ret = ipv6_conf_op(netdev.name, netdev.conf6, size6, CTL_READ, NULL);
if (ret < 0)
goto err_free;
if (!dump)
dump = write_netdev_img;
if (tb[IFLA_LINKINFO]) {
ret = nla_parse_nested(info, IFLA_INFO_MAX, tb[IFLA_LINKINFO], NULL);
if (ret < 0) {
pr_err("failed to parse nested linkinfo\n");
return -1;
}
arg = info;
}
ret = dump(&netdev, fds, arg);
err_free:
xfree(netdev.conf4);
xfree(confs4);
xfree(netdev.conf6);
xfree(confs6);
return ret;
}
static char *link_kind(struct ifinfomsg *ifi, struct nlattr **tb)
{
struct nlattr *linkinfo[IFLA_INFO_MAX + 1];
if (!tb[IFLA_LINKINFO]) {
pr_err("No linkinfo for eth link %d\n", ifi->ifi_index);
return NULL;
}
nla_parse_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO], NULL);
if (!linkinfo[IFLA_INFO_KIND]) {
pr_err("No kind for eth link %d\n", ifi->ifi_index);
return NULL;
}
return nla_data(linkinfo[IFLA_INFO_KIND]);
}
static int dump_unknown_device(struct ifinfomsg *ifi, char *kind, struct nlattr **tb, struct ns_id *ns,
struct cr_imgset *fds)
{
int ret;
ret = run_plugins(DUMP_EXT_LINK, ifi->ifi_index, ifi->ifi_type, kind);
if (ret == 0)
return dump_one_netdev(ND_TYPE__EXTLINK, ifi, tb, ns, fds, NULL);
if (ret == -ENOTSUP)
pr_err("Unsupported link %d (type %d kind %s)\n", ifi->ifi_index, ifi->ifi_type, kind);
return -1;
}
static int dump_bridge(NetDeviceEntry *nde, struct cr_imgset *imgset, struct nlattr **info)
{
return write_netdev_img(nde, imgset, info);
}
static int dump_macvlan(NetDeviceEntry *nde, struct cr_imgset *imgset, struct nlattr **info)
{
MacvlanLinkEntry macvlan = MACVLAN_LINK_ENTRY__INIT;
int ret;
struct nlattr *data[IFLA_MACVLAN_FLAGS + 1];
if (!info || !info[IFLA_INFO_DATA]) {
pr_err("no data for macvlan\n");
return -1;
}
ret = nla_parse_nested(data, IFLA_MACVLAN_FLAGS, info[IFLA_INFO_DATA], NULL);
if (ret < 0) {
pr_err("failed to parse macvlan data\n");
return -1;
}
if (!data[IFLA_MACVLAN_MODE]) {
pr_err("macvlan mode required for %s\n", nde->name);
return -1;
}
macvlan.mode = *((u32 *)RTA_DATA(data[IFLA_MACVLAN_MODE]));
if (data[IFLA_MACVLAN_FLAGS])
macvlan.flags = *((u16 *)RTA_DATA(data[IFLA_MACVLAN_FLAGS]));
nde->macvlan = &macvlan;
return write_netdev_img(nde, imgset, info);
}
static int dump_one_ethernet(struct ifinfomsg *ifi, char *kind, struct nlattr **tb, struct ns_id *ns,
struct cr_imgset *fds)
{
if (!strcmp(kind, "veth"))
/*
* This is not correct. The peer of the veth device may
* be either outside or inside the netns we're working
* on, but there's currently no way of finding this out.
*
* Sigh... we have to assume, that the veth device is a
* connection to the outer world and just dump this end :(
*/
return dump_one_netdev(ND_TYPE__VETH, ifi, tb, ns, fds, NULL);
if (!strcmp(kind, "tun"))
return dump_one_netdev(ND_TYPE__TUN, ifi, tb, ns, fds, dump_tun_link);
if (!strcmp(kind, "bridge"))
return dump_one_netdev(ND_TYPE__BRIDGE, ifi, tb, ns, fds, dump_bridge);
if (!strcmp(kind, "gretap")) {
char *name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
if (!name) {
pr_err("gretap %d has no name\n", ifi->ifi_index);
return -1;
}
if (!strcmp(name, "gretap0")) {
pr_info("found %s, ignoring\n", name);
return 0;
}
pr_warn("GRE tap device %s not supported natively\n", name);
}
if (!strcmp(kind, "macvlan"))
return dump_one_netdev(ND_TYPE__MACVLAN, ifi, tb, ns, fds, dump_macvlan);
return dump_unknown_device(ifi, kind, tb, ns, fds);
}
static int dump_one_gendev(struct ifinfomsg *ifi, char *kind, struct nlattr **tb, struct ns_id *ns,
struct cr_imgset *fds)
{
if (!strcmp(kind, "tun"))
return dump_one_netdev(ND_TYPE__TUN, ifi, tb, ns, fds, dump_tun_link);
return dump_unknown_device(ifi, kind, tb, ns, fds);
}
static int dump_one_voiddev(struct ifinfomsg *ifi, char *kind, struct nlattr **tb, struct ns_id *ns,
struct cr_imgset *fds)
{
if (!strcmp(kind, "venet"))
return dump_one_netdev(ND_TYPE__VENET, ifi, tb, ns, fds, NULL);
return dump_unknown_device(ifi, kind, tb, ns, fds);
}
static int dump_one_gre(struct ifinfomsg *ifi, char *kind, struct nlattr **tb, struct ns_id *ns, struct cr_imgset *fds)
{
if (!strcmp(kind, "gre")) {
char *name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
if (!name) {
pr_err("gre device %d has no name\n", ifi->ifi_index);
return -1;
}
if (!strcmp(name, "gre0")) {
pr_info("found %s, ignoring\n", name);
return 0;
}
pr_warn("GRE tunnel device %s not supported natively\n", name);
}
return dump_unknown_device(ifi, kind, tb, ns, fds);
}
static int dump_sit(NetDeviceEntry *nde, struct cr_imgset *imgset, struct nlattr **info)
{
int ret;
struct nlattr *data[__IFLA_IPTUN_MAX];
SitEntry se = SIT_ENTRY__INIT;
/* There are for IP(v6) addresses kernel feeds to us */
uint32_t a_local, a_remote, rd_prefix[4], rl_prefix;
if (!info || !info[IFLA_INFO_DATA]) {
pr_err("no data for sit\n");
return -1;
}
pr_info("Some data for SIT provided\n");
ret = nla_parse_nested(data, IFLA_IPTUN_MAX, info[IFLA_INFO_DATA], NULL);
if (ret < 0) {
pr_err("failed to parse sit data\n");
return -1;
}
#define ENCODE_ENTRY(__type, __ifla, __proto) \
do { \
if (data[__ifla]) { \
se.__proto = *(__type *)nla_data(data[__ifla]); \
se.has_##__proto = true; \
} \
} while (0)
if (data[IFLA_IPTUN_LOCAL]) {
a_local = *(u32 *)nla_data(data[IFLA_IPTUN_LOCAL]);
if (a_local != 0) {
se.n_local = 1;
se.local = &a_local;
}
}
if (data[IFLA_IPTUN_REMOTE]) {
a_remote = *(u32 *)nla_data(data[IFLA_IPTUN_REMOTE]);
if (a_remote != 0) {
se.n_remote = 1;
se.remote = &a_remote;
}
}
ENCODE_ENTRY(u32, IFLA_IPTUN_LINK, link);
ENCODE_ENTRY(u8, IFLA_IPTUN_TTL, ttl);
ENCODE_ENTRY(u8, IFLA_IPTUN_TOS, tos);
ENCODE_ENTRY(u16, IFLA_IPTUN_FLAGS, flags);
ENCODE_ENTRY(u8, IFLA_IPTUN_PROTO, proto);
if (data[IFLA_IPTUN_PMTUDISC]) {
u8 v;
v = *(u8 *)nla_data(data[IFLA_IPTUN_PMTUDISC]);
if (v)
se.pmtudisc = se.has_pmtudisc = true;
}
ENCODE_ENTRY(u16, IFLA_IPTUN_ENCAP_TYPE, encap_type);
ENCODE_ENTRY(u16, IFLA_IPTUN_ENCAP_FLAGS, encap_flags);
ENCODE_ENTRY(u16, IFLA_IPTUN_ENCAP_SPORT, encap_sport);
ENCODE_ENTRY(u16, IFLA_IPTUN_ENCAP_DPORT, encap_dport);
if (data[IFLA_IPTUN_6RD_PREFIXLEN]) {
se.rd_prefixlen = *(u16 *)nla_data(data[IFLA_IPTUN_6RD_PREFIXLEN]);
if (!se.rd_prefixlen)
goto skip;
if (!data[IFLA_IPTUN_6RD_PREFIX]) {
pr_err("No 6rd prefix for sit device\n");
return -1;
}
se.has_rd_prefixlen = true;
memcpy(&rd_prefix, nla_data(data[IFLA_IPTUN_6RD_PREFIX]), sizeof(rd_prefix));
se.n_rd_prefix = 4;
se.rd_prefix = rd_prefix;
se.relay_prefixlen = *(u16 *)nla_data(data[IFLA_IPTUN_6RD_RELAY_PREFIXLEN]);
if (!se.relay_prefixlen)
goto skip;
if (!data[IFLA_IPTUN_6RD_RELAY_PREFIX]) {
pr_err("No 6rd relay prefix for sit device\n");
return -1;
}
se.has_relay_prefixlen = true;
memcpy(&rl_prefix, nla_data(data[IFLA_IPTUN_6RD_RELAY_PREFIX]), sizeof(rl_prefix));
se.n_relay_prefix = 1;
se.relay_prefix = &rl_prefix;
skip:;
}
#undef ENCODE_ENTRY
nde->sit = &se;
return write_netdev_img(nde, imgset, info);
}
static int dump_one_sit(struct ifinfomsg *ifi, char *kind, struct nlattr **tb, struct ns_id *ns, struct cr_imgset *fds)
{
char *name;
if (strcmp(kind, "sit")) {
pr_err("SIT device with %s kind\n", kind);
return -1;
}
name = (char *)RTA_DATA(tb[IFLA_IFNAME]);
if (!name) {
pr_err("sit device %d has no name\n", ifi->ifi_index);
return -1;
}
if (!strcmp(name, "sit0")) {
pr_info("found %s, ignoring\n", name);
return 0;
}
return dump_one_netdev(ND_TYPE__SIT, ifi, tb, ns, fds, dump_sit);
}
static int list_one_link(struct nlmsghdr *hdr, struct ns_id *ns, void *arg)
{
return 0;
}
static int dump_one_link(struct nlmsghdr *hdr, struct ns_id *ns, void *arg)
{
struct cr_imgset *fds = arg;
struct ifinfomsg *ifi;
int ret = 0, len = hdr->nlmsg_len - NLMSG_LENGTH(sizeof(*ifi));
struct nlattr *tb[IFLA_MAX + 1];
char *kind;
ifi = NLMSG_DATA(hdr);
if (len < 0) {
pr_err("No iflas for link %d\n", ifi->ifi_index);
return -1;
}
nlmsg_parse(hdr, sizeof(struct ifinfomsg), tb, IFLA_MAX, NULL);
pr_info("\tLD: Got link %d, type %d\n", ifi->ifi_index, ifi->ifi_type);
if (ifi->ifi_type == ARPHRD_LOOPBACK)
return dump_one_netdev(ND_TYPE__LOOPBACK, ifi, tb, ns, fds, NULL);
kind = link_kind(ifi, tb);
if (!kind)
goto unk;
switch (ifi->ifi_type) {
case ARPHRD_ETHER:
ret = dump_one_ethernet(ifi, kind, tb, ns, fds);
break;
case ARPHRD_NONE:
ret = dump_one_gendev(ifi, kind, tb, ns, fds);
break;
case ARPHRD_VOID:
ret = dump_one_voiddev(ifi, kind, tb, ns, fds);
break;
case ARPHRD_IPGRE:
ret = dump_one_gre(ifi, kind, tb, ns, fds);
break;
case ARPHRD_SIT:
ret = dump_one_sit(ifi, kind, tb, ns, fds);
break;
default:
unk:
ret = dump_unknown_device(ifi, kind, tb, ns, fds);
break;
}
return ret;
}
static int dump_one_nf(struct nlmsghdr *hdr, struct ns_id *ns, void *arg)
{
struct cr_img *img = arg;
if (lazy_image(img) && open_image_lazy(img))
return -1;
if (write_img_buf(img, hdr, hdr->nlmsg_len))
return -1;
return 0;
}
static int ct_restore_callback(struct nlmsghdr *nlh)
{
struct nfgenmsg *msg;
struct nlattr *tb[CTA_MAX + 1], *tbp[CTA_PROTOINFO_MAX + 1], *tb_tcp[CTA_PROTOINFO_TCP_MAX + 1];
int err;
msg = NLMSG_DATA(nlh);
if (msg->nfgen_family != AF_INET && msg->nfgen_family != AF_INET6)
return 0;
err = nlmsg_parse(nlh, sizeof(struct nfgenmsg), tb, CTA_MAX, NULL);
if (err < 0)
return -1;
if (!tb[CTA_PROTOINFO])
return 0;
err = nla_parse_nested(tbp, CTA_PROTOINFO_MAX, tb[CTA_PROTOINFO], NULL);
if (err < 0)
return -1;
if (!tbp[CTA_PROTOINFO_TCP])
return 0;
err = nla_parse_nested(tb_tcp, CTA_PROTOINFO_TCP_MAX, tbp[CTA_PROTOINFO_TCP], NULL);
if (err < 0)
return -1;
if (tb_tcp[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]) {
struct nf_ct_tcp_flags *flags;
flags = nla_data(tb_tcp[CTA_PROTOINFO_TCP_FLAGS_ORIGINAL]);
flags->flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
flags->mask |= IP_CT_TCP_FLAG_BE_LIBERAL;
}
if (tb_tcp[CTA_PROTOINFO_TCP_FLAGS_REPLY]) {
struct nf_ct_tcp_flags *flags;
flags = nla_data(tb_tcp[CTA_PROTOINFO_TCP_FLAGS_REPLY]);
flags->flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
flags->mask |= IP_CT_TCP_FLAG_BE_LIBERAL;
}