aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/fpga/altera-hps2fpga.c
blob: 77b95f2518216863bdba6c80c38e329c51912cc5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
// SPDX-License-Identifier: GPL-2.0
/*
 * FPGA to/from HPS Bridge Driver for Altera SoCFPGA Devices
 *
 *  Copyright (C) 2013-2016 Altera Corporation, All Rights Reserved.
 *
 * Includes this patch from the mailing list:
 *   fpga: altera-hps2fpga: fix HPS2FPGA bridge visibility to L3 masters
 *   Signed-off-by: Anatolij Gustschin <agust@denx.de>
 */

/*
 * This driver manages bridges on a Altera SOCFPGA between the ARM host
 * processor system (HPS) and the embedded FPGA.
 *
 * This driver supports enabling and disabling of the configured ports, which
 * allows for safe reprogramming of the FPGA, assuming that the new FPGA image
 * uses the same port configuration.  Bridges must be disabled before
 * reprogramming the FPGA and re-enabled after the FPGA has been programmed.
 */

#include <linux/clk.h>
#include <linux/fpga/fpga-bridge.h>
#include <linux/kernel.h>
#include <linux/mfd/syscon.h>
#include <linux/module.h>
#include <linux/of_platform.h>
#include <linux/regmap.h>
#include <linux/reset.h>
#include <linux/spinlock.h>

#define ALT_L3_REMAP_OFST			0x0
#define ALT_L3_REMAP_MPUZERO_MSK		0x00000001
#define ALT_L3_REMAP_H2F_MSK			0x00000008
#define ALT_L3_REMAP_LWH2F_MSK			0x00000010

#define HPS2FPGA_BRIDGE_NAME			"hps2fpga"
#define LWHPS2FPGA_BRIDGE_NAME			"lwhps2fpga"
#define FPGA2HPS_BRIDGE_NAME			"fpga2hps"

struct altera_hps2fpga_data {
	const char *name;
	struct reset_control *bridge_reset;
	struct regmap *l3reg;
	unsigned int remap_mask;
	struct clk *clk;
};

static int alt_hps2fpga_enable_show(struct fpga_bridge *bridge)
{
	struct altera_hps2fpga_data *priv = bridge->priv;

	return reset_control_status(priv->bridge_reset);
}

/* The L3 REMAP register is write only, so keep a cached value. */
static unsigned int l3_remap_shadow;
static DEFINE_SPINLOCK(l3_remap_lock);

static int _alt_hps2fpga_enable_set(struct altera_hps2fpga_data *priv,
				    bool enable)
{
	unsigned long flags;
	int ret;

	/* bring bridge out of reset */
	if (enable)
		ret = reset_control_deassert(priv->bridge_reset);
	else
		ret = reset_control_assert(priv->bridge_reset);
	if (ret)
		return ret;

	/* Allow bridge to be visible to L3 masters or not */
	if (priv->remap_mask) {
		spin_lock_irqsave(&l3_remap_lock, flags);
		l3_remap_shadow |= ALT_L3_REMAP_MPUZERO_MSK;

		if (enable)
			l3_remap_shadow |= priv->remap_mask;
		else
			l3_remap_shadow &= ~priv->remap_mask;

		ret = regmap_write(priv->l3reg, ALT_L3_REMAP_OFST,
				   l3_remap_shadow);
		spin_unlock_irqrestore(&l3_remap_lock, flags);
	}

	return ret;
}

static int alt_hps2fpga_enable_set(struct fpga_bridge *bridge, bool enable)
{
	return _alt_hps2fpga_enable_set(bridge->priv, enable);
}

static const struct fpga_bridge_ops altera_hps2fpga_br_ops = {
	.enable_set = alt_hps2fpga_enable_set,
	.enable_show = alt_hps2fpga_enable_show,
};

static struct altera_hps2fpga_data hps2fpga_data  = {
	.name = HPS2FPGA_BRIDGE_NAME,
	.remap_mask = ALT_L3_REMAP_H2F_MSK,
};

static struct altera_hps2fpga_data lwhps2fpga_data  = {
	.name = LWHPS2FPGA_BRIDGE_NAME,
	.remap_mask = ALT_L3_REMAP_LWH2F_MSK,
};

static struct altera_hps2fpga_data fpga2hps_data  = {
	.name = FPGA2HPS_BRIDGE_NAME,
};

static const struct of_device_id altera_fpga_of_match[] = {
	{ .compatible = "altr,socfpga-hps2fpga-bridge",
	  .data = &hps2fpga_data },
	{ .compatible = "altr,socfpga-lwhps2fpga-bridge",
	  .data = &lwhps2fpga_data },
	{ .compatible = "altr,socfpga-fpga2hps-bridge",
	  .data = &fpga2hps_data },
	{},
};

static int alt_fpga_bridge_probe(struct platform_device *pdev)
{
	struct device *dev = &pdev->dev;
	struct altera_hps2fpga_data *priv;
	const struct of_device_id *of_id;
	struct fpga_bridge *br;
	u32 enable;
	int ret;

	of_id = of_match_device(altera_fpga_of_match, dev);
	if (!of_id) {
		dev_err(dev, "failed to match device\n");
		return -ENODEV;
	}

	priv = (struct altera_hps2fpga_data *)of_id->data;

	priv->bridge_reset = of_reset_control_get_exclusive_by_index(dev->of_node,
								     0);
	if (IS_ERR(priv->bridge_reset)) {
		dev_err(dev, "Could not get %s reset control\n", priv->name);
		return PTR_ERR(priv->bridge_reset);
	}

	if (priv->remap_mask) {
		priv->l3reg = syscon_regmap_lookup_by_compatible("altr,l3regs");
		if (IS_ERR(priv->l3reg)) {
			dev_err(dev, "regmap for altr,l3regs lookup failed\n");
			return PTR_ERR(priv->l3reg);
		}
	}

	priv->clk = devm_clk_get(dev, NULL);
	if (IS_ERR(priv->clk)) {
		dev_err(dev, "no clock specified\n");
		return PTR_ERR(priv->clk);
	}

	ret = clk_prepare_enable(priv->clk);
	if (ret) {
		dev_err(dev, "could not enable clock\n");
		return -EBUSY;
	}

	if (!of_property_read_u32(dev->of_node, "bridge-enable", &enable)) {
		if (enable > 1) {
			dev_warn(dev, "invalid bridge-enable %u > 1\n", enable);
		} else {
			dev_info(dev, "%s bridge\n",
				 (enable ? "enabling" : "disabling"));

			ret = _alt_hps2fpga_enable_set(priv, enable);
			if (ret)
				goto err;
		}
	}

	br = devm_fpga_bridge_create(dev, priv->name,
				     &altera_hps2fpga_br_ops, priv);
	if (!br) {
		ret = -ENOMEM;
		goto err;
	}

	platform_set_drvdata(pdev, br);

	ret = fpga_bridge_register(br);
	if (ret)
		goto err;

	return 0;

err:
	clk_disable_unprepare(priv->clk);

	return ret;
}

