Merge branch 'v1.5' into LTS : 20200122
[librecmc/librecmc.git] / package / network / services / dnsmasq / patches / 0019-Tidy-address-union-handling-move-class-into-explicit.patch
1 From 65a01b71bb433c9466e4c78a73a8d8ed218ed4e8 Mon Sep 17 00:00:00 2001
2 From: Simon Kelley <simon@thekelleys.org.uk>
3 Date: Mon, 31 Dec 2018 23:56:33 +0000
4 Subject: [PATCH 19/32] Tidy address-union handling: move class into explicit
5  argument.
6
7 This moves the class argument to cache-insert into an argument,
8 rather then overloading a union in the address argument. Note that
9 tha class is NOT stored in the cache other than for DS/DNSKEY entries,
10 so must always be C_IN except for these. The data-extraction code
11 ensures this as it only attempts to cache C_IN class records.
12
13 Signed-off-by: Kevin Darbyshire-Bryant <ldir@darbyshire-bryant.me.uk>
14 ---
15  src/cache.c   | 57 ++++++++++++++++++++++-----------------------------
16  src/dnsmasq.h |  2 +-
17  src/dnssec.c  | 13 +++---------
18  src/rfc1035.c | 12 +++++------
19  4 files changed, 34 insertions(+), 50 deletions(-)
20
21 --- a/src/cache.c
22 +++ b/src/cache.c
23 @@ -26,7 +26,7 @@ static union bigname *big_free = NULL;
24  static int bignames_left, hash_size;
25  
26  static void make_non_terminals(struct crec *source);
27 -static struct crec *really_insert(char *name, struct all_addr *addr, 
28 +static struct crec *really_insert(char *name, struct all_addr *addr, unsigned short class,
29                                   time_t now,  unsigned long ttl, unsigned short flags);
30  
31  /* type->string mapping: this is also used by the name-hash function as a mixing table. */
32 @@ -330,8 +330,8 @@ static int is_expired(time_t now, struct
33    return 1;
34  }
35  
36 -static struct crec *cache_scan_free(char *name, struct all_addr *addr, time_t now, unsigned short flags,
37 -                                   struct crec **target_crec, unsigned int *target_uid)
38 +static struct crec *cache_scan_free(char *name, struct all_addr *addr, unsigned short class, time_t now,
39 +                                   unsigned short flags, struct crec **target_crec, unsigned int *target_uid)
40  {
41    /* Scan and remove old entries.
42       If (flags & F_FORWARD) then remove any forward entries for name and any expired
43 @@ -350,6 +350,8 @@ static struct crec *cache_scan_free(char
44       This entry will get re-used with the same name, to preserve CNAMEs. */
45   
46    struct crec *crecp, **up;
47 +
48 +  (void)class;
49    
50    if (flags & F_FORWARD)
51      {
52 @@ -381,7 +383,7 @@ static struct crec *cache_scan_free(char
53               
54  #ifdef HAVE_DNSSEC
55               /* Deletion has to be class-sensitive for DS and DNSKEY */
56 -             if ((flags & crecp->flags & (F_DNSKEY | F_DS)) && crecp->uid == addr->addr.dnssec.class)
57 +             if ((flags & crecp->flags & (F_DNSKEY | F_DS)) && crecp->uid == class)
58                 {
59                   if (crecp->flags & F_CONFIG)
60                     return crecp;
61 @@ -464,7 +466,7 @@ void cache_start_insert(void)
62    insert_error = 0;
63  }
64  
65 -struct crec *cache_insert(char *name, struct all_addr *addr, 
66 +struct crec *cache_insert(char *name, struct all_addr *addr, unsigned short class,
67                           time_t now,  unsigned long ttl, unsigned short flags)
68  {
69    /* Don't log DNSSEC records here, done elsewhere */
70 @@ -478,11 +480,11 @@ struct crec *cache_insert(char *name, st
71         ttl = daemon->min_cache_ttl;
72      }
73    
74 -  return really_insert(name, addr, now, ttl, flags);
75 +  return really_insert(name, addr, class, now, ttl, flags);
76  }
77  
78  
79 -static struct crec *really_insert(char *name, struct all_addr *addr, 
80 +static struct crec *really_insert(char *name, struct all_addr *addr, unsigned short class,
81                                   time_t now,  unsigned long ttl, unsigned short flags)
82  {
83    struct crec *new, *target_crec = NULL;
84 @@ -497,7 +499,7 @@ static struct crec *really_insert(char *
85    
86    /* First remove any expired entries and entries for the name/address we
87       are currently inserting. */
88 -  if ((new = cache_scan_free(name, addr, now, flags, &target_crec, &target_uid)))
89 +  if ((new = cache_scan_free(name, addr, class, now, flags, &target_crec, &target_uid)))
90      {
91        /* We're trying to insert a record over one from 
92          /etc/hosts or DHCP, or other config. If the 
93 @@ -553,21 +555,14 @@ static struct crec *really_insert(char *
94        
95        if (freed_all)
96         {
97 -         struct all_addr free_addr = new->addr.addr;;
98 -         
99 -#ifdef HAVE_DNSSEC
100 -         /* For DNSSEC records, addr holds class. */
101 -         if (new->flags & (F_DS | F_DNSKEY))
102 -           free_addr.addr.dnssec.class = new->uid;
103 -#endif
104 -         
105 +         /* For DNSSEC records, uid holds class. */
106           free_avail = 1; /* Must be free space now. */
107 -         cache_scan_free(cache_get_name(new), &free_addr, now, new->flags, NULL, NULL);
108 +         cache_scan_free(cache_get_name(new), &new->addr.addr, new->uid, now, new->flags, NULL, NULL);
109           daemon->metrics[METRIC_DNS_CACHE_LIVE_FREED]++;
110         }
111        else
112         {
113 -         cache_scan_free(NULL, NULL, now, 0, NULL, NULL);
114 +         cache_scan_free(NULL, NULL, class, now, 0, NULL, NULL);
115           freed_all = 1;
116         }
117      }
118 @@ -615,15 +610,13 @@ static struct crec *really_insert(char *
119    else
120      *cache_get_name(new) = 0;
121  
122 -  if (addr)
123 -    {
124  #ifdef HAVE_DNSSEC
125 -      if (flags & (F_DS | F_DNSKEY))
126 -       new->uid = addr->addr.dnssec.class;
127 -      else
128 +  if (flags & (F_DS | F_DNSKEY))
129 +    new->uid = class;
130  #endif
131 -       new->addr.addr = *addr; 
132 -    }
133 +
134 +  if (addr)
135 +    new->addr.addr = *addr;    
136  
137    new->ttd = now + (time_t)ttl;
138    new->next = new_chain;
139 @@ -747,11 +740,11 @@ int cache_recv_insert(time_t now, int fd
140         {
141           if (!read_write(fd, (unsigned char *)&addr, sizeof(addr), 1))
142             return 0;
143 -         crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
144 +         crecp = really_insert(daemon->namebuff, &addr, C_IN, now, ttl, flags);
145         }
146        else if (flags & F_CNAME)
147         {
148 -         struct crec *newc = really_insert(daemon->namebuff, NULL, now, ttl, flags);
149 +         struct crec *newc = really_insert(daemon->namebuff, NULL, C_IN, now, ttl, flags);
150           /* This relies on the fact the the target of a CNAME immediately preceeds
151              it because of the order of extraction in extract_addresses, and
152              the order reversal on the new_chain. */
153 @@ -780,10 +773,8 @@ int cache_recv_insert(time_t now, int fd
154           
155           if (!read_write(fd, (unsigned char *)&class, sizeof(class), 1))
156             return 0;
157 -         /* Cache needs to known class for DNSSEC stuff */
158 -         addr.addr.dnssec.class = class;
159 -
160 -         crecp = really_insert(daemon->namebuff, &addr, now, ttl, flags);
161 +        
162 +         crecp = really_insert(daemon->namebuff, NULL, class, now, ttl, flags);
163             
164           if (flags & F_DNSKEY)
165             {
166 @@ -1463,7 +1454,7 @@ void cache_add_dhcp_entry(char *host_nam
167         }
168        else if (!(crec->flags & F_DHCP))
169         {
170 -         cache_scan_free(host_name, NULL, 0, crec->flags & (flags | F_CNAME | F_FORWARD), NULL, NULL);
171 +         cache_scan_free(host_name, NULL, C_IN, 0, crec->flags & (flags | F_CNAME | F_FORWARD), NULL, NULL);
172           /* scan_free deletes all addresses associated with name */
173           break;
174         }
175 @@ -1490,7 +1481,7 @@ void cache_add_dhcp_entry(char *host_nam
176        if (crec->flags & F_NEG)
177         {
178           flags |= F_REVERSE;
179 -         cache_scan_free(NULL, (struct all_addr *)host_address, 0, flags, NULL, NULL);
180 +         cache_scan_free(NULL, (struct all_addr *)host_address, C_IN, 0, flags, NULL, NULL);
181         }
182      }
183    else
184 --- a/src/dnsmasq.h
185 +++ b/src/dnsmasq.h
186 @@ -1144,7 +1144,7 @@ struct crec *cache_find_by_name(struct c
187  void cache_end_insert(void);
188  void cache_start_insert(void);
189  int cache_recv_insert(time_t now, int fd);
190 -struct crec *cache_insert(char *name, struct all_addr *addr,
191 +struct crec *cache_insert(char *name, struct all_addr *addr, unsigned short class, 
192                           time_t now, unsigned long ttl, unsigned short flags);
193  void cache_reload(void);
194  void cache_add_dhcp_entry(char *host_name, int prot, struct all_addr *host_address, time_t ttd);
195 --- a/src/dnssec.c
196 +++ b/src/dnssec.c
197 @@ -798,12 +798,9 @@ int dnssec_validate_by_ds(time_t now, st
198                   algo = *p++;
199                   keytag = dnskey_keytag(algo, flags, p, rdlen - 4);
200                   
201 -                 /* Cache needs to known class for DNSSEC stuff */
202 -                 a.addr.dnssec.class = class;
203 -                 
204                   if ((key = blockdata_alloc((char*)p, rdlen - 4)))
205                     {
206 -                     if (!(recp1 = cache_insert(name, &a, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
207 +                     if (!(recp1 = cache_insert(name, &a, class, now, ttl, F_FORWARD | F_DNSKEY | F_DNSSECOK)))
208                         {
209                           blockdata_free(key);
210                           return STAT_BOGUS;
211 @@ -927,12 +924,9 @@ int dnssec_validate_ds(time_t now, struc
212               algo = *p++;
213               digest = *p++;
214               
215 -             /* Cache needs to known class for DNSSEC stuff */
216 -             a.addr.dnssec.class = class;
217 -             
218               if ((key = blockdata_alloc((char*)p, rdlen - 4)))
219                 {
220 -                 if (!(crecp = cache_insert(name, &a, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
221 +                 if (!(crecp = cache_insert(name, NULL, class, now, ttl, F_FORWARD | F_DS | F_DNSSECOK)))
222                     {
223                       blockdata_free(key);
224                       return STAT_BOGUS;
225 @@ -1021,8 +1015,7 @@ int dnssec_validate_ds(time_t now, struc
226         {
227           cache_start_insert();
228           
229 -         a.addr.dnssec.class = class;
230 -         if (!cache_insert(name, &a, now, ttl, flags))
231 +         if (!cache_insert(name, NULL, class, now, ttl, flags))
232             return STAT_BOGUS;
233           
234           cache_end_insert();  
235 --- a/src/rfc1035.c
236 +++ b/src/rfc1035.c
237 @@ -701,7 +701,7 @@ int extract_addresses(struct dns_header
238                           goto cname_loop;
239                         }
240                       
241 -                     cache_insert(name, &addr, now, cttl, name_encoding | secflag | F_REVERSE);
242 +                     cache_insert(name, &addr, C_IN, now, cttl, name_encoding | secflag | F_REVERSE);
243                       found = 1; 
244                     }
245                   
246 @@ -719,7 +719,7 @@ int extract_addresses(struct dns_header
247                   ttl = find_soa(header, qlen, NULL, doctored);
248                 }
249               if (ttl)
250 -               cache_insert(NULL, &addr, now, ttl, name_encoding | F_REVERSE | F_NEG | flags | (secure ?  F_DNSSECOK : 0));    
251 +               cache_insert(NULL, &addr, C_IN, now, ttl, name_encoding | F_REVERSE | F_NEG | flags | (secure ?  F_DNSSECOK : 0));      
252             }
253         }
254        else
255 @@ -773,7 +773,7 @@ int extract_addresses(struct dns_header
256                     {
257                       if (!cname_count--)
258                         return 0; /* looped CNAMES */
259 -                     newc = cache_insert(name, NULL, now, attl, F_CNAME | F_FORWARD | secflag);
260 +                     newc = cache_insert(name, NULL, C_IN, now, attl, F_CNAME | F_FORWARD | secflag);
261                       if (newc)
262                         {
263                           newc->addr.cname.target.cache = NULL;
264 @@ -833,7 +833,7 @@ int extract_addresses(struct dns_header
265                         }
266  #endif
267                       
268 -                     newc = cache_insert(name, &addr, now, attl, flags | F_FORWARD | secflag);
269 +                     newc = cache_insert(name, &addr, C_IN, now, attl, flags | F_FORWARD | secflag);
270                       if (newc && cpp)
271                         {
272                           next_uid(newc);
273 @@ -860,7 +860,7 @@ int extract_addresses(struct dns_header
274                  pointing at this, inherit its TTL */
275               if (ttl || cpp)
276                 {
277 -                 newc = cache_insert(name, NULL, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0));        
278 +                 newc = cache_insert(name, NULL, C_IN, now, ttl ? ttl : cttl, F_FORWARD | F_NEG | flags | (secure ? F_DNSSECOK : 0));  
279                   if (newc && cpp)
280                     {
281                       next_uid(newc);
282 @@ -1054,7 +1054,7 @@ int check_for_bogus_wildcard(struct dns_
283                 /* Found a bogus address. Insert that info here, since there no SOA record
284                    to get the ttl from in the normal processing */
285                 cache_start_insert();
286 -               cache_insert(name, NULL, now, ttl, F_IPV4 | F_FORWARD | F_NEG | F_NXDOMAIN);
287 +               cache_insert(name, NULL, C_IN, now, ttl, F_IPV4 | F_FORWARD | F_NEG | F_NXDOMAIN);
288                 cache_end_insert();
289                 
290                 return 1;