This patch is to register a callback function tunnel6_rcv_cb with
is_ipip set in a xfrm_input_afinfo object for tunnel6 and tunnel46.

It will be called by xfrm_rcv_cb() from xfrm_input() when family
is AF_INET6 and proto is IPPROTO_IPIP or IPPROTO_IPV6.

v1->v2:
  - Fix a sparse warning caused by the missing "__rcu", as Jakub
    noticed.
  - Handle the err returned by xfrm_input_register_afinfo() in
    tunnel6_init/fini(), as Sabrina noticed.
v2->v3:
  - Add "#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)" to fix the build error
    when xfrm is disabled, reported by kbuild test robot

Signed-off-by: Xin Long <lucien....@gmail.com>
---
 include/net/xfrm.h |  1 +
 net/ipv6/tunnel6.c | 41 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

diff --git a/include/net/xfrm.h b/include/net/xfrm.h
index c1ec629..83a532d 100644
--- a/include/net/xfrm.h
+++ b/include/net/xfrm.h
@@ -1425,6 +1425,7 @@ struct xfrm_tunnel {
 
 struct xfrm6_tunnel {
        int (*handler)(struct sk_buff *skb);
+       int (*cb_handler)(struct sk_buff *skb, int err);
        int (*err_handler)(struct sk_buff *skb, struct inet6_skb_parm *opt,
                           u8 type, u8 code, int offset, __be32 info);
        struct xfrm6_tunnel __rcu *next;
diff --git a/net/ipv6/tunnel6.c b/net/ipv6/tunnel6.c
index 06c02eb..00e8d8b 100644
--- a/net/ipv6/tunnel6.c
+++ b/net/ipv6/tunnel6.c
@@ -155,6 +155,33 @@ static int tunnel6_rcv(struct sk_buff *skb)
        return 0;
 }
 
+#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
+static int tunnel6_rcv_cb(struct sk_buff *skb, u8 proto, int err)
+{
+       struct xfrm6_tunnel __rcu *head;
+       struct xfrm6_tunnel *handler;
+       int ret;
+
+       head = (proto == IPPROTO_IPV6) ? tunnel6_handlers : tunnel46_handlers;
+
+       for_each_tunnel_rcu(head, handler) {
+               if (handler->cb_handler) {
+                       ret = handler->cb_handler(skb, err);
+                       if (ret <= 0)
+                               return ret;
+               }
+       }
+
+       return 0;
+}
+
+static const struct xfrm_input_afinfo tunnel6_input_afinfo = {
+       .family         =       AF_INET6,
+       .is_ipip        =       true,
+       .callback       =       tunnel6_rcv_cb,
+};
+#endif
+
 static int tunnel46_rcv(struct sk_buff *skb)
 {
        struct xfrm6_tunnel *handler;
@@ -245,11 +272,25 @@ static int __init tunnel6_init(void)
                inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP);
                return -EAGAIN;
        }
+#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
+       if (xfrm_input_register_afinfo(&tunnel6_input_afinfo)) {
+               pr_err("%s: can't add input afinfo\n", __func__);
+               inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6);
+               inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP);
+               if (xfrm6_tunnel_mpls_supported())
+                       inet6_del_protocol(&tunnelmpls6_protocol, IPPROTO_MPLS);
+               return -EAGAIN;
+       }
+#endif
        return 0;
 }
 
 static void __exit tunnel6_fini(void)
 {
+#if IS_ENABLED(CONFIG_INET6_XFRM_TUNNEL)
+       if (xfrm_input_unregister_afinfo(&tunnel6_input_afinfo))
+               pr_err("%s: can't remove input afinfo\n", __func__);
+#endif
        if (inet6_del_protocol(&tunnel46_protocol, IPPROTO_IPIP))
                pr_err("%s: can't remove protocol\n", __func__);
        if (inet6_del_protocol(&tunnel6_protocol, IPPROTO_IPV6))
-- 
2.1.0

Reply via email to