xfrm: respect ip proto rules criteria in xfrm dst lookups
Subsystems (3)
v2 cover xfrm: respect ip proto rules criteria in xfrm dst lookups
This series fixes the route lookup when done for xfrm to regard L4 criteria specified in ip rules.
The first patch is a minor refactor to allow passing more parameters to dst lookup functions.
The second patch actually passes L4 information to these lookup functions.
---
v2: fix first patch based on reviews from Steffen Klassert and
Simon Horman
Eyal Birger (2):
xfrm: extract dst lookup parameters into a struct
xfrm: respect ip protocols rules criteria when performing dst lookups
include/net/xfrm.h | 28 ++++++++++++----------- net/ipv4/xfrm4_policy.c | 40 +++++++++++++++------------------ net/ipv6/xfrm6_policy.c | 31 +++++++++++++------------- net/xfrm/xfrm_device.c | 11 ++++++--- net/xfrm/xfrm_policy.c | 49 +++++++++++++++++++++++++++++++---------- 5 files changed, 94 insertions(+), 65 deletions(-)
Signature · 1 line
2.34.1
PATCH v2 1/2 xfrm: extract dst lookup parameters into a struct
Preparation for adding more fields to dst lookup functions without changing their signatures.
----
v2:
- rebase over ipsec tree
- fix daddr assignment in xfrm_device.c as noted by Simon Horman
--- include/net/xfrm.h | 26 +++++++++++++------------- net/ipv4/xfrm4_policy.c | 38 ++++++++++++++++---------------------- net/ipv6/xfrm6_policy.c | 28 +++++++++++++--------------- net/xfrm/xfrm_device.c | 11 ++++++++--- net/xfrm/xfrm_policy.c | 35 +++++++++++++++++++++++------------ 5 files changed, 73 insertions(+), 65 deletions(-)
include/net/xfrm.h +13 -13
@@ -349,20 +349,23 @@ struct xfrm_if_cb {void xfrm_if_register_cb (const struct xfrm_if_cb *ifcb);void xfrm_if_unregister_cb (void ); +struct xfrm_dst_lookup_params { +struct net *net; +int tos; +int oif; +xfrm_address_t *saddr; +xfrm_address_t *daddr; +u32 mark; +}; +struct net_device ;struct xfrm_type ;struct xfrm_dst ;struct xfrm_policy_afinfo {struct dst_ops *dst_ops; -struct dst_entry *(*dst_lookup)(struct net *net, -int tos,int oif, -const xfrm_address_t *saddr, -const xfrm_address_t *daddr, -u32 mark); -int (*get_saddr)(struct net *net,int oif, -xfrm_address_t *saddr, -xfrm_address_t *daddr, -u32 mark); +struct dst_entry *(*dst_lookup)(const struct xfrm_dst_lookup_params *params); +int (*get_saddr)(xfrm_address_t *saddr, +const struct xfrm_dst_lookup_params *params);int (*fill_dst)(struct xfrm_dst *xdst,struct net_device *dev,const struct flowi *fl);
@@ -1735,10 +1738,7 @@ static inline int xfrm_user_policy(struct sock *sk, int optname, }#endif -struct dst_entry *__xfrm_dst_lookup (struct net *net,int tos,int oif, -const xfrm_address_t *saddr, -const xfrm_address_t *daddr, -int family,u32 mark); +struct dst_entry *__xfrm_dst_lookup (int family,const struct xfrm_dst_lookup_params *params);struct xfrm_policy *xfrm_policy_alloc (struct net *net,gfp_t gfp);
net/ipv4/xfrm4_policy.c +16 -22
@@ -17,47 +17,41 @@#include <net/ip.h> #include <net/l3mdev.h> -static struct dst_entry *__xfrm4_dst_lookup (struct net *net,struct flowi4 *fl4, -int tos,int oif, -const xfrm_address_t *saddr, -const xfrm_address_t *daddr, -u32 mark) +static struct dst_entry *__xfrm4_dst_lookup (struct flowi4 *fl4, +const struct xfrm_dst_lookup_params *params) {struct rtable *rt;memset (fl4,0 ,sizeof (*fl4)); - fl4->daddr = daddr->a4; - fl4->flowi4_tos = tos; - fl4->flowi4_l3mdev =l3mdev_master_ifindex_by_index (net, oif); - fl4->flowi4_mark = mark; -if (saddr) - fl4->saddr = saddr->a4; - - rt =__ip_route_output_key (net, fl4); + fl4->daddr = params->daddr->a4; + fl4->flowi4_tos = params->tos; + fl4->flowi4_l3mdev =l3mdev_master_ifindex_by_index (params->net, + params->oif); + fl4->flowi4_mark = params->mark; +if (params->saddr) + fl4->saddr = params->saddr->a4; + + rt =__ip_route_output_key (params->net, fl4);if (!IS_ERR (rt))return &rt->dst;return ERR_CAST (rt); } -static struct dst_entry *xfrm4_dst_lookup (struct net *net,int tos,int oif, -const xfrm_address_t *saddr, -const xfrm_address_t *daddr, -u32 mark) +static struct dst_entry *xfrm4_dst_lookup (const struct xfrm_dst_lookup_params *params) {struct flowi4 fl4; -return __xfrm4_dst_lookup (net, &fl4, tos, oif, saddr, daddr, mark); +return __xfrm4_dst_lookup (&fl4, params); } -static int xfrm4_get_saddr (struct net *net,int oif, -xfrm_address_t *saddr,xfrm_address_t *daddr, -u32 mark) +static int xfrm4_get_saddr (xfrm_address_t *saddr, +const struct xfrm_dst_lookup_params *params) {struct dst_entry *dst;struct flowi4 fl4; - dst =__xfrm4_dst_lookup (net, &fl4,0 , oif,NULL , daddr, mark); + dst =__xfrm4_dst_lookup (&fl4, params);if (IS_ERR (dst))return -EHOSTUNREACH ;
net/ipv6/xfrm6_policy.c +13 -15
@@ -23,23 +23,21 @@#include <net/ip6_route.h> #include <net/l3mdev.h> -static struct dst_entry *xfrm6_dst_lookup (struct net *net,int tos,int oif, -const xfrm_address_t *saddr, -const xfrm_address_t *daddr, -u32 mark) +static struct dst_entry *xfrm6_dst_lookup (const struct xfrm_dst_lookup_params *params) {struct flowi6 fl6;struct dst_entry *dst;int err;memset (&fl6,0 ,sizeof (fl6)); - fl6.flowi6_l3mdev =l3mdev_master_ifindex_by_index (net, oif); - fl6.flowi6_mark = mark; -memcpy (&fl6.daddr, daddr,sizeof (fl6.daddr)); -if (saddr) -memcpy (&fl6.saddr, saddr,sizeof (fl6.saddr)); + fl6.flowi6_l3mdev =l3mdev_master_ifindex_by_index (params->net, + params->oif); + fl6.flowi6_mark = params->mark; +memcpy (&fl6.daddr, params->daddr,sizeof (fl6.daddr)); +if (params->saddr) +memcpy (&fl6.saddr, params->saddr,sizeof (fl6.saddr)); - dst =ip6_route_output (net,NULL , &fl6); + dst =ip6_route_output (params->net,NULL , &fl6); err = dst->error;if (dst->error) {
@@ -50,15 +48,14 @@ static struct dst_entry *xfrm6_dst_lookup(struct net *net, int tos, int oif,return dst; } -static int xfrm6_get_saddr (struct net *net,int oif, -xfrm_address_t *saddr,xfrm_address_t *daddr, -u32 mark) +static int xfrm6_get_saddr (xfrm_address_t *saddr, +const struct xfrm_dst_lookup_params *params) {struct dst_entry *dst;struct net_device *dev;struct inet6_dev *idev; - dst =xfrm6_dst_lookup (net,0 , oif,NULL , daddr, mark); + dst =xfrm6_dst_lookup (params);if (IS_ERR (dst))return -EHOSTUNREACH ;
@@ -68,7 +65,8 @@ static int xfrm6_get_saddr(struct net *net, int oif,return -EHOSTUNREACH ; } dev = idev->dev; -ipv6_dev_get_saddr (dev_net (dev), dev, &daddr->in6,0 , &saddr->in6); +ipv6_dev_get_saddr (dev_net (dev), dev, ¶ms->daddr->in6,0 , + &saddr->in6);dst_release (dst);return 0 ; }
net/xfrm/xfrm_device.c +8 -3
@@ -269,6 +269,8 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, dev =dev_get_by_index (net, xuo->ifindex);if (!dev) { +struct xfrm_dst_lookup_params params; +if (!(xuo->flags &XFRM_OFFLOAD_INBOUND )) { saddr = &x->props.saddr; daddr = &x->id.daddr;
@@ -277,9 +279,12 @@ int xfrm_dev_state_add(struct net *net, struct xfrm_state *x, daddr = &x->props.saddr; } - dst =__xfrm_dst_lookup (net,0 ,0 , saddr, daddr, - x->props.family, -xfrm_smark_get (0 , x)); +memset (¶ms,0 ,sizeof (params)); + params.net = net; + params.saddr = saddr; + params.daddr = daddr; + params.mark =xfrm_smark_get (0 , x); + dst =__xfrm_dst_lookup (x->props.family, ¶ms);if (IS_ERR (dst))return (is_packet_offload) ? -EINVAL :0 ;
net/xfrm/xfrm_policy.c +23 -12
@@ -267,10 +267,8 @@ static const struct xfrm_if_cb *xfrm_if_get_cb(void)return rcu_dereference (xfrm_if_cb); } -struct dst_entry *__xfrm_dst_lookup (struct net *net,int tos,int oif, -const xfrm_address_t *saddr, -const xfrm_address_t *daddr, -int family,u32 mark) +struct dst_entry *__xfrm_dst_lookup (int family, +const struct xfrm_dst_lookup_params *params) {const struct xfrm_policy_afinfo *afinfo;struct dst_entry *dst;
@@ -279,7 +277,7 @@ struct dst_entry *__xfrm_dst_lookup(struct net *net, int tos, int oif,if (unlikely (afinfo ==NULL ))return ERR_PTR (-EAFNOSUPPORT ); - dst = afinfo->dst_lookup (net, tos, oif, saddr, daddr, mark); + dst = afinfo->dst_lookup (params);rcu_read_unlock ();
@@ -293,6 +291,7 @@ static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,xfrm_address_t *prev_daddr, int family, u32 mark) { + struct xfrm_dst_lookup_params params;struct net *net =xs_net (x);xfrm_address_t *saddr = &x->props.saddr;xfrm_address_t *daddr = &x->id.daddr;
@@ -307,7 +306,14 @@ static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, daddr = x->coaddr; } - dst =__xfrm_dst_lookup (net, tos, oif, saddr, daddr, family, mark); + params.net = net; + params.saddr = saddr; + params.daddr = daddr; + params.tos = tos; + params.oif = oif; + params.mark = mark; + + dst =__xfrm_dst_lookup (family, ¶ms);if (!IS_ERR (dst)) {if (prev_saddr != saddr)
@@ -2440,15 +2446,15 @@ int __xfrm_sk_clone_policy(struct sock *sk, const struct sock *osk) }static int -xfrm_get_saddr (struct net *net,int oif,xfrm_address_t *local, -xfrm_address_t *remote,unsigned short family,u32 mark) +xfrm_get_saddr (unsigned short family,xfrm_address_t *saddr, +const struct xfrm_dst_lookup_params *params) {int err;const struct xfrm_policy_afinfo *afinfo =xfrm_policy_get_afinfo (family);if (unlikely (afinfo ==NULL ))return -EINVAL ; - err = afinfo->get_saddr (net, oif, local, remote, mark); + err = afinfo->get_saddr (saddr, params);rcu_read_unlock ();return err; }
@@ -2477,9 +2483,14 @@ xfrm_tmpl_resolve_one(struct xfrm_policy *policy, const struct flowi *fl, remote = &tmpl->id.daddr; local = &tmpl->saddr;if (xfrm_addr_any (local, tmpl->encap_family)) { - error =xfrm_get_saddr (net, fl->flowi_oif, - &tmp, remote, - tmpl->encap_family,0 ); +struct xfrm_dst_lookup_params params; + +memset (¶ms,0 ,sizeof (params)); + params.net = net; + params.oif = fl->flowi_oif; + params.daddr = remote; + error =xfrm_get_saddr (tmpl->encap_family, &tmp, + ¶ms);if (error) goto fail; local = &tmp;
Signature · 1 line
2.34.1
PATCH v2 2/2 xfrm: respect ip protocols rules criteria when performing dst lookups
In reply to Eyal BirgerThe series in the "fixes" tag added the ability to consider L4 attributes in routing rules.
The dst lookup in the xfrm code was not adapted to this change, thus routing behavior that relies on L4 information is not respected, which is relevant for UDP encapsulated IPsec traffic.
Pass the ip protocol information when performing dst lookups.
--- include/net/xfrm.h | 2 ++ net/ipv4/xfrm4_policy.c | 2 ++ net/ipv6/xfrm6_policy.c | 3 +++ net/xfrm/xfrm_policy.c | 14 ++++++++++++++ 4 files changed, 21 insertions(+)
include/net/xfrm.h +2 -0
@@ -356,6 +356,8 @@ struct xfrm_dst_lookup_params {xfrm_address_t *saddr;xfrm_address_t *daddr;u32 mark; +__u8 ipproto; +union flowi_uli uli; };struct net_device ;
net/ipv4/xfrm4_policy.c +2 -0
@@ -30,6 +30,8 @@ static struct dst_entry *__xfrm4_dst_lookup(struct flowi4 *fl4, fl4->flowi4_mark = params->mark;if (params->saddr) fl4->saddr = params->saddr->a4; + fl4->flowi4_proto = params->ipproto; + fl4->uli = params->uli; rt =__ip_route_output_key (params->net, fl4);if (!IS_ERR (rt))
net/ipv6/xfrm6_policy.c +3 -0
@@ -37,6 +37,9 @@ static struct dst_entry *xfrm6_dst_lookup(const struct xfrm_dst_lookup_params *pif (params->saddr)memcpy (&fl6.saddr, params->saddr,sizeof (fl6.saddr)); + fl6.flowi4_proto = params->ipproto; + fl6.uli = params->uli; + dst =ip6_route_output (params->net,NULL , &fl6); err = dst->error;
net/xfrm/xfrm_policy.c +14 -0
@@ -312,6 +312,20 @@ static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, params.tos = tos; params.oif = oif; params.mark = mark; +if (x->encap) { +switch (x->encap->encap_type) { +case UDP_ENCAP_ESPINUDP : + params.ipproto =IPPROTO_UDP ; + params.uli.ports.sport = x->encap->encap_sport; + params.uli.ports.dport = x->encap->encap_dport; +break ; +case TCP_ENCAP_ESPINTCP : + params.ipproto =IPPROTO_TCP ; + params.uli.ports.sport = x->encap->encap_sport; + params.uli.ports.dport = x->encap->encap_dport; +break ; + } + } dst =__xfrm_dst_lookup (family, ¶ms);
Signature · 1 line
2.34.1
Quoted 2 lines · Eyal Birger via Devel
This series fixes the route lookup when done for xfrm to regard L4 criteria specified in ip rules.
Hi Eyal,
This isn't a review of the patch set, instead curiosity about use cases. This sounds interesting. Would you like to elaborate on the use cases supported in this patch? From what I understand so far, it seems related to 'ip rule', but I'm wondering about possible use cases: inner packet routing rule of tunnel? May be you could explain it at the IPsec coffee hour or share some use case or test script.
Is this only for route based IPsec, i.e. with xfrmi interface, or also for a policy based without route use cases. In the later case there were discussions why do we need a route for the inner packet.
-antony
Quoted 30 lines
The first patch is a minor refactor to allow passing more parameters to dst lookup functions.
The second patch actually passes L4 information to these lookup functions.Signed-off-by Eyal Birger <[email protected]>---
v2: fix first patch based on reviews from Steffen Klassert and
Simon HormanEyal Birger (2):
xfrm: extract dst lookup parameters into a struct
xfrm: respect ip protocols rules criteria when performing dst lookupsinclude/net/xfrm.h | 28 ++++++++++++----------- net/ipv4/xfrm4_policy.c | 40 +++++++++++++++------------------ net/ipv6/xfrm6_policy.c | 31 +++++++++++++------------- net/xfrm/xfrm_device.c | 11 ++++++--- net/xfrm/xfrm_policy.c | 49 +++++++++++++++++++++++++++++++---------- 5 files changed, 94 insertions(+), 65 deletions(-)Signature · 6 lines
2.34.1 -- Devel mailing list [email protected] https://linux-ipsec.org/mailman/listinfo/devel
Quoted 2 lines · Eyal Birger via Devel
This series fixes the route lookup when done for xfrm to regard L4 criteria specified in ip rules.
Thanks Eyal for explaining the purpose of this series on the call. How about something like this for the beginning of the commit message:
'This series fixes the route lookup for the outer packet after encapsulation, including the L4 criteria specified in IP rules.'
It's just a cosmetic suggestion, so may be improve it if you're planning to send a new version of the patch series for other reasons.
We ran into this issue before and used workaround, mark instead of L4 in the "ip rule" for the outer packet.
Quoted 5 lines
The first patch is a minor refactor to allow passing more parameters to dst lookup functions.
The second patch actually passes L4 information to these lookup functions.Signed-off-by Eyal Birger <[email protected]>
And I have a further suggestion to improve this fix make it more generic.
I was doing the following rule as a work around for ESP-in-UDP tunnels. ip rule add from all to 192.1.2.23 fwmark 0x1 lookup 50
With your fix I can change it to a L4 rule when using ESP-in-UDP ip rule add from 192.1.2.45 to 192.1.2.23 ipproto udp dport 4500 lookup 50
However, when not using ESP, without UDP, and rule with "ipproto esp" does work.
ip rule add from 192.1.2.45 to 192.1.2.23 ipproto esp lookup 50
So, I have come up with a fix/hack on top of your fix.
@@ -327,6 +327,8 @@ static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x,
+ } else {
+ params.ipproto = IPPROTO_ESP;
With this fix "ipproto esp" rules also works.
see the attached full patch.
regards,
-antony
From 54cfdfaab12270784623c60d91baf499765e50f5 Mon Sep 17 00:00:00 2001
From: Antony Antony <[email protected]>
Date: Mon, 2 Sep 2024 22:08:15 +0200
Subject: [PATCH ipsec] xfrm: use IPPROTO_ESP for route lookup without encapsulation
When there is no UDP or TCP encapsulation, use IPPROTO_ESP for route lookup. This ensures that "ip rule" entries like the following match correctly:
ip rule add from 192.1.2.45 to 192.1.2.23 ipproto esp lookup 50
--- net/xfrm/xfrm_policy.c | 2 ++ 1 file changed, 2 insertions(+)
net/xfrm/xfrm_policy.c +2 -0
@@ -327,6 +327,8 @@ static inline struct dst_entry *xfrm_dst_lookup(struct xfrm_state *x, params.uli.ports.dport = x->encap->encap_dport;break ; } + }else { + params.ipproto =IPPROTO_ESP ; } dst =__xfrm_dst_lookup (family, ¶ms);
--
2.43.0