5 #include <netlink/genl/genl.h>
6 #include <netlink/genl/family.h>
7 #include <netlink/genl/ctrl.h>
8 #include <netlink/msg.h>
9 #include <netlink/attr.h>
27 PLINK_ACTION_UNDEFINED
,
33 static int print_mpath_handler(struct nl_msg
*msg
, void *arg
)
35 struct nlattr
*tb
[NL80211_ATTR_MAX
+ 1];
36 struct genlmsghdr
*gnlh
= nlmsg_data(nlmsg_hdr(msg
));
37 struct nlattr
*pinfo
[NL80211_MPATH_INFO_MAX
+ 1];
38 char dst
[20], next_hop
[20], dev
[20];
39 static struct nla_policy mpath_policy
[NL80211_MPATH_INFO_MAX
+ 1] = {
40 [NL80211_MPATH_INFO_FRAME_QLEN
] = { .type
= NLA_U32
},
41 [NL80211_MPATH_INFO_SN
] = { .type
= NLA_U32
},
42 [NL80211_MPATH_INFO_METRIC
] = { .type
= NLA_U32
},
43 [NL80211_MPATH_INFO_EXPTIME
] = { .type
= NLA_U32
},
44 [NL80211_MPATH_INFO_DISCOVERY_TIMEOUT
] = { .type
= NLA_U32
},
45 [NL80211_MPATH_INFO_DISCOVERY_RETRIES
] = { .type
= NLA_U8
},
46 [NL80211_MPATH_INFO_FLAGS
] = { .type
= NLA_U8
},
49 nla_parse(tb
, NL80211_ATTR_MAX
, genlmsg_attrdata(gnlh
, 0),
50 genlmsg_attrlen(gnlh
, 0), NULL
);
53 * TODO: validate the interface and mac address!
54 * Otherwise, there's a race condition as soon as
55 * the kernel starts sending mpath notifications.
58 if (!tb
[NL80211_ATTR_MPATH_INFO
]) {
59 fprintf(stderr
, "mpath info missing!\n");
62 if (nla_parse_nested(pinfo
, NL80211_MPATH_INFO_MAX
,
63 tb
[NL80211_ATTR_MPATH_INFO
],
65 fprintf(stderr
, "failed to parse nested attributes!\n");
69 mac_addr_n2a(dst
, nla_data(tb
[NL80211_ATTR_MAC
]));
70 mac_addr_n2a(next_hop
, nla_data(tb
[NL80211_ATTR_MPATH_NEXT_HOP
]));
71 if_indextoname(nla_get_u32(tb
[NL80211_ATTR_IFINDEX
]), dev
);
72 printf("%s %s %s", dst
, next_hop
, dev
);
73 if (pinfo
[NL80211_MPATH_INFO_SN
])
75 nla_get_u32(pinfo
[NL80211_MPATH_INFO_SN
]));
76 if (pinfo
[NL80211_MPATH_INFO_METRIC
])
78 nla_get_u32(pinfo
[NL80211_MPATH_INFO_METRIC
]));
79 if (pinfo
[NL80211_MPATH_INFO_FRAME_QLEN
])
81 nla_get_u32(pinfo
[NL80211_MPATH_INFO_FRAME_QLEN
]));
82 if (pinfo
[NL80211_MPATH_INFO_EXPTIME
])
84 nla_get_u32(pinfo
[NL80211_MPATH_INFO_EXPTIME
]));
85 if (pinfo
[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT
])
87 nla_get_u32(pinfo
[NL80211_MPATH_INFO_DISCOVERY_TIMEOUT
]));
88 if (pinfo
[NL80211_MPATH_INFO_DISCOVERY_RETRIES
])
90 nla_get_u8(pinfo
[NL80211_MPATH_INFO_DISCOVERY_RETRIES
]));
91 if (pinfo
[NL80211_MPATH_INFO_FLAGS
])
93 nla_get_u8(pinfo
[NL80211_MPATH_INFO_FLAGS
]));
99 static int handle_mpath_get(struct nl80211_state
*state
,
102 int argc
, char **argv
,
105 unsigned char dst
[ETH_ALEN
];
110 if (mac_addr_a2n(dst
, argv
[0])) {
111 fprintf(stderr
, "invalid mac address\n");
120 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, dst
);
122 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_mpath_handler
, NULL
);
128 COMMAND(mpath
, get
, "<MAC address>",
129 NL80211_CMD_GET_MPATH
, 0, CIB_NETDEV
, handle_mpath_get
,
130 "Get information on mesh path to the given node.");
131 COMMAND(mpath
, del
, "<MAC address>",
132 NL80211_CMD_DEL_MPATH
, 0, CIB_NETDEV
, handle_mpath_get
,
133 "Remove the mesh path to the given node.");
135 static int handle_mpath_set(struct nl80211_state
*state
,
138 int argc
, char **argv
,
141 unsigned char dst
[ETH_ALEN
];
142 unsigned char next_hop
[ETH_ALEN
];
147 if (mac_addr_a2n(dst
, argv
[0])) {
148 fprintf(stderr
, "invalid destination mac address\n");
154 if (strcmp("next_hop", argv
[0]) != 0)
159 if (mac_addr_a2n(next_hop
, argv
[0])) {
160 fprintf(stderr
, "invalid next hop mac address\n");
169 NLA_PUT(msg
, NL80211_ATTR_MAC
, ETH_ALEN
, dst
);
170 NLA_PUT(msg
, NL80211_ATTR_MPATH_NEXT_HOP
, ETH_ALEN
, next_hop
);
172 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_mpath_handler
, NULL
);
177 COMMAND(mpath
, new, "<destination MAC address> next_hop <next hop MAC address>",
178 NL80211_CMD_NEW_MPATH
, 0, CIB_NETDEV
, handle_mpath_set
,
179 "Create a new mesh path (instead of relying on automatic discovery).");
180 COMMAND(mpath
, set
, "<destination MAC address> next_hop <next hop MAC address>",
181 NL80211_CMD_SET_MPATH
, 0, CIB_NETDEV
, handle_mpath_set
,
182 "Set an existing mesh path's next hop.");
184 static int handle_mpath_dump(struct nl80211_state
*state
,
187 int argc
, char **argv
,
190 printf("DEST ADDR NEXT HOP IFACE\tSN\tMETRIC\tQLEN\t"
191 "EXPTIME\t\tDTIM\tDRET\tFLAGS\n");
192 nl_cb_set(cb
, NL_CB_VALID
, NL_CB_CUSTOM
, print_mpath_handler
, NULL
);
195 COMMAND(mpath
, dump
, NULL
,
196 NL80211_CMD_GET_MPATH
, NLM_F_DUMP
, CIB_NETDEV
, handle_mpath_dump
,
197 "List known mesh paths.");