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 __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 { 48 .suffix = 'a', 49 .mtu = 1500, /* could be more, but some receivers can't handle it... */ 50 .is_ip = 1, /* This is for sending IP and ARP packages */ 51 .rx = rx, 52 .build_header = build_header, 53 .prepare_tx = prepare_tx, 54 .continue_tx = continue_tx, 55 .ack_tx = NULL 56 }; 57 58 59 static int __init arcnet_rfc1201_init(void) 60 { 61 printk(VERSION); 62 63 arc_proto_map[ARC_P_IP] 64 = arc_proto_map[ARC_P_IPV6] 65 = arc_proto_map[ARC_P_ARP] 66 = arc_proto_map[ARC_P_RARP] 67 = arc_proto_map[ARC_P_IPX] 68 = arc_proto_map[ARC_P_NOVELL_EC] 69 = &rfc1201_proto; 70 71 /* if someone else already owns the broadcast, we won't take it */ 72 if (arc_bcast_proto == arc_proto_default) 73 arc_bcast_proto = &rfc1201_proto; 74 75 return 0; 76 } 77 78 static void __exit arcnet_rfc1201_exit(void) 79 { 80 arcnet_unregister_proto(&rfc1201_proto); 81 } 82 83 module_init(arcnet_rfc1201_init); 84 module_exit(arcnet_rfc1201_exit); 85 86 /* 87 * Determine a packet's protocol ID. 88 * 89 * With ARCnet we have to convert everything to Ethernet-style stuff. 90 */ 91 static __be16 type_trans(struct sk_buff *skb, struct net_device *dev) 92 { 93 struct archdr *pkt = (struct archdr *) skb->data; 94 struct arc_rfc1201 *soft = &pkt->soft.rfc1201; 95 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE; 96 97 /* Pull off the arcnet header. */ 98 skb_reset_mac_header(skb); 99 skb_pull(skb, hdr_size); 100 101 if (pkt->hard.dest == 0) 102 skb->pkt_type = PACKET_BROADCAST; 103 else if (dev->flags & IFF_PROMISC) { 104 /* if we're not sending to ourselves :) */ 105 if (pkt->hard.dest != dev->dev_addr[0]) 106 skb->pkt_type = PACKET_OTHERHOST; 107 } 108 /* now return the protocol number */ 109 switch (soft->proto) { 110 case ARC_P_IP: 111 return htons(ETH_P_IP); 112 case ARC_P_IPV6: 113 return htons(ETH_P_IPV6); 114 case ARC_P_ARP: 115 return htons(ETH_P_ARP); 116 case ARC_P_RARP: 117 return htons(ETH_P_RARP); 118 119 case ARC_P_IPX: 120 case ARC_P_NOVELL_EC: 121 return htons(ETH_P_802_3); 122 default: 123 dev->stats.rx_errors++; 124 dev->stats.rx_crc_errors++; 125 return 0; 126 } 127 128 return htons(ETH_P_IP); 129 } 130 131 132 /* packet receiver */ 133 static 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 BUGMSG(D_DURING, "it's an RFC1201 packet (length=%d)\n", length); 144 145 if (length >= MinTU) 146 ofs = 512 - length; 147 else 148 ofs = 256 - length; 149 150 if (soft->split_flag == 0xFF) { /* Exception Packet */ 151 if (length >= 4 + RFC1201_HDR_SIZE) 152 BUGMSG(D_DURING, "compensating for exception packet\n"); 153 else { 154 BUGMSG(D_EXTRA, "short RFC1201 exception packet from %02Xh", 155 saddr); 156 return; 157 } 158 159 /* skip over 4-byte junkola */ 160 length -= 4; 161 ofs += 4; 162 lp->hw.copy_from_card(dev, bufnum, 512 - length, 163 soft, sizeof(pkt->soft)); 164 } 165 if (!soft->split_flag) { /* not split */ 166 BUGMSG(D_RX, "incoming is not split (splitflag=%d)\n", 167 soft->split_flag); 168 169 if (in->skb) { /* already assembling one! */ 170 BUGMSG(D_EXTRA, "aborting assembly (seq=%d) for unsplit packet (splitflag=%d, seq=%d)\n", 171 in->sequence, soft->split_flag, soft->sequence); 172 lp->rfc1201.aborted_seq = soft->sequence; 173 dev_kfree_skb_irq(in->skb); 174 dev->stats.rx_errors++; 175 dev->stats.rx_missed_errors++; 176 in->skb = NULL; 177 } 178 in->sequence = soft->sequence; 179 180 skb = alloc_skb(length + ARC_HDR_SIZE, GFP_ATOMIC); 181 if (skb == NULL) { 182 BUGMSG(D_NORMAL, "Memory squeeze, dropping packet.\n"); 183 dev->stats.rx_dropped++; 184 return; 185 } 186 skb_put(skb, length + ARC_HDR_SIZE); 187 skb->dev = dev; 188 189 pkt = (struct archdr *) skb->data; 190 soft = &pkt->soft.rfc1201; 191 192 /* up to sizeof(pkt->soft) has already been copied from the card */ 193 memcpy(pkt, pkthdr, sizeof(struct archdr)); 194 if (length > sizeof(pkt->soft)) 195 lp->hw.copy_from_card(dev, bufnum, ofs + sizeof(pkt->soft), 196 pkt->soft.raw + sizeof(pkt->soft), 197 length - sizeof(pkt->soft)); 198 199 /* 200 * ARP packets have problems when sent from some DOS systems: the 201 * source address is always 0! So we take the hardware source addr 202 * (which is impossible to fumble) and insert it ourselves. 203 */ 204 if (soft->proto == ARC_P_ARP) { 205 struct arphdr *arp = (struct arphdr *) soft->payload; 206 207 /* make sure addresses are the right length */ 208 if (arp->ar_hln == 1 && arp->ar_pln == 4) { 209 uint8_t *cptr = (uint8_t *) arp + sizeof(struct arphdr); 210 211 if (!*cptr) { /* is saddr = 00? */ 212 BUGMSG(D_EXTRA, 213 "ARP source address was 00h, set to %02Xh.\n", 214 saddr); 215 dev->stats.rx_crc_errors++; 216 *cptr = saddr; 217 } else { 218 BUGMSG(D_DURING, "ARP source address (%Xh) is fine.\n", 219 *cptr); 220 } 221 } else { 222 BUGMSG(D_NORMAL, "funny-shaped ARP packet. (%Xh, %Xh)\n", 223 arp->ar_hln, arp->ar_pln); 224 dev->stats.rx_errors++; 225 dev->stats.rx_crc_errors++; 226 } 227 } 228 BUGLVL(D_SKB) arcnet_dump_skb(dev, skb, "rx"); 229 230 skb->protocol = type_trans(skb, dev); 231 netif_rx(skb); 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 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 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 dev->stats.rx_errors++; 272 dev->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 dev->stats.rx_errors++; 284 dev->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 dev->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 dev->stats.rx_errors++; 317 dev->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 dev->stats.rx_errors++; 328 dev->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 dev->stats.rx_errors++; 339 dev->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 } 368 } 369 } 370 371 372 /* Create the ARCnet hard/soft headers for RFC1201. */ 373 static int build_header(struct sk_buff *skb, struct net_device *dev, 374 unsigned short type, uint8_t daddr) 375 { 376 struct arcnet_local *lp = netdev_priv(dev); 377 int hdr_size = ARC_HDR_SIZE + RFC1201_HDR_SIZE; 378 struct archdr *pkt = (struct archdr *) skb_push(skb, hdr_size); 379 struct arc_rfc1201 *soft = &pkt->soft.rfc1201; 380 381 /* set the protocol ID according to RFC1201 */ 382 switch (type) { 383 case ETH_P_IP: 384 soft->proto = ARC_P_IP; 385 break; 386 case ETH_P_IPV6: 387 soft->proto = ARC_P_IPV6; 388 break; 389 case ETH_P_ARP: 390 soft->proto = ARC_P_ARP; 391 break; 392 case ETH_P_RARP: 393 soft->proto = ARC_P_RARP; 394 break; 395 case ETH_P_IPX: 396 case ETH_P_802_3: 397 case ETH_P_802_2: 398 soft->proto = ARC_P_IPX; 399 break; 400 case ETH_P_ATALK: 401 soft->proto = ARC_P_ATALK; 402 break; 403 default: 404 BUGMSG(D_NORMAL, "RFC1201: I don't understand protocol %d (%Xh)\n", 405 type, type); 406 dev->stats.tx_errors++; 407 dev->stats.tx_aborted_errors++; 408 return 0; 409 } 410 411 /* 412 * Set the source hardware address. 413 * 414 * This is pretty pointless for most purposes, but it can help in 415 * debugging. ARCnet does not allow us to change the source address in 416 * the actual packet sent) 417 */ 418 pkt->hard.source = *dev->dev_addr; 419 420 soft->sequence = htons(lp->rfc1201.sequence++); 421 soft->split_flag = 0; /* split packets are done elsewhere */ 422 423 /* see linux/net/ethernet/eth.c to see where I got the following */ 424 425 if (dev->flags & (IFF_LOOPBACK | IFF_NOARP)) { 426 /* 427 * FIXME: fill in the last byte of the dest ipaddr here to better 428 * comply with RFC1051 in "noarp" mode. For now, always broadcasting 429 * will probably at least get packets sent out :) 430 */ 431 pkt->hard.dest = 0; 432 return hdr_size; 433 } 434 /* otherwise, drop in the dest address */ 435 pkt->hard.dest = daddr; 436 return hdr_size; 437 } 438 439 440 static void load_pkt(struct net_device *dev, struct arc_hardware *hard, 441 struct arc_rfc1201 *soft, int softlen, int bufnum) 442 { 443 struct arcnet_local *lp = netdev_priv(dev); 444 int ofs; 445 446 /* assume length <= XMTU: someone should have handled that by now. */ 447 448 if (softlen > MinTU) { 449 hard->offset[0] = 0; 450 hard->offset[1] = ofs = 512 - softlen; 451 } else if (softlen > MTU) { /* exception packet - add an extra header */ 452 struct arc_rfc1201 excsoft; 453 454 excsoft.proto = soft->proto; 455 excsoft.split_flag = 0xff; 456 excsoft.sequence = htons(0xffff); 457 458 hard->offset[0] = 0; 459 ofs = 512 - softlen; 460 hard->offset[1] = ofs - RFC1201_HDR_SIZE; 461 lp->hw.copy_to_card(dev, bufnum, ofs - RFC1201_HDR_SIZE, 462 &excsoft, RFC1201_HDR_SIZE); 463 } else 464 hard->offset[0] = ofs = 256 - softlen; 465 466 lp->hw.copy_to_card(dev, bufnum, 0, hard, ARC_HDR_SIZE); 467 lp->hw.copy_to_card(dev, bufnum, ofs, soft, softlen); 468 469 lp->lastload_dest = hard->dest; 470 } 471 472 473 static int prepare_tx(struct net_device *dev, struct archdr *pkt, int length, 474 int bufnum) 475 { 476 struct arcnet_local *lp = netdev_priv(dev); 477 const int maxsegsize = XMTU - RFC1201_HDR_SIZE; 478 struct Outgoing *out; 479 480 481 BUGMSG(D_DURING, "prepare_tx: txbufs=%d/%d/%d\n", 482 lp->next_tx, lp->cur_tx, bufnum); 483 484 length -= ARC_HDR_SIZE; /* hard header is not included in packet length */ 485 pkt->soft.rfc1201.split_flag = 0; 486 487 /* need to do a split packet? */ 488 if (length > XMTU) { 489 out = &lp->outgoing; 490 491 out->length = length - RFC1201_HDR_SIZE; 492 out->dataleft = lp->outgoing.length; 493 out->numsegs = (out->dataleft + maxsegsize - 1) / maxsegsize; 494 out->segnum = 0; 495 496 BUGMSG(D_DURING, "rfc1201 prep_tx: ready for %d-segment split " 497 "(%d bytes, seq=%d)\n", out->numsegs, out->length, 498 pkt->soft.rfc1201.sequence); 499 500 return 0; /* not done */ 501 } 502 /* just load the packet into the buffers and send it off */ 503 load_pkt(dev, &pkt->hard, &pkt->soft.rfc1201, length, bufnum); 504 505 return 1; /* done */ 506 } 507 508 509 static int continue_tx(struct net_device *dev, int bufnum) 510 { 511 struct arcnet_local *lp = netdev_priv(dev); 512 struct Outgoing *out = &lp->outgoing; 513 struct arc_hardware *hard = &out->pkt->hard; 514 struct arc_rfc1201 *soft = &out->pkt->soft.rfc1201, *newsoft; 515 int maxsegsize = XMTU - RFC1201_HDR_SIZE; 516 int seglen; 517 518 BUGMSG(D_DURING, 519 "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n", 520 out->segnum, out->numsegs, soft->sequence); 521 522 /* the "new" soft header comes right before the data chunk */ 523 newsoft = (struct arc_rfc1201 *) 524 (out->pkt->soft.raw + out->length - out->dataleft); 525 526 if (!out->segnum) /* first packet; newsoft == soft */ 527 newsoft->split_flag = ((out->numsegs - 2) << 1) | 1; 528 else { 529 newsoft->split_flag = out->segnum << 1; 530 newsoft->proto = soft->proto; 531 newsoft->sequence = soft->sequence; 532 } 533 534 seglen = maxsegsize; 535 if (seglen > out->dataleft) 536 seglen = out->dataleft; 537 out->dataleft -= seglen; 538 539 load_pkt(dev, hard, newsoft, seglen + RFC1201_HDR_SIZE, bufnum); 540 541 out->segnum++; 542 if (out->segnum >= out->numsegs) 543 return 1; 544 else 545 return 0; 546 } 547