2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 2005, 2012, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include <unistd.h>
2N/A#include <ctype.h>
2N/A#include <netinet/in.h>
2N/A#include <libinetutil.h>
2N/A#include <inet/ip.h>
2N/A#include <strings.h>
2N/A#include <stddef.h>
2N/A#include <errno.h>
2N/A#include <libsocket_priv.h>
2N/A#include <sys/socket.h>
2N/A#include <netdb.h>
2N/A
2N/A/*
2N/A * Internet utility functions.
2N/A */
2N/A
2N/A/*
2N/A * Given a host-order address, calculate client's default net mask.
2N/A * Consult netmasks database to see if net is further subnetted.
2N/A * We'll only snag the first netmask that matches our criteria.
2N/A * We return the resultant netmask in host order.
2N/A */
2N/Avoid
2N/Aget_netmask4(const struct in_addr *n_addrp, struct in_addr *s_addrp)
2N/A{
2N/A struct in_addr hp, tp;
2N/A
2N/A /*
2N/A * First check if VLSM is in use.
2N/A */
2N/A hp.s_addr = htonl(n_addrp->s_addr);
2N/A if (getnetmaskbyaddr(hp, &tp) == 0) {
2N/A s_addrp->s_addr = ntohl(tp.s_addr);
2N/A return;
2N/A }
2N/A
2N/A /*
2N/A * Fall back on standard classed networks.
2N/A */
2N/A if (IN_CLASSA(n_addrp->s_addr))
2N/A s_addrp->s_addr = IN_CLASSA_NET;
2N/A else if (IN_CLASSB(n_addrp->s_addr))
2N/A s_addrp->s_addr = IN_CLASSB_NET;
2N/A else if (IN_CLASSC(n_addrp->s_addr))
2N/A s_addrp->s_addr = IN_CLASSC_NET;
2N/A else
2N/A s_addrp->s_addr = IN_CLASSE_NET;
2N/A}
2N/A
2N/A/*
2N/A * Checks if the IP addresses `ssp1' and `ssp2' are equal.
2N/A */
2N/Aboolean_t
2N/Asockaddrcmp(const struct sockaddr_storage *ssp1,
2N/A const struct sockaddr_storage *ssp2)
2N/A{
2N/A struct in_addr addr1, addr2;
2N/A const struct in6_addr *addr6p1, *addr6p2;
2N/A
2N/A if (ssp1->ss_family != ssp2->ss_family)
2N/A return (B_FALSE);
2N/A
2N/A if (ssp1 == ssp2)
2N/A return (B_TRUE);
2N/A
2N/A switch (ssp1->ss_family) {
2N/A case AF_INET:
2N/A addr1 = ((const struct sockaddr_in *)ssp1)->sin_addr;
2N/A addr2 = ((const struct sockaddr_in *)ssp2)->sin_addr;
2N/A return (addr1.s_addr == addr2.s_addr);
2N/A case AF_INET6:
2N/A addr6p1 = &((const struct sockaddr_in6 *)ssp1)->sin6_addr;
2N/A addr6p2 = &((const struct sockaddr_in6 *)ssp2)->sin6_addr;
2N/A return (IN6_ARE_ADDR_EQUAL(addr6p1, addr6p2));
2N/A }
2N/A return (B_FALSE);
2N/A}
2N/A
2N/A/*
2N/A * Stores the netmask in `mask' for the given prefixlen `plen' and also sets
2N/A * `sa_family' in `mask'. Because this function does not require aligned
2N/A * access to the data inside of the sockaddr_in/6 structures, the code can
2N/A * use offsetof() to find the right place in the incoming structure. Why is
2N/A * using that beneficial? Less issues with lint. When using a direct cast
2N/A * of the struct sockaddr_storage structure to sockaddr_in6, a lint warning
2N/A * is generated because the former is composed of 16bit & 8bit elements whilst
2N/A * sockaddr_in6 has a 32bit alignment requirement.
2N/A */
2N/Aint
2N/Aplen2mask(uint_t prefixlen, sa_family_t af, struct sockaddr *mask)
2N/A{
2N/A uint8_t *addr;
2N/A
2N/A if (af == AF_INET) {
2N/A if (prefixlen > IP_ABITS)
2N/A return (EINVAL);
2N/A bzero(mask, sizeof (struct sockaddr_in));
2N/A addr = (uint8_t *)mask;
2N/A addr += offsetof(struct sockaddr_in, sin_addr);
2N/A } else {
2N/A if (prefixlen > IPV6_ABITS)
2N/A return (EINVAL);
2N/A bzero(mask, sizeof (struct sockaddr_in6));
2N/A addr = (uint8_t *)mask;
2N/A addr += offsetof(struct sockaddr_in6, sin6_addr);
2N/A }
2N/A mask->sa_family = af;
2N/A
2N/A while (prefixlen > 0) {
2N/A if (prefixlen >= 8) {
2N/A *addr++ = 0xFF;
2N/A prefixlen -= 8;
2N/A continue;
2N/A }
2N/A *addr |= 1 << (8 - prefixlen);
2N/A prefixlen--;
2N/A }
2N/A return (0);
2N/A}
2N/A
2N/A/*
2N/A * Convert a mask to a prefix length.
2N/A * Returns prefix length on success, -1 otherwise.
2N/A * The comments (above) for plen2mask about the use of `mask' also apply
2N/A * to this function and the choice to use offsetof here too.
2N/A */
2N/Aint
2N/Amask2plen(const struct sockaddr *mask)
2N/A{
2N/A int rc = 0;
2N/A uint8_t last;
2N/A uint8_t *addr;
2N/A uint8_t *laddr;
2N/A int limit;
2N/A
2N/A if (mask->sa_family == AF_INET) {
2N/A limit = IP_ABITS;
2N/A addr = (uint8_t *)mask;
2N/A addr += offsetof(struct sockaddr_in, sin_addr);
2N/A } else {
2N/A limit = IPV6_ABITS;
2N/A addr = (uint8_t *)mask;
2N/A addr += offsetof(struct sockaddr_in6, sin6_addr);
2N/A }
2N/A laddr = addr + (limit / 8);
2N/A
2N/A while (*addr == 0xff) {
2N/A rc += 8;
2N/A if (rc == limit)
2N/A return (limit);
2N/A addr++;
2N/A }
2N/A
2N/A last = *addr;
2N/A if (*addr != 0)
2N/A addr++;
2N/A
2N/A while (last != 0) {
2N/A if ((last & 0x80) == 0)
2N/A return (-1);
2N/A rc++;
2N/A last = (last << 1) & 0xff;
2N/A }
2N/A
2N/A while (addr < laddr) {
2N/A if (*addr != 0)
2N/A return (-1);
2N/A addr++;
2N/A }
2N/A
2N/A return (rc);
2N/A}
2N/A
2N/A/*
2N/A * Returns B_TRUE if the address in `ss' is INADDR_ANY for IPv4 or
2N/A * :: for IPv6. Otherwise, returns B_FALSE.
2N/A */
2N/Aboolean_t
2N/Asockaddrunspec(const struct sockaddr *ss)
2N/A{
2N/A struct sockaddr_storage data;
2N/A
2N/A switch (ss->sa_family) {
2N/A case AF_INET:
2N/A (void) memcpy(&data, ss, sizeof (struct sockaddr_in));
2N/A return (((struct sockaddr_in *)&data)->sin_addr.s_addr ==
2N/A INADDR_ANY);
2N/A case AF_INET6:
2N/A (void) memcpy(&data, ss, sizeof (struct sockaddr_in6));
2N/A return (IN6_IS_ADDR_UNSPECIFIED(
2N/A &((struct sockaddr_in6 *)&data)->sin6_addr));
2N/A }
2N/A
2N/A return (B_FALSE);
2N/A}
2N/A
2N/Astatic const char *af2str[AF_MAX + 1] = {
2N/A "PF_UNSPEC",
2N/A "PF_UNIX",
2N/A "PF_INET",
2N/A "PF_IMPLINK",
2N/A "PF_PUP",
2N/A "PF_CHAOS",
2N/A "PF_NS",
2N/A "PF_NBS",
2N/A "PF_ECMA",
2N/A "PF_DATAKIT",
2N/A "PF_CCITT",
2N/A "PF_SNA",
2N/A "PF_DECnet",
2N/A "PF_DLI",
2N/A "PF_LAT",
2N/A "PF_HYLINK",
2N/A "PF_APPLETALK",
2N/A "PF_NIT",
2N/A "PF_802",
2N/A "PF_OSI",
2N/A "PF_X25",
2N/A "PF_OSINET",
2N/A "PF_GOSIP",
2N/A "PF_IPX",
2N/A "PF_ROUTE",
2N/A "PF_LINK",
2N/A "PF_INET6",
2N/A "PF_KEY",
2N/A "PF_NCA",
2N/A "PF_POLICY",
2N/A "PF_INET_OFFLOAD",
2N/A "PF_TRILL",
2N/A "PF_PACKET",
2N/A "PF_INET_SDP"
2N/A};
2N/A
2N/A#if PF_MAX != 33
2N/A#error Need to update address-family table
2N/A#endif
2N/A
2N/Aconst char *
2N/Asoaf2str(uint_t family)
2N/A{
2N/A if (family > PF_MAX)
2N/A return (NULL);
2N/A return (af2str[family]);
2N/A}
2N/A
2N/A/*
2N/A * IP protcols to string mapping -- Special exception for SDP
2N/A */
2N/Aconst char *
2N/Aipproto2str(uint_t family, uint_t protocol)
2N/A{
2N/A switch (family) {
2N/A default: return (NULL);
2N/A case AF_INET:
2N/A case AF_INET6:
2N/A case AF_NCA:
2N/A switch (protocol) {
2N/A case IPPROTO_IP: return ("IPPROTO_IP");
2N/A case IPPROTO_ICMP: return ("IPPROTO_ICMP");
2N/A case IPPROTO_IGMP: return ("IPPROTO_IGMP");
2N/A case IPPROTO_GGP: return ("IPPROTO_GGP");
2N/A case IPPROTO_ENCAP: return ("IPPROTO_ENCAP");
2N/A case IPPROTO_TCP: return ("IPPROTO_TCP");
2N/A case IPPROTO_EGP: return ("IPPROTO_EGP");
2N/A case IPPROTO_PUP: return ("IPPROTO_PUP");
2N/A case IPPROTO_UDP: return ("IPPROTO_UDP");
2N/A case IPPROTO_IDP: return ("IPPROTO_IDP");
2N/A case IPPROTO_IPV6: return ("IPPROTO_IPV6");
2N/A case IPPROTO_ROUTING: return ("IPPROTO_ROUTING");
2N/A case IPPROTO_FRAGMENT: return ("IPPROTO_FRAGMENT");
2N/A case IPPROTO_RSVP: return ("IPPROTO_RSVP");
2N/A case IPPROTO_ESP: return ("IPPROTO_ESP");
2N/A case IPPROTO_AH: return ("IPPROTO_AH");
2N/A case IPPROTO_ICMPV6: return ("IPPROTO_ICMPV6");
2N/A case IPPROTO_NONE: return ("IPPROTO_NONE");
2N/A case IPPROTO_DSTOPTS: return ("IPPROTO_DSTOPTS");
2N/A case IPPROTO_HELLO: return ("IPPROTO_HELLO");
2N/A case IPPROTO_ND: return ("IPPROTO_ND");
2N/A case IPPROTO_EON: return ("IPPROTO_EON");
2N/A case IPPROTO_OSPF: return ("IPPROTO_OSPF");
2N/A case IPPROTO_PIM: return ("IPPROTO_PIM");
2N/A case IPPROTO_SCTP: return ("IPPROTO_SCTP");
2N/A case IPPROTO_RAW: return ("IPPROTO_RAW");
2N/A case PROTO_SDP: return ("PROTO_SDP");
2N/A default: return (NULL);
2N/A }
2N/A }
2N/A}
2N/A
2N/Astatic const char *type2str[] = {
2N/A NULL,
2N/A "SOCK_DGRAM",
2N/A "SOCK_STREAM",
2N/A NULL,
2N/A "SOCK_RAW",
2N/A "SOCK_RDM",
2N/A "SOCK_SEQPACKET"
2N/A};
2N/A
2N/Aconst char *
2N/Asotype2str(uint_t type)
2N/A{
2N/A if (type > SOCK_SEQPACKET)
2N/A return (NULL);
2N/A return (type2str[type]);
2N/A}
2N/A
2N/A/*
2N/A * Validate the given hostname value according to RFC 952 and 1123. But we do
2N/A * allow the first and last char of the given hostname to have a period or
2N/A * hyphen or underscore. We allow the underscore ('_') as is often used on MS
2N/A * systems.
2N/A */
2N/Aboolean_t
2N/Avalid_hostname(const char *hname)
2N/A{
2N/A const char *ch;
2N/A
2N/A if (strlen(hname) >= MAXHOSTNAMELEN)
2N/A return (B_FALSE);
2N/A
2N/A for (ch = hname; *ch != '\0'; ch++) {
2N/A if (isalnum(*ch) || *ch == '.' || *ch == '-' || *ch == '_')
2N/A continue;
2N/A return (B_FALSE);
2N/A }
2N/A return (B_TRUE);
2N/A}
2N/A
2N/A/*
2N/A * A recursive function to generate an alphabetized string from a given
2N/A * decimal number. Decimal 1 to 26 maps to 'a' to 'z' and then the counting
2N/A * continues with 'aa', 'ab', 'ac', et al. This function is used by ipmgmtd
2N/A * and libnwam to automatically generate address object names.
2N/A */
2N/Avoid
2N/Anum2alpha(int32_t num, char **cp, char *endp)
2N/A{
2N/A if (num > 26)
2N/A num2alpha((num - 1) / 26, cp, endp);
2N/A if (*cp != endp) {
2N/A *cp[0] = num == 0 ? '\0' : ('a' + ((num - 1) % 26));
2N/A (*cp)++;
2N/A }
2N/A}