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