dhcp.sh 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/bin/sh
  2. . /lib/functions.sh
  3. . ../netifd-proto.sh
  4. init_proto "$@"
  5. proto_dhcp_init_config() {
  6. renew_handler=1
  7. proto_config_add_string 'ipaddr:ipaddr'
  8. proto_config_add_string 'hostname:hostname'
  9. proto_config_add_string clientid
  10. proto_config_add_string vendorid
  11. proto_config_add_boolean 'broadcast:bool'
  12. proto_config_add_boolean 'release:bool'
  13. proto_config_add_string 'reqopts:list(string)'
  14. proto_config_add_boolean 'defaultreqopts:bool'
  15. proto_config_add_string iface6rd
  16. proto_config_add_string sendopts
  17. proto_config_add_boolean delegate
  18. proto_config_add_string zone6rd
  19. proto_config_add_string zone
  20. proto_config_add_string mtu6rd
  21. proto_config_add_string customroutes
  22. proto_config_add_boolean classlessroute
  23. }
  24. proto_dhcp_setup() {
  25. local config="$1"
  26. local iface="$2"
  27. local ipaddr hostname clientid vendorid broadcast release reqopts defaultreqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes classlessroute
  28. json_get_vars ipaddr hostname clientid vendorid broadcast release reqopts defaultreqopts iface6rd sendopts delegate zone6rd zone mtu6rd customroutes classlessroute
  29. local opt dhcpopts
  30. for opt in $reqopts; do
  31. append dhcpopts "-O $opt"
  32. done
  33. for opt in $sendopts; do
  34. append dhcpopts "-x $opt"
  35. done
  36. [ -z "$hostname" ] && hostname="$(cat /proc/sys/kernel/hostname)"
  37. [ "$defaultreqopts" = 0 ] && defaultreqopts="-o" || defaultreqopts=
  38. [ "$broadcast" = 1 ] && broadcast="-B" || broadcast=
  39. [ "$release" = 1 ] && release="-R" || release=
  40. [ -n "$clientid" ] && clientid="-x 0x3d:${clientid//:/}" || clientid="-C"
  41. [ -n "$iface6rd" ] && proto_export "IFACE6RD=$iface6rd"
  42. [ "$iface6rd" != 0 -a -f /lib/netifd/proto/6rd.sh ] && append dhcpopts "-O 212"
  43. [ -n "$zone6rd" ] && proto_export "ZONE6RD=$zone6rd"
  44. [ -n "$zone" ] && proto_export "ZONE=$zone"
  45. [ -n "$mtu6rd" ] && proto_export "MTU6RD=$mtu6rd"
  46. [ -n "$customroutes" ] && proto_export "CUSTOMROUTES=$customroutes"
  47. [ "$delegate" = "0" ] && proto_export "IFACE6RD_DELEGATE=0"
  48. # Request classless route option (see RFC 3442) by default
  49. [ "$classlessroute" = "0" ] || append dhcpopts "-O 121"
  50. proto_export "INTERFACE=$config"
  51. proto_run_command "$config" udhcpc \
  52. -p /var/run/udhcpc-$iface.pid \
  53. -s /lib/netifd/dhcp.script \
  54. -f -t 0 -i "$iface" \
  55. ${ipaddr:+-r $ipaddr} \
  56. ${hostname:+-x "hostname:$hostname"} \
  57. ${vendorid:+-V "$vendorid"} \
  58. $clientid $defaultreqopts $broadcast $release $dhcpopts
  59. }
  60. proto_dhcp_renew() {
  61. local interface="$1"
  62. # SIGUSR1 forces udhcpc to renew its lease
  63. local sigusr1="$(kill -l SIGUSR1)"
  64. [ -n "$sigusr1" ] && proto_kill_command "$interface" $sigusr1
  65. }
  66. proto_dhcp_teardown() {
  67. local interface="$1"
  68. proto_kill_command "$interface"
  69. }
  70. add_protocol dhcp