summaryrefslogtreecommitdiffstats
path: root/fs/nfs/nfsroot.c
blob: b7c7dfba70599750b0ae960aa8fe7d24b95292ef (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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
/*
 *  $Id: nfsroot.c,v 1.38 1997/07/17 03:21:06 davem Exp $
 *
 *  Copyright (C) 1995, 1996  Gero Kuhlmann <gero@gkminix.han.de>
 *
 *  For parts of this file:
 *  Copyright (C) 1996, 1997  Martin Mares <mj@atrey.karlin.mff.cuni.cz>
 *
 *  Allow an NFS filesystem to be mounted as root. The way this works is:
 *     (1) Determine the local IP address via RARP or BOOTP or from the
 *         kernel command line.
 *     (2) Handle RPC negotiation with the system which replied to RARP or
 *         was reported as a boot server by BOOTP or manually.
 *     (3) The actual mounting is done later, when init() is running.
 *
 *
 *	Changes:
 *
 *	Alan Cox	:	Removed get_address name clash with FPU.
 *	Alan Cox	:	Reformatted a bit.
 *	Gero Kuhlmann	:	Code cleanup
 *	Michael Rausch  :	Fixed recognition of an incoming RARP answer.
 *	Martin Mares	: (2.0)	Auto-configuration via BOOTP supported.
 *	Martin Mares	:	Manual selection of interface & BOOTP/RARP.
 *	Martin Mares	:	Using network routes instead of host routes,
 *				allowing the default configuration to be used
 *				for normal operation of the host.
 *	Martin Mares	:	Randomized timer with exponential backoff
 *				installed to minimize network congestion.
 *	Martin Mares	:	Code cleanup.
 *	Martin Mares	: (2.1)	BOOTP and RARP made configuration options.
 *	Martin Mares	:	Server hostname generation fixed.
 *	Gerd Knorr	:	Fixed wired inode handling
 *	Martin Mares	: (2.2)	"0.0.0.0" addresses from command line ignored.
 *	Martin Mares	:	RARP replies not tested for server address.
 *	Gero Kuhlmann	: (2.3) Some bug fixes and code cleanup again (please
 *				send me your new patches _before_ bothering
 *				Linus so that I don' always have to cleanup
 *				_afterwards_ - thanks)
 *	Gero Kuhlmann	:	Last changes of Martin Mares undone.
 *	Gero Kuhlmann	: 	RARP replies are tested for specified server
 *				again. However, it's now possible to have
 *				different RARP and NFS servers.
 *	Gero Kuhlmann	:	"0.0.0.0" addresses from command line are
 *				now mapped to INADDR_NONE.
 *	Gero Kuhlmann	:	Fixed a bug which prevented BOOTP path name
 *				from being used (thanks to Leo Spiekman)
 *	Andy Walker	:	Allow to specify the NFS server in nfs_root
 *				without giving a path name
 *	Swen Th=FCmmler	:	Allow to specify the NFS options in nfs_root
 *				without giving a path name. Fix BOOTP request
 *				for domainname (domainname is NIS domain, not
 *				DNS domain!). Skip dummy devices for BOOTP.
 *	Jacek Zapala	:	Fixed a bug which prevented server-ip address
 *				from nfsroot parameter from being used.
 *	Olaf Kirch	:	Adapted to new NFS code.
 *	Jakub Jelinek	:	Free used code segment.
 *	Marko Kohtala	:	Fixed some bugs.
 *	Martin Mares	:	Debug message cleanup
 *
 */


/* Define this to allow debugging output */
#undef NFSROOT_DEBUG
#undef NFSROOT_BOOTP_DEBUG


#include <linux/config.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/fs.h>
#include <linux/random.h>
#include <linux/fcntl.h>
#include <linux/init.h>

#include <asm/param.h>
#include <linux/utsname.h>
#include <linux/in.h>
#include <linux/if.h>
#include <linux/inet.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#ifdef CONFIG_AX25
#include <net/ax25.h>	/* For AX25_P_IP */
#endif
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/socket.h>
#include <linux/route.h>
#include <linux/sunrpc/clnt.h>
#include <linux/nfs.h>
#include <linux/nfs_fs.h>
#include <linux/nfs_mount.h>
#include <linux/in.h>
#include <net/route.h>
#include <net/sock.h>

#include <asm/segment.h>
#include <asm/uaccess.h>

#define NFSDBG_FACILITY		NFSDBG_ROOT
/* Range of privileged ports */
#define STARTPORT		600
#define ENDPORT			1023
#define NPORTS			(ENDPORT - STARTPORT + 1)


/* Define the timeout for waiting for a RARP/BOOTP reply */
#define CONF_BASE_TIMEOUT	(HZ*5)	/* Initial timeout: 5 seconds */
#define CONF_RETRIES	 	10	/* 10 retries */
#define CONF_TIMEOUT_RANDOM	(HZ)	/* Maximum amount of randomization */
#define CONF_TIMEOUT_MULT	*5/4	/* Speed of timeout growth */
#define CONF_TIMEOUT_MAX	(HZ*30)	/* Maximum allowed timeout */


/* List of open devices */
struct open_dev {
	struct device *dev;
	unsigned short old_flags;
	struct open_dev *next;
};

static struct open_dev *open_base __initdata = NULL;


/* IP configuration */
static struct device *root_dev __initdata = NULL;	/* Device selected for booting */
static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Name of user-selected boot device */
static __u32 myaddr __initdata = 0;			/* My IP address */
static __u32 servaddr __initdata = 0;			/* Server IP address */
static __u32 gateway __initdata = 0;			/* Gateway IP address */
static __u32 netmask __initdata = 0;			/* Netmask for local subnet */


/* BOOTP/RARP variables */
static int bootp_flag __initdata = 0;		/* User said: Use BOOTP! */
static int rarp_flag __initdata = 0;		/* User said: Use RARP! */
static int bootp_dev_count __initdata = 0;	/* Number of devices allowing BOOTP */
static int rarp_dev_count __initdata = 0;	/* Number of devices allowing RARP */
static __u32 rarp_serv __initdata = 0;		/* IP address of RARP server */

#if defined(CONFIG_RNFS_BOOTP) || defined(CONFIG_RNFS_RARP)
#define CONFIG_RNFS_DYNAMIC			/* Enable dynamic IP config */
static volatile int pkt_arrived __initdata = 0;	/* BOOTP/RARP packet detected */

#define ARRIVED_BOOTP	1
#define ARRIVED_RARP	2
#endif


/* NFS-related data */
static struct nfs_mount_data nfs_data __initdata = { 0, };/* NFS mount info */
static char	nfs_path[NFS_MAXPATHLEN] __initdata = { 0, };/* Name of directory to mount */
static int	nfs_port __initdata = 0;		/* Port to connect to for NFS */
static int	mount_port __initdata = 0;		/* Mount daemon port number */


/* Yes, we use sys_socket, but there's no include file for it */
extern asmlinkage int sys_socket(int family, int type, int protocol);



/***************************************************************************

			Device Handling Subroutines

 ***************************************************************************/

/*
 * Setup and initialize all network devices. If there is a user-preferred
 * interface, ignore all other interfaces.
 */
__initfunc(static int root_dev_open(void))
{
	struct open_dev *openp, **last;
	struct device *dev;
	unsigned short old_flags;

	last = &open_base;
	for (dev = dev_base; dev != NULL; dev = dev->next) {
		if (dev->type < ARPHRD_SLIP &&
		    dev->family == AF_INET &&
		    !(dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
		    (0 != strncmp(dev->name, "dummy", 5)) &&
		    (!user_dev_name[0] || !strcmp(dev->name, user_dev_name))) {
			/* First up the interface */
			old_flags = dev->flags;
			dev->flags = IFF_UP | IFF_BROADCAST | IFF_RUNNING;
			if (!(old_flags & IFF_UP) && dev_open(dev)) {
				dev->flags = old_flags;
				continue;
			}
			openp = (struct open_dev *) kmalloc(sizeof(struct open_dev),
						GFP_ATOMIC);
			if (openp == NULL)
				continue;
			openp->dev = dev;
			openp->old_flags = old_flags;
			*last = openp;
			last = &openp->next;
			bootp_dev_count++;
			if (!(dev->flags & IFF_NOARP))
				rarp_dev_count++;
			dprintk("Root-NFS: Opened %s\n", dev->name);
		}
	}
	*last = NULL;

	if (!bootp_dev_count && !rarp_dev_count) {
		printk(KERN_ERR "Root-NFS: Unable to open at least one network device\n");
		return -1;
	}
	return 0;
}

static inline void
set_sockaddr(struct sockaddr_in *sin, __u32 addr, __u16 port)
{
	sin->sin_family = AF_INET;
	sin->sin_addr.s_addr = addr;
	sin->sin_port = port;
}

__initfunc(static int
root_dev_chg_route(int op, struct device *dev, __u32 dest, __u32 mask, __u32 gw))
{
	struct rtentry	route;
	mm_segment_t	oldfs;
	int		err;

	memset(&route, 0, sizeof(struct rtentry));	/* or else! */

	route.rt_dev = dev->name;
	route.rt_mtu = dev->mtu;
	route.rt_flags = RTF_UP;
	set_sockaddr((struct sockaddr_in *) &route.rt_dst, dest & mask, 0);
	set_sockaddr((struct sockaddr_in *) &route.rt_genmask, mask, 0);

	if (gw != 0) {
		set_sockaddr((struct sockaddr_in *) &route.rt_gateway, gw, 0);
		route.rt_flags |= RTF_GATEWAY;
		if ((gw ^ myaddr) & netmask) {
			printk(KERN_ERR "Root-NFS: Gateway not on local network!\n");
			return -ENETUNREACH;
		}
	}

	oldfs = get_fs();
	set_fs(KERNEL_DS);
	err = ip_rt_ioctl(op, &route);
	set_fs(oldfs);

#ifdef NFSROOT_DEBUG
	/* in_ntoa in ipv4/utils.c uses a single static buffer, so
	 * must make multiple printk calls, one for each in_ntoa
	 * invocation...
	 */
	printk(KERN_NOTICE "%s route ", (op == SIOCADDRT ? "add" : "del"));
	printk("%s ", in_ntoa(dest));
	printk("%s ", in_ntoa(mask));
	printk("%s: res %d\n", in_ntoa(gw), err);
#endif

	return err;
}

__initfunc(static int
root_dev_add_route(struct device *dev, __u32 dest, __u32 mask, __u32 gateway))
{
	return root_dev_chg_route(SIOCADDRT, dev, dest, mask, gateway);
}

__initfunc(static int
root_dev_del_route(struct device *dev, __u32 dest, __u32 mask, __u32 gateway))
{
	return root_dev_chg_route(SIOCDELRT, dev, dest, mask, gateway);
}

/*
 *  Restore the state of all devices. However, keep the root device open
 *  for the upcoming mount.
 */
__initfunc(static void root_dev_close(void))
{
	struct open_dev *openp;
	struct open_dev *nextp;

	openp = open_base;
	while (openp != NULL) {
		nextp = openp->next;
		openp->next = NULL;
		if (openp->dev != root_dev) {
			if (!(openp->old_flags & IFF_UP)) {
				dev_close(openp->dev);
			}

			openp->dev->flags = openp->old_flags;
		}
		kfree_s(openp, sizeof(struct open_dev));
		openp = nextp;
	}
}



/***************************************************************************

			      RARP Subroutines

 ***************************************************************************/

#ifdef CONFIG_RNFS_RARP

extern void arp_send(int type, int ptype, unsigned long target_ip,
		     struct device *dev, unsigned long src_ip,
		     unsigned char *dest_hw, unsigned char *src_hw,
		     unsigned char *target_hw);

static int root_rarp_recv(struct sk_buff *skb, struct device *dev,
			  struct packet_type *pt);


static struct packet_type rarp_packet_type __initdata = {
	0,			/* Should be: __constant_htons(ETH_P_RARP)
				 * - but this _doesn't_ come out constant! */
	NULL,			/* Listen to all devices */
	root_rarp_recv,
	NULL,
	NULL
};


/*
 *  Register the packet type for RARP
 */
__initfunc(static void root_rarp_open(void))
{
	rarp_packet_type.type = htons(ETH_P_RARP);
	dev_add_pack(&rarp_packet_type);
}


/*
 *  Deregister the RARP packet type
 */
__initfunc(static void root_rarp_close(void))
{
	rarp_packet_type.type = htons(ETH_P_RARP);
	dev_remove_pack(&rarp_packet_type);
}


/*
 *  Receive RARP packets.
 */
__initfunc(static int
root_rarp_recv(struct sk_buff *skb, struct device *dev, struct packet_type *pt))
{
	struct arphdr *rarp = (struct arphdr *)skb->h.raw;
	unsigned char *rarp_ptr = (unsigned char *) (rarp + 1);
	unsigned long sip, tip;
	unsigned char *sha, *tha;		/* s for "source", t for "target" */

	/* If this test doesn't pass, it's not IP, or we should ignore it anyway */
	if (rarp->ar_hln != dev->addr_len || dev->type != ntohs(rarp->ar_hrd)) {
		kfree_skb(skb, FREE_READ);
		return 0;
	}

	/* If it's not a RARP reply, delete it. */
	if (rarp->ar_op != htons(ARPOP_RREPLY)) {
		kfree_skb(skb, FREE_READ);
		return 0;
	}

	/* If it's not ethernet or AX25, delete it. */
	if ((rarp->ar_pro != htons(ETH_P_IP) && dev->type != ARPHRD_AX25) ||
#ifdef CONFIG_AX25
	   (rarp->ar_pro != htons(AX25_P_IP) && dev->type == ARPHRD_AX25) ||
#endif
	    rarp->ar_pln != 4) {
		kfree_skb(skb, FREE_READ);
		return 0;
	}

	/* Extract variable width fields */
	sha = rarp_ptr;
	rarp_ptr += dev->addr_len;
	memcpy(&sip, rarp_ptr, 4);
	rarp_ptr += 4;
	tha = rarp_ptr;
	rarp_ptr += dev->addr_len;
	memcpy(&tip, rarp_ptr, 4);

	/* Discard packets which are not meant for us. */
	if (memcmp(tha, dev->dev_addr, dev->addr_len)) {
		kfree_skb(skb, FREE_READ);
		return 0;
	}
	/* Discard packets which are not from specified server. */
	if (rarp_flag && !bootp_flag &&
	    rarp_serv != INADDR_NONE &&
	    rarp_serv != sip) {
		kfree_skb(skb, FREE_READ);
		return 0;
	}

	/*
	 * The packet is what we were looking for. Setup the global
	 * variables.
	 */
	cli();
	if (pkt_arrived) {
		sti();
		kfree_skb(skb, FREE_READ);
		return 0;
	}
	pkt_arrived = ARRIVED_RARP;
	sti();
	root_dev = dev;

	if (myaddr == INADDR_NONE)
		myaddr = tip;
	if (servaddr == INADDR_NONE)
		servaddr = sip;
	kfree_skb(skb, FREE_READ);
	return 0;
}


/*
 *  Send RARP request packet over all devices which allow RARP.
 */
__initfunc(static void root_rarp_send(void))
{
	struct open_dev *openp;
	struct device *dev;
	int num = 0;

	for (openp = open_base; openp != NULL; openp = openp->next) {
		dev = openp->dev;
		if (!(dev->flags & IFF_NOARP)) {
			arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
				 dev->dev_addr, dev->dev_addr);
			num++;
		}
	}
}
#endif



/***************************************************************************

			     BOOTP Subroutines

 ***************************************************************************/

#ifdef CONFIG_RNFS_BOOTP

static struct device *bootp_dev __initdata = NULL;	/* Device selected as best BOOTP target */

static struct socket *bootp_xmit_sock __initdata = NULL;/* BOOTP send socket */
static struct socket *bootp_recv_sock __initdata = NULL;/* BOOTP receive socket */

struct bootp_pkt {		/* BOOTP packet format */
	u8 op;			/* 1=request, 2=reply */
	u8 htype;		/* HW address type */
	u8 hlen;		/* HW address length */
	u8 hops;		/* Used only by gateways */
	u32 xid;		/* Transaction ID */
	u16 secs;		/* Seconds since we started */
	u16 flags;		/* Just what is says */
	u32 client_ip;		/* Client's IP address if known */
	u32 your_ip;		/* Assigned IP address */
	u32 server_ip;		/* Server's IP address */
	u32 relay_ip;		/* IP address of BOOTP relay */
	u8 hw_addr[16];		/* Client's HW address */
	u8 serv_name[64];	/* Server host name */
	u8 boot_file[128];	/* Name of boot file */
	u8 vendor_area[128];	/* Area for extensions */
};

#define BOOTP_REQUEST 1
#define BOOTP_REPLY 2

static struct bootp_pkt *xmit_bootp __initdata = NULL;	/* Packet being transmitted */
static struct bootp_pkt *recv_bootp __initdata = NULL;	/* Packet being received */

static int bootp_have_route __initdata = 0;	/* BOOTP route installed */


/*
 *  Free BOOTP packet buffers
 */
__initfunc(static void root_free_bootp(void))
{
	if (xmit_bootp) {
		kfree_s(xmit_bootp, sizeof(struct bootp_pkt));
		xmit_bootp = NULL;
	}
	if (recv_bootp) {
		kfree_s(recv_bootp, sizeof(struct bootp_pkt));
		recv_bootp = NULL;
	}
}


/*
 *  Allocate memory for BOOTP packet buffers
 */
static inline int root_alloc_bootp(void)
{
	if (!(xmit_bootp = kmalloc(sizeof(struct bootp_pkt), GFP_KERNEL)) ||
	    !(recv_bootp = kmalloc(sizeof(struct bootp_pkt), GFP_KERNEL))) {
		printk(KERN_ERR "BOOTP: Out of memory!\n");
		return -1;
	}
	return 0;
}


/*
 *  Create default route for BOOTP sending
 */
__initfunc(static int root_add_bootp_route(void))
{
	if (root_dev_add_route(bootp_dev, 0, 0, 0) < 0) {
		printk(KERN_ERR "BOOTP: Failed to add route\n");
		return -1;
	}
	bootp_have_route = 1;
	return 0;
}


/*
 *  Delete default route for BOOTP sending
 */
__initfunc(static int root_del_bootp_route(void))
{
	if (bootp_have_route && root_dev_del_route(bootp_dev, 0, 0, 0) < 0) {
		printk(KERN_ERR "BOOTP: Deleting of route failed!\n");
		return -1;
	}
	bootp_have_route = 0;
	return 0;
}


/*
 *  Open UDP socket.
 */
__initfunc(static int root_open_udp_sock(struct socket **sock))
{
	int	err;

	if ((err = sock_create(AF_INET, SOCK_DGRAM, IPPROTO_UDP, sock)) < 0)
		printk(KERN_ERR "BOOTP: Cannot open UDP socket!\n");
	return err;
}


/*
 *  Connect UDP socket.
 */
__initfunc(static int
root_connect_udp_sock(struct socket *sock, u32 addr, u16 port))
{
	struct sockaddr_in sa;
	int result;

	set_sockaddr(&sa, htonl(addr), htons(port));
	result = sock->ops->connect(sock, (struct sockaddr *) &sa, sizeof(sa), 0);
	if (result < 0) {
		printk(KERN_ERR "BOOTP: connect() failed\n");
		return -1;
	}
	return 0;
}


/*
 *  Bind UDP socket.
 */
__initfunc(static int
root_bind_udp_sock(struct socket *sock, u32 addr, u16 port))
{
	struct sockaddr_in sa;
	int result;

	set_sockaddr(&sa, htonl(addr), htons(port));
	result = sock->ops->bind(sock, (struct sockaddr *) &sa, sizeof(sa));
	if (result < 0) {
		printk(KERN_ERR "BOOTP: bind() failed\n");
		return -1;
	}
	return 0;
}


/*
 *  Send UDP packet.
 */
static inline int root_send_udp(struct socket *sock, void *buf, int size)
{
	mm_segment_t oldfs;
	int result;
	struct msghdr msg;
	struct iovec iov;

	oldfs = get_fs();
	set_fs(get_ds());
	iov.iov_base = buf;
	iov.iov_len = size;
	memset(&msg, 0, sizeof(msg));
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	result = sock_sendmsg(sock, &msg, size);
	set_fs(oldfs);

	return (result != size);
}


/*
 *  Try to receive UDP packet.
 */
static inline int root_recv_udp(struct socket *sock, void *buf, int size)
{
	mm_segment_t oldfs;
	int result;
	struct msghdr msg;
	struct iovec iov;

	oldfs = get_fs();
	set_fs(get_ds());
	iov.iov_base = buf;
	iov.iov_len = size;
	memset(&msg, 0, sizeof(msg));
	msg.msg_flags = MSG_DONTWAIT;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	result = sock_recvmsg(sock, &msg, size, MSG_DONTWAIT);
	set_fs(oldfs);
	return result;
}


/*
 *  Initialize BOOTP extension fields in the request.
 */
__initfunc(static void root_bootp_init_ext(u8 *e))
{
	*e++ = 99;		/* RFC1048 Magic Cookie */
	*e++ = 130;
	*e++ = 83;
	*e++ = 99;
	*e++ = 1;		/* Subnet mask request */
	*e++ = 4;
	e += 4;
	*e++ = 3;		/* Default gateway request */
	*e++ = 4;
	e += 4;
	*e++ = 12;		/* Host name request */
	*e++ = 32;
	e += 32;
	*e++ = 40;		/* NIS Domain name request */
	*e++ = 32;
	e += 32;
	*e++ = 17;		/* Boot path */
	*e++ = 32;
	e += 32;
	*e = 255;		/* End of the list */
}


/*
 *  Deinitialize the BOOTP mechanism.
 */
__initfunc(static void root_bootp_close(void))
{
	if (bootp_xmit_sock)
		sock_release(bootp_xmit_sock);
	if (bootp_recv_sock)
		sock_release(bootp_recv_sock);
	root_del_bootp_route();
	root_free_bootp();
}


/*
 *  Initialize the BOOTP mechanism.
 */
__initfunc(static int root_bootp_open(void))
{
	struct open_dev *openp;
	struct device *dev, *best_dev;

	/*
	 * Select the best interface for BOOTP. We try to select a first
	 * Ethernet-like interface. It's shame I know no simple way how to send
	 * BOOTP's to all interfaces, but it doesn't apply to usual diskless
	 * stations as they don't have multiple interfaces.
	 */

	best_dev = NULL;
	for (openp = open_base; openp != NULL; openp = openp->next) {
		dev = openp->dev;
		if (dev->flags & IFF_BROADCAST) {
			if (!best_dev ||
			   ((best_dev->flags & IFF_NOARP) && !(dev->flags & IFF_NOARP)))
				best_dev = dev;
			}
		}

	if (!best_dev) {
		printk(KERN_ERR "BOOTP: This cannot happen!\n");
		return -1;
	}
	bootp_dev = best_dev;

	/* Allocate memory for BOOTP packets */
	if (root_alloc_bootp())
		return -1;

	/* Construct BOOTP request */
	memset(xmit_bootp, 0, sizeof(struct bootp_pkt));
	xmit_bootp->op = BOOTP_REQUEST;
	get_random_bytes(&xmit_bootp->xid, sizeof(xmit_bootp->xid));
	xmit_bootp->htype = best_dev->type;
	xmit_bootp->hlen = best_dev->addr_len;
	memcpy(xmit_bootp->hw_addr, best_dev->dev_addr, best_dev->addr_len);
	root_bootp_init_ext(xmit_bootp->vendor_area);

#ifdef NFSROOT_BOOTP_DEBUG
	{
		int x;
		printk(KERN_NOTICE "BOOTP: XID=%08x, DE=%s, HT=%02x, HL=%02x, HA=",
			xmit_bootp->xid,
			best_dev->name,
			xmit_bootp->htype,
			xmit_bootp->hlen);
		for(x=0; x<xmit_bootp->hlen; x++)
			printk("%02x", xmit_bootp->hw_addr[x]);
		printk("\n");
	}
#endif

	/* Create default route to that interface */
	if (root_add_bootp_route())
		return -1;

	/* Open the sockets */
	if (root_open_udp_sock(&bootp_xmit_sock) ||
	    root_open_udp_sock(&bootp_recv_sock))
		return -1;

	/* Bind/connect the sockets */
	bootp_xmit_sock->sk->broadcast = 1;
	bootp_xmit_sock->sk->reuse = 1;
	bootp_recv_sock->sk->reuse = 1;
	if (root_bind_udp_sock(bootp_recv_sock, INADDR_ANY, 68) ||
	    root_bind_udp_sock(bootp_xmit_sock, INADDR_ANY, 68) ||
	    root_connect_udp_sock(bootp_xmit_sock, INADDR_BROADCAST, 67))
		return -1;

	return 0;
}


/*
 *  Send BOOTP request.
 */
__initfunc(static int root_bootp_send(u32 jiffies))
{
	xmit_bootp->secs = htons(jiffies / HZ);
	return root_send_udp(bootp_xmit_sock, xmit_bootp, sizeof(struct bootp_pkt));
}


/*
 *  Copy BOOTP-supplied string if not already set.
 */
__initfunc(static int
root_bootp_string(char *dest, char *src, int len, int max))
{
	if (*dest || !len)
		return 0;
	if (len > max-1)
		len = max-1;
	strncpy(dest, src, len);
	dest[len] = '\0';
	return 1;
}


/*
 *  Process BOOTP extension.
 */
__initfunc(static void root_do_bootp_ext(u8 *ext))
{
#ifdef NFSROOT_BOOTP_DEBUG
	u8 *c;

	printk(KERN_DEBUG "BOOTP: Got extension %02x",*ext);
	for(c=ext+2; c<ext+2+ext[1]; c++)
		printk(" %02x", *c);
	printk("\n");
#endif

	switch (*ext++) {
		case 1:		/* Subnet mask */
			if (netmask == INADDR_NONE)
				memcpy(&netmask, ext+1, 4);
			break;
		case 3:		/* Default gateway */
			if (gateway == INADDR_NONE)
				memcpy(&gateway, ext+1, 4);
			break;
		case 12:	/* Host name */
			root_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN);
			break;
		case 40:	/* NIS Domain name */
			root_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN);
			break;
		case 17:	/* Root path */
			root_bootp_string(nfs_path, ext+1, *ext, NFS_MAXPATHLEN);
			break;
	}
}


