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

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

Version: ~ [ linux-5.13-rc5 ] ~ [ linux-5.12.9 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.42 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.124 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.193 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.235 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.271 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.271 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ 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.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /* (C) 1999-2001 Paul `Rusty' Russell
  2  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  3  *
  4  * This program is free software; you can redistribute it and/or modify
  5  * it under the terms of the GNU General Public License version 2 as
  6  * published by the Free Software Foundation.
  7  */
  8 
  9 #include <linux/module.h>
 10 #include <net/ip.h>
 11 #include <net/tcp.h>
 12 #include <net/route.h>
 13 #include <net/dst.h>
 14 #include <net/netfilter/ipv4/nf_reject.h>
 15 #include <linux/netfilter_ipv4.h>
 16 #include <linux/netfilter_bridge.h>
 17 #include <net/netfilter/ipv4/nf_reject.h>
 18 
 19 const struct tcphdr *nf_reject_ip_tcphdr_get(struct sk_buff *oldskb,
 20                                              struct tcphdr *_oth, int hook)
 21 {
 22         const struct tcphdr *oth;
 23 
 24         /* IP header checks: fragment. */
 25         if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
 26                 return NULL;
 27 
 28         oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
 29                                  sizeof(struct tcphdr), _oth);
 30         if (oth == NULL)
 31                 return NULL;
 32 
 33         /* No RST for RST. */
 34         if (oth->rst)
 35                 return NULL;
 36 
 37         /* Check checksum */
 38         if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
 39                 return NULL;
 40 
 41         return oth;
 42 }
 43 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_get);
 44 
 45 struct iphdr *nf_reject_iphdr_put(struct sk_buff *nskb,
 46                                   const struct sk_buff *oldskb,
 47                                   __u8 protocol, int ttl)
 48 {
 49         struct iphdr *niph, *oiph = ip_hdr(oldskb);
 50 
 51         skb_reset_network_header(nskb);
 52         niph = (struct iphdr *)skb_put(nskb, sizeof(struct iphdr));
 53         niph->version   = 4;
 54         niph->ihl       = sizeof(struct iphdr) / 4;
 55         niph->tos       = 0;
 56         niph->id        = 0;
 57         niph->frag_off  = htons(IP_DF);
 58         niph->protocol  = protocol;
 59         niph->check     = 0;
 60         niph->saddr     = oiph->daddr;
 61         niph->daddr     = oiph->saddr;
 62         niph->ttl       = ttl;
 63 
 64         nskb->protocol = htons(ETH_P_IP);
 65 
 66         return niph;
 67 }
 68 EXPORT_SYMBOL_GPL(nf_reject_iphdr_put);
 69 
 70 void nf_reject_ip_tcphdr_put(struct sk_buff *nskb, const struct sk_buff *oldskb,
 71                           const struct tcphdr *oth)
 72 {
 73         struct iphdr *niph = ip_hdr(nskb);
 74         struct tcphdr *tcph;
 75 
 76         skb_reset_transport_header(nskb);
 77         tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
 78         memset(tcph, 0, sizeof(*tcph));
 79         tcph->source    = oth->dest;
 80         tcph->dest      = oth->source;
 81         tcph->doff      = sizeof(struct tcphdr) / 4;
 82 
 83         if (oth->ack) {
 84                 tcph->seq = oth->ack_seq;
 85         } else {
 86                 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
 87                                       oldskb->len - ip_hdrlen(oldskb) -
 88                                       (oth->doff << 2));
 89                 tcph->ack = 1;
 90         }
 91 
 92         tcph->rst       = 1;
 93         tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
 94                                     niph->daddr, 0);
 95         nskb->ip_summed = CHECKSUM_PARTIAL;
 96         nskb->csum_start = (unsigned char *)tcph - nskb->head;
 97         nskb->csum_offset = offsetof(struct tcphdr, check);
 98 }
 99 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_put);
100 
101 /* Send RST reply */
102 void nf_send_reset(struct sk_buff *oldskb, int hook)
103 {
104         struct sk_buff *nskb;
105         const struct iphdr *oiph;
106         struct iphdr *niph;
107         const struct tcphdr *oth;
108         struct tcphdr _oth;
109 
110         oth = nf_reject_ip_tcphdr_get(oldskb, &_oth, hook);
111         if (!oth)
112                 return;
113 
114         if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
115                 return;
116 
117         oiph = ip_hdr(oldskb);
118 
119         nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
120                          LL_MAX_HEADER, GFP_ATOMIC);
121         if (!nskb)
122                 return;
123 
124         /* ip_route_me_harder expects skb->dst to be set */
125         skb_dst_set_noref(nskb, skb_dst(oldskb));
126 
127         skb_reserve(nskb, LL_MAX_HEADER);
128         niph = nf_reject_iphdr_put(nskb, oldskb, IPPROTO_TCP,
129                                    ip4_dst_hoplimit(skb_dst(nskb)));
130         nf_reject_ip_tcphdr_put(nskb, oldskb, oth);
131 
132         if (ip_route_me_harder(nskb, RTN_UNSPEC))
133                 goto free_nskb;
134 
135         /* "Never happens" */
136         if (nskb->len > dst_mtu(skb_dst(nskb)))
137                 goto free_nskb;
138 
139         nf_ct_attach(nskb, oldskb);
140 
141 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
142         /* If we use ip_local_out for bridged traffic, the MAC source on
143          * the RST will be ours, instead of the destination's.  This confuses
144          * some routers/firewalls, and they drop the packet.  So we need to
145          * build the eth header using the original destination's MAC as the
146          * source, and send the RST packet directly.
147          */
148         if (oldskb->nf_bridge) {
149                 struct ethhdr *oeth = eth_hdr(oldskb);
150 
151                 nskb->dev = nf_bridge_get_physindev(oldskb);
152                 niph->tot_len = htons(nskb->len);
153                 ip_send_check(niph);
154                 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
155                                     oeth->h_source, oeth->h_dest, nskb->len) < 0)
156                         goto free_nskb;
157                 dev_queue_xmit(nskb);
158         } else
159 #endif
160                 ip_local_out(nskb);
161 
162         return;
163 
164  free_nskb:
165         kfree_skb(nskb);
166 }
167 EXPORT_SYMBOL_GPL(nf_send_reset);
168 
169 void nf_send_unreach(struct sk_buff *skb_in, int code, int hook)
170 {
171         struct iphdr *iph = ip_hdr(skb_in);
172         u8 proto;
173 
174         if (skb_in->csum_bad || iph->frag_off & htons(IP_OFFSET))
175                 return;
176 
177         if (skb_csum_unnecessary(skb_in)) {
178                 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
179                 return;
180         }
181 
182         if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
183                 proto = iph->protocol;
184         else
185                 proto = 0;
186 
187         if (nf_ip_checksum(skb_in, hook, ip_hdrlen(skb_in), proto) == 0)
188                 icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
189 }
190 EXPORT_SYMBOL_GPL(nf_send_unreach);
191 
192 MODULE_LICENSE("GPL");
193 

~ [ 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