Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 1 | /* |
| 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 Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 13 | #include <string.h> |
Stephen Hemminger | c595fda | 2007-12-21 09:37:30 -0800 | [diff] [blame] | 14 | #include <net/if.h> |
Stephen Hemminger | 118c923 | 2007-12-25 12:38:08 -0800 | [diff] [blame] | 15 | #include <linux/veth.h> |
Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 16 | |
| 17 | #include "utils.h" |
| 18 | #include "ip_common.h" |
Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 19 | |
| 20 | static void usage(void) |
| 21 | { |
Stephen Hemminger | ac3ff72 | 2013-08-04 15:00:42 -0700 | [diff] [blame] | 22 | printf("Usage: ip link <options> type veth [peer <options>]\n" |
| 23 | "To get <options> type 'ip link add help'\n"); |
Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | static int veth_parse_opt(struct link_util *lu, int argc, char **argv, |
Stephen Hemminger | c595fda | 2007-12-21 09:37:30 -0800 | [diff] [blame] | 27 | struct nlmsghdr *hdr) |
Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 28 | { |
Stephen Hemminger | be2c314 | 2013-12-20 08:25:13 -0800 | [diff] [blame] | 29 | char *dev = NULL; |
| 30 | char *name = NULL; |
| 31 | char *link = NULL; |
| 32 | char *type = NULL; |
Pavel Emelyanov | 5e25cf7 | 2013-12-26 23:15:20 +0400 | [diff] [blame^] | 33 | int index = 0; |
Vitaliy Gusev | f9329cc | 2007-12-18 15:15:38 +0300 | [diff] [blame] | 34 | int err, len; |
| 35 | struct rtattr * data; |
Vlad Dogaru | db02608 | 2011-02-02 20:23:41 +0200 | [diff] [blame] | 36 | int group; |
Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 37 | |
Vitaliy Gusev | f9329cc | 2007-12-18 15:15:38 +0300 | [diff] [blame] | 38 | if (strcmp(argv[0], "peer") != 0) { |
Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 39 | usage(); |
| 40 | return -1; |
| 41 | } |
| 42 | |
Vitaliy Gusev | f9329cc | 2007-12-18 15:15:38 +0300 | [diff] [blame] | 43 | data = NLMSG_TAIL(hdr); |
| 44 | addattr_l(hdr, 1024, VETH_INFO_PEER, NULL, 0); |
| 45 | |
| 46 | hdr->nlmsg_len += sizeof(struct ifinfomsg); |
| 47 | |
| 48 | err = iplink_parse(argc - 1, argv + 1, (struct iplink_req *)hdr, |
Pavel Emelyanov | 5e25cf7 | 2013-12-26 23:15:20 +0400 | [diff] [blame^] | 49 | &name, &type, &link, &dev, &group, &index); |
Vitaliy Gusev | f9329cc | 2007-12-18 15:15:38 +0300 | [diff] [blame] | 50 | if (err < 0) |
| 51 | return err; |
| 52 | |
| 53 | if (name) { |
| 54 | len = strlen(name) + 1; |
| 55 | if (len > IFNAMSIZ) |
| 56 | invarg("\"name\" too long\n", *argv); |
| 57 | addattr_l(hdr, 1024, IFLA_IFNAME, name, len); |
| 58 | } |
| 59 | |
Pavel Emelyanov | 5e25cf7 | 2013-12-26 23:15:20 +0400 | [diff] [blame^] | 60 | if (index) { |
| 61 | struct ifinfomsg *ifi = (struct ifinfomsg *)(data + 1); |
| 62 | ifi->ifi_index = index; |
| 63 | } |
| 64 | |
Sergey Popovich | e0d47aa | 2013-12-11 14:25:07 +0200 | [diff] [blame] | 65 | if (group != -1) |
| 66 | addattr32(hdr, 1024, IFLA_GROUP, group); |
| 67 | |
Vitaliy Gusev | f9329cc | 2007-12-18 15:15:38 +0300 | [diff] [blame] | 68 | data->rta_len = (void *)NLMSG_TAIL(hdr) - (void *)data; |
| 69 | return argc - 1 - err; |
Pavel Emelianov | 2da55b1 | 2007-07-11 13:33:55 +0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | struct link_util veth_link_util = { |
| 73 | .id = "veth", |
| 74 | .parse_opt = veth_parse_opt, |
| 75 | }; |