home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / rfc / 3 / rfc2292.txt < prev    next >
Encoding:
Text File  |  2003-06-11  |  148.5 KB  |  3,756 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7. Network Working Group                                        W. Stevens
  8. Request for Comments: 2292                                   Consultant
  9. Category: Informational                                       M. Thomas
  10.                                                               AltaVista
  11.                                                           February 1998
  12.  
  13.  
  14.  
  15.                      Advanced Sockets API for IPv6
  16.  
  17.  
  18. Status of this Memo
  19.  
  20.    This memo provides information for the Internet community.  It does
  21.    not specify an Internet standard of any kind.  Distribution of this
  22.    memo is unlimited.
  23.  
  24. Copyright Notice
  25.  
  26.    Copyright (C) The Internet Society (1998).  All Rights Reserved.
  27.  
  28. Abstract
  29.  
  30.    Specifications are in progress for changes to the sockets API to
  31.    support IP version 6 [RFC-2133].  These changes are for TCP and UDP-
  32.    based applications and will support most end-user applications in use
  33.    today: Telnet and FTP clients and servers, HTTP clients and servers,
  34.    and the like.
  35.  
  36.    But another class of applications exists that will also be run under
  37.    IPv6.  We call these "advanced" applications and today this includes
  38.    programs such as Ping, Traceroute, routing daemons, multicast routing
  39.    daemons, router discovery daemons, and the like.  The API feature
  40.    typically used by these programs that make them "advanced" is a raw
  41.    socket to access ICMPv4, IGMPv4, or IPv4, along with some knowledge
  42.    of the packet header formats used by these protocols.  To provide
  43.    portability for applications that use raw sockets under IPv6, some
  44.    standardization is needed for the advanced API features.
  45.  
  46.    There are other features of IPv6 that some applications will need to
  47.    access: interface identification (specifying the outgoing interface
  48.    and determining the incoming interface) and IPv6 extension headers
  49.    that are not addressed in [RFC-2133]: Hop-by-Hop options, Destination
  50.    options, and the Routing header (source routing).  This document
  51.    provides API access to these features too.
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58. Stevens & Thomas             Informational                      [Page 1]
  59.  
  60. RFC 2292             Advanced Sockets API for IPv6         February 1998
  61.  
  62.  
  63. Table of Contents
  64.  
  65.     1.  Introduction ................................................3
  66.     2.  Common Structures and Definitions ...........................5
  67.        2.1.  The ip6_hdr Structure ..................................5
  68.             2.1.1.  IPv6 Next Header Values .........................6
  69.             2.1.2.  IPv6 Extension Headers ..........................6
  70.        2.2.  The icmp6_hdr Structure ................................8
  71.             2.2.1.  ICMPv6 Type and Code Values .....................8
  72.             2.2.2.  ICMPv6 Neighbor Discovery Type and Code Values ..9
  73.        2.3.  Address Testing Macros .................................12
  74.        2.4.  Protocols File .........................................12
  75.     3.  IPv6 Raw Sockets ............................................13
  76.        3.1.  Checksums ..............................................14
  77.        3.2.  ICMPv6 Type Filtering ..................................14
  78.     4.  Ancillary Data ..............................................17
  79.        4.1.  The msghdr Structure ...................................18
  80.        4.2.  The cmsghdr Structure ..................................18
  81.        4.3.  Ancillary Data Object Macros ...........................19
  82.             4.3.1.  CMSG_FIRSTHDR ...................................20
  83.             4.3.2.  CMSG_NXTHDR .....................................22
  84.             4.3.3.  CMSG_DATA .......................................22
  85.             4.3.4.  CMSG_SPACE ......................................22
  86.             4.3.5.  CMSG_LEN ........................................22
  87.        4.4.  Summary of Options Described Using Ancillary Data ......23
  88.        4.5.  IPV6_PKTOPTIONS Socket Option ..........................24
  89.             4.5.1.  TCP Sticky Options ..............................25
  90.             4.5.2.  UDP and Raw Socket Sticky Options ...............26
  91.     5.  Packet Information ..........................................26
  92.        5.1.  Specifying/Receiving the Interface .....................27
  93.        5.2.  Specifying/Receiving Source/Destination Address ........27
  94.        5.3.  Specifying/Receiving the Hop Limit .....................28
  95.        5.4.  Specifying the Next Hop Address ........................29
  96.        5.5.  Additional Errors with sendmsg() .......................29
  97.     6.  Hop-By-Hop Options ..........................................30
  98.        6.1.  Receiving Hop-by-Hop Options ...........................31
  99.        6.2.  Sending Hop-by-Hop Options .............................31
  100.        6.3.  Hop-by-Hop and Destination Options Processing ..........32
  101.             6.3.1.  inet6_option_space ..............................32
  102.             6.3.2.  inet6_option_init ...............................32
  103.             6.3.3.  inet6_option_append .............................33
  104.             6.3.4.  inet6_option_alloc ..............................33
  105.             6.3.5.  inet6_option_next ...............................34
  106.             6.3.6.  inet6_option_find ...............................35
  107.             6.3.7.  Options Examples ................................35
  108.     7.  Destination Options .........................................42
  109.        7.1.  Receiving Destination Options ..........................42
  110.        7.2.  Sending Destination Options ............................43
  111.  
  112.  
  113.  
  114. Stevens & Thomas             Informational                      [Page 2]
  115.  
  116. RFC 2292             Advanced Sockets API for IPv6         February 1998
  117.  
  118.  
  119.     8.  Routing Header Option .......................................43
  120.        8.1.  inet6_rthdr_space ......................................44
  121.        8.2.  inet6_rthdr_init .......................................45
  122.        8.3.  inet6_rthdr_add ........................................45
  123.        8.4.  inet6_rthdr_lasthop ....................................46
  124.        8.5.  inet6_rthdr_reverse ....................................46
  125.        8.6.  inet6_rthdr_segments ...................................46
  126.        8.7.  inet6_rthdr_getaddr ....................................46
  127.        8.8.  inet6_rthdr_getflags ...................................47
  128.        8.9.  Routing Header Example .................................47
  129.     9.  Ordering of Ancillary Data and IPv6 Extension Headers .......53
  130.    10.  IPv6-Specific Options with IPv4-Mapped IPv6 Addresses .......54
  131.    11.  rresvport_af ................................................55
  132.    12.  Future Items ................................................55
  133.        12.1.  Flow Labels ...........................................55
  134.        12.2.  Path MTU Discovery and UDP ............................56
  135.        12.3.  Neighbor Reachability and UDP .........................56
  136.    13.  Summary of New Definitions ..................................56
  137.    14.  Security Considerations .....................................59
  138.    15.  Change History ..............................................59
  139.    16.  References ..................................................65
  140.    17.  Acknowledgments .............................................65
  141.    18.  Authors' Addresses ..........................................66
  142.    19.  Full Copyright Statement ....................................67
  143.  
  144. 1.  Introduction
  145.  
  146.    Specifications are in progress for changes to the sockets API to
  147.    support IP version 6 [RFC-2133].  These changes are for TCP and UDP-
  148.    based applications.  The current document defines some the "advanced"
  149.    features of the sockets API that are required for applications to
  150.    take advantage of additional features of IPv6.
  151.  
  152.    Today, the portability of applications using IPv4 raw sockets is
  153.    quite high, but this is mainly because most IPv4 implementations
  154.    started from a common base (the Berkeley source code) or at least
  155.    started with the Berkeley headers.  This allows programs such as Ping
  156.    and Traceroute, for example, to compile with minimal effort on many
  157.    hosts that support the sockets API.  With IPv6, however, there is no
  158.    common source code base that implementors are starting from, and the
  159.    possibility for divergence at this level between different
  160.    implementations is high.  To avoid a complete lack of portability
  161.    amongst applications that use raw IPv6 sockets, some standardization
  162.    is necessary.
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170. Stevens & Thomas             Informational                      [Page 3]
  171.  
  172. RFC 2292             Advanced Sockets API for IPv6         February 1998
  173.  
  174.  
  175.    There are also features from the basic IPv6 specification that are
  176.    not addressed in [RFC-2133]: sending and receiving Hop-by-Hop
  177.    options, Destination options, and Routing headers, specifying the
  178.    outgoing interface, and being told of the receiving interface.
  179.  
  180.    This document can be divided into the following main sections.
  181.  
  182.    1.  Definitions of the basic constants and structures required for
  183.        applications to use raw IPv6 sockets.  This includes structure
  184.        definitions for the IPv6 and ICMPv6 headers and all associated
  185.        constants (e.g., values for the Next Header field).
  186.  
  187.    2.  Some basic semantic definitions for IPv6 raw sockets.  For
  188.        example, a raw ICMPv4 socket requires the application to
  189.        calculate and store the ICMPv4 header checksum.  But with IPv6
  190.        this would require the application to choose the source IPv6
  191.        address because the source address is part of the pseudo header
  192.        that ICMPv6 now uses for its checksum computation.  It should be
  193.        defined that with a raw ICMPv6 socket the kernel always
  194.        calculates and stores the ICMPv6 header checksum.
  195.  
  196.    3.  Packet information: how applications can obtain the received
  197.        interface, destination address, and received hop limit, along
  198.        with specifying these values on a per-packet basis.  There are a
  199.        class of applications that need this capability and the technique
  200.        should be portable.
  201.  
  202.    4.  Access to the optional Hop-by-Hop, Destination, and Routing
  203.        headers.
  204.  
  205.    5.  Additional features required for IPv6 application portability.
  206.  
  207.    The packet information along with access to the extension headers
  208.    (Hop-by-Hop options, Destination options, and Routing header) are
  209.    specified using the "ancillary data" fields that were added to the
  210.    4.3BSD Reno sockets API in 1990.  The reason is that these ancillary
  211.    data fields are part of the Posix.1g standard (which should be
  212.    approved in 1997) and should therefore be adopted by most vendors.
  213.  
  214.    This document does not address application access to either the
  215.    authentication header or the encapsulating security payload header.
  216.  
  217.    All examples in this document omit error checking in favor of brevity
  218.    and clarity.
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226. Stevens & Thomas             Informational                      [Page 4]
  227.  
  228. RFC 2292             Advanced Sockets API for IPv6         February 1998
  229.  
  230.  
  231.    We note that many of the functions and socket options defined in this
  232.    document may have error returns that are not defined in this
  233.    document.  Many of these possible error returns will be recognized
  234.    only as implementations proceed.
  235.  
  236.    Datatypes in this document follow the Posix.1g format: intN_t means a
  237.    signed integer of exactly N bits (e.g., int16_t) and uintN_t means an
  238.    unsigned integer of exactly N bits (e.g., uint32_t).
  239.  
  240.    Note that we use the (unofficial) terminology ICMPv4, IGMPv4, and
  241.    ARPv4 to avoid any confusion with the newer ICMPv6 protocol.
  242.  
  243. 2.  Common Structures and Definitions
  244.  
  245.    Many advanced applications examine fields in the IPv6 header and set
  246.    and examine fields in the various ICMPv6 headers.  Common structure
  247.    definitions for these headers are required, along with common
  248.    constant definitions for the structure members.
  249.  
  250.    Two new headers are defined: <netinet/ip6.h> and <netinet/icmp6.h>.
  251.  
  252.    When an include file is specified, that include file is allowed to
  253.    include other files that do the actual declaration or definition.
  254.  
  255. 2.1.  The ip6_hdr Structure
  256.  
  257.    The following structure is defined as a result of including
  258.    <netinet/ip6.h>.  Note that this is a new header.
  259.  
  260.     struct ip6_hdr {
  261.       union {
  262.         struct ip6_hdrctl {
  263.           uint32_t ip6_un1_flow;   /* 24 bits of flow-ID */
  264.           uint16_t ip6_un1_plen;   /* payload length */
  265.           uint8_t  ip6_un1_nxt;    /* next header */
  266.           uint8_t  ip6_un1_hlim;   /* hop limit */
  267.         } ip6_un1;
  268.         uint8_t ip6_un2_vfc;       /* 4 bits version, 4 bits priority */
  269.       } ip6_ctlun;
  270.       struct in6_addr ip6_src;      /* source address */
  271.       struct in6_addr ip6_dst;      /* destination address */
  272.     };
  273.  
  274.     #define ip6_vfc   ip6_ctlun.ip6_un2_vfc
  275.     #define ip6_flow  ip6_ctlun.ip6_un1.ip6_un1_flow
  276.     #define ip6_plen  ip6_ctlun.ip6_un1.ip6_un1_plen
  277.     #define ip6_nxt   ip6_ctlun.ip6_un1.ip6_un1_nxt
  278.     #define ip6_hlim  ip6_ctlun.ip6_un1.ip6_un1_hlim
  279.  
  280.  
  281.  
  282. Stevens & Thomas             Informational                      [Page 5]
  283.  
  284. RFC 2292             Advanced Sockets API for IPv6         February 1998
  285.  
  286.  
  287.     #define ip6_hops  ip6_ctlun.ip6_un1.ip6_un1_hlim
  288.  
  289. 2.1.1.  IPv6 Next Header Values
  290.  
  291.    IPv6 defines many new values for the Next Header field.  The
  292.    following constants are defined as a result of including
  293.    <netinet/in.h>.
  294.  
  295.    #define IPPROTO_HOPOPTS        0 /* IPv6 Hop-by-Hop options */
  296.    #define IPPROTO_IPV6          41 /* IPv6 header */
  297.    #define IPPROTO_ROUTING       43 /* IPv6 Routing header */
  298.    #define IPPROTO_FRAGMENT      44 /* IPv6 fragmentation header */
  299.    #define IPPROTO_ESP           50 /* encapsulating security payload */
  300.    #define IPPROTO_AH            51 /* authentication header */
  301.    #define IPPROTO_ICMPV6        58 /* ICMPv6 */
  302.    #define IPPROTO_NONE          59 /* IPv6 no next header */
  303.    #define IPPROTO_DSTOPTS       60 /* IPv6 Destination options */
  304.  
  305.    Berkeley-derived IPv4 implementations also define IPPROTO_IP to be 0.
  306.    This should not be a problem since IPPROTO_IP is used only with IPv4
  307.    sockets and IPPROTO_HOPOPTS only with IPv6 sockets.
  308.  
  309. 2.1.2.  IPv6 Extension Headers
  310.  
  311.    Six extension headers are defined for IPv6.  We define structures for
  312.    all except the Authentication header and Encapsulating Security
  313.    Payload header, both of which are beyond the scope of this document.
  314.    The following structures are defined as a result of including
  315.    <netinet/ip6.h>.
  316.  
  317.    /* Hop-by-Hop options header */
  318.    /* XXX should we pad it to force alignment on an 8-byte boundary? */
  319.    struct ip6_hbh {
  320.      uint8_t  ip6h_nxt;        /* next header */
  321.      uint8_t  ip6h_len;        /* length in units of 8 octets */
  322.        /* followed by options */
  323.    };
  324.  
  325.    /* Destination options header */
  326.    /* XXX should we pad it to force alignment on an 8-byte boundary? */
  327.    struct ip6_dest {
  328.      uint8_t  ip6d_nxt;        /* next header */
  329.      uint8_t  ip6d_len;        /* length in units of 8 octets */
  330.        /* followed by options */
  331.    };
  332.  
  333.    /* Routing header */
  334.    struct ip6_rthdr {
  335.  
  336.  
  337.  
  338. Stevens & Thomas             Informational                      [Page 6]
  339.  
  340. RFC 2292             Advanced Sockets API for IPv6         February 1998
  341.  
  342.  
  343.      uint8_t  ip6r_nxt;        /* next header */
  344.      uint8_t  ip6r_len;        /* length in units of 8 octets */
  345.      uint8_t  ip6r_type;       /* routing type */
  346.      uint8_t  ip6r_segleft;    /* segments left */
  347.        /* followed by routing type specific data */
  348.    };
  349.  
  350.    /* Type 0 Routing header */
  351.    struct ip6_rthdr0 {
  352.      uint8_t  ip6r0_nxt;       /* next header */
  353.      uint8_t  ip6r0_len;       /* length in units of 8 octets */
  354.      uint8_t  ip6r0_type;      /* always zero */
  355.      uint8_t  ip6r0_segleft;   /* segments left */
  356.      uint8_t  ip6r0_reserved;  /* reserved field */
  357.      uint8_t  ip6r0_slmap[3];  /* strict/loose bit map */
  358.      struct in6_addr  ip6r0_addr[1];  /* up to 23 addresses */
  359.    };
  360.  
  361.    /* Fragment header */
  362.    struct ip6_frag {
  363.      uint8_t   ip6f_nxt;       /* next header */
  364.      uint8_t   ip6f_reserved;  /* reserved field */
  365.      uint16_t  ip6f_offlg;     /* offset, reserved, and flag */
  366.      uint32_t  ip6f_ident;     /* identification */
  367.    };
  368.  
  369.    #if     BYTE_ORDER == BIG_ENDIAN
  370.    #define IP6F_OFF_MASK       0xfff8  /* mask out offset from _offlg */
  371.    #define IP6F_RESERVED_MASK  0x0006  /* reserved bits in ip6f_offlg */
  372.    #define IP6F_MORE_FRAG      0x0001  /* more-fragments flag */
  373.    #else   /* BYTE_ORDER == LITTLE_ENDIAN */
  374.    #define IP6F_OFF_MASK       0xf8ff  /* mask out offset from _offlg */
  375.    #define IP6F_RESERVED_MASK  0x0600  /* reserved bits in ip6f_offlg */
  376.    #define IP6F_MORE_FRAG      0x0100  /* more-fragments flag */
  377.    #endif
  378.  
  379.    Defined constants for fields larger than 1 byte depend on the byte
  380.    ordering that is used.  This API assumes that the fields in the
  381.    protocol headers are left in the network byte order, which is big-
  382.    endian for the Internet protocols.  If not, then either these
  383.    constants or the fields being tested must be converted at run-time,
  384.    using something like htons() or htonl().
  385.  
  386.    (Note: We show an implementation that supports both big-endian and
  387.    little-endian byte ordering, assuming a hypothetical compile-time #if
  388.    test to determine the byte ordering.  The constant that we show,
  389.  
  390.  
  391.  
  392.  
  393.  
  394. Stevens & Thomas             Informational                      [Page 7]
  395.  
  396. RFC 2292             Advanced Sockets API for IPv6         February 1998
  397.  
  398.  
  399.    BYTE_ORDER, with values of BIG_ENDIAN and LITTLE_ENDIAN, are for
  400.    example purposes only.  If an implementation runs on only one type of
  401.    hardware it need only define the set of constants for that hardware's
  402.    byte ordering.)
  403.  
  404. 2.2.  The icmp6_hdr Structure
  405.  
  406.    The ICMPv6 header is needed by numerous IPv6 applications including
  407.    Ping, Traceroute, router discovery daemons, and neighbor discovery
  408.    daemons.  The following structure is defined as a result of including
  409.    <netinet/icmp6.h>.  Note that this is a new header.
  410.  
  411.    struct icmp6_hdr {
  412.      uint8_t     icmp6_type;   /* type field */
  413.      uint8_t     icmp6_code;   /* code field */
  414.      uint16_t    icmp6_cksum;  /* checksum field */
  415.      union {
  416.        uint32_t  icmp6_un_data32[1]; /* type-specific field */
  417.        uint16_t  icmp6_un_data16[2]; /* type-specific field */
  418.        uint8_t   icmp6_un_data8[4];  /* type-specific field */
  419.      } icmp6_dataun;
  420.    };
  421.  
  422.    #define icmp6_data32    icmp6_dataun.icmp6_un_data32
  423.    #define icmp6_data16    icmp6_dataun.icmp6_un_data16
  424.    #define icmp6_data8     icmp6_dataun.icmp6_un_data8
  425.    #define icmp6_pptr      icmp6_data32[0]  /* parameter prob */
  426.    #define icmp6_mtu       icmp6_data32[0]  /* packet too big */
  427.    #define icmp6_id        icmp6_data16[0]  /* echo request/reply */
  428.    #define icmp6_seq       icmp6_data16[1]  /* echo request/reply */
  429.    #define icmp6_maxdelay  icmp6_data16[0]  /* mcast group membership */
  430.  
  431. 2.2.1.  ICMPv6 Type and Code Values
  432.  
  433.    In addition to a common structure for the ICMPv6 header, common
  434.    definitions are required for the ICMPv6 type and code fields.  The
  435.    following constants are also defined as a result of including
  436.    <netinet/icmp6.h>.
  437.  
  438. #define ICMP6_DST_UNREACH             1
  439. #define ICMP6_PACKET_TOO_BIG          2
  440. #define ICMP6_TIME_EXCEEDED           3
  441. #define ICMP6_PARAM_PROB              4
  442.  
  443. #define ICMP6_INFOMSG_MASK  0x80    /* all informational messages */
  444.  
  445. #define ICMP6_ECHO_REQUEST          128
  446. #define ICMP6_ECHO_REPLY            129
  447.  
  448.  
  449.  
  450. Stevens & Thomas             Informational                      [Page 8]
  451.  
  452. RFC 2292             Advanced Sockets API for IPv6         February 1998
  453.  
  454.  
  455. #define ICMP6_MEMBERSHIP_QUERY      130
  456. #define ICMP6_MEMBERSHIP_REPORT     131
  457. #define ICMP6_MEMBERSHIP_REDUCTION  132
  458.  
  459. #define ICMP6_DST_UNREACH_NOROUTE     0 /* no route to destination */
  460. #define ICMP6_DST_UNREACH_ADMIN       1 /* communication with */
  461.                                         /* destination */
  462.                                         /* administratively */
  463.                                         /* prohibited */
  464. #define ICMP6_DST_UNREACH_NOTNEIGHBOR 2 /* not a neighbor */
  465. #define ICMP6_DST_UNREACH_ADDR        3 /* address unreachable */
  466. #define ICMP6_DST_UNREACH_NOPORT      4 /* bad port */
  467.  
  468. #define ICMP6_TIME_EXCEED_TRANSIT     0 /* Hop Limit == 0 in transit */
  469. #define ICMP6_TIME_EXCEED_REASSEMBLY  1 /* Reassembly time out */
  470.  
  471. #define ICMP6_PARAMPROB_HEADER        0 /* erroneous header field */
  472. #define ICMP6_PARAMPROB_NEXTHEADER    1 /* unrecognized Next Header */
  473. #define ICMP6_PARAMPROB_OPTION        2 /* unrecognized IPv6 option */
  474.  
  475.    The five ICMP message types defined by IPv6 neighbor discovery (133-
  476.    137) are defined in the next section.
  477.  
  478. 2.2.2.  ICMPv6 Neighbor Discovery Type and Code Values
  479.  
  480.    The following structures and definitions are defined as a result of
  481.    including <netinet/icmp6.h>.
  482.  
  483.    #define ND_ROUTER_SOLICIT           133
  484.    #define ND_ROUTER_ADVERT            134
  485.    #define ND_NEIGHBOR_SOLICIT         135
  486.    #define ND_NEIGHBOR_ADVERT          136
  487.    #define ND_REDIRECT                 137
  488.  
  489.    struct nd_router_solicit {     /* router solicitation */
  490.      struct icmp6_hdr  nd_rs_hdr;
  491.        /* could be followed by options */
  492.    };
  493.  
  494.    #define nd_rs_type               nd_rs_hdr.icmp6_type
  495.    #define nd_rs_code               nd_rs_hdr.icmp6_code
  496.    #define nd_rs_cksum              nd_rs_hdr.icmp6_cksum
  497.    #define nd_rs_reserved           nd_rs_hdr.icmp6_data32[0]
  498.  
  499.    struct nd_router_advert {      /* router advertisement */
  500.      struct icmp6_hdr  nd_ra_hdr;
  501.      uint32_t   nd_ra_reachable;   /* reachable time */
  502.      uint32_t   nd_ra_retransmit;  /* retransmit timer */
  503.  
  504.  
  505.  
  506. Stevens & Thomas             Informational                      [Page 9]
  507.  
  508. RFC 2292             Advanced Sockets API for IPv6         February 1998
  509.  
  510.  
  511.        /* could be followed by options */
  512.    };
  513.  
  514.    #define nd_ra_type               nd_ra_hdr.icmp6_type
  515.    #define nd_ra_code               nd_ra_hdr.icmp6_code
  516.    #define nd_ra_cksum              nd_ra_hdr.icmp6_cksum
  517.    #define nd_ra_curhoplimit        nd_ra_hdr.icmp6_data8[0]
  518.    #define nd_ra_flags_reserved     nd_ra_hdr.icmp6_data8[1]
  519.    #define ND_RA_FLAG_MANAGED       0x80
  520.    #define ND_RA_FLAG_OTHER         0x40
  521.    #define nd_ra_router_lifetime    nd_ra_hdr.icmp6_data16[1]
  522.  
  523.    struct nd_neighbor_solicit {   /* neighbor solicitation */
  524.      struct icmp6_hdr  nd_ns_hdr;
  525.      struct in6_addr   nd_ns_target; /* target address */
  526.        /* could be followed by options */
  527.    };
  528.  
  529.    #define nd_ns_type               nd_ns_hdr.icmp6_type
  530.    #define nd_ns_code               nd_ns_hdr.icmp6_code
  531.    #define nd_ns_cksum              nd_ns_hdr.icmp6_cksum
  532.    #define nd_ns_reserved           nd_ns_hdr.icmp6_data32[0]
  533.  
  534.    struct nd_neighbor_advert {    /* neighbor advertisement */
  535.      struct icmp6_hdr  nd_na_hdr;
  536.      struct in6_addr   nd_na_target; /* target address */
  537.        /* could be followed by options */
  538.    };
  539.  
  540.    #define nd_na_type               nd_na_hdr.icmp6_type
  541.    #define nd_na_code               nd_na_hdr.icmp6_code
  542.    #define nd_na_cksum              nd_na_hdr.icmp6_cksum
  543.    #define nd_na_flags_reserved     nd_na_hdr.icmp6_data32[0]
  544.    #if     BYTE_ORDER == BIG_ENDIAN
  545.    #define ND_NA_FLAG_ROUTER        0x80000000
  546.    #define ND_NA_FLAG_SOLICITED     0x40000000
  547.    #define ND_NA_FLAG_OVERRIDE      0x20000000
  548.    #else   /* BYTE_ORDER == LITTLE_ENDIAN */
  549.    #define ND_NA_FLAG_ROUTER        0x00000080
  550.    #define ND_NA_FLAG_SOLICITED     0x00000040
  551.    #define ND_NA_FLAG_OVERRIDE      0x00000020
  552.    #endif
  553.  
  554.    struct nd_redirect {           /* redirect */
  555.      struct icmp6_hdr  nd_rd_hdr;
  556.      struct in6_addr   nd_rd_target; /* target address */
  557.      struct in6_addr   nd_rd_dst;    /* destination address */
  558.        /* could be followed by options */
  559.  
  560.  
  561.  
  562. Stevens & Thomas             Informational                     [Page 10]
  563.  
  564. RFC 2292             Advanced Sockets API for IPv6         February 1998
  565.  
  566.  
  567.    };
  568.  
  569.    #define nd_rd_type               nd_rd_hdr.icmp6_type
  570.    #define nd_rd_code               nd_rd_hdr.icmp6_code
  571.    #define nd_rd_cksum              nd_rd_hdr.icmp6_cksum
  572.    #define nd_rd_reserved           nd_rd_hdr.icmp6_data32[0]
  573.  
  574.    struct nd_opt_hdr {           /* Neighbor discovery option header */
  575.      uint8_t  nd_opt_type;
  576.      uint8_t  nd_opt_len;        /* in units of 8 octets */
  577.        /* followed by option specific data */
  578.    };
  579.  
  580.    #define  ND_OPT_SOURCE_LINKADDR       1
  581.    #define  ND_OPT_TARGET_LINKADDR       2
  582.    #define  ND_OPT_PREFIX_INFORMATION    3
  583.    #define  ND_OPT_REDIRECTED_HEADER     4
  584.    #define  ND_OPT_MTU                   5
  585.  
  586.    struct nd_opt_prefix_info {    /* prefix information */
  587.      uint8_t   nd_opt_pi_type;
  588.      uint8_t   nd_opt_pi_len;
  589.      uint8_t   nd_opt_pi_prefix_len;
  590.      uint8_t   nd_opt_pi_flags_reserved;
  591.      uint32_t  nd_opt_pi_valid_time;
  592.      uint32_t  nd_opt_pi_preferred_time;
  593.      uint32_t  nd_opt_pi_reserved2;
  594.      struct in6_addr  nd_opt_pi_prefix;
  595.    };
  596.  
  597.    #define ND_OPT_PI_FLAG_ONLINK        0x80
  598.    #define ND_OPT_PI_FLAG_AUTO          0x40
  599.  
  600.    struct nd_opt_rd_hdr {         /* redirected header */
  601.      uint8_t   nd_opt_rh_type;
  602.      uint8_t   nd_opt_rh_len;
  603.      uint16_t  nd_opt_rh_reserved1;
  604.      uint32_t  nd_opt_rh_reserved2;
  605.        /* followed by IP header and data */
  606.    };
  607.  
  608.    struct nd_opt_mtu {            /* MTU option */
  609.      uint8_t   nd_opt_mtu_type;
  610.      uint8_t   nd_opt_mtu_len;
  611.      uint16_t  nd_opt_mtu_reserved;
  612.      uint32_t  nd_opt_mtu_mtu;
  613.    };
  614.  
  615.  
  616.  
  617.  
  618. Stevens & Thomas             Informational                     [Page 11]
  619.  
  620. RFC 2292             Advanced Sockets API for IPv6         February 1998
  621.  
  622.  
  623.    We note that the nd_na_flags_reserved flags have the same byte
  624.    ordering problems as we discussed with ip6f_offlg.
  625.  
  626. 2.3.  Address Testing Macros
  627.  
  628.    The basic API ([RFC-2133]) defines some macros for testing an IPv6
  629.    address for certain properties.  This API extends those definitions
  630.    with additional address testing macros, defined as a result of
  631.    including <netinet/in.h>.
  632.  
  633.     int  IN6_ARE_ADDR_EQUAL(const struct in6_addr *,
  634.                             const struct in6_addr *);
  635.  
  636. 2.4.  Protocols File
  637.  
  638.    Many hosts provide the file /etc/protocols that contains the names of
  639.    the various IP protocols and their protocol number (e.g., the value
  640.    of the protocol field in the IPv4 header for that protocol, such as 1
  641.    for ICMP).  Some programs then call the function getprotobyname() to
  642.    obtain the protocol value that is then specified as the third
  643.    argument to the socket() function.  For example, the Ping program
  644.    contains code of the form
  645.  
  646.        struct protoent  *proto;
  647.  
  648.        proto = getprotobyname("icmp");
  649.  
  650.        s = socket(AF_INET, SOCK_RAW, proto->p_proto);
  651.  
  652.    Common names are required for the new IPv6 protocols in this file, to
  653.    provide portability of applications that call the getprotoXXX()
  654.    functions.
  655.  
  656.    We define the following protocol names with the values shown.  These
  657.    are taken from ftp://ftp.isi.edu/in-notes/iana/assignments/protocol-
  658.    numbers.
  659.  
  660.        hopopt           0    # hop-by-hop options for ipv6
  661.        ipv6            41    # ipv6
  662.        ipv6-route      43    # routing header for ipv6
  663.        ipv6-frag       44    # fragment header for ipv6
  664.        esp             50    # encapsulating security payload for ipv6
  665.        ah              51    # authentication header for ipv6
  666.        ipv6-icmp       58    # icmp for ipv6
  667.        ipv6-nonxt      59    # no next header for ipv6
  668.        ipv6-opts       60    # destination options for ipv6
  669.  
  670.  
  671.  
  672.  
  673.  
  674. Stevens & Thomas             Informational                     [Page 12]
  675.  
  676. RFC 2292             Advanced Sockets API for IPv6         February 1998
  677.  
  678.  
  679. 3.  IPv6 Raw Sockets
  680.  
  681.    Raw sockets bypass the transport layer (TCP or UDP).  With IPv4, raw
  682.    sockets are used to access ICMPv4, IGMPv4, and to read and write IPv4
  683.    datagrams containing a protocol field that the kernel does not
  684.    process.  An example of the latter is a routing daemon for OSPF,
  685.    since it uses IPv4 protocol field 89.  With IPv6 raw sockets will be
  686.    used for ICMPv6 and to read and write IPv6 datagrams containing a
  687.    Next Header field that the kernel does not process.  Examples of the
  688.    latter are a routing daemon for OSPF for IPv6 and RSVP (protocol
  689.    field 46).
  690.  
  691.    All data sent via raw sockets MUST be in network byte order and all
  692.    data received via raw sockets will be in network byte order.  This
  693.    differs from the IPv4 raw sockets, which did not specify a byte
  694.    ordering and typically used the host's byte order.
  695.  
  696.    Another difference from IPv4 raw sockets is that complete packets
  697.    (that is, IPv6 packets with extension headers) cannot be read or
  698.    written using the IPv6 raw sockets API.  Instead, ancillary data
  699.    objects are used to transfer the extension headers, as described
  700.    later in this document.  Should an application need access to the
  701.    complete IPv6 packet, some other technique, such as the datalink
  702.    interfaces BPF or DLPI, must be used.
  703.  
  704.    All fields in the IPv6 header that an application might want to
  705.    change (i.e., everything other than the version number) can be
  706.    modified using ancillary data and/or socket options by the
  707.    application for output.  All fields in a received IPv6 header (other
  708.    than the version number and Next Header fields) and all extension
  709.    headers are also made available to the application as ancillary data
  710.    on input.  Hence there is no need for a socket option similar to the
  711.    IPv4 IP_HDRINCL socket option.
  712.  
  713.    When writing to a raw socket the kernel will automatically fragment
  714.    the packet if its size exceeds the path MTU, inserting the required
  715.    fragmentation headers.  On input the kernel reassembles received
  716.    fragments, so the reader of a raw socket never sees any fragment
  717.    headers.
  718.  
  719.    When we say "an ICMPv6 raw socket" we mean a socket created by
  720.    calling the socket function with the three arguments PF_INET6,
  721.    SOCK_RAW, and IPPROTO_ICMPV6.
  722.  
  723.    Most IPv4 implementations give special treatment to a raw socket
  724.    created with a third argument to socket() of IPPROTO_RAW, whose value
  725.    is normally 255.  We note that this value has no special meaning to
  726.    an IPv6 raw socket (and the IANA currently reserves the value of 255
  727.  
  728.  
  729.  
  730. Stevens & Thomas             Informational                     [Page 13]
  731.  
  732. RFC 2292             Advanced Sockets API for IPv6         February 1998
  733.  
  734.  
  735.    when used as a next-header field).  (Note: This feature was added to
  736.    IPv4 in 1988 by Van Jacobson to support traceroute, allowing a
  737.    complete IP header to be passed by the application, before the
  738.    IP_HDRINCL socket option was added.)
  739.  
  740. 3.1.  Checksums
  741.  
  742.    The kernel will calculate and insert the ICMPv6 checksum for ICMPv6
  743.    raw sockets, since this checksum is mandatory.
  744.  
  745.    For other raw IPv6 sockets (that is, for raw IPv6 sockets created
  746.    with a third argument other than IPPROTO_ICMPV6), the application
  747.    must set the new IPV6_CHECKSUM socket option to have the kernel (1)
  748.    compute and store a checksum for output, and (2) verify the received
  749.    checksum on input, discarding the packet if the checksum is in error.
  750.    This option prevents applications from having to perform source
  751.    address selection on the packets they send.  The checksum will
  752.    incorporate the IPv6 pseudo-header, defined in Section 8.1 of [RFC-
  753.    1883].  This new socket option also specifies an integer offset into
  754.    the user data of where the checksum is located.
  755.  
  756.     int  offset = 2;
  757.     setsockopt(fd, IPPROTO_IPV6, IPV6_CHECKSUM, &offset, sizeof(offset));
  758.  
  759.    By default, this socket option is disabled.  Setting the offset to -1
  760.    also disables the option.  By disabled we mean (1) the kernel will
  761.    not calculate and store a checksum for outgoing packets, and (2) the
  762.    kernel will not verify a checksum for received packets.
  763.  
  764.    (Note: Since the checksum is always calculated by the kernel for an
  765.    ICMPv6 socket, applications are not able to generate ICMPv6 packets
  766.    with incorrect checksums (presumably for testing purposes) using this
  767.    API.)
  768.  
  769. 3.2.  ICMPv6 Type Filtering
  770.  
  771.    ICMPv4 raw sockets receive most ICMPv4 messages received by the
  772.    kernel.  (We say "most" and not "all" because Berkeley-derived
  773.    kernels never pass echo requests, timestamp requests, or address mask
  774.    requests to a raw socket.  Instead these three messages are processed
  775.    entirely by the kernel.)  But ICMPv6 is a superset of ICMPv4, also
  776.    including the functionality of IGMPv4 and ARPv4.  This means that an
  777.    ICMPv6 raw socket can potentially receive many more messages than
  778.    would be received with an ICMPv4 raw socket: ICMP messages similar to
  779.    ICMPv4, along with neighbor solicitations, neighbor advertisements,
  780.    and the three group membership messages.
  781.  
  782.  
  783.  
  784.  
  785.  
  786. Stevens & Thomas             Informational                     [Page 14]
  787.  
  788. RFC 2292             Advanced Sockets API for IPv6         February 1998
  789.  
  790.  
  791.    Most applications using an ICMPv6 raw socket care about only a small
  792.    subset of the ICMPv6 message types.  To transfer extraneous ICMPv6
  793.    messages from the kernel to user can incur a significant overhead.
  794.    Therefore this API includes a method of filtering ICMPv6 messages by
  795.    the ICMPv6 type field.
  796.  
  797.    Each ICMPv6 raw socket has an associated filter whose datatype is
  798.    defined as
  799.  
  800.        struct icmp6_filter;
  801.  
  802.    This structure, along with the macros and constants defined later in
  803.    this section, are defined as a result of including the
  804.    <netinet/icmp6.h> header.
  805.  
  806.    The current filter is fetched and stored using getsockopt() and
  807.    setsockopt() with a level of IPPROTO_ICMPV6 and an option name of
  808.    ICMP6_FILTER.
  809.  
  810.    Six macros operate on an icmp6_filter structure:
  811.  
  812.        void ICMP6_FILTER_SETPASSALL (struct icmp6_filter *);
  813.        void ICMP6_FILTER_SETBLOCKALL(struct icmp6_filter *);
  814.  
  815.        void ICMP6_FILTER_SETPASS ( int, struct icmp6_filter *);
  816.        void ICMP6_FILTER_SETBLOCK( int, struct icmp6_filter *);
  817.  
  818.        int  ICMP6_FILTER_WILLPASS (int, const struct icmp6_filter *);
  819.        int  ICMP6_FILTER_WILLBLOCK(int, const struct icmp6_filter *);
  820.  
  821.    The first argument to the last four macros (an integer) is an ICMPv6
  822.    message type, between 0 and 255.  The pointer argument to all six
  823.    macros is a pointer to a filter that is modified by the first four
  824.    macros examined by the last two macros.
  825.  
  826.    The first two macros, SETPASSALL and SETBLOCKALL, let us specify that
  827.    all ICMPv6 messages are passed to the application or that all ICMPv6
  828.    messages are blocked from being passed to the application.
  829.  
  830.    The next two macros, SETPASS and SETBLOCK, let us specify that
  831.    messages of a given ICMPv6 type should be passed to the application
  832.    or not passed to the application (blocked).
  833.  
  834.    The final two macros, WILLPASS and WILLBLOCK, return true or false
  835.    depending whether the specified message type is passed to the
  836.    application or blocked from being passed to the application by the
  837.    filter pointed to by the second argument.
  838.  
  839.  
  840.  
  841.  
  842. Stevens & Thomas             Informational                     [Page 15]
  843.  
  844. RFC 2292             Advanced Sockets API for IPv6         February 1998
  845.  
  846.  
  847.    When an ICMPv6 raw socket is created, it will by default pass all
  848.    ICMPv6 message types to the application.
  849.  
  850.    As an example, a program that wants to receive only router
  851.    advertisements could execute the following:
  852.  
  853. struct icmp6_filter  myfilt;
  854.  
  855. fd = socket(PF_INET6, SOCK_RAW, IPPROTO_ICMPV6);
  856.  
  857. ICMP6_FILTER_SETBLOCKALL(&myfilt);
  858. ICMP6_FILTER_SETPASS(ND_ROUTER_ADVERT, &myfilt);
  859. setsockopt(fd, IPPROTO_ICMPV6, ICMP6_FILTER, &myfilt, sizeof(myfilt));
  860.  
  861.    The filter structure is declared and then initialized to block all
  862.    messages types.  The filter structure is then changed to allow router
  863.    advertisement messages to be passed to the application and the filter
  864.    is installed using setsockopt().
  865.  
  866.    The icmp6_filter structure is similar to the fd_set datatype used
  867.    with the select() function in the sockets API.  The icmp6_filter
  868.    structure is an opaque datatype and the application should not care
  869.    how it is implemented.  All the application does with this datatype
  870.    is allocate a variable of this type, pass a pointer to a variable of
  871.    this type to getsockopt() and setsockopt(), and operate on a variable
  872.    of this type using the six macros that we just defined.
  873.  
  874.    Nevertheless, it is worth showing a simple implementation of this
  875.    datatype and the six macros.
  876.  
  877. struct icmp6_filter {
  878.   uint32_t  icmp6_filt[8];  /* 8*32 = 256 bits */
  879. };
  880.  
  881. #define ICMP6_FILTER_WILLPASS(type, filterp) \
  882.     ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) != 0)
  883. #define ICMP6_FILTER_WILLBLOCK(type, filterp) \
  884.     ((((filterp)->icmp6_filt[(type) >> 5]) & (1 << ((type) & 31))) == 0)
  885. #define ICMP6_FILTER_SETPASS(type, filterp) \
  886.     ((((filterp)->icmp6_filt[(type) >> 5]) |=  (1 << ((type) & 31))))
  887. #define ICMP6_FILTER_SETBLOCK(type, filterp) \
  888.     ((((filterp)->icmp6_filt[(type) >> 5]) &= ~(1 << ((type) & 31))))
  889. #define ICMP6_FILTER_SETPASSALL(filterp) \
  890.     memset((filterp), 0xFF, sizeof(struct icmp6_filter))
  891. #define ICMP6_FILTER_SETBLOCKALL(filterp) \
  892.     memset((filterp), 0, sizeof(struct icmp6_filter))
  893.  
  894.  
  895.  
  896.  
  897.  
  898. Stevens & Thomas             Informational                     [Page 16]
  899.  
  900. RFC 2292             Advanced Sockets API for IPv6         February 1998
  901.  
  902.  
  903.    (Note: These sample definitions have two limitations that an
  904.    implementation may want to change.  The first four macros evaluate
  905.    their first argument two times.  The second two macros require the
  906.    inclusion of the <string.h> header for the memset() function.)
  907.  
  908. 4.  Ancillary Data
  909.  
  910.    4.2BSD allowed file descriptors to be transferred between separate
  911.    processes across a UNIX domain socket using the sendmsg() and
  912.    recvmsg() functions.  Two members of the msghdr structure,
  913.    msg_accrights and msg_accrightslen, were used to send and receive the
  914.    descriptors.  When the OSI protocols were added to 4.3BSD Reno in
  915.    1990 the names of these two fields in the msghdr structure were
  916.    changed to msg_control and msg_controllen, because they were used by
  917.    the OSI protocols for "control information", although the comments in
  918.    the source code call this "ancillary data".
  919.  
  920.    Other than the OSI protocols, the use of ancillary data has been
  921.    rare.  In 4.4BSD, for example, the only use of ancillary data with
  922.    IPv4 is to return the destination address of a received UDP datagram
  923.    if the IP_RECVDSTADDR socket option is set.  With Unix domain sockets
  924.    ancillary data is still used to send and receive descriptors.
  925.  
  926.    Nevertheless the ancillary data fields of the msghdr structure
  927.    provide a clean way to pass information in addition to the data that
  928.    is being read or written.  The inclusion of the msg_control and
  929.    msg_controllen members of the msghdr structure along with the cmsghdr
  930.    structure that is pointed to by the msg_control member is required by
  931.    the Posix.1g sockets API standard (which should be completed during
  932.    1997).
  933.  
  934.    In this document ancillary data is used to exchange the following
  935.    optional information between the application and the kernel:
  936.  
  937.        1.  the send/receive interface and source/destination address,
  938.        2.  the hop limit,
  939.        3.  next hop address,
  940.        4.  Hop-by-Hop options,
  941.        5.  Destination options, and
  942.        6.  Routing header.
  943.  
  944.    Before describing these uses in detail, we review the definition of
  945.    the msghdr structure itself, the cmsghdr structure that defines an
  946.    ancillary data object, and some functions that operate on the
  947.    ancillary data objects.
  948.  
  949.  
  950.  
  951.  
  952.  
  953.  
  954. Stevens & Thomas             Informational                     [Page 17]
  955.  
  956. RFC 2292             Advanced Sockets API for IPv6         February 1998
  957.  
  958.  
  959. 4.1.  The msghdr Structure
  960.  
  961.    The msghdr structure is used by the recvmsg() and sendmsg()
  962.    functions.  Its Posix.1g definition is:
  963.  
  964.     struct msghdr {
  965.       void      *msg_name;        /* ptr to socket address structure */
  966.       socklen_t  msg_namelen;     /* size of socket address structure */
  967.       struct iovec  *msg_iov;     /* scatter/gather array */
  968.       size_t     msg_iovlen;      /* # elements in msg_iov */
  969.       void      *msg_control;     /* ancillary data */
  970.       socklen_t  msg_controllen;  /* ancillary data buffer length */
  971.       int        msg_flags;       /* flags on received message */
  972.     };
  973.  
  974.    The structure is declared as a result of including <sys/socket.h>.
  975.  
  976.    (Note: Before Posix.1g the two "void *" pointers were typically "char
  977.    *", and the two socklen_t members and the size_t member were
  978.    typically integers.  Earlier drafts of Posix.1g had the two socklen_t
  979.    members as size_t, but Draft 6.6 of Posix.1g, apparently the final
  980.    draft, changed these to socklen_t to simplify binary portability for
  981.    64-bit implementations and to align Posix.1g with X/Open's Networking
  982.    Services, Issue 5.  The change in msg_control to a "void *" pointer
  983.    affects any code that increments this pointer.)
  984.  
  985.    Most Berkeley-derived implementations limit the amount of ancillary
  986.    data in a call to sendmsg() to no more than 108 bytes (an mbuf).
  987.    This API requires a minimum of 10240 bytes of ancillary data, but it
  988.    is recommended that the amount be limited only by the buffer space
  989.    reserved by the socket (which can be modified by the SO_SNDBUF socket
  990.    option).  (Note: This magic number 10240 was picked as a value that
  991.    should always be large enough.  108 bytes is clearly too small as the
  992.    maximum size of a Type 0 Routing header is 376 bytes.)
  993.  
  994. 4.2.  The cmsghdr Structure
  995.  
  996.    The cmsghdr structure describes ancillary data objects transferred by
  997.    recvmsg() and sendmsg().  Its Posix.1g definition is:
  998.  
  999.     struct cmsghdr {
  1000.       socklen_t  cmsg_len;   /* #bytes, including this header */
  1001.       int        cmsg_level; /* originating protocol */
  1002.       int        cmsg_type;  /* protocol-specific type */
  1003.                  /* followed by unsigned char cmsg_data[]; */
  1004.     };
  1005.  
  1006.    This structure is declared as a result of including <sys/socket.h>.
  1007.  
  1008.  
  1009.  
  1010. Stevens & Thomas             Informational                     [Page 18]
  1011.  
  1012. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1013.  
  1014.  
  1015.    As shown in this definition, normally there is no member with the
  1016.    name cmsg_data[].  Instead, the data portion is accessed using the
  1017.    CMSG_xxx() macros, as described shortly.  Nevertheless, it is common
  1018.    to refer to the cmsg_data[] member.
  1019.  
  1020.    (Note: Before Posix.1g the cmsg_len member was an integer, and not a
  1021.    socklen_t.  See the Note in the previous section for why socklen_t is
  1022.    used here.)
  1023.  
  1024.    When ancillary data is sent or received, any number of ancillary data
  1025.    objects can be specified by the msg_control and msg_controllen
  1026.    members of the msghdr structure, because each object is preceded by a
  1027.    cmsghdr structure defining the object's length (the cmsg_len member).
  1028.    Historically Berkeley-derived implementations have passed only one
  1029.    object at a time, but this API allows multiple objects to be passed
  1030.    in a single call to sendmsg() or recvmsg().  The following example
  1031.    shows two ancillary data objects in a control buffer.
  1032.  
  1033. |<--------------------------- msg_controllen -------------------------->|
  1034. |                                                                       |
  1035. |<----- ancillary data object ----->|<----- ancillary data object ----->|
  1036. |<---------- CMSG_SPACE() --------->|<---------- CMSG_SPACE() --------->|
  1037. |                                   |                                   |
  1038. |<---------- cmsg_len ---------->|  |<--------- cmsg_len ----------->|  |
  1039. |<--------- CMSG_LEN() --------->|  |<-------- CMSG_LEN() ---------->|  |
  1040. |                                |  |                                |  |
  1041. +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  1042. |cmsg_|cmsg_|cmsg_|XX|           |XX|cmsg_|cmsg_|cmsg_|XX|           |XX|
  1043. |len  |level|type |XX|cmsg_data[]|XX|len  |level|type |XX|cmsg_data[]|XX|
  1044. +-----+-----+-----+--+-----------+--+-----+-----+-----+--+-----------+--+
  1045.  ^
  1046.  |
  1047. msg_control
  1048. points here
  1049.  
  1050.    The fields shown as "XX" are possible padding, between the cmsghdr
  1051.    structure and the data, and between the data and the next cmsghdr
  1052.    structure, if required by the implementation.
  1053.  
  1054. 4.3.  Ancillary Data Object Macros
  1055.  
  1056.    To aid in the manipulation of ancillary data objects, three macros
  1057.    from 4.4BSD are defined by Posix.1g: CMSG_DATA(), CMSG_NXTHDR(), and
  1058.    CMSG_FIRSTHDR().  Before describing these macros, we show the
  1059.    following example of how they might be used with a call to recvmsg().
  1060.  
  1061.     struct msghdr   msg;
  1062.     struct cmsghdr  *cmsgptr;
  1063.  
  1064.  
  1065.  
  1066. Stevens & Thomas             Informational                     [Page 19]
  1067.  
  1068. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1069.  
  1070.  
  1071.     /* fill in msg */
  1072.  
  1073.     /* call recvmsg() */
  1074.  
  1075.     for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
  1076.          cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
  1077.         if (cmsgptr->cmsg_level == ... && cmsgptr->cmsg_type == ... ) {
  1078.             u_char  *ptr;
  1079.  
  1080.             ptr = CMSG_DATA(cmsgptr);
  1081.             /* process data pointed to by ptr */
  1082.         }
  1083.     }
  1084.  
  1085.    We now describe the three Posix.1g macros, followed by two more that
  1086.    are new with this API: CMSG_SPACE() and CMSG_LEN().  All these macros
  1087.    are defined as a result of including <sys/socket.h>.
  1088.  
  1089. 4.3.1.  CMSG_FIRSTHDR
  1090.  
  1091.        struct cmsghdr *CMSG_FIRSTHDR(const struct msghdr *mhdr);
  1092.  
  1093.    CMSG_FIRSTHDR() returns a pointer to the first cmsghdr structure in
  1094.    the msghdr structure pointed to by mhdr.  The macro returns NULL if
  1095.    there is no ancillary data pointed to the by msghdr structure (that
  1096.    is, if either msg_control is NULL or if msg_controllen is less than
  1097.    the size of a cmsghdr structure).
  1098.  
  1099.    One possible implementation could be
  1100.  
  1101.        #define CMSG_FIRSTHDR(mhdr) \
  1102.            ( (mhdr)->msg_controllen >= sizeof(struct cmsghdr) ? \
  1103.              (struct cmsghdr *)(mhdr)->msg_control : \
  1104.              (struct cmsghdr *)NULL )
  1105.  
  1106.    (Note: Most existing implementations do not test the value of
  1107.    msg_controllen, and just return the value of msg_control.  The value
  1108.    of msg_controllen must be tested, because if the application asks
  1109.    recvmsg() to return ancillary data, by setting msg_control to point
  1110.    to the application's buffer and setting msg_controllen to the length
  1111.    of this buffer, the kernel indicates that no ancillary data is
  1112.    available by setting msg_controllen to 0 on return.  It is also
  1113.    easier to put this test into this macro, than making the application
  1114.    perform the test.)
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122. Stevens & Thomas             Informational                     [Page 20]
  1123.  
  1124. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1125.  
  1126.  
  1127. 4.3.2.  CMSG_NXTHDR
  1128.  
  1129.        struct cmsghdr *CMSG_NXTHDR(const struct msghdr *mhdr,
  1130.                                    const struct cmsghdr *cmsg);
  1131.  
  1132.    CMSG_NXTHDR() returns a pointer to the cmsghdr structure describing
  1133.    the next ancillary data object.  mhdr is a pointer to a msghdr
  1134.    structure and cmsg is a pointer to a cmsghdr structure.  If there is
  1135.    not another ancillary data object, the return value is NULL.
  1136.  
  1137.    The following behavior of this macro is new to this API: if the value
  1138.    of the cmsg pointer is NULL, a pointer to the cmsghdr structure
  1139.    describing the first ancillary data object is returned.  That is,
  1140.    CMSG_NXTHDR(mhdr, NULL) is equivalent to CMSG_FIRSTHDR(mhdr).  If
  1141.    there are no ancillary data objects, the return value is NULL.  This
  1142.    provides an alternative way of coding the processing loop shown
  1143.    earlier:
  1144.  
  1145. struct msghdr  msg;
  1146. struct cmsghdr  *cmsgptr = NULL;
  1147.  
  1148. /* fill in msg */
  1149.  
  1150. /* call recvmsg() */
  1151.  
  1152. while ((cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) != NULL) {
  1153.     if (cmsgptr->cmsg_level == ... && cmsgptr->cmsg_type == ... ) {
  1154.         u_char  *ptr;
  1155.  
  1156.         ptr = CMSG_DATA(cmsgptr);
  1157.         /* process data pointed to by ptr */
  1158.     }
  1159. }
  1160.  
  1161.    One possible implementation could be:
  1162.  
  1163.     #define CMSG_NXTHDR(mhdr, cmsg) \
  1164.         ( ((cmsg) == NULL) ? CMSG_FIRSTHDR(mhdr) : \
  1165.           (((u_char *)(cmsg) + ALIGN((cmsg)->cmsg_len) \
  1166.                              + ALIGN(sizeof(struct cmsghdr)) > \
  1167.             (u_char *)((mhdr)->msg_control) + (mhdr)->msg_controllen) ? \
  1168.            (struct cmsghdr *)NULL : \
  1169.            (struct cmsghdr *)((u_char *)(cmsg) + ALIGN((cmsg)->cmsg_len))) )
  1170.  
  1171.    The macro ALIGN(), which is implementation dependent, rounds its
  1172.    argument up to the next even multiple of whatever alignment is
  1173.    required (probably a multiple of 4 or 8 bytes).
  1174.  
  1175.  
  1176.  
  1177.  
  1178. Stevens & Thomas             Informational                     [Page 21]
  1179.  
  1180. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1181.  
  1182.  
  1183. 4.3.3.  CMSG_DATA
  1184.  
  1185.        unsigned char *CMSG_DATA(const struct cmsghdr *cmsg);
  1186.  
  1187.    CMSG_DATA() returns a pointer to the data (what is called the
  1188.    cmsg_data[] member, even though such a member is not defined in the
  1189.    structure) following a cmsghdr structure.
  1190.  
  1191.    One possible implementation could be:
  1192.  
  1193.        #define CMSG_DATA(cmsg) ( (u_char *)(cmsg) + \
  1194.                                  ALIGN(sizeof(struct cmsghdr)) )
  1195.  
  1196. 4.3.4.  CMSG_SPACE
  1197.  
  1198.        unsigned int CMSG_SPACE(unsigned int length);
  1199.  
  1200.    This macro is new with this API.  Given the length of an ancillary
  1201.    data object, CMSG_SPACE() returns the space required by the object
  1202.    and its cmsghdr structure, including any padding needed to satisfy
  1203.    alignment requirements.  This macro can be used, for example, to
  1204.    allocate space dynamically for the ancillary data.  This macro should
  1205.    not be used to initialize the cmsg_len member of a cmsghdr structure;
  1206.    instead use the CMSG_LEN() macro.
  1207.  
  1208.    One possible implementation could be:
  1209.  
  1210.        #define CMSG_SPACE(length) ( ALIGN(sizeof(struct cmsghdr)) + \
  1211.                                     ALIGN(length) )
  1212.  
  1213. 4.3.5.  CMSG_LEN
  1214.  
  1215.        unsigned int CMSG_LEN(unsigned int length);
  1216.  
  1217.    This macro is new with this API.  Given the length of an ancillary
  1218.    data object, CMSG_LEN() returns the value to store in the cmsg_len
  1219.    member of the cmsghdr structure, taking into account any padding
  1220.    needed to satisfy alignment requirements.
  1221.  
  1222.    One possible implementation could be:
  1223.  
  1224.        #define CMSG_LEN(length) ( ALIGN(sizeof(struct cmsghdr)) + length
  1225.        )
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234. Stevens & Thomas             Informational                     [Page 22]
  1235.  
  1236. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1237.  
  1238.  
  1239.    Note the difference between CMSG_SPACE() and CMSG_LEN(), shown also
  1240.    in the figure in Section 4.2: the former accounts for any required
  1241.    padding at the end of the ancillary data object and the latter is the
  1242.    actual length to store in the cmsg_len member of the ancillary data
  1243.    object.
  1244.  
  1245. 4.4.  Summary of Options Described Using Ancillary Data
  1246.  
  1247.    There are six types of optional information described in this
  1248.    document that are passed between the application and the kernel using
  1249.    ancillary data:
  1250.  
  1251.        1.  the send/receive interface and source/destination address,
  1252.        2.  the hop limit,
  1253.        3.  next hop address,
  1254.        4.  Hop-by-Hop options,
  1255.        5.  Destination options, and
  1256.        6.  Routing header.
  1257.  
  1258.    First, to receive any of this optional information (other than the
  1259.    next hop address, which can only be set), the application must call
  1260.    setsockopt() to turn on the corresponding flag:
  1261.  
  1262.        int  on = 1;
  1263.  
  1264.        setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO,  &on, sizeof(on));
  1265.        setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, sizeof(on));
  1266.        setsockopt(fd, IPPROTO_IPV6, IPV6_HOPOPTS,  &on, sizeof(on));
  1267.        setsockopt(fd, IPPROTO_IPV6, IPV6_DSTOPTS,  &on, sizeof(on));
  1268.        setsockopt(fd, IPPROTO_IPV6, IPV6_RTHDR,    &on, sizeof(on));
  1269.  
  1270.    When any of these options are enabled, the corresponding data is
  1271.    returned as control information by recvmsg(), as one or more
  1272.    ancillary data objects.
  1273.  
  1274.    Nothing special need be done to send any of this optional
  1275.    information; the application just calls sendmsg() and specifies one
  1276.    or more ancillary data objects as control information.
  1277.  
  1278.    We also summarize the three cmsghdr fields that describe the
  1279.    ancillary data objects:
  1280.  
  1281.        cmsg_level    cmsg_type      cmsg_data[]               #times
  1282.        ------------  ------------   ------------------------  ------
  1283.        IPPROTO_IPV6  IPV6_PKTINFO   in6_pktinfo structure     once
  1284.        IPPROTO_IPV6  IPV6_HOPLIMIT  int                       once
  1285.        IPPROTO_IPV6  IPV6_NEXTHOP   socket address structure  once
  1286.        IPPROTO_IPV6  IPV6_HOPOPTS   implementation dependent  mult.
  1287.  
  1288.  
  1289.  
  1290. Stevens & Thomas             Informational                     [Page 23]
  1291.  
  1292. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1293.  
  1294.  
  1295.        IPPROTO_IPV6  IPV6_DSTOPTS   implementation dependent  mult.
  1296.        IPPROTO_IPV6  IPV6_RTHDR     implementation dependent  once
  1297.  
  1298.    The final column indicates how many times an ancillary data object of
  1299.    that type can appear as control information.  The Hop-by-Hop and
  1300.    Destination options can appear multiple times, while all the others
  1301.    can appear only one time.
  1302.  
  1303.    All these options are described in detail in following sections.  All
  1304.    the constants beginning with IPV6_ are defined as a result of
  1305.    including the <netinet/in.h> header.
  1306.  
  1307.    (Note: We intentionally use the same constant for the cmsg_level
  1308.    member as is used as the second argument to getsockopt() and
  1309.    setsockopt() (what is called the "level"), and the same constant for
  1310.    the cmsg_type member as is used as the third argument to getsockopt()
  1311.    and setsockopt() (what is called the "option name").  This is
  1312.    consistent with the existing use of ancillary data in 4.4BSD:
  1313.    returning the destination address of an IPv4 datagram.)
  1314.  
  1315.    (Note: It is up to the implementation what it passes as ancillary
  1316.    data for the Hop-by-Hop option, Destination option, and Routing
  1317.    header option, since the API to these features is through a set of
  1318.    inet6_option_XXX() and inet6_rthdr_XXX() functions that we define
  1319.    later.  These functions serve two purposes: to simplify the interface
  1320.    to these features (instead of requiring the application to know the
  1321.    intimate details of the extension header formats), and to hide the
  1322.    actual implementation from the application.  Nevertheless, we show
  1323.    some examples of these features that store the actual extension
  1324.    header as the ancillary data.  Implementations need not use this
  1325.    technique.)
  1326.  
  1327. 4.5.  IPV6_PKTOPTIONS Socket Option
  1328.  
  1329.    The summary in the previous section assumes a UDP socket.  Sending
  1330.    and receiving ancillary data is easy with UDP: the application calls
  1331.    sendmsg() and recvmsg() instead of sendto() and recvfrom().
  1332.  
  1333.    But there might be cases where a TCP application wants to send or
  1334.    receive this optional information.  For example, a TCP client might
  1335.    want to specify a Routing header and this needs to be done before
  1336.    calling connect().  Similarly a TCP server might want to know the
  1337.    received interface after accept() returns along with any Destination
  1338.    options.
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346. Stevens & Thomas             Informational                     [Page 24]
  1347.  
  1348. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1349.  
  1350.  
  1351.    A new socket option is defined that provides access to the optional
  1352.    information described in the previous section, but without using
  1353.    recvmsg() and sendmsg().  Setting the socket option specifies any of
  1354.    the optional output fields:
  1355.  
  1356.        setsockopt(fd, IPPROTO_IPV6, IPV6_PKTOPTIONS, &buf, len);
  1357.  
  1358.    The fourth argument points to a buffer containing one or more
  1359.    ancillary data objects, and the fifth argument is the total length of
  1360.    all these objects.  The application fills in this buffer exactly as
  1361.    if the buffer were being passed to sendmsg() as control information.
  1362.  
  1363.    The options set by calling setsockopt() for IPV6_PKTOPTIONS are
  1364.    called "sticky" options because once set they apply to all packets
  1365.    sent on that socket.  The application can call setsockopt() again to
  1366.    change all the sticky options, or it can call setsockopt() with a
  1367.    length of 0 to remove all the sticky options for the socket.
  1368.  
  1369.    The corresponding receive option
  1370.  
  1371.        getsockopt(fd, IPPROTO_IPV6, IPV6_PKTOPTIONS, &buf, &len);
  1372.  
  1373.    returns a buffer with one or more ancillary data objects for all the
  1374.    optional receive information that the application has previously
  1375.    specified that it wants to receive.  The fourth argument points to
  1376.    the buffer that is filled in by the call.  The fifth argument is a
  1377.    pointer to a value-result integer: when the function is called the
  1378.    integer specifies the size of the buffer pointed to by the fourth
  1379.    argument, and on return this integer contains the actual number of
  1380.    bytes that were returned.  The application processes this buffer
  1381.    exactly as if the buffer were returned by recvmsg() as control
  1382.    information.
  1383.  
  1384.    To simplify this document, in the remaining sections when we say "can
  1385.    be specified as ancillary data to sendmsg()" we mean "can be
  1386.    specified as ancillary data to sendmsg() or specified as a sticky
  1387.    option using setsockopt() and the IPV6_PKTOPTIONS socket option".
  1388.    Similarly when we say "can be returned as ancillary data by
  1389.    recvmsg()" we mean "can be returned as ancillary data by recvmsg() or
  1390.    returned by getsockopt() with the IPV6_PKTOPTIONS socket option".
  1391.  
  1392. 4.5.1.  TCP Sticky Options
  1393.  
  1394.    When using getsockopt() with the IPV6_PKTOPTIONS option and a TCP
  1395.    socket, only the options from the most recently received segment are
  1396.    retained and returned to the caller, and only after the socket option
  1397.    has been set.  That is, TCP need not start saving a copy of the
  1398.    options until the application says to do so.
  1399.  
  1400.  
  1401.  
  1402. Stevens & Thomas             Informational                     [Page 25]
  1403.  
  1404. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1405.  
  1406.  
  1407.    The application is not allowed to specify ancillary data in a call to
  1408.    sendmsg() on a TCP socket, and none of the ancillary data that we
  1409.    describe in this document is ever returned as control information by
  1410.    recvmsg() on a TCP socket.
  1411.  
  1412. 4.5.2.  UDP and Raw Socket Sticky Options
  1413.  
  1414.    The IPV6_PKTOPTIONS socket option can also be used with a UDP socket
  1415.    or with a raw IPv6 socket, normally to set some of the options once,
  1416.    instead of with each call to sendmsg().
  1417.  
  1418.    Unlike the TCP case, the sticky options can be overridden on a per-
  1419.    packet basis with ancillary data specified in a call to sendmsg() on
  1420.    a UDP or raw IPv6 socket.  If any ancillary data is specified in a
  1421.    call to sendmsg(), none of the sticky options are sent with that
  1422.    datagram.
  1423.  
  1424. 5.  Packet Information
  1425.  
  1426.    There are four pieces of information that an application can specify
  1427.    for an outgoing packet using ancillary data:
  1428.  
  1429.        1.  the source IPv6 address,
  1430.        2.  the outgoing interface index,
  1431.        3.  the outgoing hop limit, and
  1432.        4.  the next hop address.
  1433.  
  1434.    Three similar pieces of information can be returned for a received
  1435.    packet as ancillary data:
  1436.  
  1437.        1.  the destination IPv6 address,
  1438.        2.  the arriving interface index, and
  1439.        3.  the arriving hop limit.
  1440.  
  1441.    The first two pieces of information are contained in an in6_pktinfo
  1442.    structure that is sent as ancillary data with sendmsg() and received
  1443.    as ancillary data with recvmsg().  This structure is defined as a
  1444.    result of including the <netinet/in.h> header.
  1445.  
  1446.        struct in6_pktinfo {
  1447.          struct in6_addr ipi6_addr;    /* src/dst IPv6 address */
  1448.          unsigned int    ipi6_ifindex; /* send/recv interface index */
  1449.        };
  1450.  
  1451.    In the cmsghdr structure containing this ancillary data, the
  1452.    cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will be
  1453.    IPV6_PKTINFO, and the first byte of cmsg_data[] will be the first
  1454.    byte of the in6_pktinfo structure.
  1455.  
  1456.  
  1457.  
  1458. Stevens & Thomas             Informational                     [Page 26]
  1459.  
  1460. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1461.  
  1462.  
  1463.    This information is returned as ancillary data by recvmsg() only if
  1464.    the application has enabled the IPV6_PKTINFO socket option:
  1465.  
  1466.        int  on = 1;
  1467.        setsockopt(fd, IPPROTO_IPV6, IPV6_PKTINFO, &on, sizeof(on));
  1468.  
  1469.    Nothing special need be done to send this information: just specify
  1470.    the control information as ancillary data for sendmsg().
  1471.  
  1472.    (Note: The hop limit is not contained in the in6_pktinfo structure
  1473.    for the following reason.  Some UDP servers want to respond to client
  1474.    requests by sending their reply out the same interface on which the
  1475.    request was received and with the source IPv6 address of the reply
  1476.    equal to the destination IPv6 address of the request.  To do this the
  1477.    application can enable just the IPV6_PKTINFO socket option and then
  1478.    use the received control information from recvmsg() as the outgoing
  1479.    control information for sendmsg().  The application need not examine
  1480.    or modify the in6_pktinfo structure at all.  But if the hop limit
  1481.    were contained in this structure, the application would have to parse
  1482.    the received control information and change the hop limit member,
  1483.    since the received hop limit is not the desired value for an outgoing
  1484.    packet.)
  1485.  
  1486. 5.1.  Specifying/Receiving the Interface
  1487.  
  1488.    Interfaces on an IPv6 node are identified by a small positive
  1489.    integer, as described in Section 4 of [RFC-2133].  That document also
  1490.    describes a function to map an interface name to its interface index,
  1491.    a function to map an interface index to its interface name, and a
  1492.    function to return all the interface names and indexes.  Notice from
  1493.    this document that no interface is ever assigned an index of 0.
  1494.  
  1495.    When specifying the outgoing interface, if the ipi6_ifindex value is
  1496.    0, the kernel will choose the outgoing interface.  If the application
  1497.    specifies an outgoing interface for a multicast packet, the interface
  1498.    specified by the ancillary data overrides any interface specified by
  1499.    the IPV6_MULTICAST_IF socket option (described in [RFC-2133]), for
  1500.    that call to sendmsg() only.
  1501.  
  1502.    When the IPV6_PKTINFO socket option is enabled, the received
  1503.    interface index is always returned as the ipi6_ifindex member of the
  1504.    in6_pktinfo structure.
  1505.  
  1506. 5.2.  Specifying/Receiving Source/Destination Address
  1507.  
  1508.    The source IPv6 address can be specified by calling bind() before
  1509.    each output operation, but supplying the source address together with
  1510.    the data requires less overhead (i.e., fewer system calls) and
  1511.  
  1512.  
  1513.  
  1514. Stevens & Thomas             Informational                     [Page 27]
  1515.  
  1516. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1517.  
  1518.  
  1519.    requires less state to be stored and protected in a multithreaded
  1520.    application.
  1521.  
  1522.    When specifying the source IPv6 address as ancillary data, if the
  1523.    ipi6_addr member of the in6_pktinfo structure is the unspecified
  1524.    address (IN6ADDR_ANY_INIT), then (a) if an address is currently bound
  1525.    to the socket, it is used as the source address, or (b) if no address
  1526.    is currently bound to the socket, the kernel will choose the source
  1527.    address.  If the ipi6_addr member is not the unspecified address, but
  1528.    the socket has already bound a source address, then the ipi6_addr
  1529.    value overrides the already-bound source address for this output
  1530.    operation only.
  1531.  
  1532.    The kernel must verify that the requested source address is indeed a
  1533.    unicast address assigned to the node.
  1534.  
  1535.    When the in6_pktinfo structure is returned as ancillary data by
  1536.    recvmsg(), the ipi6_addr member contains the destination IPv6 address
  1537.    from the received packet.
  1538.  
  1539. 5.3.  Specifying/Receiving the Hop Limit
  1540.  
  1541.    The outgoing hop limit is normally specified with either the
  1542.    IPV6_UNICAST_HOPS socket option or the IPV6_MULTICAST_HOPS socket
  1543.    option, both of which are described in [RFC-2133].  Specifying the
  1544.    hop limit as ancillary data lets the application override either the
  1545.    kernel's default or a previously specified value, for either a
  1546.    unicast destination or a multicast destination, for a single output
  1547.    operation.  Returning the received hop limit is useful for programs
  1548.    such as Traceroute and for IPv6 applications that need to verify that
  1549.    the received hop limit is 255 (e.g., that the packet has not been
  1550.    forwarded).
  1551.  
  1552.    The received hop limit is returned as ancillary data by recvmsg()
  1553.    only if the application has enabled the IPV6_HOPLIMIT socket option:
  1554.  
  1555.        int  on = 1;
  1556.        setsockopt(fd, IPPROTO_IPV6, IPV6_HOPLIMIT, &on, sizeof(on));
  1557.  
  1558.    In the cmsghdr structure containing this ancillary data, the
  1559.    cmsg_level member will be IPPROTO_IPV6, the cmsg_type member will be
  1560.    IPV6_HOPLIMIT, and the first byte of cmsg_data[] will be the first
  1561.    byte of the integer hop limit.
  1562.  
  1563.    Nothing special need be done to specify the outgoing hop limit: just
  1564.    specify the control information as ancillary data for sendmsg().  As
  1565.    specified in [RFC-2133], the interpretation of the integer hop limit
  1566.    value is
  1567.  
  1568.  
  1569.  
  1570. Stevens & Thomas             Informational                     [Page 28]
  1571.  
  1572. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1573.  
  1574.  
  1575.        x < -1:        return an error of EINVAL
  1576.        x == -1:       use kernel default
  1577.        0 <= x <= 255: use x
  1578.        x >= 256:      return an error of EINVAL
  1579.  
  1580. 5.4.  Specifying the Next Hop Address
  1581.  
  1582.    The IPV6_NEXTHOP ancillary data object specifies the next hop for the
  1583.    datagram as a socket address structure.  In the cmsghdr structure
  1584.    containing this ancillary data, the cmsg_level member will be
  1585.    IPPROTO_IPV6, the cmsg_type member will be IPV6_NEXTHOP, and the
  1586.    first byte of cmsg_data[] will be the first byte of the socket
  1587.    address structure.
  1588.  
  1589.    This is a privileged option.  (Note: It is implementation defined and
  1590.    beyond the scope of this document to define what "privileged" means.
  1591.    Unix systems use this term to mean the process must have an effective
  1592.    user ID of 0.)
  1593.  
  1594.    If the socket address structure contains an IPv6 address (e.g., the
  1595.    sin6_family member is AF_INET6), then the node identified by that
  1596.    address must be a neighbor of the sending host.  If that address
  1597.    equals the destination IPv6 address of the datagram, then this is
  1598.    equivalent to the existing SO_DONTROUTE socket option.
  1599.  
  1600. 5.5.  Additional Errors with sendmsg()
  1601.  
  1602.    With the IPV6_PKTINFO socket option there are no additional errors
  1603.    possible with the call to recvmsg().  But when specifying the
  1604.    outgoing interface or the source address, additional errors are
  1605.    possible from sendmsg().  The following are examples, but some of
  1606.    these may not be provided by some implementations, and some
  1607.    implementations may define additional errors:
  1608.  
  1609.    ENXIO         The interface specified by ipi6_ifindex does not exist.
  1610.  
  1611.    ENETDOWN      The interface specified by ipi6_ifindex is not enabled
  1612.                  for IPv6 use.
  1613.  
  1614.    EADDRNOTAVAIL ipi6_ifindex specifies an interface but the address
  1615.                  ipi6_addr is not available for use on that interface.
  1616.  
  1617.    EHOSTUNREACH  No route to the destination exists over the interface
  1618.                  specified by ifi6_ifindex.
  1619.  
  1620.  
  1621.  
  1622.  
  1623.  
  1624.  
  1625.  
  1626. Stevens & Thomas             Informational                     [Page 29]
  1627.  
  1628. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1629.  
  1630.  
  1631. 6.  Hop-By-Hop Options
  1632.  
  1633.    A variable number of Hop-by-Hop options can appear in a single Hop-
  1634.    by-Hop options header.  Each option in the header is TLV-encoded with
  1635.    a type, length, and value.
  1636.  
  1637.    Today only three Hop-by-Hop options are defined for IPv6 [RFC-1883]:
  1638.    Jumbo Payload, Pad1, and PadN, although a proposal exists for a
  1639.    router-alert Hop-by-Hop option.  The Jumbo Payload option should not
  1640.    be passed back to an application and an application should receive an
  1641.    error if it attempts to set it.  This option is processed entirely by
  1642.    the kernel.  It is indirectly specified by datagram-based
  1643.    applications as the size of the datagram to send and indirectly
  1644.    passed back to these applications as the length of the received
  1645.    datagram.  The two pad options are for alignment purposes and are
  1646.    automatically inserted by a sending kernel when needed and ignored by
  1647.  
  1648.    the receiving kernel.  This section of the API is therefore defined
  1649.    for future Hop-by-Hop options that an application may need to specify
  1650.    and receive.
  1651.  
  1652.    Individual Hop-by-Hop options (and Destination options, which are
  1653.    described shortly, and which are similar to the Hop-by-Hop options)
  1654.    may have specific alignment requirements.  For example, the 4-byte
  1655.    Jumbo Payload length should appear on a 4-byte boundary, and IPv6
  1656.    addresses are normally aligned on an 8-byte boundary.  These
  1657.    requirements and the terminology used with these options are
  1658.    discussed in Section 4.2 and Appendix A of [RFC-1883].  The alignment
  1659.    of each option is specified by two values, called x and y, written as
  1660.    "xn + y".  This states that the option must appear at an integer
  1661.    multiple of x bytes from the beginning of the options header (x can
  1662.    have the values 1, 2, 4, or 8), plus y bytes (y can have a value
  1663.    between 0 and 7, inclusive).  The Pad1 and PadN options are inserted
  1664.    as needed to maintain the required alignment.  Whatever code builds
  1665.    either a Hop-by-Hop options header or a Destination options header
  1666.    must know the values of x and y for each option.
  1667.  
  1668.    Multiple Hop-by-Hop options can be specified by the application.
  1669.    Normally one ancillary data object describes all the Hop-by-Hop
  1670.    options (since each option is itself TLV-encoded) but the application
  1671.    can specify multiple ancillary data objects for the Hop-by-Hop
  1672.    options, each object specifying one or more options.  Care must be
  1673.    taken designing the API for these options since
  1674.  
  1675.    1.   it may be possible for some future Hop-by-Hop options to be
  1676.         generated by the application and processed entirely by the
  1677.         application (e.g., the kernel may not know the alignment
  1678.         restrictions for the option),
  1679.  
  1680.  
  1681.  
  1682. Stevens & Thomas             Informational                     [Page 30]
  1683.  
  1684. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1685.  
  1686.  
  1687.    2.   it must be possible for the kernel to insert its own Hop-by-Hop
  1688.         options in an outgoing packet (e.g., the Jumbo Payload option),
  1689.  
  1690.    3.   the application can place one or more Hop-by-Hop options into a
  1691.         single ancillary data object,
  1692.  
  1693.    4.   if the application specifies multiple ancillary data objects,
  1694.         each containing one or more Hop-by-Hop options, the kernel must
  1695.         combine these a single Hop-by-Hop options header, and
  1696.  
  1697.    5.   it must be possible for the kernel to remove some Hop-by-Hop
  1698.         options from a received packet before returning the remaining
  1699.         Hop-by-Hop options to the application.  (This removal might
  1700.         consist of the kernel converting the option into a pad option of
  1701.         the same length.)
  1702.  
  1703.    Finally, we note that access to some Hop-by-Hop options or to some
  1704.    Destination options, might require special privilege.  That is,
  1705.    normal applications (without special privilege) might be forbidden
  1706.    from setting certain options in outgoing packets, and might never see
  1707.    certain options in received packets.
  1708.  
  1709. 6.1.  Receiving Hop-by-Hop Options
  1710.  
  1711.    To receive Hop-by-Hop options the application must enable the
  1712.    IPV6_HOPOPTS socket option:
  1713.  
  1714.        int  on = 1;
  1715.        setsockopt(fd, IPPROTO_IPV6, IPV6_HOPOPTS, &on, sizeof(on));
  1716.  
  1717.    All the Hop-by-Hop options are returned as one ancillary data object
  1718.    described by a cmsghdr structure.  The cmsg_level member will be
  1719.    IPPROTO_IPV6 and the cmsg_type member will be IPV6_HOPOPTS.  These
  1720.    options are then processed by calling the inet6_option_next() and
  1721.    inet6_option_find() functions, described shortly.
  1722.  
  1723. 6.2.  Sending Hop-by-Hop Options
  1724.  
  1725.    To send one or more Hop-by-Hop options, the application just
  1726.    specifies them as ancillary data in a call to sendmsg().  No socket
  1727.    option need be set.
  1728.  
  1729.    Normally all the Hop-by-Hop options are specified by a single
  1730.    ancillary data object.  Multiple ancillary data objects, each
  1731.    containing one or more Hop-by-Hop options, can also be specified, in
  1732.    which case the kernel will combine all the Hop-by-Hop options into a
  1733.    single Hop-by-Hop extension header.  But it should be more efficient
  1734.    to use a single ancillary data object to describe all the Hop-by-Hop
  1735.  
  1736.  
  1737.  
  1738. Stevens & Thomas             Informational                     [Page 31]
  1739.  
  1740. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1741.  
  1742.  
  1743.    options.  The cmsg_level member is set to IPPROTO_IPV6 and the
  1744.    cmsg_type member is set to IPV6_HOPOPTS.  The option is normally
  1745.    constructed using the inet6_option_init(), inet6_option_append(), and
  1746.    inet6_option_alloc() functions, described shortly.
  1747.  
  1748.    Additional errors may be possible from sendmsg() if the specified
  1749.    option is in error.
  1750.  
  1751. 6.3.  Hop-by-Hop and Destination Options Processing
  1752.  
  1753.    Building and parsing the Hop-by-Hop and Destination options is
  1754.    complicated for the reasons given earlier.  We therefore define a set
  1755.    of functions to help the application.  The function prototypes for
  1756.    these functions are all in the <netinet/in.h> header.
  1757.  
  1758. 6.3.1.  inet6_option_space
  1759.  
  1760.        int inet6_option_space(int nbytes);
  1761.  
  1762.    This function returns the number of bytes required to hold an option
  1763.    when it is stored as ancillary data, including the cmsghdr structure
  1764.    at the beginning, and any padding at the end (to make its size a
  1765.    multiple of 8 bytes).  The argument is the size of the structure
  1766.    defining the option, which must include any pad bytes at the
  1767.    beginning (the value y in the alignment term "xn + y"), the type
  1768.    byte, the length byte, and the option data.
  1769.  
  1770.    (Note: If multiple options are stored in a single ancillary data
  1771.    object, which is the recommended technique, this function
  1772.    overestimates the amount of space required by the size of N-1 cmsghdr
  1773.    structures, where N is the number of options to be stored in the
  1774.    object.  This is of little consequence, since it is assumed that most
  1775.    Hop-by-Hop option headers and Destination option headers carry only
  1776.    one option (p. 33 of [RFC-1883]).)
  1777.  
  1778. 6.3.2.  inet6_option_init
  1779.  
  1780.        int inet6_option_init(void *bp, struct cmsghdr **cmsgp, int
  1781.        type);
  1782.  
  1783.    This function is called once per ancillary data object that will
  1784.    contain either Hop-by-Hop or Destination options.  It returns 0 on
  1785.    success or -1 on an error.
  1786.  
  1787.    bp is a pointer to previously allocated space that will contain the
  1788.    ancillary data object.  It must be large enough to contain all the
  1789.    individual options to be added by later calls to
  1790.    inet6_option_append() and inet6_option_alloc().
  1791.  
  1792.  
  1793.  
  1794. Stevens & Thomas             Informational                     [Page 32]
  1795.  
  1796. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1797.  
  1798.  
  1799.    cmsgp is a pointer to a pointer to a cmsghdr structure.  *cmsgp is
  1800.    initialized by this function to point to the cmsghdr structure
  1801.    constructed by this function in the buffer pointed to by bp.
  1802.  
  1803.    type is either IPV6_HOPOPTS or IPV6_DSTOPTS.  This type is stored in
  1804.    the cmsg_type member of the cmsghdr structure pointed to by *cmsgp.
  1805.  
  1806. 6.3.3.  inet6_option_append
  1807.  
  1808.  
  1809.        int inet6_option_append(struct cmsghdr *cmsg, const uint8_t *typep,
  1810.                                int multx, int plusy);
  1811.  
  1812.    This function appends a Hop-by-Hop option or a Destination option
  1813.    into an ancillary data object that has been initialized by
  1814.    inet6_option_init().  This function returns 0 if it succeeds or -1 on
  1815.    an error.
  1816.  
  1817.    cmsg is a pointer to the cmsghdr structure that must have been
  1818.    initialized by inet6_option_init().
  1819.  
  1820.    typep is a pointer to the 8-bit option type.  It is assumed that this
  1821.    field is immediately followed by the 8-bit option data length field,
  1822.    which is then followed immediately by the option data.  The caller
  1823.    initializes these three fields (the type-length-value, or TLV) before
  1824.    calling this function.
  1825.  
  1826.    The option type must have a value from 2 to 255, inclusive.  (0 and 1
  1827.    are reserved for the Pad1 and PadN options, respectively.)
  1828.  
  1829.    The option data length must have a value between 0 and 255,
  1830.    inclusive, and is the length of the option data that follows.
  1831.  
  1832.    multx is the value x in the alignment term "xn + y" described
  1833.    earlier.  It must have a value of 1, 2, 4, or 8.
  1834.  
  1835.    plusy is the value y in the alignment term "xn + y" described
  1836.    earlier.  It must have a value between 0 and 7, inclusive.
  1837.  
  1838. 6.3.4.  inet6_option_alloc
  1839.  
  1840.        uint8_t *inet6_option_alloc(struct cmsghdr *cmsg, int datalen,
  1841.                                     int multx, int plusy);
  1842.  
  1843.  
  1844.  
  1845.  
  1846.  
  1847.  
  1848.  
  1849.  
  1850. Stevens & Thomas             Informational                     [Page 33]
  1851.  
  1852. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1853.  
  1854.  
  1855.    This function appends a Hop-by-Hop option or a Destination option
  1856.    into an ancillary data object that has been initialized by
  1857.    inet6_option_init().  This function returns a pointer to the 8-bit
  1858.    option type field that starts the option on success, or NULL on an
  1859.    error.
  1860.  
  1861.    The difference between this function and inet6_option_append() is
  1862.    that the latter copies the contents of a previously built option into
  1863.    the ancillary data object while the current function returns a
  1864.    pointer to the space in the data object where the option's TLV must
  1865.    then be built by the caller.
  1866.  
  1867.    cmsg is a pointer to the cmsghdr structure that must have been
  1868.    initialized by inet6_option_init().
  1869.  
  1870.    datalen is the value of the option data length byte for this option.
  1871.    This value is required as an argument to allow the function to
  1872.    determine if padding must be appended at the end of the option.  (The
  1873.    inet6_option_append() function does not need a data length argument
  1874.    since the option data length must already be stored by the caller.)
  1875.  
  1876.    multx is the value x in the alignment term "xn + y" described
  1877.    earlier.  It must have a value of 1, 2, 4, or 8.
  1878.  
  1879.    plusy is the value y in the alignment term "xn + y" described
  1880.    earlier.  It must have a value between 0 and 7, inclusive.
  1881.  
  1882. 6.3.5.  inet6_option_next
  1883.  
  1884.        int inet6_option_next(const struct cmsghdr *cmsg, uint8_t
  1885.        **tptrp);
  1886.  
  1887.    This function processes the next Hop-by-Hop option or Destination
  1888.    option in an ancillary data object.  If another option remains to be
  1889.    processed, the return value of the function is 0 and *tptrp points to
  1890.    the 8-bit option type field (which is followed by the 8-bit option
  1891.    data length, followed by the option data).  If no more options remain
  1892.    to be processed, the return value is -1 and *tptrp is NULL.  If an
  1893.    error occurs, the return value is -1 and *tptrp is not NULL.
  1894.  
  1895.    cmsg is a pointer to cmsghdr structure of which cmsg_level equals
  1896.    IPPROTO_IPV6 and cmsg_type equals either IPV6_HOPOPTS or
  1897.    IPV6_DSTOPTS.
  1898.  
  1899.    tptrp is a pointer to a pointer to an 8-bit byte and *tptrp is used
  1900.    by the function to remember its place in the ancillary data object
  1901.    each time the function is called.  The first time this function is
  1902.    called for a given ancillary data object, *tptrp must be set to NULL.
  1903.  
  1904.  
  1905.  
  1906. Stevens & Thomas             Informational                     [Page 34]
  1907.  
  1908. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1909.  
  1910.  
  1911.    Each time this function returns success, *tptrp points to the 8-bit
  1912.    option type field for the next option to be processed.
  1913.  
  1914. 6.3.6.  inet6_option_find
  1915.  
  1916.        int inet6_option_find(const struct cmsghdr *cmsg, uint8_t *tptrp,
  1917.                              int type);
  1918.  
  1919.    This function is similar to the previously described
  1920.    inet6_option_next() function, except this function lets the caller
  1921.    specify the option type to be searched for, instead of always
  1922.    returning the next option in the ancillary data object.  cmsg is a
  1923.    pointer to cmsghdr structure of which cmsg_level equals IPPROTO_IPV6
  1924.    and cmsg_type equals either IPV6_HOPOPTS or IPV6_DSTOPTS.
  1925.  
  1926.    tptrp is a pointer to a pointer to an 8-bit byte and *tptrp is used
  1927.    by the function to remember its place in the ancillary data object
  1928.    each time the function is called.  The first time this function is
  1929.    called for a given ancillary data object, *tptrp must be set to NULL.
  1930.  
  1931.    This function starts searching for an option of the specified type
  1932.    beginning after the value of *tptrp.  If an option of the specified
  1933.    type is located, this function returns 0 and *tptrp points to the 8-
  1934.    bit option type field for the option of the specified type.  If an
  1935.    option of the specified type is not located, the return value is -1
  1936.    and *tptrp is NULL.  If an error occurs, the return value is -1 and
  1937.    *tptrp is not NULL.
  1938.  
  1939. 6.3.7.  Options Examples
  1940.  
  1941.    We now provide an example that builds two Hop-by-Hop options.  First
  1942.    we define two options, called X and Y, taken from the example in
  1943.    Appendix A of [RFC-1883].  We assume that all options will have
  1944.    structure definitions similar to what is shown below.
  1945.  
  1946.         /* option X and option Y are defined in [RFC-1883], pp. 33-34 */
  1947. #define IP6_X_OPT_TYPE       X   /* replace X with assigned value */
  1948. #define IP6_X_OPT_LEN       12
  1949. #define IP6_X_OPT_MULTX      8   /* 8n + 2 alignment */
  1950. #define IP6_X_OPT_OFFSETY    2
  1951.  
  1952. struct ip6_X_opt {
  1953.   uint8_t   ip6_X_opt_pad[IP6_X_OPT_OFFSETY];
  1954.   uint8_t   ip6_X_opt_type;
  1955.   uint8_t   ip6_X_opt_len;
  1956.   uint32_t  ip6_X_opt_val1;
  1957.   uint64_t  ip6_X_opt_val2;
  1958. };
  1959.  
  1960.  
  1961.  
  1962. Stevens & Thomas             Informational                     [Page 35]
  1963.  
  1964. RFC 2292             Advanced Sockets API for IPv6         February 1998
  1965.  
  1966.  
  1967. #define IP6_Y_OPT_TYPE       Y   /* replace Y with assigned value */
  1968. #define IP6_Y_OPT_LEN        7
  1969. #define IP6_Y_OPT_MULTX      4   /* 4n + 3 alignment */
  1970. #define IP6_Y_OPT_OFFSETY    3
  1971.  
  1972. struct ip6_Y_opt {
  1973.   uint8_t   ip6_Y_opt_pad[IP6_Y_OPT_OFFSETY];
  1974.   uint8_t   ip6_Y_opt_type;
  1975.   uint8_t   ip6_Y_opt_len;
  1976.   uint8_t   ip6_Y_opt_val1;
  1977.   uint16_t  ip6_Y_opt_val2;
  1978.   uint32_t  ip6_Y_opt_val3;
  1979. };
  1980.  
  1981.    We now show the code fragment to build one ancillary data object
  1982.    containing both options.
  1983.  
  1984. struct msghdr  msg;
  1985. struct cmsghdr  *cmsgptr;
  1986. struct ip6_X_opt  optX;
  1987. struct ip6_Y_opt  optY;
  1988.  
  1989. msg.msg_control = malloc(inet6_option_space(sizeof(optX) +
  1990.                                             sizeof(optY)));
  1991.  
  1992. inet6_option_init(msg.msg_control, &cmsgptr, IPV6_HOPOPTS);
  1993.  
  1994. optX.ip6_X_opt_type = IP6_X_OPT_TYPE;
  1995. optX.ip6_X_opt_len  = IP6_X_OPT_LEN;
  1996. optX.ip6_X_opt_val1 = <32-bit value>;
  1997. optX.ip6_X_opt_val2 = <64-bit value>;
  1998. inet6_option_append(cmsgptr, &optX.ip6_X_opt_type,
  1999.                     IP6_X_OPT_MULTX, IP6_X_OPT_OFFSETY);
  2000.  
  2001. optY.ip6_Y_opt_type = IP6_Y_OPT_TYPE;
  2002. optY.ip6_Y_opt_len  = IP6_Y_OPT_LEN;
  2003. optY.ip6_Y_opt_val1 = <8-bit value>;
  2004. optY.ip6_Y_opt_val2 = <16-bit value>;
  2005. optY.ip6_Y_opt_val3 = <32-bit value>;
  2006. inet6_option_append(cmsgptr, &optY.ip6_Y_opt_type,
  2007.                     IP6_Y_OPT_MULTX, IP6_Y_OPT_OFFSETY);
  2008.  
  2009. msg.msg_controllen = cmsgptr->cmsg_len;
  2010.  
  2011.    The call to inet6_option_init() builds the cmsghdr structure in the
  2012.    control buffer.
  2013.  
  2014.  
  2015.  
  2016.  
  2017.  
  2018. Stevens & Thomas             Informational                     [Page 36]
  2019.  
  2020. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2021.  
  2022.  
  2023.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2024.      |       cmsg_len = CMSG_LEN(0) = 12                             |
  2025.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2026.      |       cmsg_level = IPPROTO_IPV6                               |
  2027.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2028.      |       cmsg_type = IPV6_HOPOPTS                                |
  2029.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2030.  
  2031.    Here we assume a 32-bit architecture where sizeof(struct cmsghdr)
  2032.    equals 12, with a desired alignment of 4-byte boundaries (that is,
  2033.    the ALIGN() macro shown in the sample implementations of the
  2034.    CMSG_xxx() macros rounds up to a multiple of 4).
  2035.  
  2036.    The first call to inet6_option_append() appends the X option.  Since
  2037.    this is the first option in the ancillary data object, 2 bytes are
  2038.    allocated for the Next Header byte and for the Hdr Ext Len byte.  The
  2039.    former will be set by the kernel, depending on the type of header
  2040.    that follows this header, and the latter byte is set to 1.  These 2
  2041.    bytes form the 2 bytes of padding (IP6_X_OPT_OFFSETY) required at the
  2042.    beginning of this option.
  2043.  
  2044.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2045.      |       cmsg_len = 28                                           |
  2046.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2047.      |       cmsg_level = IPPROTO_IPV6                               |
  2048.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2049.      |       cmsg_type = IPV6_HOPOPTS                                |
  2050.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2051.      |  Next Header  | Hdr Ext Len=1 | Option Type=X |Opt Data Len=12|
  2052.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2053.      |                         4-octet field                         |
  2054.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2055.      |                                                               |
  2056.      +                         8-octet field                         +
  2057.      |                                                               |
  2058.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2059.  
  2060.    The cmsg_len member of the cmsghdr structure is incremented by 16,
  2061.    the size of the option.
  2062.  
  2063.    The next call to inet6_option_append() appends the Y option to the
  2064.    ancillary data object.
  2065.  
  2066.  
  2067.  
  2068.  
  2069.  
  2070.  
  2071.  
  2072.  
  2073.  
  2074. Stevens & Thomas             Informational                     [Page 37]
  2075.  
  2076. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2077.  
  2078.  
  2079.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2080.      |       cmsg_len = 44                                           |
  2081.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2082.      |       cmsg_level = IPPROTO_IPV6                               |
  2083.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2084.      |       cmsg_type = IPV6_HOPOPTS                                |
  2085.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2086.      |  Next Header  | Hdr Ext Len=3 | Option Type=X |Opt Data Len=12|
  2087.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2088.      |                         4-octet field                         |
  2089.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2090.      |                                                               |
  2091.      +                         8-octet field                         +
  2092.      |                                                               |
  2093.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2094.      | PadN Option=1 |Opt Data Len=1 |       0       | Option Type=Y |
  2095.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2096.      |Opt Data Len=7 | 1-octet field |         2-octet field         |
  2097.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2098.      |                         4-octet field                         |
  2099.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2100.      | PadN Option=1 |Opt Data Len=2 |       0       |       0       |
  2101.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2102.  
  2103.    16 bytes are appended by this function, so cmsg_len becomes 44.  The
  2104.    inet6_option_append() function notices that the appended data
  2105.    requires 4 bytes of padding at the end, to make the size of the
  2106.    ancillary data object a multiple of 8, and appends the PadN option
  2107.    before returning.  The Hdr Ext Len byte is incremented by 2 to become
  2108.    3.
  2109.  
  2110.    Alternately, the application could build two ancillary data objects,
  2111.    one per option, although this will probably be less efficient than
  2112.    combining the two options into a single ancillary data object (as
  2113.    just shown).  The kernel must combine these into a single Hop-by-Hop
  2114.    extension header in the final IPv6 packet.
  2115.  
  2116.        struct msghdr  msg;
  2117.        struct cmsghdr  *cmsgptr;
  2118.        struct ip6_X_opt  optX;
  2119.        struct ip6_Y_opt  optY;
  2120.  
  2121.        msg.msg_control = malloc(inet6_option_space(sizeof(optX)) +
  2122.                                 inet6_option_space(sizeof(optY)));
  2123.  
  2124.        inet6_option_init(msg.msg_control, &cmsgptr, IPPROTO_HOPOPTS);
  2125.  
  2126.        optX.ip6_X_opt_type = IP6_X_OPT_TYPE;
  2127.  
  2128.  
  2129.  
  2130. Stevens & Thomas             Informational                     [Page 38]
  2131.  
  2132. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2133.  
  2134.  
  2135.        optX.ip6_X_opt_len  = IP6_X_OPT_LEN;
  2136.        optX.ip6_X_opt_val1 = <32-bit value>;
  2137.        optX.ip6_X_opt_val2 = <64-bit value>;
  2138.        inet6_option_append(cmsgptr, &optX.ip6_X_opt_type,
  2139.                            IP6_X_OPT_MULTX, IP6_X_OPT_OFFSETY);
  2140.        msg.msg_controllen = CMSG_SPACE(sizeof(optX));
  2141.  
  2142.        inet6_option_init((u_char *)msg.msg_control + msg.msg_controllen,
  2143.                          &cmsgptr, IPPROTO_HOPOPTS);
  2144.  
  2145.        optY.ip6_Y_opt_type = IP6_Y_OPT_TYPE;
  2146.        optY.ip6_Y_opt_len  = IP6_Y_OPT_LEN;
  2147.        optY.ip6_Y_opt_val1 = <8-bit value>;
  2148.        optY.ip6_Y_opt_val2 = <16-bit value>;
  2149.        optY.ip6_Y_opt_val3 = <32-bit value>;
  2150.        inet6_option_append(cmsgptr, &optY.ip6_Y_opt_type,
  2151.                            IP6_Y_OPT_MULTX, IP6_Y_OPT_OFFSETY);
  2152.        msg.msg_controllen += cmsgptr->cmsg_len;
  2153.  
  2154.    Each call to inet6_option_init() builds a new cmsghdr structure, and
  2155.    the final result looks like the following:
  2156.  
  2157.  
  2158.  
  2159.  
  2160.  
  2161.  
  2162.  
  2163.  
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169.  
  2170.  
  2171.  
  2172.  
  2173.  
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.  
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186. Stevens & Thomas             Informational                     [Page 39]
  2187.  
  2188. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2189.  
  2190.  
  2191.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2192.      |       cmsg_len = 28                                           |
  2193.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2194.      |       cmsg_level = IPPROTO_IPV6                               |
  2195.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2196.      |       cmsg_type = IPV6_HOPOPTS                                |
  2197.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2198.      |  Next Header  | Hdr Ext Len=1 | Option Type=X |Opt Data Len=12|
  2199.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2200.      |                         4-octet field                         |
  2201.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2202.      |                                                               |
  2203.      +                         8-octet field                         +
  2204.      |                                                               |
  2205.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2206.      |       cmsg_len = 28                                           |
  2207.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2208.      |       cmsg_level = IPPROTO_IPV6                               |
  2209.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2210.      |       cmsg_type = IPV6_HOPOPTS                                |
  2211.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2212.      |  Next Header  | Hdr Ext Len=1 | Pad1 Option=0 | Option Type=Y |
  2213.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2214.      |Opt Data Len=7 | 1-octet field |         2-octet field         |
  2215.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2216.      |                         4-octet field                         |
  2217.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2218.      | PadN Option=1 |Opt Data Len=2 |       0       |       0       |
  2219.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2220.  
  2221.    When the kernel combines these two options into a single Hop-by-Hop
  2222.    extension header, the first 3 bytes of the second ancillary data
  2223.    object (the Next Header byte, the Hdr Ext Len byte, and the Pad1
  2224.    option) will be combined into a PadN option occupying 3 bytes.
  2225.  
  2226.    The following code fragment is a redo of the first example shown
  2227.    (building two options in a single ancillary data object) but this
  2228.    time we use inet6_option_alloc().
  2229.  
  2230. uint8_t  *typep;
  2231. struct msghdr  msg;
  2232. struct cmsghdr  *cmsgptr;
  2233. struct ip6_X_opt  *optXp;  /* now a pointer, not a struct */
  2234. struct ip6_Y_opt  *optYp;  /* now a pointer, not a struct */
  2235.  
  2236. msg.msg_control = malloc(inet6_option_space(sizeof(*optXp) +
  2237.                                             sizeof(*optYp)));
  2238.  
  2239.  
  2240.  
  2241.  
  2242. Stevens & Thomas             Informational                     [Page 40]
  2243.  
  2244. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2245.  
  2246.  
  2247. inet6_option_init(msg.msg_control, &cmsgptr, IPV6_HOPOPTS);
  2248.  
  2249. typep = inet6_option_alloc(cmsgptr, IP6_X_OPT_LEN,
  2250.                            IP6_X_OPT_MULTX, IP6_X_OPT_OFFSETY);
  2251. optXp = (struct ip6_X_opt *) (typep - IP6_X_OPT_OFFSETY);
  2252. optXp->ip6_X_opt_type = IP6_X_OPT_TYPE;
  2253. optXp->ip6_X_opt_len  = IP6_X_OPT_LEN;
  2254. optXp->ip6_X_opt_val1 = <32-bit value>;
  2255. optXp->ip6_X_opt_val2 = <64-bit value>;
  2256.  
  2257. typep = inet6_option_alloc(cmsgptr, IP6_Y_OPT_LEN,
  2258.                            IP6_Y_OPT_MULTX, IP6_Y_OPT_OFFSETY);
  2259. optYp = (struct ip6_Y_opt *) (typep - IP6_Y_OPT_OFFSETY);
  2260. optYp->ip6_Y_opt_type = IP6_Y_OPT_TYPE;
  2261. optYp->ip6_Y_opt_len  = IP6_Y_OPT_LEN;
  2262. optYp->ip6_Y_opt_val1 = <8-bit value>;
  2263. optYp->ip6_Y_opt_val2 = <16-bit value>;
  2264. optYp->ip6_Y_opt_val3 = <32-bit value>;
  2265.  
  2266. msg.msg_controllen = cmsgptr->cmsg_len;
  2267.  
  2268.    Notice that inet6_option_alloc() returns a pointer to the 8-bit
  2269.    option type field.  If the program wants a pointer to an option
  2270.    structure that includes the padding at the front (as shown in our
  2271.    definitions of the ip6_X_opt and ip6_Y_opt structures), the y-offset
  2272.    at the beginning of the structure must be subtracted from the
  2273.    returned pointer.
  2274.  
  2275.    The following code fragment shows the processing of Hop-by-Hop
  2276.    options using the inet6_option_next() function.
  2277.  
  2278.     struct msghdr   msg;
  2279.     struct cmsghdr  *cmsgptr;
  2280.  
  2281.     /* fill in msg */
  2282.  
  2283.     /* call recvmsg() */
  2284.  
  2285.     for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL;
  2286.          cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
  2287.         if (cmsgptr->cmsg_level == IPPROTO_IPV6 &&
  2288.             cmsgptr->cmsg_type == IPV6_HOPOPTS) {
  2289.  
  2290.             uint8_t  *tptr = NULL;
  2291.  
  2292.             while (inet6_option_next(cmsgptr, &tptr) == 0) {
  2293.                 if (*tptr == IP6_X_OPT_TYPE) {
  2294.                     struct ip6_X_opt  *optXp;
  2295.  
  2296.  
  2297.  
  2298. Stevens & Thomas             Informational                     [Page 41]
  2299.  
  2300. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2301.  
  2302.  
  2303.                     optXp = (struct ip6_X_opt *) (tptr - IP6_X_OPT_OFFSETY);
  2304.                     <do whatever with> optXp->ip6_X_opt_val1;
  2305.                     <do whatever with> optXp->ip6_X_opt_val2;
  2306.  
  2307.                 } else if (*tptr == IP6_Y_OPT_TYPE) {
  2308.                     struct ip6_Y_opt  *optYp;
  2309.  
  2310.                     optYp = (struct ip6_Y_opt *) (tptr - IP6_Y_OPT_OFFSETY);
  2311.                     <do whatever with> optYp->ip6_Y_opt_val1;
  2312.                     <do whatever with> optYp->ip6_Y_opt_val2;
  2313.                     <do whatever with> optYp->ip6_Y_opt_val3;
  2314.                 }
  2315.             }
  2316.             if (tptr != NULL)
  2317.                 <error encountered by inet6_option_next()>;
  2318.         }
  2319.     }
  2320.  
  2321. 7.  Destination Options
  2322.  
  2323.    A variable number of Destination options can appear in one or more
  2324.    Destination option headers.  As defined in [RFC-1883], a Destination
  2325.    options header appearing before a Routing header is processed by the
  2326.    first destination plus any subsequent destinations specified in the
  2327.    Routing header, while a Destination options header appearing after a
  2328.    Routing header is processed only by the final destination.  As with
  2329.    the Hop-by-Hop options, each option in a Destination options header
  2330.    is TLV-encoded with a type, length, and value.
  2331.  
  2332.    Today no Destination options are defined for IPv6 [RFC-1883],
  2333.    although proposals exist to use Destination options with mobility and
  2334.    anycasting.
  2335.  
  2336. 7.1.  Receiving Destination Options
  2337.  
  2338.    To receive Destination options the application must enable the
  2339.    IPV6_DSTOPTS socket option:
  2340.  
  2341.        int  on = 1;
  2342.        setsockopt(fd, IPPROTO_IPV6, IPV6_DSTOPTS, &on, sizeof(on));
  2343.  
  2344.    All the Destination options appearing before a Routing header are
  2345.    returned as one ancillary data object described by a cmsghdr
  2346.    structure and all the Destination options appearing after a Routing
  2347.    header are returned as another ancillary data object described by a
  2348.    cmsghdr structure.  For these ancillary data objects, the cmsg_level
  2349.  
  2350.  
  2351.  
  2352.  
  2353.  
  2354. Stevens & Thomas             Informational                     [Page 42]
  2355.  
  2356. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2357.  
  2358.  
  2359.    member will be IPPROTO_IPV6 and the cmsg_type member will be
  2360.    IPV6_HOPOPTS.  These options are then processed by calling the
  2361.    inet6_option_next() and inet6_option_find() functions.
  2362.  
  2363. 7.2.  Sending Destination Options
  2364.  
  2365.    To send one or more Destination options, the application just
  2366.    specifies them as ancillary data in a call to sendmsg().  No socket
  2367.    option need be set.
  2368.  
  2369.    As described earlier, one set of Destination options can appear
  2370.    before a Routing header, and one set can appear after a Routing
  2371.    header.  Each set can consist of one or more options.
  2372.  
  2373.    Normally all the Destination options in a set are specified by a
  2374.    single ancillary data object, since each option is itself TLV-
  2375.    encoded.  Multiple ancillary data objects, each containing one or
  2376.    more Destination options, can also be specified, in which case the
  2377.    kernel will combine all the Destination options in the set into a
  2378.    single Destination extension header.  But it should be more efficient
  2379.    to use a single ancillary data object to describe all the Destination
  2380.    options in a set.  The cmsg_level member is set to IPPROTO_IPV6 and
  2381.    the cmsg_type member is set to IPV6_DSTOPTS.  The option is normally
  2382.    constructed using the inet6_option_init(), inet6_option_append(), and
  2383.    inet6_option_alloc() functions.
  2384.  
  2385.    Additional errors may be possible from sendmsg() if the specified
  2386.    option is in error.
  2387.  
  2388. 8.  Routing Header Option
  2389.  
  2390.    Source routing in IPv6 is accomplished by specifying a Routing header
  2391.    as an extension header.  There can be different types of Routing
  2392.    headers, but IPv6 currently defines only the Type 0 Routing header
  2393.    [RFC-1883].  This type supports up to 23 intermediate nodes.  With
  2394.    this maximum number of intermediate nodes, a source, and a
  2395.    destination, there are 24 hops, each of which is defined as a strict
  2396.    or loose hop.
  2397.  
  2398.    Source routing with IPv4 sockets API (the IP_OPTIONS socket option)
  2399.    requires the application to build the source route in the format that
  2400.    appears as the IPv4 header option, requiring intimate knowledge of
  2401.    the IPv4 options format.  This IPv6 API, however, defines eight
  2402.    functions that the application calls to build and examine a Routing
  2403.    header.  Four functions build a Routing header:
  2404.  
  2405.    inet6_rthdr_space()    - return #bytes required for ancillary data
  2406.    inet6_rthdr_init()     - initialize ancillary data for Routing header
  2407.  
  2408.  
  2409.  
  2410. Stevens & Thomas             Informational                     [Page 43]
  2411.  
  2412. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2413.  
  2414.  
  2415.    inet6_rthdr_add()      - add IPv6 address & flags to Routing header
  2416.    inet6_rthdr_lasthop()  - specify the flags for the final hop
  2417.  
  2418.    Four functions deal with a returned Routing header:
  2419.  
  2420.     inet6_rthdr_reverse()  - reverse a Routing header
  2421.     inet6_rthdr_segments() - return #segments in a Routing header
  2422.     inet6_rthdr_getaddr()  - fetch one address from a Routing header
  2423.     inet6_rthdr_getflags() - fetch one flag from a Routing header
  2424.  
  2425.    The function prototypes for these functions are all in the
  2426.    <netinet/in.h> header.
  2427.  
  2428.    To receive a Routing header the application must enable the
  2429.    IPV6_RTHDR socket option:
  2430.  
  2431.        int  on = 1;
  2432.        setsockopt(fd, IPPROTO_IPV6, IPV6_RTHDR, &on, sizeof(on));
  2433.  
  2434.    To send a Routing header the application just specifies it as
  2435.    ancillary data in a call to sendmsg().
  2436.  
  2437.    A Routing header is passed between the application and the kernel as
  2438.    an ancillary data object.  The cmsg_level member has a value of
  2439.    IPPROTO_IPV6 and the cmsg_type member has a value of IPV6_RTHDR.  The
  2440.    contents of the cmsg_data[] member is implementation dependent and
  2441.    should not be accessed directly by the application, but should be
  2442.    accessed using the eight functions that we are about to describe.
  2443.  
  2444.    The following constants are defined in the <netinet/in.h> header:
  2445.  
  2446.     #define IPV6_RTHDR_LOOSE     0 /* this hop need not be a neighbor */
  2447.     #define IPV6_RTHDR_STRICT    1 /* this hop must be a neighbor */
  2448.  
  2449.     #define IPV6_RTHDR_TYPE_0    0 /* IPv6 Routing header type 0 */
  2450.  
  2451.    When a Routing header is specified, the destination address specified
  2452.    for connect(), sendto(), or sendmsg() is the final destination
  2453.    address of the datagram.  The Routing header then contains the
  2454.    addresses of all the intermediate nodes.
  2455.  
  2456. 8.1.  inet6_rthdr_space
  2457.  
  2458.        size_t inet6_rthdr_space(int type, int segments);
  2459.  
  2460.    This function returns the number of bytes required to hold a Routing
  2461.    header of the specified type containing the specified number of
  2462.  
  2463.  
  2464.  
  2465.  
  2466. Stevens & Thomas             Informational                     [Page 44]
  2467.  
  2468. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2469.  
  2470.  
  2471.    segments (addresses).  For an IPv6 Type 0 Routing header, the number
  2472.    of segments must be between 1 and 23, inclusive.  The return value
  2473.    includes the size of the cmsghdr structure that precedes the Routing
  2474.    header, and any required padding.
  2475.  
  2476.    If the return value is 0, then either the type of the Routing header
  2477.    is not supported by this implementation or the number of segments is
  2478.    invalid for this type of Routing header.
  2479.  
  2480.    (Note: This function returns the size but does not allocate the space
  2481.    required for the ancillary data.  This allows an application to
  2482.    allocate a larger buffer, if other ancillary data objects are
  2483.    desired, since all the ancillary data objects must be specified to
  2484.    sendmsg() as a single msg_control buffer.)
  2485.  
  2486. 8.2.  inet6_rthdr_init
  2487.  
  2488.        struct cmsghdr *inet6_rthdr_init(void *bp, int type);
  2489.  
  2490.    This function initializes the buffer pointed to by bp to contain a
  2491.    cmsghdr structure followed by a Routing header of the specified type.
  2492.    The cmsg_len member of the cmsghdr structure is initialized to the
  2493.    size of the structure plus the amount of space required by the
  2494.    Routing header.  The cmsg_level and cmsg_type members are also
  2495.    initialized as required.
  2496.  
  2497.    The caller must allocate the buffer and its size can be determined by
  2498.    calling inet6_rthdr_space().
  2499.  
  2500.    Upon success the return value is the pointer to the cmsghdr
  2501.    structure, and this is then used as the first argument to the next
  2502.    two functions.  Upon an error the return value is NULL.
  2503.  
  2504. 8.3.  inet6_rthdr_add
  2505.  
  2506.     int inet6_rthdr_add(struct cmsghdr *cmsg,
  2507.                         const struct in6_addr *addr, unsigned int flags);
  2508.  
  2509.    This function adds the address pointed to by addr to the end of the
  2510.    Routing header being constructed and sets the type of this hop to the
  2511.    value of flags.  For an IPv6 Type 0 Routing header, flags must be
  2512.    either IPV6_RTHDR_LOOSE or IPV6_RTHDR_STRICT.
  2513.  
  2514.    If successful, the cmsg_len member of the cmsghdr structure is
  2515.    updated to account for the new address in the Routing header and the
  2516.    return value of the function is 0.  Upon an error the return value of
  2517.    the function is -1.
  2518.  
  2519.  
  2520.  
  2521.  
  2522. Stevens & Thomas             Informational                     [Page 45]
  2523.  
  2524. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2525.  
  2526.  
  2527. 8.4.  inet6_rthdr_lasthop
  2528.  
  2529.        int inet6_rthdr_lasthop(struct cmsghdr *cmsg,
  2530.                                unsigned int flags);
  2531.  
  2532.    This function specifies the Strict/Loose flag for the final hop of a
  2533.    Routing header.  For an IPv6 Type 0 Routing header, flags must be
  2534.    either IPV6_RTHDR_LOOSE or IPV6_RTHDR_STRICT.
  2535.  
  2536.    The return value of the function is 0 upon success, or -1 upon an
  2537.    error.
  2538.  
  2539.    Notice that a Routing header specifying N intermediate nodes requires
  2540.    N+1 Strict/Loose flags.  This requires N calls to inet6_rthdr_add()
  2541.    followed by one call to inet6_rthdr_lasthop().
  2542.  
  2543. 8.5.  inet6_rthdr_reverse
  2544.  
  2545.     int inet6_rthdr_reverse(const struct cmsghdr *in, struct cmsghdr *out);
  2546.  
  2547.    This function takes a Routing header that was received as ancillary
  2548.    data (pointed to by the first argument) and writes a new Routing
  2549.    header that sends datagrams along the reverse of that route.  Both
  2550.    arguments are allowed to point to the same buffer (that is, the
  2551.    reversal can occur in place).
  2552.  
  2553.    The return value of the function is 0 on success, or -1 upon an
  2554.    error.
  2555.  
  2556. 8.6.  inet6_rthdr_segments
  2557.  
  2558.        int inet6_rthdr_segments(const struct cmsghdr *cmsg);
  2559.  
  2560.    This function returns the number of segments (addresses) contained in
  2561.    the Routing header described by cmsg.  On success the return value is
  2562.    between 1 and 23, inclusive.  The return value of the function is -1
  2563.    upon an error.
  2564.  
  2565. 8.7.  inet6_rthdr_getaddr
  2566.  
  2567.     struct in6_addr *inet6_rthdr_getaddr(struct cmsghdr *cmsg, int
  2568.     index);
  2569.  
  2570.    This function returns a pointer to the IPv6 address specified by
  2571.    index (which must have a value between 1 and the value returned by
  2572.    inet6_rthdr_segments()) in the Routing header described by cmsg.  An
  2573.    application should first call inet6_rthdr_segments() to obtain the
  2574.    number of segments in the Routing header.
  2575.  
  2576.  
  2577.  
  2578. Stevens & Thomas             Informational                     [Page 46]
  2579.  
  2580. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2581.  
  2582.  
  2583.    Upon an error the return value of the function is NULL.
  2584.  
  2585. 8.8.  inet6_rthdr_getflags
  2586.  
  2587.        int inet6_rthdr_getflags(const struct cmsghdr *cmsg, int index);
  2588.  
  2589.    This function returns the flags value specified by index (which must
  2590.    have a value between 0 and the value returned by
  2591.    inet6_rthdr_segments()) in the Routing header described by cmsg.  For
  2592.    an IPv6 Type 0 Routing header the return value will be either
  2593.    IPV6_RTHDR_LOOSE or IPV6_RTHDR_STRICT.
  2594.  
  2595.    Upon an error the return value of the function is -1.
  2596.  
  2597.    (Note: Addresses are indexed starting at 1, and flags starting at 0,
  2598.    to maintain consistency with the terminology and figures in [RFC-
  2599.    1883].)
  2600.  
  2601. 8.9.  Routing Header Example
  2602.  
  2603.    As an example of these Routing header functions, we go through the
  2604.    function calls for the example on p. 18 of [RFC-1883].  The source is
  2605.    S, the destination is D, and the three intermediate nodes are I1, I2,
  2606.    and I3.  f0, f1, f2, and f3 are the Strict/Loose flags for each hop.
  2607.  
  2608.                    f0        f1        f2        f3
  2609.                S -----> I1 -----> I2 -----> I3 -----> D
  2610.  
  2611.        src:    *    S         S         S         S   S
  2612.        dst:    D   I1        I2        I3         D   D
  2613.        A[1]:  I1   I2        I1        I1        I1  I1
  2614.        A[2]:  I2   I3        I3        I2        I2  I2
  2615.        A[3]:  I3    D         D         D        I3  I3
  2616.        #seg:   3    3         2         1         0   3
  2617.  
  2618.        check: f0        f1        f2        f3
  2619.  
  2620.    src and dst are the source and destination IPv6 addresses in the IPv6
  2621.    header.  A[1], A[2], and A[3] are the three addresses in the Routing
  2622.    header.  #seg is the Segments Left field in the Routing header.
  2623.    check indicates which bit of the Strict/Loose Bit Map (0 through 3,
  2624.    specified as f0 through f3) that node checks.
  2625.  
  2626.    The six values in the column beneath node S are the values in the
  2627.    Routing header specified by the application using sendmsg().  The
  2628.    function calls by the sender would look like:
  2629.  
  2630.  
  2631.  
  2632.  
  2633.  
  2634. Stevens & Thomas             Informational                     [Page 47]
  2635.  
  2636. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2637.  
  2638.  
  2639.        void  *ptr;
  2640.        struct msghdr  msg;
  2641.        struct cmsghdr  *cmsgptr;
  2642.        struct sockaddr_in6  I1, I2, I3, D;
  2643.        unsigned int  f0, f1, f2, f3;
  2644.  
  2645.        ptr = malloc(inet6_rthdr_space(IPV6_RTHDR_TYPE_0, 3));
  2646.        cmsgptr = inet6_rthdr_init(ptr, IPV6_RTHDR_TYPE_0);
  2647.  
  2648.        inet6_rthdr_add(cmsgptr, &I1.sin6_addr, f0);
  2649.        inet6_rthdr_add(cmsgptr, &I2.sin6_addr, f1);
  2650.        inet6_rthdr_add(cmsgptr, &I3.sin6_addr, f2);
  2651.        inet6_rthdr_lasthop(cmsgptr, f3);
  2652.  
  2653.        msg.msg_control = ptr;
  2654.        msg.msg_controllen = cmsgptr->cmsg_len;
  2655.  
  2656.        /* finish filling in msg{}, msg_name = D */
  2657.        /* call sendmsg() */
  2658.  
  2659.    We also assume that the source address for the socket is not
  2660.    specified (i.e., the asterisk in the figure).
  2661.  
  2662.    The four columns of six values that are then shown between the five
  2663.    nodes are the values of the fields in the packet while the packet is
  2664.    in transit between the two nodes.  Notice that before the packet is
  2665.    sent by the source node S, the source address is chosen (replacing
  2666.    the asterisk), I1 becomes the destination address of the datagram,
  2667.    the two addresses A[2] and A[3] are "shifted up", and D is moved to
  2668.    A[3].  If f0 is IPV6_RTHDR_STRICT, then I1 must be a neighbor of S.
  2669.  
  2670.    The columns of values that are shown beneath the destination node are
  2671.    the values returned by recvmsg(), assuming the application has
  2672.    enabled both the IPV6_PKTINFO and IPV6_RTHDR socket options.  The
  2673.    source address is S (contained in the sockaddr_in6 structure pointed
  2674.    to by the msg_name member), the destination address is D (returned as
  2675.    an ancillary data object in an in6_pktinfo structure), and the
  2676.    ancillary data object specifying the Routing header will contain
  2677.    three addresses (I1, I2, and I3) and four flags (f0, f1, f2, and f3).
  2678.    The number of segments in the Routing header is known from the Hdr
  2679.    Ext Len field in the Routing header (a value of 6, indicating 3
  2680.    addresses).
  2681.  
  2682.    The return value from inet6_rthdr_segments() will be 3 and
  2683.    inet6_rthdr_getaddr(1) will return I1, inet6_rthdr_getaddr(2) will
  2684.    return I2, and inet6_rthdr_getaddr(3) will return I3, The return
  2685.  
  2686.  
  2687.  
  2688.  
  2689.  
  2690. Stevens & Thomas             Informational                     [Page 48]
  2691.  
  2692. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2693.  
  2694.  
  2695.    value from inet6_rthdr_flags(0) will be f0, inet6_rthdr_flags(1) will
  2696.    return f1, inet6_rthdr_flags(2) will return f2, and
  2697.    inet6_rthdr_flags(3) will return f3.
  2698.  
  2699.    If the receiving application then calls inet6_rthdr_reverse(), the
  2700.    order of the three addresses will become I3, I2, and I1, and the
  2701.    order of the four Strict/Loose flags will become f3, f2, f1, and f0.
  2702.  
  2703.    We can also show what an implementation might store in the ancillary
  2704.    data object as the Routing header is being built by the sending
  2705.    process.  If we assume a 32-bit architecture where sizeof(struct
  2706.    cmsghdr) equals 12, with a desired alignment of 4-byte boundaries,
  2707.    then the call to inet6_rthdr_space(3) returns 68: 12 bytes for the
  2708.    cmsghdr structure and 56 bytes for the Routing header (8 + 3*16).
  2709.  
  2710.    The call to inet6_rthdr_init() initializes the ancillary data object
  2711.    to contain a Type 0 Routing header:
  2712.  
  2713.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2714.      |       cmsg_len = 20                                           |
  2715.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2716.      |       cmsg_level = IPPROTO_IPV6                               |
  2717.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2718.      |       cmsg_type = IPV6_RTHDR                                  |
  2719.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2720.      |  Next Header  | Hdr Ext Len=0 | Routing Type=0|  Seg Left=0   |
  2721.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2722.      |   Reserved    |             Strict/Loose Bit Map              |
  2723.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2724.  
  2725.    The first call to inet6_rthdr_add() adds I1 to the list.
  2726.  
  2727.  
  2728.  
  2729.  
  2730.  
  2731.  
  2732.  
  2733.  
  2734.  
  2735.  
  2736.  
  2737.  
  2738.  
  2739.  
  2740.  
  2741.  
  2742.  
  2743.  
  2744.  
  2745.  
  2746. Stevens & Thomas             Informational                     [Page 49]
  2747.  
  2748. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2749.  
  2750.  
  2751.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2752.      |       cmsg_len = 36                                           |
  2753.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2754.      |       cmsg_level = IPPROTO_IPV6                               |
  2755.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2756.      |       cmsg_type = IPV6_RTHDR                                  |
  2757.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2758.      |  Next Header  | Hdr Ext Len=2 | Routing Type=0|  Seg Left=1   |
  2759.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2760.      |   Reserved    |X|           Strict/Loose Bit Map              |
  2761.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2762.      |                                                               |
  2763.      +                                                               +
  2764.      |                                                               |
  2765.      +                           Address[1] = I1                     +
  2766.      |                                                               |
  2767.      +                                                               +
  2768.      |                                                               |
  2769.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2770.  
  2771.    Bit 0 of the Strict/Loose Bit Map contains the value f0, which we
  2772.    just mark as X.  cmsg_len is incremented by 16, the Hdr Ext Len field
  2773.    is incremented by 2, and the Segments Left field is incremented by 1.
  2774.  
  2775.    The next call to inet6_rthdr_add() adds I2 to the list.
  2776.  
  2777.  
  2778.  
  2779.  
  2780.  
  2781.  
  2782.  
  2783.  
  2784.  
  2785.  
  2786.  
  2787.  
  2788.  
  2789.  
  2790.  
  2791.  
  2792.  
  2793.  
  2794.  
  2795.  
  2796.  
  2797.  
  2798.  
  2799.  
  2800.  
  2801.  
  2802. Stevens & Thomas             Informational                     [Page 50]
  2803.  
  2804. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2805.  
  2806.  
  2807.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2808.      |       cmsg_len = 52                                           |
  2809.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2810.      |       cmsg_level = IPPROTO_IPV6                               |
  2811.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2812.      |       cmsg_type = IPV6_RTHDR                                  |
  2813.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2814.      |  Next Header  | Hdr Ext Len=4 | Routing Type=0|  Seg Left=2   |
  2815.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2816.      |   Reserved    |X|X|         Strict/Loose Bit Map              |
  2817.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2818.      |                                                               |
  2819.      +                                                               +
  2820.      |                                                               |
  2821.      +                           Address[1] = I1                     +
  2822.      |                                                               |
  2823.      +                                                               +
  2824.      |                                                               |
  2825.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2826.      |                                                               |
  2827.      +                                                               +
  2828.      |                                                               |
  2829.      +                           Address[2] = I2                     +
  2830.      |                                                               |
  2831.      +                                                               +
  2832.      |                                                               |
  2833.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2834.  
  2835.    The next bit of the Strict/Loose Bit Map contains the value f1.
  2836.    cmsg_len is incremented by 16, the Hdr Ext Len field is incremented
  2837.    by 2, and the Segments Left field is incremented by 1.
  2838.  
  2839.    The last call to inet6_rthdr_add() adds I3 to the list.
  2840.  
  2841.  
  2842.  
  2843.  
  2844.  
  2845.  
  2846.  
  2847.  
  2848.  
  2849.  
  2850.  
  2851.  
  2852.  
  2853.  
  2854.  
  2855.  
  2856.  
  2857.  
  2858. Stevens & Thomas             Informational                     [Page 51]
  2859.  
  2860. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2861.  
  2862.  
  2863.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2864.      |       cmsg_len = 68                                           |
  2865.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2866.      |       cmsg_level = IPPROTO_IPV6                               |
  2867.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2868.      |       cmsg_type = IPV6_RTHDR                                  |
  2869.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2870.      |  Next Header  | Hdr Ext Len=6 | Routing Type=0|  Seg Left=3   |
  2871.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2872.      |   Reserved    |X|X|X|       Strict/Loose Bit Map              |
  2873.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2874.      |                                                               |
  2875.      +                                                               +
  2876.      |                                                               |
  2877.      +                           Address[1] = I1                     +
  2878.      |                                                               |
  2879.      +                                                               +
  2880.      |                                                               |
  2881.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2882.      |                                                               |
  2883.      +                                                               +
  2884.      |                                                               |
  2885.      +                           Address[2] = I2                     +
  2886.      |                                                               |
  2887.      +                                                               +
  2888.      |                                                               |
  2889.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2890.      |                                                               |
  2891.      +                                                               +
  2892.      |                                                               |
  2893.      +                           Address[3] = I3                     +
  2894.      |                                                               |
  2895.      +                                                               +
  2896.      |                                                               |
  2897.      +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  2898.  
  2899.    The next bit of the Strict/Loose Bit Map contains the value f2.
  2900.    cmsg_len is incremented by 16, the Hdr Ext Len field is incremented
  2901.    by 2, and the Segments Left field is incremented by 1.
  2902.  
  2903.    Finally, the call to inet6_rthdr_lasthop() sets the next bit of the
  2904.    Strict/Loose Bit Map to the value specified by f3.  All the lengths
  2905.    remain unchanged.
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913.  
  2914. Stevens & Thomas             Informational                     [Page 52]
  2915.  
  2916. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2917.  
  2918.  
  2919. 9.  Ordering of Ancillary Data and IPv6 Extension Headers
  2920.  
  2921.    Three IPv6 extension headers can be specified by the application and
  2922.    returned to the application using ancillary data with sendmsg() and
  2923.    recvmsg(): Hop-by-Hop options, Destination options, and the Routing
  2924.    header.  When multiple ancillary data objects are transferred via
  2925.    sendmsg() or recvmsg() and these objects represent any of these three
  2926.    extension headers, their placement in the control buffer is directly
  2927.    tied to their location in the corresponding IPv6 datagram.  This API
  2928.    imposes some ordering constraints when using multiple ancillary data
  2929.    objects with sendmsg().
  2930.  
  2931.    When multiple IPv6 Hop-by-Hop options having the same option type are
  2932.    specified, these options will be inserted into the Hop-by-Hop options
  2933.    header in the same order as they appear in the control buffer.  But
  2934.    when multiple Hop-by-Hop options having different option types are
  2935.    specified, these options may be reordered by the kernel to reduce
  2936.    padding in the Hop-by-Hop options header.  Hop-by-Hop options may
  2937.    appear anywhere in the control buffer and will always be collected by
  2938.    the kernel and placed into a single Hop-by-Hop options header that
  2939.    immediately follows the IPv6 header.
  2940.  
  2941.    Similar rules apply to the Destination options: (1) those of the same
  2942.    type will appear in the same order as they are specified, and (2)
  2943.    those of differing types may be reordered.  But the kernel will build
  2944.    up to two Destination options headers: one to precede the Routing
  2945.    header and one to follow the Routing header.  If the application
  2946.    specifies a Routing header then all Destination options that appear
  2947.    in the control buffer before the Routing header will appear in a
  2948.    Destination options header before the Routing header and these
  2949.    options might be reordered, subject to the two rules that we just
  2950.    stated.  Similarly all Destination options that appear in the control
  2951.    buffer after the Routing header will appear in a Destination options
  2952.    header after the Routing header, and these options might be
  2953.    reordered, subject to the two rules that we just stated.
  2954.  
  2955.    As an example, assume that an application specifies control
  2956.    information to sendmsg() containing six ancillary data objects: the
  2957.    first containing two Hop-by-Hop options, the second containing one
  2958.    Destination option, the third containing two Destination options, the
  2959.    fourth containing a Routing header, the fifth containing a Hop-by-Hop
  2960.    option, and the sixth containing two Destination options.  We also
  2961.    assume that all the Hop-by-Hop options are of different types, as are
  2962.    all the Destination options.  We number these options 1-9,
  2963.    corresponding to their order in the control buffer, and show them on
  2964.    the left below.
  2965.  
  2966.  
  2967.  
  2968.  
  2969.  
  2970. Stevens & Thomas             Informational                     [Page 53]
  2971.  
  2972. RFC 2292             Advanced Sockets API for IPv6         February 1998
  2973.  
  2974.  
  2975.    In the middle we show the final arrangement of the options in the
  2976.    extension headers built by the kernel.  On the right we show the four
  2977.    ancillary data objects returned to the receiving application.
  2978.  
  2979.           Sender's                                       Receiver's
  2980.        Ancillary Data      -->   IPv6 Extension   -->  Ancillary Data
  2981.           Objects                    Headers              Objects
  2982.        ------------------        ---------------       --------------
  2983.        HOPOPT-1,2 (first)        HOPHDR(J,7,1,2)       HOPOPT-7,1,2
  2984.        DSTOPT-3                  DSTHDR(4,5,3)         DSTOPT-4,5,3
  2985.        DSTOPT-4,5                RTHDR(6)              RTHDR-6
  2986.        RTHDR-6                   DSTHDR(8,9)           DSTOPT-8,9
  2987.        HOPOPT-7
  2988.        DSTOPT-8,9 (last)
  2989.  
  2990.    The sender's two Hop-by-Hop ancillary data objects are reordered, as
  2991.    are the first two Destination ancillary data objects.  We also show a
  2992.    Jumbo Payload option (denoted as J) inserted by the kernel before the
  2993.    sender's three Hop-by-Hop options.  The first three Destination
  2994.    options must appear in a Destination header before the Routing
  2995.    header, and the final two Destination options must appear in a
  2996.    Destination header after the Routing header.
  2997.  
  2998.    If Destination options are specified in the control buffer after a
  2999.    Routing header, or if Destination options are specified without a
  3000.    Routing header, the kernel will place those Destination options after
  3001.    an authentication header and/or an encapsulating security payload
  3002.    header, if present.
  3003.  
  3004. 10.  IPv6-Specific Options with IPv4-Mapped IPv6 Addresses
  3005.  
  3006.    The various socket options and ancillary data specifications defined
  3007.    in this document apply only to true IPv6 sockets.  It is possible to
  3008.    create an IPv6 socket that actually sends and receives IPv4 packets,
  3009.    using IPv4-mapped IPv6 addresses, but the mapping of the options
  3010.    defined in this document to an IPv4 datagram is beyond the scope of
  3011.    this document.
  3012.  
  3013.    In general, attempting to specify an IPv6-only option, such as the
  3014.    Hop-by-Hop options, Destination options, or Routing header on an IPv6
  3015.    socket that is using IPv4-mapped IPv6 addresses, will probably result
  3016.    in an error.  Some implementations, however, may provide access to
  3017.    the packet information (source/destination address, send/receive
  3018.    interface, and hop limit) on an IPv6 socket that is using IPv4-mapped
  3019.    IPv6 addresses.
  3020.  
  3021.  
  3022.  
  3023.  
  3024.  
  3025.  
  3026. Stevens & Thomas             Informational                     [Page 54]
  3027.  
  3028. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3029.  
  3030.  
  3031. 11.  rresvport_af
  3032.  
  3033.    The rresvport() function is used by the rcmd() function, and this
  3034.    function is in turn called by many of the "r" commands such as
  3035.    rlogin.  While new applications are not being written to use the
  3036.    rcmd() function, legacy applications such as rlogin will continue to
  3037.    use it and these will be ported to IPv6.
  3038.  
  3039.    rresvport() creates an IPv4/TCP socket and binds a "reserved port" to
  3040.    the socket.  Instead of defining an IPv6 version of this function we
  3041.    define a new function that takes an address family as its argument.
  3042.  
  3043.        #include <unistd.h>
  3044.  
  3045.        int  rresvport_af(int *port, int family);
  3046.  
  3047.    This function behaves the same as the existing rresvport() function,
  3048.    but instead of creating an IPv4/TCP socket, it can also create an
  3049.    IPv6/TCP socket.  The family argument is either AF_INET or AF_INET6,
  3050.    and a new error return is EAFNOSUPPORT if the address family is not
  3051.    supported.
  3052.  
  3053.    (Note: There is little consensus on which header defines the
  3054.    rresvport() and rcmd() function prototypes.  4.4BSD defines it in
  3055.    <unistd.h>, others in <netdb.h>, and others don't define the function
  3056.    prototypes at all.)
  3057.  
  3058.    (Note: We define this function only, and do not define something like
  3059.    rcmd_af() or rcmd6().  The reason is that rcmd() calls
  3060.    gethostbyname(), which returns the type of address: AF_INET or
  3061.    AF_INET6.  It should therefore be possible to modify rcmd() to
  3062.    support either IPv4 or IPv6, based on the address family returned by
  3063.    gethostbyname().)
  3064.  
  3065. 12.  Future Items
  3066.  
  3067.    Some additional items may require standardization, but no concrete
  3068.    proposals have been made for the API to perform these tasks.  These
  3069.    may be addressed in a later document.
  3070.  
  3071. 12.1.  Flow Labels
  3072.  
  3073.    Earlier revisions of this document specified a set of
  3074.    inet6_flow_XXX() functions to assign, share, and free IPv6 flow
  3075.    labels.  Consensus, however, indicated that it was premature to
  3076.    specify this part of the API.
  3077.  
  3078.  
  3079.  
  3080.  
  3081.  
  3082. Stevens & Thomas             Informational                     [Page 55]
  3083.  
  3084. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3085.  
  3086.  
  3087. 12.2.  Path MTU Discovery and UDP
  3088.  
  3089.    A standard method may be desirable for a UDP application to determine
  3090.    the "maximum send transport-message size" (Section 5.1 of [RFC-1981])
  3091.    to a given destination.  This would let the UDP application send
  3092.    smaller datagrams to the destination, avoiding fragmentation.
  3093.  
  3094. 12.3.  Neighbor Reachability and UDP
  3095.  
  3096.    A standard method may be desirable for a UDP application to tell the
  3097.    kernel that it is making forward progress with a given peer (Section
  3098.    7.3.1 of [RFC-1970]).  This could save unneeded neighbor
  3099.    solicitations and neighbor advertisements.
  3100.  
  3101. 13.  Summary of New Definitions
  3102.  
  3103.    The following list summarizes the constants and structure,
  3104.    definitions discussed in this memo, sorted by header.
  3105.  
  3106.      <netinet/icmp6.h> ICMP6_DST_UNREACH
  3107.      <netinet/icmp6.h> ICMP6_DST_UNREACH_ADDR
  3108.      <netinet/icmp6.h> ICMP6_DST_UNREACH_ADMIN
  3109.      <netinet/icmp6.h> ICMP6_DST_UNREACH_NOPORT
  3110.      <netinet/icmp6.h> ICMP6_DST_UNREACH_NOROUTE
  3111.      <netinet/icmp6.h> ICMP6_DST_UNREACH_NOTNEIGHBOR
  3112.      <netinet/icmp6.h> ICMP6_ECHO_REPLY
  3113.      <netinet/icmp6.h> ICMP6_ECHO_REQUEST
  3114.      <netinet/icmp6.h> ICMP6_INFOMSG_MASK
  3115.      <netinet/icmp6.h> ICMP6_MEMBERSHIP_QUERY
  3116.      <netinet/icmp6.h> ICMP6_MEMBERSHIP_REDUCTION
  3117.      <netinet/icmp6.h> ICMP6_MEMBERSHIP_REPORT
  3118.      <netinet/icmp6.h> ICMP6_PACKET_TOO_BIG
  3119.      <netinet/icmp6.h> ICMP6_PARAMPROB_HEADER
  3120.      <netinet/icmp6.h> ICMP6_PARAMPROB_NEXTHEADER
  3121.      <netinet/icmp6.h> ICMP6_PARAMPROB_OPTION
  3122.      <netinet/icmp6.h> ICMP6_PARAM_PROB
  3123.      <netinet/icmp6.h> ICMP6_TIME_EXCEEDED
  3124.      <netinet/icmp6.h> ICMP6_TIME_EXCEED_REASSEMBLY
  3125.      <netinet/icmp6.h> ICMP6_TIME_EXCEED_TRANSIT
  3126.      <netinet/icmp6.h> ND_NA_FLAG_OVERRIDE
  3127.      <netinet/icmp6.h> ND_NA_FLAG_ROUTER
  3128.      <netinet/icmp6.h> ND_NA_FLAG_SOLICITED
  3129.      <netinet/icmp6.h> ND_NEIGHBOR_ADVERT
  3130.      <netinet/icmp6.h> ND_NEIGHBOR_SOLICIT
  3131.      <netinet/icmp6.h> ND_OPT_MTU
  3132.      <netinet/icmp6.h> ND_OPT_PI_FLAG_AUTO
  3133.      <netinet/icmp6.h> ND_OPT_PI_FLAG_ONLINK
  3134.      <netinet/icmp6.h> ND_OPT_PREFIX_INFORMATION
  3135.  
  3136.  
  3137.  
  3138. Stevens & Thomas             Informational                     [Page 56]
  3139.  
  3140. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3141.  
  3142.  
  3143.      <netinet/icmp6.h> ND_OPT_REDIRECTED_HEADER
  3144.      <netinet/icmp6.h> ND_OPT_SOURCE_LINKADDR
  3145.      <netinet/icmp6.h> ND_OPT_TARGET_LINKADDR
  3146.      <netinet/icmp6.h> ND_RA_FLAG_MANAGED
  3147.      <netinet/icmp6.h> ND_RA_FLAG_OTHER
  3148.      <netinet/icmp6.h> ND_REDIRECT
  3149.      <netinet/icmp6.h> ND_ROUTER_ADVERT
  3150.      <netinet/icmp6.h> ND_ROUTER_SOLICIT
  3151.  
  3152.      <netinet/icmp6.h> struct icmp6_filter{};
  3153.      <netinet/icmp6.h> struct icmp6_hdr{};
  3154.      <netinet/icmp6.h> struct nd_neighbor_advert{};
  3155.      <netinet/icmp6.h> struct nd_neighbor_solicit{};
  3156.      <netinet/icmp6.h> struct nd_opt_hdr{};
  3157.      <netinet/icmp6.h> struct nd_opt_mtu{};
  3158.      <netinet/icmp6.h> struct nd_opt_prefix_info{};
  3159.      <netinet/icmp6.h> struct nd_opt_rd_hdr{};
  3160.      <netinet/icmp6.h> struct nd_redirect{};
  3161.      <netinet/icmp6.h> struct nd_router_advert{};
  3162.      <netinet/icmp6.h> struct nd_router_solicit{};
  3163.  
  3164.      <netinet/in.h>    IPPROTO_AH
  3165.      <netinet/in.h>    IPPROTO_DSTOPTS
  3166.      <netinet/in.h>    IPPROTO_ESP
  3167.      <netinet/in.h>    IPPROTO_FRAGMENT
  3168.      <netinet/in.h>    IPPROTO_HOPOPTS
  3169.      <netinet/in.h>    IPPROTO_ICMPV6
  3170.      <netinet/in.h>    IPPROTO_IPV6
  3171.      <netinet/in.h>    IPPROTO_NONE
  3172.      <netinet/in.h>    IPPROTO_ROUTING
  3173.      <netinet/in.h>    IPV6_DSTOPTS
  3174.      <netinet/in.h>    IPV6_HOPLIMIT
  3175.      <netinet/in.h>    IPV6_HOPOPTS
  3176.      <netinet/in.h>    IPV6_NEXTHOP
  3177.      <netinet/in.h>    IPV6_PKTINFO
  3178.      <netinet/in.h>    IPV6_PKTOPTIONS
  3179.      <netinet/in.h>    IPV6_RTHDR
  3180.      <netinet/in.h>    IPV6_RTHDR_LOOSE
  3181.      <netinet/in.h>    IPV6_RTHDR_STRICT
  3182.      <netinet/in.h>    IPV6_RTHDR_TYPE_0
  3183.      <netinet/in.h>    struct in6_pktinfo{};
  3184.  
  3185.      <netinet/ip6.h>   IP6F_OFF_MASK
  3186.      <netinet/ip6.h>   IP6F_RESERVED_MASK
  3187.      <netinet/ip6.h>   IP6F_MORE_FRAG
  3188.      <netinet/ip6.h>   struct ip6_dest{};
  3189.      <netinet/ip6.h>   struct ip6_frag{};
  3190.      <netinet/ip6.h>   struct ip6_hbh{};
  3191.  
  3192.  
  3193.  
  3194. Stevens & Thomas             Informational                     [Page 57]
  3195.  
  3196. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3197.  
  3198.  
  3199.      <netinet/ip6.h>   struct ip6_hdr{};
  3200.      <netinet/ip6.h>   struct ip6_rthdr{};
  3201.      <netinet/ip6.h>   struct ip6_rthdr0{};
  3202.  
  3203.      <sys/socket.h>    struct cmsghdr{};
  3204.      <sys/socket.h>    struct msghdr{};
  3205.  
  3206.    The following list summarizes the function and macro prototypes
  3207.    discussed in this memo, sorted by header.
  3208.  
  3209. <netinet/icmp6.h> void ICMP6_FILTER_SETBLOCK(int,
  3210.                                                 struct icmp6_filter *);
  3211. <netinet/icmp6.h> void ICMP6_FILTER_SETBLOCKALL(struct icmp6_filter *);
  3212. <netinet/icmp6.h> void ICMP6_FILTER_SETPASS(int, struct icmp6_filter *);
  3213. <netinet/icmp6.h> void ICMP6_FILTER_SETPASSALL(struct icmp6_filter *);
  3214. <netinet/icmp6.h> int  ICMP6_FILTER_WILLBLOCK(int,
  3215.                                            const struct icmp6_filter *);
  3216. <netinet/icmp6.h> int  ICMP6_FILTER_WILLPASS(int,
  3217.                                            const struct icmp6_filter *);
  3218.  
  3219. <netinet/in.h>    int IN6_ARE_ADDR_EQUAL(const struct in6_addr *,
  3220.                                          const struct in6_addr *);
  3221.  
  3222. <netinet/in.h>    uint8_t *inet6_option_alloc(struct cmsghdr *,
  3223.                                                int, int, int);
  3224. <netinet/in.h>    int inet6_option_append(struct cmsghdr *,
  3225.                                           const uint8_t *, int, int);
  3226. <netinet/in.h>    int inet6_option_find(const struct cmsghdr *,
  3227.                                         uint8_t *, int);
  3228. <netinet/in.h>    int inet6_option_init(void *, struct cmsghdr **, int);
  3229. <netinet/in.h>    int inet6_option_next(const struct cmsghdr *,
  3230.                                         uint8_t **);
  3231. <netinet/in.h>    int inet6_option_space(int);
  3232.  
  3233. <netinet/in.h>    int inet6_rthdr_add(struct cmsghdr *,
  3234.                                       const struct in6_addr *,
  3235.                                       unsigned int);
  3236. <netinet/in.h>    struct in6_addr inet6_rthdr_getaddr(struct cmsghdr *,
  3237.                                                       int);
  3238. <netinet/in.h>    int inet6_rthdr_getflags(const struct cmsghdr *, int);
  3239. <netinet/in.h>    struct cmsghdr *inet6_rthdr_init(void *, int);
  3240. <netinet/in.h>    int inet6_rthdr_lasthop(struct cmsghdr *,
  3241.                                                           unsigned int);
  3242. <netinet/in.h>    int inet6_rthdr_reverse(const struct cmsghdr *,
  3243.                                           struct cmsghdr *);
  3244. <netinet/in.h>    int inet6_rthdr_segments(const struct cmsghdr *);
  3245. <netinet/in.h>    size_t inet6_rthdr_space(int, int);
  3246.  
  3247.  
  3248.  
  3249.  
  3250. Stevens & Thomas             Informational                     [Page 58]
  3251.  
  3252. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3253.  
  3254.  
  3255. <sys/socket.h>    unsigned char *CMSG_DATA(const struct cmsghdr *);
  3256. <sys/socket.h>    struct cmsghdr *CMSG_FIRSTHDR(const struct msghdr *);
  3257. <sys/socket.h>    unsigned int CMSG_LEN(unsigned int);
  3258. <sys/socket.h>    struct cmsghdr *CMSG_NXTHDR(const struct msghdr *mhdr,
  3259.                                               const struct cmsghdr *);
  3260. <sys/socket.h>    unsigned int CMSG_SPACE(unsigned int);
  3261.  
  3262. <unistd.h>        int rresvport_af(int *, int);
  3263.  
  3264. 14.  Security Considerations
  3265.  
  3266.    The setting of certain Hop-by-Hop options and Destination options may
  3267.    be restricted to privileged processes.  Similarly some Hop-by-Hop
  3268.    options and Destination options may not be returned to nonprivileged
  3269.    applications.
  3270.  
  3271. 15.  Change History
  3272.  
  3273.    Changes from the June 1997 Edition (-03 draft)
  3274.  
  3275.     -  Added a note that defined constants for multibyte fields are in
  3276.        network byte order.  This affects the ip6f_offlg member of the
  3277.        Fragment header (Section 2.1.2) and the nd_na_flags_reserved
  3278.        member of the nd_neighbor_advert structure (Section 2.2.2).
  3279.  
  3280.     -  Section 5: the ipi6_ifindex member of the in6_pktinfo structure
  3281.        should be "unsigned int" instead of "int", for consistency with
  3282.        the interface indexes in [RFC-2133].
  3283.  
  3284.     -  Section 6.3.7: the three calls to inet6_option_space() in the
  3285.        examples needed to be arguments to malloc().  The final one of
  3286.        these was missing the "6" in the name "inet6_option_space".
  3287.  
  3288.     -  Section 8.6: the function prototype for inet6_rthdr_segments()
  3289.        was missing the ending semicolon.
  3290.  
  3291.    Changes from the March 1997 Edition (-02 draft)
  3292.  
  3293.     -  In May 1997 Draft 6.6 of Posix 1003.1g (called Posix.1g herein)
  3294.        passed ballot and will be forwarded to the IEEE Standards Board
  3295.        later in 1997 for final approval.  Some changes made for this
  3296.        final Posix draft are incorporated into this Internet Draft,
  3297.        specifically the datatypes mentioned in Section 1 (and used
  3298.        throughout the text), and the socklen_t datatype used in Section
  3299.        4.1 and 4.2.
  3300.  
  3301.     -  Section 1: Added the intN_t signed datatypes, changed the
  3302.        datatype u_intN_t to uintN_t (no underscore after the "u"), and
  3303.  
  3304.  
  3305.  
  3306. Stevens & Thomas             Informational                     [Page 59]
  3307.  
  3308. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3309.  
  3310.  
  3311.        removed the datatype u_intNm_t, as per Draft 6.6 of Posix.1g.
  3312.  
  3313.     -  Name space issues for structure and constant names in Section 2:
  3314.        Many of the structure member names and constant names were
  3315.        changed so that the prefixes are the same.  The following
  3316.        prefixes are used for structure members: "ip6_", "icmp6_", and
  3317.        "nd_".  All constants have the prefixes "ICMP6_" and "ND_".
  3318.  
  3319.     -  New definitions: Section 2.1.2: contains definitions for the IPv6
  3320.        extension headers, other than AH and ESP.  Section 2.2.2:
  3321.        contains additional structures and constants for the neighbor
  3322.        discovery option header and redirected header.
  3323.  
  3324.     -  Section 2.2.2: the enum for the neighbor discovery option field
  3325.        was changed to be a set of #define constants.
  3326.  
  3327.     -  Changed the word "function" to "macro" for references to all the
  3328.        uppercase names in Sections 2.3 (IN6_ARE_ADDR_EQUAL), 3.2
  3329.        (ICMPV6_FILTER_xxx), and 4.3 (CMSG_xxx).
  3330.  
  3331.     -  Added more protocols to the /etc/protocols file (Section 2.4) and
  3332.        changed the name of "icmpv6" to "ipv6-icmp".
  3333.  
  3334.     -  Section 3: Made it more explicit that an application cannot read
  3335.        or write entire IPv6 packets, that all extension headers are
  3336.        passed as ancillary data.  Added a sentence that the kernel
  3337.        fragments packets written to an IPv6 raw socket when necessary.
  3338.        Added a note that IPPROTO_RAW raw IPv6 sockets are not special.
  3339.  
  3340.     -  Section 3.1: Explicitly stated that the checksum option applies
  3341.        to both outgoing packets and received packets.
  3342.  
  3343.     -  Section 3.2: Changed the array name within the icmp6_filter
  3344.        structure from "data" to "icmp6_filt".  Changes the prefix for
  3345.        the filter macros from "ICMPV6_" to "ICMP6_", for consistency
  3346.        with the names in Section 2.2.  Changed the example from a ping
  3347.        program to a program that wants to receive only router
  3348.        advertisements.
  3349.  
  3350.     -  Section 4.1: Changed msg_namelen and msg_controllen from size_t
  3351.        to the Posix.1g socklen_t datatype.  Updated the Note that
  3352.        follows.
  3353.  
  3354.     -  Section 4.2: Changed cmsg_len from size_t to the Posix.1g
  3355.        socklen_t datatype.  Updated the Note that follows.
  3356.  
  3357.  
  3358.  
  3359.  
  3360.  
  3361.  
  3362. Stevens & Thomas             Informational                     [Page 60]
  3363.  
  3364. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3365.  
  3366.  
  3367.     -  Section 4.4: Added a Note that the second and third arguments to
  3368.        getsockopt() and setsockopt() are intentionally the same as the
  3369.        cmsg_level and cmsg_type members.
  3370.  
  3371.     -  Section 4.5: Reorganized the section into a description of the
  3372.        option, followed by the TCP semantics, and the UDP and raw socket
  3373.        semantics.  Added a sentence on how to clear all the sticky
  3374.        options.  Added a note that TCP need not save the options from
  3375.        the most recently received segment until the application says to
  3376.        do so.  Added the statement that ancillary data is never passed
  3377.        with sendmsg() or recvmsg() on a TCP socket.  Simplified the
  3378.        interaction of the sticky options with ancillary data for UDP or
  3379.        raw IP: none of the sticky options are sent if ancillary data is
  3380.        specified.
  3381.  
  3382.     -  Final paragraph of Section 5.1: ipi6_index should be
  3383.        ipi6_ifindex.
  3384.  
  3385.     -  Section 5.4: Added a note on the term "privileged".
  3386.  
  3387.     -  Section 5.5: Noted that the errors listed are examples, and the
  3388.        actual errors depend on the implementation.
  3389.  
  3390.     -  Removed Section 6 ("Flow Labels") as the consensus is that it is
  3391.        premature to try and specify an API for this feature.  Access to
  3392.        the flow label field in the IPv6 header is still provided through
  3393.        the sin6_flowinfo member of the IPv6 socket address structure in
  3394.        [RFC-2133].  Added a subsection to Section 13 that this is a
  3395.        future item.
  3396.  
  3397.        All remaining changes are identified by their section number in
  3398.        the previous draft.  With the removal of Section 6, the section
  3399.        numbers are decremented by one.
  3400.  
  3401.     -  Section 7.3.7: the calls to malloc() in all three examples should
  3402.        be calls to inet6_option_space() instead.  The two calls to
  3403.        inet6_option_append() in the third example should be calls to
  3404.        inet6_option_alloc().  The two calls to CMSG_SPACE() in the first
  3405.        and third examples should be calls to CMSG_LEN().  The second
  3406.        call to CMSG_SPACE() in the second example should be a call to
  3407.        CMSG_LEN().
  3408.  
  3409.     -  Section 7.3.7: All the opt_X_ and opt_Y_ structure member names
  3410.        were changed to be ip6_X_opt_ and ip6_Y_opt_.  The two structure
  3411.        names ipv6_opt_X and ipv6_opt_Y were changed to ip6_X_opt and
  3412.        ip6_Y_opt.  The constants beginning with IPV6_OPT_X_ and
  3413.        IPV6_OPT_Y_ were changed to begin with IP6_X_OPT_ and IP6_Y_OPT_.
  3414.  
  3415.  
  3416.  
  3417.  
  3418. Stevens & Thomas             Informational                     [Page 61]
  3419.  
  3420. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3421.  
  3422.  
  3423.     -  Use the term "Routing header" throughout the draft, instead of
  3424.        "source routing".  Changed the names of the eight
  3425.        inet6_srcrt_XXX() functions in Section 9 to inet6_rthdr_XXX().
  3426.        Changed the name of the socket option from IPV6_SRCRT to
  3427.        IPV6_RTHDR, and the names of the three IPV6_SRCRT_xxx constants
  3428.        in Section 9 to IPV6_RTHDR_xxx.
  3429.  
  3430.     -  Added a paragraph to Section 9 on how to receive and send a
  3431.        Routing header.
  3432.  
  3433.     -  Changed inet6_rthdr_add() and inet6_rthdr_reverse() so that they
  3434.        return -1 upon an error, instead of an Exxx errno value.
  3435.  
  3436.     -  In the description of inet6_rthdr_space() in Section 9.1, added
  3437.        the qualifier "For an IPv6 Type 0 Routing header" to the
  3438.        restriction of between 1 and 23 segments.
  3439.  
  3440.     -  Refer to final function argument in Sections 9.7 and 9.8 as
  3441.        index, not offset.
  3442.  
  3443.     -  Updated Section 14 with new names from Section 2.
  3444.  
  3445.     -  Changed the References from "[n]" to "[RFC-abcd]".
  3446.  
  3447.    Changes from the February 1997 Edition (-01 draft)
  3448.  
  3449.     -  Changed the name of the ip6hdr structure to ip6_hdr (Section 2.1)
  3450.        for consistency with the icmp6hdr structure.  Also changed the
  3451.        name of the ip6hdrctl structure contained within the ip6_hdr
  3452.        structure to ip6_hdrctl (Section 2.1).  Finally, changed the name
  3453.        of the icmp6hdr structure to icmp6_hdr (Section 2.2).  All other
  3454.        occurrences of this structure name, within the Neighbor Discovery
  3455.        structures in Section 2.2.1, already contained the underscore.
  3456.  
  3457.     -  The "struct nd_router_solicit" and "struct nd_router_advert"
  3458.        should both begin with "nd6_". (Section 2.2.2).
  3459.  
  3460.     -  Changed the name of in6_are_addr_equal to IN6_ARE_ADDR_EQUAL
  3461.        (Section 2.3) for consistency with basic API address testing
  3462.        functions.  The header defining this macro is <netinet/in.h>.
  3463.  
  3464.     -  getprotobyname("ipv6") now returns 41, not 0 (Section 2.4).
  3465.  
  3466.     -  The first occurrence of "struct icmpv6_filter" in Section 3.2
  3467.        should be "struct icmp6_filter".
  3468.  
  3469.     -  Changed the name of the CMSG_LENGTH() macro to CMSG_LEN()
  3470.        (Section 4.3.5), since LEN is used throughout the <netinet/*.h>
  3471.  
  3472.  
  3473.  
  3474. Stevens & Thomas             Informational                     [Page 62]
  3475.  
  3476. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3477.  
  3478.  
  3479.        headers.
  3480.  
  3481.     -  Corrected the argument name for the sample implementations of the
  3482.        CMSG_SPACE() and CMSG_LEN() macros to be "length" (Sections 4.3.4
  3483.        and 4.3.5).
  3484.  
  3485.     -  Corrected the socket option mentioned in Section 5.1 to specify
  3486.        the interface for multicasting from IPV6_ADD_MEMBERSHIP to
  3487.        IPV6_MULTICAST_IF.
  3488.  
  3489.     -  There were numerous errors in the previous draft that specified
  3490.        <netinet/ip6.h> that should have been <netinet/in.h>.  These have
  3491.        all been corrected and the locations of all definitions is now
  3492.        summarized in the new Section 14 ("Summary of New Definitions").
  3493.  
  3494.     Changes from the October 1996 Edition (-00 draft)
  3495.  
  3496.     -  Numerous rationale added using the format (Note: ...).
  3497.  
  3498.     -  Added note that not all errors may be defined.
  3499.  
  3500.     -  Added note about ICMPv4, IGMPv4, and ARPv4 terminology.
  3501.  
  3502.     -  Changed the name of <netinet/ip6_icmp.h> to <netinet/icmp6.h>.
  3503.  
  3504.     -  Changed some names in Section 2.2.1: ICMPV6_PKT_TOOBIG to
  3505.        ICMPV6_PACKET_TOOBIG, ICMPV6_TIME_EXCEED to ICMPV6_TIME_EXCEEDED,
  3506.        ICMPV6_ECHORQST to ICMPV6_ECHOREQUEST, ICMPV6_ECHORPLY to
  3507.        ICMPV6_ECHOREPLY, ICMPV6_PARAMPROB_HDR to
  3508.        ICMPV6_PARAMPROB_HEADER, ICMPV6_PARAMPROB_NXT_HDR to
  3509.        ICMPV6_PARAMPROB_NEXTHEADER, and ICMPV6_PARAMPROB_OPTS to
  3510.        ICMPV6_PARAMPROB_OPTION.
  3511.  
  3512.     -  Prepend the prefix "icmp6_" to the three members of the
  3513.        icmp6_dataun union of the icmp6hdr structure (Section 2.2).
  3514.  
  3515.     -  Moved the neighbor discovery definitions into the
  3516.        <netinet/icmp6.h> header, instead of being in their own header
  3517.        (Section 2.2.1).
  3518.  
  3519.     -  Changed Section 2.3 ("Address Testing").  The basic macros are
  3520.        now in the basic API.
  3521.  
  3522.     -  Added the new Section 2.4 on "Protocols File".
  3523.  
  3524.     -  Added note to raw sockets description that something like BPF or
  3525.        DLPI must be used to read or write entire IPv6 packets.
  3526.  
  3527.  
  3528.  
  3529.  
  3530. Stevens & Thomas             Informational                     [Page 63]
  3531.  
  3532. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3533.  
  3534.  
  3535.     -  Corrected example of IPV6_CHECKSUM socket option (Section 3.1).
  3536.        Also defined value of -1 to disable.
  3537.  
  3538.     -  Noted that <netinet/icmp6.h> defines all the ICMPv6 filtering
  3539.        constants, macros, and structures (Section 3.2).
  3540.  
  3541.     -  Added note on magic number 10240 for amount of ancillary data
  3542.        (Section 4.1).
  3543.  
  3544.     -  Added possible padding to picture of ancillary data (Section
  3545.        4.2).
  3546.  
  3547.     -  Defined <sys/socket.h> header for CMSG_xxx() functions (Section
  3548.        4.2).
  3549.  
  3550.     -  Note that the data returned by getsockopt(IPV6_PKTOPTIONS) for a
  3551.        TCP socket is just from the optional headers, if present, of the
  3552.        most recently received segment.  Also note that control
  3553.        information is never returned by recvmsg() for a TCP socket.
  3554.  
  3555.     -  Changed header for struct in6_pktinfo from <netinet.in.h> to
  3556.        <netinet/ip6.h> (Section 5).
  3557.  
  3558.     -  Removed the old Sections 5.1 and 5.2, because the interface
  3559.        identification functions went into the basic API.
  3560.  
  3561.     -  Redid Section 5 to support the hop limit field.
  3562.  
  3563.     -  New Section 5.4 ("Next Hop Address").
  3564.  
  3565.     -  New Section 6 ("Flow Labels").
  3566.  
  3567.     -  Changed all of Sections 7 and 8 dealing with Hop-by-Hop and
  3568.        Destination options.  We now define a set of inet6_option_XXX()
  3569.        functions.
  3570.  
  3571.     -  Changed header for IPV6_SRCRT_xxx constants from <netinet.in.h>
  3572.        to <netinet/ip6.h> (Section 9).
  3573.  
  3574.     -  Add inet6_rthdr_lasthop() function, and fix errors in description
  3575.        of Routing header (Section 9).
  3576.  
  3577.     -  Reworded some of the Routing header descriptions to conform to
  3578.        the terminology in [RFC-1883].
  3579.  
  3580.     -  Added the example from [RFC-1883] for the Routing header (Section
  3581.        9.9).
  3582.  
  3583.  
  3584.  
  3585.  
  3586. Stevens & Thomas             Informational                     [Page 64]
  3587.  
  3588. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3589.  
  3590.  
  3591.     -  Expanded the example in Section 10 to show multiple options per
  3592.        ancillary data object, and to show the receiver's ancillary data
  3593.        objects.
  3594.  
  3595.     -  New Section 11 ("IPv6-Specific Options with IPv4-Mapped IPv6
  3596.        Addresses").
  3597.  
  3598.     -  New Section 12 ("rresvport_af").
  3599.  
  3600.     -  Redid old Section 10 ("Additional Items") into new Section 13
  3601.        ("Future Items").
  3602.  
  3603. 16.  References
  3604.  
  3605.    [RFC-1883]  Deering, S., and R. Hinden, "Internet Protocol, Version 6
  3606.                (IPv6), Specification", RFC 1883, December 1995.
  3607.  
  3608.    [RFC-2133]  Gilligan, R., Thomson, S., Bound, J., and W. Stevens,
  3609.                "Basic Socket Interface Extensions for IPv6", RFC 2133,
  3610.                April 1997.
  3611.  
  3612.    [RFC-1981]  McCann, J., Deering, S., and J. Mogul, "Path MTU
  3613.    Discovery
  3614.                for IP version 6", RFC 1981, August 1996.
  3615.  
  3616.    [RFC-1970]  Narten, T., Nordmark, E., and W. Simpson, "Neighbor
  3617.                Discovery for IP Version 6 (IPv6)", RFC 1970, August
  3618.                1996.
  3619.  
  3620. 17.  Acknowledgments
  3621.  
  3622.    Matt Thomas and Jim Bound have been working on the technical details
  3623.    in this draft for over a year.  Keith Sklower is the original
  3624.    implementor of ancillary data in the BSD networking code.  Craig Metz
  3625.    provided lots of feedback, suggestions, and comments based on his
  3626.    implementing many of these features as the document was being
  3627.    written.
  3628.  
  3629.    The following provided comments on earlier drafts: Pascal Anelli,
  3630.    Hamid Asayesh, Ran Atkinson, Karl Auerbach, Hamid Asayesh, Matt
  3631.    Crawford, Sam T. Denton, Richard Draves, Francis Dupont, Bob
  3632.    Gilligan, Tim Hartrick, Masaki Hirabaru, Yoshinobu Inoue, Mukesh
  3633.    Kacker, A. N. Kuznetsov, Pedro Marques, Jack McCann, der Mouse, John
  3634.    Moy, Thomas Narten, Erik Nordmark, Steve Parker, Charles Perkins, Tom
  3635.    Pusateri, Pedro Roque, Sameer Shah, Peter Sjodin, Stephen P.
  3636.    Spackman, Jinmei Tatuya, Karen Tracey, Quaizar Vohra, Carl Williams,
  3637.    Steve Wise, and Kazu Yamamoto.
  3638.  
  3639.  
  3640.  
  3641.  
  3642. Stevens & Thomas             Informational                     [Page 65]
  3643.  
  3644. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3645.  
  3646.  
  3647. 18.  Authors' Addresses
  3648.  
  3649.    W. Richard Stevens
  3650.    1202 E. Paseo del Zorro
  3651.    Tucson, AZ  85718
  3652.  
  3653.    EMail: rstevens@kohala.com
  3654.  
  3655.  
  3656.    Matt Thomas
  3657.    AltaVista Internet Software
  3658.    LJO2-1/J8
  3659.    30 Porter Rd
  3660.    Littleton, MA  01460
  3661.    EMail: matt.thomas@altavista-software.com
  3662.  
  3663.  
  3664.  
  3665.  
  3666.  
  3667.  
  3668.  
  3669.  
  3670.  
  3671.  
  3672.  
  3673.  
  3674.  
  3675.  
  3676.  
  3677.  
  3678.  
  3679.  
  3680.  
  3681.  
  3682.  
  3683.  
  3684.  
  3685.  
  3686.  
  3687.  
  3688.  
  3689.  
  3690.  
  3691.  
  3692.  
  3693.  
  3694.  
  3695.  
  3696.  
  3697.  
  3698. Stevens & Thomas             Informational                     [Page 66]
  3699.  
  3700. RFC 2292             Advanced Sockets API for IPv6         February 1998
  3701.  
  3702.  
  3703. 19.  Full Copyright Statement
  3704.  
  3705.    Copyright (C) The Internet Society (1998).  All Rights Reserved.
  3706.  
  3707.    This document and translations of it may be copied and furnished to
  3708.    others, and derivative works that comment on or otherwise explain it
  3709.    or assist in its implementation may be prepared, copied, published
  3710.    and distributed, in whole or in part, without restriction of any
  3711.    kind, provided that the above copyright notice and this paragraph are
  3712.    included on all such copies and derivative works.  However, this
  3713.    document itself may not be modified in any way, such as by removing
  3714.    the copyright notice or references to the Internet Society or other
  3715.    Internet organizations, except as needed for the purpose of
  3716.    developing Internet standards in which case the procedures for
  3717.    copyrights defined in the Internet Standards process must be
  3718.    followed, or as required to translate it into languages other than
  3719.    English.
  3720.  
  3721.    The limited permissions granted above are perpetual and will not be
  3722.    revoked by the Internet Society or its successors or assigns.
  3723.  
  3724.    This document and the information contained herein is provided on an
  3725.    "AS IS" basis and THE INTERNET SOCIETY AND THE INTERNET ENGINEERING
  3726.    TASK FORCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
  3727.    BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION
  3728.    HEREIN WILL NOT INFRINGE ANY RIGHTS OR ANY IMPLIED WARRANTIES OF
  3729.    MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  3730.  
  3731.  
  3732.  
  3733.  
  3734.  
  3735.  
  3736.  
  3737.  
  3738.  
  3739.  
  3740.  
  3741.  
  3742.  
  3743.  
  3744.  
  3745.  
  3746.  
  3747.  
  3748.  
  3749.  
  3750.  
  3751.  
  3752.  
  3753.  
  3754. Stevens & Thomas             Informational                     [Page 67]
  3755.  
  3756.