📄 macvlan_config.c
字号:
req.cmd = MACVLAN_GET_NUM_PORTS; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (SIOCGIFMACVLAN, GET_NUM_PORTS)"); return -1; } printf("Found: %i ports\n", rep.num); return rep.num;}int get_num_vlans(int portidx, int s) { struct macvlan_ioctl req; struct macvlan_ioctl_reply rep; /* Get the number of mac-based-vlans layered * over this ethernet device */ req.cmd = MACVLAN_GET_NUM_VLANS; req.portidx = portidx; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_NUM_VLANS)"); return -1; } printf("Found: %i vlans for port: %i\n", rep.num, portidx); return rep.num;}int htoi(char *s){ char ch; int i = 0; while ((ch = *s++)) { i <<= 4; i += (ch>='0'&&ch<='9')?(ch-'0'):((ch>='a'&&ch<='f')?(ch-'a'+10):((ch>='A'&&ch<='F')?(ch-'A'+10):0)); } return i;}int do_bind(int argc, char *argv[]){ int s; struct macvlan_ioctl req; char *ptr; unsigned char macaddr[6]; if (argc < 3) { printf("usage: %s <ifname> <macaddr>\n", argv[0]); return 1; } if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; } req.cmd = MACVLAN_BIND; req.ifname = argv[1]; /* name of vlan to which we are binding a MAC address */ /* assemble the macaddr */ ptr = argv[2]; if (strlen(ptr) != 17) { printf("bad macaddr format: need aa:bb:cc:dd:ee:ff\n"); return 1; } for (ptr = argv[2]+2; ptr < argv[2]+16; ptr+=3) *ptr = 0; ptr = argv[2]; macaddr[0] = (unsigned char)htoi(ptr); macaddr[1] = (unsigned char)htoi(ptr+3); macaddr[2] = (unsigned char)htoi(ptr+6); macaddr[3] = (unsigned char)htoi(ptr+9); macaddr[4] = (unsigned char)htoi(ptr+12); macaddr[5] = (unsigned char)htoi(ptr+15); req.macaddr = macaddr; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (MACVLAN_BIND)"); return 1; } return 0;}int do_unbind(int argc, char *argv[]){ int s; struct macvlan_ioctl req; char *ptr; unsigned char macaddr[6]; if (argc < 3) { printf("usage: %s <ifname> <macaddr>\n", argv[0]); return 1; } if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; } req.cmd = MACVLAN_UNBIND; req.ifname = argv[1]; /* name of vlan from which we are deleting a MAC address */ /* assemble the macaddr */ ptr = argv[2]; if (strlen(ptr) != 17) { printf("bad macaddr format: need aa:bb:cc:dd:ee:ff\n"); return 1; } for (ptr = argv[2]+2; ptr < argv[2]+16; ptr+=3) *ptr = 0; ptr = argv[2]; macaddr[0] = (unsigned char)htoi(ptr); macaddr[1] = (unsigned char)htoi(ptr+3); macaddr[2] = (unsigned char)htoi(ptr+6); macaddr[3] = (unsigned char)htoi(ptr+9); macaddr[4] = (unsigned char)htoi(ptr+12); macaddr[5] = (unsigned char)htoi(ptr+15); req.macaddr = macaddr; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (MACVLAN_UNBIND)"); return 1; } return 0;}int do_info(int argc, char *argv[]){ int s; struct macvlan_ioctl req; struct macvlan_ioctl_reply rep; int nports; int portidx; int nifs; int ifidx; int nmacs; int macidx; unsigned char *p; if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; } /* get the number of ethernet devices which have mac based vlans * enabled over them */ req.cmd = MACVLAN_GET_NUM_PORTS; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_NUM_PORTS)"); return 1; } nports = rep.num; for (portidx = 0; portidx < nports; portidx++) { char tmpifname[64]; /* Get the name of this mac-based-vlan enabled * ethernet device */ req.cmd = MACVLAN_GET_PORT_NAME; req.portidx = portidx; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_PORT_NAME)"); return 1; } printf("-%s\n", rep.name); /* get the port flags */ req.cmd = MACVLAN_GET_PORT_FLAGS; req.portidx = portidx; strcpy(tmpifname, rep.name); req.ifname = tmpifname; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_PORT_FLAGS)"); return 1; } printf("-%s flag: 0x%x\n", tmpifname, rep.num); /* Get the number of mac-based-vlans layered * over this ethernet device */ req.cmd = MACVLAN_GET_NUM_VLANS; req.portidx = portidx; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_NUM_VLANS)"); return 1; } nifs = rep.num; for (ifidx = 0; ifidx < nifs; ifidx++) { /* Get the name of this vlan */ req.cmd = MACVLAN_GET_VLAN_NAME; req.portidx = portidx; req.ifidx = ifidx; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_VLAN_NAME)"); return 1; } /* get the number of mac addresses owned by this vlan */ printf(" |-%s\n", rep.name); req.cmd = MACVLAN_GET_NUM_MACS; req.portidx = portidx; req.ifidx = ifidx; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_NUM_MACS)"); return 1; } nmacs = rep.num; for (macidx = 0; macidx < nmacs; macidx++) { /* get the value of this mac address */ req.cmd = MACVLAN_GET_MAC_NAME; req.portidx = portidx; req.ifidx = ifidx; req.macaddridx = macidx; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_MAC_NAME)"); return 1; } p = (unsigned char *) rep.name; printf(" | |-%02x:%02x:%02x:%02x:%02x:%02x\n", p[0],p[1],p[2],p[3],p[4],p[5]); } } } return 0;}int do_unload(int argc, char *argv[]){ int s; struct macvlan_ioctl req; struct macvlan_ioctl_reply rep; if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) { perror("socket"); return 1; } while (get_num_ports(s) > 0) { char port[64]; /* Get the name of this mac-based-vlan enabled * ethernet device */ req.cmd = MACVLAN_GET_PORT_NAME; req.portidx = 0; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_PORT_NAME)"); return 1; } strcpy(port, rep.name); while (get_num_vlans(0, s) > 0) { char cmd[128]; /* Get the name of this vlan */ req.cmd = MACVLAN_GET_VLAN_NAME; req.portidx = 0; req.ifidx = 0; req.reply = &rep; if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) { perror("ioctl (GET_VLAN_NAME)"); return 1; } /* Configure down the vlan */ /* This would be faster using IOCTLs, of course! */ printf("Configuring down interface: %s with ifconfig...", rep.name); sprintf(cmd, "ifconfig %s down", rep.name); system(cmd); /* Now, can remove it */ _do_del(rep.name, s); } /* Now, remove the port */ _do_disable(port, s); } return 0;}int main(int argc, char *argv[]){ unsigned int cmd; int err; if (argc < 2) goto usage; for (cmd = 0; cmd < NCOMMANDS; cmd++) { if (!strcmp(command_list[cmd].name,argv[1])) break; } if (cmd == NCOMMANDS) goto usage; if ((err = command_list[cmd].fn(argc-1,argv+1))) goto usage; return 0; usage: printf("\n%s subcommands:\n\n", argv[0]); for (cmd = 0; cmd < NCOMMANDS; cmd++) { printf("%s %s:\t%s\n",argv[0],command_list[cmd].name,command_list[cmd].short_help); } return err;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -