mac80211: disable greenfield by default
[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=60
5
6 USE_PROCD=1
7 PROG=/usr/sbin/dnsmasq
8
9 DNS_SERVERS=""
10 DOMAIN=""
11
12 ADD_LOCAL_DOMAIN=1
13 ADD_LOCAL_HOSTNAME=1
14
15 CONFIGFILE="/var/etc/dnsmasq.conf"
16 HOSTFILE="/tmp/hosts/dhcp"
17 TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
18
19 xappend() {
20 local value="$1"
21
22 echo "${value#--}" >> $CONFIGFILE
23 }
24
25 dhcp_calc() {
26 local ip="$1"
27 local res=0
28
29 while [ -n "$ip" ]; do
30 part="${ip%%.*}"
31 res="$(($res * 256))"
32 res="$(($res + $part))"
33 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
34 done
35 echo "$res"
36 }
37
38 dhcp_check() {
39 local ifname="$1"
40 local stamp="/var/run/dnsmasq.$ifname.dhcp"
41 local rv=0
42
43 [ -s "$stamp" ] && return $(cat "$stamp")
44
45 udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
46
47 [ $rv -eq 1 ] && \
48 logger -t dnsmasq \
49 "found already running DHCP-server on interface '$ifname'" \
50 "refusing to start, use 'option force 1' to override"
51
52 echo $rv > "$stamp"
53 return $rv
54 }
55
56 log_once() {
57 pidof dnsmasq >/dev/null || \
58 logger -t dnsmasq "$@"
59 }
60
61 append_bool() {
62 local section="$1"
63 local option="$2"
64 local value="$3"
65 local _loctmp
66 config_get_bool _loctmp "$section" "$option" 0
67 [ $_loctmp -gt 0 ] && xappend "$value"
68 }
69
70 append_parm() {
71 local section="$1"
72 local option="$2"
73 local switch="$3"
74 local _loctmp
75 config_get _loctmp "$section" "$option"
76 [ -z "$_loctmp" ] && return 0
77 xappend "$switch=$_loctmp"
78 }
79
80 append_server() {
81 xappend "--server=$1"
82 }
83
84 append_address() {
85 xappend "--address=$1"
86 }
87
88 append_interface() {
89 local ifname=$(uci_get_state network "$1" ifname "$1")
90 xappend "--interface=$ifname"
91 }
92
93 append_notinterface() {
94 local ifname=$(uci_get_state network "$1" ifname "$1")
95 xappend "--except-interface=$ifname"
96 }
97
98 append_addnhosts() {
99 xappend "--addn-hosts=$1"
100 }
101
102 append_bogusnxdomain() {
103 xappend "--bogus-nxdomain=$1"
104 }
105
106 dnsmasq() {
107 local cfg="$1"
108 append_bool "$cfg" authoritative "--dhcp-authoritative"
109 append_bool "$cfg" nodaemon "--no-daemon"
110 append_bool "$cfg" domainneeded "--domain-needed"
111 append_bool "$cfg" filterwin2k "--filterwin2k"
112 append_bool "$cfg" nohosts "--no-hosts"
113 append_bool "$cfg" nonegcache "--no-negcache"
114 append_bool "$cfg" strictorder "--strict-order"
115 append_bool "$cfg" logqueries "--log-queries"
116 append_bool "$cfg" noresolv "--no-resolv"
117 append_bool "$cfg" localise_queries "--localise-queries"
118 append_bool "$cfg" readethers "--read-ethers"
119 append_bool "$cfg" dbus "--enable-dbus"
120 append_bool "$cfg" boguspriv "--bogus-priv"
121 append_bool "$cfg" expandhosts "--expand-hosts"
122 append_bool "$cfg" enable_tftp "--enable-tftp"
123 append_bool "$cfg" nonwildcard "--bind-interfaces"
124 append_bool "$cfg" fqdn "--dhcp-fqdn"
125 append_bool "$cfg" proxydnssec "--proxy-dnssec"
126
127 append_parm "$cfg" dhcpscript "--dhcp-script"
128 append_parm "$cfg" cachesize "--cache-size"
129 append_parm "$cfg" dnsforwardmax "--dns-forward-max"
130 append_parm "$cfg" port "--port"
131 append_parm "$cfg" ednspacket_max "--edns-packet-max"
132 append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
133 append_parm "$cfg" "queryport" "--query-port"
134 append_parm "$cfg" "domain" "--domain"
135 append_parm "$cfg" "local" "--server"
136 config_list_foreach "$cfg" "server" append_server
137 config_list_foreach "$cfg" "address" append_address
138 config_list_foreach "$cfg" "interface" append_interface
139 config_list_foreach "$cfg" "notinterface" append_notinterface
140 config_list_foreach "$cfg" "addnhosts" append_addnhosts
141 config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
142 append_parm "$cfg" "leasefile" "--dhcp-leasefile"
143 append_parm "$cfg" "resolvfile" "--resolv-file"
144 append_parm "$cfg" "tftp_root" "--tftp-root"
145 append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
146 append_parm "$cfg" "local_ttl" "--local-ttl"
147
148 config_get DOMAIN "$cfg" domain
149
150 config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
151 config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
152
153 config_get_bool readethers "$cfg" readethers
154 [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
155
156 config_get leasefile $cfg leasefile
157 [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
158 config_get_bool cachelocal "$cfg" cachelocal 1
159
160 config_get hostsfile "$cfg" dhcphostsfile
161 [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
162
163 mkdir -p /tmp/hosts /tmp/dnsmasq.d
164 xappend "--addn-hosts=/tmp/hosts"
165 xappend "--conf-dir=/tmp/dnsmasq.d"
166
167 local rebind
168 config_get_bool rebind "$cfg" rebind_protection 1
169 [ $rebind -gt 0 ] && {
170 log_once \
171 "DNS rebinding protection is active," \
172 "will discard upstream RFC1918 responses!"
173 xappend "--stop-dns-rebind"
174
175 local rebind_localhost
176 config_get_bool rebind_localhost "$cfg" rebind_localhost 0
177 [ $rebind_localhost -gt 0 ] && {
178 log_once "Allowing 127.0.0.0/8 responses"
179 xappend "--rebind-localhost-ok"
180 }
181
182 append_rebind_domain() {
183 log_once "Allowing RFC1918 responses for domain $1"
184 xappend "--rebind-domain-ok=$1"
185 }
186
187 config_list_foreach "$cfg" rebind_domain append_rebind_domain
188 }
189
190 config_get dnssec "$cfg" dnssec
191 [ "$dnssec" -gt 0 ] && {
192 xappend "--conf-file=$TRUSTANCHORSFILE"
193 xappend "--dnssec"
194 append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
195 }
196
197 dhcp_option_add "$cfg" "" 0
198
199 xappend "--dhcp-broadcast=tag:needs-broadcast"
200
201 echo >> $CONFIGFILE
202 }
203
204 dhcp_subscrid_add() {
205 local cfg="$1"
206
207 config_get networkid "$cfg" networkid
208 [ -n "$networkid" ] || return 0
209
210 config_get subscriberid "$cfg" subscriberid
211 [ -n "$subscriberid" ] || return 0
212
213 xappend "--dhcp-subscrid=$networkid,$subscriberid"
214
215 config_get_bool force "$cfg" force 0
216
217 dhcp_option_add "$cfg" "$networkid" "$force"
218 }
219
220 dhcp_remoteid_add() {
221 local cfg="$1"
222
223 config_get networkid "$cfg" networkid
224 [ -n "$networkid" ] || return 0
225
226 config_get remoteid "$cfg" remoteid
227 [ -n "$remoteid" ] || return 0
228
229 xappend "--dhcp-remoteid=$networkid,$remoteid"
230
231 config_get_bool force "$cfg" force 0
232
233 dhcp_option_add "$cfg" "$networkid" "$force"
234 }
235
236 dhcp_circuitid_add() {
237 local cfg="$1"
238
239 config_get networkid "$cfg" networkid
240 [ -n "$networkid" ] || return 0
241
242 config_get circuitid "$cfg" circuitid
243 [ -n "$circuitid" ] || return 0
244
245 xappend "--dhcp-circuitid=$networkid,$circuitid"
246
247 config_get_bool force "$cfg" force 0
248
249 dhcp_option_add "$cfg" "$networkid" "$force"
250 }
251
252 dhcp_userclass_add() {
253 local cfg="$1"
254
255 config_get networkid "$cfg" networkid
256 [ -n "$networkid" ] || return 0
257
258 config_get userclass "$cfg" userclass
259 [ -n "$userclass" ] || return 0
260
261 xappend "--dhcp-userclass=$networkid,$userclass"
262
263 config_get_bool force "$cfg" force 0
264
265 dhcp_option_add "$cfg" "$networkid" "$force"
266 }
267
268 dhcp_vendorclass_add() {
269 local cfg="$1"
270
271 config_get networkid "$cfg" networkid
272 [ -n "$networkid" ] || return 0
273
274 config_get vendorclass "$cfg" vendorclass
275 [ -n "$vendorclass" ] || return 0
276
277 xappend "--dhcp-vendorclass=$networkid,$vendorclass"
278
279 config_get_bool force "$cfg" force 0
280
281 dhcp_option_add "$cfg" "$networkid" "$force"
282 }
283
284 dhcp_host_add() {
285 local cfg="$1"
286
287 config_get_bool force "$cfg" force 0
288
289 config_get networkid "$cfg" networkid
290 [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
291
292 config_get name "$cfg" name
293 config_get ip "$cfg" ip
294 [ -n "$ip" -o -n "$name" ] || return 0
295
296 config_get mac "$cfg" mac
297 [ -z "$mac" ] && {
298 [ -n "$name" ] || return 0
299 mac="$name"
300 name=""
301 }
302
303 macs=""
304 for m in $mac; do append macs "$m" ","; done
305
306 config_get tag "$cfg" tag
307
308 config_get_bool broadcast "$cfg" broadcast 0
309 [ "$broadcast" = "0" ] && broadcast=
310
311 xappend "--dhcp-host=$macs${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip}${name:+,$name}"
312
313 config_get_bool dns "$cfg" dns 0
314 [ "$dns" = "1" ] && {
315 echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
316 }
317 }
318
319 dhcp_tag_add() {
320 local cfg="$1"
321
322 tag="$cfg"
323
324 [ -n "$tag" ] || return 0
325
326 config_get_bool force "$cfg" force 0
327 [ "$force" = "0" ] && force=
328
329 config_get option "$cfg" dhcp_option
330 for o in $option; do
331 xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
332 done
333 }
334
335 dhcp_mac_add() {
336 local cfg="$1"
337
338 config_get networkid "$cfg" networkid
339 [ -n "$networkid" ] || return 0
340
341 config_get mac "$cfg" mac
342 [ -n "$mac" ] || return 0
343
344 xappend "--dhcp-mac=$networkid,$mac"
345
346 dhcp_option_add "$cfg" "$networkid"
347 }
348
349 dhcp_boot_add() {
350 local cfg="$1"
351
352 config_get networkid "$cfg" networkid
353
354 config_get filename "$cfg" filename
355 [ -n "$filename" ] || return 0
356
357 config_get servername "$cfg" servername
358 [ -n "$servername" ] || return 0
359
360 config_get serveraddress "$cfg" serveraddress
361 [ -n "$serveraddress" ] || return 0
362
363 xappend "--dhcp-boot=${networkid:+net:$networkid,}$filename,$servername,$serveraddress"
364
365 config_get_bool force "$cfg" force 0
366
367 dhcp_option_add "$cfg" "$networkid" "$force"
368 }
369
370
371 dhcp_add() {
372 local cfg="$1"
373 config_get net "$cfg" interface
374 [ -n "$net" ] || return 0
375
376 config_get networkid "$cfg" networkid
377 [ -n "$networkid" ] || networkid="$net"
378
379 network_get_subnet subnet "$net" || return 0
380 network_get_device ifname "$net" || return 0
381 network_get_protocol proto "$net" || return 0
382
383 [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
384 DNS_SERVERS="$DNS_SERVERS $dnsserver"
385 }
386
387 append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && return 0
388
389 # Do not support non-static interfaces for now
390 [ static = "$proto" ] || return 0
391
392 # Override interface netmask with dhcp config if applicable
393 config_get netmask "$cfg" netmask "${subnet##*/}"
394
395 #check for an already active dhcp server on the interface, unless 'force' is set
396 config_get_bool force "$cfg" force 0
397 [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
398
399 config_get start "$cfg" start
400 config_get limit "$cfg" limit
401 config_get leasetime "$cfg" leasetime
402 config_get options "$cfg" options
403 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
404
405 leasetime="${leasetime:-12h}"
406 start="$(dhcp_calc "${start:-100}")"
407 limit="${limit:-150}"
408 [ "$limit" -gt 0 ] && limit=$((limit-1))
409 eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
410 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
411 xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
412
413 dhcp_option_add "$cfg" "$networkid"
414 }
415
416 dhcp_option_add() {
417 local cfg="$1"
418 local networkid="$2"
419 local force="$3"
420
421 [ "$force" = "0" ] && force=
422
423 config_get dhcp_option "$cfg" dhcp_option
424 for o in $dhcp_option; do
425 xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
426 done
427
428 }
429
430 dhcp_domain_add() {
431 local cfg="$1"
432 local ip name names record
433
434 config_get names "$cfg" name "$2"
435 [ -n "$names" ] || return 0
436
437 config_get ip "$cfg" ip "$3"
438 [ -n "$ip" ] || return 0
439
440 for name in $names; do
441 [ "${name%.*}" == "$name" ] && \
442 name="$name${DOMAIN:+.$DOMAIN}"
443
444 record="${record:+$record/}$name"
445 done
446
447 xappend "--address=/$record/$ip"
448 }
449
450 dhcp_srv_add() {
451 local cfg="$1"
452
453 config_get srv "$cfg" srv
454 [ -n "$srv" ] || return 0
455
456 config_get target "$cfg" target
457 [ -n "$target" ] || return 0
458
459 config_get port "$cfg" port
460 [ -n "$port" ] || return 0
461
462 config_get class "$cfg" class
463 config_get weight "$cfg" weight
464
465 local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
466
467 xappend "--srv-host=$service"
468 }
469
470 dhcp_mx_add() {
471 local cfg="$1"
472 local domain relay pref
473
474 config_get domain "$cfg" domain
475 [ -n "$domain" ] || return 0
476
477 config_get relay "$cfg" relay
478 [ -n "$relay" ] || return 0
479
480 config_get pref "$cfg" pref 0
481
482 local service="$domain,$relay,$pref"
483
484 xappend "--mx-host=$service"
485 }
486
487 dhcp_cname_add() {
488 local cfg="$1"
489 local cname target
490
491 config_get cname "$cfg" cname
492 [ -n "$cname" ] || return 0
493
494 config_get target "$cfg" target
495 [ -n "$target" ] || return 0
496
497 xappend "--cname=${cname},${target}"
498 }
499
500 dhcp_hostrecord_add() {
501 local cfg="$1"
502 local names addresses record val
503
504 config_get names "$cfg" name "$2"
505 if [ -z "$names" ]; then
506 return 0
507 fi
508
509 config_get addresses "$cfg" ip "$3"
510 if [ -z "$addresses" ]; then
511 return 0
512 fi
513
514 for val in $names $addresses; do
515 record="${record:+$record,}$val"
516 done
517
518 xappend "--host-record=$record"
519 }
520
521 service_triggers()
522 {
523 procd_add_reload_trigger "dhcp"
524 }
525
526 boot() {
527 # Will be launched through hotplug
528 return 0
529 }
530
531 start_service() {
532 include /lib/functions
533
534 config_load dhcp
535
536 procd_open_instance
537 procd_set_param command $PROG -C $CONFIGFILE -k
538 procd_set_param file $CONFIGFILE
539 procd_set_param respawn
540 procd_close_instance
541
542 # before we can call xappend
543 mkdir -p $(dirname $CONFIGFILE)
544
545 echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE
546 echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
547
548 # if we did this last, we could override auto-generated config
549 [ -f /etc/dnsmasq.conf ] && {
550 xappend "--conf-file=/etc/dnsmasq.conf"
551 }
552
553 args=""
554 config_foreach dnsmasq dnsmasq
555 config_foreach dhcp_host_add host
556 echo >> $CONFIGFILE
557 config_foreach dhcp_boot_add boot
558 config_foreach dhcp_mac_add mac
559 config_foreach dhcp_tag_add tag
560 config_foreach dhcp_vendorclass_add vendorclass
561 config_foreach dhcp_userclass_add userclass
562 config_foreach dhcp_circuitid_add circuitid
563 config_foreach dhcp_remoteid_add remoteid
564 config_foreach dhcp_subscrid_add subscrid
565 config_foreach dhcp_domain_add domain
566 config_foreach dhcp_hostrecord_add hostrecord
567
568 # add own hostname
569 local lanaddr
570 [ $ADD_LOCAL_HOSTNAME -eq 1 ] && network_get_ipaddr lanaddr "lan" && {
571 local hostname="$(uci_get system @system[0] hostname OpenWrt)"
572 dhcp_hostrecord_add "" "${hostname%.$DOMAIN}${DOMAIN:+.$DOMAIN ${hostname%.$DOMAIN}}" "$lanaddr"
573 }
574
575 echo >> $CONFIGFILE
576 config_foreach dhcp_srv_add srvhost
577 config_foreach dhcp_mx_add mxhost
578 echo >> $CONFIGFILE
579
580 config_get odhcpd_is_active odhcpd maindhcp
581 if [ "$odhcpd_is_active" != "1" ]; then
582 config_foreach dhcp_add dhcp
583 fi
584
585 echo >> $CONFIGFILE
586 config_foreach dhcp_cname_add cname
587 echo >> $CONFIGFILE
588
589 rm -f /tmp/resolv.conf
590 [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
591 echo "search $DOMAIN" >> /tmp/resolv.conf
592 }
593 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
594 for DNS_SERVER in $DNS_SERVERS ; do
595 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
596 done
597 }
598
599 reload_service() {
600 rc_procd start_service "$@"
601 return 0
602 }
603
604 stop_service() {
605 [ -f /tmp/resolv.conf ] && {
606 rm -f /tmp/resolv.conf
607 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
608 }
609 rm -f /var/run/dnsmasq.*.dhcp
610 }