]> git.proxmox.com Git - mirror_edk2.git/blob - NetworkPkg/Ip4Dxe/Ip4Common.c
UefiCpuPkg: Move AsmRelocateApLoopStart from Mpfuncs.nasm to AmdSev.nasm
[mirror_edk2.git] / NetworkPkg / Ip4Dxe / Ip4Common.c
1 /** @file
2
3 Copyright (c) 2005 - 2017, Intel Corporation. All rights reserved.<BR>
4 SPDX-License-Identifier: BSD-2-Clause-Patent
5
6 **/
7
8 #include "Ip4Impl.h"
9
10 /**
11 Return the cast type (Unicast/Broadcast) specific to an
12 interface. All the addresses are host byte ordered.
13
14 @param[in] IpAddr The IP address to classify in host byte order
15 @param[in] IpIf The interface that IpAddr received from
16
17 @return The cast type of this IP address specific to the interface.
18 @retval IP4_LOCAL_HOST The IpAddr equals to the interface's address
19 @retval IP4_SUBNET_BROADCAST The IpAddr is a directed subnet broadcast to the
20 interface
21 @retval IP4_NET_BROADCAST The IpAddr is a network broadcast to the interface
22 @retval 0 Otherwise.
23
24 **/
25 INTN
26 Ip4GetNetCast (
27 IN IP4_ADDR IpAddr,
28 IN IP4_INTERFACE *IpIf
29 )
30 {
31 if (IpAddr == IpIf->Ip) {
32 return IP4_LOCAL_HOST;
33 } else if (IpAddr == IpIf->SubnetBrdcast) {
34 return IP4_SUBNET_BROADCAST;
35 } else if (IpAddr == IpIf->NetBrdcast) {
36 return IP4_NET_BROADCAST;
37 }
38
39 return 0;
40 }
41
42 /**
43 Find the cast type of the packet related to the local host.
44 This isn't the same as link layer cast type. For example, DHCP
45 server may send local broadcast to the local unicast MAC.
46
47 @param[in] IpSb The IP4 service binding instance that received the
48 packet
49 @param[in] Dst The destination address in the packet (host byte
50 order)
51 @param[in] Src The source address in the packet (host byte order)
52
53 @return The cast type for the Dst, it will return on the first non-promiscuous
54 cast type to a configured interface. If the packet doesn't match any of
55 the interface, multicast address and local broadcast address are checked.
56
57 **/
58 INTN
59 Ip4GetHostCast (
60 IN IP4_SERVICE *IpSb,
61 IN IP4_ADDR Dst,
62 IN IP4_ADDR Src
63 )
64 {
65 LIST_ENTRY *Entry;
66 IP4_INTERFACE *IpIf;
67 INTN Type;
68 INTN Class;
69
70 Type = 0;
71
72 if (IpSb->MnpConfigData.EnablePromiscuousReceive) {
73 Type = IP4_PROMISCUOUS;
74 }
75
76 //
77 // Go through the interface list of the IP service, most likely.
78 //
79 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
80 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
81
82 //
83 // Skip the unconfigured interface and invalid source address:
84 // source address can't be broadcast.
85 //
86 if (!IpIf->Configured || IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {
87 continue;
88 }
89
90 if ((Class = Ip4GetNetCast (Dst, IpIf)) > Type) {
91 return Class;
92 }
93 }
94
95 //
96 // If it is local broadcast address. The source address must
97 // be a unicast address on one of the direct connected network.
98 // If it is a multicast address, accept it only if we are in
99 // the group.
100 //
101 if (Dst == IP4_ALLONE_ADDRESS) {
102 IpIf = Ip4FindNet (IpSb, Src);
103
104 if ((IpIf != NULL) && !IP4_IS_BROADCAST (Ip4GetNetCast (Src, IpIf))) {
105 return IP4_LOCAL_BROADCAST;
106 }
107 } else if (IP4_IS_MULTICAST (Dst) && (Ip4FindGroup (&IpSb->IgmpCtrl, Dst) != NULL)) {
108 return IP4_MULTICAST;
109 }
110
111 return Type;
112 }
113
114 /**
115 Find an interface whose configured IP address is Ip.
116
117 @param[in] IpSb The IP4 service binding instance
118 @param[in] Ip The Ip address (host byte order) to find
119
120 @return The IP4_INTERFACE point if found, otherwise NULL
121
122 **/
123 IP4_INTERFACE *
124 Ip4FindInterface (
125 IN IP4_SERVICE *IpSb,
126 IN IP4_ADDR Ip
127 )
128 {
129 LIST_ENTRY *Entry;
130 IP4_INTERFACE *IpIf;
131
132 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
133 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
134
135 if (IpIf->Configured && (IpIf->Ip == Ip)) {
136 return IpIf;
137 }
138 }
139
140 return NULL;
141 }
142
143 /**
144 Find an interface that Ip is on that connected network.
145
146 @param[in] IpSb The IP4 service binding instance
147 @param[in] Ip The Ip address (host byte order) to find
148
149 @return The IP4_INTERFACE point if found, otherwise NULL
150
151 **/
152 IP4_INTERFACE *
153 Ip4FindNet (
154 IN IP4_SERVICE *IpSb,
155 IN IP4_ADDR Ip
156 )
157 {
158 LIST_ENTRY *Entry;
159 IP4_INTERFACE *IpIf;
160
161 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
162 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
163
164 if (IpIf->Configured && IP4_NET_EQUAL (Ip, IpIf->Ip, IpIf->SubnetMask)) {
165 return IpIf;
166 }
167 }
168
169 return NULL;
170 }
171
172 /**
173 Find an interface of the service with the same Ip/Netmask pair.
174
175 @param[in] IpSb Ip4 service binding instance
176 @param[in] Ip The Ip address to find (host byte order)
177 @param[in] Netmask The network to find (host byte order)
178
179 @return The IP4_INTERFACE point if found, otherwise NULL
180
181 **/
182 IP4_INTERFACE *
183 Ip4FindStationAddress (
184 IN IP4_SERVICE *IpSb,
185 IN IP4_ADDR Ip,
186 IN IP4_ADDR Netmask
187 )
188 {
189 LIST_ENTRY *Entry;
190 IP4_INTERFACE *IpIf;
191
192 NET_LIST_FOR_EACH (Entry, &IpSb->Interfaces) {
193 IpIf = NET_LIST_USER_STRUCT (Entry, IP4_INTERFACE, Link);
194
195 if (IpIf->Configured && (IpIf->Ip == Ip) && (IpIf->SubnetMask == Netmask)) {
196 return IpIf;
197 }
198 }
199
200 return NULL;
201 }
202
203 /**
204 Get the MAC address for a multicast IP address. Call
205 Mnp's McastIpToMac to find the MAC address in stead of
206 hard code the NIC to be Ethernet.
207
208 @param[in] Mnp The Mnp instance to get the MAC address.
209 @param[in] Multicast The multicast IP address to translate.
210 @param[out] Mac The buffer to hold the translated address.
211
212 @retval EFI_SUCCESS if the multicast IP is successfully translated to a
213 multicast MAC address.
214 @retval other Otherwise some error.
215
216 **/
217 EFI_STATUS
218 Ip4GetMulticastMac (
219 IN EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
220 IN IP4_ADDR Multicast,
221 OUT EFI_MAC_ADDRESS *Mac
222 )
223 {
224 EFI_IP_ADDRESS EfiIp;
225
226 EFI_IP4 (EfiIp.v4) = HTONL (Multicast);
227 return Mnp->McastIpToMac (Mnp, FALSE, &EfiIp, Mac);
228 }
229
230 /**
231 Convert the multibyte field in IP header's byter order.
232 In spite of its name, it can also be used to convert from
233 host to network byte order.
234
235 @param[in] Head The IP head to convert
236
237 @return Point to the converted IP head
238
239 **/
240 IP4_HEAD *
241 Ip4NtohHead (
242 IN IP4_HEAD *Head
243 )
244 {
245 Head->TotalLen = NTOHS (Head->TotalLen);
246 Head->Id = NTOHS (Head->Id);
247 Head->Fragment = NTOHS (Head->Fragment);
248 Head->Src = NTOHL (Head->Src);
249 Head->Dst = NTOHL (Head->Dst);
250
251 return Head;
252 }
253
254 /**
255 Validate that Ip/Netmask pair is OK to be used as station
256 address. Only continuous netmasks are supported. and check
257 that StationAddress is a unicast address on the network.
258
259 @param[in] Ip The IP address to validate.
260 @param[in] Netmask The netmask of the IP.
261
262 @retval TRUE The Ip/Netmask pair is valid.
263 @retval FALSE The Ip/Netmask pair is invalid.
264
265 **/
266 BOOLEAN
267 Ip4StationAddressValid (
268 IN IP4_ADDR Ip,
269 IN IP4_ADDR Netmask
270 )
271 {
272 //
273 // Only support the station address with 0.0.0.0/0 to enable DHCP client.
274 //
275 if (Netmask == IP4_ALLZERO_ADDRESS) {
276 return (BOOLEAN)(Ip == IP4_ALLZERO_ADDRESS);
277 }
278
279 //
280 // Only support the continuous net masks
281 //
282 if (NetGetMaskLength (Netmask) == (IP4_MASK_MAX + 1)) {
283 return FALSE;
284 }
285
286 //
287 // Station address can't be class D or class E address
288 //
289 if (NetGetIpClass (Ip) > IP4_ADDR_CLASSC) {
290 return FALSE;
291 }
292
293 return NetIp4IsUnicast (Ip, Netmask);
294 }