raw.c 32 KB

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