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