History

netfilter: nf_tables: merge route type into core

very little code, so it really doesn't make sense to have extra modules or even a kconfig knob for this. Merge them and make functionality available unconditionally. The merge makes inet family route support trivial, so add it as well here. Before: text data bss dec hex filename 835 832 0 1667 683 nft_chain_route_ipv4.ko 870 832 0 1702 6a6 nft_chain_route_ipv6.ko 111568 2556 529 114653 1bfdd nf_tables.ko After: text data bss dec hex filename 113133 2556 529 116218 1c5fa nf_tables.ko

Florian Westphal committed 7 years ago

Commit c1deb061 parent d16438512 files · +191 −214Browse files

Unified · Split

12 files
  1. include/linux/netfilter_ipv6.h +15 −0
  2. include/net/netfilter/nf_tables.h +2 −0
  3. net/ipv4/netfilter/Kconfig +0 −8
  4. net/ipv4/netfilter/Makefile +0 −1
  5. net/ipv4/netfilter/nft_chain_route_ipv4.c +0 −89
  6. net/ipv6/netfilter/Kconfig +0 −8
  7. net/ipv6/netfilter/Makefile +0 −1
  8. net/ipv6/netfilter/nft_chain_route_ipv6.c +0 −91
  9. net/netfilter/Makefile +2 −1
  10. net/netfilter/nf_nat_proto.c +1 −15
  11. net/netfilter/nf_tables_api.c +2 −0
  12. net/netfilter/nft_chain_route.c +169 −0
