50 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
52 #include <linux/module.h>
53 #include <linux/errno.h>
54 #include <linux/types.h>
55 #include <linux/socket.h>
57 #include <linux/net.h>
59 #include <linux/in6.h>
60 #include <linux/netdevice.h>
63 #include <linux/slab.h>
65 #include <linux/ipv6.h>
66 #include <linux/icmpv6.h>
67 #include <linux/random.h>
81 #include <asm/uaccess.h>
83 static inline int sctp_v6_addr_match_len(
union sctp_addr *
s1,
96 static int sctp_inet6addr_event(
struct notifier_block *
this,
unsigned long ev,
110 addr->
a.
v6.sin6_port = 0;
111 addr->
a.
v6.sin6_addr = ifa->
addr;
112 addr->
a.
v6.sin6_scope_id = ifa->
idev->dev->ifindex;
114 spin_lock_bh(&net->sctp.local_addr_lock);
115 list_add_tail_rcu(&addr->
list, &net->sctp.local_addr_list);
117 spin_unlock_bh(&net->sctp.local_addr_lock);
121 spin_lock_bh(&net->sctp.local_addr_lock);
123 &net->sctp.local_addr_list,
list) {
125 ipv6_addr_equal(&addr->
a.
v6.sin6_addr,
130 list_del_rcu(&addr->
list);
134 spin_unlock_bh(&net->sctp.local_addr_lock);
144 .notifier_call = sctp_inet6addr_event,
158 struct net *net = dev_net(skb->
dev);
160 idev = in6_dev_get(skb->
dev);
165 skb_reset_network_header(skb);
166 skb_set_transport_header(skb, offset);
220 memset(&fl6, 0,
sizeof(fl6));
227 fl6.daddr = transport->
ipaddr.
v6.sin6_addr;
228 fl6.saddr = transport->
saddr.
v6.sin6_addr;
233 fl6.flowi6_oif = transport->
saddr.
v6.sin6_scope_id;
235 fl6.flowi6_oif = sk->sk_bound_dev_if;
237 if (np->
opt && np->
opt->srcrt) {
239 fl6.daddr = *rt0->
addr;
243 __func__, skb, skb->
len,
244 &fl6.saddr, &fl6.daddr);
273 fl6->
daddr = daddr->
v6.sin6_addr;
274 fl6->fl6_dport = daddr->
v6.sin6_port;
277 fl6->flowi6_oif = daddr->
v6.sin6_scope_id;
282 fl6->fl6_sport =
htons(asoc->
base.bind_addr.port);
285 fl6->
saddr = saddr->
v6.sin6_addr;
286 fl6->fl6_sport = saddr->
v6.sin6_port;
294 bp = &asoc->
base.bind_addr;
311 (sctp_v6_cmp_addr(&dst_saddr, &laddr->
a))) {
333 bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->
a);
334 if (!baddr || (matchlen < bmatchlen)) {
336 matchlen = bmatchlen;
342 fl6->
saddr = baddr->
v6.sin6_addr;
343 fl6->fl6_sport = baddr->
v6.sin6_port;
353 &rt->rt6i_dst.addr, &fl6->
saddr);
363 static inline int sctp_v6_addr_match_len(
union sctp_addr *
s1,
366 return ipv6_addr_diff(&s1->
v6.sin6_addr, &s2->
v6.sin6_addr);
372 static void sctp_v6_get_saddr(
struct sctp_sock *sk,
383 saddr->
v6.sin6_addr = fl6->
saddr;
388 static void sctp_v6_copy_addrlist(
struct list_head *addrlist,
396 if ((in6_dev = __in6_dev_get(dev)) ==
NULL) {
407 addr->
a.
v6.sin6_port = 0;
408 addr->
a.
v6.sin6_addr = ifp->
addr;
411 INIT_LIST_HEAD(&addr->
list);
427 port = &addr->
v6.sin6_port;
429 addr->
v6.sin6_flowinfo = 0;
435 addr->
v6.sin6_addr = ipv6_hdr(skb)->saddr;
438 addr->
v6.sin6_addr = ipv6_hdr(skb)->daddr;
443 static void sctp_v6_from_sk(
union sctp_addr *addr,
struct sock *sk)
446 addr->
v6.sin6_port = 0;
447 addr->
v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
451 static void sctp_v6_to_sk_saddr(
union sctp_addr *addr,
struct sock *sk)
453 if (addr->
sa.sa_family ==
AF_INET && sctp_sk(sk)->v4mapped) {
454 inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
455 inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
456 inet6_sk(sk)->rcv_saddr.s6_addr32[2] =
htonl(0x0000ffff);
457 inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
458 addr->
v4.sin_addr.s_addr;
460 inet6_sk(sk)->rcv_saddr = addr->
v6.sin6_addr;
465 static void sctp_v6_to_sk_daddr(
union sctp_addr *addr,
struct sock *sk)
467 if (addr->
sa.sa_family ==
AF_INET && sctp_sk(sk)->v4mapped) {
468 inet6_sk(sk)->daddr.s6_addr32[0] = 0;
469 inet6_sk(sk)->daddr.s6_addr32[1] = 0;
470 inet6_sk(sk)->daddr.s6_addr32[2] =
htonl(0x0000ffff);
471 inet6_sk(sk)->daddr.s6_addr32[3] = addr->
v4.sin_addr.s_addr;
473 inet6_sk(sk)->daddr = addr->
v6.sin6_addr;
478 static void sctp_v6_from_addr_param(
union sctp_addr *addr,
483 addr->
v6.sin6_port =
port;
484 addr->
v6.sin6_flowinfo = 0;
485 addr->
v6.sin6_addr = param->
v6.addr;
486 addr->
v6.sin6_scope_id =
iif;
492 static int sctp_v6_to_addr_param(
const union sctp_addr *addr,
498 param->
v6.param_hdr.length =
htons(length);
499 param->
v6.addr = addr->
v6.sin6_addr;
509 addr->
v6.sin6_port =
port;
519 if (addr1->
sa.sa_family != addr2->
sa.sa_family) {
522 ipv6_addr_v4mapped(&addr2->
v6.sin6_addr)) {
523 if (addr2->
v6.sin6_port == addr1->
v4.sin_port &&
524 addr2->
v6.sin6_addr.s6_addr32[3] ==
525 addr1->
v4.sin_addr.s_addr)
530 ipv6_addr_v4mapped(&addr1->
v6.sin6_addr)) {
531 if (addr1->
v6.sin6_port == addr2->
v4.sin_port &&
532 addr1->
v6.sin6_addr.s6_addr32[3] ==
533 addr2->
v4.sin_addr.s_addr)
538 if (!ipv6_addr_equal(&addr1->
v6.sin6_addr, &addr2->
v6.sin6_addr))
542 if (addr1->
v6.sin6_scope_id && addr2->
v6.sin6_scope_id &&
543 (addr1->
v6.sin6_scope_id != addr2->
v6.sin6_scope_id)) {
556 addr->
v6.sin6_port =
port;
560 static int sctp_v6_is_any(
const union sctp_addr *addr)
562 return ipv6_addr_any(&addr->
v6.sin6_addr);
571 type = ipv6_addr_type(in6);
579 sctp_v6_map_v4(addr);
595 static int sctp_v6_addr_valid(
union sctp_addr *addr,
599 int ret = ipv6_addr_type(&addr->
v6.sin6_addr);
610 sctp_v6_map_v4(addr);
615 if (!(ret & IPV6_ADDR_UNICAST))
631 v6scope = ipv6_addr_scope(&addr->
v6.sin6_addr);
651 static struct sock *sctp_v6_create_accept_sk(
struct sock *sk,
656 struct sctp6_sock *newsctp6sk;
667 newsctp6sk = (
struct sctp6_sock *)newsk;
668 inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
670 sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
672 newnp = inet6_sk(newsk);
679 sctp_v6_to_sk_daddr(&asoc->
peer.primary_addr, newsk);
683 if (newsk->sk_prot->init(newsk)) {
696 sctp_v4_map_v6(addr);
700 static int sctp_v6_skb_iif(
const struct sk_buff *skb)
707 static int sctp_v6_is_ce(
const struct sk_buff *skb)
709 return *((
__u32 *)(ipv6_hdr(skb))) &
htonl(1 << 20);
718 static void sctp_v6_ecn_capable(
struct sock *sk)
724 static void sctp_inet6_msgname(
char *msgname,
int *addr_len)
737 char *msgname,
int *addrlen)
746 sctp_inet6_msgname(msgname, addrlen);
749 addr = &asoc->
peer.primary_addr;
756 if (sctp_sk(asoc->
base.sk)->v4mapped &&
760 addr->
v4.sin_addr.s_addr;
764 sin6from = &asoc->
peer.primary_addr.v6;
772 static void sctp_inet6_skb_msgname(
struct sk_buff *skb,
char *msgname,
779 sctp_inet6_msgname(msgname, addr_len);
785 if (sctp_sk(skb->
sk)->v4mapped &&
788 sin6->
sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
820 static int sctp_inet6_cmp_addr(
const union sctp_addr *addr1,
825 struct sock *sk = sctp_opt2sk(opt);
841 if (addr1->
sa.sa_family != addr2->
sa.sa_family)
858 int type = ipv6_addr_type(&addr->
v6.sin6_addr);
863 if (!addr->
v6.sin6_scope_id)
865 net = sock_net(&opt->
inet.sk);
895 int type = ipv6_addr_type(&addr->
v6.sin6_addr);
898 if (type & IPV6_ADDR_LINKLOCAL) {
899 if (!addr->
v6.sin6_scope_id)
903 addr->
v6.sin6_scope_id);
920 static int sctp_inet6_supported_addrs(
const struct sctp_sock *opt,
931 static const struct proto_ops inet6_seqpacket_ops = {
959 .ops = &inet6_seqpacket_ops,
967 .ops = &inet6_seqpacket_ops,
972 static int sctp6_rcv(
struct sk_buff *skb)
977 static const struct inet6_protocol sctpv6_protocol = {
978 .handler = sctp6_rcv,
980 .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
983 static struct sctp_af sctp_af_inet6 = {
985 .sctp_xmit = sctp_v6_xmit,
988 .get_dst = sctp_v6_get_dst,
989 .get_saddr = sctp_v6_get_saddr,
990 .copy_addrlist = sctp_v6_copy_addrlist,
991 .from_skb = sctp_v6_from_skb,
992 .from_sk = sctp_v6_from_sk,
993 .to_sk_saddr = sctp_v6_to_sk_saddr,
994 .to_sk_daddr = sctp_v6_to_sk_daddr,
995 .from_addr_param = sctp_v6_from_addr_param,
996 .to_addr_param = sctp_v6_to_addr_param,
997 .cmp_addr = sctp_v6_cmp_addr,
998 .scope = sctp_v6_scope,
999 .addr_valid = sctp_v6_addr_valid,
1000 .inaddr_any = sctp_v6_inaddr_any,
1001 .is_any = sctp_v6_is_any,
1002 .available = sctp_v6_available,
1003 .skb_iif = sctp_v6_skb_iif,
1004 .is_ce = sctp_v6_is_ce,
1005 .seq_dump_addr = sctp_v6_seq_dump_addr,
1006 .ecn_capable = sctp_v6_ecn_capable,
1007 .net_header_len =
sizeof(
struct ipv6hdr),
1009 #ifdef CONFIG_COMPAT
1015 static struct sctp_pf sctp_pf_inet6 = {
1016 .event_msgname = sctp_inet6_event_msgname,
1017 .skb_msgname = sctp_inet6_skb_msgname,
1018 .af_supported = sctp_inet6_af_supported,
1019 .cmp_addr = sctp_inet6_cmp_addr,
1020 .bind_verify = sctp_inet6_bind_verify,
1021 .send_verify = sctp_inet6_send_verify,
1022 .supported_addrs = sctp_inet6_supported_addrs,
1023 .create_accept_sk = sctp_v6_create_accept_sk,
1024 .addr_v4map = sctp_v6_addr_v4map,
1025 .af = &sctp_af_inet6,