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