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