| 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* Copyright (C) 2000-2002 Joakim Axelsson <gozem@linux.nu> |
| 3 | * Patrick Schaaf <bof@bof.de> |
| 4 | * Copyright (C) 2003-2013 Jozsef Kadlecsik <kadlec@netfilter.org> |
| 5 | */ |
| 6 | |
| 7 | /* Kernel module implementing an IP set type: the bitmap:ip type */ |
| 8 | |
| 9 | #include <linux/module.h> |
| 10 | #include <linux/ip.h> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/errno.h> |
| 13 | #include <linux/bitops.h> |
| 14 | #include <linux/spinlock.h> |
| 15 | #include <linux/netlink.h> |
| 16 | #include <linux/jiffies.h> |
| 17 | #include <linux/timer.h> |
| 18 | #include <net/netlink.h> |
| 19 | #include <net/tcp.h> |
| 20 | |
| 21 | #include <linux/netfilter/ipset/pfxlen.h> |
| 22 | #include <linux/netfilter/ipset/ip_set.h> |
| 23 | #include <linux/netfilter/ipset/ip_set_bitmap.h> |
| 24 | |
| 25 | #define IPSET_TYPE_REV_MIN 0 |
| 26 | /* 1 Counter support added */ |
| 27 | /* 2 Comment support added */ |
| 28 | #define IPSET_TYPE_REV_MAX 3 /* skbinfo support added */ |
| 29 | |
| 30 | MODULE_LICENSE("GPL" ); |
| 31 | MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@netfilter.org>" ); |
| 32 | IP_SET_MODULE_DESC("bitmap:ip" , IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX); |
| 33 | MODULE_ALIAS("ip_set_bitmap:ip" ); |
| 34 | |
| 35 | #define MTYPE bitmap_ip |
| 36 | #define HOST_MASK 32 |
| 37 | |
| 38 | /* Type structure */ |
| 39 | struct bitmap_ip { |
| 40 | unsigned long *members; /* the set members */ |
| 41 | u32 first_ip; /* host byte order, included in range */ |
| 42 | u32 last_ip; /* host byte order, included in range */ |
| 43 | u32 elements; /* number of max elements in the set */ |
| 44 | u32 hosts; /* number of hosts in a subnet */ |
| 45 | size_t memsize; /* members size */ |
| 46 | u8 netmask; /* subnet netmask */ |
| 47 | struct timer_list gc; /* garbage collection */ |
| 48 | struct ip_set *set; /* attached to this ip_set */ |
| 49 | unsigned char extensions[] /* data extensions */ |
| 50 | __aligned(__alignof__(u64)); |
| 51 | }; |
| 52 | |
| 53 | /* ADT structure for generic function args */ |
| 54 | struct bitmap_ip_adt_elem { |
| 55 | u16 id; |
| 56 | }; |
| 57 | |
| 58 | static u32 |
| 59 | ip_to_id(const struct bitmap_ip *m, u32 ip) |
| 60 | { |
| 61 | return ((ip & ip_set_hostmask(pfxlen: m->netmask)) - m->first_ip) / m->hosts; |
| 62 | } |
| 63 | |
| 64 | /* Common functions */ |
| 65 | |
| 66 | static int |
| 67 | bitmap_ip_do_test(const struct bitmap_ip_adt_elem *e, |
| 68 | struct bitmap_ip *map, size_t dsize) |
| 69 | { |
| 70 | return !!test_bit(e->id, map->members); |
| 71 | } |
| 72 | |
| 73 | static int |
| 74 | bitmap_ip_gc_test(u16 id, const struct bitmap_ip *map, size_t dsize) |
| 75 | { |
| 76 | return !!test_bit(id, map->members); |
| 77 | } |
| 78 | |
| 79 | static int |
| 80 | bitmap_ip_do_add(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map, |
| 81 | u32 flags, size_t dsize) |
| 82 | { |
| 83 | return !!test_bit(e->id, map->members); |
| 84 | } |
| 85 | |
| 86 | static int |
| 87 | bitmap_ip_do_del(const struct bitmap_ip_adt_elem *e, struct bitmap_ip *map) |
| 88 | { |
| 89 | return !test_and_clear_bit(nr: e->id, addr: map->members); |
| 90 | } |
| 91 | |
| 92 | static int |
| 93 | bitmap_ip_do_list(struct sk_buff *skb, const struct bitmap_ip *map, u32 id, |
| 94 | size_t dsize) |
| 95 | { |
| 96 | return nla_put_ipaddr4(skb, type: IPSET_ATTR_IP, |
| 97 | htonl(map->first_ip + id * map->hosts)); |
| 98 | } |
| 99 | |
| 100 | static int |
| 101 | bitmap_ip_do_head(struct sk_buff *skb, const struct bitmap_ip *map) |
| 102 | { |
| 103 | return nla_put_ipaddr4(skb, type: IPSET_ATTR_IP, htonl(map->first_ip)) || |
| 104 | nla_put_ipaddr4(skb, type: IPSET_ATTR_IP_TO, htonl(map->last_ip)) || |
| 105 | (map->netmask != 32 && |
| 106 | nla_put_u8(skb, attrtype: IPSET_ATTR_NETMASK, value: map->netmask)); |
| 107 | } |
| 108 | |
| 109 | static int |
| 110 | bitmap_ip_kadt(struct ip_set *set, const struct sk_buff *skb, |
| 111 | const struct xt_action_param *par, |
| 112 | enum ipset_adt adt, struct ip_set_adt_opt *opt) |
| 113 | { |
| 114 | struct bitmap_ip *map = set->data; |
| 115 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 116 | struct bitmap_ip_adt_elem e = { .id = 0 }; |
| 117 | struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set); |
| 118 | u32 ip; |
| 119 | |
| 120 | ip = ntohl(ip4addr(skb, opt->flags & IPSET_DIM_ONE_SRC)); |
| 121 | if (ip < map->first_ip || ip > map->last_ip) |
| 122 | return -IPSET_ERR_BITMAP_RANGE; |
| 123 | |
| 124 | e.id = ip_to_id(m: map, ip); |
| 125 | |
| 126 | return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags); |
| 127 | } |
| 128 | |
| 129 | static int |
| 130 | bitmap_ip_uadt(struct ip_set *set, struct nlattr *tb[], |
| 131 | enum ipset_adt adt, u32 *lineno, u32 flags, bool retried) |
| 132 | { |
| 133 | struct bitmap_ip *map = set->data; |
| 134 | ipset_adtfn adtfn = set->variant->adt[adt]; |
| 135 | u32 ip = 0, ip_to = 0; |
| 136 | struct bitmap_ip_adt_elem e = { .id = 0 }; |
| 137 | struct ip_set_ext ext = IP_SET_INIT_UEXT(set); |
| 138 | int ret = 0; |
| 139 | |
| 140 | if (tb[IPSET_ATTR_LINENO]) |
| 141 | *lineno = nla_get_u32(nla: tb[IPSET_ATTR_LINENO]); |
| 142 | |
| 143 | if (unlikely(!tb[IPSET_ATTR_IP])) |
| 144 | return -IPSET_ERR_PROTOCOL; |
| 145 | |
| 146 | ret = ip_set_get_hostipaddr4(nla: tb[IPSET_ATTR_IP], ipaddr: &ip); |
| 147 | if (ret) |
| 148 | return ret; |
| 149 | |
| 150 | ret = ip_set_get_extensions(set, tb, ext: &ext); |
| 151 | if (ret) |
| 152 | return ret; |
| 153 | |
| 154 | if (ip < map->first_ip || ip > map->last_ip) |
| 155 | return -IPSET_ERR_BITMAP_RANGE; |
| 156 | |
| 157 | if (adt == IPSET_TEST) { |
| 158 | e.id = ip_to_id(m: map, ip); |
| 159 | return adtfn(set, &e, &ext, &ext, flags); |
| 160 | } |
| 161 | |
| 162 | if (tb[IPSET_ATTR_IP_TO]) { |
| 163 | ret = ip_set_get_hostipaddr4(nla: tb[IPSET_ATTR_IP_TO], ipaddr: &ip_to); |
| 164 | if (ret) |
| 165 | return ret; |
| 166 | if (ip > ip_to) |
| 167 | swap(ip, ip_to); |
| 168 | } else if (tb[IPSET_ATTR_CIDR]) { |
| 169 | u8 cidr = nla_get_u8(nla: tb[IPSET_ATTR_CIDR]); |
| 170 | |
| 171 | if (!cidr || cidr > HOST_MASK) |
| 172 | return -IPSET_ERR_INVALID_CIDR; |
| 173 | ip_set_mask_from_to(ip, ip_to, cidr); |
| 174 | } else { |
| 175 | ip_to = ip; |
| 176 | } |
| 177 | |
| 178 | if (ip < map->first_ip || ip_to > map->last_ip) |
| 179 | return -IPSET_ERR_BITMAP_RANGE; |
| 180 | |
| 181 | for (; !before(seq1: ip_to, seq2: ip); ip += map->hosts) { |
| 182 | e.id = ip_to_id(m: map, ip); |
| 183 | ret = adtfn(set, &e, &ext, &ext, flags); |
| 184 | |
| 185 | if (ret && !ip_set_eexist(ret, flags)) |
| 186 | return ret; |
| 187 | |
| 188 | ret = 0; |
| 189 | } |
| 190 | return ret; |
| 191 | } |
| 192 | |
| 193 | static bool |
| 194 | bitmap_ip_same_set(const struct ip_set *a, const struct ip_set *b) |
| 195 | { |
| 196 | const struct bitmap_ip *x = a->data; |
| 197 | const struct bitmap_ip *y = b->data; |
| 198 | |
| 199 | return x->first_ip == y->first_ip && |
| 200 | x->last_ip == y->last_ip && |
| 201 | x->netmask == y->netmask && |
| 202 | a->timeout == b->timeout && |
| 203 | a->extensions == b->extensions; |
| 204 | } |
| 205 | |
| 206 | /* Plain variant */ |
| 207 | |
| 208 | struct bitmap_ip_elem { |
| 209 | }; |
| 210 | |
| 211 | #include "ip_set_bitmap_gen.h" |
| 212 | |
| 213 | /* Create bitmap:ip type of sets */ |
| 214 | |
| 215 | static bool |
| 216 | init_map_ip(struct ip_set *set, struct bitmap_ip *map, |
| 217 | u32 first_ip, u32 last_ip, |
| 218 | u32 elements, u32 hosts, u8 netmask) |
| 219 | { |
| 220 | map->members = bitmap_zalloc(nbits: elements, GFP_KERNEL | __GFP_NOWARN); |
| 221 | if (!map->members) |
| 222 | return false; |
| 223 | map->first_ip = first_ip; |
| 224 | map->last_ip = last_ip; |
| 225 | map->elements = elements; |
| 226 | map->hosts = hosts; |
| 227 | map->netmask = netmask; |
| 228 | set->timeout = IPSET_NO_TIMEOUT; |
| 229 | |
| 230 | map->set = set; |
| 231 | set->data = map; |
| 232 | set->family = NFPROTO_IPV4; |
| 233 | |
| 234 | return true; |
| 235 | } |
| 236 | |
| 237 | static u32 |
| 238 | range_to_mask(u32 from, u32 to, u8 *bits) |
| 239 | { |
| 240 | u32 mask = 0xFFFFFFFE; |
| 241 | |
| 242 | *bits = 32; |
| 243 | while (--(*bits) > 0 && mask && (to & mask) != from) |
| 244 | mask <<= 1; |
| 245 | |
| 246 | return mask; |
| 247 | } |
| 248 | |
| 249 | static int |
| 250 | bitmap_ip_create(struct net *net, struct ip_set *set, struct nlattr *tb[], |
| 251 | u32 flags) |
| 252 | { |
| 253 | struct bitmap_ip *map; |
| 254 | u32 first_ip = 0, last_ip = 0, hosts; |
| 255 | u64 elements; |
| 256 | u8 netmask = 32; |
| 257 | int ret; |
| 258 | |
| 259 | if (unlikely(!tb[IPSET_ATTR_IP] || |
| 260 | !ip_set_optattr_netorder(tb, IPSET_ATTR_TIMEOUT) || |
| 261 | !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS))) |
| 262 | return -IPSET_ERR_PROTOCOL; |
| 263 | |
| 264 | ret = ip_set_get_hostipaddr4(nla: tb[IPSET_ATTR_IP], ipaddr: &first_ip); |
| 265 | if (ret) |
| 266 | return ret; |
| 267 | |
| 268 | if (tb[IPSET_ATTR_IP_TO]) { |
| 269 | ret = ip_set_get_hostipaddr4(nla: tb[IPSET_ATTR_IP_TO], ipaddr: &last_ip); |
| 270 | if (ret) |
| 271 | return ret; |
| 272 | if (first_ip > last_ip) |
| 273 | swap(first_ip, last_ip); |
| 274 | } else if (tb[IPSET_ATTR_CIDR]) { |
| 275 | u8 cidr = nla_get_u8(nla: tb[IPSET_ATTR_CIDR]); |
| 276 | |
| 277 | if (cidr >= HOST_MASK) |
| 278 | return -IPSET_ERR_INVALID_CIDR; |
| 279 | ip_set_mask_from_to(first_ip, last_ip, cidr); |
| 280 | } else { |
| 281 | return -IPSET_ERR_PROTOCOL; |
| 282 | } |
| 283 | |
| 284 | if (tb[IPSET_ATTR_NETMASK]) { |
| 285 | netmask = nla_get_u8(nla: tb[IPSET_ATTR_NETMASK]); |
| 286 | |
| 287 | if (netmask > HOST_MASK) |
| 288 | return -IPSET_ERR_INVALID_NETMASK; |
| 289 | |
| 290 | first_ip &= ip_set_hostmask(pfxlen: netmask); |
| 291 | last_ip |= ~ip_set_hostmask(pfxlen: netmask); |
| 292 | } |
| 293 | |
| 294 | if (netmask == 32) { |
| 295 | hosts = 1; |
| 296 | elements = (u64)last_ip - first_ip + 1; |
| 297 | } else { |
| 298 | u8 mask_bits; |
| 299 | u32 mask; |
| 300 | |
| 301 | mask = range_to_mask(from: first_ip, to: last_ip, bits: &mask_bits); |
| 302 | |
| 303 | if ((!mask && (first_ip || last_ip != 0xFFFFFFFF)) || |
| 304 | netmask <= mask_bits) |
| 305 | return -IPSET_ERR_BITMAP_RANGE; |
| 306 | |
| 307 | pr_debug("mask_bits %u, netmask %u\n" , mask_bits, netmask); |
| 308 | hosts = 2U << (32 - netmask - 1); |
| 309 | elements = 2UL << (netmask - mask_bits - 1); |
| 310 | } |
| 311 | if (elements > IPSET_BITMAP_MAX_RANGE + 1) |
| 312 | return -IPSET_ERR_BITMAP_RANGE_SIZE; |
| 313 | |
| 314 | pr_debug("hosts %u, elements %llu\n" , |
| 315 | hosts, (unsigned long long)elements); |
| 316 | |
| 317 | set->dsize = ip_set_elem_len(set, tb, len: 0, align: 0); |
| 318 | map = ip_set_alloc(size: sizeof(*map) + elements * set->dsize); |
| 319 | if (!map) |
| 320 | return -ENOMEM; |
| 321 | |
| 322 | map->memsize = BITS_TO_LONGS(elements) * sizeof(unsigned long); |
| 323 | set->variant = &bitmap_ip; |
| 324 | if (!init_map_ip(set, map, first_ip, last_ip, |
| 325 | elements, hosts, netmask)) { |
| 326 | ip_set_free(members: map); |
| 327 | return -ENOMEM; |
| 328 | } |
| 329 | if (tb[IPSET_ATTR_TIMEOUT]) { |
| 330 | set->timeout = ip_set_timeout_uget(tb: tb[IPSET_ATTR_TIMEOUT]); |
| 331 | bitmap_ip_gc_init(set, gc: bitmap_ip_gc); |
| 332 | } |
| 333 | return 0; |
| 334 | } |
| 335 | |
| 336 | static struct ip_set_type bitmap_ip_type __read_mostly = { |
| 337 | .name = "bitmap:ip" , |
| 338 | .protocol = IPSET_PROTOCOL, |
| 339 | .features = IPSET_TYPE_IP, |
| 340 | .dimension = IPSET_DIM_ONE, |
| 341 | .family = NFPROTO_IPV4, |
| 342 | .revision_min = IPSET_TYPE_REV_MIN, |
| 343 | .revision_max = IPSET_TYPE_REV_MAX, |
| 344 | .create = bitmap_ip_create, |
| 345 | .create_policy = { |
| 346 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, |
| 347 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, |
| 348 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, |
| 349 | [IPSET_ATTR_NETMASK] = { .type = NLA_U8 }, |
| 350 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 351 | [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 }, |
| 352 | }, |
| 353 | .adt_policy = { |
| 354 | [IPSET_ATTR_IP] = { .type = NLA_NESTED }, |
| 355 | [IPSET_ATTR_IP_TO] = { .type = NLA_NESTED }, |
| 356 | [IPSET_ATTR_CIDR] = { .type = NLA_U8 }, |
| 357 | [IPSET_ATTR_TIMEOUT] = { .type = NLA_U32 }, |
| 358 | [IPSET_ATTR_LINENO] = { .type = NLA_U32 }, |
| 359 | [IPSET_ATTR_BYTES] = { .type = NLA_U64 }, |
| 360 | [IPSET_ATTR_PACKETS] = { .type = NLA_U64 }, |
| 361 | [IPSET_ATTR_COMMENT] = { .type = NLA_NUL_STRING, |
| 362 | .len = IPSET_MAX_COMMENT_SIZE }, |
| 363 | [IPSET_ATTR_SKBMARK] = { .type = NLA_U64 }, |
| 364 | [IPSET_ATTR_SKBPRIO] = { .type = NLA_U32 }, |
| 365 | [IPSET_ATTR_SKBQUEUE] = { .type = NLA_U16 }, |
| 366 | }, |
| 367 | .me = THIS_MODULE, |
| 368 | }; |
| 369 | |
| 370 | static int __init |
| 371 | bitmap_ip_init(void) |
| 372 | { |
| 373 | return ip_set_type_register(set_type: &bitmap_ip_type); |
| 374 | } |
| 375 | |
| 376 | static void __exit |
| 377 | bitmap_ip_fini(void) |
| 378 | { |
| 379 | rcu_barrier(); |
| 380 | ip_set_type_unregister(set_type: &bitmap_ip_type); |
| 381 | } |
| 382 | |
| 383 | module_init(bitmap_ip_init); |
| 384 | module_exit(bitmap_ip_fini); |
| 385 | |