/*
 *  Receive BOOTP request.
 */
__initfunc(static void root_bootp_recv(void))
{
	int len;
	u8 *ext, *end, *opt;

	len = root_recv_udp(bootp_recv_sock, recv_bootp, sizeof(struct bootp_pkt));
	if (len < 0)
		return;

	/* Check consistency of incoming packet */
	if (len < 300 ||			/* See RFC 1542:2.1 */
	    recv_bootp->op != BOOTP_REPLY ||
	    recv_bootp->htype != xmit_bootp->htype ||
	    recv_bootp->hlen != xmit_bootp->hlen ||
	    recv_bootp->xid != xmit_bootp->xid) {
		dprintk("?");
		return;
		}

	/* Record BOOTP packet arrival in the global variables */
	cli();
	if (pkt_arrived) {
		sti();
		return;
	}
	pkt_arrived = ARRIVED_BOOTP;
	sti();
	root_dev = bootp_dev;

	/* Extract basic fields */
	myaddr = recv_bootp->your_ip;
	if (servaddr==INADDR_NONE)
		servaddr = recv_bootp->server_ip;

	/* Parse extensions */
	if (recv_bootp->vendor_area[0] == 99 &&	/* Check magic cookie */
	    recv_bootp->vendor_area[1] == 130 &&
	    recv_bootp->vendor_area[2] == 83 &&
	    recv_bootp->vendor_area[3] == 99) {
		ext = &recv_bootp->vendor_area[4];
		end = (u8 *) recv_bootp + len;
		while (ext < end && *ext != 255) {
			if (*ext == 0)		/* Padding */
				ext++;
			else {
				opt = ext;
				ext += ext[1] + 2;
				if (ext <= end)
					root_do_bootp_ext(opt);
			}
		}
	}
}
#endif



