1 .\" $KAME: ip6.4,v 1.23 2005/01/11 05:56:25 itojun Exp $
2 .\" $OpenBSD: ip6.4,v 1.21 2005/01/06 03:50:46 itojun Exp $
4 .\" Copyright (c) 1983, 1991, 1993
5 .\" The Regents of the University of California. All rights reserved.
7 .\" Redistribution and use in source and binary forms, with or without
8 .\" modification, are permitted provided that the following conditions
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 University 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.
19 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS 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 REGENTS 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
33 .Dd September 29, 2006
38 .Nd Internet Protocol version 6 (IPv6) network layer
43 .Fn socket AF_INET6 SOCK_RAW proto
45 The IPv6 network layer is used by the IPv6 protocol family for
47 IPv6 packets contain an IPv6 header that is not provided as part of the
48 payload contents when passed to an application.
49 IPv6 header options affect the behavior of this protocol and may be used
50 by high-level protocols (such as the
54 protocols) as well as directly by
56 which process IPv6 messages at a lower-level and may be useful for
57 developing new protocols and special-purpose applications.
59 All IPv6 packets begin with an IPv6 header.
60 When data received by the kernel are passed to the application, this
61 header is not included in buffer, even when raw sockets are being used.
62 Likewise, when data are sent to the kernel for transmit from the
63 application, the buffer is not examined for an IPv6 header:
64 the kernel always constructs the header.
65 To directly access IPv6 headers from received packets and specify them
66 as part of the buffer passed to the kernel, link-level access
71 must instead be utilized.
73 The header has the following definition:
74 .Bd -literal -offset indent
78 u_int32_t ip6_un1_flow; /* 20 bits of flow ID */
79 u_int16_t ip6_un1_plen; /* payload length */
80 u_int8_t ip6_un1_nxt; /* next header */
81 u_int8_t ip6_un1_hlim; /* hop limit */
83 u_int8_t ip6_un2_vfc; /* version and class */
85 struct in6_addr ip6_src; /* source address */
86 struct in6_addr ip6_dst; /* destination address */
89 #define ip6_vfc ip6_ctlun.ip6_un2_vfc
90 #define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow
91 #define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen
92 #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
93 #define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim
94 #define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim
97 All fields are in network-byte order.
98 Any options specified (see
100 below) must also be specified in network-byte order.
103 specifies the flow ID.
105 specifies the payload length.
107 specifies the type of the next header.
109 specifies the hop limit.
113 specify the class and the bottom 4 bits specify the version.
118 specify the source and destination addresses.
120 The IPv6 header may be followed by any number of extension headers that start
121 with the following generic definition:
122 .Bd -literal -offset indent
129 IPv6 allows header options on packets to manipulate the behavior of the
131 These options and other control requests are accessed with the
135 system calls at level
137 and by using ancillary data in
141 They can be used to access most of the fields in the IPv6 header and
144 The following socket options are supported:
146 .\" .It Dv IPV6_OPTIONS
147 .It Dv IPV6_UNICAST_HOPS Fa "int *"
148 Get or set the default hop limit header field for outgoing unicast
149 datagrams sent on this socket.
150 A value of \-1 resets to the default value.
151 .\" .It Dv IPV6_RECVOPTS Fa "int *"
152 .\" Get or set the status of whether all header options will be
153 .\" delivered along with the datagram when it is received.
154 .\" .It Dv IPV6_RECVRETOPTS Fa "int *"
155 .\" Get or set the status of whether header options will be delivered
157 .\" .It Dv IPV6_RECVDSTADDR Fa "int *"
158 .\" Get or set the status of whether datagrams are received with
159 .\" destination addresses.
160 .\" .It Dv IPV6_RETOPTS
161 .\" Get or set IPv6 options.
162 .It Dv IPV6_MULTICAST_IF Fa "u_int *"
163 Get or set the interface from which multicast packets will be sent.
164 For hosts with multiple interfaces, each multicast transmission is sent
165 from the primary network interface.
166 The interface is specified as its index as provided by
167 .Xr if_nametoindex 3 .
168 A value of zero specifies the default interface.
169 .It Dv IPV6_MULTICAST_HOPS Fa "int *"
170 Get or set the default hop limit header field for outgoing multicast
171 datagrams sent on this socket.
172 This option controls the scope of multicast datagram transmissions.
174 Datagrams with a hop limit of 1 are not forwarded beyond the local
176 Multicast datagrams with a hop limit of zero will not be transmitted on
177 any network but may be delivered locally if the sending host belongs to
178 the destination group and if multicast loopback (see below) has not been
179 disabled on the sending socket.
180 Multicast datagrams with a hop limit greater than 1 may be forwarded to
181 the other networks if a multicast router (such as
182 .Xr mrouted 8 Pq Pa ports/net/mrouted )
183 is attached to the local network.
184 .It Dv IPV6_MULTICAST_LOOP Fa "u_int *"
185 Get or set the status of whether multicast datagrams will be looped back
186 for local delivery when a multicast datagram is sent to a group to which
187 the sending host belongs.
189 This option improves performance for applications that may have no more
190 than one instance on a single host (such as a router daemon) by
191 eliminating the overhead of receiving their own transmissions.
192 It should generally not be used by applications for which there may be
193 more than one instance on a single host (such as a conferencing program)
194 or for which the sender does not belong to the destination group
195 (such as a time-querying program).
197 A multicast datagram sent with an initial hop limit greater than 1 may
198 be delivered to the sending host on a different interface from that on
199 which it was sent if the host belongs to the destination group on that
201 The multicast loopback control option has no effect on such delivery.
202 .It Dv IPV6_JOIN_GROUP Fa "struct ipv6_mreq *"
203 Join a multicast group.
204 A host must become a member of a multicast group before it can receive
205 datagrams sent to the group.
208 struct in6_addr ipv6mr_multiaddr;
209 unsigned int ipv6mr_interface;
214 may be set to zeroes to choose the default multicast interface or to the
215 index of a particular multicast-capable interface if the host is
217 Membership is associated with a single interface; programs running on
218 multihomed hosts may need to join the same group on more than one
221 If the multicast address is unspecified (i.e., all zeroes), messages
222 from all multicast addresses will be accepted by this group.
223 Note that setting to this value requires superuser privileges.
224 .It Dv IPV6_LEAVE_GROUP Fa "struct ipv6_mreq *"
225 Drop membership from the associated multicast group.
226 Memberships are automatically dropped when the socket is closed or when
228 .It Dv IPV6_PORTRANGE Fa "int *"
229 Get or set the allocation policy of ephemeral ports for when the kernel
230 automatically binds a local address to this socket.
231 The following values are available:
233 .Bl -tag -width IPV6_PORTRANGE_DEFAULT -compact
234 .It Dv IPV6_PORTRANGE_DEFAULT
235 Use the regular range of non-reserved ports (varies, see
237 .It Dv IPV6_PORTRANGE_HIGH
238 Use a high range (varies, see
240 .It Dv IPV6_PORTRANGE_LOW
241 Use a low, reserved range (600\-1023).
243 .It Dv IPV6_PKTINFO Fa "int *"
244 Get or set whether additional information about subsequent packets will
245 be provided as ancillary data along with the payload in subsequent
248 The information is stored in the following structure in the ancillary
252 struct in6_addr ipi6_addr; /* src/dst IPv6 address */
253 unsigned int ipi6_ifindex; /* send/recv if index */
256 .It Dv IPV6_HOPLIMIT Fa "int *"
257 Get or set whether the hop limit header field from subsequent packets
258 will be provided as ancillary data along with the payload in subsequent
261 The value is stored as an
263 in the ancillary data returned.
264 .\" .It Dv IPV6_NEXTHOP Fa "int *"
265 .\" Get or set whether the address of the next hop for subsequent
266 .\" packets will be provided as ancillary data along with the payload in
270 .\" The option is stored as a
272 .\" structure in the ancillary data returned.
274 .\" This option requires superuser privileges.
275 .It Dv IPV6_HOPOPTS Fa "int *"
276 Get or set whether the hop-by-hop options from subsequent packets will be
277 provided as ancillary data along with the payload in subsequent
280 The option is stored in the following structure in the ancillary data
284 u_int8_t ip6h_nxt; /* next header */
285 u_int8_t ip6h_len; /* length in units of 8 octets */
286 /* followed by options */
291 .Fn inet6_option_space
292 routine and family of routines may be used to manipulate this data.
294 This option requires superuser privileges.
295 .It Dv IPV6_DSTOPTS Fa "int *"
296 Get or set whether the destination options from subsequent packets will
297 be provided as ancillary data along with the payload in subsequent
300 The option is stored in the following structure in the ancillary data
304 u_int8_t ip6d_nxt; /* next header */
305 u_int8_t ip6d_len; /* length in units of 8 octets */
306 /* followed by options */
311 .Fn inet6_option_space
312 routine and family of routines may be used to manipulate this data.
314 This option requires superuser privileges.
315 .It Dv IPV6_RTHDR Fa "int *"
316 Get or set whether the routing header from subsequent packets will be
317 provided as ancillary data along with the payload in subsequent
320 The header is stored in the following structure in the ancillary data
324 u_int8_t ip6r_nxt; /* next header */
325 u_int8_t ip6r_len; /* length in units of 8 octets */
326 u_int8_t ip6r_type; /* routing type */
327 u_int8_t ip6r_segleft; /* segments left */
328 /* followed by routing-type-specific data */
333 .Fn inet6_option_space
334 routine and family of routines may be used to manipulate this data.
336 This option requires superuser privileges.
337 .It Dv IPV6_PKTOPTIONS Fa "struct cmsghdr *"
338 Get or set all header options and extension headers at one time on the
339 last packet sent or received on the socket.
340 All options must fit within the size of an mbuf (see
342 Options are specified as a series of
344 structures followed by corresponding values.
349 to one of the other values in this list, and trailing data to the option
351 When setting options, if the length
355 is zero, all header options will be reset to their default values.
356 Otherwise, the length should specify the size the series of control
361 to specify option values, the ancillary data used in these calls that
362 correspond to the desired header options may be directly specified as
363 the control message in the series of control messages provided as the
366 .It Dv IPV6_CHECKSUM Fa "int *"
367 Get or set the byte offset into a packet where the 16-bit checksum is
369 When set, this byte offset is where incoming packets will be expected
370 to have checksums of their data stored and where outgoing packets will
371 have checksums of their data computed and stored by the kernel.
372 A value of \-1 specifies that no checksums will be checked on incoming
373 packets and that no checksums will be computed or stored on outgoing
375 The offset of the checksum for ICMPv6 sockets cannot be relocated or
377 .It Dv IPV6_V6ONLY Fa "int *"
378 Get or set whether only IPv6 connections can be made to this socket.
379 For wildcard sockets, this can restrict connections to IPv6 only.
382 .\"IPv6 sockets are always IPv6-only, so the socket option is read-only
384 .It Dv IPV6_FAITH Fa "int *"
385 Get or set the status of whether
387 connections can be made to this socket.
388 .It Dv IPV6_USE_MIN_MTU Fa "int *"
389 Get or set whether the minimal IPv6 maximum transmission unit (MTU) size
390 will be used to avoid fragmentation from occurring for subsequent
392 .It Dv IPV6_AUTH_LEVEL Fa "int *"
395 authentication level.
396 .It Dv IPV6_ESP_TRANS_LEVEL Fa "int *"
397 Get or set the ESP transport level.
398 .It Dv IPV6_ESP_NETWORK_LEVEL Fa "int *"
399 Get or set the ESP encapsulation level.
400 .It Dv IPV6_IPCOMP_LEVEL Fa "int *"
408 .\" .Dv IPV6_NEXTHOP ,
414 options will return ancillary data along with payload contents in subsequent
422 set to respective option name value (e.g.,
423 .Dv IPV6_HOPTLIMIT ) .
424 These options may also be used directly as ancillary
428 to set options on the packet being transmitted by the call.
433 For these options, the ancillary data object value format is the same
434 as the value returned as explained for each when received with
439 to specify options on particular packets works only on UDP and raw sockets.
440 To manipulate header options for packets on TCP sockets, only the socket
443 In some cases, there are multiple APIs defined for manipulating an IPv6
445 A good example is the outgoing interface for multicast datagrams, which
447 .Dv IPV6_MULTICAST_IF
448 socket option, through the
450 option, and through the
452 field of the socket address passed to the
456 Resolving these conflicts is implementation dependent.
457 This implementation determines the value in the following way:
458 options specified by using ancillary data (i.e.,
460 are considered first,
461 options specified by using
465 options are considered second,
466 options specified by using the individual, basic, and direct socket
468 .Dv IPV6_UNICAST_HOPS )
469 are considered third,
470 and options specified in the socket address supplied to
474 IPv6 multicasting is supported only on
480 and only on networks where the interface driver supports
482 Socket options (see above) that manipulate membership of
483 multicast groups and other multicast options include
484 .Dv IPV6_MULTICAST_IF ,
485 .Dv IPV6_MULTICAST_HOPS ,
486 .Dv IPV6_MULTICAST_LOOP ,
487 .Dv IPV6_LEAVE_GROUP ,
489 .Dv IPV6_JOIN_GROUP .
491 Raw IPv6 sockets are connectionless and are normally used with the
497 call may be used to fix the destination address for future outgoing
500 may instead be used and the
502 call may be used to fix the source address for future outgoing
503 packets instead of having the kernel choose a source address.
509 raw socket input is constrained to only packets with their
510 source address matching the socket destination address if
512 was used and to packets with their destination address
513 matching the socket source address if
521 is zero, the default protocol
523 is used for outgoing packets.
524 For incoming packets, protocols recognized by kernel are
526 passed to the application socket (e.g.,
530 except for some ICMPv6 messages.
531 The ICMPv6 messages not passed to raw sockets include echo, timestamp,
532 and address mask requests.
535 is non-zero, only packets with this protocol will be passed to the
538 IPv6 fragments are also not passed to application sockets until
539 they have been reassembled.
540 If reception of all packets is desired, link-level access (such as
542 must be used instead.
544 Outgoing packets automatically have an IPv6 header prepended to them
545 (based on the destination address and the protocol number the socket
547 Incoming packets are received by an application without the IPv6 header
548 or any extension headers.
550 Outgoing packets will be fragmented automatically by the kernel if they
552 Incoming packets will be reassembled before being sent to the raw socket,
553 so packet fragments or fragment headers will never be seen on a raw socket.
555 The following determines the hop limit on the next packet received:
566 (void)memset(&m, 0, sizeof(m));
567 (void)memset(&iov, 0, sizeof(iov));
569 iov[0].iov_base = data; /* buffer for packet payload */
570 iov[0].iov_len = sizeof(data); /* expected packet length */
572 m.msg_name = &from; /* sockaddr_in6 of peer */
573 m.msg_namelen = sizeof(from);
576 m.msg_control = (caddr_t)buf; /* buffer for control messages */
577 m.msg_controllen = sizeof(buf);
580 * Enable the hop limit value from received packets to be
581 * returned along with the payload.
584 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &optval,
585 sizeof(optval)) == -1)
586 err(1, "setsockopt");
590 if (recvmsg(s, &m, 0) == -1)
592 for (cm = CMSG_FIRSTHDR(&m); cm != NULL;
593 cm = CMSG_NXTHDR(&m, cm)) {
594 if (cm->cmsg_level == IPPROTO_IPV6 &&
595 cm->cmsg_type == IPV6_HOPLIMIT &&
596 cm->cmsg_len == CMSG_LEN(sizeof(int))) {
598 (void)printf("hop limit: %d\en",
599 *(int *)CMSG_DATA(cm));
606 A socket operation may fail with one of the following errors returned:
607 .Bl -tag -width EADDRNOTAVAILxx
609 when trying to establish a connection on a socket which
610 already has one or when trying to send a datagram with the destination
611 address specified and the socket is already connected.
613 when trying to send a datagram, but
614 no destination address is specified, and the socket has not been
617 when the system runs out of memory for
618 an internal data structure.
619 .It Bq Er EADDRNOTAVAIL
620 when an attempt is made to create a
621 socket with a network address for which no network interface
624 when an attempt is made to create
625 a raw IPv6 socket by a non-privileged process.
628 The following errors specific to IPv6 may occur when setting or getting
630 .Bl -tag -width EADDRNOTAVAILxx
632 An unknown socket option name was given.
634 An ancillary data object was improperly formed.
642 .\" .Xr inet6_option_space 3 ,
643 .\" .Xr inet6_rthdr_space 3 ,
644 .Xr if_nametoindex 3 ,
654 .%T Advanced Sockets API for IPv6
661 .%T Internet Protocol, Version 6 (IPv6) Specification
670 .%T Basic Socket Interface Extensions for IPv6
678 .%T UNIX Network Programming, third edition
681 Most of the socket options are defined in RFC 2292 or RFC 2553.
684 socket option is defined in RFC 3542.
687 socket option and the conflict resolution rule are not defined in the
688 RFCs and should be considered implementation dependent.