link_veth.c revision c595fda55bfed9753851de5675aa310f523564b3
12da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov/*
22da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov * link_veth.c	veth driver module
32da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov *
42da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov *		This program is free software; you can redistribute it and/or
52da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov *		modify it under the terms of the GNU General Public License
62da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov *		as published by the Free Software Foundation; either version
72da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov *		2 of the License, or (at your option) any later version.
82da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov *
92da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov * Authors:	Pavel Emelianov <xemul@openvz.org>
102da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov *
112da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov */
122da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
132da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov#include <string.h>
14c595fda55bfed9753851de5675aa310f523564b3Stephen Hemminger#include <net/if.h>
15c595fda55bfed9753851de5675aa310f523564b3Stephen Hemminger#include <net/veth.h>
162da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
172da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov#include "utils.h"
182da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov#include "ip_common.h"
192da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
202da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianovstatic void usage(void)
212da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov{
22f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	printf("Usage: ip link <options> type veth "
23c595fda55bfed9753851de5675aa310f523564b3Stephen Hemminger	       "[peer <options>]\nTo get <options> type "
24c595fda55bfed9753851de5675aa310f523564b3Stephen Hemminger	       "'ip link add help'\n");
252da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov}
262da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
272da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianovstatic int veth_parse_opt(struct link_util *lu, int argc, char **argv,
28c595fda55bfed9753851de5675aa310f523564b3Stephen Hemminger			  struct nlmsghdr *hdr)
292da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov{
30f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	char *name, *type, *link, *dev;
31f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	int err, len;
32f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	struct rtattr * data;
332da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
34f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	if (strcmp(argv[0], "peer") != 0) {
35f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev		usage();
36f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev		return -1;
37f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	}
382da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
39f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	data = NLMSG_TAIL(hdr);
40f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	addattr_l(hdr, 1024, VETH_INFO_PEER, NULL, 0);
412da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
42f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	hdr->nlmsg_len += sizeof(struct ifinfomsg);
432da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
44f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	err = iplink_parse(argc - 1, argv + 1, (struct iplink_req *)hdr,
45c595fda55bfed9753851de5675aa310f523564b3Stephen Hemminger			   &name, &type, &link, &dev);
46f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	if (err < 0)
47f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev		return err;
482da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
49f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	if (name) {
50f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev		len = strlen(name) + 1;
51f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev		if (len > IFNAMSIZ)
52f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev			invarg("\"name\" too long\n", *argv);
53f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev		addattr_l(hdr, 1024, IFLA_IFNAME, name, len);
542da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov	}
552da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
56f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	data->rta_len = (void *)NLMSG_TAIL(hdr) - (void *)data;
57f9329ccaf933a9dee5760de6979c7be6f17e253fVitaliy Gusev	return argc - 1 - err;
582da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov}
592da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov
602da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianovstruct link_util veth_link_util = {
612da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov	.id = "veth",
622da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov	.parse_opt = veth_parse_opt,
632da55b1bbed22e1058627f6b262158349b2b142ePavel Emelianov};
64