crypto: pcrypt - Rename pcrypt_instance
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / ipv6 / datagram.c
blob712684687c9a9d8f25baca4ff07a86f0386820bd
1 /*
2 * common UDP/RAW code
3 * Linux INET6 implementation
5 * Authors:
6 * Pedro Roque <roque@di.fc.ul.pt>
8 * 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.
14 #include <linux/capability.h>
15 #include <linux/errno.h>
16 #include <linux/types.h>
17 #include <linux/kernel.h>
18 #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>
24 #include <linux/slab.h>
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>
31 #include <net/tcp_states.h>
33 #include <linux/errqueue.h>
34 #include <asm/uaccess.h>
36 int ip6_datagram_connect(struct sock *sk, struct sockaddr *uaddr, int addr_len)
38 struct sockaddr_in6 *usin = (struct sockaddr_in6 *) uaddr;
39 struct inet_sock *inet = inet_sk(sk);
40 struct ipv6_pinfo *np = inet6_sk(sk);
41 struct in6_addr *daddr, *final_p = NULL, final;
42 struct dst_entry *dst;
43 struct flowi fl;
44 struct ip6_flowlabel *flowlabel = NULL;
45 int addr_type;
46 int err;
48 if (usin->sin6_family == AF_INET) {
49 if (__ipv6_only_sock(sk))
50 return -EAFNOSUPPORT;
51 err = ip4_datagram_connect(sk, uaddr, addr_len);
52 goto ipv4_connected;
55 if (addr_len < SIN6_LEN_RFC2133)
56 return -EINVAL;
58 if (usin->sin6_family != AF_INET6)
59 return -EAFNOSUPPORT;
61 memset(&fl, 0, sizeof(fl));
62 if (np->sndflow) {
63 fl.fl6_flowlabel = usin->sin6_flowinfo&IPV6_FLOWINFO_MASK;
64 if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
65 flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
66 if (flowlabel == NULL)
67 return -EINVAL;
68 ipv6_addr_copy(&usin->sin6_addr, &flowlabel->dst);
72 addr_type = ipv6_addr_type(&usin->sin6_addr);
74 if (addr_type == IPV6_ADDR_ANY) {
76 * connect to self
78 usin->sin6_addr.s6_addr[15] = 0x01;
81 daddr = &usin->sin6_addr;
83 if (addr_type == IPV6_ADDR_MAPPED) {
84 struct sockaddr_in sin;
86 if (__ipv6_only_sock(sk)) {
87 err = -ENETUNREACH;
88 goto out;
90 sin.sin_family = AF_INET;
91 sin.sin_addr.s_addr = daddr->s6_addr32[3];
92 sin.sin_port = usin->sin6_port;
94 err = ip4_datagram_connect(sk,
95 (struct sockaddr*) &sin,
96 sizeof(sin));
98 ipv4_connected:
99 if (err)
100 goto out;
102 ipv6_addr_set_v4mapped(inet->inet_daddr, &np->daddr);
104 if (ipv6_addr_any(&np->saddr))
105 ipv6_addr_set_v4mapped(inet->inet_saddr, &np->saddr);
107 if (ipv6_addr_any(&np->rcv_saddr))
108 ipv6_addr_set_v4mapped(inet->inet_rcv_saddr,
109 &np->rcv_saddr);
111 goto out;
114 if (addr_type&IPV6_ADDR_LINKLOCAL) {
115 if (addr_len >= sizeof(struct sockaddr_in6) &&
116 usin->sin6_scope_id) {
117 if (sk->sk_bound_dev_if &&
118 sk->sk_bound_dev_if != usin->sin6_scope_id) {
119 err = -EINVAL;
120 goto out;
122 sk->sk_bound_dev_if = usin->sin6_scope_id;
125 if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
126 sk->sk_bound_dev_if = np->mcast_oif;
128 /* Connect to link-local address requires an interface */
129 if (!sk->sk_bound_dev_if) {
130 err = -EINVAL;
131 goto out;
135 ipv6_addr_copy(&np->daddr, daddr);
136 np->flow_label = fl.fl6_flowlabel;
138 inet->inet_dport = usin->sin6_port;
141 * Check for a route to destination an obtain the
142 * destination cache for it.
145 fl.proto = sk->sk_protocol;
146 ipv6_addr_copy(&fl.fl6_dst, &np->daddr);
147 ipv6_addr_copy(&fl.fl6_src, &np->saddr);
148 fl.oif = sk->sk_bound_dev_if;
149 fl.mark = sk->sk_mark;
150 fl.fl_ip_dport = inet->inet_dport;
151 fl.fl_ip_sport = inet->inet_sport;
153 if (!fl.oif && (addr_type&IPV6_ADDR_MULTICAST))
154 fl.oif = np->mcast_oif;
156 security_sk_classify_flow(sk, &fl);
158 if (flowlabel) {
159 if (flowlabel->opt && flowlabel->opt->srcrt) {
160 struct rt0_hdr *rt0 = (struct rt0_hdr *) flowlabel->opt->srcrt;
161 ipv6_addr_copy(&final, &fl.fl6_dst);
162 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
163 final_p = &final;
165 } else if (np->opt && np->opt->srcrt) {
166 struct rt0_hdr *rt0 = (struct rt0_hdr *)np->opt->srcrt;
167 ipv6_addr_copy(&final, &fl.fl6_dst);
168 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
169 final_p = &final;
172 err = ip6_dst_lookup(sk, &dst, &fl);
173 if (err)
174 goto out;
175 if (final_p)
176 ipv6_addr_copy(&fl.fl6_dst, final_p);
178 err = __xfrm_lookup(sock_net(sk), &dst, &fl, sk, XFRM_LOOKUP_WAIT);
179 if (err < 0) {
180 if (err == -EREMOTE)
181 err = ip6_dst_blackhole(sk, &dst, &fl);
182 if (err < 0)
183 goto out;
186 /* source address lookup done in ip6_dst_lookup */
188 if (ipv6_addr_any(&np->saddr))
189 ipv6_addr_copy(&np->saddr, &fl.fl6_src);
191 if (ipv6_addr_any(&np->rcv_saddr)) {
192 ipv6_addr_copy(&np->rcv_saddr, &fl.fl6_src);
193 inet->inet_rcv_saddr = LOOPBACK4_IPV6;
196 ip6_dst_store(sk, dst,
197 ipv6_addr_equal(&fl.fl6_dst, &np->daddr) ?
198 &np->daddr : NULL,
199 #ifdef CONFIG_IPV6_SUBTREES
200 ipv6_addr_equal(&fl.fl6_src, &np->saddr) ?
201 &np->saddr :
202 #endif
203 NULL);
205 sk->sk_state = TCP_ESTABLISHED;
206 out:
207 fl6_sock_release(flowlabel);
208 return err;
211 void ipv6_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
212 __be16 port, u32 info, u8 *payload)
214 struct ipv6_pinfo *np = inet6_sk(sk);
215 struct icmp6hdr *icmph = icmp6_hdr(skb);
216 struct sock_exterr_skb *serr;
218 if (!np->recverr)
219 return;
221 skb = skb_clone(skb, GFP_ATOMIC);
222 if (!skb)
223 return;
225 skb->protocol = htons(ETH_P_IPV6);
227 serr = SKB_EXT_ERR(skb);
228 serr->ee.ee_errno = err;
229 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP6;
230 serr->ee.ee_type = icmph->icmp6_type;
231 serr->ee.ee_code = icmph->icmp6_code;
232 serr->ee.ee_pad = 0;
233 serr->ee.ee_info = info;
234 serr->ee.ee_data = 0;
235 serr->addr_offset = (u8 *)&(((struct ipv6hdr *)(icmph + 1))->daddr) -
236 skb_network_header(skb);
237 serr->port = port;
239 __skb_pull(skb, payload - skb->data);
240 skb_reset_transport_header(skb);
242 if (sock_queue_err_skb(sk, skb))
243 kfree_skb(skb);
246 void ipv6_local_error(struct sock *sk, int err, struct flowi *fl, u32 info)
248 struct ipv6_pinfo *np = inet6_sk(sk);
249 struct sock_exterr_skb *serr;
250 struct ipv6hdr *iph;
251 struct sk_buff *skb;
253 if (!np->recverr)
254 return;
256 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
257 if (!skb)
258 return;
260 skb->protocol = htons(ETH_P_IPV6);
262 skb_put(skb, sizeof(struct ipv6hdr));
263 skb_reset_network_header(skb);
264 iph = ipv6_hdr(skb);
265 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
267 serr = SKB_EXT_ERR(skb);
268 serr->ee.ee_errno = err;
269 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
270 serr->ee.ee_type = 0;
271 serr->ee.ee_code = 0;
272 serr->ee.ee_pad = 0;
273 serr->ee.ee_info = info;
274 serr->ee.ee_data = 0;
275 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
276 serr->port = fl->fl_ip_dport;
278 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
279 skb_reset_transport_header(skb);
281 if (sock_queue_err_skb(sk, skb))
282 kfree_skb(skb);
285 void ipv6_local_rxpmtu(struct sock *sk, struct flowi *fl, u32 mtu)
287 struct ipv6_pinfo *np = inet6_sk(sk);
288 struct ipv6hdr *iph;
289 struct sk_buff *skb;
290 struct ip6_mtuinfo *mtu_info;
292 if (!np->rxopt.bits.rxpmtu)
293 return;
295 skb = alloc_skb(sizeof(struct ipv6hdr), GFP_ATOMIC);
296 if (!skb)
297 return;
299 skb_put(skb, sizeof(struct ipv6hdr));
300 skb_reset_network_header(skb);
301 iph = ipv6_hdr(skb);
302 ipv6_addr_copy(&iph->daddr, &fl->fl6_dst);
304 mtu_info = IP6CBMTU(skb);
305 if (!mtu_info) {
306 kfree_skb(skb);
307 return;
310 mtu_info->ip6m_mtu = mtu;
311 mtu_info->ip6m_addr.sin6_family = AF_INET6;
312 mtu_info->ip6m_addr.sin6_port = 0;
313 mtu_info->ip6m_addr.sin6_flowinfo = 0;
314 mtu_info->ip6m_addr.sin6_scope_id = fl->oif;
315 ipv6_addr_copy(&mtu_info->ip6m_addr.sin6_addr, &ipv6_hdr(skb)->daddr);
317 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
318 skb_reset_transport_header(skb);
320 skb = xchg(&np->rxpmtu, skb);
321 kfree_skb(skb);
325 * Handle MSG_ERRQUEUE
327 int ipv6_recv_error(struct sock *sk, struct msghdr *msg, int len)
329 struct ipv6_pinfo *np = inet6_sk(sk);
330 struct sock_exterr_skb *serr;
331 struct sk_buff *skb, *skb2;
332 struct sockaddr_in6 *sin;
333 struct {
334 struct sock_extended_err ee;
335 struct sockaddr_in6 offender;
336 } errhdr;
337 int err;
338 int copied;
340 err = -EAGAIN;
341 skb = skb_dequeue(&sk->sk_error_queue);
342 if (skb == NULL)
343 goto out;
345 copied = skb->len;
346 if (copied > len) {
347 msg->msg_flags |= MSG_TRUNC;
348 copied = len;
350 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
351 if (err)
352 goto out_free_skb;
354 sock_recv_timestamp(msg, sk, skb);
356 serr = SKB_EXT_ERR(skb);
358 sin = (struct sockaddr_in6 *)msg->msg_name;
359 if (sin) {
360 const unsigned char *nh = skb_network_header(skb);
361 sin->sin6_family = AF_INET6;
362 sin->sin6_flowinfo = 0;
363 sin->sin6_port = serr->port;
364 sin->sin6_scope_id = 0;
365 if (skb->protocol == htons(ETH_P_IPV6)) {
366 ipv6_addr_copy(&sin->sin6_addr,
367 (struct in6_addr *)(nh + serr->addr_offset));
368 if (np->sndflow)
369 sin->sin6_flowinfo =
370 (*(__be32 *)(nh + serr->addr_offset - 24) &
371 IPV6_FLOWINFO_MASK);
372 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
373 sin->sin6_scope_id = IP6CB(skb)->iif;
374 } else {
375 ipv6_addr_set_v4mapped(*(__be32 *)(nh + serr->addr_offset),
376 &sin->sin6_addr);
380 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
381 sin = &errhdr.offender;
382 sin->sin6_family = AF_UNSPEC;
383 if (serr->ee.ee_origin != SO_EE_ORIGIN_LOCAL) {
384 sin->sin6_family = AF_INET6;
385 sin->sin6_flowinfo = 0;
386 sin->sin6_scope_id = 0;
387 if (skb->protocol == htons(ETH_P_IPV6)) {
388 ipv6_addr_copy(&sin->sin6_addr, &ipv6_hdr(skb)->saddr);
389 if (np->rxopt.all)
390 datagram_recv_ctl(sk, msg, skb);
391 if (ipv6_addr_type(&sin->sin6_addr) & IPV6_ADDR_LINKLOCAL)
392 sin->sin6_scope_id = IP6CB(skb)->iif;
393 } else {
394 struct inet_sock *inet = inet_sk(sk);
396 ipv6_addr_set_v4mapped(ip_hdr(skb)->saddr,
397 &sin->sin6_addr);
398 if (inet->cmsg_flags)
399 ip_cmsg_recv(msg, skb);
403 put_cmsg(msg, SOL_IPV6, IPV6_RECVERR, sizeof(errhdr), &errhdr);
405 /* Now we could try to dump offended packet options */
407 msg->msg_flags |= MSG_ERRQUEUE;
408 err = copied;
410 /* Reset and regenerate socket error */
411 spin_lock_bh(&sk->sk_error_queue.lock);
412 sk->sk_err = 0;
413 if ((skb2 = skb_peek(&sk->sk_error_queue)) != NULL) {
414 sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
415 spin_unlock_bh(&sk->sk_error_queue.lock);
416 sk->sk_error_report(sk);
417 } else {
418 spin_unlock_bh(&sk->sk_error_queue.lock);
421 out_free_skb:
422 kfree_skb(skb);
423 out:
424 return err;
428 * Handle IPV6_RECVPATHMTU
430 int ipv6_recv_rxpmtu(struct sock *sk, struct msghdr *msg, int len)
432 struct ipv6_pinfo *np = inet6_sk(sk);
433 struct sk_buff *skb;
434 struct sockaddr_in6 *sin;
435 struct ip6_mtuinfo mtu_info;
436 int err;
437 int copied;
439 err = -EAGAIN;
440 skb = xchg(&np->rxpmtu, NULL);
441 if (skb == NULL)
442 goto out;
444 copied = skb->len;
445 if (copied > len) {
446 msg->msg_flags |= MSG_TRUNC;
447 copied = len;
449 err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
450 if (err)
451 goto out_free_skb;
453 sock_recv_timestamp(msg, sk, skb);
455 memcpy(&mtu_info, IP6CBMTU(skb), sizeof(mtu_info));
457 sin = (struct sockaddr_in6 *)msg->msg_name;
458 if (sin) {
459 sin->sin6_family = AF_INET6;
460 sin->sin6_flowinfo = 0;
461 sin->sin6_port = 0;
462 sin->sin6_scope_id = mtu_info.ip6m_addr.sin6_scope_id;
463 ipv6_addr_copy(&sin->sin6_addr, &mtu_info.ip6m_addr.sin6_addr);
466 put_cmsg(msg, SOL_IPV6, IPV6_PATHMTU, sizeof(mtu_info), &mtu_info);
468 err = copied;
470 out_free_skb:
471 kfree_skb(skb);
472 out:
473 return err;
477 int datagram_recv_ctl(struct sock *sk, struct msghdr *msg, struct sk_buff *skb)
479 struct ipv6_pinfo *np = inet6_sk(sk);
480 struct inet6_skb_parm *opt = IP6CB(skb);
481 unsigned char *nh = skb_network_header(skb);
483 if (np->rxopt.bits.rxinfo) {
484 struct in6_pktinfo src_info;
486 src_info.ipi6_ifindex = opt->iif;
487 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
488 put_cmsg(msg, SOL_IPV6, IPV6_PKTINFO, sizeof(src_info), &src_info);
491 if (np->rxopt.bits.rxhlim) {
492 int hlim = ipv6_hdr(skb)->hop_limit;
493 put_cmsg(msg, SOL_IPV6, IPV6_HOPLIMIT, sizeof(hlim), &hlim);
496 if (np->rxopt.bits.rxtclass) {
497 int tclass = (ntohl(*(__be32 *)ipv6_hdr(skb)) >> 20) & 0xff;
498 put_cmsg(msg, SOL_IPV6, IPV6_TCLASS, sizeof(tclass), &tclass);
501 if (np->rxopt.bits.rxflow && (*(__be32 *)nh & IPV6_FLOWINFO_MASK)) {
502 __be32 flowinfo = *(__be32 *)nh & IPV6_FLOWINFO_MASK;
503 put_cmsg(msg, SOL_IPV6, IPV6_FLOWINFO, sizeof(flowinfo), &flowinfo);
506 /* HbH is allowed only once */
507 if (np->rxopt.bits.hopopts && opt->hop) {
508 u8 *ptr = nh + opt->hop;
509 put_cmsg(msg, SOL_IPV6, IPV6_HOPOPTS, (ptr[1]+1)<<3, ptr);
512 if (opt->lastopt &&
513 (np->rxopt.bits.dstopts || np->rxopt.bits.srcrt)) {
515 * Silly enough, but we need to reparse in order to
516 * report extension headers (except for HbH)
517 * in order.
519 * Also note that IPV6_RECVRTHDRDSTOPTS is NOT
520 * (and WILL NOT be) defined because
521 * IPV6_RECVDSTOPTS is more generic. --yoshfuji
523 unsigned int off = sizeof(struct ipv6hdr);
524 u8 nexthdr = ipv6_hdr(skb)->nexthdr;
526 while (off <= opt->lastopt) {
527 unsigned len;
528 u8 *ptr = nh + off;
530 switch(nexthdr) {
531 case IPPROTO_DSTOPTS:
532 nexthdr = ptr[0];
533 len = (ptr[1] + 1) << 3;
534 if (np->rxopt.bits.dstopts)
535 put_cmsg(msg, SOL_IPV6, IPV6_DSTOPTS, len, ptr);
536 break;
537 case IPPROTO_ROUTING:
538 nexthdr = ptr[0];
539 len = (ptr[1] + 1) << 3;
540 if (np->rxopt.bits.srcrt)
541 put_cmsg(msg, SOL_IPV6, IPV6_RTHDR, len, ptr);
542 break;
543 case IPPROTO_AH:
544 nexthdr = ptr[0];
545 len = (ptr[1] + 2) << 2;
546 break;
547 default:
548 nexthdr = ptr[0];
549 len = (ptr[1] + 1) << 3;
550 break;
553 off += len;
557 /* socket options in old style */
558 if (np->rxopt.bits.rxoinfo) {
559 struct in6_pktinfo src_info;
561 src_info.ipi6_ifindex = opt->iif;
562 ipv6_addr_copy(&src_info.ipi6_addr, &ipv6_hdr(skb)->daddr);
563 put_cmsg(msg, SOL_IPV6, IPV6_2292PKTINFO, sizeof(src_info), &src_info);
565 if (np->rxopt.bits.rxohlim) {
566 int hlim = ipv6_hdr(skb)->hop_limit;
567 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPLIMIT, sizeof(hlim), &hlim);
569 if (np->rxopt.bits.ohopopts && opt->hop) {
570 u8 *ptr = nh + opt->hop;
571 put_cmsg(msg, SOL_IPV6, IPV6_2292HOPOPTS, (ptr[1]+1)<<3, ptr);
573 if (np->rxopt.bits.odstopts && opt->dst0) {
574 u8 *ptr = nh + opt->dst0;
575 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
577 if (np->rxopt.bits.osrcrt && opt->srcrt) {
578 struct ipv6_rt_hdr *rthdr = (struct ipv6_rt_hdr *)(nh + opt->srcrt);
579 put_cmsg(msg, SOL_IPV6, IPV6_2292RTHDR, (rthdr->hdrlen+1) << 3, rthdr);
581 if (np->rxopt.bits.odstopts && opt->dst1) {
582 u8 *ptr = nh + opt->dst1;
583 put_cmsg(msg, SOL_IPV6, IPV6_2292DSTOPTS, (ptr[1]+1)<<3, ptr);
585 return 0;
588 int datagram_send_ctl(struct net *net,
589 struct msghdr *msg, struct flowi *fl,
590 struct ipv6_txoptions *opt,
591 int *hlimit, int *tclass, int *dontfrag)
593 struct in6_pktinfo *src_info;
594 struct cmsghdr *cmsg;
595 struct ipv6_rt_hdr *rthdr;
596 struct ipv6_opt_hdr *hdr;
597 int len;
598 int err = 0;
600 for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
601 int addr_type;
603 if (!CMSG_OK(msg, cmsg)) {
604 err = -EINVAL;
605 goto exit_f;
608 if (cmsg->cmsg_level != SOL_IPV6)
609 continue;
611 switch (cmsg->cmsg_type) {
612 case IPV6_PKTINFO:
613 case IPV6_2292PKTINFO:
615 struct net_device *dev = NULL;
617 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct in6_pktinfo))) {
618 err = -EINVAL;
619 goto exit_f;
622 src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
624 if (src_info->ipi6_ifindex) {
625 if (fl->oif && src_info->ipi6_ifindex != fl->oif)
626 return -EINVAL;
627 fl->oif = src_info->ipi6_ifindex;
630 addr_type = __ipv6_addr_type(&src_info->ipi6_addr);
632 rcu_read_lock();
633 if (fl->oif) {
634 dev = dev_get_by_index_rcu(net, fl->oif);
635 if (!dev) {
636 rcu_read_unlock();
637 return -ENODEV;
639 } else if (addr_type & IPV6_ADDR_LINKLOCAL) {
640 rcu_read_unlock();
641 return -EINVAL;
644 if (addr_type != IPV6_ADDR_ANY) {
645 int strict = __ipv6_addr_src_scope(addr_type) <= IPV6_ADDR_SCOPE_LINKLOCAL;
646 if (!ipv6_chk_addr(net, &src_info->ipi6_addr,
647 strict ? dev : NULL, 0))
648 err = -EINVAL;
649 else
650 ipv6_addr_copy(&fl->fl6_src, &src_info->ipi6_addr);
653 rcu_read_unlock();
655 if (err)
656 goto exit_f;
658 break;
661 case IPV6_FLOWINFO:
662 if (cmsg->cmsg_len < CMSG_LEN(4)) {
663 err = -EINVAL;
664 goto exit_f;
667 if (fl->fl6_flowlabel&IPV6_FLOWINFO_MASK) {
668 if ((fl->fl6_flowlabel^*(__be32 *)CMSG_DATA(cmsg))&~IPV6_FLOWINFO_MASK) {
669 err = -EINVAL;
670 goto exit_f;
673 fl->fl6_flowlabel = IPV6_FLOWINFO_MASK & *(__be32 *)CMSG_DATA(cmsg);
674 break;
676 case IPV6_2292HOPOPTS:
677 case IPV6_HOPOPTS:
678 if (opt->hopopt || cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
679 err = -EINVAL;
680 goto exit_f;
683 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
684 len = ((hdr->hdrlen + 1) << 3);
685 if (cmsg->cmsg_len < CMSG_LEN(len)) {
686 err = -EINVAL;
687 goto exit_f;
689 if (!capable(CAP_NET_RAW)) {
690 err = -EPERM;
691 goto exit_f;
693 opt->opt_nflen += len;
694 opt->hopopt = hdr;
695 break;
697 case IPV6_2292DSTOPTS:
698 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
699 err = -EINVAL;
700 goto exit_f;
703 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
704 len = ((hdr->hdrlen + 1) << 3);
705 if (cmsg->cmsg_len < CMSG_LEN(len)) {
706 err = -EINVAL;
707 goto exit_f;
709 if (!capable(CAP_NET_RAW)) {
710 err = -EPERM;
711 goto exit_f;
713 if (opt->dst1opt) {
714 err = -EINVAL;
715 goto exit_f;
717 opt->opt_flen += len;
718 opt->dst1opt = hdr;
719 break;
721 case IPV6_DSTOPTS:
722 case IPV6_RTHDRDSTOPTS:
723 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_opt_hdr))) {
724 err = -EINVAL;
725 goto exit_f;
728 hdr = (struct ipv6_opt_hdr *)CMSG_DATA(cmsg);
729 len = ((hdr->hdrlen + 1) << 3);
730 if (cmsg->cmsg_len < CMSG_LEN(len)) {
731 err = -EINVAL;
732 goto exit_f;
734 if (!capable(CAP_NET_RAW)) {
735 err = -EPERM;
736 goto exit_f;
738 if (cmsg->cmsg_type == IPV6_DSTOPTS) {
739 opt->opt_flen += len;
740 opt->dst1opt = hdr;
741 } else {
742 opt->opt_nflen += len;
743 opt->dst0opt = hdr;
745 break;
747 case IPV6_2292RTHDR:
748 case IPV6_RTHDR:
749 if (cmsg->cmsg_len < CMSG_LEN(sizeof(struct ipv6_rt_hdr))) {
750 err = -EINVAL;
751 goto exit_f;
754 rthdr = (struct ipv6_rt_hdr *)CMSG_DATA(cmsg);
756 switch (rthdr->type) {
757 #if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
758 case IPV6_SRCRT_TYPE_2:
759 if (rthdr->hdrlen != 2 ||
760 rthdr->segments_left != 1) {
761 err = -EINVAL;
762 goto exit_f;
764 break;
765 #endif
766 default:
767 err = -EINVAL;
768 goto exit_f;
771 len = ((rthdr->hdrlen + 1) << 3);
773 if (cmsg->cmsg_len < CMSG_LEN(len)) {
774 err = -EINVAL;
775 goto exit_f;
778 /* segments left must also match */
779 if ((rthdr->hdrlen >> 1) != rthdr->segments_left) {
780 err = -EINVAL;
781 goto exit_f;
784 opt->opt_nflen += len;
785 opt->srcrt = rthdr;
787 if (cmsg->cmsg_type == IPV6_2292RTHDR && opt->dst1opt) {
788 int dsthdrlen = ((opt->dst1opt->hdrlen+1)<<3);
790 opt->opt_nflen += dsthdrlen;
791 opt->dst0opt = opt->dst1opt;
792 opt->dst1opt = NULL;
793 opt->opt_flen -= dsthdrlen;
796 break;
798 case IPV6_2292HOPLIMIT:
799 case IPV6_HOPLIMIT:
800 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
801 err = -EINVAL;
802 goto exit_f;
805 *hlimit = *(int *)CMSG_DATA(cmsg);
806 if (*hlimit < -1 || *hlimit > 0xff) {
807 err = -EINVAL;
808 goto exit_f;
811 break;
813 case IPV6_TCLASS:
815 int tc;
817 err = -EINVAL;
818 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
819 goto exit_f;
822 tc = *(int *)CMSG_DATA(cmsg);
823 if (tc < -1 || tc > 0xff)
824 goto exit_f;
826 err = 0;
827 *tclass = tc;
829 break;
832 case IPV6_DONTFRAG:
834 int df;
836 err = -EINVAL;
837 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) {
838 goto exit_f;
841 df = *(int *)CMSG_DATA(cmsg);
842 if (df < 0 || df > 1)
843 goto exit_f;
845 err = 0;
846 *dontfrag = df;
848 break;
850 default:
851 LIMIT_NETDEBUG(KERN_DEBUG "invalid cmsg type: %d\n",
852 cmsg->cmsg_type);
853 err = -EINVAL;
854 goto exit_f;
858 exit_f:
859 return err;