1.\" $OpenBSD: netintro.4,v 1.47 2014/08/10 07:23:59 guenther Exp $ 2.\" $NetBSD: netintro.4,v 1.4 1995/10/19 08:03:40 jtc Exp $ 3.\" 4.\" Copyright (c) 1983, 1990, 1991, 1993 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)netintro.4 8.2 (Berkeley) 11/30/93 32.\" 33.Dd $Mdocdate: August 10 2014 $ 34.Dt NETINTRO 4 35.Os 36.Sh NAME 37.Nm netintro 38.Nd introduction to networking facilities 39.Sh SYNOPSIS 40.Fd #include <sys/socket.h> 41.Fd #include <net/route.h> 42.Fd #include <net/if.h> 43.Sh DESCRIPTION 44This section is a general introduction to the networking facilities 45available in the system. 46Documentation in this part of section 474 is broken up into three areas: 48.Em protocol families 49(domains), 50.Em protocols , 51and 52.Em network interfaces . 53.Pp 54All network protocols are associated with a specific 55.Em protocol family . 56A protocol family provides basic services to the protocol 57implementation to allow it to function within a specific 58network environment. 59These services may include packet fragmentation and reassembly, routing, 60addressing, and basic transport. 61A protocol family may support multiple methods of addressing, though 62the current protocol implementations do not. 63A protocol family is normally comprised of a number of protocols, one per 64.Xr socket 2 65type. 66It is not required that a protocol family support all socket types. 67A protocol family may contain multiple protocols supporting the same socket 68abstraction. 69.Pp 70A protocol supports one of the socket abstractions detailed in 71.Xr socket 2 . 72A specific protocol may be accessed either by creating a 73socket of the appropriate type and protocol family, or 74by requesting the protocol explicitly when creating a socket. 75Protocols normally accept only one type of address format, 76usually determined by the addressing structure inherent in 77the design of the protocol family/network architecture. 78Certain semantics of the basic socket abstractions are 79protocol specific. 80All protocols are expected to support the basic model for their particular 81socket type, but may, in addition, provide non-standard facilities or 82extensions to a mechanism. 83For example, a protocol supporting the 84.Dv SOCK_STREAM 85abstraction may allow more than one byte of out-of-band 86data to be transmitted per out-of-band message. 87.Pp 88A network interface is similar to a device interface. 89Network interfaces comprise the lowest layer of the 90networking subsystem, interacting with the actual transport 91hardware. 92An interface may support one or more protocol families and/or address formats. 93The 94.Sx SYNOPSIS 95section of each network interface entry gives a sample 96specification of the related drivers for use in providing a system description 97to the 98.Xr config 8 99program. 100The 101.Sx DIAGNOSTICS 102section lists messages which may appear on the console 103and/or in the system error log, 104.Pa /var/log/messages 105(see 106.Xr syslogd 8 ) , 107due to errors in device operation. 108.Pp 109Network interfaces may be collected together into interface groups. 110An interface group is a container that can be used generically when 111referring to any interface related by some criteria. 112When an action is performed on an interface group, such as packet 113filtering by the 114.Xr pf 4 115subsystem, the operation will be applied to each member interface in the 116group, if supported by the subsystem. 117The 118.Xr ifconfig 8 119utility can be used to view and assign membership of an interface to an 120interface group with the 121.Cm group 122modifier. 123.Sh PROTOCOLS 124The system currently supports the 125Internet protocols (IPv4 and IPv6), 126MPLS, 127and a few others. 128Raw socket interfaces are provided to the 129.Tn IP 130protocol 131layer of the 132Internet. 133Consult the appropriate manual pages in this section for more 134information regarding the support for each protocol family. 135.Sh ADDRESSING 136Associated with each protocol family is an address 137format. 138All network addresses adhere to a general structure, called a 139.Vt sockaddr , 140described below. 141However, each protocol imposes a finer, more specific structure, generally 142renaming the variant, which is discussed in the protocol family manual 143page alluded to above. 144.Bd -literal -offset indent 145struct sockaddr { 146 u_int8_t sa_len; /* total length */ 147 sa_family_t sa_family; /* address family */ 148 char sa_data[14]; /* actually longer */ 149}; 150.Ed 151.Pp 152The field 153.Va sa_len 154contains the total length of the structure, 155which may exceed 16 bytes. 156The following address values for 157.Va sa_family 158are known to the system 159(and additional formats are defined for possible future implementation): 160.Bd -literal 161#define AF_LOCAL 1 /* local to host */ 162#define AF_INET 2 /* internetwork: UDP, TCP, etc. */ 163#define AF_INET6 24 /* IPv6 */ 164#define AF_MPLS 33 /* Multiprotocol Label Switching */ 165.Ed 166.Pp 167The 168.Va sa_data 169field contains the actual address value. 170Note that it may be longer than 14 bytes. 171.Sh ROUTING 172.Ox 173provides some packet routing facilities. 174The kernel maintains a routing information database, which 175is used in selecting the appropriate network interface when 176transmitting packets. 177.Pp 178A user process (or possibly multiple co-operating processes) 179maintains this database by sending messages over a special kind 180of socket. 181This supplants fixed-size 182.Xr ioctl 2 Ns s 183used in earlier releases. 184.Pp 185This facility is described in 186.Xr route 4 . 187.Sh INTERFACES 188Each network interface in a system corresponds to a 189path through which messages may be sent and received. 190A network interface usually has a hardware device associated with it, 191though certain interfaces such as the loopback interface, 192.Xr lo 4 , 193do not. 194.Pp 195The following 196.Xr ioctl 2 197calls may be used to manipulate network interfaces. 198The 199.Xr ioctl 2 200is made on a socket (typically of type 201.Dv SOCK_DGRAM ) 202in the desired domain. 203Most of the requests 204take an 205.Vt ifreq 206structure pointer as their parameter. 207This structure is as follows: 208.Bd -literal 209struct ifreq { 210#define IFNAMSIZ 16 211 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 212 union { 213 struct sockaddr ifru_addr; 214 struct sockaddr ifru_dstaddr; 215 struct sockaddr ifru_broadaddr; 216 short ifru_flags; 217 int ifru_metric; 218 caddr_t ifru_data; 219 } ifr_ifru; 220#define ifr_addr ifr_ifru.ifru_addr /* address */ 221#define ifr_dstaddr ifr_ifru.ifru_dstaddr /* p-to-p peer */ 222#define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 223#define ifr_flags ifr_ifru.ifru_flags /* flags */ 224#define ifr_metric ifr_ifru.ifru_metric /* metric */ 225#define ifr_mtu ifr_ifru.ifru_metric /* mtu (overload) */ 226#define ifr_media ifr_ifru.ifru_metric /* media options */ 227#define ifr_data ifr_ifru.ifru_data /* used by interface */ 228}; 229.Ed 230.Pp 231The supported 232.Xr ioctl 2 233requests are: 234.Bl -tag -width Ds 235.It Dv SIOCSIFADDR Fa "struct ifreq *" 236Set the interface address for a protocol family. 237Following the address assignment, the 238.Dq initialization 239routine for the 240interface is called. 241.Pp 242This call has been deprecated and superseded by the 243.Dv SIOCAIFADDR 244call, described below. 245.It Dv SIOCSIFDSTADDR Fa "struct ifreq *" 246Set the point-to-point address for a protocol family and interface. 247.Pp 248This call has been deprecated and superseded by the 249.Dv SIOCAIFADDR 250call, described below. 251.It Dv SIOCSIFBRDADDR Fa "struct ifreq *" 252Set the broadcast address for a protocol family and interface. 253.Pp 254This call has been deprecated and superseded by the 255.Dv SIOCAIFADDR 256call, described below. 257.It Dv SIOCGIFADDR Fa "struct ifreq *" 258Get the interface address for a protocol family. 259.It Dv SIOCGIFDSTADDR Fa "struct ifreq *" 260Get the point-to-point address for a protocol family and interface. 261.It Dv SIOCGIFBRDADDR Fa "struct ifreq *" 262Get the broadcast address for a protocol family and interface. 263.It Dv SIOCGIFDESCR Fa "struct ifreq *" 264Get the interface description, returned in the 265.Va ifru_data 266field. 267.It Dv SIOCSIFDESCR Fa "struct ifreq *" 268Set the interface description to the value of the 269.Va ifru_data 270field, limited to the size of 271.Dv IFDESCRSIZE . 272.It Dv SIOCSIFFLAGS Fa "struct ifreq *" 273Set the interface flags. 274If the interface is marked down, any processes currently routing packets 275through the interface are notified; some interfaces may be reset so that 276incoming packets are no longer received. 277When marked up again, the interface is reinitialized. 278.It Dv SIOCGIFFLAGS Fa "struct ifreq *" 279Get the interface flags. 280.It Dv SIOCGIFXFLAGS Fa "struct ifreq *" 281Get the extended interface flags. 282.It Dv SIOCGIFMTU Fa "struct ifreq *" 283Get the current MTU of the interface. 284.It Dv SIOCGIFHARDMTU Fa "struct ifreq *" 285Get the maximum hardware MTU of the interface. 286.It Dv SIOCSIFMEDIA Fa "struct ifreq *" 287Set the interface media settings. 288See 289.Xr ifmedia 4 290for possible values. 291.It Dv SIOCGIFMEDIA Fa "struct ifmediareq *" 292Get the interface media settings. 293The 294.Vt ifmediareq 295structure is as follows: 296.Bd -literal 297struct ifmediareq { 298 char ifm_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 299 int ifm_current; /* current media options */ 300 int ifm_mask; /* don't care mask */ 301 int ifm_status; /* media status */ 302 int ifm_active; /* active options */ 303 int ifm_count; /* #entries in ifm_ulist array */ 304 int *ifm_ulist; /* media words */ 305}; 306.Ed 307.Pp 308See 309.Xr ifmedia 4 310for interpreting this value. 311.It Dv SIOCSIFMETRIC Fa "struct ifreq *" 312Set the interface routing metric. 313The metric is used only by user-level routers. 314.It Dv SIOCGIFMETRIC Fa "struct ifreq *" 315Get the interface metric. 316.It Dv SIOCSIFPRIORITY Fa "struct ifreq *" 317Set the interface routing priority. 318The interface routing priority influences the resulting routing priority of 319new static routes added to the kernel using the specified interface. 320The value is in the range of 0 to 16 with smaller numbers being better. 321.It Dv SIOCGIFPRIORITY Fa "struct ifreq *" 322Get the interface priority. 323.It Dv SIOCGIFRDOMAIN Fa "struct ifreq *" 324Get the interface routing domain. 325This identifies which routing table is used for the interface. 326.It Dv SIOCAIFADDR Fa "struct ifaliasreq *" 327An interface may have more than one address associated with it 328in some protocols. 329This request provides a means to add additional addresses (or modify 330characteristics of the primary address if the default address for the 331address family is specified). 332.Pp 333Rather than making separate calls to set destination or broadcast addresses, 334or network masks (now an integral feature of multiple protocols), a separate 335structure, 336.Vt ifaliasreq , 337is used to specify all three facets simultaneously (see below). 338One would use a slightly tailored version of this structure specific 339to each family (replacing each 340.Vt sockaddr 341by one 342of the family-specific type). 343One should always set the length of a 344.Vt sockaddr , 345as described in 346.Xr ioctl 2 . 347.Pp 348The 349.Vt ifaliasreq 350structure is as follows: 351.Bd -literal 352struct ifaliasreq { 353 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 354 struct sockaddr ifra_addr; 355 struct sockaddr ifra_dstaddr; 356#define ifra_broadaddr ifra_dstaddr 357 struct sockaddr ifra_mask; 358}; 359.Ed 360.It Dv SIOCDIFADDR Fa "struct ifreq *" 361This request deletes the specified address from the list 362associated with an interface. 363It also uses the 364.Vt ifaliasreq 365structure to allow for the possibility of protocols allowing 366multiple masks or destination addresses, and also adopts the 367convention that specification of the default address means 368to delete the first address for the interface belonging to 369the address family in which the original socket was opened. 370.It Dv SIOCGIFCONF Fa "struct ifconf *" 371Get the interface configuration list. 372This request takes an 373.Vt ifconf 374structure (see below) as a value-result parameter. 375The 376.Va ifc_len 377field should be initially set to the size of the buffer 378pointed to by 379.Va ifc_buf . 380On return it will contain the length, in bytes, of the 381configuration list. 382.Pp 383Alternately, if the 384.Va ifc_len 385passed in is set to 0, 386.Dv SIOCGIFCONF 387will set 388.Va ifc_len 389to the size that 390.Va ifc_buf 391needs to be to fit the entire configuration list and will not 392fill in the other parameters. 393This is useful for determining the exact size that 394.Va ifc_buf 395needs to be in advance. 396Note, however, that this is an extension 397that not all operating systems support. 398.Bd -literal 399struct ifconf { 400 int ifc_len; /* size of associated buffer */ 401 union { 402 caddr_t ifcu_buf; 403 struct ifreq *ifcu_req; 404 } ifc_ifcu; 405#define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 406#define ifc_req ifc_ifcu.ifcu_req /* array of structures ret'd */ 407}; 408.Ed 409.It Dv SIOCIFCREATE Fa "struct ifreq *" 410Attempt to create the specified interface. 411.It Dv SIOCIFDESTROY Fa "struct ifreq *" 412Attempt to destroy the specified interface. 413.It Dv SIOCIFGCLONERS Fa "struct if_clonereq *" 414Get the list of clonable interfaces. 415This request takes an 416.Vt if_clonereq 417structure pointer (see below) as a value-result parameter. 418The 419.Va ifcr_count 420field should be set to the number of 421.Dv IFNAMSIZ Ns -sized 422strings that can fit in the buffer pointed to by 423.Va ifcr_buffer . 424On return, 425.Va ifcr_total 426will be set to the number of clonable interfaces, and the buffer pointed 427to by 428.Va ifcr_buffer 429will be filled with the names of clonable interfaces aligned on 430.Dv IFNAMSIZ 431boundaries. 432.Pp 433The 434.Vt if_clonereq 435structure is as follows: 436.Bd -literal 437struct if_clonereq { 438 int ifcr_total; /* total cloners (out) */ 439 int ifcr_count; /* room for this many in user buf */ 440 char *ifcr_buffer; /* buffer for cloner names */ 441}; 442.Ed 443.It Dv SIOCAIFGROUP Fa "struct ifgroupreq *" 444Associate the interface named by 445.Va ifgr_name 446with the interface group named by 447.Va ifgr_group . 448The 449.Vt ifgroupreq 450structure is as follows: 451.Bd -literal 452struct ifg_req { 453 char ifgrq_group[IFNAMSIZ]; 454}; 455 456struct ifgroupreq { 457 char ifgr_name[IFNAMSIZ]; 458 u_int ifgr_len; 459 union { 460 char ifgru_group[IFNAMSIZ]; 461 struct ifg_req *ifgru_groups; 462 } ifgr_ifgru; 463#define ifgr_group ifgr_ifgru.ifgru_group 464#define ifgr_groups ifgr_ifgru.ifgru_groups 465}; 466.Ed 467.It Dv SIOCGIFGROUP Fa "struct ifgroupreq *" 468Retrieve the list of groups for which an interface is a member. 469The interface is named by 470.Va ifgr_name . 471On enter, the amount of memory in which the group names will 472be written is stored in 473.Va ifgr_len , 474and the group names themselves will be written to the memory 475pointed to by 476.Va ifgr_groups . 477On return, the amount of memory actually written is returned in 478.Va ifgr_len . 479.Pp 480Alternately, if the 481.Va ifgr_len 482passed in is set to 0, 483.Dv SIOCGIFGROUP 484will set 485.Va ifgr_len 486to the size that 487.Va ifgr_groups 488needs to be to fit the entire group list and will not 489fill in the other parameters. 490This is useful for determining the exact size that 491.Va ifgr_groups 492needs to be in advance. 493.It Dv SIOCDIFGROUP Fa "struct ifgroupreq *" 494Remove the membership of the interface named by 495.Va ifgr_name 496from the group 497.Va ifgr_group . 498.El 499.Sh SEE ALSO 500.Xr netstat 1 , 501.Xr ioctl 2 , 502.Xr socket 2 , 503.Xr arp 4 , 504.Xr bridge 4 , 505.Xr ifmedia 4 , 506.Xr inet 4 , 507.Xr intro 4 , 508.Xr ip 4 , 509.Xr ip6 4 , 510.Xr lo 4 , 511.Xr mpe 4 , 512.Xr pf 4 , 513.Xr tcp 4 , 514.Xr udp 4 , 515.Xr hosts 5 , 516.Xr networks 5 , 517.Xr bgpd 8 , 518.Xr config 8 , 519.Xr ifconfig 8 , 520.Xr mrouted 8 , 521.Xr netstart 8 , 522.Xr ospfd 8 , 523.Xr ripd 8 , 524.Xr route 8 525.Sh HISTORY 526The 527.Nm 528manual appeared in 529.Bx 4.3 Tahoe . 530