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 may be used to set the minimum acceptable TTL a packet must have when
114 received on a socket.
115 All packets with a lower TTL are silently dropped.
116 This option is only really useful when set to 255, preventing packets
117 from outside the directly connected networks reaching local listeners
121 may be used to set the Don't Fragment flag on IP packets.
122 Currently this option is respected only on
131 sockets, the Don't Fragment flag is controlled by the Path
132 MTU Discovery option.
133 Sending a packet larger than the MTU size of the egress interface,
134 determined by the destination address, returns an
140 option is enabled on a
145 call will return the destination
147 address and destination port or a
154 structure points to a buffer
157 structure followed by the
162 fields have the following values:
164 cmsg_len = CMSG_LEN(sizeof(struct in_sockaddr))
165 cmsg_level = IPPROTO_IP
166 cmsg_type = IP_ORIGDSTADDR
171 option is enabled on a
176 call will return the destination
185 structure points to a buffer
188 structure followed by the
193 fields have the following values:
195 cmsg_len = CMSG_LEN(sizeof(struct in_addr))
196 cmsg_level = IPPROTO_IP
197 cmsg_type = IP_RECVDSTADDR
200 The source address to be used for outgoing
202 datagrams on a socket can be specified as ancillary data with a type code of
204 The msg_control field in the msghdr structure should point to a buffer
207 structure followed by the
210 The cmsghdr fields should have the following values:
212 cmsg_len = CMSG_LEN(sizeof(struct in_addr))
213 cmsg_level = IPPROTO_IP
214 cmsg_type = IP_SENDSRCADDR
217 The socket should be either bound to
219 and a local port, and the address supplied with
223 or the socket should be bound to a local address and the address supplied with
227 In the latter case bound address is overriden via generic source address
228 selection logic, which would choose IP address of interface closest to
233 is defined to have the same value as
239 can be used directly as a control message for
245 option is enabled on a
249 socket, the destination address of outgoing
250 broadcast datagrams on that socket will be forced
251 to the undirected broadcast address,
252 .Dv INADDR_BROADCAST ,
254 This is in contrast to the default behavior of the
255 system, which is to transmit undirected broadcasts
256 via the first network interface with the
260 This option allows applications to choose which
261 interface is used to transmit an undirected broadcast
263 For example, the following code would force an
264 undirected broadcast to be transmitted via the interface
265 configured with the broadcast address 192.168.2.255:
268 struct sockaddr_in sin;
269 int onesbcast = 1; /* 0 = disable (default), 1 = enable */
271 setsockopt(s, IPPROTO_IP, IP_ONESBCAST, &onesbcast, sizeof(onesbcast));
272 sin.sin_addr.s_addr = inet_addr("192.168.2.255");
273 sin.sin_port = htons(1234);
274 sendto(s, msg, sizeof(msg), 0, &sin, sizeof(sin));
277 It is the application's responsibility to set the
280 to an appropriate value in order to prevent broadcast storms.
281 The application must have sufficient credentials to set the
283 socket level option, otherwise the
285 option has no effect.
289 option is enabled on a
296 to any address, even one not bound to any available network interface in the
298 This functionality (in conjunction with special firewall rules) can be used for
299 implementing a transparent proxy.
301 .Dv PRIV_NETINET_BINDANY
302 privilege is needed to set this option.
306 option is enabled on a
313 (time to live) field for a
316 The msg_control field in the msghdr structure points to a buffer
317 that contains a cmsghdr structure followed by the
319 The cmsghdr fields have the following values:
321 cmsg_len = CMSG_LEN(sizeof(u_char))
322 cmsg_level = IPPROTO_IP
323 cmsg_type = IP_RECVTTL
329 option is enabled on a
336 (type of service) field for a
339 The msg_control field in the msghdr structure points to a buffer
340 that contains a cmsghdr structure followed by the
342 The cmsghdr fields have the following values:
344 cmsg_len = CMSG_LEN(sizeof(u_char))
345 cmsg_level = IPPROTO_IP
346 cmsg_type = IP_RECVTOS
352 option is enabled on a
357 .Vt "struct sockaddr_dl"
358 corresponding to the interface on which the
364 structure points to a buffer that contains a
366 structure followed by the
367 .Vt "struct sockaddr_dl" .
370 fields have the following values:
372 cmsg_len = CMSG_LEN(sizeof(struct sockaddr_dl))
373 cmsg_level = IPPROTO_IP
374 cmsg_type = IP_RECVIF
378 may be used to set the port range used for selecting a local port number
379 on a socket with an unspecified (zero) port number.
382 .Bl -tag -width IP_PORTRANGE_DEFAULT
383 .It Dv IP_PORTRANGE_DEFAULT
384 use the default range of values, normally
385 .Dv IPPORT_HIFIRSTAUTO
387 .Dv IPPORT_HILASTAUTO .
388 This is adjustable through the sysctl setting:
389 .Va net.inet.ip.portrange.first
391 .Va net.inet.ip.portrange.last .
392 .It Dv IP_PORTRANGE_HIGH
393 use a high 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.hifirst
400 .Va net.inet.ip.portrange.hilast .
401 .It Dv IP_PORTRANGE_LOW
402 use a low range of ports, which are normally restricted to
403 privileged processes on
406 The range is normally from
409 .Li IPPORT_RESERVEDSTART
411 This is adjustable through the sysctl setting:
412 .Va net.inet.ip.portrange.lowfirst
414 .Va net.inet.ip.portrange.lowlast .
417 The range of privileged ports which only may be opened by
418 root-owned processes may be modified by the
419 .Va net.inet.ip.portrange.reservedlow
421 .Va net.inet.ip.portrange.reservedhigh
423 The values default to the traditional range,
427 (0 through 1023), respectively.
428 Note that these settings do not affect and are not accounted for in the
429 use or calculation of the other
430 .Va net.inet.ip.portrange
432 Changing these values departs from
434 tradition and has security
435 consequences that the administrator should carefully evaluate before
436 modifying these settings.
438 Ports are allocated at random within the specified port range in order
439 to increase the difficulty of random spoofing attacks.
440 In scenarios such as benchmarking, this behavior may be undesirable.
442 .Va net.inet.ip.portrange.randomized
443 can be used to toggle randomization off.
445 .Va net.inet.ip.portrange.randomcps
446 ports have been allocated in the last second, then return to sequential
448 Return to random allocation only once the current port allocation rate
450 .Va net.inet.ip.portrange.randomcps
452 .Va net.inet.ip.portrange.randomtime
454 The default values for
455 .Va net.inet.ip.portrange.randomcps
457 .Va net.inet.ip.portrange.randomtime
458 are 10 port allocations per second and 45 seconds correspondingly.
459 .Ss "Multicast Options"
461 multicasting is supported only on
467 and only on networks where the interface
468 driver supports multicasting.
472 option changes the time-to-live (TTL)
473 for outgoing multicast datagrams
474 in order to control the scope of the multicasts:
476 u_char ttl; /* range: 0 to 255, default = 1 */
477 setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl));
480 Datagrams with a TTL of 1 are not forwarded beyond the local network.
481 Multicast datagrams with a TTL of 0 will not be transmitted on any network,
482 but may be delivered locally if the sending host belongs to the destination
483 group and if multicast loopback has not been disabled on the sending socket
485 Multicast datagrams with TTL greater than 1 may be forwarded
486 to other networks if a multicast router is attached to the local network.
488 For hosts with multiple interfaces, where an interface has not
489 been specified for a multicast group membership,
490 each multicast transmission is sent from the primary network interface.
493 option overrides the default for
494 subsequent transmissions from a given socket:
497 setsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr));
500 where "addr" is the local
502 address of the desired interface or
504 to specify the default interface.
506 To specify an interface by index, an instance of
508 may be passed instead.
511 member should be set to the index of the desired interface,
512 or 0 to specify the default interface.
513 The kernel differentiates between these two structures by their size.
518 .Em not recommended ,
519 as multicast memberships are scoped to each
520 individual interface.
521 It is supported for legacy use only by applications,
522 such as routing daemons, which expect to
523 be able to transmit link-local IPv4 multicast datagrams (224.0.0.0/24)
524 on multiple interfaces,
525 without requesting an individual membership for each interface.
528 An interface's local IP address and multicast capability can
534 Normal applications should not need to use this option.
536 If a multicast datagram is sent to a group to which the sending host itself
537 belongs (on the outgoing interface), a copy of the datagram is, by default,
538 looped back by the IP layer for local delivery.
540 .Dv IP_MULTICAST_LOOP
541 option gives the sender explicit control
542 over whether or not subsequent datagrams are looped back:
544 u_char loop; /* 0 = disable, 1 = enable (default) */
545 setsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop));
549 improves performance for applications that may have no more than one
550 instance on a single host (such as a routing daemon), by eliminating
551 the overhead of receiving their own transmissions.
552 It should generally not
553 be used by applications for which there may be more than one instance on a
554 single host (such as a conferencing program) or for which the sender does
555 not belong to the destination group (such as a time querying program).
558 .Va net.inet.ip.mcast.loop
559 controls the default setting of the
560 .Dv IP_MULTICAST_LOOP
561 socket option for new sockets.
563 A multicast datagram sent with an initial TTL greater than 1 may be delivered
564 to the sending host on a different interface from that on which it was sent,
565 if the host belongs to the destination group on that other interface.
566 The loopback control option has no effect on such delivery.
568 A host must become a member of a multicast group before it can receive
569 datagrams sent to the group.
570 To join a multicast group, use the
571 .Dv IP_ADD_MEMBERSHIP
575 setsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq, sizeof(mreq));
580 is the following structure:
583 struct in_addr imr_multiaddr; /* IP multicast address of group */
584 struct in_addr imr_interface; /* local IP address of interface */
591 address of a particular multicast-capable interface if
592 the host is multihomed.
595 to choose the default interface, although this is not recommended;
596 this is considered to be the first interface corresponding
597 to the default route.
598 Otherwise, the first multicast-capable interface
599 configured in the system will be used.
605 member is within the network range
607 it is treated as an interface index in the system interface MIB,
608 as per the RIP Version 2 MIB Extension (RFC-1724).
611 since 7.0, this behavior is no longer supported.
613 instead use the RFC 3678 multicast source filter APIs; in particular,
614 .Dv MCAST_JOIN_GROUP .
617 .Dv IP_MAX_MEMBERSHIPS
618 memberships may be added on a single socket.
619 Membership is associated with a single interface;
620 programs running on multihomed hosts may need to
621 join the same group on more than one interface.
623 To drop a membership, use:
626 setsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq));
631 contains the same values as used to add the membership.
632 Memberships are dropped when the socket is closed or the process exits.
633 .\" TODO: Update this piece when IPv4 source-address selection is implemented.
635 The IGMP protocol uses the primary IP address of the interface
636 as its identifier for group membership.
637 This is the first IP address configured on the interface.
638 If this address is removed or changed, the results are
639 undefined, as the IGMP membership state will then be inconsistent.
640 If multiple IP aliases are configured on the same interface,
641 they will be ignored.
643 This shortcoming was addressed in IPv6; MLDv2 requires
644 that the unique link-local address for an interface is
645 used to identify an MLDv2 listener.
646 .Ss "Source-Specific Multicast Options"
649 the use of Source-Specific Multicast (SSM) is supported.
650 These extensions require an IGMPv3 multicast router in order to
651 make best use of them.
652 If a legacy multicast router is present on the link,
654 will simply downgrade to the version of IGMP spoken by the router,
655 and the benefits of source filtering on the upstream link
656 will not be present, although the kernel will continue to
657 squelch transmissions from blocked sources.
659 Each group membership on a socket now has a filter mode:
660 .Bl -tag -width MCAST_EXCLUDE
662 Datagrams sent to this group are accepted,
663 unless the source is in a list of blocked source addresses.
665 Datagrams sent to this group are accepted
666 only if the source is in a list of accepted source addresses.
669 Groups joined using the legacy
670 .Dv IP_ADD_MEMBERSHIP
671 option are placed in exclusive-mode,
672 and are able to request that certain sources are blocked or allowed.
674 .Em delta-based API .
676 To block a multicast source on an existing group membership:
678 struct ip_mreq_source mreqs;
679 setsockopt(s, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs, sizeof(mreqs));
684 is the following structure:
686 struct ip_mreq_source {
687 struct in_addr imr_multiaddr; /* IP multicast address of group */
688 struct in_addr imr_sourceaddr; /* IP address of source */
689 struct in_addr imr_interface; /* local IP address of interface */
693 should be set to the address of the source to be blocked.
695 To unblock a multicast source on an existing group:
697 struct ip_mreq_source mreqs;
698 setsockopt(s, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs, sizeof(mreqs));
704 .Dv IP_UNBLOCK_SOURCE
707 for inclusive-mode group memberships.
709 To join a multicast group in
711 mode with a single source,
712 or add another source to an existing inclusive-mode membership:
714 struct ip_mreq_source mreqs;
715 setsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
718 To leave a single source from an existing group in inclusive mode:
720 struct ip_mreq_source mreqs;
721 setsockopt(s, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs));
723 If this is the last accepted source for the group, the membership
727 .Dv IP_ADD_SOURCE_MEMBERSHIP
729 .Dv IP_DROP_SOURCE_MEMBERSHIP
732 for exclusive-mode group memberships.
733 However, both exclusive and inclusive mode memberships
734 support the use of the
736 documented in RFC 3678.
737 For management of source filter lists using this API,
742 .Va net.inet.ip.mcast.maxsocksrc
744 .Va net.inet.ip.mcast.maxgrpsrc
745 are used to specify an upper limit on the number of per-socket and per-group
746 source filter entries which the kernel may allocate.
747 .\"-----------------------
751 sockets are connectionless,
752 and are normally used with the
758 call may also be used to fix the destination for future
759 packets (in which case the
767 system calls may be used).
771 is 0, the default protocol
774 packets, and only incoming packets destined for that protocol
778 is non-zero, that protocol number will be used on outgoing packets
779 and to filter incoming packets.
781 Outgoing packets automatically have an
784 them (based on the destination address and the protocol
785 number the socket is created with),
791 releases, incoming packets are received with
793 header and options intact, leaving all fields in network byte order.
796 indicates the complete IP header is included with the data
797 and may be used only with the
801 #include <netinet/in_systm.h>
802 #include <netinet/ip.h>
804 int hincl = 1; /* 1 = on, 0 = off */
805 setsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl));
810 releases, the program must set all
811 the fields of the IP header, including the following:
813 ip->ip_v = IPVERSION;
814 ip->ip_hl = hlen >> 2;
815 ip->ip_id = 0; /* 0 means kernel set appropriate value */
816 ip->ip_off = htons(offset);
817 ip->ip_len = htons(len);
820 The packet should be provided as is to be sent over wire.
821 This implies all fields, including
825 to be in network byte order.
828 for more information on network byte order.
831 field is set to 0 then the kernel will choose an
833 If the header source address is set to
835 the kernel will choose an appropriate address.
837 A socket operation may fail with one of the following errors returned:
840 when trying to establish a connection on a socket which
841 already has one, or when trying to send a datagram with the destination
842 address specified and the socket is already connected;
844 when trying to send a datagram, but
845 no destination address is specified, and the socket has not been
848 when the system runs out of memory for
849 an internal data structure;
850 .It Bq Er EADDRNOTAVAIL
851 when an attempt is made to create a
852 socket with a network address for which no network interface
855 when an attempt is made to create
856 a raw IP socket by a non-privileged process.
859 The following errors specific to
861 may occur when setting or getting
866 An unknown socket option name was given.
868 The IP option field was improperly formed;
869 an option field was shorter than the minimum value
870 or longer than the option buffer provided.
873 The following errors may occur when attempting to send
884 field was not equal to the length of the datagram written to the socket.
901 .%T "Socket Interface Extensions for Multicast Source Filters"
912 structure appeared in
917 packets received on raw IP sockets had the
925 packets received on raw IP sockets had the
929 fields converted to host byte order.
930 Packets written to raw IP sockets were expected to have