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