raw.c 23 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. struct iphdr *iph;
  284. struct sk_buff *skb;
  285. unsigned int iphlen;
  286. int err;
  287. if (length > rt->u.dst.dev->mtu) {
  288. ip_local_error(sk, EMSGSIZE, rt->rt_dst, inet->dport,
  289. rt->u.dst.dev->mtu);
  290. return -EMSGSIZE;
  291. }
  292. if (flags&MSG_PROBE)
  293. goto out;
  294. skb = sock_alloc_send_skb(sk,
  295. length + LL_ALLOCATED_SPACE(rt->u.dst.dev) + 15,
  296. flags & MSG_DONTWAIT, &err);
  297. if (skb == NULL)
  298. goto error;
  299. skb_reserve(skb, LL_RESERVED_SPACE(rt->u.dst.dev));
  300. skb->priority = sk->sk_priority;
  301. skb->mark = sk->sk_mark;
  302. skb->dst = dst_clone(&rt->u.dst);
  303. skb_reset_network_header(skb);
  304. iph = ip_hdr(skb);
  305. skb_put(skb, length);
  306. skb->ip_summed = CHECKSUM_NONE;
  307. skb->transport_header = skb->network_header;
  308. err = memcpy_fromiovecend((void *)iph, from, 0, length);
  309. if (err)
  310. goto error_fault;
  311. /* We don't modify invalid header */
  312. iphlen = iph->ihl * 4;
  313. if (iphlen >= sizeof(*iph) && iphlen <= length) {
  314. if (!iph->saddr)
  315. iph->saddr = rt->rt_src;
  316. iph->check = 0;
  317. iph->tot_len = htons(length);
  318. if (!iph->id)
  319. ip_select_ident(iph, &rt->u.dst, NULL);
  320. iph->check = ip_fast_csum((unsigned char *)iph, iph->ihl);
  321. }
  322. if (iph->protocol == IPPROTO_ICMP)
  323. icmp_out_count(((struct icmphdr *)
  324. skb_transport_header(skb))->type);
  325. err = NF_HOOK(PF_INET, NF_INET_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
  326. dst_output);
  327. if (err > 0)
  328. err = inet->recverr ? net_xmit_errno(err) : 0;
  329. if (err)
  330. goto error;
  331. out:
  332. return 0;
  333. error_fault:
  334. err = -EFAULT;
  335. kfree_skb(skb);
  336. error:
  337. IP_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  338. return err;
  339. }
  340. static int raw_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
  341. {
  342. struct iovec *iov;
  343. u8 __user *type = NULL;
  344. u8 __user *code = NULL;
  345. int probed = 0;
  346. unsigned int i;
  347. if (!msg->msg_iov)
  348. return 0;
  349. for (i = 0; i < msg->msg_iovlen; i++) {
  350. iov = &msg->msg_iov[i];
  351. if (!iov)
  352. continue;
  353. switch (fl->proto) {
  354. case IPPROTO_ICMP:
  355. /* check if one-byte field is readable or not. */
  356. if (iov->iov_base && iov->iov_len < 1)
  357. break;
  358. if (!type) {
  359. type = iov->iov_base;
  360. /* check if code field is readable or not. */
  361. if (iov->iov_len > 1)
  362. code = type + 1;
  363. } else if (!code)
  364. code = iov->iov_base;
  365. if (type && code) {
  366. if (get_user(fl->fl_icmp_type, type) ||
  367. get_user(fl->fl_icmp_code, code))
  368. return -EFAULT;
  369. probed = 1;
  370. }
  371. break;
  372. default:
  373. probed = 1;
  374. break;
  375. }
  376. if (probed)
  377. break;
  378. }
  379. return 0;
  380. }
  381. static int raw_sendmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  382. size_t len)
  383. {
  384. struct inet_sock *inet = inet_sk(sk);
  385. struct ipcm_cookie ipc;
  386. struct rtable *rt = NULL;
  387. int free = 0;
  388. __be32 daddr;
  389. __be32 saddr;
  390. u8 tos;
  391. int err;
  392. err = -EMSGSIZE;
  393. if (len > 0xFFFF)
  394. goto out;
  395. /*
  396. * Check the flags.
  397. */
  398. err = -EOPNOTSUPP;
  399. if (msg->msg_flags & MSG_OOB) /* Mirror BSD error message */
  400. goto out; /* compatibility */
  401. /*
  402. * Get and verify the address.
  403. */
  404. if (msg->msg_namelen) {
  405. struct sockaddr_in *usin = (struct sockaddr_in*)msg->msg_name;
  406. err = -EINVAL;
  407. if (msg->msg_namelen < sizeof(*usin))
  408. goto out;
  409. if (usin->sin_family != AF_INET) {
  410. static int complained;
  411. if (!complained++)
  412. printk(KERN_INFO "%s forgot to set AF_INET in "
  413. "raw sendmsg. Fix it!\n",
  414. current->comm);
  415. err = -EAFNOSUPPORT;
  416. if (usin->sin_family)
  417. goto out;
  418. }
  419. daddr = usin->sin_addr.s_addr;
  420. /* ANK: I did not forget to get protocol from port field.
  421. * I just do not know, who uses this weirdness.
  422. * IP_HDRINCL is much more convenient.
  423. */
  424. } else {
  425. err = -EDESTADDRREQ;
  426. if (sk->sk_state != TCP_ESTABLISHED)
  427. goto out;
  428. daddr = inet->daddr;
  429. }
  430. ipc.addr = inet->saddr;
  431. ipc.opt = NULL;
  432. ipc.oif = sk->sk_bound_dev_if;
  433. if (msg->msg_controllen) {
  434. err = ip_cmsg_send(sock_net(sk), msg, &ipc);
  435. if (err)
  436. goto out;
  437. if (ipc.opt)
  438. free = 1;
  439. }
  440. saddr = ipc.addr;
  441. ipc.addr = daddr;
  442. if (!ipc.opt)
  443. ipc.opt = inet->opt;
  444. if (ipc.opt) {
  445. err = -EINVAL;
  446. /* Linux does not mangle headers on raw sockets,
  447. * so that IP options + IP_HDRINCL is non-sense.
  448. */
  449. if (inet->hdrincl)
  450. goto done;
  451. if (ipc.opt->srr) {
  452. if (!daddr)
  453. goto done;
  454. daddr = ipc.opt->faddr;
  455. }
  456. }
  457. tos = RT_CONN_FLAGS(sk);
  458. if (msg->msg_flags & MSG_DONTROUTE)
  459. tos |= RTO_ONLINK;
  460. if (ipv4_is_multicast(daddr)) {
  461. if (!ipc.oif)
  462. ipc.oif = inet->mc_index;
  463. if (!saddr)
  464. saddr = inet->mc_addr;
  465. }
  466. {
  467. struct flowi fl = { .oif = ipc.oif,
  468. .mark = sk->sk_mark,
  469. .nl_u = { .ip4_u =
  470. { .daddr = daddr,
  471. .saddr = saddr,
  472. .tos = tos } },
  473. .proto = inet->hdrincl ? IPPROTO_RAW :
  474. sk->sk_protocol,
  475. };
  476. if (!inet->hdrincl) {
  477. err = raw_probe_proto_opt(&fl, msg);
  478. if (err)
  479. goto done;
  480. }
  481. security_sk_classify_flow(sk, &fl);
  482. err = ip_route_output_flow(sock_net(sk), &rt, &fl, sk, 1);
  483. }
  484. if (err)
  485. goto done;
  486. err = -EACCES;
  487. if (rt->rt_flags & RTCF_BROADCAST && !sock_flag(sk, SOCK_BROADCAST))
  488. goto done;
  489. if (msg->msg_flags & MSG_CONFIRM)
  490. goto do_confirm;
  491. back_from_confirm:
  492. if (inet->hdrincl)
  493. err = raw_send_hdrinc(sk, msg->msg_iov, len,
  494. rt, msg->msg_flags);
  495. else {
  496. if (!ipc.addr)
  497. ipc.addr = rt->rt_dst;
  498. lock_sock(sk);
  499. err = ip_append_data(sk, ip_generic_getfrag, msg->msg_iov, len, 0,
  500. &ipc, rt, msg->msg_flags);
  501. if (err)
  502. ip_flush_pending_frames(sk);
  503. else if (!(msg->msg_flags & MSG_MORE))
  504. err = ip_push_pending_frames(sk);
  505. release_sock(sk);
  506. }
  507. done:
  508. if (free)
  509. kfree(ipc.opt);
  510. ip_rt_put(rt);
  511. out:
  512. if (err < 0)
  513. return err;
  514. return len;
  515. do_confirm:
  516. dst_confirm(&rt->u.dst);
  517. if (!(msg->msg_flags & MSG_PROBE) || len)
  518. goto back_from_confirm;
  519. err = 0;
  520. goto done;
  521. }
  522. static void raw_close(struct sock *sk, long timeout)
  523. {
  524. /*
  525. * Raw sockets may have direct kernel refereneces. Kill them.
  526. */
  527. ip_ra_control(sk, 0, NULL);
  528. sk_common_release(sk);
  529. }
  530. /* This gets rid of all the nasties in af_inet. -DaveM */
  531. static int raw_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  532. {
  533. struct inet_sock *inet = inet_sk(sk);
  534. struct sockaddr_in *addr = (struct sockaddr_in *) uaddr;
  535. int ret = -EINVAL;
  536. int chk_addr_ret;
  537. if (sk->sk_state != TCP_CLOSE || addr_len < sizeof(struct sockaddr_in))
  538. goto out;
  539. chk_addr_ret = inet_addr_type(sock_net(sk), addr->sin_addr.s_addr);
  540. ret = -EADDRNOTAVAIL;
  541. if (addr->sin_addr.s_addr && chk_addr_ret != RTN_LOCAL &&
  542. chk_addr_ret != RTN_MULTICAST && chk_addr_ret != RTN_BROADCAST)
  543. goto out;
  544. inet->rcv_saddr = inet->saddr = addr->sin_addr.s_addr;
  545. if (chk_addr_ret == RTN_MULTICAST || chk_addr_ret == RTN_BROADCAST)
  546. inet->saddr = 0; /* Use device */
  547. sk_dst_reset(sk);
  548. ret = 0;
  549. out: return ret;
  550. }
  551. /*
  552. * This should be easy, if there is something there
  553. * we return it, otherwise we block.
  554. */
  555. static int raw_recvmsg(struct kiocb *iocb, struct sock *sk, struct msghdr *msg,
  556. size_t len, int noblock, int flags, int *addr_len)
  557. {
  558. struct inet_sock *inet = inet_sk(sk);
  559. size_t copied = 0;
  560. int err = -EOPNOTSUPP;
  561. struct sockaddr_in *sin = (struct sockaddr_in *)msg->msg_name;
  562. struct sk_buff *skb;
  563. if (flags & MSG_OOB)
  564. goto out;
  565. if (addr_len)
  566. *addr_len = sizeof(*sin);
  567. if (flags & MSG_ERRQUEUE) {
  568. err = ip_recv_error(sk, msg, len);
  569. goto out;
  570. }
  571. skb = skb_recv_datagram(sk, flags, noblock, &err);
  572. if (!skb)
  573. goto out;
  574. copied = skb->len;
  575. if (len < copied) {
  576. msg->msg_flags |= MSG_TRUNC;
  577. copied = len;
  578. }
  579. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  580. if (err)
  581. goto done;
  582. sock_recv_timestamp(msg, sk, skb);
  583. /* Copy the address. */
  584. if (sin) {
  585. sin->sin_family = AF_INET;
  586. sin->sin_addr.s_addr = ip_hdr(skb)->saddr;
  587. sin->sin_port = 0;
  588. memset(&sin->sin_zero, 0, sizeof(sin->sin_zero));
  589. }
  590. if (inet->cmsg_flags)
  591. ip_cmsg_recv(msg, skb);
  592. if (flags & MSG_TRUNC)
  593. copied = skb->len;
  594. done:
  595. skb_free_datagram(sk, skb);
  596. out:
  597. if (err)
  598. return err;
  599. return copied;
  600. }
  601. static int raw_init(struct sock *sk)
  602. {
  603. struct raw_sock *rp = raw_sk(sk);
  604. if (inet_sk(sk)->num == IPPROTO_ICMP)
  605. memset(&rp->filter, 0, sizeof(rp->filter));
  606. return 0;
  607. }
  608. static int raw_seticmpfilter(struct sock *sk, char __user *optval, int optlen)
  609. {
  610. if (optlen > sizeof(struct icmp_filter))
  611. optlen = sizeof(struct icmp_filter);
  612. if (copy_from_user(&raw_sk(sk)->filter, optval, optlen))
  613. return -EFAULT;
  614. return 0;
  615. }
  616. static int raw_geticmpfilter(struct sock *sk, char __user *optval, int __user *optlen)
  617. {
  618. int len, ret = -EFAULT;
  619. if (get_user(len, optlen))
  620. goto out;
  621. ret = -EINVAL;
  622. if (len < 0)
  623. goto out;
  624. if (len > sizeof(struct icmp_filter))
  625. len = sizeof(struct icmp_filter);
  626. ret = -EFAULT;
  627. if (put_user(len, optlen) ||
  628. copy_to_user(optval, &raw_sk(sk)->filter, len))
  629. goto out;
  630. ret = 0;
  631. out: return ret;
  632. }
  633. static int do_raw_setsockopt(struct sock *sk, int level, int optname,
  634. char __user *optval, int optlen)
  635. {
  636. if (optname == ICMP_FILTER) {
  637. if (inet_sk(sk)->num != IPPROTO_ICMP)
  638. return -EOPNOTSUPP;
  639. else
  640. return raw_seticmpfilter(sk, optval, optlen);
  641. }
  642. return -ENOPROTOOPT;
  643. }
  644. static int raw_setsockopt(struct sock *sk, int level, int optname,
  645. char __user *optval, int optlen)
  646. {
  647. if (level != SOL_RAW)
  648. return ip_setsockopt(sk, level, optname, optval, optlen);
  649. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  650. }
  651. #ifdef CONFIG_COMPAT
  652. static int compat_raw_setsockopt(struct sock *sk, int level, int optname,
  653. char __user *optval, int optlen)
  654. {
  655. if (level != SOL_RAW)
  656. return compat_ip_setsockopt(sk, level, optname, optval, optlen);
  657. return do_raw_setsockopt(sk, level, optname, optval, optlen);
  658. }
  659. #endif
  660. static int do_raw_getsockopt(struct sock *sk, int level, int optname,
  661. char __user *optval, int __user *optlen)
  662. {
  663. if (optname == ICMP_FILTER) {
  664. if (inet_sk(sk)->num != IPPROTO_ICMP)
  665. return -EOPNOTSUPP;
  666. else
  667. return raw_geticmpfilter(sk, optval, optlen);
  668. }
  669. return -ENOPROTOOPT;
  670. }
  671. static int raw_getsockopt(struct sock *sk, int level, int optname,
  672. char __user *optval, int __user *optlen)
  673. {
  674. if (level != SOL_RAW)
  675. return ip_getsockopt(sk, level, optname, optval, optlen);
  676. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  677. }
  678. #ifdef CONFIG_COMPAT
  679. static int compat_raw_getsockopt(struct sock *sk, int level, int optname,
  680. char __user *optval, int __user *optlen)
  681. {
  682. if (level != SOL_RAW)
  683. return compat_ip_getsockopt(sk, level, optname, optval, optlen);
  684. return do_raw_getsockopt(sk, level, optname, optval, optlen);
  685. }
  686. #endif
  687. static int raw_ioctl(struct sock *sk, int cmd, unsigned long arg)
  688. {
  689. switch (cmd) {
  690. case SIOCOUTQ: {
  691. int amount = atomic_read(&sk->sk_wmem_alloc);
  692. return put_user(amount, (int __user *)arg);
  693. }
  694. case SIOCINQ: {
  695. struct sk_buff *skb;
  696. int amount = 0;
  697. spin_lock_bh(&sk->sk_receive_queue.lock);
  698. skb = skb_peek(&sk->sk_receive_queue);
  699. if (skb != NULL)
  700. amount = skb->len;
  701. spin_unlock_bh(&sk->sk_receive_queue.lock);
  702. return put_user(amount, (int __user *)arg);
  703. }
  704. default:
  705. #ifdef CONFIG_IP_MROUTE
  706. return ipmr_ioctl(sk, cmd, (void __user *)arg);
  707. #else
  708. return -ENOIOCTLCMD;
  709. #endif
  710. }
  711. }
  712. struct proto raw_prot = {
  713. .name = "RAW",
  714. .owner = THIS_MODULE,
  715. .close = raw_close,
  716. .connect = ip4_datagram_connect,
  717. .disconnect = udp_disconnect,
  718. .ioctl = raw_ioctl,
  719. .init = raw_init,
  720. .setsockopt = raw_setsockopt,
  721. .getsockopt = raw_getsockopt,
  722. .sendmsg = raw_sendmsg,
  723. .recvmsg = raw_recvmsg,
  724. .bind = raw_bind,
  725. .backlog_rcv = raw_rcv_skb,
  726. .hash = raw_hash_sk,
  727. .unhash = raw_unhash_sk,
  728. .obj_size = sizeof(struct raw_sock),
  729. .h.raw_hash = &raw_v4_hashinfo,
  730. #ifdef CONFIG_COMPAT
  731. .compat_setsockopt = compat_raw_setsockopt,
  732. .compat_getsockopt = compat_raw_getsockopt,
  733. #endif
  734. };
  735. #ifdef CONFIG_PROC_FS
  736. static struct sock *raw_get_first(struct seq_file *seq)
  737. {
  738. struct sock *sk;
  739. struct raw_iter_state* state = raw_seq_private(seq);
  740. for (state->bucket = 0; state->bucket < RAW_HTABLE_SIZE;
  741. ++state->bucket) {
  742. struct hlist_node *node;
  743. sk_for_each(sk, node, &state->h->ht[state->bucket])
  744. if (sock_net(sk) == seq_file_net(seq))
  745. goto found;
  746. }
  747. sk = NULL;
  748. found:
  749. return sk;
  750. }
  751. static struct sock *raw_get_next(struct seq_file *seq, struct sock *sk)
  752. {
  753. struct raw_iter_state* state = raw_seq_private(seq);
  754. do {
  755. sk = sk_next(sk);
  756. try_again:
  757. ;
  758. } while (sk && sock_net(sk) != seq_file_net(seq));
  759. if (!sk && ++state->bucket < RAW_HTABLE_SIZE) {
  760. sk = sk_head(&state->h->ht[state->bucket]);
  761. goto try_again;
  762. }
  763. return sk;
  764. }
  765. static struct sock *raw_get_idx(struct seq_file *seq, loff_t pos)
  766. {
  767. struct sock *sk = raw_get_first(seq);
  768. if (sk)
  769. while (pos && (sk = raw_get_next(seq, sk)) != NULL)
  770. --pos;
  771. return pos ? NULL : sk;
  772. }
  773. void *raw_seq_start(struct seq_file *seq, loff_t *pos)
  774. {
  775. struct raw_iter_state *state = raw_seq_private(seq);
  776. read_lock(&state->h->lock);
  777. return *pos ? raw_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  778. }
  779. EXPORT_SYMBOL_GPL(raw_seq_start);
  780. void *raw_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  781. {
  782. struct sock *sk;
  783. if (v == SEQ_START_TOKEN)
  784. sk = raw_get_first(seq);
  785. else
  786. sk = raw_get_next(seq, v);
  787. ++*pos;
  788. return sk;
  789. }
  790. EXPORT_SYMBOL_GPL(raw_seq_next);
  791. void raw_seq_stop(struct seq_file *seq, void *v)
  792. {
  793. struct raw_iter_state *state = raw_seq_private(seq);
  794. read_unlock(&state->h->lock);
  795. }
  796. EXPORT_SYMBOL_GPL(raw_seq_stop);
  797. static void raw_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  798. {
  799. struct inet_sock *inet = inet_sk(sp);
  800. __be32 dest = inet->daddr,
  801. src = inet->rcv_saddr;
  802. __u16 destp = 0,
  803. srcp = inet->num;
  804. seq_printf(seq, "%4d: %08X:%04X %08X:%04X"
  805. " %02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p %d",
  806. i, src, srcp, dest, destp, sp->sk_state,
  807. atomic_read(&sp->sk_wmem_alloc),
  808. atomic_read(&sp->sk_rmem_alloc),
  809. 0, 0L, 0, sock_i_uid(sp), 0, sock_i_ino(sp),
  810. atomic_read(&sp->sk_refcnt), sp, atomic_read(&sp->sk_drops));
  811. }
  812. static int raw_seq_show(struct seq_file *seq, void *v)
  813. {
  814. if (v == SEQ_START_TOKEN)
  815. seq_printf(seq, " sl local_address rem_address st tx_queue "
  816. "rx_queue tr tm->when retrnsmt uid timeout "
  817. "inode drops\n");
  818. else
  819. raw_sock_seq_show(seq, v, raw_seq_private(seq)->bucket);
  820. return 0;
  821. }
  822. static const struct seq_operations raw_seq_ops = {
  823. .start = raw_seq_start,
  824. .next = raw_seq_next,
  825. .stop = raw_seq_stop,
  826. .show = raw_seq_show,
  827. };
  828. int raw_seq_open(struct inode *ino, struct file *file,
  829. struct raw_hashinfo *h, const struct seq_operations *ops)
  830. {
  831. int err;
  832. struct raw_iter_state *i;
  833. err = seq_open_net(ino, file, ops, sizeof(struct raw_iter_state));
  834. if (err < 0)
  835. return err;
  836. i = raw_seq_private((struct seq_file *)file->private_data);
  837. i->h = h;
  838. return 0;
  839. }
  840. EXPORT_SYMBOL_GPL(raw_seq_open);
  841. static int raw_v4_seq_open(struct inode *inode, struct file *file)
  842. {
  843. return raw_seq_open(inode, file, &raw_v4_hashinfo, &raw_seq_ops);
  844. }
  845. static const struct file_operations raw_seq_fops = {
  846. .owner = THIS_MODULE,
  847. .open = raw_v4_seq_open,
  848. .read = seq_read,
  849. .llseek = seq_lseek,
  850. .release = seq_release_net,
  851. };
  852. static __net_init int raw_init_net(struct net *net)
  853. {
  854. if (!proc_net_fops_create(net, "raw", S_IRUGO, &raw_seq_fops))
  855. return -ENOMEM;
  856. return 0;
  857. }
  858. static __net_exit void raw_exit_net(struct net *net)
  859. {
  860. proc_net_remove(net, "raw");
  861. }
  862. static __net_initdata struct pernet_operations raw_net_ops = {
  863. .init = raw_init_net,
  864. .exit = raw_exit_net,
  865. };
  866. int __init raw_proc_init(void)
  867. {
  868. return register_pernet_subsys(&raw_net_ops);
  869. }
  870. void __init raw_proc_exit(void)
  871. {
  872. unregister_pernet_subsys(&raw_net_ops);
  873. }
  874. #endif /* CONFIG_PROC_FS */