qmi.sh 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #!/bin/sh
  2. [ -n "$INCLUDE_ONLY" ] || {
  3. . /lib/functions.sh
  4. . ../netifd-proto.sh
  5. init_proto "$@"
  6. }
  7. proto_qmi_init_config() {
  8. available=1
  9. no_device=1
  10. proto_config_add_string "device:device"
  11. proto_config_add_string apn
  12. proto_config_add_string auth
  13. proto_config_add_string username
  14. proto_config_add_string password
  15. proto_config_add_string pincode
  16. proto_config_add_int delay
  17. proto_config_add_string modes
  18. proto_config_add_string pdptype
  19. proto_config_add_int profile
  20. proto_config_add_boolean dhcpv6
  21. proto_config_add_boolean autoconnect
  22. proto_config_add_defaults
  23. }
  24. proto_qmi_setup() {
  25. local interface="$1"
  26. local device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect $PROTO_DEFAULT_OPTIONS
  27. local cid_4 pdh_4 cid_6 pdh_6
  28. local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
  29. json_get_vars device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect $PROTO_DEFAULT_OPTIONS
  30. [ "$metric" = "" ] && metric="0"
  31. [ -n "$ctl_device" ] && device=$ctl_device
  32. [ -n "$device" ] || {
  33. echo "No control device specified"
  34. proto_notify_error "$interface" NO_DEVICE
  35. proto_set_available "$interface" 0
  36. return 1
  37. }
  38. device="$(readlink -f $device)"
  39. [ -c "$device" ] || {
  40. echo "The specified control device does not exist"
  41. proto_notify_error "$interface" NO_DEVICE
  42. proto_set_available "$interface" 0
  43. return 1
  44. }
  45. devname="$(basename "$device")"
  46. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  47. ifname="$( ls "$devpath"/net )"
  48. [ -n "$ifname" ] || {
  49. echo "The interface could not be found."
  50. proto_notify_error "$interface" NO_IFACE
  51. proto_set_available "$interface" 0
  52. return 1
  53. }
  54. [ -n "$delay" ] && sleep "$delay"
  55. while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
  56. sleep 1;
  57. done
  58. [ -n "$pincode" ] && {
  59. uqmi -s -d "$device" --verify-pin1 "$pincode" || {
  60. echo "Unable to verify PIN"
  61. proto_notify_error "$interface" PIN_FAILED
  62. proto_block_restart "$interface"
  63. return 1
  64. }
  65. }
  66. uqmi -s -d "$device" --set-data-format 802.3
  67. uqmi -s -d "$device" --wda-set-data-format 802.3
  68. echo "Waiting for network registration"
  69. while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
  70. sleep 5;
  71. done
  72. [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
  73. echo "Starting network $interface"
  74. pdptype=`echo "$pdptype" | awk '{print tolower($0)}'`
  75. [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
  76. if [ "$pdptype" = "ip" ]; then
  77. [ -z "$autoconnect" ] && autoconnect=1
  78. [ "$autoconnect" = 0 ] && autoconnect=""
  79. else
  80. [ "$autoconnect" = 1 ] || autoconnect=""
  81. fi
  82. [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
  83. cid_4=`uqmi -s -d "$device" --get-client-id wds`
  84. [ $? -ne 0 ] && {
  85. echo "Unable to obtain client ID"
  86. proto_notify_error "$interface" NO_CID
  87. return 1
  88. }
  89. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null
  90. # try to clear previous autoconnect state
  91. uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
  92. --stop-network 0xffffffff \
  93. --autoconnect > /dev/null
  94. pdh_4=`uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
  95. --start-network \
  96. ${apn:+--apn $apn} \
  97. ${profile:+--profile $profile} \
  98. ${auth:+--auth-type $auth} \
  99. ${username:+--username $username} \
  100. ${password:+--password $password} \
  101. ${autoconnect:+--autoconnect}`
  102. [ $? -ne 0 ] && {
  103. echo "Unable to connect IPv4"
  104. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
  105. proto_notify_error "$interface" CALL_FAILED
  106. return 1
  107. }
  108. }
  109. [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
  110. cid_6=`uqmi -s -d "$device" --get-client-id wds`
  111. [ $? -ne 0 ] && {
  112. echo "Unable to obtain client ID"
  113. proto_notify_error "$interface" NO_CID
  114. return 1
  115. }
  116. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null
  117. # try to clear previous autoconnect state
  118. uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
  119. --stop-network 0xffffffff \
  120. --autoconnect > /dev/null
  121. pdh_6=`uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
  122. --start-network \
  123. ${apn:+--apn $apn} \
  124. ${profile:+--profile $profile} \
  125. ${auth:+--auth-type $auth} \
  126. ${username:+--username $username} \
  127. ${password:+--password $password} \
  128. ${autoconnect:+--autoconnect}`
  129. [ $? -ne 0 ] && {
  130. echo "Unable to connect IPv6"
  131. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
  132. proto_notify_error "$interface" CALL_FAILED
  133. return 1
  134. }
  135. }
  136. echo "Setting up $ifname"
  137. proto_init_update "$ifname" 1
  138. proto_set_keep 1
  139. proto_add_data
  140. [ -n "$pdh_4" ] && {
  141. json_add_string "cid_4" "$cid_4"
  142. json_add_string "pdh_4" "$pdh_4"
  143. }
  144. [ -n "$pdh_6" ] && {
  145. json_add_string "cid_6" "$cid_6"
  146. json_add_string "pdh_6" "$pdh_6"
  147. }
  148. proto_close_data
  149. proto_send_update "$interface"
  150. [ -n "$pdh_6" ] && {
  151. if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
  152. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
  153. json_select ipv6
  154. json_get_var ip_6 ip
  155. json_get_var gateway_6 gateway
  156. json_get_var dns1_6 dns1
  157. json_get_var dns2_6 dns2
  158. json_get_var ip_prefix_length ip-prefix-length
  159. proto_init_update "$ifname" 1
  160. proto_set_keep 1
  161. proto_add_ipv6_address "$ip_6" "128"
  162. proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
  163. proto_add_ipv6_route "$gateway_6" "128"
  164. [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
  165. [ "$peerdns" = 0 ] || {
  166. proto_add_dns_server "$dns1_6"
  167. proto_add_dns_server "$dns2_6"
  168. }
  169. proto_send_update "$interface"
  170. else
  171. json_init
  172. json_add_string name "${interface}_6"
  173. json_add_string ifname "@$interface"
  174. json_add_string proto "dhcpv6"
  175. proto_add_dynamic_defaults
  176. # RFC 7278: Extend an IPv6 /64 Prefix to LAN
  177. json_add_string extendprefix 1
  178. json_close_object
  179. ubus call network add_dynamic "$(json_dump)"
  180. fi
  181. }
  182. [ -n "$pdh_4" ] && {
  183. json_init
  184. json_add_string name "${interface}_4"
  185. json_add_string ifname "@$interface"
  186. json_add_string proto "dhcp"
  187. proto_add_dynamic_defaults
  188. json_close_object
  189. ubus call network add_dynamic "$(json_dump)"
  190. }
  191. }
  192. qmi_wds_stop() {
  193. local cid="$1"
  194. local pdh="$2"
  195. [ -n "$cid" ] || return
  196. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  197. --stop-network 0xffffffff \
  198. --autoconnect > /dev/null
  199. [ -n "$pdh" ] && uqmi -s -d "$device" --set-client-id wds,"$cid" --stop-network "$pdh"
  200. uqmi -s -d "$device" --set-client-id wds,"$cid" --release-client-id wds
  201. }
  202. proto_qmi_teardown() {
  203. local interface="$1"
  204. local device cid_4 pdh_4 cid_6 pdh_6
  205. json_get_vars device
  206. [ -n "$ctl_device" ] && device=$ctl_device
  207. echo "Stopping network $interface"
  208. json_load "$(ubus call network.interface.$interface status)"
  209. json_select data
  210. json_get_vars cid_4 pdh_4 cid_6 pdh_6
  211. qmi_wds_stop "$cid_4" "$pdh_4"
  212. qmi_wds_stop "$cid_6" "$pdh_6"
  213. proto_init_update "*" 0
  214. proto_send_update "$interface"
  215. }
  216. [ -n "$INCLUDE_ONLY" ] || {
  217. add_protocol qmi
  218. }