1 /* Kernel module to match Hop-by-Hop and Destination parameters. */
3 /* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
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.
11 #include <linux/module.h>
12 #include <linux/skbuff.h>
13 #include <linux/ipv6.h>
14 #include <linux/types.h>
15 #include <net/checksum.h>
18 #include <asm/byteorder.h>
20 #include <linux/netfilter_ipv6/ip6_tables.h>
21 #include <linux/netfilter_ipv6/ip6t_opts.h>
23 #define LOW(n) (n & 0x00FF)
27 MODULE_LICENSE("GPL");
29 MODULE_DESCRIPTION("IPv6 HbH match");
31 MODULE_DESCRIPTION("IPv6 DST match");
33 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
38 #define DEBUGP(format, args...)
44 * 1 -> must drop the packet
45 * 2 -> send ICMP PARM PROB regardless and drop packet
46 * 3 -> Send ICMP if not a multicast address and drop packet
49 * 1 -> can change the routing
51 * 0 -> PAD0 (only 1 byte!)
52 * 1 -> PAD1 LENGTH info (total length = length + 2)
53 * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k )
58 match(const struct sk_buff *skb,
59 const struct net_device *in,
60 const struct net_device *out,
61 const void *matchinfo,
67 struct ipv6_opt_hdr *optsh = NULL;
68 const struct ip6t_opts *optinfo = matchinfo;
73 unsigned int hdrlen = 0;
75 u_int16_t *optdesc = NULL;
77 /* type of the 1st exthdr */
78 nexthdr = skb->nh.ipv6h->nexthdr;
79 /* pointer to the 1st exthdr */
80 ptr = sizeof(struct ipv6hdr);
81 /* available length */
85 while (ip6t_ext_hdr(nexthdr)) {
86 struct ipv6_opt_hdr *hdr;
88 DEBUGP("ipv6_opts header iteration \n");
90 /* Is there enough space for the next ext header? */
91 if (len < (int)sizeof(struct ipv6_opt_hdr))
93 /* No more exthdr -> evaluate */
94 if (nexthdr == NEXTHDR_NONE) {
98 if (nexthdr == NEXTHDR_ESP) {
102 hdr=(void *)(skb->data)+ptr;
104 /* Calculate the header length */
105 if (nexthdr == NEXTHDR_FRAGMENT) {
107 } else if (nexthdr == NEXTHDR_AUTH)
108 hdrlen = (hdr->hdrlen+2)<<2;
110 hdrlen = ipv6_optlen(hdr);
112 /* OPTS -> evaluate */
114 if (nexthdr == NEXTHDR_HOP) {
115 temp |= MASK_HOPOPTS;
117 if (nexthdr == NEXTHDR_DEST) {
118 temp |= MASK_DSTOPTS;
127 case NEXTHDR_ROUTING:
128 case NEXTHDR_FRAGMENT:
133 DEBUGP("ipv6_opts match: unknown nextheader %u\n",nexthdr);
138 nexthdr = hdr->nexthdr;
141 if ( ptr > skb->len ) {
142 DEBUGP("ipv6_opts: new pointer is too large! \n");
147 /* OPTIONS header not found */
149 if ( temp != MASK_HOPOPTS ) return 0;
151 if ( temp != MASK_DSTOPTS ) return 0;
154 if (len < (int)sizeof(struct ipv6_opt_hdr)){
160 /* Packet smaller than it's length field */
164 optsh=(void *)(skb->data)+ptr;
166 DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, optsh->hdrlen);
168 DEBUGP("len %02X %04X %02X ",
169 optinfo->hdrlen, hdrlen,
170 (!(optinfo->flags & IP6T_OPTS_LEN) ||
171 ((optinfo->hdrlen == hdrlen) ^
172 !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
174 ret = (optsh != NULL)
176 (!(optinfo->flags & IP6T_OPTS_LEN) ||
177 ((optinfo->hdrlen == hdrlen) ^
178 !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
183 if ( !(optinfo->flags & IP6T_OPTS_OPTS) ){
185 } else if (optinfo->flags & IP6T_OPTS_NSTRICT) {
186 DEBUGP("Not strict - not implemented");
189 DEBUGP("#%d ",optinfo->optsnr);
190 for(temp=0; temp<optinfo->optsnr; temp++){
191 optdesc = (void *)(skb->data)+ptr;
193 if ( (unsigned char)*optdesc !=
194 (optinfo->opts[temp] & 0xFF00)>>8 ){
195 DEBUGP("Tbad %02X %02X\n",
196 (unsigned char)*optdesc,
197 (optinfo->opts[temp] &
204 if (((optinfo->opts[temp] & 0x00FF) != 0xFF) &&
205 (unsigned char)*optdesc != 0){
206 if ( ntohs((u16)*optdesc) !=
207 optinfo->opts[temp] ){
208 DEBUGP("Lbad %02X %04X %04X\n",
209 (unsigned char)*optdesc,
210 ntohs((u16)*optdesc),
211 optinfo->opts[temp]);
217 /* Step to the next */
218 if ((unsigned char)*optdesc == 0){
223 ptr += LOW(ntohs(*optdesc));
224 hdrlen -= LOW(ntohs(*optdesc));
226 LOW(ntohs(*optdesc)));
228 if (ptr > skb->len || ( !hdrlen &&
229 (temp != optinfo->optsnr - 1))) {
230 DEBUGP("new pointer is too large! \n");
234 if (temp == optinfo->optsnr)
242 /* Called when user tries to insert an entry of this type. */
244 checkentry(const char *tablename,
245 const struct ip6t_ip6 *ip,
247 unsigned int matchinfosize,
248 unsigned int hook_mask)
250 const struct ip6t_opts *optsinfo = matchinfo;
252 if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_opts))) {
253 DEBUGP("ip6t_opts: matchsize %u != %u\n",
254 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_opts)));
257 if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
258 DEBUGP("ip6t_opts: unknown flags %X\n",
266 static struct ip6t_match opts_match = {
273 .checkentry = &checkentry,
276 static int __init init(void)
278 return ip6t_register_match(&opts_match);
281 static void __exit cleanup(void)
283 ip6t_unregister_match(&opts_match);
287 module_exit(cleanup);