1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * Linux ethernet bridge
10 * Lennert dedicates this file to Kerstin Wurdinger.
13 #include <linux/module.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
17 #include <linux/netdevice.h>
18 #include <linux/skbuff.h>
19 #include <linux/if_arp.h>
20 #include <linux/if_ether.h>
21 #include <linux/if_vlan.h>
22 #include <linux/if_pppox.h>
23 #include <linux/ppp_defs.h>
24 #include <linux/netfilter_bridge.h>
25 #include <linux/netfilter_ipv4.h>
26 #include <linux/netfilter_ipv6.h>
27 #include <linux/netfilter_arp.h>
28 #include <linux/in_route.h>
29 #include <linux/inetdevice.h>
33 #include <net/addrconf.h>
34 #include <net/route.h>
35 #include <net/netfilter/br_netfilter.h>
37 #include <linux/uaccess.h>
38 #include "br_private.h"
40 #include <linux/sysctl.h>
43 /* We only check the length. A bridge shouldn't do any hop-by-hop stuff
46 static int br_nf_check_hbh_len(struct sk_buff *skb)
48 unsigned char *raw = (u8 *)(ipv6_hdr(skb) + 1);
50 const unsigned char *nh = skb_network_header(skb);
52 int len = (raw[1] + 1) << 3;
54 if ((raw + len) - skb->data > skb_headlen(skb))
61 int optlen = nh[off + 1] + 2;
72 if (nh[off + 1] != 4 || (off & 3) != 2)
74 pkt_len = ntohl(*(__be32 *)(nh + off + 2));
75 if (pkt_len <= IPV6_MAXPLEN ||
76 ipv6_hdr(skb)->payload_len)
78 if (pkt_len > skb->len - sizeof(struct ipv6hdr))
80 if (pskb_trim_rcsum(skb,
81 pkt_len + sizeof(struct ipv6hdr)))
83 nh = skb_network_header(skb);
99 int br_validate_ipv6(struct net *net, struct sk_buff *skb)
101 const struct ipv6hdr *hdr;
102 struct inet6_dev *idev = __in6_dev_get(skb->dev);
104 u8 ip6h_len = sizeof(struct ipv6hdr);
106 if (!pskb_may_pull(skb, ip6h_len))
109 if (skb->len < ip6h_len)
114 if (hdr->version != 6)
117 pkt_len = ntohs(hdr->payload_len);
119 if (pkt_len || hdr->nexthdr != NEXTHDR_HOP) {
120 if (pkt_len + ip6h_len > skb->len) {
121 __IP6_INC_STATS(net, idev,
122 IPSTATS_MIB_INTRUNCATEDPKTS);
125 if (pskb_trim_rcsum(skb, pkt_len + ip6h_len)) {
126 __IP6_INC_STATS(net, idev,
127 IPSTATS_MIB_INDISCARDS);
132 if (hdr->nexthdr == NEXTHDR_HOP && br_nf_check_hbh_len(skb))
135 memset(IP6CB(skb), 0, sizeof(struct inet6_skb_parm));
136 /* No IP options in IPv6 header; however it should be
137 * checked if some next headers need special treatment
142 __IP6_INC_STATS(net, idev, IPSTATS_MIB_INHDRERRORS);
148 br_nf_ipv6_daddr_was_changed(const struct sk_buff *skb,
149 const struct nf_bridge_info *nf_bridge)
151 return memcmp(&nf_bridge->ipv6_daddr, &ipv6_hdr(skb)->daddr,
152 sizeof(ipv6_hdr(skb)->daddr)) != 0;
155 /* PF_BRIDGE/PRE_ROUTING: Undo the changes made for ip6tables
156 * PREROUTING and continue the bridge PRE_ROUTING hook. See comment
157 * for br_nf_pre_routing_finish(), same logic is used here but
158 * equivalent IPv6 function ip6_route_input() called indirectly.
160 static int br_nf_pre_routing_finish_ipv6(struct net *net, struct sock *sk, struct sk_buff *skb)
162 struct nf_bridge_info *nf_bridge = nf_bridge_info_get(skb);
164 struct net_device *dev = skb->dev;
165 const struct nf_ipv6_ops *v6ops = nf_get_ipv6_ops();
167 nf_bridge->frag_max_size = IP6CB(skb)->frag_max_size;
169 if (nf_bridge->pkt_otherhost) {
170 skb->pkt_type = PACKET_OTHERHOST;
171 nf_bridge->pkt_otherhost = false;
173 nf_bridge->in_prerouting = 0;
174 if (br_nf_ipv6_daddr_was_changed(skb, nf_bridge)) {
176 v6ops->route_input(skb);
178 if (skb_dst(skb)->error) {
183 if (skb_dst(skb)->dev == dev) {
184 skb->dev = nf_bridge->physindev;
185 nf_bridge_update_protocol(skb);
186 nf_bridge_push_encap_header(skb);
187 br_nf_hook_thresh(NF_BR_PRE_ROUTING,
188 net, sk, skb, skb->dev, NULL,
189 br_nf_pre_routing_finish_bridge);
192 ether_addr_copy(eth_hdr(skb)->h_dest, dev->dev_addr);
193 skb->pkt_type = PACKET_HOST;
195 rt = bridge_parent_rtable(nf_bridge->physindev);
200 skb_dst_set_noref(skb, &rt->dst);
203 skb->dev = nf_bridge->physindev;
204 nf_bridge_update_protocol(skb);
205 nf_bridge_push_encap_header(skb);
206 br_nf_hook_thresh(NF_BR_PRE_ROUTING, net, sk, skb,
207 skb->dev, NULL, br_handle_frame_finish);
212 /* Replicate the checks that IPv6 does on packet reception and pass the packet
215 unsigned int br_nf_pre_routing_ipv6(void *priv,
217 const struct nf_hook_state *state)
219 struct nf_bridge_info *nf_bridge;
221 if (br_validate_ipv6(state->net, skb))
224 nf_bridge = nf_bridge_alloc(skb);
227 if (!setup_pre_routing(skb, state->net))
230 nf_bridge = nf_bridge_info_get(skb);
231 nf_bridge->ipv6_daddr = ipv6_hdr(skb)->daddr;
233 skb->protocol = htons(ETH_P_IPV6);
234 skb->transport_header = skb->network_header + sizeof(struct ipv6hdr);
236 NF_HOOK(NFPROTO_IPV6, NF_INET_PRE_ROUTING, state->net, state->sk, skb,
238 br_nf_pre_routing_finish_ipv6);