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#include <asm/uaccess.h>
71#include <asm/system.h>
72#include <asm/bitops.h>
73#include <linux/config.h>
74#include <linux/types.h>
75#include <linux/kernel.h>
76#include <linux/sched.h>
77#include <linux/string.h>
78#include <linux/mm.h>
79#include <linux/socket.h>
80#include <linux/sockios.h>
81#include <linux/errno.h>
82#include <linux/interrupt.h>
83#include <linux/if_ether.h>
84#include <linux/netdevice.h>
85#include <linux/etherdevice.h>
86#include <linux/notifier.h>
87#include <linux/skbuff.h>
88#include <linux/brlock.h>
89#include <net/sock.h>
90#include <linux/rtnetlink.h>
91#include <linux/proc_fs.h>
92#include <linux/stat.h>
93#include <linux/if_bridge.h>
94#include <linux/divert.h>
95#include <net/dst.h>
96#include <net/pkt_sched.h>
97#include <net/profile.h>
98#include <net/checksum.h>
99#include <linux/highmem.h>
100#include <linux/init.h>
101#include <linux/kmod.h>
102#include <linux/module.h>
103#if defined(CONFIG_NET_RADIO) || defined(CONFIG_NET_PCMCIA_RADIO)
104#include <linux/wireless.h>
105#include <net/iw_handler.h>
106#endif
107#ifdef CONFIG_PLIP
108extern int plip_init(void);
109#endif
110
111
112
113
114
115
116
117#undef RAND_LIE
118
119
120
121
122#undef OFFLINE_SAMPLE
123
124NET_PROFILE_DEFINE(dev_queue_xmit)
125NET_PROFILE_DEFINE(softnet_process)
126
127const char *if_port_text[] = {
128 "unknown",
129 "BNC",
130 "10baseT",
131 "AUI",
132 "100baseT",
133 "100baseTX",
134 "100baseFX"
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
165static struct packet_type *ptype_base[16];
166static struct packet_type *ptype_all = NULL;
167
168#ifdef OFFLINE_SAMPLE
169static void sample_queue(unsigned long dummy);
170static struct timer_list samp_timer = { function: sample_queue };
171#endif
172
173#ifdef CONFIG_HOTPLUG
174static int net_run_sbin_hotplug(struct net_device *dev, char *action);
175#else
176#define net_run_sbin_hotplug(dev, action) ({ 0; })
177#endif
178
179
180
181
182
183static struct notifier_block *netdev_chain=NULL;
184
185
186
187
188
189struct softnet_data softnet_data[NR_CPUS] __cacheline_aligned;
190
191#ifdef CONFIG_NET_FASTROUTE
192int netdev_fastroute;
193int netdev_fastroute_obstacles;
194#endif
195
196
197
198
199
200
201
202
203
204
205
206
207int netdev_nit=0;
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
234void dev_add_pack(struct packet_type *pt)
235{
236 int hash;
237
238 br_write_lock_bh(BR_NETPROTO_LOCK);
239
240#ifdef CONFIG_NET_FASTROUTE
241
242 if ((pt->data) && ((int)(pt->data)!=1)) {
243 netdev_fastroute_obstacles++;
244 dev_clear_fastroute(pt->dev);
245 }
246#endif
247 if (pt->type == htons(ETH_P_ALL)) {
248 netdev_nit++;
249 pt->next=ptype_all;
250 ptype_all=pt;
251 } else {
252 hash=ntohs(pt->type)&15;
253 pt->next = ptype_base[hash];
254 ptype_base[hash] = pt;
255 }
256 br_write_unlock_bh(BR_NETPROTO_LOCK);
257}
258
259
260
261
262
263
264
265
266
267
268
269
270void dev_remove_pack(struct packet_type *pt)
271{
272 struct packet_type **pt1;
273
274 br_write_lock_bh(BR_NETPROTO_LOCK);
275
276 if (pt->type == htons(ETH_P_ALL)) {
277 netdev_nit--;
278 pt1=&ptype_all;
279 } else {
280 pt1=&ptype_base[ntohs(pt->type)&15];
281 }
282
283 for (; (*pt1) != NULL; pt1 = &((*pt1)->next)) {
284 if (pt == (*pt1)) {
285 *pt1 = pt->next;
286#ifdef CONFIG_NET_FASTROUTE
287 if (pt->data)
288 netdev_fastroute_obstacles--;
289#endif
290 br_write_unlock_bh(BR_NETPROTO_LOCK);
291 return;
292 }
293 }
294 br_write_unlock_bh(BR_NETPROTO_LOCK);
295 printk(KERN_WARNING "dev_remove_pack: %p not found.\n", pt);
296}
297
298
299
300
301
302
303
304
305static struct netdev_boot_setup dev_boot_setup[NETDEV_BOOT_SETUP_MAX];
306
307
308
309
310
311
312
313
314
315
316int netdev_boot_setup_add(char *name, struct ifmap *map)
317{
318 struct netdev_boot_setup *s;
319 int i;
320
321 s = dev_boot_setup;
322 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
323 if (s[i].name[0] == '\0' || s[i].name[0] == ' ') {
324 memset(s[i].name, 0, sizeof(s[i].name));
325 strcpy(s[i].name, name);
326 memcpy(&s[i].map, map, sizeof(s[i].map));
327 break;
328 }
329 }
330
331 if (i >= NETDEV_BOOT_SETUP_MAX)
332 return 0;
333
334 return 1;
335}
336
337
338
339
340
341
342
343
344
345
346int netdev_boot_setup_check(struct net_device *dev)
347{
348 struct netdev_boot_setup *s;
349 int i;
350
351 s = dev_boot_setup;
352 for (i = 0; i < NETDEV_BOOT_SETUP_MAX; i++) {
353 if (s[i].name[0] != '\0' && s[i].name[0] != ' ' &&
354 !strncmp(dev->name, s[i].name, strlen(s[i].name))) {
355 dev->irq = s[i].map.irq;
356 dev->base_addr = s[i].map.base_addr;
357 dev->mem_start = s[i].map.mem_start;
358 dev->mem_end = s[i].map.mem_end;
359 return 1;
360 }
361 }
362 return 0;
363}
364
365
366
367
368int __init netdev_boot_setup(char *str)
369{
370 int ints[5];
371 struct ifmap map;
372
373 str = get_options(str, ARRAY_SIZE(ints), ints);
374 if (!str || !*str)
375 return 0;
376
377
378 memset(&map, 0, sizeof(map));
379 if (ints[0] > 0)
380 map.irq = ints[1];
381 if (ints[0] > 1)
382 map.base_addr = ints[2];
383 if (ints[0] > 2)
384 map.mem_start = ints[3];
385 if (ints[0] > 3)
386 map.mem_end = ints[4];
387
388
389 return netdev_boot_setup_add(str, &map);
390}
391
392__setup("netdev=", netdev_boot_setup);
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412struct net_device *__dev_get_by_name(const char *name)
413{
414 struct net_device *dev;
415
416 for (dev = dev_base; dev != NULL; dev = dev->next) {
417 if (strncmp(dev->name, name, IFNAMSIZ) == 0)
418 return dev;
419 }
420 return NULL;
421}
422
423
424
425
426
427
428
429
430
431
432
433
434struct net_device *dev_get_by_name(const char *name)
435{
436 struct net_device *dev;
437
438 read_lock(&dev_base_lock);
439 dev = __dev_get_by_name(name);
440 if (dev)
441 dev_hold(dev);
442 read_unlock(&dev_base_lock);
443 return dev;
444}
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466int dev_get(const char *name)
467{
468 struct net_device *dev;
469
470 read_lock(&dev_base_lock);
471 dev = __dev_get_by_name(name);
472 read_unlock(&dev_base_lock);
473 return dev != NULL;
474}
475
476
477
478
479
480
481
482
483
484
485
486
487struct net_device * __dev_get_by_index(int ifindex)
488{
489 struct net_device *dev;
490
491 for (dev = dev_base; dev != NULL; dev = dev->next) {
492 if (dev->ifindex == ifindex)
493 return dev;
494 }
495 return NULL;
496}
497
498
499
500
501
502
503
504
505
506
507
508
509struct net_device * dev_get_by_index(int ifindex)
510{
511 struct net_device *dev;
512
513 read_lock(&dev_base_lock);
514 dev = __dev_get_by_index(ifindex);
515 if (dev)
516 dev_hold(dev);
517 read_unlock(&dev_base_lock);
518 return dev;
519}
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535struct net_device *dev_getbyhwaddr(unsigned short type, char *ha)
536{
537 struct net_device *dev;
538
539 ASSERT_RTNL();
540
541 for (dev = dev_base; dev != NULL; dev = dev->next) {
542 if (dev->type == type &&
543 memcmp(dev->dev_addr, ha, dev->addr_len) == 0)
544 return dev;
545 }
546 return NULL;
547}
548
549
550
551
552
553
554
555
556
557
558
559
560struct net_device * dev_get_by_flags(unsigned short if_flags, unsigned short mask)
561{
562 struct net_device *dev;
563
564 read_lock(&dev_base_lock);
565 dev = __dev_get_by_flags(if_flags, mask);
566 if (dev)
567 dev_hold(dev);
568 read_unlock(&dev_base_lock);
569 return dev;
570}
571
572
573
574
575
576
577
578
579
580
581
582struct net_device *__dev_get_by_flags(unsigned short if_flags, unsigned short mask)
583{
584 struct net_device *dev;
585
586 for (dev = dev_base; dev != NULL; dev = dev->next) {
587 if (((dev->flags ^ if_flags) & mask) == 0)
588 return dev;
589 }
590 return NULL;
591}
592
593
594
595
596
597
598
599
600
601
602
603
604
605int dev_alloc_name(struct net_device *dev, const char *name)
606{
607 int i;
608 char buf[32];
609 char *p;
610
611
612
613
614
615
616 p = strchr(name, '%');
617 if (p && (p[1] != 'd' || strchr(p+2, '%')))
618 return -EINVAL;
619
620
621
622
623 for (i = 0; i < 100; i++) {
624 snprintf(buf,sizeof(buf),name,i);
625 if (__dev_get_by_name(buf) == NULL) {
626 strcpy(dev->name, buf);
627 return i;
628 }
629 }
630 return -ENFILE;
631}
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649struct net_device *dev_alloc(const char *name, int *err)
650{
651 struct net_device *dev=kmalloc(sizeof(struct net_device), GFP_KERNEL);
652 if (dev == NULL) {
653 *err = -ENOBUFS;
654 return NULL;
655 }
656 memset(dev, 0, sizeof(struct net_device));
657 *err = dev_alloc_name(dev, name);
658 if (*err < 0) {
659 kfree(dev);
660 return NULL;
661 }
662 return dev;
663}
664
665
666
667
668
669
670
671
672
673
674void netdev_state_change(struct net_device *dev)
675{
676 if (dev->flags&IFF_UP) {
677 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
678 rtmsg_ifinfo(RTM_NEWLINK, dev, 0);
679 }
680}
681
682
683#ifdef CONFIG_KMOD
684
685
686
687
688
689
690
691
692
693
694void dev_load(const char *name)
695{
696 if (!dev_get(name) && capable(CAP_SYS_MODULE))
697 request_module(name);
698}
699
700#else
701
702extern inline void dev_load(const char *unused){;}
703
704#endif
705
706static int default_rebuild_header(struct sk_buff *skb)
707{
708 printk(KERN_DEBUG "%s: default_rebuild_header called -- BUG!\n", skb->dev ? skb->dev->name : "NULL!!!");
709 kfree_skb(skb);
710 return 1;
711}
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726int dev_open(struct net_device *dev)
727{
728 int ret = 0;
729
730
731
732
733
734 if (dev->flags&IFF_UP)
735 return 0;
736
737
738
739
740 if (!netif_device_present(dev))
741 return -ENODEV;
742
743
744
745
746 if (try_inc_mod_count(dev->owner)) {
747 set_bit(__LINK_STATE_START, &dev->state);
748 if (dev->open) {
749 ret = dev->open(dev);
750 if (ret != 0) {
751 clear_bit(__LINK_STATE_START, &dev->state);
752 if (dev->owner)
753 __MOD_DEC_USE_COUNT(dev->owner);
754 }
755 }
756 } else {
757 ret = -ENODEV;
758 }
759
760
761
762
763
764 if (ret == 0)
765 {
766
767
768
769 dev->flags |= IFF_UP;
770
771
772
773
774 dev_mc_upload(dev);
775
776
777
778
779 dev_activate(dev);
780
781
782
783
784 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
785 }
786 return(ret);
787}
788
789#ifdef CONFIG_NET_FASTROUTE
790
791static void dev_do_clear_fastroute(struct net_device *dev)
792{
793 if (dev->accept_fastpath) {
794 int i;
795
796 for (i=0; i<=NETDEV_FASTROUTE_HMASK; i++) {
797 struct dst_entry *dst;
798
799 write_lock_irq(&dev->fastpath_lock);
800 dst = dev->fastpath[i];
801 dev->fastpath[i] = NULL;
802 write_unlock_irq(&dev->fastpath_lock);
803
804 dst_release(dst);
805 }
806 }
807}
808
809void dev_clear_fastroute(struct net_device *dev)
810{
811 if (dev) {
812 dev_do_clear_fastroute(dev);
813 } else {
814 read_lock(&dev_base_lock);
815 for (dev = dev_base; dev; dev = dev->next)
816 dev_do_clear_fastroute(dev);
817 read_unlock(&dev_base_lock);
818 }
819}
820#endif
821
822
823
824
825
826
827
828
829
830
831
832int dev_close(struct net_device *dev)
833{
834 if (!(dev->flags&IFF_UP))
835 return 0;
836
837
838
839
840
841 notifier_call_chain(&netdev_chain, NETDEV_GOING_DOWN, dev);
842
843 dev_deactivate(dev);
844
845 clear_bit(__LINK_STATE_START, &dev->state);
846
847
848
849
850
851
852
853 smp_mb__after_clear_bit();
854 while (test_bit(__LINK_STATE_RX_SCHED, &dev->state)) {
855
856 current->state = TASK_INTERRUPTIBLE;
857 schedule_timeout(1);
858 }
859
860
861
862
863
864
865
866
867
868 if (dev->stop)
869 dev->stop(dev);
870
871
872
873
874
875 dev->flags &= ~IFF_UP;
876#ifdef CONFIG_NET_FASTROUTE
877 dev_clear_fastroute(dev);
878#endif
879
880
881
882
883 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
884
885
886
887
888 if (dev->owner)
889 __MOD_DEC_USE_COUNT(dev->owner);
890
891 return(0);
892}
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910int register_netdevice_notifier(struct notifier_block *nb)
911{
912 return notifier_chain_register(&netdev_chain, nb);
913}
914
915
916
917
918
919
920
921
922
923
924
925int unregister_netdevice_notifier(struct notifier_block *nb)
926{
927 return notifier_chain_unregister(&netdev_chain,nb);
928}
929
930
931
932
933
934
935void dev_queue_xmit_nit(struct sk_buff *skb, struct net_device *dev)
936{
937 struct packet_type *ptype;
938 do_gettimeofday(&skb->stamp);
939
940 br_read_lock(BR_NETPROTO_LOCK);
941 for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next)
942 {
943
944
945
946 if ((ptype->dev == dev || !ptype->dev) &&
947 ((struct sock *)ptype->data != skb->sk))
948 {
949 struct sk_buff *skb2;
950 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
951 break;
952
953
954
955
956
957 skb2->mac.raw = skb2->data;
958
959 if (skb2->nh.raw < skb2->data || skb2->nh.raw > skb2->tail) {
960 if (net_ratelimit())
961 printk(KERN_CRIT "protocol %04x is buggy, dev %s\n", skb2->protocol, dev->name);
962 skb2->nh.raw = skb2->data;
963 }
964
965 skb2->h.raw = skb2->nh.raw;
966 skb2->pkt_type = PACKET_OUTGOING;
967 ptype->func(skb2, skb->dev, ptype);
968 }
969 }
970 br_read_unlock(BR_NETPROTO_LOCK);
971}
972
973
974
975
976
977struct sk_buff * skb_checksum_help(struct sk_buff *skb)
978{
979 int offset;
980 unsigned int csum;
981
982 offset = skb->h.raw - skb->data;
983 if (offset > (int)skb->len)
984 BUG();
985 csum = skb_checksum(skb, offset, skb->len-offset, 0);
986
987 offset = skb->tail - skb->h.raw;
988 if (offset <= 0)
989 BUG();
990 if (skb->csum+2 > offset)
991 BUG();
992
993 *(u16*)(skb->h.raw + skb->csum) = csum_fold(csum);
994 skb->ip_summed = CHECKSUM_NONE;
995 return skb;
996}
997
998#ifdef CONFIG_HIGHMEM
999
1000
1001
1002
1003
1004static inline int
1005illegal_highdma(struct net_device *dev, struct sk_buff *skb)
1006{
1007 int i;
1008
1009 if (dev->features&NETIF_F_HIGHDMA)
1010 return 0;
1011
1012 for (i=0; i<skb_shinfo(skb)->nr_frags; i++)
1013 if (skb_shinfo(skb)->frags[i].page >= highmem_start_page)
1014 return 1;
1015
1016 return 0;
1017}
1018#else
1019#define illegal_highdma(dev, skb) (0)
1020#endif
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035int dev_queue_xmit(struct sk_buff *skb)
1036{
1037 struct net_device *dev = skb->dev;
1038 struct Qdisc *q;
1039
1040 if (skb_shinfo(skb)->frag_list &&
1041 !(dev->features&NETIF_F_FRAGLIST) &&
1042 skb_linearize(skb, GFP_ATOMIC) != 0) {
1043 kfree_skb(skb);
1044 return -ENOMEM;
1045 }
1046
1047
1048
1049
1050
1051 if (skb_shinfo(skb)->nr_frags &&
1052 (!(dev->features&NETIF_F_SG) || illegal_highdma(dev, skb)) &&
1053 skb_linearize(skb, GFP_ATOMIC) != 0) {
1054 kfree_skb(skb);
1055 return -ENOMEM;
1056 }
1057
1058
1059
1060
1061 if (skb->ip_summed == CHECKSUM_HW &&
1062 (!(dev->features&(NETIF_F_HW_CSUM|NETIF_F_NO_CSUM)) &&
1063 (!(dev->features&NETIF_F_IP_CSUM) ||
1064 skb->protocol != htons(ETH_P_IP)))) {
1065 if ((skb = skb_checksum_help(skb)) == NULL)
1066 return -ENOMEM;
1067 }
1068
1069
1070 spin_lock_bh(&dev->queue_lock);
1071 q = dev->qdisc;
1072 if (q->enqueue) {
1073 int ret = q->enqueue(skb, q);
1074
1075 qdisc_run(dev);
1076
1077 spin_unlock_bh(&dev->queue_lock);
1078 return ret == NET_XMIT_BYPASS ? NET_XMIT_SUCCESS : ret;
1079 }
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092 if (dev->flags&IFF_UP) {
1093 int cpu = smp_processor_id();
1094
1095 if (dev->xmit_lock_owner != cpu) {
1096 spin_unlock(&dev->queue_lock);
1097 spin_lock(&dev->xmit_lock);
1098 dev->xmit_lock_owner = cpu;
1099
1100 if (!netif_queue_stopped(dev)) {
1101 if (netdev_nit)
1102 dev_queue_xmit_nit(skb,dev);
1103
1104 if (dev->hard_start_xmit(skb, dev) == 0) {
1105 dev->xmit_lock_owner = -1;
1106 spin_unlock_bh(&dev->xmit_lock);
1107 return 0;
1108 }
1109 }
1110 dev->xmit_lock_owner = -1;
1111 spin_unlock_bh(&dev->xmit_lock);
1112 if (net_ratelimit())
1113 printk(KERN_CRIT "Virtual device %s asks to queue packet!\n", dev->name);
1114 kfree_skb(skb);
1115 return -ENETDOWN;
1116 } else {
1117
1118 if (net_ratelimit())
1119 printk(KERN_CRIT "Dead loop on virtual device %s, fix it urgently!\n", dev->name);
1120 }
1121 }
1122 spin_unlock_bh(&dev->queue_lock);
1123
1124 kfree_skb(skb);
1125 return -ENETDOWN;
1126}
1127
1128
1129
1130
1131
1132
1133int netdev_max_backlog = 300;
1134int weight_p = 64;
1135
1136
1137
1138
1139int no_cong_thresh = 10;
1140int no_cong = 20;
1141int lo_cong = 100;
1142int mod_cong = 290;
1143
1144struct netif_rx_stats netdev_rx_stat[NR_CPUS];
1145
1146
1147#ifdef CONFIG_NET_HW_FLOWCONTROL
1148atomic_t netdev_dropping = ATOMIC_INIT(0);
1149static unsigned long netdev_fc_mask = 1;
1150unsigned long netdev_fc_xoff = 0;
1151spinlock_t netdev_fc_lock = SPIN_LOCK_UNLOCKED;
1152
1153static struct
1154{
1155 void (*stimul)(struct net_device *);
1156 struct net_device *dev;
1157} netdev_fc_slots[BITS_PER_LONG];
1158
1159int netdev_register_fc(struct net_device *dev, void (*stimul)(struct net_device *dev))
1160{
1161 int bit = 0;
1162 unsigned long flags;
1163
1164 spin_lock_irqsave(&netdev_fc_lock, flags);
1165 if (netdev_fc_mask != ~0UL) {
1166 bit = ffz(netdev_fc_mask);
1167 netdev_fc_slots[bit].stimul = stimul;
1168 netdev_fc_slots[bit].dev = dev;
1169 set_bit(bit, &netdev_fc_mask);
1170 clear_bit(bit, &netdev_fc_xoff);
1171 }
1172 spin_unlock_irqrestore(&netdev_fc_lock, flags);
1173 return bit;
1174}
1175
1176void netdev_unregister_fc(int bit)
1177{
1178 unsigned long flags;
1179
1180 spin_lock_irqsave(&netdev_fc_lock, flags);
1181 if (bit > 0) {
1182 netdev_fc_slots[bit].stimul = NULL;
1183 netdev_fc_slots[bit].dev = NULL;
1184 clear_bit(bit, &netdev_fc_mask);
1185 clear_bit(bit, &netdev_fc_xoff);
1186 }
1187 spin_unlock_irqrestore(&netdev_fc_lock, flags);
1188}
1189
1190static void netdev_wakeup(void)
1191{
1192 unsigned long xoff;
1193
1194 spin_lock(&netdev_fc_lock);
1195 xoff = netdev_fc_xoff;
1196 netdev_fc_xoff = 0;
1197 while (xoff) {
1198 int i = ffz(~xoff);
1199 xoff &= ~(1<<i);
1200 netdev_fc_slots[i].stimul(netdev_fc_slots[i].dev);
1201 }
1202 spin_unlock(&netdev_fc_lock);
1203}
1204#endif
1205
1206static void get_sample_stats(int cpu)
1207{
1208#ifdef RAND_LIE
1209 unsigned long rd;
1210 int rq;
1211#endif
1212 int blog = softnet_data[cpu].input_pkt_queue.qlen;
1213 int avg_blog = softnet_data[cpu].avg_blog;
1214
1215 avg_blog = (avg_blog >> 1)+ (blog >> 1);
1216
1217 if (avg_blog > mod_cong) {
1218
1219 softnet_data[cpu].cng_level = NET_RX_CN_HIGH;
1220#ifdef RAND_LIE
1221 rd = net_random();
1222 rq = rd % netdev_max_backlog;
1223 if (rq < avg_blog)
1224 softnet_data[cpu].cng_level = NET_RX_DROP;
1225#endif
1226 } else if (avg_blog > lo_cong) {
1227 softnet_data[cpu].cng_level = NET_RX_CN_MOD;
1228#ifdef RAND_LIE
1229 rd = net_random();
1230 rq = rd % netdev_max_backlog;
1231 if (rq < avg_blog)
1232 softnet_data[cpu].cng_level = NET_RX_CN_HIGH;
1233#endif
1234 } else if (avg_blog > no_cong)
1235 softnet_data[cpu].cng_level = NET_RX_CN_LOW;
1236 else
1237 softnet_data[cpu].cng_level = NET_RX_SUCCESS;
1238
1239 softnet_data[cpu].avg_blog = avg_blog;
1240}
1241
1242#ifdef OFFLINE_SAMPLE
1243static void sample_queue(unsigned long dummy)
1244{
1245
1246 int next_tick = 1;
1247 int cpu = smp_processor_id();
1248
1249 get_sample_stats(cpu);
1250 next_tick += jiffies;
1251 mod_timer(&samp_timer, next_tick);
1252}
1253#endif
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275int netif_rx(struct sk_buff *skb)
1276{
1277 int this_cpu = smp_processor_id();
1278 struct softnet_data *queue;
1279 unsigned long flags;
1280
1281 if (skb->stamp.tv_sec == 0)
1282 do_gettimeofday(&skb->stamp);
1283
1284
1285
1286
1287 queue = &softnet_data[this_cpu];
1288
1289 local_irq_save(flags);
1290
1291 netdev_rx_stat[this_cpu].total++;
1292 if (queue->input_pkt_queue.qlen <= netdev_max_backlog) {
1293 if (queue->input_pkt_queue.qlen) {
1294 if (queue->throttle)
1295 goto drop;
1296
1297enqueue:
1298 dev_hold(skb->dev);
1299 __skb_queue_tail(&queue->input_pkt_queue,skb);
1300 local_irq_restore(flags);
1301#ifndef OFFLINE_SAMPLE
1302 get_sample_stats(this_cpu);
1303#endif
1304 return queue->cng_level;
1305 }
1306
1307 if (queue->throttle) {
1308 queue->throttle = 0;
1309#ifdef CONFIG_NET_HW_FLOWCONTROL
1310 if (atomic_dec_and_test(&netdev_dropping))
1311 netdev_wakeup();
1312#endif
1313 }
1314
1315 netif_rx_schedule(&queue->blog_dev);
1316 goto enqueue;
1317 }
1318
1319 if (queue->throttle == 0) {
1320 queue->throttle = 1;
1321 netdev_rx_stat[this_cpu].throttled++;
1322#ifdef CONFIG_NET_HW_FLOWCONTROL
1323 atomic_inc(&netdev_dropping);
1324#endif
1325 }
1326
1327drop:
1328 netdev_rx_stat[this_cpu].dropped++;
1329 local_irq_restore(flags);
1330
1331 kfree_skb(skb);
1332 return NET_RX_DROP;
1333}
1334
1335
1336
1337
1338static int deliver_to_old_ones(struct packet_type *pt, struct sk_buff *skb, int last)
1339{
1340 static spinlock_t net_bh_lock = SPIN_LOCK_UNLOCKED;
1341 int ret = NET_RX_DROP;
1342
1343
1344 if (!last) {
1345 skb = skb_clone(skb, GFP_ATOMIC);
1346 if (skb == NULL)
1347 return ret;
1348 }
1349 if (skb_is_nonlinear(skb) && skb_linearize(skb, GFP_ATOMIC) != 0) {
1350 kfree_skb(skb);
1351 return ret;
1352 }
1353
1354
1355
1356
1357
1358
1359 spin_lock(&net_bh_lock);
1360
1361
1362 tasklet_disable(bh_task_vec+TIMER_BH);
1363
1364 ret = pt->func(skb, skb->dev, pt);
1365
1366 tasklet_hi_enable(bh_task_vec+TIMER_BH);
1367 spin_unlock(&net_bh_lock);
1368 return ret;
1369}
1370
1371static __inline__ void skb_bond(struct sk_buff *skb)
1372{
1373 struct net_device *dev = skb->dev;
1374
1375 if (dev->master) {
1376 skb->real_dev = skb->dev;
1377 skb->dev = dev->master;
1378 }
1379}
1380
1381static void net_tx_action(struct softirq_action *h)
1382{
1383 int cpu = smp_processor_id();
1384
1385 if (softnet_data[cpu].completion_queue) {
1386 struct sk_buff *clist;
1387
1388 local_irq_disable();
1389 clist = softnet_data[cpu].completion_queue;
1390 softnet_data[cpu].completion_queue = NULL;
1391 local_irq_enable();
1392
1393 while (clist != NULL) {
1394 struct sk_buff *skb = clist;
1395 clist = clist->next;
1396
1397 BUG_TRAP(atomic_read(&skb->users) == 0);
1398 __kfree_skb(skb);
1399 }
1400 }
1401
1402 if (softnet_data[cpu].output_queue) {
1403 struct net_device *head;
1404
1405 local_irq_disable();
1406 head = softnet_data[cpu].output_queue;
1407 softnet_data[cpu].output_queue = NULL;
1408 local_irq_enable();
1409
1410 while (head != NULL) {
1411 struct net_device *dev = head;
1412 head = head->next_sched;
1413
1414 smp_mb__before_clear_bit();
1415 clear_bit(__LINK_STATE_SCHED, &dev->state);
1416
1417 if (spin_trylock(&dev->queue_lock)) {
1418 qdisc_run(dev);
1419 spin_unlock(&dev->queue_lock);
1420 } else {
1421 netif_schedule(dev);
1422 }
1423 }
1424 }
1425}
1426
1427
1428#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1429void (*br_handle_frame_hook)(struct sk_buff *skb) = NULL;
1430#endif
1431
1432static __inline__ int handle_bridge(struct sk_buff *skb,
1433 struct packet_type *pt_prev)
1434{
1435 int ret = NET_RX_DROP;
1436
1437 if (pt_prev) {
1438 if (!pt_prev->data)
1439 ret = deliver_to_old_ones(pt_prev, skb, 0);
1440 else {
1441 atomic_inc(&skb->users);
1442 ret = pt_prev->func(skb, skb->dev, pt_prev);
1443 }
1444 }
1445
1446 br_handle_frame_hook(skb);
1447 return ret;
1448}
1449
1450
1451#ifdef CONFIG_NET_DIVERT
1452static inline int handle_diverter(struct sk_buff *skb)
1453{
1454
1455 if (skb->dev->divert && skb->dev->divert->divert)
1456 divert_frame(skb);
1457 return 0;
1458}
1459#endif
1460
1461int netif_receive_skb(struct sk_buff *skb)
1462{
1463 struct packet_type *ptype, *pt_prev;
1464 int ret = NET_RX_DROP;
1465 unsigned short type = skb->protocol;
1466
1467 if (skb->stamp.tv_sec == 0)
1468 do_gettimeofday(&skb->stamp);
1469
1470 skb_bond(skb);
1471
1472 netdev_rx_stat[smp_processor_id()].total++;
1473
1474#ifdef CONFIG_NET_FASTROUTE
1475 if (skb->pkt_type == PACKET_FASTROUTE) {
1476 netdev_rx_stat[smp_processor_id()].fastroute_deferred_out++;
1477 return dev_queue_xmit(skb);
1478 }
1479#endif
1480
1481 skb->h.raw = skb->nh.raw = skb->data;
1482
1483 pt_prev = NULL;
1484 for (ptype = ptype_all; ptype; ptype = ptype->next) {
1485 if (!ptype->dev || ptype->dev == skb->dev) {
1486 if (pt_prev) {
1487 if (!pt_prev->data) {
1488 ret = deliver_to_old_ones(pt_prev, skb, 0);
1489 } else {
1490 atomic_inc(&skb->users);
1491 ret = pt_prev->func(skb, skb->dev, pt_prev);
1492 }
1493 }
1494 pt_prev = ptype;
1495 }
1496 }
1497
1498#ifdef CONFIG_NET_DIVERT
1499 if (skb->dev->divert && skb->dev->divert->divert)
1500 ret = handle_diverter(skb);
1501#endif
1502
1503#if defined(CONFIG_BRIDGE) || defined(CONFIG_BRIDGE_MODULE)
1504 if (skb->dev->br_port != NULL &&
1505 br_handle_frame_hook != NULL) {
1506 return handle_bridge(skb, pt_prev);
1507 }
1508#endif
1509
1510 for (ptype=ptype_base[ntohs(type)&15];ptype;ptype=ptype->next) {
1511 if (ptype->type == type &&
1512 (!ptype->dev || ptype->dev == skb->dev)) {
1513 if (pt_prev) {
1514 if (!pt_prev->data) {
1515 ret = deliver_to_old_ones(pt_prev, skb, 0);
1516 } else {
1517 atomic_inc(&skb->users);
1518 ret = pt_prev->func(skb, skb->dev, pt_prev);
1519 }
1520 }
1521 pt_prev = ptype;
1522 }
1523 }
1524
1525 if (pt_prev) {
1526 if (!pt_prev->data) {
1527 ret = deliver_to_old_ones(pt_prev, skb, 1);
1528 } else {
1529 ret = pt_prev->func(skb, skb->dev, pt_prev);
1530 }
1531 } else {
1532 kfree_skb(skb);
1533
1534
1535
1536 ret = NET_RX_DROP;
1537 }
1538
1539 return ret;
1540}
1541
1542static int process_backlog(struct net_device *blog_dev, int *budget)
1543{
1544 int work = 0;
1545 int quota = min(blog_dev->quota, *budget);
1546 int this_cpu = smp_processor_id();
1547 struct softnet_data *queue = &softnet_data[this_cpu];
1548 unsigned long start_time = jiffies;
1549
1550 for (;;) {
1551 struct sk_buff *skb;
1552 struct net_device *dev;
1553
1554 local_irq_disable();
1555 skb = __skb_dequeue(&queue->input_pkt_queue);
1556 if (skb == NULL)
1557 goto job_done;
1558 local_irq_enable();
1559
1560 dev = skb->dev;
1561
1562 netif_receive_skb(skb);
1563
1564 dev_put(dev);
1565
1566 work++;
1567
1568 if (work >= quota || jiffies - start_time > 1)
1569 break;
1570
1571#ifdef CONFIG_NET_HW_FLOWCONTROL
1572 if (queue->throttle && queue->input_pkt_queue.qlen < no_cong_thresh ) {
1573 if (atomic_dec_and_test(&netdev_dropping)) {
1574 queue->throttle = 0;
1575 netdev_wakeup();
1576 break;
1577 }
1578 }
1579#endif
1580 }
1581
1582 blog_dev->quota -= work;
1583 *budget -= work;
1584 return -1;
1585
1586job_done:
1587 blog_dev->quota -= work;
1588 *budget -= work;
1589
1590 list_del(&blog_dev->poll_list);
1591 clear_bit(__LINK_STATE_RX_SCHED, &blog_dev->state);
1592
1593 if (queue->throttle) {
1594 queue->throttle = 0;
1595#ifdef CONFIG_NET_HW_FLOWCONTROL
1596 if (atomic_dec_and_test(&netdev_dropping))
1597 netdev_wakeup();
1598#endif
1599 }
1600 local_irq_enable();
1601 return 0;
1602}
1603
1604static void net_rx_action(struct softirq_action *h)
1605{
1606 int this_cpu = smp_processor_id();
1607 struct softnet_data *queue = &softnet_data[this_cpu];
1608 unsigned long start_time = jiffies;
1609 int budget = netdev_max_backlog;
1610
1611 br_read_lock(BR_NETPROTO_LOCK);
1612 local_irq_disable();
1613
1614 while (!list_empty(&queue->poll_list)) {
1615 struct net_device *dev;
1616
1617 if (budget <= 0 || jiffies - start_time > 1)
1618 goto softnet_break;
1619
1620 local_irq_enable();
1621
1622 dev = list_entry(queue->poll_list.next, struct net_device, poll_list);
1623
1624 if (dev->quota <= 0 || dev->poll(dev, &budget)) {
1625 local_irq_disable();
1626 list_del(&dev->poll_list);
1627 list_add_tail(&dev->poll_list, &queue->poll_list);
1628 if (dev->quota < 0)
1629 dev->quota += dev->weight;
1630 else
1631 dev->quota = dev->weight;
1632 } else {
1633 dev_put(dev);
1634 local_irq_disable();
1635 }
1636 }
1637
1638 local_irq_enable();
1639 br_read_unlock(BR_NETPROTO_LOCK);
1640 return;
1641
1642softnet_break:
1643 netdev_rx_stat[this_cpu].time_squeeze++;
1644 __cpu_raise_softirq(this_cpu, NET_RX_SOFTIRQ);
1645
1646 local_irq_enable();
1647 br_read_unlock(BR_NETPROTO_LOCK);
1648}
1649
1650static gifconf_func_t * gifconf_list [NPROTO];
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662int register_gifconf(unsigned int family, gifconf_func_t * gifconf)
1663{
1664 if (family>=NPROTO)
1665 return -EINVAL;
1666 gifconf_list[family] = gifconf;
1667 return 0;
1668}
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682static int dev_ifname(struct ifreq *arg)
1683{
1684 struct net_device *dev;
1685 struct ifreq ifr;
1686
1687
1688
1689
1690
1691 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
1692 return -EFAULT;
1693
1694 read_lock(&dev_base_lock);
1695 dev = __dev_get_by_index(ifr.ifr_ifindex);
1696 if (!dev) {
1697 read_unlock(&dev_base_lock);
1698 return -ENODEV;
1699 }
1700
1701 strcpy(ifr.ifr_name, dev->name);
1702 read_unlock(&dev_base_lock);
1703
1704 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
1705 return -EFAULT;
1706 return 0;
1707}
1708
1709
1710
1711
1712
1713
1714
1715static int dev_ifconf(char *arg)
1716{
1717 struct ifconf ifc;
1718 struct net_device *dev;
1719 char *pos;
1720 int len;
1721 int total;
1722 int i;
1723
1724
1725
1726
1727
1728 if (copy_from_user(&ifc, arg, sizeof(struct ifconf)))
1729 return -EFAULT;
1730
1731 pos = ifc.ifc_buf;
1732 len = ifc.ifc_len;
1733
1734
1735
1736
1737
1738 total = 0;
1739 for (dev = dev_base; dev != NULL; dev = dev->next) {
1740 for (i=0; i<NPROTO; i++) {
1741 if (gifconf_list[i]) {
1742 int done;
1743 if (pos==NULL) {
1744 done = gifconf_list[i](dev, NULL, 0);
1745 } else {
1746 done = gifconf_list[i](dev, pos+total, len-total);
1747 }
1748 if (done<0) {
1749 return -EFAULT;
1750 }
1751 total += done;
1752 }
1753 }
1754 }
1755
1756
1757
1758
1759 ifc.ifc_len = total;
1760
1761 if (copy_to_user(arg, &ifc, sizeof(struct ifconf)))
1762 return -EFAULT;
1763
1764
1765
1766
1767 return 0;
1768}
1769
1770
1771
1772
1773
1774
1775#ifdef CONFIG_PROC_FS
1776
1777static int sprintf_stats(char *buffer, struct net_device *dev)
1778{
1779 struct net_device_stats *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
1780 int size;
1781
1782 if (stats)
1783 size = sprintf(buffer, "%6s:%8lu %7lu %4lu %4lu %4lu %5lu %10lu %9lu %8lu %7lu %4lu %4lu %4lu %5lu %7lu %10lu\n",
1784 dev->name,
1785 stats->rx_bytes,
1786 stats->rx_packets, stats->rx_errors,
1787 stats->rx_dropped + stats->rx_missed_errors,
1788 stats->rx_fifo_errors,
1789 stats->rx_length_errors + stats->rx_over_errors
1790 + stats->rx_crc_errors + stats->rx_frame_errors,
1791 stats->rx_compressed, stats->multicast,
1792 stats->tx_bytes,
1793 stats->tx_packets, stats->tx_errors, stats->tx_dropped,
1794 stats->tx_fifo_errors, stats->collisions,
1795 stats->tx_carrier_errors + stats->tx_aborted_errors
1796 + stats->tx_window_errors + stats->tx_heartbeat_errors,
1797 stats->tx_compressed);
1798 else
1799 size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
1800
1801 return size;
1802}
1803
1804
1805
1806
1807
1808
1809static int dev_get_info(char *buffer, char **start, off_t offset, int length)
1810{
1811 int len = 0;
1812 off_t begin = 0;
1813 off_t pos = 0;
1814 int size;
1815 struct net_device *dev;
1816
1817
1818 size = sprintf(buffer,
1819 "Inter-| Receive | Transmit\n"
1820 " face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed\n");
1821
1822 pos += size;
1823 len += size;
1824
1825
1826 read_lock(&dev_base_lock);
1827 for (dev = dev_base; dev != NULL; dev = dev->next) {
1828 size = sprintf_stats(buffer+len, dev);
1829 len += size;
1830 pos = begin + len;
1831
1832 if (pos < offset) {
1833 len = 0;
1834 begin = pos;
1835 }
1836 if (pos > offset + length)
1837 break;
1838 }
1839 read_unlock(&dev_base_lock);
1840
1841 *start = buffer + (offset - begin);
1842 len -= (offset - begin);
1843 if (len > length)
1844 len = length;
1845 if (len < 0)
1846 len = 0;
1847 return len;
1848}
1849
1850static int dev_proc_stats(char *buffer, char **start, off_t offset,
1851 int length, int *eof, void *data)
1852{
1853 int i, lcpu;
1854 int len=0;
1855
1856 for (lcpu=0; lcpu<smp_num_cpus; lcpu++) {
1857 i = cpu_logical_map(lcpu);
1858 len += sprintf(buffer+len, "%08x %08x %08x %08x %08x %08x %08x %08x %08x\n",
1859 netdev_rx_stat[i].total,
1860 netdev_rx_stat[i].dropped,
1861 netdev_rx_stat[i].time_squeeze,
1862 netdev_rx_stat[i].throttled,
1863 netdev_rx_stat[i].fastroute_hit,
1864 netdev_rx_stat[i].fastroute_success,
1865 netdev_rx_stat[i].fastroute_defer,
1866 netdev_rx_stat[i].fastroute_deferred_out,
1867#if 0
1868 netdev_rx_stat[i].fastroute_latency_reduction
1869#else
1870 netdev_rx_stat[i].cpu_collision
1871#endif
1872 );
1873 }
1874
1875 len -= offset;
1876
1877 if (len > length)
1878 len = length;
1879 if (len < 0)
1880 len = 0;
1881
1882 *start = buffer + offset;
1883 *eof = 1;
1884
1885 return len;
1886}
1887
1888#endif
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903int netdev_set_master(struct net_device *slave, struct net_device *master)
1904{
1905 struct net_device *old = slave->master;
1906
1907 ASSERT_RTNL();
1908
1909 if (master) {
1910 if (old)
1911 return -EBUSY;
1912 dev_hold(master);
1913 }
1914
1915 br_write_lock_bh(BR_NETPROTO_LOCK);
1916 slave->master = master;
1917 br_write_unlock_bh(BR_NETPROTO_LOCK);
1918
1919 if (old)
1920 dev_put(old);
1921
1922 if (master)
1923 slave->flags |= IFF_SLAVE;
1924 else
1925 slave->flags &= ~IFF_SLAVE;
1926
1927 rtmsg_ifinfo(RTM_NEWLINK, slave, IFF_SLAVE);
1928 return 0;
1929}
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942void dev_set_promiscuity(struct net_device *dev, int inc)
1943{
1944 unsigned short old_flags = dev->flags;
1945
1946 dev->flags |= IFF_PROMISC;
1947 if ((dev->promiscuity += inc) == 0)
1948 dev->flags &= ~IFF_PROMISC;
1949 if (dev->flags^old_flags) {
1950#ifdef CONFIG_NET_FASTROUTE
1951 if (dev->flags&IFF_PROMISC) {
1952 netdev_fastroute_obstacles++;
1953 dev_clear_fastroute(dev);
1954 } else
1955 netdev_fastroute_obstacles--;
1956#endif
1957 dev_mc_upload(dev);
1958 printk(KERN_INFO "device %s %s promiscuous mode\n",
1959 dev->name, (dev->flags&IFF_PROMISC) ? "entered" : "left");
1960 }
1961}
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975void dev_set_allmulti(struct net_device *dev, int inc)
1976{
1977 unsigned short old_flags = dev->flags;
1978
1979 dev->flags |= IFF_ALLMULTI;
1980 if ((dev->allmulti += inc) == 0)
1981 dev->flags &= ~IFF_ALLMULTI;
1982 if (dev->flags^old_flags)
1983 dev_mc_upload(dev);
1984}
1985
1986int dev_change_flags(struct net_device *dev, unsigned flags)
1987{
1988 int ret;
1989 int old_flags = dev->flags;
1990
1991
1992
1993
1994
1995 dev->flags = (flags & (IFF_DEBUG|IFF_NOTRAILERS|IFF_NOARP|IFF_DYNAMIC|
1996 IFF_MULTICAST|IFF_PORTSEL|IFF_AUTOMEDIA)) |
1997 (dev->flags & (IFF_UP|IFF_VOLATILE|IFF_PROMISC|IFF_ALLMULTI));
1998
1999
2000
2001
2002
2003 dev_mc_upload(dev);
2004
2005
2006
2007
2008
2009
2010
2011 ret = 0;
2012 if ((old_flags^flags)&IFF_UP)
2013 {
2014 ret = ((old_flags & IFF_UP) ? dev_close : dev_open)(dev);
2015
2016 if (ret == 0)
2017 dev_mc_upload(dev);
2018 }
2019
2020 if (dev->flags&IFF_UP &&
2021 ((old_flags^dev->flags)&~(IFF_UP|IFF_PROMISC|IFF_ALLMULTI|IFF_VOLATILE)))
2022 notifier_call_chain(&netdev_chain, NETDEV_CHANGE, dev);
2023
2024 if ((flags^dev->gflags)&IFF_PROMISC) {
2025 int inc = (flags&IFF_PROMISC) ? +1 : -1;
2026 dev->gflags ^= IFF_PROMISC;
2027 dev_set_promiscuity(dev, inc);
2028 }
2029
2030
2031
2032
2033
2034 if ((flags^dev->gflags)&IFF_ALLMULTI) {
2035 int inc = (flags&IFF_ALLMULTI) ? +1 : -1;
2036 dev->gflags ^= IFF_ALLMULTI;
2037 dev_set_allmulti(dev, inc);
2038 }
2039
2040 if (old_flags^dev->flags)
2041 rtmsg_ifinfo(RTM_NEWLINK, dev, old_flags^dev->flags);
2042
2043 return ret;
2044}
2045
2046
2047
2048
2049
2050static int dev_ifsioc(struct ifreq *ifr, unsigned int cmd)
2051{
2052 struct net_device *dev;
2053 int err;
2054
2055 if ((dev = __dev_get_by_name(ifr->ifr_name)) == NULL)
2056 return -ENODEV;
2057
2058 switch(cmd)
2059 {
2060 case SIOCGIFFLAGS:
2061 ifr->ifr_flags = (dev->flags&~(IFF_PROMISC|IFF_ALLMULTI|IFF_RUNNING))
2062 |(dev->gflags&(IFF_PROMISC|IFF_ALLMULTI));
2063 if (netif_running(dev) && netif_carrier_ok(dev))
2064 ifr->ifr_flags |= IFF_RUNNING;
2065 return 0;
2066
2067 case SIOCSIFFLAGS:
2068 return dev_change_flags(dev, ifr->ifr_flags);
2069
2070 case SIOCGIFMETRIC:
2071 ifr->ifr_metric = 0;
2072 return 0;
2073
2074 case SIOCSIFMETRIC:
2075 return -EOPNOTSUPP;
2076
2077 case SIOCGIFMTU:
2078 ifr->ifr_mtu = dev->mtu;
2079 return 0;
2080
2081 case SIOCSIFMTU:
2082 if (ifr->ifr_mtu == dev->mtu)
2083 return 0;
2084
2085
2086
2087
2088
2089 if (ifr->ifr_mtu<0)
2090 return -EINVAL;
2091
2092 if (!netif_device_present(dev))
2093 return -ENODEV;
2094
2095 if (dev->change_mtu)
2096 err = dev->change_mtu(dev, ifr->ifr_mtu);
2097 else {
2098 dev->mtu = ifr->ifr_mtu;
2099 err = 0;
2100 }
2101 if (!err && dev->flags&IFF_UP)
2102 notifier_call_chain(&netdev_chain, NETDEV_CHANGEMTU, dev);
2103 return err;
2104
2105 case SIOCGIFHWADDR:
2106 memcpy(ifr->ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
2107 ifr->ifr_hwaddr.sa_family=dev->type;
2108 return 0;
2109
2110 case SIOCSIFHWADDR:
2111 if (dev->set_mac_address == NULL)
2112 return -EOPNOTSUPP;
2113 if (ifr->ifr_hwaddr.sa_family!=dev->type)
2114 return -EINVAL;
2115 if (!netif_device_present(dev))
2116 return -ENODEV;
2117 err = dev->set_mac_address(dev, &ifr->ifr_hwaddr);
2118 if (!err)
2119 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2120 return err;
2121
2122 case SIOCSIFHWBROADCAST:
2123 if (ifr->ifr_hwaddr.sa_family!=dev->type)
2124 return -EINVAL;
2125 memcpy(dev->broadcast, ifr->ifr_hwaddr.sa_data, MAX_ADDR_LEN);
2126 notifier_call_chain(&netdev_chain, NETDEV_CHANGEADDR, dev);
2127 return 0;
2128
2129 case SIOCGIFMAP:
2130 ifr->ifr_map.mem_start=dev->mem_start;
2131 ifr->ifr_map.mem_end=dev->mem_end;
2132 ifr->ifr_map.base_addr=dev->base_addr;
2133 ifr->ifr_map.irq=dev->irq;
2134 ifr->ifr_map.dma=dev->dma;
2135 ifr->ifr_map.port=dev->if_port;
2136 return 0;
2137
2138 case SIOCSIFMAP:
2139 if (dev->set_config) {
2140 if (!netif_device_present(dev))
2141 return -ENODEV;
2142 return dev->set_config(dev,&ifr->ifr_map);
2143 }
2144 return -EOPNOTSUPP;
2145
2146 case SIOCADDMULTI:
2147 if (dev->set_multicast_list == NULL ||
2148 ifr->ifr_hwaddr.sa_family != AF_UNSPEC)
2149 return -EINVAL;
2150 if (!netif_device_present(dev))
2151 return -ENODEV;
2152 dev_mc_add(dev,ifr->ifr_hwaddr.sa_data, dev->addr_len, 1);
2153 return 0;
2154
2155 case SIOCDELMULTI:
2156 if (dev->set_multicast_list == NULL ||
2157 ifr->ifr_hwaddr.sa_family!=AF_UNSPEC)
2158 return -EINVAL;
2159 if (!netif_device_present(dev))
2160 return -ENODEV;
2161 dev_mc_delete(dev,ifr->ifr_hwaddr.sa_data,dev->addr_len, 1);
2162 return 0;
2163
2164 case SIOCGIFINDEX:
2165 ifr->ifr_ifindex = dev->ifindex;
2166 return 0;
2167
2168 case SIOCGIFTXQLEN:
2169 ifr->ifr_qlen = dev->tx_queue_len;
2170 return 0;
2171
2172 case SIOCSIFTXQLEN:
2173 if (ifr->ifr_qlen<0)
2174 return -EINVAL;
2175 dev->tx_queue_len = ifr->ifr_qlen;
2176 return 0;
2177
2178 case SIOCSIFNAME:
2179 if (dev->flags&IFF_UP)
2180 return -EBUSY;
2181 if (__dev_get_by_name(ifr->ifr_newname))
2182 return -EEXIST;
2183 memcpy(dev->name, ifr->ifr_newname, IFNAMSIZ);
2184 dev->name[IFNAMSIZ-1] = 0;
2185 notifier_call_chain(&netdev_chain, NETDEV_CHANGENAME, dev);
2186 return 0;
2187
2188
2189
2190
2191
2192 default:
2193 if ((cmd >= SIOCDEVPRIVATE &&
2194 cmd <= SIOCDEVPRIVATE + 15) ||
2195 cmd == SIOCBONDENSLAVE ||
2196 cmd == SIOCBONDRELEASE ||
2197 cmd == SIOCBONDSETHWADDR ||
2198 cmd == SIOCBONDSLAVEINFOQUERY ||
2199 cmd == SIOCBONDINFOQUERY ||
2200 cmd == SIOCBONDCHANGEACTIVE ||
2201 cmd == SIOCETHTOOL ||
2202 cmd == SIOCGMIIPHY ||
2203 cmd == SIOCGMIIREG ||
2204 cmd == SIOCSMIIREG ||
2205 cmd == SIOCWANDEV) {
2206 if (dev->do_ioctl) {
2207 if (!netif_device_present(dev))
2208 return -ENODEV;
2209 return dev->do_ioctl(dev, ifr, cmd);
2210 }
2211 return -EOPNOTSUPP;
2212 }
2213
2214 }
2215 return -EINVAL;
2216}
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234int dev_ioctl(unsigned int cmd, void *arg)
2235{
2236 struct ifreq ifr;
2237 int ret;
2238 char *colon;
2239
2240
2241
2242
2243
2244
2245 if (cmd == SIOCGIFCONF) {
2246 rtnl_shlock();
2247 ret = dev_ifconf((char *) arg);
2248 rtnl_shunlock();
2249 return ret;
2250 }
2251 if (cmd == SIOCGIFNAME) {
2252 return dev_ifname((struct ifreq *)arg);
2253 }
2254
2255 if (copy_from_user(&ifr, arg, sizeof(struct ifreq)))
2256 return -EFAULT;
2257
2258 ifr.ifr_name[IFNAMSIZ-1] = 0;
2259
2260 colon = strchr(ifr.ifr_name, ':');
2261 if (colon)
2262 *colon = 0;
2263
2264
2265
2266
2267
2268 switch(cmd)
2269 {
2270
2271
2272
2273
2274
2275
2276
2277 case SIOCGIFFLAGS:
2278 case SIOCGIFMETRIC:
2279 case SIOCGIFMTU:
2280 case SIOCGIFHWADDR:
2281 case SIOCGIFSLAVE:
2282 case SIOCGIFMAP:
2283 case SIOCGIFINDEX:
2284 case SIOCGIFTXQLEN:
2285 dev_load(ifr.ifr_name);
2286 read_lock(&dev_base_lock);
2287 ret = dev_ifsioc(&ifr, cmd);
2288 read_unlock(&dev_base_lock);
2289 if (!ret) {
2290 if (colon)
2291 *colon = ':';
2292 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2293 return -EFAULT;
2294 }
2295 return ret;
2296
2297
2298
2299
2300
2301
2302
2303
2304 case SIOCETHTOOL:
2305 case SIOCGMIIPHY:
2306 case SIOCGMIIREG:
2307 if (!capable(CAP_NET_ADMIN))
2308 return -EPERM;
2309 dev_load(ifr.ifr_name);
2310 dev_probe_lock();
2311 rtnl_lock();
2312 ret = dev_ifsioc(&ifr, cmd);
2313 rtnl_unlock();
2314 dev_probe_unlock();
2315 if (!ret) {
2316 if (colon)
2317 *colon = ':';
2318 if (copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2319 return -EFAULT;
2320 }
2321 return ret;
2322
2323
2324
2325
2326
2327
2328
2329
2330 case SIOCSIFFLAGS:
2331 case SIOCSIFMETRIC:
2332 case SIOCSIFMTU:
2333 case SIOCSIFMAP:
2334 case SIOCSIFHWADDR:
2335 case SIOCSIFSLAVE:
2336 case SIOCADDMULTI:
2337 case SIOCDELMULTI:
2338 case SIOCSIFHWBROADCAST:
2339 case SIOCSIFTXQLEN:
2340 case SIOCSIFNAME:
2341 case SIOCSMIIREG:
2342 case SIOCBONDENSLAVE:
2343 case SIOCBONDRELEASE:
2344 case SIOCBONDSETHWADDR:
2345 case SIOCBONDSLAVEINFOQUERY:
2346 case SIOCBONDINFOQUERY:
2347 case SIOCBONDCHANGEACTIVE:
2348 if (!capable(CAP_NET_ADMIN))
2349 return -EPERM;
2350 dev_load(ifr.ifr_name);
2351 dev_probe_lock();
2352 rtnl_lock();
2353 ret = dev_ifsioc(&ifr, cmd);
2354 rtnl_unlock();
2355 dev_probe_unlock();
2356 return ret;
2357
2358 case SIOCGIFMEM:
2359
2360
2361 case SIOCSIFMEM:
2362
2363 case SIOCSIFLINK:
2364 return -EINVAL;
2365
2366
2367
2368
2369
2370 default:
2371 if (cmd == SIOCWANDEV ||
2372 (cmd >= SIOCDEVPRIVATE &&
2373 cmd <= SIOCDEVPRIVATE + 15)) {
2374 dev_load(ifr.ifr_name);
2375 dev_probe_lock();
2376 rtnl_lock();
2377 ret = dev_ifsioc(&ifr, cmd);
2378 rtnl_unlock();
2379 dev_probe_unlock();
2380 if (!ret && copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2381 return -EFAULT;
2382 return ret;
2383 }
2384#ifdef WIRELESS_EXT
2385
2386 if (cmd >= SIOCIWFIRST && cmd <= SIOCIWLAST) {
2387
2388
2389
2390 if (IW_IS_SET(cmd) || (cmd == SIOCGIWENCODE)) {
2391 if(!capable(CAP_NET_ADMIN))
2392 return -EPERM;
2393 }
2394 dev_load(ifr.ifr_name);
2395 rtnl_lock();
2396
2397 ret = wireless_process_ioctl(&ifr, cmd);
2398 rtnl_unlock();
2399 if (!ret && IW_IS_GET(cmd) &&
2400 copy_to_user(arg, &ifr, sizeof(struct ifreq)))
2401 return -EFAULT;
2402 return ret;
2403 }
2404#endif
2405 return -EINVAL;
2406 }
2407}
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418int dev_new_index(void)
2419{
2420 static int ifindex;
2421 for (;;) {
2422 if (++ifindex <= 0)
2423 ifindex=1;
2424 if (__dev_get_by_index(ifindex) == NULL)
2425 return ifindex;
2426 }
2427}
2428
2429static int dev_boot_phase = 1;
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449int net_dev_init(void);
2450
2451int register_netdevice(struct net_device *dev)
2452{
2453 struct net_device *d, **dp;
2454#ifdef CONFIG_NET_DIVERT
2455 int ret;
2456#endif
2457
2458 spin_lock_init(&dev->queue_lock);
2459 spin_lock_init(&dev->xmit_lock);
2460 dev->xmit_lock_owner = -1;
2461#ifdef CONFIG_NET_FASTROUTE
2462 dev->fastpath_lock=RW_LOCK_UNLOCKED;
2463#endif
2464
2465 if (dev_boot_phase)
2466 net_dev_init();
2467
2468#ifdef CONFIG_NET_DIVERT
2469 ret = alloc_divert_blk(dev);
2470 if (ret)
2471 return ret;
2472#endif
2473
2474 dev->iflink = -1;
2475
2476
2477 if (dev->init && dev->init(dev) != 0) {
2478#ifdef CONFIG_NET_DIVERT
2479 free_divert_blk(dev);
2480#endif
2481 return -EIO;
2482 }
2483
2484 dev->ifindex = dev_new_index();
2485 if (dev->iflink == -1)
2486 dev->iflink = dev->ifindex;
2487
2488
2489 for (dp=&dev_base; (d=*dp) != NULL; dp=&d->next) {
2490 if (d == dev || strcmp(d->name, dev->name) == 0) {
2491#ifdef CONFIG_NET_DIVERT
2492 free_divert_blk(dev);
2493#endif
2494 return -EEXIST;
2495 }
2496 }
2497
2498
2499 if ((dev->features & NETIF_F_SG) &&
2500 !(dev->features & (NETIF_F_IP_CSUM |
2501 NETIF_F_NO_CSUM |
2502 NETIF_F_HW_CSUM))) {
2503 printk("%s: Dropping NETIF_F_SG since no checksum feature.\n",
2504 dev->name);
2505 dev->features &= ~NETIF_F_SG;
2506 }
2507
2508
2509
2510
2511
2512
2513 if (dev->rebuild_header == NULL)
2514 dev->rebuild_header = default_rebuild_header;
2515
2516
2517
2518
2519
2520
2521 set_bit(__LINK_STATE_PRESENT, &dev->state);
2522
2523 dev->next = NULL;
2524 dev_init_scheduler(dev);
2525 write_lock_bh(&dev_base_lock);
2526 *dp = dev;
2527 dev_hold(dev);
2528 dev->deadbeaf = 0;
2529 write_unlock_bh(&dev_base_lock);
2530
2531
2532 notifier_call_chain(&netdev_chain, NETDEV_REGISTER, dev);
2533
2534 net_run_sbin_hotplug(dev, "register");
2535
2536 return 0;
2537}
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547int netdev_finish_unregister(struct net_device *dev)
2548{
2549 BUG_TRAP(dev->ip_ptr==NULL);
2550 BUG_TRAP(dev->ip6_ptr==NULL);
2551 BUG_TRAP(dev->dn_ptr==NULL);
2552
2553 if (!dev->deadbeaf) {
2554 printk(KERN_ERR "Freeing alive device %p, %s\n", dev, dev->name);
2555 return 0;
2556 }
2557#ifdef NET_REFCNT_DEBUG
2558 printk(KERN_DEBUG "netdev_finish_unregister: %s%s.\n", dev->name,
2559 (dev->features & NETIF_F_DYNALLOC)?"":", old style");
2560#endif
2561 if (dev->destructor)
2562 dev->destructor(dev);
2563 if (dev->features & NETIF_F_DYNALLOC)
2564 kfree(dev);
2565 return 0;
2566}
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581int unregister_netdevice(struct net_device *dev)
2582{
2583 unsigned long now, warning_time;
2584 struct net_device *d, **dp;
2585
2586
2587 if (dev->flags & IFF_UP)
2588 dev_close(dev);
2589
2590 BUG_TRAP(dev->deadbeaf==0);
2591 dev->deadbeaf = 1;
2592
2593
2594 for (dp = &dev_base; (d=*dp) != NULL; dp=&d->next) {
2595 if (d == dev) {
2596 write_lock_bh(&dev_base_lock);
2597 *dp = d->next;
2598 write_unlock_bh(&dev_base_lock);
2599 break;
2600 }
2601 }
2602 if (d == NULL) {
2603 printk(KERN_DEBUG "unregister_netdevice: device %s/%p never was registered\n", dev->name, dev);
2604 return -ENODEV;
2605 }
2606
2607
2608 br_write_lock_bh(BR_NETPROTO_LOCK);
2609 br_write_unlock_bh(BR_NETPROTO_LOCK);
2610
2611 if (dev_boot_phase == 0) {
2612#ifdef CONFIG_NET_FASTROUTE
2613 dev_clear_fastroute(dev);
2614#endif
2615
2616
2617 dev_shutdown(dev);
2618
2619 net_run_sbin_hotplug(dev, "unregister");
2620
2621
2622
2623
2624 notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
2625
2626
2627
2628
2629 dev_mc_discard(dev);
2630 }
2631
2632 if (dev->uninit)
2633 dev->uninit(dev);
2634
2635
2636 BUG_TRAP(dev->master==NULL);
2637
2638#ifdef CONFIG_NET_DIVERT
2639 free_divert_blk(dev);
2640#endif
2641
2642 if (dev->features & NETIF_F_DYNALLOC) {
2643#ifdef NET_REFCNT_DEBUG
2644 if (atomic_read(&dev->refcnt) != 1)
2645 printk(KERN_DEBUG "unregister_netdevice: holding %s refcnt=%d\n", dev->name, atomic_read(&dev->refcnt)-1);
2646#endif
2647 dev_put(dev);
2648 return 0;
2649 }
2650
2651
2652 if (atomic_read(&dev->refcnt) == 1) {
2653 dev_put(dev);
2654 return 0;
2655 }
2656
2657#ifdef NET_REFCNT_DEBUG
2658 printk("unregister_netdevice: waiting %s refcnt=%d\n", dev->name, atomic_read(&dev->refcnt));
2659#endif
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684 now = warning_time = jiffies;
2685 while (atomic_read(&dev->refcnt) != 1) {
2686 if ((jiffies - now) > 1*HZ) {
2687
2688 notifier_call_chain(&netdev_chain, NETDEV_UNREGISTER, dev);
2689 }
2690 current->state = TASK_INTERRUPTIBLE;
2691 schedule_timeout(HZ/4);
2692 current->state = TASK_RUNNING;
2693 if ((jiffies - warning_time) > 10*HZ) {
2694 printk(KERN_EMERG "unregister_netdevice: waiting for %s to "
2695 "become free. Usage count = %d\n",
2696 dev->name, atomic_read(&dev->refcnt));
2697 warning_time = jiffies;
2698 }
2699 }
2700 dev_put(dev);
2701 return 0;
2702}
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712extern void net_device_init(void);
2713extern void ip_auto_config(void);
2714struct proc_dir_entry *proc_net_drivers;
2715#ifdef CONFIG_NET_DIVERT
2716extern void dv_init(void);
2717#endif
2718
2719
2720
2721
2722
2723
2724int __init net_dev_init(void)
2725{
2726 struct net_device *dev, **dp;
2727 int i;
2728
2729 if (!dev_boot_phase)
2730 return 0;
2731
2732
2733#ifdef CONFIG_NET_DIVERT
2734 dv_init();
2735#endif
2736
2737
2738
2739
2740
2741 for (i = 0; i < NR_CPUS; i++) {
2742 struct softnet_data *queue;
2743
2744 queue = &softnet_data[i];
2745 skb_queue_head_init(&queue->input_pkt_queue);
2746 queue->throttle = 0;
2747 queue->cng_level = 0;
2748 queue->avg_blog = 10;
2749 queue->completion_queue = NULL;
2750 INIT_LIST_HEAD(&queue->poll_list);
2751 set_bit(__LINK_STATE_START, &queue->blog_dev.state);
2752 queue->blog_dev.weight = weight_p;
2753 queue->blog_dev.poll = process_backlog;
2754 atomic_set(&queue->blog_dev.refcnt, 1);
2755 }
2756
2757#ifdef CONFIG_NET_PROFILE
2758 net_profile_init();
2759 NET_PROFILE_REGISTER(dev_queue_xmit);
2760 NET_PROFILE_REGISTER(softnet_process);
2761#endif
2762
2763#ifdef OFFLINE_SAMPLE
2764 samp_timer.expires = jiffies + (10 * HZ);
2765 add_timer(&samp_timer);
2766#endif
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778 dp = &dev_base;
2779 while ((dev = *dp) != NULL) {
2780 spin_lock_init(&dev->queue_lock);
2781 spin_lock_init(&dev->xmit_lock);
2782#ifdef CONFIG_NET_FASTROUTE
2783 dev->fastpath_lock = RW_LOCK_UNLOCKED;
2784#endif
2785 dev->xmit_lock_owner = -1;
2786 dev->iflink = -1;
2787 dev_hold(dev);
2788
2789
2790
2791
2792
2793 if (strchr(dev->name, '%'))
2794 dev_alloc_name(dev, dev->name);
2795
2796
2797
2798
2799 netdev_boot_setup_check(dev);
2800
2801 if (dev->init && dev->init(dev)) {
2802
2803
2804
2805
2806
2807 dev->deadbeaf = 1;
2808 dp = &dev->next;
2809 } else {
2810 dp = &dev->next;
2811 dev->ifindex = dev_new_index();
2812 if (dev->iflink == -1)
2813 dev->iflink = dev->ifindex;
2814 if (dev->rebuild_header == NULL)
2815 dev->rebuild_header = default_rebuild_header;
2816 dev_init_scheduler(dev);
2817 set_bit(__LINK_STATE_PRESENT, &dev->state);
2818 }
2819 }
2820
2821
2822
2823
2824 dp = &dev_base;
2825 while ((dev = *dp) != NULL) {
2826 if (dev->deadbeaf) {
2827 write_lock_bh(&dev_base_lock);
2828 *dp = dev->next;
2829 write_unlock_bh(&dev_base_lock);
2830 dev_put(dev);
2831 } else {
2832 dp = &dev->next;
2833 }
2834 }
2835
2836#ifdef CONFIG_PROC_FS
2837 proc_net_create("dev", 0, dev_get_info);
2838 create_proc_read_entry("net/softnet_stat", 0, 0, dev_proc_stats, NULL);
2839 proc_net_drivers = proc_mkdir("net/drivers", 0);
2840#ifdef WIRELESS_EXT
2841
2842 proc_net_create("wireless", 0, dev_get_wireless_info);
2843#endif
2844#endif
2845
2846 dev_boot_phase = 0;
2847
2848 open_softirq(NET_TX_SOFTIRQ, net_tx_action, NULL);
2849 open_softirq(NET_RX_SOFTIRQ, net_rx_action, NULL);
2850
2851 dst_init();
2852 dev_mcast_init();
2853
2854#ifdef CONFIG_NET_SCHED
2855 pktsched_init();
2856#endif
2857
2858
2859
2860
2861 net_device_init();
2862
2863 return 0;
2864}
2865
2866#ifdef CONFIG_HOTPLUG
2867
2868
2869
2870
2871
2872
2873static int net_run_sbin_hotplug(struct net_device *dev, char *action)
2874{
2875 char *argv[3], *envp[5], ifname[12 + IFNAMSIZ], action_str[32];
2876 int i;
2877
2878 sprintf(ifname, "INTERFACE=%s", dev->name);
2879 sprintf(action_str, "ACTION=%s", action);
2880
2881 i = 0;
2882 argv[i++] = hotplug_path;
2883 argv[i++] = "net";
2884 argv[i] = 0;
2885
2886 i = 0;
2887
2888 envp [i++] = "HOME=/";
2889 envp [i++] = "PATH=/sbin:/bin:/usr/sbin:/usr/bin";
2890 envp [i++] = ifname;
2891 envp [i++] = action_str;
2892 envp [i] = 0;
2893
2894 return call_usermodehelper(argv [0], argv, envp);
2895}
2896#endif
2897