1 /* Copyright (C) 2006-2024 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with the GNU C Library; if not, see
16 <https://www.gnu.org/licenses/>. */
19 #include <netinet/in.h>
20 #include <netinet/ip6.h>
25 This function returns the number of bytes needed for the empty
26 extension header i.e., without any options. If EXTBUF is not NULL it
27 also initializes the extension header to have the correct length
28 field. In that case if the EXTLEN value is not a positive (i.e.,
29 non-zero) multiple of 8 the function fails and returns -1. */
31 inet6_opt_init (void *extbuf
, socklen_t extlen
)
35 if (extlen
<= 0 || (extlen
% 8) != 0 || extlen
> 256 * 8)
38 /* Fill in the length in units of 8 octets. */
39 struct ip6_hbh
*extp
= (struct ip6_hbh
*) extbuf
;
41 /* RFC 2460 requires that the header extension length is the
42 length of the option header in 8-byte units, not including
43 the first 8 bytes. Hence we have to subtract one. */
44 extp
->ip6h_len
= extlen
/ 8 - 1;
47 return sizeof (struct ip6_hbh
);
52 add_padding (uint8_t *extbuf
, int offset
, int npad
)
55 extbuf
[offset
] = IP6OPT_PAD1
;
58 struct ip6_opt
*pad_opt
= (struct ip6_opt
*) (extbuf
+ offset
);
60 pad_opt
->ip6o_type
= IP6OPT_PADN
;
61 pad_opt
->ip6o_len
= npad
- sizeof (struct ip6_opt
);
62 /* Clear the memory used by the padding. */
63 memset (pad_opt
+ 1, '\0', pad_opt
->ip6o_len
);
71 This function returns the updated total length taking into account
72 adding an option with length 'len' and alignment 'align'. If
73 EXTBUF is not NULL then, in addition to returning the length, the
74 function inserts any needed pad option, initializes the option
75 (setting the type and length fields) and returns a pointer to the
76 location for the option content in databufp. If the option does
77 not fit in the extension header buffer the function returns -1. */
79 inet6_opt_append (void *extbuf
, socklen_t extlen
, int offset
, uint8_t type
,
80 socklen_t len
, uint8_t align
, void **databufp
)
82 /* Check minimum offset. */
83 if (offset
< sizeof (struct ip6_hbh
))
86 /* One cannot add padding options. */
87 if (type
== IP6OPT_PAD1
|| type
== IP6OPT_PADN
)
90 /* The option length must fit in one octet. */
94 /* The alignment can only by 1, 2, 4, or 8 and must not exceed the
96 if (align
== 0 || align
> 8 || (align
& (align
- 1)) != 0 || align
> len
)
99 /* Determine the needed padding for alignment. Following the
100 current content of the buffer we have the is the IPv6 option type
101 and length, followed immediately by the data. The data has the
102 alignment constraints. Therefore padding must be inserted in the
103 form of padding options before the new option. */
104 int data_offset
= offset
+ sizeof (struct ip6_opt
);
105 int npad
= (align
- data_offset
% align
) & (align
- 1);
109 /* Now we can check whether the buffer is large enough. */
110 if (data_offset
+ npad
+ len
> extlen
)
113 add_padding (extbuf
, offset
, npad
);
117 /* Now prepare the option itself. */
118 struct ip6_opt
*opt
= (struct ip6_opt
*) ((uint8_t *) extbuf
+ offset
);
120 opt
->ip6o_type
= type
;
128 return offset
+ sizeof (struct ip6_opt
) + len
;
134 This function returns the updated total length taking into account
135 the final padding of the extension header to make it a multiple of
136 8 bytes. If EXTBUF is not NULL the function also initializes the
137 option by inserting a Pad1 or PadN option of the proper length. */
139 inet6_opt_finish (void *extbuf
, socklen_t extlen
, int offset
)
141 /* Check minimum offset. */
142 if (offset
< sizeof (struct ip6_hbh
))
145 /* Required padding at the end. */
146 int npad
= (8 - (offset
& 7)) & 7;
150 /* Make sure the buffer is large enough. */
151 if (offset
+ npad
> extlen
)
154 add_padding (extbuf
, offset
, npad
);
157 return offset
+ npad
;
163 This function inserts data items of various sizes in the data
164 portion of the option. VAL should point to the data to be
165 inserted. OFFSET specifies where in the data portion of the option
166 the value should be inserted; the first byte after the option type
167 and length is accessed by specifying an offset of zero. */
169 inet6_opt_set_val (void *databuf
, int offset
, void *val
, socklen_t vallen
)
171 memcpy ((uint8_t *) databuf
+ offset
, val
, vallen
);
173 return offset
+ vallen
;
179 This function parses received option extension headers returning
180 the next option. EXTBUF and EXTLEN specifies the extension header.
181 OFFSET should either be zero (for the first option) or the length
182 returned by a previous call to 'inet6_opt_next' or
183 'inet6_opt_find'. It specifies the position where to continue
184 scanning the extension buffer. */
186 inet6_opt_next (void *extbuf
, socklen_t extlen
, int offset
, uint8_t *typep
,
187 socklen_t
*lenp
, void **databufp
)
190 offset
= sizeof (struct ip6_hbh
);
191 else if (offset
< sizeof (struct ip6_hbh
))
194 while (offset
< extlen
)
196 struct ip6_opt
*opt
= (struct ip6_opt
*) ((uint8_t *) extbuf
+ offset
);
198 if (opt
->ip6o_type
== IP6OPT_PAD1
)
199 /* Single byte padding. */
201 else if (opt
->ip6o_type
== IP6OPT_PADN
)
202 offset
+= sizeof (struct ip6_opt
) + opt
->ip6o_len
;
205 /* Check whether the option is valid. */
206 offset
+= sizeof (struct ip6_opt
) + opt
->ip6o_len
;
210 *typep
= opt
->ip6o_type
;
211 *lenp
= opt
->ip6o_len
;
223 This function is similar to the previously described
224 'inet6_opt_next' function, except this function lets the caller
225 specify the option type to be searched for, instead of always
226 returning the next option in the extension header. */
228 inet6_opt_find (void *extbuf
, socklen_t extlen
, int offset
, uint8_t type
,
229 socklen_t
*lenp
, void **databufp
)
232 offset
= sizeof (struct ip6_hbh
);
233 else if (offset
< sizeof (struct ip6_hbh
))
236 while (offset
< extlen
)
238 struct ip6_opt
*opt
= (struct ip6_opt
*) ((uint8_t *) extbuf
+ offset
);
240 if (opt
->ip6o_type
== IP6OPT_PAD1
)
242 /* Single byte padding. */
244 if (type
== IP6OPT_PAD1
)
247 *databufp
= (uint8_t *) extbuf
+ offset
;
251 else if (opt
->ip6o_type
!= type
)
252 offset
+= sizeof (struct ip6_opt
) + opt
->ip6o_len
;
255 /* Check whether the option is valid. */
256 offset
+= sizeof (struct ip6_opt
) + opt
->ip6o_len
;
260 *lenp
= opt
->ip6o_len
;
272 This function extracts data items of various sizes in the data
273 portion of the option. */
275 inet6_opt_get_val (void *databuf
, int offset
, void *val
, socklen_t vallen
)
277 memcpy (val
, (uint8_t *) databuf
+ offset
, vallen
);
279 return offset
+ vallen
;