static int alt_fpga_bridge_remove(struct platform_device *pdev)
{
	struct fpga_bridge *bridge = platform_get_drvdata(pdev);
	struct altera_hps2fpga_data *priv = bridge->priv;

	fpga_bridge_unregister(bridge);

	clk_disable_unprepare(priv->clk);

	return 0;
}

MODULE_DEVICE_TABLE(of, altera_fpga_of_match);

static struct platform_driver alt_fpga_bridge_driver = {
	.probe = alt_fpga_bridge_probe,
	.remove = alt_fpga_bridge_remove,
	.driver = {
		.name	= "altera_hps2fpga_bridge",
		.of_match_table = of_match_ptr(altera_fpga_of_match),
	},
};

module_platform_driver(alt_fpga_bridge_driver);

MODULE_DESCRIPTION("Altera SoCFPGA HPS to FPGA Bridge");
MODULE_AUTHOR("Alan Tull <atull@opensource.altera.com>");
MODULE_LICENSE("GPL v2");
ds <torvalds@ppc970.osdl.org> 2005-04-16 18:20:36 -0400 Linux-2.6.12-rc2' href='/cgit/cgit.cgi/litmus-rt-budgetable-locks.git/.git/commit/net/ipv6/ip6_output.c?h=update_litmus_2019&id=1da177e4c3f41524e886b7f1b8a0c1fc7321cac2'>1da177e4c3f4
1da177e4c3f4
b5d439982343
1da177e4c3f4

4c9483b2fb5d
b903d324bee2
1da177e4c3f4
3bd653c8455b
b30bd282cbf5
4c9483b2fb5d
adf30907d638
1da177e4c3f4
4c9483b2fb5d
1da177e4c3f4
e651f03afe83
1da177e4c3f4


c2636b4d9e8a
1da177e4c3f4









a11d206d0f88
adf30907d638
a11d206d0f88

1da177e4c3f4

a11d206d0f88

83d7eb2979cd
1da177e4c3f4






e2d1bca7e613

0660e03f6b18
1da177e4c3f4



b903d324bee2
1da177e4c3f4

6b75d0908185
1da177e4c3f4
4c9483b2fb5d
41a1f8ea4fbf
1da177e4c3f4



4e3fd7a06dc2

1da177e4c3f4
a2c2064f7f23
4a19ec5800fc
a2c2064f7f23
1da177e4c3f4
283d07ac201e
adf30907d638
edf391ff1723
b2e0b385d770

1da177e4c3f4




3ffe533c8728
adf30907d638
1da177e4c3f4



7159039a128f

1da177e4c3f4







9acd9f3ae92d
1da177e4c3f4



1da177e4c3f4



55f79cc0c02f

0660e03f6b18
1da177e4c3f4
ae08e1f09221
1da177e4c3f4




4e3fd7a06dc2

1da177e4c3f4











0bd1b59b15e4


1da177e4c3f4

















e21e0b5f19ac

0660e03f6b18
e21e0b5f19ac
75f2811c6460
e21e0b5f19ac


75f2811c6460
e21e0b5f19ac







d56f90a7c96d

e21e0b5f19ac

d56f90a7c96d
e21e0b5f19ac
















74553b09dcd9









e21e0b5f19ac


1da177e4c3f4






adf30907d638
0660e03f6b18
1da177e4c3f4
c346dca10840
14f3ad6f4a12
1ab1457c42bc
53b7997fd5c6
1da177e4c3f4

4497b0763cb1


1da177e4c3f4
3bd653c8455b
1da177e4c3f4


72b43d0898e9


35fc92a9deee
1da177e4c3f4














d56f90a7c96d
1da177e4c3f4









3ffe533c8728
483a47d2fe79

1da177e4c3f4




fbea49e1e240
53b7997fd5c6
8a3edd800dcd
74553b09dcd9

e21e0b5f19ac
74553b09dcd9
3bd653c8455b

74553b09dcd9

e21e0b5f19ac

1da177e4c3f4
3bd653c8455b
1da177e4c3f4

adf30907d638
1da177e4c3f4


1e5dc1461732
1da177e4c3f4
c45a3dfb59c0
1da177e4c3f4

1da177e4c3f4






c45a3dfb59c0

1da177e4c3f4


92d868292634


1da177e4c3f4


92d868292634
4991969a1027
5bb1ab09e4f6


1da177e4c3f4
f81b2e7d8cf8

5bb1ab09e4f6


3ffe533c8728
5bb1ab09e4f6

1da177e4c3f4

14f3ad6f4a12



0aa68271510a
1da177e4c3f4

14f3ad6f4a12
483a47d2fe79



1da177e4c3f4




3bd653c8455b
1da177e4c3f4


0660e03f6b18
1da177e4c3f4

1ab1457c42bc
1da177e4c3f4

483a47d2fe79
b2e0b385d770
6e23ae2a4875
1da177e4c3f4

483a47d2fe79
1da177e4c3f4









adf30907d638

1da177e4c3f4
82e91ffef60e
1da177e4c3f4



e7ac05f3407a
ba9dda3ab5a8



984bc16cc92e
1da177e4c3f4




0660e03f6b18

27a884dc3cb6
1da177e4c3f4
0660e03f6b18
1da177e4c3f4





27637df92e25
1da177e4c3f4
27637df92e25

1da177e4c3f4
59fbb3a61e02
27637df92e25




1da177e4c3f4



27637df92e25


d56f90a7c96d

1da177e4c3f4




87c48fa3b463




e688a6048076
87c48fa3b463


















ad0081e43af6
1da177e4c3f4
1da177e4c3f4
adf30907d638
d91675f9c7f5
1da177e4c3f4


a7ae1992248e
ae08e1f09221
1da177e4c3f4

adf30907d638
1da177e4c3f4
1da177e4c3f4


628a5c561890
b881ef760323

14f3ad6f4a12
b881ef760323
f2228f785a9d
adf30907d638
3ffe533c8728
adf30907d638
3bd653c8455b
b881ef760323



d91675f9c7f5




1da177e4c3f4
21dc33015745
1da177e4c3f4
3d13008e7345
1da177e4c3f4





4d9092bb41c8
1da177e4c3f4



3d13008e7345
1da177e4c3f4
1da177e4c3f4

3d13008e7345
2fdba6b085eb


2fdba6b085eb

2fdba6b085eb
3d13008e7345
1da177e4c3f4




4d9092bb41c8
1da177e4c3f4

9a217a1c7e7f
d56f90a7c96d
1da177e4c3f4
adf30907d638
3bd653c8455b
1da177e4c3f4


1da177e4c3f4

e2d1bca7e613

d56f90a7c96d
1da177e4c3f4
87c48fa3b463
1da177e4c3f4







0660e03f6b18

a11d206d0f88
d8d1f30b95a6
1da177e4c3f4





badff6d01a85
1da177e4c3f4
e2d1bca7e613

d56f90a7c96d

1da177e4c3f4






0660e03f6b18


1da177e4c3f4

1ab1457c42bc
1da177e4c3f4
dafee490858f
d8d1f30b95a6
3bd653c8455b
dafee490858f
1da177e4c3f4







a51482bde22f
1da177e4c3f4

d8d1f30b95a6
3bd653c8455b
d8d1f30b95a6
1da177e4c3f4








d8d1f30b95a6
3bd653c8455b
d8d1f30b95a6
1da177e4c3f4
3d13008e7345








1da177e4c3f4










a7ae1992248e

1da177e4c3f4








25985edcedea
1da177e4c3f4







a7ae1992248e

64ce207306de
adf30907d638
a11d206d0f88
1da177e4c3f4








a7ae1992248e
1da177e4c3f4
c1d2bbe1cd6c
badff6d01a85
b0e380b1d8a8

1da177e4c3f4










d626f62b11e0
1da177e4c3f4





f36d6ab182a5
87c48fa3b463
1da177e4c3f4






8984e41d18a5
1da177e4c3f4





0660e03f6b18

1da177e4c3f4






1da177e4c3f4


dafee490858f
adf30907d638
3bd653c8455b
1da177e4c3f4
adf30907d638
a11d206d0f88
1da177e4c3f4
1da177e4c3f4


adf30907d638
a11d206d0f88
1ab1457c42bc
1da177e4c3f4


b71d1d426d26


cf6b1982599c
a02cec2155fb

cf6b1982599c

497c615abad7

b71d1d426d26
1da177e4c3f4
497c615abad7

1da177e4c3f4
497c615abad7







cf6b1982599c

497c615abad7









4c9483b2fb5d
8e1ef0a95b87
4c9483b2fb5d
8e1ef0a95b87
4c9483b2fb5d
497c615abad7

1da177e4c3f4

497c615abad7




4c9483b2fb5d
497c615abad7
3b1e0a655f8e
69cce1d14049



497c615abad7
1da177e4c3f4
4c9483b2fb5d
1da177e4c3f4



4c9483b2fb5d
c3968a857a6b



44456d37b59d
1da177e4c3f4
1da177e4c3f4

95c385b4d5a7
e550dfb0c2c3







f2c31e32b378
2721745501a2
69cce1d14049
e550dfb0c2c3
4c9483b2fb5d
e550dfb0c2c3

f2c31e32b378
4c9483b2fb5d
e550dfb0c2c3











4c9483b2fb5d


e550dfb0c2c3

95c385b4d5a7
f2c31e32b378

e550dfb0c2c3
95c385b4d5a7

1da177e4c3f4


ca46f9c83491
483a47d2fe79
1da177e4c3f4



34a0b3cdc078
497c615abad7



4c9483b2fb5d
497c615abad7




4c9483b2fb5d
497c615abad7

4c9483b2fb5d
497c615abad7
3cf3dc6c2e05

497c615abad7
68d0c6d34d58

4c9483b2fb5d
68d0c6d34d58
a1414715f0ac
68d0c6d34d58





4c9483b2fb5d
68d0c6d34d58
a1414715f0ac
68d0c6d34d58



4c9483b2fb5d
68d0c6d34d58


4e3fd7a06dc2
2774c131b1d1
4c9483b2fb5d
2774c131b1d1
4c9483b2fb5d
68d0c6d34d58




497c615abad7
4c9483b2fb5d
68d0c6d34d58
a1414715f0ac
497c615abad7





68d0c6d34d58

497c615abad7
4c9483b2fb5d
68d0c6d34d58
a1414715f0ac
497c615abad7
68d0c6d34d58

497c615abad7
4c9483b2fb5d
68d0c6d34d58
4c9483b2fb5d
68d0c6d34d58


4e3fd7a06dc2
2774c131b1d1
4c9483b2fb5d
2774c131b1d1
4c9483b2fb5d
497c615abad7
68d0c6d34d58
497c615abad7
34a0b3cdc078
e89e9cf539a2


87c48fa3b463

e89e9cf539a2













504744e4edc2
e89e9cf539a2







c1d2bbe1cd6c
e89e9cf539a2

b0e380b1d8a8
e89e9cf539a2
84fa7933a33f
e89e9cf539a2
e89e9cf539a2






c31d5326902c




f83ef8c0b58d
87c48fa3b463
e89e9cf539a2











1da177e4c3f4
0178b695fd6b











41a1f8ea4fbf


4c9483b2fb5d
13b52cd44670
1da177e4c3f4


bdc712b4c2ba
1da177e4c3f4


299b0767642a
1da177e4c3f4





a693e69897e7
1da177e4c3f4


bdc712b4c2ba
1da177e4c3f4




0178b695fd6b
1da177e4c3f4
0178b695fd6b




























1da177e4c3f4

d8d1f30b95a6
bdc712b4c2ba
4c9483b2fb5d
1da177e4c3f4
41a1f8ea4fbf
628a5c561890
299b0767642a
c750360938b4
d91675f9c7f5


bdc712b4c2ba
d8d1f30b95a6
bdc712b4c2ba

1da177e4c3f4

299b0767642a
1da177e4c3f4

299b0767642a
1da177e4c3f4
bdc712b4c2ba
4c9483b2fb5d
0178b695fd6b
1da177e4c3f4

299b0767642a
bdc712b4c2ba
1da177e4c3f4

d8d1f30b95a6
1da177e4c3f4
a1b051405bc1
b4ce92775c2e
1da177e4c3f4


bdc712b4c2ba
4c9483b2fb5d
1da177e4c3f4



a693e69897e7






1da177e4c3f4






1ab1457c42bc
1da177e4c3f4

1ab1457c42bc
1da177e4c3f4

1ab1457c42bc
1da177e4c3f4

bdc712b4c2ba
4b340ae20d0e


4c9483b2fb5d
4b340ae20d0e

e89e9cf539a2
4b340ae20d0e
d8d1f30b95a6
4b340ae20d0e


87c48fa3b463
4b340ae20d0e



e89e9cf539a2
1da177e4c3f4





bdc712b4c2ba
1da177e4c3f4























bdc712b4c2ba
1da177e4c3f4



d8d1f30b95a6
1da177e4c3f4



299b0767642a

1da177e4c3f4





d8d1f30b95a6
1da177e4c3f4


1ab1457c42bc
1da177e4c3f4
















a693e69897e7





1da177e4c3f4







1f85851e17b6


1da177e4c3f4
a693e69897e7


1da177e4c3f4


1f85851e17b6


b0e380b1d8a8

1da177e4c3f4






e9fa4f7bd291
1da177e4c3f4

299b0767642a
1da177e4c3f4













299b0767642a
1da177e4c3f4











d8d1f30b95a6
1da177e4c3f4


















408dadf03fe3
1da177e4c3f4



1da177e4c3f4
408dadf03fe3
1da177e4c3f4














1da177e4c3f4



