-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathnet.c
3006 lines (2476 loc) · 71.4 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
/*
*
* net.c Network related functions
*
* Copyright (c) 1996-2002 Hubert Mantel, SuSE Linux AG (mantel@suse.de)
*
*/
#define _GNU_SOURCE 1
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <fnmatch.h>
#include <dirent.h>
#include <sys/types.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/reboot.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <signal.h>
#include <net/if.h>
#include <net/route.h>
#include <netinet/in.h>
#include <netinet/ether.h>
#include <sys/wait.h>
#include <hd.h>
#include "global.h"
#include "dialog.h"
#include "window.h"
#include "util.h"
#include "net.h"
#include "file.h"
#include "module.h"
#include "url.h"
#include "auto2.h"
#if defined(__s390__) || defined(__s390x__)
// settings for config.hwp.layer2
// (layer2 attribute for network cards)
#define LAYER2_UNDEF 0
#define LAYER2_NO 1
#define LAYER2_YES 2
int net_activate_s390_devs_ex(hd_t* hd, char** device);
#endif
static int net_choose_device(void);
static int net_input_data(void);
static int net_input_vlanid(void);
static int wlan_auth_cb(dia_item_t di);
static dia_item_t di_wlan_auth_last = di_none;
static void parse_leaseinfo(char *file);
static void net_wicked_dhcp(void);
static void net_cifs_build_options(char **options, char *user, char *password, char *workgroup);
static int ifcfg_write(char *device, ifcfg_t *ifcfg, int flags);
static int _ifcfg_write(char *device, ifcfg_t *ifcfg);
static char *inet2str(inet_t *inet, int type);
static int net_get_ip(char *text, char **ip, int with_prefix);
static int net_check_ip(char *buf, int multi, int with_prefix);
static int compare_subnet(char *ip1, char *ip2, unsigned prefix);
static void get_and_copy_ifcfg_flags(ifcfg_t *ifcfg, char *device);
static void update_sysconfig(slist_t *slist, char *filename);
/*
* Ask for VNC & SSH password, unless they have already been set.
*
* Global vars changed:
* config.net.vncpassword
* config.net.sshpassword
*/
void net_ask_password()
{
int win_old = config.win;
if(config.vnc && (!config.net.vncpassword || strlen(config.net.vncpassword) < 8)) {
if(!config.win) util_disp_init();
for(;;) {
if(dia_input2("Enter your VNC password.", &config.net.vncpassword, 20, 1)) break;
if(config.net.vncpassword && strlen(config.net.vncpassword) >= 8) break;
dia_message("Password is too short (must have at least 8 characters).", MSGTYPE_ERROR);
}
}
if(config.usessh && !(config.net.sshpassword || config.net.sshpassword_enc || config.net.sshkey)) {
if(!config.win) util_disp_init();
dia_input2("Enter your temporary SSH password.", &config.net.sshpassword, 20, 1);
}
if(config.win && !win_old) util_disp_done();
}
/*
* Configure network. Ask for network config data if necessary.
* Calls either net_dhcp() or net_static() to setup the interface.
*
* Return:
* 0: ok
* != 0: error or abort
*
* Does nothing if DHCP is active.
*
* FIXME: needs window mode or not?
*/
int net_config()
{
int rc = 0;
char buf[256];
// in manual mode, ask for everything
if(config.manual) config.net.setup |= NS_DEFAULT;
// FIXME: not really here
net_ask_password();
if(
config.win &&
config.ifcfg.if_up &&
(!config.manual || dia_yesno("Your network is already configured. Keep this configuration?", YES) != NO)
) {
return 0;
}
if(net_choose_device()) return -1;
net_stop();
config.net.configured = nc_none;
if(config.win) {
rc = net_input_vlanid();
if(rc) return -1;
}
/*
* VLANID is handled in net_input_vlanid() a few lines above. Take this
* into account when deciding if there's anything else besides DHCP to be
* done.
*/
if(config.win && (config.net.setup & ~NS_VLANID) != NS_DHCP) {
if(
config.net.setup & NS_DHCP &&
#if defined(__s390__) || defined(__s390x__)
config.hwp.layer2 != LAYER2_NO &&
#endif
!config.ifcfg.manual->ptp
) {
sprintf(buf, "Automatic configuration via %s?", "DHCP");
rc = dia_yesno(buf, YES);
}
else {
rc = NO;
}
}
else {
rc = YES;
}
if(rc == ESCAPE) return -1;
if(rc == YES) {
rc = net_dhcp();
if(!rc) config.net.configured = nc_dhcp;
}
else {
rc = net_input_data();
if(!rc) {
net_static();
config.net.configured = nc_static;
}
}
if(rc) return -1;
// dia_message("An error occurred during the network configuration. Your network card probably was not recognized by the kernel.", MSGTYPE_ERROR);
return rc;
}
/*
* Shut down all network interfaces.
*
* config.net.device: interface
* /proc/net/route: configured interfaces
*/
void net_stop()
{
char *device = config.ifcfg.current;
if(!device) return;
log_debug("%s: network down\n", device);
if(config.test) {
return;
}
net_wicked_down(device);
// delete current config
if(config.ifcfg.current) {
char *buf = NULL;
strprintf(&buf, "/etc/sysconfig/network/ifcfg-%s", config.ifcfg.current);
unlink(buf);
strprintf(&buf, "/etc/sysconfig/network/ifroute-%s", config.ifcfg.current);
unlink(buf);
str_copy(&buf, NULL);
str_copy(&config.ifcfg.current, NULL);
}
}
/*
* Setup network interface with static config.
*
* Return:
* 0: ok
* != 0: error code
*
*/
int net_static()
{
char *s;
/* make sure we get the interface name if a mac was passed */
if((s = mac_to_interface(config.ifcfg.manual->device, NULL))) {
free(config.ifcfg.manual->device);
config.ifcfg.manual->device = s;
}
config.ifcfg.manual->dhcp = 0;
get_and_copy_ifcfg_flags(config.ifcfg.manual, config.ifcfg.manual->device);
if(!ifcfg_write(config.ifcfg.manual->device, config.ifcfg.manual, 0)) {
return 1;
}
char *ifname = NULL;
str_copy(&ifname, net_get_ifname(config.ifcfg.manual));
net_wicked_up(ifname);
str_copy(&ifname, NULL);
return 0;
}
/**
* Parse inet->name for ipv4/ipv6 adress.
* If do_dns is set, try nameserver lookup.
*
* Sets name && ok && ((ip6 && prefix6) || (ipv4 && ip && net && prefix4))
*
* @return
* - 0 : ok
* - 1 : failed
*/
int net_check_address(inet_t *inet, int do_dns)
{
struct hostent *he = NULL;
slist_t *sl;
char *s, buf[INET6_ADDRSTRLEN];
int net_bits = 0;
if(!inet) return 1;
s = inet->name;
memset(inet, 0, sizeof *inet);
inet->name = s;
if(!inet->name) return 1;
if(!*inet->name) {
str_copy(&inet->name, NULL);
return 1;
}
inet->ok = 1;
if((s = strchr(inet->name, '/'))) {
*s++ = 0;
net_bits = strtoul(s, &s, 0);
if(*s || net_bits < 0) {
inet->ok = 0;
}
}
if(inet->ok) {
if(strchr(inet->name, ':')) {
if(inet_pton(AF_INET6, inet->name, &inet->ip6) > 0) {
inet->ipv6 = 1;
s = (char *) inet_ntop(AF_INET6, &inet->ip6, buf, sizeof buf);
if(s) str_copy(&inet->name, s);
if(net_bits > 0) inet->prefix6 = net_bits;
}
else {
inet->ok = 0;
}
}
else {
if(inet_pton(AF_INET, inet->name, &inet->ip) > 0) {
inet->ipv4 = 1;
s = (char *) inet_ntop(AF_INET, &inet->ip, buf, sizeof buf);
if(s) str_copy(&inet->name, s);
if(net_bits > 0) {
inet->prefix4 = net_bits;
inet->net.s_addr = htonl(-1 << (32 - net_bits));
}
}
else {
inet->ok = 0;
}
}
}
if(
!do_dns ||
// do we really need this check ??
(
!config.net.dhcp_active &&
!config.net.nameserver[0].ok &&
!config.test &&
config.run_as_linuxrc
)
) {
inet->ok = inet->ok && ((config.net.ipv6 && inet->ipv6) || (config.net.ipv4 && inet->ipv4));
return inet->ok ? 0 : 1;
}
if(!inet->ipv6 && !inet->ipv4) {
for(sl = config.net.dns_cache; sl; sl = sl->next) {
if(sl->key && sl->value && !strcasecmp(sl->key, inet->name)) {
if(!inet->ipv6 && config.net.ipv6 && strchr(sl->value, ':')) {
if(inet_pton(AF_INET6, sl->value, &inet->ip6) > 0) {
inet->ipv6 = 1;
}
}
if(!inet->ipv4 && config.net.ipv4 && !strchr(sl->value, ':')) {
if(inet_pton(AF_INET, sl->value, &inet->ip) > 0) {
inet->ipv4 = 1;
}
}
}
}
}
if(inet->ipv6 || inet->ipv4) inet->ok = 1;
if(!inet->ok) {
if(config.net.ipv6) {
he = gethostbyname2(inet->name, AF_INET6);
if(!he) { sleep(1); he = gethostbyname2(inet->name, AF_INET6); }
if(!he) {
if(config.run_as_linuxrc) {
log_info("dns6: what is \"%s\"?\n", inet->name);
}
}
else {
inet->ok = 1;
inet->ipv6 = 1;
memcpy(&inet->ip6, *he->h_addr_list, he->h_length);
s = (char *) inet_ntop(AF_INET6, &inet->ip6, buf, sizeof buf);
if(s) {
sl = slist_add(&config.net.dns_cache, slist_new());
str_copy(&sl->key, inet->name);
str_copy(&sl->value, s);
if(config.run_as_linuxrc) {
log_info("dns6: %s is %s\n", inet->name, s);
}
}
}
}
if(config.net.ipv4) {
he = gethostbyname2(inet->name, AF_INET);
if(!he) { sleep(1); gethostbyname2(inet->name, AF_INET); }
if(!he) {
if(config.run_as_linuxrc) {
log_info("dns: what is \"%s\"?\n", inet->name);
}
}
else {
inet->ok = 1;
inet->ipv4 = 1;
memcpy(&inet->ip, *he->h_addr_list, he->h_length);
s = (char *) inet_ntop(AF_INET, &inet->ip, buf, sizeof buf);
if(s) {
sl = slist_add(&config.net.dns_cache, slist_new());
str_copy(&sl->key, inet->name);
str_copy(&sl->value, s);
if(config.run_as_linuxrc) {
log_info("dns: %s is %s\n", inet->name, s);
}
}
}
}
}
inet->ok = inet->ok && ((config.net.ipv6 && inet->ipv6) || (config.net.ipv4 && inet->ipv4));
return inet->ok ? 0 : 1;
}
/*
* Build mount option suitable for muont.cifs.
*/
void net_cifs_build_options(char **options, char *user, char *password, char *workgroup)
{
if(!options) return;
str_copy(options, "ro");
if(user) {
strprintf(options, "%s,username=%s,password=%s", *options, user, password ?: "");
if(workgroup) {
strprintf(options, "%s,workgroup=%s", *options, workgroup);
}
} else {
strprintf(options, "%s,guest", *options);
}
}
/*
* Mount windows share.
* (Run mount.cifs.)
*
* Return:
* 0: ok
* != 0: error code
*
* mountpoint: mount point
* server: SMB server
* share: share
* user: user (NULL: guest)
* password: password (NULL: no password)
* workgroup: workgroup (NULL: no workgroup)
*
* depending on guest login
* options += "guest"
* resp.
* options += "username=" + USERNAME + ",password=" + PASSWORD
*
* device = "//" + SERVER + "/" + SHARE
* options += ",workgroup=" + WORKGROUP falls WORKGROUP gesetzt ist
* options += ",ip=" + SERVER_IP falls SERVER_IP gesetzt ist
* " mount -t smbfs" + device + " " + mountpoint + " " + options
*/
int net_mount_cifs(char *mountpoint, char *server, char *share, char *user, char *password, char *workgroup, char *options)
{
char *cmd = NULL;
char *real_options = NULL;
int err;
if(!config.net.cifs.binary || !server) return -EDESTADDRREQ; // -89
mod_modprobe(config.net.cifs.module, NULL);
if(!share) share = "";
if(!mountpoint || !*mountpoint) mountpoint = "/";
net_cifs_build_options(&real_options, user, password, workgroup);
if(options) {
if(*options == '-') {
str_copy(&real_options, options + 1);
}
else {
strprintf(&real_options, "%s,%s", real_options, options);
}
}
strprintf(&cmd, "%s '//%s/%s' '%s' -o '%s'", config.net.cifs.binary, server, share, mountpoint, real_options);
err = lxrc_run(cmd);
str_copy(&cmd, NULL);
str_copy(&real_options, NULL);
return err ? -1 : 0;
}
/*
* Mount NFS volume.
*
* mountpoint: mount point
* server: NFS server name
* hostdir: directory on server
* options: NFS mount options
*
* config.net.nfs: nfs options if options == NULL
*
* options are added to any options linuxrc uses unless it is prefixed with '-'.
*
* return:
* 0: ok
* != 0: error code
*
*/
int net_mount_nfs(char *mountpoint, char *server, char *hostdir, unsigned port, char *options)
{
char *path = NULL;
char *real_options = NULL;
pid_t mount_pid;
if(!server) return -EDESTADDRREQ; // -89
if(!hostdir) hostdir = "/";
if(!mountpoint || !*mountpoint) mountpoint = "/";
mount_pid = fork();
if(mount_pid < 0) {
perror_info("fork");
return mount_pid;
}
else if(mount_pid > 0) {
int err;
pid_t pid;
while((pid = waitpid(-1, &err, 0)) && pid != mount_pid);
return WEXITSTATUS(err);
}
if(strchr(server, ':')) {
strprintf(&path, "[%s]:%s", server, hostdir);
}
else {
strprintf(&path, "%s:%s", server, hostdir);
}
str_copy(&real_options, "nolock");
if(!options) options = config.net.nfs.opts;
if(options) {
if(*options == '-') {
str_copy(&real_options, options + 1);
}
else {
strprintf(&real_options, "%s,%s", real_options, options);
}
}
log_debug("mount -o '%s' '%s' '%s'\n", real_options, path, mountpoint);
char *args[6] = { "mount", "-o", real_options, path, mountpoint /*, NULL */ };
signal(SIGUSR1, SIG_IGN);
execvp("mount", args);
perror_info("execvp(\"mount\")");
exit(EXIT_FAILURE);
}
/*
* Let user select a network interface.
*
* Does nothing if network interface has been explicitly specified.
* No user interaction if there is exactly one interface.
* Shows error message if there is no interface.
*
* Note: expects window mode.
*
* Global vars changed:
* config.net.device
*
*/
int net_choose_device()
{
char **items, **item_devs = NULL;
int i, max_items = 0, item_cnt, choice, width;
char *buf = NULL;
file_t *f0, *f;
slist_t *sl;
window_t win;
static int net_drivers_loaded = 0;
static int last_item = 0;
static struct {
char *dev;
char *name;
} net_dev[] = {
{ "eth", "ethernet network card" },
{ "veth", "ethernet network card" },
{ "plip", "parallel port" },
{ "arc", "arcnet network card" },
{ "fddi", "FDDI network card" },
{ "hip", "HIPPI network card" },
{ "ctc", "channel to channel connection" },
{ "ci", "channel attached cisco router" },
{ "iucv", "IUCV connection" },
{ "hsi", "HiperSocket" }
};
hd_data_t *hd_data = calloc(1, sizeof *hd_data);
hd_t *hd, *hd_cards;
hd_t **item_hds = NULL;
// return if we already have one and we're not in manual mode
if(config.ifcfg.manual->device && !config.manual) return 0;
if(config.manual == 1 && !net_drivers_loaded) {
dia_info(&win, "Detecting and loading network drivers", MSGTYPE_INFO);
load_network_mods();
win_close(&win);
net_drivers_loaded = 1;
}
/* The IUCV driver is special. There's no way for anything to detect
IUCV is available for use unless the driver is already loaded. So,
if we're running on z/VM we always load it, no matter what. */
#if defined(__s390__) || defined(__s390x__)
if(!strcmp(config.hwp.hypervisor, "z/VM")) {
dia_info(&win, "We are running on z/VM", MSGTYPE_INFO);
dia_info(&win, "Loading the IUCV network driver", MSGTYPE_INFO);
mod_modprobe("netiucv","");
}
#endif
if(config.manual >= 2) {
#if defined(__s390__) || defined(__s390x__)
/* bring up network devices, write hwcfg */
log_info("activate s390 devs 1\n");
if(net_activate_s390_devs()) return 1;
#endif
/* re-read - just in case... */
util_update_netdevice_list(NULL, 1);
for(sl = config.net.devices; sl; sl = sl->next) {
if(sl->key) max_items++;
}
items = calloc(max_items + 1, sizeof *items);
item_devs = calloc(max_items + 1, sizeof *item_devs);
f0 = file_read_file("/proc/net/dev", kf_none);
if(!f0) return -1;
for(item_cnt = 0, f = f0; f && item_cnt < max_items; f = f->next) {
for(i = 0; i < sizeof net_dev / sizeof *net_dev; i++) {
if(strstr(f->key_str, net_dev[i].dev) == f->key_str) {
strprintf(&buf, "%-6s : %s", f->key_str, net_dev[i].name);
item_devs[item_cnt] = strdup(f->key_str);
items[item_cnt++] = strdup(buf);
break;
}
}
}
file_free_file(f0);
}
else {
hd_data->flags.nowpa = 1;
hd_cards = hd_list(hd_data, hw_network_ctrl, 1, NULL);
for(hd = hd_cards; hd; hd = hd->next) max_items++;
items = calloc(max_items + 1, sizeof *items);
item_devs = calloc(max_items + 1, sizeof *item_devs);
item_hds = calloc(max_items + 1, sizeof *item_hds);
for(width = 0, hd = hd_cards; hd; hd = hd->next) {
if(hd->unix_dev_name) {
i = strlen(hd->unix_dev_name);
if(i > width) width = i;
}
}
for(item_cnt = 0, hd = hd_cards; hd; hd = hd->next) {
char* annotation = 0;
hd_res_t *res;
item_hds[item_cnt] = hd;
if(hd->unix_dev_name) {
item_devs[item_cnt] = strdup(hd->unix_dev_name);
}
#if defined(__s390__) || defined(__s390x__)
#define MAX_NET_DEVICES_SHOWN 20
if(item_cnt > MAX_NET_DEVICES_SHOWN) {
item_hds[item_cnt] = NULL;
strprintf(items + item_cnt++, "Enter network device parameters manually");
break;
} else {
int lcss = -1;
int ccw = -1;
hd_res_t* r;
if(hd->detail && hd->detail->ccw.data)
lcss = hd->detail->ccw.data->lcss;
for(r = hd->res; r; r = r->next) {
if(r->any.type == res_io) {
ccw = (int) r->io.base;
}
}
if ( ccw == -1 ) {
/* IUCV device */
if(hd->rom_id) strprintf(&annotation, "(%s)", hd->rom_id);
else strprintf(&annotation, "");
}
else {
strprintf(&annotation, "(%1x.%1x.%04x)", lcss >> 8, lcss & 0xf, ccw);
}
}
#endif
if(!annotation) {
for(res = hd->res; res; res = res->next) {
if(res->any.type == res_hwaddr) {
strprintf(&annotation, "(%s)", res->hwaddr.addr);
break;
}
}
}
if(hd->unix_dev_name) {
strprintf(items + item_cnt++, "%*s : %s %s", -width, hd->unix_dev_name, hd->model,
annotation);
}
else {
strprintf(items + item_cnt++, "%s %s", hd->model, annotation);
}
free(annotation);
annotation = 0;
}
}
// only this should be used rather than all the hd_list() calls above...
update_device_list(0);
if(item_cnt == 0) {
dia_message("No network device found.\n"
"Load a network module first.",
MSGTYPE_ERROR);
choice = -1;
} else if(item_cnt == 1) {
choice = 1;
}
else {
choice = dia_list("Choose the network device.", 0, NULL, items, last_item, align_left);
if(choice) last_item = choice;
}
/*
*
* If item_devs[choice - 1] has a value, then that means the interface should already
* configured. If so, we can check sysfs to see if we should be using layer2 or not.
*
*/
if(choice > 0 && !item_devs[choice - 1]) {
#if defined(__s390__) || defined(__s390x__)
log_info("activate s390 devs 2\n");
net_activate_s390_devs_ex(item_hds[choice - 1], &item_devs[choice - 1]);
if(!item_devs[choice - 1]) {
#endif
dia_message("No network device found.\n"
"Load a network module first.",
MSGTYPE_ERROR);
choice = -1;
#if defined(__s390__) || defined(__s390x__)
}
#endif
}
#if defined(__s390__) || defined(__s390x__)
else {
if(choice > 0) {
char path[PATH_MAX]="";
char *type;
sprintf(path, "/sys/class/net/%s/device/layer2", item_devs[choice - 1]);
type = util_get_attr(path);
// set the layer2 tag for network cards
// LAYER2_UNDEF means it doesn't have this attribute (e.g. virtio)
if(*type == 0) {
config.hwp.layer2 = LAYER2_UNDEF;
}
else if(*type == '1') {
config.hwp.layer2 = LAYER2_YES;
}
else {
config.hwp.layer2 = LAYER2_NO;
}
}
}
#endif
if(choice > 0) {
str_copy(&config.ifcfg.manual->device, item_devs[choice - 1]);
check_ptp(config.ifcfg.manual->device);
if(item_hds && item_hds[choice - 1]) {
hd = item_hds[choice - 1];
}
}
for(i = 0; i < item_cnt; i++) {
free(items[i]);
if(item_devs) free(item_devs[i]);
}
free(items);
free(item_devs);
free(item_hds);
hd_free_hd_data(hd_data);
free(hd_data);
free(buf);
return choice > 0 ? 0 : -1;
}
/*
* Let user enter network config data.
*
* Config adata are stored in config.ifcfg.manual.
*
* Note: expects window mode.
*/
int net_input_data()
{
if(config.ifcfg.manual->ptp) {
if((config.net.setup & NS_HOSTIP)) {
if(net_get_ip(
"Enter your IP address.\n\n"
"You can enter more than one, separated by space, if necessary.\n"
"Leave empty for autoconfig.\n\n"
"Examples: 192.168.5.77 2001:db8:75:fff::3",
&config.ifcfg.manual->ip, 0) == 2
) return -1;
}
if(net_get_ip(
"Enter the IP address of the PLIP partner.\n\n"
"Examples: 192.168.5.77 2001:db8:75:fff::3",
&config.ifcfg.manual->gw, 0) == 2
) return -1;
}
else {
if((config.net.setup & NS_HOSTIP)) {
if(net_get_ip(
"Enter your IP address with network prefix.\n\n"
"You can enter more than one, separated by space, if necessary.\n"
"Leave empty for autoconfig.\n\n"
"Examples: 192.168.5.77/24 2001:db8:75:fff::3/64",
&config.ifcfg.manual->ip, 1) == 2
) return -1;
}
if((config.net.setup & NS_GATEWAY)) {
if(net_get_ip(
"Enter your gateway IP address.\n\n"
"You can enter more than one, separated by space, if necessary.\n"
"Leave empty if you don't need one.\n\n"
"Examples: 192.168.5.77 2001:db8:75:fff::3",
&config.ifcfg.manual->gw, 0) == 2
) return -1;
}
}
if((config.net.setup & NS_NAMESERVER)) {
if(net_get_ip(
"Enter your name server IP address.\n\n"
"You can enter more than one, separated by space, if necessary.\n"
"Leave empty if you don't need one.\n\n"
"Examples: 192.168.5.77 2001:db8:75:fff::3",
&config.ifcfg.manual->ns, 0) == 2
) return -1;
dia_input2(
"Enter your search domains, separated by a space.",
&config.ifcfg.manual->domain, 40, 0
);
}
return 0;
}
/*
* Ask for vlan id and store in config.ifcfg.manual.
*/
int net_input_vlanid()
{
int err = 0, id;
char *buf = NULL, *s;
if(!(config.net.setup & NS_VLANID)) return 0;
str_copy(&buf, config.ifcfg.manual->vlan);
do {
err = 0;
if(dia_input2("Enter your VLAN ID\n\nLeave empty if you don't setup a VLAN.", &buf, 30, 0)) {
err = 1;
break;
}
if(!buf) break;
id = strtoul(buf, &s, 0);
if(*s || id <= 0) err = 1;
if(err) dia_message("Invalid input.", MSGTYPE_ERROR);
} while(err);
if(!err) str_copy(&config.ifcfg.manual->vlan, buf);
str_copy(&buf, NULL);
return err;
}
/*
* Ask user for a space-separated list of network addresses.
*
* If with_prefix is set, it must include the '/prefix' notation.
*
* return:
* 0: ok
* 1: empty input
* 2: error or abort
*
* Note: expects window mode.
*/
int net_get_ip(char *text, char **ip, int with_prefix)
{
int err = 0;
char *buf = NULL;
if(ip) str_copy(&buf, *ip);
do {
err = 0;
if(dia_input2(text, &buf, 40, 0)) {
err = 2;
break;
}
if(!buf) {
err = 1;
break;
}
if(!net_check_ip(buf, 1, with_prefix)) err = 2;
if(err) dia_message("Invalid input.", MSGTYPE_ERROR);
} while(err);
if(err != 2 && ip) str_copy(ip, buf);
str_copy(&buf, NULL);
return err;
}
/*
* Ask user for some network address.
* (Used for netmasks, too.)
*
* Either numeric or dns resolved.
*
* return:
* 0: ok
* 1: empty input
* 2: error or abort
*
* Note: expects window mode.
*/
int net_get_address(char *text, inet_t *inet, int do_dns)
{