1 /* 2 * Copyright (c) 1980 Regents of the University of California. 3 * All rights reserved. The Berkeley software License Agreement 4 * specifies the terms and conditions for redistribution. 5 * 6 * @(#)if.h 6.9 (Berkeley) 12/19/85 7 */ 8 9 /* 10 * Structures defining a network interface, providing a packet 11 * transport mechanism (ala level 0 of the PUP protocols). 12 * 13 * Each interface accepts output datagrams of a specified maximum 14 * length, and provides higher level routines with input datagrams 15 * received from its medium. 16 * 17 * Output occurs when the routine if_output is called, with three parameters: 18 * (*ifp->if_output)(ifp, m, dst) 19 * Here m is the mbuf chain to be sent and dst is the destination address. 20 * The output routine encapsulates the supplied datagram if necessary, 21 * and then transmits it on its medium. 22 * 23 * On input, each interface unwraps the data received by it, and either 24 * places it on the input queue of a internetwork datagram routine 25 * and posts the associated software interrupt, or passes the datagram to a raw 26 * packet input routine. 27 * 28 * Routines exist for locating interfaces by their addresses 29 * or for locating a interface on a certain network, as well as more general 30 * routing and gateway routines maintaining information used to locate 31 * interfaces. These routines live in the files if.c and route.c 32 */ 33 34 /* 35 * Structure defining a queue for a network interface. 36 * 37 * (Would like to call this struct ``if'', but C isn't PL/1.) 38 */ 39 struct ifnet { 40 char *if_name; /* name, e.g. ``en'' or ``lo'' */ 41 short if_unit; /* sub-unit for lower level driver */ 42 short if_mtu; /* maximum transmission unit */ 43 short if_flags; /* up/down, broadcast, etc. */ 44 short if_timer; /* time 'til if_watchdog called */ 45 struct ifaddr *if_addrlist; /* linked list of addresses per if */ 46 struct ifqueue { 47 struct mbuf *ifq_head; 48 struct mbuf *ifq_tail; 49 int ifq_len; 50 int ifq_maxlen; 51 int ifq_drops; 52 } if_snd; /* output queue */ 53 /* procedure handles */ 54 int (*if_init)(); /* init routine */ 55 int (*if_output)(); /* output routine */ 56 int (*if_ioctl)(); /* ioctl routine */ 57 int (*if_reset)(); /* bus reset routine */ 58 int (*if_watchdog)(); /* timer routine */ 59 /* generic interface statistics */ 60 int if_ipackets; /* packets received on interface */ 61 int if_ierrors; /* input errors on interface */ 62 int if_opackets; /* packets sent on interface */ 63 int if_oerrors; /* output errors on interface */ 64 int if_collisions; /* collisions on csma interfaces */ 65 /* end statistics */ 66 struct ifnet *if_next; 67 }; 68 69 #define IFF_UP 0x1 /* interface is up */ 70 #define IFF_BROADCAST 0x2 /* broadcast address valid */ 71 #define IFF_DEBUG 0x4 /* turn on debugging */ 72 /* was IFF_ROUTE 0x8 /* routing entry installed */ 73 #define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */ 74 #define IFF_NOTRAILERS 0x20 /* avoid use of trailers */ 75 #define IFF_RUNNING 0x40 /* resources allocated */ 76 #define IFF_NOARP 0x80 /* no address resolution protocol */ 77 /* flags set internally only: */ 78 #define IFF_CANTCHANGE (IFF_BROADCAST | IFF_POINTOPOINT | IFF_RUNNING) 79 80 /* 81 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 82 * input routines have queues of messages stored on ifqueue structures 83 * (defined above). Entries are added to and deleted from these structures 84 * by these macros, which should be called with ipl raised to splimp(). 85 */ 86 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 87 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 88 #define IF_ENQUEUE(ifq, m) { \ 89 (m)->m_act = 0; \ 90 if ((ifq)->ifq_tail == 0) \ 91 (ifq)->ifq_head = m; \ 92 else \ 93 (ifq)->ifq_tail->m_act = m; \ 94 (ifq)->ifq_tail = m; \ 95 (ifq)->ifq_len++; \ 96 } 97 #define IF_PREPEND(ifq, m) { \ 98 (m)->m_act = (ifq)->ifq_head; \ 99 if ((ifq)->ifq_tail == 0) \ 100 (ifq)->ifq_tail = (m); \ 101 (ifq)->ifq_head = (m); \ 102 (ifq)->ifq_len++; \ 103 } 104 /* 105 * Packets destined for level-1 protocol input routines 106 * have a pointer to the receiving interface prepended to the data. 107 * IF_DEQUEUEIF extracts and returns this pointer when dequeueing the packet. 108 * IF_ADJ should be used otherwise to adjust for its presence. 109 */ 110 #define IF_ADJ(m) { \ 111 (m)->m_off += sizeof(struct ifnet *); \ 112 (m)->m_len -= sizeof(struct ifnet *); \ 113 if ((m)->m_len == 0) { \ 114 struct mbuf *n; \ 115 MFREE((m), n); \ 116 (m) = n; \ 117 } \ 118 } 119 #define IF_DEQUEUEIF(ifq, m, ifp) { \ 120 (m) = (ifq)->ifq_head; \ 121 if (m) { \ 122 if (((ifq)->ifq_head = (m)->m_act) == 0) \ 123 (ifq)->ifq_tail = 0; \ 124 (m)->m_act = 0; \ 125 (ifq)->ifq_len--; \ 126 (ifp) = *(mtod((m), struct ifnet **)); \ 127 IF_ADJ(m); \ 128 } \ 129 } 130 #define IF_DEQUEUE(ifq, m) { \ 131 (m) = (ifq)->ifq_head; \ 132 if (m) { \ 133 if (((ifq)->ifq_head = (m)->m_act) == 0) \ 134 (ifq)->ifq_tail = 0; \ 135 (m)->m_act = 0; \ 136 (ifq)->ifq_len--; \ 137 } \ 138 } 139 140 #define IFQ_MAXLEN 50 141 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 142 143 /* 144 * The ifaddr structure contains information about one address 145 * of an interface. They are maintained by the different address families, 146 * are allocated and attached when an address is set, and are linked 147 * together so all addresses for an interface can be located. 148 */ 149 struct ifaddr { 150 struct sockaddr ifa_addr; /* address of interface */ 151 union { 152 struct sockaddr ifu_broadaddr; 153 struct sockaddr ifu_dstaddr; 154 } ifa_ifu; 155 #define ifa_broadaddr ifa_ifu.ifu_broadaddr /* broadcast address */ 156 #define ifa_dstaddr ifa_ifu.ifu_dstaddr /* other end of p-to-p link */ 157 struct ifnet *ifa_ifp; /* back-pointer to interface */ 158 struct ifaddr *ifa_next; /* next address for interface */ 159 }; 160 161 /* 162 * Interface request structure used for socket 163 * ioctl's. All interface ioctl's must have parameter 164 * definitions which begin with ifr_name. The 165 * remainder may be interface specific. 166 */ 167 struct ifreq { 168 #define IFNAMSIZ 16 169 char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 170 union { 171 struct sockaddr ifru_addr; 172 struct sockaddr ifru_dstaddr; 173 struct sockaddr ifru_broadaddr; 174 short ifru_flags; 175 caddr_t ifru_data; 176 } ifr_ifru; 177 #define ifr_addr ifr_ifru.ifru_addr /* address */ 178 #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 179 #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 180 #define ifr_flags ifr_ifru.ifru_flags /* flags */ 181 #define ifr_data ifr_ifru.ifru_data /* for use by interface */ 182 }; 183 184 /* 185 * Structure used in SIOCGIFCONF request. 186 * Used to retrieve interface configuration 187 * for machine (useful for programs which 188 * must know all networks accessible). 189 */ 190 struct ifconf { 191 int ifc_len; /* size of associated buffer */ 192 union { 193 caddr_t ifcu_buf; 194 struct ifreq *ifcu_req; 195 } ifc_ifcu; 196 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 197 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 198 }; 199 200 /* 201 * ARP ioctl request 202 */ 203 struct arpreq { 204 struct sockaddr arp_pa; /* protocol address */ 205 struct sockaddr arp_ha; /* hardware address */ 206 int arp_flags; /* flags */ 207 }; 208 /* arp_flags and at_flags field values */ 209 #define ATF_INUSE 1 /* entry in use */ 210 #define ATF_COM 2 /* completed entry (enaddr valid) */ 211 #define ATF_PERM 4 /* permanent entry */ 212 #define ATF_PUBL 8 /* publish entry (respond for other host) */ 213 214 #ifdef KERNEL 215 #ifdef INET 216 struct ifqueue ipintrq; /* ip packet input queue */ 217 #endif 218 struct ifqueue rawintrq; /* raw packet input queue */ 219 struct ifnet *ifnet; 220 struct ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet(), *ifa_ifwithaf(); 221 #endif 222