xref: /titanic_51/usr/src/uts/common/inet/udp_impl.h (revision cbdcbd056f15c9c9fd82d5543b3a502677c4d391)
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 2009 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 
52 #define	UDP_MOD_ID		5607
53 
54 /*
55  * Bind hash list size and hash function.  It has to be a power of 2 for
56  * hashing.
57  */
58 #define	UDP_BIND_FANOUT_SIZE	512
59 #define	UDP_BIND_HASH(lport, size) \
60 	((ntohs((uint16_t)lport)) & (size - 1))
61 
62 /* UDP bind fanout hash structure. */
63 typedef struct udp_fanout_s {
64 	struct udp_s *uf_udp;
65 	kmutex_t uf_lock;
66 #if defined(_LP64) || defined(_I32LPx)
67 	char	uf_pad[48];
68 #else
69 	char	uf_pad[56];
70 #endif
71 } udp_fanout_t;
72 
73 /* Kstats */
74 typedef struct udp_stat {			/* Class "net" kstats */
75 	kstat_named_t	udp_sock_fallback;
76 	kstat_named_t	udp_out_opt;
77 	kstat_named_t	udp_out_err_notconn;
78 	kstat_named_t	udp_out_err_output;
79 	kstat_named_t	udp_out_err_tudr;
80 #ifdef DEBUG
81 	kstat_named_t	udp_data_conn;
82 	kstat_named_t	udp_data_notconn;
83 	kstat_named_t	udp_out_lastdst;
84 	kstat_named_t	udp_out_diffdst;
85 	kstat_named_t	udp_out_ipv6;
86 	kstat_named_t	udp_out_mapped;
87 	kstat_named_t	udp_out_ipv4;
88 #endif
89 
90 } udp_stat_t;
91 
92 /* Named Dispatch Parameter Management Structure */
93 typedef struct udpparam_s {
94 	uint32_t udp_param_min;
95 	uint32_t udp_param_max;
96 	uint32_t udp_param_value;
97 	char	*udp_param_name;
98 } udpparam_t;
99 
100 #define	UDP_NUM_EPRIV_PORTS	64
101 
102 /*
103  * UDP stack instances
104  */
105 struct udp_stack {
106 	netstack_t	*us_netstack;	/* Common netstack */
107 
108 	uint_t		us_bind_fanout_size;
109 	udp_fanout_t	*us_bind_fanout;
110 
111 	int		us_num_epriv_ports;
112 	in_port_t	us_epriv_ports[UDP_NUM_EPRIV_PORTS];
113 
114 	/* Hint not protected by any lock */
115 	in_port_t	us_next_port_to_try;
116 
117 	IDP		us_nd;	/* Points to table of UDP ND variables. */
118 	udpparam_t	*us_param_arr; 	/* ndd variable table */
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_param_arr[0].udp_param_value
185 #define	us_ipv4_ttl			us_param_arr[1].udp_param_value
186 #define	us_ipv6_hoplimit		us_param_arr[2].udp_param_value
187 #define	us_smallest_nonpriv_port	us_param_arr[3].udp_param_value
188 #define	us_do_checksum			us_param_arr[4].udp_param_value
189 #define	us_smallest_anon_port		us_param_arr[5].udp_param_value
190 #define	us_largest_anon_port		us_param_arr[6].udp_param_value
191 #define	us_xmit_hiwat			us_param_arr[7].udp_param_value
192 #define	us_xmit_lowat			us_param_arr[8].udp_param_value
193 #define	us_recv_hiwat			us_param_arr[9].udp_param_value
194 #define	us_max_buf			us_param_arr[10].udp_param_value
195 #define	us_pmtu_discovery		us_param_arr[11].udp_param_value
196 #define	us_sendto_ignerr		us_param_arr[12].udp_param_value
197 
198 
199 #define	UDP_STAT(us, x)		((us)->us_statistics.x.value.ui64++)
200 #define	UDP_STAT_UPDATE(us, x, n)	\
201 			((us)->us_statistics.x.value.ui64 += (n))
202 #ifdef DEBUG
203 #define	UDP_DBGSTAT(us, x)	UDP_STAT(us, x)
204 #else
205 #define	UDP_DBGSTAT(us, x)
206 #endif /* DEBUG */
207 
208 extern int	udp_opt_default(queue_t *, t_scalar_t, t_scalar_t, uchar_t *);
209 extern int	udp_tpi_opt_get(queue_t *, t_scalar_t, t_scalar_t, uchar_t *);
210 extern int	udp_tpi_opt_set(queue_t *, uint_t, int, int, uint_t, uchar_t *,
211 		    uint_t *, uchar_t *, void *, cred_t *);
212 extern mblk_t	*udp_snmp_get(queue_t *, mblk_t *);
213 extern int	udp_snmp_set(queue_t *, t_scalar_t, t_scalar_t, uchar_t *, int);
214 extern void	udp_ddi_g_init(void);
215 extern void	udp_ddi_g_destroy(void);
216 extern void	udp_output(conn_t *connp, mblk_t *mp, struct sockaddr *addr,
217 		    socklen_t addrlen);
218 extern void	udp_wput(queue_t *, mblk_t *);
219 
220 /*
221  * Object to represent database of options to search passed to
222  * {sock,tpi}optcom_req() interface routine to take care of option
223  * management and associated methods.
224  */
225 extern optdb_obj_t	udp_opt_obj;
226 extern uint_t		udp_max_optsize;
227 
228 extern sock_lower_handle_t udp_create(int, int, int, sock_downcalls_t **,
229     uint_t *, int *, int, cred_t *);
230 extern int udp_fallback(sock_lower_handle_t, queue_t *, boolean_t,
231     so_proto_quiesced_cb_t);
232 
233 extern sock_downcalls_t sock_udp_downcalls;
234 
235 #endif	/*  _KERNEL */
236 
237 #ifdef	__cplusplus
238 }
239 #endif
240 
241 #endif	/* _UDP_IMPL_H */
242