sync comments, fix function header, rename variable name to follow coding style.
[mirror_edk2.git] / MdeModulePkg / Universal / Network / SnpDxe / Receive_filters.c
1 /** @file
2 Copyright (c) 2004 - 2007, Intel Corporation
3 All rights reserved. This program and the accompanying materials
4 are licensed and made available under the terms and conditions of the BSD License
5 which accompanies this distribution. The full text of the license may be found at
6 http://opensource.org/licenses/bsd-license.php
7
8 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
9 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
10
11 Module name:
12 receive_filters.c
13
14 Abstract:
15
16 Revision history:
17 2000-Feb-17 M(f)J Genesis.
18
19 **/
20
21
22
23 #include "Snp.h"
24
25 /**
26 this routine calls undi to enable the receive filters.
27
28 @param Snp pointer to snp driver structure
29 @param EnableFlags bit mask for enabling the receive filters
30 @param MCastAddressCount multicast address count for a new multicast address
31 list
32 @param MCastAddressList list of new multicast addresses
33
34
35 **/
36 EFI_STATUS
37 PxeRecvFilterEnable (
38 SNP_DRIVER *Snp,
39 UINT32 EnableFlags,
40 UINTN MCastAddressCount,
41 EFI_MAC_ADDRESS *MCastAddressList
42 )
43 {
44 Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS;
45 Snp->Cdb.OpFlags = PXE_OPFLAGS_RECEIVE_FILTER_ENABLE;
46 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
47 Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
48 Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
49 Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
50 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
51 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
52 Snp->Cdb.IFnum = Snp->IfNum;
53 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
54
55 if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) != 0) {
56 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_UNICAST;
57 }
58
59 if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) != 0) {
60 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
61 }
62
63 if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) != 0) {
64 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS;
65 }
66
67 if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) {
68 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
69 }
70
71 if ((EnableFlags & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) {
72 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST;
73 }
74
75 if (MCastAddressCount != 0) {
76 Snp->Cdb.CPBsize = (UINT16) (MCastAddressCount * sizeof (EFI_MAC_ADDRESS));
77 Snp->Cdb.CPBaddr = (UINT64)(UINTN)Snp->Cpb;
78 CopyMem (Snp->Cpb, MCastAddressList, Snp->Cdb.CPBsize);
79 }
80 //
81 // Issue UNDI command and check result.
82 //
83 DEBUG ((EFI_D_NET, "\nsnp->undi.receive_filters() "));
84
85 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
86
87 if (Snp->Cdb.StatCode != EFI_SUCCESS) {
88 //
89 // UNDI command failed. Return UNDI status to caller.
90 //
91 DEBUG (
92 (EFI_D_ERROR,
93 "\nsnp->undi.receive_filters() %xh:%xh\n",
94 Snp->Cdb.StatFlags,
95 Snp->Cdb.StatCode)
96 );
97
98 switch (Snp->Cdb.StatCode) {
99 case PXE_STATCODE_INVALID_CDB:
100 case PXE_STATCODE_INVALID_CPB:
101 case PXE_STATCODE_INVALID_PARAMETER:
102 return EFI_INVALID_PARAMETER;
103
104 case PXE_STATCODE_UNSUPPORTED:
105 return EFI_UNSUPPORTED;
106 }
107
108 return EFI_DEVICE_ERROR;
109 }
110
111 return EFI_SUCCESS;
112 }
113
114 /**
115 this routine calls undi to disable the receive filters.
116
117 @param Snp pointer to snp driver structure
118 @param DisableFlags bit mask for disabling the receive filters
119 @param ResetMCastList boolean flag to reset/delete the multicast filter list
120
121
122 **/
123 EFI_STATUS
124 PxeRecvFilterDisable (
125 SNP_DRIVER *Snp,
126 UINT32 DisableFlags,
127 BOOLEAN ResetMCastList
128 )
129 {
130 Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS;
131 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
132 Snp->Cdb.DBsize = PXE_DBSIZE_NOT_USED;
133 Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
134 Snp->Cdb.DBaddr = PXE_DBADDR_NOT_USED;
135 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
136 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
137 Snp->Cdb.IFnum = Snp->IfNum;
138 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
139
140 Snp->Cdb.OpFlags = (UINT16) ((DisableFlags != 0) ? PXE_OPFLAGS_RECEIVE_FILTER_DISABLE : PXE_OPFLAGS_NOT_USED);
141
142 if (ResetMCastList) {
143 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_RESET_MCAST_LIST;
144 }
145
146 if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_UNICAST) != 0) {
147 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_UNICAST;
148 }
149
150 if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST) != 0) {
151 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_BROADCAST;
152 }
153
154 if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS) != 0) {
155 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_PROMISCUOUS;
156 }
157
158 if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST) != 0) {
159 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_ALL_MULTICAST;
160 }
161
162 if ((DisableFlags & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0) {
163 Snp->Cdb.OpFlags |= PXE_OPFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST;
164 }
165 //
166 // Issue UNDI command and check result.
167 //
168 DEBUG ((EFI_D_NET, "\nsnp->undi.receive_filters() "));
169
170 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
171
172 if (Snp->Cdb.StatCode != EFI_SUCCESS) {
173 //
174 // UNDI command failed. Return UNDI status to caller.
175 //
176 DEBUG (
177 (EFI_D_ERROR,
178 "\nsnp->undi.receive_filters() %xh:%xh\n",
179 Snp->Cdb.StatFlags,
180 Snp->Cdb.StatCode)
181 );
182
183 return EFI_DEVICE_ERROR;
184 }
185
186 return EFI_SUCCESS;
187 }
188
189 /**
190 this routine calls undi to read the receive filters.
191
192 @param Snp pointer to snp driver structure
193
194
195 **/
196 EFI_STATUS
197 PxeRecvFilterRead (
198 SNP_DRIVER *Snp
199 )
200 {
201 Snp->Cdb.OpCode = PXE_OPCODE_RECEIVE_FILTERS;
202 Snp->Cdb.OpFlags = PXE_OPFLAGS_RECEIVE_FILTER_READ;
203 Snp->Cdb.CPBsize = PXE_CPBSIZE_NOT_USED;
204 Snp->Cdb.DBsize = (UINT16) (Snp->Mode.MaxMCastFilterCount * sizeof (EFI_MAC_ADDRESS));
205 Snp->Cdb.CPBaddr = PXE_CPBADDR_NOT_USED;
206 if (Snp->Cdb.DBsize == 0) {
207 Snp->Cdb.DBaddr = (UINT64)(UINTN) NULL;
208 } else {
209 Snp->Cdb.DBaddr = (UINT64)(UINTN) Snp->Db;
210 ZeroMem (Snp->Db, Snp->Cdb.DBsize);
211 }
212
213 Snp->Cdb.StatCode = PXE_STATCODE_INITIALIZE;
214 Snp->Cdb.StatFlags = PXE_STATFLAGS_INITIALIZE;
215 Snp->Cdb.IFnum = Snp->IfNum;
216 Snp->Cdb.Control = PXE_CONTROL_LAST_CDB_IN_LIST;
217
218 DEBUG ((EFI_D_NET, "\nsnp->undi.receive_filters() "));
219
220 (*Snp->IssueUndi32Command) ((UINT64)(UINTN) &Snp->Cdb);
221
222 if (Snp->Cdb.StatCode != EFI_SUCCESS) {
223 //
224 // UNDI command failed. Return UNDI status to caller.
225 //
226 DEBUG (
227 (EFI_D_ERROR,
228 "\nsnp->undi.receive_filters() %xh:%xh\n",
229 Snp->Cdb.StatFlags,
230 Snp->Cdb.StatCode)
231 );
232
233 return EFI_DEVICE_ERROR;
234 }
235 //
236 // Convert UNDI32 StatFlags to EFI SNP filter flags.
237 //
238 Snp->Mode.ReceiveFilterSetting = 0;
239
240 if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_UNICAST) != 0) {
241 Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_UNICAST;
242 }
243
244 if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_BROADCAST) != 0) {
245 Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_BROADCAST;
246 }
247
248 if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_PROMISCUOUS) != 0) {
249 Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS;
250 }
251
252 if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_ALL_MULTICAST) != 0) {
253 Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_PROMISCUOUS_MULTICAST;
254 }
255
256 if ((Snp->Cdb.StatFlags & PXE_STATFLAGS_RECEIVE_FILTER_FILTERED_MULTICAST) != 0) {
257 Snp->Mode.ReceiveFilterSetting |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST;
258 }
259
260 CopyMem (Snp->Mode.MCastFilter, Snp->Db, Snp->Cdb.DBsize);
261
262 //
263 // Count number of active entries in multicast filter list.
264 //
265 {
266 EFI_MAC_ADDRESS ZeroMacAddr;
267
268 SetMem (&ZeroMacAddr, sizeof ZeroMacAddr, 0);
269
270 for (Snp->Mode.MCastFilterCount = 0;
271 Snp->Mode.MCastFilterCount < Snp->Mode.MaxMCastFilterCount;
272 Snp->Mode.MCastFilterCount++
273 ) {
274 if (CompareMem (
275 &Snp->Mode.MCastFilter[Snp->Mode.MCastFilterCount],
276 &ZeroMacAddr,
277 sizeof ZeroMacAddr
278 ) == 0) {
279 break;
280 }
281 }
282 }
283
284 return EFI_SUCCESS;
285 }
286
287
288 /**
289 Manages the multicast receive filters of a network interface.
290
291 This function is used enable and disable the hardware and software receive
292 filters for the underlying network device.
293 The receive filter change is broken down into three steps:
294 * The filter mask bits that are set (ON) in the Enable parameter are added to
295 the current receive filter settings.
296 * The filter mask bits that are set (ON) in the Disable parameter are subtracted
297 from the updated receive filter settings.
298 * If the resulting receive filter setting is not supported by the hardware a
299 more liberal setting is selected.
300 If the same bits are set in the Enable and Disable parameters, then the bits
301 in the Disable parameter takes precedence.
302 If the ResetMCastFilter parameter is TRUE, then the multicast address list
303 filter is disabled (irregardless of what other multicast bits are set in the
304 Enable and Disable parameters). The SNP->Mode->MCastFilterCount field is set
305 to zero. The Snp->Mode->MCastFilter contents are undefined.
306 After enabling or disabling receive filter settings, software should verify
307 the new settings by checking the Snp->Mode->ReceiveFilterSettings,
308 Snp->Mode->MCastFilterCount and Snp->Mode->MCastFilter fields.
309 Note: Some network drivers and/or devices will automatically promote receive
310 filter settings if the requested setting can not be honored. For example, if
311 a request for four multicast addresses is made and the underlying hardware
312 only supports two multicast addresses the driver might set the promiscuous
313 or promiscuous multicast receive filters instead. The receiving software is
314 responsible for discarding any extra packets that get through the hardware
315 receive filters.
316 Note: Note: To disable all receive filter hardware, the network driver must
317 be Shutdown() and Stopped(). Calling ReceiveFilters() with Disable set to
318 Snp->Mode->ReceiveFilterSettings will make it so no more packets are
319 returned by the Receive() function, but the receive hardware may still be
320 moving packets into system memory before inspecting and discarding them.
321 Unexpected system errors, reboots and hangs can occur if an OS is loaded
322 and the network devices are not Shutdown() and Stopped().
323 If ResetMCastFilter is TRUE, then the multicast receive filter list on the
324 network interface will be reset to the default multicast receive filter list.
325 If ResetMCastFilter is FALSE, and this network interface allows the multicast
326 receive filter list to be modified, then the MCastFilterCnt and MCastFilter
327 are used to update the current multicast receive filter list. The modified
328 receive filter list settings can be found in the MCastFilter field of
329 EFI_SIMPLE_NETWORK_MODE. If the network interface does not allow the multicast
330 receive filter list to be modified, then EFI_INVALID_PARAMETER will be returned.
331 If the driver has not been initialized, EFI_DEVICE_ERROR will be returned.
332 If the receive filter mask and multicast receive filter list have been
333 successfully updated on the network interface, EFI_SUCCESS will be returned.
334
335 @param This A pointer to the EFI_SIMPLE_NETWORK_PROTOCOL instance.
336 @param Enable A bit mask of receive filters to enable on the network
337 interface.
338 @param Disable A bit mask of receive filters to disable on the network
339 interface. For backward compatibility with EFI 1.1
340 platforms, the EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit
341 must be set when the ResetMCastFilter parameter is TRUE.
342 @param ResetMCastFilter Set to TRUE to reset the contents of the multicast
343 receive filters on the network interface to their
344 default values.
345 @param MCastFilterCnt Number of multicast HW MAC addresses in the new MCastFilter
346 list. This value must be less than or equal to the
347 MCastFilterCnt field of EFI_SIMPLE_NETWORK_MODE.
348 This field is optional if ResetMCastFilter is TRUE.
349 @param MCastFilter A pointer to a list of new multicast receive filter HW
350 MAC addresses. This list will replace any existing
351 multicast HW MAC address list. This field is optional
352 if ResetMCastFilter is TRUE.
353
354 @retval EFI_SUCCESS The multicast receive filter list was updated.
355 @retval EFI_NOT_STARTED The network interface has not been started.
356 @retval EFI_INVALID_PARAMETER One or more of the following conditions is TRUE:
357 * This is NULL
358 * There are bits set in Enable that are not set
359 in Snp->Mode->ReceiveFilterMask
360 * There are bits set in Disable that are not set
361 in Snp->Mode->ReceiveFilterMask
362 * Multicast is being enabled (the
363 EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST bit is
364 set in Enable, it is not set in Disable, and
365 ResetMCastFilter is FALSE) and MCastFilterCount
366 is zero
367 * Multicast is being enabled and MCastFilterCount
368 is greater than Snp->Mode->MaxMCastFilterCount
369 * Multicast is being enabled and MCastFilter is NULL
370 * Multicast is being enabled and one or more of
371 the addresses in the MCastFilter list are not
372 valid multicast MAC addresses
373 @retval EFI_DEVICE_ERROR One or more of the following conditions is TRUE:
374 * The network interface has been started but has
375 not been initialized
376 * An unexpected error was returned by the
377 underlying network driver or device
378 @retval EFI_UNSUPPORTED This function is not supported by the network
379 interface.
380
381 **/
382 EFI_STATUS
383 EFIAPI
384 SnpUndi32ReceiveFilters (
385 IN EFI_SIMPLE_NETWORK_PROTOCOL *This,
386 IN UINT32 Enable,
387 IN UINT32 Disable,
388 IN BOOLEAN ResetMCastFilter,
389 IN UINTN MCastFilterCnt, OPTIONAL
390 IN EFI_MAC_ADDRESS *MCastFilter OPTIONAL
391 )
392 {
393 SNP_DRIVER *Snp;
394 EFI_STATUS Status;
395 EFI_TPL OldTpl;
396
397 if (This == NULL) {
398 return EFI_INVALID_PARAMETER;
399 }
400
401 Snp = EFI_SIMPLE_NETWORK_DEV_FROM_THIS (This);
402
403 OldTpl = gBS->RaiseTPL (TPL_CALLBACK);
404
405 switch (Snp->Mode.State) {
406 case EfiSimpleNetworkInitialized:
407 break;
408
409 case EfiSimpleNetworkStopped:
410 Status = EFI_NOT_STARTED;
411 goto ON_EXIT;
412
413 default:
414 Status = EFI_DEVICE_ERROR;
415 goto ON_EXIT;
416 }
417 //
418 // check if we are asked to enable or disable something that the UNDI
419 // does not even support!
420 //
421 if (((Enable &~Snp->Mode.ReceiveFilterMask) != 0) ||
422 ((Disable &~Snp->Mode.ReceiveFilterMask) != 0)) {
423 Status = EFI_INVALID_PARAMETER;
424 goto ON_EXIT;
425 }
426
427 if (ResetMCastFilter) {
428
429 Disable |= EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST & Snp->Mode.ReceiveFilterMask;
430 MCastFilterCnt = 0;
431 MCastFilter = NULL;
432 } else {
433 if (MCastFilterCnt != 0) {
434 if ((MCastFilterCnt > Snp->Mode.MaxMCastFilterCount) ||
435 (MCastFilter == NULL)) {
436
437 Status = EFI_INVALID_PARAMETER;
438 goto ON_EXIT;
439 }
440 }
441 }
442
443 if (Enable == 0 && Disable == 0 && !ResetMCastFilter && MCastFilterCnt == 0) {
444 Status = EFI_SUCCESS;
445 goto ON_EXIT;
446 }
447
448 if ((Enable & EFI_SIMPLE_NETWORK_RECEIVE_MULTICAST) != 0 && MCastFilterCnt == 0) {
449 Status = EFI_INVALID_PARAMETER;
450 goto ON_EXIT;
451 }
452
453 if ((Enable != 0) || (MCastFilterCnt != 0)) {
454 Status = PxeRecvFilterEnable (
455 Snp,
456 Enable,
457 MCastFilterCnt,
458 MCastFilter
459 );
460
461 if (EFI_ERROR (Status)) {
462 goto ON_EXIT;
463 }
464 }
465
466 if ((Disable != 0) || ResetMCastFilter) {
467 Status = PxeRecvFilterDisable (Snp, Disable, ResetMCastFilter);
468
469 if (EFI_ERROR (Status)) {
470 goto ON_EXIT;
471 }
472 }
473
474 Status = PxeRecvFilterRead (Snp);
475
476 ON_EXIT:
477 gBS->RestoreTPL (OldTpl);
478
479 return Status;
480 }