Commit | Line | Data |
---|---|---|
17fcfec5 | 1 | /* if.c 6.4 84/03/22 */ |
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" |
a62dd253 SL |
7 | #include "../h/dir.h" |
8 | #include "../h/user.h" | |
fb16fdca | 9 | #include "../h/kernel.h" |
0c33c832 SL |
10 | #include "../h/ioctl.h" |
11 | #include "../h/errno.h" | |
fb16fdca | 12 | |
1bfd8df7 | 13 | #include "../net/if.h" |
ee787340 | 14 | #include "../net/af.h" |
1bfd8df7 | 15 | |
17fcfec5 MK |
16 | #include "ether.h" |
17 | ||
1e977657 BJ |
18 | int ifqmaxlen = IFQ_MAXLEN; |
19 | ||
ee787340 SL |
20 | /* |
21 | * Network interface utility routines. | |
22 | * | |
23 | * Routines with if_ifwith* names take sockaddr *'s as | |
24 | * parameters. Other routines take value parameters, | |
25 | * e.g. if_ifwithnet takes the network number. | |
26 | */ | |
27 | ||
85ce71f2 BJ |
28 | ifinit() |
29 | { | |
30 | register struct ifnet *ifp; | |
31 | ||
32 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
1e977657 | 33 | if (ifp->if_init) { |
ee787340 | 34 | (*ifp->if_init)(ifp->if_unit); |
1e977657 BJ |
35 | if (ifp->if_snd.ifq_maxlen == 0) |
36 | ifp->if_snd.ifq_maxlen = ifqmaxlen; | |
37 | } | |
5248a70b | 38 | if_slowtimo(); |
85ce71f2 BJ |
39 | } |
40 | ||
a62dd253 | 41 | #ifdef vax |
ee787340 SL |
42 | /* |
43 | * Call each interface on a Unibus reset. | |
44 | */ | |
85ce71f2 BJ |
45 | ifubareset(uban) |
46 | int uban; | |
47 | { | |
48 | register struct ifnet *ifp; | |
49 | ||
50 | for (ifp = ifnet; ifp; ifp = ifp->if_next) | |
8af3ca7c | 51 | if (ifp->if_reset) |
9d6a72e7 | 52 | (*ifp->if_reset)(ifp->if_unit, uban); |
85ce71f2 | 53 | } |
14fa60f2 | 54 | #endif |
85ce71f2 | 55 | |
ee787340 SL |
56 | /* |
57 | * Attach an interface to the | |
58 | * list of "active" interfaces. | |
59 | */ | |
405c9168 BJ |
60 | if_attach(ifp) |
61 | struct ifnet *ifp; | |
62 | { | |
c4af8b24 | 63 | register struct ifnet **p = &ifnet; |
405c9168 | 64 | |
c4af8b24 BJ |
65 | while (*p) |
66 | p = &((*p)->if_next); | |
67 | *p = ifp; | |
405c9168 BJ |
68 | } |
69 | ||
ee787340 SL |
70 | /* |
71 | * Locate an interface based on a complete address. | |
72 | */ | |
4ad99bae BJ |
73 | /*ARGSUSED*/ |
74 | struct ifnet * | |
ee787340 SL |
75 | if_ifwithaddr(addr) |
76 | struct sockaddr *addr; | |
1bfd8df7 BJ |
77 | { |
78 | register struct ifnet *ifp; | |
79 | ||
ee787340 SL |
80 | #define equal(a1, a2) \ |
81 | (bcmp((caddr_t)((a1)->sa_data), (caddr_t)((a2)->sa_data), 14) == 0) | |
82 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { | |
83 | if (ifp->if_addr.sa_family != addr->sa_family) | |
84 | continue; | |
85 | if (equal(&ifp->if_addr, addr)) | |
86 | break; | |
87 | if ((ifp->if_flags & IFF_BROADCAST) && | |
88 | equal(&ifp->if_broadaddr, addr)) | |
1bfd8df7 | 89 | break; |
ee787340 | 90 | } |
1bfd8df7 BJ |
91 | return (ifp); |
92 | } | |
93 | ||
ee787340 SL |
94 | /* |
95 | * Find an interface on a specific network. If many, choice | |
96 | * is first found. | |
97 | */ | |
4ad99bae | 98 | struct ifnet * |
ee787340 SL |
99 | if_ifwithnet(addr) |
100 | register struct sockaddr *addr; | |
101 | { | |
102 | register struct ifnet *ifp; | |
14fa60f2 | 103 | register u_int af = addr->sa_family; |
e65dcd4c | 104 | register int (*netmatch)(); |
ee787340 | 105 | |
e65dcd4c SL |
106 | if (af >= AF_MAX) |
107 | return (0); | |
108 | netmatch = afswitch[af].af_netmatch; | |
ee787340 SL |
109 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
110 | if (af != ifp->if_addr.sa_family) | |
111 | continue; | |
112 | if ((*netmatch)(addr, &ifp->if_addr)) | |
113 | break; | |
114 | } | |
115 | return (ifp); | |
116 | } | |
117 | ||
118 | /* | |
119 | * As above, but parameter is network number. | |
120 | */ | |
121 | struct ifnet * | |
122 | if_ifonnetof(net) | |
123 | register int net; | |
1bfd8df7 BJ |
124 | { |
125 | register struct ifnet *ifp; | |
1bfd8df7 | 126 | |
1bfd8df7 BJ |
127 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
128 | if (ifp->if_net == net) | |
129 | break; | |
1bfd8df7 BJ |
130 | return (ifp); |
131 | } | |
132 | ||
ee787340 SL |
133 | /* |
134 | * Find an interface using a specific address family | |
135 | */ | |
8a13b737 | 136 | struct ifnet * |
ee787340 SL |
137 | if_ifwithaf(af) |
138 | register int af; | |
8a13b737 | 139 | { |
ee787340 | 140 | register struct ifnet *ifp; |
8a13b737 | 141 | |
ee787340 SL |
142 | for (ifp = ifnet; ifp; ifp = ifp->if_next) |
143 | if (ifp->if_addr.sa_family == af) | |
144 | break; | |
145 | return (ifp); | |
8a13b737 | 146 | } |
f1b2fa5b | 147 | |
72e4f44e SL |
148 | /* |
149 | * Mark an interface down and notify protocols of | |
150 | * the transition. | |
af0b24db | 151 | * NOTE: must be called at splnet or eqivalent. |
72e4f44e SL |
152 | */ |
153 | if_down(ifp) | |
154 | register struct ifnet *ifp; | |
155 | { | |
5248a70b | 156 | |
72e4f44e SL |
157 | ifp->if_flags &= ~IFF_UP; |
158 | pfctlinput(PRC_IFDOWN, (caddr_t)&ifp->if_addr); | |
159 | } | |
de602274 SL |
160 | |
161 | /* | |
162 | * Handle interface watchdog timer routines. Called | |
163 | * from softclock, we decrement timers (if set) and | |
164 | * call the appropriate interface routine on expiration. | |
165 | */ | |
166 | if_slowtimo() | |
167 | { | |
168 | register struct ifnet *ifp; | |
169 | ||
af0b24db SL |
170 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
171 | if (ifp->if_timer == 0 || --ifp->if_timer) | |
172 | continue; | |
173 | if (ifp->if_watchdog) | |
de602274 | 174 | (*ifp->if_watchdog)(ifp->if_unit); |
af0b24db | 175 | } |
6e7edb25 | 176 | timeout(if_slowtimo, (caddr_t)0, hz / IFNET_SLOWHZ); |
de602274 | 177 | } |
0c33c832 SL |
178 | |
179 | /* | |
a62dd253 SL |
180 | * Map interface name to |
181 | * interface structure pointer. | |
0c33c832 | 182 | */ |
a62dd253 SL |
183 | struct ifnet * |
184 | ifunit(name) | |
185 | register char *name; | |
0c33c832 | 186 | { |
0c33c832 | 187 | register char *cp; |
a62dd253 SL |
188 | register struct ifnet *ifp; |
189 | int unit; | |
0c33c832 | 190 | |
a62dd253 | 191 | for (cp = name; cp < name + IFNAMSIZ && *cp; cp++) |
0c33c832 SL |
192 | if (*cp >= '0' && *cp <= '9') |
193 | break; | |
a62dd253 SL |
194 | if (*cp == '\0' || cp == name + IFNAMSIZ) |
195 | return ((struct ifnet *)0); | |
ff50f144 | 196 | unit = *cp - '0'; |
0c33c832 | 197 | for (ifp = ifnet; ifp; ifp = ifp->if_next) { |
a62dd253 | 198 | if (bcmp(ifp->if_name, name, (unsigned)(cp - name))) |
0c33c832 SL |
199 | continue; |
200 | if (unit == ifp->if_unit) | |
a62dd253 | 201 | break; |
0c33c832 | 202 | } |
a62dd253 SL |
203 | return (ifp); |
204 | } | |
205 | ||
206 | /* | |
207 | * Interface ioctls. | |
208 | */ | |
209 | ifioctl(cmd, data) | |
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 | |
230 | ||
0c33c832 | 231 | case SIOCSIFADDR: |
a62dd253 SL |
232 | case SIOCSIFFLAGS: |
233 | case SIOCSIFDSTADDR: | |
234 | if (!suser()) | |
235 | return (u.u_error); | |
0c33c832 | 236 | break; |
a62dd253 SL |
237 | } |
238 | ifr = (struct ifreq *)data; | |
239 | ifp = ifunit(ifr->ifr_name); | |
240 | if (ifp == 0) | |
241 | return (ENXIO); | |
242 | switch (cmd) { | |
0c33c832 | 243 | |
a62dd253 SL |
244 | case SIOCGIFADDR: |
245 | ifr->ifr_addr = ifp->if_addr; | |
0c33c832 SL |
246 | break; |
247 | ||
a62dd253 | 248 | case SIOCGIFDSTADDR: |
0c33c832 SL |
249 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) |
250 | return (EINVAL); | |
a62dd253 | 251 | ifr->ifr_dstaddr = ifp->if_dstaddr; |
0c33c832 SL |
252 | break; |
253 | ||
254 | case SIOCGIFFLAGS: | |
255 | ifr->ifr_flags = ifp->if_flags; | |
256 | break; | |
257 | ||
81889e84 SL |
258 | case SIOCSIFFLAGS: |
259 | if (ifp->if_flags & IFF_UP && (ifr->ifr_flags & IFF_UP) == 0) { | |
260 | int s = splimp(); | |
261 | if_down(ifp); | |
262 | splx(s); | |
263 | } | |
264 | ifp->if_flags = ifr->ifr_flags; | |
265 | break; | |
266 | ||
0c33c832 | 267 | default: |
a62dd253 SL |
268 | if (ifp->if_ioctl == 0) |
269 | return (EOPNOTSUPP); | |
81889e84 | 270 | return ((*ifp->if_ioctl)(ifp, cmd, data)); |
0c33c832 SL |
271 | } |
272 | return (0); | |
273 | } | |
274 | ||
275 | /* | |
276 | * Return interface configuration | |
277 | * of system. List may be used | |
278 | * in later ioctl's (above) to get | |
279 | * other information. | |
280 | */ | |
9e9695c7 | 281 | /*ARGSUSED*/ |
0c33c832 SL |
282 | ifconf(cmd, data) |
283 | int cmd; | |
284 | caddr_t data; | |
285 | { | |
286 | register struct ifconf *ifc = (struct ifconf *)data; | |
287 | register struct ifnet *ifp = ifnet; | |
9b956fa5 SL |
288 | register char *cp, *ep; |
289 | struct ifreq ifr, *ifrp; | |
0c33c832 SL |
290 | int space = ifc->ifc_len, error = 0; |
291 | ||
9b956fa5 SL |
292 | ifrp = ifc->ifc_req; |
293 | ep = ifr.ifr_name + sizeof (ifr.ifr_name) - 2; | |
0c33c832 | 294 | for (; space > sizeof (ifr) && ifp; ifp = ifp->if_next) { |
9b956fa5 SL |
295 | bcopy(ifp->if_name, ifr.ifr_name, sizeof (ifr.ifr_name) - 2); |
296 | for (cp = ifr.ifr_name; cp < ep && *cp; cp++) | |
0c33c832 | 297 | ; |
9b956fa5 | 298 | *cp++ = '0' + ifp->if_unit; *cp = '\0'; |
0c33c832 | 299 | ifr.ifr_addr = ifp->if_addr; |
9b956fa5 | 300 | error = copyout((caddr_t)&ifr, (caddr_t)ifrp, sizeof (ifr)); |
0c33c832 SL |
301 | if (error) |
302 | break; | |
9b956fa5 | 303 | space -= sizeof (ifr), ifrp++; |
0c33c832 SL |
304 | } |
305 | ifc->ifc_len -= space; | |
306 | return (error); | |
307 | } |