sctp.h 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  1. /* SPDX-License-Identifier: GPL-2.0-or-later */
  2. /* SCTP kernel implementation
  3. * (C) Copyright IBM Corp. 2001, 2004
  4. * Copyright (c) 1999-2000 Cisco, Inc.
  5. * Copyright (c) 1999-2001 Motorola, Inc.
  6. * Copyright (c) 2001-2003 Intel Corp.
  7. *
  8. * This file is part of the SCTP kernel implementation
  9. *
  10. * The base lksctp header.
  11. *
  12. * Please send any bug reports or fixes you make to the
  13. * email address(es):
  14. * lksctp developers <[email protected]>
  15. *
  16. * Written or modified by:
  17. * La Monte H.P. Yarroll <[email protected]>
  18. * Xingang Guo <[email protected]>
  19. * Jon Grimm <[email protected]>
  20. * Daisy Chang <[email protected]>
  21. * Sridhar Samudrala <[email protected]>
  22. * Ardelle Fan <[email protected]>
  23. * Ryan Layer <[email protected]>
  24. * Kevin Gao <[email protected]>
  25. */
  26. #ifndef __net_sctp_h__
  27. #define __net_sctp_h__
  28. /* Header Strategy.
  29. * Start getting some control over the header file depencies:
  30. * includes
  31. * constants
  32. * structs
  33. * prototypes
  34. * macros, externs, and inlines
  35. *
  36. * Move test_frame specific items out of the kernel headers
  37. * and into the test frame headers. This is not perfect in any sense
  38. * and will continue to evolve.
  39. */
  40. #include <linux/types.h>
  41. #include <linux/slab.h>
  42. #include <linux/in.h>
  43. #include <linux/tty.h>
  44. #include <linux/proc_fs.h>
  45. #include <linux/spinlock.h>
  46. #include <linux/jiffies.h>
  47. #include <linux/idr.h>
  48. #if IS_ENABLED(CONFIG_IPV6)
  49. #include <net/ipv6.h>
  50. #include <net/ip6_route.h>
  51. #endif
  52. #include <linux/uaccess.h>
  53. #include <asm/page.h>
  54. #include <net/sock.h>
  55. #include <net/snmp.h>
  56. #include <net/sctp/structs.h>
  57. #include <net/sctp/constants.h>
  58. #ifdef CONFIG_IP_SCTP_MODULE
  59. #define SCTP_PROTOSW_FLAG 0
  60. #else /* static! */
  61. #define SCTP_PROTOSW_FLAG INET_PROTOSW_PERMANENT
  62. #endif
  63. /* Round an int up to the next multiple of 4. */
  64. #define SCTP_PAD4(s) (((s)+3)&~3)
  65. /* Truncate to the previous multiple of 4. */
  66. #define SCTP_TRUNC4(s) ((s)&~3)
  67. /*
  68. * Function declarations.
  69. */
  70. /*
  71. * sctp/protocol.c
  72. */
  73. int sctp_copy_local_addr_list(struct net *net, struct sctp_bind_addr *addr,
  74. enum sctp_scope, gfp_t gfp, int flags);
  75. struct sctp_pf *sctp_get_pf_specific(sa_family_t family);
  76. int sctp_register_pf(struct sctp_pf *, sa_family_t);
  77. void sctp_addr_wq_mgmt(struct net *, struct sctp_sockaddr_entry *, int);
  78. int sctp_udp_sock_start(struct net *net);
  79. void sctp_udp_sock_stop(struct net *net);
  80. /*
  81. * sctp/socket.c
  82. */
  83. int sctp_inet_connect(struct socket *sock, struct sockaddr *uaddr,
  84. int addr_len, int flags);
  85. int sctp_backlog_rcv(struct sock *sk, struct sk_buff *skb);
  86. int sctp_inet_listen(struct socket *sock, int backlog);
  87. void sctp_write_space(struct sock *sk);
  88. void sctp_data_ready(struct sock *sk);
  89. __poll_t sctp_poll(struct file *file, struct socket *sock,
  90. poll_table *wait);
  91. void sctp_sock_rfree(struct sk_buff *skb);
  92. void sctp_copy_sock(struct sock *newsk, struct sock *sk,
  93. struct sctp_association *asoc);
  94. extern struct percpu_counter sctp_sockets_allocated;
  95. int sctp_asconf_mgmt(struct sctp_sock *, struct sctp_sockaddr_entry *);
  96. struct sk_buff *sctp_skb_recv_datagram(struct sock *, int, int *);
  97. typedef int (*sctp_callback_t)(struct sctp_endpoint *, struct sctp_transport *, void *);
  98. void sctp_transport_walk_start(struct rhashtable_iter *iter);
  99. void sctp_transport_walk_stop(struct rhashtable_iter *iter);
  100. struct sctp_transport *sctp_transport_get_next(struct net *net,
  101. struct rhashtable_iter *iter);
  102. struct sctp_transport *sctp_transport_get_idx(struct net *net,
  103. struct rhashtable_iter *iter, int pos);
  104. int sctp_transport_lookup_process(sctp_callback_t cb, struct net *net,
  105. const union sctp_addr *laddr,
  106. const union sctp_addr *paddr, void *p);
  107. int sctp_transport_traverse_process(sctp_callback_t cb, sctp_callback_t cb_done,
  108. struct net *net, int *pos, void *p);
  109. int sctp_for_each_endpoint(int (*cb)(struct sctp_endpoint *, void *), void *p);
  110. int sctp_get_sctp_info(struct sock *sk, struct sctp_association *asoc,
  111. struct sctp_info *info);
  112. /*
  113. * sctp/primitive.c
  114. */
  115. int sctp_primitive_ASSOCIATE(struct net *, struct sctp_association *, void *arg);
  116. int sctp_primitive_SHUTDOWN(struct net *, struct sctp_association *, void *arg);
  117. int sctp_primitive_ABORT(struct net *, struct sctp_association *, void *arg);
  118. int sctp_primitive_SEND(struct net *, struct sctp_association *, void *arg);
  119. int sctp_primitive_REQUESTHEARTBEAT(struct net *, struct sctp_association *, void *arg);
  120. int sctp_primitive_ASCONF(struct net *, struct sctp_association *, void *arg);
  121. int sctp_primitive_RECONF(struct net *net, struct sctp_association *asoc,
  122. void *arg);
  123. /*
  124. * sctp/input.c
  125. */
  126. int sctp_rcv(struct sk_buff *skb);
  127. int sctp_v4_err(struct sk_buff *skb, u32 info);
  128. int sctp_hash_endpoint(struct sctp_endpoint *ep);
  129. void sctp_unhash_endpoint(struct sctp_endpoint *);
  130. struct sock *sctp_err_lookup(struct net *net, int family, struct sk_buff *,
  131. struct sctphdr *, struct sctp_association **,
  132. struct sctp_transport **);
  133. void sctp_err_finish(struct sock *, struct sctp_transport *);
  134. int sctp_udp_v4_err(struct sock *sk, struct sk_buff *skb);
  135. int sctp_udp_v6_err(struct sock *sk, struct sk_buff *skb);
  136. void sctp_icmp_frag_needed(struct sock *, struct sctp_association *,
  137. struct sctp_transport *t, __u32 pmtu);
  138. void sctp_icmp_redirect(struct sock *, struct sctp_transport *,
  139. struct sk_buff *);
  140. void sctp_icmp_proto_unreachable(struct sock *sk,
  141. struct sctp_association *asoc,
  142. struct sctp_transport *t);
  143. void sctp_backlog_migrate(struct sctp_association *assoc,
  144. struct sock *oldsk, struct sock *newsk);
  145. int sctp_transport_hashtable_init(void);
  146. void sctp_transport_hashtable_destroy(void);
  147. int sctp_hash_transport(struct sctp_transport *t);
  148. void sctp_unhash_transport(struct sctp_transport *t);
  149. struct sctp_transport *sctp_addrs_lookup_transport(
  150. struct net *net,
  151. const union sctp_addr *laddr,
  152. const union sctp_addr *paddr);
  153. struct sctp_transport *sctp_epaddr_lookup_transport(
  154. const struct sctp_endpoint *ep,
  155. const union sctp_addr *paddr);
  156. /*
  157. * sctp/proc.c
  158. */
  159. int __net_init sctp_proc_init(struct net *net);
  160. /*
  161. * sctp/offload.c
  162. */
  163. int sctp_offload_init(void);
  164. /*
  165. * sctp/stream_sched.c
  166. */
  167. void sctp_sched_ops_init(void);
  168. /*
  169. * sctp/stream.c
  170. */
  171. int sctp_send_reset_streams(struct sctp_association *asoc,
  172. struct sctp_reset_streams *params);
  173. int sctp_send_reset_assoc(struct sctp_association *asoc);
  174. int sctp_send_add_streams(struct sctp_association *asoc,
  175. struct sctp_add_streams *params);
  176. /*
  177. * Module global variables
  178. */
  179. /*
  180. * sctp/protocol.c
  181. */
  182. extern struct kmem_cache *sctp_chunk_cachep __read_mostly;
  183. extern struct kmem_cache *sctp_bucket_cachep __read_mostly;
  184. extern long sysctl_sctp_mem[3];
  185. extern int sysctl_sctp_rmem[3];
  186. extern int sysctl_sctp_wmem[3];
  187. /*
  188. * Section: Macros, externs, and inlines
  189. */
  190. /* SCTP SNMP MIB stats handlers */
  191. #define SCTP_INC_STATS(net, field) SNMP_INC_STATS((net)->sctp.sctp_statistics, field)
  192. #define __SCTP_INC_STATS(net, field) __SNMP_INC_STATS((net)->sctp.sctp_statistics, field)
  193. #define SCTP_DEC_STATS(net, field) SNMP_DEC_STATS((net)->sctp.sctp_statistics, field)
  194. /* sctp mib definitions */
  195. enum {
  196. SCTP_MIB_NUM = 0,
  197. SCTP_MIB_CURRESTAB, /* CurrEstab */
  198. SCTP_MIB_ACTIVEESTABS, /* ActiveEstabs */
  199. SCTP_MIB_PASSIVEESTABS, /* PassiveEstabs */
  200. SCTP_MIB_ABORTEDS, /* Aborteds */
  201. SCTP_MIB_SHUTDOWNS, /* Shutdowns */
  202. SCTP_MIB_OUTOFBLUES, /* OutOfBlues */
  203. SCTP_MIB_CHECKSUMERRORS, /* ChecksumErrors */
  204. SCTP_MIB_OUTCTRLCHUNKS, /* OutCtrlChunks */
  205. SCTP_MIB_OUTORDERCHUNKS, /* OutOrderChunks */
  206. SCTP_MIB_OUTUNORDERCHUNKS, /* OutUnorderChunks */
  207. SCTP_MIB_INCTRLCHUNKS, /* InCtrlChunks */
  208. SCTP_MIB_INORDERCHUNKS, /* InOrderChunks */
  209. SCTP_MIB_INUNORDERCHUNKS, /* InUnorderChunks */
  210. SCTP_MIB_FRAGUSRMSGS, /* FragUsrMsgs */
  211. SCTP_MIB_REASMUSRMSGS, /* ReasmUsrMsgs */
  212. SCTP_MIB_OUTSCTPPACKS, /* OutSCTPPacks */
  213. SCTP_MIB_INSCTPPACKS, /* InSCTPPacks */
  214. SCTP_MIB_T1_INIT_EXPIREDS,
  215. SCTP_MIB_T1_COOKIE_EXPIREDS,
  216. SCTP_MIB_T2_SHUTDOWN_EXPIREDS,
  217. SCTP_MIB_T3_RTX_EXPIREDS,
  218. SCTP_MIB_T4_RTO_EXPIREDS,
  219. SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS,
  220. SCTP_MIB_DELAY_SACK_EXPIREDS,
  221. SCTP_MIB_AUTOCLOSE_EXPIREDS,
  222. SCTP_MIB_T1_RETRANSMITS,
  223. SCTP_MIB_T3_RETRANSMITS,
  224. SCTP_MIB_PMTUD_RETRANSMITS,
  225. SCTP_MIB_FAST_RETRANSMITS,
  226. SCTP_MIB_IN_PKT_SOFTIRQ,
  227. SCTP_MIB_IN_PKT_BACKLOG,
  228. SCTP_MIB_IN_PKT_DISCARDS,
  229. SCTP_MIB_IN_DATA_CHUNK_DISCARDS,
  230. __SCTP_MIB_MAX
  231. };
  232. #define SCTP_MIB_MAX __SCTP_MIB_MAX
  233. struct sctp_mib {
  234. unsigned long mibs[SCTP_MIB_MAX];
  235. };
  236. /* helper function to track stats about max rto and related transport */
  237. static inline void sctp_max_rto(struct sctp_association *asoc,
  238. struct sctp_transport *trans)
  239. {
  240. if (asoc->stats.max_obs_rto < (__u64)trans->rto) {
  241. asoc->stats.max_obs_rto = trans->rto;
  242. memset(&asoc->stats.obs_rto_ipaddr, 0,
  243. sizeof(struct sockaddr_storage));
  244. memcpy(&asoc->stats.obs_rto_ipaddr, &trans->ipaddr,
  245. trans->af_specific->sockaddr_len);
  246. }
  247. }
  248. /*
  249. * Macros for keeping a global reference of object allocations.
  250. */
  251. #ifdef CONFIG_SCTP_DBG_OBJCNT
  252. extern atomic_t sctp_dbg_objcnt_sock;
  253. extern atomic_t sctp_dbg_objcnt_ep;
  254. extern atomic_t sctp_dbg_objcnt_assoc;
  255. extern atomic_t sctp_dbg_objcnt_transport;
  256. extern atomic_t sctp_dbg_objcnt_chunk;
  257. extern atomic_t sctp_dbg_objcnt_bind_addr;
  258. extern atomic_t sctp_dbg_objcnt_bind_bucket;
  259. extern atomic_t sctp_dbg_objcnt_addr;
  260. extern atomic_t sctp_dbg_objcnt_datamsg;
  261. extern atomic_t sctp_dbg_objcnt_keys;
  262. /* Macros to atomically increment/decrement objcnt counters. */
  263. #define SCTP_DBG_OBJCNT_INC(name) \
  264. atomic_inc(&sctp_dbg_objcnt_## name)
  265. #define SCTP_DBG_OBJCNT_DEC(name) \
  266. atomic_dec(&sctp_dbg_objcnt_## name)
  267. #define SCTP_DBG_OBJCNT(name) \
  268. atomic_t sctp_dbg_objcnt_## name = ATOMIC_INIT(0)
  269. /* Macro to help create new entries in the global array of
  270. * objcnt counters.
  271. */
  272. #define SCTP_DBG_OBJCNT_ENTRY(name) \
  273. {.label= #name, .counter= &sctp_dbg_objcnt_## name}
  274. void sctp_dbg_objcnt_init(struct net *);
  275. #else
  276. #define SCTP_DBG_OBJCNT_INC(name)
  277. #define SCTP_DBG_OBJCNT_DEC(name)
  278. static inline void sctp_dbg_objcnt_init(struct net *net) { return; }
  279. #endif /* CONFIG_SCTP_DBG_OBJCOUNT */
  280. #if defined CONFIG_SYSCTL
  281. void sctp_sysctl_register(void);
  282. void sctp_sysctl_unregister(void);
  283. int sctp_sysctl_net_register(struct net *net);
  284. void sctp_sysctl_net_unregister(struct net *net);
  285. #else
  286. static inline void sctp_sysctl_register(void) { return; }
  287. static inline void sctp_sysctl_unregister(void) { return; }
  288. static inline int sctp_sysctl_net_register(struct net *net) { return 0; }
  289. static inline void sctp_sysctl_net_unregister(struct net *net) { return; }
  290. #endif
  291. /* Size of Supported Address Parameter for 'x' address types. */
  292. #define SCTP_SAT_LEN(x) (sizeof(struct sctp_paramhdr) + (x) * sizeof(__u16))
  293. #if IS_ENABLED(CONFIG_IPV6)
  294. void sctp_v6_pf_init(void);
  295. void sctp_v6_pf_exit(void);
  296. int sctp_v6_protosw_init(void);
  297. void sctp_v6_protosw_exit(void);
  298. int sctp_v6_add_protocol(void);
  299. void sctp_v6_del_protocol(void);
  300. #else /* #ifdef defined(CONFIG_IPV6) */
  301. static inline void sctp_v6_pf_init(void) { return; }
  302. static inline void sctp_v6_pf_exit(void) { return; }
  303. static inline int sctp_v6_protosw_init(void) { return 0; }
  304. static inline void sctp_v6_protosw_exit(void) { return; }
  305. static inline int sctp_v6_add_protocol(void) { return 0; }
  306. static inline void sctp_v6_del_protocol(void) { return; }
  307. #endif /* #if defined(CONFIG_IPV6) */
  308. /* Map an association to an assoc_id. */
  309. static inline sctp_assoc_t sctp_assoc2id(const struct sctp_association *asoc)
  310. {
  311. return asoc ? asoc->assoc_id : 0;
  312. }
  313. static inline enum sctp_sstat_state
  314. sctp_assoc_to_state(const struct sctp_association *asoc)
  315. {
  316. /* SCTP's uapi always had SCTP_EMPTY(=0) as a dummy state, but we
  317. * got rid of it in kernel space. Therefore SCTP_CLOSED et al
  318. * start at =1 in user space, but actually as =0 in kernel space.
  319. * Now that we can not break user space and SCTP_EMPTY is exposed
  320. * there, we need to fix it up with an ugly offset not to break
  321. * applications. :(
  322. */
  323. return asoc->state + 1;
  324. }
  325. /* Look up the association by its id. */
  326. struct sctp_association *sctp_id2assoc(struct sock *sk, sctp_assoc_t id);
  327. int sctp_do_peeloff(struct sock *sk, sctp_assoc_t id, struct socket **sockp);
  328. /* A macro to walk a list of skbs. */
  329. #define sctp_skb_for_each(pos, head, tmp) \
  330. skb_queue_walk_safe(head, pos, tmp)
  331. /**
  332. * sctp_list_dequeue - remove from the head of the queue
  333. * @list: list to dequeue from
  334. *
  335. * Remove the head of the list. The head item is
  336. * returned or %NULL if the list is empty.
  337. */
  338. static inline struct list_head *sctp_list_dequeue(struct list_head *list)
  339. {
  340. struct list_head *result = NULL;
  341. if (!list_empty(list)) {
  342. result = list->next;
  343. list_del_init(result);
  344. }
  345. return result;
  346. }
  347. /* SCTP version of skb_set_owner_r. We need this one because
  348. * of the way we have to do receive buffer accounting on bundled
  349. * chunks.
  350. */
  351. static inline void sctp_skb_set_owner_r(struct sk_buff *skb, struct sock *sk)
  352. {
  353. struct sctp_ulpevent *event = sctp_skb2event(skb);
  354. skb_orphan(skb);
  355. skb->sk = sk;
  356. skb->destructor = sctp_sock_rfree;
  357. atomic_add(event->rmem_len, &sk->sk_rmem_alloc);
  358. /*
  359. * This mimics the behavior of skb_set_owner_r
  360. */
  361. sk_mem_charge(sk, event->rmem_len);
  362. }
  363. /* Tests if the list has one and only one entry. */
  364. static inline int sctp_list_single_entry(struct list_head *head)
  365. {
  366. return list_is_singular(head);
  367. }
  368. static inline bool sctp_chunk_pending(const struct sctp_chunk *chunk)
  369. {
  370. return !list_empty(&chunk->list);
  371. }
  372. /* Walk through a list of TLV parameters. Don't trust the
  373. * individual parameter lengths and instead depend on
  374. * the chunk length to indicate when to stop. Make sure
  375. * there is room for a param header too.
  376. */
  377. #define sctp_walk_params(pos, chunk, member)\
  378. _sctp_walk_params((pos), (chunk), ntohs((chunk)->chunk_hdr.length), member)
  379. #define _sctp_walk_params(pos, chunk, end, member)\
  380. for (pos.v = chunk->member;\
  381. (pos.v + offsetof(struct sctp_paramhdr, length) + sizeof(pos.p->length) <=\
  382. (void *)chunk + end) &&\
  383. pos.v <= (void *)chunk + end - ntohs(pos.p->length) &&\
  384. ntohs(pos.p->length) >= sizeof(struct sctp_paramhdr);\
  385. pos.v += SCTP_PAD4(ntohs(pos.p->length)))
  386. #define sctp_walk_errors(err, chunk_hdr)\
  387. _sctp_walk_errors((err), (chunk_hdr), ntohs((chunk_hdr)->length))
  388. #define _sctp_walk_errors(err, chunk_hdr, end)\
  389. for (err = (struct sctp_errhdr *)((void *)chunk_hdr + \
  390. sizeof(struct sctp_chunkhdr));\
  391. ((void *)err + offsetof(struct sctp_errhdr, length) + sizeof(err->length) <=\
  392. (void *)chunk_hdr + end) &&\
  393. (void *)err <= (void *)chunk_hdr + end - ntohs(err->length) &&\
  394. ntohs(err->length) >= sizeof(struct sctp_errhdr); \
  395. err = (struct sctp_errhdr *)((void *)err + SCTP_PAD4(ntohs(err->length))))
  396. #define sctp_walk_fwdtsn(pos, chunk)\
  397. _sctp_walk_fwdtsn((pos), (chunk), ntohs((chunk)->chunk_hdr->length) - sizeof(struct sctp_fwdtsn_chunk))
  398. #define _sctp_walk_fwdtsn(pos, chunk, end)\
  399. for (pos = chunk->subh.fwdtsn_hdr->skip;\
  400. (void *)pos <= (void *)chunk->subh.fwdtsn_hdr->skip + end - sizeof(struct sctp_fwdtsn_skip);\
  401. pos++)
  402. /* External references. */
  403. extern struct proto sctp_prot;
  404. extern struct proto sctpv6_prot;
  405. void sctp_put_port(struct sock *sk);
  406. extern struct idr sctp_assocs_id;
  407. extern spinlock_t sctp_assocs_id_lock;
  408. /* Static inline functions. */
  409. /* Convert from an IP version number to an Address Family symbol. */
  410. static inline int ipver2af(__u8 ipver)
  411. {
  412. switch (ipver) {
  413. case 4:
  414. return AF_INET;
  415. case 6:
  416. return AF_INET6;
  417. default:
  418. return 0;
  419. }
  420. }
  421. /* Convert from an address parameter type to an address family. */
  422. static inline int param_type2af(__be16 type)
  423. {
  424. switch (type) {
  425. case SCTP_PARAM_IPV4_ADDRESS:
  426. return AF_INET;
  427. case SCTP_PARAM_IPV6_ADDRESS:
  428. return AF_INET6;
  429. default:
  430. return 0;
  431. }
  432. }
  433. /* Warning: The following hash functions assume a power of two 'size'. */
  434. /* This is the hash function for the SCTP port hash table. */
  435. static inline int sctp_phashfn(struct net *net, __u16 lport)
  436. {
  437. return (net_hash_mix(net) + lport) & (sctp_port_hashsize - 1);
  438. }
  439. /* This is the hash function for the endpoint hash table. */
  440. static inline int sctp_ep_hashfn(struct net *net, __u16 lport)
  441. {
  442. return (net_hash_mix(net) + lport) & (sctp_ep_hashsize - 1);
  443. }
  444. #define sctp_for_each_hentry(ep, head) \
  445. hlist_for_each_entry(ep, head, node)
  446. /* Is a socket of this style? */
  447. #define sctp_style(sk, style) __sctp_style((sk), (SCTP_SOCKET_##style))
  448. static inline int __sctp_style(const struct sock *sk,
  449. enum sctp_socket_type style)
  450. {
  451. return sctp_sk(sk)->type == style;
  452. }
  453. /* Is the association in this state? */
  454. #define sctp_state(asoc, state) __sctp_state((asoc), (SCTP_STATE_##state))
  455. static inline int __sctp_state(const struct sctp_association *asoc,
  456. enum sctp_state state)
  457. {
  458. return asoc->state == state;
  459. }
  460. /* Is the socket in this state? */
  461. #define sctp_sstate(sk, state) __sctp_sstate((sk), (SCTP_SS_##state))
  462. static inline int __sctp_sstate(const struct sock *sk,
  463. enum sctp_sock_state state)
  464. {
  465. return sk->sk_state == state;
  466. }
  467. /* Map v4-mapped v6 address back to v4 address */
  468. static inline void sctp_v6_map_v4(union sctp_addr *addr)
  469. {
  470. addr->v4.sin_family = AF_INET;
  471. addr->v4.sin_port = addr->v6.sin6_port;
  472. addr->v4.sin_addr.s_addr = addr->v6.sin6_addr.s6_addr32[3];
  473. }
  474. /* Map v4 address to v4-mapped v6 address */
  475. static inline void sctp_v4_map_v6(union sctp_addr *addr)
  476. {
  477. __be16 port;
  478. port = addr->v4.sin_port;
  479. addr->v6.sin6_addr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  480. addr->v6.sin6_port = port;
  481. addr->v6.sin6_family = AF_INET6;
  482. addr->v6.sin6_flowinfo = 0;
  483. addr->v6.sin6_scope_id = 0;
  484. addr->v6.sin6_addr.s6_addr32[0] = 0;
  485. addr->v6.sin6_addr.s6_addr32[1] = 0;
  486. addr->v6.sin6_addr.s6_addr32[2] = htonl(0x0000ffff);
  487. }
  488. /* The cookie is always 0 since this is how it's used in the
  489. * pmtu code.
  490. */
  491. static inline struct dst_entry *sctp_transport_dst_check(struct sctp_transport *t)
  492. {
  493. if (t->dst && !dst_check(t->dst, t->dst_cookie))
  494. sctp_transport_dst_release(t);
  495. return t->dst;
  496. }
  497. /* Calculate max payload size given a MTU, or the total overhead if
  498. * given MTU is zero
  499. */
  500. static inline __u32 __sctp_mtu_payload(const struct sctp_sock *sp,
  501. const struct sctp_transport *t,
  502. __u32 mtu, __u32 extra)
  503. {
  504. __u32 overhead = sizeof(struct sctphdr) + extra;
  505. if (sp) {
  506. overhead += sp->pf->af->net_header_len;
  507. if (sp->udp_port && (!t || t->encap_port))
  508. overhead += sizeof(struct udphdr);
  509. } else {
  510. overhead += sizeof(struct ipv6hdr);
  511. }
  512. if (WARN_ON_ONCE(mtu && mtu <= overhead))
  513. mtu = overhead;
  514. return mtu ? mtu - overhead : overhead;
  515. }
  516. static inline __u32 sctp_mtu_payload(const struct sctp_sock *sp,
  517. __u32 mtu, __u32 extra)
  518. {
  519. return __sctp_mtu_payload(sp, NULL, mtu, extra);
  520. }
  521. static inline __u32 sctp_dst_mtu(const struct dst_entry *dst)
  522. {
  523. return SCTP_TRUNC4(max_t(__u32, dst_mtu(dst),
  524. SCTP_DEFAULT_MINSEGMENT));
  525. }
  526. static inline bool sctp_transport_pmtu_check(struct sctp_transport *t)
  527. {
  528. __u32 pmtu = sctp_dst_mtu(t->dst);
  529. if (t->pathmtu == pmtu)
  530. return true;
  531. t->pathmtu = pmtu;
  532. return false;
  533. }
  534. static inline __u32 sctp_min_frag_point(struct sctp_sock *sp, __u16 datasize)
  535. {
  536. return sctp_mtu_payload(sp, SCTP_DEFAULT_MINSEGMENT, datasize);
  537. }
  538. static inline int sctp_transport_pl_hlen(struct sctp_transport *t)
  539. {
  540. return __sctp_mtu_payload(sctp_sk(t->asoc->base.sk), t, 0, 0) -
  541. sizeof(struct sctphdr);
  542. }
  543. static inline void sctp_transport_pl_reset(struct sctp_transport *t)
  544. {
  545. if (t->probe_interval && (t->param_flags & SPP_PMTUD_ENABLE) &&
  546. (t->state == SCTP_ACTIVE || t->state == SCTP_UNKNOWN)) {
  547. if (t->pl.state == SCTP_PL_DISABLED) {
  548. t->pl.state = SCTP_PL_BASE;
  549. t->pl.pmtu = SCTP_BASE_PLPMTU;
  550. t->pl.probe_size = SCTP_BASE_PLPMTU;
  551. sctp_transport_reset_probe_timer(t);
  552. }
  553. } else {
  554. if (t->pl.state != SCTP_PL_DISABLED) {
  555. if (del_timer(&t->probe_timer))
  556. sctp_transport_put(t);
  557. t->pl.state = SCTP_PL_DISABLED;
  558. }
  559. }
  560. }
  561. static inline void sctp_transport_pl_update(struct sctp_transport *t)
  562. {
  563. if (t->pl.state == SCTP_PL_DISABLED)
  564. return;
  565. t->pl.state = SCTP_PL_BASE;
  566. t->pl.pmtu = SCTP_BASE_PLPMTU;
  567. t->pl.probe_size = SCTP_BASE_PLPMTU;
  568. sctp_transport_reset_probe_timer(t);
  569. }
  570. static inline bool sctp_transport_pl_enabled(struct sctp_transport *t)
  571. {
  572. return t->pl.state != SCTP_PL_DISABLED;
  573. }
  574. static inline bool sctp_newsk_ready(const struct sock *sk)
  575. {
  576. return sock_flag(sk, SOCK_DEAD) || sk->sk_socket;
  577. }
  578. static inline void sctp_sock_set_nodelay(struct sock *sk)
  579. {
  580. lock_sock(sk);
  581. sctp_sk(sk)->nodelay = true;
  582. release_sock(sk);
  583. }
  584. #endif /* __net_sctp_h__ */