raw.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375
  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. * 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. err = -ENODEV;
  241. dev = dev_get_by_index_rcu(sock_net(sk),
  242. sk->sk_bound_dev_if);
  243. if (!dev)
  244. goto out_unlock;
  245. }
  246. /* ipv4 addr of the socket is invalid. Only the
  247. * unspecified and mapped address have a v4 equivalent.
  248. */
  249. v4addr = LOOPBACK4_IPV6;
  250. if (!(addr_type & IPV6_ADDR_MULTICAST) &&
  251. !sock_net(sk)->ipv6.sysctl.ip_nonlocal_bind) {
  252. err = -EADDRNOTAVAIL;
  253. if (!ipv6_chk_addr(sock_net(sk), &addr->sin6_addr,
  254. dev, 0)) {
  255. goto out_unlock;
  256. }
  257. }
  258. }
  259. inet->inet_rcv_saddr = inet->inet_saddr = v4addr;
  260. sk->sk_v6_rcv_saddr = addr->sin6_addr;
  261. if (!(addr_type & IPV6_ADDR_MULTICAST))
  262. np->saddr = addr->sin6_addr;
  263. err = 0;
  264. out_unlock:
  265. rcu_read_unlock();
  266. out:
  267. release_sock(sk);
  268. return err;
  269. }
  270. static void rawv6_err(struct sock *sk, struct sk_buff *skb,
  271. struct inet6_skb_parm *opt,
  272. u8 type, u8 code, int offset, __be32 info)
  273. {
  274. struct inet_sock *inet = inet_sk(sk);
  275. struct ipv6_pinfo *np = inet6_sk(sk);
  276. int err;
  277. int harderr;
  278. /* Report error on raw socket, if:
  279. 1. User requested recverr.
  280. 2. Socket is connected (otherwise the error indication
  281. is useless without recverr and error is hard.
  282. */
  283. if (!np->recverr && sk->sk_state != TCP_ESTABLISHED)
  284. return;
  285. harderr = icmpv6_err_convert(type, code, &err);
  286. if (type == ICMPV6_PKT_TOOBIG) {
  287. ip6_sk_update_pmtu(skb, sk, info);
  288. harderr = (np->pmtudisc == IPV6_PMTUDISC_DO);
  289. }
  290. if (type == NDISC_REDIRECT) {
  291. ip6_sk_redirect(skb, sk);
  292. return;
  293. }
  294. if (np->recverr) {
  295. u8 *payload = skb->data;
  296. if (!inet->hdrincl)
  297. payload += offset;
  298. ipv6_icmp_error(sk, skb, err, 0, ntohl(info), payload);
  299. }
  300. if (np->recverr || harderr) {
  301. sk->sk_err = err;
  302. sk->sk_error_report(sk);
  303. }
  304. }
  305. void raw6_icmp_error(struct sk_buff *skb, int nexthdr,
  306. u8 type, u8 code, int inner_offset, __be32 info)
  307. {
  308. struct sock *sk;
  309. int hash;
  310. const struct in6_addr *saddr, *daddr;
  311. struct net *net;
  312. hash = nexthdr & (RAW_HTABLE_SIZE - 1);
  313. read_lock(&raw_v6_hashinfo.lock);
  314. sk = sk_head(&raw_v6_hashinfo.ht[hash]);
  315. if (sk) {
  316. /* Note: ipv6_hdr(skb) != skb->data */
  317. const struct ipv6hdr *ip6h = (const struct ipv6hdr *)skb->data;
  318. saddr = &ip6h->saddr;
  319. daddr = &ip6h->daddr;
  320. net = dev_net(skb->dev);
  321. while ((sk = __raw_v6_lookup(net, sk, nexthdr, saddr, daddr,
  322. inet6_iif(skb)))) {
  323. rawv6_err(sk, skb, NULL, type, code,
  324. inner_offset, info);
  325. sk = sk_next(sk);
  326. }
  327. }
  328. read_unlock(&raw_v6_hashinfo.lock);
  329. }
  330. static inline int rawv6_rcv_skb(struct sock *sk, struct sk_buff *skb)
  331. {
  332. if ((raw6_sk(sk)->checksum || rcu_access_pointer(sk->sk_filter)) &&
  333. skb_checksum_complete(skb)) {
  334. atomic_inc(&sk->sk_drops);
  335. kfree_skb(skb);
  336. return NET_RX_DROP;
  337. }
  338. /* Charge it to the socket. */
  339. skb_dst_drop(skb);
  340. if (sock_queue_rcv_skb(sk, skb) < 0) {
  341. kfree_skb(skb);
  342. return NET_RX_DROP;
  343. }
  344. return 0;
  345. }
  346. /*
  347. * This is next to useless...
  348. * if we demultiplex in network layer we don't need the extra call
  349. * just to queue the skb...
  350. * maybe we could have the network decide upon a hint if it
  351. * should call raw_rcv for demultiplexing
  352. */
  353. int rawv6_rcv(struct sock *sk, struct sk_buff *skb)
  354. {
  355. struct inet_sock *inet = inet_sk(sk);
  356. struct raw6_sock *rp = raw6_sk(sk);
  357. if (!xfrm6_policy_check(sk, XFRM_POLICY_IN, skb)) {
  358. atomic_inc(&sk->sk_drops);
  359. kfree_skb(skb);
  360. return NET_RX_DROP;
  361. }
  362. if (!rp->checksum)
  363. skb->ip_summed = CHECKSUM_UNNECESSARY;
  364. if (skb->ip_summed == CHECKSUM_COMPLETE) {
  365. skb_postpull_rcsum(skb, skb_network_header(skb),
  366. skb_network_header_len(skb));
  367. if (!csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  368. &ipv6_hdr(skb)->daddr,
  369. skb->len, inet->inet_num, skb->csum))
  370. skb->ip_summed = CHECKSUM_UNNECESSARY;
  371. }
  372. if (!skb_csum_unnecessary(skb))
  373. skb->csum = ~csum_unfold(csum_ipv6_magic(&ipv6_hdr(skb)->saddr,
  374. &ipv6_hdr(skb)->daddr,
  375. skb->len,
  376. inet->inet_num, 0));
  377. if (inet->hdrincl) {
  378. if (skb_checksum_complete(skb)) {
  379. atomic_inc(&sk->sk_drops);
  380. kfree_skb(skb);
  381. return NET_RX_DROP;
  382. }
  383. }
  384. rawv6_rcv_skb(sk, skb);
  385. return 0;
  386. }
  387. /*
  388. * This should be easy, if there is something there
  389. * we return it, otherwise we block.
  390. */
  391. static int rawv6_recvmsg(struct sock *sk, struct msghdr *msg, size_t len,
  392. int noblock, int flags, int *addr_len)
  393. {
  394. struct ipv6_pinfo *np = inet6_sk(sk);
  395. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  396. struct sk_buff *skb;
  397. size_t copied;
  398. int err;
  399. if (flags & MSG_OOB)
  400. return -EOPNOTSUPP;
  401. if (flags & MSG_ERRQUEUE)
  402. return ipv6_recv_error(sk, msg, len, addr_len);
  403. if (np->rxpmtu && np->rxopt.bits.rxpmtu)
  404. return ipv6_recv_rxpmtu(sk, msg, len, addr_len);
  405. skb = skb_recv_datagram(sk, flags, noblock, &err);
  406. if (!skb)
  407. goto out;
  408. copied = skb->len;
  409. if (copied > len) {
  410. copied = len;
  411. msg->msg_flags |= MSG_TRUNC;
  412. }
  413. if (skb_csum_unnecessary(skb)) {
  414. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  415. } else if (msg->msg_flags&MSG_TRUNC) {
  416. if (__skb_checksum_complete(skb))
  417. goto csum_copy_err;
  418. err = skb_copy_datagram_msg(skb, 0, msg, copied);
  419. } else {
  420. err = skb_copy_and_csum_datagram_msg(skb, 0, msg);
  421. if (err == -EINVAL)
  422. goto csum_copy_err;
  423. }
  424. if (err)
  425. goto out_free;
  426. /* Copy the address. */
  427. if (sin6) {
  428. sin6->sin6_family = AF_INET6;
  429. sin6->sin6_port = 0;
  430. sin6->sin6_addr = ipv6_hdr(skb)->saddr;
  431. sin6->sin6_flowinfo = 0;
  432. sin6->sin6_scope_id = ipv6_iface_scope_id(&sin6->sin6_addr,
  433. inet6_iif(skb));
  434. *addr_len = sizeof(*sin6);
  435. }
  436. sock_recv_ts_and_drops(msg, sk, skb);
  437. if (np->rxopt.all)
  438. ip6_datagram_recv_ctl(sk, msg, skb);
  439. err = copied;
  440. if (flags & MSG_TRUNC)
  441. err = skb->len;
  442. out_free:
  443. skb_free_datagram(sk, skb);
  444. out:
  445. return err;
  446. csum_copy_err:
  447. skb_kill_datagram(sk, skb, flags);
  448. /* Error for blocking case is chosen to masquerade
  449. as some normal condition.
  450. */
  451. err = (flags&MSG_DONTWAIT) ? -EAGAIN : -EHOSTUNREACH;
  452. goto out;
  453. }
  454. static int rawv6_push_pending_frames(struct sock *sk, struct flowi6 *fl6,
  455. struct raw6_sock *rp)
  456. {
  457. struct sk_buff *skb;
  458. int err = 0;
  459. int offset;
  460. int len;
  461. int total_len;
  462. __wsum tmp_csum;
  463. __sum16 csum;
  464. if (!rp->checksum)
  465. goto send;
  466. skb = skb_peek(&sk->sk_write_queue);
  467. if (!skb)
  468. goto out;
  469. offset = rp->offset;
  470. total_len = inet_sk(sk)->cork.base.length;
  471. if (offset >= total_len - 1) {
  472. err = -EINVAL;
  473. ip6_flush_pending_frames(sk);
  474. goto out;
  475. }
  476. /* should be check HW csum miyazawa */
  477. if (skb_queue_len(&sk->sk_write_queue) == 1) {
  478. /*
  479. * Only one fragment on the socket.
  480. */
  481. tmp_csum = skb->csum;
  482. } else {
  483. struct sk_buff *csum_skb = NULL;
  484. tmp_csum = 0;
  485. skb_queue_walk(&sk->sk_write_queue, skb) {
  486. tmp_csum = csum_add(tmp_csum, skb->csum);
  487. if (csum_skb)
  488. continue;
  489. len = skb->len - skb_transport_offset(skb);
  490. if (offset >= len) {
  491. offset -= len;
  492. continue;
  493. }
  494. csum_skb = skb;
  495. }
  496. skb = csum_skb;
  497. }
  498. offset += skb_transport_offset(skb);
  499. err = skb_copy_bits(skb, offset, &csum, 2);
  500. if (err < 0) {
  501. ip6_flush_pending_frames(sk);
  502. goto out;
  503. }
  504. /* in case cksum was not initialized */
  505. if (unlikely(csum))
  506. tmp_csum = csum_sub(tmp_csum, csum_unfold(csum));
  507. csum = csum_ipv6_magic(&fl6->saddr, &fl6->daddr,
  508. total_len, fl6->flowi6_proto, tmp_csum);
  509. if (csum == 0 && fl6->flowi6_proto == IPPROTO_UDP)
  510. csum = CSUM_MANGLED_0;
  511. BUG_ON(skb_store_bits(skb, offset, &csum, 2));
  512. send:
  513. err = ip6_push_pending_frames(sk);
  514. out:
  515. return err;
  516. }
  517. static int rawv6_send_hdrinc(struct sock *sk, struct msghdr *msg, int length,
  518. struct flowi6 *fl6, struct dst_entry **dstp,
  519. unsigned int flags)
  520. {
  521. struct ipv6_pinfo *np = inet6_sk(sk);
  522. struct net *net = sock_net(sk);
  523. struct ipv6hdr *iph;
  524. struct sk_buff *skb;
  525. int err;
  526. struct rt6_info *rt = (struct rt6_info *)*dstp;
  527. int hlen = LL_RESERVED_SPACE(rt->dst.dev);
  528. int tlen = rt->dst.dev->needed_tailroom;
  529. if (length > rt->dst.dev->mtu) {
  530. ipv6_local_error(sk, EMSGSIZE, fl6, rt->dst.dev->mtu);
  531. return -EMSGSIZE;
  532. }
  533. if (length < sizeof(struct ipv6hdr))
  534. return -EINVAL;
  535. if (flags&MSG_PROBE)
  536. goto out;
  537. skb = sock_alloc_send_skb(sk,
  538. length + hlen + tlen + 15,
  539. flags & MSG_DONTWAIT, &err);
  540. if (!skb)
  541. goto error;
  542. skb_reserve(skb, hlen);
  543. skb->protocol = htons(ETH_P_IPV6);
  544. skb->priority = sk->sk_priority;
  545. skb->mark = sk->sk_mark;
  546. skb_dst_set(skb, &rt->dst);
  547. *dstp = NULL;
  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. goto error_fault;
  556. /* if egress device is enslaved to an L3 master device pass the
  557. * skb to its handler for processing
  558. */
  559. skb = l3mdev_ip6_out(sk, skb);
  560. if (unlikely(!skb))
  561. return 0;
  562. IP6_UPD_PO_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUT, skb->len);
  563. err = NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, net, sk, skb,
  564. NULL, rt->dst.dev, dst_output);
  565. if (err > 0)
  566. err = net_xmit_errno(err);
  567. if (err)
  568. goto error;
  569. out:
  570. return 0;
  571. error_fault:
  572. err = -EFAULT;
  573. kfree_skb(skb);
  574. error:
  575. IP6_INC_STATS(net, rt->rt6i_idev, IPSTATS_MIB_OUTDISCARDS);
  576. if (err == -ENOBUFS && !np->recverr)
  577. err = 0;
  578. return err;
  579. }
  580. struct raw6_frag_vec {
  581. struct msghdr *msg;
  582. int hlen;
  583. char c[4];
  584. };
  585. static int rawv6_probe_proto_opt(struct raw6_frag_vec *rfv, struct flowi6 *fl6)
  586. {
  587. int err = 0;
  588. switch (fl6->flowi6_proto) {
  589. case IPPROTO_ICMPV6:
  590. rfv->hlen = 2;
  591. err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
  592. if (!err) {
  593. fl6->fl6_icmp_type = rfv->c[0];
  594. fl6->fl6_icmp_code = rfv->c[1];
  595. }
  596. break;
  597. case IPPROTO_MH:
  598. rfv->hlen = 4;
  599. err = memcpy_from_msg(rfv->c, rfv->msg, rfv->hlen);
  600. if (!err)
  601. fl6->fl6_mh_type = rfv->c[2];
  602. }
  603. return err;
  604. }
  605. static int raw6_getfrag(void *from, char *to, int offset, int len, int odd,
  606. struct sk_buff *skb)
  607. {
  608. struct raw6_frag_vec *rfv = from;
  609. if (offset < rfv->hlen) {
  610. int copy = min(rfv->hlen - offset, len);
  611. if (skb->ip_summed == CHECKSUM_PARTIAL)
  612. memcpy(to, rfv->c + offset, copy);
  613. else
  614. skb->csum = csum_block_add(
  615. skb->csum,
  616. csum_partial_copy_nocheck(rfv->c + offset,
  617. to, copy, 0),
  618. odd);
  619. odd = 0;
  620. offset += copy;
  621. to += copy;
  622. len -= copy;
  623. if (!len)
  624. return 0;
  625. }
  626. offset -= rfv->hlen;
  627. return ip_generic_getfrag(rfv->msg, to, offset, len, odd, skb);
  628. }
  629. static int rawv6_sendmsg(struct sock *sk, struct msghdr *msg, size_t len)
  630. {
  631. struct ipv6_txoptions *opt_to_free = NULL;
  632. struct ipv6_txoptions opt_space;
  633. DECLARE_SOCKADDR(struct sockaddr_in6 *, sin6, msg->msg_name);
  634. struct in6_addr *daddr, *final_p, final;
  635. struct inet_sock *inet = inet_sk(sk);
  636. struct ipv6_pinfo *np = inet6_sk(sk);
  637. struct raw6_sock *rp = raw6_sk(sk);
  638. struct ipv6_txoptions *opt = NULL;
  639. struct ip6_flowlabel *flowlabel = NULL;
  640. struct dst_entry *dst = NULL;
  641. struct raw6_frag_vec rfv;
  642. struct flowi6 fl6;
  643. struct sockcm_cookie sockc;
  644. struct ipcm6_cookie ipc6;
  645. int addr_len = msg->msg_namelen;
  646. u16 proto;
  647. int err;
  648. /* Rough check on arithmetic overflow,
  649. better check is made in ip6_append_data().
  650. */
  651. if (len > INT_MAX)
  652. return -EMSGSIZE;
  653. /* Mirror BSD error message compatibility */
  654. if (msg->msg_flags & MSG_OOB)
  655. return -EOPNOTSUPP;
  656. /*
  657. * Get and verify the address.
  658. */
  659. memset(&fl6, 0, sizeof(fl6));
  660. fl6.flowi6_mark = sk->sk_mark;
  661. ipc6.hlimit = -1;
  662. ipc6.tclass = -1;
  663. ipc6.dontfrag = -1;
  664. ipc6.opt = NULL;
  665. if (sin6) {
  666. if (addr_len < SIN6_LEN_RFC2133)
  667. return -EINVAL;
  668. if (sin6->sin6_family && sin6->sin6_family != AF_INET6)
  669. return -EAFNOSUPPORT;
  670. /* port is the proto value [0..255] carried in nexthdr */
  671. proto = ntohs(sin6->sin6_port);
  672. if (!proto)
  673. proto = inet->inet_num;
  674. else if (proto != inet->inet_num)
  675. return -EINVAL;
  676. if (proto > 255)
  677. return -EINVAL;
  678. daddr = &sin6->sin6_addr;
  679. if (np->sndflow) {
  680. fl6.flowlabel = sin6->sin6_flowinfo&IPV6_FLOWINFO_MASK;
  681. if (fl6.flowlabel&IPV6_FLOWLABEL_MASK) {
  682. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  683. if (!flowlabel)
  684. return -EINVAL;
  685. }
  686. }
  687. /*
  688. * Otherwise it will be difficult to maintain
  689. * sk->sk_dst_cache.
  690. */
  691. if (sk->sk_state == TCP_ESTABLISHED &&
  692. ipv6_addr_equal(daddr, &sk->sk_v6_daddr))
  693. daddr = &sk->sk_v6_daddr;
  694. if (addr_len >= sizeof(struct sockaddr_in6) &&
  695. sin6->sin6_scope_id &&
  696. __ipv6_addr_needs_scope_id(__ipv6_addr_type(daddr)))
  697. fl6.flowi6_oif = sin6->sin6_scope_id;
  698. } else {
  699. if (sk->sk_state != TCP_ESTABLISHED)
  700. return -EDESTADDRREQ;
  701. proto = inet->inet_num;
  702. daddr = &sk->sk_v6_daddr;
  703. fl6.flowlabel = np->flow_label;
  704. }
  705. if (fl6.flowi6_oif == 0)
  706. fl6.flowi6_oif = sk->sk_bound_dev_if;
  707. sockc.tsflags = sk->sk_tsflags;
  708. if (msg->msg_controllen) {
  709. opt = &opt_space;
  710. memset(opt, 0, sizeof(struct ipv6_txoptions));
  711. opt->tot_len = sizeof(struct ipv6_txoptions);
  712. ipc6.opt = opt;
  713. err = ip6_datagram_send_ctl(sock_net(sk), sk, msg, &fl6, &ipc6, &sockc);
  714. if (err < 0) {
  715. fl6_sock_release(flowlabel);
  716. return err;
  717. }
  718. if ((fl6.flowlabel&IPV6_FLOWLABEL_MASK) && !flowlabel) {
  719. flowlabel = fl6_sock_lookup(sk, fl6.flowlabel);
  720. if (!flowlabel)
  721. return -EINVAL;
  722. }
  723. if (!(opt->opt_nflen|opt->opt_flen))
  724. opt = NULL;
  725. }
  726. if (!opt) {
  727. opt = txopt_get(np);
  728. opt_to_free = opt;
  729. }
  730. if (flowlabel)
  731. opt = fl6_merge_options(&opt_space, flowlabel, opt);
  732. opt = ipv6_fixup_options(&opt_space, opt);
  733. fl6.flowi6_proto = proto;
  734. rfv.msg = msg;
  735. rfv.hlen = 0;
  736. err = rawv6_probe_proto_opt(&rfv, &fl6);
  737. if (err)
  738. goto out;
  739. if (!ipv6_addr_any(daddr))
  740. fl6.daddr = *daddr;
  741. else
  742. fl6.daddr.s6_addr[15] = 0x1; /* :: means loopback (BSD'ism) */
  743. if (ipv6_addr_any(&fl6.saddr) && !ipv6_addr_any(&np->saddr))
  744. fl6.saddr = np->saddr;
  745. final_p = fl6_update_dst(&fl6, opt, &final);
  746. if (!fl6.flowi6_oif && ipv6_addr_is_multicast(&fl6.daddr))
  747. fl6.flowi6_oif = np->mcast_oif;
  748. else if (!fl6.flowi6_oif)
  749. fl6.flowi6_oif = np->ucast_oif;
  750. security_sk_classify_flow(sk, flowi6_to_flowi(&fl6));
  751. if (inet->hdrincl)
  752. fl6.flowi6_flags |= FLOWI_FLAG_KNOWN_NH;
  753. if (ipc6.tclass < 0)
  754. ipc6.tclass = np->tclass;
  755. fl6.flowlabel = ip6_make_flowinfo(ipc6.tclass, fl6.flowlabel);
  756. dst = ip6_dst_lookup_flow(sk, &fl6, final_p);
  757. if (IS_ERR(dst)) {
  758. err = PTR_ERR(dst);
  759. goto out;
  760. }
  761. if (ipc6.hlimit < 0)
  762. ipc6.hlimit = ip6_sk_dst_hoplimit(np, &fl6, dst);
  763. if (ipc6.dontfrag < 0)
  764. ipc6.dontfrag = np->dontfrag;
  765. if (msg->msg_flags&MSG_CONFIRM)
  766. goto do_confirm;
  767. back_from_confirm:
  768. if (inet->hdrincl)
  769. err = rawv6_send_hdrinc(sk, msg, len, &fl6, &dst, msg->msg_flags);
  770. else {
  771. ipc6.opt = opt;
  772. lock_sock(sk);
  773. err = ip6_append_data(sk, raw6_getfrag, &rfv,
  774. len, 0, &ipc6, &fl6, (struct rt6_info *)dst,
  775. msg->msg_flags, &sockc);
  776. if (err)
  777. ip6_flush_pending_frames(sk);
  778. else if (!(msg->msg_flags & MSG_MORE))
  779. err = rawv6_push_pending_frames(sk, &fl6, rp);
  780. release_sock(sk);
  781. }
  782. done:
  783. dst_release(dst);
  784. out:
  785. fl6_sock_release(flowlabel);
  786. txopt_put(opt_to_free);
  787. return err < 0 ? err : len;
  788. do_confirm:
  789. dst_confirm(dst);
  790. if (!(msg->msg_flags & MSG_PROBE) || len)
  791. goto back_from_confirm;
  792. err = 0;
  793. goto done;
  794. }
  795. static int rawv6_seticmpfilter(struct sock *sk, int level, int optname,
  796. char __user *optval, int optlen)
  797. {
  798. switch (optname) {
  799. case ICMPV6_FILTER:
  800. if (optlen > sizeof(struct icmp6_filter))
  801. optlen = sizeof(struct icmp6_filter);
  802. if (copy_from_user(&raw6_sk(sk)->filter, optval, optlen))
  803. return -EFAULT;
  804. return 0;
  805. default:
  806. return -ENOPROTOOPT;
  807. }
  808. return 0;
  809. }
  810. static int rawv6_geticmpfilter(struct sock *sk, int level, int optname,
  811. char __user *optval, int __user *optlen)
  812. {
  813. int len;
  814. switch (optname) {
  815. case ICMPV6_FILTER:
  816. if (get_user(len, optlen))
  817. return -EFAULT;
  818. if (len < 0)
  819. return -EINVAL;
  820. if (len > sizeof(struct icmp6_filter))
  821. len = sizeof(struct icmp6_filter);
  822. if (put_user(len, optlen))
  823. return -EFAULT;
  824. if (copy_to_user(optval, &raw6_sk(sk)->filter, len))
  825. return -EFAULT;
  826. return 0;
  827. default:
  828. return -ENOPROTOOPT;
  829. }
  830. return 0;
  831. }
  832. static int do_rawv6_setsockopt(struct sock *sk, int level, int optname,
  833. char __user *optval, unsigned int optlen)
  834. {
  835. struct raw6_sock *rp = raw6_sk(sk);
  836. int val;
  837. if (get_user(val, (int __user *)optval))
  838. return -EFAULT;
  839. switch (optname) {
  840. case IPV6_HDRINCL:
  841. if (sk->sk_type != SOCK_RAW)
  842. return -EINVAL;
  843. inet_sk(sk)->hdrincl = !!val;
  844. return 0;
  845. case IPV6_CHECKSUM:
  846. if (inet_sk(sk)->inet_num == IPPROTO_ICMPV6 &&
  847. level == IPPROTO_IPV6) {
  848. /*
  849. * RFC3542 tells that IPV6_CHECKSUM socket
  850. * option in the IPPROTO_IPV6 level is not
  851. * allowed on ICMPv6 sockets.
  852. * If you want to set it, use IPPROTO_RAW
  853. * level IPV6_CHECKSUM socket option
  854. * (Linux extension).
  855. */
  856. return -EINVAL;
  857. }
  858. /* You may get strange result with a positive odd offset;
  859. RFC2292bis agrees with me. */
  860. if (val > 0 && (val&1))
  861. return -EINVAL;
  862. if (val < 0) {
  863. rp->checksum = 0;
  864. } else {
  865. rp->checksum = 1;
  866. rp->offset = val;
  867. }
  868. return 0;
  869. default:
  870. return -ENOPROTOOPT;
  871. }
  872. }
  873. static int rawv6_setsockopt(struct sock *sk, int level, int optname,
  874. char __user *optval, unsigned int optlen)
  875. {
  876. switch (level) {
  877. case SOL_RAW:
  878. break;
  879. case SOL_ICMPV6:
  880. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  881. return -EOPNOTSUPP;
  882. return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
  883. case SOL_IPV6:
  884. if (optname == IPV6_CHECKSUM ||
  885. optname == IPV6_HDRINCL)
  886. break;
  887. default:
  888. return ipv6_setsockopt(sk, level, optname, optval, optlen);
  889. }
  890. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  891. }
  892. #ifdef CONFIG_COMPAT
  893. static int compat_rawv6_setsockopt(struct sock *sk, int level, int optname,
  894. char __user *optval, unsigned int optlen)
  895. {
  896. switch (level) {
  897. case SOL_RAW:
  898. break;
  899. case SOL_ICMPV6:
  900. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  901. return -EOPNOTSUPP;
  902. return rawv6_seticmpfilter(sk, level, optname, optval, optlen);
  903. case SOL_IPV6:
  904. if (optname == IPV6_CHECKSUM ||
  905. optname == IPV6_HDRINCL)
  906. break;
  907. default:
  908. return compat_ipv6_setsockopt(sk, level, optname,
  909. optval, optlen);
  910. }
  911. return do_rawv6_setsockopt(sk, level, optname, optval, optlen);
  912. }
  913. #endif
  914. static int do_rawv6_getsockopt(struct sock *sk, int level, int optname,
  915. char __user *optval, int __user *optlen)
  916. {
  917. struct raw6_sock *rp = raw6_sk(sk);
  918. int val, len;
  919. if (get_user(len, optlen))
  920. return -EFAULT;
  921. switch (optname) {
  922. case IPV6_HDRINCL:
  923. val = inet_sk(sk)->hdrincl;
  924. break;
  925. case IPV6_CHECKSUM:
  926. /*
  927. * We allow getsockopt() for IPPROTO_IPV6-level
  928. * IPV6_CHECKSUM socket option on ICMPv6 sockets
  929. * since RFC3542 is silent about it.
  930. */
  931. if (rp->checksum == 0)
  932. val = -1;
  933. else
  934. val = rp->offset;
  935. break;
  936. default:
  937. return -ENOPROTOOPT;
  938. }
  939. len = min_t(unsigned int, sizeof(int), len);
  940. if (put_user(len, optlen))
  941. return -EFAULT;
  942. if (copy_to_user(optval, &val, len))
  943. return -EFAULT;
  944. return 0;
  945. }
  946. static int rawv6_getsockopt(struct sock *sk, int level, int optname,
  947. char __user *optval, int __user *optlen)
  948. {
  949. switch (level) {
  950. case SOL_RAW:
  951. break;
  952. case SOL_ICMPV6:
  953. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  954. return -EOPNOTSUPP;
  955. return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
  956. case SOL_IPV6:
  957. if (optname == IPV6_CHECKSUM ||
  958. optname == IPV6_HDRINCL)
  959. break;
  960. default:
  961. return ipv6_getsockopt(sk, level, optname, optval, optlen);
  962. }
  963. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  964. }
  965. #ifdef CONFIG_COMPAT
  966. static int compat_rawv6_getsockopt(struct sock *sk, int level, int optname,
  967. char __user *optval, int __user *optlen)
  968. {
  969. switch (level) {
  970. case SOL_RAW:
  971. break;
  972. case SOL_ICMPV6:
  973. if (inet_sk(sk)->inet_num != IPPROTO_ICMPV6)
  974. return -EOPNOTSUPP;
  975. return rawv6_geticmpfilter(sk, level, optname, optval, optlen);
  976. case SOL_IPV6:
  977. if (optname == IPV6_CHECKSUM ||
  978. optname == IPV6_HDRINCL)
  979. break;
  980. default:
  981. return compat_ipv6_getsockopt(sk, level, optname,
  982. optval, optlen);
  983. }
  984. return do_rawv6_getsockopt(sk, level, optname, optval, optlen);
  985. }
  986. #endif
  987. static int rawv6_ioctl(struct sock *sk, int cmd, unsigned long arg)
  988. {
  989. switch (cmd) {
  990. case SIOCOUTQ: {
  991. int amount = sk_wmem_alloc_get(sk);
  992. return put_user(amount, (int __user *)arg);
  993. }
  994. case SIOCINQ: {
  995. struct sk_buff *skb;
  996. int amount = 0;
  997. spin_lock_bh(&sk->sk_receive_queue.lock);
  998. skb = skb_peek(&sk->sk_receive_queue);
  999. if (skb)
  1000. amount = skb->len;
  1001. spin_unlock_bh(&sk->sk_receive_queue.lock);
  1002. return put_user(amount, (int __user *)arg);
  1003. }
  1004. default:
  1005. #ifdef CONFIG_IPV6_MROUTE
  1006. return ip6mr_ioctl(sk, cmd, (void __user *)arg);
  1007. #else
  1008. return -ENOIOCTLCMD;
  1009. #endif
  1010. }
  1011. }
  1012. #ifdef CONFIG_COMPAT
  1013. static int compat_rawv6_ioctl(struct sock *sk, unsigned int cmd, unsigned long arg)
  1014. {
  1015. switch (cmd) {
  1016. case SIOCOUTQ:
  1017. case SIOCINQ:
  1018. return -ENOIOCTLCMD;
  1019. default:
  1020. #ifdef CONFIG_IPV6_MROUTE
  1021. return ip6mr_compat_ioctl(sk, cmd, compat_ptr(arg));
  1022. #else
  1023. return -ENOIOCTLCMD;
  1024. #endif
  1025. }
  1026. }
  1027. #endif
  1028. static void rawv6_close(struct sock *sk, long timeout)
  1029. {
  1030. if (inet_sk(sk)->inet_num == IPPROTO_RAW)
  1031. ip6_ra_control(sk, -1);
  1032. ip6mr_sk_done(sk);
  1033. sk_common_release(sk);
  1034. }
  1035. static void raw6_destroy(struct sock *sk)
  1036. {
  1037. lock_sock(sk);
  1038. ip6_flush_pending_frames(sk);
  1039. release_sock(sk);
  1040. inet6_destroy_sock(sk);
  1041. }
  1042. static int rawv6_init_sk(struct sock *sk)
  1043. {
  1044. struct raw6_sock *rp = raw6_sk(sk);
  1045. switch (inet_sk(sk)->inet_num) {
  1046. case IPPROTO_ICMPV6:
  1047. rp->checksum = 1;
  1048. rp->offset = 2;
  1049. break;
  1050. case IPPROTO_MH:
  1051. rp->checksum = 1;
  1052. rp->offset = 4;
  1053. break;
  1054. default:
  1055. break;
  1056. }
  1057. return 0;
  1058. }
  1059. struct proto rawv6_prot = {
  1060. .name = "RAWv6",
  1061. .owner = THIS_MODULE,
  1062. .close = rawv6_close,
  1063. .destroy = raw6_destroy,
  1064. .connect = ip6_datagram_connect_v6_only,
  1065. .disconnect = __udp_disconnect,
  1066. .ioctl = rawv6_ioctl,
  1067. .init = rawv6_init_sk,
  1068. .setsockopt = rawv6_setsockopt,
  1069. .getsockopt = rawv6_getsockopt,
  1070. .sendmsg = rawv6_sendmsg,
  1071. .recvmsg = rawv6_recvmsg,
  1072. .bind = rawv6_bind,
  1073. .backlog_rcv = rawv6_rcv_skb,
  1074. .hash = raw_hash_sk,
  1075. .unhash = raw_unhash_sk,
  1076. .obj_size = sizeof(struct raw6_sock),
  1077. .h.raw_hash = &raw_v6_hashinfo,
  1078. #ifdef CONFIG_COMPAT
  1079. .compat_setsockopt = compat_rawv6_setsockopt,
  1080. .compat_getsockopt = compat_rawv6_getsockopt,
  1081. .compat_ioctl = compat_rawv6_ioctl,
  1082. #endif
  1083. };
  1084. #ifdef CONFIG_PROC_FS
  1085. static int raw6_seq_show(struct seq_file *seq, void *v)
  1086. {
  1087. if (v == SEQ_START_TOKEN) {
  1088. seq_puts(seq, IPV6_SEQ_DGRAM_HEADER);
  1089. } else {
  1090. struct sock *sp = v;
  1091. __u16 srcp = inet_sk(sp)->inet_num;
  1092. ip6_dgram_sock_seq_show(seq, v, srcp, 0,
  1093. raw_seq_private(seq)->bucket);
  1094. }
  1095. return 0;
  1096. }
  1097. static const struct seq_operations raw6_seq_ops = {
  1098. .start = raw_seq_start,
  1099. .next = raw_seq_next,
  1100. .stop = raw_seq_stop,
  1101. .show = raw6_seq_show,
  1102. };
  1103. static int raw6_seq_open(struct inode *inode, struct file *file)
  1104. {
  1105. return raw_seq_open(inode, file, &raw_v6_hashinfo, &raw6_seq_ops);
  1106. }
  1107. static const struct file_operations raw6_seq_fops = {
  1108. .owner = THIS_MODULE,
  1109. .open = raw6_seq_open,
  1110. .read = seq_read,
  1111. .llseek = seq_lseek,
  1112. .release = seq_release_net,
  1113. };
  1114. static int __net_init raw6_init_net(struct net *net)
  1115. {
  1116. if (!proc_create("raw6", S_IRUGO, net->proc_net, &raw6_seq_fops))
  1117. return -ENOMEM;
  1118. return 0;
  1119. }
  1120. static void __net_exit raw6_exit_net(struct net *net)
  1121. {
  1122. remove_proc_entry("raw6", net->proc_net);
  1123. }
  1124. static struct pernet_operations raw6_net_ops = {
  1125. .init = raw6_init_net,
  1126. .exit = raw6_exit_net,
  1127. };
  1128. int __init raw6_proc_init(void)
  1129. {
  1130. return register_pernet_subsys(&raw6_net_ops);
  1131. }
  1132. void raw6_proc_exit(void)
  1133. {
  1134. unregister_pernet_subsys(&raw6_net_ops);
  1135. }
  1136. #endif /* CONFIG_PROC_FS */
  1137. /* Same as inet6_dgram_ops, sans udp_poll. */
  1138. const struct proto_ops inet6_sockraw_ops = {
  1139. .family = PF_INET6,
  1140. .owner = THIS_MODULE,
  1141. .release = inet6_release,
  1142. .bind = inet6_bind,
  1143. .connect = inet_dgram_connect, /* ok */
  1144. .socketpair = sock_no_socketpair, /* a do nothing */
  1145. .accept = sock_no_accept, /* a do nothing */
  1146. .getname = inet6_getname,
  1147. .poll = datagram_poll, /* ok */
  1148. .ioctl = inet6_ioctl, /* must change */
  1149. .listen = sock_no_listen, /* ok */
  1150. .shutdown = inet_shutdown, /* ok */
  1151. .setsockopt = sock_common_setsockopt, /* ok */
  1152. .getsockopt = sock_common_getsockopt, /* ok */
  1153. .sendmsg = inet_sendmsg, /* ok */
  1154. .recvmsg = sock_common_recvmsg, /* ok */
  1155. .mmap = sock_no_mmap,
  1156. .sendpage = sock_no_sendpage,
  1157. #ifdef CONFIG_COMPAT
  1158. .compat_setsockopt = compat_sock_common_setsockopt,
  1159. .compat_getsockopt = compat_sock_common_getsockopt,
  1160. #endif
  1161. };
  1162. static struct inet_protosw rawv6_protosw = {
  1163. .type = SOCK_RAW,
  1164. .protocol = IPPROTO_IP, /* wild card */
  1165. .prot = &rawv6_prot,
  1166. .ops = &inet6_sockraw_ops,
  1167. .flags = INET_PROTOSW_REUSE,
  1168. };
  1169. int __init rawv6_init(void)
  1170. {
  1171. return inet6_register_protosw(&rawv6_protosw);
  1172. }
  1173. void rawv6_exit(void)
  1174. {
  1175. inet6_unregister_protosw(&rawv6_protosw);
  1176. }