free routes and addresses
[project/netifd.git] / proto.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "interface.h"
7 #include "proto.h"
8
9 static struct avl_tree handlers;
10
11 void add_proto_handler(struct proto_handler *p)
12 {
13         if (!handlers.comp)
14                 avl_init(&handlers, avl_strcmp, false, NULL);
15
16         if (p->avl.key)
17                 return;
18
19         p->avl.key = p->name;
20         avl_insert(&handlers, &p->avl);
21 }
22
23 static void
24 default_proto_free(struct interface_proto_state *proto)
25 {
26         free(proto);
27 }
28
29 static int
30 invalid_proto_handler(struct interface_proto_state *proto,
31                       enum interface_proto_cmd cmd, bool force)
32 {
33         return -1;
34 }
35
36 static int
37 no_proto_handler(struct interface_proto_state *proto,
38                  enum interface_proto_cmd cmd, bool force)
39 {
40         return 0;
41 }
42
43 static struct interface_proto_state *
44 default_proto_attach(const struct proto_handler *h,
45                      struct interface *iface, struct blob_attr *attr)
46 {
47         struct interface_proto_state *proto;
48
49         proto = calloc(1, sizeof(*proto));
50         proto->free = default_proto_free;
51         proto->flags = PROTO_FLAG_IMMEDIATE;
52         proto->handler = no_proto_handler;
53
54         return proto;
55 }
56
57 static const struct proto_handler no_proto = {
58         .name = "none",
59         .attach = default_proto_attach,
60 };
61
62 static const struct proto_handler *
63 get_proto_handler(const char *name)
64 {
65         struct proto_handler *proto;
66
67         if (!strcmp(name, "none"))
68             return &no_proto;
69
70         if (!handlers.comp)
71                 return NULL;
72
73         return avl_find_element(&handlers, name, proto, avl);
74 }
75
76 void
77 proto_init_interface(struct interface *iface, struct blob_attr *attr)
78 {
79         const struct proto_handler *proto = iface->proto_handler;
80         struct interface_proto_state *state = NULL;
81
82         if (proto)
83                 state = proto->attach(proto, iface, attr);
84
85         if (!state) {
86                 state = no_proto.attach(&no_proto, iface, attr);
87                 state->handler = invalid_proto_handler;
88         }
89
90         interface_set_proto_state(iface, state);
91 }
92
93 void
94 proto_attach_interface(struct interface *iface, const char *proto_name)
95 {
96         const struct proto_handler *proto = NULL;
97
98         if (!proto_name) {
99                 interface_add_error(iface, "proto", "NO_PROTO", NULL, 0);
100                 return;
101         }
102
103         proto = get_proto_handler(proto_name);
104         if (!proto)
105                 interface_add_error(iface, "proto", "INVALID_PROTO", NULL, 0);
106
107         iface->proto_handler = proto;
108 }
109
110 int
111 interface_proto_event(struct interface_proto_state *proto,
112                       enum interface_proto_cmd cmd, bool force)
113 {
114         enum interface_event ev;
115         int ret;
116
117         ret = proto->handler(proto, cmd, force);
118         if (ret || !(proto->flags & PROTO_FLAG_IMMEDIATE))
119                 goto out;
120
121         switch(cmd) {
122         case PROTO_CMD_SETUP:
123                 ev = IFEV_UP;
124                 break;
125         case PROTO_CMD_TEARDOWN:
126                 ev = IFEV_DOWN;
127                 break;
128         default:
129                 return -EINVAL;
130         }
131         proto->proto_event(proto, ev);
132
133 out:
134         return ret;
135 }