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