raw.c 25 KB

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