Tuesday, August 1, 2017

openwrt dhcp client



udhcpc -p /var/run/udhcpc-eth0.pid -s /lib/netifd/dhcp.script

udhcpc -i eth1

root@OpenWrt:~# udhcpc --help
BusyBox v1.23.2 (2017-07-28 16:20:17 PDT) multi-call binary.

Usage: udhcpc [-fbqRB] [-t N] [-T SEC] [-A SEC/-n]
[-i IFACE] [-s PROG] [-p PIDFILE]
[-oC] [-r IP] [-V VENDOR] [-F NAME] [-x OPT:VAL]... [-O OPT]...

-i,--interface IFACE Interface to use (default eth0)
-s,--script PROG Run PROG at DHCP events (default /usr/share/udhcpc/default.script)
-p,--pidfile FILE Create pidfile
-B,--broadcast Request broadcast replies
-t,--retries N Send up to N discover packets (default 3)
-T,--timeout SEC Pause between packets (default 3)
-A,--tryagain SEC Wait if lease is not obtained (default 20)
-n,--now Exit if lease is not obtained
-q,--quit Exit after obtaining lease
-R,--release Release IP on exit
-f,--foreground Run in foreground
-b,--background Background if lease is not obtained
-S,--syslog Log to syslog too
-r,--request IP Request this IP address
-o,--no-default-options Don't request any options (unless -O is given)
-O,--request-option OPT Request option OPT from server (cumulative)
-x OPT:VAL Include option OPT in sent packets (cumulative)
Examples of string, numeric, and hex byte opts:
-x hostname:bbox - option 12
-x lease:3600 - option 51 (lease time)
-x 0x3d:0100BEEFC0FFEE - option 61 (client id)
-F,--fqdn NAME Ask server to update DNS mapping for NAME
-V,--vendorclass VENDOR Vendor identifier (default 'udhcp VERSION')
-C,--clientid-none Don't send MAC as client identifier
Signals:
USR1 Renew lease
USR2 Release lease

root@OpenWrt:~#






root@OpenWrt:/lib/netifd# cat dhcp.script
#!/bin/sh
[ -z "$1" ] && echo "Error: should be run by udhcpc" && exit 1

. /lib/functions.sh
. /lib/netifd/netifd-proto.sh

set_classless_routes() {
local max=128
while [ -n "$1" -a -n "$2" -a $max -gt 0 ]; do
proto_add_ipv4_route "${1%%/*}" "${1##*/}" "$2" "$ip"
max=$(($max-1))
shift 2
done
}

setup_interface () {
proto_init_update "*" 1
proto_add_ipv4_address "$ip" "${subnet:-255.255.255.0}"
# TODO: apply $broadcast

for i in $router; do
proto_add_ipv4_route "$i" 32 "" "$ip"
proto_add_ipv4_route 0.0.0.0 0 "$i" "$ip"

for r in $CUSTOMROUTES; do
proto_add_ipv4_route "${r%%/*}" "${r##*/}" "$i" "$ip"
done
done

# CIDR STATIC ROUTES (rfc3442)
[ -n "$staticroutes" ] && set_classless_routes $staticroutes
[ -n "$msstaticroutes" ] && set_classless_routes $msstaticroutes

for dns in $dns; do
proto_add_dns_server "$dns"
done
for domain in $domain; do
proto_add_dns_search "$domain"
done

proto_add_data
[ -n "$ZONE" ]     && json_add_string zone "$ZONE"
[ -n "$ntpsrv" ]   && json_add_string ntpserver "$ntpsrv"
[ -n "$timesvr" ]  && json_add_string timeserver "$timesvr"
[ -n "$hostname" ] && json_add_string hostname "$hostname"
[ -n "$message" ]  && json_add_string message "$message"
[ -n "$timezone" ] && json_add_int timezone "$timezone"
[ -n "$lease" ]    && json_add_int leasetime "$lease"
proto_close_data

proto_send_update "$INTERFACE"


if [ "$IFACE6RD" != 0 -a -n "$ip6rd" ]; then
local v4mask="${ip6rd%% *}"
ip6rd="${ip6rd#* }"
local ip6rdprefixlen="${ip6rd%% *}"
ip6rd="${ip6rd#* }"
local ip6rdprefix="${ip6rd%% *}"
ip6rd="${ip6rd#* }"
local ip6rdbr="${ip6rd%% *}"

[ -n "$ZONE" ] || ZONE=$(fw3 -q network $INTERFACE)
[ -z "$IFACE6RD" -o "$IFACE6RD" = 1 ] && IFACE6RD=${INTERFACE}_6

json_init
json_add_string name "$IFACE6RD"
json_add_string ifname "@$INTERFACE"
json_add_string proto "6rd"
json_add_string peeraddr "$ip6rdbr"
json_add_int ip4prefixlen "$v4mask"
json_add_string ip6prefix "$ip6rdprefix"
json_add_int ip6prefixlen "$ip6rdprefixlen"
json_add_string tunlink "$INTERFACE"
[ -n "$IFACE6RD_DELEGATE" ] && json_add_boolean delegate "$IFACE6RD_DELEGATE"
[ -n "$ZONE6RD" ] || ZONE6RD=$ZONE
[ -n "$ZONE6RD" ] && json_add_string zone "$ZONE6RD"
[ -n "$MTU6RD" ] && json_add_string mtu "$MTU6RD"
json_close_object

ubus call network add_dynamic "$(json_dump)"
fi
}

