nameif.c 9.6 KB

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