1
0

dnsmasq.init 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2007-2012 OpenWrt.org
  3. START=19
  4. USE_PROCD=1
  5. PROG=/usr/sbin/dnsmasq
  6. ADD_LOCAL_DOMAIN=1
  7. ADD_LOCAL_HOSTNAME=1
  8. ADD_WAN_HOSTNAME=0
  9. BASECONFIGFILE="/var/etc/dnsmasq.conf"
  10. BASEHOSTFILE="/tmp/hosts/dhcp"
  11. BASETIMESTAMPFILE="/etc/dnsmasq.time"
  12. TRUSTANCHORSFILE="/usr/share/dnsmasq/trust-anchors.conf"
  13. TIMEVALIDFILE="/var/state/dnsmasqsec"
  14. BASEDHCPSTAMPFILE="/var/run/dnsmasq"
  15. xappend() {
  16. local value="$1"
  17. echo "${value#--}" >> $CONFIGFILE_TMP
  18. }
  19. hex_to_hostid() {
  20. local var="$1"
  21. local hex="${2#0x}" # strip optional "0x" prefix
  22. if [ -n "${hex//[0-9a-fA-F]/}" ]; then
  23. # is invalid hex literal
  24. return 1
  25. fi
  26. # convert into host id
  27. export "$var=$(
  28. printf "%0x:%0x" \
  29. $(((0x$hex >> 16) % 65536)) \
  30. $(( 0x$hex % 65536))
  31. )"
  32. return 0
  33. }
  34. dhcp_calc() {
  35. local ip="$1"
  36. local res=0
  37. while [ -n "$ip" ]; do
  38. part="${ip%%.*}"
  39. res="$(($res * 256))"
  40. res="$(($res + $part))"
  41. [ "${ip%.*}" != "$ip" ] && ip="${ip#*.}" || ip=
  42. done
  43. echo "$res"
  44. }
  45. dhcp_check() {
  46. local ifname="$1"
  47. local stamp="${BASEDHCPSTAMPFILE_CFG}.${ifname}.dhcp"
  48. local rv=0
  49. [ -s "$stamp" ] && return $(cat "$stamp")
  50. # If there's no carrier yet, skip this interface.
  51. # The init script will be called again once the link is up
  52. case "$(devstatus "$ifname" | jsonfilter -e @.carrier)" in
  53. false) return 1;;
  54. esac
  55. udhcpc -n -q -s /bin/true -t 1 -i "$ifname" >&- && rv=1 || rv=0
  56. [ $rv -eq 1 ] && \
  57. logger -t dnsmasq \
  58. "found already running DHCP-server on interface '$ifname'" \
  59. "refusing to start, use 'option force 1' to override"
  60. echo $rv > "$stamp"
  61. return $rv
  62. }
  63. log_once() {
  64. pidof dnsmasq >/dev/null || \
  65. logger -t dnsmasq "$@"
  66. }
  67. append_bool() {
  68. local section="$1"
  69. local option="$2"
  70. local value="$3"
  71. local _loctmp
  72. config_get_bool _loctmp "$section" "$option" 0
  73. [ $_loctmp -gt 0 ] && xappend "$value"
  74. }
  75. append_parm() {
  76. local section="$1"
  77. local option="$2"
  78. local switch="$3"
  79. local default="$4"
  80. local _loctmp
  81. config_get _loctmp "$section" "$option" "$default"
  82. [ -z "$_loctmp" ] && return 0
  83. xappend "$switch=$_loctmp"
  84. }
  85. append_server() {
  86. xappend "--server=$1"
  87. }
  88. append_address() {
  89. xappend "--address=$1"
  90. }
  91. append_ipset() {
  92. xappend "--ipset=$1"
  93. }
  94. append_interface() {
  95. network_get_device ifname "$1" || return
  96. xappend "--interface=$ifname"
  97. }
  98. append_notinterface() {
  99. network_get_device ifname "$1" || return
  100. xappend "--except-interface=$ifname"
  101. }
  102. append_addnhosts() {
  103. xappend "--addn-hosts=$1"
  104. }
  105. append_bogusnxdomain() {
  106. xappend "--bogus-nxdomain=$1"
  107. }
  108. append_pxe_service() {
  109. xappend "--pxe-service=$1"
  110. }
  111. filter_dnsmasq() {
  112. local cfg="$1" func="$2" match_cfg="$3" found_cfg
  113. # use entry when no instance entry set, or if it matches
  114. config_get found_cfg "$cfg" "instance"
  115. if [ -z "$found_cfg" -o "$found_cfg" = "$match_cfg" ]; then
  116. $func $cfg
  117. fi
  118. }
  119. dhcp_subscrid_add() {
  120. local cfg="$1"
  121. config_get networkid "$cfg" networkid
  122. [ -n "$networkid" ] || return 0
  123. config_get subscriberid "$cfg" subscriberid
  124. [ -n "$subscriberid" ] || return 0
  125. xappend "--dhcp-subscrid=$networkid,$subscriberid"
  126. config_get_bool force "$cfg" force 0
  127. dhcp_option_add "$cfg" "$networkid" "$force"
  128. }
  129. dhcp_remoteid_add() {
  130. local cfg="$1"
  131. config_get networkid "$cfg" networkid
  132. [ -n "$networkid" ] || return 0
  133. config_get remoteid "$cfg" remoteid
  134. [ -n "$remoteid" ] || return 0
  135. xappend "--dhcp-remoteid=$networkid,$remoteid"
  136. config_get_bool force "$cfg" force 0
  137. dhcp_option_add "$cfg" "$networkid" "$force"
  138. }
  139. dhcp_circuitid_add() {
  140. local cfg="$1"
  141. config_get networkid "$cfg" networkid
  142. [ -n "$networkid" ] || return 0
  143. config_get circuitid "$cfg" circuitid
  144. [ -n "$circuitid" ] || return 0
  145. xappend "--dhcp-circuitid=$networkid,$circuitid"
  146. config_get_bool force "$cfg" force 0
  147. dhcp_option_add "$cfg" "$networkid" "$force"
  148. }
  149. dhcp_userclass_add() {
  150. local cfg="$1"
  151. config_get networkid "$cfg" networkid
  152. [ -n "$networkid" ] || return 0
  153. config_get userclass "$cfg" userclass
  154. [ -n "$userclass" ] || return 0
  155. xappend "--dhcp-userclass=$networkid,$userclass"
  156. config_get_bool force "$cfg" force 0
  157. dhcp_option_add "$cfg" "$networkid" "$force"
  158. }
  159. dhcp_vendorclass_add() {
  160. local cfg="$1"
  161. config_get networkid "$cfg" networkid
  162. [ -n "$networkid" ] || return 0
  163. config_get vendorclass "$cfg" vendorclass
  164. [ -n "$vendorclass" ] || return 0
  165. xappend "--dhcp-vendorclass=$networkid,$vendorclass"
  166. config_get_bool force "$cfg" force 0
  167. dhcp_option_add "$cfg" "$networkid" "$force"
  168. }
  169. dhcp_match_add() {
  170. local cfg="$1"
  171. config_get networkid "$cfg" networkid
  172. [ -n "$networkid" ] || return 0
  173. config_get match "$cfg" match
  174. [ -n "$match" ] || return 0
  175. xappend "--dhcp-match=$networkid,$match"
  176. config_get_bool force "$cfg" force 0
  177. dhcp_option_add "$cfg" "$networkid" "$force"
  178. }
  179. dhcp_host_add() {
  180. local cfg="$1"
  181. config_get_bool force "$cfg" force 0
  182. config_get networkid "$cfg" networkid
  183. [ -n "$networkid" ] && dhcp_option_add "$cfg" "$networkid" "$force"
  184. config_get_bool enable "$cfg" enable 1
  185. [ "$enable" = "0" ] && return 0
  186. config_get name "$cfg" name
  187. config_get ip "$cfg" ip
  188. [ -n "$ip" -o -n "$name" ] || return 0
  189. config_get_bool dns "$cfg" dns 0
  190. [ "$dns" = "1" -a -n "$ip" -a -n "$name" ] && {
  191. echo "$ip $name${DOMAIN:+.$DOMAIN}" >> $HOSTFILE
  192. }
  193. config_get mac "$cfg" mac
  194. if [ -n "$mac" ]; then
  195. # --dhcp-host=00:20:e0:3b:13:af,192.168.0.199,lap
  196. macs=""
  197. for m in $mac; do append macs "$m" ","; done
  198. else
  199. # --dhcp-host=lap,192.168.0.199
  200. [ -n "$name" ] || return 0
  201. macs="$name"
  202. name=""
  203. fi
  204. config_get tag "$cfg" tag
  205. if [ "$DHCPv6CAPABLE" -eq 1 ]; then
  206. config_get duid "$cfg" duid
  207. config_get hostid "$cfg" hostid
  208. if [ -n "$hostid" ]; then
  209. hex_to_hostid hostid "$hostid"
  210. fi
  211. fi
  212. config_get_bool broadcast "$cfg" broadcast 0
  213. [ "$broadcast" = "0" ] && broadcast=
  214. config_get leasetime "$cfg" leasetime
  215. xappend "--dhcp-host=$macs${duid:+,id:$duid}${networkid:+,net:$networkid}${broadcast:+,set:needs-broadcast}${tag:+,set:$tag}${ip:+,$ip${hostid:+,[::$hostid]}}${name:+,$name}${leasetime:+,$leasetime}"
  216. }
  217. dhcp_this_host_add() {
  218. # TODO: case-in do/not short-host or FQDN; UCI already intended ...
  219. local ifname="$1"
  220. local do_enable="$2"
  221. local routerstub routername ifdashname
  222. if [ "$do_enable" -gt 0 ] ; then
  223. # All IP addresses discovered by dnsmasq will be labeled robustly (except fe80::)
  224. ifdashname="${ifname//./-}"
  225. routerstub="$( md5sum /etc/os-release )"
  226. routerstub="router-${routerstub// */}"
  227. routername="$( uci_get system @system[0] hostname $routerstub )"
  228. xappend "--interface-name=$ifdashname.$routername.$DOMAIN,$ifname"
  229. xappend "--interface-name=$routername.$DOMAIN,$ifname"
  230. xappend "--interface-name=$routername,$ifname"
  231. fi
  232. }
  233. dhcp_tag_add() {
  234. local cfg="$1"
  235. tag="$cfg"
  236. [ -n "$tag" ] || return 0
  237. config_get_bool force "$cfg" force 0
  238. [ "$force" = "0" ] && force=
  239. config_get option "$cfg" dhcp_option
  240. for o in $option; do
  241. xappend "--dhcp-option${force:+-force}=tag:$tag,$o"
  242. done
  243. }
  244. dhcp_mac_add() {
  245. local cfg="$1"
  246. config_get networkid "$cfg" networkid
  247. [ -n "$networkid" ] || return 0
  248. config_get mac "$cfg" mac
  249. [ -n "$mac" ] || return 0
  250. xappend "--dhcp-mac=$networkid,$mac"
  251. dhcp_option_add "$cfg" "$networkid"
  252. }
  253. dhcp_boot_add() {
  254. local cfg="$1"
  255. config_get networkid "$cfg" networkid
  256. config_get filename "$cfg" filename
  257. [ -n "$filename" ] || return 0
  258. config_get servername "$cfg" servername
  259. config_get serveraddress "$cfg" serveraddress
  260. [ -n "$serveraddress" -a ! -n "$servername" ] && return 0
  261. xappend "--dhcp-boot=${networkid:+net:$networkid,}${filename}${servername:+,$servername}${serveraddress:+,$serveraddress}"
  262. config_get_bool force "$cfg" force 0
  263. dhcp_option_add "$cfg" "$networkid" "$force"
  264. }
  265. dhcp_add() {
  266. local cfg="$1"
  267. config_get net "$cfg" interface
  268. [ -n "$net" ] || return 0
  269. config_get dhcpv4 "$cfg" dhcpv4
  270. [ "$dhcpv4" != "disabled" ] || return 0
  271. config_get networkid "$cfg" networkid
  272. [ -n "$networkid" ] || networkid="$net"
  273. network_get_subnet subnet "$net" || return 0
  274. network_get_device ifname "$net" || return 0
  275. network_get_protocol proto "$net" || return 0
  276. [ "$cachelocal" = "0" ] && network_get_dnsserver dnsserver "$net" && {
  277. DNS_SERVERS="$DNS_SERVERS $dnsserver"
  278. }
  279. append_bool "$cfg" ignore "--no-dhcp-interface=$ifname" && {
  280. # Many ISP do not have useful names for DHCP customers (your WAN).
  281. dhcp_this_host_add $ifname $ADD_WAN_HOSTNAME
  282. return 0
  283. }
  284. # Do not support non-static interfaces for now
  285. [ static = "$proto" ] || return 0
  286. # Override interface netmask with dhcp config if applicable
  287. config_get netmask "$cfg" netmask "${subnet##*/}"
  288. #check for an already active dhcp server on the interface, unless 'force' is set
  289. config_get_bool force "$cfg" force 0
  290. [ $force -gt 0 ] || dhcp_check "$ifname" || return 0
  291. config_get start "$cfg" start
  292. config_get limit "$cfg" limit
  293. config_get leasetime "$cfg" leasetime
  294. config_get options "$cfg" options
  295. config_get_bool dynamicdhcp "$cfg" dynamicdhcp 1
  296. # Put the router host name on this DHCP served interface address(es)
  297. dhcp_this_host_add $ifname $ADD_LOCAL_HOSTNAME
  298. leasetime="${leasetime:-12h}"
  299. start="$(dhcp_calc "${start:-100}")"
  300. limit="${limit:-150}"
  301. [ "$limit" -gt 0 ] && limit=$((limit-1))
  302. eval "$(ipcalc.sh "${subnet%%/*}" $netmask $start $limit)"
  303. if [ "$dynamicdhcp" = "0" ]; then END="static"; fi
  304. xappend "--dhcp-range=$networkid,$START,$END,$NETMASK,$leasetime${options:+ $options}"
  305. dhcp_option_add "$cfg" "$networkid"
  306. }
  307. dhcp_option_add() {
  308. local cfg="$1"
  309. local networkid="$2"
  310. local force="$3"
  311. [ "$force" = "0" ] && force=
  312. config_get dhcp_option "$cfg" dhcp_option
  313. for o in $dhcp_option; do
  314. xappend "--dhcp-option${force:+-force}=${networkid:+$networkid,}$o"
  315. done
  316. }
  317. dhcp_domain_add() {
  318. local cfg="$1"
  319. local ip name names record
  320. config_get names "$cfg" name "$2"
  321. [ -n "$names" ] || return 0
  322. config_get ip "$cfg" ip "$3"
  323. [ -n "$ip" ] || return 0
  324. for name in $names; do
  325. record="${record:+$record }$name"
  326. done
  327. echo "$ip $record" >> $HOSTFILE
  328. }
  329. dhcp_srv_add() {
  330. local cfg="$1"
  331. config_get srv "$cfg" srv
  332. [ -n "$srv" ] || return 0
  333. config_get target "$cfg" target
  334. [ -n "$target" ] || return 0
  335. config_get port "$cfg" port
  336. [ -n "$port" ] || return 0
  337. config_get class "$cfg" class
  338. config_get weight "$cfg" weight
  339. local service="$srv,$target,$port${class:+,$class${weight:+,$weight}}"
  340. xappend "--srv-host=$service"
  341. }
  342. dhcp_mx_add() {
  343. local cfg="$1"
  344. local domain relay pref
  345. config_get domain "$cfg" domain
  346. [ -n "$domain" ] || return 0
  347. config_get relay "$cfg" relay
  348. [ -n "$relay" ] || return 0
  349. config_get pref "$cfg" pref 0
  350. local service="$domain,$relay,$pref"
  351. xappend "--mx-host=$service"
  352. }
  353. dhcp_cname_add() {
  354. local cfg="$1"
  355. local cname target
  356. config_get cname "$cfg" cname
  357. [ -n "$cname" ] || return 0
  358. config_get target "$cfg" target
  359. [ -n "$target" ] || return 0
  360. xappend "--cname=${cname},${target}"
  361. }
  362. dhcp_hostrecord_add() {
  363. local cfg="$1"
  364. local names addresses record val
  365. config_get names "$cfg" name "$2"
  366. if [ -z "$names" ]; then
  367. return 0
  368. fi
  369. config_get addresses "$cfg" ip "$3"
  370. if [ -z "$addresses" ]; then
  371. return 0
  372. fi
  373. for val in $names $addresses; do
  374. record="${record:+$record,}$val"
  375. done
  376. xappend "--host-record=$record"
  377. }
  378. dhcp_relay_add() {
  379. local cfg="$1"
  380. local local_addr server_addr interface
  381. config_get local_addr "$cfg" local_addr
  382. [ -n "$local_addr" ] || return 0
  383. config_get server_addr "$cfg" server_addr
  384. [ -n "$server_addr" ] || return 0
  385. config_get interface "$cfg" interface
  386. if [ -z "$interface" ]; then
  387. xappend "--dhcp-relay=$local_addr,$server_addr"
  388. else
  389. xappend "--dhcp-relay=$local_addr,$server_addr,$interface"
  390. fi
  391. }
  392. dnsmasq_start()
  393. {
  394. local cfg="$1" disabled
  395. config_get_bool disabled "$cfg" disabled 0
  396. [ "$disabled" -gt 0 ] && return 0
  397. # reset list of DOMAINS and DNS servers (for each dnsmasq instance)
  398. DNS_SERVERS=""
  399. DOMAIN=""
  400. CONFIGFILE="${BASECONFIGFILE}.${cfg}"
  401. CONFIGFILE_TMP="${CONFIGFILE}.$$"
  402. HOSTFILE="${BASEHOSTFILE}.${cfg}"
  403. TIMESTAMPFILE="${BASETIMESTAMPFILE}.${cfg}"
  404. BASEDHCPSTAMPFILE_CFG="${BASEDHCPSTAMPFILE}.${cfg}"
  405. # before we can call xappend
  406. mkdir -p /var/run/dnsmasq/
  407. mkdir -p $(dirname $CONFIGFILE)
  408. mkdir -p $(dirname $HOSTFILE)
  409. mkdir -p /var/lib/misc
  410. chown dnsmasq:dnsmasq /var/run/dnsmasq
  411. [ -f "$TIMESTAMPFILE" ] && rm -f "$TIMESTAMPFILE"
  412. echo "# auto-generated config file from /etc/config/dhcp" > $CONFIGFILE_TMP
  413. echo "# auto-generated config file from /etc/config/dhcp" > $HOSTFILE
  414. # if we did this last, we could override auto-generated config
  415. [ -f /etc/dnsmasq.conf ] && {
  416. xappend "--conf-file=/etc/dnsmasq.conf"
  417. }
  418. $PROG --version | grep -osqE "^Compile time options:.* DHCPv6( |$)" && DHCPv6CAPABLE=1 || DHCPv6CAPABLE=0
  419. append_bool "$cfg" authoritative "--dhcp-authoritative"
  420. append_bool "$cfg" nodaemon "--no-daemon"
  421. append_bool "$cfg" domainneeded "--domain-needed"
  422. append_bool "$cfg" filterwin2k "--filterwin2k"
  423. append_bool "$cfg" nohosts "--no-hosts"
  424. append_bool "$cfg" nonegcache "--no-negcache"
  425. append_bool "$cfg" strictorder "--strict-order"
  426. append_bool "$cfg" logqueries "--log-queries=extra"
  427. append_bool "$cfg" noresolv "--no-resolv"
  428. append_bool "$cfg" localise_queries "--localise-queries"
  429. append_bool "$cfg" readethers "--read-ethers"
  430. append_bool "$cfg" dbus "--enable-dbus"
  431. append_bool "$cfg" boguspriv "--bogus-priv"
  432. append_bool "$cfg" expandhosts "--expand-hosts"
  433. config_get tftp_root "$cfg" "tftp_root"
  434. [ -d "$tftp_root" ] && append_bool "$cfg" enable_tftp "--enable-tftp"
  435. append_bool "$cfg" tftp_no_fail "--tftp-no-fail"
  436. append_bool "$cfg" nonwildcard "--bind-dynamic"
  437. append_bool "$cfg" fqdn "--dhcp-fqdn"
  438. append_bool "$cfg" proxydnssec "--proxy-dnssec"
  439. append_bool "$cfg" localservice "--local-service"
  440. append_bool "$cfg" logdhcp "--log-dhcp"
  441. append_bool "$cfg" quietdhcp "--quiet-dhcp"
  442. append_bool "$cfg" sequential_ip "--dhcp-sequential-ip"
  443. append_bool "$cfg" allservers "--all-servers"
  444. append_bool "$cfg" noping "--no-ping"
  445. append_parm "$cfg" logfacility "--log-facility"
  446. append_parm "$cfg" dhcpscript "--dhcp-script"
  447. append_parm "$cfg" cachesize "--cache-size"
  448. append_parm "$cfg" dnsforwardmax "--dns-forward-max"
  449. append_parm "$cfg" port "--port"
  450. append_parm "$cfg" ednspacket_max "--edns-packet-max"
  451. append_parm "$cfg" dhcpleasemax "--dhcp-lease-max"
  452. append_parm "$cfg" "queryport" "--query-port"
  453. append_parm "$cfg" "minport" "--min-port"
  454. append_parm "$cfg" "maxport" "--max-port"
  455. append_parm "$cfg" "domain" "--domain"
  456. append_parm "$cfg" "local" "--server"
  457. config_list_foreach "$cfg" "server" append_server
  458. config_list_foreach "$cfg" "address" append_address
  459. config_list_foreach "$cfg" "ipset" append_ipset
  460. config_list_foreach "$cfg" "interface" append_interface
  461. config_list_foreach "$cfg" "notinterface" append_notinterface
  462. config_list_foreach "$cfg" "addnhosts" append_addnhosts
  463. config_list_foreach "$cfg" "bogusnxdomain" append_bogusnxdomain
  464. append_parm "$cfg" "leasefile" "--dhcp-leasefile" "/tmp/dhcp.leases"
  465. append_parm "$cfg" "resolvfile" "--resolv-file" "/tmp/resolv.conf.auto"
  466. append_parm "$cfg" "serversfile" "--servers-file"
  467. append_parm "$cfg" "tftp_root" "--tftp-root"
  468. append_parm "$cfg" "dhcp_boot" "--dhcp-boot"
  469. append_parm "$cfg" "local_ttl" "--local-ttl"
  470. append_parm "$cfg" "pxe_prompt" "--pxe-prompt"
  471. config_list_foreach "$cfg" "pxe_service" append_pxe_service
  472. config_get DOMAIN "$cfg" domain
  473. config_get_bool ADD_LOCAL_DOMAIN "$cfg" add_local_domain 1
  474. config_get_bool ADD_LOCAL_HOSTNAME "$cfg" add_local_hostname 1
  475. config_get_bool ADD_WAN_HOSTNAME "$cfg" add_wan_hostname 0
  476. config_get_bool readethers "$cfg" readethers
  477. [ "$readethers" = "1" -a \! -e "/etc/ethers" ] && touch /etc/ethers
  478. config_get resolvfile $cfg resolvfile
  479. config_get dhcpscript $cfg dhcpscript
  480. config_get leasefile $cfg leasefile "/tmp/dhcp.leases"
  481. [ -n "$leasefile" -a \! -e "$leasefile" ] && touch "$leasefile"
  482. config_get_bool cachelocal "$cfg" cachelocal 1
  483. config_get_bool noresolv "$cfg" noresolv 0
  484. if [ "$noresolv" != "1" ]; then
  485. config_get resolvfile "$cfg" resolvfile "/tmp/resolv.conf.auto"
  486. # So jail doesn't complain if file missing
  487. [ -n "$resolvfile" -a \! -e "$resolvfile" ] && touch "$resolvfile"
  488. fi
  489. config_get hostsfile "$cfg" dhcphostsfile
  490. [ -e "$hostsfile" ] && xappend "--dhcp-hostsfile=$hostsfile"
  491. local rebind
  492. config_get_bool rebind "$cfg" rebind_protection 1
  493. [ $rebind -gt 0 ] && {
  494. log_once \
  495. "DNS rebinding protection is active," \
  496. "will discard upstream RFC1918 responses!"
  497. xappend "--stop-dns-rebind"
  498. local rebind_localhost
  499. config_get_bool rebind_localhost "$cfg" rebind_localhost 0
  500. [ $rebind_localhost -gt 0 ] && {
  501. log_once "Allowing 127.0.0.0/8 responses"
  502. xappend "--rebind-localhost-ok"
  503. }
  504. append_rebind_domain() {
  505. log_once "Allowing RFC1918 responses for domain $1"
  506. xappend "--rebind-domain-ok=$1"
  507. }
  508. config_list_foreach "$cfg" rebind_domain append_rebind_domain
  509. }
  510. config_get_bool dnssec "$cfg" dnssec 0
  511. [ "$dnssec" -gt 0 ] && {
  512. xappend "--conf-file=$TRUSTANCHORSFILE"
  513. xappend "--dnssec"
  514. [ -x /etc/init.d/sysntpd ] && {
  515. /etc/init.d/sysntpd enabled
  516. [ "$?" -ne 0 -o "$(uci_get system.ntp.enabled)" = "1" ] && {
  517. [ -f "$TIMEVALIDFILE" ] || xappend "--dnssec-no-timecheck"
  518. }
  519. }
  520. append_bool "$cfg" dnsseccheckunsigned "--dnssec-check-unsigned"
  521. }
  522. config_get addmac "$cfg" addmac 0
  523. [ "$addmac" != "0" ] && {
  524. [ "$addmac" = "1" ] && addmac=
  525. xappend "--add-mac${addmac:+="$addmac"}"
  526. }
  527. dhcp_option_add "$cfg" "" 0
  528. xappend "--dhcp-broadcast=tag:needs-broadcast"
  529. xappend "--addn-hosts=$(dirname $HOSTFILE)"
  530. config_get dnsmasqconfdir "$cfg" confdir "/tmp/dnsmasq.d"
  531. [ ! -d "$dnsmasqconfdir" ] && mkdir -p $dnsmasqconfdir
  532. xappend "--conf-dir=$dnsmasqconfdir"
  533. xappend "--user=dnsmasq"
  534. xappend "--group=dnsmasq"
  535. echo >> $CONFIGFILE_TMP
  536. config_get_bool enable_tftp "$cfg" enable_tftp 0
  537. [ "$enable_tftp" -gt 0 ] && {
  538. config_get tftp_root "$cfg" tftp_root
  539. append EXTRA_MOUNT $tftp_root
  540. }
  541. config_foreach filter_dnsmasq host dhcp_host_add "$cfg"
  542. echo >> $CONFIGFILE_TMP
  543. config_foreach filter_dnsmasq boot dhcp_boot_add "$cfg"
  544. config_foreach filter_dnsmasq mac dhcp_mac_add "$cfg"
  545. config_foreach filter_dnsmasq tag dhcp_tag_add "$cfg"
  546. config_foreach filter_dnsmasq vendorclass dhcp_vendorclass_add "$cfg"
  547. config_foreach filter_dnsmasq userclass dhcp_userclass_add "$cfg"
  548. config_foreach filter_dnsmasq circuitid dhcp_circuitid_add "$cfg"
  549. config_foreach filter_dnsmasq remoteid dhcp_remoteid_add "$cfg"
  550. config_foreach filter_dnsmasq subscrid dhcp_subscrid_add "$cfg"
  551. config_foreach filter_dnsmasq match dhcp_match_add "$cfg"
  552. config_foreach filter_dnsmasq domain dhcp_domain_add "$cfg"
  553. config_foreach filter_dnsmasq hostrecord dhcp_hostrecord_add "$cfg"
  554. config_foreach filter_dnsmasq relay dhcp_relay_add "$cfg"
  555. echo >> $CONFIGFILE_TMP
  556. config_foreach filter_dnsmasq srvhost dhcp_srv_add "$cfg"
  557. config_foreach filter_dnsmasq mxhost dhcp_mx_add "$cfg"
  558. echo >> $CONFIGFILE_TMP
  559. config_get odhcpd_is_active odhcpd maindhcp
  560. if [ "$odhcpd_is_active" != "1" ]; then
  561. config_foreach filter_dnsmasq dhcp dhcp_add "$cfg"
  562. fi
  563. echo >> $CONFIGFILE_TMP
  564. config_foreach filter_dnsmasq cname dhcp_cname_add "$cfg"
  565. echo >> $CONFIGFILE_TMP
  566. echo >> $CONFIGFILE_TMP
  567. mv -f $CONFIGFILE_TMP $CONFIGFILE
  568. [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
  569. rm -f /tmp/resolv.conf
  570. [ $ADD_LOCAL_DOMAIN -eq 1 ] && [ -n "$DOMAIN" ] && {
  571. echo "search $DOMAIN" >> /tmp/resolv.conf
  572. }
  573. DNS_SERVERS="$DNS_SERVERS 127.0.0.1"
  574. for DNS_SERVER in $DNS_SERVERS ; do
  575. echo "nameserver $DNS_SERVER" >> /tmp/resolv.conf
  576. done
  577. }
  578. procd_open_instance $cfg
  579. procd_set_param command $PROG -C $CONFIGFILE -k -x /var/run/dnsmasq/dnsmasq."${cfg}".pid
  580. procd_set_param file $CONFIGFILE
  581. procd_set_param respawn
  582. local dnsmasqconffile="/etc/dnsmasq.${cfg}.conf"
  583. if [ ! -r "$dnsmasqconffile" ]; then
  584. dnsmasqconffile=/etc/dnsmasq.conf
  585. fi
  586. procd_add_jail dnsmasq ubus log
  587. procd_add_jail_mount $CONFIGFILE $TRUSTANCHORSFILE $HOSTFILE /etc/passwd /etc/group /etc/TZ /dev/null /dev/urandom $dnsmasqconffile $dnsmasqconfdir $resolvfile $dhcpscript /etc/hosts /etc/ethers $EXTRA_MOUNT
  588. procd_add_jail_mount_rw /var/run/dnsmasq/ $leasefile
  589. procd_close_instance
  590. }
  591. dnsmasq_stop()
  592. {
  593. local cfg="$1"
  594. config_get resolvfile "$cfg" "resolvfile"
  595. #relink /tmp/resolve.conf only for main instance
  596. [ "$resolvfile" = "/tmp/resolv.conf.auto" ] && {
  597. [ -f /tmp/resolv.conf ] && {
  598. rm -f /tmp/resolv.conf
  599. ln -s "$resolvfile" /tmp/resolv.conf
  600. }
  601. }
  602. rm -f ${BASEDHCPSTAMPFILE}.${cfg}.*.dhcp
  603. }
  604. service_triggers()
  605. {
  606. procd_add_reload_trigger "dhcp"
  607. procd_add_raw_trigger "interface.*" 2000 /etc/init.d/dnsmasq reload
  608. }
  609. boot()
  610. {
  611. BOOT=1
  612. start "$@"
  613. }
  614. start_service() {
  615. local instance="$1"
  616. local instance_found=0
  617. [ -n "$BOOT" ] && return
  618. . /lib/functions/network.sh
  619. config_cb() {
  620. local type="$1"
  621. local name="$2"
  622. if [ "$type" = "dnsmasq" ]; then
  623. if [ -n "$instance" -a "$instance" = "$name" ]; then
  624. instance_found=1
  625. fi
  626. fi
  627. }
  628. config_load dhcp
  629. if [ -n "$instance" ]; then
  630. [ "$instance_found" -gt 0 ] || return
  631. dnsmasq_start "$instance"
  632. else
  633. config_foreach dnsmasq_start dnsmasq
  634. fi
  635. }
  636. reload_service() {
  637. rc_procd start_service "$@"
  638. return 0
  639. }
  640. stop_service() {
  641. local instance="$1"
  642. local instance_found=0
  643. config_cb() {
  644. local type="$1"
  645. local name="$2"
  646. if [ "$type" = "dnsmasq" ]; then
  647. if [ -n "$instance" -a "$instance" = "$name" ]; then
  648. instance_found=1
  649. fi
  650. fi
  651. }
  652. config_load dhcp
  653. if [ -n "$instance" ]; then
  654. [ "$instance_found" -gt 0 ] || return
  655. dnsmasq_stop "$instance"
  656. else
  657. config_foreach dnsmasq_stop dnsmasq
  658. fi
  659. }