38469d12f5c1a5fa98d2cac4eec7cb968b226eae
[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.27 2008/05/24 03:57:26 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 *,
72             struct in_ifaddr *, 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                 /*fall through*/
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
309         case SIOCAIFADDR:
310         case SIOCDIFADDR:
311                 if (ifp == NULL)
312                         return (EADDRNOTAVAIL);
313                 if (ifra->ifra_addr.sin_family == AF_INET) {
314                         for (oia = ia; ia; ia = TAILQ_NEXT(ia, ia_link)) {
315                                 if (ia->ia_ifp == ifp  &&
316                                     ia->ia_addr.sin_addr.s_addr ==
317                                     ifra->ifra_addr.sin_addr.s_addr)
318                                         break;
319                         }
320                         if ((ifp->if_flags & IFF_POINTOPOINT)
321                             && (cmd == SIOCAIFADDR)
322                             && (ifra->ifra_dstaddr.sin_addr.s_addr
323                                 == INADDR_ANY)) {
324                                 return EDESTADDRREQ;
325                         }
326                 }
327                 if (cmd == SIOCDIFADDR && ia == NULL)
328                         return (EADDRNOTAVAIL);
329                 /* FALLTHROUGH */
330         case SIOCSIFADDR:
331         case SIOCSIFNETMASK:
332         case SIOCSIFDSTADDR:
333                 if (td && (error = suser(td)) != 0)
334                         return error;
335
336                 if (ifp == NULL)
337                         return (EADDRNOTAVAIL);
338                 if (ia == NULL) {
339                         struct ifaddr *ifa;
340
341                         ia = ifa_create(sizeof(*ia), M_WAITOK);
342
343                         /*
344                          * Protect from NETISR_IP traversing address list
345                          * while we're modifying it.
346                          */
347                         crit_enter();
348                         
349                         TAILQ_INSERT_TAIL(&in_ifaddrhead, ia, ia_link);
350                         ifa = &ia->ia_ifa;
351                         ifa_iflink(ifa, ifp, 1);
352
353                         ifa->ifa_addr = (struct sockaddr *)&ia->ia_addr;
354                         ifa->ifa_dstaddr = (struct sockaddr *)&ia->ia_dstaddr;
355                         ifa->ifa_netmask = (struct sockaddr *)&ia->ia_sockmask;
356                         ia->ia_sockmask.sin_len = 8;
357                         ia->ia_sockmask.sin_family = AF_INET;
358                         if (ifp->if_flags & IFF_BROADCAST) {
359                                 ia->ia_broadaddr.sin_len = sizeof ia->ia_addr;
360                                 ia->ia_broadaddr.sin_family = AF_INET;
361                         }
362                         ia->ia_ifp = ifp;
363                         if (!(ifp->if_flags & IFF_LOOPBACK))
364                                 in_interfaces++;
365                         iaIsNew = 1;
366                         crit_exit();
367                 }
368                 break;
369
370         case SIOCSIFBRDADDR:
371                 if (td && (error = suser(td)) != 0)
372                         return error;
373                 /* FALLTHROUGH */
374
375         case SIOCGIFADDR:
376         case SIOCGIFNETMASK:
377         case SIOCGIFDSTADDR:
378         case SIOCGIFBRDADDR:
379                 if (ia == NULL)
380                         return (EADDRNOTAVAIL);
381                 break;
382         }
383         switch (cmd) {
384
385         case SIOCGIFADDR:
386                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_addr;
387                 return (0);
388
389         case SIOCGIFBRDADDR:
390                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
391                         return (EINVAL);
392                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_broadaddr;
393                 return (0);
394
395         case SIOCGIFDSTADDR:
396                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
397                         return (EINVAL);
398                 *((struct sockaddr_in *)&ifr->ifr_dstaddr) = ia->ia_dstaddr;
399                 return (0);
400
401         case SIOCGIFNETMASK:
402                 *((struct sockaddr_in *)&ifr->ifr_addr) = ia->ia_sockmask;
403                 return (0);
404
405         case SIOCSIFDSTADDR:
406                 if ((ifp->if_flags & IFF_POINTOPOINT) == 0)
407                         return (EINVAL);
408                 oldaddr = ia->ia_dstaddr;
409                 ia->ia_dstaddr = *(struct sockaddr_in *)&ifr->ifr_dstaddr;
410                 lwkt_serialize_enter(ifp->if_serializer);
411                 if (ifp->if_ioctl &&
412                     (error = ifp->if_ioctl(ifp, SIOCSIFDSTADDR, (caddr_t)ia,
413                                               td->td_proc->p_ucred))) {
414                         ia->ia_dstaddr = oldaddr;
415                         lwkt_serialize_exit(ifp->if_serializer);
416                         return (error);
417                 }
418                 if (ia->ia_flags & IFA_ROUTE) {
419                         ia->ia_ifa.ifa_dstaddr = (struct sockaddr *)&oldaddr;
420                         rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
421                         ia->ia_ifa.ifa_dstaddr =
422                                         (struct sockaddr *)&ia->ia_dstaddr;
423                         rtinit(&ia->ia_ifa, RTM_ADD, RTF_HOST | RTF_UP);
424                 }
425                 lwkt_serialize_exit(ifp->if_serializer);
426                 return (0);
427
428         case SIOCSIFBRDADDR:
429                 if ((ifp->if_flags & IFF_BROADCAST) == 0)
430                         return (EINVAL);
431                 ia->ia_broadaddr = *(struct sockaddr_in *)&ifr->ifr_broadaddr;
432                 return (0);
433
434         case SIOCSIFADDR:
435                 error = in_ifinit(ifp, ia,
436                     (struct sockaddr_in *) &ifr->ifr_addr, 1);
437                 if (error != 0 && iaIsNew)
438                         break;
439                 if (error == 0)
440                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
441                 return (0);
442
443         case SIOCSIFNETMASK:
444                 ia->ia_sockmask.sin_addr = ifra->ifra_addr.sin_addr;
445                 ia->ia_subnetmask = ntohl(ia->ia_sockmask.sin_addr.s_addr);
446                 return (0);
447
448         case SIOCAIFADDR:
449                 maskIsNew = 0;
450                 hostIsNew = 1;
451                 error = 0;
452                 if (ia->ia_addr.sin_family == AF_INET) {
453                         if (ifra->ifra_addr.sin_len == 0) {
454                                 ifra->ifra_addr = ia->ia_addr;
455                                 hostIsNew = 0;
456                         } else if (ifra->ifra_addr.sin_addr.s_addr ==
457                                                ia->ia_addr.sin_addr.s_addr)
458                                 hostIsNew = 0;
459                 }
460                 if (ifra->ifra_mask.sin_len) {
461                         in_ifscrub(ifp, ia);
462                         ia->ia_sockmask = ifra->ifra_mask;
463                         ia->ia_sockmask.sin_family = AF_INET;
464                         ia->ia_subnetmask =
465                              ntohl(ia->ia_sockmask.sin_addr.s_addr);
466                         maskIsNew = 1;
467                 }
468                 if ((ifp->if_flags & IFF_POINTOPOINT) &&
469                     (ifra->ifra_dstaddr.sin_family == AF_INET)) {
470                         in_ifscrub(ifp, ia);
471                         ia->ia_dstaddr = ifra->ifra_dstaddr;
472                         maskIsNew  = 1; /* We lie; but the effect's the same */
473                 }
474                 if (ifra->ifra_addr.sin_family == AF_INET &&
475                     (hostIsNew || maskIsNew))
476                         error = in_ifinit(ifp, ia, &ifra->ifra_addr, 0);
477
478                 if (error != 0 && iaIsNew)
479                         break;
480
481                 if ((ifp->if_flags & IFF_BROADCAST) &&
482                     (ifra->ifra_broadaddr.sin_family == AF_INET))
483                         ia->ia_broadaddr = ifra->ifra_broadaddr;
484                 if (error == 0)
485                         EVENTHANDLER_INVOKE(ifaddr_event, ifp);
486                 return (error);
487
488         case SIOCDIFADDR:
489                 /*
490                  * in_ifscrub kills the interface route.
491                  */
492                 in_ifscrub(ifp, ia);
493                 /*
494                  * in_ifadown gets rid of all the rest of
495                  * the routes.  This is not quite the right
496                  * thing to do, but at least if we are running
497                  * a routing process they will come back.
498                  */
499                 in_ifadown(&ia->ia_ifa, 1);
500                 EVENTHANDLER_INVOKE(ifaddr_event, ifp);
501                 error = 0;
502                 break;
503
504         default:
505                 if (ifp == NULL || ifp->if_ioctl == NULL)
506                         return (EOPNOTSUPP);
507                 lwkt_serialize_enter(ifp->if_serializer);
508                 error = ifp->if_ioctl(ifp, cmd, data, td->td_proc->p_ucred);
509                 lwkt_serialize_exit(ifp->if_serializer);
510                 return (error);
511         }
512
513         ifa_ifunlink(&ia->ia_ifa, ifp);
514
515         /*
516          * Protect from NETISR_IP traversing address list while we're modifying
517          * it.
518          */
519         crit_enter();   /* XXX MP */
520         TAILQ_REMOVE(&in_ifaddrhead, ia, ia_link);
521         LIST_REMOVE(ia, ia_hash);
522         crit_exit();    /* XXX MP */
523
524         ifa_destroy(&ia->ia_ifa);
525
526         return (error);
527 }
528
529 /*
530  * SIOC[GAD]LIFADDR.
531  *      SIOCGLIFADDR: get first address. (?!?)
532  *      SIOCGLIFADDR with IFLR_PREFIX:
533  *              get first address that matches the specified prefix.
534  *      SIOCALIFADDR: add the specified address.
535  *      SIOCALIFADDR with IFLR_PREFIX:
536  *              EINVAL since we can't deduce hostid part of the address.
537  *      SIOCDLIFADDR: delete the specified address.
538  *      SIOCDLIFADDR with IFLR_PREFIX:
539  *              delete the first address that matches the specified prefix.
540  * return values:
541  *      EINVAL on invalid parameters
542  *      EADDRNOTAVAIL on prefix match failed/specified address not found
543  *      other values may be returned from in_ioctl()
544  *
545  * NOTE! td might be NULL.
546  */
547 static int
548 in_lifaddr_ioctl(struct socket *so, u_long cmd, caddr_t data, struct ifnet *ifp,
549                  struct thread *td)
550 {
551         struct if_laddrreq *iflr = (struct if_laddrreq *)data;
552
553         /* sanity checks */
554         if (!data || !ifp) {
555                 panic("invalid argument to in_lifaddr_ioctl");
556                 /*NOTRECHED*/
557         }
558
559         switch (cmd) {
560         case SIOCGLIFADDR:
561                 /* address must be specified on GET with IFLR_PREFIX */
562                 if ((iflr->flags & IFLR_PREFIX) == 0)
563                         break;
564                 /*FALLTHROUGH*/
565         case SIOCALIFADDR:
566         case SIOCDLIFADDR:
567                 /* address must be specified on ADD and DELETE */
568                 if (iflr->addr.ss_family != AF_INET)
569                         return EINVAL;
570                 if (iflr->addr.ss_len != sizeof(struct sockaddr_in))
571                         return EINVAL;
572                 /* XXX need improvement */
573                 if (iflr->dstaddr.ss_family
574                  && iflr->dstaddr.ss_family != AF_INET)
575                         return EINVAL;
576                 if (iflr->dstaddr.ss_family
577                  && iflr->dstaddr.ss_len != sizeof(struct sockaddr_in))
578                         return EINVAL;
579                 break;
580         default: /*shouldn't happen*/
581                 return EOPNOTSUPP;
582         }
583         if (sizeof(struct in_addr) * 8 < iflr->prefixlen)
584                 return EINVAL;
585
586         switch (cmd) {
587         case SIOCALIFADDR:
588             {
589                 struct in_aliasreq ifra;
590
591                 if (iflr->flags & IFLR_PREFIX)
592                         return EINVAL;
593
594                 /* copy args to in_aliasreq, perform ioctl(SIOCAIFADDR_IN6). */
595                 bzero(&ifra, sizeof ifra);
596                 bcopy(iflr->iflr_name, ifra.ifra_name, sizeof ifra.ifra_name);
597
598                 bcopy(&iflr->addr, &ifra.ifra_addr, iflr->addr.ss_len);
599
600                 if (iflr->dstaddr.ss_family) {  /*XXX*/
601                         bcopy(&iflr->dstaddr, &ifra.ifra_dstaddr,
602                                 iflr->dstaddr.ss_len);
603                 }
604
605                 ifra.ifra_mask.sin_family = AF_INET;
606                 ifra.ifra_mask.sin_len = sizeof(struct sockaddr_in);
607                 in_len2mask(&ifra.ifra_mask.sin_addr, iflr->prefixlen);
608
609                 return in_control(so, SIOCAIFADDR, (caddr_t)&ifra, ifp, td);
610             }
611         case SIOCGLIFADDR:
612         case SIOCDLIFADDR:
613             {
614                 struct ifaddr_container *ifac;
615                 struct in_ifaddr *ia;
616                 struct in_addr mask, candidate, match;
617                 struct sockaddr_in *sin;
618                 int cmp;
619
620                 bzero(&mask, sizeof mask);
621                 if (iflr->flags & IFLR_PREFIX) {
622                         /* lookup a prefix rather than address. */
623                         in_len2mask(&mask, iflr->prefixlen);
624
625                         sin = (struct sockaddr_in *)&iflr->addr;
626                         match.s_addr = sin->sin_addr.s_addr;
627                         match.s_addr &= mask.s_addr;
628
629                         /* if you set extra bits, that's wrong */
630                         if (match.s_addr != sin->sin_addr.s_addr)
631                                 return EINVAL;
632
633                         cmp = 1;
634                 } else {
635                         if (cmd == SIOCGLIFADDR) {
636                                 /* on getting an address, take the 1st match */
637                                 match.s_addr = 0; /* gcc4 warning */
638                                 cmp = 0;        /*XXX*/
639                         } else {
640                                 /* on deleting an address, do exact match */
641                                 in_len2mask(&mask, 32);
642                                 sin = (struct sockaddr_in *)&iflr->addr;
643                                 match.s_addr = sin->sin_addr.s_addr;
644
645                                 cmp = 1;
646                         }
647                 }
648
649                 TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
650                         struct ifaddr *ifa = ifac->ifa;
651
652                         if (ifa->ifa_addr->sa_family != AF_INET6)
653                                 continue;
654                         if (!cmp)
655                                 break;
656                         candidate.s_addr =
657                         ((struct sockaddr_in *)&ifa->ifa_addr)->sin_addr.s_addr;
658                         candidate.s_addr &= mask.s_addr;
659                         if (candidate.s_addr == match.s_addr)
660                                 break;
661                 }
662                 if (ifac == NULL)
663                         return EADDRNOTAVAIL;
664                 ia = (struct in_ifaddr *)(ifac->ifa);
665
666                 if (cmd == SIOCGLIFADDR) {
667                         /* fill in the if_laddrreq structure */
668                         bcopy(&ia->ia_addr, &iflr->addr, ia->ia_addr.sin_len);
669
670                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
671                                 bcopy(&ia->ia_dstaddr, &iflr->dstaddr,
672                                         ia->ia_dstaddr.sin_len);
673                         } else
674                                 bzero(&iflr->dstaddr, sizeof iflr->dstaddr);
675
676                         iflr->prefixlen =
677                                 in_mask2len(&ia->ia_sockmask.sin_addr);
678
679                         iflr->flags = 0;        /*XXX*/
680
681                         return 0;
682                 } else {
683                         struct in_aliasreq ifra;
684
685                         /* fill in_aliasreq and do ioctl(SIOCDIFADDR_IN6) */
686                         bzero(&ifra, sizeof ifra);
687                         bcopy(iflr->iflr_name, ifra.ifra_name,
688                                 sizeof ifra.ifra_name);
689
690                         bcopy(&ia->ia_addr, &ifra.ifra_addr,
691                                 ia->ia_addr.sin_len);
692                         if ((ifp->if_flags & IFF_POINTOPOINT) != 0) {
693                                 bcopy(&ia->ia_dstaddr, &ifra.ifra_dstaddr,
694                                         ia->ia_dstaddr.sin_len);
695                         }
696                         bcopy(&ia->ia_sockmask, &ifra.ifra_dstaddr,
697                                 ia->ia_sockmask.sin_len);
698
699                         return in_control(so, SIOCDIFADDR, (caddr_t)&ifra,
700                                           ifp, td);
701                 }
702             }
703         }
704
705         return EOPNOTSUPP;      /*just for safety*/
706 }
707
708 /*
709  * Delete any existing route for an interface.
710  */
711 void
712 in_ifscrub(struct ifnet *ifp, struct in_ifaddr *ia)
713 {
714
715         if ((ia->ia_flags & IFA_ROUTE) == 0)
716                 return;
717         if (ifp->if_flags & (IFF_LOOPBACK|IFF_POINTOPOINT))
718                 rtinit(&ia->ia_ifa, RTM_DELETE, RTF_HOST);
719         else
720                 rtinit(&ia->ia_ifa, RTM_DELETE, 0);
721         ia->ia_flags &= ~IFA_ROUTE;
722 }
723
724 /*
725  * Initialize an interface's internet address
726  * and routing table entry.
727  */
728 static int
729 in_ifinit(struct ifnet *ifp, struct in_ifaddr *ia, struct sockaddr_in *sin, int scrub)
730 {
731         u_long i = ntohl(sin->sin_addr.s_addr);
732         struct sockaddr_in oldaddr;
733         int flags = RTF_UP, error = 0;
734
735         lwkt_serialize_enter(ifp->if_serializer);
736
737         oldaddr = ia->ia_addr;
738         if (oldaddr.sin_family == AF_INET)
739                 LIST_REMOVE(ia, ia_hash);
740         ia->ia_addr = *sin;
741         if (ia->ia_addr.sin_family == AF_INET)
742                 LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
743                     ia, ia_hash);
744         /*
745          * Give the interface a chance to initialize
746          * if this is its first address,
747          * and to validate the address if necessary.
748          */
749         if (ifp->if_ioctl &&
750             (error = ifp->if_ioctl(ifp, SIOCSIFADDR, (caddr_t)ia, NULL))) {
751                 lwkt_serialize_exit(ifp->if_serializer);
752                 /* LIST_REMOVE(ia, ia_hash) is done in in_control */
753                 ia->ia_addr = oldaddr;
754                 if (ia->ia_addr.sin_family == AF_INET)
755                         LIST_INSERT_HEAD(INADDR_HASH(ia->ia_addr.sin_addr.s_addr),
756                             ia, ia_hash);
757                 return (error);
758         }
759         lwkt_serialize_exit(ifp->if_serializer);
760         if (scrub) {
761                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&oldaddr;
762                 in_ifscrub(ifp, ia);
763                 ia->ia_ifa.ifa_addr = (struct sockaddr *)&ia->ia_addr;
764         }
765         if (IN_CLASSA(i))
766                 ia->ia_netmask = IN_CLASSA_NET;
767         else if (IN_CLASSB(i))
768                 ia->ia_netmask = IN_CLASSB_NET;
769         else
770                 ia->ia_netmask = IN_CLASSC_NET;
771         /*
772          * The subnet mask usually includes at least the standard network part,
773          * but may may be smaller in the case of supernetting.
774          * If it is set, we believe it.
775          */
776         if (ia->ia_subnetmask == 0) {
777                 ia->ia_subnetmask = ia->ia_netmask;
778                 ia->ia_sockmask.sin_addr.s_addr = htonl(ia->ia_subnetmask);
779         } else
780                 ia->ia_netmask &= ia->ia_subnetmask;
781         ia->ia_net = i & ia->ia_netmask;
782         ia->ia_subnet = i & ia->ia_subnetmask;
783         in_socktrim(&ia->ia_sockmask);
784         /*
785          * Add route for the network.
786          */
787         ia->ia_ifa.ifa_metric = ifp->if_metric;
788         if (ifp->if_flags & IFF_BROADCAST) {
789                 ia->ia_broadaddr.sin_addr.s_addr =
790                         htonl(ia->ia_subnet | ~ia->ia_subnetmask);
791                 ia->ia_netbroadcast.s_addr =
792                         htonl(ia->ia_net | ~ ia->ia_netmask);
793         } else if (ifp->if_flags & IFF_LOOPBACK) {
794                 ia->ia_ifa.ifa_dstaddr = ia->ia_ifa.ifa_addr;
795                 flags |= RTF_HOST;
796         } else if (ifp->if_flags & IFF_POINTOPOINT) {
797                 if (ia->ia_dstaddr.sin_family != AF_INET)
798                         return (0);
799                 flags |= RTF_HOST;
800         }
801
802         /*-
803          * Don't add host routes for interface addresses of
804          * 0.0.0.0 --> 0.255.255.255 netmask 255.0.0.0.  This makes it
805          * possible to assign several such address pairs with consistent
806          * results (no host route) and is required by BOOTP.
807          *
808          * XXX: This is ugly !  There should be a way for the caller to
809          *      say that they don't want a host route.
810          */
811         if (ia->ia_addr.sin_addr.s_addr != INADDR_ANY ||
812             ia->ia_netmask != IN_CLASSA_NET ||
813             ia->ia_dstaddr.sin_addr.s_addr != htonl(IN_CLASSA_HOST)) {
814                 if ((error = rtinit(&ia->ia_ifa, (int)RTM_ADD, flags)) != 0) {
815                         ia->ia_addr = oldaddr;
816                         return (error);
817                 }
818                 ia->ia_flags |= IFA_ROUTE;
819         }
820
821         /*
822          * If the interface supports multicast, join the "all hosts"
823          * multicast group on that interface.
824          */
825         if (ifp->if_flags & IFF_MULTICAST) {
826                 struct in_addr addr;
827
828                 addr.s_addr = htonl(INADDR_ALLHOSTS_GROUP);
829                 in_addmulti(&addr, ifp);
830         }
831         return (error);
832 }
833
834
835 /*
836  * Return 1 if the address might be a local broadcast address.
837  */
838 int
839 in_broadcast(struct in_addr in, struct ifnet *ifp)
840 {
841         struct ifaddr_container *ifac;
842         u_long t;
843
844         if (in.s_addr == INADDR_BROADCAST ||
845             in.s_addr == INADDR_ANY)
846                 return 1;
847         if ((ifp->if_flags & IFF_BROADCAST) == 0)
848                 return 0;
849         t = ntohl(in.s_addr);
850         /*
851          * Look through the list of addresses for a match
852          * with a broadcast address.
853          */
854 #define ia ((struct in_ifaddr *)ifa)
855         TAILQ_FOREACH(ifac, &ifp->if_addrheads[mycpuid], ifa_link) {
856                 struct ifaddr *ifa = ifac->ifa;
857
858                 if (ifa->ifa_addr->sa_family == AF_INET &&
859                     (in.s_addr == ia->ia_broadaddr.sin_addr.s_addr ||
860                      in.s_addr == ia->ia_netbroadcast.s_addr ||
861                      /*
862                       * Check for old-style (host 0) broadcast.
863                       */
864                      t == ia->ia_subnet || t == ia->ia_net) &&
865                      /*
866                       * Check for an all one subnetmask. These
867                       * only exist when an interface gets a secondary
868                       * address.
869                       */
870                      ia->ia_subnetmask != (u_long)0xffffffff)
871                             return 1;
872         }
873         return (0);
874 #undef ia
875 }
876 /*
877  * Add an address to the list of IP multicast addresses for a given interface.
878  */
879 struct in_multi *
880 in_addmulti(struct in_addr *ap, struct ifnet *ifp)
881 {
882         struct in_multi *inm;
883         int error;
884         struct sockaddr_in sin;
885         struct ifmultiaddr *ifma;
886
887         /*
888          * Call generic routine to add membership or increment
889          * refcount.  It wants addresses in the form of a sockaddr,
890          * so we build one here (being careful to zero the unused bytes).
891          */
892         bzero(&sin, sizeof sin);
893         sin.sin_family = AF_INET;
894         sin.sin_len = sizeof sin;
895         sin.sin_addr = *ap;
896         crit_enter();
897         error = if_addmulti(ifp, (struct sockaddr *)&sin, &ifma);
898         if (error) {
899                 crit_exit();
900                 return 0;
901         }
902
903         /*
904          * If ifma->ifma_protospec is null, then if_addmulti() created
905          * a new record.  Otherwise, we are done.
906          */
907         if (ifma->ifma_protospec != 0) {
908                 crit_exit();
909                 return ifma->ifma_protospec;
910         }
911
912         /* XXX - if_addmulti uses M_WAITOK.  Can this really be called
913            at interrupt time?  If so, need to fix if_addmulti. XXX */
914         inm = kmalloc(sizeof *inm, M_IPMADDR, M_WAITOK | M_ZERO);
915         inm->inm_addr = *ap;
916         inm->inm_ifp = ifp;
917         inm->inm_ifma = ifma;
918         ifma->ifma_protospec = inm;
919         LIST_INSERT_HEAD(&in_multihead, inm, inm_link);
920
921         /*
922          * Let IGMP know that we have joined a new IP multicast group.
923          */
924         igmp_joingroup(inm);
925         crit_exit();
926         return (inm);
927 }
928
929 /*
930  * Delete a multicast address record.
931  */
932 void
933 in_delmulti(struct in_multi *inm)
934 {
935         struct ifmultiaddr *ifma;
936         struct in_multi my_inm;
937
938         crit_enter();
939         ifma = inm->inm_ifma;
940         my_inm.inm_ifp = NULL ; /* don't send the leave msg */
941         if (ifma->ifma_refcount == 1) {
942                 /*
943                  * No remaining claims to this record; let IGMP know that
944                  * we are leaving the multicast group.
945                  * But do it after the if_delmulti() which might reset
946                  * the interface and nuke the packet.
947                  */
948                 my_inm = *inm ;
949                 ifma->ifma_protospec = 0;
950                 LIST_REMOVE(inm, inm_link);
951                 kfree(inm, M_IPMADDR);
952         }
953         /* XXX - should be separate API for when we have an ifma? */
954         if_delmulti(ifma->ifma_ifp, ifma->ifma_addr);
955         if (my_inm.inm_ifp != NULL)
956                 igmp_leavegroup(&my_inm);
957         crit_exit();
958 }
959
960 void
961 in_ifdetach(struct ifnet *ifp)
962 {
963         in_pcbpurgeif0(LIST_FIRST(&ripcbinfo.pcblisthead), ifp);
964         in_pcbpurgeif0(LIST_FIRST(&udbinfo.pcblisthead), ifp);
965 }