]> git.saurik.com Git - apple/xnu.git/blob - iokit/Families/IONetworking/IOEthernetController.cpp
xnu-123.5.tar.gz
[apple/xnu.git] / iokit / Families / IONetworking / IOEthernetController.cpp
1 /*
2 * Copyright (c) 1998-2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22 /*
23 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
24 *
25 * IOEthernetController.cpp
26 *
27 * Abstract Ethernet controller superclass.
28 *
29 * HISTORY
30 *
31 * Dec 3, 1998 jliu - C++ conversion.
32 */
33
34 #include <IOKit/assert.h>
35 #include <IOKit/network/IOEthernetController.h>
36 #include <IOKit/network/IOEthernetInterface.h>
37
38 extern "C" {
39 #include <sys/socket.h>
40 #include <net/if.h>
41 #include <net/etherdefs.h>
42 }
43
44 //---------------------------------------------------------------------------
45
46 #define super IONetworkController
47
48 OSDefineMetaClassAndAbstractStructors( IOEthernetController, IONetworkController)
49 OSMetaClassDefineReservedUnused( IOEthernetController, 0);
50 OSMetaClassDefineReservedUnused( IOEthernetController, 1);
51 OSMetaClassDefineReservedUnused( IOEthernetController, 2);
52 OSMetaClassDefineReservedUnused( IOEthernetController, 3);
53 OSMetaClassDefineReservedUnused( IOEthernetController, 4);
54 OSMetaClassDefineReservedUnused( IOEthernetController, 5);
55 OSMetaClassDefineReservedUnused( IOEthernetController, 6);
56 OSMetaClassDefineReservedUnused( IOEthernetController, 7);
57 OSMetaClassDefineReservedUnused( IOEthernetController, 8);
58 OSMetaClassDefineReservedUnused( IOEthernetController, 9);
59 OSMetaClassDefineReservedUnused( IOEthernetController, 10);
60 OSMetaClassDefineReservedUnused( IOEthernetController, 11);
61 OSMetaClassDefineReservedUnused( IOEthernetController, 12);
62 OSMetaClassDefineReservedUnused( IOEthernetController, 13);
63 OSMetaClassDefineReservedUnused( IOEthernetController, 14);
64 OSMetaClassDefineReservedUnused( IOEthernetController, 15);
65 OSMetaClassDefineReservedUnused( IOEthernetController, 16);
66 OSMetaClassDefineReservedUnused( IOEthernetController, 17);
67 OSMetaClassDefineReservedUnused( IOEthernetController, 18);
68 OSMetaClassDefineReservedUnused( IOEthernetController, 19);
69 OSMetaClassDefineReservedUnused( IOEthernetController, 20);
70 OSMetaClassDefineReservedUnused( IOEthernetController, 21);
71 OSMetaClassDefineReservedUnused( IOEthernetController, 22);
72 OSMetaClassDefineReservedUnused( IOEthernetController, 23);
73 OSMetaClassDefineReservedUnused( IOEthernetController, 24);
74 OSMetaClassDefineReservedUnused( IOEthernetController, 25);
75 OSMetaClassDefineReservedUnused( IOEthernetController, 26);
76 OSMetaClassDefineReservedUnused( IOEthernetController, 27);
77 OSMetaClassDefineReservedUnused( IOEthernetController, 28);
78 OSMetaClassDefineReservedUnused( IOEthernetController, 29);
79 OSMetaClassDefineReservedUnused( IOEthernetController, 30);
80 OSMetaClassDefineReservedUnused( IOEthernetController, 31);
81
82 //-------------------------------------------------------------------------
83 // Macros
84
85 #ifdef DEBUG
86 #define DLOG(fmt, args...) IOLog(fmt, ## args)
87 #else
88 #define DLOG(fmt, args...)
89 #endif
90
91 //---------------------------------------------------------------------------
92 // IOEthernetController class initializer.
93
94 void IOEthernetController::initialize()
95 {
96 }
97
98 //---------------------------------------------------------------------------
99 // Initialize an IOEthernetController instance.
100
101 bool IOEthernetController::init(OSDictionary * properties)
102 {
103 if (!super::init(properties))
104 {
105 DLOG("IOEthernetController: super::init() failed\n");
106 return false;
107 }
108
109 return true;
110 }
111
112 //---------------------------------------------------------------------------
113 // Free the IOEthernetController instance.
114
115 void IOEthernetController::free()
116 {
117 // Any allocated resources should be released here.
118
119 super::free();
120 }
121
122 //---------------------------------------------------------------------------
123 // Publish Ethernet controller capabilites and properties.
124
125 bool IOEthernetController::publishProperties()
126 {
127 bool ret = false;
128 IOEthernetAddress addr;
129 OSDictionary * dict;
130
131 do {
132 // Let the superclass publish properties first.
133
134 if (super::publishProperties() == false)
135 break;
136
137 // Publish the controller's Ethernet address.
138
139 if ( (getHardwareAddress(&addr) != kIOReturnSuccess) ||
140 (setProperty(kIOMACAddress, (void *) &addr,
141 kIOEthernetAddressSize) == false) )
142 {
143 break;
144 }
145
146 // Publish Ethernet defined packet filters.
147
148 dict = OSDynamicCast(OSDictionary, getProperty(kIOPacketFilters));
149 if ( dict )
150 {
151 UInt32 filters;
152 OSNumber * num;
153
154 if ( getPacketFilters(gIOEthernetWakeOnLANFilterGroup,
155 &filters) != kIOReturnSuccess )
156 {
157 break;
158 }
159
160 num = OSNumber::withNumber(filters, sizeof(filters) * 8);
161 if (num == 0)
162 break;
163
164 ret = dict->setObject(gIOEthernetWakeOnLANFilterGroup, num);
165 num->release();
166 }
167 }
168 while (false);
169
170 return ret;
171 }
172
173 //---------------------------------------------------------------------------
174 // Set or change the station address used by the Ethernet controller.
175
176 IOReturn
177 IOEthernetController::setHardwareAddress(const IOEthernetAddress * addr)
178 {
179 return kIOReturnUnsupported;
180 }
181
182 //---------------------------------------------------------------------------
183 // Enable or disable multicast mode.
184
185 IOReturn IOEthernetController::setMulticastMode(bool active)
186 {
187 return kIOReturnUnsupported;
188 }
189
190 //---------------------------------------------------------------------------
191 // Enable or disable promiscuous mode.
192
193 IOReturn IOEthernetController::setPromiscuousMode(bool active)
194 {
195 return kIOReturnUnsupported;
196 }
197
198 //---------------------------------------------------------------------------
199 // Enable or disable the wake on Magic Packet support.
200
201 IOReturn IOEthernetController::setWakeOnMagicPacket(bool active)
202 {
203 return kIOReturnUnsupported;
204 }
205
206 //---------------------------------------------------------------------------
207 // Set the list of multicast addresses that the multicast filter should use
208 // to match against the destination address of an incoming frame. The frame
209 // should be accepted when a match occurs.
210
211 IOReturn IOEthernetController::setMulticastList(IOEthernetAddress * /*addrs*/,
212 UInt32 /*count*/)
213 {
214 return kIOReturnUnsupported;
215 }
216
217 //---------------------------------------------------------------------------
218 // Allocate and return a new IOEthernetInterface instance.
219
220 IONetworkInterface * IOEthernetController::createInterface()
221 {
222 IOEthernetInterface * netif = new IOEthernetInterface;
223
224 if ( netif && ( netif->init( this ) == false ) )
225 {
226 netif->release();
227 netif = 0;
228 }
229 return netif;
230 }
231
232 //---------------------------------------------------------------------------
233 // Returns all the packet filters supported by the Ethernet controller.
234 // This method will perform a bitwise OR of:
235 //
236 // kIOPacketFilterUnicast
237 // kIOPacketFilterBroadcast
238 // kIOPacketFilterMulticast
239 // kIOPacketFilterPromiscuous
240 //
241 // and write it to the argument provided if the group specified is
242 // gIONetworkFilterGroup, otherwise 0 is returned. Drivers that support
243 // a different set of filters should override this method.
244 //
245 // Returns kIOReturnSuccess. Drivers that override this method must return
246 // kIOReturnSuccess to indicate success, or an error code otherwise.
247
248 IOReturn
249 IOEthernetController::getPacketFilters(const OSSymbol * group,
250 UInt32 * filters) const
251 {
252 *filters = 0;
253
254 if ( group == gIONetworkFilterGroup )
255 {
256 return getPacketFilters(filters);
257 }
258 else
259 {
260 return kIOReturnSuccess;
261 }
262 }
263
264 IOReturn IOEthernetController::getPacketFilters(UInt32 * filters) const
265 {
266 *filters = ( kIOPacketFilterUnicast |
267 kIOPacketFilterBroadcast |
268 kIOPacketFilterMulticast |
269 kIOPacketFilterPromiscuous );
270
271 return kIOReturnSuccess;
272 }
273
274 //---------------------------------------------------------------------------
275 // Enable a filter from the specified group.
276
277 #define UCAST_BCAST_MASK \
278 ( kIOPacketFilterUnicast | kIOPacketFilterBroadcast )
279
280 IOReturn IOEthernetController::enablePacketFilter(
281 const OSSymbol * group,
282 UInt32 aFilter,
283 UInt32 enabledFilters,
284 IOOptionBits options = 0)
285 {
286 IOReturn ret = kIOReturnUnsupported;
287 UInt32 newFilters = enabledFilters | aFilter;
288
289 if ( group == gIONetworkFilterGroup )
290 {
291 // The default action is to call setMulticastMode() or
292 // setPromiscuousMode() to handle multicast or promiscuous
293 // filter changes.
294
295 if ( aFilter == kIOPacketFilterMulticast )
296 {
297 ret = setMulticastMode(true);
298 }
299 else if ( aFilter == kIOPacketFilterPromiscuous )
300 {
301 ret = setPromiscuousMode(true);
302 }
303 else if ( (newFilters ^ enabledFilters) & UCAST_BCAST_MASK )
304 {
305 ret = kIOReturnSuccess;
306 }
307 }
308 else if ( group == gIOEthernetWakeOnLANFilterGroup )
309 {
310 if ( aFilter == kIOEthernetWakeOnMagicPacket )
311 {
312 ret = setWakeOnMagicPacket(true);
313 }
314 }
315
316 return ret;
317 }
318
319 //---------------------------------------------------------------------------
320 // Disable a filter from the specifed filter group.
321
322 IOReturn IOEthernetController::disablePacketFilter(
323 const OSSymbol * group,
324 UInt32 aFilter,
325 UInt32 enabledFilters,
326 IOOptionBits options = 0)
327 {
328 IOReturn ret = kIOReturnUnsupported;
329 UInt32 newFilters = enabledFilters & ~aFilter;
330
331 if ( group == gIONetworkFilterGroup )
332 {
333 // The default action is to call setMulticastMode() or
334 // setPromiscuousMode() to handle multicast or promiscuous
335 // filter changes.
336
337 if ( aFilter == kIOPacketFilterMulticast )
338 {
339 ret = setMulticastMode(false);
340 }
341 else if ( aFilter == kIOPacketFilterPromiscuous )
342 {
343 ret = setPromiscuousMode(false);
344 }
345 else if ( (newFilters ^ enabledFilters) & UCAST_BCAST_MASK )
346 {
347 ret = kIOReturnSuccess;
348 }
349 }
350 else if ( group == gIOEthernetWakeOnLANFilterGroup )
351 {
352 if ( aFilter == kIOEthernetWakeOnMagicPacket )
353 {
354 ret = setWakeOnMagicPacket(false);
355 }
356 }
357
358 return ret;
359 }
360
361 //---------------------------------------------------------------------------
362 // Get the Ethernet controller's station address.
363 // Call the Ethernet specific (overloaded) form.
364
365 IOReturn
366 IOEthernetController::getHardwareAddress(void * addr,
367 UInt32 * inOutAddrBytes)
368 {
369 UInt32 bufBytes;
370
371 if (inOutAddrBytes == 0)
372 return kIOReturnBadArgument;
373
374 // Cache the size of the caller's buffer, and replace it with the
375 // number of bytes required.
376
377 bufBytes = *inOutAddrBytes;
378 *inOutAddrBytes = kIOEthernetAddressSize;
379
380 // Make sure the buffer is large enough for a single Ethernet
381 // hardware address.
382
383 if ((addr == 0) || (bufBytes < kIOEthernetAddressSize))
384 return kIOReturnNoSpace;
385
386 return getHardwareAddress((IOEthernetAddress *) addr);
387 }
388
389 //---------------------------------------------------------------------------
390 // Set or change the station address used by the Ethernet controller.
391 // Call the Ethernet specific (overloaded) version of this method.
392
393 IOReturn
394 IOEthernetController::setHardwareAddress(const void * addr,
395 UInt32 addrBytes)
396 {
397 if ((addr == 0) || (addrBytes != kIOEthernetAddressSize))
398 return kIOReturnBadArgument;
399
400 return setHardwareAddress((const IOEthernetAddress *) addr);
401 }
402
403 //---------------------------------------------------------------------------
404 // Report the max/min packet sizes, including the frame header and FCS bytes.
405
406 IOReturn IOEthernetController::getMaxPacketSize(UInt32 * maxSize) const
407 {
408 *maxSize = kIOEthernetMaxPacketSize;
409 return kIOReturnSuccess;
410 }
411
412 IOReturn IOEthernetController::getMinPacketSize(UInt32 * minSize) const
413 {
414 *minSize = kIOEthernetMinPacketSize;
415 return kIOReturnSuccess;
416 }