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