Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
1da177e4 LT |
2 | /* |
3 | * INET An implementation of the TCP/IP protocol suite for the LINUX | |
4 | * operating system. INET is implemented using the BSD Socket | |
5 | * interface as the means of communication with the user level. | |
6 | * | |
7 | * The IP to API glue. | |
e905a9ed | 8 | * |
1da177e4 LT |
9 | * Authors: see ip.c |
10 | * | |
11 | * Fixes: | |
12 | * Many : Split from ip.c , see ip.c for history. | |
13 | * Martin Mares : TOS setting fixed. | |
e905a9ed | 14 | * Alan Cox : Fixed a couple of oopses in Martin's |
1da177e4 LT |
15 | * TOS tweaks. |
16 | * Mike McLagan : Routing by source | |
17 | */ | |
18 | ||
1da177e4 LT |
19 | #include <linux/module.h> |
20 | #include <linux/types.h> | |
21 | #include <linux/mm.h> | |
1da177e4 LT |
22 | #include <linux/skbuff.h> |
23 | #include <linux/ip.h> | |
24 | #include <linux/icmp.h> | |
14c85021 | 25 | #include <linux/inetdevice.h> |
1da177e4 | 26 | #include <linux/netdevice.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
1da177e4 LT |
28 | #include <net/sock.h> |
29 | #include <net/ip.h> | |
30 | #include <net/icmp.h> | |
d83d8461 | 31 | #include <net/tcp_states.h> |
1da177e4 LT |
32 | #include <linux/udp.h> |
33 | #include <linux/igmp.h> | |
34 | #include <linux/netfilter.h> | |
35 | #include <linux/route.h> | |
36 | #include <linux/mroute.h> | |
2c67e9ac | 37 | #include <net/inet_ecn.h> |
1da177e4 LT |
38 | #include <net/route.h> |
39 | #include <net/xfrm.h> | |
dae50295 | 40 | #include <net/compat.h> |
ad6f939a | 41 | #include <net/checksum.h> |
dfd56b8b | 42 | #if IS_ENABLED(CONFIG_IPV6) |
1da177e4 LT |
43 | #include <net/transp_v6.h> |
44 | #endif | |
35ebf65e | 45 | #include <net/ip_fib.h> |
1da177e4 LT |
46 | |
47 | #include <linux/errqueue.h> | |
7c0f6ba6 | 48 | #include <linux/uaccess.h> |
1da177e4 | 49 | |
d2ba09c1 AS |
50 | #include <linux/bpfilter.h> |
51 | ||
1da177e4 LT |
52 | /* |
53 | * SOL_IP control messages. | |
54 | */ | |
55 | ||
56 | static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb) | |
57 | { | |
d826eb14 | 58 | struct in_pktinfo info = *PKTINFO_SKB_CB(skb); |
1da177e4 | 59 | |
eddc9ec5 | 60 | info.ipi_addr.s_addr = ip_hdr(skb)->daddr; |
1da177e4 LT |
61 | |
62 | put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); | |
63 | } | |
64 | ||
65 | static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb) | |
66 | { | |
eddc9ec5 | 67 | int ttl = ip_hdr(skb)->ttl; |
1da177e4 LT |
68 | put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl); |
69 | } | |
70 | ||
71 | static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb) | |
72 | { | |
eddc9ec5 | 73 | put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos); |
1da177e4 LT |
74 | } |
75 | ||
76 | static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb) | |
77 | { | |
78 | if (IPCB(skb)->opt.optlen == 0) | |
79 | return; | |
80 | ||
eddc9ec5 ACM |
81 | put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen, |
82 | ip_hdr(skb) + 1); | |
1da177e4 LT |
83 | } |
84 | ||
85 | ||
91ed1e66 PA |
86 | static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg, |
87 | struct sk_buff *skb) | |
1da177e4 LT |
88 | { |
89 | unsigned char optbuf[sizeof(struct ip_options) + 40]; | |
5e73ea1a | 90 | struct ip_options *opt = (struct ip_options *)optbuf; |
1da177e4 LT |
91 | |
92 | if (IPCB(skb)->opt.optlen == 0) | |
93 | return; | |
94 | ||
91ed1e66 | 95 | if (ip_options_echo(net, opt, skb)) { |
1da177e4 LT |
96 | msg->msg_flags |= MSG_CTRUNC; |
97 | return; | |
98 | } | |
99 | ip_options_undo(opt); | |
100 | ||
101 | put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data); | |
102 | } | |
103 | ||
70ecc248 WB |
104 | static void ip_cmsg_recv_fragsize(struct msghdr *msg, struct sk_buff *skb) |
105 | { | |
106 | int val; | |
107 | ||
108 | if (IPCB(skb)->frag_max_size == 0) | |
109 | return; | |
110 | ||
111 | val = IPCB(skb)->frag_max_size; | |
112 | put_cmsg(msg, SOL_IP, IP_RECVFRAGSIZE, sizeof(val), &val); | |
113 | } | |
114 | ||
ad6f939a | 115 | static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb, |
10df8e61 | 116 | int tlen, int offset) |
ad6f939a TH |
117 | { |
118 | __wsum csum = skb->csum; | |
119 | ||
120 | if (skb->ip_summed != CHECKSUM_COMPLETE) | |
121 | return; | |
122 | ||
ca4ef457 PA |
123 | if (offset != 0) { |
124 | int tend_off = skb_transport_offset(skb) + tlen; | |
125 | csum = csum_sub(csum, skb_checksum(skb, tend_off, offset, 0)); | |
126 | } | |
ad6f939a TH |
127 | |
128 | put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum); | |
129 | } | |
130 | ||
2c7946a7 CZ |
131 | static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb) |
132 | { | |
133 | char *secdata; | |
dc49c1f9 | 134 | u32 seclen, secid; |
2c7946a7 CZ |
135 | int err; |
136 | ||
dc49c1f9 CZ |
137 | err = security_socket_getpeersec_dgram(NULL, skb, &secid); |
138 | if (err) | |
139 | return; | |
140 | ||
141 | err = security_secid_to_secctx(secid, &secdata, &seclen); | |
2c7946a7 CZ |
142 | if (err) |
143 | return; | |
144 | ||
145 | put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata); | |
dc49c1f9 | 146 | security_release_secctx(secdata, seclen); |
2c7946a7 CZ |
147 | } |
148 | ||
21d1a161 | 149 | static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb) |
e8b2dfe9 | 150 | { |
4a06fa67 | 151 | __be16 _ports[2], *ports; |
e8b2dfe9 | 152 | struct sockaddr_in sin; |
e8b2dfe9 BS |
153 | |
154 | /* All current transport protocols have the port numbers in the | |
155 | * first four bytes of the transport header and this function is | |
156 | * written with this assumption in mind. | |
157 | */ | |
4a06fa67 WB |
158 | ports = skb_header_pointer(skb, skb_transport_offset(skb), |
159 | sizeof(_ports), &_ports); | |
160 | if (!ports) | |
161 | return; | |
e8b2dfe9 BS |
162 | |
163 | sin.sin_family = AF_INET; | |
64199fc0 | 164 | sin.sin_addr.s_addr = ip_hdr(skb)->daddr; |
e8b2dfe9 BS |
165 | sin.sin_port = ports[1]; |
166 | memset(sin.sin_zero, 0, sizeof(sin.sin_zero)); | |
167 | ||
168 | put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin); | |
169 | } | |
1da177e4 | 170 | |
ad959036 PA |
171 | void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk, |
172 | struct sk_buff *skb, int tlen, int offset) | |
1da177e4 | 173 | { |
ad959036 | 174 | struct inet_sock *inet = inet_sk(sk); |
95c96174 | 175 | unsigned int flags = inet->cmsg_flags; |
1da177e4 LT |
176 | |
177 | /* Ordered by supposed usage frequency */ | |
c44d13d6 | 178 | if (flags & IP_CMSG_PKTINFO) { |
1da177e4 | 179 | ip_cmsg_recv_pktinfo(msg, skb); |
1da177e4 | 180 | |
c44d13d6 TH |
181 | flags &= ~IP_CMSG_PKTINFO; |
182 | if (!flags) | |
183 | return; | |
184 | } | |
185 | ||
186 | if (flags & IP_CMSG_TTL) { | |
1da177e4 | 187 | ip_cmsg_recv_ttl(msg, skb); |
1da177e4 | 188 | |
c44d13d6 TH |
189 | flags &= ~IP_CMSG_TTL; |
190 | if (!flags) | |
191 | return; | |
192 | } | |
193 | ||
194 | if (flags & IP_CMSG_TOS) { | |
1da177e4 | 195 | ip_cmsg_recv_tos(msg, skb); |
1da177e4 | 196 | |
c44d13d6 TH |
197 | flags &= ~IP_CMSG_TOS; |
198 | if (!flags) | |
199 | return; | |
200 | } | |
201 | ||
202 | if (flags & IP_CMSG_RECVOPTS) { | |
1da177e4 | 203 | ip_cmsg_recv_opts(msg, skb); |
1da177e4 | 204 | |
c44d13d6 TH |
205 | flags &= ~IP_CMSG_RECVOPTS; |
206 | if (!flags) | |
207 | return; | |
208 | } | |
209 | ||
210 | if (flags & IP_CMSG_RETOPTS) { | |
91ed1e66 | 211 | ip_cmsg_recv_retopts(sock_net(sk), msg, skb); |
2c7946a7 | 212 | |
c44d13d6 TH |
213 | flags &= ~IP_CMSG_RETOPTS; |
214 | if (!flags) | |
215 | return; | |
216 | } | |
217 | ||
218 | if (flags & IP_CMSG_PASSSEC) { | |
2c7946a7 | 219 | ip_cmsg_recv_security(msg, skb); |
e8b2dfe9 | 220 | |
c44d13d6 TH |
221 | flags &= ~IP_CMSG_PASSSEC; |
222 | if (!flags) | |
223 | return; | |
224 | } | |
225 | ||
ad6f939a | 226 | if (flags & IP_CMSG_ORIGDSTADDR) { |
e8b2dfe9 BS |
227 | ip_cmsg_recv_dstaddr(msg, skb); |
228 | ||
ad6f939a TH |
229 | flags &= ~IP_CMSG_ORIGDSTADDR; |
230 | if (!flags) | |
231 | return; | |
232 | } | |
233 | ||
234 | if (flags & IP_CMSG_CHECKSUM) | |
10df8e61 | 235 | ip_cmsg_recv_checksum(msg, skb, tlen, offset); |
70ecc248 WB |
236 | |
237 | if (flags & IP_CMSG_RECVFRAGSIZE) | |
238 | ip_cmsg_recv_fragsize(msg, skb); | |
1da177e4 | 239 | } |
5961de9f | 240 | EXPORT_SYMBOL(ip_cmsg_recv_offset); |
1da177e4 | 241 | |
24025c46 | 242 | int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc, |
c8e6ad08 | 243 | bool allow_ipv6) |
1da177e4 | 244 | { |
f02db315 | 245 | int err, val; |
1da177e4 | 246 | struct cmsghdr *cmsg; |
24025c46 | 247 | struct net *net = sock_net(sk); |
1da177e4 | 248 | |
f95b414e | 249 | for_each_cmsghdr(cmsg, msg) { |
1da177e4 LT |
250 | if (!CMSG_OK(msg, cmsg)) |
251 | return -EINVAL; | |
5337b5b7 | 252 | #if IS_ENABLED(CONFIG_IPV6) |
c8e6ad08 HFS |
253 | if (allow_ipv6 && |
254 | cmsg->cmsg_level == SOL_IPV6 && | |
255 | cmsg->cmsg_type == IPV6_PKTINFO) { | |
256 | struct in6_pktinfo *src_info; | |
257 | ||
258 | if (cmsg->cmsg_len < CMSG_LEN(sizeof(*src_info))) | |
259 | return -EINVAL; | |
260 | src_info = (struct in6_pktinfo *)CMSG_DATA(cmsg); | |
261 | if (!ipv6_addr_v4mapped(&src_info->ipi6_addr)) | |
262 | return -EINVAL; | |
1cbec076 DA |
263 | if (src_info->ipi6_ifindex) |
264 | ipc->oif = src_info->ipi6_ifindex; | |
c8e6ad08 HFS |
265 | ipc->addr = src_info->ipi6_addr.s6_addr32[3]; |
266 | continue; | |
267 | } | |
268 | #endif | |
24025c46 | 269 | if (cmsg->cmsg_level == SOL_SOCKET) { |
233baf9a | 270 | err = __sock_cmsg_send(sk, cmsg, &ipc->sockc); |
2632616b ED |
271 | if (err) |
272 | return err; | |
24025c46 SHY |
273 | continue; |
274 | } | |
275 | ||
1da177e4 LT |
276 | if (cmsg->cmsg_level != SOL_IP) |
277 | continue; | |
278 | switch (cmsg->cmsg_type) { | |
279 | case IP_RETOPTS: | |
1ff8cebf | 280 | err = cmsg->cmsg_len - sizeof(struct cmsghdr); |
91948309 ED |
281 | |
282 | /* Our caller is responsible for freeing ipc->opt */ | |
de40a3e8 CH |
283 | err = ip_options_get(net, &ipc->opt, |
284 | KERNEL_SOCKPTR(CMSG_DATA(cmsg)), | |
4d52cfbe | 285 | err < 40 ? err : 40); |
1da177e4 LT |
286 | if (err) |
287 | return err; | |
288 | break; | |
289 | case IP_PKTINFO: | |
290 | { | |
291 | struct in_pktinfo *info; | |
292 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo))) | |
293 | return -EINVAL; | |
294 | info = (struct in_pktinfo *)CMSG_DATA(cmsg); | |
1cbec076 DA |
295 | if (info->ipi_ifindex) |
296 | ipc->oif = info->ipi_ifindex; | |
1da177e4 LT |
297 | ipc->addr = info->ipi_spec_dst.s_addr; |
298 | break; | |
299 | } | |
f02db315 FF |
300 | case IP_TTL: |
301 | if (cmsg->cmsg_len != CMSG_LEN(sizeof(int))) | |
302 | return -EINVAL; | |
303 | val = *(int *)CMSG_DATA(cmsg); | |
304 | if (val < 1 || val > 255) | |
305 | return -EINVAL; | |
306 | ipc->ttl = val; | |
307 | break; | |
308 | case IP_TOS: | |
e895cdce ED |
309 | if (cmsg->cmsg_len == CMSG_LEN(sizeof(int))) |
310 | val = *(int *)CMSG_DATA(cmsg); | |
311 | else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8))) | |
312 | val = *(u8 *)CMSG_DATA(cmsg); | |
313 | else | |
f02db315 | 314 | return -EINVAL; |
f02db315 FF |
315 | if (val < 0 || val > 255) |
316 | return -EINVAL; | |
317 | ipc->tos = val; | |
318 | ipc->priority = rt_tos2priority(ipc->tos); | |
319 | break; | |
320 | ||
1da177e4 LT |
321 | default: |
322 | return -EINVAL; | |
323 | } | |
324 | } | |
325 | return 0; | |
326 | } | |
327 | ||
592fcb9d | 328 | static void ip_ra_destroy_rcu(struct rcu_head *head) |
66018506 | 329 | { |
592fcb9d ED |
330 | struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu); |
331 | ||
332 | sock_put(ra->saved_sk); | |
333 | kfree(ra); | |
66018506 | 334 | } |
1da177e4 | 335 | |
4d52cfbe ED |
336 | int ip_ra_control(struct sock *sk, unsigned char on, |
337 | void (*destructor)(struct sock *)) | |
1da177e4 | 338 | { |
43a951e9 ED |
339 | struct ip_ra_chain *ra, *new_ra; |
340 | struct ip_ra_chain __rcu **rap; | |
5796ef75 | 341 | struct net *net = sock_net(sk); |
1da177e4 | 342 | |
c720c7e8 | 343 | if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW) |
1da177e4 LT |
344 | return -EINVAL; |
345 | ||
346 | new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL; | |
425aa0e1 GZ |
347 | if (on && !new_ra) |
348 | return -ENOMEM; | |
1da177e4 | 349 | |
d9ff3049 | 350 | mutex_lock(&net->ipv4.ra_mutex); |
5796ef75 | 351 | for (rap = &net->ipv4.ra_chain; |
76d3e153 | 352 | (ra = rcu_dereference_protected(*rap, |
d9ff3049 | 353 | lockdep_is_held(&net->ipv4.ra_mutex))) != NULL; |
43a951e9 | 354 | rap = &ra->next) { |
1da177e4 LT |
355 | if (ra->sk == sk) { |
356 | if (on) { | |
d9ff3049 | 357 | mutex_unlock(&net->ipv4.ra_mutex); |
a51482bd | 358 | kfree(new_ra); |
1da177e4 LT |
359 | return -EADDRINUSE; |
360 | } | |
592fcb9d ED |
361 | /* dont let ip_call_ra_chain() use sk again */ |
362 | ra->sk = NULL; | |
8e380f00 | 363 | RCU_INIT_POINTER(*rap, ra->next); |
d9ff3049 | 364 | mutex_unlock(&net->ipv4.ra_mutex); |
1da177e4 LT |
365 | |
366 | if (ra->destructor) | |
367 | ra->destructor(sk); | |
592fcb9d ED |
368 | /* |
369 | * Delay sock_put(sk) and kfree(ra) after one rcu grace | |
370 | * period. This guarantee ip_call_ra_chain() dont need | |
371 | * to mess with socket refcounts. | |
372 | */ | |
373 | ra->saved_sk = sk; | |
374 | call_rcu(&ra->rcu, ip_ra_destroy_rcu); | |
1da177e4 LT |
375 | return 0; |
376 | } | |
377 | } | |
76d3e153 | 378 | if (!new_ra) { |
d9ff3049 | 379 | mutex_unlock(&net->ipv4.ra_mutex); |
1da177e4 | 380 | return -ENOBUFS; |
76d3e153 | 381 | } |
1da177e4 LT |
382 | new_ra->sk = sk; |
383 | new_ra->destructor = destructor; | |
384 | ||
8e380f00 | 385 | RCU_INIT_POINTER(new_ra->next, ra); |
66018506 | 386 | rcu_assign_pointer(*rap, new_ra); |
1da177e4 | 387 | sock_hold(sk); |
d9ff3049 | 388 | mutex_unlock(&net->ipv4.ra_mutex); |
1da177e4 LT |
389 | |
390 | return 0; | |
391 | } | |
392 | ||
178c49d9 WB |
393 | static void ipv4_icmp_error_rfc4884(const struct sk_buff *skb, |
394 | struct sock_ee_data_rfc4884 *out) | |
395 | { | |
396 | switch (icmp_hdr(skb)->type) { | |
397 | case ICMP_DEST_UNREACH: | |
398 | case ICMP_TIME_EXCEEDED: | |
399 | case ICMP_PARAMETERPROB: | |
400 | ip_icmp_error_rfc4884(skb, out, sizeof(struct icmphdr), | |
401 | icmp_hdr(skb)->un.reserved[1] * 4); | |
402 | } | |
403 | } | |
404 | ||
e905a9ed | 405 | void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err, |
35986b32 | 406 | __be16 port, u32 info, u8 *payload) |
1da177e4 | 407 | { |
1da177e4 LT |
408 | struct sock_exterr_skb *serr; |
409 | ||
1da177e4 LT |
410 | skb = skb_clone(skb, GFP_ATOMIC); |
411 | if (!skb) | |
412 | return; | |
413 | ||
e905a9ed | 414 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
415 | serr->ee.ee_errno = err; |
416 | serr->ee.ee_origin = SO_EE_ORIGIN_ICMP; | |
88c7664f ACM |
417 | serr->ee.ee_type = icmp_hdr(skb)->type; |
418 | serr->ee.ee_code = icmp_hdr(skb)->code; | |
1da177e4 LT |
419 | serr->ee.ee_pad = 0; |
420 | serr->ee.ee_info = info; | |
421 | serr->ee.ee_data = 0; | |
88c7664f | 422 | serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) - |
d56f90a7 | 423 | skb_network_header(skb); |
1da177e4 LT |
424 | serr->port = port; |
425 | ||
00db4124 | 426 | if (skb_pull(skb, payload - skb->data)) { |
eba75c58 | 427 | if (inet_sk(sk)->recverr_rfc4884) |
178c49d9 | 428 | ipv4_icmp_error_rfc4884(skb, &serr->ee.ee_rfc4884); |
eba75c58 | 429 | |
bd82393c ACM |
430 | skb_reset_transport_header(skb); |
431 | if (sock_queue_err_skb(sk, skb) == 0) | |
432 | return; | |
433 | } | |
434 | kfree_skb(skb); | |
1da177e4 | 435 | } |
42fb06b3 | 436 | EXPORT_SYMBOL_GPL(ip_icmp_error); |
1da177e4 | 437 | |
0579016e | 438 | void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info) |
1da177e4 LT |
439 | { |
440 | struct inet_sock *inet = inet_sk(sk); | |
441 | struct sock_exterr_skb *serr; | |
442 | struct iphdr *iph; | |
443 | struct sk_buff *skb; | |
444 | ||
445 | if (!inet->recverr) | |
446 | return; | |
447 | ||
448 | skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC); | |
449 | if (!skb) | |
450 | return; | |
451 | ||
2ca9e6f2 ACM |
452 | skb_put(skb, sizeof(struct iphdr)); |
453 | skb_reset_network_header(skb); | |
eddc9ec5 | 454 | iph = ip_hdr(skb); |
1da177e4 LT |
455 | iph->daddr = daddr; |
456 | ||
e905a9ed | 457 | serr = SKB_EXT_ERR(skb); |
1da177e4 LT |
458 | serr->ee.ee_errno = err; |
459 | serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL; | |
e905a9ed | 460 | serr->ee.ee_type = 0; |
1da177e4 LT |
461 | serr->ee.ee_code = 0; |
462 | serr->ee.ee_pad = 0; | |
463 | serr->ee.ee_info = info; | |
464 | serr->ee.ee_data = 0; | |
d56f90a7 | 465 | serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb); |
1da177e4 LT |
466 | serr->port = port; |
467 | ||
27a884dc | 468 | __skb_pull(skb, skb_tail_pointer(skb) - skb->data); |
bd82393c | 469 | skb_reset_transport_header(skb); |
1da177e4 LT |
470 | |
471 | if (sock_queue_err_skb(sk, skb)) | |
472 | kfree_skb(skb); | |
473 | } | |
474 | ||
34b99df4 JA |
475 | /* For some errors we have valid addr_offset even with zero payload and |
476 | * zero port. Also, addr_offset should be supported if port is set. | |
477 | */ | |
478 | static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr) | |
479 | { | |
480 | return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP || | |
481 | serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port; | |
482 | } | |
483 | ||
c247f053 WB |
484 | /* IPv4 supports cmsg on all imcp errors and some timestamps |
485 | * | |
486 | * Timestamp code paths do not initialize the fields expected by cmsg: | |
487 | * the PKTINFO fields in skb->cb[]. Fill those in here. | |
488 | */ | |
489 | static bool ipv4_datagram_support_cmsg(const struct sock *sk, | |
490 | struct sk_buff *skb, | |
491 | int ee_origin) | |
829ae9d6 | 492 | { |
c247f053 WB |
493 | struct in_pktinfo *info; |
494 | ||
495 | if (ee_origin == SO_EE_ORIGIN_ICMP) | |
496 | return true; | |
829ae9d6 | 497 | |
c247f053 WB |
498 | if (ee_origin == SO_EE_ORIGIN_LOCAL) |
499 | return false; | |
500 | ||
501 | /* Support IP_PKTINFO on tstamp packets if requested, to correlate | |
1862d620 | 502 | * timestamp with egress dev. Not possible for packets without iif |
c247f053 WB |
503 | * or without payload (SOF_TIMESTAMPING_OPT_TSONLY). |
504 | */ | |
1862d620 WB |
505 | info = PKTINFO_SKB_CB(skb); |
506 | if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) || | |
507 | !info->ipi_ifindex) | |
829ae9d6 WB |
508 | return false; |
509 | ||
510 | info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr; | |
829ae9d6 WB |
511 | return true; |
512 | } | |
513 | ||
e905a9ed | 514 | /* |
1da177e4 LT |
515 | * Handle MSG_ERRQUEUE |
516 | */ | |
85fbaa75 | 517 | int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len) |
1da177e4 LT |
518 | { |
519 | struct sock_exterr_skb *serr; | |
364a9e93 | 520 | struct sk_buff *skb; |
342dfc30 | 521 | DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name); |
1da177e4 LT |
522 | struct { |
523 | struct sock_extended_err ee; | |
524 | struct sockaddr_in offender; | |
525 | } errhdr; | |
526 | int err; | |
527 | int copied; | |
528 | ||
529 | err = -EAGAIN; | |
364a9e93 | 530 | skb = sock_dequeue_err_skb(sk); |
51456b29 | 531 | if (!skb) |
1da177e4 LT |
532 | goto out; |
533 | ||
534 | copied = skb->len; | |
535 | if (copied > len) { | |
536 | msg->msg_flags |= MSG_TRUNC; | |
537 | copied = len; | |
538 | } | |
51f3d02b | 539 | err = skb_copy_datagram_msg(skb, 0, msg, copied); |
960a2628 ED |
540 | if (unlikely(err)) { |
541 | kfree_skb(skb); | |
542 | return err; | |
543 | } | |
1da177e4 LT |
544 | sock_recv_timestamp(msg, sk, skb); |
545 | ||
546 | serr = SKB_EXT_ERR(skb); | |
547 | ||
34b99df4 | 548 | if (sin && ipv4_datagram_support_addr(serr)) { |
1da177e4 | 549 | sin->sin_family = AF_INET; |
d56f90a7 ACM |
550 | sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) + |
551 | serr->addr_offset); | |
1da177e4 LT |
552 | sin->sin_port = serr->port; |
553 | memset(&sin->sin_zero, 0, sizeof(sin->sin_zero)); | |
85fbaa75 | 554 | *addr_len = sizeof(*sin); |
1da177e4 LT |
555 | } |
556 | ||
557 | memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err)); | |
558 | sin = &errhdr.offender; | |
f812116b | 559 | memset(sin, 0, sizeof(*sin)); |
829ae9d6 | 560 | |
c247f053 | 561 | if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) { |
1da177e4 | 562 | sin->sin_family = AF_INET; |
eddc9ec5 | 563 | sin->sin_addr.s_addr = ip_hdr(skb)->saddr; |
f812116b | 564 | if (inet_sk(sk)->cmsg_flags) |
1da177e4 LT |
565 | ip_cmsg_recv(msg, skb); |
566 | } | |
567 | ||
568 | put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr); | |
569 | ||
570 | /* Now we could try to dump offended packet options */ | |
571 | ||
572 | msg->msg_flags |= MSG_ERRQUEUE; | |
573 | err = copied; | |
574 | ||
960a2628 | 575 | consume_skb(skb); |
1da177e4 LT |
576 | out: |
577 | return err; | |
578 | } | |
579 | ||
4f47d5d5 | 580 | void __ip_sock_set_tos(struct sock *sk, int val) |
6ebf71ba CH |
581 | { |
582 | if (sk->sk_type == SOCK_STREAM) { | |
583 | val &= ~INET_ECN_MASK; | |
584 | val |= inet_sk(sk)->tos & INET_ECN_MASK; | |
585 | } | |
586 | if (inet_sk(sk)->tos != val) { | |
587 | inet_sk(sk)->tos = val; | |
588 | sk->sk_priority = rt_tos2priority(val); | |
589 | sk_dst_reset(sk); | |
590 | } | |
591 | } | |
592 | ||
593 | void ip_sock_set_tos(struct sock *sk, int val) | |
594 | { | |
595 | lock_sock(sk); | |
596 | __ip_sock_set_tos(sk, val); | |
597 | release_sock(sk); | |
598 | } | |
599 | EXPORT_SYMBOL(ip_sock_set_tos); | |
1da177e4 | 600 | |
c4e446bf CH |
601 | void ip_sock_set_freebind(struct sock *sk) |
602 | { | |
603 | lock_sock(sk); | |
604 | inet_sk(sk)->freebind = true; | |
605 | release_sock(sk); | |
606 | } | |
607 | EXPORT_SYMBOL(ip_sock_set_freebind); | |
608 | ||
db45c0ef CH |
609 | void ip_sock_set_recverr(struct sock *sk) |
610 | { | |
611 | lock_sock(sk); | |
612 | inet_sk(sk)->recverr = true; | |
613 | release_sock(sk); | |
614 | } | |
615 | EXPORT_SYMBOL(ip_sock_set_recverr); | |
616 | ||
2de569bd CH |
617 | int ip_sock_set_mtu_discover(struct sock *sk, int val) |
618 | { | |
619 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) | |
620 | return -EINVAL; | |
621 | lock_sock(sk); | |
622 | inet_sk(sk)->pmtudisc = val; | |
623 | release_sock(sk); | |
624 | return 0; | |
625 | } | |
626 | EXPORT_SYMBOL(ip_sock_set_mtu_discover); | |
627 | ||
c1f9ec57 CH |
628 | void ip_sock_set_pktinfo(struct sock *sk) |
629 | { | |
630 | lock_sock(sk); | |
631 | inet_sk(sk)->cmsg_flags |= IP_CMSG_PKTINFO; | |
632 | release_sock(sk); | |
633 | } | |
634 | EXPORT_SYMBOL(ip_sock_set_pktinfo); | |
635 | ||
1da177e4 | 636 | /* |
4d52cfbe ED |
637 | * Socket option code for IP. This is the end of the line after any |
638 | * TCP,UDP etc options on an IP socket. | |
1da177e4 | 639 | */ |
baf606d9 MRL |
640 | static bool setsockopt_needs_rtnl(int optname) |
641 | { | |
642 | switch (optname) { | |
643 | case IP_ADD_MEMBERSHIP: | |
644 | case IP_ADD_SOURCE_MEMBERSHIP: | |
54ff9ef3 | 645 | case IP_BLOCK_SOURCE: |
baf606d9 | 646 | case IP_DROP_MEMBERSHIP: |
54ff9ef3 MRL |
647 | case IP_DROP_SOURCE_MEMBERSHIP: |
648 | case IP_MSFILTER: | |
649 | case IP_UNBLOCK_SOURCE: | |
650 | case MCAST_BLOCK_SOURCE: | |
651 | case MCAST_MSFILTER: | |
baf606d9 | 652 | case MCAST_JOIN_GROUP: |
54ff9ef3 | 653 | case MCAST_JOIN_SOURCE_GROUP: |
baf606d9 | 654 | case MCAST_LEAVE_GROUP: |
54ff9ef3 MRL |
655 | case MCAST_LEAVE_SOURCE_GROUP: |
656 | case MCAST_UNBLOCK_SOURCE: | |
baf606d9 MRL |
657 | return true; |
658 | } | |
659 | return false; | |
660 | } | |
1da177e4 | 661 | |
e986d4da AV |
662 | static int set_mcast_msfilter(struct sock *sk, int ifindex, |
663 | int numsrc, int fmode, | |
664 | struct sockaddr_storage *group, | |
665 | struct sockaddr_storage *list) | |
666 | { | |
e986d4da AV |
667 | struct ip_msfilter *msf; |
668 | struct sockaddr_in *psin; | |
669 | int err, i; | |
670 | ||
4167a960 | 671 | msf = kmalloc(IP_MSFILTER_SIZE(numsrc), GFP_KERNEL); |
e986d4da AV |
672 | if (!msf) |
673 | return -ENOBUFS; | |
674 | ||
675 | psin = (struct sockaddr_in *)group; | |
676 | if (psin->sin_family != AF_INET) | |
677 | goto Eaddrnotavail; | |
678 | msf->imsf_multiaddr = psin->sin_addr.s_addr; | |
679 | msf->imsf_interface = 0; | |
680 | msf->imsf_fmode = fmode; | |
681 | msf->imsf_numsrc = numsrc; | |
682 | for (i = 0; i < numsrc; ++i) { | |
683 | psin = (struct sockaddr_in *)&list[i]; | |
684 | ||
685 | if (psin->sin_family != AF_INET) | |
686 | goto Eaddrnotavail; | |
2d3e5caf | 687 | msf->imsf_slist_flex[i] = psin->sin_addr.s_addr; |
e986d4da AV |
688 | } |
689 | err = ip_mc_msfilter(sk, msf, ifindex); | |
690 | kfree(msf); | |
691 | return err; | |
692 | ||
693 | Eaddrnotavail: | |
694 | kfree(msf); | |
695 | return -EADDRNOTAVAIL; | |
696 | } | |
697 | ||
89654c5f CH |
698 | static int copy_group_source_from_sockptr(struct group_source_req *greqs, |
699 | sockptr_t optval, int optlen) | |
b6238c04 CH |
700 | { |
701 | if (in_compat_syscall()) { | |
702 | struct compat_group_source_req gr32; | |
703 | ||
704 | if (optlen != sizeof(gr32)) | |
705 | return -EINVAL; | |
89654c5f | 706 | if (copy_from_sockptr(&gr32, optval, sizeof(gr32))) |
b6238c04 CH |
707 | return -EFAULT; |
708 | greqs->gsr_interface = gr32.gsr_interface; | |
709 | greqs->gsr_group = gr32.gsr_group; | |
710 | greqs->gsr_source = gr32.gsr_source; | |
711 | } else { | |
712 | if (optlen != sizeof(*greqs)) | |
713 | return -EINVAL; | |
89654c5f | 714 | if (copy_from_sockptr(greqs, optval, sizeof(*greqs))) |
b6238c04 CH |
715 | return -EFAULT; |
716 | } | |
717 | ||
718 | return 0; | |
719 | } | |
720 | ||
2bbf8c1e | 721 | static int do_mcast_group_source(struct sock *sk, int optname, |
89654c5f | 722 | sockptr_t optval, int optlen) |
2bbf8c1e | 723 | { |
b6238c04 | 724 | struct group_source_req greqs; |
2bbf8c1e AV |
725 | struct ip_mreq_source mreqs; |
726 | struct sockaddr_in *psin; | |
727 | int omode, add, err; | |
728 | ||
89654c5f | 729 | err = copy_group_source_from_sockptr(&greqs, optval, optlen); |
b6238c04 CH |
730 | if (err) |
731 | return err; | |
732 | ||
733 | if (greqs.gsr_group.ss_family != AF_INET || | |
734 | greqs.gsr_source.ss_family != AF_INET) | |
2bbf8c1e AV |
735 | return -EADDRNOTAVAIL; |
736 | ||
b6238c04 | 737 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
2bbf8c1e | 738 | mreqs.imr_multiaddr = psin->sin_addr.s_addr; |
b6238c04 | 739 | psin = (struct sockaddr_in *)&greqs.gsr_source; |
2bbf8c1e AV |
740 | mreqs.imr_sourceaddr = psin->sin_addr.s_addr; |
741 | mreqs.imr_interface = 0; /* use index for mc_source */ | |
742 | ||
743 | if (optname == MCAST_BLOCK_SOURCE) { | |
744 | omode = MCAST_EXCLUDE; | |
745 | add = 1; | |
746 | } else if (optname == MCAST_UNBLOCK_SOURCE) { | |
747 | omode = MCAST_EXCLUDE; | |
748 | add = 0; | |
749 | } else if (optname == MCAST_JOIN_SOURCE_GROUP) { | |
750 | struct ip_mreqn mreq; | |
751 | ||
b6238c04 | 752 | psin = (struct sockaddr_in *)&greqs.gsr_group; |
2bbf8c1e AV |
753 | mreq.imr_multiaddr = psin->sin_addr; |
754 | mreq.imr_address.s_addr = 0; | |
b6238c04 | 755 | mreq.imr_ifindex = greqs.gsr_interface; |
2bbf8c1e AV |
756 | err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); |
757 | if (err && err != -EADDRINUSE) | |
758 | return err; | |
b6238c04 | 759 | greqs.gsr_interface = mreq.imr_ifindex; |
2bbf8c1e AV |
760 | omode = MCAST_INCLUDE; |
761 | add = 1; | |
762 | } else /* MCAST_LEAVE_SOURCE_GROUP */ { | |
763 | omode = MCAST_INCLUDE; | |
764 | add = 0; | |
765 | } | |
b6238c04 | 766 | return ip_mc_source(add, omode, sk, &mreqs, greqs.gsr_interface); |
2bbf8c1e AV |
767 | } |
768 | ||
89654c5f | 769 | static int ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, int optlen) |
d62c38f6 CH |
770 | { |
771 | struct group_filter *gsf = NULL; | |
772 | int err; | |
773 | ||
774 | if (optlen < GROUP_FILTER_SIZE(0)) | |
775 | return -EINVAL; | |
7de6d09f | 776 | if (optlen > READ_ONCE(sysctl_optmem_max)) |
d62c38f6 CH |
777 | return -ENOBUFS; |
778 | ||
89654c5f | 779 | gsf = memdup_sockptr(optval, optlen); |
d62c38f6 CH |
780 | if (IS_ERR(gsf)) |
781 | return PTR_ERR(gsf); | |
782 | ||
783 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ | |
784 | err = -ENOBUFS; | |
785 | if (gsf->gf_numsrc >= 0x1ffffff || | |
6ae0f2e5 | 786 | gsf->gf_numsrc > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf)) |
d62c38f6 CH |
787 | goto out_free_gsf; |
788 | ||
789 | err = -EINVAL; | |
790 | if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) | |
791 | goto out_free_gsf; | |
792 | ||
793 | err = set_mcast_msfilter(sk, gsf->gf_interface, gsf->gf_numsrc, | |
db243b79 GS |
794 | gsf->gf_fmode, &gsf->gf_group, |
795 | gsf->gf_slist_flex); | |
d62c38f6 CH |
796 | out_free_gsf: |
797 | kfree(gsf); | |
798 | return err; | |
799 | } | |
800 | ||
89654c5f | 801 | static int compat_ip_set_mcast_msfilter(struct sock *sk, sockptr_t optval, |
d62c38f6 CH |
802 | int optlen) |
803 | { | |
db243b79 | 804 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
d62c38f6 CH |
805 | struct compat_group_filter *gf32; |
806 | unsigned int n; | |
807 | void *p; | |
808 | int err; | |
809 | ||
810 | if (optlen < size0) | |
811 | return -EINVAL; | |
7de6d09f | 812 | if (optlen > READ_ONCE(sysctl_optmem_max) - 4) |
d62c38f6 CH |
813 | return -ENOBUFS; |
814 | ||
815 | p = kmalloc(optlen + 4, GFP_KERNEL); | |
816 | if (!p) | |
817 | return -ENOMEM; | |
db243b79 | 818 | gf32 = p + 4; /* we want ->gf_group and ->gf_slist_flex aligned */ |
d62c38f6 CH |
819 | |
820 | err = -EFAULT; | |
89654c5f | 821 | if (copy_from_sockptr(gf32, optval, optlen)) |
d62c38f6 CH |
822 | goto out_free_gsf; |
823 | ||
824 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ | |
825 | n = gf32->gf_numsrc; | |
826 | err = -ENOBUFS; | |
827 | if (n >= 0x1ffffff) | |
828 | goto out_free_gsf; | |
829 | ||
830 | err = -EINVAL; | |
db243b79 | 831 | if (offsetof(struct compat_group_filter, gf_slist_flex[n]) > optlen) |
d62c38f6 CH |
832 | goto out_free_gsf; |
833 | ||
d62c38f6 CH |
834 | /* numsrc >= (4G-140)/128 overflow in 32 bits */ |
835 | err = -ENOBUFS; | |
6ae0f2e5 | 836 | if (n > READ_ONCE(sock_net(sk)->ipv4.sysctl_igmp_max_msf)) |
b6238c04 | 837 | goto out_free_gsf; |
d62c38f6 | 838 | err = set_mcast_msfilter(sk, gf32->gf_interface, n, gf32->gf_fmode, |
db243b79 | 839 | &gf32->gf_group, gf32->gf_slist_flex); |
d62c38f6 CH |
840 | out_free_gsf: |
841 | kfree(p); | |
842 | return err; | |
843 | } | |
d62c38f6 | 844 | |
02caad7c | 845 | static int ip_mcast_join_leave(struct sock *sk, int optname, |
89654c5f | 846 | sockptr_t optval, int optlen) |
02caad7c CH |
847 | { |
848 | struct ip_mreqn mreq = { }; | |
849 | struct sockaddr_in *psin; | |
850 | struct group_req greq; | |
851 | ||
852 | if (optlen < sizeof(struct group_req)) | |
853 | return -EINVAL; | |
89654c5f | 854 | if (copy_from_sockptr(&greq, optval, sizeof(greq))) |
02caad7c CH |
855 | return -EFAULT; |
856 | ||
857 | psin = (struct sockaddr_in *)&greq.gr_group; | |
858 | if (psin->sin_family != AF_INET) | |
859 | return -EINVAL; | |
860 | mreq.imr_multiaddr = psin->sin_addr; | |
861 | mreq.imr_ifindex = greq.gr_interface; | |
862 | if (optname == MCAST_JOIN_GROUP) | |
863 | return ip_mc_join_group(sk, &mreq); | |
864 | return ip_mc_leave_group(sk, &mreq); | |
865 | } | |
866 | ||
02caad7c | 867 | static int compat_ip_mcast_join_leave(struct sock *sk, int optname, |
89654c5f | 868 | sockptr_t optval, int optlen) |
02caad7c CH |
869 | { |
870 | struct compat_group_req greq; | |
871 | struct ip_mreqn mreq = { }; | |
872 | struct sockaddr_in *psin; | |
02caad7c CH |
873 | |
874 | if (optlen < sizeof(struct compat_group_req)) | |
875 | return -EINVAL; | |
89654c5f | 876 | if (copy_from_sockptr(&greq, optval, sizeof(greq))) |
02caad7c CH |
877 | return -EFAULT; |
878 | ||
879 | psin = (struct sockaddr_in *)&greq.gr_group; | |
880 | if (psin->sin_family != AF_INET) | |
881 | return -EINVAL; | |
882 | mreq.imr_multiaddr = psin->sin_addr; | |
883 | mreq.imr_ifindex = greq.gr_interface; | |
884 | ||
02caad7c | 885 | if (optname == MCAST_JOIN_GROUP) |
b6238c04 CH |
886 | return ip_mc_join_group(sk, &mreq); |
887 | return ip_mc_leave_group(sk, &mreq); | |
02caad7c | 888 | } |
02caad7c | 889 | |
020e71a3 ED |
890 | DEFINE_STATIC_KEY_FALSE(ip4_min_ttl); |
891 | ||
ee7f1e13 MKL |
892 | int do_ip_setsockopt(struct sock *sk, int level, int optname, |
893 | sockptr_t optval, unsigned int optlen) | |
1da177e4 LT |
894 | { |
895 | struct inet_sock *inet = inet_sk(sk); | |
166b6b2d | 896 | struct net *net = sock_net(sk); |
09cb105e | 897 | int val = 0, err; |
baf606d9 | 898 | bool needs_rtnl = setsockopt_needs_rtnl(optname); |
1da177e4 | 899 | |
0c9f79be XW |
900 | switch (optname) { |
901 | case IP_PKTINFO: | |
902 | case IP_RECVTTL: | |
903 | case IP_RECVOPTS: | |
904 | case IP_RECVTOS: | |
905 | case IP_RETOPTS: | |
906 | case IP_TOS: | |
907 | case IP_TTL: | |
908 | case IP_HDRINCL: | |
909 | case IP_MTU_DISCOVER: | |
910 | case IP_RECVERR: | |
911 | case IP_ROUTER_ALERT: | |
912 | case IP_FREEBIND: | |
913 | case IP_PASSSEC: | |
914 | case IP_TRANSPARENT: | |
915 | case IP_MINTTL: | |
916 | case IP_NODEFRAG: | |
90c337da | 917 | case IP_BIND_ADDRESS_NO_PORT: |
0c9f79be XW |
918 | case IP_UNICAST_IF: |
919 | case IP_MULTICAST_TTL: | |
920 | case IP_MULTICAST_ALL: | |
921 | case IP_MULTICAST_LOOP: | |
922 | case IP_RECVORIGDSTADDR: | |
ad6f939a | 923 | case IP_CHECKSUM: |
70ecc248 | 924 | case IP_RECVFRAGSIZE: |
eba75c58 | 925 | case IP_RECVERR_RFC4884: |
1da177e4 | 926 | if (optlen >= sizeof(int)) { |
89654c5f | 927 | if (copy_from_sockptr(&val, optval, sizeof(val))) |
1da177e4 LT |
928 | return -EFAULT; |
929 | } else if (optlen >= sizeof(char)) { | |
930 | unsigned char ucval; | |
931 | ||
89654c5f | 932 | if (copy_from_sockptr(&ucval, optval, sizeof(ucval))) |
1da177e4 LT |
933 | return -EFAULT; |
934 | val = (int) ucval; | |
935 | } | |
936 | } | |
937 | ||
938 | /* If optlen==0, it is equivalent to val == 0 */ | |
939 | ||
0526947f KT |
940 | if (optname == IP_ROUTER_ALERT) |
941 | return ip_ra_control(sk, val ? 1 : 0, NULL); | |
6a9fb947 | 942 | if (ip_mroute_opt(optname)) |
89654c5f | 943 | return ip_mroute_setsockopt(sk, optname, optval, optlen); |
1da177e4 LT |
944 | |
945 | err = 0; | |
baf606d9 MRL |
946 | if (needs_rtnl) |
947 | rtnl_lock(); | |
1df055d3 | 948 | sockopt_lock_sock(sk); |
1da177e4 LT |
949 | |
950 | switch (optname) { | |
132adf54 SH |
951 | case IP_OPTIONS: |
952 | { | |
f6d8bd05 ED |
953 | struct ip_options_rcu *old, *opt = NULL; |
954 | ||
65a1c4ff | 955 | if (optlen > 40) |
132adf54 | 956 | goto e_inval; |
89654c5f | 957 | err = ip_options_get(sock_net(sk), &opt, optval, optlen); |
132adf54 SH |
958 | if (err) |
959 | break; | |
f6d8bd05 | 960 | old = rcu_dereference_protected(inet->inet_opt, |
1e1d04e6 | 961 | lockdep_sock_is_held(sk)); |
132adf54 SH |
962 | if (inet->is_icsk) { |
963 | struct inet_connection_sock *icsk = inet_csk(sk); | |
dfd56b8b | 964 | #if IS_ENABLED(CONFIG_IPV6) |
132adf54 SH |
965 | if (sk->sk_family == PF_INET || |
966 | (!((1 << sk->sk_state) & | |
967 | (TCPF_LISTEN | TCPF_CLOSE)) && | |
c720c7e8 | 968 | inet->inet_daddr != LOOPBACK4_IPV6)) { |
1da177e4 | 969 | #endif |
f6d8bd05 ED |
970 | if (old) |
971 | icsk->icsk_ext_hdr_len -= old->opt.optlen; | |
132adf54 | 972 | if (opt) |
f6d8bd05 | 973 | icsk->icsk_ext_hdr_len += opt->opt.optlen; |
132adf54 | 974 | icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie); |
dfd56b8b | 975 | #if IS_ENABLED(CONFIG_IPV6) |
1da177e4 | 976 | } |
132adf54 | 977 | #endif |
1da177e4 | 978 | } |
f6d8bd05 ED |
979 | rcu_assign_pointer(inet->inet_opt, opt); |
980 | if (old) | |
605b4afe | 981 | kfree_rcu(old, rcu); |
132adf54 SH |
982 | break; |
983 | } | |
984 | case IP_PKTINFO: | |
985 | if (val) | |
986 | inet->cmsg_flags |= IP_CMSG_PKTINFO; | |
987 | else | |
988 | inet->cmsg_flags &= ~IP_CMSG_PKTINFO; | |
989 | break; | |
990 | case IP_RECVTTL: | |
991 | if (val) | |
992 | inet->cmsg_flags |= IP_CMSG_TTL; | |
993 | else | |
994 | inet->cmsg_flags &= ~IP_CMSG_TTL; | |
995 | break; | |
996 | case IP_RECVTOS: | |
997 | if (val) | |
998 | inet->cmsg_flags |= IP_CMSG_TOS; | |
999 | else | |
1000 | inet->cmsg_flags &= ~IP_CMSG_TOS; | |
1001 | break; | |
1002 | case IP_RECVOPTS: | |
1003 | if (val) | |
1004 | inet->cmsg_flags |= IP_CMSG_RECVOPTS; | |
1005 | else | |
1006 | inet->cmsg_flags &= ~IP_CMSG_RECVOPTS; | |
1007 | break; | |
1008 | case IP_RETOPTS: | |
1009 | if (val) | |
1010 | inet->cmsg_flags |= IP_CMSG_RETOPTS; | |
1011 | else | |
1012 | inet->cmsg_flags &= ~IP_CMSG_RETOPTS; | |
1013 | break; | |
1014 | case IP_PASSSEC: | |
1015 | if (val) | |
1016 | inet->cmsg_flags |= IP_CMSG_PASSSEC; | |
1017 | else | |
1018 | inet->cmsg_flags &= ~IP_CMSG_PASSSEC; | |
1019 | break; | |
e8b2dfe9 BS |
1020 | case IP_RECVORIGDSTADDR: |
1021 | if (val) | |
1022 | inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR; | |
1023 | else | |
1024 | inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR; | |
1025 | break; | |
ad6f939a TH |
1026 | case IP_CHECKSUM: |
1027 | if (val) { | |
1028 | if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) { | |
1029 | inet_inc_convert_csum(sk); | |
1030 | inet->cmsg_flags |= IP_CMSG_CHECKSUM; | |
1031 | } | |
1032 | } else { | |
1033 | if (inet->cmsg_flags & IP_CMSG_CHECKSUM) { | |
1034 | inet_dec_convert_csum(sk); | |
1035 | inet->cmsg_flags &= ~IP_CMSG_CHECKSUM; | |
1036 | } | |
1037 | } | |
1038 | break; | |
70ecc248 WB |
1039 | case IP_RECVFRAGSIZE: |
1040 | if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM) | |
1041 | goto e_inval; | |
1042 | if (val) | |
1043 | inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE; | |
1044 | else | |
1045 | inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE; | |
1046 | break; | |
132adf54 | 1047 | case IP_TOS: /* This sets both TOS and Precedence */ |
6ebf71ba | 1048 | __ip_sock_set_tos(sk, val); |
132adf54 SH |
1049 | break; |
1050 | case IP_TTL: | |
4d52cfbe | 1051 | if (optlen < 1) |
132adf54 | 1052 | goto e_inval; |
c9be4a5c | 1053 | if (val != -1 && (val < 1 || val > 255)) |
132adf54 SH |
1054 | goto e_inval; |
1055 | inet->uc_ttl = val; | |
1056 | break; | |
1057 | case IP_HDRINCL: | |
1058 | if (sk->sk_type != SOCK_RAW) { | |
1059 | err = -ENOPROTOOPT; | |
2c7946a7 | 1060 | break; |
132adf54 SH |
1061 | } |
1062 | inet->hdrincl = val ? 1 : 0; | |
1063 | break; | |
7b2ff18e JO |
1064 | case IP_NODEFRAG: |
1065 | if (sk->sk_type != SOCK_RAW) { | |
1066 | err = -ENOPROTOOPT; | |
1067 | break; | |
1068 | } | |
1069 | inet->nodefrag = val ? 1 : 0; | |
1070 | break; | |
90c337da ED |
1071 | case IP_BIND_ADDRESS_NO_PORT: |
1072 | inet->bind_address_no_port = val ? 1 : 0; | |
1073 | break; | |
132adf54 | 1074 | case IP_MTU_DISCOVER: |
1b346576 | 1075 | if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT) |
132adf54 SH |
1076 | goto e_inval; |
1077 | inet->pmtudisc = val; | |
1078 | break; | |
1079 | case IP_RECVERR: | |
1080 | inet->recverr = !!val; | |
1081 | if (!val) | |
1082 | skb_queue_purge(&sk->sk_error_queue); | |
1083 | break; | |
eba75c58 WB |
1084 | case IP_RECVERR_RFC4884: |
1085 | if (val < 0 || val > 1) | |
1086 | goto e_inval; | |
1087 | inet->recverr_rfc4884 = !!val; | |
1088 | break; | |
132adf54 SH |
1089 | case IP_MULTICAST_TTL: |
1090 | if (sk->sk_type == SOCK_STREAM) | |
1091 | goto e_inval; | |
4d52cfbe | 1092 | if (optlen < 1) |
132adf54 | 1093 | goto e_inval; |
09cb105e | 1094 | if (val == -1) |
132adf54 SH |
1095 | val = 1; |
1096 | if (val < 0 || val > 255) | |
1097 | goto e_inval; | |
1098 | inet->mc_ttl = val; | |
1099 | break; | |
1100 | case IP_MULTICAST_LOOP: | |
4d52cfbe | 1101 | if (optlen < 1) |
132adf54 SH |
1102 | goto e_inval; |
1103 | inet->mc_loop = !!val; | |
1104 | break; | |
76e21053 EH |
1105 | case IP_UNICAST_IF: |
1106 | { | |
1107 | struct net_device *dev = NULL; | |
1108 | int ifindex; | |
9515a2e0 | 1109 | int midx; |
76e21053 EH |
1110 | |
1111 | if (optlen != sizeof(int)) | |
1112 | goto e_inval; | |
1113 | ||
1114 | ifindex = (__force int)ntohl((__force __be32)val); | |
1115 | if (ifindex == 0) { | |
1116 | inet->uc_index = 0; | |
1117 | err = 0; | |
1118 | break; | |
1119 | } | |
1120 | ||
1121 | dev = dev_get_by_index(sock_net(sk), ifindex); | |
1122 | err = -EADDRNOTAVAIL; | |
1123 | if (!dev) | |
1124 | break; | |
9515a2e0 DA |
1125 | |
1126 | midx = l3mdev_master_ifindex(dev); | |
76e21053 EH |
1127 | dev_put(dev); |
1128 | ||
1129 | err = -EINVAL; | |
fdf1923b | 1130 | if (sk->sk_bound_dev_if && midx != sk->sk_bound_dev_if) |
76e21053 EH |
1131 | break; |
1132 | ||
1133 | inet->uc_index = ifindex; | |
1134 | err = 0; | |
1135 | break; | |
1136 | } | |
132adf54 SH |
1137 | case IP_MULTICAST_IF: |
1138 | { | |
1139 | struct ip_mreqn mreq; | |
1140 | struct net_device *dev = NULL; | |
7bb387c5 | 1141 | int midx; |
132adf54 SH |
1142 | |
1143 | if (sk->sk_type == SOCK_STREAM) | |
1144 | goto e_inval; | |
1145 | /* | |
1146 | * Check the arguments are allowable | |
1147 | */ | |
1148 | ||
0915921b SW |
1149 | if (optlen < sizeof(struct in_addr)) |
1150 | goto e_inval; | |
1151 | ||
132adf54 SH |
1152 | err = -EFAULT; |
1153 | if (optlen >= sizeof(struct ip_mreqn)) { | |
89654c5f | 1154 | if (copy_from_sockptr(&mreq, optval, sizeof(mreq))) |
1da177e4 | 1155 | break; |
132adf54 SH |
1156 | } else { |
1157 | memset(&mreq, 0, sizeof(mreq)); | |
3a084ddb | 1158 | if (optlen >= sizeof(struct ip_mreq)) { |
89654c5f CH |
1159 | if (copy_from_sockptr(&mreq, optval, |
1160 | sizeof(struct ip_mreq))) | |
3a084ddb JP |
1161 | break; |
1162 | } else if (optlen >= sizeof(struct in_addr)) { | |
89654c5f CH |
1163 | if (copy_from_sockptr(&mreq.imr_address, optval, |
1164 | sizeof(struct in_addr))) | |
3a084ddb JP |
1165 | break; |
1166 | } | |
132adf54 SH |
1167 | } |
1168 | ||
1169 | if (!mreq.imr_ifindex) { | |
e6f1cebf | 1170 | if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) { |
132adf54 SH |
1171 | inet->mc_index = 0; |
1172 | inet->mc_addr = 0; | |
1173 | err = 0; | |
1da177e4 LT |
1174 | break; |
1175 | } | |
3b1e0a65 | 1176 | dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr); |
55b80503 | 1177 | if (dev) |
132adf54 | 1178 | mreq.imr_ifindex = dev->ifindex; |
132adf54 | 1179 | } else |
55b80503 | 1180 | dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex); |
1da177e4 | 1181 | |
1da177e4 | 1182 | |
132adf54 SH |
1183 | err = -EADDRNOTAVAIL; |
1184 | if (!dev) | |
1185 | break; | |
7bb387c5 DA |
1186 | |
1187 | midx = l3mdev_master_ifindex(dev); | |
1188 | ||
55b80503 | 1189 | dev_put(dev); |
132adf54 SH |
1190 | |
1191 | err = -EINVAL; | |
1192 | if (sk->sk_bound_dev_if && | |
7bb387c5 | 1193 | mreq.imr_ifindex != sk->sk_bound_dev_if && |
fdf1923b | 1194 | midx != sk->sk_bound_dev_if) |
132adf54 | 1195 | break; |
1da177e4 | 1196 | |
132adf54 SH |
1197 | inet->mc_index = mreq.imr_ifindex; |
1198 | inet->mc_addr = mreq.imr_address.s_addr; | |
1199 | err = 0; | |
1200 | break; | |
1201 | } | |
1da177e4 | 1202 | |
132adf54 SH |
1203 | case IP_ADD_MEMBERSHIP: |
1204 | case IP_DROP_MEMBERSHIP: | |
1205 | { | |
1206 | struct ip_mreqn mreq; | |
1da177e4 | 1207 | |
a96fb49b FL |
1208 | err = -EPROTO; |
1209 | if (inet_sk(sk)->is_icsk) | |
1210 | break; | |
1211 | ||
132adf54 SH |
1212 | if (optlen < sizeof(struct ip_mreq)) |
1213 | goto e_inval; | |
1214 | err = -EFAULT; | |
1215 | if (optlen >= sizeof(struct ip_mreqn)) { | |
89654c5f | 1216 | if (copy_from_sockptr(&mreq, optval, sizeof(mreq))) |
1da177e4 | 1217 | break; |
132adf54 SH |
1218 | } else { |
1219 | memset(&mreq, 0, sizeof(mreq)); | |
89654c5f CH |
1220 | if (copy_from_sockptr(&mreq, optval, |
1221 | sizeof(struct ip_mreq))) | |
1da177e4 | 1222 | break; |
132adf54 | 1223 | } |
1da177e4 | 1224 | |
132adf54 | 1225 | if (optname == IP_ADD_MEMBERSHIP) |
54ff9ef3 | 1226 | err = ip_mc_join_group(sk, &mreq); |
132adf54 | 1227 | else |
54ff9ef3 | 1228 | err = ip_mc_leave_group(sk, &mreq); |
132adf54 SH |
1229 | break; |
1230 | } | |
1231 | case IP_MSFILTER: | |
1232 | { | |
132adf54 SH |
1233 | struct ip_msfilter *msf; |
1234 | ||
4167a960 | 1235 | if (optlen < IP_MSFILTER_SIZE(0)) |
132adf54 | 1236 | goto e_inval; |
7de6d09f | 1237 | if (optlen > READ_ONCE(sysctl_optmem_max)) { |
132adf54 | 1238 | err = -ENOBUFS; |
1da177e4 LT |
1239 | break; |
1240 | } | |
89654c5f | 1241 | msf = memdup_sockptr(optval, optlen); |
a2c841d9 AV |
1242 | if (IS_ERR(msf)) { |
1243 | err = PTR_ERR(msf); | |
132adf54 SH |
1244 | break; |
1245 | } | |
1246 | /* numsrc >= (1G-4) overflow in 32 bits */ | |
1247 | if (msf->imsf_numsrc >= 0x3ffffffcU || | |
6ae0f2e5 | 1248 | msf->imsf_numsrc > READ_ONCE(net->ipv4.sysctl_igmp_max_msf)) { |
132adf54 SH |
1249 | kfree(msf); |
1250 | err = -ENOBUFS; | |
1251 | break; | |
1252 | } | |
4167a960 | 1253 | if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) { |
132adf54 SH |
1254 | kfree(msf); |
1255 | err = -EINVAL; | |
1256 | break; | |
1257 | } | |
1258 | err = ip_mc_msfilter(sk, msf, 0); | |
1259 | kfree(msf); | |
1260 | break; | |
1261 | } | |
1262 | case IP_BLOCK_SOURCE: | |
1263 | case IP_UNBLOCK_SOURCE: | |
1264 | case IP_ADD_SOURCE_MEMBERSHIP: | |
1265 | case IP_DROP_SOURCE_MEMBERSHIP: | |
1266 | { | |
1267 | struct ip_mreq_source mreqs; | |
1268 | int omode, add; | |
1da177e4 | 1269 | |
132adf54 SH |
1270 | if (optlen != sizeof(struct ip_mreq_source)) |
1271 | goto e_inval; | |
89654c5f | 1272 | if (copy_from_sockptr(&mreqs, optval, sizeof(mreqs))) { |
1da177e4 | 1273 | err = -EFAULT; |
1da177e4 LT |
1274 | break; |
1275 | } | |
132adf54 SH |
1276 | if (optname == IP_BLOCK_SOURCE) { |
1277 | omode = MCAST_EXCLUDE; | |
1278 | add = 1; | |
1279 | } else if (optname == IP_UNBLOCK_SOURCE) { | |
1280 | omode = MCAST_EXCLUDE; | |
1281 | add = 0; | |
1282 | } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) { | |
1283 | struct ip_mreqn mreq; | |
1da177e4 | 1284 | |
132adf54 SH |
1285 | mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr; |
1286 | mreq.imr_address.s_addr = mreqs.imr_interface; | |
1287 | mreq.imr_ifindex = 0; | |
6e2059b5 | 1288 | err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE); |
132adf54 | 1289 | if (err && err != -EADDRINUSE) |
1da177e4 | 1290 | break; |
132adf54 SH |
1291 | omode = MCAST_INCLUDE; |
1292 | add = 1; | |
1293 | } else /* IP_DROP_SOURCE_MEMBERSHIP */ { | |
1294 | omode = MCAST_INCLUDE; | |
1295 | add = 0; | |
1296 | } | |
1297 | err = ip_mc_source(add, omode, sk, &mreqs, 0); | |
1298 | break; | |
1299 | } | |
1300 | case MCAST_JOIN_GROUP: | |
1301 | case MCAST_LEAVE_GROUP: | |
b6238c04 CH |
1302 | if (in_compat_syscall()) |
1303 | err = compat_ip_mcast_join_leave(sk, optname, optval, | |
1304 | optlen); | |
1305 | else | |
1306 | err = ip_mcast_join_leave(sk, optname, optval, optlen); | |
132adf54 | 1307 | break; |
132adf54 SH |
1308 | case MCAST_JOIN_SOURCE_GROUP: |
1309 | case MCAST_LEAVE_SOURCE_GROUP: | |
1310 | case MCAST_BLOCK_SOURCE: | |
1311 | case MCAST_UNBLOCK_SOURCE: | |
b6238c04 | 1312 | err = do_mcast_group_source(sk, optname, optval, optlen); |
132adf54 | 1313 | break; |
132adf54 | 1314 | case MCAST_MSFILTER: |
b6238c04 CH |
1315 | if (in_compat_syscall()) |
1316 | err = compat_ip_set_mcast_msfilter(sk, optval, optlen); | |
1317 | else | |
1318 | err = ip_set_mcast_msfilter(sk, optval, optlen); | |
132adf54 | 1319 | break; |
f771bef9 NS |
1320 | case IP_MULTICAST_ALL: |
1321 | if (optlen < 1) | |
1322 | goto e_inval; | |
1323 | if (val != 0 && val != 1) | |
1324 | goto e_inval; | |
1325 | inet->mc_all = val; | |
1326 | break; | |
132adf54 SH |
1327 | |
1328 | case IP_FREEBIND: | |
4d52cfbe | 1329 | if (optlen < 1) |
132adf54 SH |
1330 | goto e_inval; |
1331 | inet->freebind = !!val; | |
1332 | break; | |
1333 | ||
1334 | case IP_IPSEC_POLICY: | |
1335 | case IP_XFRM_POLICY: | |
1336 | err = -EPERM; | |
1df055d3 | 1337 | if (!sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) |
1da177e4 | 1338 | break; |
89654c5f | 1339 | err = xfrm_user_policy(sk, optname, optval, optlen); |
132adf54 | 1340 | break; |
1da177e4 | 1341 | |
f5715aea | 1342 | case IP_TRANSPARENT: |
1df055d3 MKL |
1343 | if (!!val && !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) && |
1344 | !sockopt_ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) { | |
f5715aea KK |
1345 | err = -EPERM; |
1346 | break; | |
1347 | } | |
1348 | if (optlen < 1) | |
1349 | goto e_inval; | |
1350 | inet->transparent = !!val; | |
1351 | break; | |
1352 | ||
d218d111 SH |
1353 | case IP_MINTTL: |
1354 | if (optlen < 1) | |
1355 | goto e_inval; | |
1356 | if (val < 0 || val > 255) | |
1357 | goto e_inval; | |
020e71a3 ED |
1358 | |
1359 | if (val) | |
1360 | static_branch_enable(&ip4_min_ttl); | |
1361 | ||
14834c4f ED |
1362 | /* tcp_v4_err() and tcp_v4_rcv() might read min_ttl |
1363 | * while we are changint it. | |
1364 | */ | |
1365 | WRITE_ONCE(inet->min_ttl, val); | |
d218d111 SH |
1366 | break; |
1367 | ||
132adf54 SH |
1368 | default: |
1369 | err = -ENOPROTOOPT; | |
1370 | break; | |
1da177e4 | 1371 | } |
1df055d3 | 1372 | sockopt_release_sock(sk); |
baf606d9 MRL |
1373 | if (needs_rtnl) |
1374 | rtnl_unlock(); | |
1da177e4 LT |
1375 | return err; |
1376 | ||
1377 | e_inval: | |
1df055d3 | 1378 | sockopt_release_sock(sk); |
baf606d9 MRL |
1379 | if (needs_rtnl) |
1380 | rtnl_unlock(); | |
1da177e4 LT |
1381 | return -EINVAL; |
1382 | } | |
1383 | ||
f84af32c | 1384 | /** |
829ae9d6 | 1385 | * ipv4_pktinfo_prepare - transfer some info from rtable to skb |
f84af32c ED |
1386 | * @sk: socket |
1387 | * @skb: buffer | |
1388 | * | |
35ebf65e DM |
1389 | * To support IP_CMSG_PKTINFO option, we store rt_iif and specific |
1390 | * destination in skb->cb[] before dst drop. | |
8e3bff96 | 1391 | * This way, receiver doesn't make cache line misses to read rtable. |
f84af32c | 1392 | */ |
fbf8866d | 1393 | void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb) |
f84af32c | 1394 | { |
d826eb14 | 1395 | struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb); |
4b261c75 HFS |
1396 | bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) || |
1397 | ipv6_sk_rxinfo(sk); | |
d826eb14 | 1398 | |
4b261c75 | 1399 | if (prepare && skb_rtable(skb)) { |
0b922b7a DA |
1400 | /* skb->cb is overloaded: prior to this point it is IP{6}CB |
1401 | * which has interface index (iif) as the first member of the | |
1402 | * underlying inet{6}_skb_parm struct. This code then overlays | |
1403 | * PKTINFO_SKB_CB and in_pktinfo also has iif as the first | |
f0c16ba8 WZ |
1404 | * element so the iif is picked up from the prior IPCB. If iif |
1405 | * is the loopback interface, then return the sending interface | |
1406 | * (e.g., process binds socket to eth0 for Tx which is | |
1407 | * redirected to loopback in the rtable/dst). | |
0b922b7a | 1408 | */ |
cbea8f02 DA |
1409 | struct rtable *rt = skb_rtable(skb); |
1410 | bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags); | |
1411 | ||
1412 | if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX) | |
f0c16ba8 | 1413 | pktinfo->ipi_ifindex = inet_iif(skb); |
cbea8f02 DA |
1414 | else if (l3slave && rt && rt->rt_iif) |
1415 | pktinfo->ipi_ifindex = rt->rt_iif; | |
f0c16ba8 | 1416 | |
35ebf65e | 1417 | pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb); |
d826eb14 ED |
1418 | } else { |
1419 | pktinfo->ipi_ifindex = 0; | |
1420 | pktinfo->ipi_spec_dst.s_addr = 0; | |
1421 | } | |
61a1030b | 1422 | skb_dst_drop(skb); |
f84af32c | 1423 | } |
f84af32c | 1424 | |
a7b75c5a CH |
1425 | int ip_setsockopt(struct sock *sk, int level, int optname, sockptr_t optval, |
1426 | unsigned int optlen) | |
3fdadf7d DM |
1427 | { |
1428 | int err; | |
1429 | ||
1430 | if (level != SOL_IP) | |
1431 | return -ENOPROTOOPT; | |
1432 | ||
a7b75c5a | 1433 | err = do_ip_setsockopt(sk, level, optname, optval, optlen); |
97adadda | 1434 | #if IS_ENABLED(CONFIG_BPFILTER_UMH) |
d2ba09c1 AS |
1435 | if (optname >= BPFILTER_IPT_SO_SET_REPLACE && |
1436 | optname < BPFILTER_IPT_SET_MAX) | |
a7b75c5a | 1437 | err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen); |
d2ba09c1 | 1438 | #endif |
3fdadf7d DM |
1439 | #ifdef CONFIG_NETFILTER |
1440 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
1441 | if (err == -ENOPROTOOPT && optname != IP_HDRINCL && | |
6a9fb947 PE |
1442 | optname != IP_IPSEC_POLICY && |
1443 | optname != IP_XFRM_POLICY && | |
3f34cfae | 1444 | !ip_mroute_opt(optname)) |
a7b75c5a | 1445 | err = nf_setsockopt(sk, PF_INET, optname, optval, optlen); |
3fdadf7d DM |
1446 | #endif |
1447 | return err; | |
1448 | } | |
4d52cfbe | 1449 | EXPORT_SYMBOL(ip_setsockopt); |
3fdadf7d | 1450 | |
1da177e4 | 1451 | /* |
4d52cfbe ED |
1452 | * Get the options. Note for future reference. The GET of IP options gets |
1453 | * the _received_ ones. The set sets the _sent_ ones. | |
1da177e4 LT |
1454 | */ |
1455 | ||
87e9f031 WC |
1456 | static bool getsockopt_needs_rtnl(int optname) |
1457 | { | |
1458 | switch (optname) { | |
1459 | case IP_MSFILTER: | |
1460 | case MCAST_MSFILTER: | |
1461 | return true; | |
1462 | } | |
1463 | return false; | |
1464 | } | |
1465 | ||
728f064c MKL |
1466 | static int ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval, |
1467 | sockptr_t optlen, int len) | |
49e74c24 | 1468 | { |
db243b79 | 1469 | const int size0 = offsetof(struct group_filter, gf_slist_flex); |
49e74c24 | 1470 | struct group_filter gsf; |
728f064c | 1471 | int num, gsf_size; |
49e74c24 CH |
1472 | int err; |
1473 | ||
1474 | if (len < size0) | |
1475 | return -EINVAL; | |
728f064c | 1476 | if (copy_from_sockptr(&gsf, optval, size0)) |
49e74c24 CH |
1477 | return -EFAULT; |
1478 | ||
1479 | num = gsf.gf_numsrc; | |
728f064c MKL |
1480 | err = ip_mc_gsfget(sk, &gsf, optval, |
1481 | offsetof(struct group_filter, gf_slist_flex)); | |
49e74c24 CH |
1482 | if (err) |
1483 | return err; | |
1484 | if (gsf.gf_numsrc < num) | |
1485 | num = gsf.gf_numsrc; | |
728f064c MKL |
1486 | gsf_size = GROUP_FILTER_SIZE(num); |
1487 | if (copy_to_sockptr(optlen, &gsf_size, sizeof(int)) || | |
1488 | copy_to_sockptr(optval, &gsf, size0)) | |
49e74c24 CH |
1489 | return -EFAULT; |
1490 | return 0; | |
1491 | } | |
1492 | ||
728f064c MKL |
1493 | static int compat_ip_get_mcast_msfilter(struct sock *sk, sockptr_t optval, |
1494 | sockptr_t optlen, int len) | |
49e74c24 | 1495 | { |
db243b79 | 1496 | const int size0 = offsetof(struct compat_group_filter, gf_slist_flex); |
49e74c24 CH |
1497 | struct compat_group_filter gf32; |
1498 | struct group_filter gf; | |
49e74c24 | 1499 | int num; |
b6238c04 | 1500 | int err; |
49e74c24 | 1501 | |
49e74c24 CH |
1502 | if (len < size0) |
1503 | return -EINVAL; | |
728f064c | 1504 | if (copy_from_sockptr(&gf32, optval, size0)) |
49e74c24 CH |
1505 | return -EFAULT; |
1506 | ||
1507 | gf.gf_interface = gf32.gf_interface; | |
1508 | gf.gf_fmode = gf32.gf_fmode; | |
1509 | num = gf.gf_numsrc = gf32.gf_numsrc; | |
1510 | gf.gf_group = gf32.gf_group; | |
1511 | ||
728f064c MKL |
1512 | err = ip_mc_gsfget(sk, &gf, optval, |
1513 | offsetof(struct compat_group_filter, gf_slist_flex)); | |
49e74c24 CH |
1514 | if (err) |
1515 | return err; | |
1516 | if (gf.gf_numsrc < num) | |
1517 | num = gf.gf_numsrc; | |
1518 | len = GROUP_FILTER_SIZE(num) - (sizeof(gf) - sizeof(gf32)); | |
728f064c MKL |
1519 | if (copy_to_sockptr(optlen, &len, sizeof(int)) || |
1520 | copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_fmode), | |
1521 | &gf.gf_fmode, sizeof(gf.gf_fmode)) || | |
1522 | copy_to_sockptr_offset(optval, offsetof(struct compat_group_filter, gf_numsrc), | |
1523 | &gf.gf_numsrc, sizeof(gf.gf_numsrc))) | |
49e74c24 CH |
1524 | return -EFAULT; |
1525 | return 0; | |
1526 | } | |
49e74c24 | 1527 | |
fd969f25 MKL |
1528 | int do_ip_getsockopt(struct sock *sk, int level, int optname, |
1529 | sockptr_t optval, sockptr_t optlen) | |
1da177e4 LT |
1530 | { |
1531 | struct inet_sock *inet = inet_sk(sk); | |
87e9f031 WC |
1532 | bool needs_rtnl = getsockopt_needs_rtnl(optname); |
1533 | int val, err = 0; | |
1da177e4 | 1534 | int len; |
e905a9ed | 1535 | |
132adf54 | 1536 | if (level != SOL_IP) |
1da177e4 LT |
1537 | return -EOPNOTSUPP; |
1538 | ||
6a9fb947 | 1539 | if (ip_mroute_opt(optname)) |
09cb105e | 1540 | return ip_mroute_getsockopt(sk, optname, optval, optlen); |
1da177e4 | 1541 | |
728f064c | 1542 | if (copy_from_sockptr(&len, optlen, sizeof(int))) |
1da177e4 | 1543 | return -EFAULT; |
132adf54 | 1544 | if (len < 0) |
1da177e4 | 1545 | return -EINVAL; |
e905a9ed | 1546 | |
87e9f031 WC |
1547 | if (needs_rtnl) |
1548 | rtnl_lock(); | |
1985320c | 1549 | sockopt_lock_sock(sk); |
1da177e4 | 1550 | |
132adf54 SH |
1551 | switch (optname) { |
1552 | case IP_OPTIONS: | |
1553 | { | |
1554 | unsigned char optbuf[sizeof(struct ip_options)+40]; | |
f6d8bd05 ED |
1555 | struct ip_options *opt = (struct ip_options *)optbuf; |
1556 | struct ip_options_rcu *inet_opt; | |
1557 | ||
1558 | inet_opt = rcu_dereference_protected(inet->inet_opt, | |
1e1d04e6 | 1559 | lockdep_sock_is_held(sk)); |
132adf54 | 1560 | opt->optlen = 0; |
f6d8bd05 ED |
1561 | if (inet_opt) |
1562 | memcpy(optbuf, &inet_opt->opt, | |
1563 | sizeof(struct ip_options) + | |
1564 | inet_opt->opt.optlen); | |
1985320c | 1565 | sockopt_release_sock(sk); |
132adf54 | 1566 | |
728f064c MKL |
1567 | if (opt->optlen == 0) { |
1568 | len = 0; | |
1569 | return copy_to_sockptr(optlen, &len, sizeof(int)); | |
1570 | } | |
132adf54 SH |
1571 | |
1572 | ip_options_undo(opt); | |
1573 | ||
1574 | len = min_t(unsigned int, len, opt->optlen); | |
728f064c | 1575 | if (copy_to_sockptr(optlen, &len, sizeof(int))) |
132adf54 | 1576 | return -EFAULT; |
728f064c | 1577 | if (copy_to_sockptr(optval, opt->__data, len)) |
132adf54 SH |
1578 | return -EFAULT; |
1579 | return 0; | |
1580 | } | |
1581 | case IP_PKTINFO: | |
1582 | val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0; | |
1583 | break; | |
1584 | case IP_RECVTTL: | |
1585 | val = (inet->cmsg_flags & IP_CMSG_TTL) != 0; | |
1586 | break; | |
1587 | case IP_RECVTOS: | |
1588 | val = (inet->cmsg_flags & IP_CMSG_TOS) != 0; | |
1589 | break; | |
1590 | case IP_RECVOPTS: | |
1591 | val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0; | |
1592 | break; | |
1593 | case IP_RETOPTS: | |
1594 | val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0; | |
1595 | break; | |
1596 | case IP_PASSSEC: | |
1597 | val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0; | |
1598 | break; | |
e8b2dfe9 BS |
1599 | case IP_RECVORIGDSTADDR: |
1600 | val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0; | |
1601 | break; | |
ad6f939a TH |
1602 | case IP_CHECKSUM: |
1603 | val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0; | |
1604 | break; | |
70ecc248 WB |
1605 | case IP_RECVFRAGSIZE: |
1606 | val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0; | |
1607 | break; | |
132adf54 SH |
1608 | case IP_TOS: |
1609 | val = inet->tos; | |
1610 | break; | |
1611 | case IP_TTL: | |
fa50d974 NB |
1612 | { |
1613 | struct net *net = sock_net(sk); | |
132adf54 | 1614 | val = (inet->uc_ttl == -1 ? |
8281b7ec | 1615 | READ_ONCE(net->ipv4.sysctl_ip_default_ttl) : |
132adf54 SH |
1616 | inet->uc_ttl); |
1617 | break; | |
fa50d974 | 1618 | } |
132adf54 SH |
1619 | case IP_HDRINCL: |
1620 | val = inet->hdrincl; | |
1621 | break; | |
a89b4763 MK |
1622 | case IP_NODEFRAG: |
1623 | val = inet->nodefrag; | |
1624 | break; | |
90c337da ED |
1625 | case IP_BIND_ADDRESS_NO_PORT: |
1626 | val = inet->bind_address_no_port; | |
1627 | break; | |
132adf54 SH |
1628 | case IP_MTU_DISCOVER: |
1629 | val = inet->pmtudisc; | |
1630 | break; | |
1631 | case IP_MTU: | |
1632 | { | |
1633 | struct dst_entry *dst; | |
1634 | val = 0; | |
1635 | dst = sk_dst_get(sk); | |
1636 | if (dst) { | |
1637 | val = dst_mtu(dst); | |
1638 | dst_release(dst); | |
1da177e4 | 1639 | } |
132adf54 | 1640 | if (!val) { |
1985320c | 1641 | sockopt_release_sock(sk); |
132adf54 | 1642 | return -ENOTCONN; |
1da177e4 | 1643 | } |
132adf54 SH |
1644 | break; |
1645 | } | |
1646 | case IP_RECVERR: | |
1647 | val = inet->recverr; | |
1648 | break; | |
eba75c58 WB |
1649 | case IP_RECVERR_RFC4884: |
1650 | val = inet->recverr_rfc4884; | |
1651 | break; | |
132adf54 SH |
1652 | case IP_MULTICAST_TTL: |
1653 | val = inet->mc_ttl; | |
1654 | break; | |
1655 | case IP_MULTICAST_LOOP: | |
1656 | val = inet->mc_loop; | |
1657 | break; | |
76e21053 EH |
1658 | case IP_UNICAST_IF: |
1659 | val = (__force int)htonl((__u32) inet->uc_index); | |
1660 | break; | |
132adf54 SH |
1661 | case IP_MULTICAST_IF: |
1662 | { | |
1663 | struct in_addr addr; | |
1664 | len = min_t(unsigned int, len, sizeof(struct in_addr)); | |
1665 | addr.s_addr = inet->mc_addr; | |
1985320c | 1666 | sockopt_release_sock(sk); |
1da177e4 | 1667 | |
728f064c | 1668 | if (copy_to_sockptr(optlen, &len, sizeof(int))) |
132adf54 | 1669 | return -EFAULT; |
728f064c | 1670 | if (copy_to_sockptr(optval, &addr, len)) |
132adf54 SH |
1671 | return -EFAULT; |
1672 | return 0; | |
1673 | } | |
1674 | case IP_MSFILTER: | |
1675 | { | |
1676 | struct ip_msfilter msf; | |
132adf54 | 1677 | |
4167a960 | 1678 | if (len < IP_MSFILTER_SIZE(0)) { |
87e9f031 WC |
1679 | err = -EINVAL; |
1680 | goto out; | |
1da177e4 | 1681 | } |
728f064c | 1682 | if (copy_from_sockptr(&msf, optval, IP_MSFILTER_SIZE(0))) { |
87e9f031 WC |
1683 | err = -EFAULT; |
1684 | goto out; | |
1da177e4 | 1685 | } |
728f064c | 1686 | err = ip_mc_msfget(sk, &msf, optval, optlen); |
87e9f031 | 1687 | goto out; |
132adf54 SH |
1688 | } |
1689 | case MCAST_MSFILTER: | |
b6238c04 CH |
1690 | if (in_compat_syscall()) |
1691 | err = compat_ip_get_mcast_msfilter(sk, optval, optlen, | |
1692 | len); | |
1693 | else | |
1694 | err = ip_get_mcast_msfilter(sk, optval, optlen, len); | |
87e9f031 | 1695 | goto out; |
f771bef9 NS |
1696 | case IP_MULTICAST_ALL: |
1697 | val = inet->mc_all; | |
1698 | break; | |
132adf54 SH |
1699 | case IP_PKTOPTIONS: |
1700 | { | |
1701 | struct msghdr msg; | |
1da177e4 | 1702 | |
1985320c | 1703 | sockopt_release_sock(sk); |
1da177e4 | 1704 | |
132adf54 SH |
1705 | if (sk->sk_type != SOCK_STREAM) |
1706 | return -ENOPROTOOPT; | |
1da177e4 | 1707 | |
728f064c MKL |
1708 | if (optval.is_kernel) { |
1709 | msg.msg_control_is_user = false; | |
1710 | msg.msg_control = optval.kernel; | |
1711 | } else { | |
1712 | msg.msg_control_is_user = true; | |
1713 | msg.msg_control_user = optval.user; | |
1714 | } | |
132adf54 | 1715 | msg.msg_controllen = len; |
b6238c04 | 1716 | msg.msg_flags = in_compat_syscall() ? MSG_CMSG_COMPAT : 0; |
1da177e4 | 1717 | |
132adf54 SH |
1718 | if (inet->cmsg_flags & IP_CMSG_PKTINFO) { |
1719 | struct in_pktinfo info; | |
1720 | ||
c720c7e8 ED |
1721 | info.ipi_addr.s_addr = inet->inet_rcv_saddr; |
1722 | info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr; | |
132adf54 SH |
1723 | info.ipi_ifindex = inet->mc_index; |
1724 | put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info); | |
1da177e4 | 1725 | } |
132adf54 SH |
1726 | if (inet->cmsg_flags & IP_CMSG_TTL) { |
1727 | int hlim = inet->mc_ttl; | |
1728 | put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim); | |
1729 | } | |
4c507d28 JB |
1730 | if (inet->cmsg_flags & IP_CMSG_TOS) { |
1731 | int tos = inet->rcv_tos; | |
1732 | put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos); | |
1733 | } | |
132adf54 | 1734 | len -= msg.msg_controllen; |
728f064c | 1735 | return copy_to_sockptr(optlen, &len, sizeof(int)); |
132adf54 SH |
1736 | } |
1737 | case IP_FREEBIND: | |
1738 | val = inet->freebind; | |
1739 | break; | |
f5715aea KK |
1740 | case IP_TRANSPARENT: |
1741 | val = inet->transparent; | |
1742 | break; | |
d218d111 SH |
1743 | case IP_MINTTL: |
1744 | val = inet->min_ttl; | |
1745 | break; | |
132adf54 | 1746 | default: |
1985320c | 1747 | sockopt_release_sock(sk); |
132adf54 | 1748 | return -ENOPROTOOPT; |
1da177e4 | 1749 | } |
1985320c | 1750 | sockopt_release_sock(sk); |
e905a9ed | 1751 | |
4d52cfbe | 1752 | if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) { |
1da177e4 LT |
1753 | unsigned char ucval = (unsigned char)val; |
1754 | len = 1; | |
728f064c | 1755 | if (copy_to_sockptr(optlen, &len, sizeof(int))) |
1da177e4 | 1756 | return -EFAULT; |
728f064c | 1757 | if (copy_to_sockptr(optval, &ucval, 1)) |
1da177e4 LT |
1758 | return -EFAULT; |
1759 | } else { | |
1760 | len = min_t(unsigned int, sizeof(int), len); | |
728f064c | 1761 | if (copy_to_sockptr(optlen, &len, sizeof(int))) |
1da177e4 | 1762 | return -EFAULT; |
728f064c | 1763 | if (copy_to_sockptr(optval, &val, len)) |
1da177e4 LT |
1764 | return -EFAULT; |
1765 | } | |
1766 | return 0; | |
87e9f031 WC |
1767 | |
1768 | out: | |
1985320c | 1769 | sockopt_release_sock(sk); |
87e9f031 WC |
1770 | if (needs_rtnl) |
1771 | rtnl_unlock(); | |
1772 | return err; | |
1da177e4 LT |
1773 | } |
1774 | ||
3fdadf7d | 1775 | int ip_getsockopt(struct sock *sk, int level, |
132adf54 | 1776 | int optname, char __user *optval, int __user *optlen) |
3fdadf7d DM |
1777 | { |
1778 | int err; | |
1779 | ||
728f064c MKL |
1780 | err = do_ip_getsockopt(sk, level, optname, |
1781 | USER_SOCKPTR(optval), USER_SOCKPTR(optlen)); | |
42908c69 | 1782 | |
97adadda | 1783 | #if IS_ENABLED(CONFIG_BPFILTER_UMH) |
d2ba09c1 AS |
1784 | if (optname >= BPFILTER_IPT_SO_GET_INFO && |
1785 | optname < BPFILTER_IPT_GET_MAX) | |
1786 | err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen); | |
1787 | #endif | |
3fdadf7d DM |
1788 | #ifdef CONFIG_NETFILTER |
1789 | /* we need to exclude all possible ENOPROTOOPTs except default case */ | |
6a9fb947 PE |
1790 | if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS && |
1791 | !ip_mroute_opt(optname)) { | |
e905a9ed | 1792 | int len; |
3fdadf7d | 1793 | |
543d9cfe | 1794 | if (get_user(len, optlen)) |
3fdadf7d DM |
1795 | return -EFAULT; |
1796 | ||
77d4df41 | 1797 | err = nf_getsockopt(sk, PF_INET, optname, optval, &len); |
3fdadf7d DM |
1798 | if (err >= 0) |
1799 | err = put_user(len, optlen); | |
1800 | return err; | |
1801 | } | |
1802 | #endif | |
1803 | return err; | |
1804 | } | |
b6238c04 | 1805 | EXPORT_SYMBOL(ip_getsockopt); |