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

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

Version: ~ [ linux-5.15-rc1 ] ~ [ linux-5.14.5 ] ~ [ linux-5.13.18 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.66 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.147 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.206 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.246 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.282 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.283 ] ~ [ 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) 2003-2013 Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>
  2  *
  3  * This program is free software; you can redistribute it and/or modify
  4  * it under the terms of the GNU General Public License version 2 as
  5  * published by the Free Software Foundation.
  6  */
  7 
  8 /* Kernel module implementing an IP set type: the hash:net,port type */
  9 
 10 #include <linux/jhash.h>
 11 #include <linux/module.h>
 12 #include <linux/ip.h>
 13 #include <linux/skbuff.h>
 14 #include <linux/errno.h>
 15 #include <linux/random.h>
 16 #include <net/ip.h>
 17 #include <net/ipv6.h>
 18 #include <net/netlink.h>
 19 
 20 #include <linux/netfilter.h>
 21 #include <linux/netfilter/ipset/pfxlen.h>
 22 #include <linux/netfilter/ipset/ip_set.h>
 23 #include <linux/netfilter/ipset/ip_set_getport.h>
 24 #include <linux/netfilter/ipset/ip_set_hash.h>
 25 
 26 #define IPSET_TYPE_REV_MIN      0
 27 /*                              1    SCTP and UDPLITE support added */
 28 /*                              2    Range as input support for IPv4 added */
 29 /*                              3    nomatch flag support added */
 30 /*                              4    Counters support added */
 31 /*                              5    Comments support added */
 32 /*                              6    Forceadd support added */
 33 #define IPSET_TYPE_REV_MAX      7 /* skbinfo support added */
 34 
 35 MODULE_LICENSE("GPL");
 36 MODULE_AUTHOR("Jozsef Kadlecsik <kadlec@blackhole.kfki.hu>");
 37 IP_SET_MODULE_DESC("hash:net,port", IPSET_TYPE_REV_MIN, IPSET_TYPE_REV_MAX);
 38 MODULE_ALIAS("ip_set_hash:net,port");
 39 
 40 /* Type specific function prefix */
 41 #define HTYPE           hash_netport
 42 #define IP_SET_HASH_WITH_PROTO
 43 #define IP_SET_HASH_WITH_NETS
 44 
 45 /* We squeeze the "nomatch" flag into cidr: we don't support cidr == 0
 46  * However this way we have to store internally cidr - 1,
 47  * dancing back and forth.
 48  */
 49 #define IP_SET_HASH_WITH_NETS_PACKED
 50 
 51 /* IPv4 variant */
 52 
 53 /* Member elements */
 54 struct hash_netport4_elem {
 55         __be32 ip;
 56         __be16 port;
 57         u8 proto;
 58         u8 cidr:7;
 59         u8 nomatch:1;
 60 };
 61 
 62 /* Common functions */
 63 
 64 static inline bool
 65 hash_netport4_data_equal(const struct hash_netport4_elem *ip1,
 66                          const struct hash_netport4_elem *ip2,
 67                          u32 *multi)
 68 {
 69         return ip1->ip == ip2->ip &&
 70                ip1->port == ip2->port &&
 71                ip1->proto == ip2->proto &&
 72                ip1->cidr == ip2->cidr;
 73 }
 74 
 75 static inline int
 76 hash_netport4_do_data_match(const struct hash_netport4_elem *elem)
 77 {
 78         return elem->nomatch ? -ENOTEMPTY : 1;
 79 }
 80 
 81 static inline void
 82 hash_netport4_data_set_flags(struct hash_netport4_elem *elem, u32 flags)
 83 {
 84         elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
 85 }
 86 
 87 static inline void
 88 hash_netport4_data_reset_flags(struct hash_netport4_elem *elem, u8 *flags)
 89 {
 90         swap(*flags, elem->nomatch);
 91 }
 92 
 93 static inline void
 94 hash_netport4_data_netmask(struct hash_netport4_elem *elem, u8 cidr)
 95 {
 96         elem->ip &= ip_set_netmask(cidr);
 97         elem->cidr = cidr - 1;
 98 }
 99 
