49 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
51 #include <linux/module.h>
53 #include <linux/netdevice.h>
59 #include <linux/slab.h>
76 static struct sctp_pf *sctp_pf_inet6_specific;
77 static struct sctp_pf *sctp_pf_inet_specific;
78 static struct sctp_af *sctp_af_v4_specific;
79 static struct sctp_af *sctp_af_v6_specific;
93 if (!net->sctp.proc_net_sctp)
94 goto out_proc_net_sctp;
96 goto out_snmp_proc_init;
98 goto out_eps_proc_init;
100 goto out_assocs_proc_init;
102 goto out_remaddr_proc_init;
106 out_remaddr_proc_init:
108 out_assocs_proc_init:
114 net->sctp.proc_net_sctp =
NULL;
125 static void sctp_proc_exit(
struct net *
net)
127 #ifdef CONFIG_PROC_FS
134 net->sctp.proc_net_sctp =
NULL;
141 static void sctp_v4_copy_addrlist(
struct list_head *addrlist,
144 struct in_device *in_dev;
145 struct in_ifaddr *ifa;
149 if ((in_dev = __in_dev_get_rcu(dev)) ==
NULL) {
154 for (ifa = in_dev->ifa_list; ifa; ifa = ifa->ifa_next) {
159 addr->
a.
v4.sin_port = 0;
160 addr->
a.
v4.sin_addr.s_addr = ifa->ifa_local;
162 INIT_LIST_HEAD(&addr->
list);
173 static void sctp_get_local_addr_list(
struct net *
net)
190 static void sctp_free_local_addr_list(
struct net *net)
210 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list,
list) {
244 port = &addr->
v4.sin_port;
250 from = &ip_hdr(skb)->saddr;
253 from = &ip_hdr(skb)->daddr;
262 addr->
v4.sin_port = 0;
263 addr->
v4.sin_addr.s_addr = inet_sk(sk)->inet_rcv_saddr;
267 static void sctp_v4_to_sk_saddr(
union sctp_addr *addr,
struct sock *
sk)
269 inet_sk(sk)->inet_rcv_saddr = addr->
v4.sin_addr.s_addr;
273 static void sctp_v4_to_sk_daddr(
union sctp_addr *addr,
struct sock *
sk)
275 inet_sk(sk)->inet_daddr = addr->
v4.sin_addr.s_addr;
279 static void sctp_v4_from_addr_param(
union sctp_addr *addr,
285 addr->
v4.sin_addr.s_addr = param->
v4.addr.s_addr;
291 static int sctp_v4_to_addr_param(
const union sctp_addr *addr,
297 param->
v4.param_hdr.length =
htons(length);
298 param->
v4.addr.s_addr = addr->
v4.sin_addr.s_addr;
308 saddr->
v4.sin_port =
port;
309 saddr->
v4.sin_addr.s_addr = fl4->
saddr;
316 if (addr1->
sa.sa_family != addr2->
sa.sa_family)
318 if (addr1->
v4.sin_port != addr2->
v4.sin_port)
320 if (addr1->
v4.sin_addr.s_addr != addr2->
v4.sin_addr.s_addr)
335 static int sctp_v4_is_any(
const union sctp_addr *addr)
347 static int sctp_v4_addr_valid(
union sctp_addr *addr,
369 struct net *net = sock_net(&sp->
inet.sk);
375 !sp->
inet.freebind &&
376 !sysctl_ip_nonlocal_bind)
409 }
else if (ipv4_is_loopback(addr->
v4.sin_addr.s_addr)) {
411 }
else if (ipv4_is_linklocal_169(addr->
v4.sin_addr.s_addr)) {
413 }
else if (ipv4_is_private_10(addr->
v4.sin_addr.s_addr) ||
414 ipv4_is_private_172(addr->
v4.sin_addr.s_addr) ||
415 ipv4_is_private_192(addr->
v4.sin_addr.s_addr)) {
441 fl4->
daddr = daddr->
v4.sin_addr.s_addr;
442 fl4->fl4_dport = daddr->
v4.sin_port;
446 fl4->flowi4_oif = asoc->
base.sk->sk_bound_dev_if;
447 fl4->fl4_sport =
htons(asoc->
base.bind_addr.port);
450 fl4->
saddr = saddr->
v4.sin_addr.s_addr;
451 fl4->fl4_sport = saddr->
v4.sin_port;
457 rt = ip_route_output_key(sock_net(sk), fl4);
467 bp = &asoc->
base.bind_addr;
473 sctp_v4_dst_saddr(&dst_saddr, fl4,
htons(bp->
port));
480 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->
a))
501 fl4->
saddr = laddr->
a.
v4.sin_addr.s_addr;
502 fl4->fl4_sport = laddr->
a.
v4.sin_port;
503 rt = ip_route_output_key(sock_net(sk), fl4);
525 static void sctp_v4_get_saddr(
struct sctp_sock *sk,
534 saddr->
v4.sin_addr.s_addr = fl->
u.
ip4.saddr;
539 static int sctp_v4_skb_iif(
const struct sk_buff *skb)
541 return inet_iif(skb);
545 static int sctp_v4_is_ce(
const struct sk_buff *skb)
547 return INET_ECN_is_ce(ip_hdr(skb)->tos);
551 static struct sock *sctp_v4_create_accept_sk(
struct sock *sk,
566 newinet = inet_sk(newsk);
568 newinet->inet_daddr = asoc->
peer.primary_addr.v4.sin_addr.s_addr;
572 if (newsk->sk_prot->init(newsk)) {
593 static void sctp_v4_ecn_capable(
struct sock *sk)
600 struct net *net = (
struct net *)arg;
604 spin_lock_bh(&net->sctp.addr_wq_lock);
608 " for cmd %d at entry %p\n", &net->sctp.addr_waitq, &addrw->
a, addrw->
state,
611 #if IS_ENABLED(CONFIG_IPV6)
617 if (ipv6_addr_type(&addrw->
a.
v6.sin6_addr) &
624 unsigned long timeo_val;
630 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
638 sk = sctp_opt2sk(sp);
647 #if IS_ENABLED(CONFIG_IPV6)
653 spin_unlock_bh(&net->sctp.addr_wq_lock);
656 static void sctp_free_addr_wq(
struct net *net)
661 spin_lock_bh(&net->sctp.addr_wq_lock);
667 spin_unlock_bh(&net->sctp.addr_wq_lock);
679 if (addrw->
a.
sa.sa_family != addr->
a.
sa.sa_family)
682 if (addrw->
a.
v4.sin_addr.s_addr ==
683 addr->
a.
v4.sin_addr.s_addr)
686 if (ipv6_addr_equal(&addrw->
a.
v6.sin6_addr,
687 &addr->
a.
v6.sin6_addr))
697 unsigned long timeo_val;
705 spin_lock_bh(&net->sctp.addr_wq_lock);
707 addrw = sctp_addr_wq_lookup(net, addr);
709 if (addrw->
state != cmd) {
711 " in wq %p\n", addrw->
state, &addrw->
a,
712 &net->sctp.addr_waitq);
716 spin_unlock_bh(&net->sctp.addr_wq_lock);
723 spin_unlock_bh(&net->sctp.addr_wq_lock);
729 " in wq %p\n", addrw->
state, &addrw->
a, &net->sctp.addr_waitq);
731 if (!timer_pending(&net->sctp.addr_wq_timer)) {
734 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
736 spin_unlock_bh(&net->sctp.addr_wq_lock);
745 static int sctp_inetaddr_event(
struct notifier_block *
this,
unsigned long ev,
748 struct in_ifaddr *ifa = (
struct in_ifaddr *)ptr;
751 struct net *net = dev_net(ifa->ifa_dev->dev);
759 addr->
a.
v4.sin_port = 0;
760 addr->
a.
v4.sin_addr.s_addr = ifa->ifa_local;
762 spin_lock_bh(&net->sctp.local_addr_lock);
763 list_add_tail_rcu(&addr->
list, &net->sctp.local_addr_list);
765 spin_unlock_bh(&net->sctp.local_addr_lock);
769 spin_lock_bh(&net->sctp.local_addr_lock);
771 &net->sctp.local_addr_list,
list) {
773 addr->
a.
v4.sin_addr.s_addr ==
778 list_del_rcu(&addr->
list);
782 spin_unlock_bh(&net->sctp.local_addr_lock);
795 static int sctp_ctl_sock_init(
struct net *net)
813 pr_err(
"Failed to create the SCTP control socket\n");
824 if (sctp_af_v4_specific)
826 sctp_af_v4_specific = af;
829 if (sctp_af_v6_specific)
831 sctp_af_v6_specific = af;
837 INIT_LIST_HEAD(&af->
list);
849 return sctp_af_v4_specific;
851 return sctp_af_v6_specific;
858 static void sctp_inet_msgname(
char *msgname,
int *addr_len)
865 memset(sin->sin_zero, 0,
sizeof(sin->sin_zero));
878 sctp_inet_msgname(msgname, addr_len);
880 sinfrom = &asoc->
peer.primary_addr.v4;
887 static void sctp_inet_skb_msgname(
struct sk_buff *skb,
char *msgname,
int *len)
890 struct sctphdr *sh = sctp_hdr(skb);
893 sctp_inet_msgname(msgname, len);
895 sin->
sin_addr.s_addr = ip_hdr(skb)->saddr;
907 static int sctp_inet_cmp_addr(
const union sctp_addr *addr1,
912 if (addr1->
sa.sa_family != addr2->
sa.sa_family)
917 if (addr1->
v4.sin_addr.s_addr == addr2->
v4.sin_addr.s_addr)
928 return sctp_v4_available(addr, opt);
942 static int sctp_inet_supported_addrs(
const struct sctp_sock *opt,
950 static inline int sctp_v4_xmit(
struct sk_buff *skb,
956 __func__, skb, skb->
len,
957 &transport->
fl.u.ip4.saddr,
958 &transport->
fl.u.ip4.daddr);
967 static struct sctp_af sctp_af_inet;
969 static struct sctp_pf sctp_pf_inet = {
970 .event_msgname = sctp_inet_event_msgname,
971 .skb_msgname = sctp_inet_skb_msgname,
972 .af_supported = sctp_inet_af_supported,
973 .cmp_addr = sctp_inet_cmp_addr,
974 .bind_verify = sctp_inet_bind_verify,
975 .send_verify = sctp_inet_send_verify,
976 .supported_addrs = sctp_inet_supported_addrs,
977 .create_accept_sk = sctp_v4_create_accept_sk,
978 .addr_v4map = sctp_v4_addr_v4map,
984 .notifier_call = sctp_inetaddr_event,
988 static const struct proto_ops inet_seqpacket_ops = {
1007 #ifdef CONFIG_COMPAT
1018 .ops = &inet_seqpacket_ops,
1026 .ops = &inet_seqpacket_ops,
1040 static struct sctp_af sctp_af_inet = {
1042 .sctp_xmit = sctp_v4_xmit,
1045 .get_dst = sctp_v4_get_dst,
1046 .get_saddr = sctp_v4_get_saddr,
1047 .copy_addrlist = sctp_v4_copy_addrlist,
1048 .from_skb = sctp_v4_from_skb,
1049 .from_sk = sctp_v4_from_sk,
1050 .to_sk_saddr = sctp_v4_to_sk_saddr,
1051 .to_sk_daddr = sctp_v4_to_sk_daddr,
1052 .from_addr_param = sctp_v4_from_addr_param,
1053 .to_addr_param = sctp_v4_to_addr_param,
1054 .cmp_addr = sctp_v4_cmp_addr,
1055 .addr_valid = sctp_v4_addr_valid,
1056 .inaddr_any = sctp_v4_inaddr_any,
1057 .is_any = sctp_v4_is_any,
1058 .available = sctp_v4_available,
1059 .scope = sctp_v4_scope,
1060 .skb_iif = sctp_v4_skb_iif,
1061 .is_ce = sctp_v4_is_ce,
1062 .seq_dump_addr = sctp_v4_seq_dump_addr,
1063 .ecn_capable = sctp_v4_ecn_capable,
1064 .net_header_len =
sizeof(
struct iphdr),
1066 #ifdef CONFIG_COMPAT
1076 return sctp_pf_inet_specific;
1078 return sctp_pf_inet6_specific;
1089 if (sctp_pf_inet_specific)
1091 sctp_pf_inet_specific =
pf;
1094 if (sctp_pf_inet6_specific)
1096 sctp_pf_inet6_specific =
pf;
1104 static inline int init_sctp_mibs(
struct net *net)
1111 static inline void cleanup_sctp_mibs(
struct net *net)
1116 static void sctp_v4_pf_init(
void)
1123 static void sctp_v4_pf_exit(
void)
1128 static int sctp_v4_protosw_init(
void)
1143 static void sctp_v4_protosw_exit(
void)
1150 static int sctp_v4_add_protocol(
void)
1162 static void sctp_v4_del_protocol(
void)
1168 static int sctp_net_init(
struct net *net)
1191 net->sctp.cookie_preserve_enable = 1;
1200 net->sctp.max_retrans_association = 10;
1201 net->sctp.max_retrans_path = 5;
1202 net->sctp.max_retrans_init = 8;
1205 net->sctp.sndbuf_policy = 0;
1208 net->sctp.rcvbuf_policy = 0;
1217 net->sctp.addip_enable = 0;
1218 net->sctp.addip_noauth = 0;
1219 net->sctp.default_auto_asconf = 0;
1222 net->sctp.prsctp_enable = 1;
1225 net->sctp.auth_enable = 0;
1238 goto err_sysctl_register;
1241 status = init_sctp_mibs(net);
1246 status = sctp_proc_init(net);
1253 if ((status = sctp_ctl_sock_init(net))) {
1254 pr_err(
"Failed to initialize the SCTP control sock\n");
1255 goto err_ctl_sock_init;
1259 INIT_LIST_HEAD(&net->sctp.local_addr_list);
1261 sctp_get_local_addr_list(net);
1264 INIT_LIST_HEAD(&net->sctp.addr_waitq);
1265 INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
1267 net->sctp.addr_wq_timer.expires = 0;
1269 (
unsigned long)net);
1275 sctp_proc_exit(net);
1277 cleanup_sctp_mibs(net);
1280 err_sysctl_register:
1284 static void sctp_net_exit(
struct net *net)
1287 sctp_free_addr_wq(net);
1288 sctp_free_local_addr_list(net);
1291 inet_ctl_sock_destroy(net->sctp.ctl_sock);
1295 sctp_proc_exit(net);
1296 cleanup_sctp_mibs(net);
1301 .init = sctp_net_init,
1302 .exit = sctp_net_exit,
1311 unsigned long limit;
1316 if (!sctp_sanity_check())
1332 if (!sctp_chunk_cachep)
1333 goto err_chunk_cachep;
1337 goto err_percpu_counter_init;
1349 limit =
max(limit, 128
UL);
1356 max_share =
min(4
UL*1024*1024, limit);
1369 if (totalram_pages >= (128 * 1024))
1374 for (order = 0; (1
UL <<
order) < goal; order++)
1386 pr_err(
"Failed association hash alloc\n");
1388 goto err_ahash_alloc;
1400 pr_err(
"Failed endpoint_hash alloc\n");
1402 goto err_ehash_alloc;
1419 pr_err(
"Failed bind hash alloc\n");
1421 goto err_bhash_alloc;
1428 pr_info(
"Hash tables configured (established %d bind %d)\n",
1429 sctp_assoc_hashsize, sctp_port_hashsize);
1437 status = sctp_v4_protosw_init();
1440 goto err_protosw_init;
1444 goto err_v6_protosw_init;
1448 goto err_register_pernet_subsys;
1450 status = sctp_v4_add_protocol();
1452 goto err_add_protocol;
1457 goto err_v6_add_protocol;
1462 err_v6_add_protocol:
1463 sctp_v4_del_protocol();
1466 err_register_pernet_subsys:
1468 err_v6_protosw_init:
1469 sctp_v4_protosw_exit();
1485 err_percpu_counter_init:
1501 sctp_v4_del_protocol();
1507 sctp_v4_protosw_exit();
1542 MODULE_PARM_DESC(no_checksums,
"Disable checksums computing and verification");