]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
6235b3de | 2 | /*** |
96b2fb93 LP |
3 | Copyright © 2014 Tom Gundersen <teg@jklm.no> |
4 | Copyright © 2014 Susant Sahani | |
6235b3de TG |
5 | ***/ |
6 | ||
540eb5f0 | 7 | #include <net/if.h> |
6235b3de | 8 | |
6235b3de | 9 | #include "missing.h" |
540eb5f0 | 10 | #include "netlink-util.h" |
441e9ae4 | 11 | #include "netdev/bridge.h" |
23f53b99 | 12 | #include "networkd-manager.h" |
0d6c68eb | 13 | #include "vlan-util.h" |
540eb5f0 SS |
14 | |
15 | /* callback for brige netdev's parameter set */ | |
16 | static int netdev_bridge_set_handler(sd_netlink *rtnl, sd_netlink_message *m, void *userdata) { | |
8e766630 | 17 | _cleanup_(netdev_unrefp) NetDev *netdev = userdata; |
540eb5f0 SS |
18 | int r; |
19 | ||
20 | assert(netdev); | |
21 | assert(m); | |
22 | ||
23 | r = sd_netlink_message_get_errno(m); | |
24 | if (r < 0) { | |
25 | log_netdev_warning_errno(netdev, r, "Bridge parameters could not be set: %m"); | |
26 | return 1; | |
27 | } | |
28 | ||
82936769 | 29 | log_netdev_debug(netdev, "Bridge parameters set success"); |
540eb5f0 SS |
30 | |
31 | return 1; | |
32 | } | |
33 | ||
34 | static int netdev_bridge_post_create(NetDev *netdev, Link *link, sd_netlink_message *m) { | |
4afd3348 | 35 | _cleanup_(sd_netlink_message_unrefp) sd_netlink_message *req = NULL; |
540eb5f0 SS |
36 | Bridge *b; |
37 | int r; | |
38 | ||
39 | assert(netdev); | |
40 | ||
41 | b = BRIDGE(netdev); | |
42 | ||
43 | assert(b); | |
44 | ||
45 | r = sd_rtnl_message_new_link(netdev->manager->rtnl, &req, RTM_NEWLINK, netdev->ifindex); | |
46 | if (r < 0) | |
47 | return log_netdev_error_errno(netdev, r, "Could not allocate RTM_SETLINK message: %m"); | |
48 | ||
49 | r = sd_netlink_message_set_flags(req, NLM_F_REQUEST | NLM_F_ACK); | |
50 | if (r < 0) | |
51 | return log_link_error_errno(link, r, "Could not set netlink flags: %m"); | |
52 | ||
53 | r = sd_netlink_message_open_container(req, IFLA_LINKINFO); | |
54 | if (r < 0) | |
55 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_PROTINFO attribute: %m"); | |
56 | ||
57 | r = sd_netlink_message_open_container_union(req, IFLA_INFO_DATA, netdev_kind_to_string(netdev->kind)); | |
58 | if (r < 0) | |
59 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
60 | ||
1a14863e | 61 | /* convert to jiffes */ |
730389b6 | 62 | if (b->forward_delay != USEC_INFINITY) { |
1a14863e | 63 | r = sd_netlink_message_append_u32(req, IFLA_BR_FORWARD_DELAY, usec_to_jiffies(b->forward_delay)); |
540eb5f0 SS |
64 | if (r < 0) |
65 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_FORWARD_DELAY attribute: %m"); | |
66 | } | |
67 | ||
68 | if (b->hello_time > 0) { | |
1a14863e | 69 | r = sd_netlink_message_append_u32(req, IFLA_BR_HELLO_TIME, usec_to_jiffies(b->hello_time)); |
540eb5f0 SS |
70 | if (r < 0) |
71 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_HELLO_TIME attribute: %m"); | |
72 | } | |
73 | ||
74 | if (b->max_age > 0) { | |
1a14863e | 75 | r = sd_netlink_message_append_u32(req, IFLA_BR_MAX_AGE, usec_to_jiffies(b->max_age)); |
540eb5f0 SS |
76 | if (r < 0) |
77 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MAX_AGE attribute: %m"); | |
78 | } | |
79 | ||
0da81203 | 80 | if (b->ageing_time != USEC_INFINITY) { |
c7440e74 TJ |
81 | r = sd_netlink_message_append_u32(req, IFLA_BR_AGEING_TIME, usec_to_jiffies(b->ageing_time)); |
82 | if (r < 0) | |
83 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_AGEING_TIME attribute: %m"); | |
84 | } | |
85 | ||
86 | if (b->priority > 0) { | |
87 | r = sd_netlink_message_append_u16(req, IFLA_BR_PRIORITY, b->priority); | |
88 | if (r < 0) | |
89 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_PRIORITY attribute: %m"); | |
90 | } | |
91 | ||
c4819961 JC |
92 | if (b->group_fwd_mask > 0) { |
93 | r = sd_netlink_message_append_u16(req, IFLA_BR_GROUP_FWD_MASK, b->group_fwd_mask); | |
94 | if (r < 0) | |
95 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_GROUP_FWD_MASK attribute: %m"); | |
96 | } | |
97 | ||
0d6c68eb | 98 | if (b->default_pvid != VLANID_INVALID) { |
c7440e74 TJ |
99 | r = sd_netlink_message_append_u16(req, IFLA_BR_VLAN_DEFAULT_PVID, b->default_pvid); |
100 | if (r < 0) | |
101 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_DEFAULT_PVID attribute: %m"); | |
102 | } | |
103 | ||
3fef7a3f SS |
104 | if (b->mcast_querier >= 0) { |
105 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_QUERIER, b->mcast_querier); | |
106 | if (r < 0) | |
107 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_QUERIER attribute: %m"); | |
108 | } | |
109 | ||
6df6d898 SS |
110 | if (b->mcast_snooping >= 0) { |
111 | r = sd_netlink_message_append_u8(req, IFLA_BR_MCAST_SNOOPING, b->mcast_snooping); | |
112 | if (r < 0) | |
113 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_MCAST_SNOOPING attribute: %m"); | |
114 | } | |
115 | ||
c6f8d17d TJ |
116 | if (b->vlan_filtering >= 0) { |
117 | r = sd_netlink_message_append_u8(req, IFLA_BR_VLAN_FILTERING, b->vlan_filtering); | |
118 | if (r < 0) | |
119 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_VLAN_FILTERING attribute: %m"); | |
120 | } | |
121 | ||
b760a9af SS |
122 | if (b->stp >= 0) { |
123 | r = sd_netlink_message_append_u32(req, IFLA_BR_STP_STATE, b->stp); | |
124 | if (r < 0) | |
125 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_BR_STP_STATE attribute: %m"); | |
126 | } | |
127 | ||
540eb5f0 SS |
128 | r = sd_netlink_message_close_container(req); |
129 | if (r < 0) | |
130 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_LINKINFO attribute: %m"); | |
131 | ||
132 | r = sd_netlink_message_close_container(req); | |
133 | if (r < 0) | |
134 | return log_netdev_error_errno(netdev, r, "Could not append IFLA_INFO_DATA attribute: %m"); | |
135 | ||
136 | r = sd_netlink_call_async(netdev->manager->rtnl, req, netdev_bridge_set_handler, netdev, 0, NULL); | |
137 | if (r < 0) | |
138 | return log_netdev_error_errno(netdev, r, "Could not send rtnetlink message: %m"); | |
139 | ||
140 | netdev_ref(netdev); | |
141 | ||
142 | return r; | |
143 | } | |
6235b3de | 144 | |
3fef7a3f SS |
145 | static void bridge_init(NetDev *n) { |
146 | Bridge *b; | |
147 | ||
148 | b = BRIDGE(n); | |
149 | ||
150 | assert(b); | |
151 | ||
152 | b->mcast_querier = -1; | |
6df6d898 | 153 | b->mcast_snooping = -1; |
c6f8d17d | 154 | b->vlan_filtering = -1; |
b760a9af | 155 | b->stp = -1; |
0d6c68eb | 156 | b->default_pvid = VLANID_INVALID; |
730389b6 | 157 | b->forward_delay = USEC_INFINITY; |
0da81203 | 158 | b->ageing_time = USEC_INFINITY; |
3fef7a3f SS |
159 | } |
160 | ||
3be1d7e0 | 161 | const NetDevVTable bridge_vtable = { |
aa9f1140 | 162 | .object_size = sizeof(Bridge), |
3fef7a3f | 163 | .init = bridge_init, |
540eb5f0 SS |
164 | .sections = "Match\0NetDev\0Bridge\0", |
165 | .post_create = netdev_bridge_post_create, | |
aa9f1140 | 166 | .create_type = NETDEV_CREATE_MASTER, |
3be1d7e0 | 167 | }; |