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