Commit | Line | Data |
---|---|---|
cb1c44c2 KM |
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.c 6.7 (Berkeley) %G% | |
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) | |
1e977657 | 39 | if (ifp->if_init) { |
ee787340 | 40 | (*ifp->if_init)(ifp->if_unit); |
1e977657 BJ |
41 | if (ifp->if_snd.ifq_maxlen == 0) |
42 | ifp->if_snd.ifq_maxlen = ifqmaxlen; | |
43 | } | |
5248a70b | 44 | if_slowtimo(); |
85ce71f2 BJ |
45 | } |
46 | ||
a62dd253 | 47 | #ifdef vax |
ee787340 SL |
48 | /* |
49 | * Call each interface on a Unibus reset. | |
50 | */ | |
85ce71f2 BJ |
51 | ifubareset(uban) |
52 | int uban; | |
53 | { | |
54 | register struct ifnet *ifp; | |
55 | ||
56 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
8af3ca7c | 57 | if (ifp->if_reset) |
9d6a72e7 | 58 | (*ifp->if_reset)(ifp->if_unit, uban); |
85ce71f2 | 59 | } |
14fa60f2 | 60 | #endif |
85ce71f2 | 61 | |
ee787340 SL |
62 | /* |
63 | * Attach an interface to the | |
64 | * list of "active" interfaces. | |
65 | */ | |
405c9168 BJ |
66 | if_attach(ifp) |
67 | struct ifnet *ifp; | |
68 | { | |
c4af8b24 | 69 | register struct ifnet **p = &ifnet; |
405c9168 | 70 | |
c4af8b24 BJ |
71 | while (*p) |
72 | p = &((*p)->if_next); | |
73 | *p = ifp; | |
405c9168 BJ |
74 | } |
75 | ||
ee787340 SL |
76 | /* |
77 | * Locate an interface based on a complete address. | |
78 | */ | |
4ad99bae | 79 | /*ARGSUSED*/ |
f211a0b7 MK |
80 | struct ifaddr * |
81 | ifa_ifwithaddr(addr) | |
ee787340 | 82 | struct sockaddr *addr; |
1bfd8df7 BJ |
83 | { |
84 | register struct ifnet *ifp; | |
f211a0b7 | 85 | register struct ifaddr *ifa; |
1bfd8df7 | 86 | |
ee787340 SL |
87 | #define equal(a1, a2) \ |
88 | (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0) | |
f211a0b7 MK |
89 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
90 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { | |
91 | if (ifa->ifa_addr.sa_family != addr->sa_family) | |
ee787340 | 92 | continue; |
f211a0b7 MK |
93 | if (equal(&ifa->ifa_addr, addr)) |
94 | return (ifa); | |
ee787340 | 95 | if ((ifp->if_flags & IFF_BROADCAST) && |
f211a0b7 MK |
96 | equal(&ifa->ifa_broadaddr, addr)) |
97 | return (ifa); | |
ee787340 | 98 | } |
f211a0b7 | 99 | return ((struct ifaddr *)0); |
1bfd8df7 BJ |
100 | } |
101 | ||
ee787340 SL |
102 | /* |
103 | * Find an interface on a specific network. If many, choice | |
104 | * is first found. | |
105 | */ | |
f211a0b7 MK |
106 | struct ifaddr * |
107 | ifa_ifwithnet(addr) | |
ee787340 SL |
108 | register struct sockaddr *addr; |
109 | { | |
110 | register struct ifnet *ifp; | |
f211a0b7 | 111 | register struct ifaddr *ifa; |
14fa60f2 | 112 | register u_int af = addr->sa_family; |
e65dcd4c | 113 | register int (*netmatch)(); |
ee787340 | 114 | |
e65dcd4c SL |
115 | if (af >= AF_MAX) |
116 | return (0); | |
117 | netmatch = afswitch[af].af_netmatch; | |
f211a0b7 MK |
118 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
119 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) { | |
120 | if (ifa->ifa_addr.sa_family != addr->sa_family) | |
ee787340 | 121 | continue; |
f211a0b7 MK |
122 | if ((*netmatch)(&ifa->ifa_addr, addr)) |
123 | return (ifa); | |
ee787340 | 124 | } |
f211a0b7 | 125 | return ((struct ifaddr *)0); |
1bfd8df7 BJ |
126 | } |
127 | ||
ee787340 SL |
128 | /* |
129 | * Find an interface using a specific address family | |
130 | */ | |
f211a0b7 MK |
131 | struct ifaddr * |
132 | ifa_ifwithaf(af) | |
ee787340 | 133 | register int af; |
8a13b737 | 134 | { |
ee787340 | 135 | register struct ifnet *ifp; |
f211a0b7 | 136 | register struct ifaddr *ifa; |
8a13b737 | 137 | |
ee787340 | 138 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
f211a0b7 MK |
139 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
140 | if (ifa->ifa_addr.sa_family == af) | |
141 | return (ifa); | |
142 | return ((struct ifaddr *)0); | |
8a13b737 | 143 | } |
f1b2fa5b | 144 | |
72e4f44e SL |
145 | /* |
146 | * Mark an interface down and notify protocols of | |
147 | * the transition. | |
af0b24db | 148 | * NOTE: must be called at splnet or eqivalent. |
72e4f44e SL |
149 | */ |
150 | if_down(ifp) | |
151 | register struct ifnet *ifp; | |
152 | { | |
f211a0b7 | 153 | register struct ifaddr *ifa; |
5248a70b | 154 | |
72e4f44e | 155 | ifp->if_flags &= ~IFF_UP; |
f211a0b7 MK |
156 | for (ifa = ifp->if_addrlist; ifa; ifa = ifa->ifa_next) |
157 | pfctlinput(PRC_IFDOWN, (caddr_t)&ifa->ifa_addr); | |
72e4f44e | 158 | } |
de602274 SL |
159 | |
160 | /* | |
161 | * Handle interface watchdog timer routines. Called | |
162 | * from softclock, we decrement timers (if set) and | |
163 | * call the appropriate interface routine on expiration. | |
164 | */ | |
165 | if_slowtimo() | |
166 | { | |
167 | register struct ifnet *ifp; | |
168 | ||
af0b24db SL |
169 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
170 | if (ifp->if_timer == 0 || --ifp->if_timer) | |
171 | continue; | |
172 | if (ifp->if_watchdog) | |
de602274 | 173 | (*ifp->if_watchdog)(ifp->if_unit); |
af0b24db | 174 | } |
6e7edb25 | 175 | timeout(if_slowtimo, (caddr_t)0, hz / IFNET_SLOWHZ); |
de602274 | 176 | } |
0c33c832 SL |
177 | |
178 | /* | |
a62dd253 SL |
179 | * Map interface name to |
180 | * interface structure pointer. | |
0c33c832 | 181 | */ |
a62dd253 SL |
182 | struct ifnet * |
183 | ifunit(name) | |
184 | register char *name; | |
0c33c832 | 185 | { |
0c33c832 | 186 | register char *cp; |
a62dd253 SL |
187 | register struct ifnet *ifp; |
188 | int unit; | |
0c33c832 | 189 | |
a62dd253 | 190 | for (cp = name; cp < name + IFNAMSIZ && *cp; cp++) |
0c33c832 SL |
191 | if (*cp >= '0' && *cp <= '9') |
192 | break; | |
a62dd253 SL |
193 | if (*cp == '\0' || cp == name + IFNAMSIZ) |
194 | return ((struct ifnet *)0); | |
ff50f144 | 195 | unit = *cp - '0'; |
0c33c832 | 196 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
a62dd253 | 197 | if (bcmp(ifp->if_name, name, (unsigned)(cp - name))) |
0c33c832 SL |
198 | continue; |
199 | if (unit == ifp->if_unit) | |
a62dd253 | 200 | break; |
0c33c832 | 201 | } |
a62dd253 SL |
202 | return (ifp); |
203 | } | |
204 | ||
205 | /* | |
206 | * Interface ioctls. | |
207 | */ | |
f211a0b7 MK |
208 | ifioctl(so, cmd, data) |
209 | struct socket *so; | |
a62dd253 SL |
210 | int cmd; |
211 | caddr_t data; | |
212 | { | |
213 | register struct ifnet *ifp; | |
214 | register struct ifreq *ifr; | |
0c33c832 | 215 | |
0c33c832 SL |
216 | switch (cmd) { |
217 | ||
a62dd253 SL |
218 | case SIOCGIFCONF: |
219 | return (ifconf(cmd, data)); | |
0c33c832 | 220 | |
17fcfec5 MK |
221 | #if defined(INET) && NETHER > 0 |
222 | case SIOCSARP: | |
223 | case SIOCDARP: | |
224 | if (!suser()) | |
225 | return (u.u_error); | |
226 | /* FALL THROUGH */ | |
227 | case SIOCGARP: | |
228 | return (arpioctl(cmd, data)); | |
229 | #endif | |
a62dd253 SL |
230 | } |
231 | ifr = (struct ifreq *)data; | |
232 | ifp = ifunit(ifr->ifr_name); | |
233 | if (ifp == 0) | |
234 | return (ENXIO); | |
235 | switch (cmd) { | |
0c33c832 | 236 | |
0c33c832 SL |
237 | case SIOCGIFFLAGS: |
238 | ifr->ifr_flags = ifp->if_flags; | |
239 | break; | |
240 | ||
81889e84 SL |
241 | case SIOCSIFFLAGS: |
242 | if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { | |
243 | int s = splimp(); | |
244 | if_down(ifp); | |
245 | splx(s); | |
246 | } | |
f211a0b7 MK |
247 | ifp->if_flags = (ifp->if_flags & IFF_CANTCHANGE) | |
248 | (ifr->ifr_flags &~ IFF_CANTCHANGE); | |
81889e84 SL |
249 | break; |
250 | ||
0c33c832 | 251 | default: |
f211a0b7 | 252 | if (so->so_proto == 0) |
a62dd253 | 253 | return (EOPNOTSUPP); |
f211a0b7 MK |
254 | return ((*so->so_proto->pr_usrreq)(so, PRU_CONTROL, |
255 | cmd, data, ifp)); | |
0c33c832 SL |
256 | } |
257 | return (0); | |
258 | } | |
259 | ||
260 | /* | |
261 | * Return interface configuration | |
262 | * of system. List may be used | |
263 | * in later ioctl's (above) to get | |
264 | * other information. | |
265 | */ | |
9e9695c7 | 266 | /*ARGSUSED*/ |
0c33c832 SL |
267 | ifconf(cmd, data) |
268 | int cmd; | |
269 | caddr_t data; | |
270 | { | |
271 | register struct ifconf *ifc = (struct ifconf *)data; | |
272 | register struct ifnet *ifp = ifnet; | |
f211a0b7 | 273 | register struct ifaddr *ifa; |
9b956fa5 SL |
274 | register char *cp, *ep; |
275 | struct ifreq ifr, *ifrp; | |
0c33c832 SL |
276 | int space = ifc->ifc_len, error = 0; |
277 | ||
9b956fa5 SL |
278 | ifrp = ifc->ifc_req; |
279 | ep = ifr.ifr_name + sizeof (ifr.ifr_name) - 2; | |
0c33c832 | 280 | for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) { |
9b956fa5 SL |
281 | bcopy(ifp->if_name, ifr.ifr_name, sizeof (ifr.ifr_name) - 2); |
282 | for (cp = ifr.ifr_name; cp < ep && *cp; cp++) | |
0c33c832 | 283 | ; |
9b956fa5 | 284 | *cp++ = '0' + ifp->if_unit; *cp = '\0'; |
f211a0b7 MK |
285 | if ((ifa = ifp->if_addrlist) == 0) { |
286 | bzero((caddr_t)&ifr.ifr_addr, sizeof(ifr.ifr_addr)); | |
287 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, sizeof (ifr)); | |
288 | if (error) | |
289 | break; | |
290 | space -= sizeof (ifr), ifrp++; | |
291 | } else | |
292 | for ( ; space > sizeof (ifr) && ifa; ifa = ifa->ifa_next) { | |
293 | ifr.ifr_addr = ifa->ifa_addr; | |
294 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, sizeof (ifr)); | |
295 | if (error) | |
296 | break; | |
297 | space -= sizeof (ifr), ifrp++; | |
298 | } | |
0c33c832 SL |
299 | } |
300 | ifc->ifc_len -= space; | |
301 | return (error); | |
302 | } |