1 /*
2 * Linux ARCnet driver - RFC1051 ("simple" standard) packet encapsulation
3 *
4 * Written 1994-1999 by Avery Pennarun.
5 * Derived from skeleton.c by Donald Becker.
6 *
7 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
8 * for sponsoring the further development of this driver.
9 *
10 * **********************
11 *
12 * The original copyright of skeleton.c was as follows:
13 *
14 * skeleton.c Written 1993 by Donald Becker.
15 * Copyright 1993 United States Government as represented by the
16 * Director, National Security Agency. This software may only be used
17 * and distributed according to the terms of the GNU General Public License as
18 * modified by SRC, incorporated herein by reference.
19 *
20 * **********************
21 *
22 * For more details, see drivers/net/arcnet.c
23 *
24 * **********************
25 */
26 #include <linux/module.h>
27 #include <linux/init.h>
28 #include <linux/if_arp.h>
29 #include <net/arp.h>
30 #include <linux/netdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/arcdevice.h>
33
34 #define VERSION "arcnet: RFC1051 \"simple standard\" (`s') encapsulation support loaded.\n"
35
36
37 static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev);
38 static void rx(struct net_device *dev, int bufnum,
39 struct archdr *pkthdr, int length);
40 static int build_header(struct sk_buff *skb, struct net_device *dev,
41 unsigned short type, uint8_t daddr);
42 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
43 int bufnum);
44
45
46 struct ArcProto rfc1051_proto =
47 {
48 's',
49 XMTU - RFC1051_HDR_SIZE,
50 rx,
51 build_header,
52 prepare_tx
53 };
54
55
arcnet_rfc1051_init(void)56 void __init arcnet_rfc1051_init(void)
57 {
58 arc_proto_map[ARC_P_IP_RFC1051]
59 = arc_proto_map[ARC_P_ARP_RFC1051]
60 = &rfc1051_proto;
61
62 /* if someone else already owns the broadcast, we won't take it */
63 if (arc_bcast_proto == arc_proto_default)
64 arc_bcast_proto = &rfc1051_proto;
65
66 }
67
68
69 #ifdef MODULE
70
71 MODULE_LICENSE("GPL");
72
init_module(void)73 int __init init_module(void)
74 {
75 printk(VERSION);
76 arcnet_rfc1051_init();
77 return 0;
78 }
79
cleanup_module(void)80 void cleanup_module(void)
81 {
82 arcnet_unregister_proto(&rfc1051_proto);
83 }
84
85 #endif /* MODULE */
86
87
88 /*
89 * Determine a packet's protocol ID.
90 *
91 * With ARCnet we have to convert everything to Ethernet-style stuff.
92 */
type_trans(struct sk_buff * skb,struct net_device * dev)93 static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev)
94 {
95 struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
96 struct archdr *pkt = (struct archdr *) skb->data;
97 struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
98 int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
99
100 /* Pull off the arcnet header. */
101 skb->mac.raw = skb->data;
102 skb_pull(skb, hdr_size);
103
104 if (pkt->hard.dest == 0)
105 skb->pkt_type = PACKET_BROADCAST;
106 else if (dev->flags & IFF_PROMISC) {
107 /* if we're not sending to ourselves :) */
108 if (pkt->hard.dest != dev->dev_addr[0])
109 skb->pkt_type = PACKET_OTHERHOST;
110 }
111 /* now return the protocol number */
112 switch (soft->proto) {
113 case ARC_P_IP_RFC1051:
114 return htons(ETH_P_IP);
115 case ARC_P_ARP_RFC1051:
116 return htons(ETH_P_ARP);
117
118 default:
119 lp->stats.rx_errors++;
120 lp->stats.rx_crc_errors++;
121 return 0;
122 }
123
124 return htons(ETH_P_IP);
125 }
126
127
128 /* packet receiver */
rx(struct net_device * dev,int bufnum,struct archdr * pkthdr,int length)129 static void rx(struct net_device *dev, int bufnum,
130 struct archdr *pkthdr, int length)
131 {
132 struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
133 struct sk_buff *skb;
134 struct archdr *pkt = pkthdr;
135 int ofs;
136
137 BUGMSG(D_DURING, "it's a raw packet (length=%d)\n", length);
138
139 if (length >= MinTU)
140 ofs = 512 - length;
141 else
142 ofs = 256 - length;
143
144 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
145 if (skb == NULL) {
146 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
147 lp->stats.rx_dropped++;
148 return;
149 }
150 skb_put(skb, length + ARC_HDR_SIZE);
151 skb->dev = dev;
152
153 pkt = (struct archdr *) skb->data;
154
155 /* up to sizeof(pkt->soft) has already been copied from the card */
156 memcpy(pkt, pkthdr, sizeof(struct archdr));
157 if (length > sizeof(pkt->soft))
158 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
159 pkt->soft.raw + sizeof(pkt->soft),
160 length - sizeof(pkt->soft));
161
162 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
163
164 skb->protocol = type_trans(skb, dev);
165 netif_rx(skb);
166 dev->last_rx = jiffies;
167 }
168
169
170 /*
171 * Create the ARCnet hard/soft headers for RFC1051.
172 */
build_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,uint8_t daddr)173 static int build_header(struct sk_buff *skb, struct net_device *dev,
174 unsigned short type, uint8_t daddr)
175 {
176 struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
177 int hdr_size = ARC_HDR_SIZE + RFC1051_HDR_SIZE;
178 struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
179 struct arc_rfc1051 *soft = &pkt->soft.rfc1051;
180
181 /* set the protocol ID according to RFC1051 */
182 switch (type) {
183 case ETH_P_IP:
184 soft->proto = ARC_P_IP_RFC1051;
185 break;
186 case ETH_P_ARP:
187 soft->proto = ARC_P_ARP_RFC1051;
188 break;
189 default:
190 BUGMSG(D_NORMAL, "RFC1051: I don't understand protocol %d (%Xh)\n",
191 type, type);
192 lp->stats.tx_errors++;
193 lp->stats.tx_aborted_errors++;
194 return 0;
195 }
196
197
198 /*
199 * Set the source hardware address.
200 *
201 * This is pretty pointless for most purposes, but it can help in
202 * debugging. ARCnet does not allow us to change the source address in
203 * the actual packet sent)
204 */
205 pkt->hard.source = *dev->dev_addr;
206
207 /* see linux/net/ethernet/eth.c to see where I got the following */
208
209 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
210 /*
211 * FIXME: fill in the last byte of the dest ipaddr here to better
212 * comply with RFC1051 in "noarp" mode.
213 */
214 pkt->hard.dest = 0;
215 return hdr_size;
216 }
217 /* otherwise, just fill it in and go! */
218 pkt->hard.dest = daddr;
219
220 return hdr_size; /* success */
221 }
222
223
prepare_tx(struct net_device * dev,struct archdr * pkt,int length,int bufnum)224 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
225 int bufnum)
226 {
227 struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
228 struct arc_hardware *hard = &pkt->hard;
229 int ofs;
230
231 BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
232 lp->next_tx, lp->cur_tx, bufnum);
233
234 length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
235
236 if (length > XMTU) {
237 /* should never happen! other people already check for this. */
238 BUGMSG(D_NORMAL, "Bug! prepare_tx with size %d (> %d)\n",
239 length, XMTU);
240 length = XMTU;
241 }
242 if (length > MinTU) {
243 hard->offset[0] = 0;
244 hard->offset[1] = ofs = 512 - length;
245 } else if (length > MTU) {
246 hard->offset[0] = 0;
247 hard->offset[1] = ofs = 512 - length - 3;
248 } else
249 hard->offset[0] = ofs = 256 - length;
250
251 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
252 lp->hw.copy_to_card(dev, bufnum, ofs, &pkt->soft, length);
253
254 lp->lastload_dest = hard->dest;
255
256 return 1; /* done */
257 }
258