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 * Ethernet-type device handling.
7 *
8 * Version: @(#)eth.c 1.0.7 05/25/93
9 *
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 * Florian La Roche, <rzsfl@rz.uni-sb.de>
14 * Alan Cox, <gw4pts@gw4pts.ampr.org>
15 *
16 * Fixes:
17 * Mr Linux : Arp problems
18 * Alan Cox : Generic queue tidyup (very tiny here)
19 * Alan Cox : eth_header ntohs should be htons
20 * Alan Cox : eth_rebuild_header missing an htons and
21 * minor other things.
22 * Tegge : Arp bug fixes.
23 * Florian : Removed many unnecessary functions, code cleanup
24 * and changes for new arp and skbuff.
25 * Alan Cox : Redid header building to reflect new format.
26 * Alan Cox : ARP only when compiled with CONFIG_INET
27 * Greg Page : 802.2 and SNAP stuff.
28 * Alan Cox : MAC layer pointers/new format.
29 * Paul Gortmaker : eth_copy_and_sum shouldn't csum padding.
30 *
31 * This program is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU General Public License
33 * as published by the Free Software Foundation; either version
34 * 2 of the License, or (at your option) any later version.
35 */
36 #include <asm/segment.h>
37 #include <asm/system.h>
38 #include <linux/types.h>
39 #include <linux/kernel.h>
40 #include <linux/sched.h>
41 #include <linux/string.h>
42 #include <linux/mm.h>
43 #include <linux/socket.h>
44 #include <linux/in.h>
45 #include <linux/inet.h>
46 #include <linux/netdevice.h>
47 #include <linux/etherdevice.h>
48 #include <linux/skbuff.h>
49 #include <linux/errno.h>
50 #include <linux/config.h>
51 #include <net/arp.h>
52 #include <net/sock.h>
53 #include <asm/checksum.h>
54
55 void eth_setup(char *str, int *ints)
/* ![[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)
*/
56 {
57 struct device *d = dev_base;
58
59 if (!str || !*str)
60 return;
61 while (d)
62 {
63 if (!strcmp(str,d->name))
64 {
65 if (ints[0] > 0)
66 d->irq=ints[1];
67 if (ints[0] > 1)
68 d->base_addr=ints[2];
69 if (ints[0] > 2)
70 d->mem_start=ints[3];
71 if (ints[0] > 3)
72 d->mem_end=ints[4];
73 break;
74 }
75 d=d->next;
76 }
77 }
78
79
80 /*
81 * Create the Ethernet MAC header for an arbitrary protocol layer
82 *
83 * saddr=NULL means use device source address
84 * daddr=NULL means leave destination address (eg unresolved arp)
85 */
86
87 int eth_header(struct sk_buff *skb, struct device *dev, unsigned short type,
/* ![[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)
*/
88 void *daddr, void *saddr, unsigned len)
89 {
90 struct ethhdr *eth = (struct ethhdr *)skb_push(skb,14);
91
92 /*
93 * Set the protocol type. For a packet of type ETH_P_802_3 we put the length
94 * in here instead. It is up to the 802.2 layer to carry protocol information.
95 */
96
97 if(type!=ETH_P_802_3)
98 eth->h_proto = htons(type);
99 else
100 eth->h_proto = htons(len);
101
102 /*
103 * Set the source hardware address.
104 */
105
106 if(saddr)
107 memcpy(eth->h_source,saddr,dev->addr_len);
108 else
109 memcpy(eth->h_source,dev->dev_addr,dev->addr_len);
110
111 /*
112 * Anyway, the loopback-device should never use this function...
113 */
114
115 if (dev->flags & IFF_LOOPBACK)
116 {
117 memset(eth->h_dest, 0, dev->addr_len);
118 return(dev->hard_header_len);
119 }
120
121 if(daddr)
122 {
123 memcpy(eth->h_dest,daddr,dev->addr_len);
124 return dev->hard_header_len;
125 }
126
127 return -dev->hard_header_len;
128 }
129
130
131 /*
132 * Rebuild the Ethernet MAC header. This is called after an ARP
133 * (or in future other address resolution) has completed on this
134 * sk_buff. We now let ARP fill in the other fields.
135 */
136
137 int eth_rebuild_header(void *buff, struct device *dev, unsigned long dst,
/* ![[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)
*/
138 struct sk_buff *skb)
139 {
140 struct ethhdr *eth = (struct ethhdr *)buff;
141
142 /*
143 * Only ARP/IP is currently supported
144 */
145
146 if(eth->h_proto != htons(ETH_P_IP))
147 {
148 printk("eth_rebuild_header: Don't know how to resolve type %d addresses?\n",(int)eth->h_proto);
149 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
150 return 0;
151 }
152
153 /*
154 * Try and get ARP to resolve the header.
155 */
156 #ifdef CONFIG_INET
157 return arp_find(eth->h_dest, dst, dev, dev->pa_addr, skb)? 1 : 0;
158 #else
159 return 0;
160 #endif
161 }
162
163
164 /*
165 * Determine the packet's protocol ID. The rule here is that we
166 * assume 802.3 if the type field is short enough to be a length.
167 * This is normal practice and works for any 'now in use' protocol.
168 */
169
170 unsigned short eth_type_trans(struct sk_buff *skb, 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)
*/
171 {
172 struct ethhdr *eth;
173 unsigned char *rawp;
174
175 skb->mac.raw=skb->data;
176 skb_pull(skb,14);
177 eth= skb->mac.ethernet;
178
179 if(*eth->h_dest&1)
180 {
181 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
182 skb->pkt_type=PACKET_BROADCAST;
183 else
184 skb->pkt_type=PACKET_MULTICAST;
185 }
186
187 else if(dev->flags&IFF_PROMISC)
188 {
189 if(memcmp(eth->h_dest,dev->dev_addr, ETH_ALEN))
190 skb->pkt_type=PACKET_OTHERHOST;
191 }
192
193 if (ntohs(eth->h_proto) >= 1536)
194 return eth->h_proto;
195
196 rawp = skb->data;
197
198 /*
199 * This is a magic hack to spot IPX packets. Older Novell breaks
200 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
201 * layer. We look for FFFF which isnt a used 802.2 SSAP/DSAP. This
202 * won't work for fault tolerant netware but does for the rest.
203 */
204 if (*(unsigned short *)rawp == 0xFFFF)
205 return htons(ETH_P_802_3);
206
207 /*
208 * Real 802.2 LLC
209 */
210 return htons(ETH_P_802_2);
211 }
212
213 /*
214 * Header caching for ethernet. Try to find and cache a header to avoid arp overhead.
215 */
216
217 void eth_header_cache(struct device *dev, struct sock *sk, unsigned long saddr, unsigned long daddr)
/* ![[previous]](../icons/left.png)
![[next]](../icons/right.png)
![[first]](../icons/first.png)
![[last]](../icons/last.png)
![[top]](../icons/top.png)
![[bottom]](../icons/bottom.png)
![[index]](../icons/index.png)
*/
218 {
219 int v=arp_find_cache(sk->ip_hcache_data, daddr, dev);
220 if(v!=1)
221 sk->ip_hcache_state=0; /* Try when arp resolves */
222 else
223 {
224 memcpy(sk->ip_hcache_data+6, dev->dev_addr, ETH_ALEN);
225 sk->ip_hcache_data[12]=ETH_P_IP>>8;
226 sk->ip_hcache_data[13]=ETH_P_IP&0xFF;
227 sk->ip_hcache_state=1;
228 sk->ip_hcache_stamp=arp_cache_stamp;
229 sk->ip_hcache_ver=&arp_cache_stamp;
230 }
231 }
232
233 /*
234 * Copy from an ethernet device memory space to an sk_buff while checksumming if IP
235 * The magic "34" is Rx_addr+Tx_addr+type_field+sizeof(struct iphdr) == 6+6+2+20.
236 */
237
238 void eth_copy_and_sum(struct sk_buff *dest, unsigned char *src, int length, int base)
/* ![[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)
*/
239 {
240 struct ethhdr *eth;
241 struct iphdr *iph;
242 int ip_length;
243
244 IS_SKB(dest);
245 eth=(struct ethhdr *)dest->data;
246 if(eth->h_proto!=htons(ETH_P_IP))
247 {
248 memcpy(dest->data,src,length);
249 return;
250 }
251 /*
252 * We have to watch for padded packets. The csum doesn't include the
253 * padding, and there is no point in copying the padding anyway.
254 * We have to use the smaller of length and ip_length because it
255 * can happen that ip_length > length.
256 */
257 memcpy(dest->data,src,34); /* ethernet is always >= 34 */
258 length -= 34;
259 iph=(struct iphdr*)(src+14); /* 14 = Rx_addr+Tx_addr+type_field */
260 ip_length = ntohs(iph->tot_len) - sizeof(struct iphdr);
261 if (ip_length <= length)
262 length=ip_length;
263
264 dest->csum=csum_partial_copy(src+34,dest->data+34,length,base);
265 dest->ip_summed=1;
266 }