fedora core 6 1.2949 + vserver 2.2.0
[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 struct xt_match *match,
47       const void *matchinfo,
48       int offset,
49       unsigned int protoff,
50       int *hotdrop)
51 {
52         struct frag_hdr _frag, *fh;
53         const struct ip6t_frag *fraginfo = matchinfo;
54         unsigned int ptr;
55         int err;
56
57         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL);
58         if (err < 0) {
59                 if (err != -ENOENT)
60                         *hotdrop = 1;
61                 return 0;
62         }
63
64         fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
65         if (fh == NULL) {
66                 *hotdrop = 1;
67                 return 0;
68         }
69
70         DEBUGP("INFO %04X ", fh->frag_off);
71         DEBUGP("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
72         DEBUGP("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
73         DEBUGP("MF %04X ", fh->frag_off & htons(IP6_MF));
74         DEBUGP("ID %u %08X\n", ntohl(fh->identification),
75                ntohl(fh->identification));
76
77         DEBUGP("IPv6 FRAG id %02X ",
78                (id_match(fraginfo->ids[0], fraginfo->ids[1],
79                          ntohl(fh->identification),
80                          !!(fraginfo->invflags & IP6T_FRAG_INV_IDS))));
81         DEBUGP("res %02X %02X%04X %02X ",
82                (fraginfo->flags & IP6T_FRAG_RES), fh->reserved,
83                ntohs(fh->frag_off) & 0x6,
84                !((fraginfo->flags & IP6T_FRAG_RES)
85                  && (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
86         DEBUGP("first %02X %02X %02X ",
87                (fraginfo->flags & IP6T_FRAG_FST),
88                ntohs(fh->frag_off) & ~0x7,
89                !((fraginfo->flags & IP6T_FRAG_FST)
90                  && (ntohs(fh->frag_off) & ~0x7)));
91         DEBUGP("mf %02X %02X %02X ",
92                (fraginfo->flags & IP6T_FRAG_MF),
93                ntohs(fh->frag_off) & IP6_MF,
94                !((fraginfo->flags & IP6T_FRAG_MF)
95                  && !((ntohs(fh->frag_off) & IP6_MF))));
96         DEBUGP("last %02X %02X %02X\n",
97                (fraginfo->flags & IP6T_FRAG_NMF),
98                ntohs(fh->frag_off) & IP6_MF,
99                !((fraginfo->flags & IP6T_FRAG_NMF)
100                  && (ntohs(fh->frag_off) & IP6_MF)));
101
102         return (fh != NULL)
103                &&
104                (id_match(fraginfo->ids[0], fraginfo->ids[1],
105                          ntohl(fh->identification),
106                          !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)))
107                &&
108                !((fraginfo->flags & IP6T_FRAG_RES)
109                  && (fh->reserved || (ntohs(fh->frag_off) & 0x6)))
110                &&
111                !((fraginfo->flags & IP6T_FRAG_FST)
112                  && (ntohs(fh->frag_off) & ~0x7))
113                &&
114                !((fraginfo->flags & IP6T_FRAG_MF)
115                  && !(ntohs(fh->frag_off) & IP6_MF))
116                &&
117                !((fraginfo->flags & IP6T_FRAG_NMF)
118                  && (ntohs(fh->frag_off) & IP6_MF));
119 }
120
121 /* Called when user tries to insert an entry of this type. */
122 static int
123 checkentry(const char *tablename,
124            const void *ip,
125            const struct xt_match *match,
126            void *matchinfo,
127            unsigned int hook_mask)
128 {
129         const struct ip6t_frag *fraginfo = matchinfo;
130
131         if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
132                 DEBUGP("ip6t_frag: unknown flags %X\n", fraginfo->invflags);
133                 return 0;
134         }
135         return 1;
136 }
137
138 static struct ip6t_match frag_match = {
139         .name           = "frag",
140         .match          = match,
141         .matchsize      = sizeof(struct ip6t_frag),
142         .checkentry     = checkentry,
143         .me             = THIS_MODULE,
144 };
145
146 static int __init ip6t_frag_init(void)
147 {
148         return ip6t_register_match(&frag_match);
149 }
150
151 static void __exit ip6t_frag_fini(void)
152 {
153         ip6t_unregister_match(&frag_match);
154 }
155
156 module_init(ip6t_frag_init);
157 module_exit(ip6t_frag_fini);