/***************************************************************************

			Dynamic configuration of IP.

 ***************************************************************************/

#ifdef CONFIG_RNFS_DYNAMIC

/*
 *  Determine client and server IP numbers and appropriate device by using
 *  the RARP and BOOTP protocols.
 */
__initfunc(static int root_auto_config(void))
{
	int retries;
	unsigned long timeout, jiff;
	unsigned long start_jiffies;

	/*
	 * If neither BOOTP nor RARP was selected, return with an error. This
	 * routine gets only called when some pieces of information are mis-
	 * sing, and without BOOTP and RARP we are not able to get that in-
	 * formation.
	 */
	if (!bootp_flag && !rarp_flag) {
		printk(KERN_ERR "Root-NFS: Neither RARP nor BOOTP selected.\n");
		return -1;
	}

#ifdef CONFIG_RNFS_BOOTP
	if (bootp_flag && !bootp_dev_count) {
		printk(KERN_ERR "Root-NFS: No suitable device for BOOTP found.\n");
		bootp_flag = 0;
	}
#else
	bootp_flag = 0;
#endif

#ifdef CONFIG_RNFS_RARP
	if (rarp_flag && !rarp_dev_count) {
		printk(KERN_ERR "Root-NFS: No suitable device for RARP found.\n");
		rarp_flag = 0;
	}
#else
	rarp_flag = 0;
#endif

	if (!bootp_flag && !rarp_flag)
		/* Error message already printed */
		return -1;

	/*
	 * Setup RARP and BOOTP protocols
	 */
#ifdef CONFIG_RNFS_RARP
	if (rarp_flag)
		root_rarp_open();
#endif
#ifdef CONFIG_RNFS_BOOTP
	if (bootp_flag && root_bootp_open() < 0) {
		root_bootp_close();
		return -1;
	}
#endif

	/*
	 * Send requests and wait, until we get an answer. This loop
	 * seems to be a terrible waste of CPU time, but actually there is
	 * only one process running at all, so we don't need to use any
	 * scheduler functions.
	 * [Actually we could now, but the nothing else running note still 
	 *  applies.. - AC]
	 */
	printk(KERN_NOTICE "Sending %s%s%s requests...",
		bootp_flag ? "BOOTP" : "",
		bootp_flag && rarp_flag ? " and " : "",
		rarp_flag ? "RARP" : "");
	start_jiffies = jiffies;
	retries = CONF_RETRIES;
	get_random_bytes(&timeout, sizeof(timeout));
	timeout = CONF_BASE_TIMEOUT + (timeout % (unsigned) CONF_TIMEOUT_RANDOM);
	for(;;) {
#ifdef CONFIG_RNFS_BOOTP
		if (bootp_flag && root_bootp_send(jiffies - start_jiffies) < 0) {
			printk(" BOOTP failed!\n");
			root_bootp_close();
			bootp_flag = 0;
			if (!rarp_flag)
				break;
		}
#endif
#ifdef CONFIG_RNFS_RARP
		if (rarp_flag)
			root_rarp_send();
#endif
		printk(".");
		jiff = jiffies + timeout;
		while (jiffies < jiff && !pkt_arrived)
#ifdef CONFIG_RNFS_BOOTP
			root_bootp_recv();
#else
			;
#endif
		if (pkt_arrived) {
			printk(" OK\n");
			break;
		}
		if (! --retries) {
			printk(" timed out!\n");
			break;
		}
		timeout = timeout CONF_TIMEOUT_MULT;
		if (timeout > CONF_TIMEOUT_MAX)
			timeout = CONF_TIMEOUT_MAX;
	}

#ifdef CONFIG_RNFS_RARP
	if (rarp_flag)
		root_rarp_close();
#endif
#ifdef CONFIG_RNFS_BOOTP
	if (bootp_flag)
		root_bootp_close();
#endif

	if (!pkt_arrived)
		return -1;

	printk(KERN_NOTICE "Root-NFS: Got %s answer from %s, ",
		(pkt_arrived == ARRIVED_BOOTP) ? "BOOTP" : "RARP",
		in_ntoa(servaddr));
	printk("my address is %s\n", in_ntoa(myaddr));

	return 0;
}
#endif

