ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / net / arcnet / rfc1201.c
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 MODULE_LICENSE("GPL");
34 #define VERSION "arcnet: RFC1201 \"standard\" (`a') 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 static int continue_tx(struct net_device *dev, int bufnum);
45
46 struct ArcProto rfc1201_proto =
47 {
48         .suffix         = 'a',
49         .mtu            = 1500, /* could be more, but some receivers can't handle it... */
50         .rx             = rx,
51         .build_header   = build_header,
52         .prepare_tx     = prepare_tx,
53         .continue_tx    = continue_tx,
54 };
55
56
57 static int __init arcnet_rfc1201_init(void)
58 {
59         printk(VERSION);
60
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]
67             = &rfc1201_proto;
68
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;
72
73         return 0;
74 }
75
76 static void __exit arcnet_rfc1201_exit(void)
77 {
78         arcnet_unregister_proto(&rfc1201_proto);
79 }
80
81 module_init(arcnet_rfc1201_init);
82 module_exit(arcnet_rfc1201_exit);
83
84 /*
85  * Determine a packet's protocol ID.
86  * 
87  * With ARCnet we have to convert everything to Ethernet-style stuff.
88  */
89 static unsigned short type_trans(struct sk_buff *skb, struct net_device *dev)
90 {
91         struct archdr *pkt = (struct archdr *) skb->data;
92         struct arc_rfc1201 *soft = &pkt->soft.rfc1201;
93         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
94         int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE;
95
96         /* Pull off the arcnet header. */
97         skb->mac.raw = skb->data;
98         skb_pull(skb, hdr_size);
99
100         if (pkt->hard.dest == 0)
101                 skb->pkt_type = PACKET_BROADCAST;
102         else if (dev->flags & IFF_PROMISC) {
103                 /* if we're not sending to ourselves :) */
104                 if (pkt->hard.dest != dev->dev_addr[0])
105                         skb->pkt_type = PACKET_OTHERHOST;
106         }
107         /* now return the protocol number */
108         switch (soft->proto) {
109         case ARC_P_IP:
110                 return htons(ETH_P_IP);
111         case ARC_P_IPV6:
112                 return htons(ETH_P_IPV6);
113         case ARC_P_ARP:
114                 return htons(ETH_P_ARP);
115         case ARC_P_RARP:
116                 return htons(ETH_P_RARP);
117
118         case ARC_P_IPX:
119         case ARC_P_NOVELL_EC:
120                 return htons(ETH_P_802_3);
121         default:
122                 lp->stats.rx_errors++;
123                 lp->stats.rx_crc_errors++;
124                 return 0;
125         }
126
127         return htons(ETH_P_IP);
128 }
129
130
131 /* packet receiver */
132 static void rx(struct net_device *dev, int bufnum,
133                struct archdr *pkthdr, int length)
134 {
135         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
136         struct sk_buff *skb;
137         struct archdr *pkt = pkthdr;
138         struct arc_rfc1201 *soft = &pkthdr->soft.rfc1201;
139         int saddr = pkt->hard.source, ofs;
140         struct Incoming *in = &lp->rfc1201.incoming[saddr];
141
142         BUGMSG(D_DURING, "it's an RFC1201 packet (length=%d)\n", length);
143
144         if (length >= MinTU)
145                 ofs = 512 - length;
146         else
147                 ofs = 256 - length;
148
149         if (soft->split_flag == 0xFF) {         /* Exception Packet */
150                 if (length >= 4 + RFC1201_HDR_SIZE)
151                         BUGMSG(D_DURING, "compensating for exception packet\n");
152                 else {
153                         BUGMSG(D_EXTRA, "short RFC1201 exception packet from %02Xh",
154                                saddr);
155                         return;
156                 }
157
158                 /* skip over 4-byte junkola */
159                 length -= 4;
160                 ofs += 4;
161                 lp->hw.copy_from_card(dev, bufnum, 512 - length,
162                                       soft, sizeof(pkt->soft));
163         }
164         if (!soft->split_flag) {        /* not split */
165                 BUGMSG(D_RX, "incoming is not split (splitflag=%d)\n",
166                        soft->split_flag);
167
168                 if (in->skb) {  /* already assembling one! */
169                         BUGMSG(D_EXTRA, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n",
170                          in->sequence, soft->split_flag, soft->sequence);
171                         lp->rfc1201.aborted_seq = soft->sequence;
172                         dev_kfree_skb_irq(in->skb);
173                         lp->stats.rx_errors++;
174                         lp->stats.rx_missed_errors++;
175                         in->skb = NULL;
176                 }
177                 in->sequence = soft->sequence;
178
179                 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC);
180                 if (skb == NULL) {
181                         BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n");
182                         lp->stats.rx_dropped++;
183                         return;
184                 }
185                 skb_put(skb, length + ARC_HDR_SIZE);
186                 skb->dev = dev;
187
188                 pkt = (struct archdr *) skb->data;
189                 soft = &pkt->soft.rfc1201;
190
191                 /* up to sizeof(pkt->soft) has already been copied from the card */
192                 memcpy(pkt, pkthdr, sizeof(struct archdr));
193                 if (length > sizeof(pkt->soft))
194                         lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft),
195                                        pkt->soft.raw + sizeof(pkt->soft),
196                                               length - sizeof(pkt->soft));
197
198                 /*
199                  * ARP packets have problems when sent from some DOS systems: the
200                  * source address is always 0!  So we take the hardware source addr
201                  * (which is impossible to fumble) and insert it ourselves.
202                  */
203                 if (soft->proto == ARC_P_ARP) {
204                         struct arphdr *arp = (struct arphdr *) soft->payload;
205
206                         /* make sure addresses are the right length */
207                         if (arp->ar_hln == 1 && arp->ar_pln == 4) {
208                                 uint8_t *cptr = (uint8_t *) arp + sizeof(struct arphdr);
209
210                                 if (!*cptr) {   /* is saddr = 00? */
211                                         BUGMSG(D_EXTRA,
212                                                "ARP source address was 00h, set to %02Xh.\n",
213                                                saddr);
214                                         lp->stats.rx_crc_errors++;
215                                         *cptr = saddr;
216                                 } else {
217                                         BUGMSG(D_DURING, "ARP source address (%Xh) is fine.\n",
218                                                *cptr);
219                                 }
220                         } else {
221                                 BUGMSG(D_NORMAL, "funny-shaped ARP packet. (%Xh, %Xh)\n",
222                                        arp->ar_hln, arp->ar_pln);
223                                 lp->stats.rx_errors++;
224                                 lp->stats.rx_crc_errors++;
225                         }
226                 }
227                 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
228
229                 skb->protocol = type_trans(skb, dev);
230                 netif_rx(skb);
231                 dev->last_rx = jiffies;
232         } else {                /* split packet */
233                 /*
234                  * NOTE: MSDOS ARP packet correction should only need to apply to
235                  * unsplit packets, since ARP packets are so short.
236                  *
237                  * My interpretation of the RFC1201 document is that if a packet is
238                  * received out of order, the entire assembly process should be
239                  * aborted.
240                  *
241                  * The RFC also mentions "it is possible for successfully received
242                  * packets to be retransmitted." As of 0.40 all previously received
243                  * packets are allowed, not just the most recent one.
244                  *
245                  * We allow multiple assembly processes, one for each ARCnet card
246                  * possible on the network.  Seems rather like a waste of memory,
247                  * but there's no other way to be reliable.
248                  */
249
250                 BUGMSG(D_RX, "packet is split (splitflag=%d, seq=%d)\n",
251                        soft->split_flag, in->sequence);
252
253                 if (in->skb && in->sequence != soft->sequence) {
254                         BUGMSG(D_EXTRA, "wrong seq number (saddr=%d, expected=%d, seq=%d, splitflag=%d)\n",
255                                saddr, in->sequence, soft->sequence,
256                                soft->split_flag);
257                         dev_kfree_skb_irq(in->skb);
258                         in->skb = NULL;
259                         lp->stats.rx_errors++;
260                         lp->stats.rx_missed_errors++;
261                         in->lastpacket = in->numpackets = 0;
262                 }
263                 if (soft->split_flag & 1) {     /* first packet in split */
264                         BUGMSG(D_RX, "brand new splitpacket (splitflag=%d)\n",
265                                soft->split_flag);
266                         if (in->skb) {  /* already assembling one! */
267                                 BUGMSG(D_EXTRA, "aborting previous (seq=%d) assembly "
268                                        "(splitflag=%d, seq=%d)\n",
269                                        in->sequence, soft->split_flag,
270                                        soft->sequence);
271                                 lp->stats.rx_errors++;
272                                 lp->stats.rx_missed_errors++;
273                                 dev_kfree_skb_irq(in->skb);
274                         }
275                         in->sequence = soft->sequence;
276                         in->numpackets = ((unsigned) soft->split_flag >> 1) + 2;
277                         in->lastpacket = 1;
278
279                         if (in->numpackets > 16) {
280                                 BUGMSG(D_EXTRA, "incoming packet more than 16 segments; dropping. (splitflag=%d)\n",
281                                        soft->split_flag);
282                                 lp->rfc1201.aborted_seq = soft->sequence;
283                                 lp->stats.rx_errors++;
284                                 lp->stats.rx_length_errors++;
285                                 return;
286                         }
287                         in->skb = skb = alloc_skb(508 * in->numpackets + ARC_HDR_SIZE,
288                                                   GFP_ATOMIC);
289                         if (skb == NULL) {
290                                 BUGMSG(D_NORMAL, "(split) memory squeeze, dropping packet.\n");
291                                 lp->rfc1201.aborted_seq = soft->sequence;
292                                 lp->stats.rx_dropped++;
293                                 return;
294                         }
295                         skb->dev = dev;
296                         pkt = (struct archdr *) skb->data;
297                         soft = &pkt->soft.rfc1201;
298
299                         memcpy(pkt, pkthdr, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
300                         skb_put(skb, ARC_HDR_SIZE + RFC1201_HDR_SIZE);
301
302                         soft->split_flag = 0;   /* end result won't be split */
303                 } else {        /* not first packet */
304                         int packetnum = ((unsigned) soft->split_flag >> 1) + 1;
305
306                         /*
307                          * if we're not assembling, there's no point trying to
308                          * continue.
309                          */
310                         if (!in->skb) {
311                                 if (lp->rfc1201.aborted_seq != soft->sequence) {
312                                         BUGMSG(D_EXTRA, "can't continue split without starting "
313                                                "first! (splitflag=%d, seq=%d, aborted=%d)\n",
314                                         soft->split_flag, soft->sequence,
315                                                lp->rfc1201.aborted_seq);
316                                         lp->stats.rx_errors++;
317                                         lp->stats.rx_missed_errors++;
318                                 }
319                                 return;
320                         }
321                         in->lastpacket++;
322                         if (packetnum != in->lastpacket) {      /* not the right flag! */
323                                 /* harmless duplicate? ignore. */
324                                 if (packetnum <= in->lastpacket - 1) {
325                                         BUGMSG(D_EXTRA, "duplicate splitpacket ignored! (splitflag=%d)\n",
326                                                soft->split_flag);
327                                         lp->stats.rx_errors++;
328                                         lp->stats.rx_frame_errors++;
329                                         return;
330                                 }
331                                 /* "bad" duplicate, kill reassembly */
332                                 BUGMSG(D_EXTRA, "out-of-order splitpacket, reassembly "
333                                        "(seq=%d) aborted (splitflag=%d, seq=%d)\n",
334                                        in->sequence, soft->split_flag, soft->sequence);
335                                 lp->rfc1201.aborted_seq = soft->sequence;
336                                 dev_kfree_skb_irq(in->skb);
337                                 in->skb = NULL;
338                                 lp->stats.rx_errors++;
339                                 lp->stats.rx_missed_errors++;
340                                 in->lastpacket = in->numpackets = 0;
341                                 return;
342                         }
343                         pkt = (struct archdr *) in->skb->data;
344                         soft = &pkt->soft.rfc1201;
345                 }
346
347                 skb = in->skb;
348
349                 lp->hw.copy_from_card(dev, bufnum, ofs + RFC1201_HDR_SIZE,
350                                       skb->data + skb->len,
351                                       length - RFC1201_HDR_SIZE);
352                 skb_put(skb, length - RFC1201_HDR_SIZE);
353
354                 /* are we done? */
355                 if (in->lastpacket == in->numpackets) {
356                         in->skb = NULL;
357                         in->lastpacket = in->numpackets = 0;
358
359             BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (unsplit)\n",
360                 skb->len, pkt->hard.source);
361             BUGMSG(D_SKB_SIZE, "skb: received %d bytes from %02X (split)\n",
362                 skb->len, pkt->hard.source);
363                         BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx");
364
365                         skb->protocol = type_trans(skb, dev);
366                         netif_rx(skb);
367                         dev->last_rx = jiffies;
368                 }
369         }
370 }
371
372
373 /* Create the ARCnet hard/soft headers for RFC1201. */
374 static int build_header(struct sk_buff *skb, struct net_device *dev,
375                         unsigned short type, uint8_t daddr)
376 {
377         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
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                 lp->stats.tx_errors++;
408                 lp->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
441 static 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 = (struct arcnet_local *) dev->priv;
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 = 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
474 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length,
475                       int bufnum)
476 {
477         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
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
510 static int continue_tx(struct net_device *dev, int bufnum)
511 {
512         struct arcnet_local *lp = (struct arcnet_local *) dev->priv;
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 }