Annotation of embedaddon/dnsmasq/src/dhcp6.c, revision 1.1.1.2

1.1.1.2 ! misho       1: /* dnsmasq is Copyright (c) 2000-2014 Simon Kelley
1.1       misho       2: 
                      3:    This program is free software; you can redistribute it and/or modify
                      4:    it under the terms of the GNU General Public License as published by
                      5:    the Free Software Foundation; version 2 dated June, 1991, or
                      6:    (at your option) version 3 dated 29 June, 2007.
                      7:  
                      8:    This program is distributed in the hope that it will be useful,
                      9:    but WITHOUT ANY WARRANTY; without even the implied warranty of
                     10:    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     11:    GNU General Public License for more details.
                     12:      
                     13:    You should have received a copy of the GNU General Public License
                     14:    along with this program.  If not, see <http://www.gnu.org/licenses/>.
                     15: */
                     16: 
                     17: #include "dnsmasq.h"
                     18: 
                     19: #ifdef HAVE_DHCP6
                     20: 
1.1.1.2 ! misho      21: #include <netinet/icmp6.h>
        !            22: 
1.1       misho      23: struct iface_param {
                     24:   struct dhcp_context *current;
1.1.1.2 ! misho      25:   struct dhcp_relay *relay;
        !            26:   struct in6_addr fallback, relay_local, ll_addr, ula_addr;
1.1       misho      27:   int ind, addr_match;
                     28: };
                     29: 
1.1.1.2 ! misho      30: struct mac_param {
        !            31:   struct in6_addr *target;
        !            32:   unsigned char *mac;
        !            33:   unsigned int maclen;
        !            34: };
        !            35: 
        !            36: 
1.1       misho      37: static int complete_context6(struct in6_addr *local,  int prefix,
                     38:                             int scope, int if_index, int flags, 
                     39:                             unsigned int preferred, unsigned int valid, void *vparam);
