summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/inet/proto_set.c
blob: 499f046f6dfa9727eddc7874746a209279eae12d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#include <sys/types.h>
#include <inet/common.h>
#include <sys/stream.h>
#include <sys/stropts.h>
#include <sys/strsun.h>
#include <sys/sysmacros.h>
#include <sys/stropts.h>
#include <sys/strsubr.h>
#include <sys/tpicommon.h>
#include <sys/socket_proto.h>
#include <sys/policy.h>
#include <inet/optcom.h>
#include <inet/ipclassifier.h>

boolean_t
proto_set_rx_hiwat(queue_t *q, conn_t *connp, size_t size)
{

	if (connp != NULL && IPCL_IS_NONSTR(connp)) {
		struct sock_proto_props sopp;

		sopp.sopp_flags = SOCKOPT_RCVHIWAT;
		sopp.sopp_rxhiwat = size;
		(*connp->conn_upcalls->su_set_proto_props)
		    (connp->conn_upper_handle, &sopp);
	} else {
		MBLKP	mp;
		struct stroptions *stropt;

		if (!(mp = allocb(sizeof (*stropt), BPRI_LO)))
			return (B_FALSE);
		mp->b_datap->db_type = M_SETOPTS;
		mp->b_wptr += sizeof (*stropt);
		stropt = (struct stroptions *)mp->b_rptr;
		stropt->so_flags = SO_HIWAT;
		stropt->so_hiwat = size;
		putnext(q, mp);
	}
	return (B_TRUE);
}

boolean_t
proto_set_rx_lowat(queue_t *q, conn_t *connp, size_t size)
{

	if (connp != NULL && IPCL_IS_NONSTR(connp)) {
		struct sock_proto_props sopp;

		sopp.sopp_flags = SOCKOPT_RCVLOWAT;
		sopp.sopp_rxlowat = size;
		(*connp->conn_upcalls->su_set_proto_props)
		    (connp->conn_upper_handle, &sopp);
	} else {
		MBLKP	mp;
		struct stroptions *stropt;

		if (!(mp = allocb(sizeof (*stropt), BPRI_LO)))
			return (B_FALSE);
		mp->b_datap->db_type = M_SETOPTS;
		mp->b_wptr += sizeof (*stropt);
		stropt = (struct stroptions *)mp->b_rptr;
		stropt->so_flags = SO_LOWAT;
		stropt->so_lowat = size;
		putnext(q, mp);
	}
	return (B_TRUE);
}

/*
 * Set maximum packet size. This is the maximum amount of data the protocol
 * wants to be given at any time, Larger data needs to be broken in multiples
 * of maximum packet size and given to the protocol one at a time.
 */
boolean_t
proto_set_maxpsz(queue_t *q, conn_t *connp, size_t size)
{
	if (connp != NULL && IPCL_IS_NONSTR(connp)) {
		struct sock_proto_props sopp;

		sopp.sopp_flags = SOCKOPT_MAXPSZ;
		sopp.sopp_maxpsz = size;
		(*connp->conn_upcalls->su_set_proto_props)
		    (connp->conn_upper_handle, &sopp);
		return (B_TRUE);
	} else {
		struct stdata	*stp;
		queue_t		*wq;
		stp = STREAM(q);

		/*
		 * At this point change of a queue parameter is not allowed
		 * when a multiplexor is sitting on top.
		 */
		if (stp == NULL || stp->sd_flag & STPLEX)
			return (B_FALSE);

		claimstr(stp->sd_wrq);
		wq = stp->sd_wrq->q_next;
		ASSERT(wq != NULL);
		(void) strqset(wq, QMAXPSZ, 0, size);
		releasestr(stp->sd_wrq);
		return (B_TRUE);
	}
}

/* ARGSUSED */
boolean_t
proto_set_tx_maxblk(queue_t *q, conn_t *connp, ssize_t size)
{
	if (connp != NULL && IPCL_IS_NONSTR(connp)) {
		struct sock_proto_props sopp;

		sopp.sopp_flags = SOCKOPT_MAXBLK;
		sopp.sopp_maxblk = size;
		(*connp->conn_upcalls->su_set_proto_props)
		    (connp->conn_upper_handle, &sopp);
	} else {
		MBLKP	mp;
		struct stroptions *stropt;

		if (!(mp = allocb(sizeof (*stropt), BPRI_LO)))
			return (B_FALSE);
		mp->b_datap->db_type = M_SETOPTS;
		mp->b_wptr += sizeof (*stropt);
		stropt = (struct stroptions *)mp->b_rptr;
		stropt->so_flags = SO_MAXBLK;
		stropt->so_maxblk = size;
		putnext(q, mp);
	}
	return (B_TRUE);
}

