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