]> git.ozlabs.org Git - petitboot/blob - discover/pxe-parser.c
discover: Wait for net interfaces to be marked ready
[petitboot] / discover / pxe-parser.c
1
2 #if defined(HAVE_CONFIG_H)
3 #include "config.h"
4 #endif
5 #include <stdlib.h>
6 #include <string.h>
7
8 #include <talloc/talloc.h>
9 #include <url/url.h>
10 #include <log/log.h>
11 #include <file/file.h>
12 #include <i18n/i18n.h>
13
14 #include "device-handler.h"
15 #include "parser.h"
16 #include "parser-conf.h"
17 #include "parser-utils.h"
18 #include "resource.h"
19 #include "paths.h"
20 #include "event.h"
21 #include "user-event.h"
22 #include "network.h"
23
24 static const char *pxelinux_prefix = "pxelinux.cfg/";
25
26 static void pxe_conf_parse_cb(struct load_url_result *result, void *data);
27
28 struct pxe_parser_info {
29         struct discover_boot_option     *opt;
30         const char                      *default_name;
31         char                            **pxe_conf_files;
32         struct pb_url                   *pxe_base_url;
33         int                             current;
34 };
35
36 static void pxe_finish(struct conf_context *conf)
37 {
38         struct pxe_parser_info *info = conf->parser_info;
39         if (info->opt)
40                 discover_context_add_boot_option(conf->dc, info->opt);
41 }
42
43 /* We need a slightly modified version of pb_url_join, to allow for the
44  * pxelinux "::filename" syntax for absolute URLs
45  */
46 static struct pb_url *pxe_url_join(void *ctx, const struct pb_url *url,
47                 const char *s)
48 {
49         struct pb_url *new_url;
50         int len;
51
52         len = strlen(s);
53
54         if (len > 2 && s[0] == ':' && s[1] == ':') {
55                 char *tmp;
56
57                 if (s[2] == '/') {
58                         /* ::/path -> /path */
59                         tmp = talloc_strdup(ctx, s+2);
60                 } else {
61                         /* ::path -> /path */
62                         tmp = talloc_strdup(ctx, s+1);
63                         tmp[0] = '/';
64                 }
65
66                 new_url = pb_url_join(ctx, url, tmp);
67
68                 talloc_free(tmp);
69
70         } else {
71                 const char *tmp;
72                 /* strip leading slashes */
73                 for (tmp = s; *tmp == '/'; tmp++)
74                         ;
75                 new_url = pb_url_join(ctx, url, tmp);
76         }
77
78         return new_url;
79 }
80
81 static void pxe_append_string(struct discover_boot_option *opt,
82                 const char *str)
83 {
84         if (opt->option->boot_args)
85                 opt->option->boot_args = talloc_asprintf_append(
86                                 opt->option->boot_args, " %s", str);
87         else
88                 opt->option->boot_args = talloc_strdup(opt->option, str);
89 }
90
91 static char *pxe_sysappend_mac(void *ctx, uint8_t *mac)
92 {
93         if (!mac)
94                 return NULL;
95
96         return talloc_asprintf(ctx,
97                 "BOOTIF=01-%02x-%02x-%02x-%02x-%02x-%02x",
98                 mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
99 }
100
101 static void pxe_process_sysappend(struct discover_context *ctx,
102                 struct discover_boot_option *opt,
103                 unsigned long val)
104 {
105         struct event *event = ctx->event;
106         char *str = NULL;
107
108         if (!event)
109                 return;
110
111         if (val & 0x2) {
112                 uint8_t *mac = find_mac_by_name(ctx,
113                                 device_handler_get_network(ctx->handler),
114                                 event->device);
115                 str = pxe_sysappend_mac(ctx, mac);
116                 if (str) {
117                         pxe_append_string(opt, str);
118                         talloc_free(str);
119                 }
120                 val &= ~0x2;
121         }
122
123         if (val)
124                 pb_log("pxe: unsupported features requested in "
125                                 "ipappend/sysappend: 0x%04lx", val);
126
127 }
128
129 static void pxe_process_pair(struct conf_context *ctx,
130                 const char *name, char *value)
131 {
132         struct pxe_parser_info *parser_info = ctx->parser_info;
133         struct discover_boot_option *opt = parser_info->opt;
134         struct pb_url *url;
135
136         /* quirk in the syslinux config format: initrd can be separated
137          * by an '=' */
138         if (!name && !strncasecmp(value, "initrd=", strlen("initrd="))) {
139                 name = "initrd";
140                 value += strlen("initrd=");
141         }
142
143         if (!name)
144                 return;
145
146         if (streq(name, "DEFAULT")) {
147                 parser_info->default_name = talloc_strdup(parser_info, value);
148                 return;
149         }
150
151         if (streq(name, "LABEL")) {
152                 if (opt)
153                         pxe_finish(ctx);
154
155                 opt = discover_boot_option_create(ctx->dc, ctx->dc->device);
156
157                 opt->option->name = talloc_strdup(opt, value);
158                 opt->option->id = talloc_asprintf(opt, "%s@%p",
159                                 ctx->dc->device->device->id, opt);
160
161                 opt->option->is_default = parser_info->default_name &&
162                                         streq(parser_info->default_name, value);
163
164                 parser_info->opt = opt;
165                 return;
166         }
167
168         /* all other parameters need an option */
169         if (!opt)
170                 return;
171
172         if (streq(name, "KERNEL")) {
173                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
174                 opt->boot_image = create_url_resource(opt, url);
175
176                 char* args_sigfile_default = talloc_asprintf(opt,
177                         "%s.cmdline.sig", value);
178                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url,
179                         args_sigfile_default);
180                 opt->args_sig_file = create_url_resource(opt, url);
181                 talloc_free(args_sigfile_default);
182
183         } else if (streq(name, "INITRD")) {
184                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
185                 opt->initrd = create_url_resource(opt, url);
186
187         } else if (streq(name, "APPEND")) {
188                 char *str, *end;
189
190                 pxe_append_string(opt, value);
191
192                 str = strcasestr(value, "INITRD=");
193                 if (str) {
194                         str += strlen("INITRD=");
195                         end = strchrnul(str, ' ');
196                         *end = '\0';
197
198                         url = pxe_url_join(ctx->dc, ctx->dc->conf_url, str);
199                         opt->initrd = create_url_resource(opt, url);
200                 }
201         } else if (streq(name, "SYSAPPEND") || streq(name, "IPAPPEND")) {
202                 unsigned long type;
203                 char *end;
204
205                 type = strtoul(value, &end, 10);
206                 if (end != value && !(*end))
207                         pxe_process_sysappend(ctx->dc, opt, type);
208
209         } else if (streq(name, "DTB") || streq(name, "FDT")) {
210                 url = pxe_url_join(ctx->dc, ctx->dc->conf_url, value);
211                 opt->dtb = create_url_resource(opt, url);
212         }
213
214 }
215
216 static void pxe_load_next_filename(struct conf_context *conf)
217 {
218         struct pxe_parser_info *info = conf->parser_info;
219         struct pb_url *url;
220
221         if (!info->pxe_conf_files)
222                 return;
223
224         for (; info->pxe_conf_files[info->current]; info->current++) {
225                 url = pb_url_join(conf->dc, info->pxe_base_url,
226                                   info->pxe_conf_files[info->current]);
227                 if (!url)
228                         continue;
229
230                 if (load_url_async(conf, url, pxe_conf_parse_cb, conf,
231                                    NULL, NULL))
232                         break;
233         }
234
235         return;
236 }
237
238 /*
239  * Callback for asynchronous loads from pxe_parse()
240  * @param result Result of load_url_async()
241  * @param data   Pointer to associated conf_context
242  */
243 static void pxe_conf_parse_cb(struct load_url_result *result, void *data)
244 {
245         struct conf_context *conf = data;
246         struct device_handler *handler;
247         struct pxe_parser_info *info;
248         char *buf = NULL;
249         int len, rc = 0;
250
251         if (!data)
252                 return;
253
254         handler = talloc_parent(conf);
255
256         if (result && result->status == LOAD_OK)
257                 rc = read_file(conf, result->local, &buf, &len);
258         if (!result || result->status != LOAD_OK || rc) {
259                 /* This load failed so try the next available filename */
260                 info = conf->parser_info;
261                 if (!info->pxe_conf_files) {
262                         device_handler_status_dev_err(handler,
263                                         conf->dc->device,
264                                         _("Failed to download %s"),
265                                         pb_url_to_string(result->url));
266
267                         return;
268                 }
269
270                 info->current++;
271                 pxe_load_next_filename(conf);
272                 if (info->pxe_conf_files[info->current] == NULL) {
273                         /* Nothing left to try */
274                         device_handler_status_dev_err(handler,
275                                         conf->dc->device,
276                                         _("PXE autoconfiguration failed"));
277                         goto out_clean;
278                 }
279                 return;
280         }
281
282         /*
283          * Parse the first successfully downloaded file. We only want to parse
284          * the first because otherwise we could parse options from both a
285          * machine-specific config and a 'fallback' default config
286          */
287
288         conf_parse_buf(conf, buf, len);
289
290         /* We may be called well after the original caller of iterate_parsers(),
291          * commit any new boot options ourselves */
292         device_handler_discover_context_commit(handler, conf->dc);
293
294         /*
295          * TRANSLATORS: the format specifier in this string is a URL
296          * eg. tftp://192.168.1.1/pxelinux.cfg
297          */
298         device_handler_status_dev_info(handler, conf->dc->device,
299                         _("Parsed PXE config from %s"),
300                         pb_url_to_string(result->url));
301
302         talloc_free(buf);
303 out_clean:
304         if (result->cleanup_local)
305                 unlink(result->local);
306         talloc_free(conf);
307 }
308
309 /**
310  * Return a new conf_context and increment the talloc reference count on
311  * the discover_context struct.
312  * @param  ctx  Parent talloc context
313  * @param  orig Original discover_context
314  * @return      Pointer to new conf_context
315  */
316 static struct conf_context *copy_context(void *ctx, struct discover_context *dc)
317 {
318         struct pxe_parser_info *info;
319         struct conf_context *conf;
320
321         conf = talloc_zero(ctx, struct conf_context);
322
323         if (!conf)
324                 return NULL;
325
326         conf->get_pair = conf_get_pair_space;
327         conf->process_pair = pxe_process_pair;
328         conf->finish = pxe_finish;
329         info = talloc_zero(conf, struct pxe_parser_info);
330         if (!info) {
331                 talloc_free(conf);
332                 return NULL;
333         }
334         conf->parser_info = info;
335
336         /*
337          * The discover_context may be freed once pxe_parse() returns, but the
338          * callback will still need it. Take a reference so that that it will
339          * persist until the last callback completes.
340          */
341         conf->dc = talloc_reference(conf, dc);
342
343         return conf;
344 }
345
346 static int pxe_parse(struct discover_context *dc)
347 {
348         struct pb_url *pxe_base_url;
349         struct conf_context *conf = NULL;
350         struct load_url_result *result;
351         void *ctx = talloc_parent(dc);
352         struct pxe_parser_info *info;
353         char **pxe_conf_files;
354         bool complete_url;
355
356         /* Expects dhcp event parameters to support network boot */
357         if (!dc->event)
358                 return -1;
359
360         dc->conf_url = user_event_parse_conf_url(dc, dc->event, &complete_url);
361         if (!dc->conf_url)
362                 return -1;
363
364         /*
365          * Retrieving PXE configs over the network can take some time depending
366          * on factors such as slow network, malformed paths, bad DNS, and
367          * overzealous firewalls. Instead of blocking the discover server while
368          * we wait for these, spawn an asynchronous job that will attempt to
369          * retrieve each possible URL until it successfully finds one, and
370          * parse and process the resulting file in a callback.
371          */
372         conf = copy_context(ctx, dc);
373         if (!conf)
374                 return -1;
375
376         if (complete_url) {
377                 device_handler_status_dev_info(conf->dc->handler,
378                         dc->device,
379                         _("Requesting config %s"),
380                         pb_url_to_string(conf->dc->conf_url));
381
382                 /* we have a complete URL; use this and we're done. */
383                 result = load_url_async(conf->dc, conf->dc->conf_url,
384                                         pxe_conf_parse_cb, conf, NULL, ctx);
385                 if (!result) {
386                         pb_log("load_url_async fails for %s\n",
387                                         dc->conf_url->path);
388                         goto out_conf;
389                 } else if (result->status == LOAD_OK) {
390                         /* Local load - call pxe_conf_parse_cb() now */
391                         pxe_conf_parse_cb(result, conf);
392                 }
393         } else {
394                 pxe_conf_files = user_event_parse_conf_filenames(dc, dc->event);
395                 if (!pxe_conf_files)
396                         goto out_conf;
397
398                 pxe_base_url = pb_url_join(dc, dc->conf_url, pxelinux_prefix);
399                 if (!pxe_base_url)
400                         goto out_pxe_conf;
401
402                 info = conf->parser_info;
403                 info->pxe_conf_files = pxe_conf_files;
404                 info->pxe_base_url = pxe_base_url;
405
406                 device_handler_status_dev_info(conf->dc->handler,
407                         conf->dc->device,
408                         _("Probing from base %s"),
409                         pb_url_to_string(pxe_base_url));
410
411                 pxe_load_next_filename(conf);
412         }
413
414         return 0;
415
416 out_pxe_conf:
417         talloc_free(pxe_conf_files);
418 out_conf:
419         talloc_free(conf);
420         return -1;
421 }
422
423 static struct parser pxe_parser = {
424         .name                   = "pxe",
425         .parse                  = pxe_parse,
426 };
427
428 register_parser(pxe_parser);