summaryrefslogtreecommitdiff
path: root/static/freebsd/man4/ip6.4 3.html
blob: 5a1f70ec4a35878b310b6e2d53b1e0ac1da75676 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<table class="head">
  <tr>
    <td class="head-ltitle">IP6(4)</td>
    <td class="head-vol">Device Drivers Manual</td>
    <td class="head-rtitle">IP6(4)</td>
  </tr>
</table>
<div class="manual-text">
<section class="Sh">
<h1 class="Sh" id="NAME"><a class="permalink" href="#NAME">NAME</a></h1>
<p class="Pp"><code class="Nm">ip6</code> &#x2014; <span class="Nd">Internet
    Protocol version 6 (IPv6) network layer</span></p>
</section>
<section class="Sh">
<h1 class="Sh" id="SYNOPSIS"><a class="permalink" href="#SYNOPSIS">SYNOPSIS</a></h1>
<p class="Pp"><code class="In">#include
    &lt;<a class="In">sys/socket.h</a>&gt;</code>
  <br/>
  <code class="In">#include &lt;<a class="In">netinet/in.h</a>&gt;</code></p>
<p class="Pp"><var class="Ft">int</var>
  <br/>
  <code class="Fn">socket</code>(<var class="Fa" style="white-space: nowrap;">AF_INET6</var>,
    <var class="Fa" style="white-space: nowrap;">SOCK_RAW</var>,
    <var class="Fa" style="white-space: nowrap;">proto</var>);</p>
</section>
<section class="Sh">
<h1 class="Sh" id="DESCRIPTION"><a class="permalink" href="#DESCRIPTION">DESCRIPTION</a></h1>
<p class="Pp">The IPv6 network layer is used by the IPv6 protocol family for
    transporting data. IPv6 packets contain an IPv6 header that is not provided
    as part of the payload contents when passed to an application. IPv6 header
    options affect the behavior of this protocol and may be used by high-level
    protocols (such as the <a class="Xr">tcp(4)</a> and <a class="Xr">udp(4)</a>
    protocols) as well as directly by &#x201C;raw sockets&#x201D;, which process
    IPv6 messages at a lower-level and may be useful for developing new
    protocols and special-purpose applications.</p>
<section class="Ss">
<h2 class="Ss" id="Header"><a class="permalink" href="#Header">Header</a></h2>
<p class="Pp">All IPv6 packets begin with an IPv6 header. When data received by
    the kernel are passed to the application, this header is not included in
    buffer, even when raw sockets are being used. Likewise, when data are sent
    to the kernel for transmit from the application, the buffer is not examined
    for an IPv6 header: the kernel always constructs the header. To directly
    access IPv6 headers from received packets and specify them as part of the
    buffer passed to the kernel, link-level access (<a class="Xr">bpf(4)</a>,
    for example) must instead be utilized.</p>
<p class="Pp">The header has the following definition:</p>
<div class="Bd Pp Bd-indent Li">
<pre>struct ip6_hdr {
     union {
          struct ip6_hdrctl {
               uint32_t ip6_un1_flow;	/* 20 bits of flow ID */
               uint16_t ip6_un1_plen;	/* payload length */
               uint8_t  ip6_un1_nxt;	/* next header */
               uint8_t  ip6_un1_hlim;	/* hop limit */
          } ip6_un1;
          uint8_t ip6_un2_vfc;	/* version and class */
     } ip6_ctlun;
     struct in6_addr ip6_src;	/* source address */
     struct in6_addr ip6_dst;	/* destination address */
} __packed;

#define ip6_vfc		ip6_ctlun.ip6_un2_vfc
#define ip6_flow	ip6_ctlun.ip6_un1.ip6_un1_flow
#define ip6_plen	ip6_ctlun.ip6_un1.ip6_un1_plen
#define ip6_nxt		ip6_ctlun.ip6_un1.ip6_un1_nxt
#define ip6_hlim	ip6_ctlun.ip6_un1.ip6_un1_hlim
#define ip6_hops	ip6_ctlun.ip6_un1.ip6_un1_hlim</pre>
</div>
<p class="Pp">All fields are in network-byte order. Any options specified (see
    <a class="Sx" href="#Options">Options</a> below) must also be specified in
    network-byte order.</p>
