gluxon / wireguard-uapi-rs

MIT License
32 stars 10 forks source link

Add add_route and del_route commands #8

Open gluxon opened 4 years ago

gluxon commented 4 years ago

A helpful strace:

sudo strace ip -4 route add 127.3.1.1/32 dev test
sendmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base={{len=44, type=RTM_NEWROUTE, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1570389827, pid=0}, {rtm_family=AF_INET, rtm_dst_len=32, rtm_src_len=0, rtm_tos=0, rtm_table=RT_TABLE_MAIN, rtm_protocol=RTPROT_BOOT, rtm_scope=RT_SCOPE_LINK, rtm_type=RTN_UNICAST, rtm_flags=0}, [{{nla_len=8, nla_type=RTA_DST}, 127.3.1.1}, {{nla_len=8, nla_type=RTA_OIF}, if_nametoindex("test")}]}, iov_len=44}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 44
recvmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base=NULL, iov_len=0}], msg_iovlen=1, msg_controllen=0, msg_flags=MSG_TRUNC}, MSG_PEEK|MSG_TRUNC) = 36
recvmsg(3, {msg_name={sa_family=AF_NETLINK, nl_pid=0, nl_groups=00000000}, msg_namelen=12, msg_iov=[{iov_base={{len=36, type=NLMSG_ERROR, flags=NLM_F_CAPPED, seq=1570389827, pid=28982}, {error=0, msg={len=44, type=RTM_NEWROUTE, flags=NLM_F_REQUEST|NLM_F_ACK|NLM_F_EXCL|NLM_F_CREATE, seq=1570389827, pid=0}}}, iov_len=36}], msg_iovlen=1, msg_controllen=0, msg_flags=0}, 0) = 36

This is necessary for host -> peer communication through the internal VPN subnet. peer -> host works fine so this issue is lower priority.

jamesmcm commented 3 years ago

Is this equivalent to rtnetlink's route add? https://docs.rs/rtnetlink/0.7.0/rtnetlink/struct.RouteAddRequest.html

It might be useful to combine some of the functionality, at the moment for libvopono I'm looking at using a combination of rtnetlink for ip address assignment and routing, nftnl for adding nftables rules, and this crate for creating Wireguard tunnels (parsing wg-quick config files). But they all share a lot of Netlink calls fundamentally.

gluxon commented 3 years ago

Yup! They're the same thing. That's partly why I haven't prioritized adding these calls to this library.

If they are added it'll likely be behind a feature flag.