nft_fwd_netdev.c (6712B)
1// SPDX-License-Identifier: GPL-2.0-only 2/* 3 * Copyright (c) 2015 Pablo Neira Ayuso <pablo@netfilter.org> 4 */ 5 6#include <linux/kernel.h> 7#include <linux/init.h> 8#include <linux/module.h> 9#include <linux/netlink.h> 10#include <linux/netfilter.h> 11#include <linux/netfilter/nf_tables.h> 12#include <linux/ip.h> 13#include <linux/ipv6.h> 14#include <net/netfilter/nf_tables.h> 15#include <net/netfilter/nf_tables_offload.h> 16#include <net/netfilter/nf_dup_netdev.h> 17#include <net/neighbour.h> 18#include <net/ip.h> 19 20struct nft_fwd_netdev { 21 u8 sreg_dev; 22}; 23 24static void nft_fwd_netdev_eval(const struct nft_expr *expr, 25 struct nft_regs *regs, 26 const struct nft_pktinfo *pkt) 27{ 28 struct nft_fwd_netdev *priv = nft_expr_priv(expr); 29 int oif = regs->data[priv->sreg_dev]; 30 struct sk_buff *skb = pkt->skb; 31 32 /* This is used by ifb only. */ 33 skb->skb_iif = skb->dev->ifindex; 34 skb_set_redirected(skb, nft_hook(pkt) == NF_NETDEV_INGRESS); 35 36 nf_fwd_netdev_egress(pkt, oif); 37 regs->verdict.code = NF_STOLEN; 38} 39 40static const struct nla_policy nft_fwd_netdev_policy[NFTA_FWD_MAX + 1] = { 41 [NFTA_FWD_SREG_DEV] = { .type = NLA_U32 }, 42 [NFTA_FWD_SREG_ADDR] = { .type = NLA_U32 }, 43 [NFTA_FWD_NFPROTO] = { .type = NLA_U32 }, 44}; 45 46static int nft_fwd_netdev_init(const struct nft_ctx *ctx, 47 const struct nft_expr *expr, 48 const struct nlattr * const tb[]) 49{ 50 struct nft_fwd_netdev *priv = nft_expr_priv(expr); 51 52 if (tb[NFTA_FWD_SREG_DEV] == NULL) 53 return -EINVAL; 54 55 return nft_parse_register_load(tb[NFTA_FWD_SREG_DEV], &priv->sreg_dev, 56 sizeof(int)); 57} 58 59static int nft_fwd_netdev_dump(struct sk_buff *skb, const struct nft_expr *expr) 60{ 61 struct nft_fwd_netdev *priv = nft_expr_priv(expr); 62 63 if (nft_dump_register(skb, NFTA_FWD_SREG_DEV, priv->sreg_dev)) 64 goto nla_put_failure; 65 66 return 0; 67 68nla_put_failure: 69 return -1; 70} 71 72static int nft_fwd_netdev_offload(struct nft_offload_ctx *ctx, 73 struct nft_flow_rule *flow, 74 const struct nft_expr *expr) 75{ 76 const struct nft_fwd_netdev *priv = nft_expr_priv(expr); 77 int oif = ctx->regs[priv->sreg_dev].data.data[0]; 78 79 return nft_fwd_dup_netdev_offload(ctx, flow, FLOW_ACTION_REDIRECT, oif); 80} 81 82static bool nft_fwd_netdev_offload_action(const struct nft_expr *expr) 83{ 84 return true; 85} 86 87struct nft_fwd_neigh { 88 u8 sreg_dev; 89 u8 sreg_addr; 90 u8 nfproto; 91}; 92 93static void nft_fwd_neigh_eval(const struct nft_expr *expr, 94 struct nft_regs *regs, 95 const struct nft_pktinfo *pkt) 96{ 97 struct nft_fwd_neigh *priv = nft_expr_priv(expr); 98 void *addr = ®s->data[priv->sreg_addr]; 99 int oif = regs->data[priv->sreg_dev]; 100 unsigned int verdict = NF_STOLEN; 101 struct sk_buff *skb = pkt->skb; 102 struct net_device *dev; 103 int neigh_table; 104 105 switch (priv->nfproto) { 106 case NFPROTO_IPV4: { 107 struct iphdr *iph; 108 109 if (skb->protocol != htons(ETH_P_IP)) { 110 verdict = NFT_BREAK; 111 goto out; 112 } 113 if (skb_try_make_writable(skb, sizeof(*iph))) { 114 verdict = NF_DROP; 115 goto out; 116 } 117 iph = ip_hdr(skb); 118 ip_decrease_ttl(iph); 119 neigh_table = NEIGH_ARP_TABLE; 120 break; 121 } 122 case NFPROTO_IPV6: { 123 struct ipv6hdr *ip6h; 124 125 if (skb->protocol != htons(ETH_P_IPV6)) { 126 verdict = NFT_BREAK; 127 goto out; 128 } 129 if (skb_try_make_writable(skb, sizeof(*ip6h))) { 130 verdict = NF_DROP; 131 goto out; 132 } 133 ip6h = ipv6_hdr(skb); 134 ip6h->hop_limit--; 135 neigh_table = NEIGH_ND_TABLE; 136 break; 137 } 138 default: 139 verdict = NFT_BREAK; 140 goto out; 141 } 142 143 dev = dev_get_by_index_rcu(nft_net(pkt), oif); 144 if (dev == NULL) 145 return; 146 147 skb->dev = dev; 148 skb_clear_tstamp(skb); 149 neigh_xmit(neigh_table, dev, addr, skb); 150out: 151 regs->verdict.code = verdict; 152} 153 154static int nft_fwd_neigh_init(const struct nft_ctx *ctx, 155 const struct nft_expr *expr, 156 const struct nlattr * const tb[]) 157{ 158 struct nft_fwd_neigh *priv = nft_expr_priv(expr); 159 unsigned int addr_len; 160 int err; 161 162 if (!tb[NFTA_FWD_SREG_DEV] || 163 !tb[NFTA_FWD_SREG_ADDR] || 164 !tb[NFTA_FWD_NFPROTO]) 165 return -EINVAL; 166 167 priv->nfproto = ntohl(nla_get_be32(tb[NFTA_FWD_NFPROTO])); 168 169 switch (priv->nfproto) { 170 case NFPROTO_IPV4: 171 addr_len = sizeof(struct in_addr); 172 break; 173 case NFPROTO_IPV6: 174 addr_len = sizeof(struct in6_addr); 175 break; 176 default: 177 return -EOPNOTSUPP; 178 } 179 180 err = nft_parse_register_load(tb[NFTA_FWD_SREG_DEV], &priv->sreg_dev, 181 sizeof(int)); 182 if (err < 0) 183 return err; 184 185 return nft_parse_register_load(tb[NFTA_FWD_SREG_ADDR], &priv->sreg_addr, 186 addr_len); 187} 188 189static int nft_fwd_neigh_dump(struct sk_buff *skb, const struct nft_expr *expr) 190{ 191 struct nft_fwd_neigh *priv = nft_expr_priv(expr); 192 193 if (nft_dump_register(skb, NFTA_FWD_SREG_DEV, priv->sreg_dev) || 194 nft_dump_register(skb, NFTA_FWD_SREG_ADDR, priv->sreg_addr) || 195 nla_put_be32(skb, NFTA_FWD_NFPROTO, htonl(priv->nfproto))) 196 goto nla_put_failure; 197 198 return 0; 199 200nla_put_failure: 201 return -1; 202} 203 204static int nft_fwd_validate(const struct nft_ctx *ctx, 205 const struct nft_expr *expr, 206 const struct nft_data **data) 207{ 208 return nft_chain_validate_hooks(ctx->chain, (1 << NF_NETDEV_INGRESS) | 209 (1 << NF_NETDEV_EGRESS)); 210} 211 212static struct nft_expr_type nft_fwd_netdev_type; 213static const struct nft_expr_ops nft_fwd_neigh_netdev_ops = { 214 .type = &nft_fwd_netdev_type, 215 .size = NFT_EXPR_SIZE(sizeof(struct nft_fwd_neigh)), 216 .eval = nft_fwd_neigh_eval, 217 .init = nft_fwd_neigh_init, 218 .dump = nft_fwd_neigh_dump, 219 .validate = nft_fwd_validate, 220 .reduce = NFT_REDUCE_READONLY, 221}; 222 223static const struct nft_expr_ops nft_fwd_netdev_ops = { 224 .type = &nft_fwd_netdev_type, 225 .size = NFT_EXPR_SIZE(sizeof(struct nft_fwd_netdev)), 226 .eval = nft_fwd_netdev_eval, 227 .init = nft_fwd_netdev_init, 228 .dump = nft_fwd_netdev_dump, 229 .validate = nft_fwd_validate, 230 .reduce = NFT_REDUCE_READONLY, 231 .offload = nft_fwd_netdev_offload, 232 .offload_action = nft_fwd_netdev_offload_action, 233}; 234 235static const struct nft_expr_ops * 236nft_fwd_select_ops(const struct nft_ctx *ctx, 237 const struct nlattr * const tb[]) 238{ 239 if (tb[NFTA_FWD_SREG_ADDR]) 240 return &nft_fwd_neigh_netdev_ops; 241 if (tb[NFTA_FWD_SREG_DEV]) 242 return &nft_fwd_netdev_ops; 243 244 return ERR_PTR(-EOPNOTSUPP); 245} 246 247static struct nft_expr_type nft_fwd_netdev_type __read_mostly = { 248 .family = NFPROTO_NETDEV, 249 .name = "fwd", 250 .select_ops = nft_fwd_select_ops, 251 .policy = nft_fwd_netdev_policy, 252 .maxattr = NFTA_FWD_MAX, 253 .owner = THIS_MODULE, 254}; 255 256static int __init nft_fwd_netdev_module_init(void) 257{ 258 return nft_register_expr(&nft_fwd_netdev_type); 259} 260 261static void __exit nft_fwd_netdev_module_exit(void) 262{ 263 nft_unregister_expr(&nft_fwd_netdev_type); 264} 265 266module_init(nft_fwd_netdev_module_init); 267module_exit(nft_fwd_netdev_module_exit); 268 269MODULE_LICENSE("GPL"); 270MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>"); 271MODULE_ALIAS_NFT_AF_EXPR(5, "fwd");