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 * 30 * This program is free software; you can redistribute it and/or 31 * modify it under the terms of the GNU General Public License 32 * as published by the Free Software Foundation; either version 33 * 2 of the License, or (at your option) any later version. 34 */ 35 #include <asm/segment.h>
36 #include <asm/system.h>
37 #include <linux/types.h>
38 #include <linux/kernel.h>
39 #include <linux/sched.h>
40 #include <linux/string.h>
41 #include <linux/mm.h>
42 #include <linux/socket.h>
43 #include <linux/in.h>
44 #include <linux/inet.h>
45 #include <linux/netdevice.h>
46 #include <linux/etherdevice.h>
47 #include <linux/skbuff.h>
48 #include <linux/errno.h>
49 #include <linux/config.h>
50 #include <net/arp.h>
51 #include <net/sock.h>
52 #include <asm/checksum.h>
53
54 voideth_setup(char *str, int *ints)
/* */ 55 { 56 structdevice *d = dev_base;
57
58 if (!str || !*str)
59 return;
60 while (d)
61 { 62 if (!strcmp(str,d->name))
63 { 64 if (ints[0] > 0)
65 d->irq=ints[1];
66 if (ints[0] > 1)
67 d->base_addr=ints[2];
68 if (ints[0] > 2)
69 d->mem_start=ints[3];
70 if (ints[0] > 3)
71 d->mem_end=ints[4];
72 break;
73 } 74 d=d->next;
75 } 76 } 77
78
79 /* 80 * Create the Ethernet MAC header for an arbitrary protocol layer 81 * 82 * saddr=NULL means use device source address 83 * daddr=NULL means leave destination address (eg unresolved arp) 84 */ 85
86 inteth_header(structsk_buff *skb, structdevice *dev, unsignedshorttype,
/* */ 87 void *daddr, void *saddr, unsignedlen)
88 { 89 structethhdr *eth = (structethhdr *)skb_push(skb,14);
90
91 /* 92 * Set the protocol type. For a packet of type ETH_P_802_3 we put the length 93 * in here instead. It is up to the 802.2 layer to carry protocol information. 94 */ 95
96 if(type!=ETH_P_802_3)
97 eth->h_proto = htons(type);
98 else 99 eth->h_proto = htons(len);
100
101 /* 102 * Set the source hardware address. 103 */ 104
105 if(saddr)
106 memcpy(eth->h_source,saddr,dev->addr_len);
107 else 108 memcpy(eth->h_source,dev->dev_addr,dev->addr_len);
109
110 /* 111 * Anyway, the loopback-device should never use this function... 112 */ 113
114 if (dev->flags & IFF_LOOPBACK)
115 { 116 memset(eth->h_dest, 0, dev->addr_len);
117 return(dev->hard_header_len);
118 } 119
120 if(daddr)
121 { 122 memcpy(eth->h_dest,daddr,dev->addr_len);
123 returndev->hard_header_len;
124 } 125
126 return -dev->hard_header_len;
127 } 128
129
130 /* 131 * Rebuild the Ethernet MAC header. This is called after an ARP 132 * (or in future other address resolution) has completed on this 133 * sk_buff. We now let ARP fill in the other fields. 134 */ 135
136 inteth_rebuild_header(void *buff, structdevice *dev, unsignedlongdst,
/* */ 137 structsk_buff *skb)
138 { 139 structethhdr *eth = (structethhdr *)buff;
140
141 /* 142 * Only ARP/IP is currently supported 143 */ 144
145 if(eth->h_proto != htons(ETH_P_IP))
146 { 147 printk("eth_rebuild_header: Don't know how to resolve type %d addresses?\n",(int)eth->h_proto);
148 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
149 return 0;
150 } 151
152 /* 153 * Try and get ARP to resolve the header. 154 */ 155 #ifdefCONFIG_INET 156 returnarp_find(eth->h_dest, dst, dev, dev->pa_addr, skb)? 1 : 0;
157 #else 158 return 0;
159 #endif 160 } 161
162
163 /* 164 * Determine the packet's protocol ID. The rule here is that we 165 * assume 802.3 if the type field is short enough to be a length. 166 * This is normal practice and works for any 'now in use' protocol. 167 */ 168
169 unsignedshorteth_type_trans(structsk_buff *skb, structdevice *dev)
/* */ 170 { 171 structethhdr *eth;
172 unsignedchar *rawp;
173
174 skb->mac.raw=skb->data;
175 skb_pull(skb,14);
176 eth= skb->mac.ethernet;
177
178 if(*eth->h_dest&1)
179 { 180 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
181 skb->pkt_type=PACKET_BROADCAST;
182 else 183 skb->pkt_type=PACKET_MULTICAST;
184 } 185
186 elseif(dev->flags&IFF_PROMISC)
187 { 188 if(memcmp(eth->h_dest,dev->dev_addr, ETH_ALEN))
189 skb->pkt_type=PACKET_OTHERHOST;
190 } 191
192 if (ntohs(eth->h_proto) >= 1536)
193 returneth->h_proto;
194
195 rawp = skb->data;
196
197 /* 198 * This is a magic hack to spot IPX packets. Older Novell breaks 199 * the protocol design and runs IPX over 802.3 without an 802.2 LLC 200 * layer. We look for FFFF which isnt a used 802.2 SSAP/DSAP. This 201 * won't work for fault tolerant netware but does for the rest. 202 */ 203 if (*(unsignedshort *)rawp == 0xFFFF)
204 returnhtons(ETH_P_802_3);
205
206 /* 207 * Real 802.2 LLC 208 */ 209 returnhtons(ETH_P_802_2);
210 } 211
212 /* 213 * Header caching for ethernet. Try to find and cache a header to avoid arp overhead. 214 */ 215
216 voideth_header_cache(structdevice *dev, structsock *sk, unsignedlongsaddr, unsignedlongdaddr)
/* */ 217 { 218 intv=arp_find_cache(sk->ip_hcache_data, daddr, dev);
219 if(v!=1)
220 sk->ip_hcache_state=0; /* Try when arp resolves */ 221 else 222 { 223 memcpy(sk->ip_hcache_data+6, dev->dev_addr, ETH_ALEN);
224 sk->ip_hcache_data[12]=ETH_P_IP>>8;
225 sk->ip_hcache_data[13]=ETH_P_IP&0xFF;
226 sk->ip_hcache_state=1;
227 sk->ip_hcache_stamp=arp_cache_stamp;
228 sk->ip_hcache_ver=&arp_cache_stamp;
229 } 230 } 231
232 /* 233 * Copy from an ethernet device memory space to an sk_buff while checksumming if IP 234 */ 235
236 voideth_copy_and_sum(structsk_buff *dest,unsignedchar *src, intlength, intbase)
/* */ 237 { 238 structethhdr *eth=(structethhdr *)dest->data;
239 memcpy(dest->data,src,34); /* ethernet is always >= 60 */ 240 length-=34;
241 if(eth->h_proto!=htons(ETH_P_IP))
242 { 243 memcpy(dest->data+34,src+34,length);
244 return;
245 } 246 dest->csum=csum_partial_copy(src+34,dest->data+34,length,base);
247 dest->ip_summed=1;
248 }