1 /* Copyright (C) 2003-2015 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, see
17 <http://www.gnu.org/licenses/>. */
21 #include <netinet/in.h>
22 #include <netinet/ip6.h>
23 #include <sys/param.h>
28 add_pad (struct cmsghdr
*cmsg
, int len
)
30 unsigned char *p
= CMSG_DATA (cmsg
) + cmsg
->cmsg_len
- CMSG_LEN (0);
33 /* Special handling for 1, a one-byte solution. */
37 /* Multibyte padding. */
39 *p
++ = len
- 2; /* Discount the two header bytes. */
40 /* The rest is filled with zero. */
41 memset (p
, '\0', len
- 2);
45 /* Account for the bytes. */
46 cmsg
->cmsg_len
+= len
;
51 get_opt_end (const uint8_t **result
, const uint8_t *startp
,
58 if (*startp
== IP6OPT_PAD1
)
60 /* Just this one byte. */
65 /* Now we know there must be at least two bytes. */
67 /* Now we can get the length byte. */
68 || startp
+ startp
[1] + 2 > endp
)
71 *result
= startp
+ startp
[1] + 2;
77 static uint8_t *option_alloc (struct cmsghdr
*cmsg
, int datalen
, int multx
,
83 This function returns the number of bytes required to hold an option
84 when it is stored as ancillary data, including the cmsghdr structure
85 at the beginning, and any padding at the end (to make its size a
86 multiple of 8 bytes). The argument is the size of the structure
87 defining the option, which must include any pad bytes at the
88 beginning (the value y in the alignment term "xn + y"), the type
89 byte, the length byte, and the option data. */
91 inet6_option_space (nbytes
)
94 /* Add room for the extension header. */
95 nbytes
+= sizeof (struct ip6_ext
);
97 return CMSG_SPACE (roundup (nbytes
, 8));
99 link_warning (inet6_option_space
,
100 "inet6_option_space is obsolete, use the RFC 3542 interfaces")
105 This function is called once per ancillary data object that will
106 contain either Hop-by-Hop or Destination options. It returns 0 on
107 success or -1 on an error. */
109 inet6_option_init (bp
, cmsgp
, type
)
111 struct cmsghdr
**cmsgp
;
114 /* Only Hop-by-Hop or Destination options allowed. */
115 if (type
!= IPV6_HOPOPTS
&& type
!= IPV6_DSTOPTS
)
118 /* BP is a pointer to the previously allocated space. */
119 struct cmsghdr
*newp
= (struct cmsghdr
*) bp
;
121 /* Initialize the message header.
123 Length: No data yet, only the cmsghdr struct. */
124 newp
->cmsg_len
= CMSG_LEN (0);
125 /* Originating protocol: obviously IPv6. */
126 newp
->cmsg_level
= IPPROTO_IPV6
;
128 newp
->cmsg_type
= type
;
130 /* Pass up the result. */
135 link_warning (inet6_option_init
,
136 "inet6_option_init is obsolete, use the RFC 3542 interfaces")
141 This function appends a Hop-by-Hop option or a Destination option
142 into an ancillary data object that has been initialized by
143 inet6_option_init(). This function returns 0 if it succeeds or -1 on
146 inet6_option_append (cmsg
, typep
, multx
, plusy
)
147 struct cmsghdr
*cmsg
;
148 const uint8_t *typep
;
152 /* typep is a pointer to the 8-bit option type. It is assumed that this
153 field is immediately followed by the 8-bit option data length field,
154 which is then followed immediately by the option data.
156 The option types IP6OPT_PAD1 and IP6OPT_PADN also must be handled. */
157 int len
= typep
[0] == IP6OPT_PAD1
? 1 : typep
[1] + 2;
159 /* Get the pointer to the space in the message. */
160 uint8_t *ptr
= option_alloc (cmsg
, len
, multx
, plusy
);
162 /* Some problem with the parameters. */
165 /* Copy the content. */
166 memcpy (ptr
, typep
, len
);
170 link_warning (inet6_option_append
,
171 "inet6_option_append is obsolete, use the RFC 3542 interfaces")
176 This function appends a Hop-by-Hop option or a Destination option
177 into an ancillary data object that has been initialized by
178 inet6_option_init(). This function returns a pointer to the 8-bit
179 option type field that starts the option on success, or NULL on an
182 option_alloc (struct cmsghdr
*cmsg
, int datalen
, int multx
, int plusy
)
184 /* The RFC limits the value of the alignment values. */
185 if ((multx
!= 1 && multx
!= 2 && multx
!= 4 && multx
!= 8)
186 || ! (plusy
>= 0 && plusy
<= 7))
189 /* Current data size. */
190 int dsize
= cmsg
->cmsg_len
- CMSG_LEN (0);
192 /* The first two bytes of the option are for the extended header. */
193 if (__glibc_unlikely (dsize
== 0))
195 cmsg
->cmsg_len
+= sizeof (struct ip6_ext
);
196 dsize
= sizeof (struct ip6_ext
);
199 /* First add padding. */
200 add_pad (cmsg
, ((multx
- (dsize
& (multx
- 1))) & (multx
- 1)) + plusy
);
202 /* Return the pointer to the start of the option space. */
203 uint8_t *result
= CMSG_DATA (cmsg
) + cmsg
->cmsg_len
- CMSG_LEN (0);
204 cmsg
->cmsg_len
+= datalen
;
206 /* The extended option header length is measured in 8-byte groups.
207 To represent the current length we might have to add padding. */
208 dsize
= cmsg
->cmsg_len
- CMSG_LEN (0);
209 add_pad (cmsg
, (8 - (dsize
& (8 - 1))) & (8 - 1));
211 /* Record the new length of the option. */
212 assert (((cmsg
->cmsg_len
- CMSG_LEN (0)) % 8) == 0);
213 int len8b
= (cmsg
->cmsg_len
- CMSG_LEN (0)) / 8 - 1;
218 struct ip6_ext
*ie
= (void *) CMSG_DATA (cmsg
);
219 ie
->ip6e_len
= len8b
;
226 inet6_option_alloc (cmsg
, datalen
, multx
, plusy
)
227 struct cmsghdr
*cmsg
;
232 return option_alloc (cmsg
, datalen
, multx
, plusy
);
234 link_warning (inet6_option_alloc
,
235 "inet6_option_alloc is obsolete, use the RFC 3542 interfaces")
240 This function processes the next Hop-by-Hop option or Destination
241 option in an ancillary data object. If another option remains to be
242 processed, the return value of the function is 0 and *tptrp points to
243 the 8-bit option type field (which is followed by the 8-bit option
244 data length, followed by the option data). If no more options remain
245 to be processed, the return value is -1 and *tptrp is NULL. If an
246 error occurs, the return value is -1 and *tptrp is not NULL. */
248 inet6_option_next (cmsg
, tptrp
)
249 const struct cmsghdr
*cmsg
;
252 /* Make sure it is an option of the right type. */
253 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
254 || (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&& cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
257 /* Pointer to the extension header. We only compute the address, we
258 don't access anything yet. */
259 const struct ip6_ext
*ip6e
= (const struct ip6_ext
*) CMSG_DATA (cmsg
);
261 /* Make sure the message is long enough. */
262 if (cmsg
->cmsg_len
< CMSG_LEN (sizeof (struct ip6_ext
))
263 /* Now we can access the extension header. */
264 || cmsg
->cmsg_len
< CMSG_LEN ((ip6e
->ip6e_len
+ 1) * 8))
268 /* Determine the address of the byte past the message. */
269 const uint8_t *endp
= CMSG_DATA (cmsg
) + (ip6e
->ip6e_len
+ 1) * 8;
271 const uint8_t *result
;
273 /* This is the first call, return the first option if there is one. */
274 result
= (const uint8_t *) (ip6e
+ 1);
277 /* Make sure *TPTRP points to a beginning of a new option in
278 the message. The upper limit is checked in get_opt_end. */
279 if (*tptrp
< (const uint8_t *) (ip6e
+ 1))
282 /* Get the beginning of the next option. */
283 if (get_opt_end (&result
, *tptrp
, endp
) != 0)
287 /* We know where the next option starts. */
288 *tptrp
= (uint8_t *) result
;
290 /* Check the option is fully represented in the message. */
291 return get_opt_end (&result
, result
, endp
);
293 link_warning (inet6_option_next
,
294 "inet6_option_next is obsolete, use the RFC 3542 interfaces")
299 This function is similar to the previously described
300 inet6_option_next() function, except this function lets the caller
301 specify the option type to be searched for, instead of always
302 returning the next option in the ancillary data object. cmsg is a
303 pointer to cmsghdr structure of which cmsg_level equals IPPROTO_IPV6
304 and cmsg_type equals either IPV6_HOPOPTS or IPV6_DSTOPTS. */
306 inet6_option_find (cmsg
, tptrp
, type
)
307 const struct cmsghdr
*cmsg
;
311 /* Make sure it is an option of the right type. */
312 if (cmsg
->cmsg_level
!= IPPROTO_IPV6
313 || (cmsg
->cmsg_type
!= IPV6_HOPOPTS
&& cmsg
->cmsg_type
!= IPV6_DSTOPTS
))
316 /* Pointer to the extension header. We only compute the address, we
317 don't access anything yet. */
318 const struct ip6_ext
*ip6e
= (const struct ip6_ext
*) CMSG_DATA (cmsg
);
320 /* Make sure the message is long enough. */
321 if (cmsg
->cmsg_len
< CMSG_LEN (sizeof (struct ip6_ext
))
322 /* Now we can access the extension header. */
323 || cmsg
->cmsg_len
< CMSG_LEN ((ip6e
->ip6e_len
+ 1) * 8))
327 /* Determine the address of the byte past the message. */
328 const uint8_t *endp
= CMSG_DATA (cmsg
) + (ip6e
->ip6e_len
+ 1) * 8;
332 /* This is the first call, return the first option if there is one. */
333 next
= (const uint8_t *) (ip6e
+ 1);
336 /* Make sure *TPTRP points to a beginning of a new option in
337 the message. The upper limit is checked in get_opt_end. */
338 if (*tptrp
< (const uint8_t *) (ip6e
+ 1))
341 /* Get the beginning of the next option. */
342 if (get_opt_end (&next
, *tptrp
, endp
) != 0)
346 /* Now search for the appropriate typed entry. */
347 const uint8_t *result
;
352 /* Get the end of this entry. */
353 if (get_opt_end (&next
, result
, endp
) != 0)
356 while (*result
!= type
);
358 /* We know where the next option starts. */
359 *tptrp
= (uint8_t *) result
;
364 link_warning (inet6_option_find
,
365 "inet6_option_find is obsolete, use the RFC 3542 interfaces")