1 /*
2 * NET3 Protocol independent device support routines.
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Derived from the non IP parts of dev.c 1.0.19
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Mark Evans, <evansmp@uhura.aston.ac.uk>
13 *
14 * Additional Authors:
15 * Florian la Roche <rzsfl@rz.uni-sb.de>
16 * Alan Cox <gw4pts@gw4pts.ampr.org>
17 * David Hinds <dhinds@allegro.stanford.edu>
18 *
19 * Changes:
20 * Alan Cox : device private ioctl copies fields back.
21 * Alan Cox : Transmit queue code does relevant stunts to
22 * keep the queue safe.
23 * Alan Cox : Fixed double lock.
24 * Alan Cox : Fixed promisc NULL pointer trap
25 * ???????? : Support the full private ioctl range
26 * Alan Cox : Moved ioctl permission check into drivers
27 * Tim Kordas : SIOCADDMULTI/SIOCDELMULTI
28 * Alan Cox : 100 backlog just doesn't cut it when
29 * you start doing multicast video 8)
30 * Alan Cox : Rewrote net_bh and list manager.
31 * Alan Cox : Fix ETH_P_ALL echoback lengths.
32 * Alan Cox : Took out transmit every packet pass
33 * Saved a few bytes in the ioctl handler
34 * Alan Cox : Network driver sets packet type before calling netif_rx. Saves
35 * a function call a packet.
36 * Alan Cox : Hashed net_bh()
37 * Richard Kooijman: Timestamp fixes.
38 * Alan Cox : Wrong field in SIOCGIFDSTADDR
39 * Alan Cox : Device lock protection.
40 * Alan Cox : Fixed nasty side effect of device close changes.
41 * Rudi Cilibrasi : Pass the right thing to set_mac_address()
42 * Dave Miller : 32bit quantity for the device lock to make it work out
43 * on a Sparc.
44 * Bjorn Ekwall : Added KERNELD hack.
45 * Alan Cox : Cleaned up the backlog initialise.
46 * Craig Metz : SIOCGIFCONF fix if space for under
47 * 1 device.
48 *
49 */
50
51 #include <asm/segment.h>
52 #include <asm/system.h>
53 #include <asm/bitops.h>
54 #include <linux/config.h>
55 #include <linux/types.h>
56 #include <linux/kernel.h>
57 #include <linux/sched.h>
58 #include <linux/string.h>
59 #include <linux/mm.h>
60 #include <linux/socket.h>
61 #include <linux/sockios.h>
62 #include <linux/in.h>
63 #include <linux/errno.h>
64 #include <linux/interrupt.h>
65 #include <linux/if_ether.h>
66 #include <linux/inet.h>
67 #include <linux/netdevice.h>
68 #include <linux/etherdevice.h>
69 #include <linux/notifier.h>
70 #include <net/ip.h>
71 #include <net/route.h>
72 #include <linux/skbuff.h>
73 #include <net/sock.h>
74 #include <net/arp.h>
75 #include <net/slhc.h>
76 #include <linux/proc_fs.h>
77 #include <linux/stat.h>
78 #ifdef CONFIG_NET_ALIAS
79 #include <linux/net_alias.h>
80 #endif
81 #ifdef CONFIG_KERNELD
82 #include <linux/kerneld.h>
83 #endif
84
85 /*
86 * The list of packet types we will receive (as opposed to discard)
87 * and the routines to invoke.
88 */
89
90 struct packet_type *ptype_base[16];
91 struct packet_type *ptype_all = NULL; /* Taps */
92
93 /*
94 * Device list lock
95 */
96
97 int dev_lockct=0;
98
99 /*
100 * Our notifier list
101 */
102
103 struct notifier_block *netdev_chain=NULL;
104
105 /*
106 * Device drivers call our routines to queue packets here. We empty the
107 * queue in the bottom half handler.
108 */
109
110 static struct sk_buff_head backlog;
111
112 /*
113 * We don't overdo the queue or we will thrash memory badly.
114 */
115
116 static int backlog_size = 0;
117
118 /*
119 * Return the lesser of the two values.
120 */
121
122 static __inline__ unsigned long min(unsigned long a, unsigned long b)
/* ![[previous]](../icons/n_left.png)
![[next]](../icons/right.png)
![[first]](../icons/n_first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
123 {
124 return (a < b)? a : b;
125 }
126
127
128 /******************************************************************************************
129
130 Protocol management and registration routines
131
132 *******************************************************************************************/
133
134 /*
135 * For efficiency
136 */
137
138 static int dev_nit=0;
139
140 /*
141 * Add a protocol ID to the list. Now that the input handler is
142 * smarter we can dispense with all the messy stuff that used to be
143 * here.
144 */
145
146 void dev_add_pack(struct packet_type *pt)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
147 {
148 int hash;
149 if(pt->type==htons(ETH_P_ALL))
150 {
151 dev_nit++;
152 pt->next=ptype_all;
153 ptype_all=pt;
154 }
155 else
156 {
157 hash=ntohs(pt->type)&15;
158 pt->next = ptype_base[hash];
159 ptype_base[hash] = pt;
160 }
161 }
162
163
164 /*
165 * Remove a protocol ID from the list.
166 */
167
168 void dev_remove_pack(struct packet_type *pt)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
169 {
170 struct packet_type **pt1;
171 if(pt->type==htons(ETH_P_ALL))
172 {
173 dev_nit--;
174 pt1=&ptype_all;
175 }
176 else
177 pt1=&ptype_base[ntohs(pt->type)&15];
178 for(; (*pt1)!=NULL; pt1=&((*pt1)->next))
179 {
180 if(pt==(*pt1))
181 {
182 *pt1=pt->next;
183 return;
184 }
185 }
186 printk("dev_remove_pack: %p not found.\n", pt);
187 }
188
189 /*****************************************************************************************
190
191 Device Interface Subroutines
192
193 ******************************************************************************************/
194
195 /*
196 * Find an interface by name.
197 */
198
199 struct device *dev_get(const char *name)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
200 {
201 struct device *dev;
202
203 for (dev = dev_base; dev != NULL; dev = dev->next)
204 {
205 if (strcmp(dev->name, name) == 0)
206 return(dev);
207 }
208 return NULL;
209 }
210
211 /*
212 * Find and possibly load an interface.
213 */
214
215 #ifdef CONFIG_KERNELD
216
217 extern __inline__ void dev_load(const char *name)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
218 {
219 if(!dev_get(name)) {
220 #ifdef CONFIG_NET_ALIAS
221 const char *sptr;
222
223 for (sptr=name ; *sptr ; sptr++) if(*sptr==':') break;
224 if (!(*sptr && *(sptr+1)))
225 #endif
226 request_module(name);
227 }
228 }
229
230 #endif
231
232 /*
233 * Prepare an interface for use.
234 */
235
236 int dev_open(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
237 {
238 int ret = 0;
239
240 /*
241 * Call device private open method
242 */
243 if (dev->open)
244 ret = dev->open(dev);
245
246 /*
247 * If it went open OK then set the flags
248 */
249
250 if (ret == 0)
251 {
252 dev->flags |= (IFF_UP | IFF_RUNNING);
253 /*
254 * Initialise multicasting status
255 */
256 dev_mc_upload(dev);
257 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
258 }
259 return(ret);
260 }
261
262
263 /*
264 * Completely shutdown an interface.
265 */
266
267 int dev_close(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
268 {
269 int ct=0;
270
271 /*
272 * Call the device specific close. This cannot fail.
273 * Only if device is UP
274 */
275
276 if ((dev->flags & IFF_UP) && dev->stop)
277 dev->stop(dev);
278
279 /*
280 * Device is now down.
281 */
282
283 dev->flags&=~(IFF_UP|IFF_RUNNING);
284
285 /*
286 * Tell people we are going down
287 */
288 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
289 /*
290 * Flush the multicast chain
291 */
292 dev_mc_discard(dev);
293 /*
294 * Blank the IP addresses
295 */
296 dev->pa_addr = 0;
297 dev->pa_dstaddr = 0;
298 dev->pa_brdaddr = 0;
299 dev->pa_mask = 0;
300 /*
301 * Purge any queued packets when we down the link
302 */
303 while(ct<DEV_NUMBUFFS)
304 {
305 struct sk_buff *skb;
306 while((skb=skb_dequeue(&dev->buffs[ct]))!=NULL)
307 if(skb->free)
308 kfree_skb(skb,FREE_WRITE);
309 ct++;
310 }
311 return(0);
312 }
313
314
315 /*
316 * Device change register/unregister. These are not inline or static
317 * as we export them to the world.
318 */
319
320 int register_netdevice_notifier(struct notifier_block *nb)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
321 {
322 return notifier_chain_register(&netdev_chain, nb);
323 }
324
325 int unregister_netdevice_notifier(struct notifier_block *nb)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
326 {
327 return notifier_chain_unregister(&netdev_chain,nb);
328 }
329
330 /*
331 * Send (or queue for sending) a packet.
332 *
333 * IMPORTANT: When this is called to resend frames. The caller MUST
334 * already have locked the sk_buff. Apart from that we do the
335 * rest of the magic.
336 */
337
338 void dev_queue_xmit(struct sk_buff *skb, struct device *dev, int pri)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
339 {
340 unsigned long flags;
341 struct sk_buff_head *list;
342 int retransmission = 0; /* used to say if the packet should go */
343 /* at the front or the back of the */
344 /* queue - front is a retransmit try */
345
346 if(pri>=0 && !skb_device_locked(skb))
347 skb_device_lock(skb); /* Shove a lock on the frame */
348 #if CONFIG_SKB_CHECK
349 IS_SKB(skb);
350 #endif
351 skb->dev = dev;
352
353 /*
354 * Negative priority is used to flag a frame that is being pulled from the
355 * queue front as a retransmit attempt. It therefore goes back on the queue
356 * start on a failure.
357 */
358
359 if (pri < 0)
360 {
361 pri = -pri-1;
362 retransmission = 1;
363 }
364
365 #ifdef CONFIG_NET_DEBUG
366 if (pri >= DEV_NUMBUFFS)
367 {
368 printk("bad priority in dev_queue_xmit.\n");
369 pri = 1;
370 }
371 #endif
372
373 /*
374 * If the address has not been resolved. Call the device header rebuilder.
375 * This can cover all protocols and technically not just ARP either.
376 */
377
378 if (!skb->arp && dev->rebuild_header(skb->data, dev, skb->raddr, skb)) {
379 return;
380 }
381
382 /*
383 *
384 * If dev is an alias, switch to its main device.
385 * "arp" resolution has been made with alias device, so
386 * arp entries refer to alias, not main.
387 *
388 */
389
390 #ifdef CONFIG_NET_ALIAS
391 if (net_alias_is(dev))
392 skb->dev = dev = net_alias_main_dev(dev);
393 #endif
394 list = dev->buffs + pri;
395
396 save_flags(flags);
397 /* if this isn't a retransmission, use the first packet instead... */
398 if (!retransmission) {
399 if (skb_queue_len(list)) {
400 /* avoid overrunning the device queue.. */
401 if (skb_queue_len(list) > dev->tx_queue_len) {
402 dev_kfree_skb(skb, FREE_WRITE);
403 return;
404 }
405 cli();
406 skb_device_unlock(skb); /* Buffer is on the device queue and can be freed safely */
407 __skb_queue_tail(list, skb);
408 skb = __skb_dequeue(list);
409 skb_device_lock(skb); /* New buffer needs locking down */
410 restore_flags(flags);
411 }
412
413 /* copy outgoing packets to any sniffer packet handlers */
414 if (dev_nit) {
415 struct packet_type *ptype;
416 skb->stamp=xtime;
417 for (ptype = ptype_all; ptype!=NULL; ptype = ptype->next)
418 {
419 /* Never send packets back to the socket
420 * they originated from - MvS (miquels@drinkel.ow.org)
421 */
422 if ((ptype->dev == dev || !ptype->dev) &&
423 ((struct sock *)ptype->data != skb->sk))
424 {
425 struct sk_buff *skb2;
426 if ((skb2 = skb_clone(skb, GFP_ATOMIC)) == NULL)
427 break;
428 skb2->h.raw = skb2->data + dev->hard_header_len;
429 skb2->mac.raw = skb2->data;
430 ptype->func(skb2, skb->dev, ptype);
431 }
432 }
433 }
434 }
435 start_bh_atomic();
436 if (dev->hard_start_xmit(skb, dev) == 0) {
437 /*
438 * Packet is now solely the responsibility of the driver
439 */
440 end_bh_atomic();
441 return;
442 }
443 end_bh_atomic();
444
445 /*
446 * Transmission failed, put skb back into a list. Once on the list it's safe and
447 * no longer device locked (it can be freed safely from the device queue)
448 */
449 cli();
450 skb_device_unlock(skb);
451 __skb_queue_head(list,skb);
452 restore_flags(flags);
453 }
454
455 /*
456 * Receive a packet from a device driver and queue it for the upper
457 * (protocol) levels. It always succeeds. This is the recommended
458 * interface to use.
459 */
460
461 void netif_rx(struct sk_buff *skb)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
462 {
463 static int dropping = 0;
464
465 /*
466 * Any received buffers are un-owned and should be discarded
467 * when freed. These will be updated later as the frames get
468 * owners.
469 */
470
471 skb->sk = NULL;
472 skb->free = 1;
473 if(skb->stamp.tv_sec==0)
474 skb->stamp = xtime;
475
476 /*
477 * Check that we aren't overdoing things.
478 */
479
480 if (!backlog_size)
481 dropping = 0;
482 else if (backlog_size > 300)
483 dropping = 1;
484
485 if (dropping)
486 {
487 kfree_skb(skb, FREE_READ);
488 return;
489 }
490
491 /*
492 * Add it to the "backlog" queue.
493 */
494 #if CONFIG_SKB_CHECK
495 IS_SKB(skb);
496 #endif
497 skb_queue_tail(&backlog,skb);
498 backlog_size++;
499
500 /*
501 * If any packet arrived, mark it for processing after the
502 * hardware interrupt returns.
503 */
504
505 #ifdef CONFIG_NET_RUNONIRQ /* Dont enable yet, needs some driver mods */
506 net_bh();
507 #else
508 mark_bh(NET_BH);
509 #endif
510 return;
511 }
512
513 /*
514 * This routine causes all interfaces to try to send some data.
515 */
516
517 void dev_transmit(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
518 {
519 struct device *dev;
520
521 for (dev = dev_base; dev != NULL; dev = dev->next)
522 {
523 if (dev->flags != 0 && !dev->tbusy) {
524 /*
525 * Kick the device
526 */
527 dev_tint(dev);
528 }
529 }
530 }
531
532
533 /**********************************************************************************
534
535 Receive Queue Processor
536
537 ***********************************************************************************/
538
539 /*
540 * When we are called the queue is ready to grab, the interrupts are
541 * on and hardware can interrupt and queue to the receive queue a we
542 * run with no problems.
543 * This is run as a bottom half after an interrupt handler that does
544 * mark_bh(NET_BH);
545 */
546
547 void net_bh(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
548 {
549 struct packet_type *ptype;
550 struct packet_type *pt_prev;
551 unsigned short type;
552
553 /*
554 * Can we send anything now? We want to clear the
555 * decks for any more sends that get done as we
556 * process the input. This also minimises the
557 * latency on a transmit interrupt bh.
558 */
559
560 dev_transmit();
561
562 /*
563 * Any data left to process. This may occur because a
564 * mark_bh() is done after we empty the queue including
565 * that from the device which does a mark_bh() just after
566 */
567
568 /*
569 * While the queue is not empty..
570 *
571 * Note that the queue never shrinks due to
572 * an interrupt, so we can do this test without
573 * disabling interrupts.
574 */
575
576 while (!skb_queue_empty(&backlog)) {
577 struct sk_buff * skb = backlog.next;
578
579 /*
580 * We have a packet. Therefore the queue has shrunk
581 */
582 cli();
583 __skb_unlink(skb, &backlog);
584 backlog_size--;
585 sti();
586
587 /*
588 * Bump the pointer to the next structure.
589 *
590 * On entry to the protocol layer. skb->data and
591 * skb->h.raw point to the MAC and encapsulated data
592 */
593
594 skb->h.raw = skb->data;
595
596 /*
597 * Fetch the packet protocol ID.
598 */
599
600 type = skb->protocol;
601
602 /*
603 * We got a packet ID. Now loop over the "known protocols"
604 * list. There are two lists. The ptype_all list of taps (normally empty)
605 * and the main protocol list which is hashed perfectly for normal protocols.
606 */
607 pt_prev = NULL;
608 for (ptype = ptype_all; ptype!=NULL; ptype=ptype->next)
609 {
610 if(pt_prev)
611 {
612 struct sk_buff *skb2=skb_clone(skb, GFP_ATOMIC);
613 if(skb2)
614 pt_prev->func(skb2,skb->dev, pt_prev);
615 }
616 pt_prev=ptype;
617 }
618
619 for (ptype = ptype_base[ntohs(type)&15]; ptype != NULL; ptype = ptype->next)
620 {
621 if (ptype->type == type && (!ptype->dev || ptype->dev==skb->dev))
622 {
623 /*
624 * We already have a match queued. Deliver
625 * to it and then remember the new match
626 */
627 if(pt_prev)
628 {
629 struct sk_buff *skb2;
630
631 skb2=skb_clone(skb, GFP_ATOMIC);
632
633 /*
634 * Kick the protocol handler. This should be fast
635 * and efficient code.
636 */
637
638 if(skb2)
639 pt_prev->func(skb2, skb->dev, pt_prev);
640 }
641 /* Remember the current last to do */
642 pt_prev=ptype;
643 }
644 } /* End of protocol list loop */
645
646 /*
647 * Is there a last item to send to ?
648 */
649
650 if(pt_prev)
651 pt_prev->func(skb, skb->dev, pt_prev);
652 /*
653 * Has an unknown packet has been received ?
654 */
655
656 else
657 kfree_skb(skb, FREE_WRITE);
658 /*
659 * Again, see if we can transmit anything now.
660 * [Ought to take this out judging by tests it slows
661 * us down not speeds us up]
662 */
663 #ifdef XMIT_EVERY
664 dev_transmit();
665 #endif
666 } /* End of queue loop */
667
668 /*
669 * We have emptied the queue
670 */
671
672 /*
673 * One last output flush.
674 */
675
676 #ifdef XMIT_AFTER
677 dev_transmit();
678 #endif
679 }
680
681
682 /*
683 * This routine is called when an device driver (i.e. an
684 * interface) is ready to transmit a packet.
685 */
686
687 void dev_tint(struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
688 {
689 int i;
690 unsigned long flags;
691 struct sk_buff_head * head;
692
693 /*
694 * aliases do not transmit (for now :) )
695 */
696
697 #ifdef CONFIG_NET_ALIAS
698 if (net_alias_is(dev)) return;
699 #endif
700 head = dev->buffs;
701 save_flags(flags);
702 cli();
703
704 /*
705 * Work the queues in priority order
706 */
707 for(i = 0;i < DEV_NUMBUFFS; i++,head++)
708 {
709
710 while (!skb_queue_empty(head)) {
711 struct sk_buff *skb;
712
713 skb = head->next;
714 __skb_unlink(skb, head);
715 /*
716 * Stop anyone freeing the buffer while we retransmit it
717 */
718 skb_device_lock(skb);
719 restore_flags(flags);
720 /*
721 * Feed them to the output stage and if it fails
722 * indicate they re-queue at the front.
723 */
724 dev_queue_xmit(skb,dev,-i - 1);
725 /*
726 * If we can take no more then stop here.
727 */
728 if (dev->tbusy)
729 return;
730 cli();
731 }
732 }
733 restore_flags(flags);
734 }
735
736
737 /*
738 * Perform a SIOCGIFCONF call. This structure will change
739 * size shortly, and there is nothing I can do about it.
740 * Thus we will need a 'compatibility mode'.
741 */
742
743 static int dev_ifconf(char *arg)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
744 {
745 struct ifconf ifc;
746 struct ifreq ifr;
747 struct device *dev;
748 char *pos;
749 int len;
750 int err;
751
752 /*
753 * Fetch the caller's info block.
754 */
755
756 err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifconf));
757 if(err)
758 return err;
759 memcpy_fromfs(&ifc, arg, sizeof(struct ifconf));
760 len = ifc.ifc_len;
761 pos = ifc.ifc_buf;
762
763 /*
764 * We now walk the device list filling each active device
765 * into the array.
766 */
767
768 err=verify_area(VERIFY_WRITE,pos,len);
769 if(err)
770 return err;
771
772 /*
773 * Loop over the interfaces, and write an info block for each.
774 */
775
776 for (dev = dev_base; dev != NULL; dev = dev->next)
777 {
778 if(!(dev->flags & IFF_UP)) /* Downed devices don't count */
779 continue;
780 /*
781 * Have we run out of space here ?
782 */
783
784 if (len < sizeof(struct ifreq))
785 break;
786
787 memset(&ifr, 0, sizeof(struct ifreq));
788 strcpy(ifr.ifr_name, dev->name);
789 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_family = dev->family;
790 (*(struct sockaddr_in *) &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
791
792
793 /*
794 * Write this block to the caller's space.
795 */
796
797 memcpy_tofs(pos, &ifr, sizeof(struct ifreq));
798 pos += sizeof(struct ifreq);
799 len -= sizeof(struct ifreq);
800 }
801
802 /*
803 * All done. Write the updated control block back to the caller.
804 */
805
806 ifc.ifc_len = (pos - ifc.ifc_buf);
807 ifc.ifc_req = (struct ifreq *) ifc.ifc_buf;
808 memcpy_tofs(arg, &ifc, sizeof(struct ifconf));
809
810 /*
811 * Report how much was filled in
812 */
813
814 return(pos - arg);
815 }
816
817
818 /*
819 * This is invoked by the /proc filesystem handler to display a device
820 * in detail.
821 */
822
823 #ifdef CONFIG_PROC_FS
824 static int sprintf_stats(char *buffer, struct device *dev)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
825 {
826 struct enet_statistics *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
827 int size;
828
829 if (stats)
830 size = sprintf(buffer, "%6s:%7d %4d %4d %4d %4d %8d %4d %4d %4d %5d %4d\n",
831 dev->name,
832 stats->rx_packets, stats->rx_errors,
833 stats->rx_dropped + stats->rx_missed_errors,
834 stats->rx_fifo_errors,
835 stats->rx_length_errors + stats->rx_over_errors
836 + stats->rx_crc_errors + stats->rx_frame_errors,
837 stats->tx_packets, stats->tx_errors, stats->tx_dropped,
838 stats->tx_fifo_errors, stats->collisions,
839 stats->tx_carrier_errors + stats->tx_aborted_errors
840 + stats->tx_window_errors + stats->tx_heartbeat_errors);
841 else
842 size = sprintf(buffer, "%6s: No statistics available.\n", dev->name);
843
844 return size;
845 }
846
847 /*
848 * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
849 * to create /proc/net/dev
850 */
851
852 int dev_get_info(char *buffer, char **start, off_t offset, int length, int dummy)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
853 {
854 int len=0;
855 off_t begin=0;
856 off_t pos=0;
857 int size;
858
859 struct device *dev;
860
861
862 size = sprintf(buffer, "Inter-| Receive | Transmit\n"
863 " face |packets errs drop fifo frame|packets errs drop fifo colls carrier\n");
864
865 pos+=size;
866 len+=size;
867
868
869 for (dev = dev_base; dev != NULL; dev = dev->next)
870 {
871 size = sprintf_stats(buffer+len, dev);
872 len+=size;
873 pos=begin+len;
874
875 if(pos<offset)
876 {
877 len=0;
878 begin=pos;
879 }
880 if(pos>offset+length)
881 break;
882 }
883
884 *start=buffer+(offset-begin); /* Start of wanted data */
885 len-=(offset-begin); /* Start slop */
886 if(len>length)
887 len=length; /* Ending slop */
888 return len;
889 }
890 #endif /* CONFIG_PROC_FS */
891
892
893 /*
894 * This checks bitmasks for the ioctl calls for devices.
895 */
896
897 static inline int bad_mask(unsigned long mask, unsigned long addr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
898 {
899 if (addr & (mask = ~mask))
900 return 1;
901 mask = ntohl(mask);
902 if (mask & (mask+1))
903 return 1;
904 return 0;
905 }
906
907 /*
908 * Perform the SIOCxIFxxx calls.
909 *
910 * The socket layer has seen an ioctl the address family thinks is
911 * for the device. At this point we get invoked to make a decision
912 */
913
914 static int dev_ifsioc(void *arg, unsigned int getset)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
915 {
916 struct ifreq ifr;
917 struct device *dev;
918 int ret;
919
920 /*
921 * Fetch the caller's info block into kernel space
922 */
923
924 int err=verify_area(VERIFY_WRITE, arg, sizeof(struct ifreq));
925 if(err)
926 return err;
927
928 memcpy_fromfs(&ifr, arg, sizeof(struct ifreq));
929
930 /*
931 * See which interface the caller is talking about.
932 */
933
934 /*
935 *
936 * net_alias_dev_get(): dev_get() with added alias naming magic.
937 * only allow alias creation/deletion if (getset==SIOCSIFADDR)
938 *
939 */
940
941 #ifdef CONFIG_KERNELD
942 dev_load(ifr.ifr_name);
943 #endif
944
945 #ifdef CONFIG_NET_ALIAS
946 if ((dev = net_alias_dev_get(ifr.ifr_name, getset == SIOCSIFADDR, &err, NULL, NULL)) == NULL)
947 return(err);
948 #else
949 if ((dev = dev_get(ifr.ifr_name)) == NULL)
950 return(-ENODEV);
951 #endif
952 switch(getset)
953 {
954 case SIOCGIFFLAGS: /* Get interface flags */
955 ifr.ifr_flags = dev->flags;
956 goto rarok;
957
958 case SIOCSIFFLAGS: /* Set interface flags */
959 {
960 int old_flags = dev->flags;
961
962 /*
963 * We are not allowed to potentially close/unload
964 * a device until we get this lock.
965 */
966
967 dev_lock_wait();
968
969 /*
970 * Set the flags on our device.
971 */
972
973 dev->flags = (ifr.ifr_flags & (
974 IFF_BROADCAST | IFF_DEBUG | IFF_LOOPBACK |
975 IFF_POINTOPOINT | IFF_NOTRAILERS | IFF_RUNNING |
976 IFF_NOARP | IFF_PROMISC | IFF_ALLMULTI | IFF_SLAVE | IFF_MASTER
977 | IFF_MULTICAST)) | (dev->flags & IFF_UP);
978 /*
979 * Load in the correct multicast list now the flags have changed.
980 */
981
982 dev_mc_upload(dev);
983
984 /*
985 * Have we downed the interface. We handle IFF_UP ourselves
986 * according to user attempts to set it, rather than blindly
987 * setting it.
988 */
989
990 if ((old_flags^ifr.ifr_flags)&IFF_UP) /* Bit is different ? */
991 {
992 if(old_flags&IFF_UP) /* Gone down */
993 ret=dev_close(dev);
994 else /* Come up */
995 {
996 ret=dev_open(dev);
997 if(ret<0)
998 dev->flags&=~IFF_UP; /* Open failed */
999 }
1000 }
1001 else
1002 ret=0;
1003 /*
1004 * Load in the correct multicast list now the flags have changed.
1005 */
1006
1007 dev_mc_upload(dev);
1008 }
1009 break;
1010
1011 case SIOCGIFADDR: /* Get interface address (and family) */
1012 if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1013 {
1014 memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1015 ifr.ifr_hwaddr.sa_family=dev->type;
1016 goto rarok;
1017 }
1018 else
1019 {
1020 (*(struct sockaddr_in *)
1021 &ifr.ifr_addr).sin_addr.s_addr = dev->pa_addr;
1022 (*(struct sockaddr_in *)
1023 &ifr.ifr_addr).sin_family = dev->family;
1024 (*(struct sockaddr_in *)
1025 &ifr.ifr_addr).sin_port = 0;
1026 }
1027 goto rarok;
1028
1029 case SIOCSIFADDR: /* Set interface address (and family) */
1030
1031 /*
1032 * BSDism. SIOCSIFADDR family=AF_UNSPEC sets the
1033 * physical address. We can cope with this now.
1034 */
1035
1036 if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1037 {
1038 if(dev->set_mac_address==NULL)
1039 return -EOPNOTSUPP;
1040 ret=dev->set_mac_address(dev,&ifr.ifr_addr);
1041 }
1042 else
1043 {
1044
1045 /*
1046 * if dev is an alias, must rehash to update
1047 * address change
1048 */
1049
1050 #ifdef CONFIG_NET_ALIAS
1051 if (net_alias_is(dev))
1052 net_alias_dev_rehash(dev ,&ifr.ifr_addr);
1053 #endif
1054 dev->pa_addr = (*(struct sockaddr_in *)
1055 &ifr.ifr_addr).sin_addr.s_addr;
1056 dev->family = ifr.ifr_addr.sa_family;
1057
1058 #ifdef CONFIG_INET
1059 /* This is naughty. When net-032e comes out It wants moving into the net032
1060 code not the kernel. Till then it can sit here (SIGH) */
1061 dev->pa_mask = ip_get_mask(dev->pa_addr);
1062 #endif
1063 dev->pa_brdaddr = dev->pa_addr | ~dev->pa_mask;
1064 ret = 0;
1065 }
1066 break;
1067
1068 case SIOCGIFBRDADDR: /* Get the broadcast address */
1069 (*(struct sockaddr_in *)
1070 &ifr.ifr_broadaddr).sin_addr.s_addr = dev->pa_brdaddr;
1071 (*(struct sockaddr_in *)
1072 &ifr.ifr_broadaddr).sin_family = dev->family;
1073 (*(struct sockaddr_in *)
1074 &ifr.ifr_broadaddr).sin_port = 0;
1075 goto rarok;
1076
1077 case SIOCSIFBRDADDR: /* Set the broadcast address */
1078 dev->pa_brdaddr = (*(struct sockaddr_in *)
1079 &ifr.ifr_broadaddr).sin_addr.s_addr;
1080 ret = 0;
1081 break;
1082
1083 case SIOCGIFDSTADDR: /* Get the destination address (for point-to-point links) */
1084 (*(struct sockaddr_in *)
1085 &ifr.ifr_dstaddr).sin_addr.s_addr = dev->pa_dstaddr;
1086 (*(struct sockaddr_in *)
1087 &ifr.ifr_dstaddr).sin_family = dev->family;
1088 (*(struct sockaddr_in *)
1089 &ifr.ifr_dstaddr).sin_port = 0;
1090 goto rarok;
1091
1092 case SIOCSIFDSTADDR: /* Set the destination address (for point-to-point links) */
1093 dev->pa_dstaddr = (*(struct sockaddr_in *)
1094 &ifr.ifr_dstaddr).sin_addr.s_addr;
1095 ret = 0;
1096 break;
1097
1098 case SIOCGIFNETMASK: /* Get the netmask for the interface */
1099 (*(struct sockaddr_in *)
1100 &ifr.ifr_netmask).sin_addr.s_addr = dev->pa_mask;
1101 (*(struct sockaddr_in *)
1102 &ifr.ifr_netmask).sin_family = dev->family;
1103 (*(struct sockaddr_in *)
1104 &ifr.ifr_netmask).sin_port = 0;
1105 goto rarok;
1106
1107 case SIOCSIFNETMASK: /* Set the netmask for the interface */
1108 {
1109 unsigned long mask = (*(struct sockaddr_in *)
1110 &ifr.ifr_netmask).sin_addr.s_addr;
1111 ret = -EINVAL;
1112 /*
1113 * The mask we set must be legal.
1114 */
1115 if (bad_mask(mask,0))
1116 break;
1117 dev->pa_mask = mask;
1118 ret = 0;
1119 }
1120 break;
1121
1122 case SIOCGIFMETRIC: /* Get the metric on the interface (currently unused) */
1123
1124 ifr.ifr_metric = dev->metric;
1125 goto rarok;
1126
1127 case SIOCSIFMETRIC: /* Set the metric on the interface (currently unused) */
1128 dev->metric = ifr.ifr_metric;
1129 ret=0;
1130 break;
1131
1132 case SIOCGIFMTU: /* Get the MTU of a device */
1133 ifr.ifr_mtu = dev->mtu;
1134 goto rarok;
1135
1136 case SIOCSIFMTU: /* Set the MTU of a device */
1137
1138 /*
1139 * MTU must be positive.
1140 */
1141
1142 if(ifr.ifr_mtu<68)
1143 return -EINVAL;
1144
1145 if (dev->change_mtu)
1146 ret = (*dev->change_mtu)(dev, ifr.ifr_mtu);
1147 else
1148 {
1149 dev->mtu = ifr.ifr_mtu;
1150 ret = 0;
1151 }
1152 break;
1153
1154 case SIOCGIFMEM: /* Get the per device memory space. We can add this but currently
1155 do not support it */
1156 ret = -EINVAL;
1157 break;
1158
1159 case SIOCSIFMEM: /* Set the per device memory buffer space. Not applicable in our case */
1160 ret = -EINVAL;
1161 break;
1162
1163 case SIOCGIFHWADDR:
1164 memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1165 ifr.ifr_hwaddr.sa_family=dev->type;
1166 goto rarok;
1167
1168 case SIOCSIFHWADDR:
1169 if(dev->set_mac_address==NULL)
1170 return -EOPNOTSUPP;
1171 if(ifr.ifr_hwaddr.sa_family!=dev->type)
1172 return -EINVAL;
1173 ret=dev->set_mac_address(dev,&ifr.ifr_hwaddr);
1174 break;
1175
1176 case SIOCGIFMAP:
1177 ifr.ifr_map.mem_start=dev->mem_start;
1178 ifr.ifr_map.mem_end=dev->mem_end;
1179 ifr.ifr_map.base_addr=dev->base_addr;
1180 ifr.ifr_map.irq=dev->irq;
1181 ifr.ifr_map.dma=dev->dma;
1182 ifr.ifr_map.port=dev->if_port;
1183 goto rarok;
1184
1185 case SIOCSIFMAP:
1186 if(dev->set_config==NULL)
1187 return -EOPNOTSUPP;
1188 return dev->set_config(dev,&ifr.ifr_map);
1189
1190 case SIOCADDMULTI:
1191 if(dev->set_multicast_list==NULL)
1192 return -EINVAL;
1193 if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1194 return -EINVAL;
1195 dev_mc_add(dev,ifr.ifr_hwaddr.sa_data, dev->addr_len, 1);
1196 return 0;
1197
1198 case SIOCDELMULTI:
1199 if(dev->set_multicast_list==NULL)
1200 return -EINVAL;
1201 if(ifr.ifr_hwaddr.sa_family!=AF_UNSPEC)
1202 return -EINVAL;
1203 dev_mc_delete(dev,ifr.ifr_hwaddr.sa_data,dev->addr_len, 1);
1204 return 0;
1205 /*
1206 * Unknown or private ioctl
1207 */
1208
1209 default:
1210 if((getset >= SIOCDEVPRIVATE) &&
1211 (getset <= (SIOCDEVPRIVATE + 15))) {
1212 if(dev->do_ioctl==NULL)
1213 return -EOPNOTSUPP;
1214 ret=dev->do_ioctl(dev, &ifr, getset);
1215 memcpy_tofs(arg,&ifr,sizeof(struct ifreq));
1216 break;
1217 }
1218
1219 ret = -EINVAL;
1220 }
1221 return(ret);
1222 /*
1223 * The load of calls that return an ifreq and ok (saves memory).
1224 */
1225 rarok:
1226 memcpy_tofs(arg, &ifr, sizeof(struct ifreq));
1227 return 0;
1228 }
1229
1230
1231 /*
1232 * This function handles all "interface"-type I/O control requests. The actual
1233 * 'doing' part of this is dev_ifsioc above.
1234 */
1235
1236 int dev_ioctl(unsigned int cmd, void *arg)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1237 {
1238 switch(cmd)
1239 {
1240 case SIOCGIFCONF:
1241 (void) dev_ifconf((char *) arg);
1242 return 0;
1243
1244 /*
1245 * Ioctl calls that can be done by all.
1246 */
1247
1248 case SIOCGIFFLAGS:
1249 case SIOCGIFADDR:
1250 case SIOCGIFDSTADDR:
1251 case SIOCGIFBRDADDR:
1252 case SIOCGIFNETMASK:
1253 case SIOCGIFMETRIC:
1254 case SIOCGIFMTU:
1255 case SIOCGIFMEM:
1256 case SIOCGIFHWADDR:
1257 case SIOCSIFHWADDR:
1258 case SIOCGIFSLAVE:
1259 case SIOCGIFMAP:
1260 return dev_ifsioc(arg, cmd);
1261
1262 /*
1263 * Ioctl calls requiring the power of a superuser
1264 */
1265
1266 case SIOCSIFFLAGS:
1267 case SIOCSIFADDR:
1268 case SIOCSIFDSTADDR:
1269 case SIOCSIFBRDADDR:
1270 case SIOCSIFNETMASK:
1271 case SIOCSIFMETRIC:
1272 case SIOCSIFMTU:
1273 case SIOCSIFMEM:
1274 case SIOCSIFMAP:
1275 case SIOCSIFSLAVE:
1276 case SIOCADDMULTI:
1277 case SIOCDELMULTI:
1278 if (!suser())
1279 return -EPERM;
1280 return dev_ifsioc(arg, cmd);
1281
1282 case SIOCSIFLINK:
1283 return -EINVAL;
1284
1285 /*
1286 * Unknown or private ioctl.
1287 */
1288
1289 default:
1290 if((cmd >= SIOCDEVPRIVATE) &&
1291 (cmd <= (SIOCDEVPRIVATE + 15))) {
1292 return dev_ifsioc(arg, cmd);
1293 }
1294 return -EINVAL;
1295 }
1296 }
1297
1298
1299 /*
1300 * Initialize the DEV module. At boot time this walks the device list and
1301 * unhooks any devices that fail to initialise (normally hardware not
1302 * present) and leaves us with a valid list of present and active devices.
1303 *
1304 */
1305 extern int lance_init(void);
1306 extern int ni65_init(void);
1307 extern int pi_init(void);
1308 extern void sdla_setup(void);
1309 extern void dlci_setup(void);
1310
1311 int net_dev_init(void)
/* ![[previous]](../icons/left.png)
![[next]](../icons/n_right.png)
![[first]](../icons/first.png)
![[last]](../icons/n_last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
1312 {
1313 struct device *dev, **dp;
1314
1315 /*
1316 * Initialise the packet receive queue.
1317 */
1318
1319 skb_queue_head_init(&backlog);
1320
1321 /*
1322 * This is VeryUgly(tm).
1323 *
1324 * Some devices want to be initialized eary..
1325 */
1326 #if defined(CONFIG_LANCE)
1327 lance_init();
1328 #endif
1329 #if defined(CONFIG_NI65)
1330 ni65_init();
1331 #endif
1332 #if defined(CONFIG_PI)
1333 pi_init();
1334 #endif
1335 #if defined(CONFIG_PT)
1336 pt_init();
1337 #endif
1338 #if defined(CONFIG_DLCI)
1339 dlci_setup();
1340 #endif
1341 #if defined(CONFIG_SDLA)
1342 sdla_setup();
1343 #endif
1344 /*
1345 * SLHC if present needs attaching so other people see it
1346 * even if not opened.
1347 */
1348 #if (defined(CONFIG_SLIP) && defined(CONFIG_SLIP_COMPRESSED)) \
1349 || defined(CONFIG_PPP) \
1350 || (defined(CONFIG_ISDN) && defined(CONFIG_ISDN_PPP))
1351 slhc_install();
1352 #endif
1353
1354 /*
1355 * Add the devices.
1356 * If the call to dev->init fails, the dev is removed
1357 * from the chain disconnecting the device until the
1358 * next reboot.
1359 */
1360
1361 dp = &dev_base;
1362 while ((dev = *dp) != NULL)
1363 {
1364 int i;
1365 for (i = 0; i < DEV_NUMBUFFS; i++) {
1366 skb_queue_head_init(dev->buffs + i);
1367 }
1368
1369 if (dev->init && dev->init(dev))
1370 {
1371 /*
1372 * It failed to come up. Unhook it.
1373 */
1374 *dp = dev->next;
1375 }
1376 else
1377 {
1378 dp = &dev->next;
1379 }
1380 }
1381
1382 #ifdef CONFIG_PROC_FS
1383 proc_net_register(&(struct proc_dir_entry) {
1384 PROC_NET_DEV, 3, "dev",
1385 S_IFREG | S_IRUGO, 1, 0, 0,
1386 0, &proc_net_inode_operations,
1387 dev_get_info
1388 });
1389 #endif
1390
1391 /*
1392 * Initialise net_alias engine
1393 *
1394 * - register net_alias device notifier
1395 * - register proc entries: /proc/net/alias_types
1396 * /proc/net/aliases
1397 */
1398
1399 #ifdef CONFIG_NET_ALIAS
1400 net_alias_init();
1401 #endif
1402
1403 init_bh(NET_BH, net_bh);
1404 return 0;
1405 }