Support expected failures in .test-result files.
[glibc.git] / inet / inet6_opt.c
bloba215889b5ec0e64fe8a3fedfb5b78f0cae9e8c76
1 /* Copyright (C) 2006-2014 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2006.
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/>. */
19 #include <string.h>
20 #include <netinet/in.h>
21 #include <netinet/ip6.h>
24 /* RFC 3542, 10.1
26 This function returns the number of bytes needed for the empty
27 extension header i.e., without any options. If EXTBUF is not NULL it
28 also initializes the extension header to have the correct length
29 field. In that case if the EXTLEN value is not a positive (i.e.,
30 non-zero) multiple of 8 the function fails and returns -1. */
31 int
32 inet6_opt_init (void *extbuf, socklen_t extlen)
34 if (extbuf != NULL)
36 if (extlen <= 0 || (extlen % 8) != 0 || extlen > 256 * 8)
37 return -1;
39 /* Fill in the length in units of 8 octets. */
40 struct ip6_hbh *extp = (struct ip6_hbh *) extbuf;
42 /* RFC 2460 requires that the header extension length is the
43 length of the option header in 8-byte units, not including
44 the first 8 bytes. Hence we have to subtract one. */
45 extp->ip6h_len = extlen / 8 - 1;
48 return sizeof (struct ip6_hbh);
52 static void
53 add_padding (uint8_t *extbuf, int offset, int npad)
55 if (npad == 1)
56 extbuf[offset] = IP6OPT_PAD1;
57 else if (npad > 0)
59 struct ip6_opt *pad_opt = (struct ip6_opt *) (extbuf + offset);
61 pad_opt->ip6o_type = IP6OPT_PADN;
62 pad_opt->ip6o_len = npad - sizeof (struct ip6_opt);
63 /* Clear the memory used by the padding. */
64 memset (pad_opt + 1, '\0', pad_opt->ip6o_len);
70 /* RFC 3542, 10.2
72 This function returns the updated total length taking into account
73 adding an option with length 'len' and alignment 'align'. If
74 EXTBUF is not NULL then, in addition to returning the length, the
75 function inserts any needed pad option, initializes the option
76 (setting the type and length fields) and returns a pointer to the
77 location for the option content in databufp. If the option does
78 not fit in the extension header buffer the function returns -1. */
79 int
80 inet6_opt_append (void *extbuf, socklen_t extlen, int offset, uint8_t type,
81 socklen_t len, uint8_t align, void **databufp)
83 /* Check minimum offset. */
84 if (offset < sizeof (struct ip6_hbh))
85 return -1;
87 /* One cannot add padding options. */
88 if (type == IP6OPT_PAD1 || type == IP6OPT_PADN)
89 return -1;
91 /* The option length must fit in one octet. */
92 if (len > 255)
93 return -1;
95 /* The alignment can only by 1, 2, 4, or 8 and must not exceed the
96 option length. */
97 if (align == 0 || align > 8 || (align & (align - 1)) != 0 || align > len)
98 return -1;
100 /* Determine the needed padding for alignment. Following the
101 current content of the buffer we have the is the IPv6 option type
102 and length, followed immediately by the data. The data has the
103 alignment constraints. Therefore padding must be inserted in the
104 form of padding options before the new option. */
105 int data_offset = offset + sizeof (struct ip6_opt);
106 int npad = (align - data_offset % align) & (align - 1);
108 if (extbuf != NULL)
110 /* Now we can check whether the buffer is large enough. */
111 if (data_offset + npad + len > extlen)
112 return -1;
114 add_padding (extbuf, offset, npad);
116 offset += npad;
118 /* Now prepare the option itself. */
119 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
121 opt->ip6o_type = type;
122 opt->ip6o_len = len;
124 *databufp = opt + 1;
126 else
127 offset += npad;
129 return offset + sizeof (struct ip6_opt) + len;
133 /* RFC 3542, 10.3
135 This function returns the updated total length taking into account
136 the final padding of the extension header to make it a multiple of
137 8 bytes. If EXTBUF is not NULL the function also initializes the
138 option by inserting a Pad1 or PadN option of the proper length. */
140 inet6_opt_finish (void *extbuf, socklen_t extlen, int offset)
142 /* Check minimum offset. */
143 if (offset < sizeof (struct ip6_hbh))
144 return -1;
146 /* Required padding at the end. */
147 int npad = (8 - (offset & 7)) & 7;
149 if (extbuf != NULL)
151 /* Make sure the buffer is large enough. */
152 if (offset + npad > extlen)
153 return -1;
155 add_padding (extbuf, offset, npad);
158 return offset + npad;
162 /* RFC 3542, 10.4
164 This function inserts data items of various sizes in the data
165 portion of the option. VAL should point to the data to be
166 inserted. OFFSET specifies where in the data portion of the option
167 the value should be inserted; the first byte after the option type
168 and length is accessed by specifying an offset of zero. */
170 inet6_opt_set_val (void *databuf, int offset, void *val, socklen_t vallen)
172 memcpy ((uint8_t *) databuf + offset, val, vallen);
174 return offset + vallen;
178 /* RFC 3542, 10.5
180 This function parses received option extension headers returning
181 the next option. EXTBUF and EXTLEN specifies the extension header.
182 OFFSET should either be zero (for the first option) or the length
183 returned by a previous call to 'inet6_opt_next' or
184 'inet6_opt_find'. It specifies the position where to continue
185 scanning the extension buffer. */
187 inet6_opt_next (void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
188 socklen_t *lenp, void **databufp)
190 if (offset == 0)
191 offset = sizeof (struct ip6_hbh);
192 else if (offset < sizeof (struct ip6_hbh))
193 return -1;
195 while (offset < extlen)
197 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
199 if (opt->ip6o_type == IP6OPT_PAD1)
200 /* Single byte padding. */
201 ++offset;
202 else if (opt->ip6o_type == IP6OPT_PADN)
203 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
204 else
206 /* Check whether the option is valid. */
207 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
208 if (offset > extlen)
209 return -1;
211 *typep = opt->ip6o_type;
212 *lenp = opt->ip6o_len;
213 *databufp = opt + 1;
214 return offset;
218 return -1;
222 /* RFC 3542, 10.6
224 This function is similar to the previously described
225 'inet6_opt_next' function, except this function lets the caller
226 specify the option type to be searched for, instead of always
227 returning the next option in the extension header. */
229 inet6_opt_find (void *extbuf, socklen_t extlen, int offset, uint8_t type,
230 socklen_t *lenp, void **databufp)
232 if (offset == 0)
233 offset = sizeof (struct ip6_hbh);
234 else if (offset < sizeof (struct ip6_hbh))
235 return -1;
237 while (offset < extlen)
239 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
241 if (opt->ip6o_type == IP6OPT_PAD1)
243 /* Single byte padding. */
244 ++offset;
245 if (type == IP6OPT_PAD1)
247 *lenp = 0;
248 *databufp = (uint8_t *) extbuf + offset;
249 return offset;
252 else if (opt->ip6o_type != type)
253 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
254 else
256 /* Check whether the option is valid. */
257 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
258 if (offset > extlen)
259 return -1;
261 *lenp = opt->ip6o_len;
262 *databufp = opt + 1;
263 return offset;
267 return -1;
271 /* RFC 3542, 10.7
273 This function extracts data items of various sizes in the data
274 portion of the option. */
276 inet6_opt_get_val (void *databuf, int offset, void *val, socklen_t vallen)
278 memcpy (val, (uint8_t *) databuf + offset, vallen);
280 return offset + vallen;