<p class="Pp"><var class="Va">ip6_flow</var> specifies the flow ID.
    <var class="Va">ip6_plen</var> specifies the payload length.
    <var class="Va">ip6_nxt</var> specifies the type of the next header.
    <var class="Va">ip6_hlim</var> specifies the hop limit.</p>
<p class="Pp">The top 4 bits of <var class="Va">ip6_vfc</var> specify the class
    and the bottom 4 bits specify the version.</p>
<p class="Pp"><var class="Va">ip6_src</var> and <var class="Va">ip6_dst</var>
    specify the source and destination addresses.</p>
<p class="Pp">The IPv6 header may be followed by any number of extension headers
    that start with the following generic definition:</p>
<div class="Bd Pp Bd-indent Li">
<pre>struct ip6_ext {
     uint8_t ip6e_nxt;
     uint8_t ip6e_len;
} __packed;</pre>
</div>
</section>
<section class="Ss">
<h2 class="Ss" id="Options"><a class="permalink" href="#Options">Options</a></h2>
<p class="Pp">IPv6 allows header options on packets to manipulate the behavior
    of the protocol. These options and other control requests are accessed with
    the <a class="Xr">getsockopt(2)</a> and <a class="Xr">setsockopt(2)</a>
    system calls at level <code class="Dv">IPPROTO_IPV6</code> and by using
    ancillary data in <a class="Xr">recvmsg(2)</a> and
    <a class="Xr">sendmsg(2)</a>. They can be used to access most of the fields
    in the IPv6 header and extension headers.</p>
