Merge ~alfonsosanchezbeato/snappy-hwe-snaps/+git/wifi-ap:move-to-ip into ~snappy-hwe-team/snappy-hwe-snaps/+git/wifi-ap:master

Proposed by Alfonso Sanchez-Beato
Status: Merged
Approved by: Tony Espy
Approved revision: a3e7cd0d4353c8e81178e9919ee5618648d29499
Merged at revision: 1735830fd3ae3bc0e492e2f86af1d74f307bc97d
Proposed branch: ~alfonsosanchezbeato/snappy-hwe-snaps/+git/wifi-ap:move-to-ip
Merge into: ~snappy-hwe-team/snappy-hwe-snaps/+git/wifi-ap:master
Diff against target: 104 lines (+56/-5)
2 files modified
bin/ap.sh (+9/-5)
bin/helper.sh (+47/-0)
Reviewer Review Type Date Requested Status
System Enablement Bot continuous-integration Approve
Tony Espy Approve
Review via email: mp+359736@code.launchpad.net

Commit message

* Cleanup ip addresses when stopping AP
* Use ip command instead of ifconfig

Description of the change

* Cleanup ip addresses when stopping AP
* Use ip command instead of ifconfig

To post a comment you must log in.
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :

PASSED: Successfully build documentation, rev: eb78436cc6e08e9530eca80af3565a02b6445323

Generated documentation is available at https://jenkins.canonical.com/system-enablement/job/snappy-hwe-snaps-snap-docs/1133/

Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Tony Espy (awe) wrote :

Is there a bug associated with the ip address cleanup?

Also, re: the commit message for the ifconfig --> ip change, a better explanation would be:

"ifconfig is deprecated, and is not included in the new core18 base snap, so switching scripts to use the ip command instead."

review: Needs Fixing
Revision history for this message
Alfonso Sanchez-Beato (alfonsosanchezbeato) wrote :

@Tony, comments addressed, including adding a bug.

Revision history for this message
Tony Espy (awe) :
review: Approve
Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :

PASSED: Successfully build documentation, rev: a3e7cd0d4353c8e81178e9919ee5618648d29499

Generated documentation is available at https://jenkins.canonical.com/system-enablement/job/snappy-hwe-snaps-snap-docs/1134/

Revision history for this message
System Enablement Bot (system-enablement-ci-bot) wrote :
review: Approve (continuous-integration)

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/bin/ap.sh b/bin/ap.sh
2index add2212..80c44e8 100755
3--- a/bin/ap.sh
4+++ b/bin/ap.sh
5@@ -38,7 +38,7 @@ DEFAULT_ACCESS_POINT_INTERFACE="ap0"
6
7 # Make sure the configured WiFi interface is really available before
8 # doing anything.
9-if ! ifconfig "$WIFI_INTERFACE" ; then
10+if ! ip link show "$WIFI_INTERFACE" ; then
11 echo "ERROR: WiFi interface $WIFI_INTERFACE is not available!"
12 exit 1
13 fi
14@@ -65,11 +65,14 @@ do_cleanup() {
15
16 if [ "$SHARE_DISABLED" = "false" ] ; then
17 # flush forwarding rules out
18- iptables --table nat --delete POSTROUTING --out-interface "$SHARE_NETWORK_INTERFACE" -j MASQUERADE
19- iptables --delete FORWARD --in-interface $iface -j ACCEPT
20+ iptables --table nat --delete POSTROUTING \
21+ --out-interface "$SHARE_NETWORK_INTERFACE" -j MASQUERADE || true
22+ iptables --delete FORWARD --in-interface $iface -j ACCEPT || true
23 sysctl -w net.ipv4.ip_forward=0
24 fi
25
26+ ip address flush $iface
27+
28 if [ "$WIFI_INTERFACE_MODE" = "virtual" ] ; then
29 "$SNAP"/bin/iw dev $iface del
30 elif is_nm_running ; then
31@@ -132,7 +135,7 @@ if is_nm_running ; then
32 fi
33
34 # Initial wifi interface configuration
35-if ! ifconfig "$iface" up ; then
36+if ! ip link set dev "$iface" up ; then
37 echo "ERROR: Failed to enable WiFi network interface '$iface'"
38
39 # Remove virtual interface again if we created one
40@@ -151,7 +154,8 @@ if ! ifconfig "$iface" up ; then
41 fi
42
43 # Configure interface and give it a moment to settle
44-ifconfig "$iface" "$WIFI_ADDRESS" netmask "$WIFI_NETMASK"
45+net_prefix=$(get_prefix_from_ip_mask "$WIFI_ADDRESS" "$WIFI_NETMASK")
46+ip address add "$WIFI_ADDRESS"/"$net_prefix" dev "$iface"
47 sleep 2
48
49 if [ "$SHARE_DISABLED" = "false" ] ; then
50diff --git a/bin/helper.sh b/bin/helper.sh
51index f3426a3..135d5a5 100644
52--- a/bin/helper.sh
53+++ b/bin/helper.sh
54@@ -56,3 +56,50 @@ is_nm_running() {
55 nm_status=$("$SNAP"/bin/nmcli -t -f RUNNING general)
56 [ "$nm_status" = "running" ]
57 }
58+
59+# Returns network prefix from ip, by finding out its class
60+# $1: IPv4 address
61+ABCMaskLen() {
62+ local class
63+ # Get first integer from ipv4 address to determine class
64+ class=${1%%.*}
65+ if [ "$class" -eq 0 ] || [ "$class" -ge 224 ]; then echo 0; return
66+ elif [ "$class" -ge 192 ]; then echo 24; return
67+ elif [ "$class" -ge 128 ]; then echo 16; return
68+ else echo 8; fi
69+}
70+
71+# Returns network prefix from ip/netmask
72+# $1: IPv4 address
73+# $2: netmask (optional)
74+get_prefix_from_ip_mask() {
75+ local ip=$1
76+ local mask=$2
77+ if [[ $mask != [0-9]*.[0-9]*.[0-9]*.[0-9]* ]]; then
78+ ABCMaskLen "$ip"
79+ return
80+ fi
81+
82+ mask=$mask.
83+ local mask_bits=0
84+ while true; do
85+ # Get integer most to the left in the mask
86+ local next=${mask%%.*}
87+ if [ -z "$next" ]; then
88+ break
89+ fi
90+ if [ "$next" -eq 0 ];then
91+ break
92+ fi
93+ local right_zeros=0
94+ while [ $((next & 1)) -ne 1 ]; do
95+ ((next >>= 1)) || true
96+ ((right_zeros += 1))
97+ done
98+ ((mask_bits += 8 - right_zeros))
99+ # Remove the analyzed integer from the mask
100+ mask=${mask#[0-9]*.}
101+ done
102+
103+ echo $mask_bits
104+}

Subscribers

People subscribed via source and target branches