]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/rt-dev.c
Merge branch 'master' into int-new
[thirdparty/bird.git] / nest / rt-dev.c
1 /*
2 * BIRD -- Direct Device Routes
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 /**
10 * DOC: Direct
11 *
12 * The Direct protocol works by converting all ifa_notify() events it receives
13 * to rte_update() calls for the corresponding network.
14 */
15
16 #undef LOCAL_DEBUG
17
18 #include "nest/bird.h"
19 #include "nest/iface.h"
20 #include "nest/protocol.h"
21 #include "nest/route.h"
22 #include "nest/rt-dev.h"
23 #include "conf/conf.h"
24 #include "lib/resource.h"
25 #include "lib/string.h"
26
27
28 static void
29 dev_ifa_notify(struct proto *P, uint flags, struct ifa *ad)
30 {
31 struct rt_dev_proto *p = (void *) P;
32 struct rt_dev_config *cf = (void *) P->cf;
33 struct channel *c;
34
35 if (!EMPTY_LIST(cf->iface_list) &&
36 !iface_patt_find(&cf->iface_list, ad->iface, ad->iface->addr))
37 /* Empty list is automatically treated as "*" */
38 return;
39
40 if (ad->flags & IA_SECONDARY)
41 return;
42
43 if (ad->scope <= SCOPE_LINK)
44 return;
45
46 if (ad->prefix.type == NET_IP4)
47 c = p->ip4_channel;
48 else if (ad->prefix.type == NET_IP6)
49 c = p->ip6_channel;
50 else
51 return;
52
53 if (!c)
54 return;
55
56 if (flags & IF_CHANGE_DOWN)
57 {
58 DBG("dev_if_notify: %s:%I going down\n", ad->iface->name, ad->ip);
59
60 /* Use iface ID as local source ID */
61 struct rte_src *src = rt_get_source(P, ad->iface->index);
62 rte_update2(c, &ad->prefix, NULL, src);
63 }
64 else if (flags & IF_CHANGE_UP)
65 {
66 rta *a;
67 rte *e;
68
69 DBG("dev_if_notify: %s:%I going up\n", ad->iface->name, ad->ip);
70
71 if (cf->check_link && !(ad->iface->flags & IF_LINK_UP))
72 return;
73
74 /* Use iface ID as local source ID */
75 struct rte_src *src = rt_get_source(P, ad->iface->index);
76
77 rta a0 = {
78 .src = src,
79 .source = RTS_DEVICE,
80 .scope = SCOPE_UNIVERSE,
81 .cast = RTC_UNICAST,
82 .dest = RTD_DEVICE,
83 .iface = ad->iface
84 };
85
86 a = rta_lookup(&a0);
87 e = rte_get_temp(a);
88 e->pflags = 0;
89 rte_update2(c, &ad->prefix, e, src);
90 }
91 }
92
93 static void
94 dev_if_notify(struct proto *p, uint c, struct iface *iface)
95 {
96 struct rt_dev_config *cf = (void *) p->cf;
97
98 if (c & (IF_CHANGE_UP | IF_CHANGE_DOWN))
99 return;
100
101 if ((c & IF_CHANGE_LINK) && cf->check_link)
102 {
103 uint ac = (iface->flags & IF_LINK_UP) ? IF_CHANGE_UP : IF_CHANGE_DOWN;
104
105 struct ifa *a;
106 WALK_LIST(a, iface->addrs)
107 dev_ifa_notify(p, ac, a);
108 }
109 }
110
111
112 static struct proto *
113 dev_init(struct proto_config *CF)
114 {
115 struct proto *P = proto_new(CF);
116 struct rt_dev_proto *p = (void *) P;
117 // struct rt_dev_config *cf = (void *) CF;
118
119 proto_configure_channel(P, &p->ip4_channel, proto_cf_find_channel(CF, NET_IP4));
120 proto_configure_channel(P, &p->ip6_channel, proto_cf_find_channel(CF, NET_IP6));
121
122 P->if_notify = dev_if_notify;
123 P->ifa_notify = dev_ifa_notify;
124
125 return P;
126 }
127
128 static int
129 dev_reconfigure(struct proto *P, struct proto_config *CF)
130 {
131 struct rt_dev_proto *p = (void *) P;
132 struct rt_dev_config *o = (void *) P->cf;
133 struct rt_dev_config *n = (void *) CF;
134
135 if (!iface_patts_equal(&o->iface_list, &n->iface_list, NULL) ||
136 (o->check_link != n->check_link))
137 return 0;
138
139 return
140 proto_configure_channel(P, &p->ip4_channel, proto_cf_find_channel(CF, NET_IP4)) &&
141 proto_configure_channel(P, &p->ip6_channel, proto_cf_find_channel(CF, NET_IP6));
142
143 return 1;
144 }
145
146 static void
147 dev_copy_config(struct proto_config *dest, struct proto_config *src)
148 {
149 struct rt_dev_config *d = (void *) dest;
150 struct rt_dev_config *s = (void *) src;
151
152 /*
153 * We copy iface_list as ifaces can be shared by more direct protocols.
154 * Copy suffices to be is shallow, because new nodes can be added, but
155 * old nodes cannot be modified (although they contain internal lists).
156 */
157 cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct iface_patt));
158
159 d->check_link = s->check_link;
160 }
161
162 struct protocol proto_device = {
163 .name = "Direct",
164 .template = "direct%d",
165 .preference = DEF_PREF_DIRECT,
166 .channel_mask = NB_IP,
167 .proto_size = sizeof(struct rt_dev_proto),
168 .config_size = sizeof(struct rt_dev_config),
169 .init = dev_init,
170 .reconfigure = dev_reconfigure,
171 .copy_config = dev_copy_config
172 };