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

TOMOYO Linux Cross Reference
Linux/net/ipv6/netfilter/nf_reject_ipv6.c

Version: ~ [ linux-5.11-rc3 ] ~ [ linux-5.10.7 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.89 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.167 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.215 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.251 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.251 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ 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 // SPDX-License-Identifier: GPL-2.0-only
  2 /* (C) 1999-2001 Paul `Rusty' Russell
  3  * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
  4  */
  5 
  6 #include <linux/module.h>
  7 #include <net/ipv6.h>
  8 #include <net/ip6_route.h>
  9 #include <net/ip6_fib.h>
 10 #include <net/ip6_checksum.h>
 11 #include <net/netfilter/ipv6/nf_reject.h>
 12 #include <linux/netfilter_ipv6.h>
 13 #include <linux/netfilter_bridge.h>
 14 
 15 const struct tcphdr *nf_reject_ip6_tcphdr_get(struct sk_buff *oldskb,
 16                                               struct tcphdr *otcph,
 17                                               unsigned int *otcplen, int hook)
 18 {
 19         const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
 20         u8 proto;
 21         __be16 frag_off;
 22         int tcphoff;
 23 
 24         proto = oip6h->nexthdr;
 25         tcphoff = ipv6_skip_exthdr(oldskb, ((u8 *)(oip6h + 1) - oldskb->data),
 26                                    &proto, &frag_off);
 27 
 28         if ((tcphoff < 0) || (tcphoff > oldskb->len)) {
 29                 pr_debug("Cannot get TCP header.\n");
 30                 return NULL;
 31         }
 32 
 33         *otcplen = oldskb->len - tcphoff;
 34 
 35         /* IP header checks: fragment, too short. */
 36         if (proto != IPPROTO_TCP || *otcplen < sizeof(struct tcphdr)) {
 37                 pr_debug("proto(%d) != IPPROTO_TCP or too short (len = %d)\n",
 38                          proto, *otcplen);
 39                 return NULL;
 40         }
 41 
 42         otcph = skb_header_pointer(oldskb, tcphoff, sizeof(struct tcphdr),
 43                                    otcph);
 44         if (otcph == NULL)
 45                 return NULL;
 46 
 47         /* No RST for RST. */
 48         if (otcph->rst) {
 49                 pr_debug("RST is set\n");
 50                 return NULL;
 51         }
 52 
 53         /* Check checksum. */
 54         if (nf_ip6_checksum(oldskb, hook, tcphoff, IPPROTO_TCP)) {
 55                 pr_debug("TCP checksum is invalid\n");
 56                 return NULL;
 57         }
 58 
 59         return otcph;
 60 }
 61 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_get);
 62 
 63 struct ipv6hdr *nf_reject_ip6hdr_put(struct sk_buff *nskb,
 64                                      const struct sk_buff *oldskb,
 65                                      __u8 protocol, int hoplimit)
 66 {
 67         struct ipv6hdr *ip6h;
 68         const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
 69 #define DEFAULT_TOS_VALUE       0x0U
 70         const __u8 tclass = DEFAULT_TOS_VALUE;
 71 
 72         skb_put(nskb, sizeof(struct ipv6hdr));
 73         skb_reset_network_header(nskb);
 74         ip6h = ipv6_hdr(nskb);
 75         ip6_flow_hdr(ip6h, tclass, 0);
 76         ip6h->hop_limit = hoplimit;
 77         ip6h->nexthdr = protocol;
 78         ip6h->saddr = oip6h->daddr;
 79         ip6h->daddr = oip6h->saddr;
 80 
 81         nskb->protocol = htons(ETH_P_IPV6);
 82 
 83         return ip6h;
 84 }
 85 EXPORT_SYMBOL_GPL(nf_reject_ip6hdr_put);
 86 
 87 void nf_reject_ip6_tcphdr_put(struct sk_buff *nskb,
 88                               const struct sk_buff *oldskb,
 89                               const struct tcphdr *oth, unsigned int otcplen)
 90 {
 91         struct tcphdr *tcph;
 92         int needs_ack;
 93 
 94         skb_reset_transport_header(nskb);
 95         tcph = skb_put(nskb, sizeof(struct tcphdr));
 96         /* Truncate to length (no data) */
 97         tcph->doff = sizeof(struct tcphdr)/4;
 98         tcph->source = oth->dest;
 99         tcph->dest = oth->source;
100 
101         if (oth->ack) {
102                 needs_ack = 0;
103                 tcph->seq = oth->ack_seq;
104                 tcph->ack_seq = 0;
105         } else {
106                 needs_ack = 1;
107                 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
108                                       otcplen - (oth->doff<<2));
109                 tcph->seq = 0;
110         }
111 
112         /* Reset flags */
113         ((u_int8_t *)tcph)[13] = 0;
114         tcph->rst = 1;
115         tcph->ack = needs_ack;
116         tcph->window = 0;
117         tcph->urg_ptr = 0;
118         tcph->check = 0;
119 
120         /* Adjust TCP checksum */
121         tcph->check = csum_ipv6_magic(&ipv6_hdr(nskb)->saddr,
122                                       &ipv6_hdr(nskb)->daddr,
123                                       sizeof(struct tcphdr), IPPROTO_TCP,
124                                       csum_partial(tcph,
125                                                    sizeof(struct tcphdr), 0));
126 }
127 EXPORT_SYMBOL_GPL(nf_reject_ip6_tcphdr_put);
128 
129 void nf_send_reset6(struct net *net, struct sk_buff *oldskb, int hook)
130 {
131         struct net_device *br_indev __maybe_unused;
132         struct sk_buff *nskb;
133         struct tcphdr _otcph;
134         const struct tcphdr *otcph;
135         unsigned int otcplen, hh_len;
136         const struct ipv6hdr *oip6h = ipv6_hdr(oldskb);
137         struct ipv6hdr *ip6h;
138         struct dst_entry *dst = NULL;
139         struct flowi6 fl6;
140 
141         if ((!(ipv6_addr_type(&oip6h->saddr) & IPV6_ADDR_UNICAST)) ||
142             (!(ipv6_addr_type(&oip6h->daddr) & IPV6_ADDR_UNICAST))) {
143                 pr_debug("addr is not unicast.\n");
144                 return;
145         }
146 
147         otcph = nf_reject_ip6_tcphdr_get(oldskb, &_otcph, &otcplen, hook);
148         if (!otcph)
149                 return;
150 
151         memset(&fl6, 0, sizeof(fl6));
152         fl6.flowi6_proto = IPPROTO_TCP;
153         fl6.saddr = oip6h->daddr;
154         fl6.daddr = oip6h->saddr;
155         fl6.fl6_sport = otcph->dest;
156         fl6.fl6_dport = otcph->source;
157         fl6.flowi6_oif = l3mdev_master_ifindex(skb_dst(oldskb)->dev);
158         fl6.flowi6_mark = IP6_REPLY_MARK(net, oldskb->mark);
159         security_skb_classify_flow(oldskb, flowi6_to_flowi(&fl6));
160         dst = ip6_route_output(net, NULL, &fl6);
161         if (dst->error) {
162                 dst_release(dst);
163                 return;
164         }
165         dst = xfrm_lookup(net, dst, flowi6_to_flowi(&fl6), NULL, 0);
166         if (IS_ERR(dst))
167                 return;
168 
169         hh_len = (dst->dev->hard_header_len + 15)&~15;
170         nskb = alloc_skb(hh_len + 15 + dst->header_len + sizeof(struct ipv6hdr)
171                          + sizeof(struct tcphdr) + dst->trailer_len,
172                          GFP_ATOMIC);
173 
174         if (!nskb) {
175                 net_dbg_ratelimited("cannot alloc skb\n");
176                 dst_release(dst);
177                 return;
178         }
179 
180         skb_dst_set(nskb, dst);
181 
182         nskb->mark = fl6.flowi6_mark;
183 
184         skb_reserve(nskb, hh_len + dst->header_len);
185         ip6h = nf_reject_ip6hdr_put(nskb, oldskb, IPPROTO_TCP,
186                                     ip6_dst_hoplimit(dst));
187         nf_reject_ip6_tcphdr_put(nskb, oldskb, otcph, otcplen);
188 
189         nf_ct_attach(nskb, oldskb);
190 
191 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
192         /* If we use ip6_local_out for bridged traffic, the MAC source on
193          * the RST will be ours, instead of the destination's.  This confuses
194          * some routers/firewalls, and they drop the packet.  So we need to
195          * build the eth header using the original destination's MAC as the
196          * source, and send the RST packet directly.
197          */
198         br_indev = nf_bridge_get_physindev(oldskb);
199         if (br_indev) {
200                 struct ethhdr *oeth = eth_hdr(oldskb);
201 
202                 nskb->dev = br_indev;
203                 nskb->protocol = htons(ETH_P_IPV6);
204                 ip6h->payload_len = htons(sizeof(struct tcphdr));
205                 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
206                                     oeth->h_source, oeth->h_dest, nskb->len) < 0) {
207                         kfree_skb(nskb);
208                         return;
209                 }
210                 dev_queue_xmit(nskb);
211         } else
212 #endif
213                 ip6_local_out(net, nskb->sk, nskb);
214 }
215 EXPORT_SYMBOL_GPL(nf_send_reset6);
216 
217 static bool reject6_csum_ok(struct sk_buff *skb, int hook)
218 {
219         const struct ipv6hdr *ip6h = ipv6_hdr(skb);
220         int thoff;
221         __be16 fo;
222         u8 proto;
223 
224         if (skb_csum_unnecessary(skb))
225                 return true;
226 
227         proto = ip6h->nexthdr;
228         thoff = ipv6_skip_exthdr(skb, ((u8 *)(ip6h + 1) - skb->data), &proto, &fo);
229 
230         if (thoff < 0 || thoff >= skb->len || (fo & htons(~0x7)) != 0)
231                 return false;
232 
233         if (!nf_reject_verify_csum(proto))
234                 return true;
235 
236         return nf_ip6_checksum(skb, hook, thoff, proto) == 0;
237 }
238 
239 void nf_send_unreach6(struct net *net, struct sk_buff *skb_in,
240                       unsigned char code, unsigned int hooknum)
241 {
242         if (!reject6_csum_ok(skb_in, hooknum))
243                 return;
244 
245         if (hooknum == NF_INET_LOCAL_OUT && skb_in->dev == NULL)
246                 skb_in->dev = net->loopback_dev;
247 
248         icmpv6_send(skb_in, ICMPV6_DEST_UNREACH, code, 0);
249 }
250 EXPORT_SYMBOL_GPL(nf_send_unreach6);
251 
252 MODULE_LICENSE("GPL");
253 

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