use shorts to move data
[unix-history] / usr / src / sys / net / if.c
CommitLineData
e65dcd4c 1/* if.c 4.15 82/05/04 */
1bfd8df7
BJ
2
3#include "../h/param.h"
4#include "../h/systm.h"
ee787340 5#include "../h/socket.h"
72e4f44e 6#include "../h/protosw.h"
8a13b737
BJ
7#include "../net/in.h"
8#include "../net/in_systm.h"
1bfd8df7 9#include "../net/if.h"
ee787340 10#include "../net/af.h"
1bfd8df7 11
1e977657
BJ
12int ifqmaxlen = IFQ_MAXLEN;
13
ee787340
SL
14/*
15 * Network interface utility routines.
16 *
17 * Routines with if_ifwith* names take sockaddr *'s as
18 * parameters. Other routines take value parameters,
19 * e.g. if_ifwithnet takes the network number.
20 */
21
85ce71f2
BJ
22ifinit()
23{
24 register struct ifnet *ifp;
25
26 for (ifp = ifnet; ifp; ifp = ifp->if_next)
1e977657 27 if (ifp->if_init) {
ee787340 28 (*ifp->if_init)(ifp->if_unit);
1e977657
BJ
29 if (ifp->if_snd.ifq_maxlen == 0)
30 ifp->if_snd.ifq_maxlen = ifqmaxlen;
31 }
85ce71f2
BJ
32}
33
ee787340
SL
34/*
35 * Call each interface on a Unibus reset.
36 */
85ce71f2
BJ
37ifubareset(uban)
38 int uban;
39{
40 register struct ifnet *ifp;
41
42 for (ifp = ifnet; ifp; ifp = ifp->if_next)
43 if (ifp->if_ubareset)
44 (*ifp->if_ubareset)(uban);
45}
46
ee787340
SL
47/*
48 * Attach an interface to the
49 * list of "active" interfaces.
50 */
405c9168
BJ
51if_attach(ifp)
52 struct ifnet *ifp;
53{
c4af8b24 54 register struct ifnet **p = &ifnet;
405c9168
BJ
55
56COUNT(IF_ATTACH);
c4af8b24
BJ
57 while (*p)
58 p = &((*p)->if_next);
59 *p = ifp;
405c9168
BJ
60}
61
ee787340
SL
62/*
63 * Locate an interface based on a complete address.
64 */
4ad99bae
BJ
65/*ARGSUSED*/
66struct ifnet *
ee787340
SL
67if_ifwithaddr(addr)
68 struct sockaddr *addr;
1bfd8df7
BJ
69{
70 register struct ifnet *ifp;
71
4ad99bae 72COUNT(IF_IFWITHADDR);
ee787340
SL
73#define equal(a1, a2) \
74 (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0)
75 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
76 if (ifp->if_addr.sa_family != addr->sa_family)
77 continue;
78 if (equal(&ifp->if_addr, addr))
79 break;
80 if ((ifp->if_flags & IFF_BROADCAST) &&
81 equal(&ifp->if_broadaddr, addr))
1bfd8df7 82 break;
ee787340 83 }
1bfd8df7
BJ
84 return (ifp);
85}
86
ee787340
SL
87/*
88 * Find an interface on a specific network. If many, choice
89 * is first found.
90 */
4ad99bae 91struct ifnet *
ee787340
SL
92if_ifwithnet(addr)
93 register struct sockaddr *addr;
94{
95 register struct ifnet *ifp;
96 register int af = addr->sa_family;
e65dcd4c 97 register int (*netmatch)();
ee787340 98
e65dcd4c
SL
99 if (af >= AF_MAX)
100 return (0);
101 netmatch = afswitch[af].af_netmatch;
ee787340
SL
102 for (ifp = ifnet; ifp; ifp = ifp->if_next) {
103 if (af != ifp->if_addr.sa_family)
104 continue;
105 if ((*netmatch)(addr, &ifp->if_addr))
106 break;
107 }
108 return (ifp);
109}
110
111/*
112 * As above, but parameter is network number.
113 */
114struct ifnet *
115if_ifonnetof(net)
116 register int net;
1bfd8df7
BJ
117{
118 register struct ifnet *ifp;
1bfd8df7 119
1bfd8df7
BJ
120 for (ifp = ifnet; ifp; ifp = ifp->if_next)
121 if (ifp->if_net == net)
122 break;
1bfd8df7
BJ
123 return (ifp);
124}
125
ee787340
SL
126/*
127 * Find an interface using a specific address family
128 */
8a13b737 129struct ifnet *
ee787340
SL
130if_ifwithaf(af)
131 register int af;
8a13b737 132{
ee787340 133 register struct ifnet *ifp;
8a13b737 134
ee787340
SL
135 for (ifp = ifnet; ifp; ifp = ifp->if_next)
136 if (ifp->if_addr.sa_family == af)
137 break;
138 return (ifp);
8a13b737 139}
f1b2fa5b 140
72e4f44e
SL
141/*
142 * Mark an interface down and notify protocols of
143 * the transition.
144 */
145if_down(ifp)
146 register struct ifnet *ifp;
147{
148 ifp->if_flags &= ~IFF_UP;
149 pfctlinput(PRC_IFDOWN, (caddr_t)&ifp->if_addr);
150}
151
ee787340
SL
152/*
153 * Formulate an Internet address from network + host. Used in
154 * building addresses stored in the ifnet structure.
155 */
f1b2fa5b
BJ
156struct in_addr
157if_makeaddr(net, host)
158 int net, host;
159{
160 u_long addr;
161
162 if (net < 128)
85ce71f2 163 addr = (net << 24) | host;
f1b2fa5b 164 else if (net < 65536)
85ce71f2 165 addr = (net << 16) | host;
f1b2fa5b 166 else
85ce71f2 167 addr = (net << 8) | host;
72c68388 168#ifdef vax
f1b2fa5b 169 addr = htonl(addr);
72c68388 170#endif
f1b2fa5b
BJ
171 return (*(struct in_addr *)&addr);
172}
f6311fb6
SL
173
174/*
175 * Initialize an interface's routing
176 * table entry according to the network.
177 * INTERNET SPECIFIC.
178 */
179if_rtinit(ifp, flags)
180 register struct ifnet *ifp;
181 int flags;
182{
183 struct sockaddr_in sin;
184
185 if (ifp->if_flags & IFF_ROUTE)
186 return;
187 bzero((caddr_t)&sin, sizeof (sin));
188 sin.sin_family = AF_INET;
189 sin.sin_addr = if_makeaddr(ifp->if_net, 0);
fc74f0c9 190 rtinit(&sin, &ifp->if_addr, flags);
f6311fb6 191}