blob: 2cf0a5f2ad1d6adc541f3a5a585c0fe4fe910b19 [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;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
David S. Miller4c9483b2011-03-12 16:22:43 -0500164 if (!fl6.flowi6_oif && (addr_type&IPV6_ADDR_MULTICAST))
165 fl6.flowi6_oif = np->mcast_oif;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
David S. Miller4c9483b2011-03-12 16:22:43 -0500167 security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
Venkat Yekkiralabeb8d132006-08-04 23:12:42 -0700168
Eric Dumazeta432b342015-11-29 19:37:57 -0800169 rcu_read_lock();
170 opt = flowlabel ? flowlabel->opt : rcu_dereference(np->opt);
David S. Miller4c9483b2011-03-12 16:22:43 -0500171 final_p = fl6_update_dst(&fl6, opt, &final);
Eric Dumazeta432b342015-11-29 19:37:57 -0800172 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
David S. Miller4c9483b2011-03-12 16:22:43 -0500174 dst = ip6_dst_lookup_flow(sk, &fl6, final_p, true);
David S. Miller68d0c6d2011-03-01 13:19:07 -0800175 err = 0;
176 if (IS_ERR(dst)) {
177 err = PTR_ERR(dst);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 goto out;
David S. Miller14e50e52007-05-24 18:17:54 -0700179 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181 /* source address lookup done in ip6_dst_lookup */
182
183 if (ipv6_addr_any(&np->saddr))
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000184 np->saddr = fl6.saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
186 if (ipv6_addr_any(&np->rcv_saddr)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000187 np->rcv_saddr = fl6.saddr;
Eric Dumazetc720c7e2009-10-15 06:30:45 +0000188 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
Eric Dumazet719f8352010-09-08 05:08:44 +0000189 if (sk->sk_prot->rehash)
190 sk->sk_prot->rehash(sk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 }
192
193 ip6_dst_store(sk, dst,
David S. Miller4c9483b2011-03-12 16:22:43 -0500194 ipv6_addr_equal(&fl6.daddr, &np->daddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700195 &np->daddr : NULL,
196#ifdef CONFIG_IPV6_SUBTREES
David S. Miller4c9483b2011-03-12 16:22:43 -0500197 ipv6_addr_equal(&fl6.saddr, &np->saddr) ?
YOSHIFUJI Hideaki8e1ef0a2006-08-29 17:15:09 -0700198 &np->saddr :
199#endif
200 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 sk->sk_state = TCP_ESTABLISHED;
203out:
204 fl6_sock_release(flowlabel);
205 return err;
206}
207
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900208void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
Al Viroe69a4ad2006-11-14 20:56:00 -0800209 __be16 port, u32 info, u8 *payload)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
211 struct ipv6_pinfo *np = inet6_sk(sk);
Arnaldo Carvalho de Melocc70ab22007-03-13 14:03:22 -0300212 struct icmp6hdr *icmph = icmp6_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct sock_exterr_skb *serr;
214
215 if (!np->recverr)
216 return;
217
218 skb = skb_clone(skb, GFP_ATOMIC);
219 if (!skb)
220 return;
221
Brian Haleyd40a4de2010-05-03 15:44:27 +0000222 skb->protocol = htons(ETH_P_IPV6);
223
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 serr = SKB_EXT_ERR(skb);
225 serr->ee.ee_errno = err;
226 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900227 serr->ee.ee_type = icmph->icmp6_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 serr->ee.ee_code = icmph->icmp6_code;
229 serr->ee.ee_pad = 0;
230 serr->ee.ee_info = info;
231 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700232 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
233 skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 serr->port = port;
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 __skb_pull(skb, payload - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300237 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (sock_queue_err_skb(sk, skb))
240 kfree_skb(skb);
241}
242
David S. Miller4c9483b2011-03-12 16:22:43 -0500243void ipv6_local_error(struct sock *sk, int err, struct flowi6 *fl6, u32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 struct ipv6_pinfo *np = inet6_sk(sk);
246 struct sock_exterr_skb *serr;
247 struct ipv6hdr *iph;
248 struct sk_buff *skb;
249
250 if (!np->recverr)
251 return;
252
253 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
254 if (!skb)
255 return;
256
Brian Haleyd40a4de2010-05-03 15:44:27 +0000257 skb->protocol = htons(ETH_P_IPV6);
258
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300259 skb_put(skb, sizeof(struct ipv6hdr));
260 skb_reset_network_header(skb);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700261 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000262 iph->daddr = fl6->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 serr = SKB_EXT_ERR(skb);
265 serr->ee.ee_errno = err;
266 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900267 serr->ee.ee_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 serr->ee.ee_code = 0;
269 serr->ee.ee_pad = 0;
270 serr->ee.ee_info = info;
271 serr->ee.ee_data = 0;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700272 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
David S. Miller1958b852011-03-12 16:36:19 -0500273 serr->port = fl6->fl6_dport;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
Arnaldo Carvalho de Melo27a884d2007-04-19 20:29:13 -0700275 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
Arnaldo Carvalho de Melobd823932007-03-13 17:10:43 -0300276 skb_reset_transport_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277
278 if (sock_queue_err_skb(sk, skb))
279 kfree_skb(skb);
280}
281
David S. Miller4c9483b2011-03-12 16:22:43 -0500282void ipv6_local_rxpmtu(struct sock *sk, struct flowi6 *fl6, u32 mtu)
Brian Haley4b340ae2010-04-23 11:26:09 +0000283{
284 struct ipv6_pinfo *np = inet6_sk(sk);
285 struct ipv6hdr *iph;
286 struct sk_buff *skb;
287 struct ip6_mtuinfo *mtu_info;
288
289 if (!np->rxopt.bits.rxpmtu)
290 return;
291
292 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
293 if (!skb)
294 return;
295
296 skb_put(skb, sizeof(struct ipv6hdr));
297 skb_reset_network_header(skb);
298 iph = ipv6_hdr(skb);
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000299 iph->daddr = fl6->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000300
301 mtu_info = IP6CBMTU(skb);
Brian Haley4b340ae2010-04-23 11:26:09 +0000302
303 mtu_info->ip6m_mtu = mtu;
304 mtu_info->ip6m_addr.sin6_family = AF_INET6;
305 mtu_info->ip6m_addr.sin6_port = 0;
306 mtu_info->ip6m_addr.sin6_flowinfo = 0;
David S. Miller4c9483b2011-03-12 16:22:43 -0500307 mtu_info->ip6m_addr.sin6_scope_id = fl6->flowi6_oif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000308 mtu_info->ip6m_addr.sin6_addr = ipv6_hdr(skb)->daddr;
Brian Haley4b340ae2010-04-23 11:26:09 +0000309
310 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
311 skb_reset_transport_header(skb);
312
313 skb = xchg(&np->rxpmtu, skb);
314 kfree_skb(skb);
315}
316
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900317/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * Handle MSG_ERRQUEUE
319 */
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100320int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321{
322 struct ipv6_pinfo *np = inet6_sk(sk);
323 struct sock_exterr_skb *serr;
324 struct sk_buff *skb, *skb2;
325 struct sockaddr_in6 *sin;
326 struct {
327 struct sock_extended_err ee;
328 struct sockaddr_in6 offender;
329 } errhdr;
330 int err;
331 int copied;
332
333 err = -EAGAIN;
334 skb = skb_dequeue(&sk->sk_error_queue);
335 if (skb == NULL)
336 goto out;
337
338 copied = skb->len;
339 if (copied > len) {
340 msg->msg_flags |= MSG_TRUNC;
341 copied = len;
342 }
343 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
344 if (err)
345 goto out_free_skb;
346
347 sock_recv_timestamp(msg, sk, skb);
348
349 serr = SKB_EXT_ERR(skb);
350
351 sin = (struct sockaddr_in6 *)msg->msg_name;
352 if (sin) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700353 const unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 sin->sin6_family = AF_INET6;
355 sin->sin6_flowinfo = 0;
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900356 sin->sin6_port = serr->port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 sin->sin6_scope_id = 0;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000358 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000359 sin->sin6_addr =
360 *(struct in6_addr *)(nh + serr->addr_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (np->sndflow)
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700362 sin->sin6_flowinfo =
363 (*(__be32 *)(nh + serr->addr_offset - 24) &
364 IPV6_FLOWINFO_MASK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
366 sin->sin6_scope_id = IP6CB(skb)->iif;
367 } else {
Brian Haleyb301e822009-10-07 13:58:25 -0700368 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
369 &sin->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 }
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100371 *addr_len = sizeof(*sin);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
373
374 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
375 sin = &errhdr.offender;
376 sin->sin6_family = AF_UNSPEC;
377 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
378 sin->sin6_family = AF_INET6;
379 sin->sin6_flowinfo = 0;
Hannes Frederic Sowaee2c6c62013-11-23 07:22:33 +0100380 sin->sin6_port = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 sin->sin6_scope_id = 0;
Brian Haleyd40a4de2010-05-03 15:44:27 +0000382 if (skb->protocol == htons(ETH_P_IPV6)) {
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000383 sin->sin6_addr = ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 if (np->rxopt.all)
385 datagram_recv_ctl(sk, msg, skb);
386 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
387 sin->sin6_scope_id = IP6CB(skb)->iif;
388 } else {
389 struct inet_sock *inet = inet_sk(sk);
390
Brian Haleyb301e822009-10-07 13:58:25 -0700391 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
392 &sin->sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (inet->cmsg_flags)
394 ip_cmsg_recv(msg, skb);
395 }
396 }
397
398 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
399
400 /* Now we could try to dump offended packet options */
401
402 msg->msg_flags |= MSG_ERRQUEUE;
403 err = copied;
404
405 /* Reset and regenerate socket error */
Herbert Xue0f9f852005-06-18 22:56:18 -0700406 spin_lock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 sk->sk_err = 0;
408 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
409 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
Herbert Xue0f9f852005-06-18 22:56:18 -0700410 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 sk->sk_error_report(sk);
412 } else {
Herbert Xue0f9f852005-06-18 22:56:18 -0700413 spin_unlock_bh(&sk->sk_error_queue.lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900416out_free_skb:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 kfree_skb(skb);
418out:
419 return err;
420}
421
Brian Haley4b340ae2010-04-23 11:26:09 +0000422/*
423 * Handle IPV6_RECVPATHMTU
424 */
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100425int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len,
426 int *addr_len)
Brian Haley4b340ae2010-04-23 11:26:09 +0000427{
428 struct ipv6_pinfo *np = inet6_sk(sk);
429 struct sk_buff *skb;
430 struct sockaddr_in6 *sin;
431 struct ip6_mtuinfo mtu_info;
432 int err;
433 int copied;
434
435 err = -EAGAIN;
436 skb = xchg(&np->rxpmtu, NULL);
437 if (skb == NULL)
438 goto out;
439
440 copied = skb->len;
441 if (copied > len) {
442 msg->msg_flags |= MSG_TRUNC;
443 copied = len;
444 }
445 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
446 if (err)
447 goto out_free_skb;
448
449 sock_recv_timestamp(msg, sk, skb);
450
451 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
452
453 sin = (struct sockaddr_in6 *)msg->msg_name;
454 if (sin) {
455 sin->sin6_family = AF_INET6;
456 sin->sin6_flowinfo = 0;
457 sin->sin6_port = 0;
458 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000459 sin->sin6_addr = mtu_info.ip6m_addr.sin6_addr;
Hannes Frederic Sowaff4fe082013-11-23 00:46:12 +0100460 *addr_len = sizeof(*sin);
Brian Haley4b340ae2010-04-23 11:26:09 +0000461 }
462
463 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
464
465 err = copied;
466
467out_free_skb:
468 kfree_skb(skb);
469out:
470 return err;
471}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473
474int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
475{
476 struct ipv6_pinfo *np = inet6_sk(sk);
477 struct inet6_skb_parm *opt = IP6CB(skb);
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700478 unsigned char *nh = skb_network_header(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 if (np->rxopt.bits.rxinfo) {
481 struct in6_pktinfo src_info;
482
483 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000484 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
486 }
487
488 if (np->rxopt.bits.rxhlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700489 int hlim = ipv6_hdr(skb)->hop_limit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
491 }
492
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900493 if (np->rxopt.bits.rxtclass) {
Jiri Benc7a3198a2012-02-09 09:34:41 +0000494 int tclass = ipv6_tclass(ipv6_hdr(skb));
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900495 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
496 }
497
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700498 if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
499 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
501 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900502
503 /* HbH is allowed only once */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 if (np->rxopt.bits.hopopts && opt->hop) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700505 u8 *ptr = nh + opt->hop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
507 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900508
509 if (opt->lastopt &&
510 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
511 /*
512 * Silly enough, but we need to reparse in order to
513 * report extension headers (except for HbH)
514 * in order.
515 *
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900516 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900517 * (and WILL NOT be) defined because
518 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
519 */
520 unsigned int off = sizeof(struct ipv6hdr);
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700521 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900522
523 while (off <= opt->lastopt) {
524 unsigned len;
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700525 u8 *ptr = nh + off;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900526
527 switch(nexthdr) {
528 case IPPROTO_DSTOPTS:
529 nexthdr = ptr[0];
530 len = (ptr[1] + 1) << 3;
531 if (np->rxopt.bits.dstopts)
532 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
533 break;
534 case IPPROTO_ROUTING:
535 nexthdr = ptr[0];
536 len = (ptr[1] + 1) << 3;
537 if (np->rxopt.bits.srcrt)
538 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
539 break;
540 case IPPROTO_AH:
541 nexthdr = ptr[0];
Ville Nuorvalaa3059892005-11-20 12:21:59 +0900542 len = (ptr[1] + 2) << 2;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900543 break;
544 default:
545 nexthdr = ptr[0];
546 len = (ptr[1] + 1) << 3;
547 break;
548 }
549
550 off += len;
551 }
552 }
553
554 /* socket options in old style */
555 if (np->rxopt.bits.rxoinfo) {
556 struct in6_pktinfo src_info;
557
558 src_info.ipi6_ifindex = opt->iif;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000559 src_info.ipi6_addr = ipv6_hdr(skb)->daddr;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900560 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
561 }
562 if (np->rxopt.bits.rxohlim) {
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700563 int hlim = ipv6_hdr(skb)->hop_limit;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900564 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
565 }
566 if (np->rxopt.bits.ohopopts && opt->hop) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700567 u8 *ptr = nh + opt->hop;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900568 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
569 }
570 if (np->rxopt.bits.odstopts && opt->dst0) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700571 u8 *ptr = nh + opt->dst0;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900572 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900574 if (np->rxopt.bits.osrcrt && opt->srcrt) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700575 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900576 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 }
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900578 if (np->rxopt.bits.odstopts && opt->dst1) {
Arnaldo Carvalho de Melod56f90a2007-04-10 20:50:43 -0700579 u8 *ptr = nh + opt->dst1;
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900580 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 }
Balazs Scheidler6c468622010-10-21 16:08:28 +0200582 if (np->rxopt.bits.rxorigdstaddr) {
583 struct sockaddr_in6 sin6;
Eric Dumazet747465e2012-01-16 19:27:39 +0000584 __be16 *ports = (__be16 *) skb_transport_header(skb);
Balazs Scheidler6c468622010-10-21 16:08:28 +0200585
586 if (skb_transport_offset(skb) + 4 <= skb->len) {
587 /* All current transport protocols have the port numbers in the
588 * first four bytes of the transport header and this function is
589 * written with this assumption in mind.
590 */
591
592 sin6.sin6_family = AF_INET6;
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000593 sin6.sin6_addr = ipv6_hdr(skb)->daddr;
Balazs Scheidler6c468622010-10-21 16:08:28 +0200594 sin6.sin6_port = ports[1];
595 sin6.sin6_flowinfo = 0;
596 sin6.sin6_scope_id = 0;
597
598 put_cmsg(msg, SOL_IPV6, IPV6_ORIGDSTADDR, sizeof(sin6), &sin6);
599 }
600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 return 0;
602}
603
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000604int datagram_send_ctl(struct net *net, struct sock *sk,
David S. Miller4c9483b2011-03-12 16:22:43 -0500605 struct msghdr *msg, struct flowi6 *fl6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 struct ipv6_txoptions *opt,
Brian Haley13b52cd2010-04-23 11:26:08 +0000607 int *hlimit, int *tclass, int *dontfrag)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608{
609 struct in6_pktinfo *src_info;
610 struct cmsghdr *cmsg;
611 struct ipv6_rt_hdr *rthdr;
612 struct ipv6_opt_hdr *hdr;
613 int len;
614 int err = 0;
615
616 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
617 int addr_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618
619 if (!CMSG_OK(msg, cmsg)) {
620 err = -EINVAL;
621 goto exit_f;
622 }
623
624 if (cmsg->cmsg_level != SOL_IPV6)
625 continue;
626
627 switch (cmsg->cmsg_type) {
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900628 case IPV6_PKTINFO:
629 case IPV6_2292PKTINFO:
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900630 {
631 struct net_device *dev = NULL;
632
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900633 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 err = -EINVAL;
635 goto exit_f;
636 }
637
638 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (src_info->ipi6_ifindex) {
David S. Miller4c9483b2011-03-12 16:22:43 -0500641 if (fl6->flowi6_oif &&
642 src_info->ipi6_ifindex != fl6->flowi6_oif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 return -EINVAL;
David S. Miller4c9483b2011-03-12 16:22:43 -0500644 fl6->flowi6_oif = src_info->ipi6_ifindex;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900647 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
Eric Dumazet536b2e92009-11-02 12:21:06 +0100649 rcu_read_lock();
David S. Miller4c9483b2011-03-12 16:22:43 -0500650 if (fl6->flowi6_oif) {
651 dev = dev_get_by_index_rcu(net, fl6->flowi6_oif);
Eric Dumazet536b2e92009-11-02 12:21:06 +0100652 if (!dev) {
653 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900654 return -ENODEV;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100655 }
656 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
657 rcu_read_unlock();
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900658 return -EINVAL;
Eric Dumazet536b2e92009-11-02 12:21:06 +0100659 }
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900660
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900661 if (addr_type != IPV6_ADDR_ANY) {
662 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
Maciej Żenczykowski2563fa52011-11-07 14:57:22 +0000663 if (!(inet_sk(sk)->freebind || inet_sk(sk)->transparent) &&
Maciej Żenczykowskiec0506d2011-08-28 12:35:31 +0000664 !ipv6_chk_addr(net, &src_info->ipi6_addr,
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900665 strict ? dev : NULL, 0))
666 err = -EINVAL;
667 else
Alexey Dobriyan4e3fd7a2011-11-21 03:39:03 +0000668 fl6->saddr = src_info->ipi6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669 }
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900670
Eric Dumazet536b2e92009-11-02 12:21:06 +0100671 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900673 if (err)
674 goto exit_f;
675
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 break;
YOSHIFUJI Hideaki187e3832008-06-04 13:01:37 +0900677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 case IPV6_FLOWINFO:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900680 if (cmsg->cmsg_len < CMSG_LEN(4)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 err = -EINVAL;
682 goto exit_f;
683 }
684
David S. Miller4c9483b2011-03-12 16:22:43 -0500685 if (fl6->flowlabel&IPV6_FLOWINFO_MASK) {
686 if ((fl6->flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 err = -EINVAL;
688 goto exit_f;
689 }
690 }
David S. Miller4c9483b2011-03-12 16:22:43 -0500691 fl6->flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 break;
693
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900694 case IPV6_2292HOPOPTS:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 case IPV6_HOPOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900696 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 err = -EINVAL;
698 goto exit_f;
699 }
700
701 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
702 len = ((hdr->hdrlen + 1) << 3);
703 if (cmsg->cmsg_len < CMSG_LEN(len)) {
704 err = -EINVAL;
705 goto exit_f;
706 }
707 if (!capable(CAP_NET_RAW)) {
708 err = -EPERM;
709 goto exit_f;
710 }
711 opt->opt_nflen += len;
712 opt->hopopt = hdr;
713 break;
714
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900715 case IPV6_2292DSTOPTS:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900716 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 err = -EINVAL;
718 goto exit_f;
719 }
720
721 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
722 len = ((hdr->hdrlen + 1) << 3);
723 if (cmsg->cmsg_len < CMSG_LEN(len)) {
724 err = -EINVAL;
725 goto exit_f;
726 }
727 if (!capable(CAP_NET_RAW)) {
728 err = -EPERM;
729 goto exit_f;
730 }
731 if (opt->dst1opt) {
732 err = -EINVAL;
733 goto exit_f;
734 }
735 opt->opt_flen += len;
736 opt->dst1opt = hdr;
737 break;
738
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900739 case IPV6_DSTOPTS:
740 case IPV6_RTHDRDSTOPTS:
741 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
742 err = -EINVAL;
743 goto exit_f;
744 }
745
746 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
747 len = ((hdr->hdrlen + 1) << 3);
748 if (cmsg->cmsg_len < CMSG_LEN(len)) {
749 err = -EINVAL;
750 goto exit_f;
751 }
752 if (!capable(CAP_NET_RAW)) {
753 err = -EPERM;
754 goto exit_f;
755 }
756 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
757 opt->opt_flen += len;
758 opt->dst1opt = hdr;
759 } else {
760 opt->opt_nflen += len;
761 opt->dst0opt = hdr;
762 }
763 break;
764
765 case IPV6_2292RTHDR:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 case IPV6_RTHDR:
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900767 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 err = -EINVAL;
769 goto exit_f;
770 }
771
772 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
773
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700774 switch (rthdr->type) {
Masahide NAKAMURA59fbb3a2007-06-26 23:56:32 -0700775#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700776 case IPV6_SRCRT_TYPE_2:
Brian Haley6e093d92008-11-12 22:59:21 -0800777 if (rthdr->hdrlen != 2 ||
778 rthdr->segments_left != 1) {
779 err = -EINVAL;
780 goto exit_f;
781 }
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700782 break;
YOSHIFUJI Hideakibb4dbf92007-07-10 22:55:49 -0700783#endif
Masahide NAKAMURA280a9d32006-08-23 19:17:12 -0700784 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 err = -EINVAL;
786 goto exit_f;
787 }
788
789 len = ((rthdr->hdrlen + 1) << 3);
790
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900791 if (cmsg->cmsg_len < CMSG_LEN(len)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 err = -EINVAL;
793 goto exit_f;
794 }
795
796 /* segments left must also match */
797 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
798 err = -EINVAL;
799 goto exit_f;
800 }
801
802 opt->opt_nflen += len;
803 opt->srcrt = rthdr;
804
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900805 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
807
808 opt->opt_nflen += dsthdrlen;
809 opt->dst0opt = opt->dst1opt;
810 opt->dst1opt = NULL;
811 opt->opt_flen -= dsthdrlen;
812 }
813
814 break;
815
YOSHIFUJI Hideaki333fad52005-09-08 09:59:17 +0900816 case IPV6_2292HOPLIMIT:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 case IPV6_HOPLIMIT:
818 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
819 err = -EINVAL;
820 goto exit_f;
821 }
822
823 *hlimit = *(int *)CMSG_DATA(cmsg);
Shan Weie8766fc2008-06-10 15:50:55 +0800824 if (*hlimit < -1 || *hlimit > 0xff) {
825 err = -EINVAL;
826 goto exit_f;
827 }
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 break;
830
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900831 case IPV6_TCLASS:
832 {
833 int tc;
834
835 err = -EINVAL;
836 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
837 goto exit_f;
838 }
839
840 tc = *(int *)CMSG_DATA(cmsg);
Remi Denis-Courmontd0ee0112006-09-13 20:08:07 -0700841 if (tc < -1 || tc > 0xff)
YOSHIFUJI Hideaki41a1f8e2005-09-08 10:19:03 +0900842 goto exit_f;
843
844 err = 0;
845 *tclass = tc;
846
847 break;
848 }
Brian Haley13b52cd2010-04-23 11:26:08 +0000849
850 case IPV6_DONTFRAG:
851 {
852 int df;
853
854 err = -EINVAL;
855 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
856 goto exit_f;
857 }
858
859 df = *(int *)CMSG_DATA(cmsg);
860 if (df < 0 || df > 1)
861 goto exit_f;
862
863 err = 0;
864 *dontfrag = df;
865
866 break;
867 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868 default:
Patrick McHardy64ce2072005-08-09 20:50:53 -0700869 LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
YOSHIFUJI Hideaki1ab14572007-02-09 23:24:49 +0900870 cmsg->cmsg_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 err = -EINVAL;
Miao Xie4a367022008-07-29 23:57:58 -0700872 goto exit_f;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700873 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 }
875
876exit_f:
877 return err;
878}