Guest User

Untitled

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