You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

500 lines
15 KiB

  1. #!/bin/sh
  2. # Copyright (C) 2016-2019 Aleksander Morgado <aleksander@aleksander.es>
  3. [ -x /usr/bin/mmcli ] || exit 0
  4. [ -x /usr/sbin/pppd ] || exit 0
  5. [ -n "$INCLUDE_ONLY" ] || {
  6. . /lib/functions.sh
  7. . ../netifd-proto.sh
  8. . ./ppp.sh
  9. init_proto "$@"
  10. }
  11. cdr2mask ()
  12. {
  13. # Number of args to shift, 255..255, first non-255 byte, zeroes
  14. set -- $(( 5 - ($1 / 8) )) 255 255 255 255 $(( (255 << (8 - ($1 % 8))) & 255 )) 0 0 0
  15. if [ "$1" -gt 1 ]
  16. then
  17. shift "$1"
  18. else
  19. shift
  20. fi
  21. echo "${1-0}"."${2-0}"."${3-0}"."${4-0}"
  22. }
  23. # This method expects as first argument a list of key-value pairs, as returned by mmcli --output-keyvalue
  24. # The second argument must be exactly the name of the field to read
  25. #
  26. # Sample output:
  27. # $ mmcli -m 0 -K
  28. # modem.dbus-path : /org/freedesktop/ModemManager1/Modem/0
  29. # modem.generic.device-identifier : ed6eff2e3e0f90463da1c2a755b2acacd1335752
  30. # modem.generic.manufacturer : Dell Inc.
  31. # modem.generic.model : DW5821e Snapdragon X20 LTE
  32. # modem.generic.revision : T77W968.F1.0.0.4.0.GC.009\n026
  33. # modem.generic.carrier-configuration : GCF
  34. # modem.generic.carrier-configuration-revision : 08E00009
  35. # modem.generic.hardware-revision : DW5821e Snapdragon X20 LTE
  36. # ....
  37. modemmanager_get_field() {
  38. local list=$1
  39. local field=$2
  40. local value=""
  41. [ -z "${list}" ] || [ -z "${field}" ] && return
  42. # there is always at least a whitespace after each key, and we use that as part of the
  43. # key matching we do (e.g. to avoid getting 'modem.generic.state-failed-reason' as a result
  44. # when grepping for 'modem.generic.state'.
  45. line=$(echo "${list}" | grep "${field} ")
  46. value=$(echo ${line#*:})
  47. # not found?
  48. [ -n "${value}" ] || return 2
  49. # only print value if set
  50. [ "${value}" != "--" ] && echo "${value}"
  51. return 0
  52. }
  53. # build a comma-separated list of values from the list
  54. modemmanager_get_multivalue_field() {
  55. local list=$1
  56. local field=$2
  57. local value=""
  58. local length idx item
  59. [ -z "${list}" ] || [ -z "${field}" ] && return
  60. length=$(modemmanager_get_field "${list}" "${field}.length")
  61. [ -n "${length}" ] || return 0
  62. [ "$length" -ge 1 ] || return 0
  63. idx=1
  64. while [ $idx -le "$length" ]; do
  65. item=$(modemmanager_get_field "${list}" "${field}.value\[$idx\]")
  66. [ -n "${item}" ] && [ "${item}" != "--" ] && {
  67. [ -n "${value}" ] && value="${value}, "
  68. value="${value}${item}"
  69. }
  70. idx=$((idx + 1))
  71. done
  72. # nothing built?
  73. [ -n "${value}" ] || return 2
  74. # only print value if set
  75. echo "${value}"
  76. return 0
  77. }
  78. modemmanager_cleanup_connection() {
  79. local modemstatus="$1"
  80. local bearercount idx bearerpath
  81. bearercount=$(modemmanager_get_field "${modemstatus}" "modem.generic.bearers.length")
  82. # do nothing if no bearers reported
  83. [ -n "${bearercount}" ] && [ "$bearercount" -ge 1 ] && {
  84. # explicitly disconnect just in case
  85. mmcli --modem="${device}" --simple-disconnect >/dev/null 2>&1
  86. # and remove all bearer objects, if any found
  87. idx=1
  88. while [ $idx -le "$bearercount" ]; do
  89. bearerpath=$(modemmanager_get_field "${modemstatus}" "modem.generic.bearers.value\[$idx\]")
  90. mmcli --modem "${device}" --delete-bearer="${bearerpath}" >/dev/null 2>&1
  91. idx=$((idx + 1))
  92. done
  93. }
  94. }
  95. modemmanager_connected_method_ppp_ipv4() {
  96. local interface="$1"
  97. local ttyname="$2"
  98. local username="$3"
  99. local password="$4"
  100. proto_run_command "${interface}" /usr/sbin/pppd \
  101. "${ttyname}" \
  102. 115200 \
  103. nodetach \
  104. noaccomp \
  105. nobsdcomp \
  106. nopcomp \
  107. novj \
  108. noauth \
  109. ${username:+ user $username} \
  110. ${password:+ password $password} \
  111. lcp-echo-failure 5 \
  112. lcp-echo-interval 15 \
  113. lock \
  114. crtscts \
  115. nodefaultroute \
  116. usepeerdns \
  117. ipparam "${interface}" \
  118. ip-up-script /lib/netifd/ppp-up \
  119. ip-down-script /lib/netifd/ppp-down
  120. }
  121. modemmanager_disconnected_method_ppp_ipv4() {
  122. local interface="$1"
  123. echo "running disconnection (ppp method)"
  124. [ -n "${ERROR}" ] && {
  125. local errorstring
  126. errorstring=$(ppp_exitcode_tostring "${ERROR}")
  127. case "$ERROR" in
  128. 0)
  129. ;;
  130. 2)
  131. proto_notify_error "$interface" "$errorstring"
  132. proto_block_restart "$interface"
  133. ;;
  134. *)
  135. proto_notify_error "$interface" "$errorstring"
  136. ;;
  137. esac
  138. } || echo "pppd result code not given"
  139. proto_kill_command "$interface"
  140. }
  141. modemmanager_connected_method_dhcp_ipv4() {
  142. local interface="$1"
  143. local wwan="$2"
  144. local metric="$3"
  145. proto_init_update "${wwan}" 1
  146. proto_set_keep 1
  147. proto_send_update "${interface}"
  148. json_init
  149. json_add_string name "${interface}_4"
  150. json_add_string ifname "@${interface}"
  151. json_add_string proto "dhcp"
  152. proto_add_dynamic_defaults
  153. [ -n "$metric" ] && json_add_int metric "${metric}"
  154. json_close_object
  155. ubus call network add_dynamic "$(json_dump)"
  156. }
  157. modemmanager_connected_method_static_ipv4() {
  158. local interface="$1"
  159. local wwan="$2"
  160. local address="$3"
  161. local prefix="$4"
  162. local gateway="$5"
  163. local mtu="$6"
  164. local dns1="$7"
  165. local dns2="$8"
  166. local metric="$9"
  167. local mask=""
  168. [ -n "${address}" ] || {
  169. proto_notify_error "${interface}" ADDRESS_MISSING
  170. return
  171. }
  172. [ -n "${prefix}" ] || {
  173. proto_notify_error "${interface}" PREFIX_MISSING
  174. return
  175. }
  176. mask=$(cdr2mask "${prefix}")
  177. [ -n "${mtu}" ] && /sbin/ip link set dev "${wwan}" mtu "${mtu}"
  178. proto_init_update "${wwan}" 1
  179. proto_set_keep 1
  180. echo "adding IPv4 address ${address}, netmask ${mask}"
  181. proto_add_ipv4_address "${address}" "${mask}"
  182. [ -n "${gateway}" ] && {
  183. echo "adding default IPv4 route via ${gateway}"
  184. proto_add_ipv4_route "0.0.0.0" "0" "${gateway}" "${address}"
  185. }
  186. [ -n "${dns1}" ] && {
  187. echo "adding primary DNS at ${dns1}"
  188. proto_add_dns_server "${dns1}"
  189. }
  190. [ -n "${dns2}" ] && {
  191. echo "adding secondary DNS at ${dns2}"
  192. proto_add_dns_server "${dns2}"
  193. }
  194. [ -n "$metric" ] && json_add_int metric "${metric}"
  195. proto_send_update "${interface}"
  196. }
  197. modemmanager_connected_method_dhcp_ipv6() {
  198. local interface="$1"
  199. local wwan="$2"
  200. local metric="$3"
  201. proto_init_update "${wwan}" 1
  202. proto_set_keep 1
  203. proto_send_update "${interface}"
  204. json_init
  205. json_add_string name "${interface}_6"
  206. json_add_string ifname "@${interface}"
  207. json_add_string proto "dhcpv6"
  208. proto_add_dynamic_defaults
  209. json_add_string extendprefix 1 # RFC 7278: Extend an IPv6 /64 Prefix to LAN
  210. [ -n "$metric" ] && json_add_int metric "${metric}"
  211. json_close_object
  212. ubus call network add_dynamic "$(json_dump)"
  213. }
  214. modemmanager_connected_method_static_ipv6() {
  215. local interface="$1"
  216. local wwan="$2"
  217. local address="$3"
  218. local prefix="$4"
  219. local gateway="$5"
  220. local mtu="$6"
  221. local dns1="$7"
  222. local dns2="$8"
  223. local metric="$9"
  224. [ -n "${address}" ] || {
  225. proto_notify_error "${interface}" ADDRESS_MISSING
  226. return
  227. }
  228. [ -n "${prefix}" ] || {
  229. proto_notify_error "${interface}" PREFIX_MISSING
  230. return
  231. }
  232. [ -n "${mtu}" ] && /sbin/ip link set dev "${wwan}" mtu "${mtu}"
  233. proto_init_update "${wwan}" 1
  234. proto_set_keep 1
  235. echo "adding IPv6 address ${address}, prefix ${prefix}"
  236. proto_add_ipv6_address "${address}" "128"
  237. proto_add_ipv6_prefix "${address}/${prefix}"
  238. [ -n "${gateway}" ] && {
  239. echo "adding default IPv6 route via ${gateway}"
  240. proto_add_ipv6_route "${gateway}" "128"
  241. proto_add_ipv6_route "::0" "0" "${gateway}" "" "" "${address}/${prefix}"
  242. }
  243. [ -n "${dns1}" ] && {
  244. echo "adding primary DNS at ${dns1}"
  245. proto_add_dns_server "${dns1}"
  246. }
  247. [ -n "${dns2}" ] && {
  248. echo "adding secondary DNS at ${dns2}"
  249. proto_add_dns_server "${dns2}"
  250. }
  251. [ -n "$metric" ] && json_add_int metric "${metric}"
  252. proto_send_update "${interface}"
  253. }
  254. modemmanager_disconnected_method_common() {
  255. local interface="$1"
  256. echo "running disconnection (common)"
  257. proto_init_update "*" 0
  258. proto_send_update "${interface}"
  259. }
  260. proto_modemmanager_init_config() {
  261. available=1
  262. no_device=1
  263. proto_config_add_string device
  264. proto_config_add_string apn
  265. proto_config_add_string username
  266. proto_config_add_string password
  267. proto_config_add_string pincode
  268. proto_config_add_string iptype
  269. proto_config_add_boolean lowpower
  270. proto_config_add_defaults
  271. }
  272. proto_modemmanager_setup() {
  273. local interface="$1"
  274. local modempath modemstatus bearercount bearerpath connectargs bearerstatus beareriface
  275. local bearermethod_ipv4 bearermethod_ipv6
  276. local operatorname operatorid registration accesstech signalquality
  277. local device apn username password pincode iptype metric
  278. local address prefix gateway mtu dns1 dns2
  279. json_get_vars device apn username password pincode iptype metric
  280. # validate sysfs path given in config
  281. [ -n "${device}" ] || {
  282. echo "No device specified"
  283. proto_notify_error "${interface}" NO_DEVICE
  284. proto_set_available "${interface}" 0
  285. return 1
  286. }
  287. [ -e "${device}" ] || {
  288. echo "Device not found in sysfs"
  289. proto_set_available "${interface}" 0
  290. return 1
  291. }
  292. # validate that ModemManager is handling the modem at the sysfs path
  293. modemstatus=$(mmcli --modem="${device}" --output-keyvalue)
  294. modempath=$(modemmanager_get_field "${modemstatus}" "modem.dbus-path")
  295. [ -n "${modempath}" ] || {
  296. echo "Device not managed by ModemManager"
  297. proto_notify_error "${interface}" DEVICE_NOT_MANAGED
  298. proto_set_available "${interface}" 0
  299. return 1
  300. }
  301. echo "modem available at ${modempath}"
  302. # always cleanup before attempting a new connection, just in case
  303. modemmanager_cleanup_connection "${modemstatus}"
  304. # setup connect args; APN mandatory (even if it may be empty)
  305. echo "starting connection with apn '${apn}'..."
  306. connectargs="apn=${apn}${iptype:+,ip-type=${iptype}}${username:+,user=${username}}${password:+,password=${password}}${pincode:+,pin=${pincode}}"
  307. mmcli --modem="${device}" --timeout 120 --simple-connect="${connectargs}" || {
  308. proto_notify_error "${interface}" CONNECT_FAILED
  309. proto_block_restart "${interface}"
  310. return 1
  311. }
  312. # log additional useful information
  313. modemstatus=$(mmcli --modem="${device}" --output-keyvalue)
  314. operatorname=$(modemmanager_get_field "${modemstatus}" "modem.3gpp.operator-name")
  315. [ -n "${operatorname}" ] && echo "network operator name: ${operatorname}"
  316. operatorid=$(modemmanager_get_field "${modemstatus}" "modem.3gpp.operator-code")
  317. [ -n "${operatorid}" ] && echo "network operator MCCMNC: ${operatorid}"
  318. registration=$(modemmanager_get_field "${modemstatus}" "modem.3gpp.registration-state")
  319. [ -n "${registration}" ] && echo "registration type: ${registration}"
  320. accesstech=$(modemmanager_get_multivalue_field "${modemstatus}" "modem.generic.access-technologies")
  321. [ -n "${accesstech}" ] && echo "access technology: ${accesstech}"
  322. signalquality=$(modemmanager_get_field "${modemstatus}" "modem.generic.signal-quality.value")
  323. [ -n "${signalquality}" ] && echo "signal quality: ${signalquality}%"
  324. # we won't like it if there are more than one bearers, as that would mean the
  325. # user manually created them, and that's unsupported by this proto
  326. bearercount=$(modemmanager_get_field "${modemstatus}" "modem.generic.bearers.length")
  327. [ -n "${bearercount}" ] && [ "$bearercount" -eq 1 ] || {
  328. proto_notify_error "${interface}" INVALID_BEARER_LIST
  329. return 1
  330. }
  331. # load connected bearer information
  332. bearerpath=$(modemmanager_get_field "${modemstatus}" "modem.generic.bearers.value\[1\]")
  333. bearerstatus=$(mmcli --bearer "${bearerpath}" --output-keyvalue)
  334. # load network interface and method information
  335. beareriface=$(modemmanager_get_field "${bearerstatus}" "bearer.status.interface")
  336. bearermethod_ipv4=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.method")
  337. bearermethod_ipv6=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.method")
  338. # setup IPv4
  339. [ -n "${bearermethod_ipv4}" ] && {
  340. echo "IPv4 connection setup required in interface ${interface}: ${bearermethod_ipv4}"
  341. case "${bearermethod_ipv4}" in
  342. "dhcp")
  343. modemmanager_connected_method_dhcp_ipv4 "${interface}" "${beareriface}" "${metric}"
  344. ;;
  345. "static")
  346. address=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.address")
  347. prefix=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.prefix")
  348. gateway=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.gateway")
  349. mtu=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.mtu")
  350. dns1=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.dns.value\[1\]")
  351. dns2=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.dns.value\[2\]")
  352. modemmanager_connected_method_static_ipv4 "${interface}" "${beareriface}" "${address}" "${prefix}" "${gateway}" "${mtu}" "${dns1}" "${dns2}" "${metric}"
  353. ;;
  354. "ppp")
  355. modemmanager_connected_method_ppp_ipv4 "${interface}" "${beareriface}" "${username}" "${password}"
  356. ;;
  357. *)
  358. proto_notify_error "${interface}" UNKNOWN_METHOD
  359. return 1
  360. ;;
  361. esac
  362. }
  363. # setup IPv6
  364. # note: if using ipv4v6, both IPv4 and IPv6 settings will have the same MTU and metric values reported
  365. [ -n "${bearermethod_ipv6}" ] && {
  366. echo "IPv6 connection setup required in interface ${interface}: ${bearermethod_ipv6}"
  367. case "${bearermethod_ipv6}" in
  368. "dhcp")
  369. modemmanager_connected_method_dhcp_ipv6 "${interface}" "${beareriface}" "${metric}"
  370. ;;
  371. "static")
  372. address=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.address")
  373. prefix=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.prefix")
  374. gateway=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.gateway")
  375. mtu=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.mtu")
  376. dns1=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.dns.value\[1\]")
  377. dns2=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.dns.value\[2\]")
  378. modemmanager_connected_method_static_ipv6 "${interface}" "${beareriface}" "${address}" "${prefix}" "${gateway}" "${mtu}" "${dns1}" "${dns2}" "${metric}"
  379. ;;
  380. "ppp")
  381. proto_notify_error "${interface}" "unsupported method"
  382. return 1
  383. ;;
  384. *)
  385. proto_notify_error "${interface}" UNKNOWN_METHOD
  386. return 1
  387. ;;
  388. esac
  389. }
  390. return 0
  391. }
  392. proto_modemmanager_teardown() {
  393. local interface="$1"
  394. local modemstatus bearerpath errorstring
  395. local bearermethod_ipv4 bearermethod_ipv6
  396. local device lowpower iptype
  397. json_get_vars device lowpower iptype
  398. echo "stopping network"
  399. # load connected bearer information, just the first one should be ok
  400. modemstatus=$(mmcli --modem="${device}" --output-keyvalue)
  401. bearerpath=$(modemmanager_get_field "${modemstatus}" "modem.generic.bearers.value\[1\]")
  402. [ -n "${bearerpath}" ] || {
  403. echo "couldn't load bearer path"
  404. return
  405. }
  406. # load bearer connection methods
  407. bearerstatus=$(mmcli --bearer "${bearerpath}" --output-keyvalue)
  408. bearermethod_ipv4=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv4-config.method")
  409. [ -n "${bearermethod_ipv4}" ] &&
  410. echo "IPv4 connection teardown required in interface ${interface}: ${bearermethod_ipv4}"
  411. bearermethod_ipv6=$(modemmanager_get_field "${bearerstatus}" "bearer.ipv6-config.method")
  412. [ -n "${bearermethod_ipv6}" ] &&
  413. echo "IPv6 connection teardown required in interface ${interface}: ${bearermethod_ipv6}"
  414. # disconnection handling only requires special treatment in IPv4/PPP
  415. [ "${bearermethod_ipv4}" = "ppp" ] && modemmanager_disconnected_method_ppp_ipv4 "${interface}"
  416. modemmanager_disconnected_method_common "${interface}"
  417. # disconnect
  418. mmcli --modem="${device}" --simple-disconnect ||
  419. proto_notify_error "${interface}" DISCONNECT_FAILED
  420. # disable
  421. mmcli --modem="${device}" --disable
  422. # low power, only if requested
  423. [ "${lowpower:-0}" -lt 1 ] ||
  424. mmcli --modem="${device}" --set-power-state-low
  425. }
  426. [ -n "$INCLUDE_ONLY" ] || {
  427. add_protocol modemmanager
  428. }