xref: /openbsd/share/man/man4/ip6.4 (revision 73471bf0)
1.\"	$OpenBSD: ip6.4,v 1.40 2016/06/28 17:32:58 jca Exp $
2.\"
3.\" Copyright (c) 1983, 1991, 1993
4.\"	The Regents of the University of California.  All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\" 3. Neither the name of the University nor the names of its contributors
15.\"    may be used to endorse or promote products derived from this software
16.\"    without specific prior written permission.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
19.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
22.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28.\" SUCH DAMAGE.
29.Dd $Mdocdate: June 28 2016 $
30.Dt IP6 4
31.Os
32.Sh NAME
33.Nm ip6
34.Nd Internet Protocol version 6 (IPv6) network layer
35.Sh SYNOPSIS
36.In sys/types.h
37.In sys/socket.h
38.In netinet/in.h
39.Ft int
40.Fn socket AF_INET6 SOCK_RAW proto
41.Sh DESCRIPTION
42The IPv6 network layer is used by the IPv6 protocol family for
43transporting data.
44IPv6 packets contain an IPv6 header that is not provided as part of the
45payload contents when passed to an application.
46IPv6 header options affect the behavior of this protocol and may be used
47by high-level protocols (such as the
48.Xr tcp 4
49and
50.Xr udp 4
51protocols) as well as directly by
52.Dq raw sockets ,
53which process IPv6 messages at a lower-level and may be useful for
54developing new protocols and special-purpose applications.
55.Ss Header
56All IPv6 packets begin with an IPv6 header.
57When data received by the kernel are passed to the application, this
58header is not included in buffer, even when raw sockets are being used.
59Likewise, when data are sent to the kernel for transmit from the
60application, the buffer is not examined for an IPv6 header:
61the kernel always constructs the header.
62To directly access IPv6 headers from received packets and specify them
63as part of the buffer passed to the kernel, link-level access
64.Po
65.Xr bpf 4 ,
66for example
67.Pc
68must instead be utilized.
69.Pp
70The header has the following definition:
71.Bd -literal -offset indent
72struct ip6_hdr {
73     union {
74          struct ip6_hdrctl {
75               u_int32_t ip6_un1_flow;	/* 20 bits of flow ID */
76               u_int16_t ip6_un1_plen;	/* payload length */
77               u_int8_t	 ip6_un1_nxt;	/* next header */
78               u_int8_t	 ip6_un1_hlim;	/* hop limit */
79          } ip6_un1;
80          u_int8_t ip6_un2_vfc;   /* version and class */
81     } ip6_ctlun;
82     struct in6_addr ip6_src;	/* source address */
83     struct in6_addr ip6_dst;	/* destination address */
84} __packed;
85
86#define ip6_vfc		ip6_ctlun.ip6_un2_vfc
87#define ip6_flow	ip6_ctlun.ip6_un1.ip6_un1_flow
88#define ip6_plen	ip6_ctlun.ip6_un1.ip6_un1_plen
89#define ip6_nxt		ip6_ctlun.ip6_un1.ip6_un1_nxt
90#define ip6_hlim	ip6_ctlun.ip6_un1.ip6_un1_hlim
91#define ip6_hops	ip6_ctlun.ip6_un1.ip6_un1_hlim
92.Ed
93.Pp
94All fields are in network-byte order.
95Any options specified (see
96.Sx Options
97below) must also be specified in network-byte order.
98.Pp
99.Va ip6_flow
100specifies the flow ID.
101.Va ip6_plen
102specifies the payload length.
103.Va ip6_nxt
104specifies the type of the next header.
105.Va ip6_hlim
106specifies the hop limit.
107.Pp
108The top 4 bits of
109.Va ip6_vfc
110specify the class and the bottom 4 bits specify the version.
111.Pp
112.Va ip6_src
113and
114.Va ip6_dst
115specify the source and destination addresses.
116.Pp
117The IPv6 header may be followed by any number of extension headers that start
118with the following generic definition:
119.Bd -literal -offset indent
120struct ip6_ext {
121     u_int8_t ip6e_nxt;
122     u_int8_t ip6e_len;
123} __packed;
124.Ed
125.Ss Options
126IPv6 allows header options on packets to manipulate the behavior of the
127protocol.
128These options and other control requests are accessed with the
129.Xr getsockopt 2
130and
131.Xr setsockopt 2
132system calls at level
133.Dv IPPROTO_IPV6
134and by using ancillary data in
135.Xr recvmsg 2
136and
137.Xr sendmsg 2 .
138They can be used to access most of the fields in the IPv6 header and
139extension headers.
140.Pp
141The following socket options are supported:
142.Bl -tag -width Ds
143.It Dv IPV6_UNICAST_HOPS Fa "int *"
144Get or set the default hop limit header field for outgoing unicast
145datagrams sent on this socket.
146A value of \-1 resets to the default value.
147.It Dv IPV6_MINHOPCOUNT Fa "int *"
148Get or set the minimum hop limit header field for incoming
149packets received on TCP and UDP sockets.
150This can be used to implement the
151.Em Generalized TTL Security Mechanism (GTSM)
152according to RFC 5082.
153.It Dv IPV6_MULTICAST_IF Fa "u_int *"
154Get or set the interface from which multicast packets will be sent.
155For hosts with multiple interfaces, each multicast transmission is sent
156from the primary network interface.
157The interface is specified as its index as provided by
158.Xr if_nametoindex 3 .
159A value of zero specifies the default interface.
160.It Dv IPV6_MULTICAST_HOPS Fa "int *"
161Get or set the default hop limit header field for outgoing multicast
162datagrams sent on this socket.
163This option controls the scope of multicast datagram transmissions.
164.Pp
165Datagrams with a hop limit of 1 are not forwarded beyond the local
166network.
167Multicast datagrams with a hop limit of zero will not be transmitted on
168any network but may be delivered locally if the sending host belongs to
169the destination group and if multicast loopback (see below) has not been
170disabled on the sending socket.
171Multicast datagrams with a hop limit greater than 1 may be forwarded to
172the other networks if a multicast router (such as
173.Xr mrouted 8 )
174is attached to the local network.
175.It Dv IPV6_MULTICAST_LOOP Fa "u_int *"
176Get or set the status of whether multicast datagrams will be looped back
177for local delivery when a multicast datagram is sent to a group to which
178the sending host belongs.
179.Pp
180This option improves performance for applications that may have no more
181than one instance on a single host (such as a router daemon) by
182eliminating the overhead of receiving their own transmissions.
183It should generally not be used by applications for which there may be
184more than one instance on a single host (such as a conferencing program)
185or for which the sender does not belong to the destination group
186(such as a time-querying program).
187.Pp
188A multicast datagram sent with an initial hop limit greater than 1 may
189be delivered to the sending host on a different interface from that on
190which it was sent if the host belongs to the destination group on that
191other interface.
192The multicast loopback control option has no effect on such delivery.
193.It Dv IPV6_JOIN_GROUP Fa "struct ipv6_mreq *"
194Join a multicast group.
195A host must become a member of a multicast group before it can receive
196datagrams sent to the group.
197.Bd -literal
198struct ipv6_mreq {
199	struct in6_addr	ipv6mr_multiaddr;
200	unsigned int	ipv6mr_interface;
201};
202.Ed
203.Pp
204.Va ipv6mr_interface
205may be set to zeroes to choose the default multicast interface or to the
206index of a particular multicast-capable interface if the host is
207multihomed.
208Membership is associated with a single interface; programs running on
209multihomed hosts may need to join the same group on more than one
210interface.
211.Pp
212If the multicast address is unspecified (i.e., all zeroes), messages
213from all multicast addresses will be accepted by this group.
214Note that setting to this value requires superuser privileges.
215.It Dv IPV6_LEAVE_GROUP Fa "struct ipv6_mreq *"
216Drop membership from the associated multicast group.
217Memberships are automatically dropped when the socket is closed or when
218the process exits.
219.It Dv IPV6_PORTRANGE Fa "int *"
220Get or set the allocation policy of ephemeral ports for when the kernel
221automatically binds a local address to this socket.
222The following values are available:
223.Pp
224.Bl -tag -width IPV6_PORTRANGE_DEFAULT -compact
225.It Dv IPV6_PORTRANGE_DEFAULT
226Use the regular range of non-reserved ports (varies, see
227.Xr sysctl 8 ) .
228.It Dv IPV6_PORTRANGE_HIGH
229Use a high range (varies, see
230.Xr sysctl 8 ) .
231.It Dv IPV6_PORTRANGE_LOW
232Use a low, reserved range (600\-1023).
233.El
234.It Dv IPV6_RECVPKTINFO Fa "int *"
235Get or set whether additional information about subsequent packets will
236be provided as ancillary data along with the payload in subsequent
237.Xr recvmsg 2
238calls.
239The information is stored in the following structure in the ancillary
240data returned:
241.Bd -literal
242struct in6_pktinfo {
243	struct in6_addr ipi6_addr;    /* src/dst IPv6 address */
244	unsigned int    ipi6_ifindex; /* send/recv if index */
245};
246.Ed
247.Pp
248Turning this option on will result in this socket getting cmsg data of
249type IPV6_PKTINFO.
250.It Dv IPV6_RECVHOPLIMIT Fa "int *"
251Get or set whether the hop limit header field from subsequent packets
252will be provided as ancillary data along with the payload in subsequent
253.Xr recvmsg 2
254calls.
255The value is stored as an
256.Vt int
257in the ancillary data returned.
258Turning this option on will result in this socket getting cmsg data of
259type IPV6_HOPLIMIT.
260.It Dv IPV6_RECVHOPOPTS Fa "int *"
261Get or set whether the hop-by-hop options from subsequent packets will be
262provided as ancillary data along with the payload in subsequent
263.Xr recvmsg 2
264calls.
265The option is stored in the following structure in the ancillary data
266returned:
267.Bd -literal
268struct ip6_hbh {
269	u_int8_t ip6h_nxt;	/* next header */
270	u_int8_t ip6h_len;	/* length in units of 8 octets */
271/* followed by options */
272} __packed;
273.Ed
274.Pp
275The
276.Fn inet6_opt_init
277routine and family of routines may be used to manipulate this data.
278.Pp
279This option requires superuser privileges.
280Turning this option on will result in this socket getting cmsg data of
281type IPV6_HOPOPTS.
282.It Dv IPV6_RECVDSTOPTS Fa "int *"
283Get or set whether the destination options from subsequent packets will
284be provided as ancillary data along with the payload in subsequent
285.Xr recvmsg 2
286calls.
287The option is stored in the following structure in the ancillary data
288returned:
289.Bd -literal
290struct ip6_dest {
291	u_int8_t ip6d_nxt;	/* next header */
292	u_int8_t ip6d_len;	/* length in units of 8 octets */
293/* followed by options */
294} __packed;
295.Ed
296.Pp
297The
298.Fn inet6_opt_init
299routine and family of routines may be used to manipulate this data.
300.Pp
301This option requires superuser privileges.
302Turning this option on will result in this socket getting cmsg data of
303type IPV6_DSTOPTS.
304.It Dv IPV6_TCLASS Fa "int *"
305Get or set the value of the traffic class field used for outgoing datagrams
306on this socket.
307The value must be between \-1 and 255.
308A value of \-1 resets to the default value.
309.It Dv IPV6_RECVTCLASS Fa "int *"
310Get or set the status of whether the traffic class header field will be
311provided as ancillary data along with the payload in subsequent
312.Xr recvmsg 2
313calls.
314The header field is stored as a single value of type
315.Vt int .
316.It Dv IPV6_RECVRTHDR Fa "int *"
317Get or set whether the routing header from subsequent packets will be
318provided as ancillary data along with the payload in subsequent
319.Xr recvmsg 2
320calls.
321The header is stored in the following structure in the ancillary data
322returned:
323.Bd -literal
324struct ip6_rthdr {
325	u_int8_t ip6r_nxt;	/* next header */
326	u_int8_t ip6r_len;	/* length in units of 8 octets */
327	u_int8_t ip6r_type;	/* routing type */
328	u_int8_t ip6r_segleft;	/* segments left */
329/* followed by routing-type-specific data */
330} __packed;
331.Ed
332.Pp
333The
334.Fn inet6_rth_space
335routine and family of routines may be used to manipulate this data.
336.Pp
337This option requires superuser privileges.
338Turning this option on will result in this socket getting cmsg data of
339type IPV6_RTHDR.
340.It Dv IPV6_CHECKSUM Fa "int *"
341Get or set the byte offset into a packet where the 16-bit checksum is
342located.
343When set, this byte offset is where incoming packets will be expected
344to have checksums of their data stored and where outgoing packets will
345have checksums of their data computed and stored by the kernel.
346A value of \-1 specifies that no checksums will be checked on incoming
347packets and that no checksums will be computed or stored on outgoing
348packets.
349The offset of the checksum for ICMPv6 sockets cannot be relocated or
350turned off.
351.It Dv IPV6_V6ONLY Fa "int *"
352Get or set whether only IPv6 connections can be made to this socket.
353For wildcard sockets, this can restrict connections to IPv6 only.
354With
355.Ox
356IPv6 sockets are always IPv6-only, so the socket option is read-only
357(not modifiable).
358.It Dv IPV6_USE_MIN_MTU Fa "int *"
359Get or set whether the minimal IPv6 maximum transmission unit (MTU) size
360will be used to avoid fragmentation from occurring for subsequent
361outgoing datagrams.
362.It Dv IPV6_AUTH_LEVEL Fa "int *"
363Get or set the
364.Xr ipsec 4
365authentication level.
366.It Dv IPV6_ESP_TRANS_LEVEL Fa "int *"
367Get or set the ESP transport level.
368.It Dv IPV6_ESP_NETWORK_LEVEL Fa "int *"
369Get or set the ESP encapsulation level.
370.It Dv IPV6_IPCOMP_LEVEL Fa "int *"
371Get or set the
372.Xr ipcomp 4
373level.
374.It Dv IPV6_RECVDSTPORT Fa "int *"
375Get or set the status of whether the destination port for a UDP datagram
376will be provided as ancillary data along with the payload in subsequent
377.Xr recvmsg 2
378calls.
379The information is stored as a single value of type
380.Vt u_int16_t
381in network byte order.
382.Pp
383Turning this option on will result in this socket getting cmsg data of
384type IPV6_RECVDSTPORT.
385.El
386.Pp
387The
388.Dv IPV6_RECVPKTINFO ,
389.Dv IPV6_RECVHOPLIMIT ,
390.Dv IPV6_RECVHOPOPTS ,
391.Dv IPV6_RECVDSTOPTS ,
392and
393.Dv IPV6_RTHDR
394options will return ancillary data along with payload contents in subsequent
395.Xr recvmsg 2
396calls with
397.Va cmsg_level
398set to
399.Dv IPPROTO_IPV6
400and
401.Va cmsg_type
402set to respective option name value (e.g.,
403.Dv IPV6_HOPLIMIT ) .
404These options may also be used directly as ancillary
405.Va cmsg_type
406values in
407.Xr sendmsg 2
408to set options on the packet being transmitted by the call.
409The
410.Va cmsg_level
411value must be
412.Dv IPPROTO_IPV6 .
413For these options, the ancillary data object value format is the same
414as the value returned as explained for each when received with
415.Xr recvmsg 2 .
416.Pp
417Note that using
418.Xr sendmsg 2
419to specify options on particular packets works only on UDP and raw sockets.
420To manipulate header options for packets on TCP sockets, only the socket
421options may be used.
422.Pp
423In some cases, there are multiple APIs defined for manipulating an IPv6
424header field.
425A good example is the outgoing interface for multicast datagrams, which
426can be set by the
427.Dv IPV6_MULTICAST_IF
428socket option, through the
429.Dv IPV6_RECVPKTINFO
430option, and through the
431.Va sin6_scope_id
432field of the socket address passed to the
433.Xr sendto 2
434system call.
435.Pp
436Resolving these conflicts is implementation dependent.
437This implementation determines the value in the following way:
438options specified by using ancillary data (i.e.,
439.Xr sendmsg 2 )
440are considered first,
441options specified by using the individual, basic, and direct socket
442options (e.g.,
443.Dv IPV6_UNICAST_HOPS )
444are considered second,
445and options specified in the socket address supplied to
446.Xr sendto 2
447are the last choice.
448.Ss Multicasting
449IPv6 multicasting is supported only on
450.Dv AF_INET6
451sockets of type
452.Dv SOCK_DGRAM
453and
454.Dv SOCK_RAW ,
455and only on networks where the interface driver supports
456multicasting.
457Socket options (see above) that manipulate membership of
458multicast groups and other multicast options include
459.Dv IPV6_MULTICAST_IF ,
460.Dv IPV6_MULTICAST_HOPS ,
461.Dv IPV6_MULTICAST_LOOP ,
462.Dv IPV6_LEAVE_GROUP ,
463and
464.Dv IPV6_JOIN_GROUP .
465.Ss Raw Sockets
466Raw IPv6 sockets are connectionless and are normally used with the
467.Xr sendto 2
468and
469.Xr recvfrom 2
470calls, although the
471.Xr connect 2
472call may be used to fix the destination address for future outgoing
473packets so that
474.Xr send 2
475may instead be used and the
476.Xr bind 2
477call may be used to fix the source address for future outgoing
478packets instead of having the kernel choose a source address.
479.Pp
480By using
481.Xr connect 2
482or
483.Xr bind 2 ,
484raw socket input is constrained to only packets with their
485source address matching the socket destination address if
486.Xr connect 2
487was used and to packets with their destination address
488matching the socket source address if
489.Xr bind 2
490was used.
491.Pp
492If the
493.Ar proto
494argument to
495.Xr socket 2
496is zero, the default protocol
497.Pq Dv IPPROTO_RAW
498is used for outgoing packets.
499For incoming packets, protocols recognized by kernel are
500.Sy not
501passed to the application socket (e.g.,
502.Xr tcp 4
503and
504.Xr udp 4 )
505except for some ICMPv6 messages.
506The ICMPv6 messages not passed to raw sockets include echo, timestamp,
507and address mask requests.
508If
509.Ar proto
510is non-zero, only packets with this protocol will be passed to the
511socket.
512.Pp
513IPv6 fragments are also not passed to application sockets until
514they have been reassembled.
515If reception of all packets is desired, link-level access (such as
516.Xr bpf 4 )
517must be used instead.
518.Pp
519Outgoing packets automatically have an IPv6 header prepended to them
520(based on the destination address and the protocol number the socket
521was created with).
522Incoming packets are received by an application without the IPv6 header
523or any extension headers.
524.Pp
525Outgoing packets will be fragmented automatically by the kernel if they
526are too large.
527Incoming packets will be reassembled before being sent to the raw socket,
528so packet fragments or fragment headers will never be seen on a raw socket.
529.Sh EXAMPLES
530The following determines the hop limit on the next packet received:
531.Bd -literal
532struct iovec iov[2];
533u_char buf[BUFSIZ];
534struct cmsghdr *cm;
535struct msghdr m;
536int found, optval;
537u_char data[2048];
538
539/* Create socket. */
540
541(void)memset(&m, 0, sizeof(m));
542(void)memset(&iov, 0, sizeof(iov));
543
544iov[0].iov_base = data;		/* buffer for packet payload */
545iov[0].iov_len = sizeof(data);	/* expected packet length */
546
547m.msg_name = &from;		/* sockaddr_in6 of peer */
548m.msg_namelen = sizeof(from);
549m.msg_iov = iov;
550m.msg_iovlen = 1;
551m.msg_control = (caddr_t)buf;	/* buffer for control messages */
552m.msg_controllen = sizeof(buf);
553
554/*
555 * Enable the hop limit value from received packets to be
556 * returned along with the payload.
557 */
558optval = 1;
559if (setsockopt(s, IPPROTO_IPV6, IPV6_RECVHOPLIMIT, &optval,
560    sizeof(optval)) == -1)
561	err(1, "setsockopt");
562
563found = 0;
564while (!found) {
565	if (recvmsg(s, &m, 0) == -1)
566		err(1, "recvmsg");
567	for (cm = CMSG_FIRSTHDR(&m); cm != NULL;
568	     cm = CMSG_NXTHDR(&m, cm)) {
569		if (cm->cmsg_level == IPPROTO_IPV6 &&
570		    cm->cmsg_type == IPV6_HOPLIMIT &&
571		    cm->cmsg_len == CMSG_LEN(sizeof(int))) {
572			found = 1;
573			(void)printf("hop limit: %d\en",
574			    *(int *)CMSG_DATA(cm));
575			break;
576		}
577	}
578}
579.Ed
580.Sh DIAGNOSTICS
581A socket operation may fail with one of the following errors returned:
582.Bl -tag -width EADDRNOTAVAILxx
583.It Bq Er EISCONN
584when trying to establish a connection on a socket which
585already has one or when trying to send a datagram with the destination
586address specified and the socket is already connected.
587.It Bq Er ENOTCONN
588when trying to send a datagram, but
589no destination address is specified, and the socket hasn't been
590connected.
591.It Bq Er ENOBUFS
592when the system runs out of memory for
593an internal data structure.
594.It Bq Er EADDRNOTAVAIL
595when an attempt is made to create a
596socket with a network address for which no network interface
597exists.
598.It Bq Er EACCES
599when an attempt is made to create
600a raw IPv6 socket by a non-privileged process.
601.El
602.Pp
603The following errors specific to IPv6 may occur when setting or getting
604header options:
605.Bl -tag -width EADDRNOTAVAILxx
606.It Bq Er EINVAL
607An unknown socket option name was given.
608.It Bq Er EINVAL
609An ancillary data object was improperly formed.
610.El
611.Sh SEE ALSO
612.Xr getsockopt 2 ,
613.Xr recv 2 ,
614.Xr send 2 ,
615.Xr setsockopt 2 ,
616.Xr socket 2 ,
617.\" .Xr inet6_opt_init 3 ,
618.\" .Xr inet6_rth_space 3 ,
619.Xr if_nametoindex 3 ,
620.Xr bpf 4 ,
621.Xr icmp6 4 ,
622.Xr inet6 4 ,
623.Xr netintro 4 ,
624.Xr tcp 4 ,
625.Xr udp 4
626.Sh STANDARDS
627.Rs
628.%A S. Deering
629.%A R. Hinden
630.%D December 1998
631.%R RFC 2460
632.%T Internet Protocol, Version 6 (IPv6) Specification
633.Re
634.Pp
635.Rs
636.%A R. Gilligan
637.%A S. Thomson
638.%A J. Bound
639.%A J. McCann
640.%A W. Stevens
641.%D February 2003
642.%R RFC 3493
643.%T Basic Socket Interface Extensions for IPv6
644.Re
645.Pp
646.Rs
647.%A W. Stevens
648.%A M. Thomas
649.%A E. Nordmark
650.%A T. Jinmei
651.%D May 2003
652.%R RFC 3542
653.%T Advanced Sockets Application Program Interface (API) for IPv6
654.Re
655.Pp
656The
657.Dv IPV6_PORTRANGE
658socket option and the conflict resolution rule are not defined in the
659RFCs and should be considered implementation dependent.
660