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