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