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.15 (Berkeley) 01/30/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 defining a queue for a network interface. 50 * 51 * (Would like to call this struct ``if'', but C isn't PL/1.) 52 */ 53 54 struct ifnet { 55 char *if_name; /* name, e.g. ``en'' or ``lo'' */ 56 short if_unit; /* sub-unit for lower level driver */ 57 short if_mtu; /* maximum transmission unit */ 58 short if_flags; /* up/down, broadcast, etc. */ 59 short if_timer; /* time 'til if_watchdog called */ 60 int if_metric; /* routing metric (external only) */ 61 struct ifaddr *if_addrlist; /* linked list of addresses per if */ 62 struct ifqueue { 63 struct mbuf *ifq_head; 64 struct mbuf *ifq_tail; 65 int ifq_len; 66 int ifq_maxlen; 67 int ifq_drops; 68 } if_snd; /* output queue */ 69 /* procedure handles */ 70 int (*if_init) /* init routine */ 71 __P((int)); 72 int (*if_output) /* output routine (enqueue) */ 73 __P((struct ifnet *, struct mbuf *, struct sockaddr *, 74 struct rtentry *)); 75 int (*if_start) /* initiate output routine */ 76 __P((struct ifnet *)); 77 int (*if_done) /* output complete routine */ 78 __P((struct ifnet *)); /* (XXX not used; fake prototype) */ 79 int (*if_ioctl) /* ioctl routine */ 80 __P((struct ifnet *, int, caddr_t)); 81 int (*if_reset) /* XXX; Unibus reset routine for vax */ 82 __P((int, int)); /* new autoconfig will permit removal */ 83 int (*if_watchdog) /* timer routine */ 84 __P((int)); 85 /* generic interface statistics */ 86 int if_ipackets; /* packets received on interface */ 87 int if_ierrors; /* input errors on interface */ 88 int if_opackets; /* packets sent on interface */ 89 int if_oerrors; /* output errors on interface */ 90 int if_collisions; /* collisions on csma interfaces */ 91 /* end statistics */ 92 struct ifnet *if_next; 93 u_char if_type; /* ethernet, tokenring, etc */ 94 u_char if_addrlen; /* media address length */ 95 u_char if_hdrlen; /* media header length */ 96 u_char if_index; /* numeric abbreviation for this if */ 97 /* more statistics here to avoid recompiling netstat */ 98 struct timeval if_lastchange; /* last updated */ 99 int if_ibytes; /* total number of octets received */ 100 int if_obytes; /* total number of octets sent */ 101 int if_imcasts; /* packets received via multicast */ 102 int if_omcasts; /* packets sent via multicast */ 103 int if_iqdrops; /* dropped on input, this interface */ 104 int if_noproto; /* destined for unsupported protocol */ 105 int if_baudrate; /* linespeed */ 106 int if_pcount; /* number of promiscuous listeners */ 107 }; 108 109 #define IFF_UP 0x1 /* interface is up */ 110 #define IFF_BROADCAST 0x2 /* broadcast address valid */ 111 #define IFF_DEBUG 0x4 /* turn on debugging */ 112 #define IFF_LOOPBACK 0x8 /* is a loopback net */ 113 #define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */ 114 #define IFF_NOTRAILERS 0x20 /* avoid use of trailers */ 115 #define IFF_RUNNING 0x40 /* resources allocated */ 116 #define IFF_NOARP 0x80 /* no address resolution protocol */ 117 /* next two not supported now, but reserved: */ 118 #define IFF_PROMISC 0x100 /* receive all packets */ 119 #define IFF_ALLMULTI 0x200 /* receive all multicast packets */ 120 #define IFF_OACTIVE 0x400 /* transmission in progress */ 121 #define IFF_SIMPLEX 0x800 /* can't hear own transmissions */ 122 #define IFF_LINK0 0x1000 /* per link layer defined bit */ 123 #define IFF_LINK1 0x2000 /* per link layer defined bit */ 124 #define IFF_LINK2 0x4000 /* per link layer defined bit */ 125 126 /* flags set internally only: */ 127 #define IFF_CANTCHANGE \ 128 (IFF_BROADCAST|IFF_POINTOPOINT|IFF_RUNNING|IFF_OACTIVE|IFF_SIMPLEX) 129 130 /* 131 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 132 * input routines have queues of messages stored on ifqueue structures 133 * (defined above). Entries are added to and deleted from these structures 134 * by these macros, which should be called with ipl raised to splimp(). 135 */ 136 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 137 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 138 #define IF_ENQUEUE(ifq, m) { \ 139 (m)->m_nextpkt = 0; \ 140 if ((ifq)->ifq_tail == 0) \ 141 (ifq)->ifq_head = m; \ 142 else \ 143 (ifq)->ifq_tail->m_nextpkt = m; \ 144 (ifq)->ifq_tail = m; \ 145 (ifq)->ifq_len++; \ 146 } 147 #define IF_PREPEND(ifq, m) { \ 148 (m)->m_nextpkt = (ifq)->ifq_head; \ 149 if ((ifq)->ifq_tail == 0) \ 150 (ifq)->ifq_tail = (m); \ 151 (ifq)->ifq_head = (m); \ 152 (ifq)->ifq_len++; \ 153 } 154 #define IF_DEQUEUE(ifq, m) { \ 155 (m) = (ifq)->ifq_head; \ 156 if (m) { \ 157 if (((ifq)->ifq_head = (m)->m_nextpkt) == 0) \ 158 (ifq)->ifq_tail = 0; \ 159 (m)->m_nextpkt = 0; \ 160 (ifq)->ifq_len--; \ 161 } \ 162 } 163 164 #define IFQ_MAXLEN 50 165 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 166 167 /* 168 * The ifaddr structure contains information about one address 169 * of an interface. They are maintained by the different address families, 170 * are allocated and attached when an address is set, and are linked 171 * together so all addresses for an interface can be located. 172 */ 173 struct ifaddr { 174 struct sockaddr *ifa_addr; /* address of interface */ 175 struct sockaddr *ifa_dstaddr; /* other end of p-to-p link */ 176 #define ifa_broadaddr ifa_dstaddr /* broadcast address interface */ 177 struct sockaddr *ifa_netmask; /* used to determine subnet */ 178 struct ifnet *ifa_ifp; /* back-pointer to interface */ 179 struct ifaddr *ifa_next; /* next address for interface */ 180 int (*ifa_rtrequest)(); /* check or clean routes (+ or -)'d */ 181 struct rtentry *ifa_rt; /* ??? for ROUTETOIF */ 182 u_short ifa_flags; /* mostly rt_flags for cloning */ 183 short ifa_refcnt; /* extra to malloc for link info */ 184 }; 185 #define IFA_ROUTE RTF_UP /* route installed */ 186 /* 187 * Interface request structure used for socket 188 * ioctl's. All interface ioctl's must have parameter 189 * definitions which begin with ifr_name. The 190 * remainder may be interface specific. 191 */ 192 struct ifreq { 193 #define IFNAMSIZ 16 194 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 195 union { 196 struct sockaddr ifru_addr; 197 struct sockaddr ifru_dstaddr; 198 struct sockaddr ifru_broadaddr; 199 short ifru_flags; 200 int ifru_metric; 201 caddr_t ifru_data; 202 } ifr_ifru; 203 #define ifr_addr ifr_ifru.ifru_addr /* address */ 204 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 205 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 206 #define ifr_flags ifr_ifru.ifru_flags /* flags */ 207 #define ifr_metric ifr_ifru.ifru_metric /* metric */ 208 #define ifr_data ifr_ifru.ifru_data /* for use by interface */ 209 }; 210 211 struct ifaliasreq { 212 char ifra_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 213 struct sockaddr ifra_addr; 214 struct sockaddr ifra_broadaddr; 215 struct sockaddr ifra_mask; 216 }; 217 218 /* 219 * Structure used in SIOCGIFCONF request. 220 * Used to retrieve interface configuration 221 * for machine (useful for programs which 222 * must know all networks accessible). 223 */ 224 struct ifconf { 225 int ifc_len; /* size of associated buffer */ 226 union { 227 caddr_t ifcu_buf; 228 struct ifreq *ifcu_req; 229 } ifc_ifcu; 230 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 231 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 232 }; 233 234 #ifdef KERNEL 235 #define IFAFREE(ifa) \ 236 if ((ifa)->ifa_refcnt <= 0) \ 237 ifafree(ifa); \ 238 else \ 239 (ifa)->ifa_refcnt--; 240 241 #include "../net/if_arp.h" 242 struct ifqueue rawintrq; /* raw packet input queue */ 243 struct ifnet *ifnet; 244 struct ifaddr *ifa_ifwithaddr __P((struct sockaddr *)), 245 *ifa_ifwithnet __P((struct sockaddr *)), 246 *ifa_ifwithdstaddr __P((struct sockaddr *)); 247 void ifafree __P((struct ifaddr *)); 248 void if_attach __P((struct ifnet *)); 249 #else KERNEL 250 #include <net/if_arp.h> 251 #endif KERNEL 252