/* Get default netmask - used to be exported from net/ipv4 */
static inline unsigned long
ip_get_mask(unsigned long addr)
{
	if (!addr)
		return 0;
	addr = ntohl(addr);
	if (IN_CLASSA(addr))
		return htonl(IN_CLASSA_NET);
	if (IN_CLASSB(addr))
		return htonl(IN_CLASSB_NET);
	if (IN_CLASSC(addr))
		return htonl(IN_CLASSC_NET);
	return 0;
}

/***************************************************************************

			     Parsing of options

 ***************************************************************************/


/*
 *  The following integer options are recognized
 */
static struct nfs_int_opts {
	char *name;
	int  *val;
} root_int_opts[] __initdata = {
	{ "port",	&nfs_port },
	{ "rsize",	&nfs_data.rsize },
	{ "wsize",	&nfs_data.wsize },
	{ "timeo",	&nfs_data.timeo },
	{ "retrans",	&nfs_data.retrans },
	{ "acregmin",	&nfs_data.acregmin },
	{ "acregmax",	&nfs_data.acregmax },
	{ "acdirmin",	&nfs_data.acdirmin },
	{ "acdirmax",	&nfs_data.acdirmax },
	{ NULL,		NULL }
};


/*
 *  And now the flag options
 */
static struct nfs_bool_opts {
	char *name;
	int  and_mask;
	int  or_mask;
} root_bool_opts[] __initdata = {
	{ "soft",	~NFS_MOUNT_SOFT,	NFS_MOUNT_SOFT },
	{ "hard",	~NFS_MOUNT_SOFT,	0 },
	{ "intr",	~NFS_MOUNT_INTR,	NFS_MOUNT_INTR },
	{ "nointr",	~NFS_MOUNT_INTR,	0 },
	{ "posix",	~NFS_MOUNT_POSIX,	NFS_MOUNT_POSIX },
	{ "noposix",	~NFS_MOUNT_POSIX,	0 },
	{ "cto",	~NFS_MOUNT_NOCTO,	0 },
	{ "nocto",	~NFS_MOUNT_NOCTO,	NFS_MOUNT_NOCTO },
	{ "ac",		~NFS_MOUNT_NOAC,	0 },
	{ "noac",	~NFS_MOUNT_NOAC,	NFS_MOUNT_NOAC },
	{ NULL,		0,			0 }
};


