1
0

ppp.sh 7.4 KB

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