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