]>
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 <linux/netfilter_ipv4.h>
15 #include <net/netfilter/ipv4/nf_reject.h>
17 const struct tcphdr
*nf_reject_ip_tcphdr_get(struct sk_buff
*oldskb
,
18 struct tcphdr
*_oth
, int hook
)
20 const struct tcphdr
*oth
;
22 /* IP header checks: fragment. */
23 if (ip_hdr(oldskb
)->frag_off
& htons(IP_OFFSET
))
26 oth
= skb_header_pointer(oldskb
, ip_hdrlen(oldskb
),
27 sizeof(struct tcphdr
), _oth
);
36 if (nf_ip_checksum(oldskb
, hook
, ip_hdrlen(oldskb
), IPPROTO_TCP
))
41 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_get
);
43 struct iphdr
*nf_reject_iphdr_put(struct sk_buff
*nskb
,
44 const struct sk_buff
*oldskb
,
45 __be16 protocol
, int ttl
)
47 struct iphdr
*niph
, *oiph
= ip_hdr(oldskb
);
49 skb_reset_network_header(nskb
);
50 niph
= (struct iphdr
*)skb_put(nskb
, sizeof(struct iphdr
));
52 niph
->ihl
= sizeof(struct iphdr
) / 4;
55 niph
->frag_off
= htons(IP_DF
);
56 niph
->protocol
= protocol
;
58 niph
->saddr
= oiph
->daddr
;
59 niph
->daddr
= oiph
->saddr
;
62 nskb
->protocol
= htons(ETH_P_IP
);
66 EXPORT_SYMBOL_GPL(nf_reject_iphdr_put
);
68 void nf_reject_ip_tcphdr_put(struct sk_buff
*nskb
, const struct sk_buff
*oldskb
,
69 const struct tcphdr
*oth
)
71 struct iphdr
*niph
= ip_hdr(nskb
);
74 skb_reset_transport_header(nskb
);
75 tcph
= (struct tcphdr
*)skb_put(nskb
, sizeof(struct tcphdr
));
76 memset(tcph
, 0, sizeof(*tcph
));
77 tcph
->source
= oth
->dest
;
78 tcph
->dest
= oth
->source
;
79 tcph
->doff
= sizeof(struct tcphdr
) / 4;
82 tcph
->seq
= oth
->ack_seq
;
84 tcph
->ack_seq
= htonl(ntohl(oth
->seq
) + oth
->syn
+ oth
->fin
+
85 oldskb
->len
- ip_hdrlen(oldskb
) -
91 tcph
->check
= ~tcp_v4_check(sizeof(struct tcphdr
), niph
->saddr
,
93 nskb
->ip_summed
= CHECKSUM_PARTIAL
;
94 nskb
->csum_start
= (unsigned char *)tcph
- nskb
->head
;
95 nskb
->csum_offset
= offsetof(struct tcphdr
, check
);
97 EXPORT_SYMBOL_GPL(nf_reject_ip_tcphdr_put
);
100 void nf_send_reset(struct sk_buff
*oldskb
, int hook
)
102 struct sk_buff
*nskb
;
103 const struct iphdr
*oiph
;
105 const struct tcphdr
*oth
;
108 oth
= nf_reject_ip_tcphdr_get(oldskb
, &_oth
, hook
);
112 if (skb_rtable(oldskb
)->rt_flags
& (RTCF_BROADCAST
| RTCF_MULTICAST
))
115 oiph
= ip_hdr(oldskb
);
117 nskb
= alloc_skb(sizeof(struct iphdr
) + sizeof(struct tcphdr
) +
118 LL_MAX_HEADER
, GFP_ATOMIC
);
122 /* ip_route_me_harder expects skb->dst to be set */
123 skb_dst_set_noref(nskb
, skb_dst(oldskb
));
125 skb_reserve(nskb
, LL_MAX_HEADER
);
126 niph
= nf_reject_iphdr_put(nskb
, oldskb
, IPPROTO_TCP
,
127 ip4_dst_hoplimit(skb_dst(nskb
)));
128 nf_reject_ip_tcphdr_put(nskb
, oldskb
, oth
);
130 if (ip_route_me_harder(nskb
, RTN_UNSPEC
))
133 /* "Never happens" */
134 if (nskb
->len
> dst_mtu(skb_dst(nskb
)))
137 nf_ct_attach(nskb
, oldskb
);
139 #if IS_ENABLED(CONFIG_BRIDGE_NETFILTER)
140 /* If we use ip_local_out for bridged traffic, the MAC source on
141 * the RST will be ours, instead of the destination's. This confuses
142 * some routers/firewalls, and they drop the packet. So we need to
143 * build the eth header using the original destination's MAC as the
144 * source, and send the RST packet directly.
146 if (oldskb
->nf_bridge
) {
147 struct ethhdr
*oeth
= eth_hdr(oldskb
);
148 nskb
->dev
= oldskb
->nf_bridge
->physindev
;
149 niph
->tot_len
= htons(nskb
->len
);
151 if (dev_hard_header(nskb
, nskb
->dev
, ntohs(nskb
->protocol
),
152 oeth
->h_source
, oeth
->h_dest
, nskb
->len
) < 0)
154 dev_queue_xmit(nskb
);
164 EXPORT_SYMBOL_GPL(nf_send_reset
);
166 MODULE_LICENSE("GPL");