aboutsummaryrefslogtreecommitdiffstats
path: root/usr/kinit/ipconfig/main.c
blob: 37ca5734e62077f7eb91d72e952a571641a100c7 (plain)
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
#include <poll.h>
#include <limits.h>
#include <setjmp.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/time.h>
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>		/* for getopts */

#include <net/if_arp.h>

#include "ipconfig.h"
#include "netdev.h"
#include "bootp_packet.h"
#include "bootp_proto.h"
#include "dhcp_proto.h"
#include "packet.h"

static const char sysfs_class_net[] = "/sys/class/net";
static const char *progname;
static jmp_buf abort_buf;
static char do_not_config;
static unsigned int default_caps = CAP_DHCP | CAP_BOOTP | CAP_RARP;
static int loop_timeout = -1;
static int configured;
static int bringup_first = 0;

/* DHCP vendor class identifier */
char vendor_class_identifier[260];
int vendor_class_identifier_len;

struct state {
	int state;
	int restart_state;
	time_t expire;
	int retry_period;

	struct netdev *dev;
	struct state *next;
};

static inline const char *my_inet_ntoa(uint32_t addr)
{
	struct in_addr a;

	a.s_addr = addr;

	return inet_ntoa(a);
}

static void print_device_config(struct netdev *dev)
{
	printf("IP-Config: %s complete (from %s):\n", dev->name,
	       my_inet_ntoa(dev->serverid ? dev->serverid : dev->ip_server));
	printf(" address: %-16s ", my_inet_ntoa(dev->ip_addr));
	printf("broadcast: %-16s ", my_inet_ntoa(dev->ip_broadcast));
	printf("netmask: %-16s\n", my_inet_ntoa(dev->ip_netmask));
	printf(" gateway: %-16s ", my_inet_ntoa(dev->ip_gateway));
	printf("dns0     : %-16s ", my_inet_ntoa(dev->ip_nameserver[0]));
	printf("dns1   : %-16s\n", my_inet_ntoa(dev->ip_nameserver[1]));
	if (dev->hostname[0])
		printf(" host   : %-64s\n", dev->hostname);
	if (dev->dnsdomainname[0])
		printf(" domain : %-64s\n", dev->dnsdomainname);
	if (dev->nisdomainname[0])
		printf(" nisdomain: %-64s\n", dev->nisdomainname);
	printf(" rootserver: %s ", my_inet_ntoa(dev->ip_server));
	printf("rootpath: %s\n", dev->bootpath);
	printf(" filename  : %s\n", dev->filename);
}

static void configure_device(struct netdev *dev)
{
	if (do_not_config)
		return;

	if (netdev_setmtu(dev))
		printf("IP-Config: failed to set MTU on %s to %u\n",
		       dev->name, dev->mtu);

	if (netdev_setaddress(dev))
		printf("IP-Config: failed to set addresses on %s\n",
		       dev->name);
	if (netdev_setdefaultroute(dev))
		printf("IP-Config: failed to set default route on %s\n",
		       dev->name);
	if (dev->hostname[0] &&
			sethostname(dev->hostname, strlen(dev->hostname)))
		printf("IP-Config: failed to set hostname '%s' from %s\n",
			dev->hostname, dev->name);
}

/*
 * Escape shell varialbes in git style:
 * Always start with a single quote ('), then leave all characters
 * except ' and ! unchanged.
 */
static void write_option(FILE *f, const char *name, const char *chr)
{

	fprintf(f, "%s='", name);
	while (*chr) {
		switch (*chr) {
		case '!':
		case '\'':
			fprintf(f, "'\\%c'", *chr);
			break;
		default:
			fprintf(f, "%c", *chr);
			break;
		}
		++chr;
	}
	fprintf(f, "'\n");
}

