中文 English

How to Run N2N on Synology (DSM) - Compilation Guide

Published: 2021-01-15
network N2N dsm synology nas

Synology (abbreviated as DSM below) is currently the most famous NAS system, bar none. How to run N2N directly on DSM? Of course, cross-compilation is needed again 😂. The following cross-compilation operations require a basic understanding of Linux and proficiency in vim operations.

Pre-compiled Files

Compilation environment: Ubuntu 20.04.1 LTS Linux vm-n2n-cc2 5.4.0-59-generic #65-Ubuntu SMP Thu Dec 10 12:01:51 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux

wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.2.3/libssl.so.1.1 -O libssl.so.1.1 
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.2.3/libcrypto.so.1.1 -O libcrypto.so.1.1
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.2.3/edge -O edge
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.2.3/supernode -O supernode
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.2.3/renew -O renew
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.1.7/libssl.so.1.1 -O libssl.so.1.1
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.1.7/libcrypto.so.1.1 -O libcrypto.so.1.1
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.1.7/edge -O edge
wget https://download.margrop.net/d/oneindex/network/n2n_2.8_stable/DSM6.1.7/supernode -O supernode

N2N Compilation Environment Setup

Cross-Compilation Environment Setup

First, download the toolchain corresponding to your target machine (this is the compilation tool suite for the target platform, including gcc, g++, ld and libraries).

In 群晖的开发工具下载首页, navigate to the directory for your DSM version. For example, my version is 6.2.3. Go to the latest DSM 6.2 Tool Chains, where you will find compressed packages for different processors. Find the one that matches your CPU.

You can find the CPU platform code name through the file under ls /sys/module | grep bios.

As you can see, the CPU platform of my Synology is apollolake.

Then comes the Linux kernel version and CPU architecture. You can check these using the uname command.

uname -a
Linux Fluxworks_NAS 4.4.59+ #25426 SMP PREEMPT Wed Jul 8 03:21:29 CST 2020 x86_64 GNU/Linux synology_apollolake_918+

Extract and add to environment variables:

tar -xf apollolake-gcc493_glibc220_linaro_x86_64-GPL.txz
# cd x86_64-pc-linux-gnu/bin
# ls
x86_64-pc-linux-gnu-addr2line  x86_64-pc-linux-gnu-cc            x86_64-pc-linux-gnu-elfedit    x86_64-pc-linux-gnu-gcc-ar      x86_64-pc-linux-gnu-gprof   x86_64-pc-linux-gnu-nm        x86_64-pc-linux-gnu-ranlib   x86_64-pc-linux-gnu-strip
x86_64-pc-linux-gnu-ar         x86_64-pc-linux-gnu-c++filt       x86_64-pc-linux-gnu-g++        x86_64-pc-linux-gnu-gcc-nm      x86_64-pc-linux-gnu-ld      x86_64-pc-linux-gnu-objcopy   x86_64-pc-linux-gnu-readelf
x86_64-pc-linux-gnu-as         x86_64-pc-linux-gnu-cpp           x86_64-pc-linux-gnu-gcc        x86_64-pc-linux-gnu-gcc-ranlib  x86_64-pc-linux-gnu-ld.bfd  x86_64-pc-linux-gnu-objdump   x86_64-pc-linux-gnu-size
x86_64-pc-linux-gnu-c++        x86_64-pc-linux-gnu-ct-ng.config  x86_64-pc-linux-gnu-gcc-4.9.3  x86_64-pc-linux-gnu-gcov        x86_64-pc-linux-gnu-ldd     x86_64-pc-linux-gnu-populate  x86_64-pc-linux-gnu-strings

You can see the gcc related executable programs. At this point, you can verify whether the programs are correct by running one of them. Then add the bin directory to PATH so the system can find your toolchain:

export PATH=$PATH:你的工具包的目录/x86_64-pc-linux-gnu/bin

OpenSSL Cross-Compilation

Download and extract OpenSSL source code

wget https://www.openssl.org/source/openssl-1.1.1i.tar.gz
tar -xvzf openssl-1.1.1i.tar.gz
cd openssl-1.1.1i

