Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wireguard set example #9

Merged
merged 3 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ description = "Wireguard generic netlink packet definitions"
[dependencies]
anyhow = "1.0.42"
byteorder = "1.4.3"
getrandom = "0.2.10"
libc = "0.2.98"
log = "0.4.14"
netlink-packet-generic = { version = "0.3.3" }
Expand Down
68 changes: 68 additions & 0 deletions examples/set_wireguard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// SPDX-License-Identifier: MIT

use futures::StreamExt;
use genetlink::new_connection;
use getrandom;
use netlink_packet_core::{
NetlinkMessage, NetlinkPayload, NLM_F_ACK, NLM_F_REQUEST,
};
use netlink_packet_generic::GenlMessage;
use netlink_packet_wireguard::constants::*;
use netlink_packet_wireguard::{nlas::WgDeviceAttrs, Wireguard, WireguardCmd};
use std::env::args;

#[tokio::main]
async fn main() {
env_logger::init();

let argv: Vec<String> = args().collect();
if argv.len() < 2 {
eprintln!("Usage: set_wireguard <ifname>");
return;
}

// The wireguard interface need to exist before executing this code.
// This can be done with `ip link <name> type wireguard` command.
let name = argv[1].clone();
let priv_key = generate_priv_key();

let (connection, mut handle, _) = new_connection().unwrap();
let _ = tokio::spawn(connection);

let nlas = vec![
WgDeviceAttrs::IfName(name),
WgDeviceAttrs::PrivateKey(priv_key),
WgDeviceAttrs::ListenPort(51820),
];

let genlmsg: GenlMessage<Wireguard> =
GenlMessage::from_payload(Wireguard {
cmd: WireguardCmd::SetDevice,
nlas,
});
let mut nlmsg = NetlinkMessage::from(genlmsg);
nlmsg.header.flags = NLM_F_REQUEST | NLM_F_ACK;

let mut res = handle.request(nlmsg).await.unwrap();

if let Some(result) = res.next().await {
let rx_packet = result.unwrap();
match rx_packet.payload {
NetlinkPayload::Error(e) => {
eprintln!("Error: {:?}", e.to_io());
}
_ => (),
}
}
}

fn generate_priv_key() -> [u8; WG_KEY_LEN] {
let mut key = [0u8; WG_KEY_LEN];
getrandom::getrandom(&mut key).unwrap();
// modify random bytes using algorithm described
// at https://cr.yp.to/ecdh.html.
key[0] = key[0] & 248;
key[31] = key[31] & 127;
key[31] = key[31] | 64;
key
}