blob: 609439967c2c67579d299e9e43218a7c362da8a8 [file] [log] [blame]
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -08001/* Kernel module to match ESP parameters. */
2
3/* (C) 1999-2000 Yon Uriarte <yon@astaro.de>
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>
12#include <linux/in.h>
13#include <linux/ip.h>
14
15#include <linux/netfilter/xt_esp.h>
16#include <linux/netfilter/x_tables.h>
17
18#include <linux/netfilter_ipv4/ip_tables.h>
19#include <linux/netfilter_ipv6/ip6_tables.h>
20
21MODULE_LICENSE("GPL");
22MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080023MODULE_DESCRIPTION("Xtables: IPsec-ESP packet match");
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080024MODULE_ALIAS("ipt_esp");
25MODULE_ALIAS("ip6t_esp");
26
27#if 0
28#define duprintf(format, args...) printk(format , ## args)
29#else
30#define duprintf(format, args...)
31#endif
32
33/* Returns 1 if the spi is matched by the range, 0 otherwise */
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070034static inline bool
35spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080036{
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070037 bool r;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080038 duprintf("esp spi_match:%c 0x%x <= 0x%x <= 0x%x", invert ? '!' : ' ',
39 min, spi, max);
40 r = (spi >= min && spi <= max) ^ invert;
41 duprintf(" result %s\n", r ? "PASS" : "FAILED");
42 return r;
43}
44
Jan Engelhardtf7108a22008-10-08 11:35:18 +020045static bool esp_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080046{
Jan Engelhardt3cf93c92008-04-14 09:56:05 +020047 const struct ip_esp_hdr *eh;
48 struct ip_esp_hdr _esp;
Jan Engelhardtf7108a22008-10-08 11:35:18 +020049 const struct xt_esp *espinfo = par->matchinfo;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080050
51 /* Must not be a fragment. */
Jan Engelhardtf7108a22008-10-08 11:35:18 +020052 if (par->fragoff != 0)
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070053 return false;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080054
Jan Engelhardtf7108a22008-10-08 11:35:18 +020055 eh = skb_header_pointer(skb, par->thoff, sizeof(_esp), &_esp);
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080056 if (eh == NULL) {
57 /* We've been asked to examine this packet, and we
58 * can't. Hence, no choice but to drop.
59 */
60 duprintf("Dropping evil ESP tinygram.\n");
Jan Engelhardtf7108a22008-10-08 11:35:18 +020061 *par->hotdrop = true;
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070062 return false;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080063 }
64
65 return spi_match(espinfo->spis[0], espinfo->spis[1], ntohl(eh->spi),
66 !!(espinfo->invflags & XT_ESP_INV_SPI));
67}
68
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020069static bool esp_mt_check(const struct xt_mtchk_param *par)
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080070{
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020071 const struct xt_esp *espinfo = par->matchinfo;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080072
73 if (espinfo->invflags & ~XT_ESP_INV_MASK) {
74 duprintf("xt_esp: unknown flags %X\n", espinfo->invflags);
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070075 return false;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080076 }
77
Jan Engelhardtccb79bd2007-07-07 22:16:00 -070078 return true;
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -080079}
80
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080081static struct xt_match esp_mt_reg[] __read_mostly = {
Patrick McHardy4470bbc2006-08-22 00:34:04 -070082 {
83 .name = "esp",
Jan Engelhardtee999d82008-10-08 11:35:01 +020084 .family = NFPROTO_IPV4,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080085 .checkentry = esp_mt_check,
86 .match = esp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070087 .matchsize = sizeof(struct xt_esp),
88 .proto = IPPROTO_ESP,
89 .me = THIS_MODULE,
90 },
91 {
92 .name = "esp",
Jan Engelhardtee999d82008-10-08 11:35:01 +020093 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080094 .checkentry = esp_mt_check,
95 .match = esp_mt,
Patrick McHardy4470bbc2006-08-22 00:34:04 -070096 .matchsize = sizeof(struct xt_esp),
97 .proto = IPPROTO_ESP,
98 .me = THIS_MODULE,
99 },
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800100};
101
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800102static int __init esp_mt_init(void)
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800103{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800104 return xt_register_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800105}
106
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800107static void __exit esp_mt_exit(void)
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800108{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109 xt_unregister_matches(esp_mt_reg, ARRAY_SIZE(esp_mt_reg));
Yasuyuki Kozakaidc5ab2f2006-04-01 02:22:30 -0800110}
111
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112module_init(esp_mt_init);
113module_exit(esp_mt_exit);