static void dump_device_config(struct netdev *dev)
{
	char fn[40];
	FILE *f;

	snprintf(fn, sizeof(fn), "/tmp/net-%s.conf", dev->name);
	f = fopen(fn, "w");
	if (f) {
		write_option(f, "DEVICE", dev->name);
		write_option(f, "IPV4ADDR",
				my_inet_ntoa(dev->ip_addr));
		write_option(f, "IPV4BROADCAST",
				my_inet_ntoa(dev->ip_broadcast));
		write_option(f, "IPV4NETMASK",
				my_inet_ntoa(dev->ip_netmask));
		write_option(f, "IPV4GATEWAY",
				my_inet_ntoa(dev->ip_gateway));
		write_option(f, "IPV4DNS0",
				my_inet_ntoa(dev->ip_nameserver[0]));
		write_option(f, "IPV4DNS1",
				my_inet_ntoa(dev->ip_nameserver[1]));
		write_option(f, "HOSTNAME",  dev->hostname);
		write_option(f, "DNSDOMAIN", dev->dnsdomainname);
		write_option(f, "NISDOMAIN", dev->nisdomainname);
		write_option(f, "ROOTSERVER",
				my_inet_ntoa(dev->ip_server));
		write_option(f, "ROOTPATH", dev->bootpath);
		write_option(f, "filename", dev->filename);
		fclose(f);
	}
}

static uint32_t inet_class_netmask(uint32_t ip)
{
	ip = ntohl(ip);
	if (IN_CLASSA(ip))
		return htonl(IN_CLASSA_NET);
	if (IN_CLASSB(ip))
		return htonl(IN_CLASSB_NET);
	if (IN_CLASSC(ip))
		return htonl(IN_CLASSC_NET);
	return INADDR_ANY;
}

static void postprocess_device(struct netdev *dev)
{
	if (dev->ip_netmask == INADDR_ANY) {
		dev->ip_netmask = inet_class_netmask(dev->ip_addr);
		printf("IP-Config: %s guessed netmask %s\n",
		       dev->name, my_inet_ntoa(dev->ip_netmask));
	}
	if (dev->ip_broadcast == INADDR_ANY) {
		dev->ip_broadcast =
		    (dev->ip_addr & dev->ip_netmask) | ~dev->ip_netmask;
		printf("IP-Config: %s guessed broadcast address %s\n",
		       dev->name, my_inet_ntoa(dev->ip_broadcast));
	}
}

static void complete_device(struct netdev *dev)
{
	postprocess_device(dev);
	configure_device(dev);
	dump_device_config(dev);
	print_device_config(dev);

	++configured;

	dev->next = ifaces;
	ifaces = dev;
}

/*
 * Returns:
 *  0 = Not handled, try again later
 *  1 = Handled
 */
static int process_receive_event(struct state *s, time_t now)
{
	int handled = 1;

	switch (s->state) {
	case DEVST_ERROR:
		return 0; /* Not handled */
	case DEVST_COMPLETE:
		return 0; /* Not handled as already configured */

	case DEVST_BOOTP:
		s->restart_state = DEVST_BOOTP;
		switch (bootp_recv_reply(s->dev)) {
		case -1:
			s->state = DEVST_ERROR;
			break;
		case 0:
			handled = 0;
			break;
		case 1:
			s->state = DEVST_COMPLETE;
			dprintf("\n   bootp reply\n");
			break;
		}
		break;

	case DEVST_DHCPDISC:
		s->restart_state = DEVST_DHCPDISC;
		switch (dhcp_recv_offer(s->dev)) {
		case -1:
			s->state = DEVST_ERROR;
			break;
		case 0:
			handled = 0;
			break;
		case DHCPOFFER:	/* Offer received */
			s->state = DEVST_DHCPREQ;
			dhcp_send_request(s->dev);
			break;
		}
		break;

	case DEVST_DHCPREQ:
		s->restart_state = DEVST_DHCPDISC;
		switch (dhcp_recv_ack(s->dev)) {
		case -1:	/* error */
			s->state = DEVST_ERROR;
			break;
		case 0:
			handled = 0;
			break;
		case DHCPACK:	/* ACK received */
			s->state = DEVST_COMPLETE;
			break;
		case DHCPNAK:	/* NAK received */
			s->state = DEVST_DHCPDISC;
			break;
		}
		break;

	default:
		dprintf("\n");
		handled = 0;
		break;
	}

	switch (s->state) {
	case DEVST_COMPLETE:
		complete_device(s->dev);
		break;

	case DEVST_ERROR:
		/* error occurred, try again in 10 seconds */
		s->expire = now + 10;
		break;
	}

	return handled;
}