1.1.1.2 ! misho      40: static int find_mac(int family, char *addrp, char *mac, size_t maclen, void *parmv);
1.1       misho      41: static int make_duid1(int index, unsigned int type, char *mac, size_t maclen, void *parm); 
                     42: 
                     43: void dhcp6_init(void)
                     44: {
                     45:   int fd;
                     46:   struct sockaddr_in6 saddr;
                     47: #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
                     48:   int class = IPTOS_CLASS_CS6;
                     49: #endif
                     50:   int oneopt = 1;
                     51: 
                     52:   if ((fd = socket(PF_INET6, SOCK_DGRAM, IPPROTO_UDP)) == -1 ||
                     53: #if defined(IPV6_TCLASS) && defined(IPTOS_CLASS_CS6)
                     54:       setsockopt(fd, IPPROTO_IPV6, IPV6_TCLASS, &class, sizeof(class)) == -1 ||
                     55: #endif
                     56:       setsockopt(fd, IPPROTO_IPV6, IPV6_V6ONLY, &oneopt, sizeof(oneopt)) == -1 ||
                     57:       !fix_fd(fd) ||
                     58:       !set_ipv6pktinfo(fd))
                     59:     die (_("cannot create DHCPv6 socket: %s"), NULL, EC_BADNET);
                     60:   
                     61:  /* When bind-interfaces is set, there might be more than one dnmsasq
                     62:      instance binding port 547. That's OK if they serve different networks.
                     63:      Need to set REUSEADDR|REUSEPORT to make this posible.
                     64:      Handle the case that REUSEPORT is defined, but the kernel doesn't 
                     65:      support it. This handles the introduction of REUSEPORT on Linux. */
                     66:   if (option_bool(OPT_NOWILD) || option_bool(OPT_CLEVERBIND))
                     67:     {
1.1.1.2 ! misho      68:       int rc = 0;
1.1       misho      69: 
                     70: #ifdef SO_REUSEPORT
                     71:       if ((rc = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &oneopt, sizeof(oneopt))) == -1 &&
1.1.1.2 ! misho      72:          errno == ENOPROTOOPT)
        !            73:        rc = 0;
1.1       misho      74: #endif
                     75:       
1.1.1.2 ! misho      76:       if (rc != -1)
1.1       misho      77:        rc = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &oneopt, sizeof(oneopt));
                     78:       
                     79:       if (rc == -1)
                     80:        die(_("failed to set SO_REUSE{ADDR|PORT} on DHCPv6 socket: %s"), NULL, EC_BADNET);
                     81:     }
                     82:   
                     83:   memset(&saddr, 0, sizeof(saddr));
                     84: #ifdef HAVE_SOCKADDR_SA_LEN
                     85:   saddr.sin6_len = sizeof(struct sockaddr_in6);
                     86: #endif
                     87:   saddr.sin6_family = AF_INET6;
                     88:   saddr.sin6_addr = in6addr_any;
                     89:   saddr.sin6_port = htons(DHCPV6_SERVER_PORT);
                     90:   
                     91:   if (bind(fd, (struct sockaddr *)&saddr, sizeof(struct sockaddr_in6)))
                     92:     die(_("failed to bind DHCPv6 server socket: %s"), NULL, EC_BADNET);
                     93:   
                     94:   daemon->dhcp6fd = fd;
                     95: }
                     96: 
                     97: void dhcp6_packet(time_t now)
                     98: {
                     99:   struct dhcp_context *context;
1.1.1.2 ! misho     100:   struct dhcp_relay *relay;
1.1       misho     101:   struct iface_param parm;
                    102:   struct cmsghdr *cmptr;
                    103:   struct msghdr msg;
                    104:   int if_index = 0;
                    105:   union {
                    106:     struct cmsghdr align; /* this ensures alignment */
                    107:     char control6[CMSG_SPACE(sizeof(struct in6_pktinfo))];
                    108:   } control_u;
                    109:   struct sockaddr_in6 from;
                    110:   ssize_t sz; 
                    111:   struct ifreq ifr;
                    112:   struct iname *tmp;
                    113:   unsigned short port;
1.1.1.2 ! misho     114:   struct in6_addr dst_addr;
        !           115: 
        !           116:   memset(&dst_addr, 0, sizeof(dst_addr));
1.1       misho     117: 
                    118:   msg.msg_control = control_u.control6;
                    119:   msg.msg_controllen = sizeof(control_u);
                    120:   msg.msg_flags = 0;
                    121:   msg.msg_name = &from;
                    122:   msg.msg_namelen = sizeof(from);
                    123:   msg.msg_iov =  &daemon->dhcp_packet;
                    124:   msg.msg_iovlen = 1;
                    125:   
                    126:   if ((sz = recv_dhcp_packet(daemon->dhcp6fd, &msg)) == -1)
                    127:     return;
                    128:   
                    129:   for (cmptr = CMSG_FIRSTHDR(&msg); cmptr; cmptr = CMSG_NXTHDR(&msg, cmptr))
                    130:     if (cmptr->cmsg_level == IPPROTO_IPV6 && cmptr->cmsg_type == daemon->v6pktinfo)
                    131:       {
                    132:        union {
                    133:          unsigned char *c;
                    134:          struct in6_pktinfo *p;
                    135:        } p;
                    136:        p.c = CMSG_DATA(cmptr);
                    137:         
                    138:        if_index = p.p->ipi6_ifindex;
1.1.1.2 ! misho     139:        dst_addr = p.p->ipi6_addr;
1.1       misho     140:       }
                    141: 
                    142:   if (!indextoname(daemon->dhcp6fd, if_index, ifr.ifr_name))
                    143:     return;
                    144: 
1.1.1.2 ! misho     145:   if ((port = relay_reply6(&from, sz, ifr.ifr_name)) == 0)
1.1       misho     146:     {
1.1.1.2 ! misho     147:       for (tmp = daemon->if_except; tmp; tmp = tmp->next)
        !           148:        if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name))
        !           149:          return;
1.1       misho     150:       
1.1.1.2 ! misho     151:       for (tmp = daemon->dhcp_except; tmp; tmp = tmp->next)
1.1       misho     152:        if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name))
1.1.1.2 ! misho     153:          return;
        !           154:       
        !           155:       parm.current = NULL;
        !           156:       parm.relay = NULL;
        !           157:       memset(&parm.relay_local, 0, IN6ADDRSZ);
        !           158:       parm.ind = if_index;
        !           159:       parm.addr_match = 0;
        !           160:       memset(&parm.fallback, 0, IN6ADDRSZ);
        !           161:       memset(&parm.ll_addr, 0, IN6ADDRSZ);
        !           162:       memset(&parm.ula_addr, 0, IN6ADDRSZ);
        !           163:       
        !           164:       for (context = daemon->dhcp6; context; context = context->next)
        !           165:        if (IN6_IS_ADDR_UNSPECIFIED(&context->start6) && context->prefix == 0)
        !           166:          {
        !           167:            /* wildcard context for DHCP-stateless only */
        !           168:            parm.current = context;
        !           169:            context->current = NULL;
        !           170:          }
        !           171:        else
        !           172:          {
        !           173:            /* unlinked contexts are marked by context->current == context */
        !           174:            context->current = context;
        !           175:            memset(&context->local6, 0, IN6ADDRSZ);
        !           176:          }
