df19a9f90240a20d6f39ef0238d1aa2b327640bb
[mirror_edk2.git] / ShellPkg / Library / UefiShellNetwork1CommandsLib / Ifconfig.c
1 /** @file
2 The implementation for Shell command ifconfig based on IP4Config2 protocol.
3
4 (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P.<BR>
5 Copyright (c) 2006 - 2015, Intel Corporation. All rights reserved.<BR>
6
7 This program and the accompanying materials
8 are licensed and made available under the terms and conditions of the BSD License
9 which accompanies this distribution. The full text of the license may be found at
10 http://opensource.org/licenses/bsd-license.php.
11
12 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
13 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
14
15 **/
16
17 #include "UefiShellNetwork1CommandsLib.h"
18
19 typedef enum {
20 IfConfigOpList = 1,
21 IfConfigOpSet = 2,
22 IfConfigOpClear = 3
23 } IFCONFIG_OPCODE;
24
25 typedef enum {
26 VarCheckReserved = -1,
27 VarCheckOk = 0,
28 VarCheckDuplicate,
29 VarCheckConflict,
30 VarCheckUnknown,
31 VarCheckLackValue,
32 VarCheckOutOfMem
33 } VAR_CHECK_CODE;
34
35 typedef enum {
36 FlagTypeSingle = 0,
37 FlagTypeNeedVar,
38 FlagTypeNeedSet,
39 FlagTypeSkipUnknown
40 } VAR_CHECK_FLAG_TYPE;
41
42 #define MACADDRMAXSIZE 32
43
44 typedef struct _IFCONFIG_INTERFACE_CB {
45 EFI_HANDLE NicHandle;
46 LIST_ENTRY Link;
47 EFI_IP4_CONFIG2_PROTOCOL *IfCfg;
48 EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo;
49 EFI_IP4_CONFIG2_POLICY Policy;
50 UINT32 DnsCnt;
51 EFI_IPv4_ADDRESS DnsAddr[1];
52 } IFCONFIG_INTERFACE_CB;
53
54 typedef struct _ARG_LIST ARG_LIST;
55
56 struct _ARG_LIST {
57 ARG_LIST *Next;
58 CHAR16 *Arg;
59 };
60
61 typedef struct _IFCONFIG4_PRIVATE_DATA {
62 LIST_ENTRY IfList;
63
64 UINT32 OpCode;
65 CHAR16 *IfName;
66 ARG_LIST *VarArg;
67 } IFCONFIG_PRIVATE_DATA;
68
69 typedef struct _VAR_CHECK_ITEM{
70 CHAR16 *FlagStr;
71 UINT32 FlagID;
72 UINT32 ConflictMask;
73 VAR_CHECK_FLAG_TYPE FlagType;
74 } VAR_CHECK_ITEM;
75
76 SHELL_PARAM_ITEM mIfConfigCheckList[] = {
77 {
78 L"-b",
79 TypeFlag
80 },
81 {
82 L"-l",
83 TypeValue
84 },
85 {
86 L"-r",
87 TypeValue
88 },
89 {
90 L"-c",
91 TypeValue
92 },
93 {
94 L"-s",
95 TypeMaxValue
96 },
97 {
98 NULL,
99 TypeMax
100 },
101 };
102
103 VAR_CHECK_ITEM mSetCheckList[] = {
104 {
105 L"static",
106 0x00000001,
107 0x00000001,
108 FlagTypeSingle
109 },
110 {
111 L"dhcp",
112 0x00000002,
113 0x00000001,
114 FlagTypeSingle
115 },
116 {
117 L"dns",
118 0x00000008,
119 0x00000004,
120 FlagTypeSingle
121 },
122 {
123 NULL,
124 0x0,
125 0x0,
126 FlagTypeSkipUnknown
127 },
128 };
129
130 STATIC CONST CHAR16 PermanentString[10] = L"PERMANENT";
131
132 /**
133 Split a string with specified separator and save the substring to a list.
134
135 @param[in] String The pointer of the input string.
136 @param[in] Separator The specified separator.
137
138 @return The pointer of headnode of ARG_LIST.
139
140 **/
141 ARG_LIST *
142 SplitStrToList (
143 IN CONST CHAR16 *String,
144 IN CHAR16 Separator
145 )
146 {
147 CHAR16 *Str;
148 CHAR16 *ArgStr;
149 ARG_LIST *ArgList;
150 ARG_LIST *ArgNode;
151
152 if (*String == L'\0') {
153 return NULL;
154 }
155
156 //
157 // Copy the CONST string to a local copy.
158 //
159 Str = AllocateCopyPool (StrSize (String), String);
160 ASSERT (Str != NULL);
161 ArgStr = Str;
162
163 //
164 // init a node for the list head.
165 //
166 ArgNode = (ARG_LIST *) AllocateZeroPool (sizeof (ARG_LIST));
167 ASSERT (ArgNode != NULL);
168 ArgList = ArgNode;
169
170 //
171 // Split the local copy and save in the list node.
172 //
173 while (*Str != L'\0') {
174 if (*Str == Separator) {
175 *Str = L'\0';
176 ArgNode->Arg = ArgStr;
177 ArgStr = Str + 1;
178 ArgNode->Next = (ARG_LIST *) AllocateZeroPool (sizeof (ARG_LIST));
179 ASSERT (ArgNode->Next != NULL);
180 ArgNode = ArgNode->Next;
181 }
182
183 Str++;
184 }
185
186 ArgNode->Arg = ArgStr;
187 ArgNode->Next = NULL;
188
189 return ArgList;
190 }
191
192 /**
193 Check the correctness of input Args with '-s' option.
194
195 @param[in] CheckList The pointer of VAR_CHECK_ITEM array.
196 @param[in] Name The pointer of input arg.
197 @param[in] Init The switch to execute the check.
198
199 @return VarCheckOk Valid parameter or Initialize check successfully.
200 @return VarCheckDuplicate Duplicated parameter happened.
201 @return VarCheckConflict Conflicted parameter happened
202 @return VarCheckUnknown Unknown parameter.
203
204 **/
205 VAR_CHECK_CODE
206 IfConfigRetriveCheckListByName(
207 IN VAR_CHECK_ITEM *CheckList,
208 IN CHAR16 *Name,
209 IN BOOLEAN Init
210 )
211 {
212 STATIC UINT32 CheckDuplicate;
213 STATIC UINT32 CheckConflict;
214 VAR_CHECK_CODE RtCode;
215 UINT32 Index;
216 VAR_CHECK_ITEM Arg;
217
218 if (Init) {
219 CheckDuplicate = 0;
220 CheckConflict = 0;
221 return VarCheckOk;
222 }
223
224 RtCode = VarCheckOk;
225 Index = 0;
226 Arg = CheckList[Index];
227
228 //
229 // Check the Duplicated/Conflicted/Unknown input Args.
230 //
231 while (Arg.FlagStr != NULL) {
232 if (StrCmp (Arg.FlagStr, Name) == 0) {
233
234 if (CheckDuplicate & Arg.FlagID) {
235 RtCode = VarCheckDuplicate;
236 break;
237 }
238
239 if (CheckConflict & Arg.ConflictMask) {
240 RtCode = VarCheckConflict;
241 break;
242 }
243
244 CheckDuplicate |= Arg.FlagID;
245 CheckConflict |= Arg.ConflictMask;
246 break;
247 }
248
249 Arg = CheckList[++Index];
250 }
251
252 if (Arg.FlagStr == NULL) {
253 RtCode = VarCheckUnknown;
254 }
255
256 return RtCode;
257 }
258
259 /**
260 The notify function of create event when performing a manual config.
261
262 @param[in] Event The event this notify function registered to.
263 @param[in] Context Pointer to the context data registered to the event.
264
265 **/
266 VOID
267 EFIAPI
268 IfConfigManualAddressNotify (
269 IN EFI_EVENT Event,
270 IN VOID *Context
271 )
272 {
273 *((BOOLEAN *) Context) = TRUE;
274 }
275
276
277 /**
278 Print MAC address.
279
280 @param[in] Node The pointer of MAC address buffer.
281 @param[in] Size The size of MAC address buffer.
282
283 **/
284 VOID
285 IfConfigPrintMacAddr (
286 IN UINT8 *Node,
287 IN UINT32 Size
288 )
289 {
290 UINTN Index;
291
292 ASSERT (Size <= MACADDRMAXSIZE);
293
294 for (Index = 0; Index < Size; Index++) {
295 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_BODY), gShellNetwork1HiiHandle, Node[Index]);
296 if (Index + 1 < Size) {
297 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_COLON), gShellNetwork1HiiHandle);
298 }
299 }
300
301 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE), gShellNetwork1HiiHandle);
302 }
303
304
305 /**
306 The get current status of all handles.
307
308 @param[in] IfName The pointer of IfName(interface name).
309 @param[in] IfList The pointer of IfList(interface list).
310
311 @retval EFI_SUCCESS The get status processed successfully.
312 @retval others The get status process failed.
313
314 **/
315 EFI_STATUS
316 IfConfigGetInterfaceInfo (
317 IN CHAR16 *IfName,
318 IN LIST_ENTRY *IfList
319 )
320 {
321 EFI_STATUS Status;
322 UINTN HandleIndex;
323 UINTN HandleNum;
324 EFI_HANDLE *HandleBuffer;
325 EFI_IP4_CONFIG2_PROTOCOL *Ip4Cfg2;
326 EFI_IP4_CONFIG2_INTERFACE_INFO *IfInfo;
327 IFCONFIG_INTERFACE_CB *IfCb;
328 UINTN DataSize;
329
330 HandleBuffer = NULL;
331 HandleNum = 0;
332
333 IfInfo = NULL;
334 IfCb = NULL;
335
336 //
337 // Locate all the handles with ip4 service binding protocol.
338 //
339 Status = gBS->LocateHandleBuffer (
340 ByProtocol,
341 &gEfiIp4ServiceBindingProtocolGuid,
342 NULL,
343 &HandleNum,
344 &HandleBuffer
345 );
346 if (EFI_ERROR (Status) || (HandleNum == 0)) {
347 return EFI_ABORTED;
348 }
349
350 //
351 // Enumerate all handles that installed with ip4 service binding protocol.
352 //
353 for (HandleIndex = 0; HandleIndex < HandleNum; HandleIndex++) {
354 IfCb = NULL;
355 IfInfo = NULL;
356 DataSize = 0;
357
358 //
359 // Ip4config protocol and ip4 service binding protocol are installed
360 // on the same handle.
361 //
362 ASSERT (HandleBuffer != NULL);
363 Status = gBS->HandleProtocol (
364 HandleBuffer[HandleIndex],
365 &gEfiIp4Config2ProtocolGuid,
366 (VOID **) &Ip4Cfg2
367 );
368
369 if (EFI_ERROR (Status)) {
370 goto ON_ERROR;
371 }
372
373 //
374 // Get the interface information size.
375 //
376 Status = Ip4Cfg2->GetData (
377 Ip4Cfg2,
378 Ip4Config2DataTypeInterfaceInfo,
379 &DataSize,
380 NULL
381 );
382
383 if (Status != EFI_BUFFER_TOO_SMALL) {
384 goto ON_ERROR;
385 }
386
387 IfInfo = AllocateZeroPool (DataSize);
388
389 if (IfInfo == NULL) {
390 Status = EFI_OUT_OF_RESOURCES;
391 goto ON_ERROR;
392 }
393
394 //
395 // Get the interface info.
396 //
397 Status = Ip4Cfg2->GetData (
398 Ip4Cfg2,
399 Ip4Config2DataTypeInterfaceInfo,
400 &DataSize,
401 IfInfo
402 );
403
404 if (EFI_ERROR (Status)) {
405 goto ON_ERROR;
406 }
407
408 //
409 // Check the interface name if required.
410 //
411 if ((IfName != NULL) && (StrCmp (IfName, IfInfo->Name) != 0)) {
412 FreePool (IfInfo);
413 continue;
414 }
415
416 DataSize = 0;
417
418 //
419 // Get the size of dns server list.
420 //
421 Status = Ip4Cfg2->GetData (
422 Ip4Cfg2,
423 Ip4Config2DataTypeDnsServer,
424 &DataSize,
425 NULL
426 );
427
428 if ((Status != EFI_BUFFER_TOO_SMALL) && (Status != EFI_NOT_FOUND)) {
429 goto ON_ERROR;
430 }
431
432 IfCb = AllocateZeroPool (sizeof (IFCONFIG_INTERFACE_CB) + DataSize);
433
434 if (IfCb == NULL) {
435 Status = EFI_OUT_OF_RESOURCES;
436 goto ON_ERROR;
437 }
438
439 IfCb->NicHandle = HandleBuffer[HandleIndex];
440 IfCb->IfInfo = IfInfo;
441 IfCb->IfCfg = Ip4Cfg2;
442 IfCb->DnsCnt = (UINT32) (DataSize / sizeof (EFI_IPv4_ADDRESS));
443
444 //
445 // Get the dns server list if has.
446 //
447 if (DataSize > 0) {
448 Status = Ip4Cfg2->GetData (
449 Ip4Cfg2,
450 Ip4Config2DataTypeDnsServer,
451 &DataSize,
452 IfCb->DnsAddr
453 );
454
455 if (EFI_ERROR (Status)) {
456 goto ON_ERROR;
457 }
458 }
459
460 //
461 // Get the config policy.
462 //
463 DataSize = sizeof (EFI_IP4_CONFIG2_POLICY);
464 Status = Ip4Cfg2->GetData (
465 Ip4Cfg2,
466 Ip4Config2DataTypePolicy,
467 &DataSize,
468 &IfCb->Policy
469 );
470
471 if (EFI_ERROR (Status)) {
472 goto ON_ERROR;
473 }
474
475 InsertTailList (IfList, &IfCb->Link);
476
477 if ((IfName != NULL) && (StrCmp (IfName, IfInfo->Name) == 0)) {
478 //
479 // Only need the appointed interface, keep the allocated buffer.
480 //
481 IfCb = NULL;
482 IfInfo = NULL;
483 break;
484 }
485 }
486
487 if (HandleBuffer != NULL) {
488 FreePool (HandleBuffer);
489 }
490
491 return EFI_SUCCESS;
492
493 ON_ERROR:
494
495 if (IfInfo != NULL) {
496 FreePool (IfInfo);
497 }
498
499 if (IfCb != NULL) {
500 FreePool (IfCb);
501 }
502
503 return Status;
504 }
505
506 /**
507 The list process of the ifconfig command.
508
509 @param[in] IfList The pointer of IfList(interface list).
510
511 @retval EFI_SUCCESS The ifconfig command list processed successfully.
512 @retval others The ifconfig command list process failed.
513
514 **/
515 EFI_STATUS
516 IfConfigShowInterfaceInfo (
517 IN LIST_ENTRY *IfList
518 )
519 {
520 LIST_ENTRY *Entry;
521 LIST_ENTRY *Next;
522 IFCONFIG_INTERFACE_CB *IfCb;
523 EFI_IPv4_ADDRESS Gateway;
524 UINT32 Index;
525
526 if (IsListEmpty (IfList)) {
527 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE), gShellNetwork1HiiHandle);
528 }
529
530 //
531 // Go through the interface list.
532 //
533 NET_LIST_FOR_EACH_SAFE (Entry, Next, IfList) {
534 IfCb = NET_LIST_USER_STRUCT (Entry, IFCONFIG_INTERFACE_CB, Link);
535
536 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK), gShellNetwork1HiiHandle);
537
538 //
539 // Print interface name.
540 //
541 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_IF_NAME), gShellNetwork1HiiHandle, IfCb->IfInfo->Name);
542
543 //
544 // Print interface config policy.
545 //
546 if (IfCb->Policy == Ip4Config2PolicyDhcp) {
547 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_DHCP), gShellNetwork1HiiHandle);
548 } else {
549 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_POLICY_MAN), gShellNetwork1HiiHandle);
550 }
551
552 //
553 // Print mac address of the interface.
554 //
555 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_MAC_ADDR_HEAD), gShellNetwork1HiiHandle);
556
557 IfConfigPrintMacAddr (
558 IfCb->IfInfo->HwAddress.Addr,
559 IfCb->IfInfo->HwAddressSize
560 );
561
562 //
563 // Print IPv4 address list of the interface.
564 //
565 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_HEAD), gShellNetwork1HiiHandle);
566
567 ShellPrintHiiEx(
568 -1,
569 -1,
570 NULL,
571 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY),
572 gShellNetwork1HiiHandle,
573 (UINTN)IfCb->IfInfo->StationAddress.Addr[0],
574 (UINTN)IfCb->IfInfo->StationAddress.Addr[1],
575 (UINTN)IfCb->IfInfo->StationAddress.Addr[2],
576 (UINTN)IfCb->IfInfo->StationAddress.Addr[3]
577 );
578
579 //
580 // Print subnet mask list of the interface.
581 //
582 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_SUBNET_MASK_HEAD), gShellNetwork1HiiHandle);
583
584 ShellPrintHiiEx(
585 -1,
586 -1,
587 NULL,
588 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY),
589 gShellNetwork1HiiHandle,
590 (UINTN)IfCb->IfInfo->SubnetMask.Addr[0],
591 (UINTN)IfCb->IfInfo->SubnetMask.Addr[1],
592 (UINTN)IfCb->IfInfo->SubnetMask.Addr[2],
593 (UINTN)IfCb->IfInfo->SubnetMask.Addr[3]
594 );
595
596 //
597 // Print default gateway of the interface.
598 //
599 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_GATEWAY_HEAD), gShellNetwork1HiiHandle);
600
601 ZeroMem (&Gateway, sizeof (EFI_IPv4_ADDRESS));
602
603 for (Index = 0; Index < IfCb->IfInfo->RouteTableSize; Index++) {
604 if ((CompareMem (&IfCb->IfInfo->RouteTable[Index].SubnetAddress, &mZeroIp4Addr, sizeof (EFI_IPv4_ADDRESS)) == 0) &&
605 (CompareMem (&IfCb->IfInfo->RouteTable[Index].SubnetMask , &mZeroIp4Addr, sizeof (EFI_IPv4_ADDRESS)) == 0) ){
606 CopyMem (&Gateway, &IfCb->IfInfo->RouteTable[Index].GatewayAddress, sizeof (EFI_IPv4_ADDRESS));
607 }
608 }
609
610 ShellPrintHiiEx(
611 -1,
612 -1,
613 NULL,
614 STRING_TOKEN (STR_IFCONFIG_INFO_IP_ADDR_BODY),
615 gShellNetwork1HiiHandle,
616 (UINTN)Gateway.Addr[0],
617 (UINTN)Gateway.Addr[1],
618 (UINTN)Gateway.Addr[2],
619 (UINTN)Gateway.Addr[3]
620 );
621
622 //
623 // Print route table entry.
624 //
625 ShellPrintHiiEx(-1, -1, NULL,STRING_TOKEN (STR_IFCONFIG_ROUTES_SIZE), gShellNetwork1HiiHandle, IfCb->IfInfo->RouteTableSize);
626
627 for (Index = 0; Index < IfCb->IfInfo->RouteTableSize; Index++) {
628 ShellPrintHiiEx(-1, -1, NULL,STRING_TOKEN (STR_IFCONFIG_ROUTES_ENTRY_INDEX), gShellNetwork1HiiHandle, Index);
629
630 ShellPrintHiiEx(
631 -1,
632 -1,
633 NULL,
634 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR),
635 gShellNetwork1HiiHandle,
636 L"Subnet ",
637 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetAddress.Addr[0],
638 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetAddress.Addr[1],
639 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetAddress.Addr[2],
640 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetAddress.Addr[3]
641 );
642
643 ShellPrintHiiEx(
644 -1,
645 -1,
646 NULL,
647 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR),
648 gShellNetwork1HiiHandle,
649 L"Netmask",
650 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetMask.Addr[0],
651 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetMask.Addr[1],
652 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetMask.Addr[2],
653 (UINTN)IfCb->IfInfo->RouteTable[Index].SubnetMask.Addr[3]
654 );
655
656 ShellPrintHiiEx(
657 -1,
658 -1,
659 NULL,
660 STRING_TOKEN (STR_IFCONFIG_SHOW_IP_ADDR),
661 gShellNetwork1HiiHandle,
662 L"Gateway",
663 (UINTN)IfCb->IfInfo->RouteTable[Index].GatewayAddress.Addr[0],
664 (UINTN)IfCb->IfInfo->RouteTable[Index].GatewayAddress.Addr[1],
665 (UINTN)IfCb->IfInfo->RouteTable[Index].GatewayAddress.Addr[2],
666 (UINTN)IfCb->IfInfo->RouteTable[Index].GatewayAddress.Addr[3]
667 );
668 }
669
670 //
671 // Print dns server addresses list of the interface if has.
672 //
673 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_HEAD), gShellNetwork1HiiHandle);
674
675 for (Index = 0; Index < IfCb->DnsCnt; Index++) {
676 ShellPrintHiiEx(
677 -1,
678 -1,
679 NULL,
680 STRING_TOKEN (STR_IFCONFIG_INFO_DNS_ADDR_BODY),
681 gShellNetwork1HiiHandle,
682 (UINTN) IfCb->DnsAddr[Index].Addr[0],
683 (UINTN) IfCb->DnsAddr[Index].Addr[1],
684 (UINTN) IfCb->DnsAddr[Index].Addr[2],
685 (UINTN) IfCb->DnsAddr[Index].Addr[3]
686 );
687
688 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_NEWLINE), gShellNetwork1HiiHandle);
689 }
690 }
691
692 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INFO_BREAK), gShellNetwork1HiiHandle);
693
694 return EFI_SUCCESS;
695 }
696
697 /**
698 The clean process of the ifconfig command to clear interface info.
699
700 @param[in] IfList The pointer of IfList(interface list).
701
702 @retval EFI_SUCCESS The ifconfig command clean processed successfully.
703 @retval others The ifconfig command clean process failed.
704
705 **/
706 EFI_STATUS
707 IfConfigClearInterfaceInfo (
708 IN LIST_ENTRY *IfList
709 )
710 {
711 EFI_STATUS Status;
712 LIST_ENTRY *Entry;
713 LIST_ENTRY *Next;
714 IFCONFIG_INTERFACE_CB *IfCb;
715 EFI_IP4_CONFIG2_POLICY Policy;
716
717 Policy = Ip4Config2PolicyDhcp;
718 Status = EFI_SUCCESS;
719
720 if (IsListEmpty (IfList)) {
721 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE), gShellNetwork1HiiHandle);
722 }
723
724 //
725 // Go through the interface list.
726 //
727 NET_LIST_FOR_EACH_SAFE (Entry, Next, IfList) {
728 IfCb = NET_LIST_USER_STRUCT (Entry, IFCONFIG_INTERFACE_CB, Link);
729
730 Status = IfCb->IfCfg->SetData (
731 IfCb->IfCfg,
732 Ip4Config2DataTypePolicy,
733 sizeof (EFI_IP4_CONFIG2_POLICY),
734 &Policy
735 );
736
737 if (EFI_ERROR (Status)) {
738 break;
739 }
740 }
741
742 return Status;
743 }
744
745 /**
746 The set process of the ifconfig command.
747
748 @param[in] IfList The pointer of IfList(interface list).
749 @param[in] VarArg The pointer of ARG_LIST(Args with "-s" option).
750
751 @retval EFI_SUCCESS The ifconfig command set processed successfully.
752 @retval others The ifconfig command set process failed.
753
754 **/
755 EFI_STATUS
756 IfConfigSetInterfaceInfo (
757 IN LIST_ENTRY *IfList,
758 IN ARG_LIST *VarArg
759 )
760 {
761
762 EFI_STATUS Status;
763 IFCONFIG_INTERFACE_CB *IfCb;
764 VAR_CHECK_CODE CheckCode;
765 EFI_EVENT TimeOutEvt;
766 EFI_EVENT MappedEvt;
767 BOOLEAN IsAddressOk;
768
769 EFI_IP4_CONFIG2_POLICY Policy;
770 EFI_IP4_CONFIG2_MANUAL_ADDRESS ManualAddress;
771 UINTN DataSize;
772 EFI_IPv4_ADDRESS Gateway;
773 EFI_IPv4_ADDRESS *Dns;
774 ARG_LIST *Tmp;
775 UINTN Index;
776
777 CONST CHAR16* TempString;
778
779 Dns = NULL;
780
781 if (IsListEmpty (IfList)) {
782 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_INVALID_INTERFACE), gShellNetwork1HiiHandle);
783 return EFI_INVALID_PARAMETER;
784 }
785
786 //
787 // Make sure to set only one interface each time.
788 //
789 IfCb = NET_LIST_USER_STRUCT (IfList->ForwardLink, IFCONFIG_INTERFACE_CB, Link);
790 Status = EFI_SUCCESS;
791
792 //
793 // Initialize check list mechanism.
794 //
795 CheckCode = IfConfigRetriveCheckListByName(
796 NULL,
797 NULL,
798 TRUE
799 );
800
801 //
802 // Create events & timers for asynchronous settings.
803 //
804 Status = gBS->CreateEvent (
805 EVT_TIMER,
806 TPL_CALLBACK,
807 NULL,
808 NULL,
809 &TimeOutEvt
810 );
811 if (EFI_ERROR (Status)) {
812 goto ON_EXIT;
813 }
814
815 Status = gBS->CreateEvent (
816 EVT_NOTIFY_SIGNAL,
817 TPL_NOTIFY,
818 IfConfigManualAddressNotify,
819 &IsAddressOk,
820 &MappedEvt
821 );
822 if (EFI_ERROR (Status)) {
823 goto ON_EXIT;
824 }
825
826 //
827 // Parse the setting variables.
828 //
829 while (VarArg != NULL) {
830 //
831 // Check invalid parameters (duplication & unknown & conflict).
832 //
833 CheckCode = IfConfigRetriveCheckListByName(
834 mSetCheckList,
835 VarArg->Arg,
836 FALSE
837 );
838
839 if (VarCheckOk != CheckCode) {
840 switch (CheckCode) {
841 case VarCheckDuplicate:
842 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_DUPLICATE_COMMAND), gShellNetwork1HiiHandle, VarArg->Arg);
843 break;
844
845 case VarCheckConflict:
846 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_CONFLICT_COMMAND), gShellNetwork1HiiHandle, VarArg->Arg);
847 break;
848
849 case VarCheckUnknown:
850 //
851 // To handle unsupported option.
852 //
853 TempString = PermanentString;
854 if (StringNoCaseCompare(&VarArg->Arg, &TempString) == 0) {
855 ShellPrintHiiEx(-1, -1, NULL,STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION), gShellNetwork1HiiHandle, PermanentString);
856 goto ON_EXIT;
857 }
858
859 //
860 // To handle unknown option.
861 //
862 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_UNKNOWN_COMMAND), gShellNetwork1HiiHandle, VarArg->Arg);
863 break;
864
865 default:
866 break;
867 }
868
869 VarArg = VarArg->Next;
870 continue;
871 }
872
873 //
874 // Process valid variables.
875 //
876 if (StrCmp(VarArg->Arg, L"dhcp") == 0) {
877 //
878 // Set dhcp config policy
879 //
880 Policy = Ip4Config2PolicyDhcp;
881 Status = IfCb->IfCfg->SetData (
882 IfCb->IfCfg,
883 Ip4Config2DataTypePolicy,
884 sizeof (EFI_IP4_CONFIG2_POLICY),
885 &Policy
886 );
887
888 if (EFI_ERROR(Status)) {
889 goto ON_EXIT;
890 }
891
892 VarArg= VarArg->Next;
893
894 } else if (StrCmp (VarArg->Arg, L"static") == 0) {
895 //
896 // Set manual config policy.
897 //
898 Policy = Ip4Config2PolicyStatic;
899 Status = IfCb->IfCfg->SetData (
900 IfCb->IfCfg,
901 Ip4Config2DataTypePolicy,
902 sizeof (EFI_IP4_CONFIG2_POLICY),
903 &Policy
904 );
905
906 if (EFI_ERROR(Status)) {
907 goto ON_EXIT;
908 }
909
910 VarArg= VarArg->Next;
911
912 ZeroMem (&ManualAddress, sizeof (ManualAddress));
913
914 //
915 // Get manual IP address.
916 //
917 Status = NetLibStrToIp4 (VarArg->Arg, &ManualAddress.Address);
918 if (EFI_ERROR(Status)) {
919 goto ON_EXIT;
920 }
921
922 //
923 // Get subnetmask.
924 //
925 VarArg = VarArg->Next;
926 Status = NetLibStrToIp4 (VarArg->Arg, &ManualAddress.SubnetMask);
927 if (EFI_ERROR(Status)) {
928 goto ON_EXIT;
929 }
930
931 //
932 // Get gateway.
933 //
934 VarArg = VarArg->Next;
935 Status = NetLibStrToIp4 (VarArg->Arg, &Gateway);
936 if (EFI_ERROR(Status)) {
937 goto ON_EXIT;
938 }
939
940 IsAddressOk = FALSE;
941
942 Status = IfCb->IfCfg->RegisterDataNotify (
943 IfCb->IfCfg,
944 Ip4Config2DataTypeManualAddress,
945 MappedEvt
946 );
947 if (EFI_ERROR (Status)) {
948 goto ON_EXIT;
949 }
950
951 DataSize = sizeof (EFI_IP4_CONFIG2_MANUAL_ADDRESS);
952
953 Status = IfCb->IfCfg->SetData (
954 IfCb->IfCfg,
955 Ip4Config2DataTypeManualAddress,
956 DataSize,
957 &ManualAddress
958 );
959
960 if (Status == EFI_NOT_READY) {
961 gBS->SetTimer (TimeOutEvt, TimerRelative, 50000000);
962
963 while (EFI_ERROR (gBS->CheckEvent (TimeOutEvt))) {
964 if (IsAddressOk) {
965 Status = EFI_SUCCESS;
966 break;
967 }
968 }
969 }
970
971 IfCb->IfCfg->UnregisterDataNotify (
972 IfCb->IfCfg,
973 Ip4Config2DataTypeManualAddress,
974 MappedEvt
975 );
976
977 if (EFI_ERROR (Status)) {
978 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_SET_ADDR_FAILED), gShellNetwork1HiiHandle, Status);
979 goto ON_EXIT;
980 }
981
982 //
983 // Set gateway.
984 //
985 DataSize = sizeof (EFI_IPv4_ADDRESS);
986
987 Status = IfCb->IfCfg->SetData (
988 IfCb->IfCfg,
989 Ip4Config2DataTypeGateway,
990 DataSize,
991 &Gateway
992 );
993 VarArg = VarArg->Next;
994
995 } else if (StrCmp (VarArg->Arg, L"dns") == 0) {
996 //
997 // Get DNS addresses.
998 //
999 VarArg = VarArg->Next;
1000 Tmp = VarArg;
1001 Index = 0;
1002 while (Tmp != NULL) {
1003 Index ++;
1004 Tmp = Tmp->Next;
1005 }
1006
1007 Dns = AllocatePool (Index * sizeof (EFI_IPv4_ADDRESS));
1008 ASSERT(Dns != NULL);
1009 Tmp = VarArg;
1010 Index = 0;
1011 while (Tmp != NULL) {
1012 Status = NetLibStrToIp4 (Tmp->Arg, Dns + Index);
1013 if (EFI_ERROR(Status)) {
1014 goto ON_EXIT;
1015 }
1016 Index ++;
1017 Tmp = Tmp->Next;
1018 }
1019
1020 VarArg = Tmp;
1021
1022 //
1023 // Set DNS addresses.
1024 //
1025 DataSize = Index * sizeof (EFI_IPv4_ADDRESS);
1026
1027 Status = IfCb->IfCfg->SetData (
1028 IfCb->IfCfg,
1029 Ip4Config2DataTypeDnsServer,
1030 DataSize,
1031 Dns
1032 );
1033 }
1034 }
1035
1036 ON_EXIT:
1037 if (Dns != NULL) {
1038 FreePool (Dns);
1039 }
1040
1041 return EFI_SUCCESS;
1042
1043 }
1044
1045 /**
1046 The ifconfig command main process.
1047
1048 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1049
1050 @retval EFI_SUCCESS ifconfig command processed successfully.
1051 @retval others The ifconfig command process failed.
1052
1053 **/
1054 EFI_STATUS
1055 IfConfig (
1056 IN IFCONFIG_PRIVATE_DATA *Private
1057 )
1058 {
1059 EFI_STATUS Status;
1060
1061 //
1062 // Get configure information of all interfaces.
1063 //
1064 Status = IfConfigGetInterfaceInfo (
1065 Private->IfName,
1066 &Private->IfList
1067 );
1068
1069 if (EFI_ERROR (Status)) {
1070 goto ON_EXIT;
1071 }
1072
1073 switch (Private->OpCode) {
1074 case IfConfigOpList:
1075 Status = IfConfigShowInterfaceInfo (&Private->IfList);
1076 break;
1077
1078 case IfConfigOpClear:
1079 Status = IfConfigClearInterfaceInfo (&Private->IfList);
1080 break;
1081
1082 case IfConfigOpSet:
1083 Status = IfConfigSetInterfaceInfo (&Private->IfList, Private->VarArg);
1084 break;
1085
1086 default:
1087 Status = EFI_ABORTED;
1088 }
1089
1090 ON_EXIT:
1091
1092 return Status;
1093 }
1094
1095 /**
1096 The ifconfig command cleanup process, free the allocated memory.
1097
1098 @param[in] Private The pointer of IFCONFIG_PRIVATE_DATA.
1099
1100 **/
1101 VOID
1102 IfConfigCleanup (
1103 IN IFCONFIG_PRIVATE_DATA *Private
1104 )
1105 {
1106 LIST_ENTRY *Entry;
1107 LIST_ENTRY *NextEntry;
1108 IFCONFIG_INTERFACE_CB *IfCb;
1109 ARG_LIST *ArgNode;
1110 ARG_LIST *ArgHead;
1111
1112 ASSERT (Private != NULL);
1113
1114 //
1115 // Clean the list which save the set config Args.
1116 //
1117 if (Private->VarArg != NULL) {
1118 ArgHead = Private->VarArg;
1119
1120 while (ArgHead->Next != NULL) {
1121 ArgNode = ArgHead->Next;
1122 FreePool (ArgHead);
1123 ArgHead = ArgNode;
1124 }
1125
1126 FreePool (ArgHead);
1127 }
1128
1129 if (Private->IfName != NULL) {
1130 FreePool (Private->IfName);
1131 }
1132
1133 //
1134 // Clean the IFCONFIG_INTERFACE_CB list.
1135 //
1136 NET_LIST_FOR_EACH_SAFE (Entry, NextEntry, &Private->IfList) {
1137 IfCb = NET_LIST_USER_STRUCT (Entry, IFCONFIG_INTERFACE_CB, Link);
1138
1139 RemoveEntryList (&IfCb->Link);
1140
1141 if (IfCb->IfInfo != NULL) {
1142
1143 FreePool (IfCb->IfInfo);
1144 }
1145
1146 FreePool (IfCb);
1147 }
1148
1149 FreePool (Private);
1150 }
1151
1152 /**
1153 Function for 'ifconfig' command.
1154
1155 @param[in] ImageHandle Handle to the Image (NULL if Internal).
1156 @param[in] SystemTable Pointer to the System Table (NULL if Internal).
1157
1158 @retval EFI_SUCCESS ifconfig command processed successfully.
1159 @retval others The ifconfig command process failed.
1160
1161 **/
1162 SHELL_STATUS
1163 EFIAPI
1164 ShellCommandRunIfconfig (
1165 IN EFI_HANDLE ImageHandle,
1166 IN EFI_SYSTEM_TABLE *SystemTable
1167 )
1168 {
1169 EFI_STATUS Status;
1170 IFCONFIG_PRIVATE_DATA *Private;
1171 LIST_ENTRY *ParamPackage;
1172 CONST CHAR16 *ValueStr;
1173 ARG_LIST *ArgList;
1174 CHAR16 *ProblemParam;
1175 CHAR16 *Str;
1176
1177 Private = NULL;
1178
1179 Status = ShellCommandLineParseEx (mIfConfigCheckList, &ParamPackage, &ProblemParam, TRUE, FALSE);
1180 if (EFI_ERROR (Status)) {
1181 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_INV), gShellNetwork1HiiHandle, L"ifconfig", ProblemParam);
1182 goto ON_EXIT;
1183 }
1184
1185 //
1186 // To handle unsupported option.
1187 //
1188 if (ShellCommandLineGetFlag (ParamPackage, L"-c")) {
1189 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_UNSUPPORTED_OPTION), gShellNetwork1HiiHandle,L"-c");
1190 goto ON_EXIT;
1191 }
1192
1193 //
1194 // To handle no option.
1195 //
1196 if (!ShellCommandLineGetFlag (ParamPackage, L"-r") && !ShellCommandLineGetFlag (ParamPackage, L"-s") &&
1197 !ShellCommandLineGetFlag (ParamPackage, L"-l")) {
1198 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_LACK_OPTION), gShellNetwork1HiiHandle);
1199 goto ON_EXIT;
1200 }
1201
1202 //
1203 // To handle conflict options.
1204 //
1205 if (((ShellCommandLineGetFlag (ParamPackage, L"-r")) && (ShellCommandLineGetFlag (ParamPackage, L"-s"))) ||
1206 ((ShellCommandLineGetFlag (ParamPackage, L"-r")) && (ShellCommandLineGetFlag (ParamPackage, L"-l"))) ||
1207 ((ShellCommandLineGetFlag (ParamPackage, L"-s")) && (ShellCommandLineGetFlag (ParamPackage, L"-l")))) {
1208 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_GEN_PARAM_CON), gShellNetwork1HiiHandle, L"ifconfig");
1209 goto ON_EXIT;
1210 }
1211
1212 Status = EFI_INVALID_PARAMETER;
1213
1214 Private = AllocateZeroPool (sizeof (IFCONFIG_PRIVATE_DATA));
1215
1216 if (Private == NULL) {
1217 Status = EFI_OUT_OF_RESOURCES;
1218 goto ON_EXIT;
1219 }
1220
1221 InitializeListHead (&Private->IfList);
1222
1223 //
1224 // To get interface name for the list option.
1225 //
1226 if (ShellCommandLineGetFlag (ParamPackage, L"-l")) {
1227 Private->OpCode = IfConfigOpList;
1228 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-l");
1229 if (ValueStr != NULL) {
1230 Str = AllocateCopyPool (StrSize (ValueStr), ValueStr);
1231 ASSERT (Str != NULL);
1232 Private->IfName = Str;
1233 }
1234 }
1235
1236 //
1237 // To get interface name for the clear option.
1238 //
1239 if (ShellCommandLineGetFlag (ParamPackage, L"-r")) {
1240 Private->OpCode = IfConfigOpClear;
1241 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-r");
1242 if (ValueStr != NULL) {
1243 Str = AllocateCopyPool (StrSize (ValueStr), ValueStr);
1244 ASSERT (Str != NULL);
1245 Private->IfName = Str;
1246 }
1247 }
1248
1249 //
1250 // To get interface name and corresponding Args for the set option.
1251 //
1252 if (ShellCommandLineGetFlag (ParamPackage, L"-s")) {
1253 ValueStr = ShellCommandLineGetValue (ParamPackage, L"-s");
1254 if (ValueStr == NULL) {
1255 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_LACK_INTERFACE), gShellNetwork1HiiHandle);
1256 goto ON_EXIT;
1257 }
1258
1259 //
1260 // To split the configuration into multi-section.
1261 //
1262 ArgList = SplitStrToList (ValueStr, L' ');
1263 ASSERT (ArgList != NULL);
1264
1265 Private->OpCode = IfConfigOpSet;
1266 Private->IfName = ArgList->Arg;
1267
1268 Private->VarArg = ArgList->Next;
1269
1270 if (Private->IfName == NULL || Private->VarArg == NULL) {
1271 ShellPrintHiiEx (-1, -1, NULL, STRING_TOKEN (STR_IFCONFIG_LACK_COMMAND), gShellNetwork1HiiHandle);
1272 goto ON_EXIT;
1273 }
1274 }
1275
1276 //
1277 // Main process of ifconfig.
1278 //
1279 Status = IfConfig (Private);
1280
1281 ON_EXIT:
1282
1283 ShellCommandLineFreeVarList (ParamPackage);
1284
1285 if (Private != NULL) {
1286 IfConfigCleanup (Private);
1287 }
1288
1289 return Status;
1290 }