raw.c 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. /*
  2. * INET An implementation of the TCP/IP protocol suite for the LINUX
  3. * operating system. INET is implemented using the BSD Socket
  4. * interface as the means of communication with the user level.
  5. *
  6. * RAW - implementation of IP "raw" sockets.
  7. *
  8. * Authors: Ross Biro
  9. * Fred N. van Kempen, <waltje@uWalt.NL.Mugnet.ORG>
  10. *
  11. * Fixes:
  12. * Alan Cox : verify_area() fixed up
  13. * Alan Cox : ICMP error handling
  14. * Alan Cox : EMSGSIZE if you send too big a packet
  15. * Alan Cox : Now uses generic datagrams and shared
  16. * skbuff library. No more peek crashes,
  17. * no more backlogs
  18. * Alan Cox : Checks sk->broadcast.
  19. * Alan Cox : Uses skb_free_datagram/skb_copy_datagram
  20. * Alan Cox : Raw passes ip options too
  21. * Alan Cox : Setsocketopt added
  22. * Alan Cox : Fixed error return for broadcasts
  23. * Alan Cox : Removed wake_up calls
  24. * Alan Cox : Use ttl/tos
  25. * Alan Cox : Cleaned up old debugging
  26. * Alan Cox : Use new kernel side addresses
  27. * Arnt Gulbrandsen : Fixed MSG_DONTROUTE in raw sockets.
  28. * Alan Cox : BSD style RAW socket demultiplexing.
  29. * Alan Cox : Beginnings of mrouted support.
  30. * Alan Cox : Added IP_HDRINCL option.
  31. * Alan Cox : Skip broadcast check if BSDism set.
  32. * David S. Miller : New socket lookup architecture.
  33. *
  34. * This program is free software; you can redistribute it and/or
  35. * modify it under the terms of the GNU General Public License
  36. * as published by the Free Software Foundation; either version
  37. * 2 of the License, or (at your option) any later version.
  38. */
  39. #include <linux/types.h>
  40. #include <linux/atomic.h>
  41. #include <asm/byteorder.h>
  42. #include <asm/current.h>
  43. #include <asm/uaccess.h>
  44. #include <asm/ioctls.h>
  45. #include <linux/stddef.h>
  46. #include <linux/slab.h>
  47. #include <linux/errno.h>
  48. #include <linux/kernel.h>
  49. #include <linux/export.h>
  50. #include <linux/spinlock.h>
  51. #include <linux/sockios.h>
  52. #include <linux/socket.h>
  53. #include <linux/in.h>
  54. #include <linux/mroute.h>
  55. #include <linux/netdevice.h>
  56. #include <linux/in_route.h>
  57. #include <linux/route.h>
  58. #include <linux/skbuff.h>
  59. #include <linux/igmp.h>
  60. #include <net/net_namespace.h>
  61. #include <net/dst.h>
  62. #include <net/sock.h>
  63. #include <linux/ip.h>
  64. #include <linux/net.h>
  65. #include <net/ip.h>
  66. #include <net/icmp.h>
  67. #include <net/udp.h>
  68. #include <net/raw.h>
  69. #include <net/snmp.h>
  70. #include <net/tcp_states.h>
  71. #include <net/inet_common.h>
  72. #include <net/checksum.h>
  73. #include <net/xfrm.h>
  74. #include <linux/rtnetlink.h>
  75. #include <linux/proc_fs.h>
  76. #include <linux/seq_file.h>
  77. #include <linux/netfilter.h>
  78. #include <linux/netfilter_ipv4.h>
  79. #include <linux/compat.h>
  80. #include <linux/uio.h>
  81. struct raw_frag_vec {
  82. struct msghdr *msg;
  83. union {
  84. struct icmphdr icmph;
  85. char c[1];
  86. } hdr;
  87. int hlen;
  88. };
  89. static struct raw_hashinfo raw_v4_hashinfo = {
  90. .lock = __RW_LOCK_UNLOCKED(raw_v4_hashinfo.lock),
  91. };
  92. void raw_hash_sk(struct sock *sk)
  93. {
  94. struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
  95. struct hlist_head *head;
  96. head = &h->ht[inet_sk(sk)->inet_num & (RAW_HTABLE_SIZE - 1)];
  97. write_lock_bh(&h->lock);
  98. sk_add_node(sk, head);
  99. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, 1);
  100. write_unlock_bh(&h->lock);
  101. }
  102. EXPORT_SYMBOL_GPL(raw_hash_sk);
  103. void raw_unhash_sk(struct sock *sk)
  104. {
  105. struct raw_hashinfo *h = sk->sk_prot->h.raw_hash;
  106. write_lock_bh(&h->lock);
  107. if (sk_del_node_init(sk))
  108. sock_prot_inuse_add(sock_net(sk), sk->sk_prot, -1);
  109. write_unlock_bh(&h->lock);
  110. }
  111. EXPORT_SYMBOL_GPL(raw_unhash_sk);
  112. static struct sock *__raw_v4_lookup(struct net *net, struct sock *sk,
  113. unsigned short num, __be32 raddr, __be32 laddr, int dif)
  114. {
  115. sk_for_each_from(sk) {
  116. struct inet_sock *inet = inet_sk(sk);
  117. if (net_eq(sock_net(sk), net) && inet->inet_num == num &&
  118. !(inet->inet_daddr && inet->inet_daddr != raddr) &&
  119. !(inet->inet_rcv_saddr && inet->inet_rcv_saddr != laddr) &&
  120. !(sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif))
  121. goto found; /* gotcha */
  122. }
  123. sk = NULL;
  124. found:
  125. return sk;
  126. }
  127. /*
  128. * 0 - deliver
  129. * 1 - block
  130. */
  131. static int icmp_filter(const struct sock *sk, const struct sk_buff *skb)
  132. {
  133. struct icmphdr _hdr;
  134. const struct icmphdr *hdr;
  135. hdr = skb_header_pointer(skb, skb_transport_offset(skb),
  136. sizeof(_hdr), &_hdr);
  137. if (!hdr)
  138. return 1;
  139. if (hdr->type < 32) {
  140. __u32 data = raw_sk(sk)->filter.data;
  141. return ((1U << hdr->type) & data) != 0;
  142. }
  143. /* Do not block unknown ICMP types */
  144. return 0;
  145. }
  146. /* IP input processing comes here for RAW socket delivery.
  147. * Caller owns SKB, so we must make clones.
  148. *
  149. * RFC 1122: SHOULD pass TOS value up to the transport layer.
  150. * -> It does. And not only TOS, but all IP header.
  151. */
  152. static int raw_v4_input(struct sk_buff *skb, const struct iphdr *iph, int hash)
  153. {
  154. struct sock *sk;
  155. struct hlist_head *head;
  156. int delivered = 0;
  157. struct net *net;
  158. read_lock(&raw_v4_hashinfo.lock);
  159. head = &raw_v4_hashinfo.ht[hash];
  160. if (hlist_empty(head))
  161. goto out;
  162. net = dev_net(skb->dev);
  163. sk = __raw_v4_lookup(net, __sk_head(head), iph->protocol,
  164. iph->saddr, iph->daddr,
  165. skb->dev->ifindex);
  166. while (sk) {
  167. delivered = 1;
  168. if ((iph->protocol != IPPROTO_ICMP || !icmp_filter(sk, skb)) &&
  169. ip_mc_sf_allow(sk, iph->daddr, iph->saddr,
  170. skb->dev->ifindex)) {
  171. struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
  172. /* Not releasing hash table! */
  173. if (clone)
  174. raw_rcv(sk, clone);
  175. }
  176. sk = __raw_v4_lookup(net, sk_next(sk), iph->protocol,
  177. iph->saddr, iph->daddr,
  178. skb->dev->ifindex);
  179. }
  180. out:
  181. read_unlock(&raw_v4_hashinfo.lock);
  182. return delivered;
  183. }
  184. int raw_local_deliver(struct sk_buff *skb, int protocol)
  185. {
  186. int hash;
  187. struct sock *raw_sk;
  188. hash = protocol & (RAW_HTABLE_SIZE - 1);
  189. raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
  190. /* If there maybe a raw socket we must check - if not we
  191. * don't care less
  192. */
  193. if (raw_sk && !raw_v4_input(skb, ip_hdr(skb), hash))
  194. raw_sk = NULL;
  195. return raw_sk != NULL;
  196. }
  197. static void raw_err(struct sock *sk, struct sk_buff *skb, u32 info)
  198. {
  199. struct inet_sock *inet = inet_sk(sk);
  200. const int type = icmp_hdr(skb)->type;
  201. const int code = icmp_hdr(skb)->code;
  202. int err = 0;
  203. int harderr = 0;
  204. if (type == ICMP_DEST_UNREACH && code == ICMP_FRAG_NEEDED)
  205. ipv4_sk_update_pmtu(skb, sk, info);
  206. else if (type == ICMP_REDIRECT) {
  207. ipv4_sk_redirect(skb, sk);
  208. return;
  209. }
  210. /* Report error on raw socket, if:
  211. 1. User requested ip_recverr.
  212. 2. Socket is connected (otherwise the error indication
  213. is useless without ip_recverr and error is hard.
  214. */
  215. if (!inet->recverr && sk->sk_state != TCP_ESTABLISHED)
  216. return;
  217. switch (type) {
  218. default:
  219. case ICMP_TIME_EXCEEDED:
  220. err = EHOSTUNREACH;
  221. break;
  222. case ICMP_SOURCE_QUENCH:
  223. return;
  224. case ICMP_PARAMETERPROB:
  225. err = EPROTO;
  226. harderr = 1;
  227. break;
  228. case ICMP_DEST_UNREACH:
  229. err = EHOSTUNREACH;
  230. if (code > NR_ICMP_UNREACH)
  231. break;
  232. err = icmp_err_convert[code].errno;
  233. harderr = icmp_err_convert[code].fatal;
  234. if (code == ICMP_FRAG_NEEDED) {
  235. harderr = inet->pmtudisc != IP_PMTUDISC_DONT;
  236. err = EMSGSIZE;
  237. }
  238. }
  239. if (inet->recverr) {
  240. const struct iphdr *iph = (const struct iphdr *)skb->data;
  241. u8 *payload = skb->data + (iph->ihl << 2);
  242. if (inet->hdrincl)
  243. payload = skb->data;
  244. ip_icmp_error(sk, skb, err, 0, info, payload);
  245. }
  246. if (inet->recverr || harderr) {
  247. sk->sk_err = err;
  248. sk->sk_error_report(sk);
  249. }
  250. }
  251. void raw_icmp_error(struct sk_buff *skb, int protocol, u32 info)
  252. {
  253. int hash;
  254. struct sock *raw_sk;
  255. const struct iphdr *iph;
  256. struct net *net;
  257. hash = protocol & (RAW_HTABLE_SIZE - 1);
  258. read_lock(&raw_v4_hashinfo.lock);
  259. raw_sk = sk_head(&raw_v4_hashinfo.ht[hash]);
  260. if (raw_sk) {
  261. iph = (const struct iphdr *)skb->data;
  262. net = dev_net(skb->dev);
  263. while ((raw_sk = __raw_v4_lookup(net, raw_sk, protocol,
  264. iph->daddr, iph->saddr,
  265. skb->dev->ifindex)) != NULL) {
  266. raw_err(raw_sk, skb, info);
  267. raw_sk = sk_next(raw_sk);
  268. iph = (const struct iphdr *)skb->data;
  269. }
  270. }
  271. read_unlock(&raw_v4_hashinfo.lock);
  272. }
  273. static int raw_rcv_skb(struct sock *sk, struct sk_buff *skb)
  274. {
  275. /* Charge it to the socket. */
  276. ipv4_pktinfo_prepare(sk, skb);
  277. if (sock_queue_rcv_skb(sk, skb) < 0) {
  278. kfree_skb(skb);
  279. return NET_RX_DROP;
  280. }
  281. return NET_RX_SUCCESS;
  282. }
  283. int raw_rcv(struct sock *sk, struct sk_buff *skb)
  284. {
  285. if (!xfrm4_policy_check(sk, XFRM_POLICY_IN, skb)) {
  286. atomic_inc(&sk->sk_drops);
  287. kfree_skb(skb);
  288. return NET_RX_DROP;
  289. }
  290. nf_reset(skb);
  291. skb_push(skb, skb->data - skb_network_header(skb));
  292. raw_rcv_skb(sk, skb);
  293. return 0;
  294. }
  295. static int raw_send_hdrinc(struct sock *sk, struct flowi4 *fl4,
  296. struct msghdr *msg, size_t length,
  297. struct rtable **rtp,
  298. unsigned int flags)
  299. {
  300. struct inet_sock *inet = inet_sk(sk);
  301. struct net *net = sock_net(sk);
  302. struct iphdr *iph;
  303. struct sk_buff *skb;
  304. unsigned int iphlen;
  305. int err;
  306. struct rtable *rt = *rtp;
  307. int hlen, tlen;
  308. if (length > rt->dst.dev->mtu) {
  309. ip_local_error(sk, EMSGSIZE, fl4->daddr, inet->inet_dport,
  310. rt->dst.dev->mtu);
  311. return -EMSGSIZE;
  312. }
  313. if (length < sizeof(struct iphdr))
  314. return -EINVAL;
  315. if (flags&MSG_PROBE)
  316. goto out;
  317. hlen = LL_RESERVED_SPACE(rt->dst.dev);
  318. tlen = rt->dst.dev->needed_tailroom;
  319. skb = sock_alloc_send_skb(sk,
  320. length + hlen + tlen + 15,
  321. flags & MSG_DONTWAIT, &err);
  322. if (!skb)
  323. goto error;
  324. skb_reserve(skb, hlen);
  325. skb->priority = sk->sk_priority;
  326. skb->mark = sk->sk_mark;
  327. skb_dst_set(skb, &rt->dst);
  328. *rtp = NULL;
  329. skb_reset_network_header(skb);
  330. iph = ip_hdr(skb);
  331. skb_put(skb, length);
  332. skb->ip_summed = CHECKSUM_NONE;
  333. sock_tx_timestamp(sk, &skb_shinfo(skb)->tx_flags);
  334. skb->transport_header = skb->network_header;
  335. err = -EFAULT;
  336. if (memcpy_from_msg(iph, msg, length))
  337. goto error_free;
  338. iphlen = iph->ihl * 4;
  339. /*
  340. * We don't want to modify the ip header, but we do need to
  341. * be sure that it won't cause problems later along the network
  342. * stack. Specifically we want to make sure that iph->ihl is a
  343. * sane value. If ihl points beyond the length of the buffer passed
  344. * in, reject the frame as invalid
  345. */
  346. err = -EINVAL;
  347. if (iphlen > length)
  348. goto error_free;
  349. if (iphlen >= sizeof(*iph)) {
  350. if (!iph->saddr)
  351. iph->saddr = fl4->saddr;
  352. iph->check = 0;
  353. iph->tot_len = htons(length);
  354. if (!iph->id)
  355. ip_select_ident(net, skb, NULL);
  356. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  357. skb->transport_header += iphlen;
  358. if (iph->protocol == IPPROTO_ICMP &&
  359. length >= iphlen + sizeof(struct icmphdr))
  360. icmp_out_count(net, ((struct icmphdr *)
  361. skb_transport_header(skb))->type);
  362. }
  363. err = NF_HOOK(NFPROTO_IPV4, NF_INET_LOCAL_OUT,
  364. net, sk, skb, NULL, rt->dst.dev,
  365. dst_output);
  366. if (err > 0)
  367. err = net_xmit_errno(err);
  368. if (err)
  369. goto error;
  370. out:
  371. return 0;
  372. error_free:
  373. kfree_skb(skb);
  374. error:
  375. IP_INC_STATS(net, IPSTATS_MIB_OUTDISCARDS);
  376. if (err == -ENOBUFS && !inet->recverr)
  377. err = 0;
  378. return err;
  379. }
  380. static int raw_probe_proto_opt(struct raw_frag_vec *rfv, struct flowi4 *fl4)
  381. {
  382. int err;
  383. if (fl4->flowi4_proto != IPPROTO_ICMP)
  384. return 0;
  385. /* We only need the first two bytes. */
  386. rfv->hlen = 2;
  387. err = memcpy_from_msg(rfv->hdr.c, rfv->msg, rfv->hlen);
  388. if (err)
  389. return err;
  390. fl4->fl4_icmp_type = rfv->hdr.icmph.type;
  391. fl4->fl4_icmp_code = rfv->hdr.icmph.code;
  392. return 0;
  393. }
  394. static int raw_getfrag(void *from, char *to, int offset, int len, int odd,
  395. struct sk_buff *skb)
  396. {
  397. struct raw_frag_vec *rfv = from;
  398. if (offset < rfv->hlen) {
  399. int copy = min(rfv->hlen - offset, len);
  400. if (skb->ip_summed == CHECKSUM_PARTIAL)
  401. memcpy(to, rfv->hdr.c + offset, copy);
  402. else
  403. skb->csum = csum_block_add(
  404. skb->csum,
  405. csum_partial_copy_nocheck(rfv->hdr.c + offset,
  406. to, copy, 0),
  407. odd);
  408. odd = 0;
  409. offset += copy;
  410. to += copy;
  411. len -= copy;
  412. if (!len)
  413. return 0;
  414. }
  415. offset -= rfv->hlen;
  416. return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
  417. }
  418. static int raw_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  419. {
  420. struct inet_sock *inet = inet_sk(sk);
  421. struct net *net = sock_net(sk);
  422. struct ipcm_cookie ipc;
  423. struct rtable *rt = NULL;
  424. struct flowi4 fl4;
  425. int free = 0;
  426. __be32 daddr;
  427. __be32 saddr;
  428. u8 tos;
  429. int err;
  430. struct ip_options_data opt_copy;
  431. struct raw_frag_vec rfv;
  432. int hdrincl;
  433. err = -EMSGSIZE;
  434. if (len > 0xFFFF)
  435. goto out;
  436. /* hdrincl should be READ_ONCE(inet->hdrincl)
  437. * but READ_ONCE() doesn't work with bit fields
  438. */
  439. hdrincl = inet->hdrincl;
  440. /*
  441. * Check the flags.
  442. */
  443. err = -EOPNOTSUPP;
  444. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
  445. goto out; /* compatibility */
  446. /*
  447. * Get and verify the address.
  448. */
  449. if (msg->msg_namelen) {
  450. DECLARE_SOCKADDR(struct sockaddr_in *, usin, msg->msg_name);
  451. err = -EINVAL;
  452. if (msg->msg_namelen < sizeof(*usin))
  453. goto out;
  454. if (usin->sin_family != AF_INET) {
  455. pr_info_once("%s: %s forgot to set AF_INET. Fix it!\n",
  456. __func__, current->comm);
  457. err = -EAFNOSUPPORT;
  458. if (usin->sin_family)
  459. goto out;
  460. }
  461. daddr = usin->sin_addr.s_addr;
  462. /* ANK: I did not forget to get protocol from port field.
  463. * I just do not know, who uses this weirdness.
  464. * IP_HDRINCL is much more convenient.
  465. */
  466. } else {
  467. err = -EDESTADDRREQ;
  468. if (sk->sk_state != TCP_ESTABLISHED)
  469. goto out;
  470. daddr = inet->inet_daddr;
  471. }
  472. ipc.addr = inet->inet_saddr;
  473. ipc.opt = NULL;
  474. ipc.tx_flags = 0;
  475. ipc.ttl = 0;
  476. ipc.tos = -1;
  477. ipc.oif = sk->sk_bound_dev_if;
  478. if (msg->msg_controllen) {
  479. err = ip_cmsg_send(net, msg, &ipc, false);
  480. if (unlikely(err)) {
  481. kfree(ipc.opt);
  482. goto out;
  483. }
  484. if (ipc.opt)
  485. free = 1;
  486. }
  487. saddr = ipc.addr;
  488. ipc.addr = daddr;
  489. if (!ipc.opt) {
  490. struct ip_options_rcu *inet_opt;
  491. rcu_read_lock();
  492. inet_opt = rcu_dereference(inet->inet_opt);
  493. if (inet_opt) {
  494. memcpy(&opt_copy, inet_opt,
  495. sizeof(*inet_opt) + inet_opt->opt.optlen);
  496. ipc.opt = &opt_copy.opt;
  497. }
  498. rcu_read_unlock();
  499. }
  500. if (ipc.opt) {
  501. err = -EINVAL;
  502. /* Linux does not mangle headers on raw sockets,
  503. * so that IP options + IP_HDRINCL is non-sense.
  504. */
  505. if (hdrincl)
  506. goto done;
  507. if (ipc.opt->opt.srr) {
  508. if (!daddr)
  509. goto done;
  510. daddr = ipc.opt->opt.faddr;
  511. }
  512. }
  513. tos = get_rtconn_flags(&ipc, sk);
  514. if (msg->msg_flags & MSG_DONTROUTE)
  515. tos |= RTO_ONLINK;
  516. if (ipv4_is_multicast(daddr)) {
  517. if (!ipc.oif)
  518. ipc.oif = inet->mc_index;
  519. if (!saddr)
  520. saddr = inet->mc_addr;
  521. } else if (!ipc.oif)
  522. ipc.oif = inet->uc_index;
  523. flowi4_init_output(&fl4, ipc.oif, sk->sk_mark, tos,
  524. RT_SCOPE_UNIVERSE,
  525. hdrincl ? IPPROTO_RAW : sk->sk_protocol,
  526. inet_sk_flowi_flags(sk) |
  527. (hdrincl ? FLOWI_FLAG_KNOWN_NH : 0),
  528. daddr, saddr, 0, 0);
  529. if (!saddr && ipc.oif) {
  530. err = l3mdev_get_saddr(net, ipc.oif, &fl4);
  531. if (err < 0)
  532. goto done;
  533. }
  534. if (!hdrincl) {
  535. rfv.msg = msg;
  536. rfv.hlen = 0;
  537. err = raw_probe_proto_opt(&rfv, &fl4);
  538. if (err)
  539. goto done;
  540. }
  541. security_sk_classify_flow(sk, flowi4_to_flowi(&fl4));
  542. rt = ip_route_output_flow(net, &fl4, sk);
  543. if (IS_ERR(rt)) {
  544. err = PTR_ERR(rt);
  545. rt = NULL;
  546. goto done;
  547. }
  548. err = -EACCES;
  549. if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
  550. goto done;
  551. if (msg->msg_flags & MSG_CONFIRM)
  552. goto do_confirm;
  553. back_from_confirm:
  554. if (hdrincl)
  555. err = raw_send_hdrinc(sk, &fl4, msg, len,
  556. &rt, msg->msg_flags);
  557. else {
  558. sock_tx_timestamp(sk, &ipc.tx_flags);
  559. if (!ipc.addr)
  560. ipc.addr = fl4.daddr;
  561. lock_sock(sk);
  562. err = ip_append_data(sk, &fl4, raw_getfrag,
  563. &rfv, len, 0,
  564. &ipc, &rt, msg->msg_flags);
  565. if (err)
  566. ip_flush_pending_frames(sk);
  567. else if (!(msg->msg_flags & MSG_MORE)) {
  568. err = ip_push_pending_frames(sk, &fl4);
  569. if (err == -ENOBUFS && !inet->recverr)
  570. err = 0;
  571. }
  572. release_sock(sk);
  573. }
  574. done:
  575. if (free)
  576. kfree(ipc.opt);
  577. ip_rt_put(rt);
  578. out:
  579. if (err < 0)
  580. return err;
  581. return len;
  582. do_confirm:
  583. dst_confirm(&rt->dst);
  584. if (!(msg->msg_flags & MSG_PROBE) || len)
  585. goto back_from_confirm;
  586. err = 0;
  587. goto done;
  588. }
  589. static void raw_close(struct sock *sk, long timeout)
  590. {
  591. /*
  592. * Raw sockets may have direct kernel references. Kill them.
  593. */
  594. ip_ra_control(sk, 0, NULL);
  595. sk_common_release(sk);
  596. }
  597. static void raw_destroy(struct sock *sk)
  598. {
  599. lock_sock(sk);
  600. ip_flush_pending_frames(sk);
  601. release_sock(sk);
  602. }
  603. /* This gets rid of all the nasties in af_inet. -DaveM */
  604. static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  605. {
  606. struct inet_sock *inet = inet_sk(sk);
  607. struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
  608. int ret = -EINVAL;
  609. int chk_addr_ret;
  610. if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
  611. goto out;
  612. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  613. ret = -EADDRNOTAVAIL;
  614. if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  615. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  616. goto out;
  617. inet->inet_rcv_saddr = inet->inet_saddr = addr->sin_addr.s_addr;
  618. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  619. inet->inet_saddr = 0; /* Use device */
  620. sk_dst_reset(sk);
  621. ret = 0;
  622. out: return ret;
  623. }
  624. /*
  625. * This should be easy, if there is something there
  626. * we return it, otherwise we block.
  627. */
  628. static int raw_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  629. int noblock, int flags, int *addr_len)
  630. {
  631. struct inet_sock *inet = inet_sk(sk);
  632. size_t copied = 0;
  633. int err = -EOPNOTSUPP;
  634. DECLARE_SOCKADDR(struct sockaddr_in *, sin, msg->msg_name);
  635. struct sk_buff *skb;
  636. if (flags & MSG_OOB)
  637. goto out;
  638. if (flags & MSG_ERRQUEUE) {
  639. err = ip_recv_error(sk, msg, len, addr_len);
  640. goto out;
  641. }
  642. skb = skb_recv_datagram(sk, flags, noblock, &err);
  643. if (!skb)
  644. goto out;
  645. copied = skb->len;
  646. if (len < copied) {
  647. msg->msg_flags |= MSG_TRUNC;
  648. copied = len;
  649. }
  650. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  651. if (err)
  652. goto done;
  653. sock_recv_ts_and_drops(msg, sk, skb);
  654. /* Copy the address. */
  655. if (sin) {
  656. sin->sin_family = AF_INET;
  657. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  658. sin->sin_port = 0;
  659. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  660. *addr_len = sizeof(*sin);
  661. }
  662. if (inet->cmsg_flags)
  663. ip_cmsg_recv(msg, skb);
  664. if (flags & MSG_TRUNC)
  665. copied = skb->len;
  666. done:
  667. skb_free_datagram(sk, skb);
  668. out:
  669. if (err)
  670. return err;
  671. return copied;
  672. }
  673. static int raw_init(struct sock *sk)
  674. {
  675. struct raw_sock *rp = raw_sk(sk);
  676. if (inet_sk(sk)->inet_num == IPPROTO_ICMP)
  677. memset(&rp->filter, 0, sizeof(rp->filter));
  678. return 0;
  679. }
  680. static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
  681. {
  682. if (optlen > sizeof(struct icmp_filter))
  683. optlen = sizeof(struct icmp_filter);
  684. if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
  685. return -EFAULT;
  686. return 0;
  687. }
  688. static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
  689. {
  690. int len, ret = -EFAULT;
  691. if (get_user(len, optlen))
  692. goto out;
  693. ret = -EINVAL;
  694. if (len < 0)
  695. goto out;
  696. if (len > sizeof(struct icmp_filter))
  697. len = sizeof(struct icmp_filter);
  698. ret = -EFAULT;
  699. if (put_user(len, optlen) ||
  700. copy_to_user(optval, &raw_sk(sk)->filter, len))
  701. goto out;
  702. ret = 0;
  703. out: return ret;
  704. }
  705. static int do_raw_setsockopt(struct sock *sk, int level, int optname,
  706. char __user *optval, unsigned int optlen)
  707. {
  708. if (optname == ICMP_FILTER) {
  709. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  710. return -EOPNOTSUPP;
  711. else
  712. return raw_seticmpfilter(sk, optval, optlen);
  713. }
  714. return -ENOPROTOOPT;
  715. }
  716. static int raw_setsockopt(struct sock *sk, int level, int optname,
  717. char __user *optval, unsigned int optlen)
  718. {
  719. if (level != SOL_RAW)
  720. return ip_setsockopt(sk, level, optname, optval, optlen);
  721. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  722. }
  723. #ifdef CONFIG_COMPAT
  724. static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
  725. char __user *optval, unsigned int optlen)
  726. {
  727. if (level != SOL_RAW)
  728. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  729. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  730. }
  731. #endif
  732. static int do_raw_getsockopt(struct sock *sk, int level, int optname,
  733. char __user *optval, int __user *optlen)
  734. {
  735. if (optname == ICMP_FILTER) {
  736. if (inet_sk(sk)->inet_num != IPPROTO_ICMP)
  737. return -EOPNOTSUPP;
  738. else
  739. return raw_geticmpfilter(sk, optval, optlen);
  740. }
  741. return -ENOPROTOOPT;
  742. }
  743. static int raw_getsockopt(struct sock *sk, int level, int optname,
  744. char __user *optval, int __user *optlen)
  745. {
  746. if (level != SOL_RAW)
  747. return ip_getsockopt(sk, level, optname, optval, optlen);
  748. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  749. }
  750. #ifdef CONFIG_COMPAT
  751. static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
  752. char __user *optval, int __user *optlen)
  753. {
  754. if (level != SOL_RAW)
  755. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  756. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  757. }
  758. #endif
  759. static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
  760. {
  761. switch (cmd) {
  762. case SIOCOUTQ: {
  763. int amount = sk_wmem_alloc_get(sk);
  764. return put_user(amount, (int __user *)arg);
  765. }
  766. case SIOCINQ: {
  767. struct sk_buff *skb;
  768. int amount = 0;
  769. spin_lock_bh(&sk->sk_receive_queue.lock);
  770. skb = skb_peek(&sk->sk_receive_queue);
  771. if (skb)
  772. amount = skb->len;
  773. spin_unlock_bh(&sk->sk_receive_queue.lock);
  774. return put_user(amount, (int __user *)arg);
  775. }
  776. default:
  777. #ifdef CONFIG_IP_MROUTE
  778. return ipmr_ioctl(sk, cmd, (void __user *)arg);
  779. #else
  780. return -ENOIOCTLCMD;
  781. #endif
  782. }
  783. }
  784. #ifdef CONFIG_COMPAT
  785. static int compat_raw_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
  786. {
  787. switch (cmd) {
  788. case SIOCOUTQ:
  789. case SIOCINQ:
  790. return -ENOIOCTLCMD;
  791. default:
  792. #ifdef CONFIG_IP_MROUTE
  793. return ipmr_compat_ioctl(sk, cmd, compat_ptr(arg));
  794. #else
  795. return -ENOIOCTLCMD;
  796. #endif
  797. }
  798. }
  799. #endif
  800. struct proto raw_prot = {
  801. .name = "RAW",
  802. .owner = THIS_MODULE,
  803. .close = raw_close,
  804. .destroy = raw_destroy,
  805. .connect = ip4_datagram_connect,
  806. .disconnect = udp_disconnect,
  807. .ioctl = raw_ioctl,
  808. .init = raw_init,
  809. .setsockopt = raw_setsockopt,
  810. .getsockopt = raw_getsockopt,
  811. .sendmsg = raw_sendmsg,
  812. .recvmsg = raw_recvmsg,
  813. .bind = raw_bind,
  814. .backlog_rcv = raw_rcv_skb,
  815. .release_cb = ip4_datagram_release_cb,
  816. .hash = raw_hash_sk,
  817. .unhash = raw_unhash_sk,
  818. .obj_size = sizeof(struct raw_sock),
  819. .h.raw_hash = &raw_v4_hashinfo,
  820. #ifdef CONFIG_COMPAT
  821. .compat_setsockopt = compat_raw_setsockopt,
  822. .compat_getsockopt = compat_raw_getsockopt,
  823. .compat_ioctl = compat_raw_ioctl,
  824. #endif
  825. };
  826. #ifdef CONFIG_PROC_FS
  827. static struct sock *raw_get_first(struct seq_file *seq)
  828. {
  829. struct sock *sk;
  830. struct raw_iter_state *state = raw_seq_private(seq);
  831. for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
  832. ++state->bucket) {
  833. sk_for_each(sk, &state->h->ht[state->bucket])
  834. if (sock_net(sk) == seq_file_net(seq))
  835. goto found;
  836. }
  837. sk = NULL;
  838. found:
  839. return sk;
  840. }
  841. static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
  842. {
  843. struct raw_iter_state *state = raw_seq_private(seq);
  844. do {
  845. sk = sk_next(sk);
  846. try_again:
  847. ;
  848. } while (sk && sock_net(sk) != seq_file_net(seq));
  849. if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
  850. sk = sk_head(&state->h->ht[state->bucket]);
  851. goto try_again;
  852. }
  853. return sk;
  854. }
  855. static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
  856. {
  857. struct sock *sk = raw_get_first(seq);
  858. if (sk)
  859. while (pos && (sk = raw_get_next(seq, sk)) != NULL)
  860. --pos;
  861. return pos ? NULL : sk;
  862. }
  863. void *raw_seq_start(struct seq_file *seq, loff_t *pos)
  864. {
  865. struct raw_iter_state *state = raw_seq_private(seq);
  866. read_lock(&state->h->lock);
  867. return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  868. }
  869. EXPORT_SYMBOL_GPL(raw_seq_start);
  870. void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  871. {
  872. struct sock *sk;
  873. if (v == SEQ_START_TOKEN)
  874. sk = raw_get_first(seq);
  875. else
  876. sk = raw_get_next(seq, v);
  877. ++*pos;
  878. return sk;
  879. }
  880. EXPORT_SYMBOL_GPL(raw_seq_next);
  881. void raw_seq_stop(struct seq_file *seq, void *v)
  882. {
  883. struct raw_iter_state *state = raw_seq_private(seq);
  884. read_unlock(&state->h->lock);
  885. }
  886. EXPORT_SYMBOL_GPL(raw_seq_stop);
  887. static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  888. {
  889. struct inet_sock *inet = inet_sk(sp);
  890. __be32 dest = inet->inet_daddr,
  891. src = inet->inet_rcv_saddr;
  892. __u16 destp = 0,
  893. srcp = inet->inet_num;
  894. seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
  895. " %02X %08X:%08X %02X:%08lX %08X %5u %8d %lu %d %pK %d\n",
  896. i, src, srcp, dest, destp, sp->sk_state,
  897. sk_wmem_alloc_get(sp),
  898. sk_rmem_alloc_get(sp),
  899. 0, 0L, 0,
  900. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sp)),
  901. 0, sock_i_ino(sp),
  902. atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
  903. }
  904. static int raw_seq_show(struct seq_file *seq, void *v)
  905. {
  906. if (v == SEQ_START_TOKEN)
  907. seq_printf(seq, " sl local_address rem_address st tx_queue "
  908. "rx_queue tr tm->when retrnsmt uid timeout "
  909. "inode ref pointer drops\n");
  910. else
  911. raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
  912. return 0;
  913. }
  914. static const struct seq_operations raw_seq_ops = {
  915. .start = raw_seq_start,
  916. .next = raw_seq_next,
  917. .stop = raw_seq_stop,
  918. .show = raw_seq_show,
  919. };
  920. int raw_seq_open(struct inode *ino, struct file *file,
  921. struct raw_hashinfo *h, const struct seq_operations *ops)
  922. {
  923. int err;
  924. struct raw_iter_state *i;
  925. err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
  926. if (err < 0)
  927. return err;
  928. i = raw_seq_private((struct seq_file *)file->private_data);
  929. i->h = h;
  930. return 0;
  931. }
  932. EXPORT_SYMBOL_GPL(raw_seq_open);
  933. static int raw_v4_seq_open(struct inode *inode, struct file *file)
  934. {
  935. return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
  936. }
  937. static const struct file_operations raw_seq_fops = {
  938. .owner = THIS_MODULE,
  939. .open = raw_v4_seq_open,
  940. .read = seq_read,
  941. .llseek = seq_lseek,
  942. .release = seq_release_net,
  943. };
  944. static __net_init int raw_init_net(struct net *net)
  945. {
  946. if (!proc_create("raw", S_IRUGO, net->proc_net, &raw_seq_fops))
  947. return -ENOMEM;
  948. return 0;
  949. }
  950. static __net_exit void raw_exit_net(struct net *net)
  951. {
  952. remove_proc_entry("raw", net->proc_net);
  953. }
  954. static __net_initdata struct pernet_operations raw_net_ops = {
  955. .init = raw_init_net,
  956. .exit = raw_exit_net,
  957. };
  958. int __init raw_proc_init(void)
  959. {
  960. return register_pernet_subsys(&raw_net_ops);
  961. }
  962. void __init raw_proc_exit(void)
  963. {
  964. unregister_pernet_subsys(&raw_net_ops);
  965. }
  966. #endif /* CONFIG_PROC_FS */