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-2003 Intel Corp.
7 * This file is part of the SCTP kernel implementation
9 * The base lksctp header.
11 * This SCTP implementation is free software;
12 * you can redistribute it and/or modify it under the terms of
13 * the GNU General Public License as published by
14 * the Free Software Foundation; either version 2, or (at your option)
17 * This SCTP implementation is distributed in the hope that it
18 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
19 * ************************
20 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 * See the GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with GNU CC; see the file COPYING. If not, write to
25 * the Free Software Foundation, 59 Temple Place - Suite 330,
26 * Boston, MA 02111-1307, USA.
28 * Please send any bug reports or fixes you make to the
30 * lksctp developers <lksctp-developers@lists.sourceforge.net>
32 * Or submit a bug report through the following website:
33 * http://www.sf.net/projects/lksctp
35 * Written or modified by:
36 * La Monte H.P. Yarroll <piggy@acm.org>
37 * Xingang Guo <xingang.guo@intel.com>
38 * Jon Grimm <jgrimm@us.ibm.com>
39 * Daisy Chang <daisyc@us.ibm.com>
40 * Sridhar Samudrala <sri@us.ibm.com>
41 * Ardelle Fan <ardelle.fan@intel.com>
42 * Ryan Layer <rmlayer@us.ibm.com>
43 * Kevin Gao <kevin.gao@intel.com>
45 * Any bugs reported given to us we will try to fix... any fixes shared will
46 * be incorporated into the next SCTP release.
49 #ifndef __net_sctp_h__
50 #define __net_sctp_h__
53 * Start getting some control over the header file depencies:
58 * macros, externs, and inlines
60 * Move test_frame specific items out of the kernel headers
61 * and into the test frame headers. This is not perfect in any sense
62 * and will continue to evolve.
65 #include <linux/types.h>
66 #include <linux/slab.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>
74 #if IS_ENABLED(CONFIG_IPV6)
76 #include <net/ip6_route.h>
79 #include <asm/uaccess.h>
83 #include <net/sctp/structs.h>
84 #include <net/sctp/constants.h>
86 #ifdef CONFIG_IP_SCTP_MODULE
87 #define SCTP_PROTOSW_FLAG 0
89 #define SCTP_PROTOSW_FLAG INET_PROTOSW_PERMANENT
93 * Function declarations.
99 extern int sctp_copy_local_addr_list(struct net *, struct sctp_bind_addr *,
100 sctp_scope_t, gfp_t gfp,
102 extern struct sctp_pf *sctp_get_pf_specific(sa_family_t family);
103 extern int sctp_register_pf(struct sctp_pf *, sa_family_t);
104 extern void sctp_addr_wq_mgmt(struct net *, struct sctp_sockaddr_entry *, int);
109 int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb);
110 int sctp_inet_listen(struct socket *sock, int backlog);
111 void sctp_write_space(struct sock *sk);
112 void sctp_data_ready(struct sock *sk, int len);
113 unsigned int sctp_poll(struct file *file, struct socket *sock,
115 void sctp_sock_rfree(struct sk_buff *skb);
116 void sctp_copy_sock(struct sock *newsk, struct sock *sk,
117 struct sctp_association *asoc);
118 extern struct percpu_counter sctp_sockets_allocated;
119 extern int sctp_asconf_mgmt(struct sctp_sock *, struct sctp_sockaddr_entry *);
124 int sctp_primitive_ASSOCIATE(struct net *, struct sctp_association *, void *arg);
125 int sctp_primitive_SHUTDOWN(struct net *, struct sctp_association *, void *arg);
126 int sctp_primitive_ABORT(struct net *, struct sctp_association *, void *arg);
127 int sctp_primitive_SEND(struct net *, struct sctp_association *, void *arg);
128 int sctp_primitive_REQUESTHEARTBEAT(struct net *, struct sctp_association *, void *arg);
129 int sctp_primitive_ASCONF(struct net *, struct sctp_association *, void *arg);
134 int sctp_rcv(struct sk_buff *skb);
135 void sctp_v4_err(struct sk_buff *skb, u32 info);
136 void sctp_hash_established(struct sctp_association *);
137 void sctp_unhash_established(struct sctp_association *);
138 void sctp_hash_endpoint(struct sctp_endpoint *);
139 void sctp_unhash_endpoint(struct sctp_endpoint *);
140 struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *,
141 struct sctphdr *, struct sctp_association **,
142 struct sctp_transport **);
143 void sctp_err_finish(struct sock *, struct sctp_association *);
144 void sctp_icmp_frag_needed(struct sock *, struct sctp_association *,
145 struct sctp_transport *t, __u32 pmtu);
146 void sctp_icmp_redirect(struct sock *, struct sctp_transport *,
148 void sctp_icmp_proto_unreachable(struct sock *sk,
149 struct sctp_association *asoc,
150 struct sctp_transport *t);
151 void sctp_backlog_migrate(struct sctp_association *assoc,
152 struct sock *oldsk, struct sock *newsk);
157 int sctp_snmp_proc_init(struct net *net);
158 void sctp_snmp_proc_exit(struct net *net);
159 int sctp_eps_proc_init(struct net *net);
160 void sctp_eps_proc_exit(struct net *net);
161 int sctp_assocs_proc_init(struct net *net);
162 void sctp_assocs_proc_exit(struct net *net);
163 int sctp_remaddr_proc_init(struct net *net);
164 void sctp_remaddr_proc_exit(struct net *net);
168 * Module global variables
174 extern struct kmem_cache *sctp_chunk_cachep __read_mostly;
175 extern struct kmem_cache *sctp_bucket_cachep __read_mostly;
178 * Section: Macros, externs, and inlines
181 /* spin lock wrappers. */
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)
194 /* sock lock wrappers. */
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)
200 /* SCTP SNMP MIB stats handlers */
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)
206 /* sctp mib definitions */
209 SCTP_MIB_CURRESTAB, /* CurrEstab */
210 SCTP_MIB_ACTIVEESTABS, /* ActiveEstabs */
211 SCTP_MIB_PASSIVEESTABS, /* PassiveEstabs */
212 SCTP_MIB_ABORTEDS, /* Aborteds */
213 SCTP_MIB_SHUTDOWNS, /* Shutdowns */
214 SCTP_MIB_OUTOFBLUES, /* OutOfBlues */
215 SCTP_MIB_CHECKSUMERRORS, /* ChecksumErrors */
216 SCTP_MIB_OUTCTRLCHUNKS, /* OutCtrlChunks */
217 SCTP_MIB_OUTORDERCHUNKS, /* OutOrderChunks */
218 SCTP_MIB_OUTUNORDERCHUNKS, /* OutUnorderChunks */
219 SCTP_MIB_INCTRLCHUNKS, /* InCtrlChunks */
220 SCTP_MIB_INORDERCHUNKS, /* InOrderChunks */
221 SCTP_MIB_INUNORDERCHUNKS, /* InUnorderChunks */
222 SCTP_MIB_FRAGUSRMSGS, /* FragUsrMsgs */
223 SCTP_MIB_REASMUSRMSGS, /* ReasmUsrMsgs */
224 SCTP_MIB_OUTSCTPPACKS, /* OutSCTPPacks */
225 SCTP_MIB_INSCTPPACKS, /* 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,
245 #define SCTP_MIB_MAX __SCTP_MIB_MAX
247 unsigned long mibs[SCTP_MIB_MAX];
250 /* helper function to track stats about max rto and related transport */
251 static inline void sctp_max_rto(struct sctp_association *asoc,
252 struct sctp_transport *trans)
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);
264 * Macros for keeping a global reference of object allocations.
266 #ifdef CONFIG_SCTP_DBG_OBJCNT
268 extern atomic_t sctp_dbg_objcnt_sock;
269 extern atomic_t sctp_dbg_objcnt_ep;
270 extern atomic_t sctp_dbg_objcnt_assoc;
271 extern atomic_t sctp_dbg_objcnt_transport;
272 extern atomic_t sctp_dbg_objcnt_chunk;
273 extern atomic_t sctp_dbg_objcnt_bind_addr;
274 extern atomic_t sctp_dbg_objcnt_bind_bucket;
275 extern atomic_t sctp_dbg_objcnt_addr;
276 extern atomic_t sctp_dbg_objcnt_ssnmap;
277 extern atomic_t sctp_dbg_objcnt_datamsg;
278 extern atomic_t sctp_dbg_objcnt_keys;
280 /* Macros to atomically increment/decrement objcnt counters. */
281 #define SCTP_DBG_OBJCNT_INC(name) \
282 atomic_inc(&sctp_dbg_objcnt_## name)
283 #define SCTP_DBG_OBJCNT_DEC(name) \
284 atomic_dec(&sctp_dbg_objcnt_## name)
285 #define SCTP_DBG_OBJCNT(name) \
286 atomic_t sctp_dbg_objcnt_## name = ATOMIC_INIT(0)
288 /* Macro to help create new entries in in the global array of
291 #define SCTP_DBG_OBJCNT_ENTRY(name) \
292 {.label= #name, .counter= &sctp_dbg_objcnt_## name}
294 void sctp_dbg_objcnt_init(struct net *);
295 void sctp_dbg_objcnt_exit(struct net *);
299 #define SCTP_DBG_OBJCNT_INC(name)
300 #define SCTP_DBG_OBJCNT_DEC(name)
302 static inline void sctp_dbg_objcnt_init(struct net *net) { return; }
303 static inline void sctp_dbg_objcnt_exit(struct net *net) { return; }
305 #endif /* CONFIG_SCTP_DBG_OBJCOUNT */
307 #if defined CONFIG_SYSCTL
308 void sctp_sysctl_register(void);
309 void sctp_sysctl_unregister(void);
310 int sctp_sysctl_net_register(struct net *net);
311 void sctp_sysctl_net_unregister(struct net *net);
313 static inline void sctp_sysctl_register(void) { return; }
314 static inline void sctp_sysctl_unregister(void) { return; }
315 static inline int sctp_sysctl_net_register(struct net *net) { return 0; }
316 static inline void sctp_sysctl_net_unregister(struct net *net) { return; }
319 /* Size of Supported Address Parameter for 'x' address types. */
320 #define SCTP_SAT_LEN(x) (sizeof(struct sctp_paramhdr) + (x) * sizeof(__u16))
322 #if IS_ENABLED(CONFIG_IPV6)
324 void sctp_v6_pf_init(void);
325 void sctp_v6_pf_exit(void);
326 int sctp_v6_protosw_init(void);
327 void sctp_v6_protosw_exit(void);
328 int sctp_v6_add_protocol(void);
329 void sctp_v6_del_protocol(void);
331 #else /* #ifdef defined(CONFIG_IPV6) */
333 static inline void sctp_v6_pf_init(void) { return; }
334 static inline void sctp_v6_pf_exit(void) { return; }
335 static inline int sctp_v6_protosw_init(void) { return 0; }
336 static inline void sctp_v6_protosw_exit(void) { return; }
337 static inline int sctp_v6_add_protocol(void) { return 0; }
338 static inline void sctp_v6_del_protocol(void) { return; }
340 #endif /* #if defined(CONFIG_IPV6) */
343 /* Map an association to an assoc_id. */
344 static inline sctp_assoc_t sctp_assoc2id(const struct sctp_association *asoc)
346 return asoc ? asoc->assoc_id : 0;
349 /* Look up the association by its id. */
350 struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id);
352 int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp);
354 /* A macro to walk a list of skbs. */
355 #define sctp_skb_for_each(pos, head, tmp) \
356 skb_queue_walk_safe(head, pos, tmp)
358 /* A helper to append an entire skb list (list) to another (head). */
359 static inline void sctp_skb_list_tail(struct sk_buff_head *list,
360 struct sk_buff_head *head)
364 sctp_spin_lock_irqsave(&head->lock, flags);
365 sctp_spin_lock(&list->lock);
367 skb_queue_splice_tail_init(list, head);
369 sctp_spin_unlock(&list->lock);
370 sctp_spin_unlock_irqrestore(&head->lock, flags);
374 * sctp_list_dequeue - remove from the head of the queue
375 * @list: list to dequeue from
377 * Remove the head of the list. The head item is
378 * returned or %NULL if the list is empty.
381 static inline struct list_head *sctp_list_dequeue(struct list_head *list)
383 struct list_head *result = NULL;
385 if (list->next != list) {
387 list->next = result->next;
388 list->next->prev = list;
389 INIT_LIST_HEAD(result);
394 /* SCTP version of skb_set_owner_r. We need this one because
395 * of the way we have to do receive buffer accounting on bundled
398 static inline void sctp_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
400 struct sctp_ulpevent *event = sctp_skb2event(skb);
404 skb->destructor = sctp_sock_rfree;
405 atomic_add(event->rmem_len, &sk->sk_rmem_alloc);
407 * This mimics the behavior of skb_set_owner_r
409 sk->sk_forward_alloc -= event->rmem_len;
412 /* Tests if the list has one and only one entry. */
413 static inline int sctp_list_single_entry(struct list_head *head)
415 return (head->next != head) && (head->next == head->prev);
418 /* Generate a random jitter in the range of -50% ~ +50% of input RTO. */
419 static inline __s32 sctp_jitter(__u32 rto)
421 static __u32 sctp_rand;
424 /* Avoid divide by zero. */
428 sctp_rand += jiffies;
429 sctp_rand ^= (sctp_rand << 12);
430 sctp_rand ^= (sctp_rand >> 20);
432 /* Choose random number from 0 to rto, then move to -50% ~ +50%
435 ret = sctp_rand % rto - (rto >> 1);
439 /* Break down data chunks at this point. */
440 static inline int sctp_frag_point(const struct sctp_association *asoc, int pmtu)
442 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
445 frag -= sp->pf->af->net_header_len;
446 frag -= sizeof(struct sctphdr) + sizeof(struct sctp_data_chunk);
449 frag = min_t(int, frag, asoc->user_frag);
451 frag = min_t(int, frag, SCTP_MAX_CHUNK_LEN);
456 static inline void sctp_assoc_pending_pmtu(struct sock *sk, struct sctp_association *asoc)
459 sctp_assoc_sync_pmtu(sk, asoc);
460 asoc->pmtu_pending = 0;
463 /* Walk through a list of TLV parameters. Don't trust the
464 * individual parameter lengths and instead depend on
465 * the chunk length to indicate when to stop. Make sure
466 * there is room for a param header too.
468 #define sctp_walk_params(pos, chunk, member)\
469 _sctp_walk_params((pos), (chunk), ntohs((chunk)->chunk_hdr.length), member)
471 #define _sctp_walk_params(pos, chunk, end, member)\
472 for (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)))
477 #define sctp_walk_errors(err, chunk_hdr)\
478 _sctp_walk_errors((err), (chunk_hdr), ntohs((chunk_hdr)->length))
480 #define _sctp_walk_errors(err, chunk_hdr, end)\
481 for (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))))
487 #define sctp_walk_fwdtsn(pos, chunk)\
488 _sctp_walk_fwdtsn((pos), (chunk), ntohs((chunk)->chunk_hdr->length) - sizeof(struct sctp_fwdtsn_chunk))
490 #define _sctp_walk_fwdtsn(pos, chunk, end)\
491 for (pos = chunk->subh.fwdtsn_hdr->skip;\
492 (void *)pos <= (void *)chunk->subh.fwdtsn_hdr->skip + end - sizeof(struct sctp_fwdtsn_skip);\
495 /* Round an int up to the next multiple of 4. */
496 #define WORD_ROUND(s) (((s)+3)&~3)
498 /* External references. */
500 extern struct proto sctp_prot;
501 extern struct proto sctpv6_prot;
502 void sctp_put_port(struct sock *sk);
504 extern struct idr sctp_assocs_id;
505 extern spinlock_t sctp_assocs_id_lock;
507 /* Static inline functions. */
509 /* Convert from an IP version number to an Address Family symbol. */
510 static inline int ipver2af(__u8 ipver)
522 /* Convert from an address parameter type to an address family. */
523 static inline int param_type2af(__be16 type)
526 case SCTP_PARAM_IPV4_ADDRESS:
528 case SCTP_PARAM_IPV6_ADDRESS:
535 /* Warning: The following hash functions assume a power of two 'size'. */
536 /* This is the hash function for the SCTP port hash table. */
537 static inline int sctp_phashfn(struct net *net, __u16 lport)
539 return (net_hash_mix(net) + lport) & (sctp_port_hashsize - 1);
542 /* This is the hash function for the endpoint hash table. */
543 static inline int sctp_ep_hashfn(struct net *net, __u16 lport)
545 return (net_hash_mix(net) + lport) & (sctp_ep_hashsize - 1);
548 /* This is the hash function for the association hash table. */
549 static inline int sctp_assoc_hashfn(struct net *net, __u16 lport, __u16 rport)
551 int h = (lport << 16) + rport + net_hash_mix(net);
553 return h & (sctp_assoc_hashsize - 1);
556 /* This is the hash function for the association hash table. This is
557 * not used yet, but could be used as a better hash function when
560 static inline int sctp_vtag_hashfn(__u16 lport, __u16 rport, __u32 vtag)
562 int h = (lport << 16) + rport;
564 return h & (sctp_assoc_hashsize - 1);
567 #define sctp_for_each_hentry(epb, head) \
568 hlist_for_each_entry(epb, head, node)
570 /* Is a socket of this style? */
571 #define sctp_style(sk, style) __sctp_style((sk), (SCTP_SOCKET_##style))
572 static inline int __sctp_style(const struct sock *sk, sctp_socket_type_t style)
574 return sctp_sk(sk)->type == style;
577 /* Is the association in this state? */
578 #define sctp_state(asoc, state) __sctp_state((asoc), (SCTP_STATE_##state))
579 static inline int __sctp_state(const struct sctp_association *asoc,
582 return asoc->state == state;
585 /* Is the socket in this state? */
586 #define sctp_sstate(sk, state) __sctp_sstate((sk), (SCTP_SS_##state))
587 static inline int __sctp_sstate(const struct sock *sk, sctp_sock_state_t state)
589 return sk->sk_state == state;
592 /* Map v4-mapped v6 address back to v4 address */
593 static inline void sctp_v6_map_v4(union sctp_addr *addr)
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];
600 /* Map v4 address to v4-mapped v6 address */
601 static inline void sctp_v4_map_v6(union sctp_addr *addr)
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);
611 /* The cookie is always 0 since this is how it's used in the
614 static inline struct dst_entry *sctp_transport_dst_check(struct sctp_transport *t)
616 if (t->dst && !dst_check(t->dst, 0)) {
624 #endif /* __net_sctp_h__ */