blob: db29bbf41b5977d3228ea58ce3032725c175f989 [file] [log] [blame]
Patrick McHardy764d8a92005-08-21 23:31:06 -07001/*
2 * IP6 tables REJECT target module
3 * Linux INET6 implementation
4 *
5 * Copyright (C)2003 USAGI/WIDE Project
6 *
7 * Authors:
8 * Yasuyuki Kozakai <yasuyuki.kozakai@toshiba.co.jp>
9 *
Patrick McHardyf229f6c2013-04-06 15:24:29 +020010 * Copyright (c) 2005-2007 Patrick McHardy <kaber@trash.net>
11 *
Patrick McHardy764d8a92005-08-21 23:31:06 -070012 * Based on net/ipv4/netfilter/ipt_REJECT.c
13 *
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License
16 * as published by the Free Software Foundation; either version
17 * 2 of the License, or (at your option) any later version.
18 */
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010019#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
Patrick McHardy764d8a92005-08-21 23:31:06 -070020
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090021#include <linux/gfp.h>
Patrick McHardy764d8a92005-08-21 23:31:06 -070022#include <linux/module.h>
23#include <linux/skbuff.h>
24#include <linux/icmpv6.h>
25#include <linux/netdevice.h>
Patrick McHardy764d8a92005-08-21 23:31:06 -070026#include <net/icmp.h>
Patrick McHardy764d8a92005-08-21 23:31:06 -070027#include <net/flow.h>
Jan Engelhardt6709dbb2007-02-07 15:11:19 -080028#include <linux/netfilter/x_tables.h>
Patrick McHardy764d8a92005-08-21 23:31:06 -070029#include <linux/netfilter_ipv6/ip6_tables.h>
30#include <linux/netfilter_ipv6/ip6t_REJECT.h>
31
Eric Leblondcc70d062013-12-29 12:28:13 +010032#include <net/netfilter/ipv6/nf_reject.h>
33
Patrick McHardy764d8a92005-08-21 23:31:06 -070034MODULE_AUTHOR("Yasuyuki KOZAKAI <yasuyuki.kozakai@toshiba.co.jp>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080035MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv6");
Patrick McHardy764d8a92005-08-21 23:31:06 -070036MODULE_LICENSE("GPL");
37
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080038static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020039reject_tg6(struct sk_buff *skb, const struct xt_action_param *par)
Patrick McHardy764d8a92005-08-21 23:31:06 -070040{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020041 const struct ip6t_reject_info *reject = par->targinfo;
Eric W. Biederman686c9b52015-09-18 14:32:59 -050042 struct net *net = par->net;
Patrick McHardy764d8a92005-08-21 23:31:06 -070043
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090044 switch (reject->with) {
45 case IP6T_ICMP6_NO_ROUTE:
Eric Leblondcc70d062013-12-29 12:28:13 +010046 nf_send_unreach6(net, skb, ICMPV6_NOROUTE, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090047 break;
48 case IP6T_ICMP6_ADM_PROHIBITED:
Eric Leblondcc70d062013-12-29 12:28:13 +010049 nf_send_unreach6(net, skb, ICMPV6_ADM_PROHIBITED, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090050 break;
51 case IP6T_ICMP6_NOT_NEIGHBOUR:
Eric Leblondcc70d062013-12-29 12:28:13 +010052 nf_send_unreach6(net, skb, ICMPV6_NOT_NEIGHBOUR, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090053 break;
54 case IP6T_ICMP6_ADDR_UNREACH:
Eric Leblondcc70d062013-12-29 12:28:13 +010055 nf_send_unreach6(net, skb, ICMPV6_ADDR_UNREACH, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090056 break;
57 case IP6T_ICMP6_PORT_UNREACH:
Eric Leblondcc70d062013-12-29 12:28:13 +010058 nf_send_unreach6(net, skb, ICMPV6_PORT_UNREACH, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090059 break;
60 case IP6T_ICMP6_ECHOREPLY:
Patrick McHardy764d8a92005-08-21 23:31:06 -070061 /* Do nothing */
62 break;
63 case IP6T_TCP_RESET:
Eric Leblondcc70d062013-12-29 12:28:13 +010064 nf_send_reset6(net, skb, par->hooknum);
Patrick McHardy764d8a92005-08-21 23:31:06 -070065 break;
Andreas Herz1afe8392015-08-21 11:31:32 +020066 case IP6T_ICMP6_POLICY_FAIL:
67 nf_send_unreach6(net, skb, ICMPV6_POLICY_FAIL, par->hooknum);
68 break;
69 case IP6T_ICMP6_REJECT_ROUTE:
70 nf_send_unreach6(net, skb, ICMPV6_REJECT_ROUTE, par->hooknum);
71 break;
Patrick McHardy764d8a92005-08-21 23:31:06 -070072 }
73
74 return NF_DROP;
75}
76
Jan Engelhardt135367b2010-03-19 17:16:42 +010077static int reject_tg6_check(const struct xt_tgchk_param *par)
Patrick McHardy764d8a92005-08-21 23:31:06 -070078{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020079 const struct ip6t_reject_info *rejinfo = par->targinfo;
80 const struct ip6t_entry *e = par->entryinfo;
Patrick McHardy764d8a92005-08-21 23:31:06 -070081
Patrick McHardy764d8a92005-08-21 23:31:06 -070082 if (rejinfo->with == IP6T_ICMP6_ECHOREPLY) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010083 pr_info("ECHOREPLY is not supported.\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010084 return -EINVAL;
Patrick McHardy764d8a92005-08-21 23:31:06 -070085 } else if (rejinfo->with == IP6T_TCP_RESET) {
86 /* Must specify that it's a TCP packet */
Pablo Neira Ayusoe35158e2015-03-21 20:20:23 +010087 if (!(e->ipv6.flags & IP6T_F_PROTO) ||
88 e->ipv6.proto != IPPROTO_TCP ||
Joe Perches3666ed12009-11-23 23:17:06 +010089 (e->ipv6.invflags & XT_INV_PROTO)) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010090 pr_info("TCP_RESET illegal for non-tcp\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010091 return -EINVAL;
Patrick McHardy764d8a92005-08-21 23:31:06 -070092 }
93 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010094 return 0;
Patrick McHardy764d8a92005-08-21 23:31:06 -070095}
96
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080097static struct xt_target reject_tg6_reg __read_mostly = {
Patrick McHardy764d8a92005-08-21 23:31:06 -070098 .name = "REJECT",
Jan Engelhardtee999d82008-10-08 11:35:01 +020099 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800100 .target = reject_tg6,
Patrick McHardy7f939712006-03-20 18:01:43 -0800101 .targetsize = sizeof(struct ip6t_reject_info),
102 .table = "filter",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800103 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
104 (1 << NF_INET_LOCAL_OUT),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800105 .checkentry = reject_tg6_check,
Patrick McHardy764d8a92005-08-21 23:31:06 -0700106 .me = THIS_MODULE
107};
108
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109static int __init reject_tg6_init(void)
Patrick McHardy764d8a92005-08-21 23:31:06 -0700110{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800111 return xt_register_target(&reject_tg6_reg);
Patrick McHardy764d8a92005-08-21 23:31:06 -0700112}
113
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800114static void __exit reject_tg6_exit(void)
Patrick McHardy764d8a92005-08-21 23:31:06 -0700115{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800116 xt_unregister_target(&reject_tg6_reg);
Patrick McHardy764d8a92005-08-21 23:31:06 -0700117}
118
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800119module_init(reject_tg6_init);
120module_exit(reject_tg6_exit);