1 /* SCTP kernel 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 implementation 8 * 9 * SCTP over IPv6. 10 * 11 * This SCTP 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 * This SCTP 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, see 25 * <http://www.gnu.org/licenses/>. 26 * 27 * Please send any bug reports or fixes you make to the 28 * email address(es): 29 * lksctp developers <linux-sctp@vger.kernel.org> 30 * 31 * Written or modified by: 32 * Le Yanqun <yanqun.le@nokia.com> 33 * Hui Huang <hui.huang@nokia.com> 34 * La Monte H.P. Yarroll <piggy@acm.org> 35 * Sridhar Samudrala <sri@us.ibm.com> 36 * Jon Grimm <jgrimm@us.ibm.com> 37 * Ardelle Fan <ardelle.fan@intel.com> 38 * 39 * Based on: 40 * linux/net/ipv6/tcp_ipv6.c 41 */ 42 43 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 44 45 #include <linux/module.h> 46 #include <linux/errno.h> 47 #include <linux/types.h> 48 #include <linux/socket.h> 49 #include <linux/sockios.h> 50 #include <linux/net.h> 51 #include <linux/in.h> 52 #include <linux/in6.h> 53 #include <linux/netdevice.h> 54 #include <linux/init.h> 55 #include <linux/ipsec.h> 56 #include <linux/slab.h> 57 58 #include <linux/ipv6.h> 59 #include <linux/icmpv6.h> 60 #include <linux/random.h> 61 #include <linux/seq_file.h> 62 63 #include <net/protocol.h> 64 #include <net/ndisc.h> 65 #include <net/ip.h> 66 #include <net/ipv6.h> 67 #include <net/transp_v6.h> 68 #include <net/addrconf.h> 69 #include <net/ip6_route.h> 70 #include <net/inet_common.h> 71 #include <net/inet_ecn.h> 72 #include <net/sctp/sctp.h> 73 74 #include <linux/uaccess.h> 75 76 static inline int sctp_v6_addr_match_len(union sctp_addr *s1, 77 union sctp_addr *s2); 78 static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr, 79 __be16 port); 80 static int sctp_v6_cmp_addr(const union sctp_addr *addr1, 81 const union sctp_addr *addr2); 82 83 /* Event handler for inet6 address addition/deletion events. 84 * The sctp_local_addr_list needs to be protocted by a spin lock since 85 * multiple notifiers (say IPv4 and IPv6) may be running at the same 86 * time and thus corrupt the list. 87 * The reader side is protected with RCU. 88 */ 89 static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev, 90 void *ptr) 91 { 92 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr; 93 struct sctp_sockaddr_entry *addr = NULL; 94 struct sctp_sockaddr_entry *temp; 95 struct net *net = dev_net(ifa->idev->dev); 96 int found = 0; 97 98 switch (ev) { 99 case NETDEV_UP: 100 addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC); 101 if (addr) { 102 addr->a.v6.sin6_family = AF_INET6; 103 addr->a.v6.sin6_port = 0; 104 addr->a.v6.sin6_flowinfo = 0; 105 addr->a.v6.sin6_addr = ifa->addr; 106 addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex; 107 addr->valid = 1; 108 spin_lock_bh(&net->sctp.local_addr_lock); 109 list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list); 110 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW); 111 spin_unlock_bh(&net->sctp.local_addr_lock); 112 } 113 break; 114 case NETDEV_DOWN: 115 spin_lock_bh(&net->sctp.local_addr_lock); 116 list_for_each_entry_safe(addr, temp, 117 &net->sctp.local_addr_list, list) { 118 if (addr->a.sa.sa_family == AF_INET6 && 119 ipv6_addr_equal(&addr->a.v6.sin6_addr, 120 &ifa->addr)) { 121 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL); 122 found = 1; 123 addr->valid = 0; 124 list_del_rcu(&addr->list); 125 break; 126 } 127 } 128 spin_unlock_bh(&net->sctp.local_addr_lock); 129 if (found) 130 kfree_rcu(addr, rcu); 131 break; 132 } 133 134 return NOTIFY_DONE; 135 } 136 137 static struct notifier_block sctp_inet6addr_notifier = { 138 .notifier_call = sctp_inet6addr_event, 139 }; 140 141 /* ICMP error handler. */ 142 static int sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt, 143 u8 type, u8 code, int offset, __be32 info) 144 { 145 struct inet6_dev *idev; 146 struct sock *sk; 147 struct sctp_association *asoc; 148 struct sctp_transport *transport; 149 struct ipv6_pinfo *np; 150 __u16 saveip, savesctp; 151 int err, ret = 0; 152 struct net *net = dev_net(skb->dev); 153 154 idev = in6_dev_get(skb->dev); 155 156 /* Fix up skb to look at the embedded net header. */ 157 saveip = skb->network_header; 158 savesctp = skb->transport_header; 159 skb_reset_network_header(skb); 160 skb_set_transport_header(skb, offset); 161 sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport); 162 /* Put back, the original pointers. */ 163 skb->network_header = saveip; 164 skb->transport_header = savesctp; 165 if (!sk) { 166 __ICMP6_INC_STATS(net, idev, ICMP6_MIB_INERRORS); 167 ret = -ENOENT; 168 goto out; 169 } 170 171 /* Warning: The sock lock is held. Remember to call 172 * sctp_err_finish! 173 */ 174 175 switch (type) { 176 case ICMPV6_PKT_TOOBIG: 177 if (ip6_sk_accept_pmtu(sk)) 178 sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info)); 179 goto out_unlock; 180 case ICMPV6_PARAMPROB: 181 if (ICMPV6_UNK_NEXTHDR == code) { 182 sctp_icmp_proto_unreachable(sk, asoc, transport); 183 goto out_unlock; 184 } 185 break; 186 case NDISC_REDIRECT: 187 sctp_icmp_redirect(sk, transport, skb); 188 goto out_unlock; 189 default: 190 break; 191 } 192 193 np = inet6_sk(sk); 194 icmpv6_err_convert(type, code, &err); 195 if (!sock_owned_by_user(sk) && np->recverr) { 196 sk->sk_err = err; 197 sk->sk_error_report(sk); 198 } else { /* Only an error on timeout */ 199 sk->sk_err_soft = err; 200 } 201 202 out_unlock: 203 sctp_err_finish(sk, transport); 204 out: 205 if (likely(idev != NULL)) 206 in6_dev_put(idev); 207 208 return ret; 209 } 210 211 static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport) 212 { 213 struct sock *sk = skb->sk; 214 struct ipv6_pinfo *np = inet6_sk(sk); 215 struct flowi6 *fl6 = &transport->fl.u.ip6; 216 __u8 tclass = np->tclass; 217 int res; 218 219 pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb, 220 skb->len, &fl6->saddr, &fl6->daddr); 221 222 if (transport->dscp & SCTP_DSCP_SET_MASK) 223 tclass = transport->dscp & SCTP_DSCP_VAL_MASK; 224 225 if (INET_ECN_is_capable(tclass)) 226 IP6_ECN_flow_xmit(sk, fl6->flowlabel); 227 228 if (!(transport->param_flags & SPP_PMTUD_ENABLE)) 229 skb->ignore_df = 1; 230 231 SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS); 232 233 rcu_read_lock(); 234 res = ip6_xmit(sk, skb, fl6, sk->sk_mark, rcu_dereference(np->opt), 235 tclass); 236 rcu_read_unlock(); 237 return res; 238 } 239 240 /* Returns the dst cache entry for the given source and destination ip 241 * addresses. 242 */ 243 static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr, 244 struct flowi *fl, struct sock *sk) 245 { 246 struct sctp_association *asoc = t->asoc; 247 struct dst_entry *dst = NULL; 248 struct flowi6 *fl6 = &fl->u.ip6; 249 struct sctp_bind_addr *bp; 250 struct ipv6_pinfo *np = inet6_sk(sk); 251 struct sctp_sockaddr_entry *laddr; 252 union sctp_addr *daddr = &t->ipaddr; 253 union sctp_addr dst_saddr; 254 struct in6_addr *final_p, final; 255 enum sctp_scope scope; 256 __u8 matchlen = 0; 257 258 memset(fl6, 0, sizeof(struct flowi6)); 259 fl6->daddr = daddr->v6.sin6_addr; 260 fl6->fl6_dport = daddr->v6.sin6_port; 261 fl6->flowi6_proto = IPPROTO_SCTP; 262 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) 263 fl6->flowi6_oif = daddr->v6.sin6_scope_id; 264 else if (asoc) 265 fl6->flowi6_oif = asoc->base.sk->sk_bound_dev_if; 266 if (t->flowlabel & SCTP_FLOWLABEL_SET_MASK) 267 fl6->flowlabel = htonl(t->flowlabel & SCTP_FLOWLABEL_VAL_MASK); 268 269 if (np->sndflow && (fl6->flowlabel & IPV6_FLOWLABEL_MASK)) { 270 struct ip6_flowlabel *flowlabel; 271 272 flowlabel = fl6_sock_lookup(sk, fl6->flowlabel); 273 if (!flowlabel) 274 goto out; 275 fl6_sock_release(flowlabel); 276 } 277 278 pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr); 279 280 if (asoc) 281 fl6->fl6_sport = htons(asoc->base.bind_addr.port); 282 283 if (saddr) { 284 fl6->saddr = saddr->v6.sin6_addr; 285 fl6->fl6_sport = saddr->v6.sin6_port; 286 287 pr_debug("src=%pI6 - ", &fl6->saddr); 288 } 289 290 rcu_read_lock(); 291 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final); 292 rcu_read_unlock(); 293 294 dst = ip6_dst_lookup_flow(sk, fl6, final_p); 295 if (!asoc || saddr) 296 goto out; 297 298 bp = &asoc->base.bind_addr; 299 scope = sctp_scope(daddr); 300 /* ip6_dst_lookup has filled in the fl6->saddr for us. Check 301 * to see if we can use it. 302 */ 303 if (!IS_ERR(dst)) { 304 /* Walk through the bind address list and look for a bind 305 * address that matches the source address of the returned dst. 306 */ 307 sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port)); 308 rcu_read_lock(); 309 list_for_each_entry_rcu(laddr, &bp->address_list, list) { 310 if (!laddr->valid || laddr->state == SCTP_ADDR_DEL || 311 (laddr->state != SCTP_ADDR_SRC && 312 !asoc->src_out_of_asoc_ok)) 313 continue; 314 315 /* Do not compare against v4 addrs */ 316 if ((laddr->a.sa.sa_family == AF_INET6) && 317 (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) { 318 rcu_read_unlock(); 319 goto out; 320 } 321 } 322 rcu_read_unlock(); 323 /* None of the bound addresses match the source address of the 324 * dst. So release it. 325 */ 326 dst_release(dst); 327 dst = NULL; 328 } 329 330 /* Walk through the bind address list and try to get the 331 * best source address for a given destination. 332 */ 333 rcu_read_lock(); 334 list_for_each_entry_rcu(laddr, &bp->address_list, list) { 335 struct dst_entry *bdst; 336 __u8 bmatchlen; 337 338 if (!laddr->valid || 339 laddr->state != SCTP_ADDR_SRC || 340 laddr->a.sa.sa_family != AF_INET6 || 341 scope > sctp_scope(&laddr->a)) 342 continue; 343 344 fl6->saddr = laddr->a.v6.sin6_addr; 345 fl6->fl6_sport = laddr->a.v6.sin6_port; 346 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final); 347 bdst = ip6_dst_lookup_flow(sk, fl6, final_p); 348 349 if (IS_ERR(bdst)) 350 continue; 351 352 if (ipv6_chk_addr(dev_net(bdst->dev), 353 &laddr->a.v6.sin6_addr, bdst->dev, 1)) { 354 if (!IS_ERR_OR_NULL(dst)) 355 dst_release(dst); 356 dst = bdst; 357 break; 358 } 359 360 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a); 361 if (matchlen > bmatchlen) { 362 dst_release(bdst); 363 continue; 364 } 365 366 if (!IS_ERR_OR_NULL(dst)) 367 dst_release(dst); 368 dst = bdst; 369 matchlen = bmatchlen; 370 } 371 rcu_read_unlock(); 372 373 out: 374 if (!IS_ERR_OR_NULL(dst)) { 375 struct rt6_info *rt; 376 377 rt = (struct rt6_info *)dst; 378 t->dst = dst; 379 t->dst_cookie = rt6_get_cookie(rt); 380 pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n", 381 &rt->rt6i_dst.addr, rt->rt6i_dst.plen, 382 &fl6->saddr); 383 } else { 384 t->dst = NULL; 385 386 pr_debug("no route\n"); 387 } 388 } 389 390 /* Returns the number of consecutive initial bits that match in the 2 ipv6 391 * addresses. 392 */ 393 static inline int sctp_v6_addr_match_len(union sctp_addr *s1, 394 union sctp_addr *s2) 395 { 396 return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr); 397 } 398 399 /* Fills in the source address(saddr) based on the destination address(daddr) 400 * and asoc's bind address list. 401 */ 402 static void sctp_v6_get_saddr(struct sctp_sock *sk, 403 struct sctp_transport *t, 404 struct flowi *fl) 405 { 406 struct flowi6 *fl6 = &fl->u.ip6; 407 union sctp_addr *saddr = &t->saddr; 408 409 pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst); 410 411 if (t->dst) { 412 saddr->v6.sin6_family = AF_INET6; 413 saddr->v6.sin6_addr = fl6->saddr; 414 } 415 } 416 417 /* Make a copy of all potential local addresses. */ 418 static void sctp_v6_copy_addrlist(struct list_head *addrlist, 419 struct net_device *dev) 420 { 421 struct inet6_dev *in6_dev; 422 struct inet6_ifaddr *ifp; 423 struct sctp_sockaddr_entry *addr; 424 425 rcu_read_lock(); 426 if ((in6_dev = __in6_dev_get(dev)) == NULL) { 427 rcu_read_unlock(); 428 return; 429 } 430 431 read_lock_bh(&in6_dev->lock); 432 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) { 433 /* Add the address to the local list. */ 434 addr = kzalloc(sizeof(*addr), GFP_ATOMIC); 435 if (addr) { 436 addr->a.v6.sin6_family = AF_INET6; 437 addr->a.v6.sin6_port = 0; 438 addr->a.v6.sin6_addr = ifp->addr; 439 addr->a.v6.sin6_scope_id = dev->ifindex; 440 addr->valid = 1; 441 INIT_LIST_HEAD(&addr->list); 442 list_add_tail(&addr->list, addrlist); 443 } 444 } 445 446 read_unlock_bh(&in6_dev->lock); 447 rcu_read_unlock(); 448 } 449 450 /* Copy over any ip options */ 451 static void sctp_v6_copy_ip_options(struct sock *sk, struct sock *newsk) 452 { 453 struct ipv6_pinfo *newnp, *np = inet6_sk(sk); 454 struct ipv6_txoptions *opt; 455 456 newnp = inet6_sk(newsk); 457 458 rcu_read_lock(); 459 opt = rcu_dereference(np->opt); 460 if (opt) { 461 opt = ipv6_dup_options(newsk, opt); 462 if (!opt) 463 pr_err("%s: Failed to copy ip options\n", __func__); 464 } 465 RCU_INIT_POINTER(newnp->opt, opt); 466 rcu_read_unlock(); 467 } 468 469 /* Account for the IP options */ 470 static int sctp_v6_ip_options_len(struct sock *sk) 471 { 472 struct ipv6_pinfo *np = inet6_sk(sk); 473 struct ipv6_txoptions *opt; 474 int len = 0; 475 476 rcu_read_lock(); 477 opt = rcu_dereference(np->opt); 478 if (opt) 479 len = opt->opt_flen + opt->opt_nflen; 480 481 rcu_read_unlock(); 482 return len; 483 } 484 485 /* Initialize a sockaddr_storage from in incoming skb. */ 486 static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb, 487 int is_saddr) 488 { 489 /* Always called on head skb, so this is safe */ 490 struct sctphdr *sh = sctp_hdr(skb); 491 struct sockaddr_in6 *sa = &addr->v6; 492 493 addr->v6.sin6_family = AF_INET6; 494 addr->v6.sin6_flowinfo = 0; /* FIXME */ 495 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif; 496 497 if (is_saddr) { 498 sa->sin6_port = sh->source; 499 sa->sin6_addr = ipv6_hdr(skb)->saddr; 500 } else { 501 sa->sin6_port = sh->dest; 502 sa->sin6_addr = ipv6_hdr(skb)->daddr; 503 } 504 } 505 506 /* Initialize an sctp_addr from a socket. */ 507 static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk) 508 { 509 addr->v6.sin6_family = AF_INET6; 510 addr->v6.sin6_port = 0; 511 addr->v6.sin6_addr = sk->sk_v6_rcv_saddr; 512 } 513 514 /* Initialize sk->sk_rcv_saddr from sctp_addr. */ 515 static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk) 516 { 517 if (addr->sa.sa_family == AF_INET) { 518 sk->sk_v6_rcv_saddr.s6_addr32[0] = 0; 519 sk->sk_v6_rcv_saddr.s6_addr32[1] = 0; 520 sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff); 521 sk->sk_v6_rcv_saddr.s6_addr32[3] = 522 addr->v4.sin_addr.s_addr; 523 } else { 524 sk->sk_v6_rcv_saddr = addr->v6.sin6_addr; 525 } 526 } 527 528 /* Initialize sk->sk_daddr from sctp_addr. */ 529 static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk) 530 { 531 if (addr->sa.sa_family == AF_INET) { 532 sk->sk_v6_daddr.s6_addr32[0] = 0; 533 sk->sk_v6_daddr.s6_addr32[1] = 0; 534 sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff); 535 sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr; 536 } else { 537 sk->sk_v6_daddr = addr->v6.sin6_addr; 538 } 539 } 540 541 /* Initialize a sctp_addr from an address parameter. */ 542 static void sctp_v6_from_addr_param(union sctp_addr *addr, 543 union sctp_addr_param *param, 544 __be16 port, int iif) 545 { 546 addr->v6.sin6_family = AF_INET6; 547 addr->v6.sin6_port = port; 548 addr->v6.sin6_flowinfo = 0; /* BUG */ 549 addr->v6.sin6_addr = param->v6.addr; 550 addr->v6.sin6_scope_id = iif; 551 } 552 553 /* Initialize an address parameter from a sctp_addr and return the length 554 * of the address parameter. 555 */ 556 static int sctp_v6_to_addr_param(const union sctp_addr *addr, 557 union sctp_addr_param *param) 558 { 559 int length = sizeof(struct sctp_ipv6addr_param); 560 561 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS; 562 param->v6.param_hdr.length = htons(length); 563 param->v6.addr = addr->v6.sin6_addr; 564 565 return length; 566 } 567 568 /* Initialize a sctp_addr from struct in6_addr. */ 569 static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr, 570 __be16 port) 571 { 572 addr->sa.sa_family = AF_INET6; 573 addr->v6.sin6_port = port; 574 addr->v6.sin6_flowinfo = 0; 575 addr->v6.sin6_addr = *saddr; 576 addr->v6.sin6_scope_id = 0; 577 } 578 579 static int __sctp_v6_cmp_addr(const union sctp_addr *addr1, 580 const union sctp_addr *addr2) 581 { 582 if (addr1->sa.sa_family != addr2->sa.sa_family) { 583 if (addr1->sa.sa_family == AF_INET && 584 addr2->sa.sa_family == AF_INET6 && 585 ipv6_addr_v4mapped(&addr2->v6.sin6_addr) && 586 addr2->v6.sin6_addr.s6_addr32[3] == 587 addr1->v4.sin_addr.s_addr) 588 return 1; 589 590 if (addr2->sa.sa_family == AF_INET && 591 addr1->sa.sa_family == AF_INET6 && 592 ipv6_addr_v4mapped(&addr1->v6.sin6_addr) && 593 addr1->v6.sin6_addr.s6_addr32[3] == 594 addr2->v4.sin_addr.s_addr) 595 return 1; 596 597 return 0; 598 } 599 600 if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr)) 601 return 0; 602 603 /* If this is a linklocal address, compare the scope_id. */ 604 if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) && 605 addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id && 606 addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id) 607 return 0; 608 609 return 1; 610 } 611 612 /* Compare addresses exactly. 613 * v4-mapped-v6 is also in consideration. 614 */ 615 static int sctp_v6_cmp_addr(const union sctp_addr *addr1, 616 const union sctp_addr *addr2) 617 { 618 return __sctp_v6_cmp_addr(addr1, addr2) && 619 addr1->v6.sin6_port == addr2->v6.sin6_port; 620 } 621 622 /* Initialize addr struct to INADDR_ANY. */ 623 static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port) 624 { 625 memset(addr, 0x00, sizeof(union sctp_addr)); 626 addr->v6.sin6_family = AF_INET6; 627 addr->v6.sin6_port = port; 628 } 629 630 /* Is this a wildcard address? */ 631 static int sctp_v6_is_any(const union sctp_addr *addr) 632 { 633 return ipv6_addr_any(&addr->v6.sin6_addr); 634 } 635 636 /* Should this be available for binding? */ 637 static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp) 638 { 639 int type; 640 struct net *net = sock_net(&sp->inet.sk); 641 const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr; 642 643 type = ipv6_addr_type(in6); 644 if (IPV6_ADDR_ANY == type) 645 return 1; 646 if (type == IPV6_ADDR_MAPPED) { 647 if (sp && ipv6_only_sock(sctp_opt2sk(sp))) 648 return 0; 649 sctp_v6_map_v4(addr); 650 return sctp_get_af_specific(AF_INET)->available(addr, sp); 651 } 652 if (!(type & IPV6_ADDR_UNICAST)) 653 return 0; 654 655 return sp->inet.freebind || net->ipv6.sysctl.ip_nonlocal_bind || 656 ipv6_chk_addr(net, in6, NULL, 0); 657 } 658 659 /* This function checks if the address is a valid address to be used for 660 * SCTP. 661 * 662 * Output: 663 * Return 0 - If the address is a non-unicast or an illegal address. 664 * Return 1 - If the address is a unicast. 665 */ 666 static int sctp_v6_addr_valid(union sctp_addr *addr, 667 struct sctp_sock *sp, 668 const struct sk_buff *skb) 669 { 670 int ret = ipv6_addr_type(&addr->v6.sin6_addr); 671 672 /* Support v4-mapped-v6 address. */ 673 if (ret == IPV6_ADDR_MAPPED) { 674 /* Note: This routine is used in input, so v4-mapped-v6 675 * are disallowed here when there is no sctp_sock. 676 */ 677 if (sp && ipv6_only_sock(sctp_opt2sk(sp))) 678 return 0; 679 sctp_v6_map_v4(addr); 680 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb); 681 } 682 683 /* Is this a non-unicast address */ 684 if (!(ret & IPV6_ADDR_UNICAST)) 685 return 0; 686 687 return 1; 688 } 689 690 /* What is the scope of 'addr'? */ 691 static enum sctp_scope sctp_v6_scope(union sctp_addr *addr) 692 { 693 enum sctp_scope retval; 694 int v6scope; 695 696 /* The IPv6 scope is really a set of bit fields. 697 * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope. 698 */ 699 700 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr); 701 switch (v6scope) { 702 case IFA_HOST: 703 retval = SCTP_SCOPE_LOOPBACK; 704 break; 705 case IFA_LINK: 706 retval = SCTP_SCOPE_LINK; 707 break; 708 case IFA_SITE: 709 retval = SCTP_SCOPE_PRIVATE; 710 break; 711 default: 712 retval = SCTP_SCOPE_GLOBAL; 713 break; 714 } 715 716 return retval; 717 } 718 719 /* Create and initialize a new sk for the socket to be returned by accept(). */ 720 static struct sock *sctp_v6_create_accept_sk(struct sock *sk, 721 struct sctp_association *asoc, 722 bool kern) 723 { 724 struct sock *newsk; 725 struct ipv6_pinfo *newnp, *np = inet6_sk(sk); 726 struct sctp6_sock *newsctp6sk; 727 728 newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern); 729 if (!newsk) 730 goto out; 731 732 sock_init_data(NULL, newsk); 733 734 sctp_copy_sock(newsk, sk, asoc); 735 sock_reset_flag(sk, SOCK_ZAPPED); 736 737 newsctp6sk = (struct sctp6_sock *)newsk; 738 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6; 739 740 sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped; 741 742 newnp = inet6_sk(newsk); 743 744 memcpy(newnp, np, sizeof(struct ipv6_pinfo)); 745 newnp->ipv6_mc_list = NULL; 746 newnp->ipv6_ac_list = NULL; 747 newnp->ipv6_fl_list = NULL; 748 749 sctp_v6_copy_ip_options(sk, newsk); 750 751 /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname() 752 * and getpeername(). 753 */ 754 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk); 755 756 newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr; 757 758 sk_refcnt_debug_inc(newsk); 759 760 if (newsk->sk_prot->init(newsk)) { 761 sk_common_release(newsk); 762 newsk = NULL; 763 } 764 765 out: 766 return newsk; 767 } 768 769 /* Format a sockaddr for return to user space. This makes sure the return is 770 * AF_INET or AF_INET6 depending on the SCTP_I_WANT_MAPPED_V4_ADDR option. 771 */ 772 static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr) 773 { 774 if (sp->v4mapped) { 775 if (addr->sa.sa_family == AF_INET) 776 sctp_v4_map_v6(addr); 777 } else { 778 if (addr->sa.sa_family == AF_INET6 && 779 ipv6_addr_v4mapped(&addr->v6.sin6_addr)) 780 sctp_v6_map_v4(addr); 781 } 782 783 if (addr->sa.sa_family == AF_INET) { 784 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero)); 785 return sizeof(struct sockaddr_in); 786 } 787 return sizeof(struct sockaddr_in6); 788 } 789 790 /* Where did this skb come from? */ 791 static int sctp_v6_skb_iif(const struct sk_buff *skb) 792 { 793 return IP6CB(skb)->iif; 794 } 795 796 /* Was this packet marked by Explicit Congestion Notification? */ 797 static int sctp_v6_is_ce(const struct sk_buff *skb) 798 { 799 return *((__u32 *)(ipv6_hdr(skb))) & (__force __u32)htonl(1 << 20); 800 } 801 802 /* Dump the v6 addr to the seq file. */ 803 static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr) 804 { 805 seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr); 806 } 807 808 static void sctp_v6_ecn_capable(struct sock *sk) 809 { 810 inet6_sk(sk)->tclass |= INET_ECN_ECT_0; 811 } 812 813 /* Initialize a PF_INET msgname from a ulpevent. */ 814 static void sctp_inet6_event_msgname(struct sctp_ulpevent *event, 815 char *msgname, int *addrlen) 816 { 817 union sctp_addr *addr; 818 struct sctp_association *asoc; 819 union sctp_addr *paddr; 820 821 if (!msgname) 822 return; 823 824 addr = (union sctp_addr *)msgname; 825 asoc = event->asoc; 826 paddr = &asoc->peer.primary_addr; 827 828 if (paddr->sa.sa_family == AF_INET) { 829 addr->v4.sin_family = AF_INET; 830 addr->v4.sin_port = htons(asoc->peer.port); 831 addr->v4.sin_addr = paddr->v4.sin_addr; 832 } else { 833 addr->v6.sin6_family = AF_INET6; 834 addr->v6.sin6_flowinfo = 0; 835 if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) 836 addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id; 837 else 838 addr->v6.sin6_scope_id = 0; 839 addr->v6.sin6_port = htons(asoc->peer.port); 840 addr->v6.sin6_addr = paddr->v6.sin6_addr; 841 } 842 843 *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr); 844 } 845 846 /* Initialize a msg_name from an inbound skb. */ 847 static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname, 848 int *addr_len) 849 { 850 union sctp_addr *addr; 851 struct sctphdr *sh; 852 853 if (!msgname) 854 return; 855 856 addr = (union sctp_addr *)msgname; 857 sh = sctp_hdr(skb); 858 859 if (ip_hdr(skb)->version == 4) { 860 addr->v4.sin_family = AF_INET; 861 addr->v4.sin_port = sh->source; 862 addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr; 863 } else { 864 addr->v6.sin6_family = AF_INET6; 865 addr->v6.sin6_flowinfo = 0; 866 addr->v6.sin6_port = sh->source; 867 addr->v6.sin6_addr = ipv6_hdr(skb)->saddr; 868 if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) 869 addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb); 870 else 871 addr->v6.sin6_scope_id = 0; 872 } 873 874 *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr); 875 } 876 877 /* Do we support this AF? */ 878 static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp) 879 { 880 switch (family) { 881 case AF_INET6: 882 return 1; 883 /* v4-mapped-v6 addresses */ 884 case AF_INET: 885 if (!__ipv6_only_sock(sctp_opt2sk(sp))) 886 return 1; 887 /* fallthru */ 888 default: 889 return 0; 890 } 891 } 892 893 /* Address matching with wildcards allowed. This extra level 894 * of indirection lets us choose whether a PF_INET6 should 895 * disallow any v4 addresses if we so choose. 896 */ 897 static int sctp_inet6_cmp_addr(const union sctp_addr *addr1, 898 const union sctp_addr *addr2, 899 struct sctp_sock *opt) 900 { 901 struct sock *sk = sctp_opt2sk(opt); 902 struct sctp_af *af1, *af2; 903 904 af1 = sctp_get_af_specific(addr1->sa.sa_family); 905 af2 = sctp_get_af_specific(addr2->sa.sa_family); 906 907 if (!af1 || !af2) 908 return 0; 909 910 /* If the socket is IPv6 only, v4 addrs will not match */ 911 if (__ipv6_only_sock(sk) && af1 != af2) 912 return 0; 913 914 /* Today, wildcard AF_INET/AF_INET6. */ 915 if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2)) 916 return 1; 917 918 if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET) 919 return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr; 920 921 return __sctp_v6_cmp_addr(addr1, addr2); 922 } 923 924 /* Verify that the provided sockaddr looks bindable. Common verification, 925 * has already been taken care of. 926 */ 927 static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr) 928 { 929 struct sctp_af *af; 930 931 /* ASSERT: address family has already been verified. */ 932 if (addr->sa.sa_family != AF_INET6) 933 af = sctp_get_af_specific(addr->sa.sa_family); 934 else { 935 int type = ipv6_addr_type(&addr->v6.sin6_addr); 936 struct net_device *dev; 937 938 if (type & IPV6_ADDR_LINKLOCAL) { 939 struct net *net; 940 if (!addr->v6.sin6_scope_id) 941 return 0; 942 net = sock_net(&opt->inet.sk); 943 rcu_read_lock(); 944 dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id); 945 if (!dev || !(opt->inet.freebind || 946 net->ipv6.sysctl.ip_nonlocal_bind || 947 ipv6_chk_addr(net, &addr->v6.sin6_addr, 948 dev, 0))) { 949 rcu_read_unlock(); 950 return 0; 951 } 952 rcu_read_unlock(); 953 } 954 955 af = opt->pf->af; 956 } 957 return af->available(addr, opt); 958 } 959 960 /* Verify that the provided sockaddr looks sendable. Common verification, 961 * has already been taken care of. 962 */ 963 static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr) 964 { 965 struct sctp_af *af = NULL; 966 967 /* ASSERT: address family has already been verified. */ 968 if (addr->sa.sa_family != AF_INET6) 969 af = sctp_get_af_specific(addr->sa.sa_family); 970 else { 971 int type = ipv6_addr_type(&addr->v6.sin6_addr); 972 struct net_device *dev; 973 974 if (type & IPV6_ADDR_LINKLOCAL) { 975 if (!addr->v6.sin6_scope_id) 976 return 0; 977 rcu_read_lock(); 978 dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk), 979 addr->v6.sin6_scope_id); 980 rcu_read_unlock(); 981 if (!dev) 982 return 0; 983 } 984 af = opt->pf->af; 985 } 986 987 return af != NULL; 988 } 989 990 /* Fill in Supported Address Type information for INIT and INIT-ACK 991 * chunks. Note: In the future, we may want to look at sock options 992 * to determine whether a PF_INET6 socket really wants to have IPV4 993 * addresses. 994 * Returns number of addresses supported. 995 */ 996 static int sctp_inet6_supported_addrs(const struct sctp_sock *opt, 997 __be16 *types) 998 { 999 types[0] = SCTP_PARAM_IPV6_ADDRESS; 1000 if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) { 1001 types[1] = SCTP_PARAM_IPV4_ADDRESS; 1002 return 2; 1003 } 1004 return 1; 1005 } 1006 1007 /* Handle SCTP_I_WANT_MAPPED_V4_ADDR for getpeername() and getsockname() */ 1008 static int sctp_getname(struct socket *sock, struct sockaddr *uaddr, 1009 int peer) 1010 { 1011 int rc; 1012 1013 rc = inet6_getname(sock, uaddr, peer); 1014 1015 if (rc < 0) 1016 return rc; 1017 1018 rc = sctp_v6_addr_to_user(sctp_sk(sock->sk), 1019 (union sctp_addr *)uaddr); 1020 1021 return rc; 1022 } 1023 1024 static const struct proto_ops inet6_seqpacket_ops = { 1025 .family = PF_INET6, 1026 .owner = THIS_MODULE, 1027 .release = inet6_release, 1028 .bind = inet6_bind, 1029 .connect = sctp_inet_connect, 1030 .socketpair = sock_no_socketpair, 1031 .accept = inet_accept, 1032 .getname = sctp_getname, 1033 .poll = sctp_poll, 1034 .ioctl = inet6_ioctl, 1035 .listen = sctp_inet_listen, 1036 .shutdown = inet_shutdown, 1037 .setsockopt = sock_common_setsockopt, 1038 .getsockopt = sock_common_getsockopt, 1039 .sendmsg = inet_sendmsg, 1040 .recvmsg = inet_recvmsg, 1041 .mmap = sock_no_mmap, 1042 #ifdef CONFIG_COMPAT 1043 .compat_setsockopt = compat_sock_common_setsockopt, 1044 .compat_getsockopt = compat_sock_common_getsockopt, 1045 #endif 1046 }; 1047 1048 static struct inet_protosw sctpv6_seqpacket_protosw = { 1049 .type = SOCK_SEQPACKET, 1050 .protocol = IPPROTO_SCTP, 1051 .prot = &sctpv6_prot, 1052 .ops = &inet6_seqpacket_ops, 1053 .flags = SCTP_PROTOSW_FLAG 1054 }; 1055 static struct inet_protosw sctpv6_stream_protosw = { 1056 .type = SOCK_STREAM, 1057 .protocol = IPPROTO_SCTP, 1058 .prot = &sctpv6_prot, 1059 .ops = &inet6_seqpacket_ops, 1060 .flags = SCTP_PROTOSW_FLAG, 1061 }; 1062 1063 static int sctp6_rcv(struct sk_buff *skb) 1064 { 1065 return sctp_rcv(skb) ? -1 : 0; 1066 } 1067 1068 static const struct inet6_protocol sctpv6_protocol = { 1069 .handler = sctp6_rcv, 1070 .err_handler = sctp_v6_err, 1071 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL, 1072 }; 1073 1074 static struct sctp_af sctp_af_inet6 = { 1075 .sa_family = AF_INET6, 1076 .sctp_xmit = sctp_v6_xmit, 1077 .setsockopt = ipv6_setsockopt, 1078 .getsockopt = ipv6_getsockopt, 1079 .get_dst = sctp_v6_get_dst, 1080 .get_saddr = sctp_v6_get_saddr, 1081 .copy_addrlist = sctp_v6_copy_addrlist, 1082 .from_skb = sctp_v6_from_skb, 1083 .from_sk = sctp_v6_from_sk, 1084 .from_addr_param = sctp_v6_from_addr_param, 1085 .to_addr_param = sctp_v6_to_addr_param, 1086 .cmp_addr = sctp_v6_cmp_addr, 1087 .scope = sctp_v6_scope, 1088 .addr_valid = sctp_v6_addr_valid, 1089 .inaddr_any = sctp_v6_inaddr_any, 1090 .is_any = sctp_v6_is_any, 1091 .available = sctp_v6_available, 1092 .skb_iif = sctp_v6_skb_iif, 1093 .is_ce = sctp_v6_is_ce, 1094 .seq_dump_addr = sctp_v6_seq_dump_addr, 1095 .ecn_capable = sctp_v6_ecn_capable, 1096 .net_header_len = sizeof(struct ipv6hdr), 1097 .sockaddr_len = sizeof(struct sockaddr_in6), 1098 .ip_options_len = sctp_v6_ip_options_len, 1099 #ifdef CONFIG_COMPAT 1100 .compat_setsockopt = compat_ipv6_setsockopt, 1101 .compat_getsockopt = compat_ipv6_getsockopt, 1102 #endif 1103 }; 1104 1105 static struct sctp_pf sctp_pf_inet6 = { 1106 .event_msgname = sctp_inet6_event_msgname, 1107 .skb_msgname = sctp_inet6_skb_msgname, 1108 .af_supported = sctp_inet6_af_supported, 1109 .cmp_addr = sctp_inet6_cmp_addr, 1110 .bind_verify = sctp_inet6_bind_verify, 1111 .send_verify = sctp_inet6_send_verify, 1112 .supported_addrs = sctp_inet6_supported_addrs, 1113 .create_accept_sk = sctp_v6_create_accept_sk, 1114 .addr_to_user = sctp_v6_addr_to_user, 1115 .to_sk_saddr = sctp_v6_to_sk_saddr, 1116 .to_sk_daddr = sctp_v6_to_sk_daddr, 1117 .copy_ip_options = sctp_v6_copy_ip_options, 1118 .af = &sctp_af_inet6, 1119 }; 1120 1121 /* Initialize IPv6 support and register with socket layer. */ 1122 void sctp_v6_pf_init(void) 1123 { 1124 /* Register the SCTP specific PF_INET6 functions. */ 1125 sctp_register_pf(&sctp_pf_inet6, PF_INET6); 1126 1127 /* Register the SCTP specific AF_INET6 functions. */ 1128 sctp_register_af(&sctp_af_inet6); 1129 } 1130 1131 void sctp_v6_pf_exit(void) 1132 { 1133 list_del(&sctp_af_inet6.list); 1134 } 1135 1136 /* Initialize IPv6 support and register with socket layer. */ 1137 int sctp_v6_protosw_init(void) 1138 { 1139 int rc; 1140 1141 rc = proto_register(&sctpv6_prot, 1); 1142 if (rc) 1143 return rc; 1144 1145 /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */ 1146 inet6_register_protosw(&sctpv6_seqpacket_protosw); 1147 inet6_register_protosw(&sctpv6_stream_protosw); 1148 1149 return 0; 1150 } 1151 1152 void sctp_v6_protosw_exit(void) 1153 { 1154 inet6_unregister_protosw(&sctpv6_seqpacket_protosw); 1155 inet6_unregister_protosw(&sctpv6_stream_protosw); 1156 proto_unregister(&sctpv6_prot); 1157 } 1158 1159 1160 /* Register with inet6 layer. */ 1161 int sctp_v6_add_protocol(void) 1162 { 1163 /* Register notifier for inet6 address additions/deletions. */ 1164 register_inet6addr_notifier(&sctp_inet6addr_notifier); 1165 1166 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0) 1167 return -EAGAIN; 1168 1169 return 0; 1170 } 1171 1172 /* Unregister with inet6 layer. */ 1173 void sctp_v6_del_protocol(void) 1174 { 1175 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP); 1176 unregister_inet6addr_notifier(&sctp_inet6addr_notifier); 1177 } 1178