Linux Audio

Check our new training course

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