hufs MCLAB C hapter 22 Protocol Control Blocks HUFS ICE 김 영 준 TCP/IP Illustrated, Volume 2
hufs MCLAB Contents Introduction Code Introduction inpcb{} Structure in_pcballoc( ) and in_pcbdetach( ) Functions Binding, Connecting, and Demultiplexing in_pcblookup( ) Function in_pcbbind( ) Function in_pcbconnect( ) Function in_pcbdisconnect( ) Function in_setsockaddr( ) and in_setpeeraddr( ) Functions in_pcbnotify( ), in_rtchange( ), and in_losing( ) Functions Summary
hufs MCLAB Introduction Protocol Control Block 은 UDP, TCP socket 에 필요한 정보 들을 저장 Internet Protocol 은 Internet protocol control block 과 TCP control block 을 유지 UDP 는 connectionless 이기 때문에 유지 X (UDP control block X) Foreign, Local IP address and port, IP header proto type, IP option, routing table entry, TCP control block state information
hufs MCLAB File Type DTYPE_SOCKET Socket Type SOCK_DGRAM SOCK_STREAM Protocol layer called inpcb{} 를 만들어서 socket{} 연결 TCP tcpcb{} 생성 inp_ppcb 와 t_inpcb 연결 UDP null pointer Socket pair 들로부터 ip addr, port 입력 Internet PCB 는 double linked list Head 는 gloval inpcb structure (udb, tcp) 3 개의 member, lport 는 후에 단명포트로.. Internet PCB 는 transport layer data structure Used by TCP, UDP, raw IP Used by TCP, UDP, raw IP Not by IP, ICMP, IGMP Not by IP, ICMP, IGMP
hufs MCLAB Code Introduction Source Global Variables Kernel’s malloc function 에 의해서 PCBs 할당 Type of M_BUF, socket structure 는 M_SOCKET
hufs MCLAB inpcb Structure struct route inp_route Foreign address 로 가는 route 의 주소를 inp_route entry 에 저장 Double Linked List Head 를 가리키는 pointer UDP list udp, TCP list tcp Network Byte Order Transport Layer 마다 port number 저장 X TCP 의 경우 tcpcb 에 속해있다 User output 의 경우 kernel start, Socket layer 에 대응하는 Internet PCB 연결 Processing a received IP datagram 의 경우 Kernel start, PCB 에 대응하는 Socket Structure 와 연결
hufs MCLAB inp_flags member struct ip inp_ip Copy of IP header, but only used by TOS and TTL TOS is initialized to 0 TTL is initialized by transport layer(default TTL to 64) IP_TOS or IP_TTL socket option 사용으로 변경하면 새로운 값은 inpcb.inp_ip struct 에 저장 TCP, UDP 는 이 structure 를 사용하고, prototype IP header 처럼 IP datagram 을 전송할 때 사용한다.
hufs MCLAB struct mbuf *inp_options IP_OPTIONS socket option 은 출발지 route 를 설정할 때 사용 IP_OPTIONS socket option 을 사용해서 process 는 datagram 을 outgoing ip_pcbopts() 에 의해서 mbuf 안에 option 의 copy 가 저장, pointer 는 mbuf 를 가리킨다. struct ip_moptions *inp_moptions User’s IP multicast option 에 대한 pointer
hufs MCLAB in_pcballoc( ) and in_pcbdetach( ) Functions Internet PCB is allocated by TCP, UDP, raw IP when a socket is created. PRU_ATTACH request 은 socket system call 에 의해 나타남. Allocate PCB and initialize to zero PCB 안에서 IP, port number 가 0 로 initialize 되야 하므로.. Link Structures together insque(inp, head); 새로운 PCB inp 를 double linked list
hufs MCLAB socket structure 안의 PCB pointer 를 0 으로 set sofree(so) struct socket release If mbuf 에 IP options 이 있으면 release by m_free() If route is held by this PCB, release by rtfree() multicast options are also release by ip_freemoptions() Double linked list 에서 PCB 제거 Kernel 에 할당된 memory 를 FREE()
hufs MCLAB Binding, Connecting, and Demultiplexing Binding of Local IP Address and Port Number PCB 에 local port 가 존재하기 때문에 EADDRINUSE error 만약 포트를 사용한다면.. SO_REUSEADDR SO_REUSEPORT Connecting a UDP Socket TCP client 뿐만 아니라, UDP client, server 도 가능 Socket 을 특정한 peer 만 UDP datagram 을 교환하도록 제한
hufs MCLAB Demultiplexing of Received IP Datagrams by TCP TCP 는 in_pcblookup() 을 통해서 Internet PCB 의 list 에서 matching 되는 port 를 찾아서 segment 를 receive Match 0, 1 (local IP or foreign IP), 2 (both local IP and foreign IP) 이때 wildcard match 를 한다. Wildcard match 최소값을 match Incoming segment from { :1500} to { :23} Incoming segment from { :1501} to { :23} Match ??
hufs MCLAB Demultiplexing of Received IP Datagrams by UDP 1. Broadcast, Multicast IP address 로 들어오는 UDP datagram 에 대 해서 모두 socket 에 matching 한다. 2. Unicast IP address 로 들어오는 UDP datagram 에 대해서는 오직 하나의 socket 에 matching 한다. 이때 최소값의 wildcard match 를 적용 Received datagram from { :1500} to { :577} Received datagram from { :1500} to { :577}
hufs MCLAB in_pcblookup( ) Function The function in_pcblookup serves four different purposes 1. TCP or UDP 가 IP datagram 을 받을 때, in_pcblookup() 은 Internet PCBs 의 protocol’s list 를 scan, PCB 에 matching 되는 datagram 을 receive. Transport layer demultiplexing of received datagram 2. Process 가 bind() system call 에 의해서 실행될 때, local IP addr, local port 를 할당, 이때 in_pcbbind 는 protocol 에 의해 call 하고, 요 청된 local addr pair 가 이미 할당되는지를 확인 3. Process 가 bind() system call 에 의해서 실행될 때, 요청한 ephemeral port 가 socket 에 할당, kernel 은 in_pcbbind 에 의해 사용 가능한 port 인지 확인, 만약 사용하고 있다면 next ephemeral port 를 할당, 할당될 때 까지 4. Process 가 connect() system call 에 의해서 실행될 때, in_pcbbind() 는 요청된 socket pair 가 unique 한지 확인 in_pcbbind() call in_pcblookup
hufs MCLAB Two options confuse the logic of the function 1. SO_REUSEADDR or SO_REUSEPORT socket option 은 중복된 local address 를 허용 2. Wildcard match OK 는 socket 이 다른 local interface 에 도착하는 UDP datagram 을 accept 하는 것을 의미하고, Wildcard match 를 금하는 경우는 foreign IP address and port number 로 connecting 했을 때를 말함
hufs MCLAB Compare local port number Compare local address Compare foreign address Compare foreign port number 만약 Wildcard 를 사용하지 않으면 loop Remember best match return if exact macth found
hufs MCLAB Example – Demultiplexing of Received TCP Segment laddr= , lport=23, faddr = , fport= First loopWildcard is 1(foreign IP)matchwild 1 2. Second loopWildcard is 2(foreign IP,local IP)matchwild 1 3. Third loopWildcard is 0matchwild 0 Break;
hufs MCLAB in_pcbbind( ) Function Bind a local address and port number to a socket. 1. From bind for a TCP socket (normally to bind a server’s well-known port) 2. From bind for UDP socket (either to bind server’s well-known port or to bind an ephemeral port to a client’s socket) 3. From connect for a TCP socket, if the socket has not yet been bound to a nonzero port (this is typical for TCP clients) 4. From listen for a TCP socket, if the socket has not yet been bound to a nonzero port (this is rare, since listen is called by a TCP server, which normally binds a well-known port, not an ephemeral port) 5. From in_pcbconnect, if the local IP address and local port number have not been set (typical for a call to connect for a UDP socket or for each call to sendto for an unconnected UDP socket) Implicit bind Explicit bind
hufs MCLAB IP addr 에 assign 되었는지 확인 Wildcard 사용 여부를 확인 nam 이 NULL 이면 implicit bind Non NULL 이면 explicit bind
hufs MCLAB Multicast 라면 REUSE 설정 (port) !INADDR_ANY 인데 interface 가 0 면 error Port 존재하면 Global, all zero addr 로 주소 결정 t = 0 일 때 원하는 addr 를 찾은 경우 사용 가능한 port 결정
hufs MCLAB in_pcbconnect( ) Function in_pcbconnect specifies the foreign IP address and port number for a socket. 1. From connect for a TCP socket (required for a TCP client) 2. From connect for a UDP socket (optional for a UDP client, rare for a UDP server) 3. From sendto when a datagram is output on an unconnected UDP socket (common) 4. From tcp_input when a connection request (SYN segment) arrives on a TCP socket that is in the LISTEN state (standard for a TCP server)
hufs MCLAB Argument 에 대한 check Global, Primary IP addr (wildcard) 인지 broadcast 구분
hufs MCLAB Route release Route 가 있으면 Route 와 addr 가 다르면 DONTROUTE set 이면 DONTROUTE 가 없고 route 경로가 없으면 ROUTE 경로 설정 Outgoing interface 결정 loopback 이 아니면 interface addr 현재 지정된 addr
hufs MCLAB Multicast 일 경우 Mbuf 의 moptions 가져오고, multicast addr 을 addr 로 저장 addr 를 route addr 로 저장 socket pair 가 unique 한지 check Implicit bind Broadcast 주소 사용가능한 addr 없다는 error unique 하지 않으면 error ephemeral port
hufs MCLAB in_pcbdisconnect( ) Function Remove foreign association Foreign IP address to all 0s (INADDR_ANY) Foreign port number to 0 Unconnected UDP socket 에서 datagram 전송 시 Process sendto( ) in_pcbconnect( ) udp_output( ) datagram send in_pcbdisconnect( ) Connected UDP socket 에서 connect 가 call 했을 때 PCB 를 재사용 하기 위해서 PCB 가 file table 을 더 이상 참조하지 않을 때 (SS_NOFDREF is set) in_pcbdetach( ) release the PCB
hufs MCLAB in_setsockaddr( ) and in_setpeeraddr( ) Functions getsockname system call Local protocol address of a socket PRU_SOCKADDR request Call in_setsockaddr( ) getpeername system call Foreign protocol address PRU_PEERADDR request Call in_setpeeraddr( )
hufs MCLAB in_pcbnotify( ), in_rtchange( ), and in_losing( ) Functions Processing of ICMP errors Route 경로를 재설정, All IP datagram 에 영향을 주므로 따로 전달 TCP 에 의해서 호출 ICMP error message struct protosw{ } 안의 pr_ctlinput entry source buffer Full
hufs MCLAB cmd, dest address family verify Redirect handle PRC_HOSTDEAD old error, not used Global array 를 errno value 로 후에 notify( ) 의 argument notify tcp_notify or udp_notify Call notify function for selected PCBs
hufs MCLAB in_rtchange Function ICMP Errors and UDP Sockets UDP socket 이 connect 가 아니면 application 은 받지 못한다. Socket 의 foreign IP addr, port 로 제한했기 때문 in_pcbnotify() dst argument 안에 foreign IP addr, port 가 있어야 하는데, connect UDP 가 아니면 inp_faddr, inp_fport 를 저장 X UDP socket 은 ICMP error 를 못 받는다. ICMP error is redirect 일때, in_pcbnotify() 가 call PCB 가 route 를 hold 하면 rtfree, 그리고 empty 이 function 에서 route update 하지 않고, 다음에 PCB 가 사용될 때 new route 가 ip_output 에 의해 할당
hufs MCLAB in_losing Function 4 번 이상의 retransmission timer 초과 시 TCP 에 의해서 호출 Generate routing message rt_addrinfo structure 는 실패한 route 정보로 체워져 있다. Delete or release route RTF_DYNAMIC redirect route 는 RTM_DELETE 의 요청을 하는 rtrequest() 에 의해 delete
hufs MCLAB Summary Internet PCB 는 Internet socket(TCP, UDP, raw IP) 관련 All Internet socket 에 공통된 정보를 포함 Local and foreign IP addresses, route structure pointer All PCBs 은 protocol 에 의해서 doubly linked list 로 구성 udp, tcp … 1. in_pcblookup 는 TCP, UDP 에 의해서 call, received datagram 에 대해서 demultiplex, 이때 wildcard match Also in_pcbbind, in_pcbconnect call 해서 local address, process 가 unique 한 지, 또 local,foreign address,process 가 unique 한지 확인 2. in_pcbbind 는 explicitly, implicitly 하게 local address, port 를 bind explicitly 는 process 가 bind call, implicitly 는 TCP, UDP 가 bind 없이 connect 하거나 sendto call 할 때 3. in_pcbconnect 는 foreign address, port 를 set local address 가 set 되지 않았다면 foreign address 의 경로에 적합한 local interface 가 local address, local port 가 set 되지 않았다면 in_pcbbind 가 socket 에 ephemeral port 를 선택
hufs MCLAB Summary of in_pcbbind( ) and in_pcbconnect( )