]> git.ipfire.org Git - thirdparty/bird.git/blob - nest/rt-dev.c
Merge branch 'master' into int-new-channels
[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 net *n;
59
60 DBG("dev_if_notify: %s:%I going down\n", ad->iface->name, ad->ip);
61 n = net_find(c->table, &ad->prefix);
62 if (!n)
63 {
64 DBG("dev_if_notify: device shutdown: prefix not found\n");
65 return;
66 }
67
68 /* Use iface ID as local source ID */
69 struct rte_src *src = rt_get_source(P, ad->iface->index);
70 rte_update2(c, n, NULL, src);
71 }
72 else if (flags & IF_CHANGE_UP)
73 {
74 rta *a;
75 net *n;
76 rte *e;
77
78 DBG("dev_if_notify: %s:%I going up\n", ad->iface->name, ad->ip);
79
80 /* Use iface ID as local source ID */
81 struct rte_src *src = rt_get_source(P, ad->iface->index);
82
83 rta a0 = {
84 .src = src,
85 .source = RTS_DEVICE,
86 .scope = SCOPE_UNIVERSE,
87 .cast = RTC_UNICAST,
88 .dest = RTD_DEVICE,
89 .iface = ad->iface
90 };
91
92 a = rta_lookup(&a0);
93 n = net_get(c->table, &ad->prefix);
94 e = rte_get_temp(a);
95 e->net = n;
96 e->pflags = 0;
97 rte_update2(c, n, e, src);
98 }
99 }
100
101 static struct proto *
102 dev_init(struct proto_config *CF)
103 {
104 struct proto *P = proto_new(CF);
105 struct rt_dev_proto *p = (void *) P;
106 // struct rt_dev_config *cf = (void *) CF;
107
108 proto_configure_channel(P, &p->ip4_channel, proto_cf_find_channel(CF, NET_IP4));
109 proto_configure_channel(P, &p->ip6_channel, proto_cf_find_channel(CF, NET_IP6));
110
111 P->ifa_notify = dev_ifa_notify;
112
113 return P;
114 }
115
116 static int
117 dev_reconfigure(struct proto *P, struct proto_config *CF)
118 {
119 struct rt_dev_proto *p = (void *) P;
120 struct rt_dev_config *o = (void *) P->cf;
121 struct rt_dev_config *n = (void *) CF;
122
123 if (!iface_patts_equal(&o->iface_list, &n->iface_list, NULL))
124 return 0;
125
126 return
127 proto_configure_channel(P, &p->ip4_channel, proto_cf_find_channel(CF, NET_IP4)) &&
128 proto_configure_channel(P, &p->ip6_channel, proto_cf_find_channel(CF, NET_IP6));
129
130 return 1;
131 }
132
133 static void
134 dev_copy_config(struct proto_config *dest, struct proto_config *src)
135 {
136 struct rt_dev_config *d = (void *) dest;
137 struct rt_dev_config *s = (void *) src;
138
139 /*
140 * We copy iface_list as ifaces can be shared by more direct protocols.
141 * Copy suffices to be is shallow, because new nodes can be added, but
142 * old nodes cannot be modified (although they contain internal lists).
143 */
144 cfg_copy_list(&d->iface_list, &s->iface_list, sizeof(struct iface_patt));
145 }
146
147 struct protocol proto_device = {
148 .name = "Direct",
149 .template = "direct%d",
150 .preference = DEF_PREF_DIRECT,
151 .channel_mask = NB_IP,
152 .proto_size = sizeof(struct rt_dev_proto),
153 .config_size = sizeof(struct rt_dev_config),
154 .init = dev_init,
155 .reconfigure = dev_reconfigure,
156 .copy_config = dev_copy_config
157 };