<p class="Pp">The following socket options are supported:</p>
<dl class="Bl-tag">
  <dt id="IPV6_UNICAST_HOPS"><a class="permalink" href="#IPV6_UNICAST_HOPS"><code class="Dv">IPV6_UNICAST_HOPS</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the default hop limit header field for outgoing unicast
      datagrams sent on this socket.</dd>
  <dt id="IPV6_MULTICAST_IF"><a class="permalink" href="#IPV6_MULTICAST_IF"><code class="Dv">IPV6_MULTICAST_IF</code></a>
    <var class="Fa">u_int *</var></dt>
  <dd>Get or set the interface from which multicast packets will be sent. For
      hosts with multiple interfaces, each multicast transmission is sent from
      the primary network interface. The interface is specified as its index as
      provided by <a class="Xr">if_nametoindex(3)</a>. A value of zero specifies
      the default interface.</dd>
  <dt id="IPV6_MULTICAST_HOPS"><a class="permalink" href="#IPV6_MULTICAST_HOPS"><code class="Dv">IPV6_MULTICAST_HOPS</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the default hop limit header field for outgoing multicast
      datagrams sent on this socket. This option controls the scope of multicast
      datagram transmissions.
    <p class="Pp">Datagrams with a hop limit of 1 are not forwarded beyond the
        local network. Multicast datagrams with a hop limit of zero will not be
        transmitted on any network but may be delivered locally if the sending
        host belongs to the destination group and if multicast loopback (see
        below) has not been disabled on the sending socket. Multicast datagrams
        with a hop limit greater than 1 may be forwarded to the other networks
        if a multicast router (such as <a class="Xr">mrouted(8)</a>
        (<span class="Pa">ports/net/mrouted</span>)) is attached to the local
        network.</p>
  </dd>
  <dt id="IPV6_MULTICAST_LOOP"><a class="permalink" href="#IPV6_MULTICAST_LOOP"><code class="Dv">IPV6_MULTICAST_LOOP</code></a>
    <var class="Fa">u_int *</var></dt>
  <dd>Get or set the status of whether multicast datagrams will be looped back
      for local delivery when a multicast datagram is sent to a group to which
      the sending host belongs.
    <p class="Pp">This option improves performance for applications that may
        have no more than one instance on a single host (such as a router
        daemon) by eliminating the overhead of receiving their own
        transmissions. It should generally not be used by applications for which
        there may be more than one instance on a single host (such as a
        conferencing program) or for which the sender does not belong to the
        destination group (such as a time-querying program).</p>
    <p class="Pp">A multicast datagram sent with an initial hop limit greater
        than 1 may be delivered to the sending host on a different interface
        from that on which it was sent if the host belongs to the destination
        group on that other interface. The multicast loopback control option has
        no effect on such delivery.</p>
  </dd>
  <dt id="IPV6_JOIN_GROUP"><a class="permalink" href="#IPV6_JOIN_GROUP"><code class="Dv">IPV6_JOIN_GROUP</code></a>
    <var class="Fa">struct ipv6_mreq *</var></dt>
  <dd>Join a multicast group. A host must become a member of a multicast group
      before it can receive datagrams sent to the group.
    <div class="Bd Pp Li">
    <pre>struct ipv6_mreq {
	struct in6_addr	ipv6mr_multiaddr;
	unsigned int	ipv6mr_interface;
};</pre>
    </div>
    <p class="Pp"><var class="Va">ipv6mr_interface</var> may be set to zeroes to
        choose the default multicast interface or to the index of a particular
        multicast-capable interface if the host is multihomed. Membership is
        associated with a single interface; programs running on multihomed hosts
        may need to join the same group on more than one interface.</p>
    <p class="Pp">If the multicast address is unspecified (i.e., all zeroes),
        messages from all multicast addresses will be accepted by this group.
        Note that setting to this value requires superuser privileges.</p>
  </dd>
  <dt id="IPV6_LEAVE_GROUP"><a class="permalink" href="#IPV6_LEAVE_GROUP"><code class="Dv">IPV6_LEAVE_GROUP</code></a>
    <var class="Fa">struct ipv6_mreq *</var></dt>
  <dd>Drop membership from the associated multicast group. Memberships are
      automatically dropped when the socket is closed or when the process
    exits.</dd>
  <dt id="IPV6_ORIGDSTADDR"><a class="permalink" href="#IPV6_ORIGDSTADDR"><code class="Dv">IPV6_ORIGDSTADDR</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether a datagram's original destination address and port are
      returned as ancillary data along with the payload in subsequent
      <a class="Xr">recvmsg(2)</a> calls. The information is stored in the
      ancillary data as a sockaddr_in6 structure.</dd>
  <dt id="IPV6_PORTRANGE"><a class="permalink" href="#IPV6_PORTRANGE"><code class="Dv">IPV6_PORTRANGE</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the allocation policy of ephemeral ports for when the kernel
      automatically binds a local address to this socket. The following values
      are available:
    <p class="Pp"></p>
    <dl class="Bl-tag Bl-compact">
      <dt id="IPV6_PORTRANGE_DEFAULT"><a class="permalink" href="#IPV6_PORTRANGE_DEFAULT"><code class="Dv">IPV6_PORTRANGE_DEFAULT</code></a></dt>
      <dd>Use the regular range of non-reserved ports (varies, see
          <a class="Xr">ip(4)</a>).</dd>
      <dt id="IPV6_PORTRANGE_HIGH"><a class="permalink" href="#IPV6_PORTRANGE_HIGH"><code class="Dv">IPV6_PORTRANGE_HIGH</code></a></dt>
      <dd>Use a high range (varies, see <a class="Xr">ip(4)</a>).</dd>
      <dt id="IPV6_PORTRANGE_LOW"><a class="permalink" href="#IPV6_PORTRANGE_LOW"><code class="Dv">IPV6_PORTRANGE_LOW</code></a></dt>
      <dd>Use a low, reserved range (600-1023, see
        <a class="Xr">ip(4)</a>).</dd>
    </dl>
  </dd>
  <dt id="IPV6_PKTINFO"><a class="permalink" href="#IPV6_PKTINFO"><code class="Dv">IPV6_PKTINFO</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether additional information about subsequent packets will be
      provided as ancillary data along with the payload in subsequent
      <a class="Xr">recvmsg(2)</a> calls. The information is stored in the
      following structure in the ancillary data returned:
    <div class="Bd Pp Li">
    <pre>struct in6_pktinfo {
	struct in6_addr ipi6_addr;    /* src/dst IPv6 address */
	unsigned int    ipi6_ifindex; /* send/recv if index */
};</pre>
    </div>
  </dd>
  <dt id="IPV6_HOPLIMIT"><a class="permalink" href="#IPV6_HOPLIMIT"><code class="Dv">IPV6_HOPLIMIT</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether the hop limit header field from subsequent packets will
      be provided as ancillary data along with the payload in subsequent
      <a class="Xr">recvmsg(2)</a> calls. The value is stored as an
      <var class="Vt">int</var> in the ancillary data returned.</dd>
  <dt id="IPV6_HOPOPTS"><a class="permalink" href="#IPV6_HOPOPTS"><code class="Dv">IPV6_HOPOPTS</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether the hop-by-hop options from subsequent packets will be
      provided as ancillary data along with the payload in subsequent
      <a class="Xr">recvmsg(2)</a> calls. The option is stored in the following
      structure in the ancillary data returned:
    <div class="Bd Pp Li">
    <pre>struct ip6_hbh {
	uint8_t ip6h_nxt;	/* next header */
	uint8_t ip6h_len;	/* length in units of 8 octets */
/* followed by options */
} __packed;</pre>
    </div>
    <p class="Pp" id="inet6_opt_init">The
        <a class="permalink" href="#inet6_opt_init"><code class="Fn">inet6_opt_init</code></a>()
        routine and family of routines may be used to manipulate this data.</p>
    <p class="Pp">This option requires superuser privileges.</p>
  </dd>
  <dt id="IPV6_DSTOPTS"><a class="permalink" href="#IPV6_DSTOPTS"><code class="Dv">IPV6_DSTOPTS</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether the destination options from subsequent packets will be
      provided as ancillary data along with the payload in subsequent
      <a class="Xr">recvmsg(2)</a> calls. The option is stored in the following
      structure in the ancillary data returned:
    <div class="Bd Pp Li">
    <pre>struct ip6_dest {
	uint8_t ip6d_nxt;	/* next header */
	uint8_t ip6d_len;	/* length in units of 8 octets */
/* followed by options */
} __packed;</pre>
    </div>
    <p class="Pp" id="inet6_opt_init~2">The
        <a class="permalink" href="#inet6_opt_init~2"><code class="Fn">inet6_opt_init</code></a>()
        routine and family of routines may be used to manipulate this data.</p>
    <p class="Pp">This option requires superuser privileges.</p>
  </dd>
  <dt id="IPV6_TCLASS"><a class="permalink" href="#IPV6_TCLASS"><code class="Dv">IPV6_TCLASS</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the value of the traffic class field used for outgoing
      datagrams on this socket. The value must be between -1 and 255. A value of
      -1 resets to the default value.</dd>
  <dt id="IPV6_RECVTCLASS"><a class="permalink" href="#IPV6_RECVTCLASS"><code class="Dv">IPV6_RECVTCLASS</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the status of whether the traffic class header field will be
      provided as ancillary data along with the payload in subsequent
      <a class="Xr">recvmsg(2)</a> calls. The header field is stored as a single
      value of type <var class="Vt">int</var>.</dd>
  <dt id="IPV6_RTHDR"><a class="permalink" href="#IPV6_RTHDR"><code class="Dv">IPV6_RTHDR</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether the routing header from subsequent packets will be
      provided as ancillary data along with the payload in subsequent
      <a class="Xr">recvmsg(2)</a> calls. The header is stored in the following
      structure in the ancillary data returned:
    <div class="Bd Pp Li">
    <pre>struct ip6_rthdr {
	uint8_t ip6r_nxt;	/* next header */
	uint8_t ip6r_len;	/* length in units of 8 octets */
	uint8_t ip6r_type;	/* routing type */
	uint8_t ip6r_segleft;	/* segments left */
/* followed by routing-type-specific data */
} __packed;</pre>
    </div>
    <p class="Pp" id="inet6_opt_init~3">The
        <a class="permalink" href="#inet6_opt_init~3"><code class="Fn">inet6_opt_init</code></a>()
        routine and family of routines may be used to manipulate this data.</p>
    <p class="Pp">This option requires superuser privileges.</p>
  </dd>
  <dt id="IPV6_PKTOPTIONS"><a class="permalink" href="#IPV6_PKTOPTIONS"><code class="Dv">IPV6_PKTOPTIONS</code></a>
    <var class="Fa">struct cmsghdr *</var></dt>
  <dd>Get or set all header options and extension headers at one time on the
      last packet sent or received on the socket. All options must fit within
      the size of an mbuf (see <a class="Xr">mbuf(9)</a>). Options are specified
      as a series of <var class="Vt">cmsghdr</var> structures followed by
      corresponding values. <var class="Va">cmsg_level</var> is set to
      <code class="Dv">IPPROTO_IPV6</code>, <var class="Va">cmsg_type</var> to
      one of the other values in this list, and trailing data to the option
      value. When setting options, if the length <var class="Va">optlen</var> to
      <a class="Xr">setsockopt(2)</a> is zero, all header options will be reset
      to their default values. Otherwise, the length should specify the size the
      series of control messages consumes.
    <p class="Pp">Instead of using <a class="Xr">sendmsg(2)</a> to specify
        option values, the ancillary data used in these calls that correspond to
        the desired header options may be directly specified as the control
        message in the series of control messages provided as the argument to
        <a class="Xr">setsockopt(2)</a>.</p>
  </dd>
  <dt id="IPV6_CHECKSUM"><a class="permalink" href="#IPV6_CHECKSUM"><code class="Dv">IPV6_CHECKSUM</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the byte offset into a packet where the 16-bit checksum is
      located. When set, this byte offset is where incoming packets will be
      expected to have checksums of their data stored and where outgoing packets
      will have checksums of their data computed and stored by the kernel. A
      value of -1 specifies that no checksums will be checked on incoming
      packets and that no checksums will be computed or stored on outgoing
      packets. The offset of the checksum for ICMPv6 sockets cannot be relocated
      or turned off.</dd>
  <dt id="IPV6_V6ONLY"><a class="permalink" href="#IPV6_V6ONLY"><code class="Dv">IPV6_V6ONLY</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether only IPv6 connections can be made to this socket. For
      wildcard sockets, this can restrict connections to IPv6 only.</dd>
  <dt id="IPV6_USE_MIN_MTU"><a class="permalink" href="#IPV6_USE_MIN_MTU"><code class="Dv">IPV6_USE_MIN_MTU</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set whether the minimal IPv6 maximum transmission unit (MTU) size
      will be used to avoid fragmentation from occurring for subsequent outgoing
      datagrams.</dd>
  <dt id="IPV6_AUTH_LEVEL"><a class="permalink" href="#IPV6_AUTH_LEVEL"><code class="Dv">IPV6_AUTH_LEVEL</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the <a class="Xr">ipsec(4)</a> authentication level.</dd>
  <dt id="IPV6_ESP_TRANS_LEVEL"><a class="permalink" href="#IPV6_ESP_TRANS_LEVEL"><code class="Dv">IPV6_ESP_TRANS_LEVEL</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the ESP transport level.</dd>
  <dt id="IPV6_ESP_NETWORK_LEVEL"><a class="permalink" href="#IPV6_ESP_NETWORK_LEVEL"><code class="Dv">IPV6_ESP_NETWORK_LEVEL</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the ESP encapsulation level.</dd>
  <dt id="IPV6_IPCOMP_LEVEL"><a class="permalink" href="#IPV6_IPCOMP_LEVEL"><code class="Dv">IPV6_IPCOMP_LEVEL</code></a>
    <var class="Fa">int *</var></dt>
  <dd>Get or set the <a class="Xr">ipcomp(4)</a> level.</dd>
</dl>
<p class="Pp">The <code class="Dv">IPV6_PKTINFO</code>,
    <code class="Dv">IPV6_HOPLIMIT</code>, <code class="Dv">IPV6_HOPOPTS</code>,
    <code class="Dv">IPV6_DSTOPTS</code>, <code class="Dv">IPV6_RTHDR</code>,
    and <code class="Dv">IPV6_ORIGDSTADDR</code> options will return ancillary
    data along with payload contents in subsequent <a class="Xr">recvmsg(2)</a>
    calls with <var class="Va">cmsg_level</var> set to
    <code class="Dv">IPPROTO_IPV6</code> and <var class="Va">cmsg_type</var> set
    to respective option name value (e.g.,
    <code class="Dv">IPV6_HOPTLIMIT</code>). Some of these options may also be
    used directly as ancillary <var class="Va">cmsg_type</var> values in
    <a class="Xr">sendmsg(2)</a> to set options on the packet being transmitted
    by the call. The <var class="Va">cmsg_level</var> value must be
    <code class="Dv">IPPROTO_IPV6</code>. For these options, the ancillary data
    object value format is the same as the value returned as explained for each
    when received with <a class="Xr">recvmsg(2)</a>.</p>
<p class="Pp">Note that using <a class="Xr">sendmsg(2)</a> to specify options on
    particular packets works only on UDP and raw sockets. To manipulate header
    options for packets on TCP sockets, only the socket options may be used.</p>
