]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Extension Header handling for IPv6 | |
3 | * Linux INET6 implementation | |
4 | * | |
5 | * Authors: | |
6 | * Pedro Roque <roque@di.fc.ul.pt> | |
7 | * Andi Kleen <ak@muc.de> | |
8 | * Alexey Kuznetsov <kuznet@ms2.inr.ac.ru> | |
9 | * | |
1da177e4 LT |
10 | * This program is free software; you can redistribute it and/or |
11 | * modify it under the terms of the GNU General Public License | |
12 | * as published by the Free Software Foundation; either version | |
13 | * 2 of the License, or (at your option) any later version. | |
14 | */ | |
15 | ||
16 | /* Changes: | |
1ab1457c | 17 | * yoshfuji : ensure not to overrun while parsing |
1da177e4 LT |
18 | * tlv options. |
19 | * Mitsuru KANDA @USAGI and: Remove ipv6_parse_exthdrs(). | |
20 | * YOSHIFUJI Hideaki @USAGI Register inbound extension header | |
21 | * handlers as inet6_protocol{}. | |
22 | */ | |
23 | ||
24 | #include <linux/errno.h> | |
25 | #include <linux/types.h> | |
26 | #include <linux/socket.h> | |
27 | #include <linux/sockios.h> | |
1da177e4 LT |
28 | #include <linux/net.h> |
29 | #include <linux/netdevice.h> | |
30 | #include <linux/in6.h> | |
31 | #include <linux/icmpv6.h> | |
5a0e3ad6 | 32 | #include <linux/slab.h> |
bc3b2d7f | 33 | #include <linux/export.h> |
1da177e4 | 34 | |
352e512c | 35 | #include <net/dst.h> |
1da177e4 LT |
36 | #include <net/sock.h> |
37 | #include <net/snmp.h> | |
38 | ||
39 | #include <net/ipv6.h> | |
40 | #include <net/protocol.h> | |
41 | #include <net/transp_v6.h> | |
42 | #include <net/rawv6.h> | |
43 | #include <net/ndisc.h> | |
44 | #include <net/ip6_route.h> | |
45 | #include <net/addrconf.h> | |
2e532b70 | 46 | #include <net/calipso.h> |
07a93626 | 47 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
65d4ed92 MN |
48 | #include <net/xfrm.h> |
49 | #endif | |
1ababeba DL |
50 | #include <linux/seg6.h> |
51 | #include <net/seg6.h> | |
9baee834 DL |
52 | #ifdef CONFIG_IPV6_SEG6_HMAC |
53 | #include <net/seg6_hmac.h> | |
54 | #endif | |
1da177e4 | 55 | |
ce256981 | 56 | #include <linux/uaccess.h> |
1da177e4 LT |
57 | |
58 | /* | |
59 | * Parsing tlv encoded headers. | |
60 | * | |
a50feda5 ED |
61 | * Parsing function "func" returns true, if parsing succeed |
62 | * and false, if it failed. | |
1da177e4 LT |
63 | * It MUST NOT touch skb->h. |
64 | */ | |
65 | ||
66 | struct tlvtype_proc { | |
67 | int type; | |
a50feda5 | 68 | bool (*func)(struct sk_buff *skb, int offset); |
1da177e4 LT |
69 | }; |
70 | ||
71 | /********************* | |
72 | Generic functions | |
73 | *********************/ | |
74 | ||
75 | /* An unknown option is detected, decide what to do */ | |
76 | ||
a50feda5 | 77 | static bool ip6_tlvopt_unknown(struct sk_buff *skb, int optoff) |
1da177e4 | 78 | { |
d56f90a7 | 79 | switch ((skb_network_header(skb)[optoff] & 0xC0) >> 6) { |
1da177e4 | 80 | case 0: /* ignore */ |
a50feda5 | 81 | return true; |
1da177e4 LT |
82 | |
83 | case 1: /* drop packet */ | |
84 | break; | |
85 | ||
86 | case 3: /* Send ICMP if not a multicast address and drop packet */ | |
87 | /* Actually, it is redundant check. icmp_send | |
88 | will recheck in any case. | |
89 | */ | |
0660e03f | 90 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) |
1da177e4 | 91 | break; |
275757e6 | 92 | /* fall through */ |
1da177e4 LT |
93 | case 2: /* send ICMP PARM PROB regardless and drop packet */ |
94 | icmpv6_param_prob(skb, ICMPV6_UNK_OPTION, optoff); | |
a50feda5 | 95 | return false; |
3ff50b79 | 96 | } |
1da177e4 LT |
97 | |
98 | kfree_skb(skb); | |
a50feda5 | 99 | return false; |
1da177e4 LT |
100 | } |
101 | ||
102 | /* Parse tlv encoded option header (hop-by-hop or destination) */ | |
103 | ||
a50feda5 | 104 | static bool ip6_parse_tlv(const struct tlvtype_proc *procs, struct sk_buff *skb) |
1da177e4 | 105 | { |
a50feda5 | 106 | const struct tlvtype_proc *curr; |
d56f90a7 | 107 | const unsigned char *nh = skb_network_header(skb); |
cfe1fc77 | 108 | int off = skb_network_header_len(skb); |
9c70220b | 109 | int len = (skb_transport_header(skb)[1] + 1) << 3; |
9b905fe6 | 110 | int padlen = 0; |
1da177e4 | 111 | |
ea2ae17d | 112 | if (skb_transport_offset(skb) + len > skb_headlen(skb)) |
1da177e4 LT |
113 | goto bad; |
114 | ||
115 | off += 2; | |
116 | len -= 2; | |
117 | ||
118 | while (len > 0) { | |
d56f90a7 | 119 | int optlen = nh[off + 1] + 2; |
c1412fce | 120 | int i; |
1da177e4 | 121 | |
d56f90a7 | 122 | switch (nh[off]) { |
1de5a71c | 123 | case IPV6_TLV_PAD1: |
1da177e4 | 124 | optlen = 1; |
9b905fe6 EZ |
125 | padlen++; |
126 | if (padlen > 7) | |
127 | goto bad; | |
1da177e4 LT |
128 | break; |
129 | ||
130 | case IPV6_TLV_PADN: | |
c1412fce EZ |
131 | /* RFC 2460 states that the purpose of PadN is |
132 | * to align the containing header to multiples | |
133 | * of 8. 7 is therefore the highest valid value. | |
134 | * See also RFC 4942, Section 2.1.9.5. | |
135 | */ | |
9b905fe6 EZ |
136 | padlen += optlen; |
137 | if (padlen > 7) | |
c1412fce EZ |
138 | goto bad; |
139 | /* RFC 4942 recommends receiving hosts to | |
140 | * actively check PadN payload to contain | |
141 | * only zeroes. | |
142 | */ | |
143 | for (i = 2; i < optlen; i++) { | |
144 | if (nh[off + i] != 0) | |
145 | goto bad; | |
146 | } | |
1da177e4 LT |
147 | break; |
148 | ||
149 | default: /* Other TLV code so scan list */ | |
150 | if (optlen > len) | |
151 | goto bad; | |
67ba4152 | 152 | for (curr = procs; curr->type >= 0; curr++) { |
d56f90a7 | 153 | if (curr->type == nh[off]) { |
1ab1457c YH |
154 | /* type specific length/alignment |
155 | checks will be performed in the | |
1da177e4 | 156 | func(). */ |
a50feda5 ED |
157 | if (curr->func(skb, off) == false) |
158 | return false; | |
1da177e4 LT |
159 | break; |
160 | } | |
161 | } | |
162 | if (curr->type < 0) { | |
e5bbef20 | 163 | if (ip6_tlvopt_unknown(skb, off) == 0) |
a50feda5 | 164 | return false; |
1da177e4 | 165 | } |
9b905fe6 | 166 | padlen = 0; |
1da177e4 LT |
167 | break; |
168 | } | |
169 | off += optlen; | |
170 | len -= optlen; | |
171 | } | |
9b905fe6 | 172 | |
1da177e4 | 173 | if (len == 0) |
a50feda5 | 174 | return true; |
1da177e4 LT |
175 | bad: |
176 | kfree_skb(skb); | |
a50feda5 | 177 | return false; |
1da177e4 LT |
178 | } |
179 | ||
180 | /***************************** | |
181 | Destination options header. | |
182 | *****************************/ | |
183 | ||
07a93626 | 184 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
a50feda5 | 185 | static bool ipv6_dest_hao(struct sk_buff *skb, int optoff) |
a831f5bb | 186 | { |
a831f5bb MN |
187 | struct ipv6_destopt_hao *hao; |
188 | struct inet6_skb_parm *opt = IP6CB(skb); | |
0660e03f | 189 | struct ipv6hdr *ipv6h = ipv6_hdr(skb); |
a831f5bb MN |
190 | struct in6_addr tmp_addr; |
191 | int ret; | |
192 | ||
193 | if (opt->dsthao) { | |
ba7a46f1 | 194 | net_dbg_ratelimited("hao duplicated\n"); |
a831f5bb MN |
195 | goto discard; |
196 | } | |
197 | opt->dsthao = opt->dst1; | |
198 | opt->dst1 = 0; | |
199 | ||
d56f90a7 | 200 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + optoff); |
a831f5bb MN |
201 | |
202 | if (hao->length != 16) { | |
ba7a46f1 JP |
203 | net_dbg_ratelimited("hao invalid option length = %d\n", |
204 | hao->length); | |
a831f5bb MN |
205 | goto discard; |
206 | } | |
207 | ||
208 | if (!(ipv6_addr_type(&hao->addr) & IPV6_ADDR_UNICAST)) { | |
ba7a46f1 JP |
209 | net_dbg_ratelimited("hao is not an unicast addr: %pI6\n", |
210 | &hao->addr); | |
a831f5bb MN |
211 | goto discard; |
212 | } | |
213 | ||
214 | ret = xfrm6_input_addr(skb, (xfrm_address_t *)&ipv6h->daddr, | |
215 | (xfrm_address_t *)&hao->addr, IPPROTO_DSTOPTS); | |
216 | if (unlikely(ret < 0)) | |
217 | goto discard; | |
218 | ||
219 | if (skb_cloned(skb)) { | |
65c88466 | 220 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) |
a831f5bb MN |
221 | goto discard; |
222 | ||
a831f5bb | 223 | /* update all variable using below by copied skbuff */ |
65c88466 | 224 | hao = (struct ipv6_destopt_hao *)(skb_network_header(skb) + |
d56f90a7 | 225 | optoff); |
65c88466 | 226 | ipv6h = ipv6_hdr(skb); |
a831f5bb MN |
227 | } |
228 | ||
229 | if (skb->ip_summed == CHECKSUM_COMPLETE) | |
230 | skb->ip_summed = CHECKSUM_NONE; | |
231 | ||
4e3fd7a0 AD |
232 | tmp_addr = ipv6h->saddr; |
233 | ipv6h->saddr = hao->addr; | |
234 | hao->addr = tmp_addr; | |
a831f5bb | 235 | |
2456e855 | 236 | if (skb->tstamp == 0) |
a831f5bb MN |
237 | __net_timestamp(skb); |
238 | ||
a50feda5 | 239 | return true; |
a831f5bb MN |
240 | |
241 | discard: | |
242 | kfree_skb(skb); | |
a50feda5 | 243 | return false; |
a831f5bb MN |
244 | } |
245 | #endif | |
246 | ||
a50feda5 | 247 | static const struct tlvtype_proc tlvprocdestopt_lst[] = { |
07a93626 | 248 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
a831f5bb MN |
249 | { |
250 | .type = IPV6_TLV_HAO, | |
251 | .func = ipv6_dest_hao, | |
252 | }, | |
253 | #endif | |
1da177e4 LT |
254 | {-1, NULL} |
255 | }; | |
256 | ||
e5bbef20 | 257 | static int ipv6_destopt_rcv(struct sk_buff *skb) |
1da177e4 | 258 | { |
1da177e4 | 259 | struct inet6_skb_parm *opt = IP6CB(skb); |
07a93626 | 260 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
a831f5bb MN |
261 | __u16 dstbuf; |
262 | #endif | |
897dc80b | 263 | struct dst_entry *dst = skb_dst(skb); |
1da177e4 | 264 | |
ea2ae17d ACM |
265 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
266 | !pskb_may_pull(skb, (skb_transport_offset(skb) + | |
9c70220b | 267 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
1d015503 ED |
268 | __IP6_INC_STATS(dev_net(dst->dev), ip6_dst_idev(dst), |
269 | IPSTATS_MIB_INHDRERRORS); | |
1da177e4 LT |
270 | kfree_skb(skb); |
271 | return -1; | |
272 | } | |
273 | ||
cfe1fc77 | 274 | opt->lastopt = opt->dst1 = skb_network_header_len(skb); |
07a93626 | 275 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
a831f5bb MN |
276 | dstbuf = opt->dst1; |
277 | #endif | |
1da177e4 | 278 | |
e5bbef20 | 279 | if (ip6_parse_tlv(tlvprocdestopt_lst, skb)) { |
b0e380b1 | 280 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
dc435e6d | 281 | opt = IP6CB(skb); |
07a93626 | 282 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
a831f5bb MN |
283 | opt->nhoff = dstbuf; |
284 | #else | |
951dbc8a | 285 | opt->nhoff = opt->dst1; |
a831f5bb | 286 | #endif |
1da177e4 LT |
287 | return 1; |
288 | } | |
289 | ||
1d015503 ED |
290 | __IP6_INC_STATS(dev_net(dst->dev), |
291 | ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS); | |
1da177e4 LT |
292 | return -1; |
293 | } | |
294 | ||
1ababeba DL |
295 | static void seg6_update_csum(struct sk_buff *skb) |
296 | { | |
297 | struct ipv6_sr_hdr *hdr; | |
298 | struct in6_addr *addr; | |
299 | __be32 from, to; | |
300 | ||
301 | /* srh is at transport offset and seg_left is already decremented | |
302 | * but daddr is not yet updated with next segment | |
303 | */ | |
304 | ||
305 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); | |
306 | addr = hdr->segments + hdr->segments_left; | |
307 | ||
308 | hdr->segments_left++; | |
309 | from = *(__be32 *)hdr; | |
310 | ||
311 | hdr->segments_left--; | |
312 | to = *(__be32 *)hdr; | |
313 | ||
314 | /* update skb csum with diff resulting from seg_left decrement */ | |
315 | ||
316 | update_csum_diff4(skb, from, to); | |
317 | ||
318 | /* compute csum diff between current and next segment and update */ | |
319 | ||
320 | update_csum_diff16(skb, (__be32 *)(&ipv6_hdr(skb)->daddr), | |
321 | (__be32 *)addr); | |
322 | } | |
323 | ||
324 | static int ipv6_srh_rcv(struct sk_buff *skb) | |
325 | { | |
326 | struct inet6_skb_parm *opt = IP6CB(skb); | |
327 | struct net *net = dev_net(skb->dev); | |
328 | struct ipv6_sr_hdr *hdr; | |
329 | struct inet6_dev *idev; | |
330 | struct in6_addr *addr; | |
1ababeba DL |
331 | int accept_seg6; |
332 | ||
333 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); | |
334 | ||
335 | idev = __in6_dev_get(skb->dev); | |
336 | ||
337 | accept_seg6 = net->ipv6.devconf_all->seg6_enabled; | |
338 | if (accept_seg6 > idev->cnf.seg6_enabled) | |
339 | accept_seg6 = idev->cnf.seg6_enabled; | |
340 | ||
341 | if (!accept_seg6) { | |
342 | kfree_skb(skb); | |
343 | return -1; | |
344 | } | |
345 | ||
9baee834 DL |
346 | #ifdef CONFIG_IPV6_SEG6_HMAC |
347 | if (!seg6_hmac_validate_skb(skb)) { | |
348 | kfree_skb(skb); | |
349 | return -1; | |
350 | } | |
351 | #endif | |
352 | ||
1ababeba | 353 | looped_back: |
013e8167 | 354 | if (hdr->segments_left == 0) { |
1ababeba DL |
355 | if (hdr->nexthdr == NEXTHDR_IPV6) { |
356 | int offset = (hdr->hdrlen + 1) << 3; | |
357 | ||
358 | skb_postpull_rcsum(skb, skb_network_header(skb), | |
359 | skb_network_header_len(skb)); | |
360 | ||
361 | if (!pskb_pull(skb, offset)) { | |
362 | kfree_skb(skb); | |
363 | return -1; | |
364 | } | |
365 | skb_postpull_rcsum(skb, skb_transport_header(skb), | |
366 | offset); | |
367 | ||
368 | skb_reset_network_header(skb); | |
369 | skb_reset_transport_header(skb); | |
370 | skb->encapsulation = 0; | |
371 | ||
372 | __skb_tunnel_rx(skb, skb->dev, net); | |
373 | ||
374 | netif_rx(skb); | |
375 | return -1; | |
376 | } | |
377 | ||
378 | opt->srcrt = skb_network_header_len(skb); | |
379 | opt->lastopt = opt->srcrt; | |
380 | skb->transport_header += (hdr->hdrlen + 1) << 3; | |
381 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); | |
382 | ||
383 | return 1; | |
384 | } | |
385 | ||
386 | if (hdr->segments_left >= (hdr->hdrlen >> 1)) { | |
387 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), | |
388 | IPSTATS_MIB_INHDRERRORS); | |
389 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, | |
390 | ((&hdr->segments_left) - | |
391 | skb_network_header(skb))); | |
1ababeba DL |
392 | return -1; |
393 | } | |
394 | ||
395 | if (skb_cloned(skb)) { | |
396 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { | |
397 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), | |
398 | IPSTATS_MIB_OUTDISCARDS); | |
399 | kfree_skb(skb); | |
400 | return -1; | |
401 | } | |
402 | } | |
403 | ||
404 | hdr = (struct ipv6_sr_hdr *)skb_transport_header(skb); | |
405 | ||
406 | hdr->segments_left--; | |
407 | addr = hdr->segments + hdr->segments_left; | |
408 | ||
409 | skb_push(skb, sizeof(struct ipv6hdr)); | |
410 | ||
411 | if (skb->ip_summed == CHECKSUM_COMPLETE) | |
412 | seg6_update_csum(skb); | |
413 | ||
414 | ipv6_hdr(skb)->daddr = *addr; | |
415 | ||
1ababeba DL |
416 | skb_dst_drop(skb); |
417 | ||
418 | ip6_route_input(skb); | |
419 | ||
420 | if (skb_dst(skb)->error) { | |
421 | dst_input(skb); | |
422 | return -1; | |
423 | } | |
424 | ||
425 | if (skb_dst(skb)->dev->flags & IFF_LOOPBACK) { | |
426 | if (ipv6_hdr(skb)->hop_limit <= 1) { | |
427 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), | |
428 | IPSTATS_MIB_INHDRERRORS); | |
429 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, | |
430 | ICMPV6_EXC_HOPLIMIT, 0); | |
431 | kfree_skb(skb); | |
432 | return -1; | |
433 | } | |
434 | ipv6_hdr(skb)->hop_limit--; | |
435 | ||
013e8167 DL |
436 | skb_pull(skb, sizeof(struct ipv6hdr)); |
437 | goto looped_back; | |
1ababeba DL |
438 | } |
439 | ||
440 | dst_input(skb); | |
441 | ||
442 | return -1; | |
443 | } | |
444 | ||
1da177e4 LT |
445 | /******************************** |
446 | Routing header. | |
447 | ********************************/ | |
448 | ||
f6bc7d9e | 449 | /* called with rcu_read_lock() */ |
e5bbef20 | 450 | static int ipv6_rthdr_rcv(struct sk_buff *skb) |
1da177e4 | 451 | { |
1da177e4 | 452 | struct inet6_skb_parm *opt = IP6CB(skb); |
65d4ed92 | 453 | struct in6_addr *addr = NULL; |
1da177e4 | 454 | struct in6_addr daddr; |
0bcbc926 | 455 | struct inet6_dev *idev; |
1da177e4 | 456 | int n, i; |
1da177e4 LT |
457 | struct ipv6_rt_hdr *hdr; |
458 | struct rt0_hdr *rthdr; | |
483a47d2 DL |
459 | struct net *net = dev_net(skb->dev); |
460 | int accept_source_route = net->ipv6.devconf_all->accept_source_route; | |
0bcbc926 | 461 | |
f6bc7d9e ED |
462 | idev = __in6_dev_get(skb->dev); |
463 | if (idev && accept_source_route > idev->cnf.accept_source_route) | |
464 | accept_source_route = idev->cnf.accept_source_route; | |
0bcbc926 | 465 | |
ea2ae17d ACM |
466 | if (!pskb_may_pull(skb, skb_transport_offset(skb) + 8) || |
467 | !pskb_may_pull(skb, (skb_transport_offset(skb) + | |
9c70220b | 468 | ((skb_transport_header(skb)[1] + 1) << 3)))) { |
1d015503 ED |
469 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
470 | IPSTATS_MIB_INHDRERRORS); | |
1da177e4 LT |
471 | kfree_skb(skb); |
472 | return -1; | |
473 | } | |
474 | ||
9c70220b | 475 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
1da177e4 | 476 | |
0660e03f | 477 | if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr) || |
1da177e4 | 478 | skb->pkt_type != PACKET_HOST) { |
1d015503 ED |
479 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
480 | IPSTATS_MIB_INADDRERRORS); | |
1da177e4 LT |
481 | kfree_skb(skb); |
482 | return -1; | |
483 | } | |
484 | ||
1ababeba DL |
485 | /* segment routing */ |
486 | if (hdr->type == IPV6_SRCRT_TYPE_4) | |
487 | return ipv6_srh_rcv(skb); | |
488 | ||
1da177e4 LT |
489 | looped_back: |
490 | if (hdr->segments_left == 0) { | |
65d4ed92 | 491 | switch (hdr->type) { |
07a93626 | 492 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
65d4ed92 MN |
493 | case IPV6_SRCRT_TYPE_2: |
494 | /* Silently discard type 2 header unless it was | |
495 | * processed by own | |
496 | */ | |
497 | if (!addr) { | |
1d015503 ED |
498 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
499 | IPSTATS_MIB_INADDRERRORS); | |
65d4ed92 MN |
500 | kfree_skb(skb); |
501 | return -1; | |
502 | } | |
503 | break; | |
504 | #endif | |
505 | default: | |
506 | break; | |
507 | } | |
508 | ||
cfe1fc77 | 509 | opt->lastopt = opt->srcrt = skb_network_header_len(skb); |
b0e380b1 | 510 | skb->transport_header += (hdr->hdrlen + 1) << 3; |
1da177e4 LT |
511 | opt->dst0 = opt->dst1; |
512 | opt->dst1 = 0; | |
d56f90a7 | 513 | opt->nhoff = (&hdr->nexthdr) - skb_network_header(skb); |
1da177e4 LT |
514 | return 1; |
515 | } | |
516 | ||
65d4ed92 | 517 | switch (hdr->type) { |
07a93626 | 518 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
65d4ed92 | 519 | case IPV6_SRCRT_TYPE_2: |
c382bb9d YH |
520 | if (accept_source_route < 0) |
521 | goto unknown_rh; | |
65d4ed92 MN |
522 | /* Silently discard invalid RTH type 2 */ |
523 | if (hdr->hdrlen != 2 || hdr->segments_left != 1) { | |
1d015503 ED |
524 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
525 | IPSTATS_MIB_INHDRERRORS); | |
65d4ed92 MN |
526 | kfree_skb(skb); |
527 | return -1; | |
528 | } | |
529 | break; | |
530 | #endif | |
c382bb9d YH |
531 | default: |
532 | goto unknown_rh; | |
1da177e4 | 533 | } |
1da177e4 LT |
534 | |
535 | /* | |
536 | * This is the routing header forwarding algorithm from | |
537 | * RFC 2460, page 16. | |
538 | */ | |
539 | ||
540 | n = hdr->hdrlen >> 1; | |
541 | ||
542 | if (hdr->segments_left > n) { | |
1d015503 ED |
543 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
544 | IPSTATS_MIB_INHDRERRORS); | |
d56f90a7 ACM |
545 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
546 | ((&hdr->segments_left) - | |
547 | skb_network_header(skb))); | |
1da177e4 LT |
548 | return -1; |
549 | } | |
550 | ||
551 | /* We are about to mangle packet header. Be careful! | |
552 | Do not damage packets queued somewhere. | |
553 | */ | |
554 | if (skb_cloned(skb)) { | |
1da177e4 | 555 | /* the copy is a forwarded packet */ |
65c88466 | 556 | if (pskb_expand_head(skb, 0, 0, GFP_ATOMIC)) { |
1d015503 ED |
557 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
558 | IPSTATS_MIB_OUTDISCARDS); | |
a11d206d | 559 | kfree_skb(skb); |
1da177e4 LT |
560 | return -1; |
561 | } | |
65c88466 | 562 | hdr = (struct ipv6_rt_hdr *)skb_transport_header(skb); |
1da177e4 LT |
563 | } |
564 | ||
84fa7933 | 565 | if (skb->ip_summed == CHECKSUM_COMPLETE) |
1da177e4 LT |
566 | skb->ip_summed = CHECKSUM_NONE; |
567 | ||
568 | i = n - --hdr->segments_left; | |
569 | ||
570 | rthdr = (struct rt0_hdr *) hdr; | |
571 | addr = rthdr->addr; | |
572 | addr += i - 1; | |
573 | ||
65d4ed92 | 574 | switch (hdr->type) { |
07a93626 | 575 | #if IS_ENABLED(CONFIG_IPV6_MIP6) |
65d4ed92 MN |
576 | case IPV6_SRCRT_TYPE_2: |
577 | if (xfrm6_input_addr(skb, (xfrm_address_t *)addr, | |
0660e03f | 578 | (xfrm_address_t *)&ipv6_hdr(skb)->saddr, |
65d4ed92 | 579 | IPPROTO_ROUTING) < 0) { |
1d015503 ED |
580 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
581 | IPSTATS_MIB_INADDRERRORS); | |
65d4ed92 MN |
582 | kfree_skb(skb); |
583 | return -1; | |
584 | } | |
adf30907 | 585 | if (!ipv6_chk_home_addr(dev_net(skb_dst(skb)->dev), addr)) { |
1d015503 ED |
586 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
587 | IPSTATS_MIB_INADDRERRORS); | |
65d4ed92 MN |
588 | kfree_skb(skb); |
589 | return -1; | |
590 | } | |
591 | break; | |
592 | #endif | |
593 | default: | |
594 | break; | |
595 | } | |
596 | ||
1da177e4 | 597 | if (ipv6_addr_is_multicast(addr)) { |
1d015503 ED |
598 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
599 | IPSTATS_MIB_INADDRERRORS); | |
1da177e4 LT |
600 | kfree_skb(skb); |
601 | return -1; | |
602 | } | |
603 | ||
4e3fd7a0 AD |
604 | daddr = *addr; |
605 | *addr = ipv6_hdr(skb)->daddr; | |
606 | ipv6_hdr(skb)->daddr = daddr; | |
1da177e4 | 607 | |
adf30907 | 608 | skb_dst_drop(skb); |
1da177e4 | 609 | ip6_route_input(skb); |
adf30907 | 610 | if (skb_dst(skb)->error) { |
d56f90a7 | 611 | skb_push(skb, skb->data - skb_network_header(skb)); |
1da177e4 LT |
612 | dst_input(skb); |
613 | return -1; | |
614 | } | |
615 | ||
adf30907 | 616 | if (skb_dst(skb)->dev->flags&IFF_LOOPBACK) { |
0660e03f | 617 | if (ipv6_hdr(skb)->hop_limit <= 1) { |
1d015503 ED |
618 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), |
619 | IPSTATS_MIB_INHDRERRORS); | |
1da177e4 | 620 | icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, |
3ffe533c | 621 | 0); |
1da177e4 LT |
622 | kfree_skb(skb); |
623 | return -1; | |
624 | } | |
0660e03f | 625 | ipv6_hdr(skb)->hop_limit--; |
1da177e4 LT |
626 | goto looped_back; |
627 | } | |
628 | ||
d56f90a7 | 629 | skb_push(skb, skb->data - skb_network_header(skb)); |
1da177e4 LT |
630 | dst_input(skb); |
631 | return -1; | |
c382bb9d YH |
632 | |
633 | unknown_rh: | |
1d015503 | 634 | __IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_INHDRERRORS); |
c382bb9d YH |
635 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, |
636 | (&hdr->type) - skb_network_header(skb)); | |
637 | return -1; | |
1da177e4 LT |
638 | } |
639 | ||
41135cc8 | 640 | static const struct inet6_protocol rthdr_protocol = { |
1da177e4 | 641 | .handler = ipv6_rthdr_rcv, |
2207afc8 | 642 | .flags = INET6_PROTO_NOPOLICY, |
8ca896cf VY |
643 | }; |
644 | ||
41135cc8 | 645 | static const struct inet6_protocol destopt_protocol = { |
248b238d | 646 | .handler = ipv6_destopt_rcv, |
2207afc8 | 647 | .flags = INET6_PROTO_NOPOLICY, |
8ca896cf VY |
648 | }; |
649 | ||
41135cc8 | 650 | static const struct inet6_protocol nodata_protocol = { |
248b238d DL |
651 | .handler = dst_discard, |
652 | .flags = INET6_PROTO_NOPOLICY, | |
653 | }; | |
654 | ||
655 | int __init ipv6_exthdrs_init(void) | |
1da177e4 | 656 | { |
248b238d DL |
657 | int ret; |
658 | ||
3336288a VY |
659 | ret = inet6_add_protocol(&rthdr_protocol, IPPROTO_ROUTING); |
660 | if (ret) | |
c6b641a4 | 661 | goto out; |
3336288a | 662 | |
248b238d DL |
663 | ret = inet6_add_protocol(&destopt_protocol, IPPROTO_DSTOPTS); |
664 | if (ret) | |
665 | goto out_rthdr; | |
666 | ||
667 | ret = inet6_add_protocol(&nodata_protocol, IPPROTO_NONE); | |
668 | if (ret) | |
669 | goto out_destopt; | |
670 | ||
671 | out: | |
672 | return ret; | |
248b238d DL |
673 | out_destopt: |
674 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); | |
3336288a VY |
675 | out_rthdr: |
676 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); | |
248b238d | 677 | goto out; |
1da177e4 LT |
678 | }; |
679 | ||
248b238d DL |
680 | void ipv6_exthdrs_exit(void) |
681 | { | |
682 | inet6_del_protocol(&nodata_protocol, IPPROTO_NONE); | |
683 | inet6_del_protocol(&destopt_protocol, IPPROTO_DSTOPTS); | |
684 | inet6_del_protocol(&rthdr_protocol, IPPROTO_ROUTING); | |
685 | } | |
686 | ||
1da177e4 LT |
687 | /********************************** |
688 | Hop-by-hop options. | |
689 | **********************************/ | |
690 | ||
e76b2b25 | 691 | /* |
adf30907 | 692 | * Note: we cannot rely on skb_dst(skb) before we assign it in ip6_route_input(). |
e76b2b25 YH |
693 | */ |
694 | static inline struct inet6_dev *ipv6_skb_idev(struct sk_buff *skb) | |
695 | { | |
adf30907 | 696 | return skb_dst(skb) ? ip6_dst_idev(skb_dst(skb)) : __in6_dev_get(skb->dev); |
e76b2b25 YH |
697 | } |
698 | ||
2570a4f5 DM |
699 | static inline struct net *ipv6_skb_net(struct sk_buff *skb) |
700 | { | |
701 | return skb_dst(skb) ? dev_net(skb_dst(skb)->dev) : dev_net(skb->dev); | |
702 | } | |
703 | ||
1da177e4 LT |
704 | /* Router Alert as of RFC 2711 */ |
705 | ||
a50feda5 | 706 | static bool ipv6_hop_ra(struct sk_buff *skb, int optoff) |
1da177e4 | 707 | { |
d56f90a7 | 708 | const unsigned char *nh = skb_network_header(skb); |
a80ff03e | 709 | |
d56f90a7 | 710 | if (nh[optoff + 1] == 2) { |
dd3332bf YH |
711 | IP6CB(skb)->flags |= IP6SKB_ROUTERALERT; |
712 | memcpy(&IP6CB(skb)->ra, nh + optoff + 2, sizeof(IP6CB(skb)->ra)); | |
a50feda5 | 713 | return true; |
1da177e4 | 714 | } |
ba7a46f1 JP |
715 | net_dbg_ratelimited("ipv6_hop_ra: wrong RA length %d\n", |
716 | nh[optoff + 1]); | |
1da177e4 | 717 | kfree_skb(skb); |
a50feda5 | 718 | return false; |
1da177e4 LT |
719 | } |
720 | ||
721 | /* Jumbo payload */ | |
722 | ||
a50feda5 | 723 | static bool ipv6_hop_jumbo(struct sk_buff *skb, int optoff) |
1da177e4 | 724 | { |
d56f90a7 | 725 | const unsigned char *nh = skb_network_header(skb); |
2570a4f5 | 726 | struct net *net = ipv6_skb_net(skb); |
1da177e4 LT |
727 | u32 pkt_len; |
728 | ||
d56f90a7 | 729 | if (nh[optoff + 1] != 4 || (optoff & 3) != 2) { |
ba7a46f1 JP |
730 | net_dbg_ratelimited("ipv6_hop_jumbo: wrong jumbo opt length/alignment %d\n", |
731 | nh[optoff+1]); | |
1d015503 ED |
732 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
733 | IPSTATS_MIB_INHDRERRORS); | |
1da177e4 LT |
734 | goto drop; |
735 | } | |
736 | ||
d56f90a7 | 737 | pkt_len = ntohl(*(__be32 *)(nh + optoff + 2)); |
1da177e4 | 738 | if (pkt_len <= IPV6_MAXPLEN) { |
1d015503 ED |
739 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
740 | IPSTATS_MIB_INHDRERRORS); | |
1da177e4 | 741 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff+2); |
a50feda5 | 742 | return false; |
1da177e4 | 743 | } |
0660e03f | 744 | if (ipv6_hdr(skb)->payload_len) { |
1d015503 ED |
745 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
746 | IPSTATS_MIB_INHDRERRORS); | |
1da177e4 | 747 | icmpv6_param_prob(skb, ICMPV6_HDR_FIELD, optoff); |
a50feda5 | 748 | return false; |
1da177e4 LT |
749 | } |
750 | ||
751 | if (pkt_len > skb->len - sizeof(struct ipv6hdr)) { | |
1d015503 ED |
752 | __IP6_INC_STATS(net, ipv6_skb_idev(skb), |
753 | IPSTATS_MIB_INTRUNCATEDPKTS); | |
1da177e4 LT |
754 | goto drop; |
755 | } | |
42ca89c1 SH |
756 | |
757 | if (pskb_trim_rcsum(skb, pkt_len + sizeof(struct ipv6hdr))) | |
758 | goto drop; | |
759 | ||
cb891fa6 | 760 | IP6CB(skb)->flags |= IP6SKB_JUMBOGRAM; |
a50feda5 | 761 | return true; |
1da177e4 LT |
762 | |
763 | drop: | |
764 | kfree_skb(skb); | |
a50feda5 | 765 | return false; |
1da177e4 LT |
766 | } |
767 | ||
2e532b70 HD |
768 | /* CALIPSO RFC 5570 */ |
769 | ||
770 | static bool ipv6_hop_calipso(struct sk_buff *skb, int optoff) | |
771 | { | |
772 | const unsigned char *nh = skb_network_header(skb); | |
773 | ||
774 | if (nh[optoff + 1] < 8) | |
775 | goto drop; | |
776 | ||
777 | if (nh[optoff + 6] * 4 + 8 > nh[optoff + 1]) | |
778 | goto drop; | |
779 | ||
780 | if (!calipso_validate(skb, nh + optoff)) | |
781 | goto drop; | |
782 | ||
783 | return true; | |
784 | ||
785 | drop: | |
786 | kfree_skb(skb); | |
787 | return false; | |
788 | } | |
789 | ||
a50feda5 | 790 | static const struct tlvtype_proc tlvprochopopt_lst[] = { |
1da177e4 LT |
791 | { |
792 | .type = IPV6_TLV_ROUTERALERT, | |
793 | .func = ipv6_hop_ra, | |
794 | }, | |
795 | { | |
796 | .type = IPV6_TLV_JUMBO, | |
797 | .func = ipv6_hop_jumbo, | |
798 | }, | |
2e532b70 HD |
799 | { |
800 | .type = IPV6_TLV_CALIPSO, | |
801 | .func = ipv6_hop_calipso, | |
802 | }, | |
1da177e4 LT |
803 | { -1, } |
804 | }; | |
805 | ||
e5bbef20 | 806 | int ipv6_parse_hopopts(struct sk_buff *skb) |
1da177e4 | 807 | { |
951dbc8a PM |
808 | struct inet6_skb_parm *opt = IP6CB(skb); |
809 | ||
ec670095 | 810 | /* |
d56f90a7 | 811 | * skb_network_header(skb) is equal to skb->data, and |
cfe1fc77 | 812 | * skb_network_header_len(skb) is always equal to |
ec670095 YH |
813 | * sizeof(struct ipv6hdr) by definition of |
814 | * hop-by-hop options. | |
815 | */ | |
816 | if (!pskb_may_pull(skb, sizeof(struct ipv6hdr) + 8) || | |
9c70220b ACM |
817 | !pskb_may_pull(skb, (sizeof(struct ipv6hdr) + |
818 | ((skb_transport_header(skb)[1] + 1) << 3)))) { | |
ec670095 YH |
819 | kfree_skb(skb); |
820 | return -1; | |
821 | } | |
822 | ||
8b58a398 | 823 | opt->flags |= IP6SKB_HOPBYHOP; |
e5bbef20 | 824 | if (ip6_parse_tlv(tlvprochopopt_lst, skb)) { |
b0e380b1 | 825 | skb->transport_header += (skb_transport_header(skb)[1] + 1) << 3; |
dc435e6d | 826 | opt = IP6CB(skb); |
951dbc8a | 827 | opt->nhoff = sizeof(struct ipv6hdr); |
b809739a | 828 | return 1; |
951dbc8a | 829 | } |
1da177e4 LT |
830 | return -1; |
831 | } | |
832 | ||
833 | /* | |
834 | * Creating outbound headers. | |
835 | * | |
836 | * "build" functions work when skb is filled from head to tail (datagram) | |
837 | * "push" functions work when headers are added from tail to head (tcp) | |
838 | * | |
839 | * In both cases we assume, that caller reserved enough room | |
840 | * for headers. | |
841 | */ | |
842 | ||
a149e7c7 DL |
843 | static void ipv6_push_rthdr0(struct sk_buff *skb, u8 *proto, |
844 | struct ipv6_rt_hdr *opt, | |
845 | struct in6_addr **addr_p, struct in6_addr *saddr) | |
1da177e4 LT |
846 | { |
847 | struct rt0_hdr *phdr, *ihdr; | |
848 | int hops; | |
849 | ||
850 | ihdr = (struct rt0_hdr *) opt; | |
1ab1457c | 851 | |
d58ff351 | 852 | phdr = skb_push(skb, (ihdr->rt_hdr.hdrlen + 1) << 3); |
1da177e4 LT |
853 | memcpy(phdr, ihdr, sizeof(struct rt0_hdr)); |
854 | ||
855 | hops = ihdr->rt_hdr.hdrlen >> 1; | |
856 | ||
857 | if (hops > 1) | |
858 | memcpy(phdr->addr, ihdr->addr + 1, | |
859 | (hops - 1) * sizeof(struct in6_addr)); | |
860 | ||
4e3fd7a0 | 861 | phdr->addr[hops - 1] = **addr_p; |
1da177e4 LT |
862 | *addr_p = ihdr->addr; |
863 | ||
864 | phdr->rt_hdr.nexthdr = *proto; | |
865 | *proto = NEXTHDR_ROUTING; | |
866 | } | |
867 | ||
a149e7c7 DL |
868 | static void ipv6_push_rthdr4(struct sk_buff *skb, u8 *proto, |
869 | struct ipv6_rt_hdr *opt, | |
870 | struct in6_addr **addr_p, struct in6_addr *saddr) | |
871 | { | |
872 | struct ipv6_sr_hdr *sr_phdr, *sr_ihdr; | |
873 | int plen, hops; | |
874 | ||
875 | sr_ihdr = (struct ipv6_sr_hdr *)opt; | |
876 | plen = (sr_ihdr->hdrlen + 1) << 3; | |
877 | ||
d58ff351 | 878 | sr_phdr = skb_push(skb, plen); |
a149e7c7 DL |
879 | memcpy(sr_phdr, sr_ihdr, sizeof(struct ipv6_sr_hdr)); |
880 | ||
881 | hops = sr_ihdr->first_segment + 1; | |
882 | memcpy(sr_phdr->segments + 1, sr_ihdr->segments + 1, | |
883 | (hops - 1) * sizeof(struct in6_addr)); | |
884 | ||
885 | sr_phdr->segments[0] = **addr_p; | |
925615ce | 886 | *addr_p = &sr_ihdr->segments[sr_ihdr->segments_left]; |
a149e7c7 DL |
887 | |
888 | #ifdef CONFIG_IPV6_SEG6_HMAC | |
889 | if (sr_has_hmac(sr_phdr)) { | |
890 | struct net *net = NULL; | |
891 | ||
892 | if (skb->dev) | |
893 | net = dev_net(skb->dev); | |
894 | else if (skb->sk) | |
895 | net = sock_net(skb->sk); | |
896 | ||
897 | WARN_ON(!net); | |
898 | ||
899 | if (net) | |
900 | seg6_push_hmac(net, saddr, sr_phdr); | |
901 | } | |
902 | #endif | |
903 | ||
904 | sr_phdr->nexthdr = *proto; | |
905 | *proto = NEXTHDR_ROUTING; | |
906 | } | |
907 | ||
908 | static void ipv6_push_rthdr(struct sk_buff *skb, u8 *proto, | |
909 | struct ipv6_rt_hdr *opt, | |
910 | struct in6_addr **addr_p, struct in6_addr *saddr) | |
911 | { | |
912 | switch (opt->type) { | |
913 | case IPV6_SRCRT_TYPE_0: | |
ec9c4215 SD |
914 | case IPV6_SRCRT_STRICT: |
915 | case IPV6_SRCRT_TYPE_2: | |
a149e7c7 DL |
916 | ipv6_push_rthdr0(skb, proto, opt, addr_p, saddr); |
917 | break; | |
918 | case IPV6_SRCRT_TYPE_4: | |
919 | ipv6_push_rthdr4(skb, proto, opt, addr_p, saddr); | |
920 | break; | |
921 | default: | |
922 | break; | |
923 | } | |
924 | } | |
925 | ||
1da177e4 LT |
926 | static void ipv6_push_exthdr(struct sk_buff *skb, u8 *proto, u8 type, struct ipv6_opt_hdr *opt) |
927 | { | |
d58ff351 | 928 | struct ipv6_opt_hdr *h = skb_push(skb, ipv6_optlen(opt)); |
1da177e4 LT |
929 | |
930 | memcpy(h, opt, ipv6_optlen(opt)); | |
931 | h->nexthdr = *proto; | |
932 | *proto = type; | |
933 | } | |
934 | ||
935 | void ipv6_push_nfrag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, | |
936 | u8 *proto, | |
613fa3ca | 937 | struct in6_addr **daddr, struct in6_addr *saddr) |
1da177e4 | 938 | { |
333fad53 | 939 | if (opt->srcrt) { |
613fa3ca | 940 | ipv6_push_rthdr(skb, proto, opt->srcrt, daddr, saddr); |
333fad53 YH |
941 | /* |
942 | * IPV6_RTHDRDSTOPTS is ignored | |
943 | * unless IPV6_RTHDR is set (RFC3542). | |
944 | */ | |
945 | if (opt->dst0opt) | |
946 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst0opt); | |
947 | } | |
1da177e4 LT |
948 | if (opt->hopopt) |
949 | ipv6_push_exthdr(skb, proto, NEXTHDR_HOP, opt->hopopt); | |
950 | } | |
7159039a | 951 | |
1da177e4 LT |
952 | void ipv6_push_frag_opts(struct sk_buff *skb, struct ipv6_txoptions *opt, u8 *proto) |
953 | { | |
954 | if (opt->dst1opt) | |
955 | ipv6_push_exthdr(skb, proto, NEXTHDR_DEST, opt->dst1opt); | |
956 | } | |
5b8481fa | 957 | EXPORT_SYMBOL(ipv6_push_frag_opts); |
1da177e4 LT |
958 | |
959 | struct ipv6_txoptions * | |
960 | ipv6_dup_options(struct sock *sk, struct ipv6_txoptions *opt) | |
961 | { | |
962 | struct ipv6_txoptions *opt2; | |
963 | ||
964 | opt2 = sock_kmalloc(sk, opt->tot_len, GFP_ATOMIC); | |
965 | if (opt2) { | |
ac3c8172 | 966 | long dif = (char *)opt2 - (char *)opt; |
1da177e4 LT |
967 | memcpy(opt2, opt, opt->tot_len); |
968 | if (opt2->hopopt) | |
ac3c8172 | 969 | *((char **)&opt2->hopopt) += dif; |
1da177e4 | 970 | if (opt2->dst0opt) |
ac3c8172 | 971 | *((char **)&opt2->dst0opt) += dif; |
1da177e4 | 972 | if (opt2->dst1opt) |
ac3c8172 | 973 | *((char **)&opt2->dst1opt) += dif; |
1da177e4 | 974 | if (opt2->srcrt) |
ac3c8172 | 975 | *((char **)&opt2->srcrt) += dif; |
0aeea21a | 976 | refcount_set(&opt2->refcnt, 1); |
1da177e4 LT |
977 | } |
978 | return opt2; | |
979 | } | |
3cf3dc6c ACM |
980 | EXPORT_SYMBOL_GPL(ipv6_dup_options); |
981 | ||
333fad53 YH |
982 | static int ipv6_renew_option(void *ohdr, |
983 | struct ipv6_opt_hdr __user *newopt, int newoptlen, | |
984 | int inherit, | |
985 | struct ipv6_opt_hdr **hdr, | |
986 | char **p) | |
987 | { | |
988 | if (inherit) { | |
989 | if (ohdr) { | |
990 | memcpy(*p, ohdr, ipv6_optlen((struct ipv6_opt_hdr *)ohdr)); | |
991 | *hdr = (struct ipv6_opt_hdr *)*p; | |
e3192690 | 992 | *p += CMSG_ALIGN(ipv6_optlen(*hdr)); |
333fad53 YH |
993 | } |
994 | } else { | |
995 | if (newopt) { | |
996 | if (copy_from_user(*p, newopt, newoptlen)) | |
997 | return -EFAULT; | |
998 | *hdr = (struct ipv6_opt_hdr *)*p; | |
e3192690 | 999 | if (ipv6_optlen(*hdr) > newoptlen) |
333fad53 YH |
1000 | return -EINVAL; |
1001 | *p += CMSG_ALIGN(newoptlen); | |
1002 | } | |
1003 | } | |
1004 | return 0; | |
1005 | } | |
1006 | ||
e67ae213 HD |
1007 | /** |
1008 | * ipv6_renew_options - replace a specific ext hdr with a new one. | |
1009 | * | |
1010 | * @sk: sock from which to allocate memory | |
1011 | * @opt: original options | |
1012 | * @newtype: option type to replace in @opt | |
1013 | * @newopt: new option of type @newtype to replace (user-mem) | |
1014 | * @newoptlen: length of @newopt | |
1015 | * | |
1016 | * Returns a new set of options which is a copy of @opt with the | |
1017 | * option type @newtype replaced with @newopt. | |
1018 | * | |
1019 | * @opt may be NULL, in which case a new set of options is returned | |
1020 | * containing just @newopt. | |
1021 | * | |
1022 | * @newopt may be NULL, in which case the specified option type is | |
1023 | * not copied into the new set of options. | |
1024 | * | |
1025 | * The new set of options is allocated from the socket option memory | |
1026 | * buffer of @sk. | |
1027 | */ | |
333fad53 YH |
1028 | struct ipv6_txoptions * |
1029 | ipv6_renew_options(struct sock *sk, struct ipv6_txoptions *opt, | |
1030 | int newtype, | |
1031 | struct ipv6_opt_hdr __user *newopt, int newoptlen) | |
1032 | { | |
1033 | int tot_len = 0; | |
1034 | char *p; | |
1035 | struct ipv6_txoptions *opt2; | |
1036 | int err; | |
1037 | ||
99c7bc01 YH |
1038 | if (opt) { |
1039 | if (newtype != IPV6_HOPOPTS && opt->hopopt) | |
1040 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->hopopt)); | |
1041 | if (newtype != IPV6_RTHDRDSTOPTS && opt->dst0opt) | |
1042 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst0opt)); | |
1043 | if (newtype != IPV6_RTHDR && opt->srcrt) | |
1044 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->srcrt)); | |
1045 | if (newtype != IPV6_DSTOPTS && opt->dst1opt) | |
1046 | tot_len += CMSG_ALIGN(ipv6_optlen(opt->dst1opt)); | |
1047 | } | |
1048 | ||
333fad53 YH |
1049 | if (newopt && newoptlen) |
1050 | tot_len += CMSG_ALIGN(newoptlen); | |
1051 | ||
1052 | if (!tot_len) | |
1053 | return NULL; | |
1054 | ||
8b8aa4b5 | 1055 | tot_len += sizeof(*opt2); |
333fad53 YH |
1056 | opt2 = sock_kmalloc(sk, tot_len, GFP_ATOMIC); |
1057 | if (!opt2) | |
1058 | return ERR_PTR(-ENOBUFS); | |
1059 | ||
1060 | memset(opt2, 0, tot_len); | |
0aeea21a | 1061 | refcount_set(&opt2->refcnt, 1); |
333fad53 YH |
1062 | opt2->tot_len = tot_len; |
1063 | p = (char *)(opt2 + 1); | |
1064 | ||
99c7bc01 | 1065 | err = ipv6_renew_option(opt ? opt->hopopt : NULL, newopt, newoptlen, |
333fad53 YH |
1066 | newtype != IPV6_HOPOPTS, |
1067 | &opt2->hopopt, &p); | |
1068 | if (err) | |
1069 | goto out; | |
1070 | ||
99c7bc01 | 1071 | err = ipv6_renew_option(opt ? opt->dst0opt : NULL, newopt, newoptlen, |
333fad53 YH |
1072 | newtype != IPV6_RTHDRDSTOPTS, |
1073 | &opt2->dst0opt, &p); | |
1074 | if (err) | |
1075 | goto out; | |
1076 | ||
99c7bc01 | 1077 | err = ipv6_renew_option(opt ? opt->srcrt : NULL, newopt, newoptlen, |
333fad53 | 1078 | newtype != IPV6_RTHDR, |
99c7bc01 | 1079 | (struct ipv6_opt_hdr **)&opt2->srcrt, &p); |
333fad53 YH |
1080 | if (err) |
1081 | goto out; | |
1082 | ||
99c7bc01 | 1083 | err = ipv6_renew_option(opt ? opt->dst1opt : NULL, newopt, newoptlen, |
333fad53 YH |
1084 | newtype != IPV6_DSTOPTS, |
1085 | &opt2->dst1opt, &p); | |
1086 | if (err) | |
1087 | goto out; | |
1088 | ||
1089 | opt2->opt_nflen = (opt2->hopopt ? ipv6_optlen(opt2->hopopt) : 0) + | |
1090 | (opt2->dst0opt ? ipv6_optlen(opt2->dst0opt) : 0) + | |
1091 | (opt2->srcrt ? ipv6_optlen(opt2->srcrt) : 0); | |
1092 | opt2->opt_flen = (opt2->dst1opt ? ipv6_optlen(opt2->dst1opt) : 0); | |
1093 | ||
1094 | return opt2; | |
1095 | out: | |
8b8aa4b5 | 1096 | sock_kfree_s(sk, opt2, opt2->tot_len); |
333fad53 YH |
1097 | return ERR_PTR(err); |
1098 | } | |
1099 | ||
e67ae213 HD |
1100 | /** |
1101 | * ipv6_renew_options_kern - replace a specific ext hdr with a new one. | |
1102 | * | |
1103 | * @sk: sock from which to allocate memory | |
1104 | * @opt: original options | |
1105 | * @newtype: option type to replace in @opt | |
1106 | * @newopt: new option of type @newtype to replace (kernel-mem) | |
1107 | * @newoptlen: length of @newopt | |
1108 | * | |
1109 | * See ipv6_renew_options(). The difference is that @newopt is | |
1110 | * kernel memory, rather than user memory. | |
1111 | */ | |
1112 | struct ipv6_txoptions * | |
1113 | ipv6_renew_options_kern(struct sock *sk, struct ipv6_txoptions *opt, | |
1114 | int newtype, struct ipv6_opt_hdr *newopt, | |
1115 | int newoptlen) | |
1116 | { | |
1117 | struct ipv6_txoptions *ret_val; | |
1118 | const mm_segment_t old_fs = get_fs(); | |
1119 | ||
1120 | set_fs(KERNEL_DS); | |
1121 | ret_val = ipv6_renew_options(sk, opt, newtype, | |
1122 | (struct ipv6_opt_hdr __user *)newopt, | |
1123 | newoptlen); | |
1124 | set_fs(old_fs); | |
1125 | return ret_val; | |
1126 | } | |
1127 | ||
df9890c3 YH |
1128 | struct ipv6_txoptions *ipv6_fixup_options(struct ipv6_txoptions *opt_space, |
1129 | struct ipv6_txoptions *opt) | |
1130 | { | |
1131 | /* | |
1132 | * ignore the dest before srcrt unless srcrt is being included. | |
1133 | * --yoshfuji | |
1134 | */ | |
1135 | if (opt && opt->dst0opt && !opt->srcrt) { | |
1136 | if (opt_space != opt) { | |
1137 | memcpy(opt_space, opt, sizeof(*opt_space)); | |
1138 | opt = opt_space; | |
1139 | } | |
1140 | opt->opt_nflen -= ipv6_optlen(opt->dst0opt); | |
1141 | opt->dst0opt = NULL; | |
1142 | } | |
1143 | ||
1144 | return opt; | |
1145 | } | |
a495f836 | 1146 | EXPORT_SYMBOL_GPL(ipv6_fixup_options); |
df9890c3 | 1147 | |
20c59de2 AE |
1148 | /** |
1149 | * fl6_update_dst - update flowi destination address with info given | |
1150 | * by srcrt option, if any. | |
1151 | * | |
4c9483b2 | 1152 | * @fl6: flowi6 for which daddr is to be updated |
20c59de2 | 1153 | * @opt: struct ipv6_txoptions in which to look for srcrt opt |
4c9483b2 | 1154 | * @orig: copy of original daddr address if modified |
20c59de2 AE |
1155 | * |
1156 | * Returns NULL if no txoptions or no srcrt, otherwise returns orig | |
4c9483b2 | 1157 | * and initial value of fl6->daddr set in orig |
20c59de2 | 1158 | */ |
4c9483b2 | 1159 | struct in6_addr *fl6_update_dst(struct flowi6 *fl6, |
20c59de2 AE |
1160 | const struct ipv6_txoptions *opt, |
1161 | struct in6_addr *orig) | |
1162 | { | |
1163 | if (!opt || !opt->srcrt) | |
1164 | return NULL; | |
1165 | ||
4e3fd7a0 | 1166 | *orig = fl6->daddr; |
a149e7c7 DL |
1167 | |
1168 | switch (opt->srcrt->type) { | |
1169 | case IPV6_SRCRT_TYPE_0: | |
ec9c4215 SD |
1170 | case IPV6_SRCRT_STRICT: |
1171 | case IPV6_SRCRT_TYPE_2: | |
a149e7c7 DL |
1172 | fl6->daddr = *((struct rt0_hdr *)opt->srcrt)->addr; |
1173 | break; | |
1174 | case IPV6_SRCRT_TYPE_4: | |
1175 | { | |
1176 | struct ipv6_sr_hdr *srh = (struct ipv6_sr_hdr *)opt->srcrt; | |
1177 | ||
925615ce | 1178 | fl6->daddr = srh->segments[srh->segments_left]; |
a149e7c7 DL |
1179 | break; |
1180 | } | |
1181 | default: | |
1182 | return NULL; | |
1183 | } | |
1184 | ||
20c59de2 AE |
1185 | return orig; |
1186 | } | |
20c59de2 | 1187 | EXPORT_SYMBOL_GPL(fl6_update_dst); |