linux/net/ipv4/netfilter/ipt_REJECT.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * This is a module which is used for rejecting packets.
   4 */
   5
   6/* (C) 1999-2001 Paul `Rusty' Russell
   7 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
   8 */
   9#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10#include <linux/module.h>
  11#include <linux/skbuff.h>
  12#include <linux/slab.h>
  13#include <linux/ip.h>
  14#include <linux/udp.h>
  15#include <linux/icmp.h>
  16#include <net/icmp.h>
  17#include <linux/netfilter/x_tables.h>
  18#include <linux/netfilter_ipv4/ip_tables.h>
  19#include <linux/netfilter_ipv4/ipt_REJECT.h>
  20#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
  21#include <linux/netfilter_bridge.h>
  22#endif
  23
  24#include <net/netfilter/ipv4/nf_reject.h>
  25
  26MODULE_LICENSE("GPL");
  27MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
  28MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4");
  29
  30static unsigned int
  31reject_tg(struct sk_buff *skb, const struct xt_action_param *par)
  32{
  33        const struct ipt_reject_info *reject = par->targinfo;
  34        int hook = xt_hooknum(par);
  35
  36        switch (reject->with) {
  37        case IPT_ICMP_NET_UNREACHABLE:
  38                nf_send_unreach(skb, ICMP_NET_UNREACH, hook);
  39                break;
  40        case IPT_ICMP_HOST_UNREACHABLE:
  41                nf_send_unreach(skb, ICMP_HOST_UNREACH, hook);
  42                break;
  43        case IPT_ICMP_PROT_UNREACHABLE:
  44                nf_send_unreach(skb, ICMP_PROT_UNREACH, hook);
  45                break;
  46        case IPT_ICMP_PORT_UNREACHABLE:
  47                nf_send_unreach(skb, ICMP_PORT_UNREACH, hook);
  48                break;
  49        case IPT_ICMP_NET_PROHIBITED:
  50                nf_send_unreach(skb, ICMP_NET_ANO, hook);
  51                break;
  52        case IPT_ICMP_HOST_PROHIBITED:
  53                nf_send_unreach(skb, ICMP_HOST_ANO, hook);
  54                break;
  55        case IPT_ICMP_ADMIN_PROHIBITED:
  56                nf_send_unreach(skb, ICMP_PKT_FILTERED, hook);
  57                break;
  58        case IPT_TCP_RESET:
  59                nf_send_reset(xt_net(par), skb, hook);
  60        case IPT_ICMP_ECHOREPLY:
  61                /* Doesn't happen. */
  62                break;
  63        }
  64
  65        return NF_DROP;
  66}
  67
  68static int reject_tg_check(const struct xt_tgchk_param *par)
  69{
  70        const struct ipt_reject_info *rejinfo = par->targinfo;
  71        const struct ipt_entry *e = par->entryinfo;
  72
  73        if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
  74                pr_info_ratelimited("ECHOREPLY no longer supported.\n");
  75                return -EINVAL;
  76        } else if (rejinfo->with == IPT_TCP_RESET) {
  77                /* Must specify that it's a TCP packet */
  78                if (e->ip.proto != IPPROTO_TCP ||
  79                    (e->ip.invflags & XT_INV_PROTO)) {
  80                        pr_info_ratelimited("TCP_RESET invalid for non-tcp\n");
  81                        return -EINVAL;
  82                }
  83        }
  84        return 0;
  85}
  86
  87static struct xt_target reject_tg_reg __read_mostly = {
  88        .name           = "REJECT",
  89        .family         = NFPROTO_IPV4,
  90        .target         = reject_tg,
  91        .targetsize     = sizeof(struct ipt_reject_info),
  92        .table          = "filter",
  93        .hooks          = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
  94                          (1 << NF_INET_LOCAL_OUT),
  95        .checkentry     = reject_tg_check,
  96        .me             = THIS_MODULE,
  97};
  98
  99static int __init reject_tg_init(void)
 100{
 101        return xt_register_target(&reject_tg_reg);
 102}
 103
 104static void __exit reject_tg_exit(void)
 105{
 106        xt_unregister_target(&reject_tg_reg);
 107}
 108
 109module_init(reject_tg_init);
 110module_exit(reject_tg_exit);
 111