/*
 *  Prepare the NFS data structure and parse any options. This tries to
 *  set as many values in the nfs_data structure as known right now.
 */
__initfunc(static int root_nfs_name(char *name))
{
	char buf[NFS_MAXPATHLEN];
	char *cp, *cq, *options, *val;
	int octets = 0;

	/* It is possible to override the server IP number here */
	cp = cq = name;
	while (octets < 4) {
		while (*cp >= '0' && *cp <= '9')
			cp++;
		if (cp == cq || cp - cq > 3)
			break;
		if (*cp == '.' || octets == 3)
			octets++;
		if (octets < 4)
			cp++;
		cq = cp;
	}
	if (octets == 4 && (*cp == ':' || *cp == '\0')) {
		if (*cp == ':')
			*cp++ = '\0';
		servaddr = in_aton(name);
		name = cp;
	}

	/* Clear the nfs_data structure and setup the server hostname */
	memset(&nfs_data, 0, sizeof(nfs_data));
	strncpy(nfs_data.hostname, in_ntoa(servaddr),
						sizeof(nfs_data.hostname)-1);
	nfs_data.namlen = strlen(nfs_data.hostname);

	/* Set the name of the directory to mount */
	if (nfs_path[0] == '\0' || strncmp(name, "default", 7))
		strncpy(buf, name, NFS_MAXPATHLEN);
	else
		strncpy(buf, nfs_path, NFS_MAXPATHLEN);
	if ((options = strchr(buf, ',')))
		*options++ = '\0';
	if (!strcmp(buf, "default"))
		strcpy(buf, NFS_ROOT);
	cp = in_ntoa(myaddr);
	if (strlen(buf) + strlen(cp) > NFS_MAXPATHLEN) {
		printk(KERN_ERR "Root-NFS: Pathname for remote directory too long.\n");
		return -1;
	}
	/* update nfs_path with path from nfsroot=... command line parameter */
	if (*buf)
		sprintf(nfs_path, buf, cp);

	/* Set some default values */
	nfs_port          = -1;
	nfs_data.version  = NFS_MOUNT_VERSION;
	nfs_data.flags    = NFS_MOUNT_NONLM;	/* No lockd in nfs root yet */
	nfs_data.rsize    = NFS_DEF_FILE_IO_BUFFER_SIZE;
	nfs_data.wsize    = NFS_DEF_FILE_IO_BUFFER_SIZE;
	nfs_data.bsize	  = 0;
	nfs_data.timeo    = 7;
	nfs_data.retrans  = 3;
	nfs_data.acregmin = 3;
	nfs_data.acregmax = 60;
	nfs_data.acdirmin = 30;
	nfs_data.acdirmax = 60;

	/* Process any options */
	if (options) {
		cp = strtok(options, ",");
		while (cp) {
			if ((val = strchr(cp, '='))) {
				struct nfs_int_opts *opts = root_int_opts;
				*val++ = '\0';
				while (opts->name && strcmp(opts->name, cp))
					opts++;
				if (opts->name)
					*(opts->val) = (int) simple_strtoul(val, NULL, 10);
			} else {
				struct nfs_bool_opts *opts = root_bool_opts;
				while (opts->name && strcmp(opts->name, cp))
					opts++;
				if (opts->name) {
					nfs_data.flags &= opts->and_mask;
					nfs_data.flags |= opts->or_mask;
				}
			}
			cp = strtok(NULL, ",");
		}
	}
	return 0;
}


