raw.c 24 KB

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