]>
git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - net/ipv4/netfilter/nf_reject_ipv4.c
1 /* (C) 1999-2001 Paul `Rusty' Russell
2 * (C) 2002-2004 Netfilter Core Team <coreteam@netfilter.org>
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.
9 #include <linux/module.h>
12 #include <net/route.h>
14 #include <net/netfilter/ipv4/nf_reject.h>
15 #include <linux/netfilter_ipv4.h>
16 #include <net/netfilter/ipv4/nf_reject.h>
18 const struct tcphdr
*nf_reject_ip_tcphdr_get(struct sk_buff
*oldskb
,
19 struct tcphdr
*_oth
, int hook
)
21 const struct tcphdr
*oth
;
23 /* IP header checks: fragment. */
24 if (ip_hdr(oldskb
)->frag_off
& htons(IP_OFFSET
))
27 oth
= skb_header_pointer(oldskb
, ip_hdrlen(oldskb
),
28 sizeof(struct tcphdr
), _oth
);
37 if (nf_ip_checksum(oldskb
, hook
, ip_hdrlen(oldskb
), IPPROTO_TCP
))
42 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_get
);
44 struct iphdr
*nf_reject_iphdr_put(struct sk_buff
*nskb
,
45 const struct sk_buff
*oldskb
,
46 __be16 protocol
, int ttl
)
48 struct iphdr
*niph
, *oiph
= ip_hdr(oldskb
);
50 skb_reset_network_header(nskb
);
51 niph
= (struct iphdr
*)skb_put(nskb
, sizeof(struct iphdr
));
53 niph
->ihl
= sizeof(struct iphdr
) / 4;
56 niph
->frag_off
= htons(IP_DF
);
57 niph
->protocol
= protocol
;
59 niph
->saddr
= oiph
->daddr
;
60 niph
->daddr
= oiph
->saddr
;
63 nskb
->protocol
= htons(ETH_P_IP
);
67 EXPORT_SYMBOL_GPL(nf_reject_iphdr_put
);
69 void nf_reject_ip_tcphdr_put(struct sk_buff
*nskb
, const struct sk_buff
*oldskb
,
70 const struct tcphdr
*oth
)
72 struct iphdr
*niph
= ip_hdr(nskb
);
75 skb_reset_transport_header(nskb
);
76 tcph
= (struct tcphdr
*)skb_put(nskb
, sizeof(struct tcphdr
));
77 memset(tcph
, 0, sizeof(*tcph
));
78 tcph
->source
= oth
->dest
;
79 tcph
->dest
= oth
->source
;
80 tcph
->doff
= sizeof(struct tcphdr
) / 4;
83 tcph
->seq
= oth
->ack_seq
;
85 tcph
->ack_seq
= htonl(ntohl(oth
->seq
) + oth
->syn
+ oth
->fin
+
86 oldskb
->len
- ip_hdrlen(oldskb
) -
92 tcph
->check
= ~tcp_v4_check(sizeof(struct tcphdr
), niph
->saddr
,
94 nskb
->ip_summed
= CHECKSUM_PARTIAL
;
95 nskb
->csum_start
= (unsigned char *)tcph
- nskb
->head
;
96 nskb
->csum_offset
= offsetof(struct tcphdr
, check
);
98 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_put
);
101 void nf_send_reset(struct sk_buff
*oldskb
, int hook
)
103 struct sk_buff
*nskb
;
104 const struct iphdr
*oiph
;
106 const struct tcphdr
*oth
;
109 oth
= nf_reject_ip_tcphdr_get(oldskb
, &_oth
, hook
);
113 if (skb_rtable(oldskb
)->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))
116 oiph
= ip_hdr(oldskb
);
118 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct tcphdr
) +
119 LL_MAX_HEADER
, GFP_ATOMIC
);
123 /* ip_route_me_harder expects skb->dst to be set */
124 skb_dst_set_noref(nskb
, skb_dst(oldskb
));
126 skb_reserve(nskb
, LL_MAX_HEADER
);
127 niph
= nf_reject_iphdr_put(nskb
, oldskb
, IPPROTO_TCP
,
128 ip4_dst_hoplimit(skb_dst(nskb
)));
129 nf_reject_ip_tcphdr_put(nskb
, oldskb
, oth
);
131 if (ip_route_me_harder(nskb
, RTN_UNSPEC
))
134 /* "Never happens" */
135 if (nskb
->len
> dst_mtu(skb_dst(nskb
)))
138 nf_ct_attach(nskb
, oldskb
);
140 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
141 /* If we use ip_local_out for bridged traffic, the MAC source on
142 * the RST will be ours, instead of the destination's. This confuses
143 * some routers/firewalls, and they drop the packet. So we need to
144 * build the eth header using the original destination's MAC as the
145 * source, and send the RST packet directly.
147 if (oldskb
->nf_bridge
) {
148 struct ethhdr
*oeth
= eth_hdr(oldskb
);
149 nskb
->dev
= oldskb
->nf_bridge
->physindev
;
150 niph
->tot_len
= htons(nskb
->len
);
152 if (dev_hard_header(nskb
, nskb
->dev
, ntohs(nskb
->protocol
),
153 oeth
->h_source
, oeth
->h_dest
, nskb
->len
) < 0)
155 dev_queue_xmit(nskb
);
165 EXPORT_SYMBOL_GPL(nf_send_reset
);
167 void nf_send_unreach(struct sk_buff
*skb_in
, int code
, int hook
)
169 struct iphdr
*iph
= ip_hdr(skb_in
);
172 if (skb_in
->csum_bad
|| iph
->frag_off
& htons(IP_OFFSET
))
175 if (skb_csum_unnecessary(skb_in
)) {
176 icmp_send(skb_in
, ICMP_DEST_UNREACH
, code
, 0);
180 if (iph
->protocol
== IPPROTO_TCP
|| iph
->protocol
== IPPROTO_UDP
)
181 proto
= iph
->protocol
;
185 if (nf_ip_checksum(skb_in
, hook
, ip_hdrlen(skb_in
), proto
) == 0)
186 icmp_send(skb_in
, ICMP_DEST_UNREACH
, code
, 0);
188 EXPORT_SYMBOL_GPL(nf_send_unreach
);
190 MODULE_LICENSE("GPL");