blob: d7619e414c54f8f89b26faaf02d3b88d1a3398bd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * common UDP/RAW code
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09003 * Linux INET6 implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 * Authors:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +09006 * Pedro Roque <roque@di.fc.ul.pt>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 */
13
Randy Dunlap4fc268d2006-01-11 12:17:47 -080014#include <linux/capability.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/errno.h>
16#include <linux/types.h>
17#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include <linux/interrupt.h>
19#include <linux/socket.h>
20#include <linux/sockios.h>
21#include <linux/in6.h>
22#include <linux/ipv6.h>
23#include <linux/route.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include <net/ipv6.h>
27#include <net/ndisc.h>
28#include <net/addrconf.h>
29#include <net/transp_v6.h>
30#include <net/ip6_route.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070031#include <net/tcp_states.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <linux/errqueue.h>
34#include <asm/uaccess.h>
35
Max Matveevc15fea22011-08-05 03:56:30 -070036static inline int ipv6_mapped_addr_any(const struct in6_addr *a)
37{
38 return (ipv6_addr_v4mapped(a) && (a->s6_addr32[3] == 0));
39}
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
42{
43 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
44 struct inet_sock *inet = inet_sk(sk);
45 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaud Ebalard20c59de2010-06-01 21:35:01 +000046 struct in6_addr *daddr, *final_p, final;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047 struct dst_entry *dst;
David S. Miller4c9483b2011-03-12 16:22:43 -050048 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -070049 struct ip6_flowlabel *flowlabel = NULL;
Arnaud Ebalard20c59de2010-06-01 21:35:01 +000050 struct ipv6_txoptions *opt;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int addr_type;
52 int err;
53
54 if (usin->sin6_family == AF_INET) {
55 if (__ipv6_only_sock(sk))
56 return -EAFNOSUPPORT;
57 err = ip4_datagram_connect(sk, uaddr, addr_len);
58 goto ipv4_connected;
59 }
60
61 if (addr_len < SIN6_LEN_RFC2133)
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090062 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +090064 if (usin->sin6_family != AF_INET6)
65 return -EAFNOSUPPORT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
David S. Miller4c9483b2011-03-12 16:22:43 -050067 memset(&fl6, 0, sizeof(fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 if (np->sndflow) {
David S. Miller4c9483b2011-03-12 16:22:43 -050069 fl6.flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
70 if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
71 flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (flowlabel == NULL)
73 return -EINVAL;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +000074 usin->sin6_addr = flowlabel->dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 }
76 }
77
78 addr_type = ipv6_addr_type(&usin->sin6_addr);
79
80 if (addr_type == IPV6_ADDR_ANY) {
81 /*
82 * connect to self
83 */
84 usin->sin6_addr.s6_addr[15] = 0x01;
85 }
86
87 daddr = &usin->sin6_addr;
88
89 if (addr_type == IPV6_ADDR_MAPPED) {
90 struct sockaddr_in sin;
91
92 if (__ipv6_only_sock(sk)) {
93 err = -ENETUNREACH;
94 goto out;
95 }
96 sin.sin_family = AF_INET;
97 sin.sin_addr.s_addr = daddr->s6_addr32[3];
98 sin.sin_port = usin->sin6_port;
99
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900100 err = ip4_datagram_connect(sk,
101 (struct sockaddr*) &sin,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 sizeof(sin));
103
104ipv4_connected:
105 if (err)
106 goto out;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900107
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000108 ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Max Matveevc15fea22011-08-05 03:56:30 -0700110 if (ipv6_addr_any(&np->saddr) ||
111 ipv6_mapped_addr_any(&np->saddr))
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000112 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
Max Matveevc15fea22011-08-05 03:56:30 -0700114 if (ipv6_addr_any(&np->rcv_saddr) ||
115 ipv6_mapped_addr_any(&np->rcv_saddr)) {
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000116 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
117 &np->rcv_saddr);
Eric Dumazet719f8352010-09-08 05:08:44 +0000118 if (sk->sk_prot->rehash)
119 sk->sk_prot->rehash(sk);
120 }
Brian Haleyb301e822009-10-07 13:58:25 -0700121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 goto out;
123 }
124
125 if (addr_type&IPV6_ADDR_LINKLOCAL) {
126 if (addr_len >= sizeof(struct sockaddr_in6) &&
127 usin->sin6_scope_id) {
128 if (sk->sk_bound_dev_if &&
129 sk->sk_bound_dev_if != usin->sin6_scope_id) {
130 err = -EINVAL;
131 goto out;
132 }
133 sk->sk_bound_dev_if = usin->sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 }
135
Brian Haley1ac4f002008-01-08 23:52:21 -0800136 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
137 sk->sk_bound_dev_if = np->mcast_oif;
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* Connect to link-local address requires an interface */
140 if (!sk->sk_bound_dev_if) {
141 err = -EINVAL;
142 goto out;
143 }
144 }
145
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000146 np->daddr = *daddr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500147 np->flow_label = fl6.flowlabel;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000149 inet->inet_dport = usin->sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /*
152 * Check for a route to destination an obtain the
153 * destination cache for it.
154 */
155
David S. Miller4c9483b2011-03-12 16:22:43 -0500156 fl6.flowi6_proto = sk->sk_protocol;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000157 fl6.daddr = np->daddr;
158 fl6.saddr = np->saddr;
David S. Miller4c9483b2011-03-12 16:22:43 -0500159 fl6.flowi6_oif = sk->sk_bound_dev_if;
160 fl6.flowi6_mark = sk->sk_mark;
David S. Miller1958b852011-03-12 16:36:19 -0500161 fl6.fl6_dport = inet->inet_dport;
162 fl6.fl6_sport = inet->inet_sport;
Lorenzo Colitti00e03ac2016-11-04 02:23:43 +0900163 fl6.flowi6_uid = sk->sk_uid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
David S. Miller4c9483b2011-03-12 16:22:43 -0500165 if (!fl6.flowi6_oif && (addr_type&IPV6_ADDR_MULTICAST))
166 fl6.flowi6_oif = np->mcast_oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167
David S. Miller4c9483b2011-03-12 16:22:43 -0500168 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700169
Eric Dumazeta432b342015-11-29 19:37:57 -0800170 rcu_read_lock();
171 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
David S. Miller4c9483b2011-03-12 16:22:43 -0500172 final_p = fl6_update_dst(&fl6, opt, &final);
Eric Dumazeta432b342015-11-29 19:37:57 -0800173 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
David S. Miller4c9483b2011-03-12 16:22:43 -0500175 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800176 err = 0;
177 if (IS_ERR(dst)) {
178 err = PTR_ERR(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -0700180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
182 /* source address lookup done in ip6_dst_lookup */
183
184 if (ipv6_addr_any(&np->saddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000185 np->saddr = fl6.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (ipv6_addr_any(&np->rcv_saddr)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000188 np->rcv_saddr = fl6.saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000189 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
Eric Dumazet719f8352010-09-08 05:08:44 +0000190 if (sk->sk_prot->rehash)
191 sk->sk_prot->rehash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 }
193
194 ip6_dst_store(sk, dst,
David S. Miller4c9483b2011-03-12 16:22:43 -0500195 ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700196 &np->daddr : NULL,
197#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -0500198 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700199 &np->saddr :
200#endif
201 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202
203 sk->sk_state = TCP_ESTABLISHED;
204out:
205 fl6_sock_release(flowlabel);
206 return err;
207}
208
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900209void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4ad2006-11-14 20:56:00 -0800210 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300213 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 struct sock_exterr_skb *serr;
215
216 if (!np->recverr)
217 return;
218
219 skb = skb_clone(skb, GFP_ATOMIC);
220 if (!skb)
221 return;
222
Brian Haleyd40a4de2010-05-03 15:44:27 +0000223 skb->protocol = htons(ETH_P_IPV6);
224
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 serr = SKB_EXT_ERR(skb);
226 serr->ee.ee_errno = err;
227 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900228 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 serr->ee.ee_code = icmph->icmp6_code;
230 serr->ee.ee_pad = 0;
231 serr->ee.ee_info = info;
232 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700233 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
234 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 serr->port = port;
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300238 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239
240 if (sock_queue_err_skb(sk, skb))
241 kfree_skb(skb);
242}
243
David S. Miller4c9483b2011-03-12 16:22:43 -0500244void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245{
246 struct ipv6_pinfo *np = inet6_sk(sk);
247 struct sock_exterr_skb *serr;
248 struct ipv6hdr *iph;
249 struct sk_buff *skb;
250
251 if (!np->recverr)
252 return;
253
254 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
255 if (!skb)
256 return;
257
Brian Haleyd40a4de2010-05-03 15:44:27 +0000258 skb->protocol = htons(ETH_P_IPV6);
259
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300260 skb_put(skb, sizeof(struct ipv6hdr));
261 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700262 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000263 iph->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 serr = SKB_EXT_ERR(skb);
266 serr->ee.ee_errno = err;
267 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900268 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 serr->ee.ee_code = 0;
270 serr->ee.ee_pad = 0;
271 serr->ee.ee_info = info;
272 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700273 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500274 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700276 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300277 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (sock_queue_err_skb(sk, skb))
280 kfree_skb(skb);
281}
282
David S. Miller4c9483b2011-03-12 16:22:43 -0500283void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000284{
285 struct ipv6_pinfo *np = inet6_sk(sk);
286 struct ipv6hdr *iph;
287 struct sk_buff *skb;
288 struct ip6_mtuinfo *mtu_info;
289
290 if (!np->rxopt.bits.rxpmtu)
291 return;
292
293 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
294 if (!skb)
295 return;
296
297 skb_put(skb, sizeof(struct ipv6hdr));
298 skb_reset_network_header(skb);
299 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000300 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000301
302 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000303
304 mtu_info->ip6m_mtu = mtu;
305 mtu_info->ip6m_addr.sin6_family = AF_INET6;
306 mtu_info->ip6m_addr.sin6_port = 0;
307 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500308 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000309 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000310
311 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
312 skb_reset_transport_header(skb);
313
314 skb = xchg(&np->rxpmtu, skb);
315 kfree_skb(skb);
316}
317
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900318/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 * Handle MSG_ERRQUEUE
320 */
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100321int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct ipv6_pinfo *np = inet6_sk(sk);
324 struct sock_exterr_skb *serr;
325 struct sk_buff *skb, *skb2;
326 struct sockaddr_in6 *sin;
327 struct {
328 struct sock_extended_err ee;
329 struct sockaddr_in6 offender;
330 } errhdr;
331 int err;
332 int copied;
333
334 err = -EAGAIN;
335 skb = skb_dequeue(&sk->sk_error_queue);
336 if (skb == NULL)
337 goto out;
338
339 copied = skb->len;
340 if (copied > len) {
341 msg->msg_flags |= MSG_TRUNC;
342 copied = len;
343 }
344 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
345 if (err)
346 goto out_free_skb;
347
348 sock_recv_timestamp(msg, sk, skb);
349
350 serr = SKB_EXT_ERR(skb);
351
352 sin = (struct sockaddr_in6 *)msg->msg_name;
353 if (sin) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700354 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 sin->sin6_family = AF_INET6;
356 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900357 sin->sin6_port = serr->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 sin->sin6_scope_id = 0;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000359 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000360 sin->sin6_addr =
361 *(struct in6_addr *)(nh + serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362 if (np->sndflow)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700363 sin->sin6_flowinfo =
364 (*(__be32 *)(nh + serr->addr_offset - 24) &
365 IPV6_FLOWINFO_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
367 sin->sin6_scope_id = IP6CB(skb)->iif;
368 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700369 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
370 &sin->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 }
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100372 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 }
374
375 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
376 sin = &errhdr.offender;
377 sin->sin6_family = AF_UNSPEC;
378 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
379 sin->sin6_family = AF_INET6;
380 sin->sin6_flowinfo = 0;
Hannes Frederic Sowaee2c6c62013-11-23 07:22:33 +0100381 sin->sin6_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 sin->sin6_scope_id = 0;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000383 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000384 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 if (np->rxopt.all)
386 datagram_recv_ctl(sk, msg, skb);
387 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
388 sin->sin6_scope_id = IP6CB(skb)->iif;
389 } else {
390 struct inet_sock *inet = inet_sk(sk);
391
Brian Haleyb301e822009-10-07 13:58:25 -0700392 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
393 &sin->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 if (inet->cmsg_flags)
395 ip_cmsg_recv(msg, skb);
396 }
397 }
398
399 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
400
401 /* Now we could try to dump offended packet options */
402
403 msg->msg_flags |= MSG_ERRQUEUE;
404 err = copied;
405
406 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700407 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sk->sk_err = 0;
409 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
410 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700411 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 sk->sk_error_report(sk);
413 } else {
Herbert Xue0f9f852005-06-18 22:56:18 -0700414 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900417out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 kfree_skb(skb);
419out:
420 return err;
421}
422
Brian Haley4b340ae2010-04-23 11:26:09 +0000423/*
424 * Handle IPV6_RECVPATHMTU
425 */
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100426int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
427 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000428{
429 struct ipv6_pinfo *np = inet6_sk(sk);
430 struct sk_buff *skb;
431 struct sockaddr_in6 *sin;
432 struct ip6_mtuinfo mtu_info;
433 int err;
434 int copied;
435
436 err = -EAGAIN;
437 skb = xchg(&np->rxpmtu, NULL);
438 if (skb == NULL)
439 goto out;
440
441 copied = skb->len;
442 if (copied > len) {
443 msg->msg_flags |= MSG_TRUNC;
444 copied = len;
445 }
446 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
447 if (err)
448 goto out_free_skb;
449
450 sock_recv_timestamp(msg, sk, skb);
451
452 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
453
454 sin = (struct sockaddr_in6 *)msg->msg_name;
455 if (sin) {
456 sin->sin6_family = AF_INET6;
457 sin->sin6_flowinfo = 0;
458 sin->sin6_port = 0;
459 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000460 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100461 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000462 }
463
464 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
465
466 err = copied;
467
468out_free_skb:
469 kfree_skb(skb);
470out:
471 return err;
472}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473
474
475int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
476{
477 struct ipv6_pinfo *np = inet6_sk(sk);
478 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700479 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
481 if (np->rxopt.bits.rxinfo) {
482 struct in6_pktinfo src_info;
483
484 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000485 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
487 }
488
489 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700490 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
492 }
493
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900494 if (np->rxopt.bits.rxtclass) {
Jiri Benc7a3198a2012-02-09 09:34:41 +0000495 int tclass = ipv6_tclass(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900496 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
497 }
498
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700499 if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
500 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
502 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900503
504 /* HbH is allowed only once */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (np->rxopt.bits.hopopts && opt->hop) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700506 u8 *ptr = nh + opt->hop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
508 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900509
510 if (opt->lastopt &&
511 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
512 /*
513 * Silly enough, but we need to reparse in order to
514 * report extension headers (except for HbH)
515 * in order.
516 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900517 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900518 * (and WILL NOT be) defined because
519 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
520 */
521 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700522 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900523
524 while (off <= opt->lastopt) {
525 unsigned len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700526 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900527
528 switch(nexthdr) {
529 case IPPROTO_DSTOPTS:
530 nexthdr = ptr[0];
531 len = (ptr[1] + 1) << 3;
532 if (np->rxopt.bits.dstopts)
533 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
534 break;
535 case IPPROTO_ROUTING:
536 nexthdr = ptr[0];
537 len = (ptr[1] + 1) << 3;
538 if (np->rxopt.bits.srcrt)
539 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
540 break;
541 case IPPROTO_AH:
542 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900543 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900544 break;
545 default:
546 nexthdr = ptr[0];
547 len = (ptr[1] + 1) << 3;
548 break;
549 }
550
551 off += len;
552 }
553 }
554
555 /* socket options in old style */
556 if (np->rxopt.bits.rxoinfo) {
557 struct in6_pktinfo src_info;
558
559 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000560 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900561 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
562 }
563 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700564 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900565 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
566 }
567 if (np->rxopt.bits.ohopopts && opt->hop) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700568 u8 *ptr = nh + opt->hop;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900569 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
570 }
571 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700572 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900573 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900575 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700576 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900577 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900579 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700580 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900581 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200583 if (np->rxopt.bits.rxorigdstaddr) {
584 struct sockaddr_in6 sin6;
Eric Dumazet747465e2012-01-16 19:27:39 +0000585 __be16 *ports = (__be16 *) skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200586
587 if (skb_transport_offset(skb) + 4 <= skb->len) {
588 /* All current transport protocols have the port numbers in the
589 * first four bytes of the transport header and this function is
590 * written with this assumption in mind.
591 */
592
593 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000594 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200595 sin6.sin6_port = ports[1];
596 sin6.sin6_flowinfo = 0;
597 sin6.sin6_scope_id = 0;
598
599 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
600 }
601 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 return 0;
603}
604
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000605int datagram_send_ctl(struct net *net, struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500606 struct msghdr *msg, struct flowi6 *fl6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 struct ipv6_txoptions *opt,
Brian Haley13b52cd2010-04-23 11:26:08 +0000608 int *hlimit, int *tclass, int *dontfrag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609{
610 struct in6_pktinfo *src_info;
611 struct cmsghdr *cmsg;
612 struct ipv6_rt_hdr *rthdr;
613 struct ipv6_opt_hdr *hdr;
614 int len;
615 int err = 0;
616
617 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
618 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619
620 if (!CMSG_OK(msg, cmsg)) {
621 err = -EINVAL;
622 goto exit_f;
623 }
624
625 if (cmsg->cmsg_level != SOL_IPV6)
626 continue;
627
628 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900629 case IPV6_PKTINFO:
630 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900631 {
632 struct net_device *dev = NULL;
633
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900634 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 err = -EINVAL;
636 goto exit_f;
637 }
638
639 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500642 if (fl6->flowi6_oif &&
643 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500645 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 }
647
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900648 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
Eric Dumazet536b2e92009-11-02 12:21:06 +0100650 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500651 if (fl6->flowi6_oif) {
652 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100653 if (!dev) {
654 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900655 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100656 }
657 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
658 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900659 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100660 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900661
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900662 if (addr_type != IPV6_ADDR_ANY) {
663 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000664 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000665 !ipv6_chk_addr(net, &src_info->ipi6_addr,
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900666 strict ? dev : NULL, 0))
667 err = -EINVAL;
668 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000669 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900671
Eric Dumazet536b2e92009-11-02 12:21:06 +0100672 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900674 if (err)
675 goto exit_f;
676
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900678 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679
680 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900681 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 err = -EINVAL;
683 goto exit_f;
684 }
685
David S. Miller4c9483b2011-03-12 16:22:43 -0500686 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
687 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 err = -EINVAL;
689 goto exit_f;
690 }
691 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500692 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 break;
694
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900695 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900697 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 err = -EINVAL;
699 goto exit_f;
700 }
701
702 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
703 len = ((hdr->hdrlen + 1) << 3);
704 if (cmsg->cmsg_len < CMSG_LEN(len)) {
705 err = -EINVAL;
706 goto exit_f;
707 }
708 if (!capable(CAP_NET_RAW)) {
709 err = -EPERM;
710 goto exit_f;
711 }
712 opt->opt_nflen += len;
713 opt->hopopt = hdr;
714 break;
715
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900716 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900717 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 err = -EINVAL;
719 goto exit_f;
720 }
721
722 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
723 len = ((hdr->hdrlen + 1) << 3);
724 if (cmsg->cmsg_len < CMSG_LEN(len)) {
725 err = -EINVAL;
726 goto exit_f;
727 }
728 if (!capable(CAP_NET_RAW)) {
729 err = -EPERM;
730 goto exit_f;
731 }
732 if (opt->dst1opt) {
733 err = -EINVAL;
734 goto exit_f;
735 }
736 opt->opt_flen += len;
737 opt->dst1opt = hdr;
738 break;
739
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900740 case IPV6_DSTOPTS:
741 case IPV6_RTHDRDSTOPTS:
742 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
743 err = -EINVAL;
744 goto exit_f;
745 }
746
747 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
748 len = ((hdr->hdrlen + 1) << 3);
749 if (cmsg->cmsg_len < CMSG_LEN(len)) {
750 err = -EINVAL;
751 goto exit_f;
752 }
753 if (!capable(CAP_NET_RAW)) {
754 err = -EPERM;
755 goto exit_f;
756 }
757 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
758 opt->opt_flen += len;
759 opt->dst1opt = hdr;
760 } else {
761 opt->opt_nflen += len;
762 opt->dst0opt = hdr;
763 }
764 break;
765
766 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900768 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 err = -EINVAL;
770 goto exit_f;
771 }
772
773 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
774
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700775 switch (rthdr->type) {
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700776#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700777 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800778 if (rthdr->hdrlen != 2 ||
779 rthdr->segments_left != 1) {
780 err = -EINVAL;
781 goto exit_f;
782 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700783 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700784#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700785 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 err = -EINVAL;
787 goto exit_f;
788 }
789
790 len = ((rthdr->hdrlen + 1) << 3);
791
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900792 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 err = -EINVAL;
794 goto exit_f;
795 }
796
797 /* segments left must also match */
798 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
799 err = -EINVAL;
800 goto exit_f;
801 }
802
803 opt->opt_nflen += len;
804 opt->srcrt = rthdr;
805
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900806 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
808
809 opt->opt_nflen += dsthdrlen;
810 opt->dst0opt = opt->dst1opt;
811 opt->dst1opt = NULL;
812 opt->opt_flen -= dsthdrlen;
813 }
814
815 break;
816
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900817 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 case IPV6_HOPLIMIT:
819 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
820 err = -EINVAL;
821 goto exit_f;
822 }
823
824 *hlimit = *(int *)CMSG_DATA(cmsg);
Shan Weie8766fc2008-06-10 15:50:55 +0800825 if (*hlimit < -1 || *hlimit > 0xff) {
826 err = -EINVAL;
827 goto exit_f;
828 }
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830 break;
831
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900832 case IPV6_TCLASS:
833 {
834 int tc;
835
836 err = -EINVAL;
837 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
838 goto exit_f;
839 }
840
841 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700842 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900843 goto exit_f;
844
845 err = 0;
846 *tclass = tc;
847
848 break;
849 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000850
851 case IPV6_DONTFRAG:
852 {
853 int df;
854
855 err = -EINVAL;
856 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
857 goto exit_f;
858 }
859
860 df = *(int *)CMSG_DATA(cmsg);
861 if (df < 0 || df > 1)
862 goto exit_f;
863
864 err = 0;
865 *dontfrag = df;
866
867 break;
868 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 default:
Patrick McHardy64ce2072005-08-09 20:50:53 -0700870 LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900871 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -0700873 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700874 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
877exit_f:
878 return err;
879}