nameif.c 9.7 KB

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