1/* SPDX-License-Identifier: ((GPL-2.0 WITH Linux-syscall-note) OR BSD-3-Clause) */
2/*
3 * include/uapi/linux/tipc_config.h: Header for TIPC configuration interface
4 *
5 * Copyright (c) 2003-2006, Ericsson AB
6 * Copyright (c) 2005-2007, 2010-2011, Wind River Systems
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 *    notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 *    notice, this list of conditions and the following disclaimer in the
16 *    documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 *    contributors may be used to endorse or promote products derived from
19 *    this software without specific prior written permission.
20 *
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#ifndef _LINUX_TIPC_CONFIG_H_
39#define _LINUX_TIPC_CONFIG_H_
40
41#include <linux/types.h>
42#include <linux/string.h>
43#include <linux/tipc.h>
44#include <asm/byteorder.h>
45
46#ifndef __KERNEL__
47#include <arpa/inet.h> /* for ntohs etc. */
48#endif
49
50/*
51 * Configuration
52 *
53 * All configuration management messaging involves sending a request message
54 * to the TIPC configuration service on a node, which sends a reply message
55 * back.  (In the future multi-message replies may be supported.)
56 *
57 * Both request and reply messages consist of a transport header and payload.
58 * The transport header contains info about the desired operation;
59 * the payload consists of zero or more type/length/value (TLV) items
60 * which specify parameters or results for the operation.
61 *
62 * For many operations, the request and reply messages have a fixed number
63 * of TLVs (usually zero or one); however, some reply messages may return
64 * a variable number of TLVs.  A failed request is denoted by the presence
65 * of an "error string" TLV in the reply message instead of the TLV(s) the
66 * reply should contain if the request succeeds.
67 */
68
69/*
70 * Public commands:
71 * May be issued by any process.
72 * Accepted by own node, or by remote node only if remote management enabled.
73 */
74
75#define  TIPC_CMD_NOOP              0x0000    /* tx none, rx none */
76#define  TIPC_CMD_GET_NODES         0x0001    /* tx net_addr, rx node_info(s) */
77#define  TIPC_CMD_GET_MEDIA_NAMES   0x0002    /* tx none, rx media_name(s) */
78#define  TIPC_CMD_GET_BEARER_NAMES  0x0003    /* tx none, rx bearer_name(s) */
79#define  TIPC_CMD_GET_LINKS         0x0004    /* tx net_addr, rx link_info(s) */
80#define  TIPC_CMD_SHOW_NAME_TABLE   0x0005    /* tx name_tbl_query, rx ultra_string */
81#define  TIPC_CMD_SHOW_PORTS        0x0006    /* tx none, rx ultra_string */
82#define  TIPC_CMD_SHOW_LINK_STATS   0x000B    /* tx link_name, rx ultra_string */
83#define  TIPC_CMD_SHOW_STATS        0x000F    /* tx unsigned, rx ultra_string */
84
85/*
86 * Protected commands:
87 * May only be issued by "network administration capable" process.
88 * Accepted by own node, or by remote node only if remote management enabled
89 * and this node is zone manager.
90 */
91
92#define  TIPC_CMD_GET_REMOTE_MNG    0x4003    /* tx none, rx unsigned */
93#define  TIPC_CMD_GET_MAX_PORTS     0x4004    /* tx none, rx unsigned */
94#define  TIPC_CMD_GET_MAX_PUBL      0x4005    /* obsoleted */
95#define  TIPC_CMD_GET_MAX_SUBSCR    0x4006    /* obsoleted */
96#define  TIPC_CMD_GET_MAX_ZONES     0x4007    /* obsoleted */
97#define  TIPC_CMD_GET_MAX_CLUSTERS  0x4008    /* obsoleted */
98#define  TIPC_CMD_GET_MAX_NODES     0x4009    /* obsoleted */
99#define  TIPC_CMD_GET_MAX_SLAVES    0x400A    /* obsoleted */
100#define  TIPC_CMD_GET_NETID         0x400B    /* tx none, rx unsigned */
101
102#define  TIPC_CMD_ENABLE_BEARER     0x4101    /* tx bearer_config, rx none */
103#define  TIPC_CMD_DISABLE_BEARER    0x4102    /* tx bearer_name, rx none */
104#define  TIPC_CMD_SET_LINK_TOL      0x4107    /* tx link_config, rx none */
105#define  TIPC_CMD_SET_LINK_PRI      0x4108    /* tx link_config, rx none */
106#define  TIPC_CMD_SET_LINK_WINDOW   0x4109    /* tx link_config, rx none */
107#define  TIPC_CMD_SET_LOG_SIZE      0x410A    /* obsoleted */
108#define  TIPC_CMD_DUMP_LOG          0x410B    /* obsoleted */
109#define  TIPC_CMD_RESET_LINK_STATS  0x410C    /* tx link_name, rx none */
110
111/*
112 * Private commands:
113 * May only be issued by "network administration capable" process.
114 * Accepted by own node only; cannot be used on a remote node.
115 */
116
117#define  TIPC_CMD_SET_NODE_ADDR     0x8001    /* tx net_addr, rx none */
118#define  TIPC_CMD_SET_REMOTE_MNG    0x8003    /* tx unsigned, rx none */
119#define  TIPC_CMD_SET_MAX_PORTS     0x8004    /* tx unsigned, rx none */
120#define  TIPC_CMD_SET_MAX_PUBL      0x8005    /* obsoleted */
121#define  TIPC_CMD_SET_MAX_SUBSCR    0x8006    /* obsoleted */
122#define  TIPC_CMD_SET_MAX_ZONES     0x8007    /* obsoleted */
123#define  TIPC_CMD_SET_MAX_CLUSTERS  0x8008    /* obsoleted */
124#define  TIPC_CMD_SET_MAX_NODES     0x8009    /* obsoleted */
125#define  TIPC_CMD_SET_MAX_SLAVES    0x800A    /* obsoleted */
126#define  TIPC_CMD_SET_NETID         0x800B    /* tx unsigned, rx none */
127
128/*
129 * Reserved commands:
130 * May not be issued by any process.
131 * Used internally by TIPC.
132 */
133
134#define  TIPC_CMD_NOT_NET_ADMIN     0xC001    /* tx none, rx none */
135
136/*
137 * TLV types defined for TIPC
138 */
139
140#define TIPC_TLV_NONE		0	/* no TLV present */
141#define TIPC_TLV_VOID		1	/* empty TLV (0 data bytes)*/
142#define TIPC_TLV_UNSIGNED	2	/* 32-bit integer */
143#define TIPC_TLV_STRING		3	/* char[128] (max) */
144#define TIPC_TLV_LARGE_STRING	4	/* char[2048] (max) */
145#define TIPC_TLV_ULTRA_STRING	5	/* char[32768] (max) */
146
147#define TIPC_TLV_ERROR_STRING	16	/* char[128] containing "error code" */
148#define TIPC_TLV_NET_ADDR	17	/* 32-bit integer denoting <Z.C.N> */
149#define TIPC_TLV_MEDIA_NAME	18	/* char[TIPC_MAX_MEDIA_NAME] */
150#define TIPC_TLV_BEARER_NAME	19	/* char[TIPC_MAX_BEARER_NAME] */
151#define TIPC_TLV_LINK_NAME	20	/* char[TIPC_MAX_LINK_NAME] */
152#define TIPC_TLV_NODE_INFO	21	/* struct tipc_node_info */
153#define TIPC_TLV_LINK_INFO	22	/* struct tipc_link_info */
154#define TIPC_TLV_BEARER_CONFIG	23	/* struct tipc_bearer_config */
155#define TIPC_TLV_LINK_CONFIG	24	/* struct tipc_link_config */
156#define TIPC_TLV_NAME_TBL_QUERY	25	/* struct tipc_name_table_query */
157#define TIPC_TLV_PORT_REF	26	/* 32-bit port reference */
158
159/*
160 * Link priority limits (min, default, max, media default)
161 */
162
163#define TIPC_MIN_LINK_PRI	0
164#define TIPC_DEF_LINK_PRI	10
165#define TIPC_MAX_LINK_PRI	31
166#define TIPC_MEDIA_LINK_PRI	(TIPC_MAX_LINK_PRI + 1)
167
168/*
169 * Link tolerance limits (min, default, max), in ms
170 */
171
172#define TIPC_MIN_LINK_TOL 50
173#define TIPC_DEF_LINK_TOL 1500
174#define TIPC_MAX_LINK_TOL 30000
175
176#if (TIPC_MIN_LINK_TOL < 16)
177#error "TIPC_MIN_LINK_TOL is too small (abort limit may be NaN)"
178#endif
179
180/*
181 * Link window limits (min, default, max), in packets
182 */
183
184#define TIPC_MIN_LINK_WIN 16
185#define TIPC_DEF_LINK_WIN 50
186#define TIPC_MAX_LINK_WIN 8191
187
188/*
189 * Default MTU for UDP media
190 */
191
192#define TIPC_DEF_LINK_UDP_MTU 14000
193
194struct tipc_node_info {
195	__be32 addr;			/* network address of node */
196	__be32 up;			/* 0=down, 1= up */
197};
198
199struct tipc_link_info {
200	__be32 dest;			/* network address of peer node */
201	__be32 up;			/* 0=down, 1=up */
202	char str[TIPC_MAX_LINK_NAME];	/* link name */
203};
204
205struct tipc_bearer_config {
206	__be32 priority;		/* Range [1,31]. Override per link  */
207	__be32 disc_domain;		/* <Z.C.N> describing desired nodes */
208	char name[TIPC_MAX_BEARER_NAME];
209};
210
211struct tipc_link_config {
212	__be32 value;
213	char name[TIPC_MAX_LINK_NAME];
214};
215
216#define TIPC_NTQ_ALLTYPES 0x80000000
217
218struct tipc_name_table_query {
219	__be32 depth;	/* 1:type, 2:+name info, 3:+port info, 4+:+debug info */
220	__be32 type;	/* {t,l,u} info ignored if high bit of "depth" is set */
221	__be32 lowbound; /* (i.e. displays all entries of name table) */
222	__be32 upbound;
223};
224
225/*
226 * The error string TLV is a null-terminated string describing the cause
227 * of the request failure.  To simplify error processing (and to save space)
228 * the first character of the string can be a special error code character
229 * (lying by the range 0x80 to 0xFF) which represents a pre-defined reason.
230 */
231
232#define TIPC_CFG_TLV_ERROR      "\x80"  /* request contains incorrect TLV(s) */
233#define TIPC_CFG_NOT_NET_ADMIN  "\x81"	/* must be network administrator */
234#define TIPC_CFG_NOT_ZONE_MSTR	"\x82"	/* must be zone master */
235#define TIPC_CFG_NO_REMOTE	"\x83"	/* remote management not enabled */
236#define TIPC_CFG_NOT_SUPPORTED  "\x84"	/* request is not supported by TIPC */
237#define TIPC_CFG_INVALID_VALUE  "\x85"  /* request has invalid argument value */
238
239/*
240 * A TLV consists of a descriptor, followed by the TLV value.
241 * TLV descriptor fields are stored in network byte order;
242 * TLV values must also be stored in network byte order (where applicable).
243 * TLV descriptors must be aligned to addresses which are multiple of 4,
244 * so up to 3 bytes of padding may exist at the end of the TLV value area.
245 * There must not be any padding between the TLV descriptor and its value.
246 */
247
248struct tlv_desc {
249	__be16 tlv_len;		/* TLV length (descriptor + value) */
250	__be16 tlv_type;		/* TLV identifier */
251};
252
253#define TLV_ALIGNTO 4
254
255#define TLV_ALIGN(datalen) (((datalen)+(TLV_ALIGNTO-1)) & ~(TLV_ALIGNTO-1))
256#define TLV_LENGTH(datalen) (sizeof(struct tlv_desc) + (datalen))
257#define TLV_SPACE(datalen) (TLV_ALIGN(TLV_LENGTH(datalen)))
258#define TLV_DATA(tlv) ((void *)((char *)(tlv) + TLV_LENGTH(0)))
259
260static inline int TLV_OK(const void *tlv, __u16 space)
261{
262	/*
263	 * Would also like to check that "tlv" is a multiple of 4,
264	 * but don't know how to do this in a portable way.
265	 * - Tried doing (!(tlv & (TLV_ALIGNTO-1))), but GCC compiler
266	 *   won't allow binary "&" with a pointer.
267	 * - Tried casting "tlv" to integer type, but causes warning about size
268	 *   mismatch when pointer is bigger than chosen type (int, long, ...).
269	 */
270
271	return (space >= TLV_SPACE(0)) &&
272		(ntohs(((struct tlv_desc *)tlv)->tlv_len) <= space);
273}
274
275static inline int TLV_CHECK(const void *tlv, __u16 space, __u16 exp_type)
276{
277	return TLV_OK(tlv, space) &&
278		(ntohs(((struct tlv_desc *)tlv)->tlv_type) == exp_type);
279}
280
281static inline int TLV_GET_LEN(struct tlv_desc *tlv)
282{
283	return ntohs(tlv->tlv_len);
284}
285
286static inline void TLV_SET_LEN(struct tlv_desc *tlv, __u16 len)
287{
288	tlv->tlv_len = htons(len);
289}
290
291static inline int TLV_CHECK_TYPE(struct tlv_desc *tlv,  __u16 type)
292{
293	return (ntohs(tlv->tlv_type) == type);
294}
295
296static inline void TLV_SET_TYPE(struct tlv_desc *tlv, __u16 type)
297{
298	tlv->tlv_type = htons(type);
299}
300
301static inline int TLV_SET(void *tlv, __u16 type, void *data, __u16 len)
302{
303	struct tlv_desc *tlv_ptr;
304	int tlv_len;
305
306	tlv_len = TLV_LENGTH(len);
307	tlv_ptr = (struct tlv_desc *)tlv;
308	tlv_ptr->tlv_type = htons(type);
309	tlv_ptr->tlv_len  = htons(tlv_len);
310	if (len && data) {
311		memcpy(TLV_DATA(tlv_ptr), data, len);
312		memset((char *)TLV_DATA(tlv_ptr) + len, 0, TLV_SPACE(len) - tlv_len);
313	}
314	return TLV_SPACE(len);
315}
316
317/*
318 * A TLV list descriptor simplifies processing of messages
319 * containing multiple TLVs.
320 */
321
322struct tlv_list_desc {
323	struct tlv_desc *tlv_ptr;	/* ptr to current TLV */
324	__u32 tlv_space;		/* # bytes from curr TLV to list end */
325};
326
327static inline void TLV_LIST_INIT(struct tlv_list_desc *list,
328				 void *data, __u32 space)
329{
330	list->tlv_ptr = (struct tlv_desc *)data;
331	list->tlv_space = space;
332}
333
334static inline int TLV_LIST_EMPTY(struct tlv_list_desc *list)
335{
336	return (list->tlv_space == 0);
337}
338
339static inline int TLV_LIST_CHECK(struct tlv_list_desc *list, __u16 exp_type)
340{
341	return TLV_CHECK(list->tlv_ptr, list->tlv_space, exp_type);
342}
343
344static inline void *TLV_LIST_DATA(struct tlv_list_desc *list)
345{
346	return TLV_DATA(list->tlv_ptr);
347}
348
349static inline void TLV_LIST_STEP(struct tlv_list_desc *list)
350{
351	__u16 tlv_space = TLV_ALIGN(ntohs(list->tlv_ptr->tlv_len));
352
353	list->tlv_ptr = (struct tlv_desc *)((char *)list->tlv_ptr + tlv_space);
354	list->tlv_space -= tlv_space;
355}
356
357/*
358 * Configuration messages exchanged via NETLINK_GENERIC use the following
359 * family id, name, version and command.
360 */
361#define TIPC_GENL_NAME		"TIPC"
362#define TIPC_GENL_VERSION	0x1
363#define TIPC_GENL_CMD		0x1
364
365/*
366 * TIPC specific header used in NETLINK_GENERIC requests.
367 */
368struct tipc_genlmsghdr {
369	__u32 dest;		/* Destination address */
370	__u16 cmd;		/* Command */
371	__u16 reserved;		/* Unused */
372};
373
374#define TIPC_GENL_HDRLEN	NLMSG_ALIGN(sizeof(struct tipc_genlmsghdr))
375
376/*
377 * Configuration messages exchanged via TIPC sockets use the TIPC configuration
378 * message header, which is defined below.  This structure is analogous
379 * to the Netlink message header, but fields are stored in network byte order
380 * and no padding is permitted between the header and the message data
381 * that follows.
382 */
383
384struct tipc_cfg_msg_hdr {
385	__be32 tcm_len;		/* Message length (including header) */
386	__be16 tcm_type;	/* Command type */
387	__be16 tcm_flags;	/* Additional flags */
388	char  tcm_reserved[8];	/* Unused */
389};
390
391#define TCM_F_REQUEST	0x1	/* Flag: Request message */
392#define TCM_F_MORE	0x2	/* Flag: Message to be continued */
393
394#define TCM_ALIGN(datalen)  (((datalen)+3) & ~3)
395#define TCM_LENGTH(datalen) (sizeof(struct tipc_cfg_msg_hdr) + datalen)
396#define TCM_SPACE(datalen)  (TCM_ALIGN(TCM_LENGTH(datalen)))
397#define TCM_DATA(tcm_hdr)   ((void *)((char *)(tcm_hdr) + TCM_LENGTH(0)))
398
399static inline int TCM_SET(void *msg, __u16 cmd, __u16 flags,
400			  void *data, __u16 data_len)
401{
402	struct tipc_cfg_msg_hdr *tcm_hdr;
403	int msg_len;
404
405	msg_len = TCM_LENGTH(data_len);
406	tcm_hdr = (struct tipc_cfg_msg_hdr *)msg;
407	tcm_hdr->tcm_len   = htonl(msg_len);
408	tcm_hdr->tcm_type  = htons(cmd);
409	tcm_hdr->tcm_flags = htons(flags);
410	if (data_len && data) {
411		memcpy(TCM_DATA(msg), data, data_len);
412		memset((char *)TCM_DATA(msg) + data_len, 0, TCM_SPACE(data_len) - msg_len);
413	}
414	return TCM_SPACE(data_len);
415}
416
417#endif
418