]> git.ozlabs.org Git - petitboot/blob - discover/user-event.c
config/powerpc: Always call update_network_config
[petitboot] / discover / user-event.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <sys/socket.h>
27 #include <sys/types.h>
28 #include <sys/un.h>
29
30 #include <log/log.h>
31 #include <url/url.h>
32 #include <types/types.h>
33 #include <talloc/talloc.h>
34 #include <waiter/waiter.h>
35
36 #include "device-handler.h"
37 #include "resource.h"
38 #include "event.h"
39 #include "user-event.h"
40
41
42 #define MAC_ADDR_SIZE   6
43 #define IP_ADDR_SIZE    4
44
45 struct user_event {
46         struct device_handler *handler;
47         int socket;
48 };
49
50 static const char *event_action_name(enum event_action action)
51 {
52         switch (action) {
53         case EVENT_ACTION_ADD:
54                 return "add";
55         case EVENT_ACTION_REMOVE:
56                 return "remove";
57         case EVENT_ACTION_DHCP:
58                 return "dhcp";
59         default:
60                 break;
61         }
62
63         return "unknown";
64 }
65
66 static void user_event_print_event(struct event __attribute__((unused)) *event)
67 {
68         int i;
69
70         pb_debug("user_event %s event:\n", event_action_name(event->action));
71         pb_debug("\tdevice: %s\n", event->device);
72
73         for (i = 0; i < event->n_params; i++)
74                 pb_debug("\t%-12s => %s\n",
75                         event->params[i].name, event->params[i].value);
76 }
77
78 static struct resource *user_event_resource(struct discover_boot_option *opt,
79                 struct event *event)
80 {
81         const char *siaddr, *boot_file;
82         struct resource *res;
83         struct pb_url *url;
84         char *url_str;
85
86         siaddr = event_get_param(event, "siaddr");
87         if (!siaddr) {
88                 pb_log("%s: next server option not found\n", __func__);
89                 return NULL;
90         }
91
92         boot_file = event_get_param(event, "boot_file");
93         if (!boot_file) {
94                 pb_log("%s: boot_file not found\n", __func__);
95                 return NULL;
96         }
97
98         url_str = talloc_asprintf(opt, "%s%s/%s", "tftp://", siaddr, boot_file);
99         url = pb_url_parse(opt, url_str);
100         talloc_free(url_str);
101
102         if (!url)
103                 return NULL;
104
105         res = create_url_resource(opt, url);
106         if (!res) {
107                 talloc_free(url);
108                 return NULL;
109         }
110
111         return res;
112 }
113
114 static int parse_user_event(struct discover_context *ctx, struct event *event)
115 {
116         struct discover_boot_option *d_opt;
117         char *server_ip, *root_dir, *p;
118         struct boot_option *opt;
119         struct device *dev;
120         const char *val;
121
122         dev = ctx->device->device;
123
124         d_opt = discover_boot_option_create(ctx, ctx->device);
125         if (!d_opt)
126                 goto fail;
127
128         opt = d_opt->option;
129
130         val = event_get_param(event, "name");
131
132         if (!val) {
133                 pb_log("%s: no name found\n", __func__);
134                 goto fail_opt;
135         }
136
137         opt->id = talloc_asprintf(opt, "%s#%s", dev->id, val);
138         opt->name = talloc_strdup(opt, val);
139
140         d_opt->boot_image = user_event_resource(d_opt, event);
141         if (!d_opt->boot_image) {
142                 pb_log("%s: no boot image found for %s!\n", __func__,
143                                 opt->name);
144                 goto fail_opt;
145         }
146
147         val = event_get_param(event, "rootpath");
148         if (val) {
149                 server_ip = talloc_strdup(opt, val);
150                 p = strchr(server_ip, ':');
151                 if (p) {
152                         root_dir = talloc_strdup(opt, p + 1);
153                         *p = '\0';
154                         opt->boot_args = talloc_asprintf(opt, "%s%s:%s",
155                                         "root=/dev/nfs ip=any nfsroot=",
156                                         server_ip, root_dir);
157
158                         talloc_free(root_dir);
159                 } else {
160                         opt->boot_args = talloc_asprintf(opt, "%s",
161                                         "root=/dev/nfs ip=any nfsroot=");
162                 }
163
164                 talloc_free(server_ip);
165         }
166
167         opt->description = talloc_asprintf(opt, "%s %s", opt->boot_image_file,
168                 opt->boot_args ? : "");
169
170         if (event_get_param(event, "default"))
171                 opt->is_default = true;
172
173         discover_context_add_boot_option(ctx, d_opt);
174
175         return 0;
176
177 fail_opt:
178         talloc_free(d_opt);
179 fail:
180         return -1;
181 }
182
183 static const char *parse_host_addr(struct event *event)
184 {
185         const char *val;
186
187         val = event_get_param(event, "tftp");
188         if (val)
189                 return val;
190
191         val = event_get_param(event, "siaddr");
192         if (val)
193                 return val;
194
195         val = event_get_param(event, "serverid");
196         if (val)
197                 return val;
198
199         return NULL;
200 }
201
202 static char *parse_mac_addr(struct discover_context *ctx, const char *mac)
203 {
204         unsigned int mac_addr_arr[MAC_ADDR_SIZE];
205         char *mac_addr;
206
207         sscanf(mac, "%X:%X:%X:%X:%X:%X", mac_addr_arr, mac_addr_arr + 1,
208                         mac_addr_arr + 2, mac_addr_arr + 3, mac_addr_arr + 4,
209                         mac_addr_arr + 5);
210
211         mac_addr = talloc_asprintf(ctx, "01-%02X-%02X-%02X-%02X-%02X-%02X",
212                         mac_addr_arr[0], mac_addr_arr[1], mac_addr_arr[2],
213                         mac_addr_arr[3], mac_addr_arr[4], mac_addr_arr[5]);
214
215         return mac_addr;
216 }
217
218 static char *parse_ip_addr(struct discover_context *ctx, const char *ip)
219 {
220         unsigned int ip_addr_arr[IP_ADDR_SIZE];
221         char *ip_hex;
222
223         sscanf(ip, "%u.%u.%u.%u", ip_addr_arr, ip_addr_arr + 1,
224                         ip_addr_arr + 2, ip_addr_arr + 3);
225
226         ip_hex = talloc_asprintf(ctx, "%02X%02X%02X%02X", ip_addr_arr[0],
227                         ip_addr_arr[1], ip_addr_arr[2], ip_addr_arr[3]);
228
229         return ip_hex;
230 }
231
232 struct pb_url *user_event_parse_conf_url(struct discover_context *ctx,
233                 struct event *event)
234 {
235         const char *conffile, *host, *bootfile;
236         char *p, *basedir, *url_str;
237         struct pb_url *url;
238
239         conffile = event_get_param(event, "pxeconffile");
240         if (conffile) {
241                 if (is_url(conffile)) {
242                         url = pb_url_parse(ctx, conffile);
243                 } else {
244                         host = parse_host_addr(event);
245                         if (!host) {
246                                 pb_log("%s: host address not found\n",
247                                                 __func__);
248                                 return NULL;
249                         }
250
251                         url_str = talloc_asprintf(ctx, "%s%s/%s", "tftp://",
252                                         host, conffile);
253                         url = pb_url_parse(ctx, url_str);
254
255                         talloc_free(url_str);
256                 }
257
258                 ctx->conf_url = url;
259         } else {
260                 host = parse_host_addr(event);
261                 if (!host) {
262                         pb_log("%s: host address not found\n", __func__);
263                         return NULL;
264                 }
265
266                 bootfile = event_get_param(event, "bootfile");
267                 if (!bootfile) {
268                         pb_log("%s: bootfile param not found\n", __func__);
269                         return NULL;
270                 }
271
272                 basedir = talloc_strdup(ctx, bootfile);
273                 p = strchr(basedir, '/');
274                 if (p)
275                         *p = '\0';
276
277                 if (!strcmp(basedir,"") || !strcmp(basedir, "."))
278                         url_str = talloc_asprintf(ctx, "%s%s/", "tftp://",host);
279                 else
280                         url_str = talloc_asprintf(ctx, "%s%s/%s/", "tftp://",host,
281                                         basedir);
282
283                 url = pb_url_parse(ctx, url_str);
284
285                 talloc_free(url_str);
286                 talloc_free(basedir);
287         }
288
289         return url;
290 }
291
292 char **user_event_parse_conf_filenames(
293                 struct discover_context *ctx, struct event *event)
294 {
295         char *mac_addr, *ip_hex;
296         const char *mac, *ip;
297         char **filenames;
298         int index, len;
299
300         mac = event_get_param(event, "mac");
301         if (mac)
302                 mac_addr = parse_mac_addr(ctx, mac);
303         else
304                 mac_addr = NULL;
305
306         ip = event_get_param(event, "ip");
307         if (ip) {
308                 ip_hex = parse_ip_addr(ctx, ip);
309                 len = strlen(ip_hex);
310         } else {
311                 ip_hex = NULL;
312                 len = 0;
313         }
314
315         if (!mac_addr && !ip_hex) {
316                 pb_log("%s: neither mac nor ip parameter found\n", __func__);
317                 return NULL;
318         }
319
320         /* Filenames as fallback IP's + mac + default */
321         filenames = talloc_array(ctx, char *, len + 3);
322
323         index = 0;
324         if (mac_addr)
325                 filenames[index++] = talloc_strdup(filenames, mac_addr);
326
327         while (len) {
328                 filenames[index++] = talloc_strdup(filenames, ip_hex);
329                 ip_hex[--len] = '\0';
330         }
331
332         filenames[index++] = talloc_strdup(filenames, "default");
333         filenames[index++] = NULL;
334
335         if (mac_addr)
336                 talloc_free(mac_addr);
337
338         if (ip_hex)
339                 talloc_free(ip_hex);
340
341         return filenames;
342 }
343
344 static int user_event_dhcp(struct user_event *uev, struct event *event)
345 {
346         struct device_handler *handler = uev->handler;
347         struct discover_device *dev;
348
349         dev = discover_device_create(handler, event->device);
350
351         device_handler_dhcp(handler, dev, event);
352
353         return 0;
354 }
355
356 static int user_event_conf(struct user_event *uev, struct event *event)
357 {
358         struct device_handler *handler = uev->handler;
359         struct discover_device *dev;
360         struct pb_url *url;
361         const char *val;
362
363         val = event_get_param(event, "url");
364         if (!val)
365                 return 0;
366
367         url = pb_url_parse(event, val);
368         if (!url)
369                 return 0;
370
371         dev = discover_device_create(handler, event->device);
372
373         device_handler_conf(handler, dev, url);
374
375         return 0;
376 }
377
378 static int user_event_add(struct user_event *uev, struct event *event)
379 {
380         struct device_handler *handler = uev->handler;
381         struct discover_context *ctx;
382         struct discover_device *dev;
383
384         dev = discover_device_create(handler, event->device);
385         ctx = device_handler_discover_context_create(handler, dev);
386
387         parse_user_event(ctx, event);
388
389         device_handler_discover_context_commit(handler, ctx);
390
391         talloc_free(ctx);
392
393         return 0;
394 }
395
396 static int user_event_remove(struct user_event *uev, struct event *event)
397 {
398         struct device_handler *handler = uev->handler;
399         struct discover_device *dev;
400
401         dev = device_lookup_by_id(handler, event->device);
402         if (!dev)
403                 return 0;
404
405         device_handler_remove(handler, dev);
406
407         return 0;
408 }
409
410 static void user_event_handle_message(struct user_event *uev, char *buf,
411         int len)
412 {
413         int result;
414         struct event *event;
415
416         event = talloc(uev, struct event);
417         event->type = EVENT_TYPE_USER;
418
419         result = event_parse_ad_message(event, buf, len);
420
421         if (result)
422                 return;
423
424         user_event_print_event(event);
425
426         switch (event->action) {
427         case EVENT_ACTION_ADD:
428                 result = user_event_add(uev, event);
429                 break;
430         case EVENT_ACTION_REMOVE:
431                 result = user_event_remove(uev, event);
432                 break;
433         case EVENT_ACTION_CONF:
434                 result = user_event_conf(uev, event);
435                 break;
436         case EVENT_ACTION_DHCP:
437                 result = user_event_dhcp(uev, event);
438                 break;
439         default:
440                 break;
441         }
442
443         talloc_free(event);
444
445         return;
446 }
447
448 static int user_event_process(void *arg)
449 {
450         struct user_event *uev = arg;
451         char buf[PBOOT_USER_EVENT_SIZE];
452         int len;
453
454         len = recvfrom(uev->socket, buf, sizeof(buf), 0, NULL, NULL);
455
456         if (len < 0) {
457                 pb_log("%s: socket read failed: %s", __func__, strerror(errno));
458                 return 0;
459         }
460
461         if (len == 0) {
462                 pb_log("%s: empty", __func__);
463                 return 0;
464         }
465
466         pb_debug("%s: %u bytes\n", __func__, len);
467
468         user_event_handle_message(uev, buf, len);
469
470         return 0;
471 }
472
473 static int user_event_destructor(void *arg)
474 {
475         struct user_event *uev = arg;
476
477         pb_debug("%s\n", __func__);
478
479         if (uev->socket >= 0)
480                 close(uev->socket);
481
482         return 0;
483 }
484
485 struct user_event *user_event_init(struct waitset *waitset,
486                 struct device_handler *handler)
487 {
488         struct sockaddr_un addr;
489         struct user_event *uev;
490
491         unlink(PBOOT_USER_EVENT_SOCKET);
492
493         uev = talloc(NULL, struct user_event);
494
495         uev->handler = handler;
496
497         uev->socket = socket(AF_UNIX, SOCK_DGRAM, 0);
498         if (uev->socket < 0) {
499                 pb_log("%s: Error creating event handler socket: %s\n",
500                         __func__, strerror(errno));
501                 goto out_err;
502         }
503
504         talloc_set_destructor(uev, user_event_destructor);
505
506         memset(&addr, 0, sizeof(addr));
507         addr.sun_family = AF_UNIX;
508         strcpy(addr.sun_path, PBOOT_USER_EVENT_SOCKET);
509
510         if (bind(uev->socket, (struct sockaddr *)&addr, sizeof(addr))) {
511                 pb_log("Error binding event handler socket: %s\n",
512                         strerror(errno));
513         }
514
515         waiter_register_io(waitset, uev->socket, WAIT_IN,
516                         user_event_process, uev);
517
518         pb_debug("%s: waiting on %s\n", __func__, PBOOT_USER_EVENT_SOCKET);
519
520         return uev;
521
522 out_err:
523         talloc_free(uev);
524         return NULL;
525 }
526
527 void user_event_destroy(struct user_event *uev)
528 {
529         talloc_free(uev);
530 }