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
|
/*
* $Id: ipconfig.c,v 1.6 1998/01/09 17:19:46 mj Exp $
*
* Automatic Configuration of IP -- use BOOTP or RARP or user-supplied
* information to configure own IP address and routes.
*
* Copyright (C) 1996, 1997 Martin Mares <mj@atrey.karlin.mff.cuni.cz>
*
* Derived from network configuration code in fs/nfs/nfsroot.c,
* originally Copyright (C) 1995, 1996 Gero Kuhlmann and me.
*/
#include <linux/config.h>
#include <linux/types.h>
#include <linux/string.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/random.h>
#include <linux/init.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>
#include <linux/skbuff.h>
#include <linux/ip.h>
#include <linux/socket.h>
#include <linux/inetdevice.h>
#include <linux/route.h>
#include <net/route.h>
#include <net/sock.h>
#include <net/arp.h>
#include <net/ip_fib.h>
#include <net/ipconfig.h>
#include <asm/segment.h>
#include <asm/uaccess.h>
/* Define this to allow debugging output */
#undef IPCONFIG_DEBUG
#ifdef IPCONFIG_DEBUG
#define DBG(x) printk x
#else
#define DBG(x) do { } while(0)
#endif
/* 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 /* Rate of timeout growth */
#define CONF_TIMEOUT_MAX (HZ*30) /* Maximum allowed timeout */
/* IP configuration */
static char user_dev_name[IFNAMSIZ] __initdata = { 0, };/* Name of user-selected boot device */
u32 ic_myaddr __initdata = INADDR_NONE; /* My IP address */
u32 ic_servaddr __initdata = INADDR_NONE; /* Server IP address */
u32 ic_gateway __initdata = INADDR_NONE; /* Gateway IP address */
u32 ic_netmask __initdata = INADDR_NONE; /* Netmask for local subnet */
int ic_bootp_flag __initdata = 1; /* Use BOOTP */
int ic_rarp_flag __initdata = 1; /* Use RARP */
int ic_enable __initdata = 1; /* Automatic IP configuration enabled */
int ic_host_name_set __initdata = 0; /* Host name configured manually */
int ic_set_manually __initdata = 0; /* IPconfig parameters set manually */
u32 root_server_addr __initdata = INADDR_NONE; /* Address of boot server */
u8 root_server_path[256] __initdata = { 0, }; /* Path to mount as root */
#if defined(CONFIG_IP_PNP_BOOTP) || defined(CONFIG_IP_PNP_RARP)
#define CONFIG_IP_PNP_DYNAMIC
static int ic_got_reply __initdata = 0;
#define IC_GOT_BOOTP 1
#define IC_GOT_RARP 2
#endif
/*
* Network devices
*/
struct ic_device {
struct ic_device *next;
struct device *dev;
unsigned short flags;
};
static struct ic_device *ic_first_dev __initdata = NULL;/* List of open device */
static struct device *ic_dev __initdata = NULL; /* Selected device */
static int bootp_dev_count __initdata = 0; /* BOOTP capable devices */
static int rarp_dev_count __initdata = 0; /* RARP capable devices */
__initfunc(int ic_open_devs(void))
{
struct ic_device *d, **last;
struct device *dev;
unsigned short oflags;
last = &ic_first_dev;
for (dev = dev_base; dev; dev = dev->next)
if (dev->type < ARPHRD_SLIP &&
!(dev->flags & (IFF_LOOPBACK | IFF_POINTOPOINT)) &&
strncmp(dev->name, "dummy", 5) &&
(!user_dev_name[0] || !strcmp(dev->name, user_dev_name))) {
oflags = dev->flags;
if (dev_change_flags(dev, oflags | IFF_UP) < 0) {
printk(KERN_ERR "IP-Config: Failed to open %s\n", dev->name);
continue;
}
if (!(d = kmalloc(sizeof(struct ic_device), GFP_KERNEL)))
return -1;
d->dev = dev;
*last = d;
last = &d->next;
d->flags = oflags;
bootp_dev_count++;
if (!(dev->flags & IFF_NOARP))
rarp_dev_count++;
DBG(("IP-Config: Opened %s\n", dev->name));
}
*last = NULL;
if (!bootp_dev_count) {
if (user_dev_name[0])
printk(KERN_ERR "IP-Config: Device `%s' not found.\n", user_dev_name);
else
printk(KERN_ERR "IP-Config: No network devices available.\n");
return -1;
}
return 0;
}
__initfunc(void ic_close_devs(void))
{
struct ic_device *d, *next;
struct device *dev;
next = ic_first_dev;
while ((d = next)) {
next = d->next;
dev = d->dev;
if (dev != ic_dev) {
DBG(("IP-Config: Downing %s\n", dev->name));
dev_change_flags(dev, d->flags);
}
kfree_s(d, sizeof(struct ic_device));
}
}
/*
* Interface to various network functions.
*/
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 ic_dev_ioctl(unsigned int cmd, struct ifreq *arg))
{
int res;
mm_segment_t oldfs = get_fs();
set_fs(get_ds());
res = devinet_ioctl(cmd, arg);
set_fs(oldfs);
return res;
}
__initfunc(static int ic_route_ioctl(unsigned int cmd, struct rtentry *arg))
{
int res;
mm_segment_t oldfs = get_fs();
set_fs(get_ds());
res = ip_rt_ioctl(cmd, arg);
set_fs(oldfs);
return res;
}
/*
* Set up interface addresses and routes.
*/
__initfunc(static int ic_setup_if(void))
{
struct ifreq ir;
struct sockaddr_in *sin = (void *) &ir.ifr_ifru.ifru_addr;
int err;
memset(&ir, 0, sizeof(ir));
strcpy(ir.ifr_ifrn.ifrn_name, ic_dev->name);
set_sockaddr(sin, ic_myaddr, 0);
if ((err = ic_dev_ioctl(SIOCSIFADDR, &ir)) < 0) {
printk(KERN_ERR "IP-Config: Unable to set interface address (%d).\n", err);
return -1;
}
set_sockaddr(sin, ic_netmask, 0);
if ((err = ic_dev_ioctl(SIOCSIFNETMASK, &ir)) < 0) {
printk(KERN_ERR "IP-Config: Unable to set interface netmask (%d).\n", err);
return -1;
}
set_sockaddr(sin, ic_myaddr | ~ic_netmask, 0);
if ((err = ic_dev_ioctl(SIOCSIFBRDADDR, &ir)) < 0) {
printk(KERN_ERR "IP-Config: Unable to set interface broadcast address (%d).\n", err);
return -1;
}
return 0;
}
__initfunc(int ic_setup_routes(void))
{
/* No need to setup device routes, only the default route... */
if (ic_gateway != INADDR_NONE) {
struct rtentry rm;
int err;
memset(&rm, 0, sizeof(rm));
if ((ic_gateway ^ ic_myaddr) & ic_netmask) {
printk(KERN_ERR "IP-Config: Gateway not on directly connected network.\n");
return -1;
}
set_sockaddr((struct sockaddr_in *) &rm.rt_dst, 0, 0);
set_sockaddr((struct sockaddr_in *) &rm.rt_genmask, 0, 0);
set_sockaddr((struct sockaddr_in *) &rm.rt_gateway, ic_gateway, 0);
rm.rt_flags = RTF_UP | RTF_GATEWAY;
if ((err = ic_route_ioctl(SIOCADDRT, &rm)) < 0) {
printk(KERN_ERR "IP-Config: Cannot add default route (%d).\n", err);
return -1;
}
}
return 0;
}
/*
* Fill in default values for all missing parameters.
*/
__initfunc(int ic_defaults(void))
{
if (!ic_host_name_set)
strcpy(system_utsname.nodename, in_ntoa(ic_myaddr));
if (root_server_addr == INADDR_NONE)
root_server_addr = ic_servaddr;
if (ic_netmask == INADDR_NONE) {
if (IN_CLASSA(ic_myaddr))
ic_netmask = IN_CLASSA_NET;
else if (IN_CLASSB(ic_myaddr))
ic_netmask = IN_CLASSB_NET;
else if (IN_CLASSC(ic_myaddr))
ic_netmask = IN_CLASSC_NET;
else {
printk(KERN_ERR "IP-Config: Unable to guess netmask for address %08x\n", ic_myaddr);
return -1;
}
}
return 0;
}
/*
* RARP support.
*/
#ifdef CONFIG_IP_PNP_RARP
static int ic_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 */
ic_rarp_recv,
NULL,
NULL
};
__initfunc(static void ic_rarp_init(void))
{
rarp_packet_type.type = htons(ETH_P_RARP);
dev_add_pack(&rarp_packet_type);
}
__initfunc(static void ic_rarp_cleanup(void))
{
dev_remove_pack(&rarp_packet_type);
}
/*
* Process received RARP packet.
*/
__initfunc(static int
ic_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))
goto drop;
/* If it's not a RARP reply, delete it. */
if (rarp->ar_op != htons(ARPOP_RREPLY))
goto drop;
/* If it's not ethernet, delete it. */
if (rarp->ar_pro != htons(ETH_P_IP))
goto drop;
/* 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))
goto drop;
/* Discard packets which are not from specified server. */
if (ic_servaddr != INADDR_NONE && ic_servaddr != sip)
goto drop;
/* Victory! The packet is what we were looking for! */
if (!ic_got_reply) {
ic_got_reply = IC_GOT_RARP;
ic_dev = dev;
if (ic_myaddr == INADDR_NONE)
ic_myaddr = tip;
ic_servaddr = sip;
}
/* And throw the packet out... */
drop:
kfree_skb(skb);
return 0;
}
/*
* Send RARP request packet over all devices which allow RARP.
*/
__initfunc(static void ic_rarp_send(void))
{
struct ic_device *d;
for (d=ic_first_dev; d; d=d->next) {
struct device *dev = d->dev;
if (!(dev->flags & IFF_NOARP))
arp_send(ARPOP_RREQUEST, ETH_P_RARP, 0, dev, 0, NULL,
dev->dev_addr, dev->dev_addr);
}
}
#endif
/*
* BOOTP support.
*/
#ifdef CONFIG_IP_PNP_BOOTP
static struct socket *ic_bootp_xmit_sock __initdata = NULL; /* BOOTP send socket */
static struct socket *ic_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 it 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 *ic_xmit_bootp __initdata = NULL; /* Packet being transmitted */
static struct bootp_pkt *ic_recv_bootp __initdata = NULL; /* Packet being received */
/*
* Dirty tricks for BOOTP packet routing. We replace the standard lookup function
* for the local fib by our version which does fake lookups and returns our private
* fib entries. Ugly, but it seems to be the simplest way to do the job.
*/
static void *ic_old_local_lookup __initdata = NULL; /* Old local routing table lookup function */
static struct fib_info *ic_bootp_tx_fib __initdata = NULL; /* Our fake fib entries */
static struct fib_info *ic_bootp_rx_fib __initdata = NULL;
__initfunc(static int ic_bootp_route_lookup(struct fib_table *tb, const struct rt_key *key,
struct fib_result *res))
{
static u32 ic_brl_zero = 0;
DBG(("BOOTP: Route lookup: %d:%08x -> %d:%08x: ", key->iif, key->src, key->oif, key->dst));
res->scope = RT_SCOPE_UNIVERSE;
res->prefix = &ic_brl_zero;
res->prefixlen = 0;
res->nh_sel = 0;
if (key->src == 0 && key->dst == 0xffffffff && key->iif == loopback_dev.ifindex) { /* Packet output */
DBG(("Output\n"));
res->type = RTN_UNICAST;
res->fi = ic_bootp_tx_fib;
} else if (key->iif && key->iif != loopback_dev.ifindex && key->oif == 0) { /* Packet input */
DBG(("Input\n"));
res->type = RTN_LOCAL;
res->fi = ic_bootp_rx_fib;
} else if (!key->iif && !key->oif && !key->src) { /* Address check by inet_addr_type() */
DBG(("Check\n"));
res->type = RTN_UNICAST;
res->fi = ic_bootp_tx_fib;
} else {
DBG(("Drop\n"));
return -EINVAL;
}
return 0;
}
__initfunc(static int ic_set_bootp_route(struct ic_device *d))
{
struct fib_info *f = ic_bootp_tx_fib;
struct fib_nh *n = &f->fib_nh[0];
n->nh_dev = d->dev;
n->nh_oif = n->nh_dev->ifindex;
rt_cache_flush(0);
return 0;
}
__initfunc(static int ic_bootp_route_init(void))
{
int size = sizeof(struct fib_info) + sizeof(struct fib_nh);
struct fib_info *rf, *tf;
struct fib_nh *nh;
if (!(rf = ic_bootp_rx_fib = kmalloc(size, GFP_KERNEL)) ||
!(tf = ic_bootp_tx_fib = kmalloc(size, GFP_KERNEL)))
return -1;
memset(rf, 0, size);
rf->fib_nhs = 1;
nh = &rf->fib_nh[0];
nh->nh_scope = RT_SCOPE_UNIVERSE;
memset(tf, 0, size);
rf->fib_nhs = 1;
nh = &rf->fib_nh[0];
nh->nh_dev = ic_first_dev->dev;
nh->nh_scope = RT_SCOPE_UNIVERSE;
nh->nh_oif = nh->nh_dev->ifindex;
/* Dirty trick: replace standard routing table lookup by our function */
ic_old_local_lookup = local_table->tb_lookup;
local_table->tb_lookup = ic_bootp_route_lookup;
return 0;
}
__initfunc(static void ic_bootp_route_cleanup(void))
{
if (ic_old_local_lookup)
local_table->tb_lookup = ic_old_local_lookup;
if (ic_bootp_rx_fib)
kfree_s(ic_bootp_rx_fib, sizeof(struct fib_info) + sizeof(struct fib_nh));
if (ic_bootp_tx_fib)
kfree_s(ic_bootp_tx_fib, sizeof(struct fib_info) + sizeof(struct fib_nh));
}
/*
* Allocation and freeing of BOOTP packet buffers.
*/
__initfunc(static int ic_bootp_alloc(void))
{
if (!(ic_xmit_bootp = kmalloc(sizeof(struct bootp_pkt), GFP_KERNEL)) ||
!(ic_recv_bootp = kmalloc(sizeof(struct bootp_pkt), GFP_KERNEL))) {
printk(KERN_ERR "BOOTP: Out of memory!\n");
return -1;
}
return 0;
}
__initfunc(static void ic_bootp_free(void))
{
if (ic_xmit_bootp) {
kfree_s(ic_xmit_bootp, sizeof(struct bootp_pkt));
ic_xmit_bootp = NULL;
}
if (ic_recv_bootp) {
kfree_s(ic_recv_bootp, sizeof(struct bootp_pkt));
ic_recv_bootp = NULL;
}
}
/*
* Add / Remove fake interface addresses for BOOTP packet sending.
*/
__initfunc(static int ic_bootp_addrs_add(void))
{
struct ic_device *d;
int err;
for(d=ic_first_dev; d; d=d->next)
if ((err = inet_add_bootp_addr(d->dev)) < 0) {
printk(KERN_ERR "BOOTP: Unable to set interface address\n");
return -1;
}
return 0;
}
__initfunc(static void ic_bootp_addrs_del(void))
{
struct ic_device *d;
for(d=ic_first_dev; d; d=d->next)
inet_del_bootp_addr(d->dev);
}
/*
* UDP socket operations.
*/
__initfunc(static int ic_udp_open(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;
}
static inline void ic_udp_close(struct socket *sock)
{
if (sock)
sock_release(sock);
}
__initfunc(static int ic_udp_connect(struct socket *sock, u32 addr, u16 port))
{
struct sockaddr_in sa;
int err;
set_sockaddr(&sa, htonl(addr), htons(port));
err = sock->ops->connect(sock, (struct sockaddr *) &sa, sizeof(sa), 0);
if (err < 0) {
printk(KERN_ERR "BOOTP: connect() failed (%d)\n", err);
return -1;
}
return 0;
}
__initfunc(static int ic_udp_bind(struct socket *sock, u32 addr, u16 port))
{
struct sockaddr_in sa;
int err;
set_sockaddr(&sa, htonl(addr), htons(port));
err = sock->ops->bind(sock, (struct sockaddr *) &sa, sizeof(sa));
if (err < 0) {
printk(KERN_ERR "BOOTP: bind() failed (%d)\n", err);
return -1;
}
return 0;
}
__initfunc(static int ic_udp_send(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);
}
__initfunc(static int ic_udp_recv(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 ic_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 */
}
/*
* Initialize the BOOTP mechanism.
*/
__initfunc(static int ic_bootp_init(void))
{
/* Allocate memory for BOOTP packets */
if (ic_bootp_alloc() < 0)
return -1;
/* Add fake zero addresses to all interfaces */
if (ic_bootp_addrs_add() < 0)
return -1;
/* Initialize BOOTP routing */
if (ic_bootp_route_init() < 0)
return -1;
/* Initialize common portion of BOOTP request */
memset(ic_xmit_bootp, 0, sizeof(struct bootp_pkt));
ic_xmit_bootp->op = BOOTP_REQUEST;
get_random_bytes(&ic_xmit_bootp->xid, sizeof(ic_xmit_bootp->xid));
ic_bootp_init_ext(ic_xmit_bootp->vendor_area);
DBG(("BOOTP: XID=%08x\n", ic_xmit_bootp->xid));
/* Open the sockets */
if (ic_udp_open(&ic_bootp_xmit_sock) ||
ic_udp_open(&ic_bootp_recv_sock))
return -1;
/* Bind/connect the sockets */
ic_bootp_xmit_sock->sk->broadcast = 1;
ic_bootp_xmit_sock->sk->reuse = 1;
ic_bootp_recv_sock->sk->reuse = 1;
ic_set_bootp_route(ic_first_dev);
if (ic_udp_bind(ic_bootp_recv_sock, INADDR_ANY, 68) ||
ic_udp_bind(ic_bootp_xmit_sock, INADDR_ANY, 68) ||
ic_udp_connect(ic_bootp_xmit_sock, INADDR_BROADCAST, 67))
return -1;
return 0;
}
/*
* BOOTP cleanup.
*/
__initfunc(static void ic_bootp_cleanup(void))
{
ic_udp_close(ic_bootp_xmit_sock);
ic_udp_close(ic_bootp_recv_sock);
ic_bootp_addrs_del();
ic_bootp_free();
ic_bootp_route_cleanup();
}
/*
* Send BOOTP request to single interface.
*/
__initfunc(static int ic_bootp_send_if(struct ic_device *d, u32 jiffies))
{
struct device *dev = d->dev;
struct bootp_pkt *b = ic_xmit_bootp;
b->htype = dev->type;
b->hlen = dev->addr_len;
memset(b->hw_addr, 0, sizeof(b->hw_addr));
memcpy(b->hw_addr, dev->dev_addr, dev->addr_len);
b->secs = htons(jiffies / HZ);
ic_set_bootp_route(d);
return ic_udp_send(ic_bootp_xmit_sock, b, sizeof(struct bootp_pkt));
}
/*
* Send BOOTP requests to all interfaces.
*/
__initfunc(static int ic_bootp_send(u32 jiffies))
{
struct ic_device *d;
for(d=ic_first_dev; d; d=d->next)
if (ic_bootp_send_if(d, jiffies) < 0)
return -1;
return 0;
}
/*
* Copy BOOTP-supplied string if not already set.
*/
__initfunc(static int ic_bootp_string(char *dest, char *src, int len, int max))
{
if (!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 ic_do_bootp_ext(u8 *ext))
{
#ifdef IPCONFIG_DEBUG
u8 *c;
printk("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 (ic_netmask == INADDR_NONE)
memcpy(&ic_netmask, ext+1, 4);
break;
case 3: /* Default gateway */
if (ic_gateway == INADDR_NONE)
memcpy(&ic_gateway, ext+1, 4);
break;
case 12: /* Host name */
ic_bootp_string(system_utsname.nodename, ext+1, *ext, __NEW_UTS_LEN);
ic_host_name_set = 1;
break;
case 40: /* NIS Domain name */
ic_bootp_string(system_utsname.domainname, ext+1, *ext, __NEW_UTS_LEN);
break;
case 17: /* Root path */
if (!root_server_path[0])
ic_bootp_string(root_server_path, ext+1, *ext, sizeof(root_server_path));
break;
}
}
/*
* Receive BOOTP request.
*/
__initfunc(static void ic_bootp_recv(void))
{
int len;
u8 *ext, *end, *opt;
struct ic_device *d;
struct bootp_pkt *b = ic_recv_bootp;
if ((len = ic_udp_recv(ic_bootp_recv_sock, b, sizeof(struct bootp_pkt))) < 0)
return;
/* Check consistency of incoming packet */
if (len < 300 || /* See RFC 1542:2.1 */
b->op != BOOTP_REPLY ||
b->xid != ic_xmit_bootp->xid) {
printk("?");
return;
}
/* Find interface this arrived from */
for(d=ic_first_dev; d; d=d->next) {
struct device *dev = d->dev;
if (b->htype == dev->type ||
b->hlen == dev->addr_len ||
!memcmp(b->hw_addr, dev->dev_addr, dev->addr_len))
break;
}
if (!d) { /* Unknown device */
printk("!");
return;
}
/* Record BOOTP packet arrival */
cli();
if (ic_got_reply) {
sti();
return;
}
ic_got_reply = IC_GOT_BOOTP;
sti();
ic_dev = d->dev;
/* Extract basic fields */
ic_myaddr = b->your_ip;
ic_servaddr = b->server_ip;
/* Parse extensions */
if (b->vendor_area[0] == 99 && /* Check magic cookie */
b->vendor_area[1] == 130 &&
b->vendor_area[2] == 83 &&
b->vendor_area[3] == 99) {
ext = &b->vendor_area[4];
end = (u8 *) b + len;
while (ext < end && *ext != 0xff) {
if (*ext == 0) /* Padding */
ext++;
else {
opt = ext;
ext += ext[1] + 2;
if (ext <= end)
ic_do_bootp_ext(opt);
}
}
}
if (ic_gateway == INADDR_NONE && b->relay_ip)
ic_gateway = b->relay_ip;
}
#endif
/*
* Dynamic IP configuration -- BOOTP and RARP.
*/
#ifdef CONFIG_IP_PNP_DYNAMIC
__initfunc(int ic_dynamic(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 (!ic_bootp_flag && !ic_rarp_flag) {
printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
return -1;
}
#ifdef CONFIG_IP_PNP_BOOTP
if (ic_bootp_flag && !bootp_dev_count) {
printk(KERN_ERR "BOOTP: No suitable device found.\n");
ic_bootp_flag = 0;
}
#else
ic_bootp_flag = 0;
#endif
#ifdef CONFIG_IP_PNP_RARP
if (ic_rarp_flag && !rarp_dev_count) {
printk(KERN_ERR "RARP: No suitable device found.\n");
ic_rarp_flag = 0;
}
#else
ic_rarp_flag = 0;
#endif
if (!ic_bootp_flag && !ic_rarp_flag)
/* Error message already printed */
return -1;
/*
* Setup RARP and BOOTP protocols
*/
#ifdef CONFIG_IP_PNP_RARP
if (ic_rarp_flag)
ic_rarp_init();
#endif
#ifdef CONFIG_IP_PNP_BOOTP
if (ic_bootp_flag && ic_bootp_init() < 0) {
ic_bootp_cleanup();
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...",
ic_bootp_flag ? "BOOTP" : "",
ic_bootp_flag && ic_rarp_flag ? " and " : "",
ic_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_IP_PNP_BOOTP
if (ic_bootp_flag && ic_bootp_send(jiffies - start_jiffies) < 0) {
printk(" BOOTP failed!\n");
ic_bootp_cleanup();
ic_bootp_flag = 0;
if (!ic_rarp_flag)
break;
}
#endif
#ifdef CONFIG_IP_PNP_RARP
if (ic_rarp_flag)
ic_rarp_send();
#endif
printk(".");
jiff = jiffies + timeout;
while (jiffies < jiff && !ic_got_reply)
#ifdef CONFIG_IP_PNP_BOOTP
if (ic_bootp_flag)
ic_bootp_recv();
#else
;
#endif
if (ic_got_reply) {
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_IP_PNP_RARP
if (ic_rarp_flag)
ic_rarp_cleanup();
#endif
#ifdef CONFIG_IP_PNP_BOOTP
if (ic_bootp_flag)
ic_bootp_cleanup();
#endif
if (!ic_got_reply)
return -1;
printk("IP-Config: Got %s answer from %s, ",
(ic_got_reply == IC_GOT_BOOTP) ? "BOOTP" : "RARP",
in_ntoa(ic_servaddr));
printk("my address is %s\n", in_ntoa(ic_myaddr));
return 0;
}
#endif
/*
* IP Autoconfig dispatcher.
*/
__initfunc(int ip_auto_config(void))
{
if (!ic_enable)
return 0;
DBG(("IP-Config: Entered.\n"));
/* Setup all network devices */
if (ic_open_devs() < 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.
*/
if (ic_myaddr == INADDR_NONE ||
#ifdef CONFIG_ROOT_NFS
root_server_addr == INADDR_NONE ||
#endif
(ic_first_dev && ic_first_dev->next)) {
#ifdef CONFIG_IP_PNP_DYNAMIC
if (ic_dynamic() < 0) {
printk(KERN_ERR "IP-Config: Auto-configuration of network failed.\n");
ic_close_devs();
return -1;
}
#else
printk(KERN_ERR "IP-Config: Incomplete network configuration information.\n");
ic_close_devs();
return -1;
#endif
} else {
ic_dev = ic_first_dev->dev; /* Device selected manually or only one device -> use it */
}
/*
* Use defaults whereever applicable.
*/
if (ic_defaults() < 0)
return -1;
/*
* Close all network devices except the device we've
* autoconfigured and set up routes.
*/
ic_close_devs();
if (ic_setup_if() < 0 || ic_setup_routes() < 0)
return -1;
DBG(("IP-Config: device=%s, local=%08x, server=%08x, boot=%08x, gw=%08x, mask=%08x\n",
ic_dev->name, ic_myaddr, ic_servaddr, root_server_addr, ic_gateway, ic_netmask));
DBG(("IP-Config: host=%s, domain=%s, path=`%s'\n", system_utsname.nodename,
system_utsname.domainname, root_server_path));
return 0;
}
/*
* Decode any IP configuration options in the "ipconfig" 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
* <bootp|rarp|both|off> - use both protocols to determine my own address
*/
__initfunc(void ip_auto_config_setup(char *addrs, int *ints))
{
char *cp, *ip, *dp;
int num = 0;
ic_set_manually = 1;
if (!strcmp(addrs, "bootp")) {
ic_rarp_flag = 0;
return;
} else if (!strcmp(addrs, "rarp")) {
ic_bootp_flag = 0;
return;
} else if (!strcmp(addrs, "both")) {
return;
} else if (!strcmp(addrs, "off")) {
ic_enable = 0;
return;
}
/* Parse the whole string */
ip = addrs;
while (ip && *ip) {
if ((cp = strchr(ip, ':')))
*cp++ = '\0';
if (strlen(ip) > 0) {
DBG(("IP-Config: Parameter #%d: `%s'\n", num, ip));
switch (num) {
case 0:
if ((ic_myaddr = in_aton(ip)) == INADDR_ANY)
ic_myaddr = INADDR_NONE;
break;
case 1:
if ((ic_servaddr = in_aton(ip)) == INADDR_ANY)
ic_servaddr = INADDR_NONE;
break;
case 2:
if ((ic_gateway = in_aton(ip)) == INADDR_ANY)
ic_gateway = INADDR_NONE;
break;
case 3:
if ((ic_netmask = in_aton(ip)) == INADDR_ANY)
ic_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';
ic_host_name_set = 1;
break;
case 5:
strncpy(user_dev_name, ip, IFNAMSIZ);
user_dev_name[IFNAMSIZ-1] = '\0';
break;
case 6:
if (!strcmp(ip, "rarp"))
ic_bootp_flag = 0;
else if (!strcmp(ip, "bootp"))
ic_rarp_flag = 0;
else if (strcmp(ip, "both"))
ic_bootp_flag = ic_rarp_flag = 0;
break;
}
}
ip = cp;
num++;
}
}
|