OPENWRT L2TPv3补丁文件

OPENWRT L2TPv3补丁文件

参考连接:https://dev.openwrt.org/browser/packages/net/l2tpv3tun/files/l2tp.sh?rev=22549

source:packages/net/l2tpv3tun/files/l2tp.sh@22549

Last change on this file since 22549 was 22549, checked in by jow, 7 years ago
[packages] l2tpv3tun: add /etc/config/network backend, supports briding, multiple tunnels, session and all options defined by proto static
File size: 4.8 KB

1. 补丁文件

但是我在调试的时候,没有发现l2tpv3tun命令,所以放弃了。使用ip-full中的ip命令完成的。


# l2tp.sh - L2TPv3 tunnel backend
# Copyright (c) 2010 OpenWrt.org

l2tp_next_tunnel_id() {
        local max=0
        local val
        for val in $(
                local l
                l2tpv3tun show tunnel | while read l; do
                       case "$l" in
                               Tunnel*,*encap*) l="${l#Tunnel }"; echo "${l%%,*}";;
                       esac
               done
       ); do
               [ "$val" -gt "$max" ] && max="$val"
       done
       echo $((max + 1))
}

l2tp_next_session_id() {
       local tunnel="$1"
       local max=0
       local val
       for val in $(
               local l
               l2tpv3tun show session${tunnel:+ tunnel_id "$tunnel"} | while read l; do
                       case "$l" in
                               Session*in*) l="${l#Session }"; echo "${l%% *}";;
                       esac
               done
       ); do
               [ "$val" -gt "$max" ] && max="$val"
       done
       echo $((max + 1))
}

l2tp_tunnel_exists() {
       test -n "$(l2tpv3tun show tunnel tunnel_id "$1" 2>/dev/null)"
}

l2tp_session_exists() {
       test -n "$(l2tpv3tun show session tunnel_id "$1" session_id "$2" 2>/dev/null)"
}

l2tp_ifname() {
       l2tpv3tun show session tunnel_id "$1" session_id "$2" 2>/dev/null | \
               sed -ne 's/^.*interface name: //p'
}

l2tp_lock() {
       lock /var/lock/l2tp-setup
}

l2tp_unlock() {
       lock -u /var/lock/l2tp-setup
}

l2tp_log() {
       logger -t "ifup-l2tp" "$@"
}


# Hook into scan_interfaces() to synthesize a .device option
# This is needed for /sbin/ifup to properly dispatch control
# to setup_interface_l2tp() even if no .ifname is set in
# the configuration.
scan_l2tp() {
       local dev
       config_get dev "$1" device
       config_set "$1" device "${dev:+$dev }l2tp-$1"
}

coldplug_interface_l2tp() {
       setup_interface_l2tp "l2tp-$1" "$1"
}

setup_interface_l2tp() {
       local iface="$1"
       local cfg="$2"
       local link="l2tp-$cfg"

       l2tp_lock

       # prevent recursion
       local up="$(uci_get_state network "$cfg" up 0)"
       [ "$up" = 0 ] || {
               l2tp_unlock
               return 0
       }

       local tunnel_id
       config_get tunnel_id "$cfg" tunnel_id
       [ -n "$tunnel_id" ] || {
               tunnel_id="$(l2tp_next_tunnel_id)"
               uci_set_state network "$cfg" tunnel_id "$tunnel_id"
               l2tp_log "No tunnel ID specified, assuming $tunnel_id"
       }

       local peer_tunnel_id
      config_get peer_tunnel_id "$cfg" peer_tunnel_id
      [ -n "$peer_tunnel_id" ] || {
              peer_tunnel_id="$tunnel_id"
              uci_set_state network "$cfg" peer_tunnel_id "$peer_tunnel_id"
              l2tp_log "No peer tunnel ID specified, assuming $peer_tunnel_id"
      }

      local encap
      config_get encap "$cfg" encap udp

      local sport dport
      [ "$encap" = udp ] && {
              config_get sport "$cfg" sport 1701
              config_get dport "$cfg" dport 1701
      }

      local peeraddr
      config_get peeraddr "$cfg" peeraddr
      [ -z "$peeraddr" ] && config_get peeraddr "$cfg" peer6addr

      local localaddr
      case "$peeraddr" in
              *:*) config_get localaddr "$cfg" local6addr ;;
              *)   config_get localaddr "$cfg" localaddr  ;;
      esac

      [ -n "$localaddr" -a -n "$peeraddr" ] || {
              l2tp_log "Missing local or peer address for tunnel $cfg - skipping"
              return 1
      }

      (
              while ! l2tp_tunnel_exists "$tunnel_id"; do
                      [ -n "$sport" ] && l2tpv3tun show tunnel 2>/dev/null | grep -q "ports: $sport/" && {
                              l2tp_log "There already is a tunnel with src port $sport - skipping"
                              l2tp_unlock
                              return 1
                      }

                      l2tpv3tun add tunnel tunnel_id "$tunnel_id" peer_tunnel_id "$peer_tunnel_id" \
                              encap "$encap" local "$localaddr" remote "$peeraddr" \
                              ${sport:+udp_sport "$sport"} ${dport:+udp_dport "$dport"}

                      # Wait for tunnel
                      sleep 1
              done


              local session_id
              config_get session_id "$cfg" session_id
              [ -n "$session_id" ] || {
                      session_id="$(l2tp_next_session_id "$tunnel_id")"
                      uci_set_state network "$cfg" session_id "$session_id"
                      l2tp_log "No session ID specified, assuming $session_id"
              }

              local peer_session_id
              config_get peer_session_id "$cfg" peer_session_id
              [ -n "$peer_session_id" ] || {
                      peer_session_id="$session_id"
                      uci_set_state network "$cfg" peer_session_id "$peer_session_id"
                      l2tp_log "No peer session ID specified, assuming $peer_session_id"
              }


              while ! l2tp_session_exists "$tunnel_id" "$session_id"; do
                      l2tpv3tun add session ifname "$link" tunnel_id "$tunnel_id" \
                              session_id "$session_id" peer_session_id "$peer_session_id"

                      # Wait for session
                      sleep 1
              done


              local dev
              config_get dev "$cfg" device

              local ifn
              config_get ifn "$cfg" ifname

              uci_set_state network "$cfg" ifname "${ifn:-$dev}"
              uci_set_state network "$cfg" device "$dev"

              local mtu
              config_get mtu "$cfg" mtu 1462

              local ttl
              config_get ttl "$cfg" ttl

              ip link set mtu "$mtu" ${ttl:+ ttl "$ttl"} dev "$link"

              # IP setup inherited from proto static
              prepare_interface "$link" "$cfg"
              setup_interface_static "${ifn:-$dev}" "$cfg"

              ip link set up dev "$link"

              uci_set_state network "$cfg" up 1
              l2tp_unlock
      ) &
}

