]>
Commit | Line | Data |
---|---|---|
772db4bb | 1 | /** @file\r |
3e8c18da | 2 | IP4 option support functions.\r |
3 | \r | |
7538d536 | 4 | Copyright (c) 2005 - 2011, Intel Corporation. All rights reserved.<BR>\r |
e5eed7d3 | 5 | This program and the accompanying materials\r |
772db4bb | 6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r | |
8 | http://opensource.org/licenses/bsd-license.php\r | |
9 | \r | |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r | |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r | |
12 | \r | |
772db4bb | 13 | **/\r |
14 | \r | |
15 | #include "Ip4Impl.h"\r | |
16 | \r | |
17 | \r | |
18 | /**\r | |
19 | Validate the IP4 option format for both the packets we received\r | |
7538d536 | 20 | and will transmit.\r |
772db4bb | 21 | \r |
3e8c18da | 22 | @param[in] Option The first byte of the option\r |
23 | @param[in] OptionLen The length of the whole option\r | |
24 | @param[in] Rcvd The option is from the packet we received if TRUE,\r | |
772db4bb | 25 | otherwise the option we wants to transmit.\r |
26 | \r | |
96e1079f | 27 | @retval TRUE The option is properly formatted\r |
28 | @retval FALSE The option is mal-formated\r | |
772db4bb | 29 | \r |
30 | **/\r | |
31 | BOOLEAN\r | |
32 | Ip4OptionIsValid (\r | |
33 | IN UINT8 *Option,\r | |
34 | IN UINT32 OptionLen,\r | |
35 | IN BOOLEAN Rcvd\r | |
36 | )\r | |
37 | {\r | |
38 | UINT32 Cur;\r | |
39 | UINT32 Len;\r | |
40 | UINT32 Point;\r | |
772db4bb | 41 | \r |
42 | Cur = 0;\r | |
43 | \r | |
44 | while (Cur < OptionLen) {\r | |
45 | switch (Option[Cur]) {\r | |
46 | case IP4_OPTION_NOP:\r | |
47 | Cur++;\r | |
48 | break;\r | |
49 | \r | |
50 | case IP4_OPTION_EOP:\r | |
51 | Cur = OptionLen;\r | |
52 | break;\r | |
53 | \r | |
54 | case IP4_OPTION_LSRR:\r | |
55 | case IP4_OPTION_SSRR:\r | |
56 | case IP4_OPTION_RR:\r | |
57 | Len = Option[Cur + 1];\r | |
58 | Point = Option[Cur + 2];\r | |
59 | \r | |
60 | //\r | |
96e1079f | 61 | // SRR/RR options are formatted as |Type|Len|Point|Ip1|Ip2|...\r |
772db4bb | 62 | //\r |
63 | if ((OptionLen - Cur < Len) || (Len < 3) || ((Len - 3) % 4 != 0)) {\r | |
772db4bb | 64 | return FALSE;\r |
65 | }\r | |
66 | \r | |
67 | if ((Point > Len + 1) || (Point % 4 != 0)) {\r | |
772db4bb | 68 | return FALSE;\r |
69 | }\r | |
70 | \r | |
71 | //\r | |
72 | // The Point must point pass the last entry if the packet is received\r | |
73 | // by us. It must point to 4 if the packet is to be sent by us for\r | |
74 | // source route option.\r | |
75 | //\r | |
76 | if ((Option[Cur] != IP4_OPTION_RR) &&\r | |
77 | ((Rcvd && (Point != Len + 1)) || (!Rcvd && (Point != 4)))) {\r | |
78 | \r | |
772db4bb | 79 | return FALSE;\r |
80 | }\r | |
81 | \r | |
82 | Cur += Len;\r | |
83 | break;\r | |
84 | \r | |
85 | default:\r | |
86 | Len = Option[Cur + 1];\r | |
87 | \r | |
88 | if ((OptionLen - Cur < Len) || (Len < 2)) {\r | |
772db4bb | 89 | return FALSE;\r |
90 | }\r | |
91 | \r | |
92 | Cur = Cur + Len;\r | |
93 | break;\r | |
94 | }\r | |
95 | \r | |
96 | }\r | |
97 | \r | |
98 | return TRUE;\r | |
99 | }\r | |
100 | \r | |
101 | \r | |
102 | /**\r | |
103 | Copy the option from the original option to buffer. It\r | |
104 | handles the details such as:\r | |
105 | 1. whether copy the single IP4 option to the first/non-first\r | |
96e1079f | 106 | fragments.\r |
107 | 2. Pad the options copied over to aligned to 4 bytes.\r | |
772db4bb | 108 | \r |
3e8c18da | 109 | @param[in] Option The original option to copy from\r |
110 | @param[in] OptionLen The length of the original option\r | |
111 | @param[in] FirstFragment Whether it is the first fragment\r | |
112 | @param[in, out] Buf The buffer to copy options to. NULL \r | |
113 | @param[in, out] BufLen The length of the buffer\r | |
772db4bb | 114 | \r |
115 | @retval EFI_SUCCESS The options are copied over\r | |
96e1079f | 116 | @retval EFI_BUFFER_TOO_SMALL Buf is NULL or BufLen provided is too small.\r |
772db4bb | 117 | \r |
118 | **/\r | |
119 | EFI_STATUS\r | |
120 | Ip4CopyOption (\r | |
96e1079f | 121 | IN UINT8 *Option,\r |
122 | IN UINT32 OptionLen,\r | |
123 | IN BOOLEAN FirstFragment,\r | |
124 | IN OUT UINT8 *Buf, OPTIONAL\r | |
772db4bb | 125 | IN OUT UINT32 *BufLen\r |
126 | )\r | |
127 | {\r | |
128 | UINT8 OptBuf[40];\r | |
129 | UINT32 Cur;\r | |
130 | UINT32 Next;\r | |
131 | UINT8 Type;\r | |
132 | UINT32 Len;\r | |
133 | \r | |
134 | ASSERT ((BufLen != NULL) && (OptionLen <= 40));\r | |
135 | \r | |
136 | Cur = 0;\r | |
137 | Next = 0;\r | |
138 | \r | |
139 | while (Cur < OptionLen) {\r | |
140 | Type = Option[Cur];\r | |
141 | Len = Option[Cur + 1];\r | |
142 | \r | |
143 | if (Type == IP4_OPTION_NOP) {\r | |
144 | //\r | |
145 | // Keep the padding, in case that the sender wants to align\r | |
146 | // the option, say, to 4 bytes\r | |
147 | //\r | |
148 | OptBuf[Next] = IP4_OPTION_NOP;\r | |
149 | Next++;\r | |
150 | Cur++;\r | |
151 | \r | |
152 | } else if (Type == IP4_OPTION_EOP) {\r | |
153 | //\r | |
154 | // Don't append the EOP to avoid including only a EOP option\r | |
155 | //\r | |
156 | break;\r | |
157 | \r | |
158 | } else {\r | |
159 | //\r | |
160 | // don't copy options that is only valid for the first fragment\r | |
161 | //\r | |
5405e9a6 | 162 | if (FirstFragment || (Type & IP4_OPTION_COPY_MASK) != 0) {\r |
e48e37fc | 163 | CopyMem (OptBuf + Next, Option + Cur, Len);\r |
772db4bb | 164 | Next += Len;\r |
165 | }\r | |
166 | \r | |
167 | Cur += Len;\r | |
168 | }\r | |
169 | }\r | |
170 | \r | |
171 | //\r | |
172 | // Don't append an EOP only option.\r | |
173 | //\r | |
174 | if (Next == 0) {\r | |
175 | *BufLen = 0;\r | |
176 | return EFI_SUCCESS;\r | |
177 | }\r | |
178 | \r | |
179 | //\r | |
180 | // Append an EOP if the end of option doesn't coincide with the\r | |
181 | // end of the IP header, that is, isn't aligned to 4 bytes..\r | |
182 | //\r | |
183 | if ((Next % 4) != 0) {\r | |
184 | OptBuf[Next] = IP4_OPTION_EOP;\r | |
185 | Next++;\r | |
186 | }\r | |
187 | \r | |
188 | //\r | |
189 | // Head length is in the unit of 4 bytes. Now, Len is the\r | |
190 | // acutal option length to appear in the IP header.\r | |
191 | //\r | |
192 | Len = ((Next + 3) &~0x03);\r | |
193 | \r | |
194 | //\r | |
195 | // If the buffer is too small, set the BufLen then return\r | |
196 | //\r | |
197 | if ((Buf == NULL) || (*BufLen < Len)) {\r | |
198 | *BufLen = Len;\r | |
199 | return EFI_BUFFER_TOO_SMALL;\r | |
200 | }\r | |
201 | \r | |
202 | //\r | |
203 | // Copy the option to the Buf, zero the buffer first to pad\r | |
204 | // the options with NOP to align to 4 bytes.\r | |
205 | //\r | |
e48e37fc | 206 | ZeroMem (Buf, Len);\r |
207 | CopyMem (Buf, OptBuf, Next);\r | |
772db4bb | 208 | *BufLen = Len;\r |
209 | return EFI_SUCCESS;\r | |
210 | }\r |