Linux 2.6.14.3
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / net / sctp / ipv6.c
blobfa3be2b8fb5ffcaa88ac19e5919f7703461f90ef
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/sched.h>
57 #include <linux/in.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>
69 #include <net/ndisc.h>
70 #include <net/ip.h>
71 #include <net/ipv6.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);
93 struct sock *sk;
94 struct sctp_association *asoc;
95 struct sctp_transport *transport;
96 struct ipv6_pinfo *np;
97 char *saveip, *savesctp;
98 int err;
100 idev = in6_dev_get(skb->dev);
102 /* Fix up skb to look at the embedded net header. */
103 saveip = skb->nh.raw;
104 savesctp = skb->h.raw;
105 skb->nh.ipv6h = iph;
106 skb->h.raw = (char *)sh;
107 sk = sctp_err_lookup(AF_INET6, skb, sh, &asoc, &transport);
108 /* Put back, the original pointers. */
109 skb->nh.raw = saveip;
110 skb->h.raw = savesctp;
111 if (!sk) {
112 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
113 goto out;
116 /* Warning: The sock lock is held. Remember to call
117 * sctp_err_finish!
120 switch (type) {
121 case ICMPV6_PKT_TOOBIG:
122 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
123 goto out_unlock;
124 case ICMPV6_PARAMPROB:
125 if (ICMPV6_UNK_NEXTHDR == code) {
126 sctp_icmp_proto_unreachable(sk, asoc, transport);
127 goto out_unlock;
129 break;
130 default:
131 break;
134 np = inet6_sk(sk);
135 icmpv6_err_convert(type, code, &err);
136 if (!sock_owned_by_user(sk) && np->recverr) {
137 sk->sk_err = err;
138 sk->sk_error_report(sk);
139 } else { /* Only an error on timeout */
140 sk->sk_err_soft = err;
143 out_unlock:
144 sctp_err_finish(sk, asoc);
145 out:
146 if (likely(idev != NULL))
147 in6_dev_put(idev);
150 /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
151 static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
152 int ipfragok)
154 struct sock *sk = skb->sk;
155 struct ipv6_pinfo *np = inet6_sk(sk);
156 struct flowi fl;
158 memset(&fl, 0, sizeof(fl));
160 fl.proto = sk->sk_protocol;
162 /* Fill in the dest address from the route entry passed with the skb
163 * and the source address from the transport.
165 ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
166 ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
168 fl.fl6_flowlabel = np->flow_label;
169 IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
170 if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
171 fl.oif = transport->saddr.v6.sin6_scope_id;
172 else
173 fl.oif = sk->sk_bound_dev_if;
174 fl.fl_ip_sport = inet_sk(sk)->sport;
175 fl.fl_ip_dport = transport->ipaddr.v6.sin6_port;
177 if (np->opt && np->opt->srcrt) {
178 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
179 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
182 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
183 "src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
184 "dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
185 __FUNCTION__, skb, skb->len,
186 NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
188 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
190 return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
193 /* Returns the dst cache entry for the given source and destination ip
194 * addresses.
196 static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
197 union sctp_addr *daddr,
198 union sctp_addr *saddr)
200 struct dst_entry *dst;
201 struct flowi fl;
203 memset(&fl, 0, sizeof(fl));
204 ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
205 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
206 fl.oif = daddr->v6.sin6_scope_id;
209 SCTP_DEBUG_PRINTK("%s: DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
210 __FUNCTION__, NIP6(fl.fl6_dst));
212 if (saddr) {
213 ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
214 SCTP_DEBUG_PRINTK(
215 "SRC=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x - ",
216 NIP6(fl.fl6_src));
219 dst = ip6_route_output(NULL, &fl);
220 if (dst) {
221 struct rt6_info *rt;
222 rt = (struct rt6_info *)dst;
223 SCTP_DEBUG_PRINTK(
224 "rt6_dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
225 "rt6_src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
226 NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
227 } else {
228 SCTP_DEBUG_PRINTK("NO ROUTE\n");
231 return dst;
234 /* Returns the number of consecutive initial bits that match in the 2 ipv6
235 * addresses.
237 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
238 union sctp_addr *s2)
240 struct in6_addr *a1 = &s1->v6.sin6_addr;
241 struct in6_addr *a2 = &s2->v6.sin6_addr;
242 int i, j;
244 for (i = 0; i < 4 ; i++) {
245 __u32 a1xora2;
247 a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
249 if ((j = fls(ntohl(a1xora2))))
250 return (i * 32 + 32 - j);
253 return (i*32);
256 /* Fills in the source address(saddr) based on the destination address(daddr)
257 * and asoc's bind address list.
259 static void sctp_v6_get_saddr(struct sctp_association *asoc,
260 struct dst_entry *dst,
261 union sctp_addr *daddr,
262 union sctp_addr *saddr)
264 struct sctp_bind_addr *bp;
265 rwlock_t *addr_lock;
266 struct sctp_sockaddr_entry *laddr;
267 struct list_head *pos;
268 sctp_scope_t scope;
269 union sctp_addr *baddr = NULL;
270 __u8 matchlen = 0;
271 __u8 bmatchlen;
273 SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
274 "daddr:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
275 __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
277 if (!asoc) {
278 ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
279 SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: "
280 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
281 NIP6(saddr->v6.sin6_addr));
282 return;
285 scope = sctp_scope(daddr);
287 bp = &asoc->base.bind_addr;
288 addr_lock = &asoc->base.addr_lock;
290 /* Go through the bind address list and find the best source address
291 * that matches the scope of the destination address.
293 sctp_read_lock(addr_lock);
294 list_for_each(pos, &bp->address_list) {
295 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
296 if ((laddr->a.sa.sa_family == AF_INET6) &&
297 (scope <= sctp_scope(&laddr->a))) {
298 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
299 if (!baddr || (matchlen < bmatchlen)) {
300 baddr = &laddr->a;
301 matchlen = bmatchlen;
306 if (baddr) {
307 memcpy(saddr, baddr, sizeof(union sctp_addr));
308 SCTP_DEBUG_PRINTK("saddr: "
309 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
310 NIP6(saddr->v6.sin6_addr));
311 } else {
312 printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
313 "address for the "
314 "dest:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
315 __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
318 sctp_read_unlock(addr_lock);
321 /* Make a copy of all potential local addresses. */
322 static void sctp_v6_copy_addrlist(struct list_head *addrlist,
323 struct net_device *dev)
325 struct inet6_dev *in6_dev;
326 struct inet6_ifaddr *ifp;
327 struct sctp_sockaddr_entry *addr;
329 read_lock(&addrconf_lock);
330 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
331 read_unlock(&addrconf_lock);
332 return;
335 read_lock(&in6_dev->lock);
336 for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
337 /* Add the address to the local list. */
338 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
339 if (addr) {
340 addr->a.v6.sin6_family = AF_INET6;
341 addr->a.v6.sin6_port = 0;
342 addr->a.v6.sin6_addr = ifp->addr;
343 addr->a.v6.sin6_scope_id = dev->ifindex;
344 INIT_LIST_HEAD(&addr->list);
345 list_add_tail(&addr->list, addrlist);
349 read_unlock(&in6_dev->lock);
350 read_unlock(&addrconf_lock);
353 /* Initialize a sockaddr_storage from in incoming skb. */
354 static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
355 int is_saddr)
357 void *from;
358 __u16 *port;
359 struct sctphdr *sh;
361 port = &addr->v6.sin6_port;
362 addr->v6.sin6_family = AF_INET6;
363 addr->v6.sin6_flowinfo = 0; /* FIXME */
364 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
366 sh = (struct sctphdr *) skb->h.raw;
367 if (is_saddr) {
368 *port = ntohs(sh->source);
369 from = &skb->nh.ipv6h->saddr;
370 } else {
371 *port = ntohs(sh->dest);
372 from = &skb->nh.ipv6h->daddr;
374 ipv6_addr_copy(&addr->v6.sin6_addr, from);
377 /* Initialize an sctp_addr from a socket. */
378 static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
380 addr->v6.sin6_family = AF_INET6;
381 addr->v6.sin6_port = inet_sk(sk)->num;
382 addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
385 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
386 static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
388 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
389 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
390 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
391 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
392 inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
393 addr->v4.sin_addr.s_addr;
394 } else {
395 inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
399 /* Initialize sk->sk_daddr from sctp_addr. */
400 static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
402 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
403 inet6_sk(sk)->daddr.s6_addr32[0] = 0;
404 inet6_sk(sk)->daddr.s6_addr32[1] = 0;
405 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
406 inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
407 } else {
408 inet6_sk(sk)->daddr = addr->v6.sin6_addr;
412 /* Initialize a sctp_addr from an address parameter. */
413 static void sctp_v6_from_addr_param(union sctp_addr *addr,
414 union sctp_addr_param *param,
415 __u16 port, int iif)
417 addr->v6.sin6_family = AF_INET6;
418 addr->v6.sin6_port = port;
419 addr->v6.sin6_flowinfo = 0; /* BUG */
420 ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
421 addr->v6.sin6_scope_id = iif;
424 /* Initialize an address parameter from a sctp_addr and return the length
425 * of the address parameter.
427 static int sctp_v6_to_addr_param(const union sctp_addr *addr,
428 union sctp_addr_param *param)
430 int length = sizeof(sctp_ipv6addr_param_t);
432 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
433 param->v6.param_hdr.length = ntohs(length);
434 ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
436 return length;
439 /* Initialize a sctp_addr from a dst_entry. */
440 static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
441 unsigned short port)
443 struct rt6_info *rt = (struct rt6_info *)dst;
444 addr->sa.sa_family = AF_INET6;
445 addr->v6.sin6_port = port;
446 ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
449 /* Compare addresses exactly.
450 * v4-mapped-v6 is also in consideration.
452 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
453 const union sctp_addr *addr2)
455 if (addr1->sa.sa_family != addr2->sa.sa_family) {
456 if (addr1->sa.sa_family == AF_INET &&
457 addr2->sa.sa_family == AF_INET6 &&
458 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr2->v6.sin6_addr)) {
459 if (addr2->v6.sin6_port == addr1->v4.sin_port &&
460 addr2->v6.sin6_addr.s6_addr32[3] ==
461 addr1->v4.sin_addr.s_addr)
462 return 1;
464 if (addr2->sa.sa_family == AF_INET &&
465 addr1->sa.sa_family == AF_INET6 &&
466 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr1->v6.sin6_addr)) {
467 if (addr1->v6.sin6_port == addr2->v4.sin_port &&
468 addr1->v6.sin6_addr.s6_addr32[3] ==
469 addr2->v4.sin_addr.s_addr)
470 return 1;
472 return 0;
474 if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
475 return 0;
476 /* If this is a linklocal address, compare the scope_id. */
477 if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
478 if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
479 (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
480 return 0;
484 return 1;
487 /* Initialize addr struct to INADDR_ANY. */
488 static void sctp_v6_inaddr_any(union sctp_addr *addr, unsigned short port)
490 memset(addr, 0x00, sizeof(union sctp_addr));
491 addr->v6.sin6_family = AF_INET6;
492 addr->v6.sin6_port = port;
495 /* Is this a wildcard address? */
496 static int sctp_v6_is_any(const union sctp_addr *addr)
498 return ipv6_addr_any(&addr->v6.sin6_addr);
501 /* Should this be available for binding? */
502 static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
504 int type;
505 struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
507 type = ipv6_addr_type(in6);
508 if (IPV6_ADDR_ANY == type)
509 return 1;
510 if (type == IPV6_ADDR_MAPPED) {
511 if (sp && !sp->v4mapped)
512 return 0;
513 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
514 return 0;
515 sctp_v6_map_v4(addr);
516 return sctp_get_af_specific(AF_INET)->available(addr, sp);
518 if (!(type & IPV6_ADDR_UNICAST))
519 return 0;
521 return ipv6_chk_addr(in6, NULL, 0);
524 /* This function checks if the address is a valid address to be used for
525 * SCTP.
527 * Output:
528 * Return 0 - If the address is a non-unicast or an illegal address.
529 * Return 1 - If the address is a unicast.
531 static int sctp_v6_addr_valid(union sctp_addr *addr, struct sctp_sock *sp)
533 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
535 /* Support v4-mapped-v6 address. */
536 if (ret == IPV6_ADDR_MAPPED) {
537 /* Note: This routine is used in input, so v4-mapped-v6
538 * are disallowed here when there is no sctp_sock.
540 if (!sp || !sp->v4mapped)
541 return 0;
542 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
543 return 0;
544 sctp_v6_map_v4(addr);
545 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp);
548 /* Is this a non-unicast address */
549 if (!(ret & IPV6_ADDR_UNICAST))
550 return 0;
552 return 1;
555 /* What is the scope of 'addr'? */
556 static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
558 int v6scope;
559 sctp_scope_t retval;
561 /* The IPv6 scope is really a set of bit fields.
562 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
565 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
566 switch (v6scope) {
567 case IFA_HOST:
568 retval = SCTP_SCOPE_LOOPBACK;
569 break;
570 case IFA_LINK:
571 retval = SCTP_SCOPE_LINK;
572 break;
573 case IFA_SITE:
574 retval = SCTP_SCOPE_PRIVATE;
575 break;
576 default:
577 retval = SCTP_SCOPE_GLOBAL;
578 break;
581 return retval;
584 /* Create and initialize a new sk for the socket to be returned by accept(). */
585 static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
586 struct sctp_association *asoc)
588 struct inet_sock *inet = inet_sk(sk);
589 struct sock *newsk;
590 struct inet_sock *newinet;
591 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
592 struct sctp6_sock *newsctp6sk;
594 newsk = sk_alloc(PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
595 if (!newsk)
596 goto out;
598 sock_init_data(NULL, newsk);
600 newsk->sk_type = SOCK_STREAM;
602 newsk->sk_prot = sk->sk_prot;
603 newsk->sk_no_check = sk->sk_no_check;
604 newsk->sk_reuse = sk->sk_reuse;
606 newsk->sk_destruct = inet_sock_destruct;
607 newsk->sk_family = PF_INET6;
608 newsk->sk_protocol = IPPROTO_SCTP;
609 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
610 newsk->sk_shutdown = sk->sk_shutdown;
611 sock_reset_flag(sk, SOCK_ZAPPED);
613 newsctp6sk = (struct sctp6_sock *)newsk;
614 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
616 newinet = inet_sk(newsk);
617 newnp = inet6_sk(newsk);
619 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
621 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
622 * and getpeername().
624 newinet->sport = inet->sport;
625 newnp->saddr = np->saddr;
626 newnp->rcv_saddr = np->rcv_saddr;
627 newinet->dport = htons(asoc->peer.port);
628 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
630 /* Init the ipv4 part of the socket since we can have sockets
631 * using v6 API for ipv4.
633 newinet->uc_ttl = -1;
634 newinet->mc_loop = 1;
635 newinet->mc_ttl = 1;
636 newinet->mc_index = 0;
637 newinet->mc_list = NULL;
639 if (ipv4_config.no_pmtu_disc)
640 newinet->pmtudisc = IP_PMTUDISC_DONT;
641 else
642 newinet->pmtudisc = IP_PMTUDISC_WANT;
644 sk_refcnt_debug_inc(newsk);
646 if (newsk->sk_prot->init(newsk)) {
647 sk_common_release(newsk);
648 newsk = NULL;
651 out:
652 return newsk;
655 /* Map v4 address to mapped v6 address */
656 static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
658 if (sp->v4mapped && AF_INET == addr->sa.sa_family)
659 sctp_v4_map_v6(addr);
662 /* Where did this skb come from? */
663 static int sctp_v6_skb_iif(const struct sk_buff *skb)
665 struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
666 return opt->iif;
669 /* Was this packet marked by Explicit Congestion Notification? */
670 static int sctp_v6_is_ce(const struct sk_buff *skb)
672 return *((__u32 *)(skb->nh.ipv6h)) & htonl(1<<20);
675 /* Dump the v6 addr to the seq file. */
676 static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
678 seq_printf(seq, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
679 NIP6(addr->v6.sin6_addr));
682 /* Initialize a PF_INET6 socket msg_name. */
683 static void sctp_inet6_msgname(char *msgname, int *addr_len)
685 struct sockaddr_in6 *sin6;
687 sin6 = (struct sockaddr_in6 *)msgname;
688 sin6->sin6_family = AF_INET6;
689 sin6->sin6_flowinfo = 0;
690 sin6->sin6_scope_id = 0; /*FIXME */
691 *addr_len = sizeof(struct sockaddr_in6);
694 /* Initialize a PF_INET msgname from a ulpevent. */
695 static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
696 char *msgname, int *addrlen)
698 struct sockaddr_in6 *sin6, *sin6from;
700 if (msgname) {
701 union sctp_addr *addr;
702 struct sctp_association *asoc;
704 asoc = event->asoc;
705 sctp_inet6_msgname(msgname, addrlen);
706 sin6 = (struct sockaddr_in6 *)msgname;
707 sin6->sin6_port = htons(asoc->peer.port);
708 addr = &asoc->peer.primary_addr;
710 /* Note: If we go to a common v6 format, this code
711 * will change.
714 /* Map ipv4 address into v4-mapped-on-v6 address. */
715 if (sctp_sk(asoc->base.sk)->v4mapped &&
716 AF_INET == addr->sa.sa_family) {
717 sctp_v4_map_v6((union sctp_addr *)sin6);
718 sin6->sin6_addr.s6_addr32[3] =
719 addr->v4.sin_addr.s_addr;
720 return;
723 sin6from = &asoc->peer.primary_addr.v6;
724 ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
725 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
726 sin6->sin6_scope_id = sin6from->sin6_scope_id;
730 /* Initialize a msg_name from an inbound skb. */
731 static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
732 int *addr_len)
734 struct sctphdr *sh;
735 struct sockaddr_in6 *sin6;
737 if (msgname) {
738 sctp_inet6_msgname(msgname, addr_len);
739 sin6 = (struct sockaddr_in6 *)msgname;
740 sh = (struct sctphdr *)skb->h.raw;
741 sin6->sin6_port = sh->source;
743 /* Map ipv4 address into v4-mapped-on-v6 address. */
744 if (sctp_sk(skb->sk)->v4mapped &&
745 skb->nh.iph->version == 4) {
746 sctp_v4_map_v6((union sctp_addr *)sin6);
747 sin6->sin6_addr.s6_addr32[3] = skb->nh.iph->saddr;
748 return;
751 /* Otherwise, just copy the v6 address. */
752 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
753 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
754 struct sctp_ulpevent *ev = sctp_skb2event(skb);
755 sin6->sin6_scope_id = ev->iif;
760 /* Do we support this AF? */
761 static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
763 switch (family) {
764 case AF_INET6:
765 return 1;
766 /* v4-mapped-v6 addresses */
767 case AF_INET:
768 if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
769 return 1;
770 default:
771 return 0;
775 /* Address matching with wildcards allowed. This extra level
776 * of indirection lets us choose whether a PF_INET6 should
777 * disallow any v4 addresses if we so choose.
779 static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
780 const union sctp_addr *addr2,
781 struct sctp_sock *opt)
783 struct sctp_af *af1, *af2;
785 af1 = sctp_get_af_specific(addr1->sa.sa_family);
786 af2 = sctp_get_af_specific(addr2->sa.sa_family);
788 if (!af1 || !af2)
789 return 0;
790 /* Today, wildcard AF_INET/AF_INET6. */
791 if (sctp_is_any(addr1) || sctp_is_any(addr2))
792 return 1;
794 if (addr1->sa.sa_family != addr2->sa.sa_family)
795 return 0;
797 return af1->cmp_addr(addr1, addr2);
800 /* Verify that the provided sockaddr looks bindable. Common verification,
801 * has already been taken care of.
803 static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
805 struct sctp_af *af;
807 /* ASSERT: address family has already been verified. */
808 if (addr->sa.sa_family != AF_INET6)
809 af = sctp_get_af_specific(addr->sa.sa_family);
810 else {
811 int type = ipv6_addr_type(&addr->v6.sin6_addr);
812 struct net_device *dev;
814 if (type & IPV6_ADDR_LINKLOCAL) {
815 if (!addr->v6.sin6_scope_id)
816 return 0;
817 dev = dev_get_by_index(addr->v6.sin6_scope_id);
818 if (!dev)
819 return 0;
820 dev_put(dev);
822 af = opt->pf->af;
824 return af->available(addr, opt);
827 /* Verify that the provided sockaddr looks sendable. Common verification,
828 * has already been taken care of.
830 static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
832 struct sctp_af *af = NULL;
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 dev_put(dev);
849 af = opt->pf->af;
852 return af != NULL;
855 /* Fill in Supported Address Type information for INIT and INIT-ACK
856 * chunks. Note: In the future, we may want to look at sock options
857 * to determine whether a PF_INET6 socket really wants to have IPV4
858 * addresses.
859 * Returns number of addresses supported.
861 static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
862 __u16 *types)
864 types[0] = SCTP_PARAM_IPV4_ADDRESS;
865 types[1] = SCTP_PARAM_IPV6_ADDRESS;
866 return 2;
869 static struct proto_ops inet6_seqpacket_ops = {
870 .family = PF_INET6,
871 .owner = THIS_MODULE,
872 .release = inet6_release,
873 .bind = inet6_bind,
874 .connect = inet_dgram_connect,
875 .socketpair = sock_no_socketpair,
876 .accept = inet_accept,
877 .getname = inet6_getname,
878 .poll = sctp_poll,
879 .ioctl = inet6_ioctl,
880 .listen = sctp_inet_listen,
881 .shutdown = inet_shutdown,
882 .setsockopt = sock_common_setsockopt,
883 .getsockopt = sock_common_getsockopt,
884 .sendmsg = inet_sendmsg,
885 .recvmsg = sock_common_recvmsg,
886 .mmap = sock_no_mmap,
889 static struct inet_protosw sctpv6_seqpacket_protosw = {
890 .type = SOCK_SEQPACKET,
891 .protocol = IPPROTO_SCTP,
892 .prot = &sctpv6_prot,
893 .ops = &inet6_seqpacket_ops,
894 .capability = -1,
895 .no_check = 0,
896 .flags = SCTP_PROTOSW_FLAG
898 static struct inet_protosw sctpv6_stream_protosw = {
899 .type = SOCK_STREAM,
900 .protocol = IPPROTO_SCTP,
901 .prot = &sctpv6_prot,
902 .ops = &inet6_seqpacket_ops,
903 .capability = -1,
904 .no_check = 0,
905 .flags = SCTP_PROTOSW_FLAG,
908 static int sctp6_rcv(struct sk_buff **pskb, unsigned int *nhoffp)
910 return sctp_rcv(*pskb) ? -1 : 0;
913 static struct inet6_protocol sctpv6_protocol = {
914 .handler = sctp6_rcv,
915 .err_handler = sctp_v6_err,
916 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
919 static struct sctp_af sctp_ipv6_specific = {
920 .sctp_xmit = sctp_v6_xmit,
921 .setsockopt = ipv6_setsockopt,
922 .getsockopt = ipv6_getsockopt,
923 .get_dst = sctp_v6_get_dst,
924 .get_saddr = sctp_v6_get_saddr,
925 .copy_addrlist = sctp_v6_copy_addrlist,
926 .from_skb = sctp_v6_from_skb,
927 .from_sk = sctp_v6_from_sk,
928 .to_sk_saddr = sctp_v6_to_sk_saddr,
929 .to_sk_daddr = sctp_v6_to_sk_daddr,
930 .from_addr_param = sctp_v6_from_addr_param,
931 .to_addr_param = sctp_v6_to_addr_param,
932 .dst_saddr = sctp_v6_dst_saddr,
933 .cmp_addr = sctp_v6_cmp_addr,
934 .scope = sctp_v6_scope,
935 .addr_valid = sctp_v6_addr_valid,
936 .inaddr_any = sctp_v6_inaddr_any,
937 .is_any = sctp_v6_is_any,
938 .available = sctp_v6_available,
939 .skb_iif = sctp_v6_skb_iif,
940 .is_ce = sctp_v6_is_ce,
941 .seq_dump_addr = sctp_v6_seq_dump_addr,
942 .net_header_len = sizeof(struct ipv6hdr),
943 .sockaddr_len = sizeof(struct sockaddr_in6),
944 .sa_family = AF_INET6,
947 static struct sctp_pf sctp_pf_inet6_specific = {
948 .event_msgname = sctp_inet6_event_msgname,
949 .skb_msgname = sctp_inet6_skb_msgname,
950 .af_supported = sctp_inet6_af_supported,
951 .cmp_addr = sctp_inet6_cmp_addr,
952 .bind_verify = sctp_inet6_bind_verify,
953 .send_verify = sctp_inet6_send_verify,
954 .supported_addrs = sctp_inet6_supported_addrs,
955 .create_accept_sk = sctp_v6_create_accept_sk,
956 .addr_v4map = sctp_v6_addr_v4map,
957 .af = &sctp_ipv6_specific,
960 /* Initialize IPv6 support and register with inet6 stack. */
961 int sctp_v6_init(void)
963 int rc = proto_register(&sctpv6_prot, 1);
965 if (rc)
966 goto out;
967 /* Register inet6 protocol. */
968 rc = -EAGAIN;
969 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
970 goto out_unregister_sctp_proto;
972 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
973 inet6_register_protosw(&sctpv6_seqpacket_protosw);
974 inet6_register_protosw(&sctpv6_stream_protosw);
976 /* Register the SCTP specific PF_INET6 functions. */
977 sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
979 /* Register the SCTP specific AF_INET6 functions. */
980 sctp_register_af(&sctp_ipv6_specific);
982 /* Register notifier for inet6 address additions/deletions. */
983 register_inet6addr_notifier(&sctp_inet6addr_notifier);
984 rc = 0;
985 out:
986 return rc;
987 out_unregister_sctp_proto:
988 proto_unregister(&sctpv6_prot);
989 goto out;
992 /* IPv6 specific exit support. */
993 void sctp_v6_exit(void)
995 list_del(&sctp_ipv6_specific.list);
996 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
997 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
998 inet6_unregister_protosw(&sctpv6_stream_protosw);
999 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
1000 proto_unregister(&sctpv6_prot);