8e984e971013ac7da0e4d66a6fd16e9ccf650952
[dragonfly.git] / sys / netinet / in.c
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: src/sys/netinet/in.c,v 1.44.2.14 2002/11/08 00:45:50 suz Exp $
35  * $DragonFly: src/sys/netinet/in.c,v 1.41 2008/08/17 05:20:10 sephe Exp $
36  */
37
38 #include "opt_bootp.h"
39
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/sockio.h>
43 #include <sys/malloc.h>
44 #include <sys/proc.h>
45 #include <sys/msgport.h>
46 #include <sys/socket.h>
47
48 #include <sys/kernel.h>
49 #include <sys/sysctl.h>
50 #include <sys/thread2.h>
51
52 #include <net/if.h>
53 #include <net/if_types.h>
54 #include <net/route.h>
55 #include <net/netmsg2.h>
56
57 #include <netinet/in.h>
58 #include <netinet/in_var.h>
59 #include <netinet/in_pcb.h>
60
61 #include <netinet/igmp_var.h>
62
63 MALLOC_DEFINE(M_IPMADDR, "in_multi", "internet multicast address");
64
65 static int in_mask2len (struct in_addr *);
66 static void in_len2mask (struct in_addr *, int);
67 static int in_lifaddr_ioctl (struct socket *, u_long, caddr_t,
68         struct ifnet *, struct thread *);
69
70 static void     in_socktrim (struct sockaddr_in *);
71 static int      in_ifinit(struct ifnet *, struct in_ifaddr *,
72                     const struct sockaddr_in *, int);
73
74 static void     in_control_dispatch(struct netmsg *);
75 static int      in_control_internal(u_long, caddr_t, struct ifnet *,
76                     struct thread *);
77
78 static int subnetsarelocal = 0;
79 SYSCTL_INT(_net_inet_ip, OID_AUTO, subnets_are_local, CTLFLAG_RW,
80         &subnetsarelocal, 0, "");
81
82 struct in_multihead in_multihead; /* XXX BSS initialization */
83
84 extern struct inpcbinfo ripcbinfo;
85 extern struct inpcbinfo udbinfo;
86
87 /*
88  * Return 1 if an internet address is for a ``local'' host
89  * (one to which we have a connection).  If subnetsarelocal
90  * is true, this includes other subnets of the local net.
91  * Otherwise, it includes only the directly-connected (sub)nets.
92  */
93 int
94 in_localaddr(struct in_addr in)
95 {
96         u_long i = ntohl(in.s_addr);
97         struct in_ifaddr_container *iac;
98         struct in_ifaddr *ia;
99
100         if (subnetsarelocal) {
101                 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
102                         ia = iac->ia;
103
104                         if ((i & ia->ia_netmask) == ia->ia_net)
105                                 return (1);
106                 }
107         } else {
108                 TAILQ_FOREACH(iac, &in_ifaddrheads[mycpuid], ia_link) {
109                         ia = iac->ia;
110
111                         if ((i & ia->ia_subnetmask) == ia->ia_subnet)
112                                 return (1);
113                 }
114         }
115         return (0);
116 }
117
118 /*
119  * Determine whether an IP address is in a reserved set of addresses
120  * that may not be forwarded, or whether datagrams to that destination
121  * may be forwarded.
122  */
123 int
124 in_canforward(struct in_addr in)
125 {
126         u_long i = ntohl(in.s_addr);
127         u_long net;
128
129         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
130                 return (0);
131         if (IN_CLASSA(i)) {
132                 net = i & IN_CLASSA_NET;
133                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
134                         return (0);
135         }
136         return (1);
137 }
138
139 /*
140  * Trim a mask in a sockaddr
141  */
142 static void
143 in_socktrim(struct sockaddr_in *ap)
144 {
145     char *cplim = (char *) &ap->sin_addr;
146     char *cp = (char *) (&ap->sin_addr + 1);
147
148     ap->sin_len = 0;
149     while (--cp >= cplim)
150         if (*cp) {
151             (ap)->sin_len = cp - (char *) (ap) + 1;
152             break;
153         }
154 }
155
156 static int
157 in_mask2len(struct in_addr *mask)
158 {
159         int x, y;
160         u_char *p;
161
162         p = (u_char *)mask;
163         for (x = 0; x < sizeof *mask; x++) {
164                 if (p[x] != 0xff)
165                         break;
166         }
167         y = 0;
168         if (x < sizeof *mask) {
169                 for (y = 0; y < 8; y++) {
170                         if ((p[x] & (0x80 >> y)) == 0)
171                                 break;
172                 }
173         }
174         return x * 8 + y;
175 }
176
177 static void
178 in_len2mask(struct in_addr *mask, int len)
179 {
180         int i;
181         u_char *p;
182
183         p = (u_char *)mask;
184         bzero(mask, sizeof *mask);
185         for (i = 0; i < len / 8; i++)
186                 p[i] = 0xff;
187         if (len % 8)
188                 p[i] = (0xff00 >> (len % 8)) & 0xff;
189 }
190
191 static int in_interfaces;       /* number of external internet interfaces */
192
193 struct in_control_arg {
194         u_long          cmd;
195         caddr_t         data;
196         struct ifnet    *ifp;
197         struct thread   *td;
198 };
199
200 static void
201 in_control_dispatch(struct netmsg *nmsg)
202 {
203         struct lwkt_msg *msg = &nmsg->nm_lmsg;
204         const struct in_control_arg *arg = msg->u.ms_resultp;
205         int error;
206
207         error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
208         lwkt_replymsg(msg, error);
209 }
210
211 /*
212  * Generic internet control operations (ioctl's).
213  * Ifp is 0 if not an interface-specific ioctl.
214  *
215  * NOTE! td might be NULL.
216  */
217 /* ARGSUSED */
218 int
219 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
220            struct thread *td)
221 {
222         struct netmsg nmsg;
223         struct in_control_arg arg;
224         struct lwkt_msg *msg;
225         int error;
226
227         switch (cmd) {
228         case SIOCALIFADDR:
229         case SIOCDLIFADDR:
230                 if (td && (error = suser(td)) != 0)
231                         return error;
232                 /* FALLTHROUGH */
233         case SIOCGLIFADDR:
234                 if (!ifp)
235                         return EINVAL;
236                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
237         }
238
239         KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
240                 ("recursive SIOC%cLIFADDR!\n",
241                  cmd == SIOCDLIFADDR ? 'D' : 'A'));
242
243         /*
244          * IFADDR alterations are serialized by netisr0
245          */
246         switch (cmd) {
247         case SIOCSIFDSTADDR:
248         case SIOCSIFBRDADDR:
249         case SIOCSIFADDR:
250         case SIOCSIFNETMASK:
251         case SIOCAIFADDR:
252         case SIOCDIFADDR:
253                 bzero(&arg, sizeof(arg));
254                 arg.cmd = cmd;
255                 arg.data = data;
256                 arg.ifp = ifp;
257                 arg.td = td;
258
259                 netmsg_init(&nmsg, &curthread->td_msgport, 0,
260                             in_control_dispatch);
261                 msg = &nmsg.nm_lmsg;
262                 msg->u.ms_resultp = &arg;
263
264                 lwkt_domsg(cpu_portfn(0), msg, 0);
265                 return msg->ms_error;
266         default:
267                 return in_control_internal(cmd, data, ifp, td);
268         }
269 }
270
271 static void
272 in_ialink_dispatch(struct netmsg *nmsg)
273 {
274         struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
275         struct in_ifaddr *ia = lmsg->u.ms_resultp;
276         struct ifaddr_container *ifac;
277         struct in_ifaddr_container *iac;
278         int cpu = mycpuid;
279
280         crit_enter();
281
282         ifac = &ia->ia_ifa.ifa_containers[cpu];
283         ASSERT_IFAC_VALID(ifac);
284         KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD) == 0,
285                 ("ia is on in_ifaddrheads\n"));
286
287         ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHEAD;
288         iac = &ifac->ifa_proto_u.u_in_ifac;
289         TAILQ_INSERT_TAIL(&in_ifaddrheads[cpu], iac, ia_link);
290
291         crit_exit();
292
293         ifa_forwardmsg(lmsg, cpu + 1);
294 }
295
296 static void
297 in_iaunlink_dispatch(struct netmsg *nmsg)
298 {
299         struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
300         struct in_ifaddr *ia = lmsg->u.ms_resultp;
301         struct ifaddr_container *ifac;
302         struct in_ifaddr_container *iac;
303         int cpu = mycpuid;
304
305         crit_enter();
306
307         ifac = &ia->ia_ifa.ifa_containers[cpu];
308         ASSERT_IFAC_VALID(ifac);
309         KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
310                 ("ia is not on in_ifaddrheads\n"));
311
312         iac = &ifac->ifa_proto_u.u_in_ifac;
313         TAILQ_REMOVE(&in_ifaddrheads[cpu], iac, ia_link);
314         ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHEAD;
315
316         crit_exit();
317
318         ifa_forwardmsg(lmsg, cpu + 1);
319 }
320
321 static void
322 in_iahashins_dispatch(struct netmsg *nmsg)
323 {
324         struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
325         struct in_ifaddr *ia = lmsg->u.ms_resultp;
326         struct ifaddr_container *ifac;
327         struct in_ifaddr_container *iac;
328         int cpu = mycpuid;
329
330         crit_enter();
331
332         ifac = &ia->ia_ifa.ifa_containers[cpu];
333         ASSERT_IFAC_VALID(ifac);
334         KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
335                 ("ia is on in_ifaddrhashtbls\n"));
336
337         ifac->ifa_listmask |= IFA_LIST_IN_IFADDRHASH;
338         iac = &ifac->ifa_proto_u.u_in_ifac;
339         LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
340                          iac, ia_hash);
341
342         crit_exit();
343
344         ifa_forwardmsg(lmsg, cpu + 1);
345 }
346
347 static void
348 in_iahashrem_dispatch(struct netmsg *nmsg)
349 {
350         struct lwkt_msg *lmsg = &nmsg->nm_lmsg;
351         struct in_ifaddr *ia = lmsg->u.ms_resultp;
352         struct ifaddr_container *ifac;
353         struct in_ifaddr_container *iac;
354         int cpu = mycpuid;
355
356         crit_enter();
357
358         ifac = &ia->ia_ifa.ifa_containers[cpu];
359         ASSERT_IFAC_VALID(ifac);
360         KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH,
361                 ("ia is not on in_ifaddrhashtbls\n"));
362
363         iac = &ifac->ifa_proto_u.u_in_ifac;
364         LIST_REMOVE(iac, ia_hash);
365         ifac->ifa_listmask &= ~IFA_LIST_IN_IFADDRHASH;
366
367         crit_exit();
368
369         ifa_forwardmsg(lmsg, cpu + 1);
370 }
371
372 static void
373 in_ialink(struct in_ifaddr *ia)
374 {
375         struct netmsg nmsg;
376         struct lwkt_msg *lmsg;
377
378         netmsg_init(&nmsg, &curthread->td_msgport, 0, in_ialink_dispatch);
379         lmsg = &nmsg.nm_lmsg;
380         lmsg->u.ms_resultp = ia;
381
382         ifa_domsg(lmsg, 0);
383 }
384
385 void
386 in_iaunlink(struct in_ifaddr *ia)
387 {
388         struct netmsg nmsg;
389         struct lwkt_msg *lmsg;
390
391         netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iaunlink_dispatch);
392         lmsg = &nmsg.nm_lmsg;
393         lmsg->u.ms_resultp = ia;
394
395         ifa_domsg(lmsg, 0);
396 }
397
398 void
399 in_iahash_insert(struct in_ifaddr *ia)
400 {
401         struct netmsg nmsg;
402         struct lwkt_msg *lmsg;
403
404         netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashins_dispatch);
405         lmsg = &nmsg.nm_lmsg;
406         lmsg->u.ms_resultp = ia;
407
408         ifa_domsg(lmsg, 0);
409 }
410
411 void
412 in_iahash_remove(struct in_ifaddr *ia)
413 {
414         struct netmsg nmsg;
415         struct lwkt_msg *lmsg;
416
417         netmsg_init(&nmsg, &curthread->td_msgport, 0, in_iahashrem_dispatch);
418         lmsg = &nmsg.nm_lmsg;
419         lmsg->u.ms_resultp = ia;
420
421         ifa_domsg(lmsg, 0);
422 }
423
424 static __inline struct in_ifaddr *
425 in_ianext(struct in_ifaddr *oia)
426 {
427         struct ifaddr_container *ifac;
428         struct in_ifaddr_container *iac;
429
430         ifac = &oia->ia_ifa.ifa_containers[mycpuid];
431         ASSERT_IFAC_VALID(ifac);
432         KASSERT(ifac->ifa_listmask & IFA_LIST_IN_IFADDRHEAD,
433                 ("ia is not on in_ifaddrheads\n"));
434
435         iac = &ifac->ifa_proto_u.u_in_ifac;
436         iac = TAILQ_NEXT(iac, ia_link);
437         if (iac != NULL)
438                 return iac->ia;
439         else
440                 return NULL;
441 }
442
443 static int
444 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
445                     struct thread *td)
446 {
447         struct ifreq *ifr = (struct ifreq *)data;
448         struct in_ifaddr *ia = NULL;
449         struct in_addr dst;
450         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
451         struct ifaddr_container *ifac;
452         struct in_ifaddr_container *iac;
453         struct sockaddr_in oldaddr;
454         int hostIsNew, iaIsNew, maskIsNew;
455         int error = 0;
456
457         iaIsNew = 0;
458
459         /*
460          * Find address for this interface, if it exists.
461          *
462          * If an alias address was specified, find that one instead of
463          * the first one on the interface, if possible
464          */
465         if (ifp) {
466                 struct in_ifaddr *iap;
467
468                 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
469                 LIST_FOREACH(iac, INADDR_HASH(dst.s_addr), ia_hash) {
470                         iap = iac->ia;
471                         if (iap->ia_ifp == ifp &&
472                             iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
473                                 ia = iap;
474                                 break;
475                         }
476                 }
477                 if (ia == NULL) {
478                         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
479                                       ifa_link) {
480                                 iap = ifatoia(ifac->ifa);
481                                 if (iap->ia_addr.sin_family == AF_INET) {
482                                         ia = iap;
483                                         break;
484                                 }
485                         }
486                 }
487         }
488
489         switch (cmd) {
490         case SIOCAIFADDR:
491         case SIOCDIFADDR:
492                 if (ifp == NULL)
493                         return (EADDRNOTAVAIL);
494                 if (ifra->ifra_addr.sin_family == AF_INET) {
495                         while (ia != NULL) {
496                                 if (ia->ia_ifp == ifp  &&
497                                     ia->ia_addr.sin_addr.s_addr ==
498                                     ifra->ifra_addr.sin_addr.s_addr)
499                                         break;
500                                 ia = in_ianext(ia);
501                         }
502                         if ((ifp->if_flags & IFF_POINTOPOINT) &&
503                             cmd == SIOCAIFADDR &&
504                             ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
505                                 return EDESTADDRREQ;
506                         }
507                 }
508                 if (cmd == SIOCDIFADDR && ia == NULL)
509                         return (EADDRNOTAVAIL);
510                 /* FALLTHROUGH */
511         case SIOCSIFADDR:
512         case SIOCSIFNETMASK:
513         case SIOCSIFDSTADDR:
514                 if (td && (error = suser(td)) != 0)
515                         return error;
516
517                 if (ifp == NULL)
518                         return (EADDRNOTAVAIL);
519
520                 if (cmd == SIOCSIFDSTADDR &&
521                     (ifp->if_flags & IFF_POINTOPOINT) == 0)
522                         return (EINVAL);
523
524                 if (ia == NULL) {
525                         struct ifaddr *ifa;
526                         int i;
527
528                         ia = ifa_create(sizeof(*ia), M_WAITOK);
529                         ifa = &ia->ia_ifa;
530
531                         /*
532                          * Setup per-CPU information
533                          */
534                         for (i = 0; i < ncpus; ++i) {
535                                 ifac = &ifa->ifa_containers[i];
536                                 iac = &ifac->ifa_proto_u.u_in_ifac;
537                                 iac->ia = ia;
538                                 iac->ia_ifac = ifac;
539                         }
540
541                         /*
542                          * Protect from NETISR_IP traversing address list
543                          * while we're modifying it.
544                          */
545                         crit_enter();
546
547                         in_ialink(ia);
548                         ifa_iflink(ifa, ifp, 1);
549
550                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
551                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
552                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
553                         ia->ia_sockmask.sin_len = 8;
554                         ia->ia_sockmask.sin_family = AF_INET;
555                         if (ifp->if_flags & IFF_BROADCAST) {
556                                 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
557                                 ia->ia_broadaddr.sin_family = AF_INET;
558                         }
559                         ia->ia_ifp = ifp;
560                         if (!(ifp->if_flags & IFF_LOOPBACK))
561                                 in_interfaces++;
562                         iaIsNew = 1;
563
564                         crit_exit();
565                 }
566                 break;
567
568         case SIOCSIFBRDADDR:
569                 if (td && (error = suser(td)) != 0)
570                         return error;
571                 /* FALLTHROUGH */
572
573         case SIOCGIFADDR:
574         case SIOCGIFNETMASK:
575         case SIOCGIFDSTADDR:
576         case SIOCGIFBRDADDR:
577                 if (ia == NULL)
578                         return (EADDRNOTAVAIL);
579                 break;
580         }
581
582         switch (cmd) {
583         case SIOCGIFADDR:
584                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
585                 return (0);
586
587         case SIOCGIFBRDADDR:
588                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
589                         return (EINVAL);
590                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
591                 return (0);
592
593         case SIOCGIFDSTADDR:
594                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
595                         return (EINVAL);
596                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
597                 return (0);
598
599         case SIOCGIFNETMASK:
600                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
601                 return (0);
602
603         case SIOCSIFDSTADDR:
604                 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
605
606                 oldaddr = ia->ia_dstaddr;
607                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
608                 if (ifp->if_ioctl != NULL) {
609                         lwkt_serialize_enter(ifp->if_serializer);
610                         error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
611                                               td->td_proc->p_ucred);
612                         lwkt_serialize_exit(ifp->if_serializer);
613                         if (error) {
614                                 ia->ia_dstaddr = oldaddr;
615                                 return (error);
616                         }
617                 }
618                 if (ia->ia_flags & IFA_ROUTE) {
619                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
620                         rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
621                         ia->ia_ifa.ifa_dstaddr =
622                                         (struct sockaddr *)&ia->ia_dstaddr;
623                         rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
624                 }
625                 return (0);
626
627         case SIOCSIFBRDADDR:
628                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
629                         return (EINVAL);
630                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
631                 return (0);
632
633         case SIOCSIFADDR:
634                 error = in_ifinit(ifp, ia,
635                     (const struct sockaddr_in *)&ifr->ifr_addr, 1);
636                 if (error != 0 && iaIsNew)
637                         break;
638                 if (error == 0) {
639                         EVENTHANDLER_INVOKE(ifaddr_event, ifp,
640                         iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
641                         &ia->ia_ifa);
642                 }
643                 return (0);
644
645         case SIOCSIFNETMASK:
646                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
647                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
648                 return (0);
649
650         case SIOCAIFADDR:
651                 maskIsNew = 0;
652                 hostIsNew = 1;
653                 error = 0;
654                 if (ia->ia_addr.sin_family == AF_INET) {
655                         if (ifra->ifra_addr.sin_len == 0) {
656                                 ifra->ifra_addr = ia->ia_addr;
657                                 hostIsNew = 0;
658                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
659                                    ia->ia_addr.sin_addr.s_addr) {
660                                 hostIsNew = 0;
661                         }
662                 }
663                 if (ifra->ifra_mask.sin_len) {
664                         in_ifscrub(ifp, ia);
665                         ia->ia_sockmask = ifra->ifra_mask;
666                         ia->ia_sockmask.sin_family = AF_INET;
667                         ia->ia_subnetmask =
668                             ntohl(ia->ia_sockmask.sin_addr.s_addr);
669                         maskIsNew = 1;
670                 }
671                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
672                     ifra->ifra_dstaddr.sin_family == AF_INET) {
673                         in_ifscrub(ifp, ia);
674                         ia->ia_dstaddr = ifra->ifra_dstaddr;
675                         maskIsNew  = 1; /* We lie; but the effect's the same */
676                 }
677                 if (ifra->ifra_addr.sin_family == AF_INET &&
678                     (hostIsNew || maskIsNew))
679                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
680
681                 if (error != 0 && iaIsNew)
682                         break;
683
684                 if ((ifp->if_flags & IFF_BROADCAST) &&
685                     ifra->ifra_broadaddr.sin_family == AF_INET)
686                         ia->ia_broadaddr = ifra->ifra_broadaddr;
687                 if (error == 0) {
688                         EVENTHANDLER_INVOKE(ifaddr_event, ifp,
689                         iaIsNew ? IFADDR_EVENT_ADD : IFADDR_EVENT_CHANGE,
690                         &ia->ia_ifa);
691                 }
692                 return (error);
693
694         case SIOCDIFADDR:
695                 /*
696                  * in_ifscrub kills the interface route.
697                  */
698                 in_ifscrub(ifp, ia);
699                 /*
700                  * in_ifadown gets rid of all the rest of
701                  * the routes.  This is not quite the right
702                  * thing to do, but at least if we are running
703                  * a routing process they will come back.
704                  */
705                 in_ifadown(&ia->ia_ifa, 1);
706                 EVENTHANDLER_INVOKE(ifaddr_event, ifp, IFADDR_EVENT_DELETE,
707                                     &ia->ia_ifa);
708                 error = 0;
709                 break;
710
711         default:
712                 if (ifp == NULL || ifp->if_ioctl == NULL)
713                         return (EOPNOTSUPP);
714                 lwkt_serialize_enter(ifp->if_serializer);
715                 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
716                 lwkt_serialize_exit(ifp->if_serializer);
717                 return (error);
718         }
719
720         KKASSERT(cmd == SIOCDIFADDR ||
721                  ((cmd == SIOCAIFADDR || cmd == SIOCSIFADDR) && iaIsNew));
722
723         ifa_ifunlink(&ia->ia_ifa, ifp);
724         in_iaunlink(ia);
725
726         if (cmd == SIOCDIFADDR) {
727                 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
728                 if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
729                         in_iahash_remove(ia);
730         }
731 #ifdef INVARIANTS
732         else {
733                 /*
734                  * If cmd is SIOCSIFADDR or SIOCAIFADDR, in_ifinit() has
735                  * already taken care of the deletion from hash table
736                  */
737                 ifac = &ia->ia_ifa.ifa_containers[mycpuid];
738                 KASSERT((ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) == 0,
739                         ("SIOC%cIFADDR failed on new ia, "
740                          "but the new ia is still in hash table\n",
741                          cmd == SIOCSIFADDR ? 'S' : 'A'));
742         }
743 #endif
744
745         ifa_destroy(&ia->ia_ifa);
746
747         return (error);
748 }
749
750 /*
751  * SIOC[GAD]LIFADDR.
752  *      SIOCGLIFADDR: get first address. (?!?)
753  *      SIOCGLIFADDR with IFLR_PREFIX:
754  *              get first address that matches the specified prefix.
755  *      SIOCALIFADDR: add the specified address.
756  *      SIOCALIFADDR with IFLR_PREFIX:
757  *              EINVAL since we can't deduce hostid part of the address.
758  *      SIOCDLIFADDR: delete the specified address.
759  *      SIOCDLIFADDR with IFLR_PREFIX:
760  *              delete the first address that matches the specified prefix.
761  * return values:
762  *      EINVAL on invalid parameters
763  *      EADDRNOTAVAIL on prefix match failed/specified address not found
764  *      other values may be returned from in_ioctl()
765  *
766  * NOTE! td might be NULL.
767  */
768 static int
769 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
770                  struct thread *td)
771 {
772         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
773
774         /* sanity checks */
775         if (!data || !ifp) {
776                 panic("invalid argument to in_lifaddr_ioctl");
777                 /*NOTRECHED*/
778         }
779
780         switch (cmd) {
781         case SIOCGLIFADDR:
782                 /* address must be specified on GET with IFLR_PREFIX */
783                 if ((iflr->flags & IFLR_PREFIX) == 0)
784                         break;
785                 /*FALLTHROUGH*/
786         case SIOCALIFADDR:
787         case SIOCDLIFADDR:
788                 /* address must be specified on ADD and DELETE */
789                 if (iflr->addr.ss_family != AF_INET)
790                         return EINVAL;
791                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
792                         return EINVAL;
793                 /* XXX need improvement */
794                 if (iflr->dstaddr.ss_family
795                  && iflr->dstaddr.ss_family != AF_INET)
796                         return EINVAL;
797                 if (iflr->dstaddr.ss_family
798                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
799                         return EINVAL;
800                 break;
801         default: /*shouldn't happen*/
802                 return EOPNOTSUPP;
803         }
804         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
805                 return EINVAL;
806
807         switch (cmd) {
808         case SIOCALIFADDR:
809             {
810                 struct in_aliasreq ifra;
811
812                 if (iflr->flags & IFLR_PREFIX)
813                         return EINVAL;
814
815                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
816                 bzero(&ifra, sizeof ifra);
817                 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
818
819                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
820
821                 if (iflr->dstaddr.ss_family) {  /*XXX*/
822                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
823                                 iflr->dstaddr.ss_len);
824                 }
825
826                 ifra.ifra_mask.sin_family = AF_INET;
827                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
828                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
829
830                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
831             }
832         case SIOCGLIFADDR:
833         case SIOCDLIFADDR:
834             {
835                 struct ifaddr_container *ifac;
836                 struct in_ifaddr *ia;
837                 struct in_addr mask, candidate, match;
838                 struct sockaddr_in *sin;
839                 int cmp;
840
841                 bzero(&mask, sizeof mask);
842                 if (iflr->flags & IFLR_PREFIX) {
843                         /* lookup a prefix rather than address. */
844                         in_len2mask(&mask, iflr->prefixlen);
845
846                         sin = (struct sockaddr_in *)&iflr->addr;
847                         match.s_addr = sin->sin_addr.s_addr;
848                         match.s_addr &= mask.s_addr;
849
850                         /* if you set extra bits, that's wrong */
851                         if (match.s_addr != sin->sin_addr.s_addr)
852                                 return EINVAL;
853
854                         cmp = 1;
855                 } else {
856                         if (cmd == SIOCGLIFADDR) {
857                                 /* on getting an address, take the 1st match */
858                                 match.s_addr = 0; /* gcc4 warning */
859                                 cmp = 0;        /*XXX*/
860                         } else {
861                                 /* on deleting an address, do exact match */
862                                 in_len2mask(&mask, 32);
863                                 sin = (struct sockaddr_in *)&iflr->addr;
864                                 match.s_addr = sin->sin_addr.s_addr;
865
866                                 cmp = 1;
867                         }
868                 }
869
870                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
871                         struct ifaddr *ifa = ifac->ifa;
872
873                         if (ifa->ifa_addr->sa_family != AF_INET6)
874                                 continue;
875                         if (!cmp)
876                                 break;
877                         candidate.s_addr =
878                         ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
879                         candidate.s_addr &= mask.s_addr;
880                         if (candidate.s_addr == match.s_addr)
881                                 break;
882                 }
883                 if (ifac == NULL)
884                         return EADDRNOTAVAIL;
885                 ia = (struct in_ifaddr *)(ifac->ifa);
886
887                 if (cmd == SIOCGLIFADDR) {
888                         /* fill in the if_laddrreq structure */
889                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
890
891                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
892                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
893                                         ia->ia_dstaddr.sin_len);
894                         } else
895                                 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
896
897                         iflr->prefixlen =
898                                 in_mask2len(&ia->ia_sockmask.sin_addr);
899
900                         iflr->flags = 0;        /*XXX*/
901
902                         return 0;
903                 } else {
904                         struct in_aliasreq ifra;
905
906                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
907                         bzero(&ifra, sizeof ifra);
908                         bcopy(iflr->iflr_name, ifra.ifra_name,
909                                 sizeof ifra.ifra_name);
910
911                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
912                                 ia->ia_addr.sin_len);
913                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
914                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
915                                         ia->ia_dstaddr.sin_len);
916                         }
917                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
918                                 ia->ia_sockmask.sin_len);
919
920                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
921                                           ifp, td);
922                 }
923             }
924         }
925
926         return EOPNOTSUPP;      /*just for safety*/
927 }
928
929 /*
930  * Delete any existing route for an interface.
931  */
932 void
933 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
934 {
935
936         if ((ia->ia_flags & IFA_ROUTE) == 0)
937                 return;
938         if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
939                 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
940         else
941                 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
942         ia->ia_flags &= ~IFA_ROUTE;
943 }
944
945 /*
946  * Initialize an interface's internet address
947  * and routing table entry.
948  */
949 static int
950 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
951           const struct sockaddr_in *sin, int scrub)
952 {
953         u_long i = ntohl(sin->sin_addr.s_addr);
954         struct sockaddr_in oldaddr;
955         struct ifaddr_container *ifac;
956         int flags = RTF_UP, error = 0;
957         int was_hash = 0;
958
959         ifac = &ia->ia_ifa.ifa_containers[mycpuid];
960         oldaddr = ia->ia_addr;
961
962         if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH) {
963                 was_hash = 1;
964                 in_iahash_remove(ia);
965         }
966
967         ia->ia_addr = *sin;
968         if (ia->ia_addr.sin_family == AF_INET)
969                 in_iahash_insert(ia);
970
971         /*
972          * Give the interface a chance to initialize
973          * if this is its first address,
974          * and to validate the address if necessary.
975          */
976         if (ifp->if_ioctl != NULL) {
977                 lwkt_serialize_enter(ifp->if_serializer);
978                 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
979                 lwkt_serialize_exit(ifp->if_serializer);
980                 if (error)
981                         goto fail;
982         }
983
984         /*
985          * Delete old route, if requested.
986          */
987         if (scrub) {
988                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
989                 in_ifscrub(ifp, ia);
990                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
991         }
992
993         /*
994          * Calculate netmask/subnetmask.
995          */
996         if (IN_CLASSA(i))
997                 ia->ia_netmask = IN_CLASSA_NET;
998         else if (IN_CLASSB(i))
999                 ia->ia_netmask = IN_CLASSB_NET;
1000         else
1001                 ia->ia_netmask = IN_CLASSC_NET;
1002         /*
1003          * The subnet mask usually includes at least the standard network part,
1004          * but may may be smaller in the case of supernetting.
1005          * If it is set, we believe it.
1006          */
1007         if (ia->ia_subnetmask == 0) {
1008                 ia->ia_subnetmask = ia->ia_netmask;
1009                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
1010         } else {
1011                 ia->ia_netmask &= ia->ia_subnetmask;
1012         }
1013         ia->ia_net = i & ia->ia_netmask;
1014         ia->ia_subnet = i & ia->ia_subnetmask;
1015         in_socktrim(&ia->ia_sockmask);
1016
1017         /*
1018          * Add route for the network.
1019          */
1020         ia->ia_ifa.ifa_metric = ifp->if_metric;
1021         if (ifp->if_flags & IFF_BROADCAST) {
1022                 ia->ia_broadaddr.sin_addr.s_addr =
1023                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
1024                 ia->ia_netbroadcast.s_addr =
1025                         htonl(ia->ia_net | ~ ia->ia_netmask);
1026         } else if (ifp->if_flags & IFF_LOOPBACK) {
1027                 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
1028                 flags |= RTF_HOST;
1029         } else if (ifp->if_flags & IFF_POINTOPOINT) {
1030                 if (ia->ia_dstaddr.sin_family != AF_INET)
1031                         return (0);
1032                 flags |= RTF_HOST;
1033         }
1034
1035         /*-
1036          * Don't add host routes for interface addresses of
1037          * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
1038          * possible to assign several such address pairs with consistent
1039          * results (no host route) and is required by BOOTP.
1040          *
1041          * XXX: This is ugly !  There should be a way for the caller to
1042          *      say that they don't want a host route.
1043          */
1044         if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
1045             ia->ia_netmask != IN_CLASSA_NET ||
1046             ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
1047                 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, flags)) != 0) {
1048                         if (error != EEXIST ||
1049                             !(ifac->ifa_prflags & IA_PRF_RTEXISTOK))
1050                                 goto fail;
1051                 } else {
1052                         ia->ia_flags |= IFA_ROUTE;
1053                 }
1054         }
1055
1056         /*
1057          * If the interface supports multicast, join the "all hosts"
1058          * multicast group on that interface.
1059          */
1060         if (ifp->if_flags & IFF_MULTICAST) {
1061                 struct in_addr addr;
1062
1063                 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
1064                 in_addmulti(&addr, ifp);
1065         }
1066         return (0);
1067 fail:
1068         if (ifac->ifa_listmask & IFA_LIST_IN_IFADDRHASH)
1069                 in_iahash_remove(ia);
1070
1071         ia->ia_addr = oldaddr;
1072         if (was_hash)
1073                 in_iahash_insert(ia);
1074         return (error);
1075 }
1076
1077
1078 /*
1079  * Return 1 if the address might be a local broadcast address.
1080  */
1081 int
1082 in_broadcast(struct in_addr in, struct ifnet *ifp)
1083 {
1084         struct ifaddr_container *ifac;
1085         u_long t;
1086
1087         if (in.s_addr == INADDR_BROADCAST ||
1088             in.s_addr == INADDR_ANY)
1089                 return 1;
1090         if ((ifp->if_flags & IFF_BROADCAST) == 0)
1091                 return 0;
1092         t = ntohl(in.s_addr);
1093         /*
1094          * Look through the list of addresses for a match
1095          * with a broadcast address.
1096          */
1097 #define ia ((struct in_ifaddr *)ifa)
1098         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
1099                 struct ifaddr *ifa = ifac->ifa;
1100
1101                 if (ifa->ifa_addr->sa_family == AF_INET &&
1102                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
1103                      in.s_addr == ia->ia_netbroadcast.s_addr ||
1104                      /*
1105                       * Check for old-style (host 0) broadcast.
1106                       */
1107                      t == ia->ia_subnet || t == ia->ia_net) &&
1108                      /*
1109                       * Check for an all one subnetmask. These
1110                       * only exist when an interface gets a secondary
1111                       * address.
1112                       */
1113                      ia->ia_subnetmask != (u_long)0xffffffff)
1114                             return 1;
1115         }
1116         return (0);
1117 #undef ia
1118 }
1119 /*
1120  * Add an address to the list of IP multicast addresses for a given interface.
1121  */
1122 struct in_multi *
1123 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
1124 {
1125         struct in_multi *inm;
1126         int error;
1127         struct sockaddr_in sin;
1128         struct ifmultiaddr *ifma;
1129
1130         /*
1131          * Call generic routine to add membership or increment
1132          * refcount.  It wants addresses in the form of a sockaddr,
1133          * so we build one here (being careful to zero the unused bytes).
1134          */
1135         bzero(&sin, sizeof sin);
1136         sin.sin_family = AF_INET;
1137         sin.sin_len = sizeof sin;
1138         sin.sin_addr = *ap;
1139         crit_enter();
1140         error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
1141         if (error) {
1142                 crit_exit();
1143                 return 0;
1144         }
1145
1146         /*
1147          * If ifma->ifma_protospec is null, then if_addmulti() created
1148          * a new record.  Otherwise, we are done.
1149          */
1150         if (ifma->ifma_protospec != 0) {
1151                 crit_exit();
1152                 return ifma->ifma_protospec;
1153         }
1154
1155         /* XXX - if_addmulti uses M_WAITOK.  Can this really be called
1156            at interrupt time?  If so, need to fix if_addmulti. XXX */
1157         inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
1158         inm->inm_addr = *ap;
1159         inm->inm_ifp = ifp;
1160         inm->inm_ifma = ifma;
1161         ifma->ifma_protospec = inm;
1162         LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
1163
1164         /*
1165          * Let IGMP know that we have joined a new IP multicast group.
1166          */
1167         igmp_joingroup(inm);
1168         crit_exit();
1169         return (inm);
1170 }
1171
1172 /*
1173  * Delete a multicast address record.
1174  */
1175 void
1176 in_delmulti(struct in_multi *inm)
1177 {
1178         struct ifmultiaddr *ifma;
1179         struct in_multi my_inm;
1180
1181         crit_enter();
1182         ifma = inm->inm_ifma;
1183         my_inm.inm_ifp = NULL ; /* don't send the leave msg */
1184         if (ifma->ifma_refcount == 1) {
1185                 /*
1186                  * No remaining claims to this record; let IGMP know that
1187                  * we are leaving the multicast group.
1188                  * But do it after the if_delmulti() which might reset
1189                  * the interface and nuke the packet.
1190                  */
1191                 my_inm = *inm ;
1192                 ifma->ifma_protospec = 0;
1193                 LIST_REMOVE(inm, inm_link);
1194                 kfree(inm, M_IPMADDR);
1195         }
1196         /* XXX - should be separate API for when we have an ifma? */
1197         if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
1198         if (my_inm.inm_ifp != NULL)
1199                 igmp_leavegroup(&my_inm);
1200         crit_exit();
1201 }
1202
1203 void
1204 in_ifdetach(struct ifnet *ifp)
1205 {
1206         in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
1207         in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
1208 }