1 /* 2 * Copyright (c) 1982, 1986 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 this notice is preserved and that due credit is given 7 * to the University of California at Berkeley. The name of the University 8 * may not be used to endorse or promote products derived from this 9 * software without specific prior written permission. This software 10 * is provided ``as is'' without express or implied warranty. 11 * 12 * @(#)if.h 7.2 (Berkeley) 12/30/87 13 */ 14 15 /* 16 * Structures defining a network interface, providing a packet 17 * transport mechanism (ala level 0 of the PUP protocols). 18 * 19 * Each interface accepts output datagrams of a specified maximum 20 * length, and provides higher level routines with input datagrams 21 * received from its medium. 22 * 23 * Output occurs when the routine if_output is called, with three parameters: 24 * (*ifp->if_output)(ifp, m, dst) 25 * Here m is the mbuf chain to be sent and dst is the destination address. 26 * The output routine encapsulates the supplied datagram if necessary, 27 * and then transmits it on its medium. 28 * 29 * On input, each interface unwraps the data received by it, and either 30 * places it on the input queue of a internetwork datagram routine 31 * and posts the associated software interrupt, or passes the datagram to a raw 32 * packet input routine. 33 * 34 * Routines exist for locating interfaces by their addresses 35 * or for locating a interface on a certain network, as well as more general 36 * routing and gateway routines maintaining information used to locate 37 * interfaces. These routines live in the files if.c and route.c 38 */ 39 40 /* 41 * Structure defining a queue for a network interface. 42 * 43 * (Would like to call this struct ``if'', but C isn't PL/1.) 44 */ 45 struct ifnet { 46 char *if_name; /* name, e.g. ``en'' or ``lo'' */ 47 short if_unit; /* sub-unit for lower level driver */ 48 short if_mtu; /* maximum transmission unit */ 49 short if_flags; /* up/down, broadcast, etc. */ 50 short if_timer; /* time 'til if_watchdog called */ 51 int if_metric; /* routing metric (external only) */ 52 struct ifaddr *if_addrlist; /* linked list of addresses per if */ 53 struct ifqueue { 54 struct mbuf *ifq_head; 55 struct mbuf *ifq_tail; 56 int ifq_len; 57 int ifq_maxlen; 58 int ifq_drops; 59 } if_snd; /* output queue */ 60 /* procedure handles */ 61 int (*if_init)(); /* init routine */ 62 int (*if_output)(); /* output routine */ 63 int (*if_ioctl)(); /* ioctl routine */ 64 int (*if_reset)(); /* bus reset routine */ 65 int (*if_watchdog)(); /* timer routine */ 66 /* generic interface statistics */ 67 int if_ipackets; /* packets received on interface */ 68 int if_ierrors; /* input errors on interface */ 69 int if_opackets; /* packets sent on interface */ 70 int if_oerrors; /* output errors on interface */ 71 int if_collisions; /* collisions on csma interfaces */ 72 /* end statistics */ 73 struct ifnet *if_next; 74 }; 75 76 #define IFF_UP 0x1 /* interface is up */ 77 #define IFF_BROADCAST 0x2 /* broadcast address valid */ 78 #define IFF_DEBUG 0x4 /* turn on debugging */ 79 #define IFF_LOOPBACK 0x8 /* is a loopback net */ 80 #define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */ 81 #define IFF_NOTRAILERS 0x20 /* avoid use of trailers */ 82 #define IFF_RUNNING 0x40 /* resources allocated */ 83 #define IFF_NOARP 0x80 /* no address resolution protocol */ 84 /* next two not supported now, but reserved: */ 85 #define IFF_PROMISC 0x100 /* receive all packets */ 86 #define IFF_ALLMULTI 0x200 /* receive all multicast packets */ 87 /* flags set internally only: */ 88 #define IFF_CANTCHANGE (IFF_BROADCAST | IFF_POINTOPOINT | IFF_RUNNING) 89 90 /* 91 * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 92 * input routines have queues of messages stored on ifqueue structures 93 * (defined above). Entries are added to and deleted from these structures 94 * by these macros, which should be called with ipl raised to splimp(). 95 */ 96 #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 97 #define IF_DROP(ifq) ((ifq)->ifq_drops++) 98 #define IF_ENQUEUE(ifq, m) { \ 99 (m)->m_act = 0; \ 100 if ((ifq)->ifq_tail == 0) \ 101 (ifq)->ifq_head = m; \ 102 else \ 103 (ifq)->ifq_tail->m_act = m; \ 104 (ifq)->ifq_tail = m; \ 105 (ifq)->ifq_len++; \ 106 } 107 #define IF_PREPEND(ifq, m) { \ 108 (m)->m_act = (ifq)->ifq_head; \ 109 if ((ifq)->ifq_tail == 0) \ 110 (ifq)->ifq_tail = (m); \ 111 (ifq)->ifq_head = (m); \ 112 (ifq)->ifq_len++; \ 113 } 114 /* 115 * Packets destined for level-1 protocol input routines 116 * have a pointer to the receiving interface prepended to the data. 117 * IF_DEQUEUEIF extracts and returns this pointer when dequeueing the packet. 118 * IF_ADJ should be used otherwise to adjust for its presence. 119 */ 120 #define IF_ADJ(m) { \ 121 (m)->m_off += sizeof(struct ifnet *); \ 122 (m)->m_len -= sizeof(struct ifnet *); \ 123 if ((m)->m_len == 0) { \ 124 struct mbuf *n; \ 125 MFREE((m), n); \ 126 (m) = n; \ 127 } \ 128 } 129 #define IF_DEQUEUEIF(ifq, m, ifp) { \ 130 (m) = (ifq)->ifq_head; \ 131 if (m) { \ 132 if (((ifq)->ifq_head = (m)->m_act) == 0) \ 133 (ifq)->ifq_tail = 0; \ 134 (m)->m_act = 0; \ 135 (ifq)->ifq_len--; \ 136 (ifp) = *(mtod((m), struct ifnet **)); \ 137 IF_ADJ(m); \ 138 } \ 139 } 140 #define IF_DEQUEUE(ifq, m) { \ 141 (m) = (ifq)->ifq_head; \ 142 if (m) { \ 143 if (((ifq)->ifq_head = (m)->m_act) == 0) \ 144 (ifq)->ifq_tail = 0; \ 145 (m)->m_act = 0; \ 146 (ifq)->ifq_len--; \ 147 } \ 148 } 149 150 #define IFQ_MAXLEN 50 151 #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 152 153 /* 154 * The ifaddr structure contains information about one address 155 * of an interface. They are maintained by the different address families, 156 * are allocated and attached when an address is set, and are linked 157 * together so all addresses for an interface can be located. 158 */ 159 struct ifaddr { 160 struct sockaddr ifa_addr; /* address of interface */ 161 union { 162 struct sockaddr ifu_broadaddr; 163 struct sockaddr ifu_dstaddr; 164 } ifa_ifu; 165 #define ifa_broadaddr ifa_ifu.ifu_broadaddr /* broadcast address */ 166 #define ifa_dstaddr ifa_ifu.ifu_dstaddr /* other end of p-to-p link */ 167 struct ifnet *ifa_ifp; /* back-pointer to interface */ 168 struct ifaddr *ifa_next; /* next address for interface */ 169 }; 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 /* 197 * Structure used in SIOCGIFCONF request. 198 * Used to retrieve interface configuration 199 * for machine (useful for programs which 200 * must know all networks accessible). 201 */ 202 struct ifconf { 203 int ifc_len; /* size of associated buffer */ 204 union { 205 caddr_t ifcu_buf; 206 struct ifreq *ifcu_req; 207 } ifc_ifcu; 208 #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 209 #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 210 }; 211 212 #ifdef KERNEL 213 #include "../net/if_arp.h" 214 struct ifqueue rawintrq; /* raw packet input queue */ 215 struct ifnet *ifnet; 216 struct ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet(); 217 struct ifaddr *ifa_ifwithdstaddr(); 218 #else KERNEL 219 #include <net/if_arp.h> 220 #endif KERNEL 221