]>
Commit | Line | Data |
---|---|---|
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 | |
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", | |
36 | }; | |
37 | ||
38 | static int portstate(const char *name) | |
39 | { | |
40 | int i; | |
41 | ||
42 | for (i = 0; i < sizeof(port_states)/sizeof(port_states[0]); i++) { | |
43 | if (strcasecmp(name, port_states[i]) == 0) | |
44 | return i; | |
45 | } | |
46 | return -1; | |
47 | } | |
48 | #endif | |
49 | ||
50 | static int br_set_state(struct rtnl_handle *rth, unsigned ifindex, __u8 state) | |
51 | { | |
52 | struct { | |
53 | struct nlmsghdr n; | |
54 | struct ifinfomsg ifi; | |
55 | char buf[256]; | |
56 | } req; | |
57 | ||
58 | memset(&req, 0, sizeof(req)); | |
59 | ||
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; | |
65 | ||
66 | addattr32(&req.n, sizeof(req.buf), IFLA_PROTINFO, state); | |
67 | ||
68 | return rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL); | |
69 | } | |
70 | ||
71 | static int br_send_bpdu(struct rtnl_handle *rth, unsigned ifindex, | |
72 | const unsigned char *data, int len) | |
73 | { | |
74 | struct { | |
75 | struct nlmsghdr n; | |
76 | struct ifinfomsg ifi; | |
77 | char buf[256]; | |
78 | } req; | |
79 | ||
80 | memset(&req, 0, sizeof(req)); | |
81 | ||
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; | |
87 | ||
88 | addattr_l(&req.n, sizeof(req.buf), IFLA_PRIORITY, data, len); | |
89 | ||
90 | return rtnl_talk(rth, &req.n, 0, 0, NULL, NULL, NULL); | |
91 | } | |
92 | ||
93 | #if 0 | |
94 | int main(int argc, char **argv) | |
95 | { | |
96 | unsigned int ifindex; | |
97 | int err, brstate; | |
98 | struct rtnl_handle rth; | |
99 | ||
100 | ||
101 | if (argc != 3) { | |
102 | fprintf(stderr, | |
103 | "Usage: brstate ifname state\n"); | |
104 | exit(-1); | |
105 | } | |
106 | ||
107 | if (rtnl_open(&rth, 0) < 0) { | |
108 | fprintf(stderr, "brstate: can't setup netlink\n"); | |
109 | exit(1); | |
110 | } | |
111 | ||
112 | ifindex = if_nametoindex(argv[1]); | |
113 | if (ifindex == 0) { | |
114 | fprintf(stderr, "brstate: unknown interface '%s'\n", argv[1]); | |
115 | exit(1); | |
116 | } | |
117 | ||
118 | brstate = portstate(argv[2]); | |
119 | if (brstate < 0) { | |
120 | fprintf(stderr, "brstate: unknown port state '%s'\n", | |
121 | argv[2]); | |
122 | exit(1); | |
123 | } | |
124 | ||
125 | err = br_set_state(&rth, ifindex, brstate); | |
126 | if (err) { | |
127 | fprintf(stderr, "brstate: set %d, %d failed %d\n", | |
128 | ifindex, brstate, err); | |
129 | exit(1); | |
130 | } | |
131 | ||
132 | rtnl_close(&rth); | |
133 | return 0; | |
134 | } | |
135 | #endif | |
136 | ||
137 | #include "bridge_ctl.h" | |
138 | ||
139 | extern struct rtnl_handle rth_state; | |
140 | ||
141 | int bridge_set_state(int ifindex, int brstate) | |
142 | { | |
143 | int err = br_set_state(&rth_state, ifindex, brstate); | |
144 | if (err < 0) { | |
145 | fprintf(stderr, "Couldn't set bridge state, ifindex %d, state %d\n", | |
146 | ifindex, brstate); | |
147 | return -1; | |
148 | } | |
149 | return 0; | |
150 | } | |
151 | ||
152 | int bridge_send_bpdu(int ifindex, const unsigned char *data, int len) | |
153 | { | |
154 | int err = br_send_bpdu(&rth_state, ifindex, data, len); | |
155 | if (err < 0) { | |
156 | fprintf(stderr, "Couldn't send bpdu, ifindex %d\n", ifindex); | |
157 | return -1; | |
158 | } | |
159 | return 0; | |
160 | } |