qmi.sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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_string delay
  17. proto_config_add_string modes
  18. proto_config_add_boolean ipv6
  19. proto_config_add_boolean dhcp
  20. }
  21. proto_qmi_setup() {
  22. local interface="$1"
  23. local device apn auth username password pincode delay modes ipv6 dhcp
  24. local cid_4 pdh_4 cid_6 pdh_6 ipv4
  25. local ip subnet gateway dns1 dns2 ip_6 ip_prefix_length gateway_6 dns1_6 dns2_6
  26. json_get_vars device apn auth username password pincode delay modes ipv6 dhcp
  27. ipv4=1
  28. if [ "$ipv6" = 0 ]; then
  29. ipv6=""
  30. else
  31. ipv6=1
  32. fi
  33. [ -n "$ctl_device" ] && device=$ctl_device
  34. [ -n "$device" ] || {
  35. echo "No control device specified"
  36. proto_notify_error "$interface" NO_DEVICE
  37. proto_set_available "$interface" 0
  38. return 1
  39. }
  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 "$apn" ] || {
  68. echo "No APN specified"
  69. proto_notify_error "$interface" NO_APN
  70. return 1
  71. }
  72. uqmi -s -d "$device" --set-data-format 802.3
  73. uqmi -s -d "$device" --wda-set-data-format 802.3
  74. echo "Waiting for network registration"
  75. while uqmi -s -d "$device" --get-serving-system | grep '"searching"' > /dev/null; do
  76. sleep 5;
  77. done
  78. [ -n "$modes" ] && uqmi -s -d "$device" --set-network-modes "$modes"
  79. echo "Starting network $apn"
  80. cid_4=`uqmi -s -d "$device" --get-client-id wds`
  81. [ $? -ne 0 ] && {
  82. echo "Unable to obtain client ID"
  83. proto_notify_error "$interface" NO_CID
  84. return 1
  85. }
  86. pdh_4=`uqmi -s -d "$device" --set-client-id wds,"$cid_4" \
  87. --start-network "$apn" \
  88. ${auth:+--auth-type $auth} \
  89. ${username:+--username $username} \
  90. ${password:+--password $password} \
  91. --ip-family ipv4`
  92. [ $? -ne 0 ] && {
  93. echo "Unable to connect IPv4"
  94. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
  95. ipv4=""
  96. }
  97. [ -n "$ipv6" ] && {
  98. cid_6=`uqmi -s -d "$device" --get-client-id wds`
  99. if [ $? = 0 ]; then
  100. pdh_6=`uqmi -s -d "$device" --set-client-id wds,"$cid_6" \
  101. --start-network "$apn" \
  102. ${auth:+--auth-type $auth} \
  103. ${username:+--username $username} \
  104. ${password:+--password $password} \
  105. --ip-family ipv6`
  106. [ $? -ne 0 ] && {
  107. echo "Unable to connect IPv6"
  108. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
  109. ipv6=""
  110. }
  111. else
  112. echo "Unable to connect IPv6"
  113. ipv6=""
  114. fi
  115. }
  116. [ -z "$ipv4" -a -z "$ipv6" ] && {
  117. echo "Unable to connect"
  118. proto_notify_error "$interface" CALL_FAILED
  119. return 1
  120. }
  121. if [ -z "$dhcp" -o "$dhcp" = 0 ]; then
  122. echo "Setting up $ifname"
  123. [ -n "$ipv4" ] && {
  124. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_4 --get-current-settings)"
  125. json_select ipv4
  126. json_get_vars ip subnet gateway dns1 dns2
  127. proto_init_update "$ifname" 1
  128. proto_set_keep 1
  129. proto_add_ipv4_address "$ip" "$subnet"
  130. proto_add_dns_server "$dns1"
  131. proto_add_dns_server "$dns2"
  132. proto_add_ipv4_route "0.0.0.0" 0 "$gateway"
  133. proto_add_data
  134. json_add_string "cid_4" "$cid_4"
  135. json_add_string "pdh_4" "$pdh_4"
  136. proto_close_data
  137. proto_send_update "$interface"
  138. }
  139. [ -n "$ipv6" ] && {
  140. json_load "$(uqmi -s -d $device --set-client-id wds,$cid_6 --get-current-settings)"
  141. json_select ipv6
  142. json_get_var ip_6 ip
  143. json_get_var gateway_6 gateway
  144. json_get_var dns1_6 dns1
  145. json_get_var dns2_6 dns2
  146. json_get_var ip_prefix_length ip-prefix-length
  147. proto_init_update "$ifname" 1
  148. proto_set_keep 1
  149. # RFC 7278: Extend an IPv6 /64 Prefix to LAN
  150. proto_add_ipv6_address "$ip_6" "128"
  151. proto_add_ipv6_prefix "${ip_6}/${ip_prefix_length}"
  152. proto_add_ipv6_route "$gateway_6" "128"
  153. proto_add_ipv6_route "::0" 0 "$gateway_6" "" "" "${ip_6}/${ip_prefix_length}"
  154. proto_add_dns_server "$dns1_6"
  155. proto_add_dns_server "$dns2_6"
  156. proto_add_data
  157. json_add_string "cid_6" "$cid_6"
  158. json_add_string "pdh_6" "$pdh_6"
  159. proto_close_data
  160. proto_send_update "$interface"
  161. }
  162. else
  163. echo "Starting DHCP on $ifname"
  164. proto_init_update "$ifname" 1
  165. proto_add_data
  166. [ -n "$ipv4" ] && {
  167. json_add_string "cid_4" "$cid_4"
  168. json_add_string "pdh_4" "$pdh_4"
  169. }
  170. [ -n "$ipv6" ] && {
  171. json_add_string "cid_6" "$cid_6"
  172. json_add_string "pdh_6" "$pdh_6"
  173. }
  174. proto_close_data
  175. proto_send_update "$interface"
  176. [ -n "$ipv4" ] && {
  177. json_init
  178. json_add_string name "${interface}_4"
  179. json_add_string ifname "@$interface"
  180. json_add_string proto "dhcp"
  181. json_close_object
  182. ubus call network add_dynamic "$(json_dump)"
  183. }
  184. [ -n "$ipv6" ] && {
  185. json_init
  186. json_add_string name "${interface}_6"
  187. json_add_string ifname "@$interface"
  188. json_add_string proto "dhcpv6"
  189. # RFC 7278: Extend an IPv6 /64 Prefix to LAN
  190. json_add_string extendprefix 1
  191. json_close_object
  192. ubus call network add_dynamic "$(json_dump)"
  193. }
  194. fi
  195. }
  196. proto_qmi_teardown() {
  197. local interface="$1"
  198. local device cid_4 pdh_4 cid_6 pdh_6
  199. json_get_vars device
  200. [ -n "$ctl_device" ] && device=$ctl_device
  201. echo "Stopping network"
  202. json_load "$(ubus call network.interface.$interface status)"
  203. json_select data
  204. json_get_vars cid_4 pdh_4 cid_6 pdh_6
  205. [ -n "$cid_4" ] && {
  206. [ -n "$pdh_4" ] && {
  207. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --stop-network "$pdh_4"
  208. uqmi -s -d "$device" --set-client-id wds,"$cid_4" --release-client-id wds
  209. }
  210. }
  211. [ -n "$cid_6" ] && {
  212. [ -n "$pdh_6" ] && {
  213. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --stop-network "$pdh_6"
  214. uqmi -s -d "$device" --set-client-id wds,"$cid_6" --release-client-id wds
  215. }
  216. }
  217. proto_init_update "*" 0
  218. proto_send_update "$interface"
  219. }
  220. [ -n "$INCLUDE_ONLY" ] || {
  221. add_protocol qmi
  222. }