blob: a36882744ec7ca5966dd553548e54422ba63a0cd [file] [log] [blame]
Pavel Emelianov2da55b12007-07-11 13:33:55 +04001/*
2 * link_veth.c veth driver module
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: Pavel Emelianov <xemul@openvz.org>
10 *
11 */
12
Pavel Emelianov2da55b12007-07-11 13:33:55 +040013#include <string.h>
Stephen Hemmingerc595fda2007-12-21 09:37:30 -080014#include <net/if.h>
Stephen Hemminger118c9232007-12-25 12:38:08 -080015#include <linux/veth.h>
Pavel Emelianov2da55b12007-07-11 13:33:55 +040016
17#include "utils.h"
18#include "ip_common.h"
Pavel Emelianov2da55b12007-07-11 13:33:55 +040019
vadimk561e6502014-09-30 08:17:31 +030020static void print_usage(FILE *f)
Pavel Emelianov2da55b12007-07-11 13:33:55 +040021{
Stephen Hemmingerac3ff722013-08-04 15:00:42 -070022 printf("Usage: ip link <options> type veth [peer <options>]\n"
23 "To get <options> type 'ip link add help'\n");
Pavel Emelianov2da55b12007-07-11 13:33:55 +040024}
25
vadimk561e6502014-09-30 08:17:31 +030026static void usage(void)
27{
28 print_usage(stderr);
29}
30
Pavel Emelianov2da55b12007-07-11 13:33:55 +040031static int veth_parse_opt(struct link_util *lu, int argc, char **argv,
Stephen Hemmingerc595fda2007-12-21 09:37:30 -080032 struct nlmsghdr *hdr)
Pavel Emelianov2da55b12007-07-11 13:33:55 +040033{
Stephen Hemmingerbe2c3142013-12-20 08:25:13 -080034 char *dev = NULL;
35 char *name = NULL;
36 char *link = NULL;
37 char *type = NULL;
Pavel Emelyanov5e25cf72013-12-26 23:15:20 +040038 int index = 0;
Vitaliy Gusevf9329cc2007-12-18 15:15:38 +030039 int err, len;
Stephen Hemminger56f5daa2016-03-21 11:52:19 -070040 struct rtattr *data;
Vlad Dogarudb026082011-02-02 20:23:41 +020041 int group;
Kusanagi Kouichi18917542014-04-02 01:09:18 +090042 struct ifinfomsg *ifm, *peer_ifm;
43 unsigned int ifi_flags, ifi_change;
Pavel Emelianov2da55b12007-07-11 13:33:55 +040044
Vitaliy Gusevf9329cc2007-12-18 15:15:38 +030045 if (strcmp(argv[0], "peer") != 0) {
Pavel Emelianov2da55b12007-07-11 13:33:55 +040046 usage();
47 return -1;
48 }
49
Kusanagi Kouichi18917542014-04-02 01:09:18 +090050 ifm = NLMSG_DATA(hdr);
51 ifi_flags = ifm->ifi_flags;
52 ifi_change = ifm->ifi_change;
53 ifm->ifi_flags = 0;
54 ifm->ifi_change = 0;
55
Vitaliy Gusevf9329cc2007-12-18 15:15:38 +030056 data = NLMSG_TAIL(hdr);
57 addattr_l(hdr, 1024, VETH_INFO_PEER, NULL, 0);
58
59 hdr->nlmsg_len += sizeof(struct ifinfomsg);
60
61 err = iplink_parse(argc - 1, argv + 1, (struct iplink_req *)hdr,
Pavel Emelyanov5e25cf72013-12-26 23:15:20 +040062 &name, &type, &link, &dev, &group, &index);
Vitaliy Gusevf9329cc2007-12-18 15:15:38 +030063 if (err < 0)
64 return err;
65
66 if (name) {
67 len = strlen(name) + 1;
68 if (len > IFNAMSIZ)
69 invarg("\"name\" too long\n", *argv);
70 addattr_l(hdr, 1024, IFLA_IFNAME, name, len);
71 }
72
Kusanagi Kouichi18917542014-04-02 01:09:18 +090073 peer_ifm = RTA_DATA(data);
74 peer_ifm->ifi_index = index;
75 peer_ifm->ifi_flags = ifm->ifi_flags;
76 peer_ifm->ifi_change = ifm->ifi_change;
77 ifm->ifi_flags = ifi_flags;
78 ifm->ifi_change = ifi_change;
Pavel Emelyanov5e25cf72013-12-26 23:15:20 +040079
Sergey Popoviche0d47aa2013-12-11 14:25:07 +020080 if (group != -1)
81 addattr32(hdr, 1024, IFLA_GROUP, group);
82
Vitaliy Gusevf9329cc2007-12-18 15:15:38 +030083 data->rta_len = (void *)NLMSG_TAIL(hdr) - (void *)data;
84 return argc - 1 - err;
Pavel Emelianov2da55b12007-07-11 13:33:55 +040085}
86
vadimk561e6502014-09-30 08:17:31 +030087static void veth_print_help(struct link_util *lu, int argc, char **argv,
88 FILE *f)
89{
90 print_usage(f);
91}
92
Pavel Emelianov2da55b12007-07-11 13:33:55 +040093struct link_util veth_link_util = {
94 .id = "veth",
95 .parse_opt = veth_parse_opt,
vadimk561e6502014-09-30 08:17:31 +030096 .print_help = veth_print_help,
Pavel Emelianov2da55b12007-07-11 13:33:55 +040097};