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>
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>
69 #include <net/tcp.h>
70 #include <net/ndisc.h>
71 #include <net/ipv6.h>
72 #include <net/transp_v6.h>
73 #include <net/addrconf.h>
74 #include <net/ip6_route.h>
75 #include <net/inet_common.h>
76 #include <net/inet_ecn.h>
77 #include <net/sctp/sctp.h>
78
79 #include <asm/uaccess.h>
80
81 extern int sctp_inetaddr_event(struct notifier_block *, unsigned long, void *);
82 static struct notifier_block sctp_inet6addr_notifier = {
83 .notifier_call = sctp_inetaddr_event,
84 };
85
86 /* FIXME: This macro needs to be moved to a common header file. */
87 #define NIP6(addr) \
88 ntohs((addr)->s6_addr16[0]), \
89 ntohs((addr)->s6_addr16[1]), \
90 ntohs((addr)->s6_addr16[2]), \
91 ntohs((addr)->s6_addr16[3]), \
92 ntohs((addr)->s6_addr16[4]), \
93 ntohs((addr)->s6_addr16[5]), \
94 ntohs((addr)->s6_addr16[6]), \
95 ntohs((addr)->s6_addr16[7])
96
97 /* ICMP error handler. */
sctp_v6_err(struct sk_buff * skb,struct inet6_skb_parm * opt,int type,int code,int offset,__u32 info)98 SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
99 int type, int code, int offset, __u32 info)
100 {
101 struct inet6_dev *idev;
102 struct ipv6hdr *iph = (struct ipv6hdr *)skb->data;
103 struct sctphdr *sh = (struct sctphdr *)(skb->data + offset);
104 struct sock *sk;
105 struct sctp_endpoint *ep;
106 struct sctp_association *asoc;
107 struct sctp_transport *transport;
108 struct ipv6_pinfo *np;
109 char *saveip, *savesctp;
110 int err;
111
112 idev = in6_dev_get(skb->dev);
113
114 /* Fix up skb to look at the embedded net header. */
115 saveip = skb->nh.raw;
116 savesctp = skb->h.raw;
117 skb->nh.ipv6h = iph;
118 skb->h.raw = (char *)sh;
119 sk = sctp_err_lookup(AF_INET6, skb, sh, &ep, &asoc, &transport);
120 /* Put back, the original pointers. */
121 skb->nh.raw = saveip;
122 skb->h.raw = savesctp;
123 if (!sk) {
124 ICMP6_INC_STATS_BH(Icmp6InErrors);
125 goto out;
126 }
127
128 /* Warning: The sock lock is held. Remember to call
129 * sctp_err_finish!
130 */
131
132 switch (type) {
133 case ICMPV6_PKT_TOOBIG:
134 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
135 goto out_unlock;
136 case ICMPV6_PARAMPROB:
137 if (ICMPV6_UNK_NEXTHDR == code) {
138 sctp_icmp_proto_unreachable(sk, ep, asoc, transport);
139 goto out_unlock;
140 }
141 break;
142 default:
143 break;
144 }
145
146 np = inet6_sk(sk);
147 icmpv6_err_convert(type, code, &err);
148 if (!sock_owned_by_user(sk) && np->recverr) {
149 sk->err = err;
150 sk->error_report(sk);
151 } else { /* Only an error on timeout */
152 sk->err_soft = err;
153 }
154
155 out_unlock:
156 sctp_err_finish(sk, ep, asoc);
157 out:
158 if (likely(idev != NULL))
159 in6_dev_put(idev);
160 }
161
162 /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
sctp_v6_xmit(struct sk_buff * skb,struct sctp_transport * transport,int ipfragok)163 static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
164 int ipfragok)
165 {
166 struct sock *sk = skb->sk;
167 struct ipv6_pinfo *np = inet6_sk(sk);
168 struct flowi fl;
169
170 memset(&fl, 0, sizeof(fl));
171
172 fl.proto = sk->protocol;
173
174 /* Fill in the dest address from the route entry passed with the skb
175 * and the source address from the transport.
176 */
177 fl.fl6_dst = &transport->ipaddr.v6.sin6_addr;
178 fl.fl6_src = &transport->saddr.v6.sin6_addr;
179
180 fl.fl6_flowlabel = np->flow_label;
181 IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
182 if (ipv6_addr_type(fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
183 fl.oif = transport->saddr.v6.sin6_scope_id;
184 else
185 fl.oif = sk->bound_dev_if;
186 fl.uli_u.ports.sport = sk->sport;
187 fl.uli_u.ports.dport = transport->ipaddr.v6.sin6_port;
188
189 if (np->opt && np->opt->srcrt) {
190 struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
191 fl.fl6_dst = rt0->addr;
192 }
193
194 SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
195 "src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
196 "dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
197 __FUNCTION__, skb, skb->len,
198 NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
199
200 SCTP_INC_STATS(SctpOutSCTPPacks);
201
202 return ip6_xmit(sk, skb, &fl, np->opt);
203 }
204
205 /* Returns the dst cache entry for the given source and destination ip
206 * addresses.
207 */
sctp_v6_get_dst(struct sctp_association * asoc,union sctp_addr * daddr,union sctp_addr * saddr)208 static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
209 union sctp_addr *daddr,
210 union sctp_addr *saddr)
211 {
212 struct dst_entry *dst;
213 struct flowi fl;
214
215 memset(&fl, 0, sizeof(fl));
216 fl.fl6_dst = &daddr->v6.sin6_addr;
217 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
218 fl.oif = daddr->v6.sin6_scope_id;
219
220
221 SCTP_DEBUG_PRINTK("%s: DST=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
222 __FUNCTION__, NIP6(fl.fl6_dst));
223
224 if (saddr) {
225 fl.fl6_src = &saddr->v6.sin6_addr;
226 SCTP_DEBUG_PRINTK(
227 "SRC=%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x - ",
228 NIP6(fl.fl6_src));
229 }
230
231 dst = ip6_route_output(NULL, &fl);
232 if (dst) {
233 struct rt6_info *rt;
234 rt = (struct rt6_info *)dst;
235 SCTP_DEBUG_PRINTK(
236 "rt6_dst:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x "
237 "rt6_src:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
238 NIP6(&rt->rt6i_dst.addr), NIP6(&rt->rt6i_src.addr));
239 } else {
240 SCTP_DEBUG_PRINTK("NO ROUTE\n");
241 }
242
243 return dst;
244 }
245
246 /* Returns the number of consecutive initial bits that match in the 2 ipv6
247 * addresses.
248 */
sctp_v6_addr_match_len(union sctp_addr * s1,union sctp_addr * s2)249 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
250 union sctp_addr *s2)
251 {
252 struct in6_addr *a1 = &s1->v6.sin6_addr;
253 struct in6_addr *a2 = &s2->v6.sin6_addr;
254 int i, j;
255
256 for (i = 0; i < 4 ; i++) {
257 __u32 a1xora2;
258
259 a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
260
261 if ((j = fls(ntohl(a1xora2))))
262 return (i * 32 + 32 - j);
263 }
264
265 return (i*32);
266 }
267
268 /* Fills in the source address(saddr) based on the destination address(daddr)
269 * and asoc's bind address list.
270 */
sctp_v6_get_saddr(struct sctp_association * asoc,struct dst_entry * dst,union sctp_addr * daddr,union sctp_addr * saddr)271 static void sctp_v6_get_saddr(struct sctp_association *asoc,
272 struct dst_entry *dst,
273 union sctp_addr *daddr,
274 union sctp_addr *saddr)
275 {
276 struct sctp_bind_addr *bp;
277 rwlock_t *addr_lock;
278 struct sctp_sockaddr_entry *laddr;
279 struct list_head *pos;
280 sctp_scope_t scope;
281 union sctp_addr *baddr = NULL;
282 __u8 matchlen = 0;
283 __u8 bmatchlen;
284
285 SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
286 "daddr:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
287 __FUNCTION__, asoc, dst, NIP6(&daddr->v6.sin6_addr));
288
289 if (!asoc) {
290 ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
291 SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: "
292 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
293 NIP6(&saddr->v6.sin6_addr));
294 return;
295 }
296
297 scope = sctp_scope(daddr);
298
299 bp = &asoc->base.bind_addr;
300 addr_lock = &asoc->base.addr_lock;
301
302 /* Go through the bind address list and find the best source address
303 * that matches the scope of the destination address.
304 */
305 sctp_read_lock(addr_lock);
306 list_for_each(pos, &bp->address_list) {
307 laddr = list_entry(pos, struct sctp_sockaddr_entry, list);
308 if ((laddr->a.sa.sa_family == AF_INET6) &&
309 (scope <= sctp_scope(&laddr->a))) {
310 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
311 if (!baddr || (matchlen < bmatchlen)) {
312 baddr = &laddr->a;
313 matchlen = bmatchlen;
314 }
315 }
316 }
317
318 if (baddr) {
319 memcpy(saddr, baddr, sizeof(union sctp_addr));
320 SCTP_DEBUG_PRINTK("saddr: "
321 "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
322 NIP6(&saddr->v6.sin6_addr));
323 } else {
324 printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
325 "address for the "
326 "dest:%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
327 __FUNCTION__, asoc, NIP6(&daddr->v6.sin6_addr));
328 }
329
330 sctp_read_unlock(addr_lock);
331 }
332
333 /* Make a copy of all potential local addresses. */
sctp_v6_copy_addrlist(struct list_head * addrlist,struct net_device * dev)334 static void sctp_v6_copy_addrlist(struct list_head *addrlist,
335 struct net_device *dev)
336 {
337 struct inet6_dev *in6_dev;
338 struct inet6_ifaddr *ifp;
339 struct sctp_sockaddr_entry *addr;
340
341 read_lock(&addrconf_lock);
342 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
343 read_unlock(&addrconf_lock);
344 return;
345 }
346
347 read_lock(&in6_dev->lock);
348 for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
349 /* Add the address to the local list. */
350 addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
351 if (addr) {
352 addr->a.v6.sin6_family = AF_INET6;
353 addr->a.v6.sin6_port = 0;
354 addr->a.v6.sin6_addr = ifp->addr;
355 addr->a.v6.sin6_scope_id = dev->ifindex;
356 INIT_LIST_HEAD(&addr->list);
357 list_add_tail(&addr->list, addrlist);
358 }
359 }
360
361 read_unlock(&in6_dev->lock);
362 read_unlock(&addrconf_lock);
363 }
364
365 /* Initialize a sockaddr_storage from in incoming skb. */
sctp_v6_from_skb(union sctp_addr * addr,struct sk_buff * skb,int is_saddr)366 static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
367 int is_saddr)
368 {
369 void *from;
370 __u16 *port;
371 struct sctphdr *sh;
372
373 port = &addr->v6.sin6_port;
374 addr->v6.sin6_family = AF_INET6;
375 addr->v6.sin6_flowinfo = 0; /* FIXME */
376 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
377
378 sh = (struct sctphdr *) skb->h.raw;
379 if (is_saddr) {
380 *port = ntohs(sh->source);
381 from = &skb->nh.ipv6h->saddr;
382 } else {
383 *port = ntohs(sh->dest);
384 from = &skb->nh.ipv6h->daddr;
385 }
386 ipv6_addr_copy(&addr->v6.sin6_addr, from);
387 }
388
389 /* Initialize an sctp_addr from a socket. */
sctp_v6_from_sk(union sctp_addr * addr,struct sock * sk)390 static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
391 {
392 addr->v6.sin6_family = AF_INET6;
393 addr->v6.sin6_port = sk->num;
394 addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
395 }
396
397 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
sctp_v6_to_sk_saddr(union sctp_addr * addr,struct sock * sk)398 static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
399 {
400 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
401 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
402 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
403 inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
404 inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
405 addr->v4.sin_addr.s_addr;
406 } else {
407 inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
408 }
409 }
410
411 /* Initialize sk->sk_daddr from sctp_addr. */
sctp_v6_to_sk_daddr(union sctp_addr * addr,struct sock * sk)412 static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
413 {
414 if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
415 inet6_sk(sk)->daddr.s6_addr32[0] = 0;
416 inet6_sk(sk)->daddr.s6_addr32[1] = 0;
417 inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
418 inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
419 } else {
420 inet6_sk(sk)->daddr = addr->v6.sin6_addr;
421 }
422 }
423
424 /* Initialize a sctp_addr from an address parameter. */
sctp_v6_from_addr_param(union sctp_addr * addr,union sctp_addr_param * param,__u16 port,int iif)425 static void sctp_v6_from_addr_param(union sctp_addr *addr,
426 union sctp_addr_param *param,
427 __u16 port, int iif)
428 {
429 addr->v6.sin6_family = AF_INET6;
430 addr->v6.sin6_port = port;
431 addr->v6.sin6_flowinfo = 0; /* BUG */
432 ipv6_addr_copy(&addr->v6.sin6_addr, ¶m->v6.addr);
433 addr->v6.sin6_scope_id = iif;
434 }
435
436 /* Initialize an address parameter from a sctp_addr and return the length
437 * of the address parameter.
438 */
sctp_v6_to_addr_param(const union sctp_addr * addr,union sctp_addr_param * param)439 static int sctp_v6_to_addr_param(const union sctp_addr *addr,
440 union sctp_addr_param *param)
441 {
442 int length = sizeof(sctp_ipv6addr_param_t);
443
444 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
445 param->v6.param_hdr.length = ntohs(length);
446 ipv6_addr_copy(¶m->v6.addr, &addr->v6.sin6_addr);
447
448 return length;
449 }
450
451 /* Initialize a sctp_addr from a dst_entry. */
sctp_v6_dst_saddr(union sctp_addr * addr,struct dst_entry * dst,unsigned short port)452 static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
453 unsigned short port)
454 {
455 struct rt6_info *rt = (struct rt6_info *)dst;
456 addr->sa.sa_family = AF_INET6;
457 addr->v6.sin6_port = port;
458 ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
459 }
460
461 /* Compare addresses exactly.
462 * v4-mapped-v6 is also in consideration.
463 */
sctp_v6_cmp_addr(const union sctp_addr * addr1,const union sctp_addr * addr2)464 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
465 const union sctp_addr *addr2)
466 {
467 if (addr1->sa.sa_family != addr2->sa.sa_family) {
468 if (addr1->sa.sa_family == AF_INET &&
469 addr2->sa.sa_family == AF_INET6 &&
470 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr2->v6.sin6_addr)) {
471 if (addr2->v6.sin6_port == addr1->v4.sin_port &&
472 addr2->v6.sin6_addr.s6_addr32[3] ==
473 addr1->v4.sin_addr.s_addr)
474 return 1;
475 }
476 if (addr2->sa.sa_family == AF_INET &&
477 addr1->sa.sa_family == AF_INET6 &&
478 IPV6_ADDR_MAPPED == ipv6_addr_type(&addr1->v6.sin6_addr)) {
479 if (addr1->v6.sin6_port == addr2->v4.sin_port &&
480 addr1->v6.sin6_addr.s6_addr32[3] ==
481 addr2->v4.sin_addr.s_addr)
482 return 1;
483 }
484 return 0;
485 }
486 if (ipv6_addr_cmp(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
487 return 0;
488 /* If this is a linklocal address, compare the scope_id. */
489 if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
490 if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
491 (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
492 return 0;
493 }
494 }
495
496 return 1;
497 }
498
499 /* Initialize addr struct to INADDR_ANY. */
sctp_v6_inaddr_any(union sctp_addr * addr,unsigned short port)500 static void sctp_v6_inaddr_any(union sctp_addr *addr, unsigned short port)
501 {
502 memset(addr, 0x00, sizeof(union sctp_addr));
503 addr->v6.sin6_family = AF_INET6;
504 addr->v6.sin6_port = port;
505 }
506
507 /* Is this a wildcard address? */
sctp_v6_is_any(const union sctp_addr * addr)508 static int sctp_v6_is_any(const union sctp_addr *addr)
509 {
510 int type;
511 type = ipv6_addr_type((struct in6_addr *)&addr->v6.sin6_addr);
512 return IPV6_ADDR_ANY == type;
513 }
514
515 /* Should this be available for binding? */
sctp_v6_available(union sctp_addr * addr,struct sctp_opt * sp)516 static int sctp_v6_available(union sctp_addr *addr, struct sctp_opt *sp)
517 {
518 int type;
519 struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
520
521 type = ipv6_addr_type(in6);
522 if (IPV6_ADDR_ANY == type)
523 return 1;
524 if (type == IPV6_ADDR_MAPPED) {
525 if (sp && !sp->v4mapped)
526 return 0;
527 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
528 return 0;
529 sctp_v6_map_v4(addr);
530 return sctp_get_af_specific(AF_INET)->available(addr, sp);
531 }
532 if (!(type & IPV6_ADDR_UNICAST))
533 return 0;
534
535 return ipv6_chk_addr(in6, NULL);
536 }
537
538 /* This function checks if the address is a valid address to be used for
539 * SCTP.
540 *
541 * Output:
542 * Return 0 - If the address is a non-unicast or an illegal address.
543 * Return 1 - If the address is a unicast.
544 */
sctp_v6_addr_valid(union sctp_addr * addr,struct sctp_opt * sp)545 static int sctp_v6_addr_valid(union sctp_addr *addr, struct sctp_opt *sp)
546 {
547 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
548
549 /* Support v4-mapped-v6 address. */
550 if (ret == IPV6_ADDR_MAPPED) {
551 /* Note: This routine is used in input, so v4-mapped-v6
552 * are disallowed here when there is no sctp_opt.
553 */
554 if (!sp || !sp->v4mapped)
555 return 0;
556 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
557 return 0;
558 sctp_v6_map_v4(addr);
559 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp);
560 }
561
562 /* Is this a non-unicast address */
563 if (!(ret & IPV6_ADDR_UNICAST))
564 return 0;
565
566 return 1;
567 }
568
569 /* What is the scope of 'addr'? */
sctp_v6_scope(union sctp_addr * addr)570 static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
571 {
572 int v6scope;
573 sctp_scope_t retval;
574
575 /* The IPv6 scope is really a set of bit fields.
576 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
577 */
578
579 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
580 switch (v6scope) {
581 case IFA_HOST:
582 retval = SCTP_SCOPE_LOOPBACK;
583 break;
584 case IFA_LINK:
585 retval = SCTP_SCOPE_LINK;
586 break;
587 case IFA_SITE:
588 retval = SCTP_SCOPE_PRIVATE;
589 break;
590 default:
591 retval = SCTP_SCOPE_GLOBAL;
592 break;
593 };
594
595 return retval;
596 }
597
598 /* Create and initialize a new sk for the socket to be returned by accept(). */
sctp_v6_create_accept_sk(struct sock * sk,struct sctp_association * asoc)599 static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
600 struct sctp_association *asoc)
601 {
602 struct sock *newsk;
603 struct inet_opt *newinet;
604 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
605
606 newsk = sk_alloc(PF_INET6, GFP_KERNEL, sizeof(struct sock));
607 if (!newsk)
608 goto out;
609
610 sock_init_data(NULL, newsk);
611 sk_set_owner(newsk, THIS_MODULE);
612
613 newsk->type = SOCK_STREAM;
614
615 newsk->prot = sk->prot;
616 newsk->no_check = sk->no_check;
617 newsk->reuse = sk->reuse;
618
619 newsk->destruct = inet_sock_destruct;
620 newsk->zapped = 0;
621 newsk->family = PF_INET6;
622 newsk->protocol = IPPROTO_SCTP;
623 newsk->backlog_rcv = sk->prot->backlog_rcv;
624 newsk->shutdown = sk->shutdown;
625
626 newinet = inet_sk(newsk);
627 newnp = inet6_sk(newsk);
628
629 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
630
631 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
632 * and getpeername().
633 */
634 newsk->sport = sk->sport;
635 newsk->saddr = sk->saddr;
636 newnp->rcv_saddr = np->rcv_saddr;
637 newsk->dport = htons(asoc->peer.port);
638 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
639
640 /* Init the ipv4 part of the socket since we can have sockets
641 * using v6 API for ipv4.
642 */
643 newinet->ttl = sysctl_ip_default_ttl;
644 newinet->mc_loop = 1;
645 newinet->mc_ttl = 1;
646 newinet->mc_index = 0;
647 newinet->mc_list = NULL;
648
649 if (ipv4_config.no_pmtu_disc)
650 newinet->pmtudisc = IP_PMTUDISC_DONT;
651 else
652 newinet->pmtudisc = IP_PMTUDISC_WANT;
653
654 #ifdef INET_REFCNT_DEBUG
655 atomic_inc(&inet6_sock_nr);
656 atomic_inc(&inet_sock_nr);
657 #endif
658
659 if (newsk->prot->init(newsk)) {
660 inet_sock_release(newsk);
661 newsk = NULL;
662 }
663
664 out:
665 return newsk;
666 }
667
668 /* Map v4 address to mapped v6 address */
sctp_v6_addr_v4map(struct sctp_opt * sp,union sctp_addr * addr)669 static void sctp_v6_addr_v4map(struct sctp_opt *sp, union sctp_addr *addr)
670 {
671 if (sp->v4mapped && AF_INET == addr->sa.sa_family)
672 sctp_v4_map_v6(addr);
673 }
674
675 /* Where did this skb come from? */
sctp_v6_skb_iif(const struct sk_buff * skb)676 static int sctp_v6_skb_iif(const struct sk_buff *skb)
677 {
678 struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
679 return opt->iif;
680 }
681
682 /* Was this packet marked by Explicit Congestion Notification? */
sctp_v6_is_ce(const struct sk_buff * skb)683 static int sctp_v6_is_ce(const struct sk_buff *skb)
684 {
685 return *((__u32 *)(skb->nh.ipv6h)) & htonl(1<<20);
686 }
687
688 /* Dump the v6 addr to the seq file. */
sctp_v6_seq_dump_addr(struct seq_file * seq,union sctp_addr * addr)689 static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
690 {
691 seq_printf(seq, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x ",
692 NIP6(&addr->v6.sin6_addr));
693 }
694
695 /* Initialize a PF_INET6 socket msg_name. */
sctp_inet6_msgname(char * msgname,int * addr_len)696 static void sctp_inet6_msgname(char *msgname, int *addr_len)
697 {
698 struct sockaddr_in6 *sin6;
699
700 sin6 = (struct sockaddr_in6 *)msgname;
701 sin6->sin6_family = AF_INET6;
702 sin6->sin6_flowinfo = 0;
703 sin6->sin6_scope_id = 0; /*FIXME */
704 *addr_len = sizeof(struct sockaddr_in6);
705 }
706
707 /* Initialize a PF_INET msgname from a ulpevent. */
sctp_inet6_event_msgname(struct sctp_ulpevent * event,char * msgname,int * addrlen)708 static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
709 char *msgname, int *addrlen)
710 {
711 struct sockaddr_in6 *sin6, *sin6from;
712
713 if (msgname) {
714 union sctp_addr *addr;
715 struct sctp_association *asoc;
716
717 asoc = event->asoc;
718 sctp_inet6_msgname(msgname, addrlen);
719 sin6 = (struct sockaddr_in6 *)msgname;
720 sin6->sin6_port = htons(asoc->peer.port);
721 addr = &asoc->peer.primary_addr;
722
723 /* Note: If we go to a common v6 format, this code
724 * will change.
725 */
726
727 /* Map ipv4 address into v4-mapped-on-v6 address. */
728 if (sctp_sk(asoc->base.sk)->v4mapped &&
729 AF_INET == addr->sa.sa_family) {
730 sctp_v4_map_v6((union sctp_addr *)sin6);
731 sin6->sin6_addr.s6_addr32[3] =
732 addr->v4.sin_addr.s_addr;
733 return;
734 }
735
736 sin6from = &asoc->peer.primary_addr.v6;
737 ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
738 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
739 sin6->sin6_scope_id = sin6from->sin6_scope_id;
740 }
741 }
742
743 /* Initialize a msg_name from an inbound skb. */
sctp_inet6_skb_msgname(struct sk_buff * skb,char * msgname,int * addr_len)744 static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
745 int *addr_len)
746 {
747 struct sctphdr *sh;
748 struct sockaddr_in6 *sin6;
749
750 if (msgname) {
751 sctp_inet6_msgname(msgname, addr_len);
752 sin6 = (struct sockaddr_in6 *)msgname;
753 sh = (struct sctphdr *)skb->h.raw;
754 sin6->sin6_port = sh->source;
755
756 /* Map ipv4 address into v4-mapped-on-v6 address. */
757 if (sctp_sk(skb->sk)->v4mapped &&
758 skb->nh.iph->version == 4) {
759 sctp_v4_map_v6((union sctp_addr *)sin6);
760 sin6->sin6_addr.s6_addr32[3] = skb->nh.iph->saddr;
761 return;
762 }
763
764 /* Otherwise, just copy the v6 address. */
765 ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
766 if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
767 struct sctp_ulpevent *ev = sctp_skb2event(skb);
768 sin6->sin6_scope_id = ev->iif;
769 }
770 }
771 }
772
773 /* Do we support this AF? */
sctp_inet6_af_supported(sa_family_t family,struct sctp_opt * sp)774 static int sctp_inet6_af_supported(sa_family_t family, struct sctp_opt *sp)
775 {
776 switch (family) {
777 case AF_INET6:
778 return 1;
779 /* v4-mapped-v6 addresses */
780 case AF_INET:
781 if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
782 return 1;
783 default:
784 return 0;
785 }
786 }
787
788 /* Address matching with wildcards allowed. This extra level
789 * of indirection lets us choose whether a PF_INET6 should
790 * disallow any v4 addresses if we so choose.
791 */
sctp_inet6_cmp_addr(const union sctp_addr * addr1,const union sctp_addr * addr2,struct sctp_opt * opt)792 static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
793 const union sctp_addr *addr2,
794 struct sctp_opt *opt)
795 {
796 struct sctp_af *af1, *af2;
797
798 af1 = sctp_get_af_specific(addr1->sa.sa_family);
799 af2 = sctp_get_af_specific(addr2->sa.sa_family);
800
801 if (!af1 || !af2)
802 return 0;
803 /* Today, wildcard AF_INET/AF_INET6. */
804 if (sctp_is_any(addr1) || sctp_is_any(addr2))
805 return 1;
806
807 if (addr1->sa.sa_family != addr2->sa.sa_family)
808 return 0;
809
810 return af1->cmp_addr(addr1, addr2);
811 }
812
813 /* Verify that the provided sockaddr looks bindable. Common verification,
814 * has already been taken care of.
815 */
sctp_inet6_bind_verify(struct sctp_opt * opt,union sctp_addr * addr)816 static int sctp_inet6_bind_verify(struct sctp_opt *opt, union sctp_addr *addr)
817 {
818 struct sctp_af *af;
819
820 /* ASSERT: address family has already been verified. */
821 if (addr->sa.sa_family != AF_INET6)
822 af = sctp_get_af_specific(addr->sa.sa_family);
823 else {
824 struct sock *sk;
825 int type = ipv6_addr_type(&addr->v6.sin6_addr);
826 sk = sctp_opt2sk(opt);
827 if (type & IPV6_ADDR_LINKLOCAL) {
828 /* Note: Behavior similar to af_inet6.c:
829 * 1) Overrides previous bound_dev_if
830 * 2) Destructive even if bind isn't successful.
831 */
832
833 if (addr->v6.sin6_scope_id)
834 sk->bound_dev_if = addr->v6.sin6_scope_id;
835 if (!sk->bound_dev_if)
836 return 0;
837 }
838 af = opt->pf->af;
839 }
840 return af->available(addr, opt);
841 }
842
843 /* Verify that the provided sockaddr looks bindable. Common verification,
844 * has already been taken care of.
845 */
sctp_inet6_send_verify(struct sctp_opt * opt,union sctp_addr * addr)846 static int sctp_inet6_send_verify(struct sctp_opt *opt, union sctp_addr *addr)
847 {
848 struct sctp_af *af = NULL;
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 {
854 struct sock *sk;
855 int type = ipv6_addr_type(&addr->v6.sin6_addr);
856 sk = sctp_opt2sk(opt);
857 if (type & IPV6_ADDR_LINKLOCAL) {
858 /* Note: Behavior similar to af_inet6.c:
859 * 1) Overrides previous bound_dev_if
860 * 2) Destructive even if bind isn't successful.
861 */
862
863 if (addr->v6.sin6_scope_id)
864 sk->bound_dev_if = addr->v6.sin6_scope_id;
865 if (!sk->bound_dev_if)
866 return 0;
867 }
868 af = opt->pf->af;
869 }
870
871 return af != NULL;
872 }
873
874 /* Fill in Supported Address Type information for INIT and INIT-ACK
875 * chunks. Note: In the future, we may want to look at sock options
876 * to determine whether a PF_INET6 socket really wants to have IPV4
877 * addresses.
878 * Returns number of addresses supported.
879 */
sctp_inet6_supported_addrs(const struct sctp_opt * opt,__u16 * types)880 static int sctp_inet6_supported_addrs(const struct sctp_opt *opt,
881 __u16 *types)
882 {
883 types[0] = SCTP_PARAM_IPV4_ADDRESS;
884 types[1] = SCTP_PARAM_IPV6_ADDRESS;
885 return 2;
886 }
887
888 static struct proto_ops inet6_seqpacket_ops = {
889 .family = PF_INET6,
890 .release = inet6_release,
891 .bind = inet6_bind,
892 .connect = inet_dgram_connect,
893 .socketpair = sock_no_socketpair,
894 .accept = inet_accept,
895 .getname = inet6_getname,
896 .poll = sctp_poll,
897 .ioctl = inet6_ioctl,
898 .listen = sctp_inet_listen,
899 .shutdown = inet_shutdown,
900 .setsockopt = inet_setsockopt,
901 .getsockopt = inet_getsockopt,
902 .sendmsg = inet_sendmsg,
903 .recvmsg = inet_recvmsg,
904 .mmap = sock_no_mmap,
905 };
906
907 static struct inet_protosw sctpv6_seqpacket_protosw = {
908 .type = SOCK_SEQPACKET,
909 .protocol = IPPROTO_SCTP,
910 .prot = &sctp_prot,
911 .ops = &inet6_seqpacket_ops,
912 .capability = -1,
913 .no_check = 0,
914 .flags = SCTP_PROTOSW_FLAG
915 };
916 static struct inet_protosw sctpv6_stream_protosw = {
917 .type = SOCK_STREAM,
918 .protocol = IPPROTO_SCTP,
919 .prot = &sctp_prot,
920 .ops = &inet6_seqpacket_ops,
921 .capability = -1,
922 .no_check = 0,
923 .flags = SCTP_PROTOSW_FLAG,
924 };
925
926 static struct inet6_protocol sctpv6_protocol = {
927 .handler = sctp_rcv,
928 .err_handler = sctp_v6_err,
929 .next = NULL,
930 .protocol = IPPROTO_SCTP,
931 .copy = 0,
932 .data = NULL,
933 .name = "SCTPv6",
934 };
935
936 static struct sctp_af sctp_ipv6_specific = {
937 .sctp_xmit = sctp_v6_xmit,
938 .setsockopt = ipv6_setsockopt,
939 .getsockopt = ipv6_getsockopt,
940 .get_dst = sctp_v6_get_dst,
941 .get_saddr = sctp_v6_get_saddr,
942 .copy_addrlist = sctp_v6_copy_addrlist,
943 .from_skb = sctp_v6_from_skb,
944 .from_sk = sctp_v6_from_sk,
945 .to_sk_saddr = sctp_v6_to_sk_saddr,
946 .to_sk_daddr = sctp_v6_to_sk_daddr,
947 .from_addr_param = sctp_v6_from_addr_param,
948 .to_addr_param = sctp_v6_to_addr_param,
949 .dst_saddr = sctp_v6_dst_saddr,
950 .cmp_addr = sctp_v6_cmp_addr,
951 .scope = sctp_v6_scope,
952 .addr_valid = sctp_v6_addr_valid,
953 .inaddr_any = sctp_v6_inaddr_any,
954 .is_any = sctp_v6_is_any,
955 .available = sctp_v6_available,
956 .skb_iif = sctp_v6_skb_iif,
957 .is_ce = sctp_v6_is_ce,
958 .seq_dump_addr = sctp_v6_seq_dump_addr,
959 .net_header_len = sizeof(struct ipv6hdr),
960 .sockaddr_len = sizeof(struct sockaddr_in6),
961 .sa_family = AF_INET6,
962 };
963
964 static struct sctp_pf sctp_pf_inet6_specific = {
965 .event_msgname = sctp_inet6_event_msgname,
966 .skb_msgname = sctp_inet6_skb_msgname,
967 .af_supported = sctp_inet6_af_supported,
968 .cmp_addr = sctp_inet6_cmp_addr,
969 .bind_verify = sctp_inet6_bind_verify,
970 .send_verify = sctp_inet6_send_verify,
971 .supported_addrs = sctp_inet6_supported_addrs,
972 .create_accept_sk = sctp_v6_create_accept_sk,
973 .addr_v4map = sctp_v6_addr_v4map,
974 .af = &sctp_ipv6_specific,
975 };
976
977 /* Initialize IPv6 support and register with inet6 stack. */
sctp_v6_init(void)978 int sctp_v6_init(void)
979 {
980 /* Register inet6 protocol. */
981 inet6_add_protocol(&sctpv6_protocol);
982
983 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
984 inet6_register_protosw(&sctpv6_seqpacket_protosw);
985 inet6_register_protosw(&sctpv6_stream_protosw);
986
987 /* Register the SCTP specific PF_INET6 functions. */
988 sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
989
990 /* Register the SCTP specific AF_INET6 functions. */
991 sctp_register_af(&sctp_ipv6_specific);
992
993 /* Register notifier for inet6 address additions/deletions. */
994 register_inet6addr_notifier(&sctp_inet6addr_notifier);
995
996 return 0;
997 }
998
999 /* IPv6 specific exit support. */
sctp_v6_exit(void)1000 void sctp_v6_exit(void)
1001 {
1002 list_del(&sctp_ipv6_specific.list);
1003 inet6_del_protocol(&sctpv6_protocol);
1004 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1005 inet6_unregister_protosw(&sctpv6_stream_protosw);
1006 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
1007 }
1008