Home | History | Annotate | Line # | Download | only in srtconfig
srtconfig.c revision 1.1
      1  1.1  mouse /* This file is in the public domain. */
      2  1.1  mouse 
      3  1.1  mouse #include <stdio.h>
      4  1.1  mouse #include <errno.h>
      5  1.1  mouse #include <fcntl.h>
      6  1.1  mouse #include <stdlib.h>
      7  1.1  mouse #include <strings.h>
      8  1.1  mouse #include <sys/ioctl.h>
      9  1.1  mouse #include <arpa/inet.h>
     10  1.1  mouse #include <net/if_srt.h>
     11  1.1  mouse 
     12  1.1  mouse extern const char *__progname;
     13  1.1  mouse 
     14  1.1  mouse #define ACT_ERROR     1 /* none of the below */
     15  1.1  mouse #define ACT_QUERYALL  2 /* srtX */
     16  1.1  mouse #define ACT_QUERYONE  3 /* srtX N */
     17  1.1  mouse #define ACT_DEL       4 /* srtX del N */
     18  1.1  mouse #define ACT_ADD       5 /* srtX add srcaddr mask dstif dstaddr */
     19  1.1  mouse #define ACT_SET       6 /* srtX set N srcaddr mask dstif dstaddr */
     20  1.1  mouse #define ACT_FLAGS     7 /* srtX flags */
     21  1.1  mouse #define ACT_SFLAG     8 /* srtX flags [+|-]flag */
     22  1.1  mouse #define ACT_DEBUG     9 /* srtX debug */
     23  1.1  mouse static int action = ACT_ERROR;
     24  1.1  mouse 
     25  1.1  mouse static char *txt_dev;
     26  1.1  mouse static char *txt_n;
     27  1.1  mouse static char *txt_flg;
     28  1.1  mouse static char *txt_addr;
     29  1.1  mouse static char *txt_mask;
     30  1.1  mouse static char *txt_dstif;
     31  1.1  mouse static char *txt_dstaddr;
     32  1.1  mouse 
     33  1.1  mouse static int devfd;
     34  1.1  mouse 
     35  1.1  mouse static struct {
     36  1.1  mouse 	 const char *name;
     37  1.1  mouse 	 unsigned int bit;
     38  1.1  mouse 	 } flagbits[] = { { "mtulock", SSF_MTULOCK },
     39  1.1  mouse 			  { 0 } };
     40  1.1  mouse 
     41  1.1  mouse static void handleargs(int ac, char **av)
     42  1.1  mouse {
     43  1.1  mouse  txt_dev = av[1];
     44  1.1  mouse  if (ac == 2)
     45  1.1  mouse   { action = ACT_QUERYALL;
     46  1.1  mouse   }
     47  1.1  mouse  else if ((ac == 3) && !strcmp(av[2],"debug"))
     48  1.1  mouse   { action = ACT_DEBUG;
     49  1.1  mouse   }
     50  1.1  mouse  else if ((ac == 3) && !strcmp(av[2],"flags"))
     51  1.1  mouse   { action = ACT_FLAGS;
     52  1.1  mouse   }
     53  1.1  mouse  else if (ac == 3)
     54  1.1  mouse   { action = ACT_QUERYONE;
     55  1.1  mouse     txt_n = av[2];
     56  1.1  mouse   }
     57  1.1  mouse  else if ((ac == 4) && !strcmp(av[2],"del"))
     58  1.1  mouse   { action = ACT_DEL;
     59  1.1  mouse     txt_n = av[3];
     60  1.1  mouse   }
     61  1.1  mouse  else if ((ac == 4) && !strcmp(av[2],"flags"))
     62  1.1  mouse   { action = ACT_SFLAG;
     63  1.1  mouse     txt_flg = av[3];
     64  1.1  mouse   }
     65  1.1  mouse  else if ((ac == 7) && !strcmp(av[2],"add"))
     66  1.1  mouse   { action = ACT_ADD;
     67  1.1  mouse     txt_addr = av[3];
     68  1.1  mouse     txt_mask = av[4];
     69  1.1  mouse     txt_dstif = av[5];
     70  1.1  mouse     txt_dstaddr = av[6];
     71  1.1  mouse   }
     72  1.1  mouse  else if ((ac == 8) && !strcmp(av[2],"set"))
     73  1.1  mouse   { action = ACT_SET;
     74  1.1  mouse     txt_n = av[3];
     75  1.1  mouse     txt_addr = av[4];
     76  1.1  mouse     txt_mask = av[5];
     77  1.1  mouse     txt_dstif = av[6];
     78  1.1  mouse     txt_dstaddr = av[7];
     79  1.1  mouse   }
     80  1.1  mouse  if (action == ACT_ERROR)
     81  1.1  mouse   { fprintf(stderr,"Usage: %s srtX\n",__progname);
     82  1.1  mouse     fprintf(stderr,"       %s srtX N\n",__progname);
     83  1.1  mouse     fprintf(stderr,"       %s srtX del N\n",__progname);
     84  1.1  mouse     fprintf(stderr,"       %s srtX add addr mask dstif dstaddr\n",__progname);
     85  1.1  mouse     fprintf(stderr,"       %s srtX set N addr mask dstif dstaddr\n",__progname);
     86  1.1  mouse     fprintf(stderr,"       %s srtX flags {[+|-]flag}\n",__progname);
     87  1.1  mouse     fprintf(stderr,"       %s srtX debug\n",__progname);
     88  1.1  mouse     exit(1);
     89  1.1  mouse   }
     90  1.1  mouse }
     91  1.1  mouse 
     92  1.1  mouse static void open_dev(int how)
     93  1.1  mouse {
     94  1.1  mouse  if (! index(txt_dev,'/'))
     95  1.1  mouse   { char *tmp;
     96  1.1  mouse     asprintf(&tmp,"/dev/%s",txt_dev);
     97  1.1  mouse     txt_dev = tmp;
     98  1.1  mouse   }
     99  1.1  mouse  devfd = open(txt_dev,how,0);
    100  1.1  mouse  if (devfd < 0)
    101  1.1  mouse   { fprintf(stderr,"%s; can't open %s: %s\n",__progname,txt_dev,strerror(errno));
    102  1.1  mouse     exit(1);
    103  1.1  mouse   }
    104  1.1  mouse }
    105  1.1  mouse 
    106  1.1  mouse static void query_n(int n)
    107  1.1  mouse {
    108  1.1  mouse  struct srt_rt r;
    109  1.1  mouse  char obuf[64];
    110  1.1  mouse 
    111  1.1  mouse  r.inx = n;
    112  1.1  mouse  if (ioctl(devfd,SRT_GETRT,&r) < 0)
    113  1.1  mouse   { fprintf(stderr,"%s: can't get rt #%d: %s\n",__progname,n,strerror(errno));
    114  1.1  mouse     return;
    115  1.1  mouse   }
    116  1.1  mouse  printf("%d:",n);
    117  1.1  mouse  printf(" %s",inet_ntop(r.af,&r.srcmatch,&obuf[0],sizeof(obuf)));
    118  1.1  mouse  printf(" /%d",r.srcmask);
    119  1.1  mouse  printf(" %.*s",(int)sizeof(r.u.dstifn),&r.u.dstifn[0]);
    120  1.1  mouse  switch (r.af)
    121  1.1  mouse   { case AF_INET:
    122  1.1  mouse        printf(" %s",inet_ntoa(r.dst.sin.sin_addr));
    123  1.1  mouse        break;
    124  1.1  mouse     case AF_INET6:
    125  1.1  mouse        printf(" %s",inet_ntop(AF_INET6,&r.dst.sin6.sin6_addr,&obuf[0],sizeof(obuf)));
    126  1.1  mouse        break;
    127  1.1  mouse     default:
    128  1.1  mouse        printf(" ?af%d",r.af);
    129  1.1  mouse        break;
    130  1.1  mouse   }
    131  1.1  mouse  printf("\n");
    132  1.1  mouse }
    133  1.1  mouse 
    134  1.1  mouse static void do_query(int narg)
    135  1.1  mouse {
    136  1.1  mouse  int i;
    137  1.1  mouse  int n;
    138  1.1  mouse 
    139  1.1  mouse  open_dev(O_RDONLY);
    140  1.1  mouse  if (narg >= 0)
    141  1.1  mouse   { query_n(narg);
    142  1.1  mouse   }
    143  1.1  mouse  else
    144  1.1  mouse   { if (ioctl(devfd,SRT_GETNRT,&n) < 0)
    145  1.1  mouse      { fprintf(stderr,"%s: can't get count: %s\n",__progname,strerror(errno));
    146  1.1  mouse        exit(1);
    147  1.1  mouse      }
    148  1.1  mouse     for (i=0;i<n;i++) query_n(i);
    149  1.1  mouse   }
    150  1.1  mouse }
    151  1.1  mouse 
    152  1.1  mouse static void do_del(unsigned int n)
    153  1.1  mouse {
    154  1.1  mouse  open_dev(O_RDWR);
    155  1.1  mouse  if (ioctl(devfd,SRT_DELRT,&n) < 0)
    156  1.1  mouse   { fprintf(stderr,"%s: can't delete #%u: %s\n",__progname,n,strerror(errno));
    157  1.1  mouse     exit(1);
    158  1.1  mouse   }
    159  1.1  mouse }
    160  1.1  mouse 
    161  1.1  mouse static void do_set(int n)
    162  1.1  mouse {
    163  1.1  mouse  struct srt_rt r;
    164  1.1  mouse  int w;
    165  1.1  mouse  int maxw;
    166  1.1  mouse  void *dp;
    167  1.1  mouse 
    168  1.1  mouse  open_dev(O_RDWR);
    169  1.1  mouse  if (n < 0)
    170  1.1  mouse   { unsigned int v;
    171  1.1  mouse     if (ioctl(devfd,SRT_GETNRT,&v) < 0)
    172  1.1  mouse      { fprintf(stderr,"%s: can't get count: %s\n",__progname,strerror(errno));
    173  1.1  mouse        exit(1);
    174  1.1  mouse      }
    175  1.1  mouse     n = v;
    176  1.1  mouse   }
    177  1.1  mouse  bzero(&r.dst,sizeof(r.dst));
    178  1.1  mouse  r.inx = n;
    179  1.1  mouse  if (inet_pton(AF_INET,txt_addr,&r.srcmatch.v4) == 1)
    180  1.1  mouse   { r.af = AF_INET;
    181  1.1  mouse     r.dst.sin.sin_family = AF_INET;
    182  1.1  mouse     r.dst.sin.sin_len = sizeof(r.dst.sin);
    183  1.1  mouse     dp = &r.dst.sin.sin_addr;
    184  1.1  mouse     maxw = 32;
    185  1.1  mouse   }
    186  1.1  mouse  else if (inet_pton(AF_INET6,txt_addr,&r.srcmatch.v6) == 1)
    187  1.1  mouse   { r.af = AF_INET6;
    188  1.1  mouse     r.dst.sin6.sin6_family = AF_INET6;
    189  1.1  mouse     r.dst.sin6.sin6_len = sizeof(r.dst.sin6);
    190  1.1  mouse     dp = &r.dst.sin6.sin6_addr;
    191  1.1  mouse     maxw = 128;
    192  1.1  mouse   }
    193  1.1  mouse  else
    194  1.1  mouse   { fprintf(stderr,"%s: %s: invalid match address\n",__progname,txt_addr);
    195  1.1  mouse     exit(1);
    196  1.1  mouse   }
    197  1.1  mouse  if (txt_mask[0] == '/') txt_mask ++;
    198  1.1  mouse  w = atoi(txt_mask);
    199  1.1  mouse  if ((w < 0) || (w > maxw))
    200  1.1  mouse   { fprintf(stderr,"%s: %s: out-of-range CIDR width\n",__progname,txt_mask);
    201  1.1  mouse     exit(1);
    202  1.1  mouse   }
    203  1.1  mouse  r.srcmask = w;
    204  1.1  mouse  if (strlen(txt_dstif) > sizeof(r.u.dstifn)-1)
    205  1.1  mouse   { fprintf(stderr,"%s: %s: too long\n",__progname,txt_dstif);
    206  1.1  mouse     exit(1);
    207  1.1  mouse   }
    208  1.1  mouse  strncpy(&r.u.dstifn[0],txt_dstif,sizeof(r.u.dstifn));
    209  1.1  mouse  if (inet_pton(r.af,txt_dstaddr,dp) != 1)
    210  1.1  mouse   { fprintf(stderr,"%s: %s: invalid destination address\n",__progname,txt_dstaddr);
    211  1.1  mouse     exit(1);
    212  1.1  mouse   }
    213  1.1  mouse  if (ioctl(devfd,SRT_SETRT,&r) < 0)
    214  1.1  mouse   { fprintf(stderr,"%s: can't set route: %s\n",__progname,strerror(errno));
    215  1.1  mouse     exit(1);
    216  1.1  mouse   }
    217  1.1  mouse }
    218  1.1  mouse 
    219  1.1  mouse static void do_flags(void)
    220  1.1  mouse {
    221  1.1  mouse  unsigned int f;
    222  1.1  mouse  int i;
    223  1.1  mouse 
    224  1.1  mouse  open_dev(O_RDONLY);
    225  1.1  mouse  if (ioctl(devfd,SRT_GFLAGS,&f) < 0)
    226  1.1  mouse   { fprintf(stderr,"%s: can't get flags: %s\n",__progname,strerror(errno));
    227  1.1  mouse     exit(1);
    228  1.1  mouse   }
    229  1.1  mouse  for (i=0;flagbits[i].name;i++)
    230  1.1  mouse   { printf(" %c%s",(f&flagbits[i].bit)?'+':'-',flagbits[i].name);
    231  1.1  mouse     f &= ~flagbits[i].bit;
    232  1.1  mouse   }
    233  1.1  mouse  if (f) printf(" +0x%x",f);
    234  1.1  mouse  printf("\n");
    235  1.1  mouse }
    236  1.1  mouse 
    237  1.1  mouse static void do_sflag(void)
    238  1.1  mouse {
    239  1.1  mouse  unsigned int f;
    240  1.1  mouse  unsigned int b;
    241  1.1  mouse  int i;
    242  1.1  mouse 
    243  1.1  mouse  switch (txt_flg[0])
    244  1.1  mouse   { case '+': case '-': break;
    245  1.1  mouse     default:
    246  1.1  mouse        fprintf(stderr,"%s: last argument must be +flag or -flag\n",__progname);
    247  1.1  mouse        exit(1);
    248  1.1  mouse        break;
    249  1.1  mouse   }
    250  1.1  mouse  for (i=0;flagbits[i].name;i++) if (!strcmp(flagbits[i].name,txt_flg+1)) break;
    251  1.1  mouse  if (! flagbits[i].name)
    252  1.1  mouse   { fprintf(stderr,"%s: unrecognized flag bit `%s'\n",__progname,txt_flg+1);
    253  1.1  mouse     exit(1);
    254  1.1  mouse   }
    255  1.1  mouse  b = flagbits[i].bit;
    256  1.1  mouse  open_dev(O_RDWR);
    257  1.1  mouse  if (ioctl(devfd,SRT_GFLAGS,&f) < 0)
    258  1.1  mouse   { fprintf(stderr,"%s: can't get flags: %s\n",__progname,strerror(errno));
    259  1.1  mouse     exit(1);
    260  1.1  mouse   }
    261  1.1  mouse  if (txt_flg[0] == '+') f |= b; else f &= ~b;
    262  1.1  mouse  if (ioctl(devfd,SRT_SFLAGS,&f) < 0)
    263  1.1  mouse   { fprintf(stderr,"%s: can't set flags: %s\n",__progname,strerror(errno));
    264  1.1  mouse     exit(1);
    265  1.1  mouse   }
    266  1.1  mouse }
    267  1.1  mouse 
    268  1.1  mouse static void do_debug(void)
    269  1.1  mouse {
    270  1.1  mouse  void *vp;
    271  1.1  mouse 
    272  1.1  mouse  open_dev(O_RDWR);
    273  1.1  mouse  vp = 0;
    274  1.1  mouse  if (ioctl(devfd,SRT_DEBUG,&vp) < 0)
    275  1.1  mouse   { fprintf(stderr,"%s: can't SRT_DEBUG: %s\n",__progname,strerror(errno));
    276  1.1  mouse     exit(1);
    277  1.1  mouse   }
    278  1.1  mouse }
    279  1.1  mouse 
    280  1.1  mouse int main(int, char **);
    281  1.1  mouse int main(int ac, char **av)
    282  1.1  mouse {
    283  1.1  mouse  handleargs(ac,av);
    284  1.1  mouse  switch (action)
    285  1.1  mouse   { case ACT_QUERYALL:
    286  1.1  mouse        do_query(-1);
    287  1.1  mouse        break;
    288  1.1  mouse     case ACT_QUERYONE:
    289  1.1  mouse        do_query(atoi(txt_n));
    290  1.1  mouse        break;
    291  1.1  mouse     case ACT_DEL:
    292  1.1  mouse        do_del(atoi(txt_n));
    293  1.1  mouse        break;
    294  1.1  mouse     case ACT_ADD:
    295  1.1  mouse        do_set(-1);
    296  1.1  mouse        break;
    297  1.1  mouse     case ACT_SET:
    298  1.1  mouse        do_set(atoi(txt_n));
    299  1.1  mouse        break;
    300  1.1  mouse     case ACT_FLAGS:
    301  1.1  mouse        do_flags();
    302  1.1  mouse        break;
    303  1.1  mouse     case ACT_SFLAG:
    304  1.1  mouse        do_sflag();
    305  1.1  mouse        break;
    306  1.1  mouse     case ACT_DEBUG:
    307  1.1  mouse        do_debug();
    308  1.1  mouse        break;
    309  1.1  mouse     default:
    310  1.1  mouse        abort();
    311  1.1  mouse        break;
    312  1.1  mouse   }
    313  1.1  mouse  exit(0);
    314  1.1  mouse }
    315