1
2
3#include <linux/kernel.h>
4#include <linux/init.h>
5#include <linux/module.h>
6#include <linux/netlink.h>
7#include <linux/netfilter.h>
8#include <linux/netfilter/nf_tables.h>
9#include <net/netfilter/nf_tables_core.h>
10#include <net/netfilter/nf_tables.h>
11#include <net/netfilter/nft_fib.h>
12
13#include <net/ip_fib.h>
14#include <net/route.h>
15
16
17static __be32 get_saddr(__be32 addr)
18{
19 if (ipv4_is_multicast(addr) || ipv4_is_lbcast(addr) ||
20 ipv4_is_zeronet(addr))
21 return 0;
22 return addr;
23}
24
25#define DSCP_BITS 0xfc
26
27void nft_fib4_eval_type(const struct nft_expr *expr, struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
29{
30 const struct nft_fib *priv = nft_expr_priv(expr);
31 int noff = skb_network_offset(pkt->skb);
32 u32 *dst = ®s->data[priv->dreg];
33 const struct net_device *dev = NULL;
34 struct iphdr *iph, _iph;
35 __be32 addr;
36
37 if (priv->flags & NFTA_FIB_F_IIF)
38 dev = nft_in(pkt);
39 else if (priv->flags & NFTA_FIB_F_OIF)
40 dev = nft_out(pkt);
41
42 iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
43 if (!iph) {
44 regs->verdict.code = NFT_BREAK;
45 return;
46 }
47
48 if (priv->flags & NFTA_FIB_F_DADDR)
49 addr = iph->daddr;
50 else
51 addr = iph->saddr;
52
53 *dst = inet_dev_addr_type(nft_net(pkt), dev, addr);
54}
55EXPORT_SYMBOL_GPL(nft_fib4_eval_type);
56
57void nft_fib4_eval(const struct nft_expr *expr, struct nft_regs *regs,
58 const struct nft_pktinfo *pkt)
59{
60 const struct nft_fib *priv = nft_expr_priv(expr);
61 int noff = skb_network_offset(pkt->skb);
62 u32 *dest = ®s->data[priv->dreg];
63 struct iphdr *iph, _iph;
64 struct fib_result res;
65 struct flowi4 fl4 = {
66 .flowi4_scope = RT_SCOPE_UNIVERSE,
67 .flowi4_iif = LOOPBACK_IFINDEX,
68 };
69 const struct net_device *oif;
70 const struct net_device *found;
71
72
73
74
75
76
77
78
79 if (priv->flags & NFTA_FIB_F_OIF)
80 oif = nft_out(pkt);
81 else if (priv->flags & NFTA_FIB_F_IIF)
82 oif = nft_in(pkt);
83 else
84 oif = NULL;
85
86 if (nft_hook(pkt) == NF_INET_PRE_ROUTING &&
87 nft_fib_is_loopback(pkt->skb, nft_in(pkt))) {
88 nft_fib_store_result(dest, priv, nft_in(pkt));
89 return;
90 }
91
92 iph = skb_header_pointer(pkt->skb, noff, sizeof(_iph), &_iph);
93 if (!iph) {
94 regs->verdict.code = NFT_BREAK;
95 return;
96 }
97
98 if (ipv4_is_zeronet(iph->saddr)) {
99 if (ipv4_is_lbcast(iph->daddr) ||
100 ipv4_is_local_multicast(iph->daddr)) {
101 nft_fib_store_result(dest, priv, pkt->skb->dev);
102 return;
103 }
104 }
105
106 if (priv->flags & NFTA_FIB_F_MARK)
107 fl4.flowi4_mark = pkt->skb->mark;
108
109 fl4.flowi4_tos = iph->tos & DSCP_BITS;
110
111 if (priv->flags & NFTA_FIB_F_DADDR) {
112 fl4.daddr = iph->daddr;
113 fl4.saddr = get_saddr(iph->saddr);
114 } else {
115 fl4.daddr = iph->saddr;
116 fl4.saddr = get_saddr(iph->daddr);
117 }
118
119 *dest = 0;
120
121 if (fib_lookup(nft_net(pkt), &fl4, &res, FIB_LOOKUP_IGNORE_LINKSTATE))
122 return;
123
124 switch (res.type) {
125 case RTN_UNICAST:
126 break;
127 case RTN_LOCAL:
128 return;
129 default:
130 break;
131 }
132
133 if (!oif) {
134 found = FIB_RES_DEV(res);
135 } else {
136 if (!fib_info_nh_uses_dev(res.fi, oif))
137 return;
138
139 found = oif;
140 }
141
142 nft_fib_store_result(dest, priv, found);
143}
144EXPORT_SYMBOL_GPL(nft_fib4_eval);
145
146static struct nft_expr_type nft_fib4_type;
147
148static const struct nft_expr_ops nft_fib4_type_ops = {
149 .type = &nft_fib4_type,
150 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
151 .eval = nft_fib4_eval_type,
152 .init = nft_fib_init,
153 .dump = nft_fib_dump,
154 .validate = nft_fib_validate,
155};
156
157static const struct nft_expr_ops nft_fib4_ops = {
158 .type = &nft_fib4_type,
159 .size = NFT_EXPR_SIZE(sizeof(struct nft_fib)),
160 .eval = nft_fib4_eval,
161 .init = nft_fib_init,
162 .dump = nft_fib_dump,
163 .validate = nft_fib_validate,
164};
165
166static const struct nft_expr_ops *
167nft_fib4_select_ops(const struct nft_ctx *ctx,
168 const struct nlattr * const tb[])
169{
170 enum nft_fib_result result;
171
172 if (!tb[NFTA_FIB_RESULT])
173 return ERR_PTR(-EINVAL);
174
175 result = ntohl(nla_get_be32(tb[NFTA_FIB_RESULT]));
176
177 switch (result) {
178 case NFT_FIB_RESULT_OIF:
179 return &nft_fib4_ops;
180 case NFT_FIB_RESULT_OIFNAME:
181 return &nft_fib4_ops;
182 case NFT_FIB_RESULT_ADDRTYPE:
183 return &nft_fib4_type_ops;
184 default:
185 return ERR_PTR(-EOPNOTSUPP);
186 }
187}
188
189static struct nft_expr_type nft_fib4_type __read_mostly = {
190 .name = "fib",
191 .select_ops = nft_fib4_select_ops,
192 .policy = nft_fib_policy,
193 .maxattr = NFTA_FIB_MAX,
194 .family = NFPROTO_IPV4,
195 .owner = THIS_MODULE,
196};
197
198static int __init nft_fib4_module_init(void)
199{
200 return nft_register_expr(&nft_fib4_type);
201}
202
203static void __exit nft_fib4_module_exit(void)
204{
205 nft_unregister_expr(&nft_fib4_type);
206}
207
208module_init(nft_fib4_module_init);
209module_exit(nft_fib4_module_exit);
210MODULE_LICENSE("GPL");
211MODULE_AUTHOR("Florian Westphal <fw@strlen.de>");
212MODULE_ALIAS_NFT_AF_EXPR(2, "fib");
213MODULE_DESCRIPTION("nftables fib / ip route lookup support");
214