| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 1 | /* | 
 | 2 |  *  fs/cifs/dns_resolve.c | 
 | 3 |  * | 
 | 4 |  *   Copyright (c) 2007 Igor Mammedov | 
 | 5 |  *   Author(s): Igor Mammedov (niallain@gmail.com) | 
 | 6 |  *              Steve French (sfrench@us.ibm.com) | 
 | 7 |  * | 
 | 8 |  *   Contains the CIFS DFS upcall routines used for hostname to | 
 | 9 |  *   IP address translation. | 
 | 10 |  * | 
 | 11 |  *   This library is free software; you can redistribute it and/or modify | 
 | 12 |  *   it under the terms of the GNU Lesser General Public License as published | 
 | 13 |  *   by the Free Software Foundation; either version 2.1 of the License, or | 
 | 14 |  *   (at your option) any later version. | 
 | 15 |  * | 
 | 16 |  *   This library is distributed in the hope that it will be useful, | 
 | 17 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
 | 18 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See | 
 | 19 |  *   the GNU Lesser General Public License for more details. | 
 | 20 |  * | 
 | 21 |  *   You should have received a copy of the GNU Lesser General Public License | 
 | 22 |  *   along with this library; if not, write to the Free Software | 
 | 23 |  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | 
 | 24 |  */ | 
 | 25 |  | 
 | 26 | #include <keys/user-type.h> | 
 | 27 | #include "dns_resolve.h" | 
 | 28 | #include "cifsglob.h" | 
 | 29 | #include "cifsproto.h" | 
 | 30 | #include "cifs_debug.h" | 
 | 31 |  | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 32 | /* Checks if supplied name is IP address | 
 | 33 |  * returns: | 
 | 34 |  * 		1 - name is IP | 
 | 35 |  * 		0 - name is not IP | 
 | 36 |  */ | 
| Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 37 | static int | 
 | 38 | is_ip(const char *name) | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 39 | { | 
 | 40 | 	int rc; | 
 | 41 | 	struct sockaddr_in sin_server; | 
 | 42 | 	struct sockaddr_in6 sin_server6; | 
 | 43 |  | 
 | 44 | 	rc = cifs_inet_pton(AF_INET, name, | 
 | 45 | 			&sin_server.sin_addr.s_addr); | 
 | 46 |  | 
 | 47 | 	if (rc <= 0) { | 
 | 48 | 		/* not ipv4 address, try ipv6 */ | 
 | 49 | 		rc = cifs_inet_pton(AF_INET6, name, | 
 | 50 | 				&sin_server6.sin6_addr.in6_u); | 
 | 51 | 		if (rc > 0) | 
 | 52 | 			return 1; | 
 | 53 | 	} else { | 
 | 54 | 		return 1; | 
 | 55 | 	} | 
 | 56 | 	/* we failed translating address */ | 
 | 57 | 	return 0; | 
 | 58 | } | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 59 |  | 
| Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 60 | static int | 
 | 61 | dns_resolver_instantiate(struct key *key, const void *data, | 
 | 62 | 		size_t datalen) | 
 | 63 | { | 
 | 64 | 	int rc = 0; | 
 | 65 | 	char *ip; | 
 | 66 |  | 
 | 67 | 	ip = kmalloc(datalen + 1, GFP_KERNEL); | 
 | 68 | 	if (!ip) | 
 | 69 | 		return -ENOMEM; | 
 | 70 |  | 
 | 71 | 	memcpy(ip, data, datalen); | 
 | 72 | 	ip[datalen] = '\0'; | 
 | 73 |  | 
 | 74 | 	/* make sure this looks like an address */ | 
 | 75 | 	if (!is_ip((const char *) ip)) { | 
 | 76 | 		kfree(ip); | 
 | 77 | 		return -EINVAL; | 
 | 78 | 	} | 
 | 79 |  | 
 | 80 | 	key->type_data.x[0] = datalen; | 
 | 81 | 	rcu_assign_pointer(key->payload.data, ip); | 
 | 82 |  | 
 | 83 | 	return rc; | 
 | 84 | } | 
 | 85 |  | 
 | 86 | static void | 
 | 87 | dns_resolver_destroy(struct key *key) | 
 | 88 | { | 
 | 89 | 	kfree(key->payload.data); | 
 | 90 | } | 
 | 91 |  | 
 | 92 | struct key_type key_type_dns_resolver = { | 
 | 93 | 	.name        = "dns_resolver", | 
 | 94 | 	.def_datalen = sizeof(struct in_addr), | 
 | 95 | 	.describe    = user_describe, | 
 | 96 | 	.instantiate = dns_resolver_instantiate, | 
 | 97 | 	.destroy     = dns_resolver_destroy, | 
 | 98 | 	.match       = user_match, | 
 | 99 | }; | 
 | 100 |  | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 101 | /* Resolves server name to ip address. | 
 | 102 |  * input: | 
 | 103 |  * 	unc - server UNC | 
 | 104 |  * output: | 
 | 105 |  * 	*ip_addr - pointer to server ip, caller responcible for freeing it. | 
 | 106 |  * return 0 on success | 
 | 107 |  */ | 
 | 108 | int | 
| Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 109 | dns_resolve_server_name_to_ip(const char *unc, char **ip_addr) | 
 | 110 | { | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 111 | 	int rc = -EAGAIN; | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 112 | 	struct key *rkey = ERR_PTR(-EAGAIN); | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 113 | 	char *name; | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 114 | 	char *data = NULL; | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 115 | 	int len; | 
 | 116 |  | 
| Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 117 | 	if (!ip_addr || !unc) | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 118 | 		return -EINVAL; | 
 | 119 |  | 
 | 120 | 	/* search for server name delimiter */ | 
 | 121 | 	len = strlen(unc); | 
 | 122 | 	if (len < 3) { | 
| Harvey Harrison | 55f78e1 | 2008-03-10 17:14:34 +0000 | [diff] [blame] | 123 | 		cFYI(1, ("%s: unc is too short: %s", __func__, unc)); | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 124 | 		return -EINVAL; | 
 | 125 | 	} | 
 | 126 | 	len -= 2; | 
 | 127 | 	name = memchr(unc+2, '\\', len); | 
 | 128 | 	if (!name) { | 
 | 129 | 		cFYI(1, ("%s: probably server name is whole unc: %s", | 
| Harvey Harrison | 55f78e1 | 2008-03-10 17:14:34 +0000 | [diff] [blame] | 130 | 					__func__, unc)); | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 131 | 	} else { | 
 | 132 | 		len = (name - unc) - 2/* leading // */; | 
 | 133 | 	} | 
 | 134 |  | 
 | 135 | 	name = kmalloc(len+1, GFP_KERNEL); | 
 | 136 | 	if (!name) { | 
 | 137 | 		rc = -ENOMEM; | 
 | 138 | 		return rc; | 
 | 139 | 	} | 
 | 140 | 	memcpy(name, unc+2, len); | 
 | 141 | 	name[len] = 0; | 
 | 142 |  | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 143 | 	if (is_ip(name)) { | 
 | 144 | 		cFYI(1, ("%s: it is IP, skipping dns upcall: %s", | 
 | 145 | 					__func__, name)); | 
 | 146 | 		data = name; | 
 | 147 | 		goto skip_upcall; | 
 | 148 | 	} | 
 | 149 |  | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 150 | 	rkey = request_key(&key_type_dns_resolver, name, ""); | 
 | 151 | 	if (!IS_ERR(rkey)) { | 
| Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 152 | 		len = rkey->type_data.x[0]; | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 153 | 		data = rkey->payload.data; | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 154 | 	} else { | 
 | 155 | 		cERROR(1, ("%s: unable to resolve: %s", __func__, name)); | 
 | 156 | 		goto out; | 
 | 157 | 	} | 
 | 158 |  | 
 | 159 | skip_upcall: | 
 | 160 | 	if (data) { | 
| Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 161 | 		*ip_addr = kmalloc(len + 1, GFP_KERNEL); | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 162 | 		if (*ip_addr) { | 
| Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 163 | 			memcpy(*ip_addr, data, len + 1); | 
| Igor Mammedov | 5651ced | 2008-05-20 13:02:01 +0400 | [diff] [blame] | 164 | 			if (!IS_ERR(rkey)) | 
 | 165 | 				cFYI(1, ("%s: resolved: %s to %s", __func__, | 
 | 166 | 							name, | 
 | 167 | 							*ip_addr | 
 | 168 | 					)); | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 169 | 			rc = 0; | 
 | 170 | 		} else { | 
 | 171 | 			rc = -ENOMEM; | 
 | 172 | 		} | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 173 | 		if (!IS_ERR(rkey)) | 
 | 174 | 			key_put(rkey); | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 175 | 	} | 
 | 176 |  | 
| Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 177 | out: | 
| Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 178 | 	kfree(name); | 
 | 179 | 	return rc; | 
 | 180 | } | 
 | 181 |  | 
 | 182 |  |