1 .\" $NetBSD: ip6.4,v 1.26 2007/08/20 16:07:05 perry Exp $
2 .\" $KAME: ip6.4,v 1.23 2005/01/11 05:56:25 itojun Exp $
3 .\" $OpenBSD: ip6.4,v 1.21 2005/01/06 03:50:46 itojun Exp $
5 .\" Copyright (c) 1983, 1991, 1993
6 .\" The Regents of the University of California. All rights reserved.
8 .\" Redistribution and use in source and binary forms, with or without
9 .\" modification, are permitted provided that the following conditions
11 .\" 1. Redistributions of source code must retain the above copyright
12 .\" notice, this list of conditions and the following disclaimer.
13 .\" 2. Redistributions in binary form must reproduce the above copyright
14 .\" notice, this list of conditions and the following disclaimer in the
15 .\" documentation and/or other materials provided with the distribution.
16 .\" 3. Neither the name of the University nor the names of its contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 .Nd Internet Protocol version 6 (IPv6) network layer
41 .Fn socket AF_INET6 SOCK_RAW proto
43 The IPv6 network layer is used by the IPv6 protocol family for
45 IPv6 packets contain an IPv6 header that is not provided as part of the
46 payload contents when passed to an application.
47 IPv6 header options affect the behavior of this protocol and may be used
48 by high-level protocols (such as the
52 protocols) as well as directly by
54 which process IPv6 messages at a lower-level and may be useful for
55 developing new protocols and special-purpose applications.
57 All IPv6 packets begin with an IPv6 header.
58 When data received by the kernel are passed to the application, this
59 header is not included in buffer, even when raw sockets are being used.
60 Likewise, when data are sent to the kernel for transmit from the
61 application, the buffer is not examined for an IPv6 header:
62 the kernel always constructs the header.
63 To directly access IPv6 headers from received packets and specify them
64 as part of the buffer passed to the kernel, link-level access
71 The header has the following definition:
72 .Bd -literal -offset indent
76 uint32_t ip6_un1_flow; /* 20 bits of flow ID */
77 uint16_t ip6_un1_plen; /* payload length */
78 uint8_t ip6_un1_nxt; /* next header */
79 uint8_t ip6_un1_hlim; /* hop limit */
81 uint8_t ip6_un2_vfc; /* version and class */
83 struct in6_addr ip6_src; /* source address */
84 struct in6_addr ip6_dst; /* destination address */
87 #define ip6_vfc ip6_ctlun.ip6_un2_vfc
88 #define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_flow
89 #define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_plen
90 #define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_nxt
91 #define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_hlim
92 #define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_hlim
95 All fields are in network-byte order.
96 Any options specified (see
98 below) must also be specified in network-byte order.
101 specifies the flow ID.
103 specifies the payload length.
105 specifies the type of the next header.
107 specifies the hop limit.
111 specify the class and the bottom 4 bits specify the version.
116 specify the source and destination addresses.
118 The IPv6 header may be followed by any number of extension headers that start
119 with the following generic definition:
120 .Bd -literal -offset indent
127 IPv6 allows header options on packets to manipulate the behavior of the
129 These options and other control requests are accessed with the
133 system calls at level
135 and by using ancillary data in
139 They can be used to access most of the fields in the IPv6 header and
142 The following socket options are supported:
144 .\" .It Dv IPV6_OPTIONS
145 .It Dv IPV6_UNICAST_HOPS Fa "int *"
146 Get or set the default hop limit header field for outgoing unicast
147 datagrams sent on this socket.
148 A value of \-1 resets to the default value.
149 .\" .It Dv IPV6_RECVOPTS Fa "int *"
150 .\" Get or set the status of whether all header options will be
151 .\" delivered along with the datagram when it is received.
152 .\" .It Dv IPV6_RECVRETOPTS Fa "int *"
153 .\" Get or set the status of whether header options will be delivered
155 .\" .It Dv IPV6_RECVDSTADDR Fa "int *"
156 .\" Get or set the status of whether datagrams are received with
157 .\" destination addresses.
158 .\" .It Dv IPV6_RETOPTS
159 .\" Get or set IPv6 options.
160 .It Dv IPV6_MULTICAST_IF Fa "u_int *"
161 Get or set the interface from which multicast packets will be sent.
162 For hosts with multiple interfaces, each multicast transmission is sent
163 from the primary network interface.
164 The interface is specified as its index as provided by
165 .Xr if_nametoindex 3 .
166 A value of zero specifies the default interface.
167 .It Dv IPV6_MULTICAST_HOPS Fa "int *"
168 Get or set the default hop limit header field for outgoing multicast
169 datagrams sent on this socket.
170 This option controls the scope of multicast datagram transmissions.
172 Datagrams with a hop limit of 1 are not forwarded beyond the local
174 Multicast datagrams with a hop limit of zero will not be transmitted on
175 any network but may be delivered locally if the sending host belongs to
176 the destination group and if multicast loopback (see below) has not been
177 disabled on the sending socket.
178 Multicast datagrams with a hop limit greater than 1 may be forwarded to
179 the other networks if a multicast router (such as
181 is attached to the local network.
182 .It Dv IPV6_MULTICAST_LOOP Fa "u_int *"
183 Get or set the status of whether multicast datagrams will be looped back
184 for local delivery when a multicast datagram is sent to a group to which
185 the sending host belongs.
187 This option improves performance for applications that may have no more
188 than one instance on a single host (such as a router daemon) by
189 eliminating the overhead of receiving their own transmissions.
190 It should generally not be used by applications for which there may be
191 more than one instance on a single host (such as a conferencing program)
192 or for which the sender does not belong to the destination group
193 (such as a time-querying program).
195 A multicast datagram sent with an initial hop limit greater than 1 may
196 be delivered to the sending host on a different interface from that on
197 which it was sent if the host belongs to the destination group on that
199 The multicast loopback control option has no effect on such delivery.
200 .It Dv IPV6_JOIN_GROUP Fa "struct ipv6_mreq *"
201 Join a multicast group.
202 A host must become a member of a multicast group before it can receive
203 datagrams sent to the group.
206 struct in6_addr ipv6mr_multiaddr;
207 unsigned int ipv6mr_interface;
212 may be set to zeroes to choose the default multicast interface or to the
213 index of a particular multicast-capable interface if the host is
215 Membership is associated with a single interface; programs running on
216 multihomed hosts may need to join the same group on more than one
219 If the multicast address is unspecified (i.e., all zeroes), messages
220 from all multicast addresses will be accepted by this group.
221 Note that setting to this value requires superuser privileges.
222 .It Dv IPV6_LEAVE_GROUP Fa "struct ipv6_mreq *"
223 Drop membership from the associated multicast group.
224 Memberships are automatically dropped when the socket is closed or when
226 .It Dv IPV6_IPSEC_POLICY Fa "struct sadb_x_policy *"
227 Get or set IPSec policy for sockets.
230 const char *policy = "in ipsec ah/transport//require";
231 char *buf = ipsec_set_policy(policy, strlen(policy));
232 setsockopt(s, IPPROTO_IPV6, IPV6_IPSEC_POLICY, buf, ipsec_get_policylen(buf));
234 .It Dv IPV6_PORTRANGE Fa "int *"
235 Get or set the allocation policy of ephemeral ports for when the kernel
236 automatically binds a local address to this socket.
237 The following values are available:
239 .Bl -tag -width IPV6_PORTRANGE_DEFAULT -compact
240 .It Dv IPV6_PORTRANGE_DEFAULT
241 Use the regular range of non-reserved ports (varies, see
243 .It Dv IPV6_PORTRANGE_HIGH
244 Use a high range (varies, see
246 .It Dv IPV6_PORTRANGE_LOW
247 Use a low, reserved range (600\-1023).
249 .It Dv IPV6_PKTINFO Fa "int *"
250 Get or set whether additional information about subsequent packets will
251 be provided as ancillary data along with the payload in subsequent
254 The information is stored in the following structure in the ancillary
258 struct in6_addr ipi6_addr; /* src/dst IPv6 address */
259 unsigned int ipi6_ifindex; /* send/recv if index */
262 .It Dv IPV6_HOPLIMIT Fa "int *"
263 Get or set whether the hop limit header field from subsequent packets
264 will be provided as ancillary data along with the payload in subsequent
267 The value is stored as an
269 in the ancillary data returned.
270 .\" .It Dv IPV6_NEXTHOP Fa "int *"
271 .\" Get or set whether the address of the next hop for subsequent
272 .\" packets will be provided as ancillary data along with the payload in
276 .\" The option is stored as a
278 .\" structure in the ancillary data returned.
280 .\" This option requires superuser privileges.
281 .It Dv IPV6_HOPOPTS Fa "int *"
282 Get or set whether the hop-by-hop options from subsequent packets will be
283 provided as ancillary data along with the payload in subsequent
286 The option is stored in the following structure in the ancillary data
290 uint8_t ip6h_nxt; /* next header */
291 uint8_t ip6h_len; /* length in units of 8 octets */
292 /* followed by options */
297 .Fn inet6_option_space
298 routine and family of routines may be used to manipulate this data.
300 This option requires superuser privileges.
301 .It Dv IPV6_DSTOPTS Fa "int *"
302 Get or set whether the destination options from subsequent packets will
303 be provided as ancillary data along with the payload in subsequent
306 The option is stored in the following structure in the ancillary data
310 uint8_t ip6d_nxt; /* next header */
311 uint8_t ip6d_len; /* length in units of 8 octets */
312 /* followed by options */
317 .Fn inet6_option_space
318 routine and family of routines may be used to manipulate this data.
320 This option requires superuser privileges.
321 .It Dv IPV6_RTHDR Fa "int *"
322 Get or set whether the routing header from subsequent packets will be
323 provided as ancillary data along with the payload in subsequent
326 The header is stored in the following structure in the ancillary data
330 uint8_t ip6r_nxt; /* next header */
331 uint8_t ip6r_len; /* length in units of 8 octets */
332 uint8_t ip6r_type; /* routing type */
333 uint8_t ip6r_segleft; /* segments left */
334 /* followed by routing-type-specific data */
339 .Fn inet6_option_space
340 routine and family of routines may be used to manipulate this data.
342 This option requires superuser privileges.
343 .It Dv IPV6_PKTOPTIONS Fa "struct cmsghdr *"
344 Get or set all header options and extension headers at one time on the
345 last packet sent or received on the socket.
346 All options must fit within the size of an mbuf (see
348 Options are specified as a series of
350 structures followed by corresponding values.
355 to one of the other values in this list, and trailing data to the option
357 When setting options, if the length
361 is zero, all header options will be reset to their default values.
362 Otherwise, the length should specify the size the series of control
367 to specify option values, the ancillary data used in these calls that
368 correspond to the desired header options may be directly specified as
369 the control message in the series of control messages provided as the
372 .It Dv IPV6_CHECKSUM Fa "int *"
373 Get or set the byte offset into a packet where the 16-bit checksum is
375 When set, this byte offset is where incoming packets will be expected
376 to have checksums of their data stored and where outgoing packets will
377 have checksums of their data computed and stored by the kernel.
378 A value of \-1 specifies that no checksums will be checked on incoming
379 packets and that no checksums will be computed or stored on outgoing
381 The offset of the checksum for ICMPv6 sockets cannot be relocated or
383 .It Dv IPV6_V6ONLY Fa "int *"
384 Get or set whether only IPv6 connections can be made to this socket.
385 For wildcard sockets, this can restrict connections to IPv6 only.
388 .\"IPv6 sockets are always IPv6-only, so the socket option is read-only
390 .It Dv IPV6_FAITH Fa "int *"
391 Get or set the status of whether
393 connections can be made to this socket.
394 .It Dv IPV6_USE_MIN_MTU Fa "int *"
395 Get or set whether the minimal IPv6 maximum transmission unit (MTU) size
396 will be used to avoid fragmentation from occurring for subsequent
398 .It Dv IPV6_AUTH_LEVEL Fa "int *"
401 authentication level.
402 .It Dv IPV6_ESP_TRANS_LEVEL Fa "int *"
403 Get or set the ESP transport level.
404 .It Dv IPV6_ESP_NETWORK_LEVEL Fa "int *"
405 Get or set the ESP encapsulation level.
406 .It Dv IPV6_IPCOMP_LEVEL Fa "int *"
414 .\" .Dv IPV6_NEXTHOP ,
420 options will return ancillary data along with payload contents in subsequent
428 set to respective option name value (e.g.,
429 .Dv IPV6_HOPTLIMIT ) .
430 These options may also be used directly as ancillary
434 to set options on the packet being transmitted by the call.
439 For these options, the ancillary data object value format is the same
440 as the value returned as explained for each when received with
445 to specify options on particular packets works only on UDP and raw sockets.
446 To manipulate header options for packets on TCP sockets, only the socket
449 In some cases, there are multiple APIs defined for manipulating an IPv6
451 A good example is the outgoing interface for multicast datagrams, which
453 .Dv IPV6_MULTICAST_IF
454 socket option, through the
456 option, and through the
458 field of the socket address passed to the
462 Resolving these conflicts is implementation dependent.
463 This implementation determines the value in the following way:
464 options specified by using ancillary data (i.e.,
466 are considered first,
467 options specified by using
471 options are considered second,
472 options specified by using the individual, basic, and direct socket
474 .Dv IPV6_UNICAST_HOPS )
475 are considered third,
476 and options specified in the socket address supplied to
480 IPv6 multicasting is supported only on
486 and only on networks where the interface driver supports
488 Socket options (see above) that manipulate membership of
489 multicast groups and other multicast options include
490 .Dv IPV6_MULTICAST_IF ,
491 .Dv IPV6_MULTICAST_HOPS ,
492 .Dv IPV6_MULTICAST_LOOP ,
493 .Dv IPV6_LEAVE_GROUP ,
495 .Dv IPV6_JOIN_GROUP .
497 Raw IPv6 sockets are connectionless and are normally used with the
503 call may be used to fix the destination address for future outgoing
506 may instead be used and the
508 call may be used to fix the source address for future outgoing
509 packets instead of having the kernel choose a source address.
515 raw socket input is constrained to only packets with their
516 source address matching the socket destination address if
518 was used and to packets with their destination address
519 matching the socket source address if
527 is zero, the default protocol
529 is used for outgoing packets.
530 For incoming packets, protocols recognized by kernel are
532 passed to the application socket (e.g.,
536 except for some ICMPv6 messages.
537 The ICMPv6 messages not passed to raw sockets include echo, timestamp,
538 and address mask requests.
541 is non-zero, only packets with this protocol will be passed to the
544 IPv6 fragments are also not passed to application sockets until
545 they have been reassembled.
546 If reception of all packets is desired, link-level access (such as
548 must be used instead.
550 Outgoing packets automatically have an IPv6 header prepended to them
551 (based on the destination address and the protocol number the socket
553 Incoming packets are received by an application without the IPv6 header
554 or any extension headers.
556 Outgoing packets will be fragmented automatically by the kernel if they
558 Incoming packets will be reassembled before being sent to the raw socket,
559 so packet fragments or fragment headers will never be seen on a raw socket.
561 The following determines the hop limit on the next packet received:
572 (void)memset(\*[Am]m, 0, sizeof(m));
573 (void)memset(\*[Am]iov, 0, sizeof(iov));
575 iov[0].iov_base = data; /* buffer for packet payload */
576 iov[0].iov_len = sizeof(data); /* expected packet length */
578 m.msg_name = \*[Am]from; /* sockaddr_in6 of peer */
579 m.msg_namelen = sizeof(from);
582 m.msg_control = buf; /* buffer for control messages */
583 m.msg_controllen = sizeof(buf);
586 * Enable the hop limit value from received packets to be
587 * returned along with the payload.
590 if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, \*[Am]optval,
591 sizeof(optval)) == -1)
592 err(1, "setsockopt");
596 if (recvmsg(s, \*[Am]m, 0) == -1)
598 for (cm = CMSG_FIRSTHDR(\*[Am]m); cm != NULL;
599 cm = CMSG_NXTHDR(\*[Am]m, cm)) {
600 if (cm-\*[Gt]cmsg_level == IPPROTO_IPV6 \*[Am]\*[Am]
601 cm-\*[Gt]cmsg_type == IPV6_HOPLIMIT \*[Am]\*[Am]
602 cm-\*[Gt]cmsg_len == CMSG_LEN(sizeof(int))) {
604 (void)printf("hop limit: %d\en",
605 *(int *)CMSG_DATA(cm));
612 A socket operation may fail with one of the following errors returned:
613 .Bl -tag -width EADDRNOTAVAILxx
615 when trying to establish a connection on a socket which
616 already has one or when trying to send a datagram with the destination
617 address specified and the socket is already connected.
619 when trying to send a datagram, but
620 no destination address is specified, and the socket hasn't been
623 when the system runs out of memory for
624 an internal data structure.
625 .It Bq Er EADDRNOTAVAIL
626 when an attempt is made to create a
627 socket with a network address for which no network interface
630 when an attempt is made to create
631 a raw IPv6 socket by a non-privileged process.
634 The following errors specific to IPv6 may occur when setting or getting
636 .Bl -tag -width EADDRNOTAVAILxx
638 An unknown socket option name was given.
640 An ancillary data object was improperly formed.
648 .\" .Xr inet6_option_space 3 ,
649 .\" .Xr inet6_rthdr_space 3 ,
650 .Xr if_nametoindex 3 ,
660 .%T Advanced Sockets API for IPv6
667 .%T Internet Protocol, Version 6 (IPv6) Specification
676 .%T Basic Socket Interface Extensions for IPv6
684 .%T UNIX Network Programming, third edition
687 Most of the socket options are defined in RFC 2292 or RFC 2553.
690 socket option is defined in RFC 3542.
693 socket option and the conflict resolution rule are not defined in the
694 RFCs and should be considered implementation dependent.