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