1 /* SCTP kernel implementation
2 * (C) Copyright IBM Corp. 2002, 2004
3 * Copyright (c) 2001 Nokia, Inc.
4 * Copyright (c) 2001 La Monte H.P. Yarroll
5 * Copyright (c) 2002-2003 Intel Corp.
7 * This file is part of the SCTP kernel implementation
11 * This SCTP implementation is free software;
12 * you can redistribute it and/or modify it under the terms of
13 * the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This SCTP implementation is distributed in the hope that it
18 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 * ************************
20 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with GNU CC; see the file COPYING. If not, see
25 * <http://www.gnu.org/licenses/>.
27 * Please send any bug reports or fixes you make to the
29 * lksctp developers <linux-sctp@vger.kernel.org>
31 * Written or modified by:
32 * Le Yanqun <yanqun.le@nokia.com>
33 * Hui Huang <hui.huang@nokia.com>
34 * La Monte H.P. Yarroll <piggy@acm.org>
35 * Sridhar Samudrala <sri@us.ibm.com>
36 * Jon Grimm <jgrimm@us.ibm.com>
37 * Ardelle Fan <ardelle.fan@intel.com>
40 * linux/net/ipv6/tcp_ipv6.c
43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
45 #include <linux/module.h>
46 #include <linux/errno.h>
47 #include <linux/types.h>
48 #include <linux/socket.h>
49 #include <linux/sockios.h>
50 #include <linux/net.h>
52 #include <linux/in6.h>
53 #include <linux/netdevice.h>
54 #include <linux/init.h>
55 #include <linux/ipsec.h>
56 #include <linux/slab.h>
58 #include <linux/ipv6.h>
59 #include <linux/icmpv6.h>
60 #include <linux/random.h>
61 #include <linux/seq_file.h>
63 #include <net/protocol.h>
64 #include <net/ndisc.h>
67 #include <net/transp_v6.h>
68 #include <net/addrconf.h>
69 #include <net/ip6_route.h>
70 #include <net/inet_common.h>
71 #include <net/inet_ecn.h>
72 #include <net/sctp/sctp.h>
74 #include <asm/uaccess.h>
76 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
78 static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
80 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
81 const union sctp_addr *addr2);
83 /* Event handler for inet6 address addition/deletion events.
84 * The sctp_local_addr_list needs to be protocted by a spin lock since
85 * multiple notifiers (say IPv4 and IPv6) may be running at the same
86 * time and thus corrupt the list.
87 * The reader side is protected with RCU.
89 static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
92 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
93 struct sctp_sockaddr_entry *addr = NULL;
94 struct sctp_sockaddr_entry *temp;
95 struct net *net = dev_net(ifa->idev->dev);
100 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
102 addr->a.v6.sin6_family = AF_INET6;
103 addr->a.v6.sin6_addr = ifa->addr;
104 addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
106 spin_lock_bh(&net->sctp.local_addr_lock);
107 list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
108 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
109 spin_unlock_bh(&net->sctp.local_addr_lock);
113 spin_lock_bh(&net->sctp.local_addr_lock);
114 list_for_each_entry_safe(addr, temp,
115 &net->sctp.local_addr_list, list) {
116 if (addr->a.sa.sa_family == AF_INET6 &&
117 ipv6_addr_equal(&addr->a.v6.sin6_addr,
119 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
122 list_del_rcu(&addr->list);
126 spin_unlock_bh(&net->sctp.local_addr_lock);
128 kfree_rcu(addr, rcu);
135 static struct notifier_block sctp_inet6addr_notifier = {
136 .notifier_call = sctp_inet6addr_event,
139 /* ICMP error handler. */
140 static void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
141 u8 type, u8 code, int offset, __be32 info)
143 struct inet6_dev *idev;
145 struct sctp_association *asoc;
146 struct sctp_transport *transport;
147 struct ipv6_pinfo *np;
148 __u16 saveip, savesctp;
150 struct net *net = dev_net(skb->dev);
152 idev = in6_dev_get(skb->dev);
154 /* Fix up skb to look at the embedded net header. */
155 saveip = skb->network_header;
156 savesctp = skb->transport_header;
157 skb_reset_network_header(skb);
158 skb_set_transport_header(skb, offset);
159 sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
160 /* Put back, the original pointers. */
161 skb->network_header = saveip;
162 skb->transport_header = savesctp;
164 ICMP6_INC_STATS_BH(net, idev, ICMP6_MIB_INERRORS);
168 /* Warning: The sock lock is held. Remember to call
173 case ICMPV6_PKT_TOOBIG:
174 if (ip6_sk_accept_pmtu(sk))
175 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
177 case ICMPV6_PARAMPROB:
178 if (ICMPV6_UNK_NEXTHDR == code) {
179 sctp_icmp_proto_unreachable(sk, asoc, transport);
184 sctp_icmp_redirect(sk, transport, skb);
191 icmpv6_err_convert(type, code, &err);
192 if (!sock_owned_by_user(sk) && np->recverr) {
194 sk->sk_error_report(sk);
195 } else { /* Only an error on timeout */
196 sk->sk_err_soft = err;
200 sctp_err_finish(sk, asoc);
202 if (likely(idev != NULL))
206 static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport)
208 struct sock *sk = skb->sk;
209 struct ipv6_pinfo *np = inet6_sk(sk);
210 struct flowi6 *fl6 = &transport->fl.u.ip6;
213 pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb,
214 skb->len, &fl6->saddr, &fl6->daddr);
216 IP6_ECN_flow_xmit(sk, fl6->flowlabel);
218 if (!(transport->param_flags & SPP_PMTUD_ENABLE))
221 SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
224 res = ip6_xmit(sk, skb, fl6, rcu_dereference(np->opt), np->tclass);
229 /* Returns the dst cache entry for the given source and destination ip
232 static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
233 struct flowi *fl, struct sock *sk)
235 struct sctp_association *asoc = t->asoc;
236 struct dst_entry *dst = NULL;
237 struct flowi6 *fl6 = &fl->u.ip6;
238 struct sctp_bind_addr *bp;
239 struct ipv6_pinfo *np = inet6_sk(sk);
240 struct sctp_sockaddr_entry *laddr;
241 union sctp_addr *daddr = &t->ipaddr;
242 union sctp_addr dst_saddr;
243 struct in6_addr *final_p, final;
247 memset(fl6, 0, sizeof(struct flowi6));
248 fl6->daddr = daddr->v6.sin6_addr;
249 fl6->fl6_dport = daddr->v6.sin6_port;
250 fl6->flowi6_proto = IPPROTO_SCTP;
251 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
252 fl6->flowi6_oif = daddr->v6.sin6_scope_id;
254 pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
257 fl6->fl6_sport = htons(asoc->base.bind_addr.port);
260 fl6->saddr = saddr->v6.sin6_addr;
261 fl6->fl6_sport = saddr->v6.sin6_port;
263 pr_debug("src=%pI6 - ", &fl6->saddr);
267 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
270 dst = ip6_dst_lookup_flow(sk, fl6, final_p);
274 bp = &asoc->base.bind_addr;
275 scope = sctp_scope(daddr);
276 /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
277 * to see if we can use it.
280 /* Walk through the bind address list and look for a bind
281 * address that matches the source address of the returned dst.
283 sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
285 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
286 if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
287 (laddr->state != SCTP_ADDR_SRC &&
288 !asoc->src_out_of_asoc_ok))
291 /* Do not compare against v4 addrs */
292 if ((laddr->a.sa.sa_family == AF_INET6) &&
293 (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
299 /* None of the bound addresses match the source address of the
300 * dst. So release it.
306 /* Walk through the bind address list and try to get the
307 * best source address for a given destination.
310 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
311 struct dst_entry *bdst;
315 laddr->state != SCTP_ADDR_SRC ||
316 laddr->a.sa.sa_family != AF_INET6 ||
317 scope > sctp_scope(&laddr->a))
320 fl6->saddr = laddr->a.v6.sin6_addr;
321 fl6->fl6_sport = laddr->a.v6.sin6_port;
322 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
323 bdst = ip6_dst_lookup_flow(sk, fl6, final_p);
328 if (ipv6_chk_addr(dev_net(bdst->dev),
329 &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
330 if (!IS_ERR_OR_NULL(dst))
336 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
337 if (matchlen > bmatchlen) {
342 if (!IS_ERR_OR_NULL(dst))
345 matchlen = bmatchlen;
350 if (!IS_ERR_OR_NULL(dst)) {
353 rt = (struct rt6_info *)dst;
355 t->dst_cookie = rt6_get_cookie(rt);
356 pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
357 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
362 pr_debug("no route\n");
366 /* Returns the number of consecutive initial bits that match in the 2 ipv6
369 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
372 return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
375 /* Fills in the source address(saddr) based on the destination address(daddr)
376 * and asoc's bind address list.
378 static void sctp_v6_get_saddr(struct sctp_sock *sk,
379 struct sctp_transport *t,
382 struct flowi6 *fl6 = &fl->u.ip6;
383 union sctp_addr *saddr = &t->saddr;
385 pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
388 saddr->v6.sin6_family = AF_INET6;
389 saddr->v6.sin6_addr = fl6->saddr;
393 /* Make a copy of all potential local addresses. */
394 static void sctp_v6_copy_addrlist(struct list_head *addrlist,
395 struct net_device *dev)
397 struct inet6_dev *in6_dev;
398 struct inet6_ifaddr *ifp;
399 struct sctp_sockaddr_entry *addr;
402 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
407 read_lock_bh(&in6_dev->lock);
408 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
409 /* Add the address to the local list. */
410 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
412 addr->a.v6.sin6_family = AF_INET6;
413 addr->a.v6.sin6_addr = ifp->addr;
414 addr->a.v6.sin6_scope_id = dev->ifindex;
416 INIT_LIST_HEAD(&addr->list);
417 list_add_tail(&addr->list, addrlist);
421 read_unlock_bh(&in6_dev->lock);
425 /* Initialize a sockaddr_storage from in incoming skb. */
426 static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
432 port = &addr->v6.sin6_port;
433 addr->v6.sin6_family = AF_INET6;
434 addr->v6.sin6_flowinfo = 0; /* FIXME */
435 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
440 addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
443 addr->v6.sin6_addr = ipv6_hdr(skb)->daddr;
447 /* Initialize an sctp_addr from a socket. */
448 static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
450 addr->v6.sin6_family = AF_INET6;
451 addr->v6.sin6_port = 0;
452 addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
455 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
456 static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
458 if (addr->sa.sa_family == AF_INET) {
459 sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
460 sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
461 sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
462 sk->sk_v6_rcv_saddr.s6_addr32[3] =
463 addr->v4.sin_addr.s_addr;
465 sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
469 /* Initialize sk->sk_daddr from sctp_addr. */
470 static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
472 if (addr->sa.sa_family == AF_INET) {
473 sk->sk_v6_daddr.s6_addr32[0] = 0;
474 sk->sk_v6_daddr.s6_addr32[1] = 0;
475 sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
476 sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
478 sk->sk_v6_daddr = addr->v6.sin6_addr;
482 /* Initialize a sctp_addr from an address parameter. */
483 static void sctp_v6_from_addr_param(union sctp_addr *addr,
484 union sctp_addr_param *param,
485 __be16 port, int iif)
487 addr->v6.sin6_family = AF_INET6;
488 addr->v6.sin6_port = port;
489 addr->v6.sin6_flowinfo = 0; /* BUG */
490 addr->v6.sin6_addr = param->v6.addr;
491 addr->v6.sin6_scope_id = iif;
494 /* Initialize an address parameter from a sctp_addr and return the length
495 * of the address parameter.
497 static int sctp_v6_to_addr_param(const union sctp_addr *addr,
498 union sctp_addr_param *param)
500 int length = sizeof(sctp_ipv6addr_param_t);
502 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
503 param->v6.param_hdr.length = htons(length);
504 param->v6.addr = addr->v6.sin6_addr;
509 /* Initialize a sctp_addr from struct in6_addr. */
510 static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
513 addr->sa.sa_family = AF_INET6;
514 addr->v6.sin6_port = port;
515 addr->v6.sin6_flowinfo = 0;
516 addr->v6.sin6_addr = *saddr;
517 addr->v6.sin6_scope_id = 0;
520 static int __sctp_v6_cmp_addr(const union sctp_addr *addr1,
521 const union sctp_addr *addr2)
523 if (addr1->sa.sa_family != addr2->sa.sa_family) {
524 if (addr1->sa.sa_family == AF_INET &&
525 addr2->sa.sa_family == AF_INET6 &&
526 ipv6_addr_v4mapped(&addr2->v6.sin6_addr) &&
527 addr2->v6.sin6_addr.s6_addr32[3] ==
528 addr1->v4.sin_addr.s_addr)
531 if (addr2->sa.sa_family == AF_INET &&
532 addr1->sa.sa_family == AF_INET6 &&
533 ipv6_addr_v4mapped(&addr1->v6.sin6_addr) &&
534 addr1->v6.sin6_addr.s6_addr32[3] ==
535 addr2->v4.sin_addr.s_addr)
541 if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
544 /* If this is a linklocal address, compare the scope_id. */
545 if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
546 addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
547 addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)
553 /* Compare addresses exactly.
554 * v4-mapped-v6 is also in consideration.
556 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
557 const union sctp_addr *addr2)
559 return __sctp_v6_cmp_addr(addr1, addr2) &&
560 addr1->v6.sin6_port == addr2->v6.sin6_port;
563 /* Initialize addr struct to INADDR_ANY. */
564 static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
566 memset(addr, 0x00, sizeof(union sctp_addr));
567 addr->v6.sin6_family = AF_INET6;
568 addr->v6.sin6_port = port;
571 /* Is this a wildcard address? */
572 static int sctp_v6_is_any(const union sctp_addr *addr)
574 return ipv6_addr_any(&addr->v6.sin6_addr);
577 /* Should this be available for binding? */
578 static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
581 const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
583 type = ipv6_addr_type(in6);
584 if (IPV6_ADDR_ANY == type)
586 if (type == IPV6_ADDR_MAPPED) {
587 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
589 sctp_v6_map_v4(addr);
590 return sctp_get_af_specific(AF_INET)->available(addr, sp);
592 if (!(type & IPV6_ADDR_UNICAST))
595 return ipv6_chk_addr(sock_net(&sp->inet.sk), in6, NULL, 0);
598 /* This function checks if the address is a valid address to be used for
602 * Return 0 - If the address is a non-unicast or an illegal address.
603 * Return 1 - If the address is a unicast.
605 static int sctp_v6_addr_valid(union sctp_addr *addr,
606 struct sctp_sock *sp,
607 const struct sk_buff *skb)
609 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
611 /* Support v4-mapped-v6 address. */
612 if (ret == IPV6_ADDR_MAPPED) {
613 /* Note: This routine is used in input, so v4-mapped-v6
614 * are disallowed here when there is no sctp_sock.
616 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
618 sctp_v6_map_v4(addr);
619 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
622 /* Is this a non-unicast address */
623 if (!(ret & IPV6_ADDR_UNICAST))
629 /* What is the scope of 'addr'? */
630 static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
635 /* The IPv6 scope is really a set of bit fields.
636 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
639 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
642 retval = SCTP_SCOPE_LOOPBACK;
645 retval = SCTP_SCOPE_LINK;
648 retval = SCTP_SCOPE_PRIVATE;
651 retval = SCTP_SCOPE_GLOBAL;
658 /* Create and initialize a new sk for the socket to be returned by accept(). */
659 static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
660 struct sctp_association *asoc)
663 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
664 struct sctp6_sock *newsctp6sk;
665 struct ipv6_txoptions *opt;
667 newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, 0);
671 sock_init_data(NULL, newsk);
673 sctp_copy_sock(newsk, sk, asoc);
674 sock_reset_flag(sk, SOCK_ZAPPED);
676 newsctp6sk = (struct sctp6_sock *)newsk;
677 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
679 sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
681 newnp = inet6_sk(newsk);
683 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
684 newnp->ipv6_mc_list = NULL;
685 newnp->ipv6_ac_list = NULL;
686 newnp->ipv6_fl_list = NULL;
689 opt = rcu_dereference(np->opt);
691 opt = ipv6_dup_options(newsk, opt);
692 RCU_INIT_POINTER(newnp->opt, opt);
695 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
698 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
700 newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
702 sk_refcnt_debug_inc(newsk);
704 if (newsk->sk_prot->init(newsk)) {
705 sk_common_release(newsk);
713 /* Format a sockaddr for return to user space. This makes sure the return is
714 * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option.
716 static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
719 if (addr->sa.sa_family == AF_INET)
720 sctp_v4_map_v6(addr);
722 if (addr->sa.sa_family == AF_INET6 &&
723 ipv6_addr_v4mapped(&addr->v6.sin6_addr))
724 sctp_v6_map_v4(addr);
727 if (addr->sa.sa_family == AF_INET) {
728 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
729 return sizeof(struct sockaddr_in);
731 return sizeof(struct sockaddr_in6);
734 /* Where did this skb come from? */
735 static int sctp_v6_skb_iif(const struct sk_buff *skb)
737 struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
741 /* Was this packet marked by Explicit Congestion Notification? */
742 static int sctp_v6_is_ce(const struct sk_buff *skb)
744 return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
747 /* Dump the v6 addr to the seq file. */
748 static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
750 seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
753 static void sctp_v6_ecn_capable(struct sock *sk)
755 inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
758 /* Initialize a PF_INET msgname from a ulpevent. */
759 static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
760 char *msgname, int *addrlen)
762 union sctp_addr *addr;
763 struct sctp_association *asoc;
764 union sctp_addr *paddr;
769 addr = (union sctp_addr *)msgname;
771 paddr = &asoc->peer.primary_addr;
773 if (paddr->sa.sa_family == AF_INET) {
774 addr->v4.sin_family = AF_INET;
775 addr->v4.sin_port = htons(asoc->peer.port);
776 addr->v4.sin_addr = paddr->v4.sin_addr;
778 addr->v6.sin6_family = AF_INET6;
779 addr->v6.sin6_flowinfo = 0;
780 if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
781 addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
783 addr->v6.sin6_scope_id = 0;
784 addr->v6.sin6_port = htons(asoc->peer.port);
785 addr->v6.sin6_addr = paddr->v6.sin6_addr;
788 *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
791 /* Initialize a msg_name from an inbound skb. */
792 static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
795 union sctp_addr *addr;
801 addr = (union sctp_addr *)msgname;
804 if (ip_hdr(skb)->version == 4) {
805 addr->v4.sin_family = AF_INET;
806 addr->v4.sin_port = sh->source;
807 addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
809 addr->v6.sin6_family = AF_INET6;
810 addr->v6.sin6_flowinfo = 0;
811 addr->v6.sin6_port = sh->source;
812 addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
813 if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
814 struct sctp_ulpevent *ev = sctp_skb2event(skb);
815 addr->v6.sin6_scope_id = ev->iif;
817 addr->v6.sin6_scope_id = 0;
821 *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
824 /* Do we support this AF? */
825 static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
830 /* v4-mapped-v6 addresses */
832 if (!__ipv6_only_sock(sctp_opt2sk(sp)))
839 /* Address matching with wildcards allowed. This extra level
840 * of indirection lets us choose whether a PF_INET6 should
841 * disallow any v4 addresses if we so choose.
843 static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
844 const union sctp_addr *addr2,
845 struct sctp_sock *opt)
847 struct sock *sk = sctp_opt2sk(opt);
848 struct sctp_af *af1, *af2;
850 af1 = sctp_get_af_specific(addr1->sa.sa_family);
851 af2 = sctp_get_af_specific(addr2->sa.sa_family);
856 /* If the socket is IPv6 only, v4 addrs will not match */
857 if (__ipv6_only_sock(sk) && af1 != af2)
860 /* Today, wildcard AF_INET/AF_INET6. */
861 if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
864 if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET)
865 return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr;
867 return __sctp_v6_cmp_addr(addr1, addr2);
870 /* Verify that the provided sockaddr looks bindable. Common verification,
871 * has already been taken care of.
873 static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
877 /* ASSERT: address family has already been verified. */
878 if (addr->sa.sa_family != AF_INET6)
879 af = sctp_get_af_specific(addr->sa.sa_family);
881 int type = ipv6_addr_type(&addr->v6.sin6_addr);
882 struct net_device *dev;
884 if (type & IPV6_ADDR_LINKLOCAL) {
886 if (!addr->v6.sin6_scope_id)
888 net = sock_net(&opt->inet.sk);
890 dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
892 !ipv6_chk_addr(net, &addr->v6.sin6_addr, dev, 0)) {
901 return af->available(addr, opt);
904 /* Verify that the provided sockaddr looks sendable. Common verification,
905 * has already been taken care of.
907 static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
909 struct sctp_af *af = NULL;
911 /* ASSERT: address family has already been verified. */
912 if (addr->sa.sa_family != AF_INET6)
913 af = sctp_get_af_specific(addr->sa.sa_family);
915 int type = ipv6_addr_type(&addr->v6.sin6_addr);
916 struct net_device *dev;
918 if (type & IPV6_ADDR_LINKLOCAL) {
919 if (!addr->v6.sin6_scope_id)
922 dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
923 addr->v6.sin6_scope_id);
934 /* Fill in Supported Address Type information for INIT and INIT-ACK
935 * chunks. Note: In the future, we may want to look at sock options
936 * to determine whether a PF_INET6 socket really wants to have IPV4
938 * Returns number of addresses supported.
940 static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
943 types[0] = SCTP_PARAM_IPV6_ADDRESS;
944 if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
945 types[1] = SCTP_PARAM_IPV4_ADDRESS;
951 /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */
952 static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
953 int *uaddr_len, int peer)
957 rc = inet6_getname(sock, uaddr, uaddr_len, peer);
962 *uaddr_len = sctp_v6_addr_to_user(sctp_sk(sock->sk),
963 (union sctp_addr *)uaddr);
968 static const struct proto_ops inet6_seqpacket_ops = {
970 .owner = THIS_MODULE,
971 .release = inet6_release,
973 .connect = sctp_inet_connect,
974 .socketpair = sock_no_socketpair,
975 .accept = inet_accept,
976 .getname = sctp_getname,
978 .ioctl = inet6_ioctl,
979 .listen = sctp_inet_listen,
980 .shutdown = inet_shutdown,
981 .setsockopt = sock_common_setsockopt,
982 .getsockopt = sock_common_getsockopt,
983 .sendmsg = inet_sendmsg,
984 .recvmsg = sock_common_recvmsg,
985 .mmap = sock_no_mmap,
987 .compat_setsockopt = compat_sock_common_setsockopt,
988 .compat_getsockopt = compat_sock_common_getsockopt,
992 static struct inet_protosw sctpv6_seqpacket_protosw = {
993 .type = SOCK_SEQPACKET,
994 .protocol = IPPROTO_SCTP,
995 .prot = &sctpv6_prot,
996 .ops = &inet6_seqpacket_ops,
997 .flags = SCTP_PROTOSW_FLAG
999 static struct inet_protosw sctpv6_stream_protosw = {
1000 .type = SOCK_STREAM,
1001 .protocol = IPPROTO_SCTP,
1002 .prot = &sctpv6_prot,
1003 .ops = &inet6_seqpacket_ops,
1004 .flags = SCTP_PROTOSW_FLAG,
1007 static int sctp6_rcv(struct sk_buff *skb)
1009 return sctp_rcv(skb) ? -1 : 0;
1012 static const struct inet6_protocol sctpv6_protocol = {
1013 .handler = sctp6_rcv,
1014 .err_handler = sctp_v6_err,
1015 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1018 static struct sctp_af sctp_af_inet6 = {
1019 .sa_family = AF_INET6,
1020 .sctp_xmit = sctp_v6_xmit,
1021 .setsockopt = ipv6_setsockopt,
1022 .getsockopt = ipv6_getsockopt,
1023 .get_dst = sctp_v6_get_dst,
1024 .get_saddr = sctp_v6_get_saddr,
1025 .copy_addrlist = sctp_v6_copy_addrlist,
1026 .from_skb = sctp_v6_from_skb,
1027 .from_sk = sctp_v6_from_sk,
1028 .from_addr_param = sctp_v6_from_addr_param,
1029 .to_addr_param = sctp_v6_to_addr_param,
1030 .cmp_addr = sctp_v6_cmp_addr,
1031 .scope = sctp_v6_scope,
1032 .addr_valid = sctp_v6_addr_valid,
1033 .inaddr_any = sctp_v6_inaddr_any,
1034 .is_any = sctp_v6_is_any,
1035 .available = sctp_v6_available,
1036 .skb_iif = sctp_v6_skb_iif,
1037 .is_ce = sctp_v6_is_ce,
1038 .seq_dump_addr = sctp_v6_seq_dump_addr,
1039 .ecn_capable = sctp_v6_ecn_capable,
1040 .net_header_len = sizeof(struct ipv6hdr),
1041 .sockaddr_len = sizeof(struct sockaddr_in6),
1042 #ifdef CONFIG_COMPAT
1043 .compat_setsockopt = compat_ipv6_setsockopt,
1044 .compat_getsockopt = compat_ipv6_getsockopt,
1048 static struct sctp_pf sctp_pf_inet6 = {
1049 .event_msgname = sctp_inet6_event_msgname,
1050 .skb_msgname = sctp_inet6_skb_msgname,
1051 .af_supported = sctp_inet6_af_supported,
1052 .cmp_addr = sctp_inet6_cmp_addr,
1053 .bind_verify = sctp_inet6_bind_verify,
1054 .send_verify = sctp_inet6_send_verify,
1055 .supported_addrs = sctp_inet6_supported_addrs,
1056 .create_accept_sk = sctp_v6_create_accept_sk,
1057 .addr_to_user = sctp_v6_addr_to_user,
1058 .to_sk_saddr = sctp_v6_to_sk_saddr,
1059 .to_sk_daddr = sctp_v6_to_sk_daddr,
1060 .af = &sctp_af_inet6,
1063 /* Initialize IPv6 support and register with socket layer. */
1064 void sctp_v6_pf_init(void)
1066 /* Register the SCTP specific PF_INET6 functions. */
1067 sctp_register_pf(&sctp_pf_inet6, PF_INET6);
1069 /* Register the SCTP specific AF_INET6 functions. */
1070 sctp_register_af(&sctp_af_inet6);
1073 void sctp_v6_pf_exit(void)
1075 list_del(&sctp_af_inet6.list);
1078 /* Initialize IPv6 support and register with socket layer. */
1079 int sctp_v6_protosw_init(void)
1083 rc = proto_register(&sctpv6_prot, 1);
1087 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
1088 inet6_register_protosw(&sctpv6_seqpacket_protosw);
1089 inet6_register_protosw(&sctpv6_stream_protosw);
1094 void sctp_v6_protosw_exit(void)
1096 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1097 inet6_unregister_protosw(&sctpv6_stream_protosw);
1098 proto_unregister(&sctpv6_prot);
1102 /* Register with inet6 layer. */
1103 int sctp_v6_add_protocol(void)
1105 /* Register notifier for inet6 address additions/deletions. */
1106 register_inet6addr_notifier(&sctp_inet6addr_notifier);
1108 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1114 /* Unregister with inet6 layer. */
1115 void sctp_v6_del_protocol(void)
1117 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1118 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);