Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * link_vti.c VTI 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: Herbert Xu <herbert@gondor.apana.org.au> |
| 10 | * Saurabh Mohan <saurabh.mohan@vyatta.com> Modified link_gre.c for VTI |
| 11 | */ |
| 12 | |
| 13 | #include <string.h> |
| 14 | #include <net/if.h> |
| 15 | #include <sys/types.h> |
| 16 | #include <sys/socket.h> |
| 17 | #include <arpa/inet.h> |
| 18 | |
| 19 | #include <linux/ip.h> |
| 20 | #include <linux/if_tunnel.h> |
| 21 | #include "rt_names.h" |
| 22 | #include "utils.h" |
| 23 | #include "ip_common.h" |
| 24 | #include "tunnel.h" |
| 25 | |
| 26 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 27 | static void print_usage(FILE *f) |
| 28 | { |
| 29 | fprintf(f, "Usage: ip link { add | set | change | replace | del } NAME\n"); |
| 30 | fprintf(f, " type { vti } [ remote ADDR ] [ local ADDR ]\n"); |
| 31 | fprintf(f, " [ [i|o]key KEY ]\n"); |
| 32 | fprintf(f, " [ dev PHYS_DEV ]\n"); |
| 33 | fprintf(f, "\n"); |
| 34 | fprintf(f, "Where: NAME := STRING\n"); |
| 35 | fprintf(f, " ADDR := { IP_ADDRESS }\n"); |
| 36 | fprintf(f, " KEY := { DOTTED_QUAD | NUMBER }\n"); |
| 37 | } |
| 38 | |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 39 | static void usage(void) __attribute__((noreturn)); |
| 40 | static void usage(void) |
| 41 | { |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 42 | print_usage(stderr); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 43 | exit(-1); |
| 44 | } |
| 45 | |
| 46 | static int vti_parse_opt(struct link_util *lu, int argc, char **argv, |
| 47 | struct nlmsghdr *n) |
| 48 | { |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 49 | struct ifinfomsg *ifi = (struct ifinfomsg *)(n + 1); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 50 | struct { |
| 51 | struct nlmsghdr n; |
| 52 | struct ifinfomsg i; |
| 53 | char buf[1024]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 54 | } req = { |
| 55 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(*ifi)), |
| 56 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 57 | .n.nlmsg_type = RTM_GETLINK, |
| 58 | .i.ifi_family = preferred_family, |
| 59 | .i.ifi_index = ifi->ifi_index, |
| 60 | }; |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 61 | struct rtattr *tb[IFLA_MAX + 1]; |
| 62 | struct rtattr *linkinfo[IFLA_INFO_MAX+1]; |
| 63 | struct rtattr *vtiinfo[IFLA_VTI_MAX + 1]; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 64 | unsigned int ikey = 0; |
| 65 | unsigned int okey = 0; |
| 66 | unsigned int saddr = 0; |
| 67 | unsigned int daddr = 0; |
| 68 | unsigned int link = 0; |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 69 | int len; |
| 70 | |
| 71 | if (!(n->nlmsg_flags & NLM_F_CREATE)) { |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 72 | if (rtnl_talk(&rth, &req.n, &req.n, sizeof(req)) < 0) { |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 73 | get_failed: |
| 74 | fprintf(stderr, |
| 75 | "Failed to get existing tunnel info.\n"); |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | len = req.n.nlmsg_len; |
| 80 | len -= NLMSG_LENGTH(sizeof(*ifi)); |
| 81 | if (len < 0) |
| 82 | goto get_failed; |
| 83 | |
| 84 | parse_rtattr(tb, IFLA_MAX, IFLA_RTA(&req.i), len); |
| 85 | |
| 86 | if (!tb[IFLA_LINKINFO]) |
| 87 | goto get_failed; |
| 88 | |
| 89 | parse_rtattr_nested(linkinfo, IFLA_INFO_MAX, tb[IFLA_LINKINFO]); |
| 90 | |
| 91 | if (!linkinfo[IFLA_INFO_DATA]) |
| 92 | goto get_failed; |
| 93 | |
| 94 | parse_rtattr_nested(vtiinfo, IFLA_VTI_MAX, |
| 95 | linkinfo[IFLA_INFO_DATA]); |
| 96 | |
| 97 | if (vtiinfo[IFLA_VTI_IKEY]) |
| 98 | ikey = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_IKEY]); |
| 99 | |
| 100 | if (vtiinfo[IFLA_VTI_OKEY]) |
| 101 | okey = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_OKEY]); |
| 102 | |
| 103 | if (vtiinfo[IFLA_VTI_LOCAL]) |
| 104 | saddr = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_LOCAL]); |
| 105 | |
| 106 | if (vtiinfo[IFLA_VTI_REMOTE]) |
| 107 | daddr = *(__u32 *)RTA_DATA(vtiinfo[IFLA_VTI_REMOTE]); |
| 108 | |
| 109 | if (vtiinfo[IFLA_VTI_LINK]) |
| 110 | link = *(__u8 *)RTA_DATA(vtiinfo[IFLA_VTI_LINK]); |
| 111 | } |
| 112 | |
| 113 | while (argc > 0) { |
| 114 | if (!matches(*argv, "key")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 115 | unsigned int uval; |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 116 | |
| 117 | NEXT_ARG(); |
| 118 | if (strchr(*argv, '.')) |
| 119 | uval = get_addr32(*argv); |
| 120 | else { |
| 121 | if (get_unsigned(&uval, *argv, 0) < 0) { |
| 122 | fprintf(stderr, |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 123 | "Invalid value for \"key\": \"%s\"; it should be an unsigned integer\n", *argv); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 124 | exit(-1); |
| 125 | } |
| 126 | uval = htonl(uval); |
| 127 | } |
| 128 | |
| 129 | ikey = okey = uval; |
| 130 | } else if (!matches(*argv, "ikey")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 131 | unsigned int uval; |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 132 | |
| 133 | NEXT_ARG(); |
| 134 | if (strchr(*argv, '.')) |
| 135 | uval = get_addr32(*argv); |
| 136 | else { |
| 137 | if (get_unsigned(&uval, *argv, 0) < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 138 | fprintf(stderr, "invalid value for \"ikey\": \"%s\"; it should be an unsigned integer\n", *argv); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 139 | exit(-1); |
| 140 | } |
| 141 | uval = htonl(uval); |
| 142 | } |
| 143 | ikey = uval; |
| 144 | } else if (!matches(*argv, "okey")) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 145 | unsigned int uval; |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 146 | |
| 147 | NEXT_ARG(); |
| 148 | if (strchr(*argv, '.')) |
| 149 | uval = get_addr32(*argv); |
| 150 | else { |
| 151 | if (get_unsigned(&uval, *argv, 0) < 0) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 152 | fprintf(stderr, "invalid value for \"okey\": \"%s\"; it should be an unsigned integer\n", *argv); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 153 | exit(-1); |
| 154 | } |
| 155 | uval = htonl(uval); |
| 156 | } |
| 157 | okey = uval; |
| 158 | } else if (!matches(*argv, "remote")) { |
| 159 | NEXT_ARG(); |
| 160 | if (!strcmp(*argv, "any")) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 161 | fprintf(stderr, "invalid value for \"remote\": \"%s\"\n", *argv); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 162 | exit(-1); |
| 163 | } else { |
| 164 | daddr = get_addr32(*argv); |
| 165 | } |
| 166 | } else if (!matches(*argv, "local")) { |
| 167 | NEXT_ARG(); |
| 168 | if (!strcmp(*argv, "any")) { |
Kees van Reeuwijk | 14645ec | 2013-02-08 03:32:36 +0000 | [diff] [blame] | 169 | fprintf(stderr, "invalid value for \"local\": \"%s\"\n", *argv); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 170 | exit(-1); |
| 171 | } else { |
| 172 | saddr = get_addr32(*argv); |
| 173 | } |
| 174 | } else if (!matches(*argv, "dev")) { |
| 175 | NEXT_ARG(); |
| 176 | link = if_nametoindex(*argv); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 177 | if (link == 0) { |
| 178 | fprintf(stderr, "Cannot find device \"%s\"\n", |
| 179 | *argv); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 180 | exit(-1); |
Cong Wang | 0cb6bb5 | 2014-06-03 16:06:45 -0700 | [diff] [blame] | 181 | } |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 182 | } else |
| 183 | usage(); |
| 184 | argc--; argv++; |
| 185 | } |
| 186 | |
| 187 | addattr32(n, 1024, IFLA_VTI_IKEY, ikey); |
| 188 | addattr32(n, 1024, IFLA_VTI_OKEY, okey); |
| 189 | addattr_l(n, 1024, IFLA_VTI_LOCAL, &saddr, 4); |
| 190 | addattr_l(n, 1024, IFLA_VTI_REMOTE, &daddr, 4); |
| 191 | if (link) |
| 192 | addattr32(n, 1024, IFLA_VTI_LINK, link); |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | static void vti_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) |
| 198 | { |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 199 | char s2[64]; |
| 200 | const char *local = "any"; |
| 201 | const char *remote = "any"; |
| 202 | |
| 203 | if (!tb) |
| 204 | return; |
| 205 | |
| 206 | if (tb[IFLA_VTI_REMOTE]) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 207 | unsigned int addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_REMOTE]); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 208 | |
| 209 | if (addr) |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 210 | remote = format_host(AF_INET, 4, &addr); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | fprintf(f, "remote %s ", remote); |
| 214 | |
| 215 | if (tb[IFLA_VTI_LOCAL]) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 216 | unsigned int addr = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LOCAL]); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 217 | |
| 218 | if (addr) |
Phil Sutter | a418e45 | 2016-03-22 19:35:15 +0100 | [diff] [blame] | 219 | local = format_host(AF_INET, 4, &addr); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | fprintf(f, "local %s ", local); |
| 223 | |
| 224 | if (tb[IFLA_VTI_LINK] && *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK])) { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 225 | unsigned int link = *(__u32 *)RTA_DATA(tb[IFLA_VTI_LINK]); |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 226 | const char *n = if_indextoname(link, s2); |
| 227 | |
| 228 | if (n) |
| 229 | fprintf(f, "dev %s ", n); |
| 230 | else |
| 231 | fprintf(f, "dev %u ", link); |
| 232 | } |
| 233 | |
| 234 | if (tb[IFLA_VTI_IKEY]) { |
| 235 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_IKEY]), s2, sizeof(s2)); |
| 236 | fprintf(f, "ikey %s ", s2); |
| 237 | } |
| 238 | |
| 239 | if (tb[IFLA_VTI_OKEY]) { |
| 240 | inet_ntop(AF_INET, RTA_DATA(tb[IFLA_VTI_OKEY]), s2, sizeof(s2)); |
| 241 | fprintf(f, "okey %s ", s2); |
| 242 | } |
| 243 | } |
| 244 | |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 245 | static void vti_print_help(struct link_util *lu, int argc, char **argv, |
| 246 | FILE *f) |
| 247 | { |
| 248 | print_usage(f); |
| 249 | } |
| 250 | |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 251 | struct link_util vti_link_util = { |
| 252 | .id = "vti", |
| 253 | .maxattr = IFLA_VTI_MAX, |
| 254 | .parse_opt = vti_parse_opt, |
| 255 | .print_opt = vti_print_opt, |
vadimk | 561e650 | 2014-09-30 08:17:31 +0300 | [diff] [blame] | 256 | .print_help = vti_print_help, |
Saurabh | 7357933 | 2012-06-27 15:01:57 +0000 | [diff] [blame] | 257 | }; |