Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_ssrf.c
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
26#include "nilorea/n_ssrf.h"
27
28/* 1 if the IPv4 address is NOT safe to connect to (private/reserved/etc.). */
29static int v4_blocked(const uint8_t ip[4]) {
30 uint8_t a = ip[0], b = ip[1], c = ip[2];
31 if (a == 0)
32 return 1; /* 0.0.0.0/8 "this host" */
33 if (a == 10)
34 return 1; /* 10.0.0.0/8 private (RFC1918) */
35 if (a == 127)
36 return 1; /* 127.0.0.0/8 loopback */
37 if (a == 100 && (b & 0xC0) == 0x40)
38 return 1; /* 100.64.0.0/10 CGNAT (RFC6598) */
39 if (a == 169 && b == 254)
40 return 1; /* 169.254.0.0/16 link-local */
41 if (a == 172 && (b & 0xF0) == 0x10)
42 return 1; /* 172.16.0.0/12 private */
43 if (a == 192 && b == 168)
44 return 1; /* 192.168.0.0/16 private */
45 if (a == 192 && b == 0 && c == 0)
46 return 1; /* 192.0.0.0/24 IETF protocol */
47 if (a == 192 && b == 0 && c == 2)
48 return 1; /* 192.0.2.0/24 TEST-NET-1 */
49 if (a == 198 && (b & 0xFE) == 18)
50 return 1; /* 198.18.0.0/15 benchmarking */
51 if (a == 198 && b == 51 && c == 100)
52 return 1; /* 198.51.100.0/24 TEST-NET-2 */
53 if (a == 203 && b == 0 && c == 113)
54 return 1; /* 203.0.113.0/24 TEST-NET-3 */
55 if (a >= 224)
56 return 1; /* 224/4 multicast + 240/4 reserved + 255.. */
57 return 0;
58}
59
60int n_ssrf_ipv4_allowed(const uint8_t ip[4]) {
61 return ip && !v4_blocked(ip);
62}
63
64/* Are bytes [lo, hi) of \p ip all zero? */
65static int zero_run(const uint8_t* ip, int lo, int hi) {
66 int i;
67 for (i = lo; i < hi; i++)
68 if (ip[i])
69 return 0;
70 return 1;
71}
72
73static int v6_blocked(const uint8_t ip[16]) {
74 if (zero_run(ip, 0, 16)) /* :: unspecified */
75 return 1;
76 if (zero_run(ip, 0, 15) && ip[15] == 1) /* ::1 loopback */
77 return 1;
78 /* ::ffff:0:0/96 IPv4-mapped -> classify the embedded IPv4 */
79 if (zero_run(ip, 0, 10) && ip[10] == 0xff && ip[11] == 0xff)
80 return v4_blocked(ip + 12);
81 /* 64:ff9b::/96 NAT64 -> classify the embedded IPv4 */
82 if (ip[0] == 0x00 && ip[1] == 0x64 && ip[2] == 0xff && ip[3] == 0x9b &&
83 zero_run(ip, 4, 12))
84 return v4_blocked(ip + 12);
85 /* 2002::/16 6to4 -> classify the embedded IPv4 (bytes 2..5) */
86 if (ip[0] == 0x20 && ip[1] == 0x02)
87 return v4_blocked(ip + 2);
88 if ((ip[0] & 0xFE) == 0xFC) /* fc00::/7 unique-local (ULA) */
89 return 1;
90 if (ip[0] == 0xFE && (ip[1] & 0xC0) == 0x80) /* fe80::/10 link-local */
91 return 1;
92 if (ip[0] == 0xFF) /* ff00::/8 multicast */
93 return 1;
94 if (ip[0] == 0x20 && ip[1] == 0x01 && ip[2] == 0x0D && ip[3] == 0xB8)
95 return 1; /* 2001:db8::/32 documentation */
96 if (ip[0] == 0x01 && ip[1] == 0x00 && zero_run(ip, 2, 8))
97 return 1; /* 100::/64 discard-only */
98 return 0;
99}
100
101int n_ssrf_ipv6_allowed(const uint8_t ip[16]) {
102 return ip && !v6_blocked(ip);
103}
int n_ssrf_ipv4_allowed(const uint8_t ip[4])
classify an IPv4 address as safe-to-connect or not
Definition n_ssrf.c:60
static int v4_blocked(const uint8_t ip[4])
Definition n_ssrf.c:29
static int zero_run(const uint8_t *ip, int lo, int hi)
Definition n_ssrf.c:65
int n_ssrf_ipv6_allowed(const uint8_t ip[16])
classify an IPv6 address as safe-to-connect or not
Definition n_ssrf.c:101
static int v6_blocked(const uint8_t ip[16])
Definition n_ssrf.c:73
SSRF address guard: is a resolved IP a public, routable unicast address?