linux/net/netfilter/ipset/ip_set_hash_ipmark.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/* Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> */
   3
   4/* Kernel module implementing an IP set type: the hash:ip,mark type */
   5
   6#include <linux/jhash.h>
   7#include <linux/module.h>
   8#include <linux/ip.h>
   9#include <linux/skbuff.h>
  10#include <linux/errno.h>
  11#include <linux/random.h>
  12#include <net/ip.h>
  13#include <net/ipv6.h>
  14#include <net/netlink.h>
  15#include <net/tcp.h>
  16
  17#include <linux/netfilter.h>
  18#include <linux/netfilter/ipset/pfxlen.h>
  19#include <linux/netfilter/ipset/ip_set.h>
  20#include <linux/netfilter/ipset/ip_set_hash.h>
  21
  22#define IPSET_TYPE_REV_MIN      0
  23/*                              1          Forceadd support */
  24/*                              2          skbinfo support */
  25#define IPSET_TYPE_REV_MAX      3       /* bucketsize, initval support  */
  26
  27MODULE_LICENSE("GPL");
  28MODULE_AUTHOR("Vytas Dauksa <vytas.dauksa@smoothwall.net>");
  29IP_SET_MODULE_DESC("hash:ip,mark", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
  30MODULE_ALIAS("ip_set_hash:ip,mark");
  31
  32/* Type specific function prefix */
  33#define HTYPE           hash_ipmark
  34#define IP_SET_HASH_WITH_MARKMASK
  35
  36/* IPv4 variant */
  37
  38/* Member elements */
  39struct hash_ipmark4_elem {
  40        __be32 ip;
  41        __u32 mark;
  42};
  43
  44/* Common functions */
  45
  46static bool
  47hash_ipmark4_data_equal(const struct hash_ipmark4_elem *ip1,
  48                        const struct hash_ipmark4_elem *ip2,
  49                        u32 *multi)
  50{
  51        return ip1->ip == ip2->ip &&
  52               ip1->mark == ip2->mark;
  53}
  54
  55static bool
  56hash_ipmark4_data_list(struct sk_buff *skb,
  57                       const struct hash_ipmark4_elem *data)
  58{
  59        if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
  60            nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
  61                goto nla_put_failure;
  62        return false;
  63
  64nla_put_failure:
  65        return true;
  66}
  67
  68static void
  69hash_ipmark4_data_next(struct hash_ipmark4_elem *next,
  70                       const struct hash_ipmark4_elem *d)
  71{
  72        next->ip = d->ip;
  73}
  74
  75#define MTYPE           hash_ipmark4
  76#define HOST_MASK       32
  77#include "ip_set_hash_gen.h"
  78
  79static int
  80hash_ipmark4_kadt(struct ip_set *set, const struct sk_buff *skb,
  81                  const struct xt_action_param *par,
  82                  enum ipset_adt adt, struct ip_set_adt_opt *opt)
  83{
  84        const struct hash_ipmark4 *h = set->data;
  85        ipset_adtfn adtfn = set->variant->adt[adt];
  86        struct hash_ipmark4_elem e = { };
  87        struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
  88
  89        e.mark = skb->mark;
  90        e.mark &= h->markmask;
  91
  92        ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
  93        return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
  94}
  95
  96static int
  97hash_ipmark4_uadt(struct ip_set *set, struct nlattr *tb[],
  98                  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
  99{
 100        const struct hash_ipmark4 *h = set->data;
 101        ipset_adtfn adtfn = set->variant->adt[adt];
 102        struct hash_ipmark4_elem e = { };
 103        struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
 104        u32 ip, ip_to = 0;
 105        int ret;
 106
 107        if (tb[IPSET_ATTR_LINENO])
 108                *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
 109
 110        if (unlikely(!tb[IPSET_ATTR_IP] ||
 111                     !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
 112                return -IPSET_ERR_PROTOCOL;
 113
 114        ret = ip_set_get_ipaddr4(tb[IPSET_ATTR_IP], &e.ip);
 115        if (ret)
 116                return ret;
 117
 118        ret = ip_set_get_extensions(set, tb, &ext);
 119        if (ret)
 120                return ret;
 121
 122        e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
 123        e.mark &= h->markmask;
 124        if (e.mark == 0 && e.ip == 0)
 125                return -IPSET_ERR_HASH_ELEM;
 126
 127        if (adt == IPSET_TEST ||
 128            !(tb[IPSET_ATTR_IP_TO] || tb[IPSET_ATTR_CIDR])) {
 129                ret = adtfn(set, &e, &ext, &ext, flags);
 130                return ip_set_eexist(ret, flags) ? 0 : ret;
 131        }
 132
 133        ip_to = ip = ntohl(e.ip);
 134        if (tb[IPSET_ATTR_IP_TO]) {
 135                ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
 136                if (ret)
 137                        return ret;
 138                if (ip > ip_to) {
 139                        if (e.mark == 0 && ip_to == 0)
 140                                return -IPSET_ERR_HASH_ELEM;
 141                        swap(ip, ip_to);
 142                }
 143        } else if (tb[IPSET_ATTR_CIDR]) {
 144                u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
 145
 146                if (!cidr || cidr > HOST_MASK)
 147                        return -IPSET_ERR_INVALID_CIDR;
 148                ip_set_mask_from_to(ip, ip_to, cidr);
 149        }
 150
 151        if (((u64)ip_to - ip + 1) > IPSET_MAX_RANGE)
 152                return -ERANGE;
 153
 154        if (retried)
 155                ip = ntohl(h->next.ip);
 156        for (; ip <= ip_to; ip++) {
 157                e.ip = htonl(ip);
 158                ret = adtfn(set, &e, &ext, &ext, flags);
 159
 160                if (ret && !ip_set_eexist(ret, flags))
 161                        return ret;
 162
 163                ret = 0;
 164        }
 165        return ret;
 166}
 167
 168/* IPv6 variant */
 169
 170struct hash_ipmark6_elem {
 171        union nf_inet_addr ip;
 172        __u32 mark;
 173};
 174
 175/* Common functions */
 176
 177static bool
 178hash_ipmark6_data_equal(const struct hash_ipmark6_elem *ip1,
 179                        const struct hash_ipmark6_elem *ip2,
 180                        u32 *multi)
 181{
 182        return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
 183               ip1->mark == ip2->mark;
 184}
 185
 186static bool
 187hash_ipmark6_data_list(struct sk_buff *skb,
 188                       const struct hash_ipmark6_elem *data)
 189{
 190        if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
 191            nla_put_net32(skb, IPSET_ATTR_MARK, htonl(data->mark)))
 192                goto nla_put_failure;
 193        return false;
 194
 195nla_put_failure:
 196        return true;
 197}
 198
 199static void
 200hash_ipmark6_data_next(struct hash_ipmark6_elem *next,
 201                       const struct hash_ipmark6_elem *d)
 202{
 203}
 204
 205#undef MTYPE
 206#undef HOST_MASK
 207
 208#define MTYPE           hash_ipmark6
 209#define HOST_MASK       128
 210#define IP_SET_EMIT_CREATE
 211#include "ip_set_hash_gen.h"
 212
 213static int
 214hash_ipmark6_kadt(struct ip_set *set, const struct sk_buff *skb,
 215                  const struct xt_action_param *par,
 216                  enum ipset_adt adt, struct ip_set_adt_opt *opt)
 217{
 218        const struct hash_ipmark6 *h = set->data;
 219        ipset_adtfn adtfn = set->variant->adt[adt];
 220        struct hash_ipmark6_elem e = { };
 221        struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
 222
 223        e.mark = skb->mark;
 224        e.mark &= h->markmask;
 225
 226        ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
 227        return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
 228}
 229
 230static int
 231hash_ipmark6_uadt(struct ip_set *set, struct nlattr *tb[],
 232                  enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
 233{
 234        const struct hash_ipmark6 *h = set->data;
 235        ipset_adtfn adtfn = set->variant->adt[adt];
 236        struct hash_ipmark6_elem e = { };
 237        struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
 238        int ret;
 239
 240        if (tb[IPSET_ATTR_LINENO])
 241                *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
 242
 243        if (unlikely(!tb[IPSET_ATTR_IP] ||
 244                     !ip_set_attr_netorder(tb, IPSET_ATTR_MARK)))
 245                return -IPSET_ERR_PROTOCOL;
 246        if (unlikely(tb[IPSET_ATTR_IP_TO]))
 247                return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
 248        if (unlikely(tb[IPSET_ATTR_CIDR])) {
 249                u8 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
 250
 251                if (cidr != HOST_MASK)
 252                        return -IPSET_ERR_INVALID_CIDR;
 253        }
 254
 255        ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
 256        if (ret)
 257                return ret;
 258
 259        ret = ip_set_get_extensions(set, tb, &ext);
 260        if (ret)
 261                return ret;
 262
 263        e.mark = ntohl(nla_get_be32(tb[IPSET_ATTR_MARK]));
 264        e.mark &= h->markmask;
 265
 266        if (adt == IPSET_TEST) {
 267                ret = adtfn(set, &e, &ext, &ext, flags);
 268                return ip_set_eexist(ret, flags) ? 0 : ret;
 269        }
 270
 271        ret = adtfn(set, &e, &ext, &ext, flags);
 272        if (ret && !ip_set_eexist(ret, flags))
 273                return ret;
 274
 275        return 0;
 276}
 277
 278static struct ip_set_type hash_ipmark_type __read_mostly = {
 279        .name           = "hash:ip,mark",
 280        .protocol       = IPSET_PROTOCOL,
 281        .features       = IPSET_TYPE_IP | IPSET_TYPE_MARK,
 282        .dimension      = IPSET_DIM_TWO,
 283        .family         = NFPROTO_UNSPEC,
 284        .revision_min   = IPSET_TYPE_REV_MIN,
 285        .revision_max   = IPSET_TYPE_REV_MAX,
 286        .create_flags[IPSET_TYPE_REV_MAX] = IPSET_CREATE_FLAG_BUCKETSIZE,
 287        .create         = hash_ipmark_create,
 288        .create_policy  = {
 289                [IPSET_ATTR_MARKMASK]   = { .type = NLA_U32 },
 290                [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
 291                [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
 292                [IPSET_ATTR_INITVAL]    = { .type = NLA_U32 },
 293                [IPSET_ATTR_BUCKETSIZE] = { .type = NLA_U8 },
 294                [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
 295                [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
 296                [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
 297        },
 298        .adt_policy     = {
 299                [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
 300                [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
 301                [IPSET_ATTR_MARK]       = { .type = NLA_U32 },
 302                [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
 303                [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
 304                [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
 305                [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
 306                [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
 307                [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING,
 308                                            .len  = IPSET_MAX_COMMENT_SIZE },
 309                [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
 310                [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
 311                [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
 312        },
 313        .me             = THIS_MODULE,
 314};
 315
 316static int __init
 317hash_ipmark_init(void)
 318{
 319        return ip_set_type_register(&hash_ipmark_type);
 320}
 321
 322static void __exit
 323hash_ipmark_fini(void)
 324{
 325        rcu_barrier();
 326        ip_set_type_unregister(&hash_ipmark_type);
 327}
 328
 329module_init(hash_ipmark_init);
 330module_exit(hash_ipmark_fini);
 331