<p class="Pp">In some cases, there are multiple APIs defined for manipulating an
    IPv6 header field. A good example is the outgoing interface for multicast
    datagrams, which can be set by the <code class="Dv">IPV6_MULTICAST_IF</code>
    socket option, through the <code class="Dv">IPV6_PKTINFO</code> option, and
    through the <var class="Va">sin6_scope_id</var> field of the socket address
    passed to the <a class="Xr">sendto(2)</a> system call.</p>
<p class="Pp">Resolving these conflicts is implementation dependent. This
    implementation determines the value in the following way: options specified
    by using ancillary data (i.e., <a class="Xr">sendmsg(2)</a>) are considered
    first, options specified by using <code class="Dv">IPV6_PKTOPTIONS</code> to
    set &#x201C;sticky&#x201D; options are considered second, options specified
    by using the individual, basic, and direct socket options (e.g.,
    <code class="Dv">IPV6_UNICAST_HOPS</code>) are considered third, and options
    specified in the socket address supplied to <a class="Xr">sendto(2)</a> are
    the last choice.</p>
</section>
<section class="Ss">
<h2 class="Ss" id="Multicasting"><a class="permalink" href="#Multicasting">Multicasting</a></h2>
<p class="Pp">IPv6 multicasting is supported only on
    <code class="Dv">AF_INET6</code> sockets of type
    <code class="Dv">SOCK_DGRAM</code> and <code class="Dv">SOCK_RAW</code>, and
    only on networks where the interface driver supports multicasting. Socket
    options (see above) that manipulate membership of multicast groups and other
    multicast options include <code class="Dv">IPV6_MULTICAST_IF</code>,
    <code class="Dv">IPV6_MULTICAST_HOPS</code>,
    <code class="Dv">IPV6_MULTICAST_LOOP</code>,
    <code class="Dv">IPV6_LEAVE_GROUP</code>, and
    <code class="Dv">IPV6_JOIN_GROUP</code>.</p>
