123158Smckusick /* 229062Smckusick * Copyright (c) 1982, 1986 Regents of the University of California. 333183Sbostic * All rights reserved. 423158Smckusick * 533183Sbostic * Redistribution and use in source and binary forms are permitted 6*34844Sbostic * provided that the above copyright notice and this paragraph are 7*34844Sbostic * duplicated in all such forms and that any documentation, 8*34844Sbostic * advertising materials, and other materials related to such 9*34844Sbostic * distribution and use acknowledge that the software was developed 10*34844Sbostic * by the University of California, Berkeley. The name of the 11*34844Sbostic * University may not be used to endorse or promote products derived 12*34844Sbostic * from this software without specific prior written permission. 13*34844Sbostic * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR 14*34844Sbostic * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED 15*34844Sbostic * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. 1633183Sbostic * 17*34844Sbostic * @(#)if.h 7.3 (Berkeley) 06/27/88 1823158Smckusick */ 194945Swnj 204945Swnj /* 215104Swnj * Structures defining a network interface, providing a packet 225104Swnj * transport mechanism (ala level 0 of the PUP protocols). 235104Swnj * 245104Swnj * Each interface accepts output datagrams of a specified maximum 255104Swnj * length, and provides higher level routines with input datagrams 265104Swnj * received from its medium. 275104Swnj * 285104Swnj * Output occurs when the routine if_output is called, with three parameters: 296333Ssam * (*ifp->if_output)(ifp, m, dst) 306333Ssam * Here m is the mbuf chain to be sent and dst is the destination address. 316333Ssam * The output routine encapsulates the supplied datagram if necessary, 326333Ssam * and then transmits it on its medium. 335104Swnj * 345104Swnj * On input, each interface unwraps the data received by it, and either 355104Swnj * places it on the input queue of a internetwork datagram routine 365104Swnj * and posts the associated software interrupt, or passes the datagram to a raw 375104Swnj * packet input routine. 385104Swnj * 396333Ssam * Routines exist for locating interfaces by their addresses 405104Swnj * or for locating a interface on a certain network, as well as more general 415104Swnj * routing and gateway routines maintaining information used to locate 426333Ssam * interfaces. These routines live in the files if.c and route.c 435083Swnj */ 445083Swnj 455083Swnj /* 465083Swnj * Structure defining a queue for a network interface. 474945Swnj * 484945Swnj * (Would like to call this struct ``if'', but C isn't PL/1.) 494945Swnj */ 504945Swnj struct ifnet { 515171Swnj char *if_name; /* name, e.g. ``en'' or ``lo'' */ 524945Swnj short if_unit; /* sub-unit for lower level driver */ 534945Swnj short if_mtu; /* maximum transmission unit */ 546333Ssam short if_flags; /* up/down, broadcast, etc. */ 557264Ssam short if_timer; /* time 'til if_watchdog called */ 5626092Skarels int if_metric; /* routing metric (external only) */ 5718408Skarels struct ifaddr *if_addrlist; /* linked list of addresses per if */ 585104Swnj struct ifqueue { 595104Swnj struct mbuf *ifq_head; 605104Swnj struct mbuf *ifq_tail; 616207Swnj int ifq_len; 626207Swnj int ifq_maxlen; 636207Swnj int ifq_drops; 645104Swnj } if_snd; /* output queue */ 655104Swnj /* procedure handles */ 665104Swnj int (*if_init)(); /* init routine */ 675083Swnj int (*if_output)(); /* output routine */ 6813049Ssam int (*if_ioctl)(); /* ioctl routine */ 698972Sroot int (*if_reset)(); /* bus reset routine */ 707264Ssam int (*if_watchdog)(); /* timer routine */ 715104Swnj /* generic interface statistics */ 725171Swnj int if_ipackets; /* packets received on interface */ 735171Swnj int if_ierrors; /* input errors on interface */ 745171Swnj int if_opackets; /* packets sent on interface */ 755171Swnj int if_oerrors; /* output errors on interface */ 765104Swnj int if_collisions; /* collisions on csma interfaces */ 775104Swnj /* end statistics */ 784951Swnj struct ifnet *if_next; 794945Swnj }; 804945Swnj 816333Ssam #define IFF_UP 0x1 /* interface is up */ 826333Ssam #define IFF_BROADCAST 0x2 /* broadcast address valid */ 836333Ssam #define IFF_DEBUG 0x4 /* turn on debugging */ 8426092Skarels #define IFF_LOOPBACK 0x8 /* is a loopback net */ 856924Swnj #define IFF_POINTOPOINT 0x10 /* interface is point-to-point link */ 8613049Ssam #define IFF_NOTRAILERS 0x20 /* avoid use of trailers */ 8713053Ssam #define IFF_RUNNING 0x40 /* resources allocated */ 8814866Ssam #define IFF_NOARP 0x80 /* no address resolution protocol */ 8928153Skarels /* next two not supported now, but reserved: */ 9028153Skarels #define IFF_PROMISC 0x100 /* receive all packets */ 9128153Skarels #define IFF_ALLMULTI 0x200 /* receive all multicast packets */ 9226092Skarels /* flags set internally only: */ 9318408Skarels #define IFF_CANTCHANGE (IFF_BROADCAST | IFF_POINTOPOINT | IFF_RUNNING) 946333Ssam 955104Swnj /* 965104Swnj * Output queues (ifp->if_snd) and internetwork datagram level (pup level 1) 975104Swnj * input routines have queues of messages stored on ifqueue structures 985104Swnj * (defined above). Entries are added to and deleted from these structures 995104Swnj * by these macros, which should be called with ipl raised to splimp(). 1005104Swnj */ 1016207Swnj #define IF_QFULL(ifq) ((ifq)->ifq_len >= (ifq)->ifq_maxlen) 1026207Swnj #define IF_DROP(ifq) ((ifq)->ifq_drops++) 1035083Swnj #define IF_ENQUEUE(ifq, m) { \ 1045083Swnj (m)->m_act = 0; \ 1055083Swnj if ((ifq)->ifq_tail == 0) \ 1065160Swnj (ifq)->ifq_head = m; \ 1075083Swnj else \ 1085083Swnj (ifq)->ifq_tail->m_act = m; \ 1095160Swnj (ifq)->ifq_tail = m; \ 1106207Swnj (ifq)->ifq_len++; \ 1115083Swnj } 1125613Swnj #define IF_PREPEND(ifq, m) { \ 1135613Swnj (m)->m_act = (ifq)->ifq_head; \ 1146087Sroot if ((ifq)->ifq_tail == 0) \ 1156087Sroot (ifq)->ifq_tail = (m); \ 1165613Swnj (ifq)->ifq_head = (m); \ 1176207Swnj (ifq)->ifq_len++; \ 1185613Swnj } 11924772Skarels /* 12024772Skarels * Packets destined for level-1 protocol input routines 12124772Skarels * have a pointer to the receiving interface prepended to the data. 12224772Skarels * IF_DEQUEUEIF extracts and returns this pointer when dequeueing the packet. 12324772Skarels * IF_ADJ should be used otherwise to adjust for its presence. 12424772Skarels */ 12524772Skarels #define IF_ADJ(m) { \ 12624772Skarels (m)->m_off += sizeof(struct ifnet *); \ 12724772Skarels (m)->m_len -= sizeof(struct ifnet *); \ 12824772Skarels if ((m)->m_len == 0) { \ 12924772Skarels struct mbuf *n; \ 13024772Skarels MFREE((m), n); \ 13124772Skarels (m) = n; \ 13224772Skarels } \ 13324772Skarels } 13424772Skarels #define IF_DEQUEUEIF(ifq, m, ifp) { \ 13524772Skarels (m) = (ifq)->ifq_head; \ 13624772Skarels if (m) { \ 13724772Skarels if (((ifq)->ifq_head = (m)->m_act) == 0) \ 13824772Skarels (ifq)->ifq_tail = 0; \ 13924772Skarels (m)->m_act = 0; \ 14024772Skarels (ifq)->ifq_len--; \ 14124772Skarels (ifp) = *(mtod((m), struct ifnet **)); \ 14224772Skarels IF_ADJ(m); \ 14324772Skarels } \ 14424772Skarels } 1455083Swnj #define IF_DEQUEUE(ifq, m) { \ 1465083Swnj (m) = (ifq)->ifq_head; \ 1475083Swnj if (m) { \ 1485083Swnj if (((ifq)->ifq_head = (m)->m_act) == 0) \ 1495083Swnj (ifq)->ifq_tail = 0; \ 1505083Swnj (m)->m_act = 0; \ 1516207Swnj (ifq)->ifq_len--; \ 1525083Swnj } \ 1535083Swnj } 1544945Swnj 1556207Swnj #define IFQ_MAXLEN 50 1567264Ssam #define IFNET_SLOWHZ 1 /* granularity is 1 second */ 1576207Swnj 15811576Ssam /* 15918408Skarels * The ifaddr structure contains information about one address 16018408Skarels * of an interface. They are maintained by the different address families, 16118408Skarels * are allocated and attached when an address is set, and are linked 16218408Skarels * together so all addresses for an interface can be located. 16318408Skarels */ 16418408Skarels struct ifaddr { 16518408Skarels struct sockaddr ifa_addr; /* address of interface */ 16618408Skarels union { 16718408Skarels struct sockaddr ifu_broadaddr; 16818408Skarels struct sockaddr ifu_dstaddr; 16918408Skarels } ifa_ifu; 17018408Skarels #define ifa_broadaddr ifa_ifu.ifu_broadaddr /* broadcast address */ 17118408Skarels #define ifa_dstaddr ifa_ifu.ifu_dstaddr /* other end of p-to-p link */ 17218408Skarels struct ifnet *ifa_ifp; /* back-pointer to interface */ 17318408Skarels struct ifaddr *ifa_next; /* next address for interface */ 17418408Skarels }; 17518408Skarels 17618408Skarels /* 17713049Ssam * Interface request structure used for socket 17813049Ssam * ioctl's. All interface ioctl's must have parameter 17913049Ssam * definitions which begin with ifr_name. The 18013049Ssam * remainder may be interface specific. 18111576Ssam */ 18211576Ssam struct ifreq { 18313049Ssam #define IFNAMSIZ 16 18413049Ssam char ifr_name[IFNAMSIZ]; /* if name, e.g. "en0" */ 18511576Ssam union { 18611576Ssam struct sockaddr ifru_addr; 18711576Ssam struct sockaddr ifru_dstaddr; 18816387Skarels struct sockaddr ifru_broadaddr; 18911576Ssam short ifru_flags; 19026092Skarels int ifru_metric; 19113049Ssam caddr_t ifru_data; 19211576Ssam } ifr_ifru; 19311576Ssam #define ifr_addr ifr_ifru.ifru_addr /* address */ 19411576Ssam #define ifr_dstaddr ifr_ifru.ifru_dstaddr /* other end of p-to-p link */ 19516387Skarels #define ifr_broadaddr ifr_ifru.ifru_broadaddr /* broadcast address */ 19611576Ssam #define ifr_flags ifr_ifru.ifru_flags /* flags */ 19726092Skarels #define ifr_metric ifr_ifru.ifru_metric /* metric */ 19813049Ssam #define ifr_data ifr_ifru.ifru_data /* for use by interface */ 19911576Ssam }; 20011576Ssam 20111576Ssam /* 20211576Ssam * Structure used in SIOCGIFCONF request. 20311576Ssam * Used to retrieve interface configuration 20411576Ssam * for machine (useful for programs which 20511576Ssam * must know all networks accessible). 20611576Ssam */ 20711576Ssam struct ifconf { 20811576Ssam int ifc_len; /* size of associated buffer */ 20911576Ssam union { 21011576Ssam caddr_t ifcu_buf; 21111576Ssam struct ifreq *ifcu_req; 21211576Ssam } ifc_ifcu; 21311576Ssam #define ifc_buf ifc_ifcu.ifcu_buf /* buffer address */ 21411576Ssam #define ifc_req ifc_ifcu.ifcu_req /* array of structures returned */ 21511576Ssam }; 21611576Ssam 2174945Swnj #ifdef KERNEL 21825971Skarels #include "../net/if_arp.h" 2196333Ssam struct ifqueue rawintrq; /* raw packet input queue */ 2204945Swnj struct ifnet *ifnet; 22128943Skarels struct ifaddr *ifa_ifwithaddr(), *ifa_ifwithnet(); 22227197Skarels struct ifaddr *ifa_ifwithdstaddr(); 22325971Skarels #else KERNEL 22425971Skarels #include <net/if_arp.h> 22525971Skarels #endif KERNEL 226