Beruflich Dokumente
Kultur Dokumente
4: Transport
Socket layer
3: Network
2: Data
Protocol Layer (TCP / IP)
Link
1: Physical
Interface Layer (Ethernet, etc.) Layer
TCP/IP Stack Overview
Process
Socket Layer
3: ip_input ( ... )
3: ip_output ( . )
4: ethernet_output ( . ) 2: ethernet_input ( .. )
Physical Media
send (int socket, const char *buf, int length, int flags)
Process
Kernel sendto (int socket, const char *data_buffer, int length, int flags, struct sockaddr *destination, int destination _length)
sendit (struct proc *p, int socket, struct msghdr *mp, int flags, int *return_size) uipc_syscalls.c
sosend (struct socket *s, struct mbuf *addr, struct uio *uio, struct mbuf *top, struct mbuf *control, int flags ) uipc_socket.c
tcp_userreq (struct socket *s, int request, struct mbuf *m, struct mbuf * nam, struct mbuf * control ) tcp_userreq.c
sendto (int socket, const char *data_buffer, int length, int flags, struct sockaddr *destination, int destination _length)
MBUF Chain
m_next m_next = NULL
m_pkthdr.len = 150
128 Bytes
mBuf m_pkthdr.recvif =NULL
50 Bytes Data
150 Bytes
Data 100 Bytes
Data
58 Bytes Unused Space
Socket Layer -sosend passes data and control information to the protocol layer
sosend(struct socket *s, struct mbuf *addr, struct uio *uio, struct mbuf *data_buffer, struct mbuf *control, int flags )
no
Is there enough space
in the buffer
sbspace(s->sb_snd)
yes
Convert Socket to
Internet Protocol Control Block
inp = sotoinpcb(so)
request
PRU_SEND
return error
int error = tcp_output(tp) to tcp_userreq( )
TCP Layer (tcp_output.c) - tcp_output(struct tcpcb *tp)
Called by tcp_usrreq for one of the following reasons:
To send the initial SYN
To send a finished_sending message
To send data
To send a window update after data has been received.
tcp_ouput ( ) functionality:
1. determines whether TCP can send a segment or not depending on:
flags in the data sent by the socket layer to send an ACK, etc.
Size of window advertised by the receivers end.
Amount of data ready to send
whether unacknowledged data already exists for the connection
4. Send a segment
Allocate a buffer for the TCP and IP header from the header template
Copy the TCP and IP header template into the the buffer to be sent.
Fill the fields in the TCP header.
Decrement the number of buffers to tbe sent, so that the end can be checked.
Set sequencenumber and acknowledgement field.
Set three fields in the IP header - IP length, TTL and Tos.
Pass the datagram to IP
TCP Layer (tcp_output.c) - tcp_output(struct tcpcb *tp)
false
true
tp -> t_flags & Send acknowledgement
TF_ACKNOW
false
true
tp -> t_flags & Send sequence number
TF_SYN || TH_RST or reset
false
true
tp -> t_flags & Finished sending
TH_FIN
false
Ckeck flags to determine the type of message:
window probe
retransmission
normal data transmission
Allocate an mbuf for the TCP & IP header and data if possible.
MGETHDR ( m, M_DONTWAIT, MT_HEADR)
M_DONTWAIT indicates that if memory is not available for
mbuf then come out of the routine and return an error state.
yes
Copy the data from the socket send buffer into the
new packet header mbuf
1. Header initialization
Packets Check if there were any errors while adding headers in higher
yes ERROR layers. Most of the fields of the IP header are pre defined by
damaged?
higher layer protocols.
no
The value of flags decides whats to be done with the data
IP_FORWARDING : Forward packet
if ((flags == IP_FORWARDING ) ||
IP_ROUTETOIF : Route directly to Interface
(flags == IP_RAWOUTPUT ))
IP_ALLOWBROADCAST : Allow broadcasting of packet
yes IP_RAWOUTPUT : Packet contains pre-constructed header
Find the interface on which the If the cached route is provided, find the interface on
no
packet has to be placed. Ifp points to which the frame has to be sent.
the interfaces ifnet structure.
yes
yes
Does the packet have Fragment the packet if its size is Larger packets (packets that exceed the MTU) must
to be fragmented ? greater than the MTU. be fragmented before they can be sent.
no
1. Verification
no
Ethernet port senderr (ENETDOWN)
up and running ?
ifp -> if_flags &
(IF_UP | IF_RUNNING )
yes
Interface Layer(if_ethersubr.c) - ether_output(struct ifnet *ifp, struct mbuf *mbuf,
struct sockaddr *destination, struct rtentry *rt_entry)
Function: Takes the data portion of an Ethernet frame ans encapsulates it with a 14-byte header and places it on the interface send_queue.
Phases: Verification, Protocol-Specific Processing, Frame Construction, Interface Queuing.
Arguments -
ifp points to outgoing interfaces ifnet structure
mbuf is the data to be sent
destination is the destination address
rt_entry points o the routing entry
Initialize-
Ethernet header - struct eth_header *eh
Verification
no
Ethernet port senderr (ENETDOWN)
up and running ?
ifp -> if_flags &
(IF_UP | IF_RUNNING )
yes
0
Route valid ?
rt_entry = rtalloc1 (destination, 1) senderr (EHOSTUNREACH)
0
Next hop a gateway ?
rt = rt -> rt_gwroute
Destination responding
to ARP requests? no
If not then do not send more
packets to avoid flooding.
rt -> rt_flags &
RTF_REJECT
Verification
AF_INET
Frame Preparartion
Protocol Specific Processing
Frame Preparartion
Make sure there is room for the 14 byte
ethernet header
M_PREPEND ( m, sizeof(ethernet_header),
M_DONOTWAIT)
Interface Queuing
yes Discard the frame
Free the memory buff
Is the output queue full
senderr ( ENOBUFS )
no
Function: Dequeues frames from the interface output queue and arranges for them to be transmitted by the Ethernet Card.
0
le -> sc_if.if_flags &
return error
IFF_RUNNING