11bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma/* brctl.c - ethernet bridge control
21bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma *
31bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma * Copyright 2013 Ashwini Kumar <ak.ashwini1981@gmail.com>
41bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma * Copyright 2013 Kyungwan Han <asura321@gmail.com>
51bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma *
61bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma * No Standard
71bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
81bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini SharmaUSE_BRCTL(NEWTOY(brctl, "<1", TOYFLAG_USR|TOYFLAG_SBIN))
91bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
101bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmaconfig BRCTL
111bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  bool "brctl"
121bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  default n
131bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  help
143297e87c2ab798696b0fb3d06904dccea076b77aRob Landley    usage: brctl COMMAND [BRIDGE [INTERFACE]]
151bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
161bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    Manage ethernet bridges
171bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
181bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    Commands:
191bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    show                  Show a list of bridges
201bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    addbr BRIDGE          Create BRIDGE
211bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    delbr BRIDGE          Delete BRIDGE
221bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    addif BRIDGE IFACE    Add IFACE to BRIDGE
231bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    delif BRIDGE IFACE    Delete IFACE from BRIDGE
241bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    setageing BRIDGE TIME Set ageing time
251bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    setfd BRIDGE TIME     Set bridge forward delay
261bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    sethello BRIDGE TIME  Set hello time
271bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    setmaxage BRIDGE TIME Set max message age
281bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    setpathcost BRIDGE PORT COST   Set path cost
291bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    setportprio BRIDGE PORT PRIO   Set port priority
301bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    setbridgeprio BRIDGE PRIO      Set bridge priority
311bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    stp BRIDGE [1/yes/on|0/no/off] STP on/off
321bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma*/
331bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
341bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#define FOR_brctl
351bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#include "toys.h"
361bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#include <linux/if_bridge.h>
371bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
381bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini SharmaGLOBALS(
391bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    int sockfd;
401bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma)
411bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#define MAX_BRIDGES 1024 //same is no of ports supported
421bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
431bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmastatic void get_ports(char *bridge, int *indices)
441bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
451bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct ifreq ifr;
461bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int ifindices[MAX_BRIDGES];
471bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = { BRCTL_GET_PORT_LIST,
481bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    (unsigned long) ifindices, MAX_BRIDGES, 0 };
491bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
501bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  memset(ifindices, 0, MAX_BRIDGES);
511bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  args[1] = (unsigned long)ifindices;
5282effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(ifr.ifr_name, bridge, IFNAMSIZ);
531bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_data = (char *)args;
541bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr);
551bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (indices) memcpy(indices, ifindices, sizeof(ifindices));
561bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
571bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
581bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid get_br_info(char *bridge, struct __bridge_info *info)
591bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
601bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct ifreq ifr;
611bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = { BRCTL_GET_BRIDGE_INFO,
621bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    (unsigned long) info, 0, 0 };
631bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
641bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  memset(info, 0, sizeof(*info));
6582effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(ifr.ifr_name, bridge, IFNAMSIZ);
661bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_data = (char *)args;
671bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
681bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (ioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr) < 0) {
691bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    perror_msg("%s: can't get info %s\n", bridge, strerror(errno));
701bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    return;
711bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  }
721bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
731bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
741bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_show(char **argv)
751bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
761bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct __bridge_info info;
771bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int num, cnt, i, j, ifindices[MAX_BRIDGES], pindices[MAX_BRIDGES];
781bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = { BRCTL_GET_BRIDGES,
791bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    (unsigned long)ifindices, MAX_BRIDGES,0 };
801bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  char br[IF_NAMESIZE], ifn[IF_NAMESIZE];
811bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
821bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  num = ioctl(TT.sockfd, SIOCGIFBR, args); //ret is num of bridges found
831bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (num < 0) error_exit("get bridges fail");
841bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  printf("bridge name\tbridge id\t\tSTP enabled\tinterfaces\n");
851bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
861bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  for (i = 0; i < num; i++) {
871bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    unsigned char *id;
881bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
891bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    if (!if_indextoname(ifindices[i], br)) perror_exit("interface not found");
901bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    get_br_info(br, &info);
911bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    id = (unsigned char*)&(info.bridge_id);
921bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    printf("%s\t\t",br);
931bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    printf("%.2x%.2x.%.2x%.2x%.2x%.2x%.2x%.2x", id[0], id[1],
941bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma        id[2], id[3], id[4], id[5], id[6], id[7]);
951bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    printf("\t%s\t\t",(info.stp_enabled)?"yes" : "no");
961bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
971bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    memset(pindices, 0, sizeof(pindices));
981bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    get_ports(br, pindices);
991bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    for (j = 0, cnt = 0; j < MAX_BRIDGES; j++) {
1001bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      if (!pindices[j]) continue;
1011bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      if (!if_indextoname(pindices[j], ifn)) {
1021bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma        error_msg("no name for index :%d", pindices[j]);
1031bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma        continue;
1041bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      }
1051bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      if (cnt) printf("\n\t\t\t\t\t\t\t");
1061bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      printf("%s", ifn);
1071bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      cnt++;
1081bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    }
1091bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    xputc('\n');
1101bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  }
1111bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
1121bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1131bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_addbr(char **argv)
1141bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
1151bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  char br[IFNAMSIZ];
1161bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = {BRCTL_ADD_BRIDGE, (unsigned long) br, 0, 0};
1171bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1181bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#ifdef SIOCBRADDBR
1191bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCBRADDBR, argv[0]);
1201bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#else
12182effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(br, argv[0], IFNAMSIZ);
1221bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCSIFBR, args);
1231bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#endif
1241bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
1251bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1261bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_delbr(char **argv)
1271bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
1281bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  char br[IFNAMSIZ];
1291bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = {BRCTL_DEL_BRIDGE, (unsigned long) br, 0, 0};
1301bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1311bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#ifdef SIOCBRDELBR
1321bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCBRDELBR, argv[0]);
1331bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#else
13482effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(br, argv[0], IFNAMSIZ);
1351bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCSIFBR, args);
1361bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#endif
1371bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
1381bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1391bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_addif(char **argv)
1401bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
1411bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int index;
1421bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct ifreq ifr;
1431bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = {BRCTL_ADD_IF, 0, 0, 0};
1441bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1451bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (!(index = if_nametoindex(argv[1]))) perror_exit("interface %s", argv[1]);
1461bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#ifdef SIOCBRADDIF
1471bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_ifindex = index;
1481bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCBRADDIF, &ifr);
1491bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#else
1501bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  args[1] = index;
15182effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(ifr.ifr_name, argv[0], IFNAMSIZ);
1521bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_data = (char *)args;
1531bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr);
1541bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#endif
1551bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
1561bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1571bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_delif(char **argv)
1581bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
1591bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int index;
1601bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct ifreq ifr;
1611bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = {BRCTL_DEL_IF, 0, 0, 0};
1621bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1631bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (!(index = if_nametoindex(argv[1]))) perror_exit("interface %s",argv[1]);
1641bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#ifdef SIOCBRDELIF
1651bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_ifindex = ifindex;
1661bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCBRDELIF, &ifr);
1671bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#else
1681bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  args[1] = index;
16982effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(ifr.ifr_name, argv[0], IFNAMSIZ);
1701bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_data = (char *)args;
1711bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr);
1721bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma#endif
1731bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
1741bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1751bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmastatic void strtotimeval(struct timeval *tv, char *time)
1761bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
1771bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  double secs;
1781bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1791bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (sscanf(time, "%lf", &secs) != 1) error_exit("time format not proper");
1801bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  tv->tv_sec = secs;
1811bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  tv->tv_usec = 1000000 * (secs - tv->tv_sec);
1821bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
1831bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1841bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmastatic unsigned long tv_to_jify(struct timeval *tv)
1851bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
1861bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long long jify;
1871bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1881bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  jify = 1000000ULL * tv->tv_sec + tv->tv_usec;
1891bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  return (jify/10000);
1901bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
1911bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
1921bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid set_time(char *br, unsigned long cmd, unsigned long val)
1931bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
1941bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct ifreq ifr;
1951bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = {cmd, val, 0, 0};
1961bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
19782effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(ifr.ifr_name, br, IFNAMSIZ);
1981bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_data = (char *)args;
1991bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr);
2001bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2011bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2021bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_ageing_time(char **argv)
2031bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2041bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct timeval tv;
2051bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2061bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  strtotimeval(&tv, argv[1]);
2071bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_time(argv[0], BRCTL_SET_AGEING_TIME, tv_to_jify(&tv));
2081bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2091bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2101bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_fwd_delay(char **argv)
2111bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2121bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct timeval tv;
2131bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2141bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  strtotimeval(&tv, argv[1]);
2151bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_time(argv[0], BRCTL_SET_BRIDGE_FORWARD_DELAY, tv_to_jify(&tv));
2161bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2171bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2181bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_hello_time(char **argv)
2191bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2201bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct timeval tv;
2211bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2221bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  strtotimeval(&tv, argv[1]);
2231bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_time(argv[0], BRCTL_SET_BRIDGE_HELLO_TIME, tv_to_jify(&tv));
2241bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2251bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2261bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_max_age(char **argv)
2271bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2281bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct timeval tv;
2291bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2301bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  strtotimeval(&tv, argv[1]);
2311bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_time(argv[0], BRCTL_SET_BRIDGE_MAX_AGE, tv_to_jify(&tv));
2321bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2331bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2341bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_bridge_prio(char **argv)
2351bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2361bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int prio;
2371bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2381bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (sscanf(argv[1], "%i", &prio) != 1) error_exit("prio not proper");
2391bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_time(argv[0], BRCTL_SET_BRIDGE_PRIORITY, prio);
2401bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2411bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2421bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_stp(char **argv)
2431bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2441bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int i;
2451bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct stp {
2461bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    char *n;
2471bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    int set;
2481bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  } ss[] = {{"1", 1}, {"yes", 1},{"on", 1},
2491bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"0", 0}, {"no", 0},{"off", 0}};
2501bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2511bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  for (i = 0; i < ARRAY_LEN(ss); i++) {
2521bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    if (!strcmp(ss[i].n, argv[1])) break;
2531bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  }
2541bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (i >= ARRAY_LEN(ss)) error_exit("invalid stp state");
2551bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_time(argv[0], BRCTL_SET_BRIDGE_STP_STATE, ss[i].set);
2561bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2571bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2581bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid set_cost_prio(char *br, char *port, unsigned long cmd, unsigned long val)
2591bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2601bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct ifreq ifr;
2611bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int i, index, pindices[MAX_BRIDGES];
2621bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  unsigned long args[4] = {cmd, 0, val, 0};
2631bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2641bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (!(index = if_nametoindex(port))) error_exit("invalid port");
2651bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2661bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  memset(pindices, 0, sizeof(pindices));
2671bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  get_ports(br, pindices);
2681bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  for (i = 0; i < MAX_BRIDGES; i++) {
2691bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    if (index == pindices[i]) break;
2701bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  }
2711bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  if (i >= MAX_BRIDGES) error_exit("%s not in bridge", port);
2721bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  args[1] = i;
27382effc97f9f2d1c258ea50cb11b130753b8ba805Rob Landley  xstrncpy(ifr.ifr_name, br, IFNAMSIZ);
2741bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  ifr.ifr_data = (char *)args;
2751bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xioctl(TT.sockfd, SIOCDEVPRIVATE, &ifr);
2761bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2771bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2781bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_path_cost(char **argv)
2791bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2801bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int cost;
2811bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2821bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  cost = atolx_range(argv[2], 0, INT_MAX);
2831bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_cost_prio(argv[0], argv[1], BRCTL_SET_PATH_COST, cost);
2841bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2851bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2861bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid br_set_port_prio(char **argv)
2871bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2881bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int prio;
2891bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2901bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  prio = atolx_range(argv[2], 0, INT_MAX);
2911bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  set_cost_prio(argv[0], argv[1], BRCTL_SET_PORT_PRIORITY, prio);
2921bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2931bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
2941bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
2951bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharmavoid brctl_main(void)
2961bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma{
2971bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  int i;
2981bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  struct cmds {
2991bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    char *cmd;
3001bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    int nargs;
3011bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    void (*f)(char **argv);
3021bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  } cc[] = {{"show", 0, br_show},
3031bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"addbr", 1, br_addbr}, {"delbr", 1, br_delbr},
3041bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"addif", 2, br_addif}, {"delif", 2, br_delif},
3051bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"setageing", 2, br_set_ageing_time},
3061bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"setfd", 2, br_set_fwd_delay},
3071bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"sethello", 2, br_set_hello_time},
3081bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"setmaxage", 2, br_set_max_age},
3091bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"setpathcost", 3, br_set_path_cost},
3101bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"setportprio", 3, br_set_port_prio},
3111bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"setbridgeprio", 2, br_set_bridge_prio},
3121bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    {"stp", 2, br_set_stp},
3131bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  };
3141bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
3151bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  TT.sockfd = xsocket(AF_INET, SOCK_STREAM, 0);
3161bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  while (*toys.optargs) {
3171bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    for (i = 0; i < ARRAY_LEN(cc); i++) {
3181bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      struct cmds *t = cc + i;
3191bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
3201bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      if (strcmp(t->cmd, *toys.optargs)) continue;
3211bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
3221bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      toys.optargs++, toys.optc--;
323e5354ca12a232b3f97726214254a868771cb70d1Rob Landley      if (toys.optc < t->nargs) help_exit("check args");
3241bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      t->f(toys.optargs);
3251bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      toys.optargs += t->nargs;
3261bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      toys.optc -= t->nargs;
3271bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma      break;
3281bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma    }
3291bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma
330e5354ca12a232b3f97726214254a868771cb70d1Rob Landley    if (i == ARRAY_LEN(cc)) help_exit("invalid option '%s'", *toys.optargs);
3311bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  }
3321bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma  xclose(TT.sockfd);
3331bc21f0f98417bbad16db22ddb35eedae63765e6Ashwini Sharma}
334