2d7a2d0d8d1b22a5ac9771f90f3d33ba67dc1e68
[oweals/netifd.git] / interface-ip.c
1 #include <string.h>
2 #include <stdlib.h>
3 #include <stdio.h>
4
5 #include "netifd.h"
6 #include "device.h"
7 #include "interface.h"
8 #include "interface-ip.h"
9 #include "proto.h"
10 #include "ubus.h"
11 #include "system.h"
12
13 static int
14 addr_cmp(const void *k1, const void *k2, void *ptr)
15 {
16         return memcmp(k1, k2, sizeof(struct device_addr) -
17                       offsetof(struct device_addr, mask));
18 }
19
20 static int
21 route_cmp(const void *k1, const void *k2, void *ptr)
22 {
23         return memcmp(k1, k2, sizeof(struct device_route) -
24                       offsetof(struct device_route, mask));
25 }
26
27 static void
28 interface_update_proto_addr(struct vlist_tree *tree,
29                             struct vlist_node *node_new,
30                             struct vlist_node *node_old)
31 {
32         struct interface *iface;
33         struct device *dev;
34         struct device_addr *addr;
35
36         iface = container_of(tree, struct interface, proto_addr);
37         dev = iface->l3_dev->dev;
38
39         if (node_old) {
40                 addr = container_of(node_old, struct device_addr, node);
41                 if (!(addr->flags & DEVADDR_EXTERNAL))
42                         system_del_address(dev, addr);
43                 free(addr);
44         }
45
46         if (node_new) {
47                 addr = container_of(node_new, struct device_addr, node);
48                 if (!(addr->flags & DEVADDR_EXTERNAL))
49                         system_add_address(dev, addr);
50         }
51 }
52
53 static void
54 interface_update_proto_route(struct vlist_tree *tree,
55                              struct vlist_node *node_new,
56                              struct vlist_node *node_old)
57 {
58         struct interface *iface;
59         struct device *dev;
60         struct device_route *route;
61
62         iface = container_of(tree, struct interface, proto_route);
63         dev = iface->l3_dev->dev;
64
65         if (node_old) {
66                 route = container_of(node_old, struct device_route, node);
67                 if (!(route->flags & DEVADDR_EXTERNAL))
68                         system_del_route(dev, route);
69                 free(route);
70         }
71
72         if (node_new) {
73                 route = container_of(node_new, struct device_route, node);
74                 if (!(route->flags & DEVADDR_EXTERNAL))
75                         system_add_route(dev, route);
76         }
77 }
78
79 void
80 interface_ip_init(struct interface *iface)
81 {
82         vlist_init(&iface->proto_route, route_cmp, interface_update_proto_route,
83                    struct device_route, node, mask);
84         vlist_init(&iface->proto_addr, addr_cmp, interface_update_proto_addr,
85                    struct device_addr, node, mask);
86 }