12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406 |
- /*
- * INET An implementation of the TCP/IP protocol suite for the LINUX
- * operating system. INET is implemented using the BSD Socket
- * interface as the means of communication with the user level.
- *
- * The IP to API glue.
- *
- * Authors: see ip.c
- *
- * Fixes:
- * Many : Split from ip.c , see ip.c for history.
- * Martin Mares : TOS setting fixed.
- * Alan Cox : Fixed a couple of oopses in Martin's
- * TOS tweaks.
- * Mike McLagan : Routing by source
- */
- #include <linux/module.h>
- #include <linux/types.h>
- #include <linux/mm.h>
- #include <linux/skbuff.h>
- #include <linux/ip.h>
- #include <linux/icmp.h>
- #include <linux/inetdevice.h>
- #include <linux/netdevice.h>
- #include <linux/slab.h>
- #include <net/sock.h>
- #include <net/ip.h>
- #include <net/icmp.h>
- #include <net/tcp_states.h>
- #include <linux/udp.h>
- #include <linux/igmp.h>
- #include <linux/netfilter.h>
- #include <linux/route.h>
- #include <linux/mroute.h>
- #include <net/inet_ecn.h>
- #include <net/route.h>
- #include <net/xfrm.h>
- #include <net/compat.h>
- #if IS_ENABLED(CONFIG_IPV6)
- #include <net/transp_v6.h>
- #endif
- #include <linux/errqueue.h>
- #include <asm/uaccess.h>
- #define IP_CMSG_PKTINFO 1
- #define IP_CMSG_TTL 2
- #define IP_CMSG_TOS 4
- #define IP_CMSG_RECVOPTS 8
- #define IP_CMSG_RETOPTS 16
- #define IP_CMSG_PASSSEC 32
- #define IP_CMSG_ORIGDSTADDR 64
- /*
- * SOL_IP control messages.
- */
- #define PKTINFO_SKB_CB(__skb) ((struct in_pktinfo *)((__skb)->cb))
- static void ip_cmsg_recv_pktinfo(struct msghdr *msg, struct sk_buff *skb)
- {
- struct in_pktinfo info = *PKTINFO_SKB_CB(skb);
- info.ipi_addr.s_addr = ip_hdr(skb)->daddr;
- put_cmsg(msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
- }
- static void ip_cmsg_recv_ttl(struct msghdr *msg, struct sk_buff *skb)
- {
- int ttl = ip_hdr(skb)->ttl;
- put_cmsg(msg, SOL_IP, IP_TTL, sizeof(int), &ttl);
- }
- static void ip_cmsg_recv_tos(struct msghdr *msg, struct sk_buff *skb)
- {
- put_cmsg(msg, SOL_IP, IP_TOS, 1, &ip_hdr(skb)->tos);
- }
- static void ip_cmsg_recv_opts(struct msghdr *msg, struct sk_buff *skb)
- {
- if (IPCB(skb)->opt.optlen == 0)
- return;
- put_cmsg(msg, SOL_IP, IP_RECVOPTS, IPCB(skb)->opt.optlen,
- ip_hdr(skb) + 1);
- }
- static void ip_cmsg_recv_retopts(struct msghdr *msg, struct sk_buff *skb)
- {
- unsigned char optbuf[sizeof(struct ip_options) + 40];
- struct ip_options * opt = (struct ip_options *)optbuf;
- if (IPCB(skb)->opt.optlen == 0)
- return;
- if (ip_options_echo(opt, skb)) {
- msg->msg_flags |= MSG_CTRUNC;
- return;
- }
- ip_options_undo(opt);
- put_cmsg(msg, SOL_IP, IP_RETOPTS, opt->optlen, opt->__data);
- }
- static void ip_cmsg_recv_security(struct msghdr *msg, struct sk_buff *skb)
- {
- char *secdata;
- u32 seclen, secid;
- int err;
- err = security_socket_getpeersec_dgram(NULL, skb, &secid);
- if (err)
- return;
- err = security_secid_to_secctx(secid, &secdata, &seclen);
- if (err)
- return;
- put_cmsg(msg, SOL_IP, SCM_SECURITY, seclen, secdata);
- security_release_secctx(secdata, seclen);
- }
- static void ip_cmsg_recv_dstaddr(struct msghdr *msg, struct sk_buff *skb)
- {
- struct sockaddr_in sin;
- const struct iphdr *iph = ip_hdr(skb);
- __be16 *ports = (__be16 *)skb_transport_header(skb);
- if (skb_transport_offset(skb) + 4 > skb->len)
- return;
- /* All current transport protocols have the port numbers in the
- * first four bytes of the transport header and this function is
- * written with this assumption in mind.
- */
- sin.sin_family = AF_INET;
- sin.sin_addr.s_addr = iph->daddr;
- sin.sin_port = ports[1];
- memset(sin.sin_zero, 0, sizeof(sin.sin_zero));
- put_cmsg(msg, SOL_IP, IP_ORIGDSTADDR, sizeof(sin), &sin);
- }
- void ip_cmsg_recv(struct msghdr *msg, struct sk_buff *skb)
- {
- struct inet_sock *inet = inet_sk(skb->sk);
- unsigned flags = inet->cmsg_flags;
- /* Ordered by supposed usage frequency */
- if (flags & 1)
- ip_cmsg_recv_pktinfo(msg, skb);
- if ((flags >>= 1) == 0)
- return;
- if (flags & 1)
- ip_cmsg_recv_ttl(msg, skb);
- if ((flags >>= 1) == 0)
- return;
- if (flags & 1)
- ip_cmsg_recv_tos(msg, skb);
- if ((flags >>= 1) == 0)
- return;
- if (flags & 1)
- ip_cmsg_recv_opts(msg, skb);
- if ((flags >>= 1) == 0)
- return;
- if (flags & 1)
- ip_cmsg_recv_retopts(msg, skb);
- if ((flags >>= 1) == 0)
- return;
- if (flags & 1)
- ip_cmsg_recv_security(msg, skb);
- if ((flags >>= 1) == 0)
- return;
- if (flags & 1)
- ip_cmsg_recv_dstaddr(msg, skb);
- }
- EXPORT_SYMBOL(ip_cmsg_recv);
- int ip_cmsg_send(struct net *net, struct msghdr *msg, struct ipcm_cookie *ipc)
- {
- int err;
- struct cmsghdr *cmsg;
- for (cmsg = CMSG_FIRSTHDR(msg); cmsg; cmsg = CMSG_NXTHDR(msg, cmsg)) {
- if (!CMSG_OK(msg, cmsg))
- return -EINVAL;
- if (cmsg->cmsg_level != SOL_IP)
- continue;
- switch (cmsg->cmsg_type) {
- case IP_RETOPTS:
- err = cmsg->cmsg_len - CMSG_ALIGN(sizeof(struct cmsghdr));
- err = ip_options_get(net, &ipc->opt, CMSG_DATA(cmsg),
- err < 40 ? err : 40);
- if (err)
- return err;
- break;
- case IP_PKTINFO:
- {
- struct in_pktinfo *info;
- if (cmsg->cmsg_len != CMSG_LEN(sizeof(struct in_pktinfo)))
- return -EINVAL;
- info = (struct in_pktinfo *)CMSG_DATA(cmsg);
- ipc->oif = info->ipi_ifindex;
- ipc->addr = info->ipi_spec_dst.s_addr;
- break;
- }
- default:
- return -EINVAL;
- }
- }
- return 0;
- }
- /* Special input handler for packets caught by router alert option.
- They are selected only by protocol field, and then processed likely
- local ones; but only if someone wants them! Otherwise, router
- not running rsvpd will kill RSVP.
- It is user level problem, what it will make with them.
- I have no idea, how it will masquearde or NAT them (it is joke, joke :-)),
- but receiver should be enough clever f.e. to forward mtrace requests,
- sent to multicast group to reach destination designated router.
- */
- struct ip_ra_chain __rcu *ip_ra_chain;
- static DEFINE_SPINLOCK(ip_ra_lock);
- static void ip_ra_destroy_rcu(struct rcu_head *head)
- {
- struct ip_ra_chain *ra = container_of(head, struct ip_ra_chain, rcu);
- sock_put(ra->saved_sk);
- kfree(ra);
- }
- int ip_ra_control(struct sock *sk, unsigned char on,
- void (*destructor)(struct sock *))
- {
- struct ip_ra_chain *ra, *new_ra;
- struct ip_ra_chain __rcu **rap;
- if (sk->sk_type != SOCK_RAW || inet_sk(sk)->inet_num == IPPROTO_RAW)
- return -EINVAL;
- new_ra = on ? kmalloc(sizeof(*new_ra), GFP_KERNEL) : NULL;
- spin_lock_bh(&ip_ra_lock);
- for (rap = &ip_ra_chain;
- (ra = rcu_dereference_protected(*rap,
- lockdep_is_held(&ip_ra_lock))) != NULL;
- rap = &ra->next) {
- if (ra->sk == sk) {
- if (on) {
- spin_unlock_bh(&ip_ra_lock);
- kfree(new_ra);
- return -EADDRINUSE;
- }
- /* dont let ip_call_ra_chain() use sk again */
- ra->sk = NULL;
- rcu_assign_pointer(*rap, ra->next);
- spin_unlock_bh(&ip_ra_lock);
- if (ra->destructor)
- ra->destructor(sk);
- /*
- * Delay sock_put(sk) and kfree(ra) after one rcu grace
- * period. This guarantee ip_call_ra_chain() dont need
- * to mess with socket refcounts.
- */
- ra->saved_sk = sk;
- call_rcu(&ra->rcu, ip_ra_destroy_rcu);
- return 0;
- }
- }
- if (new_ra == NULL) {
- spin_unlock_bh(&ip_ra_lock);
- return -ENOBUFS;
- }
- new_ra->sk = sk;
- new_ra->destructor = destructor;
- new_ra->next = ra;
- rcu_assign_pointer(*rap, new_ra);
- sock_hold(sk);
- spin_unlock_bh(&ip_ra_lock);
- return 0;
- }
- void ip_icmp_error(struct sock *sk, struct sk_buff *skb, int err,
- __be16 port, u32 info, u8 *payload)
- {
- struct sock_exterr_skb *serr;
- skb = skb_clone(skb, GFP_ATOMIC);
- if (!skb)
- return;
- serr = SKB_EXT_ERR(skb);
- serr->ee.ee_errno = err;
- serr->ee.ee_origin = SO_EE_ORIGIN_ICMP;
- serr->ee.ee_type = icmp_hdr(skb)->type;
- serr->ee.ee_code = icmp_hdr(skb)->code;
- serr->ee.ee_pad = 0;
- serr->ee.ee_info = info;
- serr->ee.ee_data = 0;
- serr->addr_offset = (u8 *)&(((struct iphdr *)(icmp_hdr(skb) + 1))->daddr) -
- skb_network_header(skb);
- serr->port = port;
- if (skb_pull(skb, payload - skb->data) != NULL) {
- skb_reset_transport_header(skb);
- if (sock_queue_err_skb(sk, skb) == 0)
- return;
- }
- kfree_skb(skb);
- }
- void ip_local_error(struct sock *sk, int err, __be32 daddr, __be16 port, u32 info)
- {
- struct inet_sock *inet = inet_sk(sk);
- struct sock_exterr_skb *serr;
- struct iphdr *iph;
- struct sk_buff *skb;
- if (!inet->recverr)
- return;
- skb = alloc_skb(sizeof(struct iphdr), GFP_ATOMIC);
- if (!skb)
- return;
- skb_put(skb, sizeof(struct iphdr));
- skb_reset_network_header(skb);
- iph = ip_hdr(skb);
- iph->daddr = daddr;
- serr = SKB_EXT_ERR(skb);
- serr->ee.ee_errno = err;
- serr->ee.ee_origin = SO_EE_ORIGIN_LOCAL;
- serr->ee.ee_type = 0;
- serr->ee.ee_code = 0;
- serr->ee.ee_pad = 0;
- serr->ee.ee_info = info;
- serr->ee.ee_data = 0;
- serr->addr_offset = (u8 *)&iph->daddr - skb_network_header(skb);
- serr->port = port;
- __skb_pull(skb, skb_tail_pointer(skb) - skb->data);
- skb_reset_transport_header(skb);
- if (sock_queue_err_skb(sk, skb))
- kfree_skb(skb);
- }
- /*
- * Handle MSG_ERRQUEUE
- */
- int ip_recv_error(struct sock *sk, struct msghdr *msg, int len, int *addr_len)
- {
- struct sock_exterr_skb *serr;
- struct sk_buff *skb, *skb2;
- struct sockaddr_in *sin;
- struct {
- struct sock_extended_err ee;
- struct sockaddr_in offender;
- } errhdr;
- int err;
- int copied;
- err = -EAGAIN;
- skb = skb_dequeue(&sk->sk_error_queue);
- if (skb == NULL)
- goto out;
- copied = skb->len;
- if (copied > len) {
- msg->msg_flags |= MSG_TRUNC;
- copied = len;
- }
- err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
- if (err)
- goto out_free_skb;
- sock_recv_timestamp(msg, sk, skb);
- serr = SKB_EXT_ERR(skb);
- sin = (struct sockaddr_in *)msg->msg_name;
- if (sin) {
- sin->sin_family = AF_INET;
- sin->sin_addr.s_addr = *(__be32 *)(skb_network_header(skb) +
- serr->addr_offset);
- sin->sin_port = serr->port;
- memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
- *addr_len = sizeof(*sin);
- }
- memcpy(&errhdr.ee, &serr->ee, sizeof(struct sock_extended_err));
- sin = &errhdr.offender;
- sin->sin_family = AF_UNSPEC;
- if (serr->ee.ee_origin == SO_EE_ORIGIN_ICMP) {
- struct inet_sock *inet = inet_sk(sk);
- sin->sin_family = AF_INET;
- sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
- sin->sin_port = 0;
- memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
- if (inet->cmsg_flags)
- ip_cmsg_recv(msg, skb);
- }
- put_cmsg(msg, SOL_IP, IP_RECVERR, sizeof(errhdr), &errhdr);
- /* Now we could try to dump offended packet options */
- msg->msg_flags |= MSG_ERRQUEUE;
- err = copied;
- /* Reset and regenerate socket error */
- spin_lock_bh(&sk->sk_error_queue.lock);
- sk->sk_err = 0;
- skb2 = skb_peek(&sk->sk_error_queue);
- if (skb2 != NULL) {
- sk->sk_err = SKB_EXT_ERR(skb2)->ee.ee_errno;
- spin_unlock_bh(&sk->sk_error_queue.lock);
- sk->sk_error_report(sk);
- } else
- spin_unlock_bh(&sk->sk_error_queue.lock);
- out_free_skb:
- kfree_skb(skb);
- out:
- return err;
- }
- /*
- * Socket option code for IP. This is the end of the line after any
- * TCP,UDP etc options on an IP socket.
- */
- static int do_ip_setsockopt(struct sock *sk, int level,
- int optname, char __user *optval, unsigned int optlen)
- {
- struct inet_sock *inet = inet_sk(sk);
- int val = 0, err;
- switch (optname) {
- case IP_PKTINFO:
- case IP_RECVTTL:
- case IP_RECVOPTS:
- case IP_RECVTOS:
- case IP_RETOPTS:
- case IP_TOS:
- case IP_TTL:
- case IP_HDRINCL:
- case IP_MTU_DISCOVER:
- case IP_RECVERR:
- case IP_ROUTER_ALERT:
- case IP_FREEBIND:
- case IP_PASSSEC:
- case IP_TRANSPARENT:
- case IP_MINTTL:
- case IP_NODEFRAG:
- case IP_UNICAST_IF:
- case IP_MULTICAST_TTL:
- case IP_MULTICAST_ALL:
- case IP_MULTICAST_LOOP:
- case IP_RECVORIGDSTADDR:
- if (optlen >= sizeof(int)) {
- if (get_user(val, (int __user *) optval))
- return -EFAULT;
- } else if (optlen >= sizeof(char)) {
- unsigned char ucval;
- if (get_user(ucval, (unsigned char __user *) optval))
- return -EFAULT;
- val = (int) ucval;
- }
- }
- /* If optlen==0, it is equivalent to val == 0 */
- if (ip_mroute_opt(optname))
- return ip_mroute_setsockopt(sk, optname, optval, optlen);
- err = 0;
- lock_sock(sk);
- switch (optname) {
- case IP_OPTIONS:
- {
- struct ip_options_rcu *old, *opt = NULL;
- if (optlen > 40)
- goto e_inval;
- err = ip_options_get_from_user(sock_net(sk), &opt,
- optval, optlen);
- if (err)
- break;
- old = rcu_dereference_protected(inet->inet_opt,
- sock_owned_by_user(sk));
- if (inet->is_icsk) {
- struct inet_connection_sock *icsk = inet_csk(sk);
- #if IS_ENABLED(CONFIG_IPV6)
- if (sk->sk_family == PF_INET ||
- (!((1 << sk->sk_state) &
- (TCPF_LISTEN | TCPF_CLOSE)) &&
- inet->inet_daddr != LOOPBACK4_IPV6)) {
- #endif
- if (old)
- icsk->icsk_ext_hdr_len -= old->opt.optlen;
- if (opt)
- icsk->icsk_ext_hdr_len += opt->opt.optlen;
- icsk->icsk_sync_mss(sk, icsk->icsk_pmtu_cookie);
- #if IS_ENABLED(CONFIG_IPV6)
- }
- #endif
- }
- rcu_assign_pointer(inet->inet_opt, opt);
- if (old)
- kfree_rcu(old, rcu);
- break;
- }
- case IP_PKTINFO:
- if (val)
- inet->cmsg_flags |= IP_CMSG_PKTINFO;
- else
- inet->cmsg_flags &= ~IP_CMSG_PKTINFO;
- break;
- case IP_RECVTTL:
- if (val)
- inet->cmsg_flags |= IP_CMSG_TTL;
- else
- inet->cmsg_flags &= ~IP_CMSG_TTL;
- break;
- case IP_RECVTOS:
- if (val)
- inet->cmsg_flags |= IP_CMSG_TOS;
- else
- inet->cmsg_flags &= ~IP_CMSG_TOS;
- break;
- case IP_RECVOPTS:
- if (val)
- inet->cmsg_flags |= IP_CMSG_RECVOPTS;
- else
- inet->cmsg_flags &= ~IP_CMSG_RECVOPTS;
- break;
- case IP_RETOPTS:
- if (val)
- inet->cmsg_flags |= IP_CMSG_RETOPTS;
- else
- inet->cmsg_flags &= ~IP_CMSG_RETOPTS;
- break;
- case IP_PASSSEC:
- if (val)
- inet->cmsg_flags |= IP_CMSG_PASSSEC;
- else
- inet->cmsg_flags &= ~IP_CMSG_PASSSEC;
- break;
- case IP_RECVORIGDSTADDR:
- if (val)
- inet->cmsg_flags |= IP_CMSG_ORIGDSTADDR;
- else
- inet->cmsg_flags &= ~IP_CMSG_ORIGDSTADDR;
- break;
- case IP_TOS: /* This sets both TOS and Precedence */
- if (sk->sk_type == SOCK_STREAM) {
- val &= ~INET_ECN_MASK;
- val |= inet->tos & INET_ECN_MASK;
- }
- if (inet->tos != val) {
- inet->tos = val;
- sk->sk_priority = rt_tos2priority(val);
- sk_dst_reset(sk);
- }
- break;
- case IP_TTL:
- if (optlen < 1)
- goto e_inval;
- if (val != -1 && (val < 1 || val > 255))
- goto e_inval;
- inet->uc_ttl = val;
- break;
- case IP_HDRINCL:
- if (sk->sk_type != SOCK_RAW) {
- err = -ENOPROTOOPT;
- break;
- }
- inet->hdrincl = val ? 1 : 0;
- break;
- case IP_NODEFRAG:
- if (sk->sk_type != SOCK_RAW) {
- err = -ENOPROTOOPT;
- break;
- }
- inet->nodefrag = val ? 1 : 0;
- break;
- case IP_MTU_DISCOVER:
- if (val < IP_PMTUDISC_DONT || val > IP_PMTUDISC_PROBE)
- goto e_inval;
- inet->pmtudisc = val;
- break;
- case IP_RECVERR:
- inet->recverr = !!val;
- if (!val)
- skb_queue_purge(&sk->sk_error_queue);
- break;
- case IP_MULTICAST_TTL:
- if (sk->sk_type == SOCK_STREAM)
- goto e_inval;
- if (optlen < 1)
- goto e_inval;
- if (val == -1)
- val = 1;
- if (val < 0 || val > 255)
- goto e_inval;
- inet->mc_ttl = val;
- break;
- case IP_MULTICAST_LOOP:
- if (optlen < 1)
- goto e_inval;
- inet->mc_loop = !!val;
- break;
- case IP_UNICAST_IF:
- {
- struct net_device *dev = NULL;
- int ifindex;
- if (optlen != sizeof(int))
- goto e_inval;
- ifindex = (__force int)ntohl((__force __be32)val);
- if (ifindex == 0) {
- inet->uc_index = 0;
- err = 0;
- break;
- }
- dev = dev_get_by_index(sock_net(sk), ifindex);
- err = -EADDRNOTAVAIL;
- if (!dev)
- break;
- dev_put(dev);
- err = -EINVAL;
- if (sk->sk_bound_dev_if)
- break;
- inet->uc_index = ifindex;
- err = 0;
- break;
- }
- case IP_MULTICAST_IF:
- {
- struct ip_mreqn mreq;
- struct net_device *dev = NULL;
- if (sk->sk_type == SOCK_STREAM)
- goto e_inval;
- /*
- * Check the arguments are allowable
- */
- if (optlen < sizeof(struct in_addr))
- goto e_inval;
- err = -EFAULT;
- if (optlen >= sizeof(struct ip_mreqn)) {
- if (copy_from_user(&mreq, optval, sizeof(mreq)))
- break;
- } else {
- memset(&mreq, 0, sizeof(mreq));
- if (optlen >= sizeof(struct in_addr) &&
- copy_from_user(&mreq.imr_address, optval,
- sizeof(struct in_addr)))
- break;
- }
- if (!mreq.imr_ifindex) {
- if (mreq.imr_address.s_addr == htonl(INADDR_ANY)) {
- inet->mc_index = 0;
- inet->mc_addr = 0;
- err = 0;
- break;
- }
- dev = ip_dev_find(sock_net(sk), mreq.imr_address.s_addr);
- if (dev)
- mreq.imr_ifindex = dev->ifindex;
- } else
- dev = dev_get_by_index(sock_net(sk), mreq.imr_ifindex);
- err = -EADDRNOTAVAIL;
- if (!dev)
- break;
- dev_put(dev);
- err = -EINVAL;
- if (sk->sk_bound_dev_if &&
- mreq.imr_ifindex != sk->sk_bound_dev_if)
- break;
- inet->mc_index = mreq.imr_ifindex;
- inet->mc_addr = mreq.imr_address.s_addr;
- err = 0;
- break;
- }
- case IP_ADD_MEMBERSHIP:
- case IP_DROP_MEMBERSHIP:
- {
- struct ip_mreqn mreq;
- err = -EPROTO;
- if (inet_sk(sk)->is_icsk)
- break;
- if (optlen < sizeof(struct ip_mreq))
- goto e_inval;
- err = -EFAULT;
- if (optlen >= sizeof(struct ip_mreqn)) {
- if (copy_from_user(&mreq, optval, sizeof(mreq)))
- break;
- } else {
- memset(&mreq, 0, sizeof(mreq));
- if (copy_from_user(&mreq, optval, sizeof(struct ip_mreq)))
- break;
- }
- if (optname == IP_ADD_MEMBERSHIP)
- err = ip_mc_join_group(sk, &mreq);
- else
- err = ip_mc_leave_group(sk, &mreq);
- break;
- }
- case IP_MSFILTER:
- {
- struct ip_msfilter *msf;
- if (optlen < IP_MSFILTER_SIZE(0))
- goto e_inval;
- if (optlen > sysctl_optmem_max) {
- err = -ENOBUFS;
- break;
- }
- msf = kmalloc(optlen, GFP_KERNEL);
- if (!msf) {
- err = -ENOBUFS;
- break;
- }
- err = -EFAULT;
- if (copy_from_user(msf, optval, optlen)) {
- kfree(msf);
- break;
- }
- /* numsrc >= (1G-4) overflow in 32 bits */
- if (msf->imsf_numsrc >= 0x3ffffffcU ||
- msf->imsf_numsrc > sysctl_igmp_max_msf) {
- kfree(msf);
- err = -ENOBUFS;
- break;
- }
- if (IP_MSFILTER_SIZE(msf->imsf_numsrc) > optlen) {
- kfree(msf);
- err = -EINVAL;
- break;
- }
- err = ip_mc_msfilter(sk, msf, 0);
- kfree(msf);
- break;
- }
- case IP_BLOCK_SOURCE:
- case IP_UNBLOCK_SOURCE:
- case IP_ADD_SOURCE_MEMBERSHIP:
- case IP_DROP_SOURCE_MEMBERSHIP:
- {
- struct ip_mreq_source mreqs;
- int omode, add;
- if (optlen != sizeof(struct ip_mreq_source))
- goto e_inval;
- if (copy_from_user(&mreqs, optval, sizeof(mreqs))) {
- err = -EFAULT;
- break;
- }
- if (optname == IP_BLOCK_SOURCE) {
- omode = MCAST_EXCLUDE;
- add = 1;
- } else if (optname == IP_UNBLOCK_SOURCE) {
- omode = MCAST_EXCLUDE;
- add = 0;
- } else if (optname == IP_ADD_SOURCE_MEMBERSHIP) {
- struct ip_mreqn mreq;
- mreq.imr_multiaddr.s_addr = mreqs.imr_multiaddr;
- mreq.imr_address.s_addr = mreqs.imr_interface;
- mreq.imr_ifindex = 0;
- err = ip_mc_join_group(sk, &mreq);
- if (err && err != -EADDRINUSE)
- break;
- omode = MCAST_INCLUDE;
- add = 1;
- } else /* IP_DROP_SOURCE_MEMBERSHIP */ {
- omode = MCAST_INCLUDE;
- add = 0;
- }
- err = ip_mc_source(add, omode, sk, &mreqs, 0);
- break;
- }
- case MCAST_JOIN_GROUP:
- case MCAST_LEAVE_GROUP:
- {
- struct group_req greq;
- struct sockaddr_in *psin;
- struct ip_mreqn mreq;
- if (optlen < sizeof(struct group_req))
- goto e_inval;
- err = -EFAULT;
- if (copy_from_user(&greq, optval, sizeof(greq)))
- break;
- psin = (struct sockaddr_in *)&greq.gr_group;
- if (psin->sin_family != AF_INET)
- goto e_inval;
- memset(&mreq, 0, sizeof(mreq));
- mreq.imr_multiaddr = psin->sin_addr;
- mreq.imr_ifindex = greq.gr_interface;
- if (optname == MCAST_JOIN_GROUP)
- err = ip_mc_join_group(sk, &mreq);
- else
- err = ip_mc_leave_group(sk, &mreq);
- break;
- }
- case MCAST_JOIN_SOURCE_GROUP:
- case MCAST_LEAVE_SOURCE_GROUP:
- case MCAST_BLOCK_SOURCE:
- case MCAST_UNBLOCK_SOURCE:
- {
- struct group_source_req greqs;
- struct ip_mreq_source mreqs;
- struct sockaddr_in *psin;
- int omode, add;
- if (optlen != sizeof(struct group_source_req))
- goto e_inval;
- if (copy_from_user(&greqs, optval, sizeof(greqs))) {
- err = -EFAULT;
- break;
- }
- if (greqs.gsr_group.ss_family != AF_INET ||
- greqs.gsr_source.ss_family != AF_INET) {
- err = -EADDRNOTAVAIL;
- break;
- }
- psin = (struct sockaddr_in *)&greqs.gsr_group;
- mreqs.imr_multiaddr = psin->sin_addr.s_addr;
- psin = (struct sockaddr_in *)&greqs.gsr_source;
- mreqs.imr_sourceaddr = psin->sin_addr.s_addr;
- mreqs.imr_interface = 0; /* use index for mc_source */
- if (optname == MCAST_BLOCK_SOURCE) {
- omode = MCAST_EXCLUDE;
- add = 1;
- } else if (optname == MCAST_UNBLOCK_SOURCE) {
- omode = MCAST_EXCLUDE;
- add = 0;
- } else if (optname == MCAST_JOIN_SOURCE_GROUP) {
- struct ip_mreqn mreq;
- psin = (struct sockaddr_in *)&greqs.gsr_group;
- mreq.imr_multiaddr = psin->sin_addr;
- mreq.imr_address.s_addr = 0;
- mreq.imr_ifindex = greqs.gsr_interface;
- err = ip_mc_join_group(sk, &mreq);
- if (err && err != -EADDRINUSE)
- break;
- greqs.gsr_interface = mreq.imr_ifindex;
- omode = MCAST_INCLUDE;
- add = 1;
- } else /* MCAST_LEAVE_SOURCE_GROUP */ {
- omode = MCAST_INCLUDE;
- add = 0;
- }
- err = ip_mc_source(add, omode, sk, &mreqs,
- greqs.gsr_interface);
- break;
- }
- case MCAST_MSFILTER:
- {
- struct sockaddr_in *psin;
- struct ip_msfilter *msf = NULL;
- struct group_filter *gsf = NULL;
- int msize, i, ifindex;
- if (optlen < GROUP_FILTER_SIZE(0))
- goto e_inval;
- if (optlen > sysctl_optmem_max) {
- err = -ENOBUFS;
- break;
- }
- gsf = kmalloc(optlen, GFP_KERNEL);
- if (!gsf) {
- err = -ENOBUFS;
- break;
- }
- err = -EFAULT;
- if (copy_from_user(gsf, optval, optlen))
- goto mc_msf_out;
- /* numsrc >= (4G-140)/128 overflow in 32 bits */
- if (gsf->gf_numsrc >= 0x1ffffff ||
- gsf->gf_numsrc > sysctl_igmp_max_msf) {
- err = -ENOBUFS;
- goto mc_msf_out;
- }
- if (GROUP_FILTER_SIZE(gsf->gf_numsrc) > optlen) {
- err = -EINVAL;
- goto mc_msf_out;
- }
- msize = IP_MSFILTER_SIZE(gsf->gf_numsrc);
- msf = kmalloc(msize, GFP_KERNEL);
- if (!msf) {
- err = -ENOBUFS;
- goto mc_msf_out;
- }
- ifindex = gsf->gf_interface;
- psin = (struct sockaddr_in *)&gsf->gf_group;
- if (psin->sin_family != AF_INET) {
- err = -EADDRNOTAVAIL;
- goto mc_msf_out;
- }
- msf->imsf_multiaddr = psin->sin_addr.s_addr;
- msf->imsf_interface = 0;
- msf->imsf_fmode = gsf->gf_fmode;
- msf->imsf_numsrc = gsf->gf_numsrc;
- err = -EADDRNOTAVAIL;
- for (i = 0; i < gsf->gf_numsrc; ++i) {
- psin = (struct sockaddr_in *)&gsf->gf_slist[i];
- if (psin->sin_family != AF_INET)
- goto mc_msf_out;
- msf->imsf_slist[i] = psin->sin_addr.s_addr;
- }
- kfree(gsf);
- gsf = NULL;
- err = ip_mc_msfilter(sk, msf, ifindex);
- mc_msf_out:
- kfree(msf);
- kfree(gsf);
- break;
- }
- case IP_MULTICAST_ALL:
- if (optlen < 1)
- goto e_inval;
- if (val != 0 && val != 1)
- goto e_inval;
- inet->mc_all = val;
- break;
- case IP_ROUTER_ALERT:
- err = ip_ra_control(sk, val ? 1 : 0, NULL);
- break;
- case IP_FREEBIND:
- if (optlen < 1)
- goto e_inval;
- inet->freebind = !!val;
- break;
- case IP_IPSEC_POLICY:
- case IP_XFRM_POLICY:
- err = -EPERM;
- if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
- break;
- err = xfrm_user_policy(sk, optname, optval, optlen);
- break;
- case IP_TRANSPARENT:
- if (!!val && !ns_capable(sock_net(sk)->user_ns, CAP_NET_RAW) &&
- !ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN)) {
- err = -EPERM;
- break;
- }
- if (optlen < 1)
- goto e_inval;
- inet->transparent = !!val;
- break;
- case IP_MINTTL:
- if (optlen < 1)
- goto e_inval;
- if (val < 0 || val > 255)
- goto e_inval;
- inet->min_ttl = val;
- break;
- default:
- err = -ENOPROTOOPT;
- break;
- }
- release_sock(sk);
- return err;
- e_inval:
- release_sock(sk);
- return -EINVAL;
- }
- /**
- * ipv4_pktinfo_prepare - transfert some info from rtable to skb
- * @sk: socket
- * @skb: buffer
- *
- * To support IP_CMSG_PKTINFO option, we store rt_iif and rt_spec_dst
- * in skb->cb[] before dst drop.
- * This way, receiver doesnt make cache line misses to read rtable.
- */
- void ipv4_pktinfo_prepare(struct sk_buff *skb)
- {
- struct in_pktinfo *pktinfo = PKTINFO_SKB_CB(skb);
- const struct rtable *rt = skb_rtable(skb);
- if (rt) {
- pktinfo->ipi_ifindex = rt->rt_iif;
- pktinfo->ipi_spec_dst.s_addr = rt->rt_spec_dst;
- } else {
- pktinfo->ipi_ifindex = 0;
- pktinfo->ipi_spec_dst.s_addr = 0;
- }
- /* We need to keep the dst for __ip_options_echo()
- * We could restrict the test to opt.ts_needtime || opt.srr,
- * but the following is good enough as IP options are not often used.
- */
- if (unlikely(IPCB(skb)->opt.optlen))
- skb_dst_force(skb);
- else
- skb_dst_drop(skb);
- }
- int ip_setsockopt(struct sock *sk, int level,
- int optname, char __user *optval, unsigned int optlen)
- {
- int err;
- if (level != SOL_IP)
- return -ENOPROTOOPT;
- err = do_ip_setsockopt(sk, level, optname, optval, optlen);
- #ifdef CONFIG_NETFILTER
- /* we need to exclude all possible ENOPROTOOPTs except default case */
- if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
- optname != IP_IPSEC_POLICY &&
- optname != IP_XFRM_POLICY &&
- !ip_mroute_opt(optname)) {
- lock_sock(sk);
- err = nf_setsockopt(sk, PF_INET, optname, optval, optlen);
- release_sock(sk);
- }
- #endif
- return err;
- }
- EXPORT_SYMBOL(ip_setsockopt);
- #ifdef CONFIG_COMPAT
- int compat_ip_setsockopt(struct sock *sk, int level, int optname,
- char __user *optval, unsigned int optlen)
- {
- int err;
- if (level != SOL_IP)
- return -ENOPROTOOPT;
- if (optname >= MCAST_JOIN_GROUP && optname <= MCAST_MSFILTER)
- return compat_mc_setsockopt(sk, level, optname, optval, optlen,
- ip_setsockopt);
- err = do_ip_setsockopt(sk, level, optname, optval, optlen);
- #ifdef CONFIG_NETFILTER
- /* we need to exclude all possible ENOPROTOOPTs except default case */
- if (err == -ENOPROTOOPT && optname != IP_HDRINCL &&
- optname != IP_IPSEC_POLICY &&
- optname != IP_XFRM_POLICY &&
- !ip_mroute_opt(optname)) {
- lock_sock(sk);
- err = compat_nf_setsockopt(sk, PF_INET, optname,
- optval, optlen);
- release_sock(sk);
- }
- #endif
- return err;
- }
- EXPORT_SYMBOL(compat_ip_setsockopt);
- #endif
- /*
- * Get the options. Note for future reference. The GET of IP options gets
- * the _received_ ones. The set sets the _sent_ ones.
- */
- static int do_ip_getsockopt(struct sock *sk, int level, int optname,
- char __user *optval, int __user *optlen, unsigned flags)
- {
- struct inet_sock *inet = inet_sk(sk);
- int val;
- int len;
- if (level != SOL_IP)
- return -EOPNOTSUPP;
- if (ip_mroute_opt(optname))
- return ip_mroute_getsockopt(sk, optname, optval, optlen);
- if (get_user(len, optlen))
- return -EFAULT;
- if (len < 0)
- return -EINVAL;
- lock_sock(sk);
- switch (optname) {
- case IP_OPTIONS:
- {
- unsigned char optbuf[sizeof(struct ip_options)+40];
- struct ip_options *opt = (struct ip_options *)optbuf;
- struct ip_options_rcu *inet_opt;
- inet_opt = rcu_dereference_protected(inet->inet_opt,
- sock_owned_by_user(sk));
- opt->optlen = 0;
- if (inet_opt)
- memcpy(optbuf, &inet_opt->opt,
- sizeof(struct ip_options) +
- inet_opt->opt.optlen);
- release_sock(sk);
- if (opt->optlen == 0)
- return put_user(0, optlen);
- ip_options_undo(opt);
- len = min_t(unsigned int, len, opt->optlen);
- if (put_user(len, optlen))
- return -EFAULT;
- if (copy_to_user(optval, opt->__data, len))
- return -EFAULT;
- return 0;
- }
- case IP_PKTINFO:
- val = (inet->cmsg_flags & IP_CMSG_PKTINFO) != 0;
- break;
- case IP_RECVTTL:
- val = (inet->cmsg_flags & IP_CMSG_TTL) != 0;
- break;
- case IP_RECVTOS:
- val = (inet->cmsg_flags & IP_CMSG_TOS) != 0;
- break;
- case IP_RECVOPTS:
- val = (inet->cmsg_flags & IP_CMSG_RECVOPTS) != 0;
- break;
- case IP_RETOPTS:
- val = (inet->cmsg_flags & IP_CMSG_RETOPTS) != 0;
- break;
- case IP_PASSSEC:
- val = (inet->cmsg_flags & IP_CMSG_PASSSEC) != 0;
- break;
- case IP_RECVORIGDSTADDR:
- val = (inet->cmsg_flags & IP_CMSG_ORIGDSTADDR) != 0;
- break;
- case IP_TOS:
- val = inet->tos;
- break;
- case IP_TTL:
- val = (inet->uc_ttl == -1 ?
- sysctl_ip_default_ttl :
- inet->uc_ttl);
- break;
- case IP_HDRINCL:
- val = inet->hdrincl;
- break;
- case IP_NODEFRAG:
- val = inet->nodefrag;
- break;
- case IP_MTU_DISCOVER:
- val = inet->pmtudisc;
- break;
- case IP_MTU:
- {
- struct dst_entry *dst;
- val = 0;
- dst = sk_dst_get(sk);
- if (dst) {
- val = dst_mtu(dst);
- dst_release(dst);
- }
- if (!val) {
- release_sock(sk);
- return -ENOTCONN;
- }
- break;
- }
- case IP_RECVERR:
- val = inet->recverr;
- break;
- case IP_MULTICAST_TTL:
- val = inet->mc_ttl;
- break;
- case IP_MULTICAST_LOOP:
- val = inet->mc_loop;
- break;
- case IP_UNICAST_IF:
- val = (__force int)htonl((__u32) inet->uc_index);
- break;
- case IP_MULTICAST_IF:
- {
- struct in_addr addr;
- len = min_t(unsigned int, len, sizeof(struct in_addr));
- addr.s_addr = inet->mc_addr;
- release_sock(sk);
- if (put_user(len, optlen))
- return -EFAULT;
- if (copy_to_user(optval, &addr, len))
- return -EFAULT;
- return 0;
- }
- case IP_MSFILTER:
- {
- struct ip_msfilter msf;
- int err;
- if (len < IP_MSFILTER_SIZE(0)) {
- release_sock(sk);
- return -EINVAL;
- }
- if (copy_from_user(&msf, optval, IP_MSFILTER_SIZE(0))) {
- release_sock(sk);
- return -EFAULT;
- }
- err = ip_mc_msfget(sk, &msf,
- (struct ip_msfilter __user *)optval, optlen);
- release_sock(sk);
- return err;
- }
- case MCAST_MSFILTER:
- {
- struct group_filter gsf;
- int err;
- if (len < GROUP_FILTER_SIZE(0)) {
- release_sock(sk);
- return -EINVAL;
- }
- if (copy_from_user(&gsf, optval, GROUP_FILTER_SIZE(0))) {
- release_sock(sk);
- return -EFAULT;
- }
- err = ip_mc_gsfget(sk, &gsf,
- (struct group_filter __user *)optval,
- optlen);
- release_sock(sk);
- return err;
- }
- case IP_MULTICAST_ALL:
- val = inet->mc_all;
- break;
- case IP_PKTOPTIONS:
- {
- struct msghdr msg;
- release_sock(sk);
- if (sk->sk_type != SOCK_STREAM)
- return -ENOPROTOOPT;
- msg.msg_control = optval;
- msg.msg_controllen = len;
- msg.msg_flags = flags;
- if (inet->cmsg_flags & IP_CMSG_PKTINFO) {
- struct in_pktinfo info;
- info.ipi_addr.s_addr = inet->inet_rcv_saddr;
- info.ipi_spec_dst.s_addr = inet->inet_rcv_saddr;
- info.ipi_ifindex = inet->mc_index;
- put_cmsg(&msg, SOL_IP, IP_PKTINFO, sizeof(info), &info);
- }
- if (inet->cmsg_flags & IP_CMSG_TTL) {
- int hlim = inet->mc_ttl;
- put_cmsg(&msg, SOL_IP, IP_TTL, sizeof(hlim), &hlim);
- }
- if (inet->cmsg_flags & IP_CMSG_TOS) {
- int tos = inet->rcv_tos;
- put_cmsg(&msg, SOL_IP, IP_TOS, sizeof(tos), &tos);
- }
- len -= msg.msg_controllen;
- return put_user(len, optlen);
- }
- case IP_FREEBIND:
- val = inet->freebind;
- break;
- case IP_TRANSPARENT:
- val = inet->transparent;
- break;
- case IP_MINTTL:
- val = inet->min_ttl;
- break;
- default:
- release_sock(sk);
- return -ENOPROTOOPT;
- }
- release_sock(sk);
- if (len < sizeof(int) && len > 0 && val >= 0 && val <= 255) {
- unsigned char ucval = (unsigned char)val;
- len = 1;
- if (put_user(len, optlen))
- return -EFAULT;
- if (copy_to_user(optval, &ucval, 1))
- return -EFAULT;
- } else {
- len = min_t(unsigned int, sizeof(int), len);
- if (put_user(len, optlen))
- return -EFAULT;
- if (copy_to_user(optval, &val, len))
- return -EFAULT;
- }
- return 0;
- }
- int ip_getsockopt(struct sock *sk, int level,
- int optname, char __user *optval, int __user *optlen)
- {
- int err;
- err = do_ip_getsockopt(sk, level, optname, optval, optlen, 0);
- #ifdef CONFIG_NETFILTER
- /* we need to exclude all possible ENOPROTOOPTs except default case */
- if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
- !ip_mroute_opt(optname)) {
- int len;
- if (get_user(len, optlen))
- return -EFAULT;
- lock_sock(sk);
- err = nf_getsockopt(sk, PF_INET, optname, optval,
- &len);
- release_sock(sk);
- if (err >= 0)
- err = put_user(len, optlen);
- return err;
- }
- #endif
- return err;
- }
- EXPORT_SYMBOL(ip_getsockopt);
- #ifdef CONFIG_COMPAT
- int compat_ip_getsockopt(struct sock *sk, int level, int optname,
- char __user *optval, int __user *optlen)
- {
- int err;
- if (optname == MCAST_MSFILTER)
- return compat_mc_getsockopt(sk, level, optname, optval, optlen,
- ip_getsockopt);
- err = do_ip_getsockopt(sk, level, optname, optval, optlen,
- MSG_CMSG_COMPAT);
- #ifdef CONFIG_NETFILTER
- /* we need to exclude all possible ENOPROTOOPTs except default case */
- if (err == -ENOPROTOOPT && optname != IP_PKTOPTIONS &&
- !ip_mroute_opt(optname)) {
- int len;
- if (get_user(len, optlen))
- return -EFAULT;
- lock_sock(sk);
- err = compat_nf_getsockopt(sk, PF_INET, optname, optval, &len);
- release_sock(sk);
- if (err >= 0)
- err = put_user(len, optlen);
- return err;
- }
- #endif
- return err;
- }
- EXPORT_SYMBOL(compat_ip_getsockopt);
- #endif
|