blob: 703dafe84a722dbb72754705d423aaf60d2a3f18 [file] [log] [blame]
David K. Bainbridge215e0242017-09-05 23:18:24 -07001// Copyright 2013 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package ipv6
6
7import (
8 "net"
9 "syscall"
10
11 "golang.org/x/net/bpf"
12)
13
14// MulticastHopLimit returns the hop limit field value for outgoing
15// multicast packets.
16func (c *dgramOpt) MulticastHopLimit() (int, error) {
17 if !c.ok() {
18 return 0, syscall.EINVAL
19 }
20 so, ok := sockOpts[ssoMulticastHopLimit]
21 if !ok {
22 return 0, errOpNoSupport
23 }
24 return so.GetInt(c.Conn)
25}
26
27// SetMulticastHopLimit sets the hop limit field value for future
28// outgoing multicast packets.
29func (c *dgramOpt) SetMulticastHopLimit(hoplim int) error {
30 if !c.ok() {
31 return syscall.EINVAL
32 }
33 so, ok := sockOpts[ssoMulticastHopLimit]
34 if !ok {
35 return errOpNoSupport
36 }
37 return so.SetInt(c.Conn, hoplim)
38}
39
40// MulticastInterface returns the default interface for multicast
41// packet transmissions.
42func (c *dgramOpt) MulticastInterface() (*net.Interface, error) {
43 if !c.ok() {
44 return nil, syscall.EINVAL
45 }
46 so, ok := sockOpts[ssoMulticastInterface]
47 if !ok {
48 return nil, errOpNoSupport
49 }
50 return so.getMulticastInterface(c.Conn)
51}
52
53// SetMulticastInterface sets the default interface for future
54// multicast packet transmissions.
55func (c *dgramOpt) SetMulticastInterface(ifi *net.Interface) error {
56 if !c.ok() {
57 return syscall.EINVAL
58 }
59 so, ok := sockOpts[ssoMulticastInterface]
60 if !ok {
61 return errOpNoSupport
62 }
63 return so.setMulticastInterface(c.Conn, ifi)
64}
65
66// MulticastLoopback reports whether transmitted multicast packets
67// should be copied and send back to the originator.
68func (c *dgramOpt) MulticastLoopback() (bool, error) {
69 if !c.ok() {
70 return false, syscall.EINVAL
71 }
72 so, ok := sockOpts[ssoMulticastLoopback]
73 if !ok {
74 return false, errOpNoSupport
75 }
76 on, err := so.GetInt(c.Conn)
77 if err != nil {
78 return false, err
79 }
80 return on == 1, nil
81}
82
83// SetMulticastLoopback sets whether transmitted multicast packets
84// should be copied and send back to the originator.
85func (c *dgramOpt) SetMulticastLoopback(on bool) error {
86 if !c.ok() {
87 return syscall.EINVAL
88 }
89 so, ok := sockOpts[ssoMulticastLoopback]
90 if !ok {
91 return errOpNoSupport
92 }
93 return so.SetInt(c.Conn, boolint(on))
94}
95
96// JoinGroup joins the group address group on the interface ifi.
97// By default all sources that can cast data to group are accepted.
98// It's possible to mute and unmute data transmission from a specific
99// source by using ExcludeSourceSpecificGroup and
100// IncludeSourceSpecificGroup.
101// JoinGroup uses the system assigned multicast interface when ifi is
102// nil, although this is not recommended because the assignment
103// depends on platforms and sometimes it might require routing
104// configuration.
105func (c *dgramOpt) JoinGroup(ifi *net.Interface, group net.Addr) error {
106 if !c.ok() {
107 return syscall.EINVAL
108 }
109 so, ok := sockOpts[ssoJoinGroup]
110 if !ok {
111 return errOpNoSupport
112 }
113 grp := netAddrToIP16(group)
114 if grp == nil {
115 return errMissingAddress
116 }
117 return so.setGroup(c.Conn, ifi, grp)
118}
119
120// LeaveGroup leaves the group address group on the interface ifi
121// regardless of whether the group is any-source group or
122// source-specific group.
123func (c *dgramOpt) LeaveGroup(ifi *net.Interface, group net.Addr) error {
124 if !c.ok() {
125 return syscall.EINVAL
126 }
127 so, ok := sockOpts[ssoLeaveGroup]
128 if !ok {
129 return errOpNoSupport
130 }
131 grp := netAddrToIP16(group)
132 if grp == nil {
133 return errMissingAddress
134 }
135 return so.setGroup(c.Conn, ifi, grp)
136}
137
138// JoinSourceSpecificGroup joins the source-specific group comprising
139// group and source on the interface ifi.
140// JoinSourceSpecificGroup uses the system assigned multicast
141// interface when ifi is nil, although this is not recommended because
142// the assignment depends on platforms and sometimes it might require
143// routing configuration.
144func (c *dgramOpt) JoinSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
145 if !c.ok() {
146 return syscall.EINVAL
147 }
148 so, ok := sockOpts[ssoJoinSourceGroup]
149 if !ok {
150 return errOpNoSupport
151 }
152 grp := netAddrToIP16(group)
153 if grp == nil {
154 return errMissingAddress
155 }
156 src := netAddrToIP16(source)
157 if src == nil {
158 return errMissingAddress
159 }
160 return so.setSourceGroup(c.Conn, ifi, grp, src)
161}
162
163// LeaveSourceSpecificGroup leaves the source-specific group on the
164// interface ifi.
165func (c *dgramOpt) LeaveSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
166 if !c.ok() {
167 return syscall.EINVAL
168 }
169 so, ok := sockOpts[ssoLeaveSourceGroup]
170 if !ok {
171 return errOpNoSupport
172 }
173 grp := netAddrToIP16(group)
174 if grp == nil {
175 return errMissingAddress
176 }
177 src := netAddrToIP16(source)
178 if src == nil {
179 return errMissingAddress
180 }
181 return so.setSourceGroup(c.Conn, ifi, grp, src)
182}
183
184// ExcludeSourceSpecificGroup excludes the source-specific group from
185// the already joined any-source groups by JoinGroup on the interface
186// ifi.
187func (c *dgramOpt) ExcludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
188 if !c.ok() {
189 return syscall.EINVAL
190 }
191 so, ok := sockOpts[ssoBlockSourceGroup]
192 if !ok {
193 return errOpNoSupport
194 }
195 grp := netAddrToIP16(group)
196 if grp == nil {
197 return errMissingAddress
198 }
199 src := netAddrToIP16(source)
200 if src == nil {
201 return errMissingAddress
202 }
203 return so.setSourceGroup(c.Conn, ifi, grp, src)
204}
205
206// IncludeSourceSpecificGroup includes the excluded source-specific
207// group by ExcludeSourceSpecificGroup again on the interface ifi.
208func (c *dgramOpt) IncludeSourceSpecificGroup(ifi *net.Interface, group, source net.Addr) error {
209 if !c.ok() {
210 return syscall.EINVAL
211 }
212 so, ok := sockOpts[ssoUnblockSourceGroup]
213 if !ok {
214 return errOpNoSupport
215 }
216 grp := netAddrToIP16(group)
217 if grp == nil {
218 return errMissingAddress
219 }
220 src := netAddrToIP16(source)
221 if src == nil {
222 return errMissingAddress
223 }
224 return so.setSourceGroup(c.Conn, ifi, grp, src)
225}
226
227// Checksum reports whether the kernel will compute, store or verify a
228// checksum for both incoming and outgoing packets. If on is true, it
229// returns an offset in bytes into the data of where the checksum
230// field is located.
231func (c *dgramOpt) Checksum() (on bool, offset int, err error) {
232 if !c.ok() {
233 return false, 0, syscall.EINVAL
234 }
235 so, ok := sockOpts[ssoChecksum]
236 if !ok {
237 return false, 0, errOpNoSupport
238 }
239 offset, err = so.GetInt(c.Conn)
240 if err != nil {
241 return false, 0, err
242 }
243 if offset < 0 {
244 return false, 0, nil
245 }
246 return true, offset, nil
247}
248
249// SetChecksum enables the kernel checksum processing. If on is ture,
250// the offset should be an offset in bytes into the data of where the
251// checksum field is located.
252func (c *dgramOpt) SetChecksum(on bool, offset int) error {
253 if !c.ok() {
254 return syscall.EINVAL
255 }
256 so, ok := sockOpts[ssoChecksum]
257 if !ok {
258 return errOpNoSupport
259 }
260 if !on {
261 offset = -1
262 }
263 return so.SetInt(c.Conn, offset)
264}
265
266// ICMPFilter returns an ICMP filter.
267func (c *dgramOpt) ICMPFilter() (*ICMPFilter, error) {
268 if !c.ok() {
269 return nil, syscall.EINVAL
270 }
271 so, ok := sockOpts[ssoICMPFilter]
272 if !ok {
273 return nil, errOpNoSupport
274 }
275 return so.getICMPFilter(c.Conn)
276}
277
278// SetICMPFilter deploys the ICMP filter.
279func (c *dgramOpt) SetICMPFilter(f *ICMPFilter) error {
280 if !c.ok() {
281 return syscall.EINVAL
282 }
283 so, ok := sockOpts[ssoICMPFilter]
284 if !ok {
285 return errOpNoSupport
286 }
287 return so.setICMPFilter(c.Conn, f)
288}
289
290// SetBPF attaches a BPF program to the connection.
291//
292// Only supported on Linux.
293func (c *dgramOpt) SetBPF(filter []bpf.RawInstruction) error {
294 if !c.ok() {
295 return syscall.EINVAL
296 }
297 so, ok := sockOpts[ssoAttachFilter]
298 if !ok {
299 return errOpNoSupport
300 }
301 return so.setBPF(c.Conn, filter)
302}