raw.c 24 KB

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