1 /*
2  *  Simplified Interface To NetLink
3  *
4  *  Copyright (C) 2016-2018 Antonio Quartulli <a@unstable.cc>
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 version 2
8  *  as published by the Free Software Foundation.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU General Public License for more details.
14  *
15  *  You should have received a copy of the GNU General Public License
16  *  along with this program (see the file COPYING included with this
17  *  distribution); if not, write to the Free Software Foundation, Inc.,
18  *  59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20 
21 #ifndef SITNL_H_
22 #define SITNL_H_
23 
24 #ifdef TARGET_LINUX
25 
26 #include <stdbool.h>
27 #include <netinet/in.h>
28 
29 /**
30  * Bring interface up or down.
31  *
32  * @param iface     the interface to modify
33  * @param up        true if the interface has to be brought up, false otherwise
34  *
35  * @return          0 on success, a negative error code otherwise
36  */
37 int sitnl_iface_up(const char *iface, bool up);
38 
39 /**
40  * Set the MTU for an interface
41  *
42  * @param iface     the interface to modify
43  * @param mtru      the new MTU
44  *
45  * @return          0 on success, a negative error code otherwise
46  */
47 int sitnl_iface_mtu_set(const char *iface, uint32_t mtu);
48 
49 /**
50  * Add an IPv4 address to an interface
51  *
52  * @param iface     the interface where the address has to be added
53  * @param addr      the address to add
54  * @param prefixlen the prefix length of the network associated with the address
55  * @param broadcast the broadcast address to configure on the interface
56  *
57  * @return          0 on success, a negative error code otherwise
58  */
59 int sitnl_addr_v4_add(const char *iface, const in_addr_t *addr, int prefixlen,
60                       const in_addr_t *broadcast);
61 
62 /**
63  * Add an IPv6 address to an interface
64  *
65  * @param iface     the interface where the address has to be added
66  * @param addr      the address to add
67  * @param prefixlen the prefix length of the network associated with the address
68  *
69  * @return          0 on success, a negative error code otherwise
70  */
71 
72 int sitnl_addr_v6_add(const char *iface, const struct in6_addr *addr,
73                       int prefixlen);
74 
75 /**
76  * Remove an IPv4 from an interface
77  *
78  * @param iface     the interface to remove the address from
79  * @param prefixlen the prefix length of the network associated with the address
80  *
81  * @return          0 on success, a negative error code otherwise
82  */
83 int sitnl_addr_v4_del(const char *iface, const in_addr_t *addr, int prefixlen);
84 
85 /**
86  * Remove an IPv6 from an interface
87  *
88  * @param iface     the interface to remove the address from
89  * @param prefixlen the prefix length of the network associated with the address
90  *
91  * @return          0 on success, a negative error code otherwise
92  */
93 int sitnl_addr_v6_del(const char *iface, const struct in6_addr *addr,
94                       int prefixlen);
95 
96 /**
97  * Add a point-to-point IPv4 address to an interface
98  *
99  * @param iface     the interface where the address has to be added
100  * @param local     the address to add
101  * @param remote    the associated p-t-p remote address
102  *
103  * @return          0 on success, a negative error code otherwise
104  */
105 int sitnl_addr_ptp_v4_add(const char *iface, const in_addr_t *local,
106                           const in_addr_t *remote);
107 
108 /**
109  * Remove a point-to-point IPv4 address from an interface
110  *
111  * @param iface     the interface to remove the address from
112  * @param local     the address to remove
113  *
114  * @return          0 on success, a negative error code otherwise
115  */
116 int sitnl_addr_ptp_v4_del(const char *iface, const in_addr_t *local);
117 
118 
119 /**
120  * Add a route for an IPv4 address/network
121  *
122  * @param dst       the destination of the route
123  * @param prefixlen the length of the prefix of the destination
124  * @param gw        the gateway for this route
125  * @param iface     the interface for this route (can be NULL)
126  * @param table     the table to add this route to (if 0, will be added to the
127  *                  main table)
128  * @param metric    the metric associated with the route
129  *
130  * @return          0 on success, a negative error code otherwise
131  */
132 int sitnl_route_v4_add(const in_addr_t *dst, int prefixlen,
133                        const in_addr_t *gw, const char *iface, uint32_t table,
134                        int metric);
135 
136 /**
137  * Add a route for an IPv6 address/network
138  *
139  * @param dst       the destination of the route
140  * @param prefixlen the length of the prefix of the destination
141  * @param gw        the gateway for this route
142  * @param iface     the interface for this route (can be NULL)
143  * @param table     the table to add this route to (if 0, will be added to the
144  *                  main table)
145  * @param metric    the metric associated with the route
146  *
147  * @return          0 on success, a negative error code otherwise
148  */
149 int sitnl_route_v6_add(const struct in6_addr *dst, int prefixlen,
150                        const struct in6_addr *gw, const char *iface,
151                        uint32_t table, int metric);
152 
153 /**
154  * Delete a route for an IPv4 address/network
155  *
156  * @param dst       the destination of the route
157  * @param prefixlen the length of the prefix of the destination
158  * @param gw        the gateway for this route
159  * @param iface     the interface for this route (can be NULL)
160  * @param table     the table to add this route to (if 0, will be added to the
161  *                  main table)
162  * @param metric    the metric associated with the route
163  *
164  * @return          0 on success, a negative error code otherwise
165  */
166 int sitnl_route_v4_del(const in_addr_t *dst, int prefixlen,
167                        const in_addr_t *gw, const char *iface, uint32_t table,
168                        int metric);
169 
170 /**
171  * Delete a route for an IPv4 address/network
172  *
173  * @param dst       the destination of the route
174  * @param prefixlen the length of the prefix of the destination
175  * @param gw        the gateway for this route
176  * @param iface     the interface for this route (can be NULL)
177  * @param table     the table to add this route to (if 0, will be added to the
178  *                  main table)
179  * @param metric    the metric associated with the route
180  *
181  * @return          0 on success, a negative error code otherwise
182  */
183 int sitnl_route_v6_del(const struct in6_addr *dst, int prefixlen,
184                        const struct in6_addr *gw, const char *iface,
185                        uint32_t table, int metric);
186 
187 /**
188  * Retrieve the gateway and outgoing interface for the specified IPv4
189  * address/network
190  *
191  * @param dst           The destination to lookup
192  * @param prefixlen     The length of the prefix of the destination
193  * @param best_gw       Location where the retrieved GW has to be stored
194  * @param best_iface    Location where the retrieved interface has to be stored
195  *
196  * @return              0 on success, a negative error code otherwise
197  */
198 int sitnl_route_v4_best_gw(const in_addr_t *dst, int prefixlen,
199                            in_addr_t *best_gw, char *best_iface);
200 
201 /**
202  * Retrieve the gateway and outgoing interface for the specified IPv6
203  * address/network
204  *
205  * @param dst           The destination to lookup
206  * @param prefixlen     The length of the prefix of the destination
207  * @param best_gw       Location where the retrieved GW has to be stored
208  * @param best_iface    Location where the retrieved interface has to be stored
209  *
210  * @return              0 on success, a negative error code otherwise
211  */
212 int sitnl_route_v6_best_gw(const struct in6_addr *dst, int prefixlen,
213                            struct in6_addr *best_gw, char *best_iface);
214 
215 #endif /* TARGET_LINUX */
216 
217 #endif /* SITNL_H_ */
218