ppp.sh 8.1 KB

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