raw.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. /*
  2. * RAW sockets for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <roque@di.fc.ul.pt>
  7. *
  8. * Adapted from linux/net/ipv4/raw.c
  9. *
  10. * $Id: raw.c,v 1.51 2002/02/01 22:01:04 davem Exp $
  11. *
  12. * Fixes:
  13. * Hideaki YOSHIFUJI : sin6_scope_id support
  14. * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
  15. * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
  16. *
  17. * This program is free software; you can redistribute it and/or
  18. * modify it under the terms of the GNU General Public License
  19. * as published by the Free Software Foundation; either version
  20. * 2 of the License, or (at your option) any later version.
  21. */
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/socket.h>
  25. #include <linux/sockios.h>
  26. #include <linux/sched.h>
  27. #include <linux/net.h>
  28. #include <linux/in6.h>
  29. #include <linux/netdevice.h>
  30. #include <linux/if_arp.h>
  31. #include <linux/icmpv6.h>
  32. #include <linux/netfilter.h>
  33. #include <linux/netfilter_ipv6.h>
  34. #include <linux/skbuff.h>
  35. #include <asm/uaccess.h>
  36. #include <asm/ioctls.h>
  37. #include <net/ip.h>
  38. #include <net/sock.h>
  39. #include <net/snmp.h>
  40. #include <net/ipv6.h>
  41. #include <net/ndisc.h>
  42. #include <net/protocol.h>
  43. #include <net/ip6_route.h>
  44. #include <net/ip6_checksum.h>
  45. #include <net/addrconf.h>
  46. #include <net/transp_v6.h>
  47. #include <net/udp.h>
  48. #include <net/inet_common.h>
  49. #include <net/tcp_states.h>
  50. #include <net/rawv6.h>
  51. #include <net/xfrm.h>
  52. #include <linux/proc_fs.h>
  53. #include <linux/seq_file.h>
  54. struct hlist_head raw_v6_htable[RAWV6_HTABLE_SIZE];
  55. DEFINE_RWLOCK(raw_v6_lock);
  56. static void raw_v6_hash(struct sock *sk)
  57. {
  58. struct hlist_head *list = &raw_v6_htable[inet_sk(sk)->num &
  59. (RAWV6_HTABLE_SIZE - 1)];
  60. write_lock_bh(&raw_v6_lock);
  61. sk_add_node(sk, list);
  62. sock_prot_inc_use(sk->sk_prot);
  63. write_unlock_bh(&raw_v6_lock);
  64. }
  65. static void raw_v6_unhash(struct sock *sk)
  66. {
  67. write_lock_bh(&raw_v6_lock);
  68. if (sk_del_node_init(sk))
  69. sock_prot_dec_use(sk->sk_prot);
  70. write_unlock_bh(&raw_v6_lock);
  71. }
  72. /* Grumble... icmp and ip_input want to get at this... */
  73. struct sock *__raw_v6_lookup(struct sock *sk, unsigned short num,
  74. struct in6_addr *loc_addr, struct in6_addr *rmt_addr,
  75. int dif)
  76. {
  77. struct hlist_node *node;
  78. int is_multicast = ipv6_addr_is_multicast(loc_addr);
  79. sk_for_each_from(sk, node)
  80. if (inet_sk(sk)->num == num) {
  81. struct ipv6_pinfo *np = inet6_sk(sk);
  82. if (!ipv6_addr_any(&np->daddr) &&
  83. !ipv6_addr_equal(&np->daddr, rmt_addr))
  84. continue;
  85. if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
  86. continue;
  87. if (!ipv6_addr_any(&np->rcv_saddr)) {
  88. if (ipv6_addr_equal(&np->rcv_saddr, loc_addr))
  89. goto found;
  90. if (is_multicast &&
  91. inet6_mc_check(sk, loc_addr, rmt_addr))
  92. goto found;
  93. continue;
  94. }
  95. goto found;
  96. }
  97. sk = NULL;
  98. found:
  99. return sk;
  100. }
  101. /*
  102. * 0 - deliver
  103. * 1 - block
  104. */
  105. static __inline__ int icmpv6_filter(struct sock *sk, struct sk_buff *skb)
  106. {
  107. struct icmp6hdr *icmph;
  108. struct raw6_sock *rp = raw6_sk(sk);
  109. if (pskb_may_pull(skb, sizeof(struct icmp6hdr))) {
  110. __u32 *data = &rp->filter.data[0];
  111. int bit_nr;
  112. icmph = (struct icmp6hdr *) skb->data;
  113. bit_nr = icmph->icmp6_type;
  114. return (data[bit_nr >> 5] & (1 << (bit_nr & 31))) != 0;
  115. }
  116. return 0;
  117. }
  118. /*
  119. * demultiplex raw sockets.
  120. * (should consider queueing the skb in the sock receive_queue
  121. * without calling rawv6.c)
  122. *
  123. * Caller owns SKB so we must make clones.
  124. */
  125. int ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
  126. {
  127. struct in6_addr *saddr;
  128. struct in6_addr *daddr;
  129. struct sock *sk;
  130. int delivered = 0;
  131. __u8 hash;
  132. saddr = &skb->nh.ipv6h->saddr;
  133. daddr = saddr + 1;
  134. hash = nexthdr & (MAX_INET_PROTOS - 1);
  135. read_lock(&raw_v6_lock);
  136. sk = sk_head(&raw_v6_htable[hash]);
  137. /*
  138. * The first socket found will be delivered after
  139. * delivery to transport protocols.
  140. */
  141. if (sk == NULL)
  142. goto out;
  143. sk = __raw_v6_lookup(sk, nexthdr, daddr, saddr, IP6CB(skb)->iif);
  144. while (sk) {
  145. delivered = 1;
  146. if (nexthdr != IPPROTO_ICMPV6 || !icmpv6_filter(sk, skb)) {
  147. struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
  148. /* Not releasing hash table! */
  149. if (clone) {
  150. nf_reset(clone);
  151. rawv6_rcv(sk, clone);
  152. }
  153. }
  154. sk = __raw_v6_lookup(sk_next(sk), nexthdr, daddr, saddr,
  155. IP6CB(skb)->iif);
  156. }
  157. out:
  158. read_unlock(&raw_v6_lock);
  159. return delivered;
  160. }
  161. /* This cleans up af_inet6 a bit. -DaveM */
  162. static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  163. {
  164. struct inet_sock *inet = inet_sk(sk);
  165. struct ipv6_pinfo *np = inet6_sk(sk);
  166. struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
  167. __u32 v4addr = 0;
  168. int addr_type;
  169. int err;
  170. if (addr_len < SIN6_LEN_RFC2133)
  171. return -EINVAL;
  172. addr_type = ipv6_addr_type(&addr->sin6_addr);
  173. /* Raw sockets are IPv6 only */
  174. if (addr_type == IPV6_ADDR_MAPPED)
  175. return(-EADDRNOTAVAIL);
  176. lock_sock(sk);
  177. err = -EINVAL;
  178. if (sk->sk_state != TCP_CLOSE)
  179. goto out;
  180. /* Check if the address belongs to the host. */
  181. if (addr_type != IPV6_ADDR_ANY) {
  182. struct net_device *dev = NULL;
  183. if (addr_type & IPV6_ADDR_LINKLOCAL) {
  184. if (addr_len >= sizeof(struct sockaddr_in6) &&
  185. addr->sin6_scope_id) {
  186. /* Override any existing binding, if another
  187. * one is supplied by user.
  188. */
  189. sk->sk_bound_dev_if = addr->sin6_scope_id;
  190. }
  191. /* Binding to link-local address requires an interface */
  192. if (!sk->sk_bound_dev_if)
  193. goto out;
  194. dev = dev_get_by_index(sk->sk_bound_dev_if);
  195. if (!dev) {
  196. err = -ENODEV;
  197. goto out;
  198. }
  199. }
  200. /* ipv4 addr of the socket is invalid. Only the
  201. * unspecified and mapped address have a v4 equivalent.
  202. */
  203. v4addr = LOOPBACK4_IPV6;
  204. if (!(addr_type & IPV6_ADDR_MULTICAST)) {
  205. err = -EADDRNOTAVAIL;
  206. if (!ipv6_chk_addr(&addr->sin6_addr, dev, 0)) {
  207. if (dev)
  208. dev_put(dev);
  209. goto out;
  210. }
  211. }
  212. if (dev)
  213. dev_put(dev);
  214. }
  215. inet->rcv_saddr = inet->saddr = v4addr;
  216. ipv6_addr_copy(&np->rcv_saddr, &addr->sin6_addr);
  217. if (!(addr_type & IPV6_ADDR_MULTICAST))
  218. ipv6_addr_copy(&np->saddr, &addr->sin6_addr);
  219. err = 0;
  220. out:
  221. release_sock(sk);
  222. return err;
  223. }
  224. void rawv6_err(struct sock *sk, struct sk_buff *skb,
  225. struct inet6_skb_parm *opt,
  226. int type, int code, int offset, u32 info)
  227. {
  228. struct inet_sock *inet = inet_sk(sk);
  229. struct ipv6_pinfo *np = inet6_sk(sk);
  230. int err;
  231. int harderr;
  232. /* Report error on raw socket, if:
  233. 1. User requested recverr.
  234. 2. Socket is connected (otherwise the error indication
  235. is useless without recverr and error is hard.
  236. */
  237. if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
  238. return;
  239. harderr = icmpv6_err_convert(type, code, &err);
  240. if (type == ICMPV6_PKT_TOOBIG)
  241. harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
  242. if (np->recverr) {
  243. u8 *payload = skb->data;
  244. if (!inet->hdrincl)
  245. payload += offset;
  246. ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
  247. }
  248. if (np->recverr || harderr) {
  249. sk->sk_err = err;
  250. sk->sk_error_report(sk);
  251. }
  252. }
  253. static inline int rawv6_rcv_skb(struct sock * sk, struct sk_buff * skb)
  254. {
  255. if ((raw6_sk(sk)->checksum || sk->sk_filter) &&
  256. skb_checksum_complete(skb)) {
  257. /* FIXME: increment a raw6 drops counter here */
  258. kfree_skb(skb);
  259. return 0;
  260. }
  261. /* Charge it to the socket. */
  262. if (sock_queue_rcv_skb(sk,skb)<0) {
  263. /* FIXME: increment a raw6 drops counter here */
  264. kfree_skb(skb);
  265. return 0;
  266. }
  267. return 0;
  268. }
  269. /*
  270. * This is next to useless...
  271. * if we demultiplex in network layer we don't need the extra call
  272. * just to queue the skb...
  273. * maybe we could have the network decide upon a hint if it
  274. * should call raw_rcv for demultiplexing
  275. */
  276. int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
  277. {
  278. struct inet_sock *inet = inet_sk(sk);
  279. struct raw6_sock *rp = raw6_sk(sk);
  280. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
  281. kfree_skb(skb);
  282. return NET_RX_DROP;
  283. }
  284. if (!rp->checksum)
  285. skb->ip_summed = CHECKSUM_UNNECESSARY;
  286. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  287. skb_postpull_rcsum(skb, skb->nh.raw,
  288. skb->h.raw - skb->nh.raw);
  289. if (!csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  290. &skb->nh.ipv6h->daddr,
  291. skb->len, inet->num, skb->csum))
  292. skb->ip_summed = CHECKSUM_UNNECESSARY;
  293. }
  294. if (skb->ip_summed != CHECKSUM_UNNECESSARY)
  295. skb->csum = ~csum_ipv6_magic(&skb->nh.ipv6h->saddr,
  296. &skb->nh.ipv6h->daddr,
  297. skb->len, inet->num, 0);
  298. if (inet->hdrincl) {
  299. if (skb_checksum_complete(skb)) {
  300. /* FIXME: increment a raw6 drops counter here */
  301. kfree_skb(skb);
  302. return 0;
  303. }
  304. }
  305. rawv6_rcv_skb(sk, skb);
  306. return 0;
  307. }
  308. /*
  309. * This should be easy, if there is something there
  310. * we return it, otherwise we block.
  311. */
  312. static int rawv6_recvmsg(struct kiocb *iocb, struct sock *sk,
  313. struct msghdr *msg, size_t len,
  314. int noblock, int flags, int *addr_len)
  315. {
  316. struct ipv6_pinfo *np = inet6_sk(sk);
  317. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)msg->msg_name;
  318. struct sk_buff *skb;
  319. size_t copied;
  320. int err;
  321. if (flags & MSG_OOB)
  322. return -EOPNOTSUPP;
  323. if (addr_len)
  324. *addr_len=sizeof(*sin6);
  325. if (flags & MSG_ERRQUEUE)
  326. return ipv6_recv_error(sk, msg, len);
  327. skb = skb_recv_datagram(sk, flags, noblock, &err);
  328. if (!skb)
  329. goto out;
  330. copied = skb->len;
  331. if (copied > len) {
  332. copied = len;
  333. msg->msg_flags |= MSG_TRUNC;
  334. }
  335. if (skb->ip_summed==CHECKSUM_UNNECESSARY) {
  336. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  337. } else if (msg->msg_flags&MSG_TRUNC) {
  338. if (__skb_checksum_complete(skb))
  339. goto csum_copy_err;
  340. err = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
  341. } else {
  342. err = skb_copy_and_csum_datagram_iovec(skb, 0, msg->msg_iov);
  343. if (err == -EINVAL)
  344. goto csum_copy_err;
  345. }
  346. if (err)
  347. goto out_free;
  348. /* Copy the address. */
  349. if (sin6) {
  350. sin6->sin6_family = AF_INET6;
  351. sin6->sin6_port = 0;
  352. ipv6_addr_copy(&sin6->sin6_addr, &skb->nh.ipv6h->saddr);
  353. sin6->sin6_flowinfo = 0;
  354. sin6->sin6_scope_id = 0;
  355. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  356. sin6->sin6_scope_id = IP6CB(skb)->iif;
  357. }
  358. sock_recv_timestamp(msg, sk, skb);
  359. if (np->rxopt.all)
  360. datagram_recv_ctl(sk, msg, skb);
  361. err = copied;
  362. if (flags & MSG_TRUNC)
  363. err = skb->len;
  364. out_free:
  365. skb_free_datagram(sk, skb);
  366. out:
  367. return err;
  368. csum_copy_err:
  369. skb_kill_datagram(sk, skb, flags);
  370. /* Error for blocking case is chosen to masquerade
  371. as some normal condition.
  372. */
  373. err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
  374. /* FIXME: increment a raw6 drops counter here */
  375. goto out;
  376. }
  377. static int rawv6_push_pending_frames(struct sock *sk, struct flowi *fl,
  378. struct raw6_sock *rp)
  379. {
  380. struct sk_buff *skb;
  381. int err = 0;
  382. int offset;
  383. int len;
  384. int total_len;
  385. u32 tmp_csum;
  386. u16 csum;
  387. if (!rp->checksum)
  388. goto send;
  389. if ((skb = skb_peek(&sk->sk_write_queue)) == NULL)
  390. goto out;
  391. offset = rp->offset;
  392. total_len = inet_sk(sk)->cork.length - (skb->nh.raw - skb->data);
  393. if (offset >= total_len - 1) {
  394. err = -EINVAL;
  395. ip6_flush_pending_frames(sk);
  396. goto out;
  397. }
  398. /* should be check HW csum miyazawa */
  399. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  400. /*
  401. * Only one fragment on the socket.
  402. */
  403. tmp_csum = skb->csum;
  404. } else {
  405. struct sk_buff *csum_skb = NULL;
  406. tmp_csum = 0;
  407. skb_queue_walk(&sk->sk_write_queue, skb) {
  408. tmp_csum = csum_add(tmp_csum, skb->csum);
  409. if (csum_skb)
  410. continue;
  411. len = skb->len - (skb->h.raw - skb->data);
  412. if (offset >= len) {
  413. offset -= len;
  414. continue;
  415. }
  416. csum_skb = skb;
  417. }
  418. skb = csum_skb;
  419. }
  420. offset += skb->h.raw - skb->data;
  421. if (skb_copy_bits(skb, offset, &csum, 2))
  422. BUG();
  423. /* in case cksum was not initialized */
  424. if (unlikely(csum))
  425. tmp_csum = csum_sub(tmp_csum, csum);
  426. tmp_csum = csum_ipv6_magic(&fl->fl6_src,
  427. &fl->fl6_dst,
  428. total_len, fl->proto, tmp_csum);
  429. if (tmp_csum == 0)
  430. tmp_csum = -1;
  431. csum = tmp_csum;
  432. if (skb_store_bits(skb, offset, &csum, 2))
  433. BUG();
  434. send:
  435. err = ip6_push_pending_frames(sk);
  436. out:
  437. return err;
  438. }
  439. static int rawv6_send_hdrinc(struct sock *sk, void *from, int length,
  440. struct flowi *fl, struct rt6_info *rt,
  441. unsigned int flags)
  442. {
  443. struct ipv6_pinfo *np = inet6_sk(sk);
  444. struct ipv6hdr *iph;
  445. struct sk_buff *skb;
  446. unsigned int hh_len;
  447. int err;
  448. if (length > rt->u.dst.dev->mtu) {
  449. ipv6_local_error(sk, EMSGSIZE, fl, rt->u.dst.dev->mtu);
  450. return -EMSGSIZE;
  451. }
  452. if (flags&MSG_PROBE)
  453. goto out;
  454. hh_len = LL_RESERVED_SPACE(rt->u.dst.dev);
  455. skb = sock_alloc_send_skb(sk, length+hh_len+15,
  456. flags&MSG_DONTWAIT, &err);
  457. if (skb == NULL)
  458. goto error;
  459. skb_reserve(skb, hh_len);
  460. skb->priority = sk->sk_priority;
  461. skb->dst = dst_clone(&rt->u.dst);
  462. skb->nh.ipv6h = iph = (struct ipv6hdr *)skb_put(skb, length);
  463. skb->ip_summed = CHECKSUM_NONE;
  464. skb->h.raw = skb->nh.raw;
  465. err = memcpy_fromiovecend((void *)iph, from, 0, length);
  466. if (err)
  467. goto error_fault;
  468. IP6_INC_STATS(IPSTATS_MIB_OUTREQUESTS);
  469. err = NF_HOOK(PF_INET6, NF_IP6_LOCAL_OUT, skb, NULL, rt->u.dst.dev,
  470. dst_output);
  471. if (err > 0)
  472. err = np->recverr ? net_xmit_errno(err) : 0;
  473. if (err)
  474. goto error;
  475. out:
  476. return 0;
  477. error_fault:
  478. err = -EFAULT;
  479. kfree_skb(skb);
  480. error:
  481. IP6_INC_STATS(IPSTATS_MIB_OUTDISCARDS);
  482. return err;
  483. }
  484. static void rawv6_probe_proto_opt(struct flowi *fl, struct msghdr *msg)
  485. {
  486. struct iovec *iov;
  487. u8 __user *type = NULL;
  488. u8 __user *code = NULL;
  489. int probed = 0;
  490. int i;
  491. if (!msg->msg_iov)
  492. return;
  493. for (i = 0; i < msg->msg_iovlen; i++) {
  494. iov = &msg->msg_iov[i];
  495. if (!iov)
  496. continue;
  497. switch (fl->proto) {
  498. case IPPROTO_ICMPV6:
  499. /* check if one-byte field is readable or not. */
  500. if (iov->iov_base && iov->iov_len < 1)
  501. break;
  502. if (!type) {
  503. type = iov->iov_base;
  504. /* check if code field is readable or not. */
  505. if (iov->iov_len > 1)
  506. code = type + 1;
  507. } else if (!code)
  508. code = iov->iov_base;
  509. if (type && code) {
  510. get_user(fl->fl_icmp_type, type);
  511. get_user(fl->fl_icmp_code, code);
  512. probed = 1;
  513. }
  514. break;
  515. default:
  516. probed = 1;
  517. break;
  518. }
  519. if (probed)
  520. break;
  521. }
  522. }
  523. static int rawv6_sendmsg(struct kiocb *iocb, struct sock *sk,
  524. struct msghdr *msg, size_t len)
  525. {
  526. struct ipv6_txoptions opt_space;
  527. struct sockaddr_in6 * sin6 = (struct sockaddr_in6 *) msg->msg_name;
  528. struct in6_addr *daddr, *final_p = NULL, final;
  529. struct inet_sock *inet = inet_sk(sk);
  530. struct ipv6_pinfo *np = inet6_sk(sk);
  531. struct raw6_sock *rp = raw6_sk(sk);
  532. struct ipv6_txoptions *opt = NULL;
  533. struct ip6_flowlabel *flowlabel = NULL;
  534. struct dst_entry *dst = NULL;
  535. struct flowi fl;
  536. int addr_len = msg->msg_namelen;
  537. int hlimit = -1;
  538. int tclass = -1;
  539. u16 proto;
  540. int err;
  541. /* Rough check on arithmetic overflow,
  542. better check is made in ip6_build_xmit
  543. */
  544. if (len < 0)
  545. return -EMSGSIZE;
  546. /* Mirror BSD error message compatibility */
  547. if (msg->msg_flags & MSG_OOB)
  548. return -EOPNOTSUPP;
  549. /*
  550. * Get and verify the address.
  551. */
  552. memset(&fl, 0, sizeof(fl));
  553. if (sin6) {
  554. if (addr_len < SIN6_LEN_RFC2133)
  555. return -EINVAL;
  556. if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
  557. return(-EAFNOSUPPORT);
  558. /* port is the proto value [0..255] carried in nexthdr */
  559. proto = ntohs(sin6->sin6_port);
  560. if (!proto)
  561. proto = inet->num;
  562. else if (proto != inet->num)
  563. return(-EINVAL);
  564. if (proto > 255)
  565. return(-EINVAL);
  566. daddr = &sin6->sin6_addr;
  567. if (np->sndflow) {
  568. fl.fl6_flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  569. if (fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) {
  570. flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
  571. if (flowlabel == NULL)
  572. return -EINVAL;
  573. daddr = &flowlabel->dst;
  574. }
  575. }
  576. /*
  577. * Otherwise it will be difficult to maintain
  578. * sk->sk_dst_cache.
  579. */
  580. if (sk->sk_state == TCP_ESTABLISHED &&
  581. ipv6_addr_equal(daddr, &np->daddr))
  582. daddr = &np->daddr;
  583. if (addr_len >= sizeof(struct sockaddr_in6) &&
  584. sin6->sin6_scope_id &&
  585. ipv6_addr_type(daddr)&IPV6_ADDR_LINKLOCAL)
  586. fl.oif = sin6->sin6_scope_id;
  587. } else {
  588. if (sk->sk_state != TCP_ESTABLISHED)
  589. return -EDESTADDRREQ;
  590. proto = inet->num;
  591. daddr = &np->daddr;
  592. fl.fl6_flowlabel = np->flow_label;
  593. }
  594. if (ipv6_addr_any(daddr)) {
  595. /*
  596. * unspecified destination address
  597. * treated as error... is this correct ?
  598. */
  599. fl6_sock_release(flowlabel);
  600. return(-EINVAL);
  601. }
  602. if (fl.oif == 0)
  603. fl.oif = sk->sk_bound_dev_if;
  604. if (msg->msg_controllen) {
  605. opt = &opt_space;
  606. memset(opt, 0, sizeof(struct ipv6_txoptions));
  607. opt->tot_len = sizeof(struct ipv6_txoptions);
  608. err = datagram_send_ctl(msg, &fl, opt, &hlimit, &tclass);
  609. if (err < 0) {
  610. fl6_sock_release(flowlabel);
  611. return err;
  612. }
  613. if ((fl.fl6_flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  614. flowlabel = fl6_sock_lookup(sk, fl.fl6_flowlabel);
  615. if (flowlabel == NULL)
  616. return -EINVAL;
  617. }
  618. if (!(opt->opt_nflen|opt->opt_flen))
  619. opt = NULL;
  620. }
  621. if (opt == NULL)
  622. opt = np->opt;
  623. if (flowlabel)
  624. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  625. opt = ipv6_fixup_options(&opt_space, opt);
  626. fl.proto = proto;
  627. rawv6_probe_proto_opt(&fl, msg);
  628. ipv6_addr_copy(&fl.fl6_dst, daddr);
  629. if (ipv6_addr_any(&fl.fl6_src) && !ipv6_addr_any(&np->saddr))
  630. ipv6_addr_copy(&fl.fl6_src, &np->saddr);
  631. /* merge ip6_build_xmit from ip6_output */
  632. if (opt && opt->srcrt) {
  633. struct rt0_hdr *rt0 = (struct rt0_hdr *) opt->srcrt;
  634. ipv6_addr_copy(&final, &fl.fl6_dst);
  635. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  636. final_p = &final;
  637. }
  638. if (!fl.oif && ipv6_addr_is_multicast(&fl.fl6_dst))
  639. fl.oif = np->mcast_oif;
  640. security_sk_classify_flow(sk, &fl);
  641. err = ip6_dst_lookup(sk, &dst, &fl);
  642. if (err)
  643. goto out;
  644. if (final_p)
  645. ipv6_addr_copy(&fl.fl6_dst, final_p);
  646. if ((err = xfrm_lookup(&dst, &fl, sk, 0)) < 0)
  647. goto out;
  648. if (hlimit < 0) {
  649. if (ipv6_addr_is_multicast(&fl.fl6_dst))
  650. hlimit = np->mcast_hops;
  651. else
  652. hlimit = np->hop_limit;
  653. if (hlimit < 0)
  654. hlimit = dst_metric(dst, RTAX_HOPLIMIT);
  655. if (hlimit < 0)
  656. hlimit = ipv6_get_hoplimit(dst->dev);
  657. }
  658. if (tclass < 0) {
  659. tclass = np->tclass;
  660. if (tclass < 0)
  661. tclass = 0;
  662. }
  663. if (msg->msg_flags&MSG_CONFIRM)
  664. goto do_confirm;
  665. back_from_confirm:
  666. if (inet->hdrincl) {
  667. err = rawv6_send_hdrinc(sk, msg->msg_iov, len, &fl, (struct rt6_info*)dst, msg->msg_flags);
  668. } else {
  669. lock_sock(sk);
  670. err = ip6_append_data(sk, ip_generic_getfrag, msg->msg_iov,
  671. len, 0, hlimit, tclass, opt, &fl, (struct rt6_info*)dst,
  672. msg->msg_flags);
  673. if (err)
  674. ip6_flush_pending_frames(sk);
  675. else if (!(msg->msg_flags & MSG_MORE))
  676. err = rawv6_push_pending_frames(sk, &fl, rp);
  677. }
  678. done:
  679. dst_release(dst);
  680. release_sock(sk);
  681. out:
  682. fl6_sock_release(flowlabel);
  683. return err<0?err:len;
  684. do_confirm:
  685. dst_confirm(dst);
  686. if (!(msg->msg_flags & MSG_PROBE) || len)
  687. goto back_from_confirm;
  688. err = 0;
  689. goto done;
  690. }
  691. static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
  692. char __user *optval, int optlen)
  693. {
  694. switch (optname) {
  695. case ICMPV6_FILTER:
  696. if (optlen > sizeof(struct icmp6_filter))
  697. optlen = sizeof(struct icmp6_filter);
  698. if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
  699. return -EFAULT;
  700. return 0;
  701. default:
  702. return -ENOPROTOOPT;
  703. };
  704. return 0;
  705. }
  706. static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
  707. char __user *optval, int __user *optlen)
  708. {
  709. int len;
  710. switch (optname) {
  711. case ICMPV6_FILTER:
  712. if (get_user(len, optlen))
  713. return -EFAULT;
  714. if (len < 0)
  715. return -EINVAL;
  716. if (len > sizeof(struct icmp6_filter))
  717. len = sizeof(struct icmp6_filter);
  718. if (put_user(len, optlen))
  719. return -EFAULT;
  720. if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
  721. return -EFAULT;
  722. return 0;
  723. default:
  724. return -ENOPROTOOPT;
  725. };
  726. return 0;
  727. }
  728. static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
  729. char __user *optval, int optlen)
  730. {
  731. struct raw6_sock *rp = raw6_sk(sk);
  732. int val;
  733. if (get_user(val, (int __user *)optval))
  734. return -EFAULT;
  735. switch (optname) {
  736. case IPV6_CHECKSUM:
  737. /* You may get strange result with a positive odd offset;
  738. RFC2292bis agrees with me. */
  739. if (val > 0 && (val&1))
  740. return(-EINVAL);
  741. if (val < 0) {
  742. rp->checksum = 0;
  743. } else {
  744. rp->checksum = 1;
  745. rp->offset = val;
  746. }
  747. return 0;
  748. break;
  749. default:
  750. return(-ENOPROTOOPT);
  751. }
  752. }
  753. static int rawv6_setsockopt(struct sock *sk, int level, int optname,
  754. char __user *optval, int optlen)
  755. {
  756. switch(level) {
  757. case SOL_RAW:
  758. break;
  759. case SOL_ICMPV6:
  760. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  761. return -EOPNOTSUPP;
  762. return rawv6_seticmpfilter(sk, level, optname, optval,
  763. optlen);
  764. case SOL_IPV6:
  765. if (optname == IPV6_CHECKSUM)
  766. break;
  767. default:
  768. return ipv6_setsockopt(sk, level, optname, optval,
  769. optlen);
  770. };
  771. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  772. }
  773. #ifdef CONFIG_COMPAT
  774. static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
  775. char __user *optval, int optlen)
  776. {
  777. switch (level) {
  778. case SOL_RAW:
  779. break;
  780. case SOL_ICMPV6:
  781. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  782. return -EOPNOTSUPP;
  783. return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
  784. case SOL_IPV6:
  785. if (optname == IPV6_CHECKSUM)
  786. break;
  787. default:
  788. return compat_ipv6_setsockopt(sk, level, optname,
  789. optval, optlen);
  790. };
  791. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  792. }
  793. #endif
  794. static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
  795. char __user *optval, int __user *optlen)
  796. {
  797. struct raw6_sock *rp = raw6_sk(sk);
  798. int val, len;
  799. if (get_user(len,optlen))
  800. return -EFAULT;
  801. switch (optname) {
  802. case IPV6_CHECKSUM:
  803. if (rp->checksum == 0)
  804. val = -1;
  805. else
  806. val = rp->offset;
  807. break;
  808. default:
  809. return -ENOPROTOOPT;
  810. }
  811. len = min_t(unsigned int, sizeof(int), len);
  812. if (put_user(len, optlen))
  813. return -EFAULT;
  814. if (copy_to_user(optval,&val,len))
  815. return -EFAULT;
  816. return 0;
  817. }
  818. static int rawv6_getsockopt(struct sock *sk, int level, int optname,
  819. char __user *optval, int __user *optlen)
  820. {
  821. switch(level) {
  822. case SOL_RAW:
  823. break;
  824. case SOL_ICMPV6:
  825. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  826. return -EOPNOTSUPP;
  827. return rawv6_geticmpfilter(sk, level, optname, optval,
  828. optlen);
  829. case SOL_IPV6:
  830. if (optname == IPV6_CHECKSUM)
  831. break;
  832. default:
  833. return ipv6_getsockopt(sk, level, optname, optval,
  834. optlen);
  835. };
  836. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  837. }
  838. #ifdef CONFIG_COMPAT
  839. static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
  840. char __user *optval, int __user *optlen)
  841. {
  842. switch (level) {
  843. case SOL_RAW:
  844. break;
  845. case SOL_ICMPV6:
  846. if (inet_sk(sk)->num != IPPROTO_ICMPV6)
  847. return -EOPNOTSUPP;
  848. return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
  849. case SOL_IPV6:
  850. if (optname == IPV6_CHECKSUM)
  851. break;
  852. default:
  853. return compat_ipv6_getsockopt(sk, level, optname,
  854. optval, optlen);
  855. };
  856. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  857. }
  858. #endif
  859. static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
  860. {
  861. switch(cmd) {
  862. case SIOCOUTQ:
  863. {
  864. int amount = atomic_read(&sk->sk_wmem_alloc);
  865. return put_user(amount, (int __user *)arg);
  866. }
  867. case SIOCINQ:
  868. {
  869. struct sk_buff *skb;
  870. int amount = 0;
  871. spin_lock_bh(&sk->sk_receive_queue.lock);
  872. skb = skb_peek(&sk->sk_receive_queue);
  873. if (skb != NULL)
  874. amount = skb->tail - skb->h.raw;
  875. spin_unlock_bh(&sk->sk_receive_queue.lock);
  876. return put_user(amount, (int __user *)arg);
  877. }
  878. default:
  879. return -ENOIOCTLCMD;
  880. }
  881. }
  882. static void rawv6_close(struct sock *sk, long timeout)
  883. {
  884. if (inet_sk(sk)->num == IPPROTO_RAW)
  885. ip6_ra_control(sk, -1, NULL);
  886. sk_common_release(sk);
  887. }
  888. static int rawv6_init_sk(struct sock *sk)
  889. {
  890. if (inet_sk(sk)->num == IPPROTO_ICMPV6) {
  891. struct raw6_sock *rp = raw6_sk(sk);
  892. rp->checksum = 1;
  893. rp->offset = 2;
  894. }
  895. return(0);
  896. }
  897. struct proto rawv6_prot = {
  898. .name = "RAWv6",
  899. .owner = THIS_MODULE,
  900. .close = rawv6_close,
  901. .connect = ip6_datagram_connect,
  902. .disconnect = udp_disconnect,
  903. .ioctl = rawv6_ioctl,
  904. .init = rawv6_init_sk,
  905. .destroy = inet6_destroy_sock,
  906. .setsockopt = rawv6_setsockopt,
  907. .getsockopt = rawv6_getsockopt,
  908. .sendmsg = rawv6_sendmsg,
  909. .recvmsg = rawv6_recvmsg,
  910. .bind = rawv6_bind,
  911. .backlog_rcv = rawv6_rcv_skb,
  912. .hash = raw_v6_hash,
  913. .unhash = raw_v6_unhash,
  914. .obj_size = sizeof(struct raw6_sock),
  915. #ifdef CONFIG_COMPAT
  916. .compat_setsockopt = compat_rawv6_setsockopt,
  917. .compat_getsockopt = compat_rawv6_getsockopt,
  918. #endif
  919. };
  920. #ifdef CONFIG_PROC_FS
  921. struct raw6_iter_state {
  922. int bucket;
  923. };
  924. #define raw6_seq_private(seq) ((struct raw6_iter_state *)(seq)->private)
  925. static struct sock *raw6_get_first(struct seq_file *seq)
  926. {
  927. struct sock *sk;
  928. struct hlist_node *node;
  929. struct raw6_iter_state* state = raw6_seq_private(seq);
  930. for (state->bucket = 0; state->bucket < RAWV6_HTABLE_SIZE; ++state->bucket)
  931. sk_for_each(sk, node, &raw_v6_htable[state->bucket])
  932. if (sk->sk_family == PF_INET6)
  933. goto out;
  934. sk = NULL;
  935. out:
  936. return sk;
  937. }
  938. static struct sock *raw6_get_next(struct seq_file *seq, struct sock *sk)
  939. {
  940. struct raw6_iter_state* state = raw6_seq_private(seq);
  941. do {
  942. sk = sk_next(sk);
  943. try_again:
  944. ;
  945. } while (sk && sk->sk_family != PF_INET6);
  946. if (!sk && ++state->bucket < RAWV6_HTABLE_SIZE) {
  947. sk = sk_head(&raw_v6_htable[state->bucket]);
  948. goto try_again;
  949. }
  950. return sk;
  951. }
  952. static struct sock *raw6_get_idx(struct seq_file *seq, loff_t pos)
  953. {
  954. struct sock *sk = raw6_get_first(seq);
  955. if (sk)
  956. while (pos && (sk = raw6_get_next(seq, sk)) != NULL)
  957. --pos;
  958. return pos ? NULL : sk;
  959. }
  960. static void *raw6_seq_start(struct seq_file *seq, loff_t *pos)
  961. {
  962. read_lock(&raw_v6_lock);
  963. return *pos ? raw6_get_idx(seq, *pos - 1) : SEQ_START_TOKEN;
  964. }
  965. static void *raw6_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  966. {
  967. struct sock *sk;
  968. if (v == SEQ_START_TOKEN)
  969. sk = raw6_get_first(seq);
  970. else
  971. sk = raw6_get_next(seq, v);
  972. ++*pos;
  973. return sk;
  974. }
  975. static void raw6_seq_stop(struct seq_file *seq, void *v)
  976. {
  977. read_unlock(&raw_v6_lock);
  978. }
  979. static void raw6_sock_seq_show(struct seq_file *seq, struct sock *sp, int i)
  980. {
  981. struct ipv6_pinfo *np = inet6_sk(sp);
  982. struct in6_addr *dest, *src;
  983. __u16 destp, srcp;
  984. dest = &np->daddr;
  985. src = &np->rcv_saddr;
  986. destp = 0;
  987. srcp = inet_sk(sp)->num;
  988. seq_printf(seq,
  989. "%4d: %08X%08X%08X%08X:%04X %08X%08X%08X%08X:%04X "
  990. "%02X %08X:%08X %02X:%08lX %08X %5d %8d %lu %d %p\n",
  991. i,
  992. src->s6_addr32[0], src->s6_addr32[1],
  993. src->s6_addr32[2], src->s6_addr32[3], srcp,
  994. dest->s6_addr32[0], dest->s6_addr32[1],
  995. dest->s6_addr32[2], dest->s6_addr32[3], destp,
  996. sp->sk_state,
  997. atomic_read(&sp->sk_wmem_alloc),
  998. atomic_read(&sp->sk_rmem_alloc),
  999. 0, 0L, 0,
  1000. sock_i_uid(sp), 0,
  1001. sock_i_ino(sp),
  1002. atomic_read(&sp->sk_refcnt), sp);
  1003. }
  1004. static int raw6_seq_show(struct seq_file *seq, void *v)
  1005. {
  1006. if (v == SEQ_START_TOKEN)
  1007. seq_printf(seq,
  1008. " sl "
  1009. "local_address "
  1010. "remote_address "
  1011. "st tx_queue rx_queue tr tm->when retrnsmt"
  1012. " uid timeout inode\n");
  1013. else
  1014. raw6_sock_seq_show(seq, v, raw6_seq_private(seq)->bucket);
  1015. return 0;
  1016. }
  1017. static struct seq_operations raw6_seq_ops = {
  1018. .start = raw6_seq_start,
  1019. .next = raw6_seq_next,
  1020. .stop = raw6_seq_stop,
  1021. .show = raw6_seq_show,
  1022. };
  1023. static int raw6_seq_open(struct inode *inode, struct file *file)
  1024. {
  1025. struct seq_file *seq;
  1026. int rc = -ENOMEM;
  1027. struct raw6_iter_state *s = kzalloc(sizeof(*s), GFP_KERNEL);
  1028. if (!s)
  1029. goto out;
  1030. rc = seq_open(file, &raw6_seq_ops);
  1031. if (rc)
  1032. goto out_kfree;
  1033. seq = file->private_data;
  1034. seq->private = s;
  1035. out:
  1036. return rc;
  1037. out_kfree:
  1038. kfree(s);
  1039. goto out;
  1040. }
  1041. static struct file_operations raw6_seq_fops = {
  1042. .owner = THIS_MODULE,
  1043. .open = raw6_seq_open,
  1044. .read = seq_read,
  1045. .llseek = seq_lseek,
  1046. .release = seq_release_private,
  1047. };
  1048. int __init raw6_proc_init(void)
  1049. {
  1050. if (!proc_net_fops_create("raw6", S_IRUGO, &raw6_seq_fops))
  1051. return -ENOMEM;
  1052. return 0;
  1053. }
  1054. void raw6_proc_exit(void)
  1055. {
  1056. proc_net_remove("raw6");
  1057. }
  1058. #endif /* CONFIG_PROC_FS */