static void process_timeout_event(struct state *s, time_t now)
{
	int ret = 0;

	/*
	 * Is the link up?  If not, try again in 1 second.
	 */
	if (!netdev_running(s->dev)) {
		s->expire = now + 1;
		s->state = s->restart_state;
		return;
	}

	/*
	 * If we had an error, restore a sane state to
	 * restart from.
	 */
	if (s->state == DEVST_ERROR)
		s->state = s->restart_state;

	/*
	 * Now send a packet depending on our state.
	 */
	switch (s->state) {
	case DEVST_BOOTP:
		ret = bootp_send_request(s->dev);
		s->restart_state = DEVST_BOOTP;
		break;

	case DEVST_DHCPDISC:
		ret = dhcp_send_discover(s->dev);
		s->restart_state = DEVST_DHCPDISC;
		break;

	case DEVST_DHCPREQ:
		ret = dhcp_send_request(s->dev);
		s->restart_state = DEVST_DHCPDISC;
		break;
	}

	if (ret == -1) {
		s->state = DEVST_ERROR;
		s->expire = now + 10;
	} else {
		s->expire = now + s->retry_period;

		s->retry_period *= 2;
		if (s->retry_period > 60)
			s->retry_period = 60;
	}
}

static struct state *slist;
struct netdev *ifaces;

/*
 * Returns:
 *  0 = No dhcp/bootp packet was received
 *  1 = A packet was received and handled
 */
static int do_pkt_recv(int pkt_fd, time_t now)
{
	int ret = 0;
	struct state *s;

	for (s = slist; s; s = s->next)
		ret |= process_receive_event(s, now);
	return ret;
}

static int loop(void)
{
#define NR_FDS	1
	struct pollfd fds[NR_FDS];
	struct state *s;
	int pkt_fd;
	int nr = 0, rc = 0;
	struct timeval now, prev;
	time_t start;

	pkt_fd = packet_open();
	if (pkt_fd == -1) {
		perror("packet_open");
		return -1;
	}

	fds[0].fd = pkt_fd;
	fds[0].events = POLLRDNORM;

	gettimeofday(&now, NULL);
	start = now.tv_sec;
	while (1) {
		int timeout = 60;
		int pending = 0;
		int done = 0;
		int timeout_ms;
		int x;

		for (s = slist; s; s = s->next) {
			dprintf("%s: state = %d\n", s->dev->name, s->state);

			if (s->state == DEVST_COMPLETE) {
				done++;
				continue;
			}

			pending++;

			if (s->expire - now.tv_sec <= 0) {
				dprintf("timeout\n");
				process_timeout_event(s, now.tv_sec);
			}

			if (timeout > s->expire - now.tv_sec)
				timeout = s->expire - now.tv_sec;
		}

		if (pending == 0 || (bringup_first && done))
			break;

		timeout_ms = timeout * 1000;

		for (x = 0; x < 2; x++) {
			int delta_ms;

			if (timeout_ms <= 0)
				timeout_ms = 100;

			nr = poll(fds, NR_FDS, timeout_ms);
			prev = now;
			gettimeofday(&now, NULL);

			if ((nr > 0) && (fds[0].revents & POLLRDNORM)) {
				if (do_pkt_recv(pkt_fd, now.tv_sec) == 1)
					break;
			}

			if (loop_timeout >= 0 &&
			    now.tv_sec - start >= loop_timeout) {
				printf("IP-Config: no response after %d "
				       "secs - giving up\n", loop_timeout);
				rc = -1;
				goto bail;
			}

			delta_ms = (now.tv_sec - prev.tv_sec) * 1000;
			delta_ms += (now.tv_usec - prev.tv_usec) / 1000;

			dprintf("Delta: %d ms\n", delta_ms);

			timeout_ms -= delta_ms;
		}
	}
bail:
	packet_close();

	return rc;
}

