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