blob: 2a25fe25e0e0d236947a6f3c281b3e72415a3572 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel module to match AH parameters. */
2
3/* (C) 2001-2002 Andras Kis-Szabo <kisza@sch.bme.hu>
4 *
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.
8 */
9
10#include <linux/module.h>
11#include <linux/skbuff.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020012#include <linux/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/ipv6.h>
14#include <linux/types.h>
15#include <net/checksum.h>
16#include <net/ipv6.h>
17
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080018#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/netfilter_ipv6/ip6_tables.h>
20#include <linux/netfilter_ipv6/ip6t_ah.h>
21
22MODULE_LICENSE("GPL");
23MODULE_DESCRIPTION("IPv6 AH match");
24MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026/* Returns 1 if the spi is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070027static inline bool
28spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070030 bool r;
Patrick McHardy0d537782007-07-07 22:39:38 -070031
32 pr_debug("ah spi_match:%c 0x%x <= 0x%x <= 0x%x",
33 invert ? '!' : ' ', min, spi, max);
Linus Torvalds1da177e2005-04-16 15:20:36 -070034 r = (spi >= min && spi <= max) ^ invert;
Patrick McHardy0d537782007-07-07 22:39:38 -070035 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 return r;
37}
38
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070039static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -070040match(const struct sk_buff *skb,
41 const struct net_device *in,
42 const struct net_device *out,
Patrick McHardyc4986732006-03-20 18:02:56 -080043 const struct xt_match *match,
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 const void *matchinfo,
45 int offset,
46 unsigned int protoff,
Jan Engelhardtcff533a2007-07-07 22:15:12 -070047 bool *hotdrop)
Linus Torvalds1da177e2005-04-16 15:20:36 -070048{
Jan Engelhardta47362a2007-07-07 22:16:55 -070049 struct ip_auth_hdr _ah;
50 const struct ip_auth_hdr *ah;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 const struct ip6t_ah *ahinfo = matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 unsigned int ptr;
53 unsigned int hdrlen = 0;
Patrick McHardy6d381632006-10-24 16:15:10 -070054 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
Patrick McHardy6d381632006-10-24 16:15:10 -070056 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL);
57 if (err < 0) {
58 if (err != -ENOENT)
Jan Engelhardtcff533a2007-07-07 22:15:12 -070059 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070060 return false;
Patrick McHardy6d381632006-10-24 16:15:10 -070061 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -070063 ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
64 if (ah == NULL) {
Jan Engelhardtcff533a2007-07-07 22:15:12 -070065 *hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070066 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 }
68
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -070069 hdrlen = (ah->hdrlen + 2) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Patrick McHardy0d537782007-07-07 22:39:38 -070071 pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
72 pr_debug("RES %04X ", ah->reserved);
73 pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
Patrick McHardy0d537782007-07-07 22:39:38 -070075 pr_debug("IPv6 AH spi %02X ",
76 spi_match(ahinfo->spis[0], ahinfo->spis[1],
77 ntohl(ah->spi),
78 !!(ahinfo->invflags & IP6T_AH_INV_SPI)));
79 pr_debug("len %02X %04X %02X ",
80 ahinfo->hdrlen, hdrlen,
81 (!ahinfo->hdrlen ||
82 (ahinfo->hdrlen == hdrlen) ^
83 !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
84 pr_debug("res %02X %04X %02X\n",
85 ahinfo->hdrres, ah->reserved,
86 !(ahinfo->hdrres && ah->reserved));
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88 return (ah != NULL)
89 &&
Jan Engelhardt7c4e36b2007-07-07 22:19:08 -070090 spi_match(ahinfo->spis[0], ahinfo->spis[1],
91 ntohl(ah->spi),
92 !!(ahinfo->invflags & IP6T_AH_INV_SPI))
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 &&
94 (!ahinfo->hdrlen ||
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090095 (ahinfo->hdrlen == hdrlen) ^
96 !!(ahinfo->invflags & IP6T_AH_INV_LEN))
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 &&
98 !(ahinfo->hdrres && ah->reserved);
99}
100
101/* Called when user tries to insert an entry of this type. */
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700102static bool
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103checkentry(const char *tablename,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900104 const void *entry,
Patrick McHardyc4986732006-03-20 18:02:56 -0800105 const struct xt_match *match,
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900106 void *matchinfo,
107 unsigned int hook_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
109 const struct ip6t_ah *ahinfo = matchinfo;
110
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
Patrick McHardy0d537782007-07-07 22:39:38 -0700112 pr_debug("ip6t_ah: unknown flags %X\n", ahinfo->invflags);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700113 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 }
Jan Engelhardtccb79bd2007-07-07 22:16:00 -0700115 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116}
117
Patrick McHardy9f15c532007-07-07 22:22:02 -0700118static struct xt_match ah_match __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 .name = "ah",
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800120 .family = AF_INET6,
Patrick McHardy7f939712006-03-20 18:01:43 -0800121 .match = match,
122 .matchsize = sizeof(struct ip6t_ah),
123 .checkentry = checkentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 .me = THIS_MODULE,
125};
126
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800127static int __init ip6t_ah_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800129 return xt_register_match(&ah_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800132static void __exit ip6t_ah_fini(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133{
Jan Engelhardt6709dbb2007-02-07 15:11:19 -0800134 xt_unregister_match(&ah_match);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135}
136
Andrew Morton65b4b4e2006-03-28 16:37:06 -0800137module_init(ip6t_ah_init);
138module_exit(ip6t_ah_fini);