27.5 IPv6 Hop-by-Hop Options and Destination Options
The hop-by-hop and destination options have a similar format, shown in Figure 27.7. The 8-bit next header field identifies the next header that follows this extension header. The 8-bit header extension length is the length of this extension header, in units of 8 bytes, but not including the first 8 bytes. For example, if this extension header occupies 8 bytes, then its header extension length is 0; if this extension header occupies 16 bytes, then its header extension length is 1, and so on. These two headers are padded to be a multiple of 8 bytes with either the pad1 option or the padN option, which will be described shortly.
Figure 27.7. Format of hop-by-hop and destination options.

The hop-by-hop options header and destination options header each hold any number of individual options, which have the format shown in Figure 27.8.
Figure 27.8. Format of individual hop-by-hop and destination options.

This is called TLV coding because each option appears with its type, length, and value. The 8-bit type field identifies the option type. Additionally, the two high-order bits specify what an IPv6 node does with this option if it does not understand the option:
00 | Skip over this option and continue processing the header. |
01 | Discard the packet. |
10 | Discard the packet and send an ICMP parameter problem type 2 error (Figure A.16) to the sender, regardless of whether or not the packet's destination is a multicast address. |
11 | Discard the packet and send an ICMP parameter problem type 2 error (Figure A.16) to the sender. This error is sent only if the packet's destination is not a multicast address. |
0 | The option data does not change en route. |
1 | The option data may change en route. |
Figure 27.9. IPv6 hop-by-hop options.

The pad1 byte is the only option without a length and value. It provides 1 byte of padding. The padN option is used when 2 or more bytes of padding are required. For 2 bytes of padding, the length of this option would be 0 and the option would consist of just the type field and the length field. For 3 bytes of padding, the length would be 1, and 1 byte of 0 would follow this length. The jumbo payload length option provides a datagram length of 32 bits and is used when the 16-bit payload length field in Figure A.2 is inadequate. The router alert option indicates that this packet should be intercepted by certain routers along the path; the value in the router alert option indicates what routers should be interested.We show the padding options because each hop-by-hop and destination option also has an associated alignment requirement , written as x n + y . This means that the option must appear at an integer multiple of x bytes from the start of the header, plus y bytes. For example, the alignment requirement of the jumbo payload option is 4n + 2, and this is to force the 4-byte option value (the jumbo payload length) to be on a 4-byte boundary. The reason why the y value is 2 for this option is because of the 2 bytes that appear at the beginning of each hop-by-hop and destination options header (Figure 27.8). The alignment requirement of the router alert option is 2n + 0, to force the 2-byte option value to be on a 2-byte boundary.The hop-by-hop and destination options are normally specified as ancillary data with sendmsg and returned as ancillary data by recvmsg. Nothing special needs to be done by the application to send either or both of these options; just specify them in a call to sendmsg. To receive these options, the corresponding socket option must be enabled: IPV6_RECVHOPOPTS for the hop-by-hop options and IPV6_RECVDSTOPTS for the destination options. For example, to enable both options to be returned,
const int on = 1;
setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVHOPOPTS, &on, sizeof(on));
setsockopt(sockfd, IPPROTO_IPV6, IPV6_RECVDSTOPTS, &on, sizeof(on));
Figure 27.10 shows the format of the ancillary data objects used to send and receive the hop-by-hop and destination options.
Figure 27.10. Ancillary data objects for hop-by-hop and destination options.

The actual contents of the IPv6 option header is passed between the user and the kernel as the cmsg_data portion of these objects. To reduce code duplication, seven functions are defined to create and process these data sections. The following four functions build an option to send:
#include <netinet/in.h> |
int inet6_opt_init(void *extbuf , socklen_t extlen ) ; |
Returns: number of bytes required to hold empty extension header, -1 on error |
int inet6_opt_append(void *extbuf , socklen_t extlen , int offset , uint8_t type , socklen_t len , uint_t align , void **databufp ) ; |
Returns: updated length of overall extension header after adding option, -1 on error |
int inet6_opt_finish(void *extbuf , socklen_t extlen , int offset ) ; |
Returns: updated length of finished extension header, 1 on error |
int inet6_opt_set_val(void *databuf , int offset , const void *val , socklen_t vallen ) ; |
Returns: new offset inside databuf |
#include <netinet/in.h> |
int inet6_opt_next(const void *extbuf , socklen_t extlen , int offset , uint8_t *typep , socklen_t *lenp , void **databufp ) ; |
Returns: offset of next option, -1 on end of options or error |
int inet6_opt_find(const void *extbuf , socklen_t extlen , int offset , uint8_t type , socklen_t *lenp , void **databufp ) ; |
Returns: offset of next option, -1 on end of options or error |
int inet6_opt_get_val(const void *databuf , int offset , void *val , socklen_t vallen ) ; |
Returns: new offset inside databuf |