ipv6.c 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061
  1. /* SCTP kernel reference Implementation
  2. * (C) Copyright IBM Corp. 2002, 2004
  3. * Copyright (c) 2001 Nokia, Inc.
  4. * Copyright (c) 2001 La Monte H.P. Yarroll
  5. * Copyright (c) 2002-2003 Intel Corp.
  6. *
  7. * This file is part of the SCTP kernel reference Implementation
  8. *
  9. * SCTP over IPv6.
  10. *
  11. * The SCTP reference implementation is free software;
  12. * you can redistribute it and/or modify it under the terms of
  13. * the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2, or (at your option)
  15. * any later version.
  16. *
  17. * The SCTP reference implementation is distributed in the hope that it
  18. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  19. * ************************
  20. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  21. * See the GNU General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU General Public License
  24. * along with GNU CC; see the file COPYING. If not, write to
  25. * the Free Software Foundation, 59 Temple Place - Suite 330,
  26. * Boston, MA 02111-1307, USA.
  27. *
  28. * Please send any bug reports or fixes you make to the
  29. * email address(es):
  30. * lksctp developers <lksctp-developers@lists.sourceforge.net>
  31. *
  32. * Or submit a bug report through the following website:
  33. * http://www.sf.net/projects/lksctp
  34. *
  35. * Written or modified by:
  36. * Le Yanqun <yanqun.le@nokia.com>
  37. * Hui Huang <hui.huang@nokia.com>
  38. * La Monte H.P. Yarroll <piggy@acm.org>
  39. * Sridhar Samudrala <sri@us.ibm.com>
  40. * Jon Grimm <jgrimm@us.ibm.com>
  41. * Ardelle Fan <ardelle.fan@intel.com>
  42. *
  43. * Based on:
  44. * linux/net/ipv6/tcp_ipv6.c
  45. *
  46. * Any bugs reported given to us we will try to fix... any fixes shared will
  47. * be incorporated into the next SCTP release.
  48. */
  49. #include <linux/module.h>
  50. #include <linux/errno.h>
  51. #include <linux/types.h>
  52. #include <linux/socket.h>
  53. #include <linux/sockios.h>
  54. #include <linux/net.h>
  55. #include <linux/in.h>
  56. #include <linux/in6.h>
  57. #include <linux/netdevice.h>
  58. #include <linux/init.h>
  59. #include <linux/ipsec.h>
  60. #include <linux/ipv6.h>
  61. #include <linux/icmpv6.h>
  62. #include <linux/random.h>
  63. #include <linux/seq_file.h>
  64. #include <net/protocol.h>
  65. #include <net/ndisc.h>
  66. #include <net/ip.h>
  67. #include <net/ipv6.h>
  68. #include <net/transp_v6.h>
  69. #include <net/addrconf.h>
  70. #include <net/ip6_route.h>
  71. #include <net/inet_common.h>
  72. #include <net/inet_ecn.h>
  73. #include <net/sctp/sctp.h>
  74. #include <asm/uaccess.h>
  75. /* Event handler for inet6 address addition/deletion events.
  76. * The sctp_local_addr_list needs to be protocted by a spin lock since
  77. * multiple notifiers (say IPv4 and IPv6) may be running at the same
  78. * time and thus corrupt the list.
  79. * The reader side is protected with RCU.
  80. */
  81. static int sctp_inet6addr_event(struct notifier_block *this, unsigned long ev,
  82. void *ptr)
  83. {
  84. struct inet6_ifaddr *ifa = (struct inet6_ifaddr *)ptr;
  85. struct sctp_sockaddr_entry *addr = NULL;
  86. struct sctp_sockaddr_entry *temp;
  87. switch (ev) {
  88. case NETDEV_UP:
  89. addr = kmalloc(sizeof(struct sctp_sockaddr_entry), GFP_ATOMIC);
  90. if (addr) {
  91. addr->a.v6.sin6_family = AF_INET6;
  92. addr->a.v6.sin6_port = 0;
  93. memcpy(&addr->a.v6.sin6_addr, &ifa->addr,
  94. sizeof(struct in6_addr));
  95. addr->a.v6.sin6_scope_id = ifa->idev->dev->ifindex;
  96. addr->valid = 1;
  97. spin_lock_bh(&sctp_local_addr_lock);
  98. list_add_tail_rcu(&addr->list, &sctp_local_addr_list);
  99. spin_unlock_bh(&sctp_local_addr_lock);
  100. }
  101. break;
  102. case NETDEV_DOWN:
  103. spin_lock_bh(&sctp_local_addr_lock);
  104. list_for_each_entry_safe(addr, temp,
  105. &sctp_local_addr_list, list) {
  106. if (ipv6_addr_equal(&addr->a.v6.sin6_addr,
  107. &ifa->addr)) {
  108. addr->valid = 0;
  109. list_del_rcu(&addr->list);
  110. break;
  111. }
  112. }
  113. spin_unlock_bh(&sctp_local_addr_lock);
  114. if (addr && !addr->valid)
  115. call_rcu(&addr->rcu, sctp_local_addr_free);
  116. break;
  117. }
  118. return NOTIFY_DONE;
  119. }
  120. static struct notifier_block sctp_inet6addr_notifier = {
  121. .notifier_call = sctp_inet6addr_event,
  122. };
  123. /* ICMP error handler. */
  124. SCTP_STATIC void sctp_v6_err(struct sk_buff *skb, struct inet6_skb_parm *opt,
  125. int type, int code, int offset, __be32 info)
  126. {
  127. struct inet6_dev *idev;
  128. struct sock *sk;
  129. struct sctp_association *asoc;
  130. struct sctp_transport *transport;
  131. struct ipv6_pinfo *np;
  132. sk_buff_data_t saveip, savesctp;
  133. int err;
  134. idev = in6_dev_get(skb->dev);
  135. /* Fix up skb to look at the embedded net header. */
  136. saveip = skb->network_header;
  137. savesctp = skb->transport_header;
  138. skb_reset_network_header(skb);
  139. skb_set_transport_header(skb, offset);
  140. sk = sctp_err_lookup(AF_INET6, skb, sctp_hdr(skb), &asoc, &transport);
  141. /* Put back, the original pointers. */
  142. skb->network_header = saveip;
  143. skb->transport_header = savesctp;
  144. if (!sk) {
  145. ICMP6_INC_STATS_BH(idev, ICMP6_MIB_INERRORS);
  146. goto out;
  147. }
  148. /* Warning: The sock lock is held. Remember to call
  149. * sctp_err_finish!
  150. */
  151. switch (type) {
  152. case ICMPV6_PKT_TOOBIG:
  153. sctp_icmp_frag_needed(sk, asoc, transport, ntohl(info));
  154. goto out_unlock;
  155. case ICMPV6_PARAMPROB:
  156. if (ICMPV6_UNK_NEXTHDR == code) {
  157. sctp_icmp_proto_unreachable(sk, asoc, transport);
  158. goto out_unlock;
  159. }
  160. break;
  161. default:
  162. break;
  163. }
  164. np = inet6_sk(sk);
  165. icmpv6_err_convert(type, code, &err);
  166. if (!sock_owned_by_user(sk) && np->recverr) {
  167. sk->sk_err = err;
  168. sk->sk_error_report(sk);
  169. } else { /* Only an error on timeout */
  170. sk->sk_err_soft = err;
  171. }
  172. out_unlock:
  173. sctp_err_finish(sk, asoc);
  174. out:
  175. if (likely(idev != NULL))
  176. in6_dev_put(idev);
  177. }
  178. /* Based on tcp_v6_xmit() in tcp_ipv6.c. */
  179. static int sctp_v6_xmit(struct sk_buff *skb, struct sctp_transport *transport,
  180. int ipfragok)
  181. {
  182. struct sock *sk = skb->sk;
  183. struct ipv6_pinfo *np = inet6_sk(sk);
  184. struct flowi fl;
  185. memset(&fl, 0, sizeof(fl));
  186. fl.proto = sk->sk_protocol;
  187. /* Fill in the dest address from the route entry passed with the skb
  188. * and the source address from the transport.
  189. */
  190. ipv6_addr_copy(&fl.fl6_dst, &transport->ipaddr.v6.sin6_addr);
  191. ipv6_addr_copy(&fl.fl6_src, &transport->saddr.v6.sin6_addr);
  192. fl.fl6_flowlabel = np->flow_label;
  193. IP6_ECN_flow_xmit(sk, fl.fl6_flowlabel);
  194. if (ipv6_addr_type(&fl.fl6_src) & IPV6_ADDR_LINKLOCAL)
  195. fl.oif = transport->saddr.v6.sin6_scope_id;
  196. else
  197. fl.oif = sk->sk_bound_dev_if;
  198. if (np->opt && np->opt->srcrt) {
  199. struct rt0_hdr *rt0 = (struct rt0_hdr *) np->opt->srcrt;
  200. ipv6_addr_copy(&fl.fl6_dst, rt0->addr);
  201. }
  202. SCTP_DEBUG_PRINTK("%s: skb:%p, len:%d, "
  203. "src:" NIP6_FMT " dst:" NIP6_FMT "\n",
  204. __FUNCTION__, skb, skb->len,
  205. NIP6(fl.fl6_src), NIP6(fl.fl6_dst));
  206. SCTP_INC_STATS(SCTP_MIB_OUTSCTPPACKS);
  207. return ip6_xmit(sk, skb, &fl, np->opt, ipfragok);
  208. }
  209. /* Returns the dst cache entry for the given source and destination ip
  210. * addresses.
  211. */
  212. static struct dst_entry *sctp_v6_get_dst(struct sctp_association *asoc,
  213. union sctp_addr *daddr,
  214. union sctp_addr *saddr)
  215. {
  216. struct dst_entry *dst;
  217. struct flowi fl;
  218. memset(&fl, 0, sizeof(fl));
  219. ipv6_addr_copy(&fl.fl6_dst, &daddr->v6.sin6_addr);
  220. if (ipv6_addr_type(&daddr->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL)
  221. fl.oif = daddr->v6.sin6_scope_id;
  222. SCTP_DEBUG_PRINTK("%s: DST=" NIP6_FMT " ",
  223. __FUNCTION__, NIP6(fl.fl6_dst));
  224. if (saddr) {
  225. ipv6_addr_copy(&fl.fl6_src, &saddr->v6.sin6_addr);
  226. SCTP_DEBUG_PRINTK(
  227. "SRC=" NIP6_FMT " - ",
  228. NIP6(fl.fl6_src));
  229. }
  230. dst = ip6_route_output(NULL, &fl);
  231. if (!dst->error) {
  232. struct rt6_info *rt;
  233. rt = (struct rt6_info *)dst;
  234. SCTP_DEBUG_PRINTK(
  235. "rt6_dst:" NIP6_FMT " rt6_src:" NIP6_FMT "\n",
  236. NIP6(rt->rt6i_dst.addr), NIP6(rt->rt6i_src.addr));
  237. return dst;
  238. }
  239. SCTP_DEBUG_PRINTK("NO ROUTE\n");
  240. dst_release(dst);
  241. return NULL;
  242. }
  243. /* Returns the number of consecutive initial bits that match in the 2 ipv6
  244. * addresses.
  245. */
  246. static inline int sctp_v6_addr_match_len(union sctp_addr *s1,
  247. union sctp_addr *s2)
  248. {
  249. struct in6_addr *a1 = &s1->v6.sin6_addr;
  250. struct in6_addr *a2 = &s2->v6.sin6_addr;
  251. int i, j;
  252. for (i = 0; i < 4 ; i++) {
  253. __be32 a1xora2;
  254. a1xora2 = a1->s6_addr32[i] ^ a2->s6_addr32[i];
  255. if ((j = fls(ntohl(a1xora2))))
  256. return (i * 32 + 32 - j);
  257. }
  258. return (i*32);
  259. }
  260. /* Fills in the source address(saddr) based on the destination address(daddr)
  261. * and asoc's bind address list.
  262. */
  263. static void sctp_v6_get_saddr(struct sctp_association *asoc,
  264. struct dst_entry *dst,
  265. union sctp_addr *daddr,
  266. union sctp_addr *saddr)
  267. {
  268. struct sctp_bind_addr *bp;
  269. struct sctp_sockaddr_entry *laddr;
  270. sctp_scope_t scope;
  271. union sctp_addr *baddr = NULL;
  272. __u8 matchlen = 0;
  273. __u8 bmatchlen;
  274. SCTP_DEBUG_PRINTK("%s: asoc:%p dst:%p "
  275. "daddr:" NIP6_FMT " ",
  276. __FUNCTION__, asoc, dst, NIP6(daddr->v6.sin6_addr));
  277. if (!asoc) {
  278. ipv6_get_saddr(dst, &daddr->v6.sin6_addr,&saddr->v6.sin6_addr);
  279. SCTP_DEBUG_PRINTK("saddr from ipv6_get_saddr: " NIP6_FMT "\n",
  280. NIP6(saddr->v6.sin6_addr));
  281. return;
  282. }
  283. scope = sctp_scope(daddr);
  284. bp = &asoc->base.bind_addr;
  285. /* Go through the bind address list and find the best source address
  286. * that matches the scope of the destination address.
  287. */
  288. rcu_read_lock();
  289. list_for_each_entry_rcu(laddr, &bp->address_list, list) {
  290. if (!laddr->valid)
  291. continue;
  292. if ((laddr->use_as_src) &&
  293. (laddr->a.sa.sa_family == AF_INET6) &&
  294. (scope <= sctp_scope(&laddr->a))) {
  295. bmatchlen = sctp_v6_addr_match_len(daddr, &laddr->a);
  296. if (!baddr || (matchlen < bmatchlen)) {
  297. baddr = &laddr->a;
  298. matchlen = bmatchlen;
  299. }
  300. }
  301. }
  302. if (baddr) {
  303. memcpy(saddr, baddr, sizeof(union sctp_addr));
  304. SCTP_DEBUG_PRINTK("saddr: " NIP6_FMT "\n",
  305. NIP6(saddr->v6.sin6_addr));
  306. } else {
  307. printk(KERN_ERR "%s: asoc:%p Could not find a valid source "
  308. "address for the dest:" NIP6_FMT "\n",
  309. __FUNCTION__, asoc, NIP6(daddr->v6.sin6_addr));
  310. }
  311. rcu_read_unlock();
  312. }
  313. /* Make a copy of all potential local addresses. */
  314. static void sctp_v6_copy_addrlist(struct list_head *addrlist,
  315. struct net_device *dev)
  316. {
  317. struct inet6_dev *in6_dev;
  318. struct inet6_ifaddr *ifp;
  319. struct sctp_sockaddr_entry *addr;
  320. rcu_read_lock();
  321. if ((in6_dev = __in6_dev_get(dev)) == NULL) {
  322. rcu_read_unlock();
  323. return;
  324. }
  325. read_lock_bh(&in6_dev->lock);
  326. for (ifp = in6_dev->addr_list; ifp; ifp = ifp->if_next) {
  327. /* Add the address to the local list. */
  328. addr = t_new(struct sctp_sockaddr_entry, GFP_ATOMIC);
  329. if (addr) {
  330. addr->a.v6.sin6_family = AF_INET6;
  331. addr->a.v6.sin6_port = 0;
  332. addr->a.v6.sin6_addr = ifp->addr;
  333. addr->a.v6.sin6_scope_id = dev->ifindex;
  334. addr->valid = 1;
  335. INIT_LIST_HEAD(&addr->list);
  336. INIT_RCU_HEAD(&addr->rcu);
  337. list_add_tail(&addr->list, addrlist);
  338. }
  339. }
  340. read_unlock_bh(&in6_dev->lock);
  341. rcu_read_unlock();
  342. }
  343. /* Initialize a sockaddr_storage from in incoming skb. */
  344. static void sctp_v6_from_skb(union sctp_addr *addr,struct sk_buff *skb,
  345. int is_saddr)
  346. {
  347. void *from;
  348. __be16 *port;
  349. struct sctphdr *sh;
  350. port = &addr->v6.sin6_port;
  351. addr->v6.sin6_family = AF_INET6;
  352. addr->v6.sin6_flowinfo = 0; /* FIXME */
  353. addr->v6.sin6_scope_id = ((struct inet6_skb_parm *)skb->cb)->iif;
  354. sh = sctp_hdr(skb);
  355. if (is_saddr) {
  356. *port = sh->source;
  357. from = &ipv6_hdr(skb)->saddr;
  358. } else {
  359. *port = sh->dest;
  360. from = &ipv6_hdr(skb)->daddr;
  361. }
  362. ipv6_addr_copy(&addr->v6.sin6_addr, from);
  363. }
  364. /* Initialize an sctp_addr from a socket. */
  365. static void sctp_v6_from_sk(union sctp_addr *addr, struct sock *sk)
  366. {
  367. addr->v6.sin6_family = AF_INET6;
  368. addr->v6.sin6_port = 0;
  369. addr->v6.sin6_addr = inet6_sk(sk)->rcv_saddr;
  370. }
  371. /* Initialize sk->sk_rcv_saddr from sctp_addr. */
  372. static void sctp_v6_to_sk_saddr(union sctp_addr *addr, struct sock *sk)
  373. {
  374. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  375. inet6_sk(sk)->rcv_saddr.s6_addr32[0] = 0;
  376. inet6_sk(sk)->rcv_saddr.s6_addr32[1] = 0;
  377. inet6_sk(sk)->rcv_saddr.s6_addr32[2] = htonl(0x0000ffff);
  378. inet6_sk(sk)->rcv_saddr.s6_addr32[3] =
  379. addr->v4.sin_addr.s_addr;
  380. } else {
  381. inet6_sk(sk)->rcv_saddr = addr->v6.sin6_addr;
  382. }
  383. }
  384. /* Initialize sk->sk_daddr from sctp_addr. */
  385. static void sctp_v6_to_sk_daddr(union sctp_addr *addr, struct sock *sk)
  386. {
  387. if (addr->sa.sa_family == AF_INET && sctp_sk(sk)->v4mapped) {
  388. inet6_sk(sk)->daddr.s6_addr32[0] = 0;
  389. inet6_sk(sk)->daddr.s6_addr32[1] = 0;
  390. inet6_sk(sk)->daddr.s6_addr32[2] = htonl(0x0000ffff);
  391. inet6_sk(sk)->daddr.s6_addr32[3] = addr->v4.sin_addr.s_addr;
  392. } else {
  393. inet6_sk(sk)->daddr = addr->v6.sin6_addr;
  394. }
  395. }
  396. /* Initialize a sctp_addr from an address parameter. */
  397. static void sctp_v6_from_addr_param(union sctp_addr *addr,
  398. union sctp_addr_param *param,
  399. __be16 port, int iif)
  400. {
  401. addr->v6.sin6_family = AF_INET6;
  402. addr->v6.sin6_port = port;
  403. addr->v6.sin6_flowinfo = 0; /* BUG */
  404. ipv6_addr_copy(&addr->v6.sin6_addr, &param->v6.addr);
  405. addr->v6.sin6_scope_id = iif;
  406. }
  407. /* Initialize an address parameter from a sctp_addr and return the length
  408. * of the address parameter.
  409. */
  410. static int sctp_v6_to_addr_param(const union sctp_addr *addr,
  411. union sctp_addr_param *param)
  412. {
  413. int length = sizeof(sctp_ipv6addr_param_t);
  414. param->v6.param_hdr.type = SCTP_PARAM_IPV6_ADDRESS;
  415. param->v6.param_hdr.length = htons(length);
  416. ipv6_addr_copy(&param->v6.addr, &addr->v6.sin6_addr);
  417. return length;
  418. }
  419. /* Initialize a sctp_addr from a dst_entry. */
  420. static void sctp_v6_dst_saddr(union sctp_addr *addr, struct dst_entry *dst,
  421. __be16 port)
  422. {
  423. struct rt6_info *rt = (struct rt6_info *)dst;
  424. addr->sa.sa_family = AF_INET6;
  425. addr->v6.sin6_port = port;
  426. ipv6_addr_copy(&addr->v6.sin6_addr, &rt->rt6i_src.addr);
  427. }
  428. /* Compare addresses exactly.
  429. * v4-mapped-v6 is also in consideration.
  430. */
  431. static int sctp_v6_cmp_addr(const union sctp_addr *addr1,
  432. const union sctp_addr *addr2)
  433. {
  434. if (addr1->sa.sa_family != addr2->sa.sa_family) {
  435. if (addr1->sa.sa_family == AF_INET &&
  436. addr2->sa.sa_family == AF_INET6 &&
  437. ipv6_addr_v4mapped(&addr2->v6.sin6_addr)) {
  438. if (addr2->v6.sin6_port == addr1->v4.sin_port &&
  439. addr2->v6.sin6_addr.s6_addr32[3] ==
  440. addr1->v4.sin_addr.s_addr)
  441. return 1;
  442. }
  443. if (addr2->sa.sa_family == AF_INET &&
  444. addr1->sa.sa_family == AF_INET6 &&
  445. ipv6_addr_v4mapped(&addr1->v6.sin6_addr)) {
  446. if (addr1->v6.sin6_port == addr2->v4.sin_port &&
  447. addr1->v6.sin6_addr.s6_addr32[3] ==
  448. addr2->v4.sin_addr.s_addr)
  449. return 1;
  450. }
  451. return 0;
  452. }
  453. if (!ipv6_addr_equal(&addr1->v6.sin6_addr, &addr2->v6.sin6_addr))
  454. return 0;
  455. /* If this is a linklocal address, compare the scope_id. */
  456. if (ipv6_addr_type(&addr1->v6.sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  457. if (addr1->v6.sin6_scope_id && addr2->v6.sin6_scope_id &&
  458. (addr1->v6.sin6_scope_id != addr2->v6.sin6_scope_id)) {
  459. return 0;
  460. }
  461. }
  462. return 1;
  463. }
  464. /* Initialize addr struct to INADDR_ANY. */
  465. static void sctp_v6_inaddr_any(union sctp_addr *addr, __be16 port)
  466. {
  467. memset(addr, 0x00, sizeof(union sctp_addr));
  468. addr->v6.sin6_family = AF_INET6;
  469. addr->v6.sin6_port = port;
  470. }
  471. /* Is this a wildcard address? */
  472. static int sctp_v6_is_any(const union sctp_addr *addr)
  473. {
  474. return ipv6_addr_any(&addr->v6.sin6_addr);
  475. }
  476. /* Should this be available for binding? */
  477. static int sctp_v6_available(union sctp_addr *addr, struct sctp_sock *sp)
  478. {
  479. int type;
  480. struct in6_addr *in6 = (struct in6_addr *)&addr->v6.sin6_addr;
  481. type = ipv6_addr_type(in6);
  482. if (IPV6_ADDR_ANY == type)
  483. return 1;
  484. if (type == IPV6_ADDR_MAPPED) {
  485. if (sp && !sp->v4mapped)
  486. return 0;
  487. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  488. return 0;
  489. sctp_v6_map_v4(addr);
  490. return sctp_get_af_specific(AF_INET)->available(addr, sp);
  491. }
  492. if (!(type & IPV6_ADDR_UNICAST))
  493. return 0;
  494. return ipv6_chk_addr(in6, NULL, 0);
  495. }
  496. /* This function checks if the address is a valid address to be used for
  497. * SCTP.
  498. *
  499. * Output:
  500. * Return 0 - If the address is a non-unicast or an illegal address.
  501. * Return 1 - If the address is a unicast.
  502. */
  503. static int sctp_v6_addr_valid(union sctp_addr *addr,
  504. struct sctp_sock *sp,
  505. const struct sk_buff *skb)
  506. {
  507. int ret = ipv6_addr_type(&addr->v6.sin6_addr);
  508. /* Support v4-mapped-v6 address. */
  509. if (ret == IPV6_ADDR_MAPPED) {
  510. /* Note: This routine is used in input, so v4-mapped-v6
  511. * are disallowed here when there is no sctp_sock.
  512. */
  513. if (!sp || !sp->v4mapped)
  514. return 0;
  515. if (sp && ipv6_only_sock(sctp_opt2sk(sp)))
  516. return 0;
  517. sctp_v6_map_v4(addr);
  518. return sctp_get_af_specific(AF_INET)->addr_valid(addr, sp, skb);
  519. }
  520. /* Is this a non-unicast address */
  521. if (!(ret & IPV6_ADDR_UNICAST))
  522. return 0;
  523. return 1;
  524. }
  525. /* What is the scope of 'addr'? */
  526. static sctp_scope_t sctp_v6_scope(union sctp_addr *addr)
  527. {
  528. int v6scope;
  529. sctp_scope_t retval;
  530. /* The IPv6 scope is really a set of bit fields.
  531. * See IFA_* in <net/if_inet6.h>. Map to a generic SCTP scope.
  532. */
  533. v6scope = ipv6_addr_scope(&addr->v6.sin6_addr);
  534. switch (v6scope) {
  535. case IFA_HOST:
  536. retval = SCTP_SCOPE_LOOPBACK;
  537. break;
  538. case IFA_LINK:
  539. retval = SCTP_SCOPE_LINK;
  540. break;
  541. case IFA_SITE:
  542. retval = SCTP_SCOPE_PRIVATE;
  543. break;
  544. default:
  545. retval = SCTP_SCOPE_GLOBAL;
  546. break;
  547. }
  548. return retval;
  549. }
  550. /* Create and initialize a new sk for the socket to be returned by accept(). */
  551. static struct sock *sctp_v6_create_accept_sk(struct sock *sk,
  552. struct sctp_association *asoc)
  553. {
  554. struct inet_sock *inet = inet_sk(sk);
  555. struct sock *newsk;
  556. struct inet_sock *newinet;
  557. struct ipv6_pinfo *newnp, *np = inet6_sk(sk);
  558. struct sctp6_sock *newsctp6sk;
  559. newsk = sk_alloc(sk->sk_net, PF_INET6, GFP_KERNEL, sk->sk_prot, 1);
  560. if (!newsk)
  561. goto out;
  562. sock_init_data(NULL, newsk);
  563. newsk->sk_type = SOCK_STREAM;
  564. newsk->sk_prot = sk->sk_prot;
  565. newsk->sk_no_check = sk->sk_no_check;
  566. newsk->sk_reuse = sk->sk_reuse;
  567. newsk->sk_destruct = inet_sock_destruct;
  568. newsk->sk_family = PF_INET6;
  569. newsk->sk_protocol = IPPROTO_SCTP;
  570. newsk->sk_backlog_rcv = sk->sk_prot->backlog_rcv;
  571. newsk->sk_shutdown = sk->sk_shutdown;
  572. sock_reset_flag(sk, SOCK_ZAPPED);
  573. newsctp6sk = (struct sctp6_sock *)newsk;
  574. inet_sk(newsk)->pinet6 = &newsctp6sk->inet6;
  575. sctp_sk(newsk)->v4mapped = sctp_sk(sk)->v4mapped;
  576. newinet = inet_sk(newsk);
  577. newnp = inet6_sk(newsk);
  578. memcpy(newnp, np, sizeof(struct ipv6_pinfo));
  579. /* Initialize sk's sport, dport, rcv_saddr and daddr for getsockname()
  580. * and getpeername().
  581. */
  582. newinet->sport = inet->sport;
  583. newnp->saddr = np->saddr;
  584. newnp->rcv_saddr = np->rcv_saddr;
  585. newinet->dport = htons(asoc->peer.port);
  586. sctp_v6_to_sk_daddr(&asoc->peer.primary_addr, newsk);
  587. /* Init the ipv4 part of the socket since we can have sockets
  588. * using v6 API for ipv4.
  589. */
  590. newinet->uc_ttl = -1;
  591. newinet->mc_loop = 1;
  592. newinet->mc_ttl = 1;
  593. newinet->mc_index = 0;
  594. newinet->mc_list = NULL;
  595. if (ipv4_config.no_pmtu_disc)
  596. newinet->pmtudisc = IP_PMTUDISC_DONT;
  597. else
  598. newinet->pmtudisc = IP_PMTUDISC_WANT;
  599. sk_refcnt_debug_inc(newsk);
  600. if (newsk->sk_prot->init(newsk)) {
  601. sk_common_release(newsk);
  602. newsk = NULL;
  603. }
  604. out:
  605. return newsk;
  606. }
  607. /* Map v4 address to mapped v6 address */
  608. static void sctp_v6_addr_v4map(struct sctp_sock *sp, union sctp_addr *addr)
  609. {
  610. if (sp->v4mapped && AF_INET == addr->sa.sa_family)
  611. sctp_v4_map_v6(addr);
  612. }
  613. /* Where did this skb come from? */
  614. static int sctp_v6_skb_iif(const struct sk_buff *skb)
  615. {
  616. struct inet6_skb_parm *opt = (struct inet6_skb_parm *) skb->cb;
  617. return opt->iif;
  618. }
  619. /* Was this packet marked by Explicit Congestion Notification? */
  620. static int sctp_v6_is_ce(const struct sk_buff *skb)
  621. {
  622. return *((__u32 *)(ipv6_hdr(skb))) & htonl(1 << 20);
  623. }
  624. /* Dump the v6 addr to the seq file. */
  625. static void sctp_v6_seq_dump_addr(struct seq_file *seq, union sctp_addr *addr)
  626. {
  627. seq_printf(seq, NIP6_FMT " ", NIP6(addr->v6.sin6_addr));
  628. }
  629. /* Initialize a PF_INET6 socket msg_name. */
  630. static void sctp_inet6_msgname(char *msgname, int *addr_len)
  631. {
  632. struct sockaddr_in6 *sin6;
  633. sin6 = (struct sockaddr_in6 *)msgname;
  634. sin6->sin6_family = AF_INET6;
  635. sin6->sin6_flowinfo = 0;
  636. sin6->sin6_scope_id = 0; /*FIXME */
  637. *addr_len = sizeof(struct sockaddr_in6);
  638. }
  639. /* Initialize a PF_INET msgname from a ulpevent. */
  640. static void sctp_inet6_event_msgname(struct sctp_ulpevent *event,
  641. char *msgname, int *addrlen)
  642. {
  643. struct sockaddr_in6 *sin6, *sin6from;
  644. if (msgname) {
  645. union sctp_addr *addr;
  646. struct sctp_association *asoc;
  647. asoc = event->asoc;
  648. sctp_inet6_msgname(msgname, addrlen);
  649. sin6 = (struct sockaddr_in6 *)msgname;
  650. sin6->sin6_port = htons(asoc->peer.port);
  651. addr = &asoc->peer.primary_addr;
  652. /* Note: If we go to a common v6 format, this code
  653. * will change.
  654. */
  655. /* Map ipv4 address into v4-mapped-on-v6 address. */
  656. if (sctp_sk(asoc->base.sk)->v4mapped &&
  657. AF_INET == addr->sa.sa_family) {
  658. sctp_v4_map_v6((union sctp_addr *)sin6);
  659. sin6->sin6_addr.s6_addr32[3] =
  660. addr->v4.sin_addr.s_addr;
  661. return;
  662. }
  663. sin6from = &asoc->peer.primary_addr.v6;
  664. ipv6_addr_copy(&sin6->sin6_addr, &sin6from->sin6_addr);
  665. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL)
  666. sin6->sin6_scope_id = sin6from->sin6_scope_id;
  667. }
  668. }
  669. /* Initialize a msg_name from an inbound skb. */
  670. static void sctp_inet6_skb_msgname(struct sk_buff *skb, char *msgname,
  671. int *addr_len)
  672. {
  673. struct sctphdr *sh;
  674. struct sockaddr_in6 *sin6;
  675. if (msgname) {
  676. sctp_inet6_msgname(msgname, addr_len);
  677. sin6 = (struct sockaddr_in6 *)msgname;
  678. sh = sctp_hdr(skb);
  679. sin6->sin6_port = sh->source;
  680. /* Map ipv4 address into v4-mapped-on-v6 address. */
  681. if (sctp_sk(skb->sk)->v4mapped &&
  682. ip_hdr(skb)->version == 4) {
  683. sctp_v4_map_v6((union sctp_addr *)sin6);
  684. sin6->sin6_addr.s6_addr32[3] = ip_hdr(skb)->saddr;
  685. return;
  686. }
  687. /* Otherwise, just copy the v6 address. */
  688. ipv6_addr_copy(&sin6->sin6_addr, &ipv6_hdr(skb)->saddr);
  689. if (ipv6_addr_type(&sin6->sin6_addr) & IPV6_ADDR_LINKLOCAL) {
  690. struct sctp_ulpevent *ev = sctp_skb2event(skb);
  691. sin6->sin6_scope_id = ev->iif;
  692. }
  693. }
  694. }
  695. /* Do we support this AF? */
  696. static int sctp_inet6_af_supported(sa_family_t family, struct sctp_sock *sp)
  697. {
  698. switch (family) {
  699. case AF_INET6:
  700. return 1;
  701. /* v4-mapped-v6 addresses */
  702. case AF_INET:
  703. if (!__ipv6_only_sock(sctp_opt2sk(sp)) && sp->v4mapped)
  704. return 1;
  705. default:
  706. return 0;
  707. }
  708. }
  709. /* Address matching with wildcards allowed. This extra level
  710. * of indirection lets us choose whether a PF_INET6 should
  711. * disallow any v4 addresses if we so choose.
  712. */
  713. static int sctp_inet6_cmp_addr(const union sctp_addr *addr1,
  714. const union sctp_addr *addr2,
  715. struct sctp_sock *opt)
  716. {
  717. struct sctp_af *af1, *af2;
  718. af1 = sctp_get_af_specific(addr1->sa.sa_family);
  719. af2 = sctp_get_af_specific(addr2->sa.sa_family);
  720. if (!af1 || !af2)
  721. return 0;
  722. /* Today, wildcard AF_INET/AF_INET6. */
  723. if (sctp_is_any(addr1) || sctp_is_any(addr2))
  724. return 1;
  725. if (addr1->sa.sa_family != addr2->sa.sa_family)
  726. return 0;
  727. return af1->cmp_addr(addr1, addr2);
  728. }
  729. /* Verify that the provided sockaddr looks bindable. Common verification,
  730. * has already been taken care of.
  731. */
  732. static int sctp_inet6_bind_verify(struct sctp_sock *opt, union sctp_addr *addr)
  733. {
  734. struct sctp_af *af;
  735. /* ASSERT: address family has already been verified. */
  736. if (addr->sa.sa_family != AF_INET6)
  737. af = sctp_get_af_specific(addr->sa.sa_family);
  738. else {
  739. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  740. struct net_device *dev;
  741. if (type & IPV6_ADDR_LINKLOCAL) {
  742. if (!addr->v6.sin6_scope_id)
  743. return 0;
  744. dev = dev_get_by_index(&init_net, addr->v6.sin6_scope_id);
  745. if (!dev)
  746. return 0;
  747. if (!ipv6_chk_addr(&addr->v6.sin6_addr, dev, 0)) {
  748. dev_put(dev);
  749. return 0;
  750. }
  751. dev_put(dev);
  752. }
  753. af = opt->pf->af;
  754. }
  755. return af->available(addr, opt);
  756. }
  757. /* Verify that the provided sockaddr looks sendable. Common verification,
  758. * has already been taken care of.
  759. */
  760. static int sctp_inet6_send_verify(struct sctp_sock *opt, union sctp_addr *addr)
  761. {
  762. struct sctp_af *af = NULL;
  763. /* ASSERT: address family has already been verified. */
  764. if (addr->sa.sa_family != AF_INET6)
  765. af = sctp_get_af_specific(addr->sa.sa_family);
  766. else {
  767. int type = ipv6_addr_type(&addr->v6.sin6_addr);
  768. struct net_device *dev;
  769. if (type & IPV6_ADDR_LINKLOCAL) {
  770. if (!addr->v6.sin6_scope_id)
  771. return 0;
  772. dev = dev_get_by_index(&init_net, addr->v6.sin6_scope_id);
  773. if (!dev)
  774. return 0;
  775. dev_put(dev);
  776. }
  777. af = opt->pf->af;
  778. }
  779. return af != NULL;
  780. }
  781. /* Fill in Supported Address Type information for INIT and INIT-ACK
  782. * chunks. Note: In the future, we may want to look at sock options
  783. * to determine whether a PF_INET6 socket really wants to have IPV4
  784. * addresses.
  785. * Returns number of addresses supported.
  786. */
  787. static int sctp_inet6_supported_addrs(const struct sctp_sock *opt,
  788. __be16 *types)
  789. {
  790. types[0] = SCTP_PARAM_IPV4_ADDRESS;
  791. types[1] = SCTP_PARAM_IPV6_ADDRESS;
  792. return 2;
  793. }
  794. static const struct proto_ops inet6_seqpacket_ops = {
  795. .family = PF_INET6,
  796. .owner = THIS_MODULE,
  797. .release = inet6_release,
  798. .bind = inet6_bind,
  799. .connect = inet_dgram_connect,
  800. .socketpair = sock_no_socketpair,
  801. .accept = inet_accept,
  802. .getname = inet6_getname,
  803. .poll = sctp_poll,
  804. .ioctl = inet6_ioctl,
  805. .listen = sctp_inet_listen,
  806. .shutdown = inet_shutdown,
  807. .setsockopt = sock_common_setsockopt,
  808. .getsockopt = sock_common_getsockopt,
  809. .sendmsg = inet_sendmsg,
  810. .recvmsg = sock_common_recvmsg,
  811. .mmap = sock_no_mmap,
  812. #ifdef CONFIG_COMPAT
  813. .compat_setsockopt = compat_sock_common_setsockopt,
  814. .compat_getsockopt = compat_sock_common_getsockopt,
  815. #endif
  816. };
  817. static struct inet_protosw sctpv6_seqpacket_protosw = {
  818. .type = SOCK_SEQPACKET,
  819. .protocol = IPPROTO_SCTP,
  820. .prot = &sctpv6_prot,
  821. .ops = &inet6_seqpacket_ops,
  822. .capability = -1,
  823. .no_check = 0,
  824. .flags = SCTP_PROTOSW_FLAG
  825. };
  826. static struct inet_protosw sctpv6_stream_protosw = {
  827. .type = SOCK_STREAM,
  828. .protocol = IPPROTO_SCTP,
  829. .prot = &sctpv6_prot,
  830. .ops = &inet6_seqpacket_ops,
  831. .capability = -1,
  832. .no_check = 0,
  833. .flags = SCTP_PROTOSW_FLAG,
  834. };
  835. static int sctp6_rcv(struct sk_buff **pskb)
  836. {
  837. return sctp_rcv(*pskb) ? -1 : 0;
  838. }
  839. static struct inet6_protocol sctpv6_protocol = {
  840. .handler = sctp6_rcv,
  841. .err_handler = sctp_v6_err,
  842. .flags = INET6_PROTO_NOPOLICY | INET6_PROTO_FINAL,
  843. };
  844. static struct sctp_af sctp_ipv6_specific = {
  845. .sa_family = AF_INET6,
  846. .sctp_xmit = sctp_v6_xmit,
  847. .setsockopt = ipv6_setsockopt,
  848. .getsockopt = ipv6_getsockopt,
  849. .get_dst = sctp_v6_get_dst,
  850. .get_saddr = sctp_v6_get_saddr,
  851. .copy_addrlist = sctp_v6_copy_addrlist,
  852. .from_skb = sctp_v6_from_skb,
  853. .from_sk = sctp_v6_from_sk,
  854. .to_sk_saddr = sctp_v6_to_sk_saddr,
  855. .to_sk_daddr = sctp_v6_to_sk_daddr,
  856. .from_addr_param = sctp_v6_from_addr_param,
  857. .to_addr_param = sctp_v6_to_addr_param,
  858. .dst_saddr = sctp_v6_dst_saddr,
  859. .cmp_addr = sctp_v6_cmp_addr,
  860. .scope = sctp_v6_scope,
  861. .addr_valid = sctp_v6_addr_valid,
  862. .inaddr_any = sctp_v6_inaddr_any,
  863. .is_any = sctp_v6_is_any,
  864. .available = sctp_v6_available,
  865. .skb_iif = sctp_v6_skb_iif,
  866. .is_ce = sctp_v6_is_ce,
  867. .seq_dump_addr = sctp_v6_seq_dump_addr,
  868. .net_header_len = sizeof(struct ipv6hdr),
  869. .sockaddr_len = sizeof(struct sockaddr_in6),
  870. #ifdef CONFIG_COMPAT
  871. .compat_setsockopt = compat_ipv6_setsockopt,
  872. .compat_getsockopt = compat_ipv6_getsockopt,
  873. #endif
  874. };
  875. static struct sctp_pf sctp_pf_inet6_specific = {
  876. .event_msgname = sctp_inet6_event_msgname,
  877. .skb_msgname = sctp_inet6_skb_msgname,
  878. .af_supported = sctp_inet6_af_supported,
  879. .cmp_addr = sctp_inet6_cmp_addr,
  880. .bind_verify = sctp_inet6_bind_verify,
  881. .send_verify = sctp_inet6_send_verify,
  882. .supported_addrs = sctp_inet6_supported_addrs,
  883. .create_accept_sk = sctp_v6_create_accept_sk,
  884. .addr_v4map = sctp_v6_addr_v4map,
  885. .af = &sctp_ipv6_specific,
  886. };
  887. /* Initialize IPv6 support and register with socket layer. */
  888. int sctp_v6_init(void)
  889. {
  890. int rc;
  891. /* Register the SCTP specific PF_INET6 functions. */
  892. sctp_register_pf(&sctp_pf_inet6_specific, PF_INET6);
  893. /* Register the SCTP specific AF_INET6 functions. */
  894. sctp_register_af(&sctp_ipv6_specific);
  895. rc = proto_register(&sctpv6_prot, 1);
  896. if (rc)
  897. return rc;
  898. /* Add SCTPv6(UDP and TCP style) to inetsw6 linked list. */
  899. inet6_register_protosw(&sctpv6_seqpacket_protosw);
  900. inet6_register_protosw(&sctpv6_stream_protosw);
  901. return 0;
  902. }
  903. /* Register with inet6 layer. */
  904. int sctp_v6_add_protocol(void)
  905. {
  906. /* Register notifier for inet6 address additions/deletions. */
  907. register_inet6addr_notifier(&sctp_inet6addr_notifier);
  908. if (inet6_add_protocol(&sctpv6_protocol, IPPROTO_SCTP) < 0)
  909. return -EAGAIN;
  910. return 0;
  911. }
  912. /* IPv6 specific exit support. */
  913. void sctp_v6_exit(void)
  914. {
  915. inet6_unregister_protosw(&sctpv6_seqpacket_protosw);
  916. inet6_unregister_protosw(&sctpv6_stream_protosw);
  917. proto_unregister(&sctpv6_prot);
  918. list_del(&sctp_ipv6_specific.list);
  919. }
  920. /* Unregister with inet6 layer. */
  921. void sctp_v6_del_protocol(void)
  922. {
  923. inet6_del_protocol(&sctpv6_protocol, IPPROTO_SCTP);
  924. unregister_inet6addr_notifier(&sctp_inet6addr_notifier);
  925. }