]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/libkvm/kvm_vnet.c
Remove spurious newline
[FreeBSD/FreeBSD.git] / lib / libkvm / kvm_vnet.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Robert N. M. Watson
5  * Copyright (c) 2009 Bjoern A. Zeeb <bz@FreeBSD.org>
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include <sys/param.h>
34
35 #define _WANT_PRISON
36 #define _WANT_UCRED
37 #define _WANT_VNET
38
39 #include <sys/_lock.h>
40 #include <sys/_mutex.h>
41 #include <sys/_task.h>
42 #include <sys/jail.h>
43 #include <sys/proc.h>
44 #include <sys/types.h>
45
46 #include <net/vnet.h>
47
48 #include <kvm.h>
49 #include <limits.h>
50 #include <stdlib.h>
51 #include <unistd.h>
52
53 #include "kvm_private.h"
54
55 /*
56  * Set up libkvm to handle virtual network stack symbols by selecting a
57  * starting pid.
58  */
59 int
60 _kvm_vnet_selectpid(kvm_t *kd, pid_t pid)
61 {
62         struct proc proc;
63         struct ucred cred;
64         struct prison prison;
65         struct vnet vnet;
66         struct kvm_nlist nl[] = {
67                 /*
68                  * Note: kvm_nlist strips the first '_' so add an extra one
69                  * here to __{start,stop}_set_vnet.
70                  */
71 #define NLIST_START_VNET        0
72                 { .n_name = "___start_" VNET_SETNAME },
73 #define NLIST_STOP_VNET         1
74                 { .n_name = "___stop_" VNET_SETNAME },
75 #define NLIST_VNET_HEAD         2
76                 { .n_name = "vnet_head" },
77 #define NLIST_ALLPROC           3
78                 { .n_name = "allproc" },
79 #define NLIST_DUMPTID           4
80                 { .n_name = "dumptid" },
81 #define NLIST_PROC0             5
82                 { .n_name = "proc0" },
83                 { .n_name = NULL },
84         };
85         uintptr_t procp, credp;
86 #define VMCORE_VNET_OF_PROC0
87 #ifndef VMCORE_VNET_OF_PROC0
88         struct thread td;
89         uintptr_t tdp;
90 #endif
91         lwpid_t dumptid;
92
93         /*
94          * XXX: This only works for native kernels for now.
95          */
96         if (!kvm_native(kd))
97                 return (-1);
98
99         /*
100          * Locate and cache locations of important symbols
101          * using the internal version of _kvm_nlist, turning
102          * off initialization to avoid recursion in case of
103          * unresolveable symbols.
104          */
105         if (_kvm_nlist(kd, nl, 0) != 0) {
106                 /*
107                  * XXX-BZ: ___start_/___stop_VNET_SETNAME may fail.
108                  * For now do not report an error here as we are called
109                  * internally and in `void context' until we merge the
110                  * functionality to optionally activate this into programs.
111                  * By that time we can properly fail and let the callers
112                  * handle the error.
113                  */
114                 /* _kvm_err(kd, kd->program, "%s: no namelist", __func__); */
115                 return (-1);
116         }
117
118         /*
119          * Auto-detect if this is a crashdump by reading dumptid.
120          */
121         dumptid = 0;
122         if (nl[NLIST_DUMPTID].n_value) {
123                 if (kvm_read(kd, nl[NLIST_DUMPTID].n_value, &dumptid,
124                     sizeof(dumptid)) != sizeof(dumptid)) {
125                         _kvm_err(kd, kd->program, "%s: dumptid", __func__);
126                         return (-1);
127                 }
128         }
129
130         /*
131          * First, find the process for this pid.  If we are working on a
132          * dump, either locate the thread dumptid is referring to or proc0.
133          * Based on either, take the address of the ucred.
134          */
135         credp = 0;
136
137         procp = nl[NLIST_ALLPROC].n_value;
138 #ifdef VMCORE_VNET_OF_PROC0
139         if (dumptid > 0) {
140                 procp = nl[NLIST_PROC0].n_value;
141                 pid = 0;
142         }
143 #endif
144         while (procp != 0) {
145                 if (kvm_read(kd, procp, &proc, sizeof(proc)) != sizeof(proc)) {
146                         _kvm_err(kd, kd->program, "%s: proc", __func__);
147                         return (-1);
148                 }
149 #ifndef VMCORE_VNET_OF_PROC0
150                 if (dumptid > 0) {
151                         tdp = (uintptr_t)TAILQ_FIRST(&proc.p_threads);
152                         while (tdp != 0) {
153                                 if (kvm_read(kd, tdp, &td, sizeof(td)) !=
154                                     sizeof(td)) {
155                                         _kvm_err(kd, kd->program, "%s: thread",
156                                             __func__);
157                                         return (-1);
158                                 }
159                                 if (td.td_tid == dumptid) {
160                                         credp = (uintptr_t)td.td_ucred;
161                                         break;
162                                 }
163                                 tdp = (uintptr_t)TAILQ_NEXT(&td, td_plist);
164                         }
165                 } else
166 #endif
167                 if (proc.p_pid == pid)
168                         credp = (uintptr_t)proc.p_ucred;
169                 if (credp != 0)
170                         break;
171                 procp = (uintptr_t)LIST_NEXT(&proc, p_list);
172         }
173         if (credp == 0) {
174                 _kvm_err(kd, kd->program, "%s: pid/tid not found", __func__);
175                 return (-1);
176         }
177         if (kvm_read(kd, (uintptr_t)credp, &cred, sizeof(cred)) !=
178             sizeof(cred)) {
179                 _kvm_err(kd, kd->program, "%s: cred", __func__);
180                 return (-1);
181         }
182         if (cred.cr_prison == NULL) {
183                 _kvm_err(kd, kd->program, "%s: no jail", __func__);
184                 return (-1);
185         }
186         if (kvm_read(kd, (uintptr_t)cred.cr_prison, &prison, sizeof(prison)) !=
187             sizeof(prison)) {
188                 _kvm_err(kd, kd->program, "%s: prison", __func__);
189                 return (-1);
190         }
191         if (prison.pr_vnet == NULL) {
192                 _kvm_err(kd, kd->program, "%s: no vnet", __func__);
193                 return (-1);
194         }
195         if (kvm_read(kd, (uintptr_t)prison.pr_vnet, &vnet, sizeof(vnet)) !=
196             sizeof(vnet)) {
197                 _kvm_err(kd, kd->program, "%s: vnet", __func__);
198                 return (-1);
199         }
200         if (vnet.vnet_magic_n != VNET_MAGIC_N) {
201                 _kvm_err(kd, kd->program, "%s: invalid vnet magic#", __func__);
202                 return (-1);
203         }
204         kd->vnet_initialized = 1;
205         kd->vnet_start = nl[NLIST_START_VNET].n_value;
206         kd->vnet_stop = nl[NLIST_STOP_VNET].n_value;
207         kd->vnet_current = (uintptr_t)prison.pr_vnet;
208         kd->vnet_base = vnet.vnet_data_base;
209         return (0);
210 }
211
212 /*
213  * Check whether the vnet module has been initialized successfully
214  * or not, initialize it if permitted.
215  */
216 int
217 _kvm_vnet_initialized(kvm_t *kd, int intialize)
218 {
219
220         if (kd->vnet_initialized || !intialize)
221                 return (kd->vnet_initialized);
222
223         (void) _kvm_vnet_selectpid(kd, getpid());
224
225         return (kd->vnet_initialized);
226 }
227
228 /*
229  * Check whether the value is within the vnet symbol range and
230  * only if so adjust the offset relative to the current base.
231  */
232 kvaddr_t
233 _kvm_vnet_validaddr(kvm_t *kd, kvaddr_t value)
234 {
235
236         if (value == 0)
237                 return (value);
238
239         if (!kd->vnet_initialized)
240                 return (value);
241
242         if (value < kd->vnet_start || value >= kd->vnet_stop)
243                 return (value);
244
245         return (kd->vnet_base + value);
246 }