1.1       misho     177: 
1.1.1.2 ! misho     178:       for (relay = daemon->relay6; relay; relay = relay->next)
        !           179:        relay->current = relay;
        !           180:       
        !           181:       if (!iface_enumerate(AF_INET6, &parm, complete_context6))
1.1       misho     182:        return;
                    183: 
1.1.1.2 ! misho     184:       if (daemon->if_names || daemon->if_addrs)
        !           185:        {
        !           186:          
        !           187:          for (tmp = daemon->if_names; tmp; tmp = tmp->next)
        !           188:            if (tmp->name && wildcard_match(tmp->name, ifr.ifr_name))
        !           189:              break;
        !           190:          
        !           191:          if (!tmp && !parm.addr_match)
        !           192:            return;
        !           193:        }
        !           194:       
        !           195:       if (parm.relay)
        !           196:        {
        !           197:          /* Ignore requests sent to the ALL_SERVERS multicast address for relay when
        !           198:             we're listening there for DHCPv6 server reasons. */
        !           199:          struct in6_addr all_servers;
        !           200:          
        !           201:          inet_pton(AF_INET6, ALL_SERVERS, &all_servers);
        !           202:          
        !           203:          if (!IN6_ARE_ADDR_EQUAL(&dst_addr, &all_servers))
        !           204:            relay_upstream6(parm.relay, sz, &from.sin6_addr, from.sin6_scope_id);
        !           205:          return;
        !           206:        }
        !           207:       
        !           208:       /* May have configured relay, but not DHCP server */
        !           209:       if (!daemon->doing_dhcp6)
        !           210:        return;
        !           211:       
        !           212:       lease_prune(NULL, now); /* lose any expired leases */
        !           213:       
        !           214:       port = dhcp6_reply(parm.current, if_index, ifr.ifr_name, &parm.fallback, 
        !           215:                         &parm.ll_addr, &parm.ula_addr, sz, &from.sin6_addr, now);
        !           216:       
        !           217:       lease_update_file(now);
        !           218:       lease_update_dns(0);
        !           219:     }
        !           220:                          
1.1       misho     221:   /* The port in the source address of the original request should
                    222:      be correct, but at least once client sends from the server port,
                    223:      so we explicitly send to the client port to a client, and the
                    224:      server port to a relay. */
                    225:   if (port != 0)
                    226:     {
                    227:       from.sin6_port = htons(port);
                    228:       while (sendto(daemon->dhcp6fd, daemon->outpacket.iov_base, save_counter(0), 
                    229:                    0, (struct sockaddr *)&from, sizeof(from)) == -1 &&
                    230:           retry_send());
                    231:     }
                    232: }
                    233: 
1.1.1.2 ! misho     234: void get_client_mac(struct in6_addr *client, int iface, unsigned char *mac, unsigned int *maclenp, unsigned int *mactypep)
        !           235: {
        !           236:   /* Recieving a packet from a host does not populate the neighbour
        !           237:      cache, so we send a neighbour discovery request if we can't 
        !           238:      find the sender. Repeat a few times in case of packet loss. */
        !           239:   
        !           240:   struct neigh_packet neigh;
        !           241:   struct sockaddr_in6 addr;
        !           242:   struct mac_param mac_param;
        !           243:   int i;
        !           244: 
        !           245:   neigh.type = ND_NEIGHBOR_SOLICIT;
        !           246:   neigh.code = 0;
        !           247:   neigh.reserved = 0;
        !           248:   neigh.target = *client;
        !           249:   
        !           250:   memset(&addr, 0, sizeof(addr));
        !           251: #ifdef HAVE_SOCKADDR_SA_LEN
        !           252:   addr.sin6_len = sizeof(struct sockaddr_in6);
        !           253: #endif
        !           254:   addr.sin6_family = AF_INET6;
        !           255:   addr.sin6_port = htons(IPPROTO_ICMPV6);
        !           256:   addr.sin6_addr = *client;
        !           257:   addr.sin6_scope_id = iface;
        !           258:   
        !           259:   mac_param.target = client;
        !           260:   mac_param.maclen = 0;
        !           261:   mac_param.mac = mac;
        !           262:   
        !           263:   for (i = 0; i < 5; i++)
        !           264:     {
        !           265:       struct timespec ts;
        !           266:       
        !           267:       iface_enumerate(AF_UNSPEC, &mac_param, find_mac);
        !           268:       
        !           269:       if (mac_param.maclen != 0)
        !           270:        break;
        !           271:       
        !           272:       sendto(daemon->icmp6fd, &neigh, sizeof(neigh), 0, (struct sockaddr *)&addr, sizeof(addr));
        !           273:       
        !           274:       ts.tv_sec = 0;
        !           275:       ts.tv_nsec = 100000000; /* 100ms */
        !           276:       nanosleep(&ts, NULL);
        !           277:     }
        !           278: 
        !           279:   *maclenp = mac_param.maclen;
        !           280:   *mactypep = ARPHRD_ETHER;
        !           281: }
        !           282:     
        !           283: static int find_mac(int family, char *addrp, char *mac, size_t maclen, void *parmv)
        !           284: {
        !           285:   struct mac_param *parm = parmv;
        !           286:   
        !           287:   if (family == AF_INET6 && IN6_ARE_ADDR_EQUAL(parm->target, (struct in6_addr *)addrp))
        !           288:     {
        !           289:       if (maclen <= DHCP_CHADDR_MAX)
        !           290:        {
        !           291:          parm->maclen = maclen;
        !           292:          memcpy(parm->mac, mac, maclen);
        !           293:        }
        !           294:       
        !           295:       return 0; /* found, abort */
        !           296:     }
        !           297:   
        !           298:   return 1;
        !           299: }
        !           300: 
