]>
Commit | Line | Data |
---|---|---|
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 | ||
23 | MODULE_LICENSE("GPL"); | |
24 | MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>"); | |
2ae15b64 | 25 | MODULE_DESCRIPTION("Xtables: multiple port matching for TCP, UDP, UDP-Lite, SCTP and DCCP"); |
a89ecb6a YK |
26 | MODULE_ALIAS("ipt_multiport"); |
27 | MODULE_ALIAS("ip6t_multiport"); | |
28 | ||
a89ecb6a | 29 | /* Returns 1 if the port is matched by the test, 0 otherwise. */ |
1d93a9cb | 30 | static inline bool |
a89ecb6a YK |
31 | ports_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 | 44 | |
dd2602d0 GF |
45 | switch (minfo->flags) { |
46 | case XT_MULTIPORT_SOURCE: | |
1ed9887e GF |
47 | if (src >= s && src <= e) |
48 | return true ^ minfo->invert; | |
49 | break; | |
dd2602d0 | 50 | case XT_MULTIPORT_DESTINATION: |
1ed9887e GF |
51 | if (dst >= s && dst <= e) |
52 | return true ^ minfo->invert; | |
53 | break; | |
dd2602d0 | 54 | case XT_MULTIPORT_EITHER: |
1ed9887e GF |
55 | if ((dst >= s && dst <= e) || |
56 | (src >= s && src <= e)) | |
57 | return true ^ minfo->invert; | |
58 | break; | |
dd2602d0 GF |
59 | default: |
60 | break; | |
61 | } | |
a89ecb6a YK |
62 | } else { |
63 | /* exact port matching */ | |
be91fd5e | 64 | pr_debug("src or dst matches with %d?\n", s); |
a89ecb6a | 65 | |
dd2602d0 GF |
66 | switch (minfo->flags) { |
67 | case XT_MULTIPORT_SOURCE: | |
1ed9887e GF |
68 | if (src == s) |
69 | return true ^ minfo->invert; | |
70 | break; | |
dd2602d0 | 71 | case XT_MULTIPORT_DESTINATION: |
1ed9887e GF |
72 | if (dst == s) |
73 | return true ^ minfo->invert; | |
74 | break; | |
dd2602d0 | 75 | case XT_MULTIPORT_EITHER: |
1ed9887e GF |
76 | if (src == s || dst == s) |
77 | return true ^ minfo->invert; | |
78 | break; | |
dd2602d0 GF |
79 | default: |
80 | break; | |
81 | } | |
a89ecb6a YK |
82 | } |
83 | } | |
84 | ||
601e68e1 | 85 | return minfo->invert; |
a89ecb6a YK |
86 | } |
87 | ||
1d93a9cb | 88 | static bool |
62fc8051 | 89 | multiport_mt(const struct sk_buff *skb, struct xt_action_param *par) |
a89ecb6a | 90 | { |
3cf93c96 JE |
91 | const __be16 *pptr; |
92 | __be16 _ports[2]; | |
f7108a20 | 93 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; |
a89ecb6a | 94 | |
f7108a20 | 95 | if (par->fragoff != 0) |
1d93a9cb | 96 | return false; |
a89ecb6a | 97 | |
f7108a20 | 98 | pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports); |
a89ecb6a YK |
99 | if (pptr == NULL) { |
100 | /* We've been asked to examine this packet, and we | |
101 | * can't. Hence, no choice but to drop. | |
102 | */ | |
be91fd5e | 103 | pr_debug("Dropping evil offset=0 tinygram.\n"); |
b4ba2611 | 104 | par->hotdrop = true; |
1d93a9cb | 105 | return false; |
a89ecb6a YK |
106 | } |
107 | ||
108 | return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1])); | |
109 | } | |
110 | ||
ccb79bdc | 111 | static inline bool |
a89ecb6a YK |
112 | check(u_int16_t proto, |
113 | u_int8_t ip_invflags, | |
114 | u_int8_t match_flags, | |
115 | u_int8_t count) | |
116 | { | |
957dc80a PM |
117 | /* Must specify supported protocol, no unknown flags or bad count */ |
118 | return (proto == IPPROTO_TCP || proto == IPPROTO_UDP | |
ba4e58ec | 119 | || proto == IPPROTO_UDPLITE |
957dc80a | 120 | || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP) |
a89ecb6a YK |
121 | && !(ip_invflags & XT_INV_PROTO) |
122 | && (match_flags == XT_MULTIPORT_SOURCE | |
123 | || match_flags == XT_MULTIPORT_DESTINATION | |
124 | || match_flags == XT_MULTIPORT_EITHER) | |
125 | && count <= XT_MULTI_PORTS; | |
126 | } | |
127 | ||
b0f38452 | 128 | static int multiport_mt_check(const struct xt_mtchk_param *par) |
a89ecb6a | 129 | { |
9b4fce7a JE |
130 | const struct ipt_ip *ip = par->entryinfo; |
131 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; | |
a89ecb6a YK |
132 | |
133 | return check(ip->proto, ip->invflags, multiinfo->flags, | |
c29c9492 | 134 | multiinfo->count) ? 0 : -EINVAL; |
a89ecb6a YK |
135 | } |
136 | ||
b0f38452 | 137 | static int multiport_mt6_check(const struct xt_mtchk_param *par) |
a89ecb6a | 138 | { |
9b4fce7a JE |
139 | const struct ip6t_ip6 *ip = par->entryinfo; |
140 | const struct xt_multiport_v1 *multiinfo = par->matchinfo; | |
a89ecb6a YK |
141 | |
142 | return check(ip->proto, ip->invflags, multiinfo->flags, | |
c29c9492 | 143 | multiinfo->count) ? 0 : -EINVAL; |
a89ecb6a YK |
144 | } |
145 | ||
d3c5ee6d | 146 | static struct xt_match multiport_mt_reg[] __read_mostly = { |
4470bbc7 PM |
147 | { |
148 | .name = "multiport", | |
ee999d8b | 149 | .family = NFPROTO_IPV4, |
4470bbc7 | 150 | .revision = 1, |
d3c5ee6d JE |
151 | .checkentry = multiport_mt_check, |
152 | .match = multiport_mt, | |
4470bbc7 PM |
153 | .matchsize = sizeof(struct xt_multiport_v1), |
154 | .me = THIS_MODULE, | |
155 | }, | |
4470bbc7 PM |
156 | { |
157 | .name = "multiport", | |
ee999d8b | 158 | .family = NFPROTO_IPV6, |
4470bbc7 | 159 | .revision = 1, |
d3c5ee6d JE |
160 | .checkentry = multiport_mt6_check, |
161 | .match = multiport_mt, | |
4470bbc7 PM |
162 | .matchsize = sizeof(struct xt_multiport_v1), |
163 | .me = THIS_MODULE, | |
164 | }, | |
a89ecb6a YK |
165 | }; |
166 | ||
d3c5ee6d | 167 | static int __init multiport_mt_init(void) |
a89ecb6a | 168 | { |
d3c5ee6d JE |
169 | return xt_register_matches(multiport_mt_reg, |
170 | ARRAY_SIZE(multiport_mt_reg)); | |
a89ecb6a YK |
171 | } |
172 | ||
d3c5ee6d | 173 | static void __exit multiport_mt_exit(void) |
a89ecb6a | 174 | { |
d3c5ee6d | 175 | xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg)); |
a89ecb6a YK |
176 | } |
177 | ||
d3c5ee6d JE |
178 | module_init(multiport_mt_init); |
179 | module_exit(multiport_mt_exit); |