9de191a8162da78e3177a5e0b356d128a39363b2
[safe/jmp/linux-2.6] / net / ipv4 / netfilter / ipt_esp.c
1 /* 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/ip.h>
13
14 #include <linux/netfilter_ipv4/ipt_esp.h>
15 #include <linux/netfilter_ipv4/ip_tables.h>
16
17 MODULE_LICENSE("GPL");
18 MODULE_AUTHOR("Yon Uriarte <yon@astaro.de>");
19 MODULE_DESCRIPTION("iptables ESP SPI match module");
20
21 #ifdef DEBUG_CONNTRACK
22 #define duprintf(format, args...) printk(format , ## args)
23 #else
24 #define duprintf(format, args...)
25 #endif
26
27 /* Returns 1 if the spi is matched by the range, 0 otherwise */
28 static inline int
29 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, int invert)
30 {
31         int r=0;
32         duprintf("esp spi_match:%c 0x%x <= 0x%x <= 0x%x",invert? '!':' ',
33                 min,spi,max);
34         r=(spi >= min && spi <= max) ^ invert;
35         duprintf(" result %s\n",r? "PASS" : "FAILED");
36         return r;
37 }
38
39 static int
40 match(const struct sk_buff *skb,
41       const struct net_device *in,
42       const struct net_device *out,
43       const void *matchinfo,
44       int offset,
45       unsigned int protoff,
46       int *hotdrop)
47 {
48         struct ip_esp_hdr _esp, *eh;
49         const struct ipt_esp *espinfo = matchinfo;
50
51         /* Must not be a fragment. */
52         if (offset)
53                 return 0;
54
55         eh = skb_header_pointer(skb, protoff,
56                                 sizeof(_esp), &_esp);
57         if (eh == NULL) {
58                 /* We've been asked to examine this packet, and we
59                  * can't.  Hence, no choice but to drop.
60                  */
61                 duprintf("Dropping evil ESP tinygram.\n");
62                 *hotdrop = 1;
63                 return 0;
64         }
65
66         return spi_match(espinfo->spis[0], espinfo->spis[1],
67                          ntohl(eh->spi),
68                          !!(espinfo->invflags & IPT_ESP_INV_SPI));
69 }
70
71 /* Called when user tries to insert an entry of this type. */
72 static int
73 checkentry(const char *tablename,
74            const void *ip_void,
75            void *matchinfo,
76            unsigned int matchinfosize,
77            unsigned int hook_mask)
78 {
79         const struct ipt_esp *espinfo = matchinfo;
80         const struct ipt_ip *ip = ip_void;
81
82         /* Must specify proto == ESP, and no unknown invflags */
83         if (ip->proto != IPPROTO_ESP || (ip->invflags & IPT_INV_PROTO)) {
84                 duprintf("ipt_esp: Protocol %u != %u\n", ip->proto,
85                          IPPROTO_ESP);
86                 return 0;
87         }
88         if (matchinfosize != IPT_ALIGN(sizeof(struct ipt_esp))) {
89                 duprintf("ipt_esp: matchsize %u != %u\n",
90                          matchinfosize, IPT_ALIGN(sizeof(struct ipt_esp)));
91                 return 0;
92         }
93         if (espinfo->invflags & ~IPT_ESP_INV_MASK) {
94                 duprintf("ipt_esp: unknown flags %X\n",
95                          espinfo->invflags);
96                 return 0;
97         }
98
99         return 1;
100 }
101
102 static struct ipt_match esp_match = {
103         .name           = "esp",
104         .match          = &match,
105         .checkentry     = &checkentry,
106         .me             = THIS_MODULE,
107 };
108
109 static int __init init(void)
110 {
111         return ipt_register_match(&esp_match);
112 }
113
114 static void __exit cleanup(void)
115 {
116         ipt_unregister_match(&esp_match);
117 }
118
119 module_init(init);
120 module_exit(cleanup);