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