9e903e085262

408dadf03fe3
1da177e4c3f4



9e903e085262
1da177e4c3f4

f945fa7ad9c1

1da177e4c3f4





bdc712b4c2ba
3bd653c8455b
1da177e4c3f4


bf138862b162

0178b695fd6b








bdc712b4c2ba



bf138862b162



1da177e4c3f4






3bd653c8455b
1da177e4c3f4

bdc712b4c2ba
4c9483b2fb5d

1da177e4c3f4






d56f90a7c96d
bbe735e4247d
1da177e4c3f4
cfe1fc7759fd
1da177e4c3f4



1da177e4c3f4
1da177e4c3f4

1da177e4c3f4

28a89453b1e8
b5c15fc004ac
28a89453b1e8

4e3fd7a06dc2
cfe1fc7759fd
1da177e4c3f4




e2d1bca7e613

0660e03f6b18
1ab1457c42bc
4c9483b2fb5d
41a1f8ea4fbf
1da177e4c3f4
1da177e4c3f4

4e3fd7a06dc2

1da177e4c3f4
a2c2064f7f23
4a19ec5800fc
a2c2064f7f23
d8d1f30b95a6
edf391ff1723
14878f75abd5
adf30907d638
14878f75abd5
5a57d4c7fdac
e41b5368e029
14878f75abd5

ef76bc23ef2a
1da177e4c3f4

6ce9e7b5fe31
1da177e4c3f4




bf138862b162
1da177e4c3f4

0625491493d9
1da177e4c3f4




1da177e4c3f4


adf30907d638

e1f52208bb96
1da177e4c3f4


bf138862b162
1da177e4c3f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654

                             
                                  

                
                                                   
  




















                                                                         
                        
                         







                            
                         
                       















                                 
                          
 
                                                                       
 








                                                

                                                                  













                                      


                                                        
                                     
                                                       

                                                 
                                  
 
                            


                 
                                                  
 
                                             
                                          
                                



                                          
                                                            
                                                                    
 
                                                                          
                                                          
                                                                

                                                                    





                                                                            

                                                                           

                                                               
                                                            

                                                                       




                                               

                                                                          

         
                        
                                             

                                                   
 



                                  



                                                                     

 








                                                                    

                                   
                                                   
                                                            
                                               
                                                 
                                                       



                               


                                                                               

 
  
                                                    

   
                                                                      
                                                    
 
                                       
                                             
                                                 
                                             
                            
                                      
                               
                        


                  
                                       









                                                                                    
                                           
                                                                              

                                                                       

                                                

                                       
                                                 






                                                                           

                                              
                            



                                       
               

                                       
                                               
 
                                                                             
 



                                          

                                
 
                                        
                                
 
                           
                                                                    
                                                                 
                                                         

                                                                          




                                                                         
                                                    
                                                                              



                         

                        







                                                                            
                                                                          



                                             



                                          

                                             
                            
 
                                          




                                       

                            











                                                          


                                                                 

















                                                                                  

                                                       
                                            
                                  
                        


                                    
                                                                                  







                                                

                                                                  

                                 
                                                                              
















                                                                              









                                                                         


                 






                                                         
                                             
                                            
                                                
                                            
                
 
                                                   

                           


                                 
                                                              
                                                                              


                          


                                         
                              














                                                                      
                                                            









                                                                 
                                                                             

                                                                             




                                  
                                       
                                               
                                                                    

                                                           
                                              
                                       

                                                              

                                  

         
                                        
                                                                              

                          
                           


                                                                          
                                                                     
           
                                                                            

                                               






                                                                  

                                                   


                                             


                                             


                                                             
                                                               
                                                         


                                                           
                                                      

                                                                          


                                                             
                                                             

                                   

         



                                   
                                                 

                                                                       
                                                            



                                                                                




                                                      
                                                                               


                          
                            

                                                                 
 

                         
                                                                               
                                                                              
                                           

      
                                                                           









                                                                       

                                                  
                            
                              



                                      
                          



                                                    
                                   




                                                          

                                                                           
                                                                  
                           
                                           





                                          
                              
                                     

                                       
                                  
                                                                 




                                                                          



                                      


                                              

                                                                          




                      




                                                                  
                                                  


















                                                                          
                                                                      
 
                             
                                                             
                                                                   


                                          
                         
                           

                                   
                                                     
 


                                                  
                                   

                                                                           
                                                            
           
                                               
                                             
                                                            
                                                              
                                                     



                                 




                                              
 
                                     
                                                 
                                      





                                               
                                           



                                                              
                                                     
 

                                                   
                                                     


                                         

                                                              
                         
                                                        




                                                  
                                        

                                  
                                            
                                                                             
                               
                                                                      
                                                             


                                       

                                                                                

                                              
                                                               
 
                                          







                                                             

                                                                           
 
                                   





                                                                
                                                                 
                                                                                                 

                                                               

                                                                         






                                                                                    


                                                                              

                                                             
 
                                          
                                
                                                                          
                                                                       
 







                                         
                               

                               
                                                                  
                                                           
                                              








                                         
                                                          
                                                     
                                      
                           








                                                         










                                                                 

                                               








                                                                         
                                                                            







                                                                         

                                                                            
                                                                                        
                                                                      
                                                             








                                             
                                         
                                                                    
                                               
                                                                          

                                                                       










                                                                       
                                                                               





                                              
                               
                                                  






                                                        
                                                                             





                                                      

                                                                            






                                                                 


                                   
 
                                                              
                                                       
         
                                                      
                                           
                       


                   
                                                      
                                             
                       


                   


                                                                 
 

                                                                                   

 

                                                                
                                                                   
 

                                                     
 







                                                         

                                                       









                                                            
                                                                        
                           
                                                                        
      
                                                                        

                                 

         




                                               
                                                                          
 
                                       



                                 
 
                         
                                                      



                                     
                                         



                                                                          
                        
                                             

         
                                 







                                                          
                        
                                          
                                               
                                         
                                     

                             
                                  
                                                       











                                                                  


                                                                          

                                                     
                 

                                  
         

      


                 
                                
                                                                     



                          
 



                                                     
                            




                                                                      
                                                                               

                    
                                                 
 

                                  
   

                                                                     
                            
                                                              
                                                 





                                                                       
                                                                          
                                                                       
                                                     



                                     
                                                 


                                    
                                        
                      
                                                          
 
                                                                           




                                                                           
                                                               
                            
                                                              
                                                 





                                                                           

                                                                       
   
                                                                             
                                                                          
                                                        
 

                                                                           
 
                                             
 
                                                 


                                    
                                        
                      
                                                          
 
                                                                           
 
                                          
 
                                                      


                                                                              

                                                                    













                                                                      
                                   







                                                         
                                              

                                                        
                                                                            
 
                                                  
                              






                                                              




                                                                           
                                                        
                                             











                                                                   
 











                                                                        


                                                                      
                                                                               
                                                              


                                             
                               


                                               
                          





                                     
                          


                            
                                




                                                   
                                                  
                                               




























                                                                                

                                                               
                                   
                                     
                                           
                                            
                                         
                                                           
                                                           
                                          


                                                    
                                     
                                              

                                                      

                                          
                                                                              

                                         
                                                   
                
                                                  
                                           
                                   

                                
                                  
                                     

         
                                                
 
                                                                        
                                                   


                                                                                            
                                                                                                    
                                                                           



                                         






                                                       






                                                                    
                                                          

                       
                                                           

                                                               
                     

           
                               


                                                                                
                                                                  

                                         
 
                                           
                                                            


                                                                            
                                                                               



                                           
         





                                                                           
                                                                                                              























                                                                     
                                                                                                                                  



                                                                     
                                                                



                                                                   

                                                  





                                                                           
                                                                


                                                                     
                                                                          
















                                                                                





                                                                    







                                                              


                                                                           
 


                                                                     


                                                                 


                                                               

                                                                      






                                                                         
                                                                       

                                                               
 













                                                                                                                   
                                          











                                                                   
                                                          


















                                                                              
                                                                  



                                                                 
                                                                                               
                                                             














                                                                          



                                                

                                                                                 
                                                                       



                                                  
                                                      

                                              

                                                             





                               
                               
                                                                            


                   

                                                                           








                                             



                                                         



                                                         






                                                                   
                                       

                                                  
                                                                     

                                                  






                                                               
                                                
                                                         
                                                                        
                                                                 



                                              
                                                   

                                           

         
                                        
                                            

                                  
                                
                                                     




                                                                   

                                              
                            
 
                                        
                                                                      
 

                                            

                                
 
                                        
                                
 
                                              
                                                                        
                                      
                                                                    
 
                                                                                
                                                                 

         
                                 

                            
                                                  




                                   
                                   

                   
                                                                   




                                              


                                                                         

                                                                               
                                                               


                               
                                                    
 
/*
 *	IPv6 output functions
 *	Linux INET6 implementation
 *
 *	Authors:
 *	Pedro Roque		<roque@di.fc.ul.pt>
 *
 *	Based on linux/net/ipv4/ip_output.c
 *
 *	This program is free software; you can redistribute it and/or
 *      modify it under the terms of the GNU General Public License
 *      as published by the Free Software Foundation; either version
 *      2 of the License, or (at your option) any later version.
 *
 *	Changes:
 *	A.N.Kuznetsov	:	airthmetics in fragmentation.
 *				extension headers are implemented.
 *				route changes now work.
 *				ip6_forward does not confuse sniffers.
 *				etc.
 *
 *      H. von Brand    :       Added missing #include <linux/string.h>
 *	Imran Patel	: 	frag id should be in NBO
 *      Kazunori MIYAZAWA @USAGI
 *			:       add ip6_append_data and related functions
 *				for datagram xmit
 */

