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 * 29 * This program is free software; you can redistribute it and/or 30 * modify it under the terms of the GNU General Public License 31 * as published by the Free Software Foundation; either version 32 * 2 of the License, or (at your option) any later version. 33 */ 34 #include <asm/segment.h>
35 #include <asm/system.h>
36 #include <linux/types.h>
37 #include <linux/kernel.h>
38 #include <linux/sched.h>
39 #include <linux/string.h>
40 #include <linux/mm.h>
41 #include <linux/socket.h>
42 #include <linux/in.h>
43 #include <linux/inet.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/skbuff.h>
47 #include <linux/errno.h>
48 #include "arp.h"
49
50 voideth_setup(char *str, int *ints)
/* */ 51 { 52 structdevice *d = dev_base;
53
54 if (!str || !*str)
55 return;
56 while (d)
57 { 58 if (!strcmp(str,d->name))
59 { 60 if (ints[0] > 0)
61 d->irq=ints[1];
62 if (ints[0] > 1)
63 d->base_addr=ints[2];
64 if (ints[0] > 2)
65 d->mem_start=ints[3];
66 if (ints[0] > 3)
67 d->mem_end=ints[4];
68 break;
69 } 70 d=d->next;
71 } 72 } 73
74
75 /* 76 * Create the Ethernet MAC header for an arbitrary protocol layer 77 * 78 * saddr=NULL means use device source address 79 * daddr=NULL means leave destination address (eg unresolved arp) 80 */ 81
82 inteth_header(unsignedchar *buff, structdevice *dev, unsignedshorttype,
/* */ 83 void *daddr, void *saddr, unsignedlen,
84 structsk_buff *skb)
85 { 86 structethhdr *eth = (structethhdr *)buff;
87
88 /* 89 * Set the protocol type. For a packet of type ETH_P_802_3 we put the length 90 * in here instead. It is up to the 802.2 layer to carry protocol information. 91 */ 92
93 if(type!=ETH_P_802_3)
94 eth->h_proto = htons(type);
95 else 96 eth->h_proto = htons(len);
97
98 /* 99 * Set the source hardware address. 100 */ 101
102 if(saddr)
103 memcpy(eth->h_source,saddr,dev->addr_len);
104 else 105 memcpy(eth->h_source,dev->dev_addr,dev->addr_len);
106
107 /* 108 * Anyway, the loopback-device should never use this function... 109 */ 110
111 if (dev->flags & IFF_LOOPBACK)
112 { 113 memset(eth->h_dest, 0, dev->addr_len);
114 return(dev->hard_header_len);
115 } 116
117 if(daddr)
118 { 119 memcpy(eth->h_dest,daddr,dev->addr_len);
120 returndev->hard_header_len;
121 } 122
123 return -dev->hard_header_len;
124 } 125
126
127 /* 128 * Rebuild the Ethernet MAC header. This is called after an ARP 129 * (or in future other address resolution) has completed on this 130 * sk_buff. We now let ARP fill in the other fields. 131 */ 132
133 inteth_rebuild_header(void *buff, structdevice *dev, unsignedlongdst,
/* */ 134 structsk_buff *skb)
135 { 136 structethhdr *eth = (structethhdr *)buff;
137
138 /* 139 * Only ARP/IP is currently supported 140 */ 141
142 if(eth->h_proto != htons(ETH_P_IP))
143 { 144 printk("eth_rebuild_header: Don't know how to resolve type %d addresses?\n",(int)eth->h_proto);
145 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
146 return 0;
147 } 148
149 /* 150 * Try and get ARP to resolve the header. 151 */ 152 #ifdefCONFIG_INET 153 returnarp_find(eth->h_dest, dst, dev, dev->pa_addr, skb)? 1 : 0;
154 #else 155 return 0;
156 #endif 157 } 158
159
160 /* 161 * Determine the packet's protocol ID. The rule here is that we 162 * assume 802.3 if the type field is short enough to be a length. 163 * This is normal practice and works for any 'now in use' protocol. 164 */ 165
166 unsignedshorteth_type_trans(structsk_buff *skb, structdevice *dev)
/* */ 167 { 168 structethhdr *eth = (structethhdr *) skb->data;
169 unsignedchar *rawp;
170
171 if(*eth->h_dest&1)
172 { 173 if(memcmp(eth->h_dest,dev->broadcast, ETH_ALEN)==0)
174 skb->pkt_type=PACKET_BROADCAST;
175 else 176 skb->pkt_type=PACKET_MULTICAST;
177 } 178
179 if(dev->flags&IFF_PROMISC)
180 { 181 if(memcmp(eth->h_dest,dev->dev_addr, ETH_ALEN))
182 skb->pkt_type=PACKET_OTHERHOST;
183 } 184
185 if (ntohs(eth->h_proto) >= 1536)
186 returneth->h_proto;
187
188 rawp = (unsignedchar *)(eth + 1);
189
190 if (*(unsignedshort *)rawp == 0xFFFF)
191 returnhtons(ETH_P_802_3);
192 if (*(unsignedshort *)rawp == 0xAAAA)
193 returnhtons(ETH_P_SNAP);
194
195 returnhtons(ETH_P_802_2);
196 }