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