Codebase list vlan / ubuntu/1.9-3ubuntu4 macvlan_config.c
ubuntu/1.9-3ubuntu4

Tree @ubuntu/1.9-3ubuntu4 (Download .tar.gz)

macvlan_config.c @ubuntu/1.9-3ubuntu4raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
/*
#######################################################################
#
# (C) Copyright 2001
# Alex Zeffertt, Cambridge Broadband Ltd, ajz@cambridgebroadband.com
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA
#######################################################################
# Notes:
# 
# This configuration utility communicates with macvlan.o, the MAC address
# based VLAN support module.
#
# It uses an IOCTL interface which allows you to
#
# 1. enable/disable MAC address based VLANS over an ether type net_device
# 2. add/remove a MAC address based VLAN - which is an ether type net_device
#    layered over the original MACVLAN enabled ether type net_device.
# 3. bind/unbind MAC addresses to/from particular MAC address based VLANs
# 4. discover the state of MAC address based VLANs on the system.
# 5. set/get port flags, including whether to bind to destination MAC
#    or source mac.
# 6. Traffic to/from eth0 will not be affected.


#
# Example: (Assuming you are using source binding)
#
# If you enable MAC address based VLANS over eth0
#
# You may then create further VLANs, e.g. eth0#1 eth0#2 ....
# These will not receive any frames until you bind MAC addresses to them.
# If you bind 11:22:33:44:55:66 to eth0#1, then any frames received by
# eth0 with source MAC 11:22:33:44:55:66 will be routed up through eth0#1
# instead of eth0.
#
# Example: (Assuming you are using destination (local) binding)
#
# If you enable MAC address based VLANS over eth0
#
# You may then create further VLANs, e.g. eth0#1 eth0#2 ....
# These will not receive any frames until you bind MAC addresses to them.
# If you bind 11:22:33:44:55:66 to eth0#1, then any broadcast/multicast
# frames, or frames with a destination MAC 11:22:33:44:55:66
# will be routed up through eth0#1 instead of eth0
#
# For broadcasts, the packet will be duplicated for every VLAN
# with at least one MAC attached.  Attaching more than one MAC
# when destination binding makes no sense...don't do it!
#
#
# 
#######################################################################
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_macvlan.h>
#include <linux/sockios.h>
#include <string.h>
#include <errno.h>


int do_help(int argc, char *argv[]);
int do_enable(int argc, char *argv[]);
int do_disable(int argc, char *argv[]);
int do_add(int argc, char *argv[]);
int do_del(int argc, char *argv[]);
int do_bind(int argc, char *argv[]);
int do_unbind(int argc, char *argv[]);
int do_info(int argc, char *argv[]);
int do_setflags(int argc, char *argv[]);
int do_unload(int argc, char* argv[]);

struct command {
    char *name;
    char *short_help;
    int (*fn)(int argc, char *argv[]);
    char *long_help;
} command_list[] = {
    {"help",   "help on other commands", do_help, "help <command>"},
    {"enable", "enables mac based vlans over an ethernet device", do_enable,
         "enable <ifname>\n"
         " - enables mac based vlans over \"ifname\"\n"
         " - also creates a default vlan over \"ifname\" called \"ifname#0\""
    },
    {"disable", "disables mac based vlans over an ethernet device", do_disable, "disable <ifname>"},
    {"add", "creates new mac based vlan",  do_add,
         "add <ifname> <index>\n"
         " - creates a new mac based vlan called \"ifname#index\" layered over \"ifname\"\n"
         " - mac based vlans over \"ifname\" must first be enabled with \"enable\"\n"
         " - \"ifname#index\" is not mapped to any MAC address until \"bind\" is called"
    },
    {"del", "destroys a mac based vlan",  do_del,
         "del <ifname>\n"
         " - deletes a mac base vlan called \"ifname\""
    },
    {"bind", "binds macaddr to vlan",  do_bind,
         "bind  <ifname> <macaddr>\n"
         " - binds macaddr to vlan called \"ifname\""
    },
    {"unbind", "unbinds macaddr from vlan",  do_unbind,
         "unbind <ifname> <macaddr>\n"
         " - unbinds macaddr from vlan called \"ifname\""
    },
    {"unload", "Unconfigure all of the macvlan devices",
     do_unload, "Unconfigure all of the macvlan devices so module can be unloaded"},
    {"setflags", "Set port flags on a port",
     do_setflags,
         "setflags <ifname> <new_flags>\n"
         "0x01   Bind to Destination instead of source MAC"
    },
    {"info", "print state of mac based vlans",  do_info, "info"},
};
#define NCOMMANDS (sizeof(command_list)/sizeof(struct command))


int parseInt(const char* s) {
   return strtol(s, NULL, 0); //should parse HEX, Octal, and Decimal.  If not decimal, must start with 0x
}


int do_help(int argc, char *argv[])
{
    unsigned int cmd;
    if (argc < 2)
        return -1;

    for (cmd = 0; cmd < NCOMMANDS; cmd++) {
        if (!strcmp(command_list[cmd].name,argv[1]))
            break;
    }
    if (cmd == NCOMMANDS)
        return -1;
    puts(command_list[cmd].long_help);
    return 0;
}

int do_enable(int argc, char *argv[])
{
    struct macvlan_ioctl req;
    int s;

    if (argc < 2) {
        printf("usage: %s <ifname>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    req.cmd = MACVLAN_ENABLE;
    req.ifname = argv[1];   /* 
                             * name of ethernet device over which we 
                             * are enabling mac based vlans
                             */

    if (ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
       if (errno != EEXIST) {
          perror("ioctl (SIOCGIFMACVLAN, MACVLAN_ENABLE)");
          printf("errno: %i\n", errno);
          return 1;
       }
       else {
          return 0;
       }
    }
    return 0;
}


