tcp.c revision 3e95bd4ab92abca814bd28e854607d1975c7dc88
2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License (the "License"). 2N/A * You may not use this file except in compliance with the License. 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 2N/A * If applicable, add the following below this CDDL HEADER, with the 2N/A * fields enclosed by brackets "[]" replaced with your own identifying 2N/A * information: Portions Copyright [yyyy] [name of copyright owner] 2N/A * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved. 2N/A/* Copyright (c) 1990 Mentat Inc. */ * TCP Notes: aka FireEngine Phase I (PSARC 2002/433) * (Read the detailed design doc in PSARC case directory) * The entire tcp state is contained in tcp_t and conn_t structure * which are allocated in tandem using ipcl_conn_create() and passing * IPCL_TCPCONN as a flag. We use 'conn_ref' and 'conn_lock' to protect * the references on the tcp_t. The tcp_t structure is never compressed * and packets always land on the correct TCP perimeter from the time * eager is created till the time tcp_t dies (as such the old mentat * TCP global queue is not used for detached state and no IPSEC checking * is required). The global queue is still allocated to send out resets * for connection which have no listeners and IP directly calls * tcp_xmit_listeners_reset() which does any policy check. * Protection and Synchronisation mechanism: * The tcp data structure does not use any kind of lock for protecting * its state but instead uses 'squeues' for mutual exclusion from various * read and write side threads. To access a tcp member, the thread should * always be behind squeue (via squeue_enter with flags as SQ_FILL, SQ_PROCESS, * or SQ_NODRAIN). Since the squeues allow a direct function call, caller * can pass any tcp function having prototype of edesc_t as argument * (different from traditional STREAMs model where packets come in only * designated entry points). The list of functions that can be directly * called via squeue are listed before the usual function prototype. * TCP is MT-Hot and we use a reference based scheme to make sure that the * tcp structure doesn't disappear when its needed. When the application * creates an outgoing connection or accepts an incoming connection, we * start out with 2 references on 'conn_ref'. One for TCP and one for IP. * The IP reference is just a symbolic reference since ip_tcpclose() * looks at tcp structure after tcp_close_output() returns which could * have dropped the last TCP reference. So as long as the connection is * in attached state i.e. !TCP_IS_DETACHED, we have 2 references on the * conn_t. The classifier puts its own reference when the connection is * inserted in listen or connected hash. Anytime a thread needs to enter * the tcp connection perimeter, it retrieves the conn/tcp from q->ptr * on write side or by doing a classify on read side and then puts a * read side, the classifier itself puts the reference under fanout lock * to make sure that tcp can't disappear before it gets processed. The * squeue will drop this reference automatically so the called function * doesn't have to do a DEC_REF. * Opening a new connection: * The outgoing connection open is pretty simple. tcp_open() does the * work in creating the conn/tcp structure and initializing it. The * squeue assignment is done based on the CPU the application * is running on. So for outbound connections, processing is always done * on application CPU which might be different from the incoming CPU * being interrupted by the NIC. An optimal way would be to figure out * the NIC <-> CPU binding at listen time, and assign the outgoing * connection to the squeue attached to the CPU that will be interrupted * for incoming packets (we know the NIC based on the bind IP address). * This might seem like a problem if more data is going out but the * fact is that in most cases the transmit is ACK driven transmit where * the outgoing data normally sits on TCP's xmit queue waiting to be * Accepting a connection: * This is a more interesting case because of various races involved in * establishing a eager in its own perimeter. Read the meta comment on * top of tcp_input_listener(). But briefly, the squeue is picked by * ip_fanout based on the ring or the sender (if loopback). * The close is fairly straight forward. tcp_close() calls tcp_close_output() * via squeue to do the close and mark the tcp as detached if the connection * was in state TCPS_ESTABLISHED or greater. In the later case, TCP keep its * reference but tcp_close() drop IP's reference always. So if tcp was * not killed, it is sitting in time_wait list with 2 reference - 1 for TCP * and 1 because it is in classifier's connected hash. This is the condition * we use to determine that its OK to clean up the tcp outside of squeue * when time wait expires (check the ref under fanout and conn_lock and * if it is 2, remove it from fanout hash and kill it). * Although close just drops the necessary references and marks the * tcp_detached state, tcp_close needs to know the tcp_detached has been * set (under squeue) before letting the STREAM go away (because a * inbound packet might attempt to go up the STREAM while the close * has happened and tcp_detached is not set). So a special lock and * flag is used along with a condition variable (tcp_closelock, tcp_closed, * and tcp_closecv) to signal tcp_close that tcp_close_out() has marked * Special provisions and fast paths: * We make special provisions for sockfs by marking tcp_issocket * whenever we have only sockfs on top of TCP. This allows us to skip * putting the tcp in acceptor hash since a sockfs listener can never * become acceptor and also avoid allocating a tcp_t for acceptor STREAM * since eager has already been allocated and the accept now happens * on acceptor STREAM. There is a big blob of comment on top of * tcp_input_listener explaining the new accept. When socket is POP'd, * sockfs sends us an ioctl to mark the fact and we go back to old * behaviour. Once tcp_issocket is unset, its never set for the * life of that connection. * Since a packet is always executed on the correct TCP perimeter * all IPsec processing is defered to IP including checking new * connections and setting IPSEC policies for new connection. The * only exception is tcp_xmit_listeners_reset() which is called * directly from IP and needs to policy check to see if TH_RST * Values for squeue switch: * To prevent memory hog, limit the number of entries in tcp_free_list * to 1% of available memory / number of cpus #
define TIDUSZ 4096 /* transport interface data unit size */ * Size of acceptor hash list. It has to be a power of 2 for hashing. /* Minimum number of connections per listener. */ /* TCP Timer control structure */ * Functions called directly via squeue having a prototype of edesc_t. /* Prototype for TCP functions */ * Entry points for TCP as a device. The normal case which supports * We have separate open functions for the /dev/tcp and /dev/tcp6 devices. /* Initial entry point for TCP in socket mode. */ /* TCP entry point during fallback */ * Entry points for TCP as a acceptor STREAM opened by sockfs when doing * an accept. Avoid allocating data structures since eager has already /* For AF_INET aka /dev/tcp */ * Following assumes TPI alignment requirements stay along 32 bit /* Template for response to info request. */ sizeof (
sin_t),
/* ADDR_size */ 0,
/* OPT_size - not initialized here */ sizeof (
sin6_t),
/* ADDR_size */ 0,
/* OPT_size - not initialized here */ * TCP tunables related declarations. Definitions are in tcp_tunables.c * Forces all connections to obey the value of the tcps_maxpsz_multiplier * tunable settable via NDD. Otherwise, the per-connection behavior is * determined dynamically during tcp_set_destination(), which is the default. * If the receive buffer size is changed, this function is called to update * the upper socket layer on the new delayed receive wake up threshold. * only increase rcvthresh upto default_threshold * Figure out the value of window scale opton. Note that the rwnd is * ASSUMED to be rounded up to the nearest MSS before the calculation. * We cannot find the scale value and then do a round up of tcp_rwnd * because the scale value may not be correct after that. * Set the compiler flag to make this function inline. * Cleaup before placing on free list. * is per squeue and not per netstack. /* Cleanup that which needs the netstack first */ /* We clear any IP_OPTIONS and extension headers */ /* Release any SSL context */ * Since we will bzero the entire structure, we need to * remove it and reinsert it in global hash list. We * know the walkers can't get to this conn because we * had set CONDEMNED flag earlier and checked reference * under conn_lock so walker won't pick it and when we * go the ipcl_globalhash_remove() below, no walker * Now it is safe to decrement the reference counts. * This might be the last reference on the netstack * in which case it will cause the freeing of the IP Instance. * Adapt to the information, such as rtt and rtt_sd, provided from the * DCE and IRE maintained by IP. * Checks for multicast and broadcast destination address. * Returns zero if ok; an errno on failure. * Note that the MSS calculation here is based on the info given in * the DCE and IRE. We do not do any calculation based on TCP options. They * will be handled in tcp_input_data() when TCP knows which options to use. * Note on how TCP gets its parameters for a connection. * When a tcp_t structure is allocated, it gets all the default parameters. * In tcp_set_destination(), it gets those metric parameters, like rtt, rtt_sd, * spipe, rpipe, ... from the route metrics. Route metric overrides the * An incoming SYN with a multicast or broadcast destination address is dropped * An incoming SYN with a multicast or broadcast source address is always * dropped in tcp_set_destination, since IPDF_ALLOW_MCBC is not set in * The same logic in tcp_set_destination also serves to * reject an attempt to connect to a broadcast or multicast (destination) * Make sure we have a dce for the destination to avoid dce_ident * contention for connected sockets. /* Use conn_lock to satify ASSERT; tcp is already serialized */ * Note that up till now, acceptor always inherits receive * window from the listener. But if there is a metrics * associated with a host, we should use that instead of * inheriting it from listener. Thus we need to pass this * info back to the caller. * Use the metric option settings, iulp_tstamp_ok and * iulp_wscale_ok, only for active open. What this means * is that if the other side uses timestamp or window * scale option, TCP will also use those options. That * is for passive open. If the application sets a * large window, window scale is enabled regardless of * the value in iulp_wscale_ok. This is the behavior * since 2.6. So we keep it. * The only case left in passive open processing is the * For ECN, it should probably be like SACK. But the * current value is binary, so we treat it like the other * cases. The metric only controls active open.For passive * open, the ndd param, tcp_ecn_permitted, controls the * The if check means that the following can only * be turned on by the metrics only IRE, but not off. * As above, the if check means that SACK can only be * turned on by the metric only IRE. * XXX Note that currently, iulp_mtu can be as small as 68 * because of PMTUd. So tcp_mss may go to negative if combined * length of all those options exceeds 28 bytes. But because * of the tcp_mss_min check below, we may not have a problem if * tcp_mss_min is of a reasonable value. The default is 1 so * the negative problem still exists. And the check defeats PMTUd. * In fact, if PMTUd finds that the MSS should be smaller than * tcp_mss_min, TCP should turn off PMUTd and use the tcp_mss_min * We do not deal with that now. All those problems related to * PMTUd will be fixed later. /* Sanity check for MSS value. */ /* Note that this is the maximum MSS, excluding all options. */ * Update the tcp connection with LSO capability. * Initialize the ISS here now that we have the full connection ID. * The RFC 1948 method of initial sequence number generation requires * knowledge of the full connection ID before setting the ISS. * Make sure that conn is not marked incipient * for incoming connections. A blind * removal of incipient flag is cheaper than * tcp_clean_death / tcp_close_detached must not be called more than once * on a tcp. Thus every function that potentially calls tcp_clean_death * must check for the tcp state before calling tcp_clean_death. * Eg. tcp_input_data, tcp_eager_kill, tcp_clean_death_wrapper, * tcp_timer_handler, all check for the tcp state. * We are dying for some reason. Try to do it gracefully. (May be called * Return -1 if the structure was not cleaned up (if the cleanup had to be * done by a service procedure). * TBD - Should the return value distinguish between the tcp_t being * freed and it being reinitialized? * Its an eager that we are dealing with. We close the * eager but in case a conn_ind has already gone to the * listener, let tcp_accept_finish() send a discon_ind * to the listener and drop the last reference. If the * listener doesn't even know about the eager i.e. the * conn_ind hasn't gone up, blow away the eager and drop * the last reference as well. If the conn_ind has gone * up, state should be BOUND. tcp_accept_finish * will figure out that the connection has received a * RST and will send a DISCON_IND to the application. * The connection is dead. Decrement listener connection counter if * When a connection is moved to TIME_WAIT state, the connection * counter is already decremented. So no need to decrement here * again. See SET_TIME_WAIT() macro. /* Trash all inbound data */ * If we are at least part way open and there is error * (err==0 implies no error) * notify our client by a T_DISCON_IND. * Send M_FLUSH according to TPI. Because sockets will * (and must) ignore FLUSHR we do that only for TPI * endpoints and sockets in STREAMS mode. "tcp_clean_death: discon err %d",
err);
/* Direct socket, use upcall */ "tcp_clean_death, sending M_ERROR");
/* SYN_SENT or SYN_RCVD */ /* ESTABLISHED or CLOSE_WAIT */ * ESTABLISHED non-STREAMS eagers are not 'detached' because * an upper handle is obtained when the SYN-ACK comes in. So it * should receive the 'disconnected' upcall, but tcp_reinit should * not be called since this is an eager. * In case tcp is in the "lingering state" and waits for the SO_LINGER timeout * to expire, stop the wait and finish the close. * Need to cancel those timers which will not be used when * TCP is detached. This has to be done before the conn_wq * If delta is zero the timer event wasn't executed and was * successfully canceled. In this case we need to restart it * with the minimal delta possible. /* Signal closing thread that it can complete close */ /* If we have an upper handle (socket), release it */ * Mark the conn as closing. ipsq_pending_mp_add will not * add any mp to the pending mp list, after this conn has * tcp_closemp_used is used below without any protection of a lock * as we don't expect any one else to use it concurrently at this * point otherwise it would be a major defect. "connp %p tcp %p\n", (
void *)
connp, (
void *)
tcp);
* Cleanup any queued ioctls here. This must be done before the wq/rq * are re-written by tcp_close_output(). * As CONN_CLOSING is set, no further ioctls should be passed down to * IP for this conn (see the guards in tcp_ioctl, tcp_wput_ioctl and * tcp_wput_iocdata). If the ioctl was queued on an ipsq, * conn_ioctl_cleanup should have found it and removed it. If the ioctl * was still in flight at the time, we wait for it here. See comments * for CONN_INC_IOCTLREF in ip.h for details. * For non-STREAMS sockets, the normal case is that the conn makes * an upcall when it's finally closed, so there is no need to wait * in the protocol. But in case of SO_LINGER the thread sleeps here * so it can properly deal with the thread being interrupted. * The cv_wait_sig() was interrupted. We now do the * 1) If the endpoint was lingering, we allow this * to be interrupted by cancelling the linger timeout * 2) Revert to calling cv_wait() * We revert to using cv_wait() to avoid an * infinite loop which can occur if the calling * thread is higher priority than the squeue worker * thread and is bound to the same cpu. /* Entering squeue, bump ref count. */ * In the case of listener streams that have eagers in the q or q0 * we wait for the eagers to drop their reference to us. conn_rq and * conn_wq of the eagers point to our queues. By waiting for the * refcnt to drop to 1, we are sure that the eagers have cleaned * up their queue pointers and also dropped their references to us. * For non-STREAMS sockets we do not have to wait here; the * listener will instead make a su_closed upcall when the last * Called by tcp_close() routine via squeue when lingering is * interrupted by a signal. * Clean up the b_next and b_prev fields of every mblk pointed at by *mpp. * Some stream heads get upset if they see these later on as anything but NULL. * Clustering code serializes TCP disconnect callbacks and * cluster tcp list walks by blocking a TCP disconnect callback * if a cluster tcp list walk is in progress. This ensures * accurate accounting of TCPs in the cluster code even though * the TCP list walk itself is not atomic. * The tcp_t is going away. Remove it from all lists and set it * to TCPS_CLOSED. The freeing up of memory is deferred until * tcp_inactive. This is needed since a thread in tcp_rput might have * done a CONN_INC_REF on this structure before it was removed from the * This can be called via tcp_time_wait_processing() if TCP gets a * SYN with sequence number outside the TIME-WAIT connection's * window. So we need to check for TIME-WAIT state here as the * connection counter is already decremented. See SET_TIME_WAIT() * If we are an eager connection hanging off a listener that * hasn't formally accepted the connection yet, get off his * list and blow off any data that we have accumulated. * tcp_tconnind_started == B_TRUE means that the * conn_ind has already gone to listener. At * this point, eager will be closed but we * leave it in listeners eager list so that * if listener decides to close without doing * accept, we can clean this up. In tcp_tli_accept * we take care of the case of accept on closed * We don't want to have any pointers to the * listener queue, after we have released our * reference on the listener /* Stop all the timers */ /* Decrement listerner connection counter if necessary. */ * If the tcp_time_wait_collector (which runs outside the squeue) * is trying to remove this tcp from the time wait list, we will * block in tcp_time_wait_remove while trying to acquire the * tcp_time_wait_lock. The logic in tcp_time_wait_collector also * requires the ipcl_hash_remove to be ordered after the * tcp_time_wait_remove for the refcnt checks to work correctly. /* Need to probe before ixa_cleanup() is called */ * Mark the conn as CONDEMNED /* Release any SSL context */ * tcp is dying (called from ipcl_conn_destroy and error cases). * Free the tcp_t in either case. * Following is really a blowing away a union. * It happens to have exactly two members of identical size * the following code is enough. * If this is a non-STREAM socket still holding on to an upper * handle, release it. As a result of fallback we might also see * STREAMS based conns with upper handles, in which case there is * nothing to do other than clearing the field. * tcp_get_conn is used to get a clean tcp connection structure. * It tries to reuse the connections put on the freelist by the * time_wait_collector failing which it goes to kmem_cache. This * way has two benefits compared to just allocating from and * 1) The time_wait_collector can free (which includes the cleanup) * outside the squeue. So when the interrupt comes, we have a clean * connection sitting in the freelist. Obviously, this buys us * 2) Defence against DOS attack. Allocating a tcp/conn in tcp_input_listener * has multiple disadvantages - tying up the squeue during alloc. * But allocating the conn/tcp in IP land is also not the best since * we can't check the 'q' and 'q0' which are protected by squeue and * blindly allocate memory which might have to be freed here if we are * not allowed to accept the connection. By using the freelist and * putting the conn/tcp back in freelist, we don't pay a penalty for * allocating memory without checking 'q/q0' and freeing it if we can't * Care should be taken to put the conn back in the same squeue's freelist * from which it was allocated. Best results are obtained if conn is * allocated from listener's squeue and freed to the same. Time wait * collector will free up the freelist is the connection ends up sitting * Pre-allocate the tcp_rsrv_mp. This mblk will not be freed until * Register tcp_notify to listen to capability changes detected by IP. * This upcall is made in the context of the call to conn_ip_output * thus it is inside the squeue. * Handle connect to IPv4 destinations, including connections for AF_INET6 * sockets connecting to IPv4 mapped IPv6 destinations. * Returns zero if OK, a positive errno, or a negative TLI error. /* Check for attempt to connect to INADDR_ANY */ * SunOS 4.x and 4.3 BSD allow an application * to connect a TCP socket to INADDR_ANY. * When they do this, the kernel picks the * address of one interface and uses it * instead. The kernel usually ends up * picking the address of the loopback * interface. This is an undocumented feature. * However, we provide the same thing here * in order to have source and binary * compatibility with SunOS 4.x. * Update the T_CONN_REQ (sin/sin6) since it is used to * generate the T_CONN_CON. /* Handle __sin6_src_id if socket not bound to an IP address */ * At this point the remote destination address and remote port fields * in the tcp-four-tuple have been filled in the tcp structure. Now we * have to see which state tcp was in so we can take appropriate action. * We support a quick connect capability here, allowing * clients to transition directly from IDLE to SYN_SENT * tcp_bindi will pick an unused port, insert the connection * in the bind hash and transition to BOUND state. * Lookup the route to determine a source address and the uinfo. * Don't let an endpoint connect to itself. * Handle connect to IPv6 destinations. * Returns zero if OK, a positive errno, or a negative TLI error. * If we're here, it means that the destination address is a native * IPv6 address. Return an error if conn_ipversion is not IPv6. A * reason why it might not be IPv6 is if the socket was bound to an * IPv4-mapped IPv6 address. * Interpret a zero destination to mean loopback. * Update the T_CONN_REQ (sin/sin6) since it is used to * generate the T_CONN_CON. /* Handle __sin6_src_id if socket not bound to an IP address */ * Take care of the scope_id now. * At this point the remote destination address and remote port fields * in the tcp-four-tuple have been filled in the tcp structure. Now we * have to see which state tcp was in so we can take appropriate action. * We support a quick connect capability here, allowing * clients to transition directly from IDLE to SYN_SENT * tcp_bindi will pick an unused port, insert the connection * in the bind hash and transition to BOUND state. * Lookup the route to determine a source address and the uinfo. * Don't let an endpoint connect to itself. * Note that unlike other functions this returns a positive tli error * when it fails; it never returns an errno. * Right now, upper modules pass down a T_DISCON_REQ to TCP, * when the stream is in BOUND state. Do not send a reset, * since the destination IP address is not valid, and it can * be the initialized value of all zeros (broadcast address). * According to TPI, for non-listeners, ignore seqnum * Following interpretation of -1 seqnum is historical * and implied TPI ? (TPI only states that for T_CONN_IND, * a valid seqnum should not be -1). * -1 means disconnect everything * regardless even on a listener. * The connection can't be on the tcp_time_wait_head list * since it is not detached. * If it used to be a listener, check to make sure no one else * has taken the port before switching back to LISTEN state. /* Allow conn_bound_if listeners? */ * If this end point is not going to become a listener, * decrement the listener connection count if * necessary. Note that we do not do this if it is * going to be a listner (the above if case) since * then it may remove the counter struct. * Our client hereby directs us to reject the connection request * that tcp_input_listener() marked with 'seqnum'. Rejection consists * of sending the appropriate RST, not an ICMP error. /* Send M_FLUSH according to TPI */ * Handle reinitialization of a tcp structure. * Maintain "binding state" resetting the state to BOUND, LISTEN, or IDLE. /* tcp_reinit should never be called for detached tcp_t's */ /* Cancel outstanding timers */ * Reset everything in the state vector, after updating global * MIB data from instance counters. * Following is a union with two members which are * identical types and size so the following cleanup * The connection can't be on the tcp_time_wait_head list * since it is not detached. /* Don't reset if the initialized by bind. */ /* Note that ixa_cred gets cleared in ixa_cleanup */ * This is the case when a TLI program uses the same * transport end point to accept a connection. This * makes the TCP both a listener and acceptor. When * this connection is closed, we need to set the state * back to TCPS_LISTEN. Make sure that the eager list * Note that this stream is still bound to the four * tuples of the previous connection in IP. If a new * SYN with different foreign address comes in, IP will * not find it and will send it to the global queue. In * the global queue, TCP will do a tcp_lookup_listener() * to find this stream. This works because this stream * is only removed from connected hash. * Initially set conn_recv to tcp_input_listener_unbound to try * to pick a good squeue for the listener when the first SYN * arrives. tcp_input_listener_unbound sets it to * tcp_input_listener on that first SYN. * Initialize to default values * Force values to zero that need be zero. * Do not touch values asociated with the BOUND or LISTEN state * since the connection will end up in that state after the reinit. * NOTE: tcp_reinit_values MUST have a line for each field in the tcp_t /* Should be ASSERT NULL on these with new code! */ /* We clear any IP_OPTIONS and extension headers */ /* Remove any remnants of mapped address binding */ /* Reset fusion-related fields */ * Initialize the various fields in tcp_t. If parent (the listener) is non * NULL, certain values will be inheritted from it. * Default value of tcp_init_cwnd is 0, so no need to set here * if parent is NULL. But we need to inherit it from parent. /* Inherit various TCP parameters from the parent. */ * Initialize tcp_rtt_sa and tcp_rtt_sd so that the calculated RTO * will be close to tcp_rexmit_interval_initial. By doing this, we * allow the algorithm to adjust slowly to large fluctuations of RTT * during first few transmissions of a connection as seen in slow /* NOTE: ISS is now set in tcp_set_destination(). */ /* Reset fusion-related fields */ * Init the window scale to the max so tcp_rwnd_set() won't pare * down tcp_rwnd. tcp_set_destination() will set the right value later. * Init the tcp_debug option if it wasn't already set. This value * calls strlog() to print out debug messages. Doing this * initialization here means that this value is not inherited thru * Update the TCP connection according to change of PMTU. * Path MTU might have changed by either increase or decrease, so need to * adjust the MSS based on the value of ixa_pmtu. No need to handle tiny * or negative MSS, since tcp_mss_set() will do it. * Always call ip_get_pmtu() to make sure that IP has updated * Calculate the MSS by decreasing the PMTU by conn_ht_iphc_len and * IPsec overhead if applied. Make sure to use the most recent * Nothing to change, so just return. * Currently, for ICMP errors, only PMTU decrease is handled. * Update ixa_fragsize and ixa_pmtu. * Adjust MSS and all relevant variables. * If the PMTU is below the min size maintained by IP, then ip_get_pmtu * has set IXAF_PMTU_TOO_SMALL and cleared IXAF_PMTU_IPV4_DF. Since TCP * has a (potentially different) min size we do the same. Make sure to * clear IXAF_DONTFRAG, which is used by IP to decide whether to * LSO over IPv6 can not be fragmented. So need to disable LSO * when IPv6 fragmentation is needed. * Set the sd_qn_maxpsz according to the socket send buffer * size, and sd_maxblk to INFPSZ (-1). This will essentially * instruct the stream head to copyin user data into contiguous * kernel-allocated buffers without breaking it up into smaller * chunks. We round up the buffer size to the nearest SMSS. * Set sd_qn_maxpsz to approx half the (receivers) buffer * (and a multiple of the mss). This instructs the stream * head to break down larger than SMSS writes into SMSS- * size mblks, up to tcp_maxpsz_multiplier mblks at a time. /* Round up to nearest mss */ /* For /dev/tcp aka AF_INET open */ /* For /dev/tcp6 aka AF_INET6 open */ * Find the proper zoneid and netstack. * Special case for install: miniroot needs to be able to * access files via NFS as though it were always in the * For exclusive stacks we set the zoneid to zero * to make TCP operate as if in the global zone. * Both tcp_get_conn and netstack_find_by_cred incremented refcnt, * Besides asking IP to set the checksum for us, have conn_ip_output * to do the following checks when necessary: * IXAF_VERIFY_SOURCE: drop packets when our outer source goes invalid * IXAF_VERIFY_PMTU: verify PMTU changes * IXAF_VERIFY_LSO: verify LSO capability changes /* Cache things in the ixa without any refhold */ /* conn_allzones can not be set this early, hence no IPCL_ZONEID */ * If the caller has the process-wide flag set, then default to MAC * exempt mode. This allows read-down to unlabeled hosts. /* DTrace ignores this - it isn't a tcp:::state-change */ * Either minor numbers in the large arena were exhausted * or a non socket application is doing the open. * Try to allocate from the small arena. * Non streams socket needs a stream to fallback to * the conn_dev and minor_arena will be subsequently used by * tcp_tli_accept() and tcp_tpi_close_accept() to figure out * the minor device number for this connection from the q_ptr. * Put the ref for TCP. Ref for IP was already put * by ipcl_conn_create. Also Make the conn_t globally * Build/update the tcp header template (in conn_ht_iphc) based on * conn_xmit_ipp. The headers include ip6_t, any extension * headers, and the maximum size tcp header (to avoid reallocation * on the fly for additional tcp options). * Assumes the caller has already set conn_{faddr,laddr,fport,lport,flowinfo}. * Returns failure if can't allocate memory. * We might be called after the connection is set up, and we might * have TS options already in the TCP header. Thus we save any /* Grab lock to satisfy ASSERT; TCP is serialized using squeue */ * Any routing header/option has been massaged. The checksum difference * is stored in conn_sum for later use. /* restore any old tcp header */ * IP wants our header length in the checksum field to * allow it to perform a single pseudo-header+checksum * calculation on behalf of TCP. * Include the adjustment for a source route once IP_OPTIONS is set. * tcp_rwnd_set() is called to adjust the receive window to a desired value. * We do not allow the receive window to shrink. After setting rwnd, * set the flow control hiwat of the stream. * This function is called in 2 cases: * 1) Before data transfer begins, in tcp_input_listener() for accepting a * connection (passive open) and in tcp_input_data() for active connect. * This is called after tcp_mss_set() when the desired MSS value is known. * This makes sure that our window size is a mutiple of the other side's * 2) Handling SO_RCVBUF option. * It is ASSUMED that the requested size is a multiple of the current MSS. * XXX - Should allow a lower rwnd than tcp_recv_hiwat_minmss * mss if the * Insist on a receive window that is at least * tcp_recv_hiwat_minmss * MSS (default 4 * MSS) to avoid * funny TCP interactions of Nagle algorithm, SWS avoidance * and delayed acknowledgement. /* Caller could have changed tcp_rwnd; update tha_win */ * In the fusion case, the maxpsz stream head value of * our peer is set according to its send buffer size * and our receive buffer size; since the latter may * have changed we need to update the peer's maxpsz. * If window size info has already been exchanged, TCP should not * shrink the window. Shrinking window is doable if done carefully. * We may add that support later. But so far there is not a real /* MSS may have changed, do a round up again. */ * tcp_rcv_ws starts with TCP_MAX_WINSHIFT so the following check * can be applied even before the window scale option is decided. * If we're over the limit we may have to back down tcp_rwnd. * The increment below won't work for us. So we set all three * here and the increment below will have no effect. * For a remote host on a different subnet (through a router), * we ack every other packet to be conforming to RFC1122. * tcp_deferred_acks_max is default to 2. * Increment the current rwnd by the amount the maximum grew (we * can not overwrite it since we might be in the middle of a /* Are we already connected? */ * Need to clean up all the eagers since after the unbind, segments * will no longer be delivered to this listener stream. /* Clean up the listener connection counter if necessary. */ * Collect protocol properties to send to the upper handle. * Determine what write offset value to use depending on SACK and * whether the endpoint is fused or not. * For fused tcp loopback, set the stream head's write * offset value to zero since we won't be needing any room * for TCP/IP headers. This would also improve performance * since it would reduce the amount of work done by kmem. * Non-fused tcp loopback case is handled separately below. * Update the peer's transmit parameters according to * our recently calculated high water mark value. * If this is endpoint is handling SSL, then reserve extra * offset and space at the end. * Also have the stream head allocate SSL3_MAX_RECORD_LEN packets, * overriding the previous setting. The extra cost of signing and * encrypting multiple MSS-size records (12 of them with Ethernet), * instead of a single contiguous one by the stream head * largely outweighs the statistical reduction of ACKs, when * applicable. The peer will also save on decryption and verification * Check the usability of ZEROCOPY. It's instead checking the flag set by IP. * Backoff from a zero-copy message by copying data to a new allocated * message and freeing the original desballoca'ed segmapped message. * This function is called by following two callers: * 1. tcp_timer: fix_xmitlist is set to B_TRUE, because it's safe to free * the origial desballoca'ed message and notify sockfs. This is in re- * 2. tcp_output: fix_xmitlist is set to B_FALSE. Flag STRUIO_ZCNOTIFY need * to be copied to new message. * Copy saved information and adjust tcp_xmit_tail /* Free the original message. */ * Update the TCP connection according to change of LSO capability. * We check against IPv4 header length to preserve the old behavior * of only enabling LSO when there are no IP options. * But this restriction might not be necessary at all. Before removing * it, need to verify how LSO is handled for source routing case, with * which IP does software checksum. * For IPv6, whenever any extension header is needed, LSO is supressed. * Either the LSO capability newly became usable, or it has changed. * If LSO to be enabled, notify the STREAM header with larger }
else {
/* LSO capability is not usable any more. */ * If LSO to be disabled, notify the STREAM header with smaller * data block. And need to restore fragsize to PMTU. * Update the TCP connection according to change of ZEROCOPY capability. * Notify function registered with ip_xmit_attr_t. It's called in the squeue * so it's safe to update the TCP connection. * The TCP write service routine should never be called... * Hash list lookup routine for tcp_t structures. * Returns with a CONN_INC_REF tcp structure. Caller must do a CONN_DEC_REF. * Hash list insertion routine for tcp_t structures. * Hash list removal routine for tcp_t structures. * Extract the lock pointer in case there are concurrent * hash_remove's for this instance. * Type three generator adapted from the random() function in 4.4 BSD: * Copyright (c) 1983, 1993 * The Regents of the University of California. All rights reserved. * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. All advertising materials mentioning features or use of this software * must display the following acknowledgement: * This product includes software developed by the University of * California, Berkeley and its contributors. * 4. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF /* Type 3 -- x**31 + x**3 + 1 */ /* Protected by tcp_random_lock */ * Use high-res timer and current time for seed. Gethrtime() returns * a longlong, which may contain resolution down to nanoseconds. * The current time will either be a 32-bit or a 64-bit quantity. * XOR the two together in a 64-bit result variable. * Convert the result to a 32-bit value by multiplying the high-order * 32-bits by the low-order 32-bits. for (i =
1; i <
DEG_3; i++)
for (i = 0; i <
10 *
DEG_3; i++)
* tcp_random: Return a random number in the range [1 - (128K + 1)]. * This range is selected to be approximately centered on TCP_ISS / 2, * and easy to compute. We get this value by generating a 32-bit random * number, selecting out the high-order 17 bits, and then adding one so * that we never return zero. * The high-order bits are more random than the low-order bits, * so we select out the high-order 17 bits and add one so that * Split this function out so that if the secret changes, I'm okay. * Initialize the tcp_iss_cookie and tcp_iss_key. * Start with the current absolute time. * XXX - Need a more random number per RFC 1750, not this crap. * OTOH, if what follows is pretty random, then I'm in better shape. * The cpu_type_info is pretty non-random. Ugggh. It does serve * The pass-phrase. Normally this is supplied by user-called NDD. * See 4010593 if this section becomes a problem again, * but the local ethernet address is useful here. * Hash 'em all together. The MD5Final is called per-connection. * Called by IP when IP is loaded into the kernel /* Initialize the random number generator */ /* A single callback independently of how many netstacks we have */ * We want to be informed each time a stack is created or * destroyed in the kernel, so we can maintain the * Initialize the TCP stack instance. /* TCP's IPsec code calls the packet dropper. */ * Note: To really walk the device tree you need the devinfo * pointer to your device which is only available after probe/attach. * The following is safe only because it uses ddi_root_node() * Initialize RFC 1948 secret values. This will probably be reset once * Use NULL name, as the name is caught by the new lockstats. * Initialize with some random, non-guessable string, like the global * ncpus is the current number of CPUs, which can be bigger than * boot_ncpus. But we don't want to use ncpus to allocate all the * tcp_stats_cpu_t at system boot up time since it will be 1. While * we handle adding CPU in tcp_cpu_update(), it will be slow if * there are many CPUs as we will be adding them 1 by 1. * Note that tcps_sc_cnt never decreases and the tcps_sc[x] pointers * are not freed until the stack is going away. So there is no need * to grab a lock to access the per CPU tcps_sc[x] pointer. * Called when the IP module is about to be unloaded. * Free the TCP stack instance. * Set tcps_reclaim to false tells tcp_reclaim_timer() not to restart * Generate ISS, taking into account NDD changes may happen halfway through. * (If the iss is not zero, set it.) * Now that we've hashed into a unique per-connection sequence * space, add a random increment per strong_iss == 1. So I * tcp_{set,clr}qfull() functions are used to either set or clear QFULL * on the specified backing STREAMS q. Note, the caller may make the * decision to call based on the tcp_t.tcp_flow_stopped value which * when check outside the q's lock is only an advisory check ... * This is called once for each squeue - globally for all stack * Limit number of entries to 1% of availble memory / tcp_ncpus * Return unix error is tli error is TSYSERR, otherwise return a negative * If the return value from this function is positive, it's a UNIX error. * Otherwise, if it's negative, then the absolute value is a TLI error. * the TPI routine tcp_tpi_connect() is a wrapper function for this. * If we're connecting to an IPv4-mapped IPv6 address, we need to * make sure that the conn_ipversion is IPV4_VERSION. We * need to this before we call tcp_bindi() so that the port lookup * code will look for ports in the correct port space (IPv4 and * IPv6 have separate port spaces). * Listening sockets are not allowed to issue connect(). * We support quick connect, refer to comments in * We update our cred/cpid based on the caller of connect /* Cache things in the ixa without any refhold */ /* We need to restart with a label based on the cred */ * Destination adress is mapped IPv6 address. * Source bound address should be unspecified or * IPv6 mapped address as well. * tcp_set_destination() does not adjust for TCP/IP header length. * Just make sure our rwnd is at least rcvbuf * MSS large, and round up * We do the round up here because we need to get the interface MTU * first before we can do the round up. * Set tcp_snd_ts_ok to true * so that tcp_xmit_mp will * option in the SYN segment. * Note that tcp_snd_sack_ok can be set in tcp_set_destination() if * the SACK metric is set. So here we just check the per stack SACK * Should we use ECN? Note that the current * default value (SunOS 5.9) of tcp_ecn_permitted * is 1. The reason for doing this is that there * are equipments out there that will drop ECN * enabled IP packets. Setting it to 1 avoids * compatibility problems. /* Trace change from BOUND -> SYN_SENT here */ * We must bump the generation before sending the syn * to ensure that we use the right generation in case * this thread issues a "connected" up call. * DTrace sending the first SYN as a * tcp:::connect-request event. /* All Solaris components should pass a cred for this operation. */ * Handle listen() increasing backlog. * This is more "liberal" then what the TPI spec * requires but is needed to avoid a t_unbind * when handling listen() since the port number * might be "stolen" between the unbind and bind. /* Do an implicit bind: Request for a generic port. */ /* Fall through and do the fanout insertion */ * If this is a listener, do not reset the eager list * and other stuffs. Note that we don't check if the * existing eager list meets the new tcp_conn_req_max /* Initialize the chain. Don't need the eager_lock */ * We need to make sure that the conn_recv is set to a non-null * value before we insert the conn into the classifier table. * This is to avoid a race with an incoming packet which does an * We initially set it to tcp_input_listener_unbound to try to * pick a good squeue for the listener when the first SYN arrives. * tcp_input_listener_unbound sets it to tcp_input_listener on that /* Insert the listener in the classifier table */ /* Undo the bind - release the port number */ * If there is a connection limit, allocate and initialize * the counter struct. Note that since listen can be called * multiple times, the struct may have been allready allocated. * Calculate the connection limit based on * the configured ratio and maxusers. Maxusers * are calculated based on memory size, * ~ 1 user per MB. Note that the conn_rcvbuf * and conn_sndbuf may change after a * connection is accepted. So what we have * is only an approximation. /* At least we should allow two connections! */