include/linux/netfilter_ipv6.h+15−0
@@ -87,6 +87,21 @@ static inline int nf_ip6_route(struct net *net, struct dst_entry **dst, · 86 unmodified lines
87}87}
88 88
89int ip6_route_me_harder(struct net *net, struct sk_buff *skb);89int ip6_route_me_harder(struct net *net, struct sk_buff *skb);
90
91static inline int nf_ip6_route_me_harder(struct net *net, struct sk_buff *skb)
92{
93#if IS_MODULE(CONFIG_IPV6)
94 const struct nf_ipv6_ops *v6_ops = nf_get_ipv6_ops();
95
96 if (!v6_ops)
97 return -EHOSTUNREACH;
98
99 return v6_ops->route_me_harder(net, skb);
100#else
101 return ip6_route_me_harder(net, skb);
102#endif
103}
104
90__sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,105__sum16 nf_ip6_checksum(struct sk_buff *skb, unsigned int hook,
91 unsigned int dataoff, u_int8_t protocol);106 unsigned int dataoff, u_int8_t protocol);
92 107
include/net/netfilter/nf_tables.h+2−0
@@ -1411,4 +1411,6 @@ struct nft_trans_flowtable { · 1410 unmodified lines
1411int __init nft_chain_filter_init(void);1411int __init nft_chain_filter_init(void);
1412void nft_chain_filter_fini(void);1412void nft_chain_filter_fini(void);
1413 1413
1414void __init nft_chain_route_init(void);
1415void nft_chain_route_fini(void);
1414#endif /* _NET_NF_TABLES_H */1416#endif /* _NET_NF_TABLES_H */
net/ipv4/netfilter/Kconfig+0−8
@@ -27,14 +27,6 @@ config NF_TABLES_IPV4 · 26 unmodified lines
27 27
28if NF_TABLES_IPV428if NF_TABLES_IPV4
29 29
30config NFT_CHAIN_ROUTE_IPV4
31 tristate "IPv4 nf_tables route chain support"
32 help
33 This option enables the "route" chain for IPv4 in nf_tables. This
34 chain type is used to force packet re-routing after mangling header
35 fields such as the source, destination, type of service and
36 the packet mark.
37
38config NFT_REJECT_IPV430config NFT_REJECT_IPV4
39 select NF_REJECT_IPV431 select NF_REJECT_IPV4
40 default NFT_REJECT32 default NFT_REJECT
net/ipv4/netfilter/Makefile+0−1
@@ -24,7 +24,6 @@ nf_nat_snmp_basic-y := nf_nat_snmp_basic.asn1.o nf_nat_snmp_basic_main.o · 23 unmodified lines
24$(obj)/nf_nat_snmp_basic_main.o: $(obj)/nf_nat_snmp_basic.asn1.h24$(obj)/nf_nat_snmp_basic_main.o: $(obj)/nf_nat_snmp_basic.asn1.h
25obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o25obj-$(CONFIG_NF_NAT_SNMP_BASIC) += nf_nat_snmp_basic.o
26 26
27obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV4) += nft_chain_route_ipv4.o
28obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o27obj-$(CONFIG_NFT_REJECT_IPV4) += nft_reject_ipv4.o
29obj-$(CONFIG_NFT_FIB_IPV4) += nft_fib_ipv4.o28obj-$(CONFIG_NFT_FIB_IPV4) += nft_fib_ipv4.o
30obj-$(CONFIG_NFT_DUP_IPV4) += nft_dup_ipv4.o29obj-$(CONFIG_NFT_DUP_IPV4) += nft_dup_ipv4.o
net/ipv4/netfilter/nft_chain_route_ipv4.c+0−89
@@ -1,89 +0,0 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 */
9
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/list.h>
13#include <linux/skbuff.h>
14#include <linux/netlink.h>
15#include <linux/netfilter.h>
16#include <linux/netfilter_ipv4.h>
17#include <linux/netfilter/nfnetlink.h>
18#include <linux/netfilter/nf_tables.h>
19#include <net/netfilter/nf_tables.h>
20#include <net/netfilter/nf_tables_ipv4.h>
21#include <net/route.h>
22#include <net/ip.h>
23
24static unsigned int nf_route_table_hook(void *priv,
25 struct sk_buff *skb,
26 const struct nf_hook_state *state)
27{
28 unsigned int ret;
29 struct nft_pktinfo pkt;
30 u32 mark;
31 __be32 saddr, daddr;
32 u_int8_t tos;
33 const struct iphdr *iph;
34 int err;
35
36 nft_set_pktinfo(&pkt, skb, state);
37 nft_set_pktinfo_ipv4(&pkt, skb);
38
39 mark = skb->mark;
40 iph = ip_hdr(skb);
41 saddr = iph->saddr;
42 daddr = iph->daddr;
43 tos = iph->tos;
44
45 ret = nft_do_chain(&pkt, priv);
46 if (ret != NF_DROP && ret != NF_STOLEN) {
47 iph = ip_hdr(skb);
48
49 if (iph->saddr != saddr ||
50 iph->daddr != daddr ||
51 skb->mark != mark ||
52 iph->tos != tos) {
53 err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
54 if (err < 0)
55 ret = NF_DROP_ERR(err);
56 }
57 }
58 return ret;
59}
60
61static const struct nft_chain_type nft_chain_route_ipv4 = {
62 .name = "route",
63 .type = NFT_CHAIN_T_ROUTE,
64 .family = NFPROTO_IPV4,
65 .owner = THIS_MODULE,
66 .hook_mask = (1 << NF_INET_LOCAL_OUT),
67 .hooks = {
68 [NF_INET_LOCAL_OUT] = nf_route_table_hook,
69 },
70};
71
72static int __init nft_chain_route_init(void)
73{
74 nft_register_chain_type(&nft_chain_route_ipv4);
75
76 return 0;
77}
78
79static void __exit nft_chain_route_exit(void)
80{
81 nft_unregister_chain_type(&nft_chain_route_ipv4);
82}
83
84module_init(nft_chain_route_init);
85module_exit(nft_chain_route_exit);
86
87MODULE_LICENSE("GPL");
88MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
89MODULE_ALIAS_NFT_CHAIN(AF_INET, "route");
net/ipv6/netfilter/Kconfig+0−8
@@ -23,14 +23,6 @@ config NF_TABLES_IPV6 · 22 unmodified lines
23 23
24if NF_TABLES_IPV624if NF_TABLES_IPV6
25 25
26config NFT_CHAIN_ROUTE_IPV6
27 tristate "IPv6 nf_tables route chain support"
28 help
29 This option enables the "route" chain for IPv6 in nf_tables. This
30 chain type is used to force packet re-routing after mangling header
31 fields such as the source, destination, flowlabel, hop-limit and
32 the packet mark.
33
34config NFT_REJECT_IPV626config NFT_REJECT_IPV6
35 select NF_REJECT_IPV627 select NF_REJECT_IPV6
36 default NFT_REJECT28 default NFT_REJECT
net/ipv6/netfilter/Makefile+0−1
@@ -27,7 +27,6 @@ obj-$(CONFIG_NF_REJECT_IPV6) += nf_reject_ipv6.o · 26 unmodified lines
27obj-$(CONFIG_NF_DUP_IPV6) += nf_dup_ipv6.o27obj-$(CONFIG_NF_DUP_IPV6) += nf_dup_ipv6.o
28 28
29# nf_tables29# nf_tables
30obj-$(CONFIG_NFT_CHAIN_ROUTE_IPV6) += nft_chain_route_ipv6.o
31obj-$(CONFIG_NFT_REJECT_IPV6) += nft_reject_ipv6.o30obj-$(CONFIG_NFT_REJECT_IPV6) += nft_reject_ipv6.o
32obj-$(CONFIG_NFT_DUP_IPV6) += nft_dup_ipv6.o31obj-$(CONFIG_NFT_DUP_IPV6) += nft_dup_ipv6.o
33obj-$(CONFIG_NFT_FIB_IPV6) += nft_fib_ipv6.o32obj-$(CONFIG_NFT_FIB_IPV6) += nft_fib_ipv6.o
net/ipv6/netfilter/nft_chain_route_ipv6.c+0−91
@@ -1,91 +0,0 @@
1/*
2 * Copyright (c) 2008 Patrick McHardy <kaber@trash.net>
3 * Copyright (c) 2012 Pablo Neira Ayuso <pablo@netfilter.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Development of this code funded by Astaro AG (http://www.astaro.com/)
10 */
11
12#include <linux/module.h>
13#include <linux/init.h>
14#include <linux/list.h>
15#include <linux/skbuff.h>
16#include <linux/netlink.h>
17#include <linux/netfilter.h>
18#include <linux/netfilter_ipv6.h>
19#include <linux/netfilter/nfnetlink.h>
20#include <linux/netfilter/nf_tables.h>
21#include <net/netfilter/nf_tables.h>
22#include <net/netfilter/nf_tables_ipv6.h>
23#include <net/route.h>
24
25static unsigned int nf_route_table_hook(void *priv,
26 struct sk_buff *skb,
27 const struct nf_hook_state *state)
28{
29 unsigned int ret;
30 struct nft_pktinfo pkt;
31 struct in6_addr saddr, daddr;
32 u_int8_t hop_limit;
33 u32 mark, flowlabel;
34 int err;
35
36 nft_set_pktinfo(&pkt, skb, state);
37 nft_set_pktinfo_ipv6(&pkt, skb);
38
39 /* save source/dest address, mark, hoplimit, flowlabel, priority */
40 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
41 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
42 mark = skb->mark;
43 hop_limit = ipv6_hdr(skb)->hop_limit;
44
45 /* flowlabel and prio (includes version, which shouldn't change either */
46 flowlabel = *((u32 *)ipv6_hdr(skb));
47
48 ret = nft_do_chain(&pkt, priv);
49 if (ret != NF_DROP && ret != NF_STOLEN &&
50 (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) ||
51 memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
52 skb->mark != mark ||
53 ipv6_hdr(skb)->hop_limit != hop_limit ||
54 flowlabel != *((u_int32_t *)ipv6_hdr(skb)))) {
55 err = ip6_route_me_harder(state->net, skb);
56 if (err < 0)
57 ret = NF_DROP_ERR(err);
58 }
59
60 return ret;
61}
62
63static const struct nft_chain_type nft_chain_route_ipv6 = {
64 .name = "route",
65 .type = NFT_CHAIN_T_ROUTE,
66 .family = NFPROTO_IPV6,
67 .owner = THIS_MODULE,
68 .hook_mask = (1 << NF_INET_LOCAL_OUT),
69 .hooks = {
70 [NF_INET_LOCAL_OUT] = nf_route_table_hook,
71 },
72};
73
74static int __init nft_chain_route_init(void)
75{
76 nft_register_chain_type(&nft_chain_route_ipv6);
77
78 return 0;
79}
80
81static void __exit nft_chain_route_exit(void)
82{
83 nft_unregister_chain_type(&nft_chain_route_ipv6);
84}
85
86module_init(nft_chain_route_init);
87module_exit(nft_chain_route_exit);
88
89MODULE_LICENSE("GPL");
90MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>");
91MODULE_ALIAS_NFT_CHAIN(AF_INET6, "route");
net/netfilter/Makefile+2−1
@@ -77,7 +77,8 @@ obj-$(CONFIG_NF_DUP_NETDEV) += nf_dup_netdev.o · 76 unmodified lines
77nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \77nf_tables-objs := nf_tables_core.o nf_tables_api.o nft_chain_filter.o \
78 nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \78 nf_tables_trace.o nft_immediate.o nft_cmp.o nft_range.o \
79 nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.o \79 nft_bitwise.o nft_byteorder.o nft_payload.o nft_lookup.o \
80 nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o80 nft_dynset.o nft_meta.o nft_rt.o nft_exthdr.o \
81 nft_chain_route.o
81 82
82nf_tables_set-objs := nf_tables_set_core.o \83nf_tables_set-objs := nf_tables_set_core.o \
83 nft_set_hash.o nft_set_bitmap.o nft_set_rbtree.o84 nft_set_hash.o nft_set_bitmap.o nft_set_rbtree.o
net/netfilter/nf_nat_proto.c+1−15
@@ -926,20 +926,6 @@ nf_nat_ipv6_out(void *priv, struct sk_buff *skb, · 925 unmodified lines
926 return ret;926 return ret;
927}927}
928 928
929static int nat_route_me_harder(struct net *net, struct sk_buff *skb)
930{
931#ifdef CONFIG_IPV6_MODULE
932 const struct nf_ipv6_ops *v6_ops = nf_get_ipv6_ops();
933
934 if (!v6_ops)
935 return -EHOSTUNREACH;
936
937 return v6_ops->route_me_harder(net, skb);
938#else
939 return ip6_route_me_harder(net, skb);
940#endif
941}
942
943static unsigned int929static unsigned int
944nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb,930nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb,
945 const struct nf_hook_state *state)931 const struct nf_hook_state *state)
@@ -959,7 +945,7 @@ nf_nat_ipv6_local_fn(void *priv, struct sk_buff *skb, · 13 unmodified lines
959 945
960 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,946 if (!nf_inet_addr_cmp(&ct->tuplehash[dir].tuple.dst.u3,
961 &ct->tuplehash[!dir].tuple.src.u3)) {947 &ct->tuplehash[!dir].tuple.src.u3)) {
962 err = nat_route_me_harder(state->net, skb);948 err = nf_ip6_route_me_harder(state->net, skb);
963 if (err < 0)949 if (err < 0)
964 ret = NF_DROP_ERR(err);950 ret = NF_DROP_ERR(err);
965 }951 }
net/netfilter/nf_tables_api.c+2−0
@@ -7530,6 +7530,7 @@ static int __init nf_tables_module_init(void) · 7529 unmodified lines
7530 if (err < 0)7530 if (err < 0)
7531 goto err5;7531 goto err5;
7532 7532
7533 nft_chain_route_init();
7533 return err;7534 return err;
7534err5:7535err5:
7535 rhltable_destroy(&nft_objname_ht);7536 rhltable_destroy(&nft_objname_ht);
@@ -7549,6 +7550,7 @@ static void __exit nf_tables_module_exit(void) · 13 unmodified lines
7549 nfnetlink_subsys_unregister(&nf_tables_subsys);7550 nfnetlink_subsys_unregister(&nf_tables_subsys);
7550 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);7551 unregister_netdevice_notifier(&nf_tables_flowtable_notifier);
7551 nft_chain_filter_fini();7552 nft_chain_filter_fini();
7553 nft_chain_route_fini();
7552 unregister_pernet_subsys(&nf_tables_net_ops);7554 unregister_pernet_subsys(&nf_tables_net_ops);
7553 cancel_work_sync(&trans_destroy_work);7555 cancel_work_sync(&trans_destroy_work);
7554 rcu_barrier();7556 rcu_barrier();
net/netfilter/nft_chain_route.c+169−0
@@ -0,0 +1,169 @@
1// SPDX-License-Identifier: GPL-2.0
2
3#include <linux/skbuff.h>
4#include <linux/netfilter.h>
5#include <linux/netfilter_ipv4.h>
6#include <linux/netfilter_ipv6.h>
7#include <linux/netfilter/nfnetlink.h>
8#include <linux/netfilter/nf_tables.h>
9#include <net/netfilter/nf_tables.h>
10#include <net/netfilter/nf_tables_ipv4.h>
11#include <net/netfilter/nf_tables_ipv6.h>
12#include <net/route.h>
13#include <net/ip.h>
14
15#ifdef CONFIG_NF_TABLES_IPV4
16static unsigned int nf_route_table_hook4(void *priv,
17 struct sk_buff *skb,
18 const struct nf_hook_state *state)
19{
20 const struct iphdr *iph;
21 struct nft_pktinfo pkt;
22 __be32 saddr, daddr;
23 unsigned int ret;
24 u32 mark;
25 int err;
26 u8 tos;
27
28 nft_set_pktinfo(&pkt, skb, state);
29 nft_set_pktinfo_ipv4(&pkt, skb);
30
31 mark = skb->mark;
32 iph = ip_hdr(skb);
33 saddr = iph->saddr;
34 daddr = iph->daddr;
35 tos = iph->tos;
36
37 ret = nft_do_chain(&pkt, priv);
38 if (ret == NF_ACCEPT) {
39 iph = ip_hdr(skb);
40
41 if (iph->saddr != saddr ||
42 iph->daddr != daddr ||
43 skb->mark != mark ||
44 iph->tos != tos) {
45 err = ip_route_me_harder(state->net, skb, RTN_UNSPEC);
46 if (err < 0)
47 ret = NF_DROP_ERR(err);
48 }
49 }
50 return ret;
51}
52
53static const struct nft_chain_type nft_chain_route_ipv4 = {
54 .name = "route",
55 .type = NFT_CHAIN_T_ROUTE,
56 .family = NFPROTO_IPV4,
57 .hook_mask = (1 << NF_INET_LOCAL_OUT),
58 .hooks = {
59 [NF_INET_LOCAL_OUT] = nf_route_table_hook4,
60 },
61};
62#endif
63
64#ifdef CONFIG_NF_TABLES_IPV6
65static unsigned int nf_route_table_hook6(void *priv,
66 struct sk_buff *skb,
67 const struct nf_hook_state *state)
68{
69 struct in6_addr saddr, daddr;
70 struct nft_pktinfo pkt;
71 u32 mark, flowlabel;
72 unsigned int ret;
73 u8 hop_limit;
74 int err;
75
76 nft_set_pktinfo(&pkt, skb, state);
77 nft_set_pktinfo_ipv6(&pkt, skb);
78
79 /* save source/dest address, mark, hoplimit, flowlabel, priority */
80 memcpy(&saddr, &ipv6_hdr(skb)->saddr, sizeof(saddr));
81 memcpy(&daddr, &ipv6_hdr(skb)->daddr, sizeof(daddr));
82 mark = skb->mark;
83 hop_limit = ipv6_hdr(skb)->hop_limit;
84
85 /* flowlabel and prio (includes version, which shouldn't change either)*/
86 flowlabel = *((u32 *)ipv6_hdr(skb));
87
88 ret = nft_do_chain(&pkt, priv);
89 if (ret == NF_ACCEPT &&
90 (memcmp(&ipv6_hdr(skb)->saddr, &saddr, sizeof(saddr)) ||
91 memcmp(&ipv6_hdr(skb)->daddr, &daddr, sizeof(daddr)) ||
92 skb->mark != mark ||
93 ipv6_hdr(skb)->hop_limit != hop_limit ||
94 flowlabel != *((u32 *)ipv6_hdr(skb)))) {
95 err = nf_ip6_route_me_harder(state->net, skb);
96 if (err < 0)
97 ret = NF_DROP_ERR(err);
98 }
99
100 return ret;
101}
102
103static const struct nft_chain_type nft_chain_route_ipv6 = {
104 .name = "route",
105 .type = NFT_CHAIN_T_ROUTE,
106 .family = NFPROTO_IPV6,
107 .hook_mask = (1 << NF_INET_LOCAL_OUT),
108 .hooks = {
109 [NF_INET_LOCAL_OUT] = nf_route_table_hook6,
110 },
111};
112#endif
113
114#ifdef CONFIG_NF_TABLES_INET
115static unsigned int nf_route_table_inet(void *priv,
116 struct sk_buff *skb,
117 const struct nf_hook_state *state)
118{
119 struct nft_pktinfo pkt;
120
121 switch (state->pf) {
122 case NFPROTO_IPV4:
123 return nf_route_table_hook4(priv, skb, state);
124 case NFPROTO_IPV6:
125 return nf_route_table_hook6(priv, skb, state);
126 default:
127 nft_set_pktinfo(&pkt, skb, state);
128 break;
129 }
130
131 return nft_do_chain(&pkt, priv);
132}
133
134static const struct nft_chain_type nft_chain_route_inet = {
135 .name = "route",
136 .type = NFT_CHAIN_T_ROUTE,
137 .family = NFPROTO_INET,
138 .hook_mask = (1 << NF_INET_LOCAL_OUT),
139 .hooks = {
140 [NF_INET_LOCAL_OUT] = nf_route_table_inet,
141 },
142};
143#endif
144
145void __init nft_chain_route_init(void)
146{
147#ifdef CONFIG_NF_TABLES_IPV6
148 nft_register_chain_type(&nft_chain_route_ipv6);
149#endif
150#ifdef CONFIG_NF_TABLES_IPV4
151 nft_register_chain_type(&nft_chain_route_ipv4);
152#endif
153#ifdef CONFIG_NF_TABLES_INET
154 nft_register_chain_type(&nft_chain_route_inet);
155#endif
156}
157
158void __exit nft_chain_route_fini(void)
159{
160#ifdef CONFIG_NF_TABLES_IPV6
161 nft_unregister_chain_type(&nft_chain_route_ipv6);
162#endif
163#ifdef CONFIG_NF_TABLES_IPV4
164 nft_unregister_chain_type(&nft_chain_route_ipv4);
165#endif
166#ifdef CONFIG_NF_TABLES_INET
167 nft_unregister_chain_type(&nft_chain_route_inet);
168#endif
169}

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t