Commit | Line | Data |
---|---|---|
cb1c44c2 | 1 | /* |
1810611d | 2 | * Copyright (c) 1980, 1986 Regents of the University of California. |
cb1c44c2 KM |
3 | * All rights reserved. The Berkeley software License Agreement |
4 | * specifies the terms and conditions for redistribution. | |
5 | * | |
1810611d | 6 | * @(#)if.c 7.1 (Berkeley) %G% |
cb1c44c2 | 7 | */ |
1bfd8df7 | 8 | |
a0369dcf JB |
9 | #include "param.h" |
10 | #include "systm.h" | |
11 | #include "socket.h" | |
f211a0b7 | 12 | #include "socketvar.h" |
a0369dcf JB |
13 | #include "protosw.h" |
14 | #include "dir.h" | |
15 | #include "user.h" | |
16 | #include "kernel.h" | |
17 | #include "ioctl.h" | |
18 | #include "errno.h" | |
19 | ||
20 | #include "if.h" | |
21 | #include "af.h" | |
1bfd8df7 | 22 | |
17fcfec5 MK |
23 | #include "ether.h" |
24 | ||
1e977657 BJ |
25 | int ifqmaxlen = IFQ_MAXLEN; |
26 | ||
ee787340 SL |
27 | /* |
28 | * Network interface utility routines. | |
29 | * | |
f211a0b7 MK |
30 | * Routines with ifa_ifwith* names take sockaddr *'s as |
31 | * parameters. | |
ee787340 SL |
32 | */ |
33 | ||
85ce71f2 BJ |
34 | ifinit() |
35 | { | |
36 | register struct ifnet *ifp; | |
37 | ||
38 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
aad26eac MK |
39 | if (ifp->if_snd.ifq_maxlen == 0) |
40 | ifp->if_snd.ifq_maxlen = ifqmaxlen; | |
5248a70b | 41 | if_slowtimo(); |
85ce71f2 BJ |
42 | } |
43 | ||
a62dd253 | 44 | #ifdef vax |
ee787340 SL |
45 | /* |
46 | * Call each interface on a Unibus reset. | |
47 | */ | |
85ce71f2 BJ |
48 | ifubareset(uban) |
49 | int uban; | |
50 | { | |
51 | register struct ifnet *ifp; | |
52 | ||
53 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
8af3ca7c | 54 | if (ifp->if_reset) |
9d6a72e7 | 55 | (*ifp->if_reset)(ifp->if_unit, uban); |
85ce71f2 | 56 | } |
14fa60f2 | 57 | #endif |
85ce71f2 | 58 | |
ee787340 SL |
59 | /* |
60 | * Attach an interface to the | |
61 | * list of "active" interfaces. | |
62 | */ | |
405c9168 BJ |
63 | if_attach(ifp) |
64 | struct ifnet *ifp; | |
65 | { | |
c4af8b24 | 66 | register struct ifnet **p = &ifnet; |
405c9168 | 67 | |
c4af8b24 BJ |
68 | while (*p) |
69 | p = &((*p)->if_next); | |
70 | *p = ifp; | |
405c9168 BJ |
71 | } |
72 | ||
ee787340 SL |
73 | /* |
74 | * Locate an interface based on a complete address. | |
75 | */ | |
4ad99bae | 76 | /*ARGSUSED*/ |
f211a0b7 MK |
77 | struct ifaddr * |
78 | ifa_ifwithaddr(addr) | |
ee787340 | 79 | struct sockaddr *addr; |
1bfd8df7 BJ |
80 | { |
81 | register struct ifnet *ifp; | |
f211a0b7 | 82 | register struct ifaddr *ifa; |
1bfd8df7 | 83 | |
ee787340 SL |
84 | #define equal(a1, a2) \ |
85 | (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0) | |
f211a0b7 MK |
86 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
87 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { | |
88 | if (ifa->ifa_addr.sa_family != addr->sa_family) | |
ee787340 | 89 | continue; |
f211a0b7 MK |
90 | if (equal(&ifa->ifa_addr, addr)) |
91 | return (ifa); | |
ee787340 | 92 | if ((ifp->if_flags & IFF_BROADCAST) && |
f211a0b7 MK |
93 | equal(&ifa->ifa_broadaddr, addr)) |
94 | return (ifa); | |
ee787340 | 95 | } |
f211a0b7 | 96 | return ((struct ifaddr *)0); |
1bfd8df7 | 97 | } |
ae674e00 KS |
98 | /* |
99 | * Locate the point to point interface with a given destination address. | |
100 | */ | |
101 | /*ARGSUSED*/ | |
102 | struct ifaddr * | |
103 | ifa_ifwithdstaddr(addr) | |
104 | struct sockaddr *addr; | |
105 | { | |
106 | register struct ifnet *ifp; | |
107 | register struct ifaddr *ifa; | |
108 | ||
109 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
110 | if (ifp->if_flags & IFF_POINTOPOINT) | |
111 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { | |
112 | if (ifa->ifa_addr.sa_family != addr->sa_family) | |
113 | continue; | |
114 | if (equal(&ifa->ifa_dstaddr, addr)) | |
115 | return (ifa); | |
116 | } | |
117 | return ((struct ifaddr *)0); | |
118 | } | |
1bfd8df7 | 119 | |
ee787340 SL |
120 | /* |
121 | * Find an interface on a specific network. If many, choice | |
122 | * is first found. | |
123 | */ | |
f211a0b7 MK |
124 | struct ifaddr * |
125 | ifa_ifwithnet(addr) | |
ee787340 SL |
126 | register struct sockaddr *addr; |
127 | { | |
128 | register struct ifnet *ifp; | |
f211a0b7 | 129 | register struct ifaddr *ifa; |
14fa60f2 | 130 | register u_int af = addr->sa_family; |
e65dcd4c | 131 | register int (*netmatch)(); |
ee787340 | 132 | |
e65dcd4c SL |
133 | if (af >= AF_MAX) |
134 | return (0); | |
135 | netmatch = afswitch[af].af_netmatch; | |
f211a0b7 MK |
136 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
137 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { | |
138 | if (ifa->ifa_addr.sa_family != addr->sa_family) | |
ee787340 | 139 | continue; |
f211a0b7 MK |
140 | if ((*netmatch)(&ifa->ifa_addr, addr)) |
141 | return (ifa); | |
ee787340 | 142 | } |
f211a0b7 | 143 | return ((struct ifaddr *)0); |
1bfd8df7 BJ |
144 | } |
145 | ||
2612fc91 | 146 | #ifdef notdef |
ee787340 SL |
147 | /* |
148 | * Find an interface using a specific address family | |
149 | */ | |
f211a0b7 MK |
150 | struct ifaddr * |
151 | ifa_ifwithaf(af) | |
ee787340 | 152 | register int af; |
8a13b737 | 153 | { |
ee787340 | 154 | register struct ifnet *ifp; |
f211a0b7 | 155 | register struct ifaddr *ifa; |
8a13b737 | 156 | |
ee787340 | 157 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
f211a0b7 MK |
158 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
159 | if (ifa->ifa_addr.sa_family == af) | |
160 | return (ifa); | |
161 | return ((struct ifaddr *)0); | |
8a13b737 | 162 | } |
2612fc91 | 163 | #endif |
f1b2fa5b | 164 | |
72e4f44e SL |
165 | /* |
166 | * Mark an interface down and notify protocols of | |
167 | * the transition. | |
af0b24db | 168 | * NOTE: must be called at splnet or eqivalent. |
72e4f44e SL |
169 | */ |
170 | if_down(ifp) | |
171 | register struct ifnet *ifp; | |
172 | { | |
f211a0b7 | 173 | register struct ifaddr *ifa; |
5248a70b | 174 | |
72e4f44e | 175 | ifp->if_flags &= ~IFF_UP; |
f211a0b7 | 176 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
aad26eac | 177 | pfctlinput(PRC_IFDOWN, &ifa->ifa_addr); |
72e4f44e | 178 | } |
de602274 SL |
179 | |
180 | /* | |
181 | * Handle interface watchdog timer routines. Called | |
182 | * from softclock, we decrement timers (if set) and | |
183 | * call the appropriate interface routine on expiration. | |
184 | */ | |
185 | if_slowtimo() | |
186 | { | |
187 | register struct ifnet *ifp; | |
188 | ||
af0b24db SL |
189 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
190 | if (ifp->if_timer == 0 || --ifp->if_timer) | |
191 | continue; | |
192 | if (ifp->if_watchdog) | |
de602274 | 193 | (*ifp->if_watchdog)(ifp->if_unit); |
af0b24db | 194 | } |
6e7edb25 | 195 | timeout(if_slowtimo, (caddr_t)0, hz / IFNET_SLOWHZ); |
de602274 | 196 | } |
0c33c832 SL |
197 | |
198 | /* | |
a62dd253 SL |
199 | * Map interface name to |
200 | * interface structure pointer. | |
0c33c832 | 201 | */ |
a62dd253 SL |
202 | struct ifnet * |
203 | ifunit(name) | |
204 | register char *name; | |
0c33c832 | 205 | { |
0c33c832 | 206 | register char *cp; |
a62dd253 SL |
207 | register struct ifnet *ifp; |
208 | int unit; | |
0c33c832 | 209 | |
a62dd253 | 210 | for (cp = name; cp < name + IFNAMSIZ && *cp; cp++) |
0c33c832 SL |
211 | if (*cp >= '0' && *cp <= '9') |
212 | break; | |
a62dd253 SL |
213 | if (*cp == '\0' || cp == name + IFNAMSIZ) |
214 | return ((struct ifnet *)0); | |
ff50f144 | 215 | unit = *cp - '0'; |
0c33c832 | 216 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
a62dd253 | 217 | if (bcmp(ifp->if_name, name, (unsigned)(cp - name))) |
0c33c832 SL |
218 | continue; |
219 | if (unit == ifp->if_unit) | |
a62dd253 | 220 | break; |
0c33c832 | 221 | } |
a62dd253 SL |
222 | return (ifp); |
223 | } | |
224 | ||
225 | /* | |
226 | * Interface ioctls. | |
227 | */ | |
f211a0b7 MK |
228 | ifioctl(so, cmd, data) |
229 | struct socket *so; | |
a62dd253 SL |
230 | int cmd; |
231 | caddr_t data; | |
232 | { | |
233 | register struct ifnet *ifp; | |
234 | register struct ifreq *ifr; | |
0c33c832 | 235 | |
0c33c832 SL |
236 | switch (cmd) { |
237 | ||
a62dd253 SL |
238 | case SIOCGIFCONF: |
239 | return (ifconf(cmd, data)); | |
0c33c832 | 240 | |
799358ac | 241 | #if defined(INET) && NETHER > 0 |
17fcfec5 MK |
242 | case SIOCSARP: |
243 | case SIOCDARP: | |
244 | if (!suser()) | |
245 | return (u.u_error); | |
246 | /* FALL THROUGH */ | |
247 | case SIOCGARP: | |
248 | return (arpioctl(cmd, data)); | |
249 | #endif | |
a62dd253 SL |
250 | } |
251 | ifr = (struct ifreq *)data; | |
252 | ifp = ifunit(ifr->ifr_name); | |
253 | if (ifp == 0) | |
254 | return (ENXIO); | |
255 | switch (cmd) { | |
0c33c832 | 256 | |
0c33c832 SL |
257 | case SIOCGIFFLAGS: |
258 | ifr->ifr_flags = ifp->if_flags; | |
259 | break; | |
260 | ||
484ee22e MK |
261 | case SIOCGIFMETRIC: |
262 | ifr->ifr_metric = ifp->if_metric; | |
263 | break; | |
264 | ||
81889e84 | 265 | case SIOCSIFFLAGS: |
beb1ee55 JB |
266 | if (!suser()) |
267 | return (u.u_error); | |
81889e84 SL |
268 | if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { |
269 | int s = splimp(); | |
270 | if_down(ifp); | |
271 | splx(s); | |
272 | } | |
f211a0b7 MK |
273 | ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | |
274 | (ifr->ifr_flags &~ IFF_CANTCHANGE); | |
aad26eac MK |
275 | if (ifp->if_ioctl) |
276 | (void) (*ifp->if_ioctl)(ifp, cmd, data); | |
81889e84 SL |
277 | break; |
278 | ||
484ee22e MK |
279 | case SIOCSIFMETRIC: |
280 | if (!suser()) | |
281 | return (u.u_error); | |
282 | ifp->if_metric = ifr->ifr_metric; | |
283 | break; | |
284 | ||
0c33c832 | 285 | default: |
f211a0b7 | 286 | if (so->so_proto == 0) |
a62dd253 | 287 | return (EOPNOTSUPP); |
f211a0b7 MK |
288 | return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, |
289 | cmd, data, ifp)); | |
0c33c832 SL |
290 | } |
291 | return (0); | |
292 | } | |
293 | ||
294 | /* | |
295 | * Return interface configuration | |
296 | * of system. List may be used | |
297 | * in later ioctl's (above) to get | |
298 | * other information. | |
299 | */ | |
9e9695c7 | 300 | /*ARGSUSED*/ |
0c33c832 SL |
301 | ifconf(cmd, data) |
302 | int cmd; | |
303 | caddr_t data; | |
304 | { | |
305 | register struct ifconf *ifc = (struct ifconf *)data; | |
306 | register struct ifnet *ifp = ifnet; | |
f211a0b7 | 307 | register struct ifaddr *ifa; |
9b956fa5 SL |
308 | register char *cp, *ep; |
309 | struct ifreq ifr, *ifrp; | |
0c33c832 SL |
310 | int space = ifc->ifc_len, error = 0; |
311 | ||
9b956fa5 SL |
312 | ifrp = ifc->ifc_req; |
313 | ep = ifr.ifr_name + sizeof (ifr.ifr_name) - 2; | |
0c33c832 | 314 | for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) { |
9b956fa5 SL |
315 | bcopy(ifp->if_name, ifr.ifr_name, sizeof (ifr.ifr_name) - 2); |
316 | for (cp = ifr.ifr_name; cp < ep && *cp; cp++) | |
0c33c832 | 317 | ; |
9b956fa5 | 318 | *cp++ = '0' + ifp->if_unit; *cp = '\0'; |
f211a0b7 MK |
319 | if ((ifa = ifp->if_addrlist) == 0) { |
320 | bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); | |
321 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, sizeof (ifr)); | |
322 | if (error) | |
323 | break; | |
324 | space -= sizeof (ifr), ifrp++; | |
325 | } else | |
326 | for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) { | |
327 | ifr.ifr_addr = ifa->ifa_addr; | |
328 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, sizeof (ifr)); | |
329 | if (error) | |
330 | break; | |
331 | space -= sizeof (ifr), ifrp++; | |
332 | } | |
0c33c832 SL |
333 | } |
334 | ifc->ifc_len -= space; | |
335 | return (error); | |
336 | } |