]> git.proxmox.com Git - mirror_iproute2.git/blame - ip/ipprefix.c
ip netns: Allow exec on each netns
[mirror_iproute2.git] / ip / ipprefix.c
CommitLineData
1cb54e58 1/*
2 * Copyright (C)2005 USAGI/WIDE Project
ae665a52 3 *
1cb54e58 4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
ae665a52 8 *
1cb54e58 9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
ae665a52 13 *
1cb54e58 14 * You should have received a copy of the GNU General Public License
4d98ab00 15 * along with this program; if not, see <http://www.gnu.org/licenses>.
1cb54e58 16 */
17/*
18 * based on ip.c, iproute.c
19 */
20/*
21 * Authors:
22 * Masahide NAKAMURA @USAGI
23 */
24
25#include <stdio.h>
26#include <stdlib.h>
27#include <string.h>
5bd9dd49 28#include <sys/socket.h>
1cb54e58 29#include <netinet/icmp6.h>
d1f28cf1 30
1cb54e58 31#include "utils.h"
d1f28cf1 32#include "ip_common.h"
1cb54e58 33
34/* prefix flags; see kernel's net/ipv6/addrconf.c and include/net/if_inet6.h */
35#define IF_PREFIX_ONLINK 0x01
36#define IF_PREFIX_AUTOCONF 0x02
37
38int print_prefix(const struct sockaddr_nl *who, struct nlmsghdr *n, void *arg)
39{
40 FILE *fp = (FILE*)arg;
41 struct prefixmsg *prefix = NLMSG_DATA(n);
42 int len = n->nlmsg_len;
43 struct rtattr * tb[RTA_MAX+1];
44 int family = preferred_family;
45
46 if (n->nlmsg_type != RTM_NEWPREFIX) {
47 fprintf(stderr, "Not a prefix: %08x %08x %08x\n",
48 n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags);
49 return 0;
50 }
51
52 len -= NLMSG_LENGTH(sizeof(*prefix));
53 if (len < 0) {
54 fprintf(stderr, "BUG: wrong nlmsg len %d\n", len);
55 return -1;
56 }
57
58 if (family == AF_UNSPEC)
59 family = AF_INET6;
60 if (family != AF_INET6)
61 return 0;
62
63 if (prefix->prefix_family != AF_INET6) {
14645ec2 64 fprintf(stderr, "incorrect protocol family: %d\n", prefix->prefix_family);
1cb54e58 65 return 0;
66 }
67 if (prefix->prefix_type != ND_OPT_PREFIX_INFORMATION) {
68 fprintf(stderr, "wrong ND type %d\n", prefix->prefix_type);
69 return 0;
70 }
71
1cb54e58 72 parse_rtattr(tb, RTA_MAX, RTM_RTA(prefix), len);
73
74 fprintf(fp, "prefix ");
75
76 if (tb[PREFIX_ADDRESS]) {
77 struct in6_addr *pfx;
78 char abuf[256];
79
80 pfx = (struct in6_addr *)RTA_DATA(tb[PREFIX_ADDRESS]);
81
82 memset(abuf, '\0', sizeof(abuf));
656111b2 83 fprintf(fp, "%s", rt_addr_n2a(family, pfx,
1cb54e58 84 abuf, sizeof(abuf)));
85 }
86 fprintf(fp, "/%u ", prefix->prefix_len);
87
88 fprintf(fp, "dev %s ", ll_index_to_name(prefix->prefix_ifindex));
89
90 if (prefix->prefix_flags & IF_PREFIX_ONLINK)
91 fprintf(fp, "onlink ");
92 if (prefix->prefix_flags & IF_PREFIX_AUTOCONF)
93 fprintf(fp, "autoconf ");
94
95 if (tb[PREFIX_CACHEINFO]) {
96 struct prefix_cacheinfo *pc;
c13f5982 97 pc = (struct prefix_cacheinfo *)RTA_DATA(tb[PREFIX_CACHEINFO]);
1cb54e58 98
99 fprintf(fp, "valid %u ", pc->valid_time);
100 fprintf(fp, "preferred %u ", pc->preferred_time);
101 }
102
103 fprintf(fp, "\n");
104 fflush(fp);
105
106 return 0;
107}
108