1 // SPDX-License-Identifier: GPL-2.0
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.
8 *
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.
14 * Alan Cox : Fixed a couple of oopses in Martin's
15 * TOS tweaks.
16 * Mike McLagan : Routing by source
17 */
18
19 #include <linux/module.h>
20 #include <linux/types.h>
21 #include <linux/mm.h>
22 #include <linux/skbuff.h>
23 #include <linux/ip.h>
24 #include <linux/icmp.h>
25 #include <linux/inetdevice.h>
26 #include <linux/netdevice.h>
27 #include <linux/slab.h>
28 #include <net/sock.h>
29 #include <net/ip.h>
30 #include <net/icmp.h>
31 #include <net/tcp_states.h>
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>
37 #include <net/inet_ecn.h>
38 #include <net/route.h>
39 #include <net/xfrm.h>
40 #include <net/compat.h>
41 #include <net/checksum.h>
42 #if IS_ENABLED(CONFIG_IPV6)
43 #include <net/transp_v6.h>
44 #endif
45 #include <net/ip_fib.h>
46
47 #include <linux/errqueue.h>
48 #include <linux/uaccess.h>
49
50 #include <linux/bpfilter.h>
51
52 /*
53 * SOL_IP control messages.
54 */
55
ip_cmsg_recv_pktinfo(struct msghdr * msg,struct sk_buff * skb)56 static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
57 {
58 struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
59
60 info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
61
62 put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
63 }
64
ip_cmsg_recv_ttl(struct msghdr * msg,struct sk_buff * skb)65 static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
66 {
67 int ttl = ip_hdr(skb)->ttl;
68 put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
69 }
70
ip_cmsg_recv_tos(struct msghdr * msg,struct sk_buff * skb)71 static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
72 {
73 put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
74 }
75
ip_cmsg_recv_opts(struct msghdr * msg,struct sk_buff * skb)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
81 put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
82 ip_hdr(skb) + 1);
83 }
84
85
ip_cmsg_recv_retopts(struct net * net,struct msghdr * msg,struct sk_buff * skb)86 static void ip_cmsg_recv_retopts(struct net *net, struct msghdr *msg,
87 struct sk_buff *skb)
88 {
89 unsigned char optbuf[sizeof(struct ip_options) + 40];
90 struct ip_options *opt = (struct ip_options *)optbuf;
91
92 if (IPCB(skb)->opt.optlen == 0)
93 return;
94
95 if (ip_options_echo(net, opt, skb)) {
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
ip_cmsg_recv_fragsize(struct msghdr * msg,struct sk_buff * skb)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
ip_cmsg_recv_checksum(struct msghdr * msg,struct sk_buff * skb,int tlen,int offset)115 static void ip_cmsg_recv_checksum(struct msghdr *msg, struct sk_buff *skb,
116 int tlen, int offset)
117 {
118 __wsum csum = skb->csum;
119
120 if (skb->ip_summed != CHECKSUM_COMPLETE)
121 return;
122
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 }
127
128 put_cmsg(msg, SOL_IP, IP_CHECKSUM, sizeof(__wsum), &csum);
129 }
130
ip_cmsg_recv_security(struct msghdr * msg,struct sk_buff * skb)131 static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
132 {
133 char *secdata;
134 u32 seclen, secid;
135 int err;
136
137 err = security_socket_getpeersec_dgram(NULL, skb, &secid);
138 if (err)
139 return;
140
141 err = security_secid_to_secctx(secid, &secdata, &seclen);
142 if (err)
143 return;
144
145 put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
146 security_release_secctx(secdata, seclen);
147 }
148
ip_cmsg_recv_dstaddr(struct msghdr * msg,struct sk_buff * skb)149 static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
150 {
151 __be16 _ports[2], *ports;
152 struct sockaddr_in sin;
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 */
158 ports = skb_header_pointer(skb, skb_transport_offset(skb),
159 sizeof(_ports), &_ports);
160 if (!ports)
161 return;
162
163 sin.sin_family = AF_INET;
164 sin.sin_addr.s_addr = ip_hdr(skb)->daddr;
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 }
170
ip_cmsg_recv_offset(struct msghdr * msg,struct sock * sk,struct sk_buff * skb,int tlen,int offset)171 void ip_cmsg_recv_offset(struct msghdr *msg, struct sock *sk,
172 struct sk_buff *skb, int tlen, int offset)
173 {
174 struct inet_sock *inet = inet_sk(sk);
175 unsigned int flags = inet->cmsg_flags;
176
177 /* Ordered by supposed usage frequency */
178 if (flags & IP_CMSG_PKTINFO) {
179 ip_cmsg_recv_pktinfo(msg, skb);
180
181 flags &= ~IP_CMSG_PKTINFO;
182 if (!flags)
183 return;
184 }
185
186 if (flags & IP_CMSG_TTL) {
187 ip_cmsg_recv_ttl(msg, skb);
188
189 flags &= ~IP_CMSG_TTL;
190 if (!flags)
191 return;
192 }
193
194 if (flags & IP_CMSG_TOS) {
195 ip_cmsg_recv_tos(msg, skb);
196
197 flags &= ~IP_CMSG_TOS;
198 if (!flags)
199 return;
200 }
201
202 if (flags & IP_CMSG_RECVOPTS) {
203 ip_cmsg_recv_opts(msg, skb);
204
205 flags &= ~IP_CMSG_RECVOPTS;
206 if (!flags)
207 return;
208 }
209
210 if (flags & IP_CMSG_RETOPTS) {
211 ip_cmsg_recv_retopts(sock_net(sk), msg, skb);
212
213 flags &= ~IP_CMSG_RETOPTS;
214 if (!flags)
215 return;
216 }
217
218 if (flags & IP_CMSG_PASSSEC) {
219 ip_cmsg_recv_security(msg, skb);
220
221 flags &= ~IP_CMSG_PASSSEC;
222 if (!flags)
223 return;
224 }
225
226 if (flags & IP_CMSG_ORIGDSTADDR) {
227 ip_cmsg_recv_dstaddr(msg, skb);
228
229 flags &= ~IP_CMSG_ORIGDSTADDR;
230 if (!flags)
231 return;
232 }
233
234 if (flags & IP_CMSG_CHECKSUM)
235 ip_cmsg_recv_checksum(msg, skb, tlen, offset);
236
237 if (flags & IP_CMSG_RECVFRAGSIZE)
238 ip_cmsg_recv_fragsize(msg, skb);
239 }
240 EXPORT_SYMBOL(ip_cmsg_recv_offset);
241
ip_cmsg_send(struct sock * sk,struct msghdr * msg,struct ipcm_cookie * ipc,bool allow_ipv6)242 int ip_cmsg_send(struct sock *sk, struct msghdr *msg, struct ipcm_cookie *ipc,
243 bool allow_ipv6)
244 {
245 int err, val;
246 struct cmsghdr *cmsg;
247 struct net *net = sock_net(sk);
248
249 for_each_cmsghdr(cmsg, msg) {
250 if (!CMSG_OK(msg, cmsg))
251 return -EINVAL;
252 #if IS_ENABLED(CONFIG_IPV6)
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;
263 if (src_info->ipi6_ifindex)
264 ipc->oif = src_info->ipi6_ifindex;
265 ipc->addr = src_info->ipi6_addr.s6_addr32[3];
266 continue;
267 }
268 #endif
269 if (cmsg->cmsg_level == SOL_SOCKET) {
270 err = __sock_cmsg_send(sk, msg, cmsg, &ipc->sockc);
271 if (err)
272 return err;
273 continue;
274 }
275
276 if (cmsg->cmsg_level != SOL_IP)
277 continue;
278 switch (cmsg->cmsg_type) {
279 case IP_RETOPTS:
280 err = cmsg->cmsg_len - sizeof(struct cmsghdr);
281
282 /* Our caller is responsible for freeing ipc->opt */
283 err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
284 err < 40 ? err : 40);
285 if (err)
286 return err;
287 break;
288 case IP_PKTINFO:
289 {
290 struct in_pktinfo *info;
291 if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
292 return -EINVAL;
293 info = (struct in_pktinfo *)CMSG_DATA(cmsg);
294 if (info->ipi_ifindex)
295 ipc->oif = info->ipi_ifindex;
296 ipc->addr = info->ipi_spec_dst.s_addr;
297 break;
298 }
299 case IP_TTL:
300 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
301 return -EINVAL;
302 val = *(int *)CMSG_DATA(cmsg);
303 if (val < 1 || val > 255)
304 return -EINVAL;
305 ipc->ttl = val;
306 break;
307 case IP_TOS:
308 if (cmsg->cmsg_len == CMSG_LEN(sizeof(int)))
309 val = *(int *)CMSG_DATA(cmsg);
310 else if (cmsg->cmsg_len == CMSG_LEN(sizeof(u8)))
311 val = *(u8 *)CMSG_DATA(cmsg);
312 else
313 return -EINVAL;
314 if (val < 0 || val > 255)
315 return -EINVAL;
316 ipc->tos = val;
317 ipc->priority = rt_tos2priority(ipc->tos);
318 break;
319 case IP_PROTOCOL:
320 if (cmsg->cmsg_len != CMSG_LEN(sizeof(int)))
321 return -EINVAL;
322 val = *(int *)CMSG_DATA(cmsg);
323 if (val < 1 || val > 255)
324 return -EINVAL;
325 ipc->protocol = val;
326 break;
327 default:
328 return -EINVAL;
329 }
330 }
331 return 0;
332 }
333
ip_ra_destroy_rcu(struct rcu_head * head)334 static void ip_ra_destroy_rcu(struct rcu_head *head)
335 {
336 struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
337
338 sock_put(ra->saved_sk);
339 kfree(ra);
340 }
341
ip_ra_control(struct sock * sk,unsigned char on,void (* destructor)(struct sock *))342 int ip_ra_control(struct sock *sk, unsigned char on,
343 void (*destructor)(struct sock *))
344 {
345 struct ip_ra_chain *ra, *new_ra;
346 struct ip_ra_chain __rcu **rap;
347 struct net *net = sock_net(sk);
348
349 if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
350 return -EINVAL;
351
352 new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
353 if (on && !new_ra)
354 return -ENOMEM;
355
356 mutex_lock(&net->ipv4.ra_mutex);
357 for (rap = &net->ipv4.ra_chain;
358 (ra = rcu_dereference_protected(*rap,
359 lockdep_is_held(&net->ipv4.ra_mutex))) != NULL;
360 rap = &ra->next) {
361 if (ra->sk == sk) {
362 if (on) {
363 mutex_unlock(&net->ipv4.ra_mutex);
364 kfree(new_ra);
365 return -EADDRINUSE;
366 }
367 /* dont let ip_call_ra_chain() use sk again */
368 ra->sk = NULL;
369 RCU_INIT_POINTER(*rap, ra->next);
370 mutex_unlock(&net->ipv4.ra_mutex);
371
372 if (ra->destructor)
373 ra->destructor(sk);
374 /*
375 * Delay sock_put(sk) and kfree(ra) after one rcu grace
376 * period. This guarantee ip_call_ra_chain() dont need
377 * to mess with socket refcounts.
378 */
379 ra->saved_sk = sk;
380 call_rcu(&ra->rcu, ip_ra_destroy_rcu);
381 return 0;
382 }
383 }
384 if (!new_ra) {
385 mutex_unlock(&net->ipv4.ra_mutex);
386 return -ENOBUFS;
387 }
388 new_ra->sk = sk;
389 new_ra->destructor = destructor;
390
391 RCU_INIT_POINTER(new_ra->next, ra);
392 rcu_assign_pointer(*rap, new_ra);
393 sock_hold(sk);
394 mutex_unlock(&net->ipv4.ra_mutex);
395
396 return 0;
397 }
398
ip_icmp_error(struct sock * sk,struct sk_buff * skb,int err,__be16 port,u32 info,u8 * payload)399 void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
400 __be16 port, u32 info, u8 *payload)
401 {
402 struct sock_exterr_skb *serr;
403
404 skb = skb_clone(skb, GFP_ATOMIC);
405 if (!skb)
406 return;
407
408 serr = SKB_EXT_ERR(skb);
409 serr->ee.ee_errno = err;
410 serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
411 serr->ee.ee_type = icmp_hdr(skb)->type;
412 serr->ee.ee_code = icmp_hdr(skb)->code;
413 serr->ee.ee_pad = 0;
414 serr->ee.ee_info = info;
415 serr->ee.ee_data = 0;
416 serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
417 skb_network_header(skb);
418 serr->port = port;
419
420 if (skb_pull(skb, payload - skb->data)) {
421 skb_reset_transport_header(skb);
422 if (sock_queue_err_skb(sk, skb) == 0)
423 return;
424 }
425 kfree_skb(skb);
426 }
427
ip_local_error(struct sock * sk,int err,__be32 daddr,__be16 port,u32 info)428 void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
429 {
430 struct inet_sock *inet = inet_sk(sk);
431 struct sock_exterr_skb *serr;
432 struct iphdr *iph;
433 struct sk_buff *skb;
434
435 if (!inet->recverr)
436 return;
437
438 skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
439 if (!skb)
440 return;
441
442 skb_put(skb, sizeof(struct iphdr));
443 skb_reset_network_header(skb);
444 iph = ip_hdr(skb);
445 iph->daddr = daddr;
446
447 serr = SKB_EXT_ERR(skb);
448 serr->ee.ee_errno = err;
449 serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
450 serr->ee.ee_type = 0;
451 serr->ee.ee_code = 0;
452 serr->ee.ee_pad = 0;
453 serr->ee.ee_info = info;
454 serr->ee.ee_data = 0;
455 serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
456 serr->port = port;
457
458 __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
459 skb_reset_transport_header(skb);
460
461 if (sock_queue_err_skb(sk, skb))
462 kfree_skb(skb);
463 }
464
465 /* For some errors we have valid addr_offset even with zero payload and
466 * zero port. Also, addr_offset should be supported if port is set.
467 */
ipv4_datagram_support_addr(struct sock_exterr_skb * serr)468 static inline bool ipv4_datagram_support_addr(struct sock_exterr_skb *serr)
469 {
470 return serr->ee.ee_origin == SO_EE_ORIGIN_ICMP ||
471 serr->ee.ee_origin == SO_EE_ORIGIN_LOCAL || serr->port;
472 }
473
474 /* IPv4 supports cmsg on all imcp errors and some timestamps
475 *
476 * Timestamp code paths do not initialize the fields expected by cmsg:
477 * the PKTINFO fields in skb->cb[]. Fill those in here.
478 */
ipv4_datagram_support_cmsg(const struct sock * sk,struct sk_buff * skb,int ee_origin)479 static bool ipv4_datagram_support_cmsg(const struct sock *sk,
480 struct sk_buff *skb,
481 int ee_origin)
482 {
483 struct in_pktinfo *info;
484
485 if (ee_origin == SO_EE_ORIGIN_ICMP)
486 return true;
487
488 if (ee_origin == SO_EE_ORIGIN_LOCAL)
489 return false;
490
491 /* Support IP_PKTINFO on tstamp packets if requested, to correlate
492 * timestamp with egress dev. Not possible for packets without iif
493 * or without payload (SOF_TIMESTAMPING_OPT_TSONLY).
494 */
495 info = PKTINFO_SKB_CB(skb);
496 if (!(sk->sk_tsflags & SOF_TIMESTAMPING_OPT_CMSG) ||
497 !info->ipi_ifindex)
498 return false;
499
500 info->ipi_spec_dst.s_addr = ip_hdr(skb)->saddr;
501 return true;
502 }
503
504 /*
505 * Handle MSG_ERRQUEUE
506 */
ip_recv_error(struct sock * sk,struct msghdr * msg,int len,int * addr_len)507 int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
508 {
509 struct sock_exterr_skb *serr;
510 struct sk_buff *skb;
511 DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
512 struct {
513 struct sock_extended_err ee;
514 struct sockaddr_in offender;
515 } errhdr;
516 int err;
517 int copied;
518
519 err = -EAGAIN;
520 skb = sock_dequeue_err_skb(sk);
521 if (!skb)
522 goto out;
523
524 copied = skb->len;
525 if (copied > len) {
526 msg->msg_flags |= MSG_TRUNC;
527 copied = len;
528 }
529 err = skb_copy_datagram_msg(skb, 0, msg, copied);
530 if (unlikely(err)) {
531 kfree_skb(skb);
532 return err;
533 }
534 sock_recv_timestamp(msg, sk, skb);
535
536 serr = SKB_EXT_ERR(skb);
537
538 if (sin && ipv4_datagram_support_addr(serr)) {
539 sin->sin_family = AF_INET;
540 sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
541 serr->addr_offset);
542 sin->sin_port = serr->port;
543 memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
544 *addr_len = sizeof(*sin);
545 }
546
547 memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
548 sin = &errhdr.offender;
549 memset(sin, 0, sizeof(*sin));
550
551 if (ipv4_datagram_support_cmsg(sk, skb, serr->ee.ee_origin)) {
552 sin->sin_family = AF_INET;
553 sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
554 if (inet_sk(sk)->cmsg_flags)
555 ip_cmsg_recv(msg, skb);
556 }
557
558 put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
559
560 /* Now we could try to dump offended packet options */
561
562 msg->msg_flags |= MSG_ERRQUEUE;
563 err = copied;
564
565 consume_skb(skb);
566 out:
567 return err;
568 }
569
570
571 /*
572 * Socket option code for IP. This is the end of the line after any
573 * TCP,UDP etc options on an IP socket.
574 */
setsockopt_needs_rtnl(int optname)575 static bool setsockopt_needs_rtnl(int optname)
576 {
577 switch (optname) {
578 case IP_ADD_MEMBERSHIP:
579 case IP_ADD_SOURCE_MEMBERSHIP:
580 case IP_BLOCK_SOURCE:
581 case IP_DROP_MEMBERSHIP:
582 case IP_DROP_SOURCE_MEMBERSHIP:
583 case IP_MSFILTER:
584 case IP_UNBLOCK_SOURCE:
585 case MCAST_BLOCK_SOURCE:
586 case MCAST_MSFILTER:
587 case MCAST_JOIN_GROUP:
588 case MCAST_JOIN_SOURCE_GROUP:
589 case MCAST_LEAVE_GROUP:
590 case MCAST_LEAVE_SOURCE_GROUP:
591 case MCAST_UNBLOCK_SOURCE:
592 return true;
593 }
594 return false;
595 }
596
do_ip_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)597 static int do_ip_setsockopt(struct sock *sk, int level,
598 int optname, char __user *optval, unsigned int optlen)
599 {
600 struct inet_sock *inet = inet_sk(sk);
601 struct net *net = sock_net(sk);
602 int val = 0, err;
603 bool needs_rtnl = setsockopt_needs_rtnl(optname);
604
605 switch (optname) {
606 case IP_PKTINFO:
607 case IP_RECVTTL:
608 case IP_RECVOPTS:
609 case IP_RECVTOS:
610 case IP_RETOPTS:
611 case IP_TOS:
612 case IP_TTL:
613 case IP_HDRINCL:
614 case IP_MTU_DISCOVER:
615 case IP_RECVERR:
616 case IP_ROUTER_ALERT:
617 case IP_FREEBIND:
618 case IP_PASSSEC:
619 case IP_TRANSPARENT:
620 case IP_MINTTL:
621 case IP_NODEFRAG:
622 case IP_BIND_ADDRESS_NO_PORT:
623 case IP_UNICAST_IF:
624 case IP_MULTICAST_TTL:
625 case IP_MULTICAST_ALL:
626 case IP_MULTICAST_LOOP:
627 case IP_RECVORIGDSTADDR:
628 case IP_CHECKSUM:
629 case IP_RECVFRAGSIZE:
630 if (optlen >= sizeof(int)) {
631 if (get_user(val, (int __user *) optval))
632 return -EFAULT;
633 } else if (optlen >= sizeof(char)) {
634 unsigned char ucval;
635
636 if (get_user(ucval, (unsigned char __user *) optval))
637 return -EFAULT;
638 val = (int) ucval;
639 }
640 }
641
642 /* If optlen==0, it is equivalent to val == 0 */
643
644 if (optname == IP_ROUTER_ALERT)
645 return ip_ra_control(sk, val ? 1 : 0, NULL);
646 if (ip_mroute_opt(optname))
647 return ip_mroute_setsockopt(sk, optname, optval, optlen);
648
649 err = 0;
650 if (needs_rtnl)
651 rtnl_lock();
652 lock_sock(sk);
653
654 switch (optname) {
655 case IP_OPTIONS:
656 {
657 struct ip_options_rcu *old, *opt = NULL;
658
659 if (optlen > 40)
660 goto e_inval;
661 err = ip_options_get_from_user(sock_net(sk), &opt,
662 optval, optlen);
663 if (err)
664 break;
665 old = rcu_dereference_protected(inet->inet_opt,
666 lockdep_sock_is_held(sk));
667 if (inet->is_icsk) {
668 struct inet_connection_sock *icsk = inet_csk(sk);
669 #if IS_ENABLED(CONFIG_IPV6)
670 if (sk->sk_family == PF_INET ||
671 (!((1 << sk->sk_state) &
672 (TCPF_LISTEN | TCPF_CLOSE)) &&
673 inet->inet_daddr != LOOPBACK4_IPV6)) {
674 #endif
675 if (old)
676 icsk->icsk_ext_hdr_len -= old->opt.optlen;
677 if (opt)
678 icsk->icsk_ext_hdr_len += opt->opt.optlen;
679 icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
680 #if IS_ENABLED(CONFIG_IPV6)
681 }
682 #endif
683 }
684 rcu_assign_pointer(inet->inet_opt, opt);
685 if (old)
686 kfree_rcu(old, rcu);
687 break;
688 }
689 case IP_PKTINFO:
690 if (val)
691 inet->cmsg_flags |= IP_CMSG_PKTINFO;
692 else
693 inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
694 break;
695 case IP_RECVTTL:
696 if (val)
697 inet->cmsg_flags |= IP_CMSG_TTL;
698 else
699 inet->cmsg_flags &= ~IP_CMSG_TTL;
700 break;
701 case IP_RECVTOS:
702 if (val)
703 inet->cmsg_flags |= IP_CMSG_TOS;
704 else
705 inet->cmsg_flags &= ~IP_CMSG_TOS;
706 break;
707 case IP_RECVOPTS:
708 if (val)
709 inet->cmsg_flags |= IP_CMSG_RECVOPTS;
710 else
711 inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
712 break;
713 case IP_RETOPTS:
714 if (val)
715 inet->cmsg_flags |= IP_CMSG_RETOPTS;
716 else
717 inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
718 break;
719 case IP_PASSSEC:
720 if (val)
721 inet->cmsg_flags |= IP_CMSG_PASSSEC;
722 else
723 inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
724 break;
725 case IP_RECVORIGDSTADDR:
726 if (val)
727 inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
728 else
729 inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
730 break;
731 case IP_CHECKSUM:
732 if (val) {
733 if (!(inet->cmsg_flags & IP_CMSG_CHECKSUM)) {
734 inet_inc_convert_csum(sk);
735 inet->cmsg_flags |= IP_CMSG_CHECKSUM;
736 }
737 } else {
738 if (inet->cmsg_flags & IP_CMSG_CHECKSUM) {
739 inet_dec_convert_csum(sk);
740 inet->cmsg_flags &= ~IP_CMSG_CHECKSUM;
741 }
742 }
743 break;
744 case IP_RECVFRAGSIZE:
745 if (sk->sk_type != SOCK_RAW && sk->sk_type != SOCK_DGRAM)
746 goto e_inval;
747 if (val)
748 inet->cmsg_flags |= IP_CMSG_RECVFRAGSIZE;
749 else
750 inet->cmsg_flags &= ~IP_CMSG_RECVFRAGSIZE;
751 break;
752 case IP_TOS: /* This sets both TOS and Precedence */
753 if (sk->sk_type == SOCK_STREAM) {
754 val &= ~INET_ECN_MASK;
755 val |= inet->tos & INET_ECN_MASK;
756 }
757 if (inet->tos != val) {
758 inet->tos = val;
759 sk->sk_priority = rt_tos2priority(val);
760 sk_dst_reset(sk);
761 }
762 break;
763 case IP_TTL:
764 if (optlen < 1)
765 goto e_inval;
766 if (val != -1 && (val < 1 || val > 255))
767 goto e_inval;
768 inet->uc_ttl = val;
769 break;
770 case IP_HDRINCL:
771 if (sk->sk_type != SOCK_RAW) {
772 err = -ENOPROTOOPT;
773 break;
774 }
775 inet->hdrincl = val ? 1 : 0;
776 break;
777 case IP_NODEFRAG:
778 if (sk->sk_type != SOCK_RAW) {
779 err = -ENOPROTOOPT;
780 break;
781 }
782 inet->nodefrag = val ? 1 : 0;
783 break;
784 case IP_BIND_ADDRESS_NO_PORT:
785 inet->bind_address_no_port = val ? 1 : 0;
786 break;
787 case IP_MTU_DISCOVER:
788 if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_OMIT)
789 goto e_inval;
790 inet->pmtudisc = val;
791 break;
792 case IP_RECVERR:
793 inet->recverr = !!val;
794 if (!val)
795 skb_queue_purge(&sk->sk_error_queue);
796 break;
797 case IP_MULTICAST_TTL:
798 if (sk->sk_type == SOCK_STREAM)
799 goto e_inval;
800 if (optlen < 1)
801 goto e_inval;
802 if (val == -1)
803 val = 1;
804 if (val < 0 || val > 255)
805 goto e_inval;
806 inet->mc_ttl = val;
807 break;
808 case IP_MULTICAST_LOOP:
809 if (optlen < 1)
810 goto e_inval;
811 inet->mc_loop = !!val;
812 break;
813 case IP_UNICAST_IF:
814 {
815 struct net_device *dev = NULL;
816 int ifindex;
817 int midx;
818
819 if (optlen != sizeof(int))
820 goto e_inval;
821
822 ifindex = (__force int)ntohl((__force __be32)val);
823 if (ifindex == 0) {
824 inet->uc_index = 0;
825 err = 0;
826 break;
827 }
828
829 dev = dev_get_by_index(sock_net(sk), ifindex);
830 err = -EADDRNOTAVAIL;
831 if (!dev)
832 break;
833
834 midx = l3mdev_master_ifindex(dev);
835 dev_put(dev);
836
837 err = -EINVAL;
838 if (sk->sk_bound_dev_if &&
839 (!midx || midx != sk->sk_bound_dev_if))
840 break;
841
842 inet->uc_index = ifindex;
843 err = 0;
844 break;
845 }
846 case IP_MULTICAST_IF:
847 {
848 struct ip_mreqn mreq;
849 struct net_device *dev = NULL;
850 int midx;
851
852 if (sk->sk_type == SOCK_STREAM)
853 goto e_inval;
854 /*
855 * Check the arguments are allowable
856 */
857
858 if (optlen < sizeof(struct in_addr))
859 goto e_inval;
860
861 err = -EFAULT;
862 if (optlen >= sizeof(struct ip_mreqn)) {
863 if (copy_from_user(&mreq, optval, sizeof(mreq)))
864 break;
865 } else {
866 memset(&mreq, 0, sizeof(mreq));
867 if (optlen >= sizeof(struct ip_mreq)) {
868 if (copy_from_user(&mreq, optval,
869 sizeof(struct ip_mreq)))
870 break;
871 } else if (optlen >= sizeof(struct in_addr)) {
872 if (copy_from_user(&mreq.imr_address, optval,
873 sizeof(struct in_addr)))
874 break;
875 }
876 }
877
878 if (!mreq.imr_ifindex) {
879 if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
880 inet->mc_index = 0;
881 inet->mc_addr = 0;
882 err = 0;
883 break;
884 }
885 dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
886 if (dev)
887 mreq.imr_ifindex = dev->ifindex;
888 } else
889 dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
890
891
892 err = -EADDRNOTAVAIL;
893 if (!dev)
894 break;
895
896 midx = l3mdev_master_ifindex(dev);
897
898 dev_put(dev);
899
900 err = -EINVAL;
901 if (sk->sk_bound_dev_if &&
902 mreq.imr_ifindex != sk->sk_bound_dev_if &&
903 (!midx || midx != sk->sk_bound_dev_if))
904 break;
905
906 inet->mc_index = mreq.imr_ifindex;
907 inet->mc_addr = mreq.imr_address.s_addr;
908 err = 0;
909 break;
910 }
911
912 case IP_ADD_MEMBERSHIP:
913 case IP_DROP_MEMBERSHIP:
914 {
915 struct ip_mreqn mreq;
916
917 err = -EPROTO;
918 if (inet_sk(sk)->is_icsk)
919 break;
920
921 if (optlen < sizeof(struct ip_mreq))
922 goto e_inval;
923 err = -EFAULT;
924 if (optlen >= sizeof(struct ip_mreqn)) {
925 if (copy_from_user(&mreq, optval, sizeof(mreq)))
926 break;
927 } else {
928 memset(&mreq, 0, sizeof(mreq));
929 if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
930 break;
931 }
932
933 if (optname == IP_ADD_MEMBERSHIP)
934 err = ip_mc_join_group(sk, &mreq);
935 else
936 err = ip_mc_leave_group(sk, &mreq);
937 break;
938 }
939 case IP_MSFILTER:
940 {
941 struct ip_msfilter *msf;
942
943 if (optlen < IP_MSFILTER_SIZE(0))
944 goto e_inval;
945 if (optlen > sysctl_optmem_max) {
946 err = -ENOBUFS;
947 break;
948 }
949 msf = memdup_user(optval, optlen);
950 if (IS_ERR(msf)) {
951 err = PTR_ERR(msf);
952 break;
953 }
954 /* numsrc >= (1G-4) overflow in 32 bits */
955 if (msf->imsf_numsrc >= 0x3ffffffcU ||
956 msf->imsf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
957 kfree(msf);
958 err = -ENOBUFS;
959 break;
960 }
961 if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
962 kfree(msf);
963 err = -EINVAL;
964 break;
965 }
966 err = ip_mc_msfilter(sk, msf, 0);
967 kfree(msf);
968 break;
969 }
970 case IP_BLOCK_SOURCE:
971 case IP_UNBLOCK_SOURCE:
972 case IP_ADD_SOURCE_MEMBERSHIP:
973 case IP_DROP_SOURCE_MEMBERSHIP:
974 {
975 struct ip_mreq_source mreqs;
976 int omode, add;
977
978 if (optlen != sizeof(struct ip_mreq_source))
979 goto e_inval;
980 if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
981 err = -EFAULT;
982 break;
983 }
984 if (optname == IP_BLOCK_SOURCE) {
985 omode = MCAST_EXCLUDE;
986 add = 1;
987 } else if (optname == IP_UNBLOCK_SOURCE) {
988 omode = MCAST_EXCLUDE;
989 add = 0;
990 } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
991 struct ip_mreqn mreq;
992
993 mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
994 mreq.imr_address.s_addr = mreqs.imr_interface;
995 mreq.imr_ifindex = 0;
996 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
997 if (err && err != -EADDRINUSE)
998 break;
999 omode = MCAST_INCLUDE;
1000 add = 1;
1001 } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
1002 omode = MCAST_INCLUDE;
1003 add = 0;
1004 }
1005 err = ip_mc_source(add, omode, sk, &mreqs, 0);
1006 break;
1007 }
1008 case MCAST_JOIN_GROUP:
1009 case MCAST_LEAVE_GROUP:
1010 {
1011 struct group_req greq;
1012 struct sockaddr_in *psin;
1013 struct ip_mreqn mreq;
1014
1015 if (optlen < sizeof(struct group_req))
1016 goto e_inval;
1017 err = -EFAULT;
1018 if (copy_from_user(&greq, optval, sizeof(greq)))
1019 break;
1020 psin = (struct sockaddr_in *)&greq.gr_group;
1021 if (psin->sin_family != AF_INET)
1022 goto e_inval;
1023 memset(&mreq, 0, sizeof(mreq));
1024 mreq.imr_multiaddr = psin->sin_addr;
1025 mreq.imr_ifindex = greq.gr_interface;
1026
1027 if (optname == MCAST_JOIN_GROUP)
1028 err = ip_mc_join_group(sk, &mreq);
1029 else
1030 err = ip_mc_leave_group(sk, &mreq);
1031 break;
1032 }
1033 case MCAST_JOIN_SOURCE_GROUP:
1034 case MCAST_LEAVE_SOURCE_GROUP:
1035 case MCAST_BLOCK_SOURCE:
1036 case MCAST_UNBLOCK_SOURCE:
1037 {
1038 struct group_source_req greqs;
1039 struct ip_mreq_source mreqs;
1040 struct sockaddr_in *psin;
1041 int omode, add;
1042
1043 if (optlen != sizeof(struct group_source_req))
1044 goto e_inval;
1045 if (copy_from_user(&greqs, optval, sizeof(greqs))) {
1046 err = -EFAULT;
1047 break;
1048 }
1049 if (greqs.gsr_group.ss_family != AF_INET ||
1050 greqs.gsr_source.ss_family != AF_INET) {
1051 err = -EADDRNOTAVAIL;
1052 break;
1053 }
1054 psin = (struct sockaddr_in *)&greqs.gsr_group;
1055 mreqs.imr_multiaddr = psin->sin_addr.s_addr;
1056 psin = (struct sockaddr_in *)&greqs.gsr_source;
1057 mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
1058 mreqs.imr_interface = 0; /* use index for mc_source */
1059
1060 if (optname == MCAST_BLOCK_SOURCE) {
1061 omode = MCAST_EXCLUDE;
1062 add = 1;
1063 } else if (optname == MCAST_UNBLOCK_SOURCE) {
1064 omode = MCAST_EXCLUDE;
1065 add = 0;
1066 } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
1067 struct ip_mreqn mreq;
1068
1069 psin = (struct sockaddr_in *)&greqs.gsr_group;
1070 mreq.imr_multiaddr = psin->sin_addr;
1071 mreq.imr_address.s_addr = 0;
1072 mreq.imr_ifindex = greqs.gsr_interface;
1073 err = ip_mc_join_group_ssm(sk, &mreq, MCAST_INCLUDE);
1074 if (err && err != -EADDRINUSE)
1075 break;
1076 greqs.gsr_interface = mreq.imr_ifindex;
1077 omode = MCAST_INCLUDE;
1078 add = 1;
1079 } else /* MCAST_LEAVE_SOURCE_GROUP */ {
1080 omode = MCAST_INCLUDE;
1081 add = 0;
1082 }
1083 err = ip_mc_source(add, omode, sk, &mreqs,
1084 greqs.gsr_interface);
1085 break;
1086 }
1087 case MCAST_MSFILTER:
1088 {
1089 struct sockaddr_in *psin;
1090 struct ip_msfilter *msf = NULL;
1091 struct group_filter *gsf = NULL;
1092 int msize, i, ifindex;
1093
1094 if (optlen < GROUP_FILTER_SIZE(0))
1095 goto e_inval;
1096 if (optlen > sysctl_optmem_max) {
1097 err = -ENOBUFS;
1098 break;
1099 }
1100 gsf = memdup_user(optval, optlen);
1101 if (IS_ERR(gsf)) {
1102 err = PTR_ERR(gsf);
1103 break;
1104 }
1105
1106 /* numsrc >= (4G-140)/128 overflow in 32 bits */
1107 if (gsf->gf_numsrc >= 0x1ffffff ||
1108 gsf->gf_numsrc > net->ipv4.sysctl_igmp_max_msf) {
1109 err = -ENOBUFS;
1110 goto mc_msf_out;
1111 }
1112 if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
1113 err = -EINVAL;
1114 goto mc_msf_out;
1115 }
1116 msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
1117 msf = kmalloc(msize, GFP_KERNEL);
1118 if (!msf) {
1119 err = -ENOBUFS;
1120 goto mc_msf_out;
1121 }
1122 ifindex = gsf->gf_interface;
1123 psin = (struct sockaddr_in *)&gsf->gf_group;
1124 if (psin->sin_family != AF_INET) {
1125 err = -EADDRNOTAVAIL;
1126 goto mc_msf_out;
1127 }
1128 msf->imsf_multiaddr = psin->sin_addr.s_addr;
1129 msf->imsf_interface = 0;
1130 msf->imsf_fmode = gsf->gf_fmode;
1131 msf->imsf_numsrc = gsf->gf_numsrc;
1132 err = -EADDRNOTAVAIL;
1133 for (i = 0; i < gsf->gf_numsrc; ++i) {
1134 psin = (struct sockaddr_in *)&gsf->gf_slist[i];
1135
1136 if (psin->sin_family != AF_INET)
1137 goto mc_msf_out;
1138 msf->imsf_slist[i] = psin->sin_addr.s_addr;
1139 }
1140 kfree(gsf);
1141 gsf = NULL;
1142
1143 err = ip_mc_msfilter(sk, msf, ifindex);
1144 mc_msf_out:
1145 kfree(msf);
1146 kfree(gsf);
1147 break;
1148 }
1149 case IP_MULTICAST_ALL:
1150 if (optlen < 1)
1151 goto e_inval;
1152 if (val != 0 && val != 1)
1153 goto e_inval;
1154 inet->mc_all = val;
1155 break;
1156
1157 case IP_FREEBIND:
1158 if (optlen < 1)
1159 goto e_inval;
1160 inet->freebind = !!val;
1161 break;
1162
1163 case IP_IPSEC_POLICY:
1164 case IP_XFRM_POLICY:
1165 err = -EPERM;
1166 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
1167 break;
1168 err = xfrm_user_policy(sk, optname, optval, optlen);
1169 break;
1170
1171 case IP_TRANSPARENT:
1172 if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
1173 !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
1174 err = -EPERM;
1175 break;
1176 }
1177 if (optlen < 1)
1178 goto e_inval;
1179 inet->transparent = !!val;
1180 break;
1181
1182 case IP_MINTTL:
1183 if (optlen < 1)
1184 goto e_inval;
1185 if (val < 0 || val > 255)
1186 goto e_inval;
1187 inet->min_ttl = val;
1188 break;
1189
1190 default:
1191 err = -ENOPROTOOPT;
1192 break;
1193 }
1194 release_sock(sk);
1195 if (needs_rtnl)
1196 rtnl_unlock();
1197 return err;
1198
1199 e_inval:
1200 release_sock(sk);
1201 if (needs_rtnl)
1202 rtnl_unlock();
1203 return -EINVAL;
1204 }
1205
1206 /**
1207 * ipv4_pktinfo_prepare - transfer some info from rtable to skb
1208 * @sk: socket
1209 * @skb: buffer
1210 *
1211 * To support IP_CMSG_PKTINFO option, we store rt_iif and specific
1212 * destination in skb->cb[] before dst drop.
1213 * This way, receiver doesn't make cache line misses to read rtable.
1214 */
ipv4_pktinfo_prepare(const struct sock * sk,struct sk_buff * skb)1215 void ipv4_pktinfo_prepare(const struct sock *sk, struct sk_buff *skb)
1216 {
1217 struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
1218 bool prepare = (inet_sk(sk)->cmsg_flags & IP_CMSG_PKTINFO) ||
1219 ipv6_sk_rxinfo(sk);
1220
1221 if (prepare && skb_rtable(skb)) {
1222 /* skb->cb is overloaded: prior to this point it is IP{6}CB
1223 * which has interface index (iif) as the first member of the
1224 * underlying inet{6}_skb_parm struct. This code then overlays
1225 * PKTINFO_SKB_CB and in_pktinfo also has iif as the first
1226 * element so the iif is picked up from the prior IPCB. If iif
1227 * is the loopback interface, then return the sending interface
1228 * (e.g., process binds socket to eth0 for Tx which is
1229 * redirected to loopback in the rtable/dst).
1230 */
1231 struct rtable *rt = skb_rtable(skb);
1232 bool l3slave = ipv4_l3mdev_skb(IPCB(skb)->flags);
1233
1234 if (pktinfo->ipi_ifindex == LOOPBACK_IFINDEX)
1235 pktinfo->ipi_ifindex = inet_iif(skb);
1236 else if (l3slave && rt && rt->rt_iif)
1237 pktinfo->ipi_ifindex = rt->rt_iif;
1238
1239 pktinfo->ipi_spec_dst.s_addr = fib_compute_spec_dst(skb);
1240 } else {
1241 pktinfo->ipi_ifindex = 0;
1242 pktinfo->ipi_spec_dst.s_addr = 0;
1243 }
1244 skb_dst_drop(skb);
1245 }
1246
ip_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)1247 int ip_setsockopt(struct sock *sk, int level,
1248 int optname, char __user *optval, unsigned int optlen)
1249 {
1250 int err;
1251
1252 if (level != SOL_IP)
1253 return -ENOPROTOOPT;
1254
1255 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1256 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1257 if (optname >= BPFILTER_IPT_SO_SET_REPLACE &&
1258 optname < BPFILTER_IPT_SET_MAX)
1259 err = bpfilter_ip_set_sockopt(sk, optname, optval, optlen);
1260 #endif
1261 #ifdef CONFIG_NETFILTER
1262 /* we need to exclude all possible ENOPROTOOPTs except default case */
1263 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1264 optname != IP_IPSEC_POLICY &&
1265 optname != IP_XFRM_POLICY &&
1266 !ip_mroute_opt(optname))
1267 err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
1268 #endif
1269 return err;
1270 }
1271 EXPORT_SYMBOL(ip_setsockopt);
1272
1273 #ifdef CONFIG_COMPAT
compat_ip_setsockopt(struct sock * sk,int level,int optname,char __user * optval,unsigned int optlen)1274 int compat_ip_setsockopt(struct sock *sk, int level, int optname,
1275 char __user *optval, unsigned int optlen)
1276 {
1277 int err;
1278
1279 if (level != SOL_IP)
1280 return -ENOPROTOOPT;
1281
1282 if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
1283 return compat_mc_setsockopt(sk, level, optname, optval, optlen,
1284 ip_setsockopt);
1285
1286 err = do_ip_setsockopt(sk, level, optname, optval, optlen);
1287 #ifdef CONFIG_NETFILTER
1288 /* we need to exclude all possible ENOPROTOOPTs except default case */
1289 if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
1290 optname != IP_IPSEC_POLICY &&
1291 optname != IP_XFRM_POLICY &&
1292 !ip_mroute_opt(optname))
1293 err = compat_nf_setsockopt(sk, PF_INET, optname, optval,
1294 optlen);
1295 #endif
1296 return err;
1297 }
1298 EXPORT_SYMBOL(compat_ip_setsockopt);
1299 #endif
1300
1301 /*
1302 * Get the options. Note for future reference. The GET of IP options gets
1303 * the _received_ ones. The set sets the _sent_ ones.
1304 */
1305
getsockopt_needs_rtnl(int optname)1306 static bool getsockopt_needs_rtnl(int optname)
1307 {
1308 switch (optname) {
1309 case IP_MSFILTER:
1310 case MCAST_MSFILTER:
1311 return true;
1312 }
1313 return false;
1314 }
1315
do_ip_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen,unsigned int flags)1316 static int do_ip_getsockopt(struct sock *sk, int level, int optname,
1317 char __user *optval, int __user *optlen, unsigned int flags)
1318 {
1319 struct inet_sock *inet = inet_sk(sk);
1320 bool needs_rtnl = getsockopt_needs_rtnl(optname);
1321 int val, err = 0;
1322 int len;
1323
1324 if (level != SOL_IP)
1325 return -EOPNOTSUPP;
1326
1327 if (ip_mroute_opt(optname))
1328 return ip_mroute_getsockopt(sk, optname, optval, optlen);
1329
1330 if (get_user(len, optlen))
1331 return -EFAULT;
1332 if (len < 0)
1333 return -EINVAL;
1334
1335 if (needs_rtnl)
1336 rtnl_lock();
1337 lock_sock(sk);
1338
1339 switch (optname) {
1340 case IP_OPTIONS:
1341 {
1342 unsigned char optbuf[sizeof(struct ip_options)+40];
1343 struct ip_options *opt = (struct ip_options *)optbuf;
1344 struct ip_options_rcu *inet_opt;
1345
1346 inet_opt = rcu_dereference_protected(inet->inet_opt,
1347 lockdep_sock_is_held(sk));
1348 opt->optlen = 0;
1349 if (inet_opt)
1350 memcpy(optbuf, &inet_opt->opt,
1351 sizeof(struct ip_options) +
1352 inet_opt->opt.optlen);
1353 release_sock(sk);
1354
1355 if (opt->optlen == 0)
1356 return put_user(0, optlen);
1357
1358 ip_options_undo(opt);
1359
1360 len = min_t(unsigned int, len, opt->optlen);
1361 if (put_user(len, optlen))
1362 return -EFAULT;
1363 if (copy_to_user(optval, opt->__data, len))
1364 return -EFAULT;
1365 return 0;
1366 }
1367 case IP_PKTINFO:
1368 val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
1369 break;
1370 case IP_RECVTTL:
1371 val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
1372 break;
1373 case IP_RECVTOS:
1374 val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
1375 break;
1376 case IP_RECVOPTS:
1377 val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
1378 break;
1379 case IP_RETOPTS:
1380 val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
1381 break;
1382 case IP_PASSSEC:
1383 val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
1384 break;
1385 case IP_RECVORIGDSTADDR:
1386 val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
1387 break;
1388 case IP_CHECKSUM:
1389 val = (inet->cmsg_flags & IP_CMSG_CHECKSUM) != 0;
1390 break;
1391 case IP_RECVFRAGSIZE:
1392 val = (inet->cmsg_flags & IP_CMSG_RECVFRAGSIZE) != 0;
1393 break;
1394 case IP_TOS:
1395 val = inet->tos;
1396 break;
1397 case IP_TTL:
1398 {
1399 struct net *net = sock_net(sk);
1400 val = (inet->uc_ttl == -1 ?
1401 net->ipv4.sysctl_ip_default_ttl :
1402 inet->uc_ttl);
1403 break;
1404 }
1405 case IP_HDRINCL:
1406 val = inet->hdrincl;
1407 break;
1408 case IP_NODEFRAG:
1409 val = inet->nodefrag;
1410 break;
1411 case IP_BIND_ADDRESS_NO_PORT:
1412 val = inet->bind_address_no_port;
1413 break;
1414 case IP_MTU_DISCOVER:
1415 val = inet->pmtudisc;
1416 break;
1417 case IP_MTU:
1418 {
1419 struct dst_entry *dst;
1420 val = 0;
1421 dst = sk_dst_get(sk);
1422 if (dst) {
1423 val = dst_mtu(dst);
1424 dst_release(dst);
1425 }
1426 if (!val) {
1427 release_sock(sk);
1428 return -ENOTCONN;
1429 }
1430 break;
1431 }
1432 case IP_RECVERR:
1433 val = inet->recverr;
1434 break;
1435 case IP_MULTICAST_TTL:
1436 val = inet->mc_ttl;
1437 break;
1438 case IP_MULTICAST_LOOP:
1439 val = inet->mc_loop;
1440 break;
1441 case IP_UNICAST_IF:
1442 val = (__force int)htonl((__u32) inet->uc_index);
1443 break;
1444 case IP_MULTICAST_IF:
1445 {
1446 struct in_addr addr;
1447 len = min_t(unsigned int, len, sizeof(struct in_addr));
1448 addr.s_addr = inet->mc_addr;
1449 release_sock(sk);
1450
1451 if (put_user(len, optlen))
1452 return -EFAULT;
1453 if (copy_to_user(optval, &addr, len))
1454 return -EFAULT;
1455 return 0;
1456 }
1457 case IP_MSFILTER:
1458 {
1459 struct ip_msfilter msf;
1460
1461 if (len < IP_MSFILTER_SIZE(0)) {
1462 err = -EINVAL;
1463 goto out;
1464 }
1465 if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
1466 err = -EFAULT;
1467 goto out;
1468 }
1469 err = ip_mc_msfget(sk, &msf,
1470 (struct ip_msfilter __user *)optval, optlen);
1471 goto out;
1472 }
1473 case MCAST_MSFILTER:
1474 {
1475 struct group_filter gsf;
1476
1477 if (len < GROUP_FILTER_SIZE(0)) {
1478 err = -EINVAL;
1479 goto out;
1480 }
1481 if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
1482 err = -EFAULT;
1483 goto out;
1484 }
1485 err = ip_mc_gsfget(sk, &gsf,
1486 (struct group_filter __user *)optval,
1487 optlen);
1488 goto out;
1489 }
1490 case IP_MULTICAST_ALL:
1491 val = inet->mc_all;
1492 break;
1493 case IP_PKTOPTIONS:
1494 {
1495 struct msghdr msg;
1496
1497 release_sock(sk);
1498
1499 if (sk->sk_type != SOCK_STREAM)
1500 return -ENOPROTOOPT;
1501
1502 msg.msg_control = (__force void *) optval;
1503 msg.msg_controllen = len;
1504 msg.msg_flags = flags;
1505
1506 if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
1507 struct in_pktinfo info;
1508
1509 info.ipi_addr.s_addr = inet->inet_rcv_saddr;
1510 info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
1511 info.ipi_ifindex = inet->mc_index;
1512 put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
1513 }
1514 if (inet->cmsg_flags & IP_CMSG_TTL) {
1515 int hlim = inet->mc_ttl;
1516 put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
1517 }
1518 if (inet->cmsg_flags & IP_CMSG_TOS) {
1519 int tos = inet->rcv_tos;
1520 put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
1521 }
1522 len -= msg.msg_controllen;
1523 return put_user(len, optlen);
1524 }
1525 case IP_FREEBIND:
1526 val = inet->freebind;
1527 break;
1528 case IP_TRANSPARENT:
1529 val = inet->transparent;
1530 break;
1531 case IP_MINTTL:
1532 val = inet->min_ttl;
1533 break;
1534 case IP_PROTOCOL:
1535 val = inet_sk(sk)->inet_num;
1536 break;
1537 default:
1538 release_sock(sk);
1539 return -ENOPROTOOPT;
1540 }
1541 release_sock(sk);
1542
1543 if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
1544 unsigned char ucval = (unsigned char)val;
1545 len = 1;
1546 if (put_user(len, optlen))
1547 return -EFAULT;
1548 if (copy_to_user(optval, &ucval, 1))
1549 return -EFAULT;
1550 } else {
1551 len = min_t(unsigned int, sizeof(int), len);
1552 if (put_user(len, optlen))
1553 return -EFAULT;
1554 if (copy_to_user(optval, &val, len))
1555 return -EFAULT;
1556 }
1557 return 0;
1558
1559 out:
1560 release_sock(sk);
1561 if (needs_rtnl)
1562 rtnl_unlock();
1563 return err;
1564 }
1565
ip_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)1566 int ip_getsockopt(struct sock *sk, int level,
1567 int optname, char __user *optval, int __user *optlen)
1568 {
1569 int err;
1570
1571 err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
1572 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1573 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1574 optname < BPFILTER_IPT_GET_MAX)
1575 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1576 #endif
1577 #ifdef CONFIG_NETFILTER
1578 /* we need to exclude all possible ENOPROTOOPTs except default case */
1579 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1580 !ip_mroute_opt(optname)) {
1581 int len;
1582
1583 if (get_user(len, optlen))
1584 return -EFAULT;
1585
1586 err = nf_getsockopt(sk, PF_INET, optname, optval, &len);
1587 if (err >= 0)
1588 err = put_user(len, optlen);
1589 return err;
1590 }
1591 #endif
1592 return err;
1593 }
1594 EXPORT_SYMBOL(ip_getsockopt);
1595
1596 #ifdef CONFIG_COMPAT
compat_ip_getsockopt(struct sock * sk,int level,int optname,char __user * optval,int __user * optlen)1597 int compat_ip_getsockopt(struct sock *sk, int level, int optname,
1598 char __user *optval, int __user *optlen)
1599 {
1600 int err;
1601
1602 if (optname == MCAST_MSFILTER)
1603 return compat_mc_getsockopt(sk, level, optname, optval, optlen,
1604 ip_getsockopt);
1605
1606 err = do_ip_getsockopt(sk, level, optname, optval, optlen,
1607 MSG_CMSG_COMPAT);
1608
1609 #if IS_ENABLED(CONFIG_BPFILTER_UMH)
1610 if (optname >= BPFILTER_IPT_SO_GET_INFO &&
1611 optname < BPFILTER_IPT_GET_MAX)
1612 err = bpfilter_ip_get_sockopt(sk, optname, optval, optlen);
1613 #endif
1614 #ifdef CONFIG_NETFILTER
1615 /* we need to exclude all possible ENOPROTOOPTs except default case */
1616 if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
1617 !ip_mroute_opt(optname)) {
1618 int len;
1619
1620 if (get_user(len, optlen))
1621 return -EFAULT;
1622
1623 err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
1624 if (err >= 0)
1625 err = put_user(len, optlen);
1626 return err;
1627 }
1628 #endif
1629 return err;
1630 }
1631 EXPORT_SYMBOL(compat_ip_getsockopt);
1632 #endif
1633