blob: a1913a4b6f3a0fedf5031f2390b1595ea0171742 [file] [log] [blame]
Vlad Yasevich60c778b2008-01-11 09:57:09 -05001/* SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * (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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -05007 * This file is part of the SCTP kernel implementation
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * SCTP over IPv6.
10 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050011 * This SCTP implementation is free software;
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 * 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 *
Vlad Yasevich60c778b2008-01-11 09:57:09 -050017 * This SCTP implementation is distributed in the hope that it
Linus Torvalds1da177e2005-04-16 15:20:36 -070018 * 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
Joe Perches145ce502010-08-24 13:21:08 +000050#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#include <linux/module.h>
53#include <linux/errno.h>
54#include <linux/types.h>
55#include <linux/socket.h>
56#include <linux/sockios.h>
57#include <linux/net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058#include <linux/in.h>
59#include <linux/in6.h>
60#include <linux/netdevice.h>
61#include <linux/init.h>
62#include <linux/ipsec.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090063#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
65#include <linux/ipv6.h>
66#include <linux/icmpv6.h>
67#include <linux/random.h>
68#include <linux/seq_file.h>
69
70#include <net/protocol.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <net/ndisc.h>
Arnaldo Carvalho de Meloc752f072005-08-09 20:08:28 -070072#include <net/ip.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <net/ipv6.h>
74#include <net/transp_v6.h>
75#include <net/addrconf.h>
76#include <net/ip6_route.h>
77#include <net/inet_common.h>
78#include <net/inet_ecn.h>
79#include <net/sctp/sctp.h>
80
81#include <asm/uaccess.h>
82
Weixing Shi62503412011-04-26 21:36:32 +000083static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
84 union sctp_addr *s2);
Vlad Yasevich9914ae32011-04-26 21:51:31 +000085static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
86 __be16 port);
87static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
88 const union sctp_addr *addr2);
Weixing Shi62503412011-04-26 21:36:32 +000089
Vlad Yasevich29303542007-09-16 16:02:12 -070090/* Event handler for inet6 address addition/deletion events.
91 * The sctp_local_addr_list needs to be protocted by a spin lock since
92 * multiple notifiers (say IPv4 and IPv6) may be running at the same
93 * time and thus corrupt the list.
94 * The reader side is protected with RCU.
95 */
Adrian Bunk24123182006-12-20 16:08:22 -080096static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
97 void *ptr)
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -080098{
99 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
Vlad Yasevich29303542007-09-16 16:02:12 -0700100 struct sctp_sockaddr_entry *addr = NULL;
101 struct sctp_sockaddr_entry *temp;
Chidambar 'ilLogict' Zinnoury22626212008-03-11 18:05:02 -0700102 int found = 0;
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800103
104 switch (ev) {
105 case NETDEV_UP:
106 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
107 if (addr) {
108 addr->a.v6.sin6_family = AF_INET6;
109 addr->a.v6.sin6_port = 0;
Joe Perches4fe1d582009-01-22 13:49:44 -0800110 ipv6_addr_copy(&addr->a.v6.sin6_addr, &ifa->addr);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800111 addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
Vlad Yasevich29303542007-09-16 16:02:12 -0700112 addr->valid = 1;
113 spin_lock_bh(&sctp_local_addr_lock);
114 list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
115 spin_unlock_bh(&sctp_local_addr_lock);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800116 }
117 break;
118 case NETDEV_DOWN:
Vlad Yasevich29303542007-09-16 16:02:12 -0700119 spin_lock_bh(&sctp_local_addr_lock);
120 list_for_each_entry_safe(addr, temp,
121 &sctp_local_addr_list, list) {
Pavel Emelyanova40a7d12008-04-12 18:40:38 -0700122 if (addr->a.sa.sa_family == AF_INET6 &&
123 ipv6_addr_equal(&addr->a.v6.sin6_addr,
124 &ifa->addr)) {
Chidambar 'ilLogict' Zinnoury22626212008-03-11 18:05:02 -0700125 found = 1;
Vlad Yasevich29303542007-09-16 16:02:12 -0700126 addr->valid = 0;
127 list_del_rcu(&addr->list);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800128 break;
129 }
130 }
Vlad Yasevich29303542007-09-16 16:02:12 -0700131 spin_unlock_bh(&sctp_local_addr_lock);
Chidambar 'ilLogict' Zinnoury22626212008-03-11 18:05:02 -0700132 if (found)
Vlad Yasevich29303542007-09-16 16:02:12 -0700133 call_rcu(&addr->rcu, sctp_local_addr_free);
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800134 break;
135 }
136
137 return NOTIFY_DONE;
138}
139
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static struct notifier_block sctp_inet6addr_notifier = {
Sridhar Samudrala29c7cf92006-12-13 16:26:26 -0800141 .notifier_call = sctp_inet6addr_event,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142};
143
144/* ICMP error handler. */
145SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
Brian Haleyd5fdd6b2009-06-23 04:31:07 -0700146 u8 type, u8 code, int offset, __be32 info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct inet6_dev *idev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 struct sock *sk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 struct sctp_association *asoc;
151 struct sctp_transport *transport;
152 struct ipv6_pinfo *np;
Arnaldo Carvalho de Melo2e07fa92007-04-10 21:22:35 -0700153 sk_buff_data_t saveip, savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 int err;
155
156 idev = in6_dev_get(skb->dev);
157
158 /* Fix up skb to look at the embedded net header. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700159 saveip = skb->network_header;
160 savesctp = skb->transport_header;
Arnaldo Carvalho de Melo1ced98e2007-03-10 19:57:15 -0300161 skb_reset_network_header(skb);
Arnaldo Carvalho de Meloa27ef742007-03-13 17:17:10 -0300162 skb_set_transport_header(skb, offset);
163 sk = sctp_err_lookup(AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 /* Put back, the original pointers. */
Arnaldo Carvalho de Melob0e380b2007-04-10 21:21:55 -0700165 skb->network_header = saveip;
166 skb->transport_header = savesctp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (!sk) {
Denis V. Luneve41b5362008-10-08 10:33:26 -0700168 ICMP6_INC_STATS_BH(dev_net(skb->dev), idev, ICMP6_MIB_INERRORS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 goto out;
170 }
171
172 /* Warning: The sock lock is held. Remember to call
173 * sctp_err_finish!
174 */
175
176 switch (type) {
177 case ICMPV6_PKT_TOOBIG:
178 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
179 goto out_unlock;
180 case ICMPV6_PARAMPROB:
181 if (ICMPV6_UNK_NEXTHDR == code) {
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700182 sctp_icmp_proto_unreachable(sk, asoc, transport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 goto out_unlock;
184 }
185 break;
186 default:
187 break;
188 }
189
190 np = inet6_sk(sk);
191 icmpv6_err_convert(type, code, &err);
192 if (!sock_owned_by_user(sk) && np->recverr) {
193 sk->sk_err = err;
194 sk->sk_error_report(sk);
195 } else { /* Only an error on timeout */
196 sk->sk_err_soft = err;
197 }
198
199out_unlock:
Sridhar Samudralad1ad1ff2005-07-18 13:44:10 -0700200 sctp_err_finish(sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201out:
202 if (likely(idev != NULL))
203 in6_dev_put(idev);
204}
205
206/* Based on tcp_v6_xmit() in tcp_ipv6.c. */
Herbert Xuf8803742008-08-03 21:15:08 -0700207static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
209 struct sock *sk = skb->sk;
210 struct ipv6_pinfo *np = inet6_sk(sk);
David S. Miller4c9483b2011-03-12 16:22:43 -0500211 struct flowi6 fl6;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212
David S. Miller4c9483b2011-03-12 16:22:43 -0500213 memset(&fl6, 0, sizeof(fl6));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
David S. Miller4c9483b2011-03-12 16:22:43 -0500215 fl6.flowi6_proto = sk->sk_protocol;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216
217 /* Fill in the dest address from the route entry passed with the skb
218 * and the source address from the transport.
219 */
David S. Miller4c9483b2011-03-12 16:22:43 -0500220 ipv6_addr_copy(&fl6.daddr, &transport->ipaddr.v6.sin6_addr);
221 ipv6_addr_copy(&fl6.saddr, &transport->saddr.v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
David S. Miller4c9483b2011-03-12 16:22:43 -0500223 fl6.flowlabel = np->flow_label;
224 IP6_ECN_flow_xmit(sk, fl6.flowlabel);
225 if (ipv6_addr_type(&fl6.saddr) & IPV6_ADDR_LINKLOCAL)
226 fl6.flowi6_oif = transport->saddr.v6.sin6_scope_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 else
David S. Miller4c9483b2011-03-12 16:22:43 -0500228 fl6.flowi6_oif = sk->sk_bound_dev_if;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
230 if (np->opt && np->opt->srcrt) {
231 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
David S. Miller4c9483b2011-03-12 16:22:43 -0500232 ipv6_addr_copy(&fl6.daddr, rt0->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 }
234
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700235 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n",
Harvey Harrison0dc47872008-03-05 20:47:47 -0800236 __func__, skb, skb->len,
David S. Miller4c9483b2011-03-12 16:22:43 -0500237 &fl6.saddr, &fl6.daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
240
Herbert Xuf8803742008-08-03 21:15:08 -0700241 if (!(transport->param_flags & SPP_PMTUD_ENABLE))
242 skb->local_df = 1;
243
David S. Miller4c9483b2011-03-12 16:22:43 -0500244 return ip6_xmit(sk, skb, &fl6, np->opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
247/* Returns the dst cache entry for the given source and destination ip
248 * addresses.
249 */
250static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
251 union sctp_addr *daddr,
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000252 union sctp_addr *saddr,
253 struct flowi *fl,
254 struct sock *sk)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
Weixing Shi62503412011-04-26 21:36:32 +0000256 struct dst_entry *dst = NULL;
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000257 struct flowi6 *fl6 = &fl->u.ip6;
Weixing Shi62503412011-04-26 21:36:32 +0000258 struct sctp_bind_addr *bp;
259 struct sctp_sockaddr_entry *laddr;
260 union sctp_addr *baddr = NULL;
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000261 union sctp_addr dst_saddr;
Weixing Shi62503412011-04-26 21:36:32 +0000262 __u8 matchlen = 0;
263 __u8 bmatchlen;
264 sctp_scope_t scope;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000266 memset(fl6, 0, sizeof(struct flowi6));
267 ipv6_addr_copy(&fl6->daddr, &daddr->v6.sin6_addr);
Vlad Yasevich9c6a02f2011-04-26 21:52:27 +0000268 fl6->fl6_dport = daddr->v6.sin6_port;
269 fl6->flowi6_proto = IPPROTO_SCTP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000271 fl6->flowi6_oif = daddr->v6.sin6_scope_id;
YOSHIFUJI Hideakid808ad92007-02-09 23:25:18 +0900272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000274 SCTP_DEBUG_PRINTK("%s: DST=%pI6 ", __func__, &fl6->daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Vlad Yasevich9c6a02f2011-04-26 21:52:27 +0000276 if (asoc)
277 fl6->fl6_sport = htons(asoc->base.bind_addr.port);
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 if (saddr) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000280 ipv6_addr_copy(&fl6->saddr, &saddr->v6.sin6_addr);
Vlad Yasevich9c6a02f2011-04-26 21:52:27 +0000281 fl6->fl6_sport = saddr->v6.sin6_port;
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000282 SCTP_DEBUG_PRINTK("SRC=%pI6 - ", &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
284
Vlad Yasevich9c6a02f2011-04-26 21:52:27 +0000285 dst = ip6_dst_lookup_flow(sk, fl6, NULL, false);
Weixing Shi62503412011-04-26 21:36:32 +0000286 if (!asoc || saddr)
287 goto out;
288
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000289 bp = &asoc->base.bind_addr;
290 scope = sctp_scope(daddr);
291 /* ip6_dst_lookup has filled in the fl6->saddr for us. Check
292 * to see if we can use it.
293 */
Vlad Yasevich9c6a02f2011-04-26 21:52:27 +0000294 if (!IS_ERR(dst)) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000295 /* Walk through the bind address list and look for a bind
296 * address that matches the source address of the returned dst.
Weixing Shi62503412011-04-26 21:36:32 +0000297 */
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000298 sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
Weixing Shi62503412011-04-26 21:36:32 +0000299 rcu_read_lock();
300 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000301 if (!laddr->valid || (laddr->state != SCTP_ADDR_SRC))
Weixing Shi62503412011-04-26 21:36:32 +0000302 continue;
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000303
304 /* Do not compare against v4 addrs */
305 if ((laddr->a.sa.sa_family == AF_INET6) &&
306 (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
307 rcu_read_unlock();
308 goto out;
Weixing Shi62503412011-04-26 21:36:32 +0000309 }
310 }
311 rcu_read_unlock();
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000312 /* None of the bound addresses match the source address of the
313 * dst. So release it.
314 */
315 dst_release(dst);
316 dst = NULL;
317 }
318
319 /* Walk through the bind address list and try to get the
320 * best source address for a given destination.
321 */
322 rcu_read_lock();
323 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
324 if (!laddr->valid && laddr->state != SCTP_ADDR_SRC)
325 continue;
326 if ((laddr->a.sa.sa_family == AF_INET6) &&
327 (scope <= sctp_scope(&laddr->a))) {
328 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
329 if (!baddr || (matchlen < bmatchlen)) {
330 baddr = &laddr->a;
331 matchlen = bmatchlen;
332 }
Weixing Shi62503412011-04-26 21:36:32 +0000333 }
334 }
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000335 rcu_read_unlock();
336 if (baddr) {
337 ipv6_addr_copy(&fl6->saddr, &baddr->v6.sin6_addr);
Vlad Yasevich9c6a02f2011-04-26 21:52:27 +0000338 fl6->fl6_sport = baddr->v6.sin6_port;
339 dst = ip6_dst_lookup_flow(sk, fl6, NULL, false);
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000340 }
341
Weixing Shi62503412011-04-26 21:36:32 +0000342out:
Vlad Yasevich9c6a02f2011-04-26 21:52:27 +0000343 if (!IS_ERR(dst)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct rt6_info *rt;
345 rt = (struct rt6_info *)dst;
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700346 SCTP_DEBUG_PRINTK("rt6_dst:%pI6 rt6_src:%pI6\n",
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000347 &rt->rt6i_dst.addr, &fl6->saddr);
Ville Nuorvala42513202006-10-16 22:10:05 -0700348 return dst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Ville Nuorvala42513202006-10-16 22:10:05 -0700350 SCTP_DEBUG_PRINTK("NO ROUTE\n");
Ville Nuorvala42513202006-10-16 22:10:05 -0700351 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353
354/* Returns the number of consecutive initial bits that match in the 2 ipv6
355 * addresses.
356 */
357static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
358 union sctp_addr *s2)
359{
YOSHIFUJI Hideaki / 吉藤英明de7737e2010-03-26 08:34:30 +0000360 return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
363/* Fills in the source address(saddr) based on the destination address(daddr)
364 * and asoc's bind address list.
365 */
YOSHIFUJI Hideakie5117102008-05-29 19:55:05 +0900366static void sctp_v6_get_saddr(struct sctp_sock *sk,
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000367 struct sctp_transport *t,
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000368 struct flowi *fl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369{
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000370 struct flowi6 *fl6 = &fl->u.ip6;
371 union sctp_addr *saddr = &t->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Vlad Yasevichaf138472011-04-26 21:53:20 +0000373 SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000374
375 if (t->dst) {
376 saddr->v6.sin6_family = AF_INET6;
377 ipv6_addr_copy(&saddr->v6.sin6_addr, &fl6->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379}
380
381/* Make a copy of all potential local addresses. */
382static void sctp_v6_copy_addrlist(struct list_head *addrlist,
383 struct net_device *dev)
384{
385 struct inet6_dev *in6_dev;
386 struct inet6_ifaddr *ifp;
387 struct sctp_sockaddr_entry *addr;
388
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700389 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700391 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 return;
393 }
394
Jarek Poplawskie2eb8d42007-03-08 14:43:41 -0800395 read_lock_bh(&in6_dev->lock);
stephen hemminger502a2ff2010-03-17 20:31:13 +0000396 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 /* Add the address to the local list. */
398 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
399 if (addr) {
Al Viro2a6fd782006-11-20 17:04:42 -0800400 addr->a.v6.sin6_family = AF_INET6;
401 addr->a.v6.sin6_port = 0;
Brian Haley486f50c2010-04-03 15:10:21 -0700402 ipv6_addr_copy(&addr->a.v6.sin6_addr, &ifp->addr);
Al Viro2a6fd782006-11-20 17:04:42 -0800403 addr->a.v6.sin6_scope_id = dev->ifindex;
Vlad Yasevich29303542007-09-16 16:02:12 -0700404 addr->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 INIT_LIST_HEAD(&addr->list);
406 list_add_tail(&addr->list, addrlist);
407 }
408 }
409
Jarek Poplawskie2eb8d42007-03-08 14:43:41 -0800410 read_unlock_bh(&in6_dev->lock);
YOSHIFUJI Hideaki8814c4b2006-09-22 14:44:24 -0700411 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
414/* Initialize a sockaddr_storage from in incoming skb. */
415static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
416 int is_saddr)
417{
418 void *from;
Al Virod55c41b2006-11-20 17:09:40 -0800419 __be16 *port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 struct sctphdr *sh;
421
422 port = &addr->v6.sin6_port;
423 addr->v6.sin6_family = AF_INET6;
424 addr->v6.sin6_flowinfo = 0; /* FIXME */
425 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
426
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300427 sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 if (is_saddr) {
Al Virod55c41b2006-11-20 17:09:40 -0800429 *port = sh->source;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700430 from = &ipv6_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 } else {
Al Virod55c41b2006-11-20 17:09:40 -0800432 *port = sh->dest;
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700433 from = &ipv6_hdr(skb)->daddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 ipv6_addr_copy(&addr->v6.sin6_addr, from);
436}
437
438/* Initialize an sctp_addr from a socket. */
439static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
440{
441 addr->v6.sin6_family = AF_INET6;
Al Viro7dcdbd92006-11-20 17:24:21 -0800442 addr->v6.sin6_port = 0;
Brian Haley486f50c2010-04-03 15:10:21 -0700443 ipv6_addr_copy(&addr->v6.sin6_addr, &inet6_sk(sk)->rcv_saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446/* Initialize sk->sk_rcv_saddr from sctp_addr. */
447static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
448{
449 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
450 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
451 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
452 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
453 inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
454 addr->v4.sin_addr.s_addr;
455 } else {
Brian Haley486f50c2010-04-03 15:10:21 -0700456 ipv6_addr_copy(&inet6_sk(sk)->rcv_saddr, &addr->v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458}
459
460/* Initialize sk->sk_daddr from sctp_addr. */
461static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
462{
463 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
464 inet6_sk(sk)->daddr.s6_addr32[0] = 0;
465 inet6_sk(sk)->daddr.s6_addr32[1] = 0;
466 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
467 inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
468 } else {
Brian Haley486f50c2010-04-03 15:10:21 -0700469 ipv6_addr_copy(&inet6_sk(sk)->daddr, &addr->v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 }
471}
472
473/* Initialize a sctp_addr from an address parameter. */
474static void sctp_v6_from_addr_param(union sctp_addr *addr,
475 union sctp_addr_param *param,
Al Virodd86d132006-11-20 17:11:13 -0800476 __be16 port, int iif)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
478 addr->v6.sin6_family = AF_INET6;
479 addr->v6.sin6_port = port;
480 addr->v6.sin6_flowinfo = 0; /* BUG */
481 ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
482 addr->v6.sin6_scope_id = iif;
483}
484
485/* Initialize an address parameter from a sctp_addr and return the length
486 * of the address parameter.
487 */
488static int sctp_v6_to_addr_param(const union sctp_addr *addr,
489 union sctp_addr_param *param)
490{
491 int length = sizeof(sctp_ipv6addr_param_t);
492
493 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
Al Virodbc16db2006-11-20 17:01:42 -0800494 param->v6.param_hdr.length = htons(length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
496
497 return length;
498}
499
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000500/* Initialize a sctp_addr from struct in6_addr. */
501static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
Al Viro854d43a2006-11-20 17:06:24 -0800502 __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 addr->sa.sa_family = AF_INET6;
505 addr->v6.sin6_port = port;
Vlad Yasevich9914ae32011-04-26 21:51:31 +0000506 ipv6_addr_copy(&addr->v6.sin6_addr, saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507}
508
509/* Compare addresses exactly.
510 * v4-mapped-v6 is also in consideration.
511 */
512static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
513 const union sctp_addr *addr2)
514{
515 if (addr1->sa.sa_family != addr2->sa.sa_family) {
516 if (addr1->sa.sa_family == AF_INET &&
517 addr2->sa.sa_family == AF_INET6 &&
Brian Haleye773e4f2007-08-24 23:16:08 -0700518 ipv6_addr_v4mapped(&addr2->v6.sin6_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (addr2->v6.sin6_port == addr1->v4.sin_port &&
520 addr2->v6.sin6_addr.s6_addr32[3] ==
521 addr1->v4.sin_addr.s_addr)
522 return 1;
523 }
524 if (addr2->sa.sa_family == AF_INET &&
525 addr1->sa.sa_family == AF_INET6 &&
Brian Haleye773e4f2007-08-24 23:16:08 -0700526 ipv6_addr_v4mapped(&addr1->v6.sin6_addr)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 if (addr1->v6.sin6_port == addr2->v4.sin_port &&
528 addr1->v6.sin6_addr.s6_addr32[3] ==
529 addr2->v4.sin_addr.s_addr)
530 return 1;
531 }
532 return 0;
533 }
534 if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
535 return 0;
536 /* If this is a linklocal address, compare the scope_id. */
537 if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
538 if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
539 (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
540 return 0;
541 }
542 }
543
544 return 1;
545}
546
547/* Initialize addr struct to INADDR_ANY. */
Al Viro6fbfa9f2006-11-20 17:24:53 -0800548static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549{
550 memset(addr, 0x00, sizeof(union sctp_addr));
551 addr->v6.sin6_family = AF_INET6;
552 addr->v6.sin6_port = port;
553}
554
555/* Is this a wildcard address? */
556static int sctp_v6_is_any(const union sctp_addr *addr)
557{
Brian Haleyb9b9e102005-04-28 11:59:16 -0700558 return ipv6_addr_any(&addr->v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
561/* Should this be available for binding? */
562static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
563{
564 int type;
Eric Dumazetb71d1d42011-04-22 04:53:02 +0000565 const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
567 type = ipv6_addr_type(in6);
568 if (IPV6_ADDR_ANY == type)
569 return 1;
570 if (type == IPV6_ADDR_MAPPED) {
571 if (sp && !sp->v4mapped)
572 return 0;
573 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
574 return 0;
575 sctp_v6_map_v4(addr);
576 return sctp_get_af_specific(AF_INET)->available(addr, sp);
577 }
578 if (!(type & IPV6_ADDR_UNICAST))
579 return 0;
580
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800581 return ipv6_chk_addr(&init_net, in6, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582}
583
584/* This function checks if the address is a valid address to be used for
585 * SCTP.
586 *
587 * Output:
588 * Return 0 - If the address is a non-unicast or an illegal address.
589 * Return 1 - If the address is a unicast.
590 */
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700591static int sctp_v6_addr_valid(union sctp_addr *addr,
592 struct sctp_sock *sp,
593 const struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
595 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
596
597 /* Support v4-mapped-v6 address. */
598 if (ret == IPV6_ADDR_MAPPED) {
599 /* Note: This routine is used in input, so v4-mapped-v6
600 * are disallowed here when there is no sctp_sock.
601 */
602 if (!sp || !sp->v4mapped)
603 return 0;
604 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
605 return 0;
606 sctp_v6_map_v4(addr);
Vlad Yasevich5636bef2006-06-17 22:55:35 -0700607 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 }
609
610 /* Is this a non-unicast address */
611 if (!(ret & IPV6_ADDR_UNICAST))
612 return 0;
613
614 return 1;
615}
616
617/* What is the scope of 'addr'? */
618static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
619{
620 int v6scope;
621 sctp_scope_t retval;
622
623 /* The IPv6 scope is really a set of bit fields.
624 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
625 */
626
627 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
628 switch (v6scope) {
629 case IFA_HOST:
630 retval = SCTP_SCOPE_LOOPBACK;
631 break;
632 case IFA_LINK:
633 retval = SCTP_SCOPE_LINK;
634 break;
635 case IFA_SITE:
636 retval = SCTP_SCOPE_PRIVATE;
637 break;
638 default:
639 retval = SCTP_SCOPE_GLOBAL;
640 break;
Stephen Hemminger3ff50b72007-04-20 17:09:22 -0700641 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 return retval;
644}
645
646/* Create and initialize a new sk for the socket to be returned by accept(). */
647static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
648 struct sctp_association *asoc)
649{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 struct sock *newsk;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
652 struct sctp6_sock *newsctp6sk;
653
YOSHIFUJI Hideaki3b1e0a62008-03-26 02:26:21 +0900654 newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!newsk)
656 goto out;
657
658 sock_init_data(NULL, newsk);
659
Vlad Yasevich914e1c82009-02-13 08:33:44 +0000660 sctp_copy_sock(newsk, sk, asoc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 sock_reset_flag(sk, SOCK_ZAPPED);
662
663 newsctp6sk = (struct sctp6_sock *)newsk;
664 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
665
Dave Johnsonb225b882007-07-25 19:49:29 -0400666 sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
667
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 newnp = inet6_sk(newsk);
669
670 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
671
672 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
673 * and getpeername().
674 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
676
Arnaldo Carvalho de Meloe6848972005-08-09 19:45:38 -0700677 sk_refcnt_debug_inc(newsk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
679 if (newsk->sk_prot->init(newsk)) {
680 sk_common_release(newsk);
681 newsk = NULL;
682 }
683
684out:
685 return newsk;
686}
687
688/* Map v4 address to mapped v6 address */
689static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
690{
691 if (sp->v4mapped && AF_INET == addr->sa.sa_family)
692 sctp_v4_map_v6(addr);
693}
694
695/* Where did this skb come from? */
696static int sctp_v6_skb_iif(const struct sk_buff *skb)
697{
698 struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
699 return opt->iif;
700}
701
702/* Was this packet marked by Explicit Congestion Notification? */
703static int sctp_v6_is_ce(const struct sk_buff *skb)
704{
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700705 return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
708/* Dump the v6 addr to the seq file. */
709static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
710{
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700711 seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712}
713
Vlad Yasevichb9031d92008-06-04 12:40:15 -0700714static void sctp_v6_ecn_capable(struct sock *sk)
715{
716 inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
717}
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/* Initialize a PF_INET6 socket msg_name. */
720static void sctp_inet6_msgname(char *msgname, int *addr_len)
721{
722 struct sockaddr_in6 *sin6;
723
724 sin6 = (struct sockaddr_in6 *)msgname;
725 sin6->sin6_family = AF_INET6;
726 sin6->sin6_flowinfo = 0;
727 sin6->sin6_scope_id = 0; /*FIXME */
728 *addr_len = sizeof(struct sockaddr_in6);
729}
730
731/* Initialize a PF_INET msgname from a ulpevent. */
732static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
733 char *msgname, int *addrlen)
734{
735 struct sockaddr_in6 *sin6, *sin6from;
736
737 if (msgname) {
738 union sctp_addr *addr;
739 struct sctp_association *asoc;
740
741 asoc = event->asoc;
742 sctp_inet6_msgname(msgname, addrlen);
743 sin6 = (struct sockaddr_in6 *)msgname;
744 sin6->sin6_port = htons(asoc->peer.port);
745 addr = &asoc->peer.primary_addr;
746
747 /* Note: If we go to a common v6 format, this code
748 * will change.
749 */
750
751 /* Map ipv4 address into v4-mapped-on-v6 address. */
752 if (sctp_sk(asoc->base.sk)->v4mapped &&
753 AF_INET == addr->sa.sa_family) {
754 sctp_v4_map_v6((union sctp_addr *)sin6);
755 sin6->sin6_addr.s6_addr32[3] =
756 addr->v4.sin_addr.s_addr;
757 return;
758 }
759
760 sin6from = &asoc->peer.primary_addr.v6;
761 ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
762 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
763 sin6->sin6_scope_id = sin6from->sin6_scope_id;
764 }
765}
766
767/* Initialize a msg_name from an inbound skb. */
768static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
769 int *addr_len)
770{
771 struct sctphdr *sh;
772 struct sockaddr_in6 *sin6;
773
774 if (msgname) {
775 sctp_inet6_msgname(msgname, addr_len);
776 sin6 = (struct sockaddr_in6 *)msgname;
Arnaldo Carvalho de Melo2c0fd382007-03-13 13:59:32 -0300777 sh = sctp_hdr(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 sin6->sin6_port = sh->source;
779
780 /* Map ipv4 address into v4-mapped-on-v6 address. */
781 if (sctp_sk(skb->sk)->v4mapped &&
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700782 ip_hdr(skb)->version == 4) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 sctp_v4_map_v6((union sctp_addr *)sin6);
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -0700784 sin6->sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 return;
786 }
787
788 /* Otherwise, just copy the v6 address. */
Arnaldo Carvalho de Melo0660e032007-04-25 17:54:47 -0700789 ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
791 struct sctp_ulpevent *ev = sctp_skb2event(skb);
792 sin6->sin6_scope_id = ev->iif;
793 }
794 }
795}
796
797/* Do we support this AF? */
798static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
799{
800 switch (family) {
801 case AF_INET6:
802 return 1;
803 /* v4-mapped-v6 addresses */
804 case AF_INET:
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700805 if (!__ipv6_only_sock(sctp_opt2sk(sp)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 return 1;
807 default:
808 return 0;
809 }
810}
811
812/* Address matching with wildcards allowed. This extra level
813 * of indirection lets us choose whether a PF_INET6 should
814 * disallow any v4 addresses if we so choose.
815 */
816static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
817 const union sctp_addr *addr2,
818 struct sctp_sock *opt)
819{
820 struct sctp_af *af1, *af2;
Vlad Yasevich52cae8f2008-08-18 10:34:34 -0400821 struct sock *sk = sctp_opt2sk(opt);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 af1 = sctp_get_af_specific(addr1->sa.sa_family);
824 af2 = sctp_get_af_specific(addr2->sa.sa_family);
825
826 if (!af1 || !af2)
827 return 0;
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700828
829 /* If the socket is IPv6 only, v4 addrs will not match */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -0400830 if (__ipv6_only_sock(sk) && af1 != af2)
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700831 return 0;
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 /* Today, wildcard AF_INET/AF_INET6. */
Vlad Yasevich52cae8f2008-08-18 10:34:34 -0400834 if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 return 1;
836
837 if (addr1->sa.sa_family != addr2->sa.sa_family)
838 return 0;
839
840 return af1->cmp_addr(addr1, addr2);
841}
842
843/* Verify that the provided sockaddr looks bindable. Common verification,
844 * has already been taken care of.
845 */
846static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
847{
848 struct sctp_af *af;
849
850 /* ASSERT: address family has already been verified. */
851 if (addr->sa.sa_family != AF_INET6)
852 af = sctp_get_af_specific(addr->sa.sa_family);
853 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854 int type = ipv6_addr_type(&addr->v6.sin6_addr);
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700855 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700857 if (type & IPV6_ADDR_LINKLOCAL) {
858 if (!addr->v6.sin6_scope_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 return 0;
Eric Dumazet40c9c312009-11-05 20:56:46 -0800860 rcu_read_lock();
861 dev = dev_get_by_index_rcu(&init_net,
862 addr->v6.sin6_scope_id);
863 if (!dev ||
864 !ipv6_chk_addr(&init_net, &addr->v6.sin6_addr,
Daniel Lezcanobfeade02008-01-10 22:43:18 -0800865 dev, 0)) {
Eric Dumazet40c9c312009-11-05 20:56:46 -0800866 rcu_read_unlock();
Vlad Yasevich1669d852007-07-03 14:29:23 -0400867 return 0;
868 }
Eric Dumazet40c9c312009-11-05 20:56:46 -0800869 rcu_read_unlock();
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700870 } else if (type == IPV6_ADDR_MAPPED) {
871 if (!opt->v4mapped)
872 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 af = opt->pf->af;
876 }
877 return af->available(addr, opt);
878}
879
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700880/* Verify that the provided sockaddr looks sendable. Common verification,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 * has already been taken care of.
882 */
883static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
884{
885 struct sctp_af *af = NULL;
886
887 /* ASSERT: address family has already been verified. */
888 if (addr->sa.sa_family != AF_INET6)
889 af = sctp_get_af_specific(addr->sa.sa_family);
890 else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 int type = ipv6_addr_type(&addr->v6.sin6_addr);
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700892 struct net_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700894 if (type & IPV6_ADDR_LINKLOCAL) {
895 if (!addr->v6.sin6_scope_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 return 0;
Eric Dumazet40c9c312009-11-05 20:56:46 -0800897 rcu_read_lock();
898 dev = dev_get_by_index_rcu(&init_net,
899 addr->v6.sin6_scope_id);
900 rcu_read_unlock();
Sridhar Samudrala6a6ddb22005-06-13 15:13:05 -0700901 if (!dev)
902 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 }
904 af = opt->pf->af;
905 }
906
907 return af != NULL;
908}
909
910/* Fill in Supported Address Type information for INIT and INIT-ACK
911 * chunks. Note: In the future, we may want to look at sock options
912 * to determine whether a PF_INET6 socket really wants to have IPV4
913 * addresses.
914 * Returns number of addresses supported.
915 */
916static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
Al Viro3dbe8652006-11-20 17:25:49 -0800917 __be16 *types)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918{
Vlad Yasevich7dab83d2008-07-18 23:05:40 -0700919 types[0] = SCTP_PARAM_IPV6_ADDRESS;
920 if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
921 types[1] = SCTP_PARAM_IPV4_ADDRESS;
922 return 2;
923 }
924 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925}
926
Eric Dumazet90ddc4f2005-12-22 12:49:22 -0800927static const struct proto_ops inet6_seqpacket_ops = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800928 .family = PF_INET6,
929 .owner = THIS_MODULE,
930 .release = inet6_release,
931 .bind = inet6_bind,
932 .connect = inet_dgram_connect,
933 .socketpair = sock_no_socketpair,
934 .accept = inet_accept,
935 .getname = inet6_getname,
936 .poll = sctp_poll,
937 .ioctl = inet6_ioctl,
938 .listen = sctp_inet_listen,
939 .shutdown = inet_shutdown,
940 .setsockopt = sock_common_setsockopt,
941 .getsockopt = sock_common_getsockopt,
942 .sendmsg = inet_sendmsg,
943 .recvmsg = sock_common_recvmsg,
944 .mmap = sock_no_mmap,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800945#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800946 .compat_setsockopt = compat_sock_common_setsockopt,
947 .compat_getsockopt = compat_sock_common_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -0800948#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949};
950
951static struct inet_protosw sctpv6_seqpacket_protosw = {
952 .type = SOCK_SEQPACKET,
953 .protocol = IPPROTO_SCTP,
954 .prot = &sctpv6_prot,
955 .ops = &inet6_seqpacket_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 .no_check = 0,
957 .flags = SCTP_PROTOSW_FLAG
958};
959static struct inet_protosw sctpv6_stream_protosw = {
960 .type = SOCK_STREAM,
961 .protocol = IPPROTO_SCTP,
962 .prot = &sctpv6_prot,
963 .ops = &inet6_seqpacket_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 .no_check = 0,
965 .flags = SCTP_PROTOSW_FLAG,
966};
967
Herbert Xue5bbef22007-10-15 12:50:28 -0700968static int sctp6_rcv(struct sk_buff *skb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969{
Herbert Xue5bbef22007-10-15 12:50:28 -0700970 return sctp_rcv(skb) ? -1 : 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971}
972
Alexey Dobriyan41135cc2009-09-14 12:22:28 +0000973static const struct inet6_protocol sctpv6_protocol = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 .handler = sctp6_rcv,
975 .err_handler = sctp_v6_err,
976 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
977};
978
Neil Horman15efbe72008-02-15 09:53:59 -0500979static struct sctp_af sctp_af_inet6 = {
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800980 .sa_family = AF_INET6,
981 .sctp_xmit = sctp_v6_xmit,
982 .setsockopt = ipv6_setsockopt,
983 .getsockopt = ipv6_getsockopt,
984 .get_dst = sctp_v6_get_dst,
985 .get_saddr = sctp_v6_get_saddr,
986 .copy_addrlist = sctp_v6_copy_addrlist,
987 .from_skb = sctp_v6_from_skb,
988 .from_sk = sctp_v6_from_sk,
989 .to_sk_saddr = sctp_v6_to_sk_saddr,
990 .to_sk_daddr = sctp_v6_to_sk_daddr,
991 .from_addr_param = sctp_v6_from_addr_param,
992 .to_addr_param = sctp_v6_to_addr_param,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -0800993 .cmp_addr = sctp_v6_cmp_addr,
994 .scope = sctp_v6_scope,
995 .addr_valid = sctp_v6_addr_valid,
996 .inaddr_any = sctp_v6_inaddr_any,
997 .is_any = sctp_v6_is_any,
998 .available = sctp_v6_available,
999 .skb_iif = sctp_v6_skb_iif,
1000 .is_ce = sctp_v6_is_ce,
1001 .seq_dump_addr = sctp_v6_seq_dump_addr,
Vlad Yasevichb9031d92008-06-04 12:40:15 -07001002 .ecn_capable = sctp_v6_ecn_capable,
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001003 .net_header_len = sizeof(struct ipv6hdr),
1004 .sockaddr_len = sizeof(struct sockaddr_in6),
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001005#ifdef CONFIG_COMPAT
Arnaldo Carvalho de Melo543d9cf2006-03-20 22:48:35 -08001006 .compat_setsockopt = compat_ipv6_setsockopt,
1007 .compat_getsockopt = compat_ipv6_getsockopt,
Dmitry Mishin3fdadf72006-03-20 22:45:21 -08001008#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009};
1010
Neil Horman15efbe72008-02-15 09:53:59 -05001011static struct sctp_pf sctp_pf_inet6 = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 .event_msgname = sctp_inet6_event_msgname,
1013 .skb_msgname = sctp_inet6_skb_msgname,
1014 .af_supported = sctp_inet6_af_supported,
1015 .cmp_addr = sctp_inet6_cmp_addr,
1016 .bind_verify = sctp_inet6_bind_verify,
1017 .send_verify = sctp_inet6_send_verify,
1018 .supported_addrs = sctp_inet6_supported_addrs,
1019 .create_accept_sk = sctp_v6_create_accept_sk,
1020 .addr_v4map = sctp_v6_addr_v4map,
Neil Horman15efbe72008-02-15 09:53:59 -05001021 .af = &sctp_af_inet6,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022};
1023
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001024/* Initialize IPv6 support and register with socket layer. */
Vlad Yasevich270637a2008-03-20 15:17:14 -07001025void sctp_v6_pf_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 /* Register the SCTP specific PF_INET6 functions. */
Neil Horman15efbe72008-02-15 09:53:59 -05001028 sctp_register_pf(&sctp_pf_inet6, PF_INET6);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030 /* Register the SCTP specific AF_INET6 functions. */
Neil Horman15efbe72008-02-15 09:53:59 -05001031 sctp_register_af(&sctp_af_inet6);
Vlad Yasevich270637a2008-03-20 15:17:14 -07001032}
1033
1034void sctp_v6_pf_exit(void)
1035{
1036 list_del(&sctp_af_inet6.list);
1037}
1038
1039/* Initialize IPv6 support and register with socket layer. */
1040int sctp_v6_protosw_init(void)
1041{
1042 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001044 rc = proto_register(&sctpv6_prot, 1);
1045 if (rc)
1046 return rc;
1047
1048 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
1049 inet6_register_protosw(&sctpv6_seqpacket_protosw);
1050 inet6_register_protosw(&sctpv6_stream_protosw);
1051
1052 return 0;
1053}
1054
Vlad Yasevich270637a2008-03-20 15:17:14 -07001055void sctp_v6_protosw_exit(void)
1056{
1057 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1058 inet6_unregister_protosw(&sctpv6_stream_protosw);
1059 proto_unregister(&sctpv6_prot);
1060}
1061
1062
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001063/* Register with inet6 layer. */
1064int sctp_v6_add_protocol(void)
1065{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /* Register notifier for inet6 address additions/deletions. */
1067 register_inet6addr_notifier(&sctp_inet6addr_notifier);
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001068
1069 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1070 return -EAGAIN;
1071
1072 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073}
1074
Sridhar Samudrala827bf122007-05-04 13:36:30 -07001075/* Unregister with inet6 layer. */
1076void sctp_v6_del_protocol(void)
1077{
1078 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1079 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080}