netifd: Reload proto on topology change
[project/netifd.git] / proto-static.c
1 /*
2  * netifd - network interface daemon
3  * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
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 version 2
7  * as published by the Free Software Foundation
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 #include <string.h>
15 #include <stdlib.h>
16 #include <stdio.h>
17
18 #include <arpa/inet.h>
19 #include <netinet/in.h>
20
21 #include "netifd.h"
22 #include "interface.h"
23 #include "interface-ip.h"
24 #include "proto.h"
25 #include "system.h"
26
27 struct static_proto_state {
28         struct interface_proto_state proto;
29
30         struct blob_attr *config;
31 };
32
33 static bool
34 static_proto_setup(struct static_proto_state *state)
35 {
36         return proto_apply_static_ip_settings(state->proto.iface, state->config) == 0;
37 }
38
39 static int
40 static_handler(struct interface_proto_state *proto,
41                enum interface_proto_cmd cmd, bool force)
42 {
43         struct static_proto_state *state;
44         int ret = 0;
45
46         state = container_of(proto, struct static_proto_state, proto);
47
48         switch (cmd) {
49         case PROTO_CMD_SETUP:
50                 if (!static_proto_setup(state))
51                         return -1;
52
53                 break;
54         case PROTO_CMD_TEARDOWN:
55         case PROTO_CMD_RENEW:
56                 break;
57         }
58
59         return ret;
60 }
61
62 static void
63 static_free(struct interface_proto_state *proto)
64 {
65         struct static_proto_state *state;
66
67         state = container_of(proto, struct static_proto_state, proto);
68         free(state->config);
69         free(state);
70 }
71
72 static struct interface_proto_state *
73 static_attach(const struct proto_handler *h, struct interface *iface,
74               struct blob_attr *attr)
75 {
76         struct static_proto_state *state;
77
78         state = calloc(1, sizeof(*state));
79         if (!state)
80                 return NULL;
81
82         state->config = malloc(blob_pad_len(attr));
83         if (!state->config)
84                 goto error;
85
86         memcpy(state->config, attr, blob_pad_len(attr));
87         state->proto.free = static_free;
88         state->proto.cb = static_handler;
89
90         return &state->proto;
91
92 error:
93         free(state);
94         return NULL;
95 }
96
97 static struct proto_handler static_proto = {
98         .name = "static",
99         .flags = PROTO_FLAG_IMMEDIATE,
100         .config_params = &proto_ip_attr,
101         .attach = static_attach,
102 };
103
104 static void __init
105 static_proto_init(void)
106 {
107         add_proto_handler(&static_proto);
108 }