ppp.sh 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #!/bin/sh
  2. [ -x /usr/sbin/pppd ] || exit 0
  3. [ -n "$INCLUDE_ONLY" ] || {
  4. . /lib/functions.sh
  5. . /lib/functions/network.sh
  6. . ../netifd-proto.sh
  7. init_proto "$@"
  8. }
  9. ppp_select_ipaddr()
  10. {
  11. local subnets=$1
  12. local res
  13. local res_mask
  14. for subnet in $subnets; do
  15. local addr="${subnet%%/*}"
  16. local mask="${subnet#*/}"
  17. if [ -n "$res_mask" -a "$mask" != 32 ]; then
  18. [ "$mask" -gt "$res_mask" ] || [ "$res_mask" = 32 ] && {
  19. res="$addr"
  20. res_mask="$mask"
  21. }
  22. elif [ -z "$res_mask" ]; then
  23. res="$addr"
  24. res_mask="$mask"
  25. fi
  26. done
  27. echo "$res"
  28. }
  29. ppp_exitcode_tostring()
  30. {
  31. local errorcode=$1
  32. [ -n "$errorcode" ] || errorcode=5
  33. case "$errorcode" in
  34. 0) echo "OK" ;;
  35. 1) echo "FATAL_ERROR" ;;
  36. 2) echo "OPTION_ERROR" ;;
  37. 3) echo "NOT_ROOT" ;;
  38. 4) echo "NO_KERNEL_SUPPORT" ;;
  39. 5) echo "USER_REQUEST" ;;
  40. 6) echo "LOCK_FAILED" ;;
  41. 7) echo "OPEN_FAILED" ;;
  42. 8) echo "CONNECT_FAILED" ;;
  43. 9) echo "PTYCMD_FAILED" ;;
  44. 10) echo "NEGOTIATION_FAILED" ;;
  45. 11) echo "PEER_AUTH_FAILED" ;;
  46. 12) echo "IDLE_TIMEOUT" ;;
  47. 13) echo "CONNECT_TIME" ;;
  48. 14) echo "CALLBACK" ;;
  49. 15) echo "PEER_DEAD" ;;
  50. 16) echo "HANGUP" ;;
  51. 17) echo "LOOPBACK" ;;
  52. 18) echo "INIT_FAILED" ;;
  53. 19) echo "AUTH_TOPEER_FAILED" ;;
  54. 20) echo "TRAFFIC_LIMIT" ;;
  55. 21) echo "CNID_AUTH_FAILED";;
  56. *) echo "UNKNOWN_ERROR" ;;
  57. esac
  58. }
  59. ppp_generic_init_config() {
  60. proto_config_add_string username
  61. proto_config_add_string password
  62. proto_config_add_string keepalive
  63. proto_config_add_boolean keepalive_adaptive
  64. proto_config_add_int demand
  65. proto_config_add_string pppd_options
  66. proto_config_add_string 'connect:file'
  67. proto_config_add_string 'disconnect:file'
  68. proto_config_add_string ipv6
  69. proto_config_add_boolean authfail
  70. proto_config_add_int mtu
  71. proto_config_add_string pppname
  72. proto_config_add_string unnumbered
  73. }
  74. ppp_generic_setup() {
  75. local config="$1"; shift
  76. local localip
  77. json_get_vars ipv6 demand keepalive keepalive_adaptive username password pppd_options pppname unnumbered
  78. if [ "$ipv6" = 0 ]; then
  79. ipv6=""
  80. elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
  81. ipv6=1
  82. autoipv6=1
  83. fi
  84. if [ "${demand:-0}" -gt 0 ]; then
  85. demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
  86. else
  87. demand=""
  88. fi
  89. [ -n "$mtu" ] || json_get_var mtu mtu
  90. [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
  91. [ -n "$unnumbered" ] && {
  92. local subnets
  93. ( proto_add_host_dependency "$config" "" "$unnumbered" )
  94. network_get_subnets subnets "$unnumbered"
  95. localip=$(ppp_select_ipaddr "$subnets")
  96. [ -n "$localip" ] || {
  97. proto_block_restart "$config"
  98. return
  99. }
  100. }
  101. local lcp_failure="${keepalive%%[, ]*}"
  102. local lcp_interval="${keepalive##*[, ]}"
  103. local lcp_adaptive="lcp-echo-adaptive"
  104. [ "${lcp_failure:-0}" -lt 1 ] && lcp_failure=""
  105. [ "$lcp_interval" != "$keepalive" ] || lcp_interval=5
  106. [ "${keepalive_adaptive:-1}" -lt 1 ] && lcp_adaptive=""
  107. [ -n "$connect" ] || json_get_var connect connect
  108. [ -n "$disconnect" ] || json_get_var disconnect disconnect
  109. proto_run_command "$config" /usr/sbin/pppd \
  110. nodetach ipparam "$config" \
  111. ifname "$pppname" \
  112. ${localip:+$localip:} \
  113. ${lcp_failure:+lcp-echo-interval $lcp_interval lcp-echo-failure $lcp_failure $lcp_adaptive} \
  114. ${ipv6:++ipv6} \
  115. ${autoipv6:+set AUTOIPV6=1} \
  116. nodefaultroute \
  117. usepeerdns \
  118. $demand maxfail 1 \
  119. ${username:+user "$username" password "$password"} \
  120. ${connect:+connect "$connect"} \
  121. ${disconnect:+disconnect "$disconnect"} \
  122. ip-up-script /lib/netifd/ppp-up \
  123. ipv6-up-script /lib/netifd/ppp-up \
  124. ip-down-script /lib/netifd/ppp-down \
  125. ipv6-down-script /lib/netifd/ppp-down \
  126. ${mtu:+mtu $mtu mru $mtu} \
  127. "$@" $pppd_options
  128. }
  129. ppp_generic_teardown() {
  130. local interface="$1"
  131. local errorstring=$(ppp_exitcode_tostring $ERROR)
  132. case "$ERROR" in
  133. 0)
  134. ;;
  135. 2)
  136. proto_notify_error "$interface" "$errorstring"
  137. proto_block_restart "$interface"
  138. ;;
  139. 11|19)
  140. json_get_var authfail authfail
  141. proto_notify_error "$interface" "$errorstring"
  142. if [ "${authfail:-0}" -gt 0 ]; then
  143. proto_block_restart "$interface"
  144. fi
  145. ;;
  146. *)
  147. proto_notify_error "$interface" "$errorstring"
  148. ;;
  149. esac
  150. proto_kill_command "$interface"
  151. }
  152. # PPP on serial device
  153. proto_ppp_init_config() {
  154. proto_config_add_string "device"
  155. ppp_generic_init_config
  156. no_device=1
  157. available=1
  158. lasterror=1
  159. }
  160. proto_ppp_setup() {
  161. local config="$1"
  162. json_get_var device device
  163. ppp_generic_setup "$config" "$device"
  164. }
  165. proto_ppp_teardown() {
  166. ppp_generic_teardown "$@"
  167. }
  168. proto_pppoe_init_config() {
  169. ppp_generic_init_config
  170. proto_config_add_string "ac"
  171. proto_config_add_string "service"
  172. proto_config_add_string "host_uniq"
  173. lasterror=1
  174. }
  175. proto_pppoe_setup() {
  176. local config="$1"
  177. local iface="$2"
  178. for module in slhc ppp_generic pppox pppoe; do
  179. /sbin/insmod $module 2>&- >&-
  180. done
  181. json_get_var mtu mtu
  182. mtu="${mtu:-1492}"
  183. json_get_var ac ac
  184. json_get_var service service
  185. json_get_var host_uniq host_uniq
  186. ppp_generic_setup "$config" \
  187. plugin rp-pppoe.so \
  188. ${ac:+rp_pppoe_ac "$ac"} \
  189. ${service:+rp_pppoe_service "$service"} \
  190. ${host_uniq:+host-uniq "$host_uniq"} \
  191. "nic-$iface"
  192. }
  193. proto_pppoe_teardown() {
  194. ppp_generic_teardown "$@"
  195. }
  196. proto_pppoa_init_config() {
  197. ppp_generic_init_config
  198. proto_config_add_int "atmdev"
  199. proto_config_add_int "vci"
  200. proto_config_add_int "vpi"
  201. proto_config_add_string "encaps"
  202. no_device=1
  203. available=1
  204. lasterror=1
  205. }
  206. proto_pppoa_setup() {
  207. local config="$1"
  208. local iface="$2"
  209. for module in slhc ppp_generic pppox pppoatm; do
  210. /sbin/insmod $module 2>&- >&-
  211. done
  212. json_get_vars atmdev vci vpi encaps
  213. case "$encaps" in
  214. 1|vc) encaps="vc-encaps" ;;
  215. *) encaps="llc-encaps" ;;
  216. esac
  217. ppp_generic_setup "$config" \
  218. plugin pppoatm.so \
  219. ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
  220. ${encaps}
  221. }
  222. proto_pppoa_teardown() {
  223. ppp_generic_teardown "$@"
  224. }
  225. proto_pptp_init_config() {
  226. ppp_generic_init_config
  227. proto_config_add_string "server"
  228. proto_config_add_string "interface"
  229. available=1
  230. no_device=1
  231. lasterror=1
  232. }
  233. proto_pptp_setup() {
  234. local config="$1"
  235. local iface="$2"
  236. local ip serv_addr server interface
  237. json_get_vars interface server
  238. [ -n "$server" ] && {
  239. for ip in $(resolveip -t 5 "$server"); do
  240. ( proto_add_host_dependency "$config" "$ip" $interface )
  241. serv_addr=1
  242. done
  243. }
  244. [ -n "$serv_addr" ] || {
  245. echo "Could not resolve server address"
  246. sleep 5
  247. proto_setup_failed "$config"
  248. exit 1
  249. }
  250. local load
  251. for module in slhc ppp_generic ppp_async ppp_mppe ip_gre gre pptp; do
  252. grep -q "^$module " /proc/modules && continue
  253. /sbin/insmod $module 2>&- >&-
  254. load=1
  255. done
  256. [ "$load" = "1" ] && sleep 1
  257. ppp_generic_setup "$config" \
  258. plugin pptp.so \
  259. pptp_server $server \
  260. file /etc/ppp/options.pptp
  261. }
  262. proto_pptp_teardown() {
  263. ppp_generic_teardown "$@"
  264. }
  265. [ -n "$INCLUDE_ONLY" ] || {
  266. add_protocol ppp
  267. [ -f /usr/lib/pppd/*/rp-pppoe.so ] && add_protocol pppoe
  268. [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
  269. [ -f /usr/lib/pppd/*/pptp.so ] && add_protocol pptp
  270. }