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 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)
*/
51 {
52 struct device *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 int eth_header(unsigned char *buff, 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)
*/
83 void *daddr, void *saddr, unsigned len,
84 struct sk_buff *skb)
85 {
86 struct ethhdr *eth = (struct ethhdr *)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 return dev->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 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)
*/
134 struct sk_buff *skb)
135 {
136 struct ethhdr *eth = (struct ethhdr *)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 #ifdef CONFIG_INET
153 return arp_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 unsigned short eth_type_trans(struct sk_buff *skb, 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)
*/
167 {
168 struct ethhdr *eth = (struct ethhdr *) skb->data;
169 unsigned char *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 return eth->h_proto;
187
188 rawp = (unsigned char *)(eth + 1);
189
190 if (*(unsigned short *)rawp == 0xFFFF)
191 return htons(ETH_P_802_3);
192 if (*(unsigned short *)rawp == 0xAAAA)
193 return htons(ETH_P_SNAP);
194
195 return htons(ETH_P_802_2);
196 }