]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/ipv6/netfilter/ip6t_ah.c
netfilter: xtables: substitute temporary defines by final name
[mv-sheeva.git] / net / ipv6 / netfilter / ip6t_ah.c
1 /* 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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/module.h>
11 #include <linux/skbuff.h>
12 #include <linux/ip.h>
13 #include <linux/ipv6.h>
14 #include <linux/types.h>
15 #include <net/checksum.h>
16 #include <net/ipv6.h>
17
18 #include <linux/netfilter/x_tables.h>
19 #include <linux/netfilter_ipv6/ip6_tables.h>
20 #include <linux/netfilter_ipv6/ip6t_ah.h>
21
22 MODULE_LICENSE("GPL");
23 MODULE_DESCRIPTION("Xtables: IPv6 IPsec-AH match");
24 MODULE_AUTHOR("Andras Kis-Szabo <kisza@sch.bme.hu>");
25
26 /* Returns 1 if the spi is matched by the range, 0 otherwise */
27 static inline bool
28 spi_match(u_int32_t min, u_int32_t max, u_int32_t spi, bool invert)
29 {
30         bool r;
31
32         pr_debug("spi_match:%c 0x%x <= 0x%x <= 0x%x\n",
33                  invert ? '!' : ' ', min, spi, max);
34         r = (spi >= min && spi <= max) ^ invert;
35         pr_debug(" result %s\n", r ? "PASS" : "FAILED");
36         return r;
37 }
38
39 static bool ah_mt6(const struct sk_buff *skb,
40                    const struct xt_action_param *par)
41 {
42         struct ip_auth_hdr _ah;
43         const struct ip_auth_hdr *ah;
44         const struct ip6t_ah *ahinfo = par->matchinfo;
45         unsigned int ptr;
46         unsigned int hdrlen = 0;
47         int err;
48
49         err = ipv6_find_hdr(skb, &ptr, NEXTHDR_AUTH, NULL);
50         if (err < 0) {
51                 if (err != -ENOENT)
52                         *par->hotdrop = true;
53                 return false;
54         }
55
56         ah = skb_header_pointer(skb, ptr, sizeof(_ah), &_ah);
57         if (ah == NULL) {
58                 *par->hotdrop = true;
59                 return false;
60         }
61
62         hdrlen = (ah->hdrlen + 2) << 2;
63
64         pr_debug("IPv6 AH LEN %u %u ", hdrlen, ah->hdrlen);
65         pr_debug("RES %04X ", ah->reserved);
66         pr_debug("SPI %u %08X\n", ntohl(ah->spi), ntohl(ah->spi));
67
68         pr_debug("IPv6 AH spi %02X ",
69                  spi_match(ahinfo->spis[0], ahinfo->spis[1],
70                            ntohl(ah->spi),
71                            !!(ahinfo->invflags & IP6T_AH_INV_SPI)));
72         pr_debug("len %02X %04X %02X ",
73                  ahinfo->hdrlen, hdrlen,
74                  (!ahinfo->hdrlen ||
75                   (ahinfo->hdrlen == hdrlen) ^
76                   !!(ahinfo->invflags & IP6T_AH_INV_LEN)));
77         pr_debug("res %02X %04X %02X\n",
78                  ahinfo->hdrres, ah->reserved,
79                  !(ahinfo->hdrres && ah->reserved));
80
81         return (ah != NULL) &&
82                 spi_match(ahinfo->spis[0], ahinfo->spis[1],
83                           ntohl(ah->spi),
84                           !!(ahinfo->invflags & IP6T_AH_INV_SPI)) &&
85                 (!ahinfo->hdrlen ||
86                  (ahinfo->hdrlen == hdrlen) ^
87                  !!(ahinfo->invflags & IP6T_AH_INV_LEN)) &&
88                 !(ahinfo->hdrres && ah->reserved);
89 }
90
91 static int ah_mt6_check(const struct xt_mtchk_param *par)
92 {
93         const struct ip6t_ah *ahinfo = par->matchinfo;
94
95         if (ahinfo->invflags & ~IP6T_AH_INV_MASK) {
96                 pr_debug("unknown flags %X\n", ahinfo->invflags);
97                 return -EINVAL;
98         }
99         return 0;
100 }
101
102 static struct xt_match ah_mt6_reg __read_mostly = {
103         .name           = "ah",
104         .family         = NFPROTO_IPV6,
105         .match          = ah_mt6,
106         .matchsize      = sizeof(struct ip6t_ah),
107         .checkentry     = ah_mt6_check,
108         .me             = THIS_MODULE,
109 };
110
111 static int __init ah_mt6_init(void)
112 {
113         return xt_register_match(&ah_mt6_reg);
114 }
115
116 static void __exit ah_mt6_exit(void)
117 {
118         xt_unregister_match(&ah_mt6_reg);
119 }
120
121 module_init(ah_mt6_init);
122 module_exit(ah_mt6_exit);