2005-04-16 22:20:36 +00:00
|
|
|
/* Kernel module to match FRAG parameters. */
|
|
|
|
|
|
|
|
/* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License version 2 as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/skbuff.h>
|
|
|
|
#include <linux/ipv6.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <net/checksum.h>
|
|
|
|
#include <net/ipv6.h>
|
|
|
|
|
2007-02-07 23:11:19 +00:00
|
|
|
#include <linux/netfilter/x_tables.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/netfilter_ipv6/ip6_tables.h>
|
|
|
|
#include <linux/netfilter_ipv6/ip6t_frag.h>
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_DESCRIPTION("IPv6 FRAG match");
|
|
|
|
MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
|
|
|
|
|
|
|
|
/* Returns 1 if the id is matched by the range, 0 otherwise */
|
2007-07-08 05:15:35 +00:00
|
|
|
static inline bool
|
|
|
|
id_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-07-08 05:15:35 +00:00
|
|
|
bool r;
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("frag id_match:%c 0x%x <= 0x%x <= 0x%x", invert ? '!' : ' ',
|
|
|
|
min, id, max);
|
2006-01-17 10:39:39 +00:00
|
|
|
r = (id >= min && id <= max) ^ invert;
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug(" result %s\n", r ? "PASS" : "FAILED");
|
2006-01-17 10:39:39 +00:00
|
|
|
return r;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:15:35 +00:00
|
|
|
static bool
|
2005-04-16 22:20:36 +00:00
|
|
|
match(const struct sk_buff *skb,
|
|
|
|
const struct net_device *in,
|
|
|
|
const struct net_device *out,
|
2006-03-21 02:02:56 +00:00
|
|
|
const struct xt_match *match,
|
2005-04-16 22:20:36 +00:00
|
|
|
const void *matchinfo,
|
|
|
|
int offset,
|
|
|
|
unsigned int protoff,
|
2007-07-08 05:15:12 +00:00
|
|
|
bool *hotdrop)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-07-08 05:16:55 +00:00
|
|
|
struct frag_hdr _frag;
|
|
|
|
const struct frag_hdr *fh;
|
2006-01-17 10:39:39 +00:00
|
|
|
const struct ip6t_frag *fraginfo = matchinfo;
|
|
|
|
unsigned int ptr;
|
2006-10-24 23:15:10 +00:00
|
|
|
int err;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-10-24 23:15:10 +00:00
|
|
|
err = ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL);
|
|
|
|
if (err < 0) {
|
|
|
|
if (err != -ENOENT)
|
2007-07-08 05:15:12 +00:00
|
|
|
*hotdrop = true;
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2006-10-24 23:15:10 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2005-09-19 22:34:40 +00:00
|
|
|
fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
|
2006-01-17 10:39:39 +00:00
|
|
|
if (fh == NULL) {
|
2007-07-08 05:15:12 +00:00
|
|
|
*hotdrop = true;
|
2007-07-08 05:15:35 +00:00
|
|
|
return false;
|
2005-09-19 22:34:40 +00:00
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("INFO %04X ", fh->frag_off);
|
|
|
|
pr_debug("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
|
|
|
|
pr_debug("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
|
|
|
|
pr_debug("MF %04X ", fh->frag_off & htons(IP6_MF));
|
|
|
|
pr_debug("ID %u %08X\n", ntohl(fh->identification),
|
|
|
|
ntohl(fh->identification));
|
|
|
|
|
|
|
|
pr_debug("IPv6 FRAG id %02X ",
|
|
|
|
id_match(fraginfo->ids[0], fraginfo->ids[1],
|
|
|
|
ntohl(fh->identification),
|
|
|
|
!!(fraginfo->invflags & IP6T_FRAG_INV_IDS)));
|
|
|
|
pr_debug("res %02X %02X%04X %02X ",
|
|
|
|
fraginfo->flags & IP6T_FRAG_RES, fh->reserved,
|
|
|
|
ntohs(fh->frag_off) & 0x6,
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_RES)
|
|
|
|
&& (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
|
|
|
|
pr_debug("first %02X %02X %02X ",
|
|
|
|
fraginfo->flags & IP6T_FRAG_FST,
|
|
|
|
ntohs(fh->frag_off) & ~0x7,
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_FST)
|
|
|
|
&& (ntohs(fh->frag_off) & ~0x7)));
|
|
|
|
pr_debug("mf %02X %02X %02X ",
|
|
|
|
fraginfo->flags & IP6T_FRAG_MF,
|
|
|
|
ntohs(fh->frag_off) & IP6_MF,
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_MF)
|
|
|
|
&& !((ntohs(fh->frag_off) & IP6_MF))));
|
|
|
|
pr_debug("last %02X %02X %02X\n",
|
|
|
|
fraginfo->flags & IP6T_FRAG_NMF,
|
|
|
|
ntohs(fh->frag_off) & IP6_MF,
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_NMF)
|
|
|
|
&& (ntohs(fh->frag_off) & IP6_MF)));
|
2006-01-17 10:39:39 +00:00
|
|
|
|
|
|
|
return (fh != NULL)
|
|
|
|
&&
|
2007-07-08 05:19:08 +00:00
|
|
|
id_match(fraginfo->ids[0], fraginfo->ids[1],
|
|
|
|
ntohl(fh->identification),
|
|
|
|
!!(fraginfo->invflags & IP6T_FRAG_INV_IDS))
|
2006-01-17 10:39:39 +00:00
|
|
|
&&
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_RES)
|
|
|
|
&& (fh->reserved || (ntohs(fh->frag_off) & 0x6)))
|
|
|
|
&&
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_FST)
|
|
|
|
&& (ntohs(fh->frag_off) & ~0x7))
|
|
|
|
&&
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_MF)
|
|
|
|
&& !(ntohs(fh->frag_off) & IP6_MF))
|
|
|
|
&&
|
|
|
|
!((fraginfo->flags & IP6T_FRAG_NMF)
|
|
|
|
&& (ntohs(fh->frag_off) & IP6_MF));
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Called when user tries to insert an entry of this type. */
|
2007-07-08 05:16:00 +00:00
|
|
|
static bool
|
2005-04-16 22:20:36 +00:00
|
|
|
checkentry(const char *tablename,
|
2006-01-17 10:39:39 +00:00
|
|
|
const void *ip,
|
2006-03-21 02:02:56 +00:00
|
|
|
const struct xt_match *match,
|
2006-01-17 10:39:39 +00:00
|
|
|
void *matchinfo,
|
|
|
|
unsigned int hook_mask)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2006-01-17 10:39:39 +00:00
|
|
|
const struct ip6t_frag *fraginfo = matchinfo;
|
|
|
|
|
|
|
|
if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
|
2007-07-08 05:39:38 +00:00
|
|
|
pr_debug("ip6t_frag: unknown flags %X\n", fraginfo->invflags);
|
2007-07-08 05:16:00 +00:00
|
|
|
return false;
|
2006-01-17 10:39:39 +00:00
|
|
|
}
|
2007-07-08 05:16:00 +00:00
|
|
|
return true;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2007-07-08 05:22:02 +00:00
|
|
|
static struct xt_match frag_match __read_mostly = {
|
2005-04-16 22:20:36 +00:00
|
|
|
.name = "frag",
|
2007-02-07 23:11:19 +00:00
|
|
|
.family = AF_INET6,
|
2006-03-21 02:01:43 +00:00
|
|
|
.match = match,
|
|
|
|
.matchsize = sizeof(struct ip6t_frag),
|
|
|
|
.checkentry = checkentry,
|
2005-04-16 22:20:36 +00:00
|
|
|
.me = THIS_MODULE,
|
|
|
|
};
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
static int __init ip6t_frag_init(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-02-07 23:11:19 +00:00
|
|
|
return xt_register_match(&frag_match);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
static void __exit ip6t_frag_fini(void)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2007-02-07 23:11:19 +00:00
|
|
|
xt_unregister_match(&frag_match);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2006-03-29 00:37:06 +00:00
|
|
|
module_init(ip6t_frag_init);
|
|
|
|
module_exit(ip6t_frag_fini);
|