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 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame^] | 26 | #include <linux/slab.h> |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 27 | #include <keys/user-type.h> |
| 28 | #include "dns_resolve.h" |
| 29 | #include "cifsglob.h" |
| 30 | #include "cifsproto.h" |
| 31 | #include "cifs_debug.h" |
| 32 | |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 33 | /* Checks if supplied name is IP address |
| 34 | * returns: |
| 35 | * 1 - name is IP |
| 36 | * 0 - name is not IP |
| 37 | */ |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 38 | static int |
Jeff Layton | 681bf72 | 2009-06-11 10:27:31 -0400 | [diff] [blame] | 39 | is_ip(char *name) |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 40 | { |
Jeff Layton | 1e68b2b | 2009-06-11 10:27:30 -0400 | [diff] [blame] | 41 | struct sockaddr_storage ss; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 42 | |
Jeff Layton | 1e68b2b | 2009-06-11 10:27:30 -0400 | [diff] [blame] | 43 | return cifs_convert_address(name, &ss); |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 44 | } |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 45 | |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 46 | static int |
| 47 | dns_resolver_instantiate(struct key *key, const void *data, |
| 48 | size_t datalen) |
| 49 | { |
| 50 | int rc = 0; |
| 51 | char *ip; |
| 52 | |
| 53 | ip = kmalloc(datalen + 1, GFP_KERNEL); |
| 54 | if (!ip) |
| 55 | return -ENOMEM; |
| 56 | |
| 57 | memcpy(ip, data, datalen); |
| 58 | ip[datalen] = '\0'; |
| 59 | |
| 60 | /* make sure this looks like an address */ |
Jeff Layton | 681bf72 | 2009-06-11 10:27:31 -0400 | [diff] [blame] | 61 | if (!is_ip(ip)) { |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 62 | kfree(ip); |
| 63 | return -EINVAL; |
| 64 | } |
| 65 | |
| 66 | key->type_data.x[0] = datalen; |
Jeff Layton | d9fb5c0 | 2009-03-23 01:47:11 -0400 | [diff] [blame] | 67 | key->payload.data = ip; |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 68 | |
| 69 | return rc; |
| 70 | } |
| 71 | |
| 72 | static void |
| 73 | dns_resolver_destroy(struct key *key) |
| 74 | { |
| 75 | kfree(key->payload.data); |
| 76 | } |
| 77 | |
| 78 | struct key_type key_type_dns_resolver = { |
| 79 | .name = "dns_resolver", |
| 80 | .def_datalen = sizeof(struct in_addr), |
| 81 | .describe = user_describe, |
| 82 | .instantiate = dns_resolver_instantiate, |
| 83 | .destroy = dns_resolver_destroy, |
| 84 | .match = user_match, |
| 85 | }; |
| 86 | |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 87 | /* Resolves server name to ip address. |
| 88 | * input: |
| 89 | * unc - server UNC |
| 90 | * output: |
| 91 | * *ip_addr - pointer to server ip, caller responcible for freeing it. |
| 92 | * return 0 on success |
| 93 | */ |
| 94 | int |
Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 95 | dns_resolve_server_name_to_ip(const char *unc, char **ip_addr) |
| 96 | { |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 97 | int rc = -EAGAIN; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 98 | struct key *rkey = ERR_PTR(-EAGAIN); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 99 | char *name; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 100 | char *data = NULL; |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 101 | int len; |
| 102 | |
Steve French | 366781c | 2008-01-25 10:12:41 +0000 | [diff] [blame] | 103 | if (!ip_addr || !unc) |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 104 | return -EINVAL; |
| 105 | |
| 106 | /* search for server name delimiter */ |
| 107 | len = strlen(unc); |
| 108 | if (len < 3) { |
Harvey Harrison | 55f78e1 | 2008-03-10 17:14:34 +0000 | [diff] [blame] | 109 | cFYI(1, ("%s: unc is too short: %s", __func__, unc)); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 110 | return -EINVAL; |
| 111 | } |
| 112 | len -= 2; |
| 113 | name = memchr(unc+2, '\\', len); |
| 114 | if (!name) { |
| 115 | cFYI(1, ("%s: probably server name is whole unc: %s", |
Harvey Harrison | 55f78e1 | 2008-03-10 17:14:34 +0000 | [diff] [blame] | 116 | __func__, unc)); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 117 | } else { |
| 118 | len = (name - unc) - 2/* leading // */; |
| 119 | } |
| 120 | |
| 121 | name = kmalloc(len+1, GFP_KERNEL); |
| 122 | if (!name) { |
| 123 | rc = -ENOMEM; |
| 124 | return rc; |
| 125 | } |
| 126 | memcpy(name, unc+2, len); |
| 127 | name[len] = 0; |
| 128 | |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 129 | if (is_ip(name)) { |
| 130 | cFYI(1, ("%s: it is IP, skipping dns upcall: %s", |
| 131 | __func__, name)); |
| 132 | data = name; |
| 133 | goto skip_upcall; |
| 134 | } |
| 135 | |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 136 | rkey = request_key(&key_type_dns_resolver, name, ""); |
| 137 | if (!IS_ERR(rkey)) { |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 138 | len = rkey->type_data.x[0]; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 139 | data = rkey->payload.data; |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 140 | } else { |
| 141 | cERROR(1, ("%s: unable to resolve: %s", __func__, name)); |
| 142 | goto out; |
| 143 | } |
| 144 | |
| 145 | skip_upcall: |
| 146 | if (data) { |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 147 | *ip_addr = kmalloc(len + 1, GFP_KERNEL); |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 148 | if (*ip_addr) { |
Steve French | 9d81523 | 2008-09-23 18:46:07 +0000 | [diff] [blame] | 149 | memcpy(*ip_addr, data, len + 1); |
Igor Mammedov | 5651ced | 2008-05-20 13:02:01 +0400 | [diff] [blame] | 150 | if (!IS_ERR(rkey)) |
| 151 | cFYI(1, ("%s: resolved: %s to %s", __func__, |
| 152 | name, |
| 153 | *ip_addr |
| 154 | )); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 155 | rc = 0; |
| 156 | } else { |
| 157 | rc = -ENOMEM; |
| 158 | } |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 159 | if (!IS_ERR(rkey)) |
| 160 | key_put(rkey); |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 161 | } |
| 162 | |
Steve French | d09e860 | 2008-04-26 00:22:23 +0000 | [diff] [blame] | 163 | out: |
Steve French | 197c183 | 2008-01-10 17:10:23 +0000 | [diff] [blame] | 164 | kfree(name); |
| 165 | return rc; |
| 166 | } |
| 167 | |
| 168 | |