1.1       misho     301: static int complete_context6(struct in6_addr *local,  int prefix,
                    302:                             int scope, int if_index, int flags, unsigned int preferred, 
                    303:                             unsigned int valid, void *vparam)
                    304: {
                    305:   struct dhcp_context *context;
1.1.1.2 ! misho     306:   struct dhcp_relay *relay;
1.1       misho     307:   struct iface_param *param = vparam;
                    308:   struct iname *tmp;
                    309:  
                    310:   (void)scope; /* warning */
                    311:   
1.1.1.2 ! misho     312:   if (if_index == param->ind)
        !           313:     {
        !           314:       if (IN6_IS_ADDR_LINKLOCAL(local))
        !           315:        param->ll_addr = *local;
        !           316:       else if (IN6_IS_ADDR_ULA(local))
        !           317:        param->ula_addr = *local;
        !           318: 
        !           319:       if (!IN6_IS_ADDR_LOOPBACK(local) &&
        !           320:          !IN6_IS_ADDR_LINKLOCAL(local) &&
        !           321:          !IN6_IS_ADDR_MULTICAST(local))
1.1       misho     322:        {
1.1.1.2 ! misho     323:          /* if we have --listen-address config, see if the 
        !           324:             arrival interface has a matching address. */
        !           325:          for (tmp = daemon->if_addrs; tmp; tmp = tmp->next)
        !           326:            if (tmp->addr.sa.sa_family == AF_INET6 &&
        !           327:                IN6_ARE_ADDR_EQUAL(&tmp->addr.in6.sin6_addr, local))
        !           328:              param->addr_match = 1;
        !           329:          
        !           330:          /* Determine a globally address on the arrival interface, even
        !           331:             if we have no matching dhcp-context, because we're only
        !           332:             allocating on remote subnets via relays. This
        !           333:             is used as a default for the DNS server option. */
        !           334:          param->fallback = *local;
        !           335:          
        !           336:          for (context = daemon->dhcp6; context; context = context->next)
1.1       misho     337:            {
1.1.1.2 ! misho     338:              if ((context->flags & CONTEXT_DHCP) &&
        !           339:                  !(context->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
        !           340:                  prefix <= context->prefix &&
        !           341:                  is_same_net6(local, &context->start6, context->prefix) &&
        !           342:                  is_same_net6(local, &context->end6, context->prefix))
1.1       misho     343:                {
                    344:                  
                    345:                  
1.1.1.2 ! misho     346:                  /* link it onto the current chain if we've not seen it before */
        !           347:                  if (context->current == context)
        !           348:                    {
        !           349:                      struct dhcp_context *tmp, **up;
        !           350:                      
        !           351:                      /* use interface values only for contructed contexts */
        !           352:                      if (!(context->flags & CONTEXT_CONSTRUCTED))
        !           353:                        preferred = valid = 0xffffffff;
        !           354:                      else if (flags & IFACE_DEPRECATED)
        !           355:                        preferred = 0;
        !           356:                      
        !           357:                      if (context->flags & CONTEXT_DEPRECATE)
        !           358:                        preferred = 0;
        !           359:                      
        !           360:                      /* order chain, longest preferred time first */
        !           361:                      for (up = &param->current, tmp = param->current; tmp; tmp = tmp->current)
        !           362:                        if (tmp->preferred <= preferred)
        !           363:                          break;
        !           364:                        else
        !           365:                          up = &tmp->current;
        !           366:                      
        !           367:                      context->current = *up;
        !           368:                      *up = context;
        !           369:                      context->local6 = *local;
        !           370:                      context->preferred = preferred;
        !           371:                      context->valid = valid;
        !           372:                    }
1.1       misho     373:                }
                    374:            }
                    375:        }
1.1.1.2 ! misho     376: 
        !           377:       for (relay = daemon->relay6; relay; relay = relay->next)
        !           378:        if (IN6_ARE_ADDR_EQUAL(local, &relay->local.addr.addr6) && relay->current == relay &&
        !           379:            (IN6_IS_ADDR_UNSPECIFIED(&param->relay_local) || IN6_ARE_ADDR_EQUAL(local, &param->relay_local)))
        !           380:          {
        !           381:            relay->current = param->relay;
        !           382:            param->relay = relay;
        !           383:            param->relay_local = *local;
        !           384:          }
        !           385:       
1.1       misho     386:     }          
1.1.1.2 ! misho     387:  
        !           388:  return 1;
1.1       misho     389: }
                    390: 
                    391: struct dhcp_config *config_find_by_address6(struct dhcp_config *configs, struct in6_addr *net, int prefix, u64 addr)
                    392: {
                    393:   struct dhcp_config *config;
                    394:   
                    395:   for (config = configs; config; config = config->next)
                    396:     if ((config->flags & CONFIG_ADDR6) &&
                    397:        is_same_net6(&config->addr6, net, prefix) &&
                    398:        (prefix == 128 || addr6part(&config->addr6) == addr))
                    399:       return config;
                    400:   
                    401:   return NULL;
                    402: }
                    403: 
1.1.1.2 ! misho     404: struct dhcp_context *address6_allocate(struct dhcp_context *context,  unsigned char *clid, int clid_len, int temp_addr,
1.1       misho     405:                                       int iaid, int serial, struct dhcp_netid *netids, int plain_range, struct in6_addr *ans)   
                    406: {
                    407:   /* Find a free address: exclude anything in use and anything allocated to
                    408:      a particular hwaddr/clientid/hostname in our configuration.
                    409:      Try to return from contexts which match netids first. 
                    410:      
                    411:      Note that we assume the address prefix lengths are 64 or greater, so we can
                    412:      get by with 64 bit arithmetic.
                    413: */
                    414: 
                    415:   u64 start, addr;
                    416:   struct dhcp_context *c, *d;
                    417:   int i, pass;
                    418:   u64 j; 
                    419: 
                    420:   /* hash hwaddr: use the SDBM hashing algorithm.  This works
1.1.1.2 ! misho     421:      for MAC addresses, let's see how it manages with client-ids! 
        !           422:      For temporary addresses, we generate a new random one each time. */
        !           423:   if (temp_addr)
        !           424:     j = rand64();
        !           425:   else
        !           426:     for (j = iaid, i = 0; i < clid_len; i++)
        !           427:       j += clid[i] + (j << 6) + (j << 16) - j;
1.1       misho     428:   
                    429:   for (pass = 0; pass <= plain_range ? 1 : 0; pass++)
                    430:     for (c = context; c; c = c->current)
                    431:       if (c->flags & (CONTEXT_DEPRECATE | CONTEXT_STATIC | CONTEXT_RA_STATELESS | CONTEXT_USED))
                    432:        continue;
                    433:       else if (!match_netid(c->filter, netids, pass))
                    434:        continue;
                    435:       else
                    436:        { 
1.1.1.2 ! misho     437:          if (!temp_addr && option_bool(OPT_CONSEC_ADDR))
1.1       misho     438:            /* seed is largest extant lease addr in this context */
                    439:            start = lease_find_max_addr6(c) + serial;
                    440:          else
                    441:            start = addr6part(&c->start6) + ((j + c->addr_epoch) % (1 + addr6part(&c->end6) - addr6part(&c->start6)));
                    442: 
                    443:          /* iterate until we find a free address. */
                    444:          addr = start;
                    445:          
                    446:          do {
                    447:            /* eliminate addresses in use by the server. */
                    448:            for (d = context; d; d = d->current)
                    449:              if (addr == addr6part(&d->local6))
                    450:                break;
                    451: 
                    452:            if (!d &&
                    453:                !lease6_find_by_addr(&c->start6, c->prefix, addr) && 
                    454:                !config_find_by_address6(daemon->dhcp_conf, &c->start6, c->prefix, addr))
                    455:              {
                    456:                *ans = c->start6;
                    457:                setaddr6part (ans, addr);
                    458:                return c;
                    459:              }
                    460:        
                    461:            addr++;
                    462:            
                    463:            if (addr  == addr6part(&c->end6) + 1)
                    464:              addr = addr6part(&c->start6);
                    465:            
                    466:          } while (addr != start);
                    467:        }
                    468:           
                    469:   return NULL;
                    470: }
                    471: 
                    472: /* can dynamically allocate addr */
                    473: struct dhcp_context *address6_available(struct dhcp_context *context, 
                    474:                                        struct in6_addr *taddr,
                    475:                                        struct dhcp_netid *netids,
                    476:                                        int plain_range)
                    477: {
                    478:   u64 start, end, addr = addr6part(taddr);
                    479:   struct dhcp_context *tmp;
                    480:  
                    481:   for (tmp = context; tmp; tmp = tmp->current)
                    482:     {
                    483:       start = addr6part(&tmp->start6);
                    484:       end = addr6part(&tmp->end6);
                    485: 
                    486:       if (!(tmp->flags & (CONTEXT_STATIC | CONTEXT_RA_STATELESS)) &&
                    487:           is_same_net6(&tmp->start6, taddr, tmp->prefix) &&
                    488:          is_same_net6(&tmp->end6, taddr, tmp->prefix) &&
                    489:          addr >= start &&
                    490:           addr <= end &&
                    491:           match_netid(tmp->filter, netids, plain_range))
                    492:         return tmp;
                    493:     }
                    494: 
                    495:   return NULL;
                    496: }
                    497: 
                    498: /* address OK if configured */
                    499: struct dhcp_context *address6_valid(struct dhcp_context *context, 
                    500:                                    struct in6_addr *taddr,
                    501:                                    struct dhcp_netid *netids,
                    502:                                    int plain_range)
                    503: {
                    504:   struct dhcp_context *tmp;
                    505:  
                    506:   for (tmp = context; tmp; tmp = tmp->current)
                    507:     if (is_same_net6(&tmp->start6, taddr, tmp->prefix) &&
                    508:        match_netid(tmp->filter, netids, plain_range))
                    509:       return tmp;
                    510: 
                    511:   return NULL;
                    512: }
                    513: 
                    514: int config_valid(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr)
                    515: {
                    516:   if (!config || !(config->flags & CONFIG_ADDR6))
                    517:     return 0;
                    518: 
                    519:   if ((config->flags & CONFIG_WILDCARD) && context->prefix == 64)
                    520:     {
                    521:       *addr = context->start6;
                    522:       setaddr6part(addr, addr6part(&config->addr6));
                    523:       return 1;
                    524:     }
                    525:   
                    526:   if (is_same_net6(&context->start6, &config->addr6, context->prefix))
                    527:     {
                    528:       *addr = config->addr6;
                    529:       return 1;
                    530:     }
                    531:   
                    532:   return 0;
                    533: }
                    534: 
                    535: void make_duid(time_t now)
                    536: {
1.1.1.2 ! misho     537:   (void)now;
        !           538: 
1.1       misho     539:   if (daemon->duid_config)
                    540:     {
                    541:       unsigned char *p;
                    542:       
                    543:       daemon->duid = p = safe_malloc(daemon->duid_config_len + 6);
                    544:       daemon->duid_len = daemon->duid_config_len + 6;
                    545:       PUTSHORT(2, p); /* DUID_EN */
                    546:       PUTLONG(daemon->duid_enterprise, p);
                    547:       memcpy(p, daemon->duid_config, daemon->duid_config_len);
                    548:     }
                    549:   else
                    550:     {
1.1.1.2 ! misho     551:       time_t newnow = 0;
        !           552:       
        !           553:       /* If we have no persistent lease database, or a non-stable RTC, use DUID_LL (newnow == 0) */
        !           554: #ifndef HAVE_BROKEN_RTC
1.1       misho     555:       /* rebase epoch to 1/1/2000 */
1.1.1.2 ! misho     556:       if (!option_bool(OPT_LEASE_RO) || daemon->lease_change_command)
        !           557:        newnow = now - 946684800;
        !           558: #endif      
1.1       misho     559:       
                    560:       iface_enumerate(AF_LOCAL, &newnow, make_duid1);
                    561:       
                    562:       if(!daemon->duid)
                    563:        die("Cannot create DHCPv6 server DUID: %s", NULL, EC_MISC);
                    564:     }
                    565: }
                    566: 
                    567: static int make_duid1(int index, unsigned int type, char *mac, size_t maclen, void *parm)
                    568: {
                    569:   /* create DUID as specified in RFC3315. We use the MAC of the
                    570:      first interface we find that isn't loopback or P-to-P and
                    571:      has address-type < 256. Address types above 256 are things like 
                    572:      tunnels which don't have usable MAC addresses. */
                    573:   
                    574:   unsigned char *p;
                    575:   (void)index;
1.1.1.2 ! misho     576:   (void)parm;
        !           577:   time_t newnow = *((time_t *)parm);
        !           578:   
1.1       misho     579:   if (type >= 256)
                    580:     return 1;
                    581: 
1.1.1.2 ! misho     582:   if (newnow == 0)
        !           583:     {
        !           584:       daemon->duid = p = safe_malloc(maclen + 4);
        !           585:       daemon->duid_len = maclen + 4;
        !           586:       PUTSHORT(3, p); /* DUID_LL */
        !           587:       PUTSHORT(type, p); /* address type */
        !           588:     }
        !           589:   else
        !           590:     {
        !           591:       daemon->duid = p = safe_malloc(maclen + 8);
        !           592:       daemon->duid_len = maclen + 8;
        !           593:       PUTSHORT(1, p); /* DUID_LLT */
        !           594:       PUTSHORT(type, p); /* address type */
        !           595:       PUTLONG(*((time_t *)parm), p); /* time */
        !           596:     }
        !           597:   
1.1       misho     598:   memcpy(p, mac, maclen);
                    599: 
                    600:   return 0;
                    601: }
                    602: 
                    603: struct cparam {
                    604:   time_t now;
                    605:   int newone, newname;
                    606: };
                    607: 
                    608: static int construct_worker(struct in6_addr *local, int prefix, 
                    609:                            int scope, int if_index, int flags, 
                    610:                            int preferred, int valid, void *vparam)
                    611: {
                    612:   char ifrn_name[IFNAMSIZ];
                    613:   struct in6_addr start6, end6;
                    614:   struct dhcp_context *template, *context;
                    615: 
                    616:   (void)scope;
                    617:   (void)flags;
                    618:   (void)valid;
                    619:   (void)preferred;
                    620: 
                    621:   struct cparam *param = vparam;
                    622: 
                    623:   if (IN6_IS_ADDR_LOOPBACK(local) ||
                    624:       IN6_IS_ADDR_LINKLOCAL(local) ||
                    625:       IN6_IS_ADDR_MULTICAST(local))
                    626:     return 1;
                    627: 
1.1.1.2 ! misho     628:   if (!(flags & IFACE_PERMANENT))
        !           629:     return 1;
        !           630: 
        !           631:   if (flags & IFACE_DEPRECATED)
        !           632:     return 1;
        !           633: 
        !           634:   if (!indextoname(daemon->icmp6fd, if_index, ifrn_name))
1.1       misho     635:     return 0;
                    636:   
                    637:   for (template = daemon->dhcp6; template; template = template->next)
                    638:     if (!(template->flags & CONTEXT_TEMPLATE))
                    639:       {
                    640:        /* non-template entries, just fill in interface and local addresses */
1.1.1.2 ! misho     641:        if (prefix <= template->prefix &&
        !           642:            is_same_net6(local, &template->start6, template->prefix) &&
        !           643:            is_same_net6(local, &template->end6, template->prefix))
1.1       misho     644:          {
                    645:            template->if_index = if_index;
                    646:            template->local6 = *local;
                    647:          }
                    648:        
                    649:       }
1.1.1.2 ! misho     650:     else if (wildcard_match(template->template_interface, ifrn_name) &&
        !           651:             template->prefix >= prefix)
1.1       misho     652:       {
                    653:        start6 = *local;
                    654:        setaddr6part(&start6, addr6part(&template->start6));
                    655:        end6 = *local;
                    656:        setaddr6part(&end6, addr6part(&template->end6));
                    657:        
                    658:        for (context = daemon->dhcp6; context; context = context->next)
                    659:          if ((context->flags & CONTEXT_CONSTRUCTED) &&
                    660:              IN6_ARE_ADDR_EQUAL(&start6, &context->start6) &&
                    661:              IN6_ARE_ADDR_EQUAL(&end6, &context->end6))
                    662:            {
1.1.1.2 ! misho     663:              int flags = context->flags;
        !           664:              context->flags &= ~(CONTEXT_GC | CONTEXT_OLD);
        !           665:              if (flags & CONTEXT_OLD)
        !           666:                {
        !           667:                  /* address went, now it's back */
        !           668:                  log_context(AF_INET6, context); 
        !           669:                  /* fast RAs for a while */
        !           670:                  ra_start_unsolicted(param->now, context);
        !           671:                  param->newone = 1; 
        !           672:                  /* Add address to name again */
        !           673:                  if (context->flags & CONTEXT_RA_NAME)
        !           674:                    param->newname = 1;
        !           675:                }
1.1       misho     676:              break;
                    677:            }
                    678:        
                    679:        if (!context && (context = whine_malloc(sizeof (struct dhcp_context))))
                    680:          {
                    681:            *context = *template;
                    682:            context->start6 = start6;
                    683:            context->end6 = end6;
                    684:            context->flags &= ~CONTEXT_TEMPLATE;
                    685:            context->flags |= CONTEXT_CONSTRUCTED;
                    686:            context->if_index = if_index;
                    687:            context->local6 = *local;
1.1.1.2 ! misho     688:            context->saved_valid = 0;
1.1       misho     689:            
                    690:            context->next = daemon->dhcp6;
                    691:            daemon->dhcp6 = context;
                    692: 
                    693:            ra_start_unsolicted(param->now, context);
                    694:            /* we created a new one, need to call
                    695:               lease_update_file to get periodic functions called */
                    696:            param->newone = 1; 
                    697: 
                    698:            /* Will need to add new putative SLAAC addresses to existing leases */
                    699:            if (context->flags & CONTEXT_RA_NAME)
                    700:              param->newname = 1;
                    701:            
                    702:            log_context(AF_INET6, context);
                    703:          } 
                    704:       }
                    705:   
                    706:   return 1;
                    707: }
                    708: 
                    709: void dhcp_construct_contexts(time_t now)
                    710: { 
1.1.1.2 ! misho     711:   struct dhcp_context *context, *tmp, **up;
1.1       misho     712:   struct cparam param;
                    713:   param.newone = 0;
                    714:   param.newname = 0;
                    715:   param.now = now;
                    716: 
                    717:   for (context = daemon->dhcp6; context; context = context->next)
1.1.1.2 ! misho     718:     if (context->flags & CONTEXT_CONSTRUCTED)
        !           719:       context->flags |= CONTEXT_GC;
        !           720:    
1.1       misho     721:   iface_enumerate(AF_INET6, &param, construct_worker);
                    722: 
                    723:   for (up = &daemon->dhcp6, context = daemon->dhcp6; context; context = tmp)
                    724:     {
                    725:       
1.1.1.2 ! misho     726:       tmp = context->next; 
        !           727:      
        !           728:       if (context->flags & CONTEXT_GC && !(context->flags & CONTEXT_OLD))
1.1       misho     729:        {
1.1.1.2 ! misho     730:          if ((context->flags & (CONTEXT_RA_ONLY | CONTEXT_RA_NAME | CONTEXT_RA_STATELESS)) ||
        !           731:              option_bool(OPT_RA))
        !           732:            {
        !           733:              /* previously constructed context has gone. advertise it's demise */
        !           734:              context->flags |= CONTEXT_OLD;
        !           735:              context->address_lost_time = now;
        !           736:              /* Apply same ceiling of configured lease time as in radv.c */
        !           737:              if (context->saved_valid > context->lease_time)
        !           738:                context->saved_valid = context->lease_time;
        !           739:              /* maximum time is 2 hours, from RFC */
        !           740:              if (context->saved_valid > 7200) /* 2 hours */
        !           741:                context->saved_valid = 7200;
        !           742:              ra_start_unsolicted(now, context);
        !           743:              param.newone = 1; /* include deletion */ 
        !           744:              
        !           745:              if (context->flags & CONTEXT_RA_NAME)
        !           746:                param.newname = 1; 
        !           747:                              
        !           748:              log_context(AF_INET6, context);
        !           749:              
        !           750:              up = &context->next;
        !           751:            }
        !           752:          else
        !           753:            {
        !           754:              /* we were never doing RA for this, so free now */
        !           755:              *up = context->next;
        !           756:              free(context);
        !           757:            }
1.1       misho     758:        }
                    759:       else
1.1.1.2 ! misho     760:         up = &context->next;
1.1       misho     761:     }
                    762:   
                    763:   if (param.newone)
                    764:     {
                    765:       if (daemon->dhcp || daemon->doing_dhcp6)
                    766:        {
                    767:          if (param.newname)
                    768:            lease_update_slaac(now);
                    769:          lease_update_file(now);
                    770:        }
                    771:       else 
                    772:        /* Not doing DHCP, so no lease system, manage alarms for ra only */
                    773:        send_alarm(periodic_ra(now), now);
                    774:     }
                    775: }
                    776: 
                    777: #endif
                    778: 
                    779: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>