Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* ipv6header match - matches IPv6 packets based |
2 | on whether they contain certain headers */ | |
3 | ||
1ab1457c | 4 | /* Original idea: Brad Chapman |
1da177e4 LT |
5 | * Rewritten by: Andras Kis-Szabo <kisza@sch.bme.hu> */ |
6 | ||
7 | /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu> | |
8 | * | |
9 | * This program is free software; you can redistribute it and/or modify | |
10 | * it under the terms of the GNU General Public License version 2 as | |
11 | * published by the Free Software Foundation. | |
12 | */ | |
13 | ||
14 | #include <linux/module.h> | |
15 | #include <linux/skbuff.h> | |
16 | #include <linux/ipv6.h> | |
17 | #include <linux/types.h> | |
18 | #include <net/checksum.h> | |
19 | #include <net/ipv6.h> | |
20 | ||
6709dbbb | 21 | #include <linux/netfilter/x_tables.h> |
1da177e4 LT |
22 | #include <linux/netfilter_ipv6/ip6_tables.h> |
23 | #include <linux/netfilter_ipv6/ip6t_ipv6header.h> | |
24 | ||
25 | MODULE_LICENSE("GPL"); | |
2ae15b64 | 26 | MODULE_DESCRIPTION("Xtables: IPv6 header types match"); |
1da177e4 LT |
27 | MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>"); |
28 | ||
1d93a9cb | 29 | static bool |
d3c5ee6d JE |
30 | ipv6header_mt6(const struct sk_buff *skb, const struct net_device *in, |
31 | const struct net_device *out, const struct xt_match *match, | |
32 | const void *matchinfo, int offset, unsigned int protoff, | |
33 | bool *hotdrop) | |
1da177e4 LT |
34 | { |
35 | const struct ip6t_ipv6header_info *info = matchinfo; | |
36 | unsigned int temp; | |
37 | int len; | |
38 | u8 nexthdr; | |
39 | unsigned int ptr; | |
40 | ||
41 | /* Make sure this isn't an evil packet */ | |
42 | ||
43 | /* type of the 1st exthdr */ | |
0660e03f | 44 | nexthdr = ipv6_hdr(skb)->nexthdr; |
1da177e4 LT |
45 | /* pointer to the 1st exthdr */ |
46 | ptr = sizeof(struct ipv6hdr); | |
47 | /* available length */ | |
48 | len = skb->len - ptr; | |
49 | temp = 0; | |
50 | ||
f0daaa65 | 51 | while (ip6t_ext_hdr(nexthdr)) { |
1da177e4 | 52 | struct ipv6_opt_hdr _hdr, *hp; |
f0daaa65 | 53 | int hdrlen; |
1da177e4 LT |
54 | |
55 | /* Is there enough space for the next ext header? */ | |
f0daaa65 | 56 | if (len < (int)sizeof(struct ipv6_opt_hdr)) |
1d93a9cb | 57 | return false; |
1da177e4 | 58 | /* No more exthdr -> evaluate */ |
f0daaa65 | 59 | if (nexthdr == NEXTHDR_NONE) { |
1da177e4 LT |
60 | temp |= MASK_NONE; |
61 | break; | |
62 | } | |
63 | /* ESP -> evaluate */ | |
f0daaa65 | 64 | if (nexthdr == NEXTHDR_ESP) { |
1da177e4 LT |
65 | temp |= MASK_ESP; |
66 | break; | |
67 | } | |
68 | ||
69 | hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr); | |
70 | BUG_ON(hp == NULL); | |
71 | ||
72 | /* Calculate the header length */ | |
7c4e36bc | 73 | if (nexthdr == NEXTHDR_FRAGMENT) |
f0daaa65 | 74 | hdrlen = 8; |
7c4e36bc | 75 | else if (nexthdr == NEXTHDR_AUTH) |
f0daaa65 YK |
76 | hdrlen = (hp->hdrlen + 2) << 2; |
77 | else | |
78 | hdrlen = ipv6_optlen(hp); | |
1da177e4 LT |
79 | |
80 | /* set the flag */ | |
f0daaa65 YK |
81 | switch (nexthdr) { |
82 | case NEXTHDR_HOP: | |
83 | temp |= MASK_HOPOPTS; | |
84 | break; | |
85 | case NEXTHDR_ROUTING: | |
86 | temp |= MASK_ROUTING; | |
87 | break; | |
88 | case NEXTHDR_FRAGMENT: | |
89 | temp |= MASK_FRAGMENT; | |
90 | break; | |
91 | case NEXTHDR_AUTH: | |
92 | temp |= MASK_AH; | |
93 | break; | |
94 | case NEXTHDR_DEST: | |
95 | temp |= MASK_DSTOPTS; | |
96 | break; | |
97 | default: | |
1d93a9cb | 98 | return false; |
f0daaa65 | 99 | break; |
1da177e4 LT |
100 | } |
101 | ||
f0daaa65 YK |
102 | nexthdr = hp->nexthdr; |
103 | len -= hdrlen; | |
104 | ptr += hdrlen; | |
1da177e4 LT |
105 | if (ptr > skb->len) |
106 | break; | |
f0daaa65 | 107 | } |
1da177e4 | 108 | |
7c4e36bc | 109 | if (nexthdr != NEXTHDR_NONE && nexthdr != NEXTHDR_ESP) |
1da177e4 LT |
110 | temp |= MASK_PROTO; |
111 | ||
112 | if (info->modeflag) | |
113 | return !((temp ^ info->matchflags ^ info->invflags) | |
114 | & info->matchflags); | |
115 | else { | |
116 | if (info->invflags) | |
117 | return temp != info->matchflags; | |
118 | else | |
119 | return temp == info->matchflags; | |
120 | } | |
121 | } | |
122 | ||
ccb79bdc | 123 | static bool |
d3c5ee6d JE |
124 | ipv6header_mt6_check(const char *tablename, const void *ip, |
125 | const struct xt_match *match, void *matchinfo, | |
126 | unsigned int hook_mask) | |
1da177e4 LT |
127 | { |
128 | const struct ip6t_ipv6header_info *info = matchinfo; | |
129 | ||
1da177e4 | 130 | /* invflags is 0 or 0xff in hard mode */ |
f0daaa65 YK |
131 | if ((!info->modeflag) && info->invflags != 0x00 && |
132 | info->invflags != 0xFF) | |
ccb79bdc | 133 | return false; |
1da177e4 | 134 | |
ccb79bdc | 135 | return true; |
1da177e4 LT |
136 | } |
137 | ||
d3c5ee6d | 138 | static struct xt_match ipv6header_mt6_reg __read_mostly = { |
1da177e4 | 139 | .name = "ipv6header", |
6709dbbb | 140 | .family = AF_INET6, |
d3c5ee6d | 141 | .match = ipv6header_mt6, |
7f939713 | 142 | .matchsize = sizeof(struct ip6t_ipv6header_info), |
d3c5ee6d | 143 | .checkentry = ipv6header_mt6_check, |
1da177e4 LT |
144 | .destroy = NULL, |
145 | .me = THIS_MODULE, | |
146 | }; | |
147 | ||
d3c5ee6d | 148 | static int __init ipv6header_mt6_init(void) |
1da177e4 | 149 | { |
d3c5ee6d | 150 | return xt_register_match(&ipv6header_mt6_reg); |
1da177e4 LT |
151 | } |
152 | ||
d3c5ee6d | 153 | static void __exit ipv6header_mt6_exit(void) |
1da177e4 | 154 | { |
d3c5ee6d | 155 | xt_unregister_match(&ipv6header_mt6_reg); |
1da177e4 LT |
156 | } |
157 | ||
d3c5ee6d JE |
158 | module_init(ipv6header_mt6_init); |
159 | module_exit(ipv6header_mt6_exit); |