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