#include <linux/errno.h>
#include <linux/kernel.h>
#include <linux/string.h>
#include <linux/socket.h>
#include <linux/net.h>
#include <linux/netdevice.h>
#include <linux/if_arp.h>
#include <linux/in6.h>
#include <linux/tcp.h>
#include <linux/route.h>
#include <linux/module.h>
#include <linux/slab.h>

#include <linux/netfilter.h>
#include <linux/netfilter_ipv6.h>

#include <net/sock.h>
#include <net/snmp.h>

#include <net/ipv6.h>
#include <net/ndisc.h>
#include <net/protocol.h>
#include <net/ip6_route.h>
#include <net/addrconf.h>
#include <net/rawv6.h>
#include <net/icmp.h>
#include <net/xfrm.h>
#include <net/checksum.h>
#include <linux/mroute6.h>

int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *));

int __ip6_local_out(struct sk_buff *skb)
{
	int len;

	len = skb->len - sizeof(struct ipv6hdr);
	if (len > IPV6_MAXPLEN)
		len = 0;
	ipv6_hdr(skb)->payload_len = htons(len);

	return nf_hook(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
		       skb_dst(skb)->dev, dst_output);
}

int ip6_local_out(struct sk_buff *skb)
{
	int err;

	err = __ip6_local_out(skb);
	if (likely(err == 1))
		err = dst_output(skb);

	return err;
}
EXPORT_SYMBOL_GPL(ip6_local_out);

/* dev_loopback_xmit for use with netfilter. */
static int ip6_dev_loopback_xmit(struct sk_buff *newskb)
{
	skb_reset_mac_header(newskb);
	__skb_pull(newskb, skb_network_offset(newskb));
	newskb->pkt_type = PACKET_LOOPBACK;
	newskb->ip_summed = CHECKSUM_UNNECESSARY;
	WARN_ON(!skb_dst(newskb));

	netif_rx_ni(newskb);
	return 0;
}

static int ip6_finish_output2(struct sk_buff *skb)
{
	struct dst_entry *dst = skb_dst(skb);
	struct net_device *dev = dst->dev;
	struct neighbour *neigh;

	skb->protocol = htons(ETH_P_IPV6);
	skb->dev = dev;

	if (ipv6_addr_is_multicast(&ipv6_hdr(skb)->daddr)) {
		struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));

		if (!(dev->flags & IFF_LOOPBACK) && sk_mc_loop(skb->sk) &&
		    ((mroute6_socket(dev_net(dev), skb) &&
		     !(IP6CB(skb)->flags & IP6SKB_FORWARDED)) ||
		     ipv6_chk_mcast_addr(dev, &ipv6_hdr(skb)->daddr,
					 &ipv6_hdr(skb)->saddr))) {
			struct sk_buff *newskb = skb_clone(skb, GFP_ATOMIC);

			/* Do not check for IFF_ALLMULTI; multicast routing
			   is not supported in any case.
			 */
			if (newskb)
				NF_HOOK(NFPROTO_IPV6, NF_INET_POST_ROUTING,
					newskb, NULL, newskb->dev,
					ip6_dev_loopback_xmit);

			if (ipv6_hdr(skb)->hop_limit == 0) {
				IP6_INC_STATS(dev_net(dev), idev,
					      IPSTATS_MIB_OUTDISCARDS);
				kfree_skb(skb);
				return 0;
			}
		}

		IP6_UPD_PO_STATS(dev_net(dev), idev, IPSTATS_MIB_OUTMCAST,
				skb->len);
	}

	rcu_read_lock();
	neigh = dst_get_neighbour_noref(dst);
	if (neigh) {
		int res = neigh_output(neigh, skb);

		rcu_read_unlock();
		return res;
	}
	rcu_read_unlock();
	IP6_INC_STATS_BH(dev_net(dst->dev),
			 ip6_dst_idev(dst), IPSTATS_MIB_OUTNOROUTES);
	kfree_skb(skb);
	return -EINVAL;
}

