Re: [PATCH nf-next v4 00/10] netfilter: nft_bitwise: shift support

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 2020-01-16, at 12:22:47 +0100, Pablo Neira Ayuso wrote:
> On Thu, Jan 16, 2020 at 08:51:33AM +0000, Jeremy Sowden wrote:
> > On 2020-01-15, at 21:32:06 +0000, Jeremy Sowden wrote:
> > > The connmark xtables extension supports bit-shifts.  Add support
> > > for shifts to nft_bitwise in order to allow nftables to do
> > > likewise, e.g.:
> > >
> > >   nft add rule t c oif lo ct mark set meta mark << 8 | 0xab
> > >   nft add rule t c iif lo meta mark & 0xff 0xab ct mark set meta mark >> 8
> > >
> > > Changes since v3:
> > >
> > >   * the length of shift values sent by nft may be less than
> > >   sizeof(u32).
> >
> > Actually, having thought about this some more, I believe I had it
> > right in v3.  The difference between v3 and v4 is this:
> >
> >   @@ -146,7 +146,7 @@ static int nft_bitwise_init_shift(struct nft_bitwise *priv,
> >                               tb[NFTA_BITWISE_DATA]);
> >           if (err < 0)
> >                   return err;
> >   -       if (d.type != NFT_DATA_VALUE || d.len != sizeof(u32) ||
> >   +       if (d.type != NFT_DATA_VALUE || d.len > sizeof(u32) ||
> >               priv->data.data[0] >= BITS_PER_TYPE(u32)) {
>
> Why restrict this to 32-bits?

Because of how I implemented the shifts.  Here's the current rshift:

  static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src,
                                      const struct nft_bitwise *priv)
  {
          u32 shift = priv->data.data[0];
          unsigned int i;
          u32 carry = 0;

          for (i = 0; i < DIV_ROUND_UP(priv->len, sizeof(u32)); i++) {
                  dst[i] = carry | (src[i] >> shift);
                  carry = src[i] << (BITS_PER_TYPE(u32) - shift);
          }
  }

In order to support larger shifts, it would need to look something
like:

  static void nft_bitwise_eval_rshift(u32 *dst, const u32 *src,
                                      const struct nft_bitwise *priv)
  {
          unsigned len = DIV_ROUND_UP(priv->len, sizeof(u32));
          unsigned int d = shift / BITS_PER_TYPE(u32), s = 0;
          u32 shift = priv->data.data[0];
          u32 carry = 0;

          if (d > 0) {
                  memset(dst, '\0', d * sizeof(*dst));
                  shift %= BITS_PER_TYPE(u32);
          }

          for (s = 0; d < len; d++, s++) {
                  dst[d] = carry | (src[s] >> shift);
                  carry = src[s] << (BITS_PER_TYPE(u32) - shift);
          }
  }

J.

Attachment: signature.asc
Description: PGP signature


[Index of Archives]     [Netfitler Users]     [Berkeley Packet Filter]     [LARTC]     [Bugtraq]     [Yosemite Forum]

  Powered by Linux