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