xref: /onnv-gate/usr/src/cmd/cmd-inet/usr.sbin/in.routed/common.c (revision 5577:add1e851895c)
10Sstevel@tonic-gate /*
2*5577Ssangeeta  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
30Sstevel@tonic-gate  * Use is subject to license terms.
40Sstevel@tonic-gate  */
50Sstevel@tonic-gate 
60Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
70Sstevel@tonic-gate 
80Sstevel@tonic-gate /*
90Sstevel@tonic-gate  * Common (shared) routines used by in.routed daemon and the
100Sstevel@tonic-gate  * the rtquery utility program
110Sstevel@tonic-gate  */
120Sstevel@tonic-gate 
130Sstevel@tonic-gate #include "defs.h"
140Sstevel@tonic-gate #include <ctype.h>
150Sstevel@tonic-gate 
160Sstevel@tonic-gate /* Return the classical netmask for an IP address. */
170Sstevel@tonic-gate in_addr_t			/* host byte order */
std_mask(in_addr_t addr)180Sstevel@tonic-gate std_mask(in_addr_t addr)	/* network byte order */
190Sstevel@tonic-gate {
200Sstevel@tonic-gate 	addr = ntohl(addr);
210Sstevel@tonic-gate 
220Sstevel@tonic-gate 	if (addr == 0)		/* default route has mask 0 */
230Sstevel@tonic-gate 		return (0);
240Sstevel@tonic-gate 	if (IN_CLASSA(addr))
250Sstevel@tonic-gate 		return (IN_CLASSA_NET);
260Sstevel@tonic-gate 	if (IN_CLASSB(addr))
270Sstevel@tonic-gate 		return (IN_CLASSB_NET);
28*5577Ssangeeta 	if (IN_CLASSC(addr))
29*5577Ssangeeta 		return (IN_CLASSC_NET);
30*5577Ssangeeta 	return (IN_CLASSE_NET);
310Sstevel@tonic-gate }
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Get a network number as a name or a number, with an optional "/xx"
350Sstevel@tonic-gate  * netmask.
360Sstevel@tonic-gate  */
370Sstevel@tonic-gate boolean_t					/* 0=bad */
getnet(const char * name,in_addr_t * netp,in_addr_t * maskp)380Sstevel@tonic-gate getnet(const char *name,
390Sstevel@tonic-gate     in_addr_t *netp,			/* network in host byte order */
400Sstevel@tonic-gate     in_addr_t *maskp)			/* masks are always in host order */
410Sstevel@tonic-gate {
420Sstevel@tonic-gate 	int i;
430Sstevel@tonic-gate 	struct netent *np;
440Sstevel@tonic-gate 	in_addr_t mask;			/* in host byte order */
450Sstevel@tonic-gate 	struct in_addr in;		/* a network and so host byte order */
460Sstevel@tonic-gate 	char hname[MAXHOSTNAMELEN+1];
470Sstevel@tonic-gate 	char *mname, *p;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	/*
510Sstevel@tonic-gate 	 * The "name" argument of this function can be one of
520Sstevel@tonic-gate 	 * the follwoing:
530Sstevel@tonic-gate 	 *	a) network name/mask
540Sstevel@tonic-gate 	 *	b) network name
550Sstevel@tonic-gate 	 *	c) network number/mask
560Sstevel@tonic-gate 	 *	d) network number
570Sstevel@tonic-gate 	 *	e) host IP address/mask
580Sstevel@tonic-gate 	 *	f) host IP address
590Sstevel@tonic-gate 	 *	g) "default"
600Sstevel@tonic-gate 	 *
610Sstevel@tonic-gate 	 * Detect and separate "1.2.3.4/24"
620Sstevel@tonic-gate 	 */
630Sstevel@tonic-gate 	if (NULL != (mname = strrchr(name, '/'))) {
640Sstevel@tonic-gate 		i = (int)(mname - name);
650Sstevel@tonic-gate 		if (i > (int)sizeof (hname)-1)	/* name too long */
660Sstevel@tonic-gate 			return (_B_FALSE);
670Sstevel@tonic-gate 		(void) memmove(hname, name, i);
680Sstevel@tonic-gate 		hname[i] = '\0';
690Sstevel@tonic-gate 		mname++;
700Sstevel@tonic-gate 		name = hname;
710Sstevel@tonic-gate 	}
720Sstevel@tonic-gate 
730Sstevel@tonic-gate 	if ((in.s_addr = inet_network(name)) == (in_addr_t)-1) {
740Sstevel@tonic-gate 		if (mname == NULL && strcasecmp(name, "default") == 0)
750Sstevel@tonic-gate 			in.s_addr = ntohl(RIP_DEFAULT);
760Sstevel@tonic-gate 		else if ((np = getnetbyname(name)) != NULL)
770Sstevel@tonic-gate 			in.s_addr = np->n_net;
780Sstevel@tonic-gate 		else
790Sstevel@tonic-gate 			return (_B_FALSE);
800Sstevel@tonic-gate 	}
810Sstevel@tonic-gate 	/* Left-align the host-byte-order result from above. */
820Sstevel@tonic-gate 	if (0 == (in.s_addr & 0xff000000))
830Sstevel@tonic-gate 		in.s_addr <<= 8;
840Sstevel@tonic-gate 	if (0 == (in.s_addr & 0xff000000))
850Sstevel@tonic-gate 		in.s_addr <<= 8;
860Sstevel@tonic-gate 	if (0 == (in.s_addr & 0xff000000))
870Sstevel@tonic-gate 		in.s_addr <<= 8;
880Sstevel@tonic-gate 
890Sstevel@tonic-gate 	if (mname == NULL) {
900Sstevel@tonic-gate 		mask = std_mask(htonl(in.s_addr));
910Sstevel@tonic-gate 		if ((~mask & in.s_addr) != 0)
920Sstevel@tonic-gate 			mask = HOST_MASK;
930Sstevel@tonic-gate 	} else {
940Sstevel@tonic-gate 		mask = (uint32_t)strtoul(mname, &p, 0);
950Sstevel@tonic-gate 		if (*p != '\0' || mask > 32 || mname == p)
960Sstevel@tonic-gate 			return (_B_FALSE);
970Sstevel@tonic-gate 		if (mask != 0)
980Sstevel@tonic-gate 			mask = HOST_MASK << (32-mask);
990Sstevel@tonic-gate 	}
1000Sstevel@tonic-gate 
1010Sstevel@tonic-gate 	/* must have mask of 0 with default */
1020Sstevel@tonic-gate 	if (mask != 0 && in.s_addr == RIP_DEFAULT)
1030Sstevel@tonic-gate 		return (_B_FALSE);
1040Sstevel@tonic-gate 	/* no host bits allowed in a network number */
1050Sstevel@tonic-gate 	if ((~mask & in.s_addr) != 0)
1060Sstevel@tonic-gate 		return (_B_FALSE);
1070Sstevel@tonic-gate 	/* require non-zero network number */
1080Sstevel@tonic-gate 	if ((mask & in.s_addr) == 0 && in.s_addr != RIP_DEFAULT)
1090Sstevel@tonic-gate 		return (_B_FALSE);
1100Sstevel@tonic-gate 	if ((in.s_addr >> 24) == 0 && in.s_addr != RIP_DEFAULT)
1110Sstevel@tonic-gate 		return (_B_FALSE);
1120Sstevel@tonic-gate 	if ((in.s_addr >> 24) == 0xff)
1130Sstevel@tonic-gate 		return (_B_FALSE);
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	*netp = in.s_addr;
1160Sstevel@tonic-gate 	*maskp = mask;
1170Sstevel@tonic-gate 	return (_B_TRUE);
1180Sstevel@tonic-gate }
1190Sstevel@tonic-gate 
1200Sstevel@tonic-gate /*
1210Sstevel@tonic-gate  * Convert string to printable characters
1220Sstevel@tonic-gate  */
1230Sstevel@tonic-gate char *
qstring(const uchar_t * srcp,int len)1240Sstevel@tonic-gate qstring(const uchar_t *srcp, int len)
1250Sstevel@tonic-gate {
1260Sstevel@tonic-gate 	/*
1270Sstevel@tonic-gate 	 * Authentication schemes for RIPv2 uses the space of an
1280Sstevel@tonic-gate 	 * 20-octet route entry.
1290Sstevel@tonic-gate 	 */
1300Sstevel@tonic-gate 	static char buf[8*20+1];
1310Sstevel@tonic-gate 	char *prcp, *tmp_ptr;
1320Sstevel@tonic-gate 	uchar_t c;
1330Sstevel@tonic-gate 	const uchar_t *s2;
1340Sstevel@tonic-gate 
1350Sstevel@tonic-gate 	s2 = srcp + len;
1360Sstevel@tonic-gate 	while (s2 > srcp && *--s2 == '\0')
1370Sstevel@tonic-gate 		len--;
1380Sstevel@tonic-gate 	for (prcp = buf; len != 0 && prcp < &buf[sizeof (buf)-1]; len--) {
1390Sstevel@tonic-gate 		c = *srcp++;
1400Sstevel@tonic-gate 		if (isprint(c) && c != '\\') {
1410Sstevel@tonic-gate 			*prcp++ = c;
1420Sstevel@tonic-gate 			continue;
1430Sstevel@tonic-gate 		}
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate 		*prcp++ = '\\';
1460Sstevel@tonic-gate 		tmp_ptr = strchr("\\\\\nn\rr\tt\bb\aa\ff", c);
1470Sstevel@tonic-gate 		if (tmp_ptr != NULL)
1480Sstevel@tonic-gate 			*prcp++ = tmp_ptr[1];
1490Sstevel@tonic-gate 		else
1500Sstevel@tonic-gate 			prcp += snprintf(prcp,
1510Sstevel@tonic-gate 			    (sizeof (buf) - (strlen(buf)+1)), "%o", c);
1520Sstevel@tonic-gate 	}
1530Sstevel@tonic-gate 	*prcp = '\0';
1540Sstevel@tonic-gate 	return (buf);
1550Sstevel@tonic-gate }
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate /* like strtok(), but honoring backslash and not changing the source string */
1580Sstevel@tonic-gate int			/* 0=ok, -1=bad */
parse_quote(char ** linep,const char * delims,char * delimp,char * buf,int lim)1590Sstevel@tonic-gate parse_quote(char **linep,	/* look here */
1600Sstevel@tonic-gate     const char *delims,		/* for these delimiters */
1610Sstevel@tonic-gate     char *delimp,		/* 0 or put found delimiter here */
1620Sstevel@tonic-gate     char *buf,			/* copy token to here */
1630Sstevel@tonic-gate     int	lim)			/* at most this many bytes */
1640Sstevel@tonic-gate {
1650Sstevel@tonic-gate 	char c = '\0', *pc;
1660Sstevel@tonic-gate 	const char *p;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 
1690Sstevel@tonic-gate 	pc =  *linep;
1700Sstevel@tonic-gate 	if (*pc == '\0')
1710Sstevel@tonic-gate 		return (-1);
1720Sstevel@tonic-gate 
1730Sstevel@tonic-gate 	while (lim != 0) {
1740Sstevel@tonic-gate 		c = *pc++;
1750Sstevel@tonic-gate 		if (c == '\0')
1760Sstevel@tonic-gate 			break;
1770Sstevel@tonic-gate 
1780Sstevel@tonic-gate 		if (c == '\\' && *pc != '\0') {
1790Sstevel@tonic-gate 			c = *pc++;
1800Sstevel@tonic-gate 			switch (c) {
1810Sstevel@tonic-gate 			case 'n':
1820Sstevel@tonic-gate 				c = '\n';
1830Sstevel@tonic-gate 				break;
1840Sstevel@tonic-gate 			case 'r':
1850Sstevel@tonic-gate 				c = '\r';
1860Sstevel@tonic-gate 				break;
1870Sstevel@tonic-gate 			case 't':
1880Sstevel@tonic-gate 				c = '\t';
1890Sstevel@tonic-gate 				break;
1900Sstevel@tonic-gate 			case 'b':
1910Sstevel@tonic-gate 				c = '\b';
1920Sstevel@tonic-gate 			}
1930Sstevel@tonic-gate 			if (c >= '0' && c <= '7') {
1940Sstevel@tonic-gate 				c -= '0';
1950Sstevel@tonic-gate 				if (*pc >= '0' && *pc <= '7') {
1960Sstevel@tonic-gate 					c = (c<<3)+(*pc++ - '0');
1970Sstevel@tonic-gate 					if (*pc >= '0' && *pc <= '7')
1980Sstevel@tonic-gate 					    c = (c<<3)+(*pc++ - '0');
1990Sstevel@tonic-gate 				}
2000Sstevel@tonic-gate 			}
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 		} else {
2030Sstevel@tonic-gate 			for (p = delims; *p != '\0'; ++p) {
2040Sstevel@tonic-gate 				if (*p == c || isspace(c) && *p == ' ')
2050Sstevel@tonic-gate 					goto exit;
2060Sstevel@tonic-gate 			}
2070Sstevel@tonic-gate 		}
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 		*buf++ = c;
2100Sstevel@tonic-gate 		--lim;
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate exit:
2130Sstevel@tonic-gate 	if (lim == 0)
2140Sstevel@tonic-gate 		return (-1);
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate 	*buf = '\0';			/* terminate copy of token */
2170Sstevel@tonic-gate 	if (delimp != NULL)
2180Sstevel@tonic-gate 		*delimp = c;		/* return delimiter */
2190Sstevel@tonic-gate 	*linep = pc-1;			/* say where we ended */
2200Sstevel@tonic-gate 	return (0);
2210Sstevel@tonic-gate }
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate /*
2240Sstevel@tonic-gate  * Find the option buffer in the msg corresponding to cmsg_type.
2250Sstevel@tonic-gate  */
2260Sstevel@tonic-gate void *
find_ancillary(struct msghdr * msg,int cmsg_type)2270Sstevel@tonic-gate find_ancillary(struct msghdr *msg, int cmsg_type)
2280Sstevel@tonic-gate {
2290Sstevel@tonic-gate 	struct cmsghdr *cmsg;
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	for (cmsg = CMSG_FIRSTHDR(msg); cmsg != NULL;
2320Sstevel@tonic-gate 	    cmsg = CMSG_NXTHDR(msg, cmsg)) {
2330Sstevel@tonic-gate 		if (cmsg->cmsg_level == IPPROTO_IP &&
2340Sstevel@tonic-gate 		    cmsg->cmsg_type == cmsg_type) {
2350Sstevel@tonic-gate 			return (CMSG_DATA(cmsg));
2360Sstevel@tonic-gate 		}
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 	return (NULL);
2390Sstevel@tonic-gate }
240