ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_multiport.c
1 /* Kernel module to match one of a list of TCP/UDP ports: ports are in
2    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
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_ipv6/ip6t_multiport.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
20
21 MODULE_LICENSE("GPL");
22 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
23 MODULE_DESCRIPTION("ip6tables match for multiple ports");
24
25 #if 0
26 #define duprintf(format, args...) printk(format , ## args)
27 #else
28 #define duprintf(format, args...)
29 #endif
30
31 /* Returns 1 if the port is matched by the test, 0 otherwise. */
32 static inline int
33 ports_match(const u_int16_t *portlist, enum ip6t_multiport_flags flags,
34             u_int8_t count, u_int16_t src, u_int16_t dst)
35 {
36         unsigned int i;
37         for (i=0; i<count; i++) {
38                 if (flags != IP6T_MULTIPORT_DESTINATION
39                     && portlist[i] == src)
40                         return 1;
41
42                 if (flags != IP6T_MULTIPORT_SOURCE
43                     && portlist[i] == dst)
44                         return 1;
45         }
46
47         return 0;
48 }
49
50 static int
51 match(const struct sk_buff *skb,
52       const struct net_device *in,
53       const struct net_device *out,
54       const void *matchinfo,
55       int offset,
56       const void *hdr,
57       u_int16_t datalen,
58       int *hotdrop)
59 {
60         const struct udphdr *udp = hdr;
61         const struct ip6t_multiport *multiinfo = matchinfo;
62
63         /* Must be big enough to read ports. */
64         if (offset == 0 && datalen < sizeof(struct udphdr)) {
65                 /* We've been asked to examine this packet, and we
66                    can't.  Hence, no choice but to drop. */
67                         duprintf("ip6t_multiport:"
68                                  " Dropping evil offset=0 tinygram.\n");
69                         *hotdrop = 1;
70                         return 0;
71         }
72
73         /* Must not be a fragment. */
74         return !offset
75                 && ports_match(multiinfo->ports,
76                                multiinfo->flags, multiinfo->count,
77                                ntohs(udp->source), ntohs(udp->dest));
78 }
79
80 /* Called when user tries to insert an entry of this type. */
81 static int
82 checkentry(const char *tablename,
83            const struct ip6t_ip6 *ip,
84            void *matchinfo,
85            unsigned int matchsize,
86            unsigned int hook_mask)
87 {
88         const struct ip6t_multiport *multiinfo = matchinfo;
89
90         /* Must specify proto == TCP/UDP, no unknown flags or bad count */
91         return (ip->proto == IPPROTO_TCP || ip->proto == IPPROTO_UDP)
92                 && !(ip->flags & IP6T_INV_PROTO)
93                 && matchsize == IP6T_ALIGN(sizeof(struct ip6t_multiport))
94                 && (multiinfo->flags == IP6T_MULTIPORT_SOURCE
95                     || multiinfo->flags == IP6T_MULTIPORT_DESTINATION
96                     || multiinfo->flags == IP6T_MULTIPORT_EITHER)
97                 && multiinfo->count <= IP6T_MULTI_PORTS;
98 }
99
100 static struct ip6t_match multiport_match = {
101         .name           = "multiport",
102         .match          = &match,
103         .checkentry     = &checkentry,
104         .me             = THIS_MODULE,
105 };
106
107 static int __init init(void)
108 {
109         return ip6t_register_match(&multiport_match);
110 }
111
112 static void __exit fini(void)
113 {
114         ip6t_unregister_match(&multiport_match);
115 }
116
117 module_init(init);
118 module_exit(fini);