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