add option configure a custom netmask for dhcp clients Signed-off-by: Lorenz Schori...
[openwrt/svn-archive/archive.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
68 dhcp_subscrid_add() {
69 local cfg="$1"
70
71 config_get name "$cfg" name
72 [ -n "$name" ] || return 0
73
74 config_get subscriberid "$cfg" subscriberid
75 [ -n "$subscriberid" ] || return 0
76
77 append args "--dhcp-subscrid=$name,$subscriberid"
78
79 dhcp_option_add "$cfg" "$name"
80 }
81
82 dhcp_remoteid_add() {
83 local cfg="$1"
84
85 config_get name "$cfg" name
86 [ -n "$name" ] || return 0
87
88 config_get remoteid "$cfg" remoteid
89 [ -n "$remoteid" ] || return 0
90
91 append args "--dhcp-remoteid=$name,$remoteid"
92
93 dhcp_option_add "$cfg" "$name"
94 }
95
96 dhcp_circuitid_add() {
97 local cfg="$1"
98
99 config_get name "$cfg" name
100 [ -n "$name" ] || return 0
101
102 config_get circuitid "$cfg" circuitid
103 [ -n "$circuitid" ] || return 0
104
105 append args "--dhcp-circuitid=$name,$circuitid"
106
107 dhcp_option_add "$cfg" "$name"
108 }
109
110 dhcp_userclass_add() {
111 local cfg="$1"
112
113 config_get name "$cfg" name
114 [ -n "$name" ] || return 0
115
116 config_get userclass "$cfg" userclass
117 [ -n "$userclass" ] || return 0
118
119 append args "--dhcp-userclass=$name,$userclass"
120
121 dhcp_option_add "$cfg" "$name"
122 }
123
124 dhcp_vendorclass_add() {
125 local cfg="$1"
126
127 config_get name "$cfg" name
128 [ -n "$name" ] || return 0
129
130 config_get vendorclass "$cfg" vendorclass
131 [ -n "$vendorclass" ] || return 0
132
133 append args "--dhcp-vendorclass=$name,$vendorclass"
134
135 dhcp_option_add "$cfg" "$name"
136 }
137
138 dhcp_host_add() {
139 local cfg="$1"
140
141 config_get name "$cfg" name
142 [ -n "$name" ] || return 0
143
144 config_get mac "$cfg" mac
145 [ -n "$mac" ] || return 0
146
147 config_get ip "$cfg" ip
148 [ -n "$ip" ] || return 0
149
150 append args "--dhcp-host=$mac,$ip"
151
152 dhcp_option_add "$cfg" "$name"
153 }
154
155 dhcp_mac_add() {
156 local cfg="$1"
157
158 config_get name "$cfg" name
159 [ -n "$name" ] || return 0
160
161 config_get mac "$cfg" mac
162 [ -n "$mac" ] || return 0
163
164 append args "--dhcp-mac=$name,$mac"
165
166 dhcp_option_add "$cfg" "$name"
167 }
168
169 dhcp_add() {
170 local cfg="$1"
171 config_get net "$cfg" interface
172 [ -n "$net" ] || return 0
173
174 config_get name "$cfg" name
175 [ -n "$name" ] || name="$net"
176
177 config_get ifname "$net" ifname
178 [ -n "$ifname" ] || return 0
179
180 config_get dnsserver "$net" dns
181 [ -n "$dnsserver" ] && {
182 DNS_SERVERS="$DNS_SERVERS $dnsserver"
183 }
184
185 append_bool "$cfg" ignore "-I $ifname"
186
187 config_get proto "$net" proto
188 [ static = "$proto" ] || return 0
189
190 config_get ipaddr "$net" ipaddr
191 config_get netmask "$cfg" netmask
192 [ -n "$netmask" ] || config_get netmask "$net" netmask
193
194 #check for an already active dhcp server on the interface, unless 'force' is set
195 config_get_bool force "$cfg" force 0
196 [ "$force" -gt 0 ] || {
197 udhcpc -n -q -R -s /bin/true -t 1 -i $ifname >&- && return 0
198 }
199
200 config_get start "$cfg" start
201 config_get limit "$cfg" limit
202 config_get leasetime "$cfg" leasetime
203 config_get options "$cfg" options
204 config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
205
206 leasetime="${leasetime:-12h}"
207 start="$(dhcp_calc "${start:-100}")"
208 limit="$((${limit:-150} + 1))"
209 eval "$(ipcalc.sh $ipaddr $netmask $start $limit)"
210 if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
211 append args "--dhcp-range=$name,$START,$END,$NETMASK,$leasetime${options:+ $options}"
212
213 dhcp_option_add "$cfg" "$name"
214 }
215
216 dhcp_option_add() {
217 local cfg="$1"
218 local name="$2"
219
220 for count in $(seq 0 100); do
221 eval current_value=\$CONFIG_"$cfg"_dhcp"$count"
222 if [ -z "$current_value" ]; then
223 let "count-=1"
224 break
225 fi
226 append args "-O $name","$current_value"
227 done
228
229 }
230
231 start() {
232 include /lib/network
233 scan_interfaces
234 config_load /var/state/network
235 config_load dhcp
236
237 args=""
238 config_foreach dnsmasq dnsmasq
239 config_foreach dhcp_host_add host
240 config_foreach dhcp_mac_add mac
241 config_foreach dhcp_vendorclass_add vendorclass
242 config_foreach dhcp_userclass_add userclass
243 config_foreach dhcp_circuitid_add circuitid
244 config_foreach dhcp_remoteid_add remoteid
245 config_foreach dhcp_subscrid_add subscrid
246 config_foreach dhcp_add dhcp
247
248 /usr/sbin/dnsmasq $args && {
249 rm -f /tmp/resolv.conf
250 DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
251 for DNS_SERVER in $DNS_SERVERS ; do
252 echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
253 done
254 }
255 }
256
257 stop() {
258 killall dnsmasq
259 }