]> git.proxmox.com Git - mirror_ovs.git/blob - ofproto/in-band.c
treewide: Remove trailing whitespace
[mirror_ovs.git] / ofproto / in-band.c
1 /*
2 * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <config.h>
18 #include "in-band.h"
19 #include <arpa/inet.h>
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include "dhcp.h"
27 #include "dpif.h"
28 #include "flow.h"
29 #include "netdev.h"
30 #include "odp-util.h"
31 #include "ofproto.h"
32 #include "ofpbuf.h"
33 #include "openflow/openflow.h"
34 #include "packets.h"
35 #include "poll-loop.h"
36 #include "status.h"
37 #include "timeval.h"
38 #include "vlog.h"
39
40 VLOG_DEFINE_THIS_MODULE(in_band)
41
42 /* In-band control allows a single network to be used for OpenFlow
43 * traffic and other data traffic. Refer to ovs-vswitchd.conf(5) and
44 * secchan(8) for a description of configuring in-band control.
45 *
46 * This comment is an attempt to describe how in-band control works at a
47 * wire- and implementation-level. Correctly implementing in-band
48 * control has proven difficult due to its many subtleties, and has thus
49 * gone through many iterations. Please read through and understand the
50 * reasoning behind the chosen rules before making modifications.
51 *
52 * In Open vSwitch, in-band control is implemented as "hidden" flows (in that
53 * they are not visible through OpenFlow) and at a higher priority than
54 * wildcarded flows can be set up by through OpenFlow. This is done so that
55 * the OpenFlow controller cannot interfere with them and possibly break
56 * connectivity with its switches. It is possible to see all flows, including
57 * in-band ones, with the ovs-appctl "bridge/dump-flows" command.
58 *
59 * The Open vSwitch implementation of in-band control can hide traffic to
60 * arbitrary "remotes", where each remote is one TCP port on one IP address.
61 * Currently the remotes are automatically configured as the in-band OpenFlow
62 * controllers plus the OVSDB managers, if any. (The latter is a requirement
63 * because OVSDB managers are responsible for configuring OpenFlow controllers,
64 * so if the manager cannot be reached then OpenFlow cannot be reconfigured.)
65 *
66 * The following rules (with the OFPP_NORMAL action) are set up on any bridge
67 * that has any remotes:
68 *
69 * (a) DHCP requests sent from the local port.
70 * (b) ARP replies to the local port's MAC address.
71 * (c) ARP requests from the local port's MAC address.
72 *
73 * In-band also sets up the following rules for each unique next-hop MAC
74 * address for the remotes' IPs (the "next hop" is either the remote
75 * itself, if it is on a local subnet, or the gateway to reach the remote):
76 *
77 * (d) ARP replies to the next hop's MAC address.
78 * (e) ARP requests from the next hop's MAC address.
79 *
80 * In-band also sets up the following rules for each unique remote IP address:
81 *
82 * (f) ARP replies containing the remote's IP address as a target.
83 * (g) ARP requests containing the remote's IP address as a source.
84 *
85 * In-band also sets up the following rules for each unique remote (IP,port)
86 * pair:
87 *
88 * (h) TCP traffic to the remote's IP and port.
89 * (i) TCP traffic from the remote's IP and port.
90 *
91 * The goal of these rules is to be as narrow as possible to allow a
92 * switch to join a network and be able to communicate with the
93 * remotes. As mentioned earlier, these rules have higher priority
94 * than the controller's rules, so if they are too broad, they may
95 * prevent the controller from implementing its policy. As such,
96 * in-band actively monitors some aspects of flow and packet processing
97 * so that the rules can be made more precise.
98 *
99 * In-band control monitors attempts to add flows into the datapath that
100 * could interfere with its duties. The datapath only allows exact
101 * match entries, so in-band control is able to be very precise about
102 * the flows it prevents. Flows that miss in the datapath are sent to
103 * userspace to be processed, so preventing these flows from being
104 * cached in the "fast path" does not affect correctness. The only type
105 * of flow that is currently prevented is one that would prevent DHCP
106 * replies from being seen by the local port. For example, a rule that
107 * forwarded all DHCP traffic to the controller would not be allowed,
108 * but one that forwarded to all ports (including the local port) would.
109 *
110 * As mentioned earlier, packets that miss in the datapath are sent to
111 * the userspace for processing. The userspace has its own flow table,
112 * the "classifier", so in-band checks whether any special processing
113 * is needed before the classifier is consulted. If a packet is a DHCP
114 * response to a request from the local port, the packet is forwarded to
115 * the local port, regardless of the flow table. Note that this requires
116 * L7 processing of DHCP replies to determine whether the 'chaddr' field
117 * matches the MAC address of the local port.
118 *
119 * It is interesting to note that for an L3-based in-band control
120 * mechanism, the majority of rules are devoted to ARP traffic. At first
121 * glance, some of these rules appear redundant. However, each serves an
122 * important role. First, in order to determine the MAC address of the
123 * remote side (controller or gateway) for other ARP rules, we must allow
124 * ARP traffic for our local port with rules (b) and (c). If we are
125 * between a switch and its connection to the remote, we have to
126 * allow the other switch's ARP traffic to through. This is done with
127 * rules (d) and (e), since we do not know the addresses of the other
128 * switches a priori, but do know the remote's or gateway's. Finally,
129 * if the remote is running in a local guest VM that is not reached
130 * through the local port, the switch that is connected to the VM must
131 * allow ARP traffic based on the remote's IP address, since it will
132 * not know the MAC address of the local port that is sending the traffic
133 * or the MAC address of the remote in the guest VM.
134 *
135 * With a few notable exceptions below, in-band should work in most
136 * network setups. The following are considered "supported' in the
137 * current implementation:
138 *
139 * - Locally Connected. The switch and remote are on the same
140 * subnet. This uses rules (a), (b), (c), (h), and (i).
141 *
142 * - Reached through Gateway. The switch and remote are on
143 * different subnets and must go through a gateway. This uses
144 * rules (a), (b), (c), (h), and (i).
145 *
146 * - Between Switch and Remote. This switch is between another
147 * switch and the remote, and we want to allow the other
148 * switch's traffic through. This uses rules (d), (e), (h), and
149 * (i). It uses (b) and (c) indirectly in order to know the MAC
150 * address for rules (d) and (e). Note that DHCP for the other
151 * switch will not work unless an OpenFlow controller explicitly lets this
152 * switch pass the traffic.
153 *
154 * - Between Switch and Gateway. This switch is between another
155 * switch and the gateway, and we want to allow the other switch's
156 * traffic through. This uses the same rules and logic as the
157 * "Between Switch and Remote" configuration described earlier.
158 *
159 * - Remote on Local VM. The remote is a guest VM on the
160 * system running in-band control. This uses rules (a), (b), (c),
161 * (h), and (i).
162 *
163 * - Remote on Local VM with Different Networks. The remote
164 * is a guest VM on the system running in-band control, but the
165 * local port is not used to connect to the remote. For
166 * example, an IP address is configured on eth0 of the switch. The
167 * remote's VM is connected through eth1 of the switch, but an
168 * IP address has not been configured for that port on the switch.
169 * As such, the switch will use eth0 to connect to the remote,
170 * and eth1's rules about the local port will not work. In the
171 * example, the switch attached to eth0 would use rules (a), (b),
172 * (c), (h), and (i) on eth0. The switch attached to eth1 would use
173 * rules (f), (g), (h), and (i).
174 *
175 * The following are explicitly *not* supported by in-band control:
176 *
177 * - Specify Remote by Name. Currently, the remote must be
178 * identified by IP address. A naive approach would be to permit
179 * all DNS traffic. Unfortunately, this would prevent the
180 * controller from defining any policy over DNS. Since switches
181 * that are located behind us need to connect to the remote,
182 * in-band cannot simply add a rule that allows DNS traffic from
183 * the local port. The "correct" way to support this is to parse
184 * DNS requests to allow all traffic related to a request for the
185 * remote's name through. Due to the potential security
186 * problems and amount of processing, we decided to hold off for
187 * the time-being.
188 *
189 * - Differing Remotes for Switches. All switches must know
190 * the L3 addresses for all the remotes that other switches
191 * may use, since rules need to be set up to allow traffic related
192 * to those remotes through. See rules (f), (g), (h), and (i).
193 *
194 * - Differing Routes for Switches. In order for the switch to
195 * allow other switches to connect to a remote through a
196 * gateway, it allows the gateway's traffic through with rules (d)
197 * and (e). If the routes to the remote differ for the two
198 * switches, we will not know the MAC address of the alternate
199 * gateway.
200 */
201
202 /* Priorities used in classifier for in-band rules. These values are higher
203 * than any that may be set with OpenFlow, and "18" kind of looks like "IB".
204 * The ordering of priorities is not important because all of the rules set up
205 * by in-band control have the same action. The only reason to use more than
206 * one priority is to make the kind of flow easier to see during debugging. */
207 enum {
208 /* One set per bridge. */
209 IBR_FROM_LOCAL_DHCP = 180000, /* (a) From local port, DHCP. */
210 IBR_TO_LOCAL_ARP, /* (b) To local port, ARP. */
211 IBR_FROM_LOCAL_ARP, /* (c) From local port, ARP. */
212
213 /* One set per unique next-hop MAC. */
214 IBR_TO_NEXT_HOP_ARP, /* (d) To remote MAC, ARP. */
215 IBR_FROM_NEXT_HOP_ARP, /* (e) From remote MAC, ARP. */
216
217 /* One set per unique remote IP address. */
218 IBR_TO_REMOTE_ARP, /* (f) To remote IP, ARP. */
219 IBR_FROM_REMOTE_ARP, /* (g) From remote IP, ARP. */
220
221 /* One set per unique remote (IP,port) pair. */
222 IBR_TO_REMOTE_TCP, /* (h) To remote IP, TCP port. */
223 IBR_FROM_REMOTE_TCP /* (i) From remote IP, TCP port. */
224 };
225
226 struct in_band_rule {
227 flow_t flow;
228 uint32_t wildcards;
229 unsigned int priority;
230 };
231
232 /* Track one remote IP and next hop information. */
233 struct in_band_remote {
234 struct sockaddr_in remote_addr; /* IP address, in network byte order. */
235 uint8_t remote_mac[ETH_ADDR_LEN]; /* Next-hop MAC, all-zeros if unknown. */
236 uint8_t last_remote_mac[ETH_ADDR_LEN]; /* Previous nonzero next-hop MAC. */
237 struct netdev *remote_netdev; /* Device to send to next-hop MAC. */
238 };
239
240 struct in_band {
241 struct ofproto *ofproto;
242 struct status_category *ss_cat;
243
244 /* Remote information. */
245 time_t next_remote_refresh; /* Refresh timer. */
246 struct in_band_remote *remotes;
247 size_t n_remotes;
248
249 /* Local information. */
250 time_t next_local_refresh; /* Refresh timer. */
251 uint8_t local_mac[ETH_ADDR_LEN]; /* Current MAC. */
252 struct netdev *local_netdev; /* Local port's network device. */
253
254 /* Local and remote addresses that are installed as flows. */
255 uint8_t installed_local_mac[ETH_ADDR_LEN];
256 struct sockaddr_in *remote_addrs;
257 size_t n_remote_addrs;
258 uint8_t *remote_macs;
259 size_t n_remote_macs;
260 };
261
262 static struct vlog_rate_limit rl = VLOG_RATE_LIMIT_INIT(60, 60);
263
264 static int
265 refresh_remote(struct in_band *ib, struct in_band_remote *r)
266 {
267 struct in_addr next_hop_inaddr;
268 char *next_hop_dev;
269 int retval;
270
271 /* Find the next-hop IP address. */
272 memset(r->remote_mac, 0, sizeof r->remote_mac);
273 retval = netdev_get_next_hop(ib->local_netdev, &r->remote_addr.sin_addr,
274 &next_hop_inaddr, &next_hop_dev);
275 if (retval) {
276 VLOG_WARN("cannot find route for controller ("IP_FMT"): %s",
277 IP_ARGS(&r->remote_addr.sin_addr), strerror(retval));
278 return 1;
279 }
280 if (!next_hop_inaddr.s_addr) {
281 next_hop_inaddr = r->remote_addr.sin_addr;
282 }
283
284 /* Open the next-hop network device. */
285 if (!r->remote_netdev
286 || strcmp(netdev_get_name(r->remote_netdev), next_hop_dev))
287 {
288 netdev_close(r->remote_netdev);
289
290 retval = netdev_open_default(next_hop_dev, &r->remote_netdev);
291 if (retval) {
292 VLOG_WARN_RL(&rl, "cannot open netdev %s (next hop "
293 "to controller "IP_FMT"): %s",
294 next_hop_dev, IP_ARGS(&r->remote_addr.sin_addr),
295 strerror(retval));
296 free(next_hop_dev);
297 return 1;
298 }
299 }
300 free(next_hop_dev);
301
302 /* Look up the MAC address of the next-hop IP address. */
303 retval = netdev_arp_lookup(r->remote_netdev, next_hop_inaddr.s_addr,
304 r->remote_mac);
305 if (retval) {
306 VLOG_DBG_RL(&rl, "cannot look up remote MAC address ("IP_FMT"): %s",
307 IP_ARGS(&next_hop_inaddr.s_addr), strerror(retval));
308 }
309
310 /* If we don't have a MAC address, then refresh quickly, since we probably
311 * will get a MAC address soon (via ARP). Otherwise, we can afford to wait
312 * a little while. */
313 return eth_addr_is_zero(r->remote_mac) ? 1 : 10;
314 }
315
316 static bool
317 refresh_remotes(struct in_band *ib)
318 {
319 struct in_band_remote *r;
320 bool any_changes;
321
322 if (time_now() < ib->next_remote_refresh) {
323 return false;
324 }
325
326 any_changes = false;
327 ib->next_remote_refresh = TIME_MAX;
328 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
329 uint8_t old_remote_mac[ETH_ADDR_LEN];
330 time_t next_refresh;
331
332 /* Save old MAC. */
333 memcpy(old_remote_mac, r->remote_mac, ETH_ADDR_LEN);
334
335 /* Refresh remote information. */
336 next_refresh = refresh_remote(ib, r) + time_now();
337 ib->next_remote_refresh = MIN(ib->next_remote_refresh, next_refresh);
338
339 /* If the MAC changed, log the changes. */
340 if (!eth_addr_equals(r->remote_mac, old_remote_mac)) {
341 any_changes = true;
342 if (!eth_addr_is_zero(r->remote_mac)
343 && !eth_addr_equals(r->last_remote_mac, r->remote_mac)) {
344 VLOG_DBG("remote MAC address changed from "ETH_ADDR_FMT
345 " to "ETH_ADDR_FMT,
346 ETH_ADDR_ARGS(r->last_remote_mac),
347 ETH_ADDR_ARGS(r->remote_mac));
348 memcpy(r->last_remote_mac, r->remote_mac, ETH_ADDR_LEN);
349 }
350 }
351 }
352
353 return any_changes;
354 }
355
356 /* Refreshes the MAC address of the local port into ib->local_mac, if it is due
357 * for a refresh. Returns true if anything changed, otherwise false. */
358 static bool
359 refresh_local(struct in_band *ib)
360 {
361 uint8_t ea[ETH_ADDR_LEN];
362 time_t now;
363
364 now = time_now();
365 if (now < ib->next_local_refresh) {
366 return false;
367 }
368 ib->next_local_refresh = now + 1;
369
370 if (netdev_get_etheraddr(ib->local_netdev, ea)
371 || eth_addr_equals(ea, ib->local_mac)) {
372 return false;
373 }
374
375 memcpy(ib->local_mac, ea, ETH_ADDR_LEN);
376 return true;
377 }
378
379 static void
380 in_band_status_cb(struct status_reply *sr, void *in_band_)
381 {
382 struct in_band *in_band = in_band_;
383
384 if (!eth_addr_is_zero(in_band->local_mac)) {
385 status_reply_put(sr, "local-mac="ETH_ADDR_FMT,
386 ETH_ADDR_ARGS(in_band->local_mac));
387 }
388
389 if (in_band->n_remotes
390 && !eth_addr_is_zero(in_band->remotes[0].remote_mac)) {
391 status_reply_put(sr, "remote-mac="ETH_ADDR_FMT,
392 ETH_ADDR_ARGS(in_band->remotes[0].remote_mac));
393 }
394 }
395
396 /* Returns true if 'packet' should be sent to the local port regardless
397 * of the flow table. */
398 bool
399 in_band_msg_in_hook(struct in_band *in_band, const flow_t *flow,
400 const struct ofpbuf *packet)
401 {
402 if (!in_band) {
403 return false;
404 }
405
406 /* Regardless of how the flow table is configured, we want to be
407 * able to see replies to our DHCP requests. */
408 if (flow->dl_type == htons(ETH_TYPE_IP)
409 && flow->nw_proto == IP_TYPE_UDP
410 && flow->tp_src == htons(DHCP_SERVER_PORT)
411 && flow->tp_dst == htons(DHCP_CLIENT_PORT)
412 && packet->l7) {
413 struct dhcp_header *dhcp;
414
415 dhcp = ofpbuf_at(packet, (char *)packet->l7 - (char *)packet->data,
416 sizeof *dhcp);
417 if (!dhcp) {
418 return false;
419 }
420
421 refresh_local(in_band);
422 if (!eth_addr_is_zero(in_band->local_mac)
423 && eth_addr_equals(dhcp->chaddr, in_band->local_mac)) {
424 return true;
425 }
426 }
427
428 return false;
429 }
430
431 /* Returns true if the rule that would match 'flow' with 'actions' is
432 * allowed to be set up in the datapath. */
433 bool
434 in_band_rule_check(struct in_band *in_band, const flow_t *flow,
435 const struct odp_actions *actions)
436 {
437 if (!in_band) {
438 return true;
439 }
440
441 /* Don't allow flows that would prevent DHCP replies from being seen
442 * by the local port. */
443 if (flow->dl_type == htons(ETH_TYPE_IP)
444 && flow->nw_proto == IP_TYPE_UDP
445 && flow->tp_src == htons(DHCP_SERVER_PORT)
446 && flow->tp_dst == htons(DHCP_CLIENT_PORT)) {
447 int i;
448
449 for (i=0; i<actions->n_actions; i++) {
450 if (actions->actions[i].output.type == ODPAT_OUTPUT
451 && actions->actions[i].output.port == ODPP_LOCAL) {
452 return true;
453 }
454 }
455 return false;
456 }
457
458 return true;
459 }
460
461 static void
462 init_rule(struct in_band_rule *rule, unsigned int priority)
463 {
464 rule->wildcards = OVSFW_ALL;
465 rule->priority = priority;
466
467 /* Not strictly necessary but seems cleaner. */
468 memset(&rule->flow, 0, sizeof rule->flow);
469 }
470
471 static void
472 set_in_port(struct in_band_rule *rule, uint16_t odp_port)
473 {
474 rule->wildcards &= ~OFPFW_IN_PORT;
475 rule->flow.in_port = odp_port;
476 }
477
478 static void
479 set_dl_type(struct in_band_rule *rule, uint16_t dl_type)
480 {
481 rule->wildcards &= ~OFPFW_DL_TYPE;
482 rule->flow.dl_type = dl_type;
483 }
484
485 static void
486 set_dl_src(struct in_band_rule *rule, const uint8_t dl_src[ETH_ADDR_LEN])
487 {
488 rule->wildcards &= ~OFPFW_DL_SRC;
489 memcpy(rule->flow.dl_src, dl_src, ETH_ADDR_LEN);
490 }
491
492 static void
493 set_dl_dst(struct in_band_rule *rule, const uint8_t dl_dst[ETH_ADDR_LEN])
494 {
495 rule->wildcards &= ~OFPFW_DL_DST;
496 memcpy(rule->flow.dl_dst, dl_dst, ETH_ADDR_LEN);
497 }
498
499 static void
500 set_tp_src(struct in_band_rule *rule, uint16_t tp_src)
501 {
502 rule->wildcards &= ~OFPFW_TP_SRC;
503 rule->flow.tp_src = tp_src;
504 }
505
506 static void
507 set_tp_dst(struct in_band_rule *rule, uint16_t tp_dst)
508 {
509 rule->wildcards &= ~OFPFW_TP_DST;
510 rule->flow.tp_dst = tp_dst;
511 }
512
513 static void
514 set_nw_proto(struct in_band_rule *rule, uint8_t nw_proto)
515 {
516 rule->wildcards &= ~OFPFW_NW_PROTO;
517 rule->flow.nw_proto = nw_proto;
518 }
519
520 static void
521 set_nw_src(struct in_band_rule *rule, const struct in_addr nw_src)
522 {
523 rule->wildcards &= ~OFPFW_NW_SRC_MASK;
524 rule->flow.nw_src = nw_src.s_addr;
525 }
526
527 static void
528 set_nw_dst(struct in_band_rule *rule, const struct in_addr nw_dst)
529 {
530 rule->wildcards &= ~OFPFW_NW_DST_MASK;
531 rule->flow.nw_dst = nw_dst.s_addr;
532 }
533
534 static void
535 make_rules(struct in_band *ib,
536 void (*cb)(struct in_band *, const struct in_band_rule *))
537 {
538 struct in_band_rule rule;
539 size_t i;
540
541 if (!eth_addr_is_zero(ib->installed_local_mac)) {
542 /* (a) Allow DHCP requests sent from the local port. */
543 init_rule(&rule, IBR_FROM_LOCAL_DHCP);
544 set_in_port(&rule, ODPP_LOCAL);
545 set_dl_type(&rule, htons(ETH_TYPE_IP));
546 set_dl_src(&rule, ib->installed_local_mac);
547 set_nw_proto(&rule, IP_TYPE_UDP);
548 set_tp_src(&rule, htons(DHCP_CLIENT_PORT));
549 set_tp_dst(&rule, htons(DHCP_SERVER_PORT));
550 cb(ib, &rule);
551
552 /* (b) Allow ARP replies to the local port's MAC address. */
553 init_rule(&rule, IBR_TO_LOCAL_ARP);
554 set_dl_type(&rule, htons(ETH_TYPE_ARP));
555 set_dl_dst(&rule, ib->installed_local_mac);
556 set_nw_proto(&rule, ARP_OP_REPLY);
557 cb(ib, &rule);
558
559 /* (c) Allow ARP requests from the local port's MAC address. */
560 init_rule(&rule, IBR_FROM_LOCAL_ARP);
561 set_dl_type(&rule, htons(ETH_TYPE_ARP));
562 set_dl_src(&rule, ib->installed_local_mac);
563 set_nw_proto(&rule, ARP_OP_REQUEST);
564 cb(ib, &rule);
565 }
566
567 for (i = 0; i < ib->n_remote_macs; i++) {
568 const uint8_t *remote_mac = &ib->remote_macs[i * ETH_ADDR_LEN];
569
570 if (i > 0) {
571 const uint8_t *prev_mac = &ib->remote_macs[(i - 1) * ETH_ADDR_LEN];
572 if (eth_addr_equals(remote_mac, prev_mac)) {
573 /* Skip duplicates. */
574 continue;
575 }
576 }
577
578 /* (d) Allow ARP replies to the next hop's MAC address. */
579 init_rule(&rule, IBR_TO_NEXT_HOP_ARP);
580 set_dl_type(&rule, htons(ETH_TYPE_ARP));
581 set_dl_dst(&rule, remote_mac);
582 set_nw_proto(&rule, ARP_OP_REPLY);
583 cb(ib, &rule);
584
585 /* (e) Allow ARP requests from the next hop's MAC address. */
586 init_rule(&rule, IBR_FROM_NEXT_HOP_ARP);
587 set_dl_type(&rule, htons(ETH_TYPE_ARP));
588 set_dl_src(&rule, remote_mac);
589 set_nw_proto(&rule, ARP_OP_REQUEST);
590 cb(ib, &rule);
591 }
592
593 for (i = 0; i < ib->n_remote_addrs; i++) {
594 const struct sockaddr_in *a = &ib->remote_addrs[i];
595
596 if (!i || a->sin_addr.s_addr != a[-1].sin_addr.s_addr) {
597 /* (f) Allow ARP replies containing the remote's IP address as a
598 * target. */
599 init_rule(&rule, IBR_TO_REMOTE_ARP);
600 set_dl_type(&rule, htons(ETH_TYPE_ARP));
601 set_nw_proto(&rule, ARP_OP_REPLY);
602 set_nw_dst(&rule, a->sin_addr);
603 cb(ib, &rule);
604
605 /* (g) Allow ARP requests containing the remote's IP address as a
606 * source. */
607 init_rule(&rule, IBR_FROM_REMOTE_ARP);
608 set_dl_type(&rule, htons(ETH_TYPE_ARP));
609 set_nw_proto(&rule, ARP_OP_REQUEST);
610 set_nw_src(&rule, a->sin_addr);
611 cb(ib, &rule);
612 }
613
614 if (!i
615 || a->sin_addr.s_addr != a[-1].sin_addr.s_addr
616 || a->sin_port != a[-1].sin_port) {
617 /* (h) Allow TCP traffic to the remote's IP and port. */
618 init_rule(&rule, IBR_TO_REMOTE_TCP);
619 set_dl_type(&rule, htons(ETH_TYPE_IP));
620 set_nw_proto(&rule, IP_TYPE_TCP);
621 set_nw_dst(&rule, a->sin_addr);
622 set_tp_dst(&rule, a->sin_port);
623 cb(ib, &rule);
624
625 /* (i) Allow TCP traffic from the remote's IP and port. */
626 init_rule(&rule, IBR_FROM_REMOTE_TCP);
627 set_dl_type(&rule, htons(ETH_TYPE_IP));
628 set_nw_proto(&rule, IP_TYPE_TCP);
629 set_nw_src(&rule, a->sin_addr);
630 set_tp_src(&rule, a->sin_port);
631 cb(ib, &rule);
632 }
633 }
634 }
635
636 static void
637 drop_rule(struct in_band *ib, const struct in_band_rule *rule)
638 {
639 ofproto_delete_flow(ib->ofproto, &rule->flow,
640 rule->wildcards, rule->priority);
641 }
642
643 /* Drops from the flow table all of the flows set up by 'ib', then clears out
644 * the information about the installed flows so that they can be filled in
645 * again if necessary. */
646 static void
647 drop_rules(struct in_band *ib)
648 {
649 /* Drop rules. */
650 make_rules(ib, drop_rule);
651
652 /* Clear out state. */
653 memset(ib->installed_local_mac, 0, sizeof ib->installed_local_mac);
654
655 free(ib->remote_addrs);
656 ib->remote_addrs = NULL;
657 ib->n_remote_addrs = 0;
658
659 free(ib->remote_macs);
660 ib->remote_macs = NULL;
661 ib->n_remote_macs = 0;
662 }
663
664 static void
665 add_rule(struct in_band *ib, const struct in_band_rule *rule)
666 {
667 union ofp_action action;
668
669 action.type = htons(OFPAT_OUTPUT);
670 action.output.len = htons(sizeof action);
671 action.output.port = htons(OFPP_NORMAL);
672 action.output.max_len = htons(0);
673 ofproto_add_flow(ib->ofproto, &rule->flow, rule->wildcards,
674 rule->priority, &action, 1, 0);
675 }
676
677 /* Inserts flows into the flow table for the current state of 'ib'. */
678 static void
679 add_rules(struct in_band *ib)
680 {
681 make_rules(ib, add_rule);
682 }
683
684 static int
685 compare_addrs(const void *a_, const void *b_)
686 {
687 const struct sockaddr_in *a = a_;
688 const struct sockaddr_in *b = b_;
689 int cmp;
690
691 cmp = memcmp(&a->sin_addr.s_addr,
692 &b->sin_addr.s_addr,
693 sizeof a->sin_addr.s_addr);
694 if (cmp) {
695 return cmp;
696 }
697 return memcmp(&a->sin_port, &b->sin_port, sizeof a->sin_port);
698 }
699
700 static int
701 compare_macs(const void *a, const void *b)
702 {
703 return memcmp(a, b, ETH_ADDR_LEN);
704 }
705
706 void
707 in_band_run(struct in_band *ib)
708 {
709 struct in_band_remote *r;
710 bool local_change, remote_change;
711
712 local_change = refresh_local(ib);
713 remote_change = refresh_remotes(ib);
714 if (!local_change && !remote_change) {
715 /* Nothing changed, nothing to do. */
716 return;
717 }
718
719 /* Drop old rules. */
720 drop_rules(ib);
721
722 /* Figure out new rules. */
723 memcpy(ib->installed_local_mac, ib->local_mac, ETH_ADDR_LEN);
724 ib->remote_addrs = xmalloc(ib->n_remotes * sizeof *ib->remote_addrs);
725 ib->n_remote_addrs = 0;
726 ib->remote_macs = xmalloc(ib->n_remotes * ETH_ADDR_LEN);
727 ib->n_remote_macs = 0;
728 for (r = ib->remotes; r < &ib->remotes[ib->n_remotes]; r++) {
729 ib->remote_addrs[ib->n_remote_addrs++] = r->remote_addr;
730 if (!eth_addr_is_zero(r->remote_mac)) {
731 memcpy(&ib->remote_macs[ib->n_remote_macs * ETH_ADDR_LEN],
732 r->remote_mac, ETH_ADDR_LEN);
733 ib->n_remote_macs++;
734 }
735 }
736
737 /* Sort, to allow make_rules() to easily skip duplicates. */
738 qsort(ib->remote_addrs, ib->n_remote_addrs, sizeof *ib->remote_addrs,
739 compare_addrs);
740 qsort(ib->remote_macs, ib->n_remote_macs, ETH_ADDR_LEN, compare_macs);
741
742 /* Add new rules. */
743 add_rules(ib);
744 }
745
746 void
747 in_band_wait(struct in_band *in_band)
748 {
749 long long int wakeup
750 = MIN(in_band->next_remote_refresh, in_band->next_local_refresh);
751 poll_timer_wait_until(wakeup * 1000);
752 }
753
754 /* ofproto has flushed all flows from the flow table and it is calling us back
755 * to allow us to reinstall the ones that are important to us. */
756 void
757 in_band_flushed(struct in_band *in_band)
758 {
759 add_rules(in_band);
760 }
761
762 int
763 in_band_create(struct ofproto *ofproto, struct dpif *dpif,
764 struct switch_status *ss, struct in_band **in_bandp)
765 {
766 struct in_band *in_band;
767 char local_name[IF_NAMESIZE];
768 struct netdev *local_netdev;
769 int error;
770
771 error = dpif_port_get_name(dpif, ODPP_LOCAL,
772 local_name, sizeof local_name);
773 if (error) {
774 VLOG_ERR("failed to initialize in-band control: cannot get name "
775 "of datapath local port (%s)", strerror(error));
776 return error;
777 }
778
779 error = netdev_open_default(local_name, &local_netdev);
780 if (error) {
781 VLOG_ERR("failed to initialize in-band control: cannot open "
782 "datapath local port %s (%s)", local_name, strerror(error));
783 return error;
784 }
785
786 in_band = xzalloc(sizeof *in_band);
787 in_band->ofproto = ofproto;
788 in_band->ss_cat = switch_status_register(ss, "in-band",
789 in_band_status_cb, in_band);
790 in_band->next_remote_refresh = TIME_MIN;
791 in_band->next_local_refresh = TIME_MIN;
792 in_band->local_netdev = local_netdev;
793
794 *in_bandp = in_band;
795
796 return 0;
797 }
798
799 void
800 in_band_destroy(struct in_band *ib)
801 {
802 if (ib) {
803 drop_rules(ib);
804 in_band_set_remotes(ib, NULL, 0);
805 switch_status_unregister(ib->ss_cat);
806 netdev_close(ib->local_netdev);
807 free(ib);
808 }
809 }
810
811 static bool
812 any_addresses_changed(struct in_band *ib,
813 const struct sockaddr_in *addresses, size_t n)
814 {
815 size_t i;
816
817 if (n != ib->n_remotes) {
818 return true;
819 }
820
821 for (i = 0; i < n; i++) {
822 const struct sockaddr_in *old = &ib->remotes[i].remote_addr;
823 const struct sockaddr_in *new = &addresses[i];
824
825 if (old->sin_addr.s_addr != new->sin_addr.s_addr ||
826 old->sin_port != new->sin_port) {
827 return true;
828 }
829 }
830
831 return false;
832 }
833
834 void
835 in_band_set_remotes(struct in_band *ib,
836 const struct sockaddr_in *addresses, size_t n)
837 {
838 size_t i;
839
840 if (!any_addresses_changed(ib, addresses, n)) {
841 return;
842 }
843
844 /* Clear old remotes. */
845 for (i = 0; i < ib->n_remotes; i++) {
846 netdev_close(ib->remotes[i].remote_netdev);
847 }
848 free(ib->remotes);
849
850 /* Set up new remotes. */
851 ib->remotes = n ? xzalloc(n * sizeof *ib->remotes) : NULL;
852 ib->n_remotes = n;
853 for (i = 0; i < n; i++) {
854 ib->remotes[i].remote_addr = addresses[i];
855 }
856
857 /* Force refresh in next call to in_band_run(). */
858 ib->next_remote_refresh = TIME_MIN;
859 }