4abb20e26eadc8b28fce61d5531b5eef6000df36
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
);
85 * Determine a packet's protocol ID.
87 * With ARCnet we have to convert everything to Ethernet-style stuff.
89 static __be16
type_trans(struct sk_buff
*skb
, struct net_device
*dev
)
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
;
95 /* Pull off the arcnet header. */
96 skb_reset_mac_header(skb
);
97 skb_pull(skb
, hdr_size
);
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
;
106 /* now return the protocol number */
107 switch (soft
->proto
) {
109 return htons(ETH_P_IP
);
111 return htons(ETH_P_IPV6
);
113 return htons(ETH_P_ARP
);
115 return htons(ETH_P_RARP
);
118 case ARC_P_NOVELL_EC
:
119 return htons(ETH_P_802_3
);
121 dev
->stats
.rx_errors
++;
122 dev
->stats
.rx_crc_errors
++;
126 return htons(ETH_P_IP
);
129 /* packet receiver */
130 static void rx(struct net_device
*dev
, int bufnum
,
131 struct archdr
*pkthdr
, int length
)
133 struct arcnet_local
*lp
= netdev_priv(dev
);
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
];
140 BUGMSG(D_DURING
, "it's an RFC1201 packet (length=%d)\n", length
);
147 if (soft
->split_flag
== 0xFF) { /* Exception Packet */
148 if (length
>= 4 + RFC1201_HDR_SIZE
) {
149 BUGMSG(D_DURING
, "compensating for exception packet\n");
151 BUGMSG(D_EXTRA
, "short RFC1201 exception packet from %02Xh",
156 /* skip over 4-byte junkola */
159 lp
->hw
.copy_from_card(dev
, bufnum
, 512 - length
,
160 soft
, sizeof(pkt
->soft
));
162 if (!soft
->split_flag
) { /* not split */
163 BUGMSG(D_RX
, "incoming is not split (splitflag=%d)\n",
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
++;
175 in
->sequence
= soft
->sequence
;
177 skb
= alloc_skb(length
+ ARC_HDR_SIZE
, GFP_ATOMIC
);
179 BUGMSG(D_NORMAL
, "Memory squeeze, dropping packet.\n");
180 dev
->stats
.rx_dropped
++;
183 skb_put(skb
, length
+ ARC_HDR_SIZE
);
186 pkt
= (struct archdr
*)skb
->data
;
187 soft
= &pkt
->soft
.rfc1201
;
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
));
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.
201 if (soft
->proto
== ARC_P_ARP
) {
202 struct arphdr
*arp
= (struct arphdr
*)soft
->payload
;
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
);
208 if (!*cptr
) { /* is saddr = 00? */
210 "ARP source address was 00h, set to %02Xh.\n",
212 dev
->stats
.rx_crc_errors
++;
215 BUGMSG(D_DURING
, "ARP source address (%Xh) is fine.\n",
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
++;
225 BUGLVL(D_SKB
) arcnet_dump_skb(dev
, skb
, "rx");
227 skb
->protocol
= type_trans(skb
, dev
);
229 } else { /* split packet */
231 * NOTE: MSDOS ARP packet correction should only need to apply to
232 * unsplit packets, since ARP packets are so short.
234 * My interpretation of the RFC1201 document is that if a packet is
235 * received out of order, the entire assembly process should be
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.
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.
247 BUGMSG(D_RX
, "packet is split (splitflag=%d, seq=%d)\n",
248 soft
->split_flag
, in
->sequence
);
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
,
254 dev_kfree_skb_irq(in
->skb
);
256 dev
->stats
.rx_errors
++;
257 dev
->stats
.rx_missed_errors
++;
258 in
->lastpacket
= in
->numpackets
= 0;
260 if (soft
->split_flag
& 1) { /* first packet in split */
261 BUGMSG(D_RX
, "brand new splitpacket (splitflag=%d)\n",
263 if (in
->skb
) { /* already assembling one! */
264 BUGMSG(D_EXTRA
, "aborting previous (seq=%d) assembly "
265 "(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;
304 * if we're not assembling, there's no point trying to
308 if (lp
->rfc1201
.aborted_seq
!= soft
->sequence
) {
309 BUGMSG(D_EXTRA
, "can't continue split without starting "
310 "first! (splitflag=%d, seq=%d, aborted=%d)\n",
311 soft
->split_flag
, soft
->sequence
,
312 lp
->rfc1201
.aborted_seq
);
313 dev
->stats
.rx_errors
++;
314 dev
->stats
.rx_missed_errors
++;
319 if (packetnum
!= in
->lastpacket
) { /* not the right flag! */
320 /* harmless duplicate? ignore. */
321 if (packetnum
<= in
->lastpacket
- 1) {
322 BUGMSG(D_EXTRA
, "duplicate splitpacket ignored! (splitflag=%d)\n",
324 dev
->stats
.rx_errors
++;
325 dev
->stats
.rx_frame_errors
++;
328 /* "bad" duplicate, kill reassembly */
329 BUGMSG(D_EXTRA
, "out-of-order splitpacket, reassembly "
330 "(seq=%d) aborted (splitflag=%d, seq=%d)\n",
331 in
->sequence
, soft
->split_flag
, soft
->sequence
);
332 lp
->rfc1201
.aborted_seq
= soft
->sequence
;
333 dev_kfree_skb_irq(in
->skb
);
335 dev
->stats
.rx_errors
++;
336 dev
->stats
.rx_missed_errors
++;
337 in
->lastpacket
= in
->numpackets
= 0;
340 pkt
= (struct archdr
*)in
->skb
->data
;
341 soft
= &pkt
->soft
.rfc1201
;
346 lp
->hw
.copy_from_card(dev
, bufnum
, ofs
+ RFC1201_HDR_SIZE
,
347 skb
->data
+ skb
->len
,
348 length
- RFC1201_HDR_SIZE
);
349 skb_put(skb
, length
- RFC1201_HDR_SIZE
);
352 if (in
->lastpacket
== in
->numpackets
) {
354 in
->lastpacket
= in
->numpackets
= 0;
356 BUGMSG(D_SKB_SIZE
, "skb: received %d bytes from %02X (unsplit)\n",
357 skb
->len
, pkt
->hard
.source
);
358 BUGMSG(D_SKB_SIZE
, "skb: received %d bytes from %02X (split)\n",
359 skb
->len
, pkt
->hard
.source
);
360 BUGLVL(D_SKB
) arcnet_dump_skb(dev
, skb
, "rx");
362 skb
->protocol
= type_trans(skb
, dev
);
368 /* Create the ARCnet hard/soft headers for RFC1201. */
369 static int build_header(struct sk_buff
*skb
, struct net_device
*dev
,
370 unsigned short type
, uint8_t daddr
)
372 struct arcnet_local
*lp
= netdev_priv(dev
);
373 int hdr_size
= ARC_HDR_SIZE
+ RFC1201_HDR_SIZE
;
374 struct archdr
*pkt
= (struct archdr
*)skb_push(skb
, hdr_size
);
375 struct arc_rfc1201
*soft
= &pkt
->soft
.rfc1201
;
377 /* set the protocol ID according to RFC1201 */
380 soft
->proto
= ARC_P_IP
;
383 soft
->proto
= ARC_P_IPV6
;
386 soft
->proto
= ARC_P_ARP
;
389 soft
->proto
= ARC_P_RARP
;
394 soft
->proto
= ARC_P_IPX
;
397 soft
->proto
= ARC_P_ATALK
;
400 BUGMSG(D_NORMAL
, "RFC1201: I don't understand protocol %d (%Xh)\n",
402 dev
->stats
.tx_errors
++;
403 dev
->stats
.tx_aborted_errors
++;
408 * Set the source hardware address.
410 * This is pretty pointless for most purposes, but it can help in
411 * debugging. ARCnet does not allow us to change the source address in
412 * the actual packet sent)
414 pkt
->hard
.source
= *dev
->dev_addr
;
416 soft
->sequence
= htons(lp
->rfc1201
.sequence
++);
417 soft
->split_flag
= 0; /* split packets are done elsewhere */
419 /* see linux/net/ethernet/eth.c to see where I got the following */
421 if (dev
->flags
& (IFF_LOOPBACK
| IFF_NOARP
)) {
423 * FIXME: fill in the last byte of the dest ipaddr here to better
424 * comply with RFC1051 in "noarp" mode. For now, always broadcasting
425 * will probably at least get packets sent out :)
430 /* otherwise, drop in the dest address */
431 pkt
->hard
.dest
= daddr
;
435 static void load_pkt(struct net_device
*dev
, struct arc_hardware
*hard
,
436 struct arc_rfc1201
*soft
, int softlen
, int bufnum
)
438 struct arcnet_local
*lp
= netdev_priv(dev
);
441 /* assume length <= XMTU: someone should have handled that by now. */
443 if (softlen
> MinTU
) {
445 hard
->offset
[1] = ofs
= 512 - softlen
;
446 } else if (softlen
> MTU
) { /* exception packet - add an extra header */
447 struct arc_rfc1201 excsoft
;
449 excsoft
.proto
= soft
->proto
;
450 excsoft
.split_flag
= 0xff;
451 excsoft
.sequence
= htons(0xffff);
455 hard
->offset
[1] = ofs
- RFC1201_HDR_SIZE
;
456 lp
->hw
.copy_to_card(dev
, bufnum
, ofs
- RFC1201_HDR_SIZE
,
457 &excsoft
, RFC1201_HDR_SIZE
);
459 hard
->offset
[0] = ofs
= 256 - softlen
;
462 lp
->hw
.copy_to_card(dev
, bufnum
, 0, hard
, ARC_HDR_SIZE
);
463 lp
->hw
.copy_to_card(dev
, bufnum
, ofs
, soft
, softlen
);
465 lp
->lastload_dest
= hard
->dest
;
468 static int prepare_tx(struct net_device
*dev
, struct archdr
*pkt
, int length
,
471 struct arcnet_local
*lp
= netdev_priv(dev
);
472 const int maxsegsize
= XMTU
- RFC1201_HDR_SIZE
;
473 struct Outgoing
*out
;
475 BUGMSG(D_DURING
, "prepare_tx: txbufs=%d/%d/%d\n",
476 lp
->next_tx
, lp
->cur_tx
, bufnum
);
478 length
-= ARC_HDR_SIZE
; /* hard header is not included in packet length */
479 pkt
->soft
.rfc1201
.split_flag
= 0;
481 /* need to do a split packet? */
485 out
->length
= length
- RFC1201_HDR_SIZE
;
486 out
->dataleft
= lp
->outgoing
.length
;
487 out
->numsegs
= (out
->dataleft
+ maxsegsize
- 1) / maxsegsize
;
490 BUGMSG(D_DURING
, "rfc1201 prep_tx: ready for %d-segment split "
491 "(%d bytes, seq=%d)\n", out
->numsegs
, out
->length
,
492 pkt
->soft
.rfc1201
.sequence
);
494 return 0; /* not done */
496 /* just load the packet into the buffers and send it off */
497 load_pkt(dev
, &pkt
->hard
, &pkt
->soft
.rfc1201
, length
, bufnum
);
502 static int continue_tx(struct net_device
*dev
, int bufnum
)
504 struct arcnet_local
*lp
= netdev_priv(dev
);
505 struct Outgoing
*out
= &lp
->outgoing
;
506 struct arc_hardware
*hard
= &out
->pkt
->hard
;
507 struct arc_rfc1201
*soft
= &out
->pkt
->soft
.rfc1201
, *newsoft
;
508 int maxsegsize
= XMTU
- RFC1201_HDR_SIZE
;
512 "rfc1201 continue_tx: loading segment %d(+1) of %d (seq=%d)\n",
513 out
->segnum
, out
->numsegs
, soft
->sequence
);
515 /* the "new" soft header comes right before the data chunk */
516 newsoft
= (struct arc_rfc1201
*)
517 (out
->pkt
->soft
.raw
+ out
->length
- out
->dataleft
);
519 if (!out
->segnum
) /* first packet; newsoft == soft */
520 newsoft
->split_flag
= ((out
->numsegs
- 2) << 1) | 1;
522 newsoft
->split_flag
= out
->segnum
<< 1;
523 newsoft
->proto
= soft
->proto
;
524 newsoft
->sequence
= soft
->sequence
;
528 if (seglen
> out
->dataleft
)
529 seglen
= out
->dataleft
;
530 out
->dataleft
-= seglen
;
532 load_pkt(dev
, hard
, newsoft
, seglen
+ RFC1201_HDR_SIZE
, bufnum
);
535 if (out
->segnum
>= out
->numsegs
)
This page took 0.048149 seconds and 4 git commands to generate.