summaryrefslogtreecommitdiffstats
path: root/MdeModulePkg/Universal/Network/Ip4ConfigDxe/NicIp4Variable.c
blob: 696f363a6272e4539e43b94aae8c460404e0b0a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/** @file
  Routines used to operate the Ip4 configure variable.

Copyright (c) 2006 - 2010, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution.  The full text of the license may be found at<BR>
http://opensource.org/licenses/bsd-license.php

THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.

**/

#include "Ip4Config.h"
#include "NicIp4Variable.h"

BOOLEAN  mIp4ConfigVariableReclaimed = FALSE;

/**
  Check whether the configure parameter is valid.

  @param  NicConfig    The configure parameter to check

  @return TRUE if the parameter is valid for the interface, otherwise FALSE.

**/
BOOLEAN
Ip4ConfigIsValid (
  IN NIC_IP4_CONFIG_INFO    *NicConfig
  )
{
  EFI_IP4_IPCONFIG_DATA     *IpConfig;
  IP4_ADDR                  Station;
  IP4_ADDR                  Netmask;
  IP4_ADDR                  Gateway;
  UINT32                    Index;

  IpConfig = &NicConfig->Ip4Info;

  if (NicConfig->Source == IP4_CONFIG_SOURCE_STATIC) {
    //
    // Validate that the addresses are unicast and mask
    // is properly formated
    //
    Station = EFI_NTOHL (IpConfig->StationAddress);
    Netmask = EFI_NTOHL (IpConfig->SubnetMask);

    if ((Netmask == 0) || !IP4_IS_VALID_NETMASK (Netmask) ||
        (Station == 0) || !Ip4IsUnicast (Station, Netmask)) {
      return FALSE;
    }

    //
    // Validate that the next hops are on the connected network
    // or that is a direct route (Gateway == 0).
    //
    for (Index = 0; Index < IpConfig->RouteTableSize; Index++) {
      Gateway = EFI_NTOHL (IpConfig->RouteTable[Index].GatewayAddress);

      if ((Gateway != 0) && (!IP4_NET_EQUAL (Station, Gateway, Netmask) ||
          !Ip4IsUnicast (Gateway, Netmask))) {
        return FALSE;
      }
    }

    return TRUE;
  }

  //
  // return false if it is an unkown configure source. Valid
  // sources are static and dhcp.
  //
  return (BOOLEAN) (NicConfig->Source == IP4_CONFIG_SOURCE_DHCP);
}



/**
  Read the ip4 configure variable from the EFI variable.

  @param  Instance     The IP4 CONFIG instance.

  @return The IP4 configure read if it is there and is valid, otherwise NULL.

**/
NIC_IP4_CONFIG_INFO *
Ip4ConfigReadVariable (
  IN  IP4_CONFIG_INSTANCE   *Instance
  )
{
  NIC_IP4_CONFIG_INFO *NicConfig;

  NicConfig = GetVariable (Instance->MacString, &gEfiNicIp4ConfigVariableGuid);
  if (NicConfig != NULL) {
    Ip4ConfigFixRouteTablePointer (&NicConfig->Ip4Info);
  }

  return NicConfig;
}

/**
  Write the IP4 configure variable to the NVRAM. If Config
  is NULL, remove the variable.

  @param  Instance     The IP4 CONFIG instance.
  @param  NicConfig    The IP4 configure data to write.

  @retval EFI_SUCCESS  The variable is written to the NVRam.
  @retval Others       Failed to write the variable.

**/
EFI_STATUS
Ip4ConfigWriteVariable (
  IN IP4_CONFIG_INSTANCE    *Instance,
  IN NIC_IP4_CONFIG_INFO    *NicConfig OPTIONAL
  )
{
  EFI_STATUS  Status;

  Status = gRT->SetVariable (
                  Instance->MacString,
                  &gEfiNicIp4ConfigVariableGuid,
                  IP4_CONFIG_VARIABLE_ATTRIBUTES,
                  (NicConfig == NULL) ? 0 : SIZEOF_NIC_IP4_CONFIG_INFO (NicConfig),
                  NicConfig
                  );

  return Status;
}

