7a1530fdd3640278777bfa9e38f21b095f640c83
[openwrt.git] / package / dnsmasq / files / dnsmasq.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2007 OpenWrt.org
3
4 START=60
5 DNS_SERVERS=""
6
7 dhcp_calc() {
8         local ip="$1"
9         local res=0
10
11         while [ -n "$ip" ]; do
12                 part="${ip%%.*}"
13                 res="$(($res * 256))"
14                 res="$(($res + $part))"
15                 [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
16         done
17         echo "$res"
18 }
19
20 append_bool() {
21         local section="$1"
22         local option="$2"
23         local value="$3"
24         local _loctmp
25         config_get_bool _loctmp "$section" "$option"
26         [ "$_loctmp" -gt 0 ] && append args "$value"
27 }
28
29 append_parm() {
30         local section="$1"
31         local option="$2"
32         local switch="$3"
33         local _loctmp
34         config_get _loctmp "$section" "$option"
35         [ -z "$_loctmp" ] && return 0
36         append args "$switch $_loctmp"
37 }
38
39 append_server() {
40         append args "-S $1"
41 }
42
43 append_interface() {
44         append args "-i $1"
45 }
46
47 dnsmasq() {
48         local cfg="$1"
49         append_bool "$cfg" authoritative "-K"
50         append_bool "$cfg" nodaemon "-d"
51         append_bool "$cfg" domainneeded "-D"
52         append_bool "$cfg" filterwin2k "-f"
53         append_bool "$cfg" nohosts "-h"
54         append_bool "$cfg" nonegcache "-N"
55         append_bool "$cfg" strictorder "-o"
56         append_bool "$cfg" logqueries "-q"
57         append_bool "$cfg" noresolv "-R"
58         append_bool "$cfg" localise_queries "-y"
59         append_bool "$cfg" readethers "-Z"
60         append_bool "$cfg" dbus "-1"
61         append_bool "$cfg" boguspriv "-b"
62         append_bool "$cfg" expandhosts "-E"
63         append_bool "$cfg" enable_tftp "--enable-tftp"
64         append_bool "$cfg" nonwildcard "-z"
65
66         append_parm "$cfg" dnsforwardmax "-0"
67         append_parm "$cfg" port "-p"
68         append_parm "$cfg" ednspacket_max "-P"
69         append_parm "$cfg" dhcpleasemax "-X"
70         append_parm "$cfg" "addnhosts" "-H"
71         append_parm "$cfg" "queryport" "-Q"
72         append_parm "$cfg" "domain" "-s"
73         append_parm "$cfg" "local" "-S"
74         config_list_foreach "$cfg" "server" append_server
75         config_list_foreach "$cfg" "interface" append_interface
76         append_parm "$cfg" "leasefile" "-l"
77         append_parm "$cfg" "resolvfile" "-r"
78         append_parm "$cfg" "tftp_root" "--tftp-root"
79         append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
80
81         config_get leasefile $cfg leasefile
82         [ -e "$leasefile" ] || touch "$leasefile"
83         config_get_bool cachelocal "$cfg" cachelocal 1
84 }
85
86 dhcp_subscrid_add() {
87         local cfg="$1"
88
89         config_get name "$cfg" name
90         [ -n "$name" ] || return 0
91
92         config_get subscriberid "$cfg" subscriberid
93         [ -n "$subscriberid" ] || return 0
94
95         append args "--dhcp-subscrid=$name,$subscriberid"
96
97         dhcp_option_add "$cfg" "$name"
98 }
99
100 dhcp_remoteid_add() {
101         local cfg="$1"
102
103         config_get name "$cfg" name
104         [ -n "$name" ] || return 0
105
106         config_get remoteid "$cfg" remoteid
107         [ -n "$remoteid" ] || return 0
108
109         append args "--dhcp-remoteid=$name,$remoteid"
110
111         dhcp_option_add "$cfg" "$name"
112 }
113
114 dhcp_circuitid_add() {
115         local cfg="$1"
116
117         config_get name "$cfg" name
118         [ -n "$name" ] || return 0
119
120         config_get circuitid "$cfg" circuitid
121         [ -n "$circuitid" ] || return 0
122
123         append args "--dhcp-circuitid=$name,$circuitid"
124
125         dhcp_option_add "$cfg" "$name"
126 }
127
128 dhcp_userclass_add() {
129         local cfg="$1"
130
131         config_get name "$cfg" name
132         [ -n "$name" ] || return 0
133
134         config_get userclass "$cfg" userclass
135         [ -n "$userclass" ] || return 0
136
137         append args "--dhcp-userclass=$name,$userclass"
138
139         dhcp_option_add "$cfg" "$name"
140 }
141
142 dhcp_vendorclass_add() {
143         local cfg="$1"
144
145         config_get name "$cfg" name
146         [ -n "$name" ] || return 0
147
148         config_get vendorclass "$cfg" vendorclass
149         [ -n "$vendorclass" ] || return 0
150
151         append args "--dhcp-vendorclass=$name,$vendorclass"
152
153         dhcp_option_add "$cfg" "$name"
154 }
155
156 dhcp_host_add() {
157         local cfg="$1"
158
159         config_get name "$cfg" name
160         [ -n "$name" ] || return 0
161
162         config_get mac "$cfg" mac
163         [ -n "$mac" ] || return 0
164
165         config_get ip "$cfg" ip
166         [ -n "$ip" ] || return 0
167
168         append args "--dhcp-host=$mac,$ip"
169
170         dhcp_option_add "$cfg" "$name"
171 }
172
173 dhcp_mac_add() {
174         local cfg="$1"
175
176         config_get name "$cfg" name
177         [ -n "$name" ] || return 0
178
179         config_get mac "$cfg" mac
180         [ -n "$mac" ] || return 0
181
182         append args "--dhcp-mac=$name,$mac"
183
184         dhcp_option_add "$cfg" "$name"
185 }
186
187 dhcp_boot_add() {
188         local cfg="$1"
189
190         config_get name "$cfg" name
191
192         config_get filename "$cfg" filename
193         [ -n "$filename" ] || return 0
194
195         config_get servername "$cfg" servername
196         [ -n "$servername" ] || return 0
197
198         config_get serveraddress "$cfg" serveraddress
199         [ -n "$serveraddress" ] || return 0
200
201         append args "--dhcp-boot=${name:+net:$name,}$filename,$servername,$serveraddress"
202
203         dhcp_option_add "$cfg" "$name"
204 }
205
206
207 dhcp_add() {
208         local cfg="$1"
209         config_get net "$cfg" interface
210         [ -n "$net" ] || return 0
211
212         config_get name "$cfg" name
213         [ -n "$name" ] || name="$net"
214
215         config_get ifname "$net" ifname
216         [ -n "$ifname" ] || return 0
217
218         config_get dnsserver "$net" dns
219         [ "$cachelocal" = "0" -a -n "$dnsserver" ] && {
220                 DNS_SERVERS="$DNS_SERVERS $dnsserver"
221         }
222
223         append_bool "$cfg" ignore "-2 $ifname"
224
225         config_get proto "$net" proto
226         [ static = "$proto" ] || return 0
227
228         config_get ipaddr "$net" ipaddr
229         config_get netmask "$cfg" netmask
230         [ -n "$netmask" ] || config_get netmask "$net" netmask
231
232         #check for an already active dhcp server on the interface, unless 'force' is set
233         config_get_bool force "$cfg" force 0
234         [ "$force" -gt 0 ] || {
235                 udhcpc -n -q -s /bin/true -t 1 -i $ifname >&- && return 0
236         }
237
238         config_get start "$cfg" start
239         config_get limit "$cfg" limit
240         config_get leasetime "$cfg" leasetime
241         config_get options "$cfg" options
242         config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
243
244         leasetime="${leasetime:-12h}"
245         start="$(dhcp_calc "${start:-100}")"
246         limit="$((${limit:-150} + 1))"
247         eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
248         if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
249         append args "--dhcp-range=$name,$START,$END,$NETMASK,$leasetime${options:+ $options}"
250
251         dhcp_option_add "$cfg" "$name"
252 }
253
254 dhcp_option_add() {
255         local cfg="$1"
256         local name="$2"
257
258         config_get dhcp_option "$cfg" dhcp_option
259         for o in $dhcp_option; do
260                 append args "-O $name","$o"
261         done
262
263 }
264
265 start() {
266         include /lib/network
267         scan_interfaces
268         config_load dhcp
269
270         args=""
271         config_foreach dnsmasq dnsmasq
272         config_foreach dhcp_host_add host
273         config_foreach dhcp_boot_add boot
274         config_foreach dhcp_mac_add mac
275         config_foreach dhcp_vendorclass_add vendorclass
276         config_foreach dhcp_userclass_add userclass
277         config_foreach dhcp_circuitid_add circuitid
278         config_foreach dhcp_remoteid_add remoteid
279         config_foreach dhcp_subscrid_add subscrid
280         config_foreach dhcp_add dhcp
281
282         /usr/sbin/dnsmasq $args && {
283                 rm -f /tmp/resolv.conf
284                 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
285                 for DNS_SERVER in $DNS_SERVERS ; do
286                         echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
287                 done
288         }
289 }
290
291 stop() {
292         [ -f /tmp/resolv.conf ] && {
293                 rm -f /tmp/resolv.conf
294                 ln -s /tmp/resolv.conf.auto /tmp/resolv.conf
295         }
296         killall dnsmasq
297         return 0
298 }