blob: 3b5735e56bfe7b9194f07e0f790867a56c08b136 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080017#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/netfilter_ipv6/ip6_tables.h>
19#include <linux/netfilter_ipv6/ip6t_frag.h>
20
21MODULE_LICENSE("GPL");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080022MODULE_DESCRIPTION("Xtables: IPv6 fragment match");
Linus Torvalds1da177e2005-04-16 15:20:36 -070023MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025/* Returns 1 if the id is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070026static inline bool
27id_match(u_int32_t min, u_int32_t max, u_int32_t id, bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -070028{
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070029 bool r;
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010030 pr_debug("id_match:%c 0x%x <= 0x%x <= 0x%x\n", invert ? '!' : ' ',
Patrick McHardy0d537782007-07-07 22:39:38 -070031 min, id, max);
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080032 r = (id >= min && id <= max) ^ invert;
Patrick McHardy0d537782007-07-07 22:39:38 -070033 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080034 return r;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035}
36
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020038frag_mt6(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039{
Jan Engelhardta47362a2007-07-07 22:16:55 -070040 struct frag_hdr _frag;
41 const struct frag_hdr *fh;
Jan Engelhardtf7108a22008-10-08 11:35:18 +020042 const struct ip6t_frag *fraginfo = par->matchinfo;
Hans Schillstrom84018f52012-04-23 03:35:26 +000043 unsigned int ptr = 0;
Patrick McHardy6d381632006-10-24 16:15:10 -070044 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Hans Schillstrom84018f52012-04-23 03:35:26 +000046 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_FRAGMENT, NULL, NULL);
Patrick McHardy6d381632006-10-24 16:15:10 -070047 if (err < 0) {
48 if (err != -ENOENT)
Jan Engelhardtb4ba2612009-07-07 20:54:30 +020049 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070050 return false;
Patrick McHardy6d381632006-10-24 16:15:10 -070051 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -070053 fh = skb_header_pointer(skb, ptr, sizeof(_frag), &_frag);
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080054 if (fh == NULL) {
Jan Engelhardtb4ba2612009-07-07 20:54:30 +020055 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070056 return false;
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -070057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Patrick McHardy0d537782007-07-07 22:39:38 -070059 pr_debug("INFO %04X ", fh->frag_off);
60 pr_debug("OFFSET %04X ", ntohs(fh->frag_off) & ~0x7);
61 pr_debug("RES %02X %04X", fh->reserved, ntohs(fh->frag_off) & 0x6);
62 pr_debug("MF %04X ", fh->frag_off & htons(IP6_MF));
63 pr_debug("ID %u %08X\n", ntohl(fh->identification),
64 ntohl(fh->identification));
Linus Torvalds1da177e2005-04-16 15:20:36 -070065
Patrick McHardy0d537782007-07-07 22:39:38 -070066 pr_debug("IPv6 FRAG id %02X ",
67 id_match(fraginfo->ids[0], fraginfo->ids[1],
68 ntohl(fh->identification),
69 !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)));
70 pr_debug("res %02X %02X%04X %02X ",
71 fraginfo->flags & IP6T_FRAG_RES, fh->reserved,
72 ntohs(fh->frag_off) & 0x6,
Joe Perches3666ed12009-11-23 23:17:06 +010073 !((fraginfo->flags & IP6T_FRAG_RES) &&
74 (fh->reserved || (ntohs(fh->frag_off) & 0x06))));
Patrick McHardy0d537782007-07-07 22:39:38 -070075 pr_debug("first %02X %02X %02X ",
76 fraginfo->flags & IP6T_FRAG_FST,
77 ntohs(fh->frag_off) & ~0x7,
Joe Perches3666ed12009-11-23 23:17:06 +010078 !((fraginfo->flags & IP6T_FRAG_FST) &&
79 (ntohs(fh->frag_off) & ~0x7)));
Patrick McHardy0d537782007-07-07 22:39:38 -070080 pr_debug("mf %02X %02X %02X ",
81 fraginfo->flags & IP6T_FRAG_MF,
82 ntohs(fh->frag_off) & IP6_MF,
Joe Perches3666ed12009-11-23 23:17:06 +010083 !((fraginfo->flags & IP6T_FRAG_MF) &&
84 !((ntohs(fh->frag_off) & IP6_MF))));
Patrick McHardy0d537782007-07-07 22:39:38 -070085 pr_debug("last %02X %02X %02X\n",
86 fraginfo->flags & IP6T_FRAG_NMF,
87 ntohs(fh->frag_off) & IP6_MF,
Joe Perches3666ed12009-11-23 23:17:06 +010088 !((fraginfo->flags & IP6T_FRAG_NMF) &&
89 (ntohs(fh->frag_off) & IP6_MF)));
Linus Torvalds1da177e2005-04-16 15:20:36 -070090
Joe Perches3666ed12009-11-23 23:17:06 +010091 return (fh != NULL) &&
92 id_match(fraginfo->ids[0], fraginfo->ids[1],
93 ntohl(fh->identification),
94 !!(fraginfo->invflags & IP6T_FRAG_INV_IDS)) &&
95 !((fraginfo->flags & IP6T_FRAG_RES) &&
96 (fh->reserved || (ntohs(fh->frag_off) & 0x6))) &&
97 !((fraginfo->flags & IP6T_FRAG_FST) &&
98 (ntohs(fh->frag_off) & ~0x7)) &&
99 !((fraginfo->flags & IP6T_FRAG_MF) &&
100 !(ntohs(fh->frag_off) & IP6_MF)) &&
101 !((fraginfo->flags & IP6T_FRAG_NMF) &&
102 (ntohs(fh->frag_off) & IP6_MF));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100105static int frag_mt6_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200107 const struct ip6t_frag *fraginfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -0800109 if (fraginfo->invflags & ~IP6T_FRAG_INV_MASK) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +0100110 pr_debug("unknown flags %X\n", fraginfo->invflags);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100111 return -EINVAL;
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -0800112 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100113 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114}
115
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800116static struct xt_match frag_mt6_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 .name = "frag",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200118 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800119 .match = frag_mt6,
Patrick McHardy7f939712006-03-20 18:01:43 -0800120 .matchsize = sizeof(struct ip6t_frag),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800121 .checkentry = frag_mt6_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 .me = THIS_MODULE,
123};
124
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800125static int __init frag_mt6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800127 return xt_register_match(&frag_mt6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128}
129
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800130static void __exit frag_mt6_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800132 xt_unregister_match(&frag_mt6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800135module_init(frag_mt6_init);
136module_exit(frag_mt6_exit);