Pull dock-bay into release branch
[pandora-kernel.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/x_tables.h>
18 #include <linux/netfilter_ipv6/ip6_tables.h>
19 #include <linux/netfilter_ipv6/ip6t_frag.h>
20
21 MODULE_LICENSE("GPL");
22 MODULE_DESCRIPTION("IPv6 FRAG match");
23 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
24
25 /* Returns 1 if the id is matched by the range, 0 otherwise */
26 static inline bool
27 id_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
28 {
29         bool r;
30         pr_debug("frag id_match:%c 0x%x <= 0x%x <= 0x%x", invert ? '!' : ' ',
31                  min, id, max);
32         r = (id >= min && id <= max) ^ invert;
33         pr_debug(" result %s\n", r ? "PASS" : "FAILED");
34         return r;
35 }
36
37 static bool
38 match(const struct sk_buff *skb,
39       const struct net_device *in,
40       const struct net_device *out,
41       const struct xt_match *match,
42       const void *matchinfo,
43       int offset,
44       unsigned int protoff,
45       bool *hotdrop)
46 {
47         struct frag_hdr _frag;
48         const struct frag_hdr *fh;
49         const struct ip6t_frag *fraginfo = matchinfo;
50         unsigned int ptr;
51         int err;
52
53         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL);
54         if (err < 0) {
55                 if (err != -ENOENT)
56                         *hotdrop = true;
57                 return false;
58         }
59
60         fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
61         if (fh == NULL) {
62                 *hotdrop = true;
63                 return false;
64         }
65
66         pr_debug("INFO %04X ", fh->frag_off);
67         pr_debug("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
68         pr_debug("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
69         pr_debug("MF %04X ", fh->frag_off & htons(IP6_MF));
70         pr_debug("ID %u %08X\n", ntohl(fh->identification),
71                  ntohl(fh->identification));
72
73         pr_debug("IPv6 FRAG id %02X ",
74                  id_match(fraginfo->ids[0], fraginfo->ids[1],
75                           ntohl(fh->identification),
76                           !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)));
77         pr_debug("res %02X %02X%04X %02X ",
78                  fraginfo->flags & IP6T_FRAG_RES, fh->reserved,
79                  ntohs(fh->frag_off) & 0x6,
80                  !((fraginfo->flags & IP6T_FRAG_RES)
81                    && (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
82         pr_debug("first %02X %02X %02X ",
83                  fraginfo->flags & IP6T_FRAG_FST,
84                  ntohs(fh->frag_off) & ~0x7,
85                  !((fraginfo->flags & IP6T_FRAG_FST)
86                    && (ntohs(fh->frag_off) & ~0x7)));
87         pr_debug("mf %02X %02X %02X ",
88                  fraginfo->flags & IP6T_FRAG_MF,
89                  ntohs(fh->frag_off) & IP6_MF,
90                  !((fraginfo->flags & IP6T_FRAG_MF)
91                    && !((ntohs(fh->frag_off) & IP6_MF))));
92         pr_debug("last %02X %02X %02X\n",
93                  fraginfo->flags & IP6T_FRAG_NMF,
94                  ntohs(fh->frag_off) & IP6_MF,
95                  !((fraginfo->flags & IP6T_FRAG_NMF)
96                    && (ntohs(fh->frag_off) & IP6_MF)));
97
98         return (fh != NULL)
99                &&
100                id_match(fraginfo->ids[0], fraginfo->ids[1],
101                         ntohl(fh->identification),
102                         !!(fraginfo->invflags & IP6T_FRAG_INV_IDS))
103                &&
104                !((fraginfo->flags & IP6T_FRAG_RES)
105                  && (fh->reserved || (ntohs(fh->frag_off) & 0x6)))
106                &&
107                !((fraginfo->flags & IP6T_FRAG_FST)
108                  && (ntohs(fh->frag_off) & ~0x7))
109                &&
110                !((fraginfo->flags & IP6T_FRAG_MF)
111                  && !(ntohs(fh->frag_off) & IP6_MF))
112                &&
113                !((fraginfo->flags & IP6T_FRAG_NMF)
114                  && (ntohs(fh->frag_off) & IP6_MF));
115 }
116
117 /* Called when user tries to insert an entry of this type. */
118 static bool
119 checkentry(const char *tablename,
120            const void *ip,
121            const struct xt_match *match,
122            void *matchinfo,
123            unsigned int hook_mask)
124 {
125         const struct ip6t_frag *fraginfo = matchinfo;
126
127         if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
128                 pr_debug("ip6t_frag: unknown flags %X\n", fraginfo->invflags);
129                 return false;
130         }
131         return true;
132 }
133
134 static struct xt_match frag_match __read_mostly = {
135         .name           = "frag",
136         .family         = AF_INET6,
137         .match          = match,
138         .matchsize      = sizeof(struct ip6t_frag),
139         .checkentry     = checkentry,
140         .me             = THIS_MODULE,
141 };
142
143 static int __init ip6t_frag_init(void)
144 {
145         return xt_register_match(&frag_match);
146 }
147
148 static void __exit ip6t_frag_fini(void)
149 {
150         xt_unregister_match(&frag_match);
151 }
152
153 module_init(ip6t_frag_init);
154 module_exit(ip6t_frag_fini);