| 1 | /* |
|---|
| 2 | * dnsmasq.c |
|---|
| 3 | * |
|---|
| 4 | * Copyright (C) 2007 Sebastian Gottschall <gottschall@dd-wrt.com> |
|---|
| 5 | * |
|---|
| 6 | * This program is free software; you can redistribute it and/or |
|---|
| 7 | * modify it under the terms of the GNU General Public License |
|---|
| 8 | * as published by the Free Software Foundation; either version 2 |
|---|
| 9 | * of the License. |
|---|
| 10 | * |
|---|
| 11 | * This program is distributed in the hope that it will be useful, |
|---|
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | * GNU General Public License for more details. |
|---|
| 15 | * |
|---|
| 16 | * You should have received a copy of the GNU General Public License |
|---|
| 17 | * along with this program; if not, write to the Free Software |
|---|
| 18 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|---|
| 19 | * |
|---|
| 20 | * $Id: |
|---|
| 21 | */ |
|---|
| 22 | #ifdef HAVE_DNSMASQ |
|---|
| 23 | #include <unistd.h> |
|---|
| 24 | #include <stdio.h> |
|---|
| 25 | #include <stdlib.h> |
|---|
| 26 | #include <string.h> |
|---|
| 27 | #include <errno.h> |
|---|
| 28 | #include <sys/time.h> |
|---|
| 29 | #include <sys/types.h> |
|---|
| 30 | #include <sys/stat.h> |
|---|
| 31 | #include <syslog.h> |
|---|
| 32 | #include <utils.h> |
|---|
| 33 | #include <bcmnvram.h> |
|---|
| 34 | #include <shutils.h> |
|---|
| 35 | |
|---|
| 36 | extern int usejffs; |
|---|
| 37 | |
|---|
| 38 | extern void addHost(char *host, char *ip); |
|---|
| 39 | |
|---|
| 40 | void stop_dnsmasq(void); |
|---|
| 41 | |
|---|
| 42 | char *getmdhcp(int count, int index) |
|---|
| 43 | { |
|---|
| 44 | int cnt = 0; |
|---|
| 45 | static char word[256]; |
|---|
| 46 | char *next, *wordlist; |
|---|
| 47 | |
|---|
| 48 | wordlist = nvram_safe_get("mdhcpd"); |
|---|
| 49 | foreach(word, wordlist, next) { |
|---|
| 50 | if (cnt < index) { |
|---|
| 51 | cnt++; |
|---|
| 52 | continue; |
|---|
| 53 | } |
|---|
| 54 | char *interface = word; |
|---|
| 55 | char *dhcpon = interface; |
|---|
| 56 | |
|---|
| 57 | interface = strsep(&dhcpon, ">"); |
|---|
| 58 | char *start = dhcpon; |
|---|
| 59 | |
|---|
| 60 | dhcpon = strsep(&start, ">"); |
|---|
| 61 | char *max = start; |
|---|
| 62 | |
|---|
| 63 | start = strsep(&max, ">"); |
|---|
| 64 | char *leasetime = max; |
|---|
| 65 | |
|---|
| 66 | max = strsep(&leasetime, ">"); |
|---|
| 67 | if (count == 0) |
|---|
| 68 | return interface; |
|---|
| 69 | if (count == 1) |
|---|
| 70 | return dhcpon; |
|---|
| 71 | if (count == 2) |
|---|
| 72 | return start; |
|---|
| 73 | if (count == 3) |
|---|
| 74 | return max; |
|---|
| 75 | if (count == 4) |
|---|
| 76 | return leasetime; |
|---|
| 77 | } |
|---|
| 78 | return ""; |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | int landhcp(void) |
|---|
| 82 | { |
|---|
| 83 | if (!nvram_match("wl0_mode", "wet") |
|---|
| 84 | && !nvram_match("wl0_mode", "apstawet")) |
|---|
| 85 | if (nvram_match("dhcp_dnsmasq", "1") |
|---|
| 86 | && nvram_match("lan_proto", "dhcp") |
|---|
| 87 | && nvram_match("dhcpfwd_enable", "0")) |
|---|
| 88 | return 1; |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | int hasdhcp(void) |
|---|
| 93 | { |
|---|
| 94 | int count = 0; |
|---|
| 95 | int ret = landhcp(); |
|---|
| 96 | |
|---|
| 97 | return ret; |
|---|
| 98 | // for now, keep it disabled |
|---|
| 99 | /* if( nvram_get( "mdhcpd_count" ) != NULL ) |
|---|
| 100 | count = atoi( nvram_safe_get( "mdhcpd_count" ) ); |
|---|
| 101 | ret |= count; |
|---|
| 102 | return ret > 0 ? 1 : 0;*/ |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | int canlan(void) |
|---|
| 106 | { |
|---|
| 107 | if (nvram_match("dhcpfwd_enable", "0")) |
|---|
| 108 | return 1; |
|---|
| 109 | return 0; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | void start_dnsmasq(void) |
|---|
| 113 | { |
|---|
| 114 | FILE *fp; |
|---|
| 115 | struct dns_lists *dns_list = NULL; |
|---|
| 116 | int ret; |
|---|
| 117 | int i; |
|---|
| 118 | |
|---|
| 119 | if (nvram_match("dhcp_dnsmasq", "1") |
|---|
| 120 | && nvram_match("lan_proto", "dhcp") |
|---|
| 121 | && nvram_match("dnsmasq_enable", "0")) { |
|---|
| 122 | nvram_set("dnsmasq_enable", "1"); |
|---|
| 123 | nvram_commit(); |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | if (!nvram_invmatch("dnsmasq_enable", "0")) { |
|---|
| 127 | stop_dnsmasq(); |
|---|
| 128 | return; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | usejffs = 0; |
|---|
| 132 | |
|---|
| 133 | if (nvram_match("dhcpd_usejffs", "1")) { |
|---|
| 134 | if (!(fp = fopen("/jffs/dnsmasq.leases", "a"))) { |
|---|
| 135 | usejffs = 0; |
|---|
| 136 | } else { |
|---|
| 137 | fclose(fp); |
|---|
| 138 | usejffs = 1; |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | |
|---|
| 142 | /* |
|---|
| 143 | * Write configuration file based on current information |
|---|
| 144 | */ |
|---|
| 145 | if (!(fp = fopen("/tmp/dnsmasq.conf", "w"))) { |
|---|
| 146 | perror("/tmp/dnsmasq.conf"); |
|---|
| 147 | return; |
|---|
| 148 | } |
|---|
| 149 | // fprintf(fp, "bind-interfaces\n"); |
|---|
| 150 | if (nvram_match("fon_enable", "1") |
|---|
| 151 | || (nvram_match("chilli_nowifibridge", "1") |
|---|
| 152 | && nvram_match("chilli_enable", "1"))) { |
|---|
| 153 | if (canlan()) |
|---|
| 154 | fprintf(fp, "interface=%s,br0", |
|---|
| 155 | nvram_safe_get("wl0_ifname")); |
|---|
| 156 | else |
|---|
| 157 | fprintf(fp, "interface=%s", |
|---|
| 158 | nvram_safe_get("wl0_ifname")); |
|---|
| 159 | } else { |
|---|
| 160 | if (nvram_match("chilli_enable", "1")) { |
|---|
| 161 | if (canlan()) |
|---|
| 162 | fprintf(fp, "interface=%s", |
|---|
| 163 | nvram_safe_get("wl0_ifname")); |
|---|
| 164 | else |
|---|
| 165 | fprintf(fp, "interface=%s,", |
|---|
| 166 | nvram_safe_get("wl0_ifname")); |
|---|
| 167 | } else if (nvram_match("pptpd_enable", "1")) { |
|---|
| 168 | if (canlan()) |
|---|
| 169 | fprintf(fp, "listen-address=%s,%s", "127.0.0.1", |
|---|
| 170 | nvram_safe_get("lan_ipaddr")); |
|---|
| 171 | else |
|---|
| 172 | fprintf(fp, "listen-address=%s", "127.0.0.1"); |
|---|
| 173 | } else { |
|---|
| 174 | if (canlan()) |
|---|
| 175 | fprintf(fp, "interface=%s", |
|---|
| 176 | nvram_safe_get("lan_ifname")); |
|---|
| 177 | else |
|---|
| 178 | fprintf(fp, "interface="); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | int mdhcpcount = 0; |
|---|
| 182 | |
|---|
| 183 | if (nvram_get("mdhcpd_count") != NULL) { |
|---|
| 184 | mdhcpcount = atoi(nvram_safe_get("mdhcpd_count")); |
|---|
| 185 | for (i = 0; i < mdhcpcount; i++) { |
|---|
| 186 | if (strlen(nvram_nget("%s_ipaddr", getmdhcp(0, i))) == 0 |
|---|
| 187 | || strlen(nvram_nget("%s_netmask", getmdhcp(0, i))) |
|---|
| 188 | == 0) |
|---|
| 189 | continue; |
|---|
| 190 | if (canlan() || i > 0) { |
|---|
| 191 | if (nvram_match("pptpd_enable", "1")) |
|---|
| 192 | fprintf(fp, ",%s", |
|---|
| 193 | nvram_nget("%s_ipaddr", |
|---|
| 194 | getmdhcp(0, i))); |
|---|
| 195 | else |
|---|
| 196 | fprintf(fp, ",%s", getmdhcp(0, i)); |
|---|
| 197 | } else { |
|---|
| 198 | if (nvram_match("pptpd_enable", "1")) |
|---|
| 199 | fprintf(fp, "%s", |
|---|
| 200 | nvram_nget("%s_ipaddr", |
|---|
| 201 | getmdhcp(0, i))); |
|---|
| 202 | else |
|---|
| 203 | fprintf(fp, "%s", getmdhcp(0, i)); |
|---|
| 204 | |
|---|
| 205 | } |
|---|
| 206 | } |
|---|
| 207 | } |
|---|
| 208 | fprintf(fp, "\n"); |
|---|
| 209 | |
|---|
| 210 | fprintf(fp, "resolv-file=/tmp/resolv.dnsmasq\n"); |
|---|
| 211 | |
|---|
| 212 | /* |
|---|
| 213 | * Domain |
|---|
| 214 | */ |
|---|
| 215 | if (nvram_match("dhcp_domain", "wan")) { |
|---|
| 216 | if (nvram_invmatch("wan_domain", "")) |
|---|
| 217 | fprintf(fp, "domain=%s\n", |
|---|
| 218 | nvram_safe_get("wan_domain")); |
|---|
| 219 | else if (nvram_invmatch("wan_get_domain", "")) |
|---|
| 220 | fprintf(fp, "domain=%s\n", |
|---|
| 221 | nvram_safe_get("wan_get_domain")); |
|---|
| 222 | } else { |
|---|
| 223 | if (nvram_invmatch("lan_domain", "")) |
|---|
| 224 | fprintf(fp, "domain=%s\n", |
|---|
| 225 | nvram_safe_get("lan_domain")); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /* |
|---|
| 229 | * DD-WRT use dnsmasq as DHCP replacement |
|---|
| 230 | */ |
|---|
| 231 | |
|---|
| 232 | //bs mod |
|---|
| 233 | if (hasdhcp()) { |
|---|
| 234 | /* |
|---|
| 235 | * DHCP leasefile |
|---|
| 236 | */ |
|---|
| 237 | if (nvram_match("dhcpd_usenvram", "1")) { |
|---|
| 238 | fprintf(fp, "leasefile-ro\n"); |
|---|
| 239 | fprintf(fp, "dhcp-script=%s\n", "/etc/lease_update.sh"); |
|---|
| 240 | } else { |
|---|
| 241 | if (usejffs) |
|---|
| 242 | fprintf(fp, |
|---|
| 243 | "dhcp-leasefile=/jffs/dnsmasq.leases\n"); |
|---|
| 244 | else |
|---|
| 245 | fprintf(fp, |
|---|
| 246 | "dhcp-leasefile=/tmp/dnsmasq.leases\n"); |
|---|
| 247 | } |
|---|
| 248 | |
|---|
| 249 | int dhcp_max = 0; |
|---|
| 250 | |
|---|
| 251 | if (landhcp()) |
|---|
| 252 | dhcp_max += |
|---|
| 253 | atoi(nvram_safe_get("dhcp_num")) + |
|---|
| 254 | atoi(nvram_safe_get("static_leasenum")); |
|---|
| 255 | for (i = 0; i < mdhcpcount; i++) { |
|---|
| 256 | if (strlen(nvram_nget("%s_ipaddr", getmdhcp(0, i))) == 0 |
|---|
| 257 | || strlen(nvram_nget("%s_netmask", getmdhcp(0, i))) |
|---|
| 258 | == 0) |
|---|
| 259 | continue; |
|---|
| 260 | dhcp_max += atoi(getmdhcp(3, i)); |
|---|
| 261 | } |
|---|
| 262 | fprintf(fp, "dhcp-lease-max=%d\n", dhcp_max); |
|---|
| 263 | if (landhcp()) |
|---|
| 264 | fprintf(fp, "dhcp-option=lan,3,%s\n", |
|---|
| 265 | nvram_safe_get("lan_ipaddr")); |
|---|
| 266 | for (i = 0; i < mdhcpcount; i++) { |
|---|
| 267 | if (strlen(nvram_nget("%s_ipaddr", getmdhcp(0, i))) == 0 |
|---|
| 268 | || strlen(nvram_nget("%s_netmask", getmdhcp(0, i))) |
|---|
| 269 | == 0) |
|---|
| 270 | continue; |
|---|
| 271 | fprintf(fp, "dhcp-option=%s,3,", getmdhcp(0, i)); |
|---|
| 272 | fprintf(fp, "%s\n", |
|---|
| 273 | nvram_nget("%s_ipaddr", getmdhcp(0, i))); |
|---|
| 274 | } |
|---|
| 275 | if (nvram_invmatch("wan_wins", "") |
|---|
| 276 | && nvram_invmatch("wan_wins", "0.0.0.0")) |
|---|
| 277 | fprintf(fp, "dhcp-option=44,%s\n", |
|---|
| 278 | nvram_safe_get("wan_wins")); |
|---|
| 279 | |
|---|
| 280 | if (nvram_match("dns_dnsmasq", "0")) { |
|---|
| 281 | dns_list = get_dns_list(); |
|---|
| 282 | |
|---|
| 283 | if (dns_list |
|---|
| 284 | && (strlen(dns_list->dns_server[0]) > 0 |
|---|
| 285 | || strlen(dns_list->dns_server[1]) > 0 |
|---|
| 286 | || strlen(dns_list->dns_server[2]) > 0)) { |
|---|
| 287 | |
|---|
| 288 | fprintf(fp, "dhcp-option=6"); |
|---|
| 289 | |
|---|
| 290 | if (strlen(dns_list->dns_server[0]) > 0) |
|---|
| 291 | fprintf(fp, ",%s", |
|---|
| 292 | dns_list->dns_server[0]); |
|---|
| 293 | |
|---|
| 294 | if (strlen(dns_list->dns_server[1]) > 0) |
|---|
| 295 | fprintf(fp, ",%s", |
|---|
| 296 | dns_list->dns_server[1]); |
|---|
| 297 | |
|---|
| 298 | if (strlen(dns_list->dns_server[2]) > 0) |
|---|
| 299 | fprintf(fp, ",%s", |
|---|
| 300 | dns_list->dns_server[2]); |
|---|
| 301 | |
|---|
| 302 | fprintf(fp, "\n"); |
|---|
| 303 | } |
|---|
| 304 | |
|---|
| 305 | if (dns_list) |
|---|
| 306 | free(dns_list); |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | if (nvram_match("auth_dnsmasq", "1")) |
|---|
| 310 | fprintf(fp, "dhcp-authoritative\n"); |
|---|
| 311 | if (landhcp()) { |
|---|
| 312 | fprintf(fp, "dhcp-range=lan,"); |
|---|
| 313 | fprintf(fp, "%d.%d.%d.%s,", |
|---|
| 314 | get_single_ip(nvram_safe_get("lan_ipaddr"), 0), |
|---|
| 315 | get_single_ip(nvram_safe_get("lan_ipaddr"), 1), |
|---|
| 316 | get_single_ip(nvram_safe_get("lan_ipaddr"), 2), |
|---|
| 317 | nvram_safe_get("dhcp_start")); |
|---|
| 318 | if (nvram_match("dhcp_num", "0")) { |
|---|
| 319 | fprintf(fp, "static,"); |
|---|
| 320 | } else { |
|---|
| 321 | fprintf(fp, "%d.%d.%d.%d,", |
|---|
| 322 | get_single_ip(nvram_safe_get |
|---|
| 323 | ("lan_ipaddr"), 0), |
|---|
| 324 | get_single_ip(nvram_safe_get |
|---|
| 325 | ("lan_ipaddr"), 1), |
|---|
| 326 | get_single_ip(nvram_safe_get |
|---|
| 327 | ("lan_ipaddr"), 2), |
|---|
| 328 | atoi(nvram_safe_get("dhcp_start")) + |
|---|
| 329 | atoi(nvram_safe_get("dhcp_num")) - 1); |
|---|
| 330 | } |
|---|
| 331 | fprintf(fp, "%s,", nvram_safe_get("lan_netmask")); |
|---|
| 332 | fprintf(fp, "%sm\n", nvram_safe_get("dhcp_lease")); |
|---|
| 333 | } |
|---|
| 334 | |
|---|
| 335 | for (i = 0; i < mdhcpcount; i++) { |
|---|
| 336 | if (strcmp(getmdhcp(1, i), "On")) |
|---|
| 337 | continue; |
|---|
| 338 | if (strlen(nvram_nget("%s_ipaddr", getmdhcp(0, i))) == 0 |
|---|
| 339 | || strlen(nvram_nget("%s_netmask", getmdhcp(0, i))) |
|---|
| 340 | == 0) |
|---|
| 341 | continue; |
|---|
| 342 | fprintf(fp, "dhcp-range=%s,", getmdhcp(0, i)); |
|---|
| 343 | fprintf(fp, "%d.%d.%d.", |
|---|
| 344 | get_single_ip(nvram_nget |
|---|
| 345 | ("%s_ipaddr", getmdhcp(0, i)), |
|---|
| 346 | 0), |
|---|
| 347 | get_single_ip(nvram_nget |
|---|
| 348 | ("%s_ipaddr", getmdhcp(0, i)), |
|---|
| 349 | 1), |
|---|
| 350 | get_single_ip(nvram_nget |
|---|
| 351 | ("%s_ipaddr", getmdhcp(0, i)), |
|---|
| 352 | 2)); |
|---|
| 353 | fprintf(fp, "%s,", getmdhcp(2, i)); |
|---|
| 354 | fprintf(fp, "%d.%d.%d.", |
|---|
| 355 | get_single_ip(nvram_nget |
|---|
| 356 | ("%s_ipaddr", getmdhcp(0, i)), |
|---|
| 357 | 0), |
|---|
| 358 | get_single_ip(nvram_nget |
|---|
| 359 | ("%s_ipaddr", getmdhcp(0, i)), |
|---|
| 360 | 1), |
|---|
| 361 | get_single_ip(nvram_nget |
|---|
| 362 | ("%s_ipaddr", getmdhcp(0, i)), |
|---|
| 363 | 2)); |
|---|
| 364 | int end = atoi(getmdhcp(2, i)); |
|---|
| 365 | |
|---|
| 366 | end += atoi(getmdhcp(3, i)); |
|---|
| 367 | fprintf(fp, "%d,", end); |
|---|
| 368 | fprintf(fp, "%s,", |
|---|
| 369 | nvram_nget("%s_netmask", getmdhcp(0, i))); |
|---|
| 370 | fprintf(fp, "%sm\n", getmdhcp(4, i)); |
|---|
| 371 | } |
|---|
| 372 | |
|---|
| 373 | int leasenum = atoi(nvram_safe_get("static_leasenum")); |
|---|
| 374 | |
|---|
| 375 | if (leasenum > 0) { |
|---|
| 376 | char *lease = nvram_safe_get("static_leases"); |
|---|
| 377 | char *leasebuf = (char *)malloc(strlen(lease) + 1); |
|---|
| 378 | char *cp = leasebuf; |
|---|
| 379 | |
|---|
| 380 | strcpy(leasebuf, lease); |
|---|
| 381 | for (i = 0; i < leasenum; i++) { |
|---|
| 382 | char *mac = strsep(&leasebuf, "="); |
|---|
| 383 | char *host = strsep(&leasebuf, "="); |
|---|
| 384 | char *ip = strsep(&leasebuf, " "); |
|---|
| 385 | |
|---|
| 386 | if (mac == NULL || host == NULL || ip == NULL) |
|---|
| 387 | continue; |
|---|
| 388 | |
|---|
| 389 | fprintf(fp, "dhcp-host=%s,%s,%s,infinite\n", |
|---|
| 390 | mac, host, ip); |
|---|
| 391 | addHost(host, ip); |
|---|
| 392 | } |
|---|
| 393 | free(cp); |
|---|
| 394 | } |
|---|
| 395 | } |
|---|
| 396 | |
|---|
| 397 | /* |
|---|
| 398 | * Additional options |
|---|
| 399 | */ |
|---|
| 400 | if (nvram_invmatch("dnsmasq_options", "")) { |
|---|
| 401 | fwritenvram("dnsmasq_options", fp); |
|---|
| 402 | } |
|---|
| 403 | fclose(fp); |
|---|
| 404 | |
|---|
| 405 | dns_to_resolv(); |
|---|
| 406 | |
|---|
| 407 | chmod("/etc/lease_update.sh", 0700); |
|---|
| 408 | ret = eval("dnsmasq", "--conf-file=/tmp/dnsmasq.conf"); |
|---|
| 409 | dd_syslog(LOG_INFO, "dnsmasq : dnsmasq daemon successfully started\n"); |
|---|
| 410 | |
|---|
| 411 | cprintf("done\n"); |
|---|
| 412 | return; |
|---|
| 413 | } |
|---|
| 414 | |
|---|
| 415 | void stop_dnsmasq(void) |
|---|
| 416 | { |
|---|
| 417 | |
|---|
| 418 | if (pidof("dnsmasq") > 0) { |
|---|
| 419 | syslog(LOG_INFO, |
|---|
| 420 | "dnsmasq : dnsmasq daemon successfully stopped\n"); |
|---|
| 421 | softkill("dnsmasq"); |
|---|
| 422 | unlink("/tmp/resolv.dnsmasq"); |
|---|
| 423 | |
|---|
| 424 | cprintf("done\n"); |
|---|
| 425 | } |
|---|
| 426 | return; |
|---|
| 427 | } |
|---|
| 428 | #endif |
|---|