linux/net/netfilter/nft_byteorder.c
<<
>>
Prefs
   1/*
   2 * Copyright (c) 2008-2009 Patrick McHardy <kaber@trash.net>
   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 * Development of this code funded by Astaro AG (http://www.astaro.com/)
   9 */
  10
  11#include <linux/kernel.h>
  12#include <linux/init.h>
  13#include <linux/module.h>
  14#include <linux/netlink.h>
  15#include <linux/netfilter.h>
  16#include <linux/netfilter/nf_tables.h>
  17#include <net/netfilter/nf_tables_core.h>
  18#include <net/netfilter/nf_tables.h>
  19
  20struct nft_byteorder {
  21        enum nft_registers      sreg:8;
  22        enum nft_registers      dreg:8;
  23        enum nft_byteorder_ops  op:8;
  24        u8                      len;
  25        u8                      size;
  26};
  27
  28static void nft_byteorder_eval(const struct nft_expr *expr,
  29                               struct nft_data data[NFT_REG_MAX + 1],
  30                               const struct nft_pktinfo *pkt)
  31{
  32        const struct nft_byteorder *priv = nft_expr_priv(expr);
  33        struct nft_data *src = &data[priv->sreg], *dst = &data[priv->dreg];
  34        union { u32 u32; u16 u16; } *s, *d;
  35        unsigned int i;
  36
  37        s = (void *)src->data;
  38        d = (void *)dst->data;
  39
  40        switch (priv->size) {
  41        case 4:
  42                switch (priv->op) {
  43                case NFT_BYTEORDER_NTOH:
  44                        for (i = 0; i < priv->len / 4; i++)
  45                                d[i].u32 = ntohl((__force __be32)s[i].u32);
  46                        break;
  47                case NFT_BYTEORDER_HTON:
  48                        for (i = 0; i < priv->len / 4; i++)
  49                                d[i].u32 = (__force __u32)htonl(s[i].u32);
  50                        break;
  51                }
  52                break;
  53        case 2:
  54                switch (priv->op) {
  55                case NFT_BYTEORDER_NTOH:
  56                        for (i = 0; i < priv->len / 2; i++)
  57                                d[i].u16 = ntohs((__force __be16)s[i].u16);
  58                        break;
  59                case NFT_BYTEORDER_HTON:
  60                        for (i = 0; i < priv->len / 2; i++)
  61                                d[i].u16 = (__force __u16)htons(s[i].u16);
  62                        break;
  63                }
  64                break;
  65        }
  66}
  67
  68static const struct nla_policy nft_byteorder_policy[NFTA_BYTEORDER_MAX + 1] = {
  69        [NFTA_BYTEORDER_SREG]   = { .type = NLA_U32 },
  70        [NFTA_BYTEORDER_DREG]   = { .type = NLA_U32 },
  71        [NFTA_BYTEORDER_OP]     = { .type = NLA_U32 },
  72        [NFTA_BYTEORDER_LEN]    = { .type = NLA_U32 },
  73        [NFTA_BYTEORDER_SIZE]   = { .type = NLA_U32 },
  74};
  75
  76static int nft_byteorder_init(const struct nft_ctx *ctx,
  77                              const struct nft_expr *expr,
  78                              const struct nlattr * const tb[])
  79{
  80        struct nft_byteorder *priv = nft_expr_priv(expr);
  81        int err;
  82
  83        if (tb[NFTA_BYTEORDER_SREG] == NULL ||
  84            tb[NFTA_BYTEORDER_DREG] == NULL ||
  85            tb[NFTA_BYTEORDER_LEN] == NULL ||
  86            tb[NFTA_BYTEORDER_SIZE] == NULL ||
  87            tb[NFTA_BYTEORDER_OP] == NULL)
  88                return -EINVAL;
  89
  90        priv->sreg = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SREG]));
  91        err = nft_validate_input_register(priv->sreg);
  92        if (err < 0)
  93                return err;
  94
  95        priv->dreg = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_DREG]));
  96        err = nft_validate_output_register(priv->dreg);
  97        if (err < 0)
  98                return err;
  99        err = nft_validate_data_load(ctx, priv->dreg, NULL, NFT_DATA_VALUE);
 100        if (err < 0)
 101                return err;
 102
 103        priv->op = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_OP]));
 104        switch (priv->op) {
 105        case NFT_BYTEORDER_NTOH:
 106        case NFT_BYTEORDER_HTON:
 107                break;
 108        default:
 109                return -EINVAL;
 110        }
 111
 112        priv->len = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_LEN]));
 113        if (priv->len == 0 || priv->len > FIELD_SIZEOF(struct nft_data, data))
 114                return -EINVAL;
 115
 116        priv->size = ntohl(nla_get_be32(tb[NFTA_BYTEORDER_SIZE]));
 117        switch (priv->size) {
 118        case 2:
 119        case 4:
 120                break;
 121        default:
 122                return -EINVAL;
 123        }
 124
 125        return 0;
 126}
 127
 128static int nft_byteorder_dump(struct sk_buff *skb, const struct nft_expr *expr)
 129{
 130        const struct nft_byteorder *priv = nft_expr_priv(expr);
 131
 132        if (nla_put_be32(skb, NFTA_BYTEORDER_SREG, htonl(priv->sreg)))
 133                goto nla_put_failure;
 134        if (nla_put_be32(skb, NFTA_BYTEORDER_DREG, htonl(priv->dreg)))
 135                goto nla_put_failure;
 136        if (nla_put_be32(skb, NFTA_BYTEORDER_OP, htonl(priv->op)))
 137                goto nla_put_failure;
 138        if (nla_put_be32(skb, NFTA_BYTEORDER_LEN, htonl(priv->len)))
 139                goto nla_put_failure;
 140        if (nla_put_be32(skb, NFTA_BYTEORDER_SIZE, htonl(priv->size)))
 141                goto nla_put_failure;
 142        return 0;
 143
 144nla_put_failure:
 145        return -1;
 146}
 147
 148static struct nft_expr_type nft_byteorder_type;
 149static const struct nft_expr_ops nft_byteorder_ops = {
 150        .type           = &nft_byteorder_type,
 151        .size           = NFT_EXPR_SIZE(sizeof(struct nft_byteorder)),
 152        .eval           = nft_byteorder_eval,
 153        .init           = nft_byteorder_init,
 154        .dump           = nft_byteorder_dump,
 155};
 156
 157static struct nft_expr_type nft_byteorder_type __read_mostly = {
 158        .name           = "byteorder",
 159        .ops            = &nft_byteorder_ops,
 160        .policy         = nft_byteorder_policy,
 161        .maxattr        = NFTA_BYTEORDER_MAX,
 162        .owner          = THIS_MODULE,
 163};
 164
 165int __init nft_byteorder_module_init(void)
 166{
 167        return nft_register_expr(&nft_byteorder_type);
 168}
 169
 170void nft_byteorder_module_exit(void)
 171{
 172        nft_unregister_expr(&nft_byteorder_type);
 173}
 174