netfilter: conntrack: handle builtin l4proto packet functions via direct calls
authorFlorian Westphal <fw@strlen.de>
Tue, 15 Jan 2019 21:03:32 +0000 (22:03 +0100)
committerPablo Neira Ayuso <pablo@netfilter.org>
Fri, 18 Jan 2019 14:02:33 +0000 (15:02 +0100)
The l4 protocol trackers are invoked via indirect call: l4proto->packet().

With one exception (gre), all l4trackers are builtin, so we can make
.packet optional and use a direct call for most protocols.

Signed-off-by: Florian Westphal <fw@strlen.de>
Signed-off-by: Pablo Neira Ayuso <pablo@netfilter.org>
include/net/netfilter/nf_conntrack_l4proto.h
net/netfilter/nf_conntrack_core.c
net/netfilter/nf_conntrack_proto_dccp.c
net/netfilter/nf_conntrack_proto_icmp.c
net/netfilter/nf_conntrack_proto_icmpv6.c
net/netfilter/nf_conntrack_proto_sctp.c
net/netfilter/nf_conntrack_proto_tcp.c
net/netfilter/nf_conntrack_proto_udp.c

index ae7b86f587f2c77c5e2e05972d67b070a39b8711..6ca4355ae3dc0cef8d07220dabea3dbd01dcd2a6 100644 (file)
@@ -99,6 +99,43 @@ int nf_conntrack_icmpv6_error(struct nf_conn *tmpl,
                              struct sk_buff *skb,
                              unsigned int dataoff,
                              const struct nf_hook_state *state);
+
+int nf_conntrack_icmp_packet(struct nf_conn *ct,
+                            struct sk_buff *skb,
+                            enum ip_conntrack_info ctinfo,
+                            const struct nf_hook_state *state);
+
+int nf_conntrack_icmpv6_packet(struct nf_conn *ct,
+                              struct sk_buff *skb,
+                              enum ip_conntrack_info ctinfo,
+                              const struct nf_hook_state *state);
+
+int nf_conntrack_udp_packet(struct nf_conn *ct,
+                           struct sk_buff *skb,
+                           unsigned int dataoff,
+                           enum ip_conntrack_info ctinfo,
+                           const struct nf_hook_state *state);
+int nf_conntrack_udplite_packet(struct nf_conn *ct,
+                               struct sk_buff *skb,
+                               unsigned int dataoff,
+                               enum ip_conntrack_info ctinfo,
+                               const struct nf_hook_state *state);
+int nf_conntrack_tcp_packet(struct nf_conn *ct,
+                           struct sk_buff *skb,
+                           unsigned int dataoff,
+                           enum ip_conntrack_info ctinfo,
+                           const struct nf_hook_state *state);
+int nf_conntrack_dccp_packet(struct nf_conn *ct,
+                            struct sk_buff *skb,
+                            unsigned int dataoff,
+                            enum ip_conntrack_info ctinfo,
+                            const struct nf_hook_state *state);
+int nf_conntrack_sctp_packet(struct nf_conn *ct,
+                            struct sk_buff *skb,
+                            unsigned int dataoff,
+                            enum ip_conntrack_info ctinfo,
+                            const struct nf_hook_state *state);
+
 /* Existing built-in generic protocol */
 extern const struct nf_conntrack_l4proto nf_conntrack_l4proto_generic;
 
index 741b533148baedd70cd4184ba47bb507b055c142..5dfd95ae510d34423bd378fe94420864018591b0 100644 (file)
@@ -1522,6 +1522,45 @@ nf_conntrack_handle_icmp(struct nf_conn *tmpl,
        return ret;
 }
 
+/* Returns verdict for packet, or -1 for invalid. */
+static int nf_conntrack_handle_packet(struct nf_conn *ct,
+                                     struct sk_buff *skb,
+                                     unsigned int dataoff,
+                                     enum ip_conntrack_info ctinfo,
+                                     const struct nf_hook_state *state)
+{
+       switch (nf_ct_protonum(ct)) {
+       case IPPROTO_TCP:
+               return nf_conntrack_tcp_packet(ct, skb, dataoff,
+                                              ctinfo, state);
+       case IPPROTO_UDP:
+               return nf_conntrack_udp_packet(ct, skb, dataoff,
+                                              ctinfo, state);
+       case IPPROTO_ICMP:
+               return nf_conntrack_icmp_packet(ct, skb, ctinfo, state);
+       case IPPROTO_ICMPV6:
+               return nf_conntrack_icmpv6_packet(ct, skb, ctinfo, state);
+#ifdef CONFIG_NF_CT_PROTO_UDPLITE
+       case IPPROTO_UDPLITE:
+               return nf_conntrack_udplite_packet(ct, skb, dataoff,
+                                                  ctinfo, state);
+#endif
+#ifdef CONFIG_NF_CT_PROTO_SCTP
+       case IPPROTO_SCTP:
+               return nf_conntrack_sctp_packet(ct, skb, dataoff,
+                                               ctinfo, state);
+#endif
+#ifdef CONFIG_NF_CT_PROTO_DCCP
+       case IPPROTO_DCCP:
+               return nf_conntrack_dccp_packet(ct, skb, dataoff,
+                                               ctinfo, state);
+#endif
+       }
+
+       WARN_ON_ONCE(1);
+       return -NF_ACCEPT;
+}
+
 unsigned int
 nf_conntrack_in(struct sk_buff *skb, const struct nf_hook_state *state)
 {
@@ -1583,7 +1622,11 @@ repeat:
                goto out;
        }
 
