0001
0002 #include <linux/kernel.h>
0003 #include <linux/module.h>
0004 #include <linux/init.h>
0005 #include <linux/netlink.h>
0006 #include <linux/netfilter.h>
0007 #include <linux/workqueue.h>
0008 #include <linux/spinlock.h>
0009 #include <linux/netfilter/nf_conntrack_common.h>
0010 #include <linux/netfilter/nf_tables.h>
0011 #include <net/ip.h> /* for ipv4 options. */
0012 #include <net/netfilter/nf_tables.h>
0013 #include <net/netfilter/nf_tables_core.h>
0014 #include <net/netfilter/nf_conntrack_core.h>
0015 #include <net/netfilter/nf_conntrack_extend.h>
0016 #include <net/netfilter/nf_flow_table.h>
0017
0018 struct nft_flow_offload {
0019 struct nft_flowtable *flowtable;
0020 };
0021
0022 static enum flow_offload_xmit_type nft_xmit_type(struct dst_entry *dst)
0023 {
0024 if (dst_xfrm(dst))
0025 return FLOW_OFFLOAD_XMIT_XFRM;
0026
0027 return FLOW_OFFLOAD_XMIT_NEIGH;
0028 }
0029
0030 static void nft_default_forward_path(struct nf_flow_route *route,
0031 struct dst_entry *dst_cache,
0032 enum ip_conntrack_dir dir)
0033 {
0034 route->tuple[!dir].in.ifindex = dst_cache->dev->ifindex;
0035 route->tuple[dir].dst = dst_cache;
0036 route->tuple[dir].xmit_type = nft_xmit_type(dst_cache);
0037 }
0038
0039 static bool nft_is_valid_ether_device(const struct net_device *dev)
0040 {
0041 if (!dev || (dev->flags & IFF_LOOPBACK) || dev->type != ARPHRD_ETHER ||
0042 dev->addr_len != ETH_ALEN || !is_valid_ether_addr(dev->dev_addr))
0043 return false;
0044
0045 return true;
0046 }
0047
0048 static int nft_dev_fill_forward_path(const struct nf_flow_route *route,
0049 const struct dst_entry *dst_cache,
0050 const struct nf_conn *ct,
0051 enum ip_conntrack_dir dir, u8 *ha,
0052 struct net_device_path_stack *stack)
0053 {
0054 const void *daddr = &ct->tuplehash[!dir].tuple.src.u3;
0055 struct net_device *dev = dst_cache->dev;
0056 struct neighbour *n;
0057 u8 nud_state;
0058
0059 if (!nft_is_valid_ether_device(dev))
0060 goto out;
0061
0062 n = dst_neigh_lookup(dst_cache, daddr);
0063 if (!n)
0064 return -1;
0065
0066 read_lock_bh(&n->lock);
0067 nud_state = n->nud_state;
0068 ether_addr_copy(ha, n->ha);
0069 read_unlock_bh(&n->lock);
0070 neigh_release(n);
0071
0072 if (!(nud_state & NUD_VALID))
0073 return -1;
0074
0075 out:
0076 return dev_fill_forward_path(dev, ha, stack);
0077 }
0078
0079 struct nft_forward_info {
0080 const struct net_device *indev;
0081 const struct net_device *outdev;
0082 const struct net_device *hw_outdev;
0083 struct id {
0084 __u16 id;
0085 __be16 proto;
0086 } encap[NF_FLOW_TABLE_ENCAP_MAX];
0087 u8 num_encaps;
0088 u8 ingress_vlans;
0089 u8 h_source[ETH_ALEN];
0090 u8 h_dest[ETH_ALEN];
0091 enum flow_offload_xmit_type xmit_type;
0092 };
0093
0094 static void nft_dev_path_info(const struct net_device_path_stack *stack,
0095 struct nft_forward_info *info,
0096 unsigned char *ha, struct nf_flowtable *flowtable)
0097 {
0098 const struct net_device_path *path;
0099 int i;
0100
0101 memcpy(info->h_dest, ha, ETH_ALEN);
0102
0103 for (i = 0; i < stack->num_paths; i++) {
0104 path = &stack->path[i];
0105 switch (path->type) {
0106 case DEV_PATH_ETHERNET:
0107 case DEV_PATH_DSA:
0108 case DEV_PATH_VLAN:
0109 case DEV_PATH_PPPOE:
0110 info->indev = path->dev;
0111 if (is_zero_ether_addr(info->h_source))
0112 memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
0113
0114 if (path->type == DEV_PATH_ETHERNET)
0115 break;
0116 if (path->type == DEV_PATH_DSA) {
0117 i = stack->num_paths;
0118 break;
0119 }
0120
0121
0122 if (info->num_encaps >= NF_FLOW_TABLE_ENCAP_MAX) {
0123 info->indev = NULL;
0124 break;
0125 }
0126 if (!info->outdev)
0127 info->outdev = path->dev;
0128 info->encap[info->num_encaps].id = path->encap.id;
0129 info->encap[info->num_encaps].proto = path->encap.proto;
0130 info->num_encaps++;
0131 if (path->type == DEV_PATH_PPPOE)
0132 memcpy(info->h_dest, path->encap.h_dest, ETH_ALEN);
0133 break;
0134 case DEV_PATH_BRIDGE:
0135 if (is_zero_ether_addr(info->h_source))
0136 memcpy(info->h_source, path->dev->dev_addr, ETH_ALEN);
0137
0138 switch (path->bridge.vlan_mode) {
0139 case DEV_PATH_BR_VLAN_UNTAG_HW:
0140 info->ingress_vlans |= BIT(info->num_encaps - 1);
0141 break;
0142 case DEV_PATH_BR_VLAN_TAG:
0143 info->encap[info->num_encaps].id = path->bridge.vlan_id;
0144 info->encap[info->num_encaps].proto = path->bridge.vlan_proto;
0145 info->num_encaps++;
0146 break;
0147 case DEV_PATH_BR_VLAN_UNTAG:
0148 info->num_encaps--;
0149 break;
0150 case DEV_PATH_BR_VLAN_KEEP:
0151 break;
0152 }
0153 info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
0154 break;
0155 default:
0156 info->indev = NULL;
0157 break;
0158 }
0159 }
0160 if (!info->outdev)
0161 info->outdev = info->indev;
0162
0163 info->hw_outdev = info->indev;
0164
0165 if (nf_flowtable_hw_offload(flowtable) &&
0166 nft_is_valid_ether_device(info->indev))
0167 info->xmit_type = FLOW_OFFLOAD_XMIT_DIRECT;
0168 }
0169
0170 static bool nft_flowtable_find_dev(const struct net_device *dev,
0171 struct nft_flowtable *ft)
0172 {
0173 struct nft_hook *hook;
0174 bool found = false;
0175
0176 list_for_each_entry_rcu(hook, &ft->hook_list, list) {
0177 if (hook->ops.dev != dev)
0178 continue;
0179
0180 found = true;
0181 break;
0182 }
0183
0184 return found;
0185 }
0186
0187 static void nft_dev_forward_path(struct nf_flow_route *route,
0188 const struct nf_conn *ct,
0189 enum ip_conntrack_dir dir,
0190 struct nft_flowtable *ft)
0191 {
0192 const struct dst_entry *dst = route->tuple[dir].dst;
0193 struct net_device_path_stack stack;
0194 struct nft_forward_info info = {};
0195 unsigned char ha[ETH_ALEN];
0196 int i;
0197
0198 if (nft_dev_fill_forward_path(route, dst, ct, dir, ha, &stack) >= 0)
0199 nft_dev_path_info(&stack, &info, ha, &ft->data);
0200
0201 if (!info.indev || !nft_flowtable_find_dev(info.indev, ft))
0202 return;
0203
0204 route->tuple[!dir].in.ifindex = info.indev->ifindex;
0205 for (i = 0; i < info.num_encaps; i++) {
0206 route->tuple[!dir].in.encap[i].id = info.encap[i].id;
0207 route->tuple[!dir].in.encap[i].proto = info.encap[i].proto;
0208 }
0209 route->tuple[!dir].in.num_encaps = info.num_encaps;
0210 route->tuple[!dir].in.ingress_vlans = info.ingress_vlans;
0211
0212 if (info.xmit_type == FLOW_OFFLOAD_XMIT_DIRECT) {
0213 memcpy(route->tuple[dir].out.h_source, info.h_source, ETH_ALEN);
0214 memcpy(route->tuple[dir].out.h_dest, info.h_dest, ETH_ALEN);
0215 route->tuple[dir].out.ifindex = info.outdev->ifindex;
0216 route->tuple[dir].out.hw_ifindex = info.hw_outdev->ifindex;
0217 route->tuple[dir].xmit_type = info.xmit_type;
0218 }
0219 }
0220
0221 static int nft_flow_route(const struct nft_pktinfo *pkt,
0222 const struct nf_conn *ct,
0223 struct nf_flow_route *route,
0224 enum ip_conntrack_dir dir,
0225 struct nft_flowtable *ft)
0226 {
0227 struct dst_entry *this_dst = skb_dst(pkt->skb);
0228 struct dst_entry *other_dst = NULL;
0229 struct flowi fl;
0230
0231 memset(&fl, 0, sizeof(fl));
0232 switch (nft_pf(pkt)) {
0233 case NFPROTO_IPV4:
0234 fl.u.ip4.daddr = ct->tuplehash[dir].tuple.src.u3.ip;
0235 fl.u.ip4.saddr = ct->tuplehash[!dir].tuple.src.u3.ip;
0236 fl.u.ip4.flowi4_oif = nft_in(pkt)->ifindex;
0237 fl.u.ip4.flowi4_iif = this_dst->dev->ifindex;
0238 fl.u.ip4.flowi4_tos = RT_TOS(ip_hdr(pkt->skb)->tos);
0239 fl.u.ip4.flowi4_mark = pkt->skb->mark;
0240 fl.u.ip4.flowi4_flags = FLOWI_FLAG_ANYSRC;
0241 break;
0242 case NFPROTO_IPV6:
0243 fl.u.ip6.daddr = ct->tuplehash[dir].tuple.src.u3.in6;
0244 fl.u.ip6.saddr = ct->tuplehash[!dir].tuple.src.u3.in6;
0245 fl.u.ip6.flowi6_oif = nft_in(pkt)->ifindex;
0246 fl.u.ip6.flowi6_iif = this_dst->dev->ifindex;
0247 fl.u.ip6.flowlabel = ip6_flowinfo(ipv6_hdr(pkt->skb));
0248 fl.u.ip6.flowi6_mark = pkt->skb->mark;
0249 fl.u.ip6.flowi6_flags = FLOWI_FLAG_ANYSRC;
0250 break;
0251 }
0252
0253 nf_route(nft_net(pkt), &other_dst, &fl, false, nft_pf(pkt));
0254 if (!other_dst)
0255 return -ENOENT;
0256
0257 nft_default_forward_path(route, this_dst, dir);
0258 nft_default_forward_path(route, other_dst, !dir);
0259
0260 if (route->tuple[dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH &&
0261 route->tuple[!dir].xmit_type == FLOW_OFFLOAD_XMIT_NEIGH) {
0262 nft_dev_forward_path(route, ct, dir, ft);
0263 nft_dev_forward_path(route, ct, !dir, ft);
0264 }
0265
0266 return 0;
0267 }
0268
0269 static bool nft_flow_offload_skip(struct sk_buff *skb, int family)
0270 {
0271 if (skb_sec_path(skb))
0272 return true;
0273
0274 if (family == NFPROTO_IPV4) {
0275 const struct ip_options *opt;
0276
0277 opt = &(IPCB(skb)->opt);
0278
0279 if (unlikely(opt->optlen))
0280 return true;
0281 }
0282
0283 return false;
0284 }
0285
0286 static void nft_flow_offload_eval(const struct nft_expr *expr,
0287 struct nft_regs *regs,
0288 const struct nft_pktinfo *pkt)
0289 {
0290 struct nft_flow_offload *priv = nft_expr_priv(expr);
0291 struct nf_flowtable *flowtable = &priv->flowtable->data;
0292 struct tcphdr _tcph, *tcph = NULL;
0293 struct nf_flow_route route = {};
0294 enum ip_conntrack_info ctinfo;
0295 struct flow_offload *flow;
0296 enum ip_conntrack_dir dir;
0297 struct nf_conn *ct;
0298 int ret;
0299
0300 if (nft_flow_offload_skip(pkt->skb, nft_pf(pkt)))
0301 goto out;
0302
0303 ct = nf_ct_get(pkt->skb, &ctinfo);
0304 if (!ct)
0305 goto out;
0306
0307 switch (ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum) {
0308 case IPPROTO_TCP:
0309 tcph = skb_header_pointer(pkt->skb, nft_thoff(pkt),
0310 sizeof(_tcph), &_tcph);
0311 if (unlikely(!tcph || tcph->fin || tcph->rst ||
0312 !nf_conntrack_tcp_established(ct)))
0313 goto out;
0314 break;
0315 case IPPROTO_UDP:
0316 break;
0317 #ifdef CONFIG_NF_CT_PROTO_GRE
0318 case IPPROTO_GRE: {
0319 struct nf_conntrack_tuple *tuple;
0320
0321 if (ct->status & IPS_NAT_MASK)
0322 goto out;
0323 tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
0324
0325 if (tuple->src.u.gre.key || tuple->dst.u.gre.key)
0326 goto out;
0327 break;
0328 }
0329 #endif
0330 default:
0331 goto out;
0332 }
0333
0334 if (nf_ct_ext_exist(ct, NF_CT_EXT_HELPER) ||
0335 ct->status & (IPS_SEQ_ADJUST | IPS_NAT_CLASH))
0336 goto out;
0337
0338 if (!nf_ct_is_confirmed(ct))
0339 goto out;
0340
0341 if (test_and_set_bit(IPS_OFFLOAD_BIT, &ct->status))
0342 goto out;
0343
0344 dir = CTINFO2DIR(ctinfo);
0345 if (nft_flow_route(pkt, ct, &route, dir, priv->flowtable) < 0)
0346 goto err_flow_route;
0347
0348 flow = flow_offload_alloc(ct);
0349 if (!flow)
0350 goto err_flow_alloc;
0351
0352 if (flow_offload_route_init(flow, &route) < 0)
0353 goto err_flow_add;
0354
0355 if (tcph) {
0356 ct->proto.tcp.seen[0].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
0357 ct->proto.tcp.seen[1].flags |= IP_CT_TCP_FLAG_BE_LIBERAL;
0358 }
0359
0360 ret = flow_offload_add(flowtable, flow);
0361 if (ret < 0)
0362 goto err_flow_add;
0363
0364 dst_release(route.tuple[!dir].dst);
0365 return;
0366
0367 err_flow_add:
0368 flow_offload_free(flow);
0369 err_flow_alloc:
0370 dst_release(route.tuple[!dir].dst);
0371 err_flow_route:
0372 clear_bit(IPS_OFFLOAD_BIT, &ct->status);
0373 out:
0374 regs->verdict.code = NFT_BREAK;
0375 }
0376
0377 static int nft_flow_offload_validate(const struct nft_ctx *ctx,
0378 const struct nft_expr *expr,
0379 const struct nft_data **data)
0380 {
0381 unsigned int hook_mask = (1 << NF_INET_FORWARD);
0382
0383 return nft_chain_validate_hooks(ctx->chain, hook_mask);
0384 }
0385
0386 static const struct nla_policy nft_flow_offload_policy[NFTA_FLOW_MAX + 1] = {
0387 [NFTA_FLOW_TABLE_NAME] = { .type = NLA_STRING,
0388 .len = NFT_NAME_MAXLEN - 1 },
0389 };
0390
0391 static int nft_flow_offload_init(const struct nft_ctx *ctx,
0392 const struct nft_expr *expr,
0393 const struct nlattr * const tb[])
0394 {
0395 struct nft_flow_offload *priv = nft_expr_priv(expr);
0396 u8 genmask = nft_genmask_next(ctx->net);
0397 struct nft_flowtable *flowtable;
0398
0399 if (!tb[NFTA_FLOW_TABLE_NAME])
0400 return -EINVAL;
0401
0402 flowtable = nft_flowtable_lookup(ctx->table, tb[NFTA_FLOW_TABLE_NAME],
0403 genmask);
0404 if (IS_ERR(flowtable))
0405 return PTR_ERR(flowtable);
0406
0407 priv->flowtable = flowtable;
0408 flowtable->use++;
0409
0410 return nf_ct_netns_get(ctx->net, ctx->family);
0411 }
0412
0413 static void nft_flow_offload_deactivate(const struct nft_ctx *ctx,
0414 const struct nft_expr *expr,
0415 enum nft_trans_phase phase)
0416 {
0417 struct nft_flow_offload *priv = nft_expr_priv(expr);
0418
0419 nf_tables_deactivate_flowtable(ctx, priv->flowtable, phase);
0420 }
0421
0422 static void nft_flow_offload_activate(const struct nft_ctx *ctx,
0423 const struct nft_expr *expr)
0424 {
0425 struct nft_flow_offload *priv = nft_expr_priv(expr);
0426
0427 priv->flowtable->use++;
0428 }
0429
0430 static void nft_flow_offload_destroy(const struct nft_ctx *ctx,
0431 const struct nft_expr *expr)
0432 {
0433 nf_ct_netns_put(ctx->net, ctx->family);
0434 }
0435
0436 static int nft_flow_offload_dump(struct sk_buff *skb, const struct nft_expr *expr)
0437 {
0438 struct nft_flow_offload *priv = nft_expr_priv(expr);
0439
0440 if (nla_put_string(skb, NFTA_FLOW_TABLE_NAME, priv->flowtable->name))
0441 goto nla_put_failure;
0442
0443 return 0;
0444
0445 nla_put_failure:
0446 return -1;
0447 }
0448
0449 static struct nft_expr_type nft_flow_offload_type;
0450 static const struct nft_expr_ops nft_flow_offload_ops = {
0451 .type = &nft_flow_offload_type,
0452 .size = NFT_EXPR_SIZE(sizeof(struct nft_flow_offload)),
0453 .eval = nft_flow_offload_eval,
0454 .init = nft_flow_offload_init,
0455 .activate = nft_flow_offload_activate,
0456 .deactivate = nft_flow_offload_deactivate,
0457 .destroy = nft_flow_offload_destroy,
0458 .validate = nft_flow_offload_validate,
0459 .dump = nft_flow_offload_dump,
0460 .reduce = NFT_REDUCE_READONLY,
0461 };
0462
0463 static struct nft_expr_type nft_flow_offload_type __read_mostly = {
0464 .name = "flow_offload",
0465 .ops = &nft_flow_offload_ops,
0466 .policy = nft_flow_offload_policy,
0467 .maxattr = NFTA_FLOW_MAX,
0468 .owner = THIS_MODULE,
0469 };
0470
0471 static int flow_offload_netdev_event(struct notifier_block *this,
0472 unsigned long event, void *ptr)
0473 {
0474 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
0475
0476 if (event != NETDEV_DOWN)
0477 return NOTIFY_DONE;
0478
0479 nf_flow_table_cleanup(dev);
0480
0481 return NOTIFY_DONE;
0482 }
0483
0484 static struct notifier_block flow_offload_netdev_notifier = {
0485 .notifier_call = flow_offload_netdev_event,
0486 };
0487
0488 static int __init nft_flow_offload_module_init(void)
0489 {
0490 int err;
0491
0492 err = register_netdevice_notifier(&flow_offload_netdev_notifier);
0493 if (err)
0494 goto err;
0495
0496 err = nft_register_expr(&nft_flow_offload_type);
0497 if (err < 0)
0498 goto register_expr;
0499
0500 return 0;
0501
0502 register_expr:
0503 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
0504 err:
0505 return err;
0506 }
0507
0508 static void __exit nft_flow_offload_module_exit(void)
0509 {
0510 nft_unregister_expr(&nft_flow_offload_type);
0511 unregister_netdevice_notifier(&flow_offload_netdev_notifier);
0512 }
0513
0514 module_init(nft_flow_offload_module_init);
0515 module_exit(nft_flow_offload_module_exit);
0516
0517 MODULE_LICENSE("GPL");
0518 MODULE_AUTHOR("Pablo Neira Ayuso <pablo@netfilter.org>");
0519 MODULE_ALIAS_NFT_EXPR("flow_offload");
0520 MODULE_DESCRIPTION("nftables hardware flow offload module");