linux/net/ipv4/netfilter/nf_reject_ipv4.c
<<
>>
Prefs
   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
  18const struct tcphdr *nf_reject_ip_tcphdr_get(struct sk_buff *oldskb,
  19                                             struct tcphdr *_oth, int hook)
  20{
  21        const struct tcphdr *oth;
  22
  23        /* IP header checks: fragment. */
  24        if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
  25                return NULL;
  26
  27        if (ip_hdr(oldskb)->protocol != IPPROTO_TCP)
  28                return NULL;
  29
  30        oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
  31                                 sizeof(struct tcphdr), _oth);
  32        if (oth == NULL)
  33                return NULL;
  34
  35        /* No RST for RST. */
  36        if (oth->rst)
  37                return NULL;
  38
  39        /* Check checksum */
  40        if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
  41                return NULL;
  42
  43        return oth;
  44}
  45EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_get);
  46
  47struct iphdr *nf_reject_iphdr_put(struct sk_buff *nskb,
  48                                  const struct sk_buff *oldskb,
  49                                  __u8 protocol, int ttl)
  50{
  51        struct iphdr *niph, *oiph = ip_hdr(oldskb);
  52
  53        skb_reset_network_header(nskb);
  54        niph = skb_put(nskb, sizeof(struct iphdr));
  55        niph->version   = 4;
  56        niph->ihl       = sizeof(struct iphdr) / 4;
  57        niph->tos       = 0;
  58        niph->id        = 0;
  59        niph->frag_off  = htons(IP_DF);
  60        niph->protocol  = protocol;
  61        niph->check     = 0;
  62        niph->saddr     = oiph->daddr;
  63        niph->daddr     = oiph->saddr;
  64        niph->ttl       = ttl;
  65
  66        nskb->protocol = htons(ETH_P_IP);
  67
  68        return niph;
  69}
  70EXPORT_SYMBOL_GPL(nf_reject_iphdr_put);
  71
  72void nf_reject_ip_tcphdr_put(struct sk_buff *nskb, const struct sk_buff *oldskb,
  73                          const struct tcphdr *oth)
  74{
  75        struct iphdr *niph = ip_hdr(nskb);
  76        struct tcphdr *tcph;
  77
  78        skb_reset_transport_header(nskb);
  79        tcph = skb_put_zero(nskb, sizeof(struct tcphdr));
  80        tcph->source    = oth->dest;
  81        tcph->dest      = oth->source;
  82        tcph->doff      = sizeof(struct tcphdr) / 4;
  83
  84        if (oth->ack) {
  85                tcph->seq = oth->ack_seq;
  86        } else {
  87                tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
  88                                      oldskb->len - ip_hdrlen(oldskb) -
  89                                      (oth->doff << 2));
  90                tcph->ack = 1;
  91        }
  92
  93        tcph->rst       = 1;
  94        tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
  95                                    niph->daddr, 0);
  96        nskb->ip_summed = CHECKSUM_PARTIAL;
  97        nskb->csum_start = (unsigned char *)tcph - nskb->head;
  98        nskb->csum_offset = offsetof(struct tcphdr, check);
  99}
 100EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_put);
 101
 102/* Send RST reply */
 103void nf_send_reset(struct net *net, struct sk_buff *oldskb, int hook)
 104{
 105        struct sk_buff *nskb;
 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        nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
 118                         LL_MAX_HEADER, GFP_ATOMIC);
 119        if (!nskb)
 120                return;
 121
 122        /* ip_route_me_harder expects skb->dst to be set */
 123        skb_dst_set_noref(nskb, skb_dst(oldskb));
 124
 125        nskb->mark = IP4_REPLY_MARK(net, oldskb->mark);
 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(net, nskb, RTN_UNSPEC))
 133                goto free_nskb;
 134
 135        niph = ip_hdr(nskb);
 136
 137        /* "Never happens" */
 138        if (nskb->len > dst_mtu(skb_dst(nskb)))
 139                goto free_nskb;
 140
 141        nf_ct_attach(nskb, oldskb);
 142
 143#if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
 144        /* If we use ip_local_out for bridged traffic, the MAC source on
 145         * the RST will be ours, instead of the destination's.  This confuses
 146         * some routers/firewalls, and they drop the packet.  So we need to
 147         * build the eth header using the original destination's MAC as the
 148         * source, and send the RST packet directly.
 149         */
 150        if (oldskb->nf_bridge) {
 151                struct ethhdr *oeth = eth_hdr(oldskb);
 152
 153                nskb->dev = nf_bridge_get_physindev(oldskb);
 154                niph->tot_len = htons(nskb->len);
 155                ip_send_check(niph);
 156                if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
 157                                    oeth->h_source, oeth->h_dest, nskb->len) < 0)
 158                        goto free_nskb;
 159                dev_queue_xmit(nskb);
 160        } else
 161#endif
 162                ip_local_out(net, nskb->sk, nskb);
 163
 164        return;
 165
 166 free_nskb:
 167        kfree_skb(nskb);
 168}
 169EXPORT_SYMBOL_GPL(nf_send_reset);
 170
 171void nf_send_unreach(struct sk_buff *skb_in, int code, int hook)
 172{
 173        struct iphdr *iph = ip_hdr(skb_in);
 174        u8 proto;
 175
 176        if (iph->frag_off & htons(IP_OFFSET))
 177                return;
 178
 179        if (skb_csum_unnecessary(skb_in)) {
 180                icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
 181                return;
 182        }
 183
 184        if (iph->protocol == IPPROTO_TCP || iph->protocol == IPPROTO_UDP)
 185                proto = iph->protocol;
 186        else
 187                proto = 0;
 188
 189        if (nf_ip_checksum(skb_in, hook, ip_hdrlen(skb_in), proto) == 0)
 190                icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
 191}
 192EXPORT_SYMBOL_GPL(nf_send_unreach);
 193
 194MODULE_LICENSE("GPL");
 195