Skip to content

iptunnel: add support to ipip, ipip6 and ip6ip6 tunnels #167

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

liangwen12year
Copy link
Contributor

iptunnel: add support to ipip, ipip6 and ip6ip6 tunnels

All these tunnels use the IFLA_IPTUN_* netlink API. Therefore, both
IFLAN_INFO_KIND "ipip" and "ip6tnl" data is serialized using the
IpTunnel struct.

Unit tests added.

@liangwen12year liangwen12year force-pushed the ipip_support branch 4 times, most recently from 669f960 to 24fa311 Compare May 19, 2025 02:23
)?,
)))
}
_ => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no default in kernel for parsing this:

net/ipv4/ip_tunnel_core.c
1158:	if (data[IFLA_IPTUN_FLAGS]) {
1161:		flags = nla_get_be16(data[IFLA_IPTUN_FLAGS]);

net/ipv6/ip6_tunnel.c
1992:	if (data[IFLA_IPTUN_FLAGS])
1993:		parms->flags = nla_get_u32(data[IFLA_IPTUN_FLAGS]);
2104:		/* IFLA_IPTUN_FLAGS */
2134:	    nla_put_u32(skb, IFLA_IPTUN_FLAGS, parm->flags) ||
2170:	[IFLA_IPTUN_FLAGS]		= { .type = NLA_U32 },

net/ipv6/sit.c
1668:		/* IFLA_IPTUN_FLAGS */
1708:	    nla_put_be16(skb, IFLA_IPTUN_FLAGS,
1748:	[IFLA_IPTUN_FLAGS]		= { .type = NLA_U16 },

Always do explicit kind matching and fail for unknown kind.

Flags(TunnelFlags::from_bits_retain(u32::from(
parse_u16(payload).context(
"invalid IFLA_IPTUN_FLAGS value for SIT",
)?,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of storing u16 into u32.

I prefer InfoIpTunnel::Ipv6SitFlags(u16), InfoIpTunnel::Ipv6Flags(u32), InfoIpTunnel::Ipv4Flags(u16).

| Ipv6RdRelayPrefixLen(value) => {
NativeEndian::write_u16(buffer, *value)
}
Protocol(value) => buffer[0] = i32::from(*value) as u8,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The as is dangerous keyword because it discard data silently.

According to https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml , it is always u8.

So implement From<u8> for IpProtocol there instead of using as. When you do that, please fix src/rule/attribute.rs also.

Copy link
Member

@cathay4t cathay4t left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please also include unit test for ipv6 site support.

ffmancera and others added 3 commits May 21, 2025 20:53
All these tunnels use the IFLA_IPTUN_* netlink API. Therefore, both
IFLAN_INFO_KIND "ipip" and "ip6tnl" data is serialized using the
IpTunnel struct.

Unit tests added.

Signed-off-by: Fernando Fernandez Mancera <[email protected]>
According to the IANA protocol number specification
(https://www.iana.org/assignments/protocol-numbers/protocol-numbers.xhtml),
protocol numbers are always within the u8 range.

Using `as` for type conversion is dangerous because it can silently
discard higher-order data, leading to bugs that are hard to detect.

This change ensures all protocol number conversions are type-safe and
explicit, avoiding accidental data loss and aligning the code with the
protocol specification.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants