in.c revision 1.39 1 1.39 tls /* $NetBSD: in.c,v 1.39 1998/02/15 18:24:24 tls 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.37 scottr #include "opt_mrouting.h"
76 1.1 cgd
77 1.6 mycroft #include <sys/param.h>
78 1.6 mycroft #include <sys/ioctl.h>
79 1.12 mycroft #include <sys/errno.h>
80 1.12 mycroft #include <sys/malloc.h>
81 1.6 mycroft #include <sys/socket.h>
82 1.6 mycroft #include <sys/socketvar.h>
83 1.26 christos #include <sys/systm.h>
84 1.27 mycroft #include <sys/proc.h>
85 1.6 mycroft
86 1.6 mycroft #include <net/if.h>
87 1.6 mycroft #include <net/route.h>
88 1.6 mycroft
89 1.34 is #include <net/if_ether.h>
90 1.34 is
91 1.12 mycroft #include <netinet/in_systm.h>
92 1.6 mycroft #include <netinet/in.h>
93 1.6 mycroft #include <netinet/in_var.h>
94 1.34 is #include <netinet/if_inarp.h>
95 1.19 mycroft #include <netinet/ip_mroute.h>
96 1.26 christos #include <netinet/igmp_var.h>
97 1.8 mycroft
98 1.13 chopps #include "ether.h"
99 1.13 chopps
100 1.1 cgd #ifdef INET
101 1.1 cgd
102 1.1 cgd #ifndef SUBNETSARELOCAL
103 1.1 cgd #define SUBNETSARELOCAL 1
104 1.1 cgd #endif
105 1.1 cgd int subnetsarelocal = SUBNETSARELOCAL;
106 1.30 mrg
107 1.1 cgd /*
108 1.1 cgd * Return 1 if an internet address is for a ``local'' host
109 1.1 cgd * (one to which we have a connection). If subnetsarelocal
110 1.1 cgd * is true, this includes other subnets of the local net.
111 1.1 cgd * Otherwise, it includes only the directly-connected (sub)nets.
112 1.1 cgd */
113 1.8 mycroft int
114 1.1 cgd in_localaddr(in)
115 1.1 cgd struct in_addr in;
116 1.1 cgd {
117 1.1 cgd register struct in_ifaddr *ia;
118 1.1 cgd
119 1.1 cgd if (subnetsarelocal) {
120 1.24 mycroft for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
121 1.20 mycroft if ((in.s_addr & ia->ia_netmask) == ia->ia_net)
122 1.1 cgd return (1);
123 1.1 cgd } else {
124 1.24 mycroft for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next)
125 1.20 mycroft if ((in.s_addr & ia->ia_subnetmask) == ia->ia_subnet)
126 1.1 cgd return (1);
127 1.1 cgd }
128 1.1 cgd return (0);
129 1.1 cgd }
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * Determine whether an IP address is in a reserved set of addresses
133 1.1 cgd * that may not be forwarded, or whether datagrams to that destination
134 1.1 cgd * may be forwarded.
135 1.1 cgd */
136 1.8 mycroft int
137 1.1 cgd in_canforward(in)
138 1.1 cgd struct in_addr in;
139 1.1 cgd {
140 1.18 cgd register u_int32_t net;
141 1.1 cgd
142 1.20 mycroft if (IN_EXPERIMENTAL(in.s_addr) || IN_MULTICAST(in.s_addr))
143 1.1 cgd return (0);
144 1.20 mycroft if (IN_CLASSA(in.s_addr)) {
145 1.20 mycroft net = in.s_addr & IN_CLASSA_NET;
146 1.20 mycroft if (net == 0 || net == htonl(IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
147 1.1 cgd return (0);
148 1.1 cgd }
149 1.1 cgd return (1);
150 1.1 cgd }
151 1.1 cgd
152 1.12 mycroft /*
153 1.12 mycroft * Trim a mask in a sockaddr
154 1.12 mycroft */
155 1.12 mycroft void
156 1.12 mycroft in_socktrim(ap)
157 1.12 mycroft struct sockaddr_in *ap;
158 1.12 mycroft {
159 1.12 mycroft register char *cplim = (char *) &ap->sin_addr;
160 1.12 mycroft register char *cp = (char *) (&ap->sin_addr + 1);
161 1.12 mycroft
162 1.12 mycroft ap->sin_len = 0;
163 1.15 mycroft while (--cp >= cplim)
164 1.12 mycroft if (*cp) {
165 1.12 mycroft (ap)->sin_len = cp - (char *) (ap) + 1;
166 1.12 mycroft break;
167 1.12 mycroft }
168 1.12 mycroft }
169 1.12 mycroft
170 1.35 thorpej /*
171 1.35 thorpej * Maintain the "in_maxmtu" variable, which is the largest
172 1.35 thorpej * mtu for non-local interfaces with AF_INET addresses assigned
173 1.35 thorpej * to them that are up.
174 1.35 thorpej */
175 1.35 thorpej unsigned long in_maxmtu;
176 1.35 thorpej
177 1.35 thorpej void
178 1.35 thorpej in_setmaxmtu()
179 1.35 thorpej {
180 1.35 thorpej register struct in_ifaddr *ia;
181 1.35 thorpej register struct ifnet *ifp;
182 1.35 thorpej unsigned long maxmtu = 0;
183 1.35 thorpej
184 1.35 thorpej for (ia = in_ifaddr.tqh_first; ia != 0; ia = ia->ia_list.tqe_next) {
185 1.35 thorpej if ((ifp = ia->ia_ifp) == 0)
186 1.35 thorpej continue;
187 1.35 thorpej if ((ifp->if_flags & (IFF_UP|IFF_LOOPBACK)) != IFF_UP)
188 1.35 thorpej continue;
189 1.35 thorpej if (ifp->if_mtu > maxmtu)
190 1.38 tls maxmtu = ifp->if_mtu;
191 1.35 thorpej }
192 1.35 thorpej if (maxmtu)
193 1.35 thorpej in_maxmtu = maxmtu;
194 1.35 thorpej }
195 1.35 thorpej
196 1.1 cgd int in_interfaces; /* number of external internet interfaces */
197 1.1 cgd
198 1.1 cgd /*
199 1.1 cgd * Generic internet control operations (ioctl's).
200 1.1 cgd * Ifp is 0 if not an interface-specific ioctl.
201 1.1 cgd */
202 1.1 cgd /* ARGSUSED */
203 1.8 mycroft int
204 1.27 mycroft in_control(so, cmd, data, ifp, p)
205 1.1 cgd struct socket *so;
206 1.18 cgd u_long cmd;
207 1.1 cgd caddr_t data;
208 1.1 cgd register struct ifnet *ifp;
209 1.27 mycroft struct proc *p;
210 1.1 cgd {
211 1.1 cgd register struct ifreq *ifr = (struct ifreq *)data;
212 1.1 cgd register struct in_ifaddr *ia = 0;
213 1.1 cgd struct in_aliasreq *ifra = (struct in_aliasreq *)data;
214 1.1 cgd struct sockaddr_in oldaddr;
215 1.1 cgd int error, hostIsNew, maskIsNew;
216 1.1 cgd
217 1.1 cgd /*
218 1.1 cgd * Find address for this interface, if it exists.
219 1.1 cgd */
220 1.1 cgd if (ifp)
221 1.38 tls IFP_TO_IA(ifp, ia);
222 1.1 cgd
223 1.1 cgd switch (cmd) {
224 1.1 cgd
225 1.1 cgd case SIOCAIFADDR:
226 1.1 cgd case SIOCDIFADDR:
227 1.1 cgd if (ifra->ifra_addr.sin_family == AF_INET)
228 1.38 tls for (ia = IN_IFADDR_HASH(ifra->ifra_addr.sin_addr.s_addr).lh_first;
229 1.38 tls ia != 0; ia = ia->ia_hash.le_next) {
230 1.28 mycroft if (ia->ia_ifp == ifp &&
231 1.38 tls in_hosteq(ia->ia_addr.sin_addr,
232 1.38 tls ifra->ifra_addr.sin_addr))
233 1.28 mycroft break;
234 1.28 mycroft }
235 1.1 cgd if (cmd == SIOCDIFADDR && ia == 0)
236 1.1 cgd return (EADDRNOTAVAIL);
237 1.1 cgd /* FALLTHROUGH */
238 1.1 cgd case SIOCSIFADDR:
239 1.1 cgd case SIOCSIFNETMASK:
240 1.1 cgd case SIOCSIFDSTADDR:
241 1.27 mycroft if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
242 1.1 cgd return (EPERM);
243 1.1 cgd
244 1.1 cgd if (ifp == 0)
245 1.1 cgd panic("in_control");
246 1.28 mycroft if (ia == 0) {
247 1.28 mycroft MALLOC(ia, struct in_ifaddr *, sizeof(*ia),
248 1.28 mycroft M_IFADDR, M_WAITOK);
249 1.28 mycroft if (ia == 0)
250 1.1 cgd return (ENOBUFS);
251 1.24 mycroft bzero((caddr_t)ia, sizeof *ia);
252 1.24 mycroft TAILQ_INSERT_TAIL(&in_ifaddr, ia, ia_list);
253 1.24 mycroft TAILQ_INSERT_TAIL(&ifp->if_addrlist, (struct ifaddr *)ia,
254 1.24 mycroft ifa_list);
255 1.21 mycroft ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
256 1.21 mycroft ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
257 1.21 mycroft ia->ia_ifa.ifa_netmask = sintosa(&ia->ia_sockmask);
258 1.1 cgd ia->ia_sockmask.sin_len = 8;
259 1.1 cgd if (ifp->if_flags & IFF_BROADCAST) {
260 1.1 cgd ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
261 1.1 cgd ia->ia_broadaddr.sin_family = AF_INET;
262 1.1 cgd }
263 1.1 cgd ia->ia_ifp = ifp;
264 1.24 mycroft LIST_INIT(&ia->ia_multiaddrs);
265 1.17 mycroft if ((ifp->if_flags & IFF_LOOPBACK) == 0)
266 1.1 cgd in_interfaces++;
267 1.1 cgd }
268 1.1 cgd break;
269 1.1 cgd
270 1.1 cgd case SIOCSIFBRDADDR:
271 1.27 mycroft if (p == 0 || (error = suser(p->p_ucred, &p->p_acflag)))
272 1.1 cgd return (EPERM);
273 1.1 cgd /* FALLTHROUGH */
274 1.1 cgd
275 1.1 cgd case SIOCGIFADDR:
276 1.1 cgd case SIOCGIFNETMASK:
277 1.1 cgd case SIOCGIFDSTADDR:
278 1.1 cgd case SIOCGIFBRDADDR:
279 1.28 mycroft if (ia == 0)
280 1.1 cgd return (EADDRNOTAVAIL);
281 1.1 cgd break;
282 1.1 cgd }
283 1.1 cgd switch (cmd) {
284 1.1 cgd
285 1.1 cgd case SIOCGIFADDR:
286 1.21 mycroft *satosin(&ifr->ifr_addr) = ia->ia_addr;
287 1.1 cgd break;
288 1.1 cgd
289 1.1 cgd case SIOCGIFBRDADDR:
290 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) == 0)
291 1.1 cgd return (EINVAL);
292 1.21 mycroft *satosin(&ifr->ifr_dstaddr) = ia->ia_broadaddr;
293 1.1 cgd break;
294 1.1 cgd
295 1.1 cgd case SIOCGIFDSTADDR:
296 1.1 cgd if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
297 1.1 cgd return (EINVAL);
298 1.21 mycroft *satosin(&ifr->ifr_dstaddr) = ia->ia_dstaddr;
299 1.1 cgd break;
300 1.1 cgd
301 1.1 cgd case SIOCGIFNETMASK:
302 1.21 mycroft *satosin(&ifr->ifr_addr) = ia->ia_sockmask;
303 1.1 cgd break;
304 1.1 cgd
305 1.1 cgd case SIOCSIFDSTADDR:
306 1.1 cgd if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
307 1.1 cgd return (EINVAL);
308 1.1 cgd oldaddr = ia->ia_dstaddr;
309 1.21 mycroft ia->ia_dstaddr = *satosin(&ifr->ifr_dstaddr);
310 1.12 mycroft if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
311 1.12 mycroft (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
312 1.1 cgd ia->ia_dstaddr = oldaddr;
313 1.1 cgd return (error);
314 1.1 cgd }
315 1.1 cgd if (ia->ia_flags & IFA_ROUTE) {
316 1.21 mycroft ia->ia_ifa.ifa_dstaddr = sintosa(&oldaddr);
317 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
318 1.21 mycroft ia->ia_ifa.ifa_dstaddr = sintosa(&ia->ia_dstaddr);
319 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
320 1.1 cgd }
321 1.1 cgd break;
322 1.1 cgd
323 1.1 cgd case SIOCSIFBRDADDR:
324 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) == 0)
325 1.1 cgd return (EINVAL);
326 1.21 mycroft ia->ia_broadaddr = *satosin(&ifr->ifr_broadaddr);
327 1.1 cgd break;
328 1.1 cgd
329 1.1 cgd case SIOCSIFADDR:
330 1.21 mycroft return (in_ifinit(ifp, ia, satosin(&ifr->ifr_addr), 1));
331 1.1 cgd
332 1.1 cgd case SIOCSIFNETMASK:
333 1.20 mycroft ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr =
334 1.20 mycroft ifra->ifra_addr.sin_addr.s_addr;
335 1.1 cgd break;
336 1.1 cgd
337 1.1 cgd case SIOCAIFADDR:
338 1.1 cgd maskIsNew = 0;
339 1.1 cgd hostIsNew = 1;
340 1.1 cgd error = 0;
341 1.1 cgd if (ia->ia_addr.sin_family == AF_INET) {
342 1.1 cgd if (ifra->ifra_addr.sin_len == 0) {
343 1.1 cgd ifra->ifra_addr = ia->ia_addr;
344 1.1 cgd hostIsNew = 0;
345 1.32 mycroft } else if (in_hosteq(ia->ia_addr.sin_addr, ifra->ifra_addr.sin_addr))
346 1.1 cgd hostIsNew = 0;
347 1.1 cgd }
348 1.1 cgd if (ifra->ifra_mask.sin_len) {
349 1.1 cgd in_ifscrub(ifp, ia);
350 1.1 cgd ia->ia_sockmask = ifra->ifra_mask;
351 1.20 mycroft ia->ia_subnetmask = ia->ia_sockmask.sin_addr.s_addr;
352 1.1 cgd maskIsNew = 1;
353 1.1 cgd }
354 1.1 cgd if ((ifp->if_flags & IFF_POINTOPOINT) &&
355 1.1 cgd (ifra->ifra_dstaddr.sin_family == AF_INET)) {
356 1.1 cgd in_ifscrub(ifp, ia);
357 1.1 cgd ia->ia_dstaddr = ifra->ifra_dstaddr;
358 1.1 cgd maskIsNew = 1; /* We lie; but the effect's the same */
359 1.1 cgd }
360 1.1 cgd if (ifra->ifra_addr.sin_family == AF_INET &&
361 1.1 cgd (hostIsNew || maskIsNew))
362 1.1 cgd error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
363 1.1 cgd if ((ifp->if_flags & IFF_BROADCAST) &&
364 1.1 cgd (ifra->ifra_broadaddr.sin_family == AF_INET))
365 1.1 cgd ia->ia_broadaddr = ifra->ifra_broadaddr;
366 1.1 cgd return (error);
367 1.1 cgd
368 1.1 cgd case SIOCDIFADDR:
369 1.1 cgd in_ifscrub(ifp, ia);
370 1.38 tls LIST_REMOVE(ia, ia_hash);
371 1.24 mycroft TAILQ_REMOVE(&ifp->if_addrlist, (struct ifaddr *)ia, ifa_list);
372 1.24 mycroft TAILQ_REMOVE(&in_ifaddr, ia, ia_list);
373 1.24 mycroft IFAFREE((&ia->ia_ifa));
374 1.35 thorpej in_setmaxmtu();
375 1.1 cgd break;
376 1.19 mycroft
377 1.19 mycroft #ifdef MROUTING
378 1.19 mycroft case SIOCGETVIFCNT:
379 1.19 mycroft case SIOCGETSGCNT:
380 1.29 mycroft return (mrt_ioctl(so, cmd, data));
381 1.19 mycroft #endif /* MROUTING */
382 1.1 cgd
383 1.1 cgd default:
384 1.1 cgd if (ifp == 0 || ifp->if_ioctl == 0)
385 1.1 cgd return (EOPNOTSUPP);
386 1.35 thorpej error = (*ifp->if_ioctl)(ifp, cmd, data);
387 1.35 thorpej in_setmaxmtu();
388 1.35 thorpej return(error);
389 1.1 cgd }
390 1.1 cgd return (0);
391 1.1 cgd }
392 1.1 cgd
393 1.1 cgd /*
394 1.1 cgd * Delete any existing route for an interface.
395 1.1 cgd */
396 1.12 mycroft void
397 1.1 cgd in_ifscrub(ifp, ia)
398 1.1 cgd register struct ifnet *ifp;
399 1.1 cgd register struct in_ifaddr *ia;
400 1.1 cgd {
401 1.1 cgd
402 1.1 cgd if ((ia->ia_flags & IFA_ROUTE) == 0)
403 1.1 cgd return;
404 1.1 cgd if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
405 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
406 1.1 cgd else
407 1.1 cgd rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
408 1.1 cgd ia->ia_flags &= ~IFA_ROUTE;
409 1.1 cgd }
410 1.1 cgd
411 1.1 cgd /*
412 1.1 cgd * Initialize an interface's internet address
413 1.1 cgd * and routing table entry.
414 1.1 cgd */
415 1.12 mycroft int
416 1.1 cgd in_ifinit(ifp, ia, sin, scrub)
417 1.1 cgd register struct ifnet *ifp;
418 1.1 cgd register struct in_ifaddr *ia;
419 1.1 cgd struct sockaddr_in *sin;
420 1.12 mycroft int scrub;
421 1.1 cgd {
422 1.20 mycroft register u_int32_t i = sin->sin_addr.s_addr;
423 1.1 cgd struct sockaddr_in oldaddr;
424 1.26 christos int s = splimp(), flags = RTF_UP, error;
425 1.1 cgd
426 1.32 mycroft /*
427 1.32 mycroft * Set up new addresses.
428 1.32 mycroft */
429 1.1 cgd oldaddr = ia->ia_addr;
430 1.38 tls if (ia->ia_addr.sin_family == AF_INET)
431 1.38 tls LIST_REMOVE(ia, ia_hash);
432 1.1 cgd ia->ia_addr = *sin;
433 1.38 tls LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr), ia, ia_hash);
434 1.38 tls
435 1.1 cgd /*
436 1.1 cgd * Give the interface a chance to initialize
437 1.1 cgd * if this is its first address,
438 1.1 cgd * and to validate the address if necessary.
439 1.1 cgd */
440 1.12 mycroft if (ifp->if_ioctl &&
441 1.32 mycroft (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia)))
442 1.32 mycroft goto bad;
443 1.1 cgd splx(s);
444 1.1 cgd if (scrub) {
445 1.21 mycroft ia->ia_ifa.ifa_addr = sintosa(&oldaddr);
446 1.1 cgd in_ifscrub(ifp, ia);
447 1.21 mycroft ia->ia_ifa.ifa_addr = sintosa(&ia->ia_addr);
448 1.1 cgd }
449 1.35 thorpej
450 1.1 cgd if (IN_CLASSA(i))
451 1.1 cgd ia->ia_netmask = IN_CLASSA_NET;
452 1.1 cgd else if (IN_CLASSB(i))
453 1.1 cgd ia->ia_netmask = IN_CLASSB_NET;
454 1.1 cgd else
455 1.1 cgd ia->ia_netmask = IN_CLASSC_NET;
456 1.1 cgd /*
457 1.12 mycroft * The subnet mask usually includes at least the standard network part,
458 1.12 mycroft * but may may be smaller in the case of supernetting.
459 1.12 mycroft * If it is set, we believe it.
460 1.1 cgd */
461 1.12 mycroft if (ia->ia_subnetmask == 0) {
462 1.12 mycroft ia->ia_subnetmask = ia->ia_netmask;
463 1.20 mycroft ia->ia_sockmask.sin_addr.s_addr = ia->ia_subnetmask;
464 1.12 mycroft } else
465 1.12 mycroft ia->ia_netmask &= ia->ia_subnetmask;
466 1.35 thorpej
467 1.12 mycroft ia->ia_net = i & ia->ia_netmask;
468 1.1 cgd ia->ia_subnet = i & ia->ia_subnetmask;
469 1.12 mycroft in_socktrim(&ia->ia_sockmask);
470 1.35 thorpej /* re-calculate the "in_maxmtu" value */
471 1.35 thorpej in_setmaxmtu();
472 1.1 cgd /*
473 1.1 cgd * Add route for the network.
474 1.1 cgd */
475 1.12 mycroft ia->ia_ifa.ifa_metric = ifp->if_metric;
476 1.1 cgd if (ifp->if_flags & IFF_BROADCAST) {
477 1.12 mycroft ia->ia_broadaddr.sin_addr.s_addr =
478 1.20 mycroft ia->ia_subnet | ~ia->ia_subnetmask;
479 1.1 cgd ia->ia_netbroadcast.s_addr =
480 1.20 mycroft ia->ia_net | ~ia->ia_netmask;
481 1.1 cgd } else if (ifp->if_flags & IFF_LOOPBACK) {
482 1.1 cgd ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
483 1.1 cgd flags |= RTF_HOST;
484 1.1 cgd } else if (ifp->if_flags & IFF_POINTOPOINT) {
485 1.1 cgd if (ia->ia_dstaddr.sin_family != AF_INET)
486 1.1 cgd return (0);
487 1.1 cgd flags |= RTF_HOST;
488 1.1 cgd }
489 1.32 mycroft error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags);
490 1.32 mycroft if (!error)
491 1.1 cgd ia->ia_flags |= IFA_ROUTE;
492 1.5 hpeyerl /*
493 1.5 hpeyerl * If the interface supports multicast, join the "all hosts"
494 1.5 hpeyerl * multicast group on that interface.
495 1.5 hpeyerl */
496 1.5 hpeyerl if (ifp->if_flags & IFF_MULTICAST) {
497 1.5 hpeyerl struct in_addr addr;
498 1.5 hpeyerl
499 1.20 mycroft addr.s_addr = INADDR_ALLHOSTS_GROUP;
500 1.5 hpeyerl in_addmulti(&addr, ifp);
501 1.5 hpeyerl }
502 1.1 cgd return (error);
503 1.32 mycroft bad:
504 1.32 mycroft splx(s);
505 1.38 tls LIST_REMOVE(ia, ia_hash);
506 1.32 mycroft ia->ia_addr = oldaddr;
507 1.38 tls if (ia->ia_addr.sin_family == AF_INET)
508 1.38 tls LIST_INSERT_HEAD(&IN_IFADDR_HASH(ia->ia_addr.sin_addr.s_addr),
509 1.38 tls ia, ia_hash);
510 1.32 mycroft return (error);
511 1.1 cgd }
512 1.1 cgd
513 1.1 cgd /*
514 1.1 cgd * Return 1 if the address might be a local broadcast address.
515 1.1 cgd */
516 1.8 mycroft int
517 1.12 mycroft in_broadcast(in, ifp)
518 1.1 cgd struct in_addr in;
519 1.12 mycroft struct ifnet *ifp;
520 1.1 cgd {
521 1.12 mycroft register struct ifaddr *ifa;
522 1.1 cgd
523 1.12 mycroft if (in.s_addr == INADDR_BROADCAST ||
524 1.32 mycroft in_nullhost(in))
525 1.12 mycroft return 1;
526 1.12 mycroft if ((ifp->if_flags & IFF_BROADCAST) == 0)
527 1.12 mycroft return 0;
528 1.1 cgd /*
529 1.1 cgd * Look through the list of addresses for a match
530 1.1 cgd * with a broadcast address.
531 1.1 cgd */
532 1.22 mycroft #define ia (ifatoia(ifa))
533 1.24 mycroft for (ifa = ifp->if_addrlist.tqh_first; ifa; ifa = ifa->ifa_list.tqe_next)
534 1.12 mycroft if (ifa->ifa_addr->sa_family == AF_INET &&
535 1.32 mycroft (in_hosteq(in, ia->ia_broadaddr.sin_addr) ||
536 1.32 mycroft in_hosteq(in, ia->ia_netbroadcast) ||
537 1.12 mycroft /*
538 1.12 mycroft * Check for old-style (host 0) broadcast.
539 1.12 mycroft */
540 1.20 mycroft in.s_addr == ia->ia_subnet ||
541 1.20 mycroft in.s_addr == ia->ia_net))
542 1.12 mycroft return 1;
543 1.1 cgd return (0);
544 1.12 mycroft #undef ia
545 1.1 cgd }
546 1.5 hpeyerl
547 1.5 hpeyerl /*
548 1.5 hpeyerl * Add an address to the list of IP multicast addresses for a given interface.
549 1.5 hpeyerl */
550 1.5 hpeyerl struct in_multi *
551 1.5 hpeyerl in_addmulti(ap, ifp)
552 1.5 hpeyerl register struct in_addr *ap;
553 1.5 hpeyerl register struct ifnet *ifp;
554 1.5 hpeyerl {
555 1.5 hpeyerl register struct in_multi *inm;
556 1.5 hpeyerl struct ifreq ifr;
557 1.5 hpeyerl struct in_ifaddr *ia;
558 1.25 mycroft int s = splsoftnet();
559 1.5 hpeyerl
560 1.5 hpeyerl /*
561 1.5 hpeyerl * See if address already in list.
562 1.5 hpeyerl */
563 1.5 hpeyerl IN_LOOKUP_MULTI(*ap, ifp, inm);
564 1.5 hpeyerl if (inm != NULL) {
565 1.5 hpeyerl /*
566 1.5 hpeyerl * Found it; just increment the reference count.
567 1.5 hpeyerl */
568 1.5 hpeyerl ++inm->inm_refcount;
569 1.24 mycroft } else {
570 1.5 hpeyerl /*
571 1.5 hpeyerl * New address; allocate a new multicast record
572 1.5 hpeyerl * and link it into the interface's multicast list.
573 1.5 hpeyerl */
574 1.5 hpeyerl inm = (struct in_multi *)malloc(sizeof(*inm),
575 1.5 hpeyerl M_IPMADDR, M_NOWAIT);
576 1.5 hpeyerl if (inm == NULL) {
577 1.5 hpeyerl splx(s);
578 1.5 hpeyerl return (NULL);
579 1.5 hpeyerl }
580 1.5 hpeyerl inm->inm_addr = *ap;
581 1.5 hpeyerl inm->inm_ifp = ifp;
582 1.5 hpeyerl inm->inm_refcount = 1;
583 1.5 hpeyerl IFP_TO_IA(ifp, ia);
584 1.5 hpeyerl if (ia == NULL) {
585 1.5 hpeyerl free(inm, M_IPMADDR);
586 1.5 hpeyerl splx(s);
587 1.5 hpeyerl return (NULL);
588 1.5 hpeyerl }
589 1.5 hpeyerl inm->inm_ia = ia;
590 1.24 mycroft LIST_INSERT_HEAD(&ia->ia_multiaddrs, inm, inm_list);
591 1.5 hpeyerl /*
592 1.5 hpeyerl * Ask the network driver to update its multicast reception
593 1.5 hpeyerl * filter appropriately for the new address.
594 1.5 hpeyerl */
595 1.23 mycroft satosin(&ifr.ifr_addr)->sin_len = sizeof(struct sockaddr_in);
596 1.21 mycroft satosin(&ifr.ifr_addr)->sin_family = AF_INET;
597 1.21 mycroft satosin(&ifr.ifr_addr)->sin_addr = *ap;
598 1.12 mycroft if ((ifp->if_ioctl == NULL) ||
599 1.5 hpeyerl (*ifp->if_ioctl)(ifp, SIOCADDMULTI,(caddr_t)&ifr) != 0) {
600 1.24 mycroft LIST_REMOVE(inm, inm_list);
601 1.5 hpeyerl free(inm, M_IPMADDR);
602 1.5 hpeyerl splx(s);
603 1.5 hpeyerl return (NULL);
604 1.5 hpeyerl }
605 1.5 hpeyerl /*
606 1.5 hpeyerl * Let IGMP know that we have joined a new IP multicast group.
607 1.5 hpeyerl */
608 1.5 hpeyerl igmp_joingroup(inm);
609 1.5 hpeyerl }
610 1.5 hpeyerl splx(s);
611 1.5 hpeyerl return (inm);
612 1.5 hpeyerl }
613 1.5 hpeyerl
614 1.5 hpeyerl /*
615 1.5 hpeyerl * Delete a multicast address record.
616 1.5 hpeyerl */
617 1.26 christos void
618 1.5 hpeyerl in_delmulti(inm)
619 1.5 hpeyerl register struct in_multi *inm;
620 1.5 hpeyerl {
621 1.5 hpeyerl struct ifreq ifr;
622 1.25 mycroft int s = splsoftnet();
623 1.5 hpeyerl
624 1.5 hpeyerl if (--inm->inm_refcount == 0) {
625 1.5 hpeyerl /*
626 1.5 hpeyerl * No remaining claims to this record; let IGMP know that
627 1.5 hpeyerl * we are leaving the multicast group.
628 1.5 hpeyerl */
629 1.5 hpeyerl igmp_leavegroup(inm);
630 1.5 hpeyerl /*
631 1.5 hpeyerl * Unlink from list.
632 1.5 hpeyerl */
633 1.24 mycroft LIST_REMOVE(inm, inm_list);
634 1.5 hpeyerl /*
635 1.5 hpeyerl * Notify the network driver to update its multicast reception
636 1.5 hpeyerl * filter.
637 1.5 hpeyerl */
638 1.21 mycroft satosin(&ifr.ifr_addr)->sin_family = AF_INET;
639 1.21 mycroft satosin(&ifr.ifr_addr)->sin_addr = inm->inm_addr;
640 1.5 hpeyerl (*inm->inm_ifp->if_ioctl)(inm->inm_ifp, SIOCDELMULTI,
641 1.5 hpeyerl (caddr_t)&ifr);
642 1.5 hpeyerl free(inm, M_IPMADDR);
643 1.5 hpeyerl }
644 1.5 hpeyerl splx(s);
645 1.5 hpeyerl }
646 1.30 mrg #endif
647