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