Fix following possible bugs for SIOCSIFADDR, if in_ifinit() fails
[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.35 2008/05/26 13:29:33 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 *ia;
98
99         if (subnetsarelocal) {
100                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
101                         if ((i & ia->ia_netmask) == ia->ia_net)
102                                 return (1);
103         } else {
104                 TAILQ_FOREACH(ia, &in_ifaddrhead, ia_link)
105                         if ((i & ia->ia_subnetmask) == ia->ia_subnet)
106                                 return (1);
107         }
108         return (0);
109 }
110
111 /*
112  * Determine whether an IP address is in a reserved set of addresses
113  * that may not be forwarded, or whether datagrams to that destination
114  * may be forwarded.
115  */
116 int
117 in_canforward(struct in_addr in)
118 {
119         u_long i = ntohl(in.s_addr);
120         u_long net;
121
122         if (IN_EXPERIMENTAL(i) || IN_MULTICAST(i))
123                 return (0);
124         if (IN_CLASSA(i)) {
125                 net = i & IN_CLASSA_NET;
126                 if (net == 0 || net == (IN_LOOPBACKNET << IN_CLASSA_NSHIFT))
127                         return (0);
128         }
129         return (1);
130 }
131
132 /*
133  * Trim a mask in a sockaddr
134  */
135 static void
136 in_socktrim(struct sockaddr_in *ap)
137 {
138     char *cplim = (char *) &ap->sin_addr;
139     char *cp = (char *) (&ap->sin_addr + 1);
140
141     ap->sin_len = 0;
142     while (--cp >= cplim)
143         if (*cp) {
144             (ap)->sin_len = cp - (char *) (ap) + 1;
145             break;
146         }
147 }
148
149 static int
150 in_mask2len(struct in_addr *mask)
151 {
152         int x, y;
153         u_char *p;
154
155         p = (u_char *)mask;
156         for (x = 0; x < sizeof *mask; x++) {
157                 if (p[x] != 0xff)
158                         break;
159         }
160         y = 0;
161         if (x < sizeof *mask) {
162                 for (y = 0; y < 8; y++) {
163                         if ((p[x] & (0x80 >> y)) == 0)
164                                 break;
165                 }
166         }
167         return x * 8 + y;
168 }
169
170 static void
171 in_len2mask(struct in_addr *mask, int len)
172 {
173         int i;
174         u_char *p;
175
176         p = (u_char *)mask;
177         bzero(mask, sizeof *mask);
178         for (i = 0; i < len / 8; i++)
179                 p[i] = 0xff;
180         if (len % 8)
181                 p[i] = (0xff00 >> (len % 8)) & 0xff;
182 }
183
184 static int in_interfaces;       /* number of external internet interfaces */
185
186 struct in_control_arg {
187         u_long          cmd;
188         caddr_t         data;
189         struct ifnet    *ifp;
190         struct thread   *td;
191 };
192
193 static void
194 in_control_dispatch(struct netmsg *nmsg)
195 {
196         struct lwkt_msg *msg = &nmsg->nm_lmsg;
197         const struct in_control_arg *arg = msg->u.ms_resultp;
198         int error;
199
200         error = in_control_internal(arg->cmd, arg->data, arg->ifp, arg->td);
201         lwkt_replymsg(msg, error);
202 }
203
204 /*
205  * Generic internet control operations (ioctl's).
206  * Ifp is 0 if not an interface-specific ioctl.
207  *
208  * NOTE! td might be NULL.
209  */
210 /* ARGSUSED */
211 int
212 in_control(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
213            struct thread *td)
214 {
215         struct netmsg nmsg;
216         struct in_control_arg arg;
217         struct lwkt_msg *msg;
218         int error;
219
220         switch (cmd) {
221         case SIOCALIFADDR:
222         case SIOCDLIFADDR:
223                 if (td && (error = suser(td)) != 0)
224                         return error;
225                 /* FALLTHROUGH */
226         case SIOCGLIFADDR:
227                 if (!ifp)
228                         return EINVAL;
229                 return in_lifaddr_ioctl(so, cmd, data, ifp, td);
230         }
231
232         KASSERT(cmd != SIOCALIFADDR && cmd != SIOCDLIFADDR,
233                 ("recursive SIOC%cLIFADDR!\n",
234                  cmd == SIOCDLIFADDR ? 'D' : 'A'));
235
236         /*
237          * IFADDR alterations are serialized by netisr0
238          */
239         switch (cmd) {
240         case SIOCSIFDSTADDR:
241         case SIOCSIFBRDADDR:
242         case SIOCSIFADDR:
243         case SIOCSIFNETMASK:
244         case SIOCAIFADDR:
245         case SIOCDIFADDR:
246                 bzero(&arg, sizeof(arg));
247                 arg.cmd = cmd;
248                 arg.data = data;
249                 arg.ifp = ifp;
250                 arg.td = td;
251
252                 netmsg_init(&nmsg, &curthread->td_msgport, 0,
253                             in_control_dispatch);
254                 msg = &nmsg.nm_lmsg;
255                 msg->u.ms_resultp = &arg;
256
257                 lwkt_domsg(cpu_portfn(0), msg, 0);
258                 return msg->ms_error;
259         default:
260                 return in_control_internal(cmd, data, ifp, td);
261         }
262 }
263
264 static int
265 in_control_internal(u_long cmd, caddr_t data, struct ifnet *ifp,
266                     struct thread *td)
267 {
268         struct ifreq *ifr = (struct ifreq *)data;
269         struct in_ifaddr *ia = 0, *iap;
270         struct in_addr dst;
271         struct in_ifaddr *oia;
272         struct in_aliasreq *ifra = (struct in_aliasreq *)data;
273         struct sockaddr_in oldaddr;
274         int hostIsNew, iaIsNew, maskIsNew;
275         int error = 0;
276
277         iaIsNew = 0;
278
279         /*
280          * Find address for this interface, if it exists.
281          *
282          * If an alias address was specified, find that one instead of
283          * the first one on the interface, if possible
284          */
285         if (ifp) {
286                 dst = ((struct sockaddr_in *)&ifr->ifr_addr)->sin_addr;
287                 LIST_FOREACH(iap, INADDR_HASH(dst.s_addr), ia_hash)
288                         if (iap->ia_ifp == ifp &&
289                             iap->ia_addr.sin_addr.s_addr == dst.s_addr) {
290                                 ia = iap;
291                                 break;
292                         }
293                 if (ia == NULL) {
294                         struct ifaddr_container *ifac;
295
296                         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid],
297                                       ifa_link) {
298                                 iap = ifatoia(ifac->ifa);
299                                 if (iap->ia_addr.sin_family == AF_INET) {
300                                         ia = iap;
301                                         break;
302                                 }
303                         }
304                 }
305         }
306
307         switch (cmd) {
308         case SIOCAIFADDR:
309         case SIOCDIFADDR:
310                 if (ifp == NULL)
311                         return (EADDRNOTAVAIL);
312                 if (ifra->ifra_addr.sin_family == AF_INET) {
313                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
314                                 if (ia->ia_ifp == ifp  &&
315                                     ia->ia_addr.sin_addr.s_addr ==
316                                     ifra->ifra_addr.sin_addr.s_addr)
317                                         break;
318                         }
319                         if ((ifp->if_flags & IFF_POINTOPOINT) &&
320                             cmd == SIOCAIFADDR &&
321                             ifra->ifra_dstaddr.sin_addr.s_addr == INADDR_ANY) {
322                                 return EDESTADDRREQ;
323                         }
324                 }
325                 if (cmd == SIOCDIFADDR && ia == NULL)
326                         return (EADDRNOTAVAIL);
327                 /* FALLTHROUGH */
328         case SIOCSIFADDR:
329         case SIOCSIFNETMASK:
330         case SIOCSIFDSTADDR:
331                 if (td && (error = suser(td)) != 0)
332                         return error;
333
334                 if (ifp == NULL)
335                         return (EADDRNOTAVAIL);
336
337                 if (cmd == SIOCSIFDSTADDR &&
338                     (ifp->if_flags & IFF_POINTOPOINT) == 0)
339                         return (EINVAL);
340
341                 if (ia == NULL) {
342                         struct ifaddr *ifa;
343
344                         ia = ifa_create(sizeof(*ia), M_WAITOK);
345
346                         /*
347                          * Protect from NETISR_IP traversing address list
348                          * while we're modifying it.
349                          */
350                         crit_enter();
351
352                         TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
353                         ifa = &ia->ia_ifa;
354                         ifa_iflink(ifa, ifp, 1);
355
356                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
357                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
358                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
359                         ia->ia_sockmask.sin_len = 8;
360                         ia->ia_sockmask.sin_family = AF_INET;
361                         if (ifp->if_flags & IFF_BROADCAST) {
362                                 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
363                                 ia->ia_broadaddr.sin_family = AF_INET;
364                         }
365                         ia->ia_ifp = ifp;
366                         if (!(ifp->if_flags & IFF_LOOPBACK))
367                                 in_interfaces++;
368                         iaIsNew = 1;
369
370                         crit_exit();
371                 }
372                 break;
373
374         case SIOCSIFBRDADDR:
375                 if (td && (error = suser(td)) != 0)
376                         return error;
377                 /* FALLTHROUGH */
378
379         case SIOCGIFADDR:
380         case SIOCGIFNETMASK:
381         case SIOCGIFDSTADDR:
382         case SIOCGIFBRDADDR:
383                 if (ia == NULL)
384                         return (EADDRNOTAVAIL);
385                 break;
386         }
387
388         switch (cmd) {
389         case SIOCGIFADDR:
390                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
391                 return (0);
392
393         case SIOCGIFBRDADDR:
394                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
395                         return (EINVAL);
396                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
397                 return (0);
398
399         case SIOCGIFDSTADDR:
400                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
401                         return (EINVAL);
402                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
403                 return (0);
404
405         case SIOCGIFNETMASK:
406                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
407                 return (0);
408
409         case SIOCSIFDSTADDR:
410                 KKASSERT(ifp->if_flags & IFF_POINTOPOINT);
411
412                 oldaddr = ia->ia_dstaddr;
413                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
414                 if (ifp->if_ioctl != NULL) {
415                         lwkt_serialize_enter(ifp->if_serializer);
416                         error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
417                                               td->td_proc->p_ucred);
418                         lwkt_serialize_exit(ifp->if_serializer);
419                         if (error) {
420                                 ia->ia_dstaddr = oldaddr;
421                                 return (error);
422                         }
423                 }
424                 if (ia->ia_flags & IFA_ROUTE) {
425                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
426                         rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
427                         ia->ia_ifa.ifa_dstaddr =
428                                         (struct sockaddr *)&ia->ia_dstaddr;
429                         rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
430                 }
431                 return (0);
432
433         case SIOCSIFBRDADDR:
434                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
435                         return (EINVAL);
436                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
437                 return (0);
438
439         case SIOCSIFADDR:
440                 error = in_ifinit(ifp, ia,
441                     (const struct sockaddr_in *)&ifr->ifr_addr, 1);
442                 if (error != 0 && iaIsNew)
443                         break;
444                 if (error == 0)
445                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
446                 return (0);
447
448         case SIOCSIFNETMASK:
449                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
450                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
451                 return (0);
452
453         case SIOCAIFADDR:
454                 maskIsNew = 0;
455                 hostIsNew = 1;
456                 error = 0;
457                 if (ia->ia_addr.sin_family == AF_INET) {
458                         if (ifra->ifra_addr.sin_len == 0) {
459                                 ifra->ifra_addr = ia->ia_addr;
460                                 hostIsNew = 0;
461                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
462                                    ia->ia_addr.sin_addr.s_addr) {
463                                 hostIsNew = 0;
464                         }
465                 }
466                 if (ifra->ifra_mask.sin_len) {
467                         in_ifscrub(ifp, ia);
468                         ia->ia_sockmask = ifra->ifra_mask;
469                         ia->ia_sockmask.sin_family = AF_INET;
470                         ia->ia_subnetmask =
471                             ntohl(ia->ia_sockmask.sin_addr.s_addr);
472                         maskIsNew = 1;
473                 }
474                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
475                     ifra->ifra_dstaddr.sin_family == AF_INET) {
476                         in_ifscrub(ifp, ia);
477                         ia->ia_dstaddr = ifra->ifra_dstaddr;
478                         maskIsNew  = 1; /* We lie; but the effect's the same */
479                 }
480                 if (ifra->ifra_addr.sin_family == AF_INET &&
481                     (hostIsNew || maskIsNew))
482                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
483
484                 if (error != 0 && iaIsNew)
485                         break;
486
487                 if ((ifp->if_flags & IFF_BROADCAST) &&
488                     ifra->ifra_broadaddr.sin_family == AF_INET)
489                         ia->ia_broadaddr = ifra->ifra_broadaddr;
490                 if (error == 0)
491                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
492                 return (error);
493
494         case SIOCDIFADDR:
495                 /*
496                  * in_ifscrub kills the interface route.
497                  */
498                 in_ifscrub(ifp, ia);
499                 /*
500                  * in_ifadown gets rid of all the rest of
501                  * the routes.  This is not quite the right
502                  * thing to do, but at least if we are running
503                  * a routing process they will come back.
504                  */
505                 in_ifadown(&ia->ia_ifa, 1);
506                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
507                 error = 0;
508                 break;
509
510         default:
511                 if (ifp == NULL || ifp->if_ioctl == NULL)
512                         return (EOPNOTSUPP);
513                 lwkt_serialize_enter(ifp->if_serializer);
514                 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
515                 lwkt_serialize_exit(ifp->if_serializer);
516                 return (error);
517         }
518
519         ifa_ifunlink(&ia->ia_ifa, ifp);
520
521         /*
522          * Protect from NETISR_IP traversing address list while we're modifying
523          * it.
524          */
525         crit_enter();   /* XXX MP */
526         TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
527         if (cmd == SIOCDIFADDR && ia->ia_addr.sin_family == AF_INET) {
528                 /* XXX Assume that 'ia' is in hash table */
529                 LIST_REMOVE(ia, ia_hash);
530         }
531         crit_exit();    /* XXX MP */
532
533         ifa_destroy(&ia->ia_ifa);
534
535         return (error);
536 }
537
538 /*
539  * SIOC[GAD]LIFADDR.
540  *      SIOCGLIFADDR: get first address. (?!?)
541  *      SIOCGLIFADDR with IFLR_PREFIX:
542  *              get first address that matches the specified prefix.
543  *      SIOCALIFADDR: add the specified address.
544  *      SIOCALIFADDR with IFLR_PREFIX:
545  *              EINVAL since we can't deduce hostid part of the address.
546  *      SIOCDLIFADDR: delete the specified address.
547  *      SIOCDLIFADDR with IFLR_PREFIX:
548  *              delete the first address that matches the specified prefix.
549  * return values:
550  *      EINVAL on invalid parameters
551  *      EADDRNOTAVAIL on prefix match failed/specified address not found
552  *      other values may be returned from in_ioctl()
553  *
554  * NOTE! td might be NULL.
555  */
556 static int
557 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
558                  struct thread *td)
559 {
560         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
561
562         /* sanity checks */
563         if (!data || !ifp) {
564                 panic("invalid argument to in_lifaddr_ioctl");
565                 /*NOTRECHED*/
566         }
567
568         switch (cmd) {
569         case SIOCGLIFADDR:
570                 /* address must be specified on GET with IFLR_PREFIX */
571                 if ((iflr->flags & IFLR_PREFIX) == 0)
572                         break;
573                 /*FALLTHROUGH*/
574         case SIOCALIFADDR:
575         case SIOCDLIFADDR:
576                 /* address must be specified on ADD and DELETE */
577                 if (iflr->addr.ss_family != AF_INET)
578                         return EINVAL;
579                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
580                         return EINVAL;
581                 /* XXX need improvement */
582                 if (iflr->dstaddr.ss_family
583                  && iflr->dstaddr.ss_family != AF_INET)
584                         return EINVAL;
585                 if (iflr->dstaddr.ss_family
586                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
587                         return EINVAL;
588                 break;
589         default: /*shouldn't happen*/
590                 return EOPNOTSUPP;
591         }
592         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
593                 return EINVAL;
594
595         switch (cmd) {
596         case SIOCALIFADDR:
597             {
598                 struct in_aliasreq ifra;
599
600                 if (iflr->flags & IFLR_PREFIX)
601                         return EINVAL;
602
603                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
604                 bzero(&ifra, sizeof ifra);
605                 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
606
607                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
608
609                 if (iflr->dstaddr.ss_family) {  /*XXX*/
610                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
611                                 iflr->dstaddr.ss_len);
612                 }
613
614                 ifra.ifra_mask.sin_family = AF_INET;
615                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
616                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
617
618                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
619             }
620         case SIOCGLIFADDR:
621         case SIOCDLIFADDR:
622             {
623                 struct ifaddr_container *ifac;
624                 struct in_ifaddr *ia;
625                 struct in_addr mask, candidate, match;
626                 struct sockaddr_in *sin;
627                 int cmp;
628
629                 bzero(&mask, sizeof mask);
630                 if (iflr->flags & IFLR_PREFIX) {
631                         /* lookup a prefix rather than address. */
632                         in_len2mask(&mask, iflr->prefixlen);
633
634                         sin = (struct sockaddr_in *)&iflr->addr;
635                         match.s_addr = sin->sin_addr.s_addr;
636                         match.s_addr &= mask.s_addr;
637
638                         /* if you set extra bits, that's wrong */
639                         if (match.s_addr != sin->sin_addr.s_addr)
640                                 return EINVAL;
641
642                         cmp = 1;
643                 } else {
644                         if (cmd == SIOCGLIFADDR) {
645                                 /* on getting an address, take the 1st match */
646                                 match.s_addr = 0; /* gcc4 warning */
647                                 cmp = 0;        /*XXX*/
648                         } else {
649                                 /* on deleting an address, do exact match */
650                                 in_len2mask(&mask, 32);
651                                 sin = (struct sockaddr_in *)&iflr->addr;
652                                 match.s_addr = sin->sin_addr.s_addr;
653
654                                 cmp = 1;
655                         }
656                 }
657
658                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
659                         struct ifaddr *ifa = ifac->ifa;
660
661                         if (ifa->ifa_addr->sa_family != AF_INET6)
662                                 continue;
663                         if (!cmp)
664                                 break;
665                         candidate.s_addr =
666                         ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
667                         candidate.s_addr &= mask.s_addr;
668                         if (candidate.s_addr == match.s_addr)
669                                 break;
670                 }
671                 if (ifac == NULL)
672                         return EADDRNOTAVAIL;
673                 ia = (struct in_ifaddr *)(ifac->ifa);
674
675                 if (cmd == SIOCGLIFADDR) {
676                         /* fill in the if_laddrreq structure */
677                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
678
679                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
680                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
681                                         ia->ia_dstaddr.sin_len);
682                         } else
683                                 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
684
685                         iflr->prefixlen =
686                                 in_mask2len(&ia->ia_sockmask.sin_addr);
687
688                         iflr->flags = 0;        /*XXX*/
689
690                         return 0;
691                 } else {
692                         struct in_aliasreq ifra;
693
694                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
695                         bzero(&ifra, sizeof ifra);
696                         bcopy(iflr->iflr_name, ifra.ifra_name,
697                                 sizeof ifra.ifra_name);
698
699                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
700                                 ia->ia_addr.sin_len);
701                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
702                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
703                                         ia->ia_dstaddr.sin_len);
704                         }
705                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
706                                 ia->ia_sockmask.sin_len);
707
708                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
709                                           ifp, td);
710                 }
711             }
712         }
713
714         return EOPNOTSUPP;      /*just for safety*/
715 }
716
717 /*
718  * Delete any existing route for an interface.
719  */
720 void
721 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
722 {
723
724         if ((ia->ia_flags & IFA_ROUTE) == 0)
725                 return;
726         if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
727                 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
728         else
729                 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
730         ia->ia_flags &= ~IFA_ROUTE;
731 }
732
733 /*
734  * Initialize an interface's internet address
735  * and routing table entry.
736  */
737 static int
738 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia,
739           const struct sockaddr_in *sin, int scrub)
740 {
741         u_long i = ntohl(sin->sin_addr.s_addr);
742         struct sockaddr_in oldaddr;
743         int flags = RTF_UP, error = 0;
744         int old_hash = 0, new_hash = 0;
745
746         crit_enter();
747         oldaddr = ia->ia_addr;
748         if (oldaddr.sin_family == AF_INET) {
749                 old_hash = 1;
750                 LIST_REMOVE(ia, ia_hash);
751         }
752
753         ia->ia_addr = *sin;
754         if (ia->ia_addr.sin_family == AF_INET) {
755                 new_hash = 1;
756                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
757                     ia, ia_hash);
758         }
759         crit_exit();
760
761         /*
762          * Give the interface a chance to initialize
763          * if this is its first address,
764          * and to validate the address if necessary.
765          */
766         if (ifp->if_ioctl != NULL) {
767                 lwkt_serialize_enter(ifp->if_serializer);
768                 error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL);
769                 lwkt_serialize_exit(ifp->if_serializer);
770                 if (error)
771                         goto fail;
772         }
773
774         /*
775          * Delete old route, if requested.
776          */
777         if (scrub) {
778                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
779                 in_ifscrub(ifp, ia);
780                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
781         }
782
783         /*
784          * Calculate netmask/subnetmask.
785          */
786         if (IN_CLASSA(i))
787                 ia->ia_netmask = IN_CLASSA_NET;
788         else if (IN_CLASSB(i))
789                 ia->ia_netmask = IN_CLASSB_NET;
790         else
791                 ia->ia_netmask = IN_CLASSC_NET;
792         /*
793          * The subnet mask usually includes at least the standard network part,
794          * but may may be smaller in the case of supernetting.
795          * If it is set, we believe it.
796          */
797         if (ia->ia_subnetmask == 0) {
798                 ia->ia_subnetmask = ia->ia_netmask;
799                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
800         } else {
801                 ia->ia_netmask &= ia->ia_subnetmask;
802         }
803         ia->ia_net = i & ia->ia_netmask;
804         ia->ia_subnet = i & ia->ia_subnetmask;
805         in_socktrim(&ia->ia_sockmask);
806
807         /*
808          * Add route for the network.
809          */
810         ia->ia_ifa.ifa_metric = ifp->if_metric;
811         if (ifp->if_flags & IFF_BROADCAST) {
812                 ia->ia_broadaddr.sin_addr.s_addr =
813                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
814                 ia->ia_netbroadcast.s_addr =
815                         htonl(ia->ia_net | ~ ia->ia_netmask);
816         } else if (ifp->if_flags & IFF_LOOPBACK) {
817                 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
818                 flags |= RTF_HOST;
819         } else if (ifp->if_flags & IFF_POINTOPOINT) {
820                 if (ia->ia_dstaddr.sin_family != AF_INET)
821                         return (0);
822                 flags |= RTF_HOST;
823         }
824
825         /*-
826          * Don't add host routes for interface addresses of
827          * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
828          * possible to assign several such address pairs with consistent
829          * results (no host route) and is required by BOOTP.
830          *
831          * XXX: This is ugly !  There should be a way for the caller to
832          *      say that they don't want a host route.
833          */
834         if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
835             ia->ia_netmask != IN_CLASSA_NET ||
836             ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
837                 if ((error = rtinit(&ia->ia_ifa, RTM_ADD, flags)) != 0)
838                         goto fail;
839                 ia->ia_flags |= IFA_ROUTE;
840         }
841
842         /*
843          * If the interface supports multicast, join the "all hosts"
844          * multicast group on that interface.
845          */
846         if (ifp->if_flags & IFF_MULTICAST) {
847                 struct in_addr addr;
848
849                 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
850                 in_addmulti(&addr, ifp);
851         }
852         return (0);
853 fail:
854         crit_enter();
855         if (new_hash)
856                 LIST_REMOVE(ia, ia_hash);
857
858         ia->ia_addr = oldaddr;
859         if (old_hash) {
860                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
861                     ia, ia_hash);
862         }
863         crit_exit();
864         return (error);
865 }
866
867
868 /*
869  * Return 1 if the address might be a local broadcast address.
870  */
871 int
872 in_broadcast(struct in_addr in, struct ifnet *ifp)
873 {
874         struct ifaddr_container *ifac;
875         u_long t;
876
877         if (in.s_addr == INADDR_BROADCAST ||
878             in.s_addr == INADDR_ANY)
879                 return 1;
880         if ((ifp->if_flags & IFF_BROADCAST) == 0)
881                 return 0;
882         t = ntohl(in.s_addr);
883         /*
884          * Look through the list of addresses for a match
885          * with a broadcast address.
886          */
887 #define ia ((struct in_ifaddr *)ifa)
888         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
889                 struct ifaddr *ifa = ifac->ifa;
890
891                 if (ifa->ifa_addr->sa_family == AF_INET &&
892                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
893                      in.s_addr == ia->ia_netbroadcast.s_addr ||
894                      /*
895                       * Check for old-style (host 0) broadcast.
896                       */
897                      t == ia->ia_subnet || t == ia->ia_net) &&
898                      /*
899                       * Check for an all one subnetmask. These
900                       * only exist when an interface gets a secondary
901                       * address.
902                       */
903                      ia->ia_subnetmask != (u_long)0xffffffff)
904                             return 1;
905         }
906         return (0);
907 #undef ia
908 }
909 /*
910  * Add an address to the list of IP multicast addresses for a given interface.
911  */
912 struct in_multi *
913 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
914 {
915         struct in_multi *inm;
916         int error;
917         struct sockaddr_in sin;
918         struct ifmultiaddr *ifma;
919
920         /*
921          * Call generic routine to add membership or increment
922          * refcount.  It wants addresses in the form of a sockaddr,
923          * so we build one here (being careful to zero the unused bytes).
924          */
925         bzero(&sin, sizeof sin);
926         sin.sin_family = AF_INET;
927         sin.sin_len = sizeof sin;
928         sin.sin_addr = *ap;
929         crit_enter();
930         error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
931         if (error) {
932                 crit_exit();
933                 return 0;
934         }
935
936         /*
937          * If ifma->ifma_protospec is null, then if_addmulti() created
938          * a new record.  Otherwise, we are done.
939          */
940         if (ifma->ifma_protospec != 0) {
941                 crit_exit();
942                 return ifma->ifma_protospec;
943         }
944
945         /* XXX - if_addmulti uses M_WAITOK.  Can this really be called
946            at interrupt time?  If so, need to fix if_addmulti. XXX */
947         inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
948         inm->inm_addr = *ap;
949         inm->inm_ifp = ifp;
950         inm->inm_ifma = ifma;
951         ifma->ifma_protospec = inm;
952         LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
953
954         /*
955          * Let IGMP know that we have joined a new IP multicast group.
956          */
957         igmp_joingroup(inm);
958         crit_exit();
959         return (inm);
960 }
961
962 /*
963  * Delete a multicast address record.
964  */
965 void
966 in_delmulti(struct in_multi *inm)
967 {
968         struct ifmultiaddr *ifma;
969         struct in_multi my_inm;
970
971         crit_enter();
972         ifma = inm->inm_ifma;
973         my_inm.inm_ifp = NULL ; /* don't send the leave msg */
974         if (ifma->ifma_refcount == 1) {
975                 /*
976                  * No remaining claims to this record; let IGMP know that
977                  * we are leaving the multicast group.
978                  * But do it after the if_delmulti() which might reset
979                  * the interface and nuke the packet.
980                  */
981                 my_inm = *inm ;
982                 ifma->ifma_protospec = 0;
983                 LIST_REMOVE(inm, inm_link);
984                 kfree(inm, M_IPMADDR);
985         }
986         /* XXX - should be separate API for when we have an ifma? */
987         if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
988         if (my_inm.inm_ifp != NULL)
989                 igmp_leavegroup(&my_inm);
990         crit_exit();
991 }
992
993 void
994 in_ifdetach(struct ifnet *ifp)
995 {
996         in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
997         in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
998 }