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]; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 130 | } req; |
| 131 | |
| 132 | inet_prefix prefix; |
| 133 | uint32_t label = 0xffffffffUL; |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 134 | char *p = NULL; |
Stephen Hemminger | 0612519 | 2014-02-17 10:55:31 -0800 | [diff] [blame] | 135 | char *l = NULL; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 136 | |
| 137 | memset(&req, 0, sizeof(req)); |
| 138 | memset(&prefix, 0, sizeof(prefix)); |
| 139 | |
| 140 | req.n.nlmsg_type = cmd; |
| 141 | req.n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ifaddrlblmsg)); |
| 142 | req.n.nlmsg_flags = NLM_F_REQUEST; |
| 143 | req.ifal.ifal_family = preferred_family; |
| 144 | req.ifal.ifal_prefixlen = 0; |
| 145 | req.ifal.ifal_index = 0; |
| 146 | |
| 147 | if (cmd == RTM_NEWADDRLABEL) { |
| 148 | req.n.nlmsg_flags |= NLM_F_CREATE|NLM_F_EXCL; |
| 149 | } |
| 150 | |
| 151 | while (argc > 0) { |
| 152 | if (strcmp(*argv, "prefix") == 0) { |
| 153 | NEXT_ARG(); |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 154 | p = *argv; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 155 | get_prefix(&prefix, *argv, preferred_family); |
| 156 | } else if (strcmp(*argv, "dev") == 0) { |
| 157 | NEXT_ARG(); |
| 158 | if ((req.ifal.ifal_index = ll_name_to_index(*argv)) == 0) |
| 159 | invarg("dev is invalid\n", *argv); |
| 160 | } else if (strcmp(*argv, "label") == 0) { |
| 161 | NEXT_ARG(); |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 162 | l = *argv; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 163 | if (get_u32(&label, *argv, 0) || label == 0xffffffffUL) |
| 164 | invarg("label is invalid\n", *argv); |
| 165 | } |
| 166 | argc--; |
| 167 | argv++; |
| 168 | } |
Varun Chandramohan | 6579fee | 2008-06-02 13:55:56 +0530 | [diff] [blame] | 169 | if (p == NULL) { |
| 170 | fprintf(stderr, "Not enough information: \"prefix\" argument is required.\n"); |
| 171 | return -1; |
| 172 | } |
| 173 | if (l == NULL) { |
| 174 | fprintf(stderr, "Not enough information: \"label\" argument is required.\n"); |
| 175 | return -1; |
| 176 | } |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 177 | addattr32(&req.n, sizeof(req), IFAL_LABEL, label); |
| 178 | addattr_l(&req.n, sizeof(req), IFAL_ADDRESS, &prefix.data, prefix.bytelen); |
Varun Chandramohan | 3490740 | 2008-02-14 15:21:08 +0530 | [diff] [blame] | 179 | req.ifal.ifal_prefixlen = prefix.bitlen; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 180 | |
| 181 | if (req.ifal.ifal_family == AF_UNSPEC) |
| 182 | req.ifal.ifal_family = AF_INET6; |
| 183 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 184 | if (rtnl_talk(&rth, &req.n, NULL, 0) < 0) |
Stephen Hemminger | 906cafe | 2015-05-06 09:55:07 -0700 | [diff] [blame] | 185 | return -2; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
| 190 | |
| 191 | static int flush_addrlabel(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg) |
| 192 | { |
| 193 | struct rtnl_handle rth2; |
| 194 | struct rtmsg *r = NLMSG_DATA(n); |
| 195 | int len = n->nlmsg_len; |
Stephen Hemminger | 56f5daa | 2016-03-21 11:52:19 -0700 | [diff] [blame] | 196 | struct rtattr *tb[IFAL_MAX+1]; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 197 | |
| 198 | len -= NLMSG_LENGTH(sizeof(*r)); |
| 199 | if (len < 0) |
| 200 | return -1; |
| 201 | |
| 202 | parse_rtattr(tb, IFAL_MAX, RTM_RTA(r), len); |
| 203 | |
| 204 | if (tb[IFAL_ADDRESS]) { |
| 205 | n->nlmsg_type = RTM_DELADDRLABEL; |
| 206 | n->nlmsg_flags = NLM_F_REQUEST; |
| 207 | |
| 208 | if (rtnl_open(&rth2, 0) < 0) |
| 209 | return -1; |
| 210 | |
Stephen Hemminger | c079e12 | 2015-05-27 12:26:14 -0700 | [diff] [blame] | 211 | if (rtnl_talk(&rth2, n, NULL, 0) < 0) |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 212 | return -2; |
| 213 | |
| 214 | rtnl_close(&rth2); |
| 215 | } |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int ipaddrlabel_flush(int argc, char **argv) |
| 221 | { |
| 222 | int af = preferred_family; |
| 223 | |
| 224 | if (af == AF_UNSPEC) |
| 225 | af = AF_INET6; |
| 226 | |
| 227 | if (argc > 0) { |
| 228 | fprintf(stderr, "\"ip addrlabel flush\" does not allow extra arguments\n"); |
| 229 | return -1; |
| 230 | } |
| 231 | |
| 232 | if (rtnl_wilddump_request(&rth, af, RTM_GETADDRLABEL) < 0) { |
| 233 | perror("Cannot send dump request"); |
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 | |
Stephen Hemminger | cd70f3f | 2011-12-28 10:37:12 -0800 | [diff] [blame] | 237 | if (rtnl_dump_filter(&rth, flush_addrlabel, NULL) < 0) { |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 238 | fprintf(stderr, "Flush terminated\n"); |
Stephen Hemminger | 906cafe | 2015-05-06 09:55:07 -0700 | [diff] [blame] | 239 | return -1; |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | int do_ipaddrlabel(int argc, char **argv) |
| 246 | { |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 247 | if (argc < 1) { |
| 248 | return ipaddrlabel_list(0, NULL); |
| 249 | } else if (matches(argv[0], "list") == 0 || |
Mark Einon | 473544d | 2015-03-16 09:59:09 +0000 | [diff] [blame] | 250 | matches(argv[0], "lst") == 0 || |
YOSHIFUJI Hideaki / 吉藤英明 | 4759758 | 2008-02-14 03:20:12 +1100 | [diff] [blame] | 251 | matches(argv[0], "show") == 0) { |
| 252 | return ipaddrlabel_list(argc-1, argv+1); |
| 253 | } else if (matches(argv[0], "add") == 0) { |
| 254 | return ipaddrlabel_modify(RTM_NEWADDRLABEL, argc-1, argv+1); |
| 255 | } else if (matches(argv[0], "delete") == 0) { |
| 256 | return ipaddrlabel_modify(RTM_DELADDRLABEL, argc-1, argv+1); |
| 257 | } else if (matches(argv[0], "flush") == 0) { |
| 258 | return ipaddrlabel_flush(argc-1, argv+1); |
| 259 | } else if (matches(argv[0], "help") == 0) |
| 260 | usage(); |
| 261 | |
| 262 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip addrlabel help\".\n", *argv); |
| 263 | exit(-1); |
| 264 | } |