blob: 544b0a9da1b59db2fc59cbaf11a1eccd1de8dd3f [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
Patrick McHardy764d8a92005-08-21 23:31:06 -070038
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080039static unsigned int
Jan Engelhardt4b560b42009-07-05 19:43:26 +020040reject_tg6(struct sk_buff *skb, const struct xt_action_param *par)
Patrick McHardy764d8a92005-08-21 23:31:06 -070041{
Jan Engelhardt7eb35582008-10-08 11:35:19 +020042 const struct ip6t_reject_info *reject = par->targinfo;
43 struct net *net = dev_net((par->in != NULL) ? par->in : par->out);
Patrick McHardy764d8a92005-08-21 23:31:06 -070044
Harvey Harrison0dc47872008-03-05 20:47:47 -080045 pr_debug("%s: medium point\n", __func__);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090046 switch (reject->with) {
47 case IP6T_ICMP6_NO_ROUTE:
Eric Leblondcc70d062013-12-29 12:28:13 +010048 nf_send_unreach6(net, skb, ICMPV6_NOROUTE, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090049 break;
50 case IP6T_ICMP6_ADM_PROHIBITED:
Eric Leblondcc70d062013-12-29 12:28:13 +010051 nf_send_unreach6(net, skb, ICMPV6_ADM_PROHIBITED, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090052 break;
53 case IP6T_ICMP6_NOT_NEIGHBOUR:
Eric Leblondcc70d062013-12-29 12:28:13 +010054 nf_send_unreach6(net, skb, ICMPV6_NOT_NEIGHBOUR, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090055 break;
56 case IP6T_ICMP6_ADDR_UNREACH:
Eric Leblondcc70d062013-12-29 12:28:13 +010057 nf_send_unreach6(net, skb, ICMPV6_ADDR_UNREACH, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090058 break;
59 case IP6T_ICMP6_PORT_UNREACH:
Eric Leblondcc70d062013-12-29 12:28:13 +010060 nf_send_unreach6(net, skb, ICMPV6_PORT_UNREACH, par->hooknum);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090061 break;
62 case IP6T_ICMP6_ECHOREPLY:
Patrick McHardy764d8a92005-08-21 23:31:06 -070063 /* Do nothing */
64 break;
65 case IP6T_TCP_RESET:
Eric Leblondcc70d062013-12-29 12:28:13 +010066 nf_send_reset6(net, skb, par->hooknum);
Patrick McHardy764d8a92005-08-21 23:31:06 -070067 break;
68 default:
Joe Perchese87cc472012-05-13 21:56:26 +000069 net_info_ratelimited("case %u not handled yet\n", reject->with);
Patrick McHardy764d8a92005-08-21 23:31:06 -070070 break;
71 }
72
73 return NF_DROP;
74}
75
Jan Engelhardt135367b2010-03-19 17:16:42 +010076static int reject_tg6_check(const struct xt_tgchk_param *par)
Patrick McHardy764d8a92005-08-21 23:31:06 -070077{
Jan Engelhardtaf5d6dc2008-10-08 11:35:19 +020078 const struct ip6t_reject_info *rejinfo = par->targinfo;
79 const struct ip6t_entry *e = par->entryinfo;
Patrick McHardy764d8a92005-08-21 23:31:06 -070080
Patrick McHardy764d8a92005-08-21 23:31:06 -070081 if (rejinfo->with == IP6T_ICMP6_ECHOREPLY) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010082 pr_info("ECHOREPLY is not supported.\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010083 return -EINVAL;
Patrick McHardy764d8a92005-08-21 23:31:06 -070084 } else if (rejinfo->with == IP6T_TCP_RESET) {
85 /* Must specify that it's a TCP packet */
Joe Perches3666ed12009-11-23 23:17:06 +010086 if (e->ipv6.proto != IPPROTO_TCP ||
87 (e->ipv6.invflags & XT_INV_PROTO)) {
Jan Engelhardtff67e4e2010-03-19 21:08:16 +010088 pr_info("TCP_RESET illegal for non-tcp\n");
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010089 return -EINVAL;
Patrick McHardy764d8a92005-08-21 23:31:06 -070090 }
91 }
Jan Engelhardtd6b00a52010-03-25 16:34:45 +010092 return 0;
Patrick McHardy764d8a92005-08-21 23:31:06 -070093}
94
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080095static struct xt_target reject_tg6_reg __read_mostly = {
Patrick McHardy764d8a92005-08-21 23:31:06 -070096 .name = "REJECT",
Jan Engelhardtee999d82008-10-08 11:35:01 +020097 .family = NFPROTO_IPV6,
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080098 .target = reject_tg6,
Patrick McHardy7f939712006-03-20 18:01:43 -080099 .targetsize = sizeof(struct ip6t_reject_info),
100 .table = "filter",
Patrick McHardy6e23ae22007-11-19 18:53:30 -0800101 .hooks = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
102 (1 << NF_INET_LOCAL_OUT),
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800103 .checkentry = reject_tg6_check,
Patrick McHardy764d8a92005-08-21 23:31:06 -0700104 .me = THIS_MODULE
105};
106
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800107static int __init reject_tg6_init(void)
Patrick McHardy764d8a92005-08-21 23:31:06 -0700108{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800109 return xt_register_target(&reject_tg6_reg);
Patrick McHardy764d8a92005-08-21 23:31:06 -0700110}
111
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800112static void __exit reject_tg6_exit(void)
Patrick McHardy764d8a92005-08-21 23:31:06 -0700113{
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800114 xt_unregister_target(&reject_tg6_reg);
Patrick McHardy764d8a92005-08-21 23:31:06 -0700115}
116
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -0800117module_init(reject_tg6_init);
118module_exit(reject_tg6_exit);