/*
 *  Tell the user what's going on.
 */
#ifdef NFSROOT_DEBUG
__initfunc(static void root_nfs_print(void))
{
#define IN_NTOA(x) (((x) == INADDR_NONE) ? "none" : in_ntoa(x))

	printk(KERN_NOTICE "Root-NFS: IP config: dev=%s, ",
		root_dev ? root_dev->name : "none");
	printk("local=%s, ", IN_NTOA(myaddr));
	printk("server=%s, ", IN_NTOA(servaddr));
	printk("gw=%s, ", IN_NTOA(gateway));
	printk("mask=%s, ", IN_NTOA(netmask));
	printk("host=%s, domain=%s\n",
		system_utsname.nodename[0] ? system_utsname.nodename : "none",
		system_utsname.domainname[0] ? system_utsname.domainname : "none");
	printk(KERN_NOTICE "Root-NFS: Mounting %s on server %s as root\n",
		nfs_path, nfs_data.hostname);
	printk(KERN_NOTICE "Root-NFS:     rsize = %d, wsize = %d, timeo = %d, retrans = %d\n",
		nfs_data.rsize, nfs_data.wsize, nfs_data.timeo, nfs_data.retrans);
	printk(KERN_NOTICE "Root-NFS:     acreg (min,max) = (%d,%d), acdir (min,max) = (%d,%d)\n",
		nfs_data.acregmin, nfs_data.acregmax,
		nfs_data.acdirmin, nfs_data.acdirmax);
	printk(KERN_NOTICE "Root-NFS:     port = %d, flags = %08x\n",
		nfs_port, nfs_data.flags);

#undef IN_NTOA
}
#endif


