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