add a new event for interface reload
[project/netifd.git] / proto-static.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include <arpa/inet.h>
6 #include <netinet/in.h>
7
8 #include "netifd.h"
9 #include "interface.h"
10 #include "interface-ip.h"
11 #include "proto.h"
12 #include "system.h"
13
14 struct static_proto_state {
15 struct interface_proto_state proto;
16
17 struct blob_attr *config;
18 };
19
20 static bool
21 static_proto_setup(struct static_proto_state *state)
22 {
23 return proto_apply_ip_settings(state->proto.iface, state->config, false) == 0;
24 }
25
26 static int
27 static_handler(struct interface_proto_state *proto,
28 enum interface_proto_cmd cmd, bool force)
29 {
30 struct static_proto_state *state;
31 int ret = 0;
32
33 state = container_of(proto, struct static_proto_state, proto);
34
35 switch (cmd) {
36 case PROTO_CMD_SETUP:
37 if (!static_proto_setup(state))
38 return -1;
39
40 break;
41 case PROTO_CMD_TEARDOWN:
42 break;
43 }
44 return ret;
45 }
46
47 static void
48 static_free(struct interface_proto_state *proto)
49 {
50 struct static_proto_state *state;
51
52 state = container_of(proto, struct static_proto_state, proto);
53 free(state->config);
54 free(state);
55 }
56
57 static struct interface_proto_state *
58 static_attach(const struct proto_handler *h, struct interface *iface,
59 struct blob_attr *attr)
60 {
61 struct static_proto_state *state;
62
63 state = calloc(1, sizeof(*state));
64 if (!state)
65 return NULL;
66
67 state->config = malloc(blob_pad_len(attr));
68 if (!state->config)
69 goto error;
70
71 memcpy(state->config, attr, blob_pad_len(attr));
72 state->proto.free = static_free;
73 state->proto.cb = static_handler;
74
75 return &state->proto;
76
77 error:
78 free(state);
79 return NULL;
80 }
81
82 static struct proto_handler static_proto = {
83 .name = "static",
84 .flags = PROTO_FLAG_IMMEDIATE,
85 .config_params = &proto_ip_attr,
86 .attach = static_attach,
87 };
88
89 static void __init
90 static_proto_init(void)
91 {
92 add_proto_handler(&static_proto);
93 }