dnsmasq: rewrite config on host name modification
[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 ADD_WAN_FQDN=0
12 ADD_LOCAL_FQDN=""
13
14 BASECONFIGFILE="/var/etc/dnsmasq.conf"
15 BASEHOSTFILE="/tmp/hosts/dhcp"
16 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
17 TIMEVALIDFILE="/var/state/dnsmasqsec"
18 BASEDHCPSTAMPFILE="/var/run/dnsmasq"
19 RFC6761FILE="/usr/share/dnsmasq/rfc6761.conf"
20 DHCPSCRIPT="/usr/lib/dnsmasq/dhcp-script.sh"
21
22 DNSMASQ_DHCP_VER=4
23
24 xappend() {
25 local value="$1"
26
27 echo "${value#--}" >> $CONFIGFILE_TMP
28 }
29
30 hex_to_hostid() {
31 local var="$1"
32 local hex="${2#0x}" # strip optional "0x" prefix
33
34 if [ -n "${hex//[0-9a-fA-F]/}" ]; then
35 # is invalid hex literal
36 return 1
37 fi
38
39 # convert into host id
40 export "$var=$(
41 printf "%0x:%0x" \
42 $(((0x$hex >> 16) % 65536)) \
43 $(( 0x$hex % 65536))
44 )"
45
46 return 0
47 }
48
49 dhcp_calc() {
50 local ip="$1"
51 local res=0
52
53 while [ -n "$ip" ]; do
54 part="${ip%%.*}"
55 res="$(($res * 256))"
56 res="$(($res + $part))"
57 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
58 done
59 echo "$res"
60 }
61
62 dhcp_check() {
63 local ifname="$1"
64 local stamp="${BASEDHCPSTAMPFILE_CFG}.${ifname}.dhcp"
65 local rv=0
66
67 [ -s "$stamp" ] && return $(cat "$stamp")
68
69 # If there's no carrier yet, skip this interface.
70 # The init script will be called again once the link is up
71 case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
72 false) return 1;;
73 esac
74
75 udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
76
77 [ $rv -eq 1 ] && \
78 logger -t dnsmasq \
79 "found already running DHCP-server on interface '$ifname'" \
80 "refusing to start, use 'option force 1' to override"
81
82 echo $rv > "$stamp"
83 return $rv
84 }
85
86 log_once() {
87 pidof dnsmasq >/dev/null || \
88 logger -t dnsmasq "$@"
89 }
90
91 has_handler() {
92 local file
93
94 for file in /etc/hotplug.d/dhcp/* /etc/hotplug.d/tftp/* /etc/hotplug.d/neigh/*; do
95 [ -f "$file" ] && return 0
96 done
97
98 return 1
99 }
100
101 append_bool() {
102 local section="$1"
103 local option="$2"
104 local value="$3"
105 local default="$4"
106 local _loctmp
107 [ -z "$default" ] && default="0"
108 config_get_bool _loctmp "$section" "$option" "$default"
109 [ $_loctmp -gt 0 ] && xappend "$value"
110 }
111
112 append_parm() {
113 local section="$1"
114 local option="$2"
115 local switch="$3"
116 local default="$4"
117 local _loctmp
118 config_get _loctmp "$section" "$option" "$default"
119 [ -z "$_loctmp" ] && return 0
120 xappend "$switch=$_loctmp"
121 }
122
123 append_server() {
124 xappend "--server=$1"
125 }
126
127 append_rev_server() {
128 xappend "--rev-server=$1"
129 }
130
131 append_address() {
132 xappend "--address=$1"
133 }
134
135 append_ipset() {
136 xappend "--ipset=$1"
137 }
138
139 append_interface() {
140 network_get_device ifname "$1" || ifname="$1"
141 xappend "--interface=$ifname"
142 }
143
144 append_listenaddress() {
145 xappend "--listen-address=$1"
146 }
147
148 append_notinterface() {
149 network_get_device ifname "$1" || ifname="$1"
150 xappend "--except-interface=$ifname"
151 }
152
153 append_addnhosts() {
154 xappend "--addn-hosts=$1"
155 }
156
157 append_bogusnxdomain() {
158 xappend "--bogus-nxdomain=$1"
159 }
160
161 append_pxe_service() {
162 xappend "--pxe-service=$1"
163 }
164
165 append_interface_name() {
166 xappend "--interface-name=$1,$2"
167 }
168
169 filter_dnsmasq() {
170 local cfg="$1" func="$2" match_cfg="$3" found_cfg
171
172 # use entry when no instance entry set, or if it matches
173 config_get found_cfg "$cfg" "instance"
174 if [ -z "$found_cfg" -o "$found_cfg" = "$match_cfg" ]; then
175 $func $cfg
176 fi
177 }
178
179 dhcp_subscrid_add() {
180 local cfg="$1"
181
182 config_get networkid "$cfg" networkid
183 [ -n "$networkid" ] || return 0
184
185 config_get subscriberid "$cfg" subscriberid
186 [ -n "$subscriberid" ] || return 0
187
188 xappend "--dhcp-subscrid=$networkid,$subscriberid"
189
190 config_get_bool force "$cfg" force 0
191
192 dhcp_option_add "$cfg" "$networkid" "$force"
193 }
194
195 dhcp_remoteid_add() {
196 local cfg="$1"
197
198 config_get networkid "$cfg" networkid
199 [ -n "$networkid" ] || return 0
200
201 config_get remoteid "$cfg" remoteid
202 [ -n "$remoteid" ] || return 0
203
204 xappend "--dhcp-remoteid=$networkid,$remoteid"
205
206 config_get_bool force "$cfg" force 0
207
208 dhcp_option_add "$cfg" "$networkid" "$force"
209 }
210
211 dhcp_circuitid_add() {
212 # TODO: DHCPV6 does not have circuitid; catch "option6:"
213 local cfg="$1"
214
215 config_get networkid "$cfg" networkid
216 [ -n "$networkid" ] || return 0
217
218 config_get circuitid "$cfg" circuitid
219 [ -n "$circuitid" ] || return 0
220
221 xappend "--dhcp-circuitid=$networkid,$circuitid"
222
223 config_get_bool force "$cfg" force 0
224
225 dhcp_option_add "$cfg" "$networkid" "$force"
226 }
227
228 dhcp_userclass_add() {
229 local cfg="$1"
230
231 config_get networkid "$cfg" networkid
232 [ -n "$networkid" ] || return 0
233
234 config_get userclass "$cfg" userclass
235 [ -n "$userclass" ] || return 0
236
237 xappend "--dhcp-userclass=$networkid,$userclass"
238
239 config_get_bool force "$cfg" force 0
240
241 dhcp_option_add "$cfg" "$networkid" "$force"
242 }
243
244 dhcp_vendorclass_add() {
245 # TODO: DHCPV6 vendor class has stricter definitions; catch? fixup?
246 local cfg="$1"
247
248 config_get networkid "$cfg" networkid
249 [ -n "$networkid" ] || return 0
250
251 config_get vendorclass "$cfg" vendorclass
252 [ -n "$vendorclass" ] || return 0
253
254 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
255
256 config_get_bool force "$cfg" force 0
257
258 dhcp_option_add "$cfg" "$networkid" "$force"
259 }
260
261 dhcp_match_add() {
262 local cfg="$1"
263
264 config_get networkid "$cfg" networkid
265 [ -n "$networkid" ] || return 0
266
267 config_get match "$cfg" match
268 [ -n "$match" ] || return 0
269
270 xappend "--dhcp-match=$networkid,$match"
271
272 config_get_bool force "$cfg" force 0
273
274 dhcp_option_add "$cfg" "$networkid" "$force"
275 }
276
277 dhcp_host_add() {
278 local cfg="$1"
279 local hosttag nametime addrs duids macs tags
280
281 config_get_bool force "$cfg" force 0
282
283 config_get networkid "$cfg" networkid
284 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
285
286 config_get_bool enable "$cfg" enable 1
287 [ "$enable" = "0" ] && return 0
288
289 config_get name "$cfg" name
290 config_get ip "$cfg" ip
291 config_get hostid "$cfg" hostid
292
293 [ -n "$ip" -o -n "$name" -o -n "$hostid" ] || return 0
294
295 config_get_bool dns "$cfg" dns 0
296 [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
297 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE_TMP
298 }
299
300 config_get mac "$cfg" mac
301 config_get duid "$cfg" duid
302 config_get tag "$cfg" tag
303
304 if [ -n "$mac" ]; then
305 # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
306 # many MAC are possible to track a laptop ON/OFF dock
307 for m in $mac; do append macs "$m" ","; done
308 fi
309
310 if [ $DNSMASQ_DHCP_VER -eq 6 -a -n "$duid" ]; then
311 # --dhcp-host=id:00:03:00:01:12:00:00:01:02:03,[::beef],lap
312 # one (virtual) machine gets one DUID per RFC3315
313 duids="id:${duid// */}"
314 fi
315
316 if [ -z "$macs" -a -z "$duids" ]; then
317 # --dhcp-host=lap,192.168.0.199,[::beef]
318 [ -n "$name" ] || return 0
319 macs="$name"
320 name=""
321 fi
322
323 if [ -n "$hostid" ]; then
324 hex_to_hostid hostid "$hostid"
325 fi
326
327 if [ -n "$tag" ]; then
328 for t in $tag; do append tags "$t" ",set:"; done
329 fi
330
331 config_get_bool broadcast "$cfg" broadcast 0
332 config_get leasetime "$cfg" leasetime
333
334 [ "$broadcast" = "0" ] && broadcast= || broadcast=",set:needs-broadcast"
335
336 hosttag="${networkid:+,set:${networkid}}${tags:+,set:${tags}}$broadcast"
337 nametime="${name:+,$name}${leasetime:+,$leasetime}"
338
339 if [ $DNSMASQ_DHCP_VER -eq 6 ]; then
340 addrs="${ip:+,$ip}${hostid:+,[::$hostid]}"
341 xappend "--dhcp-host=$macs${duids:+,$duids}$hosttag$addrs$nametime"
342 else
343 xappend "--dhcp-host=$macs$hosttag${ip:+,$ip}$nametime"
344 fi
345 }
346
347 dhcp_this_host_add() {
348 local net="$1"
349 local ifname="$2"
350 local mode="$3"
351 local routerstub routername ifdashname
352 local lanaddr lanaddr6 lanaddrs6 ulaprefix
353
354 if [ "$mode" -gt 0 ] ; then
355 ifdashname="${ifname//./-}"
356 routerstub="$( md5sum /etc/os-release )"
357 routerstub="router-${routerstub// */}"
358 routername="$( uci_get system @system[0] hostname $routerstub )"
359
360 if [ "$mode" -gt 1 ] ; then
361 if [ "$mode" -gt 2 ] ; then
362 if [ "$mode" -gt 3 ] ; then
363 append_interface_name "$ifdashname.$routername.$DOMAIN" "$ifname"
364 fi
365
366 append_interface_name "$routername.$DOMAIN" "$ifname"
367 fi
368
369 # All IP addresses discovered by dnsmasq will be labeled (except fe80::)
370 append_interface_name "$routername" "$ifname"
371
372 else
373 # This uses a static host file entry for only limited addresses.
374 # Use dnsmasq option "--expandhosts" to enable FQDN on host files.
375 ulaprefix="$(uci_get network @globals[0] ula_prefix)"
376 network_get_ipaddr lanaddr "$net"
377 network_get_ipaddrs6 lanaddrs6 "$net"
378
379 if [ -n "$lanaddr" ] ; then
380 dhcp_domain_add "" "$routername" "$lanaddr"
381 fi
382
383 if [ -n "$ulaprefix" -a -n "$lanaddrs6" ] ; then
384 for lanaddr6 in $lanaddrs6 ; do
385 case "$lanaddr6" in
386 "${ulaprefix%%:/*}"*)
387 dhcp_domain_add "" "$routername" "$lanaddr6"
388 ;;
389 esac
390 done
391 fi
392 fi
393 fi
394 }
395
396 dhcp_tag_add() {
397 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
398 local cfg="$1"
399
400 tag="$cfg"
401
402 [ -n "$tag" ] || return 0
403
404 config_get_bool force "$cfg" force 0
405 [ "$force" = "0" ] && force=
406
407 config_get option "$cfg" dhcp_option
408 for o in $option; do
409 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
410 done
411 }
412
413 dhcp_mac_add() {
414 local cfg="$1"
415
416 config_get networkid "$cfg" networkid
417 [ -n "$networkid" ] || return 0
418
419 config_get mac "$cfg" mac
420 [ -n "$mac" ] || return 0
421
422 xappend "--dhcp-mac=$networkid,$mac"
423
424 dhcp_option_add "$cfg" "$networkid"
425 }
426
427 dhcp_boot_add() {
428 # TODO: BOOTURL is different between DHCPv4 and DHCPv6
429 local cfg="$1"
430
431 config_get networkid "$cfg" networkid
432
433 config_get filename "$cfg" filename
434 [ -n "$filename" ] || return 0
435
436 config_get servername "$cfg" servername
437 config_get serveraddress "$cfg" serveraddress
438
439 [ -n "$serveraddress" -a ! -n "$servername" ] && return 0
440
441 xappend "--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
442
443 config_get_bool force "$cfg" force 0
444
445 dhcp_option_add "$cfg" "$networkid" "$force"
446 }
447
448
449 dhcp_add() {
450 local cfg="$1"
451 local dhcp6range="::"
452 local nettag
453 local tags
454
455 config_get net "$cfg" interface
456 [ -n "$net" ] || return 0
457
458 config_get networkid "$cfg" networkid
459 [ -n "$networkid" ] || networkid="$net"
460
461 network_get_device ifname "$net" || return 0
462
463 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
464 DNS_SERVERS="$DNS_SERVERS $dnsserver"
465 }
466
467 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && {
468 # Many ISP do not have useful names for DHCP customers (your WAN).
469 dhcp_this_host_add "$net" "$ifname" "$ADD_WAN_FQDN"
470 return 0
471 }
472
473 network_get_subnet subnet "$net" || return 0
474 network_get_protocol proto "$net" || return 0
475
476 # Do not support non-static interfaces for now
477 [ static = "$proto" ] || return 0
478
479 # Override interface netmask with dhcp config if applicable
480 config_get netmask "$cfg" netmask "${subnet##*/}"
481
482 #check for an already active dhcp server on the interface, unless 'force' is set
483 config_get_bool force "$cfg" force 0
484 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
485
486 config_get start "$cfg" start 100
487 config_get limit "$cfg" limit 150
488 config_get leasetime "$cfg" leasetime 12h
489 config_get options "$cfg" options
490 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
491
492 config_get dhcpv4 "$cfg" dhcpv4
493 config_get dhcpv6 "$cfg" dhcpv6
494
495 config_get ra "$cfg" ra
496 config_get ra_management "$cfg" ra_management
497 config_get ra_preference "$cfg" ra_preference
498 config_get dns "$cfg" dns
499
500 config_list_foreach "$cfg" "interface_name" append_interface_name "$ifname"
501
502 # Put the router host name on this DHCP served interface address(es)
503 dhcp_this_host_add "$net" "$ifname" "$ADD_LOCAL_FQDN"
504
505 start="$( dhcp_calc "$start" )"
506
507 add_tag() {
508 tags="${tags}tag:$1,"
509 }
510 config_list_foreach "$cfg" tag add_tag
511
512 nettag="${networkid:+set:${networkid},}"
513
514 if [ "$limit" -gt 0 ] ; then
515 limit=$((limit-1))
516 fi
517
518 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
519
520 if [ "$dynamicdhcp" = "0" ] ; then
521 END="static"
522 dhcp6range="::,static"
523 else
524 dhcp6range="::1000,::ffff"
525 fi
526
527
528 if [ "$dhcpv4" != "disabled" ] ; then
529 xappend "--dhcp-range=$tags$nettag$START,$END,$NETMASK,$leasetime${options:+ $options}"
530 fi
531
532
533 if [ $DNSMASQ_DHCP_VER -eq 6 -a "$ra" = "server" ] ; then
534 # Note: dnsmasq cannot just be a DHCPv6 server (all-in-1)
535 # and let some other machine(s) send RA pointing to it.
536
537 case $ra_preference in
538 *high*)
539 xappend "--ra-param=$ifname,high,0,7200"
540 ;;
541 *low*)
542 xappend "--ra-param=$ifname,low,0,7200"
543 ;;
544 *)
545 # Send UNSOLICITED RA at default interval and live for 2 hours.
546 # TODO: convert flexible lease time into route life time (only seconds).
547 xappend "--ra-param=$ifname,0,7200"
548 ;;
549 esac
550
551 if [ "$dhcpv6" = "disabled" ] ; then
552 ra_management="3"
553 fi
554
555
556 case $ra_management in
557 0)
558 # SLACC with DCHP for extended options
559 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-stateless,ra-names"
560 ;;
561 2)
562 # DHCP address and RA only for management redirection
563 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,$leasetime"
564 ;;
565 3)
566 # SLAAC only but dnsmasq attempts to link HOSTNAME, DHCPv4 MAC, and SLAAC
567 xappend "--dhcp-range=$nettag::,constructor:$ifname,ra-only,ra-names"
568 ;;
569 *)
570 # SLAAC and full DHCP
571 xappend "--dhcp-range=$nettag$dhcp6range,constructor:$ifname,slaac,ra-names,$leasetime"
572 ;;
573 esac
574
575 if [ -n "$dns" ]; then
576 dnss=""
577 for d in $dns; do append dnss "[$d]" ","; done
578 else
579 dnss="[::]"
580 fi
581
582 dhcp_option_append "option6:dns-server,$dnss" "$networkid"
583 fi
584
585 dhcp_option_add "$cfg" "$networkid" 0
586 dhcp_option_add "$cfg" "$networkid" 2
587 }
588
589 dhcp_option_append() {
590 local option="$1"
591 local networkid="$2"
592 local force="$3"
593
594 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$option"
595 }
596
597 dhcp_option_add() {
598 # NOTE: dnsmasq has explicit "option6:" prefix for DHCPv6 so no collisions
599 local cfg="$1"
600 local networkid="$2"
601 local force="$3"
602 local opt="dhcp_option"
603
604 [ "$force" = "0" ] && force=
605 [ "$force" = "2" ] && opt="dhcp_option_force"
606
607 local list_len
608 config_get list_len "$cfg" "${opt}_LENGTH"
609
610 if [ -n "$list_len" ]; then
611 config_list_foreach "$cfg" "$opt" dhcp_option_append "$networkid" "$force"
612 else
613 config_get dhcp_option "$cfg" "$opt"
614
615 [ -n "$dhcp_option" ] && echo "Warning: the 'option $opt' syntax is deprecated, use 'list $opt'" >&2
616
617 local option
618 for option in $dhcp_option; do
619 dhcp_option_append "$option" "$networkid" "$force"
620 done
621 fi
622 }
623
624 dhcp_domain_add() {
625 local cfg="$1"
626 local ip name names record
627
628 config_get names "$cfg" name "$2"
629 [ -n "$names" ] || return 0
630
631 config_get ip "$cfg" ip "$3"
632 [ -n "$ip" ] || return 0
633
634 for name in $names; do
635 record="${record:+$record }$name"
636 done
637
638 echo "$ip $record" >> $HOSTFILE_TMP
639 }
640
641 dhcp_srv_add() {
642 local cfg="$1"
643
644 config_get srv "$cfg" srv
645 [ -n "$srv" ] || return 0
646
647 config_get target "$cfg" target
648 [ -n "$target" ] || return 0
649
650 config_get port "$cfg" port
651 [ -n "$port" ] || return 0
652
653 config_get class "$cfg" class
654 config_get weight "$cfg" weight
655
656 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
657
658 xappend "--srv-host=$service"
659 }
660
661 dhcp_mx_add() {
662 local cfg="$1"
663 local domain relay pref
664
665 config_get domain "$cfg" domain
666 [ -n "$domain" ] || return 0
667
668 config_get relay "$cfg" relay
669 [ -n "$relay" ] || return 0
670
671 config_get pref "$cfg" pref 0
672
673 local service="$domain,$relay,$pref"
674
675 xappend "--mx-host=$service"
676 }
677
678 dhcp_cname_add() {
679 local cfg="$1"
680 local cname target
681
682 config_get cname "$cfg" cname
683 [ -n "$cname" ] || return 0
684
685 config_get target "$cfg" target
686 [ -n "$target" ] || return 0
687
688 xappend "--cname=${cname},${target}"
689 }
690
691 dhcp_hostrecord_add() {
692 local cfg="$1"
693 local names addresses record val
694
695 config_get names "$cfg" name "$2"
696 if [ -z "$names" ]; then
697 return 0
698 fi
699
700 config_get addresses "$cfg" ip "$3"
701 if [ -z "$addresses" ]; then
702 return 0
703 fi
704
705 for val in $names $addresses; do
706 record="${record:+$record,}$val"
707 done
708
709 xappend "--host-record=$record"
710 }
711
712 dhcp_relay_add() {
713 local cfg="$1"
714 local local_addr server_addr interface
715
716 config_get local_addr "$cfg" local_addr
717 [ -n "$local_addr" ] || return 0
718
719 config_get server_addr "$cfg" server_addr
720 [ -n "$server_addr" ] || return 0
721
722 config_get interface "$cfg" interface
723 if [ -z "$interface" ]; then
724 xappend "--dhcp-relay=$local_addr,$server_addr"
725 else
726 network_get_device ifname "$interface" || return
727 xappend "--dhcp-relay=$local_addr,$server_addr,$ifname"
728 fi
729 }
730
731 dnsmasq_start()
732 {
733 local cfg="$1" disabled resolvfile user_dhcpscript
734
735 config_get_bool disabled "$cfg" disabled 0
736 [ "$disabled" -gt 0 ] && return 0
737
738 # reset list of DOMAINS and DNS servers (for each dnsmasq instance)
739 DNS_SERVERS=""
740 DOMAIN=""
741 CONFIGFILE="${BASECONFIGFILE}.${cfg}"
742 CONFIGFILE_TMP="${CONFIGFILE}.$$"
743 HOSTFILE="${BASEHOSTFILE}.${cfg}"
744 HOSTFILE_TMP="${HOSTFILE}.$$"
745 BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
746
747 # before we can call xappend
748 mkdir -p /var/run/dnsmasq/
749 mkdir -p $(dirname $CONFIGFILE)
750 mkdir -p $(dirname $HOSTFILE)
751 mkdir -p /var/lib/misc
752 chown dnsmasq:dnsmasq /var/run/dnsmasq
753
754 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
755 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE_TMP
756
757 local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
758 if [ ! -r "$dnsmasqconffile" ]; then
759 dnsmasqconffile=/etc/dnsmasq.conf
760 fi
761
762 # if we did this last, we could override auto-generated config
763 [ -f "${dnsmasqconffile}" ] && {
764 xappend "--conf-file=${dnsmasqconffile}"
765 }
766
767 $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
768
769
770 if [ -x /usr/sbin/odhcpd -a -x /etc/init.d/odhcpd ] ; then
771 local odhcpd_is_main odhcpd_is_enabled
772 config_get odhcpd_is_main odhcpd maindhcp 0
773 /etc/init.d/odhcpd enabled && odhcpd_is_enabled=1 || odhcpd_is_enabled=0
774
775
776 if [ "$odhcpd_is_enabled" -eq 0 -a "$DHCPv6CAPABLE" -eq 1 ] ; then
777 # DHCP V4 and V6 in DNSMASQ
778 DNSMASQ_DHCP_VER=6
779 elif [ "$odhcpd_is_main" -gt 0 ] ; then
780 # ODHCPD is doing it all
781 DNSMASQ_DHCP_VER=0
782 else
783 # You have ODHCPD but use DNSMASQ for DHCPV4
784 DNSMASQ_DHCP_VER=4
785 fi
786
787 elif [ "$DHCPv6CAPABLE" -eq 1 ] ; then
788 # DHCP V4 and V6 in DNSMASQ
789 DNSMASQ_DHCP_VER=6
790 else
791 DNSMASQ_DHCP_VER=4
792 fi
793
794 # Allow DHCP/DHCPv6 to be handled by ISC DHCPD
795 if [ -x /usr/sbin/dhcpd ] ; then
796 if [ -x /etc/init.d/dhcpd ] ; then
797 /etc/init.d/dhcpd enabled && DNSMASQ_DHCP_VER=0
798 fi
799 if [ -x /etc/init.d/dhcpd6 -a "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
800 /etc/init.d/dhcpd6 enabled && DNSMASQ_DHCP_VER=4
801 fi
802 fi
803
804 append_bool "$cfg" authoritative "--dhcp-authoritative"
805 append_bool "$cfg" nodaemon "--no-daemon"
806 append_bool "$cfg" domainneeded "--domain-needed"
807 append_bool "$cfg" filterwin2k "--filterwin2k"
808 append_bool "$cfg" nohosts "--no-hosts"
809 append_bool "$cfg" nonegcache "--no-negcache"
810 append_bool "$cfg" strictorder "--strict-order"
811 append_bool "$cfg" logqueries "--log-queries=extra"
812 append_bool "$cfg" noresolv "--no-resolv"
813 append_bool "$cfg" localise_queries "--localise-queries"
814 append_bool "$cfg" readethers "--read-ethers"
815 append_bool "$cfg" dbus "--enable-dbus"
816 append_bool "$cfg" expandhosts "--expand-hosts"
817 config_get tftp_root "$cfg" "tftp_root"
818 [ -n "$tftp_root" ] && mkdir -p "$tftp_root" && append_bool "$cfg" enable_tftp "--enable-tftp"
819 append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
820 append_bool "$cfg" nonwildcard "--bind-dynamic" 1
821 append_bool "$cfg" fqdn "--dhcp-fqdn"
822 append_bool "$cfg" proxydnssec "--proxy-dnssec"
823 append_bool "$cfg" localservice "--local-service"
824 append_bool "$cfg" logdhcp "--log-dhcp"
825 append_bool "$cfg" quietdhcp "--quiet-dhcp"
826 append_bool "$cfg" sequential_ip "--dhcp-sequential-ip"
827 append_bool "$cfg" allservers "--all-servers"
828 append_bool "$cfg" noping "--no-ping"
829
830 append_parm "$cfg" logfacility "--log-facility"
831
832 append_parm "$cfg" cachesize "--cache-size"
833 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
834 append_parm "$cfg" port "--port"
835 append_parm "$cfg" ednspacket_max "--edns-packet-max"
836 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
837 append_parm "$cfg" "queryport" "--query-port"
838 append_parm "$cfg" "minport" "--min-port"
839 append_parm "$cfg" "maxport" "--max-port"
840 append_parm "$cfg" "domain" "--domain"
841 append_parm "$cfg" "local" "--server"
842 config_list_foreach "$cfg" "listen_address" append_listenaddress
843 config_list_foreach "$cfg" "server" append_server
844 config_list_foreach "$cfg" "rev_server" append_rev_server
845 config_list_foreach "$cfg" "address" append_address
846 config_list_foreach "$cfg" "ipset" append_ipset
847 config_list_foreach "$cfg" "interface" append_interface
848 config_list_foreach "$cfg" "notinterface" append_notinterface
849 config_list_foreach "$cfg" "addnhosts" append_addnhosts
850 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
851 append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
852 append_parm "$cfg" "serversfile" "--servers-file"
853 append_parm "$cfg" "tftp_root" "--tftp-root"
854 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
855 append_parm "$cfg" "local_ttl" "--local-ttl"
856 append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
857 config_list_foreach "$cfg" "pxe_service" append_pxe_service
858 config_get DOMAIN "$cfg" domain
859
860 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
861 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
862 config_get ADD_LOCAL_FQDN "$cfg" add_local_fqdn ""
863 config_get ADD_WAN_FQDN "$cfg" add_wan_fqdn 0
864
865 if [ -z "$ADD_LOCAL_FQDN" ] ; then
866 # maintain support for previous UCI
867 ADD_LOCAL_FQDN="$ADD_LOCAL_HOSTNAME"
868 fi
869
870 config_get_bool readethers "$cfg" readethers
871 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
872
873 config_get user_dhcpscript $cfg dhcpscript
874 if has_handler || [ -n "$user_dhcpscript" ]; then
875 xappend "--dhcp-script=$DHCPSCRIPT"
876 fi
877
878 config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
879 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
880 config_get_bool cachelocal "$cfg" cachelocal 1
881
882 config_get_bool noresolv "$cfg" noresolv 0
883 if [ "$noresolv" != "1" ]; then
884 config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
885 # So jail doesn't complain if file missing
886 [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
887 fi
888
889 [ -n "$resolvfile" ] && xappend "--resolv-file=$resolvfile"
890
891 config_get hostsfile "$cfg" dhcphostsfile
892 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
893
894 local rebind
895 config_get_bool rebind "$cfg" rebind_protection 1
896 [ $rebind -gt 0 ] && {
897 log_once \
898 "DNS rebinding protection is active," \
899 "will discard upstream RFC1918 responses!"
900 xappend "--stop-dns-rebind"
901
902 local rebind_localhost
903 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
904 [ $rebind_localhost -gt 0 ] && {
905 log_once "Allowing 127.0.0.0/8 responses"
906 xappend "--rebind-localhost-ok"
907 }
908
909 append_rebind_domain() {
910 log_once "Allowing RFC1918 responses for domain $1"
911 xappend "--rebind-domain-ok=$1"
912 }
913
914 config_list_foreach "$cfg" rebind_domain append_rebind_domain
915 }
916
917 config_get_bool dnssec "$cfg" dnssec 0
918 [ "$dnssec" -gt 0 ] && {
919 xappend "--conf-file=$TRUSTANCHORSFILE"
920 xappend "--dnssec"
921 [ -x /etc/init.d/sysntpd ] && {
922 /etc/init.d/sysntpd enabled
923 [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
924 [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
925 }
926 }
927 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
928 }
929
930 config_get addmac "$cfg" addmac 0
931 [ "$addmac" != "0" ] && {
932 [ "$addmac" = "1" ] && addmac=
933 xappend "--add-mac${addmac:+="$addmac"}"
934 }
935
936 dhcp_option_add "$cfg" "" 0
937 dhcp_option_add "$cfg" "" 2
938
939 xappend "--dhcp-broadcast=tag:needs-broadcast"
940
941 xappend "--addn-hosts=$(dirname $HOSTFILE)"
942
943 config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
944 [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
945 xappend "--conf-dir=$dnsmasqconfdir"
946 xappend "--user=dnsmasq"
947 xappend "--group=dnsmasq"
948 echo >> $CONFIGFILE_TMP
949
950 config_get_bool enable_tftp "$cfg" enable_tftp 0
951 [ "$enable_tftp" -gt 0 ] && {
952 config_get tftp_root "$cfg" tftp_root
953 append EXTRA_MOUNT $tftp_root
954 }
955
956 config_foreach filter_dnsmasq host dhcp_host_add "$cfg"
957 echo >> $CONFIGFILE_TMP
958 config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
959 config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
960 config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
961 config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
962 config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
963 config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
964 config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
965 config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
966 config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
967 config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
968 config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
969 config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
970
971 echo >> $CONFIGFILE_TMP
972 config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
973 config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
974 echo >> $CONFIGFILE_TMP
975
976 config_get_bool boguspriv "$cfg" boguspriv 1
977 [ "$boguspriv" -gt 0 ] && {
978 xappend "--bogus-priv"
979 [ -r "$RFC6761FILE" ] && xappend "--conf-file=$RFC6761FILE"
980 }
981
982 if [ "$DNSMASQ_DHCP_VER" -gt 4 ] ; then
983 # Enable RA feature for when/if it is constructed,
984 # and RA is selected per interface pool (RA, DHCP, or both),
985 # but no one (should) want RA broadcast in syslog
986 config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
987 xappend "--enable-ra"
988 xappend "--quiet-ra"
989 append_bool "$cfg" quietdhcp "--quiet-dhcp6"
990
991 elif [ "$DNSMASQ_DHCP_VER" -gt 0 ] ; then
992 config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
993 fi
994
995
996 echo >> $CONFIGFILE_TMP
997 config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
998 echo >> $CONFIGFILE_TMP
999
1000 echo >> $CONFIGFILE_TMP
1001 mv -f $CONFIGFILE_TMP $CONFIGFILE
1002 mv -f $HOSTFILE_TMP $HOSTFILE
1003
1004 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
1005 rm -f /tmp/resolv.conf
1006 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
1007 echo "search $DOMAIN" >> /tmp/resolv.conf
1008 }
1009 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
1010 for DNS_SERVER in $DNS_SERVERS ; do
1011 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
1012 done
1013 }
1014
1015 procd_open_instance $cfg
1016 procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
1017 procd_set_param file $CONFIGFILE
1018 [ -n "$user_dhcpscript" ] && procd_set_param env USER_DHCPSCRIPT="$user_dhcpscript"
1019 procd_set_param respawn
1020
1021 procd_add_jail dnsmasq ubus log
1022 procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE $RFC6761FILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $user_dhcpscript /etc/hosts /etc/ethers /sbin/hotplug-call $EXTRA_MOUNT $DHCPSCRIPT
1023 procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
1024
1025 procd_close_instance
1026 }
1027
1028 dnsmasq_stop()
1029 {
1030 local cfg="$1"
1031
1032 config_get resolvfile "$cfg" "resolvfile"
1033
1034 #relink /tmp/resolve.conf only for main instance
1035 [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
1036 [ -f /tmp/resolv.conf ] && {
1037 rm -f /tmp/resolv.conf
1038 ln -s "$resolvfile" /tmp/resolv.conf
1039 }
1040 }
1041
1042 rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp
1043 }
1044
1045 service_triggers()
1046 {
1047 procd_add_reload_trigger "dhcp" "system"
1048 procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
1049 }
1050
1051 boot()
1052 {
1053 BOOT=1
1054 start "$@"
1055 }
1056
1057 start_service() {
1058 local instance="$1"
1059 local instance_found=0
1060
1061 [ -n "$BOOT" ] && return
1062
1063 . /lib/functions/network.sh
1064
1065 config_cb() {
1066 local type="$1"
1067 local name="$2"
1068 if [ "$type" = "dnsmasq" ]; then
1069 if [ -n "$instance" -a "$instance" = "$name" ]; then
1070 instance_found=1
1071 fi
1072 fi
1073 }
1074
1075 config_load dhcp
1076
1077 if [ -n "$instance" ]; then
1078 [ "$instance_found" -gt 0 ] || return
1079 dnsmasq_start "$instance"
1080 else
1081 config_foreach dnsmasq_start dnsmasq
1082 fi
1083 }
1084
1085 reload_service() {
1086 rc_procd start_service "$@"
1087 return 0
1088 }
1089
1090 stop_service() {
1091 local instance="$1"
1092 local instance_found=0
1093
1094 config_cb() {
1095 local type="$1"
1096 local name="$2"
1097 if [ "$type" = "dnsmasq" ]; then
1098 if [ -n "$instance" -a "$instance" = "$name" ]; then
1099 instance_found=1
1100 fi
1101 fi
1102 }
1103
1104 config_load dhcp
1105
1106 if [ -n "$instance" ]; then
1107 [ "$instance_found" -gt 0 ] || return
1108 dnsmasq_stop "$instance"
1109 else
1110 config_foreach dnsmasq_stop dnsmasq
1111 fi
1112 }