openwrt l2tp支持設置本地靜態ip地址

設備使用4G網絡,設備間需要通訊,又是短連接,必須要是靜態ip,所以選擇使用l2tp。然後LNS端通過/etc/ppp/chap-secrets爲每個帳號綁定一個ip,每個設備使用一個帳號,LNS這端設備默認的隧道保活時間爲60s。這時設備端(LAC)正常撥號時獲取到的ip爲LNS端綁定的,但是如果插拔一下網線,或者斷電重啓,即保活週期內未明確告知LNS端隧道斷開,會導致LAC端拿到的IP不是LNS上綁定的那個,因此會導致設備間的短連接通訊會失敗。

網絡工程師告知可以通過在LAC端設置靜態ip,來解決這個問題,並且在LNS端也不用麻煩的配置一堆的帳號和ip的綁定關係。分析l2tp網絡建立的過程知道,隧道通了後會使用ppp來做接下來的工作,而ppp使用IPCP協議來協商網絡參數,包括ip地址的分配,dns的分配等。既然是pppd在幹活,那麼肯定有參數設置,man pppd之,OPTIONS節第一個選項便是:

OPTIONS
       <local_IP_address>:<remote_IP_address>
              Set the local and/or remote interface IP addresses.  Either one may be omitted.  The IP addresses can be specified with a host  name  or  in  decimal  dot  notation  (e.g.
              150.234.56.78).   The default local address is the (first) IP address of the system (unless the noipdefault option is given).  The remote address will be obtained from the
              peer if not specified in any option.  Thus, in simple cases, this option is not required.  If a local and/or remote IP address is specified with this option, pppd will not
              accept a different value from the peer in the IPCP negotiation, unless the ipcp-accept-local and/or ipcp-accept-remote options are given, respectively.

      <本地ip地址>:<遠程ip地址>
              設置本地/遠程接口IP地址。這兩個地址均可省略。IP地址可以指定爲一個域名或者點分十進制格式(x.x.x.x)。默認的本地地址是系統網絡接口的原本IP地址(即建立隧道的那個網絡接口)(除非指定了noipdefault選項)。如果參      
              數裏面遠程ip地址未指定,則遠程地址將從對端獲取。因此,簡單來說,此選項不是必須的(因爲前面提到了local和remote都有默認值)。如果本地/遠程IP地址通過此選項指定了,需要對應的設置`ipcp-accept-local`或者 
              `ipcp-accept-remote`選項,否則pppd將不會接受和對端IPCP協商的值。

按照這裏的介紹,在/etc/ppp/options.xl2tpd裏面配置即可,也可以通過/lib/netifd/proto/l2tp.sh腳本里面使用xl2tpd-config傳參的方式實現。大致按照如下方式改動即可

+       local usestaticip localip
+       json_get_vars usestaticip  localip
+       [ "$usestaticip" = "1" ] && {
+               localaddr="${localip:+local ip=$localip}"
+       }
 
-       xl2tpd-control add l2tp-${interface} pppoptfile=${optfile} lns=${server} || {
+       xl2tpd-control add l2tp-${interface} pppoptfile=${optfile} lns=${server} ${localaddr} || {
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章