~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/netfilter/ipset/ip_set_bitmap_ip.c

Version: ~ [ linux-5.15-rc6 ] ~ [ linux-5.14.14 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.75 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.155 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.213 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.252 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.287 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.289 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp