头图

前言

之前在openwrt平台,开源代码中已经内嵌了wireguard包,所以只需要在内核编译选项中把wireguard打开即可。后来做的一个项目不是openwrt系统了,就是一个sdk软件包,用的linux内核是4.4.140。linux5.6发布之后才支持wireguard,所以在linux4.4就要集成wireguard。

内核模块集成wireguard

  • gcc版本:>=4.7
  • 安装工具

    sudo apt-get install libelf-dev linux-headers-$(uname -r) build-essential pkg-config
  • 内核要求
    WireGuard 需要 Linux ≥3.10,并具有以下配置选项,这些选项可能已在您的内核中配置,特别是如果您通过分发包进行安装。

    • CONFIG_NET 用于基本网络支持
    • CONFIG_INET 用于基本 IP 支持
    • CONFIG_NET_UDP_TUNNEL 用于发送和接收 UDP 数据包
    • CONFIG_CRYPTO_ALGAPI 对于 crypto_xor

    这些选项中的一些(但不是全部)直接对应于menuconfig条目。要启用上述选项,请在menuconfig中选择:

    [*] Networking support (NET) -->
      Networking options -->
          [*] TCP/IP networking (INET)
          [*]   IP: Foo (IP protocols) over UDP (NET_FOU)
    [*] Cryptographic API (CRYPTO) -->
          [*] Cryptographic algorithm manager (CRYPTO_MANAGER)

内置内核模块

  • 下载内核模块放到相应的目录中

    git clone https://github.com/WireGuard/wireguard-linux-compat.git
  • 执行脚本以修改内核参数:

    ${wireguard-linux-compat-DIR}/jury-rig.sh  ${KERNEL_DIR}

    根据自己目录修改上述命令中的 ${wireguard-linux-compat-DIR}${KERNEL_DIR}值。

  • 执行完可以看到linux/net目录下多了一个wireguard的软连接
  • linux/net/Makefile多了一个配置

    obj-$(CONFIG_WIREGUARD) += wireguard/
  • linux/net/Kconfig多了一行

    source "net/wireguard/Kconfig"
  • 执行make menuconfig 打开wireguard项即可。

至此,内核模块是支持了, 编译后linux-4.4.x/net/wireguard$目录下会生成wireguard.ko

下载wireguard的应用程序

下载wireguard工具:

git clone  https://github.com/WireGuard/wireguard-tools.git

打开编译选项进行编译,目录下会生成一个可执行文件wg

# wg --help
Usage: wg <cmd> [<args>]

Available subcommands:
  show: Shows the current configuration and device information
  showconf: Shows the current configuration of a given WireGuard interface, for use with `setconf'
  set: Change the current configuration, add peers, remove peers, or change peers
  setconf: Applies a configuration file to a WireGuard interface
  addconf: Appends a configuration file to a WireGuard interface
  genkey: Generates a new private key and writes it to stdout
  genpsk: Generates a new preshared key and writes it to stdout
  pubkey: Reads a private key from stdin and writes a public key to stdout
You may pass `--help' to any of these subcommands to view usage.

该可执行文件用于创建wg隧道。

欢迎关注个人博客沟通交流

NULL
30 声望0 粉丝