// return -1 if the interface was not found; if the interface was found retrn 0 and fill in IP address and mask int net_get_if_addr(const char *bridge, uint32_t *ip, uint32_t *mask, uint8_t mac[6], int *mtu) { assert(bridge); assert(ip); assert(mask); if (arg_debug) printf("get interface %s configuration\n", bridge); int rv = -1; struct ifaddrs *ifaddr, *ifa; if (getifaddrs(&ifaddr) == -1) errExit("getifaddrs"); // walk through the linked list; if the interface is found, extract IP address and mask for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL) continue; if (strcmp(ifa->ifa_name, bridge) != 0) continue; if (ifa->ifa_addr->sa_family == AF_INET) { struct sockaddr_in *si = (struct sockaddr_in *) ifa->ifa_netmask; *mask = ntohl(si->sin_addr.s_addr); si = (struct sockaddr_in *) ifa->ifa_addr; *ip = ntohl(si->sin_addr.s_addr); if (strcmp(ifa->ifa_name, "lo") != 0) { net_get_mac(ifa->ifa_name, mac); *mtu = net_get_mtu(bridge); } rv = 0; break; } } freeifaddrs(ifaddr); return rv; }
// scan interfaces in current namespace and print IP address/mask for each interface void net_ifprint(void) { uint32_t ip; uint32_t mask; struct ifaddrs *ifaddr, *ifa; if (getifaddrs(&ifaddr) == -1) errExit("getifaddrs"); printf("%-17.17s%-19.19s%-17.17s%-17.17s%-6.6s\n", "Interface", "MAC", "IP", "Mask", "Status"); // walk through the linked list for (ifa = ifaddr; ifa != NULL; ifa = ifa->ifa_next) { if (ifa->ifa_addr == NULL) continue; if (ifa->ifa_addr->sa_family == AF_INET) { struct sockaddr_in *si = (struct sockaddr_in *) ifa->ifa_netmask; mask = ntohl(si->sin_addr.s_addr); si = (struct sockaddr_in *) ifa->ifa_addr; ip = ntohl(si->sin_addr.s_addr); // interface status char *status; if (ifa->ifa_flags & IFF_RUNNING && ifa->ifa_flags & IFF_UP) status = "UP"; else status = "DOWN"; // ip address and mask char ipstr[30]; sprintf(ipstr, "%d.%d.%d.%d", PRINT_IP(ip)); char maskstr[30]; sprintf(maskstr, "%d.%d.%d.%d", PRINT_IP(mask)); // mac address unsigned char mac[6]; net_get_mac(ifa->ifa_name, mac); char macstr[30]; if (strcmp(ifa->ifa_name, "lo") == 0) macstr[0] = '\0'; else sprintf(macstr, "%02x:%02x:%02x:%02x:%02x:%02x", PRINT_MAC(mac)); // print printf("%-17.17s%-19.19s%-17.17s%-17.17s%-6.6s\n", ifa->ifa_name, macstr, ipstr, maskstr, status); // network scanning if (!arg_scan) // scanning disabled continue; if (strcmp(ifa->ifa_name, "lo") == 0) // no loopbabck scanning continue; if (mask2bits(mask) < 16) // not scanning large networks continue; if (!ip) // if not configured continue; // only if the interface is up and running if (ifa->ifa_flags & IFF_RUNNING && ifa->ifa_flags & IFF_UP) arp_scan(ifa->ifa_name, ip, mask); } } freeifaddrs(ifaddr); }
int main(void) { NetbootParam *param; // Initialize some consoles. serial_console_init(); cbmem_console_init(); video_console_init(); input_init(); printf("\n\nStarting netboot on " CONFIG_BOARD "...\n"); timestamp_init(); if (run_init_funcs()) halt(); // Make sure graphics are available if they aren't already. enable_graphics(); dc_usb_initialize(); srand(timer_raw_value()); printf("Looking for network device... "); while (!net_get_device()) usb_poll(); printf("done.\n"); printf("Waiting for link... "); int ready = 0; while (!ready) { if (net_ready(&ready)) halt(); } mdelay(200); // some dongles need more time than they think printf("done.\n"); // Start up the network stack. uip_init(); // Plug in the MAC address. const uip_eth_addr *mac_addr = net_get_mac(); if (!mac_addr) halt(); printf("MAC: "); print_mac_addr(mac_addr); printf("\n"); uip_setethaddr(*mac_addr); // Find out who we are. uip_ipaddr_t my_ip, next_ip, server_ip; const char *dhcp_bootfile; while (dhcp_request(&next_ip, &server_ip, &dhcp_bootfile)) printf("Dhcp failed, retrying.\n"); printf("My ip is "); uip_gethostaddr(&my_ip); print_ip_addr(&my_ip); printf("\nThe DHCP server ip is "); print_ip_addr(&server_ip); printf("\n"); // Retrieve settings from the shared data area. FmapArea shared_data; if (fmap_find_area("SHARED_DATA", &shared_data)) { printf("Couldn't find the shared data area.\n"); halt(); } void *data = flash_read(shared_data.offset, shared_data.size); netboot_params_init(data, shared_data.size); // Get TFTP server IP and file name from params with DHCP as fallback uip_ipaddr_t *tftp_ip = NULL; param = netboot_params_val(NetbootParamIdTftpServerIp); if (param->data && param->size >= sizeof(uip_ipaddr_t)) { tftp_ip = (uip_ipaddr_t *)param->data; printf("TFTP server IP set from firmware parameters: "); } else { tftp_ip = &next_ip; printf("TFTP server IP supplied by DHCP server: "); } print_ip_addr(tftp_ip); printf("\n"); const char *bootfile = NULL; param = netboot_params_val(NetbootParamIdBootfile); if (param->data && param->size > 0 && strnlen((const char *)param->data, param->size) < param->size) { bootfile = (const char *)param->data; printf("Bootfile set from firmware parameters: %s\n", bootfile); } else { bootfile = dhcp_bootfile; printf("Bootfile supplied by DHCP server: %s\n", bootfile); } // Download the bootfile. uint32_t size; if (tftp_read(payload, tftp_ip, bootfile, &size, MaxPayloadSize)) { printf("Tftp failed.\n"); if (dhcp_release(server_ip)) printf("Dhcp release failed.\n"); halt(); } printf("The bootfile was %d bytes long.\n", size); // Use command line from params when present (added to the default). param = netboot_params_val(NetbootParamIdKernelArgs); if (param->data && param->size > 0 && *(char *)param->data != '\0') { cmd_line[sizeof(def_cmd_line) - 1] = ' '; strncpy(&cmd_line[sizeof(def_cmd_line)], param->data, sizeof(cmd_line) - sizeof(def_cmd_line)); printf("Command line set from firmware parameters.\n"); // Otherwise, try to fetch it dynamically as a TFTP file. } else if (!(tftp_read(cmd_line, tftp_ip, "cmdline." CONFIG_BOARD, &size, sizeof(cmd_line) - 1))) { while (cmd_line[size - 1] <= ' ') // strip trailing whitespace if (!--size) break; // and control chars (\n, \r) cmd_line[size] = '\0'; while (size--) // replace inline control if (cmd_line[size] < ' ') // chars with spaces cmd_line[size] = ' '; printf("Command line loaded dynamically from TFTP server.\n"); // If the file doesn't exist, finally fall back to built-in default. } else { printf("No command line from TFTP, falling back to default.\n"); } cmd_line[sizeof(cmd_line) - 1] = '\0'; // We're done on the network, so release our IP. if (dhcp_release(server_ip)) { printf("Dhcp release failed.\n"); halt(); } // Add tftp server IP into command line. static const char def_tftp_cmdline[] = " tftpserverip=xxx.xxx.xxx.xxx"; const int tftp_cmdline_def_size = sizeof(def_tftp_cmdline) - 1; int cmd_line_size = strlen(cmd_line); if (cmd_line_size + tftp_cmdline_def_size >= sizeof(cmd_line)) { printf("Out of space adding TFTP server IP to the command line.\n"); return 1; } sprintf(&cmd_line[cmd_line_size], " tftpserverip=%d.%d.%d.%d", uip_ipaddr1(tftp_ip), uip_ipaddr2(tftp_ip), uip_ipaddr3(tftp_ip), uip_ipaddr4(tftp_ip)); printf("The command line is: %s\n", cmd_line); // Boot. boot(payload, cmd_line, NULL, NULL); // We should never get here. printf("Got to the end!\n"); halt(); return 0; }