iPXE
resolv.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Michael Brown <mbrown@fensystems.co.uk>.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License as
6  * published by the Free Software Foundation; either version 2 of the
7  * License, or any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  *
19  * You can also choose to distribute this program under the terms of
20  * the Unmodified Binary Distribution Licence (as given in the file
21  * COPYING.UBDL), provided that you have satisfied its requirements.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 FILE_SECBOOT ( PERMITTED );
26 
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <errno.h>
31 #include <ipxe/xfer.h>
32 #include <ipxe/open.h>
33 #include <ipxe/process.h>
34 #include <ipxe/socket.h>
35 #include <ipxe/resolv.h>
36 
37 /** @file
38  *
39  * Name resolution
40  *
41  */
42 
43 /***************************************************************************
44  *
45  * Name resolution interfaces
46  *
47  ***************************************************************************
48  */
49 
50 /**
51  * Name resolved
52  *
53  * @v intf Object interface
54  * @v sa Completed socket address (if successful)
55  */
56 void resolv_done ( struct interface *intf, struct sockaddr *sa ) {
57  struct interface *dest;
58  resolv_done_TYPE ( void * ) *op =
60  void *object = intf_object ( dest );
61 
62  DBGC ( INTF_COL ( intf ), "INTF " INTF_INTF_FMT " resolv_done\n",
63  INTF_INTF_DBG ( intf, dest ) );
64 
65  if ( op ) {
66  op ( object, sa );
67  } else {
68  /* Default is to ignore resolutions */
69  }
70 
71  intf_put ( dest );
72 }
73 
74 /***************************************************************************
75  *
76  * Numeric name resolver
77  *
78  ***************************************************************************
79  */
80 
81 /** A numeric name resolver */
83  /** Reference counter */
84  struct refcnt refcnt;
85  /** Name resolution interface */
86  struct interface resolv;
87  /** Process */
88  struct process process;
89  /** Completed socket address */
90  struct sockaddr sa;
91  /** Overall status code */
92  int rc;
93 };
94 
95 static void numeric_step ( struct numeric_resolv *numeric ) {
96 
97  if ( numeric->rc == 0 )
98  resolv_done ( &numeric->resolv, &numeric->sa );
99  intf_shutdown ( &numeric->resolv, numeric->rc );
100 }
101 
104 
105 static int numeric_resolv ( struct interface *resolv,
106  const char *name, struct sockaddr *sa ) {
107  struct numeric_resolv *numeric;
108 
109  /* Allocate and initialise structure */
110  numeric = zalloc ( sizeof ( *numeric ) );
111  if ( ! numeric )
112  return -ENOMEM;
113  ref_init ( &numeric->refcnt, NULL );
114  intf_init ( &numeric->resolv, &null_intf_desc, &numeric->refcnt );
116  &numeric->refcnt );
117  memcpy ( &numeric->sa, sa, sizeof ( numeric->sa ) );
118 
119  /* Attempt to resolve name */
120  numeric->rc = sock_aton ( name, &numeric->sa );
121 
122  /* Attach to parent interface, mortalise self, and return */
123  intf_plug_plug ( &numeric->resolv, resolv );
124  ref_put ( &numeric->refcnt );
125  return 0;
126 }
127 
128 struct resolver numeric_resolver __resolver ( RESOLV_NUMERIC ) = {
129  .name = "NUMERIC",
130  .resolv = numeric_resolv,
131 };
132 
133 /***************************************************************************
134  *
135  * Name resolution multiplexer
136  *
137  ***************************************************************************
138  */
139 
140 /** A name resolution multiplexer */
141 struct resolv_mux {
142  /** Reference counter */
143  struct refcnt refcnt;
144  /** Parent name resolution interface */
146 
147  /** Child name resolution interface */
148  struct interface child;
149  /** Current child resolver */
151 
152  /** Socket address to complete */
153  struct sockaddr sa;
154  /** Name to be resolved
155  *
156  * Must be at end of structure
157  */
158  char name[0];
159 };
160 
161 /**
162  * Try current child name resolver
163  *
164  * @v mux Name resolution multiplexer
165  * @ret rc Return status code
166  */
167 static int resmux_try ( struct resolv_mux *mux ) {
168  struct resolver *resolver = mux->resolver;
169  int rc;
170 
171  DBGC ( mux, "RESOLV %p trying method %s\n", mux, resolver->name );
172 
173  if ( ( rc = resolver->resolv ( &mux->child, mux->name,
174  &mux->sa ) ) != 0 ) {
175  DBGC ( mux, "RESOLV %p could not use method %s: %s\n",
176  mux, resolver->name, strerror ( rc ) );
177  return rc;
178  }
179 
180  return 0;
181 }
182 
183 /**
184  * Close name resolution multiplexer
185  *
186  * @v mux Name resolution multiplexer
187  * @v rc Reason for close
188  */
189 static void resmux_close ( struct resolv_mux *mux, int rc ) {
190 
191  /* Shut down all interfaces */
192  intf_shutdown ( &mux->child, rc );
193  intf_shutdown ( &mux->parent, rc );
194 }
195 
196 /**
197  * Child finished resolution
198  *
199  * @v mux Name resolution multiplexer
200  * @v rc Return status code
201  */
202 static void resmux_child_close ( struct resolv_mux *mux, int rc ) {
203 
204  /* Restart child interface */
205  intf_restart ( &mux->child, rc );
206 
207  /* If this resolution succeeded, stop now */
208  if ( rc == 0 ) {
209  DBGC ( mux, "RESOLV %p succeeded using method %s\n",
210  mux, mux->resolver->name );
211  goto finished;
212  }
213 
214  /* Attempt next child resolver, if possible */
215  mux->resolver++;
216  if ( mux->resolver >= table_end ( RESOLVERS ) ) {
217  DBGC ( mux, "RESOLV %p failed to resolve name\n", mux );
218  goto finished;
219  }
220  if ( ( rc = resmux_try ( mux ) ) != 0 )
221  goto finished;
222 
223  /* Next resolver is now running */
224  return;
225 
226  finished:
227  resmux_close ( mux, rc );
228 }
229 
230 /** Name resolution multiplexer child interface operations */
233 };
234 
235 /** Name resolution multiplexer child interface descriptor */
238  parent );
239 
240 /** Name resolution multiplexer parent interface operations */
242  INTF_OP ( intf_close, struct resolv_mux *, resmux_close ),
243 };
244 
245 /** Name resolution multiplexer parent interface descriptor */
248  child );
249 
250 /**
251  * Start name resolution
252  *
253  * @v resolv Name resolution interface
254  * @v name Name to resolve
255  * @v sa Socket address to complete
256  * @ret rc Return status code
257  */
258 int resolv ( struct interface *resolv, const char *name,
259  struct sockaddr *sa ) {
260  struct resolv_mux *mux;
261  size_t name_len = ( strlen ( name ) + 1 );
262  int rc;
263 
264  /* Allocate and initialise structure */
265  mux = zalloc ( sizeof ( *mux ) + name_len );
266  if ( ! mux )
267  return -ENOMEM;
268  ref_init ( &mux->refcnt, NULL );
269  intf_init ( &mux->parent, &resmux_parent_desc, &mux->refcnt );
270  intf_init ( &mux->child, &resmux_child_desc, &mux->refcnt );
271  mux->resolver = table_start ( RESOLVERS );
272  if ( sa )
273  memcpy ( &mux->sa, sa, sizeof ( mux->sa ) );
274  memcpy ( mux->name, name, name_len );
275 
276  DBGC ( mux, "RESOLV %p attempting to resolve \"%s\"\n", mux, name );
277 
278  /* Start first resolver in chain. There will always be at
279  * least one resolver (the numeric resolver), so no need to
280  * check for the zero-resolvers-available case.
281  */
282  if ( ( rc = resmux_try ( mux ) ) != 0 )
283  goto err;
284 
285  /* Attach parent interface, mortalise self, and return */
286  intf_plug_plug ( &mux->parent, resolv );
287  ref_put ( &mux->refcnt );
288  return 0;
289 
290  err:
291  ref_put ( &mux->refcnt );
292  return rc;
293 }
294 
295 /***************************************************************************
296  *
297  * Named socket opening
298  *
299  ***************************************************************************
300  */
301 
302 /** A named socket */
303 struct named_socket {
304  /** Reference counter */
305  struct refcnt refcnt;
306  /** Data transfer interface */
307  struct interface xfer;
308  /** Name resolution interface */
310  /** Communication semantics (e.g. SOCK_STREAM) */
312  /** Stored local socket address, if applicable */
313  struct sockaddr local;
314  /** Stored local socket address exists */
316 };
317 
318 /**
319  * Terminate named socket opener
320  *
321  * @v named Named socket
322  * @v rc Reason for termination
323  */
324 static void named_close ( struct named_socket *named, int rc ) {
325  /* Shut down interfaces */
326  intf_shutdown ( &named->resolv, rc );
327  intf_shutdown ( &named->xfer, rc );
328 }
329 
330 /**
331  * Check flow control window
332  *
333  * @v named Named socket
334  * @ret len Length of window
335  */
336 static size_t named_window ( struct named_socket *named __unused ) {
337  /* Not ready for data until we have redirected away */
338  return 0;
339 }
340 
341 /** Named socket opener data transfer interface operations */
345 };
346 
347 /** Named socket opener data transfer interface descriptor */
350  resolv );
351 
352 /**
353  * Name resolved
354  *
355  * @v named Named socket
356  * @v sa Completed socket address
357  */
358 static void named_resolv_done ( struct named_socket *named,
359  struct sockaddr *sa ) {
360  int rc;
361 
362  /* Nullify data transfer interface */
363  intf_nullify ( &named->xfer );
364 
365  /* Redirect data-xfer interface */
366  if ( ( rc = xfer_redirect ( &named->xfer, LOCATION_SOCKET,
367  named->semantics, sa,
368  ( named->have_local ?
369  &named->local : NULL ) ) ) != 0 ) {
370  /* Redirection failed - do not unplug data-xfer interface */
371  DBGC ( named, "NAMED %p could not redirect: %s\n",
372  named, strerror ( rc ) );
373  } else {
374  /* Redirection succeeded - unplug data-xfer interface */
375  DBGC ( named, "NAMED %p redirected successfully\n", named );
376  intf_unplug ( &named->xfer );
377  }
378 
379  /* Terminate named socket opener */
380  named_close ( named, rc );
381 }
382 
383 /** Named socket opener resolver interface operations */
387 };
388 
389 /** Named socket opener resolver interface descriptor */
392  xfer );
393 
394 /**
395  * Open named socket
396  *
397  * @v semantics Communication semantics (e.g. SOCK_STREAM)
398  * @v peer Peer socket address to complete
399  * @v name Name to resolve
400  * @v local Local socket address, or NULL
401  * @ret rc Return status code
402  */
403 int xfer_open_named_socket ( struct interface *xfer, int semantics,
404  struct sockaddr *peer, const char *name,
405  struct sockaddr *local ) {
406  struct named_socket *named;
407  int rc;
408 
409  /* Allocate and initialise structure */
410  named = zalloc ( sizeof ( *named ) );
411  if ( ! named )
412  return -ENOMEM;
413  ref_init ( &named->refcnt, NULL );
414  intf_init ( &named->xfer, &named_xfer_desc, &named->refcnt );
415  intf_init ( &named->resolv, &named_resolv_desc, &named->refcnt );
416  named->semantics = semantics;
417  if ( local ) {
418  memcpy ( &named->local, local, sizeof ( named->local ) );
419  named->have_local = 1;
420  }
421 
422  DBGC ( named, "NAMED %p opening \"%s\"\n",
423  named, name );
424 
425  /* Start name resolution */
426  if ( ( rc = resolv ( &named->resolv, name, peer ) ) != 0 )
427  goto err;
428 
429  /* Attach parent interface, mortalise self, and return */
430  intf_plug_plug ( &named->xfer, xfer );
431  ref_put ( &named->refcnt );
432  return 0;
433 
434  err:
435  ref_put ( &named->refcnt );
436  return rc;
437 }
static void named_resolv_done(struct named_socket *named, struct sockaddr *sa)
Name resolved.
Definition: resolv.c:358
A process.
Definition: process.h:18
An object interface operation.
Definition: interface.h:18
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
const char * name
Definition: ath9k_hw.c:1986
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:250
#define INTF_INTF_FMT
printf() format string for INTF_INTF_DBG()
Definition: interface.h:298
A numeric name resolver.
Definition: resolv.c:82
void intf_restart(struct interface *intf, int rc)
Shut down and restart an object interface.
Definition: interface.c:344
struct interface xfer
Data transfer interface.
Definition: resolv.c:307
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:279
FILE_SECBOOT(PERMITTED)
static struct interface_operation named_xfer_ops[]
Named socket opener data transfer interface operations.
Definition: resolv.c:342
static void resmux_close(struct resolv_mux *mux, int rc)
Close name resolution multiplexer.
Definition: resolv.c:189
struct resolver * resolver
Current child resolver.
Definition: resolv.c:150
struct interface_descriptor null_intf_desc
Null interface descriptor.
Definition: interface.c:62
#define table_start(table)
Get start of linker table.
Definition: tables.h:283
static struct interface_operation resmux_child_op[]
Name resolution multiplexer child interface operations.
Definition: resolv.c:231
int semantics
Communication semantics (e.g.
Definition: resolv.c:311
void resolv_done(struct interface *intf, struct sockaddr *sa)
Name resolved.
Definition: resolv.c:56
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:65
Error codes.
struct refcnt refcnt
Reference counter.
Definition: resolv.c:305
int have_local
Stored local socket address exists.
Definition: resolv.c:315
static size_t named_window(struct named_socket *named __unused)
Check flow control window.
Definition: resolv.c:336
Name resolution.
#define INTF_INTF_DBG(intf, dest)
printf() arguments for representing an object interface pair
Definition: interface.h:307
struct interface resolv
Name resolution interface.
Definition: resolv.c:86
static void process_init(struct process *process, struct process_descriptor *desc, struct refcnt *refcnt)
Initialise process and add to process list.
Definition: process.h:162
#define INTF_COL(intf)
Find debugging colourisation for an object interface.
Definition: interface.h:282
#define DBGC(...)
Definition: compiler.h:505
A process descriptor.
Definition: process.h:32
int rc
Overall status code.
Definition: resolv.c:92
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:108
A name resolution multiplexer.
Definition: resolv.c:141
#define PROC_DESC_ONCE(object_type, process, _step)
Define a process descriptor for a process that runs only once.
Definition: process.h:98
struct refcnt refcnt
Reference counter.
Definition: resolv.c:84
int sock_aton(const char *string, struct sockaddr *sa)
Parse socket address.
Definition: socket.c:60
struct sockaddr sa
Socket address to complete.
Definition: resolv.c:153
struct interface parent
Parent name resolution interface.
Definition: resolv.c:145
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:117
void * intf_object(struct interface *intf)
Get pointer to object containing object interface.
Definition: interface.c:160
static int resmux_try(struct resolv_mux *mux)
Try current child name resolver.
Definition: resolv.c:167
Data transfer interfaces.
struct interface * intf
Original interface.
Definition: interface.h:159
A reference counter.
Definition: refcnt.h:27
Location is a socket.
Definition: open.h:44
static struct interface_descriptor named_resolv_desc
Named socket opener resolver interface descriptor.
Definition: resolv.c:390
#define ENOMEM
Not enough space.
Definition: errno.h:535
void * memcpy(void *dest, const void *src, size_t len) __nonnull
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char name[0]
Name to be resolved.
Definition: resolv.c:158
An object interface.
Definition: interface.h:125
struct sockaddr local
Stored local socket address, if applicable.
Definition: resolv.c:313
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
A named socket.
Definition: resolv.c:303
struct sockaddr sa
Definition: syslog.c:57
static int numeric_resolv(struct interface *resolv, const char *name, struct sockaddr *sa)
Definition: resolv.c:105
void intf_unplug(struct interface *intf)
Unplug an object interface.
Definition: interface.c:118
Generalized socket address structure.
Definition: socket.h:97
An object interface descriptor.
Definition: interface.h:56
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:79
int(* resolv)(struct interface *resolv, const char *name, struct sockaddr *sa)
Start name resolution.
Definition: resolv.h:29
static void numeric_step(struct numeric_resolv *numeric)
Definition: resolv.c:95
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:662
static struct interface_operation resmux_parent_op[]
Name resolution multiplexer parent interface operations.
Definition: resolv.c:241
static struct interface_descriptor resmux_child_desc
Name resolution multiplexer child interface descriptor.
Definition: resolv.c:236
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:33
size_t strlen(const char *src)
Get length of string.
Definition: string.c:244
Processes.
Data transfer interface opening.
struct process process
Process.
Definition: resolv.c:88
static struct interface_operation named_resolv_op[]
Named socket opener resolver interface operations.
Definition: resolv.c:384
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
struct refcnt refcnt
Reference counter.
Definition: resolv.c:143
void intf_nullify(struct interface *intf)
Ignore all further operations on an object interface.
Definition: interface.c:130
A name resolver.
Definition: resolv.h:19
void intf_put(struct interface *intf)
Decrement reference count on an object interface.
Definition: interface.c:150
if(len >=6 *4) __asm__ __volatile__("movsl" if(len >=5 *4) __asm__ __volatile__("movsl" if(len >=4 *4) __asm__ __volatile__("movsl" if(len >=3 *4) __asm__ __volatile__("movsl" if(len >=2 *4) __asm__ __volatile__("movsl" if(len >=1 *4) __asm__ __volatile__("movsl" if((len % 4) >=2) __asm__ __volatile__("movsw" if((len % 2) >=1) __asm__ __volatile__("movsb" return dest
Definition: string.h:151
static void resmux_child_close(struct resolv_mux *mux, int rc)
Child finished resolution.
Definition: resolv.c:202
const char * name
Name of this resolver (e.g.
Definition: resolv.h:21
struct interface resolv
Name resolution interface.
Definition: resolv.c:309
#define table_end(table)
Get end of linker table.
Definition: tables.h:309
int resolv(struct interface *resolv, const char *name, struct sockaddr *sa)
Start name resolution.
Definition: resolv.c:258
#define INTF_DESC_PASSTHRU(object_type, intf, operations, passthru)
Define an object interface descriptor with pass-through interface.
Definition: interface.h:98
static struct interface_descriptor resmux_parent_desc
Name resolution multiplexer parent interface descriptor.
Definition: resolv.c:246
struct resolver numeric_resolver __resolver(RESOLV_NUMERIC)
static struct process_descriptor numeric_process_desc
Definition: resolv.c:102
struct mschapv2_challenge peer
Peer challenge.
Definition: mschapv2.h:12
static struct interface_descriptor named_xfer_desc
Named socket opener data transfer interface descriptor.
Definition: resolv.c:348
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:204
struct interface child
Child name resolution interface.
Definition: resolv.c:148
Socket addresses.
static void named_close(struct named_socket *named, int rc)
Terminate named socket opener.
Definition: resolv.c:324
#define resolv_done_TYPE(object_type)
Definition: resolv.h:46
#define RESOLV_NUMERIC
Numeric resolver priority.
Definition: resolv.h:34
#define NULL
NULL pointer (VOID *)
Definition: Base.h:322
String functions.
#define intf_get_dest_op(intf, type, dest)
Get object interface destination and operation method.
Definition: interface.h:270
#define RESOLVERS
Resolvers table.
Definition: resolv.h:40
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition: resolv.c:403
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:107
struct sockaddr sa
Completed socket address.
Definition: resolv.c:90
int xfer_redirect(struct interface *intf, int type,...)
Send redirection event.
Definition: xfer.c:239