</section>
<section class="Ss">
<h2 class="Ss" id="Raw_Sockets"><a class="permalink" href="#Raw_Sockets">Raw
  Sockets</a></h2>
<p class="Pp">Raw IPv6 sockets are connectionless and are normally used with the
    <a class="Xr">sendto(2)</a> and <a class="Xr">recvfrom(2)</a> calls,
    although the <a class="Xr">connect(2)</a> call may be used to fix the
    destination address for future outgoing packets so that
    <a class="Xr">send(2)</a> may instead be used and the
    <a class="Xr">bind(2)</a> call may be used to fix the source address for
    future outgoing packets instead of having the kernel choose a source
    address.</p>
<p class="Pp">By using <a class="Xr">connect(2)</a> or
    <a class="Xr">bind(2)</a>, raw socket input is constrained to only packets
    with their source address matching the socket destination address if
    <a class="Xr">connect(2)</a> was used and to packets with their destination
    address matching the socket source address if <a class="Xr">bind(2)</a> was
    used.</p>
<p class="Pp" id="not">If the <var class="Ar">proto</var> argument to
    <a class="Xr">socket(2)</a> is zero, the default protocol
    (<code class="Dv">IPPROTO_RAW</code>) is used for outgoing packets. For
    incoming packets, protocols recognized by kernel are
    <a class="permalink" href="#not"><b class="Sy">not</b></a> passed to the
    application socket (e.g., <a class="Xr">tcp(4)</a> and
    <a class="Xr">udp(4)</a>) except for some ICMPv6 messages. The ICMPv6
    messages not passed to raw sockets include echo, timestamp, and address mask
    requests. If <var class="Ar">proto</var> is non-zero, only packets with this
    protocol will be passed to the socket.</p>
<p class="Pp">IPv6 fragments are also not passed to application sockets until
    they have been reassembled. If reception of all packets is desired,
    link-level access (such as <a class="Xr">bpf(4)</a>) must be used
  instead.</p>
