1afe61c15SRodney W. Grimes.\" Copyright (c) 1983, 1991, 1993 2afe61c15SRodney W. Grimes.\" The Regents of the University of California. All rights reserved. 3afe61c15SRodney W. Grimes.\" 4afe61c15SRodney W. Grimes.\" Redistribution and use in source and binary forms, with or without 5afe61c15SRodney W. Grimes.\" modification, are permitted provided that the following conditions 6afe61c15SRodney W. Grimes.\" are met: 7afe61c15SRodney W. Grimes.\" 1. Redistributions of source code must retain the above copyright 8afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer. 9afe61c15SRodney W. Grimes.\" 2. Redistributions in binary form must reproduce the above copyright 10afe61c15SRodney W. Grimes.\" notice, this list of conditions and the following disclaimer in the 11afe61c15SRodney W. Grimes.\" documentation and/or other materials provided with the distribution. 12dda5b397SEitan Adler.\" 3. Neither the name of the University nor the names of its contributors 13afe61c15SRodney W. Grimes.\" may be used to endorse or promote products derived from this software 14afe61c15SRodney W. Grimes.\" without specific prior written permission. 15afe61c15SRodney W. Grimes.\" 16afe61c15SRodney W. Grimes.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17afe61c15SRodney W. Grimes.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18afe61c15SRodney W. Grimes.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19afe61c15SRodney W. Grimes.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20afe61c15SRodney W. Grimes.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21afe61c15SRodney W. Grimes.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22afe61c15SRodney W. Grimes.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23afe61c15SRodney W. Grimes.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24afe61c15SRodney W. Grimes.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25afe61c15SRodney W. Grimes.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26afe61c15SRodney W. Grimes.\" SUCH DAMAGE. 27afe61c15SRodney W. Grimes.\" 284a5a67feSKonstantin Belousov.Dd August 9, 2021 29afe61c15SRodney W. Grimes.Dt IP 4 308ce3f3ddSRuslan Ermilov.Os 31afe61c15SRodney W. Grimes.Sh NAME 32afe61c15SRodney W. Grimes.Nm ip 33afe61c15SRodney W. Grimes.Nd Internet Protocol 34afe61c15SRodney W. Grimes.Sh SYNOPSIS 3532eef9aeSRuslan Ermilov.In sys/types.h 3632eef9aeSRuslan Ermilov.In sys/socket.h 3732eef9aeSRuslan Ermilov.In netinet/in.h 38afe61c15SRodney W. Grimes.Ft int 39afe61c15SRodney W. Grimes.Fn socket AF_INET SOCK_RAW proto 40afe61c15SRodney W. Grimes.Sh DESCRIPTION 41afe61c15SRodney W. Grimes.Tn IP 42afe61c15SRodney W. Grimesis the transport layer protocol used 43afe61c15SRodney W. Grimesby the Internet protocol family. 44afe61c15SRodney W. GrimesOptions may be set at the 45afe61c15SRodney W. Grimes.Tn IP 46afe61c15SRodney W. Grimeslevel 47afe61c15SRodney W. Grimeswhen using higher-level protocols that are based on 48afe61c15SRodney W. Grimes.Tn IP 49afe61c15SRodney W. Grimes(such as 50afe61c15SRodney W. Grimes.Tn TCP 51afe61c15SRodney W. Grimesand 52afe61c15SRodney W. Grimes.Tn UDP ) . 53afe61c15SRodney W. GrimesIt may also be accessed 54afe61c15SRodney W. Grimesthrough a 55afe61c15SRodney W. Grimes.Dq raw socket 56afe61c15SRodney W. Grimeswhen developing new protocols, or 57afe61c15SRodney W. Grimesspecial-purpose applications. 58afe61c15SRodney W. Grimes.Pp 59afe61c15SRodney W. GrimesThere are several 60afe61c15SRodney W. Grimes.Tn IP-level 61edf0e5b3SMike Pritchard.Xr setsockopt 2 62edf0e5b3SMike Pritchardand 63afe61c15SRodney W. Grimes.Xr getsockopt 2 64afe61c15SRodney W. Grimesoptions. 65afe61c15SRodney W. Grimes.Dv IP_OPTIONS 66afe61c15SRodney W. Grimesmay be used to provide 67afe61c15SRodney W. Grimes.Tn IP 68afe61c15SRodney W. Grimesoptions to be transmitted in the 69afe61c15SRodney W. Grimes.Tn IP 70afe61c15SRodney W. Grimesheader of each outgoing packet 71afe61c15SRodney W. Grimesor to examine the header options on incoming packets. 72afe61c15SRodney W. Grimes.Tn IP 73afe61c15SRodney W. Grimesoptions may be used with any socket type in the Internet family. 74afe61c15SRodney W. GrimesThe format of 75afe61c15SRodney W. Grimes.Tn IP 76afe61c15SRodney W. Grimesoptions to be sent is that specified by the 77afe61c15SRodney W. Grimes.Tn IP 78afe61c15SRodney W. Grimesprotocol specification (RFC-791), with one exception: 79afe61c15SRodney W. Grimesthe list of addresses for Source Route options must include the first-hop 80afe61c15SRodney W. Grimesgateway at the beginning of the list of gateways. 81afe61c15SRodney W. GrimesThe first-hop gateway address will be extracted from the option list 82afe61c15SRodney W. Grimesand the size adjusted accordingly before use. 83afe61c15SRodney W. GrimesTo disable previously specified options, 84afe61c15SRodney W. Grimesuse a zero-length buffer: 85afe61c15SRodney W. Grimes.Bd -literal 86afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_OPTIONS, NULL, 0); 87afe61c15SRodney W. Grimes.Ed 88afe61c15SRodney W. Grimes.Pp 89afe61c15SRodney W. Grimes.Dv IP_TOS 903b0ee680SRichard Scheffeneggermay be used to set the differential service codepoint (DSCP) and the 91*ffc4f93eSBenedict Reuschlingexplicit congestion notification (ECN) codepoint. 923b0ee680SRichard ScheffeneggerSetting the ECN codepoint - the two least significant bits - on a 933b0ee680SRichard Scheffeneggersocket using a transport protocol implementing ECN has no effect. 943b0ee680SRichard Scheffenegger.Pp 95afe61c15SRodney W. Grimes.Dv IP_TTL 963b0ee680SRichard Scheffeneggerconfigures the time-to-live (TTL) field in the 97afe61c15SRodney W. Grimes.Tn IP 98afe61c15SRodney W. Grimesheader for 998ce3f3ddSRuslan Ermilov.Dv SOCK_STREAM , SOCK_DGRAM , 1008ce3f3ddSRuslan Ermilovand certain types of 1018ce3f3ddSRuslan Ermilov.Dv SOCK_RAW 1026d249eeeSSheldon Hearnsockets. 1036d249eeeSSheldon HearnFor example, 104afe61c15SRodney W. Grimes.Bd -literal 1053b0ee680SRichard Scheffeneggerint tos = IPTOS_DSCP_EF; /* see <netinet/ip.h> */ 106afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_TOS, &tos, sizeof(tos)); 107afe61c15SRodney W. Grimes 108afe61c15SRodney W. Grimesint ttl = 60; /* max = 255 */ 109afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_TTL, &ttl, sizeof(ttl)); 110afe61c15SRodney W. Grimes.Ed 111afe61c15SRodney W. Grimes.Pp 1124a5a67feSKonstantin Belousov.Dv IP_IPSEC_POLICY 1134a5a67feSKonstantin Belousovcontrols IPSec policy for sockets. 1144a5a67feSKonstantin BelousovFor example, 1154a5a67feSKonstantin Belousov.Bd -literal 1164a5a67feSKonstantin Belousovconst char *policy = "in ipsec ah/transport//require"; 1174a5a67feSKonstantin Belousovchar *buf = ipsec_set_policy(policy, strlen(policy)); 1184a5a67feSKonstantin Belousovsetsockopt(s, IPPROTO_IP, IP_IPSEC_POLICY, buf, ipsec_get_policylen(buf)); 1194a5a67feSKonstantin Belousov.Ed 1204a5a67feSKonstantin Belousov.Pp 1215090c754SAndre Oppermann.Dv IP_MINTTL 1225090c754SAndre Oppermannmay be used to set the minimum acceptable TTL a packet must have when 1235090c754SAndre Oppermannreceived on a socket. 1245090c754SAndre OppermannAll packets with a lower TTL are silently dropped. 12578ad5421SRuslan ErmilovThis option is only really useful when set to 255, preventing packets 1265090c754SAndre Oppermannfrom outside the directly connected networks reaching local listeners 1275090c754SAndre Oppermannon sockets. 1285090c754SAndre Oppermann.Pp 129c9ebaafaSAndre Oppermann.Dv IP_DONTFRAG 130c9ebaafaSAndre Oppermannmay be used to set the Don't Fragment flag on IP packets. 131c9ebaafaSAndre OppermannCurrently this option is respected only on 132c9ebaafaSAndre Oppermann.Xr udp 4 13378ad5421SRuslan Ermilovand raw 13416e36754SEdward Tomasz Napierala.Nm 13578ad5421SRuslan Ermilovsockets, unless the 13678ad5421SRuslan Ermilov.Dv IP_HDRINCL 13778ad5421SRuslan Ermilovoption has been set. 138c9ebaafaSAndre OppermannOn 139c9ebaafaSAndre Oppermann.Xr tcp 4 14078ad5421SRuslan Ermilovsockets, the Don't Fragment flag is controlled by the Path 141c9ebaafaSAndre OppermannMTU Discovery option. 142c9ebaafaSAndre OppermannSending a packet larger than the MTU size of the egress interface, 14378ad5421SRuslan Ermilovdetermined by the destination address, returns an 14478ad5421SRuslan Ermilov.Er EMSGSIZE 14578ad5421SRuslan Ermiloverror. 146c9ebaafaSAndre Oppermann.Pp 147afe61c15SRodney W. GrimesIf the 148dce33a45SErmal Luçi.Dv IP_ORIGDSTADDR 149dce33a45SErmal Luçioption is enabled on a 150dce33a45SErmal Luçi.Dv SOCK_DGRAM 151dce33a45SErmal Luçisocket, 152dce33a45SErmal Luçithe 153dce33a45SErmal Luçi.Xr recvmsg 2 154dce33a45SErmal Luçicall will return the destination 155dce33a45SErmal Luçi.Tn IP 156b5aac3eaSAlan Somersaddress and destination port for a 157dce33a45SErmal Luçi.Tn UDP 158dce33a45SErmal Luçidatagram. 159dce33a45SErmal LuçiThe 160dce33a45SErmal Luçi.Vt msg_control 161dce33a45SErmal Luçifield in the 162dce33a45SErmal Luçi.Vt msghdr 163dce33a45SErmal Luçistructure points to a buffer 164dce33a45SErmal Luçithat contains a 165dce33a45SErmal Luçi.Vt cmsghdr 166dce33a45SErmal Luçistructure followed by the 167b5aac3eaSAlan Somers.Tn sockaddr_in 168b5aac3eaSAlan Somersstructure. 169dce33a45SErmal LuçiThe 170dce33a45SErmal Luçi.Vt cmsghdr 171dce33a45SErmal Luçifields have the following values: 172dce33a45SErmal Luçi.Bd -literal 173b5aac3eaSAlan Somerscmsg_len = CMSG_LEN(sizeof(struct sockaddr_in)) 174dce33a45SErmal Luçicmsg_level = IPPROTO_IP 175dce33a45SErmal Luçicmsg_type = IP_ORIGDSTADDR 176dce33a45SErmal Luçi.Ed 177dce33a45SErmal Luçi.Pp 178dce33a45SErmal LuçiIf the 179afe61c15SRodney W. Grimes.Dv IP_RECVDSTADDR 180afe61c15SRodney W. Grimesoption is enabled on a 181afe61c15SRodney W. Grimes.Dv SOCK_DGRAM 182afe61c15SRodney W. Grimessocket, 183afe61c15SRodney W. Grimesthe 184edf0e5b3SMike Pritchard.Xr recvmsg 2 185afe61c15SRodney W. Grimescall will return the destination 186afe61c15SRodney W. Grimes.Tn IP 187afe61c15SRodney W. Grimesaddress for a 188afe61c15SRodney W. Grimes.Tn UDP 189afe61c15SRodney W. Grimesdatagram. 1908d22ff75SRuslan ErmilovThe 1918d22ff75SRuslan Ermilov.Vt msg_control 1928d22ff75SRuslan Ermilovfield in the 1938d22ff75SRuslan Ermilov.Vt msghdr 1948d22ff75SRuslan Ermilovstructure points to a buffer 1958d22ff75SRuslan Ermilovthat contains a 1968d22ff75SRuslan Ermilov.Vt cmsghdr 1978d22ff75SRuslan Ermilovstructure followed by the 198afe61c15SRodney W. Grimes.Tn IP 199afe61c15SRodney W. Grimesaddress. 2008d22ff75SRuslan ErmilovThe 2018d22ff75SRuslan Ermilov.Vt cmsghdr 2028d22ff75SRuslan Ermilovfields have the following values: 203afe61c15SRodney W. Grimes.Bd -literal 20430852968SMichael Tuexencmsg_len = CMSG_LEN(sizeof(struct in_addr)) 205afe61c15SRodney W. Grimescmsg_level = IPPROTO_IP 206afe61c15SRodney W. Grimescmsg_type = IP_RECVDSTADDR 207afe61c15SRodney W. Grimes.Ed 2087ee32b9fSPeter Wemm.Pp 209c557ae16SIan DowseThe source address to be used for outgoing 210c557ae16SIan Dowse.Tn UDP 211f99a2a0cSGleb Smirnoffdatagrams on a socket can be specified as ancillary data with a type code of 212c557ae16SIan Dowse.Dv IP_SENDSRCADDR . 213c557ae16SIan DowseThe msg_control field in the msghdr structure should point to a buffer 2148d22ff75SRuslan Ermilovthat contains a 2158d22ff75SRuslan Ermilov.Vt cmsghdr 2168d22ff75SRuslan Ermilovstructure followed by the 217c557ae16SIan Dowse.Tn IP 218c557ae16SIan Dowseaddress. 219c557ae16SIan DowseThe cmsghdr fields should have the following values: 220c557ae16SIan Dowse.Bd -literal 22130852968SMichael Tuexencmsg_len = CMSG_LEN(sizeof(struct in_addr)) 222c557ae16SIan Dowsecmsg_level = IPPROTO_IP 223c557ae16SIan Dowsecmsg_type = IP_SENDSRCADDR 224c557ae16SIan Dowse.Ed 225c557ae16SIan Dowse.Pp 226e8574be3SGleb SmirnoffThe socket should be either bound to 227e8574be3SGleb Smirnoff.Dv INADDR_ANY 228e8574be3SGleb Smirnoffand a local port, and the address supplied with 229f99a2a0cSGleb Smirnoff.Dv IP_SENDSRCADDR 230*ffc4f93eSBenedict Reuschlingshouldn't be 231f99a2a0cSGleb Smirnoff.Dv INADDR_ANY , 232e8574be3SGleb Smirnoffor the socket should be bound to a local address and the address supplied with 233f99a2a0cSGleb Smirnoff.Dv IP_SENDSRCADDR 234e8574be3SGleb Smirnoffshould be 235f99a2a0cSGleb Smirnoff.Dv INADDR_ANY . 2366b35d82cSEitan AdlerIn the latter case bound address is overridden via generic source address 237e8574be3SGleb Smirnoffselection logic, which would choose IP address of interface closest to 238e8574be3SGleb Smirnoffdestination. 239f99a2a0cSGleb Smirnoff.Pp 240c557ae16SIan DowseFor convenience, 241c557ae16SIan Dowse.Dv IP_SENDSRCADDR 242c557ae16SIan Dowseis defined to have the same value as 243c557ae16SIan Dowse.Dv IP_RECVDSTADDR , 244c557ae16SIan Dowseso the 245c557ae16SIan Dowse.Dv IP_RECVDSTADDR 246c557ae16SIan Dowsecontrol message from 247c557ae16SIan Dowse.Xr recvmsg 2 248c557ae16SIan Dowsecan be used directly as a control message for 249c557ae16SIan Dowse.Xr sendmsg 2 . 25004bcf5a9SBruce M Simpson.\" 251c557ae16SIan Dowse.Pp 2523b5f87e6SMatthew N. DoddIf the 2538afa2304SBruce M Simpson.Dv IP_ONESBCAST 2548afa2304SBruce M Simpsonoption is enabled on a 2558afa2304SBruce M Simpson.Dv SOCK_DGRAM 2568afa2304SBruce M Simpsonor a 2578afa2304SBruce M Simpson.Dv SOCK_RAW 2588afa2304SBruce M Simpsonsocket, the destination address of outgoing 2598afa2304SBruce M Simpsonbroadcast datagrams on that socket will be forced 2608afa2304SBruce M Simpsonto the undirected broadcast address, 2618afa2304SBruce M Simpson.Dv INADDR_BROADCAST , 2628afa2304SBruce M Simpsonbefore transmission. 2638afa2304SBruce M SimpsonThis is in contrast to the default behavior of the 2648afa2304SBruce M Simpsonsystem, which is to transmit undirected broadcasts 2658afa2304SBruce M Simpsonvia the first network interface with the 26650d922a0SGlen Barber.Dv IFF_BROADCAST 26750d922a0SGlen Barberflag set. 2688afa2304SBruce M Simpson.Pp 2698afa2304SBruce M SimpsonThis option allows applications to choose which 2708afa2304SBruce M Simpsoninterface is used to transmit an undirected broadcast 2718afa2304SBruce M Simpsondatagram. 2728afa2304SBruce M SimpsonFor example, the following code would force an 2738afa2304SBruce M Simpsonundirected broadcast to be transmitted via the interface 2748afa2304SBruce M Simpsonconfigured with the broadcast address 192.168.2.255: 2758afa2304SBruce M Simpson.Bd -literal 2768afa2304SBruce M Simpsonchar msg[512]; 2778afa2304SBruce M Simpsonstruct sockaddr_in sin; 27867a3f4b2SRuslan Ermilovint onesbcast = 1; /* 0 = disable (default), 1 = enable */ 2798afa2304SBruce M Simpson 2808afa2304SBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_ONESBCAST, &onesbcast, sizeof(onesbcast)); 2818afa2304SBruce M Simpsonsin.sin_addr.s_addr = inet_addr("192.168.2.255"); 2828afa2304SBruce M Simpsonsin.sin_port = htons(1234); 2838afa2304SBruce M Simpsonsendto(s, msg, sizeof(msg), 0, &sin, sizeof(sin)); 2848afa2304SBruce M Simpson.Ed 2858afa2304SBruce M Simpson.Pp 2868afa2304SBruce M SimpsonIt is the application's responsibility to set the 28750d922a0SGlen Barber.Dv IP_TTL 28850d922a0SGlen Barberoption 2898afa2304SBruce M Simpsonto an appropriate value in order to prevent broadcast storms. 2908afa2304SBruce M SimpsonThe application must have sufficient credentials to set the 2918afa2304SBruce M Simpson.Dv SO_BROADCAST 2928afa2304SBruce M Simpsonsocket level option, otherwise the 29350d922a0SGlen Barber.Dv IP_ONESBCAST 29450d922a0SGlen Barberoption has no effect. 2958afa2304SBruce M Simpson.Pp 2968afa2304SBruce M SimpsonIf the 29779d6b3f3SPawel Jakub Dawidek.Dv IP_BINDANY 29879d6b3f3SPawel Jakub Dawidekoption is enabled on a 29979d6b3f3SPawel Jakub Dawidek.Dv SOCK_STREAM , 30079d6b3f3SPawel Jakub Dawidek.Dv SOCK_DGRAM 30179d6b3f3SPawel Jakub Dawidekor a 30279d6b3f3SPawel Jakub Dawidek.Dv SOCK_RAW 30379d6b3f3SPawel Jakub Dawideksocket, one can 30479d6b3f3SPawel Jakub Dawidek.Xr bind 2 30579d6b3f3SPawel Jakub Dawidekto any address, even one not bound to any available network interface in the 30679d6b3f3SPawel Jakub Dawideksystem. 30779d6b3f3SPawel Jakub DawidekThis functionality (in conjunction with special firewall rules) can be used for 30879d6b3f3SPawel Jakub Dawidekimplementing a transparent proxy. 30979d6b3f3SPawel Jakub DawidekThe 31079d6b3f3SPawel Jakub Dawidek.Dv PRIV_NETINET_BINDANY 31179d6b3f3SPawel Jakub Dawidekprivilege is needed to set this option. 31279d6b3f3SPawel Jakub Dawidek.Pp 31379d6b3f3SPawel Jakub DawidekIf the 3144957466bSMatthew N. Dodd.Dv IP_RECVTTL 3154957466bSMatthew N. Doddoption is enabled on a 3164957466bSMatthew N. Dodd.Dv SOCK_DGRAM 3174957466bSMatthew N. Doddsocket, the 3184957466bSMatthew N. Dodd.Xr recvmsg 2 3194957466bSMatthew N. Doddcall will return the 3204957466bSMatthew N. Dodd.Tn IP 3214957466bSMatthew N. Dodd.Tn TTL 3224957466bSMatthew N. Dodd(time to live) field for a 3234957466bSMatthew N. Dodd.Tn UDP 3244957466bSMatthew N. Dodddatagram. 3254957466bSMatthew N. DoddThe msg_control field in the msghdr structure points to a buffer 3264957466bSMatthew N. Doddthat contains a cmsghdr structure followed by the 3274957466bSMatthew N. Dodd.Tn TTL . 3284957466bSMatthew N. DoddThe cmsghdr fields have the following values: 3294957466bSMatthew N. Dodd.Bd -literal 33030852968SMichael Tuexencmsg_len = CMSG_LEN(sizeof(u_char)) 3314957466bSMatthew N. Doddcmsg_level = IPPROTO_IP 3324957466bSMatthew N. Doddcmsg_type = IP_RECVTTL 3334957466bSMatthew N. Dodd.Ed 33404bcf5a9SBruce M Simpson.\" 3354957466bSMatthew N. Dodd.Pp 3364957466bSMatthew N. DoddIf the 3373cca425bSMichael Tuexen.Dv IP_RECVTOS 3383cca425bSMichael Tuexenoption is enabled on a 3393cca425bSMichael Tuexen.Dv SOCK_DGRAM 3403cca425bSMichael Tuexensocket, the 3413cca425bSMichael Tuexen.Xr recvmsg 2 3423cca425bSMichael Tuexencall will return the 3433cca425bSMichael Tuexen.Tn IP 3443cca425bSMichael Tuexen.Tn TOS 3453cca425bSMichael Tuexen(type of service) field for a 3463cca425bSMichael Tuexen.Tn UDP 3473cca425bSMichael Tuexendatagram. 3483cca425bSMichael TuexenThe msg_control field in the msghdr structure points to a buffer 3493cca425bSMichael Tuexenthat contains a cmsghdr structure followed by the 3503cca425bSMichael Tuexen.Tn TOS . 3513cca425bSMichael TuexenThe cmsghdr fields have the following values: 3523cca425bSMichael Tuexen.Bd -literal 3533cca425bSMichael Tuexencmsg_len = CMSG_LEN(sizeof(u_char)) 3543cca425bSMichael Tuexencmsg_level = IPPROTO_IP 3553cca425bSMichael Tuexencmsg_type = IP_RECVTOS 3563cca425bSMichael Tuexen.Ed 3573cca425bSMichael Tuexen.\" 3583cca425bSMichael Tuexen.Pp 3593cca425bSMichael TuexenIf the 3603b5f87e6SMatthew N. Dodd.Dv IP_RECVIF 3613b5f87e6SMatthew N. Doddoption is enabled on a 3623b5f87e6SMatthew N. Dodd.Dv SOCK_DGRAM 3633b5f87e6SMatthew N. Doddsocket, the 3643b5f87e6SMatthew N. Dodd.Xr recvmsg 2 3658d22ff75SRuslan Ermilovcall returns a 3668d22ff75SRuslan Ermilov.Vt "struct sockaddr_dl" 3678d22ff75SRuslan Ermilovcorresponding to the interface on which the 3688d22ff75SRuslan Ermilovpacket was received. 3698d22ff75SRuslan ErmilovThe 3708d22ff75SRuslan Ermilov.Va msg_control 3718d22ff75SRuslan Ermilovfield in the 3728d22ff75SRuslan Ermilov.Vt msghdr 3738d22ff75SRuslan Ermilovstructure points to a buffer that contains a 3748d22ff75SRuslan Ermilov.Vt cmsghdr 3758d22ff75SRuslan Ermilovstructure followed by the 3768d22ff75SRuslan Ermilov.Vt "struct sockaddr_dl" . 3778d22ff75SRuslan ErmilovThe 3788d22ff75SRuslan Ermilov.Vt cmsghdr 3798d22ff75SRuslan Ermilovfields have the following values: 3803b5f87e6SMatthew N. Dodd.Bd -literal 38130852968SMichael Tuexencmsg_len = CMSG_LEN(sizeof(struct sockaddr_dl)) 3823b5f87e6SMatthew N. Doddcmsg_level = IPPROTO_IP 3833b5f87e6SMatthew N. Doddcmsg_type = IP_RECVIF 3843b5f87e6SMatthew N. Dodd.Ed 3853b5f87e6SMatthew N. Dodd.Pp 3867ee32b9fSPeter Wemm.Dv IP_PORTRANGE 3877ee32b9fSPeter Wemmmay be used to set the port range used for selecting a local port number 3886d249eeeSSheldon Hearnon a socket with an unspecified (zero) port number. 3896d249eeeSSheldon HearnIt has the following 3907ee32b9fSPeter Wemmpossible values: 3917ee32b9fSPeter Wemm.Bl -tag -width IP_PORTRANGE_DEFAULT 3927ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_DEFAULT 3937ee32b9fSPeter Wemmuse the default range of values, normally 39474c0361aSMike Silbersack.Dv IPPORT_HIFIRSTAUTO 3957ee32b9fSPeter Wemmthrough 39674c0361aSMike Silbersack.Dv IPPORT_HILASTAUTO . 3977ee32b9fSPeter WemmThis is adjustable through the sysctl setting: 398ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.first 3997ee32b9fSPeter Wemmand 400ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.last . 4017ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_HIGH 4027ee32b9fSPeter Wemmuse a high range of values, normally 4037ee32b9fSPeter Wemm.Dv IPPORT_HIFIRSTAUTO 4047ee32b9fSPeter Wemmand 4057ee32b9fSPeter Wemm.Dv IPPORT_HILASTAUTO . 4067ee32b9fSPeter WemmThis is adjustable through the sysctl setting: 407ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.hifirst 4087ee32b9fSPeter Wemmand 409ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.hilast . 4107ee32b9fSPeter Wemm.It Dv IP_PORTRANGE_LOW 4117ee32b9fSPeter Wemmuse a low range of ports, which are normally restricted to 4127ee32b9fSPeter Wemmprivileged processes on 4137ee32b9fSPeter Wemm.Ux 4148d22ff75SRuslan Ermilovsystems. 4158d22ff75SRuslan ErmilovThe range is normally from 416ace5be68SRuslan Ermilov.Dv IPPORT_RESERVED 417ace5be68SRuslan Ermilov\- 1 down to 4188f5e8232SBen Smithurst.Li IPPORT_RESERVEDSTART 4198f5e8232SBen Smithurstin descending order. 4208f5e8232SBen SmithurstThis is adjustable through the sysctl setting: 421ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.lowfirst 4228f5e8232SBen Smithurstand 423ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.lowlast . 4247ee32b9fSPeter Wemm.El 425718b17bbSCrist J. Clark.Pp 426718b17bbSCrist J. ClarkThe range of privileged ports which only may be opened by 427718b17bbSCrist J. Clarkroot-owned processes may be modified by the 428ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.reservedlow 429718b17bbSCrist J. Clarkand 430ace5be68SRuslan Ermilov.Va net.inet.ip.portrange.reservedhigh 431718b17bbSCrist J. Clarksysctl settings. 432718b17bbSCrist J. ClarkThe values default to the traditional range, 433718b17bbSCrist J. Clark0 through 434ace5be68SRuslan Ermilov.Dv IPPORT_RESERVED 435ace5be68SRuslan Ermilov\- 1 436718b17bbSCrist J. Clark(0 through 1023), respectively. 437718b17bbSCrist J. ClarkNote that these settings do not affect and are not accounted for in the 438718b17bbSCrist J. Clarkuse or calculation of the other 439ace5be68SRuslan Ermilov.Va net.inet.ip.portrange 440718b17bbSCrist J. Clarkvalues above. 441ace5be68SRuslan ErmilovChanging these values departs from 442ace5be68SRuslan Ermilov.Ux 443ace5be68SRuslan Ermilovtradition and has security 444ace5be68SRuslan Ermilovconsequences that the administrator should carefully evaluate before 445718b17bbSCrist J. Clarkmodifying these settings. 446fe9892eaSMike Silbersack.Pp 447fe9892eaSMike SilbersackPorts are allocated at random within the specified port range in order 448bf7f20c2SRuslan Ermilovto increase the difficulty of random spoofing attacks. 449c48524c2SMike PritchardIn scenarios such as benchmarking, this behavior may be undesirable. 450bf7f20c2SRuslan ErmilovIn these cases, 451fe9892eaSMike Silbersack.Va net.inet.ip.portrange.randomized 452fe9892eaSMike Silbersackcan be used to toggle randomization off. 453afe61c15SRodney W. Grimes.Ss "Multicast Options" 454afe61c15SRodney W. Grimes.Tn IP 455afe61c15SRodney W. Grimesmulticasting is supported only on 456afe61c15SRodney W. Grimes.Dv AF_INET 457afe61c15SRodney W. Grimessockets of type 458afe61c15SRodney W. Grimes.Dv SOCK_DGRAM 459afe61c15SRodney W. Grimesand 460afe61c15SRodney W. Grimes.Dv SOCK_RAW , 461afe61c15SRodney W. Grimesand only on networks where the interface 462afe61c15SRodney W. Grimesdriver supports multicasting. 463afe61c15SRodney W. Grimes.Pp 464afe61c15SRodney W. GrimesThe 465afe61c15SRodney W. Grimes.Dv IP_MULTICAST_TTL 466afe61c15SRodney W. Grimesoption changes the time-to-live (TTL) 467afe61c15SRodney W. Grimesfor outgoing multicast datagrams 468afe61c15SRodney W. Grimesin order to control the scope of the multicasts: 469afe61c15SRodney W. Grimes.Bd -literal 470afe61c15SRodney W. Grimesu_char ttl; /* range: 0 to 255, default = 1 */ 471afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)); 472afe61c15SRodney W. Grimes.Ed 4730c3a1746SMike Pritchard.Pp 474afe61c15SRodney W. GrimesDatagrams with a TTL of 1 are not forwarded beyond the local network. 475afe61c15SRodney W. GrimesMulticast datagrams with a TTL of 0 will not be transmitted on any network, 476afe61c15SRodney W. Grimesbut may be delivered locally if the sending host belongs to the destination 477afe61c15SRodney W. Grimesgroup and if multicast loopback has not been disabled on the sending socket 4788d22ff75SRuslan Ermilov(see below). 4798d22ff75SRuslan ErmilovMulticast datagrams with TTL greater than 1 may be forwarded 480afe61c15SRodney W. Grimesto other networks if a multicast router is attached to the local network. 481afe61c15SRodney W. Grimes.Pp 482108ad5bdSBruce M SimpsonFor hosts with multiple interfaces, where an interface has not 483108ad5bdSBruce M Simpsonbeen specified for a multicast group membership, 484108ad5bdSBruce M Simpsoneach multicast transmission is sent from the primary network interface. 485afe61c15SRodney W. GrimesThe 486afe61c15SRodney W. Grimes.Dv IP_MULTICAST_IF 487afe61c15SRodney W. Grimesoption overrides the default for 488afe61c15SRodney W. Grimessubsequent transmissions from a given socket: 489afe61c15SRodney W. Grimes.Bd -literal 490afe61c15SRodney W. Grimesstruct in_addr addr; 491afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_IF, &addr, sizeof(addr)); 492afe61c15SRodney W. Grimes.Ed 4930c3a1746SMike Pritchard.Pp 494afe61c15SRodney W. Grimeswhere "addr" is the local 495afe61c15SRodney W. Grimes.Tn IP 496afe61c15SRodney W. Grimesaddress of the desired interface or 497afe61c15SRodney W. Grimes.Dv INADDR_ANY 498afe61c15SRodney W. Grimesto specify the default interface. 49971498f30SBruce M Simpson.Pp 50071498f30SBruce M SimpsonTo specify an interface by index, an instance of 50171498f30SBruce M Simpson.Vt ip_mreqn 502108ad5bdSBruce M Simpsonmay be passed instead. 50371498f30SBruce M SimpsonThe 50471498f30SBruce M Simpson.Vt imr_ifindex 50571498f30SBruce M Simpsonmember should be set to the index of the desired interface, 50671498f30SBruce M Simpsonor 0 to specify the default interface. 50771498f30SBruce M SimpsonThe kernel differentiates between these two structures by their size. 508108ad5bdSBruce M Simpson.Pp 509108ad5bdSBruce M SimpsonThe use of 510108ad5bdSBruce M Simpson.Vt IP_MULTICAST_IF 511108ad5bdSBruce M Simpsonis 512108ad5bdSBruce M Simpson.Em not recommended , 513108ad5bdSBruce M Simpsonas multicast memberships are scoped to each 514108ad5bdSBruce M Simpsonindividual interface. 515108ad5bdSBruce M SimpsonIt is supported for legacy use only by applications, 516108ad5bdSBruce M Simpsonsuch as routing daemons, which expect to 517108ad5bdSBruce M Simpsonbe able to transmit link-local IPv4 multicast datagrams (224.0.0.0/24) 518108ad5bdSBruce M Simpsonon multiple interfaces, 519108ad5bdSBruce M Simpsonwithout requesting an individual membership for each interface. 520108ad5bdSBruce M Simpson.Pp 52171498f30SBruce M Simpson.\" 522afe61c15SRodney W. GrimesAn interface's local IP address and multicast capability can 523afe61c15SRodney W. Grimesbe obtained via the 524afe61c15SRodney W. Grimes.Dv SIOCGIFCONF 525afe61c15SRodney W. Grimesand 526afe61c15SRodney W. Grimes.Dv SIOCGIFFLAGS 527afe61c15SRodney W. Grimesioctls. 528afe61c15SRodney W. GrimesNormal applications should not need to use this option. 529afe61c15SRodney W. Grimes.Pp 530afe61c15SRodney W. GrimesIf a multicast datagram is sent to a group to which the sending host itself 531afe61c15SRodney W. Grimesbelongs (on the outgoing interface), a copy of the datagram is, by default, 532afe61c15SRodney W. Grimeslooped back by the IP layer for local delivery. 533afe61c15SRodney W. GrimesThe 534afe61c15SRodney W. Grimes.Dv IP_MULTICAST_LOOP 535afe61c15SRodney W. Grimesoption gives the sender explicit control 536afe61c15SRodney W. Grimesover whether or not subsequent datagrams are looped back: 537afe61c15SRodney W. Grimes.Bd -literal 538afe61c15SRodney W. Grimesu_char loop; /* 0 = disable, 1 = enable (default) */ 539afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_MULTICAST_LOOP, &loop, sizeof(loop)); 540afe61c15SRodney W. Grimes.Ed 5410c3a1746SMike Pritchard.Pp 542afe61c15SRodney W. GrimesThis option 543afe61c15SRodney W. Grimesimproves performance for applications that may have no more than one 544d10910e6SBruce M Simpsoninstance on a single host (such as a routing daemon), by eliminating 5458d22ff75SRuslan Ermilovthe overhead of receiving their own transmissions. 5468d22ff75SRuslan ErmilovIt should generally not 547afe61c15SRodney W. Grimesbe used by applications for which there may be more than one instance on a 548afe61c15SRodney W. Grimessingle host (such as a conferencing program) or for which the sender does 549afe61c15SRodney W. Grimesnot belong to the destination group (such as a time querying program). 550afe61c15SRodney W. Grimes.Pp 551d10910e6SBruce M SimpsonThe sysctl setting 552d10910e6SBruce M Simpson.Va net.inet.ip.mcast.loop 553d10910e6SBruce M Simpsoncontrols the default setting of the 554d10910e6SBruce M Simpson.Dv IP_MULTICAST_LOOP 555d10910e6SBruce M Simpsonsocket option for new sockets. 556d10910e6SBruce M Simpson.Pp 557afe61c15SRodney W. GrimesA multicast datagram sent with an initial TTL greater than 1 may be delivered 558afe61c15SRodney W. Grimesto the sending host on a different interface from that on which it was sent, 5598d22ff75SRuslan Ermilovif the host belongs to the destination group on that other interface. 5608d22ff75SRuslan ErmilovThe loopback control option has no effect on such delivery. 561afe61c15SRodney W. Grimes.Pp 562afe61c15SRodney W. GrimesA host must become a member of a multicast group before it can receive 5638d22ff75SRuslan Ermilovdatagrams sent to the group. 5648d22ff75SRuslan ErmilovTo join a multicast group, use the 565afe61c15SRodney W. Grimes.Dv IP_ADD_MEMBERSHIP 566afe61c15SRodney W. Grimesoption: 567afe61c15SRodney W. Grimes.Bd -literal 5680dfc145aSGleb Smirnoffstruct ip_mreqn mreqn; 5690dfc145aSGleb Smirnoffsetsockopt(s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreqn, sizeof(mreqn)); 570afe61c15SRodney W. Grimes.Ed 5710c3a1746SMike Pritchard.Pp 572afe61c15SRodney W. Grimeswhere 5730dfc145aSGleb Smirnoff.Fa mreqn 574afe61c15SRodney W. Grimesis the following structure: 575afe61c15SRodney W. Grimes.Bd -literal 5760dfc145aSGleb Smirnoffstruct ip_mreqn { 5770c3a1746SMike Pritchard struct in_addr imr_multiaddr; /* IP multicast address of group */ 5780c3a1746SMike Pritchard struct in_addr imr_interface; /* local IP address of interface */ 5790dfc145aSGleb Smirnoff int imr_ifindex; /* interface index */ 580afe61c15SRodney W. Grimes} 581afe61c15SRodney W. Grimes.Ed 5820c3a1746SMike Pritchard.Pp 5830dfc145aSGleb Smirnoff.Va imr_ifindex 5840dfc145aSGleb Smirnoffshould be set to the index of a particular multicast-capable interface if 585afe61c15SRodney W. Grimesthe host is multihomed. 5860dfc145aSGleb SmirnoffIf 5870dfc145aSGleb Smirnoff.Va imr_ifindex 5880dfc145aSGleb Smirnoffis non-zero, value of 5890dfc145aSGleb Smirnoff.Va imr_interface 5900dfc145aSGleb Smirnoffis ignored. 5910dfc145aSGleb SmirnoffOtherwise, if 5920dfc145aSGleb Smirnoff.Va imr_ifindex 5930dfc145aSGleb Smirnoffis 0, kernel will use IP address from 5940dfc145aSGleb Smirnoff.Va imr_interface 5950dfc145aSGleb Smirnoffto lookup the interface. 5960dfc145aSGleb SmirnoffValue of 5970dfc145aSGleb Smirnoff.Va imr_interface 5980dfc145aSGleb Smirnoffmay be set to 5990dfc145aSGleb Smirnoff.Va INADDR_ANY 6000dfc145aSGleb Smirnoffto choose the default interface, although this is not recommended; this is 6010dfc145aSGleb Smirnoffconsidered to be the first interface corresponding to the default route. 6020dfc145aSGleb SmirnoffOtherwise, the first multicast-capable interface configured in the system 6030dfc145aSGleb Smirnoffwill be used. 6040dfc145aSGleb Smirnoff.Pp 6050dfc145aSGleb SmirnoffLegacy 6060dfc145aSGleb Smirnoff.Vt "struct ip_mreq" , 6070dfc145aSGleb Smirnoffthat lacks 6080dfc145aSGleb Smirnoff.Va imr_ifindex 6090dfc145aSGleb Smirnofffield is also supported by 6100dfc145aSGleb Smirnoff.Dv IP_ADD_MEMBERSHIP 6110dfc145aSGleb Smirnoffsetsockopt. 6120dfc145aSGleb SmirnoffIn this case kernel would behave as if 6130dfc145aSGleb Smirnoff.Va imr_ifindex 6140dfc145aSGleb Smirnoffwas set to zero: 6150dfc145aSGleb Smirnoff.Va imr_interface 6160dfc145aSGleb Smirnoffwill be used to lookup interface. 617108ad5bdSBruce M Simpson.Pp 618108ad5bdSBruce M SimpsonPrior to 619108ad5bdSBruce M Simpson.Fx 7.0 , 62026c3ef53SBruce M Simpsonif the 621bf7f20c2SRuslan Ermilov.Va imr_interface 62226c3ef53SBruce M Simpsonmember is within the network range 62326c3ef53SBruce M Simpson.Li 0.0.0.0/8 , 62426c3ef53SBruce M Simpsonit is treated as an interface index in the system interface MIB, 62526c3ef53SBruce M Simpsonas per the RIP Version 2 MIB Extension (RFC-1724). 626108ad5bdSBruce M SimpsonIn versions of 627108ad5bdSBruce M Simpson.Fx 628108ad5bdSBruce M Simpsonsince 7.0, this behavior is no longer supported. 629108ad5bdSBruce M SimpsonDevelopers should 630108ad5bdSBruce M Simpsoninstead use the RFC 3678 multicast source filter APIs; in particular, 631108ad5bdSBruce M Simpson.Dv MCAST_JOIN_GROUP . 63226c3ef53SBruce M Simpson.Pp 633afe61c15SRodney W. GrimesUp to 634afe61c15SRodney W. Grimes.Dv IP_MAX_MEMBERSHIPS 63524135314SBruce M Simpsonmemberships may be added on a single socket. 63604bcf5a9SBruce M SimpsonMembership is associated with a single interface; 63704bcf5a9SBruce M Simpsonprograms running on multihomed hosts may need to 63804bcf5a9SBruce M Simpsonjoin the same group on more than one interface. 63904bcf5a9SBruce M Simpson.Pp 640afe61c15SRodney W. GrimesTo drop a membership, use: 641afe61c15SRodney W. Grimes.Bd -literal 642afe61c15SRodney W. Grimesstruct ip_mreq mreq; 643afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_DROP_MEMBERSHIP, &mreq, sizeof(mreq)); 644afe61c15SRodney W. Grimes.Ed 6450c3a1746SMike Pritchard.Pp 646afe61c15SRodney W. Grimeswhere 647afe61c15SRodney W. Grimes.Fa mreq 648afe61c15SRodney W. Grimescontains the same values as used to add the membership. 649afe61c15SRodney W. GrimesMemberships are dropped when the socket is closed or the process exits. 650108ad5bdSBruce M Simpson.\" TODO: Update this piece when IPv4 source-address selection is implemented. 651108ad5bdSBruce M Simpson.Pp 652108ad5bdSBruce M SimpsonThe IGMP protocol uses the primary IP address of the interface 653108ad5bdSBruce M Simpsonas its identifier for group membership. 654108ad5bdSBruce M SimpsonThis is the first IP address configured on the interface. 655108ad5bdSBruce M SimpsonIf this address is removed or changed, the results are 656108ad5bdSBruce M Simpsonundefined, as the IGMP membership state will then be inconsistent. 657108ad5bdSBruce M SimpsonIf multiple IP aliases are configured on the same interface, 658108ad5bdSBruce M Simpsonthey will be ignored. 659108ad5bdSBruce M Simpson.Pp 660108ad5bdSBruce M SimpsonThis shortcoming was addressed in IPv6; MLDv2 requires 661108ad5bdSBruce M Simpsonthat the unique link-local address for an interface is 662108ad5bdSBruce M Simpsonused to identify an MLDv2 listener. 663108ad5bdSBruce M Simpson.Ss "Source-Specific Multicast Options" 664108ad5bdSBruce M SimpsonSince 665108ad5bdSBruce M Simpson.Fx 8.0 , 666108ad5bdSBruce M Simpsonthe use of Source-Specific Multicast (SSM) is supported. 667108ad5bdSBruce M SimpsonThese extensions require an IGMPv3 multicast router in order to 668108ad5bdSBruce M Simpsonmake best use of them. 669108ad5bdSBruce M SimpsonIf a legacy multicast router is present on the link, 670108ad5bdSBruce M Simpson.Fx 671108ad5bdSBruce M Simpsonwill simply downgrade to the version of IGMP spoken by the router, 672108ad5bdSBruce M Simpsonand the benefits of source filtering on the upstream link 673108ad5bdSBruce M Simpsonwill not be present, although the kernel will continue to 674108ad5bdSBruce M Simpsonsquelch transmissions from blocked sources. 675108ad5bdSBruce M Simpson.Pp 676108ad5bdSBruce M SimpsonEach group membership on a socket now has a filter mode: 677108ad5bdSBruce M Simpson.Bl -tag -width MCAST_EXCLUDE 678108ad5bdSBruce M Simpson.It Dv MCAST_EXCLUDE 679108ad5bdSBruce M SimpsonDatagrams sent to this group are accepted, 680108ad5bdSBruce M Simpsonunless the source is in a list of blocked source addresses. 681108ad5bdSBruce M Simpson.It Dv MCAST_INCLUDE 682108ad5bdSBruce M SimpsonDatagrams sent to this group are accepted 683108ad5bdSBruce M Simpsononly if the source is in a list of accepted source addresses. 684108ad5bdSBruce M Simpson.El 685108ad5bdSBruce M Simpson.Pp 686108ad5bdSBruce M SimpsonGroups joined using the legacy 687108ad5bdSBruce M Simpson.Dv IP_ADD_MEMBERSHIP 688108ad5bdSBruce M Simpsonoption are placed in exclusive-mode, 689108ad5bdSBruce M Simpsonand are able to request that certain sources are blocked or allowed. 690108ad5bdSBruce M SimpsonThis is known as the 691108ad5bdSBruce M Simpson.Em delta-based API . 692108ad5bdSBruce M Simpson.Pp 693108ad5bdSBruce M SimpsonTo block a multicast source on an existing group membership: 694108ad5bdSBruce M Simpson.Bd -literal 695108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs; 696108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_BLOCK_SOURCE, &mreqs, sizeof(mreqs)); 697108ad5bdSBruce M Simpson.Ed 698108ad5bdSBruce M Simpson.Pp 699108ad5bdSBruce M Simpsonwhere 700108ad5bdSBruce M Simpson.Fa mreqs 701108ad5bdSBruce M Simpsonis the following structure: 702108ad5bdSBruce M Simpson.Bd -literal 703108ad5bdSBruce M Simpsonstruct ip_mreq_source { 704108ad5bdSBruce M Simpson struct in_addr imr_multiaddr; /* IP multicast address of group */ 705108ad5bdSBruce M Simpson struct in_addr imr_sourceaddr; /* IP address of source */ 706108ad5bdSBruce M Simpson struct in_addr imr_interface; /* local IP address of interface */ 707108ad5bdSBruce M Simpson} 708108ad5bdSBruce M Simpson.Ed 709108ad5bdSBruce M Simpson.Va imr_sourceaddr 710108ad5bdSBruce M Simpsonshould be set to the address of the source to be blocked. 711108ad5bdSBruce M Simpson.Pp 712108ad5bdSBruce M SimpsonTo unblock a multicast source on an existing group: 713108ad5bdSBruce M Simpson.Bd -literal 714108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs; 715108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_UNBLOCK_SOURCE, &mreqs, sizeof(mreqs)); 716108ad5bdSBruce M Simpson.Ed 717108ad5bdSBruce M Simpson.Pp 718108ad5bdSBruce M SimpsonThe 719108ad5bdSBruce M Simpson.Dv IP_BLOCK_SOURCE 720108ad5bdSBruce M Simpsonand 721108ad5bdSBruce M Simpson.Dv IP_UNBLOCK_SOURCE 722108ad5bdSBruce M Simpsonoptions are 723108ad5bdSBruce M Simpson.Em not permitted 724108ad5bdSBruce M Simpsonfor inclusive-mode group memberships. 725108ad5bdSBruce M Simpson.Pp 726108ad5bdSBruce M SimpsonTo join a multicast group in 727108ad5bdSBruce M Simpson.Dv MCAST_INCLUDE 728108ad5bdSBruce M Simpsonmode with a single source, 729108ad5bdSBruce M Simpsonor add another source to an existing inclusive-mode membership: 730108ad5bdSBruce M Simpson.Bd -literal 731108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs; 732108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_ADD_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs)); 733108ad5bdSBruce M Simpson.Ed 734108ad5bdSBruce M Simpson.Pp 735108ad5bdSBruce M SimpsonTo leave a single source from an existing group in inclusive mode: 736108ad5bdSBruce M Simpson.Bd -literal 737108ad5bdSBruce M Simpsonstruct ip_mreq_source mreqs; 738108ad5bdSBruce M Simpsonsetsockopt(s, IPPROTO_IP, IP_DROP_SOURCE_MEMBERSHIP, &mreqs, sizeof(mreqs)); 739108ad5bdSBruce M Simpson.Ed 740108ad5bdSBruce M SimpsonIf this is the last accepted source for the group, the membership 741108ad5bdSBruce M Simpsonwill be dropped. 742108ad5bdSBruce M Simpson.Pp 743108ad5bdSBruce M SimpsonThe 744108ad5bdSBruce M Simpson.Dv IP_ADD_SOURCE_MEMBERSHIP 745108ad5bdSBruce M Simpsonand 746108ad5bdSBruce M Simpson.Dv IP_DROP_SOURCE_MEMBERSHIP 747108ad5bdSBruce M Simpsonoptions are 748108ad5bdSBruce M Simpson.Em not accepted 749108ad5bdSBruce M Simpsonfor exclusive-mode group memberships. 750108ad5bdSBruce M SimpsonHowever, both exclusive and inclusive mode memberships 751108ad5bdSBruce M Simpsonsupport the use of the 752108ad5bdSBruce M Simpson.Em full-state API 753108ad5bdSBruce M Simpsondocumented in RFC 3678. 754108ad5bdSBruce M SimpsonFor management of source filter lists using this API, 755108ad5bdSBruce M Simpsonplease refer to 756108ad5bdSBruce M Simpson.Xr sourcefilter 3 . 757d10910e6SBruce M Simpson.Pp 758d10910e6SBruce M SimpsonThe sysctl settings 759d10910e6SBruce M Simpson.Va net.inet.ip.mcast.maxsocksrc 760d10910e6SBruce M Simpsonand 761d10910e6SBruce M Simpson.Va net.inet.ip.mcast.maxgrpsrc 762d10910e6SBruce M Simpsonare used to specify an upper limit on the number of per-socket and per-group 763d10910e6SBruce M Simpsonsource filter entries which the kernel may allocate. 764afe61c15SRodney W. Grimes.\"----------------------- 765afe61c15SRodney W. Grimes.Ss "Raw IP Sockets" 766afe61c15SRodney W. GrimesRaw 767afe61c15SRodney W. Grimes.Tn IP 768afe61c15SRodney W. Grimessockets are connectionless, 769afe61c15SRodney W. Grimesand are normally used with the 770edf0e5b3SMike Pritchard.Xr sendto 2 771afe61c15SRodney W. Grimesand 772edf0e5b3SMike Pritchard.Xr recvfrom 2 773afe61c15SRodney W. Grimescalls, though the 774afe61c15SRodney W. Grimes.Xr connect 2 775afe61c15SRodney W. Grimescall may also be used to fix the destination for future 776afe61c15SRodney W. Grimespackets (in which case the 777afe61c15SRodney W. Grimes.Xr read 2 778afe61c15SRodney W. Grimesor 779afe61c15SRodney W. Grimes.Xr recv 2 780afe61c15SRodney W. Grimesand 781afe61c15SRodney W. Grimes.Xr write 2 782afe61c15SRodney W. Grimesor 783afe61c15SRodney W. Grimes.Xr send 2 784afe61c15SRodney W. Grimessystem calls may be used). 785afe61c15SRodney W. Grimes.Pp 786afe61c15SRodney W. GrimesIf 787afe61c15SRodney W. Grimes.Fa proto 788afe61c15SRodney W. Grimesis 0, the default protocol 789afe61c15SRodney W. Grimes.Dv IPPROTO_RAW 790afe61c15SRodney W. Grimesis used for outgoing 791afe61c15SRodney W. Grimespackets, and only incoming packets destined for that protocol 792afe61c15SRodney W. Grimesare received. 793afe61c15SRodney W. GrimesIf 794afe61c15SRodney W. Grimes.Fa proto 795afe61c15SRodney W. Grimesis non-zero, that protocol number will be used on outgoing packets 796afe61c15SRodney W. Grimesand to filter incoming packets. 797afe61c15SRodney W. Grimes.Pp 798afe61c15SRodney W. GrimesOutgoing packets automatically have an 799afe61c15SRodney W. Grimes.Tn IP 800afe61c15SRodney W. Grimesheader prepended to 801afe61c15SRodney W. Grimesthem (based on the destination address and the protocol 802afe61c15SRodney W. Grimesnumber the socket is created with), 803afe61c15SRodney W. Grimesunless the 804afe61c15SRodney W. Grimes.Dv IP_HDRINCL 805afe61c15SRodney W. Grimesoption has been set. 806c26544aaSGleb SmirnoffUnlike in previous 807c26544aaSGleb Smirnoff.Bx 808c26544aaSGleb Smirnoffreleases, incoming packets are received with 809afe61c15SRodney W. Grimes.Tn IP 810c26544aaSGleb Smirnoffheader and options intact, leaving all fields in network byte order. 811afe61c15SRodney W. Grimes.Pp 812afe61c15SRodney W. Grimes.Dv IP_HDRINCL 813afe61c15SRodney W. Grimesindicates the complete IP header is included with the data 814afe61c15SRodney W. Grimesand may be used only with the 815afe61c15SRodney W. Grimes.Dv SOCK_RAW 816afe61c15SRodney W. Grimestype. 817afe61c15SRodney W. Grimes.Bd -literal 818ddbd0698SBruce Evans#include <netinet/in_systm.h> 819afe61c15SRodney W. Grimes#include <netinet/ip.h> 820afe61c15SRodney W. Grimes 821afe61c15SRodney W. Grimesint hincl = 1; /* 1 = on, 0 = off */ 822afe61c15SRodney W. Grimessetsockopt(s, IPPROTO_IP, IP_HDRINCL, &hincl, sizeof(hincl)); 823afe61c15SRodney W. Grimes.Ed 8240c3a1746SMike Pritchard.Pp 825afe61c15SRodney W. GrimesUnlike previous 8268ce3f3ddSRuslan Ermilov.Bx 827afe61c15SRodney W. Grimesreleases, the program must set all 828afe61c15SRodney W. Grimesthe fields of the IP header, including the following: 829afe61c15SRodney W. Grimes.Bd -literal 830afe61c15SRodney W. Grimesip->ip_v = IPVERSION; 831afe61c15SRodney W. Grimesip->ip_hl = hlen >> 2; 832afe61c15SRodney W. Grimesip->ip_id = 0; /* 0 means kernel set appropriate value */ 833c26544aaSGleb Smirnoffip->ip_off = htons(offset); 834c26544aaSGleb Smirnoffip->ip_len = htons(len); 835afe61c15SRodney W. Grimes.Ed 8360c3a1746SMike Pritchard.Pp 837c26544aaSGleb SmirnoffThe packet should be provided as is to be sent over wire. 838c26544aaSGleb SmirnoffThis implies all fields, including 839538a4c1fSBruce M Simpson.Va ip_len 840538a4c1fSBruce M Simpsonand 841538a4c1fSBruce M Simpson.Va ip_off 842c26544aaSGleb Smirnoffto be in network byte order. 8436f5bc70aSBruce M SimpsonSee 8445c8e5f24SRuslan Ermilov.Xr byteorder 3 8456f5bc70aSBruce M Simpsonfor more information on network byte order. 846538a4c1fSBruce M SimpsonIf the 847538a4c1fSBruce M Simpson.Va ip_id 848538a4c1fSBruce M Simpsonfield is set to 0 then the kernel will choose an 849c48524c2SMike Pritchardappropriate value. 850afe61c15SRodney W. GrimesIf the header source address is set to 851afe61c15SRodney W. Grimes.Dv INADDR_ANY , 852afe61c15SRodney W. Grimesthe kernel will choose an appropriate address. 853ef0180edSDima Dorfman.Sh ERRORS 854afe61c15SRodney W. GrimesA socket operation may fail with one of the following errors returned: 855ef0180edSDima Dorfman.Bl -tag -width Er 856afe61c15SRodney W. Grimes.It Bq Er EISCONN 857afe61c15SRodney W. Grimeswhen trying to establish a connection on a socket which 858afe61c15SRodney W. Grimesalready has one, or when trying to send a datagram with the destination 859afe61c15SRodney W. Grimesaddress specified and the socket is already connected; 860afe61c15SRodney W. Grimes.It Bq Er ENOTCONN 861afe61c15SRodney W. Grimeswhen trying to send a datagram, but 8620227791bSRuslan Ermilovno destination address is specified, and the socket has not been 863afe61c15SRodney W. Grimesconnected; 864afe61c15SRodney W. Grimes.It Bq Er ENOBUFS 865afe61c15SRodney W. Grimeswhen the system runs out of memory for 866afe61c15SRodney W. Grimesan internal data structure; 867afe61c15SRodney W. Grimes.It Bq Er EADDRNOTAVAIL 868afe61c15SRodney W. Grimeswhen an attempt is made to create a 869afe61c15SRodney W. Grimessocket with a network address for which no network interface 870afe61c15SRodney W. Grimesexists. 8717bdf965dSAlexander Langer.It Bq Er EACCES 872afe61c15SRodney W. Grimeswhen an attempt is made to create 873afe61c15SRodney W. Grimesa raw IP socket by a non-privileged process. 874afe61c15SRodney W. Grimes.El 875afe61c15SRodney W. Grimes.Pp 876afe61c15SRodney W. GrimesThe following errors specific to 877afe61c15SRodney W. Grimes.Tn IP 878afe61c15SRodney W. Grimesmay occur when setting or getting 879afe61c15SRodney W. Grimes.Tn IP 880afe61c15SRodney W. Grimesoptions: 8818d22ff75SRuslan Ermilov.Bl -tag -width Er 882afe61c15SRodney W. Grimes.It Bq Er EINVAL 883afe61c15SRodney W. GrimesAn unknown socket option name was given. 884afe61c15SRodney W. Grimes.It Bq Er EINVAL 885afe61c15SRodney W. GrimesThe IP option field was improperly formed; 886afe61c15SRodney W. Grimesan option field was shorter than the minimum value 887afe61c15SRodney W. Grimesor longer than the option buffer provided. 888afe61c15SRodney W. Grimes.El 8896f5bc70aSBruce M Simpson.Pp 8906f5bc70aSBruce M SimpsonThe following errors may occur when attempting to send 8916f5bc70aSBruce M Simpson.Tn IP 8926f5bc70aSBruce M Simpsondatagrams via a 8936f5bc70aSBruce M Simpson.Dq raw socket 8946f5bc70aSBruce M Simpsonwith the 895538a4c1fSBruce M Simpson.Dv IP_HDRINCL 8966f5bc70aSBruce M Simpsonoption set: 8976f5bc70aSBruce M Simpson.Bl -tag -width Er 8986f5bc70aSBruce M Simpson.It Bq Er EINVAL 899538a4c1fSBruce M SimpsonThe user-supplied 900538a4c1fSBruce M Simpson.Va ip_len 901538a4c1fSBruce M Simpsonfield was not equal to the length of the datagram written to the socket. 9026f5bc70aSBruce M Simpson.El 903afe61c15SRodney W. Grimes.Sh SEE ALSO 904afe61c15SRodney W. Grimes.Xr getsockopt 2 , 905afe61c15SRodney W. Grimes.Xr recv 2 , 9060b992c1dSWolfram Schneider.Xr send 2 , 9075c8e5f24SRuslan Ermilov.Xr byteorder 3 , 908c8b8b38eSMateusz Piotrowski.Xr CMSG_DATA 3 , 9090b3504fdSChristian Brueffer.Xr sourcefilter 3 , 910afe61c15SRodney W. Grimes.Xr icmp 4 , 911d10910e6SBruce M Simpson.Xr igmp 4 , 9120b992c1dSWolfram Schneider.Xr inet 4 , 91304bcf5a9SBruce M Simpson.Xr intro 4 , 9140b3504fdSChristian Brueffer.Xr multicast 4 915108ad5bdSBruce M Simpson.Rs 916108ad5bdSBruce M Simpson.%A D. Thaler 917108ad5bdSBruce M Simpson.%A B. Fenner 918108ad5bdSBruce M Simpson.%A B. Quinn 919108ad5bdSBruce M Simpson.%T "Socket Interface Extensions for Multicast Source Filters" 920108ad5bdSBruce M Simpson.%N RFC 3678 921108ad5bdSBruce M Simpson.%D Jan 2004 922108ad5bdSBruce M Simpson.Re 923afe61c15SRodney W. Grimes.Sh HISTORY 924afe61c15SRodney W. GrimesThe 925afe61c15SRodney W. Grimes.Nm 926afe61c15SRodney W. Grimesprotocol appeared in 927afe61c15SRodney W. Grimes.Bx 4.2 . 92871498f30SBruce M SimpsonThe 92971498f30SBruce M Simpson.Vt ip_mreqn 93071498f30SBruce M Simpsonstructure appeared in 93171498f30SBruce M Simpson.Tn Linux 2.4 . 93295386930SAndre Oppermann.Sh BUGS 93395386930SAndre OppermannBefore 934c53bbc07SAndre Oppermann.Fx 10.0 935c53bbc07SAndre Oppermannpackets received on raw IP sockets had the 93695386930SAndre Oppermann.Va ip_hl 93795386930SAndre Oppermannsubtracted from the 93850d922a0SGlen Barber.Va ip_len 93950d922a0SGlen Barberfield. 940c26544aaSGleb Smirnoff.Pp 941c26544aaSGleb SmirnoffBefore 942c26544aaSGleb Smirnoff.Fx 11.0 943c26544aaSGleb Smirnoffpackets received on raw IP sockets had the 944c26544aaSGleb Smirnoff.Va ip_len 945c26544aaSGleb Smirnoffand 946c26544aaSGleb Smirnoff.Va ip_off 947c26544aaSGleb Smirnofffields converted to host byte order. 948c26544aaSGleb SmirnoffPackets written to raw IP sockets were expected to have 949c26544aaSGleb Smirnoff.Va ip_len 950c26544aaSGleb Smirnoffand 951c26544aaSGleb Smirnoff.Va ip_off 952c26544aaSGleb Smirnoffin host byte order. 953