drivers: net: cpsw: add support to show hw stats via ethtool
[linux-2.6-block.git] / include / net / sctp / sctp.h
CommitLineData
60c778b2 1/* SCTP kernel implementation
1da177e4
LT
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.
6 *
60c778b2 7 * This file is part of the SCTP kernel implementation
1da177e4
LT
8 *
9 * The base lksctp header.
10 *
60c778b2 11 * This SCTP implementation is free software;
1da177e4
LT
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)
15 * any later version.
16 *
60c778b2 17 * This SCTP implementation is distributed in the hope that it
1da177e4
LT
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.
22 *
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.
27 *
28 * Please send any bug reports or fixes you make to the
29 * email address(es):
30 * lksctp developers <lksctp-developers@lists.sourceforge.net>
31 *
32 * Or submit a bug report through the following website:
33 * http://www.sf.net/projects/lksctp
34 *
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>
44 *
45 * Any bugs reported given to us we will try to fix... any fixes shared will
46 * be incorporated into the next SCTP release.
47 */
48
49#ifndef __net_sctp_h__
50#define __net_sctp_h__
51
52/* Header Strategy.
53 * Start getting some control over the header file depencies:
54 * includes
55 * constants
56 * structs
57 * prototypes
58 * macros, externs, and inlines
59 *
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.
63 */
64
1da177e4
LT
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
dfd56b8b 74#if IS_ENABLED(CONFIG_IPV6)
1da177e4
LT
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
1da177e4
LT
86#ifdef CONFIG_IP_SCTP_MODULE
87#define SCTP_PROTOSW_FLAG 0
88#else /* static! */
89#define SCTP_PROTOSW_FLAG INET_PROTOSW_PERMANENT
90#endif
91
1da177e4
LT
92/*
93 * Function declarations.
94 */
95
96/*
97 * sctp/protocol.c
98 */
4db67e80 99extern int sctp_copy_local_addr_list(struct net *, struct sctp_bind_addr *,
dd0fc66f 100 sctp_scope_t, gfp_t gfp,
3182cd84 101 int flags);
1da177e4
LT
102extern struct sctp_pf *sctp_get_pf_specific(sa_family_t family);
103extern int sctp_register_pf(struct sctp_pf *, sa_family_t);
4db67e80 104extern void sctp_addr_wq_mgmt(struct net *, struct sctp_sockaddr_entry *, int);
1da177e4
LT
105
106/*
107 * sctp/socket.c
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);
561b1733 112void sctp_data_ready(struct sock *sk, int len);
1da177e4
LT
113unsigned int sctp_poll(struct file *file, struct socket *sock,
114 poll_table *wait);
331c4ee7 115void sctp_sock_rfree(struct sk_buff *skb);
914e1c8b
VY
116void sctp_copy_sock(struct sock *newsk, struct sock *sk,
117 struct sctp_association *asoc);
1748376b 118extern struct percpu_counter sctp_sockets_allocated;
9f7d653b 119extern int sctp_asconf_mgmt(struct sctp_sock *, struct sctp_sockaddr_entry *);
1da177e4
LT
120
121/*
122 * sctp/primitive.c
123 */
55e26eb9
EB
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);
1da177e4 130
1da177e4
LT
131/*
132 * sctp/input.c
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 *);
4110cc25 140struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *,
d1ad1ff2 141 struct sctphdr *, struct sctp_association **,
1da177e4 142 struct sctp_transport **);
d1ad1ff2 143void sctp_err_finish(struct sock *, struct sctp_association *);
1da177e4
LT
144void sctp_icmp_frag_needed(struct sock *, struct sctp_association *,
145 struct sctp_transport *t, __u32 pmtu);
ec18d9a2
DM
146void sctp_icmp_redirect(struct sock *, struct sctp_transport *,
147 struct sk_buff *);
1da177e4 148void sctp_icmp_proto_unreachable(struct sock *sk,
1da177e4
LT
149 struct sctp_association *asoc,
150 struct sctp_transport *t);
c4d2444e
SS
151void sctp_backlog_migrate(struct sctp_association *assoc,
152 struct sock *oldsk, struct sock *newsk);
1da177e4 153
16164366
AB
154/*
155 * sctp/proc.c
156 */
13d782f6
EB
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);
16164366
AB
165
166
0a5fcb9c 167/*
168 * Module global variables
169 */
170
171 /*
172 * sctp/protocol.c
173 */
174extern struct kmem_cache *sctp_chunk_cachep __read_mostly;
175extern struct kmem_cache *sctp_bucket_cachep __read_mostly;
176
1da177e4
LT
177/*
178 * Section: Macros, externs, and inlines
179 */
180
1da177e4
LT
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)
193
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)
1da177e4
LT
199
200/* SCTP SNMP MIB stats handlers */
b01a2407
EB
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)
1da177e4 205
ac0b0462 206/* sctp mib definitions */
fd2c3ef7 207enum {
ac0b0462
SS
208 SCTP_MIB_NUM = 0,
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,
b6157d8e 234 SCTP_MIB_T1_RETRANSMITS,
ac0b0462
SS
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];
ec733b15 248};
ac0b0462 249
196d6759
MB
250/* helper function to track stats about max rto and related transport */
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}
1da177e4 262
1da177e4
LT
263/*
264 * Macros for keeping a global reference of object allocations.
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;
1f485649 278extern atomic_t sctp_dbg_objcnt_keys;
1da177e4
LT
279
280/* Macros to atomically increment/decrement objcnt counters. */
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/* Macro to help create new entries in in the global array of
289 * objcnt counters.
290 */
291#define SCTP_DBG_OBJCNT_ENTRY(name) \
292{.label= #name, .counter= &sctp_dbg_objcnt_## name}
293
13d782f6
EB
294void sctp_dbg_objcnt_init(struct net *);
295void sctp_dbg_objcnt_exit(struct net *);
1da177e4
LT
296
297#else
298
299#define SCTP_DBG_OBJCNT_INC(name)
300#define SCTP_DBG_OBJCNT_DEC(name)
301
1f07b62f
CW
302static inline void sctp_dbg_objcnt_init(struct net *net) { return; }
303static inline void sctp_dbg_objcnt_exit(struct net *net) { return; }
1da177e4
LT
304
305#endif /* CONFIG_SCTP_DBG_OBJCOUNT */
306
307#if defined CONFIG_SYSCTL
308void sctp_sysctl_register(void);
309void sctp_sysctl_unregister(void);
ebb7e95d
EB
310int sctp_sysctl_net_register(struct net *net);
311void sctp_sysctl_net_unregister(struct net *net);
1da177e4
LT
312#else
313static inline void sctp_sysctl_register(void) { return; }
314static inline void sctp_sysctl_unregister(void) { return; }
ebb7e95d
EB
315static inline int sctp_sysctl_net_register(struct net *net) { return 0; }
316static inline void sctp_sysctl_net_unregister(struct net *net) { return; }
1da177e4
LT
317#endif
318
319/* Size of Supported Address Parameter for 'x' address types. */
320#define SCTP_SAT_LEN(x) (sizeof(struct sctp_paramhdr) + (x) * sizeof(__u16))
321
dfd56b8b 322#if IS_ENABLED(CONFIG_IPV6)
1da177e4 323
270637ab
VY
324void sctp_v6_pf_init(void);
325void sctp_v6_pf_exit(void);
326int sctp_v6_protosw_init(void);
327void sctp_v6_protosw_exit(void);
827bf122
SS
328int sctp_v6_add_protocol(void);
329void sctp_v6_del_protocol(void);
1da177e4
LT
330
331#else /* #ifdef defined(CONFIG_IPV6) */
332
1233823b 333static inline void sctp_v6_pf_init(void) { return; }
270637ab
VY
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; }
827bf122
SS
337static inline int sctp_v6_add_protocol(void) { return 0; }
338static inline void sctp_v6_del_protocol(void) { return; }
1da177e4
LT
339
340#endif /* #if defined(CONFIG_IPV6) */
341
1da177e4
LT
342
343/* Map an association to an assoc_id. */
344static inline sctp_assoc_t sctp_assoc2id(const struct sctp_association *asoc)
345{
a02cec21 346 return asoc ? asoc->assoc_id : 0;
1da177e4
LT
347}
348
349/* Look up the association by its id. */
350struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id);
351
0343c554 352int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp);
1da177e4
LT
353
354/* A macro to walk a list of skbs. */
355#define sctp_skb_for_each(pos, head, tmp) \
3d09274c 356 skb_queue_walk_safe(head, pos, tmp)
1da177e4
LT
357
358/* A helper to append an entire skb list (list) to another (head). */
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
3d09274c 367 skb_queue_splice_tail_init(list, head);
1da177e4
LT
368
369 sctp_spin_unlock(&list->lock);
370 sctp_spin_unlock_irqrestore(&head->lock, flags);
371}
372
373/**
374 * sctp_list_dequeue - remove from the head of the queue
375 * @list: list to dequeue from
376 *
377 * Remove the head of the list. The head item is
378 * returned or %NULL if the list is empty.
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
331c4ee7
VY
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
396 * chunks.
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
d55d87fd 402 skb_orphan(skb);
331c4ee7
VY
403 skb->sk = sk;
404 skb->destructor = sctp_sock_rfree;
405 atomic_add(event->rmem_len, &sk->sk_rmem_alloc);
4d93df0a 406 /*
3ab224be 407 * This mimics the behavior of skb_set_owner_r
4d93df0a
NH
408 */
409 sk->sk_forward_alloc -= event->rmem_len;
331c4ee7
VY
410}
411
1da177e4
LT
412/* Tests if the list has one and only one entry. */
413static inline int sctp_list_single_entry(struct list_head *head)
414{
a02cec21 415 return (head->next != head) && (head->next == head->prev);
1da177e4
LT
416}
417
1da177e4
LT
418/* Generate a random jitter in the range of -50% ~ +50% of input RTO. */
419static inline __s32 sctp_jitter(__u32 rto)
420{
421 static __u32 sctp_rand;
422 __s32 ret;
423
424 /* Avoid divide by zero. */
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 /* Choose random number from 0 to rto, then move to -50% ~ +50%
433 * of rto.
434 */
435 ret = sctp_rand % rto - (rto >> 1);
436 return ret;
437}
438
439/* Break down data chunks at this point. */
f68b2e05 440static inline int sctp_frag_point(const struct sctp_association *asoc, int pmtu)
1da177e4 441{
f68b2e05 442 struct sctp_sock *sp = sctp_sk(asoc->base.sk);
1da177e4
LT
443 int frag = pmtu;
444
445 frag -= sp->pf->af->net_header_len;
446 frag -= sizeof(struct sctphdr) + sizeof(struct sctp_data_chunk);
447
f68b2e05
VY
448 if (asoc->user_frag)
449 frag = min_t(int, frag, asoc->user_frag);
1da177e4
LT
450
451 frag = min_t(int, frag, SCTP_MAX_CHUNK_LEN);
452
453 return frag;
454}
455
02f3d4ce 456static inline void sctp_assoc_pending_pmtu(struct sock *sk, struct sctp_association *asoc)
8a479491
VY
457{
458
02f3d4ce 459 sctp_assoc_sync_pmtu(sk, asoc);
8a479491
VY
460 asoc->pmtu_pending = 0;
461}
462
1da177e4
LT
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.
467 */
468#define sctp_walk_params(pos, chunk, member)\
dd2d1c6f 469_sctp_walk_params((pos), (chunk), ntohs((chunk)->chunk_hdr.length), member)
1da177e4
LT
470
471#define _sctp_walk_params(pos, chunk, end, member)\
472for (pos.v = chunk->member;\
dd2d1c6f 473 pos.v <= (void *)chunk + end - ntohs(pos.p->length) &&\
1da177e4
LT
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));\
dd2d1c6f 483 (void *)err <= (void *)chunk_hdr + end - ntohs(err->length) &&\
1da177e4
LT
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/* Round an int up to the next multiple of 4. */
496#define WORD_ROUND(s) (((s)+3)&~3)
497
1da177e4
LT
498/* External references. */
499
500extern struct proto sctp_prot;
501extern struct proto sctpv6_prot;
1da177e4
LT
502void sctp_put_port(struct sock *sk);
503
504extern struct idr sctp_assocs_id;
505extern spinlock_t sctp_assocs_id_lock;
506
507/* Static inline functions. */
508
509/* Convert from an IP version number to an Address Family symbol. */
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;
e3cc055c 519 }
1da177e4
LT
520}
521
522/* Convert from an address parameter type to an address family. */
dbc16db1 523static inline int param_type2af(__be16 type)
1da177e4
LT
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;
e3cc055c 532 }
1da177e4
LT
533}
534
1da177e4
LT
535/* Warning: The following hash functions assume a power of two 'size'. */
536/* This is the hash function for the SCTP port hash table. */
f1f43763 537static inline int sctp_phashfn(struct net *net, __u16 lport)
1da177e4 538{
f1f43763 539 return (net_hash_mix(net) + lport) & (sctp_port_hashsize - 1);
1da177e4
LT
540}
541
542/* This is the hash function for the endpoint hash table. */
4cdadcbc 543static inline int sctp_ep_hashfn(struct net *net, __u16 lport)
1da177e4 544{
4cdadcbc 545 return (net_hash_mix(net) + lport) & (sctp_ep_hashsize - 1);
1da177e4
LT
546}
547
548/* This is the hash function for the association hash table. */
4110cc25 549static inline int sctp_assoc_hashfn(struct net *net, __u16 lport, __u16 rport)
1da177e4 550{
4110cc25 551 int h = (lport << 16) + rport + net_hash_mix(net);
1da177e4 552 h ^= h>>8;
a02cec21 553 return h & (sctp_assoc_hashsize - 1);
1da177e4
LT
554}
555
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
558 * we have a vtag.
559 */
560static inline int sctp_vtag_hashfn(__u16 lport, __u16 rport, __u32 vtag)
561{
562 int h = (lport << 16) + rport;
563 h ^= vtag;
a02cec21 564 return h & (sctp_assoc_hashsize - 1);
1da177e4
LT
565}
566
b67bfe0d
SL
567#define sctp_for_each_hentry(epb, head) \
568 hlist_for_each_entry(epb, head, node)
d970dbf8 569
1da177e4
LT
570/* Is a socket of this style? */
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/* Is the association in this state? */
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/* Is the socket in this state? */
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/* Map v4-mapped v6 address back to v4 address */
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/* Map v4 address to v4-mapped v6 address */
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
e0268868
ND
611/* The cookie is always 0 since this is how it's used in the
612 * pmtu code.
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
1da177e4 624#endif /* __net_sctp_h__ */