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