ppp.sh 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. [ -e /proc/sys/net/ipv6 ] && 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. proto_config_add_boolean persist
  74. proto_config_add_int maxfail
  75. proto_config_add_int holdoff
  76. proto_config_add_boolean sourcefilter
  77. proto_config_add_boolean delegate
  78. }
  79. ppp_generic_setup() {
  80. local config="$1"; shift
  81. local localip
  82. json_get_vars ip6table demand keepalive keepalive_adaptive username password pppd_options pppname unnumbered persist maxfail holdoff peerdns sourcefilter delegate
  83. [ ! -e /proc/sys/net/ipv6 ] && ipv6=0 || json_get_var ipv6 ipv6
  84. if [ "$ipv6" = 0 ]; then
  85. ipv6=""
  86. elif [ -z "$ipv6" -o "$ipv6" = auto ]; then
  87. ipv6=1
  88. autoipv6=1
  89. fi
  90. if [ "${demand:-0}" -gt 0 ]; then
  91. demand="precompiled-active-filter /etc/ppp/filter demand idle $demand"
  92. else
  93. demand=""
  94. fi
  95. if [ -n "$persist" ]; then
  96. [ "${persist}" -lt 1 ] && persist="nopersist" || persist="persist"
  97. fi
  98. if [ -z "$maxfail" ]; then
  99. [ "$persist" = "persist" ] && maxfail=0 || maxfail=1
  100. fi
  101. [ -n "$mtu" ] || json_get_var mtu mtu
  102. [ -n "$pppname" ] || pppname="${proto:-ppp}-$config"
  103. [ -n "$unnumbered" ] && {
  104. local subnets
  105. ( proto_add_host_dependency "$config" "" "$unnumbered" )
  106. network_get_subnets subnets "$unnumbered"
  107. localip=$(ppp_select_ipaddr "$subnets")
  108. [ -n "$localip" ] || {
  109. proto_block_restart "$config"
  110. return
  111. }
  112. }
  113. [ -n "$keepalive" ] || keepalive="5 1"
  114. local lcp_failure="${keepalive%%[, ]*}"
  115. local lcp_interval="${keepalive##*[, ]}"
  116. local lcp_adaptive="lcp-echo-adaptive"
  117. [ "${lcp_failure:-0}" -lt 1 ] && lcp_failure=""
  118. [ "$lcp_interval" != "$keepalive" ] || lcp_interval=5
  119. [ "${keepalive_adaptive:-1}" -lt 1 ] && lcp_adaptive=""
  120. [ -n "$connect" ] || json_get_var connect connect
  121. [ -n "$disconnect" ] || json_get_var disconnect disconnect
  122. [ "$sourcefilter" = "0" ] || sourcefilter=""
  123. [ "$delegate" != "0" ] && delegate=""
  124. proto_run_command "$config" /usr/sbin/pppd \
  125. nodetach ipparam "$config" \
  126. ifname "$pppname" \
  127. ${localip:+$localip:} \
  128. ${lcp_failure:+lcp-echo-interval $lcp_interval lcp-echo-failure $lcp_failure $lcp_adaptive} \
  129. ${ipv6:++ipv6} \
  130. ${autoipv6:+set AUTOIPV6=1} \
  131. ${ip6table:+set IP6TABLE=$ip6table} \
  132. ${peerdns:+set PEERDNS=$peerdns} \
  133. ${sourcefilter:+set NOSOURCEFILTER=1} \
  134. ${delegate:+set DELEGATE=0} \
  135. nodefaultroute \
  136. usepeerdns \
  137. $demand $persist maxfail $maxfail \
  138. ${holdoff:+holdoff "$holdoff"} \
  139. ${username:+user "$username" password "$password"} \
  140. ${connect:+connect "$connect"} \
  141. ${disconnect:+disconnect "$disconnect"} \
  142. ip-up-script /lib/netifd/ppp-up \
  143. ${ipv6:+ipv6-up-script /lib/netifd/ppp6-up} \
  144. ip-down-script /lib/netifd/ppp-down \
  145. ${ipv6:+ipv6-down-script /lib/netifd/ppp-down} \
  146. ${mtu:+mtu $mtu mru $mtu} \
  147. "$@" $pppd_options
  148. }
  149. ppp_generic_teardown() {
  150. local interface="$1"
  151. local errorstring=$(ppp_exitcode_tostring $ERROR)
  152. case "$ERROR" in
  153. 0)
  154. ;;
  155. 2)
  156. proto_notify_error "$interface" "$errorstring"
  157. proto_block_restart "$interface"
  158. ;;
  159. 11|19)
  160. json_get_var authfail authfail
  161. proto_notify_error "$interface" "$errorstring"
  162. if [ "${authfail:-0}" -gt 0 ]; then
  163. proto_block_restart "$interface"
  164. fi
  165. ;;
  166. *)
  167. proto_notify_error "$interface" "$errorstring"
  168. ;;
  169. esac
  170. proto_kill_command "$interface"
  171. }
  172. # PPP on serial device
  173. proto_ppp_init_config() {
  174. proto_config_add_string "device"
  175. ppp_generic_init_config
  176. no_device=1
  177. available=1
  178. lasterror=1
  179. }
  180. proto_ppp_setup() {
  181. local config="$1"
  182. json_get_var device device
  183. ppp_generic_setup "$config" "$device"
  184. }
  185. proto_ppp_teardown() {
  186. ppp_generic_teardown "$@"
  187. }
  188. proto_pppoe_init_config() {
  189. ppp_generic_init_config
  190. proto_config_add_string "ac"
  191. proto_config_add_string "service"
  192. proto_config_add_string "host_uniq"
  193. proto_config_add_int "padi_attempts"
  194. proto_config_add_int "padi_timeout"
  195. lasterror=1
  196. }
  197. proto_pppoe_setup() {
  198. local config="$1"
  199. local iface="$2"
  200. /sbin/modprobe -qa slhc ppp_generic pppox pppoe
  201. json_get_var mtu mtu
  202. mtu="${mtu:-1492}"
  203. json_get_var ac ac
  204. json_get_var service service
  205. json_get_var host_uniq host_uniq
  206. json_get_var padi_attempts padi_attempts
  207. json_get_var padi_timeout padi_timeout
  208. ppp_generic_setup "$config" \
  209. plugin pppoe.so \
  210. ${ac:+rp_pppoe_ac "$ac"} \
  211. ${service:+rp_pppoe_service "$service"} \
  212. ${host_uniq:+host-uniq "$host_uniq"} \
  213. ${padi_attempts:+pppoe-padi-attempts $padi_attempts} \
  214. ${padi_timeout:+pppoe-padi-timeout $padi_timeout} \
  215. "nic-$iface"
  216. }
  217. proto_pppoe_teardown() {
  218. ppp_generic_teardown "$@"
  219. }
  220. proto_pppoa_init_config() {
  221. ppp_generic_init_config
  222. proto_config_add_int "atmdev"
  223. proto_config_add_int "vci"
  224. proto_config_add_int "vpi"
  225. proto_config_add_string "encaps"
  226. no_device=1
  227. available=1
  228. lasterror=1
  229. }
  230. proto_pppoa_setup() {
  231. local config="$1"
  232. local iface="$2"
  233. /sbin/modprobe -qa slhc ppp_generic pppox pppoatm
  234. json_get_vars atmdev vci vpi encaps
  235. case "$encaps" in
  236. 1|vc) encaps="vc-encaps" ;;
  237. *) encaps="llc-encaps" ;;
  238. esac
  239. ppp_generic_setup "$config" \
  240. plugin pppoatm.so \
  241. ${atmdev:+$atmdev.}${vpi:-8}.${vci:-35} \
  242. ${encaps}
  243. }
  244. proto_pppoa_teardown() {
  245. ppp_generic_teardown "$@"
  246. }
  247. proto_pptp_init_config() {
  248. ppp_generic_init_config
  249. proto_config_add_string "server"
  250. proto_config_add_string "interface"
  251. available=1
  252. no_device=1
  253. lasterror=1
  254. }
  255. proto_pptp_setup() {
  256. local config="$1"
  257. local iface="$2"
  258. local ip serv_addr server interface
  259. json_get_vars interface server
  260. [ -n "$server" ] && {
  261. for ip in $(resolveip -t 5 "$server"); do
  262. ( proto_add_host_dependency "$config" "$ip" $interface )
  263. serv_addr=1
  264. done
  265. }
  266. [ -n "$serv_addr" ] || {
  267. echo "Could not resolve server address"
  268. sleep 5
  269. proto_setup_failed "$config"
  270. exit 1
  271. }
  272. /sbin/modprobe -qa slhc ppp_generic ppp_async ppp_mppe ip_gre gre pptp
  273. sleep 1
  274. ppp_generic_setup "$config" \
  275. plugin pptp.so \
  276. pptp_server $server \
  277. file /etc/ppp/options.pptp
  278. }
  279. proto_pptp_teardown() {
  280. ppp_generic_teardown "$@"
  281. }
  282. [ -n "$INCLUDE_ONLY" ] || {
  283. add_protocol ppp
  284. [ -f /usr/lib/pppd/*/pppoe.so ] && add_protocol pppoe
  285. [ -f /usr/lib/pppd/*/pppoatm.so ] && add_protocol pppoa
  286. [ -f /usr/lib/pppd/*/pptp.so ] && add_protocol pptp
  287. }