static int ip6_finish_output(struct sk_buff *skb)
{
	if ((skb->len > ip6_skb_dst_mtu(skb) && !skb_is_gso(skb)) ||
	    dst_allfrag(skb_dst(skb)))
		return ip6_fragment(skb, ip6_finish_output2);
	else
		return ip6_finish_output2(skb);
}

int ip6_output(struct sk_buff *skb)
{
	struct net_device *dev = skb_dst(skb)->dev;
	struct inet6_dev *idev = ip6_dst_idev(skb_dst(skb));
	if (unlikely(idev->cnf.disable_ipv6)) {
		IP6_INC_STATS(dev_net(dev), idev,
			      IPSTATS_MIB_OUTDISCARDS);
		kfree_skb(skb);
		return 0;
	}

	return NF_HOOK_COND(NFPROTO_IPV6, NF_INET_POST_ROUTING, skb, NULL, dev,
			    ip6_finish_output,
			    !(IP6CB(skb)->flags & IP6SKB_REROUTED));
}

/*
 *	xmit an sk_buff (used by TCP, SCTP and DCCP)
 */

int ip6_xmit(struct sock *sk, struct sk_buff *skb, struct flowi6 *fl6,
	     struct ipv6_txoptions *opt, int tclass)
{
	struct net *net = sock_net(sk);
	struct ipv6_pinfo *np = inet6_sk(sk);
	struct in6_addr *first_hop = &fl6->daddr;
	struct dst_entry *dst = skb_dst(skb);
	struct ipv6hdr *hdr;
	u8  proto = fl6->flowi6_proto;
	int seg_len = skb->len;
	int hlimit = -1;
	u32 mtu;

	if (opt) {
		unsigned int head_room;

		/* First: exthdrs may take lots of space (~8K for now)
		   MAX_HEADER is not enough.
		 */
		head_room = opt->opt_nflen + opt->opt_flen;
		seg_len += head_room;
		head_room += sizeof(struct ipv6hdr) + LL_RESERVED_SPACE(dst->dev);

		if (skb_headroom(skb) < head_room) {
			struct sk_buff *skb2 = skb_realloc_headroom(skb, head_room);
			if (skb2 == NULL) {
				IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
					      IPSTATS_MIB_OUTDISCARDS);
				kfree_skb(skb);
				return -ENOBUFS;
			}
			kfree_skb(skb);
			skb = skb2;
			skb_set_owner_w(skb, sk);
		}
		if (opt->opt_flen)
			ipv6_push_frag_opts(skb, opt, &proto);
		if (opt->opt_nflen)
			ipv6_push_nfrag_opts(skb, opt, &proto, &first_hop);
	}

	skb_push(skb, sizeof(struct ipv6hdr));
	skb_reset_network_header(skb);
	hdr = ipv6_hdr(skb);

	/*
	 *	Fill in the IPv6 header
	 */
	if (np)
		hlimit = np->hop_limit;
	if (hlimit < 0)
		hlimit = ip6_dst_hoplimit(dst);

	*(__be32 *)hdr = htonl(0x60000000 | (tclass << 20)) | fl6->flowlabel;

	hdr->payload_len = htons(seg_len);
	hdr->nexthdr = proto;
	hdr->hop_limit = hlimit;

	hdr->saddr = fl6->saddr;
	hdr->daddr = *first_hop;

	skb->priority = sk->sk_priority;
	skb->mark = sk->sk_mark;

	mtu = dst_mtu(dst);
	if ((skb->len <= mtu) || skb->local_df || skb_is_gso(skb)) {
		IP6_UPD_PO_STATS(net, ip6_dst_idev(skb_dst(skb)),
			      IPSTATS_MIB_OUT, skb->len);
		return NF_HOOK(NFPROTO_IPV6, NF_INET_LOCAL_OUT, skb, NULL,
			       dst->dev, dst_output);
	}

	if (net_ratelimit())
		printk(KERN_DEBUG "IPv6: sending pkt_too_big to self\n");
	skb->dev = dst->dev;
	icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
	IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)), IPSTATS_MIB_FRAGFAILS);
	kfree_skb(skb);
	return -EMSGSIZE;
}

EXPORT_SYMBOL(ip6_xmit);

/*
 *	To avoid extra problems ND packets are send through this
 *	routine. It's code duplication but I really want to avoid
 *	extra checks since ipv6_build_header is used by TCP (which
 *	is for us performance critical)
 */

int ip6_nd_hdr(struct sock *sk, struct sk_buff *skb, struct net_device *dev,
	       const struct in6_addr *saddr, const struct in6_addr *daddr,
	       int proto, int len)
{
	struct ipv6_pinfo *np = inet6_sk(sk);
	struct ipv6hdr *hdr;

	skb->protocol = htons(ETH_P_IPV6);
	skb->dev = dev;

	skb_reset_network_header(skb);
	skb_put(skb, sizeof(struct ipv6hdr));
	hdr = ipv6_hdr(skb);

	*(__be32*)hdr = htonl(0x60000000);

	hdr->payload_len = htons(len);
	hdr->nexthdr = proto;
	hdr->hop_limit = np->hop_limit;

	hdr->saddr = *saddr;
	hdr->daddr = *daddr;

	return 0;
}

static int ip6_call_ra_chain(struct sk_buff *skb, int sel)
{
	struct ip6_ra_chain *ra;
	struct sock *last = NULL;

	read_lock(&ip6_ra_lock);
	for (ra = ip6_ra_chain; ra; ra = ra->next) {
		struct sock *sk = ra->sk;
		if (sk && ra->sel == sel &&
		    (!sk->sk_bound_dev_if ||
		     sk->sk_bound_dev_if == skb->dev->ifindex)) {
			if (last) {
				struct sk_buff *skb2 = skb_clone(skb, GFP_ATOMIC);
				if (skb2)
					rawv6_rcv(last, skb2);
			}
			last = sk;
		}
	}

	if (last) {
		rawv6_rcv(last, skb);
		read_unlock(&ip6_ra_lock);
		return 1;
	}
	read_unlock(&ip6_ra_lock);
	return 0;
}

static int ip6_forward_proxy_check(struct sk_buff *skb)
{
	struct ipv6hdr *hdr = ipv6_hdr(skb);
	u8 nexthdr = hdr->nexthdr;
	__be16 frag_off;
	int offset;

	if (ipv6_ext_hdr(nexthdr)) {
		offset = ipv6_skip_exthdr(skb, sizeof(*hdr), &nexthdr, &frag_off);
		if (offset < 0)
			return 0;
	} else
		offset = sizeof(struct ipv6hdr);

	if (nexthdr == IPPROTO_ICMPV6) {
		struct icmp6hdr *icmp6;

		if (!pskb_may_pull(skb, (skb_network_header(skb) +
					 offset + 1 - skb->data)))
			return 0;

		icmp6 = (struct icmp6hdr *)(skb_network_header(skb) + offset);

		switch (icmp6->icmp6_type) {
		case NDISC_ROUTER_SOLICITATION:
		case NDISC_ROUTER_ADVERTISEMENT:
		case NDISC_NEIGHBOUR_SOLICITATION:
		case NDISC_NEIGHBOUR_ADVERTISEMENT:
		case NDISC_REDIRECT:
			/* For reaction involving unicast neighbor discovery
			 * message destined to the proxied address, pass it to
			 * input function.
			 */
			return 1;
		default:
			break;
		}
	}

	/*
	 * The proxying router can't forward traffic sent to a link-local
	 * address, so signal the sender and discard the packet. This
	 * behavior is clarified by the MIPv6 specification.
	 */
	if (ipv6_addr_type(&hdr->daddr) & IPV6_ADDR_LINKLOCAL) {
		dst_link_failure(skb);
		return -1;
	}

	return 0;
}

