Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 1 | /* This program is free software; you can redistribute it and/or modify |
| 2 | * it under the terms of the GNU General Public License version 2 |
| 3 | * as published by the Free Software Foundation. |
| 4 | * |
| 5 | * This program is distributed in the hope that it will be useful, |
| 6 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 7 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 8 | * GNU General Public License for more details. |
| 9 | */ |
| 10 | |
| 11 | #include <linux/if_arp.h> |
| 12 | |
| 13 | #include <net/6lowpan.h> |
| 14 | #include <net/ieee802154_netdev.h> |
| 15 | |
| 16 | #include "6lowpan_i.h" |
| 17 | |
Alexander Aring | 51e0e5d | 2015-08-10 21:15:53 +0200 | [diff] [blame] | 18 | static int lowpan_give_skb_to_device(struct sk_buff *skb, |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 19 | struct net_device *wdev) |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 20 | { |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 21 | skb->dev = wdev->ieee802154_ptr->lowpan_dev; |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 22 | skb->protocol = htons(ETH_P_IPV6); |
| 23 | skb->pkt_type = PACKET_HOST; |
| 24 | |
Alexander Aring | 51e0e5d | 2015-08-10 21:15:53 +0200 | [diff] [blame] | 25 | return netif_rx(skb); |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 26 | } |
| 27 | |
| 28 | static int |
| 29 | iphc_decompress(struct sk_buff *skb, const struct ieee802154_hdr *hdr) |
| 30 | { |
| 31 | u8 iphc0, iphc1; |
| 32 | struct ieee802154_addr_sa sa, da; |
| 33 | void *sap, *dap; |
| 34 | |
| 35 | raw_dump_table(__func__, "raw skb data dump", skb->data, skb->len); |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 36 | |
Alexander Aring | ad23d5b | 2015-09-02 14:21:22 +0200 | [diff] [blame] | 37 | if (lowpan_fetch_skb_u8(skb, &iphc0) || |
| 38 | lowpan_fetch_skb_u8(skb, &iphc1)) |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 39 | return -EINVAL; |
| 40 | |
| 41 | ieee802154_addr_to_sa(&sa, &hdr->source); |
| 42 | ieee802154_addr_to_sa(&da, &hdr->dest); |
| 43 | |
| 44 | if (sa.addr_type == IEEE802154_ADDR_SHORT) |
| 45 | sap = &sa.short_addr; |
| 46 | else |
| 47 | sap = &sa.hwaddr; |
| 48 | |
| 49 | if (da.addr_type == IEEE802154_ADDR_SHORT) |
| 50 | dap = &da.short_addr; |
| 51 | else |
| 52 | dap = &da.hwaddr; |
| 53 | |
| 54 | return lowpan_header_decompress(skb, skb->dev, sap, sa.addr_type, |
| 55 | IEEE802154_ADDR_LEN, dap, da.addr_type, |
| 56 | IEEE802154_ADDR_LEN, iphc0, iphc1); |
| 57 | } |
| 58 | |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 59 | static int lowpan_rcv(struct sk_buff *skb, struct net_device *wdev, |
| 60 | struct packet_type *pt, struct net_device *orig_wdev) |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 61 | { |
| 62 | struct ieee802154_hdr hdr; |
Alexander Aring | 989d433 | 2015-09-02 14:21:21 +0200 | [diff] [blame] | 63 | struct net_device *ldev; |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 64 | int ret; |
| 65 | |
Alexander Aring | 742c3af | 2015-09-02 14:21:23 +0200 | [diff] [blame^] | 66 | if (wdev->type != ARPHRD_IEEE802154 || |
| 67 | skb->pkt_type == PACKET_OTHERHOST) |
Alexander Aring | 989d433 | 2015-09-02 14:21:21 +0200 | [diff] [blame] | 68 | goto drop; |
| 69 | |
| 70 | ldev = wdev->ieee802154_ptr->lowpan_dev; |
| 71 | if (!ldev || !netif_running(ldev)) |
Alexander Aring | c0015bf | 2015-08-15 11:00:33 +0200 | [diff] [blame] | 72 | goto drop; |
| 73 | |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 74 | skb = skb_share_check(skb, GFP_ATOMIC); |
| 75 | if (!skb) |
| 76 | goto drop; |
| 77 | |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 78 | if (ieee802154_hdr_peek_addrs(skb, &hdr) < 0) |
| 79 | goto drop_skb; |
| 80 | |
| 81 | /* check that it's our buffer */ |
| 82 | if (skb->data[0] == LOWPAN_DISPATCH_IPV6) { |
| 83 | /* Pull off the 1-byte of 6lowpan header. */ |
| 84 | skb_pull(skb, 1); |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 85 | return lowpan_give_skb_to_device(skb, wdev); |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 86 | } else { |
| 87 | switch (skb->data[0] & 0xe0) { |
| 88 | case LOWPAN_DISPATCH_IPHC: /* ipv6 datagram */ |
| 89 | ret = iphc_decompress(skb, &hdr); |
| 90 | if (ret < 0) |
| 91 | goto drop_skb; |
| 92 | |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 93 | return lowpan_give_skb_to_device(skb, wdev); |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 94 | case LOWPAN_DISPATCH_FRAG1: /* first fragment header */ |
| 95 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAG1); |
| 96 | if (ret == 1) { |
| 97 | ret = iphc_decompress(skb, &hdr); |
| 98 | if (ret < 0) |
| 99 | goto drop_skb; |
| 100 | |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 101 | return lowpan_give_skb_to_device(skb, wdev); |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 102 | } else if (ret == -1) { |
| 103 | return NET_RX_DROP; |
| 104 | } else { |
| 105 | return NET_RX_SUCCESS; |
| 106 | } |
| 107 | case LOWPAN_DISPATCH_FRAGN: /* next fragments headers */ |
| 108 | ret = lowpan_frag_rcv(skb, LOWPAN_DISPATCH_FRAGN); |
| 109 | if (ret == 1) { |
| 110 | ret = iphc_decompress(skb, &hdr); |
| 111 | if (ret < 0) |
| 112 | goto drop_skb; |
| 113 | |
Alexander Aring | f460658 | 2015-09-02 14:21:16 +0200 | [diff] [blame] | 114 | return lowpan_give_skb_to_device(skb, wdev); |
Alexander Aring | 4662a0d | 2015-01-04 17:10:55 +0100 | [diff] [blame] | 115 | } else if (ret == -1) { |
| 116 | return NET_RX_DROP; |
| 117 | } else { |
| 118 | return NET_RX_SUCCESS; |
| 119 | } |
| 120 | default: |
| 121 | break; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | drop_skb: |
| 126 | kfree_skb(skb); |
| 127 | drop: |
| 128 | return NET_RX_DROP; |
| 129 | } |
| 130 | |
| 131 | static struct packet_type lowpan_packet_type = { |
| 132 | .type = htons(ETH_P_IEEE802154), |
| 133 | .func = lowpan_rcv, |
| 134 | }; |
| 135 | |
| 136 | void lowpan_rx_init(void) |
| 137 | { |
| 138 | dev_add_pack(&lowpan_packet_type); |
| 139 | } |
| 140 | |
| 141 | void lowpan_rx_exit(void) |
| 142 | { |
| 143 | dev_remove_pack(&lowpan_packet_type); |
| 144 | } |