deconfig_interface() {
proto_init_update "*" 0
proto_send_update "$INTERFACE"
}

case "$1" in
deconfig)
deconfig_interface
;;
renew|bound)
setup_interface
;;
esac

# user rules
[ -f /etc/udhcpc.user ] && . /etc/udhcpc.user "$@"

exit 0

Friday, July 28, 2017

wifi channel survey

channel active time - consists of the amount of time the radio has spent on the channel.

channel busy time - consists of the amount of time the radio has spent on the channel but noticed the channel was busy and could not initiate communication if it wanted to.
*** busy time = tx time + rx time + alpha (energy detector showed input power over the noise threshold)?

channel transmit time - is the amount of time the radio has spent on the channel transmitting data.

channel receive time -  is the amount of time the radio has spent on the channel receiving data.

**
 * struct survey_info - channel survey response
 *
 * @channel: the channel this survey record reports, mandatory
 * @filled: bitflag of flags from &enum survey_info_flags
 * @noise: channel noise in dBm. This and all following fields are
 * optional
 * @channel_time: amount of time in ms the radio spent on the channel
 * @channel_time_busy: amount of time the primary channel was sensed busy
 * @channel_time_ext_busy: amount of time the extension channel was sensed busy
 * @channel_time_rx: amount of time the radio spent receiving data
 * @channel_time_tx: amount of time the radio spent transmitting data
 *
 * Used by dump_survey() to report back per-channel survey information.
 *
 * This structure can later be expanded with things like
 * channel duty cycle etc.
 */
struct survey_info {
 struct ieee80211_channel *channel;
 u64 channel_time;
 u64 channel_time_busy;
 u64 channel_time_ext_busy;
 u64 channel_time_rx;
 u64 channel_time_tx;
 u32 filled;
 s8 noise;
};

  • noise: I'm guessing this is the threshold value used by the energy detection carrier sense mechanism in the CSMA/CA protocol. It's around -95 dBm for my router, which sounds like a valid value. It's not static but changes up and down by a decibel or two. It would be interesting to see how the radio dynamically determines the noise level.
  • channel_time: If you don't change channels, this goes up by 1000 ms each 1000 ms of wall-clock time.
  • channel_time_busy: Probably the amount of time the energy detector showed input power over the noise threshold, plus the time the radio was in transmit mode. This seems to be about the same as the sum of RX and TX times, unless there is a lot of external interference (like another busy Wi-Fi network on the same channel).
  • channel_time_ext_busy: This might be a counter for the secondary radio channel used in channel binding, like in 802.11ac. I haven't tested this since channel binding isn't working on my router.
  • channel_time_rx: On my router this increments at around 10% of channel_time rate, even if the network is completely idle, so I'm guessing it triggers at some very low-level, before packet CRC checks and things like that. As expected, it goes towards 100% of channel_time rate if you send a lot of data towards the interface.
  • channel_time_tx: At idle it's around 2% on my router, which seems consistent with beacon broadcasts. It goes towards 100% of channel_time rate if you send a lot of data from the interface.

tchanneltchannel−tx+tchannel−rxtchanneltchannel−busy100%

Tchannel_tx_time + Tchannel_rx_time / Tchannel_active_time
< 
Tchannel_busy_time / Tchannel_active_time
< 
100%

references:
https://www.tablix.org/~avian/blog/archives/2016/02/ieee_802_11_channel_survey_statistics/