<p class="Pp">Outgoing packets automatically have an IPv6 header prepended to
    them (based on the destination address and the protocol number the socket
    was created with). Incoming packets are received by an application without
    the IPv6 header or any extension headers.</p>
<p class="Pp">Outgoing packets will be fragmented automatically by the kernel if
    they are too large. Incoming packets will be reassembled before being sent
    to the raw socket, so packet fragments or fragment headers will never be
    seen on a raw socket.</p>
</section>
</section>
<section class="Sh">
<h1 class="Sh" id="EXAMPLES"><a class="permalink" href="#EXAMPLES">EXAMPLES</a></h1>
<p class="Pp">The following determines the hop limit on the next packet
    received:</p>
<div class="Bd Pp Li">
<pre>struct iovec iov[2];
u_char buf[BUFSIZ];
struct cmsghdr *cm;
struct msghdr m;
int optval;
bool found;
u_char data[2048];

/* Create socket. */

(void)memset(&amp;m, 0, sizeof(m));
(void)memset(&amp;iov, 0, sizeof(iov));

iov[0].iov_base = data;		/* buffer for packet payload */
iov[0].iov_len = sizeof(data);	/* expected packet length */

m.msg_name = &amp;from;		/* sockaddr_in6 of peer */
m.msg_namelen = sizeof(from);
m.msg_iov = iov;
m.msg_iovlen = 1;
m.msg_control = (caddr_t)buf;	/* buffer for control messages */
m.msg_controllen = sizeof(buf);

/*
 * Enable the hop limit value from received packets to be
 * returned along with the payload.
 */
optval = 1;
if (setsockopt(s, IPPROTO_IPV6, IPV6_HOPLIMIT, &amp;optval,
    sizeof(optval)) == -1)
	err(1, &quot;setsockopt&quot;);

found = false;
do {
	if (recvmsg(s, &amp;m, 0) == -1)
		err(1, &quot;recvmsg&quot;);
	for (cm = CMSG_FIRSTHDR(&amp;m); cm != NULL;
	     cm = CMSG_NXTHDR(&amp;m, cm)) {
		if (cm-&gt;cmsg_level == IPPROTO_IPV6 &amp;&amp;
		    cm-&gt;cmsg_type == IPV6_HOPLIMIT &amp;&amp;
		    cm-&gt;cmsg_len == CMSG_LEN(sizeof(int))) {
			found = true;
			(void)printf(&quot;hop limit: %d\n&quot;,
			    *(int *)CMSG_DATA(cm));
			break;
		}
	}
} while (!found);</pre>
</div>
</section>
<section class="Sh">
<h1 class="Sh" id="DIAGNOSTICS"><a class="permalink" href="#DIAGNOSTICS">DIAGNOSTICS</a></h1>
<p class="Pp">A socket operation may fail with one of the following errors
    returned:</p>
<dl class="Bl-tag">
  <dt>[<code class="Er">EISCONN</code>]</dt>
  <dd>when trying to establish a connection on a socket which already has one or
      when trying to send a datagram with the destination address specified and
      the socket is already connected.</dd>
  <dt>[<code class="Er">ENOTCONN</code>]</dt>
  <dd>when trying to send a datagram, but no destination address is specified,
      and the socket has not been connected.</dd>
  <dt>[<code class="Er">ENOBUFS</code>]</dt>
  <dd>when the system runs out of memory for an internal data structure.</dd>
  <dt>[<code class="Er">EADDRNOTAVAIL</code>]</dt>
  <dd>when an attempt is made to create a socket with a network address for
      which no network interface exists.</dd>
  <dt>[<code class="Er">EACCES</code>]</dt>
  <dd>when an attempt is made to create a raw IPv6 socket by a non-privileged
      process.</dd>
</dl>
<p class="Pp">The following errors specific to IPv6 may occur when setting or
    getting header options:</p>
<dl class="Bl-tag">
  <dt>[<code class="Er">EINVAL</code>]</dt>
  <dd>An unknown socket option name was given.</dd>
  <dt>[<code class="Er">EINVAL</code>]</dt>
  <dd>An ancillary data object was improperly formed.</dd>
