qmi.sh 7.5 KB

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