]>
git.ipfire.org Git - people/ms/rstp.git/blob - brstate.c
f0f9d36eb6018863dc98b340c8158d7902442227
2 * brstate.c RTnetlink port state change
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.
9 * Authors: Stephen Hemminger <shemminger@osdl.org>
11 * Modified by Srinivas Aji <Aji_Srinivas@emc.com> for use
12 * in RSTP daemon. - 2006-09-01
20 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <linux/if_bridge.h>
27 #include "libnetlink.h"
30 static 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",
38 static int portstate(const char *name
)
42 for (i
= 0; i
< sizeof(port_states
)/sizeof(port_states
[0]); i
++) {
43 if (strcasecmp(name
, port_states
[i
]) == 0)
50 static int br_set_state(struct rtnl_handle
*rth
, unsigned ifindex
, __u8 state
)
58 memset(&req
, 0, sizeof(req
));
60 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
61 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_REPLACE
;
62 req
.n
.nlmsg_type
= RTM_SETLINK
;
63 req
.ifi
.ifi_family
= AF_BRIDGE
;
64 req
.ifi
.ifi_index
= ifindex
;
66 addattr32(&req
.n
, sizeof(req
.buf
), IFLA_PROTINFO
, state
);
68 return rtnl_talk(rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
);
71 static int br_send_bpdu(struct rtnl_handle
*rth
, unsigned ifindex
,
72 const unsigned char *data
, int len
)
80 memset(&req
, 0, sizeof(req
));
82 req
.n
.nlmsg_len
= NLMSG_LENGTH(sizeof(struct ifinfomsg
));
83 req
.n
.nlmsg_flags
= NLM_F_REQUEST
|NLM_F_REPLACE
;
84 req
.n
.nlmsg_type
= RTM_SETLINK
;
85 req
.ifi
.ifi_family
= AF_BRIDGE
;
86 req
.ifi
.ifi_index
= ifindex
;
88 addattr_l(&req
.n
, sizeof(req
.buf
), IFLA_PRIORITY
, data
, len
);
90 return rtnl_talk(rth
, &req
.n
, 0, 0, NULL
, NULL
, NULL
);
94 int main(int argc
, char **argv
)
98 struct rtnl_handle rth
;
103 "Usage: brstate ifname state\n");
107 if (rtnl_open(&rth
, 0) < 0) {
108 fprintf(stderr
, "brstate: can't setup netlink\n");
112 ifindex
= if_nametoindex(argv
[1]);
114 fprintf(stderr
, "brstate: unknown interface '%s'\n", argv
[1]);
118 brstate
= portstate(argv
[2]);
120 fprintf(stderr
, "brstate: unknown port state '%s'\n",
125 err
= br_set_state(&rth
, ifindex
, brstate
);
127 fprintf(stderr
, "brstate: set %d, %d failed %d\n",
128 ifindex
, brstate
, err
);
137 #include "bridge_ctl.h"
139 extern struct rtnl_handle rth_state
;
141 int bridge_set_state(int ifindex
, int brstate
)
143 int err
= br_set_state(&rth_state
, ifindex
, brstate
);
145 fprintf(stderr
, "Couldn't set bridge state, ifindex %d, state %d\n",
152 int bridge_send_bpdu(int ifindex
, const unsigned char *data
, int len
)
154 int err
= br_send_bpdu(&rth_state
, ifindex
, data
, len
);
156 fprintf(stderr
, "Couldn't send bpdu, ifindex %d\n", ifindex
);