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