guile-netlink/netlink/constant.scm

constant.scm

1
;;;; Copyright (C) 2020 Julien Lepiller <julien@lepiller.eu>
2
;;;; 
3
;;;; This library is free software; you can redistribute it and/or
4
;;;; modify it under the terms of the GNU Lesser General Public
5
;;;; License as published by the Free Software Foundation; either
6
;;;; version 3 of the License, or (at your option) any later version.
7
;;;; 
8
;;;; This library is distributed in the hope that it will be useful,
9
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
10
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11
;;;; Lesser General Public License for more details.
12
;;;; 
13
;;;; You should have received a copy of the GNU Lesser General Public
14
;;;; License along with this library; if not, write to the Free Software
15
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16
;;;; 
17
18
(define-module (netlink constant)
19
               #:export (define-enum))
20
21
(define-syntax define-enum-aux
22
  (syntax-rules ()
23
    ((_ num (name n))
24
     (define-public name n))
25
    ((_ num name)
26
     (define-public name num))
27
    ((_ num (name n) rest ...)
28
     (begin
29
       (define-public name n)
30
       (define-enum-aux (+ n 1) rest ...)))
31
    ((_ num name rest ...)
32
     (begin
33
       (define-public name num)
34
       (define-enum-aux (+ num 1) rest ...)))))
35
36
(define-syntax define-enum
37
  (lambda (x)
38
    (define (spec-names specs)
39
      (map
40
        (lambda (spec)
41
          (syntax-case spec ()
42
            ((name val) #'name)
43
            (name #'name)))
44
        specs))
45
46
    (define (getter num name)
47
      #`((= #,num #,name) (quote #,name)))
48
49
    (syntax-case x ()
50
      ((_ integer->symbol name-spec ...)
51
       #`(begin
52
           (define-enum-aux 0 name-spec ...)
53
           (define-public (integer->symbol num)
54
             (cond
55
               #,@(map (lambda (s) (getter #'num s)) (spec-names #'(name-spec ...))))))))))
56
57
(define-enum int->link-attr-kind
58
             IFLA_UNSPEC IFLA_ADDRESS IFLA_BROADCAST IFLA_IFNAME IFLA_MTU
59
             IFLA_LINK IFLA_QDISC IFLA_STATS IFLA_COST IFLA_PRIORITY
60
             IFLA_MASTER IFLA_WIRELESS IFLA_PROTIFO IFLA_TXQLEN IFLA_MAP
61
             IFLA_WEIGHT IFLA_OPERSTATE IFLA_LINKMODE IFLA_LINKINFO
62
             IFLA_NET_NS_PID IFLA_IFALIAS IFLA_NUM_VF IFLA_VFINFO_LIST
63
             IFLA_STATS64 IFLA_VF_PORTS IFLA_PORT_SELF IFLA_AF_SPEC
64
             IFLA_GROUP IFLA_NET_NS_FD IFLA_EXT_MASK IFLA_PROMISCUITY
65
             IFLA_NUM_TX_QUEUES IFLA_NUM_RX_QUEUES IFLA_CARRIER
66
             IFLA_PHYS_PORT_ID IFLA_CARRIER_CHANGES IFLA_PHYS_SWITCH_ID
67
             IFLA_LINK_NETNSID IFLA_PHYS_PORT_NAME IFLA_PROTO_DOWN
68
             IFLA_GSO_MAX_SEGS IFLA_GSO_MAX_SIZE IFLA_PAD IFLA_XDP
69
             IFLA_EVENT IFLA_NEW_NETNSID IFLA_IF_NETNSID IFLA_CARRIER_UP_COUNT
70
             IFLA_CARRIER_DOWN_COUNT IFLA_NEW_IFINDEX IFLA_MIN_MTU IFLA_MAX_MTU
71
             IFLA_PROP_LIST IFLA_ALT_IFNAME IFLA_PERM_ADDRESS)
72
(define-public IFLA_TARGET_NETNSID IFLA_IF_NETNSID)
73
74
(define-enum int->addr-attr-kind
75
             IFA_UNSPEC IFA_ADDRESS IFA_LOCAL IFA_LABEL IFA_BROADCAST
76
             IFA_ANYCAST IFA_CACHEINFO IFA_MULTICAST IFA_FLAGS
77
             IFA_RT_PRIORITY IFA_TARGET_NETNSID)
78
79
(define-enum int->route-attr-kind
80
             RTA_UNSPEC RTA_DST RTA_SRC RTA_IIF RTA_OIF RTA_GATEWAY
81
             RTA_PRIORITY RTA_PREFSRC RTA_METRICS RTA_MULTIPATH
82
             RTA_PROTOINFO RTA_FLOW RTA_CACHEINFO RTA_SESSION RTA_MP_ALGO
83
             RTA_TABLE RTA_MARK RTA_MFC_STATS RTA_VIA RTA_NEWDST RTA_PREF
84
             RTA_ENCAP_TYPE RTA_ENCAP RTA_EXPIRES RTA_PAD RTA_UID
85
             RTA_TTL_PROPAGATE RTA_IP_PROTO RTA_SPORT RTA_DPORT RTA_NH_ID)
86
87
(define-public AF_NETLINK 16)
88
(define-public AF_PACKET 17)
89
90
(define-enum int->protocol
91
             NETLINK_ROUTE NETLINK_UNUSED NETLINK_USERSOCK NETLINK_FIREWALL
92
             NETLINK_SOCK_DIAG NETLINK_NFLOG NETLINK_XFRM NETLINK_SELINUX
93
             NETLINK_ISCSI NETLINK_AUDIT NETLINK_FIB_LOOKUP NETLINK_CONNECTOR
94
             NETLINK_NETFILTER NETLINK_IP6_FW NETLINK_DNRTMSG NETLINK_KOBJECT_UEVENT
95
             NETLINK_GENERIC NETLINK_DM NETLINK_SCSITRANSPORT NETLINK_ECRYPTFS
96
             NETLINK_RDMA NETLINK_CRYPTO NETLINK_SMC)
97
(define-public NETLINK_INET_DIAG NETLINK_SOCK_DIAG)
98
99
(define-enum int->message-kind
100
  (NLMSG_NOOP 1)
101
  NLMSG_ERROR
102
  NLMSG_DONE
103
  NLMSG_OVERUN
104
  
105
  (RTM_NEWLINK 16)
106
  RTM_DELLINK
107
  RTM_GETLINK
108
  RTM_SETLINK
109
110
  (RTM_NEWADDR 20)
111
  RTM_DELADDR
112
  RTM_GETADDR
113
  
114
  (RTM_NEWROUTE 24)
115
  RTM_DELROUTE
116
  RTM_GETROUTE
117
118
  (RTM_NEWNEIGH 28)
119
  RTM_DELNEIGH
120
  RTML_GETNEIGH
121
122
  (RTM_NEWRULE 32)
123
  RTM_DELRULE
124
  RTM_GETRULE
125
126
  (RTM_NEWQDISC 36)
127
  RTM_DELQDISC
128
  RTM_GETQDISC
129
130
  (RTM_NEWTCLASS 40)
131
  RTM_DELTCLASS
132
  RTM_GETTCLASS
133
134
  (RTM_NEWTFILTER 44)
135
  RTM_DELTFILTER
136
  RTM_GETTFILTER
137
138
  (RTM_NEWACTION 48)
139
  RTM_DELACTION
140
  RTM_GETACTION
141
142
  (RTM_NEPREFIX 52)
143
  (RTM_GETMULTICAST 58)
144
  (RTM_GETANYCAST 62)
145
  (RTM_NEWNEIGHTBL 64)
146
  (RTM_GETNEIGHTBL 66)
147
  RTM_SETNEIGHTBL
148
149
  (RTM_NEWNDUSEROPT 68)
150
  (RTM_NEWADDRLABEL 72)
151
  RTM_DELADDRLABEL
152
  RTM_GETADDRLABEL
153
154
  (RTM_GETDCB 78)
155
  RTM_SETDCB
156
157
  (RTM_NEWNETCONF 80)
158
  RTM_DELNETCONF
159
  RTM_GETNETCONF
160
161
  (RTM_NEWMDB 84)
162
  RTM_DELMDB
163
  RTM_GETMDB
164
165
  (RTM_NEWNSID 88)
166
  RTM_DELNSID
167
  RTM_GETNSID
168
169
  (RTM_NEWSTATS 92)
170
  (RTM_GETSTATS 94)
171
172
  (RTM_NEWCACHEREPORT 92)
173
174
  (RTM_NEWCHAIN 100)
175
  RTM_DELCHAIN
176
  RTM_GETCHAIN
177
178
  (RTM_NEWNEXTHOP 104)
179
  RTM_DELNEXTHOP
180
  RTM_GETNEXTHOP
181
182
  (RTM_NEWLINKPROP 108)
183
  RTM_DELLINKPROP
184
  RTM_GETLINKPROP
185
186
  (RTM_NEWVLAN 112)
187
  RTM_DELVLAN
188
  RTM_GETVLAN)
189
190
(define-public NLM_F_REQUEST #x01)
191
(define-public NLM_F_MULTI #x02)
192
(define-public NLM_F_ACK #x04)
193
(define-public NLM_F_ECHO #x08)
194
(define-public NLM_F_DUMP_INTR #x10)
195
(define-public NLM_F_DUMP_FILTERED #x20)
196
197
;; modifiers to GET requests
198
(define-public NLM_F_ROOT #x100)
199
(define-public NLM_F_MATCH #x200)
200
(define-public NLM_F_ATOMIC #x400)
201
(define-public NLM_F_DUMP (logior NLM_F_ROOT NLM_F_MATCH))
202
203
;; modifiers to NEW requests
204
(define-public NLM_F_REPLACE #x100)
205
(define-public NLM_F_EXCL #x200)
206
(define-public NLM_F_CREATE #x400)
207
(define-public NLM_F_APPEND #x800)
208
209
(define-public NLM_F_NONREC #x100)
210
211
(define-public NLM_F_CAPPED #x100)
212
(define-public NLM_F_ACK_TLVS #x200)
213
214
;; net_device_flags
215
(define-enum int->device-flags
216
  (IFF_UP 1)
217
  (IFF_BROADCAST 2)
218
  (IFF_DEBUG 4)
219
  (IFF_LOOPBACK 8)
220
  (IFF_POINTOPOINT 16)
221
  (IFF_NOTRAILERS 32)
222
  (IFF_RUNNING 64)
223
  (IFF_NOARP 128)
224
  (IFF_PROMISC 256)
225
  (IFF_ALLMULTI 512)
226
  (IFF_MASTER 1024)
227
  (IFF_SLAVE 2048)
228
  (IFF_MULTICAST 4096)
229
  (IFF_PORTSEL 8192)
230
  (IFF_AUTOMEDIA 16384)
231
  (IFF_DYNAMIC 32768)
232
  (IFF_LOWER_UP 65536)
233
  (IFF_DORMANT 131072)
234
  (IFF_ECHO 262144))
235
236
;; operstate
237
(define-enum int->operstate
238
             IF_OPER_UNKNOWN IF_OPER_NOTPRESENT IF_OPER_DOWN
239
             IF_OPER_LOWERLAYERDOWN IF_OPER_TESTING IF_OPER_DORMANT
240
             IF_OPER_UP)
241
242
;; rtm_type
243
(define-enum int->rtm-type
244
             RTN_UNSPEC RTN_UNICAST RTN_LOCAL RTN_BROADCAST RTN_ANYCAST
245
             RTN_MULTICAST RTN_BLACKHOLE RTN_UNREACHABLE RTN_PROHIBIT
246
             RTN_THROW RTN_NAT RTN_XRESOLVE)
247
248
;; rtm_protocol
249
(define-enum int->rtm-protocol
250
             RTPROT_UNSPEC RTPROT_REDIRECT RTPROT_KERNEL RTPROT_BOOT RTPROT_STATIC
251
             ;; not interpreted by the kernel, but returned anyway.
252
             (RTPROT_GATED 8)
253
             RTPROT_RA RTPROT_MRT RTPROT_ZEBRA RTPROT_BIRD RTPROT_DNROUTED
254
             RTPROT_XORP RTPROT_NTK RTPROT_DHCP RTPROT_MROUTED
255
             (RTPROT_BABEL 42)
256
             (RTPROT_BGP 186)
257
             RTPROT_ISIS RTPROT_OSPF RTPROT_RIP
258
             (RTPROT_EIGRP 192))
259
260
;; rtm_scope
261
(define-enum int->rtm-scope
262
             RT_SCOPE_UNIVERSE
263
             (RT_SCOPE_SITE 200)
264
             (RT_SCOPE_LINK 253)
265
             (RT_SCOPE_HOST 254)
266
             (RT_SCOPE_NOWHERE 255))
267
268
;; rtm_flags
269
(define-enum int->rtm-flag
270
             (RTM_F_NOTIFY #x100)
271
             (RTM_F_CLONED #x200)
272
             (RTM_F_EQUALIZE #x400)
273
             (RTM_F_PREFIX #x800)
274
             (RTM_F_LOOKUP_TABLE #x1000)
275
             (RTM_F_FIB_MATCH #x2000)
276
             (RTM_F_OFFLOAD #x4000)
277
             (RTM_F_TRAP #x8000))
278
279
280
;; rtm_table
281
(define-enum int->rtm-table
282
             RT_TABLE_UNSPEC
283
             (RT_TABLE_COMPAT 252)
284
             (RT_TABLE_DEFAULT 253)
285
             (RT_TABLE_MAIN 254)
286
             (RT_TABLE_LOCAL 255))
287