Import 1.3.85
[davej-history.git] / net / core / dev.c
blob05eeb884a89b376b4d0ab67641db17bae6daad3e
1 /*
2 * NET3 Protocol independent device support routines.
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.
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>
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>
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.
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
86 * The list of packet types we will receive (as opposed to discard)
87 * and the routines to invoke.
90 struct packet_type *ptype_base[16];
91 struct packet_type *ptype_all = NULL;/* Taps */
94 * Device list lock
97 int dev_lockct=0;
100 * Our notifier list
103 struct notifier_block *netdev_chain=NULL;
106 * Device drivers call our routines to queue packets here. We empty the
107 * queue in the bottom half handler.
110 static struct sk_buff_head backlog;
113 * We don't overdo the queue or we will thrash memory badly.
116 static int backlog_size =0;
119 * Return the lesser of the two values.
122 static __inline__ unsigned longmin(unsigned long a,unsigned long b)
124 return(a < b)? a : b;
128 /******************************************************************************************
130 Protocol management and registration routines
132 *******************************************************************************************/
135 * For efficiency
138 static int dev_nit=0;
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.
146 voiddev_add_pack(struct packet_type *pt)
148 int hash;
149 if(pt->type==htons(ETH_P_ALL))
151 dev_nit++;
152 pt->next=ptype_all;
153 ptype_all=pt;
155 else
157 hash=ntohs(pt->type)&15;
158 pt->next = ptype_base[hash];
159 ptype_base[hash] = pt;
165 * Remove a protocol ID from the list.
168 voiddev_remove_pack(struct packet_type *pt)
170 struct packet_type **pt1;
171 if(pt->type==htons(ETH_P_ALL))
173 dev_nit--;
174 pt1=&ptype_all;
176 else
177 pt1=&ptype_base[ntohs(pt->type)&15];
178 for(; (*pt1)!=NULL; pt1=&((*pt1)->next))
180 if(pt==(*pt1))
182 *pt1=pt->next;
183 return;
186 printk("dev_remove_pack: %p not found.\n", pt);
189 /*****************************************************************************************
191 Device Interface Subroutines
193 ******************************************************************************************/
196 * Find an interface by name.
199 struct device *dev_get(const char*name)
201 struct device *dev;
203 for(dev = dev_base; dev != NULL; dev = dev->next)
205 if(strcmp(dev->name, name) ==0)
206 return(dev);
208 return NULL;
212 * Find and possibly load an interface.
215 #ifdef CONFIG_KERNELD
217 extern __inline__ voiddev_load(const char*name)
219 if(!dev_get(name)) {
220 #ifdef CONFIG_NET_ALIAS
221 const char*sptr;
223 for(sptr=name ; *sptr ; sptr++)if(*sptr==':')break;
224 if(!(*sptr && *(sptr+1)))
225 #endif
226 request_module(name);
230 #endif
233 * Prepare an interface for use.
236 intdev_open(struct device *dev)
238 int ret =0;
241 * Call device private open method
243 if(dev->open)
244 ret = dev->open(dev);
247 * If it went open OK then set the flags
250 if(ret ==0)
252 dev->flags |= (IFF_UP | IFF_RUNNING);
254 * Initialise multicasting status
256 dev_mc_upload(dev);
257 notifier_call_chain(&netdev_chain, NETDEV_UP, dev);
259 return(ret);
264 * Completely shutdown an interface.
267 intdev_close(struct device *dev)
269 int ct=0;
272 * Call the device specific close. This cannot fail.
273 * Only if device is UP
276 if((dev->flags & IFF_UP) && dev->stop)
277 dev->stop(dev);
280 * Device is now down.
283 dev->flags&=~(IFF_UP|IFF_RUNNING);
286 * Tell people we are going down
288 notifier_call_chain(&netdev_chain, NETDEV_DOWN, dev);
290 * Flush the multicast chain
292 dev_mc_discard(dev);
294 * Blank the IP addresses
296 dev->pa_addr =0;
297 dev->pa_dstaddr =0;
298 dev->pa_brdaddr =0;
299 dev->pa_mask =0;
301 * Purge any queued packets when we down the link
303 while(ct<DEV_NUMBUFFS)
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++;
311 return(0);
316 * Device change register/unregister. These are not inline or static
317 * as we export them to the world.
320 intregister_netdevice_notifier(struct notifier_block *nb)
322 returnnotifier_chain_register(&netdev_chain, nb);
325 intunregister_netdevice_notifier(struct notifier_block *nb)
327 returnnotifier_chain_unregister(&netdev_chain,nb);
331 * Send (or queue for sending) a packet.
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.
338 voiddev_queue_xmit(struct sk_buff *skb,struct device *dev,int pri)
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 */
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;
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.
359 if(pri <0)
361 pri = -pri-1;
362 retransmission =1;
365 #ifdef CONFIG_NET_DEBUG
366 if(pri >= DEV_NUMBUFFS)
368 printk("bad priority in dev_queue_xmit.\n");
369 pri =1;
371 #endif
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.
378 if(!skb->arp && dev->rebuild_header(skb->data, dev, skb->raddr, skb)) {
379 return;
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.
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;
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;
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);
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)
419 /* Never send packets back to the socket
420 * they originated from - MvS (miquels@drinkel.ow.org)
422 if((ptype->dev == dev || !ptype->dev) &&
423 ((struct sock *)ptype->data != skb->sk))
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);
435 start_bh_atomic();
436 if(dev->hard_start_xmit(skb, dev) ==0) {
438 * Packet is now solely the responsibility of the driver
440 end_bh_atomic();
441 return;
443 end_bh_atomic();
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)
449 cli();
450 skb_device_unlock(skb);
451 __skb_queue_head(list,skb);
452 restore_flags(flags);
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.
461 voidnetif_rx(struct sk_buff *skb)
463 static int dropping =0;
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.
471 skb->sk = NULL;
472 skb->free =1;
473 if(skb->stamp.tv_sec==0)
474 skb->stamp = xtime;
477 * Check that we aren't overdoing things.
480 if(!backlog_size)
481 dropping =0;
482 else if(backlog_size >300)
483 dropping =1;
485 if(dropping)
487 kfree_skb(skb, FREE_READ);
488 return;
492 * Add it to the "backlog" queue.
494 #if CONFIG_SKB_CHECK
495 IS_SKB(skb);
496 #endif
497 skb_queue_tail(&backlog,skb);
498 backlog_size++;
501 * If any packet arrived, mark it for processing after the
502 * hardware interrupt returns.
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;
514 * This routine causes all interfaces to try to send some data.
517 voiddev_transmit(void)
519 struct device *dev;
521 for(dev = dev_base; dev != NULL; dev = dev->next)
523 if(dev->flags !=0&& !dev->tbusy) {
525 * Kick the device
527 dev_tint(dev);
533 /**********************************************************************************
535 Receive Queue Processor
537 ***********************************************************************************/
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);
547 voidnet_bh(void)
549 struct packet_type *ptype;
550 struct packet_type *pt_prev;
551 unsigned short type;
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.
560 dev_transmit();
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
569 * While the queue is not empty..
571 * Note that the queue never shrinks due to
572 * an interrupt, so we can do this test without
573 * disabling interrupts.
576 while(!skb_queue_empty(&backlog)) {
577 struct sk_buff * skb = backlog.next;
580 * We have a packet. Therefore the queue has shrunk
582 cli();
583 __skb_unlink(skb, &backlog);
584 backlog_size--;
585 sti();
588 * Bump the pointer to the next structure.
590 * On entry to the protocol layer. skb->data and
591 * skb->h.raw point to the MAC and encapsulated data
594 skb->h.raw = skb->data;
597 * Fetch the packet protocol ID.
600 type = skb->protocol;
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.
607 pt_prev = NULL;
608 for(ptype = ptype_all; ptype!=NULL; ptype=ptype->next)
610 if(pt_prev)
612 struct sk_buff *skb2=skb_clone(skb, GFP_ATOMIC);
613 if(skb2)
614 pt_prev->func(skb2,skb->dev, pt_prev);
616 pt_prev=ptype;
619 for(ptype = ptype_base[ntohs(type)&15]; ptype != NULL; ptype = ptype->next)
621 if(ptype->type == type && (!ptype->dev || ptype->dev==skb->dev))
624 * We already have a match queued. Deliver
625 * to it and then remember the new match
627 if(pt_prev)
629 struct sk_buff *skb2;
631 skb2=skb_clone(skb, GFP_ATOMIC);
634 * Kick the protocol handler. This should be fast
635 * and efficient code.
638 if(skb2)
639 pt_prev->func(skb2, skb->dev, pt_prev);
641 /* Remember the current last to do */
642 pt_prev=ptype;
644 }/* End of protocol list loop */
647 * Is there a last item to send to ?
650 if(pt_prev)
651 pt_prev->func(skb, skb->dev, pt_prev);
653 * Has an unknown packet has been received ?
656 else
657 kfree_skb(skb, FREE_WRITE);
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]
663 #ifdef XMIT_EVERY
664 dev_transmit();
665 #endif
666 }/* End of queue loop */
669 * We have emptied the queue
673 * One last output flush.
676 #ifdef XMIT_AFTER
677 dev_transmit();
678 #endif
683 * This routine is called when an device driver (i.e. an
684 * interface) is ready to transmit a packet.
687 voiddev_tint(struct device *dev)
689 int i;
690 unsigned long flags;
691 struct sk_buff_head * head;
694 * aliases do not transmit (for now :) )
697 #ifdef CONFIG_NET_ALIAS
698 if(net_alias_is(dev))return;
699 #endif
700 head = dev->buffs;
701 save_flags(flags);
702 cli();
705 * Work the queues in priority order
707 for(i =0;i < DEV_NUMBUFFS; i++,head++)
710 while(!skb_queue_empty(head)) {
711 struct sk_buff *skb;
713 skb = head->next;
714 __skb_unlink(skb, head);
716 * Stop anyone freeing the buffer while we retransmit it
718 skb_device_lock(skb);
719 restore_flags(flags);
721 * Feed them to the output stage and if it fails
722 * indicate they re-queue at the front.
724 dev_queue_xmit(skb,dev,-i -1);
726 * If we can take no more then stop here.
728 if(dev->tbusy)
729 return;
730 cli();
733 restore_flags(flags);
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'.
743 static intdev_ifconf(char*arg)
745 struct ifconf ifc;
746 struct ifreq ifr;
747 struct device *dev;
748 char*pos;
749 int len;
750 int err;
753 * Fetch the caller's info block.
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;
764 * We now walk the device list filling each active device
765 * into the array.
768 err=verify_area(VERIFY_WRITE,pos,len);
769 if(err)
770 return err;
773 * Loop over the interfaces, and write an info block for each.
776 for(dev = dev_base; dev != NULL; dev = dev->next)
778 if(!(dev->flags & IFF_UP))/* Downed devices don't count */
779 continue;
781 * Have we run out of space here ?
784 if(len <sizeof(struct ifreq))
785 break;
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;
794 * Write this block to the caller's space.
797 memcpy_tofs(pos, &ifr,sizeof(struct ifreq));
798 pos +=sizeof(struct ifreq);
799 len -=sizeof(struct ifreq);
803 * All done. Write the updated control block back to the caller.
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));
811 * Report how much was filled in
814 return(pos - arg);
819 * This is invoked by the /proc filesystem handler to display a device
820 * in detail.
823 #ifdef CONFIG_PROC_FS
824 static intsprintf_stats(char*buffer,struct device *dev)
826 struct enet_statistics *stats = (dev->get_stats ? dev->get_stats(dev): NULL);
827 int size;
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);
844 return size;
848 * Called from the PROCfs module. This now uses the new arbitrary sized /proc/net interface
849 * to create /proc/net/dev
852 intdev_get_info(char*buffer,char**start, off_t offset,int length,int dummy)
854 int len=0;
855 off_t begin=0;
856 off_t pos=0;
857 int size;
859 struct device *dev;
862 size =sprintf(buffer,"Inter-| Receive | Transmit\n"
863 " face |packets errs drop fifo frame|packets errs drop fifo colls carrier\n");
865 pos+=size;
866 len+=size;
869 for(dev = dev_base; dev != NULL; dev = dev->next)
871 size =sprintf_stats(buffer+len, dev);
872 len+=size;
873 pos=begin+len;
875 if(pos<offset)
877 len=0;
878 begin=pos;
880 if(pos>offset+length)
881 break;
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;
890 #endif/* CONFIG_PROC_FS */
894 * This checks bitmasks for the ioctl calls for devices.
897 staticinlineintbad_mask(unsigned long mask,unsigned long addr)
899 if(addr & (mask = ~mask))
900 return1;
901 mask =ntohl(mask);
902 if(mask & (mask+1))
903 return1;
904 return0;
908 * Perform the SIOCxIFxxx calls.
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
914 static intdev_ifsioc(void*arg,unsigned int getset)
916 struct ifreq ifr;
917 struct device *dev;
918 int ret;
921 * Fetch the caller's info block into kernel space
924 int err=verify_area(VERIFY_WRITE, arg,sizeof(struct ifreq));
925 if(err)
926 return err;
928 memcpy_fromfs(&ifr, arg,sizeof(struct ifreq));
931 * See which interface the caller is talking about.
936 * net_alias_dev_get(): dev_get() with added alias naming magic.
937 * only allow alias creation/deletion if (getset==SIOCSIFADDR)
941 #ifdef CONFIG_KERNELD
942 dev_load(ifr.ifr_name);
943 #endif
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)
954 case SIOCGIFFLAGS:/* Get interface flags */
955 ifr.ifr_flags = dev->flags;
956 goto rarok;
958 case SIOCSIFFLAGS:/* Set interface flags */
960 int old_flags = dev->flags;
963 * We are not allowed to potentially close/unload
964 * a device until we get this lock.
967 dev_lock_wait();
970 * Set the flags on our device.
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);
979 * Load in the correct multicast list now the flags have changed.
982 dev_mc_upload(dev);
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.
990 if((old_flags^ifr.ifr_flags)&IFF_UP)/* Bit is different ? */
992 if(old_flags&IFF_UP)/* Gone down */
993 ret=dev_close(dev);
994 else/* Come up */
996 ret=dev_open(dev);
997 if(ret<0)
998 dev->flags&=~IFF_UP;/* Open failed */
1001 else
1002 ret=0;
1004 * Load in the correct multicast list now the flags have changed.
1007 dev_mc_upload(dev);
1009 break;
1011 case SIOCGIFADDR:/* Get interface address (and family) */
1012 if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1014 memcpy(ifr.ifr_hwaddr.sa_data,dev->dev_addr, MAX_ADDR_LEN);
1015 ifr.ifr_hwaddr.sa_family=dev->type;
1016 goto rarok;
1018 else
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;
1027 goto rarok;
1029 case SIOCSIFADDR:/* Set interface address (and family) */
1032 * BSDism. SIOCSIFADDR family=AF_UNSPEC sets the
1033 * physical address. We can cope with this now.
1036 if(ifr.ifr_addr.sa_family==AF_UNSPEC)
1038 if(dev->set_mac_address==NULL)
1039 return-EOPNOTSUPP;
1040 ret=dev->set_mac_address(dev,&ifr.ifr_addr);
1042 else
1046 * if dev is an alias, must rehash to update
1047 * address change
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;
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;
1066 break;
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;
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;
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;
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;
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;
1107 case SIOCSIFNETMASK:/* Set the netmask for the interface */
1109 unsigned long mask = (*(struct sockaddr_in *)
1110 &ifr.ifr_netmask).sin_addr.s_addr;
1111 ret = -EINVAL;
1113 * The mask we set must be legal.
1115 if(bad_mask(mask,0))
1116 break;
1117 dev->pa_mask = mask;
1118 ret =0;
1120 break;
1122 case SIOCGIFMETRIC:/* Get the metric on the interface (currently unused) */
1124 ifr.ifr_metric = dev->metric;
1125 goto rarok;
1127 case SIOCSIFMETRIC:/* Set the metric on the interface (currently unused) */
1128 dev->metric = ifr.ifr_metric;
1129 ret=0;
1130 break;
1132 case SIOCGIFMTU:/* Get the MTU of a device */
1133 ifr.ifr_mtu = dev->mtu;
1134 goto rarok;
1136 case SIOCSIFMTU:/* Set the MTU of a device */
1139 * MTU must be positive.
1142 if(ifr.ifr_mtu<68)
1143 return-EINVAL;
1145 if(dev->change_mtu)
1146 ret = (*dev->change_mtu)(dev, ifr.ifr_mtu);
1147 else
1149 dev->mtu = ifr.ifr_mtu;
1150 ret =0;
1152 break;
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;
1159 case SIOCSIFMEM:/* Set the per device memory buffer space. Not applicable in our case */
1160 ret = -EINVAL;
1161 break;
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;
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;
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;
1185 case SIOCSIFMAP:
1186 if(dev->set_config==NULL)
1187 return-EOPNOTSUPP;
1188 return dev->set_config(dev,&ifr.ifr_map);
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 return0;
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 return0;
1206 * Unknown or private ioctl
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;
1219 ret = -EINVAL;
1221 return(ret);
1223 * The load of calls that return an ifreq and ok (saves memory).
1225 rarok:
1226 memcpy_tofs(arg, &ifr,sizeof(struct ifreq));
1227 return0;
1232 * This function handles all "interface"-type I/O control requests. The actual
1233 * 'doing' part of this is dev_ifsioc above.
1236 intdev_ioctl(unsigned int cmd,void*arg)
1238 switch(cmd)
1240 case SIOCGIFCONF:
1241 (void)dev_ifconf((char*) arg);
1242 return0;
1245 * Ioctl calls that can be done by all.
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 returndev_ifsioc(arg, cmd);
1263 * Ioctl calls requiring the power of a superuser
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 returndev_ifsioc(arg, cmd);
1282 case SIOCSIFLINK:
1283 return-EINVAL;
1286 * Unknown or private ioctl.
1289 default:
1290 if((cmd >= SIOCDEVPRIVATE) &&
1291 (cmd <= (SIOCDEVPRIVATE +15))) {
1292 returndev_ifsioc(arg, cmd);
1294 return-EINVAL;
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.
1305 externintlance_init(void);
1306 externintni65_init(void);
1307 externintpi_init(void);
1308 externvoidsdla_setup(void);
1309 externvoiddlci_setup(void);
1311 intnet_dev_init(void)
1313 struct device *dev, **dp;
1316 * Initialise the packet receive queue.
1319 skb_queue_head_init(&backlog);
1322 * This is VeryUgly(tm).
1324 * Some devices want to be initialized eary..
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
1345 * SLHC if present needs attaching so other people see it
1346 * even if not opened.
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
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.
1361 dp = &dev_base;
1362 while((dev = *dp) != NULL)
1364 int i;
1365 for(i =0; i < DEV_NUMBUFFS; i++) {
1366 skb_queue_head_init(dev->buffs + i);
1369 if(dev->init && dev->init(dev))
1372 * It failed to come up. Unhook it.
1374 *dp = dev->next;
1376 else
1378 dp = &dev->next;
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
1389 #endif
1392 * Initialise net_alias engine
1394 * - register net_alias device notifier
1395 * - register proc entries: /proc/net/alias_types
1396 * /proc/net/aliases
1399 #ifdef CONFIG_NET_ALIAS
1400 net_alias_init();
1401 #endif
1403 init_bh(NET_BH, net_bh);
1404 return0;
close