1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49#ifndef __net_sctp_h__
50#define __net_sctp_h__
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65#include <linux/types.h>
66#include <linux/slab.h>
67#include <linux/in.h>
68#include <linux/tty.h>
69#include <linux/proc_fs.h>
70#include <linux/spinlock.h>
71#include <linux/jiffies.h>
72#include <linux/idr.h>
73
74#if IS_ENABLED(CONFIG_IPV6)
75#include <net/ipv6.h>
76#include <net/ip6_route.h>
77#endif
78
79#include <asm/uaccess.h>
80#include <asm/page.h>
81#include <net/sock.h>
82#include <net/snmp.h>
83#include <net/sctp/structs.h>
84#include <net/sctp/constants.h>
85
86#ifdef CONFIG_IP_SCTP_MODULE
87#define SCTP_PROTOSW_FLAG 0
88#else
89#define SCTP_PROTOSW_FLAG INET_PROTOSW_PERMANENT
90#endif
91
92
93
94
95
96
97
98
99extern int sctp_copy_local_addr_list(struct net *, struct sctp_bind_addr *,
100 sctp_scope_t, gfp_t gfp,
101 int flags);
102extern struct sctp_pf *sctp_get_pf_specific(sa_family_t family);
103extern int sctp_register_pf(struct sctp_pf *, sa_family_t);
104extern void sctp_addr_wq_mgmt(struct net *, struct sctp_sockaddr_entry *, int);
105
106
107
108
109int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb);
110int sctp_inet_listen(struct socket *sock, int backlog);
111void sctp_write_space(struct sock *sk);
112void sctp_data_ready(struct sock *sk, int len);
113unsigned int sctp_poll(struct file *file, struct socket *sock,
114 poll_table *wait);
115void sctp_sock_rfree(struct sk_buff *skb);
116void sctp_copy_sock(struct sock *newsk, struct sock *sk,
117 struct sctp_association *asoc);
118extern struct percpu_counter sctp_sockets_allocated;
119extern int sctp_asconf_mgmt(struct sctp_sock *, struct sctp_sockaddr_entry *);
120
121
122
123
124int sctp_primitive_ASSOCIATE(struct net *, struct sctp_association *, void *arg);
125int sctp_primitive_SHUTDOWN(struct net *, struct sctp_association *, void *arg);
126int sctp_primitive_ABORT(struct net *, struct sctp_association *, void *arg);
127int sctp_primitive_SEND(struct net *, struct sctp_association *, void *arg);
128int sctp_primitive_REQUESTHEARTBEAT(struct net *, struct sctp_association *, void *arg);
129int sctp_primitive_ASCONF(struct net *, struct sctp_association *, void *arg);
130
131
132
133
134int sctp_rcv(struct sk_buff *skb);
135void sctp_v4_err(struct sk_buff *skb, u32 info);
136void sctp_hash_established(struct sctp_association *);
137void sctp_unhash_established(struct sctp_association *);
138void sctp_hash_endpoint(struct sctp_endpoint *);
139void sctp_unhash_endpoint(struct sctp_endpoint *);
140struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *,
141 struct sctphdr *, struct sctp_association **,
142 struct sctp_transport **);
143void sctp_err_finish(struct sock *, struct sctp_association *);
144void sctp_icmp_frag_needed(struct sock *, struct sctp_association *,
145 struct sctp_transport *t, __u32 pmtu);
146void sctp_icmp_redirect(struct sock *, struct sctp_transport *,
147 struct sk_buff *);
148void sctp_icmp_proto_unreachable(struct sock *sk,
149 struct sctp_association *asoc,
150 struct sctp_transport *t);
151void sctp_backlog_migrate(struct sctp_association *assoc,
152 struct sock *oldsk, struct sock *newsk);
153
154
155
156
157int sctp_snmp_proc_init(struct net *net);
158void sctp_snmp_proc_exit(struct net *net);
159int sctp_eps_proc_init(struct net *net);
160void sctp_eps_proc_exit(struct net *net);
161int sctp_assocs_proc_init(struct net *net);
162void sctp_assocs_proc_exit(struct net *net);
163int sctp_remaddr_proc_init(struct net *net);
164void sctp_remaddr_proc_exit(struct net *net);
165
166
167
168
169
170
171
172
173
174extern struct kmem_cache *sctp_chunk_cachep __read_mostly;
175extern struct kmem_cache *sctp_bucket_cachep __read_mostly;
176
177
178
179
180
181
182#define sctp_spin_lock_irqsave(lock, flags) spin_lock_irqsave(lock, flags)
183#define sctp_spin_unlock_irqrestore(lock, flags) \
184 spin_unlock_irqrestore(lock, flags)
185#define sctp_local_bh_disable() local_bh_disable()
186#define sctp_local_bh_enable() local_bh_enable()
187#define sctp_spin_lock(lock) spin_lock(lock)
188#define sctp_spin_unlock(lock) spin_unlock(lock)
189#define sctp_write_lock(lock) write_lock(lock)
190#define sctp_write_unlock(lock) write_unlock(lock)
191#define sctp_read_lock(lock) read_lock(lock)
192#define sctp_read_unlock(lock) read_unlock(lock)
193
194
195#define sctp_lock_sock(sk) lock_sock(sk)
196#define sctp_release_sock(sk) release_sock(sk)
197#define sctp_bh_lock_sock(sk) bh_lock_sock(sk)
198#define sctp_bh_unlock_sock(sk) bh_unlock_sock(sk)
199
200
201#define SCTP_INC_STATS(net, field) SNMP_INC_STATS((net)->sctp.sctp_statistics, field)
202#define SCTP_INC_STATS_BH(net, field) SNMP_INC_STATS_BH((net)->sctp.sctp_statistics, field)
203#define SCTP_INC_STATS_USER(net, field) SNMP_INC_STATS_USER((net)->sctp.sctp_statistics, field)
204#define SCTP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->sctp.sctp_statistics, field)
205
206
207enum {
208 SCTP_MIB_NUM = 0,
209 SCTP_MIB_CURRESTAB,
210 SCTP_MIB_ACTIVEESTABS,
211 SCTP_MIB_PASSIVEESTABS,
212 SCTP_MIB_ABORTEDS,
213 SCTP_MIB_SHUTDOWNS,
214 SCTP_MIB_OUTOFBLUES,
215 SCTP_MIB_CHECKSUMERRORS,
216 SCTP_MIB_OUTCTRLCHUNKS,
217 SCTP_MIB_OUTORDERCHUNKS,
218 SCTP_MIB_OUTUNORDERCHUNKS,
219 SCTP_MIB_INCTRLCHUNKS,
220 SCTP_MIB_INORDERCHUNKS,
221 SCTP_MIB_INUNORDERCHUNKS,
222 SCTP_MIB_FRAGUSRMSGS,
223 SCTP_MIB_REASMUSRMSGS,
224 SCTP_MIB_OUTSCTPPACKS,
225 SCTP_MIB_INSCTPPACKS,
226 SCTP_MIB_T1_INIT_EXPIREDS,
227 SCTP_MIB_T1_COOKIE_EXPIREDS,
228 SCTP_MIB_T2_SHUTDOWN_EXPIREDS,
229 SCTP_MIB_T3_RTX_EXPIREDS,
230 SCTP_MIB_T4_RTO_EXPIREDS,
231 SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS,
232 SCTP_MIB_DELAY_SACK_EXPIREDS,
233 SCTP_MIB_AUTOCLOSE_EXPIREDS,
234 SCTP_MIB_T1_RETRANSMITS,
235 SCTP_MIB_T3_RETRANSMITS,
236 SCTP_MIB_PMTUD_RETRANSMITS,
237 SCTP_MIB_FAST_RETRANSMITS,
238 SCTP_MIB_IN_PKT_SOFTIRQ,
239 SCTP_MIB_IN_PKT_BACKLOG,
240 SCTP_MIB_IN_PKT_DISCARDS,
241 SCTP_MIB_IN_DATA_CHUNK_DISCARDS,
242 __SCTP_MIB_MAX
243};
244
245#define SCTP_MIB_MAX __SCTP_MIB_MAX
246struct sctp_mib {
247 unsigned long mibs[SCTP_MIB_MAX];
248};
249
250
251static inline void sctp_max_rto(struct sctp_association *asoc,
252 struct sctp_transport *trans)
253{
254 if (asoc->stats.max_obs_rto < (__u64)trans->rto) {
255 asoc->stats.max_obs_rto = trans->rto;
256 memset(&asoc->stats.obs_rto_ipaddr, 0,
257 sizeof(struct sockaddr_storage));
258 memcpy(&asoc->stats.obs_rto_ipaddr, &trans->ipaddr,
259 trans->af_specific->sockaddr_len);
260 }
261}
262
263
264
265
266#ifdef CONFIG_SCTP_DBG_OBJCNT
267
268extern atomic_t sctp_dbg_objcnt_sock;
269extern atomic_t sctp_dbg_objcnt_ep;
270extern atomic_t sctp_dbg_objcnt_assoc;
271extern atomic_t sctp_dbg_objcnt_transport;
272extern atomic_t sctp_dbg_objcnt_chunk;
273extern atomic_t sctp_dbg_objcnt_bind_addr;
274extern atomic_t sctp_dbg_objcnt_bind_bucket;
275extern atomic_t sctp_dbg_objcnt_addr;
276extern atomic_t sctp_dbg_objcnt_ssnmap;
277extern atomic_t sctp_dbg_objcnt_datamsg;
278extern atomic_t sctp_dbg_objcnt_keys;
279
280
281#define SCTP_DBG_OBJCNT_INC(name) \
282atomic_inc(&sctp_dbg_objcnt_## name)
283#define SCTP_DBG_OBJCNT_DEC(name) \
284atomic_dec(&sctp_dbg_objcnt_## name)
285#define SCTP_DBG_OBJCNT(name) \
286atomic_t sctp_dbg_objcnt_## name = ATOMIC_INIT(0)
287
288
289
290
291#define SCTP_DBG_OBJCNT_ENTRY(name) \
292{.label= #name, .counter= &sctp_dbg_objcnt_## name}
293
294void sctp_dbg_objcnt_init(struct net *);
295void sctp_dbg_objcnt_exit(struct net *);
296
297#else
298
299#define SCTP_DBG_OBJCNT_INC(name)
300#define SCTP_DBG_OBJCNT_DEC(name)
301
302static inline void sctp_dbg_objcnt_init(struct net *net) { return; }
303static inline void sctp_dbg_objcnt_exit(struct net *net) { return; }
304
305#endif
306
307#if defined CONFIG_SYSCTL
308void sctp_sysctl_register(void);
309void sctp_sysctl_unregister(void);
310int sctp_sysctl_net_register(struct net *net);
311void sctp_sysctl_net_unregister(struct net *net);
312#else
313static inline void sctp_sysctl_register(void) { return; }
314static inline void sctp_sysctl_unregister(void) { return; }
315static inline int sctp_sysctl_net_register(struct net *net) { return 0; }
316static inline void sctp_sysctl_net_unregister(struct net *net) { return; }
317#endif
318
319
320#define SCTP_SAT_LEN(x) (sizeof(struct sctp_paramhdr) + (x) * sizeof(__u16))
321
322#if IS_ENABLED(CONFIG_IPV6)
323
324void sctp_v6_pf_init(void);
325void sctp_v6_pf_exit(void);
326int sctp_v6_protosw_init(void);
327void sctp_v6_protosw_exit(void);
328int sctp_v6_add_protocol(void);
329void sctp_v6_del_protocol(void);
330
331#else
332
333static inline void sctp_v6_pf_init(void) { return; }
334static inline void sctp_v6_pf_exit(void) { return; }
335static inline int sctp_v6_protosw_init(void) { return 0; }
336static inline void sctp_v6_protosw_exit(void) { return; }
337static inline int sctp_v6_add_protocol(void) { return 0; }
338static inline void sctp_v6_del_protocol(void) { return; }
339
340#endif
341
342
343
344static inline sctp_assoc_t sctp_assoc2id(const struct sctp_association *asoc)
345{
346 return asoc ? asoc->assoc_id : 0;
347}
348
349
350struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id);
351
352int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp);
353
354
355#define sctp_skb_for_each(pos, head, tmp) \
356 skb_queue_walk_safe(head, pos, tmp)
357
358
359static inline void sctp_skb_list_tail(struct sk_buff_head *list,
360 struct sk_buff_head *head)
361{
362 unsigned long flags;
363
364 sctp_spin_lock_irqsave(&head->lock, flags);
365 sctp_spin_lock(&list->lock);
366
367 skb_queue_splice_tail_init(list, head);
368
369 sctp_spin_unlock(&list->lock);
370 sctp_spin_unlock_irqrestore(&head->lock, flags);
371}
372
373
374
375
376
377
378
379
380
381static inline struct list_head *sctp_list_dequeue(struct list_head *list)
382{
383 struct list_head *result = NULL;
384
385 if (list->next != list) {
386 result = list->next;
387 list->next = result->next;
388 list->next->prev = list;
389 INIT_LIST_HEAD(result);
390 }
391 return result;
392}
393
394
395
396
397
398static inline void sctp_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
399{
400 struct sctp_ulpevent *event = sctp_skb2event(skb);
401
402 skb_orphan(skb);
403 skb->sk = sk;
404 skb->destructor = sctp_sock_rfree;
405 atomic_add(event->rmem_len, &sk->sk_rmem_alloc);
406
407
408
409 sk->sk_forward_alloc -= event->rmem_len;
410}
411
412
413static inline int sctp_list_single_entry(struct list_head *head)
414{
415 return (head->next != head) && (head->next == head->prev);
416}
417
418
419static inline __s32 sctp_jitter(__u32 rto)
420{
421 static __u32 sctp_rand;
422 __s32 ret;
423
424
425 if (!rto)
426 rto = 1;
427
428 sctp_rand += jiffies;
429 sctp_rand ^= (sctp_rand << 12);
430 sctp_rand ^= (sctp_rand >> 20);
431
432
433
434
435 ret = sctp_rand % rto - (rto >> 1);
436 return ret;
437}
438
439
440static inline int sctp_frag_point(const struct sctp_association *asoc, int pmtu)
441{
442 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
443 int frag = pmtu;
444
445 frag -= sp->pf->af->net_header_len;
446 frag -= sizeof(struct sctphdr) + sizeof(struct sctp_data_chunk);
447
448 if (asoc->user_frag)
449 frag = min_t(int, frag, asoc->user_frag);
450
451 frag = min_t(int, frag, SCTP_MAX_CHUNK_LEN);
452
453 return frag;
454}
455
456static inline void sctp_assoc_pending_pmtu(struct sock *sk, struct sctp_association *asoc)
457{
458
459 sctp_assoc_sync_pmtu(sk, asoc);
460 asoc->pmtu_pending = 0;
461}
462
463
464
465
466
467
468#define sctp_walk_params(pos, chunk, member)\
469_sctp_walk_params((pos), (chunk), ntohs((chunk)->chunk_hdr.length), member)
470
471#define _sctp_walk_params(pos, chunk, end, member)\
472for (pos.v = chunk->member;\
473 pos.v <= (void *)chunk + end - ntohs(pos.p->length) &&\
474 ntohs(pos.p->length) >= sizeof(sctp_paramhdr_t);\
475 pos.v += WORD_ROUND(ntohs(pos.p->length)))
476
477#define sctp_walk_errors(err, chunk_hdr)\
478_sctp_walk_errors((err), (chunk_hdr), ntohs((chunk_hdr)->length))
479
480#define _sctp_walk_errors(err, chunk_hdr, end)\
481for (err = (sctp_errhdr_t *)((void *)chunk_hdr + \
482 sizeof(sctp_chunkhdr_t));\
483 (void *)err <= (void *)chunk_hdr + end - ntohs(err->length) &&\
484 ntohs(err->length) >= sizeof(sctp_errhdr_t); \
485 err = (sctp_errhdr_t *)((void *)err + WORD_ROUND(ntohs(err->length))))
486
487#define sctp_walk_fwdtsn(pos, chunk)\
488_sctp_walk_fwdtsn((pos), (chunk), ntohs((chunk)->chunk_hdr->length) - sizeof(struct sctp_fwdtsn_chunk))
489
490#define _sctp_walk_fwdtsn(pos, chunk, end)\
491for (pos = chunk->subh.fwdtsn_hdr->skip;\
492 (void *)pos <= (void *)chunk->subh.fwdtsn_hdr->skip + end - sizeof(struct sctp_fwdtsn_skip);\
493 pos++)
494
495
496#define WORD_ROUND(s) (((s)+3)&~3)
497
498
499
500extern struct proto sctp_prot;
501extern struct proto sctpv6_prot;
502void sctp_put_port(struct sock *sk);
503
504extern struct idr sctp_assocs_id;
505extern spinlock_t sctp_assocs_id_lock;
506
507
508
509
510static inline int ipver2af(__u8 ipver)
511{
512 switch (ipver) {
513 case 4:
514 return AF_INET;
515 case 6:
516 return AF_INET6;
517 default:
518 return 0;
519 }
520}
521
522
523static inline int param_type2af(__be16 type)
524{
525 switch (type) {
526 case SCTP_PARAM_IPV4_ADDRESS:
527 return AF_INET;
528 case SCTP_PARAM_IPV6_ADDRESS:
529 return AF_INET6;
530 default:
531 return 0;
532 }
533}
534
535
536
537static inline int sctp_phashfn(struct net *net, __u16 lport)
538{
539 return (net_hash_mix(net) + lport) & (sctp_port_hashsize - 1);
540}
541
542
543static inline int sctp_ep_hashfn(struct net *net, __u16 lport)
544{
545 return (net_hash_mix(net) + lport) & (sctp_ep_hashsize - 1);
546}
547
548
549static inline int sctp_assoc_hashfn(struct net *net, __u16 lport, __u16 rport)
550{
551 int h = (lport << 16) + rport + net_hash_mix(net);
552 h ^= h>>8;
553 return h & (sctp_assoc_hashsize - 1);
554}
555
556
557
558
559
560static inline int sctp_vtag_hashfn(__u16 lport, __u16 rport, __u32 vtag)
561{
562 int h = (lport << 16) + rport;
563 h ^= vtag;
564 return h & (sctp_assoc_hashsize - 1);
565}
566
567#define sctp_for_each_hentry(epb, head) \
568 hlist_for_each_entry(epb, head, node)
569
570
571#define sctp_style(sk, style) __sctp_style((sk), (SCTP_SOCKET_##style))
572static inline int __sctp_style(const struct sock *sk, sctp_socket_type_t style)
573{
574 return sctp_sk(sk)->type == style;
575}
576
577
578#define sctp_state(asoc, state) __sctp_state((asoc), (SCTP_STATE_##state))
579static inline int __sctp_state(const struct sctp_association *asoc,
580 sctp_state_t state)
581{
582 return asoc->state == state;
583}
584
585
586#define sctp_sstate(sk, state) __sctp_sstate((sk), (SCTP_SS_##state))
587static inline int __sctp_sstate(const struct sock *sk, sctp_sock_state_t state)
588{
589 return sk->sk_state == state;
590}
591
592
593static inline void sctp_v6_map_v4(union sctp_addr *addr)
594{
595 addr->v4.sin_family = AF_INET;
596 addr->v4.sin_port = addr->v6.sin6_port;
597 addr->v4.sin_addr.s_addr = addr->v6.sin6_addr.s6_addr32[3];
598}
599
600
601static inline void sctp_v4_map_v6(union sctp_addr *addr)
602{
603 addr->v6.sin6_family = AF_INET6;
604 addr->v6.sin6_port = addr->v4.sin_port;
605 addr->v6.sin6_addr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
606 addr->v6.sin6_addr.s6_addr32[0] = 0;
607 addr->v6.sin6_addr.s6_addr32[1] = 0;
608 addr->v6.sin6_addr.s6_addr32[2] = htonl(0x0000ffff);
609}
610
611
612
613
614static inline struct dst_entry *sctp_transport_dst_check(struct sctp_transport *t)
615{
616 if (t->dst && !dst_check(t->dst, 0)) {
617 dst_release(t->dst);
618 t->dst = NULL;
619 }
620
621 return t->dst;
622}
623
624#endif
625