]> bbs.cooldavid.org Git - net-next-2.6.git/blame - net/sctp/ipv6.c
[TCP]: TCP Illinois congestion control (rev3)
[net-next-2.6.git] / net / sctp / ipv6.c
CommitLineData
1da177e4
LT
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.
6 *
7 * This file is part of the SCTP kernel reference Implementation
8 *
9 * SCTP over IPv6.
10 *
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.
16 *
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.
22 *
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.
27 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
30 * lksctp developers <lksctp-developers@lists.sourceforge.net>
31 *
32 * Or submit a bug report through the following website:
33 * http://www.sf.net/projects/lksctp
34 *
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>
42 *
43 * Based on:
44 * linux/net/ipv6/tcp_ipv6.c
45 *
46 * Any bugs reported given to us we will try to fix... any fixes shared will
47 * be incorporated into the next SCTP release.
48 */
49
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>
1da177e4
LT
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>
61
62#include <linux/ipv6.h>
63#include <linux/icmpv6.h>
64#include <linux/random.h>
65#include <linux/seq_file.h>
66
67#include <net/protocol.h>
1da177e4 68#include <net/ndisc.h>
c752f073 69#include <net/ip.h>
1da177e4
LT
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>
77
78#include <asm/uaccess.h>
79
29c7cf96 80/* Event handler for inet6 address addition/deletion events. */
24123186
AB
81static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
82 void *ptr)
29c7cf96
SS
83{
84 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
85 struct sctp_sockaddr_entry *addr;
86 struct list_head *pos, *temp;
87
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);
98 }
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;
107 }
108 }
109
110 break;
111 }
112
113 return NOTIFY_DONE;
114}
115
1da177e4 116static struct notifier_block sctp_inet6addr_notifier = {
29c7cf96 117 .notifier_call = sctp_inet6addr_event,
1da177e4
LT
118};
119
120/* ICMP error handler. */
121SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
04ce6909 122 int type, int code, int offset, __be32 info)
1da177e4
LT
123{
124 struct inet6_dev *idev;
1da177e4 125 struct sock *sk;
1da177e4
LT
126 struct sctp_association *asoc;
127 struct sctp_transport *transport;
128 struct ipv6_pinfo *np;
2e07fa9c 129 sk_buff_data_t saveip, savesctp;
1da177e4
LT
130 int err;
131
132 idev = in6_dev_get(skb->dev);
133
134 /* Fix up skb to look at the embedded net header. */
b0e380b1
ACM
135 saveip = skb->network_header;
136 savesctp = skb->transport_header;
1ced98e8 137 skb_reset_network_header(skb);
a27ef749
ACM
138 skb_set_transport_header(skb, offset);
139 sk = sctp_err_lookup(AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
1da177e4 140 /* Put back, the original pointers. */
b0e380b1
ACM
141 skb->network_header = saveip;
142 skb->transport_header = savesctp;
1da177e4
LT
143 if (!sk) {
144 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
145 goto out;
146 }
147
148 /* Warning: The sock lock is held. Remember to call
149 * sctp_err_finish!
150 */
151
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) {
d1ad1ff2 158 sctp_icmp_proto_unreachable(sk, asoc, transport);
1da177e4
LT
159 goto out_unlock;
160 }
161 break;
162 default:
163 break;
164 }
165
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;
173 }
174
175out_unlock:
d1ad1ff2 176 sctp_err_finish(sk, asoc);
1da177e4
LT
177out:
178 if (likely(idev != NULL))
179 in6_dev_put(idev);
180}
181
182/* Based on tcp_v6_xmit() in tcp_ipv6.c. */
183static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
184 int ipfragok)
185{
186 struct sock *sk = skb->sk;
187 struct ipv6_pinfo *np = inet6_sk(sk);
188 struct flowi fl;
189
190 memset(&fl, 0, sizeof(fl));
191
192 fl.proto = sk->sk_protocol;
193
194 /* Fill in the dest address from the route entry passed with the skb
195 * and the source address from the transport.
196 */
b3f5b3b6 197 ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
a9266268 198 ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
1da177e4
LT
199
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)
a9266268 203 fl.oif = transport->saddr.v6.sin6_scope_id;
1da177e4
LT
204 else
205 fl.oif = sk->sk_bound_dev_if;
1da177e4
LT
206
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);
210 }
211
212 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
46b86a2d 213 "src:" NIP6_FMT " dst:" NIP6_FMT "\n",
1da177e4
LT
214 __FUNCTION__, skb, skb->len,
215 NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
216
217 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
218
219 return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
220}
221
222/* Returns the dst cache entry for the given source and destination ip
223 * addresses.
224 */
225static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
226 union sctp_addr *daddr,
227 union sctp_addr *saddr)
228{
229 struct dst_entry *dst;
230 struct flowi fl;
231
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;
d808ad9a 236
1da177e4 237
46b86a2d 238 SCTP_DEBUG_PRINTK("%s: DST=" NIP6_FMT " ",
1da177e4
LT
239 __FUNCTION__, NIP6(fl.fl6_dst));
240
241 if (saddr) {
242 ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
243 SCTP_DEBUG_PRINTK(
46b86a2d 244 "SRC=" NIP6_FMT " - ",
1da177e4
LT
245 NIP6(fl.fl6_src));
246 }
247
248 dst = ip6_route_output(NULL, &fl);
4251320f 249 if (!dst->error) {
1da177e4
LT
250 struct rt6_info *rt;
251 rt = (struct rt6_info *)dst;
252 SCTP_DEBUG_PRINTK(
46b86a2d 253 "rt6_dst:" NIP6_FMT " rt6_src:" NIP6_FMT "\n",
1da177e4 254 NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
4251320f 255 return dst;
1da177e4 256 }
4251320f
VN
257 SCTP_DEBUG_PRINTK("NO ROUTE\n");
258 dst_release(dst);
259 return NULL;
1da177e4
LT
260}
261
262/* Returns the number of consecutive initial bits that match in the 2 ipv6
263 * addresses.
264 */
265static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
266 union sctp_addr *s2)
267{
268 struct in6_addr *a1 = &s1->v6.sin6_addr;
269 struct in6_addr *a2 = &s2->v6.sin6_addr;
270 int i, j;
271
272 for (i = 0; i < 4 ; i++) {
dbc16db1 273 __be32 a1xora2;
1da177e4
LT
274
275 a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
276
277 if ((j = fls(ntohl(a1xora2))))
278 return (i * 32 + 32 - j);
279 }
280
281 return (i*32);
282}
283
284/* Fills in the source address(saddr) based on the destination address(daddr)
285 * and asoc's bind address list.
286 */
287static void sctp_v6_get_saddr(struct sctp_association *asoc,
288 struct dst_entry *dst,
289 union sctp_addr *daddr,
290 union sctp_addr *saddr)
291{
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;
300
301 SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
46b86a2d 302 "daddr:" NIP6_FMT " ",
1da177e4
LT
303 __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
304
305 if (!asoc) {
306 ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
46b86a2d 307 SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: " NIP6_FMT "\n",
1da177e4
LT
308 NIP6(saddr->v6.sin6_addr));
309 return;
310 }
311
312 scope = sctp_scope(daddr);
313
314 bp = &asoc->base.bind_addr;
315 addr_lock = &asoc->base.addr_lock;
316
317 /* Go through the bind address list and find the best source address
318 * that matches the scope of the destination address.
319 */
320 sctp_read_lock(addr_lock);
321 list_for_each(pos, &bp->address_list) {
322 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
dc022a98 323 if ((laddr->use_as_src) &&
6244be4e
AV
324 (laddr->a.sa.sa_family == AF_INET6) &&
325 (scope <= sctp_scope(&laddr->a))) {
d3f7a54a 326 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
1da177e4 327 if (!baddr || (matchlen < bmatchlen)) {
d3f7a54a 328 baddr = &laddr->a;
1da177e4
LT
329 matchlen = bmatchlen;
330 }
331 }
332 }
333
334 if (baddr) {
335 memcpy(saddr, baddr, sizeof(union sctp_addr));
46b86a2d 336 SCTP_DEBUG_PRINTK("saddr: " NIP6_FMT "\n",
1da177e4
LT
337 NIP6(saddr->v6.sin6_addr));
338 } else {
339 printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
46b86a2d 340 "address for the dest:" NIP6_FMT "\n",
1da177e4
LT
341 __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
342 }
343
344 sctp_read_unlock(addr_lock);
345}
346
347/* Make a copy of all potential local addresses. */
348static void sctp_v6_copy_addrlist(struct list_head *addrlist,
349 struct net_device *dev)
350{
351 struct inet6_dev *in6_dev;
352 struct inet6_ifaddr *ifp;
353 struct sctp_sockaddr_entry *addr;
354
8814c4b5 355 rcu_read_lock();
1da177e4 356 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
8814c4b5 357 rcu_read_unlock();
1da177e4
LT
358 return;
359 }
360
e2eb8d45 361 read_lock_bh(&in6_dev->lock);
1da177e4
LT
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) {
2a6fd78a
AV
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;
1da177e4
LT
370 INIT_LIST_HEAD(&addr->list);
371 list_add_tail(&addr->list, addrlist);
372 }
373 }
374
e2eb8d45 375 read_unlock_bh(&in6_dev->lock);
8814c4b5 376 rcu_read_unlock();
1da177e4
LT
377}
378
379/* Initialize a sockaddr_storage from in incoming skb. */
380static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
381 int is_saddr)
382{
383 void *from;
d55c41b1 384 __be16 *port;
1da177e4
LT
385 struct sctphdr *sh;
386
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;
391
2c0fd387 392 sh = sctp_hdr(skb);
1da177e4 393 if (is_saddr) {
d55c41b1 394 *port = sh->source;
0660e03f 395 from = &ipv6_hdr(skb)->saddr;
1da177e4 396 } else {
d55c41b1 397 *port = sh->dest;
0660e03f 398 from = &ipv6_hdr(skb)->daddr;
1da177e4
LT
399 }
400 ipv6_addr_copy(&addr->v6.sin6_addr, from);
401}
402
403/* Initialize an sctp_addr from a socket. */
404static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
405{
406 addr->v6.sin6_family = AF_INET6;
7dcdbd95 407 addr->v6.sin6_port = 0;
1da177e4
LT
408 addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
409}
410
411/* Initialize sk->sk_rcv_saddr from sctp_addr. */
412static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
413{
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;
422 }
423}
424
425/* Initialize sk->sk_daddr from sctp_addr. */
426static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
427{
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;
435 }
436}
437
438/* Initialize a sctp_addr from an address parameter. */
439static void sctp_v6_from_addr_param(union sctp_addr *addr,
440 union sctp_addr_param *param,
dd86d136 441 __be16 port, int iif)
1da177e4
LT
442{
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;
448}
449
450/* Initialize an address parameter from a sctp_addr and return the length
451 * of the address parameter.
452 */
453static int sctp_v6_to_addr_param(const union sctp_addr *addr,
454 union sctp_addr_param *param)
455{
456 int length = sizeof(sctp_ipv6addr_param_t);
457
458 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
dbc16db1 459 param->v6.param_hdr.length = htons(length);
1da177e4
LT
460 ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
461
462 return length;
463}
464
465/* Initialize a sctp_addr from a dst_entry. */
466static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
854d43a4 467 __be16 port)
1da177e4
LT
468{
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);
473}
474
475/* Compare addresses exactly.
476 * v4-mapped-v6 is also in consideration.
477 */
478static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
479 const union sctp_addr *addr2)
480{
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;
489 }
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;
497 }
498 return 0;
499 }
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;
507 }
508 }
509
510 return 1;
511}
512
513/* Initialize addr struct to INADDR_ANY. */
6fbfa9f9 514static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
1da177e4
LT
515{
516 memset(addr, 0x00, sizeof(union sctp_addr));
517 addr->v6.sin6_family = AF_INET6;
518 addr->v6.sin6_port = port;
519}
520
521/* Is this a wildcard address? */
522static int sctp_v6_is_any(const union sctp_addr *addr)
523{
b9b9e10f 524 return ipv6_addr_any(&addr->v6.sin6_addr);
1da177e4
LT
525}
526
527/* Should this be available for binding? */
528static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
529{
530 int type;
531 struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
532
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);
543 }
544 if (!(type & IPV6_ADDR_UNICAST))
545 return 0;
546
547 return ipv6_chk_addr(in6, NULL, 0);
548}
549
550/* This function checks if the address is a valid address to be used for
551 * SCTP.
552 *
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.
556 */
5636bef7
VY
557static int sctp_v6_addr_valid(union sctp_addr *addr,
558 struct sctp_sock *sp,
559 const struct sk_buff *skb)
1da177e4
LT
560{
561 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
562
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.
567 */
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);
5636bef7 573 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
1da177e4
LT
574 }
575
576 /* Is this a non-unicast address */
577 if (!(ret & IPV6_ADDR_UNICAST))
578 return 0;
579
580 return 1;
581}
582
583/* What is the scope of 'addr'? */
584static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
585{
586 int v6scope;
587 sctp_scope_t retval;
588
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.
591 */
592
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;
607 };
608
609 return retval;
610}
611
612/* Create and initialize a new sk for the socket to be returned by accept(). */
613static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
614 struct sctp_association *asoc)
615{
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;
621
622 newsk = sk_alloc(PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
623 if (!newsk)
624 goto out;
625
626 sock_init_data(NULL, newsk);
627
628 newsk->sk_type = SOCK_STREAM;
629
630 newsk->sk_prot = sk->sk_prot;
631 newsk->sk_no_check = sk->sk_no_check;
632 newsk->sk_reuse = sk->sk_reuse;
633
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);
640
641 newsctp6sk = (struct sctp6_sock *)newsk;
642 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
643
644 newinet = inet_sk(newsk);
645 newnp = inet6_sk(newsk);
646
647 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
648
649 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
650 * and getpeername().
651 */
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);
657
658 /* Init the ipv4 part of the socket since we can have sockets
659 * using v6 API for ipv4.
660 */
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;
666
667 if (ipv4_config.no_pmtu_disc)
668 newinet->pmtudisc = IP_PMTUDISC_DONT;
669 else
670 newinet->pmtudisc = IP_PMTUDISC_WANT;
671
e6848976 672 sk_refcnt_debug_inc(newsk);
1da177e4
LT
673
674 if (newsk->sk_prot->init(newsk)) {
675 sk_common_release(newsk);
676 newsk = NULL;
677 }
678
679out:
680 return newsk;
681}
682
683/* Map v4 address to mapped v6 address */
684static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
685{
686 if (sp->v4mapped && AF_INET == addr->sa.sa_family)
687 sctp_v4_map_v6(addr);
688}
689
690/* Where did this skb come from? */
691static int sctp_v6_skb_iif(const struct sk_buff *skb)
692{
693 struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
694 return opt->iif;
695}
696
697/* Was this packet marked by Explicit Congestion Notification? */
698static int sctp_v6_is_ce(const struct sk_buff *skb)
699{
0660e03f 700 return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
1da177e4
LT
701}
702
703/* Dump the v6 addr to the seq file. */
704static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
705{
46b86a2d 706 seq_printf(seq, NIP6_FMT " ", NIP6(addr->v6.sin6_addr));
1da177e4
LT
707}
708
709/* Initialize a PF_INET6 socket msg_name. */
710static void sctp_inet6_msgname(char *msgname, int *addr_len)
711{
712 struct sockaddr_in6 *sin6;
713
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);
719}
720
721/* Initialize a PF_INET msgname from a ulpevent. */
722static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
723 char *msgname, int *addrlen)
724{
725 struct sockaddr_in6 *sin6, *sin6from;
726
727 if (msgname) {
728 union sctp_addr *addr;
729 struct sctp_association *asoc;
730
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;
736
737 /* Note: If we go to a common v6 format, this code
738 * will change.
739 */
740
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;
748 }
749
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;
754 }
755}
756
757/* Initialize a msg_name from an inbound skb. */
758static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
759 int *addr_len)
760{
761 struct sctphdr *sh;
762 struct sockaddr_in6 *sin6;
763
764 if (msgname) {
765 sctp_inet6_msgname(msgname, addr_len);
766 sin6 = (struct sockaddr_in6 *)msgname;
2c0fd387 767 sh = sctp_hdr(skb);
1da177e4
LT
768 sin6->sin6_port = sh->source;
769
770 /* Map ipv4 address into v4-mapped-on-v6 address. */
771 if (sctp_sk(skb->sk)->v4mapped &&
eddc9ec5 772 ip_hdr(skb)->version == 4) {
1da177e4 773 sctp_v4_map_v6((union sctp_addr *)sin6);
eddc9ec5 774 sin6->sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
1da177e4
LT
775 return;
776 }
777
778 /* Otherwise, just copy the v6 address. */
0660e03f 779 ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
1da177e4
LT
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;
783 }
784 }
785}
786
787/* Do we support this AF? */
788static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
789{
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;
799 }
800}
801
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.
805 */
806static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
807 const union sctp_addr *addr2,
808 struct sctp_sock *opt)
809{
810 struct sctp_af *af1, *af2;
811
812 af1 = sctp_get_af_specific(addr1->sa.sa_family);
813 af2 = sctp_get_af_specific(addr2->sa.sa_family);
814
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;
820
821 if (addr1->sa.sa_family != addr2->sa.sa_family)
822 return 0;
823
824 return af1->cmp_addr(addr1, addr2);
825}
826
827/* Verify that the provided sockaddr looks bindable. Common verification,
828 * has already been taken care of.
829 */
830static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
831{
832 struct sctp_af *af;
833
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 {
1da177e4 838 int type = ipv6_addr_type(&addr->v6.sin6_addr);
6a6ddb2a
SS
839 struct net_device *dev;
840
1da177e4 841 if (type & IPV6_ADDR_LINKLOCAL) {
6a6ddb2a
SS
842 if (!addr->v6.sin6_scope_id)
843 return 0;
844 dev = dev_get_by_index(addr->v6.sin6_scope_id);
845 if (!dev)
1da177e4 846 return 0;
6a6ddb2a 847 dev_put(dev);
1da177e4
LT
848 }
849 af = opt->pf->af;
850 }
851 return af->available(addr, opt);
852}
853
6a6ddb2a 854/* Verify that the provided sockaddr looks sendable. Common verification,
1da177e4
LT
855 * has already been taken care of.
856 */
857static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
858{
859 struct sctp_af *af = NULL;
860
861 /* ASSERT: address family has already been verified. */
862 if (addr->sa.sa_family != AF_INET6)
863 af = sctp_get_af_specific(addr->sa.sa_family);
864 else {
1da177e4 865 int type = ipv6_addr_type(&addr->v6.sin6_addr);
6a6ddb2a
SS
866 struct net_device *dev;
867
1da177e4 868 if (type & IPV6_ADDR_LINKLOCAL) {
6a6ddb2a
SS
869 if (!addr->v6.sin6_scope_id)
870 return 0;
871 dev = dev_get_by_index(addr->v6.sin6_scope_id);
872 if (!dev)
1da177e4 873 return 0;
6a6ddb2a 874 dev_put(dev);
1da177e4
LT
875 }
876 af = opt->pf->af;
877 }
878
879 return af != NULL;
880}
881
882/* Fill in Supported Address Type information for INIT and INIT-ACK
883 * chunks. Note: In the future, we may want to look at sock options
884 * to determine whether a PF_INET6 socket really wants to have IPV4
885 * addresses.
886 * Returns number of addresses supported.
887 */
888static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
3dbe8656 889 __be16 *types)
1da177e4
LT
890{
891 types[0] = SCTP_PARAM_IPV4_ADDRESS;
892 types[1] = SCTP_PARAM_IPV6_ADDRESS;
893 return 2;
894}
895
90ddc4f0 896static const struct proto_ops inet6_seqpacket_ops = {
543d9cfe
ACM
897 .family = PF_INET6,
898 .owner = THIS_MODULE,
899 .release = inet6_release,
900 .bind = inet6_bind,
901 .connect = inet_dgram_connect,
902 .socketpair = sock_no_socketpair,
903 .accept = inet_accept,
904 .getname = inet6_getname,
905 .poll = sctp_poll,
906 .ioctl = inet6_ioctl,
907 .listen = sctp_inet_listen,
908 .shutdown = inet_shutdown,
909 .setsockopt = sock_common_setsockopt,
910 .getsockopt = sock_common_getsockopt,
911 .sendmsg = inet_sendmsg,
912 .recvmsg = sock_common_recvmsg,
913 .mmap = sock_no_mmap,
3fdadf7d 914#ifdef CONFIG_COMPAT
543d9cfe
ACM
915 .compat_setsockopt = compat_sock_common_setsockopt,
916 .compat_getsockopt = compat_sock_common_getsockopt,
3fdadf7d 917#endif
1da177e4
LT
918};
919
920static struct inet_protosw sctpv6_seqpacket_protosw = {
921 .type = SOCK_SEQPACKET,
922 .protocol = IPPROTO_SCTP,
923 .prot = &sctpv6_prot,
924 .ops = &inet6_seqpacket_ops,
925 .capability = -1,
926 .no_check = 0,
927 .flags = SCTP_PROTOSW_FLAG
928};
929static struct inet_protosw sctpv6_stream_protosw = {
930 .type = SOCK_STREAM,
931 .protocol = IPPROTO_SCTP,
932 .prot = &sctpv6_prot,
933 .ops = &inet6_seqpacket_ops,
934 .capability = -1,
935 .no_check = 0,
936 .flags = SCTP_PROTOSW_FLAG,
937};
938
951dbc8a 939static int sctp6_rcv(struct sk_buff **pskb)
1da177e4
LT
940{
941 return sctp_rcv(*pskb) ? -1 : 0;
942}
943
944static struct inet6_protocol sctpv6_protocol = {
945 .handler = sctp6_rcv,
946 .err_handler = sctp_v6_err,
947 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
948};
949
950static struct sctp_af sctp_ipv6_specific = {
543d9cfe
ACM
951 .sa_family = AF_INET6,
952 .sctp_xmit = sctp_v6_xmit,
953 .setsockopt = ipv6_setsockopt,
954 .getsockopt = ipv6_getsockopt,
955 .get_dst = sctp_v6_get_dst,
956 .get_saddr = sctp_v6_get_saddr,
957 .copy_addrlist = sctp_v6_copy_addrlist,
958 .from_skb = sctp_v6_from_skb,
959 .from_sk = sctp_v6_from_sk,
960 .to_sk_saddr = sctp_v6_to_sk_saddr,
961 .to_sk_daddr = sctp_v6_to_sk_daddr,
962 .from_addr_param = sctp_v6_from_addr_param,
963 .to_addr_param = sctp_v6_to_addr_param,
964 .dst_saddr = sctp_v6_dst_saddr,
965 .cmp_addr = sctp_v6_cmp_addr,
966 .scope = sctp_v6_scope,
967 .addr_valid = sctp_v6_addr_valid,
968 .inaddr_any = sctp_v6_inaddr_any,
969 .is_any = sctp_v6_is_any,
970 .available = sctp_v6_available,
971 .skb_iif = sctp_v6_skb_iif,
972 .is_ce = sctp_v6_is_ce,
973 .seq_dump_addr = sctp_v6_seq_dump_addr,
974 .net_header_len = sizeof(struct ipv6hdr),
975 .sockaddr_len = sizeof(struct sockaddr_in6),
3fdadf7d 976#ifdef CONFIG_COMPAT
543d9cfe
ACM
977 .compat_setsockopt = compat_ipv6_setsockopt,
978 .compat_getsockopt = compat_ipv6_getsockopt,
3fdadf7d 979#endif
1da177e4
LT
980};
981
982static struct sctp_pf sctp_pf_inet6_specific = {
983 .event_msgname = sctp_inet6_event_msgname,
984 .skb_msgname = sctp_inet6_skb_msgname,
985 .af_supported = sctp_inet6_af_supported,
986 .cmp_addr = sctp_inet6_cmp_addr,
987 .bind_verify = sctp_inet6_bind_verify,
988 .send_verify = sctp_inet6_send_verify,
989 .supported_addrs = sctp_inet6_supported_addrs,
990 .create_accept_sk = sctp_v6_create_accept_sk,
991 .addr_v4map = sctp_v6_addr_v4map,
992 .af = &sctp_ipv6_specific,
993};
994
995/* Initialize IPv6 support and register with inet6 stack. */
996int sctp_v6_init(void)
997{
998 int rc = proto_register(&sctpv6_prot, 1);
999
1000 if (rc)
1001 goto out;
1002 /* Register inet6 protocol. */
1003 rc = -EAGAIN;
1004 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1005 goto out_unregister_sctp_proto;
1006
1007 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
1008 inet6_register_protosw(&sctpv6_seqpacket_protosw);
1009 inet6_register_protosw(&sctpv6_stream_protosw);
1010
1011 /* Register the SCTP specific PF_INET6 functions. */
1012 sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
1013
1014 /* Register the SCTP specific AF_INET6 functions. */
1015 sctp_register_af(&sctp_ipv6_specific);
1016
1017 /* Register notifier for inet6 address additions/deletions. */
1018 register_inet6addr_notifier(&sctp_inet6addr_notifier);
1019 rc = 0;
1020out:
1021 return rc;
1022out_unregister_sctp_proto:
1023 proto_unregister(&sctpv6_prot);
1024 goto out;
1025}
1026
1027/* IPv6 specific exit support. */
1028void sctp_v6_exit(void)
1029{
1030 list_del(&sctp_ipv6_specific.list);
1031 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1032 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1033 inet6_unregister_protosw(&sctpv6_stream_protosw);
1034 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
1035 proto_unregister(&sctpv6_prot);
1036}