1 .\" Copyright (c) 1983, 1991, 1993
2 .\" The Regents of the University of California. All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. Neither the name of the University nor the names of its contributors
13 .\" may be used to endorse or promote products derived from this software
14 .\" without specific prior written permission.
16 .\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
17 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
20 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 .\" @(#)ip.4 8.2 (Berkeley) 11/30/93
42 .Fn socket AF_INET SOCK_RAW proto
45 is the transport layer protocol used
46 by the Internet protocol family.
47 Options may be set at the
50 when using higher-level protocols that are based on
56 It may also be accessed
59 when developing new protocols, or
60 special-purpose applications.
69 may be used to provide
71 options to be transmitted in the
73 header of each outgoing packet
74 or to examine the header options on incoming packets.
76 options may be used with any socket type in the Internet family.
79 options to be sent is that specified by the
81 protocol specification (RFC-791), with one exception:
82 the list of addresses for Source Route options must include the first-hop
83 gateway at the beginning of the list of gateways.
84 The first-hop gateway address will be extracted from the option list
85 and the size adjusted accordingly before use.
86 To disable previously specified options,
87 use a zero-length buffer:
89 setsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0);
95 may be used to set the type-of-service and time-to-live
99 .Dv SOCK_STREAM , SOCK_DGRAM ,
105 int tos = IPTOS_LOWDELAY; /* see <netinet/ip.h> */
106 setsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos));
108 int ttl = 60; /* max = 255 */
109 setsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl));
113 controls IPSec policy for sockets.
116 const char *policy = "in ipsec ah/transport//require";
117 char *buf = ipsec_set_policy(policy, strlen(policy));
118 setsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, buf, ipsec_get_policylen(buf));
122 may be used to set the minimum acceptable TTL a packet must have when
123 received on a socket.
124 All packets with a lower TTL are silently dropped.
125 This option is only really useful when set to 255, preventing packets
126 from outside the directly connected networks reaching local listeners
130 may be used to set the Don't Fragment flag on IP packets.
131 Currently this option is respected only on
140 sockets, the Don't Fragment flag is controlled by the Path
141 MTU Discovery option.
142 Sending a packet larger than the MTU size of the egress interface,
143 determined by the destination address, returns an
149 option is enabled on a
154 call will return the destination
156 address and destination port for a
163 structure points to a buffer
166 structure followed by the
171 fields have the following values:
173 cmsg_len = CMSG_LEN(sizeof(struct sockaddr_in))
174 cmsg_level = IPPROTO_IP
175 cmsg_type = IP_ORIGDSTADDR
180 option is enabled on a
185 call will return the destination
194 structure points to a buffer
197 structure followed by the
202 fields have the following values:
204 cmsg_len = CMSG_LEN(sizeof(struct in_addr))
205 cmsg_level = IPPROTO_IP
206 cmsg_type = IP_RECVDSTADDR
209 The source address to be used for outgoing
211 datagrams on a socket can be specified as ancillary data with a type code of
213 The msg_control field in the msghdr structure should point to a buffer
216 structure followed by the
219 The cmsghdr fields should have the following values:
221 cmsg_len = CMSG_LEN(sizeof(struct in_addr))
222 cmsg_level = IPPROTO_IP
223 cmsg_type = IP_SENDSRCADDR
226 The socket should be either bound to
228 and a local port, and the address supplied with
232 or the socket should be bound to a local address and the address supplied with
236 In the latter case bound address is overridden via generic source address
237 selection logic, which would choose IP address of interface closest to
242 is defined to have the same value as
248 can be used directly as a control message for
254 option is enabled on a
258 socket, the destination address of outgoing
259 broadcast datagrams on that socket will be forced
260 to the undirected broadcast address,
261 .Dv INADDR_BROADCAST ,
263 This is in contrast to the default behavior of the
264 system, which is to transmit undirected broadcasts
265 via the first network interface with the
269 This option allows applications to choose which
270 interface is used to transmit an undirected broadcast
272 For example, the following code would force an
273 undirected broadcast to be transmitted via the interface
274 configured with the broadcast address 192.168.2.255:
277 struct sockaddr_in sin;
278 int onesbcast = 1; /* 0 = disable (default), 1 = enable */
280 setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &onesbcast, sizeof(onesbcast));
281 sin.sin_addr.s_addr = inet_addr("192.168.2.255");
282 sin.sin_port = htons(1234);
283 sendto(s, msg, sizeof(msg), 0, &sin, sizeof(sin));
286 It is the application's responsibility to set the
289 to an appropriate value in order to prevent broadcast storms.
290 The application must have sufficient credentials to set the
292 socket level option, otherwise the
294 option has no effect.
298 option is enabled on a
305 to any address, even one not bound to any available network interface in the
307 This functionality (in conjunction with special firewall rules) can be used for
308 implementing a transparent proxy.
310 .Dv PRIV_NETINET_BINDANY
311 privilege is needed to set this option.
315 option is enabled on a
322 (time to live) field for a
325 The msg_control field in the msghdr structure points to a buffer
326 that contains a cmsghdr structure followed by the
328 The cmsghdr fields have the following values:
330 cmsg_len = CMSG_LEN(sizeof(u_char))
331 cmsg_level = IPPROTO_IP
332 cmsg_type = IP_RECVTTL
338 option is enabled on a
345 (type of service) field for a
348 The msg_control field in the msghdr structure points to a buffer
349 that contains a cmsghdr structure followed by the
351 The cmsghdr fields have the following values:
353 cmsg_len = CMSG_LEN(sizeof(u_char))
354 cmsg_level = IPPROTO_IP
355 cmsg_type = IP_RECVTOS
361 option is enabled on a
366 .Vt "struct sockaddr_dl"
367 corresponding to the interface on which the
373 structure points to a buffer that contains a
375 structure followed by the
376 .Vt "struct sockaddr_dl" .
379 fields have the following values:
381 cmsg_len = CMSG_LEN(sizeof(struct sockaddr_dl))
382 cmsg_level = IPPROTO_IP
383 cmsg_type = IP_RECVIF
387 may be used to set the port range used for selecting a local port number
388 on a socket with an unspecified (zero) port number.
391 .Bl -tag -width IP_PORTRANGE_DEFAULT
392 .It Dv IP_PORTRANGE_DEFAULT
393 use the default range of values, normally
394 .Dv IPPORT_HIFIRSTAUTO
396 .Dv IPPORT_HILASTAUTO .
397 This is adjustable through the sysctl setting:
398 .Va net.inet.ip.portrange.first
400 .Va net.inet.ip.portrange.last .
401 .It Dv IP_PORTRANGE_HIGH
402 use a high range of values, normally
403 .Dv IPPORT_HIFIRSTAUTO
405 .Dv IPPORT_HILASTAUTO .
406 This is adjustable through the sysctl setting:
407 .Va net.inet.ip.portrange.hifirst
409 .Va net.inet.ip.portrange.hilast .
410 .It Dv IP_PORTRANGE_LOW
411 use a low range of ports, which are normally restricted to
412 privileged processes on
415 The range is normally from
418 .Li IPPORT_RESERVEDSTART
420 This is adjustable through the sysctl setting:
421 .Va net.inet.ip.portrange.lowfirst
423 .Va net.inet.ip.portrange.lowlast .
426 The range of privileged ports which only may be opened by
427 root-owned processes may be modified by the
428 .Va net.inet.ip.portrange.reservedlow
430 .Va net.inet.ip.portrange.reservedhigh
432 The values default to the traditional range,
436 (0 through 1023), respectively.
437 Note that these settings do not affect and are not accounted for in the
438 use or calculation of the other
439 .Va net.inet.ip.portrange
441 Changing these values departs from
443 tradition and has security
444 consequences that the administrator should carefully evaluate before
445 modifying these settings.
447 Ports are allocated at random within the specified port range in order
448 to increase the difficulty of random spoofing attacks.
449 In scenarios such as benchmarking, this behavior may be undesirable.
451 .Va net.inet.ip.portrange.randomized
452 can be used to toggle randomization off.
454 .Va net.inet.ip.portrange.randomcps
455 ports have been allocated in the last second, then return to sequential
457 Return to random allocation only once the current port allocation rate
459 .Va net.inet.ip.portrange.randomcps
461 .Va net.inet.ip.portrange.randomtime
463 The default values for
464 .Va net.inet.ip.portrange.randomcps
466 .Va net.inet.ip.portrange.randomtime
467 are 10 port allocations per second and 45 seconds correspondingly.
468 .Ss "Multicast Options"
470 multicasting is supported only on
476 and only on networks where the interface
477 driver supports multicasting.
481 option changes the time-to-live (TTL)
482 for outgoing multicast datagrams
483 in order to control the scope of the multicasts:
485 u_char ttl; /* range: 0 to 255, default = 1 */
486 setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
489 Datagrams with a TTL of 1 are not forwarded beyond the local network.
490 Multicast datagrams with a TTL of 0 will not be transmitted on any network,
491 but may be delivered locally if the sending host belongs to the destination
492 group and if multicast loopback has not been disabled on the sending socket
494 Multicast datagrams with TTL greater than 1 may be forwarded
495 to other networks if a multicast router is attached to the local network.
497 For hosts with multiple interfaces, where an interface has not
498 been specified for a multicast group membership,
499 each multicast transmission is sent from the primary network interface.
502 option overrides the default for
503 subsequent transmissions from a given socket:
506 setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
509 where "addr" is the local
511 address of the desired interface or
513 to specify the default interface.
515 To specify an interface by index, an instance of
517 may be passed instead.
520 member should be set to the index of the desired interface,
521 or 0 to specify the default interface.
522 The kernel differentiates between these two structures by their size.
527 .Em not recommended ,
528 as multicast memberships are scoped to each
529 individual interface.
530 It is supported for legacy use only by applications,
531 such as routing daemons, which expect to
532 be able to transmit link-local IPv4 multicast datagrams (224.0.0.0/24)
533 on multiple interfaces,
534 without requesting an individual membership for each interface.
537 An interface's local IP address and multicast capability can
543 Normal applications should not need to use this option.
545 If a multicast datagram is sent to a group to which the sending host itself
546 belongs (on the outgoing interface), a copy of the datagram is, by default,
547 looped back by the IP layer for local delivery.
549 .Dv IP_MULTICAST_LOOP
550 option gives the sender explicit control
551 over whether or not subsequent datagrams are looped back:
553 u_char loop; /* 0 = disable, 1 = enable (default) */
554 setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
558 improves performance for applications that may have no more than one
559 instance on a single host (such as a routing daemon), by eliminating
560 the overhead of receiving their own transmissions.
561 It should generally not
562 be used by applications for which there may be more than one instance on a
563 single host (such as a conferencing program) or for which the sender does
564 not belong to the destination group (such as a time querying program).
567 .Va net.inet.ip.mcast.loop
568 controls the default setting of the
569 .Dv IP_MULTICAST_LOOP
570 socket option for new sockets.
572 A multicast datagram sent with an initial TTL greater than 1 may be delivered
573 to the sending host on a different interface from that on which it was sent,
574 if the host belongs to the destination group on that other interface.
575 The loopback control option has no effect on such delivery.
577 A host must become a member of a multicast group before it can receive
578 datagrams sent to the group.
579 To join a multicast group, use the
580 .Dv IP_ADD_MEMBERSHIP
583 struct ip_mreqn mreqn;
584 setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreqn, sizeof(mreqn));
589 is the following structure:
592 struct in_addr imr_multiaddr; /* IP multicast address of group */
593 struct in_addr imr_interface; /* local IP address of interface */
594 int imr_ifindex; /* interface index */
599 should be set to the index of a particular multicast-capable interface if
600 the host is multihomed.
603 is non-zero, value of
608 is 0, kernel will use IP address from
610 to lookup the interface.
615 to choose the default interface, although this is not recommended; this is
616 considered to be the first interface corresponding to the default route.
617 Otherwise, the first multicast-capable interface configured in the system
621 .Vt "struct ip_mreq" ,
624 field is also supported by
625 .Dv IP_ADD_MEMBERSHIP
627 In this case kernel would behave as if
631 will be used to lookup interface.
637 member is within the network range
639 it is treated as an interface index in the system interface MIB,
640 as per the RIP Version 2 MIB Extension (RFC-1724).
643 since 7.0, this behavior is no longer supported.
645 instead use the RFC 3678 multicast source filter APIs; in particular,
646 .Dv MCAST_JOIN_GROUP .
649 .Dv IP_MAX_MEMBERSHIPS
650 memberships may be added on a single socket.
651 Membership is associated with a single interface;
652 programs running on multihomed hosts may need to
653 join the same group on more than one interface.
655 To drop a membership, use:
658 setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
663 contains the same values as used to add the membership.
664 Memberships are dropped when the socket is closed or the process exits.
665 .\" TODO: Update this piece when IPv4 source-address selection is implemented.
667 The IGMP protocol uses the primary IP address of the interface
668 as its identifier for group membership.
669 This is the first IP address configured on the interface.
670 If this address is removed or changed, the results are
671 undefined, as the IGMP membership state will then be inconsistent.
672 If multiple IP aliases are configured on the same interface,
673 they will be ignored.
675 This shortcoming was addressed in IPv6; MLDv2 requires
676 that the unique link-local address for an interface is
677 used to identify an MLDv2 listener.
678 .Ss "Source-Specific Multicast Options"
681 the use of Source-Specific Multicast (SSM) is supported.
682 These extensions require an IGMPv3 multicast router in order to
683 make best use of them.
684 If a legacy multicast router is present on the link,
686 will simply downgrade to the version of IGMP spoken by the router,
687 and the benefits of source filtering on the upstream link
688 will not be present, although the kernel will continue to
689 squelch transmissions from blocked sources.
691 Each group membership on a socket now has a filter mode:
692 .Bl -tag -width MCAST_EXCLUDE
694 Datagrams sent to this group are accepted,
695 unless the source is in a list of blocked source addresses.
697 Datagrams sent to this group are accepted
698 only if the source is in a list of accepted source addresses.
701 Groups joined using the legacy
702 .Dv IP_ADD_MEMBERSHIP
703 option are placed in exclusive-mode,
704 and are able to request that certain sources are blocked or allowed.
706 .Em delta-based API .
708 To block a multicast source on an existing group membership:
710 struct ip_mreq_source mreqs;
711 setsockopt(s, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs, sizeof(mreqs));
716 is the following structure:
718 struct ip_mreq_source {
719 struct in_addr imr_multiaddr; /* IP multicast address of group */
720 struct in_addr imr_sourceaddr; /* IP address of source */
721 struct in_addr imr_interface; /* local IP address of interface */
725 should be set to the address of the source to be blocked.
727 To unblock a multicast source on an existing group:
729 struct ip_mreq_source mreqs;
730 setsockopt(s, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs, sizeof(mreqs));
736 .Dv IP_UNBLOCK_SOURCE
739 for inclusive-mode group memberships.
741 To join a multicast group in
743 mode with a single source,
744 or add another source to an existing inclusive-mode membership:
746 struct ip_mreq_source mreqs;
747 setsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
750 To leave a single source from an existing group in inclusive mode:
752 struct ip_mreq_source mreqs;
753 setsockopt(s, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
755 If this is the last accepted source for the group, the membership
759 .Dv IP_ADD_SOURCE_MEMBERSHIP
761 .Dv IP_DROP_SOURCE_MEMBERSHIP
764 for exclusive-mode group memberships.
765 However, both exclusive and inclusive mode memberships
766 support the use of the
768 documented in RFC 3678.
769 For management of source filter lists using this API,
774 .Va net.inet.ip.mcast.maxsocksrc
776 .Va net.inet.ip.mcast.maxgrpsrc
777 are used to specify an upper limit on the number of per-socket and per-group
778 source filter entries which the kernel may allocate.
779 .\"-----------------------
783 sockets are connectionless,
784 and are normally used with the
790 call may also be used to fix the destination for future
791 packets (in which case the
799 system calls may be used).
803 is 0, the default protocol
806 packets, and only incoming packets destined for that protocol
810 is non-zero, that protocol number will be used on outgoing packets
811 and to filter incoming packets.
813 Outgoing packets automatically have an
816 them (based on the destination address and the protocol
817 number the socket is created with),
823 releases, incoming packets are received with
825 header and options intact, leaving all fields in network byte order.
828 indicates the complete IP header is included with the data
829 and may be used only with the
833 #include <netinet/in_systm.h>
834 #include <netinet/ip.h>
836 int hincl = 1; /* 1 = on, 0 = off */
837 setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
842 releases, the program must set all
843 the fields of the IP header, including the following:
845 ip->ip_v = IPVERSION;
846 ip->ip_hl = hlen >> 2;
847 ip->ip_id = 0; /* 0 means kernel set appropriate value */
848 ip->ip_off = htons(offset);
849 ip->ip_len = htons(len);
852 The packet should be provided as is to be sent over wire.
853 This implies all fields, including
857 to be in network byte order.
860 for more information on network byte order.
863 field is set to 0 then the kernel will choose an
865 If the header source address is set to
867 the kernel will choose an appropriate address.
869 A socket operation may fail with one of the following errors returned:
872 when trying to establish a connection on a socket which
873 already has one, or when trying to send a datagram with the destination
874 address specified and the socket is already connected;
876 when trying to send a datagram, but
877 no destination address is specified, and the socket has not been
880 when the system runs out of memory for
881 an internal data structure;
882 .It Bq Er EADDRNOTAVAIL
883 when an attempt is made to create a
884 socket with a network address for which no network interface
887 when an attempt is made to create
888 a raw IP socket by a non-privileged process.
891 The following errors specific to
893 may occur when setting or getting
898 An unknown socket option name was given.
900 The IP option field was improperly formed;
901 an option field was shorter than the minimum value
902 or longer than the option buffer provided.
905 The following errors may occur when attempting to send
916 field was not equal to the length of the datagram written to the socket.
934 .%T "Socket Interface Extensions for Multicast Source Filters"
945 structure appeared in
950 packets received on raw IP sockets had the
958 packets received on raw IP sockets had the
962 fields converted to host byte order.
963 Packets written to raw IP sockets were expected to have