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 0);
1108 return 0;
1109 }
1110
1111 static struct sctp_af sctp_af_inet;
1112
1113 static struct sctp_pf sctp_pf_inet = {
1114 .event_msgname = sctp_inet_event_msgname,
1115 .skb_msgname = sctp_inet_skb_msgname,
1116 .af_supported = sctp_inet_af_supported,
1117 .cmp_addr = sctp_inet_cmp_addr,
1118 .bind_verify = sctp_inet_bind_verify,
1119 .send_verify = sctp_inet_send_verify,
1120 .supported_addrs = sctp_inet_supported_addrs,
1121 .create_accept_sk = sctp_v4_create_accept_sk,
1122 .addr_to_user = sctp_v4_addr_to_user,
1123 .to_sk_saddr = sctp_v4_to_sk_saddr,
1124 .to_sk_daddr = sctp_v4_to_sk_daddr,
1125 .copy_ip_options = sctp_v4_copy_ip_options,
1126 .af = &sctp_af_inet
1127 };
1128
1129 /* Notifier for inetaddr addition/deletion events. */
1130 static struct notifier_block sctp_inetaddr_notifier = {
1131 .notifier_call = sctp_inetaddr_event,
1132 };
1133
1134 /* Socket operations. */
1135 static const struct proto_ops inet_seqpacket_ops = {
1136 .family = PF_INET,
1137 .owner = THIS_MODULE,
1138 .release = inet_release, /* Needs to be wrapped... */
1139 .bind = inet_bind,
1140 .connect = sctp_inet_connect,
1141 .socketpair = sock_no_socketpair,
1142 .accept = inet_accept,
1143 .getname = inet_getname, /* Semantics are different. */
1144 .poll = sctp_poll,
1145 .ioctl = inet_ioctl,
1146 .gettstamp = sock_gettstamp,
1147 .listen = sctp_inet_listen,
1148 .shutdown = inet_shutdown, /* Looks harmless. */
1149 .setsockopt = sock_common_setsockopt, /* IP_SOL IP_OPTION is a problem */
1150 .getsockopt = sock_common_getsockopt,
1151 .sendmsg = inet_sendmsg,
1152 .recvmsg = inet_recvmsg,
1153 .mmap = sock_no_mmap,
1154 };
1155
1156 /* Registration with AF_INET family. */
1157 static struct inet_protosw sctp_seqpacket_protosw = {
1158 .type = SOCK_SEQPACKET,
1159 .protocol = IPPROTO_SCTP,
1160 .prot = &sctp_prot,
1161 .ops = &inet_seqpacket_ops,
1162 .flags = SCTP_PROTOSW_FLAG
1163 };
1164 static struct inet_protosw sctp_stream_protosw = {
1165 .type = SOCK_STREAM,
1166 .protocol = IPPROTO_SCTP,
1167 .prot = &sctp_prot,
1168 .ops = &inet_seqpacket_ops,
1169 .flags = SCTP_PROTOSW_FLAG
1170 };
1171
sctp4_rcv(struct sk_buff * skb)1172 static int sctp4_rcv(struct sk_buff *skb)
1173 {
1174 SCTP_INPUT_CB(skb)->encap_port = 0;
1175 return sctp_rcv(skb);
1176 }
1177
1178 /* Register with IP layer. */
1179 static const struct net_protocol sctp_protocol = {
1180 .handler = sctp4_rcv,
1181 .err_handler = sctp_v4_err,
1182 .no_policy = 1,
1183 .icmp_strict_tag_validation = 1,
1184 };
1185
1186 /* IPv4 address related functions. */
1187 static struct sctp_af sctp_af_inet = {
1188 .sa_family = AF_INET,
1189 .sctp_xmit = sctp_v4_xmit,
1190 .setsockopt = ip_setsockopt,
1191 .getsockopt = ip_getsockopt,
1192 .get_dst = sctp_v4_get_dst,
1193 .get_saddr = sctp_v4_get_saddr,
1194 .copy_addrlist = sctp_v4_copy_addrlist,
1195 .from_skb = sctp_v4_from_skb,
1196 .from_sk = sctp_v4_from_sk,
1197 .from_addr_param = sctp_v4_from_addr_param,
1198 .to_addr_param = sctp_v4_to_addr_param,
1199 .cmp_addr = sctp_v4_cmp_addr,
1200 .addr_valid = sctp_v4_addr_valid,
1201 .inaddr_any = sctp_v4_inaddr_any,
1202 .is_any = sctp_v4_is_any,
1203 .available = sctp_v4_available,
1204 .scope = sctp_v4_scope,
1205 .skb_iif = sctp_v4_skb_iif,
1206 .skb_sdif = sctp_v4_skb_sdif,
1207 .is_ce = sctp_v4_is_ce,
1208 .seq_dump_addr = sctp_v4_seq_dump_addr,
1209 .ecn_capable = sctp_v4_ecn_capable,
1210 .net_header_len = sizeof(struct iphdr),
1211 .sockaddr_len = sizeof(struct sockaddr_in),
1212 .ip_options_len = sctp_v4_ip_options_len,
1213 };
1214
sctp_get_pf_specific(sa_family_t family)1215 struct sctp_pf *sctp_get_pf_specific(sa_family_t family)
1216 {
1217 switch (family) {
1218 case PF_INET:
1219 return sctp_pf_inet_specific;
1220 case PF_INET6:
1221 return sctp_pf_inet6_specific;
1222 default:
1223 return NULL;
1224 }
1225 }
1226
1227 /* Register the PF specific function table. */
sctp_register_pf(struct sctp_pf * pf,sa_family_t family)1228 int sctp_register_pf(struct sctp_pf *pf, sa_family_t family)
1229 {
1230 switch (family) {
1231 case PF_INET:
1232 if (sctp_pf_inet_specific)
1233 return 0;
1234 sctp_pf_inet_specific = pf;
1235 break;
1236 case PF_INET6:
1237 if (sctp_pf_inet6_specific)
1238 return 0;
1239 sctp_pf_inet6_specific = pf;
1240 break;
1241 default:
1242 return 0;
1243 }
1244 return 1;
1245 }
1246
init_sctp_mibs(struct net * net)1247 static inline int init_sctp_mibs(struct net *net)
1248 {
1249 net->sctp.sctp_statistics = alloc_percpu(struct sctp_mib);
1250 if (!net->sctp.sctp_statistics)
1251 return -ENOMEM;
1252 return 0;
1253 }
1254
cleanup_sctp_mibs(struct net * net)1255 static inline void cleanup_sctp_mibs(struct net *net)
1256 {
1257 free_percpu(net->sctp.sctp_statistics);
1258 }
1259
sctp_v4_pf_init(void)1260 static void sctp_v4_pf_init(void)
1261 {
1262 /* Initialize the SCTP specific PF functions. */
1263 sctp_register_pf(&sctp_pf_inet, PF_INET);
1264 sctp_register_af(&sctp_af_inet);
1265 }
1266
sctp_v4_pf_exit(void)1267 static void sctp_v4_pf_exit(void)
1268 {
1269 list_del(&sctp_af_inet.list);
1270 }
1271
sctp_v4_protosw_init(void)1272 static int sctp_v4_protosw_init(void)
1273 {
1274 int rc;
1275
1276 rc = proto_register(&sctp_prot, 1);
1277 if (rc)
1278 return rc;
1279
1280 /* Register SCTP(UDP and TCP style) with socket layer. */
1281 inet_register_protosw(&sctp_seqpacket_protosw);
1282 inet_register_protosw(&sctp_stream_protosw);
1283
1284 return 0;
1285 }
1286
sctp_v4_protosw_exit(void)1287 static void sctp_v4_protosw_exit(void)
1288 {
1289 inet_unregister_protosw(&sctp_stream_protosw);
1290 inet_unregister_protosw(&sctp_seqpacket_protosw);
1291 proto_unregister(&sctp_prot);
1292 }
1293
sctp_v4_add_protocol(void)1294 static int sctp_v4_add_protocol(void)
1295 {
1296 /* Register notifier for inet address additions/deletions. */
1297 register_inetaddr_notifier(&sctp_inetaddr_notifier);
1298
1299 /* Register SCTP with inet layer. */
1300 if (inet_add_protocol(&sctp_protocol, IPPROTO_SCTP) < 0)
1301 return -EAGAIN;
1302
1303 return 0;
1304 }
1305
sctp_v4_del_protocol(void)1306 static void sctp_v4_del_protocol(void)
1307 {
1308 inet_del_protocol(&sctp_protocol, IPPROTO_SCTP);
1309 unregister_inetaddr_notifier(&sctp_inetaddr_notifier);
1310 }
1311
sctp_defaults_init(struct net * net)1312 static int __net_init sctp_defaults_init(struct net *net)
1313 {
1314 int status;
1315
1316 /*
1317 * 14. Suggested SCTP Protocol Parameter Values
1318 */
1319 /* The following protocol parameters are RECOMMENDED: */
1320 /* RTO.Initial - 3 seconds */
1321 net->sctp.rto_initial = SCTP_RTO_INITIAL;
1322 /* RTO.Min - 1 second */
1323 net->sctp.rto_min = SCTP_RTO_MIN;
1324 /* RTO.Max - 60 seconds */
1325 net->sctp.rto_max = SCTP_RTO_MAX;
1326 /* RTO.Alpha - 1/8 */
1327 net->sctp.rto_alpha = SCTP_RTO_ALPHA;
1328 /* RTO.Beta - 1/4 */
1329 net->sctp.rto_beta = SCTP_RTO_BETA;
1330
1331 /* Valid.Cookie.Life - 60 seconds */
1332 net->sctp.valid_cookie_life = SCTP_DEFAULT_COOKIE_LIFE;
1333
1334 /* Whether Cookie Preservative is enabled(1) or not(0) */
1335 net->sctp.cookie_preserve_enable = 1;
1336
1337 /* Default sctp sockets to use md5 as their hmac alg */
1338 #if defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_MD5)
1339 net->sctp.sctp_hmac_alg = "md5";
1340 #elif defined (CONFIG_SCTP_DEFAULT_COOKIE_HMAC_SHA1)
1341 net->sctp.sctp_hmac_alg = "sha1";
1342 #else
1343 net->sctp.sctp_hmac_alg = NULL;
1344 #endif
1345
1346 /* Max.Burst - 4 */
1347 net->sctp.max_burst = SCTP_DEFAULT_MAX_BURST;
1348
1349 /* Disable of Primary Path Switchover by default */
1350 net->sctp.ps_retrans = SCTP_PS_RETRANS_MAX;
1351
1352 /* Enable pf state by default */
1353 net->sctp.pf_enable = 1;
1354
1355 /* Ignore pf exposure feature by default */
1356 net->sctp.pf_expose = SCTP_PF_EXPOSE_UNSET;
1357
1358 /* Association.Max.Retrans - 10 attempts
1359 * Path.Max.Retrans - 5 attempts (per destination address)
1360 * Max.Init.Retransmits - 8 attempts
1361 */
1362 net->sctp.max_retrans_association = 10;
1363 net->sctp.max_retrans_path = 5;
1364 net->sctp.max_retrans_init = 8;
1365
1366 /* Sendbuffer growth - do per-socket accounting */
1367 net->sctp.sndbuf_policy = 0;
1368
1369 /* Rcvbuffer growth - do per-socket accounting */
1370 net->sctp.rcvbuf_policy = 0;
1371
1372 /* HB.interval - 30 seconds */
1373 net->sctp.hb_interval = SCTP_DEFAULT_TIMEOUT_HEARTBEAT;
1374
1375 /* delayed SACK timeout */
1376 net->sctp.sack_timeout = SCTP_DEFAULT_TIMEOUT_SACK;
1377
1378 /* Disable ADDIP by default. */
1379 net->sctp.addip_enable = 0;
1380 net->sctp.addip_noauth = 0;
1381 net->sctp.default_auto_asconf = 0;
1382
1383 /* Enable PR-SCTP by default. */
1384 net->sctp.prsctp_enable = 1;
1385
1386 /* Disable RECONF by default. */
1387 net->sctp.reconf_enable = 0;
1388
1389 /* Disable AUTH by default. */
1390 net->sctp.auth_enable = 0;
1391
1392 /* Enable ECN by default. */
1393 net->sctp.ecn_enable = 1;
1394
1395 /* Set UDP tunneling listening port to 0 by default */
1396 net->sctp.udp_port = 0;
1397
1398 /* Set remote encap port to 0 by default */
1399 net->sctp.encap_port = 0;
1400
1401 /* Set SCOPE policy to enabled */
1402 net->sctp.scope_policy = SCTP_SCOPE_POLICY_ENABLE;
1403
1404 /* Set the default rwnd update threshold */
1405 net->sctp.rwnd_upd_shift = SCTP_DEFAULT_RWND_SHIFT;
1406
1407 /* Initialize maximum autoclose timeout. */
1408 net->sctp.max_autoclose = INT_MAX / HZ;
1409
1410 #ifdef CONFIG_NET_L3_MASTER_DEV
1411 net->sctp.l3mdev_accept = 1;
1412 #endif
1413
1414 status = sctp_sysctl_net_register(net);
1415 if (status)
1416 goto err_sysctl_register;
1417
1418 /* Allocate and initialise sctp mibs. */
1419 status = init_sctp_mibs(net);
1420 if (status)
1421 goto err_init_mibs;
1422
1423 #ifdef CONFIG_PROC_FS
1424 /* Initialize proc fs directory. */
1425 status = sctp_proc_init(net);
1426 if (status)
1427 goto err_init_proc;
1428 #endif
1429
1430 sctp_dbg_objcnt_init(net);
1431
1432 /* Initialize the local address list. */
1433 INIT_LIST_HEAD(&net->sctp.local_addr_list);
1434 spin_lock_init(&net->sctp.local_addr_lock);
1435 sctp_get_local_addr_list(net);
1436
1437 /* Initialize the address event list */
1438 INIT_LIST_HEAD(&net->sctp.addr_waitq);
1439 INIT_LIST_HEAD(&net->sctp.auto_asconf_splist);
1440 spin_lock_init(&net->sctp.addr_wq_lock);
1441 net->sctp.addr_wq_timer.expires = 0;
1442 timer_setup(&net->sctp.addr_wq_timer, sctp_addr_wq_timeout_handler, 0);
1443
1444 return 0;
1445
1446 #ifdef CONFIG_PROC_FS
1447 err_init_proc:
1448 cleanup_sctp_mibs(net);
1449 #endif
1450 err_init_mibs:
1451 sctp_sysctl_net_unregister(net);
1452 err_sysctl_register:
1453 return status;
1454 }
1455
sctp_defaults_exit(struct net * net)1456 static void __net_exit sctp_defaults_exit(struct net *net)
1457 {
1458 /* Free the local address list */
1459 sctp_free_addr_wq(net);
1460 sctp_free_local_addr_list(net);
1461
1462 #ifdef CONFIG_PROC_FS
1463 remove_proc_subtree("sctp", net->proc_net);
1464 net->sctp.proc_net_sctp = NULL;
1465 #endif
1466 cleanup_sctp_mibs(net);
1467 sctp_sysctl_net_unregister(net);
1468 }
1469
1470 static struct pernet_operations sctp_defaults_ops = {
1471 .init = sctp_defaults_init,
1472 .exit = sctp_defaults_exit,
1473 };
1474
sctp_ctrlsock_init(struct net * net)1475 static int __net_init sctp_ctrlsock_init(struct net *net)
1476 {
1477 int status;
1478
1479 /* Initialize the control inode/socket for handling OOTB packets. */
1480 status = sctp_ctl_sock_init(net);
1481 if (status)
1482 pr_err("Failed to initialize the SCTP control sock\n");
1483
1484 return status;
1485 }
1486
sctp_ctrlsock_exit(struct net * net)1487 static void __net_exit sctp_ctrlsock_exit(struct net *net)
1488 {
1489 /* Free the control endpoint. */
1490 inet_ctl_sock_destroy(net->sctp.ctl_sock);
1491 }
1492
1493 static struct pernet_operations sctp_ctrlsock_ops = {
1494 .init = sctp_ctrlsock_init,
1495 .exit = sctp_ctrlsock_exit,
1496 };
1497
1498 /* Initialize the universe into something sensible. */
sctp_init(void)1499 static __init int sctp_init(void)
1500 {
1501 unsigned long nr_pages = totalram_pages();
1502 unsigned long limit;
1503 unsigned long goal;
1504 int max_entry_order;
1505 int num_entries;
1506 int max_share;
1507 int status;
1508 int order;
1509 int i;
1510
1511 sock_skb_cb_check_size(sizeof(struct sctp_ulpevent));
1512
1513 /* Allocate bind_bucket and chunk caches. */
1514 status = -ENOBUFS;
1515 sctp_bucket_cachep = KMEM_CACHE(sctp_bind_bucket, SLAB_HWCACHE_ALIGN);
1516 if (!sctp_bucket_cachep)
1517 goto out;
1518
1519 sctp_chunk_cachep = KMEM_CACHE(sctp_chunk, SLAB_HWCACHE_ALIGN);
1520 if (!sctp_chunk_cachep)
1521 goto err_chunk_cachep;
1522
1523 status = percpu_counter_init(&sctp_sockets_allocated, 0, GFP_KERNEL);
1524 if (status)
1525 goto err_percpu_counter_init;
1526
1527 /* Implementation specific variables. */
1528
1529 /* Initialize default stream count setup information. */
1530 sctp_max_instreams = SCTP_DEFAULT_INSTREAMS;
1531 sctp_max_outstreams = SCTP_DEFAULT_OUTSTREAMS;
1532
1533 /* Initialize handle used for association ids. */
1534 idr_init(&sctp_assocs_id);
1535
1536 limit = nr_free_buffer_pages() / 8;
1537 limit = max(limit, 128UL);
1538 sysctl_sctp_mem[0] = limit / 4 * 3;
1539 sysctl_sctp_mem[1] = limit;
1540 sysctl_sctp_mem[2] = sysctl_sctp_mem[0] * 2;
1541
1542 /* Set per-socket limits to no more than 1/128 the pressure threshold*/
1543 limit = (sysctl_sctp_mem[1]) << (PAGE_SHIFT - 7);
1544 max_share = min(4UL*1024*1024, limit);
1545
1546 sysctl_sctp_rmem[0] = PAGE_SIZE; /* give each asoc 1 page min */
1547 sysctl_sctp_rmem[1] = 1500 * SKB_TRUESIZE(1);
1548 sysctl_sctp_rmem[2] = max(sysctl_sctp_rmem[1], max_share);
1549
1550 sysctl_sctp_wmem[0] = PAGE_SIZE;
1551 sysctl_sctp_wmem[1] = 16*1024;
1552 sysctl_sctp_wmem[2] = max(64*1024, max_share);
1553
1554 /* Size and allocate the association hash table.
1555 * The methodology is similar to that of the tcp hash tables.
1556 * Though not identical. Start by getting a goal size
1557 */
1558 if (nr_pages >= (128 * 1024))
1559 goal = nr_pages >> (22 - PAGE_SHIFT);
1560 else
1561 goal = nr_pages >> (24 - PAGE_SHIFT);
1562
1563 /* Then compute the page order for said goal */
1564 order = get_order(goal);
1565
1566 /* Now compute the required page order for the maximum sized table we
1567 * want to create
1568 */
1569 max_entry_order = get_order(MAX_SCTP_PORT_HASH_ENTRIES *
1570 sizeof(struct sctp_bind_hashbucket));
1571
1572 /* Limit the page order by that maximum hash table size */
1573 order = min(order, max_entry_order);
1574
1575 /* Allocate and initialize the endpoint hash table. */
1576 sctp_ep_hashsize = 64;
1577 sctp_ep_hashtable =
1578 kmalloc_array(64, sizeof(struct sctp_hashbucket), GFP_KERNEL);
1579 if (!sctp_ep_hashtable) {
1580 pr_err("Failed endpoint_hash alloc\n");
1581 status = -ENOMEM;
1582 goto err_ehash_alloc;
1583 }
1584 for (i = 0; i < sctp_ep_hashsize; i++) {
1585 rwlock_init(&sctp_ep_hashtable[i].lock);
1586 INIT_HLIST_HEAD(&sctp_ep_hashtable[i].chain);
1587 }
1588
1589 /* Allocate and initialize the SCTP port hash table.
1590 * Note that order is initalized to start at the max sized
1591 * table we want to support. If we can't get that many pages
1592 * reduce the order and try again
1593 */
1594 do {
1595 sctp_port_hashtable = (struct sctp_bind_hashbucket *)
1596 __get_free_pages(GFP_KERNEL | __GFP_NOWARN, order);
1597 } while (!sctp_port_hashtable && --order > 0);
1598
1599 if (!sctp_port_hashtable) {
1600 pr_err("Failed bind hash alloc\n");
1601 status = -ENOMEM;
1602 goto err_bhash_alloc;
1603 }
1604
1605 /* Now compute the number of entries that will fit in the
1606 * port hash space we allocated
1607 */
1608 num_entries = (1UL << order) * PAGE_SIZE /
1609 sizeof(struct sctp_bind_hashbucket);
1610
1611 /* And finish by rounding it down to the nearest power of two.
1612 * This wastes some memory of course, but it's needed because
1613 * the hash function operates based on the assumption that
1614 * the number of entries is a power of two.
1615 */
1616 sctp_port_hashsize = rounddown_pow_of_two(num_entries);
1617
1618 for (i = 0; i < sctp_port_hashsize; i++) {
1619 spin_lock_init(&sctp_port_hashtable[i].lock);
1620 INIT_HLIST_HEAD(&sctp_port_hashtable[i].chain);
1621 }
1622
1623 status = sctp_transport_hashtable_init();
1624 if (status)
1625 goto err_thash_alloc;
1626
1627 pr_info("Hash tables configured (bind %d/%d)\n", sctp_port_hashsize,
1628 num_entries);
1629
1630 sctp_sysctl_register();
1631
1632 INIT_LIST_HEAD(&sctp_address_families);
1633 sctp_v4_pf_init();
1634 sctp_v6_pf_init();
1635 sctp_sched_ops_init();
1636
1637 status = register_pernet_subsys(&sctp_defaults_ops);
1638 if (status)
1639 goto err_register_defaults;
1640
1641 status = sctp_v4_protosw_init();
1642 if (status)
1643 goto err_protosw_init;
1644
1645 status = sctp_v6_protosw_init();
1646 if (status)
1647 goto err_v6_protosw_init;
1648
1649 status = register_pernet_subsys(&sctp_ctrlsock_ops);
1650 if (status)
1651 goto err_register_ctrlsock;
1652
1653 status = sctp_v4_add_protocol();
1654 if (status)
1655 goto err_add_protocol;
1656
1657 /* Register SCTP with inet6 layer. */
1658 status = sctp_v6_add_protocol();
1659 if (status)
1660 goto err_v6_add_protocol;
1661
1662 if (sctp_offload_init() < 0)
1663 pr_crit("%s: Cannot add SCTP protocol offload\n", __func__);
1664
1665 out:
1666 return status;
1667 err_v6_add_protocol:
1668 sctp_v4_del_protocol();
1669 err_add_protocol:
1670 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1671 err_register_ctrlsock:
1672 sctp_v6_protosw_exit();
1673 err_v6_protosw_init:
1674 sctp_v4_protosw_exit();
1675 err_protosw_init:
1676 unregister_pernet_subsys(&sctp_defaults_ops);
1677 err_register_defaults:
1678 sctp_v4_pf_exit();
1679 sctp_v6_pf_exit();
1680 sctp_sysctl_unregister();
1681 free_pages((unsigned long)sctp_port_hashtable,
1682 get_order(sctp_port_hashsize *
1683 sizeof(struct sctp_bind_hashbucket)));
1684 err_bhash_alloc:
1685 sctp_transport_hashtable_destroy();
1686 err_thash_alloc:
1687 kfree(sctp_ep_hashtable);
1688 err_ehash_alloc:
1689 percpu_counter_destroy(&sctp_sockets_allocated);
1690 err_percpu_counter_init:
1691 kmem_cache_destroy(sctp_chunk_cachep);
1692 err_chunk_cachep:
1693 kmem_cache_destroy(sctp_bucket_cachep);
1694 goto out;
1695 }
1696
1697 /* Exit handler for the SCTP protocol. */
sctp_exit(void)1698 static __exit void sctp_exit(void)
1699 {
1700 /* BUG. This should probably do something useful like clean
1701 * up all the remaining associations and all that memory.
1702 */
1703
1704 /* Unregister with inet6/inet layers. */
1705 sctp_v6_del_protocol();
1706 sctp_v4_del_protocol();
1707
1708 unregister_pernet_subsys(&sctp_ctrlsock_ops);
1709
1710 /* Free protosw registrations */
1711 sctp_v6_protosw_exit();
1712 sctp_v4_protosw_exit();
1713
1714 unregister_pernet_subsys(&sctp_defaults_ops);
1715
1716 /* Unregister with socket layer. */
1717 sctp_v6_pf_exit();
1718 sctp_v4_pf_exit();
1719
1720 sctp_sysctl_unregister();
1721
1722 free_pages((unsigned long)sctp_port_hashtable,
1723 get_order(sctp_port_hashsize *
1724 sizeof(struct sctp_bind_hashbucket)));
1725 kfree(sctp_ep_hashtable);
1726 sctp_transport_hashtable_destroy();
1727
1728 percpu_counter_destroy(&sctp_sockets_allocated);
1729
1730 rcu_barrier(); /* Wait for completion of call_rcu()'s */
1731
1732 kmem_cache_destroy(sctp_chunk_cachep);
1733 kmem_cache_destroy(sctp_bucket_cachep);
1734 }
1735
1736 module_init(sctp_init);
1737 module_exit(sctp_exit);
1738
1739 /*
1740 * __stringify doesn't likes enums, so use IPPROTO_SCTP value (132) directly.
1741 */
1742 MODULE_ALIAS("net-pf-" __stringify(PF_INET) "-proto-132");
1743 MODULE_ALIAS("net-pf-" __stringify(PF_INET6) "-proto-132");
1744 MODULE_AUTHOR("Linux Kernel SCTP developers <linux-sctp@vger.kernel.org>");
1745 MODULE_DESCRIPTION("Support for the SCTP protocol (RFC2960)");
1746 module_param_named(no_checksums, sctp_checksum_disable, bool, 0644);
1747 MODULE_PARM_DESC(no_checksums, "Disable checksums computing and verification");
1748 MODULE_LICENSE("GPL");
1749