1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include "conf-parser.h"
4 #include "parse-util.h"
5 #include "string-util.h"
6 #include "vlan-util.h"
7 
parse_vlanid(const char * p,uint16_t * ret)8 int parse_vlanid(const char *p, uint16_t *ret) {
9         uint16_t id;
10         int r;
11 
12         assert(p);
13         assert(ret);
14 
15         r = safe_atou16(p, &id);
16         if (r < 0)
17                 return r;
18         if (!vlanid_is_valid(id))
19                 return -ERANGE;
20 
21         *ret = id;
22         return 0;
23 }
24 
parse_vid_range(const char * p,uint16_t * vid,uint16_t * vid_end)25 int parse_vid_range(const char *p, uint16_t *vid, uint16_t *vid_end) {
26         unsigned lower, upper;
27         int r;
28 
29         r = parse_range(p, &lower, &upper);
30         if (r < 0)
31                 return r;
32 
33         if (lower > VLANID_MAX || upper > VLANID_MAX || lower > upper)
34                 return -EINVAL;
35 
36         *vid = lower;
37         *vid_end = upper;
38         return 0;
39 }
40 
config_parse_default_port_vlanid(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)41 int config_parse_default_port_vlanid(
42                 const char *unit,
43                 const char *filename,
44                 unsigned line,
45                 const char *section,
46                 unsigned section_line,
47                 const char *lvalue,
48                 int ltype,
49                 const char *rvalue,
50                 void *data,
51                 void *userdata) {
52         uint16_t *id = data;
53 
54         assert(lvalue);
55         assert(rvalue);
56         assert(data);
57 
58         if (streq(rvalue, "none")) {
59                 *id = 0;
60                 return 0;
61         }
62 
63         return config_parse_vlanid(unit, filename, line, section, section_line,
64                                    lvalue, ltype, rvalue, data, userdata);
65 }
66 
config_parse_vlanid(const char * unit,const char * filename,unsigned line,const char * section,unsigned section_line,const char * lvalue,int ltype,const char * rvalue,void * data,void * userdata)67 int config_parse_vlanid(
68                 const char *unit,
69                 const char *filename,
70                 unsigned line,
71                 const char *section,
72                 unsigned section_line,
73                 const char *lvalue,
74                 int ltype,
75                 const char *rvalue,
76                 void *data,
77                 void *userdata) {
78 
79         uint16_t *id = data;
80         int r;
81 
82         assert(filename);
83         assert(lvalue);
84         assert(rvalue);
85         assert(data);
86 
87         r = parse_vlanid(rvalue, id);
88         if (r == -ERANGE) {
89                 log_syntax(unit, LOG_WARNING, filename, line, r,
90                            "VLAN identifier outside of valid range 0…4094, ignoring: %s", rvalue);
91                 return 0;
92         }
93         if (r < 0) {
94                 log_syntax(unit, LOG_WARNING, filename, line, r,
95                            "Failed to parse VLAN identifier value, ignoring: %s", rvalue);
96                 return 0;
97         }
98 
99         return 0;
100 }
101