Two patches. The first has l2tpd pass the calling number (if supplied) from the calling number AVP into l2tp_session_lns_handle_incoming_call()'s calling number parameter (as suggested by the "TODO" comment before said call).
The second is a patch to the sync-pppd handler which places the calling number and the peer host name into the environment variables L2TP_CALLING_NUM and
L2TP_PEER_HOSTNAME respectively.
Note that the Linux /usr/sbin/pppd clobbers the environment before calling any of its scripts such as ip-up, so you need to get at the environment before pppd gets its hands on it. Do this by having l2tpd call a script which in turn calls pppd, e.g. in /etc/l2tp/l2tp.conf:
section sync-pppd
pppd-path "/etc/l2tp/pppd-wrapper"
then have /etc/l2tp/pppd-wrapper do something like:
#!/bin/sh
exec /usr/sbin/pppd "$@" ipparam "$L2TP_CALLING_NUM"
This will cause pppd to pass the calling number to /etc/ppp/ip-up as $6.
Record calling number
Add calling number and peer name to environment