17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5c7e4935fSss150715 * Common Development and Distribution License (the "License").
6c7e4935fSss150715 * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate *
87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate * and limitations under the License.
127c478bd9Sstevel@tonic-gate *
137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate *
197c478bd9Sstevel@tonic-gate * CDDL HEADER END
207c478bd9Sstevel@tonic-gate */
217c478bd9Sstevel@tonic-gate /*
22*2b24ab6bSSebastien Roy * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
23c7e4935fSss150715 * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate */
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * This file contains a routine used to validate a ifconfig-style interface
287c478bd9Sstevel@tonic-gate * specification
297c478bd9Sstevel@tonic-gate */
307c478bd9Sstevel@tonic-gate
317c478bd9Sstevel@tonic-gate #include <stdlib.h>
327c478bd9Sstevel@tonic-gate #include <ctype.h>
337c478bd9Sstevel@tonic-gate #include <alloca.h>
347c478bd9Sstevel@tonic-gate #include <errno.h>
357c478bd9Sstevel@tonic-gate #include <string.h>
367c478bd9Sstevel@tonic-gate #include <libinetutil.h>
377c478bd9Sstevel@tonic-gate
387c478bd9Sstevel@tonic-gate /*
397c478bd9Sstevel@tonic-gate * Given a token with a logical unit spec, return the logical unit converted
407c478bd9Sstevel@tonic-gate * to a uint_t.
417c478bd9Sstevel@tonic-gate *
427c478bd9Sstevel@tonic-gate * Returns: 0 for success, nonzero if an error occurred. errno is set if
437c478bd9Sstevel@tonic-gate * necessary.
447c478bd9Sstevel@tonic-gate */
457c478bd9Sstevel@tonic-gate static int
getlun(const char * bp,int bpsize,uint_t * lun)467c478bd9Sstevel@tonic-gate getlun(const char *bp, int bpsize, uint_t *lun)
477c478bd9Sstevel@tonic-gate {
487c478bd9Sstevel@tonic-gate char *ep = (char *)&bp[bpsize - 1];
497c478bd9Sstevel@tonic-gate char *sp = strchr(bp, ':'), *tp;
507c478bd9Sstevel@tonic-gate
517c478bd9Sstevel@tonic-gate /* A logical unit spec looks like: <token>:<unsigned int>\0 */
527c478bd9Sstevel@tonic-gate if (isdigit(*bp) || !isdigit(*ep) || sp == NULL ||
537c478bd9Sstevel@tonic-gate strchr(sp + 1, ':') != NULL) {
547c478bd9Sstevel@tonic-gate errno = EINVAL;
557c478bd9Sstevel@tonic-gate return (-1);
567c478bd9Sstevel@tonic-gate }
577c478bd9Sstevel@tonic-gate
587c478bd9Sstevel@tonic-gate *sp++ = '\0';
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate /* Lun must be all digits */
617c478bd9Sstevel@tonic-gate for (tp = sp; tp < ep && isdigit(*tp); tp++)
627c478bd9Sstevel@tonic-gate /* Null body */;
637c478bd9Sstevel@tonic-gate if (tp != ep) {
647c478bd9Sstevel@tonic-gate errno = EINVAL;
657c478bd9Sstevel@tonic-gate return (-1);
667c478bd9Sstevel@tonic-gate }
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate *lun = atoi(sp);
697c478bd9Sstevel@tonic-gate return (0);
707c478bd9Sstevel@tonic-gate }
717c478bd9Sstevel@tonic-gate
727c478bd9Sstevel@tonic-gate /*
737c478bd9Sstevel@tonic-gate * Given a single token ending with a ppa spec, return the ppa spec converted
747c478bd9Sstevel@tonic-gate * to a uint_t.
757c478bd9Sstevel@tonic-gate *
767c478bd9Sstevel@tonic-gate * Returns: 0 for success, nonzero if an error occurred. errno is set if
777c478bd9Sstevel@tonic-gate * necessary.
787c478bd9Sstevel@tonic-gate */
797c478bd9Sstevel@tonic-gate static int
getppa(const char * bp,int bpsize,uint_t * ppa)807c478bd9Sstevel@tonic-gate getppa(const char *bp, int bpsize, uint_t *ppa)
817c478bd9Sstevel@tonic-gate {
827c478bd9Sstevel@tonic-gate char *ep = (char *)&bp[bpsize - 1];
837c478bd9Sstevel@tonic-gate char *tp;
847c478bd9Sstevel@tonic-gate
85c7e4935fSss150715 if (!isdigit(*ep)) {
867c478bd9Sstevel@tonic-gate errno = EINVAL;
877c478bd9Sstevel@tonic-gate return (-1);
887c478bd9Sstevel@tonic-gate }
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate for (tp = ep; tp >= bp && isdigit(*tp); tp--)
917c478bd9Sstevel@tonic-gate /* Null body */;
927c478bd9Sstevel@tonic-gate
93*2b24ab6bSSebastien Roy if (*tp == ':') {
947c478bd9Sstevel@tonic-gate errno = EINVAL;
957c478bd9Sstevel@tonic-gate return (-1);
967c478bd9Sstevel@tonic-gate }
977c478bd9Sstevel@tonic-gate
987c478bd9Sstevel@tonic-gate *ppa = atoi(tp + 1);
997c478bd9Sstevel@tonic-gate return (0);
1007c478bd9Sstevel@tonic-gate }
1017c478bd9Sstevel@tonic-gate
1027c478bd9Sstevel@tonic-gate /*
1037c478bd9Sstevel@tonic-gate * Given an ifconfig-style inet relative-path interface specification
104*2b24ab6bSSebastien Roy * (e.g: bge0:2), validate its form and decompose the contents into a
105*2b24ab6bSSebastien Roy * dynamically allocated ifspec_t.
1067c478bd9Sstevel@tonic-gate *
1077c478bd9Sstevel@tonic-gate * Returns ifspec_t for success, NULL pointer if spec is malformed.
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate boolean_t
ifparse_ifspec(const char * ifname,ifspec_t * ifsp)1107c478bd9Sstevel@tonic-gate ifparse_ifspec(const char *ifname, ifspec_t *ifsp)
1117c478bd9Sstevel@tonic-gate {
112*2b24ab6bSSebastien Roy char *lp, *tp;
113*2b24ab6bSSebastien Roy char ifnamecp[LIFNAMSIZ];
1147c478bd9Sstevel@tonic-gate
115*2b24ab6bSSebastien Roy /* snag a copy we can modify */
116*2b24ab6bSSebastien Roy if (strlcpy(ifnamecp, ifname, LIFNAMSIZ) >= LIFNAMSIZ) {
1177c478bd9Sstevel@tonic-gate errno = EINVAL;
1187c478bd9Sstevel@tonic-gate return (B_FALSE);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate
1217c478bd9Sstevel@tonic-gate ifsp->ifsp_lunvalid = B_FALSE;
1227c478bd9Sstevel@tonic-gate
1237c478bd9Sstevel@tonic-gate /*
1247c478bd9Sstevel@tonic-gate * An interface name must have the format of:
125*2b24ab6bSSebastien Roy * dev[ppa][:lun]
1267c478bd9Sstevel@tonic-gate *
1277c478bd9Sstevel@tonic-gate * lun - logical unit number.
1287c478bd9Sstevel@tonic-gate */
1297c478bd9Sstevel@tonic-gate
1307c478bd9Sstevel@tonic-gate /* Any logical units? */
1317c478bd9Sstevel@tonic-gate lp = strchr(ifnamecp, ':');
1327c478bd9Sstevel@tonic-gate if (lp != NULL) {
1337c478bd9Sstevel@tonic-gate if (getlun(lp, strlen(lp), &ifsp->ifsp_lun) != 0)
1347c478bd9Sstevel@tonic-gate return (B_FALSE);
1357c478bd9Sstevel@tonic-gate ifsp->ifsp_lunvalid = B_TRUE;
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate
1387c478bd9Sstevel@tonic-gate (void) strlcpy(ifsp->ifsp_devnm, ifnamecp, LIFNAMSIZ);
1397c478bd9Sstevel@tonic-gate
140*2b24ab6bSSebastien Roy /* Find ppa */
141*2b24ab6bSSebastien Roy if (getppa(ifsp->ifsp_devnm, strlen(ifsp->ifsp_devnm),
142*2b24ab6bSSebastien Roy &ifsp->ifsp_ppa) != 0) {
143*2b24ab6bSSebastien Roy return (B_FALSE);
144*2b24ab6bSSebastien Roy }
1457c478bd9Sstevel@tonic-gate
1467c478bd9Sstevel@tonic-gate /* strip the ppa off of the device name if present */
1477c478bd9Sstevel@tonic-gate for (tp = &ifsp->ifsp_devnm[strlen(ifsp->ifsp_devnm) - 1];
148*2b24ab6bSSebastien Roy tp >= ifsp->ifsp_devnm && isdigit(*tp); tp--) {
1497c478bd9Sstevel@tonic-gate *tp = '\0';
150c7e4935fSss150715 }
1517c478bd9Sstevel@tonic-gate
152*2b24ab6bSSebastien Roy return (B_TRUE);
1537c478bd9Sstevel@tonic-gate }
154