/*
 *  Decode any IP configuration options in the "nfsaddrs" kernel command
 *  line parameter. It consists of option fields separated by colons in
 *  the following order:
 *
 *  <client-ip>:<server-ip>:<gw-ip>:<netmask>:<host name>:<device>:<bootp|rarp>
 *
 *  Any of the fields can be empty which means to use a default value:
 *	<client-ip>	- address given by BOOTP or RARP
 *	<server-ip>	- address of host returning BOOTP or RARP packet
 *	<gw-ip>		- none, or the address returned by BOOTP
 *	<netmask>	- automatically determined from <client-ip>, or the
 *			  one returned by BOOTP
 *	<host name>	- <client-ip> in ASCII notation, or the name returned
 *			  by BOOTP
 *	<device>	- use all available devices for RARP and the first
 *			  one for BOOTP
 *	<bootp|rarp>	- use both protocols to determine my own address
 */
__initfunc(static void root_nfs_addrs(char *addrs))
{
	char *cp, *ip, *dp;
	int num = 0;

	/* Clear all addresses and strings */
	myaddr = servaddr = rarp_serv = gateway = netmask = INADDR_NONE;
	system_utsname.nodename[0] = '\0';
	system_utsname.domainname[0] = '\0';
	user_dev_name[0] = '\0';
	bootp_flag = rarp_flag = 1;

	/* The following is just a shortcut for automatic IP configuration */
	if (!strcmp(addrs, "bootp")) {
		rarp_flag = 0;
		return;
	} else if (!strcmp(addrs, "rarp")) {
		bootp_flag = 0;
		return;
	} else if (!strcmp(addrs, "both")) {
		return;
	}

	/* Parse the whole string */
	ip = addrs;
	while (ip && *ip) {
		if ((cp = strchr(ip, ':')))
			*cp++ = '\0';
		if (strlen(ip) > 0) {
			dprintk("Root-NFS: Config string num %d is \"%s\"\n",
								num, ip);
			switch (num) {
			case 0:
				if ((myaddr = in_aton(ip)) == INADDR_ANY)
					myaddr = INADDR_NONE;
				break;
			case 1:
				if ((servaddr = in_aton(ip)) == INADDR_ANY)
					servaddr = INADDR_NONE;
				break;
			case 2:
				if ((gateway = in_aton(ip)) == INADDR_ANY)
					gateway = INADDR_NONE;
				break;
			case 3:
				if ((netmask = in_aton(ip)) == INADDR_ANY)
					netmask = INADDR_NONE;
				break;
			case 4:
				if ((dp = strchr(ip, '.'))) {
					*dp++ = '\0';
					strncpy(system_utsname.domainname, dp, __NEW_UTS_LEN);
					system_utsname.domainname[__NEW_UTS_LEN] = '\0';
				}
				strncpy(system_utsname.nodename, ip, __NEW_UTS_LEN);
				system_utsname.nodename[__NEW_UTS_LEN] = '\0';
				break;
			case 5:
				strncpy(user_dev_name, ip, IFNAMSIZ);
				user_dev_name[IFNAMSIZ-1] = '\0';
				break;
			case 6:
				if (!strcmp(ip, "rarp"))
					bootp_flag = 0;
				else if (!strcmp(ip, "bootp"))
					rarp_flag = 0;
				else if (strcmp(ip, "both"))
					bootp_flag = rarp_flag = 0;
				break;
			default:
				break;
			}
		}
		ip = cp;
		num++;
	}
	rarp_serv = servaddr;
}


/*
 *  Set the interface address and configure a route to the server.
 */
