linux 2.6.16.38 w/ vs2.0.3-rc1
[linux-2.6.git] / net / ipv6 / netfilter / ip6t_frag.c
1 /* Kernel module to match FRAG parameters. */
2
3 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/ipv6.h>
13 #include <linux/types.h>
14 #include <net/checksum.h>
15 #include <net/ipv6.h>
16
17 #include <linux/netfilter_ipv6/ip6_tables.h>
18 #include <linux/netfilter_ipv6/ip6t_frag.h>
19
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("IPv6 FRAG match");
22 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
23
24 #if 0
25 #define DEBUGP printk
26 #else
27 #define DEBUGP(format, args...)
28 #endif
29
30 /* Returns 1 if the id is matched by the range, 0 otherwise */
31 static inline int
32 id_match(u_int32_t min, u_int32_t max, u_int32_t id, int invert)
33 {
34         int r = 0;
35         DEBUGP("frag id_match:%c 0x%x <= 0x%x <= 0x%x", invert ? '!' : ' ',
36                min, id, max);
37         r = (id >= min && id <= max) ^ invert;
38         DEBUGP(" result %s\n", r ? "PASS" : "FAILED");
39         return r;
40 }
41
42 static int
43 match(const struct sk_buff *skb,
44       const struct net_device *in,
45       const struct net_device *out,
46       const void *matchinfo,
47       int offset,
48       unsigned int protoff,
49       int *hotdrop)
50 {
51         struct frag_hdr _frag, *fh;
52         const struct ip6t_frag *fraginfo = matchinfo;
53         unsigned int ptr;
54         int err;
55
56         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL);
57         if (err < 0) {
58                 if (err != -ENOENT)
59                         *hotdrop = 1;
60                 return 0;
61         }
62
63         fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
64         if (fh == NULL) {
65                 *hotdrop = 1;
66                 return 0;
67         }
68
69         DEBUGP("INFO %04X ", fh->frag_off);
70         DEBUGP("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
71         DEBUGP("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
72         DEBUGP("MF %04X ", fh->frag_off & htons(IP6_MF));
73         DEBUGP("ID %u %08X\n", ntohl(fh->identification),
74                ntohl(fh->identification));
75
76         DEBUGP("IPv6 FRAG id %02X ",
77                (id_match(fraginfo->ids[0], fraginfo->ids[1],
78                          ntohl(fh->identification),
79                          !!(fraginfo->invflags & IP6T_FRAG_INV_IDS))));
80         DEBUGP("res %02X %02X%04X %02X ",
81                (fraginfo->flags & IP6T_FRAG_RES), fh->reserved,
82                ntohs(fh->frag_off) & 0x6,
83                !((fraginfo->flags & IP6T_FRAG_RES)
84                  && (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
85         DEBUGP("first %02X %02X %02X ",
86                (fraginfo->flags & IP6T_FRAG_FST),
87                ntohs(fh->frag_off) & ~0x7,
88                !((fraginfo->flags & IP6T_FRAG_FST)
89                  && (ntohs(fh->frag_off) & ~0x7)));
90         DEBUGP("mf %02X %02X %02X ",
91                (fraginfo->flags & IP6T_FRAG_MF),
92                ntohs(fh->frag_off) & IP6_MF,
93                !((fraginfo->flags & IP6T_FRAG_MF)
94                  && !((ntohs(fh->frag_off) & IP6_MF))));
95         DEBUGP("last %02X %02X %02X\n",
96                (fraginfo->flags & IP6T_FRAG_NMF),
97                ntohs(fh->frag_off) & IP6_MF,
98                !((fraginfo->flags & IP6T_FRAG_NMF)
99                  && (ntohs(fh->frag_off) & IP6_MF)));
100
101         return (fh != NULL)
102                &&
103                (id_match(fraginfo->ids[0], fraginfo->ids[1],
104                          ntohl(fh->identification),
105                          !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)))
106                &&
107                !((fraginfo->flags & IP6T_FRAG_RES)
108                  && (fh->reserved || (ntohs(fh->frag_off) & 0x6)))
109                &&
110                !((fraginfo->flags & IP6T_FRAG_FST)
111                  && (ntohs(fh->frag_off) & ~0x7))
112                &&
113                !((fraginfo->flags & IP6T_FRAG_MF)
114                  && !(ntohs(fh->frag_off) & IP6_MF))
115                &&
116                !((fraginfo->flags & IP6T_FRAG_NMF)
117                  && (ntohs(fh->frag_off) & IP6_MF));
118 }
119
120 /* Called when user tries to insert an entry of this type. */
121 static int
122 checkentry(const char *tablename,
123            const void *ip,
124            void *matchinfo,
125            unsigned int matchinfosize,
126            unsigned int hook_mask)
127 {
128         const struct ip6t_frag *fraginfo = matchinfo;
129
130         if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_frag))) {
131                 DEBUGP("ip6t_frag: matchsize %u != %u\n",
132                        matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_frag)));
133                 return 0;
134         }
135         if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
136                 DEBUGP("ip6t_frag: unknown flags %X\n", fraginfo->invflags);
137                 return 0;
138         }
139
140         return 1;
141 }
142
143 static struct ip6t_match frag_match = {
144         .name           = "frag",
145         .match          = &match,
146         .checkentry     = &checkentry,
147         .me             = THIS_MODULE,
148 };
149
150 static int __init init(void)
151 {
152         return ip6t_register_match(&frag_match);
153 }
154
155 static void __exit cleanup(void)
156 {
157         ip6t_unregister_match(&frag_match);
158 }
159
160 module_init(init);
161 module_exit(cleanup);