boolean_t
proto_set_tx_copyopt(queue_t *q, conn_t *connp, int copyopt)
{
	if (connp != NULL && IPCL_IS_NONSTR(connp)) {
		struct sock_proto_props sopp;

		sopp.sopp_flags = SOCKOPT_ZCOPY;
		sopp.sopp_zcopyflag = (ushort_t)copyopt;
		(*connp->conn_upcalls->su_set_proto_props)
		    (connp->conn_upper_handle, &sopp);
	} else {
		MBLKP	mp;
		struct stroptions *stropt;

		if (!(mp = allocb(sizeof (*stropt), BPRI_LO)))
			return (B_FALSE);
		mp->b_datap->db_type = M_SETOPTS;
		mp->b_wptr += sizeof (*stropt);
		stropt = (struct stroptions *)mp->b_rptr;
		stropt->so_flags = SO_COPYOPT;
		stropt->so_copyopt = (ushort_t)copyopt;
		putnext(q, mp);
	}
	return (B_TRUE);
}

boolean_t
proto_set_tx_wroff(queue_t *q, conn_t *connp, size_t size)
{
	if (connp != NULL && IPCL_IS_NONSTR(connp)) {
		struct sock_proto_props sopp;

		sopp.sopp_flags = SOCKOPT_WROFF;
		sopp.sopp_wroff = size;

		/* XXX workaround for CR6757374 */
		if (connp->conn_upper_handle != NULL)
			(*connp->conn_upcalls->su_set_proto_props)
			    (connp->conn_upper_handle, &sopp);
	} else {

		MBLKP	mp;
		struct stroptions *stropt;
		if (!(mp = allocb(sizeof (*stropt), BPRI_LO)))
			return (B_FALSE);
		mp->b_datap->db_type = M_SETOPTS;
		mp->b_wptr += sizeof (*stropt);
		stropt = (struct stroptions *)mp->b_rptr;
		stropt->so_flags = SO_WROFF;
		stropt->so_wroff = (ushort_t)size;
		putnext(q, mp);
	}
	return (B_TRUE);
}

/*
 * set OOBINLINE processing on the socket
 */
void
proto_set_rx_oob_opt(conn_t *connp, boolean_t onoff)
{
	struct sock_proto_props sopp;

	ASSERT(IPCL_IS_NONSTR(connp));

	sopp.sopp_flags = SOCKOPT_OOBINLINE;
	sopp.sopp_oobinline = onoff;
	(*connp->conn_upcalls->su_set_proto_props)
	    (connp->conn_upper_handle, &sopp);
}

/*
 * Translate a TLI(/XTI) error into a system error as best we can.
 */
static const int tli_errs[] = {
		0,		/* no error	*/
		EADDRNOTAVAIL,  /* TBADADDR	*/
		ENOPROTOOPT,	/* TBADOPT	*/
		EACCES,		/* TACCES	*/
		EBADF,		/* TBADF	*/
		EADDRNOTAVAIL,	/* TNOADDR	*/
		EPROTO,		/* TOUTSTATE	*/
		ECONNABORTED,	/* TBADSEQ	*/
		0,		/* TSYSERR - will never get	*/
		EPROTO,		/* TLOOK - should never be sent by transport */
		EMSGSIZE,	/* TBADDATA	*/
		EMSGSIZE,	/* TBUFOVFLW	*/
		EPROTO,		/* TFLOW	*/
		EWOULDBLOCK,	/* TNODATA	*/
		EPROTO,		/* TNODIS	*/
		EPROTO,		/* TNOUDERR	*/
		EINVAL,		/* TBADFLAG	*/
		EPROTO,		/* TNOREL	*/
		EOPNOTSUPP,	/* TNOTSUPPORT	*/
		EPROTO,		/* TSTATECHNG	*/
		/* following represent error namespace expansion with XTI */
		EPROTO,		/* TNOSTRUCTYPE - never sent by transport */
		EPROTO,		/* TBADNAME - never sent by transport */
		EPROTO,		/* TBADQLEN - never sent by transport */
		EADDRINUSE,	/* TADDRBUSY	*/
		EBADF,		/* TINDOUT	*/
		EBADF,		/* TPROVMISMATCH */
		EBADF,		/* TRESQLEN	*/
		EBADF,		/* TRESADDR	*/
		EPROTO,		/* TQFULL - never sent by transport */
		EPROTO,		/* TPROTO	*/
};

int
proto_tlitosyserr(int terr)
{
	ASSERT(terr != TSYSERR);
	if (terr >= (sizeof (tli_errs) / sizeof (tli_errs[0])))
		return (EPROTO);
	else
		return (tli_errs[terr]);
}

/*
 * Verify that address is suitable for connect/sendmsg and is aligned properly
 * Since this is a generic function we do not test for port being zero
 * as some protocols like icmp do not require a port
 */