static int add_one_dev(struct netdev *dev)
{
	struct state *state;

	state = malloc(sizeof(struct state));
	if (!state)
		return -1;

	state->dev = dev;
	state->expire = time(NULL);
	state->retry_period = 1;

	/*
	 * Select the state that we start from.
	 */
	if (dev->caps & CAP_DHCP && dev->ip_addr == INADDR_ANY)
		state->restart_state = state->state = DEVST_DHCPDISC;
	else if (dev->caps & CAP_DHCP)
		state->restart_state = state->state = DEVST_DHCPREQ;
	else if (dev->caps & CAP_BOOTP)
		state->restart_state = state->state = DEVST_BOOTP;

	state->next = slist;
	slist = state;

	return 0;
}

static void parse_addr(uint32_t *addr, const char *ip)
{
	struct in_addr in;
	if (inet_aton(ip, &in) == 0) {
		fprintf(stderr, "%s: can't parse IP address '%s'\n",
			progname, ip);
		longjmp(abort_buf, 1);
	}
	*addr = in.s_addr;
}

static unsigned int parse_proto(const char *ip)
{
	unsigned int caps = 0;

	if (*ip == '\0' || strcmp(ip, "on") == 0 || strcmp(ip, "any") == 0)
		caps = CAP_BOOTP | CAP_DHCP | CAP_RARP;
	else if (strcmp(ip, "both") == 0)
		caps = CAP_BOOTP | CAP_RARP;
	else if (strcmp(ip, "dhcp") == 0)
		caps = CAP_BOOTP | CAP_DHCP;
	else if (strcmp(ip, "bootp") == 0)
		caps = CAP_BOOTP;
	else if (strcmp(ip, "rarp") == 0)
		caps = CAP_RARP;
	else if (strcmp(ip, "none") == 0 || strcmp(ip, "static") == 0
		 || strcmp(ip, "off") == 0)
		goto bail;
	else {
		fprintf(stderr, "%s: invalid protocol '%s'\n", progname, ip);
		longjmp(abort_buf, 1);
	}
bail:
	return caps;
}

static int add_all_devices(struct netdev *template);

static int parse_device(struct netdev *dev, const char *ip)
{
	char *cp;
	int opt;
	int is_ip = 0;

	dprintf("IP-Config: parse_device: \"%s\"\n", ip);

	if (strncmp(ip, "ip=", 3) == 0) {
		ip += 3;
		is_ip = 1;
	} else if (strncmp(ip, "nfsaddrs=", 9) == 0) {
		ip += 9;
		is_ip = 1;	/* Not sure about this...? */
	}

	if (!strchr(ip, ':')) {
		/* Only one option, e.g. "ip=dhcp", or an interface name */
		if (is_ip) {
			dev->caps = parse_proto(ip);
			bringup_first = 1;
		} else {
			dev->name = ip;
		}
	} else {
		for (opt = 0; ip && *ip; ip = cp, opt++) {
			if ((cp = strchr(ip, ':'))) {
				*cp++ = '\0';
			}
			if (opt > 6) {
				fprintf(stderr,
					"%s: too many options for %s\n",
					progname, dev->name);
				longjmp(abort_buf, 1);
			}

			if (*ip == '\0')
				continue;
			dprintf("IP-Config: opt #%d: '%s'\n", opt, ip);
			switch (opt) {
			case 0:
				parse_addr(&dev->ip_addr, ip);
				dev->caps = 0;
				break;
			case 1:
				parse_addr(&dev->ip_server, ip);
				break;
			case 2:
				parse_addr(&dev->ip_gateway, ip);
				break;
			case 3:
				parse_addr(&dev->ip_netmask, ip);
				break;
			case 4:
				strncpy(dev->hostname, ip, SYS_NMLN - 1);
				dev->hostname[SYS_NMLN - 1] = '\0';
				memcpy(dev->reqhostname, dev->hostname,
				       SYS_NMLN);
				break;
			case 5:
				dev->name = ip;
				break;
			case 6:
				dev->caps = parse_proto(ip);
				break;
			}
		}
	}

	if (dev->name == NULL ||
	    dev->name[0] == '\0' || strcmp(dev->name, "all") == 0) {
		add_all_devices(dev);
		bringup_first = 1;
		return 0;
	}
	return 1;
}

