~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/netfilter/ipt_REJECT.c

Version: ~ [ linux-5.19-rc8 ] ~ [ linux-5.18.14 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.57 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.133 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.207 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.253 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.289 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.324 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.302 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * This is a module which is used for rejecting packets.
  3  */
  4 
  5 /* (C) 1999-2001 Paul `Rusty' Russell
  6  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  7  *
  8  * This program is free software; you can redistribute it and/or modify
  9  * it under the terms of the GNU General Public License version 2 as
 10  * published by the Free Software Foundation.
 11  */
 12 
 13 #include <linux/module.h>
 14 #include <linux/skbuff.h>
 15 #include <linux/ip.h>
 16 #include <linux/udp.h>
 17 #include <linux/icmp.h>
 18 #include <net/icmp.h>
 19 #include <net/ip.h>
 20 #include <net/tcp.h>
 21 #include <net/route.h>
 22 #include <net/dst.h>
 23 #include <linux/netfilter/x_tables.h>
 24 #include <linux/netfilter_ipv4/ip_tables.h>
 25 #include <linux/netfilter_ipv4/ipt_REJECT.h>
 26 #ifdef CONFIG_BRIDGE_NETFILTER
 27 #include <linux/netfilter_bridge.h>
 28 #endif
 29 
 30 MODULE_LICENSE("GPL");
 31 MODULE_AUTHOR("Netfilter Core Team <coreteam@netfilter.org>");
 32 MODULE_DESCRIPTION("Xtables: packet \"rejection\" target for IPv4");
 33 
 34 /* Send RST reply */
 35 static void send_reset(struct sk_buff *oldskb, int hook)
 36 {
 37         struct sk_buff *nskb;
 38         const struct iphdr *oiph;
 39         struct iphdr *niph;
 40         const struct tcphdr *oth;
 41         struct tcphdr _otcph, *tcph;
 42         unsigned int addr_type;
 43 
 44         /* IP header checks: fragment. */
 45         if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
 46                 return;
 47 
 48         oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
 49                                  sizeof(_otcph), &_otcph);
 50         if (oth == NULL)
 51                 return;
 52 
 53         /* No RST for RST. */
 54         if (oth->rst)
 55                 return;
 56 
 57         /* Check checksum */
 58         if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
 59                 return;
 60         oiph = ip_hdr(oldskb);
 61 
 62         nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
 63                          LL_MAX_HEADER, GFP_ATOMIC);
 64         if (!nskb)
 65                 return;
 66 
 67         skb_reserve(nskb, LL_MAX_HEADER);
 68 
 69         skb_reset_network_header(nskb);
 70         niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
 71         niph->version   = 4;
 72         niph->ihl       = sizeof(struct iphdr) / 4;
 73         niph->tos       = 0;
 74         niph->id        = 0;
 75         niph->frag_off  = htons(IP_DF);
 76         niph->protocol  = IPPROTO_TCP;
 77         niph->check     = 0;
 78         niph->saddr     = oiph->daddr;
 79         niph->daddr     = oiph->saddr;
 80 
 81         tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
 82         memset(tcph, 0, sizeof(*tcph));
 83         tcph->source    = oth->dest;
 84         tcph->dest      = oth->source;
 85         tcph->doff      = sizeof(struct tcphdr) / 4;
 86 
 87         if (oth->ack)
 88                 tcph->seq = oth->ack_seq;
 89         else {
 90                 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
 91                                       oldskb->len - ip_hdrlen(oldskb) -
 92                                       (oth->doff << 2));
 93                 tcph->ack = 1;
 94         }
 95 
 96         tcph->rst       = 1;
 97         tcph->check     = tcp_v4_check(sizeof(struct tcphdr),
 98                                        niph->saddr, niph->daddr,
 99                                        csum_partial(tcph,
100                                                     sizeof(struct tcphdr), 0));
101 
102         addr_type = RTN_UNSPEC;
103         if (hook != NF_INET_FORWARD
104 #ifdef CONFIG_BRIDGE_NETFILTER
105             || (nskb->nf_bridge && nskb->nf_bridge->mask & BRNF_BRIDGED)
106 #endif
107            )
108                 addr_type = RTN_LOCAL;
109 
110         /* ip_route_me_harder expects skb->dst to be set */
111         skb_dst_set(nskb, dst_clone(skb_dst(oldskb)));
112 
113         if (ip_route_me_harder(nskb, addr_type))
114                 goto free_nskb;
115 
116         niph->ttl       = dst_metric(skb_dst(nskb), RTAX_HOPLIMIT);
117         nskb->ip_summed = CHECKSUM_NONE;
118 
119         /* "Never happens" */
120         if (nskb->len > dst_mtu(skb_dst(nskb)))
121                 goto free_nskb;
122 
123         nf_ct_attach(nskb, oldskb);
124 
125         ip_local_out(nskb);
126         return;
127 
128  free_nskb:
129         kfree_skb(nskb);
130 }
131 
132 static inline void send_unreach(struct sk_buff *skb_in, int code)
133 {
134         icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
135 }
136 
137 static unsigned int
138 reject_tg(struct sk_buff *skb, const struct xt_target_param *par)
139 {
140         const struct ipt_reject_info *reject = par->targinfo;
141 
142         /* WARNING: This code causes reentry within iptables.
143            This means that the iptables jump stack is now crap.  We
144            must return an absolute verdict. --RR */
145         switch (reject->with) {
146         case IPT_ICMP_NET_UNREACHABLE:
147                 send_unreach(skb, ICMP_NET_UNREACH);
148                 break;
149         case IPT_ICMP_HOST_UNREACHABLE:
150                 send_unreach(skb, ICMP_HOST_UNREACH);
151                 break;
152         case IPT_ICMP_PROT_UNREACHABLE:
153                 send_unreach(skb, ICMP_PROT_UNREACH);
154                 break;
155         case IPT_ICMP_PORT_UNREACHABLE:
156                 send_unreach(skb, ICMP_PORT_UNREACH);
157                 break;
158         case IPT_ICMP_NET_PROHIBITED:
159                 send_unreach(skb, ICMP_NET_ANO);
160                 break;
161         case IPT_ICMP_HOST_PROHIBITED:
162                 send_unreach(skb, ICMP_HOST_ANO);
163                 break;
164         case IPT_ICMP_ADMIN_PROHIBITED:
165                 send_unreach(skb, ICMP_PKT_FILTERED);
166                 break;
167         case IPT_TCP_RESET:
168                 send_reset(skb, par->hooknum);
169         case IPT_ICMP_ECHOREPLY:
170                 /* Doesn't happen. */
171                 break;
172         }
173 
174         return NF_DROP;
175 }
176 
177 static bool reject_tg_check(const struct xt_tgchk_param *par)
178 {
179         const struct ipt_reject_info *rejinfo = par->targinfo;
180         const struct ipt_entry *e = par->entryinfo;
181 
182         if (rejinfo->with == IPT_ICMP_ECHOREPLY) {
183                 printk("ipt_REJECT: ECHOREPLY no longer supported.\n");
184                 return false;
185         } else if (rejinfo->with == IPT_TCP_RESET) {
186                 /* Must specify that it's a TCP packet */
187                 if (e->ip.proto != IPPROTO_TCP
188                     || (e->ip.invflags & XT_INV_PROTO)) {
189                         printk("ipt_REJECT: TCP_RESET invalid for non-tcp\n");
190                         return false;
191                 }
192         }
193         return true;
194 }
195 
196 static struct xt_target reject_tg_reg __read_mostly = {
197         .name           = "REJECT",
198         .family         = NFPROTO_IPV4,
199         .target         = reject_tg,
200         .targetsize     = sizeof(struct ipt_reject_info),
201         .table          = "filter",
202         .hooks          = (1 << NF_INET_LOCAL_IN) | (1 << NF_INET_FORWARD) |
203                           (1 << NF_INET_LOCAL_OUT),
204         .checkentry     = reject_tg_check,
205         .me             = THIS_MODULE,
206 };
207 
208 static int __init reject_tg_init(void)
209 {
210         return xt_register_target(&reject_tg_reg);
211 }
212 
213 static void __exit reject_tg_exit(void)
214 {
215         xt_unregister_target(&reject_tg_reg);
216 }
217 
218 module_init(reject_tg_init);
219 module_exit(reject_tg_exit);
220 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp