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
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)
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
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>
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/sched.h>
58 #include <linux/in6.h>
59 #include <linux/netdevice.h>
60 #include <linux/init.h>
61 #include <linux/ipsec.h>
63 #include <linux/ipv6.h>
64 #include <linux/icmpv6.h>
65 #include <linux/random.h>
66 #include <linux/seq_file.h>
68 #include <net/protocol.h>
70 #include <net/ndisc.h>
72 #include <net/transp_v6.h>
73 #include <net/addrconf.h>
74 #include <net/ip6_route.h>
75 #include <net/inet_common.h>
76 #include <net/inet_ecn.h>
77 #include <net/sctp/sctp.h>
79 #include <asm/uaccess.h>
81 extern int sctp_inetaddr_event(struct notifier_block
*, unsigned long, void *);
82 static struct notifier_block sctp_inet6addr_notifier
= {
83 .notifier_call
= sctp_inetaddr_event
,
86 /* ICMP error handler. */
87 SCTP_STATIC
void sctp_v6_err(struct sk_buff
*skb
, struct inet6_skb_parm
*opt
,
88 int type
, int code
, int offset
, __u32 info
)
90 struct inet6_dev
*idev
;
91 struct ipv6hdr
*iph
= (struct ipv6hdr
*)skb
->data
;
92 struct sctphdr
*sh
= (struct sctphdr
*)(skb
->data
+ offset
);
94 struct sctp_endpoint
*ep
;
95 struct sctp_association
*asoc
;
96 struct sctp_transport
*transport
;
97 struct ipv6_pinfo
*np
;
98 char *saveip
, *savesctp
;
101 idev
= in6_dev_get(skb
->dev
);
103 /* Fix up skb to look at the embedded net header. */
104 saveip
= skb
->nh
.raw
;
105 savesctp
= skb
->h
.raw
;
107 skb
->h
.raw
= (char *)sh
;
108 sk
= sctp_err_lookup(AF_INET6
, skb
, sh
, &ep
, &asoc
, &transport
);
109 /* Put back, the original pointers. */
110 skb
->nh
.raw
= saveip
;
111 skb
->h
.raw
= savesctp
;
113 ICMP6_INC_STATS_BH(idev
, ICMP6_MIB_INERRORS
);
117 /* Warning: The sock lock is held. Remember to call
122 case ICMPV6_PKT_TOOBIG
:
123 sctp_icmp_frag_needed(sk
, asoc
, transport
, ntohl(info
));
125 case ICMPV6_PARAMPROB
:
126 if (ICMPV6_UNK_NEXTHDR
== code
) {
127 sctp_icmp_proto_unreachable(sk
, ep
, asoc
, transport
);
136 icmpv6_err_convert(type
, code
, &err
);
137 if (!sock_owned_by_user(sk
) && np
->recverr
) {
139 sk
->sk_error_report(sk
);
140 } else { /* Only an error on timeout */
141 sk
->sk_err_soft
= err
;
145 sctp_err_finish(sk
, ep
, asoc
);
147 if (likely(idev
!= NULL
))
151 /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
152 static int sctp_v6_xmit(struct sk_buff
*skb
, struct sctp_transport
*transport
,
155 struct sock
*sk
= skb
->sk
;
156 struct ipv6_pinfo
*np
= inet6_sk(sk
);
159 memset(&fl
, 0, sizeof(fl
));
161 fl
.proto
= sk
->sk_protocol
;
163 /* Fill in the dest address from the route entry passed with the skb
164 * and the source address from the transport.
166 ipv6_addr_copy(&fl
.fl6_dst
, &transport
->ipaddr
.v6
.sin6_addr
);
167 ipv6_addr_copy(&fl
.fl6_src
, &transport
->saddr
.v6
.sin6_addr
);
169 fl
.fl6_flowlabel
= np
->flow_label
;
170 IP6_ECN_flow_xmit(sk
, fl
.fl6_flowlabel
);
171 if (ipv6_addr_type(&fl
.fl6_src
) & IPV6_ADDR_LINKLOCAL
)
172 fl
.oif
= transport
->saddr
.v6
.sin6_scope_id
;
174 fl
.oif
= sk
->sk_bound_dev_if
;
175 fl
.fl_ip_sport
= inet_sk(sk
)->sport
;
176 fl
.fl_ip_dport
= transport
->ipaddr
.v6
.sin6_port
;
178 if (np
->opt
&& np
->opt
->srcrt
) {
179 struct rt0_hdr
*rt0
= (struct rt0_hdr
*) np
->opt
->srcrt
;
180 ipv6_addr_copy(&fl
.fl6_dst
, rt0
->addr
);
183 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
184 "src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
185 "dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
186 __FUNCTION__
, skb
, skb
->len
,
187 NIP6(fl
.fl6_src
), NIP6(fl
.fl6_dst
));
189 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS
);
191 return ip6_xmit(sk
, skb
, &fl
, np
->opt
, ipfragok
);
194 /* Returns the dst cache entry for the given source and destination ip
197 static struct dst_entry
*sctp_v6_get_dst(struct sctp_association
*asoc
,
198 union sctp_addr
*daddr
,
199 union sctp_addr
*saddr
)
201 struct dst_entry
*dst
;
204 memset(&fl
, 0, sizeof(fl
));
205 ipv6_addr_copy(&fl
.fl6_dst
, &daddr
->v6
.sin6_addr
);
206 if (ipv6_addr_type(&daddr
->v6
.sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
207 fl
.oif
= daddr
->v6
.sin6_scope_id
;
210 SCTP_DEBUG_PRINTK("%s: DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
211 __FUNCTION__
, NIP6(fl
.fl6_dst
));
214 ipv6_addr_copy(&fl
.fl6_src
, &saddr
->v6
.sin6_addr
);
216 "SRC=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x - ",
220 dst
= ip6_route_output(NULL
, &fl
);
223 rt
= (struct rt6_info
*)dst
;
225 "rt6_dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
226 "rt6_src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
227 NIP6(rt
->rt6i_dst
.addr
), NIP6(rt
->rt6i_src
.addr
));
229 SCTP_DEBUG_PRINTK("NO ROUTE\n");
235 /* Returns the number of consecutive initial bits that match in the 2 ipv6
238 static inline int sctp_v6_addr_match_len(union sctp_addr
*s1
,
241 struct in6_addr
*a1
= &s1
->v6
.sin6_addr
;
242 struct in6_addr
*a2
= &s2
->v6
.sin6_addr
;
245 for (i
= 0; i
< 4 ; i
++) {
248 a1xora2
= a1
->s6_addr32
[i
] ^ a2
->s6_addr32
[i
];
250 if ((j
= fls(ntohl(a1xora2
))))
251 return (i
* 32 + 32 - j
);
257 /* Fills in the source address(saddr) based on the destination address(daddr)
258 * and asoc's bind address list.
260 static void sctp_v6_get_saddr(struct sctp_association
*asoc
,
261 struct dst_entry
*dst
,
262 union sctp_addr
*daddr
,
263 union sctp_addr
*saddr
)
265 struct sctp_bind_addr
*bp
;
267 struct sctp_sockaddr_entry
*laddr
;
268 struct list_head
*pos
;
270 union sctp_addr
*baddr
= NULL
;
274 SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
275 "daddr:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
276 __FUNCTION__
, asoc
, dst
, NIP6(daddr
->v6
.sin6_addr
));
279 ipv6_get_saddr(dst
, &daddr
->v6
.sin6_addr
,&saddr
->v6
.sin6_addr
);
280 SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: "
281 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
282 NIP6(saddr
->v6
.sin6_addr
));
286 scope
= sctp_scope(daddr
);
288 bp
= &asoc
->base
.bind_addr
;
289 addr_lock
= &asoc
->base
.addr_lock
;
291 /* Go through the bind address list and find the best source address
292 * that matches the scope of the destination address.
294 sctp_read_lock(addr_lock
);
295 list_for_each(pos
, &bp
->address_list
) {
296 laddr
= list_entry(pos
, struct sctp_sockaddr_entry
, list
);
297 if ((laddr
->a
.sa
.sa_family
== AF_INET6
) &&
298 (scope
<= sctp_scope(&laddr
->a
))) {
299 bmatchlen
= sctp_v6_addr_match_len(daddr
, &laddr
->a
);
300 if (!baddr
|| (matchlen
< bmatchlen
)) {
302 matchlen
= bmatchlen
;
308 memcpy(saddr
, baddr
, sizeof(union sctp_addr
));
309 SCTP_DEBUG_PRINTK("saddr: "
310 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
311 NIP6(saddr
->v6
.sin6_addr
));
313 printk(KERN_ERR
"%s: asoc:%p Could not find a valid source "
315 "dest:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
316 __FUNCTION__
, asoc
, NIP6(daddr
->v6
.sin6_addr
));
319 sctp_read_unlock(addr_lock
);
322 /* Make a copy of all potential local addresses. */
323 static void sctp_v6_copy_addrlist(struct list_head
*addrlist
,
324 struct net_device
*dev
)
326 struct inet6_dev
*in6_dev
;
327 struct inet6_ifaddr
*ifp
;
328 struct sctp_sockaddr_entry
*addr
;
330 read_lock(&addrconf_lock
);
331 if ((in6_dev
= __in6_dev_get(dev
)) == NULL
) {
332 read_unlock(&addrconf_lock
);
336 read_lock(&in6_dev
->lock
);
337 for (ifp
= in6_dev
->addr_list
; ifp
; ifp
= ifp
->if_next
) {
338 /* Add the address to the local list. */
339 addr
= t_new(struct sctp_sockaddr_entry
, GFP_ATOMIC
);
341 addr
->a
.v6
.sin6_family
= AF_INET6
;
342 addr
->a
.v6
.sin6_port
= 0;
343 addr
->a
.v6
.sin6_addr
= ifp
->addr
;
344 addr
->a
.v6
.sin6_scope_id
= dev
->ifindex
;
345 INIT_LIST_HEAD(&addr
->list
);
346 list_add_tail(&addr
->list
, addrlist
);
350 read_unlock(&in6_dev
->lock
);
351 read_unlock(&addrconf_lock
);
354 /* Initialize a sockaddr_storage from in incoming skb. */
355 static void sctp_v6_from_skb(union sctp_addr
*addr
,struct sk_buff
*skb
,
362 port
= &addr
->v6
.sin6_port
;
363 addr
->v6
.sin6_family
= AF_INET6
;
364 addr
->v6
.sin6_flowinfo
= 0; /* FIXME */
365 addr
->v6
.sin6_scope_id
= ((struct inet6_skb_parm
*)skb
->cb
)->iif
;
367 sh
= (struct sctphdr
*) skb
->h
.raw
;
369 *port
= ntohs(sh
->source
);
370 from
= &skb
->nh
.ipv6h
->saddr
;
372 *port
= ntohs(sh
->dest
);
373 from
= &skb
->nh
.ipv6h
->daddr
;
375 ipv6_addr_copy(&addr
->v6
.sin6_addr
, from
);
378 /* Initialize an sctp_addr from a socket. */
379 static void sctp_v6_from_sk(union sctp_addr
*addr
, struct sock
*sk
)
381 addr
->v6
.sin6_family
= AF_INET6
;
382 addr
->v6
.sin6_port
= inet_sk(sk
)->num
;
383 addr
->v6
.sin6_addr
= inet6_sk(sk
)->rcv_saddr
;
386 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
387 static void sctp_v6_to_sk_saddr(union sctp_addr
*addr
, struct sock
*sk
)
389 if (addr
->sa
.sa_family
== AF_INET
&& sctp_sk(sk
)->v4mapped
) {
390 inet6_sk(sk
)->rcv_saddr
.s6_addr32
[0] = 0;
391 inet6_sk(sk
)->rcv_saddr
.s6_addr32
[1] = 0;
392 inet6_sk(sk
)->rcv_saddr
.s6_addr32
[2] = htonl(0x0000ffff);
393 inet6_sk(sk
)->rcv_saddr
.s6_addr32
[3] =
394 addr
->v4
.sin_addr
.s_addr
;
396 inet6_sk(sk
)->rcv_saddr
= addr
->v6
.sin6_addr
;
400 /* Initialize sk->sk_daddr from sctp_addr. */
401 static void sctp_v6_to_sk_daddr(union sctp_addr
*addr
, struct sock
*sk
)
403 if (addr
->sa
.sa_family
== AF_INET
&& sctp_sk(sk
)->v4mapped
) {
404 inet6_sk(sk
)->daddr
.s6_addr32
[0] = 0;
405 inet6_sk(sk
)->daddr
.s6_addr32
[1] = 0;
406 inet6_sk(sk
)->daddr
.s6_addr32
[2] = htonl(0x0000ffff);
407 inet6_sk(sk
)->daddr
.s6_addr32
[3] = addr
->v4
.sin_addr
.s_addr
;
409 inet6_sk(sk
)->daddr
= addr
->v6
.sin6_addr
;
413 /* Initialize a sctp_addr from an address parameter. */
414 static void sctp_v6_from_addr_param(union sctp_addr
*addr
,
415 union sctp_addr_param
*param
,
418 addr
->v6
.sin6_family
= AF_INET6
;
419 addr
->v6
.sin6_port
= port
;
420 addr
->v6
.sin6_flowinfo
= 0; /* BUG */
421 ipv6_addr_copy(&addr
->v6
.sin6_addr
, ¶m
->v6
.addr
);
422 addr
->v6
.sin6_scope_id
= iif
;
425 /* Initialize an address parameter from a sctp_addr and return the length
426 * of the address parameter.
428 static int sctp_v6_to_addr_param(const union sctp_addr
*addr
,
429 union sctp_addr_param
*param
)
431 int length
= sizeof(sctp_ipv6addr_param_t
);
433 param
->v6
.param_hdr
.type
= SCTP_PARAM_IPV6_ADDRESS
;
434 param
->v6
.param_hdr
.length
= ntohs(length
);
435 ipv6_addr_copy(¶m
->v6
.addr
, &addr
->v6
.sin6_addr
);
440 /* Initialize a sctp_addr from a dst_entry. */
441 static void sctp_v6_dst_saddr(union sctp_addr
*addr
, struct dst_entry
*dst
,
444 struct rt6_info
*rt
= (struct rt6_info
*)dst
;
445 addr
->sa
.sa_family
= AF_INET6
;
446 addr
->v6
.sin6_port
= port
;
447 ipv6_addr_copy(&addr
->v6
.sin6_addr
, &rt
->rt6i_src
.addr
);
450 /* Compare addresses exactly.
451 * v4-mapped-v6 is also in consideration.
453 static int sctp_v6_cmp_addr(const union sctp_addr
*addr1
,
454 const union sctp_addr
*addr2
)
456 if (addr1
->sa
.sa_family
!= addr2
->sa
.sa_family
) {
457 if (addr1
->sa
.sa_family
== AF_INET
&&
458 addr2
->sa
.sa_family
== AF_INET6
&&
459 IPV6_ADDR_MAPPED
== ipv6_addr_type(&addr2
->v6
.sin6_addr
)) {
460 if (addr2
->v6
.sin6_port
== addr1
->v4
.sin_port
&&
461 addr2
->v6
.sin6_addr
.s6_addr32
[3] ==
462 addr1
->v4
.sin_addr
.s_addr
)
465 if (addr2
->sa
.sa_family
== AF_INET
&&
466 addr1
->sa
.sa_family
== AF_INET6
&&
467 IPV6_ADDR_MAPPED
== ipv6_addr_type(&addr1
->v6
.sin6_addr
)) {
468 if (addr1
->v6
.sin6_port
== addr2
->v4
.sin_port
&&
469 addr1
->v6
.sin6_addr
.s6_addr32
[3] ==
470 addr2
->v4
.sin_addr
.s_addr
)
475 if (!ipv6_addr_equal(&addr1
->v6
.sin6_addr
, &addr2
->v6
.sin6_addr
))
477 /* If this is a linklocal address, compare the scope_id. */
478 if (ipv6_addr_type(&addr1
->v6
.sin6_addr
) & IPV6_ADDR_LINKLOCAL
) {
479 if (addr1
->v6
.sin6_scope_id
&& addr2
->v6
.sin6_scope_id
&&
480 (addr1
->v6
.sin6_scope_id
!= addr2
->v6
.sin6_scope_id
)) {
488 /* Initialize addr struct to INADDR_ANY. */
489 static void sctp_v6_inaddr_any(union sctp_addr
*addr
, unsigned short port
)
491 memset(addr
, 0x00, sizeof(union sctp_addr
));
492 addr
->v6
.sin6_family
= AF_INET6
;
493 addr
->v6
.sin6_port
= port
;
496 /* Is this a wildcard address? */
497 static int sctp_v6_is_any(const union sctp_addr
*addr
)
499 return ipv6_addr_any(&addr
->v6
.sin6_addr
);
502 /* Should this be available for binding? */
503 static int sctp_v6_available(union sctp_addr
*addr
, struct sctp_sock
*sp
)
506 struct in6_addr
*in6
= (struct in6_addr
*)&addr
->v6
.sin6_addr
;
508 type
= ipv6_addr_type(in6
);
509 if (IPV6_ADDR_ANY
== type
)
511 if (type
== IPV6_ADDR_MAPPED
) {
512 if (sp
&& !sp
->v4mapped
)
514 if (sp
&& ipv6_only_sock(sctp_opt2sk(sp
)))
516 sctp_v6_map_v4(addr
);
517 return sctp_get_af_specific(AF_INET
)->available(addr
, sp
);
519 if (!(type
& IPV6_ADDR_UNICAST
))
522 return ipv6_chk_addr(in6
, NULL
, 0);
525 /* This function checks if the address is a valid address to be used for
529 * Return 0 - If the address is a non-unicast or an illegal address.
530 * Return 1 - If the address is a unicast.
532 static int sctp_v6_addr_valid(union sctp_addr
*addr
, struct sctp_sock
*sp
)
534 int ret
= ipv6_addr_type(&addr
->v6
.sin6_addr
);
536 /* Support v4-mapped-v6 address. */
537 if (ret
== IPV6_ADDR_MAPPED
) {
538 /* Note: This routine is used in input, so v4-mapped-v6
539 * are disallowed here when there is no sctp_sock.
541 if (!sp
|| !sp
->v4mapped
)
543 if (sp
&& ipv6_only_sock(sctp_opt2sk(sp
)))
545 sctp_v6_map_v4(addr
);
546 return sctp_get_af_specific(AF_INET
)->addr_valid(addr
, sp
);
549 /* Is this a non-unicast address */
550 if (!(ret
& IPV6_ADDR_UNICAST
))
556 /* What is the scope of 'addr'? */
557 static sctp_scope_t
sctp_v6_scope(union sctp_addr
*addr
)
562 /* The IPv6 scope is really a set of bit fields.
563 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
566 v6scope
= ipv6_addr_scope(&addr
->v6
.sin6_addr
);
569 retval
= SCTP_SCOPE_LOOPBACK
;
572 retval
= SCTP_SCOPE_LINK
;
575 retval
= SCTP_SCOPE_PRIVATE
;
578 retval
= SCTP_SCOPE_GLOBAL
;
585 /* Create and initialize a new sk for the socket to be returned by accept(). */
586 static struct sock
*sctp_v6_create_accept_sk(struct sock
*sk
,
587 struct sctp_association
*asoc
)
589 struct inet_sock
*inet
= inet_sk(sk
);
591 struct inet_sock
*newinet
;
592 struct ipv6_pinfo
*newnp
, *np
= inet6_sk(sk
);
593 struct sctp6_sock
*newsctp6sk
;
595 newsk
= sk_alloc(PF_INET6
, GFP_KERNEL
, sk
->sk_prot
, 1);
599 sock_init_data(NULL
, newsk
);
601 newsk
->sk_type
= SOCK_STREAM
;
603 newsk
->sk_prot
= sk
->sk_prot
;
604 newsk
->sk_no_check
= sk
->sk_no_check
;
605 newsk
->sk_reuse
= sk
->sk_reuse
;
607 newsk
->sk_destruct
= inet_sock_destruct
;
608 newsk
->sk_family
= PF_INET6
;
609 newsk
->sk_protocol
= IPPROTO_SCTP
;
610 newsk
->sk_backlog_rcv
= sk
->sk_prot
->backlog_rcv
;
611 newsk
->sk_shutdown
= sk
->sk_shutdown
;
612 sock_reset_flag(sk
, SOCK_ZAPPED
);
614 newsctp6sk
= (struct sctp6_sock
*)newsk
;
615 inet_sk(newsk
)->pinet6
= &newsctp6sk
->inet6
;
617 newinet
= inet_sk(newsk
);
618 newnp
= inet6_sk(newsk
);
620 memcpy(newnp
, np
, sizeof(struct ipv6_pinfo
));
622 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
625 newinet
->sport
= inet
->sport
;
626 newnp
->saddr
= np
->saddr
;
627 newnp
->rcv_saddr
= np
->rcv_saddr
;
628 newinet
->dport
= htons(asoc
->peer
.port
);
629 sctp_v6_to_sk_daddr(&asoc
->peer
.primary_addr
, newsk
);
631 /* Init the ipv4 part of the socket since we can have sockets
632 * using v6 API for ipv4.
634 newinet
->uc_ttl
= -1;
635 newinet
->mc_loop
= 1;
637 newinet
->mc_index
= 0;
638 newinet
->mc_list
= NULL
;
640 if (ipv4_config
.no_pmtu_disc
)
641 newinet
->pmtudisc
= IP_PMTUDISC_DONT
;
643 newinet
->pmtudisc
= IP_PMTUDISC_WANT
;
645 #ifdef INET_REFCNT_DEBUG
646 atomic_inc(&inet6_sock_nr
);
647 atomic_inc(&inet_sock_nr
);
650 if (newsk
->sk_prot
->init(newsk
)) {
651 sk_common_release(newsk
);
659 /* Map v4 address to mapped v6 address */
660 static void sctp_v6_addr_v4map(struct sctp_sock
*sp
, union sctp_addr
*addr
)
662 if (sp
->v4mapped
&& AF_INET
== addr
->sa
.sa_family
)
663 sctp_v4_map_v6(addr
);
666 /* Where did this skb come from? */
667 static int sctp_v6_skb_iif(const struct sk_buff
*skb
)
669 struct inet6_skb_parm
*opt
= (struct inet6_skb_parm
*) skb
->cb
;
673 /* Was this packet marked by Explicit Congestion Notification? */
674 static int sctp_v6_is_ce(const struct sk_buff
*skb
)
676 return *((__u32
*)(skb
->nh
.ipv6h
)) & htonl(1<<20);
679 /* Dump the v6 addr to the seq file. */
680 static void sctp_v6_seq_dump_addr(struct seq_file
*seq
, union sctp_addr
*addr
)
682 seq_printf(seq
, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
683 NIP6(addr
->v6
.sin6_addr
));
686 /* Initialize a PF_INET6 socket msg_name. */
687 static void sctp_inet6_msgname(char *msgname
, int *addr_len
)
689 struct sockaddr_in6
*sin6
;
691 sin6
= (struct sockaddr_in6
*)msgname
;
692 sin6
->sin6_family
= AF_INET6
;
693 sin6
->sin6_flowinfo
= 0;
694 sin6
->sin6_scope_id
= 0; /*FIXME */
695 *addr_len
= sizeof(struct sockaddr_in6
);
698 /* Initialize a PF_INET msgname from a ulpevent. */
699 static void sctp_inet6_event_msgname(struct sctp_ulpevent
*event
,
700 char *msgname
, int *addrlen
)
702 struct sockaddr_in6
*sin6
, *sin6from
;
705 union sctp_addr
*addr
;
706 struct sctp_association
*asoc
;
709 sctp_inet6_msgname(msgname
, addrlen
);
710 sin6
= (struct sockaddr_in6
*)msgname
;
711 sin6
->sin6_port
= htons(asoc
->peer
.port
);
712 addr
= &asoc
->peer
.primary_addr
;
714 /* Note: If we go to a common v6 format, this code
718 /* Map ipv4 address into v4-mapped-on-v6 address. */
719 if (sctp_sk(asoc
->base
.sk
)->v4mapped
&&
720 AF_INET
== addr
->sa
.sa_family
) {
721 sctp_v4_map_v6((union sctp_addr
*)sin6
);
722 sin6
->sin6_addr
.s6_addr32
[3] =
723 addr
->v4
.sin_addr
.s_addr
;
727 sin6from
= &asoc
->peer
.primary_addr
.v6
;
728 ipv6_addr_copy(&sin6
->sin6_addr
, &sin6from
->sin6_addr
);
729 if (ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
)
730 sin6
->sin6_scope_id
= sin6from
->sin6_scope_id
;
734 /* Initialize a msg_name from an inbound skb. */
735 static void sctp_inet6_skb_msgname(struct sk_buff
*skb
, char *msgname
,
739 struct sockaddr_in6
*sin6
;
742 sctp_inet6_msgname(msgname
, addr_len
);
743 sin6
= (struct sockaddr_in6
*)msgname
;
744 sh
= (struct sctphdr
*)skb
->h
.raw
;
745 sin6
->sin6_port
= sh
->source
;
747 /* Map ipv4 address into v4-mapped-on-v6 address. */
748 if (sctp_sk(skb
->sk
)->v4mapped
&&
749 skb
->nh
.iph
->version
== 4) {
750 sctp_v4_map_v6((union sctp_addr
*)sin6
);
751 sin6
->sin6_addr
.s6_addr32
[3] = skb
->nh
.iph
->saddr
;
755 /* Otherwise, just copy the v6 address. */
756 ipv6_addr_copy(&sin6
->sin6_addr
, &skb
->nh
.ipv6h
->saddr
);
757 if (ipv6_addr_type(&sin6
->sin6_addr
) & IPV6_ADDR_LINKLOCAL
) {
758 struct sctp_ulpevent
*ev
= sctp_skb2event(skb
);
759 sin6
->sin6_scope_id
= ev
->iif
;
764 /* Do we support this AF? */
765 static int sctp_inet6_af_supported(sa_family_t family
, struct sctp_sock
*sp
)
770 /* v4-mapped-v6 addresses */
772 if (!__ipv6_only_sock(sctp_opt2sk(sp
)) && sp
->v4mapped
)
779 /* Address matching with wildcards allowed. This extra level
780 * of indirection lets us choose whether a PF_INET6 should
781 * disallow any v4 addresses if we so choose.
783 static int sctp_inet6_cmp_addr(const union sctp_addr
*addr1
,
784 const union sctp_addr
*addr2
,
785 struct sctp_sock
*opt
)
787 struct sctp_af
*af1
, *af2
;
789 af1
= sctp_get_af_specific(addr1
->sa
.sa_family
);
790 af2
= sctp_get_af_specific(addr2
->sa
.sa_family
);
794 /* Today, wildcard AF_INET/AF_INET6. */
795 if (sctp_is_any(addr1
) || sctp_is_any(addr2
))
798 if (addr1
->sa
.sa_family
!= addr2
->sa
.sa_family
)
801 return af1
->cmp_addr(addr1
, addr2
);
804 /* Verify that the provided sockaddr looks bindable. Common verification,
805 * has already been taken care of.
807 static int sctp_inet6_bind_verify(struct sctp_sock
*opt
, union sctp_addr
*addr
)
811 /* ASSERT: address family has already been verified. */
812 if (addr
->sa
.sa_family
!= AF_INET6
)
813 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
816 int type
= ipv6_addr_type(&addr
->v6
.sin6_addr
);
817 sk
= sctp_opt2sk(opt
);
818 if (type
& IPV6_ADDR_LINKLOCAL
) {
819 /* Note: Behavior similar to af_inet6.c:
820 * 1) Overrides previous bound_dev_if
821 * 2) Destructive even if bind isn't successful.
824 if (addr
->v6
.sin6_scope_id
)
825 sk
->sk_bound_dev_if
= addr
->v6
.sin6_scope_id
;
826 if (!sk
->sk_bound_dev_if
)
831 return af
->available(addr
, opt
);
834 /* Verify that the provided sockaddr looks bindable. Common verification,
835 * has already been taken care of.
837 static int sctp_inet6_send_verify(struct sctp_sock
*opt
, union sctp_addr
*addr
)
839 struct sctp_af
*af
= NULL
;
841 /* ASSERT: address family has already been verified. */
842 if (addr
->sa
.sa_family
!= AF_INET6
)
843 af
= sctp_get_af_specific(addr
->sa
.sa_family
);
846 int type
= ipv6_addr_type(&addr
->v6
.sin6_addr
);
847 sk
= sctp_opt2sk(opt
);
848 if (type
& IPV6_ADDR_LINKLOCAL
) {
849 /* Note: Behavior similar to af_inet6.c:
850 * 1) Overrides previous bound_dev_if
851 * 2) Destructive even if bind isn't successful.
854 if (addr
->v6
.sin6_scope_id
)
855 sk
->sk_bound_dev_if
= addr
->v6
.sin6_scope_id
;
856 if (!sk
->sk_bound_dev_if
)
865 /* Fill in Supported Address Type information for INIT and INIT-ACK
866 * chunks. Note: In the future, we may want to look at sock options
867 * to determine whether a PF_INET6 socket really wants to have IPV4
869 * Returns number of addresses supported.
871 static int sctp_inet6_supported_addrs(const struct sctp_sock
*opt
,
874 types
[0] = SCTP_PARAM_IPV4_ADDRESS
;
875 types
[1] = SCTP_PARAM_IPV6_ADDRESS
;
879 static struct proto_ops inet6_seqpacket_ops
= {
881 .owner
= THIS_MODULE
,
882 .release
= inet6_release
,
884 .connect
= inet_dgram_connect
,
885 .socketpair
= sock_no_socketpair
,
886 .accept
= inet_accept
,
887 .getname
= inet6_getname
,
889 .ioctl
= inet6_ioctl
,
890 .listen
= sctp_inet_listen
,
891 .shutdown
= inet_shutdown
,
892 .setsockopt
= sock_common_setsockopt
,
893 .getsockopt
= sock_common_getsockopt
,
894 .sendmsg
= inet_sendmsg
,
895 .recvmsg
= sock_common_recvmsg
,
896 .mmap
= sock_no_mmap
,
899 static struct inet_protosw sctpv6_seqpacket_protosw
= {
900 .type
= SOCK_SEQPACKET
,
901 .protocol
= IPPROTO_SCTP
,
902 .prot
= &sctpv6_prot
,
903 .ops
= &inet6_seqpacket_ops
,
906 .flags
= SCTP_PROTOSW_FLAG
908 static struct inet_protosw sctpv6_stream_protosw
= {
910 .protocol
= IPPROTO_SCTP
,
911 .prot
= &sctpv6_prot
,
912 .ops
= &inet6_seqpacket_ops
,
915 .flags
= SCTP_PROTOSW_FLAG
,
918 static int sctp6_rcv(struct sk_buff
**pskb
, unsigned int *nhoffp
)
920 return sctp_rcv(*pskb
) ? -1 : 0;
923 static struct inet6_protocol sctpv6_protocol
= {
924 .handler
= sctp6_rcv
,
925 .err_handler
= sctp_v6_err
,
926 .flags
= INET6_PROTO_NOPOLICY
| INET6_PROTO_FINAL
,
929 static struct sctp_af sctp_ipv6_specific
= {
930 .sctp_xmit
= sctp_v6_xmit
,
931 .setsockopt
= ipv6_setsockopt
,
932 .getsockopt
= ipv6_getsockopt
,
933 .get_dst
= sctp_v6_get_dst
,
934 .get_saddr
= sctp_v6_get_saddr
,
935 .copy_addrlist
= sctp_v6_copy_addrlist
,
936 .from_skb
= sctp_v6_from_skb
,
937 .from_sk
= sctp_v6_from_sk
,
938 .to_sk_saddr
= sctp_v6_to_sk_saddr
,
939 .to_sk_daddr
= sctp_v6_to_sk_daddr
,
940 .from_addr_param
= sctp_v6_from_addr_param
,
941 .to_addr_param
= sctp_v6_to_addr_param
,
942 .dst_saddr
= sctp_v6_dst_saddr
,
943 .cmp_addr
= sctp_v6_cmp_addr
,
944 .scope
= sctp_v6_scope
,
945 .addr_valid
= sctp_v6_addr_valid
,
946 .inaddr_any
= sctp_v6_inaddr_any
,
947 .is_any
= sctp_v6_is_any
,
948 .available
= sctp_v6_available
,
949 .skb_iif
= sctp_v6_skb_iif
,
950 .is_ce
= sctp_v6_is_ce
,
951 .seq_dump_addr
= sctp_v6_seq_dump_addr
,
952 .net_header_len
= sizeof(struct ipv6hdr
),
953 .sockaddr_len
= sizeof(struct sockaddr_in6
),
954 .sa_family
= AF_INET6
,
957 static struct sctp_pf sctp_pf_inet6_specific
= {
958 .event_msgname
= sctp_inet6_event_msgname
,
959 .skb_msgname
= sctp_inet6_skb_msgname
,
960 .af_supported
= sctp_inet6_af_supported
,
961 .cmp_addr
= sctp_inet6_cmp_addr
,
962 .bind_verify
= sctp_inet6_bind_verify
,
963 .send_verify
= sctp_inet6_send_verify
,
964 .supported_addrs
= sctp_inet6_supported_addrs
,
965 .create_accept_sk
= sctp_v6_create_accept_sk
,
966 .addr_v4map
= sctp_v6_addr_v4map
,
967 .af
= &sctp_ipv6_specific
,
970 /* Initialize IPv6 support and register with inet6 stack. */
971 int sctp_v6_init(void)
973 int rc
= proto_register(&sctpv6_prot
, 1);
977 /* Register inet6 protocol. */
979 if (inet6_add_protocol(&sctpv6_protocol
, IPPROTO_SCTP
) < 0)
980 goto out_unregister_sctp_proto
;
982 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
983 inet6_register_protosw(&sctpv6_seqpacket_protosw
);
984 inet6_register_protosw(&sctpv6_stream_protosw
);
986 /* Register the SCTP specific PF_INET6 functions. */
987 sctp_register_pf(&sctp_pf_inet6_specific
, PF_INET6
);
989 /* Register the SCTP specific AF_INET6 functions. */
990 sctp_register_af(&sctp_ipv6_specific
);
992 /* Register notifier for inet6 address additions/deletions. */
993 register_inet6addr_notifier(&sctp_inet6addr_notifier
);
997 out_unregister_sctp_proto
:
998 proto_unregister(&sctpv6_prot
);
1002 /* IPv6 specific exit support. */
1003 void sctp_v6_exit(void)
1005 list_del(&sctp_ipv6_specific
.list
);
1006 inet6_del_protocol(&sctpv6_protocol
, IPPROTO_SCTP
);
1007 inet6_unregister_protosw(&sctpv6_seqpacket_protosw
);
1008 inet6_unregister_protosw(&sctpv6_stream_protosw
);
1009 unregister_inet6addr_notifier(&sctp_inet6addr_notifier
);
1010 proto_unregister(&sctpv6_prot
);