blob: a787d07f6cb757b741fcd600f9972c3399c95c85 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* Kernel module to match AH parameters. */
2/* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +01008#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Jan Engelhardt6709dbb2007-02-07 15:11:19 -08009#include <linux/in.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/skbuff.h>
12#include <linux/ip.h>
13
14#include <linux/netfilter_ipv4/ipt_ah.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080015#include <linux/netfilter/x_tables.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
17MODULE_LICENSE("GPL");
18MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080019MODULE_DESCRIPTION("Xtables: IPv4 IPsec-AH SPI match");
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021/* Returns 1 if the spi is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070022static inline bool
23spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
Linus Torvalds1da177e2005-04-16 15:20:36 -070024{
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070025 bool r;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010026 pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
27 invert ? '!' : ' ', min, spi, max);
Ian Morrisc8d71d02015-10-14 23:17:08 +010028 r = (spi >= min && spi <= max) ^ invert;
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010029 pr_debug(" result %s\n", r ? "PASS" : "FAILED");
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 return r;
31}
32
Jan Engelhardt62fc8052009-07-07 20:42:08 +020033static bool ah_mt(const struct sk_buff *skb, struct xt_action_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070034{
Jan Engelhardta47362a2007-07-07 22:16:55 -070035 struct ip_auth_hdr _ahdr;
36 const struct ip_auth_hdr *ah;
Jan Engelhardtf7108a22008-10-08 11:35:18 +020037 const struct ipt_ah *ahinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +020040 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070041 return false;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
Jan Engelhardtf7108a22008-10-08 11:35:18 +020043 ah = skb_header_pointer(skb, par->thoff, sizeof(_ahdr), &_ahdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 if (ah == NULL) {
45 /* We've been asked to examine this packet, and we
46 * can't. Hence, no choice but to drop.
47 */
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010048 pr_debug("Dropping evil AH tinygram.\n");
Jan Engelhardtb4ba2612009-07-07 20:54:30 +020049 par->hotdrop = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -070050 return 0;
51 }
52
53 return spi_match(ahinfo->spis[0], ahinfo->spis[1],
54 ntohl(ah->spi),
55 !!(ahinfo->invflags & IPT_AH_INV_SPI));
56}
57
Jan Engelhardtb0f38452010-03-19 17:16:42 +010058static int ah_mt_check(const struct xt_mtchk_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070059{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020060 const struct ipt_ah *ahinfo = par->matchinfo;
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Patrick McHardy1d5cd902006-03-20 18:01:14 -080062 /* Must specify no unknown invflags */
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (ahinfo->invflags & ~IPT_AH_INV_MASK) {
Jan Engelhardtbe91fd52010-03-18 02:22:32 +010064 pr_debug("unknown flags %X\n", ahinfo->invflags);
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010065 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 }
Jan Engelhardtbd414ee2010-03-23 16:35:56 +010067 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070068}
69
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080070static struct xt_match ah_mt_reg __read_mostly = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 .name = "ah",
Jan Engelhardtee999d82008-10-08 11:35:01 +020072 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080073 .match = ah_mt,
Patrick McHardy1d5cd902006-03-20 18:01:14 -080074 .matchsize = sizeof(struct ipt_ah),
75 .proto = IPPROTO_AH,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080076 .checkentry = ah_mt_check,
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 .me = THIS_MODULE,
78};
79
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080080static int __init ah_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080082 return xt_register_match(&ah_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080085static void __exit ah_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080087 xt_unregister_match(&ah_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080090module_init(ah_mt_init);
91module_exit(ah_mt_exit);