static inline int ip6_forward_finish(struct sk_buff *skb)
{
	return dst_output(skb);
}

int ip6_forward(struct sk_buff *skb)
{
	struct dst_entry *dst = skb_dst(skb);
	struct ipv6hdr *hdr = ipv6_hdr(skb);
	struct inet6_skb_parm *opt = IP6CB(skb);
	struct net *net = dev_net(dst->dev);
	u32 mtu;

	if (net->ipv6.devconf_all->forwarding == 0)
		goto error;

	if (skb_warn_if_lro(skb))
		goto drop;

	if (!xfrm6_policy_check(NULL, XFRM_POLICY_FWD, skb)) {
		IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
		goto drop;
	}

	if (skb->pkt_type != PACKET_HOST)
		goto drop;

	skb_forward_csum(skb);

	/*
	 *	We DO NOT make any processing on
	 *	RA packets, pushing them to user level AS IS
	 *	without ane WARRANTY that application will be able
	 *	to interpret them. The reason is that we
	 *	cannot make anything clever here.
	 *
	 *	We are not end-node, so that if packet contains
	 *	AH/ESP, we cannot make anything.
	 *	Defragmentation also would be mistake, RA packets
	 *	cannot be fragmented, because there is no warranty
	 *	that different fragments will go along one path. --ANK
	 */
	if (opt->ra) {
		u8 *ptr = skb_network_header(skb) + opt->ra;
		if (ip6_call_ra_chain(skb, (ptr[2]<<8) + ptr[3]))
			return 0;
	}

	/*
	 *	check and decrement ttl
	 */
	if (hdr->hop_limit <= 1) {
		/* Force OUTPUT device used as source address */
		skb->dev = dst->dev;
		icmpv6_send(skb, ICMPV6_TIME_EXCEED, ICMPV6_EXC_HOPLIMIT, 0);
		IP6_INC_STATS_BH(net,
				 ip6_dst_idev(dst), IPSTATS_MIB_INHDRERRORS);

		kfree_skb(skb);
		return -ETIMEDOUT;
	}

	/* XXX: idev->cnf.proxy_ndp? */
	if (net->ipv6.devconf_all->proxy_ndp &&
	    pneigh_lookup(&nd_tbl, net, &hdr->daddr, skb->dev, 0)) {
		int proxied = ip6_forward_proxy_check(skb);
		if (proxied > 0)
			return ip6_input(skb);
		else if (proxied < 0) {
			IP6_INC_STATS(net, ip6_dst_idev(dst),
				      IPSTATS_MIB_INDISCARDS);
			goto drop;
		}
	}

	if (!xfrm6_route_forward(skb)) {
		IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_INDISCARDS);
		goto drop;
	}
	dst = skb_dst(skb);

	/* IPv6 specs say nothing about it, but it is clear that we cannot
	   send redirects to source routed frames.
	   We don't send redirects to frames decapsulated from IPsec.
	 */
	if (skb->dev == dst->dev && opt->srcrt == 0 && !skb_sec_path(skb)) {
		struct in6_addr *target = NULL;
		struct rt6_info *rt;

		/*
		 *	incoming and outgoing devices are the same
		 *	send a redirect.
		 */

		rt = (struct rt6_info *) dst;
		if (rt->rt6i_flags & RTF_GATEWAY)
			target = &rt->rt6i_gateway;
		else
			target = &hdr->daddr;

		if (!rt->rt6i_peer)
			rt6_bind_peer(rt, 1);

		/* Limit redirects both by destination (here)
		   and by source (inside ndisc_send_redirect)
		 */
		if (inet_peer_xrlim_allow(rt->rt6i_peer, 1*HZ))
			ndisc_send_redirect(skb, target);
	} else {
		int addrtype = ipv6_addr_type(&hdr->saddr);

		/* This check is security critical. */
		if (addrtype == IPV6_ADDR_ANY ||
		    addrtype & (IPV6_ADDR_MULTICAST | IPV6_ADDR_LOOPBACK))
			goto error;
		if (addrtype & IPV6_ADDR_LINKLOCAL) {
			icmpv6_send(skb, ICMPV6_DEST_UNREACH,
				    ICMPV6_NOT_NEIGHBOUR, 0);
			goto error;
		}
	}

	mtu = dst_mtu(dst);
	if (mtu < IPV6_MIN_MTU)
		mtu = IPV6_MIN_MTU;

	if (skb->len > mtu && !skb_is_gso(skb)) {
		/* Again, force OUTPUT device used as source address */
		skb->dev = dst->dev;
		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
		IP6_INC_STATS_BH(net,
				 ip6_dst_idev(dst), IPSTATS_MIB_INTOOBIGERRORS);
		IP6_INC_STATS_BH(net,
				 ip6_dst_idev(dst), IPSTATS_MIB_FRAGFAILS);
		kfree_skb(skb);
		return -EMSGSIZE;
	}

	if (skb_cow(skb, dst->dev->hard_header_len)) {
		IP6_INC_STATS(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTDISCARDS);
		goto drop;
	}

	hdr = ipv6_hdr(skb);

	/* Mangling hops number delayed to point after skb COW */

	hdr->hop_limit--;

	IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_OUTFORWDATAGRAMS);
	return NF_HOOK(NFPROTO_IPV6, NF_INET_FORWARD, skb, skb->dev, dst->dev,
		       ip6_forward_finish);

error:
	IP6_INC_STATS_BH(net, ip6_dst_idev(dst), IPSTATS_MIB_INADDRERRORS);
drop:
	kfree_skb(skb);
	return -EINVAL;
}

static void ip6_copy_metadata(struct sk_buff *to, struct sk_buff *from)
{
	to->pkt_type = from->pkt_type;
	to->priority = from->priority;
	to->protocol = from->protocol;
	skb_dst_drop(to);
	skb_dst_set(to, dst_clone(skb_dst(from)));
	to->dev = from->dev;
	to->mark = from->mark;

#ifdef CONFIG_NET_SCHED
	to->tc_index = from->tc_index;
#endif
	nf_copy(to, from);
#if defined(CONFIG_NETFILTER_XT_TARGET_TRACE) || \
    defined(CONFIG_NETFILTER_XT_TARGET_TRACE_MODULE)
	to->nf_trace = from->nf_trace;
#endif
	skb_copy_secmark(to, from);
}

