Make enum transport_type match the networkProtocol enumeration

This means no conversion is necessary between the transport_type and
the networkProtocol field of the PortAddress struct. Not currently an
issue, but will be needed for implementing the CLOCK_DESCRIPTION
management TLV.
transport: adds interface for getting type, and physical/protocol address

Needed for CLOCK_DESCRIPTION management TLV.
raw: implement getting physical and protocol addrs
add sk_interface_addr for getting an interface's IP
udp: implement getting physical and protocol addrs
udp6: implement getting physical and protocol addrs
Fix initial drift calculation in PI servo.

Convert the calculated drift to ppb and also clamp it.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Modify servo_sample() to accept integer values.

Current date stored in nanoseconds doesn't fit in 64-bit double format.
Keep the offset and the time stamp in integer nanoseconds.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Modify PI servo to set frequency when jumping.

Similarly to the servo in phc2sys, when clock is stepped, set
immediately also its frequency. This significantly improves the initial
convergence with large frequency offsets.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Remove unnecessary states in PI servo.

Step the clock as soon as the second sample is collected.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
phc2sys: use servo code from ptp4l.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
phc2sys: move phc loop to its own function.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
phc2sys: don't zero clock frequency on start.

Instead of always starting at zero frequency offset, read the currently
stored value on start and pass it to the servo. As the read may silently
fail and return zero, set the clock frequency back to the read value to
make sure it's always equal to the actual frequency of the clock.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
phc2sys: remove unnecessary clock step with non-PPS loops.

With non-PPS loops let the servo make the inital correction. Move the
code to the PPS loop and change it to use the sample filtering to reduce
the error in the initial correction.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
phc2sys: read PHC with each PPS sample.

In the PPS loop, instead of setting the system clock from the PHC only
once on start, read PHC with each PPS sample and use the time stamp to
get the whole number of seconds in the offset. This will prevent phc2sys
from losing track of the system clock.

Also, check if the PPS is synchronized to the PHC.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
phc2sys: allow PPS loop only with system clock.

The PPS time stamps are always made by the system clock, don't allow
running the PPS loop with other clocks.

Signed-off-by: Miroslav Lichvar <mlichvar@redhat.com>
Add missing documentation for msg_post_recv.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
Check that TLV length is correct when receiving TLVs.

The function, tlv_post_recv, and the functions it calls don't check
the length of the tlv before flipping the byte order of fields. An
attacker (or a really buggy client) can craft a message causing the
byte order of data outside the received message to be flipped.

None of the supported tlvs are large enough to flip bytes outside the
ptp_message struct, which could corrupt the heap. However, it's easy
to mess up the message's refcnt field, leading to memory leaks.

The fix is to check that the tlv length is what is expected when
receiving, and tlv_post_recv needs to return an int to signal when a
tlv is invalid.

Signed-off-by: Geoff Salmon <gsalmon@se-instruments.com>
Start P2P messages right away when listening after initializing.

Because of an oversight in the event code, a port will not send peer delay
request messages while in the initial listening state. This patch fixes
the issue by expanding this special, initial case.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>
Introduce a test for running in 802.1AS mode.

We use the follow_up_info to control behavior that is specific to the
802.1AS standard. In several instances, that standard goes against the
1588 standard or requires new run time logic that exceeds what can be
reasonably described as a 1588 profile.

Since we will need a few more run time exceptions in order to support
802.1AS, we introduce a helper function to identify this case, rather
than hard coding a test for follow_up_info, in order to be more clear
about it.

Signed-off-by: Richard Cochran <richardcochran@gmail.com>