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

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

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