int ip6_find_1stfragopt(struct sk_buff *skb, u8 **nexthdr)
{
	u16 offset = sizeof(struct ipv6hdr);
	struct ipv6_opt_hdr *exthdr =
				(struct ipv6_opt_hdr *)(ipv6_hdr(skb) + 1);
	unsigned int packet_len = skb->tail - skb->network_header;
	int found_rhdr = 0;
	*nexthdr = &ipv6_hdr(skb)->nexthdr;

	while (offset + 1 <= packet_len) {

		switch (**nexthdr) {

		case NEXTHDR_HOP:
			break;
		case NEXTHDR_ROUTING:
			found_rhdr = 1;
			break;
		case NEXTHDR_DEST:
#if defined(CONFIG_IPV6_MIP6) || defined(CONFIG_IPV6_MIP6_MODULE)
			if (ipv6_find_tlv(skb, offset, IPV6_TLV_HAO) >= 0)
				break;
#endif
			if (found_rhdr)
				return offset;
			break;
		default :
			return offset;
		}

		offset += ipv6_optlen(exthdr);
		*nexthdr = &exthdr->nexthdr;
		exthdr = (struct ipv6_opt_hdr *)(skb_network_header(skb) +
						 offset);
	}

	return offset;
}

void ipv6_select_ident(struct frag_hdr *fhdr, struct rt6_info *rt)
{
	static atomic_t ipv6_fragmentation_id;
	int old, new;

	if (rt && !(rt->dst.flags & DST_NOPEER)) {
		struct inet_peer *peer;

		if (!rt->rt6i_peer)
			rt6_bind_peer(rt, 1);
		peer = rt->rt6i_peer;
		if (peer) {
			fhdr->identification = htonl(inet_getid(peer, 0));
			return;
		}
	}
	do {
		old = atomic_read(&ipv6_fragmentation_id);
		new = old + 1;
		if (!new)
			new = 1;
	} while (atomic_cmpxchg(&ipv6_fragmentation_id, old, new) != old);
	fhdr->identification = htonl(new);
}

int ip6_fragment(struct sk_buff *skb, int (*output)(struct sk_buff *))
{
	struct sk_buff *frag;
	struct rt6_info *rt = (struct rt6_info*)skb_dst(skb);
	struct ipv6_pinfo *np = skb->sk ? inet6_sk(skb->sk) : NULL;
	struct ipv6hdr *tmp_hdr;
	struct frag_hdr *fh;
	unsigned int mtu, hlen, left, len;
	int hroom, troom;
	__be32 frag_id = 0;
	int ptr, offset = 0, err=0;
	u8 *prevhdr, nexthdr = 0;
	struct net *net = dev_net(skb_dst(skb)->dev);

	hlen = ip6_find_1stfragopt(skb, &prevhdr);
	nexthdr = *prevhdr;

	mtu = ip6_skb_dst_mtu(skb);

	/* We must not fragment if the socket is set to force MTU discovery
	 * or if the skb it not generated by a local socket.
	 */
	if (!skb->local_df && skb->len > mtu) {
		skb->dev = skb_dst(skb)->dev;
		icmpv6_send(skb, ICMPV6_PKT_TOOBIG, 0, mtu);
		IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
			      IPSTATS_MIB_FRAGFAILS);
		kfree_skb(skb);
		return -EMSGSIZE;
	}

	if (np && np->frag_size < mtu) {
		if (np->frag_size)
			mtu = np->frag_size;
	}
	mtu -= hlen + sizeof(struct frag_hdr);

	if (skb_has_frag_list(skb)) {
		int first_len = skb_pagelen(skb);
		struct sk_buff *frag2;

		if (first_len - hlen > mtu ||
		    ((first_len - hlen) & 7) ||
		    skb_cloned(skb))
			goto slow_path;

		skb_walk_frags(skb, frag) {
			/* Correct geometry. */
			if (frag->len > mtu ||
			    ((frag->len & 7) && frag->next) ||
			    skb_headroom(frag) < hlen)
				goto slow_path_clean;

			/* Partially cloned skb? */
			if (skb_shared(frag))
				goto slow_path_clean;

			BUG_ON(frag->sk);
			if (skb->sk) {
				frag->sk = skb->sk;
				frag->destructor = sock_wfree;
			}
			skb->truesize -= frag->truesize;
		}

		err = 0;
		offset = 0;
		frag = skb_shinfo(skb)->frag_list;
		skb_frag_list_init(skb);
		/* BUILD HEADER */

		*prevhdr = NEXTHDR_FRAGMENT;
		tmp_hdr = kmemdup(skb_network_header(skb), hlen, GFP_ATOMIC);
		if (!tmp_hdr) {
			IP6_INC_STATS(net, ip6_dst_idev(skb_dst(skb)),
				      IPSTATS_MIB_FRAGFAILS);
			return -ENOMEM;
		}

		__skb_pull(skb, hlen);
		fh = (struct frag_hdr*)__skb_push(skb, sizeof(struct frag_hdr));
		__skb_push(skb, hlen);
		skb_reset_network_header(skb);
		memcpy(skb_network_header(skb), tmp_hdr, hlen);

		ipv6_select_ident(fh, rt);
		fh->nexthdr = nexthdr;
		fh->reserved = 0;
		fh->frag_off = htons(IP6_MF);
		frag_id = fh->identification;

		first_len = skb_pagelen(skb);
		skb->data_len = first_len - skb_headlen(skb);
		skb->len = first_len;
		ipv6_hdr(skb)->payload_len = htons(first_len -
						   sizeof(struct ipv6hdr));

		dst_hold(&rt->dst);

		for (;;) {
			/* Prepare header of the next frame,
			 * before previous one went down. */
			if (frag) {
				frag->ip_summed = CHECKSUM_NONE;
				skb_reset_transport_header(frag);
				fh = (struct frag_hdr*)__skb_push(frag, sizeof(struct frag_hdr));
				__skb_push(frag, hlen);
				skb_reset_network_header(frag);
				memcpy(skb_network_header(frag), tmp_hdr,
				       hlen);
				offset += skb->len - hlen - sizeof(struct frag_hdr);
				fh->nexthdr = nexthdr;
				fh->reserved = 0;
				fh->frag_off = htons(offset);
				if (frag->next != NULL)
					fh->frag_off |= htons(IP6_MF);
				fh->identification = frag_id;
				ipv6_hdr(frag)->payload_len =
						htons(frag->len -
						      sizeof(struct ipv6hdr));
				ip6_copy_metadata(frag, skb);
			}

			err = output(skb);
			if(!err)
				IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),
					      IPSTATS_MIB_FRAGCREATES);

			if (err || !frag)
				break;

			skb = frag;
			frag = skb->next;
			skb->next = NULL;
		}

		kfree(tmp_hdr);

		if (err == 0) {
			IP6_INC_STATS(net, ip6_dst_idev(&rt->dst),