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.
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>
17 #include <asm/byteorder.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
20 #include <linux/netfilter_ipv6/ip6t_opts.h>
22 #define LOW(n) (n & 0x00FF)
26 MODULE_LICENSE("GPL");
28 MODULE_DESCRIPTION("IPv6 HbH match");
30 MODULE_DESCRIPTION("IPv6 DST match");
32 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
37 #define DEBUGP(format, args...)
43 * 1 -> must drop the packet
44 * 2 -> send ICMP PARM PROB regardless and drop packet
45 * 3 -> Send ICMP if not a multicast address and drop packet
48 * 1 -> can change the routing
50 * 0 -> PAD0 (only 1 byte!)
51 * 1 -> PAD1 LENGTH info (total length = length + 2)
52 * C0 | 2 -> JUMBO 4 x x x x ( xxxx > 64k )
57 match(const struct sk_buff *skb,
58 const struct net_device *in,
59 const struct net_device *out,
60 const void *matchinfo,
66 struct ipv6_opt_hdr *optsh = NULL;
67 const struct ip6t_opts *optinfo = matchinfo;
72 unsigned int hdrlen = 0;
74 u_int16_t *optdesc = NULL;
76 /* type of the 1st exthdr */
77 nexthdr = skb->nh.ipv6h->nexthdr;
78 /* pointer to the 1st exthdr */
79 ptr = sizeof(struct ipv6hdr);
80 /* available length */
84 while (ip6t_ext_hdr(nexthdr)) {
85 struct ipv6_opt_hdr *hdr;
87 DEBUGP("ipv6_opts header iteration \n");
89 /* Is there enough space for the next ext header? */
90 if (len < (int)sizeof(struct ipv6_opt_hdr))
92 /* No more exthdr -> evaluate */
93 if (nexthdr == NEXTHDR_NONE) {
97 if (nexthdr == NEXTHDR_ESP) {
101 hdr=(void *)(skb->data)+ptr;
103 /* Calculate the header length */
104 if (nexthdr == NEXTHDR_FRAGMENT) {
106 } else if (nexthdr == NEXTHDR_AUTH)
107 hdrlen = (hdr->hdrlen+2)<<2;
109 hdrlen = ipv6_optlen(hdr);
111 /* OPTS -> evaluate */
113 if (nexthdr == NEXTHDR_HOP) {
114 temp |= MASK_HOPOPTS;
116 if (nexthdr == NEXTHDR_DEST) {
117 temp |= MASK_DSTOPTS;
126 case NEXTHDR_ROUTING:
127 case NEXTHDR_FRAGMENT:
132 DEBUGP("ipv6_opts match: unknown nextheader %u\n",nexthdr);
137 nexthdr = hdr->nexthdr;
140 if ( ptr > skb->len ) {
141 DEBUGP("ipv6_opts: new pointer is too large! \n");
146 /* OPTIONS header not found */
148 if ( temp != MASK_HOPOPTS ) return 0;
150 if ( temp != MASK_DSTOPTS ) return 0;
153 if (len < (int)sizeof(struct ipv6_opt_hdr)){
159 /* Packet smaller than it's length field */
163 optsh=(void *)(skb->data)+ptr;
165 DEBUGP("IPv6 OPTS LEN %u %u ", hdrlen, optsh->hdrlen);
167 DEBUGP("len %02X %04X %02X ",
168 optinfo->hdrlen, hdrlen,
169 (!(optinfo->flags & IP6T_OPTS_LEN) ||
170 ((optinfo->hdrlen == hdrlen) ^
171 !!(optinfo->invflags & IP6T_OPTS_INV_LEN))));
173 ret = (optsh != NULL)
175 (!(optinfo->flags & IP6T_OPTS_LEN) ||
176 ((optinfo->hdrlen == hdrlen) ^
177 !!(optinfo->invflags & IP6T_OPTS_INV_LEN)));
182 if ( !(optinfo->flags & IP6T_OPTS_OPTS) ){
184 } else if (optinfo->flags & IP6T_OPTS_NSTRICT) {
185 DEBUGP("Not strict - not implemented");
188 DEBUGP("#%d ",optinfo->optsnr);
189 for(temp=0; temp<optinfo->optsnr; temp++){
190 optdesc = (void *)(skb->data)+ptr;
192 if ( (unsigned char)*optdesc !=
193 (optinfo->opts[temp] & 0xFF00)>>8 ){
194 DEBUGP("Tbad %02X %02X\n",
195 (unsigned char)*optdesc,
196 (optinfo->opts[temp] &
203 if (((optinfo->opts[temp] & 0x00FF) != 0xFF) &&
204 (unsigned char)*optdesc != 0){
205 if ( ntohs((u16)*optdesc) !=
206 optinfo->opts[temp] ){
207 DEBUGP("Lbad %02X %04X %04X\n",
208 (unsigned char)*optdesc,
209 ntohs((u16)*optdesc),
210 optinfo->opts[temp]);
216 /* Step to the next */
217 if ((unsigned char)*optdesc == 0){
222 ptr += LOW(ntohs(*optdesc));
223 hdrlen -= LOW(ntohs(*optdesc));
225 LOW(ntohs(*optdesc)));
227 if (ptr > skb->len || ( !hdrlen &&
228 (temp != optinfo->optsnr - 1))) {
229 DEBUGP("new pointer is too large! \n");
233 if (temp == optinfo->optsnr)
241 /* Called when user tries to insert an entry of this type. */
243 checkentry(const char *tablename,
244 const struct ip6t_ip6 *ip,
246 unsigned int matchinfosize,
247 unsigned int hook_mask)
249 const struct ip6t_opts *optsinfo = matchinfo;
251 if (matchinfosize != IP6T_ALIGN(sizeof(struct ip6t_opts))) {
252 DEBUGP("ip6t_opts: matchsize %u != %u\n",
253 matchinfosize, IP6T_ALIGN(sizeof(struct ip6t_opts)));
256 if (optsinfo->invflags & ~IP6T_OPTS_INV_MASK) {
257 DEBUGP("ip6t_opts: unknown flags %X\n",
265 static struct ip6t_match opts_match = {
272 .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);