qmi.sh 6.2 KB

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