qmi.sh 7.4 KB

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