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