1 /* Copyright (C) 2003, 2006 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
22 #include <netinet/in.h>
23 #include <netinet/ip6.h>
24 #include <sys/param.h>
29 add_pad (struct cmsghdr
*cmsg
, int len
)
31 unsigned char *p
= CMSG_DATA (cmsg
) + cmsg
->cmsg_len
- CMSG_LEN (0);
34 /* Special handling for 1, a one-byte solution. */
38 /* Multibyte padding. */
40 *p
++ = len
- 2; /* Discount the two header bytes. */
41 /* The rest is filled with zero. */
42 memset (p
, '\0', len
- 2);
46 /* Account for the bytes. */
47 cmsg
->cmsg_len
+= len
;
52 get_opt_end (const uint8_t **result
, const uint8_t *startp
,
59 if (*startp
== IP6OPT_PAD1
)
61 /* Just this one byte. */
66 /* Now we know there must be at least two bytes. */
68 /* Now we can get the length byte. */
69 || startp
+ startp
[1] + 2 > endp
)
72 *result
= startp
+ startp
[1] + 2;
78 static uint8_t *option_alloc (struct cmsghdr
*cmsg
, int datalen
, int multx
,
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. */
92 inet6_option_space (nbytes
)
95 /* Add room for the extension header. */
96 nbytes
+= sizeof (struct ip6_ext
);
98 return CMSG_SPACE (roundup (nbytes
, 8));
104 This function is called once per ancillary data object that will
105 contain either Hop-by-Hop or Destination options. It returns 0 on
106 success or -1 on an error. */
108 inet6_option_init (bp
, cmsgp
, type
)
110 struct cmsghdr
**cmsgp
;
113 /* Only Hop-by-Hop or Destination options allowed. */
114 if (type
!= IPV6_HOPOPTS
&& type
!= IPV6_DSTOPTS
)
117 /* BP is a pointer to the previously allocated space. */
118 struct cmsghdr
*newp
= (struct cmsghdr
*) bp
;
120 /* Initialize the message header.
122 Length: No data yet, only the cmsghdr struct. */
123 newp
->cmsg_len
= CMSG_LEN (0);
124 /* Originating protocol: obviously IPv6. */
125 newp
->cmsg_level
= IPPROTO_IPV6
;
127 newp
->cmsg_type
= type
;
129 /* Pass up the result. */
138 This function appends a Hop-by-Hop option or a Destination option
139 into an ancillary data object that has been initialized by
140 inet6_option_init(). This function returns 0 if it succeeds or -1 on
143 inet6_option_append (cmsg
, typep
, multx
, plusy
)
144 struct cmsghdr
*cmsg
;
145 const uint8_t *typep
;
149 /* typep is a pointer to the 8-bit option type. It is assumed that this
150 field is immediately followed by the 8-bit option data length field,
151 which is then followed immediately by the option data.
153 The option types IP6OPT_PAD1 and IP6OPT_PADN also must be handled. */
154 int len
= typep
[0] == IP6OPT_PAD1
? 1 : typep
[1] + 2;
156 /* Get the pointer to the space in the message. */
157 uint8_t *ptr
= option_alloc (cmsg
, len
, multx
, plusy
);
159 /* Some problem with the parameters. */
162 /* Copy the content. */
163 memcpy (ptr
, typep
, len
);
171 This function appends a Hop-by-Hop option or a Destination option
172 into an ancillary data object that has been initialized by
173 inet6_option_init(). This function returns a pointer to the 8-bit
174 option type field that starts the option on success, or NULL on an
177 option_alloc (struct cmsghdr
*cmsg
, int datalen
, int multx
, int plusy
)
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))
184 /* Current data size. */
185 int dsize
= cmsg
->cmsg_len
- CMSG_LEN (0);
187 /* The first two bytes of the option are for the extended header. */
188 if (__builtin_expect (dsize
== 0, 0))
190 cmsg
->cmsg_len
+= sizeof (struct ip6_ext
);
191 dsize
= sizeof (struct ip6_ext
);
194 /* First add padding. */
195 add_pad (cmsg
, ((multx
- (dsize
& (multx
- 1))) & (multx
- 1)) + plusy
);
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
;
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));
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;
213 ((struct ip6_ext
*) CMSG_DATA (cmsg
))->ip6e_len
= len8b
;
220 inet6_option_alloc (cmsg
, datalen
, multx
, plusy
)
221 struct cmsghdr
*cmsg
;
226 return option_alloc (cmsg
, datalen
, multx
, plusy
);
232 This function processes the next Hop-by-Hop option or Destination
233 option in an ancillary data object. If another option remains to be
234 processed, the return value of the function is 0 and *tptrp points to
235 the 8-bit option type field (which is followed by the 8-bit option
236 data length, followed by the option data). If no more options remain
237 to be processed, the return value is -1 and *tptrp is NULL. If an
238 error occurs, the return value is -1 and *tptrp is not NULL. */
240 inet6_option_next (cmsg
, tptrp
)
241 const struct cmsghdr
*cmsg
;
244 /* Make sure it is an option of the right type. */
245 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
246 || (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&& cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
249 /* Pointer to the extension header. We only compute the address, we
250 don't access anything yet. */
251 const struct ip6_ext
*ip6e
= (const struct ip6_ext
*) CMSG_DATA (cmsg
);
253 /* Make sure the message is long enough. */
254 if (cmsg
->cmsg_len
< CMSG_LEN (sizeof (struct ip6_ext
))
255 /* Now we can access the extension header. */
256 || cmsg
->cmsg_len
< CMSG_LEN ((ip6e
->ip6e_len
+ 1) * 8))
260 /* Determine the address of the byte past the message. */
261 const uint8_t *endp
= CMSG_DATA (cmsg
) + (ip6e
->ip6e_len
+ 1) * 8;
263 const uint8_t *result
;
265 /* This is the first call, return the first option if there is one. */
266 result
= (const uint8_t *) (ip6e
+ 1);
269 /* Make sure *TPTRP points to a beginning of a new option in
270 the message. The upper limit is checked in get_opt_end. */
271 if (*tptrp
< (const uint8_t *) (ip6e
+ 1))
274 /* Get the beginning of the next option. */
275 if (get_opt_end (&result
, *tptrp
, endp
) != 0)
279 /* We know where the next option starts. */
280 *tptrp
= (uint8_t *) result
;
282 /* Check the option is fully represented in the message. */
283 return get_opt_end (&result
, result
, endp
);
289 This function is similar to the previously described
290 inet6_option_next() function, except this function lets the caller
291 specify the option type to be searched for, instead of always
292 returning the next option in the ancillary data object. cmsg is a
293 pointer to cmsghdr structure of which cmsg_level equals IPPROTO_IPV6
294 and cmsg_type equals either IPV6_HOPOPTS or IPV6_DSTOPTS. */
296 inet6_option_find (cmsg
, tptrp
, type
)
297 const struct cmsghdr
*cmsg
;
301 /* Make sure it is an option of the right type. */
302 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
303 || (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&& cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
306 /* Pointer to the extension header. We only compute the address, we
307 don't access anything yet. */
308 const struct ip6_ext
*ip6e
= (const struct ip6_ext
*) CMSG_DATA (cmsg
);
310 /* Make sure the message is long enough. */
311 if (cmsg
->cmsg_len
< CMSG_LEN (sizeof (struct ip6_ext
))
312 /* Now we can access the extension header. */
313 || cmsg
->cmsg_len
< CMSG_LEN ((ip6e
->ip6e_len
+ 1) * 8))
317 /* Determine the address of the byte past the message. */
318 const uint8_t *endp
= CMSG_DATA (cmsg
) + (ip6e
->ip6e_len
+ 1) * 8;
322 /* This is the first call, return the first option if there is one. */
323 next
= (const uint8_t *) (ip6e
+ 1);
326 /* Make sure *TPTRP points to a beginning of a new option in
327 the message. The upper limit is checked in get_opt_end. */
328 if (*tptrp
< (const uint8_t *) (ip6e
+ 1))
331 /* Get the beginning of the next option. */
332 if (get_opt_end (&next
, *tptrp
, endp
) != 0)
336 /* Now search for the appropriate typed entry. */
337 const uint8_t *result
;
342 /* Get the end of this entry. */
343 if (get_opt_end (&next
, result
, endp
) != 0)
346 while (*result
!= type
);
348 /* We know where the next option starts. */
349 *tptrp
= (uint8_t *) result
;