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