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