summaryrefslogtreecommitdiff
path: root/ip/iplink_hsr.c
blob: 7d9167d4e6a34f9064e5508890d31e7b36c79534 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * iplink_hsr.c	HSR device support
 *
 *		This program is free software; you can redistribute it and/or
 *		modify it under the terms of the GNU General Public License
 *		as published by the Free Software Foundation; either version
 *		2 of the License, or (at your option) any later version.
 *
 * Authors:	Arvid Brodin <arvid.brodin@alten.se>
 *
 *		Based on iplink_vlan.c by Patrick McHardy <kaber@trash.net>
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>  /* Needed by linux/if.h for some reason */
#include <linux/if.h>
#include <linux/if_arp.h>
#include "rt_names.h"
#include "utils.h"
#include "ip_common.h"

static void print_usage(FILE *f)
{
	fprintf(f,
		"Usage:\tip link add name NAME type hsr slave1 SLAVE1-IF slave2 SLAVE2-IF\n"
		"\t[ supervision ADDR-BYTE ] [version VERSION]\n"
		"\n"
		"NAME\n"
		"	name of new hsr device (e.g. hsr0)\n"
		"SLAVE1-IF, SLAVE2-IF\n"
		"	the two slave devices bound to the HSR device\n"
		"ADDR-BYTE\n"
		"	0-255; the last byte of the multicast address used for HSR supervision\n"
		"	frames (default = 0)\n"
		"VERSION\n"
		"	0,1; the protocol version to be used. (default = 0)\n");
}

static void usage(void)
{
	print_usage(stderr);
}

static int hsr_parse_opt(struct link_util *lu, int argc, char **argv,
			 struct nlmsghdr *n)
{
	int ifindex;
	unsigned char multicast_spec;
	unsigned char protocol_version;

	while (argc > 0) {
		if (matches(*argv, "supervision") == 0) {
			NEXT_ARG();
			if (get_u8(&multicast_spec, *argv, 0))
				invarg("ADDR-BYTE is invalid", *argv);
			addattr_l(n, 1024, IFLA_HSR_MULTICAST_SPEC,
				  &multicast_spec, 1);
		} else if (matches(*argv, "version") == 0) {
			NEXT_ARG();
			if (!(get_u8(&protocol_version, *argv, 0) == 0 ||
			      get_u8(&protocol_version, *argv, 0) == 1))
				invarg("version is invalid", *argv);
			addattr_l(n, 1024, IFLA_HSR_VERSION,
				  &protocol_version, 1);
		} else if (matches(*argv, "slave1") == 0) {
			NEXT_ARG();
			ifindex = ll_name_to_index(*argv);
			if (ifindex == 0)
				invarg("No such interface", *argv);
			addattr_l(n, 1024, IFLA_HSR_SLAVE1, &ifindex, 4);
		} else if (matches(*argv, "slave2") == 0) {
			NEXT_ARG();
			ifindex = ll_name_to_index(*argv);
			if (ifindex == 0)
				invarg("No such interface", *argv);
			addattr_l(n, 1024, IFLA_HSR_SLAVE2, &ifindex, 4);
		} else if (matches(*argv, "help") == 0) {
			usage();
			return -1;
		} else {
			fprintf(stderr, "hsr: what is \"%s\"?\n", *argv);
			usage();
			return -1;
		}
		argc--, argv++;
	}

	return 0;
}

static void hsr_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[])
{
	SPRINT_BUF(b1);

	if (!tb)
		return;

	if (tb[IFLA_HSR_SLAVE1] &&
	    RTA_PAYLOAD(tb[IFLA_HSR_SLAVE1]) < sizeof(__u32))
		return;
	if (tb[IFLA_HSR_SLAVE2] &&
	    RTA_PAYLOAD(tb[IFLA_HSR_SLAVE2]) < sizeof(__u32))
		return;
	if (tb[IFLA_HSR_SEQ_NR] &&
	    RTA_PAYLOAD(tb[IFLA_HSR_SEQ_NR]) < sizeof(__u16))
		return;
	if (tb[IFLA_HSR_SUPERVISION_ADDR] &&
	    RTA_PAYLOAD(tb[IFLA_HSR_SUPERVISION_ADDR]) < ETH_ALEN)
		return;

	if (tb[IFLA_HSR_SLAVE1])
		print_string(PRINT_ANY,
			     "slave1",
			     "slave1 %s ",
			     ll_index_to_name(rta_getattr_u32(tb[IFLA_HSR_SLAVE1])));
	else
		print_null(PRINT_ANY, "slave1", "slave1 %s ", "<none>");

	if (tb[IFLA_HSR_SLAVE2])
		print_string(PRINT_ANY,
			     "slave2",
			     "slave2 %s ",
			     ll_index_to_name(rta_getattr_u32(tb[IFLA_HSR_SLAVE2])));
	else
		print_null(PRINT_ANY, "slave2", "slave2 %s ", "<none>");

	if (tb[IFLA_HSR_SEQ_NR])
		print_int(PRINT_ANY,
			  "seq_nr",
			  "sequence %d ",
			  rta_getattr_u16(tb[IFLA_HSR_SEQ_NR]));

	if (tb[IFLA_HSR_SUPERVISION_ADDR])
		print_string(PRINT_ANY,
			     "supervision_addr",
			     "supervision %s ",
			     ll_addr_n2a(RTA_DATA(tb[IFLA_HSR_SUPERVISION_ADDR]),
					 RTA_PAYLOAD(tb[IFLA_HSR_SUPERVISION_ADDR]),
					 ARPHRD_VOID,
					 b1, sizeof(b1)));
}

static void hsr_print_help(struct link_util *lu, int argc, char **argv,
	FILE *f)
{
	print_usage(f);
}

struct link_util hsr_link_util = {
	.id		= "hsr",
	.maxattr	= IFLA_HSR_MAX,
	.parse_opt	= hsr_parse_opt,
	.print_opt	= hsr_print_opt,
	.print_help	= hsr_print_help,
};