ipv6.c 27 KB

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