1 /*
2  * Copyright (c) 2017 Pablo M. Bermudo Garay <pablombg@gmail.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * This code is based on net/netfilter/nft_fib_inet.c, written by
9  * Florian Westphal <fw@strlen.de>.
10  */
11 
12 #include <linux/kernel.h>
13 #include <linux/init.h>
14 #include <linux/module.h>
15 #include <linux/netlink.h>
16 #include <linux/netfilter.h>
17 #include <linux/netfilter/nf_tables.h>
18 #include <net/netfilter/nf_tables_core.h>
19 #include <net/netfilter/nf_tables.h>
20 
21 #include <net/netfilter/nft_fib.h>
22 
23 static void nft_fib_netdev_eval(const struct nft_expr *expr,
24 				struct nft_regs *regs,
25 				const struct nft_pktinfo *pkt)
26 {
27 	const struct nft_fib *priv = nft_expr_priv(expr);
28 
29 	switch (ntohs(pkt->skb->protocol)) {
30 	case ETH_P_IP:
31 		switch (priv->result) {
32 		case NFT_FIB_RESULT_OIF:
33 		case NFT_FIB_RESULT_OIFNAME:
34 			return nft_fib4_eval(expr, regs, pkt);
35 		case NFT_FIB_RESULT_ADDRTYPE:
36 			return nft_fib4_eval_type(expr, regs, pkt);
37 		}
38 		break;
39 	case ETH_P_IPV6:
40 		switch (priv->result) {
41 		case NFT_FIB_RESULT_OIF:
42 		case NFT_FIB_RESULT_OIFNAME:
43 			return nft_fib6_eval(expr, regs, pkt);
44 		case NFT_FIB_RESULT_ADDRTYPE:
45 			return nft_fib6_eval_type(expr, regs, pkt);
46 		}
47 		break;
48 	}
49 
50 	regs->verdict.code = NFT_BREAK;
51 }
52 
53 static struct nft_expr_type nft_fib_netdev_type;
54 static const struct nft_expr_ops nft_fib_netdev_ops = {
55 	.type		= &nft_fib_netdev_type,
56 	.size		= NFT_EXPR_SIZE(sizeof(struct nft_fib)),
57 	.eval		= nft_fib_netdev_eval,
58 	.init		= nft_fib_init,
59 	.dump		= nft_fib_dump,
60 	.validate	= nft_fib_validate,
61 };
62 
63 static struct nft_expr_type nft_fib_netdev_type __read_mostly = {
64 	.family		= NFPROTO_NETDEV,
65 	.name		= "fib",
66 	.ops		= &nft_fib_netdev_ops,
67 	.policy		= nft_fib_policy,
68 	.maxattr	= NFTA_FIB_MAX,
69 	.owner		= THIS_MODULE,
70 };
71 
72 static int __init nft_fib_netdev_module_init(void)
73 {
74 	return nft_register_expr(&nft_fib_netdev_type);
75 }
76 
77 static void __exit nft_fib_netdev_module_exit(void)
78 {
79 	nft_unregister_expr(&nft_fib_netdev_type);
80 }
81 
82 module_init(nft_fib_netdev_module_init);
83 module_exit(nft_fib_netdev_module_exit);
84 
85 MODULE_LICENSE("GPL");
86 MODULE_AUTHOR("Pablo M. Bermudo Garay <pablombg@gmail.com>");
87 MODULE_ALIAS_NFT_AF_EXPR(5, "fib");
88