__initfunc(static int root_nfs_setup(void))
{
	/* Set the default system name in case none was previously found */
	if (!system_utsname.nodename[0]) {
		strncpy(system_utsname.nodename, in_ntoa(myaddr), __NEW_UTS_LEN);
		system_utsname.nodename[__NEW_UTS_LEN] = '\0';
	}

	/* Set the correct netmask */
	if (netmask == INADDR_NONE)
		netmask = ip_get_mask(myaddr);

	/* Setup the device correctly */
	root_dev->family     = AF_INET;
	root_dev->pa_addr    = myaddr;
	root_dev->pa_mask    = netmask;
	root_dev->pa_brdaddr = root_dev->pa_addr | ~root_dev->pa_mask;
	root_dev->pa_dstaddr = 0;

	/* Sticky situation, but it has a solution.  We opened it earlier,
	 * but before we knew what pa_addr etc. to give to it, thus the
	 * routing code did not add a RTF_LOCAL route for it (how could
	 * it?) so we send the pseudo device state change event now.  -DaveM
	 */
	ip_rt_event(NETDEV_CHANGE, root_dev);

	/*
	 * Now add a route to the server. If there is no gateway given,
	 * the server is on the same subnet, so we establish only a route to
	 * the local network. Otherwise we create a route to the gateway (the
	 * same local network router as in the former case) and then setup a
	 * gatewayed default route. Note that this gives sufficient network
	 * setup even for full system operation in all common cases.
	 */
	if (root_dev_add_route(root_dev, myaddr, netmask, 0))
	{
		printk(KERN_ERR "Root-NFS: Adding of local route failed!\n");
		return -1;
	}

	if (gateway != INADDR_NONE) {	/* Default route */
		if (root_dev_add_route(root_dev, INADDR_ANY, INADDR_ANY, gateway)) {
			printk(KERN_ERR "Root-NFS: Adding of default route failed!\n");
			return -1;
		}
	} else if ((servaddr ^ myaddr) & netmask) {
		printk(KERN_ERR "Root-NFS: Boot server not on local network and no default gateway configured!\n");
		return -1;
	}

	return 0;
}


/*
 *  Get the necessary IP addresses and prepare for mounting the required
 *  NFS filesystem.
 */
__initfunc(int nfs_root_init(char *nfsname, char *nfsaddrs))
{
#ifdef NFSROOT_DEBUG
	nfs_debug |= NFSDBG_ROOT;
#endif

	/*
	 * Decode IP addresses and other configuration info contained
	 * in the nfsaddrs string (which came from the kernel command
	 * line).
	 */
	root_nfs_addrs(nfsaddrs);

	/*
	 * Setup all network devices
	 */
	if (root_dev_open() < 0)
		return -1;

	/*
	 * If the config information is insufficient (e.g., our IP address or
	 * IP address of the boot server is missing or we have multiple network
	 * interfaces and no default was set), use BOOTP or RARP to get the
	 * missing values.
	 *
	 * Note that we don't try to set up correct routes for multiple
	 * interfaces (could be solved by trying icmp echo requests), because
	 * it's only necessary in the rare case of multiple ethernet devices
	 * in the (diskless) system and if the server is on another subnet.
	 * If only one interface is installed, the routing is obvious.
	 */
	if ((myaddr == INADDR_NONE ||
	     servaddr == INADDR_NONE ||
	     (open_base != NULL && open_base->next != NULL))
#ifdef CONFIG_RNFS_DYNAMIC
		&& root_auto_config() < 0
#endif
	   ) {
		root_dev_close();
		return -1;
	}
	if (root_dev == NULL) {
		if (open_base != NULL && open_base->next == NULL) {
			root_dev = open_base->dev;
		} else {
			printk(KERN_ERR "Root-NFS: Multiple devices and no server\n");
			root_dev_close();
			return -1;
		}
	}

	/*
	 * Close all network devices except the device which connects to
	 * server
	 */
	root_dev_close();

	/*
	 * Decode the root directory path name and NFS options from
	 * the kernel command line. This has to go here in order to
	 * be able to use the client IP address for the remote root
	 * directory (necessary for pure RARP booting).
	 */
	if (root_nfs_name(nfsname) < 0)
		return -1;

	/*
	 * Setup devices and routes. The server directory is actually
	 * mounted after init() has been started.
	 */
	if (root_nfs_setup() < 0)
		return -1;

#ifdef NFSROOT_DEBUG
	root_nfs_print();
#endif

	return 0;
}


/***************************************************************************

	       Routines to actually mount the root directory

 ***************************************************************************/
/*
 *  Query server portmapper for the port of a daemon program
 */
__initfunc(static int root_nfs_getport(int program, int version))
{
	struct sockaddr_in	sin;

	printk(KERN_NOTICE "Looking up port of RPC %d/%d on %s\n",
		program, version, in_ntoa(servaddr));
		set_sockaddr(&sin, servaddr, 0);
		return rpc_getport_external(&sin, program, version, IPPROTO_UDP);
}


/*
 *  Get portnumbers for mountd and nfsd from server
 *  The RPC layer does support portmapper queries; the only reason to
 *  keep this code is that we may want to use fallback ports. But is there
 *  actually someone who does not run portmap?
 */
__initfunc(static int root_nfs_ports(void))
{
	int	port;

	if (nfs_port < 0) {
		if ((port = root_nfs_getport(NFS_PROGRAM, NFS_VERSION)) < 0) {
			printk(KERN_ERR "Root-NFS: Unable to get nfsd port "
					"number from server, using default\n");
			port = NFS_PORT;
		}
		nfs_port = htons(port);
		dprintk("Root-NFS: Portmapper on server returned %d "
			"as nfsd port\n", port);
	}

	if ((port = root_nfs_getport(NFS_MNT_PROGRAM, NFS_MNT_VERSION)) < 0) {
		printk(KERN_ERR "Root-NFS: Unable to get mountd port "
				"number from server, using default\n");
		port = NFS_MNT_PORT;
	}

	mount_port = htons(port);
	dprintk("Root-NFS: Portmapper on server returned %d "
		"as mountd port\n", port);

	return 0;
}


/*
 *  Get a file handle from the server for the directory which is to be
 *  mounted
 */
__initfunc(static int root_nfs_get_handle(void))
{
	struct sockaddr_in sin;
	int		status;

	set_sockaddr(&sin, servaddr, mount_port);
	status = nfs_mount(&sin, nfs_path, &nfs_data.root);
	if (status < 0)
		printk(KERN_ERR "Root-NFS: Server returned error %d "
				"while mounting %s\n", status, nfs_path);

	return status;
}


/*
 *  Now actually mount the given directory
 */
__initfunc(static int root_nfs_do_mount(struct super_block *sb))
{
	/* Pass the server address to NFS */
	set_sockaddr((struct sockaddr_in *) &nfs_data.addr, servaddr, nfs_port);

	/* Now (finally ;-)) read the super block for mounting */
	if (nfs_read_super(sb, &nfs_data, 1) == NULL)
		return -1;
	return 0;
}


/*
 *  Get the NFS port numbers and file handle, and then read the super-
 *  block for mounting.
 */
__initfunc(int nfs_root_mount(struct super_block *sb))
{
	if (root_nfs_ports() < 0)
		return -1;
	if (root_nfs_get_handle() < 0)
		return -1;
	if (root_nfs_do_mount(sb) < 0)
		return -1;
	return 0;
}