Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1991 Regents of the University of California. | |
3 | * 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 | * | |
38e82238 | 33 | * from: @(#)in.c 7.17 (Berkeley) 4/20/91 |
dec69c36 | 34 | * $Id: in.c,v 1.3 1993/11/07 17:47:49 wollman Exp $ |
15637ed4 RG |
35 | */ |
36 | ||
37 | #include "param.h" | |
38 | #include "ioctl.h" | |
39 | #include "mbuf.h" | |
40 | #include "socket.h" | |
41 | #include "socketvar.h" | |
42 | #include "in_systm.h" | |
43 | #include "net/if.h" | |
44 | #include "net/route.h" | |
15637ed4 RG |
45 | #include "in.h" |
46 | #include "in_var.h" | |
47 | ||
48 | #ifdef INET | |
8ace4366 GW |
49 | struct in_ifaddr *in_ifaddr; |
50 | struct ifqueue ipintrq; | |
51 | ||
15637ed4 RG |
52 | /* |
53 | * Formulate an Internet address from network + host. | |
54 | */ | |
55 | struct in_addr | |
56 | in_makeaddr(net, host) | |
57 | u_long net, host; | |
58 | { | |
59 | register struct in_ifaddr *ia; | |
60 | register u_long mask; | |
61 | u_long addr; | |
62 | ||
63 | if (IN_CLASSA(net)) | |
64 | mask = IN_CLASSA_HOST; | |
65 | else if (IN_CLASSB(net)) | |
66 | mask = IN_CLASSB_HOST; | |
67 | else | |
68 | mask = IN_CLASSC_HOST; | |
69 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
70 | if ((ia->ia_netmask & net) == ia->ia_net) { | |
71 | mask = ~ia->ia_subnetmask; | |
72 | break; | |
73 | } | |
74 | addr = htonl(net | (host & mask)); | |
75 | return (*(struct in_addr *)&addr); | |
76 | } | |
77 | ||
78 | /* | |
79 | * Return the network number from an internet address. | |
80 | */ | |
81 | u_long | |
82 | in_netof(in) | |
83 | struct in_addr in; | |
84 | { | |
85 | register u_long i = ntohl(in.s_addr); | |
86 | register u_long net; | |
87 | register struct in_ifaddr *ia; | |
88 | ||
89 | if (IN_CLASSA(i)) | |
90 | net = i & IN_CLASSA_NET; | |
91 | else if (IN_CLASSB(i)) | |
92 | net = i & IN_CLASSB_NET; | |
93 | else if (IN_CLASSC(i)) | |
94 | net = i & IN_CLASSC_NET; | |
95 | else | |
96 | return (0); | |
97 | ||
98 | /* | |
99 | * Check whether network is a subnet; | |
100 | * if so, return subnet number. | |
101 | */ | |
102 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
103 | if (net == ia->ia_net) | |
104 | return (i & ia->ia_subnetmask); | |
105 | return (net); | |
106 | } | |
107 | ||
108 | /* | |
109 | * Compute and save network mask as sockaddr from an internet address. | |
110 | */ | |
111 | in_sockmaskof(in, sockmask) | |
112 | struct in_addr in; | |
113 | register struct sockaddr_in *sockmask; | |
114 | { | |
115 | register u_long net; | |
116 | register u_long mask; | |
117 | { | |
118 | register u_long i = ntohl(in.s_addr); | |
119 | ||
120 | if (i == 0) | |
121 | net = 0, mask = 0; | |
122 | else if (IN_CLASSA(i)) | |
123 | net = i & IN_CLASSA_NET, mask = IN_CLASSA_NET; | |
124 | else if (IN_CLASSB(i)) | |
125 | net = i & IN_CLASSB_NET, mask = IN_CLASSB_NET; | |
126 | else if (IN_CLASSC(i)) | |
127 | net = i & IN_CLASSC_NET, mask = IN_CLASSC_NET; | |
128 | else | |
129 | net = i, mask = -1; | |
130 | } | |
131 | { | |
132 | register struct in_ifaddr *ia; | |
133 | /* | |
134 | * Check whether network is a subnet; | |
135 | * if so, return subnet number. | |
136 | */ | |
137 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
138 | if (net == ia->ia_net) | |
139 | mask = ia->ia_subnetmask; | |
140 | } | |
141 | { | |
142 | register char *cpbase = (char *)&(sockmask->sin_addr); | |
143 | register char *cp = (char *)(1 + &(sockmask->sin_addr)); | |
144 | ||
145 | sockmask->sin_addr.s_addr = htonl(mask); | |
146 | sockmask->sin_len = 0; | |
147 | while (--cp >= cpbase) | |
148 | if (*cp) { | |
149 | sockmask->sin_len = 1 + cp - (caddr_t)sockmask; | |
150 | break; | |
151 | } | |
152 | } | |
153 | } | |
154 | ||
155 | /* | |
156 | * Return the host portion of an internet address. | |
157 | */ | |
158 | u_long | |
159 | in_lnaof(in) | |
160 | struct in_addr in; | |
161 | { | |
162 | register u_long i = ntohl(in.s_addr); | |
163 | register u_long net, host; | |
164 | register struct in_ifaddr *ia; | |
165 | ||
166 | if (IN_CLASSA(i)) { | |
167 | net = i & IN_CLASSA_NET; | |
168 | host = i & IN_CLASSA_HOST; | |
169 | } else if (IN_CLASSB(i)) { | |
170 | net = i & IN_CLASSB_NET; | |
171 | host = i & IN_CLASSB_HOST; | |
172 | } else if (IN_CLASSC(i)) { | |
173 | net = i & IN_CLASSC_NET; | |
174 | host = i & IN_CLASSC_HOST; | |
175 | } else | |
176 | return (i); | |
177 | ||
178 | /* | |
179 | * Check whether network is a subnet; | |
180 | * if so, use the modified interpretation of `host'. | |
181 | */ | |
182 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
183 | if (net == ia->ia_net) | |
184 | return (host &~ ia->ia_subnetmask); | |
185 | return (host); | |
186 | } | |
187 | ||
188 | #ifndef SUBNETSARELOCAL | |
189 | #define SUBNETSARELOCAL 1 | |
190 | #endif | |
191 | int subnetsarelocal = SUBNETSARELOCAL; | |
192 | /* | |
193 | * Return 1 if an internet address is for a ``local'' host | |
194 | * (one to which we have a connection). If subnetsarelocal | |
195 | * is true, this includes other subnets of the local net. | |
196 | * Otherwise, it includes only the directly-connected (sub)nets. | |
197 | */ | |
198 | in_localaddr(in) | |
199 | struct in_addr in; | |
200 | { | |
201 | register u_long i = ntohl(in.s_addr); | |
202 | register struct in_ifaddr *ia; | |
203 | ||
204 | if (subnetsarelocal) { | |
205 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
206 | if ((i & ia->ia_netmask) == ia->ia_net) | |
207 | return (1); | |
208 | } else { | |
209 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
210 | if ((i & ia->ia_subnetmask) == ia->ia_subnet) | |
211 | return (1); | |
212 | } | |
213 | return (0); | |
214 | } | |
215 | ||
216 | /* | |
217 | * Determine whether an IP address is in a reserved set of addresses | |
218 | * that may not be forwarded, or whether datagrams to that destination | |
219 | * may be forwarded. | |
220 | */ | |
221 | in_canforward(in) | |
222 | struct in_addr in; | |
223 | { | |
224 | register u_long i = ntohl(in.s_addr); | |
225 | register u_long net; | |
226 | ||
227 | if (IN_EXPERIMENTAL(i)) | |
228 | return (0); | |
229 | if (IN_CLASSA(i)) { | |
230 | net = i & IN_CLASSA_NET; | |
231 | if (net == 0 || net == IN_LOOPBACKNET) | |
232 | return (0); | |
233 | } | |
234 | return (1); | |
235 | } | |
236 | ||
237 | int in_interfaces; /* number of external internet interfaces */ | |
238 | extern struct ifnet loif; | |
239 | ||
240 | /* | |
241 | * Generic internet control operations (ioctl's). | |
242 | * Ifp is 0 if not an interface-specific ioctl. | |
243 | */ | |
244 | /* ARGSUSED */ | |
245 | in_control(so, cmd, data, ifp) | |
246 | struct socket *so; | |
247 | int cmd; | |
248 | caddr_t data; | |
249 | register struct ifnet *ifp; | |
250 | { | |
251 | register struct ifreq *ifr = (struct ifreq *)data; | |
252 | register struct in_ifaddr *ia = 0; | |
253 | register struct ifaddr *ifa; | |
254 | struct in_ifaddr *oia; | |
255 | struct in_aliasreq *ifra = (struct in_aliasreq *)data; | |
256 | struct mbuf *m; | |
257 | struct sockaddr_in oldaddr; | |
258 | int error, hostIsNew, maskIsNew; | |
259 | u_long i; | |
260 | ||
261 | /* | |
262 | * Find address for this interface, if it exists. | |
263 | */ | |
264 | if (ifp) | |
265 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
266 | if (ia->ia_ifp == ifp) | |
267 | break; | |
268 | ||
269 | switch (cmd) { | |
270 | ||
271 | case SIOCAIFADDR: | |
272 | case SIOCDIFADDR: | |
273 | if (ifra->ifra_addr.sin_family == AF_INET) | |
274 | for (oia = ia; ia; ia = ia->ia_next) { | |
275 | if (ia->ia_ifp == ifp && | |
276 | ia->ia_addr.sin_addr.s_addr == | |
277 | ifra->ifra_addr.sin_addr.s_addr) | |
278 | break; | |
279 | } | |
280 | if (cmd == SIOCDIFADDR && ia == 0) | |
281 | return (EADDRNOTAVAIL); | |
282 | /* FALLTHROUGH */ | |
283 | case SIOCSIFADDR: | |
284 | case SIOCSIFNETMASK: | |
285 | case SIOCSIFDSTADDR: | |
286 | if ((so->so_state & SS_PRIV) == 0) | |
287 | return (EPERM); | |
288 | ||
289 | if (ifp == 0) | |
290 | panic("in_control"); | |
291 | if (ia == (struct in_ifaddr *)0) { | |
292 | m = m_getclr(M_WAIT, MT_IFADDR); | |
293 | if (m == (struct mbuf *)NULL) | |
294 | return (ENOBUFS); | |
295 | if (ia = in_ifaddr) { | |
296 | for ( ; ia->ia_next; ia = ia->ia_next) | |
297 | ; | |
298 | ia->ia_next = mtod(m, struct in_ifaddr *); | |
299 | } else | |
300 | in_ifaddr = mtod(m, struct in_ifaddr *); | |
301 | ia = mtod(m, struct in_ifaddr *); | |
302 | if (ifa = ifp->if_addrlist) { | |
303 | for ( ; ifa->ifa_next; ifa = ifa->ifa_next) | |
304 | ; | |
305 | ifa->ifa_next = (struct ifaddr *) ia; | |
306 | } else | |
307 | ifp->if_addrlist = (struct ifaddr *) ia; | |
308 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; | |
309 | ia->ia_ifa.ifa_dstaddr | |
310 | = (struct sockaddr *)&ia->ia_dstaddr; | |
311 | ia->ia_ifa.ifa_netmask | |
312 | = (struct sockaddr *)&ia->ia_sockmask; | |
313 | ia->ia_sockmask.sin_len = 8; | |
314 | if (ifp->if_flags & IFF_BROADCAST) { | |
315 | ia->ia_broadaddr.sin_len = sizeof(ia->ia_addr); | |
316 | ia->ia_broadaddr.sin_family = AF_INET; | |
317 | } | |
318 | ia->ia_ifp = ifp; | |
319 | if (ifp != &loif) | |
320 | in_interfaces++; | |
321 | } | |
322 | break; | |
323 | ||
324 | case SIOCSIFBRDADDR: | |
325 | if ((so->so_state & SS_PRIV) == 0) | |
326 | return (EPERM); | |
327 | /* FALLTHROUGH */ | |
328 | ||
329 | case SIOCGIFADDR: | |
330 | case SIOCGIFNETMASK: | |
331 | case SIOCGIFDSTADDR: | |
332 | case SIOCGIFBRDADDR: | |
333 | if (ia == (struct in_ifaddr *)0) | |
334 | return (EADDRNOTAVAIL); | |
335 | break; | |
336 | ||
337 | default: | |
338 | return (EOPNOTSUPP); | |
339 | break; | |
340 | } | |
341 | switch (cmd) { | |
342 | ||
343 | case SIOCGIFADDR: | |
344 | *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr; | |
345 | break; | |
346 | ||
347 | case SIOCGIFBRDADDR: | |
348 | if ((ifp->if_flags & IFF_BROADCAST) == 0) | |
349 | return (EINVAL); | |
350 | *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr; | |
351 | break; | |
352 | ||
353 | case SIOCGIFDSTADDR: | |
354 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) | |
355 | return (EINVAL); | |
356 | *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr; | |
357 | break; | |
358 | ||
359 | case SIOCGIFNETMASK: | |
360 | *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask; | |
361 | break; | |
362 | ||
363 | case SIOCSIFDSTADDR: | |
364 | if ((ifp->if_flags & IFF_POINTOPOINT) == 0) | |
365 | return (EINVAL); | |
366 | oldaddr = ia->ia_dstaddr; | |
367 | ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr; | |
368 | if (ifp->if_ioctl && | |
369 | (error = (*ifp->if_ioctl)(ifp, SIOCSIFDSTADDR, ia))) { | |
370 | ia->ia_dstaddr = oldaddr; | |
371 | return (error); | |
372 | } | |
373 | if (ia->ia_flags & IFA_ROUTE) { | |
374 | ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr; | |
375 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); | |
376 | ia->ia_ifa.ifa_dstaddr = | |
377 | (struct sockaddr *)&ia->ia_dstaddr; | |
378 | rtinit(&(ia->ia_ifa), (int)RTM_ADD, RTF_HOST|RTF_UP); | |
379 | } | |
380 | break; | |
381 | ||
382 | case SIOCSIFBRDADDR: | |
383 | if ((ifp->if_flags & IFF_BROADCAST) == 0) | |
384 | return (EINVAL); | |
385 | ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr; | |
386 | break; | |
387 | ||
388 | case SIOCSIFADDR: | |
389 | return (in_ifinit(ifp, ia, | |
390 | (struct sockaddr_in *) &ifr->ifr_addr, 1)); | |
391 | ||
392 | case SIOCSIFNETMASK: | |
393 | i = ifra->ifra_addr.sin_addr.s_addr; | |
394 | ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr = i); | |
395 | break; | |
396 | ||
397 | case SIOCAIFADDR: | |
398 | maskIsNew = 0; | |
399 | hostIsNew = 1; | |
400 | error = 0; | |
401 | if (ia->ia_addr.sin_family == AF_INET) { | |
402 | if (ifra->ifra_addr.sin_len == 0) { | |
403 | ifra->ifra_addr = ia->ia_addr; | |
404 | hostIsNew = 0; | |
405 | } else if (ifra->ifra_addr.sin_addr.s_addr == | |
406 | ia->ia_addr.sin_addr.s_addr) | |
407 | hostIsNew = 0; | |
408 | } | |
409 | if (ifra->ifra_mask.sin_len) { | |
410 | in_ifscrub(ifp, ia); | |
411 | ia->ia_sockmask = ifra->ifra_mask; | |
412 | ia->ia_subnetmask = | |
413 | ntohl(ia->ia_sockmask.sin_addr.s_addr); | |
414 | maskIsNew = 1; | |
415 | } | |
416 | if ((ifp->if_flags & IFF_POINTOPOINT) && | |
417 | (ifra->ifra_dstaddr.sin_family == AF_INET)) { | |
418 | in_ifscrub(ifp, ia); | |
419 | ia->ia_dstaddr = ifra->ifra_dstaddr; | |
420 | maskIsNew = 1; /* We lie; but the effect's the same */ | |
421 | } | |
422 | if (ifra->ifra_addr.sin_family == AF_INET && | |
423 | (hostIsNew || maskIsNew)) | |
424 | error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0); | |
425 | if ((ifp->if_flags & IFF_BROADCAST) && | |
426 | (ifra->ifra_broadaddr.sin_family == AF_INET)) | |
427 | ia->ia_broadaddr = ifra->ifra_broadaddr; | |
428 | return (error); | |
429 | ||
430 | case SIOCDIFADDR: | |
431 | in_ifscrub(ifp, ia); | |
432 | if ((ifa = ifp->if_addrlist) == (struct ifaddr *)ia) | |
433 | ifp->if_addrlist = ifa->ifa_next; | |
434 | else { | |
435 | while (ifa->ifa_next && | |
436 | (ifa->ifa_next != (struct ifaddr *)ia)) | |
437 | ifa = ifa->ifa_next; | |
438 | if (ifa->ifa_next) | |
439 | ifa->ifa_next = ((struct ifaddr *)ia)->ifa_next; | |
440 | else | |
441 | printf("Couldn't unlink inifaddr from ifp\n"); | |
442 | } | |
443 | oia = ia; | |
444 | if (oia == (ia = in_ifaddr)) | |
445 | in_ifaddr = ia->ia_next; | |
446 | else { | |
447 | while (ia->ia_next && (ia->ia_next != oia)) | |
448 | ia = ia->ia_next; | |
449 | if (ia->ia_next) | |
450 | ia->ia_next = oia->ia_next; | |
451 | else | |
452 | printf("Didn't unlink inifadr from list\n"); | |
453 | } | |
454 | (void) m_free(dtom(oia)); | |
455 | break; | |
456 | ||
457 | default: | |
458 | if (ifp == 0 || ifp->if_ioctl == 0) | |
459 | return (EOPNOTSUPP); | |
460 | return ((*ifp->if_ioctl)(ifp, cmd, data)); | |
461 | } | |
462 | return (0); | |
463 | } | |
464 | ||
465 | /* | |
466 | * Delete any existing route for an interface. | |
467 | */ | |
468 | in_ifscrub(ifp, ia) | |
469 | register struct ifnet *ifp; | |
470 | register struct in_ifaddr *ia; | |
471 | { | |
472 | ||
473 | if ((ia->ia_flags & IFA_ROUTE) == 0) | |
474 | return; | |
475 | if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT)) | |
476 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, RTF_HOST); | |
477 | else | |
478 | rtinit(&(ia->ia_ifa), (int)RTM_DELETE, 0); | |
479 | ia->ia_flags &= ~IFA_ROUTE; | |
480 | } | |
481 | ||
482 | /* | |
483 | * Initialize an interface's internet address | |
484 | * and routing table entry. | |
485 | */ | |
486 | in_ifinit(ifp, ia, sin, scrub) | |
487 | register struct ifnet *ifp; | |
488 | register struct in_ifaddr *ia; | |
489 | struct sockaddr_in *sin; | |
490 | { | |
491 | register u_long i = ntohl(sin->sin_addr.s_addr); | |
492 | struct sockaddr_in oldaddr; | |
493 | int s = splimp(), error, flags = RTF_UP; | |
494 | ||
495 | oldaddr = ia->ia_addr; | |
496 | ia->ia_addr = *sin; | |
497 | /* | |
498 | * Give the interface a chance to initialize | |
499 | * if this is its first address, | |
500 | * and to validate the address if necessary. | |
501 | */ | |
502 | if (ifp->if_ioctl && (error = (*ifp->if_ioctl)(ifp, SIOCSIFADDR, ia))) { | |
503 | splx(s); | |
504 | ia->ia_addr = oldaddr; | |
505 | return (error); | |
506 | } | |
507 | splx(s); | |
508 | if (scrub) { | |
509 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr; | |
510 | in_ifscrub(ifp, ia); | |
511 | ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr; | |
512 | } | |
513 | if (IN_CLASSA(i)) | |
514 | ia->ia_netmask = IN_CLASSA_NET; | |
515 | else if (IN_CLASSB(i)) | |
516 | ia->ia_netmask = IN_CLASSB_NET; | |
517 | else | |
518 | ia->ia_netmask = IN_CLASSC_NET; | |
519 | ia->ia_net = i & ia->ia_netmask; | |
520 | /* | |
521 | * The subnet mask includes at least the standard network part, | |
522 | * but may already have been set to a larger value. | |
523 | */ | |
524 | ia->ia_subnetmask |= ia->ia_netmask; | |
525 | ia->ia_subnet = i & ia->ia_subnetmask; | |
526 | ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask); | |
527 | { | |
528 | register char *cp = (char *) (1 + &(ia->ia_sockmask.sin_addr)); | |
529 | register char *cpbase = (char *) &(ia->ia_sockmask.sin_addr); | |
530 | while (--cp >= cpbase) | |
531 | if (*cp) { | |
532 | ia->ia_sockmask.sin_len = | |
533 | 1 + cp - (char *) &(ia->ia_sockmask); | |
534 | break; | |
535 | } | |
536 | } | |
537 | /* | |
538 | * Add route for the network. | |
539 | */ | |
540 | if (ifp->if_flags & IFF_BROADCAST) { | |
541 | ia->ia_broadaddr.sin_addr = | |
542 | in_makeaddr(ia->ia_subnet, INADDR_BROADCAST); | |
543 | ia->ia_netbroadcast.s_addr = | |
544 | htonl(ia->ia_net | (INADDR_BROADCAST &~ ia->ia_netmask)); | |
545 | } else if (ifp->if_flags & IFF_LOOPBACK) { | |
546 | ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr; | |
547 | flags |= RTF_HOST; | |
548 | } else if (ifp->if_flags & IFF_POINTOPOINT) { | |
549 | if (ia->ia_dstaddr.sin_family != AF_INET) | |
550 | return (0); | |
551 | flags |= RTF_HOST; | |
552 | } | |
553 | if ((error = rtinit(&(ia->ia_ifa), (int)RTM_ADD, flags)) == 0) | |
554 | ia->ia_flags |= IFA_ROUTE; | |
555 | return (error); | |
556 | } | |
557 | ||
558 | /* | |
559 | * Return address info for specified internet network. | |
560 | */ | |
561 | struct in_ifaddr * | |
562 | in_iaonnetof(net) | |
563 | u_long net; | |
564 | { | |
565 | register struct in_ifaddr *ia; | |
566 | ||
567 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
568 | if (ia->ia_subnet == net) | |
569 | return (ia); | |
570 | return ((struct in_ifaddr *)0); | |
571 | } | |
572 | ||
573 | /* | |
574 | * Return 1 if the address might be a local broadcast address. | |
575 | */ | |
576 | in_broadcast(in) | |
577 | struct in_addr in; | |
578 | { | |
579 | register struct in_ifaddr *ia; | |
580 | u_long t; | |
581 | ||
582 | /* | |
583 | * Look through the list of addresses for a match | |
584 | * with a broadcast address. | |
585 | */ | |
586 | for (ia = in_ifaddr; ia; ia = ia->ia_next) | |
587 | if (ia->ia_ifp->if_flags & IFF_BROADCAST) { | |
588 | if (ia->ia_broadaddr.sin_addr.s_addr == in.s_addr) | |
589 | return (1); | |
590 | /* | |
591 | * Check for old-style (host 0) broadcast. | |
592 | */ | |
593 | if ((t = ntohl(in.s_addr)) == ia->ia_subnet || t == ia->ia_net) | |
594 | return (1); | |
595 | } | |
596 | if (in.s_addr == INADDR_BROADCAST || in.s_addr == INADDR_ANY) | |
597 | return (1); | |
598 | return (0); | |
599 | } | |
600 | #endif |