qmi.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  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 v6apn
  13. proto_config_add_string auth
  14. proto_config_add_string username
  15. proto_config_add_string password
  16. proto_config_add_string pincode
  17. proto_config_add_int delay
  18. proto_config_add_string modes
  19. proto_config_add_string pdptype
  20. proto_config_add_int profile
  21. proto_config_add_int v6profile
  22. proto_config_add_boolean dhcp
  23. proto_config_add_boolean dhcpv6
  24. proto_config_add_boolean autoconnect
  25. proto_config_add_int plmn
  26. proto_config_add_int timeout
  27. proto_config_add_int mtu
  28. proto_config_add_defaults
  29. }
  30. proto_qmi_setup() {
  31. local interface="$1"
  32. local dataformat connstat plmn_mode mcc mnc
  33. local device apn v6apn auth username password pincode delay modes pdptype
  34. local profile v6profile dhcp dhcpv6 autoconnect plmn timeout mtu $PROTO_DEFAULT_OPTIONS
  35. local ip4table ip6table
  36. local cid_4 pdh_4 cid_6 pdh_6
  37. local ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
  38. json_get_vars device apn v6apn auth username password pincode delay modes
  39. json_get_vars pdptype profile v6profile dhcp dhcpv6 autoconnect plmn ip4table
  40. json_get_vars ip6table timeout mtu $PROTO_DEFAULT_OPTIONS
  41. [ "$timeout" = "" ] && timeout="10"
  42. [ "$metric" = "" ] && metric="0"
  43. [ -n "$ctl_device" ] && device=$ctl_device
  44. [ -n "$device" ] || {
  45. echo "No control device specified"
  46. proto_notify_error "$interface" NO_DEVICE
  47. proto_set_available "$interface" 0
  48. return 1
  49. }
  50. [ -n "$delay" ] && sleep "$delay"
  51. device="$(readlink -f $device)"
  52. [ -c "$device" ] || {
  53. echo "The specified control device does not exist"
  54. proto_notify_error "$interface" NO_DEVICE
  55. proto_set_available "$interface" 0
  56. return 1
  57. }
  58. devname="$(basename "$device")"
  59. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  60. ifname="$( ls "$devpath"/net )"
  61. [ -n "$ifname" ] || {
  62. echo "The interface could not be found."
  63. proto_notify_error "$interface" NO_IFACE
  64. proto_set_available "$interface" 0
  65. return 1
  66. }
  67. [ -n "$mtu" ] && {
  68. echo "Setting MTU to $mtu"
  69. /sbin/ip link set dev $ifname mtu $mtu
  70. }
  71. echo "Waiting for SIM initialization"
  72. local uninitialized_timeout=0
  73. # timeout 3s for first call to avoid hanging uqmi
  74. uqmi -d "$device" --get-pin-status -t 3000 > /dev/null 2>&1
  75. while uqmi -s -d "$device" --get-pin-status | grep '"UIM uninitialized"' > /dev/null; do
  76. [ -e "$device" ] || return 1
  77. if [ "$uninitialized_timeout" -lt "$timeout" -o "$timeout" = "0" ]; then
  78. let uninitialized_timeout++
  79. sleep 1;
  80. else
  81. echo "SIM not initialized"
  82. proto_notify_error "$interface" SIM_NOT_INITIALIZED
  83. proto_block_restart "$interface"
  84. return 1
  85. fi
  86. done
  87. if uqmi -s -d "$device" --uim-get-sim-state | grep -q '"Not supported"\|"Invalid QMI command"' &&
  88. uqmi -s -d "$device" --get-pin-status | grep -q '"Not supported"\|"Invalid QMI command"' ; then
  89. [ -n "$pincode" ] && {
  90. uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null || {
  91. echo "Unable to verify PIN"
  92. proto_notify_error "$interface" PIN_FAILED
  93. proto_block_restart "$interface"
  94. return 1
  95. }
  96. }
  97. else
  98. json_load "$(uqmi -s -d "$device" --get-pin-status)"
  99. json_get_var pin1_status pin1_status
  100. if [ -z "$pin1_status" ]; then
  101. json_load "$(uqmi -s -d "$device" --uim-get-sim-state)"
  102. json_get_var pin1_status pin1_status
  103. fi
  104. json_get_var pin1_verify_tries pin1_verify_tries
  105. case "$pin1_status" in
  106. disabled)
  107. echo "PIN verification is disabled"
  108. ;;
  109. blocked)
  110. echo "SIM locked PUK required"
  111. proto_notify_error "$interface" PUK_NEEDED
  112. proto_block_restart "$interface"
  113. return 1
  114. ;;
  115. not_verified)
  116. [ "$pin1_verify_tries" -lt "3" ] && {
  117. echo "PIN verify count value is $pin1_verify_tries this is below the limit of 3"
  118. proto_notify_error "$interface" PIN_TRIES_BELOW_LIMIT
  119. proto_block_restart "$interface"
  120. return 1
  121. }
  122. if [ -n "$pincode" ]; then
  123. uqmi -s -d "$device" --verify-pin1 "$pincode" > /dev/null 2>&1 || uqmi -s -d "$device" --uim-verify-pin1 "$pincode" > /dev/null 2>&1 || {
  124. echo "Unable to verify PIN"
  125. proto_notify_error "$interface" PIN_FAILED
  126. proto_block_restart "$interface"
  127. return 1
  128. }
  129. else
  130. echo "PIN not specified but required"
  131. proto_notify_error "$interface" PIN_NOT_SPECIFIED
  132. proto_block_restart "$interface"
  133. return 1
  134. fi
  135. ;;
  136. verified)
  137. echo "PIN already verified"
  138. ;;
  139. *)
  140. echo "PIN status failed (${pin1_status:-sim_not_present})"
  141. proto_notify_error "$interface" PIN_STATUS_FAILED
  142. proto_block_restart "$interface"
  143. return 1
  144. ;;
  145. esac
  146. json_cleanup
  147. fi
  148. if [ -n "$plmn" ]; then
  149. json_load "$(uqmi -s -d "$device" --get-plmn)"
  150. json_get_var plmn_mode mode
  151. json_get_vars mcc mnc || {
  152. mcc=0
  153. mnc=0
  154. }
  155. if [ "$plmn" = "0" ]; then
  156. if [ "$plmn_mode" != "automatic" ]; then
  157. mcc=0
  158. mnc=0
  159. echo "Setting PLMN to auto"
  160. fi
  161. elif [ "$mcc" -ne "${plmn:0:3}" -o "$mnc" -ne "${plmn:3}" ]; then
  162. mcc=${plmn:0:3}
  163. mnc=${plmn:3}
  164. echo "Setting PLMN to $plmn"
  165. else
  166. mcc=""
  167. mnc=""
  168. fi
  169. fi
  170. if [ -n "$mcc" -a -n "$mnc" ]; then
  171. uqmi -s -d "$device" --set-plmn --mcc "$mcc" --mnc "$mnc" > /dev/null 2>&1 || {
  172. echo "Unable to set PLMN"
  173. proto_notify_error "$interface" PLMN_FAILED
  174. proto_block_restart "$interface"
  175. return 1
  176. }
  177. fi
  178. # Cleanup current state if any
  179. uqmi -s -d "$device" --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
  180. uqmi -s -d "$device" --set-ip-family ipv6 --stop-network 0xffffffff --autoconnect > /dev/null 2>&1
  181. # Go online
  182. uqmi -s -d "$device" --set-device-operating-mode online > /dev/null 2>&1
  183. # Set IP format
  184. uqmi -s -d "$device" --set-data-format 802.3 > /dev/null 2>&1
  185. uqmi -s -d "$device" --wda-set-data-format 802.3 > /dev/null 2>&1
  186. dataformat="$(uqmi -s -d "$device" --wda-get-data-format)"
  187. if [ "$dataformat" = '"raw-ip"' ]; then
  188. [ -f /sys/class/net/$ifname/qmi/raw_ip ] || {
  189. echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
  190. return 1
  191. }
  192. echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
  193. echo "Y" > /sys/class/net/$ifname/qmi/raw_ip
  194. fi
  195. uqmi -s -d "$device" --sync > /dev/null 2>&1
  196. uqmi -s -d "$device" --network-register > /dev/null 2>&1
  197. echo "Waiting for network registration"
  198. sleep 1
  199. local registration_timeout=0
  200. local registration_state=""
  201. while true; do
  202. registration_state=$(uqmi -s -d "$device" --get-serving-system 2>/dev/null | jsonfilter -e "@.registration" 2>/dev/null)
  203. [ "$registration_state" = "registered" ] && break
  204. if [ "$registration_state" = "searching" ] || [ "$registration_state" = "not_registered" ]; then
  205. if [ "$registration_timeout" -lt "$timeout" ] || [ "$timeout" = "0" ]; then
  206. [ "$registration_state" = "searching" ] || {
  207. echo "Device stopped network registration. Restart network registration"
  208. uqmi -s -d "$device" --network-register > /dev/null 2>&1
  209. }
  210. let registration_timeout++
  211. sleep 1
  212. continue
  213. fi
  214. echo "Network registration failed, registration timeout reached"
  215. else
  216. # registration_state is 'registration_denied' or 'unknown' or ''
  217. echo "Network registration failed (reason: '$registration_state')"
  218. fi
  219. proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
  220. proto_block_restart "$interface"
  221. return 1
  222. done
  223. [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
  224. echo "Starting network $interface"
  225. pdptype="$(echo "$pdptype" | awk '{print tolower($0)}')"
  226. [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
  227. if [ "$pdptype" = "ip" ]; then
  228. [ -z "$autoconnect" ] && autoconnect=1
  229. [ "$autoconnect" = 0 ] && autoconnect=""
  230. else
  231. [ "$autoconnect" = 1 ] || autoconnect=""
  232. fi
  233. [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
  234. cid_4=$(uqmi -s -d "$device" --get-client-id wds)
  235. if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev/null; then
  236. echo "Unable to obtain client ID"
  237. proto_notify_error "$interface" NO_CID
  238. return 1
  239. fi
  240. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
  241. pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
  242. --start-network \
  243. ${apn:+--apn $apn} \
  244. ${profile:+--profile $profile} \
  245. ${auth:+--auth-type $auth} \
  246. ${username:+--username $username} \
  247. ${password:+--password $password} \
  248. ${autoconnect:+--autoconnect})
  249. # pdh_4 is a numeric value on success
  250. if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev/null; then
  251. echo "Unable to connect IPv4"
  252. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
  253. proto_notify_error "$interface" CALL_FAILED
  254. return 1
  255. fi
  256. # Check data connection state
  257. connstat=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" --get-data-status)
  258. [ "$connstat" == '"connected"' ] || {
  259. echo "No data link!"
  260. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
  261. proto_notify_error "$interface" CALL_FAILED
  262. return 1
  263. }
  264. }
  265. [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
  266. cid_6=$(uqmi -s -d "$device" --get-client-id wds)
  267. if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev/null; then
  268. echo "Unable to obtain client ID"
  269. proto_notify_error "$interface" NO_CID
  270. return 1
  271. fi
  272. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
  273. : "${v6apn:=${apn}}"
  274. : "${v6profile:=${profile}}"
  275. pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
  276. --start-network \
  277. ${v6apn:+--apn $v6apn} \
  278. ${v6profile:+--profile $v6profile} \
  279. ${auth:+--auth-type $auth} \
  280. ${username:+--username $username} \
  281. ${password:+--password $password} \
  282. ${autoconnect:+--autoconnect})
  283. # pdh_6 is a numeric value on success
  284. if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev/null; then
  285. echo "Unable to connect IPv6"
  286. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
  287. proto_notify_error "$interface" CALL_FAILED
  288. return 1
  289. fi
  290. # Check data connection state
  291. connstat=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 --get-data-status)
  292. [ "$connstat" == '"connected"' ] || {
  293. echo "No data link!"
  294. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
  295. proto_notify_error "$interface" CALL_FAILED
  296. return 1
  297. }
  298. }
  299. echo "Setting up $ifname"
  300. proto_init_update "$ifname" 1
  301. proto_set_keep 1
  302. proto_add_data
  303. [ -n "$pdh_4" ] && {
  304. json_add_string "cid_4" "$cid_4"
  305. json_add_string "pdh_4" "$pdh_4"
  306. }
  307. [ -n "$pdh_6" ] && {
  308. json_add_string "cid_6" "$cid_6"
  309. json_add_string "pdh_6" "$pdh_6"
  310. }
  311. proto_close_data
  312. proto_send_update "$interface"
  313. local zone="$(fw3 -q network "$interface" 2>/dev/null)"
  314. [ -n "$pdh_6" ] && {
  315. if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
  316. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
  317. json_select ipv6
  318. json_get_var ip_6 ip
  319. json_get_var gateway_6 gateway
  320. json_get_var dns1_6 dns1
  321. json_get_var dns2_6 dns2
  322. json_get_var ip_prefix_length ip-prefix-length
  323. proto_init_update "$ifname" 1
  324. proto_set_keep 1
  325. proto_add_ipv6_address "$ip_6" "128"
  326. proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
  327. proto_add_ipv6_route "$gateway_6" "128"
  328. [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
  329. [ "$peerdns" = 0 ] || {
  330. proto_add_dns_server "$dns1_6"
  331. proto_add_dns_server "$dns2_6"
  332. }
  333. [ -n "$zone" ] && {
  334. proto_add_data
  335. json_add_string zone "$zone"
  336. proto_close_data
  337. }
  338. proto_send_update "$interface"
  339. else
  340. json_init
  341. json_add_string name "${interface}_6"
  342. json_add_string ifname "@$interface"
  343. [ "$pdptype" = "ipv4v6" ] && json_add_string iface_464xlat "0"
  344. json_add_string proto "dhcpv6"
  345. [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
  346. proto_add_dynamic_defaults
  347. # RFC 7278: Extend an IPv6 /64 Prefix to LAN
  348. json_add_string extendprefix 1
  349. [ -n "$zone" ] && json_add_string zone "$zone"
  350. json_close_object
  351. ubus call network add_dynamic "$(json_dump)"
  352. fi
  353. }
  354. [ -n "$pdh_4" ] && {
  355. if [ "$dhcp" = 0 ]; then
  356. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
  357. json_select ipv4
  358. json_get_var ip_4 ip
  359. json_get_var gateway_4 gateway
  360. json_get_var dns1_4 dns1
  361. json_get_var dns2_4 dns2
  362. json_get_var subnet_4 subnet
  363. proto_init_update "$ifname" 1
  364. proto_set_keep 1
  365. proto_add_ipv4_address "$ip_4" "$subnet_4"
  366. proto_add_ipv4_route "$gateway_4" "128"
  367. [ "$defaultroute" = 0 ] || proto_add_ipv4_route "0.0.0.0" 0 "$gateway_4"
  368. [ "$peerdns" = 0 ] || {
  369. proto_add_dns_server "$dns1_4"
  370. proto_add_dns_server "$dns2_4"
  371. }
  372. [ -n "$zone" ] && {
  373. proto_add_data
  374. json_add_string zone "$zone"
  375. proto_close_data
  376. }
  377. proto_send_update "$interface"
  378. else
  379. json_init
  380. json_add_string name "${interface}_4"
  381. json_add_string ifname "@$interface"
  382. json_add_string proto "dhcp"
  383. [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
  384. proto_add_dynamic_defaults
  385. [ -n "$zone" ] && json_add_string zone "$zone"
  386. json_close_object
  387. ubus call network add_dynamic "$(json_dump)"
  388. fi
  389. }
  390. }
  391. qmi_wds_stop() {
  392. local cid="$1"
  393. local pdh="$2"
  394. [ -n "$cid" ] || return
  395. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  396. --stop-network 0xffffffff \
  397. --autoconnect > /dev/null 2>&1
  398. [ -n "$pdh" ] && {
  399. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  400. --stop-network "$pdh" > /dev/null 2>&1
  401. }
  402. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  403. --release-client-id wds > /dev/null 2>&1
  404. }
  405. proto_qmi_teardown() {
  406. local interface="$1"
  407. local device cid_4 pdh_4 cid_6 pdh_6
  408. json_get_vars device
  409. [ -n "$ctl_device" ] && device=$ctl_device
  410. echo "Stopping network $interface"
  411. json_load "$(ubus call network.interface.$interface status)"
  412. json_select data
  413. json_get_vars cid_4 pdh_4 cid_6 pdh_6
  414. qmi_wds_stop "$cid_4" "$pdh_4"
  415. qmi_wds_stop "$cid_6" "$pdh_6"
  416. proto_init_update "*" 0
  417. proto_send_update "$interface"
  418. }
  419. [ -n "$INCLUDE_ONLY" ] || {
  420. add_protocol qmi
  421. }