fix: conn is not active after restart ipsec.

This commit is contained in:
Daniel Ding
2024-06-08 12:02:19 +08:00
parent 6575af730e
commit e8d4cc09aa
5 changed files with 72 additions and 58 deletions

View File

@@ -12,9 +12,9 @@ fi
archive=$(grep -a -n "__ARCHIVE_BELOW__:$" $installer | cut -f1 -d:) archive=$(grep -a -n "__ARCHIVE_BELOW__:$" $installer | cut -f1 -d:)
OS="linux" OS="linux"
if type yum > /dev/null; then if type yum 2> /dev/null; then
OS="centos" OS="centos"
elif type apt > /dev/null; then elif type apt 2> /dev/null; then
OS="ubuntu" OS="ubuntu"
fi fi
@@ -40,37 +40,42 @@ function requires() {
function install() { function install() {
echo "Installing files ..." echo "Installing files ..."
local source=$(find $tmp -maxdepth 1 -name 'openlan-*') local source=$(find $tmp -maxdepth 1 -name 'openlan-*')
cd $source && { pushd $source
/usr/bin/env \cp -rf ./{etc,usr,var} / /usr/bin/env \cp -rf ./{etc,usr,var} /
chmod +x /var/openlan/script/*.sh chmod +x /var/openlan/script/*.sh
/usr/bin/env find ./ -type f > /usr/share/openlan.db /usr/bin/env find ./ -type f > /usr/share/openlan.db
} popd
} }
function post() { function post() {
echo "Initlizing ..." echo "Initializing ..."
if [ x"$DOCKER" == x"no" ] || [ x"$DOCKER" == x"" ]; then if [ x"$DOCKER" == x"no" ] || [ x"$DOCKER" == x"" ]; then
sysctl -p /etc/sysctl.d/90-openlan.conf sysctl -p /etc/sysctl.d/90-openlan.conf
fi fi
[ -e "/etc/openlan/switch/switch.json" ] || {
cp -rf /etc/openlan/switch/switch.json.example /etc/openlan/switch/switch.json
}
[ -e "/var/openlan/openvpn/dh.pem" ] || {
openssl dhparam -out /var/openlan/openvpn/dh.pem 2048
}
[ -e "/var/openlan/openvpn/ta.key" ] || {
openvpn --genkey --secret /var/openlan/openvpn/ta.key
}
if [ "$OS"x == "centos"x ]; then if [ "$OS"x == "centos"x ]; then
## Prepare openvpn.
[ -e "/var/openlan/openvpn/dh.pem" ] || {
openssl dhparam -out /var/openlan/openvpn/dh.pem 1024
}
[ -e "/var/openlan/openvpn/ta.key" ] || {
openvpn --genkey --secret /var/openlan/openvpn/ta.key
}
## Install CA.
cp -rf /var/openlan/cert/ca.crt /etc/pki/ca-trust/source/anchors/OpenLAN_CA.crt cp -rf /var/openlan/cert/ca.crt /etc/pki/ca-trust/source/anchors/OpenLAN_CA.crt
update-ca-trust update-ca-trust
elif [ "$OS"x == "ubuntu"x ]; then elif [ "$OS"x == "ubuntu"x ]; then
## Prepare openvpn.
[ -e "/var/openlan/openvpn/dh.pem" ] || {
openssl dhparam -out /var/openlan/openvpn/dh.pem 2048
}
[ -e "/var/openlan/openvpn/ta.key" ] || {
openvpn --genkey > /var/openlan/openvpn/ta.key
}
## Install CA.
cp -rf /var/openlan/cert/ca.crt /usr/local/share/ca-certificates/OpenLAN_CA.crt cp -rf /var/openlan/cert/ca.crt /usr/local/share/ca-certificates/OpenLAN_CA.crt
update-ca-certificates update-ca-certificates
fi fi
## Initialize NSS database
certutil -N -d sql:/var/lib/ipsec/nss --empty-password
} }
function finish() { function finish() {

View File

@@ -2,8 +2,15 @@
set -ex set -ex
## Clear xfrm
/sbin/ip xfrm policy flush /sbin/ip xfrm policy flush
/sbin/ip xfrm state flush /sbin/ip xfrm state flush
## Checking ipsec
/usr/libexec/ipsec/addconn --config /etc/ipsec.conf --checkconfig /usr/libexec/ipsec/addconn --config /etc/ipsec.conf --checkconfig
/usr/libexec/ipsec/_stackmanager start
/usr/sbin/ipsec --checknss
## Start pluto
/usr/libexec/ipsec/pluto --leak-detective --config /etc/ipsec.conf --nofork /usr/libexec/ipsec/pluto --leak-detective --config /etc/ipsec.conf --nofork

View File

@@ -4,9 +4,7 @@ ARG linux_bin
WORKDIR /root WORKDIR /root
RUN yum update -y && \ RUN yum update -y && yum install -y epel-release
yum install -y epel-release centos-release-openstack-train && \
yum install -y rdma-core libibverbs
ADD ${linux_bin} /tmp ADD ${linux_bin} /tmp
RUN DOCKER=yes /tmp/${linux_bin} RUN DOCKER=yes /tmp/${linux_bin}

View File

@@ -322,7 +322,6 @@ func (w *WorkerImpl) loadRoute(rt co.PrefixRoute) {
w.out.Info("WorkerImpl.loadRoute: %v success", rt_c.String()) w.out.Info("WorkerImpl.loadRoute: %v success", rt_c.String())
return nil return nil
}) })
} }
func (w *WorkerImpl) loadRoutes() { func (w *WorkerImpl) loadRoutes() {
@@ -1014,16 +1013,13 @@ func (w *WorkerImpl) findRoute(rt co.PrefixRoute) (co.PrefixRoute, int) {
} }
func (w *WorkerImpl) AddRoute(route *schema.PrefixRoute, switcher api.Switcher) error { func (w *WorkerImpl) AddRoute(route *schema.PrefixRoute, switcher api.Switcher) error {
rt := w.correctRoute(route) rt := w.correctRoute(route)
if _, index := w.findRoute(rt); index != -1 { if _, index := w.findRoute(rt); index != -1 {
w.out.Warn("WorkerImpl.AddRoute: route exist") w.out.Warn("WorkerImpl.AddRoute: route exist")
return nil return nil
} }
w.cfg.Routes = append(w.cfg.Routes, rt) w.cfg.Routes = append(w.cfg.Routes, rt)
w.out.Info("WorkerImpl.AddRoute: %v", rt) w.out.Info("WorkerImpl.AddRoute: %v", rt)
w.addIpSet(rt) w.addIpSet(rt)
@@ -1039,9 +1035,7 @@ func (w *WorkerImpl) AddRoute(route *schema.PrefixRoute, switcher api.Switcher)
} }
func (w *WorkerImpl) DelRoute(route *schema.PrefixRoute, switcher api.Switcher) error { func (w *WorkerImpl) DelRoute(route *schema.PrefixRoute, switcher api.Switcher) error {
correctRt := w.correctRoute(route) correctRt := w.correctRoute(route)
delRt, index := w.findRoute(correctRt) delRt, index := w.findRoute(correctRt)
if index == -1 { if index == -1 {
w.out.Warn("WorkerImpl.DelRoute: route not found") w.out.Warn("WorkerImpl.DelRoute: route not found")
@@ -1049,7 +1043,6 @@ func (w *WorkerImpl) DelRoute(route *schema.PrefixRoute, switcher api.Switcher)
} }
w.cfg.Routes = append(w.cfg.Routes[:index], w.cfg.Routes[index+1:]...) w.cfg.Routes = append(w.cfg.Routes[:index], w.cfg.Routes[index+1:]...)
w.delIpSet(delRt) w.delIpSet(delRt)
if inet, err := libol.ParseNet(delRt.Prefix); err == nil { if inet, err := libol.ParseNet(delRt.Prefix); err == nil {
w.delVPNSet(inet.String()) w.delVPNSet(inet.String())
@@ -1091,7 +1084,7 @@ const (
vxlanConn = ` vxlanConn = `
conn vxlan{{ .Segment }}-in conn vxlan{{ .Segment }}-in
keyingtries=%forever keyingtries=%forever
auto=route auto=start
ike=aes_gcm256-sha2_256 ike=aes_gcm256-sha2_256
esp=aes_gcm256 esp=aes_gcm256
ikev2=insist ikev2=insist
@@ -1104,7 +1097,7 @@ conn vxlan{{ .Segment }}-in
conn vxlan{{ .Segment }}-out conn vxlan{{ .Segment }}-out
keyingtries=%forever keyingtries=%forever
auto=route auto=start
ike=aes_gcm256-sha2_256 ike=aes_gcm256-sha2_256
esp=aes_gcm256 esp=aes_gcm256
ikev2=insist ikev2=insist
@@ -1118,7 +1111,7 @@ conn vxlan{{ .Segment }}-out
greConn = ` greConn = `
conn gre{{ .Segment }} conn gre{{ .Segment }}
keyingtries=%forever keyingtries=%forever
auto=route auto=start
ike=aes_gcm256-sha2_256 ike=aes_gcm256-sha2_256
esp=aes_gcm256 esp=aes_gcm256
ikev2=insist ikev2=insist
@@ -1134,6 +1127,35 @@ conn gre{{ .Segment }}
` `
) )
func (w *WorkerImpl) saveSec(name, tmpl string, data interface{}) error {
file := fmt.Sprintf("/etc/ipsec.d/%s", name)
out, err := libol.CreateFile(file)
if err != nil || out == nil {
return err
}
defer out.Close()
if obj, err := template.New("main").Parse(tmpl); err != nil {
return err
} else {
if err := obj.Execute(out, data); err != nil {
return err
}
}
return nil
}
func (w *WorkerImpl) startSecConn(name string) {
promise := libol.NewPromise()
promise.Go(func() error {
if out, err := libol.Exec("ipsec", "auto", "--start", "--asynchronous", name); err != nil {
w.out.Warn("WorkerImpl.startSecConn: %v %s", out, err)
return err
}
w.out.Info("WorkerImpl.startSecConn: %v success", name)
return nil
})
}
func (w *WorkerImpl) addSecConn(port *LinuxPort) error { func (w *WorkerImpl) addSecConn(port *LinuxPort) error {
connTmpl := "" connTmpl := ""
secTmpl := "" secTmpl := ""
@@ -1149,40 +1171,22 @@ func (w *WorkerImpl) addSecConn(port *LinuxPort) error {
} }
if secTmpl != "" { if secTmpl != "" {
file := fmt.Sprintf("/etc/ipsec.d/%s.secrets", name) if err := w.saveSec(name+".secrets", secTmpl, data); err != nil {
out, err := libol.CreateFile(file) w.out.Error("WorkerImpl.addSecConn %s", err)
if err != nil || out == nil {
return err return err
} }
defer out.Close()
if tmpl, err := template.New("main").Parse(secTmpl); err != nil {
return err
} else {
if err := tmpl.Execute(out, data); err != nil {
return err
}
}
libol.Exec("ipsec", "auto", "--rereadsecrets") libol.Exec("ipsec", "auto", "--rereadsecrets")
} }
if connTmpl != "" { if connTmpl != "" {
file := fmt.Sprintf("/etc/ipsec.d/%s.conf", name) if err := w.saveSec(name+".conf", connTmpl, data); err != nil {
out, err := libol.CreateFile(file) w.out.Error("WorkerImpl.addSecConn %s", err)
if err != nil || out == nil {
return err return err
} }
defer out.Close()
if tmpl, err := template.New("main").Parse(connTmpl); err != nil {
return err
} else {
if err := tmpl.Execute(out, data); err != nil {
return err
}
}
if data.Protocol == "vxlan" { if data.Protocol == "vxlan" {
libol.Exec("ipsec", "auto", "--start", "--asynchronous", name+"-in") w.startSecConn(name + "-in")
libol.Exec("ipsec", "auto", "--start", "--asynchronous", name+"-out") w.startSecConn(name + "-out")
} else if data.Protocol == "gre" { } else if data.Protocol == "gre" {
libol.Exec("ipsec", "auto", "--start", "--asynchronous", name) w.startSecConn(name)
} }
} }

View File

@@ -211,7 +211,7 @@ func (v *Switch) GetPort(listen string) string {
func (v *Switch) openPorts() { func (v *Switch) openPorts() {
port := v.GetPort(v.cfg.Listen) port := v.GetPort(v.cfg.Listen)
UdpPorts := []string{"500", "4500", "8472", "4789", port} UdpPorts := []string{"500", "4500", "8472", "4789", port}
TcpPorts := []string{"10000", port} TcpPorts := []string{port}
if v.cfg.Http != nil { if v.cfg.Http != nil {
TcpPorts = append(TcpPorts, v.GetPort(v.cfg.Http.Listen)) TcpPorts = append(TcpPorts, v.GetPort(v.cfg.Http.Listen))
} }