]> git.ipfire.org Git - people/ms/network.git/blob - src/udev/network-hotplug-serial
Use autotools.
[people/ms/network.git] / src / udev / network-hotplug-serial
1 #!/bin/bash
2 ###############################################################################
3 # #
4 # IPFire.org - A linux based firewall #
5 # Copyright (C) 2011 Michael Tremer & Christian Schmidt #
6 # #
7 # This program is free software: you can redistribute it and/or modify #
8 # it under the terms of the GNU General Public License as published by #
9 # the Free Software Foundation, either version 3 of the License, or #
10 # (at your option) any later version. #
11 # #
12 # This program is distributed in the hope that it will be useful, #
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15 # GNU General Public License for more details. #
16 # #
17 # You should have received a copy of the GNU General Public License #
18 # along with this program. If not, see <http://www.gnu.org/licenses/>. #
19 # #
20 ###############################################################################
21
22 . /usr/lib/network/functions
23
24 # Setup logging.
25 LOG_FACILITY="network-hotplug-serial"
26
27 # Associated hooks.
28 ASSOCIATED_HOOKS="modem"
29
30 # Read network configuration.
31 network_config_read
32
33 log DEBUG "Called with ACTION='${ACTION}', DEVNAME='${DEVNAME}'."
34
35 # If DEVNAME is not set, we cannot handle anything here and will
36 # exit silently.
37 isset DEVNAME || exit ${EXIT_OK}
38
39 # Check if the udev environment variables are properly set.
40 assert isset ACTION
41
42 case "${ACTION}" in
43 add|register)
44 # Check if the device node already exists.
45 if ! serial_exists ${DEVNAME}; then
46 log DEBUG "Device node ${DEVNAME} does not exist"
47 exit ${EXIT_ERROR}
48 fi
49
50 # USB modems often register multiple serial interfaces.
51 # Some of them allow to send AT commands, some don't know.
52 # However, there is only one interface that can actually be used
53 # to connect to somewhere. This interface always runs in
54 # interrupt mode. Exit for all interfaces, not in this mode.
55 bus_type="$(serial_get_bus_type "${DEVNAME}")"
56 if [ "${bus_type}" = "usb" ]; then
57 # Find USB device.
58 usb_device="$(usb_device_find_by_tty "${DEVNAME}")"
59
60 # Find an interface in interrupt mode.
61 if ! usb_device_has_interface_type_interrupt "${usb_device}"; then
62 log DEBUG "USB device has no interface in interrupt mode: ${DEVNAME}"
63 exit ${EXIT_OK}
64 fi
65 fi
66
67 # Check if the device is actually a modem.
68 if ! serial_is_modem ${DEVNAME}; then
69 log DEBUG "${DEVNAME} does not look like a modem"
70 exit ${EXIT_OK}
71 fi
72
73 # When we get here, the modem is alive and responds to
74 # AT commands.
75 log DEBUG "${DEVNAME} looks like a modem"
76
77 # Initialize the modem here. Resets all established connections
78 # and so on.
79 modem_initialize "${DEVNAME}" --sleep=3
80
81 # Unlock the SIM card if it has one.
82 if modem_sim_locked "${DEVNAME}"; then
83 log ERROR "SIM card is locked. Needs unlocking."
84 exit ${EXIT_OK}
85 fi
86
87 # Try to find the zone configuration by the IMSI of the
88 # SIM card.
89 sim_imsi="$(modem_get_sim_imsi "${DEVNAME}")"
90 isset sim_imsi || exit ${EXIT_OK}
91
92 for zone in $(zones_get_all); do
93 # XXX Check if the zone is enabled.
94
95 # Skip unsupported hook types.
96 hook="$(zone_get_hook "${zone}")"
97 list_match "${hook}" ${ASSOCIATED_HOOKS} || continue
98
99 # Read IMSI from zone configuration.
100 zone_imsi="$(zone_config_option "${zone}" IMSI)"
101
102 # If IMSIs match, we start that zone.
103 if [ "${zone_imsi}" = "${sim_imsi}" ]; then
104 # Start the matching zone.
105 zone_up "${zone}"
106 break
107 fi
108 done
109
110 exit ${EXIT_OK}
111 ;;
112
113 remove|unregister)
114 # After the interface has been removed/unplugged,
115 # there are often daemons (like pppd) which need
116 # to be stopped.
117 ;;
118 esac
119
120 exit ${EXIT_OK}