xref: /freebsd/share/examples/libifconfig/ifcreatevlan.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 
38*b1d757bcSAlan Somers int
main(int argc,char * argv[])39*b1d757bcSAlan Somers main(int argc, char *argv[])
40*b1d757bcSAlan Somers {
41*b1d757bcSAlan Somers 	char *parentif, *ifactualname;
42*b1d757bcSAlan Somers 	unsigned short vlantag;
43*b1d757bcSAlan Somers 	const char *errstr;
44*b1d757bcSAlan Somers 	ifconfig_handle_t *lifh;
45*b1d757bcSAlan Somers 
46*b1d757bcSAlan Somers 	if (argc != 3) {
47*b1d757bcSAlan Somers 		errx(EINVAL, "Invalid number of arguments."
48*b1d757bcSAlan Somers 		    " Should provide exactly two arguments: "
49*b1d757bcSAlan Somers 		    "PARENT_INTERFACE and VLAN_TAG.");
50*b1d757bcSAlan Somers 	}
51*b1d757bcSAlan Somers 
52*b1d757bcSAlan Somers 	/* We have a static number of arguments. Therefore we can do it simple. */
53*b1d757bcSAlan Somers 	parentif = strdup(argv[1]);
54*b1d757bcSAlan Somers 	vlantag = strtonum(argv[2], 0, USHRT_MAX, &errstr);
55*b1d757bcSAlan Somers 
56*b1d757bcSAlan Somers 	if (errstr != NULL) {
57*b1d757bcSAlan Somers 		errx(1, "VLAN_TAG must be between 0 and %i.\n", USHRT_MAX);
58*b1d757bcSAlan Somers 	}
59*b1d757bcSAlan Somers 
60*b1d757bcSAlan Somers 	printf("Parent interface: %s\nVLAN tag: %i\n", parentif, vlantag);
61*b1d757bcSAlan Somers 
62*b1d757bcSAlan Somers 	lifh = ifconfig_open();
63*b1d757bcSAlan Somers 	if (lifh == NULL) {
64*b1d757bcSAlan Somers 		errx(ENOMEM, "Failed to open libifconfig handle.");
65*b1d757bcSAlan Somers 		return (-1);
66*b1d757bcSAlan Somers 	}
67*b1d757bcSAlan Somers 
68*b1d757bcSAlan Somers 	if (ifconfig_create_interface_vlan(lifh, "vlan", &ifactualname,
69*b1d757bcSAlan Somers 	    parentif, vlantag) == 0) {
70*b1d757bcSAlan Somers 		printf("Successfully created interface '%s'\n", ifactualname);
71*b1d757bcSAlan Somers 		ifconfig_close(lifh);
72*b1d757bcSAlan Somers 		lifh = NULL;
73*b1d757bcSAlan Somers 		free(parentif);
74*b1d757bcSAlan Somers 		free(ifactualname);
75*b1d757bcSAlan Somers 		return (0);
76*b1d757bcSAlan Somers 	}
77*b1d757bcSAlan Somers 
78*b1d757bcSAlan Somers 	switch (ifconfig_err_errtype(lifh)) {
79*b1d757bcSAlan Somers 	case SOCKET:
80*b1d757bcSAlan Somers 		warnx("couldn't create socket. This shouldn't happen.\n");
81*b1d757bcSAlan Somers 		break;
82*b1d757bcSAlan Somers 	case IOCTL:
83*b1d757bcSAlan Somers 		if (ifconfig_err_ioctlreq(lifh) == SIOCIFCREATE2) {
84*b1d757bcSAlan Somers 			warnx(
85*b1d757bcSAlan Somers 				"Failed to create interface (SIOCIFCREATE2)\n");
86*b1d757bcSAlan Somers 		}
87*b1d757bcSAlan Somers 		break;
88*b1d757bcSAlan Somers 	default:
89*b1d757bcSAlan Somers 		warnx(
90*b1d757bcSAlan Somers 			"This is a thorough example accommodating for temporary"
91*b1d757bcSAlan Somers 			" 'not implemented yet' errors. That's likely what happened"
92*b1d757bcSAlan Somers 			" now. If not, your guess is as good as mine. ;)"
93*b1d757bcSAlan Somers 			" Error code: %d\n", ifconfig_err_errno(
94*b1d757bcSAlan Somers 				lifh));
95*b1d757bcSAlan Somers 		break;
96*b1d757bcSAlan Somers 	}
97*b1d757bcSAlan Somers 
98*b1d757bcSAlan Somers 	ifconfig_close(lifh);
99*b1d757bcSAlan Somers 	lifh = NULL;
100*b1d757bcSAlan Somers 	free(parentif);
101*b1d757bcSAlan Somers 	free(ifactualname);
102*b1d757bcSAlan Somers 	return (-1);
103*b1d757bcSAlan Somers }
104