1 // SPDX-License-Identifier: GPL-2.0-only
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. */
5 /* (C) 1999-2001 Paul `Rusty' Russell
6 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
9 #include <linux/module.h>
10 #include <linux/types.h>
11 #include <linux/udp.h>
12 #include <linux/skbuff.h>
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>
20 MODULE_LICENSE("GPL");
21 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
22 MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP");
23 MODULE_ALIAS("ipt_multiport");
24 MODULE_ALIAS("ip6t_multiport");
26 /* Returns 1 if the port is matched by the test, 0 otherwise. */
28 ports_match_v1(const struct xt_multiport_v1
*minfo
,
29 u_int16_t src
, u_int16_t dst
)
34 for (i
= 0; i
< minfo
->count
; i
++) {
37 if (minfo
->pflags
[i
]) {
38 /* range port matching */
39 e
= minfo
->ports
[++i
];
40 pr_debug("src or dst matches with %d-%d?\n", s
, e
);
42 switch (minfo
->flags
) {
43 case XT_MULTIPORT_SOURCE
:
44 if (src
>= s
&& src
<= e
)
45 return true ^ minfo
->invert
;
47 case XT_MULTIPORT_DESTINATION
:
48 if (dst
>= s
&& dst
<= e
)
49 return true ^ minfo
->invert
;
51 case XT_MULTIPORT_EITHER
:
52 if ((dst
>= s
&& dst
<= e
) ||
53 (src
>= s
&& src
<= e
))
54 return true ^ minfo
->invert
;
60 /* exact port matching */
61 pr_debug("src or dst matches with %d?\n", s
);
63 switch (minfo
->flags
) {
64 case XT_MULTIPORT_SOURCE
:
66 return true ^ minfo
->invert
;
68 case XT_MULTIPORT_DESTINATION
:
70 return true ^ minfo
->invert
;
72 case XT_MULTIPORT_EITHER
:
73 if (src
== s
|| dst
== s
)
74 return true ^ minfo
->invert
;
86 multiport_mt(const struct sk_buff
*skb
, struct xt_action_param
*par
)
90 const struct xt_multiport_v1
*multiinfo
= par
->matchinfo
;
92 if (par
->fragoff
!= 0)
95 pptr
= skb_header_pointer(skb
, par
->thoff
, sizeof(_ports
), _ports
);
97 /* We've been asked to examine this packet, and we
98 * can't. Hence, no choice but to drop.
100 pr_debug("Dropping evil offset=0 tinygram.\n");
105 return ports_match_v1(multiinfo
, ntohs(pptr
[0]), ntohs(pptr
[1]));
109 check(u_int16_t proto
,
110 u_int8_t ip_invflags
,
111 u_int8_t match_flags
,
114 /* Must specify supported protocol, no unknown flags or bad count */
115 return (proto
== IPPROTO_TCP
|| proto
== IPPROTO_UDP
116 || proto
== IPPROTO_UDPLITE
117 || proto
== IPPROTO_SCTP
|| proto
== IPPROTO_DCCP
)
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
;
125 static int multiport_mt_check(const struct xt_mtchk_param
*par
)
127 const struct ipt_ip
*ip
= par
->entryinfo
;
128 const struct xt_multiport_v1
*multiinfo
= par
->matchinfo
;
130 return check(ip
->proto
, ip
->invflags
, multiinfo
->flags
,
131 multiinfo
->count
) ? 0 : -EINVAL
;
134 static int multiport_mt6_check(const struct xt_mtchk_param
*par
)
136 const struct ip6t_ip6
*ip
= par
->entryinfo
;
137 const struct xt_multiport_v1
*multiinfo
= par
->matchinfo
;
139 return check(ip
->proto
, ip
->invflags
, multiinfo
->flags
,
140 multiinfo
->count
) ? 0 : -EINVAL
;
143 static struct xt_match multiport_mt_reg
[] __read_mostly
= {
146 .family
= NFPROTO_IPV4
,
148 .checkentry
= multiport_mt_check
,
149 .match
= multiport_mt
,
150 .matchsize
= sizeof(struct xt_multiport_v1
),
155 .family
= NFPROTO_IPV6
,
157 .checkentry
= multiport_mt6_check
,
158 .match
= multiport_mt
,
159 .matchsize
= sizeof(struct xt_multiport_v1
),
164 static int __init
multiport_mt_init(void)
166 return xt_register_matches(multiport_mt_reg
,
167 ARRAY_SIZE(multiport_mt_reg
));
170 static void __exit
multiport_mt_exit(void)
172 xt_unregister_matches(multiport_mt_reg
, ARRAY_SIZE(multiport_mt_reg
));
175 module_init(multiport_mt_init
);
176 module_exit(multiport_mt_exit
);