2
0

qmi.sh 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  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. # Go online
  160. uqmi -s -d "$device" --set-device-operating-mode online > /dev/null 2>&1
  161. # Set IP format
  162. uqmi -s -d "$device" --set-data-format 802.3 > /dev/null 2>&1
  163. uqmi -s -d "$device" --wda-set-data-format 802.3 > /dev/null 2>&1
  164. dataformat="$(uqmi -s -d "$device" --wda-get-data-format)"
  165. if [ "$dataformat" = '"raw-ip"' ]; then
  166. [ -f /sys/class/net/$ifname/qmi/raw_ip ] || {
  167. echo "Device only supports raw-ip mode but is missing this required driver attribute: /sys/class/net/$ifname/qmi/raw_ip"
  168. return 1
  169. }
  170. echo "Device does not support 802.3 mode. Informing driver of raw-ip only for $ifname .."
  171. echo "Y" > /sys/class/net/$ifname/qmi/raw_ip
  172. fi
  173. uqmi -s -d "$device" --sync > /dev/null 2>&1
  174. echo "Waiting for network registration"
  175. local registration_timeout=0
  176. while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
  177. [ -e "$device" ] || return 1
  178. if [ "$registration_timeout" -lt "$timeout" -o "$timeout" = "0" ]; then
  179. let registration_timeout++
  180. sleep 1;
  181. else
  182. echo "Network registration failed"
  183. proto_notify_error "$interface" NETWORK_REGISTRATION_FAILED
  184. proto_block_restart "$interface"
  185. return 1
  186. fi
  187. done
  188. [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes" > /dev/null 2>&1
  189. echo "Starting network $interface"
  190. pdptype=$(echo "$pdptype" | awk '{print tolower($0)}')
  191. [ "$pdptype" = "ip" -o "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] || pdptype="ip"
  192. if [ "$pdptype" = "ip" ]; then
  193. [ -z "$autoconnect" ] && autoconnect=1
  194. [ "$autoconnect" = 0 ] && autoconnect=""
  195. else
  196. [ "$autoconnect" = 1 ] || autoconnect=""
  197. fi
  198. [ "$pdptype" = "ip" -o "$pdptype" = "ipv4v6" ] && {
  199. cid_4=$(uqmi -s -d "$device" --get-client-id wds)
  200. if ! [ "$cid_4" -eq "$cid_4" ] 2> /dev/null; then
  201. echo "Unable to obtain client ID"
  202. proto_notify_error "$interface" NO_CID
  203. return 1
  204. fi
  205. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --set-ip-family ipv4 > /dev/null 2>&1
  206. pdh_4=$(uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
  207. --start-network \
  208. ${apn:+--apn $apn} \
  209. ${profile:+--profile $profile} \
  210. ${auth:+--auth-type $auth} \
  211. ${username:+--username $username} \
  212. ${password:+--password $password} \
  213. ${autoconnect:+--autoconnect})
  214. # pdh_4 is a numeric value on success
  215. if ! [ "$pdh_4" -eq "$pdh_4" ] 2> /dev/null; then
  216. echo "Unable to connect IPv4"
  217. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
  218. proto_notify_error "$interface" CALL_FAILED
  219. return 1
  220. fi
  221. # Check data connection state
  222. connstat=$(uqmi -s -d "$device" --get-data-status)
  223. [ "$connstat" == '"connected"' ] || {
  224. echo "No data link!"
  225. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds > /dev/null 2>&1
  226. proto_notify_error "$interface" CALL_FAILED
  227. return 1
  228. }
  229. }
  230. [ "$pdptype" = "ipv6" -o "$pdptype" = "ipv4v6" ] && {
  231. cid_6=$(uqmi -s -d "$device" --get-client-id wds)
  232. if ! [ "$cid_6" -eq "$cid_6" ] 2> /dev/null; then
  233. echo "Unable to obtain client ID"
  234. proto_notify_error "$interface" NO_CID
  235. return 1
  236. fi
  237. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --set-ip-family ipv6 > /dev/null 2>&1
  238. pdh_6=$(uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
  239. --start-network \
  240. ${apn:+--apn $apn} \
  241. ${profile:+--profile $profile} \
  242. ${auth:+--auth-type $auth} \
  243. ${username:+--username $username} \
  244. ${password:+--password $password} \
  245. ${autoconnect:+--autoconnect})
  246. # pdh_6 is a numeric value on success
  247. if ! [ "$pdh_6" -eq "$pdh_6" ] 2> /dev/null; then
  248. echo "Unable to connect IPv6"
  249. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
  250. proto_notify_error "$interface" CALL_FAILED
  251. return 1
  252. fi
  253. # Check data connection state
  254. connstat=$(uqmi -s -d "$device" --get-data-status)
  255. [ "$connstat" == '"connected"' ] || {
  256. echo "No data link!"
  257. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds > /dev/null 2>&1
  258. proto_notify_error "$interface" CALL_FAILED
  259. return 1
  260. }
  261. }
  262. echo "Setting up $ifname"
  263. proto_init_update "$ifname" 1
  264. proto_set_keep 1
  265. proto_add_data
  266. [ -n "$pdh_4" ] && {
  267. json_add_string "cid_4" "$cid_4"
  268. json_add_string "pdh_4" "$pdh_4"
  269. }
  270. [ -n "$pdh_6" ] && {
  271. json_add_string "cid_6" "$cid_6"
  272. json_add_string "pdh_6" "$pdh_6"
  273. }
  274. proto_close_data
  275. proto_send_update "$interface"
  276. local zone="$(fw3 -q network "$interface" 2>/dev/null)"
  277. [ -n "$pdh_6" ] && {
  278. if [ -z "$dhcpv6" -o "$dhcpv6" = 0 ]; then
  279. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
  280. json_select ipv6
  281. json_get_var ip_6 ip
  282. json_get_var gateway_6 gateway
  283. json_get_var dns1_6 dns1
  284. json_get_var dns2_6 dns2
  285. json_get_var ip_prefix_length ip-prefix-length
  286. proto_init_update "$ifname" 1
  287. proto_set_keep 1
  288. proto_add_ipv6_address "$ip_6" "128"
  289. proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
  290. proto_add_ipv6_route "$gateway_6" "128"
  291. [ "$defaultroute" = 0 ] || proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
  292. [ "$peerdns" = 0 ] || {
  293. proto_add_dns_server "$dns1_6"
  294. proto_add_dns_server "$dns2_6"
  295. }
  296. [ -n "$zone" ] && {
  297. proto_add_data
  298. json_add_string zone "$zone"
  299. proto_close_data
  300. }
  301. proto_send_update "$interface"
  302. else
  303. json_init
  304. json_add_string name "${interface}_6"
  305. json_add_string ifname "@$interface"
  306. json_add_string proto "dhcpv6"
  307. [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
  308. proto_add_dynamic_defaults
  309. # RFC 7278: Extend an IPv6 /64 Prefix to LAN
  310. json_add_string extendprefix 1
  311. [ -n "$zone" ] && json_add_string zone "$zone"
  312. json_close_object
  313. ubus call network add_dynamic "$(json_dump)"
  314. fi
  315. }
  316. [ -n "$pdh_4" ] && {
  317. if [ "$dhcp" = 0 ]; then
  318. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
  319. json_select ipv4
  320. json_get_var ip_4 ip
  321. json_get_var gateway_4 gateway
  322. json_get_var dns1_4 dns1
  323. json_get_var dns2_4 dns2
  324. json_get_var subnet_4 subnet
  325. proto_init_update "$ifname" 1
  326. proto_set_keep 1
  327. proto_add_ipv4_address "$ip_4" "$subnet_4"
  328. proto_add_ipv4_route "$gateway_4" "128"
  329. [ "$defaultroute" = 0 ] || proto_add_ipv4_route "0.0.0.0" 0 "$gateway_4"
  330. [ "$peerdns" = 0 ] || {
  331. proto_add_dns_server "$dns1_4"
  332. proto_add_dns_server "$dns2_4"
  333. }
  334. [ -n "$zone" ] && {
  335. proto_add_data
  336. json_add_string zone "$zone"
  337. proto_close_data
  338. }
  339. proto_send_update "$interface"
  340. else
  341. json_init
  342. json_add_string name "${interface}_4"
  343. json_add_string ifname "@$interface"
  344. json_add_string proto "dhcp"
  345. [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
  346. proto_add_dynamic_defaults
  347. [ -n "$zone" ] && json_add_string zone "$zone"
  348. json_close_object
  349. ubus call network add_dynamic "$(json_dump)"
  350. fi
  351. }
  352. }
  353. qmi_wds_stop() {
  354. local cid="$1"
  355. local pdh="$2"
  356. [ -n "$cid" ] || return
  357. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  358. --stop-network 0xffffffff \
  359. --autoconnect > /dev/null 2>&1
  360. [ -n "$pdh" ] && {
  361. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  362. --stop-network "$pdh" > /dev/null 2>&1
  363. }
  364. uqmi -s -d "$device" --set-client-id wds,"$cid" \
  365. --release-client-id wds > /dev/null 2>&1
  366. }
  367. proto_qmi_teardown() {
  368. local interface="$1"
  369. local device cid_4 pdh_4 cid_6 pdh_6
  370. json_get_vars device
  371. [ -n "$ctl_device" ] && device=$ctl_device
  372. echo "Stopping network $interface"
  373. json_load "$(ubus call network.interface.$interface status)"
  374. json_select data
  375. json_get_vars cid_4 pdh_4 cid_6 pdh_6
  376. qmi_wds_stop "$cid_4" "$pdh_4"
  377. qmi_wds_stop "$cid_6" "$pdh_6"
  378. proto_init_update "*" 0
  379. proto_send_update "$interface"
  380. }
  381. [ -n "$INCLUDE_ONLY" ] || {
  382. add_protocol qmi
  383. }