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