GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usr.sbin/switchctl/../switchd/packet.c Lines: 0 29 0.0 %
Date: 2017-11-13 Branches: 0 24 0.0 %

Line Branch Exec Source
1
/*	$OpenBSD: packet.c,v 1.5 2017/08/06 17:31:19 rob Exp $	*/
2
3
/*
4
 * Copyright (c) 2013-2016 Reyk Floeter <reyk@openbsd.org>
5
 *
6
 * Permission to use, copy, modify, and distribute this software for any
7
 * purpose with or without fee is hereby granted, provided that the above
8
 * copyright notice and this permission notice appear in all copies.
9
 *
10
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17
 */
18
19
#include <sys/types.h>
20
#include <sys/socket.h>
21
22
#include <net/if.h>
23
#include <net/if_arp.h>
24
#include <netinet/in.h>
25
#include <netinet/if_ether.h>
26
#include <netinet/tcp.h>
27
28
#include <stdio.h>
29
#include <stdlib.h>
30
#include <string.h>
31
#include <fcntl.h>
32
#include <pwd.h>
33
#include <event.h>
34
35
#include "switchd.h"
36
37
const uint8_t etherbroadcastaddr[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
38
const uint8_t etherzeroaddr[] = { 0, 0, 0, 0, 0, 0 };
39
40
int	 packet_ether_unicast(uint8_t *);
41
42
int
43
packet_ether_unicast(uint8_t *ea)
44
{
45
	if (memcmp(ea, etherbroadcastaddr, ETHER_ADDR_LEN) == 0 ||
46
	    memcmp(ea, etherzeroaddr, ETHER_ADDR_LEN) == 0 ||
47
	    ETHER_IS_MULTICAST(ea))
48
		return (-1);
49
	return (0);
50
}
51
52
int
53
packet_input(struct switchd *sc, struct switch_control *sw, uint32_t srcport,
54
    uint32_t *dstport, struct ibuf *ibuf, size_t len, struct packet *pkt)
55
{
56
	struct ether_header	*eh;
57
	struct macaddr		*src, *dst;
58
59
	if (sw == NULL)
60
		return (-1);
61
	if (len < sizeof(*eh))
62
		return (-1);
63
64
	pkt->pkt_len = ibuf_dataleft(ibuf);
65
	if ((eh = ibuf_getdata(ibuf, sizeof(*eh))) == NULL) {
66
		log_debug("short packet");
67
		return (-1);
68
	}
69
	len -= sizeof(*eh);
70
71
	if ((packet_ether_unicast(eh->ether_shost) == -1) ||
72
	    (src = switch_learn(sc, sw, eh->ether_shost, srcport)) == NULL)
73
		return (-1);
74
75
	if (packet_ether_unicast(eh->ether_dhost) == -1)
76
		dst = NULL;
77
	else
78
		dst = switch_cached(sw, eh->ether_dhost);
79
80
	log_debug("%s: %s -> %s, port %u -> %u", __func__,
81
	    print_ether(eh->ether_shost),
82
	    print_ether(eh->ether_dhost),
83
	    src->mac_port,
84
	    dst == NULL ? OFP_PORT_ANY : dst->mac_port);
85
86
	if (dstport)
87
		*dstport = dst == NULL ? OFP_PORT_ANY : dst->mac_port;
88
89
	pkt->pkt_eh = eh;
90
	pkt->pkt_buf = (uint8_t *)eh;
91
92
	return (0);
93
}