sctp.h 20 KB

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