Deleted Added
full compact
in.c (50477) in.c (55009)
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
1/*
2 * Copyright (c) 1982, 1986, 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)in.c 8.4 (Berkeley) 1/9/95
34 * $FreeBSD: head/sys/netinet/in.c 50477 1999-08-28 01:08:13Z peter $
34 * $FreeBSD: head/sys/netinet/in.c 55009 1999-12-22 19:13:38Z shin $
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/sockio.h>
40#include <sys/malloc.h>
41#include <sys/proc.h>
42#include <sys/socket.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45
46#include <net/if.h>
35 */
36
37#include <sys/param.h>
38#include <sys/systm.h>
39#include <sys/sockio.h>
40#include <sys/malloc.h>
41#include <sys/proc.h>
42#include <sys/socket.h>
43#include <sys/kernel.h>
44#include <sys/sysctl.h>
45
46#include <net/if.h>
47#include <net/if_types.h>
47#include <net/route.h>
48
49#include <netinet/in.h>
50#include <netinet/in_var.h>
51
52#include <netinet/igmp_var.h>
53
48#include <net/route.h>
49
50#include <netinet/in.h>
51#include <netinet/in_var.h>
52
53#include <netinet/igmp_var.h>
54
55#include "gif.h"
56#if NGIF > 0
57#include <net/if_gif.h>
58#endif
59
54static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
55
60static MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
61
62static int in_mask2len __P((struct in_addr *));
63static void in_len2mask __P((struct in_addr *, int));
64static int in_lifaddr_ioctl __P((struct socket *, u_long, caddr_t,
65 struct ifnet *, struct proc *));
66
56static void in_socktrim __P((struct sockaddr_in *));
57static int in_ifinit __P((struct ifnet *,
58 struct in_ifaddr *, struct sockaddr_in *, int));
59
60static int subnetsarelocal = 0;
61SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
62 &subnetsarelocal, 0, "");
63
64struct in_multihead in_multihead; /* XXX BSS initialization */
65
66/*
67 * Return 1 if an internet address is for a ``local'' host
68 * (one to which we have a connection). If subnetsarelocal
69 * is true, this includes other subnets of the local net.
70 * Otherwise, it includes only the directly-connected (sub)nets.
71 */
72int
73in_localaddr(in)
74 struct in_addr in;
75{
76 register u_long i = ntohl(in.s_addr);
77 register struct in_ifaddr *ia;
78
79 if (subnetsarelocal) {
80 for (ia = in_ifaddrhead.tqh_first; ia;
81 ia = ia->ia_link.tqe_next)
82 if ((i & ia->ia_netmask) == ia->ia_net)
83 return (1);
84 } else {
85 for (ia = in_ifaddrhead.tqh_first; ia;
86 ia = ia->ia_link.tqe_next)
87 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
88 return (1);
89 }
90 return (0);
91}
92
93/*
94 * Determine whether an IP address is in a reserved set of addresses
95 * that may not be forwarded, or whether datagrams to that destination
96 * may be forwarded.
97 */
98int
99in_canforward(in)
100 struct in_addr in;
101{
102 register u_long i = ntohl(in.s_addr);
103 register u_long net;
104
105 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
106 return (0);
107 if (IN_CLASSA(i)) {
108 net = i & IN_CLASSA_NET;
109 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
110 return (0);
111 }
112 return (1);
113}
114
115/*
116 * Trim a mask in a sockaddr
117 */
118static void
119in_socktrim(ap)
120struct sockaddr_in *ap;
121{
122 register char *cplim = (char *) &ap->sin_addr;
123 register char *cp = (char *) (&ap->sin_addr + 1);
124
125 ap->sin_len = 0;
126 while (--cp >= cplim)
127 if (*cp) {
128 (ap)->sin_len = cp - (char *) (ap) + 1;
129 break;
130 }
131}
132
67static void in_socktrim __P((struct sockaddr_in *));
68static int in_ifinit __P((struct ifnet *,
69 struct in_ifaddr *, struct sockaddr_in *, int));
70
71static int subnetsarelocal = 0;
72SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
73 &subnetsarelocal, 0, "");
74
75struct in_multihead in_multihead; /* XXX BSS initialization */
76
77/*
78 * Return 1 if an internet address is for a ``local'' host
79 * (one to which we have a connection). If subnetsarelocal
80 * is true, this includes other subnets of the local net.
81 * Otherwise, it includes only the directly-connected (sub)nets.
82 */
83int
84in_localaddr(in)
85 struct in_addr in;
86{
87 register u_long i = ntohl(in.s_addr);
88 register struct in_ifaddr *ia;
89
90 if (subnetsarelocal) {
91 for (ia = in_ifaddrhead.tqh_first; ia;
92 ia = ia->ia_link.tqe_next)
93 if ((i & ia->ia_netmask) == ia->ia_net)
94 return (1);
95 } else {
96 for (ia = in_ifaddrhead.tqh_first; ia;
97 ia = ia->ia_link.tqe_next)
98 if ((i & ia->ia_subnetmask) == ia->ia_subnet)
99 return (1);
100 }
101 return (0);
102}
103
104/*
105 * Determine whether an IP address is in a reserved set of addresses
106 * that may not be forwarded, or whether datagrams to that destination
107 * may be forwarded.
108 */
109int
110in_canforward(in)
111 struct in_addr in;
112{
113 register u_long i = ntohl(in.s_addr);
114 register u_long net;
115
116 if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
117 return (0);
118 if (IN_CLASSA(i)) {
119 net = i & IN_CLASSA_NET;
120 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
121 return (0);
122 }
123 return (1);
124}
125
126/*
127 * Trim a mask in a sockaddr
128 */
129static void
130in_socktrim(ap)
131struct sockaddr_in *ap;
132{
133 register char *cplim = (char *) &ap->sin_addr;
134 register char *cp = (char *) (&ap->sin_addr + 1);
135
136 ap->sin_len = 0;
137 while (--cp >= cplim)
138 if (*cp) {
139 (ap)->sin_len = cp - (char *) (ap) + 1;
140 break;
141 }
142}
143
144static int
145in_mask2len(mask)
146 struct in_addr *mask;
147{
148 int x, y;
149 u_char *p;
150
151 p = (u_char *)mask;
152 for (x = 0; x < sizeof(*mask); x++) {
153 if (p[x] != 0xff)
154 break;
155 }
156 y = 0;
157 if (x < sizeof(*mask)) {
158 for (y = 0; y < 8; y++) {
159 if ((p[x] & (0x80 >> y)) == 0)
160 break;
161 }
162 }
163 return x * 8 + y;
164}
165
166static void
167in_len2mask(mask, len)
168 struct in_addr *mask;
169 int len;
170{
171 int i;
172 u_char *p;
173
174 p = (u_char *)mask;
175 bzero(mask, sizeof(*mask));
176 for (i = 0; i < len / 8; i++)
177 p[i] = 0xff;
178 if (len % 8)
179 p[i] = (0xff00 >> (len % 8)) & 0xff;
180}
181
133static int in_interfaces; /* number of external internet interfaces */
134
135/*
136 * Generic internet control operations (ioctl's).
137 * Ifp is 0 if not an interface-specific ioctl.
138 */
139/* ARGSUSED */
140int
141in_control(so, cmd, data, ifp, p)
142 struct socket *so;
143 u_long cmd;
144 caddr_t data;
145 register struct ifnet *ifp;
146 struct proc *p;
147{
148 register struct ifreq *ifr = (struct ifreq *)data;
149 register struct in_ifaddr *ia = 0, *iap;
150 register struct ifaddr *ifa;
151 struct in_ifaddr *oia;
152 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
153 struct sockaddr_in oldaddr;
154 int error, hostIsNew, maskIsNew, s;
155 u_long i;
156
182static int in_interfaces; /* number of external internet interfaces */
183
184/*
185 * Generic internet control operations (ioctl's).
186 * Ifp is 0 if not an interface-specific ioctl.
187 */
188/* ARGSUSED */
189int
190in_control(so, cmd, data, ifp, p)
191 struct socket *so;
192 u_long cmd;
193 caddr_t data;
194 register struct ifnet *ifp;
195 struct proc *p;
196{
197 register struct ifreq *ifr = (struct ifreq *)data;
198 register struct in_ifaddr *ia = 0, *iap;
199 register struct ifaddr *ifa;
200 struct in_ifaddr *oia;
201 struct in_aliasreq *ifra = (struct in_aliasreq *)data;
202 struct sockaddr_in oldaddr;
203 int error, hostIsNew, maskIsNew, s;
204 u_long i;
205
206#if NGIF > 0
207 if (ifp && ifp->if_type == IFT_GIF) {
208 switch (cmd) {
209 case SIOCSIFPHYADDR:
210 if (p &&
211 (error = suser(p)) != 0)
212 return(error);
213 case SIOCGIFPSRCADDR:
214 case SIOCGIFPDSTADDR:
215 return gif_ioctl(ifp, cmd, data);
216 }
217 }
218#endif
219
220 switch (cmd) {
221 case SIOCALIFADDR:
222 case SIOCDLIFADDR:
223 if (p && (error = suser(p)) != 0)
224 return error;
225 /*fall through*/
226 case SIOCGLIFADDR:
227 if (!ifp)
228 return EINVAL;
229 return in_lifaddr_ioctl(so, cmd, data, ifp, p);
230 }
231
157 /*
158 * Find address for this interface, if it exists.
159 *
160 * If an alias address was specified, find that one instead of
161 * the first one on the interface.
162 */
163 if (ifp)
164 for (iap = in_ifaddrhead.tqh_first; iap;
165 iap = iap->ia_link.tqe_next)
166 if (iap->ia_ifp == ifp) {
167 if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr ==
168 iap->ia_addr.sin_addr.s_addr) {
169 ia = iap;
170 break;
171 } else if (ia == NULL) {
172 ia = iap;
173 if (ifr->ifr_addr.sa_family != AF_INET)
174 break;
175 }
176 }
177
178 switch (cmd) {
179
180 case SIOCAIFADDR:
181 case SIOCDIFADDR:
182 if (ifp == 0)
183 return (EADDRNOTAVAIL);
184 if (ifra->ifra_addr.sin_family == AF_INET) {
185 for (oia = ia; ia; ia = ia->ia_link.tqe_next) {
186 if (ia->ia_ifp == ifp &&
187 ia->ia_addr.sin_addr.s_addr ==
188 ifra->ifra_addr.sin_addr.s_addr)
189 break;
190 }
191 if ((ifp->if_flags & IFF_POINTOPOINT)
192 && (cmd == SIOCAIFADDR)
193 && (ifra->ifra_dstaddr.sin_addr.s_addr
194 == INADDR_ANY)) {
195 return EDESTADDRREQ;
196 }
197 }
198 if (cmd == SIOCDIFADDR && ia == 0)
199 return (EADDRNOTAVAIL);
200 /* FALLTHROUGH */
201 case SIOCSIFADDR:
202 case SIOCSIFNETMASK:
203 case SIOCSIFDSTADDR:
204 if (p && (error = suser(p)) != 0)
205 return error;
206
207 if (ifp == 0)
208 return (EADDRNOTAVAIL);
209 if (ia == (struct in_ifaddr *)0) {
210 ia = (struct in_ifaddr *)
211 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
212 if (ia == (struct in_ifaddr *)NULL)
213 return (ENOBUFS);
214 bzero((caddr_t)ia, sizeof *ia);
215 /*
216 * Protect from ipintr() traversing address list
217 * while we're modifying it.
218 */
219 s = splnet();
220
221 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
222 ifa = &ia->ia_ifa;
223 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
224
225 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
226 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
227 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
228 ia->ia_sockmask.sin_len = 8;
229 if (ifp->if_flags & IFF_BROADCAST) {
230 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
231 ia->ia_broadaddr.sin_family = AF_INET;
232 }
233 ia->ia_ifp = ifp;
234 if (!(ifp->if_flags & IFF_LOOPBACK))
235 in_interfaces++;
236 splx(s);
237 }
238 break;
239
240 case SIOCSIFBRDADDR:
241 if (p && (error = suser(p)) != 0)
242 return error;
243 /* FALLTHROUGH */
244
245 case SIOCGIFADDR:
246 case SIOCGIFNETMASK:
247 case SIOCGIFDSTADDR:
248 case SIOCGIFBRDADDR:
249 if (ia == (struct in_ifaddr *)0)
250 return (EADDRNOTAVAIL);
251 break;
252 }
253 switch (cmd) {
254
255 case SIOCGIFADDR:
256 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
257 break;
258
259 case SIOCGIFBRDADDR:
260 if ((ifp->if_flags & IFF_BROADCAST) == 0)
261 return (EINVAL);
262 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
263 break;
264
265 case SIOCGIFDSTADDR:
266 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
267 return (EINVAL);
268 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
269 break;
270
271 case SIOCGIFNETMASK:
272 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
273 break;
274
275 case SIOCSIFDSTADDR:
276 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
277 return (EINVAL);
278 oldaddr = ia->ia_dstaddr;
279 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
280 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
281 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
282 ia->ia_dstaddr = oldaddr;
283 return (error);
284 }
285 if (ia->ia_flags & IFA_ROUTE) {
286 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
287 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
288 ia->ia_ifa.ifa_dstaddr =
289 (struct sockaddr *)&ia->ia_dstaddr;
290 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
291 }
292 break;
293
294 case SIOCSIFBRDADDR:
295 if ((ifp->if_flags & IFF_BROADCAST) == 0)
296 return (EINVAL);
297 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
298 break;
299
300 case SIOCSIFADDR:
301 return (in_ifinit(ifp, ia,
302 (struct sockaddr_in *) &ifr->ifr_addr, 1));
303
304 case SIOCSIFNETMASK:
305 i = ifra->ifra_addr.sin_addr.s_addr;
306 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
307 break;
308
309 case SIOCAIFADDR:
310 maskIsNew = 0;
311 hostIsNew = 1;
312 error = 0;
313 if (ia->ia_addr.sin_family == AF_INET) {
314 if (ifra->ifra_addr.sin_len == 0) {
315 ifra->ifra_addr = ia->ia_addr;
316 hostIsNew = 0;
317 } else if (ifra->ifra_addr.sin_addr.s_addr ==
318 ia->ia_addr.sin_addr.s_addr)
319 hostIsNew = 0;
320 }
321 if (ifra->ifra_mask.sin_len) {
322 in_ifscrub(ifp, ia);
323 ia->ia_sockmask = ifra->ifra_mask;
324 ia->ia_subnetmask =
325 ntohl(ia->ia_sockmask.sin_addr.s_addr);
326 maskIsNew = 1;
327 }
328 if ((ifp->if_flags & IFF_POINTOPOINT) &&
329 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
330 in_ifscrub(ifp, ia);
331 ia->ia_dstaddr = ifra->ifra_dstaddr;
332 maskIsNew = 1; /* We lie; but the effect's the same */
333 }
334 if (ifra->ifra_addr.sin_family == AF_INET &&
335 (hostIsNew || maskIsNew))
336 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
337 if ((ifp->if_flags & IFF_BROADCAST) &&
338 (ifra->ifra_broadaddr.sin_family == AF_INET))
339 ia->ia_broadaddr = ifra->ifra_broadaddr;
340 return (error);
341
342 case SIOCDIFADDR:
343 in_ifscrub(ifp, ia);
344 /*
345 * Protect from ipintr() traversing address list
346 * while we're modifying it.
347 */
348 s = splnet();
349
350 ifa = &ia->ia_ifa;
351 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
352 oia = ia;
353 TAILQ_REMOVE(&in_ifaddrhead, oia, ia_link);
354 IFAFREE(&oia->ia_ifa);
355 splx(s);
356 break;
357
358 default:
359 if (ifp == 0 || ifp->if_ioctl == 0)
360 return (EOPNOTSUPP);
361 return ((*ifp->if_ioctl)(ifp, cmd, data));
362 }
363 return (0);
364}
365
366/*
232 /*
233 * Find address for this interface, if it exists.
234 *
235 * If an alias address was specified, find that one instead of
236 * the first one on the interface.
237 */
238 if (ifp)
239 for (iap = in_ifaddrhead.tqh_first; iap;
240 iap = iap->ia_link.tqe_next)
241 if (iap->ia_ifp == ifp) {
242 if (((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr.s_addr ==
243 iap->ia_addr.sin_addr.s_addr) {
244 ia = iap;
245 break;
246 } else if (ia == NULL) {
247 ia = iap;
248 if (ifr->ifr_addr.sa_family != AF_INET)
249 break;
250 }
251 }
252
253 switch (cmd) {
254
255 case SIOCAIFADDR:
256 case SIOCDIFADDR:
257 if (ifp == 0)
258 return (EADDRNOTAVAIL);
259 if (ifra->ifra_addr.sin_family == AF_INET) {
260 for (oia = ia; ia; ia = ia->ia_link.tqe_next) {
261 if (ia->ia_ifp == ifp &&
262 ia->ia_addr.sin_addr.s_addr ==
263 ifra->ifra_addr.sin_addr.s_addr)
264 break;
265 }
266 if ((ifp->if_flags & IFF_POINTOPOINT)
267 && (cmd == SIOCAIFADDR)
268 && (ifra->ifra_dstaddr.sin_addr.s_addr
269 == INADDR_ANY)) {
270 return EDESTADDRREQ;
271 }
272 }
273 if (cmd == SIOCDIFADDR && ia == 0)
274 return (EADDRNOTAVAIL);
275 /* FALLTHROUGH */
276 case SIOCSIFADDR:
277 case SIOCSIFNETMASK:
278 case SIOCSIFDSTADDR:
279 if (p && (error = suser(p)) != 0)
280 return error;
281
282 if (ifp == 0)
283 return (EADDRNOTAVAIL);
284 if (ia == (struct in_ifaddr *)0) {
285 ia = (struct in_ifaddr *)
286 malloc(sizeof *ia, M_IFADDR, M_WAITOK);
287 if (ia == (struct in_ifaddr *)NULL)
288 return (ENOBUFS);
289 bzero((caddr_t)ia, sizeof *ia);
290 /*
291 * Protect from ipintr() traversing address list
292 * while we're modifying it.
293 */
294 s = splnet();
295
296 TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
297 ifa = &ia->ia_ifa;
298 TAILQ_INSERT_TAIL(&ifp->if_addrhead, ifa, ifa_link);
299
300 ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
301 ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
302 ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
303 ia->ia_sockmask.sin_len = 8;
304 if (ifp->if_flags & IFF_BROADCAST) {
305 ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr);
306 ia->ia_broadaddr.sin_family = AF_INET;
307 }
308 ia->ia_ifp = ifp;
309 if (!(ifp->if_flags & IFF_LOOPBACK))
310 in_interfaces++;
311 splx(s);
312 }
313 break;
314
315 case SIOCSIFBRDADDR:
316 if (p && (error = suser(p)) != 0)
317 return error;
318 /* FALLTHROUGH */
319
320 case SIOCGIFADDR:
321 case SIOCGIFNETMASK:
322 case SIOCGIFDSTADDR:
323 case SIOCGIFBRDADDR:
324 if (ia == (struct in_ifaddr *)0)
325 return (EADDRNOTAVAIL);
326 break;
327 }
328 switch (cmd) {
329
330 case SIOCGIFADDR:
331 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
332 break;
333
334 case SIOCGIFBRDADDR:
335 if ((ifp->if_flags & IFF_BROADCAST) == 0)
336 return (EINVAL);
337 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
338 break;
339
340 case SIOCGIFDSTADDR:
341 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
342 return (EINVAL);
343 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
344 break;
345
346 case SIOCGIFNETMASK:
347 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
348 break;
349
350 case SIOCSIFDSTADDR:
351 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
352 return (EINVAL);
353 oldaddr = ia->ia_dstaddr;
354 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
355 if (ifp->if_ioctl && (error = (*ifp->if_ioctl)
356 (ifp, SIOCSIFDSTADDR, (caddr_t)ia))) {
357 ia->ia_dstaddr = oldaddr;
358 return (error);
359 }
360 if (ia->ia_flags & IFA_ROUTE) {
361 ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
362 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
363 ia->ia_ifa.ifa_dstaddr =
364 (struct sockaddr *)&ia->ia_dstaddr;
365 rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP);
366 }
367 break;
368
369 case SIOCSIFBRDADDR:
370 if ((ifp->if_flags & IFF_BROADCAST) == 0)
371 return (EINVAL);
372 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
373 break;
374
375 case SIOCSIFADDR:
376 return (in_ifinit(ifp, ia,
377 (struct sockaddr_in *) &ifr->ifr_addr, 1));
378
379 case SIOCSIFNETMASK:
380 i = ifra->ifra_addr.sin_addr.s_addr;
381 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i);
382 break;
383
384 case SIOCAIFADDR:
385 maskIsNew = 0;
386 hostIsNew = 1;
387 error = 0;
388 if (ia->ia_addr.sin_family == AF_INET) {
389 if (ifra->ifra_addr.sin_len == 0) {
390 ifra->ifra_addr = ia->ia_addr;
391 hostIsNew = 0;
392 } else if (ifra->ifra_addr.sin_addr.s_addr ==
393 ia->ia_addr.sin_addr.s_addr)
394 hostIsNew = 0;
395 }
396 if (ifra->ifra_mask.sin_len) {
397 in_ifscrub(ifp, ia);
398 ia->ia_sockmask = ifra->ifra_mask;
399 ia->ia_subnetmask =
400 ntohl(ia->ia_sockmask.sin_addr.s_addr);
401 maskIsNew = 1;
402 }
403 if ((ifp->if_flags & IFF_POINTOPOINT) &&
404 (ifra->ifra_dstaddr.sin_family == AF_INET)) {
405 in_ifscrub(ifp, ia);
406 ia->ia_dstaddr = ifra->ifra_dstaddr;
407 maskIsNew = 1; /* We lie; but the effect's the same */
408 }
409 if (ifra->ifra_addr.sin_family == AF_INET &&
410 (hostIsNew || maskIsNew))
411 error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
412 if ((ifp->if_flags & IFF_BROADCAST) &&
413 (ifra->ifra_broadaddr.sin_family == AF_INET))
414 ia->ia_broadaddr = ifra->ifra_broadaddr;
415 return (error);
416
417 case SIOCDIFADDR:
418 in_ifscrub(ifp, ia);
419 /*
420 * Protect from ipintr() traversing address list
421 * while we're modifying it.
422 */
423 s = splnet();
424
425 ifa = &ia->ia_ifa;
426 TAILQ_REMOVE(&ifp->if_addrhead, ifa, ifa_link);
427 oia = ia;
428 TAILQ_REMOVE(&in_ifaddrhead, oia, ia_link);
429 IFAFREE(&oia->ia_ifa);
430 splx(s);
431 break;
432
433 default:
434 if (ifp == 0 || ifp->if_ioctl == 0)
435 return (EOPNOTSUPP);
436 return ((*ifp->if_ioctl)(ifp, cmd, data));
437 }
438 return (0);
439}
440
441/*
442 * SIOC[GAD]LIFADDR.
443 * SIOCGLIFADDR: get first address. (?!?)
444 * SIOCGLIFADDR with IFLR_PREFIX:
445 * get first address that matches the specified prefix.
446 * SIOCALIFADDR: add the specified address.
447 * SIOCALIFADDR with IFLR_PREFIX:
448 * EINVAL since we can't deduce hostid part of the address.
449 * SIOCDLIFADDR: delete the specified address.
450 * SIOCDLIFADDR with IFLR_PREFIX:
451 * delete the first address that matches the specified prefix.
452 * return values:
453 * EINVAL on invalid parameters
454 * EADDRNOTAVAIL on prefix match failed/specified address not found
455 * other values may be returned from in_ioctl()
456 */
457static int
458in_lifaddr_ioctl(so, cmd, data, ifp, p)
459 struct socket *so;
460 u_long cmd;
461 caddr_t data;
462 struct ifnet *ifp;
463 struct proc *p;
464{
465 struct if_laddrreq *iflr = (struct if_laddrreq *)data;
466 struct ifaddr *ifa;
467
468 /* sanity checks */
469 if (!data || !ifp) {
470 panic("invalid argument to in_lifaddr_ioctl");
471 /*NOTRECHED*/
472 }
473
474 switch (cmd) {
475 case SIOCGLIFADDR:
476 /* address must be specified on GET with IFLR_PREFIX */
477 if ((iflr->flags & IFLR_PREFIX) == 0)
478 break;
479 /*FALLTHROUGH*/
480 case SIOCALIFADDR:
481 case SIOCDLIFADDR:
482 /* address must be specified on ADD and DELETE */
483 if (iflr->addr.__ss_family != AF_INET)
484 return EINVAL;
485 if (iflr->addr.__ss_len != sizeof(struct sockaddr_in))
486 return EINVAL;
487 /* XXX need improvement */
488 if (iflr->dstaddr.__ss_family
489 && iflr->dstaddr.__ss_family != AF_INET)
490 return EINVAL;
491 if (iflr->dstaddr.__ss_family
492 && iflr->dstaddr.__ss_len != sizeof(struct sockaddr_in))
493 return EINVAL;
494 break;
495 default: /*shouldn't happen*/
496 return EOPNOTSUPP;
497 }
498 if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
499 return EINVAL;
500
501 switch (cmd) {
502 case SIOCALIFADDR:
503 {
504 struct in_aliasreq ifra;
505
506 if (iflr->flags & IFLR_PREFIX)
507 return EINVAL;
508
509 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
510 bzero(&ifra, sizeof(ifra));
511 bcopy(iflr->iflr_name, ifra.ifra_name,
512 sizeof(ifra.ifra_name));
513
514 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.__ss_len);
515
516 if (iflr->dstaddr.__ss_family) { /*XXX*/
517 bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
518 iflr->dstaddr.__ss_len);
519 }
520
521 ifra.ifra_mask.sin_family = AF_INET;
522 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
523 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
524
525 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, p);
526 }
527 case SIOCGLIFADDR:
528 case SIOCDLIFADDR:
529 {
530 struct in_ifaddr *ia;
531 struct in_addr mask, candidate, match;
532 struct sockaddr_in *sin;
533 int cmp;
534
535 bzero(&mask, sizeof(mask));
536 if (iflr->flags & IFLR_PREFIX) {
537 /* lookup a prefix rather than address. */
538 in_len2mask(&mask, iflr->prefixlen);
539
540 sin = (struct sockaddr_in *)&iflr->addr;
541 match.s_addr = sin->sin_addr.s_addr;
542 match.s_addr &= mask.s_addr;
543
544 /* if you set extra bits, that's wrong */
545 if (match.s_addr != sin->sin_addr.s_addr)
546 return EINVAL;
547
548 cmp = 1;
549 } else {
550 if (cmd == SIOCGLIFADDR) {
551 /* on getting an address, take the 1st match */
552 cmp = 0; /*XXX*/
553 } else {
554 /* on deleting an address, do exact match */
555 in_len2mask(&mask, 32);
556 sin = (struct sockaddr_in *)&iflr->addr;
557 match.s_addr = sin->sin_addr.s_addr;
558
559 cmp = 1;
560 }
561 }
562
563 TAILQ_FOREACH(ifa, &ifp->if_addrhead, ifa_link) {
564 if (ifa->ifa_addr->sa_family != AF_INET6)
565 continue;
566 if (!cmp)
567 break;
568 candidate.s_addr = ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
569 candidate.s_addr &= mask.s_addr;
570 if (candidate.s_addr == match.s_addr)
571 break;
572 }
573 if (!ifa)
574 return EADDRNOTAVAIL;
575 ia = (struct in_ifaddr *)ifa;
576
577 if (cmd == SIOCGLIFADDR) {
578 /* fill in the if_laddrreq structure */
579 bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
580
581 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
582 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
583 ia->ia_dstaddr.sin_len);
584 } else
585 bzero(&iflr->dstaddr, sizeof(iflr->dstaddr));
586
587 iflr->prefixlen =
588 in_mask2len(&ia->ia_sockmask.sin_addr);
589
590 iflr->flags = 0; /*XXX*/
591
592 return 0;
593 } else {
594 struct in_aliasreq ifra;
595
596 /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
597 bzero(&ifra, sizeof(ifra));
598 bcopy(iflr->iflr_name, ifra.ifra_name,
599 sizeof(ifra.ifra_name));
600
601 bcopy(&ia->ia_addr, &ifra.ifra_addr,
602 ia->ia_addr.sin_len);
603 if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
604 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
605 ia->ia_dstaddr.sin_len);
606 }
607 bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
608 ia->ia_sockmask.sin_len);
609
610 return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
611 ifp, p);
612 }
613 }
614 }
615
616 return EOPNOTSUPP; /*just for safety*/
617}
618
619/*
367 * Delete any existing route for an interface.
368 */
369void
370in_ifscrub(ifp, ia)
371 register struct ifnet *ifp;
372 register struct in_ifaddr *ia;
373{
374
375 if ((ia->ia_flags & IFA_ROUTE) == 0)
376 return;
377 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
378 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
379 else
380 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
381 ia->ia_flags &= ~IFA_ROUTE;
382}
383
384/*
385 * Initialize an interface's internet address
386 * and routing table entry.
387 */
388static int
389in_ifinit(ifp, ia, sin, scrub)
390 register struct ifnet *ifp;
391 register struct in_ifaddr *ia;
392 struct sockaddr_in *sin;
393 int scrub;
394{
395 register u_long i = ntohl(sin->sin_addr.s_addr);
396 struct sockaddr_in oldaddr;
397 int s = splimp(), flags = RTF_UP, error;
398
399 oldaddr = ia->ia_addr;
400 ia->ia_addr = *sin;
401 /*
402 * Give the interface a chance to initialize
403 * if this is its first address,
404 * and to validate the address if necessary.
405 */
406 if (ifp->if_ioctl &&
407 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
408 splx(s);
409 ia->ia_addr = oldaddr;
410 return (error);
411 }
412 splx(s);
413 if (scrub) {
414 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
415 in_ifscrub(ifp, ia);
416 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
417 }
418 if (IN_CLASSA(i))
419 ia->ia_netmask = IN_CLASSA_NET;
420 else if (IN_CLASSB(i))
421 ia->ia_netmask = IN_CLASSB_NET;
422 else
423 ia->ia_netmask = IN_CLASSC_NET;
424 /*
425 * The subnet mask usually includes at least the standard network part,
426 * but may may be smaller in the case of supernetting.
427 * If it is set, we believe it.
428 */
429 if (ia->ia_subnetmask == 0) {
430 ia->ia_subnetmask = ia->ia_netmask;
431 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
432 } else
433 ia->ia_netmask &= ia->ia_subnetmask;
434 ia->ia_net = i & ia->ia_netmask;
435 ia->ia_subnet = i & ia->ia_subnetmask;
436 in_socktrim(&ia->ia_sockmask);
437 /*
438 * Add route for the network.
439 */
440 ia->ia_ifa.ifa_metric = ifp->if_metric;
441 if (ifp->if_flags & IFF_BROADCAST) {
442 ia->ia_broadaddr.sin_addr.s_addr =
443 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
444 ia->ia_netbroadcast.s_addr =
445 htonl(ia->ia_net | ~ ia->ia_netmask);
446 } else if (ifp->if_flags & IFF_LOOPBACK) {
447 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
448 flags |= RTF_HOST;
449 } else if (ifp->if_flags & IFF_POINTOPOINT) {
450 if (ia->ia_dstaddr.sin_family != AF_INET)
451 return (0);
452 flags |= RTF_HOST;
453 }
454 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
455 ia->ia_flags |= IFA_ROUTE;
456
457 /*
458 * If the interface supports multicast, join the "all hosts"
459 * multicast group on that interface.
460 */
461 if (ifp->if_flags & IFF_MULTICAST) {
462 struct in_addr addr;
463
464 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
465 in_addmulti(&addr, ifp);
466 }
467 return (error);
468}
469
470
471/*
472 * Return 1 if the address might be a local broadcast address.
473 */
474int
475in_broadcast(in, ifp)
476 struct in_addr in;
477 struct ifnet *ifp;
478{
479 register struct ifaddr *ifa;
480 u_long t;
481
482 if (in.s_addr == INADDR_BROADCAST ||
483 in.s_addr == INADDR_ANY)
484 return 1;
485 if ((ifp->if_flags & IFF_BROADCAST) == 0)
486 return 0;
487 t = ntohl(in.s_addr);
488 /*
489 * Look through the list of addresses for a match
490 * with a broadcast address.
491 */
492#define ia ((struct in_ifaddr *)ifa)
493 for (ifa = ifp->if_addrhead.tqh_first; ifa;
494 ifa = ifa->ifa_link.tqe_next)
495 if (ifa->ifa_addr->sa_family == AF_INET &&
496 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
497 in.s_addr == ia->ia_netbroadcast.s_addr ||
498 /*
499 * Check for old-style (host 0) broadcast.
500 */
501 t == ia->ia_subnet || t == ia->ia_net) &&
502 /*
503 * Check for an all one subnetmask. These
504 * only exist when an interface gets a secondary
505 * address.
506 */
507 ia->ia_subnetmask != (u_long)0xffffffff)
508 return 1;
509 return (0);
510#undef ia
511}
512/*
513 * Add an address to the list of IP multicast addresses for a given interface.
514 */
515struct in_multi *
516in_addmulti(ap, ifp)
517 register struct in_addr *ap;
518 register struct ifnet *ifp;
519{
520 register struct in_multi *inm;
521 int error;
522 struct sockaddr_in sin;
523 struct ifmultiaddr *ifma;
524 int s = splnet();
525
526 /*
527 * Call generic routine to add membership or increment
528 * refcount. It wants addresses in the form of a sockaddr,
529 * so we build one here (being careful to zero the unused bytes).
530 */
531 bzero(&sin, sizeof sin);
532 sin.sin_family = AF_INET;
533 sin.sin_len = sizeof sin;
534 sin.sin_addr = *ap;
535 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
536 if (error) {
537 splx(s);
538 return 0;
539 }
540
541 /*
542 * If ifma->ifma_protospec is null, then if_addmulti() created
543 * a new record. Otherwise, we are done.
544 */
545 if (ifma->ifma_protospec != 0)
546 return ifma->ifma_protospec;
547
548 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
549 at interrupt time? If so, need to fix if_addmulti. XXX */
550 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT);
551 if (inm == NULL) {
552 splx(s);
553 return (NULL);
554 }
555
556 bzero(inm, sizeof *inm);
557 inm->inm_addr = *ap;
558 inm->inm_ifp = ifp;
559 inm->inm_ifma = ifma;
560 ifma->ifma_protospec = inm;
561 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
562
563 /*
564 * Let IGMP know that we have joined a new IP multicast group.
565 */
566 igmp_joingroup(inm);
567 splx(s);
568 return (inm);
569}
570
571/*
572 * Delete a multicast address record.
573 */
574void
575in_delmulti(inm)
576 register struct in_multi *inm;
577{
578 struct ifmultiaddr *ifma = inm->inm_ifma;
579 struct in_multi my_inm;
580 int s = splnet();
581
582 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
583 if (ifma->ifma_refcount == 1) {
584 /*
585 * No remaining claims to this record; let IGMP know that
586 * we are leaving the multicast group.
587 * But do it after the if_delmulti() which might reset
588 * the interface and nuke the packet.
589 */
590 my_inm = *inm ;
591 ifma->ifma_protospec = 0;
592 LIST_REMOVE(inm, inm_link);
593 free(inm, M_IPMADDR);
594 }
595 /* XXX - should be separate API for when we have an ifma? */
596 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
597 if (my_inm.inm_ifp != NULL)
598 igmp_leavegroup(&my_inm);
599 splx(s);
600}
620 * Delete any existing route for an interface.
621 */
622void
623in_ifscrub(ifp, ia)
624 register struct ifnet *ifp;
625 register struct in_ifaddr *ia;
626{
627
628 if ((ia->ia_flags & IFA_ROUTE) == 0)
629 return;
630 if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
631 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST);
632 else
633 rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0);
634 ia->ia_flags &= ~IFA_ROUTE;
635}
636
637/*
638 * Initialize an interface's internet address
639 * and routing table entry.
640 */
641static int
642in_ifinit(ifp, ia, sin, scrub)
643 register struct ifnet *ifp;
644 register struct in_ifaddr *ia;
645 struct sockaddr_in *sin;
646 int scrub;
647{
648 register u_long i = ntohl(sin->sin_addr.s_addr);
649 struct sockaddr_in oldaddr;
650 int s = splimp(), flags = RTF_UP, error;
651
652 oldaddr = ia->ia_addr;
653 ia->ia_addr = *sin;
654 /*
655 * Give the interface a chance to initialize
656 * if this is its first address,
657 * and to validate the address if necessary.
658 */
659 if (ifp->if_ioctl &&
660 (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, (caddr_t)ia))) {
661 splx(s);
662 ia->ia_addr = oldaddr;
663 return (error);
664 }
665 splx(s);
666 if (scrub) {
667 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
668 in_ifscrub(ifp, ia);
669 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
670 }
671 if (IN_CLASSA(i))
672 ia->ia_netmask = IN_CLASSA_NET;
673 else if (IN_CLASSB(i))
674 ia->ia_netmask = IN_CLASSB_NET;
675 else
676 ia->ia_netmask = IN_CLASSC_NET;
677 /*
678 * The subnet mask usually includes at least the standard network part,
679 * but may may be smaller in the case of supernetting.
680 * If it is set, we believe it.
681 */
682 if (ia->ia_subnetmask == 0) {
683 ia->ia_subnetmask = ia->ia_netmask;
684 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
685 } else
686 ia->ia_netmask &= ia->ia_subnetmask;
687 ia->ia_net = i & ia->ia_netmask;
688 ia->ia_subnet = i & ia->ia_subnetmask;
689 in_socktrim(&ia->ia_sockmask);
690 /*
691 * Add route for the network.
692 */
693 ia->ia_ifa.ifa_metric = ifp->if_metric;
694 if (ifp->if_flags & IFF_BROADCAST) {
695 ia->ia_broadaddr.sin_addr.s_addr =
696 htonl(ia->ia_subnet | ~ia->ia_subnetmask);
697 ia->ia_netbroadcast.s_addr =
698 htonl(ia->ia_net | ~ ia->ia_netmask);
699 } else if (ifp->if_flags & IFF_LOOPBACK) {
700 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
701 flags |= RTF_HOST;
702 } else if (ifp->if_flags & IFF_POINTOPOINT) {
703 if (ia->ia_dstaddr.sin_family != AF_INET)
704 return (0);
705 flags |= RTF_HOST;
706 }
707 if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0)
708 ia->ia_flags |= IFA_ROUTE;
709
710 /*
711 * If the interface supports multicast, join the "all hosts"
712 * multicast group on that interface.
713 */
714 if (ifp->if_flags & IFF_MULTICAST) {
715 struct in_addr addr;
716
717 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
718 in_addmulti(&addr, ifp);
719 }
720 return (error);
721}
722
723
724/*
725 * Return 1 if the address might be a local broadcast address.
726 */
727int
728in_broadcast(in, ifp)
729 struct in_addr in;
730 struct ifnet *ifp;
731{
732 register struct ifaddr *ifa;
733 u_long t;
734
735 if (in.s_addr == INADDR_BROADCAST ||
736 in.s_addr == INADDR_ANY)
737 return 1;
738 if ((ifp->if_flags & IFF_BROADCAST) == 0)
739 return 0;
740 t = ntohl(in.s_addr);
741 /*
742 * Look through the list of addresses for a match
743 * with a broadcast address.
744 */
745#define ia ((struct in_ifaddr *)ifa)
746 for (ifa = ifp->if_addrhead.tqh_first; ifa;
747 ifa = ifa->ifa_link.tqe_next)
748 if (ifa->ifa_addr->sa_family == AF_INET &&
749 (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
750 in.s_addr == ia->ia_netbroadcast.s_addr ||
751 /*
752 * Check for old-style (host 0) broadcast.
753 */
754 t == ia->ia_subnet || t == ia->ia_net) &&
755 /*
756 * Check for an all one subnetmask. These
757 * only exist when an interface gets a secondary
758 * address.
759 */
760 ia->ia_subnetmask != (u_long)0xffffffff)
761 return 1;
762 return (0);
763#undef ia
764}
765/*
766 * Add an address to the list of IP multicast addresses for a given interface.
767 */
768struct in_multi *
769in_addmulti(ap, ifp)
770 register struct in_addr *ap;
771 register struct ifnet *ifp;
772{
773 register struct in_multi *inm;
774 int error;
775 struct sockaddr_in sin;
776 struct ifmultiaddr *ifma;
777 int s = splnet();
778
779 /*
780 * Call generic routine to add membership or increment
781 * refcount. It wants addresses in the form of a sockaddr,
782 * so we build one here (being careful to zero the unused bytes).
783 */
784 bzero(&sin, sizeof sin);
785 sin.sin_family = AF_INET;
786 sin.sin_len = sizeof sin;
787 sin.sin_addr = *ap;
788 error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
789 if (error) {
790 splx(s);
791 return 0;
792 }
793
794 /*
795 * If ifma->ifma_protospec is null, then if_addmulti() created
796 * a new record. Otherwise, we are done.
797 */
798 if (ifma->ifma_protospec != 0)
799 return ifma->ifma_protospec;
800
801 /* XXX - if_addmulti uses M_WAITOK. Can this really be called
802 at interrupt time? If so, need to fix if_addmulti. XXX */
803 inm = (struct in_multi *)malloc(sizeof(*inm), M_IPMADDR, M_NOWAIT);
804 if (inm == NULL) {
805 splx(s);
806 return (NULL);
807 }
808
809 bzero(inm, sizeof *inm);
810 inm->inm_addr = *ap;
811 inm->inm_ifp = ifp;
812 inm->inm_ifma = ifma;
813 ifma->ifma_protospec = inm;
814 LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
815
816 /*
817 * Let IGMP know that we have joined a new IP multicast group.
818 */
819 igmp_joingroup(inm);
820 splx(s);
821 return (inm);
822}
823
824/*
825 * Delete a multicast address record.
826 */
827void
828in_delmulti(inm)
829 register struct in_multi *inm;
830{
831 struct ifmultiaddr *ifma = inm->inm_ifma;
832 struct in_multi my_inm;
833 int s = splnet();
834
835 my_inm.inm_ifp = NULL ; /* don't send the leave msg */
836 if (ifma->ifma_refcount == 1) {
837 /*
838 * No remaining claims to this record; let IGMP know that
839 * we are leaving the multicast group.
840 * But do it after the if_delmulti() which might reset
841 * the interface and nuke the packet.
842 */
843 my_inm = *inm ;
844 ifma->ifma_protospec = 0;
845 LIST_REMOVE(inm, inm_link);
846 free(inm, M_IPMADDR);
847 }
848 /* XXX - should be separate API for when we have an ifma? */
849 if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
850 if (my_inm.inm_ifp != NULL)
851 igmp_leavegroup(&my_inm);
852 splx(s);
853}