stop_interface_l2tp() {
      local cfg="$1"
      local link="l2tp-$cfg"

      local tunnel=$(uci_get_state network "$cfg" tunnel_id)
      local session=$(uci_get_state network "$cfg" session_id)

      [ -n "$tunnel" ] && [ -n "$session" ] && {
              l2tpv3tun del session tunnel_id "$tunnel" session_id "$session"
              l2tpv3tun del tunnel tunnel_id "$tunnel"
      }
}
OpenWrt中配置***进行以下步骤: 1. 安装必要的软件包:在OpenWrt设备上安装必要的软件包,包括xl2tpd和ipsec-tools。可以使用以下命令进行安装: ```shell opkg update opkg install xl2tpd ipsec-tools ``` 2. 配置IPsec:编辑/etc/ipsec.conf文件,添加IPsec配置。可以参考以下示例配置: ```shell config setup protostack=netkey nat_traversal=yes virtual_private=%v4:10.0.0.0/8,%v4:192.168.0.0/16,%v4:172.16.0.0/12,%v6:fd00::/8,%v6:fe80::/10 conn L2TP-PSK-NAT rightsubnet=vhost:%priv also=L2TP-PSK-noNAT conn L2TP-PSK-noNAT authby=secret pfs=no auto=add keyingtries=3 rekey=no ikelifetime=8h keylife=1h type=transport left=%defaultroute leftprotoport=17/1701 right=%any rightprotoport=17/%any ``` 3. 配置L2TP:编辑/etc/xl2tpd/xl2tpd.conf文件,添加L2TP配置。可以参考以下示例配置: ```shell [global] ipsec saref = yes [lns default] ip range = 192.168.1.2-192.168.1.254 local ip = 192.168.1.1 require chap = yes refuse pap = yes require authentication = yes name = l2tpd ppp debug = yes pppoptfile = /etc/ppp/options.xl2tpd length bit = yes ``` 4. 配置PPP选项:编辑/etc/ppp/options.xl2tpd文件,添加PPP选项配置。可以参考以下示例配置: ```shell require-mschap-v2 ms-dns 192.168.100.99 auth mtu 1200 mru 1000 crtscts hide-password modem name l2tpd proxyarp lcp-echo-interval 30 lcp-echo-failure 4 ``` 5. 配置用户凭据:编辑/etc/ppp/chap-secrets文件,添加L2TP用户凭据。可以参考以下示例配置: ```shell # Secrets for authentication using CHAP # client server secret IP addresses username * password * ``` 6. 启动服务:启动IPsec和xl2tpd服务,并设置开机自启动。可以使用以下命令进行操作: ```shell /etc/init.d/ipsec start /etc/init.d/xl2tpd start /etc/init.d/ipsec enable /etc/init.d/xl2tpd enable ```***根据客户端的不同,配置连接参数,包括服务器IP地址、用户名和密码等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值