~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/ipv4/ip_sockglue.c

Version: ~ [ linux-5.11 ] ~ [ linux-5.10.17 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.99 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.176 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.221 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.257 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.257 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.85 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp