[MIPS] update cobalt_defconfig
[pv_ops_mirror.git] / net / sctp / ipv6.c
blob2c29394fd92ebf4367a24d7bb8883b657832c10f
1 /* SCTP kernel reference 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 reference Implementation
9 * SCTP over IPv6.
11 * The SCTP reference 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)
15 * any later version.
17 * The SCTP reference 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, write to
25 * the Free Software Foundation, 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
30 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 * Or submit a bug report through the following website:
33 * http://www.sf.net/projects/lksctp
35 * Written or modified by:
36 * Le Yanqun <yanqun.le@nokia.com>
37 * Hui Huang <hui.huang@nokia.com>
38 * La Monte H.P. Yarroll <piggy@acm.org>
39 * Sridhar Samudrala <sri@us.ibm.com>
40 * Jon Grimm <jgrimm@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
43 * Based on:
44 * linux/net/ipv6/tcp_ipv6.c
46 * Any bugs reported given to us we will try to fix... any fixes shared will
47 * be incorporated into the next SCTP release.
50 #include <linux/module.h>
51 #include <linux/errno.h>
52 #include <linux/types.h>
53 #include <linux/socket.h>
54 #include <linux/sockios.h>
55 #include <linux/net.h>
56 #include <linux/in.h>
57 #include <linux/in6.h>
58 #include <linux/netdevice.h>
59 #include <linux/init.h>
60 #include <linux/ipsec.h>
62 #include <linux/ipv6.h>
63 #include <linux/icmpv6.h>
64 #include <linux/random.h>
65 #include <linux/seq_file.h>
67 #include <net/protocol.h>
68 #include <net/ndisc.h>
69 #include <net/ip.h>
70 #include <net/ipv6.h>
71 #include <net/transp_v6.h>
72 #include <net/addrconf.h>
73 #include <net/ip6_route.h>
74 #include <net/inet_common.h>
75 #include <net/inet_ecn.h>
76 #include <net/sctp/sctp.h>
78 #include <asm/uaccess.h>
80 /* Event handler for inet6 address addition/deletion events. */
81 static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
82 void *ptr)
84 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
85 struct sctp_sockaddr_entry *addr;
86 struct list_head *pos, *temp;
88 switch (ev) {
89 case NETDEV_UP:
90 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
91 if (addr) {
92 addr->a.v6.sin6_family = AF_INET6;
93 addr->a.v6.sin6_port = 0;
94 memcpy(&addr->a.v6.sin6_addr, &ifa->addr,
95 sizeof(struct in6_addr));
96 addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
97 list_add_tail(&addr->list, &sctp_local_addr_list);
99 break;
100 case NETDEV_DOWN:
101 list_for_each_safe(pos, temp, &sctp_local_addr_list) {
102 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
103 if (ipv6_addr_equal(&addr->a.v6.sin6_addr, &ifa->addr)) {
104 list_del(pos);
105 kfree(addr);
106 break;
110 break;
113 return NOTIFY_DONE;
116 static struct notifier_block sctp_inet6addr_notifier = {
117 .notifier_call = sctp_inet6addr_event,
120 /* ICMP error handler. */
121 SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
122 int type, int code, int offset, __be32 info)
124 struct inet6_dev *idev;
125 struct sock *sk;
126 struct sctp_association *asoc;
127 struct sctp_transport *transport;
128 struct ipv6_pinfo *np;
129 sk_buff_data_t saveip, savesctp;
130 int err;
132 idev = in6_dev_get(skb->dev);
134 /* Fix up skb to look at the embedded net header. */
135 saveip = skb->network_header;
136 savesctp = skb->transport_header;
137 skb_reset_network_header(skb);
138 skb_set_transport_header(skb, offset);
139 sk = sctp_err_lookup(AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
140 /* Put back, the original pointers. */
141 skb->network_header = saveip;
142 skb->transport_header = savesctp;
143 if (!sk) {
144 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
145 goto out;
148 /* Warning: The sock lock is held. Remember to call
149 * sctp_err_finish!
152 switch (type) {
153 case ICMPV6_PKT_TOOBIG:
154 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
155 goto out_unlock;
156 case ICMPV6_PARAMPROB:
157 if (ICMPV6_UNK_NEXTHDR == code) {
158 sctp_icmp_proto_unreachable(sk, asoc, transport);
159 goto out_unlock;
161 break;
162 default:
163 break;
166 np = inet6_sk(sk);
167 icmpv6_err_convert(type, code, &err);
168 if (!sock_owned_by_user(sk) && np->recverr) {
169 sk->sk_err = err;
170 sk->sk_error_report(sk);
171 } else { /* Only an error on timeout */
172 sk->sk_err_soft = err;
175 out_unlock:
176 sctp_err_finish(sk, asoc);
177 out:
178 if (likely(idev != NULL))
179 in6_dev_put(idev);
182 /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
183 static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
184 int ipfragok)
186 struct sock *sk = skb->sk;
187 struct ipv6_pinfo *np = inet6_sk(sk);
188 struct flowi fl;
190 memset(&fl, 0, sizeof(fl));
192 fl.proto = sk->sk_protocol;
194 /* Fill in the dest address from the route entry passed with the skb
195 * and the source address from the transport.
197 ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
198 ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
200 fl.fl6_flowlabel = np->flow_label;
201 IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
202 if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
203 fl.oif = transport->saddr.v6.sin6_scope_id;
204 else
205 fl.oif = sk->sk_bound_dev_if;
207 if (np->opt && np->opt->srcrt) {
208 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
209 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
212 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
213 "src:" NIP6_FMT " dst:" NIP6_FMT "\n",
214 __FUNCTION__, skb, skb->len,
215 NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
217 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
219 return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
222 /* Returns the dst cache entry for the given source and destination ip
223 * addresses.
225 static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
226 union sctp_addr *daddr,
227 union sctp_addr *saddr)
229 struct dst_entry *dst;
230 struct flowi fl;
232 memset(&fl, 0, sizeof(fl));
233 ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
234 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
235 fl.oif = daddr->v6.sin6_scope_id;
238 SCTP_DEBUG_PRINTK("%s: DST=" NIP6_FMT " ",
239 __FUNCTION__, NIP6(fl.fl6_dst));
241 if (saddr) {
242 ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
243 SCTP_DEBUG_PRINTK(
244 "SRC=" NIP6_FMT " - ",
245 NIP6(fl.fl6_src));
248 dst = ip6_route_output(NULL, &fl);
249 if (!dst->error) {
250 struct rt6_info *rt;
251 rt = (struct rt6_info *)dst;
252 SCTP_DEBUG_PRINTK(
253 "rt6_dst:" NIP6_FMT " rt6_src:" NIP6_FMT "\n",
254 NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
255 return dst;
257 SCTP_DEBUG_PRINTK("NO ROUTE\n");
258 dst_release(dst);
259 return NULL;
262 /* Returns the number of consecutive initial bits that match in the 2 ipv6
263 * addresses.
265 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
266 union sctp_addr *s2)
268 struct in6_addr *a1 = &s1->v6.sin6_addr;
269 struct in6_addr *a2 = &s2->v6.sin6_addr;
270 int i, j;
272 for (i = 0; i < 4 ; i++) {
273 __be32 a1xora2;
275 a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
277 if ((j = fls(ntohl(a1xora2))))
278 return (i * 32 + 32 - j);
281 return (i*32);
284 /* Fills in the source address(saddr) based on the destination address(daddr)
285 * and asoc's bind address list.
287 static void sctp_v6_get_saddr(struct sctp_association *asoc,
288 struct dst_entry *dst,
289 union sctp_addr *daddr,
290 union sctp_addr *saddr)
292 struct sctp_bind_addr *bp;
293 rwlock_t *addr_lock;
294 struct sctp_sockaddr_entry *laddr;
295 struct list_head *pos;
296 sctp_scope_t scope;
297 union sctp_addr *baddr = NULL;
298 __u8 matchlen = 0;
299 __u8 bmatchlen;
301 SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
302 "daddr:" NIP6_FMT " ",
303 __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
305 if (!asoc) {
306 ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
307 SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: " NIP6_FMT "\n",
308 NIP6(saddr->v6.sin6_addr));
309 return;
312 scope = sctp_scope(daddr);
314 bp = &asoc->base.bind_addr;
315 addr_lock = &asoc->base.addr_lock;
317 /* Go through the bind address list and find the best source address
318 * that matches the scope of the destination address.
320 sctp_read_lock(addr_lock);
321 list_for_each(pos, &bp->address_list) {
322 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
323 if ((laddr->use_as_src) &&
324 (laddr->a.sa.sa_family == AF_INET6) &&
325 (scope <= sctp_scope(&laddr->a))) {
326 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
327 if (!baddr || (matchlen < bmatchlen)) {
328 baddr = &laddr->a;
329 matchlen = bmatchlen;
334 if (baddr) {
335 memcpy(saddr, baddr, sizeof(union sctp_addr));
336 SCTP_DEBUG_PRINTK("saddr: " NIP6_FMT "\n",
337 NIP6(saddr->v6.sin6_addr));
338 } else {
339 printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
340 "address for the dest:" NIP6_FMT "\n",
341 __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
344 sctp_read_unlock(addr_lock);
347 /* Make a copy of all potential local addresses. */
348 static void sctp_v6_copy_addrlist(struct list_head *addrlist,
349 struct net_device *dev)
351 struct inet6_dev *in6_dev;
352 struct inet6_ifaddr *ifp;
353 struct sctp_sockaddr_entry *addr;
355 rcu_read_lock();
356 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
357 rcu_read_unlock();
358 return;
361 read_lock_bh(&in6_dev->lock);
362 for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
363 /* Add the address to the local list. */
364 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
365 if (addr) {
366 addr->a.v6.sin6_family = AF_INET6;
367 addr->a.v6.sin6_port = 0;
368 addr->a.v6.sin6_addr = ifp->addr;
369 addr->a.v6.sin6_scope_id = dev->ifindex;
370 INIT_LIST_HEAD(&addr->list);
371 list_add_tail(&addr->list, addrlist);
375 read_unlock_bh(&in6_dev->lock);
376 rcu_read_unlock();
379 /* Initialize a sockaddr_storage from in incoming skb. */
380 static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
381 int is_saddr)
383 void *from;
384 __be16 *port;
385 struct sctphdr *sh;
387 port = &addr->v6.sin6_port;
388 addr->v6.sin6_family = AF_INET6;
389 addr->v6.sin6_flowinfo = 0; /* FIXME */
390 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
392 sh = sctp_hdr(skb);
393 if (is_saddr) {
394 *port = sh->source;
395 from = &ipv6_hdr(skb)->saddr;
396 } else {
397 *port = sh->dest;
398 from = &ipv6_hdr(skb)->daddr;
400 ipv6_addr_copy(&addr->v6.sin6_addr, from);
403 /* Initialize an sctp_addr from a socket. */
404 static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
406 addr->v6.sin6_family = AF_INET6;
407 addr->v6.sin6_port = 0;
408 addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
411 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
412 static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
414 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
415 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
416 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
417 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
418 inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
419 addr->v4.sin_addr.s_addr;
420 } else {
421 inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
425 /* Initialize sk->sk_daddr from sctp_addr. */
426 static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
428 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
429 inet6_sk(sk)->daddr.s6_addr32[0] = 0;
430 inet6_sk(sk)->daddr.s6_addr32[1] = 0;
431 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
432 inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
433 } else {
434 inet6_sk(sk)->daddr = addr->v6.sin6_addr;
438 /* Initialize a sctp_addr from an address parameter. */
439 static void sctp_v6_from_addr_param(union sctp_addr *addr,
440 union sctp_addr_param *param,
441 __be16 port, int iif)
443 addr->v6.sin6_family = AF_INET6;
444 addr->v6.sin6_port = port;
445 addr->v6.sin6_flowinfo = 0; /* BUG */
446 ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
447 addr->v6.sin6_scope_id = iif;
450 /* Initialize an address parameter from a sctp_addr and return the length
451 * of the address parameter.
453 static int sctp_v6_to_addr_param(const union sctp_addr *addr,
454 union sctp_addr_param *param)
456 int length = sizeof(sctp_ipv6addr_param_t);
458 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
459 param->v6.param_hdr.length = htons(length);
460 ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
462 return length;
465 /* Initialize a sctp_addr from a dst_entry. */
466 static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
467 __be16 port)
469 struct rt6_info *rt = (struct rt6_info *)dst;
470 addr->sa.sa_family = AF_INET6;
471 addr->v6.sin6_port = port;
472 ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
475 /* Compare addresses exactly.
476 * v4-mapped-v6 is also in consideration.
478 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
479 const union sctp_addr *addr2)
481 if (addr1->sa.sa_family != addr2->sa.sa_family) {
482 if (addr1->sa.sa_family == AF_INET &&
483 addr2->sa.sa_family == AF_INET6 &&
484 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr2->v6.sin6_addr)) {
485 if (addr2->v6.sin6_port == addr1->v4.sin_port &&
486 addr2->v6.sin6_addr.s6_addr32[3] ==
487 addr1->v4.sin_addr.s_addr)
488 return 1;
490 if (addr2->sa.sa_family == AF_INET &&
491 addr1->sa.sa_family == AF_INET6 &&
492 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr1->v6.sin6_addr)) {
493 if (addr1->v6.sin6_port == addr2->v4.sin_port &&
494 addr1->v6.sin6_addr.s6_addr32[3] ==
495 addr2->v4.sin_addr.s_addr)
496 return 1;
498 return 0;
500 if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
501 return 0;
502 /* If this is a linklocal address, compare the scope_id. */
503 if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
504 if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
505 (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
506 return 0;
510 return 1;
513 /* Initialize addr struct to INADDR_ANY. */
514 static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
516 memset(addr, 0x00, sizeof(union sctp_addr));
517 addr->v6.sin6_family = AF_INET6;
518 addr->v6.sin6_port = port;
521 /* Is this a wildcard address? */
522 static int sctp_v6_is_any(const union sctp_addr *addr)
524 return ipv6_addr_any(&addr->v6.sin6_addr);
527 /* Should this be available for binding? */
528 static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
530 int type;
531 struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
533 type = ipv6_addr_type(in6);
534 if (IPV6_ADDR_ANY == type)
535 return 1;
536 if (type == IPV6_ADDR_MAPPED) {
537 if (sp && !sp->v4mapped)
538 return 0;
539 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
540 return 0;
541 sctp_v6_map_v4(addr);
542 return sctp_get_af_specific(AF_INET)->available(addr, sp);
544 if (!(type & IPV6_ADDR_UNICAST))
545 return 0;
547 return ipv6_chk_addr(in6, NULL, 0);
550 /* This function checks if the address is a valid address to be used for
551 * SCTP.
553 * Output:
554 * Return 0 - If the address is a non-unicast or an illegal address.
555 * Return 1 - If the address is a unicast.
557 static int sctp_v6_addr_valid(union sctp_addr *addr,
558 struct sctp_sock *sp,
559 const struct sk_buff *skb)
561 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
563 /* Support v4-mapped-v6 address. */
564 if (ret == IPV6_ADDR_MAPPED) {
565 /* Note: This routine is used in input, so v4-mapped-v6
566 * are disallowed here when there is no sctp_sock.
568 if (!sp || !sp->v4mapped)
569 return 0;
570 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
571 return 0;
572 sctp_v6_map_v4(addr);
573 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
576 /* Is this a non-unicast address */
577 if (!(ret & IPV6_ADDR_UNICAST))
578 return 0;
580 return 1;
583 /* What is the scope of 'addr'? */
584 static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
586 int v6scope;
587 sctp_scope_t retval;
589 /* The IPv6 scope is really a set of bit fields.
590 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
593 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
594 switch (v6scope) {
595 case IFA_HOST:
596 retval = SCTP_SCOPE_LOOPBACK;
597 break;
598 case IFA_LINK:
599 retval = SCTP_SCOPE_LINK;
600 break;
601 case IFA_SITE:
602 retval = SCTP_SCOPE_PRIVATE;
603 break;
604 default:
605 retval = SCTP_SCOPE_GLOBAL;
606 break;
609 return retval;
612 /* Create and initialize a new sk for the socket to be returned by accept(). */
613 static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
614 struct sctp_association *asoc)
616 struct inet_sock *inet = inet_sk(sk);
617 struct sock *newsk;
618 struct inet_sock *newinet;
619 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
620 struct sctp6_sock *newsctp6sk;
622 newsk = sk_alloc(PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
623 if (!newsk)
624 goto out;
626 sock_init_data(NULL, newsk);
628 newsk->sk_type = SOCK_STREAM;
630 newsk->sk_prot = sk->sk_prot;
631 newsk->sk_no_check = sk->sk_no_check;
632 newsk->sk_reuse = sk->sk_reuse;
634 newsk->sk_destruct = inet_sock_destruct;
635 newsk->sk_family = PF_INET6;
636 newsk->sk_protocol = IPPROTO_SCTP;
637 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
638 newsk->sk_shutdown = sk->sk_shutdown;
639 sock_reset_flag(sk, SOCK_ZAPPED);
641 newsctp6sk = (struct sctp6_sock *)newsk;
642 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
644 newinet = inet_sk(newsk);
645 newnp = inet6_sk(newsk);
647 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
649 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
650 * and getpeername().
652 newinet->sport = inet->sport;
653 newnp->saddr = np->saddr;
654 newnp->rcv_saddr = np->rcv_saddr;
655 newinet->dport = htons(asoc->peer.port);
656 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
658 /* Init the ipv4 part of the socket since we can have sockets
659 * using v6 API for ipv4.
661 newinet->uc_ttl = -1;
662 newinet->mc_loop = 1;
663 newinet->mc_ttl = 1;
664 newinet->mc_index = 0;
665 newinet->mc_list = NULL;
667 if (ipv4_config.no_pmtu_disc)
668 newinet->pmtudisc = IP_PMTUDISC_DONT;
669 else
670 newinet->pmtudisc = IP_PMTUDISC_WANT;
672 sk_refcnt_debug_inc(newsk);
674 if (newsk->sk_prot->init(newsk)) {
675 sk_common_release(newsk);
676 newsk = NULL;
679 out:
680 return newsk;
683 /* Map v4 address to mapped v6 address */
684 static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
686 if (sp->v4mapped && AF_INET == addr->sa.sa_family)
687 sctp_v4_map_v6(addr);
690 /* Where did this skb come from? */
691 static int sctp_v6_skb_iif(const struct sk_buff *skb)
693 struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
694 return opt->iif;
697 /* Was this packet marked by Explicit Congestion Notification? */
698 static int sctp_v6_is_ce(const struct sk_buff *skb)
700 return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
703 /* Dump the v6 addr to the seq file. */
704 static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
706 seq_printf(seq, NIP6_FMT " ", NIP6(addr->v6.sin6_addr));
709 /* Initialize a PF_INET6 socket msg_name. */
710 static void sctp_inet6_msgname(char *msgname, int *addr_len)
712 struct sockaddr_in6 *sin6;
714 sin6 = (struct sockaddr_in6 *)msgname;
715 sin6->sin6_family = AF_INET6;
716 sin6->sin6_flowinfo = 0;
717 sin6->sin6_scope_id = 0; /*FIXME */
718 *addr_len = sizeof(struct sockaddr_in6);
721 /* Initialize a PF_INET msgname from a ulpevent. */
722 static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
723 char *msgname, int *addrlen)
725 struct sockaddr_in6 *sin6, *sin6from;
727 if (msgname) {
728 union sctp_addr *addr;
729 struct sctp_association *asoc;
731 asoc = event->asoc;
732 sctp_inet6_msgname(msgname, addrlen);
733 sin6 = (struct sockaddr_in6 *)msgname;
734 sin6->sin6_port = htons(asoc->peer.port);
735 addr = &asoc->peer.primary_addr;
737 /* Note: If we go to a common v6 format, this code
738 * will change.
741 /* Map ipv4 address into v4-mapped-on-v6 address. */
742 if (sctp_sk(asoc->base.sk)->v4mapped &&
743 AF_INET == addr->sa.sa_family) {
744 sctp_v4_map_v6((union sctp_addr *)sin6);
745 sin6->sin6_addr.s6_addr32[3] =
746 addr->v4.sin_addr.s_addr;
747 return;
750 sin6from = &asoc->peer.primary_addr.v6;
751 ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
752 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
753 sin6->sin6_scope_id = sin6from->sin6_scope_id;
757 /* Initialize a msg_name from an inbound skb. */
758 static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
759 int *addr_len)
761 struct sctphdr *sh;
762 struct sockaddr_in6 *sin6;
764 if (msgname) {
765 sctp_inet6_msgname(msgname, addr_len);
766 sin6 = (struct sockaddr_in6 *)msgname;
767 sh = sctp_hdr(skb);
768 sin6->sin6_port = sh->source;
770 /* Map ipv4 address into v4-mapped-on-v6 address. */
771 if (sctp_sk(skb->sk)->v4mapped &&
772 ip_hdr(skb)->version == 4) {
773 sctp_v4_map_v6((union sctp_addr *)sin6);
774 sin6->sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
775 return;
778 /* Otherwise, just copy the v6 address. */
779 ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
780 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
781 struct sctp_ulpevent *ev = sctp_skb2event(skb);
782 sin6->sin6_scope_id = ev->iif;
787 /* Do we support this AF? */
788 static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
790 switch (family) {
791 case AF_INET6:
792 return 1;
793 /* v4-mapped-v6 addresses */
794 case AF_INET:
795 if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
796 return 1;
797 default:
798 return 0;
802 /* Address matching with wildcards allowed. This extra level
803 * of indirection lets us choose whether a PF_INET6 should
804 * disallow any v4 addresses if we so choose.
806 static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
807 const union sctp_addr *addr2,
808 struct sctp_sock *opt)
810 struct sctp_af *af1, *af2;
812 af1 = sctp_get_af_specific(addr1->sa.sa_family);
813 af2 = sctp_get_af_specific(addr2->sa.sa_family);
815 if (!af1 || !af2)
816 return 0;
817 /* Today, wildcard AF_INET/AF_INET6. */
818 if (sctp_is_any(addr1) || sctp_is_any(addr2))
819 return 1;
821 if (addr1->sa.sa_family != addr2->sa.sa_family)
822 return 0;
824 return af1->cmp_addr(addr1, addr2);
827 /* Verify that the provided sockaddr looks bindable. Common verification,
828 * has already been taken care of.
830 static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
832 struct sctp_af *af;
834 /* ASSERT: address family has already been verified. */
835 if (addr->sa.sa_family != AF_INET6)
836 af = sctp_get_af_specific(addr->sa.sa_family);
837 else {
838 int type = ipv6_addr_type(&addr->v6.sin6_addr);
839 struct net_device *dev;
841 if (type & IPV6_ADDR_LINKLOCAL) {
842 if (!addr->v6.sin6_scope_id)
843 return 0;
844 dev = dev_get_by_index(addr->v6.sin6_scope_id);
845 if (!dev)
846 return 0;
847 if (!ipv6_chk_addr(&addr->v6.sin6_addr, dev, 0)) {
848 dev_put(dev);
849 return 0;
851 dev_put(dev);
853 af = opt->pf->af;
855 return af->available(addr, opt);
858 /* Verify that the provided sockaddr looks sendable. Common verification,
859 * has already been taken care of.
861 static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
863 struct sctp_af *af = NULL;
865 /* ASSERT: address family has already been verified. */
866 if (addr->sa.sa_family != AF_INET6)
867 af = sctp_get_af_specific(addr->sa.sa_family);
868 else {
869 int type = ipv6_addr_type(&addr->v6.sin6_addr);
870 struct net_device *dev;
872 if (type & IPV6_ADDR_LINKLOCAL) {
873 if (!addr->v6.sin6_scope_id)
874 return 0;
875 dev = dev_get_by_index(addr->v6.sin6_scope_id);
876 if (!dev)
877 return 0;
878 dev_put(dev);
880 af = opt->pf->af;
883 return af != NULL;
886 /* Fill in Supported Address Type information for INIT and INIT-ACK
887 * chunks. Note: In the future, we may want to look at sock options
888 * to determine whether a PF_INET6 socket really wants to have IPV4
889 * addresses.
890 * Returns number of addresses supported.
892 static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
893 __be16 *types)
895 types[0] = SCTP_PARAM_IPV4_ADDRESS;
896 types[1] = SCTP_PARAM_IPV6_ADDRESS;
897 return 2;
900 static const struct proto_ops inet6_seqpacket_ops = {
901 .family = PF_INET6,
902 .owner = THIS_MODULE,
903 .release = inet6_release,
904 .bind = inet6_bind,
905 .connect = inet_dgram_connect,
906 .socketpair = sock_no_socketpair,
907 .accept = inet_accept,
908 .getname = inet6_getname,
909 .poll = sctp_poll,
910 .ioctl = inet6_ioctl,
911 .listen = sctp_inet_listen,
912 .shutdown = inet_shutdown,
913 .setsockopt = sock_common_setsockopt,
914 .getsockopt = sock_common_getsockopt,
915 .sendmsg = inet_sendmsg,
916 .recvmsg = sock_common_recvmsg,
917 .mmap = sock_no_mmap,
918 #ifdef CONFIG_COMPAT
919 .compat_setsockopt = compat_sock_common_setsockopt,
920 .compat_getsockopt = compat_sock_common_getsockopt,
921 #endif
924 static struct inet_protosw sctpv6_seqpacket_protosw = {
925 .type = SOCK_SEQPACKET,
926 .protocol = IPPROTO_SCTP,
927 .prot = &sctpv6_prot,
928 .ops = &inet6_seqpacket_ops,
929 .capability = -1,
930 .no_check = 0,
931 .flags = SCTP_PROTOSW_FLAG
933 static struct inet_protosw sctpv6_stream_protosw = {
934 .type = SOCK_STREAM,
935 .protocol = IPPROTO_SCTP,
936 .prot = &sctpv6_prot,
937 .ops = &inet6_seqpacket_ops,
938 .capability = -1,
939 .no_check = 0,
940 .flags = SCTP_PROTOSW_FLAG,
943 static int sctp6_rcv(struct sk_buff **pskb)
945 return sctp_rcv(*pskb) ? -1 : 0;
948 static struct inet6_protocol sctpv6_protocol = {
949 .handler = sctp6_rcv,
950 .err_handler = sctp_v6_err,
951 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
954 static struct sctp_af sctp_ipv6_specific = {
955 .sa_family = AF_INET6,
956 .sctp_xmit = sctp_v6_xmit,
957 .setsockopt = ipv6_setsockopt,
958 .getsockopt = ipv6_getsockopt,
959 .get_dst = sctp_v6_get_dst,
960 .get_saddr = sctp_v6_get_saddr,
961 .copy_addrlist = sctp_v6_copy_addrlist,
962 .from_skb = sctp_v6_from_skb,
963 .from_sk = sctp_v6_from_sk,
964 .to_sk_saddr = sctp_v6_to_sk_saddr,
965 .to_sk_daddr = sctp_v6_to_sk_daddr,
966 .from_addr_param = sctp_v6_from_addr_param,
967 .to_addr_param = sctp_v6_to_addr_param,
968 .dst_saddr = sctp_v6_dst_saddr,
969 .cmp_addr = sctp_v6_cmp_addr,
970 .scope = sctp_v6_scope,
971 .addr_valid = sctp_v6_addr_valid,
972 .inaddr_any = sctp_v6_inaddr_any,
973 .is_any = sctp_v6_is_any,
974 .available = sctp_v6_available,
975 .skb_iif = sctp_v6_skb_iif,
976 .is_ce = sctp_v6_is_ce,
977 .seq_dump_addr = sctp_v6_seq_dump_addr,
978 .net_header_len = sizeof(struct ipv6hdr),
979 .sockaddr_len = sizeof(struct sockaddr_in6),
980 #ifdef CONFIG_COMPAT
981 .compat_setsockopt = compat_ipv6_setsockopt,
982 .compat_getsockopt = compat_ipv6_getsockopt,
983 #endif
986 static struct sctp_pf sctp_pf_inet6_specific = {
987 .event_msgname = sctp_inet6_event_msgname,
988 .skb_msgname = sctp_inet6_skb_msgname,
989 .af_supported = sctp_inet6_af_supported,
990 .cmp_addr = sctp_inet6_cmp_addr,
991 .bind_verify = sctp_inet6_bind_verify,
992 .send_verify = sctp_inet6_send_verify,
993 .supported_addrs = sctp_inet6_supported_addrs,
994 .create_accept_sk = sctp_v6_create_accept_sk,
995 .addr_v4map = sctp_v6_addr_v4map,
996 .af = &sctp_ipv6_specific,
999 /* Initialize IPv6 support and register with socket layer. */
1000 int sctp_v6_init(void)
1002 int rc;
1004 /* Register the SCTP specific PF_INET6 functions. */
1005 sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
1007 /* Register the SCTP specific AF_INET6 functions. */
1008 sctp_register_af(&sctp_ipv6_specific);
1010 rc = proto_register(&sctpv6_prot, 1);
1011 if (rc)
1012 return rc;
1014 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
1015 inet6_register_protosw(&sctpv6_seqpacket_protosw);
1016 inet6_register_protosw(&sctpv6_stream_protosw);
1018 return 0;
1021 /* Register with inet6 layer. */
1022 int sctp_v6_add_protocol(void)
1024 /* Register notifier for inet6 address additions/deletions. */
1025 register_inet6addr_notifier(&sctp_inet6addr_notifier);
1027 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1028 return -EAGAIN;
1030 return 0;
1033 /* IPv6 specific exit support. */
1034 void sctp_v6_exit(void)
1036 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1037 inet6_unregister_protosw(&sctpv6_stream_protosw);
1038 proto_unregister(&sctpv6_prot);
1039 list_del(&sctp_ipv6_specific.list);
1042 /* Unregister with inet6 layer. */
1043 void sctp_v6_del_protocol(void)
1045 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1046 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);