]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/netfilter/xt_multiport.c
netfilter: x_tables: pass xt_counters struct to counter allocator
[mirror_ubuntu-artful-kernel.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_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];
43 pr_debug("src or dst matches with %d-%d?\n", s, e);
44
45 switch (minfo->flags) {
46 case XT_MULTIPORT_SOURCE:
47 return (src >= s && src <= e) ^ minfo->invert;
48 case XT_MULTIPORT_DESTINATION:
49 return (dst >= s && dst <= e) ^ minfo->invert;
50 case XT_MULTIPORT_EITHER:
51 return ((dst >= s && dst <= e) ||
52 (src >= s && src <= e)) ^ minfo->invert;
53 default:
54 break;
55 }
56 } else {
57 /* exact port matching */
58 pr_debug("src or dst matches with %d?\n", s);
59
60 switch (minfo->flags) {
61 case XT_MULTIPORT_SOURCE:
62 return (src == s) ^ minfo->invert;
63 case XT_MULTIPORT_DESTINATION:
64 return (dst == s) ^ minfo->invert;
65 case XT_MULTIPORT_EITHER:
66 return (src == s || dst == s) ^ minfo->invert;
67 default:
68 break;
69 }
70 }
71 }
72
73 return minfo->invert;
74 }
75
76 static bool
77 multiport_mt(const struct sk_buff *skb, struct xt_action_param *par)
78 {
79 const __be16 *pptr;
80 __be16 _ports[2];
81 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
82
83 if (par->fragoff != 0)
84 return false;
85
86 pptr = skb_header_pointer(skb, par->thoff, sizeof(_ports), _ports);
87 if (pptr == NULL) {
88 /* We've been asked to examine this packet, and we
89 * can't. Hence, no choice but to drop.
90 */
91 pr_debug("Dropping evil offset=0 tinygram.\n");
92 par->hotdrop = true;
93 return false;
94 }
95
96 return ports_match_v1(multiinfo, ntohs(pptr[0]), ntohs(pptr[1]));
97 }
98
99 static inline bool
100 check(u_int16_t proto,
101 u_int8_t ip_invflags,
102 u_int8_t match_flags,
103 u_int8_t count)
104 {
105 /* Must specify supported protocol, no unknown flags or bad count */
106 return (proto == IPPROTO_TCP || proto == IPPROTO_UDP
107 || proto == IPPROTO_UDPLITE
108 || proto == IPPROTO_SCTP || proto == IPPROTO_DCCP)
109 && !(ip_invflags & XT_INV_PROTO)
110 && (match_flags == XT_MULTIPORT_SOURCE
111 || match_flags == XT_MULTIPORT_DESTINATION
112 || match_flags == XT_MULTIPORT_EITHER)
113 && count <= XT_MULTI_PORTS;
114 }
115
116 static int multiport_mt_check(const struct xt_mtchk_param *par)
117 {
118 const struct ipt_ip *ip = par->entryinfo;
119 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
120
121 return check(ip->proto, ip->invflags, multiinfo->flags,
122 multiinfo->count) ? 0 : -EINVAL;
123 }
124
125 static int multiport_mt6_check(const struct xt_mtchk_param *par)
126 {
127 const struct ip6t_ip6 *ip = par->entryinfo;
128 const struct xt_multiport_v1 *multiinfo = par->matchinfo;
129
130 return check(ip->proto, ip->invflags, multiinfo->flags,
131 multiinfo->count) ? 0 : -EINVAL;
132 }
133
134 static struct xt_match multiport_mt_reg[] __read_mostly = {
135 {
136 .name = "multiport",
137 .family = NFPROTO_IPV4,
138 .revision = 1,
139 .checkentry = multiport_mt_check,
140 .match = multiport_mt,
141 .matchsize = sizeof(struct xt_multiport_v1),
142 .me = THIS_MODULE,
143 },
144 {
145 .name = "multiport",
146 .family = NFPROTO_IPV6,
147 .revision = 1,
148 .checkentry = multiport_mt6_check,
149 .match = multiport_mt,
150 .matchsize = sizeof(struct xt_multiport_v1),
151 .me = THIS_MODULE,
152 },
153 };
154
155 static int __init multiport_mt_init(void)
156 {
157 return xt_register_matches(multiport_mt_reg,
158 ARRAY_SIZE(multiport_mt_reg));
159 }
160
161 static void __exit multiport_mt_exit(void)
162 {
163 xt_unregister_matches(multiport_mt_reg, ARRAY_SIZE(multiport_mt_reg));
164 }
165
166 module_init(multiport_mt_init);
167 module_exit(multiport_mt_exit);