]> git.ipfire.org Git - people/ms/rstp.git/blame - brstate.c
reindent source
[people/ms/rstp.git] / brstate.c
CommitLineData
ad02a0eb
SH
1/*
2 * brstate.c RTnetlink port state change
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
8 *
9 * Authors: Stephen Hemminger <shemminger@osdl.org>
10 *
11 * Modified by Srinivas Aji <Aji_Srinivas@emc.com> for use
12 * in RSTP daemon. - 2006-09-01
13 */
14
15#include <stdio.h>
16#include <stdlib.h>
17#include <unistd.h>
18#include <syslog.h>
19#include <fcntl.h>
20#include <sys/socket.h>
21#include <sys/time.h>
22#include <net/if.h>
23#include <netinet/in.h>
24#include <linux/if_bridge.h>
25#include <string.h>
26
27#include "libnetlink.h"
28
29#if 0
30static const char *port_states[] = {
31 [BR_STATE_DISABLED] = "disabled",
32 [BR_STATE_LISTENING] = "listening",
33 [BR_STATE_LEARNING] = "learning",
34 [BR_STATE_FORWARDING] = "forwarding",
35 [BR_STATE_BLOCKING] = "blocking",
36};
37
38static int portstate(const char *name)
39{
40 int i;
41
11904a35
SH
42 for (i = 0; i < sizeof(port_states) / sizeof(port_states[0]); i++) {
43 if (strcasecmp(name, port_states[i]) == 0)
ad02a0eb
SH
44 return i;
45 }
46 return -1;
47}
48#endif
49
50static int br_set_state(struct rtnl_handle *rth, unsigned ifindex, __u8 state)
51{
52 struct {
11904a35
SH
53 struct nlmsghdr n;
54 struct ifinfomsg ifi;
55 char buf[256];
ad02a0eb
SH
56 } req;
57
58 memset(&req, 0, sizeof(req));
59
60 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
11904a35 61 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE;
ad02a0eb
SH
62 req.n.nlmsg_type = RTM_SETLINK;
63 req.ifi.ifi_family = AF_BRIDGE;
64 req.ifi.ifi_index = ifindex;
65
66 addattr32(&req.n, sizeof(req.buf), IFLA_PROTINFO, state);
11904a35 67
ad02a0eb
SH
68 return rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL);
69}
70
71static int br_send_bpdu(struct rtnl_handle *rth, unsigned ifindex,
11904a35 72 const unsigned char *data, int len)
ad02a0eb
SH
73{
74 struct {
11904a35
SH
75 struct nlmsghdr n;
76 struct ifinfomsg ifi;
77 char buf[256];
ad02a0eb
SH
78 } req;
79
80 memset(&req, 0, sizeof(req));
81
82 req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifinfomsg));
11904a35 83 req.n.nlmsg_flags = NLM_F_REQUEST | NLM_F_REPLACE;
ad02a0eb
SH
84 req.n.nlmsg_type = RTM_SETLINK;
85 req.ifi.ifi_family = AF_BRIDGE;
86 req.ifi.ifi_index = ifindex;
87
88 addattr_l(&req.n, sizeof(req.buf), IFLA_PRIORITY, data, len);
11904a35 89
ad02a0eb
SH
90 return rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL);
91}
92
93#if 0
94int main(int argc, char **argv)
95{
96 unsigned int ifindex;
97 int err, brstate;
98 struct rtnl_handle rth;
99
ad02a0eb 100 if (argc != 3) {
11904a35 101 fprintf(stderr, "Usage: brstate ifname state\n");
ad02a0eb
SH
102 exit(-1);
103 }
104
105 if (rtnl_open(&rth, 0) < 0) {
106 fprintf(stderr, "brstate: can't setup netlink\n");
107 exit(1);
108 }
109
110 ifindex = if_nametoindex(argv[1]);
111 if (ifindex == 0) {
112 fprintf(stderr, "brstate: unknown interface '%s'\n", argv[1]);
113 exit(1);
114 }
11904a35 115
ad02a0eb
SH
116 brstate = portstate(argv[2]);
117 if (brstate < 0) {
11904a35 118 fprintf(stderr, "brstate: unknown port state '%s'\n", argv[2]);
ad02a0eb
SH
119 exit(1);
120 }
121
122 err = br_set_state(&rth, ifindex, brstate);
123 if (err) {
124 fprintf(stderr, "brstate: set %d, %d failed %d\n",
11904a35 125 ifindex, brstate, err);
ad02a0eb
SH
126 exit(1);
127 }
128
129 rtnl_close(&rth);
130 return 0;
131}
132#endif
133
134#include "bridge_ctl.h"
135
136extern struct rtnl_handle rth_state;
137
138int bridge_set_state(int ifindex, int brstate)
139{
11904a35
SH
140 int err = br_set_state(&rth_state, ifindex, brstate);
141 if (err < 0) {
142 fprintf(stderr,
143 "Couldn't set bridge state, ifindex %d, state %d\n",
144 ifindex, brstate);
145 return -1;
146 }
147 return 0;
ad02a0eb
SH
148}
149
150int bridge_send_bpdu(int ifindex, const unsigned char *data, int len)
151{
11904a35
SH
152 int err = br_send_bpdu(&rth_state, ifindex, data, len);
153 if (err < 0) {
154 fprintf(stderr, "Couldn't send bpdu, ifindex %d\n", ifindex);
155 return -1;
156 }
157 return 0;
ad02a0eb 158}