Linux-libre 3.14.34-gnu
[librecmc/linux-libre.git] / include / net / netfilter / ipv4 / nf_reject.h
1 #ifndef _IPV4_NF_REJECT_H
2 #define _IPV4_NF_REJECT_H
3
4 #include <net/ip.h>
5 #include <net/tcp.h>
6 #include <net/route.h>
7 #include <net/dst.h>
8
9 static inline void nf_send_unreach(struct sk_buff *skb_in, int code)
10 {
11         icmp_send(skb_in, ICMP_DEST_UNREACH, code, 0);
12 }
13
14 /* Send RST reply */
15 static void nf_send_reset(struct sk_buff *oldskb, int hook)
16 {
17         struct sk_buff *nskb;
18         const struct iphdr *oiph;
19         struct iphdr *niph;
20         const struct tcphdr *oth;
21         struct tcphdr _otcph, *tcph;
22
23         /* IP header checks: fragment. */
24         if (ip_hdr(oldskb)->frag_off & htons(IP_OFFSET))
25                 return;
26
27         oth = skb_header_pointer(oldskb, ip_hdrlen(oldskb),
28                                  sizeof(_otcph), &_otcph);
29         if (oth == NULL)
30                 return;
31
32         /* No RST for RST. */
33         if (oth->rst)
34                 return;
35
36         if (skb_rtable(oldskb)->rt_flags & (RTCF_BROADCAST | RTCF_MULTICAST))
37                 return;
38
39         /* Check checksum */
40         if (nf_ip_checksum(oldskb, hook, ip_hdrlen(oldskb), IPPROTO_TCP))
41                 return;
42         oiph = ip_hdr(oldskb);
43
44         nskb = alloc_skb(sizeof(struct iphdr) + sizeof(struct tcphdr) +
45                          LL_MAX_HEADER, GFP_ATOMIC);
46         if (!nskb)
47                 return;
48
49         skb_reserve(nskb, LL_MAX_HEADER);
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  = IPPROTO_TCP;
59         niph->check     = 0;
60         niph->saddr     = oiph->daddr;
61         niph->daddr     = oiph->saddr;
62
63         skb_reset_transport_header(nskb);
64         tcph = (struct tcphdr *)skb_put(nskb, sizeof(struct tcphdr));
65         memset(tcph, 0, sizeof(*tcph));
66         tcph->source    = oth->dest;
67         tcph->dest      = oth->source;
68         tcph->doff      = sizeof(struct tcphdr) / 4;
69
70         if (oth->ack)
71                 tcph->seq = oth->ack_seq;
72         else {
73                 tcph->ack_seq = htonl(ntohl(oth->seq) + oth->syn + oth->fin +
74                                       oldskb->len - ip_hdrlen(oldskb) -
75                                       (oth->doff << 2));
76                 tcph->ack = 1;
77         }
78
79         tcph->rst       = 1;
80         tcph->check = ~tcp_v4_check(sizeof(struct tcphdr), niph->saddr,
81                                     niph->daddr, 0);
82         nskb->ip_summed = CHECKSUM_PARTIAL;
83         nskb->csum_start = (unsigned char *)tcph - nskb->head;
84         nskb->csum_offset = offsetof(struct tcphdr, check);
85
86         /* ip_route_me_harder expects skb->dst to be set */
87         skb_dst_set_noref(nskb, skb_dst(oldskb));
88
89         nskb->protocol = htons(ETH_P_IP);
90         if (ip_route_me_harder(nskb, RTN_UNSPEC))
91                 goto free_nskb;
92
93         niph->ttl       = ip4_dst_hoplimit(skb_dst(nskb));
94
95         /* "Never happens" */
96         if (nskb->len > dst_mtu(skb_dst(nskb)))
97                 goto free_nskb;
98
99         nf_ct_attach(nskb, oldskb);
100
101 #ifdef CONFIG_BRIDGE_NETFILTER
102         /* If we use ip_local_out for bridged traffic, the MAC source on
103          * the RST will be ours, instead of the destination's.  This confuses
104          * some routers/firewalls, and they drop the packet.  So we need to
105          * build the eth header using the original destination's MAC as the
106          * source, and send the RST packet directly.
107          */
108         if (oldskb->nf_bridge) {
109                 struct ethhdr *oeth = eth_hdr(oldskb);
110                 nskb->dev = oldskb->nf_bridge->physindev;
111                 niph->tot_len = htons(nskb->len);
112                 ip_send_check(niph);
113                 if (dev_hard_header(nskb, nskb->dev, ntohs(nskb->protocol),
114                                     oeth->h_source, oeth->h_dest, nskb->len) < 0)
115                         goto free_nskb;
116                 dev_queue_xmit(nskb);
117         } else
118 #endif
119                 ip_local_out(nskb);
120
121         return;
122
123  free_nskb:
124         kfree_skb(nskb);
125 }
126
127
128 #endif /* _IPV4_NF_REJECT_H */