Clear netlink receive queue after reading to avoid an overflow

tlsdated only reads its NETLINK_ROUTE socket until finding the first
RTM_NEWROUTE message. After that, it will sleep at least
wait_between_tries seconds before reading the socket again.

On subnets with many hosts (such as a large public WiFi), the kernel
can generate a _lot_ of route updates for IPv6 solicited-node multicast
addresses. If tlsdated does not read them fast enough, they will queue
up and eventually overflow the socket receive buffer. This results in an
ENOBUFS error which will cause the process to terminate itself.

This patch ensures that tlsdated always reads all available packets in
its netlink receive queue before proceeding. As we only care if there
have been any route updates at all during one read, the additional
packets can simply be discarded. This also makes sure that they will not
be read at a later date (and misinterpreted as current packets at a time
when there are no more route updates).

Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-by: Elly Jones <ellyjones@chromium.org>
[tlsdate] support minimum check interval.

On systems where routes come up and down a lot, tlsdated can end up checking the
date too often and thus loading the check host; support limiting it to a minimum
frequency of successful checks. Failing checks can still happen arbitrarily
often.

BUG=chromium-os:37184
TEST=Adhoc

Change-Id: Ief413e91fcce98ed6bfc6c44d844a50b650438e0
Signed-off-by: Elly Fong-Jones <ellyjones@chromium.org>
(cherry picked from commit 6a7ded4a1848d8eb50a181f898fa9330f4e3ba34)
[tlsdate] fix certificate time compensation

Fix the code that compensates for certificates whose time is in the future, to
prevent a situation where it's impossible to fetch a new time because our
current time is too old. There are two bugs:

1. We try to use the server's time too late, after we've already done cert
validation; we need to do it earlier (during the server hello).

2. We set the time on the wrong context - we need to set the time on the cert
store instead of just the context object, since when OpenSSL is setting up to
verify the cert, it copies the cert store's params instead of the SSL context's
params.

BUG=chromium-os:36738
TEST=adhoc
date -s '2012-01-01 00:00:00' && tlsdate -l -v -H clients3.google.com -p 443

Change-Id: Ie1b11a2e0ecdc2196b729f6d5483b53c54a1933d
Signed-off-by: Elly Fong-Jones <ellyjones@chromium.org>
(cherry picked from commit 87ee9c2c23eff1816adc069c3b41f84933779643)
Merge pull request #52 from elly/min-delay

[tlsdate] support minimum check interval.
Merge pull request #53 from elly/cert-time

[tlsdate] fix certificate time compensation; in theory - we want this ordering.
Clear netlink receive queue after reading to avoid an overflow

tlsdated only reads its NETLINK_ROUTE socket until finding the first
RTM_NEWROUTE message. After that, it will sleep at least
wait_between_tries seconds before reading the socket again.

On subnets with many hosts (such as a large public WiFi), the kernel
can generate a _lot_ of route updates for IPv6 solicited-node multicast
addresses. If tlsdated does not read them fast enough, they will queue
up and eventually overflow the socket receive buffer. This results in an
ENOBUFS error which will cause the process to terminate itself.

This patch ensures that tlsdated always reads all available packets in
its netlink receive queue before proceeding. As we only care if there
have been any route updates at all during one read, the additional
packets can simply be discarded. This also makes sure that they will not
be read at a later date (and misinterpreted as current packets at a time
when there are no more route updates).

BUG=chromium-os:36426
TEST=Join GoogleGuest or a similar large network. strace tlsdated and
observe how it will not die despite occasional large bursts of netlink
packets.

Change-Id: Idbaf4be6d888c45256220055242b6f9b4d77bc93
Signed-off-by: Julius Werner <jwerner@chromium.org>
Reviewed-on: https://gerrit.chromium.org/gerrit/38173
Reviewed-by: Elly Jones <ellyjones@chromium.org>

Conflicts:
	src/routeup.c
Merge pull request #56 from elly/netlink-drain

Clear netlink receive queue after reading to avoid an overflow
resolve merge issue