1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms are permitted 6 * provided that the above copyright notice and this paragraph are 7 * duplicated in all such forms and that any documentation, 8 * advertising materials, and other materials related to such 9 * distribution and use acknowledge that the software was developed 10 * by the University of California, Berkeley. The name of the 11 * University may not be used to endorse or promote products derived 12 * from this software without specific prior written permission. 13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 16 * 17 * @(#)if.h 7.6 (Berkeley) 09/20/89 18 */ 19 20 /* 21 * Structures defining a network interface, providing a packet 22 * transport mechanism (ala level 0 of the PUP protocols). 23 * 24 * Each interface accepts output datagrams of a specified maximum 25 * length, and provides higher level routines with input datagrams 26 * received from its medium. 27 * 28 * Output occurs when the routine if_output is called, with three parameters: 29 * (*ifp->if_output)(ifp, m, dst) 30 * Here m is the mbuf chain to be sent and dst is the destination address. 31 * The output routine encapsulates the supplied datagram if necessary, 32 * and then transmits it on its medium. 33 * 34 * On input, each interface unwraps the data received by it, and either 35 * places it on the input queue of a internetwork datagram routine 36 * and posts the associated software interrupt, or passes the datagram to a raw 37 * packet input routine. 38 * 39 * Routines exist for locating interfaces by their addresses 40 * or for locating a interface on a certain network, as well as more general 41 * routing and gateway routines maintaining information used to locate 42 * interfaces. These routines live in the files if.c and route.c 43 */ 44 #ifndef _TIME_ /* XXX fast fix for SNMP, going away soon */ 45 #ifdef KERNEL 46 #include "../sys/time.h" 47 #else 48 #include <sys/time.h> 49 #endif 50 #endif 51 52 /* 53 * Structure defining a queue for a network interface. 54 * 55 * (Would like to call this struct ``if'', but C isn't PL/1.) 56 */ 57 58 struct ifnet { 59 char *if_name; /* name, e.g. ``en'' or ``lo'' */ 60 short if_unit; /* sub-unit for lower level driver */ 61 short if_mtu; /* maximum transmission unit */ 62 short if_flags; /* up/down, broadcast, etc. */ 63 short if_timer; /* time 'til if_watchdog called */ 64 int if_metric; /* routing metric (external only) */ 65 struct ifaddr *if_addrlist; /* linked list of addresses per if */ 66 struct ifqueue { 67 struct mbuf *ifq_head; 68 struct mbuf *ifq_tail; 69 int ifq_len; 70 int ifq_maxlen; 71 int ifq_drops; 72 } if_snd; /* output queue */ 73 /* procedure handles */ 74 int (*if_init)(); /* init routine */ 75 int (*if_output)(); /* output routine (enqueue) */ 76 int (*if_start)(); /* initiate output routine */ 77 int (*if_done)(); /* output complete routine */ 78 int (*if_ioctl)(); /* ioctl routine */ 79 int (*if_reset)(); /* bus reset routine */ 80 int (*if_watchdog)(); /* timer routine */ 81 /* generic interface statistics */ 82 int if_ipackets; /* packets received on interface */ 83 int if_ierrors; /* input errors on interface */ 84 int if_opackets; /* packets sent on interface */ 85 int if_oerrors; /* output errors on interface */ 86 int if_collisions; /* collisions on csma interfaces */ 87 /* end statistics */ 88 struct ifnet *if_next; 89 u_char if_type; /* ethernet, tokenring, etc */ 90 u_char if_addrlen; /* media address length */ 91 u_char if_hdrlen; /* media header length */ 92 /* more statistics here to avoid recompiling netstat */ 93 struct timeval if_lastchange; /* last updated */ 94 int if_ibytes; /* total number of octets received */ 95 int if_obytes; /* total number of octets sent */ 96 int if_imcasts; /* packets received via multicast */ 97 int if_omcasts; /* packets sent via multicast */ 98 int if_iqdrops; /* dropped on input, this interface */ 99 int if_noproto; /* destined for unsupported protocol */ 100 int if_baudrate; /* linespeed */ 101 }; 102 103 #define IFF_UP 0x1 /* interface is up */ 104 #define IFF_BROADCAST 0x2 /* broadcast address valid */ 105 #define IFF_DEBUG 0x4 /* turn on debugging */ 106 #define IFF_LOOPBACK 0x8 /* is a loopback net */ 107 #define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */ 108 #define IFF_NOTRAILERS 0x20 /* avoid use of trailers */ 109 #define IFF_RUNNING 0x40 /* resources allocated */ 110 #define IFF_NOARP 0x80 /* no address resolution protocol */ 111 /* next two not supported now, but reserved: */ 112 #define IFF_PROMISC 0x100 /* receive all packets */ 113 #define IFF_ALLMULTI 0x200 /* receive all multicast packets */ 114 #define IFF_OACTIVE 0x400 /* transmission in progress */ 115 #define IFF_SIMPLEX 0x800 /* can't hear own transmissions */ 116 117 /* flags set internally only: */ 118 #define IFF_CANTCHANGE (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE) 119 120 /* 121 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 122 * input routines have queues of messages stored on ifqueue structures 123 * (defined above). Entries are added to and deleted from these structures 124 * by these macros, which should be called with ipl raised to splimp(). 125 */ 126 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 127 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 128 #define IF_ENQUEUE(ifq, m) { \ 129 (m)->m_act = 0; \ 130 if ((ifq)->ifq_tail == 0) \ 131 (ifq)->ifq_head = m; \ 132 else \ 133 (ifq)->ifq_tail->m_act = m; \ 134 (ifq)->ifq_tail = m; \ 135 (ifq)->ifq_len++; \ 136 } 137 #define IF_PREPEND(ifq, m) { \ 138 (m)->m_act = (ifq)->ifq_head; \ 139 if ((ifq)->ifq_tail == 0) \ 140 (ifq)->ifq_tail = (m); \ 141 (ifq)->ifq_head = (m); \ 142 (ifq)->ifq_len++; \ 143 } 144 #define IF_DEQUEUE(ifq, m) { \ 145 (m) = (ifq)->ifq_head; \ 146 if (m) { \ 147 if (((ifq)->ifq_head = (m)->m_act) == 0) \ 148 (ifq)->ifq_tail = 0; \ 149 (m)->m_act = 0; \ 150 (ifq)->ifq_len--; \ 151 } \ 152 } 153 154 #define IFQ_MAXLEN 50 155 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 156 157 /* 158 * The ifaddr structure contains information about one address 159 * of an interface. They are maintained by the different address families, 160 * are allocated and attached when an address is set, and are linked 161 * together so all addresses for an interface can be located. 162 */ 163 struct ifaddr { 164 struct sockaddr *ifa_addr; /* address of interface */ 165 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 166 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 167 struct sockaddr *ifa_netmask; /* used to determine subnet */ 168 struct ifnet *ifa_ifp; /* back-pointer to interface */ 169 struct ifaddr *ifa_next; /* next address for interface */ 170 }; 171 /* 172 * Interface request structure used for socket 173 * ioctl's. All interface ioctl's must have parameter 174 * definitions which begin with ifr_name. The 175 * remainder may be interface specific. 176 */ 177 struct ifreq { 178 #define IFNAMSIZ 16 179 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 180 union { 181 struct sockaddr ifru_addr; 182 struct sockaddr ifru_dstaddr; 183 struct sockaddr ifru_broadaddr; 184 short ifru_flags; 185 int ifru_metric; 186 caddr_t ifru_data; 187 } ifr_ifru; 188 #define ifr_addr ifr_ifru.ifru_addr /* address */ 189 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 190 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 191 #define ifr_flags ifr_ifru.ifru_flags /* flags */ 192 #define ifr_metric ifr_ifru.ifru_metric /* metric */ 193 #define ifr_data ifr_ifru.ifru_data /* for use by interface */ 194 }; 195 196 struct ifaliasreq { 197 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 198 struct sockaddr ifra_addr; 199 struct sockaddr ifra_broadaddr; 200 struct sockaddr ifra_mask; 201 }; 202 203 /* 204 * Structure used in SIOCGIFCONF request. 205 * Used to retrieve interface configuration 206 * for machine (useful for programs which 207 * must know all networks accessible). 208 */ 209 struct ifconf { 210 int ifc_len; /* size of associated buffer */ 211 union { 212 caddr_t ifcu_buf; 213 struct ifreq *ifcu_req; 214 } ifc_ifcu; 215 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 216 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 217 }; 218 219 #ifdef KERNEL 220 #include "../net/if_arp.h" 221 struct ifqueue rawintrq; /* raw packet input queue */ 222 struct ifnet *ifnet; 223 struct ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet(); 224 struct ifaddr *ifa_ifwithdstaddr(); 225 #else KERNEL 226 #include <net/if_arp.h> 227 #endif KERNEL 228