1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* SCTP kernel implementation
3 * (C) Copyright IBM Corp. 2001, 2004
4 * Copyright (c) 1999-2000 Cisco, Inc.
5 * Copyright (c) 1999-2001 Motorola, Inc.
6 * Copyright (c) 2001 Intel Corp.
7 * Copyright (c) 2001 Nokia, Inc.
8 * Copyright (c) 2001 La Monte H.P. Yarroll
9 *
10 * This file is part of the SCTP kernel implementation
11 *
12 * Initialization/cleanup for SCTP protocol support.
13 *
14 * Please send any bug reports or fixes you make to the
15 * email address(es):
16 * lksctp developers <linux-sctp@vger.kernel.org>
17 *
18 * Written or modified by:
19 * La Monte H.P. Yarroll <piggy@acm.org>
20 * Karl Knutson <karl@athena.chicago.il.us>
21 * Jon Grimm <jgrimm@us.ibm.com>
22 * Sridhar Samudrala <sri@us.ibm.com>
23 * Daisy Chang <daisyc@us.ibm.com>
24 * Ardelle Fan <ardelle.fan@intel.com>
25 */
26
27 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
28
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/netdevice.h>
32 #include <linux/inetdevice.h>
33 #include <linux/seq_file.h>
34 #include <linux/memblock.h>
35 #include <linux/highmem.h>
36 #include <linux/slab.h>
37 #include <net/net_namespace.h>
38 #include <net/protocol.h>
39 #include <net/ip.h>
40 #include <net/ipv6.h>
41 #include <net/route.h>
42 #include <net/sctp/sctp.h>
43 #include <net/addrconf.h>
44 #include <net/inet_common.h>
45 #include <net/inet_ecn.h>
46 #include <net/inet_sock.h>
47 #include <net/udp_tunnel.h>
48 #include <net/inet_dscp.h>
49
50 #define MAX_SCTP_PORT_HASH_ENTRIES (64 * 1024)
51
52 /* Global data structures. */
53 struct sctp_globals sctp_globals __read_mostly;
54
55 struct idr sctp_assocs_id;
56 DEFINE_SPINLOCK(sctp_assocs_id_lock);
57
58 static struct sctp_pf *sctp_pf_inet6_specific;
59 static struct sctp_pf *sctp_pf_inet_specific;
60 static struct sctp_af *sctp_af_v4_specific;
61 static struct sctp_af *sctp_af_v6_specific;
62
63 struct kmem_cache *sctp_chunk_cachep __read_mostly;
64 struct kmem_cache *sctp_bucket_cachep __read_mostly;
65
66 long sysctl_sctp_mem[3];
67 int sysctl_sctp_rmem[3];
68 int sysctl_sctp_wmem[3];
69
70 /* Private helper to extract ipv4 address and stash them in
71 * the protocol structure.
72 */
sctp_v4_copy_addrlist(struct list_head * addrlist,struct net_device * dev)73 static void sctp_v4_copy_addrlist(struct list_head *addrlist,
74 struct net_device *dev)
75 {
76 struct in_device *in_dev;
77 struct in_ifaddr *ifa;
78 struct sctp_sockaddr_entry *addr;
79
80 rcu_read_lock();
81 if ((in_dev = __in_dev_get_rcu(dev)) == NULL) {
82 rcu_read_unlock();
83 return;
84 }
85
86 in_dev_for_each_ifa_rcu(ifa, in_dev) {
87 /* Add the address to the local list. */
88 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
89 if (addr) {
90 addr->a.v4.sin_family = AF_INET;
91 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
92 addr->valid = 1;
93 INIT_LIST_HEAD(&addr->list);
94 list_add_tail(&addr->list, addrlist);
95 }
96 }
97
98 rcu_read_unlock();
99 }
100
101 /* Extract our IP addresses from the system and stash them in the
102 * protocol structure.
103 */
sctp_get_local_addr_list(struct net * net)104 static void sctp_get_local_addr_list(struct net *net)
105 {
106 struct net_device *dev;
107 struct list_head *pos;
108 struct sctp_af *af;
109
110 rcu_read_lock();
111 for_each_netdev_rcu(net, dev) {
112 list_for_each(pos, &sctp_address_families) {
113 af = list_entry(pos, struct sctp_af, list);
114 af->copy_addrlist(&net->sctp.local_addr_list, dev);
115 }
116 }
117 rcu_read_unlock();
118 }
119
120 /* Free the existing local addresses. */
sctp_free_local_addr_list(struct net * net)121 static void sctp_free_local_addr_list(struct net *net)
122 {
123 struct sctp_sockaddr_entry *addr;
124 struct list_head *pos, *temp;
125
126 list_for_each_safe(pos, temp, &net->sctp.local_addr_list) {
127 addr = list_entry(pos, struct sctp_sockaddr_entry, list);
128 list_del(pos);
129 kfree(addr);
130 }
131 }
132
133 /* Copy the local addresses which are valid for 'scope' into 'bp'. */
sctp_copy_local_addr_list(struct net * net,struct sctp_bind_addr * bp,enum sctp_scope scope,gfp_t gfp,int copy_flags)134 int sctp_copy_local_addr_list(struct net *net, struct sctp_bind_addr *bp,
135 enum sctp_scope scope, gfp_t gfp, int copy_flags)
136 {
137 struct sctp_sockaddr_entry *addr;
138 union sctp_addr laddr;
139 int error = 0;
140
141 rcu_read_lock();
142 list_for_each_entry_rcu(addr, &net->sctp.local_addr_list, list) {
143 if (!addr->valid)
144 continue;
145 if (!sctp_in_scope(net, &addr->a, scope))
146 continue;
147
148 /* Now that the address is in scope, check to see if
149 * the address type is really supported by the local
150 * sock as well as the remote peer.
151 */
152 if (addr->a.sa.sa_family == AF_INET &&
153 (!(copy_flags & SCTP_ADDR4_ALLOWED) ||
154 !(copy_flags & SCTP_ADDR4_PEERSUPP)))
155 continue;
156 if (addr->a.sa.sa_family == AF_INET6 &&
157 (!(copy_flags & SCTP_ADDR6_ALLOWED) ||
158 !(copy_flags & SCTP_ADDR6_PEERSUPP)))
159 continue;
160
161 laddr = addr->a;
162 /* also works for setting ipv6 address port */
163 laddr.v4.sin_port = htons(bp->port);
164 if (sctp_bind_addr_state(bp, &laddr) != -1)
165 continue;
166
167 error = sctp_add_bind_addr(bp, &addr->a, sizeof(addr->a),
168 SCTP_ADDR_SRC, GFP_ATOMIC);
169 if (error)
170 break;
171 }
172
173 rcu_read_unlock();
174 return error;
175 }
176
177 /* Copy over any ip options */
sctp_v4_copy_ip_options(struct sock * sk,struct sock * newsk)178 static void sctp_v4_copy_ip_options(struct sock *sk, struct sock *newsk)
179 {
180 struct inet_sock *newinet, *inet = inet_sk(sk);
181 struct ip_options_rcu *inet_opt, *newopt = NULL;
182
183 newinet = inet_sk(newsk);
184
185 rcu_read_lock();
186 inet_opt = rcu_dereference(inet->inet_opt);
187 if (inet_opt) {
188 newopt = sock_kmemdup(newsk, inet_opt, sizeof(*inet_opt) +
189 inet_opt->opt.optlen, GFP_ATOMIC);
190 if (!newopt)
191 pr_err("%s: Failed to copy ip options\n", __func__);
192 }
193 RCU_INIT_POINTER(newinet->inet_opt, newopt);
194 rcu_read_unlock();
195 }
196
197 /* Account for the IP options */
sctp_v4_ip_options_len(struct sock * sk)198 static int sctp_v4_ip_options_len(struct sock *sk)
199 {
200 struct inet_sock *inet = inet_sk(sk);
201 struct ip_options_rcu *inet_opt;
202 int len = 0;
203
204 rcu_read_lock();
205 inet_opt = rcu_dereference(inet->inet_opt);
206 if (inet_opt)
207 len = inet_opt->opt.optlen;
208
209 rcu_read_unlock();
210 return len;
211 }
212
213 /* Initialize a sctp_addr from in incoming skb. */
sctp_v4_from_skb(union sctp_addr * addr,struct sk_buff * skb,int is_saddr)214 static void sctp_v4_from_skb(union sctp_addr *addr, struct sk_buff *skb,
215 int is_saddr)
216 {
217 /* Always called on head skb, so this is safe */
218 struct sctphdr *sh = sctp_hdr(skb);
219 struct sockaddr_in *sa = &addr->v4;
220
221 addr->v4.sin_family = AF_INET;
222
223 if (is_saddr) {
224 sa->sin_port = sh->source;
225 sa->sin_addr.s_addr = ip_hdr(skb)->saddr;
226 } else {
227 sa->sin_port = sh->dest;
228 sa->sin_addr.s_addr = ip_hdr(skb)->daddr;
229 }
230 memset(sa->sin_zero, 0, sizeof(sa->sin_zero));
231 }
232
233 /* Initialize an sctp_addr from a socket. */
sctp_v4_from_sk(union sctp_addr * addr,struct sock * sk)234 static void sctp_v4_from_sk(union sctp_addr *addr, struct sock *sk)
235 {
236 addr->v4.sin_family = AF_INET;
237 addr->v4.sin_port = 0;
238 addr->v4.sin_addr.s_addr = inet_sk(sk)->inet_rcv_saddr;
239 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
240 }
241
242 /* Initialize sk->sk_rcv_saddr from sctp_addr. */
sctp_v4_to_sk_saddr(union sctp_addr * addr,struct sock * sk)243 static void sctp_v4_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
244 {
245 inet_sk(sk)->inet_rcv_saddr = addr->v4.sin_addr.s_addr;
246 }
247
248 /* Initialize sk->sk_daddr from sctp_addr. */
sctp_v4_to_sk_daddr(union sctp_addr * addr,struct sock * sk)249 static void sctp_v4_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
250 {
251 inet_sk(sk)->inet_daddr = addr->v4.sin_addr.s_addr;
252 }
253
254 /* Initialize a sctp_addr from an address parameter. */
sctp_v4_from_addr_param(union sctp_addr * addr,union sctp_addr_param * param,__be16 port,int iif)255 static bool sctp_v4_from_addr_param(union sctp_addr *addr,
256 union sctp_addr_param *param,
257 __be16 port, int iif)
258 {
259 if (ntohs(param->v4.param_hdr.length) < sizeof(struct sctp_ipv4addr_param))
260 return false;
261
262 addr->v4.sin_family = AF_INET;
263 addr->v4.sin_port = port;
264 addr->v4.sin_addr.s_addr = param->v4.addr.s_addr;
265 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
266
267 return true;
268 }
269
270 /* Initialize an address parameter from a sctp_addr and return the length
271 * of the address parameter.
272 */
sctp_v4_to_addr_param(const union sctp_addr * addr,union sctp_addr_param * param)273 static int sctp_v4_to_addr_param(const union sctp_addr *addr,
274 union sctp_addr_param *param)
275 {
276 int length = sizeof(struct sctp_ipv4addr_param);
277
278 param->v4.param_hdr.type = SCTP_PARAM_IPV4_ADDRESS;
279 param->v4.param_hdr.length = htons(length);
280 param->v4.addr.s_addr = addr->v4.sin_addr.s_addr;
281
282 return length;
283 }
284
285 /* Initialize a sctp_addr from a dst_entry. */
sctp_v4_dst_saddr(union sctp_addr * saddr,struct flowi4 * fl4,__be16 port)286 static void sctp_v4_dst_saddr(union sctp_addr *saddr, struct flowi4 *fl4,
287 __be16 port)
288 {
289 saddr->v4.sin_family = AF_INET;
290 saddr->v4.sin_port = port;
291 saddr->v4.sin_addr.s_addr = fl4->saddr;
292 memset(saddr->v4.sin_zero, 0, sizeof(saddr->v4.sin_zero));
293 }
294
295 /* Compare two addresses exactly. */
sctp_v4_cmp_addr(const union sctp_addr * addr1,const union sctp_addr * addr2)296 static int sctp_v4_cmp_addr(const union sctp_addr *addr1,
297 const union sctp_addr *addr2)
298 {
299 if (addr1->sa.sa_family != addr2->sa.sa_family)
300 return 0;
301 if (addr1->v4.sin_port != addr2->v4.sin_port)
302 return 0;
303 if (addr1->v4.sin_addr.s_addr != addr2->v4.sin_addr.s_addr)
304 return 0;
305
306 return 1;
307 }
308
309 /* Initialize addr struct to INADDR_ANY. */
sctp_v4_inaddr_any(union sctp_addr * addr,__be16 port)310 static void sctp_v4_inaddr_any(union sctp_addr *addr, __be16 port)
311 {
312 addr->v4.sin_family = AF_INET;
313 addr->v4.sin_addr.s_addr = htonl(INADDR_ANY);
314 addr->v4.sin_port = port;
315 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
316 }
317
318 /* Is this a wildcard address? */
sctp_v4_is_any(const union sctp_addr * addr)319 static int sctp_v4_is_any(const union sctp_addr *addr)
320 {
321 return htonl(INADDR_ANY) == addr->v4.sin_addr.s_addr;
322 }
323
324 /* This function checks if the address is a valid address to be used for
325 * SCTP binding.
326 *
327 * Output:
328 * Return 0 - If the address is a non-unicast or an illegal address.
329 * Return 1 - If the address is a unicast.
330 */
sctp_v4_addr_valid(union sctp_addr * addr,struct sctp_sock * sp,const struct sk_buff * skb)331 static int sctp_v4_addr_valid(union sctp_addr *addr,
332 struct sctp_sock *sp,
333 const struct sk_buff *skb)
334 {
335 /* IPv4 addresses not allowed */
336 if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
337 return 0;
338
339 /* Is this a non-unicast address or a unusable SCTP address? */
340 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr))
341 return 0;
342
343 /* Is this a broadcast address? */
344 if (skb && skb_rtable(skb)->rt_flags & RTCF_BROADCAST)
345 return 0;
346
347 return 1;
348 }
349
350 /* Should this be available for binding? */
sctp_v4_available(union sctp_addr * addr,struct sctp_sock * sp)351 static int sctp_v4_available(union sctp_addr *addr, struct sctp_sock *sp)
352 {
353 struct sock *sk = &sp->inet.sk;
354 struct net *net = sock_net(sk);
355 int tb_id = RT_TABLE_LOCAL;
356 int ret;
357
358 tb_id = l3mdev_fib_table_by_index(net, sk->sk_bound_dev_if) ?: tb_id;
359 ret = inet_addr_type_table(net, addr->v4.sin_addr.s_addr, tb_id);
360 if (addr->v4.sin_addr.s_addr != htonl(INADDR_ANY) &&
361 ret != RTN_LOCAL &&
362 !inet_test_bit(FREEBIND, sk) &&
363 !READ_ONCE(net->ipv4.sysctl_ip_nonlocal_bind))
364 return 0;
365
366 if (ipv6_only_sock(sctp_opt2sk(sp)))
367 return 0;
368
369 return 1;
370 }
371
372 /* Checking the loopback, private and other address scopes as defined in
373 * RFC 1918. The IPv4 scoping is based on the draft for SCTP IPv4
374 * scoping <draft-stewart-tsvwg-sctp-ipv4-00.txt>.
375 *
376 * Level 0 - unusable SCTP addresses
377 * Level 1 - loopback address
378 * Level 2 - link-local addresses
379 * Level 3 - private addresses.
380 * Level 4 - global addresses
381 * For INIT and INIT-ACK address list, let L be the level of
382 * requested destination address, sender and receiver
383 * SHOULD include all of its addresses with level greater
384 * than or equal to L.
385 *
386 * IPv4 scoping can be controlled through sysctl option
387 * net.sctp.addr_scope_policy
388 */
sctp_v4_scope(union sctp_addr * addr)389 static enum sctp_scope sctp_v4_scope(union sctp_addr *addr)
390 {
391 enum sctp_scope retval;
392
393 /* Check for unusable SCTP addresses. */
394 if (IS_IPV4_UNUSABLE_ADDRESS(addr->v4.sin_addr.s_addr)) {
395 retval = SCTP_SCOPE_UNUSABLE;
396 } else if (ipv4_is_loopback(addr->v4.sin_addr.s_addr)) {
397 retval = SCTP_SCOPE_LOOPBACK;
398 } else if (ipv4_is_linklocal_169(addr->v4.sin_addr.s_addr)) {
399 retval = SCTP_SCOPE_LINK;
400 } else if (ipv4_is_private_10(addr->v4.sin_addr.s_addr) ||
401 ipv4_is_private_172(addr->v4.sin_addr.s_addr) ||
402 ipv4_is_private_192(addr->v4.sin_addr.s_addr) ||
403 ipv4_is_test_198(addr->v4.sin_addr.s_addr)) {
404 retval = SCTP_SCOPE_PRIVATE;
405 } else {
406 retval = SCTP_SCOPE_GLOBAL;
407 }
408
409 return retval;
410 }
411
412 /* Returns a valid dst cache entry for the given source and destination ip
413 * addresses. If an association is passed, trys to get a dst entry with a
414 * source address that matches an address in the bind address list.
415 */
sctp_v4_get_dst(struct sctp_transport * t,union sctp_addr * saddr,struct flowi * fl,struct sock * sk)416 static void sctp_v4_get_dst(struct sctp_transport *t, union sctp_addr *saddr,
417 struct flowi *fl, struct sock *sk)
418 {
419 struct sctp_association *asoc = t->asoc;
420 struct rtable *rt;
421 struct flowi _fl;
422 struct flowi4 *fl4 = &_fl.u.ip4;
423 struct sctp_bind_addr *bp;
424 struct sctp_sockaddr_entry *laddr;
425 struct dst_entry *dst = NULL;
426 union sctp_addr *daddr = &t->ipaddr;
427 union sctp_addr dst_saddr;
428 dscp_t dscp;
429
430 if (t->dscp & SCTP_DSCP_SET_MASK)
431 dscp = inet_dsfield_to_dscp(t->dscp);
432 else
433 dscp = inet_sk_dscp(inet_sk(sk));
434
435 memset(&_fl, 0x0, sizeof(_fl));
436 fl4->daddr = daddr->v4.sin_addr.s_addr;
437 fl4->fl4_dport = daddr->v4.sin_port;
438 fl4->flowi4_proto = IPPROTO_SCTP;
439 if (asoc) {
440 fl4->flowi4_tos = inet_dscp_to_dsfield(dscp);
441 fl4->flowi4_scope = ip_sock_rt_scope(asoc->base.sk);
442 fl4->flowi4_oif = asoc->base.sk->sk_bound_dev_if;
443 fl4->fl4_sport = htons(asoc->base.bind_addr.port);
444 }
445 if (saddr) {
446 fl4->saddr = saddr->v4.sin_addr.s_addr;
447 if (!fl4->fl4_sport)
448 fl4->fl4_sport = saddr->v4.sin_port;
449 }
450
451 pr_debug("%s: dst:%pI4, src:%pI4 - ", __func__, &fl4->daddr,
452 &fl4->saddr);
453
454 rt = ip_route_output_key(sock_net(sk), fl4);
455 if (!IS_ERR(rt)) {
456 dst = &rt->dst;
457 t->dst = dst;
458 memcpy(fl, &_fl, sizeof(_fl));
459 }
460
461 /* If there is no association or if a source address is passed, no
462 * more validation is required.
463 */
464 if (!asoc || saddr)
465 goto out;
466
467 bp = &asoc->base.bind_addr;
468
469 if (dst) {
470 /* Walk through the bind address list and look for a bind
471 * address that matches the source address of the returned dst.
472 */
473 sctp_v4_dst_saddr(&dst_saddr, fl4, htons(bp->port));
474 rcu_read_lock();
475 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
476 if (!laddr->valid || (laddr->state == SCTP_ADDR_DEL) ||
477 (laddr->state != SCTP_ADDR_SRC &&
478 !asoc->src_out_of_asoc_ok))
479 continue;
480 if (sctp_v4_cmp_addr(&dst_saddr, &laddr->a))
481 goto out_unlock;
482 }
483 rcu_read_unlock();
484
485 /* None of the bound addresses match the source address of the
486 * dst. So release it.
487 */
488 dst_release(dst);
489 dst = NULL;
490 }
491
492 /* Walk through the bind address list and try to get a dst that
493 * matches a bind address as the source address.
494 */
495 rcu_read_lock();
496 list_for_each_entry_rcu(laddr, &bp->address_list, list) {
497 struct net_device *odev;
498
499 if (!laddr->valid)
500 continue;
501 if (laddr->state != SCTP_ADDR_SRC ||
502 AF_INET != laddr->a.sa.sa_family)
503 continue;
504
505 fl4->fl4_sport = laddr->a.v4.sin_port;
506 flowi4_update_output(fl4, asoc->base.sk->sk_bound_dev_if,
507 daddr->v4.sin_addr.s_addr,
508 laddr->a.v4.sin_addr.s_addr);
509
510 rt = ip_route_output_key(sock_net(sk), fl4);
511 if (IS_ERR(rt))
512 continue;
513
514 /* Ensure the src address belongs to the output
515 * interface.
516 */
517 odev = __ip_dev_find(sock_net(sk), laddr->a.v4.sin_addr.s_addr,
518 false);
519 if (!odev || odev->ifindex != fl4->flowi4_oif) {
520 if (!dst) {
521 dst = &rt->dst;
522 t->dst = dst;
523 memcpy(fl, &_fl, sizeof(_fl));
524 } else {
525 dst_release(&rt->dst);
526 }
527 continue;
528 }
529
530 dst_release(dst);
531 dst = &rt->dst;
532 t->dst = dst;
533 memcpy(fl, &_fl, sizeof(_fl));
534 break;
535 }
536
537 out_unlock:
538 rcu_read_unlock();
539 out:
540 if (dst) {
541 pr_debug("rt_dst:%pI4, rt_src:%pI4\n",
542 &fl->u.ip4.daddr, &fl->u.ip4.saddr);
543 } else {
544 t->dst = NULL;
545 pr_debug("no route\n");
546 }
547 }
548
549 /* For v4, the source address is cached in the route entry(dst). So no need
550 * to cache it separately and hence this is an empty routine.
551 */
sctp_v4_get_saddr(struct sctp_sock * sk,struct sctp_transport * t,struct flowi * fl)552 static void sctp_v4_get_saddr(struct sctp_sock *sk,
553 struct sctp_transport *t,
554 struct flowi *fl)
555 {
556 union sctp_addr *saddr = &t->saddr;
557 struct rtable *rt = dst_rtable(t->dst);
558
559 if (rt) {
560 saddr->v4.sin_family = AF_INET;
561 saddr->v4.sin_addr.s_addr = fl->u.ip4.saddr;
562 }
563 }
564
565 /* What interface did this skb arrive on? */
sctp_v4_skb_iif(const struct sk_buff * skb)566 static int sctp_v4_skb_iif(const struct sk_buff *skb)
567 {
568 return inet_iif(skb);
569 }
570
sctp_v4_skb_sdif(const struct sk_buff * skb)571 static int sctp_v4_skb_sdif(const struct sk_buff *skb)
572 {
573 return inet_sdif(skb);
574 }
575
576 /* Was this packet marked by Explicit Congestion Notification? */
sctp_v4_is_ce(const struct sk_buff * skb)577 static int sctp_v4_is_ce(const struct sk_buff *skb)
578 {
579 return INET_ECN_is_ce(ip_hdr(skb)->tos);
580 }
581
582 /* Create and initialize a new sk for the socket returned by accept(). */
sctp_v4_create_accept_sk(struct sock * sk,struct sctp_association * asoc,bool kern)583 static struct sock *sctp_v4_create_accept_sk(struct sock *sk,
584 struct sctp_association *asoc,
585 bool kern)
586 {
587 struct sock *newsk = sk_alloc(sock_net(sk), PF_INET, GFP_KERNEL,
588 sk->sk_prot, kern);
589 struct inet_sock *newinet;
590
591 if (!newsk)
592 goto out;
593
594 sock_init_data(NULL, newsk);
595
596 sctp_copy_sock(newsk, sk, asoc);
597 sock_reset_flag(newsk, SOCK_ZAPPED);
598
599 sctp_v4_copy_ip_options(sk, newsk);
600
601 newinet = inet_sk(newsk);
602
603 newinet->inet_daddr = asoc->peer.primary_addr.v4.sin_addr.s_addr;
604
605 if (newsk->sk_prot->init(newsk)) {
606 sk_common_release(newsk);
607 newsk = NULL;
608 }
609
610 out:
611 return newsk;
612 }
613
sctp_v4_addr_to_user(struct sctp_sock * sp,union sctp_addr * addr)614 static int sctp_v4_addr_to_user(struct sctp_sock *sp, union sctp_addr *addr)
615 {
616 /* No address mapping for V4 sockets */
617 memset(addr->v4.sin_zero, 0, sizeof(addr->v4.sin_zero));
618 return sizeof(struct sockaddr_in);
619 }
620
621 /* Dump the v4 addr to the seq file. */
sctp_v4_seq_dump_addr(struct seq_file * seq,union sctp_addr * addr)622 static void sctp_v4_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
623 {
624 seq_printf(seq, "%pI4 ", &addr->v4.sin_addr);
625 }
626
sctp_v4_ecn_capable(struct sock * sk)627 static void sctp_v4_ecn_capable(struct sock *sk)
628 {
629 INET_ECN_xmit(sk);
630 }
631
sctp_addr_wq_timeout_handler(struct timer_list * t)632 static void sctp_addr_wq_timeout_handler(struct timer_list *t)
633 {
634 struct net *net = timer_container_of(net, t, sctp.addr_wq_timer);
635 struct sctp_sockaddr_entry *addrw, *temp;
636 struct sctp_sock *sp;
637
638 spin_lock_bh(&net->sctp.addr_wq_lock);
639
640 list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
641 pr_debug("%s: the first ent in wq:%p is addr:%pISc for cmd:%d at "
642 "entry:%p\n", __func__, &net->sctp.addr_waitq, &addrw->a.sa,
643 addrw->state, addrw);
644
645 #if IS_ENABLED(CONFIG_IPV6)
646 /* Now we send an ASCONF for each association */
647 /* Note. we currently don't handle link local IPv6 addressees */
648 if (addrw->a.sa.sa_family == AF_INET6) {
649 struct in6_addr *in6;
650
651 if (ipv6_addr_type(&addrw->a.v6.sin6_addr) &
652 IPV6_ADDR_LINKLOCAL)
653 goto free_next;
654
655 in6 = (struct in6_addr *)&addrw->a.v6.sin6_addr;
656 if (ipv6_chk_addr(net, in6, NULL, 0) == 0 &&
657 addrw->state == SCTP_ADDR_NEW) {
658 unsigned long timeo_val;
659
660 pr_debug("%s: this is on DAD, trying %d sec "
661 "later\n", __func__,
662 SCTP_ADDRESS_TICK_DELAY);
663
664 timeo_val = jiffies;
665 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
666 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
667 break;
668 }
669 }
670 #endif
671 list_for_each_entry(sp, &net->sctp.auto_asconf_splist, auto_asconf_list) {
672 struct sock *sk;
673
674 sk = sctp_opt2sk(sp);
675 /* ignore bound-specific endpoints */
676 if (!sctp_is_ep_boundall(sk))
677 continue;
678 bh_lock_sock(sk);
679 if (sctp_asconf_mgmt(sp, addrw) < 0)
680 pr_debug("%s: sctp_asconf_mgmt failed\n", __func__);
681 bh_unlock_sock(sk);
682 }
683 #if IS_ENABLED(CONFIG_IPV6)
684 free_next:
685 #endif
686 list_del(&addrw->list);
687 kfree(addrw);
688 }
689 spin_unlock_bh(&net->sctp.addr_wq_lock);
690 }
691
sctp_free_addr_wq(struct net * net)692 static void sctp_free_addr_wq(struct net *net)
693 {
694 struct sctp_sockaddr_entry *addrw;
695 struct sctp_sockaddr_entry *temp;
696
697 spin_lock_bh(&net->sctp.addr_wq_lock);
698 timer_delete(&net->sctp.addr_wq_timer);
699 list_for_each_entry_safe(addrw, temp, &net->sctp.addr_waitq, list) {
700 list_del(&addrw->list);
701 kfree(addrw);
702 }
703 spin_unlock_bh(&net->sctp.addr_wq_lock);
704 }
705
706 /* lookup the entry for the same address in the addr_waitq
707 * sctp_addr_wq MUST be locked
708 */
sctp_addr_wq_lookup(struct net * net,struct sctp_sockaddr_entry * addr)709 static struct sctp_sockaddr_entry *sctp_addr_wq_lookup(struct net *net,
710 struct sctp_sockaddr_entry *addr)
711 {
712 struct sctp_sockaddr_entry *addrw;
713
714 list_for_each_entry(addrw, &net->sctp.addr_waitq, list) {
715 if (addrw->a.sa.sa_family != addr->a.sa.sa_family)
716 continue;
717 if (addrw->a.sa.sa_family == AF_INET) {
718 if (addrw->a.v4.sin_addr.s_addr ==
719 addr->a.v4.sin_addr.s_addr)
720 return addrw;
721 } else if (addrw->a.sa.sa_family == AF_INET6) {
722 if (ipv6_addr_equal(&addrw->a.v6.sin6_addr,
723 &addr->a.v6.sin6_addr))
724 return addrw;
725 }
726 }
727 return NULL;
728 }
729
sctp_addr_wq_mgmt(struct net * net,struct sctp_sockaddr_entry * addr,int cmd)730 void sctp_addr_wq_mgmt(struct net *net, struct sctp_sockaddr_entry *addr, int cmd)
731 {
732 struct sctp_sockaddr_entry *addrw;
733 unsigned long timeo_val;
734
735 /* first, we check if an opposite message already exist in the queue.
736 * If we found such message, it is removed.
737 * This operation is a bit stupid, but the DHCP client attaches the
738 * new address after a couple of addition and deletion of that address
739 */
740
741 spin_lock_bh(&net->sctp.addr_wq_lock);
742
743 /* Avoid searching the queue or modifying it if there are no consumers,
744 * as it can lead to performance degradation if addresses are modified
745 * en-masse.
746 *
747 * If the queue already contains some events, update it anyway to avoid
748 * ugly races between new sessions and new address events.
749 */
750 if (list_empty(&net->sctp.auto_asconf_splist) &&
751 list_empty(&net->sctp.addr_waitq)) {
752 spin_unlock_bh(&net->sctp.addr_wq_lock);
753 return;
754 }
755
756 /* Offsets existing events in addr_wq */
757 addrw = sctp_addr_wq_lookup(net, addr);
758 if (addrw) {
759 if (addrw->state != cmd) {
760 pr_debug("%s: offsets existing entry for %d, addr:%pISc "
761 "in wq:%p\n", __func__, addrw->state, &addrw->a.sa,
762 &net->sctp.addr_waitq);
763
764 list_del(&addrw->list);
765 kfree(addrw);
766 }
767 spin_unlock_bh(&net->sctp.addr_wq_lock);
768 return;
769 }
770
771 /* OK, we have to add the new address to the wait queue */
772 addrw = kmemdup(addr, sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
773 if (addrw == NULL) {
774 spin_unlock_bh(&net->sctp.addr_wq_lock);
775 return;
776 }
777 addrw->state = cmd;
778 list_add_tail(&addrw->list, &net->sctp.addr_waitq);
779
780 pr_debug("%s: add new entry for cmd:%d, addr:%pISc in wq:%p\n",
781 __func__, addrw->state, &addrw->a.sa, &net->sctp.addr_waitq);
782
783 if (!timer_pending(&net->sctp.addr_wq_timer)) {
784 timeo_val = jiffies;
785 timeo_val += msecs_to_jiffies(SCTP_ADDRESS_TICK_DELAY);
786 mod_timer(&net->sctp.addr_wq_timer, timeo_val);
787 }
788 spin_unlock_bh(&net->sctp.addr_wq_lock);
789 }
790
791 /* Event handler for inet address addition/deletion events.
792 * The sctp_local_addr_list needs to be protocted by a spin lock since
793 * multiple notifiers (say IPv4 and IPv6) may be running at the same
794 * time and thus corrupt the list.
795 * The reader side is protected with RCU.
796 */
sctp_inetaddr_event(struct notifier_block * this,unsigned long ev,void * ptr)797 static int sctp_inetaddr_event(struct notifier_block *this, unsigned long ev,
798 void *ptr)
799 {
800 struct in_ifaddr *ifa = (struct in_ifaddr *)ptr;
801 struct sctp_sockaddr_entry *addr = NULL;
802 struct sctp_sockaddr_entry *temp;
803 struct net *net = dev_net(ifa->ifa_dev->dev);
804 int found = 0;
805
806 switch (ev) {
807 case NETDEV_UP:
808 addr = kzalloc(sizeof(*addr), GFP_ATOMIC);
809 if (addr) {
810 addr->a.v4.sin_family = AF_INET;
811 addr->a.v4.sin_addr.s_addr = ifa->ifa_local;
812 addr->valid = 1;
813 spin_lock_bh(&net->sctp.local_addr_lock);
814 list_add_tail_rcu(&addr->list, &net->sctp.local_addr_list);
815 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_NEW);
816 spin_unlock_bh(&net->sctp.local_addr_lock);
817 }
818 break;
819 case NETDEV_DOWN:
820 spin_lock_bh(&net->sctp.local_addr_lock);
821 list_for_each_entry_safe(addr, temp,
822 &net->sctp.local_addr_list, list) {
823 if (addr->a.sa.sa_family == AF_INET &&
824 addr->a.v4.sin_addr.s_addr ==
825 ifa->ifa_local) {
826 found = 1;
827 addr->valid = 0;
828 list_del_rcu(&addr->list);
829 sctp_addr_wq_mgmt(net, addr, SCTP_ADDR_DEL);
830 break;
831 }
832 }
833 spin_unlock_bh(&net->sctp.local_addr_lock);
834 if (found)
835 kfree_rcu(addr, rcu);
836 break;
837 }
838
839 return NOTIFY_DONE;
840 }
841
842 /*
843 * Initialize the control inode/socket with a control endpoint data
844 * structure. This endpoint is reserved exclusively for the OOTB processing.
845 */
sctp_ctl_sock_init(struct net * net)846 static int sctp_ctl_sock_init(struct net *net)
847 {
848 int err;
849 sa_family_t family = PF_INET;
850
851 if (sctp_get_pf_specific(PF_INET6))
852 family = PF_INET6;
853
854 err = inet_ctl_sock_create(&net->sctp.ctl_sock, family,
855 SOCK_SEQPACKET, IPPROTO_SCTP, net);
856
857 /* If IPv6 socket could not be created, try the IPv4 socket */
858 if (err < 0 && family == PF_INET6)
859 err = inet_ctl_sock_create(&net->sctp.ctl_sock, AF_INET,
860 SOCK_SEQPACKET, IPPROTO_SCTP,
861 net);
862
863 if (err < 0) {
864 pr_err("Failed to create the SCTP control socket\n");
865 return err;
866 }
867 return 0;
868 }
869
sctp_udp_rcv(struct sock * sk,struct sk_buff * skb)870 static int sctp_udp_rcv(struct sock *sk, struct sk_buff *skb)
871 {
872 SCTP_INPUT_CB(skb)->encap_port = udp_hdr(skb)->source;
873
874 skb_set_transport_header(skb, sizeof(struct udphdr));
875 sctp_rcv(skb);
876 return 0;
877 }
878
sctp_udp_sock_start(struct net * net)879 int sctp_udp_sock_start(struct net *net)
880 {
881 struct udp_tunnel_sock_cfg tuncfg = {NULL};
882 struct udp_port_cfg udp_conf = {0};
883 struct socket *sock;
884 int err;
885
886 udp_conf.family = AF_INET;
887 udp_conf.local_ip.s_addr = htonl(INADDR_ANY);
888 udp_conf.local_udp_port = htons(net->sctp.udp_port);
889 err = udp_sock_create(net, &udp_conf, &sock);
890 if (err) {
891 pr_err("Failed to create the SCTP UDP tunneling v4 sock\n");
892 return err;
893 }
894
895 tuncfg.encap_type = 1;
896 tuncfg.encap_rcv = sctp_udp_rcv;
897 tuncfg.encap_err_lookup = sctp_udp_v4_err;
898 setup_udp_tunnel_sock(net, sock, &tuncfg);
899 net->sctp.udp4_sock = sock->sk;
900
901 #if IS_ENABLED(CONFIG_IPV6)
902 memset(&udp_conf, 0, sizeof(udp_conf));
903
904 udp_conf.family = AF_INET6;
905 udp_conf.local_ip6 = in6addr_any;
906 udp_conf.local_udp_port = htons(net->sctp.udp_port);
907 udp_conf.use_udp6_rx_checksums = true;
908 udp_conf.ipv6_v6only = true;
909 err = udp_sock_create(net, &udp_conf, &sock);
910 if (err) {
911 pr_err("Failed to create the SCTP UDP tunneling v6 sock\n");
912 udp_tunnel_sock_release(net->sctp.udp4_sock->sk_socket);
913 net->sctp.udp4_sock = NULL;
914 return err;
915 }
916
917 tuncfg.encap_type = 1;
918 tuncfg.encap_rcv = sctp_udp_rcv;
919 tuncfg.encap_err_lookup = sctp_udp_v6_err;
920 setup_udp_tunnel_sock(net, sock, &tuncfg);
921 net->sctp.udp6_sock = sock->sk;
922 #endif
923
924 return 0;
925 }
926
sctp_udp_sock_stop(struct net * net)927 void sctp_udp_sock_stop(struct net *net)
928 {
929 if (net->sctp.udp4_sock) {
930 udp_tunnel_sock_release(net->sctp.udp4_sock->sk_socket);
931 net->sctp.udp4_sock = NULL;
932 }
933 if (net->sctp.udp6_sock) {
934 udp_tunnel_sock_release(net->sctp.udp6_sock->sk_socket);
935 net->sctp.udp6_sock = NULL;
936 }
937 }
938
939 /* Register address family specific functions. */
sctp_register_af(struct sctp_af * af)940 int sctp_register_af(struct sctp_af *af)
941 {
942 switch (af->sa_family) {
943 case AF_INET:
944 if (sctp_af_v4_specific)
945 return 0;
946 sctp_af_v4_specific = af;
947 break;
948 case AF_INET6:
949 if (sctp_af_v6_specific)
950 return 0;
951 sctp_af_v6_specific = af;
952 break;
953 default:
954 return 0;
955 }
956
957 INIT_LIST_HEAD(&af->list);
958 list_add_tail(&af->list, &sctp_address_families);
959 return 1;
960 }
961
962 /* Get the table of functions for manipulating a particular address
963 * family.
964 */
sctp_get_af_specific(sa_family_t family)965 struct sctp_af *sctp_get_af_specific(sa_family_t family)
966 {
967 switch (family) {
968 case AF_INET:
969 return sctp_af_v4_specific;
970 case AF_INET6:
971 return sctp_af_v6_specific;
972 default:
973 return NULL;
974 }
975 }
976
977 /* Common code to initialize a AF_INET msg_name. */
sctp_inet_msgname(char * msgname,int * addr_len)978 static void sctp_inet_msgname(char *msgname, int *addr_len)
979 {
980 struct sockaddr_in *sin;
981
982 sin = (struct sockaddr_in *)msgname;
983 *addr_len = sizeof(struct sockaddr_in);
984 sin->sin_family = AF_INET;
985 memset(sin->sin_zero, 0, sizeof(sin->sin_zero));
986 }
987
988 /* Copy the primary address of the peer primary address as the msg_name. */
sctp_inet_event_msgname(struct sctp_ulpevent * event,char * msgname,int * addr_len)989 static void sctp_inet_event_msgname(struct sctp_ulpevent *event, char *msgname,
990 int *addr_len)
991 {
992 struct sockaddr_in *sin, *sinfrom;
993
994 if (msgname) {
995 struct sctp_association *asoc;
996
997 asoc = event->asoc;
998 sctp_inet_msgname(msgname, addr_len);
999 sin = (struct sockaddr_in *)msgname;
1000 sinfrom = &asoc->peer.primary_addr.v4;
1001 sin->sin_port = htons(asoc->peer.port);
1002 sin->sin_addr.s_addr = sinfrom->sin_addr.s_addr;
1003 }
1004 }
1005
1006 /* Initialize and copy out a msgname from an inbound skb. */
sctp_inet_skb_msgname(struct sk_buff * skb,char * msgname,int * len)1007 static void sctp_inet_skb_msgname(struct sk_buff *skb, char *msgname, int *len)
1008 {
1009 if (msgname) {
1010 struct sctphdr *sh = sctp_hdr(skb);
1011 struct sockaddr_in *sin = (struct sockaddr_in *)msgname;
1012
1013 sctp_inet_msgname(msgname, len);
1014 sin->sin_port = sh->source;
1015 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
1016 }
1017 }
1018
1019 /* Do we support this AF? */
sctp_inet_af_supported(sa_family_t family,struct sctp_sock * sp)1020 static int sctp_inet_af_supported(sa_family_t family, struct sctp_sock *sp)
1021 {
1022 /* PF_INET only supports AF_INET addresses. */
1023 return AF_INET == family;
1024 }
1025
1026 /* Address matching with wildcards allowed. */
sctp_inet_cmp_addr(const union sctp_addr * addr1,const union sctp_addr * addr2,struct sctp_sock * opt)1027 static int sctp_inet_cmp_addr(const union sctp_addr *addr1,
1028 const union sctp_addr *addr2,
1029 struct sctp_sock *opt)
1030 {
1031 /* PF_INET only supports AF_INET addresses. */
1032 if (addr1->sa.sa_family != addr2->sa.sa_family)
1033 return 0;
1034 if (htonl(INADDR_ANY) == addr1->v4.sin_addr.s_addr ||
1035 htonl(INADDR_ANY) == addr2->v4.sin_addr.s_addr)
1036 return 1;
1037 if (addr1->v4.sin_addr.s_addr == addr2->v4.sin_addr.s_addr)
1038 return 1;
1039
1040 return 0;
1041 }
1042
1043 /* Verify that provided sockaddr looks bindable. Common verification has
1044 * already been taken care of.
1045 */
sctp_inet_bind_verify(struct sctp_sock * opt,union sctp_addr * addr)1046 static int sctp_inet_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
1047 {
1048 return sctp_v4_available(addr, opt);
1049 }
1050
1051 /* Verify that sockaddr looks sendable. Common verification has already
1052 * been taken care of.
1053 */
sctp_inet_send_verify(struct sctp_sock * opt,union sctp_addr * addr)1054 static int sctp_inet_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
1055 {
1056 return 1;
1057 }
1058
1059 /* Fill in Supported Address Type information for INIT and INIT-ACK
1060 * chunks. Returns number of addresses supported.
1061 */
sctp_inet_supported_addrs(const struct sctp_sock * opt,__be16 * types)1062 static int sctp_inet_supported_addrs(const struct sctp_sock *opt,
1063 __be16 *types)
1064 {
1065 types[0] = SCTP_PARAM_IPV4_ADDRESS;
1066 return 1;
1067 }
1068
1069 /* Wrapper routine that calls the ip transmit routine. */
sctp_v4_xmit(struct sk_buff * skb,struct sctp_transport * t)1070 static inline int sctp_v4_xmit(struct sk_buff *skb, struct sctp_transport *t)
1071 {
1072 struct dst_entry *dst = dst_clone(t->dst);
1073 struct flowi4 *fl4 = &t->fl.u.ip4;
1074 struct sock *sk = skb->sk;
1075 struct inet_sock *inet = inet_sk(sk);
1076 __u8 dscp = READ_ONCE(inet->tos);
1077 __be16 df = 0;
1078
1079 pr_debug("%s: skb:%p, len:%d, src:%pI4, dst:%pI4\n", __func__, skb,
1080 skb->len, &fl4->saddr, &fl4->daddr);
1081
1082 if (t->dscp & SCTP_DSCP_SET_MASK)
1083 dscp = t->dscp & SCTP_DSCP_VAL_MASK;
1084
1085 inet->pmtudisc = t->param_flags & SPP_PMTUD_ENABLE ? IP_PMTUDISC_DO
1086 : IP_PMTUDISC_DONT;
1087 SCTP_INC_STATS(sock_net(sk), SCTP_MIB_OUTSCTPPACKS);
1088
1089 if (!t->encap_port || !sctp_sk(sk)->udp_port) {
1090 skb_dst_set(skb, dst);
1091 return __ip_queue_xmit(sk, skb, &t->fl, dscp);
1092 }
1093
1094 if (skb_is_gso(skb))
1095 skb_shinfo(skb)->gso_type |= SKB_GSO_UDP_TUNNEL_CSUM;
1096
1097 if (ip_dont_fragment(sk, dst) && !skb->ignore_df)
1098 df = htons(IP_DF);
1099
1100 skb->encapsulation = 1;
1101 skb_reset_inner_mac_header(skb);
1102 skb_reset_inner_transport_header(skb);
1103 skb_set_inner_ipproto(skb, IPPROTO_SCTP);
1104 udp_tunnel_xmit_skb(dst_rtable(dst), sk, skb, fl4->saddr,
1105 fl4->daddr, dscp, ip4_dst_hoplimit(dst), df,
1106 sctp_sk(sk)->udp_port, t->encap_port, false, false);
1107 return 0;
1108 }
1109
1110 static struct sctp_af sctp_af_inet;
1111
1112 static struct sctp_pf sctp_pf_inet = {
1113 .event_msgname = sctp_inet_event_msgname,
1114 .skb_msgname = sctp_inet_skb_msgname,
1115 .af_supported = sctp_inet_af_supported,
1116 .cmp_addr = sctp_inet_cmp_addr,
1117 .bind_verify = sctp_inet_bind_verify,
1118 .send_verify = sctp_inet_send_verify,
1119 .supported_addrs = sctp_inet_supported_addrs,
1120 .create_accept_sk = sctp_v4_create_accept_sk,
1121 .addr_to_user = sctp_v4_addr_to_user,
1122 .to_sk_saddr = sctp_v4_to_sk_saddr,
1123 .to_sk_daddr = sctp_v4_to_sk_daddr,
1124 .copy_ip_options = sctp_v4_copy_ip_options,
1125 .af = &sctp_af_inet
1126 };
1127
1128 /* Notifier for inetaddr addition/deletion events. */
1129 static struct notifier_block sctp_inetaddr_notifier = {
1130 .notifier_call = sctp_inetaddr_event,
1131 };
1132
1133 /* Socket operations. */
1134 static const struct proto_ops inet_seqpacket_ops = {
1135 .family = PF_INET,
1136 .owner = THIS_MODULE,
1137 .release = inet_release, /* Needs to be wrapped... */
1138 .bind = inet_bind,
1139 .connect = sctp_inet_connect,
1140 .socketpair = sock_no_socketpair,
1141 .accept = inet_accept,
1142 .getname = inet_getname, /* Semantics are different. */
1143 .poll = sctp_poll,
1144 .ioctl = inet_ioctl,
1145 .gettstamp = sock_gettstamp,
1146 .listen = sctp_inet_listen,
1147 .shutdown = inet_shutdown, /* Looks harmless. */
1148 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
1149 .getsockopt = sock_common_getsockopt,
1150 .sendmsg = inet_sendmsg,
1151 .recvmsg = inet_recvmsg,
1152 .mmap = sock_no_mmap,
1153 };
1154
1155 /* Registration with AF_INET family. */
1156 static struct inet_protosw sctp_seqpacket_protosw = {
1157 .type = SOCK_SEQPACKET,
1158 .protocol = IPPROTO_SCTP,
1159 .prot = &sctp_prot,
1160 .ops = &inet_seqpacket_ops,
1161 .flags = SCTP_PROTOSW_FLAG
1162 };
1163 static struct inet_protosw sctp_stream_protosw = {
1164 .type = SOCK_STREAM,
1165 .protocol = IPPROTO_SCTP,
1166 .prot = &sctp_prot,
1167 .ops = &inet_seqpacket_ops,
1168 .flags = SCTP_PROTOSW_FLAG
1169 };
1170
sctp4_rcv(struct sk_buff * skb)1171 static int sctp4_rcv(struct sk_buff *skb)
1172 {
1173 SCTP_INPUT_CB(skb)->encap_port = 0;
1174 return sctp_rcv(skb);
1175 }
1176
1177 /* Register with IP layer. */
1178 static const struct net_protocol sctp_protocol = {
1179 .handler = sctp4_rcv,
1180 .err_handler = sctp_v4_err,
1181 .no_policy = 1,
1182 .icmp_strict_tag_validation = 1,
1183 };
1184
1185 /* IPv4 address related functions. */
1186 static struct sctp_af sctp_af_inet = {
1187 .sa_family = AF_INET,
1188 .sctp_xmit = sctp_v4_xmit,
1189 .setsockopt = ip_setsockopt,
1190 .getsockopt = ip_getsockopt,
1191 .get_dst = sctp_v4_get_dst,
1192 .get_saddr = sctp_v4_get_saddr,
1193 .copy_addrlist = sctp_v4_copy_addrlist,
1194 .from_skb = sctp_v4_from_skb,
1195 .from_sk = sctp_v4_from_sk,
1196 .from_addr_param = sctp_v4_from_addr_param,
1197 .to_addr_param = sctp_v4_to_addr_param,
1198 .cmp_addr = sctp_v4_cmp_addr,
1199 .addr_valid = sctp_v4_addr_valid,
1200 .inaddr_any = sctp_v4_inaddr_any,
1201 .is_any = sctp_v4_is_any,
1202 .available = sctp_v4_available,
1203 .scope = sctp_v4_scope,
1204 .skb_iif = sctp_v4_skb_iif,
1205 .skb_sdif = sctp_v4_skb_sdif,
1206 .is_ce = sctp_v4_is_ce,
1207 .seq_dump_addr = sctp_v4_seq_dump_addr,
1208 .ecn_capable = sctp_v4_ecn_capable,
1209 .net_header_len = sizeof(struct iphdr),
1210 .sockaddr_len = sizeof(struct sockaddr_in),
1211 .ip_options_len = sctp_v4_ip_options_len,
1212 };
1213
sctp_get_pf_specific(sa_family_t family)1214 struct sctp_pf *sctp_get_pf_specific(sa_family_t family)
1215 {
1216 switch (family) {
1217 case PF_INET:
1218 return sctp_pf_inet_specific;
1219 case PF_INET6:
1220 return sctp_pf_inet6_specific;
1221 default:
1222 return NULL;
1223 }
1224 }
1225
1226 /* Register the PF specific function table. */
sctp_register_pf(struct sctp_pf * pf,sa_family_t family)1227 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
1228 {
1229 switch (family) {
1230 case PF_INET:
1231 if (sctp_pf_inet_specific)
1232 return 0;
1233 sctp_pf_inet_specific = pf;
1234 break;
1235 case PF_INET6:
1236 if (sctp_pf_inet6_specific)
1237 return 0;
1238 sctp_pf_inet6_specific = pf;
1239 break;
1240 default:
1241 return 0;
1242 }
1243 return 1;
1244 }
1245
init_sctp_mibs(struct net * net)1246 static inline int init_sctp_mibs(struct net *net)
1247 {
1248 net->sctp.sctp_statistics = alloc_percpu(struct sctp_mib);
1249 if (!net->sctp.sctp_statistics)
1250 return -ENOMEM;
1251 return 0;
1252 }
1253
cleanup_sctp_mibs(struct net * net)1254 static inline void cleanup_sctp_mibs(struct net *net)
1255 {
1256 free_percpu(net->sctp.sctp_statistics);
1257 }
1258
sctp_v4_pf_init(void)1259 static void sctp_v4_pf_init(void)
1260 {
1261 /* Initialize the SCTP specific PF functions. */
1262 sctp_register_pf(&sctp_pf_inet, PF_INET);
1263 sctp_register_af(&sctp_af_inet);
1264 }
1265
sctp_v4_pf_exit(void)1266 static void sctp_v4_pf_exit(void)
1267 {
1268 list_del(&sctp_af_inet.list);
1269 }
1270
sctp_v4_protosw_init(void)1271 static int sctp_v4_protosw_init(void)
1272 {
1273 int rc;
1274
1275 rc = proto_register(&sctp_prot, 1);
1276 if (rc)
1277 return rc;
1278
1279 /* Register SCTP(UDP and TCP style) with socket layer. */
1280 inet_register_protosw(&sctp_seqpacket_protosw);
1281 inet_register_protosw(&sctp_stream_protosw);
1282
1283 return 0;
1284 }
1285
sctp_v4_protosw_exit(void)1286 static void sctp_v4_protosw_exit(void)
1287 {
1288 inet_unregister_protosw(&sctp_stream_protosw);
1289 inet_unregister_protosw(&sctp_seqpacket_protosw);
1290 proto_unregister(&sctp_prot);
1291 }
1292
sctp_v4_add_protocol(void)1293 static int sctp_v4_add_protocol(void)
1294 {
1295 /* Register notifier for inet address additions/deletions. */
1296 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1297
1298 /* Register SCTP with inet layer. */
1299 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
1300 return -EAGAIN;
1301
1302 return 0;
1303 }
1304
sctp_v4_del_protocol(void)1305 static void sctp_v4_del_protocol(void)
1306 {
1307 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1308 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1309 }
1310
sctp_defaults_init(struct net * net)1311 static int __net_init sctp_defaults_init(struct net *net)
1312 {
1313 int status;
1314
1315 /*
1316 * 14. Suggested SCTP Protocol Parameter Values
1317 */
1318 /* The following protocol parameters are RECOMMENDED: */
1319 /* RTO.Initial - 3 seconds */
1320 net->sctp.rto_initial = SCTP_RTO_INITIAL;
1321 /* RTO.Min - 1 second */
1322 net->sctp.rto_min = SCTP_RTO_MIN;
1323 /* RTO.Max - 60 seconds */
1324 net->sctp.rto_max = SCTP_RTO_MAX;
1325 /* RTO.Alpha - 1/8 */
1326 net->sctp.rto_alpha = SCTP_RTO_ALPHA;
1327 /* RTO.Beta - 1/4 */
1328 net->sctp.rto_beta = SCTP_RTO_BETA;
1329
1330 /* Valid.Cookie.Life - 60 seconds */
1331 net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1332
1333 /* Whether Cookie Preservative is enabled(1) or not(0) */
1334 net->sctp.cookie_preserve_enable = 1;
1335
1336 /* Default sctp sockets to use md5 as their hmac alg */
1337 #if defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_MD5)
1338 net->sctp.sctp_hmac_alg = "md5";
1339 #elif defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1)
1340 net->sctp.sctp_hmac_alg = "sha1";
1341 #else
1342 net->sctp.sctp_hmac_alg = NULL;
1343 #endif
1344
1345 /* Max.Burst - 4 */
1346 net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
1347
1348 /* Disable of Primary Path Switchover by default */
1349 net->sctp.ps_retrans = SCTP_PS_RETRANS_MAX;
1350
1351 /* Enable pf state by default */
1352 net->sctp.pf_enable = 1;
1353
1354 /* Ignore pf exposure feature by default */
1355 net->sctp.pf_expose = SCTP_PF_EXPOSE_UNSET;
1356
1357 /* Association.Max.Retrans - 10 attempts
1358 * Path.Max.Retrans - 5 attempts (per destination address)
1359 * Max.Init.Retransmits - 8 attempts
1360 */
1361 net->sctp.max_retrans_association = 10;
1362 net->sctp.max_retrans_path = 5;
1363 net->sctp.max_retrans_init = 8;
1364
1365 /* Sendbuffer growth - do per-socket accounting */
1366 net->sctp.sndbuf_policy = 0;
1367
1368 /* Rcvbuffer growth - do per-socket accounting */
1369 net->sctp.rcvbuf_policy = 0;
1370
1371 /* HB.interval - 30 seconds */
1372 net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1373
1374 /* delayed SACK timeout */
1375 net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1376
1377 /* Disable ADDIP by default. */
1378 net->sctp.addip_enable = 0;
1379 net->sctp.addip_noauth = 0;
1380 net->sctp.default_auto_asconf = 0;
1381
1382 /* Enable PR-SCTP by default. */
1383 net->sctp.prsctp_enable = 1;
1384
1385 /* Disable RECONF by default. */
1386 net->sctp.reconf_enable = 0;
1387
1388 /* Disable AUTH by default. */
1389 net->sctp.auth_enable = 0;
1390
1391 /* Enable ECN by default. */
1392 net->sctp.ecn_enable = 1;
1393
1394 /* Set UDP tunneling listening port to 0 by default */
1395 net->sctp.udp_port = 0;
1396
1397 /* Set remote encap port to 0 by default */
1398 net->sctp.encap_port = 0;
1399
1400 /* Set SCOPE policy to enabled */
1401 net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
1402
1403 /* Set the default rwnd update threshold */
1404 net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
1405
1406 /* Initialize maximum autoclose timeout. */
1407 net->sctp.max_autoclose = INT_MAX / HZ;
1408
1409 #ifdef CONFIG_NET_L3_MASTER_DEV
1410 net->sctp.l3mdev_accept = 1;
1411 #endif
1412
1413 status = sctp_sysctl_net_register(net);
1414 if (status)
1415 goto err_sysctl_register;
1416
1417 /* Allocate and initialise sctp mibs. */
1418 status = init_sctp_mibs(net);
1419 if (status)
1420 goto err_init_mibs;
1421
1422 #ifdef CONFIG_PROC_FS
1423 /* Initialize proc fs directory. */
1424 status = sctp_proc_init(net);
1425 if (status)
1426 goto err_init_proc;
1427 #endif
1428
1429 sctp_dbg_objcnt_init(net);
1430
1431 /* Initialize the local address list. */
1432 INIT_LIST_HEAD(&net->sctp.local_addr_list);
1433 spin_lock_init(&net->sctp.local_addr_lock);
1434 sctp_get_local_addr_list(net);
1435
1436 /* Initialize the address event list */
1437 INIT_LIST_HEAD(&net->sctp.addr_waitq);
1438 INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
1439 spin_lock_init(&net->sctp.addr_wq_lock);
1440 net->sctp.addr_wq_timer.expires = 0;
1441 timer_setup(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler, 0);
1442
1443 return 0;
1444
1445 #ifdef CONFIG_PROC_FS
1446 err_init_proc:
1447 cleanup_sctp_mibs(net);
1448 #endif
1449 err_init_mibs:
1450 sctp_sysctl_net_unregister(net);
1451 err_sysctl_register:
1452 return status;
1453 }
1454
sctp_defaults_exit(struct net * net)1455 static void __net_exit sctp_defaults_exit(struct net *net)
1456 {
1457 /* Free the local address list */
1458 sctp_free_addr_wq(net);
1459 sctp_free_local_addr_list(net);
1460
1461 #ifdef CONFIG_PROC_FS
1462 remove_proc_subtree("sctp", net->proc_net);
1463 net->sctp.proc_net_sctp = NULL;
1464 #endif
1465 cleanup_sctp_mibs(net);
1466 sctp_sysctl_net_unregister(net);
1467 }
1468
1469 static struct pernet_operations sctp_defaults_ops = {
1470 .init = sctp_defaults_init,
1471 .exit = sctp_defaults_exit,
1472 };
1473
sctp_ctrlsock_init(struct net * net)1474 static int __net_init sctp_ctrlsock_init(struct net *net)
1475 {
1476 int status;
1477
1478 /* Initialize the control inode/socket for handling OOTB packets. */
1479 status = sctp_ctl_sock_init(net);
1480 if (status)
1481 pr_err("Failed to initialize the SCTP control sock\n");
1482
1483 return status;
1484 }
1485
sctp_ctrlsock_exit(struct net * net)1486 static void __net_exit sctp_ctrlsock_exit(struct net *net)
1487 {
1488 /* Free the control endpoint. */
1489 inet_ctl_sock_destroy(net->sctp.ctl_sock);
1490 }
1491
1492 static struct pernet_operations sctp_ctrlsock_ops = {
1493 .init = sctp_ctrlsock_init,
1494 .exit = sctp_ctrlsock_exit,
1495 };
1496
1497 /* Initialize the universe into something sensible. */
sctp_init(void)1498 static __init int sctp_init(void)
1499 {
1500 unsigned long nr_pages = totalram_pages();
1501 unsigned long limit;
1502 unsigned long goal;
1503 int max_entry_order;
1504 int num_entries;
1505 int max_share;
1506 int status;
1507 int order;
1508 int i;
1509
1510 sock_skb_cb_check_size(sizeof(struct sctp_ulpevent));
1511
1512 /* Allocate bind_bucket and chunk caches. */
1513 status = -ENOBUFS;
1514 sctp_bucket_cachep = KMEM_CACHE(sctp_bind_bucket, SLAB_HWCACHE_ALIGN);
1515 if (!sctp_bucket_cachep)
1516 goto out;
1517
1518 sctp_chunk_cachep = KMEM_CACHE(sctp_chunk, SLAB_HWCACHE_ALIGN);
1519 if (!sctp_chunk_cachep)
1520 goto err_chunk_cachep;
1521
1522 status = percpu_counter_init(&sctp_sockets_allocated, 0, GFP_KERNEL);
1523 if (status)
1524 goto err_percpu_counter_init;
1525
1526 /* Implementation specific variables. */
1527
1528 /* Initialize default stream count setup information. */
1529 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1530 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1531
1532 /* Initialize handle used for association ids. */
1533 idr_init(&sctp_assocs_id);
1534
1535 limit = nr_free_buffer_pages() / 8;
1536 limit = max(limit, 128UL);
1537 sysctl_sctp_mem[0] = limit / 4 * 3;
1538 sysctl_sctp_mem[1] = limit;
1539 sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
1540
1541 /* Set per-socket limits to no more than 1/128 the pressure threshold*/
1542 limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
1543 max_share = min(4UL*1024*1024, limit);
1544
1545 sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */
1546 sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
1547 sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
1548
1549 sysctl_sctp_wmem[0] = PAGE_SIZE;
1550 sysctl_sctp_wmem[1] = 16*1024;
1551 sysctl_sctp_wmem[2] = max(64*1024, max_share);
1552
1553 /* Size and allocate the association hash table.
1554 * The methodology is similar to that of the tcp hash tables.
1555 * Though not identical. Start by getting a goal size
1556 */
1557 if (nr_pages >= (128 * 1024))
1558 goal = nr_pages >> (22 - PAGE_SHIFT);
1559 else
1560 goal = nr_pages >> (24 - PAGE_SHIFT);
1561
1562 /* Then compute the page order for said goal */
1563 order = get_order(goal);
1564
1565 /* Now compute the required page order for the maximum sized table we
1566 * want to create
1567 */
1568 max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES *
1569 sizeof(struct sctp_bind_hashbucket));
1570
1571 /* Limit the page order by that maximum hash table size */
1572 order = min(order, max_entry_order);
1573
1574 /* Allocate and initialize the endpoint hash table. */
1575 sctp_ep_hashsize = 64;
1576 sctp_ep_hashtable =
1577 kmalloc_array(64, sizeof(struct sctp_hashbucket), GFP_KERNEL);
1578 if (!sctp_ep_hashtable) {
1579 pr_err("Failed endpoint_hash alloc\n");
1580 status = -ENOMEM;
1581 goto err_ehash_alloc;
1582 }
1583 for (i = 0; i < sctp_ep_hashsize; i++) {
1584 rwlock_init(&sctp_ep_hashtable[i].lock);
1585 INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
1586 }
1587
1588 /* Allocate and initialize the SCTP port hash table.
1589 * Note that order is initalized to start at the max sized
1590 * table we want to support. If we can't get that many pages
1591 * reduce the order and try again
1592 */
1593 do {
1594 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1595 __get_free_pages(GFP_KERNEL | __GFP_NOWARN, order);
1596 } while (!sctp_port_hashtable && --order > 0);
1597
1598 if (!sctp_port_hashtable) {
1599 pr_err("Failed bind hash alloc\n");
1600 status = -ENOMEM;
1601 goto err_bhash_alloc;
1602 }
1603
1604 /* Now compute the number of entries that will fit in the
1605 * port hash space we allocated
1606 */
1607 num_entries = (1UL << order) * PAGE_SIZE /
1608 sizeof(struct sctp_bind_hashbucket);
1609
1610 /* And finish by rounding it down to the nearest power of two.
1611 * This wastes some memory of course, but it's needed because
1612 * the hash function operates based on the assumption that
1613 * the number of entries is a power of two.
1614 */
1615 sctp_port_hashsize = rounddown_pow_of_two(num_entries);
1616
1617 for (i = 0; i < sctp_port_hashsize; i++) {
1618 spin_lock_init(&sctp_port_hashtable[i].lock);
1619 INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
1620 }
1621
1622 status = sctp_transport_hashtable_init();
1623 if (status)
1624 goto err_thash_alloc;
1625
1626 pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize,
1627 num_entries);
1628
1629 sctp_sysctl_register();
1630
1631 INIT_LIST_HEAD(&sctp_address_families);
1632 sctp_v4_pf_init();
1633 sctp_v6_pf_init();
1634 sctp_sched_ops_init();
1635
1636 status = register_pernet_subsys(&sctp_defaults_ops);
1637 if (status)
1638 goto err_register_defaults;
1639
1640 status = sctp_v4_protosw_init();
1641 if (status)
1642 goto err_protosw_init;
1643
1644 status = sctp_v6_protosw_init();
1645 if (status)
1646 goto err_v6_protosw_init;
1647
1648 status = register_pernet_subsys(&sctp_ctrlsock_ops);
1649 if (status)
1650 goto err_register_ctrlsock;
1651
1652 status = sctp_v4_add_protocol();
1653 if (status)
1654 goto err_add_protocol;
1655
1656 /* Register SCTP with inet6 layer. */
1657 status = sctp_v6_add_protocol();
1658 if (status)
1659 goto err_v6_add_protocol;
1660
1661 if (sctp_offload_init() < 0)
1662 pr_crit("%s: Cannot add SCTP protocol offload\n", __func__);
1663
1664 out:
1665 return status;
1666 err_v6_add_protocol:
1667 sctp_v4_del_protocol();
1668 err_add_protocol:
1669 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1670 err_register_ctrlsock:
1671 sctp_v6_protosw_exit();
1672 err_v6_protosw_init:
1673 sctp_v4_protosw_exit();
1674 err_protosw_init:
1675 unregister_pernet_subsys(&sctp_defaults_ops);
1676 err_register_defaults:
1677 sctp_v4_pf_exit();
1678 sctp_v6_pf_exit();
1679 sctp_sysctl_unregister();
1680 free_pages((unsigned long)sctp_port_hashtable,
1681 get_order(sctp_port_hashsize *
1682 sizeof(struct sctp_bind_hashbucket)));
1683 err_bhash_alloc:
1684 sctp_transport_hashtable_destroy();
1685 err_thash_alloc:
1686 kfree(sctp_ep_hashtable);
1687 err_ehash_alloc:
1688 percpu_counter_destroy(&sctp_sockets_allocated);
1689 err_percpu_counter_init:
1690 kmem_cache_destroy(sctp_chunk_cachep);
1691 err_chunk_cachep:
1692 kmem_cache_destroy(sctp_bucket_cachep);
1693 goto out;
1694 }
1695
1696 /* Exit handler for the SCTP protocol. */
sctp_exit(void)1697 static __exit void sctp_exit(void)
1698 {
1699 /* BUG. This should probably do something useful like clean
1700 * up all the remaining associations and all that memory.
1701 */
1702
1703 /* Unregister with inet6/inet layers. */
1704 sctp_v6_del_protocol();
1705 sctp_v4_del_protocol();
1706
1707 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1708
1709 /* Free protosw registrations */
1710 sctp_v6_protosw_exit();
1711 sctp_v4_protosw_exit();
1712
1713 unregister_pernet_subsys(&sctp_defaults_ops);
1714
1715 /* Unregister with socket layer. */
1716 sctp_v6_pf_exit();
1717 sctp_v4_pf_exit();
1718
1719 sctp_sysctl_unregister();
1720
1721 free_pages((unsigned long)sctp_port_hashtable,
1722 get_order(sctp_port_hashsize *
1723 sizeof(struct sctp_bind_hashbucket)));
1724 kfree(sctp_ep_hashtable);
1725 sctp_transport_hashtable_destroy();
1726
1727 percpu_counter_destroy(&sctp_sockets_allocated);
1728
1729 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1730
1731 kmem_cache_destroy(sctp_chunk_cachep);
1732 kmem_cache_destroy(sctp_bucket_cachep);
1733 }
1734
1735 module_init(sctp_init);
1736 module_exit(sctp_exit);
1737
1738 /*
1739 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1740 */
1741 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1742 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1743 MODULE_AUTHOR("Linux Kernel SCTP developers <linux-sctp@vger.kernel.org>");
1744 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1745 module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
1746 MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
1747 MODULE_LICENSE("GPL");
1748