nameif.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * nameif.c - Naming Interfaces based on MAC address for busybox.
  4. *
  5. * Written 2000 by Andi Kleen.
  6. * Busybox port 2002 by Nick Fedchik <nick@fedchik.org.ua>
  7. * Glenn McGrath
  8. * Extended matching support 2008 by Nico Erfurth <masta@perlgolf.de>
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. //config:config NAMEIF
  13. //config: bool "nameif (6.6 kb)"
  14. //config: default y
  15. //config: select PLATFORM_LINUX
  16. //config: select FEATURE_SYSLOG
  17. //config: help
  18. //config: nameif is used to rename network interface by its MAC address.
  19. //config: Renamed interfaces MUST be in the down state.
  20. //config: It is possible to use a file (default: /etc/mactab)
  21. //config: with list of new interface names and MACs.
  22. //config: Maximum interface name length: IFNAMSIZ = 16
  23. //config: File fields are separated by space or tab.
  24. //config: File format:
  25. //config: # Comment
  26. //config: new_interface_name XX:XX:XX:XX:XX:XX
  27. //config:
  28. //config:config FEATURE_NAMEIF_EXTENDED
  29. //config: bool "Extended nameif"
  30. //config: default y
  31. //config: depends on NAMEIF
  32. //config: help
  33. //config: This extends the nameif syntax to support the bus_info, driver,
  34. //config: phyaddr selectors. The syntax is compatible to the normal nameif.
  35. //config: File format:
  36. //config: new_interface_name driver=asix bus=usb-0000:00:08.2-3
  37. //config: new_interface_name bus=usb-0000:00:08.2-3 00:80:C8:38:91:B5
  38. //config: new_interface_name phy_address=2 00:80:C8:38:91:B5
  39. //config: new_interface_name mac=00:80:C8:38:91:B5
  40. //config: new_interface_name 00:80:C8:38:91:B5
  41. //applet:IF_NAMEIF(APPLET_NOEXEC(nameif, nameif, BB_DIR_SBIN, BB_SUID_DROP, nameif))
  42. //kbuild:lib-$(CONFIG_NAMEIF) += nameif.o
  43. //usage:#define nameif_trivial_usage
  44. //usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
  45. //usage: "[-s] [-c FILE] [IFNAME HWADDR]..."
  46. //usage: )
  47. //usage: IF_FEATURE_NAMEIF_EXTENDED(
  48. //usage: "[-s] [-c FILE] [IFNAME SELECTOR]..."
  49. //usage: )
  50. //usage:#define nameif_full_usage "\n\n"
  51. //usage: "Rename network interface while it in the down state."
  52. //usage: IF_NOT_FEATURE_NAMEIF_EXTENDED(
  53. //usage: "\nThe device with address HWADDR is renamed to IFACE."
  54. //usage: )
  55. //usage: IF_FEATURE_NAMEIF_EXTENDED(
  56. //usage: "\nThe device matched by SELECTOR is renamed to IFACE."
  57. //usage: "\nSELECTOR can be a combination of:"
  58. //usage: "\n driver=STRING"
  59. //usage: "\n bus=STRING"
  60. //usage: "\n phy_address=NUM"
  61. //usage: "\n [mac=]XX:XX:XX:XX:XX:XX"
  62. //usage: )
  63. //usage: "\n"
  64. //usage: "\n -c FILE Configuration file (default: /etc/mactab)"
  65. //usage: "\n -s Log to syslog"
  66. //usage:
  67. //usage:#define nameif_example_usage
  68. //usage: "$ nameif -s dmz0 00:A0:C9:8C:F6:3F\n"
  69. //usage: " or\n"
  70. //usage: "$ nameif -c /etc/my_mactab_file\n"
  71. #include "libbb.h"
  72. #include <syslog.h>
  73. #include <net/if.h>
  74. #include <netinet/ether.h>
  75. #include <linux/sockios.h>
  76. #ifndef IFNAMSIZ
  77. #define IFNAMSIZ 16
  78. #endif
  79. /* Taken from linux/sockios.h */
  80. #define SIOCSIFNAME 0x8923 /* set interface name */
  81. /* Octets in one Ethernet addr, from <linux/if_ether.h> */
  82. #define ETH_ALEN 6
  83. #ifndef ifr_newname
  84. #define ifr_newname ifr_ifru.ifru_slave
  85. #endif
  86. typedef struct ethtable_s {
  87. struct ethtable_s *next;
  88. struct ethtable_s *prev;
  89. char *ifname;
  90. struct ether_addr *mac;
  91. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  92. char *bus_info;
  93. char *driver;
  94. int32_t phy_address;
  95. #endif
  96. } ethtable_t;
  97. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  98. /* Cut'n'paste from ethtool.h */
  99. #define ETHTOOL_BUSINFO_LEN 32
  100. /* these strings are set to whatever the driver author decides... */
  101. struct ethtool_drvinfo {
  102. uint32_t cmd;
  103. char driver[32]; /* driver short name, "tulip", "eepro100" */
  104. char version[32]; /* driver version string */
  105. char fw_version[32]; /* firmware version string, if applicable */
  106. char bus_info[ETHTOOL_BUSINFO_LEN]; /* Bus info for this IF. */
  107. /* For PCI devices, use pci_dev->slot_name. */
  108. char reserved1[32];
  109. char reserved2[16];
  110. uint32_t n_stats; /* number of u64's from ETHTOOL_GSTATS */
  111. uint32_t testinfo_len;
  112. uint32_t eedump_len; /* Size of data from ETHTOOL_GEEPROM (bytes) */
  113. uint32_t regdump_len; /* Size of data from ETHTOOL_GREGS (bytes) */
  114. };
  115. struct ethtool_cmd {
  116. uint32_t cmd;
  117. uint32_t supported; /* Features this interface supports */
  118. uint32_t advertising; /* Features this interface advertises */
  119. uint16_t speed; /* The forced speed, 10Mb, 100Mb, gigabit */
  120. uint8_t duplex; /* Duplex, half or full */
  121. uint8_t port; /* Which connector port */
  122. uint8_t phy_address;
  123. uint8_t transceiver; /* Which transceiver to use */
  124. uint8_t autoneg; /* Enable or disable autonegotiation */
  125. uint32_t maxtxpkt; /* Tx pkts before generating tx int */
  126. uint32_t maxrxpkt; /* Rx pkts before generating rx int */
  127. uint16_t speed_hi;
  128. uint16_t reserved2;
  129. uint32_t reserved[3];
  130. };
  131. #define ETHTOOL_GSET 0x00000001 /* Get settings. */
  132. #define ETHTOOL_GDRVINFO 0x00000003 /* Get driver info. */
  133. #endif
  134. static void nameif_parse_selector(ethtable_t *ch, char *selector)
  135. {
  136. struct ether_addr *lmac;
  137. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  138. int found_selector = 0;
  139. while (*selector) {
  140. char *next;
  141. #endif
  142. selector = skip_whitespace(selector);
  143. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  144. ch->phy_address = -1;
  145. if (*selector == '\0')
  146. break;
  147. /* Search for the end .... */
  148. next = skip_non_whitespace(selector);
  149. if (*next)
  150. *next++ = '\0';
  151. /* Check for selectors, mac= is assumed */
  152. if (is_prefixed_with(selector, "bus=")) {
  153. ch->bus_info = xstrdup(selector + 4);
  154. found_selector++;
  155. } else if (is_prefixed_with(selector, "driver=")) {
  156. ch->driver = xstrdup(selector + 7);
  157. found_selector++;
  158. } else if (is_prefixed_with(selector, "phyaddr=")) {
  159. ch->phy_address = xatoi_positive(selector + 8);
  160. found_selector++;
  161. } else {
  162. #endif
  163. lmac = xmalloc(ETH_ALEN);
  164. ch->mac = ether_aton_r(selector + (is_prefixed_with(selector, "mac=") ? 4 : 0), lmac);
  165. if (ch->mac == NULL)
  166. bb_error_msg_and_die("can't parse %s", selector);
  167. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  168. found_selector++;
  169. };
  170. selector = next;
  171. }
  172. if (found_selector == 0)
  173. bb_error_msg_and_die("no selectors found for %s", ch->ifname);
  174. #endif
  175. }
  176. static void prepend_new_eth_table(ethtable_t **clist, char *ifname, char *selector)
  177. {
  178. ethtable_t *ch;
  179. if (strlen(ifname) >= IFNAMSIZ)
  180. bb_error_msg_and_die("interface name '%s' too long", ifname);
  181. ch = xzalloc(sizeof(*ch));
  182. ch->ifname = xstrdup(ifname);
  183. nameif_parse_selector(ch, selector);
  184. ch->next = *clist;
  185. if (*clist)
  186. (*clist)->prev = ch;
  187. *clist = ch;
  188. }
  189. #if ENABLE_FEATURE_CLEAN_UP
  190. static void delete_eth_table(ethtable_t *ch)
  191. {
  192. free(ch->ifname);
  193. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  194. free(ch->bus_info);
  195. free(ch->driver);
  196. #endif
  197. free(ch->mac);
  198. free(ch);
  199. };
  200. #else
  201. void delete_eth_table(ethtable_t *ch);
  202. #endif
  203. int nameif_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  204. int nameif_main(int argc UNUSED_PARAM, char **argv)
  205. {
  206. ethtable_t *clist = NULL;
  207. const char *fname = "/etc/mactab";
  208. int ctl_sk;
  209. ethtable_t *ch;
  210. parser_t *parser;
  211. char *token[2];
  212. if (1 & getopt32(argv, "sc:", &fname)) {
  213. openlog(applet_name, 0, LOG_LOCAL0);
  214. /* Why not just "="? I assume logging to stderr
  215. * can't hurt. 2>/dev/null if you don't like it: */
  216. logmode |= LOGMODE_SYSLOG;
  217. }
  218. argv += optind;
  219. if (argv[0]) {
  220. do {
  221. if (!argv[1])
  222. bb_show_usage();
  223. prepend_new_eth_table(&clist, argv[0], argv[1]);
  224. argv += 2;
  225. } while (*argv);
  226. } else {
  227. parser = config_open(fname);
  228. while (config_read(parser, token, 2, 2, "# \t", PARSE_NORMAL))
  229. prepend_new_eth_table(&clist, token[0], token[1]);
  230. config_close(parser);
  231. }
  232. ctl_sk = xsocket(PF_INET, SOCK_DGRAM, 0);
  233. parser = config_open2("/proc/net/dev", xfopen_for_read);
  234. while (clist && config_read(parser, token, 2, 2, "\0: \t", PARSE_NORMAL)) {
  235. struct ifreq ifr;
  236. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  237. struct ethtool_drvinfo drvinfo;
  238. struct ethtool_cmd eth_settings;
  239. #endif
  240. if (parser->lineno <= 2)
  241. continue; /* Skip the first two lines */
  242. /* Find the current interface name and copy it to ifr.ifr_name */
  243. memset(&ifr, 0, sizeof(struct ifreq));
  244. strncpy_IFNAMSIZ(ifr.ifr_name, token[0]);
  245. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  246. /* Check for phy address */
  247. memset(&eth_settings, 0, sizeof(eth_settings));
  248. eth_settings.cmd = ETHTOOL_GSET;
  249. ifr.ifr_data = (caddr_t) &eth_settings;
  250. ioctl(ctl_sk, SIOCETHTOOL, &ifr);
  251. /* Check for driver etc. */
  252. memset(&drvinfo, 0, sizeof(drvinfo));
  253. drvinfo.cmd = ETHTOOL_GDRVINFO;
  254. ifr.ifr_data = (caddr_t) &drvinfo;
  255. /* Get driver and businfo first, so we have it in drvinfo */
  256. ioctl(ctl_sk, SIOCETHTOOL, &ifr);
  257. #endif
  258. ioctl(ctl_sk, SIOCGIFHWADDR, &ifr);
  259. /* Search the list for a matching device */
  260. for (ch = clist; ch; ch = ch->next) {
  261. #if ENABLE_FEATURE_NAMEIF_EXTENDED
  262. if (ch->bus_info && strcmp(ch->bus_info, drvinfo.bus_info) != 0)
  263. continue;
  264. if (ch->driver && strcmp(ch->driver, drvinfo.driver) != 0)
  265. continue;
  266. if (ch->phy_address != -1 && ch->phy_address != eth_settings.phy_address)
  267. continue;
  268. #endif
  269. if (ch->mac && memcmp(ch->mac, ifr.ifr_hwaddr.sa_data, ETH_ALEN) != 0)
  270. continue;
  271. /* if we came here, all selectors have matched */
  272. goto found;
  273. }
  274. /* Nothing found for current interface */
  275. continue;
  276. found:
  277. if (strcmp(ifr.ifr_name, ch->ifname) != 0) {
  278. strcpy(ifr.ifr_newname, ch->ifname);
  279. ioctl_or_perror_and_die(ctl_sk, SIOCSIFNAME, &ifr,
  280. "can't change ifname %s to %s",
  281. ifr.ifr_name, ch->ifname);
  282. }
  283. /* Remove list entry of renamed interface */
  284. if (ch->prev != NULL)
  285. ch->prev->next = ch->next;
  286. else
  287. clist = ch->next;
  288. if (ch->next != NULL)
  289. ch->next->prev = ch->prev;
  290. if (ENABLE_FEATURE_CLEAN_UP)
  291. delete_eth_table(ch);
  292. } /* while */
  293. if (ENABLE_FEATURE_CLEAN_UP) {
  294. ethtable_t *next;
  295. for (ch = clist; ch; ch = next) {
  296. next = ch->next;
  297. delete_eth_table(ch);
  298. }
  299. config_close(parser);
  300. };
  301. return 0;
  302. }