Tuesday, July 19, 2016

Network Time Protocol (NTP)


What does NTP client asks NTP server when it bumps into it ? Hey buddy, What's the time? 

NTP protocol is used by computers and embedded devices to synchronize time to a common clock reference across the internet. Many embedded devices like a WLAN Access Point(AP) ,IPTV Set Top Box (STB), smart TV etc do not have hardware clock inside them. These devices heavily rely on NTP Server to get the correct time.
NTP based time is used for many critical processes inside these devices. For example a smart TV needs a correct time stamp for various things like:
  • Logging : If we have to debug some issue, which user is facing, we need a correct time stamp  to relate user specified problem with exact date and time inside the logs.
  • Device Lease : When your device gets connected to an ethernet interface or to WiFi interface , it will have DHCP lease allocated , If your device does not have correct date and time, your DHCP lease could lie in past and DHCP client on your device can start to behave insanely and it can get abrupt disconnections.  
The piece of software which runs on embedded devices to synchronize the time over the internet is called NTP client. NTP client talks to NTP server to get the correct date and time. NTP client uses UDP protocol for its operation and it operates over UDP port 123 at transport layer. Following sequence of packets exchange happens between NTP server and NTP client to update the time:
  • NTP client sends the packet to NTP server containing its own time stamp
  • NTP server responds  this request with a packet containing 2 time stamps:
    • The original client timestamp.
    • the time at which server received the request from NTP client.
    • the time at which server is sending the response to NTP client.
  • Client estimates the delta between its timestamp and server timestamp.
  • Client can talk to multiple NTP servers.
  • But it will sync up with only one NTP server based on the offset and jitter.

On most of the embedded devices the default time zone is UTC. NTP server and NTP client exchange messages  in UTC format. If after NTP synchronization , We want our device to display the correct date and time , then it should apply the correct timezone on UTC time.

Sunday, July 3, 2016

How a WLAN station connects to an Access Point ?



WLAN connection is a 3 step process:

  • Scanning
  • Authentication
  • Association
Scanning : - Station does a scan to find an appropriate AP to which a WLAN station(STA) wants to connect to. For detailed understanding, Please read my another article on WLAN Scanning.

Authentication:-
Authentication is a process by which WLAN STA proves its identity to an AP. Authentication could be :

  • An open system authentication : In an open system authentication ,WLAN STA does not need any password/secret passphrase to connect to an AP. WLAN STA just says "authenticate me" and  an AP says "Yes, you are authenticated",  And AP and STA can start data exchange after this.
  • Pre-shared key authentication : In shared key authentication there is a pre-shared,    key which both parties(AP and STA) know about. When STA wants to authenticate with an AP, 
    • it sends an authentication request frame to an AP.
    • Ap sends the "Challenge Text" to STA.
    • STA encrypts this "Challenge Text" with shared key it has, and sends back to an AP.
    • AP decrypts this "Challenge Text" and matches it with the original  "Challenge Text" it had. If both texts match AP sends the OK confirmation to a STA. And after this association process starts.
Association:
  1. 802.11 association  process finalizes the security and bit rate options and establishes the data link between station and an access point.
  2. 802.11 association process enables the access point to allocate resources to WLAN STA.
  3. WLAN STA starts the association process by sending an association request frame to an AP.
    1. This association frame contains information about the station like supported data rates and SSID it wants to associate with.
    2. In response to association request, AP sends back an association response, which may contain approval or disapproval.
    3. If an AP approves the association request, it will send back the Association ID (AID) and supported data rates.
    4. After association STA becomes the member of a BSS.
    5. STA can send data to an AP.
    6. AID  identifies each station.