blob: d6b585b85cbb40673580f2b124e986b3270af030 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/* 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>
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>
62
63#include <linux/ipv6.h>
64#include <linux/icmpv6.h>
65#include <linux/random.h>
66#include <linux/seq_file.h>
67
68#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#include <net/ndisc.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070070#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#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>
78
79#include <asm/uaccess.h>
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static struct notifier_block sctp_inet6addr_notifier = {
82 .notifier_call = sctp_inetaddr_event,
83};
84
85/* ICMP error handler. */
86SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Al Viro04ce6902006-11-08 00:21:01 -080087 int type, int code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070088{
89 struct inet6_dev *idev;
90 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
91 struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
92 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 struct sctp_association *asoc;
94 struct sctp_transport *transport;
95 struct ipv6_pinfo *np;
96 char *saveip, *savesctp;
97 int err;
98
99 idev = in6_dev_get(skb->dev);
100
101 /* Fix up skb to look at the embedded net header. */
102 saveip = skb->nh.raw;
103 savesctp = skb->h.raw;
104 skb->nh.ipv6h = iph;
105 skb->h.raw = (char *)sh;
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700106 sk = sctp_err_lookup(AF_INET6, skb, sh, &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 /* Put back, the original pointers. */
108 skb->nh.raw = saveip;
109 skb->h.raw = savesctp;
110 if (!sk) {
111 ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
112 goto out;
113 }
114
115 /* Warning: The sock lock is held. Remember to call
116 * sctp_err_finish!
117 */
118
119 switch (type) {
120 case ICMPV6_PKT_TOOBIG:
121 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
122 goto out_unlock;
123 case ICMPV6_PARAMPROB:
124 if (ICMPV6_UNK_NEXTHDR == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700125 sctp_icmp_proto_unreachable(sk, asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 goto out_unlock;
127 }
128 break;
129 default:
130 break;
131 }
132
133 np = inet6_sk(sk);
134 icmpv6_err_convert(type, code, &err);
135 if (!sock_owned_by_user(sk) && np->recverr) {
136 sk->sk_err = err;
137 sk->sk_error_report(sk);
138 } else { /* Only an error on timeout */
139 sk->sk_err_soft = err;
140 }
141
142out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700143 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144out:
145 if (likely(idev != NULL))
146 in6_dev_put(idev);
147}
148
149/* Based on tcp_v6_xmit() in tcp_ipv6.c. */
150static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
151 int ipfragok)
152{
153 struct sock *sk = skb->sk;
154 struct ipv6_pinfo *np = inet6_sk(sk);
155 struct flowi fl;
156
157 memset(&fl, 0, sizeof(fl));
158
159 fl.proto = sk->sk_protocol;
160
161 /* Fill in the dest address from the route entry passed with the skb
162 * and the source address from the transport.
163 */
Al Viro09ef7fe2006-11-20 17:04:10 -0800164 ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr_h.v6.sin6_addr);
165 ipv6_addr_copy(&fl.fl6_src, &transport->saddr_h.v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 fl.fl6_flowlabel = np->flow_label;
168 IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
169 if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
Al Viro09ef7fe2006-11-20 17:04:10 -0800170 fl.oif = transport->saddr_h.v6.sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 else
172 fl.oif = sk->sk_bound_dev_if;
173 fl.fl_ip_sport = inet_sk(sk)->sport;
Al Viro09ef7fe2006-11-20 17:04:10 -0800174 fl.fl_ip_dport = transport->ipaddr_h.v6.sin6_port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175
176 if (np->opt && np->opt->srcrt) {
177 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
178 ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
179 }
180
181 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
Joe Perches46b86a22006-01-13 14:29:07 -0800182 "src:" NIP6_FMT " dst:" NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 __FUNCTION__, skb, skb->len,
184 NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
185
186 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
187
188 return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
189}
190
191/* Returns the dst cache entry for the given source and destination ip
192 * addresses.
193 */
194static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
195 union sctp_addr *daddr,
196 union sctp_addr *saddr)
197{
198 struct dst_entry *dst;
199 struct flowi fl;
200
201 memset(&fl, 0, sizeof(fl));
202 ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
203 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
204 fl.oif = daddr->v6.sin6_scope_id;
205
206
Joe Perches46b86a22006-01-13 14:29:07 -0800207 SCTP_DEBUG_PRINTK("%s: DST=" NIP6_FMT " ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 __FUNCTION__, NIP6(fl.fl6_dst));
209
210 if (saddr) {
211 ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
212 SCTP_DEBUG_PRINTK(
Joe Perches46b86a22006-01-13 14:29:07 -0800213 "SRC=" NIP6_FMT " - ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 NIP6(fl.fl6_src));
215 }
216
217 dst = ip6_route_output(NULL, &fl);
Ville Nuorvala42513202006-10-16 22:10:05 -0700218 if (!dst->error) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 struct rt6_info *rt;
220 rt = (struct rt6_info *)dst;
221 SCTP_DEBUG_PRINTK(
Joe Perches46b86a22006-01-13 14:29:07 -0800222 "rt6_dst:" NIP6_FMT " rt6_src:" NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223 NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
Ville Nuorvala42513202006-10-16 22:10:05 -0700224 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 }
Ville Nuorvala42513202006-10-16 22:10:05 -0700226 SCTP_DEBUG_PRINTK("NO ROUTE\n");
227 dst_release(dst);
228 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
231/* Returns the number of consecutive initial bits that match in the 2 ipv6
232 * addresses.
233 */
234static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
235 union sctp_addr *s2)
236{
237 struct in6_addr *a1 = &s1->v6.sin6_addr;
238 struct in6_addr *a2 = &s2->v6.sin6_addr;
239 int i, j;
240
241 for (i = 0; i < 4 ; i++) {
Al Virodbc16db2006-11-20 17:01:42 -0800242 __be32 a1xora2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
245
246 if ((j = fls(ntohl(a1xora2))))
247 return (i * 32 + 32 - j);
248 }
249
250 return (i*32);
251}
252
253/* Fills in the source address(saddr) based on the destination address(daddr)
254 * and asoc's bind address list.
255 */
256static void sctp_v6_get_saddr(struct sctp_association *asoc,
257 struct dst_entry *dst,
258 union sctp_addr *daddr,
259 union sctp_addr *saddr)
260{
261 struct sctp_bind_addr *bp;
262 rwlock_t *addr_lock;
263 struct sctp_sockaddr_entry *laddr;
264 struct list_head *pos;
265 sctp_scope_t scope;
266 union sctp_addr *baddr = NULL;
267 __u8 matchlen = 0;
268 __u8 bmatchlen;
269
270 SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
Joe Perches46b86a22006-01-13 14:29:07 -0800271 "daddr:" NIP6_FMT " ",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
273
274 if (!asoc) {
275 ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
Joe Perches46b86a22006-01-13 14:29:07 -0800276 SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: " NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 NIP6(saddr->v6.sin6_addr));
278 return;
279 }
280
281 scope = sctp_scope(daddr);
282
283 bp = &asoc->base.bind_addr;
284 addr_lock = &asoc->base.addr_lock;
285
286 /* Go through the bind address list and find the best source address
287 * that matches the scope of the destination address.
288 */
289 sctp_read_lock(addr_lock);
290 list_for_each(pos, &bp->address_list) {
291 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
Sridhar Samudraladc022a92006-07-21 14:49:25 -0700292 if ((laddr->use_as_src) &&
Al Viro09ef7fe2006-11-20 17:04:10 -0800293 (laddr->a_h.sa.sa_family == AF_INET6) &&
294 (scope <= sctp_scope(&laddr->a_h))) {
295 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a_h);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 if (!baddr || (matchlen < bmatchlen)) {
Al Viro09ef7fe2006-11-20 17:04:10 -0800297 baddr = &laddr->a_h;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 matchlen = bmatchlen;
299 }
300 }
301 }
302
303 if (baddr) {
304 memcpy(saddr, baddr, sizeof(union sctp_addr));
Joe Perches46b86a22006-01-13 14:29:07 -0800305 SCTP_DEBUG_PRINTK("saddr: " NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 NIP6(saddr->v6.sin6_addr));
307 } else {
308 printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
Joe Perches46b86a22006-01-13 14:29:07 -0800309 "address for the dest:" NIP6_FMT "\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
311 }
312
313 sctp_read_unlock(addr_lock);
314}
315
316/* Make a copy of all potential local addresses. */
317static void sctp_v6_copy_addrlist(struct list_head *addrlist,
318 struct net_device *dev)
319{
320 struct inet6_dev *in6_dev;
321 struct inet6_ifaddr *ifp;
322 struct sctp_sockaddr_entry *addr;
323
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700324 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700326 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 return;
328 }
329
330 read_lock(&in6_dev->lock);
331 for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
332 /* Add the address to the local list. */
333 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
334 if (addr) {
Al Viro2a6fd782006-11-20 17:04:42 -0800335 addr->a.v6.sin6_family = AF_INET6;
336 addr->a.v6.sin6_port = 0;
337 addr->a.v6.sin6_addr = ifp->addr;
338 addr->a.v6.sin6_scope_id = dev->ifindex;
339 addr->a_h = addr->a;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 INIT_LIST_HEAD(&addr->list);
341 list_add_tail(&addr->list, addrlist);
342 }
343 }
344
345 read_unlock(&in6_dev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700346 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347}
348
349/* Initialize a sockaddr_storage from in incoming skb. */
350static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
351 int is_saddr)
352{
353 void *from;
Al Virod55c41b2006-11-20 17:09:40 -0800354 __be16 *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 struct sctphdr *sh;
356
357 port = &addr->v6.sin6_port;
358 addr->v6.sin6_family = AF_INET6;
359 addr->v6.sin6_flowinfo = 0; /* FIXME */
360 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
361
362 sh = (struct sctphdr *) skb->h.raw;
363 if (is_saddr) {
Al Virod55c41b2006-11-20 17:09:40 -0800364 *port = sh->source;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 from = &skb->nh.ipv6h->saddr;
366 } else {
Al Virod55c41b2006-11-20 17:09:40 -0800367 *port = sh->dest;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 from = &skb->nh.ipv6h->daddr;
369 }
370 ipv6_addr_copy(&addr->v6.sin6_addr, from);
371}
372
373/* Initialize an sctp_addr from a socket. */
374static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
375{
376 addr->v6.sin6_family = AF_INET6;
377 addr->v6.sin6_port = inet_sk(sk)->num;
378 addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
379}
380
381/* Initialize sk->sk_rcv_saddr from sctp_addr. */
382static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
383{
384 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
385 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
386 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
387 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
388 inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
389 addr->v4.sin_addr.s_addr;
390 } else {
391 inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
392 }
393}
394
395/* Initialize sk->sk_daddr from sctp_addr. */
396static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
397{
398 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
399 inet6_sk(sk)->daddr.s6_addr32[0] = 0;
400 inet6_sk(sk)->daddr.s6_addr32[1] = 0;
401 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
402 inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
403 } else {
404 inet6_sk(sk)->daddr = addr->v6.sin6_addr;
405 }
406}
407
408/* Initialize a sctp_addr from an address parameter. */
409static void sctp_v6_from_addr_param(union sctp_addr *addr,
410 union sctp_addr_param *param,
411 __u16 port, int iif)
412{
413 addr->v6.sin6_family = AF_INET6;
414 addr->v6.sin6_port = port;
415 addr->v6.sin6_flowinfo = 0; /* BUG */
416 ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
417 addr->v6.sin6_scope_id = iif;
418}
419
420/* Initialize an address parameter from a sctp_addr and return the length
421 * of the address parameter.
422 */
423static int sctp_v6_to_addr_param(const union sctp_addr *addr,
424 union sctp_addr_param *param)
425{
426 int length = sizeof(sctp_ipv6addr_param_t);
427
428 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
Al Virodbc16db2006-11-20 17:01:42 -0800429 param->v6.param_hdr.length = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
431
432 return length;
433}
434
435/* Initialize a sctp_addr from a dst_entry. */
436static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
Al Viro854d43a2006-11-20 17:06:24 -0800437 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 struct rt6_info *rt = (struct rt6_info *)dst;
440 addr->sa.sa_family = AF_INET6;
441 addr->v6.sin6_port = port;
442 ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
443}
444
445/* Compare addresses exactly.
446 * v4-mapped-v6 is also in consideration.
447 */
448static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
449 const union sctp_addr *addr2)
450{
451 if (addr1->sa.sa_family != addr2->sa.sa_family) {
452 if (addr1->sa.sa_family == AF_INET &&
453 addr2->sa.sa_family == AF_INET6 &&
454 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr2->v6.sin6_addr)) {
455 if (addr2->v6.sin6_port == addr1->v4.sin_port &&
456 addr2->v6.sin6_addr.s6_addr32[3] ==
457 addr1->v4.sin_addr.s_addr)
458 return 1;
459 }
460 if (addr2->sa.sa_family == AF_INET &&
461 addr1->sa.sa_family == AF_INET6 &&
462 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr1->v6.sin6_addr)) {
463 if (addr1->v6.sin6_port == addr2->v4.sin_port &&
464 addr1->v6.sin6_addr.s6_addr32[3] ==
465 addr2->v4.sin_addr.s_addr)
466 return 1;
467 }
468 return 0;
469 }
470 if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
471 return 0;
472 /* If this is a linklocal address, compare the scope_id. */
473 if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
474 if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
475 (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
476 return 0;
477 }
478 }
479
480 return 1;
481}
482
483/* Initialize addr struct to INADDR_ANY. */
484static void sctp_v6_inaddr_any(union sctp_addr *addr, unsigned short port)
485{
486 memset(addr, 0x00, sizeof(union sctp_addr));
487 addr->v6.sin6_family = AF_INET6;
488 addr->v6.sin6_port = port;
489}
490
491/* Is this a wildcard address? */
492static int sctp_v6_is_any(const union sctp_addr *addr)
493{
Brian Haleyb9b9e102005-04-28 11:59:16 -0700494 return ipv6_addr_any(&addr->v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495}
496
497/* Should this be available for binding? */
498static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
499{
500 int type;
501 struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
502
503 type = ipv6_addr_type(in6);
504 if (IPV6_ADDR_ANY == type)
505 return 1;
506 if (type == IPV6_ADDR_MAPPED) {
507 if (sp && !sp->v4mapped)
508 return 0;
509 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
510 return 0;
511 sctp_v6_map_v4(addr);
512 return sctp_get_af_specific(AF_INET)->available(addr, sp);
513 }
514 if (!(type & IPV6_ADDR_UNICAST))
515 return 0;
516
517 return ipv6_chk_addr(in6, NULL, 0);
518}
519
520/* This function checks if the address is a valid address to be used for
521 * SCTP.
522 *
523 * Output:
524 * Return 0 - If the address is a non-unicast or an illegal address.
525 * Return 1 - If the address is a unicast.
526 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700527static int sctp_v6_addr_valid(union sctp_addr *addr,
528 struct sctp_sock *sp,
529 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
532
533 /* Support v4-mapped-v6 address. */
534 if (ret == IPV6_ADDR_MAPPED) {
535 /* Note: This routine is used in input, so v4-mapped-v6
536 * are disallowed here when there is no sctp_sock.
537 */
538 if (!sp || !sp->v4mapped)
539 return 0;
540 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
541 return 0;
542 sctp_v6_map_v4(addr);
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700543 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 }
545
546 /* Is this a non-unicast address */
547 if (!(ret & IPV6_ADDR_UNICAST))
548 return 0;
549
550 return 1;
551}
552
553/* What is the scope of 'addr'? */
554static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
555{
556 int v6scope;
557 sctp_scope_t retval;
558
559 /* The IPv6 scope is really a set of bit fields.
560 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
561 */
562
563 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
564 switch (v6scope) {
565 case IFA_HOST:
566 retval = SCTP_SCOPE_LOOPBACK;
567 break;
568 case IFA_LINK:
569 retval = SCTP_SCOPE_LINK;
570 break;
571 case IFA_SITE:
572 retval = SCTP_SCOPE_PRIVATE;
573 break;
574 default:
575 retval = SCTP_SCOPE_GLOBAL;
576 break;
577 };
578
579 return retval;
580}
581
582/* Create and initialize a new sk for the socket to be returned by accept(). */
583static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
584 struct sctp_association *asoc)
585{
586 struct inet_sock *inet = inet_sk(sk);
587 struct sock *newsk;
588 struct inet_sock *newinet;
589 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
590 struct sctp6_sock *newsctp6sk;
591
592 newsk = sk_alloc(PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
593 if (!newsk)
594 goto out;
595
596 sock_init_data(NULL, newsk);
597
598 newsk->sk_type = SOCK_STREAM;
599
600 newsk->sk_prot = sk->sk_prot;
601 newsk->sk_no_check = sk->sk_no_check;
602 newsk->sk_reuse = sk->sk_reuse;
603
604 newsk->sk_destruct = inet_sock_destruct;
605 newsk->sk_family = PF_INET6;
606 newsk->sk_protocol = IPPROTO_SCTP;
607 newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
608 newsk->sk_shutdown = sk->sk_shutdown;
609 sock_reset_flag(sk, SOCK_ZAPPED);
610
611 newsctp6sk = (struct sctp6_sock *)newsk;
612 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
613
614 newinet = inet_sk(newsk);
615 newnp = inet6_sk(newsk);
616
617 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
618
619 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
620 * and getpeername().
621 */
622 newinet->sport = inet->sport;
623 newnp->saddr = np->saddr;
624 newnp->rcv_saddr = np->rcv_saddr;
625 newinet->dport = htons(asoc->peer.port);
626 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
627
628 /* Init the ipv4 part of the socket since we can have sockets
629 * using v6 API for ipv4.
630 */
631 newinet->uc_ttl = -1;
632 newinet->mc_loop = 1;
633 newinet->mc_ttl = 1;
634 newinet->mc_index = 0;
635 newinet->mc_list = NULL;
636
637 if (ipv4_config.no_pmtu_disc)
638 newinet->pmtudisc = IP_PMTUDISC_DONT;
639 else
640 newinet->pmtudisc = IP_PMTUDISC_WANT;
641
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700642 sk_refcnt_debug_inc(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
644 if (newsk->sk_prot->init(newsk)) {
645 sk_common_release(newsk);
646 newsk = NULL;
647 }
648
649out:
650 return newsk;
651}
652
653/* Map v4 address to mapped v6 address */
654static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
655{
656 if (sp->v4mapped && AF_INET == addr->sa.sa_family)
657 sctp_v4_map_v6(addr);
658}
659
660/* Where did this skb come from? */
661static int sctp_v6_skb_iif(const struct sk_buff *skb)
662{
663 struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
664 return opt->iif;
665}
666
667/* Was this packet marked by Explicit Congestion Notification? */
668static int sctp_v6_is_ce(const struct sk_buff *skb)
669{
670 return *((__u32 *)(skb->nh.ipv6h)) & htonl(1<<20);
671}
672
673/* Dump the v6 addr to the seq file. */
674static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
675{
Joe Perches46b86a22006-01-13 14:29:07 -0800676 seq_printf(seq, NIP6_FMT " ", NIP6(addr->v6.sin6_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677}
678
679/* Initialize a PF_INET6 socket msg_name. */
680static void sctp_inet6_msgname(char *msgname, int *addr_len)
681{
682 struct sockaddr_in6 *sin6;
683
684 sin6 = (struct sockaddr_in6 *)msgname;
685 sin6->sin6_family = AF_INET6;
686 sin6->sin6_flowinfo = 0;
687 sin6->sin6_scope_id = 0; /*FIXME */
688 *addr_len = sizeof(struct sockaddr_in6);
689}
690
691/* Initialize a PF_INET msgname from a ulpevent. */
692static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
693 char *msgname, int *addrlen)
694{
695 struct sockaddr_in6 *sin6, *sin6from;
696
697 if (msgname) {
698 union sctp_addr *addr;
699 struct sctp_association *asoc;
700
701 asoc = event->asoc;
702 sctp_inet6_msgname(msgname, addrlen);
703 sin6 = (struct sockaddr_in6 *)msgname;
704 sin6->sin6_port = htons(asoc->peer.port);
705 addr = &asoc->peer.primary_addr;
706
707 /* Note: If we go to a common v6 format, this code
708 * will change.
709 */
710
711 /* Map ipv4 address into v4-mapped-on-v6 address. */
712 if (sctp_sk(asoc->base.sk)->v4mapped &&
713 AF_INET == addr->sa.sa_family) {
714 sctp_v4_map_v6((union sctp_addr *)sin6);
715 sin6->sin6_addr.s6_addr32[3] =
716 addr->v4.sin_addr.s_addr;
717 return;
718 }
719
720 sin6from = &asoc->peer.primary_addr.v6;
721 ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
722 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
723 sin6->sin6_scope_id = sin6from->sin6_scope_id;
724 }
725}
726
727/* Initialize a msg_name from an inbound skb. */
728static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
729 int *addr_len)
730{
731 struct sctphdr *sh;
732 struct sockaddr_in6 *sin6;
733
734 if (msgname) {
735 sctp_inet6_msgname(msgname, addr_len);
736 sin6 = (struct sockaddr_in6 *)msgname;
737 sh = (struct sctphdr *)skb->h.raw;
738 sin6->sin6_port = sh->source;
739
740 /* Map ipv4 address into v4-mapped-on-v6 address. */
741 if (sctp_sk(skb->sk)->v4mapped &&
742 skb->nh.iph->version == 4) {
743 sctp_v4_map_v6((union sctp_addr *)sin6);
744 sin6->sin6_addr.s6_addr32[3] = skb->nh.iph->saddr;
745 return;
746 }
747
748 /* Otherwise, just copy the v6 address. */
749 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
750 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
751 struct sctp_ulpevent *ev = sctp_skb2event(skb);
752 sin6->sin6_scope_id = ev->iif;
753 }
754 }
755}
756
757/* Do we support this AF? */
758static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
759{
760 switch (family) {
761 case AF_INET6:
762 return 1;
763 /* v4-mapped-v6 addresses */
764 case AF_INET:
765 if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
766 return 1;
767 default:
768 return 0;
769 }
770}
771
772/* Address matching with wildcards allowed. This extra level
773 * of indirection lets us choose whether a PF_INET6 should
774 * disallow any v4 addresses if we so choose.
775 */
776static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
777 const union sctp_addr *addr2,
778 struct sctp_sock *opt)
779{
780 struct sctp_af *af1, *af2;
781
782 af1 = sctp_get_af_specific(addr1->sa.sa_family);
783 af2 = sctp_get_af_specific(addr2->sa.sa_family);
784
785 if (!af1 || !af2)
786 return 0;
787 /* Today, wildcard AF_INET/AF_INET6. */
788 if (sctp_is_any(addr1) || sctp_is_any(addr2))
789 return 1;
790
791 if (addr1->sa.sa_family != addr2->sa.sa_family)
792 return 0;
793
794 return af1->cmp_addr(addr1, addr2);
795}
796
797/* Verify that the provided sockaddr looks bindable. Common verification,
798 * has already been taken care of.
799 */
800static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
801{
802 struct sctp_af *af;
803
804 /* ASSERT: address family has already been verified. */
805 if (addr->sa.sa_family != AF_INET6)
806 af = sctp_get_af_specific(addr->sa.sa_family);
807 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 int type = ipv6_addr_type(&addr->v6.sin6_addr);
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700809 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700811 if (type & IPV6_ADDR_LINKLOCAL) {
812 if (!addr->v6.sin6_scope_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 0;
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700814 dev = dev_get_by_index(addr->v6.sin6_scope_id);
815 if (!dev)
816 return 0;
817 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 }
819 af = opt->pf->af;
820 }
821 return af->available(addr, opt);
822}
823
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700824/* Verify that the provided sockaddr looks sendable. Common verification,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 * has already been taken care of.
826 */
827static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
828{
829 struct sctp_af *af = NULL;
830
831 /* ASSERT: address family has already been verified. */
832 if (addr->sa.sa_family != AF_INET6)
833 af = sctp_get_af_specific(addr->sa.sa_family);
834 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 int type = ipv6_addr_type(&addr->v6.sin6_addr);
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700836 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700838 if (type & IPV6_ADDR_LINKLOCAL) {
839 if (!addr->v6.sin6_scope_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 return 0;
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700841 dev = dev_get_by_index(addr->v6.sin6_scope_id);
842 if (!dev)
843 return 0;
844 dev_put(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 }
846 af = opt->pf->af;
847 }
848
849 return af != NULL;
850}
851
852/* Fill in Supported Address Type information for INIT and INIT-ACK
853 * chunks. Note: In the future, we may want to look at sock options
854 * to determine whether a PF_INET6 socket really wants to have IPV4
855 * addresses.
856 * Returns number of addresses supported.
857 */
858static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
859 __u16 *types)
860{
861 types[0] = SCTP_PARAM_IPV4_ADDRESS;
862 types[1] = SCTP_PARAM_IPV6_ADDRESS;
863 return 2;
864}
865
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800866static const struct proto_ops inet6_seqpacket_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800867 .family = PF_INET6,
868 .owner = THIS_MODULE,
869 .release = inet6_release,
870 .bind = inet6_bind,
871 .connect = inet_dgram_connect,
872 .socketpair = sock_no_socketpair,
873 .accept = inet_accept,
874 .getname = inet6_getname,
875 .poll = sctp_poll,
876 .ioctl = inet6_ioctl,
877 .listen = sctp_inet_listen,
878 .shutdown = inet_shutdown,
879 .setsockopt = sock_common_setsockopt,
880 .getsockopt = sock_common_getsockopt,
881 .sendmsg = inet_sendmsg,
882 .recvmsg = sock_common_recvmsg,
883 .mmap = sock_no_mmap,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800884#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800885 .compat_setsockopt = compat_sock_common_setsockopt,
886 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800887#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888};
889
890static struct inet_protosw sctpv6_seqpacket_protosw = {
891 .type = SOCK_SEQPACKET,
892 .protocol = IPPROTO_SCTP,
893 .prot = &sctpv6_prot,
894 .ops = &inet6_seqpacket_ops,
895 .capability = -1,
896 .no_check = 0,
897 .flags = SCTP_PROTOSW_FLAG
898};
899static struct inet_protosw sctpv6_stream_protosw = {
900 .type = SOCK_STREAM,
901 .protocol = IPPROTO_SCTP,
902 .prot = &sctpv6_prot,
903 .ops = &inet6_seqpacket_ops,
904 .capability = -1,
905 .no_check = 0,
906 .flags = SCTP_PROTOSW_FLAG,
907};
908
Patrick McHardy951dbc82006-01-06 23:02:34 -0800909static int sctp6_rcv(struct sk_buff **pskb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910{
911 return sctp_rcv(*pskb) ? -1 : 0;
912}
913
914static struct inet6_protocol sctpv6_protocol = {
915 .handler = sctp6_rcv,
916 .err_handler = sctp_v6_err,
917 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
918};
919
920static struct sctp_af sctp_ipv6_specific = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800921 .sa_family = AF_INET6,
922 .sctp_xmit = sctp_v6_xmit,
923 .setsockopt = ipv6_setsockopt,
924 .getsockopt = ipv6_getsockopt,
925 .get_dst = sctp_v6_get_dst,
926 .get_saddr = sctp_v6_get_saddr,
927 .copy_addrlist = sctp_v6_copy_addrlist,
928 .from_skb = sctp_v6_from_skb,
929 .from_sk = sctp_v6_from_sk,
930 .to_sk_saddr = sctp_v6_to_sk_saddr,
931 .to_sk_daddr = sctp_v6_to_sk_daddr,
932 .from_addr_param = sctp_v6_from_addr_param,
933 .to_addr_param = sctp_v6_to_addr_param,
934 .dst_saddr = sctp_v6_dst_saddr,
935 .cmp_addr = sctp_v6_cmp_addr,
936 .scope = sctp_v6_scope,
937 .addr_valid = sctp_v6_addr_valid,
938 .inaddr_any = sctp_v6_inaddr_any,
939 .is_any = sctp_v6_is_any,
940 .available = sctp_v6_available,
941 .skb_iif = sctp_v6_skb_iif,
942 .is_ce = sctp_v6_is_ce,
943 .seq_dump_addr = sctp_v6_seq_dump_addr,
944 .net_header_len = sizeof(struct ipv6hdr),
945 .sockaddr_len = sizeof(struct sockaddr_in6),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800946#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800947 .compat_setsockopt = compat_ipv6_setsockopt,
948 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800949#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950};
951
952static struct sctp_pf sctp_pf_inet6_specific = {
953 .event_msgname = sctp_inet6_event_msgname,
954 .skb_msgname = sctp_inet6_skb_msgname,
955 .af_supported = sctp_inet6_af_supported,
956 .cmp_addr = sctp_inet6_cmp_addr,
957 .bind_verify = sctp_inet6_bind_verify,
958 .send_verify = sctp_inet6_send_verify,
959 .supported_addrs = sctp_inet6_supported_addrs,
960 .create_accept_sk = sctp_v6_create_accept_sk,
961 .addr_v4map = sctp_v6_addr_v4map,
962 .af = &sctp_ipv6_specific,
963};
964
965/* Initialize IPv6 support and register with inet6 stack. */
966int sctp_v6_init(void)
967{
968 int rc = proto_register(&sctpv6_prot, 1);
969
970 if (rc)
971 goto out;
972 /* Register inet6 protocol. */
973 rc = -EAGAIN;
974 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
975 goto out_unregister_sctp_proto;
976
977 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
978 inet6_register_protosw(&sctpv6_seqpacket_protosw);
979 inet6_register_protosw(&sctpv6_stream_protosw);
980
981 /* Register the SCTP specific PF_INET6 functions. */
982 sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
983
984 /* Register the SCTP specific AF_INET6 functions. */
985 sctp_register_af(&sctp_ipv6_specific);
986
987 /* Register notifier for inet6 address additions/deletions. */
988 register_inet6addr_notifier(&sctp_inet6addr_notifier);
989 rc = 0;
990out:
991 return rc;
992out_unregister_sctp_proto:
993 proto_unregister(&sctpv6_prot);
994 goto out;
995}
996
997/* IPv6 specific exit support. */
998void sctp_v6_exit(void)
999{
1000 list_del(&sctp_ipv6_specific.list);
1001 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1002 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1003 inet6_unregister_protosw(&sctpv6_stream_protosw);
1004 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
1005 proto_unregister(&sctpv6_prot);
1006}