free routes and addresses
[project/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 int interface_add_address(struct interface *iface, struct device_addr *addr)
14 {
15 int family;
16
17 if (addr->flags & DEVADDR_INET6)
18 family = AF_INET6;
19 else
20 family = AF_INET;
21
22 list_add_tail(&addr->list, &iface->address);
23 return system_add_address(iface->l3_iface->dev, addr);
24 }
25
26 void interface_del_address(struct interface *iface, struct device_addr *addr)
27 {
28 int family;
29
30 if (addr->flags & DEVADDR_INET6)
31 family = AF_INET6;
32 else
33 family = AF_INET;
34
35 list_del(&addr->list);
36 system_del_address(iface->l3_iface->dev, addr);
37 free(addr);
38 }
39
40 void interface_del_ctx_addr(struct interface *iface, void *ctx)
41 {
42 struct device_addr *addr, *tmp;
43
44 list_for_each_entry_safe(addr, tmp, &iface->address, list) {
45 if (ctx && addr->ctx != ctx)
46 continue;
47
48 interface_del_address(iface, addr);
49 }
50 }
51
52 int interface_add_route(struct interface *iface, struct device_route *route)
53 {
54 list_add_tail(&route->list, &iface->routes);
55 return system_add_route(iface->l3_iface->dev, route);
56 }
57
58 void interface_del_route(struct interface *iface, struct device_route *route)
59 {
60 list_del(&route->list);
61 system_del_route(iface->l3_iface->dev, route);
62 if (!route->keep)
63 free(route);
64 }
65
66 void interface_del_all_routes(struct interface *iface)
67 {
68 struct device_route *route, *tmp;
69
70 list_for_each_entry_safe(route, tmp, &iface->routes, list)
71 interface_del_route(iface, route);
72 }