1 /*
2 * INET An implementation of the TCP/IP protocol suite for the LINUX
3 * operating system. INET is implemented using the BSD Socket
4 * interface as the means of communication with the user level.
5 *
6 * Pseudo-driver for the loopback interface.
7 *
8 * Version: @(#)loopback.c 1.0.4b 08/16/93
9 *
10 * Authors: Ross Biro, <bir7@leland.Stanford.Edu>
11 * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
12 * Donald Becker, <becker@cesdis.gsfc.nasa.gov>
13 *
14 * Alan Cox : Fixed oddments for NET3.014
15 * Alan Cox : Rejig for NET3.029 snap #3
16 * Alan Cox : Fixed NET3.029 bugs and sped up
17 * Larry McVoy : Tiny tweak to double performance
18 * Alan Cox : Backed out LMV's tweak - the linux mm cant
19 * take it...
20 *
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License
23 * as published by the Free Software Foundation; either version
24 * 2 of the License, or (at your option) any later version.
25 */
26 #include <linux/config.h>
27 #include <linux/kernel.h>
28 #include <linux/sched.h>
29 #include <linux/interrupt.h>
30 #include <linux/fs.h>
31 #include <linux/types.h>
32 #include <linux/string.h>
33 #include <linux/socket.h>
34 #include <linux/errno.h>
35 #include <linux/fcntl.h>
36 #include <linux/in.h>
37 #include <linux/if_ether.h> /* For the statistics structure. */
38 #include <linux/if_arp.h> /* For ARPHRD_ETHER */
39
40 #include <asm/system.h>
41 #include <asm/segment.h>
42 #include <asm/io.h>
43
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/skbuff.h>
48 #include <net/sock.h>
49
50
51 static int loopback_xmit(struct sk_buff *skb, struct device *dev)
/* ![[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)
*/
52 {
53 struct enet_statistics *stats = (struct enet_statistics *)dev->priv;
54 unsigned long flags;
55 int unlock=1;
56
57 if (skb == NULL || dev == NULL)
58 return(0);
59
60 save_flags(flags);
61 cli();
62 if (dev->tbusy != 0)
63 {
64 restore_flags(flags);
65 stats->tx_errors++;
66 return(1);
67 }
68 dev->tbusy = 1;
69 restore_flags(flags);
70
71 /*
72 * Optimise so buffers with skb->free=1 are not copied but
73 * instead are lobbed from tx queue to rx queue
74 */
75
76 if(skb->free==0)
77 {
78 struct sk_buff *skb2=skb;
79 skb=skb_clone(skb, GFP_ATOMIC); /* Clone the buffer */
80 if(skb==NULL)
81 return 1;
82 dev_kfree_skb(skb2, FREE_READ);
83 unlock=0;
84 }
85 else if(skb->sk)
86 {
87 /*
88 * Packet sent but looped back around. Cease to charge
89 * the socket for the frame.
90 */
91 save_flags(flags);
92 cli();
93 skb->sk->wmem_alloc-=skb->truesize;
94 skb->sk->write_space(skb->sk);
95 restore_flags(flags);
96 }
97
98 skb->protocol=eth_type_trans(skb,dev);
99 skb->dev=dev;
100 save_flags(flags);
101 cli();
102 netif_rx(skb);
103 if(unlock)
104 skb_device_unlock(skb);
105 restore_flags(flags);
106
107 stats->tx_packets++;
108 stats->rx_packets++;
109
110 dev->tbusy = 0;
111
112 return(0);
113 }
114
115 static struct enet_statistics *get_stats(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)
*/
116 {
117 return (struct enet_statistics *)dev->priv;
118 }
119
120 static int loopback_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)
*/
121 {
122 dev->flags|=IFF_LOOPBACK;
123 return 0;
124 }
125
126 /* Initialize the rest of the LOOPBACK device. */
127 int loopback_init(struct device *dev)
/* ![[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)
*/
128 {
129 int i;
130 #if LINUS_EVER_SOLVES_THE_LARGE_ATOMIC_BUFFER_ISSUE
131 dev->mtu = 7900; /* MTU */
132 #else
133 /*
134 * If Alpha uses 8K pages then I guess 7K would be good for it.
135 */
136 dev->mtu = 2000; /* Kept under 1 page */
137 #endif
138 dev->tbusy = 0;
139 dev->hard_start_xmit = loopback_xmit;
140 dev->open = NULL;
141 dev->hard_header = eth_header;
142 dev->hard_header_len = ETH_HLEN; /* 14 */
143 dev->addr_len = ETH_ALEN; /* 6 */
144 dev->type = ARPHRD_ETHER; /* 0x0001 */
145 dev->rebuild_header = eth_rebuild_header;
146 dev->open = loopback_open;
147 dev->flags = IFF_LOOPBACK|IFF_BROADCAST;
148 dev->family = AF_INET;
149 #ifdef CONFIG_INET
150 dev->pa_addr = in_aton("127.0.0.1");
151 dev->pa_brdaddr = in_aton("127.255.255.255");
152 dev->pa_mask = in_aton("255.0.0.0");
153 dev->pa_alen = 4;
154 #endif
155 dev->priv = kmalloc(sizeof(struct enet_statistics), GFP_KERNEL);
156 if (dev->priv == NULL)
157 return -ENOMEM;
158 memset(dev->priv, 0, sizeof(struct enet_statistics));
159 dev->get_stats = get_stats;
160
161 /*
162 * Fill in the generic fields of the device structure.
163 */
164
165 for (i = 0; i < DEV_NUMBUFFS; i++)
166 skb_queue_head_init(&dev->buffs[i]);
167
168 return(0);
169 };