Imported Upstream version 2.88
[platform/upstream/dnsmasq.git] / src / rfc3315.c
1 /* dnsmasq is Copyright (c) 2000-2022 Simon Kelley
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
18 #include "dnsmasq.h"
19
20 #ifdef HAVE_DHCP6
21
22 struct state {
23   unsigned char *clid;
24   int clid_len, ia_type, interface, hostname_auth, lease_allocate;
25   char *client_hostname, *hostname, *domain, *send_domain;
26   struct dhcp_context *context;
27   struct in6_addr *link_address, *fallback, *ll_addr, *ula_addr;
28   unsigned int xid, fqdn_flags, iaid;
29   char *iface_name;
30   void *packet_options, *end;
31   struct dhcp_netid *tags, *context_tags;
32   unsigned char mac[DHCP_CHADDR_MAX];
33   unsigned int mac_len, mac_type;
34 };
35
36 static int dhcp6_maybe_relay(struct state *state, unsigned char *inbuff, size_t sz, 
37                              struct in6_addr *client_addr, int is_unicast, time_t now);
38 static int dhcp6_no_relay(struct state *state, int msg_type, unsigned char *inbuff, size_t sz, int is_unicast, time_t now);
39 static void log6_opts(int nest, unsigned int xid, void *start_opts, void *end_opts);
40 static void log6_packet(struct state *state, char *type, struct in6_addr *addr, char *string);
41 static void log6_quiet(struct state *state, char *type, struct in6_addr *addr, char *string);
42 static void *opt6_find (void *opts, void *end, unsigned int search, unsigned int minsize);
43 static void *opt6_next(void *opts, void *end);
44 static unsigned int opt6_uint(unsigned char *opt, int offset, int size);
45 static void get_context_tag(struct state *state, struct dhcp_context *context);
46 static int check_ia(struct state *state, void *opt, void **endp, void **ia_option);
47 static int build_ia(struct state *state, int *t1cntr);
48 static void end_ia(int t1cntr, unsigned int min_time, int do_fuzz);
49 static void mark_context_used(struct state *state, struct in6_addr *addr);
50 static void mark_config_used(struct dhcp_context *context, struct in6_addr *addr);
51 static int check_address(struct state *state, struct in6_addr *addr);
52 static int config_valid(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr, struct state *state, time_t now);
53 static struct addrlist *config_implies(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr);
54 static void add_address(struct state *state, struct dhcp_context *context, unsigned int lease_time, void *ia_option, 
55                         unsigned int *min_time, struct in6_addr *addr, time_t now);
56 static void update_leases(struct state *state, struct dhcp_context *context, struct in6_addr *addr, unsigned int lease_time, time_t now);
57 static int add_local_addrs(struct dhcp_context *context);
58 static struct dhcp_netid *add_options(struct state *state, int do_refresh);
59 static void calculate_times(struct dhcp_context *context, unsigned int *min_time, unsigned int *valid_timep, 
60                             unsigned int *preferred_timep, unsigned int lease_time);
61
62 #define opt6_len(opt) ((int)(opt6_uint(opt, -2, 2)))
63 #define opt6_type(opt) (opt6_uint(opt, -4, 2))
64 #define opt6_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[4+(i)]))
65
66 #define opt6_user_vendor_ptr(opt, i) ((void *)&(((unsigned char *)(opt))[2+(i)]))
67 #define opt6_user_vendor_len(opt) ((int)(opt6_uint(opt, -4, 2)))
68 #define opt6_user_vendor_next(opt, end) (opt6_next(((void *) opt) - 2, end))
69  
70
71 unsigned short dhcp6_reply(struct dhcp_context *context, int interface, char *iface_name,
72                            struct in6_addr *fallback,  struct in6_addr *ll_addr, struct in6_addr *ula_addr,
73                            size_t sz, struct in6_addr *client_addr, time_t now)
74 {
75   struct dhcp_vendor *vendor;
76   int msg_type;
77   struct state state;
78   
79   if (sz <= 4)
80     return 0;
81   
82   msg_type = *((unsigned char *)daemon->dhcp_packet.iov_base);
83   
84   /* Mark these so we only match each at most once, to avoid tangled linked lists */
85   for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
86     vendor->netid.next = &vendor->netid;
87   
88   reset_counter();
89   state.context = context;
90   state.interface = interface;
91   state.iface_name = iface_name;
92   state.fallback = fallback;
93   state.ll_addr = ll_addr;
94   state.ula_addr = ula_addr;
95   state.mac_len = 0;
96   state.tags = NULL;
97   state.link_address = NULL;
98
99   if (dhcp6_maybe_relay(&state, daemon->dhcp_packet.iov_base, sz, client_addr, 
100                         IN6_IS_ADDR_MULTICAST(client_addr), now))
101     return msg_type == DHCP6RELAYFORW ? DHCPV6_SERVER_PORT : DHCPV6_CLIENT_PORT;
102
103   return 0;
104 }
105
106 /* This cost me blood to write, it will probably cost you blood to understand - srk. */
107 static int dhcp6_maybe_relay(struct state *state, unsigned char *inbuff, size_t sz, 
108                              struct in6_addr *client_addr, int is_unicast, time_t now)
109 {
110   void *end = inbuff + sz;
111   void *opts = inbuff + 34;
112   int msg_type = *inbuff;
113   unsigned char *outmsgtypep;
114   void *opt;
115   struct dhcp_vendor *vendor;
116
117   /* if not an encapsulated relayed message, just do the stuff */
118   if (msg_type != DHCP6RELAYFORW)
119     {
120       /* if link_address != NULL if points to the link address field of the 
121          innermost nested RELAYFORW message, which is where we find the
122          address of the network on which we can allocate an address.
123          Recalculate the available contexts using that information. 
124
125       link_address == NULL means there's no relay in use, so we try and find the client's 
126       MAC address from the local ND cache. */
127       
128       if (!state->link_address)
129         get_client_mac(client_addr, state->interface, state->mac, &state->mac_len, &state->mac_type, now);
130       else
131         {
132           struct dhcp_context *c;
133           struct shared_network *share = NULL;
134           state->context = NULL;
135
136           if (!IN6_IS_ADDR_LOOPBACK(state->link_address) &&
137               !IN6_IS_ADDR_LINKLOCAL(state->link_address) &&
138               !IN6_IS_ADDR_MULTICAST(state->link_address))
139             for (c = daemon->dhcp6; c; c = c->next)
140               {
141                 for (share = daemon->shared_networks; share; share = share->next)
142                   {
143                     if (share->shared_addr.s_addr != 0)
144                       continue;
145                     
146                     if (share->if_index != 0 ||
147                         !IN6_ARE_ADDR_EQUAL(state->link_address, &share->match_addr6))
148                       continue;
149                     
150                     if ((c->flags & CONTEXT_DHCP) &&
151                         !(c->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
152                         is_same_net6(&share->shared_addr6, &c->start6, c->prefix) &&
153                         is_same_net6(&share->shared_addr6, &c->end6, c->prefix))
154                       break;
155                   }
156                 
157                 if (share ||
158                     ((c->flags & CONTEXT_DHCP) &&
159                      !(c->flags & (CONTEXT_TEMPLATE | CONTEXT_OLD)) &&
160                      is_same_net6(state->link_address, &c->start6, c->prefix) &&
161                      is_same_net6(state->link_address, &c->end6, c->prefix)))
162                   {
163                     c->preferred = c->valid = 0xffffffff;
164                     c->current = state->context;
165                     state->context = c;
166                   }
167               }
168           
169           if (!state->context)
170             {
171               inet_ntop(AF_INET6, state->link_address, daemon->addrbuff, ADDRSTRLEN); 
172               my_syslog(MS_DHCP | LOG_WARNING, 
173                         _("no address range available for DHCPv6 request from relay at %s"),
174                         daemon->addrbuff);
175               return 0;
176             }
177         }
178           
179       if (!state->context)
180         {
181           my_syslog(MS_DHCP | LOG_WARNING, 
182                     _("no address range available for DHCPv6 request via %s"), state->iface_name);
183           return 0;
184         }
185
186       return dhcp6_no_relay(state, msg_type, inbuff, sz, is_unicast, now);
187     }
188
189   /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option
190      which is               1   +    1   +    16      +     16     + 2 + 2 = 38 */
191   if (sz < 38)
192     return 0;
193   
194   /* copy header stuff into reply message and set type to reply */
195   if (!(outmsgtypep = put_opt6(inbuff, 34)))
196     return 0;
197   *outmsgtypep = DHCP6RELAYREPL;
198
199   /* look for relay options and set tags if found. */
200   for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
201     {
202       int mopt;
203       
204       if (vendor->match_type == MATCH_SUBSCRIBER)
205         mopt = OPTION6_SUBSCRIBER_ID;
206       else if (vendor->match_type == MATCH_REMOTE)
207         mopt = OPTION6_REMOTE_ID; 
208       else
209         continue;
210
211       if ((opt = opt6_find(opts, end, mopt, 1)) &&
212           vendor->len == opt6_len(opt) &&
213           memcmp(vendor->data, opt6_ptr(opt, 0), vendor->len) == 0 &&
214           vendor->netid.next != &vendor->netid)
215         {
216           vendor->netid.next = state->tags;
217           state->tags = &vendor->netid;
218           break;
219         }
220     }
221   
222   /* RFC-6939 */
223   if ((opt = opt6_find(opts, end, OPTION6_CLIENT_MAC, 3)))
224     {
225       if (opt6_len(opt) - 2 > DHCP_CHADDR_MAX) {
226         return 0;
227       }
228       state->mac_type = opt6_uint(opt, 0, 2);
229       state->mac_len = opt6_len(opt) - 2;
230       memcpy(&state->mac[0], opt6_ptr(opt, 2), state->mac_len);
231     }
232   
233   for (opt = opts; opt; opt = opt6_next(opt, end))
234     {
235       if (opt6_ptr(opt, 0) + opt6_len(opt) > end) 
236         return 0;
237      
238       /* Don't copy MAC address into reply. */
239       if (opt6_type(opt) != OPTION6_CLIENT_MAC)
240         {
241           int o = new_opt6(opt6_type(opt));
242           if (opt6_type(opt) == OPTION6_RELAY_MSG)
243             {
244               struct in6_addr align;
245               /* the packet data is unaligned, copy to aligned storage */
246               memcpy(&align, inbuff + 2, IN6ADDRSZ); 
247               state->link_address = &align;
248               /* zero is_unicast since that is now known to refer to the 
249                  relayed packet, not the original sent by the client */
250               if (!dhcp6_maybe_relay(state, opt6_ptr(opt, 0), opt6_len(opt), client_addr, 0, now))
251                 return 0;
252             }
253           else
254             put_opt6(opt6_ptr(opt, 0), opt6_len(opt));
255           end_opt6(o);
256         }
257     }
258   
259   return 1;
260 }
261
262 static int dhcp6_no_relay(struct state *state, int msg_type, unsigned char *inbuff, size_t sz, int is_unicast, time_t now)
263 {
264   void *opt;
265   int i, o, o1, start_opts, start_msg;
266   struct dhcp_opt *opt_cfg;
267   struct dhcp_netid *tagif;
268   struct dhcp_config *config = NULL;
269   struct dhcp_netid known_id, iface_id, v6_id;
270   unsigned char outmsgtype;
271   struct dhcp_vendor *vendor;
272   struct dhcp_context *context_tmp;
273   struct dhcp_mac *mac_opt;
274   unsigned int ignore = 0;
275
276   state->packet_options = inbuff + 4;
277   state->end = inbuff + sz;
278   state->clid = NULL;
279   state->clid_len = 0;
280   state->lease_allocate = 0;
281   state->context_tags = NULL;
282   state->domain = NULL;
283   state->send_domain = NULL;
284   state->hostname_auth = 0;
285   state->hostname = NULL;
286   state->client_hostname = NULL;
287   state->fqdn_flags = 0x01; /* default to send if we receive no FQDN option */
288
289   /* set tag with name == interface */
290   iface_id.net = state->iface_name;
291   iface_id.next = state->tags;
292   state->tags = &iface_id; 
293
294   /* set tag "dhcpv6" */
295   v6_id.net = "dhcpv6";
296   v6_id.next = state->tags;
297   state->tags = &v6_id;
298
299   start_msg = save_counter(-1);
300   /* copy over transaction-id */
301   if (!put_opt6(inbuff, 4))
302     return 0;
303   start_opts = save_counter(-1);
304   state->xid = inbuff[3] | inbuff[2] << 8 | inbuff[1] << 16;
305     
306   /* We're going to be linking tags from all context we use. 
307      mark them as unused so we don't link one twice and break the list */
308   for (context_tmp = state->context; context_tmp; context_tmp = context_tmp->current)
309     {
310       context_tmp->netid.next = &context_tmp->netid;
311
312       if (option_bool(OPT_LOG_OPTS))
313         {
314            inet_ntop(AF_INET6, &context_tmp->start6, daemon->dhcp_buff, ADDRSTRLEN); 
315            inet_ntop(AF_INET6, &context_tmp->end6, daemon->dhcp_buff2, ADDRSTRLEN); 
316            if (context_tmp->flags & (CONTEXT_STATIC))
317              my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCPv6 subnet: %s/%d"),
318                        state->xid, daemon->dhcp_buff, context_tmp->prefix);
319            else
320              my_syslog(MS_DHCP | LOG_INFO, _("%u available DHCP range: %s -- %s"), 
321                        state->xid, daemon->dhcp_buff, daemon->dhcp_buff2);
322         }
323     }
324
325   if ((opt = opt6_find(state->packet_options, state->end, OPTION6_CLIENT_ID, 1)))
326     {
327       state->clid = opt6_ptr(opt, 0);
328       state->clid_len = opt6_len(opt);
329       o = new_opt6(OPTION6_CLIENT_ID);
330       put_opt6(state->clid, state->clid_len);
331       end_opt6(o);
332     }
333   else if (msg_type != DHCP6IREQ)
334     return 0;
335
336   /* server-id must match except for SOLICIT, CONFIRM and REBIND messages */
337   if (msg_type != DHCP6SOLICIT && msg_type != DHCP6CONFIRM && msg_type != DHCP6IREQ && msg_type != DHCP6REBIND &&
338       (!(opt = opt6_find(state->packet_options, state->end, OPTION6_SERVER_ID, 1)) ||
339        opt6_len(opt) != daemon->duid_len ||
340        memcmp(opt6_ptr(opt, 0), daemon->duid, daemon->duid_len) != 0))
341     return 0;
342   
343   o = new_opt6(OPTION6_SERVER_ID);
344   put_opt6(daemon->duid, daemon->duid_len);
345   end_opt6(o);
346
347   if (is_unicast &&
348       (msg_type == DHCP6REQUEST || msg_type == DHCP6RENEW || msg_type == DHCP6RELEASE || msg_type == DHCP6DECLINE))
349     
350     {  
351       outmsgtype = DHCP6REPLY;
352       o1 = new_opt6(OPTION6_STATUS_CODE);
353       put_opt6_short(DHCP6USEMULTI);
354       put_opt6_string("Use multicast");
355       end_opt6(o1);
356       return 1;
357     }
358
359   /* match vendor and user class options */
360   for (vendor = daemon->dhcp_vendors; vendor; vendor = vendor->next)
361     {
362       int mopt;
363       
364       if (vendor->match_type == MATCH_VENDOR)
365         mopt = OPTION6_VENDOR_CLASS;
366       else if (vendor->match_type == MATCH_USER)
367         mopt = OPTION6_USER_CLASS; 
368       else
369         continue;
370
371       if ((opt = opt6_find(state->packet_options, state->end, mopt, 2)))
372         {
373           void *enc_opt, *enc_end = opt6_ptr(opt, opt6_len(opt));
374           int offset = 0;
375           
376           if (mopt == OPTION6_VENDOR_CLASS)
377             {
378               if (opt6_len(opt) < 4)
379                 continue;
380               
381               if (vendor->enterprise != opt6_uint(opt, 0, 4))
382                 continue;
383             
384               offset = 4;
385             }
386  
387           /* Note that format if user/vendor classes is different to DHCP options - no option types. */
388           for (enc_opt = opt6_ptr(opt, offset); enc_opt; enc_opt = opt6_user_vendor_next(enc_opt, enc_end))
389             for (i = 0; i <= (opt6_user_vendor_len(enc_opt) - vendor->len); i++)
390               if (memcmp(vendor->data, opt6_user_vendor_ptr(enc_opt, i), vendor->len) == 0)
391                 {
392                   vendor->netid.next = state->tags;
393                   state->tags = &vendor->netid;
394                   break;
395                 }
396         }
397     }
398
399   if (option_bool(OPT_LOG_OPTS) && (opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_CLASS, 4)))
400     my_syslog(MS_DHCP | LOG_INFO, _("%u vendor class: %u"), state->xid, opt6_uint(opt, 0, 4));
401   
402   /* dhcp-match. If we have hex-and-wildcards, look for a left-anchored match.
403      Otherwise assume the option is an array, and look for a matching element. 
404      If no data given, existence of the option is enough. This code handles 
405      V-I opts too. */
406   for (opt_cfg = daemon->dhcp_match6; opt_cfg; opt_cfg = opt_cfg->next)
407     {
408       int match = 0;
409       
410       if (opt_cfg->flags & DHOPT_RFC3925)
411         {
412           for (opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_OPTS, 4);
413                opt;
414                opt = opt6_find(opt6_next(opt, state->end), state->end, OPTION6_VENDOR_OPTS, 4))
415             {
416               void *vopt;
417               void *vend = opt6_ptr(opt, opt6_len(opt));
418               
419               for (vopt = opt6_find(opt6_ptr(opt, 4), vend, opt_cfg->opt, 0);
420                    vopt;
421                    vopt = opt6_find(opt6_next(vopt, vend), vend, opt_cfg->opt, 0))
422                 if ((match = match_bytes(opt_cfg, opt6_ptr(vopt, 0), opt6_len(vopt))))
423                   break;
424             }
425           if (match)
426             break;
427         }
428       else
429         {
430           if (!(opt = opt6_find(state->packet_options, state->end, opt_cfg->opt, 1)))
431             continue;
432           
433           match = match_bytes(opt_cfg, opt6_ptr(opt, 0), opt6_len(opt));
434         } 
435   
436       if (match)
437         {
438           opt_cfg->netid->next = state->tags;
439           state->tags = opt_cfg->netid;
440         }
441     }
442
443   if (state->mac_len != 0)
444     {
445       if (option_bool(OPT_LOG_OPTS))
446         {
447           print_mac(daemon->dhcp_buff, state->mac, state->mac_len);
448           my_syslog(MS_DHCP | LOG_INFO, _("%u client MAC address: %s"), state->xid, daemon->dhcp_buff);
449         }
450
451       for (mac_opt = daemon->dhcp_macs; mac_opt; mac_opt = mac_opt->next)
452         if ((unsigned)mac_opt->hwaddr_len == state->mac_len &&
453             ((unsigned)mac_opt->hwaddr_type == state->mac_type || mac_opt->hwaddr_type == 0) &&
454             memcmp_masked(mac_opt->hwaddr, state->mac, state->mac_len, mac_opt->mask))
455           {
456             mac_opt->netid.next = state->tags;
457             state->tags = &mac_opt->netid;
458           }
459     }
460   
461   if ((opt = opt6_find(state->packet_options, state->end, OPTION6_FQDN, 1)))
462     {
463       /* RFC4704 refers */
464        int len = opt6_len(opt) - 1;
465        
466        state->fqdn_flags = opt6_uint(opt, 0, 1);
467        
468        /* Always force update, since the client has no way to do it itself. */
469        if (!option_bool(OPT_FQDN_UPDATE) && !(state->fqdn_flags & 0x01))
470          state->fqdn_flags |= 0x03;
471  
472        state->fqdn_flags &= ~0x04;
473
474        if (len != 0 && len < 255)
475          {
476            unsigned char *pp, *op = opt6_ptr(opt, 1);
477            char *pq = daemon->dhcp_buff;
478            
479            pp = op;
480            while (*op != 0 && ((op + (*op)) - pp) < len)
481              {
482                memcpy(pq, op+1, *op);
483                pq += *op;
484                op += (*op)+1;
485                *(pq++) = '.';
486              }
487            
488            if (pq != daemon->dhcp_buff)
489              pq--;
490            *pq = 0;
491            
492            if (legal_hostname(daemon->dhcp_buff))
493              {
494                struct dhcp_match_name *m;
495                size_t nl = strlen(daemon->dhcp_buff);
496                
497                state->client_hostname = daemon->dhcp_buff;
498                
499                if (option_bool(OPT_LOG_OPTS))
500                  my_syslog(MS_DHCP | LOG_INFO, _("%u client provides name: %s"), state->xid, state->client_hostname);
501                
502                for (m = daemon->dhcp_name_match; m; m = m->next)
503                  {
504                    size_t ml = strlen(m->name);
505                    char save = 0;
506                    
507                    if (nl < ml)
508                      continue;
509                    if (nl > ml)
510                      {
511                        save = state->client_hostname[ml];
512                        state->client_hostname[ml] = 0;
513                      }
514                    
515                    if (hostname_isequal(state->client_hostname, m->name) &&
516                        (save == 0 || m->wildcard))
517                      {
518                        m->netid->next = state->tags;
519                        state->tags = m->netid;
520                      }
521                    
522                    if (save != 0)
523                      state->client_hostname[ml] = save;
524                  }
525              }
526          }
527     }    
528   
529   if (state->clid &&
530       (config = find_config(daemon->dhcp_conf, state->context, state->clid, state->clid_len,
531                             state->mac, state->mac_len, state->mac_type, NULL, run_tag_if(state->tags))) &&
532       have_config(config, CONFIG_NAME))
533     {
534       state->hostname = config->hostname;
535       state->domain = config->domain;
536       state->hostname_auth = 1;
537     }
538   else if (state->client_hostname)
539     {
540       state->domain = strip_hostname(state->client_hostname);
541       
542       if (strlen(state->client_hostname) != 0)
543         {
544           state->hostname = state->client_hostname;
545           
546           if (!config)
547             {
548               /* Search again now we have a hostname. 
549                  Only accept configs without CLID here, (it won't match)
550                  to avoid impersonation by name. */
551               struct dhcp_config *new = find_config(daemon->dhcp_conf, state->context, NULL, 0, NULL, 0, 0, state->hostname, run_tag_if(state->tags));
552               if (new && !have_config(new, CONFIG_CLID) && !new->hwaddr)
553                 config = new;
554             }
555         }
556     }
557
558   if (config)
559     {
560       struct dhcp_netid_list *list;
561       
562       for (list = config->netid; list; list = list->next)
563         {
564           list->list->next = state->tags;
565           state->tags = list->list;
566         }
567
568       /* set "known" tag for known hosts */
569       known_id.net = "known";
570       known_id.next = state->tags;
571       state->tags = &known_id;
572
573       if (have_config(config, CONFIG_DISABLE))
574         ignore = 1;
575     }
576   else if (state->clid &&
577            find_config(daemon->dhcp_conf, NULL, state->clid, state->clid_len,
578                        state->mac, state->mac_len, state->mac_type, NULL, run_tag_if(state->tags)))
579     {
580       known_id.net = "known-othernet";
581       known_id.next = state->tags;
582       state->tags = &known_id;
583     }
584   
585   tagif = run_tag_if(state->tags);
586   
587   /* if all the netids in the ignore list are present, ignore this client */
588   if (daemon->dhcp_ignore)
589     {
590       struct dhcp_netid_list *id_list;
591      
592       for (id_list = daemon->dhcp_ignore; id_list; id_list = id_list->next)
593         if (match_netid(id_list->list, tagif, 0))
594           ignore = 1;
595     }
596   
597   /* if all the netids in the ignore_name list are present, ignore client-supplied name */
598   if (!state->hostname_auth)
599     {
600        struct dhcp_netid_list *id_list;
601        
602        for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
603          if ((!id_list->list) || match_netid(id_list->list, tagif, 0))
604            break;
605        if (id_list)
606          state->hostname = NULL;
607     }
608   
609
610   switch (msg_type)
611     {
612     default:
613       return 0;
614       
615       
616     case DHCP6SOLICIT:
617       {
618         int address_assigned = 0;
619         /* tags without all prefix-class tags */
620         struct dhcp_netid *solicit_tags;
621         struct dhcp_context *c;
622         
623         outmsgtype = DHCP6ADVERTISE;
624         
625         if (opt6_find(state->packet_options, state->end, OPTION6_RAPID_COMMIT, 0))
626           {
627             outmsgtype = DHCP6REPLY;
628             state->lease_allocate = 1;
629             o = new_opt6(OPTION6_RAPID_COMMIT);
630             end_opt6(o);
631           }
632         
633         log6_quiet(state, "DHCPSOLICIT", NULL, ignore ? _("ignored") : NULL);
634
635       request_no_address:
636         solicit_tags = tagif;
637         
638         if (ignore)
639           return 0;
640         
641         /* reset USED bits in leases */
642         lease6_reset();
643
644         /* Can use configured address max once per prefix */
645         for (c = state->context; c; c = c->current)
646           c->flags &= ~CONTEXT_CONF_USED;
647
648         for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
649           {   
650             void *ia_option, *ia_end;
651             unsigned int min_time = 0xffffffff;
652             int t1cntr;
653             int ia_counter;
654             /* set unless we're sending a particular prefix-class, when we
655                want only dhcp-ranges with the correct tags set and not those without any tags. */
656             int plain_range = 1;
657             u32 lease_time;
658             struct dhcp_lease *ltmp;
659             struct in6_addr req_addr, addr;
660             
661             if (!check_ia(state, opt, &ia_end, &ia_option))
662               continue;
663             
664             /* reset USED bits in contexts - one address per prefix per IAID */
665             for (c = state->context; c; c = c->current)
666               c->flags &= ~CONTEXT_USED;
667
668             o = build_ia(state, &t1cntr);
669             if (address_assigned)
670                 address_assigned = 2;
671
672             for (ia_counter = 0; ia_option; ia_counter++, ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
673               {
674                 /* worry about alignment here. */
675                 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
676                                 
677                 if ((c = address6_valid(state->context, &req_addr, solicit_tags, plain_range)))
678                   {
679                     lease_time = c->lease_time;
680                     /* If the client asks for an address on the same network as a configured address, 
681                        offer the configured address instead, to make moving to newly-configured
682                        addresses automatic. */
683                     if (!(c->flags & CONTEXT_CONF_USED) && config_valid(config, c, &addr, state, now))
684                       {
685                         req_addr = addr;
686                         mark_config_used(c, &addr);
687                         if (have_config(config, CONFIG_TIME))
688                           lease_time = config->lease_time;
689                       }
690                     else if (!(c = address6_available(state->context, &req_addr, solicit_tags, plain_range)))
691                       continue; /* not an address we're allowed */
692                     else if (!check_address(state, &req_addr))
693                       continue; /* address leased elsewhere */
694                     
695                     /* add address to output packet */
696                     add_address(state, c, lease_time, ia_option, &min_time, &req_addr, now);
697                     mark_context_used(state, &req_addr);
698                     get_context_tag(state, c);
699                     address_assigned = 1;
700                   }
701               }
702             
703             /* Suggest configured address(es) */
704             for (c = state->context; c; c = c->current) 
705               if (!(c->flags & CONTEXT_CONF_USED) &&
706                   match_netid(c->filter, solicit_tags, plain_range) &&
707                   config_valid(config, c, &addr, state, now))
708                 {
709                   mark_config_used(state->context, &addr);
710                   if (have_config(config, CONFIG_TIME))
711                     lease_time = config->lease_time;
712                   else
713                     lease_time = c->lease_time;
714
715                   /* add address to output packet */
716                   add_address(state, c, lease_time, NULL, &min_time, &addr, now);
717                   mark_context_used(state, &addr);
718                   get_context_tag(state, c);
719                   address_assigned = 1;
720                 }
721             
722             /* return addresses for existing leases */
723             ltmp = NULL;
724             while ((ltmp = lease6_find_by_client(ltmp, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA, state->clid, state->clid_len, state->iaid)))
725               {
726                 req_addr = ltmp->addr6;
727                 if ((c = address6_available(state->context, &req_addr, solicit_tags, plain_range)))
728                   {
729                     add_address(state, c, c->lease_time, NULL, &min_time, &req_addr, now);
730                     mark_context_used(state, &req_addr);
731                     get_context_tag(state, c);
732                     address_assigned = 1;
733                   }
734               }
735                            
736             /* Return addresses for all valid contexts which don't yet have one */
737             while ((c = address6_allocate(state->context, state->clid, state->clid_len, state->ia_type == OPTION6_IA_TA,
738                                           state->iaid, ia_counter, solicit_tags, plain_range, &addr)))
739               {
740                 add_address(state, c, c->lease_time, NULL, &min_time, &addr, now);
741                 mark_context_used(state, &addr);
742                 get_context_tag(state, c);
743                 address_assigned = 1;
744               }
745             
746             if (address_assigned != 1)
747               {
748                 /* If the server will not assign any addresses to any IAs in a
749                    subsequent Request from the client, the server MUST send an Advertise
750                    message to the client that doesn't include any IA options. */
751                 if (!state->lease_allocate)
752                   {
753                     save_counter(o);
754                     continue;
755                   }
756                 
757                 /* If the server cannot assign any addresses to an IA in the message
758                    from the client, the server MUST include the IA in the Reply message
759                    with no addresses in the IA and a Status Code option in the IA
760                    containing status code NoAddrsAvail. */
761                 o1 = new_opt6(OPTION6_STATUS_CODE);
762                 put_opt6_short(DHCP6NOADDRS);
763                 put_opt6_string(_("address unavailable"));
764                 end_opt6(o1);
765               }
766             
767             end_ia(t1cntr, min_time, 0);
768             end_opt6(o);        
769           }
770
771         if (address_assigned) 
772           {
773             o1 = new_opt6(OPTION6_STATUS_CODE);
774             put_opt6_short(DHCP6SUCCESS);
775             put_opt6_string(_("success"));
776             end_opt6(o1);
777             
778             /* If --dhcp-authoritative is set, we can tell client not to wait for
779                other possible servers */
780             o = new_opt6(OPTION6_PREFERENCE);
781             put_opt6_char(option_bool(OPT_AUTHORITATIVE) ? 255 : 0);
782             end_opt6(o);
783             tagif = add_options(state, 0);
784           }
785         else
786           { 
787             /* no address, return error */
788             o1 = new_opt6(OPTION6_STATUS_CODE);
789             put_opt6_short(DHCP6NOADDRS);
790             put_opt6_string(_("no addresses available"));
791             end_opt6(o1);
792
793             /* Some clients will ask repeatedly when we're not giving
794                out addresses because we're in stateless mode. Avoid spamming
795                the log in that case. */
796             for (c = state->context; c; c = c->current)
797               if (!(c->flags & CONTEXT_RA_STATELESS))
798                 {
799                   log6_packet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", NULL, _("no addresses available"));
800                   break;
801                 }
802           }
803
804         break;
805       }
806       
807     case DHCP6REQUEST:
808       {
809         int address_assigned = 0;
810         int start = save_counter(-1);
811
812         /* set reply message type */
813         outmsgtype = DHCP6REPLY;
814         state->lease_allocate = 1;
815
816         log6_quiet(state, "DHCPREQUEST", NULL, ignore ? _("ignored") : NULL);
817         
818         if (ignore)
819           return 0;
820         
821         for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
822           {   
823             void *ia_option, *ia_end;
824             unsigned int min_time = 0xffffffff;
825             int t1cntr;
826             
827              if (!check_ia(state, opt, &ia_end, &ia_option))
828                continue;
829
830              if (!ia_option)
831                {
832                  /* If we get a request with an IA_*A without addresses, treat it exactly like
833                     a SOLICT with rapid commit set. */
834                  save_counter(start);
835                  goto request_no_address; 
836                }
837
838             o = build_ia(state, &t1cntr);
839               
840             for (; ia_option; ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
841               {
842                 struct in6_addr req_addr;
843                 struct dhcp_context *dynamic, *c;
844                 unsigned int lease_time;
845                 int config_ok = 0;
846
847                 /* align. */
848                 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
849                 
850                 if ((c = address6_valid(state->context, &req_addr, tagif, 1)))
851                   config_ok = (config_implies(config, c, &req_addr) != NULL);
852                 
853                 if ((dynamic = address6_available(state->context, &req_addr, tagif, 1)) || c)
854                   {
855                     if (!dynamic && !config_ok)
856                       {
857                         /* Static range, not configured. */
858                         o1 = new_opt6(OPTION6_STATUS_CODE);
859                         put_opt6_short(DHCP6NOADDRS);
860                         put_opt6_string(_("address unavailable"));
861                         end_opt6(o1);
862                       }
863                     else if (!check_address(state, &req_addr))
864                       {
865                         /* Address leased to another DUID/IAID */
866                         o1 = new_opt6(OPTION6_STATUS_CODE);
867                         put_opt6_short(DHCP6UNSPEC);
868                         put_opt6_string(_("address in use"));
869                         end_opt6(o1);
870                       } 
871                     else 
872                       {
873                         if (!dynamic)
874                           dynamic = c;
875
876                         lease_time = dynamic->lease_time;
877                         
878                         if (config_ok && have_config(config, CONFIG_TIME))
879                           lease_time = config->lease_time;
880
881                         add_address(state, dynamic, lease_time, ia_option, &min_time, &req_addr, now);
882                         get_context_tag(state, dynamic);
883                         address_assigned = 1;
884                       }
885                   }
886                 else 
887                   {
888                     /* requested address not on the correct link */
889                     o1 = new_opt6(OPTION6_STATUS_CODE);
890                     put_opt6_short(DHCP6NOTONLINK);
891                     put_opt6_string(_("not on link"));
892                     end_opt6(o1);
893                   }
894               }
895          
896             end_ia(t1cntr, min_time, 0);
897             end_opt6(o);        
898           }
899
900         if (address_assigned) 
901           {
902             o1 = new_opt6(OPTION6_STATUS_CODE);
903             put_opt6_short(DHCP6SUCCESS);
904             put_opt6_string(_("success"));
905             end_opt6(o1);
906           }
907         else
908           { 
909             /* no address, return error */
910             o1 = new_opt6(OPTION6_STATUS_CODE);
911             put_opt6_short(DHCP6NOADDRS);
912             put_opt6_string(_("no addresses available"));
913             end_opt6(o1);
914             log6_packet(state, "DHCPREPLY", NULL, _("no addresses available"));
915           }
916
917         tagif = add_options(state, 0);
918         break;
919       }
920       
921   
922     case DHCP6RENEW:
923     case DHCP6REBIND:
924       {
925         int address_assigned = 0;
926
927         /* set reply message type */
928         outmsgtype = DHCP6REPLY;
929         
930         log6_quiet(state, msg_type == DHCP6RENEW ? "DHCPRENEW" : "DHCPREBIND", NULL, NULL);
931
932         for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
933           {
934             void *ia_option, *ia_end;
935             unsigned int min_time = 0xffffffff;
936             int t1cntr, iacntr;
937             
938             if (!check_ia(state, opt, &ia_end, &ia_option))
939               continue;
940             
941             o = build_ia(state, &t1cntr);
942             iacntr = save_counter(-1); 
943             
944             for (; ia_option; ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
945               {
946                 struct dhcp_lease *lease = NULL;
947                 struct in6_addr req_addr;
948                 unsigned int preferred_time =  opt6_uint(ia_option, 16, 4);
949                 unsigned int valid_time =  opt6_uint(ia_option, 20, 4);
950                 char *message = NULL;
951                 struct dhcp_context *this_context;
952
953                 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ); 
954                 
955                 if (!(lease = lease6_find(state->clid, state->clid_len,
956                                           state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA, 
957                                           state->iaid, &req_addr)))
958                   {
959                     if (msg_type == DHCP6REBIND)
960                       {
961                         /* When rebinding, we can create a lease if it doesn't exist. */
962                         lease = lease6_allocate(&req_addr, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA);
963                         if (lease)
964                           lease_set_iaid(lease, state->iaid);
965                         else
966                           break;
967                       }
968                     else
969                       {
970                         /* If the server cannot find a client entry for the IA the server
971                            returns the IA containing no addresses with a Status Code option set
972                            to NoBinding in the Reply message. */
973                         save_counter(iacntr);
974                         t1cntr = 0;
975                         
976                         log6_packet(state, "DHCPREPLY", &req_addr, _("lease not found"));
977                         
978                         o1 = new_opt6(OPTION6_STATUS_CODE);
979                         put_opt6_short(DHCP6NOBINDING);
980                         put_opt6_string(_("no binding found"));
981                         end_opt6(o1);
982                         
983                         preferred_time = valid_time = 0;
984                         break;
985                       }
986                   }
987                 
988                 if ((this_context = address6_available(state->context, &req_addr, tagif, 1)) ||
989                     (this_context = address6_valid(state->context, &req_addr, tagif, 1)))
990                   {
991                     unsigned int lease_time;
992
993                     get_context_tag(state, this_context);
994                     
995                     if (config_implies(config, this_context, &req_addr) && have_config(config, CONFIG_TIME))
996                       lease_time = config->lease_time;
997                     else 
998                       lease_time = this_context->lease_time;
999                     
1000                     calculate_times(this_context, &min_time, &valid_time, &preferred_time, lease_time); 
1001                     
1002                     lease_set_expires(lease, valid_time, now);
1003                     /* Update MAC record in case it's new information. */
1004                     if (state->mac_len != 0)
1005                       lease_set_hwaddr(lease, state->mac, state->clid, state->mac_len, state->mac_type, state->clid_len, now, 0);
1006                     if (state->ia_type == OPTION6_IA_NA && state->hostname)
1007                       {
1008                         char *addr_domain = get_domain6(&req_addr);
1009                         if (!state->send_domain)
1010                           state->send_domain = addr_domain;
1011                         lease_set_hostname(lease, state->hostname, state->hostname_auth, addr_domain, state->domain); 
1012                         message = state->hostname;
1013                       }
1014                     
1015                     
1016                     if (preferred_time == 0)
1017                       message = _("deprecated");
1018
1019                     address_assigned = 1;
1020                   }
1021                 else
1022                   {
1023                     preferred_time = valid_time = 0;
1024                     message = _("address invalid");
1025                   } 
1026
1027                 if (message && (message != state->hostname))
1028                   log6_packet(state, "DHCPREPLY", &req_addr, message);  
1029                 else
1030                   log6_quiet(state, "DHCPREPLY", &req_addr, message);
1031         
1032                 o1 =  new_opt6(OPTION6_IAADDR);
1033                 put_opt6(&req_addr, sizeof(req_addr));
1034                 put_opt6_long(preferred_time);
1035                 put_opt6_long(valid_time);
1036                 end_opt6(o1);
1037               }
1038             
1039             end_ia(t1cntr, min_time, 1);
1040             end_opt6(o);
1041           }
1042
1043         if (!address_assigned && msg_type == DHCP6REBIND)
1044           { 
1045             /* can't create lease for any address, return error */
1046             o1 = new_opt6(OPTION6_STATUS_CODE);
1047             put_opt6_short(DHCP6NOADDRS);
1048             put_opt6_string(_("no addresses available"));
1049             end_opt6(o1);
1050           }
1051         
1052         tagif = add_options(state, 0);
1053         break;
1054       }
1055       
1056     case DHCP6CONFIRM:
1057       {
1058         int good_addr = 0;
1059
1060         /* set reply message type */
1061         outmsgtype = DHCP6REPLY;
1062         
1063         log6_quiet(state, "DHCPCONFIRM", NULL, NULL);
1064         
1065         for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
1066           {
1067             void *ia_option, *ia_end;
1068             
1069             for (check_ia(state, opt, &ia_end, &ia_option);
1070                  ia_option;
1071                  ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24))
1072               {
1073                 struct in6_addr req_addr;
1074
1075                 /* alignment */
1076                 memcpy(&req_addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
1077                 
1078                 if (!address6_valid(state->context, &req_addr, tagif, 1))
1079                   {
1080                     o1 = new_opt6(OPTION6_STATUS_CODE);
1081                     put_opt6_short(DHCP6NOTONLINK);
1082                     put_opt6_string(_("confirm failed"));
1083                     end_opt6(o1);
1084                     log6_quiet(state, "DHCPREPLY", &req_addr, _("confirm failed"));
1085                     return 1;
1086                   }
1087
1088                 good_addr = 1;
1089                 log6_quiet(state, "DHCPREPLY", &req_addr, state->hostname);
1090               }
1091           }      
1092         
1093         /* No addresses, no reply: RFC 3315 18.2.2 */
1094         if (!good_addr)
1095           return 0;
1096
1097         o1 = new_opt6(OPTION6_STATUS_CODE);
1098         put_opt6_short(DHCP6SUCCESS );
1099         put_opt6_string(_("all addresses still on link"));
1100         end_opt6(o1);
1101         break;
1102     }
1103       
1104     case DHCP6IREQ:
1105       {
1106         /* We can't discriminate contexts based on address, as we don't know it.
1107            If there is only one possible context, we can use its tags */
1108         if (state->context && state->context->netid.net && !state->context->current)
1109           {
1110             state->context->netid.next = NULL;
1111             state->context_tags =  &state->context->netid;
1112           }
1113
1114         /* Similarly, we can't determine domain from address, but if the FQDN is
1115            given in --dhcp-host, we can use that, and failing that we can use the 
1116            unqualified configured domain, if any. */
1117         if (state->hostname_auth)
1118           state->send_domain = state->domain;
1119         else
1120           state->send_domain = get_domain6(NULL);
1121
1122         log6_quiet(state, "DHCPINFORMATION-REQUEST", NULL, ignore ? _("ignored") : state->hostname);
1123         if (ignore)
1124           return 0;
1125         outmsgtype = DHCP6REPLY;
1126         tagif = add_options(state, 1);
1127         break;
1128       }
1129       
1130       
1131     case DHCP6RELEASE:
1132       {
1133         /* set reply message type */
1134         outmsgtype = DHCP6REPLY;
1135
1136         log6_quiet(state, "DHCPRELEASE", NULL, NULL);
1137
1138         for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
1139           {
1140             void *ia_option, *ia_end;
1141             int made_ia = 0;
1142                     
1143             for (check_ia(state, opt, &ia_end, &ia_option);
1144                  ia_option;
1145                  ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 
1146               {
1147                 struct dhcp_lease *lease;
1148                 struct in6_addr addr;
1149
1150                 /* align */
1151                 memcpy(&addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
1152                 if ((lease = lease6_find(state->clid, state->clid_len, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA,
1153                                          state->iaid, &addr)))
1154                   lease_prune(lease, now);
1155                 else
1156                   {
1157                     if (!made_ia)
1158                       {
1159                         o = new_opt6(state->ia_type);
1160                         put_opt6_long(state->iaid);
1161                         if (state->ia_type == OPTION6_IA_NA)
1162                           {
1163                             put_opt6_long(0);
1164                             put_opt6_long(0); 
1165                           }
1166                         made_ia = 1;
1167                       }
1168                     
1169                     o1 = new_opt6(OPTION6_IAADDR);
1170                     put_opt6(&addr, IN6ADDRSZ);
1171                     put_opt6_long(0);
1172                     put_opt6_long(0);
1173                     end_opt6(o1);
1174                   }
1175               }
1176             
1177             if (made_ia)
1178               {
1179                 o1 = new_opt6(OPTION6_STATUS_CODE);
1180                 put_opt6_short(DHCP6NOBINDING);
1181                 put_opt6_string(_("no binding found"));
1182                 end_opt6(o1);
1183                 
1184                 end_opt6(o);
1185               }
1186           }
1187         
1188         o1 = new_opt6(OPTION6_STATUS_CODE);
1189         put_opt6_short(DHCP6SUCCESS);
1190         put_opt6_string(_("release received"));
1191         end_opt6(o1);
1192         
1193         break;
1194       }
1195
1196     case DHCP6DECLINE:
1197       {
1198         /* set reply message type */
1199         outmsgtype = DHCP6REPLY;
1200         
1201         log6_quiet(state, "DHCPDECLINE", NULL, NULL);
1202
1203         for (opt = state->packet_options; opt; opt = opt6_next(opt, state->end))
1204           {
1205             void *ia_option, *ia_end;
1206             int made_ia = 0;
1207                     
1208             for (check_ia(state, opt, &ia_end, &ia_option);
1209                  ia_option;
1210                  ia_option = opt6_find(opt6_next(ia_option, ia_end), ia_end, OPTION6_IAADDR, 24)) 
1211               {
1212                 struct dhcp_lease *lease;
1213                 struct in6_addr addr;
1214                 struct addrlist *addr_list;
1215                 
1216                 /* align */
1217                 memcpy(&addr, opt6_ptr(ia_option, 0), IN6ADDRSZ);
1218
1219                 if ((addr_list = config_implies(config, state->context, &addr)))
1220                   {
1221                     prettyprint_time(daemon->dhcp_buff3, DECLINE_BACKOFF);
1222                     inet_ntop(AF_INET6, &addr, daemon->addrbuff, ADDRSTRLEN);
1223                     my_syslog(MS_DHCP | LOG_WARNING, _("disabling DHCP static address %s for %s"), 
1224                               daemon->addrbuff, daemon->dhcp_buff3);
1225                     addr_list->flags |= ADDRLIST_DECLINED;
1226                     addr_list->decline_time = now;
1227                   }
1228                 else
1229                   /* make sure this host gets a different address next time. */
1230                   for (context_tmp = state->context; context_tmp; context_tmp = context_tmp->current)
1231                     context_tmp->addr_epoch++;
1232                 
1233                 if ((lease = lease6_find(state->clid, state->clid_len, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA,
1234                                          state->iaid, &addr)))
1235                   lease_prune(lease, now);
1236                 else
1237                   {
1238                     if (!made_ia)
1239                       {
1240                         o = new_opt6(state->ia_type);
1241                         put_opt6_long(state->iaid);
1242                         if (state->ia_type == OPTION6_IA_NA)
1243                           {
1244                             put_opt6_long(0);
1245                             put_opt6_long(0); 
1246                           }
1247                         made_ia = 1;
1248                       }
1249                     
1250                     o1 = new_opt6(OPTION6_IAADDR);
1251                     put_opt6(&addr, IN6ADDRSZ);
1252                     put_opt6_long(0);
1253                     put_opt6_long(0);
1254                     end_opt6(o1);
1255                   }
1256               }
1257             
1258             if (made_ia)
1259               {
1260                 o1 = new_opt6(OPTION6_STATUS_CODE);
1261                 put_opt6_short(DHCP6NOBINDING);
1262                 put_opt6_string(_("no binding found"));
1263                 end_opt6(o1);
1264                 
1265                 end_opt6(o);
1266               }
1267             
1268           }
1269
1270         /* We must answer with 'success' in global section anyway */
1271         o1 = new_opt6(OPTION6_STATUS_CODE);
1272         put_opt6_short(DHCP6SUCCESS);
1273         put_opt6_string(_("success"));
1274         end_opt6(o1);
1275         break;
1276       }
1277
1278     }
1279
1280   /* Fill in the message type. Note that we store the offset,
1281      not a direct pointer, since the packet memory may have been 
1282      reallocated. */
1283   ((unsigned char *)(daemon->outpacket.iov_base))[start_msg] = outmsgtype;
1284
1285   log_tags(tagif, state->xid);
1286   log6_opts(0, state->xid, daemon->outpacket.iov_base + start_opts, daemon->outpacket.iov_base + save_counter(-1));
1287   
1288   return 1;
1289
1290 }
1291
1292 static struct dhcp_netid *add_options(struct state *state, int do_refresh)  
1293 {
1294   void *oro;
1295   /* filter options based on tags, those we want get DHOPT_TAGOK bit set */
1296   struct dhcp_netid *tagif = option_filter(state->tags, state->context_tags, daemon->dhcp_opts6);
1297   struct dhcp_opt *opt_cfg;
1298   int done_dns = 0, done_refresh = !do_refresh, do_encap = 0;
1299   int i, o, o1;
1300
1301   oro = opt6_find(state->packet_options, state->end, OPTION6_ORO, 0);
1302   
1303   for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
1304     {
1305       /* netids match and not encapsulated? */
1306       if (!(opt_cfg->flags & DHOPT_TAGOK))
1307         continue;
1308       
1309       if (!(opt_cfg->flags & DHOPT_FORCE) && oro)
1310         {
1311           for (i = 0; i <  opt6_len(oro) - 1; i += 2)
1312             if (opt6_uint(oro, i, 2) == (unsigned)opt_cfg->opt)
1313               break;
1314           
1315           /* option not requested */
1316           if (i >=  opt6_len(oro) - 1)
1317             continue;
1318         }
1319       
1320       if (opt_cfg->opt == OPTION6_REFRESH_TIME)
1321         done_refresh = 1;
1322        
1323       if (opt_cfg->opt == OPTION6_DNS_SERVER)
1324         done_dns = 1;
1325       
1326       if (opt_cfg->flags & DHOPT_ADDR6)
1327         {
1328           int len, j;
1329           struct in6_addr *a;
1330           
1331           for (a = (struct in6_addr *)opt_cfg->val, len = opt_cfg->len, j = 0; 
1332                j < opt_cfg->len; j += IN6ADDRSZ, a++)
1333             if ((IN6_IS_ADDR_ULA_ZERO(a) && IN6_IS_ADDR_UNSPECIFIED(state->ula_addr)) ||
1334                 (IN6_IS_ADDR_LINK_LOCAL_ZERO(a) && IN6_IS_ADDR_UNSPECIFIED(state->ll_addr)))
1335               len -= IN6ADDRSZ;
1336           
1337           if (len != 0)
1338             {
1339               
1340               o = new_opt6(opt_cfg->opt);
1341                   
1342               for (a = (struct in6_addr *)opt_cfg->val, j = 0; j < opt_cfg->len; j+=IN6ADDRSZ, a++)
1343                 {
1344                   struct in6_addr *p = NULL;
1345
1346                   if (IN6_IS_ADDR_UNSPECIFIED(a))
1347                     {
1348                       if (!add_local_addrs(state->context))
1349                         p = state->fallback;
1350                     }
1351                   else if (IN6_IS_ADDR_ULA_ZERO(a))
1352                     {
1353                       if (!IN6_IS_ADDR_UNSPECIFIED(state->ula_addr))
1354                         p = state->ula_addr;
1355                     }
1356                   else if (IN6_IS_ADDR_LINK_LOCAL_ZERO(a))
1357                     {
1358                       if (!IN6_IS_ADDR_UNSPECIFIED(state->ll_addr))
1359                         p = state->ll_addr;
1360                     }
1361                   else
1362                     p = a;
1363
1364                   if (!p)
1365                     continue;
1366                   else if (opt_cfg->opt == OPTION6_NTP_SERVER)
1367                     {
1368                       if (IN6_IS_ADDR_MULTICAST(p))
1369                         o1 = new_opt6(NTP_SUBOPTION_MC_ADDR);
1370                       else
1371                         o1 = new_opt6(NTP_SUBOPTION_SRV_ADDR);
1372                       put_opt6(p, IN6ADDRSZ);
1373                       end_opt6(o1);
1374                     }
1375                   else
1376                     put_opt6(p, IN6ADDRSZ);
1377                 }
1378
1379               end_opt6(o);
1380             }
1381         }
1382       else
1383         {
1384           o = new_opt6(opt_cfg->opt);
1385           if (opt_cfg->val)
1386             put_opt6(opt_cfg->val, opt_cfg->len);
1387           end_opt6(o);
1388         }
1389     }
1390   
1391   if (daemon->port == NAMESERVER_PORT && !done_dns)
1392     {
1393       o = new_opt6(OPTION6_DNS_SERVER);
1394       if (!add_local_addrs(state->context))
1395         put_opt6(state->fallback, IN6ADDRSZ);
1396       end_opt6(o); 
1397     }
1398
1399   if (state->context && !done_refresh)
1400     {
1401       struct dhcp_context *c;
1402       unsigned int lease_time = 0xffffffff;
1403       
1404       /* Find the smallest lease tie of all contexts,
1405          subject to the RFC-4242 stipulation that this must not 
1406          be less than 600. */
1407       for (c = state->context; c; c = c->next)
1408         if (c->lease_time < lease_time)
1409           {
1410             if (c->lease_time < 600)
1411               lease_time = 600;
1412             else
1413               lease_time = c->lease_time;
1414           }
1415
1416       o = new_opt6(OPTION6_REFRESH_TIME);
1417       put_opt6_long(lease_time);
1418       end_opt6(o); 
1419     }
1420    
1421     /* handle vendor-identifying vendor-encapsulated options,
1422        dhcp-option = vi-encap:13,17,....... */
1423   for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
1424     opt_cfg->flags &= ~DHOPT_ENCAP_DONE;
1425     
1426   if (oro)
1427     for (i = 0; i <  opt6_len(oro) - 1; i += 2)
1428       if (opt6_uint(oro, i, 2) == OPTION6_VENDOR_OPTS)
1429         do_encap = 1;
1430   
1431   for (opt_cfg = daemon->dhcp_opts6; opt_cfg; opt_cfg = opt_cfg->next)
1432     { 
1433       if (opt_cfg->flags & DHOPT_RFC3925)
1434         {
1435           int found = 0;
1436           struct dhcp_opt *oc;
1437           
1438           if (opt_cfg->flags & DHOPT_ENCAP_DONE)
1439             continue;
1440           
1441           for (oc = daemon->dhcp_opts6; oc; oc = oc->next)
1442             {
1443               oc->flags &= ~DHOPT_ENCAP_MATCH;
1444               
1445               if (!(oc->flags & DHOPT_RFC3925) || opt_cfg->u.encap != oc->u.encap)
1446                 continue;
1447               
1448               oc->flags |= DHOPT_ENCAP_DONE;
1449               if (match_netid(oc->netid, tagif, 1))
1450                 {
1451                   /* option requested/forced? */
1452                   if (!oro || do_encap || (oc->flags & DHOPT_FORCE))
1453                     {
1454                       oc->flags |= DHOPT_ENCAP_MATCH;
1455                       found = 1;
1456                     }
1457                 } 
1458             }
1459           
1460           if (found)
1461             { 
1462               o = new_opt6(OPTION6_VENDOR_OPTS);              
1463               put_opt6_long(opt_cfg->u.encap);  
1464              
1465               for (oc = daemon->dhcp_opts6; oc; oc = oc->next)
1466                 if (oc->flags & DHOPT_ENCAP_MATCH)
1467                   {
1468                     o1 = new_opt6(oc->opt);
1469                     put_opt6(oc->val, oc->len);
1470                     end_opt6(o1);
1471                   }
1472               end_opt6(o);
1473             }
1474         }
1475     }      
1476
1477
1478   if (state->hostname)
1479     {
1480       unsigned char *p;
1481       size_t len = strlen(state->hostname);
1482       
1483       if (state->send_domain)
1484         len += strlen(state->send_domain) + 2;
1485
1486       o = new_opt6(OPTION6_FQDN);
1487       if ((p = expand(len + 2)))
1488         {
1489           *(p++) = state->fqdn_flags;
1490           p = do_rfc1035_name(p, state->hostname, NULL);
1491           if (state->send_domain)
1492             {
1493               p = do_rfc1035_name(p, state->send_domain, NULL);
1494               *p = 0;
1495             }
1496         }
1497       end_opt6(o);
1498     }
1499
1500
1501   /* logging */
1502   if (option_bool(OPT_LOG_OPTS) && oro)
1503     {
1504       char *q = daemon->namebuff;
1505       for (i = 0; i <  opt6_len(oro) - 1; i += 2)
1506         {
1507           char *s = option_string(AF_INET6, opt6_uint(oro, i, 2), NULL, 0, NULL, 0);
1508           q += snprintf(q, MAXDNAME - (q - daemon->namebuff),
1509                         "%d%s%s%s", 
1510                         opt6_uint(oro, i, 2),
1511                         strlen(s) != 0 ? ":" : "",
1512                         s, 
1513                         (i > opt6_len(oro) - 3) ? "" : ", ");
1514           if ( i >  opt6_len(oro) - 3 || (q - daemon->namebuff) > 40)
1515             {
1516               q = daemon->namebuff;
1517               my_syslog(MS_DHCP | LOG_INFO, _("%u requested options: %s"), state->xid, daemon->namebuff);
1518             }
1519         }
1520     } 
1521
1522   return tagif;
1523 }
1524  
1525 static int add_local_addrs(struct dhcp_context *context)
1526 {
1527   int done = 0;
1528   
1529   for (; context; context = context->current)
1530     if ((context->flags & CONTEXT_USED) && !IN6_IS_ADDR_UNSPECIFIED(&context->local6))
1531       {
1532         /* squash duplicates */
1533         struct dhcp_context *c;
1534         for (c = context->current; c; c = c->current)
1535           if ((c->flags & CONTEXT_USED) &&
1536               IN6_ARE_ADDR_EQUAL(&context->local6, &c->local6))
1537             break;
1538         
1539         if (!c)
1540           { 
1541             done = 1;
1542             put_opt6(&context->local6, IN6ADDRSZ);
1543           }
1544       }
1545
1546   return done;
1547 }
1548
1549
1550 static void get_context_tag(struct state *state, struct dhcp_context *context)
1551 {
1552   /* get tags from context if we've not used it before */
1553   if (context->netid.next == &context->netid && context->netid.net)
1554     {
1555       context->netid.next = state->context_tags;
1556       state->context_tags = &context->netid;
1557       if (!state->hostname_auth)
1558         {
1559           struct dhcp_netid_list *id_list;
1560           
1561           for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
1562             if ((!id_list->list) || match_netid(id_list->list, &context->netid, 0))
1563               break;
1564           if (id_list)
1565             state->hostname = NULL;
1566         }
1567     }
1568
1569
1570 static int check_ia(struct state *state, void *opt, void **endp, void **ia_option)
1571 {
1572   state->ia_type = opt6_type(opt);
1573   *ia_option = NULL;
1574
1575   if (state->ia_type != OPTION6_IA_NA && state->ia_type != OPTION6_IA_TA)
1576     return 0;
1577   
1578   if (state->ia_type == OPTION6_IA_NA && opt6_len(opt) < 12)
1579     return 0;
1580             
1581   if (state->ia_type == OPTION6_IA_TA && opt6_len(opt) < 4)
1582     return 0;
1583   
1584   *endp = opt6_ptr(opt, opt6_len(opt));
1585   state->iaid = opt6_uint(opt, 0, 4);
1586   *ia_option = opt6_find(opt6_ptr(opt, state->ia_type == OPTION6_IA_NA ? 12 : 4), *endp, OPTION6_IAADDR, 24);
1587
1588   return 1;
1589 }
1590
1591
1592 static int build_ia(struct state *state, int *t1cntr)
1593 {
1594   int  o = new_opt6(state->ia_type);
1595  
1596   put_opt6_long(state->iaid);
1597   *t1cntr = 0;
1598             
1599   if (state->ia_type == OPTION6_IA_NA)
1600     {
1601       /* save pointer */
1602       *t1cntr = save_counter(-1);
1603       /* so we can fill these in later */
1604       put_opt6_long(0);
1605       put_opt6_long(0); 
1606     }
1607
1608   return o;
1609 }
1610
1611 static void end_ia(int t1cntr, unsigned int min_time, int do_fuzz)
1612 {
1613   if (t1cntr != 0)
1614     {
1615       /* go back and fill in fields in IA_NA option */
1616       int sav = save_counter(t1cntr);
1617       unsigned int t1, t2, fuzz = 0;
1618
1619       if (do_fuzz)
1620         {
1621           fuzz = rand16();
1622       
1623           while (fuzz > (min_time/16))
1624             fuzz = fuzz/2;
1625         }
1626       
1627       t1 = (min_time == 0xffffffff) ? 0xffffffff : min_time/2 - fuzz;
1628       t2 = (min_time == 0xffffffff) ? 0xffffffff : ((min_time/8)*7) - fuzz;
1629       put_opt6_long(t1);
1630       put_opt6_long(t2);
1631       save_counter(sav);
1632     }   
1633 }
1634
1635 static void add_address(struct state *state, struct dhcp_context *context, unsigned int lease_time, void *ia_option, 
1636                         unsigned int *min_time, struct in6_addr *addr, time_t now)
1637 {
1638   unsigned int valid_time = 0, preferred_time = 0;
1639   int o = new_opt6(OPTION6_IAADDR);
1640   struct dhcp_lease *lease;
1641
1642   /* get client requested times */
1643   if (ia_option)
1644     {
1645       preferred_time =  opt6_uint(ia_option, 16, 4);
1646       valid_time =  opt6_uint(ia_option, 20, 4);
1647     }
1648
1649   calculate_times(context, min_time, &valid_time, &preferred_time, lease_time); 
1650   
1651   put_opt6(addr, sizeof(*addr));
1652   put_opt6_long(preferred_time);
1653   put_opt6_long(valid_time);                
1654   end_opt6(o);
1655   
1656   if (state->lease_allocate)
1657     update_leases(state, context, addr, valid_time, now);
1658
1659   if ((lease = lease6_find_by_addr(addr, 128, 0)))
1660     lease->flags |= LEASE_USED;
1661
1662   /* get tags from context if we've not used it before */
1663   if (context->netid.next == &context->netid && context->netid.net)
1664     {
1665       context->netid.next = state->context_tags;
1666       state->context_tags = &context->netid;
1667       
1668       if (!state->hostname_auth)
1669         {
1670           struct dhcp_netid_list *id_list;
1671           
1672           for (id_list = daemon->dhcp_ignore_names; id_list; id_list = id_list->next)
1673             if ((!id_list->list) || match_netid(id_list->list, &context->netid, 0))
1674               break;
1675           if (id_list)
1676             state->hostname = NULL;
1677         }
1678     }
1679
1680   log6_quiet(state, state->lease_allocate ? "DHCPREPLY" : "DHCPADVERTISE", addr, state->hostname);
1681
1682 }
1683
1684 static void mark_context_used(struct state *state, struct in6_addr *addr)
1685 {
1686   struct dhcp_context *context;
1687
1688   /* Mark that we have an address for this prefix. */
1689   for (context = state->context; context; context = context->current)
1690     if (is_same_net6(addr, &context->start6, context->prefix))
1691       context->flags |= CONTEXT_USED;
1692 }
1693
1694 static void mark_config_used(struct dhcp_context *context, struct in6_addr *addr)
1695 {
1696   for (; context; context = context->current)
1697     if (is_same_net6(addr, &context->start6, context->prefix))
1698       context->flags |= CONTEXT_CONF_USED;
1699 }
1700
1701 /* make sure address not leased to another CLID/IAID */
1702 static int check_address(struct state *state, struct in6_addr *addr)
1703
1704   struct dhcp_lease *lease;
1705
1706   if (!(lease = lease6_find_by_addr(addr, 128, 0)))
1707     return 1;
1708
1709   if (lease->clid_len != state->clid_len || 
1710       memcmp(lease->clid, state->clid, state->clid_len) != 0 ||
1711       lease->iaid != state->iaid)
1712     return 0;
1713
1714   return 1;
1715 }
1716
1717
1718 /* return true of *addr could have been generated from config. */
1719 static struct addrlist *config_implies(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr)
1720 {
1721   int prefix;
1722   struct in6_addr wild_addr;
1723   struct addrlist *addr_list;
1724   
1725   if (!config || !(config->flags & CONFIG_ADDR6))
1726     return NULL;
1727   
1728   for (addr_list = config->addr6; addr_list; addr_list = addr_list->next)
1729     {
1730       prefix = (addr_list->flags & ADDRLIST_PREFIX) ? addr_list->prefixlen : 128;
1731       wild_addr = addr_list->addr.addr6;
1732       
1733       if ((addr_list->flags & ADDRLIST_WILDCARD) && context->prefix == 64)
1734         {
1735           wild_addr = context->start6;
1736           setaddr6part(&wild_addr, addr6part(&addr_list->addr.addr6));
1737         }
1738       else if (!is_same_net6(&context->start6, addr, context->prefix))
1739         continue;
1740       
1741       if (is_same_net6(&wild_addr, addr, prefix))
1742         return addr_list;
1743     }
1744   
1745   return NULL;
1746 }
1747
1748 static int config_valid(struct dhcp_config *config, struct dhcp_context *context, struct in6_addr *addr, struct state *state, time_t now)
1749 {
1750   u64 addrpart, i, addresses;
1751   struct addrlist *addr_list;
1752   
1753   if (!config || !(config->flags & CONFIG_ADDR6))
1754     return 0;
1755
1756   for (addr_list = config->addr6; addr_list; addr_list = addr_list->next)
1757     if (!(addr_list->flags & ADDRLIST_DECLINED) ||
1758         difftime(now, addr_list->decline_time) >= (float)DECLINE_BACKOFF)
1759       {
1760         addrpart = addr6part(&addr_list->addr.addr6);
1761         addresses = 1;
1762         
1763         if (addr_list->flags & ADDRLIST_PREFIX)
1764           addresses = (u64)1<<(128-addr_list->prefixlen);
1765         
1766         if ((addr_list->flags & ADDRLIST_WILDCARD))
1767           {
1768             if (context->prefix != 64)
1769               continue;
1770             
1771             *addr = context->start6;
1772           }
1773         else if (is_same_net6(&context->start6, &addr_list->addr.addr6, context->prefix))
1774           *addr = addr_list->addr.addr6;
1775         else
1776           continue;
1777         
1778         for (i = 0 ; i < addresses; i++)
1779           {
1780             setaddr6part(addr, addrpart+i);
1781             
1782             if (check_address(state, addr))
1783               return 1;
1784           }
1785       }
1786   
1787   return 0;
1788 }
1789
1790 /* Calculate valid and preferred times to send in leases/renewals. 
1791
1792    Inputs are:
1793
1794    *valid_timep, *preferred_timep - requested times from IAADDR options.
1795    context->valid, context->preferred - times associated with subnet address on local interface.
1796    context->flags | CONTEXT_DEPRECATE - "deprecated" flag in dhcp-range.
1797    lease_time - configured time for context for individual client.
1798    *min_time - smallest valid time sent so far.
1799
1800    Outputs are :
1801    
1802    *valid_timep, *preferred_timep - times to be send in IAADDR option.
1803    *min_time - smallest valid time sent so far, to calculate T1 and T2.
1804    
1805    */
1806 static void calculate_times(struct dhcp_context *context, unsigned int *min_time, unsigned int *valid_timep, 
1807                             unsigned int *preferred_timep, unsigned int lease_time)
1808 {
1809   unsigned int req_preferred = *preferred_timep, req_valid = *valid_timep;
1810   unsigned int valid_time = lease_time, preferred_time = lease_time;
1811   
1812   /* RFC 3315: "A server ignores the lifetimes set
1813      by the client if the preferred lifetime is greater than the valid
1814      lifetime. */
1815   if (req_preferred <= req_valid)
1816     {
1817       if (req_preferred != 0)
1818         {
1819           /* 0 == "no preference from client" */
1820           if (req_preferred < 120u)
1821             req_preferred = 120u; /* sanity */
1822           
1823           if (req_preferred < preferred_time)
1824             preferred_time = req_preferred;
1825         }
1826       
1827       if (req_valid != 0)
1828         /* 0 == "no preference from client" */
1829         {
1830           if (req_valid < 120u)
1831             req_valid = 120u; /* sanity */
1832           
1833           if (req_valid < valid_time)
1834             valid_time = req_valid;
1835         }
1836     }
1837
1838   /* deprecate (preferred == 0) which configured, or when local address 
1839      is deprecated */
1840   if ((context->flags & CONTEXT_DEPRECATE) || context->preferred == 0)
1841     preferred_time = 0;
1842   
1843   if (preferred_time != 0 && preferred_time < *min_time)
1844     *min_time = preferred_time;
1845   
1846   if (valid_time != 0 && valid_time < *min_time)
1847     *min_time = valid_time;
1848   
1849   *valid_timep = valid_time;
1850   *preferred_timep = preferred_time;
1851 }
1852
1853 static void update_leases(struct state *state, struct dhcp_context *context, struct in6_addr *addr, unsigned int lease_time, time_t now)
1854 {
1855   struct dhcp_lease *lease = lease6_find_by_addr(addr, 128, 0);
1856 #ifdef HAVE_SCRIPT
1857   struct dhcp_netid *tagif = run_tag_if(state->tags);
1858 #endif
1859
1860   (void)context;
1861
1862   if (!lease)
1863     lease = lease6_allocate(addr, state->ia_type == OPTION6_IA_NA ? LEASE_NA : LEASE_TA);
1864   
1865   if (lease)
1866     {
1867       lease_set_expires(lease, lease_time, now);
1868       lease_set_iaid(lease, state->iaid); 
1869       lease_set_hwaddr(lease, state->mac, state->clid, state->mac_len, state->mac_type, state->clid_len, now, 0);
1870       lease_set_interface(lease, state->interface, now);
1871       if (state->hostname && state->ia_type == OPTION6_IA_NA)
1872         {
1873           char *addr_domain = get_domain6(addr);
1874           if (!state->send_domain)
1875             state->send_domain = addr_domain;
1876           lease_set_hostname(lease, state->hostname, state->hostname_auth, addr_domain, state->domain);
1877         }
1878       
1879 #ifdef HAVE_SCRIPT
1880       if (daemon->lease_change_command)
1881         {
1882           void *opt;
1883           
1884           lease->flags |= LEASE_CHANGED;
1885           free(lease->extradata);
1886           lease->extradata = NULL;
1887           lease->extradata_size = lease->extradata_len = 0;
1888           lease->vendorclass_count = 0; 
1889           
1890           if ((opt = opt6_find(state->packet_options, state->end, OPTION6_VENDOR_CLASS, 4)))
1891             {
1892               void *enc_opt, *enc_end = opt6_ptr(opt, opt6_len(opt));
1893               lease->vendorclass_count++;
1894               /* send enterprise number first  */
1895               sprintf(daemon->dhcp_buff2, "%u", opt6_uint(opt, 0, 4));
1896               lease_add_extradata(lease, (unsigned char *)daemon->dhcp_buff2, strlen(daemon->dhcp_buff2), 0);
1897               
1898               if (opt6_len(opt) >= 6) 
1899                 for (enc_opt = opt6_ptr(opt, 4); enc_opt; enc_opt = opt6_next(enc_opt, enc_end))
1900                   {
1901                     lease->vendorclass_count++;
1902                     lease_add_extradata(lease, opt6_ptr(enc_opt, 0), opt6_len(enc_opt), 0);
1903                   }
1904             }
1905           
1906           lease_add_extradata(lease, (unsigned char *)state->client_hostname, 
1907                               state->client_hostname ? strlen(state->client_hostname) : 0, 0);                          
1908           
1909           /* DNSMASQ_REQUESTED_OPTIONS */
1910           if ((opt = opt6_find(state->packet_options, state->end, OPTION6_ORO, 2)))
1911             {
1912               int i, len = opt6_len(opt)/2;
1913               u16 *rop = opt6_ptr(opt, 0);
1914               
1915               for (i = 0; i < len; i++)
1916                 lease_add_extradata(lease, (unsigned char *)daemon->namebuff,
1917                                     sprintf(daemon->namebuff, "%u", ntohs(rop[i])), (i + 1) == len ? 0 : ',');
1918             }
1919           else
1920             lease_add_extradata(lease, NULL, 0, 0);
1921
1922           if ((opt = opt6_find(state->packet_options, state->end, OPTION6_MUD_URL, 1)))
1923             lease_add_extradata(lease, opt6_ptr(opt, 0), opt6_len(opt), 0);
1924           else
1925             lease_add_extradata(lease, NULL, 0, 0);
1926
1927           /* space-concat tag set */
1928           if (!tagif && !context->netid.net)
1929             lease_add_extradata(lease, NULL, 0, 0);
1930           else
1931             {
1932               if (context->netid.net)
1933                 lease_add_extradata(lease, (unsigned char *)context->netid.net, strlen(context->netid.net), tagif ? ' ' : 0);
1934               
1935               if (tagif)
1936                 {
1937                   struct dhcp_netid *n;
1938                   for (n = tagif; n; n = n->next)
1939                     {
1940                       struct dhcp_netid *n1;
1941                       /* kill dupes */
1942                       for (n1 = n->next; n1; n1 = n1->next)
1943                         if (strcmp(n->net, n1->net) == 0)
1944                           break;
1945                       if (!n1)
1946                         lease_add_extradata(lease, (unsigned char *)n->net, strlen(n->net), n->next ? ' ' : 0); 
1947                     }
1948                 }
1949             }
1950           
1951           if (state->link_address)
1952             inet_ntop(AF_INET6, state->link_address, daemon->addrbuff, ADDRSTRLEN);
1953           
1954           lease_add_extradata(lease, (unsigned char *)daemon->addrbuff, state->link_address ? strlen(daemon->addrbuff) : 0, 0);
1955           
1956           if ((opt = opt6_find(state->packet_options, state->end, OPTION6_USER_CLASS, 2)))
1957             {
1958               void *enc_opt, *enc_end = opt6_ptr(opt, opt6_len(opt));
1959               for (enc_opt = opt6_ptr(opt, 0); enc_opt; enc_opt = opt6_next(enc_opt, enc_end))
1960                 lease_add_extradata(lease, opt6_ptr(enc_opt, 0), opt6_len(enc_opt), 0);
1961             }
1962         }
1963 #endif  
1964       
1965     }
1966 }
1967                           
1968                         
1969         
1970 static void log6_opts(int nest, unsigned int xid, void *start_opts, void *end_opts)
1971 {
1972   void *opt;
1973   char *desc = nest ? "nest" : "sent";
1974   
1975   if (!option_bool(OPT_LOG_OPTS) || start_opts == end_opts)
1976     return;
1977   
1978   for (opt = start_opts; opt; opt = opt6_next(opt, end_opts))
1979     {
1980       int type = opt6_type(opt);
1981       void *ia_options = NULL;
1982       char *optname;
1983       
1984       if (type == OPTION6_IA_NA)
1985         {
1986           sprintf(daemon->namebuff, "IAID=%u T1=%u T2=%u",
1987                   opt6_uint(opt, 0, 4), opt6_uint(opt, 4, 4), opt6_uint(opt, 8, 4));
1988           optname = "ia-na";
1989           ia_options = opt6_ptr(opt, 12);
1990         }
1991       else if (type == OPTION6_IA_TA)
1992         {
1993           sprintf(daemon->namebuff, "IAID=%u", opt6_uint(opt, 0, 4));
1994           optname = "ia-ta";
1995           ia_options = opt6_ptr(opt, 4);
1996         }
1997       else if (type == OPTION6_IAADDR)
1998         {
1999           struct in6_addr addr;
2000
2001           /* align */
2002           memcpy(&addr, opt6_ptr(opt, 0), IN6ADDRSZ);
2003           inet_ntop(AF_INET6, &addr, daemon->addrbuff, ADDRSTRLEN);
2004           sprintf(daemon->namebuff, "%s PL=%u VL=%u", 
2005                   daemon->addrbuff, opt6_uint(opt, 16, 4), opt6_uint(opt, 20, 4));
2006           optname = "iaaddr";
2007           ia_options = opt6_ptr(opt, 24);
2008         }
2009       else if (type == OPTION6_STATUS_CODE)
2010         {
2011           int len = sprintf(daemon->namebuff, "%u ", opt6_uint(opt, 0, 2));
2012           memcpy(daemon->namebuff + len, opt6_ptr(opt, 2), opt6_len(opt)-2);
2013           daemon->namebuff[len + opt6_len(opt) - 2] = 0;
2014           optname = "status";
2015         }
2016       else
2017         {
2018           /* account for flag byte on FQDN */
2019           int offset = type == OPTION6_FQDN ? 1 : 0;
2020           optname = option_string(AF_INET6, type, opt6_ptr(opt, offset), opt6_len(opt) - offset, daemon->namebuff, MAXDNAME);
2021         }
2022       
2023       my_syslog(MS_DHCP | LOG_INFO, "%u %s size:%3d option:%3d %s  %s", 
2024                 xid, desc, opt6_len(opt), type, optname, daemon->namebuff);
2025       
2026       if (ia_options)
2027         log6_opts(1, xid, ia_options, opt6_ptr(opt, opt6_len(opt)));
2028     }
2029 }                
2030  
2031 static void log6_quiet(struct state *state, char *type, struct in6_addr *addr, char *string)
2032 {
2033   if (option_bool(OPT_LOG_OPTS) || !option_bool(OPT_QUIET_DHCP6))
2034     log6_packet(state, type, addr, string);
2035 }
2036
2037 static void log6_packet(struct state *state, char *type, struct in6_addr *addr, char *string)
2038 {
2039   int clid_len = state->clid_len;
2040
2041   /* avoid buffer overflow */
2042   if (clid_len > 100)
2043     clid_len = 100;
2044   
2045   print_mac(daemon->namebuff, state->clid, clid_len);
2046
2047   if (addr)
2048     {
2049       inet_ntop(AF_INET6, addr, daemon->dhcp_buff2, DHCP_BUFF_SZ - 1);
2050       strcat(daemon->dhcp_buff2, " ");
2051     }
2052   else
2053     daemon->dhcp_buff2[0] = 0;
2054
2055   if(option_bool(OPT_LOG_OPTS))
2056     my_syslog(MS_DHCP | LOG_INFO, "%u %s(%s) %s%s %s",
2057               state->xid, 
2058               type,
2059               state->iface_name, 
2060               daemon->dhcp_buff2,
2061               daemon->namebuff,
2062               string ? string : "");
2063   else
2064     my_syslog(MS_DHCP | LOG_INFO, "%s(%s) %s%s %s",
2065               type,
2066               state->iface_name, 
2067               daemon->dhcp_buff2,
2068               daemon->namebuff,
2069               string ? string : "");
2070 }
2071
2072 static void *opt6_find (void *opts, void *end, unsigned int search, unsigned int minsize)
2073 {
2074   u16 opt, opt_len;
2075   void *start;
2076   
2077   if (!opts)
2078     return NULL;
2079     
2080   while (1)
2081     {
2082       if (end - opts < 4) 
2083         return NULL;
2084       
2085       start = opts;
2086       GETSHORT(opt, opts);
2087       GETSHORT(opt_len, opts);
2088       
2089       if (opt_len > (end - opts))
2090         return NULL;
2091       
2092       if (opt == search && (opt_len >= minsize))
2093         return start;
2094       
2095       opts += opt_len;
2096     }
2097 }
2098
2099 static void *opt6_next(void *opts, void *end)
2100 {
2101   u16 opt_len;
2102   
2103   if (end - opts < 4) 
2104     return NULL;
2105   
2106   opts += 2;
2107   GETSHORT(opt_len, opts);
2108   
2109   if (opt_len >= (end - opts))
2110     return NULL;
2111   
2112   return opts + opt_len;
2113 }
2114
2115 static unsigned int opt6_uint(unsigned char *opt, int offset, int size)
2116 {
2117   /* this worries about unaligned data and byte order */
2118   unsigned int ret = 0;
2119   int i;
2120   unsigned char *p = opt6_ptr(opt, offset);
2121   
2122   for (i = 0; i < size; i++)
2123     ret = (ret << 8) | *p++;
2124   
2125   return ret;
2126
2127
2128 int relay_upstream6(int iface_index, ssize_t sz, 
2129                     struct in6_addr *peer_address, u32 scope_id, time_t now)
2130 {
2131   unsigned char *header;
2132   unsigned char *inbuff = daemon->dhcp_packet.iov_base;
2133   int msg_type = *inbuff;
2134   int hopcount, o;
2135   struct in6_addr multicast;
2136   unsigned int maclen, mactype;
2137   unsigned char mac[DHCP_CHADDR_MAX];
2138   struct dhcp_relay *relay;
2139   
2140   for (relay = daemon->relay6; relay; relay = relay->next)
2141     if (relay->iface_index != 0 && relay->iface_index == iface_index)
2142       break;
2143
2144   /* No relay config. */
2145   if (!relay)
2146     return 0;
2147   
2148   inet_pton(AF_INET6, ALL_SERVERS, &multicast);
2149   get_client_mac(peer_address, scope_id, mac, &maclen, &mactype, now);
2150   
2151   /* Get hop count from nested relayed message */ 
2152   if (msg_type == DHCP6RELAYFORW)
2153     hopcount = *((unsigned char *)inbuff+1) + 1;
2154   else
2155     hopcount = 0;
2156
2157   reset_counter();
2158
2159   /* RFC 3315 HOP_COUNT_LIMIT */
2160   if (hopcount > 32 || !(header = put_opt6(NULL, 34)))
2161     return 1;
2162   
2163   header[0] = DHCP6RELAYFORW;
2164   header[1] = hopcount;
2165   memcpy(&header[18], peer_address, IN6ADDRSZ);
2166   
2167   /* RFC-6939 */
2168   if (maclen != 0)
2169     {
2170       o = new_opt6(OPTION6_CLIENT_MAC);
2171       put_opt6_short(mactype);
2172       put_opt6(mac, maclen);
2173       end_opt6(o);
2174     }
2175   
2176   o = new_opt6(OPTION6_RELAY_MSG);
2177   put_opt6(inbuff, sz);
2178   end_opt6(o);
2179   
2180   for (; relay; relay = relay->next)
2181     if (relay->iface_index != 0 && relay->iface_index == iface_index)
2182       {
2183         union mysockaddr to;
2184
2185         memcpy(&header[2], &relay->local.addr6, IN6ADDRSZ);
2186         
2187         to.sa.sa_family = AF_INET6;
2188         to.in6.sin6_addr = relay->server.addr6;
2189         to.in6.sin6_port = htons(relay->port);
2190         to.in6.sin6_flowinfo = 0;
2191         to.in6.sin6_scope_id = 0;
2192         
2193         if (IN6_ARE_ADDR_EQUAL(&relay->server.addr6, &multicast))
2194           {
2195             int multicast_iface;
2196             if (!relay->interface || strchr(relay->interface, '*') ||
2197                 (multicast_iface = if_nametoindex(relay->interface)) == 0 ||
2198                 setsockopt(daemon->dhcp6fd, IPPROTO_IPV6, IPV6_MULTICAST_IF, &multicast_iface, sizeof(multicast_iface)) == -1)
2199               {
2200                 my_syslog(MS_DHCP | LOG_ERR, _("Cannot multicast DHCP relay via interface %s"), relay->interface);
2201                 continue;
2202               }
2203           }
2204         
2205 #ifdef HAVE_DUMPFILE
2206         dump_packet_udp(DUMP_DHCPV6, (void *)daemon->outpacket.iov_base, save_counter(-1), NULL, &to, daemon->dhcp6fd);
2207 #endif
2208
2209         while (retry_send(sendto(daemon->dhcp6fd, (void *)daemon->outpacket.iov_base, save_counter(-1),
2210                                  0, (struct sockaddr *)&to, sa_len(&to))));
2211         
2212         if (option_bool(OPT_LOG_OPTS))
2213           {
2214             inet_ntop(AF_INET6, &relay->local, daemon->addrbuff, ADDRSTRLEN);
2215             if (IN6_ARE_ADDR_EQUAL(&relay->server.addr6, &multicast))
2216               snprintf(daemon->namebuff, MAXDNAME, _("multicast via %s"), relay->interface);
2217             else
2218               inet_ntop(AF_INET6, &relay->server, daemon->namebuff, ADDRSTRLEN);
2219             my_syslog(MS_DHCP | LOG_INFO, _("DHCP relay at %s -> %s"), daemon->addrbuff, daemon->namebuff);
2220           }
2221         
2222       }
2223   
2224   return 1;
2225 }
2226
2227 int relay_reply6(struct sockaddr_in6 *peer, ssize_t sz, char *arrival_interface)
2228 {
2229   struct dhcp_relay *relay;
2230   struct in6_addr link;
2231   unsigned char *inbuff = daemon->dhcp_packet.iov_base;
2232   
2233   /* must have at least msg_type+hopcount+link_address+peer_address+minimal size option
2234      which is               1   +    1   +    16      +     16     + 2 + 2 = 38 */
2235   
2236   if (sz < 38 || *inbuff != DHCP6RELAYREPL)
2237     return 0;
2238   
2239   memcpy(&link, &inbuff[2], IN6ADDRSZ); 
2240   
2241   for (relay = daemon->relay6; relay; relay = relay->next)
2242     if (IN6_ARE_ADDR_EQUAL(&link, &relay->local.addr6) &&
2243         (!relay->interface || wildcard_match(relay->interface, arrival_interface)))
2244       break;
2245       
2246   reset_counter();
2247
2248   if (relay)
2249     {
2250       void *opt, *opts = inbuff + 34;
2251       void *end = inbuff + sz;
2252       for (opt = opts; opt; opt = opt6_next(opt, end))
2253         if (opt6_type(opt) == OPTION6_RELAY_MSG && opt6_len(opt) > 0)
2254           {
2255             int encap_type = *((unsigned char *)opt6_ptr(opt, 0));
2256             put_opt6(opt6_ptr(opt, 0), opt6_len(opt));
2257             memcpy(&peer->sin6_addr, &inbuff[18], IN6ADDRSZ); 
2258             peer->sin6_scope_id = relay->iface_index;
2259
2260             if (encap_type == DHCP6RELAYREPL)
2261               {
2262                 peer->sin6_port = ntohs(DHCPV6_SERVER_PORT);
2263                 return 1;
2264               }
2265
2266             peer->sin6_port = ntohs(DHCPV6_CLIENT_PORT);
2267             
2268 #ifdef HAVE_SCRIPT
2269             if (daemon->lease_change_command && encap_type == DHCP6REPLY)
2270               {
2271                 /* decapsulate relayed message */
2272                 opts = opt6_ptr(opt, 4);
2273                 end = opt6_ptr(opt, opt6_len(opt));
2274
2275                 for (opt = opts; opt; opt = opt6_next(opt, end))
2276                   if (opt6_type(opt) == OPTION6_IA_PD && opt6_len(opt) > 12) 
2277                     {
2278                       void *ia_opts = opt6_ptr(opt, 12);
2279                       void *ia_end = opt6_ptr(opt, opt6_len(opt));
2280                       void *ia_opt;
2281                       
2282                       for (ia_opt = ia_opts; ia_opt; ia_opt = opt6_next(ia_opt, ia_end))
2283                         /* valid lifetime must not be zero. */
2284                         if (opt6_type(ia_opt) == OPTION6_IAPREFIX && opt6_len(ia_opt) >= 25 && opt6_uint(ia_opt, 4, 4) != 0)
2285                           {
2286                             if (daemon->free_snoops ||
2287                                 (daemon->free_snoops = whine_malloc(sizeof(struct snoop_record))))
2288                               {
2289                                 struct snoop_record *snoop = daemon->free_snoops;
2290                                 
2291                                 daemon->free_snoops = snoop->next;
2292                                 snoop->client = peer->sin6_addr;
2293                                 snoop->prefix_len = opt6_uint(ia_opt, 8, 1); 
2294                                 memcpy(&snoop->prefix, opt6_ptr(ia_opt, 9), IN6ADDRSZ); 
2295                                 snoop->next = relay->snoop_records;
2296                                 relay->snoop_records = snoop;
2297                               }
2298                           }
2299                     }
2300               }
2301 #endif          
2302             return 1;
2303           }
2304       
2305     }
2306   
2307   return 0;
2308 }
2309
2310 #ifdef HAVE_SCRIPT
2311 int do_snoop_script_run(void)
2312 {
2313   struct dhcp_relay *relay;
2314   struct snoop_record *snoop;
2315   
2316   for (relay = daemon->relay6; relay; relay = relay->next)
2317     if ((snoop = relay->snoop_records))
2318       {
2319         relay->snoop_records = snoop->next;
2320         snoop->next = daemon->free_snoops;
2321         daemon->free_snoops = snoop;
2322         
2323         queue_relay_snoop(&snoop->client, relay->iface_index, &snoop->prefix, snoop->prefix_len);
2324         return 1;
2325       }
2326   
2327   return 0;
2328 }
2329 #endif
2330
2331 #endif