Enscript Output

extractedLnx/linux-2.6.2/Documentation/networking/ifenslave.c_main.c

int
main(int argc, char **argv)
{
	struct ifreq  ifr2, if_hwaddr, if_ipaddr, if_metric, if_mtu, if_dstaddr;
	struct ifreq  if_netmask, if_brdaddr, if_flags;
	int rv, goterr = 0;
	int c, errflag = 0;
	sa_family_t master_family;
	char **spp, *master_ifname, *slave_ifname;
	int hwaddr_notset;
	int abi_ver = 0;

	while ((c = getopt_long(argc, argv, "acdfrvV?h", longopts, 0)) != EOF)
		switch (c) {
		case 'a': opt_a++; break;
		case 'f': opt_f++; break;
		case 'r': opt_r++; break;
		case 'd': opt_d++; break;
		case 'c': opt_c++; break;
		case 'v': verbose++;		break;
		case 'V': opt_version++;	break;
		case 'h': opt_howto++;	break;
		case '?': errflag++;
		}

	/* option check */
	if (opt_c)
		if(opt_a || opt_f || opt_r || opt_d || verbose || opt_version ||
		   opt_howto || errflag ) {
			fprintf(stderr, usage_msg);
			return 2;
		}

	if (errflag) {
		fprintf(stderr, usage_msg);
		return 2;
	}

	if (opt_howto) {
		fprintf(stderr, howto_msg);
		return 0;
	}

	if (verbose || opt_version) {
		printf(version);
		if (opt_version)
			exit(0);
	}

	/* Open a basic socket. */
	if ((skfd = socket(AF_INET, SOCK_DGRAM,0)) < 0) {
		perror("socket");
		exit(-1);
	}

	if (verbose)
		fprintf(stderr, "DEBUG: argc=%d, optind=%d and argv[optind] is %s.\n",
				argc, optind, argv[optind]);

	/* No remaining args means show all interfaces. */
	if (optind == argc) {
		if_print((char *)NULL);
		(void) close(skfd);
		exit(0);
	}

	/* Copy the interface name. */
	spp = argv + optind;
	master_ifname = *spp++;
	slave_ifname = *spp++;

	/* Check command line. */
	if (opt_c) {
		char **tempp = spp;
		if ((master_ifname == NULL)||(slave_ifname == NULL)||(*tempp++ != NULL)) {
			fprintf(stderr, usage_msg);
			(void) close(skfd);
			return 2;
		}
	}

	/* A single args means show the configuration for this interface. */
	if (slave_ifname == NULL) {
		if_print(master_ifname);
		(void) close(skfd);
		exit(0);
	}

	/* exchange abi version with bonding driver */
	abi_ver = get_abi_ver(master_ifname);
	if (abi_ver < 0) {
		(void) close(skfd);
		exit(1);
	}

	/* Get the vitals from the master interface. */
	{
		struct ifreq *ifra[7] = { &if_ipaddr, &if_mtu, &if_dstaddr,
								  &if_brdaddr, &if_netmask, &if_flags,
								  &if_hwaddr };
		const char *req_name[7] = {
			"IP address", "MTU", "destination address",
			"broadcast address", "netmask", "status flags",
			"hardware address" };
		const int ioctl_req_type[7] = {
			SIOCGIFADDR, SIOCGIFMTU, SIOCGIFDSTADDR,
			SIOCGIFBRDADDR, SIOCGIFNETMASK, SIOCGIFFLAGS,
			SIOCGIFHWADDR };
		int i;

		for (i = 0; i < 7; i++) {
			strncpy(ifra[i]->ifr_name, master_ifname, IFNAMSIZ);
			if (ioctl(skfd, ioctl_req_type[i], ifra[i]) < 0) {
				fprintf(stderr,
						"Something broke getting the master's %s: %s.\n",
						req_name[i], strerror(errno));
			}
		}

		/* check if master is up; if not then fail any operation */
		if (!(if_flags.ifr_flags & IFF_UP)) {
			fprintf(stderr, "Illegal operation; the specified master interface '%s' is not up.\n", master_ifname);
			(void) close(skfd);
			exit (1);
		}

		hwaddr_notset = 1; /* assume master's address not set yet */
		for (i = 0; hwaddr_notset && (i < 6); i++) {
			hwaddr_notset &= ((unsigned char *)if_hwaddr.ifr_hwaddr.sa_data)[i] == 0;
		}

		/* The family '1' is ARPHRD_ETHER for ethernet. */
		if (if_hwaddr.ifr_hwaddr.sa_family != 1 && !opt_f) {
			fprintf(stderr, "The specified master interface '%s' is not"
					" ethernet-like.\n  This program is designed to work"
					" with ethernet-like network interfaces.\n"
					" Use the '-f' option to force the operation.\n",
					master_ifname);
			(void) close(skfd);
			exit (1);
		}
		master_family = if_hwaddr.ifr_hwaddr.sa_family;
		if (verbose) {
			unsigned char *hwaddr = (unsigned char *)if_hwaddr.ifr_hwaddr.sa_data;
			printf("The current hardware address (SIOCGIFHWADDR) of %s is type %d  "
				   "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n", master_ifname,
				   if_hwaddr.ifr_hwaddr.sa_family, hwaddr[0], hwaddr[1],
				   hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
		}
	}


	/* do this when enslaving interfaces */
	do {
		if (opt_d) {  /* detach a slave interface from the master */
			strncpy(if_flags.ifr_name, master_ifname, IFNAMSIZ);
			strncpy(if_flags.ifr_slave, slave_ifname, IFNAMSIZ);
			if ((ioctl(skfd, SIOCBONDRELEASE, &if_flags) < 0) &&
				(ioctl(skfd, BOND_RELEASE_OLD, &if_flags) < 0)) {
					fprintf(stderr,	"SIOCBONDRELEASE: cannot detach %s from %s. errno=%s.\n",
							slave_ifname, master_ifname, strerror(errno));
			}
			else if (abi_ver < 1) {
			      	/* The driver is using an old ABI, so we'll set the interface
				 * down to avoid any conflicts due to same IP/MAC
				 */
				strncpy(ifr2.ifr_name, slave_ifname, IFNAMSIZ);
				if (ioctl(skfd, SIOCGIFFLAGS, &ifr2) < 0) {
					int saved_errno = errno;
					fprintf(stderr, "SIOCGIFFLAGS on %s failed: %s\n", slave_ifname,
						strerror(saved_errno));
				}
				else {
					ifr2.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
					if (ioctl(skfd, SIOCSIFFLAGS, &ifr2) < 0) {
						int saved_errno = errno;
						fprintf(stderr, "Shutting down interface %s failed: %s\n",
							slave_ifname, strerror(saved_errno));
					}
				}
			}
		} else if (opt_c) { /* change primary slave */
			strncpy(if_flags.ifr_name, master_ifname, IFNAMSIZ);
			strncpy(if_flags.ifr_slave, slave_ifname, IFNAMSIZ);
			if ((ioctl(skfd, SIOCBONDCHANGEACTIVE, &if_flags) < 0) &&
			    (ioctl(skfd, BOND_CHANGE_ACTIVE_OLD, &if_flags) < 0)) {
				fprintf(stderr,	"SIOCBONDCHANGEACTIVE: %s.\n", strerror(errno));
			}
		} else {  /* attach a slave interface to the master */

			strncpy(ifr2.ifr_name, slave_ifname, IFNAMSIZ);
			if (ioctl(skfd, SIOCGIFFLAGS, &ifr2) < 0) {
				int saved_errno = errno;
				fprintf(stderr, "SIOCGIFFLAGS on %s failed: %s\n", slave_ifname,
						strerror(saved_errno));
				(void) close(skfd);
				return 1;
			}

			if ((ifr2.ifr_flags & IFF_SLAVE) && !opt_r) {
				fprintf(stderr, "%s is already a slave\n", slave_ifname);
				(void) close(skfd);
				return 1;
			}

			/* if hwaddr_notset, assign the slave hw address to the master */
			if (hwaddr_notset) {
				/* assign the slave hw address to the
				 * master since it currently does not
				 * have one; otherwise, slaves may
				 * have different hw addresses in
				 * active-backup mode as seen when enslaving
				 * using "ifenslave bond0 eth0 eth1" because
				 * hwaddr_notset is set outside this loop.
				 * TODO: put this and the "else" portion in
				 *       a function.
				 */
				/* get the slaves MAC address */
				strncpy(if_hwaddr.ifr_name, slave_ifname,
					IFNAMSIZ);
				rv = ioctl(skfd, SIOCGIFHWADDR, &if_hwaddr);
				if (-1 == rv) {
					fprintf(stderr, "Could not get MAC "
						"address of %s: %s\n",
						slave_ifname,
						strerror(errno));
					strncpy(if_hwaddr.ifr_name,
						master_ifname, IFNAMSIZ);
					goterr = 1;
				}

				if (!goterr) {
					if (abi_ver < 1) {
						/* In ABI versions older than 1, the
						 * master's set_mac routine couldn't
						 * work if it was up, because it
						 * used the default ethernet set_mac
						 * function.
						 */
						/* bring master down */
						if_flags.ifr_flags &= ~IFF_UP;
						if (ioctl(skfd, SIOCSIFFLAGS,
								&if_flags) < 0) {
							goterr = 1;
							fprintf(stderr,
								"Shutting down "
								"interface %s failed: "
								"%s\n",
								master_ifname,
								strerror(errno));
						}
					}

					strncpy(if_hwaddr.ifr_name,
						master_ifname, IFNAMSIZ);
					if (ioctl(skfd, SIOCSIFHWADDR,
							&if_hwaddr) < 0) {
						fprintf(stderr,
							"Could not set MAC "
							"address of %s: %s\n",
							master_ifname,
							strerror(errno));
						goterr=1;
					} else {
						hwaddr_notset = 0;
					}

					if (abi_ver < 1) {
						/* bring master back up */
						if_flags.ifr_flags |= IFF_UP;
						if (ioctl(skfd, SIOCSIFFLAGS,
							  &if_flags) < 0) {
							fprintf(stderr,
								"Bringing up interface "
								"%s failed: %s\n",
								master_ifname,
								strerror(errno));
						}
					}
				}
			} else if (abi_ver < 1) { /* if (hwaddr_notset) */

			      	/* The driver is using an old ABI, so we'll set the interface
				 * down and assign the master's hwaddr to it
				 */
				if (ifr2.ifr_flags & IFF_UP) {
					ifr2.ifr_flags &= ~IFF_UP;
					if (ioctl(skfd, SIOCSIFFLAGS, &ifr2) < 0) {
						int saved_errno = errno;
						fprintf(stderr, "Shutting down interface %s failed: %s\n",
							slave_ifname, strerror(saved_errno));
					}
				}

				strncpy(if_hwaddr.ifr_name, slave_ifname, IFNAMSIZ);
				if (ioctl(skfd, SIOCSIFHWADDR, &if_hwaddr) < 0) {
					int saved_errno = errno;
					fprintf(stderr, "SIOCSIFHWADDR on %s failed: %s\n", if_hwaddr.ifr_name,
						strerror(saved_errno));
					if (saved_errno == EBUSY)
						fprintf(stderr, "  The slave device %s is busy: it must be"
							" idle before running this command.\n", slave_ifname);
					else if (saved_errno == EOPNOTSUPP)
						fprintf(stderr, "  The slave device you specified does not support"
							" setting the MAC address.\n  Your kernel likely does not"
							" support slave devices.\n");
					else if (saved_errno == EINVAL)
						fprintf(stderr, "  The slave device's address type does not match"
							" the master's address type.\n");
				} else {
					if (verbose) {
						unsigned char *hwaddr = if_hwaddr.ifr_hwaddr.sa_data;
						printf("Slave's (%s) hardware address set to "
							   "%2.2x:%2.2x:%2.2x:%2.2x:%2.2x:%2.2x.\n", slave_ifname,
							   hwaddr[0], hwaddr[1], hwaddr[2], hwaddr[3], hwaddr[4], hwaddr[5]);
					}
				}
			}

			if (*spp  &&  !strcmp(*spp, "metric")) {
				if (*++spp == NULL) {
					fprintf(stderr, usage_msg);
					(void) close(skfd);
					exit(2);
				}
				if_metric.ifr_metric = atoi(*spp);
				strncpy(if_metric.ifr_name, slave_ifname, IFNAMSIZ);
				if (ioctl(skfd, SIOCSIFMETRIC, &if_metric) < 0) {
					fprintf(stderr, "SIOCSIFMETRIC on %s: %s\n", slave_ifname,
							strerror(errno));
					goterr = 1;
				}
				spp++;
			}

			if (strncpy(if_ipaddr.ifr_name, slave_ifname, IFNAMSIZ) <= 0
				|| ioctl(skfd, SIOCSIFADDR, &if_ipaddr) < 0) {
				fprintf(stderr,
						"Something broke setting the slave's address: %s.\n",
						strerror(errno));
			} else {
				if (verbose) {
					unsigned char *ipaddr = if_ipaddr.ifr_addr.sa_data;
					printf("Set the slave's (%s) IP address to %d.%d.%d.%d.\n",
						   slave_ifname, ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
				}
			}

			if (strncpy(if_mtu.ifr_name, slave_ifname, IFNAMSIZ) <= 0
				|| ioctl(skfd, SIOCSIFMTU, &if_mtu) < 0) {
				fprintf(stderr, "Something broke setting the slave MTU: %s.\n",
						strerror(errno));
			} else {
				if (verbose)
					printf("Set the slave's (%s) MTU to %d.\n", slave_ifname, if_mtu.ifr_mtu);
			}

			if (strncpy(if_dstaddr.ifr_name, slave_ifname, IFNAMSIZ) <= 0
				|| ioctl(skfd, SIOCSIFDSTADDR, &if_dstaddr) < 0) {
				fprintf(stderr, "Error setting the slave (%s) with SIOCSIFDSTADDR: %s.\n",
						slave_ifname, strerror(errno));
			} else {
				if (verbose) {
					unsigned char *ipaddr = if_dstaddr.ifr_dstaddr.sa_data;
					printf("Set the slave's (%s) destination address to %d.%d.%d.%d.\n",
						   slave_ifname, ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
				}
			}

			if (strncpy(if_brdaddr.ifr_name, slave_ifname, IFNAMSIZ) <= 0
				|| ioctl(skfd, SIOCSIFBRDADDR, &if_brdaddr) < 0) {
				fprintf(stderr,
						"Something broke setting the slave (%s) broadcast address: %s.\n",
						slave_ifname, strerror(errno));
			} else {
				if (verbose) {
					unsigned char *ipaddr = if_brdaddr.ifr_broadaddr.sa_data;
					printf("Set the slave's (%s) broadcast address to %d.%d.%d.%d.\n",
						   slave_ifname, ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
				}
			}

			if (strncpy(if_netmask.ifr_name, slave_ifname, IFNAMSIZ) <= 0
				|| ioctl(skfd, SIOCSIFNETMASK, &if_netmask) < 0) {
				fprintf(stderr,
						"Something broke setting the slave (%s) netmask: %s.\n",
						slave_ifname, strerror(errno));
			} else {
				if (verbose) {
					unsigned char *ipaddr = if_netmask.ifr_netmask.sa_data;
					printf("Set the slave's (%s) netmask to %d.%d.%d.%d.\n",
						   slave_ifname, ipaddr[0], ipaddr[1], ipaddr[2], ipaddr[3]);
				}
			}

			if (abi_ver < 1) {

			      	/* The driver is using an old ABI, so we'll set the interface
				 * up before enslaving it
				 */
				ifr2.ifr_flags |= IFF_UP;
				if ((ifr2.ifr_flags &= ~(IFF_SLAVE | IFF_MASTER)) == 0
					|| strncpy(ifr2.ifr_name, slave_ifname, IFNAMSIZ) <= 0
					|| ioctl(skfd, SIOCSIFFLAGS, &ifr2) < 0) {
						fprintf(stderr,
							"Something broke setting the slave (%s) flags: %s.\n",
							slave_ifname, strerror(errno));
				} else {
					if (verbose)
						printf("Set the slave's (%s) flags %4.4x.\n",
							slave_ifname, if_flags.ifr_flags);
				}
			} else {
				/* the bonding module takes care of setting the slave's mac address
			 	 * and opening its interface
			 	 */
				if (ifr2.ifr_flags & IFF_UP) { /* the interface will need to be down */
					ifr2.ifr_flags &= ~IFF_UP;
					if (ioctl(skfd, SIOCSIFFLAGS, &ifr2) < 0) {
						int saved_errno = errno;
						fprintf(stderr, "Shutting down interface %s failed: %s\n",
							slave_ifname, strerror(saved_errno));
					}
				}
			}

			/* Do the real thing */
			if (!opt_r) {
				strncpy(if_flags.ifr_name, master_ifname, IFNAMSIZ);
				strncpy(if_flags.ifr_slave, slave_ifname, IFNAMSIZ);
				if ((ioctl(skfd, SIOCBONDENSLAVE, &if_flags) < 0) &&
				    (ioctl(skfd, BOND_ENSLAVE_OLD, &if_flags) < 0)) {
					fprintf(stderr,	"SIOCBONDENSLAVE: %s.\n", strerror(errno));
				}
			}
		}
	} while ( (slave_ifname = *spp++) != NULL);

	/* Close the socket. */
	(void) close(skfd);

	return(goterr);
}

Generated by GNU enscript 1.6.4.