1dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat/*
2dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat * rtm_map.c
3dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat *
4dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat *		This program is free software; you can redistribute it and/or
5dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat *		modify it under the terms of the GNU General Public License
6dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat *		as published by the Free Software Foundation; either version
7dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat *		2 of the License, or (at your option) any later version.
8dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat *
9dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat * Authors:	Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
10dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat *
11dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat */
12dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
13dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <stdio.h>
14dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <stdlib.h>
15dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <unistd.h>
16dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <syslog.h>
17dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <fcntl.h>
18dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <string.h>
19dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <sys/socket.h>
20dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include <netinet/in.h>
21dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
22dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include "rt_names.h"
23dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat#include "utils.h"
24dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
25dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehatchar *rtnl_rtntype_n2a(int id, char *buf, int len)
26dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat{
27dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	switch (id) {
28dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_UNSPEC:
29dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "none";
30dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_UNICAST:
31dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "unicast";
32dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_LOCAL:
33dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "local";
34dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_BROADCAST:
35dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "broadcast";
36dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_ANYCAST:
37dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "anycast";
38dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_MULTICAST:
39dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "multicast";
40dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_BLACKHOLE:
41dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "blackhole";
42dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_UNREACHABLE:
43dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "unreachable";
44dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_PROHIBIT:
45dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "prohibit";
46dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_THROW:
47dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "throw";
48dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_NAT:
49dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "nat";
50dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	case RTN_XRESOLVE:
51dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return "xresolve";
52dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	default:
53dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		snprintf(buf, len, "%d", id);
54dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return buf;
55dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	}
56dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat}
57dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
58dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
59dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehatint rtnl_rtntype_a2n(int *id, char *arg)
60dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat{
61dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	char *end;
62dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	unsigned long res;
63dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
64dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	if (strcmp(arg, "local") == 0)
65dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_LOCAL;
66dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (strcmp(arg, "nat") == 0)
67dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_NAT;
68dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "broadcast") == 0 ||
69dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		 strcmp(arg, "brd") == 0)
70dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_BROADCAST;
71dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "anycast") == 0)
72dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_ANYCAST;
73dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "multicast") == 0)
74dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_MULTICAST;
75dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "prohibit") == 0)
76dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_PROHIBIT;
77dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "unreachable") == 0)
78dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_UNREACHABLE;
79dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "blackhole") == 0)
80dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_BLACKHOLE;
81dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "xresolve") == 0)
82dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_XRESOLVE;
83dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (matches(arg, "unicast") == 0)
84dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_UNICAST;
85dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else if (strcmp(arg, "throw") == 0)
86dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = RTN_THROW;
87dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	else {
88dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		res = strtoul(arg, &end, 0);
89dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		if (!end || end == arg || *end || res > 255)
90dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat			return -1;
91dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	}
92dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	*id = res;
93dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	return 0;
94dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat}
95dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
96dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehatint get_rt_realms(__u32 *realms, char *arg)
97dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat{
98dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	__u32 realm = 0;
99dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	char *p = strchr(arg, '/');
100dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat
101dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	*realms = 0;
102dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	if (p) {
103dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		*p = 0;
104dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		if (rtnl_rtrealm_a2n(realms, arg)) {
105dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat			*p = '/';
106dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat			return -1;
107dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		}
108dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		*realms <<= 16;
109dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		*p = '/';
110dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		arg = p+1;
111dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	}
112dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	if (*arg && rtnl_rtrealm_a2n(&realm, arg))
113dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat		return -1;
114dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	*realms |= realm;
115dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat	return 0;
116dcfb7a77f8709125e97c313cb8ab6ec4d87468f4San Mehat}
117