Skip to content

Commit 89b80c2

Browse files
authored
Add IP v6 user-space address support
1 parent 87ed3bc commit 89b80c2

File tree

1 file changed

+34
-26
lines changed

1 file changed

+34
-26
lines changed

src/wgapi_userspace.rs

+34-26
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,23 @@ impl WireguardInterfaceApi for WireguardApiUserspace {
149149
/// Assign IP address to network interface.
150150
fn assign_address(&self, address: &IpAddrMask) -> Result<(), WireguardInterfaceError> {
151151
debug!("Assigning address {address} to interface {}", self.ifname);
152+
let address_string = address.ip.to_string();
153+
152154
let output = if cfg!(target_os = "macos") {
153-
// On macOS, interface is point-to-point and requires a pair of addresses
154-
let address_string = address.ip.to_string();
155-
Command::new("ifconfig")
156-
.args([&self.ifname, &address_string, &address_string])
157-
.output()?
155+
match address.ip {
156+
IpAddr::V4(_) => {
157+
Command::new("ifconfig")
158+
// On macOS ipv4, interface is point-to-point and requires a pair of addresses
159+
.args([&self.ifname, "inet", &address_string, &address_string])
160+
.output()?
161+
}
162+
IpAddr::V6(_) => Command::new("ifconfig")
163+
.args([&self.ifname, "inet6", &address_string])
164+
.output()?,
165+
}
158166
} else {
159167
Command::new("ifconfig")
160-
.args([&self.ifname, &address.to_string()])
168+
.args([&self.ifname, &address_string])
161169
.output()?
162170
};
163171
check_command_output_status(output)?;
@@ -187,35 +195,35 @@ impl WireguardInterfaceApi for WireguardApiUserspace {
187195
/// Add peer addresses to network routing table.
188196
///
189197
/// # Linux:
190-
/// On a Linux system, the `sysctl` command is required to work if using `0.0.0.0/0` or `::/0`.
191-
/// For every allowed IP, it runs:
192-
/// `ip <ip_version> route add <allowed_ip> dev <ifname>`
193-
/// `<ifname>` - interface name while creating api
194-
/// `<ip_version>` - `-4` or `-6` based on allowed ip type
198+
/// On a Linux system, the `sysctl` command is required to work if using `0.0.0.0/0` or `::/0`.
199+
/// For every allowed IP, it runs:
200+
/// `ip <ip_version> route add <allowed_ip> dev <ifname>`
201+
/// `<ifname>` - interface name while creating api
202+
/// `<ip_version>` - `-4` or `-6` based on allowed ip type
195203
/// `<allowed_ip>`- one of [Peer](crate::Peer) allowed ip
196204
///
197205
/// For `0.0.0.0/0` or `::/0` allowed IP, it runs belowed additional commands in order:
198-
/// - `ip <ip_version> route add 0.0.0.0/0 dev <ifname> table <fwmark>`
199-
/// `<fwmark>` - fwmark attribute of [Host](crate::Host) or 51820 default if value is `None`.
200-
/// `<ifname>` - Interface name.
201-
/// - `ip <ip_version> rule add not fwmark <fwmark> table <fwmark>`.
202-
/// - `ip <ip_version> rule add table main suppress_prefixlength 0`.
203-
/// - `sysctl -q net.ipv4.conf.all.src_valid_mark=1` - runs only for `0.0.0.0/0`.
204-
/// - `iptables-restore -n`. For `0.0.0.0/0` only.
205-
/// - `iptables6-restore -n`. For `::/0` only.
206+
/// - `ip <ip_version> route add 0.0.0.0/0 dev <ifname> table <fwmark>`
207+
/// `<fwmark>` - fwmark attribute of [Host](crate::Host) or 51820 default if value is `None`.
208+
/// `<ifname>` - Interface name.
209+
/// - `ip <ip_version> rule add not fwmark <fwmark> table <fwmark>`.
210+
/// - `ip <ip_version> rule add table main suppress_prefixlength 0`.
211+
/// - `sysctl -q net.ipv4.conf.all.src_valid_mark=1` - runs only for `0.0.0.0/0`.
212+
/// - `iptables-restore -n`. For `0.0.0.0/0` only.
213+
/// - `iptables6-restore -n`. For `::/0` only.
206214
/// Based on IP type `<ip_version>` will be equal to `-4` or `-6`.
207215
///
208216
///
209217
/// # macOS, FreeBSD:
210-
/// For every allowed IP, it runs:
211-
/// - `route -q -n add <inet> allowed_ip -interface if_name`
212-
/// `ifname` - interface name while creating api
218+
/// For every allowed IP, it runs:
219+
/// - `route -q -n add <inet> allowed_ip -interface if_name`
220+
/// `ifname` - interface name while creating api
213221
/// `allowed_ip`- one of [Peer](crate::Peer) allowed ip
214222
/// For `0.0.0.0/0` or `::/0` allowed IP, it adds default routing and skips other routings.
215-
/// - `route -q -n add <inet> 0.0.0.0/1 -interface if_name`.
216-
/// - `route -q -n add <inet> 128.0.0.0/1 -interface if_name`.
217-
/// - `route -q -n add <inet> <endpoint> -gateway <gateway>`
218-
/// `<endpoint>` - Add routing for every unique Peer endpoint.
223+
/// - `route -q -n add <inet> 0.0.0.0/1 -interface if_name`.
224+
/// - `route -q -n add <inet> 128.0.0.0/1 -interface if_name`.
225+
/// - `route -q -n add <inet> <endpoint> -gateway <gateway>`
226+
/// `<endpoint>` - Add routing for every unique Peer endpoint.
219227
/// `<gateway>`- Gateway extracted using `netstat -nr -f <inet>`.
220228
fn configure_peer_routing(&self, peers: &[Peer]) -> Result<(), WireguardInterfaceError> {
221229
add_peer_routing(peers, &self.ifname)

0 commit comments

Comments
 (0)