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