100 static bool
101 hash_netport4_data_list(struct sk_buff *skb,
102                         const struct hash_netport4_elem *data)
103 {
104         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
105 
106         if (nla_put_ipaddr4(skb, IPSET_ATTR_IP, data->ip) ||
107             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
108             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
109             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
110             (flags &&
111              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
112                 goto nla_put_failure;
113         return false;
114 
115 nla_put_failure:
116         return true;
117 }
118 
119 static inline void
120 hash_netport4_data_next(struct hash_netport4_elem *next,
121                         const struct hash_netport4_elem *d)
122 {
123         next->ip = d->ip;
124         next->port = d->port;
125 }
126 
127 #define MTYPE           hash_netport4
128 #define HOST_MASK       32
129 #include "ip_set_hash_gen.h"
130 
131 static int
132 hash_netport4_kadt(struct ip_set *set, const struct sk_buff *skb,
133                    const struct xt_action_param *par,
134                    enum ipset_adt adt, struct ip_set_adt_opt *opt)
135 {
136         const struct hash_netport4 *h = set->data;
137         ipset_adtfn adtfn = set->variant->adt[adt];
138         struct hash_netport4_elem e = {
139                 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
140         };
141         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
142 
143         if (adt == IPSET_TEST)
144                 e.cidr = HOST_MASK - 1;
145 
146         if (!ip_set_get_ip4_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
147                                  &e.port, &e.proto))
148                 return -EINVAL;
149 
150         ip4addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip);
151         e.ip &= ip_set_netmask(e.cidr + 1);
152 
153         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
154 }
155 
156 static int
157 hash_netport4_uadt(struct ip_set *set, struct nlattr *tb[],
158                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
159 {
160         const struct hash_netport4 *h = set->data;
161         ipset_adtfn adtfn = set->variant->adt[adt];
162         struct hash_netport4_elem e = { .cidr = HOST_MASK - 1 };
163         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
164         u32 port, port_to, p = 0, ip = 0, ip_to = 0;
165         bool with_ports = false;
166         u8 cidr;
167         int ret;
168 
169         if (tb[IPSET_ATTR_LINENO])
170                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
171 
172         if (unlikely(!tb[IPSET_ATTR_IP] ||
173                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
174                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
175                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
176                 return -IPSET_ERR_PROTOCOL;
177 
178         ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP], &ip);
179         if (ret)
180                 return ret;
181 
182         ret = ip_set_get_extensions(set, tb, &ext);
183         if (ret)
184                 return ret;
185 
186         if (tb[IPSET_ATTR_CIDR]) {
187                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
188                 if (!cidr || cidr > HOST_MASK)
189                         return -IPSET_ERR_INVALID_CIDR;
190                 e.cidr = cidr - 1;
191         }
192 
193         e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
194 
195         if (tb[IPSET_ATTR_PROTO]) {
196                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
197                 with_ports = ip_set_proto_with_ports(e.proto);
198 
199                 if (e.proto == 0)
200                         return -IPSET_ERR_INVALID_PROTO;
201         } else {
202                 return -IPSET_ERR_MISSING_PROTO;
203         }
204 
205         if (!(with_ports || e.proto == IPPROTO_ICMP))
206                 e.port = 0;
207 
208         with_ports = with_ports && tb[IPSET_ATTR_PORT_TO];
209 
210         if (tb[IPSET_ATTR_CADT_FLAGS]) {
211                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
212 
213                 if (cadt_flags & IPSET_FLAG_NOMATCH)
214                         flags |= (IPSET_FLAG_NOMATCH << 16);
215         }
216 
217         if (adt == IPSET_TEST || !(with_ports || tb[IPSET_ATTR_IP_TO])) {
218                 e.ip = htonl(ip & ip_set_hostmask(e.cidr + 1));
219                 ret = adtfn(set, &e, &ext, &ext, flags);
220                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
221                        ip_set_eexist(ret, flags) ? 0 : ret;
222         }
223 
224         port = port_to = ntohs(e.port);
225         if (tb[IPSET_ATTR_PORT_TO]) {
226                 port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
227                 if (port_to < port)
228                         swap(port, port_to);
229         }
230         if (tb[IPSET_ATTR_IP_TO]) {
231                 ret = ip_set_get_hostipaddr4(tb[IPSET_ATTR_IP_TO], &ip_to);
232                 if (ret)
233                         return ret;
234                 if (ip_to < ip)
235                         swap(ip, ip_to);
236                 if (ip + UINT_MAX == ip_to)
237                         return -IPSET_ERR_HASH_RANGE;
238         } else {
239                 ip_set_mask_from_to(ip, ip_to, e.cidr + 1);
240         }
241 
242         if (retried) {
243                 ip = ntohl(h->next.ip);
244                 p = ntohs(h->next.port);
245         } else {
246                 p = port;
247         }
248         do {
249                 e.ip = htonl(ip);
250                 ip = ip_set_range_to_cidr(ip, ip_to, &cidr);
251                 e.cidr = cidr - 1;
252                 for (; p <= port_to; p++) {
253                         e.port = htons(p);
254                         ret = adtfn(set, &e, &ext, &ext, flags);
255                         if (ret && !ip_set_eexist(ret, flags))
256                                 return ret;
257 
258                         ret = 0;
259                 }
260                 p = port;
261         } while (ip++ < ip_to);
262         return ret;
263 }
264 
265 /* IPv6 variant */
266 
267 struct hash_netport6_elem {
268         union nf_inet_addr ip;
269         __be16 port;
270         u8 proto;
271         u8 cidr:7;
272         u8 nomatch:1;
273 };
274 
275 /* Common functions */
276 
277 static inline bool
278 hash_netport6_data_equal(const struct hash_netport6_elem *ip1,
279                          const struct hash_netport6_elem *ip2,
280                          u32 *multi)
281 {
282         return ipv6_addr_equal(&ip1->ip.in6, &ip2->ip.in6) &&
283                ip1->port == ip2->port &&
284                ip1->proto == ip2->proto &&
285                ip1->cidr == ip2->cidr;
286 }
287 
288 static inline int
289 hash_netport6_do_data_match(const struct hash_netport6_elem *elem)
290 {
291         return elem->nomatch ? -ENOTEMPTY : 1;
292 }
293 
294 static inline void
295 hash_netport6_data_set_flags(struct hash_netport6_elem *elem, u32 flags)
296 {
297         elem->nomatch = !!((flags >> 16) & IPSET_FLAG_NOMATCH);
298 }
299 
300 static inline void
301 hash_netport6_data_reset_flags(struct hash_netport6_elem *elem, u8 *flags)
302 {
303         swap(*flags, elem->nomatch);
304 }
305 
306 static inline void
307 hash_netport6_data_netmask(struct hash_netport6_elem *elem, u8 cidr)
308 {
309         ip6_netmask(&elem->ip, cidr);
310         elem->cidr = cidr - 1;
311 }
312 
313 static bool
314 hash_netport6_data_list(struct sk_buff *skb,
315                         const struct hash_netport6_elem *data)
316 {
317         u32 flags = data->nomatch ? IPSET_FLAG_NOMATCH : 0;
318 
319         if (nla_put_ipaddr6(skb, IPSET_ATTR_IP, &data->ip.in6) ||
320             nla_put_net16(skb, IPSET_ATTR_PORT, data->port) ||
321             nla_put_u8(skb, IPSET_ATTR_CIDR, data->cidr + 1) ||
322             nla_put_u8(skb, IPSET_ATTR_PROTO, data->proto) ||
323             (flags &&
324              nla_put_net32(skb, IPSET_ATTR_CADT_FLAGS, htonl(flags))))
325                 goto nla_put_failure;
326         return false;
327 
328 nla_put_failure:
329         return true;
330 }
331 
332 static inline void
333 hash_netport6_data_next(struct hash_netport6_elem *next,
334                         const struct hash_netport6_elem *d)
335 {
336         next->port = d->port;
337 }
338 
339 #undef MTYPE
340 #undef HOST_MASK
341 
342 #define MTYPE           hash_netport6
343 #define HOST_MASK       128
344 #define IP_SET_EMIT_CREATE
345 #include "ip_set_hash_gen.h"
346 
347 static int
348 hash_netport6_kadt(struct ip_set *set, const struct sk_buff *skb,
349                    const struct xt_action_param *par,
350                    enum ipset_adt adt, struct ip_set_adt_opt *opt)
351 {
352         const struct hash_netport6 *h = set->data;
353         ipset_adtfn adtfn = set->variant->adt[adt];
354         struct hash_netport6_elem e = {
355                 .cidr = INIT_CIDR(h->nets[0].cidr[0], HOST_MASK),
356         };
357         struct ip_set_ext ext = IP_SET_INIT_KEXT(skb, opt, set);
358 
359         if (adt == IPSET_TEST)
360                 e.cidr = HOST_MASK - 1;
361 
362         if (!ip_set_get_ip6_port(skb, opt->flags & IPSET_DIM_TWO_SRC,
363                                  &e.port, &e.proto))
364                 return -EINVAL;
365 
366         ip6addrptr(skb, opt->flags & IPSET_DIM_ONE_SRC, &e.ip.in6);
367         ip6_netmask(&e.ip, e.cidr + 1);
368 
369         return adtfn(set, &e, &ext, &opt->ext, opt->cmdflags);
370 }
371 
372 static int
373 hash_netport6_uadt(struct ip_set *set, struct nlattr *tb[],
374                    enum ipset_adt adt, u32 *lineno, u32 flags, bool retried)
375 {
376         const struct hash_netport6 *h = set->data;
377         ipset_adtfn adtfn = set->variant->adt[adt];
378         struct hash_netport6_elem e = { .cidr = HOST_MASK  - 1 };
379         struct ip_set_ext ext = IP_SET_INIT_UEXT(set);
380         u32 port, port_to;
381         bool with_ports = false;
382         u8 cidr;
383         int ret;
384 
385         if (tb[IPSET_ATTR_LINENO])
386                 *lineno = nla_get_u32(tb[IPSET_ATTR_LINENO]);
387 
388         if (unlikely(!tb[IPSET_ATTR_IP] ||
389                      !ip_set_attr_netorder(tb, IPSET_ATTR_PORT) ||
390                      !ip_set_optattr_netorder(tb, IPSET_ATTR_PORT_TO) ||
391                      !ip_set_optattr_netorder(tb, IPSET_ATTR_CADT_FLAGS)))
392                 return -IPSET_ERR_PROTOCOL;
393         if (unlikely(tb[IPSET_ATTR_IP_TO]))
394                 return -IPSET_ERR_HASH_RANGE_UNSUPPORTED;
395 
396         ret = ip_set_get_ipaddr6(tb[IPSET_ATTR_IP], &e.ip);
397         if (ret)
398                 return ret;
399 
400         ret = ip_set_get_extensions(set, tb, &ext);
401         if (ret)
402                 return ret;
403 
404         if (tb[IPSET_ATTR_CIDR]) {
405                 cidr = nla_get_u8(tb[IPSET_ATTR_CIDR]);
406                 if (!cidr || cidr > HOST_MASK)
407                         return -IPSET_ERR_INVALID_CIDR;
408                 e.cidr = cidr - 1;
409         }
410         ip6_netmask(&e.ip, e.cidr + 1);
411 
412         e.port = nla_get_be16(tb[IPSET_ATTR_PORT]);
413 
414         if (tb[IPSET_ATTR_PROTO]) {
415                 e.proto = nla_get_u8(tb[IPSET_ATTR_PROTO]);
416                 with_ports = ip_set_proto_with_ports(e.proto);
417 
418                 if (e.proto == 0)
419                         return -IPSET_ERR_INVALID_PROTO;
420         } else {
421                 return -IPSET_ERR_MISSING_PROTO;
422         }
423 
424         if (!(with_ports || e.proto == IPPROTO_ICMPV6))
425                 e.port = 0;
426 
427         if (tb[IPSET_ATTR_CADT_FLAGS]) {
428                 u32 cadt_flags = ip_set_get_h32(tb[IPSET_ATTR_CADT_FLAGS]);
429 
430                 if (cadt_flags & IPSET_FLAG_NOMATCH)
431                         flags |= (IPSET_FLAG_NOMATCH << 16);
432         }
433 
434         if (adt == IPSET_TEST || !with_ports || !tb[IPSET_ATTR_PORT_TO]) {
435                 ret = adtfn(set, &e, &ext, &ext, flags);
436                 return ip_set_enomatch(ret, flags, adt, set) ? -ret :
437                        ip_set_eexist(ret, flags) ? 0 : ret;
438         }
439 
440         port = ntohs(e.port);
441         port_to = ip_set_get_h16(tb[IPSET_ATTR_PORT_TO]);
442         if (port > port_to)
443                 swap(port, port_to);
444 
445         if (retried)
446                 port = ntohs(h->next.port);
447         for (; port <= port_to; port++) {
448                 e.port = htons(port);
449                 ret = adtfn(set, &e, &ext, &ext, flags);
450 
451                 if (ret && !ip_set_eexist(ret, flags))
452                         return ret;
453 
454                 ret = 0;
455         }
456         return ret;
457 }
458 
459 static struct ip_set_type hash_netport_type __read_mostly = {
460         .name           = "hash:net,port",
461         .protocol       = IPSET_PROTOCOL,
462         .features       = IPSET_TYPE_IP | IPSET_TYPE_PORT | IPSET_TYPE_NOMATCH,
463         .dimension      = IPSET_DIM_TWO,
464         .family         = NFPROTO_UNSPEC,
465         .revision_min   = IPSET_TYPE_REV_MIN,
466         .revision_max   = IPSET_TYPE_REV_MAX,
467         .create         = hash_netport_create,
468         .create_policy  = {
469                 [IPSET_ATTR_HASHSIZE]   = { .type = NLA_U32 },
470                 [IPSET_ATTR_MAXELEM]    = { .type = NLA_U32 },
471                 [IPSET_ATTR_PROBES]     = { .type = NLA_U8 },
472                 [IPSET_ATTR_RESIZE]     = { .type = NLA_U8  },
473                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
474                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
475                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
476         },
477         .adt_policy     = {
478                 [IPSET_ATTR_IP]         = { .type = NLA_NESTED },
479                 [IPSET_ATTR_IP_TO]      = { .type = NLA_NESTED },
480                 [IPSET_ATTR_PORT]       = { .type = NLA_U16 },
481                 [IPSET_ATTR_PORT_TO]    = { .type = NLA_U16 },
482                 [IPSET_ATTR_PROTO]      = { .type = NLA_U8 },
483                 [IPSET_ATTR_CIDR]       = { .type = NLA_U8 },
484                 [IPSET_ATTR_TIMEOUT]    = { .type = NLA_U32 },
485                 [IPSET_ATTR_LINENO]     = { .type = NLA_U32 },
486                 [IPSET_ATTR_CADT_FLAGS] = { .type = NLA_U32 },
487                 [IPSET_ATTR_BYTES]      = { .type = NLA_U64 },
488                 [IPSET_ATTR_PACKETS]    = { .type = NLA_U64 },
489                 [IPSET_ATTR_COMMENT]    = { .type = NLA_NUL_STRING,
490                                             .len  = IPSET_MAX_COMMENT_SIZE },
491                 [IPSET_ATTR_SKBMARK]    = { .type = NLA_U64 },
492                 [IPSET_ATTR_SKBPRIO]    = { .type = NLA_U32 },
493                 [IPSET_ATTR_SKBQUEUE]   = { .type = NLA_U16 },
494         },
495         .me             = THIS_MODULE,
496 };
497 
498 static int __init
499 hash_netport_init(void)
500 {
501         return ip_set_type_register(&hash_netport_type);
502 }
503 
504 static void __exit
505 hash_netport_fini(void)
506 {
507         rcu_barrier();
508         ip_set_type_unregister(&hash_netport_type);
509 }
510 
511 module_init(hash_netport_init);
512 module_exit(hash_netport_fini);
513 

~ [ 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