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.
9 #include <linux/kernel.h>
10 #include <linux/init.h>
11 #include <linux/module.h>
12 #include <linux/netlink.h>
13 #include <linux/netfilter.h>
14 #include <linux/netfilter/nf_tables.h>
16 #include <net/ip6_route.h>
17 #include <net/route.h>
18 #include <net/netfilter/nf_tables.h>
19 #include <net/netfilter/nf_tables_core.h>
22 enum nft_rt_keys key:8;
23 enum nft_registers dreg:8;
26 void nft_rt_get_eval(const struct nft_expr *expr,
27 struct nft_regs *regs,
28 const struct nft_pktinfo *pkt)
30 const struct nft_rt *priv = nft_expr_priv(expr);
31 const struct sk_buff *skb = pkt->skb;
32 u32 *dest = ®s->data[priv->dreg];
33 const struct dst_entry *dst;
40 #ifdef CONFIG_IP_ROUTE_CLASSID
42 *dest = dst->tclassid;
46 if (nft_pf(pkt) != NFPROTO_IPV4)
49 *dest = rt_nexthop((const struct rtable *)dst,
53 if (nft_pf(pkt) != NFPROTO_IPV6)
56 memcpy(dest, rt6_nexthop((struct rt6_info *)dst,
57 &ipv6_hdr(skb)->daddr),
58 sizeof(struct in6_addr));
67 regs->verdict.code = NFT_BREAK;
70 const struct nla_policy nft_rt_policy[NFTA_RT_MAX + 1] = {
71 [NFTA_RT_DREG] = { .type = NLA_U32 },
72 [NFTA_RT_KEY] = { .type = NLA_U32 },
75 int nft_rt_get_init(const struct nft_ctx *ctx,
76 const struct nft_expr *expr,
77 const struct nlattr * const tb[])
79 struct nft_rt *priv = nft_expr_priv(expr);
82 if (tb[NFTA_RT_KEY] == NULL ||
83 tb[NFTA_RT_DREG] == NULL)
86 priv->key = ntohl(nla_get_be32(tb[NFTA_RT_KEY]));
88 #ifdef CONFIG_IP_ROUTE_CLASSID
95 len = sizeof(struct in6_addr);
101 priv->dreg = nft_parse_register(tb[NFTA_RT_DREG]);
102 return nft_validate_register_store(ctx, priv->dreg, NULL,
103 NFT_DATA_VALUE, len);
106 int nft_rt_get_dump(struct sk_buff *skb,
107 const struct nft_expr *expr)
109 const struct nft_rt *priv = nft_expr_priv(expr);
111 if (nla_put_be32(skb, NFTA_RT_KEY, htonl(priv->key)))
112 goto nla_put_failure;
113 if (nft_dump_register(skb, NFTA_RT_DREG, priv->dreg))
114 goto nla_put_failure;
121 static struct nft_expr_type nft_rt_type;
122 static const struct nft_expr_ops nft_rt_get_ops = {
123 .type = &nft_rt_type,
124 .size = NFT_EXPR_SIZE(sizeof(struct nft_rt)),
125 .eval = nft_rt_get_eval,
126 .init = nft_rt_get_init,
127 .dump = nft_rt_get_dump,
130 static struct nft_expr_type nft_rt_type __read_mostly = {
132 .ops = &nft_rt_get_ops,
133 .policy = nft_rt_policy,
134 .maxattr = NFTA_RT_MAX,
135 .owner = THIS_MODULE,
138 static int __init nft_rt_module_init(void)
140 return nft_register_expr(&nft_rt_type);
143 static void __exit nft_rt_module_exit(void)
145 nft_unregister_expr(&nft_rt_type);
148 module_init(nft_rt_module_init);
149 module_exit(nft_rt_module_exit);
151 MODULE_LICENSE("GPL");
153 MODULE_ALIAS_NFT_EXPR("rt");