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