YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 1 | /* |
| 2 | * ipaddrlabel.c "ip addrlabel" |
| 3 | * |
| 4 | * Copyright (C)2007 USAGI/WIDE Project |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 2 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
Stephen Hemminger | 4d98ab0 | 2013-12-06 15:05:07 -0800 | [diff] [blame] | 17 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 18 | * |
| 19 | * |
| 20 | * Based on iprule.c. |
| 21 | * |
| 22 | * Authors: YOSHIFUJI Hideaki <yoshfuji@linux-ipv6.org> |
| 23 | * |
| 24 | */ |
| 25 | |
| 26 | #include <stdio.h> |
| 27 | #include <stdlib.h> |
| 28 | #include <unistd.h> |
| 29 | #include <syslog.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <sys/socket.h> |
| 32 | #include <netinet/in.h> |
| 33 | #include <netinet/ip.h> |
| 34 | #include <arpa/inet.h> |
| 35 | #include <string.h> |
| 36 | #include <linux/types.h> |
| 37 | #include <linux/if_addrlabel.h> |
| 38 | |
| 39 | #include "rt_names.h" |
| 40 | #include "utils.h" |
| 41 | #include "ip_common.h" |
| 42 | |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 43 | #define IFAL_RTA(r) ((struct rtattr *)(((char *)(r)) + NLMSG_ALIGN(sizeof(struct ifaddrlblmsg)))) |
| 44 | #define IFAL_PAYLOAD(n) NLMSG_PAYLOAD(n, sizeof(struct ifaddrlblmsg)) |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 45 | |
| 46 | extern struct rtnl_handle rth; |
| 47 | |
| 48 | static void usage(void) __attribute__((noreturn)); |
| 49 | |
| 50 | static void usage(void) |
| 51 | { |
Phil Sutter | 27ff1a5 | 2016-03-02 19:19:50 +0100 | [diff] [blame] | 52 | fprintf(stderr, "Usage: ip addrlabel { add | del } prefix PREFIX [ dev DEV ] [ label LABEL ]\n"); |
| 53 | fprintf(stderr, " ip addrlabel [ list | flush | help ]\n"); |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 54 | exit(-1); |
| 55 | } |
| 56 | |
| 57 | int print_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 58 | { |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 59 | FILE *fp = (FILE *)arg; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 60 | struct ifaddrlblmsg *ifal = NLMSG_DATA(n); |
| 61 | int len = n->nlmsg_len; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 62 | struct rtattr *tb[IFAL_MAX+1]; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 63 | |
| 64 | if (n->nlmsg_type != RTM_NEWADDRLABEL && n->nlmsg_type != RTM_DELADDRLABEL) |
| 65 | return 0; |
| 66 | |
| 67 | len -= NLMSG_LENGTH(sizeof(*ifal)); |
| 68 | if (len < 0) |
| 69 | return -1; |
| 70 | |
| 71 | parse_rtattr(tb, IFAL_MAX, IFAL_RTA(ifal), len); |
| 72 | |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 73 | if (n->nlmsg_type == RTM_DELADDRLABEL) |
| 74 | fprintf(fp, "Deleted "); |
| 75 | |
| 76 | if (tb[IFAL_ADDRESS]) { |
| 77 | fprintf(fp, "prefix %s/%u ", |
Phil Sutter | d49f934 | 2016-03-22 19:35:17 +0100 | [diff] [blame] | 78 | format_host_rta(ifal->ifal_family, |
| 79 | tb[IFAL_ADDRESS]), |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 80 | ifal->ifal_prefixlen); |
| 81 | } |
| 82 | |
| 83 | if (ifal->ifal_index) |
| 84 | fprintf(fp, "dev %s ", ll_index_to_name(ifal->ifal_index)); |
| 85 | |
Hangbin Liu | 9787033 | 2013-11-19 23:46:20 +0800 | [diff] [blame] | 86 | if (tb[IFAL_LABEL] && RTA_PAYLOAD(tb[IFAL_LABEL]) == sizeof(uint32_t)) { |
| 87 | uint32_t label; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 88 | |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 89 | memcpy(&label, RTA_DATA(tb[IFAL_LABEL]), sizeof(label)); |
Hangbin Liu | 9787033 | 2013-11-19 23:46:20 +0800 | [diff] [blame] | 90 | fprintf(fp, "label %u ", label); |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | fprintf(fp, "\n"); |
| 94 | fflush(fp); |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int ipaddrlabel_list(int argc, char **argv) |
| 99 | { |
| 100 | int af = preferred_family; |
| 101 | |
| 102 | if (af == AF_UNSPEC) |
| 103 | af = AF_INET6; |
| 104 | |
| 105 | if (argc > 0) { |
| 106 | fprintf(stderr, "\"ip addrlabel show\" does not take any arguments.\n"); |
| 107 | return -1; |
| 108 | } |
| 109 | |
| 110 | if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) { |
| 111 | perror("Cannot send dump request"); |
| 112 | return 1; |
| 113 | } |
| 114 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 115 | if (rtnl_dump_filter(&rth, print_addrlabel, stdout) < 0) { |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 116 | fprintf(stderr, "Dump terminated\n"); |
| 117 | return 1; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | |
| 124 | static int ipaddrlabel_modify(int cmd, int argc, char **argv) |
| 125 | { |
| 126 | struct { |
Stephen Hemminger | 4806867 | 2014-02-17 10:56:31 -0800 | [diff] [blame] | 127 | struct nlmsghdr n; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 128 | struct ifaddrlblmsg ifal; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 129 | char buf[1024]; |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 130 | } req = { |
| 131 | .n.nlmsg_type = cmd, |
| 132 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)), |
| 133 | .n.nlmsg_flags = NLM_F_REQUEST, |
| 134 | .ifal.ifal_family = preferred_family, |
| 135 | }; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 136 | |
Phil Sutter | d17b136 | 2016-07-18 16:48:42 +0200 | [diff] [blame] | 137 | inet_prefix prefix = {}; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 138 | uint32_t label = 0xffffffffUL; |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 139 | char *p = NULL; |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 140 | char *l = NULL; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 141 | |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 142 | if (cmd == RTM_NEWADDRLABEL) { |
| 143 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 144 | } |
| 145 | |
| 146 | while (argc > 0) { |
| 147 | if (strcmp(*argv, "prefix") == 0) { |
| 148 | NEXT_ARG(); |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 149 | p = *argv; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 150 | get_prefix(&prefix, *argv, preferred_family); |
| 151 | } else if (strcmp(*argv, "dev") == 0) { |
| 152 | NEXT_ARG(); |
| 153 | if ((req.ifal.ifal_index = ll_name_to_index(*argv)) == 0) |
| 154 | invarg("dev is invalid\n", *argv); |
| 155 | } else if (strcmp(*argv, "label") == 0) { |
| 156 | NEXT_ARG(); |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 157 | l = *argv; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 158 | if (get_u32(&label, *argv, 0) || label == 0xffffffffUL) |
| 159 | invarg("label is invalid\n", *argv); |
| 160 | } |
| 161 | argc--; |
| 162 | argv++; |
| 163 | } |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 164 | if (p == NULL) { |
| 165 | fprintf(stderr, "Not enough information: \"prefix\" argument is required.\n"); |
| 166 | return -1; |
| 167 | } |
| 168 | if (l == NULL) { |
| 169 | fprintf(stderr, "Not enough information: \"label\" argument is required.\n"); |
| 170 | return -1; |
| 171 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 172 | addattr32(&req.n, sizeof(req), IFAL_LABEL, label); |
| 173 | addattr_l(&req.n, sizeof(req), IFAL_ADDRESS, &prefix.data, prefix.bytelen); |
Varun Chandramohan | 3490740 | 2008-02-14 15:21:08 +0530 | [diff] [blame] | 174 | req.ifal.ifal_prefixlen = prefix.bitlen; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 175 | |
| 176 | if (req.ifal.ifal_family == AF_UNSPEC) |
| 177 | req.ifal.ifal_family = AF_INET6; |
| 178 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 179 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
Stephen Hemminger | 906cafe | 2015-05-06 09:55:07 -0700 | [diff] [blame] | 180 | return -2; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | |
| 186 | static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 187 | { |
| 188 | struct rtnl_handle rth2; |
| 189 | struct rtmsg *r = NLMSG_DATA(n); |
| 190 | int len = n->nlmsg_len; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 191 | struct rtattr *tb[IFAL_MAX+1]; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 192 | |
| 193 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 194 | if (len < 0) |
| 195 | return -1; |
| 196 | |
| 197 | parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len); |
| 198 | |
| 199 | if (tb[IFAL_ADDRESS]) { |
| 200 | n->nlmsg_type = RTM_DELADDRLABEL; |
| 201 | n->nlmsg_flags = NLM_F_REQUEST; |
| 202 | |
| 203 | if (rtnl_open(&rth2, 0) < 0) |
| 204 | return -1; |
| 205 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 206 | if (rtnl_talk(&rth2, n, NULL, 0) < 0) |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 207 | return -2; |
| 208 | |
| 209 | rtnl_close(&rth2); |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int ipaddrlabel_flush(int argc, char **argv) |
| 216 | { |
| 217 | int af = preferred_family; |
| 218 | |
| 219 | if (af == AF_UNSPEC) |
| 220 | af = AF_INET6; |
| 221 | |
| 222 | if (argc > 0) { |
| 223 | fprintf(stderr, "\"ip addrlabel flush\" does not allow extra arguments\n"); |
| 224 | return -1; |
| 225 | } |
| 226 | |
| 227 | if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) { |
| 228 | perror("Cannot send dump request"); |
Stephen Hemminger | 906cafe | 2015-05-06 09:55:07 -0700 | [diff] [blame] | 229 | return -1; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 230 | } |
| 231 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 232 | if (rtnl_dump_filter(&rth, flush_addrlabel, NULL) < 0) { |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 233 | fprintf(stderr, "Flush terminated\n"); |
Stephen Hemminger | 906cafe | 2015-05-06 09:55:07 -0700 | [diff] [blame] | 234 | return -1; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
| 240 | int do_ipaddrlabel(int argc, char **argv) |
| 241 | { |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 242 | if (argc < 1) { |
| 243 | return ipaddrlabel_list(0, NULL); |
| 244 | } else if (matches(argv[0], "list") == 0 || |
Mark Einon | 473544d | 2015-03-16 09:59:09 +0000 | [diff] [blame] | 245 | matches(argv[0], "lst") == 0 || |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 246 | matches(argv[0], "show") == 0) { |
| 247 | return ipaddrlabel_list(argc-1, argv+1); |
| 248 | } else if (matches(argv[0], "add") == 0) { |
| 249 | return ipaddrlabel_modify(RTM_NEWADDRLABEL, argc-1, argv+1); |
| 250 | } else if (matches(argv[0], "delete") == 0) { |
| 251 | return ipaddrlabel_modify(RTM_DELADDRLABEL, argc-1, argv+1); |
| 252 | } else if (matches(argv[0], "flush") == 0) { |
| 253 | return ipaddrlabel_flush(argc-1, argv+1); |
| 254 | } else if (matches(argv[0], "help") == 0) |
| 255 | usage(); |
| 256 | |
| 257 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip addrlabel help\".\n", *argv); |
| 258 | exit(-1); |
| 259 | } |