1*b1d757bcSAlan Somers /* 2*b1d757bcSAlan Somers * Copyright (c) 2017, Marie Helene Kvello-Aune 3*b1d757bcSAlan Somers * All rights reserved. 4*b1d757bcSAlan Somers * 5*b1d757bcSAlan Somers * Redistribution and use in source and binary forms, with or without modification, 6*b1d757bcSAlan Somers * are permitted provided that the following conditions are met: 7*b1d757bcSAlan Somers * 8*b1d757bcSAlan Somers * 1. Redistributions of source code must retain the above copyright notice, 9*b1d757bcSAlan Somers * thislist of conditions and the following disclaimer. 10*b1d757bcSAlan Somers * 11*b1d757bcSAlan Somers * 2. Redistributions in binary form must reproduce the above copyright notice, 12*b1d757bcSAlan Somers * this list of conditions and the following disclaimer in the documentation and/or 13*b1d757bcSAlan Somers * other materials provided with the distribution. 14*b1d757bcSAlan Somers * 15*b1d757bcSAlan Somers * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16*b1d757bcSAlan Somers * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 17*b1d757bcSAlan Somers * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18*b1d757bcSAlan Somers * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 19*b1d757bcSAlan Somers * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20*b1d757bcSAlan Somers * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 21*b1d757bcSAlan Somers * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 22*b1d757bcSAlan Somers * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 23*b1d757bcSAlan Somers * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 24*b1d757bcSAlan Somers * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25*b1d757bcSAlan Somers * 26*b1d757bcSAlan Somers * $FreeBSD$ 27*b1d757bcSAlan Somers */ 28*b1d757bcSAlan Somers 29*b1d757bcSAlan Somers #include <err.h> 30*b1d757bcSAlan Somers #include <errno.h> 31*b1d757bcSAlan Somers #include <net/if.h> 32*b1d757bcSAlan Somers #include <sys/ioctl.h> 33*b1d757bcSAlan Somers #include <limits.h> 34*b1d757bcSAlan Somers #include <stdio.h> 35*b1d757bcSAlan Somers #include <stdlib.h> 36*b1d757bcSAlan Somers #include <string.h> 37*b1d757bcSAlan Somers #include <libifconfig.h> 38*b1d757bcSAlan Somers 39*b1d757bcSAlan Somers #include <net/if_vlan_var.h> 40*b1d757bcSAlan Somers 41*b1d757bcSAlan Somers int 42*b1d757bcSAlan Somers main(int argc, char *argv[]) 43*b1d757bcSAlan Somers { 44*b1d757bcSAlan Somers char *ifname, *parentif; 45*b1d757bcSAlan Somers unsigned short vlantag; 46*b1d757bcSAlan Somers const char *errstr; 47*b1d757bcSAlan Somers ifconfig_handle_t *lifh; 48*b1d757bcSAlan Somers 49*b1d757bcSAlan Somers if (argc != 4) { 50*b1d757bcSAlan Somers errx(EINVAL, "Invalid number of arguments." 51*b1d757bcSAlan Somers " Should provide exactly three arguments: " 52*b1d757bcSAlan Somers "INTERFACE, PARENT_INTERFACE and VLAN_TAG."); 53*b1d757bcSAlan Somers } 54*b1d757bcSAlan Somers 55*b1d757bcSAlan Somers /* We have a static number of arguments. Therefore we can do it simple. */ 56*b1d757bcSAlan Somers ifname = strdup(argv[1]); 57*b1d757bcSAlan Somers parentif = strdup(argv[2]); 58*b1d757bcSAlan Somers vlantag = strtonum(argv[3], 0, USHRT_MAX, &errstr); 59*b1d757bcSAlan Somers 60*b1d757bcSAlan Somers if (errstr != NULL) { 61*b1d757bcSAlan Somers errx(1, "VLAN_TAG must be between 0 and %i.\n", USHRT_MAX); 62*b1d757bcSAlan Somers } 63*b1d757bcSAlan Somers 64*b1d757bcSAlan Somers printf("Interface: %s\nNew VLAN tag: %i\n", ifname, vlantag); 65*b1d757bcSAlan Somers 66*b1d757bcSAlan Somers lifh = ifconfig_open(); 67*b1d757bcSAlan Somers if (lifh == NULL) { 68*b1d757bcSAlan Somers errx(ENOMEM, "Failed to open libifconfig handle."); 69*b1d757bcSAlan Somers return (-1); 70*b1d757bcSAlan Somers } 71*b1d757bcSAlan Somers 72*b1d757bcSAlan Somers if (ifconfig_set_vlantag(lifh, ifname, parentif, vlantag) == 0) { 73*b1d757bcSAlan Somers printf("Successfully changed vlan tag.\n"); 74*b1d757bcSAlan Somers ifconfig_close(lifh); 75*b1d757bcSAlan Somers lifh = NULL; 76*b1d757bcSAlan Somers free(ifname); 77*b1d757bcSAlan Somers free(parentif); 78*b1d757bcSAlan Somers return (0); 79*b1d757bcSAlan Somers } 80*b1d757bcSAlan Somers 81*b1d757bcSAlan Somers switch (ifconfig_err_errtype(lifh)) { 82*b1d757bcSAlan Somers case SOCKET: 83*b1d757bcSAlan Somers warnx("couldn't create socket. This shouldn't happen.\n"); 84*b1d757bcSAlan Somers break; 85*b1d757bcSAlan Somers case IOCTL: 86*b1d757bcSAlan Somers if (ifconfig_err_ioctlreq(lifh) == SIOCGETVLAN) { 87*b1d757bcSAlan Somers warnx("Target interface isn't a VLAN interface.\n"); 88*b1d757bcSAlan Somers } 89*b1d757bcSAlan Somers if (ifconfig_err_ioctlreq(lifh) == SIOCSETVLAN) { 90*b1d757bcSAlan Somers warnx( 91*b1d757bcSAlan Somers "Couldn't change VLAN properties of interface.\n"); 92*b1d757bcSAlan Somers } 93*b1d757bcSAlan Somers break; 94*b1d757bcSAlan Somers default: 95*b1d757bcSAlan Somers warnx( 96*b1d757bcSAlan Somers "This is a thorough example accommodating for temporary" 97*b1d757bcSAlan Somers " 'not implemented yet' errors. That's likely what happened" 98*b1d757bcSAlan Somers " now. If not, your guess is as good as mine. ;)" 99*b1d757bcSAlan Somers " Error code: %d\n", ifconfig_err_errno( 100*b1d757bcSAlan Somers lifh)); 101*b1d757bcSAlan Somers break; 102*b1d757bcSAlan Somers } 103*b1d757bcSAlan Somers 104*b1d757bcSAlan Somers ifconfig_close(lifh); 105*b1d757bcSAlan Somers lifh = NULL; 106*b1d757bcSAlan Somers free(ifname); 107*b1d757bcSAlan Somers free(parentif); 108*b1d757bcSAlan Somers return (-1); 109*b1d757bcSAlan Somers } 110