netfilter: xtables: change xt_match.checkentry return type
[deliverable/linux.git] / net / netfilter / xt_multiport.c
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. */
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 */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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
23 MODULE_LICENSE("GPL");
24 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
25 MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP");
26 MODULE_ALIAS("ipt_multiport");
27 MODULE_ALIAS("ip6t_multiport");
28
29 /* Returns 1 if the port is matched by the test, 0 otherwise. */
30 static inline bool
31 ports_match_v0(const u_int16_t *portlist, enum xt_multiport_flags flags,
32 u_int8_t count, u_int16_t src, u_int16_t dst)
33 {
34 unsigned int i;
35 for (i = 0; i < count; i++) {
36 if (flags != XT_MULTIPORT_DESTINATION && portlist[i] == src)
37 return true;
38
39 if (flags != XT_MULTIPORT_SOURCE && portlist[i] == dst)
40 return true;
41 }
42
43 return false;
44 }
45
46 /* Returns 1 if the port is matched by the test, 0 otherwise. */
47 static inline bool
48 ports_match_v1(const struct xt_multiport_v1 *minfo,
49 u_int16_t src, u_int16_t dst)
50 {
51 unsigned int i;
52 u_int16_t s, e;
53
54 for (i = 0; i < minfo->count; i++) {
55 s = minfo->ports[i];
56
57 if (minfo->pflags[i]) {
58 /* range port matching */
59 e = minfo->ports[++i];
60 pr_debug("src or dst matches with %d-%d?\n", s, e);
61
62 if (minfo->flags == XT_MULTIPORT_SOURCE
63 && src >= s && src <= e)
64 return true ^ minfo->invert;
65 if (minfo->flags == XT_MULTIPORT_DESTINATION
66 && dst >= s && dst <= e)
67 return true ^ minfo->invert;
68 if (minfo->flags == XT_MULTIPORT_EITHER
69 && ((dst >= s && dst <= e)
70 || (src >= s && src <= e)))
71 return true ^ minfo->invert;
72 } else {
73 /* exact port matching */
74 pr_debug("src or dst matches with %d?\n", s);
75
76 if (minfo->flags == XT_MULTIPORT_SOURCE
77 && src == s)
78 return true ^ minfo->invert;
79 if (minfo->flags == XT_MULTIPORT_DESTINATION
80 && dst == s)
81 return true ^ minfo->invert;
82 if (minfo->flags == XT_MULTIPORT_EITHER
83 && (src == s || dst == s))
84 return true ^ minfo->invert;
85 }
86 }
87
88 return minfo->invert;
89 }
90
91 static bool
92 multiport_mt_v0(const struct sk_buff *skb, const struct xt_match_param *par)
93 {
94 const __be16 *pptr;
95 __be16 _ports[2];
96 const struct xt_multiport *multiinfo = par->matchinfo;
97
98 if (par->fragoff != 0)
99 return false;
100
101 pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
102 if (pptr == NULL) {
103 /* We've been asked to examine this packet, and we
104 * can't. Hence, no choice but to drop.
105 */
106 pr_debug("Dropping evil offset=0 tinygram.\n");
107 *par->hotdrop = true;
108 return false;
109 }
110
111 return ports_match_v0(multiinfo->ports, multiinfo->flags,
112 multiinfo->count, ntohs(pptr[0]), ntohs(pptr[1]));
113 }
114
115 static bool
116 multiport_mt(const struct sk_buff *skb, const struct xt_match_param *par)
117 {
118 const __be16 *pptr;
119 __be16 _ports[2];
120 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
121
122 if (par->fragoff != 0)
123 return false;
124
125 pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
126 if (pptr == NULL) {
127 /* We've been asked to examine this packet, and we
128 * can't. Hence, no choice but to drop.
129 */
130 pr_debug("Dropping evil offset=0 tinygram.\n");
131 *par->hotdrop = true;
132 return false;
133 }
134
135 return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
136 }
137
138 static inline bool
139 check(u_int16_t proto,
140 u_int8_t ip_invflags,
141 u_int8_t match_flags,
142 u_int8_t count)
143 {
144 /* Must specify supported protocol, no unknown flags or bad count */
145 return (proto == IPPROTO_TCP || proto == IPPROTO_UDP
146 || proto == IPPROTO_UDPLITE
147 || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)
148 && !(ip_invflags & XT_INV_PROTO)
149 && (match_flags == XT_MULTIPORT_SOURCE
150 || match_flags == XT_MULTIPORT_DESTINATION
151 || match_flags == XT_MULTIPORT_EITHER)
152 && count <= XT_MULTI_PORTS;
153 }
154
155 static int multiport_mt_check_v0(const struct xt_mtchk_param *par)
156 {
157 const struct ipt_ip *ip = par->entryinfo;
158 const struct xt_multiport *multiinfo = par->matchinfo;
159
160 return check(ip->proto, ip->invflags, multiinfo->flags,
161 multiinfo->count);
162 }
163
164 static int multiport_mt_check(const struct xt_mtchk_param *par)
165 {
166 const struct ipt_ip *ip = par->entryinfo;
167 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
168
169 return check(ip->proto, ip->invflags, multiinfo->flags,
170 multiinfo->count);
171 }
172
173 static int multiport_mt6_check_v0(const struct xt_mtchk_param *par)
174 {
175 const struct ip6t_ip6 *ip = par->entryinfo;
176 const struct xt_multiport *multiinfo = par->matchinfo;
177
178 return check(ip->proto, ip->invflags, multiinfo->flags,
179 multiinfo->count);
180 }
181
182 static int multiport_mt6_check(const struct xt_mtchk_param *par)
183 {
184 const struct ip6t_ip6 *ip = par->entryinfo;
185 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
186
187 return check(ip->proto, ip->invflags, multiinfo->flags,
188 multiinfo->count);
189 }
190
191 static struct xt_match multiport_mt_reg[] __read_mostly = {
192 {
193 .name = "multiport",
194 .family = NFPROTO_IPV4,
195 .revision = 0,
196 .checkentry = multiport_mt_check_v0,
197 .match = multiport_mt_v0,
198 .matchsize = sizeof(struct xt_multiport),
199 .me = THIS_MODULE,
200 },
201 {
202 .name = "multiport",
203 .family = NFPROTO_IPV4,
204 .revision = 1,
205 .checkentry = multiport_mt_check,
206 .match = multiport_mt,
207 .matchsize = sizeof(struct xt_multiport_v1),
208 .me = THIS_MODULE,
209 },
210 {
211 .name = "multiport",
212 .family = NFPROTO_IPV6,
213 .revision = 0,
214 .checkentry = multiport_mt6_check_v0,
215 .match = multiport_mt_v0,
216 .matchsize = sizeof(struct xt_multiport),
217 .me = THIS_MODULE,
218 },
219 {
220 .name = "multiport",
221 .family = NFPROTO_IPV6,
222 .revision = 1,
223 .checkentry = multiport_mt6_check,
224 .match = multiport_mt,
225 .matchsize = sizeof(struct xt_multiport_v1),
226 .me = THIS_MODULE,
227 },
228 };
229
230 static int __init multiport_mt_init(void)
231 {
232 return xt_register_matches(multiport_mt_reg,
233 ARRAY_SIZE(multiport_mt_reg));
234 }
235
236 static void __exit multiport_mt_exit(void)
237 {
238 xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg));
239 }
240
241 module_init(multiport_mt_init);
242 module_exit(multiport_mt_exit);
This page took 0.044013 seconds and 5 git commands to generate.