qmi.sh 12 KB

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