0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0029
0030 #include <linux/module.h>
0031 #include <linux/errno.h>
0032 #include <linux/types.h>
0033 #include <linux/socket.h>
0034 #include <linux/sockios.h>
0035 #include <linux/net.h>
0036 #include <linux/in.h>
0037 #include <linux/in6.h>
0038 #include <linux/netdevice.h>
0039 #include <linux/init.h>
0040 #include <linux/ipsec.h>
0041 #include <linux/slab.h>
0042
0043 #include <linux/ipv6.h>
0044 #include <linux/icmpv6.h>
0045 #include <linux/random.h>
0046 #include <linux/seq_file.h>
0047
0048 #include <net/protocol.h>
0049 #include <net/ndisc.h>
0050 #include <net/ip.h>
0051 #include <net/ipv6.h>
0052 #include <net/transp_v6.h>
0053 #include <net/addrconf.h>
0054 #include <net/ip6_route.h>
0055 #include <net/inet_common.h>
0056 #include <net/inet_ecn.h>
0057 #include <net/sctp/sctp.h>
0058 #include <net/udp_tunnel.h>
0059
0060 #include <linux/uaccess.h>
0061
0062 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
0063 union sctp_addr *s2);
0064 static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
0065 __be16 port);
0066 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
0067 const union sctp_addr *addr2);
0068
0069
0070
0071
0072
0073
0074
0075 static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
0076 void *ptr)
0077 {
0078 struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
0079 struct sctp_sockaddr_entry *addr = NULL;
0080 struct sctp_sockaddr_entry *temp;
0081 struct net *net = dev_net(ifa->idev->dev);
0082 int found = 0;
0083
0084 switch (ev) {
0085 case NETDEV_UP:
0086 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
0087 if (addr) {
0088 addr->a.v6.sin6_family = AF_INET6;
0089 addr->a.v6.sin6_addr = ifa->addr;
0090 addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
0091 addr->valid = 1;
0092 spin_lock_bh(&net->sctp.local_addr_lock);
0093 list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
0094 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
0095 spin_unlock_bh(&net->sctp.local_addr_lock);
0096 }
0097 break;
0098 case NETDEV_DOWN:
0099 spin_lock_bh(&net->sctp.local_addr_lock);
0100 list_for_each_entry_safe(addr, temp,
0101 &net->sctp.local_addr_list, list) {
0102 if (addr->a.sa.sa_family == AF_INET6 &&
0103 ipv6_addr_equal(&addr->a.v6.sin6_addr,
0104 &ifa->addr) &&
0105 addr->a.v6.sin6_scope_id == ifa->idev->dev->ifindex) {
0106 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
0107 found = 1;
0108 addr->valid = 0;
0109 list_del_rcu(&addr->list);
0110 break;
0111 }
0112 }
0113 spin_unlock_bh(&net->sctp.local_addr_lock);
0114 if (found)
0115 kfree_rcu(addr, rcu);
0116 break;
0117 }
0118
0119 return NOTIFY_DONE;
0120 }
0121
0122 static struct notifier_block sctp_inet6addr_notifier = {
0123 .notifier_call = sctp_inet6addr_event,
0124 };
0125
0126 static void sctp_v6_err_handle(struct sctp_transport *t, struct sk_buff *skb,
0127 __u8 type, __u8 code, __u32 info)
0128 {
0129 struct sctp_association *asoc = t->asoc;
0130 struct sock *sk = asoc->base.sk;
0131 struct ipv6_pinfo *np;
0132 int err = 0;
0133
0134 switch (type) {
0135 case ICMPV6_PKT_TOOBIG:
0136 if (ip6_sk_accept_pmtu(sk))
0137 sctp_icmp_frag_needed(sk, asoc, t, info);
0138 return;
0139 case ICMPV6_PARAMPROB:
0140 if (ICMPV6_UNK_NEXTHDR == code) {
0141 sctp_icmp_proto_unreachable(sk, asoc, t);
0142 return;
0143 }
0144 break;
0145 case NDISC_REDIRECT:
0146 sctp_icmp_redirect(sk, t, skb);
0147 return;
0148 default:
0149 break;
0150 }
0151
0152 np = inet6_sk(sk);
0153 icmpv6_err_convert(type, code, &err);
0154 if (!sock_owned_by_user(sk) && np->recverr) {
0155 sk->sk_err = err;
0156 sk_error_report(sk);
0157 } else {
0158 sk->sk_err_soft = err;
0159 }
0160 }
0161
0162
0163 static int sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
0164 u8 type, u8 code, int offset, __be32 info)
0165 {
0166 struct net *net = dev_net(skb->dev);
0167 struct sctp_transport *transport;
0168 struct sctp_association *asoc;
0169 __u16 saveip, savesctp;
0170 struct sock *sk;
0171
0172
0173 saveip = skb->network_header;
0174 savesctp = skb->transport_header;
0175 skb_reset_network_header(skb);
0176 skb_set_transport_header(skb, offset);
0177 sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
0178
0179 skb->network_header = saveip;
0180 skb->transport_header = savesctp;
0181 if (!sk) {
0182 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
0183 return -ENOENT;
0184 }
0185
0186 sctp_v6_err_handle(transport, skb, type, code, ntohl(info));
0187 sctp_err_finish(sk, transport);
0188
0189 return 0;
0190 }
0191
0192 int sctp_udp_v6_err(struct sock *sk, struct sk_buff *skb)
0193 {
0194 struct net *net = dev_net(skb->dev);
0195 struct sctp_association *asoc;
0196 struct sctp_transport *t;
0197 struct icmp6hdr *hdr;
0198 __u32 info = 0;
0199
0200 skb->transport_header += sizeof(struct udphdr);
0201 sk = sctp_err_lookup(net, AF_INET6, skb, sctp_hdr(skb), &asoc, &t);
0202 if (!sk) {
0203 __ICMP6_INC_STATS(net, __in6_dev_get(skb->dev), ICMP6_MIB_INERRORS);
0204 return -ENOENT;
0205 }
0206
0207 skb->transport_header -= sizeof(struct udphdr);
0208 hdr = (struct icmp6hdr *)(skb_network_header(skb) - sizeof(struct icmp6hdr));
0209 if (hdr->icmp6_type == NDISC_REDIRECT) {
0210
0211 sctp_err_finish(sk, t);
0212 return 0;
0213 }
0214 if (hdr->icmp6_type == ICMPV6_PKT_TOOBIG)
0215 info = ntohl(hdr->icmp6_mtu);
0216 sctp_v6_err_handle(t, skb, hdr->icmp6_type, hdr->icmp6_code, info);
0217
0218 sctp_err_finish(sk, t);
0219 return 1;
0220 }
0221
0222 static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *t)
0223 {
0224 struct dst_entry *dst = dst_clone(t->dst);
0225 struct flowi6 *fl6 = &t->fl.u.ip6;
0226 struct sock *sk = skb->sk;
0227 struct ipv6_pinfo *np = inet6_sk(sk);
0228 __u8 tclass = np->tclass;
0229 __be32 label;
0230
0231 pr_debug("%s: skb:%p, len:%d, src:%pI6 dst:%pI6\n", __func__, skb,
0232 skb->len, &fl6->saddr, &fl6->daddr);
0233
0234 if (t->dscp & SCTP_DSCP_SET_MASK)
0235 tclass = t->dscp & SCTP_DSCP_VAL_MASK;
0236
0237 if (INET_ECN_is_capable(tclass))
0238 IP6_ECN_flow_xmit(sk, fl6->flowlabel);
0239
0240 if (!(t->param_flags & SPP_PMTUD_ENABLE))
0241 skb->ignore_df = 1;
0242
0243 SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
0244
0245 if (!t->encap_port || !sctp_sk(sk)->udp_port) {
0246 int res;
0247
0248 skb_dst_set(skb, dst);
0249 rcu_read_lock();
0250 res = ip6_xmit(sk, skb, fl6, sk->sk_mark,
0251 rcu_dereference(np->opt),
0252 tclass, sk->sk_priority);
0253 rcu_read_unlock();
0254 return res;
0255 }
0256
0257 if (skb_is_gso(skb))
0258 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
0259
0260 skb->encapsulation = 1;
0261 skb_reset_inner_mac_header(skb);
0262 skb_reset_inner_transport_header(skb);
0263 skb_set_inner_ipproto(skb, IPPROTO_SCTP);
0264 label = ip6_make_flowlabel(sock_net(sk), skb, fl6->flowlabel, true, fl6);
0265
0266 return udp_tunnel6_xmit_skb(dst, sk, skb, NULL, &fl6->saddr,
0267 &fl6->daddr, tclass, ip6_dst_hoplimit(dst),
0268 label, sctp_sk(sk)->udp_port, t->encap_port, false);
0269 }
0270
0271
0272
0273
0274 static void sctp_v6_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
0275 struct flowi *fl, struct sock *sk)
0276 {
0277 struct sctp_association *asoc = t->asoc;
0278 struct dst_entry *dst = NULL;
0279 struct flowi _fl;
0280 struct flowi6 *fl6 = &_fl.u.ip6;
0281 struct sctp_bind_addr *bp;
0282 struct ipv6_pinfo *np = inet6_sk(sk);
0283 struct sctp_sockaddr_entry *laddr;
0284 union sctp_addr *daddr = &t->ipaddr;
0285 union sctp_addr dst_saddr;
0286 struct in6_addr *final_p, final;
0287 enum sctp_scope scope;
0288 __u8 matchlen = 0;
0289
0290 memset(&_fl, 0, sizeof(_fl));
0291 fl6->daddr = daddr->v6.sin6_addr;
0292 fl6->fl6_dport = daddr->v6.sin6_port;
0293 fl6->flowi6_proto = IPPROTO_SCTP;
0294 if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
0295 fl6->flowi6_oif = daddr->v6.sin6_scope_id;
0296 else if (asoc)
0297 fl6->flowi6_oif = asoc->base.sk->sk_bound_dev_if;
0298 if (t->flowlabel & SCTP_FLOWLABEL_SET_MASK)
0299 fl6->flowlabel = htonl(t->flowlabel & SCTP_FLOWLABEL_VAL_MASK);
0300
0301 if (np->sndflow && (fl6->flowlabel & IPV6_FLOWLABEL_MASK)) {
0302 struct ip6_flowlabel *flowlabel;
0303
0304 flowlabel = fl6_sock_lookup(sk, fl6->flowlabel);
0305 if (IS_ERR(flowlabel))
0306 goto out;
0307 fl6_sock_release(flowlabel);
0308 }
0309
0310 pr_debug("%s: dst=%pI6 ", __func__, &fl6->daddr);
0311
0312 if (asoc)
0313 fl6->fl6_sport = htons(asoc->base.bind_addr.port);
0314
0315 if (saddr) {
0316 fl6->saddr = saddr->v6.sin6_addr;
0317 if (!fl6->fl6_sport)
0318 fl6->fl6_sport = saddr->v6.sin6_port;
0319
0320 pr_debug("src=%pI6 - ", &fl6->saddr);
0321 }
0322
0323 rcu_read_lock();
0324 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
0325 rcu_read_unlock();
0326
0327 dst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
0328 if (!asoc || saddr) {
0329 t->dst = dst;
0330 memcpy(fl, &_fl, sizeof(_fl));
0331 goto out;
0332 }
0333
0334 bp = &asoc->base.bind_addr;
0335 scope = sctp_scope(daddr);
0336
0337
0338
0339 if (!IS_ERR(dst)) {
0340
0341
0342
0343 sctp_v6_to_addr(&dst_saddr, &fl6->saddr, htons(bp->port));
0344 rcu_read_lock();
0345 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
0346 if (!laddr->valid || laddr->state == SCTP_ADDR_DEL ||
0347 (laddr->state != SCTP_ADDR_SRC &&
0348 !asoc->src_out_of_asoc_ok))
0349 continue;
0350
0351
0352 if ((laddr->a.sa.sa_family == AF_INET6) &&
0353 (sctp_v6_cmp_addr(&dst_saddr, &laddr->a))) {
0354 rcu_read_unlock();
0355 t->dst = dst;
0356 memcpy(fl, &_fl, sizeof(_fl));
0357 goto out;
0358 }
0359 }
0360 rcu_read_unlock();
0361
0362
0363
0364 dst_release(dst);
0365 dst = NULL;
0366 }
0367
0368
0369
0370
0371 rcu_read_lock();
0372 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
0373 struct dst_entry *bdst;
0374 __u8 bmatchlen;
0375
0376 if (!laddr->valid ||
0377 laddr->state != SCTP_ADDR_SRC ||
0378 laddr->a.sa.sa_family != AF_INET6 ||
0379 scope > sctp_scope(&laddr->a))
0380 continue;
0381
0382 fl6->saddr = laddr->a.v6.sin6_addr;
0383 fl6->fl6_sport = laddr->a.v6.sin6_port;
0384 final_p = fl6_update_dst(fl6, rcu_dereference(np->opt), &final);
0385 bdst = ip6_dst_lookup_flow(sock_net(sk), sk, fl6, final_p);
0386
0387 if (IS_ERR(bdst))
0388 continue;
0389
0390 if (ipv6_chk_addr(dev_net(bdst->dev),
0391 &laddr->a.v6.sin6_addr, bdst->dev, 1)) {
0392 if (!IS_ERR_OR_NULL(dst))
0393 dst_release(dst);
0394 dst = bdst;
0395 t->dst = dst;
0396 memcpy(fl, &_fl, sizeof(_fl));
0397 break;
0398 }
0399
0400 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
0401 if (matchlen > bmatchlen) {
0402 dst_release(bdst);
0403 continue;
0404 }
0405
0406 if (!IS_ERR_OR_NULL(dst))
0407 dst_release(dst);
0408 dst = bdst;
0409 matchlen = bmatchlen;
0410 t->dst = dst;
0411 memcpy(fl, &_fl, sizeof(_fl));
0412 }
0413 rcu_read_unlock();
0414
0415 out:
0416 if (!IS_ERR_OR_NULL(dst)) {
0417 struct rt6_info *rt;
0418
0419 rt = (struct rt6_info *)dst;
0420 t->dst_cookie = rt6_get_cookie(rt);
0421 pr_debug("rt6_dst:%pI6/%d rt6_src:%pI6\n",
0422 &rt->rt6i_dst.addr, rt->rt6i_dst.plen,
0423 &fl->u.ip6.saddr);
0424 } else {
0425 t->dst = NULL;
0426 pr_debug("no route\n");
0427 }
0428 }
0429
0430
0431
0432
0433 static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
0434 union sctp_addr *s2)
0435 {
0436 return ipv6_addr_diff(&s1->v6.sin6_addr, &s2->v6.sin6_addr);
0437 }
0438
0439
0440
0441
0442 static void sctp_v6_get_saddr(struct sctp_sock *sk,
0443 struct sctp_transport *t,
0444 struct flowi *fl)
0445 {
0446 struct flowi6 *fl6 = &fl->u.ip6;
0447 union sctp_addr *saddr = &t->saddr;
0448
0449 pr_debug("%s: asoc:%p dst:%p\n", __func__, t->asoc, t->dst);
0450
0451 if (t->dst) {
0452 saddr->v6.sin6_family = AF_INET6;
0453 saddr->v6.sin6_addr = fl6->saddr;
0454 }
0455 }
0456
0457
0458 static void sctp_v6_copy_addrlist(struct list_head *addrlist,
0459 struct net_device *dev)
0460 {
0461 struct inet6_dev *in6_dev;
0462 struct inet6_ifaddr *ifp;
0463 struct sctp_sockaddr_entry *addr;
0464
0465 rcu_read_lock();
0466 if ((in6_dev = __in6_dev_get(dev)) == NULL) {
0467 rcu_read_unlock();
0468 return;
0469 }
0470
0471 read_lock_bh(&in6_dev->lock);
0472 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
0473
0474 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
0475 if (addr) {
0476 addr->a.v6.sin6_family = AF_INET6;
0477 addr->a.v6.sin6_addr = ifp->addr;
0478 addr->a.v6.sin6_scope_id = dev->ifindex;
0479 addr->valid = 1;
0480 INIT_LIST_HEAD(&addr->list);
0481 list_add_tail(&addr->list, addrlist);
0482 }
0483 }
0484
0485 read_unlock_bh(&in6_dev->lock);
0486 rcu_read_unlock();
0487 }
0488
0489
0490 static void sctp_v6_copy_ip_options(struct sock *sk, struct sock *newsk)
0491 {
0492 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
0493 struct ipv6_txoptions *opt;
0494
0495 newnp = inet6_sk(newsk);
0496
0497 rcu_read_lock();
0498 opt = rcu_dereference(np->opt);
0499 if (opt) {
0500 opt = ipv6_dup_options(newsk, opt);
0501 if (!opt)
0502 pr_err("%s: Failed to copy ip options\n", __func__);
0503 }
0504 RCU_INIT_POINTER(newnp->opt, opt);
0505 rcu_read_unlock();
0506 }
0507
0508
0509 static int sctp_v6_ip_options_len(struct sock *sk)
0510 {
0511 struct ipv6_pinfo *np = inet6_sk(sk);
0512 struct ipv6_txoptions *opt;
0513 int len = 0;
0514
0515 rcu_read_lock();
0516 opt = rcu_dereference(np->opt);
0517 if (opt)
0518 len = opt->opt_flen + opt->opt_nflen;
0519
0520 rcu_read_unlock();
0521 return len;
0522 }
0523
0524
0525 static void sctp_v6_from_skb(union sctp_addr *addr, struct sk_buff *skb,
0526 int is_saddr)
0527 {
0528
0529 struct sctphdr *sh = sctp_hdr(skb);
0530 struct sockaddr_in6 *sa = &addr->v6;
0531
0532 addr->v6.sin6_family = AF_INET6;
0533 addr->v6.sin6_flowinfo = 0;
0534 addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
0535
0536 if (is_saddr) {
0537 sa->sin6_port = sh->source;
0538 sa->sin6_addr = ipv6_hdr(skb)->saddr;
0539 } else {
0540 sa->sin6_port = sh->dest;
0541 sa->sin6_addr = ipv6_hdr(skb)->daddr;
0542 }
0543 }
0544
0545
0546 static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
0547 {
0548 addr->v6.sin6_family = AF_INET6;
0549 addr->v6.sin6_port = 0;
0550 addr->v6.sin6_addr = sk->sk_v6_rcv_saddr;
0551 }
0552
0553
0554 static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
0555 {
0556 if (addr->sa.sa_family == AF_INET) {
0557 sk->sk_v6_rcv_saddr.s6_addr32[0] = 0;
0558 sk->sk_v6_rcv_saddr.s6_addr32[1] = 0;
0559 sk->sk_v6_rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
0560 sk->sk_v6_rcv_saddr.s6_addr32[3] =
0561 addr->v4.sin_addr.s_addr;
0562 } else {
0563 sk->sk_v6_rcv_saddr = addr->v6.sin6_addr;
0564 }
0565 }
0566
0567
0568 static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
0569 {
0570 if (addr->sa.sa_family == AF_INET) {
0571 sk->sk_v6_daddr.s6_addr32[0] = 0;
0572 sk->sk_v6_daddr.s6_addr32[1] = 0;
0573 sk->sk_v6_daddr.s6_addr32[2] = htonl(0x0000ffff);
0574 sk->sk_v6_daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
0575 } else {
0576 sk->sk_v6_daddr = addr->v6.sin6_addr;
0577 }
0578 }
0579
0580
0581 static bool sctp_v6_from_addr_param(union sctp_addr *addr,
0582 union sctp_addr_param *param,
0583 __be16 port, int iif)
0584 {
0585 if (ntohs(param->v6.param_hdr.length) < sizeof(struct sctp_ipv6addr_param))
0586 return false;
0587
0588 addr->v6.sin6_family = AF_INET6;
0589 addr->v6.sin6_port = port;
0590 addr->v6.sin6_flowinfo = 0;
0591 addr->v6.sin6_addr = param->v6.addr;
0592 addr->v6.sin6_scope_id = iif;
0593
0594 return true;
0595 }
0596
0597
0598
0599
0600 static int sctp_v6_to_addr_param(const union sctp_addr *addr,
0601 union sctp_addr_param *param)
0602 {
0603 int length = sizeof(struct sctp_ipv6addr_param);
0604
0605 param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
0606 param->v6.param_hdr.length = htons(length);
0607 param->v6.addr = addr->v6.sin6_addr;
0608
0609 return length;
0610 }
0611
0612
0613 static void sctp_v6_to_addr(union sctp_addr *addr, struct in6_addr *saddr,
0614 __be16 port)
0615 {
0616 addr->sa.sa_family = AF_INET6;
0617 addr->v6.sin6_port = port;
0618 addr->v6.sin6_flowinfo = 0;
0619 addr->v6.sin6_addr = *saddr;
0620 addr->v6.sin6_scope_id = 0;
0621 }
0622
0623 static int __sctp_v6_cmp_addr(const union sctp_addr *addr1,
0624 const union sctp_addr *addr2)
0625 {
0626 if (addr1->sa.sa_family != addr2->sa.sa_family) {
0627 if (addr1->sa.sa_family == AF_INET &&
0628 addr2->sa.sa_family == AF_INET6 &&
0629 ipv6_addr_v4mapped(&addr2->v6.sin6_addr) &&
0630 addr2->v6.sin6_addr.s6_addr32[3] ==
0631 addr1->v4.sin_addr.s_addr)
0632 return 1;
0633
0634 if (addr2->sa.sa_family == AF_INET &&
0635 addr1->sa.sa_family == AF_INET6 &&
0636 ipv6_addr_v4mapped(&addr1->v6.sin6_addr) &&
0637 addr1->v6.sin6_addr.s6_addr32[3] ==
0638 addr2->v4.sin_addr.s_addr)
0639 return 1;
0640
0641 return 0;
0642 }
0643
0644 if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
0645 return 0;
0646
0647
0648 if ((ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) &&
0649 addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
0650 addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)
0651 return 0;
0652
0653 return 1;
0654 }
0655
0656
0657
0658
0659 static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
0660 const union sctp_addr *addr2)
0661 {
0662 return __sctp_v6_cmp_addr(addr1, addr2) &&
0663 addr1->v6.sin6_port == addr2->v6.sin6_port;
0664 }
0665
0666
0667 static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
0668 {
0669 memset(addr, 0x00, sizeof(union sctp_addr));
0670 addr->v6.sin6_family = AF_INET6;
0671 addr->v6.sin6_port = port;
0672 }
0673
0674
0675 static int sctp_v6_is_any(const union sctp_addr *addr)
0676 {
0677 return ipv6_addr_any(&addr->v6.sin6_addr);
0678 }
0679
0680
0681 static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
0682 {
0683 int type;
0684 struct net *net = sock_net(&sp->inet.sk);
0685 const struct in6_addr *in6 = (const struct in6_addr *)&addr->v6.sin6_addr;
0686
0687 type = ipv6_addr_type(in6);
0688 if (IPV6_ADDR_ANY == type)
0689 return 1;
0690 if (type == IPV6_ADDR_MAPPED) {
0691 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
0692 return 0;
0693 sctp_v6_map_v4(addr);
0694 return sctp_get_af_specific(AF_INET)->available(addr, sp);
0695 }
0696 if (!(type & IPV6_ADDR_UNICAST))
0697 return 0;
0698
0699 return ipv6_can_nonlocal_bind(net, &sp->inet) ||
0700 ipv6_chk_addr(net, in6, NULL, 0);
0701 }
0702
0703
0704
0705
0706
0707
0708
0709
0710 static int sctp_v6_addr_valid(union sctp_addr *addr,
0711 struct sctp_sock *sp,
0712 const struct sk_buff *skb)
0713 {
0714 int ret = ipv6_addr_type(&addr->v6.sin6_addr);
0715
0716
0717 if (ret == IPV6_ADDR_MAPPED) {
0718
0719
0720
0721 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
0722 return 0;
0723 sctp_v6_map_v4(addr);
0724 return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
0725 }
0726
0727
0728 if (!(ret & IPV6_ADDR_UNICAST))
0729 return 0;
0730
0731 return 1;
0732 }
0733
0734
0735 static enum sctp_scope sctp_v6_scope(union sctp_addr *addr)
0736 {
0737 enum sctp_scope retval;
0738 int v6scope;
0739
0740
0741
0742
0743
0744 v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
0745 switch (v6scope) {
0746 case IFA_HOST:
0747 retval = SCTP_SCOPE_LOOPBACK;
0748 break;
0749 case IFA_LINK:
0750 retval = SCTP_SCOPE_LINK;
0751 break;
0752 case IFA_SITE:
0753 retval = SCTP_SCOPE_PRIVATE;
0754 break;
0755 default:
0756 retval = SCTP_SCOPE_GLOBAL;
0757 break;
0758 }
0759
0760 return retval;
0761 }
0762
0763
0764 static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
0765 struct sctp_association *asoc,
0766 bool kern)
0767 {
0768 struct sock *newsk;
0769 struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
0770 struct sctp6_sock *newsctp6sk;
0771
0772 newsk = sk_alloc(sock_net(sk), PF_INET6, GFP_KERNEL, sk->sk_prot, kern);
0773 if (!newsk)
0774 goto out;
0775
0776 sock_init_data(NULL, newsk);
0777
0778 sctp_copy_sock(newsk, sk, asoc);
0779 sock_reset_flag(sk, SOCK_ZAPPED);
0780
0781 newsctp6sk = (struct sctp6_sock *)newsk;
0782 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
0783
0784 sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
0785
0786 newnp = inet6_sk(newsk);
0787
0788 memcpy(newnp, np, sizeof(struct ipv6_pinfo));
0789 newnp->ipv6_mc_list = NULL;
0790 newnp->ipv6_ac_list = NULL;
0791 newnp->ipv6_fl_list = NULL;
0792
0793 sctp_v6_copy_ip_options(sk, newsk);
0794
0795
0796
0797
0798 sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
0799
0800 newsk->sk_v6_rcv_saddr = sk->sk_v6_rcv_saddr;
0801
0802 sk_refcnt_debug_inc(newsk);
0803
0804 if (newsk->sk_prot->init(newsk)) {
0805 sk_common_release(newsk);
0806 newsk = NULL;
0807 }
0808
0809 out:
0810 return newsk;
0811 }
0812
0813
0814
0815
0816 static int sctp_v6_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
0817 {
0818 if (sp->v4mapped) {
0819 if (addr->sa.sa_family == AF_INET)
0820 sctp_v4_map_v6(addr);
0821 } else {
0822 if (addr->sa.sa_family == AF_INET6 &&
0823 ipv6_addr_v4mapped(&addr->v6.sin6_addr))
0824 sctp_v6_map_v4(addr);
0825 }
0826
0827 if (addr->sa.sa_family == AF_INET) {
0828 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
0829 return sizeof(struct sockaddr_in);
0830 }
0831 return sizeof(struct sockaddr_in6);
0832 }
0833
0834
0835 static int sctp_v6_skb_iif(const struct sk_buff *skb)
0836 {
0837 return IP6CB(skb)->iif;
0838 }
0839
0840
0841 static int sctp_v6_is_ce(const struct sk_buff *skb)
0842 {
0843 return *((__u32 *)(ipv6_hdr(skb))) & (__force __u32)htonl(1 << 20);
0844 }
0845
0846
0847 static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
0848 {
0849 seq_printf(seq, "%pI6 ", &addr->v6.sin6_addr);
0850 }
0851
0852 static void sctp_v6_ecn_capable(struct sock *sk)
0853 {
0854 inet6_sk(sk)->tclass |= INET_ECN_ECT_0;
0855 }
0856
0857
0858 static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
0859 char *msgname, int *addrlen)
0860 {
0861 union sctp_addr *addr;
0862 struct sctp_association *asoc;
0863 union sctp_addr *paddr;
0864
0865 if (!msgname)
0866 return;
0867
0868 addr = (union sctp_addr *)msgname;
0869 asoc = event->asoc;
0870 paddr = &asoc->peer.primary_addr;
0871
0872 if (paddr->sa.sa_family == AF_INET) {
0873 addr->v4.sin_family = AF_INET;
0874 addr->v4.sin_port = htons(asoc->peer.port);
0875 addr->v4.sin_addr = paddr->v4.sin_addr;
0876 } else {
0877 addr->v6.sin6_family = AF_INET6;
0878 addr->v6.sin6_flowinfo = 0;
0879 if (ipv6_addr_type(&paddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
0880 addr->v6.sin6_scope_id = paddr->v6.sin6_scope_id;
0881 else
0882 addr->v6.sin6_scope_id = 0;
0883 addr->v6.sin6_port = htons(asoc->peer.port);
0884 addr->v6.sin6_addr = paddr->v6.sin6_addr;
0885 }
0886
0887 *addrlen = sctp_v6_addr_to_user(sctp_sk(asoc->base.sk), addr);
0888 }
0889
0890
0891 static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
0892 int *addr_len)
0893 {
0894 union sctp_addr *addr;
0895 struct sctphdr *sh;
0896
0897 if (!msgname)
0898 return;
0899
0900 addr = (union sctp_addr *)msgname;
0901 sh = sctp_hdr(skb);
0902
0903 if (ip_hdr(skb)->version == 4) {
0904 addr->v4.sin_family = AF_INET;
0905 addr->v4.sin_port = sh->source;
0906 addr->v4.sin_addr.s_addr = ip_hdr(skb)->saddr;
0907 } else {
0908 addr->v6.sin6_family = AF_INET6;
0909 addr->v6.sin6_flowinfo = 0;
0910 addr->v6.sin6_port = sh->source;
0911 addr->v6.sin6_addr = ipv6_hdr(skb)->saddr;
0912 if (ipv6_addr_type(&addr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
0913 addr->v6.sin6_scope_id = sctp_v6_skb_iif(skb);
0914 else
0915 addr->v6.sin6_scope_id = 0;
0916 }
0917
0918 *addr_len = sctp_v6_addr_to_user(sctp_sk(skb->sk), addr);
0919 }
0920
0921
0922 static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
0923 {
0924 switch (family) {
0925 case AF_INET6:
0926 return 1;
0927
0928 case AF_INET:
0929 if (!ipv6_only_sock(sctp_opt2sk(sp)))
0930 return 1;
0931 fallthrough;
0932 default:
0933 return 0;
0934 }
0935 }
0936
0937
0938
0939
0940
0941 static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
0942 const union sctp_addr *addr2,
0943 struct sctp_sock *opt)
0944 {
0945 struct sock *sk = sctp_opt2sk(opt);
0946 struct sctp_af *af1, *af2;
0947
0948 af1 = sctp_get_af_specific(addr1->sa.sa_family);
0949 af2 = sctp_get_af_specific(addr2->sa.sa_family);
0950
0951 if (!af1 || !af2)
0952 return 0;
0953
0954
0955 if (ipv6_only_sock(sk) && af1 != af2)
0956 return 0;
0957
0958
0959 if (sctp_is_any(sk, addr1) || sctp_is_any(sk, addr2))
0960 return 1;
0961
0962 if (addr1->sa.sa_family == AF_INET && addr2->sa.sa_family == AF_INET)
0963 return addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr;
0964
0965 return __sctp_v6_cmp_addr(addr1, addr2);
0966 }
0967
0968
0969
0970
0971 static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
0972 {
0973 struct sctp_af *af;
0974
0975
0976 if (addr->sa.sa_family != AF_INET6)
0977 af = sctp_get_af_specific(addr->sa.sa_family);
0978 else {
0979 int type = ipv6_addr_type(&addr->v6.sin6_addr);
0980 struct net_device *dev;
0981
0982 if (type & IPV6_ADDR_LINKLOCAL) {
0983 struct net *net;
0984 if (!addr->v6.sin6_scope_id)
0985 return 0;
0986 net = sock_net(&opt->inet.sk);
0987 rcu_read_lock();
0988 dev = dev_get_by_index_rcu(net, addr->v6.sin6_scope_id);
0989 if (!dev || !(ipv6_can_nonlocal_bind(net, &opt->inet) ||
0990 ipv6_chk_addr(net, &addr->v6.sin6_addr,
0991 dev, 0))) {
0992 rcu_read_unlock();
0993 return 0;
0994 }
0995 rcu_read_unlock();
0996 }
0997
0998 af = opt->pf->af;
0999 }
1000 return af->available(addr, opt);
1001 }
1002
1003
1004
1005
1006 static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
1007 {
1008 struct sctp_af *af = NULL;
1009
1010
1011 if (addr->sa.sa_family != AF_INET6)
1012 af = sctp_get_af_specific(addr->sa.sa_family);
1013 else {
1014 int type = ipv6_addr_type(&addr->v6.sin6_addr);
1015 struct net_device *dev;
1016
1017 if (type & IPV6_ADDR_LINKLOCAL) {
1018 if (!addr->v6.sin6_scope_id)
1019 return 0;
1020 rcu_read_lock();
1021 dev = dev_get_by_index_rcu(sock_net(&opt->inet.sk),
1022 addr->v6.sin6_scope_id);
1023 rcu_read_unlock();
1024 if (!dev)
1025 return 0;
1026 }
1027 af = opt->pf->af;
1028 }
1029
1030 return af != NULL;
1031 }
1032
1033
1034
1035
1036
1037
1038
1039 static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
1040 __be16 *types)
1041 {
1042 types[0] = SCTP_PARAM_IPV6_ADDRESS;
1043 if (!opt || !ipv6_only_sock(sctp_opt2sk(opt))) {
1044 types[1] = SCTP_PARAM_IPV4_ADDRESS;
1045 return 2;
1046 }
1047 return 1;
1048 }
1049
1050
1051 static int sctp_getname(struct socket *sock, struct sockaddr *uaddr,
1052 int peer)
1053 {
1054 int rc;
1055
1056 rc = inet6_getname(sock, uaddr, peer);
1057
1058 if (rc < 0)
1059 return rc;
1060
1061 rc = sctp_v6_addr_to_user(sctp_sk(sock->sk),
1062 (union sctp_addr *)uaddr);
1063
1064 return rc;
1065 }
1066
1067 static const struct proto_ops inet6_seqpacket_ops = {
1068 .family = PF_INET6,
1069 .owner = THIS_MODULE,
1070 .release = inet6_release,
1071 .bind = inet6_bind,
1072 .connect = sctp_inet_connect,
1073 .socketpair = sock_no_socketpair,
1074 .accept = inet_accept,
1075 .getname = sctp_getname,
1076 .poll = sctp_poll,
1077 .ioctl = inet6_ioctl,
1078 .gettstamp = sock_gettstamp,
1079 .listen = sctp_inet_listen,
1080 .shutdown = inet_shutdown,
1081 .setsockopt = sock_common_setsockopt,
1082 .getsockopt = sock_common_getsockopt,
1083 .sendmsg = inet_sendmsg,
1084 .recvmsg = inet_recvmsg,
1085 .mmap = sock_no_mmap,
1086 #ifdef CONFIG_COMPAT
1087 .compat_ioctl = inet6_compat_ioctl,
1088 #endif
1089 };
1090
1091 static struct inet_protosw sctpv6_seqpacket_protosw = {
1092 .type = SOCK_SEQPACKET,
1093 .protocol = IPPROTO_SCTP,
1094 .prot = &sctpv6_prot,
1095 .ops = &inet6_seqpacket_ops,
1096 .flags = SCTP_PROTOSW_FLAG
1097 };
1098 static struct inet_protosw sctpv6_stream_protosw = {
1099 .type = SOCK_STREAM,
1100 .protocol = IPPROTO_SCTP,
1101 .prot = &sctpv6_prot,
1102 .ops = &inet6_seqpacket_ops,
1103 .flags = SCTP_PROTOSW_FLAG,
1104 };
1105
1106 static int sctp6_rcv(struct sk_buff *skb)
1107 {
1108 SCTP_INPUT_CB(skb)->encap_port = 0;
1109 return sctp_rcv(skb) ? -1 : 0;
1110 }
1111
1112 static const struct inet6_protocol sctpv6_protocol = {
1113 .handler = sctp6_rcv,
1114 .err_handler = sctp_v6_err,
1115 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
1116 };
1117
1118 static struct sctp_af sctp_af_inet6 = {
1119 .sa_family = AF_INET6,
1120 .sctp_xmit = sctp_v6_xmit,
1121 .setsockopt = ipv6_setsockopt,
1122 .getsockopt = ipv6_getsockopt,
1123 .get_dst = sctp_v6_get_dst,
1124 .get_saddr = sctp_v6_get_saddr,
1125 .copy_addrlist = sctp_v6_copy_addrlist,
1126 .from_skb = sctp_v6_from_skb,
1127 .from_sk = sctp_v6_from_sk,
1128 .from_addr_param = sctp_v6_from_addr_param,
1129 .to_addr_param = sctp_v6_to_addr_param,
1130 .cmp_addr = sctp_v6_cmp_addr,
1131 .scope = sctp_v6_scope,
1132 .addr_valid = sctp_v6_addr_valid,
1133 .inaddr_any = sctp_v6_inaddr_any,
1134 .is_any = sctp_v6_is_any,
1135 .available = sctp_v6_available,
1136 .skb_iif = sctp_v6_skb_iif,
1137 .is_ce = sctp_v6_is_ce,
1138 .seq_dump_addr = sctp_v6_seq_dump_addr,
1139 .ecn_capable = sctp_v6_ecn_capable,
1140 .net_header_len = sizeof(struct ipv6hdr),
1141 .sockaddr_len = sizeof(struct sockaddr_in6),
1142 .ip_options_len = sctp_v6_ip_options_len,
1143 };
1144
1145 static struct sctp_pf sctp_pf_inet6 = {
1146 .event_msgname = sctp_inet6_event_msgname,
1147 .skb_msgname = sctp_inet6_skb_msgname,
1148 .af_supported = sctp_inet6_af_supported,
1149 .cmp_addr = sctp_inet6_cmp_addr,
1150 .bind_verify = sctp_inet6_bind_verify,
1151 .send_verify = sctp_inet6_send_verify,
1152 .supported_addrs = sctp_inet6_supported_addrs,
1153 .create_accept_sk = sctp_v6_create_accept_sk,
1154 .addr_to_user = sctp_v6_addr_to_user,
1155 .to_sk_saddr = sctp_v6_to_sk_saddr,
1156 .to_sk_daddr = sctp_v6_to_sk_daddr,
1157 .copy_ip_options = sctp_v6_copy_ip_options,
1158 .af = &sctp_af_inet6,
1159 };
1160
1161
1162 void sctp_v6_pf_init(void)
1163 {
1164
1165 sctp_register_pf(&sctp_pf_inet6, PF_INET6);
1166
1167
1168 sctp_register_af(&sctp_af_inet6);
1169 }
1170
1171 void sctp_v6_pf_exit(void)
1172 {
1173 list_del(&sctp_af_inet6.list);
1174 }
1175
1176
1177 int sctp_v6_protosw_init(void)
1178 {
1179 int rc;
1180
1181 rc = proto_register(&sctpv6_prot, 1);
1182 if (rc)
1183 return rc;
1184
1185
1186 inet6_register_protosw(&sctpv6_seqpacket_protosw);
1187 inet6_register_protosw(&sctpv6_stream_protosw);
1188
1189 return 0;
1190 }
1191
1192 void sctp_v6_protosw_exit(void)
1193 {
1194 inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
1195 inet6_unregister_protosw(&sctpv6_stream_protosw);
1196 proto_unregister(&sctpv6_prot);
1197 }
1198
1199
1200
1201 int sctp_v6_add_protocol(void)
1202 {
1203
1204 register_inet6addr_notifier(&sctp_inet6addr_notifier);
1205
1206 if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
1207 return -EAGAIN;
1208
1209 return 0;
1210 }
1211
1212
1213 void sctp_v6_del_protocol(void)
1214 {
1215 inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
1216 unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
1217 }