1
0

ppp.sh 7.2 KB

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