1
0

dnsmasq.init 21 KB

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