(optin_alloc): Add temporary variable to avoid warning.
[glibc/history.git] / inet / inet6_option.c
blobaa693cc9e4f60fd4960a6106fee9a5afa98fc085
1 /* Copyright (C) 2003, 2006, 2009 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2003.
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.
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.
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. */
20 #include <assert.h>
21 #include <string.h>
22 #include <netinet/in.h>
23 #include <netinet/ip6.h>
24 #include <sys/param.h>
27 static void
28 internal_function
29 add_pad (struct cmsghdr *cmsg, int len)
31 unsigned char *p = CMSG_DATA (cmsg) + cmsg->cmsg_len - CMSG_LEN (0);
33 if (len == 1)
34 /* Special handling for 1, a one-byte solution. */
35 *p++ = IP6OPT_PAD1;
36 else if (len != 0)
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;
46 /* Account for the bytes. */
47 cmsg->cmsg_len += len;
51 static int
52 get_opt_end (const uint8_t **result, const uint8_t *startp,
53 const uint8_t *endp)
55 if (startp >= endp)
56 /* Out of bounds. */
57 return -1;
59 if (*startp == IP6OPT_PAD1)
61 /* Just this one byte. */
62 *result = startp + 1;
63 return 0;
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;
72 *result = startp + startp[1] + 2;
74 return 0;
78 static uint8_t *option_alloc (struct cmsghdr *cmsg, int datalen, int multx,
79 int plusy);
82 /* RFC 2292, 6.3.1
84 This function returns the number of bytes required to hold an option
85 when it is stored as ancillary data, including the cmsghdr structure
86 at the beginning, and any padding at the end (to make its size a
87 multiple of 8 bytes). The argument is the size of the structure
88 defining the option, which must include any pad bytes at the
89 beginning (the value y in the alignment term "xn + y"), the type
90 byte, the length byte, and the option data. */
91 int
92 inet6_option_space (nbytes)
93 int nbytes;
95 /* Add room for the extension header. */
96 nbytes += sizeof (struct ip6_ext);
98 return CMSG_SPACE (roundup (nbytes, 8));
100 link_warning (inet6_option_space,
101 "inet6_option_space is obsolete, use the RFC 3542 interfaces")
104 /* RFC 2292, 6.3.2
106 This function is called once per ancillary data object that will
107 contain either Hop-by-Hop or Destination options. It returns 0 on
108 success or -1 on an error. */
110 inet6_option_init (bp, cmsgp, type)
111 void *bp;
112 struct cmsghdr **cmsgp;
113 int type;
115 /* Only Hop-by-Hop or Destination options allowed. */
116 if (type != IPV6_HOPOPTS && type != IPV6_DSTOPTS)
117 return -1;
119 /* BP is a pointer to the previously allocated space. */
120 struct cmsghdr *newp = (struct cmsghdr *) bp;
122 /* Initialize the message header.
124 Length: No data yet, only the cmsghdr struct. */
125 newp->cmsg_len = CMSG_LEN (0);
126 /* Originating protocol: obviously IPv6. */
127 newp->cmsg_level = IPPROTO_IPV6;
128 /* Message type. */
129 newp->cmsg_type = type;
131 /* Pass up the result. */
132 *cmsgp = newp;
134 return 0;
136 link_warning (inet6_option_init,
137 "inet6_option_init is obsolete, use the RFC 3542 interfaces")
140 /* RFC 2292, 6.3.3
142 This function appends a Hop-by-Hop option or a Destination option
143 into an ancillary data object that has been initialized by
144 inet6_option_init(). This function returns 0 if it succeeds or -1 on
145 an error. */
147 inet6_option_append (cmsg, typep, multx, plusy)
148 struct cmsghdr *cmsg;
149 const uint8_t *typep;
150 int multx;
151 int plusy;
153 /* typep is a pointer to the 8-bit option type. It is assumed that this
154 field is immediately followed by the 8-bit option data length field,
155 which is then followed immediately by the option data.
157 The option types IP6OPT_PAD1 and IP6OPT_PADN also must be handled. */
158 int len = typep[0] == IP6OPT_PAD1 ? 1 : typep[1] + 2;
160 /* Get the pointer to the space in the message. */
161 uint8_t *ptr = option_alloc (cmsg, len, multx, plusy);
162 if (ptr == NULL)
163 /* Some problem with the parameters. */
164 return -1;
166 /* Copy the content. */
167 memcpy (ptr, typep, len);
169 return 0;
171 link_warning (inet6_option_append,
172 "inet6_option_append is obsolete, use the RFC 3542 interfaces")
175 /* RFC 2292, 6.3.4
177 This function appends a Hop-by-Hop option or a Destination option
178 into an ancillary data object that has been initialized by
179 inet6_option_init(). This function returns a pointer to the 8-bit
180 option type field that starts the option on success, or NULL on an
181 error. */
182 static uint8_t *
183 option_alloc (struct cmsghdr *cmsg, int datalen, int multx, int plusy)
185 /* The RFC limits the value of the alignment values. */
186 if ((multx != 1 && multx != 2 && multx != 4 && multx != 8)
187 || ! (plusy >= 0 && plusy <= 7))
188 return NULL;
190 /* Current data size. */
191 int dsize = cmsg->cmsg_len - CMSG_LEN (0);
193 /* The first two bytes of the option are for the extended header. */
194 if (__builtin_expect (dsize == 0, 0))
196 cmsg->cmsg_len += sizeof (struct ip6_ext);
197 dsize = sizeof (struct ip6_ext);
200 /* First add padding. */
201 add_pad (cmsg, ((multx - (dsize & (multx - 1))) & (multx - 1)) + plusy);
203 /* Return the pointer to the start of the option space. */
204 uint8_t *result = CMSG_DATA (cmsg) + cmsg->cmsg_len - CMSG_LEN (0);
205 cmsg->cmsg_len += datalen;
207 /* The extended option header length is measured in 8-byte groups.
208 To represent the current length we might have to add padding. */
209 dsize = cmsg->cmsg_len - CMSG_LEN (0);
210 add_pad (cmsg, (8 - (dsize & (8 - 1))) & (8 - 1));
212 /* Record the new length of the option. */
213 assert (((cmsg->cmsg_len - CMSG_LEN (0)) % 8) == 0);
214 int len8b = (cmsg->cmsg_len - CMSG_LEN (0)) / 8 - 1;
215 if (len8b >= 256)
216 /* Too long. */
217 return NULL;
219 struct ip6_ext *ie = (void *) CMSG_DATA (cmsg);
220 ie->ip6e_len = len8b;
222 return result;
226 uint8_t *
227 inet6_option_alloc (cmsg, datalen, multx, plusy)
228 struct cmsghdr *cmsg;
229 int datalen;
230 int multx;
231 int plusy;
233 return option_alloc (cmsg, datalen, multx, plusy);
235 link_warning (inet6_option_alloc,
236 "inet6_option_alloc is obsolete, use the RFC 3542 interfaces")
239 /* RFC 2292, 6.3.5
241 This function processes the next Hop-by-Hop option or Destination
242 option in an ancillary data object. If another option remains to be
243 processed, the return value of the function is 0 and *tptrp points to
244 the 8-bit option type field (which is followed by the 8-bit option
245 data length, followed by the option data). If no more options remain
246 to be processed, the return value is -1 and *tptrp is NULL. If an
247 error occurs, the return value is -1 and *tptrp is not NULL. */
249 inet6_option_next (cmsg, tptrp)
250 const struct cmsghdr *cmsg;
251 uint8_t **tptrp;
253 /* Make sure it is an option of the right type. */
254 if (cmsg->cmsg_level != IPPROTO_IPV6
255 || (cmsg->cmsg_type != IPV6_HOPOPTS && cmsg->cmsg_type != IPV6_DSTOPTS))
256 return -1;
258 /* Pointer to the extension header. We only compute the address, we
259 don't access anything yet. */
260 const struct ip6_ext *ip6e = (const struct ip6_ext *) CMSG_DATA (cmsg);
262 /* Make sure the message is long enough. */
263 if (cmsg->cmsg_len < CMSG_LEN (sizeof (struct ip6_ext))
264 /* Now we can access the extension header. */
265 || cmsg->cmsg_len < CMSG_LEN ((ip6e->ip6e_len + 1) * 8))
266 /* Too small. */
267 return -1;
269 /* Determine the address of the byte past the message. */
270 const uint8_t *endp = CMSG_DATA (cmsg) + (ip6e->ip6e_len + 1) * 8;
272 const uint8_t *result;
273 if (*tptrp == NULL)
274 /* This is the first call, return the first option if there is one. */
275 result = (const uint8_t *) (ip6e + 1);
276 else
278 /* Make sure *TPTRP points to a beginning of a new option in
279 the message. The upper limit is checked in get_opt_end. */
280 if (*tptrp < (const uint8_t *) (ip6e + 1))
281 return -1;
283 /* Get the beginning of the next option. */
284 if (get_opt_end (&result, *tptrp, endp) != 0)
285 return -1;
288 /* We know where the next option starts. */
289 *tptrp = (uint8_t *) result;
291 /* Check the option is fully represented in the message. */
292 return get_opt_end (&result, result, endp);
294 link_warning (inet6_option_next,
295 "inet6_option_next is obsolete, use the RFC 3542 interfaces")
298 /* RFC 2292, 6.3.6
300 This function is similar to the previously described
301 inet6_option_next() function, except this function lets the caller
302 specify the option type to be searched for, instead of always
303 returning the next option in the ancillary data object. cmsg is a
304 pointer to cmsghdr structure of which cmsg_level equals IPPROTO_IPV6
305 and cmsg_type equals either IPV6_HOPOPTS or IPV6_DSTOPTS. */
307 inet6_option_find (cmsg, tptrp, type)
308 const struct cmsghdr *cmsg;
309 uint8_t **tptrp;
310 int type;
312 /* Make sure it is an option of the right type. */
313 if (cmsg->cmsg_level != IPPROTO_IPV6
314 || (cmsg->cmsg_type != IPV6_HOPOPTS && cmsg->cmsg_type != IPV6_DSTOPTS))
315 return -1;
317 /* Pointer to the extension header. We only compute the address, we
318 don't access anything yet. */
319 const struct ip6_ext *ip6e = (const struct ip6_ext *) CMSG_DATA (cmsg);
321 /* Make sure the message is long enough. */
322 if (cmsg->cmsg_len < CMSG_LEN (sizeof (struct ip6_ext))
323 /* Now we can access the extension header. */
324 || cmsg->cmsg_len < CMSG_LEN ((ip6e->ip6e_len + 1) * 8))
325 /* Too small. */
326 return -1;
328 /* Determine the address of the byte past the message. */
329 const uint8_t *endp = CMSG_DATA (cmsg) + (ip6e->ip6e_len + 1) * 8;
331 const uint8_t *next;
332 if (*tptrp == NULL)
333 /* This is the first call, return the first option if there is one. */
334 next = (const uint8_t *) (ip6e + 1);
335 else
337 /* Make sure *TPTRP points to a beginning of a new option in
338 the message. The upper limit is checked in get_opt_end. */
339 if (*tptrp < (const uint8_t *) (ip6e + 1))
340 return -1;
342 /* Get the beginning of the next option. */
343 if (get_opt_end (&next, *tptrp, endp) != 0)
344 return -1;
347 /* Now search for the appropriate typed entry. */
348 const uint8_t *result;
351 result = next;
353 /* Get the end of this entry. */
354 if (get_opt_end (&next, result, endp) != 0)
355 return -1;
357 while (*result != type);
359 /* We know where the next option starts. */
360 *tptrp = (uint8_t *) result;
362 /* Success. */
363 return 0;
365 link_warning (inet6_option_find,
366 "inet6_option_find is obsolete, use the RFC 3542 interfaces")