2 * Linux ARCnet driver - RFC1201 (standard) packet encapsulation
4 * Written 1994-1999 by Avery Pennarun.
5 * Derived from skeleton.c by Donald Becker.
7 * Special thanks to Contemporary Controls, Inc. (www.ccontrols.com)
8 * for sponsoring the further development of this driver.
10 * **********************
12 * The original copyright of skeleton.c was as follows:
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.
20 * **********************
22 * For more details, see drivers/net/arcnet.c
24 * **********************
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>
34 MODULE_LICENSE("GPL");
35 #define VERSION "arcnet: RFC1201 \"standard\" (`a') encapsulation support loaded.\n"
37 static __be16 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,
44 static int continue_tx(struct net_device *dev, int bufnum);
46 static struct ArcProto rfc1201_proto = {
48 .mtu = 1500, /* could be more, but some receivers can't handle it... */
49 .is_ip = 1, /* This is for sending IP and ARP packages */
51 .build_header = build_header,
52 .prepare_tx = prepare_tx,
53 .continue_tx = continue_tx,
57 static int __init arcnet_rfc1201_init(void)
61 arc_proto_map[ARC_P_IP]
62 = arc_proto_map[ARC_P_IPV6]
63 = arc_proto_map[ARC_P_ARP]
64 = arc_proto_map[ARC_P_RARP]
65 = arc_proto_map[ARC_P_IPX]
66 = arc_proto_map[ARC_P_NOVELL_EC]
69 /* if someone else already owns the broadcast, we won't take it */
70 if (arc_bcast_proto == arc_proto_default)
71 arc_bcast_proto = &rfc1201_proto;
76 static void __exit arcnet_rfc1201_exit(void)
78 arcnet_unregister_proto(&rfc1201_proto);
81 module_init(arcnet_rfc1201_init);
82 module_exit(arcnet_rfc1201_exit);
84 /* Determine a packet's protocol ID.
86 * With ARCnet we have to convert everything to Ethernet-style stuff.
88 static __be16 type_trans(struct sk_buff *skb, struct net_device *dev)
90 struct archdr *pkt = (struct archdr *)skb->data;
91 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
92 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
94 /* Pull off the arcnet header. */
95 skb_reset_mac_header(skb);
96 skb_pull(skb, hdr_size);
98 if (pkt->hard.dest == 0) {
99 skb->pkt_type = PACKET_BROADCAST;
100 } else if (dev->flags & IFF_PROMISC) {
101 /* if we're not sending to ourselves :) */
102 if (pkt->hard.dest != dev->dev_addr[0])
103 skb->pkt_type = PACKET_OTHERHOST;
105 /* now return the protocol number */
106 switch (soft->proto) {
108 return htons(ETH_P_IP);
110 return htons(ETH_P_IPV6);
112 return htons(ETH_P_ARP);
114 return htons(ETH_P_RARP);
117 case ARC_P_NOVELL_EC:
118 return htons(ETH_P_802_3);
120 dev->stats.rx_errors++;
121 dev->stats.rx_crc_errors++;
125 return htons(ETH_P_IP);
128 /* packet receiver */
129 static void rx(struct net_device *dev, int bufnum,
130 struct archdr *pkthdr, int length)
132 struct arcnet_local *lp = netdev_priv(dev);
134 struct archdr *pkt = pkthdr;
135 struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
136 int saddr = pkt->hard.source, ofs;
137 struct Incoming *in = &lp->rfc1201.incoming[saddr];
139 BUGMSG(D_DURING, "it's an RFC1201 packet (length=%d)\n", length);
146 if (soft->split_flag == 0xFF) { /* Exception Packet */
147 if (length >= 4 + RFC1201_HDR_SIZE) {
148 BUGMSG(D_DURING, "compensating for exception packet\n");
150 BUGMSG(D_EXTRA, "short RFC1201 exception packet from %02Xh",
155 /* skip over 4-byte junkola */
158 lp->hw.copy_from_card(dev, bufnum, 512 - length,
159 soft, sizeof(pkt->soft));
161 if (!soft->split_flag) { /* not split */
162 BUGMSG(D_RX, "incoming is not split (splitflag=%d)\n",
165 if (in->skb) { /* already assembling one! */
166 BUGMSG(D_EXTRA, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
167 in->sequence, soft->split_flag, soft->sequence);
168 lp->rfc1201.aborted_seq = soft->sequence;
169 dev_kfree_skb_irq(in->skb);
170 dev->stats.rx_errors++;
171 dev->stats.rx_missed_errors++;
174 in->sequence = soft->sequence;
176 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
178 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
179 dev->stats.rx_dropped++;
182 skb_put(skb, length + ARC_HDR_SIZE);
185 pkt = (struct archdr *)skb->data;
186 soft = &pkt->soft.rfc1201;
188 /* up to sizeof(pkt->soft) has already been copied from the card */
189 memcpy(pkt, pkthdr, sizeof(struct archdr));
190 if (length > sizeof(pkt->soft))
191 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
192 pkt->soft.raw + sizeof(pkt->soft),
193 length - sizeof(pkt->soft));
195 /* ARP packets have problems when sent from some DOS systems:
196 * the source address is always 0!
197 * So we take the hardware source addr (which is impossible
198 * to fumble) and insert it ourselves.
200 if (soft->proto == ARC_P_ARP) {
201 struct arphdr *arp = (struct arphdr *)soft->payload;
203 /* make sure addresses are the right length */
204 if (arp->ar_hln == 1 && arp->ar_pln == 4) {
205 uint8_t *cptr = (uint8_t *)arp + sizeof(struct arphdr);
207 if (!*cptr) { /* is saddr = 00? */
209 "ARP source address was 00h, set to %02Xh.\n",
211 dev->stats.rx_crc_errors++;
214 BUGMSG(D_DURING, "ARP source address (%Xh) is fine.\n",
218 BUGMSG(D_NORMAL, "funny-shaped ARP packet. (%Xh, %Xh)\n",
219 arp->ar_hln, arp->ar_pln);
220 dev->stats.rx_errors++;
221 dev->stats.rx_crc_errors++;
225 arcnet_dump_skb(dev, skb, "rx");
227 skb->protocol = type_trans(skb, dev);
229 } else { /* split packet */
230 /* NOTE: MSDOS ARP packet correction should only need to
231 * apply to unsplit packets, since ARP packets are so short.
233 * My interpretation of the RFC1201 document is that if a
234 * packet is received out of order, the entire assembly
235 * process should be aborted.
237 * The RFC also mentions "it is possible for successfully
238 * received packets to be retransmitted." As of 0.40 all
239 * previously received packets are allowed, not just the
242 * We allow multiple assembly processes, one for each
243 * ARCnet card possible on the network.
244 * Seems rather like a waste of memory, but there's no
245 * other way to be reliable.
248 BUGMSG(D_RX, "packet is split (splitflag=%d, seq=%d)\n",
249 soft->split_flag, in->sequence);
251 if (in->skb && in->sequence != soft->sequence) {
252 BUGMSG(D_EXTRA, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
253 saddr, in->sequence, soft->sequence,
255 dev_kfree_skb_irq(in->skb);
257 dev->stats.rx_errors++;
258 dev->stats.rx_missed_errors++;
259 in->lastpacket = in->numpackets = 0;
261 if (soft->split_flag & 1) { /* first packet in split */
262 BUGMSG(D_RX, "brand new splitpacket (splitflag=%d)\n",
264 if (in->skb) { /* already assembling one! */
265 BUGMSG(D_EXTRA, "aborting previous (seq=%d) assembly (splitflag=%d, seq=%d)\n",
266 in->sequence, soft->split_flag,
268 dev->stats.rx_errors++;
269 dev->stats.rx_missed_errors++;
270 dev_kfree_skb_irq(in->skb);
272 in->sequence = soft->sequence;
273 in->numpackets = ((unsigned)soft->split_flag >> 1) + 2;
276 if (in->numpackets > 16) {
277 BUGMSG(D_EXTRA, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
279 lp->rfc1201.aborted_seq = soft->sequence;
280 dev->stats.rx_errors++;
281 dev->stats.rx_length_errors++;
284 in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
287 BUGMSG(D_NORMAL, "(split) memory squeeze, dropping packet.\n");
288 lp->rfc1201.aborted_seq = soft->sequence;
289 dev->stats.rx_dropped++;
293 pkt = (struct archdr *)skb->data;
294 soft = &pkt->soft.rfc1201;
296 memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
297 skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
299 soft->split_flag = 0; /* end result won't be split */
300 } else { /* not first packet */
301 int packetnum = ((unsigned)soft->split_flag >> 1) + 1;
303 /* if we're not assembling, there's no point trying to
307 if (lp->rfc1201.aborted_seq != soft->sequence) {
308 BUGMSG(D_EXTRA, "can't continue split without starting first! (splitflag=%d, seq=%d, aborted=%d)\n",
309 soft->split_flag, soft->sequence,
310 lp->rfc1201.aborted_seq);
311 dev->stats.rx_errors++;
312 dev->stats.rx_missed_errors++;
317 if (packetnum != in->lastpacket) { /* not the right flag! */
318 /* harmless duplicate? ignore. */
319 if (packetnum <= in->lastpacket - 1) {
320 BUGMSG(D_EXTRA, "duplicate splitpacket ignored! (splitflag=%d)\n",
322 dev->stats.rx_errors++;
323 dev->stats.rx_frame_errors++;
326 /* "bad" duplicate, kill reassembly */
327 BUGMSG(D_EXTRA, "out-of-order splitpacket, reassembly (seq=%d) aborted (splitflag=%d, seq=%d)\n",
328 in->sequence, soft->split_flag, soft->sequence);
329 lp->rfc1201.aborted_seq = soft->sequence;
330 dev_kfree_skb_irq(in->skb);
332 dev->stats.rx_errors++;
333 dev->stats.rx_missed_errors++;
334 in->lastpacket = in->numpackets = 0;
337 pkt = (struct archdr *)in->skb->data;
338 soft = &pkt->soft.rfc1201;
343 lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
344 skb->data + skb->len,
345 length - RFC1201_HDR_SIZE);
346 skb_put(skb, length - RFC1201_HDR_SIZE);
349 if (in->lastpacket == in->numpackets) {
351 in->lastpacket = in->numpackets = 0;
353 BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (unsplit)\n",
354 skb->len, pkt->hard.source);
355 BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (split)\n",
356 skb->len, pkt->hard.source);
358 arcnet_dump_skb(dev, skb, "rx");
360 skb->protocol = type_trans(skb, dev);
366 /* Create the ARCnet hard/soft headers for RFC1201. */
367 static int build_header(struct sk_buff *skb, struct net_device *dev,
368 unsigned short type, uint8_t daddr)
370 struct arcnet_local *lp = netdev_priv(dev);
371 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
372 struct archdr *pkt = (struct archdr *)skb_push(skb, hdr_size);
373 struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
375 /* set the protocol ID according to RFC1201 */
378 soft->proto = ARC_P_IP;
381 soft->proto = ARC_P_IPV6;
384 soft->proto = ARC_P_ARP;
387 soft->proto = ARC_P_RARP;
392 soft->proto = ARC_P_IPX;
395 soft->proto = ARC_P_ATALK;
398 BUGMSG(D_NORMAL, "RFC1201: I don't understand protocol %d (%Xh)\n",
400 dev->stats.tx_errors++;
401 dev->stats.tx_aborted_errors++;
405 /* Set the source hardware address.
407 * This is pretty pointless for most purposes, but it can help in
408 * debugging. ARCnet does not allow us to change the source address
409 * in the actual packet sent.
411 pkt->hard.source = *dev->dev_addr;
413 soft->sequence = htons(lp->rfc1201.sequence++);
414 soft->split_flag = 0; /* split packets are done elsewhere */
416 /* see linux/net/ethernet/eth.c to see where I got the following */
418 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) {
419 /* FIXME: fill in the last byte of the dest ipaddr here
420 * to better comply with RFC1051 in "noarp" mode.
421 * For now, always broadcasting will probably at least get
422 * packets sent out :)
427 /* otherwise, drop in the dest address */
428 pkt->hard.dest = daddr;
432 static void load_pkt(struct net_device *dev, struct arc_hardware *hard,
433 struct arc_rfc1201 *soft, int softlen, int bufnum)
435 struct arcnet_local *lp = netdev_priv(dev);
438 /* assume length <= XMTU: someone should have handled that by now. */
440 if (softlen > MinTU) {
442 hard->offset[1] = ofs = 512 - softlen;
443 } else if (softlen > MTU) { /* exception packet - add an extra header */
444 struct arc_rfc1201 excsoft;
446 excsoft.proto = soft->proto;
447 excsoft.split_flag = 0xff;
448 excsoft.sequence = htons(0xffff);
452 hard->offset[1] = ofs - RFC1201_HDR_SIZE;
453 lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE,
454 &excsoft, RFC1201_HDR_SIZE);
456 hard->offset[0] = ofs = 256 - softlen;
459 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE);
460 lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen);
462 lp->lastload_dest = hard->dest;
465 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
468 struct arcnet_local *lp = netdev_priv(dev);
469 const int maxsegsize = XMTU - RFC1201_HDR_SIZE;
470 struct Outgoing *out;
472 BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n",
473 lp->next_tx, lp->cur_tx, bufnum);
475 length -= ARC_HDR_SIZE; /* hard header is not included in packet length */
476 pkt->soft.rfc1201.split_flag = 0;
478 /* need to do a split packet? */
482 out->length = length - RFC1201_HDR_SIZE;
483 out->dataleft = lp->outgoing.length;
484 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize;
487 BUGMSG(D_DURING, "rfc1201 prep_tx: ready for %d-segment split (%d bytes, seq=%d)\n",
488 out->numsegs, out->length,
489 pkt->soft.rfc1201.sequence);
491 return 0; /* not done */
493 /* just load the packet into the buffers and send it off */
494 load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum);
499 static int continue_tx(struct net_device *dev, int bufnum)
501 struct arcnet_local *lp = netdev_priv(dev);
502 struct Outgoing *out = &lp->outgoing;
503 struct arc_hardware *hard = &out->pkt->hard;
504 struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft;
505 int maxsegsize = XMTU - RFC1201_HDR_SIZE;
509 "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
510 out->segnum, out->numsegs, soft->sequence);
512 /* the "new" soft header comes right before the data chunk */
513 newsoft = (struct arc_rfc1201 *)
514 (out->pkt->soft.raw + out->length - out->dataleft);
516 if (!out->segnum) /* first packet; newsoft == soft */
517 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1;
519 newsoft->split_flag = out->segnum << 1;
520 newsoft->proto = soft->proto;
521 newsoft->sequence = soft->sequence;
525 if (seglen > out->dataleft)
526 seglen = out->dataleft;
527 out->dataleft -= seglen;
529 load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum);
532 if (out->segnum >= out->numsegs)