1/* Copyright (C) 2006-2017 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Ulrich Drepper <drepper@redhat.com>, 2006.
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, see
17 <http://www.gnu.org/licenses/>. */
18
19#include <string.h>
20#include <netinet/in.h>
21#include <netinet/ip6.h>
22
23
24/* RFC 3542, 10.1
25
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. */
31int
32inet6_opt_init (void *extbuf, socklen_t extlen)
33{
34 if (extbuf != NULL)
35 {
36 if (extlen <= 0 || (extlen % 8) != 0 || extlen > 256 * 8)
37 return -1;
38
39 /* Fill in the length in units of 8 octets. */
40 struct ip6_hbh *extp = (struct ip6_hbh *) extbuf;
41
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;
46 }
47
48 return sizeof (struct ip6_hbh);
49}
50
51
52static void
53add_padding (uint8_t *extbuf, int offset, int npad)
54{
55 if (npad == 1)
56 extbuf[offset] = IP6OPT_PAD1;
57 else if (npad > 0)
58 {
59 struct ip6_opt *pad_opt = (struct ip6_opt *) (extbuf + offset);
60
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);
65 }
66}
67
68
69
70/* RFC 3542, 10.2
71
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. */
79int
80inet6_opt_append (void *extbuf, socklen_t extlen, int offset, uint8_t type,
81 socklen_t len, uint8_t align, void **databufp)
82{
83 /* Check minimum offset. */
84 if (offset < sizeof (struct ip6_hbh))
85 return -1;
86
87 /* One cannot add padding options. */
88 if (type == IP6OPT_PAD1 || type == IP6OPT_PADN)
89 return -1;
90
91 /* The option length must fit in one octet. */
92 if (len > 255)
93 return -1;
94
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;
99
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);
107
108 if (extbuf != NULL)
109 {
110 /* Now we can check whether the buffer is large enough. */
111 if (data_offset + npad + len > extlen)
112 return -1;
113
114 add_padding (extbuf, offset, npad);
115
116 offset += npad;
117
118 /* Now prepare the option itself. */
119 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
120
121 opt->ip6o_type = type;
122 opt->ip6o_len = len;
123
124 *databufp = opt + 1;
125 }
126 else
127 offset += npad;
128
129 return offset + sizeof (struct ip6_opt) + len;
130}
131
132
133/* RFC 3542, 10.3
134
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. */
139int
140inet6_opt_finish (void *extbuf, socklen_t extlen, int offset)
141{
142 /* Check minimum offset. */
143 if (offset < sizeof (struct ip6_hbh))
144 return -1;
145
146 /* Required padding at the end. */
147 int npad = (8 - (offset & 7)) & 7;
148
149 if (extbuf != NULL)
150 {
151 /* Make sure the buffer is large enough. */
152 if (offset + npad > extlen)
153 return -1;
154
155 add_padding (extbuf, offset, npad);
156 }
157
158 return offset + npad;
159}
160
161
162/* RFC 3542, 10.4
163
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. */
169int
170inet6_opt_set_val (void *databuf, int offset, void *val, socklen_t vallen)
171{
172 memcpy ((uint8_t *) databuf + offset, val, vallen);
173
174 return offset + vallen;
175}
176
177
178/* RFC 3542, 10.5
179
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. */
186int
187inet6_opt_next (void *extbuf, socklen_t extlen, int offset, uint8_t *typep,
188 socklen_t *lenp, void **databufp)
189{
190 if (offset == 0)
191 offset = sizeof (struct ip6_hbh);
192 else if (offset < sizeof (struct ip6_hbh))
193 return -1;
194
195 while (offset < extlen)
196 {
197 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
198
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
205 {
206 /* Check whether the option is valid. */
207 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
208 if (offset > extlen)
209 return -1;
210
211 *typep = opt->ip6o_type;
212 *lenp = opt->ip6o_len;
213 *databufp = opt + 1;
214 return offset;
215 }
216 }
217
218 return -1;
219}
220
221
222/* RFC 3542, 10.6
223
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. */
228int
229inet6_opt_find (void *extbuf, socklen_t extlen, int offset, uint8_t type,
230 socklen_t *lenp, void **databufp)
231{
232 if (offset == 0)
233 offset = sizeof (struct ip6_hbh);
234 else if (offset < sizeof (struct ip6_hbh))
235 return -1;
236
237 while (offset < extlen)
238 {
239 struct ip6_opt *opt = (struct ip6_opt *) ((uint8_t *) extbuf + offset);
240
241 if (opt->ip6o_type == IP6OPT_PAD1)
242 {
243 /* Single byte padding. */
244 ++offset;
245 if (type == IP6OPT_PAD1)
246 {
247 *lenp = 0;
248 *databufp = (uint8_t *) extbuf + offset;
249 return offset;
250 }
251 }
252 else if (opt->ip6o_type != type)
253 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
254 else
255 {
256 /* Check whether the option is valid. */
257 offset += sizeof (struct ip6_opt) + opt->ip6o_len;
258 if (offset > extlen)
259 return -1;
260
261 *lenp = opt->ip6o_len;
262 *databufp = opt + 1;
263 return offset;
264 }
265 }
266
267 return -1;
268}
269
270
271/* RFC 3542, 10.7
272
273 This function extracts data items of various sizes in the data
274 portion of the option. */
275int
276inet6_opt_get_val (void *databuf, int offset, void *val, socklen_t vallen)
277{
278 memcpy (val, (uint8_t *) databuf + offset, vallen);
279
280 return offset + vallen;
281}
282