76128301037cd8c4b1387fce532cacabf4d0f1a1
[openwrt/openwrt.git] / package / network / services / dnsmasq / files / dnsmasq.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007-2012 OpenWrt.org
3
4 START=19
5
6 USE_PROCD=1
7 PROG=/usr/sbin/dnsmasq
8
9 ADD_LOCAL_DOMAIN=1
10 ADD_LOCAL_HOSTNAME=1
11
12 BASECONFIGFILE="/var/etc/dnsmasq.conf"
13 BASEHOSTFILE="/tmp/hosts/dhcp"
14 BASETIMESTAMPFILE="/etc/dnsmasq.time"
15 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
16 TIMEVALIDFILE="/var/state/dnsmasqsec"
17 BASEDHCPSTAMPFILE="/var/run/dnsmasq"
18
19 xappend() {
20 local value="$1"
21
22 echo "${value#--}" >> $CONFIGFILE_TMP
23 }
24
25 hex_to_hostid() {
26 local var="$1"
27 local hex="${2#0x}" # strip optional "0x" prefix
28
29 if [ -n "${hex//[0-9a-fA-F]/}" ]; then
30 # is invalid hex literal
31 return 1
32 fi
33
34 # convert into host id
35 export "$var=$(
36 printf "%0x:%0x" \
37 $(((0x$hex >> 16) % 65536)) \
38 $(( 0x$hex % 256))
39 )"
40
41 return 0
42 }
43
44 dhcp_calc() {
45 local ip="$1"
46 local res=0
47
48 while [ -n "$ip" ]; do
49 part="${ip%%.*}"
50 res="$(($res * 256))"
51 res="$(($res + $part))"
52 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
53 done
54 echo "$res"
55 }
56
57 dhcp_check() {
58 local ifname="$1"
59 local stamp="${BASEDHCPSTAMPFILE_CFG}.${ifname}.dhcp"
60 local rv=0
61
62 [ -s "$stamp" ] && return $(cat "$stamp")
63
64 # If there's no carrier yet, skip this interface.
65 # The init script will be called again once the link is up
66 case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
67 false) return 1;;
68 esac
69
70 udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
71
72 [ $rv -eq 1 ] && \
73 logger -t dnsmasq \
74 "found already running DHCP-server on interface '$ifname'" \
75 "refusing to start, use 'option force 1' to override"
76
77 echo $rv > "$stamp"
78 return $rv
79 }
80
81 log_once() {
82 pidof dnsmasq >/dev/null || \
83 logger -t dnsmasq "$@"
84 }
85
86 append_bool() {
87 local section="$1"
88 local option="$2"
89 local value="$3"
90 local _loctmp
91 config_get_bool _loctmp "$section" "$option" 0
92 [ $_loctmp -gt 0 ] && xappend "$value"
93 }
94
95 append_parm() {
96 local section="$1"
97 local option="$2"
98 local switch="$3"
99 local default="$4"
100 local _loctmp
101 config_get _loctmp "$section" "$option" "$default"
102 [ -z "$_loctmp" ] && return 0
103 xappend "$switch=$_loctmp"
104 }
105
106 append_server() {
107 xappend "--server=$1"
108 }
109
110 append_address() {
111 xappend "--address=$1"
112 }
113
114 append_ipset() {
115 xappend "--ipset=$1"
116 }
117
118 append_interface() {
119 network_get_device ifname "$1" || return
120 xappend "--interface=$ifname"
121 }
122
123 append_notinterface() {
124 network_get_device ifname "$1" || return
125 xappend "--except-interface=$ifname"
126 }
127
128 append_addnhosts() {
129 xappend "--addn-hosts=$1"
130 }
131
132 append_bogusnxdomain() {
133 xappend "--bogus-nxdomain=$1"
134 }
135
136 append_pxe_service() {
137 xappend "--pxe-service=$1"
138 }
139
140 filter_dnsmasq() {
141 local cfg="$1" func="$2" match_cfg="$3" found_cfg
142
143 # use entry when no instance entry set, or if it matches
144 config_get found_cfg "$cfg" "instance"
145 if [ -z "$found_cfg" -o "$found_cfg" = "$match_cfg" ]; then
146 $func $cfg
147 fi
148 }
149
150 dhcp_subscrid_add() {
151 local cfg="$1"
152
153 config_get networkid "$cfg" networkid
154 [ -n "$networkid" ] || return 0
155
156 config_get subscriberid "$cfg" subscriberid
157 [ -n "$subscriberid" ] || return 0
158
159 xappend "--dhcp-subscrid=$networkid,$subscriberid"
160
161 config_get_bool force "$cfg" force 0
162
163 dhcp_option_add "$cfg" "$networkid" "$force"
164 }
165
166 dhcp_remoteid_add() {
167 local cfg="$1"
168
169 config_get networkid "$cfg" networkid
170 [ -n "$networkid" ] || return 0
171
172 config_get remoteid "$cfg" remoteid
173 [ -n "$remoteid" ] || return 0
174
175 xappend "--dhcp-remoteid=$networkid,$remoteid"
176
177 config_get_bool force "$cfg" force 0
178
179 dhcp_option_add "$cfg" "$networkid" "$force"
180 }
181
182 dhcp_circuitid_add() {
183 local cfg="$1"
184
185 config_get networkid "$cfg" networkid
186 [ -n "$networkid" ] || return 0
187
188 config_get circuitid "$cfg" circuitid
189 [ -n "$circuitid" ] || return 0
190
191 xappend "--dhcp-circuitid=$networkid,$circuitid"
192
193 config_get_bool force "$cfg" force 0
194
195 dhcp_option_add "$cfg" "$networkid" "$force"
196 }
197
198 dhcp_userclass_add() {
199 local cfg="$1"
200
201 config_get networkid "$cfg" networkid
202 [ -n "$networkid" ] || return 0
203
204 config_get userclass "$cfg" userclass
205 [ -n "$userclass" ] || return 0
206
207 xappend "--dhcp-userclass=$networkid,$userclass"
208
209 config_get_bool force "$cfg" force 0
210
211 dhcp_option_add "$cfg" "$networkid" "$force"
212 }
213
214 dhcp_vendorclass_add() {
215 local cfg="$1"
216
217 config_get networkid "$cfg" networkid
218 [ -n "$networkid" ] || return 0
219
220 config_get vendorclass "$cfg" vendorclass
221 [ -n "$vendorclass" ] || return 0
222
223 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
224
225 config_get_bool force "$cfg" force 0
226
227 dhcp_option_add "$cfg" "$networkid" "$force"
228 }
229
230 dhcp_match_add() {
231 local cfg="$1"
232
233 config_get networkid "$cfg" networkid
234 [ -n "$networkid" ] || return 0
235
236 config_get match "$cfg" match
237 [ -n "$match" ] || return 0
238
239 xappend "--dhcp-match=$networkid,$match"
240
241 config_get_bool force "$cfg" force 0
242
243 dhcp_option_add "$cfg" "$networkid" "$force"
244 }
245
246 dhcp_host_add() {
247 local cfg="$1"
248
249 config_get_bool force "$cfg" force 0
250
251 config_get networkid "$cfg" networkid
252 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
253
254 config_get_bool enable "$cfg" enable 1
255 [ "$enable" = "0" ] && return 0
256
257 config_get name "$cfg" name
258 config_get ip "$cfg" ip
259 [ -n "$ip" -o -n "$name" ] || return 0
260
261 config_get_bool dns "$cfg" dns 0
262 [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
263 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
264 }
265
266 config_get mac "$cfg" mac
267 if [ -n "$mac" ]; then
268 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
269 macs=""
270 for m in $mac; do append macs "$m" ","; done
271 else
272 # --dhcp-host=lap,192.168.0.199
273 [ -n "$name" ] || return 0
274 macs="$name"
275 name=""
276 fi
277
278 config_get tag "$cfg" tag
279
280 if [ "$DHCPv6CAPABLE" -eq 1 ]; then
281 config_get hostid "$cfg" hostid
282 if [ -n "$hostid" ]; then
283 hex_to_hostid hostid "$hostid"
284 fi
285 fi
286
287 config_get_bool broadcast "$cfg" broadcast 0
288 [ "$broadcast" = "0" ] && broadcast=
289
290 config_get leasetime "$cfg" leasetime
291
292 xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
293 }
294
295 dhcp_tag_add() {
296 local cfg="$1"
297
298 tag="$cfg"
299
300 [ -n "$tag" ] || return 0
301
302 config_get_bool force "$cfg" force 0
303 [ "$force" = "0" ] && force=
304
305 config_get option "$cfg" dhcp_option
306 for o in $option; do
307 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
308 done
309 }
310
311 dhcp_mac_add() {
312 local cfg="$1"
313
314 config_get networkid "$cfg" networkid
315 [ -n "$networkid" ] || return 0
316
317 config_get mac "$cfg" mac
318 [ -n "$mac" ] || return 0
319
320 xappend "--dhcp-mac=$networkid,$mac"
321
322 dhcp_option_add "$cfg" "$networkid"
323 }
324
325 dhcp_boot_add() {
326 local cfg="$1"
327
328 config_get networkid "$cfg" networkid
329
330 config_get filename "$cfg" filename
331 [ -n "$filename" ] || return 0
332
333 config_get servername "$cfg" servername
334 config_get serveraddress "$cfg" serveraddress
335
336 [ -n "$serveraddress" -a ! -n "$servername" ] && return 0
337
338 xappend "--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
339
340 config_get_bool force "$cfg" force 0
341
342 dhcp_option_add "$cfg" "$networkid" "$force"
343 }
344
345
346 dhcp_add() {
347 local cfg="$1"
348 config_get net "$cfg" interface
349 [ -n "$net" ] || return 0
350
351 config_get dhcpv4 "$cfg" dhcpv4
352 [ "$dhcpv4" != "disabled" ] || return 0
353
354 config_get networkid "$cfg" networkid
355 [ -n "$networkid" ] || networkid="$net"
356
357 network_get_subnet subnet "$net" || return 0
358 network_get_device ifname "$net" || return 0
359 network_get_protocol proto "$net" || return 0
360
361 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
362 DNS_SERVERS="$DNS_SERVERS $dnsserver"
363 }
364
365 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
366
367 # Do not support non-static interfaces for now
368 [ static = "$proto" ] || return 0
369
370 # Override interface netmask with dhcp config if applicable
371 config_get netmask "$cfg" netmask "${subnet##*/}"
372
373 #check for an already active dhcp server on the interface, unless 'force' is set
374 config_get_bool force "$cfg" force 0
375 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
376
377 config_get start "$cfg" start
378 config_get limit "$cfg" limit
379 config_get leasetime "$cfg" leasetime
380 config_get options "$cfg" options
381 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
382
383 leasetime="${leasetime:-12h}"
384 start="$(dhcp_calc "${start:-100}")"
385 limit="${limit:-150}"
386 [ "$limit" -gt 0 ] && limit=$((limit-1))
387 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
388 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
389 xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
390
391 dhcp_option_add "$cfg" "$networkid"
392 }
393
394 dhcp_option_add() {
395 local cfg="$1"
396 local networkid="$2"
397 local force="$3"
398
399 [ "$force" = "0" ] && force=
400
401 config_get dhcp_option "$cfg" dhcp_option
402 for o in $dhcp_option; do
403 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
404 done
405
406 }
407
408 dhcp_domain_add() {
409 local cfg="$1"
410 local ip name names record
411
412 config_get names "$cfg" name "$2"
413 [ -n "$names" ] || return 0
414
415 config_get ip "$cfg" ip "$3"
416 [ -n "$ip" ] || return 0
417
418 for name in $names; do
419 record="${record:+$record }$name"
420 done
421
422 echo "$ip $record" >> $HOSTFILE
423 }
424
425 dhcp_srv_add() {
426 local cfg="$1"
427
428 config_get srv "$cfg" srv
429 [ -n "$srv" ] || return 0
430
431 config_get target "$cfg" target
432 [ -n "$target" ] || return 0
433
434 config_get port "$cfg" port
435 [ -n "$port" ] || return 0
436
437 config_get class "$cfg" class
438 config_get weight "$cfg" weight
439
440 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
441
442 xappend "--srv-host=$service"
443 }
444
445 dhcp_mx_add() {
446 local cfg="$1"
447 local domain relay pref
448
449 config_get domain "$cfg" domain
450 [ -n "$domain" ] || return 0
451
452 config_get relay "$cfg" relay
453 [ -n "$relay" ] || return 0
454
455 config_get pref "$cfg" pref 0
456
457 local service="$domain,$relay,$pref"
458
459 xappend "--mx-host=$service"
460 }
461
462 dhcp_cname_add() {
463 local cfg="$1"
464 local cname target
465
466 config_get cname "$cfg" cname
467 [ -n "$cname" ] || return 0
468
469 config_get target "$cfg" target
470 [ -n "$target" ] || return 0
471
472 xappend "--cname=${cname},${target}"
473 }
474
475 dhcp_hostrecord_add() {
476 local cfg="$1"
477 local names addresses record val
478
479 config_get names "$cfg" name "$2"
480 if [ -z "$names" ]; then
481 return 0
482 fi
483
484 config_get addresses "$cfg" ip "$3"
485 if [ -z "$addresses" ]; then
486 return 0
487 fi
488
489 for val in $names $addresses; do
490 record="${record:+$record,}$val"
491 done
492
493 xappend "--host-record=$record"
494 }
495
496 dhcp_relay_add() {
497 local cfg="$1"
498 local local_addr server_addr interface
499
500 config_get local_addr "$cfg" local_addr
501 [ -n "$local_addr" ] || return 0
502
503 config_get server_addr "$cfg" server_addr
504 [ -n "$server_addr" ] || return 0
505
506 config_get interface "$cfg" interface
507 if [ -z "$interface" ]; then
508 xappend "--dhcp-relay=$local_addr,$server_addr"
509 else
510 xappend "--dhcp-relay=$local_addr,$server_addr,$interface"
511 fi
512 }
513
514 dnsmasq_start()
515 {
516 local cfg="$1" disabled
517
518 config_get_bool disabled "$cfg" disabled 0
519 [ "$disabled" -gt 0 ] && return 0
520
521 # reset list of DOMAINS and DNS servers (for each dnsmasq instance)
522 DNS_SERVERS=""
523 DOMAIN=""
524 CONFIGFILE="${BASECONFIGFILE}.${cfg}"
525 CONFIGFILE_TMP="${CONFIGFILE}.$$"
526 HOSTFILE="${BASEHOSTFILE}.${cfg}"
527 TIMESTAMPFILE="${BASETIMESTAMPFILE}.${cfg}"
528 BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
529
530 # before we can call xappend
531 mkdir -p /var/run/dnsmasq/
532 mkdir -p $(dirname $CONFIGFILE)
533 mkdir -p $(dirname $HOSTFILE)
534 mkdir -p /var/lib/misc
535 chown dnsmasq:dnsmasq /var/run/dnsmasq
536
537 [ -f "$TIMESTAMPFILE" ] && rm -f "$TIMESTAMPFILE"
538
539 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
540 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
541
542 # if we did this last, we could override auto-generated config
543 [ -f /etc/dnsmasq.conf ] && {
544 xappend "--conf-file=/etc/dnsmasq.conf"
545 }
546
547 $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
548
549 append_bool "$cfg" authoritative "--dhcp-authoritative"
550 append_bool "$cfg" nodaemon "--no-daemon"
551 append_bool "$cfg" domainneeded "--domain-needed"
552 append_bool "$cfg" filterwin2k "--filterwin2k"
553 append_bool "$cfg" nohosts "--no-hosts"
554 append_bool "$cfg" nonegcache "--no-negcache"
555 append_bool "$cfg" strictorder "--strict-order"
556 append_bool "$cfg" logqueries "--log-queries=extra"
557 append_bool "$cfg" noresolv "--no-resolv"
558 append_bool "$cfg" localise_queries "--localise-queries"
559 append_bool "$cfg" readethers "--read-ethers"
560 append_bool "$cfg" dbus "--enable-dbus"
561 append_bool "$cfg" boguspriv "--bogus-priv"
562 append_bool "$cfg" expandhosts "--expand-hosts"
563 config_get tftp_root "$cfg" "tftp_root"
564 [ -d "$tftp_root" ] && append_bool "$cfg" enable_tftp "--enable-tftp"
565 append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
566 append_bool "$cfg" nonwildcard "--bind-dynamic"
567 append_bool "$cfg" fqdn "--dhcp-fqdn"
568 append_bool "$cfg" proxydnssec "--proxy-dnssec"
569 append_bool "$cfg" localservice "--local-service"
570 append_bool "$cfg" logdhcp "--log-dhcp"
571 append_bool "$cfg" quietdhcp "--quiet-dhcp"
572 append_bool "$cfg" sequential_ip "--dhcp-sequential-ip"
573 append_bool "$cfg" allservers "--all-servers"
574 append_bool "$cfg" noping "--no-ping"
575
576 append_parm "$cfg" dhcpscript "--dhcp-script"
577 append_parm "$cfg" cachesize "--cache-size"
578 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
579 append_parm "$cfg" port "--port"
580 append_parm "$cfg" ednspacket_max "--edns-packet-max"
581 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
582 append_parm "$cfg" "queryport" "--query-port"
583 append_parm "$cfg" "minport" "--min-port"
584 append_parm "$cfg" "maxport" "--max-port"
585 append_parm "$cfg" "domain" "--domain"
586 append_parm "$cfg" "local" "--server"
587 config_list_foreach "$cfg" "server" append_server
588 config_list_foreach "$cfg" "address" append_address
589 config_list_foreach "$cfg" "ipset" append_ipset
590 config_list_foreach "$cfg" "interface" append_interface
591 config_list_foreach "$cfg" "notinterface" append_notinterface
592 config_list_foreach "$cfg" "addnhosts" append_addnhosts
593 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
594 append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
595 append_parm "$cfg" "resolvfile" "--resolv-file" "/tmp/resolv.conf.auto"
596 append_parm "$cfg" "serversfile" "--servers-file"
597 append_parm "$cfg" "tftp_root" "--tftp-root"
598 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
599 append_parm "$cfg" "local_ttl" "--local-ttl"
600 append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
601 config_list_foreach "$cfg" "pxe_service" append_pxe_service
602 config_get DOMAIN "$cfg" domain
603
604 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
605 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
606
607 config_get_bool readethers "$cfg" readethers
608 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
609
610 config_get resolvfile $cfg resolvfile
611 config_get dhcpscript $cfg dhcpscript
612
613 config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
614 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
615 config_get_bool cachelocal "$cfg" cachelocal 1
616
617 config_get_bool noresolv "$cfg" noresolv 0
618 if [ "$noresolv" != "1" ]; then
619 config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
620 # So jail doesn't complain if file missing
621 [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
622 fi
623
624 config_get hostsfile "$cfg" dhcphostsfile
625 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
626
627 local rebind
628 config_get_bool rebind "$cfg" rebind_protection 1
629 [ $rebind -gt 0 ] && {
630 log_once \
631 "DNS rebinding protection is active," \
632 "will discard upstream RFC1918 responses!"
633 xappend "--stop-dns-rebind"
634
635 local rebind_localhost
636 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
637 [ $rebind_localhost -gt 0 ] && {
638 log_once "Allowing 127.0.0.0/8 responses"
639 xappend "--rebind-localhost-ok"
640 }
641
642 append_rebind_domain() {
643 log_once "Allowing RFC1918 responses for domain $1"
644 xappend "--rebind-domain-ok=$1"
645 }
646
647 config_list_foreach "$cfg" rebind_domain append_rebind_domain
648 }
649
650 config_get_bool dnssec "$cfg" dnssec 0
651 [ "$dnssec" -gt 0 ] && {
652 xappend "--conf-file=$TRUSTANCHORSFILE"
653 xappend "--dnssec"
654 [ -x /etc/init.d/sysntpd ] && {
655 /etc/init.d/sysntpd enabled
656 [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
657 [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
658 }
659 }
660 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
661 }
662
663 dhcp_option_add "$cfg" "" 0
664
665 xappend "--dhcp-broadcast=tag:needs-broadcast"
666
667 xappend "--addn-hosts=$HOSTFILE"
668
669 config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
670 [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
671 xappend "--conf-dir=$dnsmasqconfdir"
672 xappend "--user=dnsmasq"
673 xappend "--group=dnsmasq"
674 echo >> $CONFIGFILE_TMP
675
676 config_get_bool enable_tftp "$cfg" enable_tftp 0
677 [ "$enable_tftp" -gt 0 ] && {
678 config_get tftp_root "$cfg" tftp_root
679 append EXTRA_MOUNT $tftp_root
680 }
681
682 config_foreach filter_dnsmasq host dhcp_host_add "$cfg"
683 echo >> $CONFIGFILE_TMP
684 config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
685 config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
686 config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
687 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
688 config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
689 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
690 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
691 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
692 config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
693 config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
694 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
695 config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
696
697 # add own hostname
698 [ $ADD_LOCAL_HOSTNAME -eq 1 ] && {
699 local lanaddr lanaddr6
700 local ulaprefix="$(uci_get network @globals[0] ula_prefix)"
701 local hostname="$(uci_get system @system[0] hostname Lede)"
702
703 network_get_ipaddr lanaddr "lan" && {
704 dhcp_domain_add "" "$hostname" "$lanaddr"
705 }
706
707 [ -n "$ulaprefix" ] && network_get_ipaddrs6 lanaddr6 "lan" && {
708 for lanaddr6 in $lanaddr6; do
709 case "$lanaddr6" in
710 "${ulaprefix%%:/*}"*)
711 dhcp_domain_add "" "$hostname" "$lanaddr6"
712 ;;
713 esac
714 done
715 }
716 }
717
718 echo >> $CONFIGFILE_TMP
719 config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
720 config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
721 echo >> $CONFIGFILE_TMP
722
723 config_get odhcpd_is_active odhcpd maindhcp
724 if [ "$odhcpd_is_active" != "1" ]; then
725 config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
726 fi
727
728 echo >> $CONFIGFILE_TMP
729 config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
730 echo >> $CONFIGFILE_TMP
731
732 echo >> $CONFIGFILE_TMP
733 mv -f $CONFIGFILE_TMP $CONFIGFILE
734
735 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
736 rm -f /tmp/resolv.conf
737 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
738 echo "search $DOMAIN" >> /tmp/resolv.conf
739 }
740 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
741 for DNS_SERVER in $DNS_SERVERS ; do
742 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
743 done
744 }
745
746 procd_open_instance $cfg
747 procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
748 procd_set_param file $CONFIGFILE
749 procd_set_param respawn
750
751 local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
752 if [ ! -r "$dnsmasqconffile" ]; then
753 dnsmasqconffile=/etc/dnsmasq.conf
754 fi
755
756 procd_add_jail dnsmasq ubus log
757 procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $dhcpscript /etc/hosts /etc/ethers $EXTRA_MOUNT
758 procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
759
760 procd_close_instance
761 }
762
763 dnsmasq_stop()
764 {
765 local cfg="$1"
766
767 config_get resolvfile "$cfg" "resolvfile"
768
769 #relink /tmp/resolve.conf only for main instance
770 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
771 [ -f /tmp/resolv.conf ] && {
772 rm -f /tmp/resolv.conf
773 ln -s "$resolvfile" /tmp/resolv.conf
774 }
775 }
776
777 rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp
778 }
779
780 service_triggers()
781 {
782 procd_add_reload_trigger "dhcp"
783 procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
784 }
785
786 boot()
787 {
788 BOOT=1
789 start "$@"
790 }
791
792 start_service() {
793 local instance="$1"
794 local instance_found=0
795
796 [ -n "$BOOT" ] && return
797
798 . /lib/functions/network.sh
799
800 config_cb() {
801 local type="$1"
802 local name="$2"
803 if [ "$type" = "dnsmasq" ]; then
804 if [ -n "$instance" -a "$instance" = "$name" ]; then
805 instance_found=1
806 fi
807 fi
808 }
809
810 config_load dhcp
811
812 if [ -n "$instance" ]; then
813 [ "$instance_found" -gt 0 ] || return
814 dnsmasq_start "$instance"
815 else
816 config_foreach dnsmasq_start dnsmasq
817 fi
818 }
819
820 reload_service() {
821 rc_procd start_service "$@"
822 return 0
823 }
824
825 stop_service() {
826 local instance="$1"
827 local instance_found=0
828
829 config_cb() {
830 local type="$1"
831 local name="$2"
832 if [ "$type" = "dnsmasq" ]; then
833 if [ -n "$instance" -a "$instance" = "$name" ]; then
834 instance_found=1
835 fi
836 fi
837 }
838
839 config_load dhcp
840
841 if [ -n "$instance" ]; then
842 [ "$instance_found" -gt 0 ] || return
843 dnsmasq_stop "$instance"
844 else
845 config_foreach dnsmasq_stop dnsmasq
846 fi
847 }