1.\"	$NetBSD: inet6_opt_init.3,v 1.2 2014/01/04 15:37:26 wiz Exp $
2.\"	$KAME: inet6_opt_init.3,v 1.7 2004/12/27 05:08:23 itojun Exp $
3.\"
4.\" Copyright (C) 2004 WIDE Project.
5.\" All rights reserved.
6.\"
7.\" Redistribution and use in source and binary forms, with or without
8.\" modification, are permitted provided that the following conditions
9.\" are met:
10.\" 1. Redistributions of source code must retain the above copyright
11.\"    notice, this list of conditions and the following disclaimer.
12.\" 2. Redistributions in binary form must reproduce the above copyright
13.\"    notice, this list of conditions and the following disclaimer in the
14.\"    documentation and/or other materials provided with the distribution.
15.\" 3. Neither the name of the project nor the names of its contributors
16.\"    may be used to endorse or promote products derived from this software
17.\"    without specific prior written permission.
18.\"
19.\" THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29.\" SUCH DAMAGE.
30.\"
31.Dd December 23, 2004
32.Dt INET6_OPT_INIT 3
33.Os
34.\"
35.Sh NAME
36.Nm inet6_opt_init ,
37.Nm inet6_opt_append ,
38.Nm inet6_opt_finish ,
39.Nm inet6_opt_set_val ,
40.Nm inet6_opt_next ,
41.Nm inet6_opt_find ,
42.Nm inet6_opt_get_val
43.Nd IPv6 Hop-by-Hop and Destination Options manipulation
44.\"
45.Sh SYNOPSIS
46.In netinet/in.h
47.Ft "int"
48.Fn inet6_opt_init "void *extbuf" "socklen_t extlen"
49.Ft "int"
50.Fn inet6_opt_append "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t len" "u_int8_t align" "void **databufp"
51.Ft "int"
52.Fn inet6_opt_finish "void *extbuf" "socklen_t extlen" "int offset"
53.Ft "int"
54.Fn inet6_opt_set_val "void *databuf" "int offset" "void *val" "socklen_t vallen"
55.Ft "int"
56.Fn inet6_opt_next "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t *typep" "socklen_t *lenp" "void **databufp"
57.Ft "int"
58.Fn inet6_opt_find "void *extbuf" "socklen_t extlen" "int offset" "u_int8_t type" "socklen_t *lenp" "void **databufp"
59.Ft "int"
60.Fn inet6_opt_get_val "void *databuf" "socklen_t offset" "void *val" "socklen_t vallen"
61.\"
62.Sh DESCRIPTION
63Building and parsing the Hop-by-Hop and Destination options is
64complicated.
65The advanced sockets API defines a set of functions to
66help applications create and manipulate Hop-by-Hope and Destination
67options.
68.\"This man page describes the functions specified in
69.\"IETF Draft RFC3542 while the
70.\".Xr inet6_options_space 3
71.\"man page documents the functions defined in RFC 2292.
72.\"It is expected
73.\"that this set of functions will supersede those in RFC 2292 but for
74.\"the time being both APIs are retained.
75These functions use the
76formatting rules specified in Appendix B in RFC2460, i.e., that the
77largest field is placed last in the option.
78The function prototypes
79for these functions are all contained in the
80.In netinet/in.h
81header file.
82.\"
83.Ss inet6_opt_init
84The
85.Fn inet6_opt_init
86function
87returns the number of bytes needed for an empty
88extension header, one without any options.
89If the
90.Va extbuf
91argument points to a valid section of memory
92then the
93.Fn inet6_opt_init
94function also initializes the extension header's length field.
95When attempting to initialize an extension buffer passed in the
96.Va extbuf argument
97.Fa extlen
98must be a positive multiple of 8 or else the function fails and
99returns \-1 to the caller.
100.\"
101.Ss inet6_opt_append
102The
103.Fn inet6_opt_append
104function can perform to different jobs.
105When a valid
106.Fa extbuf
107argument is supplied it appends an option to the extension buffer and
108returns the updated total length as well as a pointer to the newly
109created option in
110.Fa databufp .
111If the value
112of
113.Fa extbuf
114is
115.Dv NULL
116then the
117.Fn inet6_opt_append function only reports what the total length would
118be if the option were actually appended.
119The
120.Fa len
121and
122.Fa align
123arguments specify the length of the option and the required data
124alignment which must be used when appending the option.
125The
126.Fa offset
127argument should be the length returned by the
128.Fn inet6_opt_init
129function or a previous call to
130.Fn inet6_opt_append .
131.Pp
132The
133.Fa type
134argument is the 8-bit option type.
135.Pp
136After
137.Fn inet6_opt_append
138has been called, the application can use the buffer pointed to by
139.Fa databufp
140directly, or use
141.Fn inet6_opt_set_val
142to specify the data to be contained in the option.
143.Pp
144Option types of
145.Li 0
146and
147.Li 1
148are reserved for the
149.Li Pad1
150and
151.Li PadN
152options.
153All other values from 2 through 255 may be used by applications.
154.Pp
155The length of the option data is contained in an 8-bit value and so
156may contain any value from 0 through 255.
157.Pp
158The
159.Fa align
160parameter must have a value of 1, 2, 4, or 8 and cannot exceed the
161value of
162.Fa len .
163The alignment values represent no alignment, 16 bit, 32 bit and 64 bit
164alignments respectively.
165.\"
166.Ss inet6_opt_finish
167The
168.Fn inet6_opt_finish
169calculates the final padding necessary to make the extension header a
170multiple of 8 bytes, as required by the IPv6 extension header
171specification, and returns the extension header's updated total
172length.
173The
174.Fa offset
175argument should be the length returned by
176.Fn inet6_opt_init
177or
178.Fn inet6_opt_append .
179When
180.Fa extbuf
181is not
182.Dv NULL
183the function also sets up the appropriate padding bytes by inserting a
184Pad1 or PadN option of the proper length.
185.Pp
186If the extension header is too small to contain the proper padding
187then an error of \-1 is returned to the caller.
188.\"
189.Ss inet6_opt_set_val
190The
191.Fn inet6_opt_set_val
192function inserts data items of various sizes into the data portion of
193the option.
194The
195.Fa databuf
196argument is a pointer to memory that was returned by the
197.Fn inet6_opt_append
198call and the
199.Fa offset argument specifies where the option should be placed in the
200data buffer.
201The
202.Fa val
203argument points to an area of memory containing the data to be
204inserted into the extension header, and the
205.Fa vallen
206argument indicates how much data to copy.
207.Pp
208The caller should ensure that each field is aligned on its natural
209boundaries as described in Appendix B of RFC2460.
210.Pp
211The function returns the offset for the next field which is calculated as
212.Fa offset
213+
214.Fa vallen
215and is used when composing options with multiple fields.
216.\"
217.Ss inet6_opt_next
218The
219.Fn inet6_opt_next
220function parses received extension headers.
221The
222.Fa extbuf
223and
224.Fa extlen
225arguments specify the location and length of the extension header
226being parsed.
227The
228.Fa offset
229argument should either be zero, for the first option, or the length value
230returned by a previous call to
231.Fn inet6_opt_next
232or
233.Fn inet6_opt_find .
234The return value specifies the position where to continue scanning the
235extension buffer.
236The option is returned in the arguments
237.Fa typep , lenp ,
238and
239.Fa databufp .
240.Fa typep , lenp ,
241and
242.Fa databufp
243point to the 8-bit option type, the 8-bit option length and the option
244data respectively.
245This function does not return any PAD1 or PADN options.
246When an error occurs or there are no more options the return
247value is \-1.
248.\"
249.Ss inet6_opt_find
250The
251.Fn inet6_opt_find
252function searches the extension buffer for a particular option type,
253passed in through the
254.Fa type
255argument.
256If the option is found then the
257.Fa lenp
258and
259.Fa databufp
260arguments are updated to point to the option's length and data
261respectively.
262.Fa extbuf
263and
264.Fa extlen
265must point to a valid extension buffer and give its length.
266The
267.Fa offset
268argument can be used to search from a location anywhere in the
269extension header.
270.Ss inet6_opt_get_val
271The
272.Fn inet6_opt_get_val
273function extracts data items of various sizes in the data portion of
274the option.
275The
276.Fa databuf
277is a pointer returned by the
278.Fn inet6_opt_next
279or
280.Fn inet6_opt_find
281functions.
282The
283.Fa val
284argument points where the data will be extracted.
285The
286.Fa offset
287argument specifies from where in the data portion of the option the
288value should be extracted; the first byte of option data is specified
289by an offset of zero.
290.Pp
291It is expected that each field is aligned on its natural boundaries as
292described in Appendix B of RFC2460.
293.Pp
294The function returns the offset for the next field
295by calculating
296.Fa offset
297+
298.Fa vallen
299which can be used when extracting option content with multiple fields.
300Robust receivers must verify alignment before calling this function.
301.\"
302.Sh EXAMPLES
303RFC3542 gives comprehensive examples in Section 23.
304.Pp
305KAME also provides examples in the
306.Pa advapitest
307directory of its kit.
308.\"
309.Sh DIAGNOSTICS
310All the functions return
311\-1
312on an error.
313.\"
314.Sh SEE ALSO
315.Rs
316.%A W. Stevens
317.%A M. Thomas
318.%A E. Nordmark
319.%A T. Jinmei
320.%T "Advanced Sockets API for IPv6"
321.%N RFC3542
322.%D October 2002
323.Re
324.Rs
325.%A S. Deering
326.%A R. Hinden
327.%T "Internet Protocol, Version 6 (IPv6) Specification"
328.%N RFC2460
329.%D December 1998
330.Re
331.Sh STANDARDS
332The functions are documented in
333.Dq Advanced Sockets API for IPv6
334.Pq RFC3542 .
335.\"
336.Sh HISTORY
337The implementation first appeared in KAME advanced networking kit.
338