Print this page
10096 kstat update routines shouldn't check for NULL kstat
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/inet/ip/icmp.c
+++ new/usr/src/uts/common/inet/ip/icmp.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
↓ open down ↓ |
14 lines elided |
↑ open up ↑ |
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21 /*
22 22 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
23 23 * Copyright (c) 2013 by Delphix. All rights reserved.
24 24 * Copyright 2014, OmniTI Computer Consulting, Inc. All rights reserved.
25 + * Copyright (c) 2018, Joyent, Inc.
25 26 */
26 27 /* Copyright (c) 1990 Mentat Inc. */
27 28
28 29 #include <sys/types.h>
29 30 #include <sys/stream.h>
30 31 #include <sys/stropts.h>
31 32 #include <sys/strlog.h>
32 33 #include <sys/strsun.h>
33 34 #define _SUN_TPI_VERSION 2
34 35 #include <sys/tihdr.h>
35 36 #include <sys/timod.h>
36 37 #include <sys/ddi.h>
37 38 #include <sys/sunddi.h>
38 39 #include <sys/strsubr.h>
39 40 #include <sys/suntpi.h>
40 41 #include <sys/xti_inet.h>
41 42 #include <sys/cmn_err.h>
42 43 #include <sys/kmem.h>
43 44 #include <sys/cred.h>
44 45 #include <sys/policy.h>
45 46 #include <sys/priv.h>
46 47 #include <sys/ucred.h>
47 48 #include <sys/zone.h>
48 49
49 50 #include <sys/sockio.h>
50 51 #include <sys/socket.h>
51 52 #include <sys/socketvar.h>
52 53 #include <sys/vtrace.h>
53 54 #include <sys/sdt.h>
54 55 #include <sys/debug.h>
55 56 #include <sys/isa_defs.h>
56 57 #include <sys/random.h>
57 58 #include <netinet/in.h>
58 59 #include <netinet/ip6.h>
59 60 #include <netinet/icmp6.h>
60 61 #include <netinet/udp.h>
61 62
62 63 #include <inet/common.h>
63 64 #include <inet/ip.h>
64 65 #include <inet/ip_impl.h>
65 66 #include <inet/ipsec_impl.h>
66 67 #include <inet/ip6.h>
67 68 #include <inet/ip_ire.h>
68 69 #include <inet/ip_if.h>
69 70 #include <inet/ip_multi.h>
70 71 #include <inet/ip_ndp.h>
71 72 #include <inet/proto_set.h>
72 73 #include <inet/mib2.h>
73 74 #include <inet/nd.h>
74 75 #include <inet/optcom.h>
75 76 #include <inet/snmpcom.h>
76 77 #include <inet/kstatcom.h>
77 78 #include <inet/ipclassifier.h>
78 79
79 80 #include <sys/tsol/label.h>
80 81 #include <sys/tsol/tnet.h>
81 82
82 83 #include <inet/rawip_impl.h>
83 84
84 85 #include <sys/disp.h>
85 86
86 87 /*
87 88 * Synchronization notes:
88 89 *
89 90 * RAWIP is MT and uses the usual kernel synchronization primitives. We use
90 91 * conn_lock to protect the icmp_t.
91 92 *
92 93 * Plumbing notes:
93 94 * ICMP is always a device driver. For compatibility with mibopen() code
94 95 * it is possible to I_PUSH "icmp", but that results in pushing a passthrough
95 96 * dummy module.
96 97 */
97 98 static void icmp_addr_req(queue_t *q, mblk_t *mp);
98 99 static void icmp_tpi_bind(queue_t *q, mblk_t *mp);
99 100 static void icmp_bind_proto(icmp_t *icmp);
100 101 static int icmp_build_hdr_template(conn_t *, const in6_addr_t *,
101 102 const in6_addr_t *, uint32_t);
102 103 static void icmp_capability_req(queue_t *q, mblk_t *mp);
103 104 static int icmp_close(queue_t *q, int flags, cred_t *);
104 105 static void icmp_close_free(conn_t *);
105 106 static void icmp_tpi_connect(queue_t *q, mblk_t *mp);
106 107 static void icmp_tpi_disconnect(queue_t *q, mblk_t *mp);
107 108 static void icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error,
108 109 int sys_error);
109 110 static void icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
110 111 t_scalar_t tlierr, int sys_error);
111 112 static void icmp_icmp_input(void *arg1, mblk_t *mp, void *arg2,
112 113 ip_recv_attr_t *);
113 114 static void icmp_icmp_error_ipv6(conn_t *connp, mblk_t *mp,
114 115 ip_recv_attr_t *);
115 116 static void icmp_info_req(queue_t *q, mblk_t *mp);
116 117 static void icmp_input(void *, mblk_t *, void *, ip_recv_attr_t *);
117 118 static conn_t *icmp_open(int family, cred_t *credp, int *err, int flags);
118 119 static int icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag,
119 120 cred_t *credp);
120 121 static int icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag,
121 122 cred_t *credp);
122 123 static boolean_t icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name);
123 124 int icmp_opt_set(conn_t *connp, uint_t optset_context,
124 125 int level, int name, uint_t inlen,
125 126 uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
126 127 void *thisdg_attrs, cred_t *cr);
127 128 int icmp_opt_get(conn_t *connp, int level, int name,
128 129 uchar_t *ptr);
129 130 static int icmp_output_newdst(conn_t *connp, mblk_t *data_mp, sin_t *sin,
130 131 sin6_t *sin6, cred_t *cr, pid_t pid, ip_xmit_attr_t *ixa);
131 132 static mblk_t *icmp_prepend_hdr(conn_t *, ip_xmit_attr_t *, const ip_pkt_t *,
132 133 const in6_addr_t *, const in6_addr_t *, uint32_t, mblk_t *, int *);
133 134 static mblk_t *icmp_prepend_header_template(conn_t *, ip_xmit_attr_t *,
134 135 mblk_t *, const in6_addr_t *, uint32_t, int *);
135 136 static int icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
136 137 uchar_t *ptr, int len);
137 138 static void icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err);
138 139 static void icmp_tpi_unbind(queue_t *q, mblk_t *mp);
139 140 static int icmp_wput(queue_t *q, mblk_t *mp);
140 141 static int icmp_wput_fallback(queue_t *q, mblk_t *mp);
141 142 static void icmp_wput_other(queue_t *q, mblk_t *mp);
142 143 static void icmp_wput_iocdata(queue_t *q, mblk_t *mp);
143 144 static void icmp_wput_restricted(queue_t *q, mblk_t *mp);
144 145 static void icmp_ulp_recv(conn_t *, mblk_t *, uint_t);
145 146
146 147 static void *rawip_stack_init(netstackid_t stackid, netstack_t *ns);
147 148 static void rawip_stack_fini(netstackid_t stackid, void *arg);
148 149
149 150 static void *rawip_kstat_init(netstackid_t stackid);
150 151 static void rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp);
151 152 static int rawip_kstat_update(kstat_t *kp, int rw);
152 153 static void rawip_stack_shutdown(netstackid_t stackid, void *arg);
153 154
154 155 /* Common routines for TPI and socket module */
155 156 static conn_t *rawip_do_open(int, cred_t *, int *, int);
156 157 static void rawip_do_close(conn_t *);
157 158 static int rawip_do_bind(conn_t *, struct sockaddr *, socklen_t);
158 159 static int rawip_do_unbind(conn_t *);
159 160 static int rawip_do_connect(conn_t *, const struct sockaddr *, socklen_t,
160 161 cred_t *, pid_t);
161 162
162 163 int rawip_getsockname(sock_lower_handle_t, struct sockaddr *,
163 164 socklen_t *, cred_t *);
164 165 int rawip_getpeername(sock_lower_handle_t, struct sockaddr *,
165 166 socklen_t *, cred_t *);
166 167
167 168 static struct module_info icmp_mod_info = {
168 169 5707, "icmp", 1, INFPSZ, 512, 128
169 170 };
170 171
171 172 /*
172 173 * Entry points for ICMP as a device.
173 174 * We have separate open functions for the /dev/icmp and /dev/icmp6 devices.
174 175 */
175 176 static struct qinit icmprinitv4 = {
176 177 NULL, NULL, icmp_openv4, icmp_close, NULL, &icmp_mod_info
177 178 };
178 179
179 180 static struct qinit icmprinitv6 = {
180 181 NULL, NULL, icmp_openv6, icmp_close, NULL, &icmp_mod_info
181 182 };
182 183
183 184 static struct qinit icmpwinit = {
184 185 icmp_wput, ip_wsrv, NULL, NULL, NULL, &icmp_mod_info
185 186 };
186 187
187 188 /* ICMP entry point during fallback */
188 189 static struct qinit icmp_fallback_sock_winit = {
189 190 icmp_wput_fallback, NULL, NULL, NULL, NULL, &icmp_mod_info
190 191 };
191 192
192 193 /* For AF_INET aka /dev/icmp */
193 194 struct streamtab icmpinfov4 = {
194 195 &icmprinitv4, &icmpwinit
195 196 };
196 197
197 198 /* For AF_INET6 aka /dev/icmp6 */
198 199 struct streamtab icmpinfov6 = {
199 200 &icmprinitv6, &icmpwinit
200 201 };
201 202
202 203 /* Default structure copied into T_INFO_ACK messages */
203 204 static struct T_info_ack icmp_g_t_info_ack = {
204 205 T_INFO_ACK,
205 206 IP_MAXPACKET, /* TSDU_size. icmp allows maximum size messages. */
206 207 T_INVALID, /* ETSDU_size. icmp does not support expedited data. */
207 208 T_INVALID, /* CDATA_size. icmp does not support connect data. */
208 209 T_INVALID, /* DDATA_size. icmp does not support disconnect data. */
209 210 0, /* ADDR_size - filled in later. */
210 211 0, /* OPT_size - not initialized here */
211 212 IP_MAXPACKET, /* TIDU_size. icmp allows maximum size messages. */
212 213 T_CLTS, /* SERV_type. icmp supports connection-less. */
213 214 TS_UNBND, /* CURRENT_state. This is set from icmp_state. */
214 215 (XPG4_1|SENDZERO) /* PROVIDER_flag */
215 216 };
216 217
217 218 static int
218 219 icmp_set_buf_prop(netstack_t *stack, cred_t *cr, mod_prop_info_t *pinfo,
219 220 const char *ifname, const void *pval, uint_t flags)
220 221 {
221 222 return (mod_set_buf_prop(stack->netstack_icmp->is_propinfo_tbl,
222 223 stack, cr, pinfo, ifname, pval, flags));
223 224 }
224 225
225 226 static int
226 227 icmp_get_buf_prop(netstack_t *stack, mod_prop_info_t *pinfo, const char *ifname,
227 228 void *val, uint_t psize, uint_t flags)
228 229 {
229 230 return (mod_get_buf_prop(stack->netstack_icmp->is_propinfo_tbl, stack,
230 231 pinfo, ifname, val, psize, flags));
231 232 }
232 233
233 234 /*
234 235 * All of these are alterable, within the min/max values given, at run time.
235 236 *
236 237 * Note: All those tunables which do not start with "icmp_" are Committed and
237 238 * therefore are public. See PSARC 2010/080.
238 239 */
239 240 static mod_prop_info_t icmp_propinfo_tbl[] = {
240 241 /* tunable - 0 */
241 242 { "_wroff_extra", MOD_PROTO_RAWIP,
242 243 mod_set_uint32, mod_get_uint32,
243 244 {0, 128, 32}, {32} },
244 245
245 246 { "_ipv4_ttl", MOD_PROTO_RAWIP,
246 247 mod_set_uint32, mod_get_uint32,
247 248 {1, 255, 255}, {255} },
248 249
249 250 { "_ipv6_hoplimit", MOD_PROTO_RAWIP,
250 251 mod_set_uint32, mod_get_uint32,
251 252 {0, IPV6_MAX_HOPS, IPV6_DEFAULT_HOPS},
252 253 {IPV6_DEFAULT_HOPS} },
253 254
254 255 { "_bsd_compat", MOD_PROTO_RAWIP,
255 256 mod_set_boolean, mod_get_boolean,
256 257 {B_TRUE}, {B_TRUE} },
257 258
258 259 { "send_buf", MOD_PROTO_RAWIP,
259 260 icmp_set_buf_prop, icmp_get_buf_prop,
260 261 {4096, 65536, 8192}, {8192} },
261 262
262 263 { "_xmit_lowat", MOD_PROTO_RAWIP,
263 264 mod_set_uint32, mod_get_uint32,
264 265 {0, 65536, 1024}, {1024} },
265 266
266 267 { "recv_buf", MOD_PROTO_RAWIP,
267 268 icmp_set_buf_prop, icmp_get_buf_prop,
268 269 {4096, 65536, 8192}, {8192} },
269 270
270 271 { "max_buf", MOD_PROTO_RAWIP,
271 272 mod_set_uint32, mod_get_uint32,
272 273 {65536, ULP_MAX_BUF, 256*1024}, {256*1024} },
273 274
274 275 { "_pmtu_discovery", MOD_PROTO_RAWIP,
275 276 mod_set_boolean, mod_get_boolean,
276 277 {B_FALSE}, {B_FALSE} },
277 278
278 279 { "_sendto_ignerr", MOD_PROTO_RAWIP,
279 280 mod_set_boolean, mod_get_boolean,
280 281 {B_FALSE}, {B_FALSE} },
281 282
282 283 { "?", MOD_PROTO_RAWIP, NULL, mod_get_allprop, {0}, {0} },
283 284
284 285 { NULL, 0, NULL, NULL, {0}, {0} }
285 286 };
286 287
287 288 #define is_wroff_extra is_propinfo_tbl[0].prop_cur_uval
288 289 #define is_ipv4_ttl is_propinfo_tbl[1].prop_cur_uval
289 290 #define is_ipv6_hoplimit is_propinfo_tbl[2].prop_cur_uval
290 291 #define is_bsd_compat is_propinfo_tbl[3].prop_cur_bval
291 292 #define is_xmit_hiwat is_propinfo_tbl[4].prop_cur_uval
292 293 #define is_xmit_lowat is_propinfo_tbl[5].prop_cur_uval
293 294 #define is_recv_hiwat is_propinfo_tbl[6].prop_cur_uval
294 295 #define is_max_buf is_propinfo_tbl[7].prop_cur_uval
295 296 #define is_pmtu_discovery is_propinfo_tbl[8].prop_cur_bval
296 297 #define is_sendto_ignerr is_propinfo_tbl[9].prop_cur_bval
297 298
298 299 typedef union T_primitives *t_primp_t;
299 300
300 301 /*
301 302 * This routine is called to handle each O_T_BIND_REQ/T_BIND_REQ message
302 303 * passed to icmp_wput.
303 304 * It calls IP to verify the local IP address, and calls IP to insert
304 305 * the conn_t in the fanout table.
305 306 * If everything is ok it then sends the T_BIND_ACK back up.
306 307 */
307 308 static void
308 309 icmp_tpi_bind(queue_t *q, mblk_t *mp)
309 310 {
310 311 int error;
311 312 struct sockaddr *sa;
312 313 struct T_bind_req *tbr;
313 314 socklen_t len;
314 315 sin_t *sin;
315 316 sin6_t *sin6;
316 317 icmp_t *icmp;
317 318 conn_t *connp = Q_TO_CONN(q);
318 319 mblk_t *mp1;
319 320 cred_t *cr;
320 321
321 322 /*
322 323 * All Solaris components should pass a db_credp
323 324 * for this TPI message, hence we ASSERT.
324 325 * But in case there is some other M_PROTO that looks
325 326 * like a TPI message sent by some other kernel
326 327 * component, we check and return an error.
327 328 */
328 329 cr = msg_getcred(mp, NULL);
329 330 ASSERT(cr != NULL);
330 331 if (cr == NULL) {
331 332 icmp_err_ack(q, mp, TSYSERR, EINVAL);
332 333 return;
333 334 }
334 335
335 336 icmp = connp->conn_icmp;
336 337 if ((mp->b_wptr - mp->b_rptr) < sizeof (*tbr)) {
337 338 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
338 339 "icmp_bind: bad req, len %u",
339 340 (uint_t)(mp->b_wptr - mp->b_rptr));
340 341 icmp_err_ack(q, mp, TPROTO, 0);
341 342 return;
342 343 }
343 344
344 345 if (icmp->icmp_state != TS_UNBND) {
345 346 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
346 347 "icmp_bind: bad state, %u", icmp->icmp_state);
347 348 icmp_err_ack(q, mp, TOUTSTATE, 0);
348 349 return;
349 350 }
350 351
351 352 /*
352 353 * Reallocate the message to make sure we have enough room for an
353 354 * address.
354 355 */
355 356 mp1 = reallocb(mp, sizeof (struct T_bind_ack) + sizeof (sin6_t), 1);
356 357 if (mp1 == NULL) {
357 358 icmp_err_ack(q, mp, TSYSERR, ENOMEM);
358 359 return;
359 360 }
360 361 mp = mp1;
361 362
362 363 /* Reset the message type in preparation for shipping it back. */
363 364 DB_TYPE(mp) = M_PCPROTO;
364 365 tbr = (struct T_bind_req *)mp->b_rptr;
365 366 len = tbr->ADDR_length;
366 367 switch (len) {
367 368 case 0: /* request for a generic port */
368 369 tbr->ADDR_offset = sizeof (struct T_bind_req);
369 370 if (connp->conn_family == AF_INET) {
370 371 tbr->ADDR_length = sizeof (sin_t);
371 372 sin = (sin_t *)&tbr[1];
372 373 *sin = sin_null;
373 374 sin->sin_family = AF_INET;
374 375 mp->b_wptr = (uchar_t *)&sin[1];
375 376 sa = (struct sockaddr *)sin;
376 377 len = sizeof (sin_t);
377 378 } else {
378 379 ASSERT(connp->conn_family == AF_INET6);
379 380 tbr->ADDR_length = sizeof (sin6_t);
380 381 sin6 = (sin6_t *)&tbr[1];
381 382 *sin6 = sin6_null;
382 383 sin6->sin6_family = AF_INET6;
383 384 mp->b_wptr = (uchar_t *)&sin6[1];
384 385 sa = (struct sockaddr *)sin6;
385 386 len = sizeof (sin6_t);
386 387 }
387 388 break;
388 389
389 390 case sizeof (sin_t): /* Complete IPv4 address */
390 391 sa = (struct sockaddr *)mi_offset_param(mp, tbr->ADDR_offset,
391 392 sizeof (sin_t));
392 393 break;
393 394
394 395 case sizeof (sin6_t): /* Complete IPv6 address */
395 396 sa = (struct sockaddr *)mi_offset_param(mp,
396 397 tbr->ADDR_offset, sizeof (sin6_t));
397 398 break;
398 399
399 400 default:
400 401 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
401 402 "icmp_bind: bad ADDR_length %u", tbr->ADDR_length);
402 403 icmp_err_ack(q, mp, TBADADDR, 0);
403 404 return;
404 405 }
405 406
406 407 error = rawip_do_bind(connp, sa, len);
407 408 if (error != 0) {
408 409 if (error > 0) {
409 410 icmp_err_ack(q, mp, TSYSERR, error);
410 411 } else {
411 412 icmp_err_ack(q, mp, -error, 0);
412 413 }
413 414 } else {
414 415 tbr->PRIM_type = T_BIND_ACK;
415 416 qreply(q, mp);
416 417 }
417 418 }
418 419
419 420 static int
420 421 rawip_do_bind(conn_t *connp, struct sockaddr *sa, socklen_t len)
421 422 {
422 423 sin_t *sin;
423 424 sin6_t *sin6;
424 425 icmp_t *icmp = connp->conn_icmp;
425 426 int error = 0;
426 427 ip_laddr_t laddr_type = IPVL_UNICAST_UP; /* INADDR_ANY */
427 428 in_port_t lport; /* Network byte order */
428 429 ipaddr_t v4src; /* Set if AF_INET */
429 430 in6_addr_t v6src;
430 431 uint_t scopeid = 0;
431 432 zoneid_t zoneid = IPCL_ZONEID(connp);
432 433 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
433 434
434 435 if (sa == NULL || !OK_32PTR((char *)sa)) {
435 436 return (EINVAL);
436 437 }
437 438
438 439 switch (len) {
439 440 case sizeof (sin_t): /* Complete IPv4 address */
440 441 sin = (sin_t *)sa;
441 442 if (sin->sin_family != AF_INET ||
442 443 connp->conn_family != AF_INET) {
443 444 /* TSYSERR, EAFNOSUPPORT */
444 445 return (EAFNOSUPPORT);
445 446 }
446 447 v4src = sin->sin_addr.s_addr;
447 448 IN6_IPADDR_TO_V4MAPPED(v4src, &v6src);
448 449 if (v4src != INADDR_ANY) {
449 450 laddr_type = ip_laddr_verify_v4(v4src, zoneid, ipst,
450 451 B_TRUE);
451 452 }
452 453 lport = sin->sin_port;
453 454 break;
454 455 case sizeof (sin6_t): /* Complete IPv6 address */
455 456 sin6 = (sin6_t *)sa;
456 457 if (sin6->sin6_family != AF_INET6 ||
457 458 connp->conn_family != AF_INET6) {
458 459 /* TSYSERR, EAFNOSUPPORT */
459 460 return (EAFNOSUPPORT);
460 461 }
461 462 /* No support for mapped addresses on raw sockets */
462 463 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
463 464 /* TSYSERR, EADDRNOTAVAIL */
464 465 return (EADDRNOTAVAIL);
465 466 }
466 467 v6src = sin6->sin6_addr;
467 468 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) {
468 469 if (IN6_IS_ADDR_LINKSCOPE(&v6src))
469 470 scopeid = sin6->sin6_scope_id;
470 471 laddr_type = ip_laddr_verify_v6(&v6src, zoneid, ipst,
471 472 B_TRUE, scopeid);
472 473 }
473 474 lport = sin6->sin6_port;
474 475 break;
475 476
476 477 default:
477 478 /* TBADADDR */
478 479 return (EADDRNOTAVAIL);
479 480 }
480 481
481 482 /* Is the local address a valid unicast, multicast, or broadcast? */
482 483 if (laddr_type == IPVL_BAD)
483 484 return (EADDRNOTAVAIL);
484 485
485 486 /*
486 487 * The state must be TS_UNBND.
487 488 */
488 489 mutex_enter(&connp->conn_lock);
489 490 if (icmp->icmp_state != TS_UNBND) {
490 491 mutex_exit(&connp->conn_lock);
491 492 return (-TOUTSTATE);
492 493 }
493 494
494 495 /*
495 496 * Copy the source address into our icmp structure. This address
496 497 * may still be zero; if so, ip will fill in the correct address
497 498 * each time an outbound packet is passed to it.
498 499 * If we are binding to a broadcast or multicast address then
499 500 * we just set the conn_bound_addr since we don't want to use
500 501 * that as the source address when sending.
501 502 */
502 503 connp->conn_bound_addr_v6 = v6src;
503 504 connp->conn_laddr_v6 = v6src;
504 505 if (scopeid != 0) {
505 506 connp->conn_ixa->ixa_flags |= IXAF_SCOPEID_SET;
506 507 connp->conn_ixa->ixa_scopeid = scopeid;
507 508 connp->conn_incoming_ifindex = scopeid;
508 509 } else {
509 510 connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
510 511 connp->conn_incoming_ifindex = connp->conn_bound_if;
511 512 }
512 513
513 514 switch (laddr_type) {
514 515 case IPVL_UNICAST_UP:
515 516 case IPVL_UNICAST_DOWN:
516 517 connp->conn_saddr_v6 = v6src;
517 518 connp->conn_mcbc_bind = B_FALSE;
518 519 break;
519 520 case IPVL_MCAST:
520 521 case IPVL_BCAST:
521 522 /* ip_set_destination will pick a source address later */
522 523 connp->conn_saddr_v6 = ipv6_all_zeros;
523 524 connp->conn_mcbc_bind = B_TRUE;
524 525 break;
525 526 }
526 527
527 528 /* Any errors after this point should use late_error */
528 529
529 530 /*
530 531 * Use sin_port/sin6_port since applications like psh use SOCK_RAW
531 532 * with IPPROTO_TCP.
532 533 */
533 534 connp->conn_lport = lport;
534 535 connp->conn_fport = 0;
535 536
536 537 if (connp->conn_family == AF_INET) {
537 538 ASSERT(connp->conn_ipversion == IPV4_VERSION);
538 539 } else {
539 540 ASSERT(connp->conn_ipversion == IPV6_VERSION);
540 541 }
541 542
542 543 icmp->icmp_state = TS_IDLE;
543 544
544 545 /*
545 546 * We create an initial header template here to make a subsequent
546 547 * sendto have a starting point. Since conn_last_dst is zero the
547 548 * first sendto will always follow the 'dst changed' code path.
548 549 * Note that we defer massaging options and the related checksum
549 550 * adjustment until we have a destination address.
550 551 */
551 552 error = icmp_build_hdr_template(connp, &connp->conn_saddr_v6,
552 553 &connp->conn_faddr_v6, connp->conn_flowinfo);
553 554 if (error != 0) {
554 555 mutex_exit(&connp->conn_lock);
555 556 goto late_error;
556 557 }
557 558 /* Just in case */
558 559 connp->conn_faddr_v6 = ipv6_all_zeros;
559 560 connp->conn_v6lastdst = ipv6_all_zeros;
560 561 mutex_exit(&connp->conn_lock);
561 562
562 563 error = ip_laddr_fanout_insert(connp);
563 564 if (error != 0)
564 565 goto late_error;
565 566
566 567 /* Bind succeeded */
567 568 return (0);
568 569
569 570 late_error:
570 571 mutex_enter(&connp->conn_lock);
571 572 connp->conn_saddr_v6 = ipv6_all_zeros;
572 573 connp->conn_bound_addr_v6 = ipv6_all_zeros;
573 574 connp->conn_laddr_v6 = ipv6_all_zeros;
574 575 if (scopeid != 0) {
575 576 connp->conn_ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
576 577 connp->conn_incoming_ifindex = connp->conn_bound_if;
577 578 }
578 579 icmp->icmp_state = TS_UNBND;
579 580 connp->conn_v6lastdst = ipv6_all_zeros;
580 581 connp->conn_lport = 0;
581 582
582 583 /* Restore the header that was built above - different source address */
583 584 (void) icmp_build_hdr_template(connp, &connp->conn_saddr_v6,
584 585 &connp->conn_faddr_v6, connp->conn_flowinfo);
585 586 mutex_exit(&connp->conn_lock);
586 587 return (error);
587 588 }
588 589
589 590 /*
590 591 * Tell IP to just bind to the protocol.
591 592 */
592 593 static void
593 594 icmp_bind_proto(icmp_t *icmp)
594 595 {
595 596 conn_t *connp = icmp->icmp_connp;
596 597
597 598 mutex_enter(&connp->conn_lock);
598 599 connp->conn_saddr_v6 = ipv6_all_zeros;
599 600 connp->conn_laddr_v6 = ipv6_all_zeros;
600 601 connp->conn_faddr_v6 = ipv6_all_zeros;
601 602 connp->conn_v6lastdst = ipv6_all_zeros;
602 603 mutex_exit(&connp->conn_lock);
603 604
604 605 (void) ip_laddr_fanout_insert(connp);
605 606 }
606 607
607 608 /*
608 609 * This routine handles each T_CONN_REQ message passed to icmp. It
609 610 * associates a default destination address with the stream.
610 611 *
611 612 * After various error checks are completed, icmp_connect() lays
612 613 * the target address and port into the composite header template.
613 614 * Then we ask IP for information, including a source address if we didn't
614 615 * already have one. Finally we send up the T_OK_ACK reply message.
615 616 */
616 617 static void
617 618 icmp_tpi_connect(queue_t *q, mblk_t *mp)
618 619 {
619 620 conn_t *connp = Q_TO_CONN(q);
620 621 struct T_conn_req *tcr;
621 622 struct sockaddr *sa;
622 623 socklen_t len;
623 624 int error;
624 625 cred_t *cr;
625 626 pid_t pid;
626 627 /*
627 628 * All Solaris components should pass a db_credp
628 629 * for this TPI message, hence we ASSERT.
629 630 * But in case there is some other M_PROTO that looks
630 631 * like a TPI message sent by some other kernel
631 632 * component, we check and return an error.
632 633 */
633 634 cr = msg_getcred(mp, &pid);
634 635 ASSERT(cr != NULL);
635 636 if (cr == NULL) {
636 637 icmp_err_ack(q, mp, TSYSERR, EINVAL);
637 638 return;
638 639 }
639 640
640 641 tcr = (struct T_conn_req *)mp->b_rptr;
641 642 /* Sanity checks */
642 643 if ((mp->b_wptr - mp->b_rptr) < sizeof (struct T_conn_req)) {
643 644 icmp_err_ack(q, mp, TPROTO, 0);
644 645 return;
645 646 }
646 647
647 648 if (tcr->OPT_length != 0) {
648 649 icmp_err_ack(q, mp, TBADOPT, 0);
649 650 return;
650 651 }
651 652
652 653 len = tcr->DEST_length;
653 654
654 655 switch (len) {
655 656 default:
656 657 icmp_err_ack(q, mp, TBADADDR, 0);
657 658 return;
658 659 case sizeof (sin_t):
659 660 sa = (struct sockaddr *)mi_offset_param(mp, tcr->DEST_offset,
660 661 sizeof (sin_t));
661 662 break;
662 663 case sizeof (sin6_t):
663 664 sa = (struct sockaddr *)mi_offset_param(mp,
664 665 tcr->DEST_offset, sizeof (sin6_t));
665 666 break;
666 667 }
667 668
668 669 error = proto_verify_ip_addr(connp->conn_family, sa, len);
669 670 if (error != 0) {
670 671 icmp_err_ack(q, mp, TSYSERR, error);
671 672 return;
672 673 }
673 674
674 675 error = rawip_do_connect(connp, sa, len, cr, pid);
675 676 if (error != 0) {
676 677 if (error < 0) {
677 678 icmp_err_ack(q, mp, -error, 0);
678 679 } else {
679 680 icmp_err_ack(q, mp, 0, error);
680 681 }
681 682 } else {
682 683 mblk_t *mp1;
683 684
684 685 /*
685 686 * We have to send a connection confirmation to
686 687 * keep TLI happy.
687 688 */
688 689 if (connp->conn_family == AF_INET) {
689 690 mp1 = mi_tpi_conn_con(NULL, (char *)sa,
690 691 sizeof (sin_t), NULL, 0);
691 692 } else {
692 693 ASSERT(connp->conn_family == AF_INET6);
693 694 mp1 = mi_tpi_conn_con(NULL, (char *)sa,
694 695 sizeof (sin6_t), NULL, 0);
695 696 }
696 697 if (mp1 == NULL) {
697 698 icmp_err_ack(q, mp, TSYSERR, ENOMEM);
698 699 return;
699 700 }
700 701
701 702 /*
702 703 * Send ok_ack for T_CONN_REQ
703 704 */
704 705 mp = mi_tpi_ok_ack_alloc(mp);
705 706 if (mp == NULL) {
706 707 /* Unable to reuse the T_CONN_REQ for the ack. */
707 708 icmp_err_ack_prim(q, mp1, T_CONN_REQ, TSYSERR, ENOMEM);
708 709 return;
709 710 }
710 711 putnext(connp->conn_rq, mp);
711 712 putnext(connp->conn_rq, mp1);
712 713 }
713 714 }
714 715
715 716 static int
716 717 rawip_do_connect(conn_t *connp, const struct sockaddr *sa, socklen_t len,
717 718 cred_t *cr, pid_t pid)
718 719 {
719 720 icmp_t *icmp;
720 721 sin_t *sin;
721 722 sin6_t *sin6;
722 723 int error;
723 724 uint16_t dstport;
724 725 ipaddr_t v4dst;
725 726 in6_addr_t v6dst;
726 727 uint32_t flowinfo;
727 728 ip_xmit_attr_t *ixa;
728 729 ip_xmit_attr_t *oldixa;
729 730 uint_t scopeid = 0;
730 731 uint_t srcid = 0;
731 732 in6_addr_t v6src = connp->conn_saddr_v6;
732 733
733 734 icmp = connp->conn_icmp;
734 735
735 736 if (sa == NULL || !OK_32PTR((char *)sa)) {
736 737 return (EINVAL);
737 738 }
738 739
739 740 ASSERT(sa != NULL && len != 0);
740 741
741 742 /*
742 743 * Determine packet type based on type of address passed in
743 744 * the request should contain an IPv4 or IPv6 address.
744 745 * Make sure that address family matches the type of
745 746 * family of the address passed down.
746 747 */
747 748 switch (len) {
748 749 case sizeof (sin_t):
749 750 sin = (sin_t *)sa;
750 751
751 752 v4dst = sin->sin_addr.s_addr;
752 753 dstport = sin->sin_port;
753 754 IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
754 755 ASSERT(connp->conn_ipversion == IPV4_VERSION);
755 756 break;
756 757
757 758 case sizeof (sin6_t):
758 759 sin6 = (sin6_t *)sa;
759 760
760 761 /* No support for mapped addresses on raw sockets */
761 762 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
762 763 return (EADDRNOTAVAIL);
763 764 }
764 765 v6dst = sin6->sin6_addr;
765 766 dstport = sin6->sin6_port;
766 767 ASSERT(connp->conn_ipversion == IPV6_VERSION);
767 768 flowinfo = sin6->sin6_flowinfo;
768 769 if (IN6_IS_ADDR_LINKLOCAL(&sin6->sin6_addr))
769 770 scopeid = sin6->sin6_scope_id;
770 771 srcid = sin6->__sin6_src_id;
771 772 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&v6src)) {
772 773 /* Due to check above, we know sin6_addr is v6-only. */
773 774 if (!ip_srcid_find_id(srcid, &v6src, IPCL_ZONEID(connp),
774 775 B_FALSE, connp->conn_netstack)) {
775 776 /* Mismatch - v6src would be v4mapped. */
776 777 return (EADDRNOTAVAIL);
777 778 }
778 779 }
779 780 break;
780 781 }
781 782
782 783 /*
783 784 * If there is a different thread using conn_ixa then we get a new
784 785 * copy and cut the old one loose from conn_ixa. Otherwise we use
785 786 * conn_ixa and prevent any other thread from using/changing it.
786 787 * Once connect() is done other threads can use conn_ixa since the
787 788 * refcnt will be back at one.
788 789 * We defer updating conn_ixa until later to handle any concurrent
789 790 * conn_ixa_cleanup thread.
790 791 */
791 792 ixa = conn_get_ixa(connp, B_FALSE);
792 793 if (ixa == NULL)
793 794 return (ENOMEM);
794 795
795 796 mutex_enter(&connp->conn_lock);
796 797 /*
797 798 * This icmp_t must have bound already before doing a connect.
798 799 * Reject if a connect is in progress (we drop conn_lock during
799 800 * rawip_do_connect).
800 801 */
801 802 if (icmp->icmp_state == TS_UNBND || icmp->icmp_state == TS_WCON_CREQ) {
802 803 mutex_exit(&connp->conn_lock);
803 804 ixa_refrele(ixa);
804 805 return (-TOUTSTATE);
805 806 }
806 807
807 808 if (icmp->icmp_state == TS_DATA_XFER) {
808 809 /* Already connected - clear out state */
809 810 if (connp->conn_mcbc_bind)
810 811 connp->conn_saddr_v6 = ipv6_all_zeros;
811 812 else
812 813 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
813 814 connp->conn_laddr_v6 = connp->conn_bound_addr_v6;
814 815 connp->conn_faddr_v6 = ipv6_all_zeros;
815 816 icmp->icmp_state = TS_IDLE;
816 817 }
817 818
818 819 /*
819 820 * Use sin_port/sin6_port since applications like psh use SOCK_RAW
820 821 * with IPPROTO_TCP.
821 822 */
822 823 connp->conn_fport = dstport;
823 824 if (connp->conn_ipversion == IPV4_VERSION) {
824 825 /*
825 826 * Interpret a zero destination to mean loopback.
826 827 * Update the T_CONN_REQ (sin/sin6) since it is used to
827 828 * generate the T_CONN_CON.
828 829 */
829 830 if (v4dst == INADDR_ANY) {
830 831 v4dst = htonl(INADDR_LOOPBACK);
831 832 IN6_IPADDR_TO_V4MAPPED(v4dst, &v6dst);
832 833 ASSERT(connp->conn_family == AF_INET);
833 834 sin->sin_addr.s_addr = v4dst;
834 835 }
835 836 connp->conn_faddr_v6 = v6dst;
836 837 connp->conn_flowinfo = 0;
837 838 } else {
838 839 ASSERT(connp->conn_ipversion == IPV6_VERSION);
839 840 /*
840 841 * Interpret a zero destination to mean loopback.
841 842 * Update the T_CONN_REQ (sin/sin6) since it is used to
842 843 * generate the T_CONN_CON.
843 844 */
844 845 if (IN6_IS_ADDR_UNSPECIFIED(&v6dst)) {
845 846 v6dst = ipv6_loopback;
846 847 sin6->sin6_addr = v6dst;
847 848 }
848 849 connp->conn_faddr_v6 = v6dst;
849 850 connp->conn_flowinfo = flowinfo;
850 851 }
851 852
852 853 /*
853 854 * We update our cred/cpid based on the caller of connect
854 855 */
855 856 if (connp->conn_cred != cr) {
856 857 crhold(cr);
857 858 crfree(connp->conn_cred);
858 859 connp->conn_cred = cr;
859 860 }
860 861 connp->conn_cpid = pid;
861 862 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
862 863 ixa->ixa_cred = cr;
863 864 ixa->ixa_cpid = pid;
864 865 if (is_system_labeled()) {
865 866 /* We need to restart with a label based on the cred */
866 867 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred);
867 868 }
868 869
869 870 if (scopeid != 0) {
870 871 ixa->ixa_flags |= IXAF_SCOPEID_SET;
871 872 ixa->ixa_scopeid = scopeid;
872 873 connp->conn_incoming_ifindex = scopeid;
873 874 } else {
874 875 ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
875 876 connp->conn_incoming_ifindex = connp->conn_bound_if;
876 877 }
877 878
878 879 /*
879 880 * conn_connect will drop conn_lock and reacquire it.
880 881 * To prevent a send* from messing with this icmp_t while the lock
881 882 * is dropped we set icmp_state and clear conn_v6lastdst.
882 883 * That will make all send* fail with EISCONN.
883 884 */
884 885 connp->conn_v6lastdst = ipv6_all_zeros;
885 886 icmp->icmp_state = TS_WCON_CREQ;
886 887
887 888 error = conn_connect(connp, NULL, IPDF_ALLOW_MCBC);
888 889 mutex_exit(&connp->conn_lock);
889 890 if (error != 0)
890 891 goto connect_failed;
891 892
892 893 /*
893 894 * The addresses have been verified. Time to insert in
894 895 * the correct fanout list.
895 896 */
896 897 error = ipcl_conn_insert(connp);
897 898 if (error != 0)
898 899 goto connect_failed;
899 900
900 901 mutex_enter(&connp->conn_lock);
901 902 error = icmp_build_hdr_template(connp, &connp->conn_saddr_v6,
902 903 &connp->conn_faddr_v6, connp->conn_flowinfo);
903 904 if (error != 0) {
904 905 mutex_exit(&connp->conn_lock);
905 906 goto connect_failed;
906 907 }
907 908
908 909 icmp->icmp_state = TS_DATA_XFER;
909 910 /* Record this as the "last" send even though we haven't sent any */
910 911 connp->conn_v6lastdst = connp->conn_faddr_v6;
911 912 connp->conn_lastipversion = connp->conn_ipversion;
912 913 connp->conn_lastdstport = connp->conn_fport;
913 914 connp->conn_lastflowinfo = connp->conn_flowinfo;
914 915 connp->conn_lastscopeid = scopeid;
915 916 connp->conn_lastsrcid = srcid;
916 917 /* Also remember a source to use together with lastdst */
917 918 connp->conn_v6lastsrc = v6src;
918 919
919 920 oldixa = conn_replace_ixa(connp, ixa);
920 921 mutex_exit(&connp->conn_lock);
921 922 ixa_refrele(oldixa);
922 923
923 924 ixa_refrele(ixa);
924 925 return (0);
925 926
926 927 connect_failed:
927 928 if (ixa != NULL)
928 929 ixa_refrele(ixa);
929 930 mutex_enter(&connp->conn_lock);
930 931 icmp->icmp_state = TS_IDLE;
931 932 /* In case the source address was set above */
932 933 if (connp->conn_mcbc_bind)
933 934 connp->conn_saddr_v6 = ipv6_all_zeros;
934 935 else
935 936 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
936 937 connp->conn_laddr_v6 = connp->conn_bound_addr_v6;
937 938 connp->conn_faddr_v6 = ipv6_all_zeros;
938 939 connp->conn_v6lastdst = ipv6_all_zeros;
939 940 connp->conn_flowinfo = 0;
940 941
941 942 (void) icmp_build_hdr_template(connp, &connp->conn_saddr_v6,
942 943 &connp->conn_faddr_v6, connp->conn_flowinfo);
943 944 mutex_exit(&connp->conn_lock);
944 945 return (error);
945 946 }
946 947
947 948 static void
948 949 rawip_do_close(conn_t *connp)
949 950 {
950 951 ASSERT(connp != NULL && IPCL_IS_RAWIP(connp));
951 952
952 953 ip_quiesce_conn(connp);
953 954
954 955 if (!IPCL_IS_NONSTR(connp)) {
955 956 qprocsoff(connp->conn_rq);
956 957 }
957 958
958 959 icmp_close_free(connp);
959 960
960 961 /*
961 962 * Now we are truly single threaded on this stream, and can
962 963 * delete the things hanging off the connp, and finally the connp.
963 964 * We removed this connp from the fanout list, it cannot be
964 965 * accessed thru the fanouts, and we already waited for the
965 966 * conn_ref to drop to 0. We are already in close, so
966 967 * there cannot be any other thread from the top. qprocsoff
967 968 * has completed, and service has completed or won't run in
968 969 * future.
969 970 */
970 971 ASSERT(connp->conn_ref == 1);
971 972
972 973 if (!IPCL_IS_NONSTR(connp)) {
973 974 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
974 975 } else {
975 976 ip_free_helper_stream(connp);
976 977 }
977 978
978 979 connp->conn_ref--;
979 980 ipcl_conn_destroy(connp);
980 981 }
981 982
982 983 /* ARGSUSED */
983 984 static int
984 985 icmp_close(queue_t *q, int flags, cred_t *credp __unused)
985 986 {
986 987 conn_t *connp;
987 988
988 989 if (flags & SO_FALLBACK) {
989 990 /*
990 991 * stream is being closed while in fallback
991 992 * simply free the resources that were allocated
992 993 */
993 994 inet_minor_free(WR(q)->q_ptr, (dev_t)(RD(q)->q_ptr));
994 995 qprocsoff(q);
995 996 goto done;
996 997 }
997 998
998 999 connp = Q_TO_CONN(q);
999 1000 (void) rawip_do_close(connp);
1000 1001 done:
1001 1002 q->q_ptr = WR(q)->q_ptr = NULL;
1002 1003 return (0);
1003 1004 }
1004 1005
1005 1006 static void
1006 1007 icmp_close_free(conn_t *connp)
1007 1008 {
1008 1009 icmp_t *icmp = connp->conn_icmp;
1009 1010
1010 1011 if (icmp->icmp_filter != NULL) {
1011 1012 kmem_free(icmp->icmp_filter, sizeof (icmp6_filter_t));
1012 1013 icmp->icmp_filter = NULL;
1013 1014 }
1014 1015
1015 1016 /*
1016 1017 * Clear any fields which the kmem_cache constructor clears.
1017 1018 * Only icmp_connp needs to be preserved.
1018 1019 * TBD: We should make this more efficient to avoid clearing
1019 1020 * everything.
1020 1021 */
1021 1022 ASSERT(icmp->icmp_connp == connp);
1022 1023 bzero(icmp, sizeof (icmp_t));
1023 1024 icmp->icmp_connp = connp;
1024 1025 }
1025 1026
1026 1027 /*
1027 1028 * This routine handles each T_DISCON_REQ message passed to icmp
1028 1029 * as an indicating that ICMP is no longer connected. This results
1029 1030 * in telling IP to restore the binding to just the local address.
1030 1031 */
1031 1032 static int
1032 1033 icmp_do_disconnect(conn_t *connp)
1033 1034 {
1034 1035 icmp_t *icmp = connp->conn_icmp;
1035 1036 int error;
1036 1037
1037 1038 mutex_enter(&connp->conn_lock);
1038 1039 if (icmp->icmp_state != TS_DATA_XFER) {
1039 1040 mutex_exit(&connp->conn_lock);
1040 1041 return (-TOUTSTATE);
1041 1042 }
1042 1043 if (connp->conn_mcbc_bind)
1043 1044 connp->conn_saddr_v6 = ipv6_all_zeros;
1044 1045 else
1045 1046 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
1046 1047 connp->conn_laddr_v6 = connp->conn_bound_addr_v6;
1047 1048 connp->conn_faddr_v6 = ipv6_all_zeros;
1048 1049 icmp->icmp_state = TS_IDLE;
1049 1050
1050 1051 connp->conn_v6lastdst = ipv6_all_zeros;
1051 1052 error = icmp_build_hdr_template(connp, &connp->conn_saddr_v6,
1052 1053 &connp->conn_faddr_v6, connp->conn_flowinfo);
1053 1054 mutex_exit(&connp->conn_lock);
1054 1055 if (error != 0)
1055 1056 return (error);
1056 1057
1057 1058 /*
1058 1059 * Tell IP to remove the full binding and revert
1059 1060 * to the local address binding.
1060 1061 */
1061 1062 return (ip_laddr_fanout_insert(connp));
1062 1063 }
1063 1064
1064 1065 static void
1065 1066 icmp_tpi_disconnect(queue_t *q, mblk_t *mp)
1066 1067 {
1067 1068 conn_t *connp = Q_TO_CONN(q);
1068 1069 int error;
1069 1070
1070 1071 /*
1071 1072 * Allocate the largest primitive we need to send back
1072 1073 * T_error_ack is > than T_ok_ack
1073 1074 */
1074 1075 mp = reallocb(mp, sizeof (struct T_error_ack), 1);
1075 1076 if (mp == NULL) {
1076 1077 /* Unable to reuse the T_DISCON_REQ for the ack. */
1077 1078 icmp_err_ack_prim(q, mp, T_DISCON_REQ, TSYSERR, ENOMEM);
1078 1079 return;
1079 1080 }
1080 1081
1081 1082 error = icmp_do_disconnect(connp);
1082 1083
1083 1084 if (error != 0) {
1084 1085 if (error > 0) {
1085 1086 icmp_err_ack(q, mp, 0, error);
1086 1087 } else {
1087 1088 icmp_err_ack(q, mp, -error, 0);
1088 1089 }
1089 1090 } else {
1090 1091 mp = mi_tpi_ok_ack_alloc(mp);
1091 1092 ASSERT(mp != NULL);
1092 1093 qreply(q, mp);
1093 1094 }
1094 1095 }
1095 1096
1096 1097 static int
1097 1098 icmp_disconnect(conn_t *connp)
1098 1099 {
1099 1100 int error;
1100 1101
1101 1102 connp->conn_dgram_errind = B_FALSE;
1102 1103
1103 1104 error = icmp_do_disconnect(connp);
1104 1105
1105 1106 if (error < 0)
1106 1107 error = proto_tlitosyserr(-error);
1107 1108 return (error);
1108 1109 }
1109 1110
1110 1111 /* This routine creates a T_ERROR_ACK message and passes it upstream. */
1111 1112 static void
1112 1113 icmp_err_ack(queue_t *q, mblk_t *mp, t_scalar_t t_error, int sys_error)
1113 1114 {
1114 1115 if ((mp = mi_tpi_err_ack_alloc(mp, t_error, sys_error)) != NULL)
1115 1116 qreply(q, mp);
1116 1117 }
1117 1118
1118 1119 /* Shorthand to generate and send TPI error acks to our client */
1119 1120 static void
1120 1121 icmp_err_ack_prim(queue_t *q, mblk_t *mp, t_scalar_t primitive,
1121 1122 t_scalar_t t_error, int sys_error)
1122 1123 {
1123 1124 struct T_error_ack *teackp;
1124 1125
1125 1126 if ((mp = tpi_ack_alloc(mp, sizeof (struct T_error_ack),
1126 1127 M_PCPROTO, T_ERROR_ACK)) != NULL) {
1127 1128 teackp = (struct T_error_ack *)mp->b_rptr;
1128 1129 teackp->ERROR_prim = primitive;
1129 1130 teackp->TLI_error = t_error;
1130 1131 teackp->UNIX_error = sys_error;
1131 1132 qreply(q, mp);
1132 1133 }
1133 1134 }
1134 1135
1135 1136 /*
1136 1137 * icmp_icmp_input is called as conn_recvicmp to process ICMP messages.
1137 1138 * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors.
1138 1139 * Assumes that IP has pulled up everything up to and including the ICMP header.
1139 1140 */
1140 1141 /* ARGSUSED2 */
1141 1142 static void
1142 1143 icmp_icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
1143 1144 {
1144 1145 conn_t *connp = (conn_t *)arg1;
1145 1146 icmp_t *icmp = connp->conn_icmp;
1146 1147 icmph_t *icmph;
1147 1148 ipha_t *ipha;
1148 1149 int iph_hdr_length;
1149 1150 sin_t sin;
1150 1151 mblk_t *mp1;
1151 1152 int error = 0;
1152 1153
1153 1154 ipha = (ipha_t *)mp->b_rptr;
1154 1155
1155 1156 ASSERT(OK_32PTR(mp->b_rptr));
1156 1157
1157 1158 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
1158 1159 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
1159 1160 icmp_icmp_error_ipv6(connp, mp, ira);
1160 1161 return;
1161 1162 }
1162 1163 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
1163 1164
1164 1165 /* Skip past the outer IP and ICMP headers */
1165 1166 ASSERT(IPH_HDR_LENGTH(ipha) == ira->ira_ip_hdr_length);
1166 1167 iph_hdr_length = ira->ira_ip_hdr_length;
1167 1168 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1168 1169 ipha = (ipha_t *)&icmph[1]; /* Inner IP header */
1169 1170
1170 1171 iph_hdr_length = IPH_HDR_LENGTH(ipha);
1171 1172
1172 1173 switch (icmph->icmph_type) {
1173 1174 case ICMP_DEST_UNREACHABLE:
1174 1175 switch (icmph->icmph_code) {
1175 1176 case ICMP_FRAGMENTATION_NEEDED: {
1176 1177 ipha_t *ipha;
1177 1178 ip_xmit_attr_t *ixa;
1178 1179 /*
1179 1180 * IP has already adjusted the path MTU.
1180 1181 * But we need to adjust DF for IPv4.
1181 1182 */
1182 1183 if (connp->conn_ipversion != IPV4_VERSION)
1183 1184 break;
1184 1185
1185 1186 ixa = conn_get_ixa(connp, B_FALSE);
1186 1187 if (ixa == NULL || ixa->ixa_ire == NULL) {
1187 1188 /*
1188 1189 * Some other thread holds conn_ixa. We will
1189 1190 * redo this on the next ICMP too big.
1190 1191 */
1191 1192 if (ixa != NULL)
1192 1193 ixa_refrele(ixa);
1193 1194 break;
1194 1195 }
1195 1196 (void) ip_get_pmtu(ixa);
1196 1197
1197 1198 mutex_enter(&connp->conn_lock);
1198 1199 ipha = (ipha_t *)connp->conn_ht_iphc;
1199 1200 if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF) {
1200 1201 ipha->ipha_fragment_offset_and_flags |=
1201 1202 IPH_DF_HTONS;
1202 1203 } else {
1203 1204 ipha->ipha_fragment_offset_and_flags &=
1204 1205 ~IPH_DF_HTONS;
1205 1206 }
1206 1207 mutex_exit(&connp->conn_lock);
1207 1208 ixa_refrele(ixa);
1208 1209 break;
1209 1210 }
1210 1211 case ICMP_PORT_UNREACHABLE:
1211 1212 case ICMP_PROTOCOL_UNREACHABLE:
1212 1213 error = ECONNREFUSED;
1213 1214 break;
1214 1215 default:
1215 1216 /* Transient errors */
1216 1217 break;
1217 1218 }
1218 1219 break;
1219 1220 default:
1220 1221 /* Transient errors */
1221 1222 break;
1222 1223 }
1223 1224 if (error == 0) {
1224 1225 freemsg(mp);
1225 1226 return;
1226 1227 }
1227 1228
1228 1229 /*
1229 1230 * Deliver T_UDERROR_IND when the application has asked for it.
1230 1231 * The socket layer enables this automatically when connected.
1231 1232 */
1232 1233 if (!connp->conn_dgram_errind) {
1233 1234 freemsg(mp);
1234 1235 return;
1235 1236 }
1236 1237
1237 1238 sin = sin_null;
1238 1239 sin.sin_family = AF_INET;
1239 1240 sin.sin_addr.s_addr = ipha->ipha_dst;
1240 1241
1241 1242 if (IPCL_IS_NONSTR(connp)) {
1242 1243 mutex_enter(&connp->conn_lock);
1243 1244 if (icmp->icmp_state == TS_DATA_XFER) {
1244 1245 if (sin.sin_addr.s_addr == connp->conn_faddr_v4) {
1245 1246 mutex_exit(&connp->conn_lock);
1246 1247 (*connp->conn_upcalls->su_set_error)
1247 1248 (connp->conn_upper_handle, error);
1248 1249 goto done;
1249 1250 }
1250 1251 } else {
1251 1252 icmp->icmp_delayed_error = error;
1252 1253 *((sin_t *)&icmp->icmp_delayed_addr) = sin;
1253 1254 }
1254 1255 mutex_exit(&connp->conn_lock);
1255 1256 } else {
1256 1257 mp1 = mi_tpi_uderror_ind((char *)&sin, sizeof (sin_t), NULL, 0,
1257 1258 error);
1258 1259 if (mp1 != NULL)
1259 1260 putnext(connp->conn_rq, mp1);
1260 1261 }
1261 1262 done:
1262 1263 freemsg(mp);
1263 1264 }
1264 1265
1265 1266 /*
1266 1267 * icmp_icmp_error_ipv6 is called by icmp_icmp_error to process ICMP for IPv6.
1267 1268 * Generates the appropriate T_UDERROR_IND for permanent (non-transient) errors.
1268 1269 * Assumes that IP has pulled up all the extension headers as well as the
1269 1270 * ICMPv6 header.
1270 1271 */
1271 1272 static void
1272 1273 icmp_icmp_error_ipv6(conn_t *connp, mblk_t *mp, ip_recv_attr_t *ira)
1273 1274 {
1274 1275 icmp6_t *icmp6;
1275 1276 ip6_t *ip6h, *outer_ip6h;
1276 1277 uint16_t iph_hdr_length;
1277 1278 uint8_t *nexthdrp;
1278 1279 sin6_t sin6;
1279 1280 mblk_t *mp1;
1280 1281 int error = 0;
1281 1282 icmp_t *icmp = connp->conn_icmp;
1282 1283
1283 1284 outer_ip6h = (ip6_t *)mp->b_rptr;
1284 1285 #ifdef DEBUG
1285 1286 if (outer_ip6h->ip6_nxt != IPPROTO_ICMPV6)
1286 1287 iph_hdr_length = ip_hdr_length_v6(mp, outer_ip6h);
1287 1288 else
1288 1289 iph_hdr_length = IPV6_HDR_LEN;
1289 1290 ASSERT(iph_hdr_length == ira->ira_ip_hdr_length);
1290 1291 #endif
1291 1292 /* Skip past the outer IP and ICMP headers */
1292 1293 iph_hdr_length = ira->ira_ip_hdr_length;
1293 1294 icmp6 = (icmp6_t *)&mp->b_rptr[iph_hdr_length];
1294 1295
1295 1296 ip6h = (ip6_t *)&icmp6[1]; /* Inner IP header */
1296 1297 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &iph_hdr_length, &nexthdrp)) {
1297 1298 freemsg(mp);
1298 1299 return;
1299 1300 }
1300 1301
1301 1302 switch (icmp6->icmp6_type) {
1302 1303 case ICMP6_DST_UNREACH:
1303 1304 switch (icmp6->icmp6_code) {
1304 1305 case ICMP6_DST_UNREACH_NOPORT:
1305 1306 error = ECONNREFUSED;
1306 1307 break;
1307 1308 case ICMP6_DST_UNREACH_ADMIN:
1308 1309 case ICMP6_DST_UNREACH_NOROUTE:
1309 1310 case ICMP6_DST_UNREACH_BEYONDSCOPE:
1310 1311 case ICMP6_DST_UNREACH_ADDR:
1311 1312 /* Transient errors */
1312 1313 break;
1313 1314 default:
1314 1315 break;
1315 1316 }
1316 1317 break;
1317 1318 case ICMP6_PACKET_TOO_BIG: {
1318 1319 struct T_unitdata_ind *tudi;
1319 1320 struct T_opthdr *toh;
1320 1321 size_t udi_size;
1321 1322 mblk_t *newmp;
1322 1323 t_scalar_t opt_length = sizeof (struct T_opthdr) +
1323 1324 sizeof (struct ip6_mtuinfo);
1324 1325 sin6_t *sin6;
1325 1326 struct ip6_mtuinfo *mtuinfo;
1326 1327
1327 1328 /*
1328 1329 * If the application has requested to receive path mtu
1329 1330 * information, send up an empty message containing an
1330 1331 * IPV6_PATHMTU ancillary data item.
1331 1332 */
1332 1333 if (!connp->conn_ipv6_recvpathmtu)
1333 1334 break;
1334 1335
1335 1336 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t) +
1336 1337 opt_length;
1337 1338 if ((newmp = allocb(udi_size, BPRI_MED)) == NULL) {
1338 1339 BUMP_MIB(&icmp->icmp_is->is_rawip_mib, rawipInErrors);
1339 1340 break;
1340 1341 }
1341 1342
1342 1343 /*
1343 1344 * newmp->b_cont is left to NULL on purpose. This is an
1344 1345 * empty message containing only ancillary data.
1345 1346 */
1346 1347 newmp->b_datap->db_type = M_PROTO;
1347 1348 tudi = (struct T_unitdata_ind *)newmp->b_rptr;
1348 1349 newmp->b_wptr = (uchar_t *)tudi + udi_size;
1349 1350 tudi->PRIM_type = T_UNITDATA_IND;
1350 1351 tudi->SRC_length = sizeof (sin6_t);
1351 1352 tudi->SRC_offset = sizeof (struct T_unitdata_ind);
1352 1353 tudi->OPT_offset = tudi->SRC_offset + sizeof (sin6_t);
1353 1354 tudi->OPT_length = opt_length;
1354 1355
1355 1356 sin6 = (sin6_t *)&tudi[1];
1356 1357 bzero(sin6, sizeof (sin6_t));
1357 1358 sin6->sin6_family = AF_INET6;
1358 1359 sin6->sin6_addr = connp->conn_faddr_v6;
1359 1360
1360 1361 toh = (struct T_opthdr *)&sin6[1];
1361 1362 toh->level = IPPROTO_IPV6;
1362 1363 toh->name = IPV6_PATHMTU;
1363 1364 toh->len = opt_length;
1364 1365 toh->status = 0;
1365 1366
1366 1367 mtuinfo = (struct ip6_mtuinfo *)&toh[1];
1367 1368 bzero(mtuinfo, sizeof (struct ip6_mtuinfo));
1368 1369 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
1369 1370 mtuinfo->ip6m_addr.sin6_addr = ip6h->ip6_dst;
1370 1371 mtuinfo->ip6m_mtu = icmp6->icmp6_mtu;
1371 1372 /*
1372 1373 * We've consumed everything we need from the original
1373 1374 * message. Free it, then send our empty message.
1374 1375 */
1375 1376 freemsg(mp);
1376 1377 icmp_ulp_recv(connp, newmp, msgdsize(newmp));
1377 1378 return;
1378 1379 }
1379 1380 case ICMP6_TIME_EXCEEDED:
1380 1381 /* Transient errors */
1381 1382 break;
1382 1383 case ICMP6_PARAM_PROB:
1383 1384 /* If this corresponds to an ICMP_PROTOCOL_UNREACHABLE */
1384 1385 if (icmp6->icmp6_code == ICMP6_PARAMPROB_NEXTHEADER &&
1385 1386 (uchar_t *)ip6h + icmp6->icmp6_pptr ==
1386 1387 (uchar_t *)nexthdrp) {
1387 1388 error = ECONNREFUSED;
1388 1389 break;
1389 1390 }
1390 1391 break;
1391 1392 }
1392 1393 if (error == 0) {
1393 1394 freemsg(mp);
1394 1395 return;
1395 1396 }
1396 1397
1397 1398 /*
1398 1399 * Deliver T_UDERROR_IND when the application has asked for it.
1399 1400 * The socket layer enables this automatically when connected.
1400 1401 */
1401 1402 if (!connp->conn_dgram_errind) {
1402 1403 freemsg(mp);
1403 1404 return;
1404 1405 }
1405 1406
1406 1407 sin6 = sin6_null;
1407 1408 sin6.sin6_family = AF_INET6;
1408 1409 sin6.sin6_addr = ip6h->ip6_dst;
1409 1410 sin6.sin6_flowinfo = ip6h->ip6_vcf & ~IPV6_VERS_AND_FLOW_MASK;
1410 1411 if (IPCL_IS_NONSTR(connp)) {
1411 1412 mutex_enter(&connp->conn_lock);
1412 1413 if (icmp->icmp_state == TS_DATA_XFER) {
1413 1414 if (IN6_ARE_ADDR_EQUAL(&sin6.sin6_addr,
1414 1415 &connp->conn_faddr_v6)) {
1415 1416 mutex_exit(&connp->conn_lock);
1416 1417 (*connp->conn_upcalls->su_set_error)
1417 1418 (connp->conn_upper_handle, error);
1418 1419 goto done;
1419 1420 }
1420 1421 } else {
1421 1422 icmp->icmp_delayed_error = error;
1422 1423 *((sin6_t *)&icmp->icmp_delayed_addr) = sin6;
1423 1424 }
1424 1425 mutex_exit(&connp->conn_lock);
1425 1426 } else {
1426 1427 mp1 = mi_tpi_uderror_ind((char *)&sin6, sizeof (sin6_t),
1427 1428 NULL, 0, error);
1428 1429 if (mp1 != NULL)
1429 1430 putnext(connp->conn_rq, mp1);
1430 1431 }
1431 1432 done:
1432 1433 freemsg(mp);
1433 1434 }
1434 1435
1435 1436 /*
1436 1437 * This routine responds to T_ADDR_REQ messages. It is called by icmp_wput.
1437 1438 * The local address is filled in if endpoint is bound. The remote address
1438 1439 * is filled in if remote address has been precified ("connected endpoint")
1439 1440 * (The concept of connected CLTS sockets is alien to published TPI
1440 1441 * but we support it anyway).
1441 1442 */
1442 1443 static void
1443 1444 icmp_addr_req(queue_t *q, mblk_t *mp)
1444 1445 {
1445 1446 struct sockaddr *sa;
1446 1447 mblk_t *ackmp;
1447 1448 struct T_addr_ack *taa;
1448 1449 icmp_t *icmp = Q_TO_ICMP(q);
1449 1450 conn_t *connp = icmp->icmp_connp;
1450 1451 uint_t addrlen;
1451 1452
1452 1453 /* Make it large enough for worst case */
1453 1454 ackmp = reallocb(mp, sizeof (struct T_addr_ack) +
1454 1455 2 * sizeof (sin6_t), 1);
1455 1456 if (ackmp == NULL) {
1456 1457 icmp_err_ack(q, mp, TSYSERR, ENOMEM);
1457 1458 return;
1458 1459 }
1459 1460 taa = (struct T_addr_ack *)ackmp->b_rptr;
1460 1461
1461 1462 bzero(taa, sizeof (struct T_addr_ack));
1462 1463 ackmp->b_wptr = (uchar_t *)&taa[1];
1463 1464
1464 1465 taa->PRIM_type = T_ADDR_ACK;
1465 1466 ackmp->b_datap->db_type = M_PCPROTO;
1466 1467
1467 1468 if (connp->conn_family == AF_INET)
1468 1469 addrlen = sizeof (sin_t);
1469 1470 else
1470 1471 addrlen = sizeof (sin6_t);
1471 1472
1472 1473 mutex_enter(&connp->conn_lock);
1473 1474 /*
1474 1475 * Note: Following code assumes 32 bit alignment of basic
1475 1476 * data structures like sin_t and struct T_addr_ack.
1476 1477 */
1477 1478 if (icmp->icmp_state != TS_UNBND) {
1478 1479 /*
1479 1480 * Fill in local address first
1480 1481 */
1481 1482 taa->LOCADDR_offset = sizeof (*taa);
1482 1483 taa->LOCADDR_length = addrlen;
1483 1484 sa = (struct sockaddr *)&taa[1];
1484 1485 (void) conn_getsockname(connp, sa, &addrlen);
1485 1486 ackmp->b_wptr += addrlen;
1486 1487 }
1487 1488 if (icmp->icmp_state == TS_DATA_XFER) {
1488 1489 /*
1489 1490 * connected, fill remote address too
1490 1491 */
1491 1492 taa->REMADDR_length = addrlen;
1492 1493 /* assumed 32-bit alignment */
1493 1494 taa->REMADDR_offset = taa->LOCADDR_offset + taa->LOCADDR_length;
1494 1495 sa = (struct sockaddr *)(ackmp->b_rptr + taa->REMADDR_offset);
1495 1496 (void) conn_getpeername(connp, sa, &addrlen);
1496 1497 ackmp->b_wptr += addrlen;
1497 1498 }
1498 1499 mutex_exit(&connp->conn_lock);
1499 1500 ASSERT(ackmp->b_wptr <= ackmp->b_datap->db_lim);
1500 1501 qreply(q, ackmp);
1501 1502 }
1502 1503
1503 1504 static void
1504 1505 icmp_copy_info(struct T_info_ack *tap, icmp_t *icmp)
1505 1506 {
1506 1507 conn_t *connp = icmp->icmp_connp;
1507 1508
1508 1509 *tap = icmp_g_t_info_ack;
1509 1510
1510 1511 if (connp->conn_family == AF_INET6)
1511 1512 tap->ADDR_size = sizeof (sin6_t);
1512 1513 else
1513 1514 tap->ADDR_size = sizeof (sin_t);
1514 1515 tap->CURRENT_state = icmp->icmp_state;
1515 1516 tap->OPT_size = icmp_max_optsize;
1516 1517 }
1517 1518
1518 1519 static void
1519 1520 icmp_do_capability_ack(icmp_t *icmp, struct T_capability_ack *tcap,
1520 1521 t_uscalar_t cap_bits1)
1521 1522 {
1522 1523 tcap->CAP_bits1 = 0;
1523 1524
1524 1525 if (cap_bits1 & TC1_INFO) {
1525 1526 icmp_copy_info(&tcap->INFO_ack, icmp);
1526 1527 tcap->CAP_bits1 |= TC1_INFO;
1527 1528 }
1528 1529 }
1529 1530
1530 1531 /*
1531 1532 * This routine responds to T_CAPABILITY_REQ messages. It is called by
1532 1533 * icmp_wput. Much of the T_CAPABILITY_ACK information is copied from
1533 1534 * icmp_g_t_info_ack. The current state of the stream is copied from
1534 1535 * icmp_state.
1535 1536 */
1536 1537 static void
1537 1538 icmp_capability_req(queue_t *q, mblk_t *mp)
1538 1539 {
1539 1540 icmp_t *icmp = Q_TO_ICMP(q);
1540 1541 t_uscalar_t cap_bits1;
1541 1542 struct T_capability_ack *tcap;
1542 1543
1543 1544 cap_bits1 = ((struct T_capability_req *)mp->b_rptr)->CAP_bits1;
1544 1545
1545 1546 mp = tpi_ack_alloc(mp, sizeof (struct T_capability_ack),
1546 1547 mp->b_datap->db_type, T_CAPABILITY_ACK);
1547 1548 if (!mp)
1548 1549 return;
1549 1550
1550 1551 tcap = (struct T_capability_ack *)mp->b_rptr;
1551 1552
1552 1553 icmp_do_capability_ack(icmp, tcap, cap_bits1);
1553 1554
1554 1555 qreply(q, mp);
1555 1556 }
1556 1557
1557 1558 /*
1558 1559 * This routine responds to T_INFO_REQ messages. It is called by icmp_wput.
1559 1560 * Most of the T_INFO_ACK information is copied from icmp_g_t_info_ack.
1560 1561 * The current state of the stream is copied from icmp_state.
1561 1562 */
1562 1563 static void
1563 1564 icmp_info_req(queue_t *q, mblk_t *mp)
1564 1565 {
1565 1566 icmp_t *icmp = Q_TO_ICMP(q);
1566 1567
1567 1568 /* Create a T_INFO_ACK message. */
1568 1569 mp = tpi_ack_alloc(mp, sizeof (struct T_info_ack), M_PCPROTO,
1569 1570 T_INFO_ACK);
1570 1571 if (!mp)
1571 1572 return;
1572 1573 icmp_copy_info((struct T_info_ack *)mp->b_rptr, icmp);
1573 1574 qreply(q, mp);
1574 1575 }
1575 1576
1576 1577 static int
1577 1578 icmp_tpi_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
1578 1579 int family)
1579 1580 {
1580 1581 conn_t *connp;
1581 1582 dev_t conn_dev;
1582 1583 int error;
1583 1584
1584 1585 /* If the stream is already open, return immediately. */
1585 1586 if (q->q_ptr != NULL)
1586 1587 return (0);
1587 1588
1588 1589 if (sflag == MODOPEN)
1589 1590 return (EINVAL);
1590 1591
1591 1592 /*
1592 1593 * Since ICMP is not used so heavily, allocating from the small
1593 1594 * arena should be sufficient.
1594 1595 */
1595 1596 if ((conn_dev = inet_minor_alloc(ip_minor_arena_sa)) == 0) {
1596 1597 return (EBUSY);
1597 1598 }
1598 1599
1599 1600 if (flag & SO_FALLBACK) {
1600 1601 /*
1601 1602 * Non streams socket needs a stream to fallback to
1602 1603 */
1603 1604 RD(q)->q_ptr = (void *)conn_dev;
1604 1605 WR(q)->q_qinfo = &icmp_fallback_sock_winit;
1605 1606 WR(q)->q_ptr = (void *)ip_minor_arena_sa;
1606 1607 qprocson(q);
1607 1608 return (0);
1608 1609 }
1609 1610
1610 1611 connp = rawip_do_open(family, credp, &error, KM_SLEEP);
1611 1612 if (connp == NULL) {
1612 1613 ASSERT(error != 0);
1613 1614 inet_minor_free(ip_minor_arena_sa, conn_dev);
1614 1615 return (error);
1615 1616 }
1616 1617
1617 1618 *devp = makedevice(getemajor(*devp), (minor_t)conn_dev);
1618 1619 connp->conn_dev = conn_dev;
1619 1620 connp->conn_minor_arena = ip_minor_arena_sa;
1620 1621
1621 1622 /*
1622 1623 * Initialize the icmp_t structure for this stream.
1623 1624 */
1624 1625 q->q_ptr = connp;
1625 1626 WR(q)->q_ptr = connp;
1626 1627 connp->conn_rq = q;
1627 1628 connp->conn_wq = WR(q);
1628 1629
1629 1630 WR(q)->q_hiwat = connp->conn_sndbuf;
1630 1631 WR(q)->q_lowat = connp->conn_sndlowat;
1631 1632
1632 1633 qprocson(q);
1633 1634
1634 1635 /* Set the Stream head write offset. */
1635 1636 (void) proto_set_tx_wroff(q, connp, connp->conn_wroff);
1636 1637 (void) proto_set_rx_hiwat(connp->conn_rq, connp, connp->conn_rcvbuf);
1637 1638
1638 1639 mutex_enter(&connp->conn_lock);
1639 1640 connp->conn_state_flags &= ~CONN_INCIPIENT;
1640 1641 mutex_exit(&connp->conn_lock);
1641 1642
1642 1643 icmp_bind_proto(connp->conn_icmp);
1643 1644
1644 1645 return (0);
1645 1646 }
1646 1647
1647 1648 /* For /dev/icmp aka AF_INET open */
1648 1649 static int
1649 1650 icmp_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
1650 1651 {
1651 1652 return (icmp_tpi_open(q, devp, flag, sflag, credp, AF_INET));
1652 1653 }
1653 1654
1654 1655 /* For /dev/icmp6 aka AF_INET6 open */
1655 1656 static int
1656 1657 icmp_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
1657 1658 {
1658 1659 return (icmp_tpi_open(q, devp, flag, sflag, credp, AF_INET6));
1659 1660 }
1660 1661
1661 1662 /*
1662 1663 * This is the open routine for icmp. It allocates a icmp_t structure for
1663 1664 * the stream and, on the first open of the module, creates an ND table.
1664 1665 */
1665 1666 static conn_t *
1666 1667 rawip_do_open(int family, cred_t *credp, int *err, int flags)
1667 1668 {
1668 1669 icmp_t *icmp;
1669 1670 conn_t *connp;
1670 1671 zoneid_t zoneid;
1671 1672 netstack_t *ns;
1672 1673 icmp_stack_t *is;
1673 1674 int len;
1674 1675 boolean_t isv6 = B_FALSE;
1675 1676
1676 1677 *err = secpolicy_net_icmpaccess(credp);
1677 1678 if (*err != 0)
1678 1679 return (NULL);
1679 1680
1680 1681 if (family == AF_INET6)
1681 1682 isv6 = B_TRUE;
1682 1683
1683 1684 ns = netstack_find_by_cred(credp);
1684 1685 ASSERT(ns != NULL);
1685 1686 is = ns->netstack_icmp;
1686 1687 ASSERT(is != NULL);
1687 1688
1688 1689 /*
1689 1690 * For exclusive stacks we set the zoneid to zero
1690 1691 * to make ICMP operate as if in the global zone.
1691 1692 */
1692 1693 if (ns->netstack_stackid != GLOBAL_NETSTACKID)
1693 1694 zoneid = GLOBAL_ZONEID;
1694 1695 else
1695 1696 zoneid = crgetzoneid(credp);
1696 1697
1697 1698 ASSERT(flags == KM_SLEEP || flags == KM_NOSLEEP);
1698 1699
1699 1700 connp = ipcl_conn_create(IPCL_RAWIPCONN, flags, ns);
1700 1701 icmp = connp->conn_icmp;
1701 1702
1702 1703 /*
1703 1704 * ipcl_conn_create did a netstack_hold. Undo the hold that was
1704 1705 * done by netstack_find_by_cred()
1705 1706 */
1706 1707 netstack_rele(ns);
1707 1708
1708 1709 /*
1709 1710 * Since this conn_t/icmp_t is not yet visible to anybody else we don't
1710 1711 * need to lock anything.
1711 1712 */
1712 1713 ASSERT(connp->conn_proto == IPPROTO_ICMP);
1713 1714 ASSERT(connp->conn_icmp == icmp);
1714 1715 ASSERT(icmp->icmp_connp == connp);
1715 1716
1716 1717 /* Set the initial state of the stream and the privilege status. */
1717 1718 icmp->icmp_state = TS_UNBND;
1718 1719 connp->conn_ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
1719 1720 if (isv6) {
1720 1721 connp->conn_family = AF_INET6;
1721 1722 connp->conn_ipversion = IPV6_VERSION;
1722 1723 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
1723 1724 connp->conn_proto = IPPROTO_ICMPV6;
1724 1725 /* May be changed by a SO_PROTOTYPE socket option. */
1725 1726 connp->conn_proto = IPPROTO_ICMPV6;
1726 1727 connp->conn_ixa->ixa_protocol = connp->conn_proto;
1727 1728 connp->conn_ixa->ixa_raw_cksum_offset = 2;
1728 1729 connp->conn_default_ttl = is->is_ipv6_hoplimit;
1729 1730 len = sizeof (ip6_t);
1730 1731 } else {
1731 1732 connp->conn_family = AF_INET;
1732 1733 connp->conn_ipversion = IPV4_VERSION;
1733 1734 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
1734 1735 /* May be changed by a SO_PROTOTYPE socket option. */
1735 1736 connp->conn_proto = IPPROTO_ICMP;
1736 1737 connp->conn_ixa->ixa_protocol = connp->conn_proto;
1737 1738 connp->conn_default_ttl = is->is_ipv4_ttl;
1738 1739 len = sizeof (ipha_t);
1739 1740 }
1740 1741 connp->conn_xmit_ipp.ipp_unicast_hops = connp->conn_default_ttl;
1741 1742
1742 1743 connp->conn_ixa->ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1743 1744
1744 1745 /*
1745 1746 * For the socket of protocol IPPROTO_RAW or when IP_HDRINCL is set,
1746 1747 * the checksum is provided in the pre-built packet. We clear
1747 1748 * IXAF_SET_ULP_CKSUM to tell IP that the application has sent a
1748 1749 * complete IP header and not to compute the transport checksum.
1749 1750 */
1750 1751 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
1751 1752 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
1752 1753 connp->conn_ixa->ixa_zoneid = zoneid;
1753 1754
1754 1755 connp->conn_zoneid = zoneid;
1755 1756
1756 1757 /*
1757 1758 * If the caller has the process-wide flag set, then default to MAC
1758 1759 * exempt mode. This allows read-down to unlabeled hosts.
1759 1760 */
1760 1761 if (getpflags(NET_MAC_AWARE, credp) != 0)
1761 1762 connp->conn_mac_mode = CONN_MAC_AWARE;
1762 1763
1763 1764 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
1764 1765
1765 1766 icmp->icmp_is = is;
1766 1767
1767 1768 connp->conn_rcvbuf = is->is_recv_hiwat;
1768 1769 connp->conn_sndbuf = is->is_xmit_hiwat;
1769 1770 connp->conn_sndlowat = is->is_xmit_lowat;
1770 1771 connp->conn_rcvlowat = icmp_mod_info.mi_lowat;
1771 1772
1772 1773 connp->conn_wroff = len + is->is_wroff_extra;
1773 1774 connp->conn_so_type = SOCK_RAW;
1774 1775
1775 1776 connp->conn_recv = icmp_input;
1776 1777 connp->conn_recvicmp = icmp_icmp_input;
1777 1778 crhold(credp);
1778 1779 connp->conn_cred = credp;
1779 1780 connp->conn_cpid = curproc->p_pid;
1780 1781 connp->conn_open_time = ddi_get_lbolt64();
1781 1782 /* Cache things in ixa without an extra refhold */
1782 1783 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
1783 1784 connp->conn_ixa->ixa_cred = connp->conn_cred;
1784 1785 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
1785 1786 if (is_system_labeled())
1786 1787 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
1787 1788
1788 1789 connp->conn_flow_cntrld = B_FALSE;
1789 1790
1790 1791 if (is->is_pmtu_discovery)
1791 1792 connp->conn_ixa->ixa_flags |= IXAF_PMTU_DISCOVERY;
1792 1793
1793 1794 return (connp);
1794 1795 }
1795 1796
1796 1797 /*
1797 1798 * Which ICMP options OK to set through T_UNITDATA_REQ...
1798 1799 */
1799 1800 /* ARGSUSED */
1800 1801 static boolean_t
1801 1802 icmp_opt_allow_udr_set(t_scalar_t level, t_scalar_t name)
1802 1803 {
1803 1804 return (B_TRUE);
1804 1805 }
1805 1806
1806 1807 /*
1807 1808 * This routine gets default values of certain options whose default
1808 1809 * values are maintained by protcol specific code
1809 1810 */
1810 1811 int
1811 1812 icmp_opt_default(queue_t *q, t_scalar_t level, t_scalar_t name, uchar_t *ptr)
1812 1813 {
1813 1814 icmp_t *icmp = Q_TO_ICMP(q);
1814 1815 icmp_stack_t *is = icmp->icmp_is;
1815 1816 int *i1 = (int *)ptr;
1816 1817
1817 1818 switch (level) {
1818 1819 case IPPROTO_IP:
1819 1820 switch (name) {
1820 1821 case IP_MULTICAST_TTL:
1821 1822 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_TTL;
1822 1823 return (sizeof (uchar_t));
1823 1824 case IP_MULTICAST_LOOP:
1824 1825 *ptr = (uchar_t)IP_DEFAULT_MULTICAST_LOOP;
1825 1826 return (sizeof (uchar_t));
1826 1827 }
1827 1828 break;
1828 1829 case IPPROTO_IPV6:
1829 1830 switch (name) {
1830 1831 case IPV6_MULTICAST_HOPS:
1831 1832 *i1 = IP_DEFAULT_MULTICAST_TTL;
1832 1833 return (sizeof (int));
1833 1834 case IPV6_MULTICAST_LOOP:
1834 1835 *i1 = IP_DEFAULT_MULTICAST_LOOP;
1835 1836 return (sizeof (int));
1836 1837 case IPV6_UNICAST_HOPS:
1837 1838 *i1 = is->is_ipv6_hoplimit;
1838 1839 return (sizeof (int));
1839 1840 }
1840 1841 break;
1841 1842 case IPPROTO_ICMPV6:
1842 1843 switch (name) {
1843 1844 case ICMP6_FILTER:
1844 1845 /* Make it look like "pass all" */
1845 1846 ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
1846 1847 return (sizeof (icmp6_filter_t));
1847 1848 }
1848 1849 break;
1849 1850 }
1850 1851 return (-1);
1851 1852 }
1852 1853
1853 1854 /*
1854 1855 * This routine retrieves the current status of socket options.
1855 1856 * It returns the size of the option retrieved, or -1.
1856 1857 */
1857 1858 int
1858 1859 icmp_opt_get(conn_t *connp, int level, int name, uchar_t *ptr)
1859 1860 {
1860 1861 icmp_t *icmp = connp->conn_icmp;
1861 1862 int *i1 = (int *)ptr;
1862 1863 conn_opt_arg_t coas;
1863 1864 int retval;
1864 1865
1865 1866 coas.coa_connp = connp;
1866 1867 coas.coa_ixa = connp->conn_ixa;
1867 1868 coas.coa_ipp = &connp->conn_xmit_ipp;
1868 1869 coas.coa_ancillary = B_FALSE;
1869 1870 coas.coa_changed = 0;
1870 1871
1871 1872 /*
1872 1873 * We assume that the optcom framework has checked for the set
1873 1874 * of levels and names that are supported, hence we don't worry
1874 1875 * about rejecting based on that.
1875 1876 * First check for ICMP specific handling, then pass to common routine.
1876 1877 */
1877 1878 switch (level) {
1878 1879 case IPPROTO_IP:
1879 1880 /*
1880 1881 * Only allow IPv4 option processing on IPv4 sockets.
1881 1882 */
1882 1883 if (connp->conn_family != AF_INET)
1883 1884 return (-1);
1884 1885
1885 1886 switch (name) {
1886 1887 case IP_OPTIONS:
1887 1888 case T_IP_OPTIONS:
1888 1889 /* Options are passed up with each packet */
1889 1890 return (0);
1890 1891 case IP_HDRINCL:
1891 1892 mutex_enter(&connp->conn_lock);
1892 1893 *i1 = (int)icmp->icmp_hdrincl;
1893 1894 mutex_exit(&connp->conn_lock);
1894 1895 return (sizeof (int));
1895 1896 }
1896 1897 break;
1897 1898
1898 1899 case IPPROTO_IPV6:
1899 1900 /*
1900 1901 * Only allow IPv6 option processing on native IPv6 sockets.
1901 1902 */
1902 1903 if (connp->conn_family != AF_INET6)
1903 1904 return (-1);
1904 1905
1905 1906 switch (name) {
1906 1907 case IPV6_CHECKSUM:
1907 1908 /*
1908 1909 * Return offset or -1 if no checksum offset.
1909 1910 * Does not apply to IPPROTO_ICMPV6
1910 1911 */
1911 1912 if (connp->conn_proto == IPPROTO_ICMPV6)
1912 1913 return (-1);
1913 1914
1914 1915 mutex_enter(&connp->conn_lock);
1915 1916 if (connp->conn_ixa->ixa_flags & IXAF_SET_RAW_CKSUM)
1916 1917 *i1 = connp->conn_ixa->ixa_raw_cksum_offset;
1917 1918 else
1918 1919 *i1 = -1;
1919 1920 mutex_exit(&connp->conn_lock);
1920 1921 return (sizeof (int));
1921 1922 }
1922 1923 break;
1923 1924
1924 1925 case IPPROTO_ICMPV6:
1925 1926 /*
1926 1927 * Only allow IPv6 option processing on native IPv6 sockets.
1927 1928 */
1928 1929 if (connp->conn_family != AF_INET6)
1929 1930 return (-1);
1930 1931
1931 1932 if (connp->conn_proto != IPPROTO_ICMPV6)
1932 1933 return (-1);
1933 1934
1934 1935 switch (name) {
1935 1936 case ICMP6_FILTER:
1936 1937 mutex_enter(&connp->conn_lock);
1937 1938 if (icmp->icmp_filter == NULL) {
1938 1939 /* Make it look like "pass all" */
1939 1940 ICMP6_FILTER_SETPASSALL((icmp6_filter_t *)ptr);
1940 1941 } else {
1941 1942 (void) bcopy(icmp->icmp_filter, ptr,
1942 1943 sizeof (icmp6_filter_t));
1943 1944 }
1944 1945 mutex_exit(&connp->conn_lock);
1945 1946 return (sizeof (icmp6_filter_t));
1946 1947 }
1947 1948 }
1948 1949 mutex_enter(&connp->conn_lock);
1949 1950 retval = conn_opt_get(&coas, level, name, ptr);
1950 1951 mutex_exit(&connp->conn_lock);
1951 1952 return (retval);
1952 1953 }
1953 1954
1954 1955 /*
1955 1956 * This routine retrieves the current status of socket options.
1956 1957 * It returns the size of the option retrieved, or -1.
1957 1958 */
1958 1959 int
1959 1960 icmp_tpi_opt_get(queue_t *q, int level, int name, uchar_t *ptr)
1960 1961 {
1961 1962 conn_t *connp = Q_TO_CONN(q);
1962 1963 int err;
1963 1964
1964 1965 err = icmp_opt_get(connp, level, name, ptr);
1965 1966 return (err);
1966 1967 }
1967 1968
1968 1969 /*
1969 1970 * This routine sets socket options.
1970 1971 */
1971 1972 int
1972 1973 icmp_do_opt_set(conn_opt_arg_t *coa, int level, int name,
1973 1974 uint_t inlen, uchar_t *invalp, cred_t *cr, boolean_t checkonly)
1974 1975 {
1975 1976 conn_t *connp = coa->coa_connp;
1976 1977 ip_xmit_attr_t *ixa = coa->coa_ixa;
1977 1978 icmp_t *icmp = connp->conn_icmp;
1978 1979 icmp_stack_t *is = icmp->icmp_is;
1979 1980 int *i1 = (int *)invalp;
1980 1981 boolean_t onoff = (*i1 == 0) ? 0 : 1;
1981 1982 int error;
1982 1983
1983 1984 ASSERT(MUTEX_NOT_HELD(&coa->coa_connp->conn_lock));
1984 1985
1985 1986 /*
1986 1987 * For fixed length options, no sanity check
1987 1988 * of passed in length is done. It is assumed *_optcom_req()
1988 1989 * routines do the right thing.
1989 1990 */
1990 1991
1991 1992 switch (level) {
1992 1993 case SOL_SOCKET:
1993 1994 switch (name) {
1994 1995 case SO_PROTOTYPE:
1995 1996 if ((*i1 & 0xFF) != IPPROTO_ICMP &&
1996 1997 (*i1 & 0xFF) != IPPROTO_ICMPV6 &&
1997 1998 secpolicy_net_rawaccess(cr) != 0) {
1998 1999 return (EACCES);
1999 2000 }
2000 2001 if (checkonly)
2001 2002 break;
2002 2003
2003 2004 mutex_enter(&connp->conn_lock);
2004 2005 connp->conn_proto = *i1 & 0xFF;
2005 2006 ixa->ixa_protocol = connp->conn_proto;
2006 2007 if ((connp->conn_proto == IPPROTO_RAW ||
2007 2008 connp->conn_proto == IPPROTO_IGMP) &&
2008 2009 connp->conn_family == AF_INET) {
2009 2010 icmp->icmp_hdrincl = 1;
2010 2011 ixa->ixa_flags &= ~IXAF_SET_ULP_CKSUM;
2011 2012 } else if (connp->conn_proto == IPPROTO_UDP ||
2012 2013 connp->conn_proto == IPPROTO_TCP ||
2013 2014 connp->conn_proto == IPPROTO_SCTP) {
2014 2015 /* Used by test applications like psh */
2015 2016 icmp->icmp_hdrincl = 0;
2016 2017 ixa->ixa_flags &= ~IXAF_SET_ULP_CKSUM;
2017 2018 } else {
2018 2019 icmp->icmp_hdrincl = 0;
2019 2020 ixa->ixa_flags |= IXAF_SET_ULP_CKSUM;
2020 2021 }
2021 2022
2022 2023 if (connp->conn_family == AF_INET6 &&
2023 2024 connp->conn_proto == IPPROTO_ICMPV6) {
2024 2025 /* Set offset for icmp6_cksum */
2025 2026 ixa->ixa_flags &= ~IXAF_SET_RAW_CKSUM;
2026 2027 ixa->ixa_raw_cksum_offset = 2;
2027 2028 }
2028 2029 if (icmp->icmp_filter != NULL &&
2029 2030 connp->conn_proto != IPPROTO_ICMPV6) {
2030 2031 kmem_free(icmp->icmp_filter,
2031 2032 sizeof (icmp6_filter_t));
2032 2033 icmp->icmp_filter = NULL;
2033 2034 }
2034 2035 mutex_exit(&connp->conn_lock);
2035 2036
2036 2037 coa->coa_changed |= COA_HEADER_CHANGED;
2037 2038 /*
2038 2039 * For SCTP, we don't use icmp_bind_proto() for
2039 2040 * raw socket binding.
2040 2041 */
2041 2042 if (connp->conn_proto == IPPROTO_SCTP)
2042 2043 return (0);
2043 2044
2044 2045 coa->coa_changed |= COA_ICMP_BIND_NEEDED;
2045 2046 return (0);
2046 2047
2047 2048 case SO_SNDBUF:
2048 2049 if (*i1 > is->is_max_buf) {
2049 2050 return (ENOBUFS);
2050 2051 }
2051 2052 break;
2052 2053 case SO_RCVBUF:
2053 2054 if (*i1 > is->is_max_buf) {
2054 2055 return (ENOBUFS);
2055 2056 }
2056 2057 break;
2057 2058 }
2058 2059 break;
2059 2060
2060 2061 case IPPROTO_IP:
2061 2062 /*
2062 2063 * Only allow IPv4 option processing on IPv4 sockets.
2063 2064 */
2064 2065 if (connp->conn_family != AF_INET)
2065 2066 return (EINVAL);
2066 2067
2067 2068 switch (name) {
2068 2069 case IP_HDRINCL:
2069 2070 if (!checkonly) {
2070 2071 mutex_enter(&connp->conn_lock);
2071 2072 icmp->icmp_hdrincl = onoff;
2072 2073 if (onoff)
2073 2074 ixa->ixa_flags &= ~IXAF_SET_ULP_CKSUM;
2074 2075 else
2075 2076 ixa->ixa_flags |= IXAF_SET_ULP_CKSUM;
2076 2077 mutex_exit(&connp->conn_lock);
2077 2078 }
2078 2079 break;
2079 2080 }
2080 2081 break;
2081 2082
2082 2083 case IPPROTO_IPV6:
2083 2084 if (connp->conn_family != AF_INET6)
2084 2085 return (EINVAL);
2085 2086
2086 2087 switch (name) {
2087 2088 case IPV6_CHECKSUM:
2088 2089 /*
2089 2090 * Integer offset into the user data of where the
2090 2091 * checksum is located.
2091 2092 * Offset of -1 disables option.
2092 2093 * Does not apply to IPPROTO_ICMPV6.
2093 2094 */
2094 2095 if (connp->conn_proto == IPPROTO_ICMPV6 ||
2095 2096 coa->coa_ancillary) {
2096 2097 return (EINVAL);
2097 2098 }
2098 2099 if ((*i1 != -1) && ((*i1 < 0) || (*i1 & 0x1) != 0)) {
2099 2100 /* Negative or not 16 bit aligned offset */
2100 2101 return (EINVAL);
2101 2102 }
2102 2103 if (checkonly)
2103 2104 break;
2104 2105
2105 2106 mutex_enter(&connp->conn_lock);
2106 2107 if (*i1 == -1) {
2107 2108 ixa->ixa_flags &= ~IXAF_SET_RAW_CKSUM;
2108 2109 ixa->ixa_raw_cksum_offset = 0;
2109 2110 ixa->ixa_flags &= ~IXAF_SET_ULP_CKSUM;
2110 2111 } else {
2111 2112 ixa->ixa_flags |= IXAF_SET_RAW_CKSUM;
2112 2113 ixa->ixa_raw_cksum_offset = *i1;
2113 2114 ixa->ixa_flags |= IXAF_SET_ULP_CKSUM;
2114 2115 }
2115 2116 mutex_exit(&connp->conn_lock);
2116 2117 break;
2117 2118 }
2118 2119 break;
2119 2120
2120 2121 case IPPROTO_ICMPV6:
2121 2122 /*
2122 2123 * Only allow IPv6 option processing on IPv6 sockets.
2123 2124 */
2124 2125 if (connp->conn_family != AF_INET6)
2125 2126 return (EINVAL);
2126 2127 if (connp->conn_proto != IPPROTO_ICMPV6)
2127 2128 return (EINVAL);
2128 2129
2129 2130 switch (name) {
2130 2131 case ICMP6_FILTER:
2131 2132 if (checkonly)
2132 2133 break;
2133 2134
2134 2135 if ((inlen != 0) &&
2135 2136 (inlen != sizeof (icmp6_filter_t)))
2136 2137 return (EINVAL);
2137 2138
2138 2139 mutex_enter(&connp->conn_lock);
2139 2140 if (inlen == 0) {
2140 2141 if (icmp->icmp_filter != NULL) {
2141 2142 kmem_free(icmp->icmp_filter,
2142 2143 sizeof (icmp6_filter_t));
2143 2144 icmp->icmp_filter = NULL;
2144 2145 }
2145 2146 } else {
2146 2147 if (icmp->icmp_filter == NULL) {
2147 2148 icmp->icmp_filter = kmem_alloc(
2148 2149 sizeof (icmp6_filter_t),
2149 2150 KM_NOSLEEP);
2150 2151 if (icmp->icmp_filter == NULL) {
2151 2152 mutex_exit(&connp->conn_lock);
2152 2153 return (ENOBUFS);
2153 2154 }
2154 2155 }
2155 2156 (void) bcopy(invalp, icmp->icmp_filter, inlen);
2156 2157 }
2157 2158 mutex_exit(&connp->conn_lock);
2158 2159 break;
2159 2160 }
2160 2161 break;
2161 2162 }
2162 2163 error = conn_opt_set(coa, level, name, inlen, invalp,
2163 2164 checkonly, cr);
2164 2165 return (error);
2165 2166 }
2166 2167
2167 2168 /*
2168 2169 * This routine sets socket options.
2169 2170 */
2170 2171 int
2171 2172 icmp_opt_set(conn_t *connp, uint_t optset_context, int level, int name,
2172 2173 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
2173 2174 void *thisdg_attrs, cred_t *cr)
2174 2175 {
2175 2176 icmp_t *icmp = connp->conn_icmp;
2176 2177 int err;
2177 2178 conn_opt_arg_t coas, *coa;
2178 2179 boolean_t checkonly;
2179 2180 icmp_stack_t *is = icmp->icmp_is;
2180 2181
2181 2182 switch (optset_context) {
2182 2183 case SETFN_OPTCOM_CHECKONLY:
2183 2184 checkonly = B_TRUE;
2184 2185 /*
2185 2186 * Note: Implies T_CHECK semantics for T_OPTCOM_REQ
2186 2187 * inlen != 0 implies value supplied and
2187 2188 * we have to "pretend" to set it.
2188 2189 * inlen == 0 implies that there is no
2189 2190 * value part in T_CHECK request and just validation
2190 2191 * done elsewhere should be enough, we just return here.
2191 2192 */
2192 2193 if (inlen == 0) {
2193 2194 *outlenp = 0;
2194 2195 return (0);
2195 2196 }
2196 2197 break;
2197 2198 case SETFN_OPTCOM_NEGOTIATE:
2198 2199 checkonly = B_FALSE;
2199 2200 break;
2200 2201 case SETFN_UD_NEGOTIATE:
2201 2202 case SETFN_CONN_NEGOTIATE:
2202 2203 checkonly = B_FALSE;
2203 2204 /*
2204 2205 * Negotiating local and "association-related" options
2205 2206 * through T_UNITDATA_REQ.
2206 2207 *
2207 2208 * Following routine can filter out ones we do not
2208 2209 * want to be "set" this way.
2209 2210 */
2210 2211 if (!icmp_opt_allow_udr_set(level, name)) {
2211 2212 *outlenp = 0;
2212 2213 return (EINVAL);
2213 2214 }
2214 2215 break;
2215 2216 default:
2216 2217 /*
2217 2218 * We should never get here
2218 2219 */
2219 2220 *outlenp = 0;
2220 2221 return (EINVAL);
2221 2222 }
2222 2223
2223 2224 ASSERT((optset_context != SETFN_OPTCOM_CHECKONLY) ||
2224 2225 (optset_context == SETFN_OPTCOM_CHECKONLY && inlen != 0));
2225 2226
2226 2227 if (thisdg_attrs != NULL) {
2227 2228 /* Options from T_UNITDATA_REQ */
2228 2229 coa = (conn_opt_arg_t *)thisdg_attrs;
2229 2230 ASSERT(coa->coa_connp == connp);
2230 2231 ASSERT(coa->coa_ixa != NULL);
2231 2232 ASSERT(coa->coa_ipp != NULL);
2232 2233 ASSERT(coa->coa_ancillary);
2233 2234 } else {
2234 2235 coa = &coas;
2235 2236 coas.coa_connp = connp;
2236 2237 /* Get a reference on conn_ixa to prevent concurrent mods */
2237 2238 coas.coa_ixa = conn_get_ixa(connp, B_TRUE);
2238 2239 if (coas.coa_ixa == NULL) {
2239 2240 *outlenp = 0;
2240 2241 return (ENOMEM);
2241 2242 }
2242 2243 coas.coa_ipp = &connp->conn_xmit_ipp;
2243 2244 coas.coa_ancillary = B_FALSE;
2244 2245 coas.coa_changed = 0;
2245 2246 }
2246 2247
2247 2248 err = icmp_do_opt_set(coa, level, name, inlen, invalp,
2248 2249 cr, checkonly);
2249 2250 if (err != 0) {
2250 2251 errout:
2251 2252 if (!coa->coa_ancillary)
2252 2253 ixa_refrele(coa->coa_ixa);
2253 2254 *outlenp = 0;
2254 2255 return (err);
2255 2256 }
2256 2257
2257 2258 /*
2258 2259 * Common case of OK return with outval same as inval.
2259 2260 */
2260 2261 if (invalp != outvalp) {
2261 2262 /* don't trust bcopy for identical src/dst */
2262 2263 (void) bcopy(invalp, outvalp, inlen);
2263 2264 }
2264 2265 *outlenp = inlen;
2265 2266
2266 2267 /*
2267 2268 * If this was not ancillary data, then we rebuild the headers,
2268 2269 * update the IRE/NCE, and IPsec as needed.
2269 2270 * Since the label depends on the destination we go through
2270 2271 * ip_set_destination first.
2271 2272 */
2272 2273 if (coa->coa_ancillary) {
2273 2274 return (0);
2274 2275 }
2275 2276
2276 2277 if (coa->coa_changed & COA_ROUTE_CHANGED) {
2277 2278 in6_addr_t saddr, faddr, nexthop;
2278 2279 in_port_t fport;
2279 2280
2280 2281 /*
2281 2282 * We clear lastdst to make sure we pick up the change
2282 2283 * next time sending.
2283 2284 * If we are connected we re-cache the information.
2284 2285 * We ignore errors to preserve BSD behavior.
2285 2286 * Note that we don't redo IPsec policy lookup here
2286 2287 * since the final destination (or source) didn't change.
2287 2288 */
2288 2289 mutex_enter(&connp->conn_lock);
2289 2290 connp->conn_v6lastdst = ipv6_all_zeros;
2290 2291
2291 2292 ip_attr_nexthop(coa->coa_ipp, coa->coa_ixa,
2292 2293 &connp->conn_faddr_v6, &nexthop);
2293 2294 saddr = connp->conn_saddr_v6;
2294 2295 faddr = connp->conn_faddr_v6;
2295 2296 fport = connp->conn_fport;
2296 2297 mutex_exit(&connp->conn_lock);
2297 2298
2298 2299 if (!IN6_IS_ADDR_UNSPECIFIED(&faddr) &&
2299 2300 !IN6_IS_ADDR_V4MAPPED_ANY(&faddr)) {
2300 2301 (void) ip_attr_connect(connp, coa->coa_ixa,
2301 2302 &saddr, &faddr, &nexthop, fport, NULL, NULL,
2302 2303 IPDF_ALLOW_MCBC | IPDF_VERIFY_DST);
2303 2304 }
2304 2305 }
2305 2306
2306 2307 ixa_refrele(coa->coa_ixa);
2307 2308
2308 2309 if (coa->coa_changed & COA_HEADER_CHANGED) {
2309 2310 /*
2310 2311 * Rebuild the header template if we are connected.
2311 2312 * Otherwise clear conn_v6lastdst so we rebuild the header
2312 2313 * in the data path.
2313 2314 */
2314 2315 mutex_enter(&connp->conn_lock);
2315 2316 if (!IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6) &&
2316 2317 !IN6_IS_ADDR_V4MAPPED_ANY(&connp->conn_faddr_v6)) {
2317 2318 err = icmp_build_hdr_template(connp,
2318 2319 &connp->conn_saddr_v6, &connp->conn_faddr_v6,
2319 2320 connp->conn_flowinfo);
2320 2321 if (err != 0) {
2321 2322 mutex_exit(&connp->conn_lock);
2322 2323 return (err);
2323 2324 }
2324 2325 } else {
2325 2326 connp->conn_v6lastdst = ipv6_all_zeros;
2326 2327 }
2327 2328 mutex_exit(&connp->conn_lock);
2328 2329 }
2329 2330 if (coa->coa_changed & COA_RCVBUF_CHANGED) {
2330 2331 (void) proto_set_rx_hiwat(connp->conn_rq, connp,
2331 2332 connp->conn_rcvbuf);
2332 2333 }
2333 2334 if ((coa->coa_changed & COA_SNDBUF_CHANGED) && !IPCL_IS_NONSTR(connp)) {
2334 2335 connp->conn_wq->q_hiwat = connp->conn_sndbuf;
2335 2336 }
2336 2337 if (coa->coa_changed & COA_WROFF_CHANGED) {
2337 2338 /* Increase wroff if needed */
2338 2339 uint_t wroff;
2339 2340
2340 2341 mutex_enter(&connp->conn_lock);
2341 2342 wroff = connp->conn_ht_iphc_allocated + is->is_wroff_extra;
2342 2343 if (wroff > connp->conn_wroff) {
2343 2344 connp->conn_wroff = wroff;
2344 2345 mutex_exit(&connp->conn_lock);
2345 2346 (void) proto_set_tx_wroff(connp->conn_rq, connp, wroff);
2346 2347 } else {
2347 2348 mutex_exit(&connp->conn_lock);
2348 2349 }
2349 2350 }
2350 2351 if (coa->coa_changed & COA_ICMP_BIND_NEEDED) {
2351 2352 icmp_bind_proto(icmp);
2352 2353 }
2353 2354 return (err);
2354 2355 }
2355 2356
2356 2357 /* This routine sets socket options. */
2357 2358 int
2358 2359 icmp_tpi_opt_set(queue_t *q, uint_t optset_context, int level, int name,
2359 2360 uint_t inlen, uchar_t *invalp, uint_t *outlenp, uchar_t *outvalp,
2360 2361 void *thisdg_attrs, cred_t *cr)
2361 2362 {
2362 2363 conn_t *connp = Q_TO_CONN(q);
2363 2364 int error;
2364 2365
2365 2366 error = icmp_opt_set(connp, optset_context, level, name, inlen, invalp,
2366 2367 outlenp, outvalp, thisdg_attrs, cr);
2367 2368 return (error);
2368 2369 }
2369 2370
2370 2371 /*
2371 2372 * Setup IP headers.
2372 2373 *
2373 2374 * Note that IP_HDRINCL has ipha_protocol that is different than conn_proto,
2374 2375 * but icmp_output_hdrincl restores ipha_protocol once we return.
2375 2376 */
2376 2377 mblk_t *
2377 2378 icmp_prepend_hdr(conn_t *connp, ip_xmit_attr_t *ixa, const ip_pkt_t *ipp,
2378 2379 const in6_addr_t *v6src, const in6_addr_t *v6dst, uint32_t flowinfo,
2379 2380 mblk_t *data_mp, int *errorp)
2380 2381 {
2381 2382 mblk_t *mp;
2382 2383 icmp_stack_t *is = connp->conn_netstack->netstack_icmp;
2383 2384 uint_t data_len;
2384 2385 uint32_t cksum;
2385 2386
2386 2387 data_len = msgdsize(data_mp);
2387 2388 mp = conn_prepend_hdr(ixa, ipp, v6src, v6dst, connp->conn_proto,
2388 2389 flowinfo, 0, data_mp, data_len, is->is_wroff_extra, &cksum, errorp);
2389 2390 if (mp == NULL) {
2390 2391 ASSERT(*errorp != 0);
2391 2392 return (NULL);
2392 2393 }
2393 2394
2394 2395 ixa->ixa_pktlen = data_len + ixa->ixa_ip_hdr_length;
2395 2396
2396 2397 /*
2397 2398 * If there was a routing option/header then conn_prepend_hdr
2398 2399 * has massaged it and placed the pseudo-header checksum difference
2399 2400 * in the cksum argument.
2400 2401 *
2401 2402 * Prepare for ICMPv6 checksum done in IP.
2402 2403 *
2403 2404 * We make it easy for IP to include our pseudo header
2404 2405 * by putting our length (and any routing header adjustment)
2405 2406 * in the ICMPv6 checksum field.
2406 2407 * The IP source, destination, and length have already been set by
2407 2408 * conn_prepend_hdr.
2408 2409 */
2409 2410 cksum += data_len;
2410 2411 cksum = (cksum >> 16) + (cksum & 0xFFFF);
2411 2412 ASSERT(cksum < 0x10000);
2412 2413
2413 2414 if (ixa->ixa_flags & IXAF_IS_IPV4) {
2414 2415 ipha_t *ipha = (ipha_t *)mp->b_rptr;
2415 2416
2416 2417 ASSERT(ntohs(ipha->ipha_length) == ixa->ixa_pktlen);
2417 2418 } else {
2418 2419 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
2419 2420 uint_t cksum_offset = 0;
2420 2421
2421 2422 ASSERT(ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN == ixa->ixa_pktlen);
2422 2423
2423 2424 if (ixa->ixa_flags & IXAF_SET_ULP_CKSUM) {
2424 2425 if (connp->conn_proto == IPPROTO_ICMPV6) {
2425 2426 cksum_offset = ixa->ixa_ip_hdr_length +
2426 2427 offsetof(icmp6_t, icmp6_cksum);
2427 2428 } else if (ixa->ixa_flags & IXAF_SET_RAW_CKSUM) {
2428 2429 cksum_offset = ixa->ixa_ip_hdr_length +
2429 2430 ixa->ixa_raw_cksum_offset;
2430 2431 }
2431 2432 }
2432 2433 if (cksum_offset != 0) {
2433 2434 uint16_t *ptr;
2434 2435
2435 2436 /* Make sure the checksum fits in the first mblk */
2436 2437 if (cksum_offset + sizeof (short) > MBLKL(mp)) {
2437 2438 mblk_t *mp1;
2438 2439
2439 2440 mp1 = msgpullup(mp,
2440 2441 cksum_offset + sizeof (short));
2441 2442 freemsg(mp);
2442 2443 if (mp1 == NULL) {
2443 2444 *errorp = ENOMEM;
2444 2445 return (NULL);
2445 2446 }
2446 2447 mp = mp1;
2447 2448 ip6h = (ip6_t *)mp->b_rptr;
2448 2449 }
2449 2450 ptr = (uint16_t *)(mp->b_rptr + cksum_offset);
2450 2451 *ptr = htons(cksum);
2451 2452 }
2452 2453 }
2453 2454
2454 2455 /* Note that we don't try to update wroff due to ancillary data */
2455 2456 return (mp);
2456 2457 }
2457 2458
2458 2459 static int
2459 2460 icmp_build_hdr_template(conn_t *connp, const in6_addr_t *v6src,
2460 2461 const in6_addr_t *v6dst, uint32_t flowinfo)
2461 2462 {
2462 2463 int error;
2463 2464
2464 2465 ASSERT(MUTEX_HELD(&connp->conn_lock));
2465 2466 /*
2466 2467 * We clear lastdst to make sure we don't use the lastdst path
2467 2468 * next time sending since we might not have set v6dst yet.
2468 2469 */
2469 2470 connp->conn_v6lastdst = ipv6_all_zeros;
2470 2471
2471 2472 error = conn_build_hdr_template(connp, 0, 0, v6src, v6dst, flowinfo);
2472 2473 if (error != 0)
2473 2474 return (error);
2474 2475
2475 2476 /*
2476 2477 * Any routing header/option has been massaged. The checksum difference
2477 2478 * is stored in conn_sum.
2478 2479 */
2479 2480 return (0);
2480 2481 }
2481 2482
2482 2483 static mblk_t *
2483 2484 icmp_queue_fallback(icmp_t *icmp, mblk_t *mp)
2484 2485 {
2485 2486 ASSERT(MUTEX_HELD(&icmp->icmp_recv_lock));
2486 2487 if (IPCL_IS_NONSTR(icmp->icmp_connp)) {
2487 2488 /*
2488 2489 * fallback has started but messages have not been moved yet
2489 2490 */
2490 2491 if (icmp->icmp_fallback_queue_head == NULL) {
2491 2492 ASSERT(icmp->icmp_fallback_queue_tail == NULL);
2492 2493 icmp->icmp_fallback_queue_head = mp;
2493 2494 icmp->icmp_fallback_queue_tail = mp;
2494 2495 } else {
2495 2496 ASSERT(icmp->icmp_fallback_queue_tail != NULL);
2496 2497 icmp->icmp_fallback_queue_tail->b_next = mp;
2497 2498 icmp->icmp_fallback_queue_tail = mp;
2498 2499 }
2499 2500 return (NULL);
2500 2501 } else {
2501 2502 /*
2502 2503 * Fallback completed, let the caller putnext() the mblk.
2503 2504 */
2504 2505 return (mp);
2505 2506 }
2506 2507 }
2507 2508
2508 2509 /*
2509 2510 * Deliver data to ULP. In case we have a socket, and it's falling back to
2510 2511 * TPI, then we'll queue the mp for later processing.
2511 2512 */
2512 2513 static void
2513 2514 icmp_ulp_recv(conn_t *connp, mblk_t *mp, uint_t len)
2514 2515 {
2515 2516 if (IPCL_IS_NONSTR(connp)) {
2516 2517 icmp_t *icmp = connp->conn_icmp;
2517 2518 int error;
2518 2519
2519 2520 ASSERT(len == msgdsize(mp));
2520 2521 if ((*connp->conn_upcalls->su_recv)
2521 2522 (connp->conn_upper_handle, mp, len, 0, &error, NULL) < 0) {
2522 2523 mutex_enter(&icmp->icmp_recv_lock);
2523 2524 if (error == ENOSPC) {
2524 2525 /*
2525 2526 * let's confirm while holding the lock
2526 2527 */
2527 2528 if ((*connp->conn_upcalls->su_recv)
2528 2529 (connp->conn_upper_handle, NULL, 0, 0,
2529 2530 &error, NULL) < 0) {
2530 2531 ASSERT(error == ENOSPC);
2531 2532 if (error == ENOSPC) {
2532 2533 connp->conn_flow_cntrld =
2533 2534 B_TRUE;
2534 2535 }
2535 2536 }
2536 2537 mutex_exit(&icmp->icmp_recv_lock);
2537 2538 } else {
2538 2539 ASSERT(error == EOPNOTSUPP);
2539 2540 mp = icmp_queue_fallback(icmp, mp);
2540 2541 mutex_exit(&icmp->icmp_recv_lock);
2541 2542 if (mp != NULL)
2542 2543 putnext(connp->conn_rq, mp);
2543 2544 }
2544 2545 }
2545 2546 ASSERT(MUTEX_NOT_HELD(&icmp->icmp_recv_lock));
2546 2547 } else {
2547 2548 putnext(connp->conn_rq, mp);
2548 2549 }
2549 2550 }
2550 2551
2551 2552 /*
2552 2553 * This is the inbound data path.
2553 2554 * IP has already pulled up the IP headers and verified alignment
2554 2555 * etc.
2555 2556 */
2556 2557 /* ARGSUSED2 */
2557 2558 static void
2558 2559 icmp_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
2559 2560 {
2560 2561 conn_t *connp = (conn_t *)arg1;
2561 2562 struct T_unitdata_ind *tudi;
2562 2563 uchar_t *rptr; /* Pointer to IP header */
2563 2564 int ip_hdr_length;
2564 2565 int udi_size; /* Size of T_unitdata_ind */
2565 2566 int pkt_len;
2566 2567 icmp_t *icmp;
2567 2568 ip_pkt_t ipps;
2568 2569 ip6_t *ip6h;
2569 2570 mblk_t *mp1;
2570 2571 crb_t recv_ancillary;
2571 2572 icmp_stack_t *is;
2572 2573 sin_t *sin;
2573 2574 sin6_t *sin6;
2574 2575 ipha_t *ipha;
2575 2576
2576 2577 ASSERT(connp->conn_flags & IPCL_RAWIPCONN);
2577 2578
2578 2579 icmp = connp->conn_icmp;
2579 2580 is = icmp->icmp_is;
2580 2581 rptr = mp->b_rptr;
2581 2582
2582 2583 ASSERT(DB_TYPE(mp) == M_DATA);
2583 2584 ASSERT(OK_32PTR(rptr));
2584 2585 ASSERT(ira->ira_pktlen == msgdsize(mp));
2585 2586 pkt_len = ira->ira_pktlen;
2586 2587
2587 2588 /*
2588 2589 * Get a snapshot of these and allow other threads to change
2589 2590 * them after that. We need the same recv_ancillary when determining
2590 2591 * the size as when adding the ancillary data items.
2591 2592 */
2592 2593 mutex_enter(&connp->conn_lock);
2593 2594 recv_ancillary = connp->conn_recv_ancillary;
2594 2595 mutex_exit(&connp->conn_lock);
2595 2596
2596 2597 ip_hdr_length = ira->ira_ip_hdr_length;
2597 2598 ASSERT(MBLKL(mp) >= ip_hdr_length); /* IP did a pullup */
2598 2599
2599 2600 /* Initialize regardless of IP version */
2600 2601 ipps.ipp_fields = 0;
2601 2602
2602 2603 if (ira->ira_flags & IRAF_IS_IPV4) {
2603 2604 ASSERT(IPH_HDR_VERSION(rptr) == IPV4_VERSION);
2604 2605 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
2605 2606 ASSERT(ira->ira_ip_hdr_length == IPH_HDR_LENGTH(rptr));
2606 2607
2607 2608 ipha = (ipha_t *)mp->b_rptr;
2608 2609 if (recv_ancillary.crb_all != 0)
2609 2610 (void) ip_find_hdr_v4(ipha, &ipps, B_FALSE);
2610 2611
2611 2612 /*
2612 2613 * BSD for some reason adjusts ipha_length to exclude the
2613 2614 * IP header length. We do the same.
2614 2615 */
2615 2616 if (is->is_bsd_compat) {
2616 2617 ushort_t len;
2617 2618
2618 2619 len = ntohs(ipha->ipha_length);
2619 2620 if (mp->b_datap->db_ref > 1) {
2620 2621 /*
2621 2622 * Allocate a new IP header so that we can
2622 2623 * modify ipha_length.
2623 2624 */
2624 2625 mblk_t *mp1;
2625 2626
2626 2627 mp1 = allocb(ip_hdr_length, BPRI_MED);
2627 2628 if (mp1 == NULL) {
2628 2629 freemsg(mp);
2629 2630 BUMP_MIB(&is->is_rawip_mib,
2630 2631 rawipInErrors);
2631 2632 return;
2632 2633 }
2633 2634 bcopy(rptr, mp1->b_rptr, ip_hdr_length);
2634 2635 mp->b_rptr = rptr + ip_hdr_length;
2635 2636 rptr = mp1->b_rptr;
2636 2637 ipha = (ipha_t *)rptr;
2637 2638 mp1->b_cont = mp;
2638 2639 mp1->b_wptr = rptr + ip_hdr_length;
2639 2640 mp = mp1;
2640 2641 }
2641 2642 len -= ip_hdr_length;
2642 2643 ipha->ipha_length = htons(len);
2643 2644 }
2644 2645
2645 2646 /*
2646 2647 * For RAW sockets we not pass ICMP/IPv4 packets to AF_INET6
2647 2648 * sockets. This is ensured by icmp_bind and the IP fanout code.
2648 2649 */
2649 2650 ASSERT(connp->conn_family == AF_INET);
2650 2651
2651 2652 /*
2652 2653 * This is the inbound data path. Packets are passed upstream
2653 2654 * as T_UNITDATA_IND messages with full IPv4 headers still
2654 2655 * attached.
2655 2656 */
2656 2657
2657 2658 /*
2658 2659 * Normally only send up the source address.
2659 2660 * If any ancillary data items are wanted we add those.
2660 2661 */
2661 2662 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin_t);
2662 2663 if (recv_ancillary.crb_all != 0) {
2663 2664 udi_size += conn_recvancillary_size(connp,
2664 2665 recv_ancillary, ira, mp, &ipps);
2665 2666 }
2666 2667
2667 2668 /* Allocate a message block for the T_UNITDATA_IND structure. */
2668 2669 mp1 = allocb(udi_size, BPRI_MED);
2669 2670 if (mp1 == NULL) {
2670 2671 freemsg(mp);
2671 2672 BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
2672 2673 return;
2673 2674 }
2674 2675 mp1->b_cont = mp;
2675 2676 tudi = (struct T_unitdata_ind *)mp1->b_rptr;
2676 2677 mp1->b_datap->db_type = M_PROTO;
2677 2678 mp1->b_wptr = (uchar_t *)tudi + udi_size;
2678 2679 tudi->PRIM_type = T_UNITDATA_IND;
2679 2680 tudi->SRC_length = sizeof (sin_t);
2680 2681 tudi->SRC_offset = sizeof (struct T_unitdata_ind);
2681 2682 sin = (sin_t *)&tudi[1];
2682 2683 *sin = sin_null;
2683 2684 sin->sin_family = AF_INET;
2684 2685 sin->sin_addr.s_addr = ipha->ipha_src;
2685 2686 *(uint32_t *)&sin->sin_zero[0] = 0;
2686 2687 *(uint32_t *)&sin->sin_zero[4] = 0;
2687 2688 tudi->OPT_offset = sizeof (struct T_unitdata_ind) +
2688 2689 sizeof (sin_t);
2689 2690 udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin_t));
2690 2691 tudi->OPT_length = udi_size;
2691 2692
2692 2693 /*
2693 2694 * Add options if IP_RECVIF etc is set
2694 2695 */
2695 2696 if (udi_size != 0) {
2696 2697 conn_recvancillary_add(connp, recv_ancillary, ira,
2697 2698 &ipps, (uchar_t *)&sin[1], udi_size);
2698 2699 }
2699 2700 goto deliver;
2700 2701 }
2701 2702
2702 2703 ASSERT(IPH_HDR_VERSION(rptr) == IPV6_VERSION);
2703 2704 /*
2704 2705 * IPv6 packets can only be received by applications
2705 2706 * that are prepared to receive IPv6 addresses.
2706 2707 * The IP fanout must ensure this.
2707 2708 */
2708 2709 ASSERT(connp->conn_family == AF_INET6);
2709 2710
2710 2711 /*
2711 2712 * Handle IPv6 packets. We don't pass up the IP headers with the
2712 2713 * payload for IPv6.
2713 2714 */
2714 2715
2715 2716 ip6h = (ip6_t *)rptr;
2716 2717 if (recv_ancillary.crb_all != 0) {
2717 2718 /*
2718 2719 * Call on ip_find_hdr_v6 which gets individual lenghts of
2719 2720 * extension headers (and pointers to them).
2720 2721 */
2721 2722 uint8_t nexthdr;
2722 2723
2723 2724 /* We don't care about the length or nextheader. */
2724 2725 (void) ip_find_hdr_v6(mp, ip6h, B_TRUE, &ipps, &nexthdr);
2725 2726
2726 2727 /*
2727 2728 * We do not pass up hop-by-hop options or any other
2728 2729 * extension header as part of the packet. Applications
2729 2730 * that want to see them have to specify IPV6_RECV* socket
2730 2731 * options. And conn_recvancillary_size/add explicitly
2731 2732 * drops the TX option from IPV6_HOPOPTS as it does for UDP.
2732 2733 *
2733 2734 * If we had multilevel ICMP sockets, then we'd want to
2734 2735 * modify conn_recvancillary_size/add to
2735 2736 * allow the user to see the label.
2736 2737 */
2737 2738 }
2738 2739
2739 2740 /*
2740 2741 * Check a filter for ICMPv6 types if needed.
2741 2742 * Verify raw checksums if needed.
2742 2743 */
2743 2744 mutex_enter(&connp->conn_lock);
2744 2745 if (icmp->icmp_filter != NULL) {
2745 2746 int type;
2746 2747
2747 2748 /* Assumes that IP has done the pullupmsg */
2748 2749 type = mp->b_rptr[ip_hdr_length];
2749 2750
2750 2751 ASSERT(mp->b_rptr + ip_hdr_length <= mp->b_wptr);
2751 2752 if (ICMP6_FILTER_WILLBLOCK(type, icmp->icmp_filter)) {
2752 2753 mutex_exit(&connp->conn_lock);
2753 2754 freemsg(mp);
2754 2755 return;
2755 2756 }
2756 2757 }
2757 2758 if (connp->conn_ixa->ixa_flags & IXAF_SET_RAW_CKSUM) {
2758 2759 /* Checksum */
2759 2760 uint16_t *up;
2760 2761 uint32_t sum;
2761 2762 int remlen;
2762 2763
2763 2764 up = (uint16_t *)&ip6h->ip6_src;
2764 2765
2765 2766 remlen = msgdsize(mp) - ip_hdr_length;
2766 2767 sum = htons(connp->conn_proto + remlen)
2767 2768 + up[0] + up[1] + up[2] + up[3]
2768 2769 + up[4] + up[5] + up[6] + up[7]
2769 2770 + up[8] + up[9] + up[10] + up[11]
2770 2771 + up[12] + up[13] + up[14] + up[15];
2771 2772 sum = (sum & 0xffff) + (sum >> 16);
2772 2773 sum = IP_CSUM(mp, ip_hdr_length, sum);
2773 2774 if (sum != 0) {
2774 2775 /* IPv6 RAW checksum failed */
2775 2776 ip0dbg(("icmp_rput: RAW checksum failed %x\n", sum));
2776 2777 mutex_exit(&connp->conn_lock);
2777 2778 freemsg(mp);
2778 2779 BUMP_MIB(&is->is_rawip_mib, rawipInCksumErrs);
2779 2780 return;
2780 2781 }
2781 2782 }
2782 2783 mutex_exit(&connp->conn_lock);
2783 2784
2784 2785 udi_size = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
2785 2786
2786 2787 if (recv_ancillary.crb_all != 0) {
2787 2788 udi_size += conn_recvancillary_size(connp,
2788 2789 recv_ancillary, ira, mp, &ipps);
2789 2790 }
2790 2791
2791 2792 mp1 = allocb(udi_size, BPRI_MED);
2792 2793 if (mp1 == NULL) {
2793 2794 freemsg(mp);
2794 2795 BUMP_MIB(&is->is_rawip_mib, rawipInErrors);
2795 2796 return;
2796 2797 }
2797 2798 mp1->b_cont = mp;
2798 2799 mp1->b_datap->db_type = M_PROTO;
2799 2800 tudi = (struct T_unitdata_ind *)mp1->b_rptr;
2800 2801 mp1->b_wptr = (uchar_t *)tudi + udi_size;
2801 2802 tudi->PRIM_type = T_UNITDATA_IND;
2802 2803 tudi->SRC_length = sizeof (sin6_t);
2803 2804 tudi->SRC_offset = sizeof (struct T_unitdata_ind);
2804 2805 tudi->OPT_offset = sizeof (struct T_unitdata_ind) + sizeof (sin6_t);
2805 2806 udi_size -= (sizeof (struct T_unitdata_ind) + sizeof (sin6_t));
2806 2807 tudi->OPT_length = udi_size;
2807 2808 sin6 = (sin6_t *)&tudi[1];
2808 2809 *sin6 = sin6_null;
2809 2810 sin6->sin6_port = 0;
2810 2811 sin6->sin6_family = AF_INET6;
2811 2812
2812 2813 sin6->sin6_addr = ip6h->ip6_src;
2813 2814 /* No sin6_flowinfo per API */
2814 2815 sin6->sin6_flowinfo = 0;
2815 2816 /* For link-scope pass up scope id */
2816 2817 if (IN6_IS_ADDR_LINKSCOPE(&ip6h->ip6_src))
2817 2818 sin6->sin6_scope_id = ira->ira_ruifindex;
2818 2819 else
2819 2820 sin6->sin6_scope_id = 0;
2820 2821 sin6->__sin6_src_id = ip_srcid_find_addr(&ip6h->ip6_dst,
2821 2822 IPCL_ZONEID(connp), is->is_netstack);
2822 2823
2823 2824 if (udi_size != 0) {
2824 2825 conn_recvancillary_add(connp, recv_ancillary, ira,
2825 2826 &ipps, (uchar_t *)&sin6[1], udi_size);
2826 2827 }
2827 2828
2828 2829 /* Skip all the IPv6 headers per API */
2829 2830 mp->b_rptr += ip_hdr_length;
2830 2831 pkt_len -= ip_hdr_length;
2831 2832
2832 2833 deliver:
2833 2834 BUMP_MIB(&is->is_rawip_mib, rawipInDatagrams);
2834 2835 icmp_ulp_recv(connp, mp1, pkt_len);
2835 2836 }
2836 2837
2837 2838 /*
2838 2839 * return SNMP stuff in buffer in mpdata. We don't hold any lock and report
2839 2840 * information that can be changing beneath us.
2840 2841 */
2841 2842 mblk_t *
2842 2843 icmp_snmp_get(queue_t *q, mblk_t *mpctl)
2843 2844 {
2844 2845 mblk_t *mpdata;
2845 2846 struct opthdr *optp;
2846 2847 conn_t *connp = Q_TO_CONN(q);
2847 2848 icmp_stack_t *is = connp->conn_netstack->netstack_icmp;
2848 2849 mblk_t *mp2ctl;
2849 2850
2850 2851 /*
2851 2852 * make a copy of the original message
2852 2853 */
2853 2854 mp2ctl = copymsg(mpctl);
2854 2855
2855 2856 if (mpctl == NULL ||
2856 2857 (mpdata = mpctl->b_cont) == NULL) {
2857 2858 freemsg(mpctl);
2858 2859 freemsg(mp2ctl);
2859 2860 return (0);
2860 2861 }
2861 2862
2862 2863 /* fixed length structure for IPv4 and IPv6 counters */
2863 2864 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
2864 2865 optp->level = EXPER_RAWIP;
2865 2866 optp->name = 0;
2866 2867 (void) snmp_append_data(mpdata, (char *)&is->is_rawip_mib,
2867 2868 sizeof (is->is_rawip_mib));
2868 2869 optp->len = msgdsize(mpdata);
2869 2870 qreply(q, mpctl);
2870 2871
2871 2872 return (mp2ctl);
2872 2873 }
2873 2874
2874 2875 /*
2875 2876 * Return 0 if invalid set request, 1 otherwise, including non-rawip requests.
2876 2877 * TODO: If this ever actually tries to set anything, it needs to be
2877 2878 * to do the appropriate locking.
2878 2879 */
2879 2880 /* ARGSUSED */
2880 2881 int
2881 2882 icmp_snmp_set(queue_t *q, t_scalar_t level, t_scalar_t name,
2882 2883 uchar_t *ptr, int len)
2883 2884 {
2884 2885 switch (level) {
2885 2886 case EXPER_RAWIP:
2886 2887 return (0);
2887 2888 default:
2888 2889 return (1);
2889 2890 }
2890 2891 }
2891 2892
2892 2893 /*
2893 2894 * This routine creates a T_UDERROR_IND message and passes it upstream.
2894 2895 * The address and options are copied from the T_UNITDATA_REQ message
2895 2896 * passed in mp. This message is freed.
2896 2897 */
2897 2898 static void
2898 2899 icmp_ud_err(queue_t *q, mblk_t *mp, t_scalar_t err)
2899 2900 {
2900 2901 struct T_unitdata_req *tudr;
2901 2902 mblk_t *mp1;
2902 2903 uchar_t *destaddr;
2903 2904 t_scalar_t destlen;
2904 2905 uchar_t *optaddr;
2905 2906 t_scalar_t optlen;
2906 2907
2907 2908 if ((mp->b_wptr < mp->b_rptr) ||
2908 2909 (MBLKL(mp)) < sizeof (struct T_unitdata_req)) {
2909 2910 goto done;
2910 2911 }
2911 2912 tudr = (struct T_unitdata_req *)mp->b_rptr;
2912 2913 destaddr = mp->b_rptr + tudr->DEST_offset;
2913 2914 if (destaddr < mp->b_rptr || destaddr >= mp->b_wptr ||
2914 2915 destaddr + tudr->DEST_length < mp->b_rptr ||
2915 2916 destaddr + tudr->DEST_length > mp->b_wptr) {
2916 2917 goto done;
2917 2918 }
2918 2919 optaddr = mp->b_rptr + tudr->OPT_offset;
2919 2920 if (optaddr < mp->b_rptr || optaddr >= mp->b_wptr ||
2920 2921 optaddr + tudr->OPT_length < mp->b_rptr ||
2921 2922 optaddr + tudr->OPT_length > mp->b_wptr) {
2922 2923 goto done;
2923 2924 }
2924 2925 destlen = tudr->DEST_length;
2925 2926 optlen = tudr->OPT_length;
2926 2927
2927 2928 mp1 = mi_tpi_uderror_ind((char *)destaddr, destlen,
2928 2929 (char *)optaddr, optlen, err);
2929 2930 if (mp1 != NULL)
2930 2931 qreply(q, mp1);
2931 2932
2932 2933 done:
2933 2934 freemsg(mp);
2934 2935 }
2935 2936
2936 2937 static int
2937 2938 rawip_do_unbind(conn_t *connp)
2938 2939 {
2939 2940 icmp_t *icmp = connp->conn_icmp;
2940 2941
2941 2942 mutex_enter(&connp->conn_lock);
2942 2943 /* If a bind has not been done, we can't unbind. */
2943 2944 if (icmp->icmp_state == TS_UNBND) {
2944 2945 mutex_exit(&connp->conn_lock);
2945 2946 return (-TOUTSTATE);
2946 2947 }
2947 2948 connp->conn_saddr_v6 = ipv6_all_zeros;
2948 2949 connp->conn_bound_addr_v6 = ipv6_all_zeros;
2949 2950 connp->conn_laddr_v6 = ipv6_all_zeros;
2950 2951 connp->conn_mcbc_bind = B_FALSE;
2951 2952 connp->conn_lport = 0;
2952 2953 connp->conn_fport = 0;
2953 2954 /* In case we were also connected */
2954 2955 connp->conn_faddr_v6 = ipv6_all_zeros;
2955 2956 connp->conn_v6lastdst = ipv6_all_zeros;
2956 2957
2957 2958 icmp->icmp_state = TS_UNBND;
2958 2959
2959 2960 (void) icmp_build_hdr_template(connp, &connp->conn_saddr_v6,
2960 2961 &connp->conn_faddr_v6, connp->conn_flowinfo);
2961 2962 mutex_exit(&connp->conn_lock);
2962 2963
2963 2964 ip_unbind(connp);
2964 2965 return (0);
2965 2966 }
2966 2967
2967 2968 /*
2968 2969 * This routine is called by icmp_wput to handle T_UNBIND_REQ messages.
2969 2970 * After some error checking, the message is passed downstream to ip.
2970 2971 */
2971 2972 static void
2972 2973 icmp_tpi_unbind(queue_t *q, mblk_t *mp)
2973 2974 {
2974 2975 conn_t *connp = Q_TO_CONN(q);
2975 2976 int error;
2976 2977
2977 2978 ASSERT(mp->b_cont == NULL);
2978 2979 error = rawip_do_unbind(connp);
2979 2980 if (error) {
2980 2981 if (error < 0) {
2981 2982 icmp_err_ack(q, mp, -error, 0);
2982 2983 } else {
2983 2984 icmp_err_ack(q, mp, 0, error);
2984 2985 }
2985 2986 return;
2986 2987 }
2987 2988
2988 2989 /*
2989 2990 * Convert mp into a T_OK_ACK
2990 2991 */
2991 2992
2992 2993 mp = mi_tpi_ok_ack_alloc(mp);
2993 2994
2994 2995 /*
2995 2996 * should not happen in practice... T_OK_ACK is smaller than the
2996 2997 * original message.
2997 2998 */
2998 2999 ASSERT(mp != NULL);
2999 3000 ASSERT(((struct T_ok_ack *)mp->b_rptr)->PRIM_type == T_OK_ACK);
3000 3001 qreply(q, mp);
3001 3002 }
3002 3003
3003 3004 /*
3004 3005 * Process IPv4 packets that already include an IP header.
3005 3006 * Used when IP_HDRINCL has been set (implicit for IPPROTO_RAW and
3006 3007 * IPPROTO_IGMP).
3007 3008 * In this case we ignore the address and any options in the T_UNITDATA_REQ.
3008 3009 *
3009 3010 * The packet is assumed to have a base (20 byte) IP header followed
3010 3011 * by the upper-layer protocol. We include any IP_OPTIONS including a
3011 3012 * CIPSO label but otherwise preserve the base IP header.
3012 3013 */
3013 3014 static int
3014 3015 icmp_output_hdrincl(conn_t *connp, mblk_t *mp, cred_t *cr, pid_t pid)
3015 3016 {
3016 3017 icmp_t *icmp = connp->conn_icmp;
3017 3018 icmp_stack_t *is = icmp->icmp_is;
3018 3019 ipha_t iphas;
3019 3020 ipha_t *ipha;
3020 3021 int ip_hdr_length;
3021 3022 int tp_hdr_len;
3022 3023 ip_xmit_attr_t *ixa;
3023 3024 ip_pkt_t *ipp;
3024 3025 in6_addr_t v6src;
3025 3026 in6_addr_t v6dst;
3026 3027 in6_addr_t v6nexthop;
3027 3028 int error;
3028 3029 boolean_t do_ipsec;
3029 3030
3030 3031 /*
3031 3032 * We need an exclusive copy of conn_ixa since the included IP
3032 3033 * header could have any destination.
3033 3034 * That copy has no pointers hence we
3034 3035 * need to set them up once we've parsed the ancillary data.
3035 3036 */
3036 3037 ixa = conn_get_ixa_exclusive(connp);
3037 3038 if (ixa == NULL) {
3038 3039 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3039 3040 freemsg(mp);
3040 3041 return (ENOMEM);
3041 3042 }
3042 3043 ASSERT(cr != NULL);
3043 3044 /*
3044 3045 * Caller has a reference on cr; from db_credp or because we
3045 3046 * are running in process context.
3046 3047 */
3047 3048 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3048 3049 ixa->ixa_cred = cr;
3049 3050 ixa->ixa_cpid = pid;
3050 3051 if (is_system_labeled()) {
3051 3052 /* We need to restart with a label based on the cred */
3052 3053 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred);
3053 3054 }
3054 3055
3055 3056 /* In case previous destination was multicast or multirt */
3056 3057 ip_attr_newdst(ixa);
3057 3058
3058 3059 /* Get a copy of conn_xmit_ipp since the TX label might change it */
3059 3060 ipp = kmem_zalloc(sizeof (*ipp), KM_NOSLEEP);
3060 3061 if (ipp == NULL) {
3061 3062 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3062 3063 ixa->ixa_cred = connp->conn_cred; /* Restore */
3063 3064 ixa->ixa_cpid = connp->conn_cpid;
3064 3065 ixa_refrele(ixa);
3065 3066 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3066 3067 freemsg(mp);
3067 3068 return (ENOMEM);
3068 3069 }
3069 3070 mutex_enter(&connp->conn_lock);
3070 3071 error = ip_pkt_copy(&connp->conn_xmit_ipp, ipp, KM_NOSLEEP);
3071 3072 mutex_exit(&connp->conn_lock);
3072 3073 if (error != 0) {
3073 3074 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3074 3075 freemsg(mp);
3075 3076 goto done;
3076 3077 }
3077 3078
3078 3079 /* Sanity check length of packet */
3079 3080 ipha = (ipha_t *)mp->b_rptr;
3080 3081
3081 3082 ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
3082 3083 if ((mp->b_wptr - mp->b_rptr) < IP_SIMPLE_HDR_LENGTH) {
3083 3084 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
3084 3085 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3085 3086 freemsg(mp);
3086 3087 goto done;
3087 3088 }
3088 3089 ipha = (ipha_t *)mp->b_rptr;
3089 3090 }
3090 3091 ipha->ipha_version_and_hdr_length =
3091 3092 (IP_VERSION<<4) | (ip_hdr_length>>2);
3092 3093
3093 3094 /*
3094 3095 * We set IXAF_DONTFRAG if the application set DF which makes
3095 3096 * IP not fragment.
3096 3097 */
3097 3098 ipha->ipha_fragment_offset_and_flags &= htons(IPH_DF);
3098 3099 if (ipha->ipha_fragment_offset_and_flags & htons(IPH_DF))
3099 3100 ixa->ixa_flags |= (IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
3100 3101 else
3101 3102 ixa->ixa_flags &= ~(IXAF_DONTFRAG | IXAF_PMTU_IPV4_DF);
3102 3103
3103 3104 /* Even for multicast and broadcast we honor the apps ttl */
3104 3105 ixa->ixa_flags |= IXAF_NO_TTL_CHANGE;
3105 3106
3106 3107 /*
3107 3108 * No source verification for non-local addresses
3108 3109 */
3109 3110 if (ipha->ipha_src != INADDR_ANY &&
3110 3111 ip_laddr_verify_v4(ipha->ipha_src, ixa->ixa_zoneid,
3111 3112 is->is_netstack->netstack_ip, B_FALSE)
3112 3113 != IPVL_UNICAST_UP) {
3113 3114 ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
3114 3115 }
3115 3116
3116 3117 if (ipha->ipha_dst == INADDR_ANY)
3117 3118 ipha->ipha_dst = htonl(INADDR_LOOPBACK);
3118 3119
3119 3120 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src);
3120 3121 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &v6dst);
3121 3122
3122 3123 /* Defer IPsec if it might need to look at ICMP type/code */
3123 3124 do_ipsec = ipha->ipha_protocol != IPPROTO_ICMP;
3124 3125 ixa->ixa_flags |= IXAF_IS_IPV4;
3125 3126
3126 3127 ip_attr_nexthop(ipp, ixa, &v6dst, &v6nexthop);
3127 3128 error = ip_attr_connect(connp, ixa, &v6src, &v6dst, &v6nexthop,
3128 3129 connp->conn_fport, &v6src, NULL, IPDF_ALLOW_MCBC | IPDF_VERIFY_DST |
3129 3130 (do_ipsec ? IPDF_IPSEC : 0));
3130 3131 switch (error) {
3131 3132 case 0:
3132 3133 break;
3133 3134 case EADDRNOTAVAIL:
3134 3135 /*
3135 3136 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3136 3137 * Don't have the application see that errno
3137 3138 */
3138 3139 error = ENETUNREACH;
3139 3140 goto failed;
3140 3141 case ENETDOWN:
3141 3142 /*
3142 3143 * Have !ipif_addr_ready address; drop packet silently
3143 3144 * until we can get applications to not send until we
3144 3145 * are ready.
3145 3146 */
3146 3147 error = 0;
3147 3148 goto failed;
3148 3149 case EHOSTUNREACH:
3149 3150 case ENETUNREACH:
3150 3151 if (ixa->ixa_ire != NULL) {
3151 3152 /*
3152 3153 * Let conn_ip_output/ire_send_noroute return
3153 3154 * the error and send any local ICMP error.
3154 3155 */
3155 3156 error = 0;
3156 3157 break;
3157 3158 }
3158 3159 /* FALLTHRU */
3159 3160 default:
3160 3161 failed:
3161 3162 freemsg(mp);
3162 3163 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3163 3164 goto done;
3164 3165 }
3165 3166 if (ipha->ipha_src == INADDR_ANY)
3166 3167 IN6_V4MAPPED_TO_IPADDR(&v6src, ipha->ipha_src);
3167 3168
3168 3169 /*
3169 3170 * We might be going to a different destination than last time,
3170 3171 * thus check that TX allows the communication and compute any
3171 3172 * needed label.
3172 3173 *
3173 3174 * TSOL Note: We have an exclusive ipp and ixa for this thread so we
3174 3175 * don't have to worry about concurrent threads.
3175 3176 */
3176 3177 if (is_system_labeled()) {
3177 3178 /*
3178 3179 * Check whether Trusted Solaris policy allows communication
3179 3180 * with this host, and pretend that the destination is
3180 3181 * unreachable if not.
3181 3182 * Compute any needed label and place it in ipp_label_v4/v6.
3182 3183 *
3183 3184 * Later conn_build_hdr_template/conn_prepend_hdr takes
3184 3185 * ipp_label_v4/v6 to form the packet.
3185 3186 *
3186 3187 * Tsol note: We have ipp structure local to this thread so
3187 3188 * no locking is needed.
3188 3189 */
3189 3190 error = conn_update_label(connp, ixa, &v6dst, ipp);
3190 3191 if (error != 0) {
3191 3192 freemsg(mp);
3192 3193 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3193 3194 goto done;
3194 3195 }
3195 3196 }
3196 3197
3197 3198 /*
3198 3199 * Save away a copy of the IPv4 header the application passed down
3199 3200 * and then prepend an IPv4 header complete with any IP options
3200 3201 * including label.
3201 3202 * We need a struct copy since icmp_prepend_hdr will reuse the available
3202 3203 * space in the mblk.
3203 3204 */
3204 3205 iphas = *ipha;
3205 3206 mp->b_rptr += IP_SIMPLE_HDR_LENGTH;
3206 3207
3207 3208 mp = icmp_prepend_hdr(connp, ixa, ipp, &v6src, &v6dst, 0, mp, &error);
3208 3209 if (mp == NULL) {
3209 3210 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3210 3211 ASSERT(error != 0);
3211 3212 goto done;
3212 3213 }
3213 3214 if (ixa->ixa_pktlen > IP_MAXPACKET) {
3214 3215 error = EMSGSIZE;
3215 3216 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3216 3217 freemsg(mp);
3217 3218 goto done;
3218 3219 }
3219 3220 /* Restore key parts of the header that the application passed down */
3220 3221 ipha = (ipha_t *)mp->b_rptr;
3221 3222 ipha->ipha_type_of_service = iphas.ipha_type_of_service;
3222 3223 ipha->ipha_ident = iphas.ipha_ident;
3223 3224 ipha->ipha_fragment_offset_and_flags =
3224 3225 iphas.ipha_fragment_offset_and_flags;
3225 3226 ipha->ipha_ttl = iphas.ipha_ttl;
3226 3227 ipha->ipha_protocol = iphas.ipha_protocol;
3227 3228 ipha->ipha_src = iphas.ipha_src;
3228 3229 ipha->ipha_dst = iphas.ipha_dst;
3229 3230
3230 3231 ixa->ixa_protocol = ipha->ipha_protocol;
3231 3232
3232 3233 /*
3233 3234 * Make sure that the IP header plus any transport header that is
3234 3235 * checksumed by ip_output is in the first mblk. (ip_output assumes
3235 3236 * that at least the checksum field is in the first mblk.)
3236 3237 */
3237 3238 switch (ipha->ipha_protocol) {
3238 3239 case IPPROTO_UDP:
3239 3240 tp_hdr_len = 8;
3240 3241 break;
3241 3242 case IPPROTO_TCP:
3242 3243 tp_hdr_len = 20;
3243 3244 break;
3244 3245 default:
3245 3246 tp_hdr_len = 0;
3246 3247 break;
3247 3248 }
3248 3249 ip_hdr_length = IPH_HDR_LENGTH(ipha);
3249 3250 if (mp->b_wptr - mp->b_rptr < ip_hdr_length + tp_hdr_len) {
3250 3251 if (!pullupmsg(mp, ip_hdr_length + tp_hdr_len)) {
3251 3252 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3252 3253 if (mp->b_cont == NULL)
3253 3254 error = EINVAL;
3254 3255 else
3255 3256 error = ENOMEM;
3256 3257 freemsg(mp);
3257 3258 goto done;
3258 3259 }
3259 3260 }
3260 3261
3261 3262 if (!do_ipsec) {
3262 3263 /* Policy might differ for different ICMP type/code */
3263 3264 if (ixa->ixa_ipsec_policy != NULL) {
3264 3265 IPPOL_REFRELE(ixa->ixa_ipsec_policy);
3265 3266 ixa->ixa_ipsec_policy = NULL;
3266 3267 ixa->ixa_flags &= ~IXAF_IPSEC_SECURE;
3267 3268 }
3268 3269 mp = ip_output_attach_policy(mp, ipha, NULL, connp, ixa);
3269 3270 if (mp == NULL) {
3270 3271 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3271 3272 error = EHOSTUNREACH; /* IPsec policy failure */
3272 3273 goto done;
3273 3274 }
3274 3275 }
3275 3276
3276 3277 /* We're done. Pass the packet to ip. */
3277 3278 BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
3278 3279
3279 3280 error = conn_ip_output(mp, ixa);
3280 3281 /* No rawipOutErrors if an error since IP increases its error counter */
3281 3282 switch (error) {
3282 3283 case 0:
3283 3284 break;
3284 3285 case EWOULDBLOCK:
3285 3286 (void) ixa_check_drain_insert(connp, ixa);
3286 3287 error = 0;
3287 3288 break;
3288 3289 case EADDRNOTAVAIL:
3289 3290 /*
3290 3291 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3291 3292 * Don't have the application see that errno
3292 3293 */
3293 3294 error = ENETUNREACH;
3294 3295 break;
3295 3296 }
3296 3297 done:
3297 3298 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3298 3299 ixa->ixa_cred = connp->conn_cred; /* Restore */
3299 3300 ixa->ixa_cpid = connp->conn_cpid;
3300 3301 ixa_refrele(ixa);
3301 3302 ip_pkt_free(ipp);
3302 3303 kmem_free(ipp, sizeof (*ipp));
3303 3304 return (error);
3304 3305 }
3305 3306
3306 3307 static mblk_t *
3307 3308 icmp_output_attach_policy(mblk_t *mp, conn_t *connp, ip_xmit_attr_t *ixa)
3308 3309 {
3309 3310 ipha_t *ipha = NULL;
3310 3311 ip6_t *ip6h = NULL;
3311 3312
3312 3313 if (ixa->ixa_flags & IXAF_IS_IPV4)
3313 3314 ipha = (ipha_t *)mp->b_rptr;
3314 3315 else
3315 3316 ip6h = (ip6_t *)mp->b_rptr;
3316 3317
3317 3318 if (ixa->ixa_ipsec_policy != NULL) {
3318 3319 IPPOL_REFRELE(ixa->ixa_ipsec_policy);
3319 3320 ixa->ixa_ipsec_policy = NULL;
3320 3321 ixa->ixa_flags &= ~IXAF_IPSEC_SECURE;
3321 3322 }
3322 3323 return (ip_output_attach_policy(mp, ipha, ip6h, connp, ixa));
3323 3324 }
3324 3325
3325 3326 /*
3326 3327 * Handle T_UNITDATA_REQ with options. Both IPv4 and IPv6
3327 3328 * Either tudr_mp or msg is set. If tudr_mp we take ancillary data from
3328 3329 * the TPI options, otherwise we take them from msg_control.
3329 3330 * If both sin and sin6 is set it is a connected socket and we use conn_faddr.
3330 3331 * Always consumes mp; never consumes tudr_mp.
3331 3332 */
3332 3333 static int
3333 3334 icmp_output_ancillary(conn_t *connp, sin_t *sin, sin6_t *sin6, mblk_t *mp,
3334 3335 mblk_t *tudr_mp, struct nmsghdr *msg, cred_t *cr, pid_t pid)
3335 3336 {
3336 3337 icmp_t *icmp = connp->conn_icmp;
3337 3338 icmp_stack_t *is = icmp->icmp_is;
3338 3339 int error;
3339 3340 ip_xmit_attr_t *ixa;
3340 3341 ip_pkt_t *ipp;
3341 3342 in6_addr_t v6src;
3342 3343 in6_addr_t v6dst;
3343 3344 in6_addr_t v6nexthop;
3344 3345 in_port_t dstport;
3345 3346 uint32_t flowinfo;
3346 3347 int is_absreq_failure = 0;
3347 3348 conn_opt_arg_t coas, *coa;
3348 3349
3349 3350 ASSERT(tudr_mp != NULL || msg != NULL);
3350 3351
3351 3352 /*
3352 3353 * Get ixa before checking state to handle a disconnect race.
3353 3354 *
3354 3355 * We need an exclusive copy of conn_ixa since the ancillary data
3355 3356 * options might modify it. That copy has no pointers hence we
3356 3357 * need to set them up once we've parsed the ancillary data.
3357 3358 */
3358 3359 ixa = conn_get_ixa_exclusive(connp);
3359 3360 if (ixa == NULL) {
3360 3361 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3361 3362 freemsg(mp);
3362 3363 return (ENOMEM);
3363 3364 }
3364 3365 ASSERT(cr != NULL);
3365 3366 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3366 3367 ixa->ixa_cred = cr;
3367 3368 ixa->ixa_cpid = pid;
3368 3369 if (is_system_labeled()) {
3369 3370 /* We need to restart with a label based on the cred */
3370 3371 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred);
3371 3372 }
3372 3373
3373 3374 /* In case previous destination was multicast or multirt */
3374 3375 ip_attr_newdst(ixa);
3375 3376
3376 3377 /* Get a copy of conn_xmit_ipp since the options might change it */
3377 3378 ipp = kmem_zalloc(sizeof (*ipp), KM_NOSLEEP);
3378 3379 if (ipp == NULL) {
3379 3380 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3380 3381 ixa->ixa_cred = connp->conn_cred; /* Restore */
3381 3382 ixa->ixa_cpid = connp->conn_cpid;
3382 3383 ixa_refrele(ixa);
3383 3384 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3384 3385 freemsg(mp);
3385 3386 return (ENOMEM);
3386 3387 }
3387 3388 mutex_enter(&connp->conn_lock);
3388 3389 error = ip_pkt_copy(&connp->conn_xmit_ipp, ipp, KM_NOSLEEP);
3389 3390 mutex_exit(&connp->conn_lock);
3390 3391 if (error != 0) {
3391 3392 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3392 3393 freemsg(mp);
3393 3394 goto done;
3394 3395 }
3395 3396
3396 3397 /*
3397 3398 * Parse the options and update ixa and ipp as a result.
3398 3399 */
3399 3400
3400 3401 coa = &coas;
3401 3402 coa->coa_connp = connp;
3402 3403 coa->coa_ixa = ixa;
3403 3404 coa->coa_ipp = ipp;
3404 3405 coa->coa_ancillary = B_TRUE;
3405 3406 coa->coa_changed = 0;
3406 3407
3407 3408 if (msg != NULL) {
3408 3409 error = process_auxiliary_options(connp, msg->msg_control,
3409 3410 msg->msg_controllen, coa, &icmp_opt_obj, icmp_opt_set, cr);
3410 3411 } else {
3411 3412 struct T_unitdata_req *tudr;
3412 3413
3413 3414 tudr = (struct T_unitdata_req *)tudr_mp->b_rptr;
3414 3415 ASSERT(tudr->PRIM_type == T_UNITDATA_REQ);
3415 3416 error = tpi_optcom_buf(connp->conn_wq, tudr_mp,
3416 3417 &tudr->OPT_length, tudr->OPT_offset, cr, &icmp_opt_obj,
3417 3418 coa, &is_absreq_failure);
3418 3419 }
3419 3420 if (error != 0) {
3420 3421 /*
3421 3422 * Note: No special action needed in this
3422 3423 * module for "is_absreq_failure"
3423 3424 */
3424 3425 freemsg(mp);
3425 3426 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3426 3427 goto done;
3427 3428 }
3428 3429 ASSERT(is_absreq_failure == 0);
3429 3430
3430 3431 mutex_enter(&connp->conn_lock);
3431 3432 /*
3432 3433 * If laddr is unspecified then we look at sin6_src_id.
3433 3434 * We will give precedence to a source address set with IPV6_PKTINFO
3434 3435 * (aka IPPF_ADDR) but that is handled in build_hdrs. However, we don't
3435 3436 * want ip_attr_connect to select a source (since it can fail) when
3436 3437 * IPV6_PKTINFO is specified.
3437 3438 * If this doesn't result in a source address then we get a source
3438 3439 * from ip_attr_connect() below.
3439 3440 */
3440 3441 v6src = connp->conn_saddr_v6;
3441 3442 if (sin != NULL) {
3442 3443 IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &v6dst);
3443 3444 dstport = sin->sin_port;
3444 3445 flowinfo = 0;
3445 3446 ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
3446 3447 ixa->ixa_flags |= IXAF_IS_IPV4;
3447 3448 } else if (sin6 != NULL) {
3448 3449 boolean_t v4mapped;
3449 3450 uint_t srcid;
3450 3451
3451 3452 v6dst = sin6->sin6_addr;
3452 3453 dstport = sin6->sin6_port;
3453 3454 flowinfo = sin6->sin6_flowinfo;
3454 3455 srcid = sin6->__sin6_src_id;
3455 3456 if (IN6_IS_ADDR_LINKSCOPE(&v6dst) && sin6->sin6_scope_id != 0) {
3456 3457 ixa->ixa_scopeid = sin6->sin6_scope_id;
3457 3458 ixa->ixa_flags |= IXAF_SCOPEID_SET;
3458 3459 } else {
3459 3460 ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
3460 3461 }
3461 3462 v4mapped = IN6_IS_ADDR_V4MAPPED(&v6dst);
3462 3463 if (v4mapped)
3463 3464 ixa->ixa_flags |= IXAF_IS_IPV4;
3464 3465 else
3465 3466 ixa->ixa_flags &= ~IXAF_IS_IPV4;
3466 3467 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&v6src)) {
3467 3468 if (!ip_srcid_find_id(srcid, &v6src, IPCL_ZONEID(connp),
3468 3469 v4mapped, connp->conn_netstack)) {
3469 3470 /* Mismatched v4mapped/v6 specified by srcid. */
3470 3471 mutex_exit(&connp->conn_lock);
3471 3472 error = EADDRNOTAVAIL;
3472 3473 goto failed; /* Does freemsg() and mib. */
3473 3474 }
3474 3475 }
3475 3476 } else {
3476 3477 /* Connected case */
3477 3478 v6dst = connp->conn_faddr_v6;
3478 3479 flowinfo = connp->conn_flowinfo;
3479 3480 }
3480 3481 mutex_exit(&connp->conn_lock);
3481 3482 /* Handle IP_PKTINFO/IPV6_PKTINFO setting source address. */
3482 3483 if (ipp->ipp_fields & IPPF_ADDR) {
3483 3484 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3484 3485 if (IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
3485 3486 v6src = ipp->ipp_addr;
3486 3487 } else {
3487 3488 if (!IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
3488 3489 v6src = ipp->ipp_addr;
3489 3490 }
3490 3491 }
3491 3492 /*
3492 3493 * Allow source not assigned to the system
3493 3494 * only if it is not a local addresses
3494 3495 */
3495 3496 if (!V6_OR_V4_INADDR_ANY(v6src)) {
3496 3497 ip_laddr_t laddr_type;
3497 3498
3498 3499 if (ixa->ixa_flags & IXAF_IS_IPV4) {
3499 3500 ipaddr_t v4src;
3500 3501
3501 3502 IN6_V4MAPPED_TO_IPADDR(&v6src, v4src);
3502 3503 laddr_type = ip_laddr_verify_v4(v4src, ixa->ixa_zoneid,
3503 3504 is->is_netstack->netstack_ip, B_FALSE);
3504 3505 } else {
3505 3506 laddr_type = ip_laddr_verify_v6(&v6src, ixa->ixa_zoneid,
3506 3507 is->is_netstack->netstack_ip, B_FALSE, B_FALSE);
3507 3508 }
3508 3509 if (laddr_type != IPVL_UNICAST_UP)
3509 3510 ixa->ixa_flags &= ~IXAF_VERIFY_SOURCE;
3510 3511 }
3511 3512
3512 3513 ip_attr_nexthop(ipp, ixa, &v6dst, &v6nexthop);
3513 3514 error = ip_attr_connect(connp, ixa, &v6src, &v6dst, &v6nexthop, dstport,
3514 3515 &v6src, NULL, IPDF_ALLOW_MCBC | IPDF_VERIFY_DST);
3515 3516
3516 3517 switch (error) {
3517 3518 case 0:
3518 3519 break;
3519 3520 case EADDRNOTAVAIL:
3520 3521 /*
3521 3522 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3522 3523 * Don't have the application see that errno
3523 3524 */
3524 3525 error = ENETUNREACH;
3525 3526 goto failed;
3526 3527 case ENETDOWN:
3527 3528 /*
3528 3529 * Have !ipif_addr_ready address; drop packet silently
3529 3530 * until we can get applications to not send until we
3530 3531 * are ready.
3531 3532 */
3532 3533 error = 0;
3533 3534 goto failed;
3534 3535 case EHOSTUNREACH:
3535 3536 case ENETUNREACH:
3536 3537 if (ixa->ixa_ire != NULL) {
3537 3538 /*
3538 3539 * Let conn_ip_output/ire_send_noroute return
3539 3540 * the error and send any local ICMP error.
3540 3541 */
3541 3542 error = 0;
3542 3543 break;
3543 3544 }
3544 3545 /* FALLTHRU */
3545 3546 default:
3546 3547 failed:
3547 3548 freemsg(mp);
3548 3549 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3549 3550 goto done;
3550 3551 }
3551 3552
3552 3553 /*
3553 3554 * We might be going to a different destination than last time,
3554 3555 * thus check that TX allows the communication and compute any
3555 3556 * needed label.
3556 3557 *
3557 3558 * TSOL Note: We have an exclusive ipp and ixa for this thread so we
3558 3559 * don't have to worry about concurrent threads.
3559 3560 */
3560 3561 if (is_system_labeled()) {
3561 3562 /*
3562 3563 * Check whether Trusted Solaris policy allows communication
3563 3564 * with this host, and pretend that the destination is
3564 3565 * unreachable if not.
3565 3566 * Compute any needed label and place it in ipp_label_v4/v6.
3566 3567 *
3567 3568 * Later conn_build_hdr_template/conn_prepend_hdr takes
3568 3569 * ipp_label_v4/v6 to form the packet.
3569 3570 *
3570 3571 * Tsol note: We have ipp structure local to this thread so
3571 3572 * no locking is needed.
3572 3573 */
3573 3574 error = conn_update_label(connp, ixa, &v6dst, ipp);
3574 3575 if (error != 0) {
3575 3576 freemsg(mp);
3576 3577 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3577 3578 goto done;
3578 3579 }
3579 3580 }
3580 3581 mp = icmp_prepend_hdr(connp, ixa, ipp, &v6src, &v6dst, flowinfo, mp,
3581 3582 &error);
3582 3583 if (mp == NULL) {
3583 3584 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3584 3585 ASSERT(error != 0);
3585 3586 goto done;
3586 3587 }
3587 3588 if (ixa->ixa_pktlen > IP_MAXPACKET) {
3588 3589 error = EMSGSIZE;
3589 3590 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3590 3591 freemsg(mp);
3591 3592 goto done;
3592 3593 }
3593 3594
3594 3595 /* Policy might differ for different ICMP type/code */
3595 3596 mp = icmp_output_attach_policy(mp, connp, ixa);
3596 3597 if (mp == NULL) {
3597 3598 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3598 3599 error = EHOSTUNREACH; /* IPsec policy failure */
3599 3600 goto done;
3600 3601 }
3601 3602
3602 3603 /* We're done. Pass the packet to ip. */
3603 3604 BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
3604 3605
3605 3606 error = conn_ip_output(mp, ixa);
3606 3607 if (!connp->conn_unspec_src)
3607 3608 ixa->ixa_flags |= IXAF_VERIFY_SOURCE;
3608 3609 /* No rawipOutErrors if an error since IP increases its error counter */
3609 3610 switch (error) {
3610 3611 case 0:
3611 3612 break;
3612 3613 case EWOULDBLOCK:
3613 3614 (void) ixa_check_drain_insert(connp, ixa);
3614 3615 error = 0;
3615 3616 break;
3616 3617 case EADDRNOTAVAIL:
3617 3618 /*
3618 3619 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3619 3620 * Don't have the application see that errno
3620 3621 */
3621 3622 error = ENETUNREACH;
3622 3623 /* FALLTHRU */
3623 3624 default:
3624 3625 mutex_enter(&connp->conn_lock);
3625 3626 /*
3626 3627 * Clear the source and v6lastdst so we call ip_attr_connect
3627 3628 * for the next packet and try to pick a better source.
3628 3629 */
3629 3630 if (connp->conn_mcbc_bind)
3630 3631 connp->conn_saddr_v6 = ipv6_all_zeros;
3631 3632 else
3632 3633 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
3633 3634 connp->conn_v6lastdst = ipv6_all_zeros;
3634 3635 mutex_exit(&connp->conn_lock);
3635 3636 break;
3636 3637 }
3637 3638 done:
3638 3639 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3639 3640 ixa->ixa_cred = connp->conn_cred; /* Restore */
3640 3641 ixa->ixa_cpid = connp->conn_cpid;
3641 3642 ixa_refrele(ixa);
3642 3643 ip_pkt_free(ipp);
3643 3644 kmem_free(ipp, sizeof (*ipp));
3644 3645 return (error);
3645 3646 }
3646 3647
3647 3648 /*
3648 3649 * Handle sending an M_DATA for a connected socket.
3649 3650 * Handles both IPv4 and IPv6.
3650 3651 */
3651 3652 int
3652 3653 icmp_output_connected(conn_t *connp, mblk_t *mp, cred_t *cr, pid_t pid)
3653 3654 {
3654 3655 icmp_t *icmp = connp->conn_icmp;
3655 3656 icmp_stack_t *is = icmp->icmp_is;
3656 3657 int error;
3657 3658 ip_xmit_attr_t *ixa;
3658 3659 boolean_t do_ipsec;
3659 3660
3660 3661 /*
3661 3662 * If no other thread is using conn_ixa this just gets a reference to
3662 3663 * conn_ixa. Otherwise we get a safe copy of conn_ixa.
3663 3664 */
3664 3665 ixa = conn_get_ixa(connp, B_FALSE);
3665 3666 if (ixa == NULL) {
3666 3667 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3667 3668 freemsg(mp);
3668 3669 return (ENOMEM);
3669 3670 }
3670 3671
3671 3672 ASSERT(cr != NULL);
3672 3673 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3673 3674 ixa->ixa_cred = cr;
3674 3675 ixa->ixa_cpid = pid;
3675 3676
3676 3677 /* Defer IPsec if it might need to look at ICMP type/code */
3677 3678 switch (ixa->ixa_protocol) {
3678 3679 case IPPROTO_ICMP:
3679 3680 case IPPROTO_ICMPV6:
3680 3681 do_ipsec = B_FALSE;
3681 3682 break;
3682 3683 default:
3683 3684 do_ipsec = B_TRUE;
3684 3685 }
3685 3686
3686 3687 mutex_enter(&connp->conn_lock);
3687 3688 mp = icmp_prepend_header_template(connp, ixa, mp,
3688 3689 &connp->conn_saddr_v6, connp->conn_flowinfo, &error);
3689 3690
3690 3691 if (mp == NULL) {
3691 3692 ASSERT(error != 0);
3692 3693 mutex_exit(&connp->conn_lock);
3693 3694 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3694 3695 ixa->ixa_cred = connp->conn_cred; /* Restore */
3695 3696 ixa->ixa_cpid = connp->conn_cpid;
3696 3697 ixa_refrele(ixa);
3697 3698 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3698 3699 freemsg(mp);
3699 3700 return (error);
3700 3701 }
3701 3702
3702 3703 if (!do_ipsec) {
3703 3704 /* Policy might differ for different ICMP type/code */
3704 3705 mp = icmp_output_attach_policy(mp, connp, ixa);
3705 3706 if (mp == NULL) {
3706 3707 mutex_exit(&connp->conn_lock);
3707 3708 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3708 3709 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3709 3710 ixa->ixa_cred = connp->conn_cred; /* Restore */
3710 3711 ixa->ixa_cpid = connp->conn_cpid;
3711 3712 ixa_refrele(ixa);
3712 3713 return (EHOSTUNREACH); /* IPsec policy failure */
3713 3714 }
3714 3715 }
3715 3716
3716 3717 /*
3717 3718 * In case we got a safe copy of conn_ixa, or if opt_set made us a new
3718 3719 * safe copy, then we need to fill in any pointers in it.
3719 3720 */
3720 3721 if (ixa->ixa_ire == NULL) {
3721 3722 in6_addr_t faddr, saddr;
3722 3723 in6_addr_t nexthop;
3723 3724 in_port_t fport;
3724 3725
3725 3726 saddr = connp->conn_saddr_v6;
3726 3727 faddr = connp->conn_faddr_v6;
3727 3728 fport = connp->conn_fport;
3728 3729 ip_attr_nexthop(&connp->conn_xmit_ipp, ixa, &faddr, &nexthop);
3729 3730 mutex_exit(&connp->conn_lock);
3730 3731
3731 3732 error = ip_attr_connect(connp, ixa, &saddr, &faddr, &nexthop,
3732 3733 fport, NULL, NULL, IPDF_ALLOW_MCBC | IPDF_VERIFY_DST |
3733 3734 (do_ipsec ? IPDF_IPSEC : 0));
3734 3735 switch (error) {
3735 3736 case 0:
3736 3737 break;
3737 3738 case EADDRNOTAVAIL:
3738 3739 /*
3739 3740 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3740 3741 * Don't have the application see that errno
3741 3742 */
3742 3743 error = ENETUNREACH;
3743 3744 goto failed;
3744 3745 case ENETDOWN:
3745 3746 /*
3746 3747 * Have !ipif_addr_ready address; drop packet silently
3747 3748 * until we can get applications to not send until we
3748 3749 * are ready.
3749 3750 */
3750 3751 error = 0;
3751 3752 goto failed;
3752 3753 case EHOSTUNREACH:
3753 3754 case ENETUNREACH:
3754 3755 if (ixa->ixa_ire != NULL) {
3755 3756 /*
3756 3757 * Let conn_ip_output/ire_send_noroute return
3757 3758 * the error and send any local ICMP error.
3758 3759 */
3759 3760 error = 0;
3760 3761 break;
3761 3762 }
3762 3763 /* FALLTHRU */
3763 3764 default:
3764 3765 failed:
3765 3766 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3766 3767 ixa->ixa_cred = connp->conn_cred; /* Restore */
3767 3768 ixa->ixa_cpid = connp->conn_cpid;
3768 3769 ixa_refrele(ixa);
3769 3770 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3770 3771 freemsg(mp);
3771 3772 return (error);
3772 3773 }
3773 3774 } else {
3774 3775 /* Done with conn_t */
3775 3776 mutex_exit(&connp->conn_lock);
3776 3777 }
3777 3778
3778 3779 /* We're done. Pass the packet to ip. */
3779 3780 BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
3780 3781
3781 3782 error = conn_ip_output(mp, ixa);
3782 3783 /* No rawipOutErrors if an error since IP increases its error counter */
3783 3784 switch (error) {
3784 3785 case 0:
3785 3786 break;
3786 3787 case EWOULDBLOCK:
3787 3788 (void) ixa_check_drain_insert(connp, ixa);
3788 3789 error = 0;
3789 3790 break;
3790 3791 case EADDRNOTAVAIL:
3791 3792 /*
3792 3793 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3793 3794 * Don't have the application see that errno
3794 3795 */
3795 3796 error = ENETUNREACH;
3796 3797 break;
3797 3798 }
3798 3799 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3799 3800 ixa->ixa_cred = connp->conn_cred; /* Restore */
3800 3801 ixa->ixa_cpid = connp->conn_cpid;
3801 3802 ixa_refrele(ixa);
3802 3803 return (error);
3803 3804 }
3804 3805
3805 3806 /*
3806 3807 * Handle sending an M_DATA to the last destination.
3807 3808 * Handles both IPv4 and IPv6.
3808 3809 *
3809 3810 * NOTE: The caller must hold conn_lock and we drop it here.
3810 3811 */
3811 3812 int
3812 3813 icmp_output_lastdst(conn_t *connp, mblk_t *mp, cred_t *cr, pid_t pid,
3813 3814 ip_xmit_attr_t *ixa)
3814 3815 {
3815 3816 icmp_t *icmp = connp->conn_icmp;
3816 3817 icmp_stack_t *is = icmp->icmp_is;
3817 3818 int error;
3818 3819 boolean_t do_ipsec;
3819 3820
3820 3821 ASSERT(MUTEX_HELD(&connp->conn_lock));
3821 3822 ASSERT(ixa != NULL);
3822 3823
3823 3824 ASSERT(cr != NULL);
3824 3825 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3825 3826 ixa->ixa_cred = cr;
3826 3827 ixa->ixa_cpid = pid;
3827 3828
3828 3829 /* Defer IPsec if it might need to look at ICMP type/code */
3829 3830 switch (ixa->ixa_protocol) {
3830 3831 case IPPROTO_ICMP:
3831 3832 case IPPROTO_ICMPV6:
3832 3833 do_ipsec = B_FALSE;
3833 3834 break;
3834 3835 default:
3835 3836 do_ipsec = B_TRUE;
3836 3837 }
3837 3838
3838 3839
3839 3840 mp = icmp_prepend_header_template(connp, ixa, mp,
3840 3841 &connp->conn_v6lastsrc, connp->conn_lastflowinfo, &error);
3841 3842
3842 3843 if (mp == NULL) {
3843 3844 ASSERT(error != 0);
3844 3845 mutex_exit(&connp->conn_lock);
3845 3846 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3846 3847 ixa->ixa_cred = connp->conn_cred; /* Restore */
3847 3848 ixa->ixa_cpid = connp->conn_cpid;
3848 3849 ixa_refrele(ixa);
3849 3850 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3850 3851 freemsg(mp);
3851 3852 return (error);
3852 3853 }
3853 3854
3854 3855 if (!do_ipsec) {
3855 3856 /* Policy might differ for different ICMP type/code */
3856 3857 mp = icmp_output_attach_policy(mp, connp, ixa);
3857 3858 if (mp == NULL) {
3858 3859 mutex_exit(&connp->conn_lock);
3859 3860 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3860 3861 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3861 3862 ixa->ixa_cred = connp->conn_cred; /* Restore */
3862 3863 ixa->ixa_cpid = connp->conn_cpid;
3863 3864 ixa_refrele(ixa);
3864 3865 return (EHOSTUNREACH); /* IPsec policy failure */
3865 3866 }
3866 3867 }
3867 3868
3868 3869 /*
3869 3870 * In case we got a safe copy of conn_ixa, or if opt_set made us a new
3870 3871 * safe copy, then we need to fill in any pointers in it.
3871 3872 */
3872 3873 if (ixa->ixa_ire == NULL) {
3873 3874 in6_addr_t lastdst, lastsrc;
3874 3875 in6_addr_t nexthop;
3875 3876 in_port_t lastport;
3876 3877
3877 3878 lastsrc = connp->conn_v6lastsrc;
3878 3879 lastdst = connp->conn_v6lastdst;
3879 3880 lastport = connp->conn_lastdstport;
3880 3881 ip_attr_nexthop(&connp->conn_xmit_ipp, ixa, &lastdst, &nexthop);
3881 3882 mutex_exit(&connp->conn_lock);
3882 3883
3883 3884 error = ip_attr_connect(connp, ixa, &lastsrc, &lastdst,
3884 3885 &nexthop, lastport, NULL, NULL, IPDF_ALLOW_MCBC |
3885 3886 IPDF_VERIFY_DST | (do_ipsec ? IPDF_IPSEC : 0));
3886 3887 switch (error) {
3887 3888 case 0:
3888 3889 break;
3889 3890 case EADDRNOTAVAIL:
3890 3891 /*
3891 3892 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3892 3893 * Don't have the application see that errno
3893 3894 */
3894 3895 error = ENETUNREACH;
3895 3896 goto failed;
3896 3897 case ENETDOWN:
3897 3898 /*
3898 3899 * Have !ipif_addr_ready address; drop packet silently
3899 3900 * until we can get applications to not send until we
3900 3901 * are ready.
3901 3902 */
3902 3903 error = 0;
3903 3904 goto failed;
3904 3905 case EHOSTUNREACH:
3905 3906 case ENETUNREACH:
3906 3907 if (ixa->ixa_ire != NULL) {
3907 3908 /*
3908 3909 * Let conn_ip_output/ire_send_noroute return
3909 3910 * the error and send any local ICMP error.
3910 3911 */
3911 3912 error = 0;
3912 3913 break;
3913 3914 }
3914 3915 /* FALLTHRU */
3915 3916 default:
3916 3917 failed:
3917 3918 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3918 3919 ixa->ixa_cred = connp->conn_cred; /* Restore */
3919 3920 ixa->ixa_cpid = connp->conn_cpid;
3920 3921 ixa_refrele(ixa);
3921 3922 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
3922 3923 freemsg(mp);
3923 3924 return (error);
3924 3925 }
3925 3926 } else {
3926 3927 /* Done with conn_t */
3927 3928 mutex_exit(&connp->conn_lock);
3928 3929 }
3929 3930
3930 3931 /* We're done. Pass the packet to ip. */
3931 3932 BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
3932 3933 error = conn_ip_output(mp, ixa);
3933 3934 /* No rawipOutErrors if an error since IP increases its error counter */
3934 3935 switch (error) {
3935 3936 case 0:
3936 3937 break;
3937 3938 case EWOULDBLOCK:
3938 3939 (void) ixa_check_drain_insert(connp, ixa);
3939 3940 error = 0;
3940 3941 break;
3941 3942 case EADDRNOTAVAIL:
3942 3943 /*
3943 3944 * IXAF_VERIFY_SOURCE tells us to pick a better source.
3944 3945 * Don't have the application see that errno
3945 3946 */
3946 3947 error = ENETUNREACH;
3947 3948 /* FALLTHRU */
3948 3949 default:
3949 3950 mutex_enter(&connp->conn_lock);
3950 3951 /*
3951 3952 * Clear the source and v6lastdst so we call ip_attr_connect
3952 3953 * for the next packet and try to pick a better source.
3953 3954 */
3954 3955 if (connp->conn_mcbc_bind)
3955 3956 connp->conn_saddr_v6 = ipv6_all_zeros;
3956 3957 else
3957 3958 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
3958 3959 connp->conn_v6lastdst = ipv6_all_zeros;
3959 3960 mutex_exit(&connp->conn_lock);
3960 3961 break;
3961 3962 }
3962 3963 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
3963 3964 ixa->ixa_cred = connp->conn_cred; /* Restore */
3964 3965 ixa->ixa_cpid = connp->conn_cpid;
3965 3966 ixa_refrele(ixa);
3966 3967 return (error);
3967 3968 }
3968 3969
3969 3970
3970 3971 /*
3971 3972 * Prepend the header template and then fill in the source and
3972 3973 * flowinfo. The caller needs to handle the destination address since
3973 3974 * it's setting is different if rthdr or source route.
3974 3975 *
3975 3976 * Returns NULL is allocation failed or if the packet would exceed IP_MAXPACKET.
3976 3977 * When it returns NULL it sets errorp.
3977 3978 */
3978 3979 static mblk_t *
3979 3980 icmp_prepend_header_template(conn_t *connp, ip_xmit_attr_t *ixa, mblk_t *mp,
3980 3981 const in6_addr_t *v6src, uint32_t flowinfo, int *errorp)
3981 3982 {
3982 3983 icmp_t *icmp = connp->conn_icmp;
3983 3984 icmp_stack_t *is = icmp->icmp_is;
3984 3985 uint_t pktlen;
3985 3986 uint_t copylen;
3986 3987 uint8_t *iph;
3987 3988 uint_t ip_hdr_length;
3988 3989 uint32_t cksum;
3989 3990 ip_pkt_t *ipp;
3990 3991
3991 3992 ASSERT(MUTEX_HELD(&connp->conn_lock));
3992 3993
3993 3994 /*
3994 3995 * Copy the header template.
3995 3996 */
3996 3997 copylen = connp->conn_ht_iphc_len;
3997 3998 pktlen = copylen + msgdsize(mp);
3998 3999 if (pktlen > IP_MAXPACKET) {
3999 4000 freemsg(mp);
4000 4001 *errorp = EMSGSIZE;
4001 4002 return (NULL);
4002 4003 }
4003 4004 ixa->ixa_pktlen = pktlen;
4004 4005
4005 4006 /* check/fix buffer config, setup pointers into it */
4006 4007 iph = mp->b_rptr - copylen;
4007 4008 if (DB_REF(mp) != 1 || iph < DB_BASE(mp) || !OK_32PTR(iph)) {
4008 4009 mblk_t *mp1;
4009 4010
4010 4011 mp1 = allocb(copylen + is->is_wroff_extra, BPRI_MED);
4011 4012 if (mp1 == NULL) {
4012 4013 freemsg(mp);
4013 4014 *errorp = ENOMEM;
4014 4015 return (NULL);
4015 4016 }
4016 4017 mp1->b_wptr = DB_LIM(mp1);
4017 4018 mp1->b_cont = mp;
4018 4019 mp = mp1;
4019 4020 iph = (mp->b_wptr - copylen);
4020 4021 }
4021 4022 mp->b_rptr = iph;
4022 4023 bcopy(connp->conn_ht_iphc, iph, copylen);
4023 4024 ip_hdr_length = (uint_t)(connp->conn_ht_ulp - connp->conn_ht_iphc);
4024 4025
4025 4026 ixa->ixa_ip_hdr_length = ip_hdr_length;
4026 4027
4027 4028 /*
4028 4029 * Prepare for ICMPv6 checksum done in IP.
4029 4030 *
4030 4031 * icmp_build_hdr_template has already massaged any routing header
4031 4032 * and placed the result in conn_sum.
4032 4033 *
4033 4034 * We make it easy for IP to include our pseudo header
4034 4035 * by putting our length (and any routing header adjustment)
4035 4036 * in the ICMPv6 checksum field.
4036 4037 */
4037 4038 cksum = pktlen - ip_hdr_length;
4038 4039
4039 4040 cksum += connp->conn_sum;
4040 4041 cksum = (cksum >> 16) + (cksum & 0xFFFF);
4041 4042 ASSERT(cksum < 0x10000);
4042 4043
4043 4044 ipp = &connp->conn_xmit_ipp;
4044 4045 if (ixa->ixa_flags & IXAF_IS_IPV4) {
4045 4046 ipha_t *ipha = (ipha_t *)iph;
4046 4047
4047 4048 ipha->ipha_length = htons((uint16_t)pktlen);
4048 4049
4049 4050 /* if IP_PKTINFO specified an addres it wins over bind() */
4050 4051 if ((ipp->ipp_fields & IPPF_ADDR) &&
4051 4052 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) {
4052 4053 ASSERT(ipp->ipp_addr_v4 != INADDR_ANY);
4053 4054 ipha->ipha_src = ipp->ipp_addr_v4;
4054 4055 } else {
4055 4056 IN6_V4MAPPED_TO_IPADDR(v6src, ipha->ipha_src);
4056 4057 }
4057 4058 } else {
4058 4059 ip6_t *ip6h = (ip6_t *)iph;
4059 4060 uint_t cksum_offset = 0;
4060 4061
4061 4062 ip6h->ip6_plen = htons((uint16_t)(pktlen - IPV6_HDR_LEN));
4062 4063
4063 4064 /* if IP_PKTINFO specified an addres it wins over bind() */
4064 4065 if ((ipp->ipp_fields & IPPF_ADDR) &&
4065 4066 !IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr)) {
4066 4067 ASSERT(!IN6_IS_ADDR_UNSPECIFIED(&ipp->ipp_addr));
4067 4068 ip6h->ip6_src = ipp->ipp_addr;
4068 4069 } else {
4069 4070 ip6h->ip6_src = *v6src;
4070 4071 }
4071 4072 ip6h->ip6_vcf =
4072 4073 (IPV6_DEFAULT_VERS_AND_FLOW & IPV6_VERS_AND_FLOW_MASK) |
4073 4074 (flowinfo & ~IPV6_VERS_AND_FLOW_MASK);
4074 4075 if (ipp->ipp_fields & IPPF_TCLASS) {
4075 4076 /* Overrides the class part of flowinfo */
4076 4077 ip6h->ip6_vcf = IPV6_TCLASS_FLOW(ip6h->ip6_vcf,
4077 4078 ipp->ipp_tclass);
4078 4079 }
4079 4080
4080 4081 if (ixa->ixa_flags & IXAF_SET_ULP_CKSUM) {
4081 4082 if (connp->conn_proto == IPPROTO_ICMPV6) {
4082 4083 cksum_offset = ixa->ixa_ip_hdr_length +
4083 4084 offsetof(icmp6_t, icmp6_cksum);
4084 4085 } else if (ixa->ixa_flags & IXAF_SET_RAW_CKSUM) {
4085 4086 cksum_offset = ixa->ixa_ip_hdr_length +
4086 4087 ixa->ixa_raw_cksum_offset;
4087 4088 }
4088 4089 }
4089 4090 if (cksum_offset != 0) {
4090 4091 uint16_t *ptr;
4091 4092
4092 4093 /* Make sure the checksum fits in the first mblk */
4093 4094 if (cksum_offset + sizeof (short) > MBLKL(mp)) {
4094 4095 mblk_t *mp1;
4095 4096
4096 4097 mp1 = msgpullup(mp,
4097 4098 cksum_offset + sizeof (short));
4098 4099 freemsg(mp);
4099 4100 if (mp1 == NULL) {
4100 4101 *errorp = ENOMEM;
4101 4102 return (NULL);
4102 4103 }
4103 4104 mp = mp1;
4104 4105 iph = mp->b_rptr;
4105 4106 ip6h = (ip6_t *)iph;
4106 4107 }
4107 4108 ptr = (uint16_t *)(mp->b_rptr + cksum_offset);
4108 4109 *ptr = htons(cksum);
4109 4110 }
4110 4111 }
4111 4112
4112 4113 return (mp);
4113 4114 }
4114 4115
4115 4116 /*
4116 4117 * This routine handles all messages passed downstream. It either
4117 4118 * consumes the message or passes it downstream; it never queues a
4118 4119 * a message.
4119 4120 */
4120 4121 int
4121 4122 icmp_wput(queue_t *q, mblk_t *mp)
4122 4123 {
4123 4124 sin6_t *sin6;
4124 4125 sin_t *sin = NULL;
4125 4126 uint_t srcid;
4126 4127 conn_t *connp = Q_TO_CONN(q);
4127 4128 icmp_t *icmp = connp->conn_icmp;
4128 4129 int error = 0;
4129 4130 struct sockaddr *addr = NULL;
4130 4131 socklen_t addrlen;
4131 4132 icmp_stack_t *is = icmp->icmp_is;
4132 4133 struct T_unitdata_req *tudr;
4133 4134 mblk_t *data_mp;
4134 4135 cred_t *cr;
4135 4136 pid_t pid;
4136 4137
4137 4138 /*
4138 4139 * We directly handle several cases here: T_UNITDATA_REQ message
4139 4140 * coming down as M_PROTO/M_PCPROTO and M_DATA messages for connected
4140 4141 * socket.
4141 4142 */
4142 4143 switch (DB_TYPE(mp)) {
4143 4144 case M_DATA:
4144 4145 /* sockfs never sends down M_DATA */
4145 4146 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
4146 4147 freemsg(mp);
4147 4148 return (0);
4148 4149
4149 4150 case M_PROTO:
4150 4151 case M_PCPROTO:
4151 4152 tudr = (struct T_unitdata_req *)mp->b_rptr;
4152 4153 if (MBLKL(mp) < sizeof (*tudr) ||
4153 4154 ((t_primp_t)mp->b_rptr)->type != T_UNITDATA_REQ) {
4154 4155 icmp_wput_other(q, mp);
4155 4156 return (0);
4156 4157 }
4157 4158 break;
4158 4159
4159 4160 default:
4160 4161 icmp_wput_other(q, mp);
4161 4162 return (0);
4162 4163 }
4163 4164
4164 4165 /* Handle valid T_UNITDATA_REQ here */
4165 4166 data_mp = mp->b_cont;
4166 4167 if (data_mp == NULL) {
4167 4168 error = EPROTO;
4168 4169 goto ud_error2;
4169 4170 }
4170 4171 mp->b_cont = NULL;
4171 4172
4172 4173 if (!MBLKIN(mp, 0, tudr->DEST_offset + tudr->DEST_length)) {
4173 4174 error = EADDRNOTAVAIL;
4174 4175 goto ud_error2;
4175 4176 }
4176 4177
4177 4178 /*
4178 4179 * All Solaris components should pass a db_credp
4179 4180 * for this message, hence we ASSERT.
4180 4181 * On production kernels we return an error to be robust against
4181 4182 * random streams modules sitting on top of us.
4182 4183 */
4183 4184 cr = msg_getcred(mp, &pid);
4184 4185 ASSERT(cr != NULL);
4185 4186 if (cr == NULL) {
4186 4187 error = EINVAL;
4187 4188 goto ud_error2;
4188 4189 }
4189 4190
4190 4191 /*
4191 4192 * If a port has not been bound to the stream, fail.
4192 4193 * This is not a problem when sockfs is directly
4193 4194 * above us, because it will ensure that the socket
4194 4195 * is first bound before allowing data to be sent.
4195 4196 */
4196 4197 if (icmp->icmp_state == TS_UNBND) {
4197 4198 error = EPROTO;
4198 4199 goto ud_error2;
4199 4200 }
4200 4201 addr = (struct sockaddr *)&mp->b_rptr[tudr->DEST_offset];
4201 4202 addrlen = tudr->DEST_length;
4202 4203
4203 4204 switch (connp->conn_family) {
4204 4205 case AF_INET6:
4205 4206 sin6 = (sin6_t *)addr;
4206 4207 if (!OK_32PTR((char *)sin6) || (addrlen != sizeof (sin6_t)) ||
4207 4208 (sin6->sin6_family != AF_INET6)) {
4208 4209 error = EADDRNOTAVAIL;
4209 4210 goto ud_error2;
4210 4211 }
4211 4212
4212 4213 /* No support for mapped addresses on raw sockets */
4213 4214 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
4214 4215 error = EADDRNOTAVAIL;
4215 4216 goto ud_error2;
4216 4217 }
4217 4218 srcid = sin6->__sin6_src_id;
4218 4219
4219 4220 /*
4220 4221 * If the local address is a mapped address return
4221 4222 * an error.
4222 4223 * It would be possible to send an IPv6 packet but the
4223 4224 * response would never make it back to the application
4224 4225 * since it is bound to a mapped address.
4225 4226 */
4226 4227 if (IN6_IS_ADDR_V4MAPPED(&connp->conn_saddr_v6)) {
4227 4228 error = EADDRNOTAVAIL;
4228 4229 goto ud_error2;
4229 4230 }
4230 4231
4231 4232 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
4232 4233 sin6->sin6_addr = ipv6_loopback;
4233 4234
4234 4235 if (tudr->OPT_length != 0) {
4235 4236 /*
4236 4237 * If we are connected then the destination needs to be
4237 4238 * the same as the connected one.
4238 4239 */
4239 4240 if (icmp->icmp_state == TS_DATA_XFER &&
4240 4241 !conn_same_as_last_v6(connp, sin6)) {
4241 4242 error = EISCONN;
4242 4243 goto ud_error2;
4243 4244 }
4244 4245 error = icmp_output_ancillary(connp, NULL, sin6,
4245 4246 data_mp, mp, NULL, cr, pid);
4246 4247 } else {
4247 4248 ip_xmit_attr_t *ixa;
4248 4249
4249 4250 /*
4250 4251 * We have to allocate an ip_xmit_attr_t before we grab
4251 4252 * conn_lock and we need to hold conn_lock once we've
4252 4253 * checked conn_same_as_last_v6 to handle concurrent
4253 4254 * send* calls on a socket.
4254 4255 */
4255 4256 ixa = conn_get_ixa(connp, B_FALSE);
4256 4257 if (ixa == NULL) {
4257 4258 error = ENOMEM;
4258 4259 goto ud_error2;
4259 4260 }
4260 4261 mutex_enter(&connp->conn_lock);
4261 4262
4262 4263 if (conn_same_as_last_v6(connp, sin6) &&
4263 4264 connp->conn_lastsrcid == srcid &&
4264 4265 ipsec_outbound_policy_current(ixa)) {
4265 4266 /* icmp_output_lastdst drops conn_lock */
4266 4267 error = icmp_output_lastdst(connp, data_mp, cr,
4267 4268 pid, ixa);
4268 4269 } else {
4269 4270 /* icmp_output_newdst drops conn_lock */
4270 4271 error = icmp_output_newdst(connp, data_mp, NULL,
4271 4272 sin6, cr, pid, ixa);
4272 4273 }
4273 4274 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock));
4274 4275 }
4275 4276 if (error == 0) {
4276 4277 freeb(mp);
4277 4278 return (0);
4278 4279 }
4279 4280 break;
4280 4281
4281 4282 case AF_INET:
4282 4283 sin = (sin_t *)addr;
4283 4284 if ((!OK_32PTR((char *)sin) || addrlen != sizeof (sin_t)) ||
4284 4285 (sin->sin_family != AF_INET)) {
4285 4286 error = EADDRNOTAVAIL;
4286 4287 goto ud_error2;
4287 4288 }
4288 4289 if (sin->sin_addr.s_addr == INADDR_ANY)
4289 4290 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
4290 4291
4291 4292 /* Protocol 255 contains full IP headers */
4292 4293 /* Read without holding lock */
4293 4294 if (icmp->icmp_hdrincl) {
4294 4295 if (MBLKL(data_mp) < IP_SIMPLE_HDR_LENGTH) {
4295 4296 if (!pullupmsg(data_mp, IP_SIMPLE_HDR_LENGTH)) {
4296 4297 error = EINVAL;
4297 4298 goto ud_error2;
4298 4299 }
4299 4300 }
4300 4301 error = icmp_output_hdrincl(connp, data_mp, cr, pid);
4301 4302 if (error == 0) {
4302 4303 freeb(mp);
4303 4304 return (0);
4304 4305 }
4305 4306 /* data_mp consumed above */
4306 4307 data_mp = NULL;
4307 4308 goto ud_error2;
4308 4309 }
4309 4310
4310 4311 if (tudr->OPT_length != 0) {
4311 4312 /*
4312 4313 * If we are connected then the destination needs to be
4313 4314 * the same as the connected one.
4314 4315 */
4315 4316 if (icmp->icmp_state == TS_DATA_XFER &&
4316 4317 !conn_same_as_last_v4(connp, sin)) {
4317 4318 error = EISCONN;
4318 4319 goto ud_error2;
4319 4320 }
4320 4321 error = icmp_output_ancillary(connp, sin, NULL,
4321 4322 data_mp, mp, NULL, cr, pid);
4322 4323 } else {
4323 4324 ip_xmit_attr_t *ixa;
4324 4325
4325 4326 /*
4326 4327 * We have to allocate an ip_xmit_attr_t before we grab
4327 4328 * conn_lock and we need to hold conn_lock once we've
4328 4329 * checked conn_same_as_last_v4 to handle concurrent
4329 4330 * send* calls on a socket.
4330 4331 */
4331 4332 ixa = conn_get_ixa(connp, B_FALSE);
4332 4333 if (ixa == NULL) {
4333 4334 error = ENOMEM;
4334 4335 goto ud_error2;
4335 4336 }
4336 4337 mutex_enter(&connp->conn_lock);
4337 4338
4338 4339 if (conn_same_as_last_v4(connp, sin) &&
4339 4340 ipsec_outbound_policy_current(ixa)) {
4340 4341 /* icmp_output_lastdst drops conn_lock */
4341 4342 error = icmp_output_lastdst(connp, data_mp, cr,
4342 4343 pid, ixa);
4343 4344 } else {
4344 4345 /* icmp_output_newdst drops conn_lock */
4345 4346 error = icmp_output_newdst(connp, data_mp, sin,
4346 4347 NULL, cr, pid, ixa);
4347 4348 }
4348 4349 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock));
4349 4350 }
4350 4351 if (error == 0) {
4351 4352 freeb(mp);
4352 4353 return (0);
4353 4354 }
4354 4355 break;
4355 4356 }
4356 4357 ASSERT(mp != NULL);
4357 4358 /* mp is freed by the following routine */
4358 4359 icmp_ud_err(q, mp, (t_scalar_t)error);
4359 4360 return (0);
4360 4361
4361 4362 ud_error2:
4362 4363 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
4363 4364 freemsg(data_mp);
4364 4365 ASSERT(mp != NULL);
4365 4366 /* mp is freed by the following routine */
4366 4367 icmp_ud_err(q, mp, (t_scalar_t)error);
4367 4368 return (0);
4368 4369 }
4369 4370
4370 4371 /*
4371 4372 * Handle the case of the IP address or flow label being different
4372 4373 * for both IPv4 and IPv6.
4373 4374 *
4374 4375 * NOTE: The caller must hold conn_lock and we drop it here.
4375 4376 */
4376 4377 static int
4377 4378 icmp_output_newdst(conn_t *connp, mblk_t *data_mp, sin_t *sin, sin6_t *sin6,
4378 4379 cred_t *cr, pid_t pid, ip_xmit_attr_t *ixa)
4379 4380 {
4380 4381 icmp_t *icmp = connp->conn_icmp;
4381 4382 icmp_stack_t *is = icmp->icmp_is;
4382 4383 int error;
4383 4384 ip_xmit_attr_t *oldixa;
4384 4385 boolean_t do_ipsec;
4385 4386 uint_t srcid;
4386 4387 uint32_t flowinfo;
4387 4388 in6_addr_t v6src;
4388 4389 in6_addr_t v6dst;
4389 4390 in6_addr_t v6nexthop;
4390 4391 in_port_t dstport;
4391 4392
4392 4393 ASSERT(MUTEX_HELD(&connp->conn_lock));
4393 4394 ASSERT(ixa != NULL);
4394 4395
4395 4396 /*
4396 4397 * We hold conn_lock across all the use and modifications of
4397 4398 * the conn_lastdst, conn_ixa, and conn_xmit_ipp to ensure that they
4398 4399 * stay consistent.
4399 4400 */
4400 4401
4401 4402 ASSERT(cr != NULL);
4402 4403 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
4403 4404 ixa->ixa_cred = cr;
4404 4405 ixa->ixa_cpid = pid;
4405 4406 if (is_system_labeled()) {
4406 4407 /* We need to restart with a label based on the cred */
4407 4408 ip_xmit_attr_restore_tsl(ixa, ixa->ixa_cred);
4408 4409 }
4409 4410 /*
4410 4411 * If we are connected then the destination needs to be the
4411 4412 * same as the connected one, which is not the case here since we
4412 4413 * checked for that above.
4413 4414 */
4414 4415 if (icmp->icmp_state == TS_DATA_XFER) {
4415 4416 mutex_exit(&connp->conn_lock);
4416 4417 error = EISCONN;
4417 4418 goto ud_error;
4418 4419 }
4419 4420
4420 4421 /* In case previous destination was multicast or multirt */
4421 4422 ip_attr_newdst(ixa);
4422 4423
4423 4424 /*
4424 4425 * If laddr is unspecified then we look at sin6_src_id.
4425 4426 * We will give precedence to a source address set with IPV6_PKTINFO
4426 4427 * (aka IPPF_ADDR) but that is handled in build_hdrs. However, we don't
4427 4428 * want ip_attr_connect to select a source (since it can fail) when
4428 4429 * IPV6_PKTINFO is specified.
4429 4430 * If this doesn't result in a source address then we get a source
4430 4431 * from ip_attr_connect() below.
4431 4432 */
4432 4433 v6src = connp->conn_saddr_v6;
4433 4434 if (sin != NULL) {
4434 4435 IN6_IPADDR_TO_V4MAPPED(sin->sin_addr.s_addr, &v6dst);
4435 4436 dstport = sin->sin_port;
4436 4437 flowinfo = 0;
4437 4438 /* Don't bother with ip_srcid_find_id(), but indicate anyway. */
4438 4439 srcid = 0;
4439 4440 ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
4440 4441 ixa->ixa_flags |= IXAF_IS_IPV4;
4441 4442 } else {
4442 4443 boolean_t v4mapped;
4443 4444
4444 4445 v6dst = sin6->sin6_addr;
4445 4446 dstport = sin6->sin6_port;
4446 4447 flowinfo = sin6->sin6_flowinfo;
4447 4448 srcid = sin6->__sin6_src_id;
4448 4449 if (IN6_IS_ADDR_LINKSCOPE(&v6dst) && sin6->sin6_scope_id != 0) {
4449 4450 ixa->ixa_scopeid = sin6->sin6_scope_id;
4450 4451 ixa->ixa_flags |= IXAF_SCOPEID_SET;
4451 4452 } else {
4452 4453 ixa->ixa_flags &= ~IXAF_SCOPEID_SET;
4453 4454 }
4454 4455 v4mapped = IN6_IS_ADDR_V4MAPPED(&v6dst);
4455 4456 if (v4mapped)
4456 4457 ixa->ixa_flags |= IXAF_IS_IPV4;
4457 4458 else
4458 4459 ixa->ixa_flags &= ~IXAF_IS_IPV4;
4459 4460 if (srcid != 0 && IN6_IS_ADDR_UNSPECIFIED(&v6src)) {
4460 4461 if (!ip_srcid_find_id(srcid, &v6src, IPCL_ZONEID(connp),
4461 4462 v4mapped, connp->conn_netstack)) {
4462 4463 /* Mismatched v4mapped/v6 specified by srcid. */
4463 4464 mutex_exit(&connp->conn_lock);
4464 4465 error = EADDRNOTAVAIL;
4465 4466 goto ud_error;
4466 4467 }
4467 4468 }
4468 4469 }
4469 4470 /* Handle IP_PKTINFO/IPV6_PKTINFO setting source address. */
4470 4471 if (connp->conn_xmit_ipp.ipp_fields & IPPF_ADDR) {
4471 4472 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
4472 4473
4473 4474 if (ixa->ixa_flags & IXAF_IS_IPV4) {
4474 4475 if (IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
4475 4476 v6src = ipp->ipp_addr;
4476 4477 } else {
4477 4478 if (!IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
4478 4479 v6src = ipp->ipp_addr;
4479 4480 }
4480 4481 }
4481 4482
4482 4483 /* Defer IPsec if it might need to look at ICMP type/code */
4483 4484 switch (ixa->ixa_protocol) {
4484 4485 case IPPROTO_ICMP:
4485 4486 case IPPROTO_ICMPV6:
4486 4487 do_ipsec = B_FALSE;
4487 4488 break;
4488 4489 default:
4489 4490 do_ipsec = B_TRUE;
4490 4491 }
4491 4492
4492 4493 ip_attr_nexthop(&connp->conn_xmit_ipp, ixa, &v6dst, &v6nexthop);
4493 4494 mutex_exit(&connp->conn_lock);
4494 4495
4495 4496 error = ip_attr_connect(connp, ixa, &v6src, &v6dst, &v6nexthop, dstport,
4496 4497 &v6src, NULL, IPDF_ALLOW_MCBC | IPDF_VERIFY_DST |
4497 4498 (do_ipsec ? IPDF_IPSEC : 0));
4498 4499 switch (error) {
4499 4500 case 0:
4500 4501 break;
4501 4502 case EADDRNOTAVAIL:
4502 4503 /*
4503 4504 * IXAF_VERIFY_SOURCE tells us to pick a better source.
4504 4505 * Don't have the application see that errno
4505 4506 */
4506 4507 error = ENETUNREACH;
4507 4508 goto failed;
4508 4509 case ENETDOWN:
4509 4510 /*
4510 4511 * Have !ipif_addr_ready address; drop packet silently
4511 4512 * until we can get applications to not send until we
4512 4513 * are ready.
4513 4514 */
4514 4515 error = 0;
4515 4516 goto failed;
4516 4517 case EHOSTUNREACH:
4517 4518 case ENETUNREACH:
4518 4519 if (ixa->ixa_ire != NULL) {
4519 4520 /*
4520 4521 * Let conn_ip_output/ire_send_noroute return
4521 4522 * the error and send any local ICMP error.
4522 4523 */
4523 4524 error = 0;
4524 4525 break;
4525 4526 }
4526 4527 /* FALLTHRU */
4527 4528 default:
4528 4529 failed:
4529 4530 goto ud_error;
4530 4531 }
4531 4532
4532 4533 mutex_enter(&connp->conn_lock);
4533 4534 /*
4534 4535 * While we dropped the lock some other thread might have connected
4535 4536 * this socket. If so we bail out with EISCONN to ensure that the
4536 4537 * connecting thread is the one that updates conn_ixa, conn_ht_*
4537 4538 * and conn_*last*.
4538 4539 */
4539 4540 if (icmp->icmp_state == TS_DATA_XFER) {
4540 4541 mutex_exit(&connp->conn_lock);
4541 4542 error = EISCONN;
4542 4543 goto ud_error;
4543 4544 }
4544 4545
4545 4546 /*
4546 4547 * We need to rebuild the headers if
4547 4548 * - we are labeling packets (could be different for different
4548 4549 * destinations)
4549 4550 * - we have a source route (or routing header) since we need to
4550 4551 * massage that to get the pseudo-header checksum
4551 4552 * - a socket option with COA_HEADER_CHANGED has been set which
4552 4553 * set conn_v6lastdst to zero.
4553 4554 *
4554 4555 * Otherwise the prepend function will just update the src, dst,
4555 4556 * and flow label.
4556 4557 */
4557 4558 if (is_system_labeled()) {
4558 4559 /* TX MLP requires SCM_UCRED and don't have that here */
4559 4560 if (connp->conn_mlp_type != mlptSingle) {
4560 4561 mutex_exit(&connp->conn_lock);
4561 4562 error = ECONNREFUSED;
4562 4563 goto ud_error;
4563 4564 }
4564 4565 /*
4565 4566 * Check whether Trusted Solaris policy allows communication
4566 4567 * with this host, and pretend that the destination is
4567 4568 * unreachable if not.
4568 4569 * Compute any needed label and place it in ipp_label_v4/v6.
4569 4570 *
4570 4571 * Later conn_build_hdr_template/conn_prepend_hdr takes
4571 4572 * ipp_label_v4/v6 to form the packet.
4572 4573 *
4573 4574 * Tsol note: Since we hold conn_lock we know no other
4574 4575 * thread manipulates conn_xmit_ipp.
4575 4576 */
4576 4577 error = conn_update_label(connp, ixa, &v6dst,
4577 4578 &connp->conn_xmit_ipp);
4578 4579 if (error != 0) {
4579 4580 mutex_exit(&connp->conn_lock);
4580 4581 goto ud_error;
4581 4582 }
4582 4583 /* Rebuild the header template */
4583 4584 error = icmp_build_hdr_template(connp, &v6src, &v6dst,
4584 4585 flowinfo);
4585 4586 if (error != 0) {
4586 4587 mutex_exit(&connp->conn_lock);
4587 4588 goto ud_error;
4588 4589 }
4589 4590 } else if (connp->conn_xmit_ipp.ipp_fields &
4590 4591 (IPPF_IPV4_OPTIONS|IPPF_RTHDR) ||
4591 4592 IN6_IS_ADDR_UNSPECIFIED(&connp->conn_v6lastdst)) {
4592 4593 /* Rebuild the header template */
4593 4594 error = icmp_build_hdr_template(connp, &v6src, &v6dst,
4594 4595 flowinfo);
4595 4596 if (error != 0) {
4596 4597 mutex_exit(&connp->conn_lock);
4597 4598 goto ud_error;
4598 4599 }
4599 4600 } else {
4600 4601 /* Simply update the destination address if no source route */
4601 4602 if (ixa->ixa_flags & IXAF_IS_IPV4) {
4602 4603 ipha_t *ipha = (ipha_t *)connp->conn_ht_iphc;
4603 4604
4604 4605 IN6_V4MAPPED_TO_IPADDR(&v6dst, ipha->ipha_dst);
4605 4606 if (ixa->ixa_flags & IXAF_PMTU_IPV4_DF) {
4606 4607 ipha->ipha_fragment_offset_and_flags |=
4607 4608 IPH_DF_HTONS;
4608 4609 } else {
4609 4610 ipha->ipha_fragment_offset_and_flags &=
4610 4611 ~IPH_DF_HTONS;
4611 4612 }
4612 4613 } else {
4613 4614 ip6_t *ip6h = (ip6_t *)connp->conn_ht_iphc;
4614 4615 ip6h->ip6_dst = v6dst;
4615 4616 }
4616 4617 }
4617 4618
4618 4619 /*
4619 4620 * Remember the dst etc which corresponds to the built header
4620 4621 * template and conn_ixa.
4621 4622 */
4622 4623 oldixa = conn_replace_ixa(connp, ixa);
4623 4624 connp->conn_v6lastdst = v6dst;
4624 4625 connp->conn_lastflowinfo = flowinfo;
4625 4626 connp->conn_lastscopeid = ixa->ixa_scopeid;
4626 4627 connp->conn_lastsrcid = srcid;
4627 4628 /* Also remember a source to use together with lastdst */
4628 4629 connp->conn_v6lastsrc = v6src;
4629 4630
4630 4631 data_mp = icmp_prepend_header_template(connp, ixa, data_mp, &v6src,
4631 4632 flowinfo, &error);
4632 4633
4633 4634 /* Done with conn_t */
4634 4635 mutex_exit(&connp->conn_lock);
4635 4636 ixa_refrele(oldixa);
4636 4637
4637 4638 if (data_mp == NULL) {
4638 4639 ASSERT(error != 0);
4639 4640 goto ud_error;
4640 4641 }
4641 4642
4642 4643 if (!do_ipsec) {
4643 4644 /* Policy might differ for different ICMP type/code */
4644 4645 data_mp = icmp_output_attach_policy(data_mp, connp, ixa);
4645 4646 if (data_mp == NULL) {
4646 4647 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
4647 4648 error = EHOSTUNREACH; /* IPsec policy failure */
4648 4649 goto done;
4649 4650 }
4650 4651 }
4651 4652
4652 4653 /* We're done. Pass the packet to ip. */
4653 4654 BUMP_MIB(&is->is_rawip_mib, rawipOutDatagrams);
4654 4655
4655 4656 error = conn_ip_output(data_mp, ixa);
4656 4657 /* No rawipOutErrors if an error since IP increases its error counter */
4657 4658 switch (error) {
4658 4659 case 0:
4659 4660 break;
4660 4661 case EWOULDBLOCK:
4661 4662 (void) ixa_check_drain_insert(connp, ixa);
4662 4663 error = 0;
4663 4664 break;
4664 4665 case EADDRNOTAVAIL:
4665 4666 /*
4666 4667 * IXAF_VERIFY_SOURCE tells us to pick a better source.
4667 4668 * Don't have the application see that errno
4668 4669 */
4669 4670 error = ENETUNREACH;
4670 4671 /* FALLTHRU */
4671 4672 default:
4672 4673 mutex_enter(&connp->conn_lock);
4673 4674 /*
4674 4675 * Clear the source and v6lastdst so we call ip_attr_connect
4675 4676 * for the next packet and try to pick a better source.
4676 4677 */
4677 4678 if (connp->conn_mcbc_bind)
4678 4679 connp->conn_saddr_v6 = ipv6_all_zeros;
4679 4680 else
4680 4681 connp->conn_saddr_v6 = connp->conn_bound_addr_v6;
4681 4682 connp->conn_v6lastdst = ipv6_all_zeros;
4682 4683 mutex_exit(&connp->conn_lock);
4683 4684 break;
4684 4685 }
4685 4686 done:
4686 4687 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
4687 4688 ixa->ixa_cred = connp->conn_cred; /* Restore */
4688 4689 ixa->ixa_cpid = connp->conn_cpid;
4689 4690 ixa_refrele(ixa);
4690 4691 return (error);
4691 4692
4692 4693 ud_error:
4693 4694 ASSERT(!(ixa->ixa_free_flags & IXA_FREE_CRED));
4694 4695 ixa->ixa_cred = connp->conn_cred; /* Restore */
4695 4696 ixa->ixa_cpid = connp->conn_cpid;
4696 4697 ixa_refrele(ixa);
4697 4698
4698 4699 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
4699 4700 freemsg(data_mp);
4700 4701 return (error);
4701 4702 }
4702 4703
4703 4704 /* ARGSUSED */
4704 4705 static int
4705 4706 icmp_wput_fallback(queue_t *q, mblk_t *mp)
4706 4707 {
4707 4708 #ifdef DEBUG
4708 4709 cmn_err(CE_CONT, "icmp_wput_fallback: Message during fallback \n");
4709 4710 #endif
4710 4711 freemsg(mp);
4711 4712 return (0);
4712 4713 }
4713 4714
4714 4715 static void
4715 4716 icmp_wput_other(queue_t *q, mblk_t *mp)
4716 4717 {
4717 4718 uchar_t *rptr = mp->b_rptr;
4718 4719 struct iocblk *iocp;
4719 4720 conn_t *connp = Q_TO_CONN(q);
4720 4721 icmp_t *icmp = connp->conn_icmp;
4721 4722 cred_t *cr;
4722 4723
4723 4724 switch (mp->b_datap->db_type) {
4724 4725 case M_PROTO:
4725 4726 case M_PCPROTO:
4726 4727 if (mp->b_wptr - rptr < sizeof (t_scalar_t)) {
4727 4728 /*
4728 4729 * If the message does not contain a PRIM_type,
4729 4730 * throw it away.
4730 4731 */
4731 4732 freemsg(mp);
4732 4733 return;
4733 4734 }
4734 4735 switch (((t_primp_t)rptr)->type) {
4735 4736 case T_ADDR_REQ:
4736 4737 icmp_addr_req(q, mp);
4737 4738 return;
4738 4739 case O_T_BIND_REQ:
4739 4740 case T_BIND_REQ:
4740 4741 icmp_tpi_bind(q, mp);
4741 4742 return;
4742 4743 case T_CONN_REQ:
4743 4744 icmp_tpi_connect(q, mp);
4744 4745 return;
4745 4746 case T_CAPABILITY_REQ:
4746 4747 icmp_capability_req(q, mp);
4747 4748 return;
4748 4749 case T_INFO_REQ:
4749 4750 icmp_info_req(q, mp);
4750 4751 return;
4751 4752 case T_UNITDATA_REQ:
4752 4753 /*
4753 4754 * If a T_UNITDATA_REQ gets here, the address must
4754 4755 * be bad. Valid T_UNITDATA_REQs are handled
4755 4756 * in icmp_wput.
4756 4757 */
4757 4758 icmp_ud_err(q, mp, EADDRNOTAVAIL);
4758 4759 return;
4759 4760 case T_UNBIND_REQ:
4760 4761 icmp_tpi_unbind(q, mp);
4761 4762 return;
4762 4763 case T_SVR4_OPTMGMT_REQ:
4763 4764 /*
4764 4765 * All Solaris components should pass a db_credp
4765 4766 * for this TPI message, hence we ASSERT.
4766 4767 * But in case there is some other M_PROTO that looks
4767 4768 * like a TPI message sent by some other kernel
4768 4769 * component, we check and return an error.
4769 4770 */
4770 4771 cr = msg_getcred(mp, NULL);
4771 4772 ASSERT(cr != NULL);
4772 4773 if (cr == NULL) {
4773 4774 icmp_err_ack(q, mp, TSYSERR, EINVAL);
4774 4775 return;
4775 4776 }
4776 4777
4777 4778 if (!snmpcom_req(q, mp, icmp_snmp_set, ip_snmp_get,
4778 4779 cr)) {
4779 4780 svr4_optcom_req(q, mp, cr, &icmp_opt_obj);
4780 4781 }
4781 4782 return;
4782 4783
4783 4784 case T_OPTMGMT_REQ:
4784 4785 /*
4785 4786 * All Solaris components should pass a db_credp
4786 4787 * for this TPI message, hence we ASSERT.
4787 4788 * But in case there is some other M_PROTO that looks
4788 4789 * like a TPI message sent by some other kernel
4789 4790 * component, we check and return an error.
4790 4791 */
4791 4792 cr = msg_getcred(mp, NULL);
4792 4793 ASSERT(cr != NULL);
4793 4794 if (cr == NULL) {
4794 4795 icmp_err_ack(q, mp, TSYSERR, EINVAL);
4795 4796 return;
4796 4797 }
4797 4798 tpi_optcom_req(q, mp, cr, &icmp_opt_obj);
4798 4799 return;
4799 4800
4800 4801 case T_DISCON_REQ:
4801 4802 icmp_tpi_disconnect(q, mp);
4802 4803 return;
4803 4804
4804 4805 /* The following TPI message is not supported by icmp. */
4805 4806 case O_T_CONN_RES:
4806 4807 case T_CONN_RES:
4807 4808 icmp_err_ack(q, mp, TNOTSUPPORT, 0);
4808 4809 return;
4809 4810
4810 4811 /* The following 3 TPI requests are illegal for icmp. */
4811 4812 case T_DATA_REQ:
4812 4813 case T_EXDATA_REQ:
4813 4814 case T_ORDREL_REQ:
4814 4815 icmp_err_ack(q, mp, TNOTSUPPORT, 0);
4815 4816 return;
4816 4817 default:
4817 4818 break;
4818 4819 }
4819 4820 break;
4820 4821 case M_FLUSH:
4821 4822 if (*rptr & FLUSHW)
4822 4823 flushq(q, FLUSHDATA);
4823 4824 break;
4824 4825 case M_IOCTL:
4825 4826 iocp = (struct iocblk *)mp->b_rptr;
4826 4827 switch (iocp->ioc_cmd) {
4827 4828 case TI_GETPEERNAME:
4828 4829 if (icmp->icmp_state != TS_DATA_XFER) {
4829 4830 /*
4830 4831 * If a default destination address has not
4831 4832 * been associated with the stream, then we
4832 4833 * don't know the peer's name.
4833 4834 */
4834 4835 iocp->ioc_error = ENOTCONN;
4835 4836 iocp->ioc_count = 0;
4836 4837 mp->b_datap->db_type = M_IOCACK;
4837 4838 qreply(q, mp);
4838 4839 return;
4839 4840 }
4840 4841 /* FALLTHRU */
4841 4842 case TI_GETMYNAME:
4842 4843 /*
4843 4844 * For TI_GETPEERNAME and TI_GETMYNAME, we first
4844 4845 * need to copyin the user's strbuf structure.
4845 4846 * Processing will continue in the M_IOCDATA case
4846 4847 * below.
4847 4848 */
4848 4849 mi_copyin(q, mp, NULL,
4849 4850 SIZEOF_STRUCT(strbuf, iocp->ioc_flag));
4850 4851 return;
4851 4852 default:
4852 4853 break;
4853 4854 }
4854 4855 break;
4855 4856 case M_IOCDATA:
4856 4857 icmp_wput_iocdata(q, mp);
4857 4858 return;
4858 4859 default:
4859 4860 /* Unrecognized messages are passed through without change. */
4860 4861 break;
4861 4862 }
4862 4863 ip_wput_nondata(q, mp);
4863 4864 }
4864 4865
4865 4866 /*
4866 4867 * icmp_wput_iocdata is called by icmp_wput_other to handle all M_IOCDATA
4867 4868 * messages.
4868 4869 */
4869 4870 static void
4870 4871 icmp_wput_iocdata(queue_t *q, mblk_t *mp)
4871 4872 {
4872 4873 mblk_t *mp1;
4873 4874 STRUCT_HANDLE(strbuf, sb);
4874 4875 uint_t addrlen;
4875 4876 conn_t *connp = Q_TO_CONN(q);
4876 4877 icmp_t *icmp = connp->conn_icmp;
4877 4878
4878 4879 /* Make sure it is one of ours. */
4879 4880 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
4880 4881 case TI_GETMYNAME:
4881 4882 case TI_GETPEERNAME:
4882 4883 break;
4883 4884 default:
4884 4885 ip_wput_nondata(q, mp);
4885 4886 return;
4886 4887 }
4887 4888
4888 4889 switch (mi_copy_state(q, mp, &mp1)) {
4889 4890 case -1:
4890 4891 return;
4891 4892 case MI_COPY_CASE(MI_COPY_IN, 1):
4892 4893 break;
4893 4894 case MI_COPY_CASE(MI_COPY_OUT, 1):
4894 4895 /*
4895 4896 * The address has been copied out, so now
4896 4897 * copyout the strbuf.
4897 4898 */
4898 4899 mi_copyout(q, mp);
4899 4900 return;
4900 4901 case MI_COPY_CASE(MI_COPY_OUT, 2):
4901 4902 /*
4902 4903 * The address and strbuf have been copied out.
4903 4904 * We're done, so just acknowledge the original
4904 4905 * M_IOCTL.
4905 4906 */
4906 4907 mi_copy_done(q, mp, 0);
4907 4908 return;
4908 4909 default:
4909 4910 /*
4910 4911 * Something strange has happened, so acknowledge
4911 4912 * the original M_IOCTL with an EPROTO error.
4912 4913 */
4913 4914 mi_copy_done(q, mp, EPROTO);
4914 4915 return;
4915 4916 }
4916 4917
4917 4918 /*
4918 4919 * Now we have the strbuf structure for TI_GETMYNAME
4919 4920 * and TI_GETPEERNAME. Next we copyout the requested
4920 4921 * address and then we'll copyout the strbuf.
4921 4922 */
4922 4923 STRUCT_SET_HANDLE(sb, ((struct iocblk *)mp->b_rptr)->ioc_flag,
4923 4924 (void *)mp1->b_rptr);
4924 4925
4925 4926 if (connp->conn_family == AF_INET)
4926 4927 addrlen = sizeof (sin_t);
4927 4928 else
4928 4929 addrlen = sizeof (sin6_t);
4929 4930
4930 4931 if (STRUCT_FGET(sb, maxlen) < addrlen) {
4931 4932 mi_copy_done(q, mp, EINVAL);
4932 4933 return;
4933 4934 }
4934 4935 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
4935 4936 case TI_GETMYNAME:
4936 4937 break;
4937 4938 case TI_GETPEERNAME:
4938 4939 if (icmp->icmp_state != TS_DATA_XFER) {
4939 4940 mi_copy_done(q, mp, ENOTCONN);
4940 4941 return;
4941 4942 }
4942 4943 break;
4943 4944 default:
4944 4945 mi_copy_done(q, mp, EPROTO);
4945 4946 return;
4946 4947 }
4947 4948 mp1 = mi_copyout_alloc(q, mp, STRUCT_FGETP(sb, buf), addrlen, B_TRUE);
4948 4949 if (!mp1)
4949 4950 return;
4950 4951
4951 4952 STRUCT_FSET(sb, len, addrlen);
4952 4953 switch (((struct iocblk *)mp->b_rptr)->ioc_cmd) {
4953 4954 case TI_GETMYNAME:
4954 4955 (void) conn_getsockname(connp, (struct sockaddr *)mp1->b_wptr,
4955 4956 &addrlen);
4956 4957 break;
4957 4958 case TI_GETPEERNAME:
4958 4959 (void) conn_getpeername(connp, (struct sockaddr *)mp1->b_wptr,
4959 4960 &addrlen);
4960 4961 break;
4961 4962 }
4962 4963 mp1->b_wptr += addrlen;
4963 4964 /* Copy out the address */
4964 4965 mi_copyout(q, mp);
4965 4966 }
4966 4967
4967 4968 void
4968 4969 icmp_ddi_g_init(void)
4969 4970 {
4970 4971 icmp_max_optsize = optcom_max_optsize(icmp_opt_obj.odb_opt_des_arr,
4971 4972 icmp_opt_obj.odb_opt_arr_cnt);
4972 4973
4973 4974 /*
4974 4975 * We want to be informed each time a stack is created or
4975 4976 * destroyed in the kernel, so we can maintain the
4976 4977 * set of icmp_stack_t's.
4977 4978 */
4978 4979 netstack_register(NS_ICMP, rawip_stack_init, NULL, rawip_stack_fini);
4979 4980 }
4980 4981
4981 4982 void
4982 4983 icmp_ddi_g_destroy(void)
4983 4984 {
4984 4985 netstack_unregister(NS_ICMP);
4985 4986 }
4986 4987
4987 4988 #define INET_NAME "ip"
4988 4989
4989 4990 /*
4990 4991 * Initialize the ICMP stack instance.
4991 4992 */
4992 4993 static void *
4993 4994 rawip_stack_init(netstackid_t stackid, netstack_t *ns)
4994 4995 {
4995 4996 icmp_stack_t *is;
4996 4997 int error = 0;
4997 4998 size_t arrsz;
4998 4999 major_t major;
4999 5000
5000 5001 is = (icmp_stack_t *)kmem_zalloc(sizeof (*is), KM_SLEEP);
5001 5002 is->is_netstack = ns;
5002 5003
5003 5004 arrsz = sizeof (icmp_propinfo_tbl);
5004 5005 is->is_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
5005 5006 bcopy(icmp_propinfo_tbl, is->is_propinfo_tbl, arrsz);
5006 5007
5007 5008 is->is_ksp = rawip_kstat_init(stackid);
5008 5009
5009 5010 major = mod_name_to_major(INET_NAME);
5010 5011 error = ldi_ident_from_major(major, &is->is_ldi_ident);
5011 5012 ASSERT(error == 0);
5012 5013 return (is);
5013 5014 }
5014 5015
5015 5016 /*
5016 5017 * Free the ICMP stack instance.
5017 5018 */
5018 5019 static void
5019 5020 rawip_stack_fini(netstackid_t stackid, void *arg)
5020 5021 {
5021 5022 icmp_stack_t *is = (icmp_stack_t *)arg;
5022 5023
5023 5024 kmem_free(is->is_propinfo_tbl, sizeof (icmp_propinfo_tbl));
5024 5025 is->is_propinfo_tbl = NULL;
5025 5026
5026 5027 rawip_kstat_fini(stackid, is->is_ksp);
5027 5028 is->is_ksp = NULL;
5028 5029 ldi_ident_release(is->is_ldi_ident);
5029 5030 kmem_free(is, sizeof (*is));
5030 5031 }
5031 5032
5032 5033 static void *
5033 5034 rawip_kstat_init(netstackid_t stackid)
5034 5035 {
5035 5036 kstat_t *ksp;
5036 5037
5037 5038 rawip_named_kstat_t template = {
5038 5039 { "inDatagrams", KSTAT_DATA_UINT32, 0 },
5039 5040 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
5040 5041 { "inErrors", KSTAT_DATA_UINT32, 0 },
5041 5042 { "outDatagrams", KSTAT_DATA_UINT32, 0 },
5042 5043 { "outErrors", KSTAT_DATA_UINT32, 0 },
5043 5044 };
5044 5045
5045 5046 ksp = kstat_create_netstack("icmp", 0, "rawip", "mib2",
5046 5047 KSTAT_TYPE_NAMED, NUM_OF_FIELDS(rawip_named_kstat_t), 0, stackid);
5047 5048 if (ksp == NULL || ksp->ks_data == NULL)
5048 5049 return (NULL);
5049 5050
5050 5051 bcopy(&template, ksp->ks_data, sizeof (template));
5051 5052 ksp->ks_update = rawip_kstat_update;
5052 5053 ksp->ks_private = (void *)(uintptr_t)stackid;
5053 5054
5054 5055 kstat_install(ksp);
5055 5056 return (ksp);
5056 5057 }
5057 5058
5058 5059 static void
5059 5060 rawip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
5060 5061 {
5061 5062 if (ksp != NULL) {
5062 5063 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
5063 5064 kstat_delete_netstack(ksp, stackid);
5064 5065 }
↓ open down ↓ |
5030 lines elided |
↑ open up ↑ |
5065 5066 }
5066 5067
5067 5068 static int
5068 5069 rawip_kstat_update(kstat_t *ksp, int rw)
5069 5070 {
5070 5071 rawip_named_kstat_t *rawipkp;
5071 5072 netstackid_t stackid = (netstackid_t)(uintptr_t)ksp->ks_private;
5072 5073 netstack_t *ns;
5073 5074 icmp_stack_t *is;
5074 5075
5075 - if ((ksp == NULL) || (ksp->ks_data == NULL))
5076 + if (ksp->ks_data == NULL)
5076 5077 return (EIO);
5077 5078
5078 5079 if (rw == KSTAT_WRITE)
5079 5080 return (EACCES);
5080 5081
5081 5082 rawipkp = (rawip_named_kstat_t *)ksp->ks_data;
5082 5083
5083 5084 ns = netstack_find_by_stackid(stackid);
5084 5085 if (ns == NULL)
5085 5086 return (-1);
5086 5087 is = ns->netstack_icmp;
5087 5088 if (is == NULL) {
5088 5089 netstack_rele(ns);
5089 5090 return (-1);
5090 5091 }
5091 5092 rawipkp->inDatagrams.value.ui32 = is->is_rawip_mib.rawipInDatagrams;
5092 5093 rawipkp->inCksumErrs.value.ui32 = is->is_rawip_mib.rawipInCksumErrs;
5093 5094 rawipkp->inErrors.value.ui32 = is->is_rawip_mib.rawipInErrors;
5094 5095 rawipkp->outDatagrams.value.ui32 = is->is_rawip_mib.rawipOutDatagrams;
5095 5096 rawipkp->outErrors.value.ui32 = is->is_rawip_mib.rawipOutErrors;
5096 5097 netstack_rele(ns);
5097 5098 return (0);
5098 5099 }
5099 5100
5100 5101 /* ARGSUSED */
5101 5102 int
5102 5103 rawip_accept(sock_lower_handle_t lproto_handle,
5103 5104 sock_lower_handle_t eproto_handle, sock_upper_handle_t sock_handle,
5104 5105 cred_t *cr)
5105 5106 {
5106 5107 return (EOPNOTSUPP);
5107 5108 }
5108 5109
5109 5110 /* ARGSUSED */
5110 5111 int
5111 5112 rawip_bind(sock_lower_handle_t proto_handle, struct sockaddr *sa,
5112 5113 socklen_t len, cred_t *cr)
5113 5114 {
5114 5115 conn_t *connp = (conn_t *)proto_handle;
5115 5116 int error;
5116 5117
5117 5118 /* All Solaris components should pass a cred for this operation. */
5118 5119 ASSERT(cr != NULL);
5119 5120
5120 5121 /* Binding to a NULL address really means unbind */
5121 5122 if (sa == NULL)
5122 5123 error = rawip_do_unbind(connp);
5123 5124 else
5124 5125 error = rawip_do_bind(connp, sa, len);
5125 5126
5126 5127 if (error < 0) {
5127 5128 if (error == -TOUTSTATE)
5128 5129 error = EINVAL;
5129 5130 else
5130 5131 error = proto_tlitosyserr(-error);
5131 5132 }
5132 5133 return (error);
5133 5134 }
5134 5135
5135 5136 static int
5136 5137 rawip_implicit_bind(conn_t *connp)
5137 5138 {
5138 5139 sin6_t sin6addr;
5139 5140 sin_t *sin;
5140 5141 sin6_t *sin6;
5141 5142 socklen_t len;
5142 5143 int error;
5143 5144
5144 5145 if (connp->conn_family == AF_INET) {
5145 5146 len = sizeof (struct sockaddr_in);
5146 5147 sin = (sin_t *)&sin6addr;
5147 5148 *sin = sin_null;
5148 5149 sin->sin_family = AF_INET;
5149 5150 sin->sin_addr.s_addr = INADDR_ANY;
5150 5151 } else {
5151 5152 ASSERT(connp->conn_family == AF_INET6);
5152 5153 len = sizeof (sin6_t);
5153 5154 sin6 = (sin6_t *)&sin6addr;
5154 5155 *sin6 = sin6_null;
5155 5156 sin6->sin6_family = AF_INET6;
5156 5157 V6_SET_ZERO(sin6->sin6_addr);
5157 5158 }
5158 5159
5159 5160 error = rawip_do_bind(connp, (struct sockaddr *)&sin6addr, len);
5160 5161
5161 5162 return ((error < 0) ? proto_tlitosyserr(-error) : error);
5162 5163 }
5163 5164
5164 5165 static int
5165 5166 rawip_unbind(conn_t *connp)
5166 5167 {
5167 5168 int error;
5168 5169
5169 5170 error = rawip_do_unbind(connp);
5170 5171 if (error < 0) {
5171 5172 error = proto_tlitosyserr(-error);
5172 5173 }
5173 5174 return (error);
5174 5175 }
5175 5176
5176 5177 /* ARGSUSED */
5177 5178 int
5178 5179 rawip_listen(sock_lower_handle_t proto_handle, int backlog, cred_t *cr)
5179 5180 {
5180 5181 return (EOPNOTSUPP);
5181 5182 }
5182 5183
5183 5184 int
5184 5185 rawip_connect(sock_lower_handle_t proto_handle, const struct sockaddr *sa,
5185 5186 socklen_t len, sock_connid_t *id, cred_t *cr)
5186 5187 {
5187 5188 conn_t *connp = (conn_t *)proto_handle;
5188 5189 icmp_t *icmp = connp->conn_icmp;
5189 5190 int error;
5190 5191 boolean_t did_bind = B_FALSE;
5191 5192 pid_t pid = curproc->p_pid;
5192 5193
5193 5194 /* All Solaris components should pass a cred for this operation. */
5194 5195 ASSERT(cr != NULL);
5195 5196
5196 5197 if (sa == NULL) {
5197 5198 /*
5198 5199 * Disconnect
5199 5200 * Make sure we are connected
5200 5201 */
5201 5202 if (icmp->icmp_state != TS_DATA_XFER)
5202 5203 return (EINVAL);
5203 5204
5204 5205 error = icmp_disconnect(connp);
5205 5206 return (error);
5206 5207 }
5207 5208
5208 5209 error = proto_verify_ip_addr(connp->conn_family, sa, len);
5209 5210 if (error != 0)
5210 5211 return (error);
5211 5212
5212 5213 /* do an implicit bind if necessary */
5213 5214 if (icmp->icmp_state == TS_UNBND) {
5214 5215 error = rawip_implicit_bind(connp);
5215 5216 /*
5216 5217 * We could be racing with an actual bind, in which case
5217 5218 * we would see EPROTO. We cross our fingers and try
5218 5219 * to connect.
5219 5220 */
5220 5221 if (!(error == 0 || error == EPROTO))
5221 5222 return (error);
5222 5223 did_bind = B_TRUE;
5223 5224 }
5224 5225
5225 5226 /*
5226 5227 * set SO_DGRAM_ERRIND
5227 5228 */
5228 5229 connp->conn_dgram_errind = B_TRUE;
5229 5230
5230 5231 error = rawip_do_connect(connp, sa, len, cr, pid);
5231 5232 if (error != 0 && did_bind) {
5232 5233 int unbind_err;
5233 5234
5234 5235 unbind_err = rawip_unbind(connp);
5235 5236 ASSERT(unbind_err == 0);
5236 5237 }
5237 5238
5238 5239 if (error == 0) {
5239 5240 *id = 0;
5240 5241 (*connp->conn_upcalls->su_connected)(connp->conn_upper_handle,
5241 5242 0, NULL, -1);
5242 5243 } else if (error < 0) {
5243 5244 error = proto_tlitosyserr(-error);
5244 5245 }
5245 5246 return (error);
5246 5247 }
5247 5248
5248 5249 /* ARGSUSED2 */
5249 5250 int
5250 5251 rawip_fallback(sock_lower_handle_t proto_handle, queue_t *q,
5251 5252 boolean_t direct_sockfs, so_proto_quiesced_cb_t quiesced_cb,
5252 5253 sock_quiesce_arg_t *arg)
5253 5254 {
5254 5255 conn_t *connp = (conn_t *)proto_handle;
5255 5256 icmp_t *icmp;
5256 5257 struct T_capability_ack tca;
5257 5258 struct sockaddr_in6 laddr, faddr;
5258 5259 socklen_t laddrlen, faddrlen;
5259 5260 short opts;
5260 5261 struct stroptions *stropt;
5261 5262 mblk_t *mp, *stropt_mp;
5262 5263 int error;
5263 5264
5264 5265 icmp = connp->conn_icmp;
5265 5266
5266 5267 stropt_mp = allocb_wait(sizeof (*stropt), BPRI_HI, STR_NOSIG, NULL);
5267 5268
5268 5269 /*
5269 5270 * setup the fallback stream that was allocated
5270 5271 */
5271 5272 connp->conn_dev = (dev_t)RD(q)->q_ptr;
5272 5273 connp->conn_minor_arena = WR(q)->q_ptr;
5273 5274
5274 5275 RD(q)->q_ptr = WR(q)->q_ptr = connp;
5275 5276
5276 5277 WR(q)->q_qinfo = &icmpwinit;
5277 5278
5278 5279 connp->conn_rq = RD(q);
5279 5280 connp->conn_wq = WR(q);
5280 5281
5281 5282 /* Notify stream head about options before sending up data */
5282 5283 stropt_mp->b_datap->db_type = M_SETOPTS;
5283 5284 stropt_mp->b_wptr += sizeof (*stropt);
5284 5285 stropt = (struct stroptions *)stropt_mp->b_rptr;
5285 5286 stropt->so_flags = SO_WROFF | SO_HIWAT;
5286 5287 stropt->so_wroff = connp->conn_wroff;
5287 5288 stropt->so_hiwat = connp->conn_rcvbuf;
5288 5289 putnext(RD(q), stropt_mp);
5289 5290
5290 5291 /*
5291 5292 * free helper stream
5292 5293 */
5293 5294 ip_free_helper_stream(connp);
5294 5295
5295 5296 /*
5296 5297 * Collect the information needed to sync with the sonode
5297 5298 */
5298 5299 icmp_do_capability_ack(icmp, &tca, TC1_INFO);
5299 5300
5300 5301 laddrlen = faddrlen = sizeof (sin6_t);
5301 5302 (void) rawip_getsockname((sock_lower_handle_t)connp,
5302 5303 (struct sockaddr *)&laddr, &laddrlen, CRED());
5303 5304 error = rawip_getpeername((sock_lower_handle_t)connp,
5304 5305 (struct sockaddr *)&faddr, &faddrlen, CRED());
5305 5306 if (error != 0)
5306 5307 faddrlen = 0;
5307 5308 opts = 0;
5308 5309 if (connp->conn_dgram_errind)
5309 5310 opts |= SO_DGRAM_ERRIND;
5310 5311 if (connp->conn_ixa->ixa_flags & IXAF_DONTROUTE)
5311 5312 opts |= SO_DONTROUTE;
5312 5313
5313 5314 mp = (*quiesced_cb)(connp->conn_upper_handle, arg, &tca,
5314 5315 (struct sockaddr *)&laddr, laddrlen,
5315 5316 (struct sockaddr *)&faddr, faddrlen, opts);
5316 5317
5317 5318 /*
5318 5319 * Attempts to send data up during fallback will result in it being
5319 5320 * queued in icmp_t. Now we push up any queued packets.
5320 5321 */
5321 5322 mutex_enter(&icmp->icmp_recv_lock);
5322 5323 if (mp != NULL) {
5323 5324 mp->b_next = icmp->icmp_fallback_queue_head;
5324 5325 icmp->icmp_fallback_queue_head = mp;
5325 5326 }
5326 5327 while (icmp->icmp_fallback_queue_head != NULL) {
5327 5328 mp = icmp->icmp_fallback_queue_head;
5328 5329 icmp->icmp_fallback_queue_head = mp->b_next;
5329 5330 mp->b_next = NULL;
5330 5331 mutex_exit(&icmp->icmp_recv_lock);
5331 5332 putnext(RD(q), mp);
5332 5333 mutex_enter(&icmp->icmp_recv_lock);
5333 5334 }
5334 5335 icmp->icmp_fallback_queue_tail = icmp->icmp_fallback_queue_head;
5335 5336
5336 5337 /*
5337 5338 * No longer a streams less socket
5338 5339 */
5339 5340 mutex_enter(&connp->conn_lock);
5340 5341 connp->conn_flags &= ~IPCL_NONSTR;
5341 5342 mutex_exit(&connp->conn_lock);
5342 5343
5343 5344 mutex_exit(&icmp->icmp_recv_lock);
5344 5345
5345 5346 ASSERT(icmp->icmp_fallback_queue_head == NULL &&
5346 5347 icmp->icmp_fallback_queue_tail == NULL);
5347 5348
5348 5349 ASSERT(connp->conn_ref >= 1);
5349 5350
5350 5351 return (0);
5351 5352 }
5352 5353
5353 5354 /* ARGSUSED2 */
5354 5355 sock_lower_handle_t
5355 5356 rawip_create(int family, int type, int proto, sock_downcalls_t **sock_downcalls,
5356 5357 uint_t *smodep, int *errorp, int flags, cred_t *credp)
5357 5358 {
5358 5359 conn_t *connp;
5359 5360
5360 5361 if (type != SOCK_RAW || (family != AF_INET && family != AF_INET6)) {
5361 5362 *errorp = EPROTONOSUPPORT;
5362 5363 return (NULL);
5363 5364 }
5364 5365
5365 5366 connp = rawip_do_open(family, credp, errorp, flags);
5366 5367 if (connp != NULL) {
5367 5368 connp->conn_flags |= IPCL_NONSTR;
5368 5369
5369 5370 mutex_enter(&connp->conn_lock);
5370 5371 connp->conn_state_flags &= ~CONN_INCIPIENT;
5371 5372 mutex_exit(&connp->conn_lock);
5372 5373 *sock_downcalls = &sock_rawip_downcalls;
5373 5374 *smodep = SM_ATOMIC;
5374 5375 } else {
5375 5376 ASSERT(*errorp != 0);
5376 5377 }
5377 5378
5378 5379 return ((sock_lower_handle_t)connp);
5379 5380 }
5380 5381
5381 5382 /* ARGSUSED3 */
5382 5383 void
5383 5384 rawip_activate(sock_lower_handle_t proto_handle,
5384 5385 sock_upper_handle_t sock_handle, sock_upcalls_t *sock_upcalls, int flags,
5385 5386 cred_t *cr)
5386 5387 {
5387 5388 conn_t *connp = (conn_t *)proto_handle;
5388 5389 struct sock_proto_props sopp;
5389 5390
5390 5391 /* All Solaris components should pass a cred for this operation. */
5391 5392 ASSERT(cr != NULL);
5392 5393
5393 5394 connp->conn_upcalls = sock_upcalls;
5394 5395 connp->conn_upper_handle = sock_handle;
5395 5396
5396 5397 sopp.sopp_flags = SOCKOPT_WROFF | SOCKOPT_RCVHIWAT | SOCKOPT_RCVLOWAT |
5397 5398 SOCKOPT_MAXBLK | SOCKOPT_MAXPSZ | SOCKOPT_MINPSZ;
5398 5399 sopp.sopp_wroff = connp->conn_wroff;
5399 5400 sopp.sopp_rxhiwat = connp->conn_rcvbuf;
5400 5401 sopp.sopp_rxlowat = connp->conn_rcvlowat;
5401 5402 sopp.sopp_maxblk = INFPSZ;
5402 5403 sopp.sopp_maxpsz = IP_MAXPACKET;
5403 5404 sopp.sopp_minpsz = (icmp_mod_info.mi_minpsz == 1) ? 0 :
5404 5405 icmp_mod_info.mi_minpsz;
5405 5406
5406 5407 (*connp->conn_upcalls->su_set_proto_props)
5407 5408 (connp->conn_upper_handle, &sopp);
5408 5409
5409 5410 icmp_bind_proto(connp->conn_icmp);
5410 5411 }
5411 5412
5412 5413 /* ARGSUSED3 */
5413 5414 int
5414 5415 rawip_getpeername(sock_lower_handle_t proto_handle, struct sockaddr *sa,
5415 5416 socklen_t *salenp, cred_t *cr)
5416 5417 {
5417 5418 conn_t *connp = (conn_t *)proto_handle;
5418 5419 icmp_t *icmp = connp->conn_icmp;
5419 5420 int error;
5420 5421
5421 5422 /* All Solaris components should pass a cred for this operation. */
5422 5423 ASSERT(cr != NULL);
5423 5424
5424 5425 mutex_enter(&connp->conn_lock);
5425 5426 if (icmp->icmp_state != TS_DATA_XFER)
5426 5427 error = ENOTCONN;
5427 5428 else
5428 5429 error = conn_getpeername(connp, sa, salenp);
5429 5430 mutex_exit(&connp->conn_lock);
5430 5431 return (error);
5431 5432 }
5432 5433
5433 5434 /* ARGSUSED3 */
5434 5435 int
5435 5436 rawip_getsockname(sock_lower_handle_t proto_handle, struct sockaddr *sa,
5436 5437 socklen_t *salenp, cred_t *cr)
5437 5438 {
5438 5439 conn_t *connp = (conn_t *)proto_handle;
5439 5440 int error;
5440 5441
5441 5442 /* All Solaris components should pass a cred for this operation. */
5442 5443 ASSERT(cr != NULL);
5443 5444
5444 5445 mutex_enter(&connp->conn_lock);
5445 5446 error = conn_getsockname(connp, sa, salenp);
5446 5447 mutex_exit(&connp->conn_lock);
5447 5448 return (error);
5448 5449 }
5449 5450
5450 5451 int
5451 5452 rawip_setsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
5452 5453 const void *optvalp, socklen_t optlen, cred_t *cr)
5453 5454 {
5454 5455 conn_t *connp = (conn_t *)proto_handle;
5455 5456 int error;
5456 5457
5457 5458 /* All Solaris components should pass a cred for this operation. */
5458 5459 ASSERT(cr != NULL);
5459 5460
5460 5461 error = proto_opt_check(level, option_name, optlen, NULL,
5461 5462 icmp_opt_obj.odb_opt_des_arr,
5462 5463 icmp_opt_obj.odb_opt_arr_cnt,
5463 5464 B_TRUE, B_FALSE, cr);
5464 5465
5465 5466 if (error != 0) {
5466 5467 /*
5467 5468 * option not recognized
5468 5469 */
5469 5470 if (error < 0) {
5470 5471 error = proto_tlitosyserr(-error);
5471 5472 }
5472 5473 return (error);
5473 5474 }
5474 5475
5475 5476 error = icmp_opt_set(connp, SETFN_OPTCOM_NEGOTIATE, level,
5476 5477 option_name, optlen, (uchar_t *)optvalp, (uint_t *)&optlen,
5477 5478 (uchar_t *)optvalp, NULL, cr);
5478 5479
5479 5480 ASSERT(error >= 0);
5480 5481
5481 5482 return (error);
5482 5483 }
5483 5484
5484 5485 int
5485 5486 rawip_getsockopt(sock_lower_handle_t proto_handle, int level, int option_name,
5486 5487 void *optvalp, socklen_t *optlen, cred_t *cr)
5487 5488 {
5488 5489 int error;
5489 5490 conn_t *connp = (conn_t *)proto_handle;
5490 5491 t_uscalar_t max_optbuf_len;
5491 5492 void *optvalp_buf;
5492 5493 int len;
5493 5494
5494 5495 /* All Solaris components should pass a cred for this operation. */
5495 5496 ASSERT(cr != NULL);
5496 5497
5497 5498 error = proto_opt_check(level, option_name, *optlen, &max_optbuf_len,
5498 5499 icmp_opt_obj.odb_opt_des_arr,
5499 5500 icmp_opt_obj.odb_opt_arr_cnt,
5500 5501 B_FALSE, B_TRUE, cr);
5501 5502
5502 5503 if (error != 0) {
5503 5504 if (error < 0) {
5504 5505 error = proto_tlitosyserr(-error);
5505 5506 }
5506 5507 return (error);
5507 5508 }
5508 5509
5509 5510 optvalp_buf = kmem_alloc(max_optbuf_len, KM_SLEEP);
5510 5511 len = icmp_opt_get(connp, level, option_name, optvalp_buf);
5511 5512 if (len == -1) {
5512 5513 kmem_free(optvalp_buf, max_optbuf_len);
5513 5514 return (EINVAL);
5514 5515 }
5515 5516
5516 5517 /*
5517 5518 * update optlen and copy option value
5518 5519 */
5519 5520 t_uscalar_t size = MIN(len, *optlen);
5520 5521
5521 5522 bcopy(optvalp_buf, optvalp, size);
5522 5523 bcopy(&size, optlen, sizeof (size));
5523 5524
5524 5525 kmem_free(optvalp_buf, max_optbuf_len);
5525 5526 return (0);
5526 5527 }
5527 5528
5528 5529 /* ARGSUSED1 */
5529 5530 int
5530 5531 rawip_close(sock_lower_handle_t proto_handle, int flags, cred_t *cr)
5531 5532 {
5532 5533 conn_t *connp = (conn_t *)proto_handle;
5533 5534
5534 5535 /* All Solaris components should pass a cred for this operation. */
5535 5536 ASSERT(cr != NULL);
5536 5537
5537 5538 (void) rawip_do_close(connp);
5538 5539 return (0);
5539 5540 }
5540 5541
5541 5542 /* ARGSUSED2 */
5542 5543 int
5543 5544 rawip_shutdown(sock_lower_handle_t proto_handle, int how, cred_t *cr)
5544 5545 {
5545 5546 conn_t *connp = (conn_t *)proto_handle;
5546 5547
5547 5548 /* All Solaris components should pass a cred for this operation. */
5548 5549 ASSERT(cr != NULL);
5549 5550
5550 5551 /* shut down the send side */
5551 5552 if (how != SHUT_RD)
5552 5553 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
5553 5554 SOCK_OPCTL_SHUT_SEND, 0);
5554 5555 /* shut down the recv side */
5555 5556 if (how != SHUT_WR)
5556 5557 (*connp->conn_upcalls->su_opctl)(connp->conn_upper_handle,
5557 5558 SOCK_OPCTL_SHUT_RECV, 0);
5558 5559 return (0);
5559 5560 }
5560 5561
5561 5562 void
5562 5563 rawip_clr_flowctrl(sock_lower_handle_t proto_handle)
5563 5564 {
5564 5565 conn_t *connp = (conn_t *)proto_handle;
5565 5566 icmp_t *icmp = connp->conn_icmp;
5566 5567
5567 5568 mutex_enter(&icmp->icmp_recv_lock);
5568 5569 connp->conn_flow_cntrld = B_FALSE;
5569 5570 mutex_exit(&icmp->icmp_recv_lock);
5570 5571 }
5571 5572
5572 5573 int
5573 5574 rawip_ioctl(sock_lower_handle_t proto_handle, int cmd, intptr_t arg,
5574 5575 int mode, int32_t *rvalp, cred_t *cr)
5575 5576 {
5576 5577 conn_t *connp = (conn_t *)proto_handle;
5577 5578 int error;
5578 5579
5579 5580 /* All Solaris components should pass a cred for this operation. */
5580 5581 ASSERT(cr != NULL);
5581 5582
5582 5583 /*
5583 5584 * If we don't have a helper stream then create one.
5584 5585 * ip_create_helper_stream takes care of locking the conn_t,
5585 5586 * so this check for NULL is just a performance optimization.
5586 5587 */
5587 5588 if (connp->conn_helper_info == NULL) {
5588 5589 icmp_stack_t *is = connp->conn_icmp->icmp_is;
5589 5590
5590 5591 ASSERT(is->is_ldi_ident != NULL);
5591 5592
5592 5593 /*
5593 5594 * Create a helper stream for non-STREAMS socket.
5594 5595 */
5595 5596 error = ip_create_helper_stream(connp, is->is_ldi_ident);
5596 5597 if (error != 0) {
5597 5598 ip0dbg(("rawip_ioctl: create of IP helper stream "
5598 5599 "failed %d\n", error));
5599 5600 return (error);
5600 5601 }
5601 5602 }
5602 5603
5603 5604 switch (cmd) {
5604 5605 case _SIOCSOCKFALLBACK:
5605 5606 case TI_GETPEERNAME:
5606 5607 case TI_GETMYNAME:
5607 5608 #ifdef DEBUG
5608 5609 cmn_err(CE_CONT, "icmp_ioctl cmd 0x%x on non streams"
5609 5610 " socket", cmd);
5610 5611 #endif
5611 5612 error = EINVAL;
5612 5613 break;
5613 5614 default:
5614 5615 /*
5615 5616 * Pass on to IP using helper stream
5616 5617 */
5617 5618 error = ldi_ioctl(connp->conn_helper_info->iphs_handle,
5618 5619 cmd, arg, mode, cr, rvalp);
5619 5620 break;
5620 5621 }
5621 5622 return (error);
5622 5623 }
5623 5624
5624 5625 int
5625 5626 rawip_send(sock_lower_handle_t proto_handle, mblk_t *mp, struct nmsghdr *msg,
5626 5627 cred_t *cr)
5627 5628 {
5628 5629 sin6_t *sin6;
5629 5630 sin_t *sin = NULL;
5630 5631 uint_t srcid;
5631 5632 conn_t *connp = (conn_t *)proto_handle;
5632 5633 icmp_t *icmp = connp->conn_icmp;
5633 5634 int error = 0;
5634 5635 icmp_stack_t *is = icmp->icmp_is;
5635 5636 pid_t pid = curproc->p_pid;
5636 5637 ip_xmit_attr_t *ixa;
5637 5638
5638 5639 ASSERT(DB_TYPE(mp) == M_DATA);
5639 5640
5640 5641 /* All Solaris components should pass a cred for this operation. */
5641 5642 ASSERT(cr != NULL);
5642 5643
5643 5644 /* do an implicit bind if necessary */
5644 5645 if (icmp->icmp_state == TS_UNBND) {
5645 5646 error = rawip_implicit_bind(connp);
5646 5647 /*
5647 5648 * We could be racing with an actual bind, in which case
5648 5649 * we would see EPROTO. We cross our fingers and try
5649 5650 * to connect.
5650 5651 */
5651 5652 if (!(error == 0 || error == EPROTO)) {
5652 5653 freemsg(mp);
5653 5654 return (error);
5654 5655 }
5655 5656 }
5656 5657
5657 5658 /* Protocol 255 contains full IP headers */
5658 5659 /* Read without holding lock */
5659 5660 if (icmp->icmp_hdrincl) {
5660 5661 ASSERT(connp->conn_ipversion == IPV4_VERSION);
5661 5662 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH) {
5662 5663 if (!pullupmsg(mp, IP_SIMPLE_HDR_LENGTH)) {
5663 5664 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5664 5665 freemsg(mp);
5665 5666 return (EINVAL);
5666 5667 }
5667 5668 }
5668 5669 error = icmp_output_hdrincl(connp, mp, cr, pid);
5669 5670 if (is->is_sendto_ignerr)
5670 5671 return (0);
5671 5672 else
5672 5673 return (error);
5673 5674 }
5674 5675
5675 5676 /* Connected? */
5676 5677 if (msg->msg_name == NULL) {
5677 5678 if (icmp->icmp_state != TS_DATA_XFER) {
5678 5679 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5679 5680 return (EDESTADDRREQ);
5680 5681 }
5681 5682 if (msg->msg_controllen != 0) {
5682 5683 error = icmp_output_ancillary(connp, NULL, NULL, mp,
5683 5684 NULL, msg, cr, pid);
5684 5685 } else {
5685 5686 error = icmp_output_connected(connp, mp, cr, pid);
5686 5687 }
5687 5688 if (is->is_sendto_ignerr)
5688 5689 return (0);
5689 5690 else
5690 5691 return (error);
5691 5692 }
5692 5693 if (icmp->icmp_state == TS_DATA_XFER) {
5693 5694 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5694 5695 return (EISCONN);
5695 5696 }
5696 5697 error = proto_verify_ip_addr(connp->conn_family,
5697 5698 (struct sockaddr *)msg->msg_name, msg->msg_namelen);
5698 5699 if (error != 0) {
5699 5700 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5700 5701 return (error);
5701 5702 }
5702 5703 switch (connp->conn_family) {
5703 5704 case AF_INET6:
5704 5705 sin6 = (sin6_t *)msg->msg_name;
5705 5706
5706 5707 /* No support for mapped addresses on raw sockets */
5707 5708 if (IN6_IS_ADDR_V4MAPPED(&sin6->sin6_addr)) {
5708 5709 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5709 5710 return (EADDRNOTAVAIL);
5710 5711 }
5711 5712 srcid = sin6->__sin6_src_id;
5712 5713
5713 5714 /*
5714 5715 * If the local address is a mapped address return
5715 5716 * an error.
5716 5717 * It would be possible to send an IPv6 packet but the
5717 5718 * response would never make it back to the application
5718 5719 * since it is bound to a mapped address.
5719 5720 */
5720 5721 if (IN6_IS_ADDR_V4MAPPED(&connp->conn_saddr_v6)) {
5721 5722 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5722 5723 return (EADDRNOTAVAIL);
5723 5724 }
5724 5725
5725 5726 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
5726 5727 sin6->sin6_addr = ipv6_loopback;
5727 5728
5728 5729 /*
5729 5730 * We have to allocate an ip_xmit_attr_t before we grab
5730 5731 * conn_lock and we need to hold conn_lock once we've check
5731 5732 * conn_same_as_last_v6 to handle concurrent send* calls on a
5732 5733 * socket.
5733 5734 */
5734 5735 if (msg->msg_controllen == 0) {
5735 5736 ixa = conn_get_ixa(connp, B_FALSE);
5736 5737 if (ixa == NULL) {
5737 5738 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5738 5739 return (ENOMEM);
5739 5740 }
5740 5741 } else {
5741 5742 ixa = NULL;
5742 5743 }
5743 5744 mutex_enter(&connp->conn_lock);
5744 5745 if (icmp->icmp_delayed_error != 0) {
5745 5746 sin6_t *sin2 = (sin6_t *)&icmp->icmp_delayed_addr;
5746 5747
5747 5748 error = icmp->icmp_delayed_error;
5748 5749 icmp->icmp_delayed_error = 0;
5749 5750
5750 5751 /* Compare IP address and family */
5751 5752
5752 5753 if (IN6_ARE_ADDR_EQUAL(&sin6->sin6_addr,
5753 5754 &sin2->sin6_addr) &&
5754 5755 sin6->sin6_family == sin2->sin6_family) {
5755 5756 mutex_exit(&connp->conn_lock);
5756 5757 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5757 5758 if (ixa != NULL)
5758 5759 ixa_refrele(ixa);
5759 5760 return (error);
5760 5761 }
5761 5762 }
5762 5763 if (msg->msg_controllen != 0) {
5763 5764 mutex_exit(&connp->conn_lock);
5764 5765 ASSERT(ixa == NULL);
5765 5766 error = icmp_output_ancillary(connp, NULL, sin6, mp,
5766 5767 NULL, msg, cr, pid);
5767 5768 } else if (conn_same_as_last_v6(connp, sin6) &&
5768 5769 connp->conn_lastsrcid == srcid &&
5769 5770 ipsec_outbound_policy_current(ixa)) {
5770 5771 /* icmp_output_lastdst drops conn_lock */
5771 5772 error = icmp_output_lastdst(connp, mp, cr, pid, ixa);
5772 5773 } else {
5773 5774 /* icmp_output_newdst drops conn_lock */
5774 5775 error = icmp_output_newdst(connp, mp, NULL, sin6, cr,
5775 5776 pid, ixa);
5776 5777 }
5777 5778 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock));
5778 5779 if (is->is_sendto_ignerr)
5779 5780 return (0);
5780 5781 else
5781 5782 return (error);
5782 5783 case AF_INET:
5783 5784 sin = (sin_t *)msg->msg_name;
5784 5785
5785 5786 if (sin->sin_addr.s_addr == INADDR_ANY)
5786 5787 sin->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
5787 5788
5788 5789 /*
5789 5790 * We have to allocate an ip_xmit_attr_t before we grab
5790 5791 * conn_lock and we need to hold conn_lock once we've check
5791 5792 * conn_same_as_last_v6 to handle concurrent send* on a socket.
5792 5793 */
5793 5794 if (msg->msg_controllen == 0) {
5794 5795 ixa = conn_get_ixa(connp, B_FALSE);
5795 5796 if (ixa == NULL) {
5796 5797 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5797 5798 return (ENOMEM);
5798 5799 }
5799 5800 } else {
5800 5801 ixa = NULL;
5801 5802 }
5802 5803 mutex_enter(&connp->conn_lock);
5803 5804 if (icmp->icmp_delayed_error != 0) {
5804 5805 sin_t *sin2 = (sin_t *)&icmp->icmp_delayed_addr;
5805 5806
5806 5807 error = icmp->icmp_delayed_error;
5807 5808 icmp->icmp_delayed_error = 0;
5808 5809
5809 5810 /* Compare IP address */
5810 5811
5811 5812 if (sin->sin_addr.s_addr == sin2->sin_addr.s_addr) {
5812 5813 mutex_exit(&connp->conn_lock);
5813 5814 BUMP_MIB(&is->is_rawip_mib, rawipOutErrors);
5814 5815 if (ixa != NULL)
5815 5816 ixa_refrele(ixa);
5816 5817 return (error);
5817 5818 }
5818 5819 }
5819 5820
5820 5821 if (msg->msg_controllen != 0) {
5821 5822 mutex_exit(&connp->conn_lock);
5822 5823 ASSERT(ixa == NULL);
5823 5824 error = icmp_output_ancillary(connp, sin, NULL, mp,
5824 5825 NULL, msg, cr, pid);
5825 5826 } else if (conn_same_as_last_v4(connp, sin) &&
5826 5827 ipsec_outbound_policy_current(ixa)) {
5827 5828 /* icmp_output_lastdst drops conn_lock */
5828 5829 error = icmp_output_lastdst(connp, mp, cr, pid, ixa);
5829 5830 } else {
5830 5831 /* icmp_output_newdst drops conn_lock */
5831 5832 error = icmp_output_newdst(connp, mp, sin, NULL, cr,
5832 5833 pid, ixa);
5833 5834 }
5834 5835 ASSERT(MUTEX_NOT_HELD(&connp->conn_lock));
5835 5836 if (is->is_sendto_ignerr)
5836 5837 return (0);
5837 5838 else
5838 5839 return (error);
5839 5840 default:
5840 5841 return (EINVAL);
5841 5842 }
5842 5843 }
5843 5844
5844 5845 sock_downcalls_t sock_rawip_downcalls = {
5845 5846 rawip_activate,
5846 5847 rawip_accept,
5847 5848 rawip_bind,
5848 5849 rawip_listen,
5849 5850 rawip_connect,
5850 5851 rawip_getpeername,
5851 5852 rawip_getsockname,
5852 5853 rawip_getsockopt,
5853 5854 rawip_setsockopt,
5854 5855 rawip_send,
5855 5856 NULL,
5856 5857 NULL,
5857 5858 NULL,
5858 5859 rawip_shutdown,
5859 5860 rawip_clr_flowctrl,
5860 5861 rawip_ioctl,
5861 5862 rawip_close
5862 5863 };
↓ open down ↓ |
777 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX