As per Relevance of the word structure, we have this rfc below:
Network Working Group W.
Request for Comments: 2292
Category: Informational M.
February 1998
Advanced Sockets API for IPv
Status of this
This memo provides information for the Internet community. It
not specify an Internet standard of any kind. Distribution of
memo is unlimited
Copyright
Copyright (C) The Internet Society (1998). All Rights Reserved
Specifications are in progress for changes to the sockets API
support IP version 6 [RFC-2133]. These changes are for TCP and UDP
based applications and will support most end-user applications in
today: Telnet and FTP clients and servers, HTTP clients and servers
and the like
But another class of applications exists that will also be run
IPv6. We call these "advanced" applications and today this
programs such as Ping, Traceroute, routing daemons, multicast
daemons, router discovery daemons, and the like. The API
typically used by these programs that make them "advanced" is a
socket to access ICMPv4, IGMPv4, or IPv4, along with some
of the packet header formats used by these protocols. To
portability for applications that use raw sockets under IPv6,
standardization is needed for the advanced API features
There are other features of IPv6 that some applications will need
access: interface identification (specifying the outgoing
and determining the incoming interface) and IPv6 extension
that are not addressed in [RFC-2133]: Hop-by-Hop options,
options, and the Routing header (source routing). This
provides API access to these features too
Stevens & Thomas Informational [Page 1]
RFC 2292 Advanced Sockets API for IPv6 February 1998
Table of
1. Introduction ................................................3
2. Common Structures and Definitions ...........................5
2.1. The ip6_hdr Structure ..................................5
2.1.1. IPv6 Next Header Values .........................6
2.1.2. IPv6 Extension Headers ..........................6
2.2. The icmp6_hdr Structure ................................8
2.2.1. ICMPv6 Type and Code Values .....................8
2.2.2. ICMPv6 Neighbor Discovery Type and Code Values ..9
2.3. Address Testing Macros .................................12
2.4. Protocols File .........................................12
3. IPv6 Raw Sockets ............................................13
3.1. Checksums ..............................................14
3.2. ICMPv6 Type Filtering ..................................14
4. Ancillary Data ..............................................17
4.1. The msghdr Structure ...................................18
4.2. The cmsghdr Structure ..................................18
4.3. Ancillary Data Object Macros ...........................19
4.3.1. CMSG_FIRSTHDR ...................................20
4.3.2. CMSG_NXTHDR .....................................22
4.3.3. CMSG_DATA .......................................22
4.3.4. CMSG_SPACE ......................................22
4.3.5. CMSG_LEN ........................................22
4.4. Summary of Options Described Using Ancillary Data ......23
4.5. IPV6_PKTOPTIONS Socket Option ..........................24
4.5.1. TCP Sticky Options ..............................25
4.5.2. UDP and Raw Socket Sticky Options ...............26
5. Packet Information ..........................................26
5.1. Specifying/Receiving the Interface .....................27
5.2. Specifying/Receiving Source/Destination Address ........27
5.3. Specifying/Receiving the Hop Limit .....................28
5.4. Specifying the Next Hop Address ........................29
5.5. Additional Errors with sendmsg() .......................29
6. Hop-By-Hop Options ..........................................30
6.1. Receiving Hop-by-Hop Options ...........................31
6.2. Sending Hop-by-Hop Options .............................31
6.3. Hop-by-Hop and Destination Options Processing ..........32
6.3.1. inet6_option_space ..............................32
6.3.2. inet6_option_init ...............................32
6.3.3. inet6_option_append .............................33
6.3.4. inet6_option_alloc ..............................33
6.3.5. inet6_option_next ...............................34
6.3.6. inet6_option_find ...............................35
6.3.7. Options Examples ................................35
7. Destination Options .........................................42
7.1. Receiving Destination Options ..........................42
7.2. Sending Destination Options ............................43
Stevens & Thomas Informational [Page 2]
RFC 2292 Advanced Sockets API for IPv6 February 1998
8. Routing Header Option .......................................43
8.1. inet6_rthdr_space ......................................44
8.2. inet6_rthdr_init .......................................45
8.3. inet6_rthdr_add ........................................45
8.4. inet6_rthdr_lasthop ....................................46
8.5. inet6_rthdr_reverse ....................................46
8.6. inet6_rthdr_segments ...................................46
8.7. inet6_rthdr_getaddr ....................................46
8.8. inet6_rthdr_getflags ...................................47
8.9. Routing Header Example .................................47
9. Ordering of Ancillary Data and IPv6 Extension Headers .......53
10. IPv6-Specific Options with IPv4-Mapped IPv6 Addresses .......54
11. rresvport_af ................................................55
12. Future Items ................................................55
12.1. Flow Labels ...........................................55
12.2. Path MTU Discovery and UDP ............................56
12.3. Neighbor Reachability and UDP .........................56
13. Summary of New Definitions ..................................56
14. Security Considerations .....................................59
15. Change History ..............................................59
16. References ..................................................65
17. Acknowledgments .............................................65
18. Authors' Addresses ..........................................66
19. Full Copyright Statement ....................................67
1.
Specifications are in progress for changes to the sockets API
support IP version 6 [RFC-2133]. These changes are for TCP and UDP
based applications. The current document defines some the "advanced
features of the sockets API that are required for applications
take advantage of additional features of IPv6.
Today, the portability of applications using IPv4 raw sockets
quite high, but this is mainly because most IPv4
started from a common base (the Berkeley source code) or at
started with the Berkeley headers. This allows programs such as
and Traceroute, for example, to compile with minimal effort on
hosts that support the sockets API. With IPv6, however, there is
common source code base that implementors are starting from, and
possibility for divergence at this level between
implementations is high. To avoid a complete lack of
amongst applications that use raw IPv6 sockets, some
is necessary
Stevens & Thomas Informational [Page 3]
RFC 2292 Advanced Sockets API for IPv6 February 1998
There are also features from the basic IPv6 specification that
not addressed in [RFC-2133]: sending and receiving Hop-by-
options, Destination options, and Routing headers, specifying
outgoing interface, and being told of the receiving interface
This document can be divided into the following main sections
1. Definitions of the basic constants and structures required
applications to use raw IPv6 sockets. This includes
definitions for the IPv6 and ICMPv6 headers and all
constants (e.g., values for the Next Header field).
2. Some basic semantic definitions for IPv6 raw sockets.
example, a raw ICMPv4 socket requires the application
calculate and store the ICMPv4 header checksum. But with IPv
this would require the application to choose the source IPv
address because the source address is part of the pseudo
that ICMPv6 now uses for its checksum computation. It should
defined that with a raw ICMPv6 socket the kernel
calculates and stores the ICMPv6 header checksum
3. Packet information: how applications can obtain the
interface, destination address, and received hop limit,
with specifying these values on a per-packet basis. There are
class of applications that need this capability and the
should be portable
4. Access to the optional Hop-by-Hop, Destination, and
headers
5. Additional features required for IPv6 application portability
The packet information along with access to the extension
(Hop-by-Hop options, Destination options, and Routing header)
specified using the "ancillary data" fields that were added to
4.3BSD Reno sockets API in 1990. The reason is that these
data fields are part of the Posix.1g standard (which should
approved in 1997) and should therefore be adopted by most vendors
This document does not address application access to either
authentication header or the encapsulating security payload header
All examples in this document omit error checking in favor of
and clarity
Stevens & Thomas Informational [Page 4]
RFC 2292 Advanced Sockets API for IPv6 February 1998
We note that many of the functions and socket options defined in
document may have error returns that are not defined in
document. Many of these possible error returns will be
only as implementations proceed
Datatypes in this document follow the Posix.1g format: intN_t means
signed integer of exactly N bits (e.g., int16_t) and uintN_t means
unsigned integer of exactly N bits (e.g., uint32_t).
Note that we use the (unofficial) terminology ICMPv4, IGMPv4,
ARPv4 to avoid any confusion with the newer ICMPv6 protocol
2. Common Structures and
Many advanced applications examine fields in the IPv6 header and
and examine fields in the various ICMPv6 headers. Common
definitions for these headers are required, along with
constant definitions for the structure members
Two new headers are defined: and .
When an include file is specified, that include file is allowed
include other files that do the actual declaration or definition
2.1. The ip6_hdr
The following structure is defined as a result of
. Note that this is a new header
struct ip6_hdr {
union {
struct ip6_hdrctl {
uint32_t ip6_un1_flow; /* 24 bits of flow-ID */
uint16_t ip6_un1_plen; /* payload length */
uint8_t ip6_un1_nxt; /* next header */
uint8_t ip6_un1_hlim; /* hop limit */
} ip6_un1;
uint8_t ip6_un2_vfc; /* 4 bits version, 4 bits priority */
} ip6_ctlun
struct in6_addr ip6_src; /* source address */
struct in6_addr ip6_dst; /* destination address */
};
#define ip6_vfc ip6_ctlun.ip6_un2_
#define ip6_flow ip6_ctlun.ip6_un1.ip6_un1_
#define ip6_plen ip6_ctlun.ip6_un1.ip6_un1_
#define ip6_nxt ip6_ctlun.ip6_un1.ip6_un1_
#define ip6_hlim ip6_ctlun.ip6_un1.ip6_un1_
Stevens & Thomas Informational [Page 5]
RFC 2292 Advanced Sockets API for IPv6 February 1998
#define ip6_hops ip6_ctlun.ip6_un1.ip6_un1_
2.1.1. IPv6 Next Header
IPv6 defines many new values for the Next Header field.
following constants are defined as a result of
.
#define IPPROTO_HOPOPTS 0 /* IPv6 Hop-by-Hop options */
#define IPPROTO_IPV6 41 /* IPv6 header */
#define IPPROTO_ROUTING 43 /* IPv6 Routing header */
#define IPPROTO_FRAGMENT 44 /* IPv6 fragmentation header */
#define IPPROTO_ESP 50 /* encapsulating security payload */
#define IPPROTO_AH 51 /* authentication header */
#define IPPROTO_ICMPV6 58 /* ICMPv6 */
#define IPPROTO_NONE 59 /* IPv6 no next header */
#define IPPROTO_DSTOPTS 60 /* IPv6 Destination options */
Berkeley-derived IPv4 implementations also define IPPROTO_IP to be 0.
This should not be a problem since IPPROTO_IP is used only with IPv
sockets and IPPROTO_HOPOPTS only with IPv6 sockets
2.1.2. IPv6 Extension
Six extension headers are defined for IPv6. We define structures
all except the Authentication header and Encapsulating
Payload header, both of which are beyond the scope of this document
The following structures are defined as a result of
.
/* Hop-by-Hop options header */
/* XXX should we pad it to force alignment on an 8-byte boundary? */
struct ip6_hbh {
uint8_t ip6h_nxt; /* next header */
uint8_t ip6h_len; /* length in units of 8 octets */
/* followed by options */
};
/* Destination options header */
/* XXX should we pad it to force alignment on an 8-byte boundary? */
struct ip6_dest {
uint8_t ip6d_nxt; /* next header */
uint8_t ip6d_len; /* length in units of 8 octets */
/* followed by options */
};
/* Routing header */
struct ip6_rthdr {
Stevens & Thomas Informational [Page 6]
RFC 2292 Advanced Sockets API for IPv6 February 1998
uint8_t ip6r_nxt; /* next header */
uint8_t ip6r_len; /* length in units of 8 octets */
uint8_t ip6r_type; /* routing type */
uint8_t ip6r_segleft; /* segments left */
/* followed by routing type specific data */
};
/* Type 0 Routing header */
struct ip6_rthdr0 {
uint8_t ip6r0_nxt; /* next header */
uint8_t ip6r0_len; /* length in units of 8 octets */
uint8_t ip6r0_type; /* always zero */
uint8_t ip6r0_segleft; /* segments left */
uint8_t ip6r0_reserved; /* reserved field */
uint8_t ip6r0_slmap[3]; /* strict/loose bit map */
struct in6_addr ip6r0_addr[1]; /* up to 23 addresses */
};
/* Fragment header */
struct ip6_frag {
uint8_t ip6f_nxt; /* next header */
uint8_t ip6f_reserved; /* reserved field */
uint16_t ip6f_offlg; /* offset, reserved, and flag */
uint32_t ip6f_ident; /* identification */
};
#if BYTE_ORDER == BIG_
#define IP6F_OFF_MASK 0xfff8 /* mask out offset from _offlg */
#define IP6F_RESERVED_MASK 0x0006 /* reserved bits in ip6f_offlg */
#define IP6F_MORE_FRAG 0x0001 /* more-fragments flag */
#else /* BYTE_ORDER == LITTLE_ENDIAN */
#define IP6F_OFF_MASK 0xf8ff /* mask out offset from _offlg */
#define IP6F_RESERVED_MASK 0x0600 /* reserved bits in ip6f_offlg */
#define IP6F_MORE_FRAG 0x0100 /* more-fragments flag */
#
Defined constants for fields larger than 1 byte depend on the
ordering that is used. This API assumes that the fields in
protocol headers are left in the network byte order, which is big
endian for the Internet protocols. If not, then either
constants or the fields being tested must be converted at run-time
using something like htons() or htonl().
(Note: We show an implementation that supports both big-endian
little-endian byte ordering, assuming a hypothetical compile-time #
test to determine the byte ordering. The constant that we show
Stevens & Thomas Informational [Page 7]
RFC 2292 Advanced Sockets API for IPv6 February 1998
BYTE_ORDER, with values of BIG_ENDIAN and LITTLE_ENDIAN, are
example purposes only. If an implementation runs on only one type
hardware it need only define the set of constants for that hardware'
byte ordering.)
2.2. The icmp6_hdr
The ICMPv6 header is needed by numerous IPv6 applications
Ping, Traceroute, router discovery daemons, and neighbor
daemons. The following structure is defined as a result of
. Note that this is a new header
struct icmp6_hdr {
uint8_t icmp6_type; /* type field */
uint8_t icmp6_code; /* code field */
uint16_t icmp6_cksum; /* checksum field */
union {
uint32_t icmp6_un_data32[1]; /* type-specific field */
uint16_t icmp6_un_data16[2]; /* type-specific field */
uint8_t icmp6_un_data8[4]; /* type-specific field */
} icmp6_dataun
};
#define icmp6_data32 icmp6_dataun.icmp6_un_data32
#define icmp6_data16 icmp6_dataun.icmp6_un_data16
#define icmp6_data8 icmp6_dataun.icmp6_un_data
#define icmp6_pptr icmp6_data32[0] /* parameter prob */
#define icmp6_mtu icmp6_data32[0] /* packet too big */
#define icmp6_id icmp6_data16[0] /* echo request/reply */
#define icmp6_seq icmp6_data16[1] /* echo request/reply */
#define icmp6_maxdelay icmp6_data16[0] /* mcast group membership */
2.2.1. ICMPv6 Type and Code
In addition to a common structure for the ICMPv6 header,
definitions are required for the ICMPv6 type and code fields.
following constants are also defined as a result of
.
#define ICMP6_DST_UNREACH 1
#define ICMP6_PACKET_TOO_BIG 2
#define ICMP6_TIME_EXCEEDED 3
#define ICMP6_PARAM_PROB 4
#define ICMP6_INFOMSG_MASK 0x80 /* all informational messages */
#define ICMP6_ECHO_REQUEST 128
#define ICMP6_ECHO_REPLY 129
Stevens & Thomas Informational [Page 8]
RFC 2292 Advanced Sockets API for IPv6 February 1998
#define ICMP6_MEMBERSHIP_QUERY 130
#define ICMP6_MEMBERSHIP_REPORT 131
#define ICMP6_MEMBERSHIP_REDUCTION 132
#define ICMP6_DST_UNREACH_NOROUTE 0 /* no route to destination */
#define ICMP6_DST_UNREACH_ADMIN 1 /* communication with */
/* destination */
/* administratively */
/* prohibited */
#define ICMP6_DST_UNREACH_NOTNEIGHBOR 2 /* not a neighbor */
#define ICMP6_DST_UNREACH_ADDR 3 /* address unreachable */
#define ICMP6_DST_UNREACH_NOPORT 4 /* bad port */
#define ICMP6_TIME_EXCEED_TRANSIT 0 /* Hop Limit == 0 in transit */
#define ICMP6_TIME_EXCEED_REASSEMBLY 1 /* Reassembly time out */
#define ICMP6_PARAMPROB_HEADER 0 /* erroneous header field */
#define ICMP6_PARAMPROB_NEXTHEADER 1 /* unrecognized Next Header */
#define ICMP6_PARAMPROB_OPTION 2 /* unrecognized IPv6 option */
The five ICMP message types defined by IPv6 neighbor discovery (133-
137) are defined in the next section
2.2.2. ICMPv6 Neighbor Discovery Type and Code
The following structures and definitions are defined as a result
including .
#define ND_ROUTER_SOLICIT 133
#define ND_ROUTER_ADVERT 134
#define ND_NEIGHBOR_SOLICIT 135
#define ND_NEIGHBOR_ADVERT 136
#define ND_REDIRECT 137
struct nd_router_solicit { /* router solicitation */
struct icmp6_hdr nd_rs_hdr
/* could be followed by options */
};
#define nd_rs_type nd_rs_hdr.icmp6_
#define nd_rs_code nd_rs_hdr.icmp6_
#define nd_rs_cksum nd_rs_hdr.icmp6_
#define nd_rs_reserved nd_rs_hdr.icmp6_data32[0]
struct nd_router_advert { /* router advertisement */
struct icmp6_hdr nd_ra_hdr
uint32_t nd_ra_reachable; /* reachable time */
uint32_t nd_ra_retransmit; /* retransmit timer */
Stevens & Thomas Informational [Page 9]
RFC 2292 Advanced Sockets API for IPv6 February 1998
/* could be followed by options */
};
#define nd_ra_type nd_ra_hdr.icmp6_
#define nd_ra_code nd_ra_hdr.icmp6_
#define nd_ra_cksum nd_ra_hdr.icmp6_
#define nd_ra_curhoplimit nd_ra_hdr.icmp6_data8[0]
#define nd_ra_flags_reserved nd_ra_hdr.icmp6_data8[1]
#define ND_RA_FLAG_MANAGED 0x80
#define ND_RA_FLAG_OTHER 0x40
#define nd_ra_router_lifetime nd_ra_hdr.icmp6_data16[1]
struct nd_neighbor_solicit { /* neighbor solicitation */
struct icmp6_hdr nd_ns_hdr
struct in6_addr nd_ns_target; /* target address */
/* could be followed by options */
};
#define nd_ns_type nd_ns_hdr.icmp6_
#define nd_ns_code nd_ns_hdr.icmp6_
#define nd_ns_cksum nd_ns_hdr.icmp6_
#define nd_ns_reserved nd_ns_hdr.icmp6_data32[0]
struct nd_neighbor_advert { /* neighbor advertisement */
struct icmp6_hdr nd_na_hdr
struct in6_addr nd_na_target; /* target address */
/* could be followed by options */
};
#define nd_na_type nd_na_hdr.icmp6_
#define nd_na_code nd_na_hdr.icmp6_
#define nd_na_cksum nd_na_hdr.icmp6_
#define nd_na_flags_reserved nd_na_hdr.icmp6_data32[0]
#if BYTE_ORDER == BIG_
#define ND_NA_FLAG_ROUTER 0x80000000
#define ND_NA_FLAG_SOLICITED 0x40000000
#define ND_NA_FLAG_OVERRIDE 0x20000000
#else /* BYTE_ORDER == LITTLE_ENDIAN */
#define ND_NA_FLAG_ROUTER 0x00000080
#define ND_NA_FLAG_SOLICITED 0x00000040
#define ND_NA_FLAG_OVERRIDE 0x00000020
#
struct nd_redirect { /* redirect */
struct icmp6_hdr nd_rd_hdr
struct in6_addr nd_rd_target; /* target address */
struct in6_addr nd_rd_dst; /* destination address */
/* could be followed by options */
Stevens & Thomas Informational [Page 10]
RFC 2292 Advanced Sockets API for IPv6 February 1998
};
#define nd_rd_type nd_rd_hdr.icmp6_
#define nd_rd_code nd_rd_hdr.icmp6_
#define nd_rd_cksum nd_rd_hdr.icmp6_
#define nd_rd_reserved nd_rd_hdr.icmp6_data32[0]
struct nd_opt_hdr { /* Neighbor discovery option header */
uint8_t nd_opt_type
uint8_t nd_opt_len; /* in units of 8 octets */
/* followed by option specific data */
};
#define ND_OPT_SOURCE_LINKADDR 1
#define ND_OPT_TARGET_LINKADDR 2
#define ND_OPT_PREFIX_INFORMATION 3
#define ND_OPT_REDIRECTED_HEADER 4
#define ND_OPT_MTU 5
struct nd_opt_prefix_info { /* prefix information */
uint8_t nd_opt_pi_type
uint8_t nd_opt_pi_len
uint8_t nd_opt_pi_prefix_len
uint8_t nd_opt_pi_flags_reserved
uint32_t nd_opt_pi_valid_time
uint32_t nd_opt_pi_preferred_time
uint32_t nd_opt_pi_reserved2;
struct in6_addr nd_opt_pi_prefix
};
#define ND_OPT_PI_FLAG_ONLINK 0x80
#define ND_OPT_PI_FLAG_AUTO 0x40
struct nd_opt_rd_hdr { /* redirected header */
uint8_t nd_opt_rh_type
uint8_t nd_opt_rh_len
uint16_t nd_opt_rh_reserved1;
uint32_t nd_opt_rh_reserved2;
/* followed by IP header and data */
};
struct nd_opt_mtu { /* MTU option */
uint8_t nd_opt_mtu_type
uint8_t nd_opt_mtu_len
uint16_t nd_opt_mtu_reserved
uint32_t nd_opt_mtu_mtu
};
Stevens & Thomas Informational [Page 11]
RFC 2292 Advanced Sockets API for IPv6 February 1998
We note that the nd_na_flags_reserved flags have the same
ordering problems as we discussed with ip6f_offlg
2.3. Address Testing
The basic API ([RFC-2133]) defines some macros for testing an IPv
address for certain properties. This API extends those
with additional address testing macros, defined as a result
including .
int IN6_ARE_ADDR_EQUAL(const struct in6_addr *,
const struct in6_addr *);
2.4. Protocols
Many hosts provide the file /etc/protocols that contains the names
the various IP protocols and their protocol number (e.g., the
of the protocol field in the IPv4 header for that protocol, such as 1
for ICMP). Some programs then call the function getprotobyname()
obtain the protocol value that is then specified as the
argument to the socket() function. For example, the Ping
contains code of the
struct protoent *proto
proto = getprotobyname("icmp");
s = socket(AF_INET, SOCK_RAW, proto->p_proto);
Common names are required for the new IPv6 protocols in this file,
provide portability of applications that call the getprotoXXX()
functions
We define the following protocol names with the values shown.
are taken from ftp://ftp.isi.edu/in-notes/iana/assignments/protocol
numbers
hopopt 0 # hop-by-hop options for ipv
ipv6 41 # ipv
ipv6-route 43 # routing header for ipv
ipv6-frag 44 # fragment header for ipv
esp 50 # encapsulating security payload for ipv
ah 51 # authentication header for ipv
ipv6-icmp 58 # icmp for ipv
ipv6-nonxt 59 # no next header for ipv
ipv6-opts 60 # destination options for ipv
Stevens & Thomas Informational [Page 12]
RFC 2292 Advanced Sockets API for IPv6 February 1998
3. IPv6 Raw
Raw sockets bypass the transport layer (TCP or UDP). With IPv4,
sockets are used to access ICMPv4, IGMPv4, and to read and write IPv
datagrams containing a protocol field that the kernel does
process. An example of the latter is a routing daemon for OSPF
since it uses IPv4 protocol field 89. With IPv6 raw sockets will
used for ICMPv6 and to read and write IPv6 datagrams containing
Next Header field that the kernel does not process. Examples of
latter are a routing daemon for OSPF for IPv6 and RSVP (
field 46).
All data sent via raw sockets MUST be in network byte order and
data received via raw sockets will be in network byte order.
differs from the IPv4 raw sockets, which did not specify a
ordering and typically used the host's byte order
Another difference from IPv4 raw sockets is that complete
(that is, IPv6 packets with extension headers) cannot be read
written using the IPv6 raw sockets API. Instead, ancillary
objects are used to transfer the extension headers, as
later in this document. Should an application need access to
complete IPv6 packet, some other technique, such as the
interfaces BPF or DLPI, must be used
All fields in the IPv6 header that an application might want
change (i.e., everything other than the version number) can
modified using ancillary data and/or socket options by
application for output. All fields in a received IPv6 header (
than the version number and Next Header fields) and all
headers are also made available to the application as ancillary
on input. Hence there is no need for a socket option similar to
IPv4 IP_HDRINCL socket option
When writing to a raw socket the kernel will automatically
the packet if its size exceeds the path MTU, inserting the
fragmentation headers. On input the kernel reassembles
fragments, so the reader of a raw socket never sees any
headers
When we say "an ICMPv6 raw socket" we mean a socket created
calling the socket function with the three arguments PF_INET6,
SOCK_RAW, and IPPROTO_ICMPV6.
Most IPv4 implementations give special treatment to a raw
created with a third argument to socket() of IPPROTO_RAW, whose
is normally 255. We note that this value has no special meaning
an IPv6 raw socket (and the IANA currently reserves the value of 255
Stevens & Thomas Informational [Page 13]
RFC 2292 Advanced Sockets API for IPv6 February 1998
when used as a next-header field). (Note: This feature was added
IPv4 in 1988 by Van Jacobson to support traceroute, allowing
complete IP header to be passed by the application, before
IP_HDRINCL socket option was added.)
3.1.
The kernel will calculate and insert the ICMPv6 checksum for ICMPv
raw sockets, since this checksum is mandatory
For other raw IPv6 sockets (that is, for raw IPv6 sockets
with a third argument other than IPPROTO_ICMPV6), the
must set the new IPV6_CHECKSUM socket option to have the kernel (1)
compute and store a checksum for output, and (2) verify the
checksum on input, discarding the packet if the checksum is in error
This option prevents applications from having to perform
address selection on the packets they send. The checksum
incorporate the IPv6 pseudo-header, defined in Section 8.1 of [RFC
1883]. This new socket option also specifies an integer offset
the user data of where the checksum is located
int offset = 2;
setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, sizeof(offset));
By default, this socket option is disabled. Setting the offset to -1
also disables the option. By disabled we mean (1) the kernel
not calculate and store a checksum for outgoing packets, and (2)
kernel will not verify a checksum for received packets
(Note: Since the checksum is always calculated by the kernel for
ICMPv6 socket, applications are not able to generate ICMPv6
with incorrect checksums (presumably for testing purposes) using
API.)
3.2. ICMPv6 Type
ICMPv4 raw sockets receive most ICMPv4 messages received by
kernel. (We say "most" and not "all" because Berkeley-
kernels never pass echo requests, timestamp requests, or address
requests to a raw socket. Instead these three messages are
entirely by the kernel.) But ICMPv6 is a superset of ICMPv4,
including the functionality of IGMPv4 and ARPv4. This means that
ICMPv6 raw socket can potentially receive many more messages
would be received with an ICMPv4 raw socket: ICMP messages similar
ICMPv4, along with neighbor solicitations, neighbor advertisements
and the three group membership messages
Stevens & Thomas Informational [Page 14]
RFC 2292 Advanced Sockets API for IPv6 February 1998
Most applications using an ICMPv6 raw socket care about only a
subset of the ICMPv6 message types. To transfer extraneous ICMPv
messages from the kernel to user can incur a significant overhead
Therefore this API includes a method of filtering ICMPv6 messages
the ICMPv6 type field
Each ICMPv6 raw socket has an associated filter whose datatype
defined
struct icmp6_filter
This structure, along with the macros and constants defined later
this section, are defined as a result of including
header
The current filter is fetched and stored using getsockopt()
setsockopt() with a level of IPPROTO_ICMPV6 and an option name
ICMP6_FILTER
Six macros operate on an icmp6_filter structure
void ICMP6_FILTER_SETPASSALL (struct icmp6_filter *);
void ICMP6_FILTER_SETBLOCKALL(struct icmp6_filter *);
void ICMP6_FILTER_SETPASS ( int, struct icmp6_filter *);
void ICMP6_FILTER_SETBLOCK( int, struct icmp6_filter *);
int ICMP6_FILTER_WILLPASS (int, const struct icmp6_filter *);
int ICMP6_FILTER_WILLBLOCK(int, const struct icmp6_filter *);
The first argument to the last four macros (an integer) is an ICMPv
message type, between 0 and 255. The pointer argument to all
macros is a pointer to a filter that is modified by the first
macros examined by the last two macros
The first two macros, SETPASSALL and SETBLOCKALL, let us specify
all ICMPv6 messages are passed to the application or that all ICMPv
messages are blocked from being passed to the application
The next two macros, SETPASS and SETBLOCK, let us specify
messages of a given ICMPv6 type should be passed to the
or not passed to the application (blocked).
The final two macros, WILLPASS and WILLBLOCK, return true or
depending whether the specified message type is passed to
application or blocked from being passed to the application by
filter pointed to by the second argument
Stevens & Thomas Informational [Page 15]
RFC 2292 Advanced Sockets API for IPv6 February 1998
When an ICMPv6 raw socket is created, it will by default pass
ICMPv6 message types to the application
As an example, a program that wants to receive only
advertisements could execute the following
struct icmp6_filter myfilt
fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
ICMP6_FILTER_SETBLOCKALL(&myfilt);
ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &myfilt);
setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &myfilt, sizeof(myfilt));
The filter structure is declared and then initialized to block
messages types. The filter structure is then changed to allow
advertisement messages to be passed to the application and the
is installed using setsockopt().
The icmp6_filter structure is similar to the fd_set datatype
with the select() function in the sockets API. The icmp6_
structure is an opaque datatype and the application should not
how it is implemented. All the application does with this
is allocate a variable of this type, pass a pointer to a variable
this type to getsockopt() and setsockopt(), and operate on a
of this type using the six macros that we just defined
Nevertheless, it is worth showing a simple implementation of
datatype and the six macros
struct icmp6_filter {
uint32_t icmp6_filt[8]; /* 8*32 = 256 bits */
};
#define ICMP6_FILTER_WILLPASS(type, filterp) \
((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) != 0)
#define ICMP6_FILTER_WILLBLOCK(type, filterp) \
((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) == 0)
#define ICMP6_FILTER_SETPASS(type, filterp) \
((((filterp)->icmp6_filt[(type) >> 5]) |= (1 << ((type) & 31))))
#define ICMP6_FILTER_SETBLOCK(type, filterp) \
((((filterp)->icmp6_filt[(type) >> 5]) &= ~(1 << ((type) & 31))))
#define ICMP6_FILTER_SETPASSALL(filterp) \
memset((filterp), 0xFF, sizeof(struct icmp6_filter))
#define ICMP6_FILTER_SETBLOCKALL(filterp) \
memset((filterp), 0, sizeof(struct icmp6_filter))
Stevens & Thomas Informational [Page 16]
RFC 2292 Advanced Sockets API for IPv6 February 1998
(Note: These sample definitions have two limitations that
implementation may want to change. The first four macros
their first argument two times. The second two macros require
inclusion of the header for the memset() function.)
4. Ancillary
4.2BSD allowed file descriptors to be transferred between
processes across a UNIX domain socket using the sendmsg()
recvmsg() functions. Two members of the msghdr structure
msg_accrights and msg_accrightslen, were used to send and receive
descriptors. When the OSI protocols were added to 4.3BSD Reno
1990 the names of these two fields in the msghdr structure
changed to msg_control and msg_controllen, because they were used
the OSI protocols for "control information", although the comments
the source code call this "ancillary data".
Other than the OSI protocols, the use of ancillary data has
rare. In 4.4BSD, for example, the only use of ancillary data
IPv4 is to return the destination address of a received UDP
if the IP_RECVDSTADDR socket option is set. With Unix domain
ancillary data is still used to send and receive descriptors
Nevertheless the ancillary data fields of the msghdr
provide a clean way to pass information in addition to the data
is being read or written. The inclusion of the msg_control
msg_controllen members of the msghdr structure along with the
structure that is pointed to by the msg_control member is required
the Posix.1g sockets API standard (which should be completed
1997).
In this document ancillary data is used to exchange the
optional information between the application and the kernel
1. the send/receive interface and source/destination address
2. the hop limit
3. next hop address
4. Hop-by-Hop options
5. Destination options,
6. Routing header
Before describing these uses in detail, we review the definition
the msghdr structure itself, the cmsghdr structure that defines
ancillary data object, and some functions that operate on
ancillary data objects
Stevens & Thomas Informational [Page 17]
RFC 2292 Advanced Sockets API for IPv6 February 1998
4.1. The msghdr
The msghdr structure is used by the recvmsg() and sendmsg()
functions. Its Posix.1g definition is
struct msghdr {
void *msg_name; /* ptr to socket address structure */
socklen_t msg_namelen; /* size of socket address structure */
struct iovec *msg_iov; /* scatter/gather array */
size_t msg_iovlen; /* # elements in msg_iov */
void *msg_control; /* ancillary data */
socklen_t msg_controllen; /* ancillary data buffer length */
int msg_flags; /* flags on received message */
};
The structure is declared as a result of including .
(Note: Before Posix.1g the two "void *" pointers were typically "
*", and the two socklen_t members and the size_t member
typically integers. Earlier drafts of Posix.1g had the two socklen_
members as size_t, but Draft 6.6 of Posix.1g, apparently the
draft, changed these to socklen_t to simplify binary portability
64-bit implementations and to align Posix.1g with X/Open's
Services, Issue 5. The change in msg_control to a "void *"
affects any code that increments this pointer.)
Most Berkeley-derived implementations limit the amount of
data in a call to sendmsg() to no more than 108 bytes (an mbuf).
This API requires a minimum of 10240 bytes of ancillary data, but
is recommended that the amount be limited only by the buffer
reserved by the socket (which can be modified by the SO_SNDBUF
option). (Note: This magic number 10240 was picked as a value
should always be large enough. 108 bytes is clearly too small as
maximum size of a Type 0 Routing header is 376 bytes.)
4.2. The cmsghdr
The cmsghdr structure describes ancillary data objects transferred
recvmsg() and sendmsg(). Its Posix.1g definition is
struct cmsghdr {
socklen_t cmsg_len; /* #bytes, including this header */
int cmsg_level; /* originating protocol */
int cmsg_type; /* protocol-specific type */
/* followed by unsigned char cmsg_data[]; */
};
This structure is declared as a result of including .
Stevens & Thomas Informational [Page 18]
RFC 2292 Advanced Sockets API for IPv6 February 1998
As shown in this definition, normally there is no member with
name cmsg_data[]. Instead, the data portion is accessed using
CMSG_xxx() macros, as described shortly. Nevertheless, it is
to refer to the cmsg_data[] member
(Note: Before Posix.1g the cmsg_len member was an integer, and not
socklen_t. See the Note in the previous section for why socklen_t
used here.)
When ancillary data is sent or received, any number of ancillary
objects can be specified by the msg_control and msg_
members of the msghdr structure, because each object is preceded by
cmsghdr structure defining the object's length (the cmsg_len member).
Historically Berkeley-derived implementations have passed only
object at a time, but this API allows multiple objects to be
in a single call to sendmsg() or recvmsg(). The following
shows two ancillary data objects in a control buffer
|<--------------------------- msg_controllen -------------------------->|
| |
|<----- ancillary data object ----->|<----- ancillary data object ----->|
|<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
| | |
|<---------- cmsg_len ---------->| |<--------- cmsg_len ----------->| |
|<--------- CMSG_LEN() --------->| |<-------- CMSG_LEN() ---------->| |
| | | | |
+-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
|cmsg_|cmsg_|cmsg_|XX| |XX|cmsg_|cmsg_|cmsg_|XX| |XX
|len |level|type |XX|cmsg_data[]|XX|len |level|type |XX|cmsg_data[]|XX
+-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
^
|
msg_
points
The fields shown as "XX" are possible padding, between the
structure and the data, and between the data and the next
structure, if required by the implementation
4.3. Ancillary Data Object
To aid in the manipulation of ancillary data objects, three
from 4.4BSD are defined by Posix.1g: CMSG_DATA(), CMSG_NXTHDR(),
CMSG_FIRSTHDR(). Before describing these macros, we show
following example of how they might be used with a call to recvmsg().
struct msghdr msg
struct cmsghdr *cmsgptr
Stevens & Thomas Informational [Page 19]
RFC 2292 Advanced Sockets API for IPv6 February 1998
/* fill in msg */
/* call recvmsg() */
for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL
cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
if (cmsgptr->cmsg_level == ... && cmsgptr->cmsg_type == ... ) {
u_char *ptr
ptr = CMSG_DATA(cmsgptr);
/* process data pointed to by ptr */
}
}
We now describe the three Posix.1g macros, followed by two more
are new with this API: CMSG_SPACE() and CMSG_LEN(). All these
are defined as a result of including .
4.3.1. CMSG_
struct cmsghdr *CMSG_FIRSTHDR(const struct msghdr *mhdr);
CMSG_FIRSTHDR() returns a pointer to the first cmsghdr structure
the msghdr structure pointed to by mhdr. The macro returns NULL
there is no ancillary data pointed to the by msghdr structure (
is, if either msg_control is NULL or if msg_controllen is less
the size of a cmsghdr structure).
One possible implementation could
#define CMSG_FIRSTHDR(mhdr) \
( (mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
(struct cmsghdr *)(mhdr)->msg_control : \
(struct cmsghdr *)NULL )
(Note: Most existing implementations do not test the value
msg_controllen, and just return the value of msg_control. The
of msg_controllen must be tested, because if the application
recvmsg() to return ancillary data, by setting msg_control to
to the application's buffer and setting msg_controllen to the
of this buffer, the kernel indicates that no ancillary data
available by setting msg_controllen to 0 on return. It is
easier to put this test into this macro, than making the
perform the test.)
Stevens & Thomas Informational [Page 20]
RFC 2292 Advanced Sockets API for IPv6 February 1998
4.3.2. CMSG_
struct cmsghdr *CMSG_NXTHDR(const struct msghdr *mhdr
const struct cmsghdr *cmsg);
CMSG_NXTHDR() returns a pointer to the cmsghdr structure
the next ancillary data object. mhdr is a pointer to a
structure and cmsg is a pointer to a cmsghdr structure. If there
not another ancillary data object, the return value is NULL
The following behavior of this macro is new to this API: if the
of the cmsg pointer is NULL, a pointer to the cmsghdr
describing the first ancillary data object is returned. That is
CMSG_NXTHDR(mhdr, NULL) is equivalent to CMSG_FIRSTHDR(mhdr).
there are no ancillary data objects, the return value is NULL.
provides an alternative way of coding the processing loop
earlier
struct msghdr msg
struct cmsghdr *cmsgptr = NULL
/* fill in msg */
/* call recvmsg() */
while ((cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) != NULL) {
if (cmsgptr->cmsg_level == ... && cmsgptr->cmsg_type == ... ) {
u_char *ptr
ptr = CMSG_DATA(cmsgptr);
/* process data pointed to by ptr */
}
One possible implementation could be
#define CMSG_NXTHDR(mhdr, cmsg) \
( ((cmsg) == NULL) ? CMSG_FIRSTHDR(mhdr) : \
(((u_char *)(cmsg) + ALIGN((cmsg)->cmsg_len) \
+ ALIGN(sizeof(struct cmsghdr)) > \
(u_char *)((mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
(struct cmsghdr *)NULL : \
(struct cmsghdr *)((u_char *)(cmsg) + ALIGN((cmsg)->cmsg_len))) )
The macro ALIGN(), which is implementation dependent, rounds
argument up to the next even multiple of whatever alignment
required (probably a multiple of 4 or 8 bytes).
Stevens & Thomas Informational [Page 21]
RFC 2292 Advanced Sockets API for IPv6 February 1998
4.3.3. CMSG_
unsigned char *CMSG_DATA(const struct cmsghdr *cmsg);
CMSG_DATA() returns a pointer to the data (what is called
cmsg_data[] member, even though such a member is not defined in
structure) following a cmsghdr structure
One possible implementation could be
#define CMSG_DATA(cmsg) ( (u_char *)(cmsg) + \
ALIGN(sizeof(struct cmsghdr)) )
4.3.4. CMSG_
unsigned int CMSG_SPACE(unsigned int length);
This macro is new with this API. Given the length of an
data object, CMSG_SPACE() returns the space required by the
and its cmsghdr structure, including any padding needed to
alignment requirements. This macro can be used, for example,
allocate space dynamically for the ancillary data. This macro
not be used to initialize the cmsg_len member of a cmsghdr structure
instead use the CMSG_LEN() macro
One possible implementation could be
#define CMSG_SPACE(length) ( ALIGN(sizeof(struct cmsghdr)) + \
ALIGN(length) )
4.3.5. CMSG_
unsigned int CMSG_LEN(unsigned int length);
This macro is new with this API. Given the length of an
data object, CMSG_LEN() returns the value to store in the cmsg_
member of the cmsghdr structure, taking into account any
needed to satisfy alignment requirements
One possible implementation could be
#define CMSG_LEN(length) ( ALIGN(sizeof(struct cmsghdr)) +
)
Stevens & Thomas Informational [Page 22]
RFC 2292 Advanced Sockets API for IPv6 February 1998
Note the difference between CMSG_SPACE() and CMSG_LEN(), shown
in the figure in Section 4.2: the former accounts for any
padding at the end of the ancillary data object and the latter is
actual length to store in the cmsg_len member of the ancillary
object
4.4. Summary of Options Described Using Ancillary
There are six types of optional information described in
document that are passed between the application and the kernel
ancillary data
1. the send/receive interface and source/destination address
2. the hop limit
3. next hop address
4. Hop-by-Hop options
5. Destination options,
6. Routing header
First, to receive any of this optional information (other than
next hop address, which can only be set), the application must
setsockopt() to turn on the corresponding flag
int on = 1;
setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on));
setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, sizeof(on));
setsockopt(fd, IPPROTO_IPV6, IPV6_HOPOPTS, &on, sizeof(on));
setsockopt(fd, IPPROTO_IPV6, IPV6_DSTOPTS, &on, sizeof(on));
setsockopt(fd, IPPROTO_IPV6, IPV6_RTHDR, &on, sizeof(on));
When any of these options are enabled, the corresponding data
returned as control information by recvmsg(), as one or
ancillary data objects
Nothing special need be done to send any of this
information; the application just calls sendmsg() and specifies
or more ancillary data objects as control information
We also summarize the three cmsghdr fields that describe
ancillary data objects
cmsg_level cmsg_type cmsg_data[] #
------------ ------------ ------------------------ ------
IPPROTO_IPV6 IPV6_PKTINFO in6_pktinfo structure
IPPROTO_IPV6 IPV6_HOPLIMIT int
IPPROTO_IPV6 IPV6_NEXTHOP socket address structure
IPPROTO_IPV6 IPV6_HOPOPTS implementation dependent mult
Stevens & Thomas Informational [Page 23]
RFC 2292 Advanced Sockets API for IPv6 February 1998
IPPROTO_IPV6 IPV6_DSTOPTS implementation dependent mult
IPPROTO_IPV6 IPV6_RTHDR implementation dependent
The final column indicates how many times an ancillary data object
that type can appear as control information. The Hop-by-Hop
Destination options can appear multiple times, while all the
can appear only one time
All these options are described in detail in following sections.
the constants beginning with IPV6_ are defined as a result
including the header
(Note: We intentionally use the same constant for the cmsg_
member as is used as the second argument to getsockopt()
setsockopt() (what is called the "level"), and the same constant
the cmsg_type member as is used as the third argument to getsockopt()
and setsockopt() (what is called the "option name"). This
consistent with the existing use of ancillary data in 4.4BSD
returning the destination address of an IPv4 datagram.)
(Note: It is up to the implementation what it passes as
data for the Hop-by-Hop option, Destination option, and
header option, since the API to these features is through a set
inet6_option_XXX() and inet6_rthdr_XXX() functions that we
later. These functions serve two purposes: to simplify the
to these features (instead of requiring the application to know
intimate details of the extension header formats), and to hide
actual implementation from the application. Nevertheless, we
some examples of these features that store the actual
header as the ancillary data. Implementations need not use
technique.)
4.5. IPV6_PKTOPTIONS Socket
The summary in the previous section assumes a UDP socket.
and receiving ancillary data is easy with UDP: the application
sendmsg() and recvmsg() instead of sendto() and recvfrom().
But there might be cases where a TCP application wants to send
receive this optional information. For example, a TCP client
want to specify a Routing header and this needs to be done
calling connect(). Similarly a TCP server might want to know
received interface after accept() returns along with any
options
Stevens & Thomas Informational [Page 24]
RFC 2292 Advanced Sockets API for IPv6 February 1998
A new socket option is defined that provides access to the
information described in the previous section, but without
recvmsg() and sendmsg(). Setting the socket option specifies any
the optional output fields
setsockopt(fd, IPPROTO_IPV6, IPV6_PKTOPTIONS, &buf, len);
The fourth argument points to a buffer containing one or
ancillary data objects, and the fifth argument is the total length
all these objects. The application fills in this buffer exactly
if the buffer were being passed to sendmsg() as control information
The options set by calling setsockopt() for IPV6_PKTOPTIONS
called "sticky" options because once set they apply to all
sent on that socket. The application can call setsockopt() again
change all the sticky options, or it can call setsockopt() with
length of 0 to remove all the sticky options for the socket
The corresponding receive
getsockopt(fd, IPPROTO_IPV6, IPV6_PKTOPTIONS, &buf, &len);
returns a buffer with one or more ancillary data objects for all
optional receive information that the application has
specified that it wants to receive. The fourth argument points
the buffer that is filled in by the call. The fifth argument is
pointer to a value-result integer: when the function is called
integer specifies the size of the buffer pointed to by the
argument, and on return this integer contains the actual number
bytes that were returned. The application processes this
exactly as if the buffer were returned by recvmsg() as
information
To simplify this document, in the remaining sections when we say "
be specified as ancillary data to sendmsg()" we mean "can
specified as ancillary data to sendmsg() or specified as a
option using setsockopt() and the IPV6_PKTOPTIONS socket option".
Similarly when we say "can be returned as ancillary data
recvmsg()" we mean "can be returned as ancillary data by recvmsg()
returned by getsockopt() with the IPV6_PKTOPTIONS socket option".
4.5.1. TCP Sticky
When using getsockopt() with the IPV6_PKTOPTIONS option and a
socket, only the options from the most recently received segment
retained and returned to the caller, and only after the socket
has been set. That is, TCP need not start saving a copy of
options until the application says to do so
Stevens & Thomas Informational [Page 25]
RFC 2292 Advanced Sockets API for IPv6 February 1998
The application is not allowed to specify ancillary data in a call
sendmsg() on a TCP socket, and none of the ancillary data that
describe in this document is ever returned as control information
recvmsg() on a TCP socket
4.5.2. UDP and Raw Socket Sticky
The IPV6_PKTOPTIONS socket option can also be used with a UDP
or with a raw IPv6 socket, normally to set some of the options once
instead of with each call to sendmsg().
Unlike the TCP case, the sticky options can be overridden on a per
packet basis with ancillary data specified in a call to sendmsg()
a UDP or raw IPv6 socket. If any ancillary data is specified in
call to sendmsg(), none of the sticky options are sent with
datagram
5. Packet
There are four pieces of information that an application can
for an outgoing packet using ancillary data
1. the source IPv6 address
2. the outgoing interface index
3. the outgoing hop limit,
4. the next hop address
Three similar pieces of information can be returned for a
packet as ancillary data
1. the destination IPv6 address
2. the arriving interface index,
3. the arriving hop limit
The first two pieces of information are contained in an in6_
structure that is sent as ancillary data with sendmsg() and
as ancillary data with recvmsg(). This structure is defined as
result of including the header
struct in6_pktinfo {
struct in6_addr ipi6_addr; /* src/dst IPv6 address */
unsigned int ipi6_ifindex; /* send/recv interface index */
};
In the cmsghdr structure containing this ancillary data,
cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will
IPV6_PKTINFO, and the first byte of cmsg_data[] will be the
byte of the in6_pktinfo structure
Stevens & Thomas Informational [Page 26]
RFC 2292 Advanced Sockets API for IPv6 February 1998
This information is returned as ancillary data by recvmsg() only
the application has enabled the IPV6_PKTINFO socket option
int on = 1;
setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on));
Nothing special need be done to send this information: just
the control information as ancillary data for sendmsg().
(Note: The hop limit is not contained in the in6_pktinfo
for the following reason. Some UDP servers want to respond to
requests by sending their reply out the same interface on which
request was received and with the source IPv6 address of the
equal to the destination IPv6 address of the request. To do this
application can enable just the IPV6_PKTINFO socket option and
use the received control information from recvmsg() as the
control information for sendmsg(). The application need not
or modify the in6_pktinfo structure at all. But if the hop
were contained in this structure, the application would have to
the received control information and change the hop limit member
since the received hop limit is not the desired value for an
packet.)
5.1. Specifying/Receiving the
Interfaces on an IPv6 node are identified by a small
integer, as described in Section 4 of [RFC-2133]. That document
describes a function to map an interface name to its interface index
a function to map an interface index to its interface name, and
function to return all the interface names and indexes. Notice
this document that no interface is ever assigned an index of 0.
When specifying the outgoing interface, if the ipi6_ifindex value
0, the kernel will choose the outgoing interface. If the
specifies an outgoing interface for a multicast packet, the
specified by the ancillary data overrides any interface specified
the IPV6_MULTICAST_IF socket option (described in [RFC-2133]),
that call to sendmsg() only
When the IPV6_PKTINFO socket option is enabled, the
interface index is always returned as the ipi6_ifindex member of
in6_pktinfo structure
5.2. Specifying/Receiving Source/Destination
The source IPv6 address can be specified by calling bind()
each output operation, but supplying the source address together
the data requires less overhead (i.e., fewer system calls)
Stevens & Thomas Informational [Page 27]
RFC 2292 Advanced Sockets API for IPv6 February 1998
requires less state to be stored and protected in a
application
When specifying the source IPv6 address as ancillary data, if
ipi6_addr member of the in6_pktinfo structure is the
address (IN6ADDR_ANY_INIT), then (a) if an address is currently
to the socket, it is used as the source address, or (b) if no
is currently bound to the socket, the kernel will choose the
address. If the ipi6_addr member is not the unspecified address,
the socket has already bound a source address, then the ipi6_
value overrides the already-bound source address for this
operation only
The kernel must verify that the requested source address is indeed
unicast address assigned to the node
When the in6_pktinfo structure is returned as ancillary data
recvmsg(), the ipi6_addr member contains the destination IPv6
from the received packet
5.3. Specifying/Receiving the Hop
The outgoing hop limit is normally specified with either
IPV6_UNICAST_HOPS socket option or the IPV6_MULTICAST_HOPS
option, both of which are described in [RFC-2133]. Specifying
hop limit as ancillary data lets the application override either
kernel's default or a previously specified value, for either
unicast destination or a multicast destination, for a single
operation. Returning the received hop limit is useful for
such as Traceroute and for IPv6 applications that need to verify
the received hop limit is 255 (e.g., that the packet has not
forwarded).
The received hop limit is returned as ancillary data by recvmsg()
only if the application has enabled the IPV6_HOPLIMIT socket option
int on = 1;
setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, sizeof(on));
In the cmsghdr structure containing this ancillary data,
cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will
IPV6_HOPLIMIT, and the first byte of cmsg_data[] will be the
byte of the integer hop limit
Nothing special need be done to specify the outgoing hop limit:
specify the control information as ancillary data for sendmsg().
specified in [RFC-2133], the interpretation of the integer hop
value
Stevens & Thomas Informational [Page 28]
RFC 2292 Advanced Sockets API for IPv6 February 1998
x < -1: return an error of
x == -1: use kernel
0 <= x <= 255: use
x >= 256: return an error of
5.4. Specifying the Next Hop
The IPV6_NEXTHOP ancillary data object specifies the next hop for
datagram as a socket address structure. In the cmsghdr
containing this ancillary data, the cmsg_level member will
IPPROTO_IPV6, the cmsg_type member will be IPV6_NEXTHOP, and
first byte of cmsg_data[] will be the first byte of the
address structure
This is a privileged option. (Note: It is implementation defined
beyond the scope of this document to define what "privileged" means
Unix systems use this term to mean the process must have an
user ID of 0.)
If the socket address structure contains an IPv6 address (e.g.,
sin6_family member is AF_INET6), then the node identified by
address must be a neighbor of the sending host. If that
equals the destination IPv6 address of the datagram, then this
equivalent to the existing SO_DONTROUTE socket option
5.5. Additional Errors with sendmsg()
With the IPV6_PKTINFO socket option there are no additional
possible with the call to recvmsg(). But when specifying
outgoing interface or the source address, additional errors
possible from sendmsg(). The following are examples, but some
these may not be provided by some implementations, and
implementations may define additional errors
ENXIO The interface specified by ipi6_ifindex does not exist
ENETDOWN The interface specified by ipi6_ifindex is not
for IPv6 use
EADDRNOTAVAIL ipi6_ifindex specifies an interface but the
ipi6_addr is not available for use on that interface
EHOSTUNREACH No route to the destination exists over the
specified by ifi6_ifindex
Stevens & Thomas Informational [Page 29]
RFC 2292 Advanced Sockets API for IPv6 February 1998
6. Hop-By-Hop
A variable number of Hop-by-Hop options can appear in a single Hop
by-Hop options header. Each option in the header is TLV-encoded
a type, length, and value
Today only three Hop-by-Hop options are defined for IPv6 [RFC-1883]:
Jumbo Payload, Pad1, and PadN, although a proposal exists for
router-alert Hop-by-Hop option. The Jumbo Payload option should
be passed back to an application and an application should receive
error if it attempts to set it. This option is processed entirely
the kernel. It is indirectly specified by datagram-
applications as the size of the datagram to send and
passed back to these applications as the length of the
datagram. The two pad options are for alignment purposes and
automatically inserted by a sending kernel when needed and ignored
the receiving kernel. This section of the API is therefore
for future Hop-by-Hop options that an application may need to
and receive
Individual Hop-by-Hop options (and Destination options, which
described shortly, and which are similar to the Hop-by-Hop options
may have specific alignment requirements. For example, the 4-
Jumbo Payload length should appear on a 4-byte boundary, and IPv
addresses are normally aligned on an 8-byte boundary.
requirements and the terminology used with these options
discussed in Section 4.2 and Appendix A of [RFC-1883]. The
of each option is specified by two values, called x and y, written
"xn + y". This states that the option must appear at an
multiple of x bytes from the beginning of the options header (x
have the values 1, 2, 4, or 8), plus y bytes (y can have a
between 0 and 7, inclusive). The Pad1 and PadN options are
as needed to maintain the required alignment. Whatever code
either a Hop-by-Hop options header or a Destination options
must know the values of x and y for each option
Multiple Hop-by-Hop options can be specified by the application
Normally one ancillary data object describes all the Hop-by-
options (since each option is itself TLV-encoded) but the
can specify multiple ancillary data objects for the Hop-by-
options, each object specifying one or more options. Care must
taken designing the API for these options
1. it may be possible for some future Hop-by-Hop options to
generated by the application and processed entirely by
application (e.g., the kernel may not know the
restrictions for the option),
Stevens & Thomas Informational [Page 30]
RFC 2292 Advanced Sockets API for IPv6 February 1998
2. it must be possible for the kernel to insert its own Hop-by-
options in an outgoing packet (e.g., the Jumbo Payload option),
3. the application can place one or more Hop-by-Hop options into
single ancillary data object
4. if the application specifies multiple ancillary data objects
each containing one or more Hop-by-Hop options, the kernel
combine these a single Hop-by-Hop options header,
5. it must be possible for the kernel to remove some Hop-by-
options from a received packet before returning the
Hop-by-Hop options to the application. (This removal
consist of the kernel converting the option into a pad option
the same length.)
Finally, we note that access to some Hop-by-Hop options or to
Destination options, might require spe