in.c revision 1.43 1 1.43 christos /* $NetBSD: in.c,v 1.43 1998/09/06 17:52:28 christos Exp $ */
2 1.39 tls
3 1.39 tls /*-
4 1.39 tls * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.39 tls * All rights reserved.
6 1.39 tls *
7 1.39 tls * This code is derived from software contributed to The NetBSD Foundation
8 1.39 tls * by Public Access Networks Corporation ("Panix"). It was developed under
9 1.39 tls * contract to Panix by Eric Haszlakiewicz and Thor Lancelot Simon.
10 1.39 tls *
11 1.39 tls * Redistribution and use in source and binary forms, with or without
12 1.39 tls * modification, are permitted provided that the following conditions
13 1.39 tls * are met:
14 1.39 tls * 1. Redistributions of source code must retain the above copyright
15 1.39 tls * notice, this list of conditions and the following disclaimer.
16 1.39 tls * 2. Redistributions in binary form must reproduce the above copyright
17 1.39 tls * notice, this list of conditions and the following disclaimer in the
18 1.39 tls * documentation and/or other materials provided with the distribution.
19 1.39 tls * 3. All advertising materials mentioning features or use of this software
20 1.39 tls * must display the following acknowledgement:
21 1.39 tls * This product includes software developed by the NetBSD
22 1.39 tls * Foundation, Inc. and its contributors.
23 1.39 tls * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.39 tls * contributors may be used to endorse or promote products derived
25 1.39 tls * from this software without specific prior written permission.
26 1.39 tls *
27 1.39 tls * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.39 tls * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.39 tls * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.39 tls * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.39 tls * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.39 tls * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.39 tls * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.39 tls * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.39 tls * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.39 tls * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.39 tls * POSSIBILITY OF SUCH DAMAGE.
38 1.39 tls */
39 1.14 cgd
40 1.1 cgd /*
41 1.12 mycroft * Copyright (c) 1982, 1986, 1991, 1993
42 1.12 mycroft * The Regents of the University of California. All rights reserved.
43 1.1 cgd *
44 1.1 cgd * Redistribution and use in source and binary forms, with or without
45 1.1 cgd * modification, are permitted provided that the following conditions
46 1.1 cgd * are met:
47 1.1 cgd * 1. Redistributions of source code must retain the above copyright
48 1.1 cgd * notice, this list of conditions and the following disclaimer.
49 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 cgd * notice, this list of conditions and the following disclaimer in the
51 1.1 cgd * documentation and/or other materials provided with the distribution.
52 1.1 cgd * 3. All advertising materials mentioning features or use of this software
53 1.1 cgd * must display the following acknowledgement:
54 1.1 cgd * This product includes software developed by the University of
55 1.1 cgd * California, Berkeley and its contributors.
56 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
57 1.1 cgd * may be used to endorse or promote products derived from this software
58 1.1 cgd * without specific prior written permission.
59 1.1 cgd *
60 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.1 cgd * SUCH DAMAGE.
71 1.1 cgd *
72 1.36 thorpej * @(#)in.c 8.4 (Berkeley) 1/9/95
73 1.1 cgd */
74 1.37 scottr
75 1.41 jonathan #include "opt_inet.h"
76 1.37 scottr #include "opt_mrouting.h"
77 1.1 cgd
78 1.6 mycroft #include <sys/param.h>
79 1.6 mycroft #include <sys/ioctl.h>
80 1.12 mycroft #include <sys/errno.h>
81 1.12 mycroft #include <sys/malloc.h>
82 1.6 mycroft #include <sys/socket.h>
83 1.6 mycroft #include <sys/socketvar.h>
84 1.26 christos #include <sys/systm.h>
85 1.27 mycroft #include <sys/proc.h>
86 1.6 mycroft
87 1.6 mycroft #include <net/if.h>
88 1.6 mycroft #include <net/route.h>
89 1.6 mycroft
90 1.34 is #include <net/if_ether.h>
91 1.34 is
92 1.12 mycroft #include <netinet/in_systm.h>
93 1.6 mycroft #include <netinet/in.h>
94 1.6 mycroft #include <netinet/in_var.h>
95 1.34 is #include <netinet/if_inarp.h>
96 1.19 mycroft #include <netinet/ip_mroute.h>
97 1.26 christos #include <netinet/igmp_var.h>
98 1.13 chopps
99 1.1 cgd #ifdef INET
100 1.1 cgd
101 1.1 cgd #ifndef SUBNETSARELOCAL
102 1.1 cgd #define SUBNETSARELOCAL 1
103 1.1 cgd #endif
104 1.1 cgd int subnetsarelocal = SUBNETSARELOCAL;
105 1.30 mrg
106 1.1 cgd /*
107 1.1 cgd * Return 1 if an internet address is for a ``local'' host
108 1.1 cgd * (one to which we have a connection). If subnetsarelocal
109 1.1 cgd * is true, this includes other subnets of the local net.
110 1.1 cgd * Otherwise, it includes only the directly-connected (sub)nets.
111 1.1 cgd */
112 1.8 mycroft int
113 1.1 cgd in_localaddr(in)
114 1.1 cgd struct in_addr in;
115 1.1 cgd {
116 1.1 cgd register struct in_ifaddr *ia;
117 1.1 cgd
118 1.1 cgd if (subnetsarelocal) {
119 1.24 mycroft for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
120 1.20 mycroft if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
121 1.1 cgd return (1);
122 1.1 cgd } else {
123 1.24 mycroft for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
124 1.20 mycroft if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
125 1.1 cgd return (1);
126 1.1 cgd }
127 1.1 cgd return (0);
128 1.1 cgd }
129 1.1 cgd
130 1.1 cgd /*
131 1.1 cgd * Determine whether an IP address is in a reserved set of addresses
132 1.1 cgd * that may not be forwarded, or whether datagrams to that destination
133 1.1 cgd * may be forwarded.
134 1.1 cgd */
135 1.8 mycroft int
136 1.1 cgd in_canforward(in)
137 1.1 cgd struct in_addr in;
138 1.1 cgd {
139 1.18 cgd register u_int32_t net;
140 1.1 cgd
141 1.20 mycroft if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
142 1.1 cgd return (0);
143 1.20 mycroft if (IN_CLASSA(in.s_addr)) {
144 1.20 mycroft net = in.s_addr & IN_CLASSA_NET;
145 1.20 mycroft if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
146 1.1 cgd return (0);
147 1.1 cgd }
148 1.1 cgd return (1);
149 1.1 cgd }
150 1.1 cgd
151 1.12 mycroft /*
152 1.12 mycroft * Trim a mask in a sockaddr
153 1.12 mycroft */
154 1.12 mycroft void
155 1.12 mycroft in_socktrim(ap)
156 1.12 mycroft struct sockaddr_in *ap;
157 1.12 mycroft {
158 1.12 mycroft register char *cplim = (char *) &ap->sin_addr;
159 1.12 mycroft register char *cp = (char *) (&ap->sin_addr + 1);
160 1.12 mycroft
161 1.12 mycroft ap->sin_len = 0;
162 1.15 mycroft while (--cp >= cplim)
163 1.12 mycroft if (*cp) {
164 1.12 mycroft (ap)->sin_len = cp - (char *) (ap) + 1;
165 1.12 mycroft break;
166 1.12 mycroft }
167 1.40 matt }
168 1.40 matt
169 1.40 matt /*
170 1.40 matt * Routine to take an Internet address and convert into a
171 1.40 matt * "dotted quad" representation for printing.
172 1.40 matt */
173 1.40 matt const char *
174 1.40 matt in_fmtaddr(addr)
175 1.40 matt struct in_addr addr;
176 1.40 matt {
177 1.40 matt static char buf[sizeof("123.456.789.123")];
178 1.40 matt
179 1.40 matt addr.s_addr = ntohl(addr.s_addr);
180 1.40 matt
181 1.40 matt sprintf(buf, "%d.%d.%d.%d",
182 1.40 matt (addr.s_addr >> 24) & 0xFF,
183 1.40 matt (addr.s_addr >> 16) & 0xFF,
184 1.40 matt (addr.s_addr >> 8) & 0xFF,
185 1.40 matt (addr.s_addr >> 0) & 0xFF);
186 1.40 matt return buf;
187 1.12 mycroft }
188 1.12 mycroft
189 1.35 thorpej /*
190 1.35 thorpej * Maintain the "in_maxmtu" variable, which is the largest
191 1.35 thorpej * mtu for non-local interfaces with AF_INET addresses assigned
192 1.35 thorpej * to them that are up.
193 1.35 thorpej */
194 1.35 thorpej unsigned long in_maxmtu;
195 1.35 thorpej
196 1.35 thorpej void
197 1.35 thorpej in_setmaxmtu()
198 1.35 thorpej {
199 1.35 thorpej register struct in_ifaddr *ia;
200 1.35 thorpej register struct ifnet *ifp;
201 1.35 thorpej unsigned long maxmtu = 0;
202 1.35 thorpej
203 1.35 thorpej for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
204 1.35 thorpej if ((ifp = ia->ia_ifp) == 0)
205 1.35 thorpej continue;
206 1.35 thorpej if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
207 1.35 thorpej continue;
208 1.35 thorpej if (ifp->if_mtu > maxmtu)
209 1.38 tls maxmtu = ifp->if_mtu;
210 1.35 thorpej }
211 1.35 thorpej if (maxmtu)
212 1.35 thorpej in_maxmtu = maxmtu;
213 1.35 thorpej }
214 1.35 thorpej
215 1.1 cgd int in_interfaces; /* number of external internet interfaces */
216 1.1 cgd
217 1.1 cgd /*
218 1.1 cgd * Generic internet control operations (ioctl's).
219 1.1 cgd * Ifp is 0 if not an interface-specific ioctl.
220 1.1 cgd */
221 1.1 cgd /* ARGSUSED */
222 1.8 mycroft int
223 1.27 mycroft in_control(so, cmd, data, ifp, p)
224 1.1 cgd struct socket *so;
225 1.18 cgd u_long cmd;
226 1.1 cgd caddr_t data;
227 1.1 cgd register struct ifnet *ifp;
228 1.27 mycroft struct proc *p;
229 1.1 cgd {
230 1.1 cgd register struct ifreq *ifr = (struct ifreq *)data;
231 1.1 cgd register struct in_ifaddr *ia = 0;
232 1.1 cgd struct in_aliasreq *ifra = (struct in_aliasreq *)data;
233 1.1 cgd struct sockaddr_in oldaddr;
234 1.1 cgd int error, hostIsNew, maskIsNew;
235 1.1 cgd
236 1.1 cgd /*
237 1.1 cgd * Find address for this interface, if it exists.
238 1.1 cgd */
239 1.1 cgd if (ifp)
240 1.38 tls IFP_TO_IA(ifp, ia);
241 1.1 cgd
242 1.1 cgd switch (cmd) {
243 1.1 cgd
244 1.1 cgd case SIOCAIFADDR:
245 1.1 cgd case SIOCDIFADDR:
246 1.43 christos case SIOCGIFALIAS:
247 1.1 cgd if (ifra->ifra_addr.sin_family == AF_INET)
248 1.38 tls for (ia = IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr).lh_first;
249 1.38 tls ia != 0; ia = ia->ia_hash.le_next) {
250 1.28 mycroft if (ia->ia_ifp == ifp &&
251 1.38 tls in_hosteq(ia->ia_addr.sin_addr,
252 1.38 tls ifra->ifra_addr.sin_addr))
253 1.28 mycroft break;
254 1.28 mycroft }
255 1.1 cgd if (cmd == SIOCDIFADDR && ia == 0)
256 1.1 cgd return (EADDRNOTAVAIL);
257 1.1 cgd /* FALLTHROUGH */
258 1.1 cgd case SIOCSIFADDR:
259 1.1 cgd case SIOCSIFNETMASK:
260 1.1 cgd case SIOCSIFDSTADDR:
261 1.27 mycroft if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
262 1.1 cgd return (EPERM);
263 1.1 cgd
264 1.1 cgd if (ifp == 0)
265 1.1 cgd panic("in_control");
266 1.28 mycroft if (ia == 0) {
267 1.28 mycroft MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
268 1.28 mycroft M_IFADDR, M_WAITOK);
269 1.28 mycroft if (ia == 0)
270 1.1 cgd return (ENOBUFS);
271 1.24 mycroft bzero((caddr_t)ia, sizeof *ia);
272 1.24 mycroft TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
273 1.24 mycroft TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
274 1.24 mycroft ifa_list);
275 1.21 mycroft ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
276 1.21 mycroft ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
277 1.21 mycroft ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
278 1.1 cgd ia->ia_sockmask.sin_len = 8;
279 1.1 cgd if (ifp->if_flags & IFF_BROADCAST) {
280 1.1 cgd ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
281 1.1 cgd ia->ia_broadaddr.sin_family = AF_INET;
282 1.1 cgd }
283 1.1 cgd ia->ia_ifp = ifp;
284 1.24 mycroft LIST_INIT(&ia->ia_multiaddrs);
285 1.17 mycroft if ((ifp->if_flags & IFF_LOOPBACK) == 0)
286 1.1 cgd in_interfaces++;
287 1.1 cgd }
288 1.1 cgd break;
289 1.1 cgd
290 1.1 cgd case SIOCSIFBRDADDR:
291 1.27 mycroft if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
292 1.1 cgd return (EPERM);
293 1.1 cgd /* FALLTHROUGH */
294 1.1 cgd
295 1.1 cgd case SIOCGIFADDR:
296 1.1 cgd case SIOCGIFNETMASK:
297 1.1 cgd case SIOCGIFDSTADDR:
298 1.1 cgd case SIOCGIFBRDADDR:
299 1.28 mycroft if (ia == 0)
300 1.1 cgd return (EADDRNOTAVAIL);
301 1.1 cgd break;
302 1.1 cgd }
303 1.1 cgd switch (cmd) {
304 1.1 cgd
305 1.1 cgd case SIOCGIFADDR:
306 1.21 mycroft *satosin(&ifr->ifr_addr) = ia->ia_addr;
307 1.1 cgd break;
308 1.1 cgd
309 1.1 cgd case SIOCGIFBRDADDR:
310 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) == 0)
311 1.1 cgd return (EINVAL);
312 1.21 mycroft *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
313 1.1 cgd break;
314 1.1 cgd
315 1.1 cgd case SIOCGIFDSTADDR:
316 1.1 cgd if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
317 1.1 cgd return (EINVAL);
318 1.21 mycroft *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
319 1.1 cgd break;
320 1.1 cgd
321 1.1 cgd case SIOCGIFNETMASK:
322 1.21 mycroft *satosin(&ifr->ifr_addr) = ia->ia_sockmask;
323 1.1 cgd break;
324 1.1 cgd
325 1.1 cgd case SIOCSIFDSTADDR:
326 1.1 cgd if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
327 1.1 cgd return (EINVAL);
328 1.1 cgd oldaddr = ia->ia_dstaddr;
329 1.21 mycroft ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
330 1.12 mycroft if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
331 1.12 mycroft (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
332 1.1 cgd ia->ia_dstaddr = oldaddr;
333 1.1 cgd return (error);
334 1.1 cgd }
335 1.1 cgd if (ia->ia_flags & IFA_ROUTE) {
336 1.21 mycroft ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
337 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
338 1.21 mycroft ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
339 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
340 1.1 cgd }
341 1.1 cgd break;
342 1.1 cgd
343 1.1 cgd case SIOCSIFBRDADDR:
344 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) == 0)
345 1.1 cgd return (EINVAL);
346 1.21 mycroft ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
347 1.1 cgd break;
348 1.1 cgd
349 1.1 cgd case SIOCSIFADDR:
350 1.21 mycroft return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
351 1.1 cgd
352 1.1 cgd case SIOCSIFNETMASK:
353 1.20 mycroft ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
354 1.20 mycroft ifra->ifra_addr.sin_addr.s_addr;
355 1.1 cgd break;
356 1.1 cgd
357 1.1 cgd case SIOCAIFADDR:
358 1.1 cgd maskIsNew = 0;
359 1.1 cgd hostIsNew = 1;
360 1.1 cgd error = 0;
361 1.1 cgd if (ia->ia_addr.sin_family == AF_INET) {
362 1.1 cgd if (ifra->ifra_addr.sin_len == 0) {
363 1.1 cgd ifra->ifra_addr = ia->ia_addr;
364 1.1 cgd hostIsNew = 0;
365 1.32 mycroft } else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
366 1.1 cgd hostIsNew = 0;
367 1.1 cgd }
368 1.1 cgd if (ifra->ifra_mask.sin_len) {
369 1.1 cgd in_ifscrub(ifp, ia);
370 1.1 cgd ia->ia_sockmask = ifra->ifra_mask;
371 1.20 mycroft ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
372 1.1 cgd maskIsNew = 1;
373 1.1 cgd }
374 1.1 cgd if ((ifp->if_flags & IFF_POINTOPOINT) &&
375 1.1 cgd (ifra->ifra_dstaddr.sin_family == AF_INET)) {
376 1.1 cgd in_ifscrub(ifp, ia);
377 1.1 cgd ia->ia_dstaddr = ifra->ifra_dstaddr;
378 1.1 cgd maskIsNew = 1; /* We lie; but the effect's the same */
379 1.1 cgd }
380 1.1 cgd if (ifra->ifra_addr.sin_family == AF_INET &&
381 1.1 cgd (hostIsNew || maskIsNew))
382 1.1 cgd error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
383 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) &&
384 1.1 cgd (ifra->ifra_broadaddr.sin_family == AF_INET))
385 1.1 cgd ia->ia_broadaddr = ifra->ifra_broadaddr;
386 1.1 cgd return (error);
387 1.43 christos
388 1.43 christos case SIOCGIFALIAS:
389 1.43 christos ifra->ifra_mask = ia->ia_sockmask;
390 1.43 christos if ((ifp->if_flags & IFF_POINTOPOINT) &&
391 1.43 christos (ia->ia_dstaddr.sin_family == AF_INET))
392 1.43 christos ifra->ifra_dstaddr = ia->ia_dstaddr;
393 1.43 christos else if ((ifp->if_flags & IFF_BROADCAST) &&
394 1.43 christos (ia->ia_broadaddr.sin_family == AF_INET))
395 1.43 christos ifra->ifra_broadaddr = ia->ia_broadaddr;
396 1.43 christos else
397 1.43 christos memset(&ifra->ifra_broadaddr, 0,
398 1.43 christos sizeof(ifra->ifra_broadaddr));
399 1.43 christos return 0;
400 1.1 cgd
401 1.1 cgd case SIOCDIFADDR:
402 1.1 cgd in_ifscrub(ifp, ia);
403 1.38 tls LIST_REMOVE(ia, ia_hash);
404 1.24 mycroft TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
405 1.24 mycroft TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
406 1.24 mycroft IFAFREE((&ia->ia_ifa));
407 1.35 thorpej in_setmaxmtu();
408 1.1 cgd break;
409 1.19 mycroft
410 1.19 mycroft #ifdef MROUTING
411 1.19 mycroft case SIOCGETVIFCNT:
412 1.19 mycroft case SIOCGETSGCNT:
413 1.29 mycroft return (mrt_ioctl(so, cmd, data));
414 1.19 mycroft #endif /* MROUTING */
415 1.1 cgd
416 1.1 cgd default:
417 1.1 cgd if (ifp == 0 || ifp->if_ioctl == 0)
418 1.1 cgd return (EOPNOTSUPP);
419 1.35 thorpej error = (*ifp->if_ioctl)(ifp, cmd, data);
420 1.35 thorpej in_setmaxmtu();
421 1.35 thorpej return(error);
422 1.1 cgd }
423 1.1 cgd return (0);
424 1.1 cgd }
425 1.1 cgd
426 1.1 cgd /*
427 1.1 cgd * Delete any existing route for an interface.
428 1.1 cgd */
429 1.12 mycroft void
430 1.1 cgd in_ifscrub(ifp, ia)
431 1.1 cgd register struct ifnet *ifp;
432 1.1 cgd register struct in_ifaddr *ia;
433 1.1 cgd {
434 1.1 cgd
435 1.1 cgd if ((ia->ia_flags & IFA_ROUTE) == 0)
436 1.1 cgd return;
437 1.1 cgd if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
438 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
439 1.1 cgd else
440 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
441 1.1 cgd ia->ia_flags &= ~IFA_ROUTE;
442 1.1 cgd }
443 1.1 cgd
444 1.1 cgd /*
445 1.1 cgd * Initialize an interface's internet address
446 1.1 cgd * and routing table entry.
447 1.1 cgd */
448 1.12 mycroft int
449 1.1 cgd in_ifinit(ifp, ia, sin, scrub)
450 1.1 cgd register struct ifnet *ifp;
451 1.1 cgd register struct in_ifaddr *ia;
452 1.1 cgd struct sockaddr_in *sin;
453 1.12 mycroft int scrub;
454 1.1 cgd {
455 1.20 mycroft register u_int32_t i = sin->sin_addr.s_addr;
456 1.1 cgd struct sockaddr_in oldaddr;
457 1.26 christos int s = splimp(), flags = RTF_UP, error;
458 1.1 cgd
459 1.32 mycroft /*
460 1.32 mycroft * Set up new addresses.
461 1.32 mycroft */
462 1.1 cgd oldaddr = ia->ia_addr;
463 1.38 tls if (ia->ia_addr.sin_family == AF_INET)
464 1.38 tls LIST_REMOVE(ia, ia_hash);
465 1.1 cgd ia->ia_addr = *sin;
466 1.38 tls LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
467 1.38 tls
468 1.1 cgd /*
469 1.1 cgd * Give the interface a chance to initialize
470 1.1 cgd * if this is its first address,
471 1.1 cgd * and to validate the address if necessary.
472 1.1 cgd */
473 1.12 mycroft if (ifp->if_ioctl &&
474 1.32 mycroft (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
475 1.32 mycroft goto bad;
476 1.1 cgd splx(s);
477 1.1 cgd if (scrub) {
478 1.21 mycroft ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
479 1.1 cgd in_ifscrub(ifp, ia);
480 1.21 mycroft ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
481 1.1 cgd }
482 1.35 thorpej
483 1.1 cgd if (IN_CLASSA(i))
484 1.1 cgd ia->ia_netmask = IN_CLASSA_NET;
485 1.1 cgd else if (IN_CLASSB(i))
486 1.1 cgd ia->ia_netmask = IN_CLASSB_NET;
487 1.1 cgd else
488 1.1 cgd ia->ia_netmask = IN_CLASSC_NET;
489 1.1 cgd /*
490 1.12 mycroft * The subnet mask usually includes at least the standard network part,
491 1.12 mycroft * but may may be smaller in the case of supernetting.
492 1.12 mycroft * If it is set, we believe it.
493 1.1 cgd */
494 1.12 mycroft if (ia->ia_subnetmask == 0) {
495 1.12 mycroft ia->ia_subnetmask = ia->ia_netmask;
496 1.20 mycroft ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
497 1.12 mycroft } else
498 1.12 mycroft ia->ia_netmask &= ia->ia_subnetmask;
499 1.35 thorpej
500 1.12 mycroft ia->ia_net = i & ia->ia_netmask;
501 1.1 cgd ia->ia_subnet = i & ia->ia_subnetmask;
502 1.12 mycroft in_socktrim(&ia->ia_sockmask);
503 1.35 thorpej /* re-calculate the "in_maxmtu" value */
504 1.35 thorpej in_setmaxmtu();
505 1.1 cgd /*
506 1.1 cgd * Add route for the network.
507 1.1 cgd */
508 1.12 mycroft ia->ia_ifa.ifa_metric = ifp->if_metric;
509 1.1 cgd if (ifp->if_flags & IFF_BROADCAST) {
510 1.12 mycroft ia->ia_broadaddr.sin_addr.s_addr =
511 1.20 mycroft ia->ia_subnet | ~ia->ia_subnetmask;
512 1.1 cgd ia->ia_netbroadcast.s_addr =
513 1.20 mycroft ia->ia_net | ~ia->ia_netmask;
514 1.1 cgd } else if (ifp->if_flags & IFF_LOOPBACK) {
515 1.1 cgd ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
516 1.1 cgd flags |= RTF_HOST;
517 1.1 cgd } else if (ifp->if_flags & IFF_POINTOPOINT) {
518 1.1 cgd if (ia->ia_dstaddr.sin_family != AF_INET)
519 1.1 cgd return (0);
520 1.1 cgd flags |= RTF_HOST;
521 1.1 cgd }
522 1.32 mycroft error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
523 1.32 mycroft if (!error)
524 1.1 cgd ia->ia_flags |= IFA_ROUTE;
525 1.5 hpeyerl /*
526 1.5 hpeyerl * If the interface supports multicast, join the "all hosts"
527 1.5 hpeyerl * multicast group on that interface.
528 1.5 hpeyerl */
529 1.5 hpeyerl if (ifp->if_flags & IFF_MULTICAST) {
530 1.5 hpeyerl struct in_addr addr;
531 1.5 hpeyerl
532 1.20 mycroft addr.s_addr = INADDR_ALLHOSTS_GROUP;
533 1.5 hpeyerl in_addmulti(&addr, ifp);
534 1.5 hpeyerl }
535 1.1 cgd return (error);
536 1.32 mycroft bad:
537 1.32 mycroft splx(s);
538 1.38 tls LIST_REMOVE(ia, ia_hash);
539 1.32 mycroft ia->ia_addr = oldaddr;
540 1.38 tls if (ia->ia_addr.sin_family == AF_INET)
541 1.38 tls LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
542 1.38 tls ia, ia_hash);
543 1.32 mycroft return (error);
544 1.1 cgd }
545 1.1 cgd
546 1.1 cgd /*
547 1.1 cgd * Return 1 if the address might be a local broadcast address.
548 1.1 cgd */
549 1.8 mycroft int
550 1.12 mycroft in_broadcast(in, ifp)
551 1.1 cgd struct in_addr in;
552 1.12 mycroft struct ifnet *ifp;
553 1.1 cgd {
554 1.12 mycroft register struct ifaddr *ifa;
555 1.1 cgd
556 1.12 mycroft if (in.s_addr == INADDR_BROADCAST ||
557 1.32 mycroft in_nullhost(in))
558 1.12 mycroft return 1;
559 1.12 mycroft if ((ifp->if_flags & IFF_BROADCAST) == 0)
560 1.12 mycroft return 0;
561 1.1 cgd /*
562 1.1 cgd * Look through the list of addresses for a match
563 1.1 cgd * with a broadcast address.
564 1.1 cgd */
565 1.22 mycroft #define ia (ifatoia(ifa))
566 1.24 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
567 1.12 mycroft if (ifa->ifa_addr->sa_family == AF_INET &&
568 1.32 mycroft (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
569 1.32 mycroft in_hosteq(in, ia->ia_netbroadcast) ||
570 1.12 mycroft /*
571 1.12 mycroft * Check for old-style (host 0) broadcast.
572 1.12 mycroft */
573 1.20 mycroft in.s_addr == ia->ia_subnet ||
574 1.20 mycroft in.s_addr == ia->ia_net))
575 1.12 mycroft return 1;
576 1.1 cgd return (0);
577 1.12 mycroft #undef ia
578 1.1 cgd }
579 1.5 hpeyerl
580 1.5 hpeyerl /*
581 1.5 hpeyerl * Add an address to the list of IP multicast addresses for a given interface.
582 1.5 hpeyerl */
583 1.5 hpeyerl struct in_multi *
584 1.5 hpeyerl in_addmulti(ap, ifp)
585 1.5 hpeyerl register struct in_addr *ap;
586 1.5 hpeyerl register struct ifnet *ifp;
587 1.5 hpeyerl {
588 1.5 hpeyerl register struct in_multi *inm;
589 1.5 hpeyerl struct ifreq ifr;
590 1.5 hpeyerl struct in_ifaddr *ia;
591 1.25 mycroft int s = splsoftnet();
592 1.5 hpeyerl
593 1.5 hpeyerl /*
594 1.5 hpeyerl * See if address already in list.
595 1.5 hpeyerl */
596 1.5 hpeyerl IN_LOOKUP_MULTI(*ap, ifp, inm);
597 1.5 hpeyerl if (inm != NULL) {
598 1.5 hpeyerl /*
599 1.5 hpeyerl * Found it; just increment the reference count.
600 1.5 hpeyerl */
601 1.5 hpeyerl ++inm->inm_refcount;
602 1.24 mycroft } else {
603 1.5 hpeyerl /*
604 1.5 hpeyerl * New address; allocate a new multicast record
605 1.5 hpeyerl * and link it into the interface's multicast list.
606 1.5 hpeyerl */
607 1.5 hpeyerl inm = (struct in_multi *)malloc(sizeof(*inm),
608 1.5 hpeyerl M_IPMADDR, M_NOWAIT);
609 1.5 hpeyerl if (inm == NULL) {
610 1.5 hpeyerl splx(s);
611 1.5 hpeyerl return (NULL);
612 1.5 hpeyerl }
613 1.5 hpeyerl inm->inm_addr = *ap;
614 1.5 hpeyerl inm->inm_ifp = ifp;
615 1.5 hpeyerl inm->inm_refcount = 1;
616 1.5 hpeyerl IFP_TO_IA(ifp, ia);
617 1.5 hpeyerl if (ia == NULL) {
618 1.5 hpeyerl free(inm, M_IPMADDR);
619 1.5 hpeyerl splx(s);
620 1.5 hpeyerl return (NULL);
621 1.5 hpeyerl }
622 1.5 hpeyerl inm->inm_ia = ia;
623 1.24 mycroft LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
624 1.5 hpeyerl /*
625 1.5 hpeyerl * Ask the network driver to update its multicast reception
626 1.5 hpeyerl * filter appropriately for the new address.
627 1.5 hpeyerl */
628 1.23 mycroft satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
629 1.21 mycroft satosin(&ifr.ifr_addr)->sin_family = AF_INET;
630 1.21 mycroft satosin(&ifr.ifr_addr)->sin_addr = *ap;
631 1.12 mycroft if ((ifp->if_ioctl == NULL) ||
632 1.5 hpeyerl (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
633 1.24 mycroft LIST_REMOVE(inm, inm_list);
634 1.5 hpeyerl free(inm, M_IPMADDR);
635 1.5 hpeyerl splx(s);
636 1.5 hpeyerl return (NULL);
637 1.5 hpeyerl }
638 1.5 hpeyerl /*
639 1.5 hpeyerl * Let IGMP know that we have joined a new IP multicast group.
640 1.5 hpeyerl */
641 1.5 hpeyerl igmp_joingroup(inm);
642 1.5 hpeyerl }
643 1.5 hpeyerl splx(s);
644 1.5 hpeyerl return (inm);
645 1.5 hpeyerl }
646 1.5 hpeyerl
647 1.5 hpeyerl /*
648 1.5 hpeyerl * Delete a multicast address record.
649 1.5 hpeyerl */
650 1.26 christos void
651 1.5 hpeyerl in_delmulti(inm)
652 1.5 hpeyerl register struct in_multi *inm;
653 1.5 hpeyerl {
654 1.5 hpeyerl struct ifreq ifr;
655 1.25 mycroft int s = splsoftnet();
656 1.5 hpeyerl
657 1.5 hpeyerl if (--inm->inm_refcount == 0) {
658 1.5 hpeyerl /*
659 1.5 hpeyerl * No remaining claims to this record; let IGMP know that
660 1.5 hpeyerl * we are leaving the multicast group.
661 1.5 hpeyerl */
662 1.5 hpeyerl igmp_leavegroup(inm);
663 1.5 hpeyerl /*
664 1.5 hpeyerl * Unlink from list.
665 1.5 hpeyerl */
666 1.24 mycroft LIST_REMOVE(inm, inm_list);
667 1.5 hpeyerl /*
668 1.5 hpeyerl * Notify the network driver to update its multicast reception
669 1.5 hpeyerl * filter.
670 1.5 hpeyerl */
671 1.21 mycroft satosin(&ifr.ifr_addr)->sin_family = AF_INET;
672 1.21 mycroft satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
673 1.5 hpeyerl (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
674 1.5 hpeyerl (caddr_t)&ifr);
675 1.5 hpeyerl free(inm, M_IPMADDR);
676 1.5 hpeyerl }
677 1.5 hpeyerl splx(s);
678 1.5 hpeyerl }
679 1.30 mrg #endif
680