raw.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399
  1. /*
  2. * RAW sockets for IPv6
  3. * Linux INET6 implementation
  4. *
  5. * Authors:
  6. * Pedro Roque <[email protected]>
  7. *
  8. * Adapted from linux/net/ipv4/raw.c
  9. *
  10. * Fixes:
  11. * Hideaki YOSHIFUJI : sin6_scope_id support
  12. * YOSHIFUJI,H.@USAGI : raw checksum (RFC2292(bis) compliance)
  13. * Kazunori MIYAZAWA @USAGI: change process style to use ip6_append_data
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * as published by the Free Software Foundation; either version
  18. * 2 of the License, or (at your option) any later version.
  19. */
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/socket.h>
  23. #include <linux/slab.h>
  24. #include <linux/sockios.h>
  25. #include <linux/net.h>
  26. #include <linux/in6.h>
  27. #include <linux/netdevice.h>
  28. #include <linux/if_arp.h>
  29. #include <linux/icmpv6.h>
  30. #include <linux/netfilter.h>
  31. #include <linux/netfilter_ipv6.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/compat.h>
  34. #include <linux/uaccess.h>
  35. #include <asm/ioctls.h>
  36. #include <net/net_namespace.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. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  51. #include <net/mip6.h>
  52. #endif
  53. #include <linux/mroute6.h>
  54. #include <net/raw.h>
  55. #include <net/rawv6.h>
  56. #include <net/xfrm.h>
  57. #include <linux/proc_fs.h>
  58. #include <linux/seq_file.h>
  59. #include <linux/export.h>
  60. #define ICMPV6_HDRLEN 4 /* ICMPv6 header, RFC 4443 Section 2.1 */
  61. static struct raw_hashinfo raw_v6_hashinfo = {
  62. .lock = __RW_LOCK_UNLOCKED(raw_v6_hashinfo.lock),
  63. };
  64. static struct sock *__raw_v6_lookup(struct net *net, struct sock *sk,
  65. unsigned short num, const struct in6_addr *loc_addr,
  66. const struct in6_addr *rmt_addr, int dif)
  67. {
  68. bool is_multicast = ipv6_addr_is_multicast(loc_addr);
  69. sk_for_each_from(sk)
  70. if (inet_sk(sk)->inet_num == num) {
  71. if (!net_eq(sock_net(sk), net))
  72. continue;
  73. if (!ipv6_addr_any(&sk->sk_v6_daddr) &&
  74. !ipv6_addr_equal(&sk->sk_v6_daddr, rmt_addr))
  75. continue;
  76. if (sk->sk_bound_dev_if && sk->sk_bound_dev_if != dif)
  77. continue;
  78. if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
  79. if (ipv6_addr_equal(&sk->sk_v6_rcv_saddr, loc_addr))
  80. goto found;
  81. if (is_multicast &&
  82. inet6_mc_check(sk, loc_addr, rmt_addr))
  83. goto found;
  84. continue;
  85. }
  86. goto found;
  87. }
  88. sk = NULL;
  89. found:
  90. return sk;
  91. }
  92. /*
  93. * 0 - deliver
  94. * 1 - block
  95. */
  96. static int icmpv6_filter(const struct sock *sk, const struct sk_buff *skb)
  97. {
  98. struct icmp6hdr _hdr;
  99. const struct icmp6hdr *hdr;
  100. /* We require only the four bytes of the ICMPv6 header, not any
  101. * additional bytes of message body in "struct icmp6hdr".
  102. */
  103. hdr = skb_header_pointer(skb, skb_transport_offset(skb),
  104. ICMPV6_HDRLEN, &_hdr);
  105. if (hdr) {
  106. const __u32 *data = &raw6_sk(sk)->filter.data[0];
  107. unsigned int type = hdr->icmp6_type;
  108. return (data[type >> 5] & (1U << (type & 31))) != 0;
  109. }
  110. return 1;
  111. }
  112. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  113. typedef int mh_filter_t(struct sock *sock, struct sk_buff *skb);
  114. static mh_filter_t __rcu *mh_filter __read_mostly;
  115. int rawv6_mh_filter_register(mh_filter_t filter)
  116. {
  117. rcu_assign_pointer(mh_filter, filter);
  118. return 0;
  119. }
  120. EXPORT_SYMBOL(rawv6_mh_filter_register);
  121. int rawv6_mh_filter_unregister(mh_filter_t filter)
  122. {
  123. RCU_INIT_POINTER(mh_filter, NULL);
  124. synchronize_rcu();
  125. return 0;
  126. }
  127. EXPORT_SYMBOL(rawv6_mh_filter_unregister);
  128. #endif
  129. /*
  130. * demultiplex raw sockets.
  131. * (should consider queueing the skb in the sock receive_queue
  132. * without calling rawv6.c)
  133. *
  134. * Caller owns SKB so we must make clones.
  135. */
  136. static bool ipv6_raw_deliver(struct sk_buff *skb, int nexthdr)
  137. {
  138. const struct in6_addr *saddr;
  139. const struct in6_addr *daddr;
  140. struct sock *sk;
  141. bool delivered = false;
  142. __u8 hash;
  143. struct net *net;
  144. saddr = &ipv6_hdr(skb)->saddr;
  145. daddr = saddr + 1;
  146. hash = nexthdr & (RAW_HTABLE_SIZE - 1);
  147. read_lock(&raw_v6_hashinfo.lock);
  148. sk = sk_head(&raw_v6_hashinfo.ht[hash]);
  149. if (!sk)
  150. goto out;
  151. net = dev_net(skb->dev);
  152. sk = __raw_v6_lookup(net, sk, nexthdr, daddr, saddr, inet6_iif(skb));
  153. while (sk) {
  154. int filtered;
  155. delivered = true;
  156. switch (nexthdr) {
  157. case IPPROTO_ICMPV6:
  158. filtered = icmpv6_filter(sk, skb);
  159. break;
  160. #if IS_ENABLED(CONFIG_IPV6_MIP6)
  161. case IPPROTO_MH:
  162. {
  163. /* XXX: To validate MH only once for each packet,
  164. * this is placed here. It should be after checking
  165. * xfrm policy, however it doesn't. The checking xfrm
  166. * policy is placed in rawv6_rcv() because it is
  167. * required for each socket.
  168. */
  169. mh_filter_t *filter;
  170. filter = rcu_dereference(mh_filter);
  171. filtered = filter ? (*filter)(sk, skb) : 0;
  172. break;
  173. }
  174. #endif
  175. default:
  176. filtered = 0;
  177. break;
  178. }
  179. if (filtered < 0)
  180. break;
  181. if (filtered == 0) {
  182. struct sk_buff *clone = skb_clone(skb, GFP_ATOMIC);
  183. /* Not releasing hash table! */
  184. if (clone) {
  185. nf_reset(clone);
  186. rawv6_rcv(sk, clone);
  187. }
  188. }
  189. sk = __raw_v6_lookup(net, sk_next(sk), nexthdr, daddr, saddr,
  190. inet6_iif(skb));
  191. }
  192. out:
  193. read_unlock(&raw_v6_hashinfo.lock);
  194. return delivered;
  195. }
  196. bool raw6_local_deliver(struct sk_buff *skb, int nexthdr)
  197. {
  198. struct sock *raw_sk;
  199. raw_sk = sk_head(&raw_v6_hashinfo.ht[nexthdr & (RAW_HTABLE_SIZE - 1)]);
  200. if (raw_sk && !ipv6_raw_deliver(skb, nexthdr))
  201. raw_sk = NULL;
  202. return raw_sk != NULL;
  203. }
  204. /* This cleans up af_inet6 a bit. -DaveM */
  205. static int rawv6_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len)
  206. {
  207. struct inet_sock *inet = inet_sk(sk);
  208. struct ipv6_pinfo *np = inet6_sk(sk);
  209. struct sockaddr_in6 *addr = (struct sockaddr_in6 *) uaddr;
  210. __be32 v4addr = 0;
  211. int addr_type;
  212. int err;
  213. if (addr_len < SIN6_LEN_RFC2133)
  214. return -EINVAL;
  215. if (addr->sin6_family != AF_INET6)
  216. return -EINVAL;
  217. addr_type = ipv6_addr_type(&addr->sin6_addr);
  218. /* Raw sockets are IPv6 only */
  219. if (addr_type == IPV6_ADDR_MAPPED)
  220. return -EADDRNOTAVAIL;
  221. lock_sock(sk);
  222. err = -EINVAL;
  223. if (sk->sk_state != TCP_CLOSE)
  224. goto out;
  225. rcu_read_lock();
  226. /* Check if the address belongs to the host. */
  227. if (addr_type != IPV6_ADDR_ANY) {
  228. struct net_device *dev = NULL;
  229. if (__ipv6_addr_needs_scope_id(addr_type)) {
  230. if (addr_len >= sizeof(struct sockaddr_in6) &&
  231. addr->sin6_scope_id) {
  232. /* Override any existing binding, if another
  233. * one is supplied by user.
  234. */
  235. sk->sk_bound_dev_if = addr->sin6_scope_id;
  236. }
  237. /* Binding to link-local address requires an interface */
  238. if (!sk->sk_bound_dev_if)
  239. goto out_unlock;
  240. }
  241. if (sk->sk_bound_dev_if) {
  242. err = -ENODEV;
  243. dev = dev_get_by_index_rcu(sock_net(sk),
  244. sk->sk_bound_dev_if);
  245. if (!dev)
  246. goto out_unlock;
  247. }
  248. /* ipv4 addr of the socket is invalid. Only the
  249. * unspecified and mapped address have a v4 equivalent.
  250. */
  251. v4addr = LOOPBACK4_IPV6;
  252. if (!(addr_type & IPV6_ADDR_MULTICAST) &&
  253. !sock_net(sk)->ipv6.sysctl.ip_nonlocal_bind) {
  254. err = -EADDRNOTAVAIL;
  255. if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
  256. dev, 0)) {
  257. goto out_unlock;
  258. }
  259. }
  260. }
  261. inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
  262. sk->sk_v6_rcv_saddr = addr->sin6_addr;
  263. if (!(addr_type & IPV6_ADDR_MULTICAST))
  264. np->saddr = addr->sin6_addr;
  265. err = 0;
  266. out_unlock:
  267. rcu_read_unlock();
  268. out:
  269. release_sock(sk);
  270. return err;
  271. }
  272. static void rawv6_err(struct sock *sk, struct sk_buff *skb,
  273. struct inet6_skb_parm *opt,
  274. u8 type, u8 code, int offset, __be32 info)
  275. {
  276. struct inet_sock *inet = inet_sk(sk);
  277. struct ipv6_pinfo *np = inet6_sk(sk);
  278. int err;
  279. int harderr;
  280. /* Report error on raw socket, if:
  281. 1. User requested recverr.
  282. 2. Socket is connected (otherwise the error indication
  283. is useless without recverr and error is hard.
  284. */
  285. if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
  286. return;
  287. harderr = icmpv6_err_convert(type, code, &err);
  288. if (type == ICMPV6_PKT_TOOBIG) {
  289. ip6_sk_update_pmtu(skb, sk, info);
  290. harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
  291. }
  292. if (type == NDISC_REDIRECT) {
  293. ip6_sk_redirect(skb, sk);
  294. return;
  295. }
  296. if (np->recverr) {
  297. u8 *payload = skb->data;
  298. if (!inet->hdrincl)
  299. payload += offset;
  300. ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
  301. }
  302. if (np->recverr || harderr) {
  303. sk->sk_err = err;
  304. sk->sk_error_report(sk);
  305. }
  306. }
  307. void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
  308. u8 type, u8 code, int inner_offset, __be32 info)
  309. {
  310. struct sock *sk;
  311. int hash;
  312. const struct in6_addr *saddr, *daddr;
  313. struct net *net;
  314. hash = nexthdr & (RAW_HTABLE_SIZE - 1);
  315. read_lock(&raw_v6_hashinfo.lock);
  316. sk = sk_head(&raw_v6_hashinfo.ht[hash]);
  317. if (sk) {
  318. /* Note: ipv6_hdr(skb) != skb->data */
  319. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
  320. saddr = &ip6h->saddr;
  321. daddr = &ip6h->daddr;
  322. net = dev_net(skb->dev);
  323. while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
  324. inet6_iif(skb)))) {
  325. rawv6_err(sk, skb, NULL, type, code,
  326. inner_offset, info);
  327. sk = sk_next(sk);
  328. }
  329. }
  330. read_unlock(&raw_v6_hashinfo.lock);
  331. }
  332. static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
  333. {
  334. if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
  335. skb_checksum_complete(skb)) {
  336. atomic_inc(&sk->sk_drops);
  337. kfree_skb(skb);
  338. return NET_RX_DROP;
  339. }
  340. /* Charge it to the socket. */
  341. skb_dst_drop(skb);
  342. if (sock_queue_rcv_skb(sk, skb) < 0) {
  343. kfree_skb(skb);
  344. return NET_RX_DROP;
  345. }
  346. return 0;
  347. }
  348. /*
  349. * This is next to useless...
  350. * if we demultiplex in network layer we don't need the extra call
  351. * just to queue the skb...
  352. * maybe we could have the network decide upon a hint if it
  353. * should call raw_rcv for demultiplexing
  354. */
  355. int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
  356. {
  357. struct inet_sock *inet = inet_sk(sk);
  358. struct raw6_sock *rp = raw6_sk(sk);
  359. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
  360. atomic_inc(&sk->sk_drops);
  361. kfree_skb(skb);
  362. return NET_RX_DROP;
  363. }
  364. if (!rp->checksum)
  365. skb->ip_summed = CHECKSUM_UNNECESSARY;
  366. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  367. skb_postpull_rcsum(skb, skb_network_header(skb),
  368. skb_network_header_len(skb));
  369. if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  370. &ipv6_hdr(skb)->daddr,
  371. skb->len, inet->inet_num, skb->csum))
  372. skb->ip_summed = CHECKSUM_UNNECESSARY;
  373. }
  374. if (!skb_csum_unnecessary(skb))
  375. skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  376. &ipv6_hdr(skb)->daddr,
  377. skb->len,
  378. inet->inet_num, 0));
  379. if (inet->hdrincl) {
  380. if (skb_checksum_complete(skb)) {
  381. atomic_inc(&sk->sk_drops);
  382. kfree_skb(skb);
  383. return NET_RX_DROP;
  384. }
  385. }
  386. rawv6_rcv_skb(sk, skb);
  387. return 0;
  388. }
  389. /*
  390. * This should be easy, if there is something there
  391. * we return it, otherwise we block.
  392. */
  393. static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  394. int noblock, int flags, int *addr_len)
  395. {
  396. struct ipv6_pinfo *np = inet6_sk(sk);
  397. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  398. struct sk_buff *skb;
  399. size_t copied;
  400. int err;
  401. if (flags & MSG_OOB)
  402. return -EOPNOTSUPP;
  403. if (flags & MSG_ERRQUEUE)
  404. return ipv6_recv_error(sk, msg, len, addr_len);
  405. if (np->rxpmtu && np->rxopt.bits.rxpmtu)
  406. return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
  407. skb = skb_recv_datagram(sk, flags, noblock, &err);
  408. if (!skb)
  409. goto out;
  410. copied = skb->len;
  411. if (copied > len) {
  412. copied = len;
  413. msg->msg_flags |= MSG_TRUNC;
  414. }
  415. if (skb_csum_unnecessary(skb)) {
  416. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  417. } else if (msg->msg_flags&MSG_TRUNC) {
  418. if (__skb_checksum_complete(skb))
  419. goto csum_copy_err;
  420. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  421. } else {
  422. err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
  423. if (err == -EINVAL)
  424. goto csum_copy_err;
  425. }
  426. if (err)
  427. goto out_free;
  428. /* Copy the address. */
  429. if (sin6) {
  430. sin6->sin6_family = AF_INET6;
  431. sin6->sin6_port = 0;
  432. sin6->sin6_addr = ipv6_hdr(skb)->saddr;
  433. sin6->sin6_flowinfo = 0;
  434. sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
  435. inet6_iif(skb));
  436. *addr_len = sizeof(*sin6);
  437. }
  438. sock_recv_ts_and_drops(msg, sk, skb);
  439. if (np->rxopt.all)
  440. ip6_datagram_recv_ctl(sk, msg, skb);
  441. err = copied;
  442. if (flags & MSG_TRUNC)
  443. err = skb->len;
  444. out_free:
  445. skb_free_datagram(sk, skb);
  446. out:
  447. return err;
  448. csum_copy_err:
  449. skb_kill_datagram(sk, skb, flags);
  450. /* Error for blocking case is chosen to masquerade
  451. as some normal condition.
  452. */
  453. err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
  454. goto out;
  455. }
  456. static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
  457. struct raw6_sock *rp)
  458. {
  459. struct sk_buff *skb;
  460. int err = 0;
  461. int offset;
  462. int len;
  463. int total_len;
  464. __wsum tmp_csum;
  465. __sum16 csum;
  466. if (!rp->checksum)
  467. goto send;
  468. skb = skb_peek(&sk->sk_write_queue);
  469. if (!skb)
  470. goto out;
  471. offset = rp->offset;
  472. total_len = inet_sk(sk)->cork.base.length;
  473. if (offset >= total_len - 1) {
  474. err = -EINVAL;
  475. ip6_flush_pending_frames(sk);
  476. goto out;
  477. }
  478. /* should be check HW csum miyazawa */
  479. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  480. /*
  481. * Only one fragment on the socket.
  482. */
  483. tmp_csum = skb->csum;
  484. } else {
  485. struct sk_buff *csum_skb = NULL;
  486. tmp_csum = 0;
  487. skb_queue_walk(&sk->sk_write_queue, skb) {
  488. tmp_csum = csum_add(tmp_csum, skb->csum);
  489. if (csum_skb)
  490. continue;
  491. len = skb->len - skb_transport_offset(skb);
  492. if (offset >= len) {
  493. offset -= len;
  494. continue;
  495. }
  496. csum_skb = skb;
  497. }
  498. skb = csum_skb;
  499. }
  500. offset += skb_transport_offset(skb);
  501. err = skb_copy_bits(skb, offset, &csum, 2);
  502. if (err < 0) {
  503. ip6_flush_pending_frames(sk);
  504. goto out;
  505. }
  506. /* in case cksum was not initialized */
  507. if (unlikely(csum))
  508. tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
  509. csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
  510. total_len, fl6->flowi6_proto, tmp_csum);
  511. if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
  512. csum = CSUM_MANGLED_0;
  513. BUG_ON(skb_store_bits(skb, offset, &csum, 2));
  514. send:
  515. err = ip6_push_pending_frames(sk);
  516. out:
  517. return err;
  518. }
  519. static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
  520. struct flowi6 *fl6, struct dst_entry **dstp,
  521. unsigned int flags)
  522. {
  523. struct ipv6_pinfo *np = inet6_sk(sk);
  524. struct net *net = sock_net(sk);
  525. struct ipv6hdr *iph;
  526. struct sk_buff *skb;
  527. int err;
  528. struct rt6_info *rt = (struct rt6_info *)*dstp;
  529. int hlen = LL_RESERVED_SPACE(rt->dst.dev);
  530. int tlen = rt->dst.dev->needed_tailroom;
  531. if (length > rt->dst.dev->mtu) {
  532. ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
  533. return -EMSGSIZE;
  534. }
  535. if (length < sizeof(struct ipv6hdr))
  536. return -EINVAL;
  537. if (flags&MSG_PROBE)
  538. goto out;
  539. skb = sock_alloc_send_skb(sk,
  540. length + hlen + tlen + 15,
  541. flags & MSG_DONTWAIT, &err);
  542. if (!skb)
  543. goto error;
  544. skb_reserve(skb, hlen);
  545. skb->protocol = htons(ETH_P_IPV6);
  546. skb->priority = sk->sk_priority;
  547. skb->mark = sk->sk_mark;
  548. skb_put(skb, length);
  549. skb_reset_network_header(skb);
  550. iph = ipv6_hdr(skb);
  551. skb->ip_summed = CHECKSUM_NONE;
  552. skb->transport_header = skb->network_header;
  553. err = memcpy_from_msg(iph, msg, length);
  554. if (err) {
  555. err = -EFAULT;
  556. kfree_skb(skb);
  557. goto error;
  558. }
  559. skb_dst_set(skb, &rt->dst);
  560. *dstp = NULL;
  561. /* if egress device is enslaved to an L3 master device pass the
  562. * skb to its handler for processing
  563. */
  564. skb = l3mdev_ip6_out(sk, skb);
  565. if (unlikely(!skb))
  566. return 0;
  567. /* Acquire rcu_read_lock() in case we need to use rt->rt6i_idev
  568. * in the error path. Since skb has been freed, the dst could
  569. * have been queued for deletion.
  570. */
  571. rcu_read_lock();
  572. IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
  573. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
  574. NULL, rt->dst.dev, dst_output);
  575. if (err > 0)
  576. err = net_xmit_errno(err);
  577. if (err) {
  578. IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  579. rcu_read_unlock();
  580. goto error_check;
  581. }
  582. rcu_read_unlock();
  583. out:
  584. return 0;
  585. error:
  586. IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  587. error_check:
  588. if (err == -ENOBUFS && !np->recverr)
  589. err = 0;
  590. return err;
  591. }
  592. struct raw6_frag_vec {
  593. struct msghdr *msg;
  594. int hlen;
  595. char c[4];
  596. };
  597. static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
  598. {
  599. int err = 0;
  600. switch (fl6->flowi6_proto) {
  601. case IPPROTO_ICMPV6:
  602. rfv->hlen = 2;
  603. err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
  604. if (!err) {
  605. fl6->fl6_icmp_type = rfv->c[0];
  606. fl6->fl6_icmp_code = rfv->c[1];
  607. }
  608. break;
  609. case IPPROTO_MH:
  610. rfv->hlen = 4;
  611. err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
  612. if (!err)
  613. fl6->fl6_mh_type = rfv->c[2];
  614. }
  615. return err;
  616. }
  617. static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
  618. struct sk_buff *skb)
  619. {
  620. struct raw6_frag_vec *rfv = from;
  621. if (offset < rfv->hlen) {
  622. int copy = min(rfv->hlen - offset, len);
  623. if (skb->ip_summed == CHECKSUM_PARTIAL)
  624. memcpy(to, rfv->c + offset, copy);
  625. else
  626. skb->csum = csum_block_add(
  627. skb->csum,
  628. csum_partial_copy_nocheck(rfv->c + offset,
  629. to, copy, 0),
  630. odd);
  631. odd = 0;
  632. offset += copy;
  633. to += copy;
  634. len -= copy;
  635. if (!len)
  636. return 0;
  637. }
  638. offset -= rfv->hlen;
  639. return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
  640. }
  641. static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  642. {
  643. struct ipv6_txoptions *opt_to_free = NULL;
  644. struct ipv6_txoptions opt_space;
  645. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  646. struct in6_addr *daddr, *final_p, final;
  647. struct inet_sock *inet = inet_sk(sk);
  648. struct ipv6_pinfo *np = inet6_sk(sk);
  649. struct raw6_sock *rp = raw6_sk(sk);
  650. struct ipv6_txoptions *opt = NULL;
  651. struct ip6_flowlabel *flowlabel = NULL;
  652. struct dst_entry *dst = NULL;
  653. struct raw6_frag_vec rfv;
  654. struct flowi6 fl6;
  655. struct sockcm_cookie sockc;
  656. struct ipcm6_cookie ipc6;
  657. int addr_len = msg->msg_namelen;
  658. int hdrincl;
  659. u16 proto;
  660. int err;
  661. /* Rough check on arithmetic overflow,
  662. better check is made in ip6_append_data().
  663. */
  664. if (len > INT_MAX)
  665. return -EMSGSIZE;
  666. /* Mirror BSD error message compatibility */
  667. if (msg->msg_flags & MSG_OOB)
  668. return -EOPNOTSUPP;
  669. /* hdrincl should be READ_ONCE(inet->hdrincl)
  670. * but READ_ONCE() doesn't work with bit fields.
  671. * Doing this indirectly yields the same result.
  672. */
  673. hdrincl = inet->hdrincl;
  674. hdrincl = READ_ONCE(hdrincl);
  675. /*
  676. * Get and verify the address.
  677. */
  678. memset(&fl6, 0, sizeof(fl6));
  679. fl6.flowi6_mark = sk->sk_mark;
  680. fl6.flowi6_uid = sk->sk_uid;
  681. ipc6.hlimit = -1;
  682. ipc6.tclass = -1;
  683. ipc6.dontfrag = -1;
  684. ipc6.opt = NULL;
  685. if (sin6) {
  686. if (addr_len < SIN6_LEN_RFC2133)
  687. return -EINVAL;
  688. if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
  689. return -EAFNOSUPPORT;
  690. /* port is the proto value [0..255] carried in nexthdr */
  691. proto = ntohs(sin6->sin6_port);
  692. if (!proto)
  693. proto = inet->inet_num;
  694. else if (proto != inet->inet_num)
  695. return -EINVAL;
  696. if (proto > 255)
  697. return -EINVAL;
  698. daddr = &sin6->sin6_addr;
  699. if (np->sndflow) {
  700. fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  701. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  702. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  703. if (!flowlabel)
  704. return -EINVAL;
  705. }
  706. }
  707. /*
  708. * Otherwise it will be difficult to maintain
  709. * sk->sk_dst_cache.
  710. */
  711. if (sk->sk_state == TCP_ESTABLISHED &&
  712. ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
  713. daddr = &sk->sk_v6_daddr;
  714. if (addr_len >= sizeof(struct sockaddr_in6) &&
  715. sin6->sin6_scope_id &&
  716. __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
  717. fl6.flowi6_oif = sin6->sin6_scope_id;
  718. } else {
  719. if (sk->sk_state != TCP_ESTABLISHED)
  720. return -EDESTADDRREQ;
  721. proto = inet->inet_num;
  722. daddr = &sk->sk_v6_daddr;
  723. fl6.flowlabel = np->flow_label;
  724. }
  725. if (fl6.flowi6_oif == 0)
  726. fl6.flowi6_oif = sk->sk_bound_dev_if;
  727. sockc.tsflags = sk->sk_tsflags;
  728. if (msg->msg_controllen) {
  729. opt = &opt_space;
  730. memset(opt, 0, sizeof(struct ipv6_txoptions));
  731. opt->tot_len = sizeof(struct ipv6_txoptions);
  732. ipc6.opt = opt;
  733. err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
  734. if (err < 0) {
  735. fl6_sock_release(flowlabel);
  736. return err;
  737. }
  738. if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  739. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  740. if (!flowlabel)
  741. return -EINVAL;
  742. }
  743. if (!(opt->opt_nflen|opt->opt_flen))
  744. opt = NULL;
  745. }
  746. if (!opt) {
  747. opt = txopt_get(np);
  748. opt_to_free = opt;
  749. }
  750. if (flowlabel)
  751. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  752. opt = ipv6_fixup_options(&opt_space, opt);
  753. fl6.flowi6_proto = proto;
  754. if (!hdrincl) {
  755. rfv.msg = msg;
  756. rfv.hlen = 0;
  757. err = rawv6_probe_proto_opt(&rfv, &fl6);
  758. if (err)
  759. goto out;
  760. }
  761. if (!ipv6_addr_any(daddr))
  762. fl6.daddr = *daddr;
  763. else
  764. fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  765. if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
  766. fl6.saddr = np->saddr;
  767. final_p = fl6_update_dst(&fl6, opt, &final);
  768. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  769. fl6.flowi6_oif = np->mcast_oif;
  770. else if (!fl6.flowi6_oif)
  771. fl6.flowi6_oif = np->ucast_oif;
  772. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  773. if (hdrincl)
  774. fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
  775. if (ipc6.tclass < 0)
  776. ipc6.tclass = np->tclass;
  777. fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
  778. dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  779. if (IS_ERR(dst)) {
  780. err = PTR_ERR(dst);
  781. goto out;
  782. }
  783. if (ipc6.hlimit < 0)
  784. ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
  785. if (ipc6.dontfrag < 0)
  786. ipc6.dontfrag = np->dontfrag;
  787. if (msg->msg_flags&MSG_CONFIRM)
  788. goto do_confirm;
  789. back_from_confirm:
  790. if (hdrincl)
  791. err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, msg->msg_flags);
  792. else {
  793. ipc6.opt = opt;
  794. lock_sock(sk);
  795. err = ip6_append_data(sk, raw6_getfrag, &rfv,
  796. len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
  797. msg->msg_flags, &sockc);
  798. if (err)
  799. ip6_flush_pending_frames(sk);
  800. else if (!(msg->msg_flags & MSG_MORE))
  801. err = rawv6_push_pending_frames(sk, &fl6, rp);
  802. release_sock(sk);
  803. }
  804. done:
  805. dst_release(dst);
  806. out:
  807. fl6_sock_release(flowlabel);
  808. txopt_put(opt_to_free);
  809. return err < 0 ? err : len;
  810. do_confirm:
  811. dst_confirm(dst);
  812. if (!(msg->msg_flags & MSG_PROBE) || len)
  813. goto back_from_confirm;
  814. err = 0;
  815. goto done;
  816. }
  817. static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
  818. char __user *optval, int optlen)
  819. {
  820. switch (optname) {
  821. case ICMPV6_FILTER:
  822. if (optlen > sizeof(struct icmp6_filter))
  823. optlen = sizeof(struct icmp6_filter);
  824. if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
  825. return -EFAULT;
  826. return 0;
  827. default:
  828. return -ENOPROTOOPT;
  829. }
  830. return 0;
  831. }
  832. static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
  833. char __user *optval, int __user *optlen)
  834. {
  835. int len;
  836. switch (optname) {
  837. case ICMPV6_FILTER:
  838. if (get_user(len, optlen))
  839. return -EFAULT;
  840. if (len < 0)
  841. return -EINVAL;
  842. if (len > sizeof(struct icmp6_filter))
  843. len = sizeof(struct icmp6_filter);
  844. if (put_user(len, optlen))
  845. return -EFAULT;
  846. if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
  847. return -EFAULT;
  848. return 0;
  849. default:
  850. return -ENOPROTOOPT;
  851. }
  852. return 0;
  853. }
  854. static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
  855. char __user *optval, unsigned int optlen)
  856. {
  857. struct raw6_sock *rp = raw6_sk(sk);
  858. int val;
  859. if (get_user(val, (int __user *)optval))
  860. return -EFAULT;
  861. switch (optname) {
  862. case IPV6_HDRINCL:
  863. if (sk->sk_type != SOCK_RAW)
  864. return -EINVAL;
  865. inet_sk(sk)->hdrincl = !!val;
  866. return 0;
  867. case IPV6_CHECKSUM:
  868. if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
  869. level == IPPROTO_IPV6) {
  870. /*
  871. * RFC3542 tells that IPV6_CHECKSUM socket
  872. * option in the IPPROTO_IPV6 level is not
  873. * allowed on ICMPv6 sockets.
  874. * If you want to set it, use IPPROTO_RAW
  875. * level IPV6_CHECKSUM socket option
  876. * (Linux extension).
  877. */
  878. return -EINVAL;
  879. }
  880. /* You may get strange result with a positive odd offset;
  881. RFC2292bis agrees with me. */
  882. if (val > 0 && (val&1))
  883. return -EINVAL;
  884. if (val < 0) {
  885. rp->checksum = 0;
  886. } else {
  887. rp->checksum = 1;
  888. rp->offset = val;
  889. }
  890. return 0;
  891. default:
  892. return -ENOPROTOOPT;
  893. }
  894. }
  895. static int rawv6_setsockopt(struct sock *sk, int level, int optname,
  896. char __user *optval, unsigned int optlen)
  897. {
  898. switch (level) {
  899. case SOL_RAW:
  900. break;
  901. case SOL_ICMPV6:
  902. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  903. return -EOPNOTSUPP;
  904. return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
  905. case SOL_IPV6:
  906. if (optname == IPV6_CHECKSUM ||
  907. optname == IPV6_HDRINCL)
  908. break;
  909. default:
  910. return ipv6_setsockopt(sk, level, optname, optval, optlen);
  911. }
  912. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  913. }
  914. #ifdef CONFIG_COMPAT
  915. static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
  916. char __user *optval, unsigned int optlen)
  917. {
  918. switch (level) {
  919. case SOL_RAW:
  920. break;
  921. case SOL_ICMPV6:
  922. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  923. return -EOPNOTSUPP;
  924. return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
  925. case SOL_IPV6:
  926. if (optname == IPV6_CHECKSUM ||
  927. optname == IPV6_HDRINCL)
  928. break;
  929. default:
  930. return compat_ipv6_setsockopt(sk, level, optname,
  931. optval, optlen);
  932. }
  933. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  934. }
  935. #endif
  936. static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
  937. char __user *optval, int __user *optlen)
  938. {
  939. struct raw6_sock *rp = raw6_sk(sk);
  940. int val, len;
  941. if (get_user(len, optlen))
  942. return -EFAULT;
  943. switch (optname) {
  944. case IPV6_HDRINCL:
  945. val = inet_sk(sk)->hdrincl;
  946. break;
  947. case IPV6_CHECKSUM:
  948. /*
  949. * We allow getsockopt() for IPPROTO_IPV6-level
  950. * IPV6_CHECKSUM socket option on ICMPv6 sockets
  951. * since RFC3542 is silent about it.
  952. */
  953. if (rp->checksum == 0)
  954. val = -1;
  955. else
  956. val = rp->offset;
  957. break;
  958. default:
  959. return -ENOPROTOOPT;
  960. }
  961. len = min_t(unsigned int, sizeof(int), len);
  962. if (put_user(len, optlen))
  963. return -EFAULT;
  964. if (copy_to_user(optval, &val, len))
  965. return -EFAULT;
  966. return 0;
  967. }
  968. static int rawv6_getsockopt(struct sock *sk, int level, int optname,
  969. char __user *optval, int __user *optlen)
  970. {
  971. switch (level) {
  972. case SOL_RAW:
  973. break;
  974. case SOL_ICMPV6:
  975. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  976. return -EOPNOTSUPP;
  977. return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
  978. case SOL_IPV6:
  979. if (optname == IPV6_CHECKSUM ||
  980. optname == IPV6_HDRINCL)
  981. break;
  982. default:
  983. return ipv6_getsockopt(sk, level, optname, optval, optlen);
  984. }
  985. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  986. }
  987. #ifdef CONFIG_COMPAT
  988. static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
  989. char __user *optval, int __user *optlen)
  990. {
  991. switch (level) {
  992. case SOL_RAW:
  993. break;
  994. case SOL_ICMPV6:
  995. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  996. return -EOPNOTSUPP;
  997. return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
  998. case SOL_IPV6:
  999. if (optname == IPV6_CHECKSUM ||
  1000. optname == IPV6_HDRINCL)
  1001. break;
  1002. default:
  1003. return compat_ipv6_getsockopt(sk, level, optname,
  1004. optval, optlen);
  1005. }
  1006. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  1007. }
  1008. #endif
  1009. static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
  1010. {
  1011. switch (cmd) {
  1012. case SIOCOUTQ: {
  1013. int amount = sk_wmem_alloc_get(sk);
  1014. return put_user(amount, (int __user *)arg);
  1015. }
  1016. case SIOCINQ: {
  1017. struct sk_buff *skb;
  1018. int amount = 0;
  1019. spin_lock_bh(&sk->sk_receive_queue.lock);
  1020. skb = skb_peek(&sk->sk_receive_queue);
  1021. if (skb)
  1022. amount = skb->len;
  1023. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1024. return put_user(amount, (int __user *)arg);
  1025. }
  1026. default:
  1027. #ifdef CONFIG_IPV6_MROUTE
  1028. return ip6mr_ioctl(sk, cmd, (void __user *)arg);
  1029. #else
  1030. return -ENOIOCTLCMD;
  1031. #endif
  1032. }
  1033. }
  1034. #ifdef CONFIG_COMPAT
  1035. static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
  1036. {
  1037. switch (cmd) {
  1038. case SIOCOUTQ:
  1039. case SIOCINQ:
  1040. return -ENOIOCTLCMD;
  1041. default:
  1042. #ifdef CONFIG_IPV6_MROUTE
  1043. return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
  1044. #else
  1045. return -ENOIOCTLCMD;
  1046. #endif
  1047. }
  1048. }
  1049. #endif
  1050. static void rawv6_close(struct sock *sk, long timeout)
  1051. {
  1052. if (inet_sk(sk)->inet_num == IPPROTO_RAW)
  1053. ip6_ra_control(sk, -1);
  1054. ip6mr_sk_done(sk);
  1055. sk_common_release(sk);
  1056. }
  1057. static void raw6_destroy(struct sock *sk)
  1058. {
  1059. lock_sock(sk);
  1060. ip6_flush_pending_frames(sk);
  1061. release_sock(sk);
  1062. inet6_destroy_sock(sk);
  1063. }
  1064. static int rawv6_init_sk(struct sock *sk)
  1065. {
  1066. struct raw6_sock *rp = raw6_sk(sk);
  1067. switch (inet_sk(sk)->inet_num) {
  1068. case IPPROTO_ICMPV6:
  1069. rp->checksum = 1;
  1070. rp->offset = 2;
  1071. break;
  1072. case IPPROTO_MH:
  1073. rp->checksum = 1;
  1074. rp->offset = 4;
  1075. break;
  1076. default:
  1077. break;
  1078. }
  1079. return 0;
  1080. }
  1081. struct proto rawv6_prot = {
  1082. .name = "RAWv6",
  1083. .owner = THIS_MODULE,
  1084. .close = rawv6_close,
  1085. .destroy = raw6_destroy,
  1086. .connect = ip6_datagram_connect_v6_only,
  1087. .disconnect = __udp_disconnect,
  1088. .ioctl = rawv6_ioctl,
  1089. .init = rawv6_init_sk,
  1090. .setsockopt = rawv6_setsockopt,
  1091. .getsockopt = rawv6_getsockopt,
  1092. .sendmsg = rawv6_sendmsg,
  1093. .recvmsg = rawv6_recvmsg,
  1094. .bind = rawv6_bind,
  1095. .backlog_rcv = rawv6_rcv_skb,
  1096. .hash = raw_hash_sk,
  1097. .unhash = raw_unhash_sk,
  1098. .obj_size = sizeof(struct raw6_sock),
  1099. .h.raw_hash = &raw_v6_hashinfo,
  1100. #ifdef CONFIG_COMPAT
  1101. .compat_setsockopt = compat_rawv6_setsockopt,
  1102. .compat_getsockopt = compat_rawv6_getsockopt,
  1103. .compat_ioctl = compat_rawv6_ioctl,
  1104. #endif
  1105. };
  1106. #ifdef CONFIG_PROC_FS
  1107. static int raw6_seq_show(struct seq_file *seq, void *v)
  1108. {
  1109. if (v == SEQ_START_TOKEN) {
  1110. seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
  1111. } else {
  1112. struct sock *sp = v;
  1113. __u16 srcp = inet_sk(sp)->inet_num;
  1114. ip6_dgram_sock_seq_show(seq, v, srcp, 0,
  1115. raw_seq_private(seq)->bucket);
  1116. }
  1117. return 0;
  1118. }
  1119. static const struct seq_operations raw6_seq_ops = {
  1120. .start = raw_seq_start,
  1121. .next = raw_seq_next,
  1122. .stop = raw_seq_stop,
  1123. .show = raw6_seq_show,
  1124. };
  1125. static int raw6_seq_open(struct inode *inode, struct file *file)
  1126. {
  1127. return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops);
  1128. }
  1129. static const struct file_operations raw6_seq_fops = {
  1130. .owner = THIS_MODULE,
  1131. .open = raw6_seq_open,
  1132. .read = seq_read,
  1133. .llseek = seq_lseek,
  1134. .release = seq_release_net,
  1135. };
  1136. static int __net_init raw6_init_net(struct net *net)
  1137. {
  1138. if (!proc_create("raw6", S_IRUGO, net->proc_net, &raw6_seq_fops))
  1139. return -ENOMEM;
  1140. return 0;
  1141. }
  1142. static void __net_exit raw6_exit_net(struct net *net)
  1143. {
  1144. remove_proc_entry("raw6", net->proc_net);
  1145. }
  1146. static struct pernet_operations raw6_net_ops = {
  1147. .init = raw6_init_net,
  1148. .exit = raw6_exit_net,
  1149. };
  1150. int __init raw6_proc_init(void)
  1151. {
  1152. return register_pernet_subsys(&raw6_net_ops);
  1153. }
  1154. void raw6_proc_exit(void)
  1155. {
  1156. unregister_pernet_subsys(&raw6_net_ops);
  1157. }
  1158. #endif /* CONFIG_PROC_FS */
  1159. /* Same as inet6_dgram_ops, sans udp_poll. */
  1160. const struct proto_ops inet6_sockraw_ops = {
  1161. .family = PF_INET6,
  1162. .owner = THIS_MODULE,
  1163. .release = inet6_release,
  1164. .bind = inet6_bind,
  1165. .connect = inet_dgram_connect, /* ok */
  1166. .socketpair = sock_no_socketpair, /* a do nothing */
  1167. .accept = sock_no_accept, /* a do nothing */
  1168. .getname = inet6_getname,
  1169. .poll = datagram_poll, /* ok */
  1170. .ioctl = inet6_ioctl, /* must change */
  1171. .listen = sock_no_listen, /* ok */
  1172. .shutdown = inet_shutdown, /* ok */
  1173. .setsockopt = sock_common_setsockopt, /* ok */
  1174. .getsockopt = sock_common_getsockopt, /* ok */
  1175. .sendmsg = inet_sendmsg, /* ok */
  1176. .recvmsg = sock_common_recvmsg, /* ok */
  1177. .mmap = sock_no_mmap,
  1178. .sendpage = sock_no_sendpage,
  1179. #ifdef CONFIG_COMPAT
  1180. .compat_setsockopt = compat_sock_common_setsockopt,
  1181. .compat_getsockopt = compat_sock_common_getsockopt,
  1182. #endif
  1183. };
  1184. static struct inet_protosw rawv6_protosw = {
  1185. .type = SOCK_RAW,
  1186. .protocol = IPPROTO_IP, /* wild card */
  1187. .prot = &rawv6_prot,
  1188. .ops = &inet6_sockraw_ops,
  1189. .flags = INET_PROTOSW_REUSE,
  1190. };
  1191. int __init rawv6_init(void)
  1192. {
  1193. return inet6_register_protosw(&rawv6_protosw);
  1194. }
  1195. void rawv6_exit(void)
  1196. {
  1197. inet6_unregister_protosw(&rawv6_protosw);
  1198. }