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 5*c7e4935fSss150715 * Common Development and Distribution License (the "License"). 6*c7e4935fSss150715 * 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*c7e4935fSss150715 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 23*c7e4935fSss150715 * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate /* 297c478bd9Sstevel@tonic-gate * This file contains a routine used to validate a ifconfig-style interface 307c478bd9Sstevel@tonic-gate * specification 317c478bd9Sstevel@tonic-gate */ 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <ctype.h> 357c478bd9Sstevel@tonic-gate #include <alloca.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <string.h> 387c478bd9Sstevel@tonic-gate #include <libinetutil.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate /* 417c478bd9Sstevel@tonic-gate * Given a token with a logical unit spec, return the logical unit converted 427c478bd9Sstevel@tonic-gate * to a uint_t. 437c478bd9Sstevel@tonic-gate * 447c478bd9Sstevel@tonic-gate * Returns: 0 for success, nonzero if an error occurred. errno is set if 457c478bd9Sstevel@tonic-gate * necessary. 467c478bd9Sstevel@tonic-gate */ 477c478bd9Sstevel@tonic-gate static int 487c478bd9Sstevel@tonic-gate getlun(const char *bp, int bpsize, uint_t *lun) 497c478bd9Sstevel@tonic-gate { 507c478bd9Sstevel@tonic-gate char *ep = (char *)&bp[bpsize - 1]; 517c478bd9Sstevel@tonic-gate char *sp = strchr(bp, ':'), *tp; 527c478bd9Sstevel@tonic-gate 537c478bd9Sstevel@tonic-gate /* A logical unit spec looks like: <token>:<unsigned int>\0 */ 547c478bd9Sstevel@tonic-gate if (isdigit(*bp) || !isdigit(*ep) || sp == NULL || 557c478bd9Sstevel@tonic-gate strchr(sp + 1, ':') != NULL) { 567c478bd9Sstevel@tonic-gate errno = EINVAL; 577c478bd9Sstevel@tonic-gate return (-1); 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate *sp++ = '\0'; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate /* Lun must be all digits */ 637c478bd9Sstevel@tonic-gate for (tp = sp; tp < ep && isdigit(*tp); tp++) 647c478bd9Sstevel@tonic-gate /* Null body */; 657c478bd9Sstevel@tonic-gate if (tp != ep) { 667c478bd9Sstevel@tonic-gate errno = EINVAL; 677c478bd9Sstevel@tonic-gate return (-1); 687c478bd9Sstevel@tonic-gate } 697c478bd9Sstevel@tonic-gate 707c478bd9Sstevel@tonic-gate *lun = atoi(sp); 717c478bd9Sstevel@tonic-gate return (0); 727c478bd9Sstevel@tonic-gate } 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate /* 757c478bd9Sstevel@tonic-gate * Given a single token ending with a ppa spec, return the ppa spec converted 767c478bd9Sstevel@tonic-gate * to a uint_t. 777c478bd9Sstevel@tonic-gate * 787c478bd9Sstevel@tonic-gate * Returns: 0 for success, nonzero if an error occurred. errno is set if 797c478bd9Sstevel@tonic-gate * necessary. 807c478bd9Sstevel@tonic-gate */ 817c478bd9Sstevel@tonic-gate static int 827c478bd9Sstevel@tonic-gate getppa(const char *bp, int bpsize, uint_t *ppa) 837c478bd9Sstevel@tonic-gate { 847c478bd9Sstevel@tonic-gate char *ep = (char *)&bp[bpsize - 1]; 857c478bd9Sstevel@tonic-gate char *tp; 867c478bd9Sstevel@tonic-gate 87*c7e4935fSss150715 if (!isdigit(*ep)) { 887c478bd9Sstevel@tonic-gate errno = EINVAL; 897c478bd9Sstevel@tonic-gate return (-1); 907c478bd9Sstevel@tonic-gate } 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate for (tp = ep; tp >= bp && isdigit(*tp); tp--) 937c478bd9Sstevel@tonic-gate /* Null body */; 947c478bd9Sstevel@tonic-gate 957c478bd9Sstevel@tonic-gate if (*tp == '.' || *tp == ':') { 967c478bd9Sstevel@tonic-gate errno = EINVAL; 977c478bd9Sstevel@tonic-gate return (-1); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate *ppa = atoi(tp + 1); 1017c478bd9Sstevel@tonic-gate return (0); 1027c478bd9Sstevel@tonic-gate } 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate /* 1057c478bd9Sstevel@tonic-gate * Given an ifconfig-style inet relative-path interface specification 106*c7e4935fSss150715 * (e.g: hme.[module].[module][PPA]:2), validate its form and decompose the 107*c7e4935fSss150715 * contents into a dynamically allocated ifspec_t. 1087c478bd9Sstevel@tonic-gate * 1097c478bd9Sstevel@tonic-gate * Returns ifspec_t for success, NULL pointer if spec is malformed. 1107c478bd9Sstevel@tonic-gate */ 1117c478bd9Sstevel@tonic-gate boolean_t 1127c478bd9Sstevel@tonic-gate ifparse_ifspec(const char *ifname, ifspec_t *ifsp) 1137c478bd9Sstevel@tonic-gate { 1147c478bd9Sstevel@tonic-gate char *mp, *ep, *lp, *tp; 1157c478bd9Sstevel@tonic-gate char *ifnamecp; 1167c478bd9Sstevel@tonic-gate size_t iflen; 1177c478bd9Sstevel@tonic-gate boolean_t have_ppa = B_FALSE; 1187c478bd9Sstevel@tonic-gate 1197c478bd9Sstevel@tonic-gate iflen = strlen(ifname); 1207c478bd9Sstevel@tonic-gate if (iflen > LIFNAMSIZ) { 1217c478bd9Sstevel@tonic-gate errno = EINVAL; 1227c478bd9Sstevel@tonic-gate return (B_FALSE); 1237c478bd9Sstevel@tonic-gate } 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* snag a copy we can modify */ 1267c478bd9Sstevel@tonic-gate ifnamecp = alloca(iflen + 1); 1277c478bd9Sstevel@tonic-gate (void) strlcpy(ifnamecp, ifname, iflen + 1); 1287c478bd9Sstevel@tonic-gate 1297c478bd9Sstevel@tonic-gate ifsp->ifsp_lunvalid = B_FALSE; 1307c478bd9Sstevel@tonic-gate 1317c478bd9Sstevel@tonic-gate /* 1327c478bd9Sstevel@tonic-gate * An interface name must have the format of: 133*c7e4935fSss150715 * dev[.module[.module...]][ppa][:lun] 1347c478bd9Sstevel@tonic-gate * 135*c7e4935fSss150715 * where the ppa must be specified at the end of the interface name. 136*c7e4935fSss150715 * e.g. ip.foo.tun0 1377c478bd9Sstevel@tonic-gate * 1387c478bd9Sstevel@tonic-gate * lun - logical unit number. 1397c478bd9Sstevel@tonic-gate * 1407c478bd9Sstevel@tonic-gate * Produce substrings for each grouping, starting first with modules, 1417c478bd9Sstevel@tonic-gate * then lun, devname, and finally ppa. 1427c478bd9Sstevel@tonic-gate */ 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate /* Any modules? */ 1457c478bd9Sstevel@tonic-gate mp = strchr(ifnamecp, '.'); 1467c478bd9Sstevel@tonic-gate 1477c478bd9Sstevel@tonic-gate /* Any logical units? */ 1487c478bd9Sstevel@tonic-gate lp = strchr(ifnamecp, ':'); 1497c478bd9Sstevel@tonic-gate 1507c478bd9Sstevel@tonic-gate if (lp != NULL && mp != NULL && lp < mp) { 1517c478bd9Sstevel@tonic-gate errno = EINVAL; 1527c478bd9Sstevel@tonic-gate return (B_FALSE); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate ifsp->ifsp_modcnt = 0; 1567c478bd9Sstevel@tonic-gate if (mp != NULL) { 1577c478bd9Sstevel@tonic-gate *mp++ = '\0'; 1587c478bd9Sstevel@tonic-gate if (lp != NULL) 1597c478bd9Sstevel@tonic-gate *lp = '\0'; 1607c478bd9Sstevel@tonic-gate while (mp != NULL && ifsp->ifsp_modcnt <= IFSP_MAXMODS) { 1617c478bd9Sstevel@tonic-gate if ((ep = strchr(mp, '.')) != NULL) 1627c478bd9Sstevel@tonic-gate *ep++ = '\0'; 1637c478bd9Sstevel@tonic-gate (void) strlcpy(ifsp->ifsp_mods[ifsp->ifsp_modcnt++], 1647c478bd9Sstevel@tonic-gate mp, LIFNAMSIZ); 1657c478bd9Sstevel@tonic-gate mp = ep; 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate if (lp != NULL) 1687c478bd9Sstevel@tonic-gate *lp = ':'; 1697c478bd9Sstevel@tonic-gate if (ifsp->ifsp_modcnt > IFSP_MAXMODS) { 1707c478bd9Sstevel@tonic-gate errno = E2BIG; 1717c478bd9Sstevel@tonic-gate return (B_FALSE); 1727c478bd9Sstevel@tonic-gate } 1737c478bd9Sstevel@tonic-gate } 1747c478bd9Sstevel@tonic-gate 1757c478bd9Sstevel@tonic-gate if (lp != NULL) { 1767c478bd9Sstevel@tonic-gate if (getlun(lp, strlen(lp), &ifsp->ifsp_lun) != 0) 1777c478bd9Sstevel@tonic-gate return (B_FALSE); 1787c478bd9Sstevel@tonic-gate ifsp->ifsp_lunvalid = B_TRUE; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate 1817c478bd9Sstevel@tonic-gate (void) strlcpy(ifsp->ifsp_devnm, ifnamecp, LIFNAMSIZ); 1827c478bd9Sstevel@tonic-gate 183*c7e4935fSss150715 /* 184*c7e4935fSss150715 * Find ppa - has to be part of devname or if modules exist part of 185*c7e4935fSss150715 * last module name. 186*c7e4935fSss150715 */ 1877c478bd9Sstevel@tonic-gate if (ifsp->ifsp_modcnt != 0 && 1887c478bd9Sstevel@tonic-gate getppa(ifsp->ifsp_mods[ifsp->ifsp_modcnt - 1], 1897c478bd9Sstevel@tonic-gate strlen(ifsp->ifsp_mods[ifsp->ifsp_modcnt - 1]), 1907c478bd9Sstevel@tonic-gate &ifsp->ifsp_ppa) == 0) { 1917c478bd9Sstevel@tonic-gate have_ppa = B_TRUE; 192*c7e4935fSss150715 } else if (ifsp->ifsp_modcnt == 0 && 193*c7e4935fSss150715 getppa(ifsp->ifsp_devnm, strlen(ifsp->ifsp_devnm), 194*c7e4935fSss150715 &ifsp->ifsp_ppa) == 0) { 195*c7e4935fSss150715 have_ppa = B_TRUE; 1967c478bd9Sstevel@tonic-gate 1977c478bd9Sstevel@tonic-gate /* strip the ppa off of the device name if present */ 1987c478bd9Sstevel@tonic-gate for (tp = &ifsp->ifsp_devnm[strlen(ifsp->ifsp_devnm) - 1]; 1997c478bd9Sstevel@tonic-gate tp >= ifsp->ifsp_devnm && isdigit(*tp); tp--) 2007c478bd9Sstevel@tonic-gate *tp = '\0'; 201*c7e4935fSss150715 } 2027c478bd9Sstevel@tonic-gate 203*c7e4935fSss150715 return (have_ppa); 2047c478bd9Sstevel@tonic-gate } 205