directip.sh 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/bin/sh
  2. [ -n "$INCLUDE_ONLY" ] || {
  3. . /lib/functions.sh
  4. . ../netifd-proto.sh
  5. init_proto "$@"
  6. }
  7. proto_directip_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 "pincode"
  13. proto_config_add_string "auth"
  14. proto_config_add_string "username"
  15. proto_config_add_string "password"
  16. }
  17. proto_directip_setup() {
  18. local interface="$1"
  19. local chat devpath devname
  20. local device apn pincode ifname auth username password
  21. json_get_vars device apn pincode auth username password
  22. [ -n "$ctl_device" ] && device=$ctl_device
  23. [ -e "$device" ] || {
  24. proto_notify_error "$interface" NO_DEVICE
  25. proto_set_available "$interface" 0
  26. return 1
  27. }
  28. devname="$(basename "$device")"
  29. devpath="$(readlink -f /sys/class/tty/$devname/device)"
  30. ifname="$( ls "$devpath"/../../*/net )"
  31. [ -n "$ifname" ] || {
  32. proto_notify_error "$interface" NO_IFNAME
  33. proto_set_available "$interface" 0
  34. return 1
  35. }
  36. cardinfo=$(gcom -d "$device" -s /etc/gcom/getcardinfo.gcom)
  37. [ -n $(echo "$cardinfo" | grep -q "Sierra Wireless") ] || {
  38. proto_notify_error "$interface" BAD_DEVICE
  39. proto_block_restart "$interface"
  40. return 1
  41. }
  42. if [ -n "$pincode" ]; then
  43. PINCODE="$pincode" gcom -d "$device" -s /etc/gcom/setpin.gcom || {
  44. proto_notify_error "$interface" PIN_FAILED
  45. proto_block_restart "$interface"
  46. return 1
  47. }
  48. fi
  49. # wait for carrier to avoid firmware stability bugs
  50. gcom -d "$device" -s /etc/gcom/getcarrier.gcom || return 1
  51. local auth_type=0
  52. case $auth in
  53. pap) auth_type=1;;
  54. chap) auth_type=2;;
  55. esac
  56. USE_APN="$apn" USE_USER="$username" USE_PASS="$password" USE_AUTH="$auth_type" \
  57. gcom -d "$device" -s /etc/gcom/directip.gcom || {
  58. proto_notify_error "$interface" CONNECT_FAILED
  59. proto_block_restart "$interface"
  60. return 1
  61. }
  62. logger -p daemon.info -t "directip[$$]" "Connected, starting DHCP"
  63. proto_init_update "$ifname" 1
  64. proto_send_update "$interface"
  65. json_init
  66. json_add_string name "${interface}_4"
  67. json_add_string ifname "@$interface"
  68. json_add_string proto "dhcp"
  69. ubus call network add_dynamic "$(json_dump)"
  70. json_init
  71. json_add_string name "${interface}_6"
  72. json_add_string ifname "@$interface"
  73. json_add_string proto "dhcpv6"
  74. ubus call network add_dynamic "$(json_dump)"
  75. return 0
  76. }
  77. proto_directip_teardown() {
  78. local interface="$1"
  79. local device
  80. json_get_vars device
  81. [ -n "$ctl_device" ] && device=$ctl_device
  82. gcom -d "$device" -s /etc/gcom/directip-stop.gcom || proto_notify_error "$interface" CONNECT_FAILED
  83. proto_init_update "*" 0
  84. proto_send_update "$interface"
  85. }
  86. [ -n "$INCLUDE_ONLY" ] || {
  87. add_protocol directip
  88. }