</dl>
</section>
<section class="Sh">
<h1 class="Sh" id="SEE_ALSO"><a class="permalink" href="#SEE_ALSO">SEE
  ALSO</a></h1>
<p class="Pp"><a class="Xr">getsockopt(2)</a>, <a class="Xr">recv(2)</a>,
    <a class="Xr">send(2)</a>, <a class="Xr">setsockopt(2)</a>,
    <a class="Xr">socket(2)</a>, <a class="Xr">CMSG_DATA(3)</a>,
    <a class="Xr">if_nametoindex(3)</a>, <a class="Xr">inet6_opt_init(3)</a>,
    <a class="Xr">bpf(4)</a>, <a class="Xr">icmp6(4)</a>,
    <a class="Xr">inet6(4)</a>, <a class="Xr">ip(4)</a>,
    <a class="Xr">netintro(4)</a>, <a class="Xr">tcp(4)</a>,
    <a class="Xr">udp(4)</a></p>
<p class="Pp"><cite class="Rs"><span class="RsA">W. Stevens</span> and
    <span class="RsA">M. Thomas</span>, <span class="RsT">Advanced Sockets API
    for IPv6</span>, <span class="RsR">RFC 2292</span>,
    <span class="RsD">February 1998</span>.</cite></p>
<p class="Pp"><cite class="Rs"><span class="RsA">S. Deering</span> and
    <span class="RsA">R. Hinden</span>, <span class="RsT">Internet Protocol,
    Version 6 (IPv6) Specification</span>, <span class="RsR">RFC 2460</span>,
    <span class="RsD">December 1998</span>.</cite></p>
<p class="Pp"><cite class="Rs"><span class="RsA">R. Gilligan</span>,
    <span class="RsA">S. Thomson</span>, <span class="RsA">J. Bound</span>, and
    <span class="RsA">W. Stevens</span>, <span class="RsT">Basic Socket
    Interface Extensions for IPv6</span>, <span class="RsR">RFC 2553</span>,
    <span class="RsD">March 1999</span>.</cite></p>
<p class="Pp"><cite class="Rs"><span class="RsA">R. Gilligan</span>,
    <span class="RsA">S. Thomson</span>, <span class="RsA">J. Bound</span>,
    <span class="RsA">J. McCann</span>, and <span class="RsA">W. Stevens</span>,
    <span class="RsT">Basic Socket Interface Extensions for IPv6</span>,
    <span class="RsR">RFC 3493</span>, <span class="RsD">February
    2003</span>.</cite></p>
<p class="Pp"><cite class="Rs"><span class="RsA">W. Stevens</span>,
    <span class="RsA">M. Thomas</span>, <span class="RsA">E. Nordmark</span>,
    and <span class="RsA">T. Jinmei</span>, <span class="RsT">Advanced Sockets
    Application Program Interface (API) for IPv6</span>, <span class="RsR">RFC
    3542</span>, <span class="RsD">May 2003</span>.</cite></p>
<p class="Pp"><cite class="Rs"><span class="RsA">S. Deering</span> and
    <span class="RsA">R. Hinden</span>, <span class="RsT">Internet Protocol,
    Version 6 (IPv6) Specification</span>, <span class="RsR">RFC 8200</span>,
    <span class="RsD">July 2017</span>.</cite></p>
<p class="Pp"><cite class="Rs"><span class="RsA">W. Stevens</span>,
    <span class="RsA">B. Fenner</span>, and <span class="RsA">A. Rudoff</span>,
    <span class="RsT">UNIX Network Programming, 3rd Edition</span>,
    <i class="RsI">Addison-Wesley Professional</i>, <span class="RsD">November
    2003</span>.</cite></p>
</section>
<section class="Sh">
<h1 class="Sh" id="STANDARDS"><a class="permalink" href="#STANDARDS">STANDARDS</a></h1>
<p class="Pp">Most of the socket options are defined in RFC 2292 / 3542 or RFC
    2553 / 3493. The <code class="Dv">IPV6_PORTRANGE</code> socket option and
    the conflict resolution rule are not defined in the RFCs and should be
    considered implementation dependent.</p>
</section>
</div>
<table class="foot">
  <tr>
    <td class="foot-date">July 24, 2022</td>
    <td class="foot-os">FreeBSD 15.0</td>
  </tr>
</table>