xref: /titanic_51/usr/src/uts/common/inet/udp_impl.h (revision 861a91627796c35220e75654dac61e5707536dcd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_UDP_IMPL_H
27 #define	_UDP_IMPL_H
28 
29 /*
30  * UDP implementation private declarations.  These interfaces are
31  * used to build the IP module and are not meant to be accessed
32  * by any modules except IP itself.  They are undocumented and are
33  * subject to change without notice.
34  */
35 
36 #ifdef	__cplusplus
37 extern "C" {
38 #endif
39 
40 #ifdef _KERNEL
41 
42 #include <sys/int_types.h>
43 #include <sys/netstack.h>
44 
45 #include <netinet/in.h>
46 #include <netinet/ip6.h>
47 
48 #include <inet/common.h>
49 #include <inet/ip.h>
50 #include <inet/optcom.h>
51 #include <inet/tunables.h>
52 
53 #define	UDP_MOD_ID		5607
54 
55 /*
56  * Bind hash list size and hash function.  It has to be a power of 2 for
57  * hashing.
58  */
59 #define	UDP_BIND_FANOUT_SIZE	512
60 #define	UDP_BIND_HASH(lport, size) \
61 	((ntohs((uint16_t)lport)) & (size - 1))
62 
63 /* UDP bind fanout hash structure. */
64 typedef struct udp_fanout_s {
65 	struct udp_s *uf_udp;
66 	kmutex_t uf_lock;
67 #if defined(_LP64) || defined(_I32LPx)
68 	char	uf_pad[48];
69 #else
70 	char	uf_pad[56];
71 #endif
72 } udp_fanout_t;
73 
74 /* Kstats */
75 typedef struct udp_stat {			/* Class "net" kstats */
76 	kstat_named_t	udp_sock_fallback;
77 	kstat_named_t	udp_out_opt;
78 	kstat_named_t	udp_out_err_notconn;
79 	kstat_named_t	udp_out_err_output;
80 	kstat_named_t	udp_out_err_tudr;
81 #ifdef DEBUG
82 	kstat_named_t	udp_data_conn;
83 	kstat_named_t	udp_data_notconn;
84 	kstat_named_t	udp_out_lastdst;
85 	kstat_named_t	udp_out_diffdst;
86 	kstat_named_t	udp_out_ipv6;
87 	kstat_named_t	udp_out_mapped;
88 	kstat_named_t	udp_out_ipv4;
89 #endif
90 
91 } udp_stat_t;
92 
93 #define	UDP_NUM_EPRIV_PORTS	64
94 
95 /* Default buffer size and flow control wake up threshold. */
96 #define	UDP_RECV_HIWATER	(56 * 1024)
97 #define	UDP_RECV_LOWATER	128
98 #define	UDP_XMIT_HIWATER	(56 * 1024)
99 #define	UDP_XMIT_LOWATER	1024
100 
101 /*
102  * UDP stack instances
103  */
104 struct udp_stack {
105 	netstack_t	*us_netstack;	/* Common netstack */
106 
107 	uint_t		us_bind_fanout_size;
108 	udp_fanout_t	*us_bind_fanout;
109 
110 	int		us_num_epriv_ports;
111 	in_port_t	us_epriv_ports[UDP_NUM_EPRIV_PORTS];
112 	kmutex_t	us_epriv_port_lock;
113 
114 	/* Hint not protected by any lock */
115 	in_port_t	us_next_port_to_try;
116 
117 	/* UDP tunables table */
118 	struct mod_prop_info_s	*us_propinfo_tbl;
119 
120 	kstat_t		*us_mibkp;	/* kstats exporting mib data */
121 	kstat_t		*us_kstat;
122 	udp_stat_t	us_statistics;
123 
124 	mib2_udp_t	us_udp_mib;	/* SNMP fixed size info */
125 
126 /*
127  * The smallest anonymous port in the priviledged port range which UDP
128  * looks for free port.  Use in the option UDP_ANONPRIVBIND.
129  */
130 	in_port_t	us_min_anonpriv_port;
131 
132 	ldi_ident_t	us_ldi_ident;
133 };
134 
135 typedef struct udp_stack udp_stack_t;
136 
137 /* Internal udp control structure, one per open stream */
138 typedef	struct udp_s {
139 	/*
140 	 * The addresses and ports in the conn_t and udp_state are protected by
141 	 * conn_lock and the fanout lock i.e. uf_lock. Need both locks to change
142 	 * the fields, either lock is sufficient for reading the field.
143 	 * conn_lock also protects the content of udp_t.
144 	 */
145 	uint32_t	udp_state;	/* TPI state */
146 
147 	ip_pkt_t	udp_recv_ipp;	/* Used for IPv4 options received */
148 
149 	/* Written to only once at the time of opening the endpoint */
150 	conn_t		*udp_connp;
151 
152 	uint32_t
153 		udp_issocket : 1,	/* socket mode; sockfs is on top */
154 		udp_nat_t_endpoint : 1,	/* UDP_NAT_T_ENDPOINT option */
155 		udp_rcvhdr : 1,		/* UDP_RCVHDR option */
156 
157 		udp_pad_to_bit_31 : 29;
158 
159 	/* Following 2 fields protected by the uf_lock */
160 	struct udp_s	*udp_bind_hash; /* Bind hash chain */
161 	struct udp_s	**udp_ptpbhn; /* Pointer to previous bind hash next. */
162 
163 	kmutex_t	udp_recv_lock;		/* recv lock */
164 	size_t		udp_rcv_disply_hiwat;	/* user's view of rcvbuf */
165 	size_t		udp_rcv_hiwat;		/* receive high watermark */
166 
167 	/* Set at open time and never changed */
168 	udp_stack_t	*udp_us;		/* Stack instance for zone */
169 
170 	int		udp_delayed_error;
171 	mblk_t		*udp_fallback_queue_head;
172 	mblk_t		*udp_fallback_queue_tail;
173 	struct sockaddr_storage	udp_delayed_addr;
174 } udp_t;
175 
176 /* UDP Protocol header aligned */
177 typedef	struct udpahdr_s {
178 	in_port_t	uha_src_port;		/* Source port */
179 	in_port_t	uha_dst_port;		/* Destination port */
180 	uint16_t	uha_length;		/* UDP length */
181 	uint16_t	uha_checksum;		/* UDP checksum */
182 } udpha_t;
183 
184 #define	us_wroff_extra			us_propinfo_tbl[0].prop_cur_uval
185 #define	us_ipv4_ttl			us_propinfo_tbl[1].prop_cur_uval
186 #define	us_ipv6_hoplimit		us_propinfo_tbl[2].prop_cur_uval
187 #define	us_smallest_nonpriv_port	us_propinfo_tbl[3].prop_cur_uval
188 #define	us_do_checksum			us_propinfo_tbl[4].prop_cur_bval
189 #define	us_smallest_anon_port		us_propinfo_tbl[5].prop_cur_uval
190 #define	us_largest_anon_port		us_propinfo_tbl[6].prop_cur_uval
191 #define	us_xmit_hiwat			us_propinfo_tbl[7].prop_cur_uval
192 #define	us_xmit_lowat			us_propinfo_tbl[8].prop_cur_uval
193 #define	us_recv_hiwat			us_propinfo_tbl[9].prop_cur_uval
194 #define	us_max_buf			us_propinfo_tbl[10].prop_cur_uval
195 #define	us_pmtu_discovery		us_propinfo_tbl[11].prop_cur_bval
196 #define	us_sendto_ignerr		us_propinfo_tbl[12].prop_cur_bval
197 
198 #define	UDP_STAT(us, x)		((us)->us_statistics.x.value.ui64++)
199 #define	UDP_STAT_UPDATE(us, x, n)	\
200 			((us)->us_statistics.x.value.ui64 += (n))
201 #ifdef DEBUG
202 #define	UDP_DBGSTAT(us, x)	UDP_STAT(us, x)
203 #else
204 #define	UDP_DBGSTAT(us, x)
205 #endif /* DEBUG */
206 
207 extern int	udp_opt_default(queue_t *, t_scalar_t, t_scalar_t, uchar_t *);
208 extern int	udp_tpi_opt_get(queue_t *, t_scalar_t, t_scalar_t, uchar_t *);
209 extern int	udp_tpi_opt_set(queue_t *, uint_t, int, int, uint_t, uchar_t *,
210 		    uint_t *, uchar_t *, void *, cred_t *);
211 extern mblk_t	*udp_snmp_get(queue_t *, mblk_t *);
212 extern int	udp_snmp_set(queue_t *, t_scalar_t, t_scalar_t, uchar_t *, int);
213 extern void	udp_ddi_g_init(void);
214 extern void	udp_ddi_g_destroy(void);
215 extern void	udp_output(conn_t *connp, mblk_t *mp, struct sockaddr *addr,
216 		    socklen_t addrlen);
217 extern void	udp_wput(queue_t *, mblk_t *);
218 
219 /*
220  * Object to represent database of options to search passed to
221  * {sock,tpi}optcom_req() interface routine to take care of option
222  * management and associated methods.
223  */
224 extern optdb_obj_t	udp_opt_obj;
225 extern uint_t		udp_max_optsize;
226 
227 extern sock_lower_handle_t udp_create(int, int, int, sock_downcalls_t **,
228     uint_t *, int *, int, cred_t *);
229 extern int udp_fallback(sock_lower_handle_t, queue_t *, boolean_t,
230     so_proto_quiesced_cb_t);
231 
232 extern sock_downcalls_t sock_udp_downcalls;
233 
234 #endif	/*  _KERNEL */
235 
236 #ifdef	__cplusplus
237 }
238 #endif
239 
240 #endif	/* _UDP_IMPL_H */
241