1 /* 2 * Copyright (c) 1982, 1986, 1989 Regents of the University of California. 3 * All rights reserved. 4 * 5 * %sccs.include.redist.c% 6 * 7 * @(#)if.h 7.16 (Berkeley) 02/18/92 8 */ 9 10 /* 11 * Structures defining a network interface, providing a packet 12 * transport mechanism (ala level 0 of the PUP protocols). 13 * 14 * Each interface accepts output datagrams of a specified maximum 15 * length, and provides higher level routines with input datagrams 16 * received from its medium. 17 * 18 * Output occurs when the routine if_output is called, with three parameters: 19 * (*ifp->if_output)(ifp, m, dst, rt) 20 * Here m is the mbuf chain to be sent and dst is the destination address. 21 * The output routine encapsulates the supplied datagram if necessary, 22 * and then transmits it on its medium. 23 * 24 * On input, each interface unwraps the data received by it, and either 25 * places it on the input queue of a internetwork datagram routine 26 * and posts the associated software interrupt, or passes the datagram to a raw 27 * packet input routine. 28 * 29 * Routines exist for locating interfaces by their addresses 30 * or for locating a interface on a certain network, as well as more general 31 * routing and gateway routines maintaining information used to locate 32 * interfaces. These routines live in the files if.c and route.c 33 */ 34 #ifndef _TIME_ /* XXX fast fix for SNMP, going away soon */ 35 #ifdef KERNEL 36 #include "../sys/time.h" 37 #else 38 #include <sys/time.h> 39 #endif 40 #endif 41 42 #ifdef __STDC__ 43 /* 44 * Forward structure declarations for function prototypes [sic]. 45 */ 46 struct rtentry; 47 #endif 48 /* 49 * Structure describing information about an interface 50 * which may be of interest to management entities. 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 struct ifnet *if_next; /* all struct ifnets are chained */ 61 struct ifaddr *if_addrlist; /* linked list of addresses per if */ 62 int if_pcount; /* number of promiscuous listeners */ 63 u_short if_index; /* numeric abbreviation for this if */ 64 short if_unit; /* sub-unit for lower level driver */ 65 short if_timer; /* time 'til if_watchdog called */ 66 short if_flags; /* up/down, broadcast, etc. */ 67 struct if_data { 68 /* generic interface information */ 69 short ifi_mtu; /* maximum transmission unit */ 70 u_char ifi_type; /* ethernet, tokenring, etc */ 71 u_char ifi_addrlen; /* media address length */ 72 u_char ifi_hdrlen; /* media header length */ 73 int ifi_metric; /* routing metric (external only) */ 74 int ifi_baudrate; /* linespeed */ 75 /* volatile statistics */ 76 int ifi_ipackets; /* packets received on interface */ 77 int ifi_ierrors; /* input errors on interface */ 78 int ifi_opackets; /* packets sent on interface */ 79 int ifi_oerrors; /* output errors on interface */ 80 int ifi_collisions; /* collisions on csma interfaces */ 81 int ifi_ibytes; /* total number of octets received */ 82 int ifi_obytes; /* total number of octets sent */ 83 int ifi_imcasts; /* packets received via multicast */ 84 int ifi_omcasts; /* packets sent via multicast */ 85 int ifi_iqdrops; /* dropped on input, this interface */ 86 int ifi_noproto; /* destined for unsupported protocol */ 87 struct timeval ifi_lastchange;/* last updated */ 88 } if_data; 89 /* procedure handles */ 90 int (*if_init) /* init routine */ 91 __P((int)); 92 int (*if_output) /* output routine (enqueue) */ 93 __P((struct ifnet *, struct mbuf *, struct sockaddr *, 94 struct rtentry *)); 95 int (*if_start) /* initiate output routine */ 96 __P((struct ifnet *)); 97 int (*if_done) /* output complete routine */ 98 __P((struct ifnet *)); /* (XXX not used; fake prototype) */ 99 int (*if_ioctl) /* ioctl routine */ 100 __P((struct ifnet *, int, caddr_t)); 101 int (*if_reset) /* XXX; Unibus reset routine for vax */ 102 __P((int, int)); /* new autoconfig will permit removal */ 103 int (*if_watchdog) /* timer routine */ 104 __P((int)); 105 struct ifqueue { 106 struct mbuf *ifq_head; 107 struct mbuf *ifq_tail; 108 int ifq_len; 109 int ifq_maxlen; 110 int ifq_drops; 111 } if_snd; /* output queue */ 112 }; 113 #define if_mtu if_data.ifi_mtu 114 #define if_type if_data.ifi_type 115 #define if_addrlen if_data.ifi_addrlen 116 #define if_hdrlen if_data.ifi_hdrlen 117 #define if_metric if_data.ifi_metric 118 #define if_baudrate if_data.ifi_baudrate 119 #define if_ipackets if_data.ifi_ipackets 120 #define if_ierrors if_data.ifi_ierrors 121 #define if_opackets if_data.ifi_opackets 122 #define if_oerrors if_data.ifi_oerrors 123 #define if_collisions if_data.ifi_collisions 124 #define if_ibytes if_data.ifi_ibytes 125 #define if_obytes if_data.ifi_obytes 126 #define if_imcasts if_data.ifi_imcasts 127 #define if_omcasts if_data.ifi_omcasts 128 #define if_iqdrops if_data.ifi_iqdrops 129 #define if_noproto if_data.ifi_noproto 130 #define if_lastchange if_data.ifi_lastchange 131 132 #define IFF_UP 0x1 /* interface is up */ 133 #define IFF_BROADCAST 0x2 /* broadcast address valid */ 134 #define IFF_DEBUG 0x4 /* turn on debugging */ 135 #define IFF_LOOPBACK 0x8 /* is a loopback net */ 136 #define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */ 137 #define IFF_NOTRAILERS 0x20 /* avoid use of trailers */ 138 #define IFF_RUNNING 0x40 /* resources allocated */ 139 #define IFF_NOARP 0x80 /* no address resolution protocol */ 140 /* next two not supported now, but reserved: */ 141 #define IFF_PROMISC 0x100 /* receive all packets */ 142 #define IFF_ALLMULTI 0x200 /* receive all multicast packets */ 143 #define IFF_OACTIVE 0x400 /* transmission in progress */ 144 #define IFF_SIMPLEX 0x800 /* can't hear own transmissions */ 145 #define IFF_LINK0 0x1000 /* per link layer defined bit */ 146 #define IFF_LINK1 0x2000 /* per link layer defined bit */ 147 #define IFF_LINK2 0x4000 /* per link layer defined bit */ 148 149 /* flags set internally only: */ 150 #define IFF_CANTCHANGE \ 151 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|IFF_SIMPLEX) 152 153 /* 154 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 155 * input routines have queues of messages stored on ifqueue structures 156 * (defined above). Entries are added to and deleted from these structures 157 * by these macros, which should be called with ipl raised to splimp(). 158 */ 159 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 160 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 161 #define IF_ENQUEUE(ifq, m) { \ 162 (m)->m_nextpkt = 0; \ 163 if ((ifq)->ifq_tail == 0) \ 164 (ifq)->ifq_head = m; \ 165 else \ 166 (ifq)->ifq_tail->m_nextpkt = m; \ 167 (ifq)->ifq_tail = m; \ 168 (ifq)->ifq_len++; \ 169 } 170 #define IF_PREPEND(ifq, m) { \ 171 (m)->m_nextpkt = (ifq)->ifq_head; \ 172 if ((ifq)->ifq_tail == 0) \ 173 (ifq)->ifq_tail = (m); \ 174 (ifq)->ifq_head = (m); \ 175 (ifq)->ifq_len++; \ 176 } 177 #define IF_DEQUEUE(ifq, m) { \ 178 (m) = (ifq)->ifq_head; \ 179 if (m) { \ 180 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \ 181 (ifq)->ifq_tail = 0; \ 182 (m)->m_nextpkt = 0; \ 183 (ifq)->ifq_len--; \ 184 } \ 185 } 186 187 #define IFQ_MAXLEN 50 188 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 189 190 /* 191 * The ifaddr structure contains information about one address 192 * of an interface. They are maintained by the different address families, 193 * are allocated and attached when an address is set, and are linked 194 * together so all addresses for an interface can be located. 195 */ 196 struct ifaddr { 197 struct sockaddr *ifa_addr; /* address of interface */ 198 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 199 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 200 struct sockaddr *ifa_netmask; /* used to determine subnet */ 201 struct ifnet *ifa_ifp; /* back-pointer to interface */ 202 struct ifaddr *ifa_next; /* next address for interface */ 203 int (*ifa_rtrequest)(); /* check or clean routes (+ or -)'d */ 204 u_short ifa_flags; /* mostly rt_flags for cloning */ 205 short ifa_refcnt; /* extra to malloc for link info */ 206 int ifa_metric; /* cost of going out this interface */ 207 /* struct rtentry *ifa_rt; /* XXXX for ROUTETOIF ????? */ 208 }; 209 #define IFA_ROUTE RTF_UP /* route installed */ 210 211 /* 212 * Message format for use in obtaining information about interfaces 213 * from getkerninfo and the routing socket 214 */ 215 struct if_msghdr { 216 u_short ifm_msglen; /* to skip over non-understood messages */ 217 u_char ifm_version; /* future binary compatability */ 218 u_char ifm_type; /* message type */ 219 int ifm_addrs; /* like rtm_addrs */ 220 int ifm_flags; /* value of if_flags */ 221 u_short ifm_index; /* index for associated ifp */ 222 struct if_data ifm_data;/* statistics and other data about if */ 223 }; 224 225 /* 226 * Message format for use in obtaining information about interface addresses 227 * from getkerninfo and the routing socket 228 */ 229 struct ifa_msghdr { 230 u_short ifam_msglen; /* to skip over non-understood messages */ 231 u_char ifam_version; /* future binary compatability */ 232 u_char ifam_type; /* message type */ 233 int ifam_addrs; /* like rtm_addrs */ 234 int ifam_flags; /* value of ifa_flags */ 235 u_short ifam_index; /* index for associated ifp */ 236 int ifam_metric; /* value of ifa_metric */ 237 }; 238 239 /* 240 * Interface request structure used for socket 241 * ioctl's. All interface ioctl's must have parameter 242 * definitions which begin with ifr_name. The 243 * remainder may be interface specific. 244 */ 245 struct ifreq { 246 #define IFNAMSIZ 16 247 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 248 union { 249 struct sockaddr ifru_addr; 250 struct sockaddr ifru_dstaddr; 251 struct sockaddr ifru_broadaddr; 252 short ifru_flags; 253 int ifru_metric; 254 caddr_t ifru_data; 255 } ifr_ifru; 256 #define ifr_addr ifr_ifru.ifru_addr /* address */ 257 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 258 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 259 #define ifr_flags ifr_ifru.ifru_flags /* flags */ 260 #define ifr_metric ifr_ifru.ifru_metric /* metric */ 261 #define ifr_data ifr_ifru.ifru_data /* for use by interface */ 262 }; 263 264 struct ifaliasreq { 265 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 266 struct sockaddr ifra_addr; 267 struct sockaddr ifra_broadaddr; 268 struct sockaddr ifra_mask; 269 }; 270 271 /* 272 * Structure used in SIOCGIFCONF request. 273 * Used to retrieve interface configuration 274 * for machine (useful for programs which 275 * must know all networks accessible). 276 */ 277 struct ifconf { 278 int ifc_len; /* size of associated buffer */ 279 union { 280 caddr_t ifcu_buf; 281 struct ifreq *ifcu_req; 282 } ifc_ifcu; 283 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 284 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 285 }; 286 287 #ifdef KERNEL 288 #define IFAFREE(ifa) \ 289 if ((ifa)->ifa_refcnt <= 0) \ 290 ifafree(ifa); \ 291 else \ 292 (ifa)->ifa_refcnt--; 293 294 #include "../net/if_arp.h" 295 struct ifqueue rawintrq; /* raw packet input queue */ 296 struct ifnet *ifnet; 297 struct ifaddr *ifa_ifwithaddr __P((struct sockaddr *)), 298 *ifa_ifwithnet __P((struct sockaddr *)), 299 *ifa_ifwithdstaddr __P((struct sockaddr *)); 300 void ifafree __P((struct ifaddr *)); 301 void if_attach __P((struct ifnet *)); 302 #else KERNEL 303 #include <net/if_arp.h> 304 #endif KERNEL 305