ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/SheepShaver/src/ether.cpp
Revision: 1.7
Committed: 2005-01-30T21:48:19Z (19 years, 10 months ago) by gbeauche
Branch: MAIN
Changes since 1.6: +1 -1 lines
Log Message:
Happy New Year 2005!

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     #include "ether.h"
31     #include "ether_defs.h"
32     #include "macos_util.h"
33     #include "cpu_emulation.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     AO_get_ethernet_address(hardware_address);
402    
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     AO_disable_multicast(the_stream->multicast_list->addr);
483     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     AO_transmit_packet(mp);
933     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    
1105     /*
1106     * Build and send an error acknowledge
1107     */
1108    
1109     static void DLPI_error_ack(DLPIStream *the_stream, queue_t *q, mblk_t *ack_mp, uint32 prim, uint32 err, uint32 uerr)
1110     {
1111     D(bug(" DLPI_error_ack(%p,%p) prim %d, err %d, uerr %d\n", the_stream, ack_mp, prim, err, uerr));
1112     num_error_acks++;
1113    
1114     if (ack_mp != NULL)
1115     freemsg(ack_mp);
1116     if ((ack_mp = allocb(sizeof(dl_error_ack_t), BPRI_HI)) == NULL)
1117     return;
1118    
1119     ack_mp->b_datap->db_type = M_PCPROTO;
1120 gbeauche 1.3 dl_error_ack_t *errp = (dl_error_ack_t *)(void *)ack_mp->b_wptr;
1121 cebix 1.1 errp->dl_primitive = DL_ERROR_ACK;
1122     errp->dl_error_primitive = prim;
1123     errp->dl_errno = err;
1124     errp->dl_unix_errno = uerr;
1125     ack_mp->b_wptr += sizeof(dl_error_ack_t);
1126     qreply(q, ack_mp);
1127     }
1128    
1129    
1130     /*
1131     * Build and send an OK acknowledge
1132     */
1133    
1134     static void DLPI_ok_ack(DLPIStream *the_stream, queue_t *q, mblk_t *ack_mp, uint32 prim)
1135     {
1136     if (ack_mp->b_datap->db_ref != 1) {
1137     // Message already in use, create a new one
1138     freemsg(ack_mp);
1139     if ((ack_mp = allocb(sizeof(dl_error_ack_t), BPRI_HI)) == NULL)
1140     return;
1141     } else {
1142     // Message free
1143     if (ack_mp->b_cont != NULL) {
1144     freemsg(ack_mp->b_cont);
1145     ack_mp->b_cont = NULL;
1146     }
1147     }
1148    
1149     ack_mp->b_datap->db_type = M_PCPROTO;
1150 gbeauche 1.3 dl_ok_ack_t *ackp = (dl_ok_ack_t *)(void *)ack_mp->b_rptr;
1151 cebix 1.1 ackp->dl_primitive = DL_OK_ACK;
1152     ackp->dl_correct_primitive = prim;
1153     ack_mp->b_wptr = ack_mp->b_rptr + sizeof(dl_ok_ack_t);
1154     qreply(q, ack_mp);
1155     }
1156    
1157    
1158     /*
1159     * Handle DL_INFO_REQ (report general information)
1160     */
1161    
1162     static void DLPI_info(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1163     {
1164     D(bug(" DLPI_info(%p)\n", the_stream));
1165     uint32 saplen = 0;
1166     uint32 addrlen = kEnetPhysicalAddressLength;
1167     uint32 bcastlen = kEnetPhysicalAddressLength;
1168     uint32 hdrlen = kEnetPacketHeaderLength;
1169    
1170     // Calculate header length
1171     if (the_stream->dlpi_state != DL_UNBOUND) {
1172     saplen = (the_stream->flags & kSnapStream) ? k8022DLSAPLength+k8022SNAPLength : k8022DLSAPLength;
1173     if (the_stream->dlsap == kSNAPSAP)
1174     hdrlen = kEnetPacketHeaderLength + k8022SNAPHeaderLength; // SNAP address
1175     else if ((the_stream->dlsap <= kMax8022SAP) || (the_stream->dlsap == kIPXSAP))
1176     hdrlen = kEnetPacketHeaderLength + k8022BasicHeaderLength; // SAP or IPX
1177     else
1178     hdrlen = kEnetPacketHeaderLength; // Basic Ethernet
1179     }
1180    
1181     // Allocate message block for reply
1182     mblk_t *ack_mp;
1183     if ((ack_mp = allocb(sizeof(dl_info_ack_t) + addrlen + saplen + bcastlen, BPRI_LO)) == NULL) {
1184     DLPI_error_ack(the_stream, q, mp, DL_INFO_REQ, DL_SYSERR, MAC_ENOMEM);
1185     return;
1186     }
1187    
1188     // Set up message type
1189     ack_mp->b_datap->db_type = M_PCPROTO;
1190 gbeauche 1.3 dl_info_ack_t *ackp = (dl_info_ack_t *)(void *)ack_mp->b_rptr;
1191 cebix 1.1 ackp->dl_primitive = DL_INFO_ACK;
1192    
1193     // Info/version fields
1194     ackp->dl_service_mode = DL_CLDLS;
1195     ackp->dl_provider_style = DL_STYLE1;
1196     ackp->dl_version = DL_VERSION_2;
1197     ackp->dl_current_state = the_stream->dlpi_state;
1198     ackp->dl_mac_type = the_stream->framing_8022 ? DL_CSMACD : DL_ETHER;
1199     ackp->dl_reserved = 0;
1200     ackp->dl_qos_length = 0;
1201     ackp->dl_qos_offset = (uint32)DL_UNKNOWN;
1202     ackp->dl_qos_range_length = 0;
1203     ackp->dl_qos_range_offset = (uint32)DL_UNKNOWN;
1204     ackp->dl_growth = 0;
1205     ackp->dl_min_sdu = 1;
1206     ackp->dl_max_sdu = kEnetTSDU - hdrlen;
1207    
1208     // Address fields
1209     ackp->dl_sap_length = -saplen; // Negative to indicate sap follows physical address
1210     ackp->dl_addr_length = addrlen + saplen;
1211     ackp->dl_addr_offset = sizeof(dl_info_ack_t);
1212     T8022AddressStruct *boundAddr = ((T8022AddressStruct *)(ack_mp->b_rptr + ackp->dl_addr_offset));
1213     OTCopy48BitAddress(hardware_address, boundAddr->fHWAddr);
1214     if (saplen) {
1215     boundAddr->fSAP = the_stream->dlsap;
1216     if (the_stream->flags & kSnapStream)
1217     OTCopy8022SNAP(the_stream->snap, boundAddr->fSNAP);
1218     }
1219     ackp->dl_brdcst_addr_length = bcastlen;
1220     ackp->dl_brdcst_addr_offset = sizeof(dl_info_ack_t) + addrlen + saplen;
1221     OTSet48BitBroadcastAddress(ack_mp->b_rptr + ackp->dl_brdcst_addr_offset);
1222    
1223     // Advance write pointer
1224     ack_mp->b_wptr += sizeof(dl_info_ack_t) + addrlen + saplen + bcastlen;
1225    
1226     // Free request
1227     freemsg(mp);
1228    
1229     // Send reply
1230     qreply(q, ack_mp);
1231     return;
1232     }
1233    
1234    
1235     /*
1236     * Handle DL_PHYS_ADDR_REQ (report physical address)
1237     */
1238    
1239     static void DLPI_phys_addr(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1240     {
1241     D(bug(" DLPI_phys_addr(%p,%p)\n", the_stream, mp));
1242 gbeauche 1.3 dl_phys_addr_req_t *req = (dl_phys_addr_req_t *)(void *)mp->b_rptr;
1243 cebix 1.1
1244     // Allocate message block for reply
1245     mblk_t *ack_mp;
1246     if ((ack_mp = allocb(sizeof(dl_phys_addr_ack_t) + kEnetPhysicalAddressLength, BPRI_HI)) == NULL) {
1247     DLPI_error_ack(the_stream, q, mp, DL_PHYS_ADDR_REQ, DL_SYSERR, MAC_ENOMEM);
1248     return;
1249     }
1250    
1251     // Set up message type
1252     ack_mp->b_datap->db_type = M_PCPROTO;
1253 gbeauche 1.3 dl_phys_addr_ack_t *ackp = (dl_phys_addr_ack_t *)(void *)ack_mp->b_wptr;
1254 cebix 1.1 ackp->dl_primitive = DL_PHYS_ADDR_ACK;
1255    
1256     // Fill in address
1257     ackp->dl_addr_length = kEnetPhysicalAddressLength;
1258     ackp->dl_addr_offset = sizeof(dl_phys_addr_ack_t);
1259     ack_mp->b_wptr += sizeof(dl_phys_addr_ack_t) + kEnetPhysicalAddressLength;
1260     if (req->dl_addr_type == DL_CURR_PHYS_ADDR || req->dl_addr_type == DL_FACT_PHYS_ADDR)
1261     OTCopy48BitAddress(hardware_address, ack_mp->b_rptr + ackp->dl_addr_offset);
1262     else {
1263     DLPI_error_ack(the_stream, q, mp, DL_PHYS_ADDR_REQ, DL_BADPRIM, 0);
1264     return;
1265     }
1266    
1267     // Free request
1268     freemsg(mp);
1269    
1270     // Send reply
1271     qreply(q, ack_mp);
1272     return;
1273     }
1274    
1275    
1276     /*
1277     * Handle DL_BIND_REQ (bind a stream)
1278     */
1279    
1280     static void DLPI_bind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1281     {
1282 gbeauche 1.3 dl_bind_req_t *req = (dl_bind_req_t *)(void *)mp->b_rptr;
1283 cebix 1.1 uint32 sap = req->dl_sap;
1284     D(bug(" DLPI_bind(%p,%p) SAP %04x\n", the_stream, mp, sap));
1285    
1286     // Stream must be unbound
1287     if (the_stream->dlpi_state != DL_UNBOUND) {
1288     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_OUTSTATE, 0);
1289     return;
1290     }
1291    
1292     // We only support connectionless data link services
1293     if (req->dl_service_mode != DL_CLDLS || req->dl_max_conind != 0) {
1294     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_UNSUPPORTED, 0);
1295     return;
1296     }
1297    
1298     // Don't bind to 802.2 group saps, can't check 802.2 global sap (0xFF)
1299     // because it looks like IPX
1300     if ((sap <= kMax8022SAP) && (sap & 1)) {
1301     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_BADADDR, 0);
1302     return;
1303     }
1304    
1305     if (classify_packet_type(sap, sap) == kPktUnknown) {
1306     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_BADADDR, 0);
1307     return;
1308     }
1309    
1310     // Allocate message block for reply
1311     mblk_t *ack_mp;
1312     if ((ack_mp = allocb(sizeof(dl_bind_ack_t) + kEnetAndSAPAddressLength, BPRI_HI)) == NULL) {
1313     DLPI_error_ack(the_stream, q, mp, DL_BIND_REQ, DL_SYSERR, MAC_ENOMEM);
1314     return;
1315     }
1316    
1317     // Set up message type
1318     ack_mp->b_datap->db_type = M_PCPROTO;
1319 gbeauche 1.3 dl_bind_ack_t *ackp = (dl_bind_ack_t *)(void *)ack_mp->b_rptr;
1320 cebix 1.1 ackp->dl_primitive = DL_BIND_ACK;
1321    
1322     // Fill in other fields
1323     ackp->dl_sap = sap;
1324     ackp->dl_addr_length = kEnetAndSAPAddressLength;
1325     ackp->dl_addr_offset = sizeof(dl_bind_ack_t);
1326     ackp->dl_max_conind = 0;
1327     ackp->dl_xidtest_flg = 0;
1328    
1329     T8022AddressStruct *addrInfo = (T8022AddressStruct *)(ack_mp->b_rptr + sizeof(dl_bind_ack_t));
1330     OTCopy48BitAddress(hardware_address, addrInfo->fHWAddr);
1331     addrInfo->fSAP = sap;
1332    
1333     // Must move b_wptr past the address info data
1334     ack_mp->b_wptr = ack_mp->b_rptr + sizeof(dl_bind_ack_t) + kEnetAndSAPAddressLength;
1335    
1336     // Set group SAP if necessary
1337     the_stream->ClearAllGroupSAPs();
1338     if (sap <= kMax8022SAP)
1339     the_stream->SetGroupSAP(k8022GlobalSAP);
1340    
1341     // The stream is now bound and idle
1342     the_stream->dlpi_state = DL_IDLE;
1343     the_stream->dlsap = sap;
1344     the_stream->flags &= ~kSnapStream;
1345    
1346     // Free request
1347     freemsg(mp);
1348    
1349     // Send reply
1350     qreply(q, ack_mp);
1351     return;
1352     }
1353    
1354    
1355     /*
1356     * Handle DL_UNBIND_REQ (unbind a stream)
1357     */
1358    
1359     static void DLPI_unbind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1360     {
1361     D(bug(" DLPI_unbind(%p,%p)\n", the_stream, mp));
1362    
1363     // Stream must be bound and idle
1364     if (the_stream->dlpi_state != DL_IDLE) {
1365     DLPI_error_ack(the_stream, q, mp, DL_UNBIND_REQ, DL_OUTSTATE, 0);
1366     return;
1367     }
1368    
1369     // Stream is now unbound
1370     the_stream->dlpi_state = DL_UNBOUND;
1371     the_stream->dlsap = 0;
1372    
1373     // Flush all pending outbound messages
1374     flushq(q, FLUSHDATA);
1375    
1376     // Flush all inbound messages pending on the stream
1377     flushq(RD(q), FLUSHDATA);
1378     putnextctl1(RD(q), M_FLUSH, FLUSHRW);
1379    
1380     // Send reply
1381     DLPI_ok_ack(the_stream, q, mp, DL_UNBIND_REQ);
1382     return;
1383     }
1384    
1385    
1386     /*
1387     * Handle DL_SUBS_BIND_REQ (register 802.2 SAP group addresses and SNAPs)
1388     */
1389    
1390     static void DLPI_subs_bind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1391     {
1392 gbeauche 1.3 dl_subs_bind_req_t *req = (dl_subs_bind_req_t *)(void *)mp->b_rptr;
1393 cebix 1.1 uint8 *sap = ((uint8 *)req) + req->dl_subs_sap_offset;
1394     int32 length = req->dl_subs_sap_length;
1395 gbeauche 1.3 uint16 theSap = ntohs(*((uint16 *)sap));
1396 cebix 1.1 int32 error = 0;
1397     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]));
1398    
1399     // Stream must be idle
1400     if (the_stream->dlpi_state != DL_IDLE) {
1401     DLPI_error_ack(the_stream, q, mp, DL_SUBS_BIND_REQ, DL_OUTSTATE, 0);
1402     return;
1403     }
1404    
1405     // Check if address is valid
1406     switch (req->dl_subs_bind_class) {
1407     case DL_PEER_BIND: // Bind a group address
1408     if (the_stream->dlsap <= kMax8022SAP) {
1409     if ((theSap & 1) && (length == sizeof(theSap)))
1410     the_stream->SetGroupSAP(theSap);
1411     else
1412     if (theSap == 0x0000) // special case to receive all 802.2 packets
1413     the_stream->flags |= kAcceptAll8022Packets;
1414     else
1415     error = DL_BADADDR;
1416     } else
1417     error = DL_UNSUPPORTED;
1418     break;
1419    
1420     case DL_HIERARCHICAL_BIND: // Bind an additional SNAP
1421     if (the_stream->dlsap == kSNAPSAP) {
1422     if (the_stream->flags & kSnapStream)
1423     error = DL_TOOMANY; // only one SNAP binding allowed
1424     else {
1425     OTCopy8022SNAP(sap, the_stream->snap);
1426     the_stream->flags |= kSnapStream;
1427     }
1428     } else
1429     error = DL_BADADDR;
1430     break;
1431    
1432     default:
1433     error = DL_UNSUPPORTED;
1434     break;
1435     }
1436     if (error) {
1437     DLPI_error_ack(the_stream, q, mp, DL_SUBS_BIND_REQ, error, 0);
1438     return;
1439     }
1440    
1441     // Allocate message block for reply
1442     mblk_t *ack_mp;
1443     if ((ack_mp = allocb(sizeof(dl_subs_bind_ack_t) + length, BPRI_HI)) == NULL) {
1444     DLPI_error_ack(the_stream, q, mp, DL_SUBS_BIND_REQ, DL_SYSERR, MAC_ENOMEM);
1445     return;
1446     }
1447    
1448     // Set up message type
1449     ack_mp->b_datap->db_type = M_PCPROTO;
1450 gbeauche 1.3 dl_subs_bind_ack_t *ackp = (dl_subs_bind_ack_t *)(void *)ack_mp->b_wptr;
1451 cebix 1.1 memset(ackp, 0, sizeof(dl_subs_bind_ack_t) + length);
1452     ackp->dl_primitive = DL_SUBS_BIND_ACK;
1453    
1454     // Fill in other fields
1455     ackp->dl_subs_sap_length = length;
1456     ackp->dl_subs_sap_offset = length ? sizeof(dl_subs_bind_ack_t) : 0;
1457     ack_mp->b_wptr += sizeof(dl_subs_bind_ack_t);
1458     if (length)
1459     memcpy(ack_mp->b_wptr, sap, length);
1460     ack_mp->b_wptr += length;
1461    
1462     // Free request
1463     freemsg(mp);
1464    
1465     // Send reply
1466     qreply(q, ack_mp);
1467     return;
1468     }
1469    
1470    
1471     /*
1472     * Handle DL_SUBS_UNBIND_REQ (unregister 802.2 SAP group addresses and snaps)
1473     */
1474    
1475     static void DLPI_subs_unbind(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1476     {
1477 gbeauche 1.3 dl_subs_unbind_req_t *req = (dl_subs_unbind_req_t *)(void *)mp->b_rptr;
1478 cebix 1.1 uint8 *sap = ((uint8 *)req) + req->dl_subs_sap_offset;
1479     int32 length = req->dl_subs_sap_length;
1480     int32 error = 0;
1481     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]));
1482    
1483     // Stream must be idle
1484     if (the_stream->dlpi_state != DL_IDLE) {
1485     DLPI_error_ack(the_stream, q, mp, DL_SUBS_UNBIND_REQ, DL_OUTSTATE, 0);
1486     return;
1487     }
1488    
1489     // Check if we are unbinding from an address we are bound to
1490     if (length == k8022SAPLength) {
1491     if ((*sap & 1) && (*sap != kIPXSAP)) {
1492     if (the_stream->dlsap <= kMax8022SAP)
1493     the_stream->ClearGroupSAP(*sap);
1494     else
1495     error = DL_UNSUPPORTED;
1496     } else
1497     error = DL_BADADDR;
1498     } else if (length == k8022SNAPLength) {
1499     if (the_stream->dlsap == kSNAPSAP) {
1500     if (the_stream->flags & kSnapStream) {
1501     if (memcmp(the_stream->snap, sap, length) != 0)
1502     error = DL_BADADDR;
1503     } else
1504     error = DL_BADADDR;
1505     } else
1506     error = DL_UNSUPPORTED;
1507     }
1508     if (error) {
1509     DLPI_error_ack(the_stream, q, mp, DL_SUBS_UNBIND_REQ, error, 0);
1510     return;
1511     }
1512    
1513     // Stream is no longer bound to SNAP
1514     the_stream->flags &= ~kSnapStream;
1515    
1516     // Send reply
1517     DLPI_ok_ack(the_stream, q, mp, DL_SUBS_UNBIND_REQ);
1518     return;
1519     }
1520    
1521    
1522     /*
1523     * Handles DL_ENABMULTI_REQ (enable multicast address)
1524     */
1525    
1526     static void DLPI_enable_multi(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1527     {
1528 gbeauche 1.3 dl_enabmulti_req_t* req = (dl_enabmulti_req_t*)(void *)mp->b_rptr;
1529 cebix 1.1 uint8 *reqaddr = (uint8 *)(mp->b_rptr + req->dl_addr_offset);
1530     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]));
1531    
1532     // Address must be a multicast address
1533     if (get_address_type(reqaddr) != keaMulticast) {
1534     DLPI_error_ack(the_stream, q, mp, DL_ENABMULTI_REQ, DL_BADADDR, 0);
1535     return;
1536     }
1537    
1538     // Address already in multicast list?
1539     if (the_stream->IsMulticastRegistered(reqaddr)) {
1540     DLPI_error_ack(the_stream, q, mp, DL_ENABMULTI_REQ, DL_BADADDR, 0);
1541     return;
1542     }
1543    
1544     // Tell add-on to enable multicast address
1545     AO_enable_multicast(reqaddr);
1546    
1547     // Add new address to multicast list
1548 gbeauche 1.6 uint8 *addr = Mac2HostAddr(Mac_sysalloc(kEnetPhysicalAddressLength));
1549 cebix 1.1 OTCopy48BitAddress(reqaddr, addr);
1550     the_stream->AddMulticast(addr);
1551    
1552     // On receive now check multicast packets
1553     the_stream->flags |= kAcceptMulticasts;
1554    
1555     // Send reply
1556     DLPI_ok_ack(the_stream, q, mp, DL_ENABMULTI_REQ);
1557     return;
1558     }
1559    
1560    
1561     /*
1562     * Handles DL_DISABMULTI_REQ (disable multicast address)
1563     */
1564    
1565     static void DLPI_disable_multi(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1566     {
1567 gbeauche 1.3 dl_disabmulti_req_t *req = (dl_disabmulti_req_t*)(void *)mp->b_rptr;
1568 cebix 1.1 uint8 *reqaddr = (uint8 *)(mp->b_rptr + req->dl_addr_offset);
1569     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]));
1570    
1571     // Address must be a multicast address
1572     if (get_address_type(reqaddr) != keaMulticast) {
1573     DLPI_error_ack(the_stream, q, mp, DL_DISABMULTI_REQ, DL_BADADDR, 0);
1574     return;
1575     }
1576    
1577     // Find address in multicast list
1578     uint8 *addr = the_stream->IsMulticastRegistered(reqaddr);
1579     if (addr == NULL) {
1580     DLPI_error_ack(the_stream, q, mp, DL_DISABMULTI_REQ, DL_BADADDR, 0);
1581     return;
1582     }
1583    
1584     // Found, then remove
1585     the_stream->RemoveMulticast(addr);
1586 gbeauche 1.6 Mac_sysfree(Host2MacAddr(addr));
1587 cebix 1.1
1588     // Tell add-on to disable multicast address
1589     AO_disable_multicast(reqaddr);
1590    
1591     // No longer check multicast packets if no multicast addresses are registered
1592     if (the_stream->multicast_list == NULL)
1593     the_stream->flags &= ~kAcceptMulticasts;
1594    
1595     // Send reply
1596     DLPI_ok_ack(the_stream, q, mp, DL_DISABMULTI_REQ);
1597     return;
1598     }
1599    
1600    
1601     /*
1602     * Handle DL_UNITDATA_REQ (transmit packet)
1603     */
1604    
1605     static void DLPI_unit_data(DLPIStream *the_stream, queue_t *q, mblk_t *mp)
1606     {
1607     D(bug(" DLPI_unit_data(%p,%p)\n", the_stream, mp));
1608 gbeauche 1.3 dl_unitdata_req_t *req = (dl_unitdata_req_t *)(void *)mp->b_rptr;
1609 cebix 1.1
1610     // Stream must be idle
1611     if (the_stream->dlpi_state != DL_IDLE) {
1612    
1613     // Not idle, send error response
1614     dl_uderror_ind_t *errp;
1615     mblk_t *bp;
1616    
1617     int i = sizeof(dl_uderror_ind_t) + req->dl_dest_addr_length;
1618     if ((bp = allocb(i, BPRI_HI)) == NULL) {
1619     freemsg(mp);
1620     return;
1621     }
1622     bp->b_datap->db_type = M_PROTO;
1623 gbeauche 1.3 errp = (dl_uderror_ind_t *)(void *)bp->b_wptr;
1624 cebix 1.1 errp->dl_primitive = DL_UDERROR_IND;
1625     errp->dl_errno = DL_OUTSTATE;
1626     errp->dl_unix_errno = 0;
1627     errp->dl_dest_addr_length = req->dl_dest_addr_length;
1628     errp->dl_dest_addr_offset = sizeof(dl_uderror_ind_t);
1629     bp->b_wptr += sizeof(dl_uderror_ind_t);
1630     memcpy((uint8 *)bp->b_wptr, ((uint8 *)req) + req->dl_dest_addr_offset, req->dl_dest_addr_length);
1631     bp->b_wptr += req->dl_dest_addr_length;
1632     qreply(q, bp);
1633    
1634     freemsg(mp);
1635     return;
1636     }
1637    
1638     // Build packet header and transmit packet
1639     if ((mp = build_tx_packet_header(the_stream, mp, false)) != NULL)
1640     transmit_packet(mp);
1641     }