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