1/*
2 * Copyright (c) 1998-2008 Apple 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#ifndef _IOETHERNETINTERFACE_H
24#define _IOETHERNETINTERFACE_H
25
26/*! @defined kIOEthernetInterfaceClass
27    @abstract The name of the
28        IOEthernetInterface class.
29*/
30
31#define kIOEthernetInterfaceClass     "IOEthernetInterface"
32
33/*! @defined kIOActivePacketFilters
34    @abstract A property of IOEthernetInterface objects.
35    @discussion The kIOActivePacketFilters property has an OSDictionary value that describes the current
36        set of packet filters that have been successfully activated. Each
37        entry in the dictionary is a key/value pair consisting of the filter
38        group name, and an OSNumber describing the set of active filters for
39        that group. Entries in this dictionary will mirror those in
40        kIORequiredPacketFilters if the controller has reported success for
41        all filter change requests from the IOEthernetInterface object.
42*/
43
44#define kIOActivePacketFilters        "IOActivePacketFilters"
45
46/*! @defined kIORequiredPacketFilters
47    @abstract A property of IOEthernetInterface objects.
48    @discussion The kIORequiredPacketFilters property has an OSDictionary value that describes the current
49        set of required packet filters. Each entry in the dictionary is a
50        key/value pair consisting of the filter group name, and an OSNumber
51        describing the set of required filters for that group.
52*/
53
54#define kIORequiredPacketFilters      "IORequiredPacketFilters"
55
56/*! @defined kIOMulticastAddressList
57    @abstract A property of IOEthernetInterface objects.
58    @discussion The kIOMulticastAddressList property is an OSData object that describes the
59        list of multicast addresses that are being used by the
60        controller to match against the destination address of an
61        incoming frame.
62*/
63
64#define kIOMulticastAddressList       "IOMulticastAddressList"
65#define kIOMulticastFilterData        kIOMulticastAddressList
66
67#ifdef KERNEL
68#ifdef __cplusplus
69
70#include <IOKit/network/IONetworkInterface.h>
71#include <IOKit/network/IOEthernetController.h>
72#include <IOKit/network/IOEthernetStats.h>
73
74/*! @class IOEthernetInterface
75    @abstract The Ethernet interface object.
76    @discussion An Ethernet controller driver,
77    that is a subclass of IOEthernetController, will instantiate an object
78    of this class when the driver calls the attachInterface() method.
79    This interface object will then vend an Ethernet interface to DLIL,
80    and manage the connection between the controller driver and the upper
81    networking layers. Drivers will seldom need to subclass
82    IOEthernetInterface.
83*/
84
85class IOEthernetInterface : public IONetworkInterface
86{
87    OSDeclareDefaultStructors( IOEthernetInterface )
88
89private:
90    thread_call_t    _inputEventThreadCall; // inputEvent() thread call
91    UInt32           _mcAddrCount;          // # of multicast addresses
92    bool             _ctrEnabled;           // Is controller enabled?
93    OSDictionary *   _supportedFilters;     // Controller's supported filters
94    OSDictionary *   _requiredFilters;      // The required filters
95    OSDictionary *   _activeFilters;        // Currently active filters
96    bool             _controllerLostPower;  // true if controller is unusable
97
98    struct ExpansionData {
99        UInt32      altMTU;                 // track the physical mtu of controller
100        UInt32      publishedFeatureID;     // id for published wake packet
101        uint32_t    supportedWakeFilters;   // bitmask of supported wake filters
102        OSNumber *  disabledWakeFilters;    // OSNumber of disabled wake filters
103        uint64_t    wompEnabledAssertionID;
104	};
105    /*! @var reserved
106        Reserved for future use.  (Internal use only)  */
107    ExpansionData *  _reserved;
108
109
110    IOReturn enableController(IONetworkController * ctr);
111    IOReturn setupMulticastFilter(IONetworkController * ctr);
112
113    UInt32 getFilters(const OSDictionary * dict,
114                      const OSSymbol *     group);
115
116    bool setFilters(OSDictionary *   dict,
117                    const OSSymbol * group,
118                    UInt32           filters);
119
120    IOReturn disableFilter(IONetworkController * ctr,
121                           const OSSymbol *      group,
122                           UInt32                filter,
123                           IOOptionBits          options = 0);
124
125    IOReturn enableFilter(IONetworkController * ctr,
126                          const OSSymbol *      group,
127                          UInt32                filter,
128                          IOOptionBits          options = 0);
129
130    int syncSIOCSIFFLAGS(IONetworkController * ctr);
131    int syncSIOCSIFADDR(IONetworkController * ctr);
132    int syncSIOCADDMULTI(IONetworkController * ctr);
133    int syncSIOCDELMULTI(IONetworkController * ctr);
134    int syncSIOCSIFMTU(IONetworkController * ctr, struct ifreq * ifr, bool);
135    int syncSIOCGIFDEVMTU(IONetworkController * ctr, struct ifreq * ifr);
136    int syncSIOCSIFLLADDR(IONetworkController * ctr, const char * lladdr, int len);
137	void _fixupVlanPacket(mbuf_t, u_int16_t, int);
138    void reportInterfaceWakeFlags(IONetworkController * ctr);
139
140    static void handleEthernetInputEvent(thread_call_param_t param0, thread_call_param_t param1);
141    static int performGatedCommand(void *, void *, void *, void *, void *);
142	static IOReturn enableFilter_Wrapper(
143        IOEthernetInterface *, IONetworkController *, const OSSymbol *, UInt32 , IOOptionBits);
144
145public:
146
147/*! @function init
148    @abstract Initializes an IOEthernetInterface instance.
149    @discussion Instance variables are initialized, and an arpcom
150    structure is allocated.
151    @param controller A network controller object that will service
152    the interface object being initialized.
153    @result Returns true on success, false otherwise.
154*/
155
156    virtual bool init( IONetworkController * controller );
157
158/*! @function getNamePrefix
159    @abstract Returns a string containing the prefix to use when
160    creating a BSD name for this interface.
161    @discussion The BSD name for each interface object is created by
162    concatenating a string returned by this method, with an unique
163    unit number assigned by IONetworkStack.
164    @result Returns a pointer to a constant C string "en". Therefore, Ethernet
165    interfaces will be registered with BSD as en0, en1, etc.
166*/
167
168    virtual const char * getNamePrefix() const;
169
170protected:
171
172/*! @function free
173    @abstract Frees the IOEthernetInterface instance.
174    @discussion The memory allocated for the arpcom structure is released,
175    followed by a call to super::free().
176*/
177
178    virtual void free();
179
180/*! @function performCommand
181    @abstract Handles an ioctl command sent to the Ethernet interface.
182    @discussion This method handles socket ioctl commands sent to the Ethernet
183    interface from DLIL. Commands recognized and processed by this method are
184    SIOCSIFADDR, SIOCSIFFLAGS, SIOCADDMULTI, and SIOCDELMULTI. Other commands
185    are passed to the superclass.
186    @param controller The controller object.
187    @param cmd  The ioctl command code.
188    @param arg0 Command argument 0. Generally a pointer to an ifnet structure
189        associated with the interface.
190    @param arg1 Command argument 1.
191    @result Returns a BSD return value defined in bsd/sys/errno.h.
192*/
193
194    virtual SInt32 performCommand(IONetworkController * controller,
195                                  unsigned long         cmd,
196                                  void *                arg0,
197                                  void *                arg1);
198
199/*! @function controllerDidOpen
200    @abstract A notification that the interface has opened the network
201    controller.
202    @discussion This method will be called by IONetworkInterface after a
203    network controller has accepted an open from this interface object.
204    IOEthernetInterface will first call the implementation in its
205    superclass, then inspect the controller through properties published
206    in the registry. This method is called with the arbitration lock held.
207    @param controller The controller object that was opened.
208    @result Returns true on success, false otherwise. Returning false will
209    cause the controller to be closed, and any pending client opens to be
210    rejected.
211*/
212
213    virtual bool controllerDidOpen(IONetworkController * controller);
214
215/*! @function controllerWillClose
216    @abstract A notification that the interface will close the network
217    controller.
218    @discussion This method will simply call super to propagate the method
219    call. This method is called with the arbitration lock held.
220    @param controller The controller that is about to be closed.
221*/
222
223    virtual void controllerWillClose(IONetworkController * controller);
224
225
226/*! @function controllerWillChangePowerState
227    @abstract Handles a notification that the network controller
228    servicing this interface object is about to transition to a new power state.
229    @discussion If the controller is about to transition to an unusable state,
230    and it is currently enabled, then the disable() method on the controller is
231    called.
232    @param controller The network controller object.
233    @param flags Flags that describe the capability of the controller in the new
234    power state.
235    @param stateNumber An index to a state in the network controller's
236    power state array that the controller is switching to.
237    @param policyMaker A reference to the network controller's policy-maker,
238    and is also the originator of this notification.
239    @result Always returns kIOReturnSuccess.
240*/
241
242    virtual IOReturn controllerWillChangePowerState(
243                               IONetworkController * controller,
244                               IOPMPowerFlags        flags,
245                               UInt32                stateNumber,
246                               IOService *           policyMaker);
247
248/*! @function controllerDidChangePowerState
249    @abstract Handles a notification that the network controller servicing
250    this interface object has transitioned to a new power state.
251    @discussion If the controller did transition to a usable state, and it was
252    previously disabled due to a previous power change, then it is re-enabled.
253    @param controller The network controller object.
254    @param flags Flags that describe the capability of the controller in the new
255    power state.
256    @param stateNumber An index to a state in the network controller's
257    power state array that the controller has switched to.
258    @param policyMaker A reference to the network controller's policy-maker,
259    and is also the originator of this notification.
260    @result Always returns kIOReturnSuccess.
261*/
262
263    virtual IOReturn controllerDidChangePowerState(
264                               IONetworkController * controller,
265                               IOPMPowerFlags        flags,
266                               UInt32                stateNumber,
267                               IOService *           policyMaker);
268
269public:
270    /* Override IONetworkInterface::willTerminate() */
271
272    virtual bool willTerminate( IOService *  provider,
273                                IOOptionBits options );
274
275    /* Override IONetworkInterface::attachToDataLinkLayer() */
276
277    virtual IOReturn attachToDataLinkLayer( IOOptionBits options,
278                                            void *       parameter );
279
280    /* Override IONetworkInterface::inputEvent() */
281
282    virtual bool inputEvent( UInt32 type, void * data );
283
284protected:
285	virtual void feedPacketInputTap(mbuf_t);
286	virtual void feedPacketOutputTap(mbuf_t);
287	virtual bool initIfnetParams(struct ifnet_init_params *params);
288
289public:
290    // Virtual function padding
291    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  0);
292    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  1);
293    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  2);
294    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  3);
295    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  4);
296    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  5);
297    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  6);
298    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  7);
299    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  8);
300    OSMetaClassDeclareReservedUnused( IOEthernetInterface,  9);
301    OSMetaClassDeclareReservedUnused( IOEthernetInterface, 10);
302    OSMetaClassDeclareReservedUnused( IOEthernetInterface, 11);
303    OSMetaClassDeclareReservedUnused( IOEthernetInterface, 12);
304    OSMetaClassDeclareReservedUnused( IOEthernetInterface, 13);
305    OSMetaClassDeclareReservedUnused( IOEthernetInterface, 14);
306    OSMetaClassDeclareReservedUnused( IOEthernetInterface, 15);
307};
308
309#endif /* __cplusplus */
310#endif /* KERNEL */
311#endif /* !_IOETHERNETINTERFACE_H */
312