blob: 04099ab7d2e3d32aa1928c3e6131e154b88774a4 [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 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +01009#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080023MODULE_DESCRIPTION("Xtables: IPv6 IPsec-AH match");
Linus Torvalds1da177e2005-04-16 15:20:36 -070024MODULE_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
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010032 pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
Patrick McHardy0d537782007-07-07 22:39:38 -070033 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 Engelhardt62fc8052009-07-07 20:42:08 +020039static bool ah_mt6(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070040{
Jan Engelhardta47362a2007-07-07 22:16:55 -070041 struct ip_auth_hdr _ah;
42 const struct ip_auth_hdr *ah;
Jan Engelhardtf7108a22008-10-08 11:35:18 +020043 const struct ip6t_ah *ahinfo = par->matchinfo;
Hans Schillstrom84018f52012-04-23 03:35:26 +000044 unsigned int ptr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 unsigned int hdrlen = 0;
Patrick McHardy6d381632006-10-24 16:15:10 -070046 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
Hans Schillstrom84018f52012-04-23 03:35:26 +000048 err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL, NULL);
Patrick McHardy6d381632006-10-24 16:15:10 -070049 if (err < 0) {
50 if (err != -ENOENT)
Jan Engelhardtb4ba2612009-07-07 20:54:30 +020051 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070052 return false;
Patrick McHardy6d381632006-10-24 16:15:10 -070053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -070055 ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
56 if (ah == NULL) {
Jan Engelhardtb4ba2612009-07-07 20:54:30 +020057 par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070058 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 }
60
Yasuyuki Kozakaie674d0f2005-09-19 15:34:40 -070061 hdrlen = (ah->hdrlen + 2) << 2;
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Patrick McHardy0d537782007-07-07 22:39:38 -070063 pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
64 pr_debug("RES %04X ", ah->reserved);
65 pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Patrick McHardy0d537782007-07-07 22:39:38 -070067 pr_debug("IPv6 AH spi %02X ",
68 spi_match(ahinfo->spis[0], ahinfo->spis[1],
69 ntohl(ah->spi),
70 !!(ahinfo->invflags & IP6T_AH_INV_SPI)));
71 pr_debug("len %02X %04X %02X ",
72 ahinfo->hdrlen, hdrlen,
73 (!ahinfo->hdrlen ||
74 (ahinfo->hdrlen == hdrlen) ^
75 !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
76 pr_debug("res %02X %04X %02X\n",
77 ahinfo->hdrres, ah->reserved,
78 !(ahinfo->hdrres && ah->reserved));
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Joe Perches3666ed12009-11-23 23:17:06 +010080 return (ah != NULL) &&
81 spi_match(ahinfo->spis[0], ahinfo->spis[1],
82 ntohl(ah->spi),
83 !!(ahinfo->invflags & IP6T_AH_INV_SPI)) &&
84 (!ahinfo->hdrlen ||
85 (ahinfo->hdrlen == hdrlen) ^
86 !!(ahinfo->invflags & IP6T_AH_INV_LEN)) &&
87 !(ahinfo->hdrres && ah->reserved);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Jan Engelhardtb0f38452010-03-19 17:16:42 +010090static int ah_mt6_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020092 const struct ip6t_ah *ahinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010095 pr_debug("unknown flags %X\n", ahinfo->invflags);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010096 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010098 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800101static struct xt_match ah_mt6_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 .name = "ah",
Jan Engelhardtee999d82008-10-08 11:35:01 +0200103 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800104 .match = ah_mt6,
Patrick McHardy7f939712006-03-20 18:01:43 -0800105 .matchsize = sizeof(struct ip6t_ah),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800106 .checkentry = ah_mt6_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 .me = THIS_MODULE,
108};
109
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800110static int __init ah_mt6_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112 return xt_register_match(&ah_mt6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800115static void __exit ah_mt6_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800117 xt_unregister_match(&ah_mt6_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118}
119
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800120module_init(ah_mt6_init);
121module_exit(ah_mt6_exit);