]> sourceware.org Git - glibc.git/blame - inet/inet6_option.c
Update.
[glibc.git] / inet / inet6_option.c
CommitLineData
a4596570
UD
1/* Copyright (C) 2003 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
19
20#include <assert.h>
21#include <string.h>
22#include <netinet/in.h>
23#include <netinet/ip6.h>
24#include <sys/param.h>
25
26
27static void
28internal_function
29add_pad (struct cmsghdr *cmsg, int len)
30{
31 unsigned char *p = CMSG_DATA (cmsg) + cmsg->cmsg_len - CMSG_LEN (0);
32
33 if (len == 1)
34 /* Special handling for 1, a one-byte solution. */
35 *p++ = IP6OPT_PAD1;
36 else if (len != 0)
37 {
38 /* Multibyte padding. */
39 *p++ = IP6OPT_PADN;
40 *p++ = len - 2; /* Discount the two header bytes. */
41 /* The rest is filled with zero. */
42 memset (p, '\0', len - 2);
43 p += len - 2;
44 }
45
46 /* Account for the bytes. */
47 cmsg->cmsg_len += len;
48}
49
50
51static int
52get_opt_end (const uint8_t **result, const uint8_t *startp,
53 const uint8_t *endp)
54{
55 if (startp >= endp)
56 /* Out of bounds. */
57 return -1;
58
59 if (*startp == IP6OPT_PAD1)
60 {
61 /* Just this one byte. */
62 *result = startp + 1;
63 return 0;
64 }
65
66 /* Now we know there must be at least two bytes. */
67 if (startp + 2 > endp
68 /* Now we can get the length byte. */
69 || startp + startp[1] + 2 > endp)
70 return -1;
71
72 *result = startp + startp[1] + 2;
73
74 return 0;
75}
76
77
78/* RFC 2292, 6.3.1
79
80 This function returns the number of bytes required to hold an option
81 when it is stored as ancillary data, including the cmsghdr structure
82 at the beginning, and any padding at the end (to make its size a
83 multiple of 8 bytes). The argument is the size of the structure
84 defining the option, which must include any pad bytes at the
85 beginning (the value y in the alignment term "xn + y"), the type
86 byte, the length byte, and the option data. */
87int
88inet6_option_space (nbytes)
89 int nbytes;
90{
91 /* Add room for the extension header. */
92 nbytes += sizeof (struct ip6_ext);
93
94 return CMSG_SPACE (roundup (nbytes, 8));
95}
96
97
98/* RFC 2292, 6.3.2
99
100 This function is called once per ancillary data object that will
101 contain either Hop-by-Hop or Destination options. It returns 0 on
102 success or -1 on an error. */
103int
104inet6_option_init (bp, cmsgp, type)
105 void *bp;
106 struct cmsghdr **cmsgp;
107 int type;
108{
109 /* Only Hop-by-Hop or Destination options allowed. */
110 if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
111 return -1;
112
113 /* BP is a pointer to the previously allocated space. */
114 struct cmsghdr *newp = (struct cmsghdr *) bp;
115
116 /* Initialize the message header.
117
118 Length: No data yet, only the cmsghdr struct. */
119 newp->cmsg_len = CMSG_LEN (0);
120 /* Originating protocol: obviously IPv6. */
121 newp->cmsg_level = IPPROTO_IPV6;
122 /* Message type. */
123 newp->cmsg_type = type;
124
125 /* Pass up the result. */
126 *cmsgp = newp;
127
128 return 0;
129}
130
131
132/* RFC 2292, 6.3.3
133
134 This function appends a Hop-by-Hop option or a Destination option
135 into an ancillary data object that has been initialized by
136 inet6_option_init(). This function returns 0 if it succeeds or -1 on
137 an error. */
138int
139inet6_option_append (cmsg, typep, multx, plusy)
140 struct cmsghdr *cmsg;
141 const uint8_t *typep;
142 int multx;
143 int plusy;
144{
145 /* typep is a pointer to the 8-bit option type. It is assumed that this
146 field is immediately followed by the 8-bit option data length field,
147 which is then followed immediately by the option data.
148
149 The option types IP6OPT_PAD1 and IP6OPT_PADN also must be handled. */
150 int len = typep[0] == IP6OPT_PAD1 ? 1 : typep[1] + 2;
151
152 /* Get the pointer to the space in the message. */
153 uint8_t *ptr = inet6_option_alloc (cmsg, len, multx, plusy);
154 if (ptr == NULL)
155 /* Some problem with the parameters. */
156 return -1;
157
158 /* Copy the content. */
159 memcpy (ptr, typep, len);
160
161 return 0;
162}
163
164
165/* RFC 2292, 6.3.4
166
167 This function appends a Hop-by-Hop option or a Destination option
168 into an ancillary data object that has been initialized by
169 inet6_option_init(). This function returns a pointer to the 8-bit
170 option type field that starts the option on success, or NULL on an
171 error. */
172uint8_t *
173inet6_option_alloc (cmsg, datalen, multx, plusy)
174 struct cmsghdr *cmsg;
175 int datalen;
176 int multx;
177 int plusy;
178{
179 /* The RFC limits the value of the alignment values. */
180 if ((multx != 1 && multx != 2 && multx != 4 && multx != 8)
181 || ! (plusy >= 0 && plusy <= 7))
182 return NULL;
183
184 /* Current data size. */
185 int dsize = cmsg->cmsg_len - CMSG_LEN (0);
186
187 /* The first two bytes of the option are for the extended header. */
188 if (__builtin_expect (dsize == 0, 0))
189 {
190 cmsg->cmsg_len += sizeof (struct ip6_ext);
191 dsize = sizeof (struct ip6_ext);
192 }
193
194 /* First add padding. */
195 add_pad (cmsg, ((multx - (dsize & (multx - 1))) & (multx - 1)) + plusy);
196
197 /* Return the pointer to the start of the option space. */
198 uint8_t *result = CMSG_DATA (cmsg) + cmsg->cmsg_len - CMSG_LEN (0);
199 cmsg->cmsg_len += datalen;
200
201 /* The extended option header length is measured in 8-byte groups.
202 To represent the current length we might have to add padding. */
203 dsize = cmsg->cmsg_len - CMSG_LEN (0);
204 add_pad (cmsg, (8 - (dsize & (8 - 1))) & (8 - 1));
205
206 /* Record the new length of the option. */
207 assert (((cmsg->cmsg_len - CMSG_LEN (0)) % 8) == 0);
208 int len8b = (cmsg->cmsg_len - CMSG_LEN (0)) / 8 - 1;
209 if (len8b >= 256)
210 /* Too long. */
211 return NULL;
212
213 ((struct ip6_ext *) CMSG_DATA (cmsg))->ip6e_len = len8b;
214
215 return result;
216}
217
218
219/* RFC 2292, 6.3.5
220
221 This function processes the next Hop-by-Hop option or Destination
222 option in an ancillary data object. If another option remains to be
223 processed, the return value of the function is 0 and *tptrp points to
224 the 8-bit option type field (which is followed by the 8-bit option
225 data length, followed by the option data). If no more options remain
226 to be processed, the return value is -1 and *tptrp is NULL. If an
227 error occurs, the return value is -1 and *tptrp is not NULL. */
228int
229inet6_option_next (cmsg, tptrp)
230 const struct cmsghdr *cmsg;
231 uint8_t **tptrp;
232{
233 /* Make sure it is an option of the right type. */
234 if (cmsg->cmsg_level != IPPROTO_IPV6
235 || (cmsg->cmsg_type != IPV6_HOPOPTS && cmsg->cmsg_type != IPV6_DSTOPTS))
236 return -1;
237
238 /* Pointer to the extension header. We only compute the address, we
239 don't access anything yet. */
240 const struct ip6_ext *ip6e = (const struct ip6_ext *) CMSG_DATA (cmsg);
241
242 /* Make sure the message is long enough. */
243 if (cmsg->cmsg_len < CMSG_LEN (sizeof (struct ip6_ext))
244 /* Now we can access the extension header. */
245 || cmsg->cmsg_len < CMSG_LEN ((ip6e->ip6e_len + 1) * 8))
246 /* Too small. */
247 return -1;
248
249 /* Determine the address of the byte past the message. */
250 const uint8_t *endp = CMSG_DATA (cmsg) + (ip6e->ip6e_len + 1) * 8;
251
252 const uint8_t *result;
253 if (tptrp == NULL)
254 /* This is the first call, return the first option if there is one. */
255 result = (const uint8_t *) (ip6e + 1);
256 else
257 {
258 /* Make sure *TPTRP points to a beginning of a new option in
259 the message. The upper limit is checked in get_opt_end. */
260 if (*tptrp < (const uint8_t *) (ip6e + 1))
261 return -1;
262
263 /* Get the beginning of the next option. */
264 if (get_opt_end (&result, *tptrp, endp) != 0)
265 return -1;
266 }
267
268 /* We know where the next option starts. */
269 *tptrp = (uint8_t *) result;
270
271 /* Check the option is fully represented in the message. */
272 return get_opt_end (&result, result, endp);
273}
274
275
276/* RFC 2292, 6.3.6
277
278 This function is similar to the previously described
279 inet6_option_next() function, except this function lets the caller
280 specify the option type to be searched for, instead of always
281 returning the next option in the ancillary data object. cmsg is a
282 pointer to cmsghdr structure of which cmsg_level equals IPPROTO_IPV6
283 and cmsg_type equals either IPV6_HOPOPTS or IPV6_DSTOPTS. */
284int
285inet6_option_find (cmsg, tptrp, type)
286 const struct cmsghdr *cmsg;
287 uint8_t **tptrp;
288 int type;
289{
290 /* Make sure it is an option of the right type. */
291 if (cmsg->cmsg_level != IPPROTO_IPV6
292 || (cmsg->cmsg_type != IPV6_HOPOPTS && cmsg->cmsg_type != IPV6_DSTOPTS))
293 return -1;
294
295 /* Pointer to the extension header. We only compute the address, we
296 don't access anything yet. */
297 const struct ip6_ext *ip6e = (const struct ip6_ext *) CMSG_DATA (cmsg);
298
299 /* Make sure the message is long enough. */
300 if (cmsg->cmsg_len < CMSG_LEN (sizeof (struct ip6_ext))
301 /* Now we can access the extension header. */
302 || cmsg->cmsg_len < CMSG_LEN ((ip6e->ip6e_len + 1) * 8))
303 /* Too small. */
304 return -1;
305
306 /* Determine the address of the byte past the message. */
307 const uint8_t *endp = CMSG_DATA (cmsg) + (ip6e->ip6e_len + 1) * 8;
308
309 const uint8_t *next;
310 if (tptrp == NULL)
311 /* This is the first call, return the first option if there is one. */
312 next = (const uint8_t *) (ip6e + 1);
313 else
314 {
315 /* Make sure *TPTRP points to a beginning of a new option in
316 the message. The upper limit is checked in get_opt_end. */
317 if (*tptrp < (const uint8_t *) (ip6e + 1))
318 return -1;
319
320 /* Get the beginning of the next option. */
321 if (get_opt_end (&next, *tptrp, endp) != 0)
322 return -1;
323 }
324
325 /* Now search for the appropriate typed entry. */
326 const uint8_t *result;
327 do
328 {
329 result = next;
330
331 /* Get the end of this entry. */
332 if (get_opt_end (&next, result, endp) != 0)
333 return -1;
334 }
335 while (*result != type);
336
337 /* We know where the next option starts. */
338 *tptrp = (uint8_t *) result;
339
340 /* Success. */
341 return 0;
342}
This page took 0.05105 seconds and 5 git commands to generate.