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