ncm.sh 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/bin/sh
  2. [ -n "$INCLUDE_ONLY" ] || {
  3. . /lib/functions.sh
  4. . ../netifd-proto.sh
  5. init_proto "$@"
  6. }
  7. proto_ncm_init_config() {
  8. no_device=1
  9. available=1
  10. proto_config_add_string "device:device"
  11. proto_config_add_string ifname
  12. proto_config_add_string apn
  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_string delay
  18. proto_config_add_string mode
  19. proto_config_add_string pdptype
  20. proto_config_add_boolean sourcefilter
  21. proto_config_add_boolean delegate
  22. proto_config_add_int profile
  23. proto_config_add_int mtu
  24. proto_config_add_defaults
  25. }
  26. proto_ncm_setup() {
  27. local interface="$1"
  28. local connect context_type devname devpath finalize ifpath initialize manufacturer setmode
  29. local delegate ip4table ip6table mtu sourcefilter $PROTO_DEFAULT_OPTIONS
  30. json_get_vars delegate ip4table ip6table mtu sourcefilter $PROTO_DEFAULT_OPTIONS
  31. local apn auth delay device ifname mode password pdptype pincode profile username
  32. json_get_vars apn auth delay device ifname mode password pdptype pincode profile username
  33. [ "$metric" = "" ] && metric="0"
  34. [ -n "$profile" ] || profile=1
  35. pdptype=$(echo "$pdptype" | awk '{print toupper($0)}')
  36. [ "$pdptype" = "IP" -o "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] || pdptype="IP"
  37. [ "$pdptype" = "IPV4V6" ] && context_type=3
  38. [ -z "$context_type" -a "$pdptype" = "IPV6" ] && context_type=2
  39. [ -n "$context_type" ] || context_type=1
  40. [ -n "$ctl_device" ] && device=$ctl_device
  41. [ -n "$device" ] || {
  42. echo "No control device specified"
  43. proto_notify_error "$interface" NO_DEVICE
  44. proto_set_available "$interface" 0
  45. return 1
  46. }
  47. device="$(readlink -f $device)"
  48. [ -e "$device" ] || {
  49. echo "Control device not valid"
  50. proto_set_available "$interface" 0
  51. return 1
  52. }
  53. [ -z "$ifname" ] && {
  54. devname="$(basename "$device")"
  55. case "$devname" in
  56. 'ttyACM'*)
  57. devpath="$(readlink -f /sys/class/tty/$devname/device)"
  58. ifpath="$devpath/../*/net"
  59. ;;
  60. 'tty'*)
  61. devpath="$(readlink -f /sys/class/tty/$devname/device)"
  62. ifpath="$devpath/../../*/net"
  63. ;;
  64. *)
  65. devpath="$(readlink -f /sys/class/usbmisc/$devname/device/)"
  66. ifpath="$devpath/net"
  67. ;;
  68. esac
  69. ifname="$(ls $(ls -1 -d $ifpath | head -n 1))"
  70. }
  71. [ -n "$ifname" ] || {
  72. echo "The interface could not be found."
  73. proto_notify_error "$interface" NO_IFACE
  74. proto_set_available "$interface" 0
  75. return 1
  76. }
  77. start=$(date +%s)
  78. while true; do
  79. manufacturer=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk -v RS='\r?\n' 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }')
  80. [ "$manufacturer" = "error" ] && {
  81. manufacturer=""
  82. }
  83. [ -n "$manufacturer" ] && {
  84. break
  85. }
  86. [ -z "$delay" ] && {
  87. break
  88. }
  89. sleep 1
  90. elapsed=$(($(date +%s) - start))
  91. [ "$elapsed" -gt "$delay" ] && {
  92. break
  93. }
  94. done
  95. [ -z "$manufacturer" ] && {
  96. echo "Failed to get modem information"
  97. proto_notify_error "$interface" GETINFO_FAILED
  98. return 1
  99. }
  100. json_load "$(cat /etc/gcom/ncm.json)"
  101. json_select "$manufacturer"
  102. [ $? -ne 0 ] && {
  103. echo "Unsupported modem"
  104. proto_notify_error "$interface" UNSUPPORTED_MODEM
  105. proto_set_available "$interface" 0
  106. return 1
  107. }
  108. json_get_values initialize initialize
  109. for i in $initialize; do
  110. eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  111. echo "Failed to initialize modem"
  112. proto_notify_error "$interface" INITIALIZE_FAILED
  113. return 1
  114. }
  115. done
  116. [ -n "$pincode" ] && {
  117. PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
  118. echo "Unable to verify PIN"
  119. proto_notify_error "$interface" PIN_FAILED
  120. proto_block_restart "$interface"
  121. return 1
  122. }
  123. }
  124. json_get_values configure configure
  125. echo "Configuring modem"
  126. for i in $configure; do
  127. eval COMMAND="$i" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  128. echo "Failed to configure modem"
  129. proto_notify_error "$interface" CONFIGURE_FAILED
  130. return 1
  131. }
  132. done
  133. [ -n "$mode" ] && {
  134. json_select modes
  135. json_get_var setmode "$mode"
  136. [ -n "$setmode" ] && {
  137. echo "Setting mode"
  138. eval COMMAND="$setmode" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  139. echo "Failed to set operating mode"
  140. proto_notify_error "$interface" SETMODE_FAILED
  141. return 1
  142. }
  143. }
  144. json_select ..
  145. }
  146. echo "Starting network $interface"
  147. json_get_vars connect
  148. [ -n "$connect" ] && {
  149. echo "Connecting modem"
  150. eval COMMAND="$connect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  151. echo "Failed to connect"
  152. proto_notify_error "$interface" CONNECT_FAILED
  153. return 1
  154. }
  155. }
  156. json_get_vars finalize
  157. echo "Setting up $ifname"
  158. proto_init_update "$ifname" 1
  159. proto_add_data
  160. json_add_string "manufacturer" "$manufacturer"
  161. proto_close_data
  162. proto_send_update "$interface"
  163. local zone="$(fw3 -q network "$interface" 2>/dev/null)"
  164. [ "$pdptype" = "IP" -o "$pdptype" = "IPV4V6" ] && {
  165. json_init
  166. json_add_string name "${interface}_4"
  167. json_add_string ifname "@$interface"
  168. json_add_string proto "dhcp"
  169. proto_add_dynamic_defaults
  170. [ -n "$zone" ] && json_add_string zone "$zone"
  171. [ -n "$ip4table" ] && json_add_string ip4table "$ip4table"
  172. json_close_object
  173. ubus call network add_dynamic "$(json_dump)"
  174. }
  175. [ "$pdptype" = "IPV6" -o "$pdptype" = "IPV4V6" ] && {
  176. json_init
  177. json_add_string name "${interface}_6"
  178. json_add_string ifname "@$interface"
  179. json_add_string proto "dhcpv6"
  180. json_add_string extendprefix 1
  181. [ "$delegate" = "0" ] && json_add_boolean delegate "0"
  182. [ "$sourcefilter" = "0" ] && json_add_boolean sourcefilter "0"
  183. proto_add_dynamic_defaults
  184. [ -n "$zone" ] && json_add_string zone "$zone"
  185. [ -n "$ip6table" ] && json_add_string ip6table "$ip6table"
  186. json_close_object
  187. ubus call network add_dynamic "$(json_dump)"
  188. }
  189. [ -n "$mtu" -a "$mtu" != 0 ] && {
  190. echo "Setting MTU of $ifname to $mtu"
  191. /sbin/ip link set dev $ifname mtu $mtu
  192. }
  193. [ -n "$finalize" ] && {
  194. eval COMMAND="$finalize" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  195. echo "Failed to configure modem"
  196. proto_notify_error "$interface" FINALIZE_FAILED
  197. return 1
  198. }
  199. }
  200. }
  201. proto_ncm_teardown() {
  202. local interface="$1"
  203. local manufacturer disconnect
  204. local device profile
  205. json_get_vars device profile
  206. [ -n "$ctl_device" ] && device=$ctl_device
  207. [ -n "$device" ] || {
  208. echo "No control device specified"
  209. proto_notify_error "$interface" NO_DEVICE
  210. proto_set_available "$interface" 0
  211. return 1
  212. }
  213. device="$(readlink -f $device)"
  214. [ -e "$device" ] || {
  215. echo "Control device not valid"
  216. proto_set_available "$interface" 0
  217. return 1
  218. }
  219. [ -n "$profile" ] || profile=1
  220. echo "Stopping network $interface"
  221. json_load "$(ubus call network.interface.$interface status)"
  222. json_select data
  223. json_get_vars manufacturer
  224. [ $? -ne 0 -o -z "$manufacturer" ] && {
  225. # Fallback to direct detect, for proper handle device replug.
  226. manufacturer=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom | awk 'NF && $0 !~ /AT\+CGMI/ { sub(/\+CGMI: /,""); print tolower($1); exit; }')
  227. [ $? -ne 0 -o -z "$manufacturer" ] && {
  228. echo "Failed to get modem information"
  229. proto_notify_error "$interface" GETINFO_FAILED
  230. return 1
  231. }
  232. json_add_string "manufacturer" "$manufacturer"
  233. }
  234. json_load "$(cat /etc/gcom/ncm.json)"
  235. json_select "$manufacturer" || {
  236. echo "Unsupported modem"
  237. proto_notify_error "$interface" UNSUPPORTED_MODEM
  238. return 1
  239. }
  240. json_get_vars disconnect
  241. [ -n "$disconnect" ] && {
  242. eval COMMAND="$disconnect" gcom -d "$device" -s /etc/gcom/runcommand.gcom || {
  243. echo "Failed to disconnect"
  244. proto_notify_error "$interface" DISCONNECT_FAILED
  245. return 1
  246. }
  247. }
  248. proto_init_update "*" 0
  249. proto_send_update "$interface"
  250. }
  251. [ -n "$INCLUDE_ONLY" ] || {
  252. add_protocol ncm
  253. }