Configure the toolchain path and generate the Makefile file

export PATH=$PATH:/root/dsm/x86_64-pc-linux-gnu/bin
./config no-asm -shared --prefix=/opt/toolchain/openssl/install-x86_64

Search for :/CROSS_COMPILE= in Makefile and add x86_64-pc-linux-gnu- after it

vim Makefile
:/CROSS_COMPILE=
x86_64-pc-linux-gnu-

Delete the 2 -m64

:/-m64

Start cross-compiling OpenSSL

make
make install

Download N2N Source Code

cd ~
git clone https://github.com/ntop/n2n
cd n2n
git checkout 2.8-stable

N2N Cross-Compilation Preparation

cd ~/n2n
vim toolChain.cmake

Create a toolChain.cmake file in the directory of n2n and enter the following configuration:

SET(CMAKE_SYSTEM_NAME Linux)
SET(CMAKE_CROSSCOMPILING TRUE)
SET(CMAKE_CROSSCOMPILER "x86_64-pc-linux-gnu-")
SET(CMAKE_C_COMPILER "${CMAKE_CROSSCOMPILER}gcc")
SET(CMAKE_CXX_COMPILER "${CMAKE_CROSSCOMPILER}g++")
SET(CMAKE_FIND_ROOT_PATH /opt/toolchain/openssl/install-x86_64)
SET(CMAKE_VERBOSE_MAKEFILE on)

N2N Cross-Compilation

Use CMake to cross-compile n2n,

mkdir build
cd build
export PATH=$PATH:/root/dsm/x86_64-pc-linux-gnu/bin
cmake -DCMAKE_TOOLCHAIN_FILE=../toolChain.cmake ..
make

N2N Installation on Synology

Use WinSCP or wget (or similar software) to upload edge and supernode to the /usr/bin directory. Upload libssl.so.1.1 and libcrypto.so.1.1 to /lib64 (for 32-bit CPUs, upload to /lib). Set all file attributes to 0755.

chmod 755 edge
chmod 755 supernode
chmod 755 libssl.so.1.1
chmod 755 libcrypto.so.1.1
cp edge /usr/bin/
cp supernode /usr/bin/
cp libssl.so.1.1 /lib64/
cp libcrypto.so.1.1 /lib64/

Manually enable the tun module:

insmod /lib/modules/tun.ko

My white Synology was accidentally upgraded to the latest version dsm6.2.2. I later downgraded to 6.1.7 because with 6.2.2, half of the edges couldn’t be pinged. Even using the method below didn’t help. However, 6.1.7 worked perfectly without any issues. This shows that the 6.1.7 system has better network functionality. I accidentally discovered that Synology dsm6.1 and dsm6.2 (dsm6.0 status unknown) have normal N2N functionality right after startup, and N2N routing tables exist. However, Synology quickly starts deleting N2N routing tables. This is the reason why N2N networks are pingable right after startup but become disconnected shortly after, even with auto-start configured. Below is the solution. The August 15, 2018 method is for reference only. Download the file from here (hover over it, right-click, and select “Save Link As…”), save it as renew, and add it to the startup rc.local file after the edge command: /etc/new/renew & (set renew attributes to 0755). This way, even if you manually start N2N, it will add back the static routes that Synology deletes within 10 seconds.

Automatically enable the tun module and start edge:

cat <<EOF > /usr/local/etc/rc.d/tun.sh
!/bin/sh -e
insmod /lib/modules/tun.ko
sleep 30
/root/n2n/edge -d h0 -a 10.0.0.1 -c margrop -k UFDMIlrK3ueQz5mS -l blog.margrop.net:2345 -r -v -f &
#/etc/new/renew &
sleep 10
ifconfig h0 down
sleep 10
ifconfig h0 up
EOF

Grant executable permissions to the script:

chmod a+x /usr/local/etc/rc.d/tun.sh

Reference Articles

http://www.lucktu.com/archives/778.html 群晖交叉编译记录(编译subversion) 交叉编译N2N-LEDE Koolshare x86_64 CMake交叉编译配置 群晖(Synology)下N2N的设置方法