int
proto_verify_ip_addr(int family, const struct sockaddr *name, socklen_t namelen)
{

	if (name == NULL || !OK_32PTR((char *)name))
		return (EINVAL);

	switch (family) {
	case AF_INET:
		if (name->sa_family != AF_INET) {
			return (EAFNOSUPPORT);
		}

		if (namelen != (socklen_t)sizeof (struct sockaddr_in)) {
			return (EINVAL);
		}
		break;
	case AF_INET6: {
#ifdef DEBUG
		struct sockaddr_in6 *sin6;
#endif /* DEBUG */

		if (name->sa_family != AF_INET6) {
			return (EAFNOSUPPORT);
		}
		if (namelen != (socklen_t)sizeof (struct sockaddr_in6)) {
			return (EINVAL);
		}
#ifdef DEBUG
		/* Verify that apps don't forget to clear sin6_scope_id etc */
		sin6 = (struct sockaddr_in6 *)name;
		if (sin6->sin6_scope_id != 0 &&
		    !IN6_IS_ADDR_LINKSCOPE(&sin6->sin6_addr)) {
			zcmn_err(getzoneid(), CE_WARN,
			    "connect/send* with uninitialized sin6_scope_id "
			    "(%d) on socket. Pid = %d\n",
			    (int)sin6->sin6_scope_id, (int)curproc->p_pid);
		}
#endif /* DEBUG */
		break;
	}
	default:
		return (EINVAL);
	}

	return (0);
}

/*
 * Do a lookup of the options in the array.
 * Rerurn NULL if there isn't a match.
 */
opdes_t *
proto_opt_lookup(t_uscalar_t level, t_uscalar_t name, opdes_t *opt_arr,
    uint_t opt_arr_cnt)
{
	opdes_t		*optd;

	for (optd = opt_arr; optd < &opt_arr[opt_arr_cnt];
	    optd++) {
		if (level == (uint_t)optd->opdes_level &&
		    name == (uint_t)optd->opdes_name)
			return (optd);
	}
	return (NULL);
}

/*
 * Do a lookup of the options in the array and do permission and length checking
 * Returns zero if there is no error (note: for non-tpi-providers not being able
 * to find the option is not an error). TPI errors are returned as negative
 * numbers and errnos as positive numbers.
 * If max_len is set we update it based on the max length of the option.
 */
int
proto_opt_check(int level, int name, int len, t_uscalar_t *max_len,
    opdes_t *opt_arr, uint_t opt_arr_cnt, boolean_t negotiate, boolean_t check,
    cred_t *cr)
{
	opdes_t *optd;

	/* Find the option in the opt_arr. */
	optd = proto_opt_lookup(level, name, opt_arr, opt_arr_cnt);
	if (optd == NULL)
		return (-TBADOPT);

	/* Additional checks dependent on operation. */
	if (negotiate) {
		/* Cannot be true at the same time */
		ASSERT(check == B_FALSE);

		if (!OA_WRITE_OR_EXECUTE(optd, cr)) {
			/* can't negotiate option */
			if (!(OA_MATCHED_PRIV(optd, cr)) &&
			    OA_WX_ANYPRIV(optd)) {
				/*
				 * not privileged but privilege
				 * will help negotiate option.
				 */
				return (-TACCES);
			} else {
				return (-TBADOPT);
			}
		}
		/*
		 * Verify size for options
		 * Note: For retaining compatibility with historical
		 * behavior, variable lengths options will have their
		 * length verified in the setfn() processing.
		 * In order to be compatible with SunOS 4.X we return
		 * EINVAL errors for bad lengths.
		 */
		if (!(optd->opdes_props & OP_VARLEN)) {
			/* fixed length - size must match */
			if (len != optd->opdes_size) {
				return (EINVAL);
			}
		}
	} else {
		if (check) {
			if (!OA_RWX_ANYPRIV(optd))
				/* any of "rwx" permission but not none */
				return (-TBADOPT);
		}
		/*
		 * XXX Since T_CURRENT was not there in TLI and the
		 * official TLI inspired TPI standard, getsockopt()
		 * API uses T_CHECK (for T_CURRENT semantics)
		 * Thus T_CHECK includes the T_CURRENT semantics due to that
		 * historical use.
		 */
		if (!OA_READ_PERMISSION(optd, cr)) {
			/* can't read option value */
			if (!(OA_MATCHED_PRIV(optd, cr)) &&
			    OA_R_ANYPRIV(optd)) {
				/*
				 * not privileged but privilege
				 * will help in reading option value.
				 */
				return (-TACCES);
			} else {
				return (-TBADOPT);
			}
		}
	}
	if (max_len != NULL)
		*max_len = optd->opdes_size;

	/* We liked it.  Keep going. */
	return (0);
}