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