Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 2 | /* |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 3 | * ifup/ifdown for busybox |
Denis Vlasenko | 0beaff8 | 2007-09-21 13:16:32 +0000 | [diff] [blame] | 4 | * Copyright (c) 2002 Glenn McGrath |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (c) 2003-2004 Erik Andersen <andersen@codepoet.org> |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 6 | * |
| 7 | * Based on ifupdown v 0.6.4 by Anthony Towns |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 8 | * Copyright (c) 1999 Anthony Towns <aj@azure.humbug.org.au> |
| 9 | * |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 10 | * Changes to upstream version |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 11 | * Remove checks for kernel version, assume kernel version 2.2.0 or better. |
Glenn L McGrath | 398ff9d | 2002-12-06 11:51:46 +0000 | [diff] [blame] | 12 | * Lines in the interfaces file cannot wrap. |
Denis Vlasenko | 2a86a61 | 2007-07-19 21:49:30 +0000 | [diff] [blame] | 13 | * To adhere to the FHS, the default state file is /var/run/ifstate |
| 14 | * (defined via CONFIG_IFUPDOWN_IFSTATE_PATH) and can be overridden by build |
| 15 | * configuration. |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 16 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 17 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 20 | //config:config IFUP |
| 21 | //config: bool "ifup" |
| 22 | //config: default y |
| 23 | //config: help |
| 24 | //config: Activate the specified interfaces. This applet makes use |
| 25 | //config: of either "ifconfig" and "route" or the "ip" command to actually |
| 26 | //config: configure network interfaces. Therefore, you will probably also want |
| 27 | //config: to enable either IFCONFIG and ROUTE, or enable |
| 28 | //config: FEATURE_IFUPDOWN_IP and the various IP options. Of |
| 29 | //config: course you could use non-busybox versions of these programs, so |
| 30 | //config: against my better judgement (since this will surely result in plenty |
| 31 | //config: of support questions on the mailing list), I do not force you to |
| 32 | //config: enable these additional options. It is up to you to supply either |
| 33 | //config: "ifconfig", "route" and "run-parts" or the "ip" command, either |
| 34 | //config: via busybox or via standalone utilities. |
| 35 | //config: |
| 36 | //config:config IFDOWN |
| 37 | //config: bool "ifdown" |
| 38 | //config: default y |
| 39 | //config: help |
| 40 | //config: Deactivate the specified interfaces. |
| 41 | //config: |
| 42 | //config:config IFUPDOWN_IFSTATE_PATH |
| 43 | //config: string "Absolute path to ifstate file" |
| 44 | //config: default "/var/run/ifstate" |
| 45 | //config: depends on IFUP || IFDOWN |
| 46 | //config: help |
| 47 | //config: ifupdown keeps state information in a file called ifstate. |
| 48 | //config: Typically it is located in /var/run/ifstate, however |
| 49 | //config: some distributions tend to put it in other places |
| 50 | //config: (debian, for example, uses /etc/network/run/ifstate). |
| 51 | //config: This config option defines location of ifstate. |
| 52 | //config: |
| 53 | //config:config FEATURE_IFUPDOWN_IP |
Denys Vlasenko | 100aea8 | 2016-12-23 15:18:31 +0100 | [diff] [blame] | 54 | //config: bool "Use ip tool (else ifconfig/route is used)" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 55 | //config: default y |
| 56 | //config: depends on IFUP || IFDOWN |
| 57 | //config: help |
| 58 | //config: Use the iproute "ip" command to implement "ifup" and "ifdown", rather |
Denys Vlasenko | 100aea8 | 2016-12-23 15:18:31 +0100 | [diff] [blame] | 59 | //config: than the default of using the older "ifconfig" and "route" utilities. |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 60 | //config: |
Denys Vlasenko | 100aea8 | 2016-12-23 15:18:31 +0100 | [diff] [blame] | 61 | //config: If Y: you must install either the full-blown iproute2 package |
| 62 | //config: or enable "ip" applet in Busybox, or the "ifup" and "ifdown" applets |
| 63 | //config: will not work. |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 64 | //config: |
Denys Vlasenko | 100aea8 | 2016-12-23 15:18:31 +0100 | [diff] [blame] | 65 | //config: If N: you must install either the full-blown ifconfig and route |
| 66 | //config: utilities, or enable these applets in Busybox. |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 67 | //config: |
| 68 | //config:config FEATURE_IFUPDOWN_IPV4 |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 69 | //config: bool "Support IPv4" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 70 | //config: default y |
| 71 | //config: depends on IFUP || IFDOWN |
| 72 | //config: help |
| 73 | //config: If you want ifup/ifdown to talk IPv4, leave this on. |
| 74 | //config: |
| 75 | //config:config FEATURE_IFUPDOWN_IPV6 |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 76 | //config: bool "Support IPv6" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 77 | //config: default y |
| 78 | //config: depends on (IFUP || IFDOWN) && FEATURE_IPV6 |
| 79 | //config: help |
| 80 | //config: If you need support for IPv6, turn this option on. |
| 81 | //config: |
| 82 | //UNUSED: |
| 83 | ////////:config FEATURE_IFUPDOWN_IPX |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 84 | ////////: bool "Support IPX" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 85 | ////////: default y |
| 86 | ////////: depends on IFUP || IFDOWN |
| 87 | ////////: help |
| 88 | ////////: If this option is selected you can use busybox to work with IPX |
| 89 | ////////: networks. |
| 90 | //config: |
| 91 | //config:config FEATURE_IFUPDOWN_MAPPING |
| 92 | //config: bool "Enable mapping support" |
| 93 | //config: default y |
| 94 | //config: depends on IFUP || IFDOWN |
| 95 | //config: help |
| 96 | //config: This enables support for the "mapping" stanza, unless you have |
| 97 | //config: a weird network setup you don't need it. |
| 98 | //config: |
| 99 | //config:config FEATURE_IFUPDOWN_EXTERNAL_DHCP |
Denys Vlasenko | f560422 | 2017-01-10 14:58:54 +0100 | [diff] [blame] | 100 | //config: bool "Support external DHCP clients" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 101 | //config: default n |
| 102 | //config: depends on IFUP || IFDOWN |
| 103 | //config: help |
| 104 | //config: This enables support for the external dhcp clients. Clients are |
| 105 | //config: tried in the following order: dhcpcd, dhclient, pump and udhcpc. |
| 106 | //config: Otherwise, if udhcpc applet is enabled, it is used. |
| 107 | //config: Otherwise, ifup/ifdown will have no support for DHCP. |
| 108 | |
| 109 | //applet:IF_IFUP(APPLET_ODDNAME(ifup, ifupdown, BB_DIR_SBIN, BB_SUID_DROP, ifup)) |
| 110 | //applet:IF_IFDOWN(APPLET_ODDNAME(ifdown, ifupdown, BB_DIR_SBIN, BB_SUID_DROP, ifdown)) |
| 111 | |
| 112 | //kbuild:lib-$(CONFIG_IFUP) += ifupdown.o |
| 113 | //kbuild:lib-$(CONFIG_IFDOWN) += ifupdown.o |
| 114 | |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 115 | //usage:#define ifup_trivial_usage |
| 116 | //usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..." |
| 117 | //usage:#define ifup_full_usage "\n\n" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 118 | //usage: " -a Configure all interfaces" |
Denys Vlasenko | bcf47ea | 2016-09-25 02:05:24 +0200 | [diff] [blame] | 119 | //usage: "\n -i FILE Use FILE instead of /etc/network/interfaces" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 120 | //usage: "\n -n Print out what would happen, but don't do it" |
| 121 | //usage: IF_FEATURE_IFUPDOWN_MAPPING( |
| 122 | //usage: "\n (note: doesn't disable mappings)" |
| 123 | //usage: "\n -m Don't run any mappings" |
| 124 | //usage: ) |
| 125 | //usage: "\n -v Print out what would happen before doing it" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 126 | //usage: "\n -f Force configuration" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 127 | //usage: |
| 128 | //usage:#define ifdown_trivial_usage |
| 129 | //usage: "[-an"IF_FEATURE_IFUPDOWN_MAPPING("m")"vf] [-i FILE] IFACE..." |
| 130 | //usage:#define ifdown_full_usage "\n\n" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 131 | //usage: " -a Deconfigure all interfaces" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 132 | //usage: "\n -i FILE Use FILE for interface definitions" |
| 133 | //usage: "\n -n Print out what would happen, but don't do it" |
| 134 | //usage: IF_FEATURE_IFUPDOWN_MAPPING( |
| 135 | //usage: "\n (note: doesn't disable mappings)" |
| 136 | //usage: "\n -m Don't run any mappings" |
| 137 | //usage: ) |
| 138 | //usage: "\n -v Print out what would happen before doing it" |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 139 | //usage: "\n -f Force deconfiguration" |
Pere Orga | 5bc8c00 | 2011-04-11 03:29:49 +0200 | [diff] [blame] | 140 | |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 141 | #include "libbb.h" |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 142 | #include "common_bufsiz.h" |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 143 | /* After libbb.h, since it needs sys/types.h on some systems */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 144 | #include <sys/utsname.h> |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 145 | #include <fnmatch.h> |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 146 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 147 | #define MAX_OPT_DEPTH 10 |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 148 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 149 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 150 | #define MAX_INTERFACE_LENGTH 10 |
| 151 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 152 | |
Wade Berrier | 142c5cb | 2008-11-14 21:18:45 +0000 | [diff] [blame] | 153 | #define UDHCPC_CMD_OPTIONS CONFIG_IFUPDOWN_UDHCPC_CMD_OPTIONS |
Denys Vlasenko | 3720a61 | 2016-10-09 23:04:16 +0200 | [diff] [blame] | 154 | #define IFSTATE_FILE_PATH CONFIG_IFUPDOWN_IFSTATE_PATH |
Wade Berrier | 142c5cb | 2008-11-14 21:18:45 +0000 | [diff] [blame] | 155 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 156 | #define debug_noise(args...) /*fprintf(stderr, args)*/ |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 157 | |
| 158 | /* Forward declaration */ |
| 159 | struct interface_defn_t; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 160 | |
Denis Vlasenko | 097c324 | 2006-11-27 16:59:15 +0000 | [diff] [blame] | 161 | typedef int execfn(char *command); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 162 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 163 | struct method_t { |
| 164 | const char *name; |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 165 | int (*up)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC; |
| 166 | int (*down)(struct interface_defn_t *ifd, execfn *e) FAST_FUNC; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 167 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 168 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 169 | struct address_family_t { |
| 170 | const char *name; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 171 | int n_methods; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 172 | const struct method_t *method; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 173 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 174 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 175 | struct mapping_defn_t { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 176 | struct mapping_defn_t *next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 177 | |
| 178 | int max_matches; |
| 179 | int n_matches; |
| 180 | char **match; |
| 181 | |
| 182 | char *script; |
| 183 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 184 | int n_mappings; |
| 185 | char **mapping; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 186 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 187 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 188 | struct variable_t { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 189 | char *name; |
| 190 | char *value; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 191 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 192 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 193 | struct interface_defn_t { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 194 | const struct address_family_t *address_family; |
| 195 | const struct method_t *method; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 196 | |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 197 | char *iface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 198 | int n_options; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 199 | struct variable_t *option; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 200 | }; |
| 201 | |
Denis Vlasenko | 89ef65f | 2007-01-29 23:43:18 +0000 | [diff] [blame] | 202 | struct interfaces_file_t { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 203 | llist_t *autointerfaces; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 204 | llist_t *ifaces; |
| 205 | struct mapping_defn_t *mappings; |
| 206 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 207 | |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 208 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 209 | #define OPTION_STR "anvf" IF_FEATURE_IFUPDOWN_MAPPING("m") "i:" |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 210 | enum { |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 211 | OPT_do_all = 0x1, |
| 212 | OPT_no_act = 0x2, |
| 213 | OPT_verbose = 0x4, |
| 214 | OPT_force = 0x8, |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 215 | OPT_no_mappings = 0x10, |
| 216 | }; |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 217 | #define DO_ALL (option_mask32 & OPT_do_all) |
| 218 | #define NO_ACT (option_mask32 & OPT_no_act) |
| 219 | #define VERBOSE (option_mask32 & OPT_verbose) |
| 220 | #define FORCE (option_mask32 & OPT_force) |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 221 | #define NO_MAPPINGS (option_mask32 & OPT_no_mappings) |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 222 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 223 | |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 224 | struct globals { |
| 225 | char **my_environ; |
| 226 | const char *startup_PATH; |
Denys Vlasenko | 681efe2 | 2011-03-08 21:00:36 +0100 | [diff] [blame] | 227 | char *shell; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 228 | } FIX_ALIASING; |
Denys Vlasenko | e6a2f4c | 2016-04-21 16:26:30 +0200 | [diff] [blame] | 229 | #define G (*(struct globals*)bb_common_bufsiz1) |
Denys Vlasenko | 47cfbf3 | 2016-04-21 18:18:48 +0200 | [diff] [blame] | 230 | #define INIT_G() do { setup_common_bufsiz(); } while (0) |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 231 | |
Denis Vlasenko | 2f4399c | 2006-09-27 14:14:51 +0000 | [diff] [blame] | 232 | |
Denys Vlasenko | 9c28fb8 | 2011-10-19 02:37:08 +0200 | [diff] [blame] | 233 | static const char keywords_up_down[] ALIGN1 = |
| 234 | "up\0" |
| 235 | "down\0" |
| 236 | "pre-up\0" |
Denys Vlasenko | 9c28fb8 | 2011-10-19 02:37:08 +0200 | [diff] [blame] | 237 | "post-down\0" |
| 238 | ; |
| 239 | |
| 240 | |
Mike Frysinger | b049c0e | 2006-06-20 23:03:27 +0000 | [diff] [blame] | 241 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 || ENABLE_FEATURE_IFUPDOWN_IPV6 |
| 242 | |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 243 | static void addstr(char **bufp, const char *str, size_t str_length) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 244 | { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 245 | /* xasprintf trick will be smaller, but we are often |
| 246 | * called with str_length == 1 - don't want to have |
| 247 | * THAT much of malloc/freeing! */ |
| 248 | char *buf = *bufp; |
| 249 | int len = (buf ? strlen(buf) : 0); |
| 250 | str_length++; |
| 251 | buf = xrealloc(buf, len + str_length); |
| 252 | /* copies at most str_length-1 chars! */ |
| 253 | safe_strncpy(buf + len, str, str_length); |
| 254 | *bufp = buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 257 | static int strncmpz(const char *l, const char *r, size_t llen) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 258 | { |
| 259 | int i = strncmp(l, r, llen); |
| 260 | |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 261 | if (i == 0) |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 262 | return - (unsigned char)r[llen]; |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 263 | return i; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 266 | static char *get_var(const char *id, size_t idlen, struct interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 267 | { |
| 268 | int i; |
| 269 | |
| 270 | if (strncmpz(id, "iface", idlen) == 0) { |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 271 | // ubuntu's ifup doesn't do this: |
| 272 | //static char *label_buf; |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 273 | //char *result; |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 274 | //free(label_buf); |
| 275 | //label_buf = xstrdup(ifd->iface); |
| 276 | // Remove virtual iface suffix |
Denis Vlasenko | ff2b6d2 | 2007-11-23 03:39:45 +0000 | [diff] [blame] | 277 | //result = strchrnul(label_buf, ':'); |
| 278 | //*result = '\0'; |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 279 | //return label_buf; |
| 280 | |
| 281 | return ifd->iface; |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 282 | } |
| 283 | if (strncmpz(id, "label", idlen) == 0) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 284 | return ifd->iface; |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 285 | } |
| 286 | for (i = 0; i < ifd->n_options; i++) { |
| 287 | if (strncmpz(id, ifd->option[i].name, idlen) == 0) { |
| 288 | return ifd->option[i].value; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 291 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 292 | } |
| 293 | |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 294 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 295 | static int count_netmask_bits(const char *dotted_quad) |
| 296 | { |
| 297 | // int result; |
| 298 | // unsigned a, b, c, d; |
| 299 | // /* Found a netmask... Check if it is dotted quad */ |
| 300 | // if (sscanf(dotted_quad, "%u.%u.%u.%u", &a, &b, &c, &d) != 4) |
| 301 | // return -1; |
| 302 | // if ((a|b|c|d) >> 8) |
| 303 | // return -1; /* one of numbers is >= 256 */ |
| 304 | // d |= (a << 24) | (b << 16) | (c << 8); /* IP */ |
| 305 | // d = ~d; /* 11110000 -> 00001111 */ |
| 306 | |
| 307 | /* Shorter version */ |
| 308 | int result; |
| 309 | struct in_addr ip; |
| 310 | unsigned d; |
| 311 | |
| 312 | if (inet_aton(dotted_quad, &ip) == 0) |
| 313 | return -1; /* malformed dotted IP */ |
| 314 | d = ntohl(ip.s_addr); /* IP in host order */ |
| 315 | d = ~d; /* 11110000 -> 00001111 */ |
| 316 | if (d & (d+1)) /* check that it is in 00001111 form */ |
| 317 | return -1; /* no it is not */ |
| 318 | result = 32; |
| 319 | while (d) { |
| 320 | d >>= 1; |
| 321 | result--; |
| 322 | } |
| 323 | return result; |
| 324 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 325 | # endif |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 326 | |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 327 | static char *parse(const char *command, struct interface_defn_t *ifd) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 328 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 329 | size_t old_pos[MAX_OPT_DEPTH] = { 0 }; |
Denys Vlasenko | 3f21044 | 2012-11-09 17:16:40 +0100 | [diff] [blame] | 330 | smallint okay[MAX_OPT_DEPTH] = { 1 }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 331 | int opt_depth = 1; |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 332 | char *result = NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 333 | |
| 334 | while (*command) { |
| 335 | switch (*command) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 336 | default: |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 337 | addstr(&result, command, 1); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 338 | command++; |
| 339 | break; |
| 340 | case '\\': |
Denys Vlasenko | 3f21044 | 2012-11-09 17:16:40 +0100 | [diff] [blame] | 341 | if (command[1]) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 342 | command++; |
Denys Vlasenko | 3f21044 | 2012-11-09 17:16:40 +0100 | [diff] [blame] | 343 | addstr(&result, command, 1); |
| 344 | command++; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 345 | break; |
| 346 | case '[': |
| 347 | if (command[1] == '[' && opt_depth < MAX_OPT_DEPTH) { |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 348 | old_pos[opt_depth] = result ? strlen(result) : 0; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 349 | okay[opt_depth] = 1; |
| 350 | opt_depth++; |
| 351 | command += 2; |
| 352 | } else { |
Denys Vlasenko | 1118133 | 2012-11-05 13:22:56 +0100 | [diff] [blame] | 353 | addstr(&result, command, 1); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 354 | command++; |
| 355 | } |
| 356 | break; |
| 357 | case ']': |
| 358 | if (command[1] == ']' && opt_depth > 1) { |
| 359 | opt_depth--; |
| 360 | if (!okay[opt_depth]) { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 361 | result[old_pos[opt_depth]] = '\0'; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 362 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 363 | command += 2; |
| 364 | } else { |
Denys Vlasenko | 1118133 | 2012-11-05 13:22:56 +0100 | [diff] [blame] | 365 | addstr(&result, command, 1); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 366 | command++; |
| 367 | } |
| 368 | break; |
| 369 | case '%': |
| 370 | { |
| 371 | char *nextpercent; |
| 372 | char *varvalue; |
| 373 | |
| 374 | command++; |
| 375 | nextpercent = strchr(command, '%'); |
| 376 | if (!nextpercent) { |
Denys Vlasenko | a803bfa | 2012-11-05 14:18:36 +0100 | [diff] [blame] | 377 | /* Unterminated %var% */ |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 378 | free(result); |
| 379 | return NULL; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 380 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 381 | |
| 382 | varvalue = get_var(command, nextpercent - command, ifd); |
| 383 | |
| 384 | if (varvalue) { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 385 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | ee777c4 | 2008-08-12 01:35:34 +0000 | [diff] [blame] | 386 | /* "hwaddress <class> <address>": |
| 387 | * unlike ifconfig, ip doesnt want <class> |
| 388 | * (usually "ether" keyword). Skip it. */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 389 | if (is_prefixed_with(command, "hwaddress")) { |
Denis Vlasenko | ee777c4 | 2008-08-12 01:35:34 +0000 | [diff] [blame] | 390 | varvalue = skip_whitespace(skip_non_whitespace(varvalue)); |
| 391 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 392 | # endif |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 393 | addstr(&result, varvalue, strlen(varvalue)); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 394 | } else { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 395 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 396 | /* Sigh... Add a special case for 'ip' to convert from |
| 397 | * dotted quad to bit count style netmasks. */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 398 | if (is_prefixed_with(command, "bnmask")) { |
Denis Vlasenko | 93ad1c2 | 2006-11-23 15:07:38 +0000 | [diff] [blame] | 399 | unsigned res; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 400 | varvalue = get_var("netmask", 7, ifd); |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 401 | if (varvalue) { |
| 402 | res = count_netmask_bits(varvalue); |
| 403 | if (res > 0) { |
| 404 | const char *argument = utoa(res); |
| 405 | addstr(&result, argument, strlen(argument)); |
| 406 | command = nextpercent + 1; |
| 407 | break; |
| 408 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 409 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 410 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 411 | # endif |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 412 | okay[opt_depth - 1] = 0; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 413 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 414 | |
| 415 | command = nextpercent + 1; |
| 416 | } |
| 417 | break; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | if (opt_depth > 1) { |
Denys Vlasenko | a803bfa | 2012-11-05 14:18:36 +0100 | [diff] [blame] | 422 | /* Unbalanced bracket */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 423 | free(result); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 424 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (!okay[0]) { |
Denys Vlasenko | a803bfa | 2012-11-05 14:18:36 +0100 | [diff] [blame] | 428 | /* Undefined variable and we aren't in a bracket */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 429 | free(result); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 430 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 431 | } |
| 432 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 433 | return result; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 434 | } |
| 435 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 436 | /* execute() returns 1 for success and 0 for failure */ |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 437 | static int execute(const char *command, struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 438 | { |
| 439 | char *out; |
| 440 | int ret; |
| 441 | |
| 442 | out = parse(command, ifd); |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 443 | if (!out) { |
Denis Vlasenko | 2375d75 | 2006-12-19 23:15:46 +0000 | [diff] [blame] | 444 | /* parse error? */ |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 445 | return 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 446 | } |
Denis Vlasenko | 2375d75 | 2006-12-19 23:15:46 +0000 | [diff] [blame] | 447 | /* out == "": parsed ok but not all needed variables known, skip */ |
| 448 | ret = out[0] ? (*exec)(out) : 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 449 | |
| 450 | free(out); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 451 | if (ret != 1) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 452 | return 0; |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 453 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 454 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 455 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 456 | |
| 457 | #endif /* FEATURE_IFUPDOWN_IPV4 || FEATURE_IFUPDOWN_IPV6 */ |
| 458 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 459 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 460 | #if ENABLE_FEATURE_IFUPDOWN_IPV6 |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 461 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 462 | static int FAST_FUNC loopback_up6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 463 | { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 464 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 465 | int result; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 466 | result = execute("ip addr add ::1 dev %iface%", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 467 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 468 | return ((result == 2) ? 2 : 0); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 469 | # else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 470 | return execute("ifconfig %iface% add ::1", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 471 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 472 | } |
| 473 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 474 | static int FAST_FUNC loopback_down6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 475 | { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 476 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 477 | return execute("ip link set %iface% down", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 478 | # else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 479 | return execute("ifconfig %iface% del ::1", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 480 | # endif |
| 481 | } |
| 482 | |
| 483 | static int FAST_FUNC manual_up_down6(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM) |
| 484 | { |
| 485 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 486 | } |
| 487 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 488 | static int FAST_FUNC static_up6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 489 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 490 | int result; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 491 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 492 | result = execute("ip addr add %address%/%netmask% dev %iface%[[ label %label%]]", ifd, exec); |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 493 | result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec); |
Timo Teräs | 0285243 | 2015-10-26 17:06:12 +0100 | [diff] [blame] | 494 | /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */ |
| 495 | result += execute("[[ip route add ::/0 via %gateway% dev %iface%]][[ metric %metric%]]", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 496 | # else |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 497 | result = execute("ifconfig %iface%[[ media %media%]][[ hw %hwaddress%]][[ mtu %mtu%]] up", ifd, exec); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 498 | result += execute("ifconfig %iface% add %address%/%netmask%", ifd, exec); |
Natanael Copa | cfcb1da | 2012-02-17 19:36:26 +0000 | [diff] [blame] | 499 | result += execute("[[route -A inet6 add ::/0 gw %gateway%[[ metric %metric%]]]]", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 500 | # endif |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 501 | return ((result == 3) ? 3 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 504 | static int FAST_FUNC static_down6(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 505 | { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 506 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 507 | return execute("ip link set %iface% down", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 508 | # else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 509 | return execute("ifconfig %iface% down", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 510 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 513 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 514 | static int FAST_FUNC v4tunnel_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 515 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 516 | int result; |
| 517 | result = execute("ip tunnel add %iface% mode sit remote " |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 518 | "%endpoint%[[ local %local%]][[ ttl %ttl%]]", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 519 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 62b031f | 2003-08-29 07:47:52 +0000 | [diff] [blame] | 520 | result += execute("ip addr add %address%/%netmask% dev %iface%", ifd, exec); |
Timo Teräs | 0285243 | 2015-10-26 17:06:12 +0100 | [diff] [blame] | 521 | /* Reportedly, IPv6 needs "dev %iface%", but IPv4 does not: */ |
| 522 | result += execute("[[ip route add ::/0 via %gateway% dev %iface%]]", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 523 | return ((result == 4) ? 4 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 524 | } |
| 525 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 526 | static int FAST_FUNC v4tunnel_down(struct interface_defn_t * ifd, execfn * exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 527 | { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 528 | return execute("ip tunnel del %iface%", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 529 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 530 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 531 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 532 | static const struct method_t methods6[] = { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 533 | # if ENABLE_FEATURE_IFUPDOWN_IP |
| 534 | { "v4tunnel" , v4tunnel_up , v4tunnel_down , }, |
| 535 | # endif |
| 536 | { "static" , static_up6 , static_down6 , }, |
| 537 | { "manual" , manual_up_down6 , manual_up_down6 , }, |
| 538 | { "loopback" , loopback_up6 , loopback_down6 , }, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 539 | }; |
| 540 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 541 | static const struct address_family_t addr_inet6 = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 542 | "inet6", |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 543 | ARRAY_SIZE(methods6), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 544 | methods6 |
| 545 | }; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 546 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 547 | #endif /* FEATURE_IFUPDOWN_IPV6 */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 548 | |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 549 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 550 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 551 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 552 | static int FAST_FUNC loopback_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 553 | { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 554 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 555 | int result; |
Eric Andersen | fdd2a0f | 2003-08-06 09:23:44 +0000 | [diff] [blame] | 556 | result = execute("ip addr add 127.0.0.1/8 dev %iface%", ifd, exec); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 557 | result += execute("ip link set %iface% up", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 558 | return ((result == 2) ? 2 : 0); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 559 | # else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 560 | return execute("ifconfig %iface% 127.0.0.1 up", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 561 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 562 | } |
| 563 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 564 | static int FAST_FUNC loopback_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 565 | { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 566 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 567 | int result; |
| 568 | result = execute("ip addr flush dev %iface%", ifd, exec); |
| 569 | result += execute("ip link set %iface% down", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 570 | return ((result == 2) ? 2 : 0); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 571 | # else |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 572 | return execute("ifconfig %iface% 127.0.0.1 down", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 573 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 574 | } |
| 575 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 576 | static int FAST_FUNC static_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 577 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 578 | int result; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 579 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 580 | result = execute("ip addr add %address%/%bnmask%[[ broadcast %broadcast%]] " |
| 581 | "dev %iface%[[ peer %pointopoint%]][[ label %label%]]", ifd, exec); |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 582 | result += execute("ip link set[[ mtu %mtu%]][[ addr %hwaddress%]] %iface% up", ifd, exec); |
Alex Dowad | 65c501a | 2015-08-07 11:53:26 +0200 | [diff] [blame] | 583 | result += execute("[[ip route add default via %gateway% dev %iface%[[ metric %metric%]]]]", ifd, exec); |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 584 | return ((result == 3) ? 3 : 0); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 585 | # else |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 586 | /* ifconfig said to set iface up before it processes hw %hwaddress%, |
| 587 | * which then of course fails. Thus we run two separate ifconfig */ |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 588 | result = execute("ifconfig %iface%[[ hw %hwaddress%]][[ media %media%]][[ mtu %mtu%]] up", |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 589 | ifd, exec); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 590 | result += execute("ifconfig %iface% %address% netmask %netmask%" |
Denys Vlasenko | 92d98f0 | 2016-09-20 17:31:11 +0200 | [diff] [blame] | 591 | "[[ broadcast %broadcast%]][[ pointopoint %pointopoint%]]", |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 592 | ifd, exec); |
Natanael Copa | cfcb1da | 2012-02-17 19:36:26 +0000 | [diff] [blame] | 593 | result += execute("[[route add default gw %gateway%[[ metric %metric%]] %iface%]]", ifd, exec); |
Denis Vlasenko | a741b77 | 2006-11-23 15:08:37 +0000 | [diff] [blame] | 594 | return ((result == 3) ? 3 : 0); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 595 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 596 | } |
| 597 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 598 | static int FAST_FUNC static_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 599 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 600 | int result; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 601 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denys Vlasenko | 92d98f0 | 2016-09-20 17:31:11 +0200 | [diff] [blame] | 602 | /* Optional "label LBL" is necessary if interface is an alias (eth0:0), |
| 603 | * otherwise "ip addr flush dev eth0:0" flushes all addresses on eth0. |
| 604 | */ |
| 605 | result = execute("ip addr flush dev %iface%[[ label %label%]]", ifd, exec); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 606 | result += execute("ip link set %iface% down", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 607 | # else |
Denis Vlasenko | 6c4eb44 | 2008-08-19 23:02:23 +0000 | [diff] [blame] | 608 | /* result = execute("[[route del default gw %gateway% %iface%]]", ifd, exec); */ |
| 609 | /* Bringing the interface down deletes the routes in itself. |
| 610 | Otherwise this fails if we reference 'gateway' when using this from dhcp_down */ |
| 611 | result = 1; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 612 | result += execute("ifconfig %iface% down", ifd, exec); |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 613 | # endif |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 614 | return ((result == 2) ? 2 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 617 | # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 618 | struct dhcp_client_t { |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 619 | const char *name; |
| 620 | const char *startcmd; |
| 621 | const char *stopcmd; |
| 622 | }; |
| 623 | |
| 624 | static const struct dhcp_client_t ext_dhcp_clients[] = { |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 625 | { "dhcpcd", |
Denys Vlasenko | 2ab3acb | 2010-09-03 16:58:37 +0200 | [diff] [blame] | 626 | "dhcpcd[[ -h %hostname%]][[ -i %vendor%]][[ -I %client%]][[ -l %leasetime%]] %iface%", |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 627 | "dhcpcd -k %iface%", |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 628 | }, |
| 629 | { "dhclient", |
| 630 | "dhclient -pf /var/run/dhclient.%iface%.pid %iface%", |
| 631 | "kill -9 `cat /var/run/dhclient.%iface%.pid` 2>/dev/null", |
| 632 | }, |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 633 | { "pump", |
| 634 | "pump -i %iface%[[ -h %hostname%]][[ -l %leasehours%]]", |
| 635 | "pump -i %iface% -k", |
| 636 | }, |
| 637 | { "udhcpc", |
Natanael Copa | 1062391 | 2012-09-19 16:55:08 +0200 | [diff] [blame] | 638 | "udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid -i %iface%[[ -x hostname:%hostname%]][[ -c %client%]]" |
Denis Vlasenko | 2e4c3c4 | 2008-04-02 13:04:19 +0000 | [diff] [blame] | 639 | "[[ -s %script%]][[ %udhcpc_opts%]]", |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 640 | "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 641 | }, |
| 642 | }; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 643 | # endif /* FEATURE_IFUPDOWN_EXTERNAL_DHCPC */ |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 644 | |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 645 | # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 646 | static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 647 | { |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 648 | unsigned i; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 649 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 6cd84da | 2007-07-21 14:57:54 +0000 | [diff] [blame] | 650 | /* ip doesn't up iface when it configures it (unlike ifconfig) */ |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 651 | if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec)) |
Denis Vlasenko | ee777c4 | 2008-08-12 01:35:34 +0000 | [diff] [blame] | 652 | return 0; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 653 | # else |
Denis Vlasenko | ee777c4 | 2008-08-12 01:35:34 +0000 | [diff] [blame] | 654 | /* needed if we have hwaddress on dhcp iface */ |
| 655 | if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec)) |
Denis Vlasenko | 6cd84da | 2007-07-21 14:57:54 +0000 | [diff] [blame] | 656 | return 0; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 657 | # endif |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 658 | for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) { |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 659 | if (executable_exists(ext_dhcp_clients[i].name)) |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 660 | return execute(ext_dhcp_clients[i].startcmd, ifd, exec); |
| 661 | } |
| 662 | bb_error_msg("no dhcp clients found"); |
Denis Vlasenko | 2f4399c | 2006-09-27 14:14:51 +0000 | [diff] [blame] | 663 | return 0; |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 664 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 665 | # elif ENABLE_UDHCPC |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 666 | static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd, execfn *exec) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 667 | { |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 668 | # if ENABLE_FEATURE_IFUPDOWN_IP |
Denis Vlasenko | 6cd84da | 2007-07-21 14:57:54 +0000 | [diff] [blame] | 669 | /* ip doesn't up iface when it configures it (unlike ifconfig) */ |
Denis Vlasenko | 44d5dce | 2008-11-01 00:10:51 +0000 | [diff] [blame] | 670 | if (!execute("ip link set[[ addr %hwaddress%]] %iface% up", ifd, exec)) |
Denis Vlasenko | ee777c4 | 2008-08-12 01:35:34 +0000 | [diff] [blame] | 671 | return 0; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 672 | # else |
Denis Vlasenko | ee777c4 | 2008-08-12 01:35:34 +0000 | [diff] [blame] | 673 | /* needed if we have hwaddress on dhcp iface */ |
| 674 | if (!execute("ifconfig %iface%[[ hw %hwaddress%]] up", ifd, exec)) |
Denis Vlasenko | 6cd84da | 2007-07-21 14:57:54 +0000 | [diff] [blame] | 675 | return 0; |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 676 | # endif |
Wade Berrier | 142c5cb | 2008-11-14 21:18:45 +0000 | [diff] [blame] | 677 | return execute("udhcpc " UDHCPC_CMD_OPTIONS " -p /var/run/udhcpc.%iface%.pid " |
Natanael Copa | 1062391 | 2012-09-19 16:55:08 +0200 | [diff] [blame] | 678 | "-i %iface%[[ -x hostname:%hostname%]][[ -c %client%]][[ -s %script%]][[ %udhcpc_opts%]]", |
Denis Vlasenko | eda43d7 | 2007-05-02 22:04:38 +0000 | [diff] [blame] | 679 | ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 680 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 681 | # else |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 682 | static int FAST_FUNC dhcp_up(struct interface_defn_t *ifd UNUSED_PARAM, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 683 | execfn *exec UNUSED_PARAM) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 684 | { |
| 685 | return 0; /* no dhcp support */ |
| 686 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 687 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 688 | |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 689 | # if ENABLE_FEATURE_IFUPDOWN_EXTERNAL_DHCP |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 690 | static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 691 | { |
Denis Vlasenko | 6c4eb44 | 2008-08-19 23:02:23 +0000 | [diff] [blame] | 692 | int result = 0; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 693 | unsigned i; |
Denis Vlasenko | 6c4eb44 | 2008-08-19 23:02:23 +0000 | [diff] [blame] | 694 | |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 695 | for (i = 0; i < ARRAY_SIZE(ext_dhcp_clients); i++) { |
Denys Vlasenko | e765b5a | 2014-05-02 17:15:58 +0200 | [diff] [blame] | 696 | if (executable_exists(ext_dhcp_clients[i].name)) { |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 697 | result = execute(ext_dhcp_clients[i].stopcmd, ifd, exec); |
Denis Vlasenko | 6c4eb44 | 2008-08-19 23:02:23 +0000 | [diff] [blame] | 698 | if (result) |
| 699 | break; |
| 700 | } |
Denis Vlasenko | f6f43df | 2006-10-11 22:16:56 +0000 | [diff] [blame] | 701 | } |
Denis Vlasenko | 6c4eb44 | 2008-08-19 23:02:23 +0000 | [diff] [blame] | 702 | |
| 703 | if (!result) |
| 704 | bb_error_msg("warning: no dhcp clients found and stopped"); |
| 705 | |
| 706 | /* Sleep a bit, otherwise static_down tries to bring down interface too soon, |
| 707 | and it may come back up because udhcpc is still shutting down */ |
| 708 | usleep(100000); |
| 709 | result += static_down(ifd, exec); |
| 710 | return ((result == 3) ? 3 : 0); |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 711 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 712 | # elif ENABLE_UDHCPC |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 713 | static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd, execfn *exec) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 714 | { |
Denis Vlasenko | 6c4eb44 | 2008-08-19 23:02:23 +0000 | [diff] [blame] | 715 | int result; |
Denys Vlasenko | 78abf88 | 2010-05-02 20:30:28 +0200 | [diff] [blame] | 716 | result = execute( |
| 717 | "test -f /var/run/udhcpc.%iface%.pid && " |
| 718 | "kill `cat /var/run/udhcpc.%iface%.pid` 2>/dev/null", |
| 719 | ifd, exec); |
Denis Vlasenko | 6c4eb44 | 2008-08-19 23:02:23 +0000 | [diff] [blame] | 720 | /* Also bring the hardware interface down since |
| 721 | killing the dhcp client alone doesn't do it. |
| 722 | This enables consecutive ifup->ifdown->ifup */ |
| 723 | /* Sleep a bit, otherwise static_down tries to bring down interface too soon, |
| 724 | and it may come back up because udhcpc is still shutting down */ |
| 725 | usleep(100000); |
| 726 | result += static_down(ifd, exec); |
| 727 | return ((result == 3) ? 3 : 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 728 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 729 | # else |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 730 | static int FAST_FUNC dhcp_down(struct interface_defn_t *ifd UNUSED_PARAM, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 731 | execfn *exec UNUSED_PARAM) |
Denis Vlasenko | 85c2471 | 2008-03-17 09:04:04 +0000 | [diff] [blame] | 732 | { |
| 733 | return 0; /* no dhcp support */ |
| 734 | } |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 735 | # endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 736 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 737 | static int FAST_FUNC manual_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM) |
Denis Vlasenko | 1c3577f | 2006-10-02 20:57:10 +0000 | [diff] [blame] | 738 | { |
| 739 | return 1; |
| 740 | } |
| 741 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 742 | static int FAST_FUNC bootp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 743 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 744 | return execute("bootpc[[ --bootfile %bootfile%]] --dev %iface%" |
Denis Vlasenko | 1caca34 | 2007-08-02 10:14:29 +0000 | [diff] [blame] | 745 | "[[ --server %server%]][[ --hwaddr %hwaddr%]]" |
| 746 | " --returniffail --serverbcast", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 747 | } |
| 748 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 749 | static int FAST_FUNC ppp_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 750 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 751 | return execute("pon[[ %provider%]]", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 752 | } |
| 753 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 754 | static int FAST_FUNC ppp_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 755 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 756 | return execute("poff[[ %provider%]]", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 757 | } |
| 758 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 759 | static int FAST_FUNC wvdial_up(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 760 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 761 | return execute("start-stop-daemon --start -x wvdial " |
| 762 | "-p /var/run/wvdial.%iface% -b -m --[[ %provider%]]", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Denys Vlasenko | d5f1b1b | 2009-06-05 12:06:05 +0200 | [diff] [blame] | 765 | static int FAST_FUNC wvdial_down(struct interface_defn_t *ifd, execfn *exec) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 766 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 767 | return execute("start-stop-daemon --stop -x wvdial " |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 768 | "-p /var/run/wvdial.%iface% -s 2", ifd, exec); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 769 | } |
| 770 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 771 | static const struct method_t methods[] = { |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 772 | { "manual" , manual_up_down, manual_up_down, }, |
| 773 | { "wvdial" , wvdial_up , wvdial_down , }, |
| 774 | { "ppp" , ppp_up , ppp_down , }, |
| 775 | { "static" , static_up , static_down , }, |
| 776 | { "bootp" , bootp_up , static_down , }, |
| 777 | { "dhcp" , dhcp_up , dhcp_down , }, |
| 778 | { "loopback", loopback_up , loopback_down , }, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 779 | }; |
| 780 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 781 | static const struct address_family_t addr_inet = { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 782 | "inet", |
Denis Vlasenko | 80b8b39 | 2007-06-25 10:55:35 +0000 | [diff] [blame] | 783 | ARRAY_SIZE(methods), |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 784 | methods |
| 785 | }; |
| 786 | |
Denys Vlasenko | 66cb7be | 2010-10-29 02:42:20 +0200 | [diff] [blame] | 787 | #endif /* FEATURE_IFUPDOWN_IPV4 */ |
| 788 | |
Kaarle Ritvanen | fe8390e | 2014-02-03 15:56:20 +0100 | [diff] [blame] | 789 | static int FAST_FUNC link_up_down(struct interface_defn_t *ifd UNUSED_PARAM, execfn *exec UNUSED_PARAM) |
| 790 | { |
| 791 | return 1; |
| 792 | } |
| 793 | |
| 794 | static const struct method_t link_methods[] = { |
| 795 | { "none", link_up_down, link_up_down } |
| 796 | }; |
| 797 | |
| 798 | static const struct address_family_t addr_link = { |
| 799 | "link", ARRAY_SIZE(link_methods), link_methods |
| 800 | }; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 801 | |
Denys Vlasenko | 2ace0ad | 2009-10-16 23:56:10 +0200 | [diff] [blame] | 802 | /* Returns pointer to the next word, or NULL. |
| 803 | * In 1st case, advances *buf to the word after this one. |
| 804 | */ |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 805 | static char *next_word(char **buf) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 806 | { |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 807 | unsigned length; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 808 | char *word; |
| 809 | |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 810 | /* Skip over leading whitespace */ |
Denis Vlasenko | d18a3a2 | 2006-10-25 12:46:03 +0000 | [diff] [blame] | 811 | word = skip_whitespace(*buf); |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 812 | |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 813 | /* Stop on EOL */ |
| 814 | if (*word == '\0') |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 815 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 816 | |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 817 | /* Find the length of this word (can't be 0) */ |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 818 | length = strcspn(word, " \t\n"); |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 819 | |
| 820 | /* Unless we are already at NUL, store NUL and advance */ |
| 821 | if (word[length] != '\0') |
| 822 | word[length++] = '\0'; |
| 823 | |
Denys Vlasenko | 2ace0ad | 2009-10-16 23:56:10 +0200 | [diff] [blame] | 824 | *buf = skip_whitespace(word + length); |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 825 | |
| 826 | return word; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 827 | } |
| 828 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 829 | static const struct address_family_t *get_address_family(const struct address_family_t *const af[], char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 830 | { |
| 831 | int i; |
| 832 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 833 | if (!name) |
| 834 | return NULL; |
| 835 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 836 | for (i = 0; af[i]; i++) { |
| 837 | if (strcmp(af[i]->name, name) == 0) { |
| 838 | return af[i]; |
| 839 | } |
| 840 | } |
| 841 | return NULL; |
| 842 | } |
| 843 | |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 844 | static const struct method_t *get_method(const struct address_family_t *af, char *name) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 845 | { |
| 846 | int i; |
| 847 | |
Denis Vlasenko | 736230e | 2006-11-20 19:40:36 +0000 | [diff] [blame] | 848 | if (!name) |
| 849 | return NULL; |
Bernhard Reutner-Fischer | e747f62 | 2007-10-06 20:47:53 +0000 | [diff] [blame] | 850 | /* TODO: use index_in_str_array() */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 851 | for (i = 0; i < af->n_methods; i++) { |
| 852 | if (strcmp(af->method[i].name, name) == 0) { |
| 853 | return &af->method[i]; |
| 854 | } |
| 855 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 856 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 857 | } |
| 858 | |
Denys Vlasenko | fb03663 | 2013-02-28 19:01:28 +0100 | [diff] [blame] | 859 | static struct interfaces_file_t *read_interfaces(const char *filename, struct interfaces_file_t *defn) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 860 | { |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 861 | /* Let's try to be compatible. |
| 862 | * |
| 863 | * "man 5 interfaces" says: |
| 864 | * Lines starting with "#" are ignored. Note that end-of-line |
| 865 | * comments are NOT supported, comments must be on a line of their own. |
| 866 | * A line may be extended across multiple lines by making |
| 867 | * the last character a backslash. |
| 868 | * |
| 869 | * Seen elsewhere in example config file: |
Denis Vlasenko | 4938292 | 2008-07-08 03:25:25 +0000 | [diff] [blame] | 870 | * A first non-blank "#" character makes the rest of the line |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 871 | * be ignored. Blank lines are ignored. Lines may be indented freely. |
| 872 | * A "\" character at the very end of the line indicates the next line |
| 873 | * should be treated as a continuation of the current one. |
Denys Vlasenko | fb03663 | 2013-02-28 19:01:28 +0100 | [diff] [blame] | 874 | * |
| 875 | * Lines beginning with "source" are used to include stanzas from |
| 876 | * other files, so configuration can be split into many files. |
| 877 | * The word "source" is followed by the path of file to be sourced. |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 878 | */ |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 879 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 880 | struct mapping_defn_t *currmap = NULL; |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 881 | #endif |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 882 | struct interface_defn_t *currif = NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 883 | FILE *f; |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 884 | char *buf; |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 885 | char *first_word; |
| 886 | char *rest_of_line; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 887 | enum { NONE, IFACE, MAPPING } currently_processing = NONE; |
| 888 | |
Denys Vlasenko | fb03663 | 2013-02-28 19:01:28 +0100 | [diff] [blame] | 889 | if (!defn) |
| 890 | defn = xzalloc(sizeof(*defn)); |
| 891 | |
| 892 | debug_noise("reading %s file:\n", filename); |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 893 | f = xfopen_for_read(filename); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 894 | |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 895 | while ((buf = xmalloc_fgetline(f)) != NULL) { |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 896 | #if ENABLE_DESKTOP |
| 897 | /* Trailing "\" concatenates lines */ |
| 898 | char *p; |
| 899 | while ((p = last_char_is(buf, '\\')) != NULL) { |
| 900 | *p = '\0'; |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 901 | rest_of_line = xmalloc_fgetline(f); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 902 | if (!rest_of_line) |
| 903 | break; |
| 904 | p = xasprintf("%s%s", buf, rest_of_line); |
Eric Andersen | 3c8bca3 | 2003-06-20 10:02:29 +0000 | [diff] [blame] | 905 | free(buf); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 906 | free(rest_of_line); |
| 907 | buf = p; |
| 908 | } |
| 909 | #endif |
| 910 | rest_of_line = buf; |
| 911 | first_word = next_word(&rest_of_line); |
Denis Vlasenko | 4938292 | 2008-07-08 03:25:25 +0000 | [diff] [blame] | 912 | if (!first_word || *first_word == '#') { |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 913 | free(buf); |
| 914 | continue; /* blank/comment line */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 915 | } |
| 916 | |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 917 | if (strcmp(first_word, "mapping") == 0) { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 918 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 919 | currmap = xzalloc(sizeof(*currmap)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 920 | |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 921 | while ((first_word = next_word(&rest_of_line)) != NULL) { |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 922 | currmap->match = xrealloc_vector(currmap->match, 4, currmap->n_matches); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 923 | currmap->match[currmap->n_matches++] = xstrdup(first_word); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 924 | } |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 925 | /*currmap->n_mappings = 0;*/ |
| 926 | /*currmap->mapping = NULL;*/ |
| 927 | /*currmap->script = NULL;*/ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 928 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 929 | struct mapping_defn_t **where = &defn->mappings; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 930 | while (*where != NULL) { |
| 931 | where = &(*where)->next; |
| 932 | } |
| 933 | *where = currmap; |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 934 | /*currmap->next = NULL;*/ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 935 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 936 | debug_noise("Added mapping\n"); |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 937 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 938 | currently_processing = MAPPING; |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 939 | } else if (strcmp(first_word, "iface") == 0) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 940 | static const struct address_family_t *const addr_fams[] = { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 941 | #if ENABLE_FEATURE_IFUPDOWN_IPV4 |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 942 | &addr_inet, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 943 | #endif |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 944 | #if ENABLE_FEATURE_IFUPDOWN_IPV6 |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 945 | &addr_inet6, |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 946 | #endif |
Kaarle Ritvanen | fe8390e | 2014-02-03 15:56:20 +0100 | [diff] [blame] | 947 | &addr_link, |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 948 | NULL |
| 949 | }; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 950 | char *iface_name; |
| 951 | char *address_family_name; |
| 952 | char *method_name; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 953 | |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 954 | currif = xzalloc(sizeof(*currif)); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 955 | iface_name = next_word(&rest_of_line); |
| 956 | address_family_name = next_word(&rest_of_line); |
| 957 | method_name = next_word(&rest_of_line); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 958 | |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 959 | if (method_name == NULL) |
| 960 | bb_error_msg_and_die("too few parameters for line \"%s\"", buf); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 961 | |
| 962 | /* ship any trailing whitespace */ |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 963 | rest_of_line = skip_whitespace(rest_of_line); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 964 | |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 965 | if (rest_of_line[0] != '\0' /* && rest_of_line[0] != '#' */) |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 966 | bb_error_msg_and_die("too many parameters \"%s\"", buf); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 967 | |
| 968 | currif->iface = xstrdup(iface_name); |
| 969 | |
| 970 | currif->address_family = get_address_family(addr_fams, address_family_name); |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 971 | if (!currif->address_family) |
| 972 | bb_error_msg_and_die("unknown address type \"%s\"", address_family_name); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 973 | |
| 974 | currif->method = get_method(currif->address_family, method_name); |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 975 | if (!currif->method) |
| 976 | bb_error_msg_and_die("unknown method \"%s\"", method_name); |
Nicolas Cavallari | e5aba88 | 2016-03-01 18:59:08 +0100 | [diff] [blame] | 977 | #if 0 |
| 978 | // Allegedly, Debian allows a duplicate definition: |
| 979 | // iface eth0 inet static |
| 980 | // address 192.168.0.15 |
| 981 | // netmask 255.255.0.0 |
| 982 | // gateway 192.168.0.1 |
| 983 | // |
| 984 | // iface eth0 inet static |
| 985 | // address 10.0.0.1 |
| 986 | // netmask 255.255.255.0 |
| 987 | // |
| 988 | // This adds *two* addresses to eth0 (probably requires use of "ip", not "ifconfig" |
| 989 | // |
Denys Vlasenko | 35e063e | 2016-03-03 02:19:16 +0100 | [diff] [blame] | 990 | llist_t *iface_list; |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 991 | for (iface_list = defn->ifaces; iface_list; iface_list = iface_list->link) { |
| 992 | struct interface_defn_t *tmp = (struct interface_defn_t *) iface_list->data; |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 993 | if ((strcmp(tmp->iface, currif->iface) == 0) |
| 994 | && (tmp->address_family == currif->address_family) |
| 995 | ) { |
| 996 | bb_error_msg_and_die("duplicate interface \"%s\"", tmp->iface); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
Nicolas Cavallari | e5aba88 | 2016-03-01 18:59:08 +0100 | [diff] [blame] | 999 | #endif |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1000 | llist_add_to_end(&(defn->ifaces), (char*)currif); |
| 1001 | |
| 1002 | debug_noise("iface %s %s %s\n", currif->iface, address_family_name, method_name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1003 | currently_processing = IFACE; |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1004 | } else if (strcmp(first_word, "auto") == 0) { |
| 1005 | while ((first_word = next_word(&rest_of_line)) != NULL) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1006 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1007 | /* Check the interface isnt already listed */ |
Denis Vlasenko | 0b791d9 | 2009-04-13 20:52:00 +0000 | [diff] [blame] | 1008 | if (llist_find_str(defn->autointerfaces, first_word)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1009 | bb_perror_msg_and_die("interface declared auto twice \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1010 | } |
| 1011 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1012 | /* Add the interface to the list */ |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1013 | llist_add_to_end(&(defn->autointerfaces), xstrdup(first_word)); |
| 1014 | debug_noise("\nauto %s\n", first_word); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1015 | } |
| 1016 | currently_processing = NONE; |
Denys Vlasenko | fb03663 | 2013-02-28 19:01:28 +0100 | [diff] [blame] | 1017 | } else if (strcmp(first_word, "source") == 0) { |
| 1018 | read_interfaces(next_word(&rest_of_line), defn); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1019 | } else { |
| 1020 | switch (currently_processing) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1021 | case IFACE: |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1022 | if (rest_of_line[0] == '\0') |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 1023 | bb_error_msg_and_die("option with empty value \"%s\"", buf); |
| 1024 | |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1025 | if (strcmp(first_word, "post-up") == 0) |
| 1026 | first_word += 5; /* "up" */ |
| 1027 | else if (strcmp(first_word, "pre-down") == 0) |
| 1028 | first_word += 4; /* "down" */ |
| 1029 | |
Denys Vlasenko | 9c28fb8 | 2011-10-19 02:37:08 +0200 | [diff] [blame] | 1030 | /* If not one of "up", "down",... words... */ |
| 1031 | if (index_in_strings(keywords_up_down, first_word) < 0) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1032 | int i; |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 1033 | for (i = 0; i < currif->n_options; i++) { |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1034 | if (strcmp(currif->option[i].name, first_word) == 0) |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 1035 | bb_error_msg_and_die("duplicate option \"%s\"", buf); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1036 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1037 | } |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1038 | debug_noise("\t%s=%s\n", first_word, rest_of_line); |
Denys Vlasenko | 9c28fb8 | 2011-10-19 02:37:08 +0200 | [diff] [blame] | 1039 | currif->option = xrealloc_vector(currif->option, 4, currif->n_options); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1040 | currif->option[currif->n_options].name = xstrdup(first_word); |
| 1041 | currif->option[currif->n_options].value = xstrdup(rest_of_line); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1042 | currif->n_options++; |
| 1043 | break; |
| 1044 | case MAPPING: |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1045 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1046 | if (strcmp(first_word, "script") == 0) { |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 1047 | if (currmap->script != NULL) |
| 1048 | bb_error_msg_and_die("duplicate script in mapping \"%s\"", buf); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1049 | currmap->script = xstrdup(next_word(&rest_of_line)); |
| 1050 | } else if (strcmp(first_word, "map") == 0) { |
Denys Vlasenko | 9c28fb8 | 2011-10-19 02:37:08 +0200 | [diff] [blame] | 1051 | currmap->mapping = xrealloc_vector(currmap->mapping, 2, currmap->n_mappings); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1052 | currmap->mapping[currmap->n_mappings] = xstrdup(next_word(&rest_of_line)); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1053 | currmap->n_mappings++; |
| 1054 | } else { |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 1055 | bb_error_msg_and_die("misplaced option \"%s\"", buf); |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1056 | } |
| 1057 | #endif |
| 1058 | break; |
| 1059 | case NONE: |
| 1060 | default: |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 1061 | bb_error_msg_and_die("misplaced option \"%s\"", buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1062 | } |
| 1063 | } |
Glenn L McGrath | 8573704 | 2003-01-14 23:26:57 +0000 | [diff] [blame] | 1064 | free(buf); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1065 | } /* while (fgets) */ |
| 1066 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1067 | if (ferror(f) != 0) { |
Denis Vlasenko | a483691 | 2007-07-03 08:26:24 +0000 | [diff] [blame] | 1068 | /* ferror does NOT set errno! */ |
| 1069 | bb_error_msg_and_die("%s: I/O error", filename); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1070 | } |
| 1071 | fclose(f); |
Denys Vlasenko | fb03663 | 2013-02-28 19:01:28 +0100 | [diff] [blame] | 1072 | debug_noise("\ndone reading %s\n\n", filename); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1073 | |
| 1074 | return defn; |
| 1075 | } |
| 1076 | |
Denis Vlasenko | ab2aea4 | 2007-01-29 22:51:58 +0000 | [diff] [blame] | 1077 | static char *setlocalenv(const char *format, const char *name, const char *value) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1078 | { |
| 1079 | char *result; |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1080 | char *dst; |
| 1081 | char *src; |
| 1082 | char c; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1083 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1084 | result = xasprintf(format, name, value); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1085 | |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1086 | for (dst = src = result; (c = *src) != '=' && c; src++) { |
| 1087 | if (c == '-') |
| 1088 | c = '_'; |
| 1089 | if (c >= 'a' && c <= 'z') |
| 1090 | c -= ('a' - 'A'); |
| 1091 | if (isalnum(c) || c == '_') |
| 1092 | *dst++ = c; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1093 | } |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1094 | overlapping_strcpy(dst, src); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1095 | |
| 1096 | return result; |
| 1097 | } |
| 1098 | |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1099 | static void set_environ(struct interface_defn_t *iface, const char *mode, const char *opt) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1100 | { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1101 | int i; |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1102 | char **pp; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1103 | |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1104 | if (G.my_environ != NULL) { |
| 1105 | for (pp = G.my_environ; *pp; pp++) { |
| 1106 | free(*pp); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1107 | } |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1108 | free(G.my_environ); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1109 | } |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1110 | |
| 1111 | /* note: last element will stay NULL: */ |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1112 | G.my_environ = xzalloc(sizeof(char *) * (iface->n_options + 7)); |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1113 | pp = G.my_environ; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1114 | |
| 1115 | for (i = 0; i < iface->n_options; i++) { |
Denys Vlasenko | 9c28fb8 | 2011-10-19 02:37:08 +0200 | [diff] [blame] | 1116 | if (index_in_strings(keywords_up_down, iface->option[i].name) >= 0) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1117 | continue; |
| 1118 | } |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1119 | *pp++ = setlocalenv("IF_%s=%s", iface->option[i].name, iface->option[i].value); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1122 | *pp++ = setlocalenv("%s=%s", "IFACE", iface->iface); |
| 1123 | *pp++ = setlocalenv("%s=%s", "ADDRFAM", iface->address_family->name); |
| 1124 | *pp++ = setlocalenv("%s=%s", "METHOD", iface->method->name); |
| 1125 | *pp++ = setlocalenv("%s=%s", "MODE", mode); |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1126 | *pp++ = setlocalenv("%s=%s", "PHASE", opt); |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1127 | if (G.startup_PATH) |
| 1128 | *pp++ = setlocalenv("%s=%s", "PATH", G.startup_PATH); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | static int doit(char *str) |
| 1132 | { |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 1133 | if (option_mask32 & (OPT_no_act|OPT_verbose)) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1134 | puts(str); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1135 | } |
Denis Vlasenko | c12f530 | 2006-10-06 09:49:47 +0000 | [diff] [blame] | 1136 | if (!(option_mask32 & OPT_no_act)) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1137 | pid_t child; |
| 1138 | int status; |
| 1139 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1140 | fflush_all(); |
Denis Vlasenko | 0764a7f | 2008-03-19 16:24:17 +0000 | [diff] [blame] | 1141 | child = vfork(); |
Denys Vlasenko | 681efe2 | 2011-03-08 21:00:36 +0100 | [diff] [blame] | 1142 | if (child < 0) /* failure */ |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1143 | return 0; |
Denys Vlasenko | 681efe2 | 2011-03-08 21:00:36 +0100 | [diff] [blame] | 1144 | if (child == 0) { /* child */ |
| 1145 | execle(G.shell, G.shell, "-c", str, (char *) NULL, G.my_environ); |
Denis Vlasenko | 0764a7f | 2008-03-19 16:24:17 +0000 | [diff] [blame] | 1146 | _exit(127); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1147 | } |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1148 | safe_waitpid(child, &status, 0); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1149 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) { |
| 1150 | return 0; |
| 1151 | } |
| 1152 | } |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1153 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1154 | } |
| 1155 | |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1156 | static int execute_all(struct interface_defn_t *ifd, const char *opt) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1157 | { |
| 1158 | int i; |
Eric Andersen | eb213bd | 2003-09-12 08:39:05 +0000 | [diff] [blame] | 1159 | char *buf; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1160 | for (i = 0; i < ifd->n_options; i++) { |
| 1161 | if (strcmp(ifd->option[i].name, opt) == 0) { |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1162 | if (!doit(ifd->option[i].value)) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1163 | return 0; |
| 1164 | } |
| 1165 | } |
| 1166 | } |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 1167 | |
Denys Vlasenko | bcf47ea | 2016-09-25 02:05:24 +0200 | [diff] [blame] | 1168 | /* Tested on Debian Squeeze: "standard" ifup runs this without |
| 1169 | * checking that directory exists. If it doesn't, run-parts |
| 1170 | * complains, and this message _is_ annoyingly visible. |
| 1171 | * Don't "fix" this (unless newer Debian does). |
| 1172 | */ |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1173 | buf = xasprintf("run-parts /etc/network/if-%s.d", opt); |
Denis Vlasenko | 2375d75 | 2006-12-19 23:15:46 +0000 | [diff] [blame] | 1174 | /* heh, we don't bother free'ing it */ |
| 1175 | return doit(buf); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1176 | } |
| 1177 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1178 | static int check(char *str) |
| 1179 | { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1180 | return str != NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1181 | } |
| 1182 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1183 | static int iface_up(struct interface_defn_t *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1184 | { |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1185 | if (!iface->method->up(iface, check)) return -1; |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1186 | set_environ(iface, "start", "pre-up"); |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1187 | if (!execute_all(iface, "pre-up")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 1188 | if (!iface->method->up(iface, doit)) return 0; |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1189 | set_environ(iface, "start", "post-up"); |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1190 | if (!execute_all(iface, "up")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 1191 | return 1; |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1192 | } |
| 1193 | |
| 1194 | static int iface_down(struct interface_defn_t *iface) |
| 1195 | { |
Denys Vlasenko | a0fd4a7 | 2012-09-02 14:29:14 +0200 | [diff] [blame] | 1196 | if (!iface->method->down(iface, check)) return -1; |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1197 | set_environ(iface, "stop", "pre-down"); |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1198 | if (!execute_all(iface, "down")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 1199 | if (!iface->method->down(iface, doit)) return 0; |
Andreas Oberritter | 3a9365e | 2012-04-23 22:08:39 +0200 | [diff] [blame] | 1200 | set_environ(iface, "stop", "post-down"); |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1201 | if (!execute_all(iface, "post-down")) return 0; |
Eric Andersen | 658f8b1 | 2003-12-19 10:46:00 +0000 | [diff] [blame] | 1202 | return 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1203 | } |
| 1204 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1205 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1206 | static int popen2(FILE **in, FILE **out, char *command, char *param) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1207 | { |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1208 | char *argv[3] = { command, param, NULL }; |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 1209 | struct fd_pair infd, outfd; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1210 | pid_t pid; |
| 1211 | |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 1212 | xpiped_pair(infd); |
| 1213 | xpiped_pair(outfd); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1214 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 1215 | fflush_all(); |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 1216 | pid = xvfork(); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1217 | |
Pascal Bellard | 926031b | 2010-07-04 15:32:38 +0200 | [diff] [blame] | 1218 | if (pid == 0) { |
| 1219 | /* Child */ |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1220 | /* NB: close _first_, then move fds! */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 1221 | close(infd.wr); |
| 1222 | close(outfd.rd); |
| 1223 | xmove_fd(infd.rd, 0); |
| 1224 | xmove_fd(outfd.wr, 1); |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 1225 | BB_EXECVP_or_die(argv); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1226 | } |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1227 | /* parent */ |
Denis Vlasenko | 3718832 | 2008-02-16 13:20:56 +0000 | [diff] [blame] | 1228 | close(infd.rd); |
| 1229 | close(outfd.wr); |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 1230 | *in = xfdopen_for_write(infd.wr); |
| 1231 | *out = xfdopen_for_read(outfd.rd); |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1232 | return pid; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1235 | static char *run_mapping(char *physical, struct mapping_defn_t *map) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1236 | { |
| 1237 | FILE *in, *out; |
| 1238 | int i, status; |
| 1239 | pid_t pid; |
| 1240 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1241 | char *logical = xstrdup(physical); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1242 | |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1243 | /* Run the mapping script. Never fails. */ |
| 1244 | pid = popen2(&in, &out, map->script, physical); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1245 | |
| 1246 | /* Write mappings to stdin of mapping script. */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1247 | for (i = 0; i < map->n_mappings; i++) { |
| 1248 | fprintf(in, "%s\n", map->mapping[i]); |
| 1249 | } |
| 1250 | fclose(in); |
Denis Vlasenko | fb0eba7 | 2008-01-02 19:55:04 +0000 | [diff] [blame] | 1251 | safe_waitpid(pid, &status, 0); |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1252 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1253 | if (WIFEXITED(status) && WEXITSTATUS(status) == 0) { |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1254 | /* If the mapping script exited successfully, try to |
| 1255 | * grab a line of output and use that as the name of the |
| 1256 | * logical interface. */ |
Denis Vlasenko | 8ee649a | 2008-03-26 20:04:27 +0000 | [diff] [blame] | 1257 | char *new_logical = xmalloc_fgetline(out); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1258 | |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1259 | if (new_logical) { |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1260 | /* If we are able to read a line of output from the script, |
| 1261 | * remove any trailing whitespace and use this value |
| 1262 | * as the name of the logical interface. */ |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 1263 | char *pch = new_logical + strlen(new_logical) - 1; |
Eric Andersen | 233b170 | 2003-06-05 19:37:01 +0000 | [diff] [blame] | 1264 | |
| 1265 | while (pch >= new_logical && isspace(*pch)) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1266 | *(pch--) = '\0'; |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1267 | |
| 1268 | free(logical); |
| 1269 | logical = new_logical; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1270 | } |
| 1271 | } |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1272 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1273 | fclose(out); |
| 1274 | |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1275 | return logical; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1276 | } |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1277 | #endif /* FEATURE_IFUPDOWN_MAPPING */ |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1278 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1279 | static llist_t *find_iface_state(llist_t *state_list, const char *iface) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1280 | { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1281 | llist_t *search = state_list; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1282 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1283 | while (search) { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 1284 | char *after_iface = is_prefixed_with(search->data, iface); |
| 1285 | if (after_iface |
| 1286 | && *after_iface == '=' |
Denis Vlasenko | 1e18f1b | 2008-02-16 13:19:19 +0000 | [diff] [blame] | 1287 | ) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1288 | return search; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1289 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1290 | search = search->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1291 | } |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1292 | return NULL; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1295 | /* read the previous state from the state file */ |
| 1296 | static llist_t *read_iface_state(void) |
| 1297 | { |
| 1298 | llist_t *state_list = NULL; |
Denys Vlasenko | 3720a61 | 2016-10-09 23:04:16 +0200 | [diff] [blame] | 1299 | FILE *state_fp = fopen_for_read(IFSTATE_FILE_PATH); |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1300 | |
| 1301 | if (state_fp) { |
| 1302 | char *start, *end_ptr; |
| 1303 | while ((start = xmalloc_fgets(state_fp)) != NULL) { |
| 1304 | /* We should only need to check for a single character */ |
| 1305 | end_ptr = start + strcspn(start, " \t\n"); |
| 1306 | *end_ptr = '\0'; |
| 1307 | llist_add_to(&state_list, start); |
| 1308 | } |
| 1309 | fclose(state_fp); |
| 1310 | } |
| 1311 | return state_list; |
| 1312 | } |
| 1313 | |
Denys Vlasenko | 3720a61 | 2016-10-09 23:04:16 +0200 | [diff] [blame] | 1314 | /* read the previous state from the state file */ |
| 1315 | static FILE *open_new_state_file(void) |
| 1316 | { |
| 1317 | int fd, flags, cnt; |
| 1318 | |
| 1319 | cnt = 0; |
| 1320 | flags = (O_WRONLY | O_CREAT | O_EXCL); |
| 1321 | for (;;) { |
| 1322 | fd = open(IFSTATE_FILE_PATH".new", flags, 0666); |
| 1323 | if (fd >= 0) |
| 1324 | break; |
| 1325 | if (errno != EEXIST |
| 1326 | || flags == (O_WRONLY | O_CREAT | O_TRUNC) |
| 1327 | ) { |
| 1328 | bb_perror_msg_and_die("can't open '%s'", |
| 1329 | IFSTATE_FILE_PATH".new"); |
| 1330 | } |
| 1331 | /* Someone else created the .new file */ |
| 1332 | if (cnt > 30 * 1000) { |
| 1333 | /* Waited for 30*30/2 = 450 milliseconds, still EEXIST. |
| 1334 | * Assuming a stale file, rewriting it. |
| 1335 | */ |
| 1336 | flags = (O_WRONLY | O_CREAT | O_TRUNC); |
| 1337 | continue; |
| 1338 | } |
| 1339 | usleep(cnt); |
| 1340 | cnt += 1000; |
| 1341 | } |
| 1342 | |
| 1343 | return xfdopen_for_write(fd); |
| 1344 | } |
| 1345 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 1346 | int ifupdown_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 1347 | int ifupdown_main(int argc UNUSED_PARAM, char **argv) |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1348 | { |
Denis Vlasenko | cd5c61c | 2008-02-15 16:20:26 +0000 | [diff] [blame] | 1349 | int (*cmds)(struct interface_defn_t *); |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1350 | struct interfaces_file_t *defn; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1351 | llist_t *target_list = NULL; |
Glenn L McGrath | d4004ee | 2004-09-14 17:24:59 +0000 | [diff] [blame] | 1352 | const char *interfaces = "/etc/network/interfaces"; |
Bernhard Reutner-Fischer | 16deb86 | 2007-03-19 19:54:56 +0000 | [diff] [blame] | 1353 | bool any_failures = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1354 | |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1355 | INIT_G(); |
| 1356 | |
| 1357 | G.startup_PATH = getenv("PATH"); |
Denys Vlasenko | 681efe2 | 2011-03-08 21:00:36 +0100 | [diff] [blame] | 1358 | G.shell = xstrdup(get_shell_name()); |
Denys Vlasenko | 62152da | 2009-06-05 21:53:11 +0200 | [diff] [blame] | 1359 | |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 1360 | if (ENABLE_IFUP |
| 1361 | && (!ENABLE_IFDOWN || applet_name[2] == 'u') |
| 1362 | ) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1363 | /* ifup command */ |
| 1364 | cmds = iface_up; |
Denys Vlasenko | a8c696b | 2016-11-14 18:31:07 +0100 | [diff] [blame] | 1365 | } else { |
| 1366 | cmds = iface_down; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1367 | } |
| 1368 | |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 1369 | getopt32(argv, OPTION_STR, &interfaces); |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 1370 | argv += optind; |
| 1371 | if (argv[0]) { |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1372 | if (DO_ALL) bb_show_usage(); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1373 | } else { |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1374 | if (!DO_ALL) bb_show_usage(); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1375 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1376 | |
Denys Vlasenko | fb03663 | 2013-02-28 19:01:28 +0100 | [diff] [blame] | 1377 | defn = read_interfaces(interfaces, NULL); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1378 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1379 | /* Create a list of interfaces to work on */ |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1380 | if (DO_ALL) { |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1381 | target_list = defn->autointerfaces; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1382 | } else { |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 1383 | llist_add_to_end(&target_list, argv[0]); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1384 | } |
| 1385 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1386 | /* Update the interfaces */ |
| 1387 | while (target_list) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1388 | llist_t *iface_list; |
| 1389 | struct interface_defn_t *currif; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1390 | char *iface; |
| 1391 | char *liface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1392 | char *pch; |
Bernhard Reutner-Fischer | 16deb86 | 2007-03-19 19:54:56 +0000 | [diff] [blame] | 1393 | bool okay = 0; |
Denis Vlasenko | 6b06cb8 | 2008-05-15 21:30:45 +0000 | [diff] [blame] | 1394 | int cmds_ret; |
Frank Bergmann | 6ca03f2 | 2015-03-13 10:05:08 +0100 | [diff] [blame] | 1395 | bool curr_failure = 0; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1396 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1397 | iface = xstrdup(target_list->data); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1398 | target_list = target_list->link; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1399 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1400 | pch = strchr(iface, '='); |
| 1401 | if (pch) { |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1402 | *pch = '\0'; |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1403 | liface = xstrdup(pch + 1); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1404 | } else { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 1405 | liface = xstrdup(iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1406 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1407 | |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1408 | if (!FORCE) { |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1409 | llist_t *state_list = read_iface_state(); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1410 | const llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1411 | |
| 1412 | if (cmds == iface_up) { |
| 1413 | /* ifup */ |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1414 | if (iface_state) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1415 | bb_error_msg("interface %s already configured", iface); |
Alexey Fomenko | 1df0af7 | 2011-03-02 04:13:21 +0100 | [diff] [blame] | 1416 | goto next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1417 | } |
| 1418 | } else { |
| 1419 | /* ifdown */ |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 1420 | if (!iface_state) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 1421 | bb_error_msg("interface %s not configured", iface); |
Alexey Fomenko | 1df0af7 | 2011-03-02 04:13:21 +0100 | [diff] [blame] | 1422 | goto next; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1423 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1424 | } |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1425 | llist_free(state_list, free); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1426 | } |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1427 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1428 | #if ENABLE_FEATURE_IFUPDOWN_MAPPING |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1429 | if ((cmds == iface_up) && !NO_MAPPINGS) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1430 | struct mapping_defn_t *currmap; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1431 | |
| 1432 | for (currmap = defn->mappings; currmap; currmap = currmap->next) { |
Denis Vlasenko | 4e33e07 | 2006-10-16 18:24:57 +0000 | [diff] [blame] | 1433 | int i; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1434 | for (i = 0; i < currmap->n_matches; i++) { |
| 1435 | if (fnmatch(currmap->match[i], liface, 0) != 0) |
| 1436 | continue; |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1437 | if (VERBOSE) { |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1438 | printf("Running mapping script %s on %s\n", currmap->script, liface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1439 | } |
Eric Andersen | 8a93179 | 2003-07-03 10:20:29 +0000 | [diff] [blame] | 1440 | liface = run_mapping(iface, currmap); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1441 | break; |
| 1442 | } |
| 1443 | } |
| 1444 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1445 | #endif |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1446 | |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1447 | iface_list = defn->ifaces; |
| 1448 | while (iface_list) { |
| 1449 | currif = (struct interface_defn_t *) iface_list->data; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1450 | if (strcmp(liface, currif->iface) == 0) { |
| 1451 | char *oldiface = currif->iface; |
| 1452 | |
| 1453 | okay = 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1454 | currif->iface = iface; |
| 1455 | |
Denis Vlasenko | 8cd1a28 | 2006-12-19 23:01:33 +0000 | [diff] [blame] | 1456 | debug_noise("\nConfiguring interface %s (%s)\n", liface, currif->address_family->name); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1457 | |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1458 | /* Call the cmds function pointer, does either iface_up() or iface_down() */ |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 1459 | cmds_ret = cmds(currif); |
| 1460 | if (cmds_ret == -1) { |
Denys Vlasenko | 6d8ea1d | 2015-03-22 17:08:51 +0100 | [diff] [blame] | 1461 | bb_error_msg("don't have all variables for %s/%s", |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1462 | liface, currif->address_family->name); |
Frank Bergmann | 6ca03f2 | 2015-03-13 10:05:08 +0100 | [diff] [blame] | 1463 | any_failures = curr_failure = 1; |
Glenn L McGrath | 0177ce1 | 2004-07-21 23:56:31 +0000 | [diff] [blame] | 1464 | } else if (cmds_ret == 0) { |
Frank Bergmann | 6ca03f2 | 2015-03-13 10:05:08 +0100 | [diff] [blame] | 1465 | any_failures = curr_failure = 1; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1466 | } |
Glenn L McGrath | cdbe5e5 | 2002-12-06 08:35:55 +0000 | [diff] [blame] | 1467 | |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1468 | currif->iface = oldiface; |
| 1469 | } |
Eric Andersen | 8320b42 | 2003-04-02 10:13:26 +0000 | [diff] [blame] | 1470 | iface_list = iface_list->link; |
| 1471 | } |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1472 | if (VERBOSE) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1473 | bb_putchar('\n'); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1474 | } |
| 1475 | |
Denis Vlasenko | 7f1f5b0 | 2006-09-23 12:49:01 +0000 | [diff] [blame] | 1476 | if (!okay && !FORCE) { |
Denis Vlasenko | 0534125 | 2006-09-26 20:35:30 +0000 | [diff] [blame] | 1477 | bb_error_msg("ignoring unknown interface %s", liface); |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1478 | any_failures = 1; |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1479 | } else if (!NO_ACT) { |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1480 | /* update the state file */ |
Denys Vlasenko | 3720a61 | 2016-10-09 23:04:16 +0200 | [diff] [blame] | 1481 | FILE *new_state_fp = open_new_state_file(); |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1482 | llist_t *state; |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1483 | llist_t *state_list = read_iface_state(); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1484 | llist_t *iface_state = find_iface_state(state_list, iface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1485 | |
Frank Bergmann | 6ca03f2 | 2015-03-13 10:05:08 +0100 | [diff] [blame] | 1486 | if (cmds == iface_up && !curr_failure) { |
Denys Vlasenko | d6f5000 | 2011-12-15 12:39:25 +0100 | [diff] [blame] | 1487 | char *newiface = xasprintf("%s=%s", iface, liface); |
| 1488 | if (!iface_state) { |
Rob Landley | 8bb5078 | 2006-05-26 23:44:51 +0000 | [diff] [blame] | 1489 | llist_add_to_end(&state_list, newiface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1490 | } else { |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1491 | free(iface_state->data); |
| 1492 | iface_state->data = newiface; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1493 | } |
Rob Landley | e813ddb | 2006-02-28 03:53:14 +0000 | [diff] [blame] | 1494 | } else { |
Denis Vlasenko | c115fdb | 2007-03-06 22:53:10 +0000 | [diff] [blame] | 1495 | /* Remove an interface from state_list */ |
| 1496 | llist_unlink(&state_list, iface_state); |
Rob Landley | a6e131d | 2006-05-29 06:43:55 +0000 | [diff] [blame] | 1497 | free(llist_pop(&iface_state)); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1498 | } |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1499 | |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1500 | /* Actually write the new state */ |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1501 | state = state_list; |
| 1502 | while (state) { |
| 1503 | if (state->data) { |
Denys Vlasenko | 3720a61 | 2016-10-09 23:04:16 +0200 | [diff] [blame] | 1504 | fprintf(new_state_fp, "%s\n", state->data); |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1505 | } |
Denis Vlasenko | f92df58 | 2007-05-02 22:22:23 +0000 | [diff] [blame] | 1506 | state = state->link; |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1507 | } |
Denys Vlasenko | 3720a61 | 2016-10-09 23:04:16 +0200 | [diff] [blame] | 1508 | fclose(new_state_fp); |
| 1509 | xrename(IFSTATE_FILE_PATH".new", IFSTATE_FILE_PATH); |
Denis Vlasenko | bd100b7 | 2007-05-02 21:38:44 +0000 | [diff] [blame] | 1510 | llist_free(state_list, free); |
Glenn L McGrath | 8e49caa | 2002-12-08 01:23:39 +0000 | [diff] [blame] | 1511 | } |
Alexey Fomenko | 1df0af7 | 2011-03-02 04:13:21 +0100 | [diff] [blame] | 1512 | next: |
| 1513 | free(iface); |
| 1514 | free(liface); |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Denis Vlasenko | fcfe834 | 2006-12-18 21:02:00 +0000 | [diff] [blame] | 1517 | return any_failures; |
Glenn L McGrath | 021fa7d | 2002-11-09 09:34:15 +0000 | [diff] [blame] | 1518 | } |