Debian Codegraph @

utils_linux.go

raw symbols SCIP
1 // Copyright The Prometheus Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 //go:build linux
15
16 package probing
17
18 import (
19 "errors"
20 "os"
21 "reflect"
22 "syscall"
23
24 "golang.org/x/net/bpf"
25 "golang.org/x/net/icmp"
26 "golang.org/x/net/ipv4"
27 "golang.org/x/net/ipv6"
28 )
29
30 // Returns the length of an ICMP message.
31 func (p *Pinger) getMessageLength() int {
32 return p.Size + 8
33 }
34
35 // Attempts to match the ID of an ICMP packet.
36 func (p *Pinger) matchID(ID int) bool {
37 // On Linux we can only match ID if we are privileged.
38 if p.protocol == "icmp" {
39 return ID == p.id
40 }
41 return true
42 }
43
44 // SetMark sets the SO_MARK socket option on outgoing ICMP packets.
45 // Setting this option requires CAP_NET_ADMIN.
46 func (c *icmpConn) SetMark(mark uint) error {
47 fd, err := getFD(c.c)
48 if err != nil {
49 return err
50 }
51 return os.NewSyscallError(
52 "setsockopt",
53 syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(mark)),
54 )
55 }
56
57 // SetMark sets the SO_MARK socket option on outgoing ICMP packets.
58 // Setting this option requires CAP_NET_ADMIN.
59 func (c *icmpv4Conn) SetMark(mark uint) error {
60 fd, err := getFD(c.c)
61 if err != nil {
62 return err
63 }
64 return os.NewSyscallError(
65 "setsockopt",
66 syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(mark)),
67 )
68 }
69
70 // SetMark sets the SO_MARK socket option on outgoing ICMP packets.
71 // Setting this option requires CAP_NET_ADMIN.
72 func (c *icmpV6Conn) SetMark(mark uint) error {
73 fd, err := getFD(c.c)
74 if err != nil {
75 return err
76 }
77 return os.NewSyscallError(
78 "setsockopt",
79 syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_MARK, int(mark)),
80 )
81 }
82
83 // SetDoNotFragment sets the do-not-fragment bit in the IP header of outgoing ICMP packets.
84 func (c *icmpConn) SetDoNotFragment() error {
85 fd, err := getFD(c.c)
86 if err != nil {
87 return err
88 }
89 return os.NewSyscallError(
90 "setsockopt",
91 syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_MTU_DISCOVER, syscall.IP_PMTUDISC_DO),
92 )
93 }
94
95 // SetDoNotFragment sets the do-not-fragment bit in the IP header of outgoing ICMP packets.
96 func (c *icmpv4Conn) SetDoNotFragment() error {
97 fd, err := getFD(c.c)
98 if err != nil {
99 return err
100 }
101 return os.NewSyscallError(
102 "setsockopt",
103 syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IP, syscall.IP_MTU_DISCOVER, syscall.IP_PMTUDISC_DO),
104 )
105 }
106
107 // SetDoNotFragment sets the do-not-fragment bit in the IPv6 header of outgoing ICMPv6 packets.
108 func (c *icmpV6Conn) SetDoNotFragment() error {
109 fd, err := getFD(c.c)
110 if err != nil {
111 return err
112 }
113 return os.NewSyscallError(
114 "setsockopt",
115 syscall.SetsockoptInt(int(fd), syscall.IPPROTO_IPV6, syscall.IPV6_MTU_DISCOVER, syscall.IP_PMTUDISC_DO),
116 )
117 }
118
119 func (c *icmpConn) SetBroadcastFlag() error {
120 fd, err := getFD(c.c)
121 if err != nil {
122 return err
123 }
124
125 return os.NewSyscallError(
126 "setsockopt",
127 syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1),
128 )
129 }
130
131 func (c *icmpv4Conn) SetBroadcastFlag() error {
132 fd, err := getFD(c.c)
133 if err != nil {
134 return err
135 }
136
137 return os.NewSyscallError(
138 "setsockopt",
139 syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1),
140 )
141 }
142
143 func (c *icmpV6Conn) SetBroadcastFlag() error {
144 fd, err := getFD(c.c)
145 if err != nil {
146 return err
147 }
148
149 return os.NewSyscallError(
150 "setsockopt",
151 syscall.SetsockoptInt(int(fd), syscall.SOL_SOCKET, syscall.SO_BROADCAST, 1),
152 )
153 }
154
155 // InstallICMPIDFilter attaches a BPF program to the connection to filter ICMP packets id.
156 func (c *icmpv4Conn) InstallICMPIDFilter(id int) error {
157 filter, err := bpf.Assemble([]bpf.Instruction{
158 bpf.LoadMemShift{Off: 0}, // Skip IP header
159 bpf.LoadIndirect{Off: 0, Size: 1}, // Load ICMP type
160 bpf.JumpIf{Cond: bpf.JumpEqual, Val: uint32(ipv4.ICMPTypeEchoReply), SkipTrue: 1, SkipFalse: 0}, // Check if ICMP Echo Reply
161 bpf.RetConstant{Val: 0}, // Reject if false
162 bpf.LoadIndirect{Off: 4, Size: 2}, // Load ICMP echo ident
163 bpf.JumpIf{Cond: bpf.JumpEqual, Val: uint32(id), SkipTrue: 0, SkipFalse: 1}, // Check if it matches our identifier
164 bpf.RetConstant{Val: ^uint32(0)}, // Accept if true
165 bpf.RetConstant{Val: 0}, // Reject if false
166 })
167 if err != nil {
168 return err
169 }
170 return c.c.IPv4PacketConn().SetBPF(filter)
171 }
172
173 // InstallICMPIDFilter attaches a BPF program to the connection to filter ICMPv6 packets id.
174 func (c *icmpV6Conn) InstallICMPIDFilter(id int) error {
175 filter, err := bpf.Assemble([]bpf.Instruction{
176 bpf.LoadAbsolute{Off: 0, Size: 1}, // Load ICMP type
177 bpf.JumpIf{Cond: bpf.JumpEqual, Val: uint32(ipv6.ICMPTypeEchoReply), SkipTrue: 1, SkipFalse: 0}, // Check if it is an ICMP6 echo reply
178 bpf.RetConstant{Val: 0}, // Reject if false
179 bpf.LoadAbsolute{Off: 4, Size: 2}, // Load ICMP echo identifier
180 bpf.JumpIf{Cond: bpf.JumpEqual, Val: uint32(id), SkipTrue: 0, SkipFalse: 1}, // Check if it matches our identifier
181 bpf.RetConstant{Val: ^uint32(0)}, // Accept if true
182 bpf.RetConstant{Val: 0}, // Reject if false
183 })
184 if err != nil {
185 return err
186 }
187 return c.c.IPv6PacketConn().SetBPF(filter)
188 }
189
190 // getFD gets the system file descriptor for an icmp.PacketConn
191 func getFD(c *icmp.PacketConn) (uintptr, error) {
192 v := reflect.ValueOf(c).Elem().FieldByName("c").Elem()
193 if v.Elem().Kind() != reflect.Struct {
194 return 0, errors.New("invalid type")
195 }
196
197 fd := v.Elem().FieldByName("conn").FieldByName("fd")
198 if fd.Elem().Kind() != reflect.Struct {
199 return 0, errors.New("invalid type")
200 }
201
202 pfd := fd.Elem().FieldByName("pfd")
203 if pfd.Kind() != reflect.Struct {
204 return 0, errors.New("invalid type")
205 }
206
207 return uintptr(pfd.FieldByName("Sysfd").Int()), nil
208 }