1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (the "License"). You may not use this file except in compliance
* with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2001 by Sun Microsystems, Inc.
* All rights reserved.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
/*
* This file contains a routine used to validate a ifconfig-style interface
* specification
*/
#include <stdlib.h>
#include <ctype.h>
#include <alloca.h>
#include <errno.h>
#include <string.h>
#include <libinetutil.h>
/*
* Given a token with a logical unit spec, return the logical unit converted
* to a uint_t.
*
* Returns: 0 for success, nonzero if an error occurred. errno is set if
* necessary.
*/
static int
getlun(const char *bp, int bpsize, uint_t *lun)
{
char *ep = (char *)&bp[bpsize - 1];
char *sp = strchr(bp, ':'), *tp;
/* A logical unit spec looks like: <token>:<unsigned int>\0 */
if (isdigit(*bp) || !isdigit(*ep) || sp == NULL ||
strchr(sp + 1, ':') != NULL) {
errno = EINVAL;
return (-1);
}
*sp++ = '\0';
/* Lun must be all digits */
for (tp = sp; tp < ep && isdigit(*tp); tp++)
/* Null body */;
if (tp != ep) {
errno = EINVAL;
return (-1);
}
*lun = atoi(sp);
return (0);
}
/*
* Given a single token ending with a ppa spec, return the ppa spec converted
* to a uint_t.
*
* Returns: 0 for success, nonzero if an error occurred. errno is set if
* necessary.
*/
static int
getppa(const char *bp, int bpsize, uint_t *ppa)
{
char *ep = (char *)&bp[bpsize - 1];
char *tp;
if (isdigit(*bp) || !isdigit(*ep)) {
errno = EINVAL;
return (-1);
}
for (tp = ep; tp >= bp && isdigit(*tp); tp--)
/* Null body */;
if (*tp == '.' || *tp == ':') {
errno = EINVAL;
return (-1);
}
*ppa = atoi(tp + 1);
return (0);
}
/*
* Given an ifconfig-style inet relative-path interface specification
* (e.g: hme0.foo.ip.udp:2), validate its form and decompose the contents
* into a dynamically allocated ifspec_t.
*
* Returns ifspec_t for success, NULL pointer if spec is malformed.
*/
boolean_t
ifparse_ifspec(const char *ifname, ifspec_t *ifsp)
{
char *mp, *ep, *lp, *tp;
char *ifnamecp;
size_t iflen;
boolean_t have_ppa = B_FALSE;
iflen = strlen(ifname);
if (iflen > LIFNAMSIZ) {
errno = EINVAL;
return (B_FALSE);
}
/* snag a copy we can modify */
ifnamecp = alloca(iflen + 1);
(void) strlcpy(ifnamecp, ifname, iflen + 1);
ifsp->ifsp_lunvalid = B_FALSE;
/*
* An interface name must have the format of:
* dev[ppa][.module[.module...][ppa]][:lun]
*
* where only one ppa may be specified e.g. ip0.foo.tun or ip.foo.tun0
*
* lun - logical unit number.
*
* Produce substrings for each grouping, starting first with modules,
* then lun, devname, and finally ppa.
*/
/* Any modules? */
mp = strchr(ifnamecp, '.');
/* Any logical units? */
lp = strchr(ifnamecp, ':');
if (lp != NULL && mp != NULL && lp < mp) {
errno = EINVAL;
return (B_FALSE);
}
ifsp->ifsp_modcnt = 0;
if (mp != NULL) {
*mp++ = '\0';
if (lp != NULL)
*lp = '\0';
while (mp != NULL && ifsp->ifsp_modcnt <= IFSP_MAXMODS) {
if ((ep = strchr(mp, '.')) != NULL)
*ep++ = '\0';
(void) strlcpy(ifsp->ifsp_mods[ifsp->ifsp_modcnt++],
mp, LIFNAMSIZ);
mp = ep;
}
if (lp != NULL)
*lp = ':';
if (ifsp->ifsp_modcnt > IFSP_MAXMODS) {
errno = E2BIG;
return (B_FALSE);
}
}
if (lp != NULL) {
if (getlun(lp, strlen(lp), &ifsp->ifsp_lun) != 0)
return (B_FALSE);
ifsp->ifsp_lunvalid = B_TRUE;
}
(void) strlcpy(ifsp->ifsp_devnm, ifnamecp, LIFNAMSIZ);
/* Find ppa - has to be part of devname or part of last module name */
/* This should be changed to require the latter of the two */
if (getppa(ifsp->ifsp_devnm, strlen(ifsp->ifsp_devnm),
&ifsp->ifsp_ppa) == 0)
have_ppa = B_TRUE;
if (ifsp->ifsp_modcnt != 0 &&
getppa(ifsp->ifsp_mods[ifsp->ifsp_modcnt - 1],
strlen(ifsp->ifsp_mods[ifsp->ifsp_modcnt - 1]),
&ifsp->ifsp_ppa) == 0) {
if (!have_ppa)
have_ppa = B_TRUE;
else
return (B_FALSE); /* only one please */
}
if (!have_ppa)
return (B_FALSE);
/* strip the ppa off of the device name if present */
for (tp = &ifsp->ifsp_devnm[strlen(ifsp->ifsp_devnm) - 1];
tp >= ifsp->ifsp_devnm && isdigit(*tp); tp--)
*tp = '\0';
return (B_TRUE);
}
|