1 /* SPDX-License-Identifier: LGPL-2.1-or-later
2 * Copyright © 2020 VMware, Inc. */
3
4 #include <netinet/in.h>
5 #include <linux/if_arp.h>
6
7 #include "bareudp.h"
8 #include "netlink-util.h"
9 #include "networkd-manager.h"
10 #include "string-table.h"
11
12 static const char* const bare_udp_protocol_table[_BARE_UDP_PROTOCOL_MAX] = {
13 [BARE_UDP_PROTOCOL_IPV4] = "ipv4",
14 [BARE_UDP_PROTOCOL_IPV6] = "ipv6",
15 [BARE_UDP_PROTOCOL_MPLS_UC] = "mpls-uc",
16 [BARE_UDP_PROTOCOL_MPLS_MC] = "mpls-mc",
17 };
18
19 DEFINE_STRING_TABLE_LOOKUP(bare_udp_protocol, BareUDPProtocol);
20 DEFINE_CONFIG_PARSE_ENUM(config_parse_bare_udp_iftype, bare_udp_protocol, BareUDPProtocol,
21 "Failed to parse EtherType=");
22
netdev_bare_udp_fill_message_create(NetDev * netdev,Link * link,sd_netlink_message * m)23 static int netdev_bare_udp_fill_message_create(NetDev *netdev, Link *link, sd_netlink_message *m) {
24 BareUDP *u;
25 int r;
26
27 assert(netdev);
28 assert(m);
29
30 u = BAREUDP(netdev);
31
32 assert(u);
33
34 r = sd_netlink_message_append_u16(m, IFLA_BAREUDP_ETHERTYPE, htobe16(u->iftype));
35 if (r < 0)
36 return r;
37
38 r = sd_netlink_message_append_u16(m, IFLA_BAREUDP_PORT, htobe16(u->dest_port));
39 if (r < 0)
40 return r;
41
42 return 0;
43 }
44
netdev_bare_udp_verify(NetDev * netdev,const char * filename)45 static int netdev_bare_udp_verify(NetDev *netdev, const char *filename) {
46 BareUDP *u;
47
48 assert(netdev);
49 assert(filename);
50
51 u = BAREUDP(netdev);
52
53 assert(u);
54
55 if (u->dest_port == 0)
56 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
57 "%s: BareUDP DesinationPort= is not set. Ignoring.", filename);
58
59 if (u->iftype == _BARE_UDP_PROTOCOL_INVALID)
60 return log_netdev_warning_errno(netdev, SYNTHETIC_ERRNO(EINVAL),
61 "%s: BareUDP EtherType= is not set. Ignoring.", filename);
62
63 return 0;
64 }
65
bare_udp_init(NetDev * netdev)66 static void bare_udp_init(NetDev *netdev) {
67 BareUDP *u;
68
69 assert(netdev);
70
71 u = BAREUDP(netdev);
72
73 assert(u);
74
75 u->iftype = _BARE_UDP_PROTOCOL_INVALID;
76 }
77
78 const NetDevVTable bare_udp_vtable = {
79 .object_size = sizeof(BareUDP),
80 .sections = NETDEV_COMMON_SECTIONS "BareUDP\0",
81 .init = bare_udp_init,
82 .config_verify = netdev_bare_udp_verify,
83 .fill_message_create = netdev_bare_udp_fill_message_create,
84 .create_type = NETDEV_CREATE_INDEPENDENT,
85 .iftype = ARPHRD_NONE,
86 };
87