]> git.saurik.com Git - apple/network_cmds.git/blame - ifconfig.tproj/if6lowpan.c
network_cmds-606.100.3.tar.gz
[apple/network_cmds.git] / ifconfig.tproj / if6lowpan.c
CommitLineData
e0b07f2d
A
1/*
2 * Copyright (c) 2017-2018 Apple Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28
29/*
30 * Copyright (c) 1999
31 * Bill Paul <wpaul@ctr.columbia.edu>. All rights reserved.
32 *
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
35 * are met:
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by Bill Paul.
44 * 4. Neither the name of the author nor the names of any co-contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
52 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
53 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
54 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
55 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
56 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
57 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
58 * THE POSSIBILITY OF SUCH DAMAGE.
59 */
60
61#include <sys/param.h>
62#include <sys/ioctl.h>
63#include <sys/socket.h>
64#include <sys/sockio.h>
65
66#include <stdlib.h>
67#include <unistd.h>
68
69#include <net/ethernet.h>
70#include <net/if.h>
71#include <net/if_var.h>
72#include <net/if_6lowpan_var.h>
73#include <net/route.h>
74
75#include <ctype.h>
76#include <stdio.h>
77#include <string.h>
78#include <stdlib.h>
79#include <unistd.h>
80#include <err.h>
81#include <errno.h>
82
83#include "ifconfig.h"
84
85#include <sys/cdefs.h>
86
87
88static boolean_t is_sixlowpan_inited = FALSE;
89static struct sixlowpanreq params;
90
91static int
92get6lowpan(int s, struct ifreq *ifr, struct sixlowpanreq *req)
93{
94 bzero((char *)req, sizeof(*req));
95 ifr->ifr_data = (caddr_t)req;
96 return ioctl(s, SIOCGIF6LOWPAN, (caddr_t)ifr);
97}
98
99static void
100sixlowpan_status(int s)
101{
102 struct sixlowpanreq req;
103
104 if (get6lowpan(s, &ifr, &req) != -1)
105 printf("\t6lowpan: parent interface: %s\n",
106 req.parent[0] == '\0' ?
107 "<none>" : req.parent);
108}
109
110
111static void
112set6lowpandev(const char *val, int d, int s, const struct afswtch *afp)
113{
114 struct sixlowpanreq req;
115
116 strlcpy(params.parent, val, sizeof(params.parent));
117 is_sixlowpan_inited = TRUE;
118 fprintf(stderr, "val %s\n", val);
119
120 strlcpy(req.parent, val, sizeof(req.parent));
121 ifr.ifr_data = (caddr_t) &req;
122 if (ioctl(s, SIOCSIF6LOWPAN, (caddr_t)&ifr) == -1)
123 err(1, "SIOCSIF6LOWPAN");
124}
125
126static void
127unset6lowpandev(const char *val, int d, int s, const struct afswtch *afp)
128{
129 struct sixlowpanreq req;
130
131 bzero((char *)&req, sizeof(req));
132 ifr.ifr_data = (caddr_t)&req;
133
134 if (ioctl(s, SIOCGIF6LOWPAN, (caddr_t)&ifr) == -1)
135 err(1, "SIOCGIF6LOWPAN");
136
137 bzero((char *)&req, sizeof(req));
138 if (ioctl(s, SIOCSIF6LOWPAN, (caddr_t)&ifr) == -1)
139 err(1, "SIOCSIF6LOWPAN");
140}
141
142static void
143sixlowpan_create(const char *val, int d, int s, const struct afswtch *afp)
144{
145 strlcpy(params.parent, val, sizeof(params.parent));
146 is_sixlowpan_inited = TRUE;
147}
148
149static void
150sixlowpan_clone_cb(int s, void *arg)
151{
152 if (is_sixlowpan_inited == TRUE && params.parent[0] == '\0')
153 errx(1, "6lowpandev must be specified");
154}
155
156static struct cmd sixlowpan_cmds[] = {
157 DEF_CLONE_CMD_ARG("6lowpandev", sixlowpan_create),
158 DEF_CMD_OPTARG("6lowpansetdev", set6lowpandev),
159 DEF_CMD_OPTARG("6lowpanunsetdev", unset6lowpandev),
160};
161static struct afswtch af_6lowpan = {
162 .af_name = "af_6lowpan",
163 .af_af = AF_UNSPEC,
164 .af_other_status = sixlowpan_status,
165};
166
167static __constructor void
168sixlowpan_ctor(void)
169{
170#define N(a) (sizeof(a) / sizeof(a[0]))
171 int i;
172
173 for (i = 0; i < N(sixlowpan_cmds); i++)
174 cmd_register(&sixlowpan_cmds[i]);
175 af_register(&af_6lowpan);
176 callback_register(sixlowpan_clone_cb, NULL);
177#undef N
178}