static void bringup_device(struct netdev *dev)
{
	if (netdev_up(dev) == 0) {
		if (dev->caps)
			add_one_dev(dev);
		else
			complete_device(dev);
	}
}

static void bringup_one_dev(struct netdev *template, struct netdev *dev)
{
	if (template->ip_addr != INADDR_NONE)
		dev->ip_addr = template->ip_addr;
	if (template->ip_server != INADDR_NONE)
		dev->ip_server = template->ip_server;
	if (template->ip_gateway != INADDR_NONE)
		dev->ip_gateway = template->ip_gateway;
	if (template->ip_netmask != INADDR_NONE)
		dev->ip_netmask = template->ip_netmask;
	if (template->ip_nameserver[0] != INADDR_NONE)
		dev->ip_nameserver[0] = template->ip_nameserver[0];
	if (template->ip_nameserver[1] != INADDR_NONE)
		dev->ip_nameserver[1] = template->ip_nameserver[1];
	if (template->hostname[0] != '\0')
		strcpy(dev->hostname, template->hostname);
	if (template->reqhostname[0] != '\0')
		strcpy(dev->reqhostname, template->reqhostname);
	dev->caps &= template->caps;

	bringup_device(dev);
}

static struct netdev *add_device(const char *info)
{
	struct netdev *dev;
	int i;

	dev = malloc(sizeof(struct netdev));
	if (dev == NULL) {
		fprintf(stderr, "%s: out of memory\n", progname);
		longjmp(abort_buf, 1);
	}

	memset(dev, 0, sizeof(struct netdev));
	dev->caps = default_caps;

	if (parse_device(dev, info) == 0)
		goto bail;

	if (netdev_init_if(dev) == -1)
		goto bail;

	if (bootp_init_if(dev) == -1)
		goto bail;

	printf("IP-Config: %s hardware address", dev->name);
	for (i = 0; i < dev->hwlen; i++)
		printf("%c%02x", i == 0 ? ' ' : ':', dev->hwaddr[i]);
	printf(" mtu %d%s%s\n", dev->mtu,
	       dev->caps & CAP_DHCP ? " DHCP" :
	       dev->caps & CAP_BOOTP ? " BOOTP" : "",
	       dev->caps & CAP_RARP ? " RARP" : "");
	return dev;
bail:
	free(dev);
	return NULL;
}

