ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/ether.cpp
Revision: 1.8
Committed: 2005-07-03T08:23:42Z (19 years, 4 months ago) by gbeauche
Branch: MAIN
Changes since 1.7: +56 -6 lines
Log Message:
First round of patches to increase ethernet code portability.

File Contents

# User Rev Content
1 cebix 1.1 /*
2     * ether.cpp - SheepShaver Ethernet Device Driver (DLPI)
3     *
4 gbeauche 1.7 * SheepShaver (C) 1997-2005 Marc Hellwig and Christian Bauer
5 cebix 1.1 *
6     * This program is free software; you can redistribute it and/or modify
7     * it under the terms of the GNU General Public License as published by
8     * the Free Software Foundation; either version 2 of the License, or
9     * (at your option) any later version.
10     *
11     * This program is distributed in the hope that it will be useful,
12     * but WITHOUT ANY WARRANTY; without even the implied warranty of
13     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14     * GNU General Public License for more details.
15     *
16     * You should have received a copy of the GNU General Public License
17     * along with this program; if not, write to the Free Software
18     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19     */
20    
21     /*
22     * TODO
23     * - 802.2 TEST/XID
24     * - MIB statistics
25     */
26    
27     #include <string.h>
28    
29     #include "sysdeps.h"
30 gbeauche 1.8 #include "cpu_emulation.h"
31 cebix 1.1 #include "ether.h"
32     #include "ether_defs.h"
33     #include "macos_util.h"
34     #include "emul_op.h"
35     #include "main.h"
36    
37     #define DEBUG 0
38     #include "debug.h"
39    
40 gbeauche 1.6 #if DIRECT_ADDRESSING
41     #warning "This code is not direct addressing clean"
42     #endif
43 cebix 1.1
44     // Packet types
45     enum {
46     kPktDIX = 0,
47     kPkt8022SAP = 1,
48     kPkt8022GroupSAP = 2,
49     kPkt8022SNAP = 3,
50     kPktIPX = 4,
51     kPktUnknown = 5
52     };
53    
54    
55     /*
56     * Stream private data structure
57     */
58    
59     static const int kGroupSAPMapSize = 128/32; // Number of 32-bit values we need for 128 bits
60     static const int kGSshift = 6;
61     static const int kGSmask = 0x1F;
62    
63     struct multicast_node {
64 gbeauche 1.3 nw_multicast_node_p next;
65 cebix 1.1 uint8 addr[kEnetPhysicalAddressLength];
66     };
67    
68     struct DLPIStream {
69     void SetGroupSAP(uint8 sap)
70     {
71     group_sap[sap >> kGSshift] |= (1L << ((sap >> 1) & kGSmask));
72     }
73    
74     void ClearGroupSAP(uint8 sap)
75     {
76     group_sap[sap >> kGSshift] &= ~(1L << ((sap >> 1) & kGSmask));
77     }
78    
79     void ClearAllGroupSAPs(void)
80     {
81     for (int i=0; i<kGroupSAPMapSize; i++)
82     group_sap[i] = 0;
83     }
84    
85     bool TestGroupSAP(uint8 sap)
86     {
87     return group_sap[sap >> kGSshift] & (1L << ((sap >> 1) & kGSmask));
88     }
89    
90     void AddMulticast(uint8 *addr)
91     {
92 gbeauche 1.6 multicast_node *n = (multicast_node *)Mac2HostAddr(Mac_sysalloc(sizeof(multicast_node)));
93 cebix 1.1 memcpy(n->addr, addr, kEnetPhysicalAddressLength);
94     n->next = multicast_list;
95     multicast_list = n;
96     }
97    
98     void RemoveMulticast(uint8 *addr)
99     {
100     multicast_node *p = multicast_list;
101     while (p) {
102     if (memcmp(addr, p->addr, kEnetPhysicalAddressLength) == 0)
103     goto found;
104     p = p->next;
105     }
106     return;
107     found:
108     multicast_node *q = (multicast_node *)&multicast_list;
109     while (q) {
110     if (q->next == p) {
111     q->next = p->next;
112 gbeauche 1.6 Mac_sysfree(Host2MacAddr((uint8 *)p));
113 cebix 1.1 return;
114     }
115     q = q->next;
116     }
117     }
118    
119     uint8 *IsMulticastRegistered(uint8 *addr)
120     {
121     multicast_node *n = multicast_list;
122     while (n) {
123     if (memcmp(addr, n->addr, kEnetPhysicalAddressLength) == 0)
124     return n->addr;
125     n = n->next;
126     }
127     return NULL;
128     }
129    
130 gbeauche 1.3 nw_uint32 minor_num; // Minor device number of this stream
131     nw_uint32 dlpi_state; // DLPI state of this stream
132     nw_uint32 flags; // Flags
133     nw_uint16 dlsap; // SAP bound to this stream
134     nw_bool framing_8022; // Using 802.2 framing? This is only used to report the MAC type for DL_INFO_ACK and can be set with an ioctl() call
135     nw_queue_p rdq; // Read queue for this stream
136     nw_uint32 group_sap[kGroupSAPMapSize]; // Map of bound group SAPs
137     uint8 snap[k8022SNAPLength]; // SNAP bound to this stream
138     nw_multicast_node_p multicast_list; // List of enabled multicast addresses
139     };
140    
141     // Hack to make DLPIStream list initialization early to NULL (do we really need this?)
142     struct DLPIStreamInit {
143     DLPIStreamInit(nw_DLPIStream_p *dlpi_stream_p) { *dlpi_stream_p = NULL; }
144 cebix 1.1 };
145    
146     // Stream flags
147     enum {
148     kSnapStream = 0x00000001,
149     kAcceptMulticasts = 0x00000002,
150     kAcceptAll8022Packets = 0x00000004,
151     kFastPathMode = 0x00000008
152     };
153    
154     // List of opened streams (used internally by OpenTransport)
155 gbeauche 1.3 static nw_DLPIStream_p dlpi_stream_list;
156     static DLPIStreamInit dlpi_stream_init(&dlpi_stream_list);
157 cebix 1.1
158     // Are we open?
159     bool ether_driver_opened = false;
160    
161     // Our ethernet hardware address
162     static uint8 hardware_address[6] = {0, 0, 0, 0, 0, 0};
163    
164     // Statistics
165     int32 num_wput = 0;
166     int32 num_error_acks = 0;
167     int32 num_tx_packets = 0;
168     int32 num_tx_raw_packets = 0;
169     int32 num_tx_normal_packets = 0;
170     int32 num_tx_buffer_full = 0;
171     int32 num_rx_packets = 0;
172     int32 num_ether_irq = 0;
173     int32 num_unitdata_ind = 0;
174     int32 num_rx_fastpath = 0;
175     int32 num_rx_no_mem = 0;
176     int32 num_rx_dropped = 0;
177     int32 num_rx_stream_not_ready = 0;
178     int32 num_rx_no_unitdata_mem = 0;
179    
180    
181     // Function pointers of imported functions
182     typedef mblk_t *(*allocb_ptr)(size_t size, int pri);
183     static uint32 allocb_tvect = 0;
184     mblk_t *allocb(size_t arg1, int arg2)
185     {
186 gbeauche 1.5 return (mblk_t *)Mac2HostAddr(CallMacOS2(allocb_ptr, allocb_tvect, arg1, arg2));
187 cebix 1.1 }
188     typedef void (*freeb_ptr)(mblk_t *);
189     static uint32 freeb_tvect = 0;
190     static inline void freeb(mblk_t *arg1)
191     {
192     CallMacOS1(freeb_ptr, freeb_tvect, arg1);
193     }
194     typedef int16 (*freemsg_ptr)(mblk_t *);
195     static uint32 freemsg_tvect = 0;
196     static inline int16 freemsg(mblk_t *arg1)
197     {
198     return (int16)CallMacOS1(freemsg_ptr, freemsg_tvect, arg1);
199     }
200     typedef mblk_t *(*copyb_ptr)(mblk_t *);
201     static uint32 copyb_tvect = 0;
202     static inline mblk_t *copyb(mblk_t *arg1)
203     {
204 gbeauche 1.5 return (mblk_t *)Mac2HostAddr(CallMacOS1(copyb_ptr, copyb_tvect, arg1));
205 cebix 1.1 }
206     typedef mblk_t *(*dupmsg_ptr)(mblk_t *);
207     static uint32 dupmsg_tvect = 0;
208     static inline mblk_t *dupmsg(mblk_t *arg1)
209     {
210 gbeauche 1.5 return (mblk_t *)Mac2HostAddr(CallMacOS1(dupmsg_ptr, dupmsg_tvect, arg1));
211 cebix 1.1 }
212     typedef mblk_t *(*getq_ptr)(queue_t *);
213     static uint32 getq_tvect = 0;
214     static inline mblk_t *getq(queue_t *arg1)
215     {
216 gbeauche 1.5 return (mblk_t *)Mac2HostAddr(CallMacOS1(getq_ptr, getq_tvect, arg1));
217 cebix 1.1 }
218     typedef int (*putq_ptr)(queue_t *, mblk_t *);
219     static uint32 putq_tvect = 0;
220     static inline int putq(queue_t *arg1, mblk_t *arg2)
221     {
222     return (int)CallMacOS2(putq_ptr, putq_tvect, arg1, arg2);
223     }
224     typedef int (*putnext_ptr)(queue_t *, mblk_t *);
225     static uint32 putnext_tvect = 0;
226     static inline int putnext(queue_t *arg1, mblk_t *arg2)
227     {
228     return (int)CallMacOS2(putnext_ptr, putnext_tvect, arg1, arg2);
229     }
230     typedef int (*putnextctl1_ptr)(queue_t *, int type, int c);
231     static uint32 putnextctl1_tvect = 0;
232     static inline int putnextctl1(queue_t *arg1, int arg2, int arg3)
233     {
234     return (int)CallMacOS3(putnextctl1_ptr, putnextctl1_tvect, arg1, arg2, arg3);
235     }
236     typedef int (*canputnext_ptr)(queue_t *);
237     static uint32 canputnext_tvect = 0;
238     static inline int canputnext(queue_t *arg1)
239     {
240     return (int)CallMacOS1(canputnext_ptr, canputnext_tvect, arg1);
241     }
242     typedef int (*qreply_ptr)(queue_t *, mblk_t *);
243     static uint32 qreply_tvect = 0;
244     static inline int qreply(queue_t *arg1, mblk_t *arg2)
245     {
246     return (int)CallMacOS2(qreply_ptr, qreply_tvect, arg1, arg2);
247     }
248     typedef void (*flushq_ptr)(queue_t *, int flag);
249     static uint32 flushq_tvect = 0;
250     static inline void flushq(queue_t *arg1, int arg2)
251     {
252     CallMacOS2(flushq_ptr, flushq_tvect, arg1, arg2);
253     }
254     typedef int (*msgdsize_ptr)(const mblk_t *);
255     static uint32 msgdsize_tvect = 0;
256     static inline int msgdsize(const mblk_t *arg1)
257     {
258     return (int)CallMacOS1(msgdsize_ptr, msgdsize_tvect, arg1);
259     }
260     typedef void (*otenterint_ptr)(void);
261     static uint32 otenterint_tvect = 0;
262     void OTEnterInterrupt(void)
263     {
264     CallMacOS(otenterint_ptr, otenterint_tvect);
265     }
266     typedef void (*otleaveint_ptr)(void);
267     static uint32 otleaveint_tvect = 0;
268     void OTLeaveInterrupt(void)
269     {
270     CallMacOS(otleaveint_ptr, otleaveint_tvect);
271     }
272     typedef int (*mi_open_comm_ptr)(DLPIStream **mi_opp_orig, size_t size, queue_t *q, void *dev, int flag, int sflag, void *credp);
273     static uint32 mi_open_comm_tvect = 0;
274     static inline int mi_open_comm(DLPIStream **arg1, size_t arg2, queue_t *arg3, void *arg4, int arg5, int arg6, void *arg7)
275     {
276     return (int)CallMacOS7(mi_open_comm_ptr, mi_open_comm_tvect, arg1, arg2, arg3, arg4, arg5, arg6, arg7);
277     }
278     typedef int (*mi_close_comm_ptr)(DLPIStream **mi_opp_orig, queue_t *q);
279     static uint32 mi_close_comm_tvect = 0;
280     static inline int mi_close_comm(DLPIStream **arg1, queue_t *arg2)
281     {
282     return (int)CallMacOS2(mi_close_comm_ptr, mi_close_comm_tvect, arg1, arg2);
283     }
284     typedef DLPIStream *(*mi_next_ptr_ptr)(DLPIStream *);
285     static uint32 mi_next_ptr_tvect = 0;
286     static inline DLPIStream *mi_next_ptr(DLPIStream *arg1)
287     {
288 gbeauche 1.5 return (DLPIStream *)Mac2HostAddr(CallMacOS1(mi_next_ptr_ptr, mi_next_ptr_tvect, arg1));
289 cebix 1.1 }
290    
291    
292     // Prototypes
293     static void ether_ioctl(DLPIStream *the_stream, queue_t* q, mblk_t* mp);
294     static void ether_flush(queue_t* q, mblk_t* mp);
295     static mblk_t *build_tx_packet_header(DLPIStream *the_stream, mblk_t *mp, bool fast_path);
296     static void transmit_packet(mblk_t *mp);
297     static void DLPI_error_ack(DLPIStream *the_stream, queue_t *q, mblk_t *ack_mp, uint32 prim, uint32 err, uint32 uerr);
298     static void DLPI_ok_ack(DLPIStream *the_stream, queue_t *q, mblk_t *ack_mp, uint32 prim);
299     static void DLPI_info(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
300     static void DLPI_phys_addr(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
301     static void DLPI_bind(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
302     static void DLPI_unbind(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
303     static void DLPI_subs_bind(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
304     static void DLPI_subs_unbind(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
305     static void DLPI_enable_multi(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
306     static void DLPI_disable_multi(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
307     static void DLPI_unit_data(DLPIStream *the_stream, queue_t *q, mblk_t *mp);
308    
309    
310     /*
311     * Initialize ethernet stream module
312     */
313    
314     uint8 InitStreamModule(void *theID)
315     {
316     D(bug("InitStreamModule\n"));
317    
318     // Don't re-open if already open
319     if (ether_driver_opened)
320     return true;
321     ether_driver_opened = false;
322    
323     // Import functions from OTKernelLib
324 gbeauche 1.6 allocb_tvect = FindLibSymbol("\013OTKernelLib", "\006allocb");
325 cebix 1.1 D(bug("allocb TVECT at %08lx\n", allocb_tvect));
326     if (allocb_tvect == 0)
327     return false;
328 gbeauche 1.6 freeb_tvect = FindLibSymbol("\013OTKernelLib", "\005freeb");
329 cebix 1.1 D(bug("freeb TVECT at %08lx\n", freeb_tvect));
330     if (freeb_tvect == 0)
331     return false;
332 gbeauche 1.6 freemsg_tvect = FindLibSymbol("\013OTKernelLib", "\007freemsg");
333 cebix 1.1 D(bug("freemsg TVECT at %08lx\n", freemsg_tvect));
334     if (freemsg_tvect == 0)
335     return false;
336 gbeauche 1.6 copyb_tvect = FindLibSymbol("\013OTKernelLib", "\005copyb");
337 cebix 1.1 D(bug("copyb TVECT at %08lx\n", copyb_tvect));
338     if (copyb_tvect == 0)
339     return false;
340 gbeauche 1.6 dupmsg_tvect = FindLibSymbol("\013OTKernelLib", "\006dupmsg");
341 cebix 1.1 D(bug("dupmsg TVECT at %08lx\n", dupmsg_tvect));
342     if (dupmsg_tvect == 0)
343     return false;
344 gbeauche 1.6 getq_tvect = FindLibSymbol("\013OTKernelLib", "\004getq");
345 cebix 1.1 D(bug("getq TVECT at %08lx\n", getq_tvect));
346     if (getq_tvect == 0)
347     return false;
348 gbeauche 1.6 putq_tvect = FindLibSymbol("\013OTKernelLib", "\004putq");
349 cebix 1.1 D(bug("putq TVECT at %08lx\n", putq_tvect));
350     if (putq_tvect == 0)
351     return false;
352 gbeauche 1.6 putnext_tvect = FindLibSymbol("\013OTKernelLib", "\007putnext");
353 cebix 1.1 D(bug("putnext TVECT at %08lx\n", putnext_tvect));
354     if (putnext_tvect == 0)
355     return false;
356 gbeauche 1.6 putnextctl1_tvect = FindLibSymbol("\013OTKernelLib", "\013putnextctl1");
357 cebix 1.1 D(bug("putnextctl1 TVECT at %08lx\n", putnextctl1_tvect));
358     if (putnextctl1_tvect == 0)
359     return false;
360 gbeauche 1.6 canputnext_tvect = FindLibSymbol("\013OTKernelLib", "\012canputnext");
361 cebix 1.1 D(bug("canputnext TVECT at %08lx\n", canputnext_tvect));
362     if (canputnext_tvect == 0)
363     return false;
364 gbeauche 1.6 qreply_tvect = FindLibSymbol("\013OTKernelLib", "\006qreply");
365 cebix 1.1 D(bug("qreply TVECT at %08lx\n", qreply_tvect));
366     if (qreply_tvect == 0)
367     return false;
368 gbeauche 1.6 flushq_tvect = FindLibSymbol("\013OTKernelLib", "\006flushq");
369 cebix 1.1 D(bug("flushq TVECT at %08lx\n", flushq_tvect));
370     if (flushq_tvect == 0)
371     return false;
372 gbeauche 1.6 msgdsize_tvect = FindLibSymbol("\013OTKernelLib", "\010msgdsize");
373 cebix 1.1 D(bug("msgdsize TVECT at %08lx\n", msgdsize_tvect));
374     if (msgdsize_tvect == 0)
375     return false;
376 gbeauche 1.6 otenterint_tvect = FindLibSymbol("\017OTKernelUtilLib", "\020OTEnterInterrupt");
377 cebix 1.1 D(bug("OTEnterInterrupt TVECT at %08lx\n", otenterint_tvect));
378     if (otenterint_tvect == 0)
379     return false;
380 gbeauche 1.6 otleaveint_tvect = FindLibSymbol("\017OTKernelUtilLib", "\020OTLeaveInterrupt");
381 cebix 1.1 D(bug("OTLeaveInterrupt TVECT at %08lx\n", otleaveint_tvect));
382     if (otleaveint_tvect == 0)
383     return false;
384 gbeauche 1.6 mi_open_comm_tvect = FindLibSymbol("\013OTKernelLib", "\014mi_open_comm");
385 cebix 1.1 D(bug("mi_open_comm TVECT at %08lx\n", mi_open_comm_tvect));
386     if (mi_open_comm_tvect == 0)
387     return false;
388 gbeauche 1.6 mi_close_comm_tvect = FindLibSymbol("\013OTKernelLib", "\015mi_close_comm");
389 cebix 1.1 D(bug("mi_close_comm TVECT at %08lx\n", mi_close_comm_tvect));
390     if (mi_close_comm_tvect == 0)
391     return false;
392 gbeauche 1.6 mi_next_ptr_tvect = FindLibSymbol("\013OTKernelLib", "\013mi_next_ptr");
393 cebix 1.1 D(bug("mi_next_ptr TVECT at %08lx\n", mi_next_ptr_tvect));
394     if (mi_next_ptr_tvect == 0)
395     return false;
396    
397     // Initialize stream list (which might be leftover)
398     dlpi_stream_list = NULL;
399    
400     // Ask add-on for ethernet hardware address
401 gbeauche 1.8 AO_get_ethernet_address(Host2MacAddr(hardware_address));
402 cebix 1.1
403     // Yes, we're open
404     ether_driver_opened = true;
405     return true;
406     }
407    
408    
409     /*
410     * Terminate ethernet stream module
411     */
412    
413     void TerminateStreamModule(void)
414     {
415     D(bug("TerminateStreamModule\n"));
416    
417     // This happens sometimes. I don't know why.
418     if (dlpi_stream_list != NULL)
419     printf("FATAL: TerminateStreamModule() called, but streams still open\n");
420    
421     // Sorry, we're closed
422     ether_driver_opened = false;
423     }
424    
425    
426     /*
427     * Open new stream
428     */
429    
430     int ether_open(queue_t *rdq, void *dev, int flag, int sflag, void *creds)
431     {
432     D(bug("ether_open(%p,%p,%d,%d,%p)\n", rdq, dev, flag, sflag, creds));
433    
434     // Return if driver was closed
435     if (!ether_driver_opened) {
436     printf("FATAL: ether_open(): Ethernet driver not opened\n");
437     return MAC_ENXIO;
438     }
439    
440     // If we're being reopened, just return
441     if (rdq->q_ptr != NULL)
442     return 0;
443    
444     // Allocate DLPIStream structure
445 gbeauche 1.3 int err = mi_open_comm((DLPIStream **)&dlpi_stream_list, sizeof(DLPIStream), rdq, dev, flag, sflag, creds);
446 cebix 1.1 if (err)
447     return err;
448     DLPIStream *the_stream = (DLPIStream *)rdq->q_ptr;
449     the_stream->rdq = rdq;
450     the_stream->dlpi_state = DL_UNBOUND;
451     the_stream->flags = 0;
452     the_stream->dlsap = 0;
453     the_stream->framing_8022 = false;
454     the_stream->multicast_list = NULL;
455     return 0;
456     }
457    
458    
459     /*
460     * Close stream
461     */
462    
463     int ether_close(queue_t *rdq, int flag, void *creds)
464     {
465     D(bug("ether_close(%p,%d,%p)\n", rdq, flag, creds));
466    
467     // Return if driver was closed
468     if (!ether_driver_opened) {
469     printf("FATAL: ether_close(): Ethernet driver not opened\n");
470     return MAC_ENXIO;
471     }
472    
473     // Get stream
474     DLPIStream *the_stream = (DLPIStream *)rdq->q_ptr;
475    
476     // Don't close if never opened
477     if (the_stream == NULL)
478     return 0;
479    
480     // Disable all registered multicast addresses
481     while (the_stream->multicast_list) {
482 gbeauche 1.8 AO_disable_multicast(Host2MacAddr(the_stream->multicast_list->addr));
483 cebix 1.1 the_stream->RemoveMulticast(the_stream->multicast_list->addr);
484     }
485     the_stream->multicast_list = NULL;
486    
487     // Delete the DLPIStream
488 gbeauche 1.3 return mi_close_comm((DLPIStream **)&dlpi_stream_list, rdq);
489 cebix 1.1 }
490    
491    
492     /*
493     * Put something on the write queue
494     */
495    
496     int ether_wput(queue_t *q, mblk_t *mp)
497     {
498     D(bug("ether_wput(%p,%p)\n", q, mp));
499    
500     // Return if driver was closed
501     if (!ether_driver_opened) {
502     printf("FATAL: ether_wput(): Ethernet driver not opened\n");
503     return MAC_ENXIO;
504     }
505    
506     // Get stream
507     DLPIStream *the_stream = (DLPIStream *)q->q_ptr;
508     if (the_stream == NULL)
509     return MAC_ENXIO;
510    
511 gbeauche 1.3 D(bug(" db_type %d\n", (int)mp->b_datap->db_type));
512 cebix 1.1 switch (mp->b_datap->db_type) {
513    
514     case M_DATA:
515     // Transmit raw packet
516     D(bug(" raw packet\n"));
517     num_tx_raw_packets++;
518     transmit_packet(mp);
519     break;
520    
521     case M_PROTO:
522     case M_PCPROTO: {
523 gbeauche 1.3 union DL_primitives *dlp = (union DL_primitives *)(void *)mp->b_rptr;
524 cebix 1.1 uint32 prim = dlp->dl_primitive;
525     D(bug(" dl_primitive %d\n", prim));
526     switch (prim) {
527     case DL_UNITDATA_REQ:
528     // Transmit normal packet
529     num_tx_normal_packets++;
530     DLPI_unit_data(the_stream, q, mp);
531     break;
532    
533     case DL_INFO_REQ:
534     DLPI_info(the_stream, q, mp);
535     break;
536    
537     case DL_PHYS_ADDR_REQ:
538     DLPI_phys_addr(the_stream, q, mp);
539     break;
540    
541     case DL_BIND_REQ:
542     DLPI_bind(the_stream, q, mp);
543     break;
544    
545     case DL_UNBIND_REQ:
546     DLPI_unbind(the_stream, q, mp);
547     break;
548    
549     case DL_SUBS_BIND_REQ:
550     DLPI_subs_bind(the_stream, q, mp);
551     break;
552    
553     case DL_SUBS_UNBIND_REQ:
554     DLPI_subs_unbind(the_stream, q, mp);
555     break;
556    
557     case DL_ENABMULTI_REQ:
558     DLPI_enable_multi(the_stream, q, mp);
559     break;
560    
561     case DL_DISABMULTI_REQ:
562     DLPI_disable_multi(the_stream, q, mp);
563     break;
564    
565     default:
566     D(bug("WARNING: ether_wsrv(): Unknown primitive\n"));
567     DLPI_error_ack(the_stream, q, mp, prim, DL_NOTSUPPORTED, 0);
568     break;
569     }
570     break;
571     }
572    
573     case M_IOCTL:
574     ether_ioctl(the_stream, q, mp);
575     break;
576    
577     case M_FLUSH:
578     ether_flush(q, mp);
579     break;
580    
581     default:
582     D(bug("WARNING: ether_wput(): Unknown message type\n"));
583     freemsg(mp);
584     break;
585     }
586     num_wput++;
587     return 0;
588     }
589    
590    
591     /*
592     * Dequeue and process messages from the read queue
593     */
594    
595     int ether_rsrv(queue_t *q)
596     {
597     mblk_t *mp;
598     while ((mp = getq(q)) != NULL) {
599     if (canputnext(q))
600     putnext(q, mp);
601     else {
602     freemsg(mp);
603     flushq(q, FLUSHDATA);
604     break;
605     }
606     }
607     return 0;
608     }
609    
610    
611     /*
612     * Handle ioctl calls
613     */
614    
615     static void ether_ioctl(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
616     {
617 gbeauche 1.3 struct iocblk *ioc = (struct iocblk *)(void *)mp->b_rptr;
618     D(bug(" ether_ioctl(%p,%p) cmd %d\n", q, mp, (int)ioc->ioc_cmd));
619 cebix 1.1
620     switch (ioc->ioc_cmd) {
621    
622     case I_OTSetFramingType: { // Toggles what the general info primitive returns for dl_mac_type in dl_info_ack_t structure
623     mblk_t *info_mp = mp->b_cont;
624     if (info_mp == NULL || ((info_mp->b_wptr - info_mp->b_rptr) != sizeof(uint32))) {
625     ioc->ioc_error = MAC_EINVAL;
626     goto ioctl_error;
627     }
628 gbeauche 1.3 uint32 framing_type = ntohl(*(uint32 *)(void *)info_mp->b_rptr);
629 cebix 1.1 D(bug(" I_OTSetFramingType type %d\n", framing_type));
630     if (framing_type != kOTGetFramingValue)
631     the_stream->framing_8022 = (framing_type == kOTFraming8022);
632     mp->b_cont = NULL;
633     freemsg(info_mp);
634     if (the_stream->framing_8022)
635     ioc->ioc_rval = kOTFraming8022;
636     else
637     ioc->ioc_rval = kOTFramingEthernet;
638     goto ioctl_ok;
639     }
640    
641     case DL_IOC_HDR_INFO: { // Special Mentat call, for fast transmits
642     D(bug(" DL_IOC_HDR_INFO\n"));
643     mblk_t *info_mp = mp->b_cont;
644    
645     // Copy DL_UNITDATA_REQ block
646     mblk_t *unitdata_mp = copyb(info_mp);
647     if (unitdata_mp == NULL) {
648     ioc->ioc_error = MAC_ENOMEM;
649     goto ioctl_error;
650     }
651     unitdata_mp->b_datap->db_type = M_PROTO;
652    
653     // Construct header (converts DL_UNITDATA_REQ -> M_DATA)
654     mblk_t *header_mp = build_tx_packet_header(the_stream, unitdata_mp, true);
655    
656     if (header_mp == NULL) {
657     // Could not allocate a message block large enough
658     ioc->ioc_error = MAC_ENOMEM;
659     goto ioctl_error;
660     }
661    
662     // Attach header block at the end
663     mp->b_cont->b_cont = header_mp;
664     the_stream->flags |= kFastPathMode;
665     goto ioctl_ok;
666     }
667    
668     case I_OTSetRawMode: {
669     mblk_t *info_mp = mp->b_cont;
670     dl_recv_control_t *dlrc;
671     if (info_mp == NULL || ((info_mp->b_wptr - info_mp->b_rptr) != sizeof(dlrc->dl_primitive))) {
672     ioc->ioc_error = MAC_EINVAL;
673     goto ioctl_error;
674     }
675 gbeauche 1.3 dlrc = (dl_recv_control_t *)(void *)info_mp->b_rptr;
676     D(bug(" I_OTSetRawMode primitive %d\n", (int)dlrc->dl_primitive));
677 cebix 1.1 ioc->ioc_error = MAC_EINVAL;
678     goto ioctl_error;
679     }
680    
681     default:
682     D(bug("WARNING: Unknown ether_ioctl() call\n"));
683     ioc->ioc_error = MAC_EINVAL;
684     goto ioctl_error;
685     }
686    
687     ioctl_ok:
688     ioc->ioc_count = 0;
689     for (mblk_t *mp1 = mp; (mp1 = mp1->b_cont) != NULL;)
690     ioc->ioc_count += mp1->b_wptr - mp1->b_rptr;
691     ioc->ioc_error = 0;
692     mp->b_datap->db_type = M_IOCACK;
693     qreply(q, mp);
694     return;
695    
696     ioctl_error:
697     mp->b_datap->db_type = M_IOCNAK;
698     qreply(q, mp);
699     return;
700     }
701    
702    
703     /*
704     * Flush call, send it up to the read side of the stream
705     */
706    
707     static void ether_flush(queue_t* q, mblk_t* mp)
708     {
709     D(bug(" ether_flush(%p,%p)\n", q, mp));
710    
711     uint8 *rptr = mp->b_rptr;
712     if (*rptr & FLUSHW)
713     flushq(q, FLUSHALL);
714     if (*rptr & FLUSHR) {
715     flushq(RD(q), FLUSHALL);
716     *rptr &= ~FLUSHW;
717     qreply(q, mp);
718     } else
719     freemsg(mp);
720     }
721    
722    
723     /*
724     * Classify packet into the different types of protocols
725     */
726    
727     static uint16 classify_packet_type(uint16 primarySAP, uint16 secondarySAP)
728     {
729     if (primarySAP >= kMinDIXSAP)
730     return kPktDIX;
731    
732     if ((primarySAP == kIPXSAP) && (secondarySAP == kIPXSAP))
733     return kPktIPX;
734    
735     if (primarySAP == kSNAPSAP)
736     return kPkt8022SNAP;
737    
738     if (primarySAP <= k8022GlobalSAP)
739     return kPkt8022SAP;
740    
741     return kPktUnknown;
742     }
743    
744    
745     /*
746     * Check if the address is a multicast, broadcast or standard address
747     */
748    
749     static int32 get_address_type(uint8 *addr)
750     {
751     if (addr[0] & 1) { // Multicast/broadcast flag
752     if (OTIs48BitBroadcastAddress(addr))
753     return keaBroadcast;
754     else
755     return keaMulticast;
756     } else
757     return keaStandardAddress;
758     }
759    
760    
761     /*
762     * Reuse a message block, make room for more data
763     */
764    
765     static mblk_t *reuse_message_block(mblk_t *mp, uint16 needed_size)
766     {
767     mblk_t *nmp;
768    
769     if ((mp->b_datap->db_ref == 1) && ((mp->b_datap->db_lim - mp->b_datap->db_base) >= needed_size)) {
770     mp->b_datap->db_type = M_DATA;
771     mp->b_rptr = mp->b_datap->db_base;
772     mp->b_wptr = mp->b_datap->db_base + needed_size;
773     } else {
774     nmp = mp->b_cont; // Grab the M_DATA blocks
775     mp->b_cont = NULL; // Detach the M_(PC)PROTO
776     freemsg(mp); // Free the M_(PC)PROTO
777     mp = nmp; // Point to the M_DATA blocks
778    
779     // Try to get space on the first M_DATA block
780     if (mp && (mp->b_datap->db_ref == 1) && ((mp->b_rptr - mp->b_datap->db_base) >= needed_size))
781     mp->b_rptr -= needed_size;
782     else {
783     // Try to allocate a new message
784     if ((nmp = allocb(needed_size, BPRI_HI)) == NULL) {
785     // Could not get a new message block so lets forget about the message altogether
786     freemsg(mp); // Free the original M_DATA portion of the message
787     mp = NULL; // Indicates the reuse failed
788     } else {
789     nmp->b_cont = mp; // Attach the new message block as the head
790     nmp->b_wptr += needed_size;
791     mp = nmp;
792     }
793     }
794     }
795    
796     return mp;
797     }
798    
799    
800     /*
801     * Built header for packet to be transmitted (convert DL_UNITDATA_REQ -> M_DATA)
802     * The passed-in message has the header info in the first message block and the data
803     * in the following blocks
804     */
805    
806     static mblk_t *build_tx_packet_header(DLPIStream *the_stream, mblk_t *mp, bool fast_path)
807     {
808     // Only handle unit_data requests
809 gbeauche 1.3 dl_unitdata_req_t *req = (dl_unitdata_req_t *)(void *)mp->b_rptr;
810 cebix 1.1 if (req->dl_primitive != DL_UNITDATA_REQ) {
811     freemsg(mp);
812     return NULL;
813     }
814    
815     // Extract destination address and its length
816     uint8 *destAddrOrig = ((uint8 *)req) + req->dl_dest_addr_offset;
817     uint32 destAddrLen = req->dl_dest_addr_length;
818     uint8 ctrl = 0x03;
819    
820     // Extract DLSAP
821     uint16 dlsap;
822     switch (destAddrLen) {
823     case kEnetPhysicalAddressLength:
824     dlsap = the_stream->dlsap;
825     break;
826     case kEnetAndSAPAddressLength:
827 gbeauche 1.3 dlsap = ntohs(*(uint16 *)(destAddrOrig + kEnetPhysicalAddressLength));
828 cebix 1.1 break;
829     case kEnetPhysicalAddressLength + k8022DLSAPLength + k8022SNAPLength: // SNAP SAP
830 gbeauche 1.3 dlsap = ntohs(*(uint16 *)(destAddrOrig + kEnetPhysicalAddressLength));
831 cebix 1.1 break;
832     default:
833     dlsap = the_stream->dlsap;
834     break;
835     }
836    
837     // Extract data size (excluding header info) and packet type
838     uint16 datasize = msgdsize(mp);
839     uint16 packetType = classify_packet_type(the_stream->dlsap, dlsap);
840    
841     // Calculate header size and protocol type/size field
842     uint16 hdrsize, proto;
843     switch (packetType) {
844     case kPktDIX:
845     hdrsize = kEnetPacketHeaderLength;
846     proto = dlsap;
847     break;
848     case kPkt8022SAP:
849     hdrsize = kEnetPacketHeaderLength + k8022BasicHeaderLength;
850     if (fast_path)
851     proto = 0;
852     else
853     proto = datasize + k8022BasicHeaderLength;
854     break;
855     case kPkt8022SNAP:
856     hdrsize = kEnetPacketHeaderLength + k8022SNAPHeaderLength;
857     if (fast_path)
858     proto = 0;
859     else
860     proto = datasize + k8022SNAPHeaderLength;
861     break;
862     case kPktIPX:
863     hdrsize = kEnetPacketHeaderLength;
864     if (fast_path)
865     proto = 0;
866     else
867     proto = datasize;
868     break;
869     default:
870     hdrsize = kEnetPacketHeaderLength;
871     proto = dlsap;
872     break;
873     }
874    
875     // We need to copy the dest address info in the message before we can reuse it
876     uint8 destAddrCopy[kMaxBoundAddrLength];
877     memcpy(destAddrCopy, destAddrOrig, destAddrLen);
878    
879     // Resize header info in message block
880     if ((mp = reuse_message_block(mp, hdrsize)) == NULL)
881     return NULL;
882 gbeauche 1.3 struct T8022FullPacketHeader *packetHeader = (struct T8022FullPacketHeader *)(void *)mp->b_rptr;
883 cebix 1.1
884     // Set protocol type/size field
885     packetHeader->fEnetPart.fProto = proto;
886    
887     // Set destination ethernet address
888     OTCopy48BitAddress(destAddrCopy, packetHeader->fEnetPart.fDestAddr);
889    
890     // Set other header fields
891     switch (packetType) {
892     case kPkt8022SAP:
893     packetHeader->f8022Part.fDSAP = (uint8)dlsap;
894     packetHeader->f8022Part.fSSAP = (uint8)the_stream->dlsap;
895     packetHeader->f8022Part.fCtrl = ctrl;
896     break;
897     case kPkt8022SNAP: {
898     uint8 *snapStart;
899     packetHeader->f8022Part.fDSAP = (uint8)dlsap;
900     packetHeader->f8022Part.fSSAP = (uint8)the_stream->dlsap;
901     packetHeader->f8022Part.fCtrl = ctrl;
902     if (destAddrLen >= kEnetAndSAPAddressLength + k8022SNAPLength)
903     snapStart = destAddrCopy + kEnetAndSAPAddressLength;
904     else
905     snapStart = the_stream->snap;
906     OTCopy8022SNAP(snapStart, packetHeader->f8022Part.fSNAP);
907     break;
908     }
909     }
910    
911     // Return updated message
912     return mp;
913     }
914    
915    
916     /*
917     * Transmit packet
918     */
919    
920     static void transmit_packet(mblk_t *mp)
921     {
922 gbeauche 1.3 EnetPacketHeader *enetHeader = (EnetPacketHeader *)(void *)mp->b_rptr;
923 cebix 1.1
924     // Fill in length in 802.3 packets
925     if (enetHeader->fProto == 0)
926     enetHeader->fProto = msgdsize(mp) - sizeof(EnetPacketHeader);
927    
928     // Fill in ethernet source address
929     OTCopy48BitAddress(hardware_address, enetHeader->fSourceAddr);
930    
931     // Tell add-on to transmit packet
932 gbeauche 1.8 AO_transmit_packet(Host2MacAddr((uint8 *)mp));
933 cebix 1.1 freemsg(mp);
934     }
935    
936    
937     /*
938     * Handle incoming packet (one stream), construct DL_UNITDATA_IND message
939     */
940    
941     static void handle_received_packet(DLPIStream *the_stream, mblk_t *mp, uint16 packet_type, int32 dest_addr_type)
942     {
943     // Find address and header length
944     uint32 addr_len;
945     uint32 header_len;
946     switch (packet_type) {
947     case kPkt8022SAP:
948     addr_len = kEnetAndSAPAddressLength;
949     header_len = kEnetPacketHeaderLength + k8022BasicHeaderLength;
950     break;
951     case kPkt8022SNAP:
952     addr_len = kEnetAndSAPAddressLength + k8022SNAPLength;
953     header_len = kEnetPacketHeaderLength + k8022SNAPHeaderLength;
954     break;
955     default: // DIX and IPX
956     addr_len = kEnetAndSAPAddressLength;
957     header_len = kEnetPacketHeaderLength;
958     break;
959     }
960    
961     // In Fast Path mode, don't send DL_UNITDATA_IND messages for unicast packets
962     if ((the_stream->flags & kFastPathMode) && dest_addr_type == keaStandardAddress) {
963     mp->b_rptr += header_len;
964     num_rx_fastpath++;
965     putq(the_stream->rdq, mp);
966     return;
967     }
968    
969     // Allocate the dl_unitdata_ind_t message
970     mblk_t *nmp;
971     if ((nmp = allocb(sizeof(dl_unitdata_ind_t) + 2*addr_len, BPRI_HI)) == NULL) {
972     freemsg(mp);
973     num_rx_no_unitdata_mem++;
974     return;
975     }
976    
977     // Set message type
978     nmp->b_datap->db_type = M_PROTO;
979 gbeauche 1.3 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t*)(void *)nmp->b_rptr;
980 cebix 1.1 ind->dl_primitive = DL_UNITDATA_IND;
981     nmp->b_wptr += (sizeof(dl_unitdata_ind_t) + 2*addr_len);
982    
983     // Link M_DATA block
984     nmp->b_cont = mp;
985    
986     // Set address fields
987     ind->dl_dest_addr_length = addr_len;
988     ind->dl_dest_addr_offset = sizeof(dl_unitdata_ind_t);
989     ind->dl_src_addr_length = addr_len;
990     ind->dl_src_addr_offset = sizeof(dl_unitdata_ind_t) + addr_len;
991    
992     // Set address type
993     ind->dl_group_address = dest_addr_type;
994    
995     // Set address fields
996 gbeauche 1.3 T8022FullPacketHeader *packetHeader = (T8022FullPacketHeader *)(void *)mp->b_rptr;
997 cebix 1.1 T8022AddressStruct *destAddr = ((T8022AddressStruct*)(nmp->b_rptr + ind->dl_dest_addr_offset));
998     T8022AddressStruct *srcAddr = ((T8022AddressStruct*)(nmp->b_rptr + ind->dl_src_addr_offset));
999    
1000     OTCopy48BitAddress(packetHeader->fEnetPart.fDestAddr, destAddr->fHWAddr);
1001     OTCopy48BitAddress(packetHeader->fEnetPart.fSourceAddr, srcAddr->fHWAddr);
1002    
1003     destAddr->fSAP = packetHeader->f8022Part.fDSAP;
1004     srcAddr->fSAP = packetHeader->f8022Part.fSSAP;
1005    
1006     if (packet_type == kPkt8022SNAP) {
1007     OTCopy8022SNAP(packetHeader->f8022Part.fSNAP, destAddr->fSNAP);
1008     OTCopy8022SNAP(packetHeader->f8022Part.fSNAP, srcAddr->fSNAP);
1009     }
1010    
1011     // "Hide" the ethernet and protocol header(s)
1012     mp->b_rptr += header_len;
1013    
1014     // Pass message up the stream
1015     num_unitdata_ind++;
1016     putq(the_stream->rdq, nmp);
1017     return;
1018     }
1019    
1020    
1021     /*
1022     * Packet received, distribute it to the streams that want it
1023     */
1024    
1025     void ether_packet_received(mblk_t *mp)
1026     {
1027     // Extract address and types
1028 gbeauche 1.3 EnetPacketHeader *pkt = (EnetPacketHeader *)(void *)mp->b_rptr;
1029 cebix 1.1 T8022FullPacketHeader *fullpkt = (T8022FullPacketHeader *)pkt;
1030     uint16 sourceSAP, destSAP;
1031     destSAP = fullpkt->fEnetPart.fProto;
1032     if (destSAP >= kMinDIXSAP) {
1033     // Classic ethernet
1034     sourceSAP = destSAP;
1035     } else {
1036     destSAP = fullpkt->f8022Part.fDSAP;
1037     sourceSAP = fullpkt->f8022Part.fSSAP;
1038     }
1039     uint16 packetType = classify_packet_type(sourceSAP, destSAP);
1040     int32 destAddressType = get_address_type(pkt->fDestAddr);
1041    
1042     // Look which streams want it
1043     DLPIStream *the_stream, *found_stream = NULL;
1044     uint16 found_packetType = 0;
1045     int32 found_destAddressType = 0;
1046     for (the_stream = dlpi_stream_list; the_stream != NULL; the_stream = mi_next_ptr(the_stream)) {
1047    
1048     // Don't send to unbound streams
1049     if (the_stream->dlpi_state == DL_UNBOUND)
1050     continue;
1051    
1052     // Does this stream want all 802.2 packets?
1053     if ((the_stream->flags & kAcceptAll8022Packets) && (destSAP <= 0xff))
1054     goto type_found;
1055    
1056     // No, check SAP/SNAP
1057     if (destSAP == the_stream->dlsap) {
1058     if (the_stream->flags & kSnapStream) {
1059     // Check SNAPs if necessary
1060     uint8 sum = fullpkt->f8022Part.fSNAP[0] ^ the_stream->snap[0];
1061     sum |= fullpkt->f8022Part.fSNAP[1] ^ the_stream->snap[1];
1062     sum |= fullpkt->f8022Part.fSNAP[2] ^ the_stream->snap[2];
1063     sum |= fullpkt->f8022Part.fSNAP[3] ^ the_stream->snap[3];
1064     sum |= fullpkt->f8022Part.fSNAP[4] ^ the_stream->snap[4];
1065     if (sum == 0)
1066     goto type_found;
1067     } else {
1068     // No SNAP, found a match since saps match
1069     goto type_found;
1070     }
1071     } else {
1072     // Check for an 802.3 Group/Global (odd)
1073     if (((packetType == kPkt8022SAP) || (packetType == kPkt8022SNAP)) && (destSAP & 1) && the_stream->TestGroupSAP(destSAP))
1074     goto type_found;
1075     }
1076    
1077     // No stream for this SAP/SNAP found
1078     continue;
1079    
1080     type_found:
1081     // If it's a multicast packet, it must be in the stream's multicast list
1082     if ((destAddressType == keaMulticast) && (the_stream->flags & kAcceptMulticasts) && (!the_stream->IsMulticastRegistered(pkt->fDestAddr)))
1083     continue;
1084    
1085     // Send packet to stream
1086     // found_stream keeps a pointer to the previously found stream, so that only the last
1087     // stream gets the original message, the other ones get duplicates
1088     if (found_stream)
1089     handle_received_packet(found_stream, dupmsg(mp), found_packetType, found_destAddressType);
1090     found_stream = the_stream;
1091     found_packetType = packetType;
1092     found_destAddressType = destAddressType;
1093     }
1094    
1095     // Send original message to last found stream
1096     if (found_stream)
1097     handle_received_packet(found_stream, mp, found_packetType, found_destAddressType);
1098     else {
1099     freemsg(mp); // Nobody wants it *snief*
1100     num_rx_dropped++;
1101     }
1102     }
1103    
1104 gbeauche 1.8 void ether_dispatch_packet(uint32 p, uint32 size)
1105     {
1106     // Wrap packet in message block
1107     num_rx_packets++;
1108     mblk_t *mp;
1109     if ((mp = allocb(size, 0)) != NULL) {
1110     D(bug(" packet data at %p\n", (void *)mp->b_rptr));
1111     Mac2Host_memcpy(mp->b_rptr, p, size);
1112     mp->b_wptr += size;
1113     ether_packet_received(mp);
1114     } else {
1115     D(bug("WARNING: Cannot allocate mblk for received packet\n"));
1116     num_rx_no_mem++;
1117     }
1118     }
1119    
1120 cebix 1.1
1121     /*
1122     * Build and send an error acknowledge
1123     */
1124    
1125     static void DLPI_error_ack(DLPIStream *the_stream, queue_t *q, mblk_t *ack_mp, uint32 prim, uint32 err, uint32 uerr)
1126     {
1127     D(bug(" DLPI_error_ack(%p,%p) prim %d, err %d, uerr %d\n", the_stream, ack_mp, prim, err, uerr));
1128     num_error_acks++;
1129    
1130     if (ack_mp != NULL)
1131     freemsg(ack_mp);
1132     if ((ack_mp = allocb(sizeof(dl_error_ack_t), BPRI_HI)) == NULL)
1133     return;
1134    
1135     ack_mp->b_datap->db_type = M_PCPROTO;
1136 gbeauche 1.3 dl_error_ack_t *errp = (dl_error_ack_t *)(void *)ack_mp->b_wptr;
1137 cebix 1.1 errp->dl_primitive = DL_ERROR_ACK;
1138     errp->dl_error_primitive = prim;
1139     errp->dl_errno = err;
1140     errp->dl_unix_errno = uerr;
1141     ack_mp->b_wptr += sizeof(dl_error_ack_t);
1142     qreply(q, ack_mp);
1143     }
1144    
1145    
1146     /*
1147     * Build and send an OK acknowledge
1148     */
1149    
1150     static void DLPI_ok_ack(DLPIStream *the_stream, queue_t *q, mblk_t *ack_mp, uint32 prim)
1151     {
1152     if (ack_mp->b_datap->db_ref != 1) {
1153     // Message already in use, create a new one
1154     freemsg(ack_mp);
1155     if ((ack_mp = allocb(sizeof(dl_error_ack_t), BPRI_HI)) == NULL)
1156     return;
1157     } else {
1158     // Message free
1159     if (ack_mp->b_cont != NULL) {
1160     freemsg(ack_mp->b_cont);
1161     ack_mp->b_cont = NULL;
1162     }
1163     }
1164    
1165     ack_mp->b_datap->db_type = M_PCPROTO;
1166 gbeauche 1.3 dl_ok_ack_t *ackp = (dl_ok_ack_t *)(void *)ack_mp->b_rptr;
1167 cebix 1.1 ackp->dl_primitive = DL_OK_ACK;
1168     ackp->dl_correct_primitive = prim;
1169     ack_mp->b_wptr = ack_mp->b_rptr + sizeof(dl_ok_ack_t);
1170     qreply(q, ack_mp);
1171     }
1172    
1173    
1174     /*
1175     * Handle DL_INFO_REQ (report general information)
1176     */
1177    
1178     static void DLPI_info(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1179     {
1180     D(bug(" DLPI_info(%p)\n", the_stream));
1181     uint32 saplen = 0;
1182     uint32 addrlen = kEnetPhysicalAddressLength;
1183     uint32 bcastlen = kEnetPhysicalAddressLength;
1184     uint32 hdrlen = kEnetPacketHeaderLength;
1185    
1186     // Calculate header length
1187     if (the_stream->dlpi_state != DL_UNBOUND) {
1188     saplen = (the_stream->flags & kSnapStream) ? k8022DLSAPLength+k8022SNAPLength : k8022DLSAPLength;
1189     if (the_stream->dlsap == kSNAPSAP)
1190     hdrlen = kEnetPacketHeaderLength + k8022SNAPHeaderLength; // SNAP address
1191     else if ((the_stream->dlsap <= kMax8022SAP) || (the_stream->dlsap == kIPXSAP))
1192     hdrlen = kEnetPacketHeaderLength + k8022BasicHeaderLength; // SAP or IPX
1193     else
1194     hdrlen = kEnetPacketHeaderLength; // Basic Ethernet
1195     }
1196    
1197     // Allocate message block for reply
1198     mblk_t *ack_mp;
1199     if ((ack_mp = allocb(sizeof(dl_info_ack_t) + addrlen + saplen + bcastlen, BPRI_LO)) == NULL) {
1200     DLPI_error_ack(the_stream, q, mp, DL_INFO_REQ, DL_SYSERR, MAC_ENOMEM);
1201     return;
1202     }
1203    
1204     // Set up message type
1205     ack_mp->b_datap->db_type = M_PCPROTO;
1206 gbeauche 1.3 dl_info_ack_t *ackp = (dl_info_ack_t *)(void *)ack_mp->b_rptr;
1207 cebix 1.1 ackp->dl_primitive = DL_INFO_ACK;
1208    
1209     // Info/version fields
1210     ackp->dl_service_mode = DL_CLDLS;
1211     ackp->dl_provider_style = DL_STYLE1;
1212     ackp->dl_version = DL_VERSION_2;
1213     ackp->dl_current_state = the_stream->dlpi_state;
1214     ackp->dl_mac_type = the_stream->framing_8022 ? DL_CSMACD : DL_ETHER;
1215     ackp->dl_reserved = 0;
1216     ackp->dl_qos_length = 0;
1217     ackp->dl_qos_offset = (uint32)DL_UNKNOWN;
1218     ackp->dl_qos_range_length = 0;
1219     ackp->dl_qos_range_offset = (uint32)DL_UNKNOWN;
1220     ackp->dl_growth = 0;
1221     ackp->dl_min_sdu = 1;
1222     ackp->dl_max_sdu = kEnetTSDU - hdrlen;
1223    
1224     // Address fields
1225     ackp->dl_sap_length = -saplen; // Negative to indicate sap follows physical address
1226     ackp->dl_addr_length = addrlen + saplen;
1227     ackp->dl_addr_offset = sizeof(dl_info_ack_t);
1228     T8022AddressStruct *boundAddr = ((T8022AddressStruct *)(ack_mp->b_rptr + ackp->dl_addr_offset));
1229     OTCopy48BitAddress(hardware_address, boundAddr->fHWAddr);
1230     if (saplen) {
1231     boundAddr->fSAP = the_stream->dlsap;
1232     if (the_stream->flags & kSnapStream)
1233     OTCopy8022SNAP(the_stream->snap, boundAddr->fSNAP);
1234     }
1235     ackp->dl_brdcst_addr_length = bcastlen;
1236     ackp->dl_brdcst_addr_offset = sizeof(dl_info_ack_t) + addrlen + saplen;
1237     OTSet48BitBroadcastAddress(ack_mp->b_rptr + ackp->dl_brdcst_addr_offset);
1238    
1239     // Advance write pointer
1240     ack_mp->b_wptr += sizeof(dl_info_ack_t) + addrlen + saplen + bcastlen;
1241    
1242     // Free request
1243     freemsg(mp);
1244    
1245     // Send reply
1246     qreply(q, ack_mp);
1247     return;
1248     }
1249    
1250    
1251     /*
1252     * Handle DL_PHYS_ADDR_REQ (report physical address)
1253     */
1254    
1255     static void DLPI_phys_addr(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1256     {
1257     D(bug(" DLPI_phys_addr(%p,%p)\n", the_stream, mp));
1258 gbeauche 1.3 dl_phys_addr_req_t *req = (dl_phys_addr_req_t *)(void *)mp->b_rptr;
1259 cebix 1.1
1260     // Allocate message block for reply
1261     mblk_t *ack_mp;
1262     if ((ack_mp = allocb(sizeof(dl_phys_addr_ack_t) + kEnetPhysicalAddressLength, BPRI_HI)) == NULL) {
1263     DLPI_error_ack(the_stream, q, mp, DL_PHYS_ADDR_REQ, DL_SYSERR, MAC_ENOMEM);
1264     return;
1265     }
1266    
1267     // Set up message type
1268     ack_mp->b_datap->db_type = M_PCPROTO;
1269 gbeauche 1.3 dl_phys_addr_ack_t *ackp = (dl_phys_addr_ack_t *)(void *)ack_mp->b_wptr;
1270 cebix 1.1 ackp->dl_primitive = DL_PHYS_ADDR_ACK;
1271    
1272     // Fill in address
1273     ackp->dl_addr_length = kEnetPhysicalAddressLength;
1274     ackp->dl_addr_offset = sizeof(dl_phys_addr_ack_t);
1275     ack_mp->b_wptr += sizeof(dl_phys_addr_ack_t) + kEnetPhysicalAddressLength;
1276     if (req->dl_addr_type == DL_CURR_PHYS_ADDR || req->dl_addr_type == DL_FACT_PHYS_ADDR)
1277     OTCopy48BitAddress(hardware_address, ack_mp->b_rptr + ackp->dl_addr_offset);
1278     else {
1279     DLPI_error_ack(the_stream, q, mp, DL_PHYS_ADDR_REQ, DL_BADPRIM, 0);
1280     return;
1281     }
1282    
1283     // Free request
1284     freemsg(mp);
1285    
1286     // Send reply
1287     qreply(q, ack_mp);
1288     return;
1289     }
1290    
1291    
1292     /*
1293     * Handle DL_BIND_REQ (bind a stream)
1294     */
1295    
1296     static void DLPI_bind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1297     {
1298 gbeauche 1.3 dl_bind_req_t *req = (dl_bind_req_t *)(void *)mp->b_rptr;
1299 cebix 1.1 uint32 sap = req->dl_sap;
1300     D(bug(" DLPI_bind(%p,%p) SAP %04x\n", the_stream, mp, sap));
1301    
1302     // Stream must be unbound
1303     if (the_stream->dlpi_state != DL_UNBOUND) {
1304     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_OUTSTATE, 0);
1305     return;
1306     }
1307    
1308     // We only support connectionless data link services
1309     if (req->dl_service_mode != DL_CLDLS || req->dl_max_conind != 0) {
1310     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_UNSUPPORTED, 0);
1311     return;
1312     }
1313    
1314     // Don't bind to 802.2 group saps, can't check 802.2 global sap (0xFF)
1315     // because it looks like IPX
1316     if ((sap <= kMax8022SAP) && (sap & 1)) {
1317     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_BADADDR, 0);
1318     return;
1319     }
1320    
1321     if (classify_packet_type(sap, sap) == kPktUnknown) {
1322     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_BADADDR, 0);
1323     return;
1324     }
1325    
1326     // Allocate message block for reply
1327     mblk_t *ack_mp;
1328     if ((ack_mp = allocb(sizeof(dl_bind_ack_t) + kEnetAndSAPAddressLength, BPRI_HI)) == NULL) {
1329     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_SYSERR, MAC_ENOMEM);
1330     return;
1331     }
1332    
1333     // Set up message type
1334     ack_mp->b_datap->db_type = M_PCPROTO;
1335 gbeauche 1.3 dl_bind_ack_t *ackp = (dl_bind_ack_t *)(void *)ack_mp->b_rptr;
1336 cebix 1.1 ackp->dl_primitive = DL_BIND_ACK;
1337    
1338     // Fill in other fields
1339     ackp->dl_sap = sap;
1340     ackp->dl_addr_length = kEnetAndSAPAddressLength;
1341     ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1342     ackp->dl_max_conind = 0;
1343     ackp->dl_xidtest_flg = 0;
1344    
1345     T8022AddressStruct *addrInfo = (T8022AddressStruct *)(ack_mp->b_rptr + sizeof(dl_bind_ack_t));
1346     OTCopy48BitAddress(hardware_address, addrInfo->fHWAddr);
1347     addrInfo->fSAP = sap;
1348    
1349     // Must move b_wptr past the address info data
1350     ack_mp->b_wptr = ack_mp->b_rptr + sizeof(dl_bind_ack_t) + kEnetAndSAPAddressLength;
1351    
1352     // Set group SAP if necessary
1353     the_stream->ClearAllGroupSAPs();
1354     if (sap <= kMax8022SAP)
1355     the_stream->SetGroupSAP(k8022GlobalSAP);
1356    
1357     // The stream is now bound and idle
1358     the_stream->dlpi_state = DL_IDLE;
1359     the_stream->dlsap = sap;
1360     the_stream->flags &= ~kSnapStream;
1361    
1362     // Free request
1363     freemsg(mp);
1364    
1365     // Send reply
1366     qreply(q, ack_mp);
1367     return;
1368     }
1369    
1370    
1371     /*
1372     * Handle DL_UNBIND_REQ (unbind a stream)
1373     */
1374    
1375     static void DLPI_unbind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1376     {
1377     D(bug(" DLPI_unbind(%p,%p)\n", the_stream, mp));
1378    
1379     // Stream must be bound and idle
1380     if (the_stream->dlpi_state != DL_IDLE) {
1381     DLPI_error_ack(the_stream, q, mp, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1382     return;
1383     }
1384    
1385     // Stream is now unbound
1386     the_stream->dlpi_state = DL_UNBOUND;
1387     the_stream->dlsap = 0;
1388    
1389     // Flush all pending outbound messages
1390     flushq(q, FLUSHDATA);
1391    
1392     // Flush all inbound messages pending on the stream
1393     flushq(RD(q), FLUSHDATA);
1394     putnextctl1(RD(q), M_FLUSH, FLUSHRW);
1395    
1396     // Send reply
1397     DLPI_ok_ack(the_stream, q, mp, DL_UNBIND_REQ);
1398     return;
1399     }
1400    
1401    
1402     /*
1403     * Handle DL_SUBS_BIND_REQ (register 802.2 SAP group addresses and SNAPs)
1404     */
1405    
1406     static void DLPI_subs_bind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1407     {
1408 gbeauche 1.3 dl_subs_bind_req_t *req = (dl_subs_bind_req_t *)(void *)mp->b_rptr;
1409 cebix 1.1 uint8 *sap = ((uint8 *)req) + req->dl_subs_sap_offset;
1410     int32 length = req->dl_subs_sap_length;
1411 gbeauche 1.3 uint16 theSap = ntohs(*((uint16 *)sap));
1412 cebix 1.1 int32 error = 0;
1413     D(bug(" DLPI_subs_bind(%p,%p) SAP %02x%02x%02x%02x%02x\n", the_stream, mp, sap[0], sap[1], sap[2], sap[3], sap[4]));
1414    
1415     // Stream must be idle
1416     if (the_stream->dlpi_state != DL_IDLE) {
1417     DLPI_error_ack(the_stream, q, mp, DL_SUBS_BIND_REQ, DL_OUTSTATE, 0);
1418     return;
1419     }
1420    
1421     // Check if address is valid
1422     switch (req->dl_subs_bind_class) {
1423     case DL_PEER_BIND: // Bind a group address
1424     if (the_stream->dlsap <= kMax8022SAP) {
1425     if ((theSap & 1) && (length == sizeof(theSap)))
1426     the_stream->SetGroupSAP(theSap);
1427     else
1428     if (theSap == 0x0000) // special case to receive all 802.2 packets
1429     the_stream->flags |= kAcceptAll8022Packets;
1430     else
1431     error = DL_BADADDR;
1432     } else
1433     error = DL_UNSUPPORTED;
1434     break;
1435    
1436     case DL_HIERARCHICAL_BIND: // Bind an additional SNAP
1437     if (the_stream->dlsap == kSNAPSAP) {
1438     if (the_stream->flags & kSnapStream)
1439     error = DL_TOOMANY; // only one SNAP binding allowed
1440     else {
1441     OTCopy8022SNAP(sap, the_stream->snap);
1442     the_stream->flags |= kSnapStream;
1443     }
1444     } else
1445     error = DL_BADADDR;
1446     break;
1447    
1448     default:
1449     error = DL_UNSUPPORTED;
1450     break;
1451     }
1452     if (error) {
1453     DLPI_error_ack(the_stream, q, mp, DL_SUBS_BIND_REQ, error, 0);
1454     return;
1455     }
1456    
1457     // Allocate message block for reply
1458     mblk_t *ack_mp;
1459     if ((ack_mp = allocb(sizeof(dl_subs_bind_ack_t) + length, BPRI_HI)) == NULL) {
1460     DLPI_error_ack(the_stream, q, mp, DL_SUBS_BIND_REQ, DL_SYSERR, MAC_ENOMEM);
1461     return;
1462     }
1463    
1464     // Set up message type
1465     ack_mp->b_datap->db_type = M_PCPROTO;
1466 gbeauche 1.3 dl_subs_bind_ack_t *ackp = (dl_subs_bind_ack_t *)(void *)ack_mp->b_wptr;
1467 cebix 1.1 memset(ackp, 0, sizeof(dl_subs_bind_ack_t) + length);
1468     ackp->dl_primitive = DL_SUBS_BIND_ACK;
1469    
1470     // Fill in other fields
1471     ackp->dl_subs_sap_length = length;
1472     ackp->dl_subs_sap_offset = length ? sizeof(dl_subs_bind_ack_t) : 0;
1473     ack_mp->b_wptr += sizeof(dl_subs_bind_ack_t);
1474     if (length)
1475     memcpy(ack_mp->b_wptr, sap, length);
1476     ack_mp->b_wptr += length;
1477    
1478     // Free request
1479     freemsg(mp);
1480    
1481     // Send reply
1482     qreply(q, ack_mp);
1483     return;
1484     }
1485    
1486    
1487     /*
1488     * Handle DL_SUBS_UNBIND_REQ (unregister 802.2 SAP group addresses and snaps)
1489     */
1490    
1491     static void DLPI_subs_unbind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1492     {
1493 gbeauche 1.3 dl_subs_unbind_req_t *req = (dl_subs_unbind_req_t *)(void *)mp->b_rptr;
1494 cebix 1.1 uint8 *sap = ((uint8 *)req) + req->dl_subs_sap_offset;
1495     int32 length = req->dl_subs_sap_length;
1496     int32 error = 0;
1497     D(bug(" DLPI_subs_unbind(%p,%p) SAP %02x%02x%02x%02x%02x\n", the_stream, mp, sap[0], sap[1], sap[2], sap[3], sap[4]));
1498    
1499     // Stream must be idle
1500     if (the_stream->dlpi_state != DL_IDLE) {
1501     DLPI_error_ack(the_stream, q, mp, DL_SUBS_UNBIND_REQ, DL_OUTSTATE, 0);
1502     return;
1503     }
1504    
1505     // Check if we are unbinding from an address we are bound to
1506     if (length == k8022SAPLength) {
1507     if ((*sap & 1) && (*sap != kIPXSAP)) {
1508     if (the_stream->dlsap <= kMax8022SAP)
1509     the_stream->ClearGroupSAP(*sap);
1510     else
1511     error = DL_UNSUPPORTED;
1512     } else
1513     error = DL_BADADDR;
1514     } else if (length == k8022SNAPLength) {
1515     if (the_stream->dlsap == kSNAPSAP) {
1516     if (the_stream->flags & kSnapStream) {
1517     if (memcmp(the_stream->snap, sap, length) != 0)
1518     error = DL_BADADDR;
1519     } else
1520     error = DL_BADADDR;
1521     } else
1522     error = DL_UNSUPPORTED;
1523     }
1524     if (error) {
1525     DLPI_error_ack(the_stream, q, mp, DL_SUBS_UNBIND_REQ, error, 0);
1526     return;
1527     }
1528    
1529     // Stream is no longer bound to SNAP
1530     the_stream->flags &= ~kSnapStream;
1531    
1532     // Send reply
1533     DLPI_ok_ack(the_stream, q, mp, DL_SUBS_UNBIND_REQ);
1534     return;
1535     }
1536    
1537    
1538     /*
1539     * Handles DL_ENABMULTI_REQ (enable multicast address)
1540     */
1541    
1542     static void DLPI_enable_multi(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1543     {
1544 gbeauche 1.3 dl_enabmulti_req_t* req = (dl_enabmulti_req_t*)(void *)mp->b_rptr;
1545 cebix 1.1 uint8 *reqaddr = (uint8 *)(mp->b_rptr + req->dl_addr_offset);
1546     D(bug(" DLPI_enable_multi(%p,%p) addr %02x%02x%02x%02x%02x%02x\n", the_stream, mp, reqaddr[0], reqaddr[1], reqaddr[2], reqaddr[3], reqaddr[4], reqaddr[5]));
1547    
1548     // Address must be a multicast address
1549     if (get_address_type(reqaddr) != keaMulticast) {
1550     DLPI_error_ack(the_stream, q, mp, DL_ENABMULTI_REQ, DL_BADADDR, 0);
1551     return;
1552     }
1553    
1554     // Address already in multicast list?
1555     if (the_stream->IsMulticastRegistered(reqaddr)) {
1556     DLPI_error_ack(the_stream, q, mp, DL_ENABMULTI_REQ, DL_BADADDR, 0);
1557     return;
1558     }
1559    
1560     // Tell add-on to enable multicast address
1561 gbeauche 1.8 AO_enable_multicast(Host2MacAddr((uint8 *)reqaddr));
1562 cebix 1.1
1563     // Add new address to multicast list
1564 gbeauche 1.6 uint8 *addr = Mac2HostAddr(Mac_sysalloc(kEnetPhysicalAddressLength));
1565 cebix 1.1 OTCopy48BitAddress(reqaddr, addr);
1566     the_stream->AddMulticast(addr);
1567    
1568     // On receive now check multicast packets
1569     the_stream->flags |= kAcceptMulticasts;
1570    
1571     // Send reply
1572     DLPI_ok_ack(the_stream, q, mp, DL_ENABMULTI_REQ);
1573     return;
1574     }
1575    
1576    
1577     /*
1578     * Handles DL_DISABMULTI_REQ (disable multicast address)
1579     */
1580    
1581     static void DLPI_disable_multi(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1582     {
1583 gbeauche 1.3 dl_disabmulti_req_t *req = (dl_disabmulti_req_t*)(void *)mp->b_rptr;
1584 cebix 1.1 uint8 *reqaddr = (uint8 *)(mp->b_rptr + req->dl_addr_offset);
1585     D(bug(" DLPI_disable_multi(%p,%p) addr %02x%02x%02x%02x%02x%02x\n", the_stream, mp, reqaddr[0], reqaddr[1], reqaddr[2], reqaddr[3], reqaddr[4], reqaddr[5]));
1586    
1587     // Address must be a multicast address
1588     if (get_address_type(reqaddr) != keaMulticast) {
1589     DLPI_error_ack(the_stream, q, mp, DL_DISABMULTI_REQ, DL_BADADDR, 0);
1590     return;
1591     }
1592    
1593     // Find address in multicast list
1594     uint8 *addr = the_stream->IsMulticastRegistered(reqaddr);
1595     if (addr == NULL) {
1596     DLPI_error_ack(the_stream, q, mp, DL_DISABMULTI_REQ, DL_BADADDR, 0);
1597     return;
1598     }
1599    
1600     // Found, then remove
1601     the_stream->RemoveMulticast(addr);
1602 gbeauche 1.6 Mac_sysfree(Host2MacAddr(addr));
1603 cebix 1.1
1604     // Tell add-on to disable multicast address
1605 gbeauche 1.8 AO_disable_multicast(Host2MacAddr((uint8 *)reqaddr));
1606 cebix 1.1
1607     // No longer check multicast packets if no multicast addresses are registered
1608     if (the_stream->multicast_list == NULL)
1609     the_stream->flags &= ~kAcceptMulticasts;
1610    
1611     // Send reply
1612     DLPI_ok_ack(the_stream, q, mp, DL_DISABMULTI_REQ);
1613     return;
1614     }
1615    
1616    
1617     /*
1618     * Handle DL_UNITDATA_REQ (transmit packet)
1619     */
1620    
1621     static void DLPI_unit_data(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1622     {
1623     D(bug(" DLPI_unit_data(%p,%p)\n", the_stream, mp));
1624 gbeauche 1.3 dl_unitdata_req_t *req = (dl_unitdata_req_t *)(void *)mp->b_rptr;
1625 cebix 1.1
1626     // Stream must be idle
1627     if (the_stream->dlpi_state != DL_IDLE) {
1628    
1629     // Not idle, send error response
1630     dl_uderror_ind_t *errp;
1631     mblk_t *bp;
1632    
1633     int i = sizeof(dl_uderror_ind_t) + req->dl_dest_addr_length;
1634     if ((bp = allocb(i, BPRI_HI)) == NULL) {
1635     freemsg(mp);
1636     return;
1637     }
1638     bp->b_datap->db_type = M_PROTO;
1639 gbeauche 1.3 errp = (dl_uderror_ind_t *)(void *)bp->b_wptr;
1640 cebix 1.1 errp->dl_primitive = DL_UDERROR_IND;
1641     errp->dl_errno = DL_OUTSTATE;
1642     errp->dl_unix_errno = 0;
1643     errp->dl_dest_addr_length = req->dl_dest_addr_length;
1644     errp->dl_dest_addr_offset = sizeof(dl_uderror_ind_t);
1645     bp->b_wptr += sizeof(dl_uderror_ind_t);
1646     memcpy((uint8 *)bp->b_wptr, ((uint8 *)req) + req->dl_dest_addr_offset, req->dl_dest_addr_length);
1647     bp->b_wptr += req->dl_dest_addr_length;
1648     qreply(q, bp);
1649    
1650     freemsg(mp);
1651     return;
1652     }
1653    
1654     // Build packet header and transmit packet
1655     if ((mp = build_tx_packet_header(the_stream, mp, false)) != NULL)
1656     transmit_packet(mp);
1657     }
1658 gbeauche 1.8
1659    
1660     /*
1661     * Ethernet packet allocator
1662     */
1663    
1664     #if SIZEOF_VOID_P != 4 || REAL_ADDRESSING == 0
1665     static uint32 ether_packet = 0; // Ethernet packet (cached allocation)
1666     static uint32 n_ether_packets = 0; // Number of ethernet packets allocated so far (should be at most 1)
1667    
1668     EthernetPacket::EthernetPacket()
1669     {
1670     ++n_ether_packets;
1671     if (ether_packet && n_ether_packets == 1)
1672     packet = ether_packet;
1673     else {
1674     packet = Mac_sysalloc(1516);
1675     assert(packet != 0);
1676     Mac_memset(packet, 0, 1516);
1677     if (ether_packet == 0)
1678     ether_packet = packet;
1679     }
1680     }
1681    
1682     EthernetPacket::~EthernetPacket()
1683     {
1684     --n_ether_packets;
1685     if (packet != ether_packet)
1686     Mac_sysfree(packet);
1687     if (n_ether_packets > 0) {
1688     bug("WARNING: Nested allocation of ethernet packets!\n");
1689     }
1690     }
1691     #endif