drm/panel-edp: Add BOE NV140FHM-NZ panel entry
[drm/drm-misc.git] / net / netfilter / xt_connlimit.c
blob0189f8b6b0bd15e7b0714de41359964b6e6990aa
1 /*
2 * netfilter module to limit the number of parallel tcp
3 * connections per IP address.
4 * (c) 2000 Gerd Knorr <kraxel@bytesex.org>
5 * Nov 2002: Martin Bene <martin.bene@icomedias.com>:
6 * only ignore TIME_WAIT or gone connections
7 * (C) CC Computer Consultants GmbH, 2007
9 * based on ...
11 * Kernel module to match connection tracking information.
12 * GPL (C) 1999 Rusty Russell (rusty@rustcorp.com.au).
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/ip.h>
17 #include <linux/ipv6.h>
18 #include <linux/module.h>
19 #include <linux/skbuff.h>
20 #include <linux/netfilter/x_tables.h>
21 #include <linux/netfilter/xt_connlimit.h>
23 #include <net/netfilter/nf_conntrack.h>
24 #include <net/netfilter/nf_conntrack_core.h>
25 #include <net/netfilter/nf_conntrack_tuple.h>
26 #include <net/netfilter/nf_conntrack_zones.h>
27 #include <net/netfilter/nf_conntrack_count.h>
29 static bool
30 connlimit_mt(const struct sk_buff *skb, struct xt_action_param *par)
32 struct net *net = xt_net(par);
33 const struct xt_connlimit_info *info = par->matchinfo;
34 struct nf_conntrack_tuple tuple;
35 const struct nf_conntrack_tuple *tuple_ptr = &tuple;
36 const struct nf_conntrack_zone *zone = &nf_ct_zone_dflt;
37 enum ip_conntrack_info ctinfo;
38 const struct nf_conn *ct;
39 unsigned int connections;
40 u32 key[5];
42 ct = nf_ct_get(skb, &ctinfo);
43 if (ct != NULL) {
44 tuple_ptr = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
45 zone = nf_ct_zone(ct);
46 } else if (!nf_ct_get_tuplepr(skb, skb_network_offset(skb),
47 xt_family(par), net, &tuple)) {
48 goto hotdrop;
51 if (xt_family(par) == NFPROTO_IPV6) {
52 const struct ipv6hdr *iph = ipv6_hdr(skb);
53 union nf_inet_addr addr;
54 unsigned int i;
56 memcpy(&addr.ip6, (info->flags & XT_CONNLIMIT_DADDR) ?
57 &iph->daddr : &iph->saddr, sizeof(addr.ip6));
59 for (i = 0; i < ARRAY_SIZE(addr.ip6); ++i)
60 addr.ip6[i] &= info->mask.ip6[i];
61 memcpy(key, &addr, sizeof(addr.ip6));
62 key[4] = zone->id;
63 } else {
64 const struct iphdr *iph = ip_hdr(skb);
66 key[0] = (info->flags & XT_CONNLIMIT_DADDR) ?
67 (__force __u32)iph->daddr : (__force __u32)iph->saddr;
68 key[0] &= (__force __u32)info->mask.ip;
69 key[1] = zone->id;
72 connections = nf_conncount_count(net, info->data, key, tuple_ptr,
73 zone);
74 if (connections == 0)
75 /* kmalloc failed, drop it entirely */
76 goto hotdrop;
78 return (connections > info->limit) ^ !!(info->flags & XT_CONNLIMIT_INVERT);
80 hotdrop:
81 par->hotdrop = true;
82 return false;
85 static int connlimit_mt_check(const struct xt_mtchk_param *par)
87 struct xt_connlimit_info *info = par->matchinfo;
88 unsigned int keylen;
89 int ret;
91 keylen = sizeof(u32);
92 if (par->family == NFPROTO_IPV6)
93 keylen += sizeof(struct in6_addr);
94 else
95 keylen += sizeof(struct in_addr);
97 ret = nf_ct_netns_get(par->net, par->family);
98 if (ret < 0) {
99 pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
100 par->family);
101 return ret;
104 /* init private data */
105 info->data = nf_conncount_init(par->net, keylen);
106 if (IS_ERR(info->data))
107 nf_ct_netns_put(par->net, par->family);
109 return PTR_ERR_OR_ZERO(info->data);
112 static void connlimit_mt_destroy(const struct xt_mtdtor_param *par)
114 const struct xt_connlimit_info *info = par->matchinfo;
116 nf_conncount_destroy(par->net, info->data);
117 nf_ct_netns_put(par->net, par->family);
120 static struct xt_match connlimit_mt_reg[] __read_mostly = {
122 .name = "connlimit",
123 .revision = 1,
124 .family = NFPROTO_IPV4,
125 .checkentry = connlimit_mt_check,
126 .match = connlimit_mt,
127 .matchsize = sizeof(struct xt_connlimit_info),
128 .usersize = offsetof(struct xt_connlimit_info, data),
129 .destroy = connlimit_mt_destroy,
130 .me = THIS_MODULE,
132 #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
134 .name = "connlimit",
135 .revision = 1,
136 .family = NFPROTO_IPV6,
137 .checkentry = connlimit_mt_check,
138 .match = connlimit_mt,
139 .matchsize = sizeof(struct xt_connlimit_info),
140 .usersize = offsetof(struct xt_connlimit_info, data),
141 .destroy = connlimit_mt_destroy,
142 .me = THIS_MODULE,
144 #endif
147 static int __init connlimit_mt_init(void)
149 return xt_register_matches(connlimit_mt_reg, ARRAY_SIZE(connlimit_mt_reg));
152 static void __exit connlimit_mt_exit(void)
154 xt_unregister_matches(connlimit_mt_reg, ARRAY_SIZE(connlimit_mt_reg));
157 module_init(connlimit_mt_init);
158 module_exit(connlimit_mt_exit);
159 MODULE_AUTHOR("Jan Engelhardt <jengelh@medozas.de>");
160 MODULE_DESCRIPTION("Xtables: Number of connections matching");
161 MODULE_LICENSE("GPL");
162 MODULE_ALIAS("ipt_connlimit");
163 MODULE_ALIAS("ip6t_connlimit");