qmi.sh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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_int timeout
  24. proto_config_add_defaults
  25. }
  26. proto_qmi_setup() {
  27. local interface="$1"
  28. local dataformat connstat
  29. local device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn timeout $PROTO_DEFAULT_OPTIONS
  30. local ip4table ip6table
  31. local cid_4 pdh_4 cid_6 pdh_6
  32. local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
  33. json_get_vars device apn auth username password pincode delay modes pdptype profile dhcpv6 autoconnect plmn ip4table ip6table timeout $PROTO_DEFAULT_OPTIONS
  34. [ "$timeout" = "" ] && timeout="10"
  35. [ "$metric" = "" ] && metric="0"
  36. [ -n "$ctl_device" ] && device=$ctl_device
  37. [ -n "$device" ] || {
  38. echo "No control device specified"
  39. proto_notify_error "$interface" NO_DEVICE
  40. proto_set_available "$interface" 0
  41. return 1
  42. }
  43. [ -n "$delay" ] && sleep "$delay"
  44. device="$(readlink -f $device)"
  45. [ -c "$device" ] || {
  46. echo "The specified control device does not exist"
  47. proto_notify_error "$interface" NO_DEVICE
  48. proto_set_available "$interface" 0
  49. return 1
  50. }
  51. devname="$(basename "$device")"
  52. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  53. ifname="$( ls "$devpath"/net )"
  54. [ -n "$ifname" ] || {
  55. echo "The interface could not be found."
  56. proto_notify_error "$interface" NO_IFACE
  57. proto_set_available "$interface" 0
  58. return 1
  59. }
  60. echo "Waiting for SIM initialization"
  61. local uninitialized_timeout=0
  62. while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
  63. [ -e "$device" ] || return 1
  64. if [ "$uninitialized_timeout" -lt "$timeout" ]; then
  65. let uninitialized_timeout++
  66. sleep 1;
  67. else
  68. echo "SIM not initialized"
  69. proto_notify_error "$interface" SIM_NOT_INITIALIZED
  70. proto_block_restart "$interface"
  71. return 1
  72. fi
  73. done
  74. if uqmi -s -d "$device" --get-pin-status | grep '"Not supported"\|"Invalid QMI command"' > /dev/null; then
  75. [ -n "$pincode" ] && {
  76. uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null || {
  77. echo "Unable to verify PIN"
  78. proto_notify_error "$interface" PIN_FAILED
  79. proto_block_restart "$interface"
  80. return 1
  81. }
  82. }
  83. else
  84. . /usr/share/libubox/jshn.sh
  85. json_load "$(uqmi -s -d "$device" --get-pin-status)"
  86. json_get_var pin1_status pin1_status
  87. json_get_var pin1_verify_tries pin1_verify_tries
  88. case "$pin1_status" in
  89. disabled)
  90. echo "PIN verification is disabled"
  91. ;;
  92. blocked)
  93. echo "SIM locked PUK required"
  94. proto_notify_error "$interface" PUK_NEEDED
  95. proto_block_restart "$interface"
  96. return 1
  97. ;;
  98. not_verified)
  99. [ "$pin1_verify_tries" -lt "3" ] && {
  100. echo "PIN verify count value is $pin1_verify_tries this is below the limit of 3"
  101. proto_notify_error "$interface" PIN_TRIES_BELOW_LIMIT
  102. proto_block_restart "$interface"
  103. return 1
  104. }
  105. if [ -n "$pincode" ]; then
  106. uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null 2>&1 || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null 2>&1 || {
  107. echo "Unable to verify PIN"
  108. proto_notify_error "$interface" PIN_FAILED
  109. proto_block_restart "$interface"
  110. return 1
  111. }
  112. else
  113. echo "PIN not specified but required"
  114. proto_notify_error "$interface" PIN_NOT_SPECIFIED
  115. proto_block_restart "$interface"
  116. return 1
  117. fi
  118. ;;
  119. verified)
  120. echo "PIN already verified"
  121. ;;
  122. *)
  123. echo "PIN status failed ($pin1_status)"
  124. proto_notify_error "$interface" PIN_STATUS_FAILED
  125. proto_block_restart "$interface"
  126. return 1
  127. ;;
  128. esac
  129. fi
  130. [ -n "$plmn" ] && {
  131. local mcc mnc
  132. if [ "$plmn" = 0 ]; then
  133. mcc=0
  134. mnc=0
  135. echo "Setting PLMN to auto"
  136. else
  137. mcc=${plmn:0:3}
  138. mnc=${plmn:3}
  139. echo "Setting PLMN to $plmn"
  140. fi
  141. uqmi -s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev/null 2>&1 || {
  142. echo "Unable to set PLMN"
  143. proto_notify_error "$interface" PLMN_FAILED
  144. proto_block_restart "$interface"
  145. return 1
  146. }
  147. }
  148. # Cleanup current state if any
  149. uqmi -s -d "$device" --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
  150. # Set IP format
  151. uqmi -s -d "$device" --set-data-format 802.3 > /dev/null 2>&1
  152. uqmi -s -d "$device" --wda-set-data-format 802.3 > /dev/null 2>&1
  153. dataformat="$(uqmi -s -d "$device" --wda-get-data-format)"
  154. if [ "$dataformat" = '"raw-ip"' ]; then
  155. [ -f /sys/class/net/$ifname/qmi/raw_ip ] || {
  156. echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
  157. return 1
  158. }
  159. echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
  160. echo "Y" > /sys/class/net/$ifname/qmi/raw_ip
  161. fi
  162. uqmi -s -d "$device" --sync > /dev/null 2>&1
  163. echo "Waiting for network registration"
  164. local registration_timeout=0
  165. while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
  166. [ -e "$device" ] || return 1
  167. if [ "$registration_timeout" -lt "$timeout" ]; then
  168. let registration_timeout++
  169. sleep 1;
  170. else
  171. echo "Network registration failed"
  172. proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
  173. proto_block_restart "$interface"
  174. return 1
  175. fi
  176. done
  177. [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
  178. echo "Starting network $interface"
  179. pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
  180. [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
  181. if [ "$pdptype" = "ip" ]; then
  182. [ -z "$autoconnect" ] && autoconnect=1
  183. [ "$autoconnect" = 0 ] && autoconnect=""
  184. else
  185. [ "$autoconnect" = 1 ] || autoconnect=""
  186. fi
  187. [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
  188. cid_4=$(uqmi -s -d "$device" --get-client-id wds)
  189. if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev/null; then
  190. echo "Unable to obtain client ID"
  191. proto_notify_error "$interface" NO_CID
  192. return 1
  193. fi
  194. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
  195. pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
  196. --start-network \
  197. ${apn:+--apn $apn} \
  198. ${profile:+--profile $profile} \
  199. ${auth:+--auth-type $auth} \
  200. ${username:+--username $username} \
  201. ${password:+--password $password} \
  202. ${autoconnect:+--autoconnect})
  203. # pdh_4 is a numeric value on success
  204. if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev/null; then
  205. echo "Unable to connect IPv4"
  206. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
  207. proto_notify_error "$interface" CALL_FAILED
  208. return 1
  209. fi
  210. # Check data connection state
  211. connstat=$(uqmi -s -d "$device" --get-data-status)
  212. [ "$connstat" == '"connected"' ] || {
  213. echo "No data link!"
  214. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
  215. proto_notify_error "$interface" CALL_FAILED
  216. return 1
  217. }
  218. }
  219. [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
  220. cid_6=$(uqmi -s -d "$device" --get-client-id wds)
  221. if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev/null; then
  222. echo "Unable to obtain client ID"
  223. proto_notify_error "$interface" NO_CID
  224. return 1
  225. fi
  226. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
  227. pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
  228. --start-network \
  229. ${apn:+--apn $apn} \
  230. ${profile:+--profile $profile} \
  231. ${auth:+--auth-type $auth} \
  232. ${username:+--username $username} \
  233. ${password:+--password $password} \
  234. ${autoconnect:+--autoconnect})
  235. # pdh_6 is a numeric value on success
  236. if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev/null; then
  237. echo "Unable to connect IPv6"
  238. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
  239. proto_notify_error "$interface" CALL_FAILED
  240. return 1
  241. fi
  242. # Check data connection state
  243. connstat=$(uqmi -s -d "$device" --get-data-status)
  244. [ "$connstat" == '"connected"' ] || {
  245. echo "No data link!"
  246. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
  247. proto_notify_error "$interface" CALL_FAILED
  248. return 1
  249. }
  250. }
  251. echo "Setting up $ifname"
  252. proto_init_update "$ifname" 1
  253. proto_set_keep 1
  254. proto_add_data
  255. [ -n "$pdh_4" ] && {
  256. json_add_string "cid_4" "$cid_4"
  257. json_add_string "pdh_4" "$pdh_4"
  258. }
  259. [ -n "$pdh_6" ] && {
  260. json_add_string "cid_6" "$cid_6"
  261. json_add_string "pdh_6" "$pdh_6"
  262. }
  263. proto_close_data
  264. proto_send_update "$interface"
  265. [ -n "$pdh_6" ] && {
  266. if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
  267. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
  268. json_select ipv6
  269. json_get_var ip_6 ip
  270. json_get_var gateway_6 gateway
  271. json_get_var dns1_6 dns1
  272. json_get_var dns2_6 dns2
  273. json_get_var ip_prefix_length ip-prefix-length
  274. proto_init_update "$ifname" 1
  275. proto_set_keep 1
  276. proto_add_ipv6_address "$ip_6" "128"
  277. proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
  278. proto_add_ipv6_route "$gateway_6" "128"
  279. [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
  280. [ "$peerdns" = 0 ] || {
  281. proto_add_dns_server "$dns1_6"
  282. proto_add_dns_server "$dns2_6"
  283. }
  284. proto_send_update "$interface"
  285. else
  286. json_init
  287. json_add_string name "${interface}_6"
  288. json_add_string ifname "@$interface"
  289. json_add_string proto "dhcpv6"
  290. [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
  291. proto_add_dynamic_defaults
  292. # RFC 7278: Extend an IPv6 /64 Prefix to LAN
  293. json_add_string extendprefix 1
  294. json_close_object
  295. ubus call network add_dynamic "$(json_dump)"
  296. fi
  297. }
  298. [ -n "$pdh_4" ] && {
  299. json_init
  300. json_add_string name "${interface}_4"
  301. json_add_string ifname "@$interface"
  302. json_add_string proto "dhcp"
  303. [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
  304. proto_add_dynamic_defaults
  305. json_close_object
  306. ubus call network add_dynamic "$(json_dump)"
  307. }
  308. }
  309. qmi_wds_stop() {
  310. local cid="$1"
  311. local pdh="$2"
  312. [ -n "$cid" ] || return
  313. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  314. --stop-network 0xffffffff \
  315. --autoconnect > /dev/null 2>&1
  316. [ -n "$pdh" ] && {
  317. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  318. --stop-network "$pdh" > /dev/null 2>&1
  319. }
  320. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  321. --release-client-id wds > /dev/null 2>&1
  322. }
  323. proto_qmi_teardown() {
  324. local interface="$1"
  325. local device cid_4 pdh_4 cid_6 pdh_6
  326. json_get_vars device
  327. [ -n "$ctl_device" ] && device=$ctl_device
  328. echo "Stopping network $interface"
  329. json_load "$(ubus call network.interface.$interface status)"
  330. json_select data
  331. json_get_vars cid_4 pdh_4 cid_6 pdh_6
  332. qmi_wds_stop "$cid_4" "$pdh_4"
  333. qmi_wds_stop "$cid_6" "$pdh_6"
  334. proto_init_update "*" 0
  335. proto_send_update "$interface"
  336. }
  337. [ -n "$INCLUDE_ONLY" ] || {
  338. add_protocol qmi
  339. }