xref: /freebsd/share/examples/libifconfig/ifchangevlan.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
27*b1d757bcSAlan Somers #include <err.h>
28*b1d757bcSAlan Somers #include <errno.h>
29*b1d757bcSAlan Somers #include <net/if.h>
30*b1d757bcSAlan Somers #include <sys/ioctl.h>
31*b1d757bcSAlan Somers #include <limits.h>
32*b1d757bcSAlan Somers #include <stdio.h>
33*b1d757bcSAlan Somers #include <stdlib.h>
34*b1d757bcSAlan Somers #include <string.h>
35*b1d757bcSAlan Somers #include <libifconfig.h>
36*b1d757bcSAlan Somers 
37*b1d757bcSAlan Somers #include <net/if_vlan_var.h>
38*b1d757bcSAlan Somers 
39*b1d757bcSAlan Somers int
main(int argc,char * argv[])40*b1d757bcSAlan Somers main(int argc, char *argv[])
41*b1d757bcSAlan Somers {
42*b1d757bcSAlan Somers 	char *ifname, *parentif;
43*b1d757bcSAlan Somers 	unsigned short vlantag;
44*b1d757bcSAlan Somers 	const char *errstr;
45*b1d757bcSAlan Somers 	ifconfig_handle_t *lifh;
46*b1d757bcSAlan Somers 
47*b1d757bcSAlan Somers 	if (argc != 4) {
48*b1d757bcSAlan Somers 		errx(EINVAL, "Invalid number of arguments."
49*b1d757bcSAlan Somers 		    " Should provide exactly three arguments: "
50*b1d757bcSAlan Somers 		    "INTERFACE, PARENT_INTERFACE and VLAN_TAG.");
51*b1d757bcSAlan Somers 	}
52*b1d757bcSAlan Somers 
53*b1d757bcSAlan Somers 	/* We have a static number of arguments. Therefore we can do it simple. */
54*b1d757bcSAlan Somers 	ifname = strdup(argv[1]);
55*b1d757bcSAlan Somers 	parentif = strdup(argv[2]);
56*b1d757bcSAlan Somers 	vlantag = strtonum(argv[3], 0, USHRT_MAX, &errstr);
57*b1d757bcSAlan Somers 
58*b1d757bcSAlan Somers 	if (errstr != NULL) {
59*b1d757bcSAlan Somers 		errx(1, "VLAN_TAG must be between 0 and %i.\n", USHRT_MAX);
60*b1d757bcSAlan Somers 	}
61*b1d757bcSAlan Somers 
62*b1d757bcSAlan Somers 	printf("Interface: %s\nNew VLAN tag: %i\n", ifname, vlantag);
63*b1d757bcSAlan Somers 
64*b1d757bcSAlan Somers 	lifh = ifconfig_open();
65*b1d757bcSAlan Somers 	if (lifh == NULL) {
66*b1d757bcSAlan Somers 		errx(ENOMEM, "Failed to open libifconfig handle.");
67*b1d757bcSAlan Somers 		return (-1);
68*b1d757bcSAlan Somers 	}
69*b1d757bcSAlan Somers 
70*b1d757bcSAlan Somers 	if (ifconfig_set_vlantag(lifh, ifname, parentif, vlantag) == 0) {
71*b1d757bcSAlan Somers 		printf("Successfully changed vlan tag.\n");
72*b1d757bcSAlan Somers 		ifconfig_close(lifh);
73*b1d757bcSAlan Somers 		lifh = NULL;
74*b1d757bcSAlan Somers 		free(ifname);
75*b1d757bcSAlan Somers 		free(parentif);
76*b1d757bcSAlan Somers 		return (0);
77*b1d757bcSAlan Somers 	}
78*b1d757bcSAlan Somers 
79*b1d757bcSAlan Somers 	switch (ifconfig_err_errtype(lifh)) {
80*b1d757bcSAlan Somers 	case SOCKET:
81*b1d757bcSAlan Somers 		warnx("couldn't create socket. This shouldn't happen.\n");
82*b1d757bcSAlan Somers 		break;
83*b1d757bcSAlan Somers 	case IOCTL:
84*b1d757bcSAlan Somers 		if (ifconfig_err_ioctlreq(lifh) == SIOCGETVLAN) {
85*b1d757bcSAlan Somers 			warnx("Target interface isn't a VLAN interface.\n");
86*b1d757bcSAlan Somers 		}
87*b1d757bcSAlan Somers 		if (ifconfig_err_ioctlreq(lifh) == SIOCSETVLAN) {
88*b1d757bcSAlan Somers 			warnx(
89*b1d757bcSAlan Somers 				"Couldn't change VLAN properties of interface.\n");
90*b1d757bcSAlan Somers 		}
91*b1d757bcSAlan Somers 		break;
92*b1d757bcSAlan Somers 	default:
93*b1d757bcSAlan Somers 		warnx(
94*b1d757bcSAlan Somers 			"This is a thorough example accommodating for temporary"
95*b1d757bcSAlan Somers 			" 'not implemented yet' errors. That's likely what happened"
96*b1d757bcSAlan Somers 			" now. If not, your guess is as good as mine. ;)"
97*b1d757bcSAlan Somers 			" Error code: %d\n", ifconfig_err_errno(
98*b1d757bcSAlan Somers 				lifh));
99*b1d757bcSAlan Somers 		break;
100*b1d757bcSAlan Somers 	}
101*b1d757bcSAlan Somers 
102*b1d757bcSAlan Somers 	ifconfig_close(lifh);
103*b1d757bcSAlan Somers 	lifh = NULL;
104*b1d757bcSAlan Somers 	free(ifname);
105*b1d757bcSAlan Somers 	free(parentif);
106*b1d757bcSAlan Somers 	return (-1);
107*b1d757bcSAlan Somers }
108