xref: /linux/net/dcb/dcbnl.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Copyright (c) 2008-2011, Intel Corporation.
4  *
5  * Description: Data Center Bridging netlink interface
6  * Author: Lucy Liu <lucy.liu@intel.com>
7  */
8 
9 #include <linux/netdevice.h>
10 #include <linux/netlink.h>
11 #include <linux/slab.h>
12 #include <net/netlink.h>
13 #include <net/rtnetlink.h>
14 #include <linux/dcbnl.h>
15 #include <net/dcbevent.h>
16 #include <linux/rtnetlink.h>
17 #include <linux/init.h>
18 #include <net/sock.h>
19 
20 /* Data Center Bridging (DCB) is a collection of Ethernet enhancements
21  * intended to allow network traffic with differing requirements
22  * (highly reliable, no drops vs. best effort vs. low latency) to operate
23  * and co-exist on Ethernet.  Current DCB features are:
24  *
25  * Enhanced Transmission Selection (aka Priority Grouping [PG]) - provides a
26  *   framework for assigning bandwidth guarantees to traffic classes.
27  *
28  * Priority-based Flow Control (PFC) - provides a flow control mechanism which
29  *   can work independently for each 802.1p priority.
30  *
31  * Congestion Notification - provides a mechanism for end-to-end congestion
32  *   control for protocols which do not have built-in congestion management.
33  *
34  * More information about the emerging standards for these Ethernet features
35  * can be found at: http://www.ieee802.org/1/pages/dcbridges.html
36  *
37  * This file implements an rtnetlink interface to allow configuration of DCB
38  * features for capable devices.
39  */
40 
41 /**************** DCB attribute policies *************************************/
42 
43 /* DCB netlink attributes policy */
44 static const struct nla_policy dcbnl_rtnl_policy[DCB_ATTR_MAX + 1] = {
45 	[DCB_ATTR_IFNAME]      = {.type = NLA_NUL_STRING, .len = IFNAMSIZ - 1},
46 	[DCB_ATTR_STATE]       = {.type = NLA_U8},
47 	[DCB_ATTR_PFC_CFG]     = {.type = NLA_NESTED},
48 	[DCB_ATTR_PG_CFG]      = {.type = NLA_NESTED},
49 	[DCB_ATTR_SET_ALL]     = {.type = NLA_U8},
50 	[DCB_ATTR_PERM_HWADDR] = {.type = NLA_FLAG},
51 	[DCB_ATTR_CAP]         = {.type = NLA_NESTED},
52 	[DCB_ATTR_PFC_STATE]   = {.type = NLA_U8},
53 	[DCB_ATTR_BCN]         = {.type = NLA_NESTED},
54 	[DCB_ATTR_APP]         = {.type = NLA_NESTED},
55 	[DCB_ATTR_IEEE]	       = {.type = NLA_NESTED},
56 	[DCB_ATTR_DCBX]        = {.type = NLA_U8},
57 	[DCB_ATTR_FEATCFG]     = {.type = NLA_NESTED},
58 };
59 
60 /* DCB priority flow control to User Priority nested attributes */
61 static const struct nla_policy dcbnl_pfc_up_nest[DCB_PFC_UP_ATTR_MAX + 1] = {
62 	[DCB_PFC_UP_ATTR_0]   = {.type = NLA_U8},
63 	[DCB_PFC_UP_ATTR_1]   = {.type = NLA_U8},
64 	[DCB_PFC_UP_ATTR_2]   = {.type = NLA_U8},
65 	[DCB_PFC_UP_ATTR_3]   = {.type = NLA_U8},
66 	[DCB_PFC_UP_ATTR_4]   = {.type = NLA_U8},
67 	[DCB_PFC_UP_ATTR_5]   = {.type = NLA_U8},
68 	[DCB_PFC_UP_ATTR_6]   = {.type = NLA_U8},
69 	[DCB_PFC_UP_ATTR_7]   = {.type = NLA_U8},
70 	[DCB_PFC_UP_ATTR_ALL] = {.type = NLA_FLAG},
71 };
72 
73 /* DCB priority grouping nested attributes */
74 static const struct nla_policy dcbnl_pg_nest[DCB_PG_ATTR_MAX + 1] = {
75 	[DCB_PG_ATTR_TC_0]      = {.type = NLA_NESTED},
76 	[DCB_PG_ATTR_TC_1]      = {.type = NLA_NESTED},
77 	[DCB_PG_ATTR_TC_2]      = {.type = NLA_NESTED},
78 	[DCB_PG_ATTR_TC_3]      = {.type = NLA_NESTED},
79 	[DCB_PG_ATTR_TC_4]      = {.type = NLA_NESTED},
80 	[DCB_PG_ATTR_TC_5]      = {.type = NLA_NESTED},
81 	[DCB_PG_ATTR_TC_6]      = {.type = NLA_NESTED},
82 	[DCB_PG_ATTR_TC_7]      = {.type = NLA_NESTED},
83 	[DCB_PG_ATTR_TC_ALL]    = {.type = NLA_NESTED},
84 	[DCB_PG_ATTR_BW_ID_0]   = {.type = NLA_U8},
85 	[DCB_PG_ATTR_BW_ID_1]   = {.type = NLA_U8},
86 	[DCB_PG_ATTR_BW_ID_2]   = {.type = NLA_U8},
87 	[DCB_PG_ATTR_BW_ID_3]   = {.type = NLA_U8},
88 	[DCB_PG_ATTR_BW_ID_4]   = {.type = NLA_U8},
89 	[DCB_PG_ATTR_BW_ID_5]   = {.type = NLA_U8},
90 	[DCB_PG_ATTR_BW_ID_6]   = {.type = NLA_U8},
91 	[DCB_PG_ATTR_BW_ID_7]   = {.type = NLA_U8},
92 	[DCB_PG_ATTR_BW_ID_ALL] = {.type = NLA_FLAG},
93 };
94 
95 /* DCB traffic class nested attributes. */
96 static const struct nla_policy dcbnl_tc_param_nest[DCB_TC_ATTR_PARAM_MAX + 1] = {
97 	[DCB_TC_ATTR_PARAM_PGID]            = {.type = NLA_U8},
98 	[DCB_TC_ATTR_PARAM_UP_MAPPING]      = {.type = NLA_U8},
99 	[DCB_TC_ATTR_PARAM_STRICT_PRIO]     = {.type = NLA_U8},
100 	[DCB_TC_ATTR_PARAM_BW_PCT]          = {.type = NLA_U8},
101 	[DCB_TC_ATTR_PARAM_ALL]             = {.type = NLA_FLAG},
102 };
103 
104 /* DCB capabilities nested attributes. */
105 static const struct nla_policy dcbnl_cap_nest[DCB_CAP_ATTR_MAX + 1] = {
106 	[DCB_CAP_ATTR_ALL]     = {.type = NLA_FLAG},
107 	[DCB_CAP_ATTR_PG]      = {.type = NLA_U8},
108 	[DCB_CAP_ATTR_PFC]     = {.type = NLA_U8},
109 	[DCB_CAP_ATTR_UP2TC]   = {.type = NLA_U8},
110 	[DCB_CAP_ATTR_PG_TCS]  = {.type = NLA_U8},
111 	[DCB_CAP_ATTR_PFC_TCS] = {.type = NLA_U8},
112 	[DCB_CAP_ATTR_GSP]     = {.type = NLA_U8},
113 	[DCB_CAP_ATTR_BCN]     = {.type = NLA_U8},
114 	[DCB_CAP_ATTR_DCBX]    = {.type = NLA_U8},
115 };
116 
117 /* DCB capabilities nested attributes. */
118 static const struct nla_policy dcbnl_numtcs_nest[DCB_NUMTCS_ATTR_MAX + 1] = {
119 	[DCB_NUMTCS_ATTR_ALL]     = {.type = NLA_FLAG},
120 	[DCB_NUMTCS_ATTR_PG]      = {.type = NLA_U8},
121 	[DCB_NUMTCS_ATTR_PFC]     = {.type = NLA_U8},
122 };
123 
124 /* DCB BCN nested attributes. */
125 static const struct nla_policy dcbnl_bcn_nest[DCB_BCN_ATTR_MAX + 1] = {
126 	[DCB_BCN_ATTR_RP_0]         = {.type = NLA_U8},
127 	[DCB_BCN_ATTR_RP_1]         = {.type = NLA_U8},
128 	[DCB_BCN_ATTR_RP_2]         = {.type = NLA_U8},
129 	[DCB_BCN_ATTR_RP_3]         = {.type = NLA_U8},
130 	[DCB_BCN_ATTR_RP_4]         = {.type = NLA_U8},
131 	[DCB_BCN_ATTR_RP_5]         = {.type = NLA_U8},
132 	[DCB_BCN_ATTR_RP_6]         = {.type = NLA_U8},
133 	[DCB_BCN_ATTR_RP_7]         = {.type = NLA_U8},
134 	[DCB_BCN_ATTR_RP_ALL]       = {.type = NLA_FLAG},
135 	[DCB_BCN_ATTR_BCNA_0]       = {.type = NLA_U32},
136 	[DCB_BCN_ATTR_BCNA_1]       = {.type = NLA_U32},
137 	[DCB_BCN_ATTR_ALPHA]        = {.type = NLA_U32},
138 	[DCB_BCN_ATTR_BETA]         = {.type = NLA_U32},
139 	[DCB_BCN_ATTR_GD]           = {.type = NLA_U32},
140 	[DCB_BCN_ATTR_GI]           = {.type = NLA_U32},
141 	[DCB_BCN_ATTR_TMAX]         = {.type = NLA_U32},
142 	[DCB_BCN_ATTR_TD]           = {.type = NLA_U32},
143 	[DCB_BCN_ATTR_RMIN]         = {.type = NLA_U32},
144 	[DCB_BCN_ATTR_W]            = {.type = NLA_U32},
145 	[DCB_BCN_ATTR_RD]           = {.type = NLA_U32},
146 	[DCB_BCN_ATTR_RU]           = {.type = NLA_U32},
147 	[DCB_BCN_ATTR_WRTT]         = {.type = NLA_U32},
148 	[DCB_BCN_ATTR_RI]           = {.type = NLA_U32},
149 	[DCB_BCN_ATTR_C]            = {.type = NLA_U32},
150 	[DCB_BCN_ATTR_ALL]          = {.type = NLA_FLAG},
151 };
152 
153 /* DCB APP nested attributes. */
154 static const struct nla_policy dcbnl_app_nest[DCB_APP_ATTR_MAX + 1] = {
155 	[DCB_APP_ATTR_IDTYPE]       = {.type = NLA_U8},
156 	[DCB_APP_ATTR_ID]           = {.type = NLA_U16},
157 	[DCB_APP_ATTR_PRIORITY]     = {.type = NLA_U8},
158 };
159 
160 /* IEEE 802.1Qaz nested attributes. */
161 static const struct nla_policy dcbnl_ieee_policy[DCB_ATTR_IEEE_MAX + 1] = {
162 	[DCB_ATTR_IEEE_ETS]	    = {.len = sizeof(struct ieee_ets)},
163 	[DCB_ATTR_IEEE_PFC]	    = {.len = sizeof(struct ieee_pfc)},
164 	[DCB_ATTR_IEEE_APP_TABLE]   = {.type = NLA_NESTED},
165 	[DCB_ATTR_IEEE_MAXRATE]   = {.len = sizeof(struct ieee_maxrate)},
166 	[DCB_ATTR_IEEE_QCN]         = {.len = sizeof(struct ieee_qcn)},
167 	[DCB_ATTR_IEEE_QCN_STATS]   = {.len = sizeof(struct ieee_qcn_stats)},
168 	[DCB_ATTR_DCB_BUFFER]       = {.len = sizeof(struct dcbnl_buffer)},
169 	[DCB_ATTR_DCB_APP_TRUST_TABLE] = {.type = NLA_NESTED},
170 };
171 
172 /* DCB number of traffic classes nested attributes. */
173 static const struct nla_policy dcbnl_featcfg_nest[DCB_FEATCFG_ATTR_MAX + 1] = {
174 	[DCB_FEATCFG_ATTR_ALL]      = {.type = NLA_FLAG},
175 	[DCB_FEATCFG_ATTR_PG]       = {.type = NLA_U8},
176 	[DCB_FEATCFG_ATTR_PFC]      = {.type = NLA_U8},
177 	[DCB_FEATCFG_ATTR_APP]      = {.type = NLA_U8},
178 };
179 
180 static LIST_HEAD(dcb_app_list);
181 static LIST_HEAD(dcb_rewr_list);
182 static DEFINE_SPINLOCK(dcb_lock);
183 
184 static enum ieee_attrs_app dcbnl_app_attr_type_get(u8 selector)
185 {
186 	switch (selector) {
187 	case IEEE_8021QAZ_APP_SEL_ETHERTYPE:
188 	case IEEE_8021QAZ_APP_SEL_STREAM:
189 	case IEEE_8021QAZ_APP_SEL_DGRAM:
190 	case IEEE_8021QAZ_APP_SEL_ANY:
191 	case IEEE_8021QAZ_APP_SEL_DSCP:
192 		return DCB_ATTR_IEEE_APP;
193 	case DCB_APP_SEL_PCP:
194 		return DCB_ATTR_DCB_APP;
195 	default:
196 		return DCB_ATTR_IEEE_APP_UNSPEC;
197 	}
198 }
199 
200 static bool dcbnl_app_attr_type_validate(enum ieee_attrs_app type)
201 {
202 	switch (type) {
203 	case DCB_ATTR_IEEE_APP:
204 	case DCB_ATTR_DCB_APP:
205 		return true;
206 	default:
207 		return false;
208 	}
209 }
210 
211 static bool dcbnl_app_selector_validate(enum ieee_attrs_app type, u8 selector)
212 {
213 	return dcbnl_app_attr_type_get(selector) == type;
214 }
215 
216 static struct sk_buff *dcbnl_newmsg(int type, u8 cmd, u32 port, u32 seq,
217 				    u32 flags, struct nlmsghdr **nlhp)
218 {
219 	struct sk_buff *skb;
220 	struct dcbmsg *dcb;
221 	struct nlmsghdr *nlh;
222 
223 	skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
224 	if (!skb)
225 		return NULL;
226 
227 	nlh = nlmsg_put(skb, port, seq, type, sizeof(*dcb), flags);
228 	BUG_ON(!nlh);
229 
230 	dcb = nlmsg_data(nlh);
231 	dcb->dcb_family = AF_UNSPEC;
232 	dcb->cmd = cmd;
233 	dcb->dcb_pad = 0;
234 
235 	if (nlhp)
236 		*nlhp = nlh;
237 
238 	return skb;
239 }
240 
241 static int dcbnl_getstate(struct net_device *netdev, struct nlmsghdr *nlh,
242 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
243 {
244 	/* if (!tb[DCB_ATTR_STATE] || !netdev->dcbnl_ops->getstate) */
245 	if (!netdev->dcbnl_ops->getstate)
246 		return -EOPNOTSUPP;
247 
248 	return nla_put_u8(skb, DCB_ATTR_STATE,
249 			  netdev->dcbnl_ops->getstate(netdev));
250 }
251 
252 static int dcbnl_getpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
253 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
254 {
255 	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1], *nest;
256 	u8 value;
257 	int ret;
258 	int i;
259 	int getall = 0;
260 
261 	if (!tb[DCB_ATTR_PFC_CFG])
262 		return -EINVAL;
263 
264 	if (!netdev->dcbnl_ops->getpfccfg)
265 		return -EOPNOTSUPP;
266 
267 	ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
268 					  tb[DCB_ATTR_PFC_CFG],
269 					  dcbnl_pfc_up_nest, NULL);
270 	if (ret)
271 		return ret;
272 
273 	nest = nla_nest_start_noflag(skb, DCB_ATTR_PFC_CFG);
274 	if (!nest)
275 		return -EMSGSIZE;
276 
277 	if (data[DCB_PFC_UP_ATTR_ALL])
278 		getall = 1;
279 
280 	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
281 		if (!getall && !data[i])
282 			continue;
283 
284 		value = 0;
285 		netdev->dcbnl_ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0,
286 		                             &value);
287 		ret = nla_put_u8(skb, i, value);
288 		if (ret) {
289 			nla_nest_cancel(skb, nest);
290 			return ret;
291 		}
292 	}
293 	nla_nest_end(skb, nest);
294 
295 	return 0;
296 }
297 
298 static int dcbnl_getperm_hwaddr(struct net_device *netdev, struct nlmsghdr *nlh,
299 				u32 seq, struct nlattr **tb, struct sk_buff *skb)
300 {
301 	u8 perm_addr[MAX_ADDR_LEN];
302 
303 	if (!netdev->dcbnl_ops->getpermhwaddr)
304 		return -EOPNOTSUPP;
305 
306 	memset(perm_addr, 0, sizeof(perm_addr));
307 	netdev->dcbnl_ops->getpermhwaddr(netdev, perm_addr);
308 
309 	return nla_put(skb, DCB_ATTR_PERM_HWADDR, sizeof(perm_addr), perm_addr);
310 }
311 
312 static int dcbnl_getcap(struct net_device *netdev, struct nlmsghdr *nlh,
313 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
314 {
315 	struct nlattr *data[DCB_CAP_ATTR_MAX + 1], *nest;
316 	u8 value;
317 	int ret;
318 	int i;
319 	int getall = 0;
320 
321 	if (!tb[DCB_ATTR_CAP])
322 		return -EINVAL;
323 
324 	if (!netdev->dcbnl_ops->getcap)
325 		return -EOPNOTSUPP;
326 
327 	ret = nla_parse_nested_deprecated(data, DCB_CAP_ATTR_MAX,
328 					  tb[DCB_ATTR_CAP], dcbnl_cap_nest,
329 					  NULL);
330 	if (ret)
331 		return ret;
332 
333 	nest = nla_nest_start_noflag(skb, DCB_ATTR_CAP);
334 	if (!nest)
335 		return -EMSGSIZE;
336 
337 	if (data[DCB_CAP_ATTR_ALL])
338 		getall = 1;
339 
340 	for (i = DCB_CAP_ATTR_ALL+1; i <= DCB_CAP_ATTR_MAX; i++) {
341 		if (!getall && !data[i])
342 			continue;
343 
344 		if (!netdev->dcbnl_ops->getcap(netdev, i, &value)) {
345 			ret = nla_put_u8(skb, i, value);
346 			if (ret) {
347 				nla_nest_cancel(skb, nest);
348 				return ret;
349 			}
350 		}
351 	}
352 	nla_nest_end(skb, nest);
353 
354 	return 0;
355 }
356 
357 static int dcbnl_getnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
358 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
359 {
360 	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1], *nest;
361 	u8 value;
362 	int ret;
363 	int i;
364 	int getall = 0;
365 
366 	if (!tb[DCB_ATTR_NUMTCS])
367 		return -EINVAL;
368 
369 	if (!netdev->dcbnl_ops->getnumtcs)
370 		return -EOPNOTSUPP;
371 
372 	ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
373 					  tb[DCB_ATTR_NUMTCS],
374 					  dcbnl_numtcs_nest, NULL);
375 	if (ret)
376 		return ret;
377 
378 	nest = nla_nest_start_noflag(skb, DCB_ATTR_NUMTCS);
379 	if (!nest)
380 		return -EMSGSIZE;
381 
382 	if (data[DCB_NUMTCS_ATTR_ALL])
383 		getall = 1;
384 
385 	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
386 		if (!getall && !data[i])
387 			continue;
388 
389 		ret = netdev->dcbnl_ops->getnumtcs(netdev, i, &value);
390 		if (!ret) {
391 			ret = nla_put_u8(skb, i, value);
392 			if (ret) {
393 				nla_nest_cancel(skb, nest);
394 				return ret;
395 			}
396 		} else
397 			return -EINVAL;
398 	}
399 	nla_nest_end(skb, nest);
400 
401 	return 0;
402 }
403 
404 static int dcbnl_setnumtcs(struct net_device *netdev, struct nlmsghdr *nlh,
405 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
406 {
407 	struct nlattr *data[DCB_NUMTCS_ATTR_MAX + 1];
408 	int ret;
409 	u8 value;
410 	int i;
411 
412 	if (!tb[DCB_ATTR_NUMTCS])
413 		return -EINVAL;
414 
415 	if (!netdev->dcbnl_ops->setnumtcs)
416 		return -EOPNOTSUPP;
417 
418 	ret = nla_parse_nested_deprecated(data, DCB_NUMTCS_ATTR_MAX,
419 					  tb[DCB_ATTR_NUMTCS],
420 					  dcbnl_numtcs_nest, NULL);
421 	if (ret)
422 		return ret;
423 
424 	for (i = DCB_NUMTCS_ATTR_ALL+1; i <= DCB_NUMTCS_ATTR_MAX; i++) {
425 		if (data[i] == NULL)
426 			continue;
427 
428 		value = nla_get_u8(data[i]);
429 
430 		ret = netdev->dcbnl_ops->setnumtcs(netdev, i, value);
431 		if (ret)
432 			break;
433 	}
434 
435 	return nla_put_u8(skb, DCB_ATTR_NUMTCS, !!ret);
436 }
437 
438 static int dcbnl_getpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
439 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
440 {
441 	if (!netdev->dcbnl_ops->getpfcstate)
442 		return -EOPNOTSUPP;
443 
444 	return nla_put_u8(skb, DCB_ATTR_PFC_STATE,
445 			  netdev->dcbnl_ops->getpfcstate(netdev));
446 }
447 
448 static int dcbnl_setpfcstate(struct net_device *netdev, struct nlmsghdr *nlh,
449 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
450 {
451 	u8 value;
452 
453 	if (!tb[DCB_ATTR_PFC_STATE])
454 		return -EINVAL;
455 
456 	if (!netdev->dcbnl_ops->setpfcstate)
457 		return -EOPNOTSUPP;
458 
459 	value = nla_get_u8(tb[DCB_ATTR_PFC_STATE]);
460 
461 	netdev->dcbnl_ops->setpfcstate(netdev, value);
462 
463 	return nla_put_u8(skb, DCB_ATTR_PFC_STATE, 0);
464 }
465 
466 static int dcbnl_getapp(struct net_device *netdev, struct nlmsghdr *nlh,
467 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
468 {
469 	struct nlattr *app_nest;
470 	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
471 	u16 id;
472 	u8 up, idtype;
473 	int ret;
474 
475 	if (!tb[DCB_ATTR_APP])
476 		return -EINVAL;
477 
478 	ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
479 					  tb[DCB_ATTR_APP], dcbnl_app_nest,
480 					  NULL);
481 	if (ret)
482 		return ret;
483 
484 	/* all must be non-null */
485 	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
486 	    (!app_tb[DCB_APP_ATTR_ID]))
487 		return -EINVAL;
488 
489 	/* either by eth type or by socket number */
490 	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
491 	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
492 	    (idtype != DCB_APP_IDTYPE_PORTNUM))
493 		return -EINVAL;
494 
495 	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
496 
497 	if (netdev->dcbnl_ops->getapp) {
498 		ret = netdev->dcbnl_ops->getapp(netdev, idtype, id);
499 		if (ret < 0)
500 			return ret;
501 		else
502 			up = ret;
503 	} else {
504 		struct dcb_app app = {
505 					.selector = idtype,
506 					.protocol = id,
507 				     };
508 		up = dcb_getapp(netdev, &app);
509 	}
510 
511 	app_nest = nla_nest_start_noflag(skb, DCB_ATTR_APP);
512 	if (!app_nest)
513 		return -EMSGSIZE;
514 
515 	ret = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE, idtype);
516 	if (ret)
517 		goto out_cancel;
518 
519 	ret = nla_put_u16(skb, DCB_APP_ATTR_ID, id);
520 	if (ret)
521 		goto out_cancel;
522 
523 	ret = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY, up);
524 	if (ret)
525 		goto out_cancel;
526 
527 	nla_nest_end(skb, app_nest);
528 
529 	return 0;
530 
531 out_cancel:
532 	nla_nest_cancel(skb, app_nest);
533 	return ret;
534 }
535 
536 static int dcbnl_setapp(struct net_device *netdev, struct nlmsghdr *nlh,
537 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
538 {
539 	int ret;
540 	u16 id;
541 	u8 up, idtype;
542 	struct nlattr *app_tb[DCB_APP_ATTR_MAX + 1];
543 
544 	if (!tb[DCB_ATTR_APP])
545 		return -EINVAL;
546 
547 	ret = nla_parse_nested_deprecated(app_tb, DCB_APP_ATTR_MAX,
548 					  tb[DCB_ATTR_APP], dcbnl_app_nest,
549 					  NULL);
550 	if (ret)
551 		return ret;
552 
553 	/* all must be non-null */
554 	if ((!app_tb[DCB_APP_ATTR_IDTYPE]) ||
555 	    (!app_tb[DCB_APP_ATTR_ID]) ||
556 	    (!app_tb[DCB_APP_ATTR_PRIORITY]))
557 		return -EINVAL;
558 
559 	/* either by eth type or by socket number */
560 	idtype = nla_get_u8(app_tb[DCB_APP_ATTR_IDTYPE]);
561 	if ((idtype != DCB_APP_IDTYPE_ETHTYPE) &&
562 	    (idtype != DCB_APP_IDTYPE_PORTNUM))
563 		return -EINVAL;
564 
565 	id = nla_get_u16(app_tb[DCB_APP_ATTR_ID]);
566 	up = nla_get_u8(app_tb[DCB_APP_ATTR_PRIORITY]);
567 
568 	if (netdev->dcbnl_ops->setapp) {
569 		ret = netdev->dcbnl_ops->setapp(netdev, idtype, id, up);
570 		if (ret < 0)
571 			return ret;
572 	} else {
573 		struct dcb_app app;
574 		app.selector = idtype;
575 		app.protocol = id;
576 		app.priority = up;
577 		ret = dcb_setapp(netdev, &app);
578 	}
579 
580 	ret = nla_put_u8(skb, DCB_ATTR_APP, ret);
581 	dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SAPP, seq, 0);
582 
583 	return ret;
584 }
585 
586 static int __dcbnl_pg_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
587 			     struct nlattr **tb, struct sk_buff *skb, int dir)
588 {
589 	struct nlattr *pg_nest, *param_nest, *data;
590 	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
591 	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
592 	u8 prio, pgid, tc_pct, up_map;
593 	int ret;
594 	int getall = 0;
595 	int i;
596 
597 	if (!tb[DCB_ATTR_PG_CFG])
598 		return -EINVAL;
599 
600 	if (!netdev->dcbnl_ops->getpgtccfgtx ||
601 	    !netdev->dcbnl_ops->getpgtccfgrx ||
602 	    !netdev->dcbnl_ops->getpgbwgcfgtx ||
603 	    !netdev->dcbnl_ops->getpgbwgcfgrx)
604 		return -EOPNOTSUPP;
605 
606 	ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
607 					  tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
608 					  NULL);
609 	if (ret)
610 		return ret;
611 
612 	pg_nest = nla_nest_start_noflag(skb, DCB_ATTR_PG_CFG);
613 	if (!pg_nest)
614 		return -EMSGSIZE;
615 
616 	if (pg_tb[DCB_PG_ATTR_TC_ALL])
617 		getall = 1;
618 
619 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
620 		if (!getall && !pg_tb[i])
621 			continue;
622 
623 		if (pg_tb[DCB_PG_ATTR_TC_ALL])
624 			data = pg_tb[DCB_PG_ATTR_TC_ALL];
625 		else
626 			data = pg_tb[i];
627 		ret = nla_parse_nested_deprecated(param_tb,
628 						  DCB_TC_ATTR_PARAM_MAX, data,
629 						  dcbnl_tc_param_nest, NULL);
630 		if (ret)
631 			goto err_pg;
632 
633 		param_nest = nla_nest_start_noflag(skb, i);
634 		if (!param_nest)
635 			goto err_pg;
636 
637 		pgid = DCB_ATTR_VALUE_UNDEFINED;
638 		prio = DCB_ATTR_VALUE_UNDEFINED;
639 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
640 		up_map = DCB_ATTR_VALUE_UNDEFINED;
641 
642 		if (dir) {
643 			/* Rx */
644 			netdev->dcbnl_ops->getpgtccfgrx(netdev,
645 						i - DCB_PG_ATTR_TC_0, &prio,
646 						&pgid, &tc_pct, &up_map);
647 		} else {
648 			/* Tx */
649 			netdev->dcbnl_ops->getpgtccfgtx(netdev,
650 						i - DCB_PG_ATTR_TC_0, &prio,
651 						&pgid, &tc_pct, &up_map);
652 		}
653 
654 		if (param_tb[DCB_TC_ATTR_PARAM_PGID] ||
655 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
656 			ret = nla_put_u8(skb,
657 			                 DCB_TC_ATTR_PARAM_PGID, pgid);
658 			if (ret)
659 				goto err_param;
660 		}
661 		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING] ||
662 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
663 			ret = nla_put_u8(skb,
664 			                 DCB_TC_ATTR_PARAM_UP_MAPPING, up_map);
665 			if (ret)
666 				goto err_param;
667 		}
668 		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO] ||
669 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
670 			ret = nla_put_u8(skb,
671 			                 DCB_TC_ATTR_PARAM_STRICT_PRIO, prio);
672 			if (ret)
673 				goto err_param;
674 		}
675 		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT] ||
676 		    param_tb[DCB_TC_ATTR_PARAM_ALL]) {
677 			ret = nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT,
678 			                 tc_pct);
679 			if (ret)
680 				goto err_param;
681 		}
682 		nla_nest_end(skb, param_nest);
683 	}
684 
685 	if (pg_tb[DCB_PG_ATTR_BW_ID_ALL])
686 		getall = 1;
687 	else
688 		getall = 0;
689 
690 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
691 		if (!getall && !pg_tb[i])
692 			continue;
693 
694 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
695 
696 		if (dir) {
697 			/* Rx */
698 			netdev->dcbnl_ops->getpgbwgcfgrx(netdev,
699 					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
700 		} else {
701 			/* Tx */
702 			netdev->dcbnl_ops->getpgbwgcfgtx(netdev,
703 					i - DCB_PG_ATTR_BW_ID_0, &tc_pct);
704 		}
705 		ret = nla_put_u8(skb, i, tc_pct);
706 		if (ret)
707 			goto err_pg;
708 	}
709 
710 	nla_nest_end(skb, pg_nest);
711 
712 	return 0;
713 
714 err_param:
715 	nla_nest_cancel(skb, param_nest);
716 err_pg:
717 	nla_nest_cancel(skb, pg_nest);
718 
719 	return -EMSGSIZE;
720 }
721 
722 static int dcbnl_pgtx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
723 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
724 {
725 	return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 0);
726 }
727 
728 static int dcbnl_pgrx_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
729 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
730 {
731 	return __dcbnl_pg_getcfg(netdev, nlh, tb, skb, 1);
732 }
733 
734 static int dcbnl_setstate(struct net_device *netdev, struct nlmsghdr *nlh,
735 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
736 {
737 	u8 value;
738 
739 	if (!tb[DCB_ATTR_STATE])
740 		return -EINVAL;
741 
742 	if (!netdev->dcbnl_ops->setstate)
743 		return -EOPNOTSUPP;
744 
745 	value = nla_get_u8(tb[DCB_ATTR_STATE]);
746 
747 	return nla_put_u8(skb, DCB_ATTR_STATE,
748 			  netdev->dcbnl_ops->setstate(netdev, value));
749 }
750 
751 static int dcbnl_setpfccfg(struct net_device *netdev, struct nlmsghdr *nlh,
752 			   u32 seq, struct nlattr **tb, struct sk_buff *skb)
753 {
754 	struct nlattr *data[DCB_PFC_UP_ATTR_MAX + 1];
755 	int i;
756 	int ret;
757 	u8 value;
758 
759 	if (!tb[DCB_ATTR_PFC_CFG])
760 		return -EINVAL;
761 
762 	if (!netdev->dcbnl_ops->setpfccfg)
763 		return -EOPNOTSUPP;
764 
765 	ret = nla_parse_nested_deprecated(data, DCB_PFC_UP_ATTR_MAX,
766 					  tb[DCB_ATTR_PFC_CFG],
767 					  dcbnl_pfc_up_nest, NULL);
768 	if (ret)
769 		return ret;
770 
771 	for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
772 		if (data[i] == NULL)
773 			continue;
774 		value = nla_get_u8(data[i]);
775 		netdev->dcbnl_ops->setpfccfg(netdev,
776 			data[i]->nla_type - DCB_PFC_UP_ATTR_0, value);
777 	}
778 
779 	return nla_put_u8(skb, DCB_ATTR_PFC_CFG, 0);
780 }
781 
782 static int dcbnl_setall(struct net_device *netdev, struct nlmsghdr *nlh,
783 			u32 seq, struct nlattr **tb, struct sk_buff *skb)
784 {
785 	int ret;
786 
787 	if (!tb[DCB_ATTR_SET_ALL])
788 		return -EINVAL;
789 
790 	if (!netdev->dcbnl_ops->setall)
791 		return -EOPNOTSUPP;
792 
793 	ret = nla_put_u8(skb, DCB_ATTR_SET_ALL,
794 			 netdev->dcbnl_ops->setall(netdev));
795 	dcbnl_cee_notify(netdev, RTM_SETDCB, DCB_CMD_SET_ALL, seq, 0);
796 
797 	return ret;
798 }
799 
800 static int __dcbnl_pg_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
801 			     u32 seq, struct nlattr **tb, struct sk_buff *skb,
802 			     int dir)
803 {
804 	struct nlattr *pg_tb[DCB_PG_ATTR_MAX + 1];
805 	struct nlattr *param_tb[DCB_TC_ATTR_PARAM_MAX + 1];
806 	int ret;
807 	int i;
808 	u8 pgid;
809 	u8 up_map;
810 	u8 prio;
811 	u8 tc_pct;
812 
813 	if (!tb[DCB_ATTR_PG_CFG])
814 		return -EINVAL;
815 
816 	if (!netdev->dcbnl_ops->setpgtccfgtx ||
817 	    !netdev->dcbnl_ops->setpgtccfgrx ||
818 	    !netdev->dcbnl_ops->setpgbwgcfgtx ||
819 	    !netdev->dcbnl_ops->setpgbwgcfgrx)
820 		return -EOPNOTSUPP;
821 
822 	ret = nla_parse_nested_deprecated(pg_tb, DCB_PG_ATTR_MAX,
823 					  tb[DCB_ATTR_PG_CFG], dcbnl_pg_nest,
824 					  NULL);
825 	if (ret)
826 		return ret;
827 
828 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
829 		if (!pg_tb[i])
830 			continue;
831 
832 		ret = nla_parse_nested_deprecated(param_tb,
833 						  DCB_TC_ATTR_PARAM_MAX,
834 						  pg_tb[i],
835 						  dcbnl_tc_param_nest, NULL);
836 		if (ret)
837 			return ret;
838 
839 		pgid = DCB_ATTR_VALUE_UNDEFINED;
840 		prio = DCB_ATTR_VALUE_UNDEFINED;
841 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
842 		up_map = DCB_ATTR_VALUE_UNDEFINED;
843 
844 		if (param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO])
845 			prio =
846 			    nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_STRICT_PRIO]);
847 
848 		if (param_tb[DCB_TC_ATTR_PARAM_PGID])
849 			pgid = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_PGID]);
850 
851 		if (param_tb[DCB_TC_ATTR_PARAM_BW_PCT])
852 			tc_pct = nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_BW_PCT]);
853 
854 		if (param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING])
855 			up_map =
856 			     nla_get_u8(param_tb[DCB_TC_ATTR_PARAM_UP_MAPPING]);
857 
858 		/* dir: Tx = 0, Rx = 1 */
859 		if (dir) {
860 			/* Rx */
861 			netdev->dcbnl_ops->setpgtccfgrx(netdev,
862 				i - DCB_PG_ATTR_TC_0,
863 				prio, pgid, tc_pct, up_map);
864 		} else {
865 			/* Tx */
866 			netdev->dcbnl_ops->setpgtccfgtx(netdev,
867 				i - DCB_PG_ATTR_TC_0,
868 				prio, pgid, tc_pct, up_map);
869 		}
870 	}
871 
872 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
873 		if (!pg_tb[i])
874 			continue;
875 
876 		tc_pct = nla_get_u8(pg_tb[i]);
877 
878 		/* dir: Tx = 0, Rx = 1 */
879 		if (dir) {
880 			/* Rx */
881 			netdev->dcbnl_ops->setpgbwgcfgrx(netdev,
882 					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
883 		} else {
884 			/* Tx */
885 			netdev->dcbnl_ops->setpgbwgcfgtx(netdev,
886 					 i - DCB_PG_ATTR_BW_ID_0, tc_pct);
887 		}
888 	}
889 
890 	return nla_put_u8(skb, DCB_ATTR_PG_CFG, 0);
891 }
892 
893 static int dcbnl_pgtx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
894 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
895 {
896 	return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 0);
897 }
898 
899 static int dcbnl_pgrx_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
900 			     u32 seq, struct nlattr **tb, struct sk_buff *skb)
901 {
902 	return __dcbnl_pg_setcfg(netdev, nlh, seq, tb, skb, 1);
903 }
904 
905 static int dcbnl_bcn_getcfg(struct net_device *netdev, struct nlmsghdr *nlh,
906 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
907 {
908 	struct nlattr *bcn_nest;
909 	struct nlattr *bcn_tb[DCB_BCN_ATTR_MAX + 1];
910 	u8 value_byte;
911 	u32 value_integer;
912 	int ret;
913 	bool getall = false;
914 	int i;
915 
916 	if (!tb[DCB_ATTR_BCN])
917 		return -EINVAL;
918 
919 	if (!netdev->dcbnl_ops->getbcnrp ||
920 	    !netdev->dcbnl_ops->getbcncfg)
921 		return -EOPNOTSUPP;
922 
923 	ret = nla_parse_nested_deprecated(bcn_tb, DCB_BCN_ATTR_MAX,
924 					  tb[DCB_ATTR_BCN], dcbnl_bcn_nest,
925 					  NULL);
926 	if (ret)
927 		return ret;
928 
929 	bcn_nest = nla_nest_start_noflag(skb, DCB_ATTR_BCN);
930 	if (!bcn_nest)
931 		return -EMSGSIZE;
932 
933 	if (bcn_tb[DCB_BCN_ATTR_ALL])
934 		getall = true;
935 
936 	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
937 		if (!getall && !bcn_tb[i])
938 			continue;
939 
940 		netdev->dcbnl_ops->getbcnrp(netdev, i - DCB_BCN_ATTR_RP_0,
941 		                            &value_byte);
942 		ret = nla_put_u8(skb, i, value_byte);
943 		if (ret)
944 			goto err_bcn;
945 	}
946 
947 	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
948 		if (!getall && !bcn_tb[i])
949 			continue;
950 
951 		netdev->dcbnl_ops->getbcncfg(netdev, i,
952 		                             &value_integer);
953 		ret = nla_put_u32(skb, i, value_integer);
954 		if (ret)
955 			goto err_bcn;
956 	}
957 
958 	nla_nest_end(skb, bcn_nest);
959 
960 	return 0;
961 
962 err_bcn:
963 	nla_nest_cancel(skb, bcn_nest);
964 	return ret;
965 }
966 
967 static int dcbnl_bcn_setcfg(struct net_device *netdev, struct nlmsghdr *nlh,
968 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
969 {
970 	struct nlattr *data[DCB_BCN_ATTR_MAX + 1];
971 	int i;
972 	int ret;
973 	u8 value_byte;
974 	u32 value_int;
975 
976 	if (!tb[DCB_ATTR_BCN])
977 		return -EINVAL;
978 
979 	if (!netdev->dcbnl_ops->setbcncfg ||
980 	    !netdev->dcbnl_ops->setbcnrp)
981 		return -EOPNOTSUPP;
982 
983 	ret = nla_parse_nested_deprecated(data, DCB_BCN_ATTR_MAX,
984 					  tb[DCB_ATTR_BCN], dcbnl_bcn_nest,
985 					  NULL);
986 	if (ret)
987 		return ret;
988 
989 	for (i = DCB_BCN_ATTR_RP_0; i <= DCB_BCN_ATTR_RP_7; i++) {
990 		if (data[i] == NULL)
991 			continue;
992 		value_byte = nla_get_u8(data[i]);
993 		netdev->dcbnl_ops->setbcnrp(netdev,
994 			data[i]->nla_type - DCB_BCN_ATTR_RP_0, value_byte);
995 	}
996 
997 	for (i = DCB_BCN_ATTR_BCNA_0; i <= DCB_BCN_ATTR_RI; i++) {
998 		if (data[i] == NULL)
999 			continue;
1000 		value_int = nla_get_u32(data[i]);
1001 		netdev->dcbnl_ops->setbcncfg(netdev,
1002 	                                     i, value_int);
1003 	}
1004 
1005 	return nla_put_u8(skb, DCB_ATTR_BCN, 0);
1006 }
1007 
1008 static int dcbnl_build_peer_app(struct net_device *netdev, struct sk_buff* skb,
1009 				int app_nested_type, int app_info_type,
1010 				int app_entry_type)
1011 {
1012 	struct dcb_peer_app_info info;
1013 	struct dcb_app *table = NULL;
1014 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1015 	u16 app_count;
1016 	int err;
1017 
1018 
1019 	/**
1020 	 * retrieve the peer app configuration form the driver. If the driver
1021 	 * handlers fail exit without doing anything
1022 	 */
1023 	err = ops->peer_getappinfo(netdev, &info, &app_count);
1024 	if (!err && app_count) {
1025 		table = kmalloc_objs(struct dcb_app, app_count);
1026 		if (!table)
1027 			return -ENOMEM;
1028 
1029 		err = ops->peer_getapptable(netdev, table);
1030 	}
1031 
1032 	if (!err) {
1033 		u16 i;
1034 		struct nlattr *app;
1035 
1036 		/**
1037 		 * build the message, from here on the only possible failure
1038 		 * is due to the skb size
1039 		 */
1040 		err = -EMSGSIZE;
1041 
1042 		app = nla_nest_start_noflag(skb, app_nested_type);
1043 		if (!app)
1044 			goto nla_put_failure;
1045 
1046 		if (app_info_type &&
1047 		    nla_put(skb, app_info_type, sizeof(info), &info))
1048 			goto nla_put_failure;
1049 
1050 		for (i = 0; i < app_count; i++) {
1051 			if (nla_put(skb, app_entry_type, sizeof(struct dcb_app),
1052 				    &table[i]))
1053 				goto nla_put_failure;
1054 		}
1055 		nla_nest_end(skb, app);
1056 	}
1057 	err = 0;
1058 
1059 nla_put_failure:
1060 	kfree(table);
1061 	return err;
1062 }
1063 
1064 static int dcbnl_getapptrust(struct net_device *netdev, struct sk_buff *skb)
1065 {
1066 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1067 	enum ieee_attrs_app type;
1068 	struct nlattr *apptrust;
1069 	int nselectors, err, i;
1070 	u8 *selectors;
1071 
1072 	selectors = kzalloc(IEEE_8021QAZ_APP_SEL_MAX + 1, GFP_KERNEL);
1073 	if (!selectors)
1074 		return -ENOMEM;
1075 
1076 	err = ops->dcbnl_getapptrust(netdev, selectors, &nselectors);
1077 	if (err) {
1078 		err = 0;
1079 		goto out;
1080 	}
1081 
1082 	apptrust = nla_nest_start(skb, DCB_ATTR_DCB_APP_TRUST_TABLE);
1083 	if (!apptrust) {
1084 		err = -EMSGSIZE;
1085 		goto out;
1086 	}
1087 
1088 	for (i = 0; i < nselectors; i++) {
1089 		type = dcbnl_app_attr_type_get(selectors[i]);
1090 		err = nla_put_u8(skb, type, selectors[i]);
1091 		if (err) {
1092 			nla_nest_cancel(skb, apptrust);
1093 			goto out;
1094 		}
1095 	}
1096 	nla_nest_end(skb, apptrust);
1097 
1098 out:
1099 	kfree(selectors);
1100 	return err;
1101 }
1102 
1103 /* Set or delete APP table or rewrite table entries. The APP struct is validated
1104  * and the appropriate callback function is called.
1105  */
1106 static int dcbnl_app_table_setdel(struct nlattr *attr,
1107 				  struct net_device *netdev,
1108 				  int (*setdel)(struct net_device *dev,
1109 						struct dcb_app *app))
1110 {
1111 	struct dcb_app *app_data;
1112 	enum ieee_attrs_app type;
1113 	struct nlattr *attr_itr;
1114 	int rem, err;
1115 
1116 	nla_for_each_nested(attr_itr, attr, rem) {
1117 		type = nla_type(attr_itr);
1118 
1119 		if (!dcbnl_app_attr_type_validate(type))
1120 			continue;
1121 
1122 		if (nla_len(attr_itr) < sizeof(struct dcb_app))
1123 			return -ERANGE;
1124 
1125 		app_data = nla_data(attr_itr);
1126 
1127 		if (!dcbnl_app_selector_validate(type, app_data->selector))
1128 			return -EINVAL;
1129 
1130 		err = setdel(netdev, app_data);
1131 		if (err)
1132 			return err;
1133 	}
1134 
1135 	return 0;
1136 }
1137 
1138 /* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb GET commands. */
1139 static int dcbnl_ieee_fill(struct sk_buff *skb, struct net_device *netdev)
1140 {
1141 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1142 	struct nlattr *ieee, *app, *rewr;
1143 	struct dcb_app_type *itr;
1144 	int dcbx;
1145 	int err;
1146 
1147 	if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
1148 		return -EMSGSIZE;
1149 
1150 	ieee = nla_nest_start_noflag(skb, DCB_ATTR_IEEE);
1151 	if (!ieee)
1152 		return -EMSGSIZE;
1153 
1154 	if (ops->ieee_getets) {
1155 		struct ieee_ets ets;
1156 		memset(&ets, 0, sizeof(ets));
1157 		err = ops->ieee_getets(netdev, &ets);
1158 		if (!err &&
1159 		    nla_put(skb, DCB_ATTR_IEEE_ETS, sizeof(ets), &ets))
1160 			return -EMSGSIZE;
1161 	}
1162 
1163 	if (ops->ieee_getmaxrate) {
1164 		struct ieee_maxrate maxrate;
1165 		memset(&maxrate, 0, sizeof(maxrate));
1166 		err = ops->ieee_getmaxrate(netdev, &maxrate);
1167 		if (!err) {
1168 			err = nla_put(skb, DCB_ATTR_IEEE_MAXRATE,
1169 				      sizeof(maxrate), &maxrate);
1170 			if (err)
1171 				return -EMSGSIZE;
1172 		}
1173 	}
1174 
1175 	if (ops->ieee_getqcn) {
1176 		struct ieee_qcn qcn;
1177 
1178 		memset(&qcn, 0, sizeof(qcn));
1179 		err = ops->ieee_getqcn(netdev, &qcn);
1180 		if (!err) {
1181 			err = nla_put(skb, DCB_ATTR_IEEE_QCN,
1182 				      sizeof(qcn), &qcn);
1183 			if (err)
1184 				return -EMSGSIZE;
1185 		}
1186 	}
1187 
1188 	if (ops->ieee_getqcnstats) {
1189 		struct ieee_qcn_stats qcn_stats;
1190 
1191 		memset(&qcn_stats, 0, sizeof(qcn_stats));
1192 		err = ops->ieee_getqcnstats(netdev, &qcn_stats);
1193 		if (!err) {
1194 			err = nla_put(skb, DCB_ATTR_IEEE_QCN_STATS,
1195 				      sizeof(qcn_stats), &qcn_stats);
1196 			if (err)
1197 				return -EMSGSIZE;
1198 		}
1199 	}
1200 
1201 	if (ops->ieee_getpfc) {
1202 		struct ieee_pfc pfc;
1203 		memset(&pfc, 0, sizeof(pfc));
1204 		err = ops->ieee_getpfc(netdev, &pfc);
1205 		if (!err &&
1206 		    nla_put(skb, DCB_ATTR_IEEE_PFC, sizeof(pfc), &pfc))
1207 			return -EMSGSIZE;
1208 	}
1209 
1210 	if (ops->dcbnl_getbuffer) {
1211 		struct dcbnl_buffer buffer;
1212 
1213 		memset(&buffer, 0, sizeof(buffer));
1214 		err = ops->dcbnl_getbuffer(netdev, &buffer);
1215 		if (!err &&
1216 		    nla_put(skb, DCB_ATTR_DCB_BUFFER, sizeof(buffer), &buffer))
1217 			return -EMSGSIZE;
1218 	}
1219 
1220 	app = nla_nest_start_noflag(skb, DCB_ATTR_IEEE_APP_TABLE);
1221 	if (!app)
1222 		return -EMSGSIZE;
1223 
1224 	spin_lock_bh(&dcb_lock);
1225 	list_for_each_entry(itr, &dcb_app_list, list) {
1226 		if (itr->ifindex == netdev->ifindex) {
1227 			enum ieee_attrs_app type =
1228 				dcbnl_app_attr_type_get(itr->app.selector);
1229 			err = nla_put(skb, type, sizeof(itr->app), &itr->app);
1230 			if (err) {
1231 				spin_unlock_bh(&dcb_lock);
1232 				return -EMSGSIZE;
1233 			}
1234 		}
1235 	}
1236 
1237 	if (netdev->dcbnl_ops->getdcbx)
1238 		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1239 	else
1240 		dcbx = -EOPNOTSUPP;
1241 
1242 	spin_unlock_bh(&dcb_lock);
1243 	nla_nest_end(skb, app);
1244 
1245 	rewr = nla_nest_start(skb, DCB_ATTR_DCB_REWR_TABLE);
1246 	if (!rewr)
1247 		return -EMSGSIZE;
1248 
1249 	spin_lock_bh(&dcb_lock);
1250 	list_for_each_entry(itr, &dcb_rewr_list, list) {
1251 		if (itr->ifindex == netdev->ifindex) {
1252 			enum ieee_attrs_app type =
1253 				dcbnl_app_attr_type_get(itr->app.selector);
1254 			err = nla_put(skb, type, sizeof(itr->app), &itr->app);
1255 			if (err) {
1256 				spin_unlock_bh(&dcb_lock);
1257 				nla_nest_cancel(skb, rewr);
1258 				return -EMSGSIZE;
1259 			}
1260 		}
1261 	}
1262 
1263 	spin_unlock_bh(&dcb_lock);
1264 	nla_nest_end(skb, rewr);
1265 
1266 	if (ops->dcbnl_getapptrust) {
1267 		err = dcbnl_getapptrust(netdev, skb);
1268 		if (err)
1269 			return err;
1270 	}
1271 
1272 	/* get peer info if available */
1273 	if (ops->ieee_peer_getets) {
1274 		struct ieee_ets ets;
1275 		memset(&ets, 0, sizeof(ets));
1276 		err = ops->ieee_peer_getets(netdev, &ets);
1277 		if (!err &&
1278 		    nla_put(skb, DCB_ATTR_IEEE_PEER_ETS, sizeof(ets), &ets))
1279 			return -EMSGSIZE;
1280 	}
1281 
1282 	if (ops->ieee_peer_getpfc) {
1283 		struct ieee_pfc pfc;
1284 		memset(&pfc, 0, sizeof(pfc));
1285 		err = ops->ieee_peer_getpfc(netdev, &pfc);
1286 		if (!err &&
1287 		    nla_put(skb, DCB_ATTR_IEEE_PEER_PFC, sizeof(pfc), &pfc))
1288 			return -EMSGSIZE;
1289 	}
1290 
1291 	if (ops->peer_getappinfo && ops->peer_getapptable) {
1292 		err = dcbnl_build_peer_app(netdev, skb,
1293 					   DCB_ATTR_IEEE_PEER_APP,
1294 					   DCB_ATTR_IEEE_APP_UNSPEC,
1295 					   DCB_ATTR_IEEE_APP);
1296 		if (err)
1297 			return -EMSGSIZE;
1298 	}
1299 
1300 	nla_nest_end(skb, ieee);
1301 	if (dcbx >= 0) {
1302 		err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1303 		if (err)
1304 			return -EMSGSIZE;
1305 	}
1306 
1307 	return 0;
1308 }
1309 
1310 static int dcbnl_cee_pg_fill(struct sk_buff *skb, struct net_device *dev,
1311 			     int dir)
1312 {
1313 	u8 pgid, up_map, prio, tc_pct;
1314 	const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1315 	int i = dir ? DCB_ATTR_CEE_TX_PG : DCB_ATTR_CEE_RX_PG;
1316 	struct nlattr *pg = nla_nest_start_noflag(skb, i);
1317 
1318 	if (!pg)
1319 		return -EMSGSIZE;
1320 
1321 	for (i = DCB_PG_ATTR_TC_0; i <= DCB_PG_ATTR_TC_7; i++) {
1322 		struct nlattr *tc_nest = nla_nest_start_noflag(skb, i);
1323 
1324 		if (!tc_nest)
1325 			return -EMSGSIZE;
1326 
1327 		pgid = DCB_ATTR_VALUE_UNDEFINED;
1328 		prio = DCB_ATTR_VALUE_UNDEFINED;
1329 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1330 		up_map = DCB_ATTR_VALUE_UNDEFINED;
1331 
1332 		if (!dir)
1333 			ops->getpgtccfgrx(dev, i - DCB_PG_ATTR_TC_0,
1334 					  &prio, &pgid, &tc_pct, &up_map);
1335 		else
1336 			ops->getpgtccfgtx(dev, i - DCB_PG_ATTR_TC_0,
1337 					  &prio, &pgid, &tc_pct, &up_map);
1338 
1339 		if (nla_put_u8(skb, DCB_TC_ATTR_PARAM_PGID, pgid) ||
1340 		    nla_put_u8(skb, DCB_TC_ATTR_PARAM_UP_MAPPING, up_map) ||
1341 		    nla_put_u8(skb, DCB_TC_ATTR_PARAM_STRICT_PRIO, prio) ||
1342 		    nla_put_u8(skb, DCB_TC_ATTR_PARAM_BW_PCT, tc_pct))
1343 			return -EMSGSIZE;
1344 		nla_nest_end(skb, tc_nest);
1345 	}
1346 
1347 	for (i = DCB_PG_ATTR_BW_ID_0; i <= DCB_PG_ATTR_BW_ID_7; i++) {
1348 		tc_pct = DCB_ATTR_VALUE_UNDEFINED;
1349 
1350 		if (!dir)
1351 			ops->getpgbwgcfgrx(dev, i - DCB_PG_ATTR_BW_ID_0,
1352 					   &tc_pct);
1353 		else
1354 			ops->getpgbwgcfgtx(dev, i - DCB_PG_ATTR_BW_ID_0,
1355 					   &tc_pct);
1356 		if (nla_put_u8(skb, i, tc_pct))
1357 			return -EMSGSIZE;
1358 	}
1359 	nla_nest_end(skb, pg);
1360 	return 0;
1361 }
1362 
1363 static int dcbnl_cee_fill(struct sk_buff *skb, struct net_device *netdev)
1364 {
1365 	struct nlattr *cee, *app;
1366 	struct dcb_app_type *itr;
1367 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1368 	int dcbx, i, err = -EMSGSIZE;
1369 	u8 value;
1370 
1371 	if (nla_put_string(skb, DCB_ATTR_IFNAME, netdev->name))
1372 		goto nla_put_failure;
1373 	cee = nla_nest_start_noflag(skb, DCB_ATTR_CEE);
1374 	if (!cee)
1375 		goto nla_put_failure;
1376 
1377 	/* local pg */
1378 	if (ops->getpgtccfgtx && ops->getpgbwgcfgtx) {
1379 		err = dcbnl_cee_pg_fill(skb, netdev, 1);
1380 		if (err)
1381 			goto nla_put_failure;
1382 	}
1383 
1384 	if (ops->getpgtccfgrx && ops->getpgbwgcfgrx) {
1385 		err = dcbnl_cee_pg_fill(skb, netdev, 0);
1386 		if (err)
1387 			goto nla_put_failure;
1388 	}
1389 
1390 	/* local pfc */
1391 	if (ops->getpfccfg) {
1392 		struct nlattr *pfc_nest = nla_nest_start_noflag(skb,
1393 								DCB_ATTR_CEE_PFC);
1394 
1395 		if (!pfc_nest)
1396 			goto nla_put_failure;
1397 
1398 		for (i = DCB_PFC_UP_ATTR_0; i <= DCB_PFC_UP_ATTR_7; i++) {
1399 			value = 0;
1400 			ops->getpfccfg(netdev, i - DCB_PFC_UP_ATTR_0, &value);
1401 			if (nla_put_u8(skb, i, value))
1402 				goto nla_put_failure;
1403 		}
1404 		nla_nest_end(skb, pfc_nest);
1405 	}
1406 
1407 	/* local app */
1408 	spin_lock_bh(&dcb_lock);
1409 	app = nla_nest_start_noflag(skb, DCB_ATTR_CEE_APP_TABLE);
1410 	if (!app)
1411 		goto dcb_unlock;
1412 
1413 	list_for_each_entry(itr, &dcb_app_list, list) {
1414 		if (itr->ifindex == netdev->ifindex) {
1415 			struct nlattr *app_nest = nla_nest_start_noflag(skb,
1416 									DCB_ATTR_APP);
1417 			if (!app_nest)
1418 				goto dcb_unlock;
1419 
1420 			err = nla_put_u8(skb, DCB_APP_ATTR_IDTYPE,
1421 					 itr->app.selector);
1422 			if (err)
1423 				goto dcb_unlock;
1424 
1425 			err = nla_put_u16(skb, DCB_APP_ATTR_ID,
1426 					  itr->app.protocol);
1427 			if (err)
1428 				goto dcb_unlock;
1429 
1430 			err = nla_put_u8(skb, DCB_APP_ATTR_PRIORITY,
1431 					 itr->app.priority);
1432 			if (err)
1433 				goto dcb_unlock;
1434 
1435 			nla_nest_end(skb, app_nest);
1436 		}
1437 	}
1438 	nla_nest_end(skb, app);
1439 
1440 	if (netdev->dcbnl_ops->getdcbx)
1441 		dcbx = netdev->dcbnl_ops->getdcbx(netdev);
1442 	else
1443 		dcbx = -EOPNOTSUPP;
1444 
1445 	spin_unlock_bh(&dcb_lock);
1446 
1447 	/* features flags */
1448 	if (ops->getfeatcfg) {
1449 		struct nlattr *feat = nla_nest_start_noflag(skb,
1450 							    DCB_ATTR_CEE_FEAT);
1451 		if (!feat)
1452 			goto nla_put_failure;
1453 
1454 		for (i = DCB_FEATCFG_ATTR_ALL + 1; i <= DCB_FEATCFG_ATTR_MAX;
1455 		     i++)
1456 			if (!ops->getfeatcfg(netdev, i, &value) &&
1457 			    nla_put_u8(skb, i, value))
1458 				goto nla_put_failure;
1459 
1460 		nla_nest_end(skb, feat);
1461 	}
1462 
1463 	/* peer info if available */
1464 	if (ops->cee_peer_getpg) {
1465 		struct cee_pg pg;
1466 		memset(&pg, 0, sizeof(pg));
1467 		err = ops->cee_peer_getpg(netdev, &pg);
1468 		if (!err &&
1469 		    nla_put(skb, DCB_ATTR_CEE_PEER_PG, sizeof(pg), &pg))
1470 			goto nla_put_failure;
1471 	}
1472 
1473 	if (ops->cee_peer_getpfc) {
1474 		struct cee_pfc pfc;
1475 		memset(&pfc, 0, sizeof(pfc));
1476 		err = ops->cee_peer_getpfc(netdev, &pfc);
1477 		if (!err &&
1478 		    nla_put(skb, DCB_ATTR_CEE_PEER_PFC, sizeof(pfc), &pfc))
1479 			goto nla_put_failure;
1480 	}
1481 
1482 	if (ops->peer_getappinfo && ops->peer_getapptable) {
1483 		err = dcbnl_build_peer_app(netdev, skb,
1484 					   DCB_ATTR_CEE_PEER_APP_TABLE,
1485 					   DCB_ATTR_CEE_PEER_APP_INFO,
1486 					   DCB_ATTR_CEE_PEER_APP);
1487 		if (err)
1488 			goto nla_put_failure;
1489 	}
1490 	nla_nest_end(skb, cee);
1491 
1492 	/* DCBX state */
1493 	if (dcbx >= 0) {
1494 		err = nla_put_u8(skb, DCB_ATTR_DCBX, dcbx);
1495 		if (err)
1496 			goto nla_put_failure;
1497 	}
1498 	return 0;
1499 
1500 dcb_unlock:
1501 	spin_unlock_bh(&dcb_lock);
1502 nla_put_failure:
1503 	err = -EMSGSIZE;
1504 	return err;
1505 }
1506 
1507 static int dcbnl_notify(struct net_device *dev, int event, int cmd,
1508 			u32 seq, u32 portid, int dcbx_ver)
1509 {
1510 	struct net *net = dev_net(dev);
1511 	struct sk_buff *skb;
1512 	struct nlmsghdr *nlh;
1513 	const struct dcbnl_rtnl_ops *ops = dev->dcbnl_ops;
1514 	int err;
1515 
1516 	if (!ops)
1517 		return -EOPNOTSUPP;
1518 
1519 	skb = dcbnl_newmsg(event, cmd, portid, seq, 0, &nlh);
1520 	if (!skb)
1521 		return -ENOMEM;
1522 
1523 	if (dcbx_ver == DCB_CAP_DCBX_VER_IEEE)
1524 		err = dcbnl_ieee_fill(skb, dev);
1525 	else
1526 		err = dcbnl_cee_fill(skb, dev);
1527 
1528 	if (err < 0) {
1529 		/* Report error to broadcast listeners */
1530 		nlmsg_free(skb);
1531 		rtnl_set_sk_err(net, RTNLGRP_DCB, err);
1532 	} else {
1533 		/* End nlmsg and notify broadcast listeners */
1534 		nlmsg_end(skb, nlh);
1535 		rtnl_notify(skb, net, 0, RTNLGRP_DCB, NULL, GFP_KERNEL);
1536 	}
1537 
1538 	return err;
1539 }
1540 
1541 int dcbnl_ieee_notify(struct net_device *dev, int event, int cmd,
1542 		      u32 seq, u32 portid)
1543 {
1544 	return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_IEEE);
1545 }
1546 EXPORT_SYMBOL(dcbnl_ieee_notify);
1547 
1548 int dcbnl_cee_notify(struct net_device *dev, int event, int cmd,
1549 		     u32 seq, u32 portid)
1550 {
1551 	return dcbnl_notify(dev, event, cmd, seq, portid, DCB_CAP_DCBX_VER_CEE);
1552 }
1553 EXPORT_SYMBOL(dcbnl_cee_notify);
1554 
1555 /* Handle IEEE 802.1Qaz/802.1Qau/802.1Qbb SET commands.
1556  * If any requested operation can not be completed
1557  * the entire msg is aborted and error value is returned.
1558  * No attempt is made to reconcile the case where only part of the
1559  * cmd can be completed.
1560  */
1561 static int dcbnl_ieee_set(struct net_device *netdev, struct nlmsghdr *nlh,
1562 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1563 {
1564 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1565 	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
1566 	int prio;
1567 	int err;
1568 
1569 	if (!ops)
1570 		return -EOPNOTSUPP;
1571 
1572 	if (!tb[DCB_ATTR_IEEE])
1573 		return -EINVAL;
1574 
1575 	err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
1576 					  tb[DCB_ATTR_IEEE],
1577 					  dcbnl_ieee_policy, NULL);
1578 	if (err)
1579 		return err;
1580 
1581 	if (ieee[DCB_ATTR_IEEE_ETS] && ops->ieee_setets) {
1582 		struct ieee_ets *ets = nla_data(ieee[DCB_ATTR_IEEE_ETS]);
1583 
1584 		/* IEEE 802.1Qaz: BW allocation applies only to ETS TCs */
1585 		for (int i = 0; i < IEEE_8021QAZ_MAX_TCS; i++) {
1586 			if (ets->tc_tsa[i] != IEEE_8021QAZ_TSA_ETS &&
1587 			    ets->tc_tx_bw[i]) {
1588 				err = -EINVAL;
1589 				goto err;
1590 			}
1591 		}
1592 		err = ops->ieee_setets(netdev, ets);
1593 		if (err)
1594 			goto err;
1595 	}
1596 
1597 	if (ieee[DCB_ATTR_IEEE_MAXRATE] && ops->ieee_setmaxrate) {
1598 		struct ieee_maxrate *maxrate =
1599 			nla_data(ieee[DCB_ATTR_IEEE_MAXRATE]);
1600 		err = ops->ieee_setmaxrate(netdev, maxrate);
1601 		if (err)
1602 			goto err;
1603 	}
1604 
1605 	if (ieee[DCB_ATTR_IEEE_QCN] && ops->ieee_setqcn) {
1606 		struct ieee_qcn *qcn =
1607 			nla_data(ieee[DCB_ATTR_IEEE_QCN]);
1608 
1609 		err = ops->ieee_setqcn(netdev, qcn);
1610 		if (err)
1611 			goto err;
1612 	}
1613 
1614 	if (ieee[DCB_ATTR_IEEE_PFC] && ops->ieee_setpfc) {
1615 		struct ieee_pfc *pfc = nla_data(ieee[DCB_ATTR_IEEE_PFC]);
1616 		err = ops->ieee_setpfc(netdev, pfc);
1617 		if (err)
1618 			goto err;
1619 	}
1620 
1621 	if (ieee[DCB_ATTR_DCB_BUFFER] && ops->dcbnl_setbuffer) {
1622 		struct dcbnl_buffer *buffer =
1623 			nla_data(ieee[DCB_ATTR_DCB_BUFFER]);
1624 
1625 		for (prio = 0; prio < ARRAY_SIZE(buffer->prio2buffer); prio++) {
1626 			if (buffer->prio2buffer[prio] >= DCBX_MAX_BUFFERS) {
1627 				err = -EINVAL;
1628 				goto err;
1629 			}
1630 		}
1631 
1632 		err = ops->dcbnl_setbuffer(netdev, buffer);
1633 		if (err)
1634 			goto err;
1635 	}
1636 
1637 	if (ieee[DCB_ATTR_DCB_REWR_TABLE]) {
1638 		err = dcbnl_app_table_setdel(ieee[DCB_ATTR_DCB_REWR_TABLE],
1639 					     netdev,
1640 					     ops->dcbnl_setrewr ?: dcb_setrewr);
1641 		if (err)
1642 			goto err;
1643 	}
1644 
1645 	if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1646 		err = dcbnl_app_table_setdel(ieee[DCB_ATTR_IEEE_APP_TABLE],
1647 					     netdev, ops->ieee_setapp ?:
1648 					     dcb_ieee_setapp);
1649 		if (err)
1650 			goto err;
1651 	}
1652 
1653 	if (ieee[DCB_ATTR_DCB_APP_TRUST_TABLE]) {
1654 		u8 selectors[IEEE_8021QAZ_APP_SEL_MAX + 1] = {0};
1655 		struct nlattr *attr;
1656 		int nselectors = 0;
1657 		int rem;
1658 
1659 		if (!ops->dcbnl_setapptrust) {
1660 			err = -EOPNOTSUPP;
1661 			goto err;
1662 		}
1663 
1664 		nla_for_each_nested(attr, ieee[DCB_ATTR_DCB_APP_TRUST_TABLE],
1665 				    rem) {
1666 			enum ieee_attrs_app type = nla_type(attr);
1667 			u8 selector;
1668 			int i;
1669 
1670 			if (!dcbnl_app_attr_type_validate(type) ||
1671 			    nla_len(attr) != 1 ||
1672 			    nselectors >= sizeof(selectors)) {
1673 				err = -EINVAL;
1674 				goto err;
1675 			}
1676 
1677 			selector = nla_get_u8(attr);
1678 
1679 			if (!dcbnl_app_selector_validate(type, selector)) {
1680 				err = -EINVAL;
1681 				goto err;
1682 			}
1683 
1684 			/* Duplicate selector ? */
1685 			for (i = 0; i < nselectors; i++) {
1686 				if (selectors[i] == selector) {
1687 					err = -EINVAL;
1688 					goto err;
1689 				}
1690 			}
1691 
1692 			selectors[nselectors++] = selector;
1693 		}
1694 
1695 		err = ops->dcbnl_setapptrust(netdev, selectors, nselectors);
1696 		if (err)
1697 			goto err;
1698 	}
1699 
1700 err:
1701 	err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
1702 	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_SET, seq, 0);
1703 	return err;
1704 }
1705 
1706 static int dcbnl_ieee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1707 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1708 {
1709 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1710 
1711 	if (!ops)
1712 		return -EOPNOTSUPP;
1713 
1714 	return dcbnl_ieee_fill(skb, netdev);
1715 }
1716 
1717 static int dcbnl_ieee_del(struct net_device *netdev, struct nlmsghdr *nlh,
1718 			  u32 seq, struct nlattr **tb, struct sk_buff *skb)
1719 {
1720 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1721 	struct nlattr *ieee[DCB_ATTR_IEEE_MAX + 1];
1722 	int err;
1723 
1724 	if (!ops)
1725 		return -EOPNOTSUPP;
1726 
1727 	if (!tb[DCB_ATTR_IEEE])
1728 		return -EINVAL;
1729 
1730 	err = nla_parse_nested_deprecated(ieee, DCB_ATTR_IEEE_MAX,
1731 					  tb[DCB_ATTR_IEEE],
1732 					  dcbnl_ieee_policy, NULL);
1733 	if (err)
1734 		return err;
1735 
1736 	if (ieee[DCB_ATTR_IEEE_APP_TABLE]) {
1737 		err = dcbnl_app_table_setdel(ieee[DCB_ATTR_IEEE_APP_TABLE],
1738 					     netdev, ops->ieee_delapp ?:
1739 					     dcb_ieee_delapp);
1740 		if (err)
1741 			goto err;
1742 	}
1743 
1744 	if (ieee[DCB_ATTR_DCB_REWR_TABLE]) {
1745 		err = dcbnl_app_table_setdel(ieee[DCB_ATTR_DCB_REWR_TABLE],
1746 					     netdev,
1747 					     ops->dcbnl_delrewr ?: dcb_delrewr);
1748 		if (err)
1749 			goto err;
1750 	}
1751 
1752 err:
1753 	err = nla_put_u8(skb, DCB_ATTR_IEEE, err);
1754 	dcbnl_ieee_notify(netdev, RTM_SETDCB, DCB_CMD_IEEE_DEL, seq, 0);
1755 	return err;
1756 }
1757 
1758 
1759 /* DCBX configuration */
1760 static int dcbnl_getdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1761 			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
1762 {
1763 	if (!netdev->dcbnl_ops->getdcbx)
1764 		return -EOPNOTSUPP;
1765 
1766 	return nla_put_u8(skb, DCB_ATTR_DCBX,
1767 			  netdev->dcbnl_ops->getdcbx(netdev));
1768 }
1769 
1770 static int dcbnl_setdcbx(struct net_device *netdev, struct nlmsghdr *nlh,
1771 			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
1772 {
1773 	u8 value;
1774 
1775 	if (!netdev->dcbnl_ops->setdcbx)
1776 		return -EOPNOTSUPP;
1777 
1778 	if (!tb[DCB_ATTR_DCBX])
1779 		return -EINVAL;
1780 
1781 	value = nla_get_u8(tb[DCB_ATTR_DCBX]);
1782 
1783 	return nla_put_u8(skb, DCB_ATTR_DCBX,
1784 			  netdev->dcbnl_ops->setdcbx(netdev, value));
1785 }
1786 
1787 static int dcbnl_getfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1788 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
1789 {
1790 	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1], *nest;
1791 	u8 value;
1792 	int ret, i;
1793 	int getall = 0;
1794 
1795 	if (!netdev->dcbnl_ops->getfeatcfg)
1796 		return -EOPNOTSUPP;
1797 
1798 	if (!tb[DCB_ATTR_FEATCFG])
1799 		return -EINVAL;
1800 
1801 	ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
1802 					  tb[DCB_ATTR_FEATCFG],
1803 					  dcbnl_featcfg_nest, NULL);
1804 	if (ret)
1805 		return ret;
1806 
1807 	nest = nla_nest_start_noflag(skb, DCB_ATTR_FEATCFG);
1808 	if (!nest)
1809 		return -EMSGSIZE;
1810 
1811 	if (data[DCB_FEATCFG_ATTR_ALL])
1812 		getall = 1;
1813 
1814 	for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1815 		if (!getall && !data[i])
1816 			continue;
1817 
1818 		ret = netdev->dcbnl_ops->getfeatcfg(netdev, i, &value);
1819 		if (!ret)
1820 			ret = nla_put_u8(skb, i, value);
1821 
1822 		if (ret) {
1823 			nla_nest_cancel(skb, nest);
1824 			goto nla_put_failure;
1825 		}
1826 	}
1827 	nla_nest_end(skb, nest);
1828 
1829 nla_put_failure:
1830 	return ret;
1831 }
1832 
1833 static int dcbnl_setfeatcfg(struct net_device *netdev, struct nlmsghdr *nlh,
1834 			    u32 seq, struct nlattr **tb, struct sk_buff *skb)
1835 {
1836 	struct nlattr *data[DCB_FEATCFG_ATTR_MAX + 1];
1837 	int ret, i;
1838 	u8 value;
1839 
1840 	if (!netdev->dcbnl_ops->setfeatcfg)
1841 		return -ENOTSUPP;
1842 
1843 	if (!tb[DCB_ATTR_FEATCFG])
1844 		return -EINVAL;
1845 
1846 	ret = nla_parse_nested_deprecated(data, DCB_FEATCFG_ATTR_MAX,
1847 					  tb[DCB_ATTR_FEATCFG],
1848 					  dcbnl_featcfg_nest, NULL);
1849 
1850 	if (ret)
1851 		goto err;
1852 
1853 	for (i = DCB_FEATCFG_ATTR_ALL+1; i <= DCB_FEATCFG_ATTR_MAX; i++) {
1854 		if (data[i] == NULL)
1855 			continue;
1856 
1857 		value = nla_get_u8(data[i]);
1858 
1859 		ret = netdev->dcbnl_ops->setfeatcfg(netdev, i, value);
1860 
1861 		if (ret)
1862 			goto err;
1863 	}
1864 err:
1865 	ret = nla_put_u8(skb, DCB_ATTR_FEATCFG, ret);
1866 
1867 	return ret;
1868 }
1869 
1870 /* Handle CEE DCBX GET commands. */
1871 static int dcbnl_cee_get(struct net_device *netdev, struct nlmsghdr *nlh,
1872 			 u32 seq, struct nlattr **tb, struct sk_buff *skb)
1873 {
1874 	const struct dcbnl_rtnl_ops *ops = netdev->dcbnl_ops;
1875 
1876 	if (!ops)
1877 		return -EOPNOTSUPP;
1878 
1879 	return dcbnl_cee_fill(skb, netdev);
1880 }
1881 
1882 struct reply_func {
1883 	/* reply netlink message type */
1884 	int	type;
1885 
1886 	/* function to fill message contents */
1887 	int   (*cb)(struct net_device *, struct nlmsghdr *, u32,
1888 		    struct nlattr **, struct sk_buff *);
1889 };
1890 
1891 static const struct reply_func reply_funcs[DCB_CMD_MAX+1] = {
1892 	[DCB_CMD_GSTATE]	= { RTM_GETDCB, dcbnl_getstate },
1893 	[DCB_CMD_SSTATE]	= { RTM_SETDCB, dcbnl_setstate },
1894 	[DCB_CMD_PFC_GCFG]	= { RTM_GETDCB, dcbnl_getpfccfg },
1895 	[DCB_CMD_PFC_SCFG]	= { RTM_SETDCB, dcbnl_setpfccfg },
1896 	[DCB_CMD_GPERM_HWADDR]	= { RTM_GETDCB, dcbnl_getperm_hwaddr },
1897 	[DCB_CMD_GCAP]		= { RTM_GETDCB, dcbnl_getcap },
1898 	[DCB_CMD_GNUMTCS]	= { RTM_GETDCB, dcbnl_getnumtcs },
1899 	[DCB_CMD_SNUMTCS]	= { RTM_SETDCB, dcbnl_setnumtcs },
1900 	[DCB_CMD_PFC_GSTATE]	= { RTM_GETDCB, dcbnl_getpfcstate },
1901 	[DCB_CMD_PFC_SSTATE]	= { RTM_SETDCB, dcbnl_setpfcstate },
1902 	[DCB_CMD_GAPP]		= { RTM_GETDCB, dcbnl_getapp },
1903 	[DCB_CMD_SAPP]		= { RTM_SETDCB, dcbnl_setapp },
1904 	[DCB_CMD_PGTX_GCFG]	= { RTM_GETDCB, dcbnl_pgtx_getcfg },
1905 	[DCB_CMD_PGTX_SCFG]	= { RTM_SETDCB, dcbnl_pgtx_setcfg },
1906 	[DCB_CMD_PGRX_GCFG]	= { RTM_GETDCB, dcbnl_pgrx_getcfg },
1907 	[DCB_CMD_PGRX_SCFG]	= { RTM_SETDCB, dcbnl_pgrx_setcfg },
1908 	[DCB_CMD_SET_ALL]	= { RTM_SETDCB, dcbnl_setall },
1909 	[DCB_CMD_BCN_GCFG]	= { RTM_GETDCB, dcbnl_bcn_getcfg },
1910 	[DCB_CMD_BCN_SCFG]	= { RTM_SETDCB, dcbnl_bcn_setcfg },
1911 	[DCB_CMD_IEEE_GET]	= { RTM_GETDCB, dcbnl_ieee_get },
1912 	[DCB_CMD_IEEE_SET]	= { RTM_SETDCB, dcbnl_ieee_set },
1913 	[DCB_CMD_IEEE_DEL]	= { RTM_SETDCB, dcbnl_ieee_del },
1914 	[DCB_CMD_GDCBX]		= { RTM_GETDCB, dcbnl_getdcbx },
1915 	[DCB_CMD_SDCBX]		= { RTM_SETDCB, dcbnl_setdcbx },
1916 	[DCB_CMD_GFEATCFG]	= { RTM_GETDCB, dcbnl_getfeatcfg },
1917 	[DCB_CMD_SFEATCFG]	= { RTM_SETDCB, dcbnl_setfeatcfg },
1918 	[DCB_CMD_CEE_GET]	= { RTM_GETDCB, dcbnl_cee_get },
1919 };
1920 
1921 static int dcb_doit(struct sk_buff *skb, struct nlmsghdr *nlh,
1922 		    struct netlink_ext_ack *extack)
1923 {
1924 	struct net *net = sock_net(skb->sk);
1925 	struct net_device *netdev;
1926 	struct dcbmsg *dcb = nlmsg_data(nlh);
1927 	struct nlattr *tb[DCB_ATTR_MAX + 1];
1928 	u32 portid = NETLINK_CB(skb).portid;
1929 	int ret = -EINVAL;
1930 	struct sk_buff *reply_skb;
1931 	struct nlmsghdr *reply_nlh = NULL;
1932 	const struct reply_func *fn;
1933 
1934 	if ((nlh->nlmsg_type == RTM_SETDCB) && !netlink_capable(skb, CAP_NET_ADMIN))
1935 		return -EPERM;
1936 
1937 	ret = nlmsg_parse_deprecated(nlh, sizeof(*dcb), tb, DCB_ATTR_MAX,
1938 				     dcbnl_rtnl_policy, extack);
1939 	if (ret < 0)
1940 		return ret;
1941 
1942 	if (dcb->cmd > DCB_CMD_MAX)
1943 		return -EINVAL;
1944 
1945 	/* check if a reply function has been defined for the command */
1946 	fn = &reply_funcs[dcb->cmd];
1947 	if (!fn->cb)
1948 		return -EOPNOTSUPP;
1949 	if (fn->type == RTM_SETDCB && !netlink_capable(skb, CAP_NET_ADMIN))
1950 		return -EPERM;
1951 
1952 	if (!tb[DCB_ATTR_IFNAME])
1953 		return -EINVAL;
1954 
1955 	netdev = __dev_get_by_name(net, nla_data(tb[DCB_ATTR_IFNAME]));
1956 	if (!netdev)
1957 		return -ENODEV;
1958 
1959 	if (!netdev->dcbnl_ops)
1960 		return -EOPNOTSUPP;
1961 
1962 	reply_skb = dcbnl_newmsg(fn->type, dcb->cmd, portid, nlh->nlmsg_seq,
1963 				 nlh->nlmsg_flags, &reply_nlh);
1964 	if (!reply_skb)
1965 		return -ENOMEM;
1966 
1967 	ret = fn->cb(netdev, nlh, nlh->nlmsg_seq, tb, reply_skb);
1968 	if (ret < 0) {
1969 		nlmsg_free(reply_skb);
1970 		goto out;
1971 	}
1972 
1973 	nlmsg_end(reply_skb, reply_nlh);
1974 
1975 	ret = rtnl_unicast(reply_skb, net, portid);
1976 out:
1977 	return ret;
1978 }
1979 
1980 static struct dcb_app_type *dcb_rewr_lookup(const struct dcb_app *app,
1981 					    int ifindex, int proto)
1982 {
1983 	struct dcb_app_type *itr;
1984 
1985 	list_for_each_entry(itr, &dcb_rewr_list, list) {
1986 		if (itr->app.selector == app->selector &&
1987 		    itr->app.priority == app->priority &&
1988 		    itr->ifindex == ifindex &&
1989 		    ((proto == -1) || itr->app.protocol == proto))
1990 			return itr;
1991 	}
1992 
1993 	return NULL;
1994 }
1995 
1996 static struct dcb_app_type *dcb_app_lookup(const struct dcb_app *app,
1997 					   int ifindex, int prio)
1998 {
1999 	struct dcb_app_type *itr;
2000 
2001 	list_for_each_entry(itr, &dcb_app_list, list) {
2002 		if (itr->app.selector == app->selector &&
2003 		    itr->app.protocol == app->protocol &&
2004 		    itr->ifindex == ifindex &&
2005 		    ((prio == -1) || itr->app.priority == prio))
2006 			return itr;
2007 	}
2008 
2009 	return NULL;
2010 }
2011 
2012 static int dcb_app_add(struct list_head *list, const struct dcb_app *app,
2013 		       int ifindex)
2014 {
2015 	struct dcb_app_type *entry;
2016 
2017 	entry = kmalloc_obj(*entry, GFP_ATOMIC);
2018 	if (!entry)
2019 		return -ENOMEM;
2020 
2021 	memcpy(&entry->app, app, sizeof(*app));
2022 	entry->ifindex = ifindex;
2023 	list_add(&entry->list, list);
2024 
2025 	return 0;
2026 }
2027 
2028 /**
2029  * dcb_getapp - retrieve the DCBX application user priority
2030  * @dev: network interface
2031  * @app: application to get user priority of
2032  *
2033  * On success returns a non-zero 802.1p user priority bitmap
2034  * otherwise returns 0 as the invalid user priority bitmap to
2035  * indicate an error.
2036  */
2037 u8 dcb_getapp(struct net_device *dev, struct dcb_app *app)
2038 {
2039 	struct dcb_app_type *itr;
2040 	u8 prio = 0;
2041 
2042 	spin_lock_bh(&dcb_lock);
2043 	itr = dcb_app_lookup(app, dev->ifindex, -1);
2044 	if (itr)
2045 		prio = itr->app.priority;
2046 	spin_unlock_bh(&dcb_lock);
2047 
2048 	return prio;
2049 }
2050 EXPORT_SYMBOL(dcb_getapp);
2051 
2052 /**
2053  * dcb_setapp - add CEE dcb application data to app list
2054  * @dev: network interface
2055  * @new: application data to add
2056  *
2057  * Priority 0 is an invalid priority in CEE spec. This routine
2058  * removes applications from the app list if the priority is
2059  * set to zero. Priority is expected to be 8-bit 802.1p user priority bitmap
2060  */
2061 int dcb_setapp(struct net_device *dev, struct dcb_app *new)
2062 {
2063 	struct dcb_app_type *itr;
2064 	struct dcb_app_type event;
2065 	int err = 0;
2066 
2067 	event.ifindex = dev->ifindex;
2068 	memcpy(&event.app, new, sizeof(event.app));
2069 	if (dev->dcbnl_ops->getdcbx)
2070 		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
2071 
2072 	spin_lock_bh(&dcb_lock);
2073 	/* Search for existing match and replace */
2074 	itr = dcb_app_lookup(new, dev->ifindex, -1);
2075 	if (itr) {
2076 		if (new->priority)
2077 			itr->app.priority = new->priority;
2078 		else {
2079 			list_del(&itr->list);
2080 			kfree(itr);
2081 		}
2082 		goto out;
2083 	}
2084 	/* App type does not exist add new application type */
2085 	if (new->priority)
2086 		err = dcb_app_add(&dcb_app_list, new, dev->ifindex);
2087 out:
2088 	spin_unlock_bh(&dcb_lock);
2089 	if (!err)
2090 		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
2091 	return err;
2092 }
2093 EXPORT_SYMBOL(dcb_setapp);
2094 
2095 /**
2096  * dcb_ieee_getapp_mask - retrieve the IEEE DCB application priority
2097  * @dev: network interface
2098  * @app: where to store the retrieve application data
2099  *
2100  * Helper routine which on success returns a non-zero 802.1Qaz user
2101  * priority bitmap otherwise returns 0 to indicate the dcb_app was
2102  * not found in APP list.
2103  */
2104 u8 dcb_ieee_getapp_mask(struct net_device *dev, struct dcb_app *app)
2105 {
2106 	struct dcb_app_type *itr;
2107 	u8 prio = 0;
2108 
2109 	spin_lock_bh(&dcb_lock);
2110 	itr = dcb_app_lookup(app, dev->ifindex, -1);
2111 	if (itr)
2112 		prio |= 1 << itr->app.priority;
2113 	spin_unlock_bh(&dcb_lock);
2114 
2115 	return prio;
2116 }
2117 EXPORT_SYMBOL(dcb_ieee_getapp_mask);
2118 
2119 /* Get protocol value from rewrite entry. */
2120 u16 dcb_getrewr(struct net_device *dev, struct dcb_app *app)
2121 {
2122 	struct dcb_app_type *itr;
2123 	u16 proto = 0;
2124 
2125 	spin_lock_bh(&dcb_lock);
2126 	itr = dcb_rewr_lookup(app, dev->ifindex, -1);
2127 	if (itr)
2128 		proto = itr->app.protocol;
2129 	spin_unlock_bh(&dcb_lock);
2130 
2131 	return proto;
2132 }
2133 EXPORT_SYMBOL(dcb_getrewr);
2134 
2135  /* Add rewrite entry to the rewrite list. */
2136 int dcb_setrewr(struct net_device *dev, struct dcb_app *new)
2137 {
2138 	int err;
2139 
2140 	spin_lock_bh(&dcb_lock);
2141 	/* Search for existing match and abort if found. */
2142 	if (dcb_rewr_lookup(new, dev->ifindex, new->protocol)) {
2143 		err = -EEXIST;
2144 		goto out;
2145 	}
2146 
2147 	err = dcb_app_add(&dcb_rewr_list, new, dev->ifindex);
2148 out:
2149 	spin_unlock_bh(&dcb_lock);
2150 
2151 	return err;
2152 }
2153 EXPORT_SYMBOL(dcb_setrewr);
2154 
2155 /* Delete rewrite entry from the rewrite list. */
2156 int dcb_delrewr(struct net_device *dev, struct dcb_app *del)
2157 {
2158 	struct dcb_app_type *itr;
2159 	int err = -ENOENT;
2160 
2161 	spin_lock_bh(&dcb_lock);
2162 	/* Search for existing match and remove it. */
2163 	itr = dcb_rewr_lookup(del, dev->ifindex, del->protocol);
2164 	if (itr) {
2165 		list_del(&itr->list);
2166 		kfree(itr);
2167 		err = 0;
2168 	}
2169 
2170 	spin_unlock_bh(&dcb_lock);
2171 
2172 	return err;
2173 }
2174 EXPORT_SYMBOL(dcb_delrewr);
2175 
2176 /**
2177  * dcb_ieee_setapp - add IEEE dcb application data to app list
2178  * @dev: network interface
2179  * @new: application data to add
2180  *
2181  * This adds Application data to the list. Multiple application
2182  * entries may exists for the same selector and protocol as long
2183  * as the priorities are different. Priority is expected to be a
2184  * 3-bit unsigned integer
2185  */
2186 int dcb_ieee_setapp(struct net_device *dev, struct dcb_app *new)
2187 {
2188 	struct dcb_app_type event;
2189 	int err = 0;
2190 
2191 	event.ifindex = dev->ifindex;
2192 	memcpy(&event.app, new, sizeof(event.app));
2193 	if (dev->dcbnl_ops->getdcbx)
2194 		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
2195 
2196 	spin_lock_bh(&dcb_lock);
2197 	/* Search for existing match and abort if found */
2198 	if (dcb_app_lookup(new, dev->ifindex, new->priority)) {
2199 		err = -EEXIST;
2200 		goto out;
2201 	}
2202 
2203 	err = dcb_app_add(&dcb_app_list, new, dev->ifindex);
2204 out:
2205 	spin_unlock_bh(&dcb_lock);
2206 	if (!err)
2207 		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
2208 	return err;
2209 }
2210 EXPORT_SYMBOL(dcb_ieee_setapp);
2211 
2212 /**
2213  * dcb_ieee_delapp - delete IEEE dcb application data from list
2214  * @dev: network interface
2215  * @del: application data to delete
2216  *
2217  * This removes a matching APP data from the APP list
2218  */
2219 int dcb_ieee_delapp(struct net_device *dev, struct dcb_app *del)
2220 {
2221 	struct dcb_app_type *itr;
2222 	struct dcb_app_type event;
2223 	int err = -ENOENT;
2224 
2225 	event.ifindex = dev->ifindex;
2226 	memcpy(&event.app, del, sizeof(event.app));
2227 	if (dev->dcbnl_ops->getdcbx)
2228 		event.dcbx = dev->dcbnl_ops->getdcbx(dev);
2229 
2230 	spin_lock_bh(&dcb_lock);
2231 	/* Search for existing match and remove it. */
2232 	if ((itr = dcb_app_lookup(del, dev->ifindex, del->priority))) {
2233 		list_del(&itr->list);
2234 		kfree(itr);
2235 		err = 0;
2236 	}
2237 
2238 	spin_unlock_bh(&dcb_lock);
2239 	if (!err)
2240 		call_dcbevent_notifiers(DCB_APP_EVENT, &event);
2241 	return err;
2242 }
2243 EXPORT_SYMBOL(dcb_ieee_delapp);
2244 
2245 /* dcb_getrewr_prio_pcp_mask_map - For a given device, find mapping from
2246  * priorities to the PCP and DEI values assigned to that priority.
2247  */
2248 void dcb_getrewr_prio_pcp_mask_map(const struct net_device *dev,
2249 				   struct dcb_rewr_prio_pcp_map *p_map)
2250 {
2251 	int ifindex = dev->ifindex;
2252 	struct dcb_app_type *itr;
2253 	u8 prio;
2254 
2255 	memset(p_map->map, 0, sizeof(p_map->map));
2256 
2257 	spin_lock_bh(&dcb_lock);
2258 	list_for_each_entry(itr, &dcb_rewr_list, list) {
2259 		if (itr->ifindex == ifindex &&
2260 		    itr->app.selector == DCB_APP_SEL_PCP &&
2261 		    itr->app.protocol < 16 &&
2262 		    itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
2263 			prio = itr->app.priority;
2264 			p_map->map[prio] |= 1 << itr->app.protocol;
2265 		}
2266 	}
2267 	spin_unlock_bh(&dcb_lock);
2268 }
2269 EXPORT_SYMBOL(dcb_getrewr_prio_pcp_mask_map);
2270 
2271 /* dcb_getrewr_prio_dscp_mask_map - For a given device, find mapping from
2272  * priorities to the DSCP values assigned to that priority.
2273  */
2274 void dcb_getrewr_prio_dscp_mask_map(const struct net_device *dev,
2275 				    struct dcb_ieee_app_prio_map *p_map)
2276 {
2277 	int ifindex = dev->ifindex;
2278 	struct dcb_app_type *itr;
2279 	u8 prio;
2280 
2281 	memset(p_map->map, 0, sizeof(p_map->map));
2282 
2283 	spin_lock_bh(&dcb_lock);
2284 	list_for_each_entry(itr, &dcb_rewr_list, list) {
2285 		if (itr->ifindex == ifindex &&
2286 		    itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
2287 		    itr->app.protocol < 64 &&
2288 		    itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
2289 			prio = itr->app.priority;
2290 			p_map->map[prio] |= 1ULL << itr->app.protocol;
2291 		}
2292 	}
2293 	spin_unlock_bh(&dcb_lock);
2294 }
2295 EXPORT_SYMBOL(dcb_getrewr_prio_dscp_mask_map);
2296 
2297 /*
2298  * dcb_ieee_getapp_prio_dscp_mask_map - For a given device, find mapping from
2299  * priorities to the DSCP values assigned to that priority. Initialize p_map
2300  * such that each map element holds a bit mask of DSCP values configured for
2301  * that priority by APP entries.
2302  */
2303 void dcb_ieee_getapp_prio_dscp_mask_map(const struct net_device *dev,
2304 					struct dcb_ieee_app_prio_map *p_map)
2305 {
2306 	int ifindex = dev->ifindex;
2307 	struct dcb_app_type *itr;
2308 	u8 prio;
2309 
2310 	memset(p_map->map, 0, sizeof(p_map->map));
2311 
2312 	spin_lock_bh(&dcb_lock);
2313 	list_for_each_entry(itr, &dcb_app_list, list) {
2314 		if (itr->ifindex == ifindex &&
2315 		    itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
2316 		    itr->app.protocol < 64 &&
2317 		    itr->app.priority < IEEE_8021QAZ_MAX_TCS) {
2318 			prio = itr->app.priority;
2319 			p_map->map[prio] |= 1ULL << itr->app.protocol;
2320 		}
2321 	}
2322 	spin_unlock_bh(&dcb_lock);
2323 }
2324 EXPORT_SYMBOL(dcb_ieee_getapp_prio_dscp_mask_map);
2325 
2326 /*
2327  * dcb_ieee_getapp_dscp_prio_mask_map - For a given device, find mapping from
2328  * DSCP values to the priorities assigned to that DSCP value. Initialize p_map
2329  * such that each map element holds a bit mask of priorities configured for a
2330  * given DSCP value by APP entries.
2331  */
2332 void
2333 dcb_ieee_getapp_dscp_prio_mask_map(const struct net_device *dev,
2334 				   struct dcb_ieee_app_dscp_map *p_map)
2335 {
2336 	int ifindex = dev->ifindex;
2337 	struct dcb_app_type *itr;
2338 
2339 	memset(p_map->map, 0, sizeof(p_map->map));
2340 
2341 	spin_lock_bh(&dcb_lock);
2342 	list_for_each_entry(itr, &dcb_app_list, list) {
2343 		if (itr->ifindex == ifindex &&
2344 		    itr->app.selector == IEEE_8021QAZ_APP_SEL_DSCP &&
2345 		    itr->app.protocol < 64 &&
2346 		    itr->app.priority < IEEE_8021QAZ_MAX_TCS)
2347 			p_map->map[itr->app.protocol] |= 1 << itr->app.priority;
2348 	}
2349 	spin_unlock_bh(&dcb_lock);
2350 }
2351 EXPORT_SYMBOL(dcb_ieee_getapp_dscp_prio_mask_map);
2352 
2353 /*
2354  * Per 802.1Q-2014, the selector value of 1 is used for matching on Ethernet
2355  * type, with valid PID values >= 1536. A special meaning is then assigned to
2356  * protocol value of 0: "default priority. For use when priority is not
2357  * otherwise specified".
2358  *
2359  * dcb_ieee_getapp_default_prio_mask - For a given device, find all APP entries
2360  * of the form {$PRIO, ETHERTYPE, 0} and construct a bit mask of all default
2361  * priorities set by these entries.
2362  */
2363 u8 dcb_ieee_getapp_default_prio_mask(const struct net_device *dev)
2364 {
2365 	int ifindex = dev->ifindex;
2366 	struct dcb_app_type *itr;
2367 	u8 mask = 0;
2368 
2369 	spin_lock_bh(&dcb_lock);
2370 	list_for_each_entry(itr, &dcb_app_list, list) {
2371 		if (itr->ifindex == ifindex &&
2372 		    itr->app.selector == IEEE_8021QAZ_APP_SEL_ETHERTYPE &&
2373 		    itr->app.protocol == 0 &&
2374 		    itr->app.priority < IEEE_8021QAZ_MAX_TCS)
2375 			mask |= 1 << itr->app.priority;
2376 	}
2377 	spin_unlock_bh(&dcb_lock);
2378 
2379 	return mask;
2380 }
2381 EXPORT_SYMBOL(dcb_ieee_getapp_default_prio_mask);
2382 
2383 static void dcbnl_flush_dev(struct net_device *dev)
2384 {
2385 	struct dcb_app_type *itr, *tmp;
2386 
2387 	spin_lock_bh(&dcb_lock);
2388 
2389 	list_for_each_entry_safe(itr, tmp, &dcb_app_list, list) {
2390 		if (itr->ifindex == dev->ifindex) {
2391 			list_del(&itr->list);
2392 			kfree(itr);
2393 		}
2394 	}
2395 
2396 	list_for_each_entry_safe(itr, tmp, &dcb_rewr_list, list) {
2397 		if (itr->ifindex == dev->ifindex) {
2398 			list_del(&itr->list);
2399 			kfree(itr);
2400 		}
2401 	}
2402 
2403 	spin_unlock_bh(&dcb_lock);
2404 }
2405 
2406 static int dcbnl_netdevice_event(struct notifier_block *nb,
2407 				 unsigned long event, void *ptr)
2408 {
2409 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2410 
2411 	switch (event) {
2412 	case NETDEV_UNREGISTER:
2413 		if (!dev->dcbnl_ops)
2414 			return NOTIFY_DONE;
2415 
2416 		dcbnl_flush_dev(dev);
2417 
2418 		return NOTIFY_OK;
2419 	default:
2420 		return NOTIFY_DONE;
2421 	}
2422 }
2423 
2424 static struct notifier_block dcbnl_nb __read_mostly = {
2425 	.notifier_call  = dcbnl_netdevice_event,
2426 };
2427 
2428 static const struct rtnl_msg_handler dcbnl_rtnl_msg_handlers[] __initconst = {
2429 	{.msgtype = RTM_GETDCB, .doit = dcb_doit},
2430 	{.msgtype = RTM_SETDCB, .doit = dcb_doit},
2431 };
2432 
2433 static int __init dcbnl_init(void)
2434 {
2435 	int err;
2436 
2437 	err = register_netdevice_notifier(&dcbnl_nb);
2438 	if (err)
2439 		return err;
2440 
2441 	rtnl_register_many(dcbnl_rtnl_msg_handlers);
2442 
2443 	return 0;
2444 }
2445 device_initcall(dcbnl_init);
2446