-       ret = l4proto->packet(ct, skb, dataoff, ctinfo, state);
+       if (l4proto->packet)
+               ret = l4proto->packet(ct, skb, dataoff, ctinfo, state);
+       else
+               ret = nf_conntrack_handle_packet(ct, skb, dataoff, ctinfo, state);
+
        if (ret <= 0) {
                /* Invalid: inverse of the return code tells
                 * the netfilter core what to do */
index 023c1445bc3960de8c3d2350d9fb5c8d743e920f..84408ec806244d05dadc54ddad4f901435a2bda9 100644 (file)
@@ -472,9 +472,10 @@ out_invalid:
        return true;
 }
 
-static int dccp_packet(struct nf_conn *ct, struct sk_buff *skb,
-                      unsigned int dataoff, enum ip_conntrack_info ctinfo,
-                      const struct nf_hook_state *state)
+int nf_conntrack_dccp_packet(struct nf_conn *ct, struct sk_buff *skb,
+                            unsigned int dataoff,
+                            enum ip_conntrack_info ctinfo,
+                            const struct nf_hook_state *state)
 {
        enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
        struct dccp_hdr _dh, *dh;
@@ -839,7 +840,6 @@ static struct nf_proto_net *dccp_get_net_proto(struct net *net)
 
 const struct nf_conntrack_l4proto nf_conntrack_l4proto_dccp = {
        .l4proto                = IPPROTO_DCCP,
-       .packet                 = dccp_packet,
        .can_early_drop         = dccp_can_early_drop,
 #ifdef CONFIG_NF_CONNTRACK_PROCFS
        .print_conntrack        = dccp_print_conntrack,
index de64d8a5fdfd137aca48a9e62a143a01f63bec07..381a7f3ea68a932450961643580d083c1973b39c 100644 (file)
@@ -68,11 +68,10 @@ static bool icmp_invert_tuple(struct nf_conntrack_tuple *tuple,
 }
 
 /* Returns verdict for packet, or -1 for invalid. */
-static int icmp_packet(struct nf_conn *ct,
-                      struct sk_buff *skb,
-                      unsigned int dataoff,
-                      enum ip_conntrack_info ctinfo,
-                      const struct nf_hook_state *state)
+int nf_conntrack_icmp_packet(struct nf_conn *ct,
+                            struct sk_buff *skb,
+                            enum ip_conntrack_info ctinfo,
+                            const struct nf_hook_state *state)
 {
        /* Do not immediately delete the connection after the first
           successful reply to avoid excessive conntrackd traffic
@@ -350,9 +349,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmp =
        .l4proto                = IPPROTO_ICMP,
        .pkt_to_tuple           = icmp_pkt_to_tuple,
        .invert_tuple           = icmp_invert_tuple,
-       .packet                 = icmp_packet,
-       .destroy                = NULL,
-       .me                     = NULL,
 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
        .tuple_to_nlattr        = icmp_tuple_to_nlattr,
        .nlattr_tuple_size      = icmp_nlattr_tuple_size,
index a15eefb8e3173c5d89268bd7f2a6c076ff787b1c..93f27d0bcb7558b1805ccf631448d444ca2b5308 100644 (file)
@@ -86,11 +86,10 @@ static unsigned int *icmpv6_get_timeouts(struct net *net)
 }
 
 /* Returns verdict for packet, or -1 for invalid. */
-static int icmpv6_packet(struct nf_conn *ct,
-                        struct sk_buff *skb,
-                        unsigned int dataoff,
-                        enum ip_conntrack_info ctinfo,
-                        const struct nf_hook_state *state)
+int nf_conntrack_icmpv6_packet(struct nf_conn *ct,
+                              struct sk_buff *skb,
+                              enum ip_conntrack_info ctinfo,
+                              const struct nf_hook_state *state)
 {
        unsigned int *timeout = nf_ct_timeout_lookup(ct);
        static const u8 valid_new[] = {
@@ -361,7 +360,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_icmpv6 =
        .l4proto                = IPPROTO_ICMPV6,
        .pkt_to_tuple           = icmpv6_pkt_to_tuple,
        .invert_tuple           = icmpv6_invert_tuple,
-       .packet                 = icmpv6_packet,
 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
        .tuple_to_nlattr        = icmpv6_tuple_to_nlattr,
        .nlattr_tuple_size      = icmpv6_nlattr_tuple_size,
index d53e3e78f6052a1f8d8fde973ee03b0763470b30..952f2cc0fd74a3cd93440df09cb1760b1a874421 100644 (file)
@@ -357,11 +357,11 @@ out_invalid:
 }
 
 /* Returns verdict for packet, or -NF_ACCEPT for invalid. */
-static int sctp_packet(struct nf_conn *ct,
-                      struct sk_buff *skb,
-                      unsigned int dataoff,
-                      enum ip_conntrack_info ctinfo,
-                      const struct nf_hook_state *state)
+int nf_conntrack_sctp_packet(struct nf_conn *ct,
+                            struct sk_buff *skb,
+                            unsigned int dataoff,
+                            enum ip_conntrack_info ctinfo,
+                            const struct nf_hook_state *state)
 {
        enum sctp_conntrack new_state, old_state;
        enum ip_conntrack_dir dir = CTINFO2DIR(ctinfo);
@@ -759,9 +759,7 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_sctp = {
 #ifdef CONFIG_NF_CONNTRACK_PROCFS
        .print_conntrack        = sctp_print_conntrack,
 #endif
-       .packet                 = sctp_packet,
        .can_early_drop         = sctp_can_early_drop,
-       .me                     = THIS_MODULE,
 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
        .nlattr_size            = SCTP_NLATTR_SIZE,
        .to_nlattr              = sctp_to_nlattr,
index 4dcbd51a8e97f04ad8056374ed892887d2f0798e..32aac9a835d486fd0a86be17d8be3b35cc779659 100644 (file)
@@ -829,11 +829,11 @@ static noinline bool tcp_new(struct nf_conn *ct, const struct sk_buff *skb,
 }
 
 /* Returns verdict for packet, or -1 for invalid. */
-static int tcp_packet(struct nf_conn *ct,
-                     struct sk_buff *skb,
-                     unsigned int dataoff,
-                     enum ip_conntrack_info ctinfo,
-                     const struct nf_hook_state *state)
+int nf_conntrack_tcp_packet(struct nf_conn *ct,
+                           struct sk_buff *skb,
+                           unsigned int dataoff,
+                           enum ip_conntrack_info ctinfo,
+                           const struct nf_hook_state *state)
 {
        struct net *net = nf_ct_net(ct);
        struct nf_tcp_net *tn = nf_tcp_pernet(net);
@@ -1535,7 +1535,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_tcp =
 #ifdef CONFIG_NF_CONNTRACK_PROCFS
        .print_conntrack        = tcp_print_conntrack,
 #endif
-       .packet                 = tcp_packet,
        .can_early_drop         = tcp_can_early_drop,
 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
        .to_nlattr              = tcp_to_nlattr,
index b4f5d5e82031cb6a3d5c82735930a92ba2ddcb32..e1f409aa0006dc31273064f6eea0f5058ab85d03 100644 (file)
@@ -85,11 +85,11 @@ static bool udp_error(struct sk_buff *skb,
 }
 
 /* Returns verdict for packet, and may modify conntracktype */
-static int udp_packet(struct nf_conn *ct,
-                     struct sk_buff *skb,
-                     unsigned int dataoff,
-                     enum ip_conntrack_info ctinfo,
-                     const struct nf_hook_state *state)
+int nf_conntrack_udp_packet(struct nf_conn *ct,
+                           struct sk_buff *skb,
+                           unsigned int dataoff,
+                           enum ip_conntrack_info ctinfo,
+                           const struct nf_hook_state *state)
 {
        unsigned int *timeouts;
 
@@ -177,11 +177,11 @@ static bool udplite_error(struct sk_buff *skb,
 }
 
 /* Returns verdict for packet, and may modify conntracktype */
-static int udplite_packet(struct nf_conn *ct,
-                         struct sk_buff *skb,
-                         unsigned int dataoff,
-                         enum ip_conntrack_info ctinfo,
-                         const struct nf_hook_state *state)
+int nf_conntrack_udplite_packet(struct nf_conn *ct,
+                               struct sk_buff *skb,
+                               unsigned int dataoff,
+                               enum ip_conntrack_info ctinfo,
+                               const struct nf_hook_state *state)
 {
        unsigned int *timeouts;
 
@@ -319,7 +319,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_udp =
 {
        .l4proto                = IPPROTO_UDP,
        .allow_clash            = true,
-       .packet                 = udp_packet,
 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
        .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
        .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,
@@ -344,7 +343,6 @@ const struct nf_conntrack_l4proto nf_conntrack_l4proto_udplite =
 {
        .l4proto                = IPPROTO_UDPLITE,
        .allow_clash            = true,
-       .packet                 = udplite_packet,
 #if IS_ENABLED(CONFIG_NF_CT_NETLINK)
        .tuple_to_nlattr        = nf_ct_port_tuple_to_nlattr,
        .nlattr_to_tuple        = nf_ct_port_nlattr_to_tuple,