blob: 8b147440fbdced8dbc23023785596f0565b6ddef [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* ipv6header match - matches IPv6 packets based
2 on whether they contain certain headers */
3
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09004/* Original idea: Brad Chapman
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 * 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
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080021#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/netfilter_ipv6/ip6_tables.h>
23#include <linux/netfilter_ipv6/ip6t_ipv6header.h>
24
25MODULE_LICENSE("GPL");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080026MODULE_DESCRIPTION("Xtables: IPv6 header types match");
Linus Torvalds1da177e2005-04-16 15:20:36 -070027MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
28
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070029static bool
Jan Engelhardt62fc8052009-07-07 20:42:08 +020030ipv6header_mt6(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070031{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020032 const struct ip6t_ipv6header_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 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 */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -070041 nexthdr = ipv6_hdr(skb)->nexthdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 /* pointer to the 1st exthdr */
43 ptr = sizeof(struct ipv6hdr);
44 /* available length */
45 len = skb->len - ptr;
46 temp = 0;
47
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080048 while (ip6t_ext_hdr(nexthdr)) {
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020049 const struct ipv6_opt_hdr *hp;
50 struct ipv6_opt_hdr _hdr;
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080051 int hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 /* No more exthdr -> evaluate */
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080054 if (nexthdr == NEXTHDR_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070055 temp |= MASK_NONE;
56 break;
57 }
Christoph Paaschb98b4942009-05-05 15:32:16 +020058 /* Is there enough space for the next ext header? */
59 if (len < (int)sizeof(struct ipv6_opt_hdr))
60 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 /* ESP -> evaluate */
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080062 if (nexthdr == NEXTHDR_ESP) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 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 */
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070071 if (nexthdr == NEXTHDR_FRAGMENT)
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080072 hdrlen = 8;
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070073 else if (nexthdr == NEXTHDR_AUTH)
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080074 hdrlen = (hp->hdrlen + 2) << 2;
75 else
76 hdrlen = ipv6_optlen(hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78 /* set the flag */
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080079 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:
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070096 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
98
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -080099 nexthdr = hp->nexthdr;
100 len -= hdrlen;
101 ptr += hdrlen;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 if (ptr > skb->len)
103 break;
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -0800104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -0700106 if (nexthdr != NEXTHDR_NONE && nexthdr != NEXTHDR_ESP)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 temp |= MASK_PROTO;
108
109 if (info->modeflag)
110 return !((temp ^ info->matchflags ^ info->invflags)
111 & info->matchflags);
112 else {
113 if (info->invflags)
114 return temp != info->matchflags;
115 else
116 return temp == info->matchflags;
117 }
118}
119
Jan Engelhardtb0f38452010-03-19 17:16:42 +0100120static int ipv6header_mt6_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +0200122 const struct ip6t_ipv6header_info *info = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 /* invflags is 0 or 0xff in hard mode */
Yasuyuki Kozakaif0daaa62006-01-17 02:39:39 -0800125 if ((!info->modeflag) && info->invflags != 0x00 &&
126 info->invflags != 0xFF)
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100127 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
Jan Engelhardtbd414ee2010-03-23 16:35:56 +0100129 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800132static struct xt_match ipv6header_mt6_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 .name = "ipv6header",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200134 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800135 .match = ipv6header_mt6,
Patrick McHardy7f939712006-03-20 18:01:43 -0800136 .matchsize = sizeof(struct ip6t_ipv6header_info),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800137 .checkentry = ipv6header_mt6_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 .destroy = NULL,
139 .me = THIS_MODULE,
140};
141
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800142static int __init ipv6header_mt6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800144 return xt_register_match(&ipv6header_mt6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800147static void __exit ipv6header_mt6_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800149 xt_unregister_match(&ipv6header_mt6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150}
151
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800152module_init(ipv6header_mt6_init);
153module_exit(ipv6header_mt6_exit);