1 /** @file
2   Common definition for IP4.
3 
4 Copyright (c) 2005 - 2018, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
6 
7 **/
8 
9 #ifndef __EFI_IP4_COMMON_H__
10 #define __EFI_IP4_COMMON_H__
11 
12 typedef struct _IP4_INTERFACE  IP4_INTERFACE;
13 typedef struct _IP4_PROTOCOL   IP4_PROTOCOL;
14 typedef struct _IP4_SERVICE    IP4_SERVICE;
15 
16 #define IP4_ETHER_PROTO       0x0800
17 
18 //
19 // The packet is received as link level broadcast/multicast/promiscuous.
20 //
21 #define IP4_LINK_BROADCAST    0x00000001
22 #define IP4_LINK_MULTICAST    0x00000002
23 #define IP4_LINK_PROMISC      0x00000004
24 
25 //
26 // IP4 address cast type classfication. Keep it true that any
27 // type bigger than or equal to LOCAL_BROADCAST is broadcast.
28 //
29 #define IP4_PROMISCUOUS       1
30 #define IP4_LOCAL_HOST        2
31 #define IP4_MULTICAST         3
32 #define IP4_LOCAL_BROADCAST   4  // Destination is 255.255.255.255
33 #define IP4_SUBNET_BROADCAST  5
34 #define IP4_NET_BROADCAST     6
35 
36 //
37 // IP4 header flags
38 //
39 #define IP4_HEAD_DF_MASK      0x4000
40 #define IP4_HEAD_MF_MASK      0x2000
41 #define IP4_HEAD_OFFSET_MASK  0x1fff
42 
43 #define IP4_ALLZERO_ADDRESS   0x00000000u
44 #define IP4_ALLONE_ADDRESS    0xFFFFFFFFu
45 #define IP4_ALLSYSTEM_ADDRESS 0xE0000001u
46 #define IP4_ALLROUTER_ADDRESS 0xE0000002u
47 
48 ///
49 /// Compose the fragment field to be used in the IP4 header.
50 ///
51 #define IP4_HEAD_FRAGMENT_FIELD(Df, Mf, Offset) \
52     ((UINT16)(((Df) ? IP4_HEAD_DF_MASK : 0) | ((Mf) ? IP4_HEAD_MF_MASK : 0) | (((Offset) >> 3) & IP4_HEAD_OFFSET_MASK)))
53 
54 #define IP4_LAST_FRAGMENT(FragmentField)  \
55           (((FragmentField) & IP4_HEAD_MF_MASK) == 0)
56 
57 #define IP4_FIRST_FRAGMENT(FragmentField) \
58           ((BOOLEAN)(((FragmentField) & IP4_HEAD_OFFSET_MASK) == 0))
59 
60 #define IP4_DO_NOT_FRAGMENT(FragmentField) \
61           ((BOOLEAN)(((FragmentField) & IP4_HEAD_DF_MASK) == IP4_HEAD_DF_MASK))
62 
63 #define IP4_IS_BROADCAST(CastType) ((CastType) >= IP4_LOCAL_BROADCAST)
64 
65 ///
66 /// Conver the Microsecond to second. IP transmit/receive time is
67 /// in the unit of microsecond. IP ticks once per second.
68 ///
69 #define IP4_US_TO_SEC(Us) (((Us) + 999999) / 1000000)
70 
71 /**
72   Return the cast type (Unicast/Boradcast) specific to an
73   interface. All the addresses are host byte ordered.
74 
75   @param[in]  IpAddr                The IP address to classify in host byte order
76   @param[in]  IpIf                  The interface that IpAddr received from
77 
78   @return The cast type of this IP address specific to the interface.
79   @retval IP4_LOCAL_HOST        The IpAddr equals to the interface's address
80   @retval IP4_SUBNET_BROADCAST  The IpAddr is a directed subnet boradcast to  the
81                                 interface
82   @retval IP4_NET_BROADCAST     The IpAddr is a network broadcast to the interface
83   @retval 0                     Otherwise.
84 
85 **/
86 INTN
87 Ip4GetNetCast (
88   IN  IP4_ADDR          IpAddr,
89   IN  IP4_INTERFACE     *IpIf
90   );
91 
92 /**
93   Find the cast type of the packet related to the local host.
94   This isn't the same as link layer cast type. For example, DHCP
95   server may send local broadcast to the local unicast MAC.
96 
97   @param[in]  IpSb                  The IP4 service binding instance that received the
98                                     packet
99   @param[in]  Dst                   The destination address in the packet (host byte
100                                     order)
101   @param[in]  Src                   The source address in the packet (host byte order)
102 
103   @return The cast type for the Dst, it will return on the first non-promiscuous
104           cast type to a configured interface. If the packet doesn't match any of
105           the interface, multicast address and local broadcast address are checked.
106 
107 **/
108 INTN
109 Ip4GetHostCast (
110   IN  IP4_SERVICE       *IpSb,
111   IN  IP4_ADDR          Dst,
112   IN  IP4_ADDR          Src
113   );
114 
115 /**
116   Find an interface whose configured IP address is Ip.
117 
118   @param[in]  IpSb                  The IP4 service binding instance
119   @param[in]  Ip                    The Ip address (host byte order) to find
120 
121   @return The IP4_INTERFACE point if found, otherwise NULL
122 
123 **/
124 IP4_INTERFACE *
125 Ip4FindInterface (
126   IN IP4_SERVICE        *IpSb,
127   IN IP4_ADDR           Ip
128   );
129 
130 /**
131   Find an interface that Ip is on that connected network.
132 
133   @param[in]  IpSb                  The IP4 service binding instance
134   @param[in]  Ip                    The Ip address (host byte order) to find
135 
136   @return The IP4_INTERFACE point if found, otherwise NULL
137 
138 **/
139 IP4_INTERFACE *
140 Ip4FindNet (
141   IN IP4_SERVICE        *IpSb,
142   IN IP4_ADDR           Ip
143   );
144 
145 /**
146   Find an interface of the service with the same Ip/Netmask pair.
147 
148   @param[in]  IpSb                  Ip4 service binding instance
149   @param[in]  Ip                    The Ip adress to find (host byte order)
150   @param[in]  Netmask               The network to find (host byte order)
151 
152   @return The IP4_INTERFACE point if found, otherwise NULL
153 
154 **/
155 IP4_INTERFACE *
156 Ip4FindStationAddress (
157   IN IP4_SERVICE        *IpSb,
158   IN IP4_ADDR           Ip,
159   IN IP4_ADDR           Netmask
160   );
161 
162 /**
163   Get the MAC address for a multicast IP address. Call
164   Mnp's McastIpToMac to find the MAC address in stead of
165   hard code the NIC to be Ethernet.
166 
167   @param[in]  Mnp                   The Mnp instance to get the MAC address.
168   @param[in]  Multicast             The multicast IP address to translate.
169   @param[out] Mac                   The buffer to hold the translated address.
170 
171   @retval EFI_SUCCESS if the multicast IP is successfully translated to a
172                       multicast MAC address.
173   @retval other       Otherwise some error.
174 
175 **/
176 EFI_STATUS
177 Ip4GetMulticastMac (
178   IN  EFI_MANAGED_NETWORK_PROTOCOL *Mnp,
179   IN  IP4_ADDR                     Multicast,
180   OUT EFI_MAC_ADDRESS              *Mac
181   );
182 
183 /**
184   Convert the multibyte field in IP header's byter order.
185   In spite of its name, it can also be used to convert from
186   host to network byte order.
187 
188   @param[in]  Head                  The IP head to convert
189 
190   @return Point to the converted IP head
191 
192 **/
193 IP4_HEAD *
194 Ip4NtohHead (
195   IN IP4_HEAD           *Head
196   );
197 
198 
199 /**
200   Validate that Ip/Netmask pair is OK to be used as station
201   address. Only continuous netmasks are supported. and check
202   that StationAddress is a unicast address on the newtwork.
203 
204   @param[in]  Ip                 The IP address to validate.
205   @param[in]  Netmask            The netmaks of the IP.
206 
207   @retval TRUE                   The Ip/Netmask pair is valid.
208   @retval FALSE                  The Ip/Netmask pair is invalid.
209 
210 **/
211 BOOLEAN
212 Ip4StationAddressValid (
213   IN IP4_ADDR               Ip,
214   IN IP4_ADDR               Netmask
215   );
216 
217 #endif
218