Thursday, July 6, 2017

scp example

Copy the file "foobar.txt" from a remote host to the local host

    $ scp your_username@remotehost.edu:~/work/foobar.txt ./some/local/directory

Copy the file "foobar.txt" from the local host to a remote host

    $ scp foobar.txt your_username@remotehost.edu:~/some/remote/directory

Copy the directory "foo" from the local host to a remote host's directory "bar"

    $ scp -r foo your_username@remotehost.edu:~/some/remote/directory/bar

Copy the file "foobar.txt" from remote host "rh1.edu" to remote host "rh2.edu"

    $ scp your_username@rh1.edu:/some/remote/directory/foobar.txt \
    your_username@rh2.edu:/some/remote/directory/

Copying the files "foo.txt" and "bar.txt" from the local host to your home directory on the remote host

    $ scp foo.txt bar.txt your_username@remotehost.edu:~

Copy the file "foobar.txt" from the local host to a remote host using port 2264

    $ scp -P 2264 foobar.txt your_username@remotehost.edu:/some/remote/directory

Copy multiple files from the remote host to your current directory on the local host

    $ scp your_username@remotehost.edu:/some/remote/directory/\{a,b,c\} .
    $ scp your_username@remotehost.edu:~/\{foo.txt,bar.txt\} .

Friday, June 23, 2017

wireshark tips

to find binary contents

frame contains 00:40:3f
(rather than 00403f, if you're searching for a byte with the value hex 00, followed by a byte with the value hex 40, followed by a byte with the value hex 3f) will match regardless of whether the frames are Ethernet frames or not.
to find string
frame contains "string"

Wednesday, June 14, 2017

802.11ac channel segment calculation

Seg0 = center frequency channel
Seg0 = (primary channel number) + (((bandwidth / 2) - 10) / 5)
* ( bandwidth / 2 ) = look up center
* ( -10 ) = 20MHz channel center is 10Mhz above
* ( /5 ) = 1 channel width is 5Mhz

Ch36 40MHz Seg0 = 36 + (((40 / 2) - 10) / 5) = 38 (36 + 2)
Ch36 80MHz Seg0 = 36 + (((80 / 2) - 10) / 5) = 42 (36 + 6)
Ch36 160MHz Seg0 = 36 + (((160 / 2) - 10) / 5) = 50 (36 + 14)

dot11CurrentChannelWidth
Channel width. Possible values represent 20 MHz, 40 MHz,
80 MHz, 160 MHz, and 80+80 MHz channels.

dot11CurrentChannelCenterFrequencyIndex0
For a 20 MHz, 40 MHz, 80 MHz, or 160 MHz channel, denotes
the channel center frequency.
For an 80+80 MHz channel, denotes the center frequency of the
frequency segment 0, which is the frequency segment containing
the primary channel.
Valid range is 1 to 200.
See Equation (22-102).

dot11CurrentChannelCenterFrequencyIndex1
For an 80+80 MHz channel, denotes the center frequency of the
frequency segment 1, which is the frequency segment that does
not contain the primary channel.
Valid range is 1 to 200.
See Equation (22-102).
For a 20 MHz, 40 MHz, 80 MHz, or 160 MHz channel, set to 0.

dot11CurrentPrimaryChannel
Denotes the location of the primary 20 MHz channel.
Valid range is 1 to 200.
See Equation (22-103).

22-103:
Primary 20 MHz channel center frequency [MHz]
= Channel starting frequency + 5 x dot11CurrentPrimaryChannel
ex) channel 36: 5000MHz + 5 x 36 = 5180Mhz
dot11CurrentPrimaryChannel = (5180 - 5000) / 5 = 36

22-102:
VHT80 Channel 36 center frequency = 5000 + 5 x 36 = 5180Mhz
* Ch36 80MHz Seg0 = 36 + (((80 / 2) - 10) / 5) = 42 (36 + 6)
Channel center frequency [MHz]
= Channel starting frequency + 5 x dot11CurrentChannelCenterFrequencyIndex
Channel center frequency [MHz] = 5000 + 5 x 42 = 5210MHz
dot11CurrentChannelCenterFrequencyIndex = dot11CurrentChannelCenterFrequencyIndex0 = 42

dot11CurrentChannelCenterFrequencyIndex is either dot11CurrentChannelCenterFrequencyIndex0 or
dot11CurrentChannelCenterFrequencyIndex1