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