raw.c 25 KB

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