blob: bee34226abfa1e0aea58bac9e7f998c5634c45be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Linux ARCnet driver - RFC1201 (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 <linux/netdevice.h>
30#include <linux/skbuff.h>
31#include <linux/arcdevice.h>
32
33MODULE_LICENSE("GPL");
34#define VERSION "arcnet: RFC1201 \"standard\" (`a') encapsulation support loaded.\n"
35
36
37static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev);
38static void rx(struct net_device *dev, int bufnum,
39 struct archdr *pkthdr, int length);
40static int build_header(struct sk_buff *skb, struct net_device *dev,
41 unsigned short type, uint8_t daddr);
42static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
43 int bufnum);
44static int continue_tx(struct net_device *dev, int bufnum);
45
Adrian Bunkf03aa2d2006-01-14 03:10:22 +010046static struct ArcProto rfc1201_proto =
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 .suffix = 'a',
49 .mtu = 1500, /* could be more, but some receivers can't handle it... */
50 .is_ip = 1, /* This is for sending IP and ARP packages */
51 .rx = rx,
52 .build_header = build_header,
53 .prepare_tx = prepare_tx,
54 .continue_tx = continue_tx,
55 .ack_tx = NULL
56};
57
58
59static int __init arcnet_rfc1201_init(void)
60{
61 printk(VERSION);
62
63 arc_proto_map[ARC_P_IP]
64 = arc_proto_map[ARC_P_IPV6]
65 = arc_proto_map[ARC_P_ARP]
66 = arc_proto_map[ARC_P_RARP]
67 = arc_proto_map[ARC_P_IPX]
68 = arc_proto_map[ARC_P_NOVELL_EC]
69 = &rfc1201_proto;
70
71 /* if someone else already owns the broadcast, we won't take it */
72 if (arc_bcast_proto == arc_proto_default)
73 arc_bcast_proto = &rfc1201_proto;
74
75 return 0;
76}
77
78static void __exit arcnet_rfc1201_exit(void)
79{
80 arcnet_unregister_proto(&rfc1201_proto);
81}
82
83module_init(arcnet_rfc1201_init);
84module_exit(arcnet_rfc1201_exit);
85
86/*
87 * Determine a packet's protocol ID.
88 *
89 * With ARCnet we have to convert everything to Ethernet-style stuff.
90 */
91static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev)
92{
93 struct archdr *pkt = (struct archdr *) skb->data;
94 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
95 struct arcnet_local *lp = dev->priv;
96 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
97
98 /* Pull off the arcnet header. */
99 skb->mac.raw = skb->data;
100 skb_pull(skb, hdr_size);
101
102 if (pkt->hard.dest == 0)
103 skb->pkt_type = PACKET_BROADCAST;
104 else if (dev->flags & IFF_PROMISC) {
105 /* if we're not sending to ourselves :) */
106 if (pkt->hard.dest != dev->dev_addr[0])
107 skb->pkt_type = PACKET_OTHERHOST;
108 }
109 /* now return the protocol number */
110 switch (soft->proto) {
111 case ARC_P_IP:
112 return htons(ETH_P_IP);
113 case ARC_P_IPV6:
114 return htons(ETH_P_IPV6);
115 case ARC_P_ARP:
116 return htons(ETH_P_ARP);
117 case ARC_P_RARP:
118 return htons(ETH_P_RARP);
119
120 case ARC_P_IPX:
121 case ARC_P_NOVELL_EC:
122 return htons(ETH_P_802_3);
123 default:
124 lp->stats.rx_errors++;
125 lp->stats.rx_crc_errors++;
126 return 0;
127 }
128
129 return htons(ETH_P_IP);
130}
131
132
133/* packet receiver */
134static void rx(struct net_device *dev, int bufnum,
135 struct archdr *pkthdr, int length)
136{
137 struct arcnet_local *lp = dev->priv;
138 struct sk_buff *skb;
139 struct archdr *pkt = pkthdr;
140 struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
141 int saddr = pkt->hard.source, ofs;
142 struct Incoming *in = &lp->rfc1201.incoming[saddr];
143
144 BUGMSG(D_DURING, "it's an RFC1201 packet (length=%d)\n", length);
145
146 if (length >= MinTU)
147 ofs = 512 - length;
148 else
149 ofs = 256 - length;
150
151 if (soft->split_flag == 0xFF) { /* Exception Packet */
152 if (length >= 4 + RFC1201_HDR_SIZE)
153 BUGMSG(D_DURING, "compensating for exception packet\n");
154 else {
155 BUGMSG(D_EXTRA, "short RFC1201 exception packet from %02Xh",
156 saddr);
157 return;
158 }
159
160 /* skip over 4-byte junkola */
161 length -= 4;
162 ofs += 4;
163 lp->hw.copy_from_card(dev, bufnum, 512 - length,
164 soft, sizeof(pkt->soft));
165 }
166 if (!soft->split_flag) { /* not split */
167 BUGMSG(D_RX, "incoming is not split (splitflag=%d)\n",
168 soft->split_flag);
169
170 if (in->skb) { /* already assembling one! */
171 BUGMSG(D_EXTRA, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
172 in->sequence, soft->split_flag, soft->sequence);
173 lp->rfc1201.aborted_seq = soft->sequence;
174 dev_kfree_skb_irq(in->skb);
175 lp->stats.rx_errors++;
176 lp->stats.rx_missed_errors++;
177 in->skb = NULL;
178 }
179 in->sequence = soft->sequence;
180
181 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
182 if (skb == NULL) {
183 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
184 lp->stats.rx_dropped++;
185 return;
186 }
187 skb_put(skb, length + ARC_HDR_SIZE);
188 skb->dev = dev;
189
190 pkt = (struct archdr *) skb->data;
191 soft = &pkt->soft.rfc1201;
192
193 /* up to sizeof(pkt->soft) has already been copied from the card */
194 memcpy(pkt, pkthdr, sizeof(struct archdr));
195 if (length > sizeof(pkt->soft))
196 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
197 pkt->soft.raw + sizeof(pkt->soft),
198 length - sizeof(pkt->soft));
199
200 /*
201 * ARP packets have problems when sent from some DOS systems: the
202 * source address is always 0! So we take the hardware source addr
203 * (which is impossible to fumble) and insert it ourselves.
204 */
205 if (soft->proto == ARC_P_ARP) {
206 struct arphdr *arp = (struct arphdr *) soft->payload;
207
208 /* make sure addresses are the right length */
209 if (arp->ar_hln == 1 && arp->ar_pln == 4) {
210 uint8_t *cptr = (uint8_t *) arp + sizeof(struct arphdr);
211
212 if (!*cptr) { /* is saddr = 00? */
213 BUGMSG(D_EXTRA,
214 "ARP source address was 00h, set to %02Xh.\n",
215 saddr);
216 lp->stats.rx_crc_errors++;
217 *cptr = saddr;
218 } else {
219 BUGMSG(D_DURING, "ARP source address (%Xh) is fine.\n",
220 *cptr);
221 }
222 } else {
223 BUGMSG(D_NORMAL, "funny-shaped ARP packet. (%Xh, %Xh)\n",
224 arp->ar_hln, arp->ar_pln);
225 lp->stats.rx_errors++;
226 lp->stats.rx_crc_errors++;
227 }
228 }
229 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
230
231 skb->protocol = type_trans(skb, dev);
232 netif_rx(skb);
233 dev->last_rx = jiffies;
234 } else { /* split packet */
235 /*
236 * NOTE: MSDOS ARP packet correction should only need to apply to
237 * unsplit packets, since ARP packets are so short.
238 *
239 * My interpretation of the RFC1201 document is that if a packet is
240 * received out of order, the entire assembly process should be
241 * aborted.
242 *
243 * The RFC also mentions "it is possible for successfully received
244 * packets to be retransmitted." As of 0.40 all previously received
245 * packets are allowed, not just the most recent one.
246 *
247 * We allow multiple assembly processes, one for each ARCnet card
248 * possible on the network. Seems rather like a waste of memory,
249 * but there's no other way to be reliable.
250 */
251
252 BUGMSG(D_RX, "packet is split (splitflag=%d, seq=%d)\n",
253 soft->split_flag, in->sequence);
254
255 if (in->skb && in->sequence != soft->sequence) {
256 BUGMSG(D_EXTRA, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
257 saddr, in->sequence, soft->sequence,
258 soft->split_flag);
259 dev_kfree_skb_irq(in->skb);
260 in->skb = NULL;
261 lp->stats.rx_errors++;
262 lp->stats.rx_missed_errors++;
263 in->lastpacket = in->numpackets = 0;
264 }
265 if (soft->split_flag & 1) { /* first packet in split */
266 BUGMSG(D_RX, "brand new splitpacket (splitflag=%d)\n",
267 soft->split_flag);
268 if (in->skb) { /* already assembling one! */
269 BUGMSG(D_EXTRA, "aborting previous (seq=%d) assembly "
270 "(splitflag=%d, seq=%d)\n",
271 in->sequence, soft->split_flag,
272 soft->sequence);
273 lp->stats.rx_errors++;
274 lp->stats.rx_missed_errors++;
275 dev_kfree_skb_irq(in->skb);
276 }
277 in->sequence = soft->sequence;
278 in->numpackets = ((unsigned) soft->split_flag >> 1) + 2;
279 in->lastpacket = 1;
280
281 if (in->numpackets > 16) {
282 BUGMSG(D_EXTRA, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
283 soft->split_flag);
284 lp->rfc1201.aborted_seq = soft->sequence;
285 lp->stats.rx_errors++;
286 lp->stats.rx_length_errors++;
287 return;
288 }
289 in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
290 GFP_ATOMIC);
291 if (skb == NULL) {
292 BUGMSG(D_NORMAL, "(split) memory squeeze, dropping packet.\n");
293 lp->rfc1201.aborted_seq = soft->sequence;
294 lp->stats.rx_dropped++;
295 return;
296 }
297 skb->dev = dev;
298 pkt = (struct archdr *) skb->data;
299 soft = &pkt->soft.rfc1201;
300
301 memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
302 skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
303
304 soft->split_flag = 0; /* end result won't be split */
305 } else { /* not first packet */
306 int packetnum = ((unsigned) soft->split_flag >> 1) + 1;
307
308 /*
309 * if we're not assembling, there's no point trying to
310 * continue.
311 */
312 if (!in->skb) {
313 if (lp->rfc1201.aborted_seq != soft->sequence) {
314 BUGMSG(D_EXTRA, "can't continue split without starting "
315 "first! (splitflag=%d, seq=%d, aborted=%d)\n",
316 soft->split_flag, soft->sequence,
317 lp->rfc1201.aborted_seq);
318 lp->stats.rx_errors++;
319 lp->stats.rx_missed_errors++;
320 }
321 return;
322 }
323 in->lastpacket++;
324 if (packetnum != in->lastpacket) { /* not the right flag! */
325 /* harmless duplicate? ignore. */
326 if (packetnum <= in->lastpacket - 1) {
327 BUGMSG(D_EXTRA, "duplicate splitpacket ignored! (splitflag=%d)\n",
328 soft->split_flag);
329 lp->stats.rx_errors++;
330 lp->stats.rx_frame_errors++;
331 return;
332 }
333 /* "bad" duplicate, kill reassembly */
334 BUGMSG(D_EXTRA, "out-of-order splitpacket, reassembly "
335 "(seq=%d) aborted (splitflag=%d, seq=%d)\n",
336 in->sequence, soft->split_flag, soft->sequence);
337 lp->rfc1201.aborted_seq = soft->sequence;
338 dev_kfree_skb_irq(in->skb);
339 in->skb = NULL;
340 lp->stats.rx_errors++;
341 lp->stats.rx_missed_errors++;
342 in->lastpacket = in->numpackets = 0;
343 return;
344 }
345 pkt = (struct archdr *) in->skb->data;
346 soft = &pkt->soft.rfc1201;
347 }
348
349 skb = in->skb;
350
351 lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
352 skb->data + skb->len,
353 length - RFC1201_HDR_SIZE);
354 skb_put(skb, length - RFC1201_HDR_SIZE);
355
356 /* are we done? */
357 if (in->lastpacket == in->numpackets) {
358 in->skb = NULL;
359 in->lastpacket = in->numpackets = 0;
360
361 BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (unsplit)\n",
362 skb->len, pkt->hard.source);
363 BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (split)\n",
364 skb->len, pkt->hard.source);
365 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
366
367 skb->protocol = type_trans(skb, dev);
368 netif_rx(skb);
369 dev->last_rx = jiffies;
370 }
371 }
372}
373
374
375/* Create the ARCnet hard/soft headers for RFC1201. */
376static int build_header(struct sk_buff *skb, struct net_device *dev,
377 unsigned short type, uint8_t daddr)
378{
379 struct arcnet_local *lp = dev->priv;
380 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
381 struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size);
382 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
383
384 /* set the protocol ID according to RFC1201 */
385 switch (type) {
386 case ETH_P_IP:
387 soft->proto = ARC_P_IP;
388 break;
389 case ETH_P_IPV6:
390 soft->proto = ARC_P_IPV6;
391 break;
392 case ETH_P_ARP:
393 soft->proto = ARC_P_ARP;
394 break;
395 case ETH_P_RARP:
396 soft->proto = ARC_P_RARP;
397 break;
398 case ETH_P_IPX:
399 case ETH_P_802_3:
400 case ETH_P_802_2:
401 soft->proto = ARC_P_IPX;
402 break;
403 case ETH_P_ATALK:
404 soft->proto = ARC_P_ATALK;
405 break;
406 default:
407 BUGMSG(D_NORMAL, "RFC1201: I don't understand protocol %d (%Xh)\n",
408 type, type);
409 lp->stats.tx_errors++;
410 lp->stats.tx_aborted_errors++;
411 return 0;
412 }
413
414 /*
415 * Set the source hardware address.
416 *
417 * This is pretty pointless for most purposes, but it can help in
418 * debugging. ARCnet does not allow us to change the source address in
419 * the actual packet sent)
420 */
421 pkt->hard.source = *dev->dev_addr;
422
423 soft->sequence = htons(lp->rfc1201.sequence++);
424 soft->split_flag = 0; /* split packets are done elsewhere */
425
426 /* see linux/net/ethernet/eth.c to see where I got the following */
427
428 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
429 /*
430 * FIXME: fill in the last byte of the dest ipaddr here to better
431 * comply with RFC1051 in "noarp" mode. For now, always broadcasting
432 * will probably at least get packets sent out :)
433 */
434 pkt->hard.dest = 0;
435 return hdr_size;
436 }
437 /* otherwise, drop in the dest address */
438 pkt->hard.dest = daddr;
439 return hdr_size;
440}
441
442
443static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
444 struct arc_rfc1201 *soft, int softlen, int bufnum)
445{
446 struct arcnet_local *lp = dev->priv;
447 int ofs;
448
449 /* assume length <= XMTU: someone should have handled that by now. */
450
451 if (softlen > MinTU) {
452 hard->offset[0] = 0;
453 hard->offset[1] = ofs = 512 - softlen;
454 } else if (softlen > MTU) { /* exception packet - add an extra header */
455 struct arc_rfc1201 excsoft;
456
457 excsoft.proto = soft->proto;
458 excsoft.split_flag = 0xff;
459 excsoft.sequence = 0xffff;
460
461 hard->offset[0] = 0;
462 ofs = 512 - softlen;
463 hard->offset[1] = ofs - RFC1201_HDR_SIZE;
464 lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
465 &excsoft, RFC1201_HDR_SIZE);
466 } else
467 hard->offset[0] = ofs = 256 - softlen;
468
469 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
470 lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
471
472 lp->lastload_dest = hard->dest;
473}
474
475
476static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
477 int bufnum)
478{
479 struct arcnet_local *lp = dev->priv;
480 const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
481 struct Outgoing *out;
482
483
484 BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
485 lp->next_tx, lp->cur_tx, bufnum);
486
487 length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
488 pkt->soft.rfc1201.split_flag = 0;
489
490 /* need to do a split packet? */
491 if (length > XMTU) {
492 out = &lp->outgoing;
493
494 out->length = length - RFC1201_HDR_SIZE;
495 out->dataleft = lp->outgoing.length;
496 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
497 out->segnum = 0;
498
499 BUGMSG(D_DURING, "rfc1201 prep_tx: ready for %d-segment split "
500 "(%d bytes, seq=%d)\n", out->numsegs, out->length,
501 pkt->soft.rfc1201.sequence);
502
503 return 0; /* not done */
504 }
505 /* just load the packet into the buffers and send it off */
506 load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
507
508 return 1; /* done */
509}
510
511
512static int continue_tx(struct net_device *dev, int bufnum)
513{
514 struct arcnet_local *lp = dev->priv;
515 struct Outgoing *out = &lp->outgoing;
516 struct arc_hardware *hard = &out->pkt->hard;
517 struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
518 int maxsegsize = XMTU - RFC1201_HDR_SIZE;
519 int seglen;
520
521 BUGMSG(D_DURING,
522 "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
523 out->segnum, out->numsegs, soft->sequence);
524
525 /* the "new" soft header comes right before the data chunk */
526 newsoft = (struct arc_rfc1201 *)
527 (out->pkt->soft.raw + out->length - out->dataleft);
528
529 if (!out->segnum) /* first packet; newsoft == soft */
530 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
531 else {
532 newsoft->split_flag = out->segnum << 1;
533 newsoft->proto = soft->proto;
534 newsoft->sequence = soft->sequence;
535 }
536
537 seglen = maxsegsize;
538 if (seglen > out->dataleft)
539 seglen = out->dataleft;
540 out->dataleft -= seglen;
541
542 load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
543
544 out->segnum++;
545 if (out->segnum >= out->numsegs)
546 return 1;
547 else
548 return 0;
549}