int do_setflags(int argc, char *argv[])
{
    struct macvlan_ioctl req;
    int s;

    if (argc < 3) {
        printf("usage: %s <ifname> <flags>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    req.cmd = MACVLAN_SET_PORT_FLAGS;
    req.ifname = argv[1];   /* 
                             * name of ethernet device over which we 
                             * are enabling mac based vlans
                             */
    req.ifidx = parseInt(argv[2]);

    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        perror("ioctl (SIOCGIFMACVLAN, SET_PORT_FLAGS)");
        return 1;
    }
    return 0;
}

int _do_disable(char* port, int s) {
    struct macvlan_ioctl req;
       
    req.cmd = MACVLAN_DISABLE;
    req.ifname = port;   /* 
                          * name of ethernet device over which we 
                          * are disabling mac based vlans
                          */

    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        perror("disable-port");
        return -1;
    }
    else {
       printf("Disabled port: %s\n", port);
    }
    return 0;
}

int do_disable(int argc, char *argv[])
{
    int s;

    if (argc < 2) {
        printf("usage: %s <ifname>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    return _do_disable(argv[1], s);
}

int do_add(int argc, char *argv[])
{
    int s;
    struct macvlan_ioctl req;

    if (argc < 3) {
        printf("usage: %s <ifname> <index>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    req.cmd = MACVLAN_ADD;
    req.ifname = argv[1];       /* name of lower layer i/f over which we are adding an upper layer i/f */
    req.ifidx = parseInt(argv[2]);

    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        perror("ioctl (SIOCGIFMACVLAN, MACVLAN_ADD)");
        return 1;
    }
    return 0;
}

int _do_del(char* ifname, int s) {
    struct macvlan_ioctl req;
   
    req.cmd = MACVLAN_DEL;
    req.ifname = ifname; /* name mac based vlan to destroy */
    
    if(ioctl(s, SIOCGIFMACVLAN, &req) < 0) {
        printf("failed to delete interface: %s\n", ifname);
        perror("ioctl (SIOCGIFMACVLAN, MACVLAN_DEL)");
        return -1;
    }
    else {
       printf("Deleted interface: %s\n", ifname);
    }
    
    return 0;
}

int do_del(int argc, char *argv[])
{
    int s;

    if (argc < 2) {
        printf("usage: %s <ifname>\n", argv[0]);
        return 1;
    }

    if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        perror("socket");
        return 1;
    }

    return _do_del(argv[1], s);
}

    

int get_num_ports(int s) {
   struct macvlan_ioctl req;
   struct macvlan_ioctl_reply rep;
   
   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;
}