static int add_all_devices(struct netdev *template)
{
	DIR *d;
	struct dirent *de;
	struct netdev *dev;
	char t[PATH_MAX], p[255];
	int i, fd;
	unsigned long flags;

	d = opendir(sysfs_class_net);
	if (!d)
		return 0;

	while ((de = readdir(d)) != NULL) {
		/* This excludes devices beginning with dots or "dummy",
		   as well as . or .. */
		if (de->d_name[0] == '.' || !strcmp(de->d_name, ".."))
			continue;
		i = snprintf(t, PATH_MAX - 1, "%s/%s/flags", sysfs_class_net,
			     de->d_name);
		if (i < 0 || i >= PATH_MAX - 1)
			continue;
		t[i] = '\0';
		fd = open(t, O_RDONLY);
		if (fd < 0) {
			perror(t);
			continue;
		}
		i = read(fd, &p, sizeof(p) - 1);
		close(fd);
		if (i < 0) {
			perror(t);
			continue;
		}
		p[i] = '\0';
		flags = strtoul(p, NULL, 0);
		/* Heuristic for if this is a reasonable boot interface.
		   This is the same
		   logic the in-kernel ipconfig uses... */
		if (!(flags & IFF_LOOPBACK) &&
		    (flags & (IFF_BROADCAST | IFF_POINTOPOINT))) {
			dprintf("Trying to bring up %s\n", de->d_name);

			dev = add_device(de->d_name);
			if (!dev)
				continue;
			bringup_one_dev(template, dev);
		}
	}
	closedir(d);
	return 1;
}

static int check_autoconfig(void)
{
	int ndev = 0, nauto = 0;
	struct state *s;

	for (s = slist; s; s = s->next) {
		ndev++;
		if (s->dev->caps)
			nauto++;
	}

	if (ndev == 0) {
		if (configured == 0) {
			fprintf(stderr, "%s: no devices to configure\n",
				progname);
			longjmp(abort_buf, 1);
		}
	}

	return nauto;
}

static void set_vendor_identifier(const char *id)
{
	int len = strlen(id);
	if (len >= 255) {
		fprintf(stderr,
			"%s: invalid vendor class identifier: "
			"%s\n", progname, id);
		longjmp(abort_buf, 1);
	}
	memcpy(vendor_class_identifier+2, id, len);
	vendor_class_identifier[0] = 60;
	vendor_class_identifier[1] = len;
	vendor_class_identifier_len = len+2;
}

int main(int argc, char *argv[])
    __attribute__ ((weak, alias("ipconfig_main")));

int ipconfig_main(int argc, char *argv[])
{
	struct netdev *dev;
	int c, port;
	int err = 0;

	/* If progname is set we're invoked from another program */
	if (!progname) {
		struct timeval now;
		progname = argv[0];
		gettimeofday(&now, NULL);
		srand48(now.tv_usec ^ (now.tv_sec << 24));
	}

	if ((err = setjmp(abort_buf)))
		return err;

	/* Default vendor identifier */
	set_vendor_identifier("Linux ipconfig");

	do {
		c = getopt(argc, argv, "c:d:i:onp:t:");
		if (c == EOF)
			break;

		switch (c) {
		case 'c':
			default_caps = parse_proto(optarg);
			break;
		case 'p':
			port = atoi(optarg);
			if (port <= 0 || port > USHRT_MAX) {
				fprintf(stderr,
					"%s: invalid port number %d\n",
					progname, port);
				longjmp(abort_buf, 1);
			}
			cfg_local_port = port;
			cfg_remote_port = cfg_local_port - 1;
			break;
		case 't':
			loop_timeout = atoi(optarg);
			if (loop_timeout < 0) {
				fprintf(stderr,
					"%s: invalid timeout %d\n",
					progname, loop_timeout);
				longjmp(abort_buf, 1);
			}
			break;
		case 'i':
			set_vendor_identifier(optarg);
			break;
		case 'o':
			bringup_first = 1;
			break;
		case 'n':
			do_not_config = 1;
			break;
		case 'd':
			dev = add_device(optarg);
			if (dev)
				bringup_device(dev);
			break;
		case '?':
			fprintf(stderr, "%s: invalid option -%c\n",
				progname, optopt);
			longjmp(abort_buf, 1);
		}
	} while (1);

	for (c = optind; c < argc; c++) {
		dev = add_device(argv[c]);
		if (dev)
			bringup_device(dev);
	}

	if (check_autoconfig()) {
		if (cfg_local_port != LOCAL_PORT) {
			printf("IP-Config: binding source port to %d, "
			       "dest to %d\n",
			       cfg_local_port, cfg_remote_port);
		}
		err = loop();
	}

	return err;
}