sctp.h 20 KB

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