2 This file is part of systemd.
4 Copyright 2016 Daniel Mack
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
24 #include "extract-word.h"
25 #include "hostname-util.h"
26 #include "ip-address-access.h"
27 #include "parse-util.h"
28 #include "string-util.h"
30 int config_parse_ip_address_access(
35 unsigned section_line
,
42 IPAddressAccessItem
**list
= data
;
48 if (isempty(rvalue
)) {
49 *list
= ip_address_access_free_all(*list
);
56 _cleanup_free_ IPAddressAccessItem
*a
= NULL
;
57 _cleanup_free_
char *word
= NULL
;
59 r
= extract_first_word(&p
, &word
, NULL
, 0);
65 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Invalid syntax, ignoring: %s", rvalue
);
69 a
= new0(IPAddressAccessItem
, 1);
73 if (streq(word
, "any")) {
74 /* "any" is a shortcut for 0.0.0.0/0 and ::/0 */
77 LIST_APPEND(items
, *list
, a
);
79 a
= new0(IPAddressAccessItem
, 1);
85 } else if (is_localhost(word
)) {
86 /* "localhost" is a shortcut for 127.0.0.0/8 and ::1/128 */
89 a
->address
.in
.s_addr
= htobe32(0x7f000000);
91 LIST_APPEND(items
, *list
, a
);
93 a
= new0(IPAddressAccessItem
, 1);
98 a
->address
.in6
= (struct in6_addr
) IN6ADDR_LOOPBACK_INIT
;
101 } else if (streq(word
, "link-local")) {
103 /* "link-local" is a shortcut for 169.254.0.0/16 and fe80::/64 */
106 a
->address
.in
.s_addr
= htobe32((UINT32_C(169) << 24 | UINT32_C(254) << 16));
108 LIST_APPEND(items
, *list
, a
);
110 a
= new0(IPAddressAccessItem
, 1);
114 a
->family
= AF_INET6
;
115 a
->address
.in6
= (struct in6_addr
) {
116 .__in6_u
.__u6_addr32
[0] = htobe32(0xfe800000)
120 } else if (streq(word
, "multicast")) {
122 /* "multicast" is a shortcut for 224.0.0.0/4 and ff00::/8 */
125 a
->address
.in
.s_addr
= htobe32((UINT32_C(224) << 24));
127 LIST_APPEND(items
, *list
, a
);
129 a
= new0(IPAddressAccessItem
, 1);
133 a
->family
= AF_INET6
;
134 a
->address
.in6
= (struct in6_addr
) {
135 .__in6_u
.__u6_addr32
[0] = htobe32(0xff000000)
140 r
= in_addr_prefix_from_string_auto(word
, &a
->family
, &a
->address
, &a
->prefixlen
);
142 log_syntax(unit
, LOG_WARNING
, filename
, line
, r
, "Address prefix is invalid, ignoring assignment: %s", word
);
147 LIST_APPEND(items
, *list
, a
);
154 IPAddressAccessItem
* ip_address_access_free_all(IPAddressAccessItem
*first
) {
155 IPAddressAccessItem
*next
, *p
= first
;
158 next
= p
->items_next
;