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 |
f7108a20 | 30 | ipv6header_mt6(const struct sk_buff *skb, const struct xt_match_param *par) |
1da177e4 | 31 | { |
f7108a20 | 32 | const struct ip6t_ipv6header_info *info = par->matchinfo; |
1da177e4 LT |
33 | unsigned int temp; |
34 | int len; | |
35 | u8 nexthdr; | |
36 | unsigned int ptr; | |
37 | ||
38 | /* Make sure this isn't an evil packet */ | |
39 | ||
40 | /* type of the 1st exthdr */ | |
0660e03f | 41 | nexthdr = ipv6_hdr(skb)->nexthdr; |
1da177e4 LT |
42 | /* pointer to the 1st exthdr */ |
43 | ptr = sizeof(struct ipv6hdr); | |
44 | /* available length */ | |
45 | len = skb->len - ptr; | |
46 | temp = 0; | |
47 | ||
f0daaa65 | 48 | while (ip6t_ext_hdr(nexthdr)) { |
3cf93c96 JE |
49 | const struct ipv6_opt_hdr *hp; |
50 | struct ipv6_opt_hdr _hdr; | |
f0daaa65 | 51 | int hdrlen; |
1da177e4 LT |
52 | |
53 | /* Is there enough space for the next ext header? */ | |
f0daaa65 | 54 | if (len < (int)sizeof(struct ipv6_opt_hdr)) |
1d93a9cb | 55 | return false; |
1da177e4 | 56 | /* No more exthdr -> evaluate */ |
f0daaa65 | 57 | if (nexthdr == NEXTHDR_NONE) { |
1da177e4 LT |
58 | temp |= MASK_NONE; |
59 | break; | |
60 | } | |
61 | /* ESP -> evaluate */ | |
f0daaa65 | 62 | if (nexthdr == NEXTHDR_ESP) { |
1da177e4 LT |
63 | temp |= MASK_ESP; |
64 | break; | |
65 | } | |
66 | ||
67 | hp = skb_header_pointer(skb, ptr, sizeof(_hdr), &_hdr); | |
68 | BUG_ON(hp == NULL); | |
69 | ||
70 | /* Calculate the header length */ | |
7c4e36bc | 71 | if (nexthdr == NEXTHDR_FRAGMENT) |
f0daaa65 | 72 | hdrlen = 8; |
7c4e36bc | 73 | else if (nexthdr == NEXTHDR_AUTH) |
f0daaa65 YK |
74 | hdrlen = (hp->hdrlen + 2) << 2; |
75 | else | |
76 | hdrlen = ipv6_optlen(hp); | |
1da177e4 LT |
77 | |
78 | /* set the flag */ | |
f0daaa65 YK |
79 | switch (nexthdr) { |
80 | case NEXTHDR_HOP: | |
81 | temp |= MASK_HOPOPTS; | |
82 | break; | |
83 | case NEXTHDR_ROUTING: | |
84 | temp |= MASK_ROUTING; | |
85 | break; | |
86 | case NEXTHDR_FRAGMENT: | |
87 | temp |= MASK_FRAGMENT; | |
88 | break; | |
89 | case NEXTHDR_AUTH: | |
90 | temp |= MASK_AH; | |
91 | break; | |
92 | case NEXTHDR_DEST: | |
93 | temp |= MASK_DSTOPTS; | |
94 | break; | |
95 | default: | |
1d93a9cb | 96 | return false; |
f0daaa65 | 97 | break; |
1da177e4 LT |
98 | } |
99 | ||
f0daaa65 YK |
100 | nexthdr = hp->nexthdr; |
101 | len -= hdrlen; | |
102 | ptr += hdrlen; | |
1da177e4 LT |
103 | if (ptr > skb->len) |
104 | break; | |
f0daaa65 | 105 | } |
1da177e4 | 106 | |
7c4e36bc | 107 | if (nexthdr != NEXTHDR_NONE && nexthdr != NEXTHDR_ESP) |
1da177e4 LT |
108 | temp |= MASK_PROTO; |
109 | ||
110 | if (info->modeflag) | |
111 | return !((temp ^ info->matchflags ^ info->invflags) | |
112 | & info->matchflags); | |
113 | else { | |
114 | if (info->invflags) | |
115 | return temp != info->matchflags; | |
116 | else | |
117 | return temp == info->matchflags; | |
118 | } | |
119 | } | |
120 | ||
ccb79bdc | 121 | static bool |
d3c5ee6d JE |
122 | ipv6header_mt6_check(const char *tablename, const void *ip, |
123 | const struct xt_match *match, void *matchinfo, | |
124 | unsigned int hook_mask) | |
1da177e4 LT |
125 | { |
126 | const struct ip6t_ipv6header_info *info = matchinfo; | |
127 | ||
1da177e4 | 128 | /* invflags is 0 or 0xff in hard mode */ |
f0daaa65 YK |
129 | if ((!info->modeflag) && info->invflags != 0x00 && |
130 | info->invflags != 0xFF) | |
ccb79bdc | 131 | return false; |
1da177e4 | 132 | |
ccb79bdc | 133 | return true; |
1da177e4 LT |
134 | } |
135 | ||
d3c5ee6d | 136 | static struct xt_match ipv6header_mt6_reg __read_mostly = { |
1da177e4 | 137 | .name = "ipv6header", |
ee999d8b | 138 | .family = NFPROTO_IPV6, |
d3c5ee6d | 139 | .match = ipv6header_mt6, |
7f939713 | 140 | .matchsize = sizeof(struct ip6t_ipv6header_info), |
d3c5ee6d | 141 | .checkentry = ipv6header_mt6_check, |
1da177e4 LT |
142 | .destroy = NULL, |
143 | .me = THIS_MODULE, | |
144 | }; | |
145 | ||
d3c5ee6d | 146 | static int __init ipv6header_mt6_init(void) |
1da177e4 | 147 | { |
d3c5ee6d | 148 | return xt_register_match(&ipv6header_mt6_reg); |
1da177e4 LT |
149 | } |
150 | ||
d3c5ee6d | 151 | static void __exit ipv6header_mt6_exit(void) |
1da177e4 | 152 | { |
d3c5ee6d | 153 | xt_unregister_match(&ipv6header_mt6_reg); |
1da177e4 LT |
154 | } |
155 | ||
d3c5ee6d JE |
156 | module_init(ipv6header_mt6_init); |
157 | module_exit(ipv6header_mt6_exit); |