Merge tag 'modules-for-v5.5' of git://git.kernel.org/pub/scm/linux/kernel/git/jeyu...
[linux-block.git] / net / netfilter / xt_multiport.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
ba4e58ec
GR
2/* Kernel module to match one of a list of TCP/UDP(-Lite)/SCTP/DCCP ports:
3 ports are in the same place so we can treat them as equal. */
a89ecb6a
YK
4
5/* (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
a89ecb6a 7 */
be91fd5e 8#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
a89ecb6a
YK
9#include <linux/module.h>
10#include <linux/types.h>
11#include <linux/udp.h>
12#include <linux/skbuff.h>
13#include <linux/in.h>
14
15#include <linux/netfilter/xt_multiport.h>
16#include <linux/netfilter/x_tables.h>
17#include <linux/netfilter_ipv4/ip_tables.h>
18#include <linux/netfilter_ipv6/ip6_tables.h>
19
20MODULE_LICENSE("GPL");
21MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
2ae15b64 22MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP");
a89ecb6a
YK
23MODULE_ALIAS("ipt_multiport");
24MODULE_ALIAS("ip6t_multiport");
25
a89ecb6a 26/* Returns 1 if the port is matched by the test, 0 otherwise. */
1d93a9cb 27static inline bool
a89ecb6a
YK
28ports_match_v1(const struct xt_multiport_v1 *minfo,
29 u_int16_t src, u_int16_t dst)
30{
31 unsigned int i;
32 u_int16_t s, e;
33
34 for (i = 0; i < minfo->count; i++) {
35 s = minfo->ports[i];
36
37 if (minfo->pflags[i]) {
38 /* range port matching */
39 e = minfo->ports[++i];
be91fd5e 40 pr_debug("src or dst matches with %d-%d?\n", s, e);
a89ecb6a 41
dd2602d0
GF
42 switch (minfo->flags) {
43 case XT_MULTIPORT_SOURCE:
1ed9887e
GF
44 if (src >= s && src <= e)
45 return true ^ minfo->invert;
46 break;
dd2602d0 47 case XT_MULTIPORT_DESTINATION:
1ed9887e
GF
48 if (dst >= s && dst <= e)
49 return true ^ minfo->invert;
50 break;
dd2602d0 51 case XT_MULTIPORT_EITHER:
1ed9887e
GF
52 if ((dst >= s && dst <= e) ||
53 (src >= s && src <= e))
54 return true ^ minfo->invert;
55 break;
dd2602d0
GF
56 default:
57 break;
58 }
a89ecb6a
YK
59 } else {
60 /* exact port matching */
be91fd5e 61 pr_debug("src or dst matches with %d?\n", s);
a89ecb6a 62
dd2602d0
GF
63 switch (minfo->flags) {
64 case XT_MULTIPORT_SOURCE:
1ed9887e
GF
65 if (src == s)
66 return true ^ minfo->invert;
67 break;
dd2602d0 68 case XT_MULTIPORT_DESTINATION:
1ed9887e
GF
69 if (dst == s)
70 return true ^ minfo->invert;
71 break;
dd2602d0 72 case XT_MULTIPORT_EITHER:
1ed9887e
GF
73 if (src == s || dst == s)
74 return true ^ minfo->invert;
75 break;
dd2602d0
GF
76 default:
77 break;
78 }
a89ecb6a
YK
79 }
80 }
81
601e68e1 82 return minfo->invert;
a89ecb6a
YK
83}
84
1d93a9cb 85static bool
62fc8051 86multiport_mt(const struct sk_buff *skb, struct xt_action_param *par)
a89ecb6a 87{
3cf93c96
JE
88 const __be16 *pptr;
89 __be16 _ports[2];
f7108a20 90 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
a89ecb6a 91
f7108a20 92 if (par->fragoff != 0)
1d93a9cb 93 return false;
a89ecb6a 94
f7108a20 95 pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
a89ecb6a
YK
96 if (pptr == NULL) {
97 /* We've been asked to examine this packet, and we
98 * can't. Hence, no choice but to drop.
99 */
be91fd5e 100 pr_debug("Dropping evil offset=0 tinygram.\n");
b4ba2611 101 par->hotdrop = true;
1d93a9cb 102 return false;
a89ecb6a
YK
103 }
104
105 return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
106}
107
ccb79bdc 108static inline bool
a89ecb6a
YK
109check(u_int16_t proto,
110 u_int8_t ip_invflags,
111 u_int8_t match_flags,
112 u_int8_t count)
113{
957dc80a
PM
114 /* Must specify supported protocol, no unknown flags or bad count */
115 return (proto == IPPROTO_TCP || proto == IPPROTO_UDP
ba4e58ec 116 || proto == IPPROTO_UDPLITE
957dc80a 117 || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)
a89ecb6a
YK
118 && !(ip_invflags & XT_INV_PROTO)
119 && (match_flags == XT_MULTIPORT_SOURCE
120 || match_flags == XT_MULTIPORT_DESTINATION
121 || match_flags == XT_MULTIPORT_EITHER)
122 && count <= XT_MULTI_PORTS;
123}
124
b0f38452 125static int multiport_mt_check(const struct xt_mtchk_param *par)
a89ecb6a 126{
9b4fce7a
JE
127 const struct ipt_ip *ip = par->entryinfo;
128 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
a89ecb6a
YK
129
130 return check(ip->proto, ip->invflags, multiinfo->flags,
c29c9492 131 multiinfo->count) ? 0 : -EINVAL;
a89ecb6a
YK
132}
133
b0f38452 134static int multiport_mt6_check(const struct xt_mtchk_param *par)
a89ecb6a 135{
9b4fce7a
JE
136 const struct ip6t_ip6 *ip = par->entryinfo;
137 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
a89ecb6a
YK
138
139 return check(ip->proto, ip->invflags, multiinfo->flags,
c29c9492 140 multiinfo->count) ? 0 : -EINVAL;
a89ecb6a
YK
141}
142
d3c5ee6d 143static struct xt_match multiport_mt_reg[] __read_mostly = {
4470bbc7
PM
144 {
145 .name = "multiport",
ee999d8b 146 .family = NFPROTO_IPV4,
4470bbc7 147 .revision = 1,
d3c5ee6d
JE
148 .checkentry = multiport_mt_check,
149 .match = multiport_mt,
4470bbc7
PM
150 .matchsize = sizeof(struct xt_multiport_v1),
151 .me = THIS_MODULE,
152 },
4470bbc7
PM
153 {
154 .name = "multiport",
ee999d8b 155 .family = NFPROTO_IPV6,
4470bbc7 156 .revision = 1,
d3c5ee6d
JE
157 .checkentry = multiport_mt6_check,
158 .match = multiport_mt,
4470bbc7
PM
159 .matchsize = sizeof(struct xt_multiport_v1),
160 .me = THIS_MODULE,
161 },
a89ecb6a
YK
162};
163
d3c5ee6d 164static int __init multiport_mt_init(void)
a89ecb6a 165{
d3c5ee6d
JE
166 return xt_register_matches(multiport_mt_reg,
167 ARRAY_SIZE(multiport_mt_reg));
a89ecb6a
YK
168}
169
d3c5ee6d 170static void __exit multiport_mt_exit(void)
a89ecb6a 171{
d3c5ee6d 172 xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg));
a89ecb6a
YK
173}
174
d3c5ee6d
JE
175module_init(multiport_mt_init);
176module_exit(multiport_mt_exit);