/**
  Check whether a NIC exist in the platform given its MAC address.

  @param  NicAddr      The MAC address for the NIC to be checked.

  @retval TRUE         The NIC exist in the platform.
  @retval FALSE        The NIC doesn't exist in the platform.

**/
BOOLEAN
Ip4ConfigIsNicExist (
  IN NIC_ADDR               *NicAddr
  )
{
  EFI_STATUS      Status;
  EFI_HANDLE      *HandleBuffer;
  UINTN           NumberOfHandles;
  UINTN           Index;
  BOOLEAN         Found;
  UINTN           AddrSize;
  EFI_MAC_ADDRESS MacAddr;

  //
  // Locate Service Binding handles.
  //
  Status = gBS->LocateHandleBuffer (
                 ByProtocol,
                 &gEfiManagedNetworkServiceBindingProtocolGuid,
                 NULL,
                 &NumberOfHandles,
                 &HandleBuffer
                 );
  if (EFI_ERROR (Status)) {
    return FALSE;
  }

  Found = FALSE;
  for (Index = 0; Index < NumberOfHandles; Index++) {
    //
    // Get MAC address.
    //
    AddrSize = 0;
    Status = NetLibGetMacAddress (HandleBuffer[Index], &MacAddr, &AddrSize);
    if (EFI_ERROR (Status)) {
      Found = FALSE;
      goto Exit;
    }

    if ((NicAddr->Len == AddrSize) && (CompareMem (NicAddr->MacAddr.Addr, MacAddr.Addr, AddrSize) == 0)) {
      Found = TRUE;
      goto Exit;
    }
  }

Exit:
  FreePool (HandleBuffer);
  return Found;
}

/**
  Reclaim Ip4Config Variables for NIC which has been removed from the platform.

**/
VOID
Ip4ConfigReclaimVariable (
  VOID
  )
{
  EFI_STATUS           Status;
  UINTN                VariableNameSize;
  CHAR16               *VariableName;
  CHAR16               *CurrentVariableName;
  EFI_GUID             VendorGuid;
  UINTN                VariableNameBufferSize;
  NIC_IP4_CONFIG_INFO  *NicConfig;

  //
  // Check whether we need perform reclaim.
  //
  if (mIp4ConfigVariableReclaimed) {
    return;
  }
  mIp4ConfigVariableReclaimed = TRUE;

  //
  // Get all Ip4Config Variable.
  //
  VariableNameSize = sizeof (CHAR16);
  VariableName = AllocateZeroPool (VariableNameSize);
  VariableNameBufferSize = VariableNameSize;

  while (TRUE) {
    Status = gRT->GetNextVariableName (
                    &VariableNameSize,
                    VariableName,
                    &VendorGuid
                    );

Check:
    if (Status == EFI_BUFFER_TOO_SMALL) {
      VariableName = ReallocatePool (VariableNameBufferSize, VariableNameSize, VariableName);
      VariableNameBufferSize = VariableNameSize;
      //
      // Try again using the new buffer.
      //
      Status = gRT->GetNextVariableName (
                      &VariableNameSize,
                      VariableName,
                      &VendorGuid
                      );
    }

    if (EFI_ERROR (Status)) {
      //
      // No more variable available, finish search.
      //
      break;
    }

    //
    // Check variable GUID.
    //
    if (!CompareGuid (&VendorGuid, &gEfiNicIp4ConfigVariableGuid)) {
      continue;
    }

    NicConfig = GetVariable (VariableName, &gEfiNicIp4ConfigVariableGuid);
    if (NicConfig == NULL) {
      break;
    }

    if (!Ip4ConfigIsNicExist (&NicConfig->NicAddr)) {
      //
      // No NIC found for this Ip4Config variable, remove it.
      // Since we are in loop of GetNextVariableName(), we need move on to next
      // Variable first and then delete current Variable.
      //
      CurrentVariableName = AllocateCopyPool (VariableNameSize, VariableName);
      Status = gRT->GetNextVariableName (
                      &VariableNameSize,
                      VariableName,
                      &VendorGuid
                      );

      gRT->SetVariable (
             CurrentVariableName,
             &gEfiNicIp4ConfigVariableGuid,
             IP4_CONFIG_VARIABLE_ATTRIBUTES,
             0,
             NULL
             );
      FreePool (CurrentVariableName);

      //
      // We already get next variable, go to check it.
      //
      goto Check;
    }
  }

  FreePool (VariableName);
}

/**
  Fix the RouteTable pointer in an EFI_IP4_IPCONFIG_DATA structure.

  The pointer is set to be immediately follow the ConfigData if there're entries
  in the RouteTable. Otherwise it is set to NULL.

  @param  ConfigData     The IP4 IP configure data.

**/
VOID
Ip4ConfigFixRouteTablePointer (
  IN OUT EFI_IP4_IPCONFIG_DATA  *ConfigData
  )
{
  //
  // The memory used for route table entries must immediately follow
  // the ConfigData and be not packed.
  //
  if (ConfigData->RouteTableSize > 0) {
    ConfigData->RouteTable = (EFI_IP4_ROUTE_TABLE *) (ConfigData + 1);
  } else {
    ConfigData->RouteTable = NULL;
  }
}