raw.c 23 KB

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