1 /* Kernel module to match FRAG parameters. */
3 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
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.
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>
17 #include <linux/netfilter_ipv6/ip6_tables.h>
18 #include <linux/netfilter_ipv6/ip6t_frag.h>
20 MODULE_LICENSE("GPL");
21 MODULE_DESCRIPTION("IPv6 FRAG match");
22 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
27 #define DEBUGP(format, args...)
30 /* Returns 1 if the id is matched by the range, 0 otherwise */
32 id_match(u_int32_t min, u_int32_t max, u_int32_t id, int invert)
35 DEBUGP("frag id_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
37 r=(id >= min && id <= max) ^ invert;
38 DEBUGP(" result %s\n",r? "PASS" : "FAILED");
43 match(const struct sk_buff *skb,
44 const struct net_device *in,
45 const struct net_device *out,
46 const void *matchinfo,
51 struct frag_hdr _frag, *fh = NULL;
52 const struct ip6t_frag *fraginfo = matchinfo;
57 unsigned int hdrlen = 0;
59 /* type of the 1st exthdr */
60 nexthdr = skb->nh.ipv6h->nexthdr;
61 /* pointer to the 1st exthdr */
62 ptr = sizeof(struct ipv6hdr);
63 /* available length */
67 while (ip6t_ext_hdr(nexthdr)) {
68 struct ipv6_opt_hdr _hdr, *hp;
70 DEBUGP("ipv6_frag header iteration \n");
72 /* Is there enough space for the next ext header? */
73 if (len < (int)sizeof(struct ipv6_opt_hdr))
75 /* No more exthdr -> evaluate */
76 if (nexthdr == NEXTHDR_NONE) {
80 if (nexthdr == NEXTHDR_ESP) {
84 hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr);
87 /* Calculate the header length */
88 if (nexthdr == NEXTHDR_FRAGMENT) {
90 } else if (nexthdr == NEXTHDR_AUTH)
91 hdrlen = (hp->hdrlen+2)<<2;
93 hdrlen = ipv6_optlen(hp);
95 /* FRAG -> evaluate */
96 if (nexthdr == NEXTHDR_FRAGMENT) {
97 temp |= MASK_FRAGMENT;
105 case NEXTHDR_ROUTING:
106 case NEXTHDR_FRAGMENT:
111 DEBUGP("ipv6_frag match: unknown nextheader %u\n",nexthdr);
116 nexthdr = hp->nexthdr;
119 if ( ptr > skb->len ) {
120 DEBUGP("ipv6_frag: new pointer too large! \n");
125 /* FRAG header not found */
126 if ( temp != MASK_FRAGMENT ) return 0;
128 if (len < sizeof(struct frag_hdr)){
133 fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
136 DEBUGP("INFO %04X ", fh->frag_off);
137 DEBUGP("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
138 DEBUGP("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
139 DEBUGP("MF %04X ", fh->frag_off & htons(IP6_MF));
140 DEBUGP("ID %u %08X\n", ntohl(fh->identification),
141 ntohl(fh->identification));
143 DEBUGP("IPv6 FRAG id %02X ",
144 (id_match(fraginfo->ids[0], fraginfo->ids[1],
145 ntohl(fh->identification),
146 !!(fraginfo->invflags & IP6T_FRAG_INV_IDS))));
147 DEBUGP("res %02X %02X%04X %02X ",
148 (fraginfo->flags & IP6T_FRAG_RES), fh->reserved,
149 ntohs(fh->frag_off) & 0x6,
150 !((fraginfo->flags & IP6T_FRAG_RES)
151 && (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
152 DEBUGP("first %02X %02X %02X ",
153 (fraginfo->flags & IP6T_FRAG_FST),
154 ntohs(fh->frag_off) & ~0x7,
155 !((fraginfo->flags & IP6T_FRAG_FST)
156 && (ntohs(fh->frag_off) & ~0x7)));
157 DEBUGP("mf %02X %02X %02X ",
158 (fraginfo->flags & IP6T_FRAG_MF),
159 ntohs(fh->frag_off) & IP6_MF,
160 !((fraginfo->flags & IP6T_FRAG_MF)
161 && !((ntohs(fh->frag_off) & IP6_MF))));
162 DEBUGP("last %02X %02X %02X\n",
163 (fraginfo->flags & IP6T_FRAG_NMF),
164 ntohs(fh->frag_off) & IP6_MF,
165 !((fraginfo->flags & IP6T_FRAG_NMF)
166 && (ntohs(fh->frag_off) & IP6_MF)));
170 (id_match(fraginfo->ids[0], fraginfo->ids[1],
171 ntohl(fh->identification),
172 !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)))
174 !((fraginfo->flags & IP6T_FRAG_RES)
175 && (fh->reserved || (ntohs(fh->frag_off) & 0x6)))
177 !((fraginfo->flags & IP6T_FRAG_FST)
178 && (ntohs(fh->frag_off) & ~0x7))
180 !((fraginfo->flags & IP6T_FRAG_MF)
181 && !(ntohs(fh->frag_off) & IP6_MF))
183 !((fraginfo->flags & IP6T_FRAG_NMF)
184 && (ntohs(fh->frag_off) & IP6_MF));
187 /* Called when user tries to insert an entry of this type. */
189 checkentry(const char *tablename,
190 const struct ip6t_ip6 *ip,
192 unsigned int matchinfosize,
193 unsigned int hook_mask)
195 const struct ip6t_frag *fraginfo = matchinfo;
197 if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_frag))) {
198 DEBUGP("ip6t_frag: matchsize %u != %u\n",
199 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_frag)));
202 if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
203 DEBUGP("ip6t_frag: unknown flags %X\n",
211 static struct ip6t_match frag_match = {
214 .checkentry = &checkentry,
218 static int __init init(void)
220 return ip6t_register_match(&frag_match);
223 static void __exit cleanup(void)
225 ip6t_unregister_match(&frag_match);
229 module_exit(cleanup);